1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
28 #include "coretypes.h"
31 #include "stor-layout.h"
32 #include "trans-mem.h"
35 #include "langhooks.h"
41 #include "tree-iterator.h"
43 #include "basic-block.h"
44 #include "gimple-expr.h"
46 #include "tree-inline.h"
48 #include "c-family/c-objc.h"
49 #include "c-family/c-common.h"
50 #include "c-family/c-ubsan.h"
54 /* Possible cases of implicit bad conversions. Used to select
55 diagnostic messages in convert_for_assignment. */
63 /* The level of nesting inside "__alignof__". */
66 /* The level of nesting inside "sizeof". */
69 /* The level of nesting inside "typeof". */
72 /* The argument of last parsed sizeof expression, only to be tested
73 if expr.original_code == SIZEOF_EXPR. */
74 tree c_last_sizeof_arg
;
76 /* Nonzero if we might need to print a "missing braces around
77 initializer" message within this initializer. */
78 static int found_missing_braces
;
80 static int require_constant_value
;
81 static int require_constant_elements
;
83 static bool null_pointer_constant_p (const_tree
);
84 static tree
qualify_type (tree
, tree
);
85 static int tagged_types_tu_compatible_p (const_tree
, const_tree
, bool *,
87 static int comp_target_types (location_t
, tree
, tree
);
88 static int function_types_compatible_p (const_tree
, const_tree
, bool *,
90 static int type_lists_compatible_p (const_tree
, const_tree
, bool *, bool *);
91 static tree
lookup_field (tree
, tree
);
92 static int convert_arguments (location_t
, vec
<location_t
>, tree
,
93 vec
<tree
, va_gc
> *, vec
<tree
, va_gc
> *, tree
,
95 static tree
pointer_diff (location_t
, tree
, tree
);
96 static tree
convert_for_assignment (location_t
, location_t
, tree
, tree
, tree
,
97 enum impl_conv
, bool, tree
, tree
, int);
98 static tree
valid_compound_expr_initializer (tree
, tree
);
99 static void push_string (const char *);
100 static void push_member_name (tree
);
101 static int spelling_length (void);
102 static char *print_spelling (char *);
103 static void warning_init (location_t
, int, const char *);
104 static tree
digest_init (location_t
, tree
, tree
, tree
, bool, bool, int);
105 static void output_init_element (location_t
, tree
, tree
, bool, tree
, tree
, int,
106 bool, struct obstack
*);
107 static void output_pending_init_elements (int, struct obstack
*);
108 static int set_designator (location_t
, int, struct obstack
*);
109 static void push_range_stack (tree
, struct obstack
*);
110 static void add_pending_init (location_t
, tree
, tree
, tree
, bool,
112 static void set_nonincremental_init (struct obstack
*);
113 static void set_nonincremental_init_from_string (tree
, struct obstack
*);
114 static tree
find_init_member (tree
, struct obstack
*);
115 static void readonly_warning (tree
, enum lvalue_use
);
116 static int lvalue_or_else (location_t
, const_tree
, enum lvalue_use
);
117 static void record_maybe_used_decl (tree
);
118 static int comptypes_internal (const_tree
, const_tree
, bool *, bool *);
120 /* Return true if EXP is a null pointer constant, false otherwise. */
123 null_pointer_constant_p (const_tree expr
)
125 /* This should really operate on c_expr structures, but they aren't
126 yet available everywhere required. */
127 tree type
= TREE_TYPE (expr
);
128 return (TREE_CODE (expr
) == INTEGER_CST
129 && !TREE_OVERFLOW (expr
)
130 && integer_zerop (expr
)
131 && (INTEGRAL_TYPE_P (type
)
132 || (TREE_CODE (type
) == POINTER_TYPE
133 && VOID_TYPE_P (TREE_TYPE (type
))
134 && TYPE_QUALS (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
)));
137 /* EXPR may appear in an unevaluated part of an integer constant
138 expression, but not in an evaluated part. Wrap it in a
139 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
140 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
143 note_integer_operands (tree expr
)
146 if (TREE_CODE (expr
) == INTEGER_CST
&& in_late_binary_op
)
148 ret
= copy_node (expr
);
149 TREE_OVERFLOW (ret
) = 1;
153 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL_TREE
, expr
);
154 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret
) = 1;
159 /* Having checked whether EXPR may appear in an unevaluated part of an
160 integer constant expression and found that it may, remove any
161 C_MAYBE_CONST_EXPR noting this fact and return the resulting
165 remove_c_maybe_const_expr (tree expr
)
167 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
168 return C_MAYBE_CONST_EXPR_EXPR (expr
);
173 \f/* This is a cache to hold if two types are compatible or not. */
175 struct tagged_tu_seen_cache
{
176 const struct tagged_tu_seen_cache
* next
;
179 /* The return value of tagged_types_tu_compatible_p if we had seen
180 these two types already. */
184 static const struct tagged_tu_seen_cache
* tagged_tu_seen_base
;
185 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*);
187 /* Do `exp = require_complete_type (exp);' to make sure exp
188 does not have an incomplete type. (That includes void types.) */
191 require_complete_type (tree value
)
193 tree type
= TREE_TYPE (value
);
195 if (value
== error_mark_node
|| type
== error_mark_node
)
196 return error_mark_node
;
198 /* First, detect a valid value with a complete type. */
199 if (COMPLETE_TYPE_P (type
))
202 c_incomplete_type_error (value
, type
);
203 return error_mark_node
;
206 /* Print an error message for invalid use of an incomplete type.
207 VALUE is the expression that was used (or 0 if that isn't known)
208 and TYPE is the type that was invalid. */
211 c_incomplete_type_error (const_tree value
, const_tree type
)
213 const char *type_code_string
;
215 /* Avoid duplicate error message. */
216 if (TREE_CODE (type
) == ERROR_MARK
)
219 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
220 || TREE_CODE (value
) == PARM_DECL
))
221 error ("%qD has an incomplete type", value
);
225 /* We must print an error message. Be clever about what it says. */
227 switch (TREE_CODE (type
))
230 type_code_string
= "struct";
234 type_code_string
= "union";
238 type_code_string
= "enum";
242 error ("invalid use of void expression");
246 if (TYPE_DOMAIN (type
))
248 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
250 error ("invalid use of flexible array member");
253 type
= TREE_TYPE (type
);
256 error ("invalid use of array with unspecified bounds");
263 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
264 error ("invalid use of undefined type %<%s %E%>",
265 type_code_string
, TYPE_NAME (type
));
267 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
268 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type
));
272 /* Given a type, apply default promotions wrt unnamed function
273 arguments and return the new type. */
276 c_type_promotes_to (tree type
)
278 tree ret
= NULL_TREE
;
280 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
281 ret
= double_type_node
;
282 else if (c_promoting_integer_type_p (type
))
284 /* Preserve unsignedness if not really getting any wider. */
285 if (TYPE_UNSIGNED (type
)
286 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
287 ret
= unsigned_type_node
;
289 ret
= integer_type_node
;
292 if (ret
!= NULL_TREE
)
293 return (TYPE_ATOMIC (type
)
294 ? c_build_qualified_type (ret
, TYPE_QUAL_ATOMIC
)
300 /* Return true if between two named address spaces, whether there is a superset
301 named address space that encompasses both address spaces. If there is a
302 superset, return which address space is the superset. */
305 addr_space_superset (addr_space_t as1
, addr_space_t as2
, addr_space_t
*common
)
312 else if (targetm
.addr_space
.subset_p (as1
, as2
))
317 else if (targetm
.addr_space
.subset_p (as2
, as1
))
326 /* Return a variant of TYPE which has all the type qualifiers of LIKE
327 as well as those of TYPE. */
330 qualify_type (tree type
, tree like
)
332 addr_space_t as_type
= TYPE_ADDR_SPACE (type
);
333 addr_space_t as_like
= TYPE_ADDR_SPACE (like
);
334 addr_space_t as_common
;
336 /* If the two named address spaces are different, determine the common
337 superset address space. If there isn't one, raise an error. */
338 if (!addr_space_superset (as_type
, as_like
, &as_common
))
341 error ("%qT and %qT are in disjoint named address spaces",
345 return c_build_qualified_type (type
,
346 TYPE_QUALS_NO_ADDR_SPACE (type
)
347 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like
)
348 | ENCODE_QUAL_ADDR_SPACE (as_common
));
351 /* Return true iff the given tree T is a variable length array. */
354 c_vla_type_p (const_tree t
)
356 if (TREE_CODE (t
) == ARRAY_TYPE
357 && C_TYPE_VARIABLE_SIZE (t
))
362 /* Return the composite type of two compatible types.
364 We assume that comptypes has already been done and returned
365 nonzero; if that isn't so, this may crash. In particular, we
366 assume that qualifiers match. */
369 composite_type (tree t1
, tree t2
)
371 enum tree_code code1
;
372 enum tree_code code2
;
375 /* Save time if the two types are the same. */
377 if (t1
== t2
) return t1
;
379 /* If one type is nonsense, use the other. */
380 if (t1
== error_mark_node
)
382 if (t2
== error_mark_node
)
385 code1
= TREE_CODE (t1
);
386 code2
= TREE_CODE (t2
);
388 /* Merge the attributes. */
389 attributes
= targetm
.merge_type_attributes (t1
, t2
);
391 /* If one is an enumerated type and the other is the compatible
392 integer type, the composite type might be either of the two
393 (DR#013 question 3). For consistency, use the enumerated type as
394 the composite type. */
396 if (code1
== ENUMERAL_TYPE
&& code2
== INTEGER_TYPE
)
398 if (code2
== ENUMERAL_TYPE
&& code1
== INTEGER_TYPE
)
401 gcc_assert (code1
== code2
);
406 /* For two pointers, do this recursively on the target type. */
408 tree pointed_to_1
= TREE_TYPE (t1
);
409 tree pointed_to_2
= TREE_TYPE (t2
);
410 tree target
= composite_type (pointed_to_1
, pointed_to_2
);
411 t1
= build_pointer_type_for_mode (target
, TYPE_MODE (t1
), false);
412 t1
= build_type_attribute_variant (t1
, attributes
);
413 return qualify_type (t1
, t2
);
418 tree elt
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
421 tree d1
= TYPE_DOMAIN (t1
);
422 tree d2
= TYPE_DOMAIN (t2
);
423 bool d1_variable
, d2_variable
;
424 bool d1_zero
, d2_zero
;
425 bool t1_complete
, t2_complete
;
427 /* We should not have any type quals on arrays at all. */
428 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1
)
429 && !TYPE_QUALS_NO_ADDR_SPACE (t2
));
431 t1_complete
= COMPLETE_TYPE_P (t1
);
432 t2_complete
= COMPLETE_TYPE_P (t2
);
434 d1_zero
= d1
== 0 || !TYPE_MAX_VALUE (d1
);
435 d2_zero
= d2
== 0 || !TYPE_MAX_VALUE (d2
);
437 d1_variable
= (!d1_zero
438 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
439 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
440 d2_variable
= (!d2_zero
441 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
442 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
443 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
444 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
446 /* Save space: see if the result is identical to one of the args. */
447 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
)
448 && (d2_variable
|| d2_zero
|| !d1_variable
))
449 return build_type_attribute_variant (t1
, attributes
);
450 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
)
451 && (d1_variable
|| d1_zero
|| !d2_variable
))
452 return build_type_attribute_variant (t2
, attributes
);
454 if (elt
== TREE_TYPE (t1
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
455 return build_type_attribute_variant (t1
, attributes
);
456 if (elt
== TREE_TYPE (t2
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
457 return build_type_attribute_variant (t2
, attributes
);
459 /* Merge the element types, and have a size if either arg has
460 one. We may have qualifiers on the element types. To set
461 up TYPE_MAIN_VARIANT correctly, we need to form the
462 composite of the unqualified types and add the qualifiers
464 quals
= TYPE_QUALS (strip_array_types (elt
));
465 unqual_elt
= c_build_qualified_type (elt
, TYPE_UNQUALIFIED
);
466 t1
= build_array_type (unqual_elt
,
467 TYPE_DOMAIN ((TYPE_DOMAIN (t1
)
473 /* Ensure a composite type involving a zero-length array type
474 is a zero-length type not an incomplete type. */
475 if (d1_zero
&& d2_zero
476 && (t1_complete
|| t2_complete
)
477 && !COMPLETE_TYPE_P (t1
))
479 TYPE_SIZE (t1
) = bitsize_zero_node
;
480 TYPE_SIZE_UNIT (t1
) = size_zero_node
;
482 t1
= c_build_qualified_type (t1
, quals
);
483 return build_type_attribute_variant (t1
, attributes
);
489 if (attributes
!= NULL
)
491 /* Try harder not to create a new aggregate type. */
492 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
494 if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
497 return build_type_attribute_variant (t1
, attributes
);
500 /* Function types: prefer the one that specified arg types.
501 If both do, merge the arg types. Also merge the return types. */
503 tree valtype
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
504 tree p1
= TYPE_ARG_TYPES (t1
);
505 tree p2
= TYPE_ARG_TYPES (t2
);
510 /* Save space: see if the result is identical to one of the args. */
511 if (valtype
== TREE_TYPE (t1
) && !TYPE_ARG_TYPES (t2
))
512 return build_type_attribute_variant (t1
, attributes
);
513 if (valtype
== TREE_TYPE (t2
) && !TYPE_ARG_TYPES (t1
))
514 return build_type_attribute_variant (t2
, attributes
);
516 /* Simple way if one arg fails to specify argument types. */
517 if (TYPE_ARG_TYPES (t1
) == 0)
519 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
520 t1
= build_type_attribute_variant (t1
, attributes
);
521 return qualify_type (t1
, t2
);
523 if (TYPE_ARG_TYPES (t2
) == 0)
525 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
526 t1
= build_type_attribute_variant (t1
, attributes
);
527 return qualify_type (t1
, t2
);
530 /* If both args specify argument types, we must merge the two
531 lists, argument by argument. */
533 len
= list_length (p1
);
536 for (i
= 0; i
< len
; i
++)
537 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
542 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
544 /* A null type means arg type is not specified.
545 Take whatever the other function type has. */
546 if (TREE_VALUE (p1
) == 0)
548 TREE_VALUE (n
) = TREE_VALUE (p2
);
551 if (TREE_VALUE (p2
) == 0)
553 TREE_VALUE (n
) = TREE_VALUE (p1
);
557 /* Given wait (union {union wait *u; int *i} *)
558 and wait (union wait *),
559 prefer union wait * as type of parm. */
560 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
561 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
564 tree mv2
= TREE_VALUE (p2
);
565 if (mv2
&& mv2
!= error_mark_node
566 && TREE_CODE (mv2
) != ARRAY_TYPE
)
567 mv2
= TYPE_MAIN_VARIANT (mv2
);
568 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
569 memb
; memb
= DECL_CHAIN (memb
))
571 tree mv3
= TREE_TYPE (memb
);
572 if (mv3
&& mv3
!= error_mark_node
573 && TREE_CODE (mv3
) != ARRAY_TYPE
)
574 mv3
= TYPE_MAIN_VARIANT (mv3
);
575 if (comptypes (mv3
, mv2
))
577 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
579 pedwarn (input_location
, OPT_Wpedantic
,
580 "function types not truly compatible in ISO C");
585 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
586 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
589 tree mv1
= TREE_VALUE (p1
);
590 if (mv1
&& mv1
!= error_mark_node
591 && TREE_CODE (mv1
) != ARRAY_TYPE
)
592 mv1
= TYPE_MAIN_VARIANT (mv1
);
593 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
594 memb
; memb
= DECL_CHAIN (memb
))
596 tree mv3
= TREE_TYPE (memb
);
597 if (mv3
&& mv3
!= error_mark_node
598 && TREE_CODE (mv3
) != ARRAY_TYPE
)
599 mv3
= TYPE_MAIN_VARIANT (mv3
);
600 if (comptypes (mv3
, mv1
))
602 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
604 pedwarn (input_location
, OPT_Wpedantic
,
605 "function types not truly compatible in ISO C");
610 TREE_VALUE (n
) = composite_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
614 t1
= build_function_type (valtype
, newargs
);
615 t1
= qualify_type (t1
, t2
);
616 /* ... falls through ... */
620 return build_type_attribute_variant (t1
, attributes
);
625 /* Return the type of a conditional expression between pointers to
626 possibly differently qualified versions of compatible types.
628 We assume that comp_target_types has already been done and returned
629 nonzero; if that isn't so, this may crash. */
632 common_pointer_type (tree t1
, tree t2
)
635 tree pointed_to_1
, mv1
;
636 tree pointed_to_2
, mv2
;
638 unsigned target_quals
;
639 addr_space_t as1
, as2
, as_common
;
642 /* Save time if the two types are the same. */
644 if (t1
== t2
) return t1
;
646 /* If one type is nonsense, use the other. */
647 if (t1
== error_mark_node
)
649 if (t2
== error_mark_node
)
652 gcc_assert (TREE_CODE (t1
) == POINTER_TYPE
653 && TREE_CODE (t2
) == POINTER_TYPE
);
655 /* Merge the attributes. */
656 attributes
= targetm
.merge_type_attributes (t1
, t2
);
658 /* Find the composite type of the target types, and combine the
659 qualifiers of the two types' targets. Do not lose qualifiers on
660 array element types by taking the TYPE_MAIN_VARIANT. */
661 mv1
= pointed_to_1
= TREE_TYPE (t1
);
662 mv2
= pointed_to_2
= TREE_TYPE (t2
);
663 if (TREE_CODE (mv1
) != ARRAY_TYPE
)
664 mv1
= TYPE_MAIN_VARIANT (pointed_to_1
);
665 if (TREE_CODE (mv2
) != ARRAY_TYPE
)
666 mv2
= TYPE_MAIN_VARIANT (pointed_to_2
);
667 target
= composite_type (mv1
, mv2
);
669 /* For function types do not merge const qualifiers, but drop them
670 if used inconsistently. The middle-end uses these to mark const
671 and noreturn functions. */
672 quals1
= TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1
);
673 quals2
= TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2
);
675 if (TREE_CODE (pointed_to_1
) == FUNCTION_TYPE
)
676 target_quals
= (quals1
& quals2
);
678 target_quals
= (quals1
| quals2
);
680 /* If the two named address spaces are different, determine the common
681 superset address space. This is guaranteed to exist due to the
682 assumption that comp_target_type returned non-zero. */
683 as1
= TYPE_ADDR_SPACE (pointed_to_1
);
684 as2
= TYPE_ADDR_SPACE (pointed_to_2
);
685 if (!addr_space_superset (as1
, as2
, &as_common
))
688 target_quals
|= ENCODE_QUAL_ADDR_SPACE (as_common
);
690 t1
= build_pointer_type (c_build_qualified_type (target
, target_quals
));
691 return build_type_attribute_variant (t1
, attributes
);
694 /* Return the common type for two arithmetic types under the usual
695 arithmetic conversions. The default conversions have already been
696 applied, and enumerated types converted to their compatible integer
697 types. The resulting type is unqualified and has no attributes.
699 This is the type for the result of most arithmetic operations
700 if the operands have the given two types. */
703 c_common_type (tree t1
, tree t2
)
705 enum tree_code code1
;
706 enum tree_code code2
;
708 /* If one type is nonsense, use the other. */
709 if (t1
== error_mark_node
)
711 if (t2
== error_mark_node
)
714 if (TYPE_QUALS (t1
) != TYPE_UNQUALIFIED
)
715 t1
= TYPE_MAIN_VARIANT (t1
);
717 if (TYPE_QUALS (t2
) != TYPE_UNQUALIFIED
)
718 t2
= TYPE_MAIN_VARIANT (t2
);
720 if (TYPE_ATTRIBUTES (t1
) != NULL_TREE
)
721 t1
= build_type_attribute_variant (t1
, NULL_TREE
);
723 if (TYPE_ATTRIBUTES (t2
) != NULL_TREE
)
724 t2
= build_type_attribute_variant (t2
, NULL_TREE
);
726 /* Save time if the two types are the same. */
728 if (t1
== t2
) return t1
;
730 code1
= TREE_CODE (t1
);
731 code2
= TREE_CODE (t2
);
733 gcc_assert (code1
== VECTOR_TYPE
|| code1
== COMPLEX_TYPE
734 || code1
== FIXED_POINT_TYPE
|| code1
== REAL_TYPE
735 || code1
== INTEGER_TYPE
);
736 gcc_assert (code2
== VECTOR_TYPE
|| code2
== COMPLEX_TYPE
737 || code2
== FIXED_POINT_TYPE
|| code2
== REAL_TYPE
738 || code2
== INTEGER_TYPE
);
740 /* When one operand is a decimal float type, the other operand cannot be
741 a generic float type or a complex type. We also disallow vector types
743 if ((DECIMAL_FLOAT_TYPE_P (t1
) || DECIMAL_FLOAT_TYPE_P (t2
))
744 && !(DECIMAL_FLOAT_TYPE_P (t1
) && DECIMAL_FLOAT_TYPE_P (t2
)))
746 if (code1
== VECTOR_TYPE
|| code2
== VECTOR_TYPE
)
748 error ("can%'t mix operands of decimal float and vector types");
749 return error_mark_node
;
751 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
753 error ("can%'t mix operands of decimal float and complex types");
754 return error_mark_node
;
756 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
758 error ("can%'t mix operands of decimal float and other float types");
759 return error_mark_node
;
763 /* If one type is a vector type, return that type. (How the usual
764 arithmetic conversions apply to the vector types extension is not
765 precisely specified.) */
766 if (code1
== VECTOR_TYPE
)
769 if (code2
== VECTOR_TYPE
)
772 /* If one type is complex, form the common type of the non-complex
773 components, then make that complex. Use T1 or T2 if it is the
775 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
777 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
778 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
779 tree subtype
= c_common_type (subtype1
, subtype2
);
781 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
783 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
786 return build_complex_type (subtype
);
789 /* If only one is real, use it as the result. */
791 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
794 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
797 /* If both are real and either are decimal floating point types, use
798 the decimal floating point type with the greater precision. */
800 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
802 if (TYPE_MAIN_VARIANT (t1
) == dfloat128_type_node
803 || TYPE_MAIN_VARIANT (t2
) == dfloat128_type_node
)
804 return dfloat128_type_node
;
805 else if (TYPE_MAIN_VARIANT (t1
) == dfloat64_type_node
806 || TYPE_MAIN_VARIANT (t2
) == dfloat64_type_node
)
807 return dfloat64_type_node
;
808 else if (TYPE_MAIN_VARIANT (t1
) == dfloat32_type_node
809 || TYPE_MAIN_VARIANT (t2
) == dfloat32_type_node
)
810 return dfloat32_type_node
;
813 /* Deal with fixed-point types. */
814 if (code1
== FIXED_POINT_TYPE
|| code2
== FIXED_POINT_TYPE
)
816 unsigned int unsignedp
= 0, satp
= 0;
817 enum machine_mode m1
, m2
;
818 unsigned int fbit1
, ibit1
, fbit2
, ibit2
, max_fbit
, max_ibit
;
823 /* If one input type is saturating, the result type is saturating. */
824 if (TYPE_SATURATING (t1
) || TYPE_SATURATING (t2
))
827 /* If both fixed-point types are unsigned, the result type is unsigned.
828 When mixing fixed-point and integer types, follow the sign of the
830 Otherwise, the result type is signed. */
831 if ((TYPE_UNSIGNED (t1
) && TYPE_UNSIGNED (t2
)
832 && code1
== FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
)
833 || (code1
== FIXED_POINT_TYPE
&& code2
!= FIXED_POINT_TYPE
834 && TYPE_UNSIGNED (t1
))
835 || (code1
!= FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
836 && TYPE_UNSIGNED (t2
)))
839 /* The result type is signed. */
842 /* If the input type is unsigned, we need to convert to the
844 if (code1
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t1
))
846 enum mode_class mclass
= (enum mode_class
) 0;
847 if (GET_MODE_CLASS (m1
) == MODE_UFRACT
)
849 else if (GET_MODE_CLASS (m1
) == MODE_UACCUM
)
853 m1
= mode_for_size (GET_MODE_PRECISION (m1
), mclass
, 0);
855 if (code2
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t2
))
857 enum mode_class mclass
= (enum mode_class
) 0;
858 if (GET_MODE_CLASS (m2
) == MODE_UFRACT
)
860 else if (GET_MODE_CLASS (m2
) == MODE_UACCUM
)
864 m2
= mode_for_size (GET_MODE_PRECISION (m2
), mclass
, 0);
868 if (code1
== FIXED_POINT_TYPE
)
870 fbit1
= GET_MODE_FBIT (m1
);
871 ibit1
= GET_MODE_IBIT (m1
);
876 /* Signed integers need to subtract one sign bit. */
877 ibit1
= TYPE_PRECISION (t1
) - (!TYPE_UNSIGNED (t1
));
880 if (code2
== FIXED_POINT_TYPE
)
882 fbit2
= GET_MODE_FBIT (m2
);
883 ibit2
= GET_MODE_IBIT (m2
);
888 /* Signed integers need to subtract one sign bit. */
889 ibit2
= TYPE_PRECISION (t2
) - (!TYPE_UNSIGNED (t2
));
892 max_ibit
= ibit1
>= ibit2
? ibit1
: ibit2
;
893 max_fbit
= fbit1
>= fbit2
? fbit1
: fbit2
;
894 return c_common_fixed_point_type_for_size (max_ibit
, max_fbit
, unsignedp
,
898 /* Both real or both integers; use the one with greater precision. */
900 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
902 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
905 /* Same precision. Prefer long longs to longs to ints when the
906 same precision, following the C99 rules on integer type rank
907 (which are equivalent to the C90 rules for C90 types). */
909 if (TYPE_MAIN_VARIANT (t1
) == long_long_unsigned_type_node
910 || TYPE_MAIN_VARIANT (t2
) == long_long_unsigned_type_node
)
911 return long_long_unsigned_type_node
;
913 if (TYPE_MAIN_VARIANT (t1
) == long_long_integer_type_node
914 || TYPE_MAIN_VARIANT (t2
) == long_long_integer_type_node
)
916 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
917 return long_long_unsigned_type_node
;
919 return long_long_integer_type_node
;
922 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
923 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
924 return long_unsigned_type_node
;
926 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
927 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
929 /* But preserve unsignedness from the other type,
930 since long cannot hold all the values of an unsigned int. */
931 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
932 return long_unsigned_type_node
;
934 return long_integer_type_node
;
937 /* Likewise, prefer long double to double even if same size. */
938 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
939 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
940 return long_double_type_node
;
942 /* Likewise, prefer double to float even if same size.
943 We got a couple of embedded targets with 32 bit doubles, and the
944 pdp11 might have 64 bit floats. */
945 if (TYPE_MAIN_VARIANT (t1
) == double_type_node
946 || TYPE_MAIN_VARIANT (t2
) == double_type_node
)
947 return double_type_node
;
949 /* Otherwise prefer the unsigned one. */
951 if (TYPE_UNSIGNED (t1
))
957 /* Wrapper around c_common_type that is used by c-common.c and other
958 front end optimizations that remove promotions. ENUMERAL_TYPEs
959 are allowed here and are converted to their compatible integer types.
960 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
961 preferably a non-Boolean type as the common type. */
963 common_type (tree t1
, tree t2
)
965 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
966 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
967 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
968 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
970 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
971 if (TREE_CODE (t1
) == BOOLEAN_TYPE
972 && TREE_CODE (t2
) == BOOLEAN_TYPE
)
973 return boolean_type_node
;
975 /* If either type is BOOLEAN_TYPE, then return the other. */
976 if (TREE_CODE (t1
) == BOOLEAN_TYPE
)
978 if (TREE_CODE (t2
) == BOOLEAN_TYPE
)
981 return c_common_type (t1
, t2
);
984 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
985 or various other operations. Return 2 if they are compatible
986 but a warning may be needed if you use them together. */
989 comptypes (tree type1
, tree type2
)
991 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
994 val
= comptypes_internal (type1
, type2
, NULL
, NULL
);
995 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1000 /* Like comptypes, but if it returns non-zero because enum and int are
1001 compatible, it sets *ENUM_AND_INT_P to true. */
1004 comptypes_check_enum_int (tree type1
, tree type2
, bool *enum_and_int_p
)
1006 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1009 val
= comptypes_internal (type1
, type2
, enum_and_int_p
, NULL
);
1010 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1015 /* Like comptypes, but if it returns nonzero for different types, it
1016 sets *DIFFERENT_TYPES_P to true. */
1019 comptypes_check_different_types (tree type1
, tree type2
,
1020 bool *different_types_p
)
1022 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1025 val
= comptypes_internal (type1
, type2
, NULL
, different_types_p
);
1026 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1031 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1032 or various other operations. Return 2 if they are compatible
1033 but a warning may be needed if you use them together. If
1034 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1035 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1036 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1037 NULL, and the types are compatible but different enough not to be
1038 permitted in C11 typedef redeclarations, then this sets
1039 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1040 false, but may or may not be set if the types are incompatible.
1041 This differs from comptypes, in that we don't free the seen
1045 comptypes_internal (const_tree type1
, const_tree type2
, bool *enum_and_int_p
,
1046 bool *different_types_p
)
1048 const_tree t1
= type1
;
1049 const_tree t2
= type2
;
1052 /* Suppress errors caused by previously reported errors. */
1054 if (t1
== t2
|| !t1
|| !t2
1055 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
1058 /* Enumerated types are compatible with integer types, but this is
1059 not transitive: two enumerated types in the same translation unit
1060 are compatible with each other only if they are the same type. */
1062 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
1064 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TYPE_UNSIGNED (t1
));
1065 if (TREE_CODE (t2
) != VOID_TYPE
)
1067 if (enum_and_int_p
!= NULL
)
1068 *enum_and_int_p
= true;
1069 if (different_types_p
!= NULL
)
1070 *different_types_p
= true;
1073 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
1075 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TYPE_UNSIGNED (t2
));
1076 if (TREE_CODE (t1
) != VOID_TYPE
)
1078 if (enum_and_int_p
!= NULL
)
1079 *enum_and_int_p
= true;
1080 if (different_types_p
!= NULL
)
1081 *different_types_p
= true;
1088 /* Different classes of types can't be compatible. */
1090 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1093 /* Qualifiers must match. C99 6.7.3p9 */
1095 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1098 /* Allow for two different type nodes which have essentially the same
1099 definition. Note that we already checked for equality of the type
1100 qualifiers (just above). */
1102 if (TREE_CODE (t1
) != ARRAY_TYPE
1103 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1106 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1107 if (!(attrval
= comp_type_attributes (t1
, t2
)))
1110 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1113 switch (TREE_CODE (t1
))
1116 /* Do not remove mode or aliasing information. */
1117 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1118 || TYPE_REF_CAN_ALIAS_ALL (t1
) != TYPE_REF_CAN_ALIAS_ALL (t2
))
1120 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
1121 ? 1 : comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1122 enum_and_int_p
, different_types_p
));
1126 val
= function_types_compatible_p (t1
, t2
, enum_and_int_p
,
1132 tree d1
= TYPE_DOMAIN (t1
);
1133 tree d2
= TYPE_DOMAIN (t2
);
1134 bool d1_variable
, d2_variable
;
1135 bool d1_zero
, d2_zero
;
1138 /* Target types must match incl. qualifiers. */
1139 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
1140 && 0 == (val
= comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1142 different_types_p
)))
1145 if (different_types_p
!= NULL
1146 && (d1
== 0) != (d2
== 0))
1147 *different_types_p
= true;
1148 /* Sizes must match unless one is missing or variable. */
1149 if (d1
== 0 || d2
== 0 || d1
== d2
)
1152 d1_zero
= !TYPE_MAX_VALUE (d1
);
1153 d2_zero
= !TYPE_MAX_VALUE (d2
);
1155 d1_variable
= (!d1_zero
1156 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
1157 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
1158 d2_variable
= (!d2_zero
1159 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
1160 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
1161 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
1162 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
1164 if (different_types_p
!= NULL
1165 && d1_variable
!= d2_variable
)
1166 *different_types_p
= true;
1167 if (d1_variable
|| d2_variable
)
1169 if (d1_zero
&& d2_zero
)
1171 if (d1_zero
|| d2_zero
1172 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
1173 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
1182 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
1184 tree a1
= TYPE_ATTRIBUTES (t1
);
1185 tree a2
= TYPE_ATTRIBUTES (t2
);
1187 if (! attribute_list_contained (a1
, a2
)
1188 && ! attribute_list_contained (a2
, a1
))
1192 return tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1194 val
= tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1200 val
= (TYPE_VECTOR_SUBPARTS (t1
) == TYPE_VECTOR_SUBPARTS (t2
)
1201 && comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1202 enum_and_int_p
, different_types_p
));
1208 return attrval
== 2 && val
== 1 ? 2 : val
;
1211 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1212 their qualifiers, except for named address spaces. If the pointers point to
1213 different named addresses, then we must determine if one address space is a
1214 subset of the other. */
1217 comp_target_types (location_t location
, tree ttl
, tree ttr
)
1220 tree mvl
= TREE_TYPE (ttl
);
1221 tree mvr
= TREE_TYPE (ttr
);
1222 addr_space_t asl
= TYPE_ADDR_SPACE (mvl
);
1223 addr_space_t asr
= TYPE_ADDR_SPACE (mvr
);
1224 addr_space_t as_common
;
1225 bool enum_and_int_p
;
1227 /* Fail if pointers point to incompatible address spaces. */
1228 if (!addr_space_superset (asl
, asr
, &as_common
))
1231 /* Do not lose qualifiers on element types of array types that are
1232 pointer targets by taking their TYPE_MAIN_VARIANT. */
1233 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
1234 mvl
= (TYPE_ATOMIC (mvl
)
1235 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
), TYPE_QUAL_ATOMIC
)
1236 : TYPE_MAIN_VARIANT (mvl
));
1237 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
1238 mvr
= (TYPE_ATOMIC (mvr
)
1239 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
), TYPE_QUAL_ATOMIC
)
1240 : TYPE_MAIN_VARIANT (mvr
));
1241 enum_and_int_p
= false;
1242 val
= comptypes_check_enum_int (mvl
, mvr
, &enum_and_int_p
);
1245 pedwarn (location
, OPT_Wpedantic
, "types are not quite compatible");
1247 if (val
== 1 && enum_and_int_p
&& warn_cxx_compat
)
1248 warning_at (location
, OPT_Wc___compat
,
1249 "pointer target types incompatible in C++");
1254 /* Subroutines of `comptypes'. */
1256 /* Determine whether two trees derive from the same translation unit.
1257 If the CONTEXT chain ends in a null, that tree's context is still
1258 being parsed, so if two trees have context chains ending in null,
1259 they're in the same translation unit. */
1261 same_translation_unit_p (const_tree t1
, const_tree t2
)
1263 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
1264 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
1266 case tcc_declaration
:
1267 t1
= DECL_CONTEXT (t1
); break;
1269 t1
= TYPE_CONTEXT (t1
); break;
1270 case tcc_exceptional
:
1271 t1
= BLOCK_SUPERCONTEXT (t1
); break; /* assume block */
1272 default: gcc_unreachable ();
1275 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
1276 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
1278 case tcc_declaration
:
1279 t2
= DECL_CONTEXT (t2
); break;
1281 t2
= TYPE_CONTEXT (t2
); break;
1282 case tcc_exceptional
:
1283 t2
= BLOCK_SUPERCONTEXT (t2
); break; /* assume block */
1284 default: gcc_unreachable ();
1290 /* Allocate the seen two types, assuming that they are compatible. */
1292 static struct tagged_tu_seen_cache
*
1293 alloc_tagged_tu_seen_cache (const_tree t1
, const_tree t2
)
1295 struct tagged_tu_seen_cache
*tu
= XNEW (struct tagged_tu_seen_cache
);
1296 tu
->next
= tagged_tu_seen_base
;
1300 tagged_tu_seen_base
= tu
;
1302 /* The C standard says that two structures in different translation
1303 units are compatible with each other only if the types of their
1304 fields are compatible (among other things). We assume that they
1305 are compatible until proven otherwise when building the cache.
1306 An example where this can occur is:
1311 If we are comparing this against a similar struct in another TU,
1312 and did not assume they were compatible, we end up with an infinite
1318 /* Free the seen types until we get to TU_TIL. */
1321 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*tu_til
)
1323 const struct tagged_tu_seen_cache
*tu
= tagged_tu_seen_base
;
1324 while (tu
!= tu_til
)
1326 const struct tagged_tu_seen_cache
*const tu1
1327 = (const struct tagged_tu_seen_cache
*) tu
;
1329 free (CONST_CAST (struct tagged_tu_seen_cache
*, tu1
));
1331 tagged_tu_seen_base
= tu_til
;
1334 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1335 compatible. If the two types are not the same (which has been
1336 checked earlier), this can only happen when multiple translation
1337 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1338 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1339 comptypes_internal. */
1342 tagged_types_tu_compatible_p (const_tree t1
, const_tree t2
,
1343 bool *enum_and_int_p
, bool *different_types_p
)
1346 bool needs_warning
= false;
1348 /* We have to verify that the tags of the types are the same. This
1349 is harder than it looks because this may be a typedef, so we have
1350 to go look at the original type. It may even be a typedef of a
1352 In the case of compiler-created builtin structs the TYPE_DECL
1353 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1354 while (TYPE_NAME (t1
)
1355 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1356 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
1357 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
1359 while (TYPE_NAME (t2
)
1360 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1361 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
1362 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
1364 /* C90 didn't have the requirement that the two tags be the same. */
1365 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
1368 /* C90 didn't say what happened if one or both of the types were
1369 incomplete; we choose to follow C99 rules here, which is that they
1371 if (TYPE_SIZE (t1
) == NULL
1372 || TYPE_SIZE (t2
) == NULL
)
1376 const struct tagged_tu_seen_cache
* tts_i
;
1377 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
1378 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
1382 switch (TREE_CODE (t1
))
1386 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1387 /* Speed up the case where the type values are in the same order. */
1388 tree tv1
= TYPE_VALUES (t1
);
1389 tree tv2
= TYPE_VALUES (t2
);
1396 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
1398 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
1400 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
1407 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
1411 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
1417 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
1423 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
1425 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
1427 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
1438 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1439 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
1445 /* Speed up the common case where the fields are in the same order. */
1446 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
); s1
&& s2
;
1447 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1451 if (DECL_NAME (s1
) != DECL_NAME (s2
))
1453 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1454 enum_and_int_p
, different_types_p
);
1456 if (result
!= 1 && !DECL_NAME (s1
))
1464 needs_warning
= true;
1466 if (TREE_CODE (s1
) == FIELD_DECL
1467 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1468 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1476 tu
->val
= needs_warning
? 2 : 1;
1480 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= DECL_CHAIN (s1
))
1484 for (s2
= TYPE_FIELDS (t2
); s2
; s2
= DECL_CHAIN (s2
))
1485 if (DECL_NAME (s1
) == DECL_NAME (s2
))
1489 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1493 if (result
!= 1 && !DECL_NAME (s1
))
1501 needs_warning
= true;
1503 if (TREE_CODE (s1
) == FIELD_DECL
1504 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1505 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1517 tu
->val
= needs_warning
? 2 : 10;
1523 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1525 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
1527 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1530 if (TREE_CODE (s1
) != TREE_CODE (s2
)
1531 || DECL_NAME (s1
) != DECL_NAME (s2
))
1533 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1534 enum_and_int_p
, different_types_p
);
1538 needs_warning
= true;
1540 if (TREE_CODE (s1
) == FIELD_DECL
1541 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1542 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1548 tu
->val
= needs_warning
? 2 : 1;
1557 /* Return 1 if two function types F1 and F2 are compatible.
1558 If either type specifies no argument types,
1559 the other must specify a fixed number of self-promoting arg types.
1560 Otherwise, if one type specifies only the number of arguments,
1561 the other must specify that number of self-promoting arg types.
1562 Otherwise, the argument types must match.
1563 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1566 function_types_compatible_p (const_tree f1
, const_tree f2
,
1567 bool *enum_and_int_p
, bool *different_types_p
)
1570 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1575 ret1
= TREE_TYPE (f1
);
1576 ret2
= TREE_TYPE (f2
);
1578 /* 'volatile' qualifiers on a function's return type used to mean
1579 the function is noreturn. */
1580 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
1581 pedwarn (input_location
, 0, "function return types not compatible due to %<volatile%>");
1582 if (TYPE_VOLATILE (ret1
))
1583 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
1584 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
1585 if (TYPE_VOLATILE (ret2
))
1586 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
1587 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
1588 val
= comptypes_internal (ret1
, ret2
, enum_and_int_p
, different_types_p
);
1592 args1
= TYPE_ARG_TYPES (f1
);
1593 args2
= TYPE_ARG_TYPES (f2
);
1595 if (different_types_p
!= NULL
1596 && (args1
== 0) != (args2
== 0))
1597 *different_types_p
= true;
1599 /* An unspecified parmlist matches any specified parmlist
1600 whose argument types don't need default promotions. */
1604 if (!self_promoting_args_p (args2
))
1606 /* If one of these types comes from a non-prototype fn definition,
1607 compare that with the other type's arglist.
1608 If they don't match, ask for a warning (but no error). */
1609 if (TYPE_ACTUAL_ARG_TYPES (f1
)
1610 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
1611 enum_and_int_p
, different_types_p
))
1617 if (!self_promoting_args_p (args1
))
1619 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1620 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
1621 enum_and_int_p
, different_types_p
))
1626 /* Both types have argument lists: compare them and propagate results. */
1627 val1
= type_lists_compatible_p (args1
, args2
, enum_and_int_p
,
1629 return val1
!= 1 ? val1
: val
;
1632 /* Check two lists of types for compatibility, returning 0 for
1633 incompatible, 1 for compatible, or 2 for compatible with
1634 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1635 comptypes_internal. */
1638 type_lists_compatible_p (const_tree args1
, const_tree args2
,
1639 bool *enum_and_int_p
, bool *different_types_p
)
1641 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1647 tree a1
, mv1
, a2
, mv2
;
1648 if (args1
== 0 && args2
== 0)
1650 /* If one list is shorter than the other,
1651 they fail to match. */
1652 if (args1
== 0 || args2
== 0)
1654 mv1
= a1
= TREE_VALUE (args1
);
1655 mv2
= a2
= TREE_VALUE (args2
);
1656 if (mv1
&& mv1
!= error_mark_node
&& TREE_CODE (mv1
) != ARRAY_TYPE
)
1657 mv1
= (TYPE_ATOMIC (mv1
)
1658 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1
),
1660 : TYPE_MAIN_VARIANT (mv1
));
1661 if (mv2
&& mv2
!= error_mark_node
&& TREE_CODE (mv2
) != ARRAY_TYPE
)
1662 mv2
= (TYPE_ATOMIC (mv2
)
1663 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2
),
1665 : TYPE_MAIN_VARIANT (mv2
));
1666 /* A null pointer instead of a type
1667 means there is supposed to be an argument
1668 but nothing is specified about what type it has.
1669 So match anything that self-promotes. */
1670 if (different_types_p
!= NULL
1671 && (a1
== 0) != (a2
== 0))
1672 *different_types_p
= true;
1675 if (c_type_promotes_to (a2
) != a2
)
1680 if (c_type_promotes_to (a1
) != a1
)
1683 /* If one of the lists has an error marker, ignore this arg. */
1684 else if (TREE_CODE (a1
) == ERROR_MARK
1685 || TREE_CODE (a2
) == ERROR_MARK
)
1687 else if (!(newval
= comptypes_internal (mv1
, mv2
, enum_and_int_p
,
1688 different_types_p
)))
1690 if (different_types_p
!= NULL
)
1691 *different_types_p
= true;
1692 /* Allow wait (union {union wait *u; int *i} *)
1693 and wait (union wait *) to be compatible. */
1694 if (TREE_CODE (a1
) == UNION_TYPE
1695 && (TYPE_NAME (a1
) == 0
1696 || TYPE_TRANSPARENT_AGGR (a1
))
1697 && TREE_CODE (TYPE_SIZE (a1
)) == INTEGER_CST
1698 && tree_int_cst_equal (TYPE_SIZE (a1
),
1702 for (memb
= TYPE_FIELDS (a1
);
1703 memb
; memb
= DECL_CHAIN (memb
))
1705 tree mv3
= TREE_TYPE (memb
);
1706 if (mv3
&& mv3
!= error_mark_node
1707 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1708 mv3
= (TYPE_ATOMIC (mv3
)
1709 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1711 : TYPE_MAIN_VARIANT (mv3
));
1712 if (comptypes_internal (mv3
, mv2
, enum_and_int_p
,
1719 else if (TREE_CODE (a2
) == UNION_TYPE
1720 && (TYPE_NAME (a2
) == 0
1721 || TYPE_TRANSPARENT_AGGR (a2
))
1722 && TREE_CODE (TYPE_SIZE (a2
)) == INTEGER_CST
1723 && tree_int_cst_equal (TYPE_SIZE (a2
),
1727 for (memb
= TYPE_FIELDS (a2
);
1728 memb
; memb
= DECL_CHAIN (memb
))
1730 tree mv3
= TREE_TYPE (memb
);
1731 if (mv3
&& mv3
!= error_mark_node
1732 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1733 mv3
= (TYPE_ATOMIC (mv3
)
1734 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1736 : TYPE_MAIN_VARIANT (mv3
));
1737 if (comptypes_internal (mv3
, mv1
, enum_and_int_p
,
1748 /* comptypes said ok, but record if it said to warn. */
1752 args1
= TREE_CHAIN (args1
);
1753 args2
= TREE_CHAIN (args2
);
1757 /* Compute the size to increment a pointer by. When a function type or void
1758 type or incomplete type is passed, size_one_node is returned.
1759 This function does not emit any diagnostics; the caller is responsible
1763 c_size_in_bytes (const_tree type
)
1765 enum tree_code code
= TREE_CODE (type
);
1767 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
1768 || !COMPLETE_TYPE_P (type
))
1769 return size_one_node
;
1771 /* Convert in case a char is more than one unit. */
1772 return size_binop_loc (input_location
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1773 size_int (TYPE_PRECISION (char_type_node
)
1777 /* Return either DECL or its known constant value (if it has one). */
1780 decl_constant_value (tree decl
)
1782 if (/* Don't change a variable array bound or initial value to a constant
1783 in a place where a variable is invalid. Note that DECL_INITIAL
1784 isn't valid for a PARM_DECL. */
1785 current_function_decl
!= 0
1786 && TREE_CODE (decl
) != PARM_DECL
1787 && !TREE_THIS_VOLATILE (decl
)
1788 && TREE_READONLY (decl
)
1789 && DECL_INITIAL (decl
) != 0
1790 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
1791 /* This is invalid if initial value is not constant.
1792 If it has either a function call, a memory reference,
1793 or a variable, then re-evaluating it could give different results. */
1794 && TREE_CONSTANT (DECL_INITIAL (decl
))
1795 /* Check for cases where this is sub-optimal, even though valid. */
1796 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1797 return DECL_INITIAL (decl
);
1801 /* Convert the array expression EXP to a pointer. */
1803 array_to_pointer_conversion (location_t loc
, tree exp
)
1805 tree orig_exp
= exp
;
1806 tree type
= TREE_TYPE (exp
);
1808 tree restype
= TREE_TYPE (type
);
1811 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1813 STRIP_TYPE_NOPS (exp
);
1815 if (TREE_NO_WARNING (orig_exp
))
1816 TREE_NO_WARNING (exp
) = 1;
1818 ptrtype
= build_pointer_type (restype
);
1820 if (TREE_CODE (exp
) == INDIRECT_REF
)
1821 return convert (ptrtype
, TREE_OPERAND (exp
, 0));
1823 /* In C++ array compound literals are temporary objects unless they are
1824 const or appear in namespace scope, so they are destroyed too soon
1825 to use them for much of anything (c++/53220). */
1826 if (warn_cxx_compat
&& TREE_CODE (exp
) == COMPOUND_LITERAL_EXPR
)
1828 tree decl
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
1829 if (!TREE_READONLY (decl
) && !TREE_STATIC (decl
))
1830 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
1831 "converting an array compound literal to a pointer "
1832 "is ill-formed in C++");
1835 adr
= build_unary_op (loc
, ADDR_EXPR
, exp
, 1);
1836 return convert (ptrtype
, adr
);
1839 /* Convert the function expression EXP to a pointer. */
1841 function_to_pointer_conversion (location_t loc
, tree exp
)
1843 tree orig_exp
= exp
;
1845 gcc_assert (TREE_CODE (TREE_TYPE (exp
)) == FUNCTION_TYPE
);
1847 STRIP_TYPE_NOPS (exp
);
1849 if (TREE_NO_WARNING (orig_exp
))
1850 TREE_NO_WARNING (exp
) = 1;
1852 return build_unary_op (loc
, ADDR_EXPR
, exp
, 0);
1855 /* Mark EXP as read, not just set, for set but not used -Wunused
1856 warning purposes. */
1859 mark_exp_read (tree exp
)
1861 switch (TREE_CODE (exp
))
1865 DECL_READ_P (exp
) = 1;
1874 mark_exp_read (TREE_OPERAND (exp
, 0));
1877 case C_MAYBE_CONST_EXPR
:
1878 mark_exp_read (TREE_OPERAND (exp
, 1));
1885 /* Perform the default conversion of arrays and functions to pointers.
1886 Return the result of converting EXP. For any other expression, just
1889 LOC is the location of the expression. */
1892 default_function_array_conversion (location_t loc
, struct c_expr exp
)
1894 tree orig_exp
= exp
.value
;
1895 tree type
= TREE_TYPE (exp
.value
);
1896 enum tree_code code
= TREE_CODE (type
);
1902 bool not_lvalue
= false;
1903 bool lvalue_array_p
;
1905 while ((TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
1906 || CONVERT_EXPR_P (exp
.value
))
1907 && TREE_TYPE (TREE_OPERAND (exp
.value
, 0)) == type
)
1909 if (TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
)
1911 exp
.value
= TREE_OPERAND (exp
.value
, 0);
1914 if (TREE_NO_WARNING (orig_exp
))
1915 TREE_NO_WARNING (exp
.value
) = 1;
1917 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
.value
);
1918 if (!flag_isoc99
&& !lvalue_array_p
)
1920 /* Before C99, non-lvalue arrays do not decay to pointers.
1921 Normally, using such an array would be invalid; but it can
1922 be used correctly inside sizeof or as a statement expression.
1923 Thus, do not give an error here; an error will result later. */
1927 exp
.value
= array_to_pointer_conversion (loc
, exp
.value
);
1931 exp
.value
= function_to_pointer_conversion (loc
, exp
.value
);
1941 default_function_array_read_conversion (location_t loc
, struct c_expr exp
)
1943 mark_exp_read (exp
.value
);
1944 return default_function_array_conversion (loc
, exp
);
1947 /* Return whether EXPR should be treated as an atomic lvalue for the
1948 purposes of load and store handling. */
1951 really_atomic_lvalue (tree expr
)
1953 if (expr
== error_mark_node
|| TREE_TYPE (expr
) == error_mark_node
)
1955 if (!TYPE_ATOMIC (TREE_TYPE (expr
)))
1957 if (!lvalue_p (expr
))
1960 /* Ignore _Atomic on register variables, since their addresses can't
1961 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1962 sequences wouldn't work. Ignore _Atomic on structures containing
1963 bit-fields, since accessing elements of atomic structures or
1964 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1965 it's undefined at translation time or execution time, and the
1966 normal atomic sequences again wouldn't work. */
1967 while (handled_component_p (expr
))
1969 if (TREE_CODE (expr
) == COMPONENT_REF
1970 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
1972 expr
= TREE_OPERAND (expr
, 0);
1974 if (DECL_P (expr
) && C_DECL_REGISTER (expr
))
1979 /* Convert expression EXP (location LOC) from lvalue to rvalue,
1980 including converting functions and arrays to pointers if CONVERT_P.
1981 If READ_P, also mark the expression as having been read. */
1984 convert_lvalue_to_rvalue (location_t loc
, struct c_expr exp
,
1985 bool convert_p
, bool read_p
)
1988 mark_exp_read (exp
.value
);
1990 exp
= default_function_array_conversion (loc
, exp
);
1991 if (really_atomic_lvalue (exp
.value
))
1993 vec
<tree
, va_gc
> *params
;
1994 tree nonatomic_type
, tmp
, tmp_addr
, fndecl
, func_call
;
1995 tree expr_type
= TREE_TYPE (exp
.value
);
1996 tree expr_addr
= build_unary_op (loc
, ADDR_EXPR
, exp
.value
, 0);
1997 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
1999 gcc_assert (TYPE_ATOMIC (expr_type
));
2001 /* Expansion of a generic atomic load may require an addition
2002 element, so allocate enough to prevent a resize. */
2003 vec_alloc (params
, 4);
2005 /* Remove the qualifiers for the rest of the expressions and
2006 create the VAL temp variable to hold the RHS. */
2007 nonatomic_type
= build_qualified_type (expr_type
, TYPE_UNQUALIFIED
);
2008 tmp
= create_tmp_var (nonatomic_type
, NULL
);
2009 tmp_addr
= build_unary_op (loc
, ADDR_EXPR
, tmp
, 0);
2010 TREE_ADDRESSABLE (tmp
) = 1;
2011 TREE_NO_WARNING (tmp
) = 1;
2013 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2014 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
2015 params
->quick_push (expr_addr
);
2016 params
->quick_push (tmp_addr
);
2017 params
->quick_push (seq_cst
);
2018 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
2020 /* EXPR is always read. */
2021 mark_exp_read (exp
.value
);
2023 /* Return tmp which contains the value loaded. */
2024 exp
.value
= build2 (COMPOUND_EXPR
, nonatomic_type
, func_call
, tmp
);
2029 /* EXP is an expression of integer type. Apply the integer promotions
2030 to it and return the promoted value. */
2033 perform_integral_promotions (tree exp
)
2035 tree type
= TREE_TYPE (exp
);
2036 enum tree_code code
= TREE_CODE (type
);
2038 gcc_assert (INTEGRAL_TYPE_P (type
));
2040 /* Normally convert enums to int,
2041 but convert wide enums to something wider. */
2042 if (code
== ENUMERAL_TYPE
)
2044 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
2045 TYPE_PRECISION (integer_type_node
)),
2046 ((TYPE_PRECISION (type
)
2047 >= TYPE_PRECISION (integer_type_node
))
2048 && TYPE_UNSIGNED (type
)));
2050 return convert (type
, exp
);
2053 /* ??? This should no longer be needed now bit-fields have their
2055 if (TREE_CODE (exp
) == COMPONENT_REF
2056 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
2057 /* If it's thinner than an int, promote it like a
2058 c_promoting_integer_type_p, otherwise leave it alone. */
2059 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
2060 TYPE_PRECISION (integer_type_node
)))
2061 return convert (integer_type_node
, exp
);
2063 if (c_promoting_integer_type_p (type
))
2065 /* Preserve unsignedness if not really getting any wider. */
2066 if (TYPE_UNSIGNED (type
)
2067 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
2068 return convert (unsigned_type_node
, exp
);
2070 return convert (integer_type_node
, exp
);
2077 /* Perform default promotions for C data used in expressions.
2078 Enumeral types or short or char are converted to int.
2079 In addition, manifest constants symbols are replaced by their values. */
2082 default_conversion (tree exp
)
2085 tree type
= TREE_TYPE (exp
);
2086 enum tree_code code
= TREE_CODE (type
);
2089 mark_exp_read (exp
);
2091 /* Functions and arrays have been converted during parsing. */
2092 gcc_assert (code
!= FUNCTION_TYPE
);
2093 if (code
== ARRAY_TYPE
)
2096 /* Constants can be used directly unless they're not loadable. */
2097 if (TREE_CODE (exp
) == CONST_DECL
)
2098 exp
= DECL_INITIAL (exp
);
2100 /* Strip no-op conversions. */
2102 STRIP_TYPE_NOPS (exp
);
2104 if (TREE_NO_WARNING (orig_exp
))
2105 TREE_NO_WARNING (exp
) = 1;
2107 if (code
== VOID_TYPE
)
2109 error_at (EXPR_LOC_OR_LOC (exp
, input_location
),
2110 "void value not ignored as it ought to be");
2111 return error_mark_node
;
2114 exp
= require_complete_type (exp
);
2115 if (exp
== error_mark_node
)
2116 return error_mark_node
;
2118 promoted_type
= targetm
.promoted_type (type
);
2120 return convert (promoted_type
, exp
);
2122 if (INTEGRAL_TYPE_P (type
))
2123 return perform_integral_promotions (exp
);
2128 /* Look up COMPONENT in a structure or union TYPE.
2130 If the component name is not found, returns NULL_TREE. Otherwise,
2131 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2132 stepping down the chain to the component, which is in the last
2133 TREE_VALUE of the list. Normally the list is of length one, but if
2134 the component is embedded within (nested) anonymous structures or
2135 unions, the list steps down the chain to the component. */
2138 lookup_field (tree type
, tree component
)
2142 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2143 to the field elements. Use a binary search on this array to quickly
2144 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2145 will always be set for structures which have many elements. */
2147 if (TYPE_LANG_SPECIFIC (type
) && TYPE_LANG_SPECIFIC (type
)->s
)
2150 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
2152 field
= TYPE_FIELDS (type
);
2154 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
2155 while (top
- bot
> 1)
2157 half
= (top
- bot
+ 1) >> 1;
2158 field
= field_array
[bot
+half
];
2160 if (DECL_NAME (field
) == NULL_TREE
)
2162 /* Step through all anon unions in linear fashion. */
2163 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
2165 field
= field_array
[bot
++];
2166 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
2167 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
2169 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2172 return tree_cons (NULL_TREE
, field
, anon
);
2174 /* The Plan 9 compiler permits referring
2175 directly to an anonymous struct/union field
2176 using a typedef name. */
2177 if (flag_plan9_extensions
2178 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2179 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field
)))
2181 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2187 /* Entire record is only anon unions. */
2191 /* Restart the binary search, with new lower bound. */
2195 if (DECL_NAME (field
) == component
)
2197 if (DECL_NAME (field
) < component
)
2203 if (DECL_NAME (field_array
[bot
]) == component
)
2204 field
= field_array
[bot
];
2205 else if (DECL_NAME (field
) != component
)
2210 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2212 if (DECL_NAME (field
) == NULL_TREE
2213 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
2214 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
))
2216 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2219 return tree_cons (NULL_TREE
, field
, anon
);
2221 /* The Plan 9 compiler permits referring directly to an
2222 anonymous struct/union field using a typedef
2224 if (flag_plan9_extensions
2225 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2226 && TREE_CODE (TYPE_NAME (TREE_TYPE (field
))) == TYPE_DECL
2227 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2232 if (DECL_NAME (field
) == component
)
2236 if (field
== NULL_TREE
)
2240 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
2243 /* Make an expression to refer to the COMPONENT field of structure or
2244 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2245 location of the COMPONENT_REF. */
2248 build_component_ref (location_t loc
, tree datum
, tree component
)
2250 tree type
= TREE_TYPE (datum
);
2251 enum tree_code code
= TREE_CODE (type
);
2254 bool datum_lvalue
= lvalue_p (datum
);
2256 if (!objc_is_public (datum
, component
))
2257 return error_mark_node
;
2259 /* Detect Objective-C property syntax object.property. */
2260 if (c_dialect_objc ()
2261 && (ref
= objc_maybe_build_component_ref (datum
, component
)))
2264 /* See if there is a field or component with name COMPONENT. */
2266 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2268 if (!COMPLETE_TYPE_P (type
))
2270 c_incomplete_type_error (NULL_TREE
, type
);
2271 return error_mark_node
;
2274 field
= lookup_field (type
, component
);
2278 error_at (loc
, "%qT has no member named %qE", type
, component
);
2279 return error_mark_node
;
2282 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2283 This might be better solved in future the way the C++ front
2284 end does it - by giving the anonymous entities each a
2285 separate name and type, and then have build_component_ref
2286 recursively call itself. We can't do that here. */
2289 tree subdatum
= TREE_VALUE (field
);
2292 bool use_datum_quals
;
2294 if (TREE_TYPE (subdatum
) == error_mark_node
)
2295 return error_mark_node
;
2297 /* If this is an rvalue, it does not have qualifiers in C
2298 standard terms and we must avoid propagating such
2299 qualifiers down to a non-lvalue array that is then
2300 converted to a pointer. */
2301 use_datum_quals
= (datum_lvalue
2302 || TREE_CODE (TREE_TYPE (subdatum
)) != ARRAY_TYPE
);
2304 quals
= TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum
)));
2305 if (use_datum_quals
)
2306 quals
|= TYPE_QUALS (TREE_TYPE (datum
));
2307 subtype
= c_build_qualified_type (TREE_TYPE (subdatum
), quals
);
2309 ref
= build3 (COMPONENT_REF
, subtype
, datum
, subdatum
,
2311 SET_EXPR_LOCATION (ref
, loc
);
2312 if (TREE_READONLY (subdatum
)
2313 || (use_datum_quals
&& TREE_READONLY (datum
)))
2314 TREE_READONLY (ref
) = 1;
2315 if (TREE_THIS_VOLATILE (subdatum
)
2316 || (use_datum_quals
&& TREE_THIS_VOLATILE (datum
)))
2317 TREE_THIS_VOLATILE (ref
) = 1;
2319 if (TREE_DEPRECATED (subdatum
))
2320 warn_deprecated_use (subdatum
, NULL_TREE
);
2324 field
= TREE_CHAIN (field
);
2330 else if (code
!= ERROR_MARK
)
2332 "request for member %qE in something not a structure or union",
2335 return error_mark_node
;
2338 /* Given an expression PTR for a pointer, return an expression
2339 for the value pointed to.
2340 ERRORSTRING is the name of the operator to appear in error messages.
2342 LOC is the location to use for the generated tree. */
2345 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errstring
)
2347 tree pointer
= default_conversion (ptr
);
2348 tree type
= TREE_TYPE (pointer
);
2351 if (TREE_CODE (type
) == POINTER_TYPE
)
2353 if (CONVERT_EXPR_P (pointer
)
2354 || TREE_CODE (pointer
) == VIEW_CONVERT_EXPR
)
2356 /* If a warning is issued, mark it to avoid duplicates from
2357 the backend. This only needs to be done at
2358 warn_strict_aliasing > 2. */
2359 if (warn_strict_aliasing
> 2)
2360 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer
, 0)),
2361 type
, TREE_OPERAND (pointer
, 0)))
2362 TREE_NO_WARNING (pointer
) = 1;
2365 if (TREE_CODE (pointer
) == ADDR_EXPR
2366 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
2367 == TREE_TYPE (type
)))
2369 ref
= TREE_OPERAND (pointer
, 0);
2370 protected_set_expr_location (ref
, loc
);
2375 tree t
= TREE_TYPE (type
);
2377 ref
= build1 (INDIRECT_REF
, t
, pointer
);
2379 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
2381 error_at (loc
, "dereferencing pointer to incomplete type");
2382 return error_mark_node
;
2384 if (VOID_TYPE_P (t
) && c_inhibit_evaluation_warnings
== 0)
2385 warning_at (loc
, 0, "dereferencing %<void *%> pointer");
2387 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2388 so that we get the proper error message if the result is used
2389 to assign to. Also, &* is supposed to be a no-op.
2390 And ANSI C seems to specify that the type of the result
2391 should be the const type. */
2392 /* A de-reference of a pointer to const is not a const. It is valid
2393 to change it via some other pointer. */
2394 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2395 TREE_SIDE_EFFECTS (ref
)
2396 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
2397 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2398 protected_set_expr_location (ref
, loc
);
2402 else if (TREE_CODE (pointer
) != ERROR_MARK
)
2403 invalid_indirection_error (loc
, type
, errstring
);
2405 return error_mark_node
;
2408 /* This handles expressions of the form "a[i]", which denotes
2411 This is logically equivalent in C to *(a+i), but we may do it differently.
2412 If A is a variable or a member, we generate a primitive ARRAY_REF.
2413 This avoids forcing the array out of registers, and can work on
2414 arrays that are not lvalues (for example, members of structures returned
2417 For vector types, allow vector[i] but not i[vector], and create
2418 *(((type*)&vectortype) + i) for the expression.
2420 LOC is the location to use for the returned expression. */
2423 build_array_ref (location_t loc
, tree array
, tree index
)
2426 bool swapped
= false;
2427 if (TREE_TYPE (array
) == error_mark_node
2428 || TREE_TYPE (index
) == error_mark_node
)
2429 return error_mark_node
;
2431 if (flag_cilkplus
&& contains_array_notation_expr (index
))
2434 if (!find_rank (loc
, index
, index
, true, &rank
))
2435 return error_mark_node
;
2438 error_at (loc
, "rank of the array's index is greater than 1");
2439 return error_mark_node
;
2442 if (TREE_CODE (TREE_TYPE (array
)) != ARRAY_TYPE
2443 && TREE_CODE (TREE_TYPE (array
)) != POINTER_TYPE
2444 /* Allow vector[index] but not index[vector]. */
2445 && TREE_CODE (TREE_TYPE (array
)) != VECTOR_TYPE
)
2448 if (TREE_CODE (TREE_TYPE (index
)) != ARRAY_TYPE
2449 && TREE_CODE (TREE_TYPE (index
)) != POINTER_TYPE
)
2452 "subscripted value is neither array nor pointer nor vector");
2454 return error_mark_node
;
2462 if (!INTEGRAL_TYPE_P (TREE_TYPE (index
)))
2464 error_at (loc
, "array subscript is not an integer");
2465 return error_mark_node
;
2468 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array
))) == FUNCTION_TYPE
)
2470 error_at (loc
, "subscripted value is pointer to function");
2471 return error_mark_node
;
2474 /* ??? Existing practice has been to warn only when the char
2475 index is syntactically the index, not for char[array]. */
2477 warn_array_subscript_with_type_char (index
);
2479 /* Apply default promotions *after* noticing character types. */
2480 index
= default_conversion (index
);
2482 gcc_assert (TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
);
2484 convert_vector_to_pointer_for_subscript (loc
, &array
, index
);
2486 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
2490 /* An array that is indexed by a non-constant
2491 cannot be stored in a register; we must be able to do
2492 address arithmetic on its address.
2493 Likewise an array of elements of variable size. */
2494 if (TREE_CODE (index
) != INTEGER_CST
2495 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2496 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2498 if (!c_mark_addressable (array
))
2499 return error_mark_node
;
2501 /* An array that is indexed by a constant value which is not within
2502 the array bounds cannot be stored in a register either; because we
2503 would get a crash in store_bit_field/extract_bit_field when trying
2504 to access a non-existent part of the register. */
2505 if (TREE_CODE (index
) == INTEGER_CST
2506 && TYPE_DOMAIN (TREE_TYPE (array
))
2507 && !int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
2509 if (!c_mark_addressable (array
))
2510 return error_mark_node
;
2513 if (pedantic
|| warn_c90_c99_compat
)
2516 while (TREE_CODE (foo
) == COMPONENT_REF
)
2517 foo
= TREE_OPERAND (foo
, 0);
2518 if (TREE_CODE (foo
) == VAR_DECL
&& C_DECL_REGISTER (foo
))
2519 pedwarn (loc
, OPT_Wpedantic
,
2520 "ISO C forbids subscripting %<register%> array");
2521 else if (!lvalue_p (foo
))
2522 pedwarn_c90 (loc
, OPT_Wpedantic
,
2523 "ISO C90 forbids subscripting non-lvalue "
2527 type
= TREE_TYPE (TREE_TYPE (array
));
2528 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
2529 /* Array ref is const/volatile if the array elements are
2530 or if the array is. */
2531 TREE_READONLY (rval
)
2532 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2533 | TREE_READONLY (array
));
2534 TREE_SIDE_EFFECTS (rval
)
2535 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2536 | TREE_SIDE_EFFECTS (array
));
2537 TREE_THIS_VOLATILE (rval
)
2538 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2539 /* This was added by rms on 16 Nov 91.
2540 It fixes vol struct foo *a; a->elts[1]
2541 in an inline function.
2542 Hope it doesn't break something else. */
2543 | TREE_THIS_VOLATILE (array
));
2544 ret
= require_complete_type (rval
);
2545 protected_set_expr_location (ret
, loc
);
2550 tree ar
= default_conversion (array
);
2552 if (ar
== error_mark_node
)
2555 gcc_assert (TREE_CODE (TREE_TYPE (ar
)) == POINTER_TYPE
);
2556 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) != FUNCTION_TYPE
);
2558 return build_indirect_ref
2559 (loc
, build_binary_op (loc
, PLUS_EXPR
, ar
, index
, 0),
2564 /* Build an external reference to identifier ID. FUN indicates
2565 whether this will be used for a function call. LOC is the source
2566 location of the identifier. This sets *TYPE to the type of the
2567 identifier, which is not the same as the type of the returned value
2568 for CONST_DECLs defined as enum constants. If the type of the
2569 identifier is not available, *TYPE is set to NULL. */
2571 build_external_ref (location_t loc
, tree id
, int fun
, tree
*type
)
2574 tree decl
= lookup_name (id
);
2576 /* In Objective-C, an instance variable (ivar) may be preferred to
2577 whatever lookup_name() found. */
2578 decl
= objc_lookup_ivar (decl
, id
);
2581 if (decl
&& decl
!= error_mark_node
)
2584 *type
= TREE_TYPE (ref
);
2587 /* Implicit function declaration. */
2588 ref
= implicitly_declare (loc
, id
);
2589 else if (decl
== error_mark_node
)
2590 /* Don't complain about something that's already been
2591 complained about. */
2592 return error_mark_node
;
2595 undeclared_variable (loc
, id
);
2596 return error_mark_node
;
2599 if (TREE_TYPE (ref
) == error_mark_node
)
2600 return error_mark_node
;
2602 if (TREE_DEPRECATED (ref
))
2603 warn_deprecated_use (ref
, NULL_TREE
);
2605 /* Recursive call does not count as usage. */
2606 if (ref
!= current_function_decl
)
2608 TREE_USED (ref
) = 1;
2611 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
2613 if (!in_sizeof
&& !in_typeof
)
2614 C_DECL_USED (ref
) = 1;
2615 else if (DECL_INITIAL (ref
) == 0
2616 && DECL_EXTERNAL (ref
)
2617 && !TREE_PUBLIC (ref
))
2618 record_maybe_used_decl (ref
);
2621 if (TREE_CODE (ref
) == CONST_DECL
)
2623 used_types_insert (TREE_TYPE (ref
));
2626 && TREE_CODE (TREE_TYPE (ref
)) == ENUMERAL_TYPE
2627 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref
)))
2629 warning_at (loc
, OPT_Wc___compat
,
2630 ("enum constant defined in struct or union "
2631 "is not visible in C++"));
2632 inform (DECL_SOURCE_LOCATION (ref
), "enum constant defined here");
2635 ref
= DECL_INITIAL (ref
);
2636 TREE_CONSTANT (ref
) = 1;
2638 else if (current_function_decl
!= 0
2639 && !DECL_FILE_SCOPE_P (current_function_decl
)
2640 && (TREE_CODE (ref
) == VAR_DECL
2641 || TREE_CODE (ref
) == PARM_DECL
2642 || TREE_CODE (ref
) == FUNCTION_DECL
))
2644 tree context
= decl_function_context (ref
);
2646 if (context
!= 0 && context
!= current_function_decl
)
2647 DECL_NONLOCAL (ref
) = 1;
2649 /* C99 6.7.4p3: An inline definition of a function with external
2650 linkage ... shall not contain a reference to an identifier with
2651 internal linkage. */
2652 else if (current_function_decl
!= 0
2653 && DECL_DECLARED_INLINE_P (current_function_decl
)
2654 && DECL_EXTERNAL (current_function_decl
)
2655 && VAR_OR_FUNCTION_DECL_P (ref
)
2656 && (TREE_CODE (ref
) != VAR_DECL
|| TREE_STATIC (ref
))
2657 && ! TREE_PUBLIC (ref
)
2658 && DECL_CONTEXT (ref
) != current_function_decl
)
2659 record_inline_static (loc
, current_function_decl
, ref
,
2665 /* Record details of decls possibly used inside sizeof or typeof. */
2666 struct maybe_used_decl
2670 /* The level seen at (in_sizeof + in_typeof). */
2672 /* The next one at this level or above, or NULL. */
2673 struct maybe_used_decl
*next
;
2676 static struct maybe_used_decl
*maybe_used_decls
;
2678 /* Record that DECL, an undefined static function reference seen
2679 inside sizeof or typeof, might be used if the operand of sizeof is
2680 a VLA type or the operand of typeof is a variably modified
2684 record_maybe_used_decl (tree decl
)
2686 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
2688 t
->level
= in_sizeof
+ in_typeof
;
2689 t
->next
= maybe_used_decls
;
2690 maybe_used_decls
= t
;
2693 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2694 USED is false, just discard them. If it is true, mark them used
2695 (if no longer inside sizeof or typeof) or move them to the next
2696 level up (if still inside sizeof or typeof). */
2699 pop_maybe_used (bool used
)
2701 struct maybe_used_decl
*p
= maybe_used_decls
;
2702 int cur_level
= in_sizeof
+ in_typeof
;
2703 while (p
&& p
->level
> cur_level
)
2708 C_DECL_USED (p
->decl
) = 1;
2710 p
->level
= cur_level
;
2714 if (!used
|| cur_level
== 0)
2715 maybe_used_decls
= p
;
2718 /* Return the result of sizeof applied to EXPR. */
2721 c_expr_sizeof_expr (location_t loc
, struct c_expr expr
)
2724 if (expr
.value
== error_mark_node
)
2726 ret
.value
= error_mark_node
;
2727 ret
.original_code
= ERROR_MARK
;
2728 ret
.original_type
= NULL
;
2729 pop_maybe_used (false);
2733 bool expr_const_operands
= true;
2735 if (TREE_CODE (expr
.value
) == PARM_DECL
2736 && C_ARRAY_PARAMETER (expr
.value
))
2738 if (warning_at (loc
, OPT_Wsizeof_array_argument
,
2739 "%<sizeof%> on array function parameter %qE will "
2740 "return size of %qT", expr
.value
,
2741 expr
.original_type
))
2742 inform (DECL_SOURCE_LOCATION (expr
.value
), "declared here");
2744 tree folded_expr
= c_fully_fold (expr
.value
, require_constant_value
,
2745 &expr_const_operands
);
2746 ret
.value
= c_sizeof (loc
, TREE_TYPE (folded_expr
));
2747 c_last_sizeof_arg
= expr
.value
;
2748 ret
.original_code
= SIZEOF_EXPR
;
2749 ret
.original_type
= NULL
;
2750 if (c_vla_type_p (TREE_TYPE (folded_expr
)))
2752 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2753 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2754 folded_expr
, ret
.value
);
2755 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !expr_const_operands
;
2756 SET_EXPR_LOCATION (ret
.value
, loc
);
2758 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr
)));
2763 /* Return the result of sizeof applied to T, a structure for the type
2764 name passed to sizeof (rather than the type itself). LOC is the
2765 location of the original expression. */
2768 c_expr_sizeof_type (location_t loc
, struct c_type_name
*t
)
2772 tree type_expr
= NULL_TREE
;
2773 bool type_expr_const
= true;
2774 type
= groktypename (t
, &type_expr
, &type_expr_const
);
2775 ret
.value
= c_sizeof (loc
, type
);
2776 c_last_sizeof_arg
= type
;
2777 ret
.original_code
= SIZEOF_EXPR
;
2778 ret
.original_type
= NULL
;
2779 if ((type_expr
|| TREE_CODE (ret
.value
) == INTEGER_CST
)
2780 && c_vla_type_p (type
))
2782 /* If the type is a [*] array, it is a VLA but is represented as
2783 having a size of zero. In such a case we must ensure that
2784 the result of sizeof does not get folded to a constant by
2785 c_fully_fold, because if the size is evaluated the result is
2786 not constant and so constraints on zero or negative size
2787 arrays must not be applied when this sizeof call is inside
2788 another array declarator. */
2790 type_expr
= integer_zero_node
;
2791 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2792 type_expr
, ret
.value
);
2793 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !type_expr_const
;
2795 pop_maybe_used (type
!= error_mark_node
2796 ? C_TYPE_VARIABLE_SIZE (type
) : false);
2800 /* Build a function call to function FUNCTION with parameters PARAMS.
2801 The function call is at LOC.
2802 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2803 TREE_VALUE of each node is a parameter-expression.
2804 FUNCTION's data type may be a function type or a pointer-to-function. */
2807 build_function_call (location_t loc
, tree function
, tree params
)
2809 vec
<tree
, va_gc
> *v
;
2812 vec_alloc (v
, list_length (params
));
2813 for (; params
; params
= TREE_CHAIN (params
))
2814 v
->quick_push (TREE_VALUE (params
));
2815 ret
= c_build_function_call_vec (loc
, vNULL
, function
, v
, NULL
);
2820 /* Give a note about the location of the declaration of DECL. */
2822 static void inform_declaration (tree decl
)
2824 if (decl
&& (TREE_CODE (decl
) != FUNCTION_DECL
|| !DECL_BUILT_IN (decl
)))
2825 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
2828 /* Build a function call to function FUNCTION with parameters PARAMS.
2829 ORIGTYPES, if not NULL, is a vector of types; each element is
2830 either NULL or the original type of the corresponding element in
2831 PARAMS. The original type may differ from TREE_TYPE of the
2832 parameter for enums. FUNCTION's data type may be a function type
2833 or pointer-to-function. This function changes the elements of
2837 build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
2838 tree function
, vec
<tree
, va_gc
> *params
,
2839 vec
<tree
, va_gc
> *origtypes
)
2841 tree fntype
, fundecl
= 0;
2842 tree name
= NULL_TREE
, result
;
2848 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2849 STRIP_TYPE_NOPS (function
);
2851 /* Convert anything with function type to a pointer-to-function. */
2852 if (TREE_CODE (function
) == FUNCTION_DECL
)
2854 name
= DECL_NAME (function
);
2857 tm_malloc_replacement (function
);
2859 /* Atomic functions have type checking/casting already done. They are
2860 often rewritten and don't match the original parameter list. */
2861 if (name
&& !strncmp (IDENTIFIER_POINTER (name
), "__atomic_", 9))
2865 && is_cilkplus_reduce_builtin (function
))
2868 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
)
2869 function
= function_to_pointer_conversion (loc
, function
);
2871 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2872 expressions, like those used for ObjC messenger dispatches. */
2873 if (params
&& !params
->is_empty ())
2874 function
= objc_rewrite_function_call (function
, (*params
)[0]);
2876 function
= c_fully_fold (function
, false, NULL
);
2878 fntype
= TREE_TYPE (function
);
2880 if (TREE_CODE (fntype
) == ERROR_MARK
)
2881 return error_mark_node
;
2883 if (!(TREE_CODE (fntype
) == POINTER_TYPE
2884 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
2886 if (!flag_diagnostics_show_caret
)
2888 "called object %qE is not a function or function pointer",
2890 else if (DECL_P (function
))
2893 "called object %qD is not a function or function pointer",
2895 inform_declaration (function
);
2899 "called object is not a function or function pointer");
2900 return error_mark_node
;
2903 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
2904 current_function_returns_abnormally
= 1;
2906 /* fntype now gets the type of function pointed to. */
2907 fntype
= TREE_TYPE (fntype
);
2909 /* Convert the parameters to the types declared in the
2910 function prototype, or apply default promotions. */
2912 nargs
= convert_arguments (loc
, arg_loc
, TYPE_ARG_TYPES (fntype
), params
,
2913 origtypes
, function
, fundecl
);
2915 return error_mark_node
;
2917 /* Check that the function is called through a compatible prototype.
2918 If it is not, warn. */
2919 if (CONVERT_EXPR_P (function
)
2920 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
2921 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
2922 && !comptypes (fntype
, TREE_TYPE (tem
)))
2924 tree return_type
= TREE_TYPE (fntype
);
2926 /* This situation leads to run-time undefined behavior. We can't,
2927 therefore, simply error unless we can prove that all possible
2928 executions of the program must execute the code. */
2929 warning_at (loc
, 0, "function called through a non-compatible type");
2931 if (VOID_TYPE_P (return_type
)
2932 && TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
)
2934 "function with qualified void return type called");
2937 argarray
= vec_safe_address (params
);
2939 /* Check that arguments to builtin functions match the expectations. */
2941 && DECL_BUILT_IN (fundecl
)
2942 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
2943 && !check_builtin_function_arguments (fundecl
, nargs
, argarray
))
2944 return error_mark_node
;
2946 /* Check that the arguments to the function are valid. */
2947 check_function_arguments (fntype
, nargs
, argarray
);
2949 if (name
!= NULL_TREE
2950 && !strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10))
2952 if (require_constant_value
)
2954 fold_build_call_array_initializer_loc (loc
, TREE_TYPE (fntype
),
2955 function
, nargs
, argarray
);
2957 result
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
2958 function
, nargs
, argarray
);
2959 if (TREE_CODE (result
) == NOP_EXPR
2960 && TREE_CODE (TREE_OPERAND (result
, 0)) == INTEGER_CST
)
2961 STRIP_TYPE_NOPS (result
);
2964 result
= build_call_array_loc (loc
, TREE_TYPE (fntype
),
2965 function
, nargs
, argarray
);
2967 if (VOID_TYPE_P (TREE_TYPE (result
)))
2969 if (TYPE_QUALS (TREE_TYPE (result
)) != TYPE_UNQUALIFIED
)
2971 "function with qualified void return type called");
2974 return require_complete_type (result
);
2977 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2980 c_build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
2981 tree function
, vec
<tree
, va_gc
> *params
,
2982 vec
<tree
, va_gc
> *origtypes
)
2984 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2985 STRIP_TYPE_NOPS (function
);
2987 /* Convert anything with function type to a pointer-to-function. */
2988 if (TREE_CODE (function
) == FUNCTION_DECL
)
2990 /* Implement type-directed function overloading for builtins.
2991 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2992 handle all the type checking. The result is a complete expression
2993 that implements this function call. */
2994 tree tem
= resolve_overloaded_builtin (loc
, function
, params
);
2998 return build_function_call_vec (loc
, arg_loc
, function
, params
, origtypes
);
3001 /* Convert the argument expressions in the vector VALUES
3002 to the types in the list TYPELIST.
3004 If TYPELIST is exhausted, or when an element has NULL as its type,
3005 perform the default conversions.
3007 ORIGTYPES is the original types of the expressions in VALUES. This
3008 holds the type of enum values which have been converted to integral
3009 types. It may be NULL.
3011 FUNCTION is a tree for the called function. It is used only for
3012 error messages, where it is formatted with %qE.
3014 This is also where warnings about wrong number of args are generated.
3016 ARG_LOC are locations of function arguments (if any).
3018 Returns the actual number of arguments processed (which may be less
3019 than the length of VALUES in some error situations), or -1 on
3023 convert_arguments (location_t loc
, vec
<location_t
> arg_loc
, tree typelist
,
3024 vec
<tree
, va_gc
> *values
, vec
<tree
, va_gc
> *origtypes
,
3025 tree function
, tree fundecl
)
3028 unsigned int parmnum
;
3029 bool error_args
= false;
3030 const bool type_generic
= fundecl
3031 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl
)));
3032 bool type_generic_remove_excess_precision
= false;
3035 /* Change pointer to function to the function itself for
3037 if (TREE_CODE (function
) == ADDR_EXPR
3038 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
3039 function
= TREE_OPERAND (function
, 0);
3041 /* Handle an ObjC selector specially for diagnostics. */
3042 selector
= objc_message_selector ();
3044 /* For type-generic built-in functions, determine whether excess
3045 precision should be removed (classification) or not
3048 && DECL_BUILT_IN (fundecl
)
3049 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
)
3051 switch (DECL_FUNCTION_CODE (fundecl
))
3053 case BUILT_IN_ISFINITE
:
3054 case BUILT_IN_ISINF
:
3055 case BUILT_IN_ISINF_SIGN
:
3056 case BUILT_IN_ISNAN
:
3057 case BUILT_IN_ISNORMAL
:
3058 case BUILT_IN_FPCLASSIFY
:
3059 type_generic_remove_excess_precision
= true;
3063 type_generic_remove_excess_precision
= false;
3067 if (flag_cilkplus
&& fundecl
&& is_cilkplus_reduce_builtin (fundecl
))
3068 return vec_safe_length (values
);
3070 /* Scan the given expressions and types, producing individual
3071 converted arguments. */
3073 for (typetail
= typelist
, parmnum
= 0;
3074 values
&& values
->iterate (parmnum
, &val
);
3077 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3078 tree valtype
= TREE_TYPE (val
);
3079 tree rname
= function
;
3080 int argnum
= parmnum
+ 1;
3081 const char *invalid_func_diag
;
3082 bool excess_precision
= false;
3085 /* Some __atomic_* builtins have additional hidden argument at
3088 = !arg_loc
.is_empty () && values
->length () == arg_loc
.length ()
3089 ? expansion_point_location_if_in_system_header (arg_loc
[parmnum
])
3092 if (type
== void_type_node
)
3095 error_at (loc
, "too many arguments to method %qE", selector
);
3097 error_at (loc
, "too many arguments to function %qE", function
);
3098 inform_declaration (fundecl
);
3102 if (selector
&& argnum
> 2)
3108 npc
= null_pointer_constant_p (val
);
3110 /* If there is excess precision and a prototype, convert once to
3111 the required type rather than converting via the semantic
3112 type. Likewise without a prototype a float value represented
3113 as long double should be converted once to double. But for
3114 type-generic classification functions excess precision must
3116 if (TREE_CODE (val
) == EXCESS_PRECISION_EXPR
3117 && (type
|| !type_generic
|| !type_generic_remove_excess_precision
))
3119 val
= TREE_OPERAND (val
, 0);
3120 excess_precision
= true;
3122 val
= c_fully_fold (val
, false, NULL
);
3123 STRIP_TYPE_NOPS (val
);
3125 val
= require_complete_type (val
);
3129 /* Formal parm type is specified by a function prototype. */
3131 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3133 error_at (ploc
, "type of formal parameter %d is incomplete",
3141 /* Optionally warn about conversions that
3142 differ from the default conversions. */
3143 if (warn_traditional_conversion
|| warn_traditional
)
3145 unsigned int formal_prec
= TYPE_PRECISION (type
);
3147 if (INTEGRAL_TYPE_P (type
)
3148 && TREE_CODE (valtype
) == REAL_TYPE
)
3149 warning_at (ploc
, OPT_Wtraditional_conversion
,
3150 "passing argument %d of %qE as integer rather "
3151 "than floating due to prototype",
3153 if (INTEGRAL_TYPE_P (type
)
3154 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3155 warning_at (ploc
, OPT_Wtraditional_conversion
,
3156 "passing argument %d of %qE as integer rather "
3157 "than complex due to prototype",
3159 else if (TREE_CODE (type
) == COMPLEX_TYPE
3160 && TREE_CODE (valtype
) == REAL_TYPE
)
3161 warning_at (ploc
, OPT_Wtraditional_conversion
,
3162 "passing argument %d of %qE as complex rather "
3163 "than floating due to prototype",
3165 else if (TREE_CODE (type
) == REAL_TYPE
3166 && INTEGRAL_TYPE_P (valtype
))
3167 warning_at (ploc
, OPT_Wtraditional_conversion
,
3168 "passing argument %d of %qE as floating rather "
3169 "than integer due to prototype",
3171 else if (TREE_CODE (type
) == COMPLEX_TYPE
3172 && INTEGRAL_TYPE_P (valtype
))
3173 warning_at (ploc
, OPT_Wtraditional_conversion
,
3174 "passing argument %d of %qE as complex rather "
3175 "than integer due to prototype",
3177 else if (TREE_CODE (type
) == REAL_TYPE
3178 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3179 warning_at (ploc
, OPT_Wtraditional_conversion
,
3180 "passing argument %d of %qE as floating rather "
3181 "than complex due to prototype",
3183 /* ??? At some point, messages should be written about
3184 conversions between complex types, but that's too messy
3186 else if (TREE_CODE (type
) == REAL_TYPE
3187 && TREE_CODE (valtype
) == REAL_TYPE
)
3189 /* Warn if any argument is passed as `float',
3190 since without a prototype it would be `double'. */
3191 if (formal_prec
== TYPE_PRECISION (float_type_node
)
3192 && type
!= dfloat32_type_node
)
3193 warning_at (ploc
, 0,
3194 "passing argument %d of %qE as %<float%> "
3195 "rather than %<double%> due to prototype",
3198 /* Warn if mismatch between argument and prototype
3199 for decimal float types. Warn of conversions with
3200 binary float types and of precision narrowing due to
3202 else if (type
!= valtype
3203 && (type
== dfloat32_type_node
3204 || type
== dfloat64_type_node
3205 || type
== dfloat128_type_node
3206 || valtype
== dfloat32_type_node
3207 || valtype
== dfloat64_type_node
3208 || valtype
== dfloat128_type_node
)
3210 <= TYPE_PRECISION (valtype
)
3211 || (type
== dfloat128_type_node
3213 != dfloat64_type_node
3215 != dfloat32_type_node
)))
3216 || (type
== dfloat64_type_node
3218 != dfloat32_type_node
))))
3219 warning_at (ploc
, 0,
3220 "passing argument %d of %qE as %qT "
3221 "rather than %qT due to prototype",
3222 argnum
, rname
, type
, valtype
);
3225 /* Detect integer changing in width or signedness.
3226 These warnings are only activated with
3227 -Wtraditional-conversion, not with -Wtraditional. */
3228 else if (warn_traditional_conversion
&& INTEGRAL_TYPE_P (type
)
3229 && INTEGRAL_TYPE_P (valtype
))
3231 tree would_have_been
= default_conversion (val
);
3232 tree type1
= TREE_TYPE (would_have_been
);
3234 if (TREE_CODE (type
) == ENUMERAL_TYPE
3235 && (TYPE_MAIN_VARIANT (type
)
3236 == TYPE_MAIN_VARIANT (valtype
)))
3237 /* No warning if function asks for enum
3238 and the actual arg is that enum type. */
3240 else if (formal_prec
!= TYPE_PRECISION (type1
))
3241 warning_at (ploc
, OPT_Wtraditional_conversion
,
3242 "passing argument %d of %qE "
3243 "with different width due to prototype",
3245 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
3247 /* Don't complain if the formal parameter type
3248 is an enum, because we can't tell now whether
3249 the value was an enum--even the same enum. */
3250 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3252 else if (TREE_CODE (val
) == INTEGER_CST
3253 && int_fits_type_p (val
, type
))
3254 /* Change in signedness doesn't matter
3255 if a constant value is unaffected. */
3257 /* If the value is extended from a narrower
3258 unsigned type, it doesn't matter whether we
3259 pass it as signed or unsigned; the value
3260 certainly is the same either way. */
3261 else if (TYPE_PRECISION (valtype
) < TYPE_PRECISION (type
)
3262 && TYPE_UNSIGNED (valtype
))
3264 else if (TYPE_UNSIGNED (type
))
3265 warning_at (ploc
, OPT_Wtraditional_conversion
,
3266 "passing argument %d of %qE "
3267 "as unsigned due to prototype",
3270 warning_at (ploc
, OPT_Wtraditional_conversion
,
3271 "passing argument %d of %qE "
3272 "as signed due to prototype",
3277 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3278 sake of better warnings from convert_and_check. */
3279 if (excess_precision
)
3280 val
= build1 (EXCESS_PRECISION_EXPR
, valtype
, val
);
3281 origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3282 parmval
= convert_for_assignment (loc
, ploc
, type
,
3283 val
, origtype
, ic_argpass
,
3284 npc
, fundecl
, function
,
3287 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
3288 && INTEGRAL_TYPE_P (type
)
3289 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3290 parmval
= default_conversion (parmval
);
3293 else if (TREE_CODE (valtype
) == REAL_TYPE
3294 && (TYPE_PRECISION (valtype
)
3295 <= TYPE_PRECISION (double_type_node
))
3296 && TYPE_MAIN_VARIANT (valtype
) != double_type_node
3297 && TYPE_MAIN_VARIANT (valtype
) != long_double_type_node
3298 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype
)))
3304 /* Convert `float' to `double'. */
3305 if (warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
3306 warning_at (ploc
, OPT_Wdouble_promotion
,
3307 "implicit conversion from %qT to %qT when passing "
3308 "argument to function",
3309 valtype
, double_type_node
);
3310 parmval
= convert (double_type_node
, val
);
3313 else if (excess_precision
&& !type_generic
)
3314 /* A "double" argument with excess precision being passed
3315 without a prototype or in variable arguments. */
3316 parmval
= convert (valtype
, val
);
3317 else if ((invalid_func_diag
=
3318 targetm
.calls
.invalid_arg_for_unprototyped_fn (typelist
, fundecl
, val
)))
3320 error (invalid_func_diag
);
3324 /* Convert `short' and `char' to full-size `int'. */
3325 parmval
= default_conversion (val
);
3327 (*values
)[parmnum
] = parmval
;
3328 if (parmval
== error_mark_node
)
3332 typetail
= TREE_CHAIN (typetail
);
3335 gcc_assert (parmnum
== vec_safe_length (values
));
3337 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
3339 error_at (loc
, "too few arguments to function %qE", function
);
3340 inform_declaration (fundecl
);
3344 return error_args
? -1 : (int) parmnum
;
3347 /* This is the entry point used by the parser to build unary operators
3348 in the input. CODE, a tree_code, specifies the unary operator, and
3349 ARG is the operand. For unary plus, the C parser currently uses
3350 CONVERT_EXPR for code.
3352 LOC is the location to use for the tree generated.
3356 parser_build_unary_op (location_t loc
, enum tree_code code
, struct c_expr arg
)
3358 struct c_expr result
;
3360 result
.value
= build_unary_op (loc
, code
, arg
.value
, 0);
3361 result
.original_code
= code
;
3362 result
.original_type
= NULL
;
3364 if (TREE_OVERFLOW_P (result
.value
) && !TREE_OVERFLOW_P (arg
.value
))
3365 overflow_warning (loc
, result
.value
);
3370 /* This is the entry point used by the parser to build binary operators
3371 in the input. CODE, a tree_code, specifies the binary operator, and
3372 ARG1 and ARG2 are the operands. In addition to constructing the
3373 expression, we check for operands that were written with other binary
3374 operators in a way that is likely to confuse the user.
3376 LOCATION is the location of the binary operator. */
3379 parser_build_binary_op (location_t location
, enum tree_code code
,
3380 struct c_expr arg1
, struct c_expr arg2
)
3382 struct c_expr result
;
3384 enum tree_code code1
= arg1
.original_code
;
3385 enum tree_code code2
= arg2
.original_code
;
3386 tree type1
= (arg1
.original_type
3387 ? arg1
.original_type
3388 : TREE_TYPE (arg1
.value
));
3389 tree type2
= (arg2
.original_type
3390 ? arg2
.original_type
3391 : TREE_TYPE (arg2
.value
));
3393 result
.value
= build_binary_op (location
, code
,
3394 arg1
.value
, arg2
.value
, 1);
3395 result
.original_code
= code
;
3396 result
.original_type
= NULL
;
3398 if (TREE_CODE (result
.value
) == ERROR_MARK
)
3401 if (location
!= UNKNOWN_LOCATION
)
3402 protected_set_expr_location (result
.value
, location
);
3404 /* Check for cases such as x+y<<z which users are likely
3406 if (warn_parentheses
)
3407 warn_about_parentheses (location
, code
, code1
, arg1
.value
, code2
,
3410 if (warn_logical_op
)
3411 warn_logical_operator (location
, code
, TREE_TYPE (result
.value
),
3412 code1
, arg1
.value
, code2
, arg2
.value
);
3414 if (warn_logical_not_paren
3415 && code1
== TRUTH_NOT_EXPR
3416 && code2
!= TRUTH_NOT_EXPR
)
3417 warn_logical_not_parentheses (location
, code
, arg1
.value
, arg2
.value
);
3419 /* Warn about comparisons against string literals, with the exception
3420 of testing for equality or inequality of a string literal with NULL. */
3421 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3423 if ((code1
== STRING_CST
&& !integer_zerop (arg2
.value
))
3424 || (code2
== STRING_CST
&& !integer_zerop (arg1
.value
)))
3425 warning_at (location
, OPT_Waddress
,
3426 "comparison with string literal results in unspecified behavior");
3428 else if (TREE_CODE_CLASS (code
) == tcc_comparison
3429 && (code1
== STRING_CST
|| code2
== STRING_CST
))
3430 warning_at (location
, OPT_Waddress
,
3431 "comparison with string literal results in unspecified behavior");
3433 if (TREE_OVERFLOW_P (result
.value
)
3434 && !TREE_OVERFLOW_P (arg1
.value
)
3435 && !TREE_OVERFLOW_P (arg2
.value
))
3436 overflow_warning (location
, result
.value
);
3438 /* Warn about comparisons of different enum types. */
3439 if (warn_enum_compare
3440 && TREE_CODE_CLASS (code
) == tcc_comparison
3441 && TREE_CODE (type1
) == ENUMERAL_TYPE
3442 && TREE_CODE (type2
) == ENUMERAL_TYPE
3443 && TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
3444 warning_at (location
, OPT_Wenum_compare
,
3445 "comparison between %qT and %qT",
3451 /* Return a tree for the difference of pointers OP0 and OP1.
3452 The resulting tree has type int. */
3455 pointer_diff (location_t loc
, tree op0
, tree op1
)
3457 tree restype
= ptrdiff_type_node
;
3458 tree result
, inttype
;
3460 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0
)));
3461 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1
)));
3462 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3463 tree orig_op1
= op1
;
3465 /* If the operands point into different address spaces, we need to
3466 explicitly convert them to pointers into the common address space
3467 before we can subtract the numerical address values. */
3470 addr_space_t as_common
;
3473 /* Determine the common superset address space. This is guaranteed
3474 to exist because the caller verified that comp_target_types
3475 returned non-zero. */
3476 if (!addr_space_superset (as0
, as1
, &as_common
))
3479 common_type
= common_pointer_type (TREE_TYPE (op0
), TREE_TYPE (op1
));
3480 op0
= convert (common_type
, op0
);
3481 op1
= convert (common_type
, op1
);
3484 /* Determine integer type to perform computations in. This will usually
3485 be the same as the result type (ptrdiff_t), but may need to be a wider
3486 type if pointers for the address space are wider than ptrdiff_t. */
3487 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
3488 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
3492 if (TREE_CODE (target_type
) == VOID_TYPE
)
3493 pedwarn (loc
, OPT_Wpointer_arith
,
3494 "pointer of type %<void *%> used in subtraction");
3495 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3496 pedwarn (loc
, OPT_Wpointer_arith
,
3497 "pointer to a function used in subtraction");
3499 /* First do the subtraction as integers;
3500 then drop through to build the divide operator.
3501 Do not do default conversions on the minus operator
3502 in case restype is a short type. */
3504 op0
= build_binary_op (loc
,
3505 MINUS_EXPR
, convert (inttype
, op0
),
3506 convert (inttype
, op1
), 0);
3507 /* This generates an error if op1 is pointer to incomplete type. */
3508 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
3509 error_at (loc
, "arithmetic on pointer to an incomplete type");
3511 op1
= c_size_in_bytes (target_type
);
3513 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1
)))
3514 error_at (loc
, "arithmetic on pointer to an empty aggregate");
3516 /* Divide by the size, in easiest possible way. */
3517 result
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, inttype
,
3518 op0
, convert (inttype
, op1
));
3520 /* Convert to final result type if necessary. */
3521 return convert (restype
, result
);
3524 /* Expand atomic compound assignments into an approriate sequence as
3525 specified by the C11 standard section 6.5.16.2.
3531 This sequence is used for all types for which these operations are
3534 In addition, built-in versions of the 'fe' prefixed routines may
3535 need to be invoked for floating point (real, complex or vector) when
3536 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3546 __atomic_load (addr, &old, SEQ_CST);
3547 feholdexcept (&fenv);
3549 newval = old op val;
3550 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3553 feclearexcept (FE_ALL_EXCEPT);
3556 feupdateenv (&fenv);
3558 Also note that the compiler is simply issuing the generic form of
3559 the atomic operations. This requires temp(s) and has their address
3560 taken. The atomic processing is smart enough to figure out when the
3561 size of an object can utilize a lock-free version, and convert the
3562 built-in call to the appropriate lock-free routine. The optimizers
3563 will then dispose of any temps that are no longer required, and
3564 lock-free implementations are utilized as long as there is target
3565 support for the required size.
3567 If the operator is NOP_EXPR, then this is a simple assignment, and
3568 an __atomic_store is issued to perform the assignment rather than
3573 /* Build an atomic assignment at LOC, expanding into the proper
3574 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3575 the result of the operation, unless RETURN_OLD_P in which case
3576 return the old value of LHS (this is only for postincrement and
3579 build_atomic_assign (location_t loc
, tree lhs
, enum tree_code modifycode
,
3580 tree rhs
, bool return_old_p
)
3582 tree fndecl
, func_call
;
3583 vec
<tree
, va_gc
> *params
;
3584 tree val
, nonatomic_lhs_type
, nonatomic_rhs_type
, newval
, newval_addr
;
3587 tree stmt
, goto_stmt
;
3588 tree loop_label
, loop_decl
, done_label
, done_decl
;
3590 tree lhs_type
= TREE_TYPE (lhs
);
3591 tree lhs_addr
= build_unary_op (loc
, ADDR_EXPR
, lhs
, 0);
3592 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
3593 tree rhs_type
= TREE_TYPE (rhs
);
3595 gcc_assert (TYPE_ATOMIC (lhs_type
));
3598 gcc_assert (modifycode
== PLUS_EXPR
|| modifycode
== MINUS_EXPR
);
3600 /* Allocate enough vector items for a compare_exchange. */
3601 vec_alloc (params
, 6);
3603 /* Create a compound statement to hold the sequence of statements
3605 compound_stmt
= c_begin_compound_stmt (false);
3607 /* Fold the RHS if it hasn't already been folded. */
3608 if (modifycode
!= NOP_EXPR
)
3609 rhs
= c_fully_fold (rhs
, false, NULL
);
3611 /* Remove the qualifiers for the rest of the expressions and create
3612 the VAL temp variable to hold the RHS. */
3613 nonatomic_lhs_type
= build_qualified_type (lhs_type
, TYPE_UNQUALIFIED
);
3614 nonatomic_rhs_type
= build_qualified_type (rhs_type
, TYPE_UNQUALIFIED
);
3615 val
= create_tmp_var (nonatomic_rhs_type
, NULL
);
3616 TREE_ADDRESSABLE (val
) = 1;
3617 TREE_NO_WARNING (val
) = 1;
3618 rhs
= build2 (MODIFY_EXPR
, nonatomic_rhs_type
, val
, rhs
);
3619 SET_EXPR_LOCATION (rhs
, loc
);
3622 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3624 if (modifycode
== NOP_EXPR
)
3626 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3627 rhs
= build_unary_op (loc
, ADDR_EXPR
, val
, 0);
3628 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
3629 params
->quick_push (lhs_addr
);
3630 params
->quick_push (rhs
);
3631 params
->quick_push (seq_cst
);
3632 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3633 add_stmt (func_call
);
3635 /* Finish the compound statement. */
3636 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3638 /* VAL is the value which was stored, return a COMPOUND_STMT of
3639 the statement and that value. */
3640 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, val
);
3643 /* Create the variables and labels required for the op= form. */
3644 old
= create_tmp_var (nonatomic_lhs_type
, NULL
);
3645 old_addr
= build_unary_op (loc
, ADDR_EXPR
, old
, 0);
3646 TREE_ADDRESSABLE (old
) = 1;
3647 TREE_NO_WARNING (old
) = 1;
3649 newval
= create_tmp_var (nonatomic_lhs_type
, NULL
);
3650 newval_addr
= build_unary_op (loc
, ADDR_EXPR
, newval
, 0);
3651 TREE_ADDRESSABLE (newval
) = 1;
3653 loop_decl
= create_artificial_label (loc
);
3654 loop_label
= build1 (LABEL_EXPR
, void_type_node
, loop_decl
);
3656 done_decl
= create_artificial_label (loc
);
3657 done_label
= build1 (LABEL_EXPR
, void_type_node
, done_decl
);
3659 /* __atomic_load (addr, &old, SEQ_CST). */
3660 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
3661 params
->quick_push (lhs_addr
);
3662 params
->quick_push (old_addr
);
3663 params
->quick_push (seq_cst
);
3664 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3665 add_stmt (func_call
);
3666 params
->truncate (0);
3668 /* Create the expressions for floating-point environment
3669 manipulation, if required. */
3670 bool need_fenv
= (flag_trapping_math
3671 && (FLOAT_TYPE_P (lhs_type
) || FLOAT_TYPE_P (rhs_type
)));
3672 tree hold_call
= NULL_TREE
, clear_call
= NULL_TREE
, update_call
= NULL_TREE
;
3674 targetm
.atomic_assign_expand_fenv (&hold_call
, &clear_call
, &update_call
);
3677 add_stmt (hold_call
);
3680 add_stmt (loop_label
);
3682 /* newval = old + val; */
3683 rhs
= build_binary_op (loc
, modifycode
, old
, val
, 1);
3684 rhs
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, nonatomic_lhs_type
,
3685 rhs
, NULL_TREE
, ic_assign
, false, NULL_TREE
,
3687 if (rhs
!= error_mark_node
)
3689 rhs
= build2 (MODIFY_EXPR
, nonatomic_lhs_type
, newval
, rhs
);
3690 SET_EXPR_LOCATION (rhs
, loc
);
3694 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3696 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE
);
3697 params
->quick_push (lhs_addr
);
3698 params
->quick_push (old_addr
);
3699 params
->quick_push (newval_addr
);
3700 params
->quick_push (integer_zero_node
);
3701 params
->quick_push (seq_cst
);
3702 params
->quick_push (seq_cst
);
3703 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3705 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, done_decl
);
3706 SET_EXPR_LOCATION (goto_stmt
, loc
);
3708 stmt
= build3 (COND_EXPR
, void_type_node
, func_call
, goto_stmt
, NULL_TREE
);
3709 SET_EXPR_LOCATION (stmt
, loc
);
3713 add_stmt (clear_call
);
3716 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, loop_decl
);
3717 SET_EXPR_LOCATION (goto_stmt
, loc
);
3718 add_stmt (goto_stmt
);
3721 add_stmt (done_label
);
3724 add_stmt (update_call
);
3726 /* Finish the compound statement. */
3727 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3729 /* NEWVAL is the value that was successfully stored, return a
3730 COMPOUND_EXPR of the statement and the appropriate value. */
3731 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
,
3732 return_old_p
? old
: newval
);
3735 /* Construct and perhaps optimize a tree representation
3736 for a unary operation. CODE, a tree_code, specifies the operation
3737 and XARG is the operand.
3738 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3739 the default promotions (such as from short to int).
3740 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3741 allows non-lvalues; this is only used to handle conversion of non-lvalue
3742 arrays to pointers in C99.
3744 LOCATION is the location of the operator. */
3747 build_unary_op (location_t location
,
3748 enum tree_code code
, tree xarg
, int flag
)
3750 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3753 enum tree_code typecode
;
3755 tree ret
= error_mark_node
;
3756 tree eptype
= NULL_TREE
;
3757 int noconvert
= flag
;
3758 const char *invalid_op_diag
;
3761 int_operands
= EXPR_INT_CONST_OPERANDS (xarg
);
3763 arg
= remove_c_maybe_const_expr (arg
);
3765 if (code
!= ADDR_EXPR
)
3766 arg
= require_complete_type (arg
);
3768 typecode
= TREE_CODE (TREE_TYPE (arg
));
3769 if (typecode
== ERROR_MARK
)
3770 return error_mark_node
;
3771 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
3772 typecode
= INTEGER_TYPE
;
3774 if ((invalid_op_diag
3775 = targetm
.invalid_unary_op (code
, TREE_TYPE (xarg
))))
3777 error_at (location
, invalid_op_diag
);
3778 return error_mark_node
;
3781 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
3783 eptype
= TREE_TYPE (arg
);
3784 arg
= TREE_OPERAND (arg
, 0);
3790 /* This is used for unary plus, because a CONVERT_EXPR
3791 is enough to prevent anybody from looking inside for
3792 associativity, but won't generate any code. */
3793 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3794 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3795 || typecode
== VECTOR_TYPE
))
3797 error_at (location
, "wrong type argument to unary plus");
3798 return error_mark_node
;
3800 else if (!noconvert
)
3801 arg
= default_conversion (arg
);
3802 arg
= non_lvalue_loc (location
, arg
);
3806 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3807 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3808 || typecode
== VECTOR_TYPE
))
3810 error_at (location
, "wrong type argument to unary minus");
3811 return error_mark_node
;
3813 else if (!noconvert
)
3814 arg
= default_conversion (arg
);
3818 /* ~ works on integer types and non float vectors. */
3819 if (typecode
== INTEGER_TYPE
3820 || (typecode
== VECTOR_TYPE
3821 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg
))))
3824 arg
= default_conversion (arg
);
3826 else if (typecode
== COMPLEX_TYPE
)
3829 pedwarn (location
, OPT_Wpedantic
,
3830 "ISO C does not support %<~%> for complex conjugation");
3832 arg
= default_conversion (arg
);
3836 error_at (location
, "wrong type argument to bit-complement");
3837 return error_mark_node
;
3842 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
3844 error_at (location
, "wrong type argument to abs");
3845 return error_mark_node
;
3847 else if (!noconvert
)
3848 arg
= default_conversion (arg
);
3852 /* Conjugating a real value is a no-op, but allow it anyway. */
3853 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3854 || typecode
== COMPLEX_TYPE
))
3856 error_at (location
, "wrong type argument to conjugation");
3857 return error_mark_node
;
3859 else if (!noconvert
)
3860 arg
= default_conversion (arg
);
3863 case TRUTH_NOT_EXPR
:
3864 if (typecode
!= INTEGER_TYPE
&& typecode
!= FIXED_POINT_TYPE
3865 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
3866 && typecode
!= COMPLEX_TYPE
)
3869 "wrong type argument to unary exclamation mark");
3870 return error_mark_node
;
3874 arg
= c_objc_common_truthvalue_conversion (location
, xarg
);
3875 arg
= remove_c_maybe_const_expr (arg
);
3878 arg
= c_objc_common_truthvalue_conversion (location
, arg
);
3879 ret
= invert_truthvalue_loc (location
, arg
);
3880 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3881 if (EXPR_P (ret
) && EXPR_HAS_LOCATION (ret
))
3882 location
= EXPR_LOCATION (ret
);
3883 goto return_build_unary_op
;
3887 ret
= build_real_imag_expr (location
, code
, arg
);
3888 if (ret
== error_mark_node
)
3889 return error_mark_node
;
3890 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
3891 eptype
= TREE_TYPE (eptype
);
3892 goto return_build_unary_op
;
3894 case PREINCREMENT_EXPR
:
3895 case POSTINCREMENT_EXPR
:
3896 case PREDECREMENT_EXPR
:
3897 case POSTDECREMENT_EXPR
:
3899 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
3901 tree inner
= build_unary_op (location
, code
,
3902 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
3903 if (inner
== error_mark_node
)
3904 return error_mark_node
;
3905 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
3906 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
3907 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
3908 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = 1;
3909 goto return_build_unary_op
;
3912 /* Complain about anything that is not a true lvalue. In
3913 Objective-C, skip this check for property_refs. */
3914 if (!objc_is_property_ref (arg
)
3915 && !lvalue_or_else (location
,
3916 arg
, ((code
== PREINCREMENT_EXPR
3917 || code
== POSTINCREMENT_EXPR
)
3920 return error_mark_node
;
3922 if (warn_cxx_compat
&& TREE_CODE (TREE_TYPE (arg
)) == ENUMERAL_TYPE
)
3924 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3925 warning_at (location
, OPT_Wc___compat
,
3926 "increment of enumeration value is invalid in C++");
3928 warning_at (location
, OPT_Wc___compat
,
3929 "decrement of enumeration value is invalid in C++");
3932 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3933 arg
= c_fully_fold (arg
, false, NULL
);
3936 atomic_op
= really_atomic_lvalue (arg
);
3938 /* Increment or decrement the real part of the value,
3939 and don't change the imaginary part. */
3940 if (typecode
== COMPLEX_TYPE
)
3944 pedwarn (location
, OPT_Wpedantic
,
3945 "ISO C does not support %<++%> and %<--%> on complex types");
3949 arg
= stabilize_reference (arg
);
3950 real
= build_unary_op (EXPR_LOCATION (arg
), REALPART_EXPR
, arg
, 1);
3951 imag
= build_unary_op (EXPR_LOCATION (arg
), IMAGPART_EXPR
, arg
, 1);
3952 real
= build_unary_op (EXPR_LOCATION (arg
), code
, real
, 1);
3953 if (real
== error_mark_node
|| imag
== error_mark_node
)
3954 return error_mark_node
;
3955 ret
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
3957 goto return_build_unary_op
;
3961 /* Report invalid types. */
3963 if (typecode
!= POINTER_TYPE
&& typecode
!= FIXED_POINT_TYPE
3964 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
3965 && typecode
!= COMPLEX_TYPE
&& typecode
!= VECTOR_TYPE
)
3967 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3968 error_at (location
, "wrong type argument to increment");
3970 error_at (location
, "wrong type argument to decrement");
3972 return error_mark_node
;
3978 argtype
= TREE_TYPE (arg
);
3980 /* Compute the increment. */
3982 if (typecode
== POINTER_TYPE
)
3984 /* If pointer target is an incomplete type,
3985 we just cannot know how to do the arithmetic. */
3986 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype
)))
3988 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3990 "increment of pointer to an incomplete type %qT",
3991 TREE_TYPE (argtype
));
3994 "decrement of pointer to an incomplete type %qT",
3995 TREE_TYPE (argtype
));
3997 else if (TREE_CODE (TREE_TYPE (argtype
)) == FUNCTION_TYPE
3998 || TREE_CODE (TREE_TYPE (argtype
)) == VOID_TYPE
)
4000 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4001 pedwarn (location
, OPT_Wpointer_arith
,
4002 "wrong type argument to increment");
4004 pedwarn (location
, OPT_Wpointer_arith
,
4005 "wrong type argument to decrement");
4008 inc
= c_size_in_bytes (TREE_TYPE (argtype
));
4009 inc
= convert_to_ptrofftype_loc (location
, inc
);
4011 else if (FRACT_MODE_P (TYPE_MODE (argtype
)))
4013 /* For signed fract types, we invert ++ to -- or
4014 -- to ++, and change inc from 1 to -1, because
4015 it is not possible to represent 1 in signed fract constants.
4016 For unsigned fract types, the result always overflows and
4017 we get an undefined (original) or the maximum value. */
4018 if (code
== PREINCREMENT_EXPR
)
4019 code
= PREDECREMENT_EXPR
;
4020 else if (code
== PREDECREMENT_EXPR
)
4021 code
= PREINCREMENT_EXPR
;
4022 else if (code
== POSTINCREMENT_EXPR
)
4023 code
= POSTDECREMENT_EXPR
;
4024 else /* code == POSTDECREMENT_EXPR */
4025 code
= POSTINCREMENT_EXPR
;
4027 inc
= integer_minus_one_node
;
4028 inc
= convert (argtype
, inc
);
4032 inc
= VECTOR_TYPE_P (argtype
)
4033 ? build_one_cst (argtype
)
4035 inc
= convert (argtype
, inc
);
4038 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4039 need to ask Objective-C to build the increment or decrement
4040 expression for it. */
4041 if (objc_is_property_ref (arg
))
4042 return objc_build_incr_expr_for_property_ref (location
, code
,
4045 /* Report a read-only lvalue. */
4046 if (TYPE_READONLY (argtype
))
4048 readonly_error (location
, arg
,
4049 ((code
== PREINCREMENT_EXPR
4050 || code
== POSTINCREMENT_EXPR
)
4051 ? lv_increment
: lv_decrement
));
4052 return error_mark_node
;
4054 else if (TREE_READONLY (arg
))
4055 readonly_warning (arg
,
4056 ((code
== PREINCREMENT_EXPR
4057 || code
== POSTINCREMENT_EXPR
)
4058 ? lv_increment
: lv_decrement
));
4060 /* If the argument is atomic, use the special code sequences for
4061 atomic compound assignment. */
4064 arg
= stabilize_reference (arg
);
4065 ret
= build_atomic_assign (location
, arg
,
4066 ((code
== PREINCREMENT_EXPR
4067 || code
== POSTINCREMENT_EXPR
)
4070 (FRACT_MODE_P (TYPE_MODE (argtype
))
4072 : integer_one_node
),
4073 (code
== POSTINCREMENT_EXPR
4074 || code
== POSTDECREMENT_EXPR
));
4075 goto return_build_unary_op
;
4078 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4079 val
= boolean_increment (code
, arg
);
4081 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
4082 TREE_SIDE_EFFECTS (val
) = 1;
4083 if (TREE_CODE (val
) != code
)
4084 TREE_NO_WARNING (val
) = 1;
4086 goto return_build_unary_op
;
4090 /* Note that this operation never does default_conversion. */
4092 /* The operand of unary '&' must be an lvalue (which excludes
4093 expressions of type void), or, in C99, the result of a [] or
4094 unary '*' operator. */
4095 if (VOID_TYPE_P (TREE_TYPE (arg
))
4096 && TYPE_QUALS (TREE_TYPE (arg
)) == TYPE_UNQUALIFIED
4097 && (TREE_CODE (arg
) != INDIRECT_REF
4099 pedwarn (location
, 0, "taking address of expression of type %<void%>");
4101 /* Let &* cancel out to simplify resulting code. */
4102 if (TREE_CODE (arg
) == INDIRECT_REF
)
4104 /* Don't let this be an lvalue. */
4105 if (lvalue_p (TREE_OPERAND (arg
, 0)))
4106 return non_lvalue_loc (location
, TREE_OPERAND (arg
, 0));
4107 ret
= TREE_OPERAND (arg
, 0);
4108 goto return_build_unary_op
;
4111 /* For &x[y], return x+y */
4112 if (TREE_CODE (arg
) == ARRAY_REF
)
4114 tree op0
= TREE_OPERAND (arg
, 0);
4115 if (!c_mark_addressable (op0
))
4116 return error_mark_node
;
4119 /* Anything not already handled and not a true memory reference
4120 or a non-lvalue array is an error. */
4121 else if (typecode
!= FUNCTION_TYPE
&& !flag
4122 && !lvalue_or_else (location
, arg
, lv_addressof
))
4123 return error_mark_node
;
4125 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4127 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4129 tree inner
= build_unary_op (location
, code
,
4130 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
4131 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4132 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4133 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4134 C_MAYBE_CONST_EXPR_NON_CONST (ret
)
4135 = C_MAYBE_CONST_EXPR_NON_CONST (arg
);
4136 goto return_build_unary_op
;
4139 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4140 argtype
= TREE_TYPE (arg
);
4142 /* If the lvalue is const or volatile, merge that into the type
4143 to which the address will point. This is only needed
4144 for function types. */
4145 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
4146 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4147 && TREE_CODE (argtype
) == FUNCTION_TYPE
)
4149 int orig_quals
= TYPE_QUALS (strip_array_types (argtype
));
4150 int quals
= orig_quals
;
4152 if (TREE_READONLY (arg
))
4153 quals
|= TYPE_QUAL_CONST
;
4154 if (TREE_THIS_VOLATILE (arg
))
4155 quals
|= TYPE_QUAL_VOLATILE
;
4157 argtype
= c_build_qualified_type (argtype
, quals
);
4160 if (!c_mark_addressable (arg
))
4161 return error_mark_node
;
4163 gcc_assert (TREE_CODE (arg
) != COMPONENT_REF
4164 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)));
4166 argtype
= build_pointer_type (argtype
);
4168 /* ??? Cope with user tricks that amount to offsetof. Delete this
4169 when we have proper support for integer constant expressions. */
4170 val
= get_base_address (arg
);
4171 if (val
&& TREE_CODE (val
) == INDIRECT_REF
4172 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
4174 ret
= fold_convert_loc (location
, argtype
, fold_offsetof_1 (arg
));
4175 goto return_build_unary_op
;
4178 val
= build1 (ADDR_EXPR
, argtype
, arg
);
4181 goto return_build_unary_op
;
4188 argtype
= TREE_TYPE (arg
);
4189 if (TREE_CODE (arg
) == INTEGER_CST
)
4190 ret
= (require_constant_value
4191 ? fold_build1_initializer_loc (location
, code
, argtype
, arg
)
4192 : fold_build1_loc (location
, code
, argtype
, arg
));
4194 ret
= build1 (code
, argtype
, arg
);
4195 return_build_unary_op
:
4196 gcc_assert (ret
!= error_mark_node
);
4197 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
)
4198 && !(TREE_CODE (xarg
) == INTEGER_CST
&& !TREE_OVERFLOW (xarg
)))
4199 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
4200 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
)
4201 ret
= note_integer_operands (ret
);
4203 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4204 protected_set_expr_location (ret
, location
);
4208 /* Return nonzero if REF is an lvalue valid for this language.
4209 Lvalues can be assigned, unless their type has TYPE_READONLY.
4210 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4213 lvalue_p (const_tree ref
)
4215 const enum tree_code code
= TREE_CODE (ref
);
4222 return lvalue_p (TREE_OPERAND (ref
, 0));
4224 case C_MAYBE_CONST_EXPR
:
4225 return lvalue_p (TREE_OPERAND (ref
, 1));
4227 case COMPOUND_LITERAL_EXPR
:
4233 case ARRAY_NOTATION_REF
:
4238 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
4239 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
4242 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
4249 /* Give a warning for storing in something that is read-only in GCC
4250 terms but not const in ISO C terms. */
4253 readonly_warning (tree arg
, enum lvalue_use use
)
4258 warning (0, "assignment of read-only location %qE", arg
);
4261 warning (0, "increment of read-only location %qE", arg
);
4264 warning (0, "decrement of read-only location %qE", arg
);
4273 /* Return nonzero if REF is an lvalue valid for this language;
4274 otherwise, print an error message and return zero. USE says
4275 how the lvalue is being used and so selects the error message.
4276 LOCATION is the location at which any error should be reported. */
4279 lvalue_or_else (location_t loc
, const_tree ref
, enum lvalue_use use
)
4281 int win
= lvalue_p (ref
);
4284 lvalue_error (loc
, use
);
4289 /* Mark EXP saying that we need to be able to take the
4290 address of it; it should not be allocated in a register.
4291 Returns true if successful. */
4294 c_mark_addressable (tree exp
)
4299 switch (TREE_CODE (x
))
4302 if (DECL_C_BIT_FIELD (TREE_OPERAND (x
, 1)))
4305 ("cannot take address of bit-field %qD", TREE_OPERAND (x
, 1));
4309 /* ... fall through ... */
4315 x
= TREE_OPERAND (x
, 0);
4318 case COMPOUND_LITERAL_EXPR
:
4320 TREE_ADDRESSABLE (x
) = 1;
4327 if (C_DECL_REGISTER (x
)
4328 && DECL_NONLOCAL (x
))
4330 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
4333 ("global register variable %qD used in nested function", x
);
4336 pedwarn (input_location
, 0, "register variable %qD used in nested function", x
);
4338 else if (C_DECL_REGISTER (x
))
4340 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
4341 error ("address of global register variable %qD requested", x
);
4343 error ("address of register variable %qD requested", x
);
4349 TREE_ADDRESSABLE (x
) = 1;
4356 /* Convert EXPR to TYPE, warning about conversion problems with
4357 constants. SEMANTIC_TYPE is the type this conversion would use
4358 without excess precision. If SEMANTIC_TYPE is NULL, this function
4359 is equivalent to convert_and_check. This function is a wrapper that
4360 handles conversions that may be different than
4361 the usual ones because of excess precision. */
4364 ep_convert_and_check (location_t loc
, tree type
, tree expr
,
4367 if (TREE_TYPE (expr
) == type
)
4371 return convert_and_check (loc
, type
, expr
);
4373 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
4374 && TREE_TYPE (expr
) != semantic_type
)
4376 /* For integers, we need to check the real conversion, not
4377 the conversion to the excess precision type. */
4378 expr
= convert_and_check (loc
, semantic_type
, expr
);
4380 /* Result type is the excess precision type, which should be
4381 large enough, so do not check. */
4382 return convert (type
, expr
);
4385 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4386 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4387 if folded to an integer constant then the unselected half may
4388 contain arbitrary operations not normally permitted in constant
4389 expressions. Set the location of the expression to LOC. */
4392 build_conditional_expr (location_t colon_loc
, tree ifexp
, bool ifexp_bcp
,
4393 tree op1
, tree op1_original_type
, tree op2
,
4394 tree op2_original_type
)
4398 enum tree_code code1
;
4399 enum tree_code code2
;
4400 tree result_type
= NULL
;
4401 tree semantic_result_type
= NULL
;
4402 tree orig_op1
= op1
, orig_op2
= op2
;
4403 bool int_const
, op1_int_operands
, op2_int_operands
, int_operands
;
4404 bool ifexp_int_operands
;
4407 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
4408 if (op1_int_operands
)
4409 op1
= remove_c_maybe_const_expr (op1
);
4410 op2_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op2
);
4411 if (op2_int_operands
)
4412 op2
= remove_c_maybe_const_expr (op2
);
4413 ifexp_int_operands
= EXPR_INT_CONST_OPERANDS (ifexp
);
4414 if (ifexp_int_operands
)
4415 ifexp
= remove_c_maybe_const_expr (ifexp
);
4417 /* Promote both alternatives. */
4419 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
4420 op1
= default_conversion (op1
);
4421 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
4422 op2
= default_conversion (op2
);
4424 if (TREE_CODE (ifexp
) == ERROR_MARK
4425 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
4426 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
4427 return error_mark_node
;
4429 type1
= TREE_TYPE (op1
);
4430 code1
= TREE_CODE (type1
);
4431 type2
= TREE_TYPE (op2
);
4432 code2
= TREE_CODE (type2
);
4434 /* C90 does not permit non-lvalue arrays in conditional expressions.
4435 In C99 they will be pointers by now. */
4436 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
4438 error_at (colon_loc
, "non-lvalue array in conditional expression");
4439 return error_mark_node
;
4442 if ((TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
4443 || TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4444 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4445 || code1
== COMPLEX_TYPE
)
4446 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4447 || code2
== COMPLEX_TYPE
))
4449 semantic_result_type
= c_common_type (type1
, type2
);
4450 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
4452 op1
= TREE_OPERAND (op1
, 0);
4453 type1
= TREE_TYPE (op1
);
4454 gcc_assert (TREE_CODE (type1
) == code1
);
4456 if (TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4458 op2
= TREE_OPERAND (op2
, 0);
4459 type2
= TREE_TYPE (op2
);
4460 gcc_assert (TREE_CODE (type2
) == code2
);
4464 if (warn_cxx_compat
)
4466 tree t1
= op1_original_type
? op1_original_type
: TREE_TYPE (orig_op1
);
4467 tree t2
= op2_original_type
? op2_original_type
: TREE_TYPE (orig_op2
);
4469 if (TREE_CODE (t1
) == ENUMERAL_TYPE
4470 && TREE_CODE (t2
) == ENUMERAL_TYPE
4471 && TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
4472 warning_at (colon_loc
, OPT_Wc___compat
,
4473 ("different enum types in conditional is "
4474 "invalid in C++: %qT vs %qT"),
4478 /* Quickly detect the usual case where op1 and op2 have the same type
4480 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
4483 result_type
= type1
;
4485 result_type
= TYPE_MAIN_VARIANT (type1
);
4487 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4488 || code1
== COMPLEX_TYPE
)
4489 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4490 || code2
== COMPLEX_TYPE
))
4492 result_type
= c_common_type (type1
, type2
);
4493 do_warn_double_promotion (result_type
, type1
, type2
,
4494 "implicit conversion from %qT to %qT to "
4495 "match other result of conditional",
4498 /* If -Wsign-compare, warn here if type1 and type2 have
4499 different signedness. We'll promote the signed to unsigned
4500 and later code won't know it used to be different.
4501 Do this check on the original types, so that explicit casts
4502 will be considered, but default promotions won't. */
4503 if (c_inhibit_evaluation_warnings
== 0)
4505 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
4506 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
4508 if (unsigned_op1
^ unsigned_op2
)
4512 /* Do not warn if the result type is signed, since the
4513 signed type will only be chosen if it can represent
4514 all the values of the unsigned type. */
4515 if (!TYPE_UNSIGNED (result_type
))
4519 bool op1_maybe_const
= true;
4520 bool op2_maybe_const
= true;
4522 /* Do not warn if the signed quantity is an
4523 unsuffixed integer literal (or some static
4524 constant expression involving such literals) and
4525 it is non-negative. This warning requires the
4526 operands to be folded for best results, so do
4527 that folding in this case even without
4528 warn_sign_compare to avoid warning options
4529 possibly affecting code generation. */
4530 c_inhibit_evaluation_warnings
4531 += (ifexp
== truthvalue_false_node
);
4532 op1
= c_fully_fold (op1
, require_constant_value
,
4534 c_inhibit_evaluation_warnings
4535 -= (ifexp
== truthvalue_false_node
);
4537 c_inhibit_evaluation_warnings
4538 += (ifexp
== truthvalue_true_node
);
4539 op2
= c_fully_fold (op2
, require_constant_value
,
4541 c_inhibit_evaluation_warnings
4542 -= (ifexp
== truthvalue_true_node
);
4544 if (warn_sign_compare
)
4547 && tree_expr_nonnegative_warnv_p (op1
, &ovf
))
4549 && tree_expr_nonnegative_warnv_p (op2
, &ovf
)))
4552 warning_at (colon_loc
, OPT_Wsign_compare
,
4553 ("signed and unsigned type in "
4554 "conditional expression"));
4556 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
4557 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
4558 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
4559 op2
= c_wrap_maybe_const (op2
, !op2_maybe_const
);
4564 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
4566 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
4567 pedwarn (colon_loc
, OPT_Wpedantic
,
4568 "ISO C forbids conditional expr with only one void side");
4569 result_type
= void_type_node
;
4571 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
4573 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
4574 addr_space_t as2
= TYPE_ADDR_SPACE (TREE_TYPE (type2
));
4575 addr_space_t as_common
;
4577 if (comp_target_types (colon_loc
, type1
, type2
))
4578 result_type
= common_pointer_type (type1
, type2
);
4579 else if (null_pointer_constant_p (orig_op1
))
4580 result_type
= type2
;
4581 else if (null_pointer_constant_p (orig_op2
))
4582 result_type
= type1
;
4583 else if (!addr_space_superset (as1
, as2
, &as_common
))
4585 error_at (colon_loc
, "pointers to disjoint address spaces "
4586 "used in conditional expression");
4587 return error_mark_node
;
4589 else if (VOID_TYPE_P (TREE_TYPE (type1
))
4590 && !TYPE_ATOMIC (TREE_TYPE (type1
)))
4592 if (TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
4593 pedwarn (colon_loc
, OPT_Wpedantic
,
4594 "ISO C forbids conditional expr between "
4595 "%<void *%> and function pointer");
4596 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
4597 TREE_TYPE (type2
)));
4599 else if (VOID_TYPE_P (TREE_TYPE (type2
))
4600 && !TYPE_ATOMIC (TREE_TYPE (type2
)))
4602 if (TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
4603 pedwarn (colon_loc
, OPT_Wpedantic
,
4604 "ISO C forbids conditional expr between "
4605 "%<void *%> and function pointer");
4606 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
4607 TREE_TYPE (type1
)));
4609 /* Objective-C pointer comparisons are a bit more lenient. */
4610 else if (objc_have_common_type (type1
, type2
, -3, NULL_TREE
))
4611 result_type
= objc_common_type (type1
, type2
);
4614 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
4616 pedwarn (colon_loc
, 0,
4617 "pointer type mismatch in conditional expression");
4618 result_type
= build_pointer_type
4619 (build_qualified_type (void_type_node
, qual
));
4622 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
4624 if (!null_pointer_constant_p (orig_op2
))
4625 pedwarn (colon_loc
, 0,
4626 "pointer/integer type mismatch in conditional expression");
4629 op2
= null_pointer_node
;
4631 result_type
= type1
;
4633 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4635 if (!null_pointer_constant_p (orig_op1
))
4636 pedwarn (colon_loc
, 0,
4637 "pointer/integer type mismatch in conditional expression");
4640 op1
= null_pointer_node
;
4642 result_type
= type2
;
4647 if (flag_cond_mismatch
)
4648 result_type
= void_type_node
;
4651 error_at (colon_loc
, "type mismatch in conditional expression");
4652 return error_mark_node
;
4656 /* Merge const and volatile flags of the incoming types. */
4658 = build_type_variant (result_type
,
4659 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
4660 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
4662 op1
= ep_convert_and_check (colon_loc
, result_type
, op1
,
4663 semantic_result_type
);
4664 op2
= ep_convert_and_check (colon_loc
, result_type
, op2
,
4665 semantic_result_type
);
4667 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
4669 op2_int_operands
= true;
4670 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
4672 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
4674 op1_int_operands
= true;
4675 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
4677 int_const
= int_operands
= (ifexp_int_operands
4679 && op2_int_operands
);
4682 int_const
= ((ifexp
== truthvalue_true_node
4683 && TREE_CODE (orig_op1
) == INTEGER_CST
4684 && !TREE_OVERFLOW (orig_op1
))
4685 || (ifexp
== truthvalue_false_node
4686 && TREE_CODE (orig_op2
) == INTEGER_CST
4687 && !TREE_OVERFLOW (orig_op2
)));
4689 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
4690 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4695 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4696 nested inside of the expression. */
4697 op1
= c_fully_fold (op1
, false, NULL
);
4698 op2
= c_fully_fold (op2
, false, NULL
);
4700 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4702 ret
= note_integer_operands (ret
);
4704 if (semantic_result_type
)
4705 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
4707 protected_set_expr_location (ret
, colon_loc
);
4711 /* Return a compound expression that performs two expressions and
4712 returns the value of the second of them.
4714 LOC is the location of the COMPOUND_EXPR. */
4717 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
4719 bool expr1_int_operands
, expr2_int_operands
;
4720 tree eptype
= NULL_TREE
;
4724 && (TREE_CODE (expr1
) == CILK_SPAWN_STMT
4725 || TREE_CODE (expr2
) == CILK_SPAWN_STMT
))
4728 "spawned function call cannot be part of a comma expression");
4729 return error_mark_node
;
4731 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
4732 if (expr1_int_operands
)
4733 expr1
= remove_c_maybe_const_expr (expr1
);
4734 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
4735 if (expr2_int_operands
)
4736 expr2
= remove_c_maybe_const_expr (expr2
);
4738 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
4739 expr1
= TREE_OPERAND (expr1
, 0);
4740 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
4742 eptype
= TREE_TYPE (expr2
);
4743 expr2
= TREE_OPERAND (expr2
, 0);
4746 if (!TREE_SIDE_EFFECTS (expr1
))
4748 /* The left-hand operand of a comma expression is like an expression
4749 statement: with -Wunused, we should warn if it doesn't have
4750 any side-effects, unless it was explicitly cast to (void). */
4751 if (warn_unused_value
)
4753 if (VOID_TYPE_P (TREE_TYPE (expr1
))
4754 && CONVERT_EXPR_P (expr1
))
4756 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
4757 && TREE_CODE (expr1
) == COMPOUND_EXPR
4758 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
4759 ; /* (void) a, (void) b, c */
4761 warning_at (loc
, OPT_Wunused_value
,
4762 "left-hand operand of comma expression has no effect");
4765 else if (TREE_CODE (expr1
) == COMPOUND_EXPR
4766 && warn_unused_value
)
4769 location_t cloc
= loc
;
4770 while (TREE_CODE (r
) == COMPOUND_EXPR
)
4772 if (EXPR_HAS_LOCATION (r
))
4773 cloc
= EXPR_LOCATION (r
);
4774 r
= TREE_OPERAND (r
, 1);
4776 if (!TREE_SIDE_EFFECTS (r
)
4777 && !VOID_TYPE_P (TREE_TYPE (r
))
4778 && !CONVERT_EXPR_P (r
))
4779 warning_at (cloc
, OPT_Wunused_value
,
4780 "right-hand operand of comma expression has no effect");
4783 /* With -Wunused, we should also warn if the left-hand operand does have
4784 side-effects, but computes a value which is not used. For example, in
4785 `foo() + bar(), baz()' the result of the `+' operator is not used,
4786 so we should issue a warning. */
4787 else if (warn_unused_value
)
4788 warn_if_unused_value (expr1
, loc
);
4790 if (expr2
== error_mark_node
)
4791 return error_mark_node
;
4793 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
4796 && expr1_int_operands
4797 && expr2_int_operands
)
4798 ret
= note_integer_operands (ret
);
4801 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4803 protected_set_expr_location (ret
, loc
);
4807 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4808 which we are casting. OTYPE is the type of the expression being
4809 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4810 of the cast. -Wcast-qual appeared on the command line. Named
4811 address space qualifiers are not handled here, because they result
4812 in different warnings. */
4815 handle_warn_cast_qual (location_t loc
, tree type
, tree otype
)
4817 tree in_type
= type
;
4818 tree in_otype
= otype
;
4823 /* Check that the qualifiers on IN_TYPE are a superset of the
4824 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4825 nodes is uninteresting and we stop as soon as we hit a
4826 non-POINTER_TYPE node on either type. */
4829 in_otype
= TREE_TYPE (in_otype
);
4830 in_type
= TREE_TYPE (in_type
);
4832 /* GNU C allows cv-qualified function types. 'const' means the
4833 function is very pure, 'volatile' means it can't return. We
4834 need to warn when such qualifiers are added, not when they're
4836 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
4837 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
4838 added
|= (TYPE_QUALS_NO_ADDR_SPACE (in_type
)
4839 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype
));
4841 discarded
|= (TYPE_QUALS_NO_ADDR_SPACE (in_otype
)
4842 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type
));
4844 while (TREE_CODE (in_type
) == POINTER_TYPE
4845 && TREE_CODE (in_otype
) == POINTER_TYPE
);
4848 warning_at (loc
, OPT_Wcast_qual
,
4849 "cast adds %q#v qualifier to function type", added
);
4852 /* There are qualifiers present in IN_OTYPE that are not present
4854 warning_at (loc
, OPT_Wcast_qual
,
4855 "cast discards %qv qualifier from pointer target type",
4858 if (added
|| discarded
)
4861 /* A cast from **T to const **T is unsafe, because it can cause a
4862 const value to be changed with no additional warning. We only
4863 issue this warning if T is the same on both sides, and we only
4864 issue the warning if there are the same number of pointers on
4865 both sides, as otherwise the cast is clearly unsafe anyhow. A
4866 cast is unsafe when a qualifier is added at one level and const
4867 is not present at all outer levels.
4869 To issue this warning, we check at each level whether the cast
4870 adds new qualifiers not already seen. We don't need to special
4871 case function types, as they won't have the same
4872 TYPE_MAIN_VARIANT. */
4874 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
4876 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
4881 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
4884 in_type
= TREE_TYPE (in_type
);
4885 in_otype
= TREE_TYPE (in_otype
);
4886 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
4889 warning_at (loc
, OPT_Wcast_qual
,
4890 "to be safe all intermediate pointers in cast from "
4891 "%qT to %qT must be %<const%> qualified",
4896 is_const
= TYPE_READONLY (in_type
);
4898 while (TREE_CODE (in_type
) == POINTER_TYPE
);
4901 /* Build an expression representing a cast to type TYPE of expression EXPR.
4902 LOC is the location of the cast-- typically the open paren of the cast. */
4905 build_c_cast (location_t loc
, tree type
, tree expr
)
4909 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
4910 expr
= TREE_OPERAND (expr
, 0);
4914 if (type
== error_mark_node
|| expr
== error_mark_node
)
4915 return error_mark_node
;
4917 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4918 only in <protocol> qualifications. But when constructing cast expressions,
4919 the protocols do matter and must be kept around. */
4920 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
4921 return build1 (NOP_EXPR
, type
, expr
);
4923 type
= TYPE_MAIN_VARIANT (type
);
4925 if (TREE_CODE (type
) == ARRAY_TYPE
)
4927 error_at (loc
, "cast specifies array type");
4928 return error_mark_node
;
4931 if (TREE_CODE (type
) == FUNCTION_TYPE
)
4933 error_at (loc
, "cast specifies function type");
4934 return error_mark_node
;
4937 if (!VOID_TYPE_P (type
))
4939 value
= require_complete_type (value
);
4940 if (value
== error_mark_node
)
4941 return error_mark_node
;
4944 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
4946 if (TREE_CODE (type
) == RECORD_TYPE
4947 || TREE_CODE (type
) == UNION_TYPE
)
4948 pedwarn (loc
, OPT_Wpedantic
,
4949 "ISO C forbids casting nonscalar to the same type");
4951 else if (TREE_CODE (type
) == UNION_TYPE
)
4955 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4956 if (TREE_TYPE (field
) != error_mark_node
4957 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
4958 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
4964 bool maybe_const
= true;
4966 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids casts to union type");
4967 t
= c_fully_fold (value
, false, &maybe_const
);
4968 t
= build_constructor_single (type
, field
, t
);
4970 t
= c_wrap_maybe_const (t
, true);
4971 t
= digest_init (loc
, type
, t
,
4972 NULL_TREE
, false, true, 0);
4973 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
4976 error_at (loc
, "cast to union type from type not present in union");
4977 return error_mark_node
;
4983 if (type
== void_type_node
)
4985 tree t
= build1 (CONVERT_EXPR
, type
, value
);
4986 SET_EXPR_LOCATION (t
, loc
);
4990 otype
= TREE_TYPE (value
);
4992 /* Optionally warn about potentially worrisome casts. */
4994 && TREE_CODE (type
) == POINTER_TYPE
4995 && TREE_CODE (otype
) == POINTER_TYPE
)
4996 handle_warn_cast_qual (loc
, type
, otype
);
4998 /* Warn about conversions between pointers to disjoint
5000 if (TREE_CODE (type
) == POINTER_TYPE
5001 && TREE_CODE (otype
) == POINTER_TYPE
5002 && !null_pointer_constant_p (value
))
5004 addr_space_t as_to
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
5005 addr_space_t as_from
= TYPE_ADDR_SPACE (TREE_TYPE (otype
));
5006 addr_space_t as_common
;
5008 if (!addr_space_superset (as_to
, as_from
, &as_common
))
5010 if (ADDR_SPACE_GENERIC_P (as_from
))
5011 warning_at (loc
, 0, "cast to %s address space pointer "
5012 "from disjoint generic address space pointer",
5013 c_addr_space_name (as_to
));
5015 else if (ADDR_SPACE_GENERIC_P (as_to
))
5016 warning_at (loc
, 0, "cast to generic address space pointer "
5017 "from disjoint %s address space pointer",
5018 c_addr_space_name (as_from
));
5021 warning_at (loc
, 0, "cast to %s address space pointer "
5022 "from disjoint %s address space pointer",
5023 c_addr_space_name (as_to
),
5024 c_addr_space_name (as_from
));
5028 /* Warn about possible alignment problems. */
5029 if (STRICT_ALIGNMENT
5030 && TREE_CODE (type
) == POINTER_TYPE
5031 && TREE_CODE (otype
) == POINTER_TYPE
5032 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5033 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5034 /* Don't warn about opaque types, where the actual alignment
5035 restriction is unknown. */
5036 && !((TREE_CODE (TREE_TYPE (otype
)) == UNION_TYPE
5037 || TREE_CODE (TREE_TYPE (otype
)) == RECORD_TYPE
)
5038 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
5039 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5040 warning_at (loc
, OPT_Wcast_align
,
5041 "cast increases required alignment of target type");
5043 if (TREE_CODE (type
) == INTEGER_TYPE
5044 && TREE_CODE (otype
) == POINTER_TYPE
5045 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5046 /* Unlike conversion of integers to pointers, where the
5047 warning is disabled for converting constants because
5048 of cases such as SIG_*, warn about converting constant
5049 pointers to integers. In some cases it may cause unwanted
5050 sign extension, and a warning is appropriate. */
5051 warning_at (loc
, OPT_Wpointer_to_int_cast
,
5052 "cast from pointer to integer of different size");
5054 if (TREE_CODE (value
) == CALL_EXPR
5055 && TREE_CODE (type
) != TREE_CODE (otype
))
5056 warning_at (loc
, OPT_Wbad_function_cast
,
5057 "cast from function call of type %qT "
5058 "to non-matching type %qT", otype
, type
);
5060 if (TREE_CODE (type
) == POINTER_TYPE
5061 && TREE_CODE (otype
) == INTEGER_TYPE
5062 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5063 /* Don't warn about converting any constant. */
5064 && !TREE_CONSTANT (value
))
5066 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
5067 "of different size");
5069 if (warn_strict_aliasing
<= 2)
5070 strict_aliasing_warning (otype
, type
, expr
);
5072 /* If pedantic, warn for conversions between function and object
5073 pointer types, except for converting a null pointer constant
5074 to function pointer type. */
5076 && TREE_CODE (type
) == POINTER_TYPE
5077 && TREE_CODE (otype
) == POINTER_TYPE
5078 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
5079 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
5080 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5081 "conversion of function pointer to object pointer type");
5084 && TREE_CODE (type
) == POINTER_TYPE
5085 && TREE_CODE (otype
) == POINTER_TYPE
5086 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
5087 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5088 && !null_pointer_constant_p (value
))
5089 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5090 "conversion of object pointer to function pointer type");
5093 value
= convert (type
, value
);
5095 /* Ignore any integer overflow caused by the cast. */
5096 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
5098 if (CONSTANT_CLASS_P (ovalue
) && TREE_OVERFLOW (ovalue
))
5100 if (!TREE_OVERFLOW (value
))
5102 /* Avoid clobbering a shared constant. */
5103 value
= copy_node (value
);
5104 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5107 else if (TREE_OVERFLOW (value
))
5108 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5109 value
= wide_int_to_tree (TREE_TYPE (value
), value
);
5113 /* Don't let a cast be an lvalue. */
5115 value
= non_lvalue_loc (loc
, value
);
5117 /* Don't allow the results of casting to floating-point or complex
5118 types be confused with actual constants, or casts involving
5119 integer and pointer types other than direct integer-to-integer
5120 and integer-to-pointer be confused with integer constant
5121 expressions and null pointer constants. */
5122 if (TREE_CODE (value
) == REAL_CST
5123 || TREE_CODE (value
) == COMPLEX_CST
5124 || (TREE_CODE (value
) == INTEGER_CST
5125 && !((TREE_CODE (expr
) == INTEGER_CST
5126 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
5127 || TREE_CODE (expr
) == REAL_CST
5128 || TREE_CODE (expr
) == COMPLEX_CST
)))
5129 value
= build1 (NOP_EXPR
, type
, value
);
5131 if (CAN_HAVE_LOCATION_P (value
))
5132 SET_EXPR_LOCATION (value
, loc
);
5136 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5137 location of the open paren of the cast, or the position of the cast
5140 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
5143 tree type_expr
= NULL_TREE
;
5144 bool type_expr_const
= true;
5146 int saved_wsp
= warn_strict_prototypes
;
5148 /* This avoids warnings about unprototyped casts on
5149 integers. E.g. "#define SIG_DFL (void(*)())0". */
5150 if (TREE_CODE (expr
) == INTEGER_CST
)
5151 warn_strict_prototypes
= 0;
5152 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
5153 warn_strict_prototypes
= saved_wsp
;
5155 ret
= build_c_cast (loc
, type
, expr
);
5158 bool inner_expr_const
= true;
5159 ret
= c_fully_fold (ret
, require_constant_value
, &inner_expr_const
);
5160 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
5161 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !(type_expr_const
5162 && inner_expr_const
);
5163 SET_EXPR_LOCATION (ret
, loc
);
5166 if (CAN_HAVE_LOCATION_P (ret
) && !EXPR_HAS_LOCATION (ret
))
5167 SET_EXPR_LOCATION (ret
, loc
);
5169 /* C++ does not permits types to be defined in a cast, but it
5170 allows references to incomplete types. */
5171 if (warn_cxx_compat
&& type_name
->specs
->typespec_kind
== ctsk_tagdef
)
5172 warning_at (loc
, OPT_Wc___compat
,
5173 "defining a type in a cast is invalid in C++");
5178 /* Build an assignment expression of lvalue LHS from value RHS.
5179 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5180 may differ from TREE_TYPE (LHS) for an enum bitfield.
5181 MODIFYCODE is the code for a binary operator that we use
5182 to combine the old value of LHS with RHS to get the new value.
5183 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5184 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5185 which may differ from TREE_TYPE (RHS) for an enum value.
5187 LOCATION is the location of the MODIFYCODE operator.
5188 RHS_LOC is the location of the RHS. */
5191 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
5192 enum tree_code modifycode
,
5193 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
5197 tree rhseval
= NULL_TREE
;
5198 tree rhs_semantic_type
= NULL_TREE
;
5199 tree lhstype
= TREE_TYPE (lhs
);
5200 tree olhstype
= lhstype
;
5204 /* Types that aren't fully specified cannot be used in assignments. */
5205 lhs
= require_complete_type (lhs
);
5207 /* Avoid duplicate error messages from operands that had errors. */
5208 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
5209 return error_mark_node
;
5211 /* Ensure an error for assigning a non-lvalue array to an array in
5213 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
5215 error_at (location
, "assignment to expression with array type");
5216 return error_mark_node
;
5219 /* For ObjC properties, defer this check. */
5220 if (!objc_is_property_ref (lhs
) && !lvalue_or_else (location
, lhs
, lv_assign
))
5221 return error_mark_node
;
5223 is_atomic_op
= really_atomic_lvalue (lhs
);
5225 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5227 rhs_semantic_type
= TREE_TYPE (rhs
);
5228 rhs
= TREE_OPERAND (rhs
, 0);
5233 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
5235 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
5236 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
5238 if (inner
== error_mark_node
)
5239 return error_mark_node
;
5240 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
5241 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
5242 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
5243 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
5244 protected_set_expr_location (result
, location
);
5248 /* If a binary op has been requested, combine the old LHS value with the RHS
5249 producing the value we should actually store into the LHS. */
5251 if (modifycode
!= NOP_EXPR
)
5253 lhs
= c_fully_fold (lhs
, false, NULL
);
5254 lhs
= stabilize_reference (lhs
);
5256 /* Construct the RHS for any non-atomic compound assignemnt. */
5259 /* If in LHS op= RHS the RHS has side-effects, ensure they
5260 are preevaluated before the rest of the assignment expression's
5261 side-effects, because RHS could contain e.g. function calls
5263 if (TREE_SIDE_EFFECTS (rhs
))
5265 newrhs
= in_late_binary_op
? save_expr (rhs
) : c_save_expr (rhs
);
5268 newrhs
= build_binary_op (location
,
5269 modifycode
, lhs
, newrhs
, 1);
5271 /* The original type of the right hand side is no longer
5273 rhs_origtype
= NULL_TREE
;
5277 if (c_dialect_objc ())
5279 /* Check if we are modifying an Objective-C property reference;
5280 if so, we need to generate setter calls. */
5281 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
5285 /* Else, do the check that we postponed for Objective-C. */
5286 if (!lvalue_or_else (location
, lhs
, lv_assign
))
5287 return error_mark_node
;
5290 /* Give an error for storing in something that is 'const'. */
5292 if (TYPE_READONLY (lhstype
)
5293 || ((TREE_CODE (lhstype
) == RECORD_TYPE
5294 || TREE_CODE (lhstype
) == UNION_TYPE
)
5295 && C_TYPE_FIELDS_READONLY (lhstype
)))
5297 readonly_error (location
, lhs
, lv_assign
);
5298 return error_mark_node
;
5300 else if (TREE_READONLY (lhs
))
5301 readonly_warning (lhs
, lv_assign
);
5303 /* If storing into a structure or union member,
5304 it has probably been given type `int'.
5305 Compute the type that would go with
5306 the actual amount of storage the member occupies. */
5308 if (TREE_CODE (lhs
) == COMPONENT_REF
5309 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5310 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
5311 || TREE_CODE (lhstype
) == REAL_TYPE
5312 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5313 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5315 /* If storing in a field that is in actuality a short or narrower than one,
5316 we must store in the field in its actual type. */
5318 if (lhstype
!= TREE_TYPE (lhs
))
5320 lhs
= copy_node (lhs
);
5321 TREE_TYPE (lhs
) = lhstype
;
5324 /* Issue -Wc++-compat warnings about an assignment to an enum type
5325 when LHS does not have its original type. This happens for,
5326 e.g., an enum bitfield in a struct. */
5328 && lhs_origtype
!= NULL_TREE
5329 && lhs_origtype
!= lhstype
5330 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
5332 tree checktype
= (rhs_origtype
!= NULL_TREE
5335 if (checktype
!= error_mark_node
5336 && (TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
)
5337 || (is_atomic_op
&& modifycode
!= NOP_EXPR
)))
5338 warning_at (location
, OPT_Wc___compat
,
5339 "enum conversion in assignment is invalid in C++");
5342 /* If the lhs is atomic, remove that qualifier. */
5345 lhstype
= build_qualified_type (lhstype
,
5346 (TYPE_QUALS (lhstype
)
5347 & ~TYPE_QUAL_ATOMIC
));
5348 olhstype
= build_qualified_type (olhstype
,
5349 (TYPE_QUALS (lhstype
)
5350 & ~TYPE_QUAL_ATOMIC
));
5353 /* Convert new value to destination type. Fold it first, then
5354 restore any excess precision information, for the sake of
5355 conversion warnings. */
5357 if (!(is_atomic_op
&& modifycode
!= NOP_EXPR
))
5359 npc
= null_pointer_constant_p (newrhs
);
5360 newrhs
= c_fully_fold (newrhs
, false, NULL
);
5361 if (rhs_semantic_type
)
5362 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
5363 newrhs
= convert_for_assignment (location
, rhs_loc
, lhstype
, newrhs
,
5364 rhs_origtype
, ic_assign
, npc
,
5365 NULL_TREE
, NULL_TREE
, 0);
5366 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5367 return error_mark_node
;
5370 /* Emit ObjC write barrier, if necessary. */
5371 if (c_dialect_objc () && flag_objc_gc
)
5373 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
5376 protected_set_expr_location (result
, location
);
5381 /* Scan operands. */
5384 result
= build_atomic_assign (location
, lhs
, modifycode
, newrhs
, false);
5387 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
5388 TREE_SIDE_EFFECTS (result
) = 1;
5389 protected_set_expr_location (result
, location
);
5392 /* If we got the LHS in a different type for storing in,
5393 convert the result back to the nominal type of LHS
5394 so that the value we return always has the same type
5395 as the LHS argument. */
5397 if (olhstype
== TREE_TYPE (result
))
5400 result
= convert_for_assignment (location
, rhs_loc
, olhstype
, result
,
5401 rhs_origtype
, ic_assign
, false, NULL_TREE
,
5403 protected_set_expr_location (result
, location
);
5407 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), rhseval
, result
);
5411 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5412 This is used to implement -fplan9-extensions. */
5415 find_anonymous_field_with_type (tree struct_type
, tree type
)
5420 gcc_assert (TREE_CODE (struct_type
) == RECORD_TYPE
5421 || TREE_CODE (struct_type
) == UNION_TYPE
);
5423 for (field
= TYPE_FIELDS (struct_type
);
5425 field
= TREE_CHAIN (field
))
5427 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5428 ? c_build_qualified_type (TREE_TYPE (field
),
5430 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5431 if (DECL_NAME (field
) == NULL
5432 && comptypes (type
, fieldtype
))
5438 else if (DECL_NAME (field
) == NULL
5439 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
5440 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
5441 && find_anonymous_field_with_type (TREE_TYPE (field
), type
))
5451 /* RHS is an expression whose type is pointer to struct. If there is
5452 an anonymous field in RHS with type TYPE, then return a pointer to
5453 that field in RHS. This is used with -fplan9-extensions. This
5454 returns NULL if no conversion could be found. */
5457 convert_to_anonymous_field (location_t location
, tree type
, tree rhs
)
5459 tree rhs_struct_type
, lhs_main_type
;
5460 tree field
, found_field
;
5461 bool found_sub_field
;
5464 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs
)));
5465 rhs_struct_type
= TREE_TYPE (TREE_TYPE (rhs
));
5466 gcc_assert (TREE_CODE (rhs_struct_type
) == RECORD_TYPE
5467 || TREE_CODE (rhs_struct_type
) == UNION_TYPE
);
5469 gcc_assert (POINTER_TYPE_P (type
));
5470 lhs_main_type
= (TYPE_ATOMIC (TREE_TYPE (type
))
5471 ? c_build_qualified_type (TREE_TYPE (type
),
5473 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
5475 found_field
= NULL_TREE
;
5476 found_sub_field
= false;
5477 for (field
= TYPE_FIELDS (rhs_struct_type
);
5479 field
= TREE_CHAIN (field
))
5481 if (DECL_NAME (field
) != NULL_TREE
5482 || (TREE_CODE (TREE_TYPE (field
)) != RECORD_TYPE
5483 && TREE_CODE (TREE_TYPE (field
)) != UNION_TYPE
))
5485 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5486 ? c_build_qualified_type (TREE_TYPE (field
),
5488 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5489 if (comptypes (lhs_main_type
, fieldtype
))
5491 if (found_field
!= NULL_TREE
)
5493 found_field
= field
;
5495 else if (find_anonymous_field_with_type (TREE_TYPE (field
),
5498 if (found_field
!= NULL_TREE
)
5500 found_field
= field
;
5501 found_sub_field
= true;
5505 if (found_field
== NULL_TREE
)
5508 ret
= fold_build3_loc (location
, COMPONENT_REF
, TREE_TYPE (found_field
),
5509 build_fold_indirect_ref (rhs
), found_field
,
5511 ret
= build_fold_addr_expr_loc (location
, ret
);
5513 if (found_sub_field
)
5515 ret
= convert_to_anonymous_field (location
, type
, ret
);
5516 gcc_assert (ret
!= NULL_TREE
);
5522 /* Issue an error message for a bad initializer component.
5523 GMSGID identifies the message.
5524 The component name is taken from the spelling stack. */
5527 error_init (location_t loc
, const char *gmsgid
)
5531 /* The gmsgid may be a format string with %< and %>. */
5532 error_at (loc
, gmsgid
);
5533 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5535 inform (loc
, "(near initialization for %qs)", ofwhat
);
5538 /* Issue a pedantic warning for a bad initializer component. OPT is
5539 the option OPT_* (from options.h) controlling this warning or 0 if
5540 it is unconditionally given. GMSGID identifies the message. The
5541 component name is taken from the spelling stack. */
5544 pedwarn_init (location_t location
, int opt
, const char *gmsgid
)
5549 /* The gmsgid may be a format string with %< and %>. */
5550 warned
= pedwarn (location
, opt
, gmsgid
);
5551 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5552 if (*ofwhat
&& warned
)
5553 inform (location
, "(near initialization for %qs)", ofwhat
);
5556 /* Issue a warning for a bad initializer component.
5558 OPT is the OPT_W* value corresponding to the warning option that
5559 controls this warning. GMSGID identifies the message. The
5560 component name is taken from the spelling stack. */
5563 warning_init (location_t loc
, int opt
, const char *gmsgid
)
5568 /* The gmsgid may be a format string with %< and %>. */
5569 warned
= warning_at (loc
, opt
, gmsgid
);
5570 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5571 if (*ofwhat
&& warned
)
5572 inform (loc
, "(near initialization for %qs)", ofwhat
);
5575 /* If TYPE is an array type and EXPR is a parenthesized string
5576 constant, warn if pedantic that EXPR is being used to initialize an
5577 object of type TYPE. */
5580 maybe_warn_string_init (location_t loc
, tree type
, struct c_expr expr
)
5583 && TREE_CODE (type
) == ARRAY_TYPE
5584 && TREE_CODE (expr
.value
) == STRING_CST
5585 && expr
.original_code
!= STRING_CST
)
5586 pedwarn_init (loc
, OPT_Wpedantic
,
5587 "array initialized from parenthesized string constant");
5590 /* Convert value RHS to type TYPE as preparation for an assignment to
5591 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5592 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5593 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5594 constant before any folding.
5595 The real work of conversion is done by `convert'.
5596 The purpose of this function is to generate error messages
5597 for assignments that are not allowed in C.
5598 ERRTYPE says whether it is argument passing, assignment,
5599 initialization or return.
5601 LOCATION is the location of the assignment, EXPR_LOC is the location of
5602 the RHS or, for a function, location of an argument.
5603 FUNCTION is a tree for the function being called.
5604 PARMNUM is the number of the argument, for printing in error messages. */
5607 convert_for_assignment (location_t location
, location_t expr_loc
, tree type
,
5608 tree rhs
, tree origtype
, enum impl_conv errtype
,
5609 bool null_pointer_constant
, tree fundecl
,
5610 tree function
, int parmnum
)
5612 enum tree_code codel
= TREE_CODE (type
);
5613 tree orig_rhs
= rhs
;
5615 enum tree_code coder
;
5616 tree rname
= NULL_TREE
;
5617 bool objc_ok
= false;
5619 if (errtype
== ic_argpass
)
5622 /* Change pointer to function to the function itself for
5624 if (TREE_CODE (function
) == ADDR_EXPR
5625 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
5626 function
= TREE_OPERAND (function
, 0);
5628 /* Handle an ObjC selector specially for diagnostics. */
5629 selector
= objc_message_selector ();
5631 if (selector
&& parmnum
> 2)
5638 /* This macro is used to emit diagnostics to ensure that all format
5639 strings are complete sentences, visible to gettext and checked at
5641 #define WARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5646 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5647 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5648 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5649 "expected %qT but argument is of type %qT", \
5653 pedwarn (LOCATION, OPT, AS); \
5656 pedwarn_init (LOCATION, OPT, IN); \
5659 pedwarn (LOCATION, OPT, RE); \
5662 gcc_unreachable (); \
5666 /* This macro is used to emit diagnostics to ensure that all format
5667 strings are complete sentences, visible to gettext and checked at
5668 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5669 extra parameter to enumerate qualifiers. */
5671 #define WARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5676 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5677 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5678 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5679 "expected %qT but argument is of type %qT", \
5683 pedwarn (LOCATION, OPT, AS, QUALS); \
5686 pedwarn (LOCATION, OPT, IN, QUALS); \
5689 pedwarn (LOCATION, OPT, RE, QUALS); \
5692 gcc_unreachable (); \
5696 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5697 rhs
= TREE_OPERAND (rhs
, 0);
5699 rhstype
= TREE_TYPE (rhs
);
5700 coder
= TREE_CODE (rhstype
);
5702 if (coder
== ERROR_MARK
)
5703 return error_mark_node
;
5705 if (c_dialect_objc ())
5728 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
5731 if (warn_cxx_compat
)
5733 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
5734 if (checktype
!= error_mark_node
5735 && TREE_CODE (type
) == ENUMERAL_TYPE
5736 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
5738 WARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wc___compat
,
5739 G_("enum conversion when passing argument "
5740 "%d of %qE is invalid in C++"),
5741 G_("enum conversion in assignment is "
5743 G_("enum conversion in initialization is "
5745 G_("enum conversion in return is "
5750 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
5753 if (coder
== VOID_TYPE
)
5755 /* Except for passing an argument to an unprototyped function,
5756 this is a constraint violation. When passing an argument to
5757 an unprototyped function, it is compile-time undefined;
5758 making it a constraint in that case was rejected in
5760 error_at (location
, "void value not ignored as it ought to be");
5761 return error_mark_node
;
5763 rhs
= require_complete_type (rhs
);
5764 if (rhs
== error_mark_node
)
5765 return error_mark_node
;
5766 /* A non-reference type can convert to a reference. This handles
5767 va_start, va_copy and possibly port built-ins. */
5768 if (codel
== REFERENCE_TYPE
&& coder
!= REFERENCE_TYPE
)
5770 if (!lvalue_p (rhs
))
5772 error_at (location
, "cannot pass rvalue to reference parameter");
5773 return error_mark_node
;
5775 if (!c_mark_addressable (rhs
))
5776 return error_mark_node
;
5777 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
5778 SET_EXPR_LOCATION (rhs
, location
);
5780 rhs
= convert_for_assignment (location
, expr_loc
,
5781 build_pointer_type (TREE_TYPE (type
)),
5782 rhs
, origtype
, errtype
,
5783 null_pointer_constant
, fundecl
, function
,
5785 if (rhs
== error_mark_node
)
5786 return error_mark_node
;
5788 rhs
= build1 (NOP_EXPR
, type
, rhs
);
5789 SET_EXPR_LOCATION (rhs
, location
);
5792 /* Some types can interconvert without explicit casts. */
5793 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
5794 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
5795 return convert (type
, rhs
);
5796 /* Arithmetic types all interconvert, and enum is treated like int. */
5797 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
5798 || codel
== FIXED_POINT_TYPE
5799 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
5800 || codel
== BOOLEAN_TYPE
)
5801 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
5802 || coder
== FIXED_POINT_TYPE
5803 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
5804 || coder
== BOOLEAN_TYPE
))
5807 bool save
= in_late_binary_op
;
5808 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
)
5809 in_late_binary_op
= true;
5810 ret
= convert_and_check (expr_loc
!= UNKNOWN_LOCATION
5811 ? expr_loc
: location
, type
, orig_rhs
);
5812 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
)
5813 in_late_binary_op
= save
;
5817 /* Aggregates in different TUs might need conversion. */
5818 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
5820 && comptypes (type
, rhstype
))
5821 return convert_and_check (expr_loc
!= UNKNOWN_LOCATION
5822 ? expr_loc
: location
, type
, rhs
);
5824 /* Conversion to a transparent union or record from its member types.
5825 This applies only to function arguments. */
5826 if (((codel
== UNION_TYPE
|| codel
== RECORD_TYPE
)
5827 && TYPE_TRANSPARENT_AGGR (type
))
5828 && errtype
== ic_argpass
)
5830 tree memb
, marginal_memb
= NULL_TREE
;
5832 for (memb
= TYPE_FIELDS (type
); memb
; memb
= DECL_CHAIN (memb
))
5834 tree memb_type
= TREE_TYPE (memb
);
5836 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
5837 TYPE_MAIN_VARIANT (rhstype
)))
5840 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
5843 if (coder
== POINTER_TYPE
)
5845 tree ttl
= TREE_TYPE (memb_type
);
5846 tree ttr
= TREE_TYPE (rhstype
);
5848 /* Any non-function converts to a [const][volatile] void *
5849 and vice versa; otherwise, targets must be the same.
5850 Meanwhile, the lhs target must have all the qualifiers of
5852 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
5853 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
5854 || comp_target_types (location
, memb_type
, rhstype
))
5856 int lquals
= TYPE_QUALS (ttl
) & ~TYPE_QUAL_ATOMIC
;
5857 int rquals
= TYPE_QUALS (ttr
) & ~TYPE_QUAL_ATOMIC
;
5858 /* If this type won't generate any warnings, use it. */
5859 if (lquals
== rquals
5860 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
5861 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
5862 ? ((lquals
| rquals
) == rquals
)
5863 : ((lquals
| rquals
) == lquals
)))
5866 /* Keep looking for a better type, but remember this one. */
5868 marginal_memb
= memb
;
5872 /* Can convert integer zero to any pointer type. */
5873 if (null_pointer_constant
)
5875 rhs
= null_pointer_node
;
5880 if (memb
|| marginal_memb
)
5884 /* We have only a marginally acceptable member type;
5885 it needs a warning. */
5886 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
5887 tree ttr
= TREE_TYPE (rhstype
);
5889 /* Const and volatile mean something different for function
5890 types, so the usual warnings are not appropriate. */
5891 if (TREE_CODE (ttr
) == FUNCTION_TYPE
5892 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
5894 /* Because const and volatile on functions are
5895 restrictions that say the function will not do
5896 certain things, it is okay to use a const or volatile
5897 function where an ordinary one is wanted, but not
5899 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
5900 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
5901 WARN_FOR_QUALIFIERS (location
, expr_loc
,
5902 OPT_Wdiscarded_qualifiers
,
5903 G_("passing argument %d of %qE "
5904 "makes %q#v qualified function "
5905 "pointer from unqualified"),
5906 G_("assignment makes %q#v qualified "
5907 "function pointer from "
5909 G_("initialization makes %q#v qualified "
5910 "function pointer from "
5912 G_("return makes %q#v qualified function "
5913 "pointer from unqualified"),
5914 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
5916 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr
)
5917 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl
))
5918 WARN_FOR_QUALIFIERS (location
, expr_loc
,
5919 OPT_Wdiscarded_qualifiers
,
5920 G_("passing argument %d of %qE discards "
5921 "%qv qualifier from pointer target type"),
5922 G_("assignment discards %qv qualifier "
5923 "from pointer target type"),
5924 G_("initialization discards %qv qualifier "
5925 "from pointer target type"),
5926 G_("return discards %qv qualifier from "
5927 "pointer target type"),
5928 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
5930 memb
= marginal_memb
;
5933 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
5934 pedwarn (location
, OPT_Wpedantic
,
5935 "ISO C prohibits argument conversion to union type");
5937 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
5938 return build_constructor_single (type
, memb
, rhs
);
5942 /* Conversions among pointers */
5943 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
5944 && (coder
== codel
))
5946 tree ttl
= TREE_TYPE (type
);
5947 tree ttr
= TREE_TYPE (rhstype
);
5950 bool is_opaque_pointer
;
5951 int target_cmp
= 0; /* Cache comp_target_types () result. */
5955 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
5956 mvl
= (TYPE_ATOMIC (mvl
)
5957 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
),
5959 : TYPE_MAIN_VARIANT (mvl
));
5960 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
5961 mvr
= (TYPE_ATOMIC (mvr
)
5962 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
),
5964 : TYPE_MAIN_VARIANT (mvr
));
5965 /* Opaque pointers are treated like void pointers. */
5966 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
5968 /* The Plan 9 compiler permits a pointer to a struct to be
5969 automatically converted into a pointer to an anonymous field
5970 within the struct. */
5971 if (flag_plan9_extensions
5972 && (TREE_CODE (mvl
) == RECORD_TYPE
|| TREE_CODE(mvl
) == UNION_TYPE
)
5973 && (TREE_CODE (mvr
) == RECORD_TYPE
|| TREE_CODE(mvr
) == UNION_TYPE
)
5976 tree new_rhs
= convert_to_anonymous_field (location
, type
, rhs
);
5977 if (new_rhs
!= NULL_TREE
)
5980 rhstype
= TREE_TYPE (rhs
);
5981 coder
= TREE_CODE (rhstype
);
5982 ttr
= TREE_TYPE (rhstype
);
5983 mvr
= TYPE_MAIN_VARIANT (ttr
);
5987 /* C++ does not allow the implicit conversion void* -> T*. However,
5988 for the purpose of reducing the number of false positives, we
5989 tolerate the special case of
5993 where NULL is typically defined in C to be '(void *) 0'. */
5994 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
5995 warning_at (errtype
== ic_argpass
? expr_loc
: location
,
5997 "request for implicit conversion "
5998 "from %qT to %qT not permitted in C++", rhstype
, type
);
6000 /* See if the pointers point to incompatible address spaces. */
6001 asl
= TYPE_ADDR_SPACE (ttl
);
6002 asr
= TYPE_ADDR_SPACE (ttr
);
6003 if (!null_pointer_constant_p (rhs
)
6004 && asr
!= asl
&& !targetm
.addr_space
.subset_p (asr
, asl
))
6009 error_at (expr_loc
, "passing argument %d of %qE from pointer to "
6010 "non-enclosed address space", parmnum
, rname
);
6013 error_at (location
, "assignment from pointer to "
6014 "non-enclosed address space");
6017 error_at (location
, "initialization from pointer to "
6018 "non-enclosed address space");
6021 error_at (location
, "return from pointer to "
6022 "non-enclosed address space");
6027 return error_mark_node
;
6030 /* Check if the right-hand side has a format attribute but the
6031 left-hand side doesn't. */
6032 if (warn_suggest_attribute_format
6033 && check_missing_format_attribute (type
, rhstype
))
6038 warning_at (expr_loc
, OPT_Wsuggest_attribute_format
,
6039 "argument %d of %qE might be "
6040 "a candidate for a format attribute",
6044 warning_at (location
, OPT_Wsuggest_attribute_format
,
6045 "assignment left-hand side might be "
6046 "a candidate for a format attribute");
6049 warning_at (location
, OPT_Wsuggest_attribute_format
,
6050 "initialization left-hand side might be "
6051 "a candidate for a format attribute");
6054 warning_at (location
, OPT_Wsuggest_attribute_format
,
6055 "return type might be "
6056 "a candidate for a format attribute");
6063 /* Any non-function converts to a [const][volatile] void *
6064 and vice versa; otherwise, targets must be the same.
6065 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6066 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
6067 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
6068 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
6069 || is_opaque_pointer
6070 || ((c_common_unsigned_type (mvl
)
6071 == c_common_unsigned_type (mvr
))
6072 && (c_common_signed_type (mvl
)
6073 == c_common_signed_type (mvr
))
6074 && TYPE_ATOMIC (mvl
) == TYPE_ATOMIC (mvr
)))
6077 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6080 && !null_pointer_constant
6081 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
6082 WARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpedantic
,
6083 G_("ISO C forbids passing argument %d of "
6084 "%qE between function pointer "
6086 G_("ISO C forbids assignment between "
6087 "function pointer and %<void *%>"),
6088 G_("ISO C forbids initialization between "
6089 "function pointer and %<void *%>"),
6090 G_("ISO C forbids return between function "
6091 "pointer and %<void *%>"));
6092 /* Const and volatile mean something different for function types,
6093 so the usual warnings are not appropriate. */
6094 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
6095 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
6097 /* Assignments between atomic and non-atomic objects are OK. */
6098 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
6099 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
6101 WARN_FOR_QUALIFIERS (location
, expr_loc
,
6102 OPT_Wdiscarded_qualifiers
,
6103 G_("passing argument %d of %qE discards "
6104 "%qv qualifier from pointer target type"),
6105 G_("assignment discards %qv qualifier "
6106 "from pointer target type"),
6107 G_("initialization discards %qv qualifier "
6108 "from pointer target type"),
6109 G_("return discards %qv qualifier from "
6110 "pointer target type"),
6111 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6113 /* If this is not a case of ignoring a mismatch in signedness,
6115 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
6118 /* If there is a mismatch, do warn. */
6119 else if (warn_pointer_sign
)
6120 WARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpointer_sign
,
6121 G_("pointer targets in passing argument "
6122 "%d of %qE differ in signedness"),
6123 G_("pointer targets in assignment "
6124 "differ in signedness"),
6125 G_("pointer targets in initialization "
6126 "differ in signedness"),
6127 G_("pointer targets in return differ "
6130 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
6131 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6133 /* Because const and volatile on functions are restrictions
6134 that say the function will not do certain things,
6135 it is okay to use a const or volatile function
6136 where an ordinary one is wanted, but not vice-versa. */
6137 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6138 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6139 WARN_FOR_QUALIFIERS (location
, expr_loc
,
6140 OPT_Wdiscarded_qualifiers
,
6141 G_("passing argument %d of %qE makes "
6142 "%q#v qualified function pointer "
6143 "from unqualified"),
6144 G_("assignment makes %q#v qualified function "
6145 "pointer from unqualified"),
6146 G_("initialization makes %q#v qualified "
6147 "function pointer from unqualified"),
6148 G_("return makes %q#v qualified function "
6149 "pointer from unqualified"),
6150 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6154 /* Avoid warning about the volatile ObjC EH puts on decls. */
6156 WARN_FOR_ASSIGNMENT (location
, expr_loc
,
6157 OPT_Wincompatible_pointer_types
,
6158 G_("passing argument %d of %qE from "
6159 "incompatible pointer type"),
6160 G_("assignment from incompatible pointer type"),
6161 G_("initialization from incompatible "
6163 G_("return from incompatible pointer type"));
6165 return convert (type
, rhs
);
6167 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
6169 /* ??? This should not be an error when inlining calls to
6170 unprototyped functions. */
6171 error_at (location
, "invalid use of non-lvalue array");
6172 return error_mark_node
;
6174 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
6176 /* An explicit constant 0 can convert to a pointer,
6177 or one that results from arithmetic, even including
6178 a cast to integer type. */
6179 if (!null_pointer_constant
)
6180 WARN_FOR_ASSIGNMENT (location
, expr_loc
,
6181 OPT_Wint_conversion
,
6182 G_("passing argument %d of %qE makes "
6183 "pointer from integer without a cast"),
6184 G_("assignment makes pointer from integer "
6186 G_("initialization makes pointer from "
6187 "integer without a cast"),
6188 G_("return makes pointer from integer "
6191 return convert (type
, rhs
);
6193 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
6195 WARN_FOR_ASSIGNMENT (location
, expr_loc
,
6196 OPT_Wint_conversion
,
6197 G_("passing argument %d of %qE makes integer "
6198 "from pointer without a cast"),
6199 G_("assignment makes integer from pointer "
6201 G_("initialization makes integer from pointer "
6203 G_("return makes integer from pointer "
6205 return convert (type
, rhs
);
6207 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
6210 bool save
= in_late_binary_op
;
6211 in_late_binary_op
= true;
6212 ret
= convert (type
, rhs
);
6213 in_late_binary_op
= save
;
6220 error_at (expr_loc
, "incompatible type for argument %d of %qE", parmnum
,
6222 inform ((fundecl
&& !DECL_IS_BUILTIN (fundecl
))
6223 ? DECL_SOURCE_LOCATION (fundecl
) : expr_loc
,
6224 "expected %qT but argument is of type %qT", type
, rhstype
);
6227 error_at (location
, "incompatible types when assigning to type %qT from "
6228 "type %qT", type
, rhstype
);
6232 "incompatible types when initializing type %qT using type %qT",
6237 "incompatible types when returning type %qT but %qT was "
6238 "expected", rhstype
, type
);
6244 return error_mark_node
;
6247 /* If VALUE is a compound expr all of whose expressions are constant, then
6248 return its value. Otherwise, return error_mark_node.
6250 This is for handling COMPOUND_EXPRs as initializer elements
6251 which is allowed with a warning when -pedantic is specified. */
6254 valid_compound_expr_initializer (tree value
, tree endtype
)
6256 if (TREE_CODE (value
) == COMPOUND_EXPR
)
6258 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
6260 return error_mark_node
;
6261 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
6264 else if (!initializer_constant_valid_p (value
, endtype
))
6265 return error_mark_node
;
6270 /* Perform appropriate conversions on the initial value of a variable,
6271 store it in the declaration DECL,
6272 and print any error messages that are appropriate.
6273 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6274 If the init is invalid, store an ERROR_MARK.
6276 INIT_LOC is the location of the initial value. */
6279 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
6284 /* If variable's type was invalidly declared, just ignore it. */
6286 type
= TREE_TYPE (decl
);
6287 if (TREE_CODE (type
) == ERROR_MARK
)
6290 /* Digest the specified initializer into an expression. */
6293 npc
= null_pointer_constant_p (init
);
6294 value
= digest_init (init_loc
, type
, init
, origtype
, npc
,
6295 true, TREE_STATIC (decl
));
6297 /* Store the expression if valid; else report error. */
6299 if (!in_system_header_at (input_location
)
6300 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
6301 warning (OPT_Wtraditional
, "traditional C rejects automatic "
6302 "aggregate initialization");
6304 DECL_INITIAL (decl
) = value
;
6306 /* ANSI wants warnings about out-of-range constant initializers. */
6307 STRIP_TYPE_NOPS (value
);
6308 if (TREE_STATIC (decl
))
6309 constant_expression_warning (value
);
6311 /* Check if we need to set array size from compound literal size. */
6312 if (TREE_CODE (type
) == ARRAY_TYPE
6313 && TYPE_DOMAIN (type
) == 0
6314 && value
!= error_mark_node
)
6316 tree inside_init
= init
;
6318 STRIP_TYPE_NOPS (inside_init
);
6319 inside_init
= fold (inside_init
);
6321 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6323 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6325 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
6327 /* For int foo[] = (int [3]){1}; we need to set array size
6328 now since later on array initializer will be just the
6329 brace enclosed list of the compound literal. */
6330 tree etype
= strip_array_types (TREE_TYPE (decl
));
6331 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6332 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
6334 layout_decl (cldecl
, 0);
6336 = c_build_qualified_type (type
, TYPE_QUALS (etype
));
6342 /* Methods for storing and printing names for error messages. */
6344 /* Implement a spelling stack that allows components of a name to be pushed
6345 and popped. Each element on the stack is this structure. */
6352 unsigned HOST_WIDE_INT i
;
6357 #define SPELLING_STRING 1
6358 #define SPELLING_MEMBER 2
6359 #define SPELLING_BOUNDS 3
6361 static struct spelling
*spelling
; /* Next stack element (unused). */
6362 static struct spelling
*spelling_base
; /* Spelling stack base. */
6363 static int spelling_size
; /* Size of the spelling stack. */
6365 /* Macros to save and restore the spelling stack around push_... functions.
6366 Alternative to SAVE_SPELLING_STACK. */
6368 #define SPELLING_DEPTH() (spelling - spelling_base)
6369 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6371 /* Push an element on the spelling stack with type KIND and assign VALUE
6374 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6376 int depth = SPELLING_DEPTH (); \
6378 if (depth >= spelling_size) \
6380 spelling_size += 10; \
6381 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6383 RESTORE_SPELLING_DEPTH (depth); \
6386 spelling->kind = (KIND); \
6387 spelling->MEMBER = (VALUE); \
6391 /* Push STRING on the stack. Printed literally. */
6394 push_string (const char *string
)
6396 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
6399 /* Push a member name on the stack. Printed as '.' STRING. */
6402 push_member_name (tree decl
)
6404 const char *const string
6406 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
6407 : _("<anonymous>"));
6408 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
6411 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6414 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
6416 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
6419 /* Compute the maximum size in bytes of the printed spelling. */
6422 spelling_length (void)
6427 for (p
= spelling_base
; p
< spelling
; p
++)
6429 if (p
->kind
== SPELLING_BOUNDS
)
6432 size
+= strlen (p
->u
.s
) + 1;
6438 /* Print the spelling to BUFFER and return it. */
6441 print_spelling (char *buffer
)
6446 for (p
= spelling_base
; p
< spelling
; p
++)
6447 if (p
->kind
== SPELLING_BOUNDS
)
6449 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
6455 if (p
->kind
== SPELLING_MEMBER
)
6457 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
6464 /* Digest the parser output INIT as an initializer for type TYPE.
6465 Return a C expression of type TYPE to represent the initial value.
6467 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6469 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6471 If INIT is a string constant, STRICT_STRING is true if it is
6472 unparenthesized or we should not warn here for it being parenthesized.
6473 For other types of INIT, STRICT_STRING is not used.
6475 INIT_LOC is the location of the INIT.
6477 REQUIRE_CONSTANT requests an error if non-constant initializers or
6478 elements are seen. */
6481 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
6482 bool null_pointer_constant
, bool strict_string
,
6483 int require_constant
)
6485 enum tree_code code
= TREE_CODE (type
);
6486 tree inside_init
= init
;
6487 tree semantic_type
= NULL_TREE
;
6488 bool maybe_const
= true;
6490 if (type
== error_mark_node
6492 || init
== error_mark_node
6493 || TREE_TYPE (init
) == error_mark_node
)
6494 return error_mark_node
;
6496 STRIP_TYPE_NOPS (inside_init
);
6498 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
6500 semantic_type
= TREE_TYPE (inside_init
);
6501 inside_init
= TREE_OPERAND (inside_init
, 0);
6503 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
6504 inside_init
= decl_constant_value_for_optimization (inside_init
);
6506 /* Initialization of an array of chars from a string constant
6507 optionally enclosed in braces. */
6509 if (code
== ARRAY_TYPE
&& inside_init
6510 && TREE_CODE (inside_init
) == STRING_CST
)
6513 = (TYPE_ATOMIC (TREE_TYPE (type
))
6514 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
6516 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
6517 /* Note that an array could be both an array of character type
6518 and an array of wchar_t if wchar_t is signed char or unsigned
6520 bool char_array
= (typ1
== char_type_node
6521 || typ1
== signed_char_type_node
6522 || typ1
== unsigned_char_type_node
);
6523 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
6524 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
6525 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
6527 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
6530 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
6531 expr
.value
= inside_init
;
6532 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
6533 expr
.original_type
= NULL
;
6534 maybe_warn_string_init (init_loc
, type
, expr
);
6536 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
6537 pedwarn_init (init_loc
, OPT_Wpedantic
,
6538 "initialization of a flexible array member");
6540 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6541 TYPE_MAIN_VARIANT (type
)))
6546 if (typ2
!= char_type_node
)
6548 error_init (init_loc
, "char-array initialized from wide "
6550 return error_mark_node
;
6555 if (typ2
== char_type_node
)
6557 error_init (init_loc
, "wide character array initialized "
6558 "from non-wide string");
6559 return error_mark_node
;
6561 else if (!comptypes(typ1
, typ2
))
6563 error_init (init_loc
, "wide character array initialized "
6564 "from incompatible wide string");
6565 return error_mark_node
;
6569 TREE_TYPE (inside_init
) = type
;
6570 if (TYPE_DOMAIN (type
) != 0
6571 && TYPE_SIZE (type
) != 0
6572 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
6574 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
6576 /* Subtract the size of a single (possibly wide) character
6577 because it's ok to ignore the terminating null char
6578 that is counted in the length of the constant. */
6579 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
6581 - (TYPE_PRECISION (typ1
)
6583 pedwarn_init (init_loc
, 0,
6584 ("initializer-string for array of chars "
6586 else if (warn_cxx_compat
6587 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
), len
))
6588 warning_at (init_loc
, OPT_Wc___compat
,
6589 ("initializer-string for array chars "
6590 "is too long for C++"));
6595 else if (INTEGRAL_TYPE_P (typ1
))
6597 error_init (init_loc
, "array of inappropriate type initialized "
6598 "from string constant");
6599 return error_mark_node
;
6603 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6604 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6605 below and handle as a constructor. */
6606 if (code
== VECTOR_TYPE
6607 && TREE_CODE (TREE_TYPE (inside_init
)) == VECTOR_TYPE
6608 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
6609 && TREE_CONSTANT (inside_init
))
6611 if (TREE_CODE (inside_init
) == VECTOR_CST
6612 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6613 TYPE_MAIN_VARIANT (type
)))
6616 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
6618 unsigned HOST_WIDE_INT ix
;
6620 bool constant_p
= true;
6622 /* Iterate through elements and check if all constructor
6623 elements are *_CSTs. */
6624 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
6625 if (!CONSTANT_CLASS_P (value
))
6632 return build_vector_from_ctor (type
,
6633 CONSTRUCTOR_ELTS (inside_init
));
6637 if (warn_sequence_point
)
6638 verify_sequence_points (inside_init
);
6640 /* Any type can be initialized
6641 from an expression of the same type, optionally with braces. */
6643 if (inside_init
&& TREE_TYPE (inside_init
) != 0
6644 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6645 TYPE_MAIN_VARIANT (type
))
6646 || (code
== ARRAY_TYPE
6647 && comptypes (TREE_TYPE (inside_init
), type
))
6648 || (code
== VECTOR_TYPE
6649 && comptypes (TREE_TYPE (inside_init
), type
))
6650 || (code
== POINTER_TYPE
6651 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
6652 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
6653 TREE_TYPE (type
)))))
6655 if (code
== POINTER_TYPE
)
6657 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
6659 if (TREE_CODE (inside_init
) == STRING_CST
6660 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6661 inside_init
= array_to_pointer_conversion
6662 (init_loc
, inside_init
);
6665 error_init (init_loc
, "invalid use of non-lvalue array");
6666 return error_mark_node
;
6671 if (code
== VECTOR_TYPE
)
6672 /* Although the types are compatible, we may require a
6674 inside_init
= convert (type
, inside_init
);
6676 if (require_constant
6677 && (code
== VECTOR_TYPE
|| !flag_isoc99
)
6678 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6680 /* As an extension, allow initializing objects with static storage
6681 duration with compound literals (which are then treated just as
6682 the brace enclosed list they contain). Also allow this for
6683 vectors, as we can only assign them with compound literals. */
6684 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6685 inside_init
= DECL_INITIAL (decl
);
6688 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
6689 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
6691 error_init (init_loc
, "array initialized from non-constant array "
6693 return error_mark_node
;
6696 /* Compound expressions can only occur here if -Wpedantic or
6697 -pedantic-errors is specified. In the later case, we always want
6698 an error. In the former case, we simply want a warning. */
6699 if (require_constant
&& pedantic
6700 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
6703 = valid_compound_expr_initializer (inside_init
,
6704 TREE_TYPE (inside_init
));
6705 if (inside_init
== error_mark_node
)
6706 error_init (init_loc
, "initializer element is not constant");
6708 pedwarn_init (init_loc
, OPT_Wpedantic
,
6709 "initializer element is not constant");
6710 if (flag_pedantic_errors
)
6711 inside_init
= error_mark_node
;
6713 else if (require_constant
6714 && !initializer_constant_valid_p (inside_init
,
6715 TREE_TYPE (inside_init
)))
6717 error_init (init_loc
, "initializer element is not constant");
6718 inside_init
= error_mark_node
;
6720 else if (require_constant
&& !maybe_const
)
6721 pedwarn_init (init_loc
, 0,
6722 "initializer element is not a constant expression");
6724 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6725 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
6726 inside_init
= convert_for_assignment (init_loc
, UNKNOWN_LOCATION
,
6727 type
, inside_init
, origtype
,
6728 ic_init
, null_pointer_constant
,
6729 NULL_TREE
, NULL_TREE
, 0);
6733 /* Handle scalar types, including conversions. */
6735 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
6736 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
6737 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
)
6739 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
6740 && (TREE_CODE (init
) == STRING_CST
6741 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
6742 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
6744 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
6747 = convert_for_assignment (init_loc
, UNKNOWN_LOCATION
, type
,
6748 inside_init
, origtype
, ic_init
,
6749 null_pointer_constant
, NULL_TREE
, NULL_TREE
,
6752 /* Check to see if we have already given an error message. */
6753 if (inside_init
== error_mark_node
)
6755 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
6757 error_init (init_loc
, "initializer element is not constant");
6758 inside_init
= error_mark_node
;
6760 else if (require_constant
6761 && !initializer_constant_valid_p (inside_init
,
6762 TREE_TYPE (inside_init
)))
6764 error_init (init_loc
, "initializer element is not computable at "
6766 inside_init
= error_mark_node
;
6768 else if (require_constant
&& !maybe_const
)
6769 pedwarn_init (init_loc
, 0,
6770 "initializer element is not a constant expression");
6775 /* Come here only for records and arrays. */
6777 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
6779 error_init (init_loc
, "variable-sized object may not be initialized");
6780 return error_mark_node
;
6783 error_init (init_loc
, "invalid initializer");
6784 return error_mark_node
;
6787 /* Handle initializers that use braces. */
6789 /* Type of object we are accumulating a constructor for.
6790 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6791 static tree constructor_type
;
6793 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6795 static tree constructor_fields
;
6797 /* For an ARRAY_TYPE, this is the specified index
6798 at which to store the next element we get. */
6799 static tree constructor_index
;
6801 /* For an ARRAY_TYPE, this is the maximum index. */
6802 static tree constructor_max_index
;
6804 /* For a RECORD_TYPE, this is the first field not yet written out. */
6805 static tree constructor_unfilled_fields
;
6807 /* For an ARRAY_TYPE, this is the index of the first element
6808 not yet written out. */
6809 static tree constructor_unfilled_index
;
6811 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6812 This is so we can generate gaps between fields, when appropriate. */
6813 static tree constructor_bit_index
;
6815 /* If we are saving up the elements rather than allocating them,
6816 this is the list of elements so far (in reverse order,
6817 most recent first). */
6818 static vec
<constructor_elt
, va_gc
> *constructor_elements
;
6820 /* 1 if constructor should be incrementally stored into a constructor chain,
6821 0 if all the elements should be kept in AVL tree. */
6822 static int constructor_incremental
;
6824 /* 1 if so far this constructor's elements are all compile-time constants. */
6825 static int constructor_constant
;
6827 /* 1 if so far this constructor's elements are all valid address constants. */
6828 static int constructor_simple
;
6830 /* 1 if this constructor has an element that cannot be part of a
6831 constant expression. */
6832 static int constructor_nonconst
;
6834 /* 1 if this constructor is erroneous so far. */
6835 static int constructor_erroneous
;
6837 /* 1 if this constructor is the universal zero initializer { 0 }. */
6838 static int constructor_zeroinit
;
6840 /* Structure for managing pending initializer elements, organized as an
6845 struct init_node
*left
, *right
;
6846 struct init_node
*parent
;
6853 /* Tree of pending elements at this constructor level.
6854 These are elements encountered out of order
6855 which belong at places we haven't reached yet in actually
6857 Will never hold tree nodes across GC runs. */
6858 static struct init_node
*constructor_pending_elts
;
6860 /* The SPELLING_DEPTH of this constructor. */
6861 static int constructor_depth
;
6863 /* DECL node for which an initializer is being read.
6864 0 means we are reading a constructor expression
6865 such as (struct foo) {...}. */
6866 static tree constructor_decl
;
6868 /* Nonzero if this is an initializer for a top-level decl. */
6869 static int constructor_top_level
;
6871 /* Nonzero if there were any member designators in this initializer. */
6872 static int constructor_designated
;
6874 /* Nesting depth of designator list. */
6875 static int designator_depth
;
6877 /* Nonzero if there were diagnosed errors in this designator list. */
6878 static int designator_erroneous
;
6881 /* This stack has a level for each implicit or explicit level of
6882 structuring in the initializer, including the outermost one. It
6883 saves the values of most of the variables above. */
6885 struct constructor_range_stack
;
6887 struct constructor_stack
6889 struct constructor_stack
*next
;
6894 tree unfilled_index
;
6895 tree unfilled_fields
;
6897 vec
<constructor_elt
, va_gc
> *elements
;
6898 struct init_node
*pending_elts
;
6901 /* If value nonzero, this value should replace the entire
6902 constructor at this level. */
6903 struct c_expr replacement_value
;
6904 struct constructor_range_stack
*range_stack
;
6913 int designator_depth
;
6916 static struct constructor_stack
*constructor_stack
;
6918 /* This stack represents designators from some range designator up to
6919 the last designator in the list. */
6921 struct constructor_range_stack
6923 struct constructor_range_stack
*next
, *prev
;
6924 struct constructor_stack
*stack
;
6931 static struct constructor_range_stack
*constructor_range_stack
;
6933 /* This stack records separate initializers that are nested.
6934 Nested initializers can't happen in ANSI C, but GNU C allows them
6935 in cases like { ... (struct foo) { ... } ... }. */
6937 struct initializer_stack
6939 struct initializer_stack
*next
;
6941 struct constructor_stack
*constructor_stack
;
6942 struct constructor_range_stack
*constructor_range_stack
;
6943 vec
<constructor_elt
, va_gc
> *elements
;
6944 struct spelling
*spelling
;
6945 struct spelling
*spelling_base
;
6948 char require_constant_value
;
6949 char require_constant_elements
;
6952 static struct initializer_stack
*initializer_stack
;
6954 /* Prepare to parse and output the initializer for variable DECL. */
6957 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
)
6960 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
6962 p
->decl
= constructor_decl
;
6963 p
->require_constant_value
= require_constant_value
;
6964 p
->require_constant_elements
= require_constant_elements
;
6965 p
->constructor_stack
= constructor_stack
;
6966 p
->constructor_range_stack
= constructor_range_stack
;
6967 p
->elements
= constructor_elements
;
6968 p
->spelling
= spelling
;
6969 p
->spelling_base
= spelling_base
;
6970 p
->spelling_size
= spelling_size
;
6971 p
->top_level
= constructor_top_level
;
6972 p
->next
= initializer_stack
;
6973 initializer_stack
= p
;
6975 constructor_decl
= decl
;
6976 constructor_designated
= 0;
6977 constructor_top_level
= top_level
;
6979 if (decl
!= 0 && decl
!= error_mark_node
)
6981 require_constant_value
= TREE_STATIC (decl
);
6982 require_constant_elements
6983 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
6984 /* For a scalar, you can always use any value to initialize,
6985 even within braces. */
6986 && (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
6987 || TREE_CODE (TREE_TYPE (decl
)) == RECORD_TYPE
6988 || TREE_CODE (TREE_TYPE (decl
)) == UNION_TYPE
6989 || TREE_CODE (TREE_TYPE (decl
)) == QUAL_UNION_TYPE
));
6990 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
6994 require_constant_value
= 0;
6995 require_constant_elements
= 0;
6996 locus
= _("(anonymous)");
6999 constructor_stack
= 0;
7000 constructor_range_stack
= 0;
7002 found_missing_braces
= 0;
7006 RESTORE_SPELLING_DEPTH (0);
7009 push_string (locus
);
7015 struct initializer_stack
*p
= initializer_stack
;
7017 /* Free the whole constructor stack of this initializer. */
7018 while (constructor_stack
)
7020 struct constructor_stack
*q
= constructor_stack
;
7021 constructor_stack
= q
->next
;
7025 gcc_assert (!constructor_range_stack
);
7027 /* Pop back to the data of the outer initializer (if any). */
7028 free (spelling_base
);
7030 constructor_decl
= p
->decl
;
7031 require_constant_value
= p
->require_constant_value
;
7032 require_constant_elements
= p
->require_constant_elements
;
7033 constructor_stack
= p
->constructor_stack
;
7034 constructor_range_stack
= p
->constructor_range_stack
;
7035 constructor_elements
= p
->elements
;
7036 spelling
= p
->spelling
;
7037 spelling_base
= p
->spelling_base
;
7038 spelling_size
= p
->spelling_size
;
7039 constructor_top_level
= p
->top_level
;
7040 initializer_stack
= p
->next
;
7044 /* Call here when we see the initializer is surrounded by braces.
7045 This is instead of a call to push_init_level;
7046 it is matched by a call to pop_init_level.
7048 TYPE is the type to initialize, for a constructor expression.
7049 For an initializer for a decl, TYPE is zero. */
7052 really_start_incremental_init (tree type
)
7054 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
7057 type
= TREE_TYPE (constructor_decl
);
7059 if (TREE_CODE (type
) == VECTOR_TYPE
7060 && TYPE_VECTOR_OPAQUE (type
))
7061 error ("opaque vector types cannot be initialized");
7063 p
->type
= constructor_type
;
7064 p
->fields
= constructor_fields
;
7065 p
->index
= constructor_index
;
7066 p
->max_index
= constructor_max_index
;
7067 p
->unfilled_index
= constructor_unfilled_index
;
7068 p
->unfilled_fields
= constructor_unfilled_fields
;
7069 p
->bit_index
= constructor_bit_index
;
7070 p
->elements
= constructor_elements
;
7071 p
->constant
= constructor_constant
;
7072 p
->simple
= constructor_simple
;
7073 p
->nonconst
= constructor_nonconst
;
7074 p
->erroneous
= constructor_erroneous
;
7075 p
->pending_elts
= constructor_pending_elts
;
7076 p
->depth
= constructor_depth
;
7077 p
->replacement_value
.value
= 0;
7078 p
->replacement_value
.original_code
= ERROR_MARK
;
7079 p
->replacement_value
.original_type
= NULL
;
7083 p
->incremental
= constructor_incremental
;
7084 p
->designated
= constructor_designated
;
7085 p
->designator_depth
= designator_depth
;
7087 constructor_stack
= p
;
7089 constructor_constant
= 1;
7090 constructor_simple
= 1;
7091 constructor_nonconst
= 0;
7092 constructor_depth
= SPELLING_DEPTH ();
7093 constructor_elements
= NULL
;
7094 constructor_pending_elts
= 0;
7095 constructor_type
= type
;
7096 constructor_incremental
= 1;
7097 constructor_designated
= 0;
7098 constructor_zeroinit
= 1;
7099 designator_depth
= 0;
7100 designator_erroneous
= 0;
7102 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7103 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7105 constructor_fields
= TYPE_FIELDS (constructor_type
);
7106 /* Skip any nameless bit fields at the beginning. */
7107 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7108 && DECL_NAME (constructor_fields
) == 0)
7109 constructor_fields
= DECL_CHAIN (constructor_fields
);
7111 constructor_unfilled_fields
= constructor_fields
;
7112 constructor_bit_index
= bitsize_zero_node
;
7114 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7116 if (TYPE_DOMAIN (constructor_type
))
7118 constructor_max_index
7119 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7121 /* Detect non-empty initializations of zero-length arrays. */
7122 if (constructor_max_index
== NULL_TREE
7123 && TYPE_SIZE (constructor_type
))
7124 constructor_max_index
= integer_minus_one_node
;
7126 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7127 to initialize VLAs will cause a proper error; avoid tree
7128 checking errors as well by setting a safe value. */
7129 if (constructor_max_index
7130 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7131 constructor_max_index
= integer_minus_one_node
;
7134 = convert (bitsizetype
,
7135 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7139 constructor_index
= bitsize_zero_node
;
7140 constructor_max_index
= NULL_TREE
;
7143 constructor_unfilled_index
= constructor_index
;
7145 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7147 /* Vectors are like simple fixed-size arrays. */
7148 constructor_max_index
=
7149 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7150 constructor_index
= bitsize_zero_node
;
7151 constructor_unfilled_index
= constructor_index
;
7155 /* Handle the case of int x = {5}; */
7156 constructor_fields
= constructor_type
;
7157 constructor_unfilled_fields
= constructor_type
;
7161 /* Push down into a subobject, for initialization.
7162 If this is for an explicit set of braces, IMPLICIT is 0.
7163 If it is because the next element belongs at a lower level,
7164 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7167 push_init_level (location_t loc
, int implicit
,
7168 struct obstack
*braced_init_obstack
)
7170 struct constructor_stack
*p
;
7171 tree value
= NULL_TREE
;
7173 /* If we've exhausted any levels that didn't have braces,
7174 pop them now. If implicit == 1, this will have been done in
7175 process_init_element; do not repeat it here because in the case
7176 of excess initializers for an empty aggregate this leads to an
7177 infinite cycle of popping a level and immediately recreating
7181 while (constructor_stack
->implicit
)
7183 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7184 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7185 && constructor_fields
== 0)
7186 process_init_element (input_location
,
7187 pop_init_level (loc
, 1, braced_init_obstack
),
7188 true, braced_init_obstack
);
7189 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
7190 && constructor_max_index
7191 && tree_int_cst_lt (constructor_max_index
,
7193 process_init_element (input_location
,
7194 pop_init_level (loc
, 1, braced_init_obstack
),
7195 true, braced_init_obstack
);
7201 /* Unless this is an explicit brace, we need to preserve previous
7205 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7206 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7207 && constructor_fields
)
7208 value
= find_init_member (constructor_fields
, braced_init_obstack
);
7209 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7210 value
= find_init_member (constructor_index
, braced_init_obstack
);
7213 p
= XNEW (struct constructor_stack
);
7214 p
->type
= constructor_type
;
7215 p
->fields
= constructor_fields
;
7216 p
->index
= constructor_index
;
7217 p
->max_index
= constructor_max_index
;
7218 p
->unfilled_index
= constructor_unfilled_index
;
7219 p
->unfilled_fields
= constructor_unfilled_fields
;
7220 p
->bit_index
= constructor_bit_index
;
7221 p
->elements
= constructor_elements
;
7222 p
->constant
= constructor_constant
;
7223 p
->simple
= constructor_simple
;
7224 p
->nonconst
= constructor_nonconst
;
7225 p
->erroneous
= constructor_erroneous
;
7226 p
->pending_elts
= constructor_pending_elts
;
7227 p
->depth
= constructor_depth
;
7228 p
->replacement_value
.value
= 0;
7229 p
->replacement_value
.original_code
= ERROR_MARK
;
7230 p
->replacement_value
.original_type
= NULL
;
7231 p
->implicit
= implicit
;
7233 p
->incremental
= constructor_incremental
;
7234 p
->designated
= constructor_designated
;
7235 p
->designator_depth
= designator_depth
;
7236 p
->next
= constructor_stack
;
7238 constructor_stack
= p
;
7240 constructor_constant
= 1;
7241 constructor_simple
= 1;
7242 constructor_nonconst
= 0;
7243 constructor_depth
= SPELLING_DEPTH ();
7244 constructor_elements
= NULL
;
7245 constructor_incremental
= 1;
7246 constructor_designated
= 0;
7247 constructor_pending_elts
= 0;
7250 p
->range_stack
= constructor_range_stack
;
7251 constructor_range_stack
= 0;
7252 designator_depth
= 0;
7253 designator_erroneous
= 0;
7256 /* Don't die if an entire brace-pair level is superfluous
7257 in the containing level. */
7258 if (constructor_type
== 0)
7260 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
7261 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7263 /* Don't die if there are extra init elts at the end. */
7264 if (constructor_fields
== 0)
7265 constructor_type
= 0;
7268 constructor_type
= TREE_TYPE (constructor_fields
);
7269 push_member_name (constructor_fields
);
7270 constructor_depth
++;
7272 /* If upper initializer is designated, then mark this as
7273 designated too to prevent bogus warnings. */
7274 constructor_designated
= p
->designated
;
7276 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7278 constructor_type
= TREE_TYPE (constructor_type
);
7279 push_array_bounds (tree_to_uhwi (constructor_index
));
7280 constructor_depth
++;
7283 if (constructor_type
== 0)
7285 error_init (loc
, "extra brace group at end of initializer");
7286 constructor_fields
= 0;
7287 constructor_unfilled_fields
= 0;
7291 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
7293 constructor_constant
= TREE_CONSTANT (value
);
7294 constructor_simple
= TREE_STATIC (value
);
7295 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
7296 constructor_elements
= CONSTRUCTOR_ELTS (value
);
7297 if (!vec_safe_is_empty (constructor_elements
)
7298 && (TREE_CODE (constructor_type
) == RECORD_TYPE
7299 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
7300 set_nonincremental_init (braced_init_obstack
);
7304 found_missing_braces
= 1;
7306 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7307 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7309 constructor_fields
= TYPE_FIELDS (constructor_type
);
7310 /* Skip any nameless bit fields at the beginning. */
7311 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7312 && DECL_NAME (constructor_fields
) == 0)
7313 constructor_fields
= DECL_CHAIN (constructor_fields
);
7315 constructor_unfilled_fields
= constructor_fields
;
7316 constructor_bit_index
= bitsize_zero_node
;
7318 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7320 /* Vectors are like simple fixed-size arrays. */
7321 constructor_max_index
=
7322 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7323 constructor_index
= bitsize_int (0);
7324 constructor_unfilled_index
= constructor_index
;
7326 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7328 if (TYPE_DOMAIN (constructor_type
))
7330 constructor_max_index
7331 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7333 /* Detect non-empty initializations of zero-length arrays. */
7334 if (constructor_max_index
== NULL_TREE
7335 && TYPE_SIZE (constructor_type
))
7336 constructor_max_index
= integer_minus_one_node
;
7338 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7339 to initialize VLAs will cause a proper error; avoid tree
7340 checking errors as well by setting a safe value. */
7341 if (constructor_max_index
7342 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7343 constructor_max_index
= integer_minus_one_node
;
7346 = convert (bitsizetype
,
7347 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7350 constructor_index
= bitsize_zero_node
;
7352 constructor_unfilled_index
= constructor_index
;
7353 if (value
&& TREE_CODE (value
) == STRING_CST
)
7355 /* We need to split the char/wchar array into individual
7356 characters, so that we don't have to special case it
7358 set_nonincremental_init_from_string (value
, braced_init_obstack
);
7363 if (constructor_type
!= error_mark_node
)
7364 warning_init (input_location
, 0, "braces around scalar initializer");
7365 constructor_fields
= constructor_type
;
7366 constructor_unfilled_fields
= constructor_type
;
7370 /* At the end of an implicit or explicit brace level,
7371 finish up that level of constructor. If a single expression
7372 with redundant braces initialized that level, return the
7373 c_expr structure for that expression. Otherwise, the original_code
7374 element is set to ERROR_MARK.
7375 If we were outputting the elements as they are read, return 0 as the value
7376 from inner levels (process_init_element ignores that),
7377 but return error_mark_node as the value from the outermost level
7378 (that's what we want to put in DECL_INITIAL).
7379 Otherwise, return a CONSTRUCTOR expression as the value. */
7382 pop_init_level (location_t loc
, int implicit
,
7383 struct obstack
*braced_init_obstack
)
7385 struct constructor_stack
*p
;
7388 ret
.original_code
= ERROR_MARK
;
7389 ret
.original_type
= NULL
;
7393 /* When we come to an explicit close brace,
7394 pop any inner levels that didn't have explicit braces. */
7395 while (constructor_stack
->implicit
)
7396 process_init_element (input_location
,
7397 pop_init_level (loc
, 1, braced_init_obstack
),
7398 true, braced_init_obstack
);
7399 gcc_assert (!constructor_range_stack
);
7402 /* Now output all pending elements. */
7403 constructor_incremental
= 1;
7404 output_pending_init_elements (1, braced_init_obstack
);
7406 p
= constructor_stack
;
7408 /* Error for initializing a flexible array member, or a zero-length
7409 array member in an inappropriate context. */
7410 if (constructor_type
&& constructor_fields
7411 && TREE_CODE (constructor_type
) == ARRAY_TYPE
7412 && TYPE_DOMAIN (constructor_type
)
7413 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
7415 /* Silently discard empty initializations. The parser will
7416 already have pedwarned for empty brackets. */
7417 if (integer_zerop (constructor_unfilled_index
))
7418 constructor_type
= NULL_TREE
;
7421 gcc_assert (!TYPE_SIZE (constructor_type
));
7423 if (constructor_depth
> 2)
7424 error_init (loc
, "initialization of flexible array member in a nested context");
7426 pedwarn_init (loc
, OPT_Wpedantic
,
7427 "initialization of a flexible array member");
7429 /* We have already issued an error message for the existence
7430 of a flexible array member not at the end of the structure.
7431 Discard the initializer so that we do not die later. */
7432 if (DECL_CHAIN (constructor_fields
) != NULL_TREE
)
7433 constructor_type
= NULL_TREE
;
7437 if (vec_safe_length (constructor_elements
) != 1)
7438 constructor_zeroinit
= 0;
7440 /* Warn when some structs are initialized with direct aggregation. */
7441 if (!implicit
&& found_missing_braces
&& warn_missing_braces
7442 && !constructor_zeroinit
)
7444 warning_init (loc
, OPT_Wmissing_braces
,
7445 "missing braces around initializer");
7448 /* Warn when some struct elements are implicitly initialized to zero. */
7449 if (warn_missing_field_initializers
7451 && TREE_CODE (constructor_type
) == RECORD_TYPE
7452 && constructor_unfilled_fields
)
7454 /* Do not warn for flexible array members or zero-length arrays. */
7455 while (constructor_unfilled_fields
7456 && (!DECL_SIZE (constructor_unfilled_fields
)
7457 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
7458 constructor_unfilled_fields
= DECL_CHAIN (constructor_unfilled_fields
);
7460 if (constructor_unfilled_fields
7461 /* Do not warn if this level of the initializer uses member
7462 designators; it is likely to be deliberate. */
7463 && !constructor_designated
7464 /* Do not warn about initializing with ` = {0}'. */
7465 && !constructor_zeroinit
)
7467 if (warning_at (input_location
, OPT_Wmissing_field_initializers
,
7468 "missing initializer for field %qD of %qT",
7469 constructor_unfilled_fields
,
7471 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields
),
7472 "%qD declared here", constructor_unfilled_fields
);
7476 /* Pad out the end of the structure. */
7477 if (p
->replacement_value
.value
)
7478 /* If this closes a superfluous brace pair,
7479 just pass out the element between them. */
7480 ret
= p
->replacement_value
;
7481 else if (constructor_type
== 0)
7483 else if (TREE_CODE (constructor_type
) != RECORD_TYPE
7484 && TREE_CODE (constructor_type
) != UNION_TYPE
7485 && TREE_CODE (constructor_type
) != ARRAY_TYPE
7486 && TREE_CODE (constructor_type
) != VECTOR_TYPE
)
7488 /* A nonincremental scalar initializer--just return
7489 the element, after verifying there is just one. */
7490 if (vec_safe_is_empty (constructor_elements
))
7492 if (!constructor_erroneous
)
7493 error_init (loc
, "empty scalar initializer");
7494 ret
.value
= error_mark_node
;
7496 else if (vec_safe_length (constructor_elements
) != 1)
7498 error_init (loc
, "extra elements in scalar initializer");
7499 ret
.value
= (*constructor_elements
)[0].value
;
7502 ret
.value
= (*constructor_elements
)[0].value
;
7506 if (constructor_erroneous
)
7507 ret
.value
= error_mark_node
;
7510 ret
.value
= build_constructor (constructor_type
,
7511 constructor_elements
);
7512 if (constructor_constant
)
7513 TREE_CONSTANT (ret
.value
) = 1;
7514 if (constructor_constant
&& constructor_simple
)
7515 TREE_STATIC (ret
.value
) = 1;
7516 if (constructor_nonconst
)
7517 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
7521 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
7523 if (constructor_nonconst
)
7524 ret
.original_code
= C_MAYBE_CONST_EXPR
;
7525 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
7526 ret
.original_code
= ERROR_MARK
;
7529 constructor_type
= p
->type
;
7530 constructor_fields
= p
->fields
;
7531 constructor_index
= p
->index
;
7532 constructor_max_index
= p
->max_index
;
7533 constructor_unfilled_index
= p
->unfilled_index
;
7534 constructor_unfilled_fields
= p
->unfilled_fields
;
7535 constructor_bit_index
= p
->bit_index
;
7536 constructor_elements
= p
->elements
;
7537 constructor_constant
= p
->constant
;
7538 constructor_simple
= p
->simple
;
7539 constructor_nonconst
= p
->nonconst
;
7540 constructor_erroneous
= p
->erroneous
;
7541 constructor_incremental
= p
->incremental
;
7542 constructor_designated
= p
->designated
;
7543 designator_depth
= p
->designator_depth
;
7544 constructor_pending_elts
= p
->pending_elts
;
7545 constructor_depth
= p
->depth
;
7547 constructor_range_stack
= p
->range_stack
;
7548 RESTORE_SPELLING_DEPTH (constructor_depth
);
7550 constructor_stack
= p
->next
;
7553 if (ret
.value
== 0 && constructor_stack
== 0)
7554 ret
.value
= error_mark_node
;
7558 /* Common handling for both array range and field name designators.
7559 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7562 set_designator (location_t loc
, int array
,
7563 struct obstack
*braced_init_obstack
)
7566 enum tree_code subcode
;
7568 /* Don't die if an entire brace-pair level is superfluous
7569 in the containing level. */
7570 if (constructor_type
== 0)
7573 /* If there were errors in this designator list already, bail out
7575 if (designator_erroneous
)
7578 if (!designator_depth
)
7580 gcc_assert (!constructor_range_stack
);
7582 /* Designator list starts at the level of closest explicit
7584 while (constructor_stack
->implicit
)
7585 process_init_element (input_location
,
7586 pop_init_level (loc
, 1, braced_init_obstack
),
7587 true, braced_init_obstack
);
7588 constructor_designated
= 1;
7592 switch (TREE_CODE (constructor_type
))
7596 subtype
= TREE_TYPE (constructor_fields
);
7597 if (subtype
!= error_mark_node
)
7598 subtype
= TYPE_MAIN_VARIANT (subtype
);
7601 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
7607 subcode
= TREE_CODE (subtype
);
7608 if (array
&& subcode
!= ARRAY_TYPE
)
7610 error_init (loc
, "array index in non-array initializer");
7613 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
7615 error_init (loc
, "field name not in record or union initializer");
7619 constructor_designated
= 1;
7620 push_init_level (loc
, 2, braced_init_obstack
);
7624 /* If there are range designators in designator list, push a new designator
7625 to constructor_range_stack. RANGE_END is end of such stack range or
7626 NULL_TREE if there is no range designator at this level. */
7629 push_range_stack (tree range_end
, struct obstack
* braced_init_obstack
)
7631 struct constructor_range_stack
*p
;
7633 p
= (struct constructor_range_stack
*)
7634 obstack_alloc (braced_init_obstack
,
7635 sizeof (struct constructor_range_stack
));
7636 p
->prev
= constructor_range_stack
;
7638 p
->fields
= constructor_fields
;
7639 p
->range_start
= constructor_index
;
7640 p
->index
= constructor_index
;
7641 p
->stack
= constructor_stack
;
7642 p
->range_end
= range_end
;
7643 if (constructor_range_stack
)
7644 constructor_range_stack
->next
= p
;
7645 constructor_range_stack
= p
;
7648 /* Within an array initializer, specify the next index to be initialized.
7649 FIRST is that index. If LAST is nonzero, then initialize a range
7650 of indices, running from FIRST through LAST. */
7653 set_init_index (location_t loc
, tree first
, tree last
,
7654 struct obstack
*braced_init_obstack
)
7656 if (set_designator (loc
, 1, braced_init_obstack
))
7659 designator_erroneous
= 1;
7661 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
7662 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
7664 error_init (loc
, "array index in initializer not of integer type");
7668 if (TREE_CODE (first
) != INTEGER_CST
)
7670 first
= c_fully_fold (first
, false, NULL
);
7671 if (TREE_CODE (first
) == INTEGER_CST
)
7672 pedwarn_init (loc
, OPT_Wpedantic
,
7673 "array index in initializer is not "
7674 "an integer constant expression");
7677 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
7679 last
= c_fully_fold (last
, false, NULL
);
7680 if (TREE_CODE (last
) == INTEGER_CST
)
7681 pedwarn_init (loc
, OPT_Wpedantic
,
7682 "array index in initializer is not "
7683 "an integer constant expression");
7686 if (TREE_CODE (first
) != INTEGER_CST
)
7687 error_init (loc
, "nonconstant array index in initializer");
7688 else if (last
!= 0 && TREE_CODE (last
) != INTEGER_CST
)
7689 error_init (loc
, "nonconstant array index in initializer");
7690 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
7691 error_init (loc
, "array index in non-array initializer");
7692 else if (tree_int_cst_sgn (first
) == -1)
7693 error_init (loc
, "array index in initializer exceeds array bounds");
7694 else if (constructor_max_index
7695 && tree_int_cst_lt (constructor_max_index
, first
))
7696 error_init (loc
, "array index in initializer exceeds array bounds");
7699 constant_expression_warning (first
);
7701 constant_expression_warning (last
);
7702 constructor_index
= convert (bitsizetype
, first
);
7703 if (tree_int_cst_lt (constructor_index
, first
))
7705 constructor_index
= copy_node (constructor_index
);
7706 TREE_OVERFLOW (constructor_index
) = 1;
7711 if (tree_int_cst_equal (first
, last
))
7713 else if (tree_int_cst_lt (last
, first
))
7715 error_init (loc
, "empty index range in initializer");
7720 last
= convert (bitsizetype
, last
);
7721 if (constructor_max_index
!= 0
7722 && tree_int_cst_lt (constructor_max_index
, last
))
7724 error_init (loc
, "array index range in initializer exceeds "
7732 designator_erroneous
= 0;
7733 if (constructor_range_stack
|| last
)
7734 push_range_stack (last
, braced_init_obstack
);
7738 /* Within a struct initializer, specify the next field to be initialized. */
7741 set_init_label (location_t loc
, tree fieldname
,
7742 struct obstack
*braced_init_obstack
)
7746 if (set_designator (loc
, 0, braced_init_obstack
))
7749 designator_erroneous
= 1;
7751 if (TREE_CODE (constructor_type
) != RECORD_TYPE
7752 && TREE_CODE (constructor_type
) != UNION_TYPE
)
7754 error_init (loc
, "field name not in record or union initializer");
7758 field
= lookup_field (constructor_type
, fieldname
);
7761 error ("unknown field %qE specified in initializer", fieldname
);
7765 constructor_fields
= TREE_VALUE (field
);
7767 designator_erroneous
= 0;
7768 if (constructor_range_stack
)
7769 push_range_stack (NULL_TREE
, braced_init_obstack
);
7770 field
= TREE_CHAIN (field
);
7773 if (set_designator (loc
, 0, braced_init_obstack
))
7777 while (field
!= NULL_TREE
);
7780 /* Add a new initializer to the tree of pending initializers. PURPOSE
7781 identifies the initializer, either array index or field in a structure.
7782 VALUE is the value of that index or field. If ORIGTYPE is not
7783 NULL_TREE, it is the original type of VALUE.
7785 IMPLICIT is true if value comes from pop_init_level (1),
7786 the new initializer has been merged with the existing one
7787 and thus no warnings should be emitted about overriding an
7788 existing initializer. */
7791 add_pending_init (location_t loc
, tree purpose
, tree value
, tree origtype
,
7792 bool implicit
, struct obstack
*braced_init_obstack
)
7794 struct init_node
*p
, **q
, *r
;
7796 q
= &constructor_pending_elts
;
7799 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7804 if (tree_int_cst_lt (purpose
, p
->purpose
))
7806 else if (tree_int_cst_lt (p
->purpose
, purpose
))
7812 if (TREE_SIDE_EFFECTS (p
->value
))
7813 warning_init (loc
, 0,
7814 "initialized field with side-effects "
7816 else if (warn_override_init
)
7817 warning_init (loc
, OPT_Woverride_init
,
7818 "initialized field overwritten");
7821 p
->origtype
= origtype
;
7830 bitpos
= bit_position (purpose
);
7834 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
7836 else if (p
->purpose
!= purpose
)
7842 if (TREE_SIDE_EFFECTS (p
->value
))
7843 warning_init (loc
, 0,
7844 "initialized field with side-effects "
7846 else if (warn_override_init
)
7847 warning_init (loc
, OPT_Woverride_init
,
7848 "initialized field overwritten");
7851 p
->origtype
= origtype
;
7857 r
= (struct init_node
*) obstack_alloc (braced_init_obstack
,
7858 sizeof (struct init_node
));
7859 r
->purpose
= purpose
;
7861 r
->origtype
= origtype
;
7871 struct init_node
*s
;
7875 if (p
->balance
== 0)
7877 else if (p
->balance
< 0)
7884 p
->left
->parent
= p
;
7901 constructor_pending_elts
= r
;
7906 struct init_node
*t
= r
->right
;
7910 r
->right
->parent
= r
;
7915 p
->left
->parent
= p
;
7918 p
->balance
= t
->balance
< 0;
7919 r
->balance
= -(t
->balance
> 0);
7934 constructor_pending_elts
= t
;
7940 /* p->balance == +1; growth of left side balances the node. */
7945 else /* r == p->right */
7947 if (p
->balance
== 0)
7948 /* Growth propagation from right side. */
7950 else if (p
->balance
> 0)
7957 p
->right
->parent
= p
;
7974 constructor_pending_elts
= r
;
7976 else /* r->balance == -1 */
7979 struct init_node
*t
= r
->left
;
7983 r
->left
->parent
= r
;
7988 p
->right
->parent
= p
;
7991 r
->balance
= (t
->balance
< 0);
7992 p
->balance
= -(t
->balance
> 0);
8007 constructor_pending_elts
= t
;
8013 /* p->balance == -1; growth of right side balances the node. */
8024 /* Build AVL tree from a sorted chain. */
8027 set_nonincremental_init (struct obstack
* braced_init_obstack
)
8029 unsigned HOST_WIDE_INT ix
;
8032 if (TREE_CODE (constructor_type
) != RECORD_TYPE
8033 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
8036 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
8037 add_pending_init (input_location
, index
, value
, NULL_TREE
, true,
8038 braced_init_obstack
);
8039 constructor_elements
= NULL
;
8040 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8042 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
8043 /* Skip any nameless bit fields at the beginning. */
8044 while (constructor_unfilled_fields
!= 0
8045 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8046 && DECL_NAME (constructor_unfilled_fields
) == 0)
8047 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
8050 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8052 if (TYPE_DOMAIN (constructor_type
))
8053 constructor_unfilled_index
8054 = convert (bitsizetype
,
8055 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
8057 constructor_unfilled_index
= bitsize_zero_node
;
8059 constructor_incremental
= 0;
8062 /* Build AVL tree from a string constant. */
8065 set_nonincremental_init_from_string (tree str
,
8066 struct obstack
* braced_init_obstack
)
8068 tree value
, purpose
, type
;
8069 HOST_WIDE_INT val
[2];
8070 const char *p
, *end
;
8071 int byte
, wchar_bytes
, charwidth
, bitpos
;
8073 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
8075 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
8076 charwidth
= TYPE_PRECISION (char_type_node
);
8077 type
= TREE_TYPE (constructor_type
);
8078 p
= TREE_STRING_POINTER (str
);
8079 end
= p
+ TREE_STRING_LENGTH (str
);
8081 for (purpose
= bitsize_zero_node
;
8083 && !(constructor_max_index
8084 && tree_int_cst_lt (constructor_max_index
, purpose
));
8085 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
8087 if (wchar_bytes
== 1)
8089 val
[0] = (unsigned char) *p
++;
8096 for (byte
= 0; byte
< wchar_bytes
; byte
++)
8098 if (BYTES_BIG_ENDIAN
)
8099 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
8101 bitpos
= byte
* charwidth
;
8102 val
[bitpos
% HOST_BITS_PER_WIDE_INT
]
8103 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
8104 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
8108 if (!TYPE_UNSIGNED (type
))
8110 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
8111 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
8113 if (val
[0] & (((HOST_WIDE_INT
) 1) << (bitpos
- 1)))
8115 val
[0] |= ((HOST_WIDE_INT
) -1) << bitpos
;
8119 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
8124 else if (val
[1] & (((HOST_WIDE_INT
) 1)
8125 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
8126 val
[1] |= ((HOST_WIDE_INT
) -1)
8127 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
8130 value
= wide_int_to_tree (type
,
8131 wide_int::from_array (val
, 2,
8132 HOST_BITS_PER_WIDE_INT
* 2));
8133 add_pending_init (input_location
, purpose
, value
, NULL_TREE
, true,
8134 braced_init_obstack
);
8137 constructor_incremental
= 0;
8140 /* Return value of FIELD in pending initializer or zero if the field was
8141 not initialized yet. */
8144 find_init_member (tree field
, struct obstack
* braced_init_obstack
)
8146 struct init_node
*p
;
8148 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8150 if (constructor_incremental
8151 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8152 set_nonincremental_init (braced_init_obstack
);
8154 p
= constructor_pending_elts
;
8157 if (tree_int_cst_lt (field
, p
->purpose
))
8159 else if (tree_int_cst_lt (p
->purpose
, field
))
8165 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8167 tree bitpos
= bit_position (field
);
8169 if (constructor_incremental
8170 && (!constructor_unfilled_fields
8171 || tree_int_cst_lt (bitpos
,
8172 bit_position (constructor_unfilled_fields
))))
8173 set_nonincremental_init (braced_init_obstack
);
8175 p
= constructor_pending_elts
;
8178 if (field
== p
->purpose
)
8180 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
8186 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8188 if (!vec_safe_is_empty (constructor_elements
)
8189 && (constructor_elements
->last ().index
== field
))
8190 return constructor_elements
->last ().value
;
8195 /* "Output" the next constructor element.
8196 At top level, really output it to assembler code now.
8197 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8198 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8199 TYPE is the data type that the containing data type wants here.
8200 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8201 If VALUE is a string constant, STRICT_STRING is true if it is
8202 unparenthesized or we should not warn here for it being parenthesized.
8203 For other types of VALUE, STRICT_STRING is not used.
8205 PENDING if non-nil means output pending elements that belong
8206 right after this element. (PENDING is normally 1;
8207 it is 0 while outputting pending elements, to avoid recursion.)
8209 IMPLICIT is true if value comes from pop_init_level (1),
8210 the new initializer has been merged with the existing one
8211 and thus no warnings should be emitted about overriding an
8212 existing initializer. */
8215 output_init_element (location_t loc
, tree value
, tree origtype
,
8216 bool strict_string
, tree type
, tree field
, int pending
,
8217 bool implicit
, struct obstack
* braced_init_obstack
)
8219 tree semantic_type
= NULL_TREE
;
8220 bool maybe_const
= true;
8223 if (type
== error_mark_node
|| value
== error_mark_node
)
8225 constructor_erroneous
= 1;
8228 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
8229 && (TREE_CODE (value
) == STRING_CST
8230 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
8231 && !(TREE_CODE (value
) == STRING_CST
8232 && TREE_CODE (type
) == ARRAY_TYPE
8233 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
8234 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
8235 TYPE_MAIN_VARIANT (type
)))
8236 value
= array_to_pointer_conversion (input_location
, value
);
8238 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
8239 && require_constant_value
&& !flag_isoc99
&& pending
)
8241 /* As an extension, allow initializing objects with static storage
8242 duration with compound literals (which are then treated just as
8243 the brace enclosed list they contain). */
8244 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
8245 value
= DECL_INITIAL (decl
);
8248 npc
= null_pointer_constant_p (value
);
8249 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
8251 semantic_type
= TREE_TYPE (value
);
8252 value
= TREE_OPERAND (value
, 0);
8254 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
8256 if (value
== error_mark_node
)
8257 constructor_erroneous
= 1;
8258 else if (!TREE_CONSTANT (value
))
8259 constructor_constant
= 0;
8260 else if (!initializer_constant_valid_p (value
, TREE_TYPE (value
))
8261 || ((TREE_CODE (constructor_type
) == RECORD_TYPE
8262 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8263 && DECL_C_BIT_FIELD (field
)
8264 && TREE_CODE (value
) != INTEGER_CST
))
8265 constructor_simple
= 0;
8267 constructor_nonconst
= 1;
8269 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
8271 if (require_constant_value
)
8273 error_init (loc
, "initializer element is not constant");
8274 value
= error_mark_node
;
8276 else if (require_constant_elements
)
8277 pedwarn (loc
, OPT_Wpedantic
,
8278 "initializer element is not computable at load time");
8280 else if (!maybe_const
8281 && (require_constant_value
|| require_constant_elements
))
8282 pedwarn_init (loc
, OPT_Wpedantic
,
8283 "initializer element is not a constant expression");
8285 /* Issue -Wc++-compat warnings about initializing a bitfield with
8288 && field
!= NULL_TREE
8289 && TREE_CODE (field
) == FIELD_DECL
8290 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
8291 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
8292 != TYPE_MAIN_VARIANT (type
))
8293 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
8295 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
8296 if (checktype
!= error_mark_node
8297 && (TYPE_MAIN_VARIANT (checktype
)
8298 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
8299 warning_init (loc
, OPT_Wc___compat
,
8300 "enum conversion in initialization is invalid in C++");
8303 /* If this field is empty (and not at the end of structure),
8304 don't do anything other than checking the initializer. */
8306 && (TREE_TYPE (field
) == error_mark_node
8307 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
8308 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
8309 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
8310 || DECL_CHAIN (field
)))))
8314 value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
8315 value
= digest_init (loc
, type
, value
, origtype
, npc
, strict_string
,
8316 require_constant_value
);
8317 if (value
== error_mark_node
)
8319 constructor_erroneous
= 1;
8322 if (require_constant_value
|| require_constant_elements
)
8323 constant_expression_warning (value
);
8325 /* If this element doesn't come next in sequence,
8326 put it on constructor_pending_elts. */
8327 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
8328 && (!constructor_incremental
8329 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
8331 if (constructor_incremental
8332 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8333 set_nonincremental_init (braced_init_obstack
);
8335 add_pending_init (loc
, field
, value
, origtype
, implicit
,
8336 braced_init_obstack
);
8339 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8340 && (!constructor_incremental
8341 || field
!= constructor_unfilled_fields
))
8343 /* We do this for records but not for unions. In a union,
8344 no matter which field is specified, it can be initialized
8345 right away since it starts at the beginning of the union. */
8346 if (constructor_incremental
)
8348 if (!constructor_unfilled_fields
)
8349 set_nonincremental_init (braced_init_obstack
);
8352 tree bitpos
, unfillpos
;
8354 bitpos
= bit_position (field
);
8355 unfillpos
= bit_position (constructor_unfilled_fields
);
8357 if (tree_int_cst_lt (bitpos
, unfillpos
))
8358 set_nonincremental_init (braced_init_obstack
);
8362 add_pending_init (loc
, field
, value
, origtype
, implicit
,
8363 braced_init_obstack
);
8366 else if (TREE_CODE (constructor_type
) == UNION_TYPE
8367 && !vec_safe_is_empty (constructor_elements
))
8371 if (TREE_SIDE_EFFECTS (constructor_elements
->last ().value
))
8372 warning_init (loc
, 0,
8373 "initialized field with side-effects overwritten");
8374 else if (warn_override_init
)
8375 warning_init (loc
, OPT_Woverride_init
,
8376 "initialized field overwritten");
8379 /* We can have just one union field set. */
8380 constructor_elements
= NULL
;
8383 /* Otherwise, output this element either to
8384 constructor_elements or to the assembler file. */
8386 constructor_elt celt
= {field
, value
};
8387 vec_safe_push (constructor_elements
, celt
);
8389 /* Advance the variable that indicates sequential elements output. */
8390 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8391 constructor_unfilled_index
8392 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
8394 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8396 constructor_unfilled_fields
8397 = DECL_CHAIN (constructor_unfilled_fields
);
8399 /* Skip any nameless bit fields. */
8400 while (constructor_unfilled_fields
!= 0
8401 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8402 && DECL_NAME (constructor_unfilled_fields
) == 0)
8403 constructor_unfilled_fields
=
8404 DECL_CHAIN (constructor_unfilled_fields
);
8406 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8407 constructor_unfilled_fields
= 0;
8409 /* Now output any pending elements which have become next. */
8411 output_pending_init_elements (0, braced_init_obstack
);
8414 /* Output any pending elements which have become next.
8415 As we output elements, constructor_unfilled_{fields,index}
8416 advances, which may cause other elements to become next;
8417 if so, they too are output.
8419 If ALL is 0, we return when there are
8420 no more pending elements to output now.
8422 If ALL is 1, we output space as necessary so that
8423 we can output all the pending elements. */
8425 output_pending_init_elements (int all
, struct obstack
* braced_init_obstack
)
8427 struct init_node
*elt
= constructor_pending_elts
;
8432 /* Look through the whole pending tree.
8433 If we find an element that should be output now,
8434 output it. Otherwise, set NEXT to the element
8435 that comes first among those still pending. */
8440 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8442 if (tree_int_cst_equal (elt
->purpose
,
8443 constructor_unfilled_index
))
8444 output_init_element (input_location
, elt
->value
, elt
->origtype
,
8445 true, TREE_TYPE (constructor_type
),
8446 constructor_unfilled_index
, 0, false,
8447 braced_init_obstack
);
8448 else if (tree_int_cst_lt (constructor_unfilled_index
,
8451 /* Advance to the next smaller node. */
8456 /* We have reached the smallest node bigger than the
8457 current unfilled index. Fill the space first. */
8458 next
= elt
->purpose
;
8464 /* Advance to the next bigger node. */
8469 /* We have reached the biggest node in a subtree. Find
8470 the parent of it, which is the next bigger node. */
8471 while (elt
->parent
&& elt
->parent
->right
== elt
)
8474 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
8477 next
= elt
->purpose
;
8483 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8484 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8486 tree ctor_unfilled_bitpos
, elt_bitpos
;
8488 /* If the current record is complete we are done. */
8489 if (constructor_unfilled_fields
== 0)
8492 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
8493 elt_bitpos
= bit_position (elt
->purpose
);
8494 /* We can't compare fields here because there might be empty
8495 fields in between. */
8496 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
8498 constructor_unfilled_fields
= elt
->purpose
;
8499 output_init_element (input_location
, elt
->value
, elt
->origtype
,
8500 true, TREE_TYPE (elt
->purpose
),
8501 elt
->purpose
, 0, false,
8502 braced_init_obstack
);
8504 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
8506 /* Advance to the next smaller node. */
8511 /* We have reached the smallest node bigger than the
8512 current unfilled field. Fill the space first. */
8513 next
= elt
->purpose
;
8519 /* Advance to the next bigger node. */
8524 /* We have reached the biggest node in a subtree. Find
8525 the parent of it, which is the next bigger node. */
8526 while (elt
->parent
&& elt
->parent
->right
== elt
)
8530 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
8531 bit_position (elt
->purpose
))))
8533 next
= elt
->purpose
;
8541 /* Ordinarily return, but not if we want to output all
8542 and there are elements left. */
8543 if (!(all
&& next
!= 0))
8546 /* If it's not incremental, just skip over the gap, so that after
8547 jumping to retry we will output the next successive element. */
8548 if (TREE_CODE (constructor_type
) == RECORD_TYPE
8549 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8550 constructor_unfilled_fields
= next
;
8551 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8552 constructor_unfilled_index
= next
;
8554 /* ELT now points to the node in the pending tree with the next
8555 initializer to output. */
8559 /* Add one non-braced element to the current constructor level.
8560 This adjusts the current position within the constructor's type.
8561 This may also start or terminate implicit levels
8562 to handle a partly-braced initializer.
8564 Once this has found the correct level for the new element,
8565 it calls output_init_element.
8567 IMPLICIT is true if value comes from pop_init_level (1),
8568 the new initializer has been merged with the existing one
8569 and thus no warnings should be emitted about overriding an
8570 existing initializer. */
8573 process_init_element (location_t loc
, struct c_expr value
, bool implicit
,
8574 struct obstack
* braced_init_obstack
)
8576 tree orig_value
= value
.value
;
8577 int string_flag
= orig_value
!= 0 && TREE_CODE (orig_value
) == STRING_CST
;
8578 bool strict_string
= value
.original_code
== STRING_CST
;
8579 bool was_designated
= designator_depth
!= 0;
8581 designator_depth
= 0;
8582 designator_erroneous
= 0;
8584 if (!implicit
&& value
.value
&& !integer_zerop (value
.value
))
8585 constructor_zeroinit
= 0;
8587 /* Handle superfluous braces around string cst as in
8588 char x[] = {"foo"}; */
8592 && TREE_CODE (constructor_type
) == ARRAY_TYPE
8593 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
8594 && integer_zerop (constructor_unfilled_index
))
8596 if (constructor_stack
->replacement_value
.value
)
8597 error_init (loc
, "excess elements in char array initializer");
8598 constructor_stack
->replacement_value
= value
;
8602 if (constructor_stack
->replacement_value
.value
!= 0)
8604 error_init (loc
, "excess elements in struct initializer");
8608 /* Ignore elements of a brace group if it is entirely superfluous
8609 and has already been diagnosed. */
8610 if (constructor_type
== 0)
8613 if (!implicit
&& warn_designated_init
&& !was_designated
8614 && TREE_CODE (constructor_type
) == RECORD_TYPE
8615 && lookup_attribute ("designated_init",
8616 TYPE_ATTRIBUTES (constructor_type
)))
8618 OPT_Wdesignated_init
,
8619 "positional initialization of field "
8620 "in %<struct%> declared with %<designated_init%> attribute");
8622 /* If we've exhausted any levels that didn't have braces,
8624 while (constructor_stack
->implicit
)
8626 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
8627 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8628 && constructor_fields
== 0)
8629 process_init_element (loc
,
8630 pop_init_level (loc
, 1, braced_init_obstack
),
8631 true, braced_init_obstack
);
8632 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
8633 || TREE_CODE (constructor_type
) == VECTOR_TYPE
)
8634 && constructor_max_index
8635 && tree_int_cst_lt (constructor_max_index
,
8637 process_init_element (loc
,
8638 pop_init_level (loc
, 1, braced_init_obstack
),
8639 true, braced_init_obstack
);
8644 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8645 if (constructor_range_stack
)
8647 /* If value is a compound literal and we'll be just using its
8648 content, don't put it into a SAVE_EXPR. */
8649 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
8650 || !require_constant_value
8653 tree semantic_type
= NULL_TREE
;
8654 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
8656 semantic_type
= TREE_TYPE (value
.value
);
8657 value
.value
= TREE_OPERAND (value
.value
, 0);
8659 value
.value
= c_save_expr (value
.value
);
8661 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
8668 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8671 enum tree_code fieldcode
;
8673 if (constructor_fields
== 0)
8675 pedwarn_init (loc
, 0, "excess elements in struct initializer");
8679 fieldtype
= TREE_TYPE (constructor_fields
);
8680 if (fieldtype
!= error_mark_node
)
8681 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
8682 fieldcode
= TREE_CODE (fieldtype
);
8684 /* Error for non-static initialization of a flexible array member. */
8685 if (fieldcode
== ARRAY_TYPE
8686 && !require_constant_value
8687 && TYPE_SIZE (fieldtype
) == NULL_TREE
8688 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
8690 error_init (loc
, "non-static initialization of a flexible "
8695 /* Accept a string constant to initialize a subarray. */
8696 if (value
.value
!= 0
8697 && fieldcode
== ARRAY_TYPE
8698 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
8700 value
.value
= orig_value
;
8701 /* Otherwise, if we have come to a subaggregate,
8702 and we don't have an element of its type, push into it. */
8703 else if (value
.value
!= 0
8704 && value
.value
!= error_mark_node
8705 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
8706 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
8707 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
8709 push_init_level (loc
, 1, braced_init_obstack
);
8715 push_member_name (constructor_fields
);
8716 output_init_element (loc
, value
.value
, value
.original_type
,
8717 strict_string
, fieldtype
,
8718 constructor_fields
, 1, implicit
,
8719 braced_init_obstack
);
8720 RESTORE_SPELLING_DEPTH (constructor_depth
);
8723 /* Do the bookkeeping for an element that was
8724 directly output as a constructor. */
8726 /* For a record, keep track of end position of last field. */
8727 if (DECL_SIZE (constructor_fields
))
8728 constructor_bit_index
8729 = size_binop_loc (input_location
, PLUS_EXPR
,
8730 bit_position (constructor_fields
),
8731 DECL_SIZE (constructor_fields
));
8733 /* If the current field was the first one not yet written out,
8734 it isn't now, so update. */
8735 if (constructor_unfilled_fields
== constructor_fields
)
8737 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
8738 /* Skip any nameless bit fields. */
8739 while (constructor_unfilled_fields
!= 0
8740 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8741 && DECL_NAME (constructor_unfilled_fields
) == 0)
8742 constructor_unfilled_fields
=
8743 DECL_CHAIN (constructor_unfilled_fields
);
8747 constructor_fields
= DECL_CHAIN (constructor_fields
);
8748 /* Skip any nameless bit fields at the beginning. */
8749 while (constructor_fields
!= 0
8750 && DECL_C_BIT_FIELD (constructor_fields
)
8751 && DECL_NAME (constructor_fields
) == 0)
8752 constructor_fields
= DECL_CHAIN (constructor_fields
);
8754 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8757 enum tree_code fieldcode
;
8759 if (constructor_fields
== 0)
8761 pedwarn_init (loc
, 0,
8762 "excess elements in union initializer");
8766 fieldtype
= TREE_TYPE (constructor_fields
);
8767 if (fieldtype
!= error_mark_node
)
8768 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
8769 fieldcode
= TREE_CODE (fieldtype
);
8771 /* Warn that traditional C rejects initialization of unions.
8772 We skip the warning if the value is zero. This is done
8773 under the assumption that the zero initializer in user
8774 code appears conditioned on e.g. __STDC__ to avoid
8775 "missing initializer" warnings and relies on default
8776 initialization to zero in the traditional C case.
8777 We also skip the warning if the initializer is designated,
8778 again on the assumption that this must be conditional on
8779 __STDC__ anyway (and we've already complained about the
8780 member-designator already). */
8781 if (!in_system_header_at (input_location
) && !constructor_designated
8782 && !(value
.value
&& (integer_zerop (value
.value
)
8783 || real_zerop (value
.value
))))
8784 warning (OPT_Wtraditional
, "traditional C rejects initialization "
8787 /* Accept a string constant to initialize a subarray. */
8788 if (value
.value
!= 0
8789 && fieldcode
== ARRAY_TYPE
8790 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
8792 value
.value
= orig_value
;
8793 /* Otherwise, if we have come to a subaggregate,
8794 and we don't have an element of its type, push into it. */
8795 else if (value
.value
!= 0
8796 && value
.value
!= error_mark_node
8797 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
8798 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
8799 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
8801 push_init_level (loc
, 1, braced_init_obstack
);
8807 push_member_name (constructor_fields
);
8808 output_init_element (loc
, value
.value
, value
.original_type
,
8809 strict_string
, fieldtype
,
8810 constructor_fields
, 1, implicit
,
8811 braced_init_obstack
);
8812 RESTORE_SPELLING_DEPTH (constructor_depth
);
8815 /* Do the bookkeeping for an element that was
8816 directly output as a constructor. */
8818 constructor_bit_index
= DECL_SIZE (constructor_fields
);
8819 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
8822 constructor_fields
= 0;
8824 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8826 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8827 enum tree_code eltcode
= TREE_CODE (elttype
);
8829 /* Accept a string constant to initialize a subarray. */
8830 if (value
.value
!= 0
8831 && eltcode
== ARRAY_TYPE
8832 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
8834 value
.value
= orig_value
;
8835 /* Otherwise, if we have come to a subaggregate,
8836 and we don't have an element of its type, push into it. */
8837 else if (value
.value
!= 0
8838 && value
.value
!= error_mark_node
8839 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
8840 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
8841 || eltcode
== UNION_TYPE
|| eltcode
== VECTOR_TYPE
))
8843 push_init_level (loc
, 1, braced_init_obstack
);
8847 if (constructor_max_index
!= 0
8848 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
8849 || integer_all_onesp (constructor_max_index
)))
8851 pedwarn_init (loc
, 0,
8852 "excess elements in array initializer");
8856 /* Now output the actual element. */
8859 push_array_bounds (tree_to_uhwi (constructor_index
));
8860 output_init_element (loc
, value
.value
, value
.original_type
,
8861 strict_string
, elttype
,
8862 constructor_index
, 1, implicit
,
8863 braced_init_obstack
);
8864 RESTORE_SPELLING_DEPTH (constructor_depth
);
8868 = size_binop_loc (input_location
, PLUS_EXPR
,
8869 constructor_index
, bitsize_one_node
);
8872 /* If we are doing the bookkeeping for an element that was
8873 directly output as a constructor, we must update
8874 constructor_unfilled_index. */
8875 constructor_unfilled_index
= constructor_index
;
8877 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
8879 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8881 /* Do a basic check of initializer size. Note that vectors
8882 always have a fixed size derived from their type. */
8883 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
8885 pedwarn_init (loc
, 0,
8886 "excess elements in vector initializer");
8890 /* Now output the actual element. */
8893 if (TREE_CODE (value
.value
) == VECTOR_CST
)
8894 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
8895 output_init_element (loc
, value
.value
, value
.original_type
,
8896 strict_string
, elttype
,
8897 constructor_index
, 1, implicit
,
8898 braced_init_obstack
);
8902 = size_binop_loc (input_location
,
8903 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
8906 /* If we are doing the bookkeeping for an element that was
8907 directly output as a constructor, we must update
8908 constructor_unfilled_index. */
8909 constructor_unfilled_index
= constructor_index
;
8912 /* Handle the sole element allowed in a braced initializer
8913 for a scalar variable. */
8914 else if (constructor_type
!= error_mark_node
8915 && constructor_fields
== 0)
8917 pedwarn_init (loc
, 0,
8918 "excess elements in scalar initializer");
8924 output_init_element (loc
, value
.value
, value
.original_type
,
8925 strict_string
, constructor_type
,
8926 NULL_TREE
, 1, implicit
,
8927 braced_init_obstack
);
8928 constructor_fields
= 0;
8931 /* Handle range initializers either at this level or anywhere higher
8932 in the designator stack. */
8933 if (constructor_range_stack
)
8935 struct constructor_range_stack
*p
, *range_stack
;
8938 range_stack
= constructor_range_stack
;
8939 constructor_range_stack
= 0;
8940 while (constructor_stack
!= range_stack
->stack
)
8942 gcc_assert (constructor_stack
->implicit
);
8943 process_init_element (loc
,
8944 pop_init_level (loc
, 1,
8945 braced_init_obstack
),
8946 true, braced_init_obstack
);
8948 for (p
= range_stack
;
8949 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
8952 gcc_assert (constructor_stack
->implicit
);
8953 process_init_element (loc
,
8954 pop_init_level (loc
, 1,
8955 braced_init_obstack
),
8956 true, braced_init_obstack
);
8959 p
->index
= size_binop_loc (input_location
,
8960 PLUS_EXPR
, p
->index
, bitsize_one_node
);
8961 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
8966 constructor_index
= p
->index
;
8967 constructor_fields
= p
->fields
;
8968 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
8976 push_init_level (loc
, 2, braced_init_obstack
);
8977 p
->stack
= constructor_stack
;
8978 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
8979 p
->index
= p
->range_start
;
8983 constructor_range_stack
= range_stack
;
8990 constructor_range_stack
= 0;
8993 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8994 (guaranteed to be 'volatile' or null) and ARGS (represented using
8995 an ASM_EXPR node). */
8997 build_asm_stmt (tree cv_qualifier
, tree args
)
8999 if (!ASM_VOLATILE_P (args
) && cv_qualifier
)
9000 ASM_VOLATILE_P (args
) = 1;
9001 return add_stmt (args
);
9004 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9005 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9006 SIMPLE indicates whether there was anything at all after the
9007 string in the asm expression -- asm("blah") and asm("blah" : )
9008 are subtly different. We use a ASM_EXPR node to represent this. */
9010 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
9011 tree clobbers
, tree labels
, bool simple
)
9016 const char *constraint
;
9017 const char **oconstraints
;
9018 bool allows_mem
, allows_reg
, is_inout
;
9019 int ninputs
, noutputs
;
9021 ninputs
= list_length (inputs
);
9022 noutputs
= list_length (outputs
);
9023 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
9025 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
9027 /* Remove output conversions that change the type but not the mode. */
9028 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
9030 tree output
= TREE_VALUE (tail
);
9032 output
= c_fully_fold (output
, false, NULL
);
9034 /* ??? Really, this should not be here. Users should be using a
9035 proper lvalue, dammit. But there's a long history of using casts
9036 in the output operands. In cases like longlong.h, this becomes a
9037 primitive form of typechecking -- if the cast can be removed, then
9038 the output operand had a type of the proper width; otherwise we'll
9039 get an error. Gross, but ... */
9040 STRIP_NOPS (output
);
9042 if (!lvalue_or_else (loc
, output
, lv_asm
))
9043 output
= error_mark_node
;
9045 if (output
!= error_mark_node
9046 && (TREE_READONLY (output
)
9047 || TYPE_READONLY (TREE_TYPE (output
))
9048 || ((TREE_CODE (TREE_TYPE (output
)) == RECORD_TYPE
9049 || TREE_CODE (TREE_TYPE (output
)) == UNION_TYPE
)
9050 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
9051 readonly_error (loc
, output
, lv_asm
);
9053 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9054 oconstraints
[i
] = constraint
;
9056 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
9057 &allows_mem
, &allows_reg
, &is_inout
))
9059 /* If the operand is going to end up in memory,
9060 mark it addressable. */
9061 if (!allows_reg
&& !c_mark_addressable (output
))
9062 output
= error_mark_node
;
9063 if (!(!allows_reg
&& allows_mem
)
9064 && output
!= error_mark_node
9065 && VOID_TYPE_P (TREE_TYPE (output
)))
9067 error_at (loc
, "invalid use of void expression");
9068 output
= error_mark_node
;
9072 output
= error_mark_node
;
9074 TREE_VALUE (tail
) = output
;
9077 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
9081 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9082 input
= TREE_VALUE (tail
);
9084 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
9085 oconstraints
, &allows_mem
, &allows_reg
))
9087 /* If the operand is going to end up in memory,
9088 mark it addressable. */
9089 if (!allows_reg
&& allows_mem
)
9091 input
= c_fully_fold (input
, false, NULL
);
9093 /* Strip the nops as we allow this case. FIXME, this really
9094 should be rejected or made deprecated. */
9096 if (!c_mark_addressable (input
))
9097 input
= error_mark_node
;
9102 memset (&expr
, 0, sizeof (expr
));
9104 expr
= convert_lvalue_to_rvalue (loc
, expr
, true, false);
9105 input
= c_fully_fold (expr
.value
, false, NULL
);
9107 if (input
!= error_mark_node
&& VOID_TYPE_P (TREE_TYPE (input
)))
9109 error_at (loc
, "invalid use of void expression");
9110 input
= error_mark_node
;
9115 input
= error_mark_node
;
9117 TREE_VALUE (tail
) = input
;
9120 /* ASMs with labels cannot have outputs. This should have been
9121 enforced by the parser. */
9122 gcc_assert (outputs
== NULL
|| labels
== NULL
);
9124 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
9126 /* asm statements without outputs, including simple ones, are treated
9128 ASM_INPUT_P (args
) = simple
;
9129 ASM_VOLATILE_P (args
) = (noutputs
== 0);
9134 /* Generate a goto statement to LABEL. LOC is the location of the
9138 c_finish_goto_label (location_t loc
, tree label
)
9140 tree decl
= lookup_label_for_goto (loc
, label
);
9143 TREE_USED (decl
) = 1;
9145 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
9146 SET_EXPR_LOCATION (t
, loc
);
9147 return add_stmt (t
);
9151 /* Generate a computed goto statement to EXPR. LOC is the location of
9155 c_finish_goto_ptr (location_t loc
, tree expr
)
9158 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids %<goto *expr;%>");
9159 expr
= c_fully_fold (expr
, false, NULL
);
9160 expr
= convert (ptr_type_node
, expr
);
9161 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
9162 SET_EXPR_LOCATION (t
, loc
);
9163 return add_stmt (t
);
9166 /* Generate a C `return' statement. RETVAL is the expression for what
9167 to return, or a null pointer for `return;' with no value. LOC is
9168 the location of the return statement, or the location of the expression,
9169 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9170 is the original type of RETVAL. */
9173 c_finish_return (location_t loc
, tree retval
, tree origtype
)
9175 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
9176 bool no_warning
= false;
9180 if (TREE_THIS_VOLATILE (current_function_decl
))
9182 "function declared %<noreturn%> has a %<return%> statement");
9184 if (flag_cilkplus
&& contains_array_notation_expr (retval
))
9186 /* Array notations are allowed in a return statement if it is inside a
9187 built-in array notation reduction function. */
9188 if (!find_rank (loc
, retval
, retval
, false, &rank
))
9189 return error_mark_node
;
9192 error_at (loc
, "array notation expression cannot be used as a "
9194 return error_mark_node
;
9197 if (flag_cilkplus
&& retval
&& contains_cilk_spawn_stmt (retval
))
9199 error_at (loc
, "use of %<_Cilk_spawn%> in a return statement is not "
9201 return error_mark_node
;
9205 tree semantic_type
= NULL_TREE
;
9206 npc
= null_pointer_constant_p (retval
);
9207 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
9209 semantic_type
= TREE_TYPE (retval
);
9210 retval
= TREE_OPERAND (retval
, 0);
9212 retval
= c_fully_fold (retval
, false, NULL
);
9214 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
9219 current_function_returns_null
= 1;
9220 if ((warn_return_type
|| flag_isoc99
)
9221 && valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
9223 pedwarn_c99 (loc
, flag_isoc99
? 0 : OPT_Wreturn_type
,
9224 "%<return%> with no value, in "
9225 "function returning non-void");
9229 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
9231 current_function_returns_null
= 1;
9232 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
9234 "%<return%> with a value, in function returning void");
9236 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
9237 "%<return%> with expression, in function returning void");
9241 tree t
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, valtype
,
9242 retval
, origtype
, ic_return
,
9243 npc
, NULL_TREE
, NULL_TREE
, 0);
9244 tree res
= DECL_RESULT (current_function_decl
);
9248 current_function_returns_value
= 1;
9249 if (t
== error_mark_node
)
9252 save
= in_late_binary_op
;
9253 if (TREE_CODE (TREE_TYPE (res
)) == BOOLEAN_TYPE
9254 || TREE_CODE (TREE_TYPE (res
)) == COMPLEX_TYPE
)
9255 in_late_binary_op
= true;
9256 inner
= t
= convert (TREE_TYPE (res
), t
);
9257 in_late_binary_op
= save
;
9259 /* Strip any conversions, additions, and subtractions, and see if
9260 we are returning the address of a local variable. Warn if so. */
9263 switch (TREE_CODE (inner
))
9266 case NON_LVALUE_EXPR
:
9268 case POINTER_PLUS_EXPR
:
9269 inner
= TREE_OPERAND (inner
, 0);
9273 /* If the second operand of the MINUS_EXPR has a pointer
9274 type (or is converted from it), this may be valid, so
9275 don't give a warning. */
9277 tree op1
= TREE_OPERAND (inner
, 1);
9279 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
9280 && (CONVERT_EXPR_P (op1
)
9281 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
9282 op1
= TREE_OPERAND (op1
, 0);
9284 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
9287 inner
= TREE_OPERAND (inner
, 0);
9292 inner
= TREE_OPERAND (inner
, 0);
9294 while (REFERENCE_CLASS_P (inner
)
9295 && TREE_CODE (inner
) != INDIRECT_REF
)
9296 inner
= TREE_OPERAND (inner
, 0);
9299 && !DECL_EXTERNAL (inner
)
9300 && !TREE_STATIC (inner
)
9301 && DECL_CONTEXT (inner
) == current_function_decl
)
9303 if (TREE_CODE (inner
) == LABEL_DECL
)
9304 warning_at (loc
, OPT_Wreturn_local_addr
,
9305 "function returns address of label");
9308 warning_at (loc
, OPT_Wreturn_local_addr
,
9309 "function returns address of local variable");
9310 tree zero
= build_zero_cst (TREE_TYPE (res
));
9311 t
= build2 (COMPOUND_EXPR
, TREE_TYPE (res
), t
, zero
);
9323 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
9324 SET_EXPR_LOCATION (retval
, loc
);
9326 if (warn_sequence_point
)
9327 verify_sequence_points (retval
);
9330 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
9331 TREE_NO_WARNING (ret_stmt
) |= no_warning
;
9332 return add_stmt (ret_stmt
);
9336 /* The SWITCH_EXPR being built. */
9339 /* The original type of the testing expression, i.e. before the
9340 default conversion is applied. */
9343 /* A splay-tree mapping the low element of a case range to the high
9344 element, or NULL_TREE if there is no high element. Used to
9345 determine whether or not a new case label duplicates an old case
9346 label. We need a tree, rather than simply a hash table, because
9347 of the GNU case range extension. */
9350 /* The bindings at the point of the switch. This is used for
9351 warnings crossing decls when branching to a case label. */
9352 struct c_spot_bindings
*bindings
;
9354 /* The next node on the stack. */
9355 struct c_switch
*next
;
9358 /* A stack of the currently active switch statements. The innermost
9359 switch statement is on the top of the stack. There is no need to
9360 mark the stack for garbage collection because it is only active
9361 during the processing of the body of a function, and we never
9362 collect at that point. */
9364 struct c_switch
*c_switch_stack
;
9366 /* Start a C switch statement, testing expression EXP. Return the new
9367 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9368 SWITCH_COND_LOC is the location of the switch's condition.
9369 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9372 c_start_case (location_t switch_loc
,
9373 location_t switch_cond_loc
,
9374 tree exp
, bool explicit_cast_p
)
9376 tree orig_type
= error_mark_node
;
9377 struct c_switch
*cs
;
9379 if (exp
!= error_mark_node
)
9381 orig_type
= TREE_TYPE (exp
);
9383 if (!INTEGRAL_TYPE_P (orig_type
))
9385 if (orig_type
!= error_mark_node
)
9387 error_at (switch_cond_loc
, "switch quantity not an integer");
9388 orig_type
= error_mark_node
;
9390 exp
= integer_zero_node
;
9394 tree type
= TYPE_MAIN_VARIANT (orig_type
);
9397 /* Warn if the condition has boolean value. */
9398 while (TREE_CODE (e
) == COMPOUND_EXPR
)
9399 e
= TREE_OPERAND (e
, 1);
9401 if ((TREE_CODE (type
) == BOOLEAN_TYPE
9402 || truth_value_p (TREE_CODE (e
)))
9403 /* Explicit cast to int suppresses this warning. */
9404 && !(TREE_CODE (type
) == INTEGER_TYPE
9405 && explicit_cast_p
))
9406 warning_at (switch_cond_loc
, OPT_Wswitch_bool
,
9407 "switch condition has boolean value");
9409 if (!in_system_header_at (input_location
)
9410 && (type
== long_integer_type_node
9411 || type
== long_unsigned_type_node
))
9412 warning_at (switch_cond_loc
,
9413 OPT_Wtraditional
, "%<long%> switch expression not "
9414 "converted to %<int%> in ISO C");
9416 exp
= c_fully_fold (exp
, false, NULL
);
9417 exp
= default_conversion (exp
);
9419 if (warn_sequence_point
)
9420 verify_sequence_points (exp
);
9424 /* Add this new SWITCH_EXPR to the stack. */
9425 cs
= XNEW (struct c_switch
);
9426 cs
->switch_expr
= build3 (SWITCH_EXPR
, orig_type
, exp
, NULL_TREE
, NULL_TREE
);
9427 SET_EXPR_LOCATION (cs
->switch_expr
, switch_loc
);
9428 cs
->orig_type
= orig_type
;
9429 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
9430 cs
->bindings
= c_get_switch_bindings ();
9431 cs
->next
= c_switch_stack
;
9432 c_switch_stack
= cs
;
9434 return add_stmt (cs
->switch_expr
);
9437 /* Process a case label at location LOC. */
9440 do_case (location_t loc
, tree low_value
, tree high_value
)
9442 tree label
= NULL_TREE
;
9444 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
9446 low_value
= c_fully_fold (low_value
, false, NULL
);
9447 if (TREE_CODE (low_value
) == INTEGER_CST
)
9448 pedwarn (loc
, OPT_Wpedantic
,
9449 "case label is not an integer constant expression");
9452 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
9454 high_value
= c_fully_fold (high_value
, false, NULL
);
9455 if (TREE_CODE (high_value
) == INTEGER_CST
)
9456 pedwarn (input_location
, OPT_Wpedantic
,
9457 "case label is not an integer constant expression");
9460 if (c_switch_stack
== NULL
)
9463 error_at (loc
, "case label not within a switch statement");
9465 error_at (loc
, "%<default%> label not within a switch statement");
9469 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
9470 EXPR_LOCATION (c_switch_stack
->switch_expr
),
9474 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
9475 SWITCH_COND (c_switch_stack
->switch_expr
),
9476 c_switch_stack
->orig_type
,
9477 low_value
, high_value
);
9478 if (label
== error_mark_node
)
9483 /* Finish the switch statement. */
9486 c_finish_case (tree body
)
9488 struct c_switch
*cs
= c_switch_stack
;
9489 location_t switch_location
;
9491 SWITCH_BODY (cs
->switch_expr
) = body
;
9493 /* Emit warnings as needed. */
9494 switch_location
= EXPR_LOCATION (cs
->switch_expr
);
9495 c_do_switch_warnings (cs
->cases
, switch_location
,
9496 TREE_TYPE (cs
->switch_expr
),
9497 SWITCH_COND (cs
->switch_expr
));
9499 /* Pop the stack. */
9500 c_switch_stack
= cs
->next
;
9501 splay_tree_delete (cs
->cases
);
9502 c_release_switch_bindings (cs
->bindings
);
9506 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9507 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9508 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9509 statement, and was not surrounded with parenthesis. */
9512 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
9513 tree else_block
, bool nested_if
)
9517 /* If the condition has array notations, then the rank of the then_block and
9518 else_block must be either 0 or be equal to the rank of the condition. If
9519 the condition does not have array notations then break them up as it is
9520 broken up in a normal expression. */
9521 if (flag_cilkplus
&& contains_array_notation_expr (cond
))
9523 size_t then_rank
= 0, cond_rank
= 0, else_rank
= 0;
9524 if (!find_rank (if_locus
, cond
, cond
, true, &cond_rank
))
9527 && !find_rank (if_locus
, then_block
, then_block
, true, &then_rank
))
9530 && !find_rank (if_locus
, else_block
, else_block
, true, &else_rank
))
9532 if (cond_rank
!= then_rank
&& then_rank
!= 0)
9534 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
9535 " and the then-block");
9538 else if (cond_rank
!= else_rank
&& else_rank
!= 0)
9540 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
9541 " and the else-block");
9545 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9546 if (warn_parentheses
&& nested_if
&& else_block
== NULL
)
9548 tree inner_if
= then_block
;
9550 /* We know from the grammar productions that there is an IF nested
9551 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9552 it might not be exactly THEN_BLOCK, but should be the last
9553 non-container statement within. */
9555 switch (TREE_CODE (inner_if
))
9560 inner_if
= BIND_EXPR_BODY (inner_if
);
9562 case STATEMENT_LIST
:
9563 inner_if
= expr_last (then_block
);
9565 case TRY_FINALLY_EXPR
:
9566 case TRY_CATCH_EXPR
:
9567 inner_if
= TREE_OPERAND (inner_if
, 0);
9574 if (COND_EXPR_ELSE (inner_if
))
9575 warning_at (if_locus
, OPT_Wparentheses
,
9576 "suggest explicit braces to avoid ambiguous %<else%>");
9579 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
9580 SET_EXPR_LOCATION (stmt
, if_locus
);
9584 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9585 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9586 is false for DO loops. INCR is the FOR increment expression. BODY is
9587 the statement controlled by the loop. BLAB is the break label. CLAB is
9588 the continue label. Everything is allowed to be NULL. */
9591 c_finish_loop (location_t start_locus
, tree cond
, tree incr
, tree body
,
9592 tree blab
, tree clab
, bool cond_is_first
)
9594 tree entry
= NULL
, exit
= NULL
, t
;
9596 if (flag_cilkplus
&& contains_array_notation_expr (cond
))
9598 error_at (start_locus
, "array notation expression cannot be used in a "
9599 "loop%'s condition");
9603 /* If the condition is zero don't generate a loop construct. */
9604 if (cond
&& integer_zerop (cond
))
9608 t
= build_and_jump (&blab
);
9609 SET_EXPR_LOCATION (t
, start_locus
);
9615 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
9617 /* If we have an exit condition, then we build an IF with gotos either
9618 out of the loop, or to the top of it. If there's no exit condition,
9619 then we just build a jump back to the top. */
9620 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
9622 if (cond
&& !integer_nonzerop (cond
))
9624 /* Canonicalize the loop condition to the end. This means
9625 generating a branch to the loop condition. Reuse the
9626 continue label, if possible. */
9631 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
9632 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
9635 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
9636 SET_EXPR_LOCATION (t
, start_locus
);
9640 t
= build_and_jump (&blab
);
9642 exit
= fold_build3_loc (start_locus
,
9643 COND_EXPR
, void_type_node
, cond
, exit
, t
);
9645 exit
= fold_build3_loc (input_location
,
9646 COND_EXPR
, void_type_node
, cond
, exit
, t
);
9655 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
9663 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
9667 c_finish_bc_stmt (location_t loc
, tree
*label_p
, bool is_break
)
9670 tree label
= *label_p
;
9672 /* In switch statements break is sometimes stylistically used after
9673 a return statement. This can lead to spurious warnings about
9674 control reaching the end of a non-void function when it is
9675 inlined. Note that we are calling block_may_fallthru with
9676 language specific tree nodes; this works because
9677 block_may_fallthru returns true when given something it does not
9679 skip
= !block_may_fallthru (cur_stmt_list
);
9684 *label_p
= label
= create_artificial_label (loc
);
9686 else if (TREE_CODE (label
) == LABEL_DECL
)
9688 else switch (TREE_INT_CST_LOW (label
))
9692 error_at (loc
, "break statement not within loop or switch");
9694 error_at (loc
, "continue statement not within a loop");
9698 gcc_assert (is_break
);
9699 error_at (loc
, "break statement used with OpenMP for loop");
9704 error ("break statement within %<#pragma simd%> loop body");
9706 error ("continue statement within %<#pragma simd%> loop body");
9717 add_stmt (build_predict_expr (PRED_CONTINUE
, NOT_TAKEN
));
9719 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
9722 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9725 emit_side_effect_warnings (location_t loc
, tree expr
)
9727 if (expr
== error_mark_node
)
9729 else if (!TREE_SIDE_EFFECTS (expr
))
9731 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
9732 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
9734 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
9737 location_t cloc
= loc
;
9738 while (TREE_CODE (r
) == COMPOUND_EXPR
)
9740 if (EXPR_HAS_LOCATION (r
))
9741 cloc
= EXPR_LOCATION (r
);
9742 r
= TREE_OPERAND (r
, 1);
9744 if (!TREE_SIDE_EFFECTS (r
)
9745 && !VOID_TYPE_P (TREE_TYPE (r
))
9746 && !CONVERT_EXPR_P (r
)
9747 && !TREE_NO_WARNING (r
)
9748 && !TREE_NO_WARNING (expr
))
9749 warning_at (cloc
, OPT_Wunused_value
,
9750 "right-hand operand of comma expression has no effect");
9753 warn_if_unused_value (expr
, loc
);
9756 /* Process an expression as if it were a complete statement. Emit
9757 diagnostics, but do not call ADD_STMT. LOC is the location of the
9761 c_process_expr_stmt (location_t loc
, tree expr
)
9768 expr
= c_fully_fold (expr
, false, NULL
);
9770 if (warn_sequence_point
)
9771 verify_sequence_points (expr
);
9773 if (TREE_TYPE (expr
) != error_mark_node
9774 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
9775 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
9776 error_at (loc
, "expression statement has incomplete type");
9778 /* If we're not processing a statement expression, warn about unused values.
9779 Warnings for statement expressions will be emitted later, once we figure
9780 out which is the result. */
9781 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
9782 && warn_unused_value
)
9783 emit_side_effect_warnings (loc
, expr
);
9786 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
9787 exprv
= TREE_OPERAND (exprv
, 1);
9788 while (CONVERT_EXPR_P (exprv
))
9789 exprv
= TREE_OPERAND (exprv
, 0);
9791 || handled_component_p (exprv
)
9792 || TREE_CODE (exprv
) == ADDR_EXPR
)
9793 mark_exp_read (exprv
);
9795 /* If the expression is not of a type to which we cannot assign a line
9796 number, wrap the thing in a no-op NOP_EXPR. */
9797 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
9799 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
9800 SET_EXPR_LOCATION (expr
, loc
);
9806 /* Emit an expression as a statement. LOC is the location of the
9810 c_finish_expr_stmt (location_t loc
, tree expr
)
9813 return add_stmt (c_process_expr_stmt (loc
, expr
));
9818 /* Do the opposite and emit a statement as an expression. To begin,
9819 create a new binding level and return it. */
9822 c_begin_stmt_expr (void)
9826 /* We must force a BLOCK for this level so that, if it is not expanded
9827 later, there is a way to turn off the entire subtree of blocks that
9828 are contained in it. */
9830 ret
= c_begin_compound_stmt (true);
9832 c_bindings_start_stmt_expr (c_switch_stack
== NULL
9834 : c_switch_stack
->bindings
);
9836 /* Mark the current statement list as belonging to a statement list. */
9837 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
9842 /* LOC is the location of the compound statement to which this body
9846 c_finish_stmt_expr (location_t loc
, tree body
)
9848 tree last
, type
, tmp
, val
;
9851 body
= c_end_compound_stmt (loc
, body
, true);
9853 c_bindings_end_stmt_expr (c_switch_stack
== NULL
9855 : c_switch_stack
->bindings
);
9857 /* Locate the last statement in BODY. See c_end_compound_stmt
9858 about always returning a BIND_EXPR. */
9859 last_p
= &BIND_EXPR_BODY (body
);
9860 last
= BIND_EXPR_BODY (body
);
9863 if (TREE_CODE (last
) == STATEMENT_LIST
)
9865 tree_stmt_iterator i
;
9867 /* This can happen with degenerate cases like ({ }). No value. */
9868 if (!TREE_SIDE_EFFECTS (last
))
9871 /* If we're supposed to generate side effects warnings, process
9872 all of the statements except the last. */
9873 if (warn_unused_value
)
9875 for (i
= tsi_start (last
); !tsi_one_before_end_p (i
); tsi_next (&i
))
9878 tree t
= tsi_stmt (i
);
9880 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
9881 emit_side_effect_warnings (tloc
, t
);
9885 i
= tsi_last (last
);
9886 last_p
= tsi_stmt_ptr (i
);
9890 /* If the end of the list is exception related, then the list was split
9891 by a call to push_cleanup. Continue searching. */
9892 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
9893 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
9895 last_p
= &TREE_OPERAND (last
, 0);
9897 goto continue_searching
;
9900 if (last
== error_mark_node
)
9903 /* In the case that the BIND_EXPR is not necessary, return the
9904 expression out from inside it. */
9905 if (last
== BIND_EXPR_BODY (body
)
9906 && BIND_EXPR_VARS (body
) == NULL
)
9908 /* Even if this looks constant, do not allow it in a constant
9910 last
= c_wrap_maybe_const (last
, true);
9911 /* Do not warn if the return value of a statement expression is
9913 TREE_NO_WARNING (last
) = 1;
9917 /* Extract the type of said expression. */
9918 type
= TREE_TYPE (last
);
9920 /* If we're not returning a value at all, then the BIND_EXPR that
9921 we already have is a fine expression to return. */
9922 if (!type
|| VOID_TYPE_P (type
))
9925 /* Now that we've located the expression containing the value, it seems
9926 silly to make voidify_wrapper_expr repeat the process. Create a
9927 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9928 tmp
= create_tmp_var_raw (type
, NULL
);
9930 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9931 tree_expr_nonnegative_p giving up immediately. */
9933 if (TREE_CODE (val
) == NOP_EXPR
9934 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
9935 val
= TREE_OPERAND (val
, 0);
9937 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
9938 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
9941 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
9942 SET_EXPR_LOCATION (t
, loc
);
9947 /* Begin and end compound statements. This is as simple as pushing
9948 and popping new statement lists from the tree. */
9951 c_begin_compound_stmt (bool do_scope
)
9953 tree stmt
= push_stmt_list ();
9959 /* End a compound statement. STMT is the statement. LOC is the
9960 location of the compound statement-- this is usually the location
9961 of the opening brace. */
9964 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
9970 if (c_dialect_objc ())
9971 objc_clear_super_receiver ();
9972 block
= pop_scope ();
9975 stmt
= pop_stmt_list (stmt
);
9976 stmt
= c_build_bind_expr (loc
, block
, stmt
);
9978 /* If this compound statement is nested immediately inside a statement
9979 expression, then force a BIND_EXPR to be created. Otherwise we'll
9980 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9981 STATEMENT_LISTs merge, and thus we can lose track of what statement
9983 if (building_stmt_list_p ()
9984 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
9985 && TREE_CODE (stmt
) != BIND_EXPR
)
9987 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
9988 TREE_SIDE_EFFECTS (stmt
) = 1;
9989 SET_EXPR_LOCATION (stmt
, loc
);
9995 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9996 when the current scope is exited. EH_ONLY is true when this is not
9997 meant to apply to normal control flow transfer. */
10000 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
10002 enum tree_code code
;
10006 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
10007 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
10009 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
10010 list
= push_stmt_list ();
10011 TREE_OPERAND (stmt
, 0) = list
;
10012 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
10015 /* Build a binary-operation expression without default conversions.
10016 CODE is the kind of expression to build.
10017 LOCATION is the operator's location.
10018 This function differs from `build' in several ways:
10019 the data type of the result is computed and recorded in it,
10020 warnings are generated if arg data types are invalid,
10021 special handling for addition and subtraction of pointers is known,
10022 and some optimization is done (operations on narrow ints
10023 are done in the narrower type when that gives the same result).
10024 Constant folding is also done before the result is returned.
10026 Note that the operands will never have enumeral types, or function
10027 or array types, because either they will have the default conversions
10028 performed or they have both just been converted to some other type in which
10029 the arithmetic is to be done. */
10032 build_binary_op (location_t location
, enum tree_code code
,
10033 tree orig_op0
, tree orig_op1
, int convert_p
)
10035 tree type0
, type1
, orig_type0
, orig_type1
;
10037 enum tree_code code0
, code1
;
10039 tree ret
= error_mark_node
;
10040 const char *invalid_op_diag
;
10041 bool op0_int_operands
, op1_int_operands
;
10042 bool int_const
, int_const_or_overflow
, int_operands
;
10044 /* Expression code to give to the expression when it is built.
10045 Normally this is CODE, which is what the caller asked for,
10046 but in some special cases we change it. */
10047 enum tree_code resultcode
= code
;
10049 /* Data type in which the computation is to be performed.
10050 In the simplest cases this is the common type of the arguments. */
10051 tree result_type
= NULL
;
10053 /* When the computation is in excess precision, the type of the
10054 final EXCESS_PRECISION_EXPR. */
10055 tree semantic_result_type
= NULL
;
10057 /* Nonzero means operands have already been type-converted
10058 in whatever way is necessary.
10059 Zero means they need to be converted to RESULT_TYPE. */
10062 /* Nonzero means create the expression with this type, rather than
10064 tree build_type
= 0;
10066 /* Nonzero means after finally constructing the expression
10067 convert it to this type. */
10068 tree final_type
= 0;
10070 /* Nonzero if this is an operation like MIN or MAX which can
10071 safely be computed in short if both args are promoted shorts.
10072 Also implies COMMON.
10073 -1 indicates a bitwise operation; this makes a difference
10074 in the exact conditions for when it is safe to do the operation
10075 in a narrower mode. */
10078 /* Nonzero if this is a comparison operation;
10079 if both args are promoted shorts, compare the original shorts.
10080 Also implies COMMON. */
10081 int short_compare
= 0;
10083 /* Nonzero if this is a right-shift operation, which can be computed on the
10084 original short and then promoted if the operand is a promoted short. */
10085 int short_shift
= 0;
10087 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10090 /* True means types are compatible as far as ObjC is concerned. */
10093 /* True means this is an arithmetic operation that may need excess
10095 bool may_need_excess_precision
;
10097 /* True means this is a boolean operation that converts both its
10098 operands to truth-values. */
10099 bool boolean_op
= false;
10101 /* Remember whether we're doing / or %. */
10102 bool doing_div_or_mod
= false;
10104 /* Remember whether we're doing << or >>. */
10105 bool doing_shift
= false;
10107 /* Tree holding instrumentation expression. */
10108 tree instrument_expr
= NULL
;
10110 if (location
== UNKNOWN_LOCATION
)
10111 location
= input_location
;
10116 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
10117 if (op0_int_operands
)
10118 op0
= remove_c_maybe_const_expr (op0
);
10119 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
10120 if (op1_int_operands
)
10121 op1
= remove_c_maybe_const_expr (op1
);
10122 int_operands
= (op0_int_operands
&& op1_int_operands
);
10125 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
10126 && TREE_CODE (orig_op1
) == INTEGER_CST
);
10127 int_const
= (int_const_or_overflow
10128 && !TREE_OVERFLOW (orig_op0
)
10129 && !TREE_OVERFLOW (orig_op1
));
10132 int_const
= int_const_or_overflow
= false;
10134 /* Do not apply default conversion in mixed vector/scalar expression. */
10136 && !((TREE_CODE (TREE_TYPE (op0
)) == VECTOR_TYPE
)
10137 != (TREE_CODE (TREE_TYPE (op1
)) == VECTOR_TYPE
)))
10139 op0
= default_conversion (op0
);
10140 op1
= default_conversion (op1
);
10143 /* When Cilk Plus is enabled and there are array notations inside op0, then
10144 we check to see if there are builtin array notation functions. If
10145 so, then we take on the type of the array notation inside it. */
10146 if (flag_cilkplus
&& contains_array_notation_expr (op0
))
10147 orig_type0
= type0
= find_correct_array_notation_type (op0
);
10149 orig_type0
= type0
= TREE_TYPE (op0
);
10151 if (flag_cilkplus
&& contains_array_notation_expr (op1
))
10152 orig_type1
= type1
= find_correct_array_notation_type (op1
);
10154 orig_type1
= type1
= TREE_TYPE (op1
);
10156 /* The expression codes of the data types of the arguments tell us
10157 whether the arguments are integers, floating, pointers, etc. */
10158 code0
= TREE_CODE (type0
);
10159 code1
= TREE_CODE (type1
);
10161 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10162 STRIP_TYPE_NOPS (op0
);
10163 STRIP_TYPE_NOPS (op1
);
10165 /* If an error was already reported for one of the arguments,
10166 avoid reporting another error. */
10168 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10169 return error_mark_node
;
10171 if ((invalid_op_diag
10172 = targetm
.invalid_binary_op (code
, type0
, type1
)))
10174 error_at (location
, invalid_op_diag
);
10175 return error_mark_node
;
10183 case TRUNC_DIV_EXPR
:
10184 case CEIL_DIV_EXPR
:
10185 case FLOOR_DIV_EXPR
:
10186 case ROUND_DIV_EXPR
:
10187 case EXACT_DIV_EXPR
:
10188 may_need_excess_precision
= true;
10191 may_need_excess_precision
= false;
10194 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
10196 op0
= TREE_OPERAND (op0
, 0);
10197 type0
= TREE_TYPE (op0
);
10199 else if (may_need_excess_precision
10200 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
10203 op0
= convert (eptype
, op0
);
10205 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
10207 op1
= TREE_OPERAND (op1
, 0);
10208 type1
= TREE_TYPE (op1
);
10210 else if (may_need_excess_precision
10211 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
10214 op1
= convert (eptype
, op1
);
10217 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
10219 /* In case when one of the operands of the binary operation is
10220 a vector and another is a scalar -- convert scalar to vector. */
10221 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
10223 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
10226 switch (convert_flag
)
10229 return error_mark_node
;
10232 bool maybe_const
= true;
10234 sc
= c_fully_fold (op0
, false, &maybe_const
);
10235 sc
= save_expr (sc
);
10236 sc
= convert (TREE_TYPE (type1
), sc
);
10237 op0
= build_vector_from_val (type1
, sc
);
10239 op0
= c_wrap_maybe_const (op0
, true);
10240 orig_type0
= type0
= TREE_TYPE (op0
);
10241 code0
= TREE_CODE (type0
);
10245 case stv_secondarg
:
10247 bool maybe_const
= true;
10249 sc
= c_fully_fold (op1
, false, &maybe_const
);
10250 sc
= save_expr (sc
);
10251 sc
= convert (TREE_TYPE (type0
), sc
);
10252 op1
= build_vector_from_val (type0
, sc
);
10254 op1
= c_wrap_maybe_const (op1
, true);
10255 orig_type1
= type1
= TREE_TYPE (op1
);
10256 code1
= TREE_CODE (type1
);
10268 /* Handle the pointer + int case. */
10269 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10271 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
10272 goto return_build_binary_op
;
10274 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
10276 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
10277 goto return_build_binary_op
;
10284 /* Subtraction of two similar pointers.
10285 We must subtract them as integers, then divide by object size. */
10286 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
10287 && comp_target_types (location
, type0
, type1
))
10289 ret
= pointer_diff (location
, op0
, op1
);
10290 goto return_build_binary_op
;
10292 /* Handle pointer minus int. Just like pointer plus int. */
10293 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10295 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
10296 goto return_build_binary_op
;
10306 case TRUNC_DIV_EXPR
:
10307 case CEIL_DIV_EXPR
:
10308 case FLOOR_DIV_EXPR
:
10309 case ROUND_DIV_EXPR
:
10310 case EXACT_DIV_EXPR
:
10311 doing_div_or_mod
= true;
10312 warn_for_div_by_zero (location
, op1
);
10314 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10315 || code0
== FIXED_POINT_TYPE
10316 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10317 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10318 || code1
== FIXED_POINT_TYPE
10319 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
10321 enum tree_code tcode0
= code0
, tcode1
= code1
;
10323 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10324 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
10325 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
10326 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
10328 if (!((tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
)
10329 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
10330 resultcode
= RDIV_EXPR
;
10332 /* Although it would be tempting to shorten always here, that
10333 loses on some targets, since the modulo instruction is
10334 undefined if the quotient can't be represented in the
10335 computation mode. We shorten only if unsigned or if
10336 dividing by something we know != -1. */
10337 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10338 || (TREE_CODE (op1
) == INTEGER_CST
10339 && !integer_all_onesp (op1
)));
10347 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10349 /* Allow vector types which are not floating point types. */
10350 else if (code0
== VECTOR_TYPE
10351 && code1
== VECTOR_TYPE
10352 && !VECTOR_FLOAT_TYPE_P (type0
)
10353 && !VECTOR_FLOAT_TYPE_P (type1
))
10357 case TRUNC_MOD_EXPR
:
10358 case FLOOR_MOD_EXPR
:
10359 doing_div_or_mod
= true;
10360 warn_for_div_by_zero (location
, op1
);
10362 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10363 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10364 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
10366 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10368 /* Although it would be tempting to shorten always here, that loses
10369 on some targets, since the modulo instruction is undefined if the
10370 quotient can't be represented in the computation mode. We shorten
10371 only if unsigned or if dividing by something we know != -1. */
10372 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10373 || (TREE_CODE (op1
) == INTEGER_CST
10374 && !integer_all_onesp (op1
)));
10379 case TRUTH_ANDIF_EXPR
:
10380 case TRUTH_ORIF_EXPR
:
10381 case TRUTH_AND_EXPR
:
10382 case TRUTH_OR_EXPR
:
10383 case TRUTH_XOR_EXPR
:
10384 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
10385 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
10386 || code0
== FIXED_POINT_TYPE
)
10387 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
10388 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
10389 || code1
== FIXED_POINT_TYPE
))
10391 /* Result of these operations is always an int,
10392 but that does not mean the operands should be
10393 converted to ints! */
10394 result_type
= integer_type_node
;
10395 if (op0_int_operands
)
10397 op0
= c_objc_common_truthvalue_conversion (location
, orig_op0
);
10398 op0
= remove_c_maybe_const_expr (op0
);
10401 op0
= c_objc_common_truthvalue_conversion (location
, op0
);
10402 if (op1_int_operands
)
10404 op1
= c_objc_common_truthvalue_conversion (location
, orig_op1
);
10405 op1
= remove_c_maybe_const_expr (op1
);
10408 op1
= c_objc_common_truthvalue_conversion (location
, op1
);
10412 if (code
== TRUTH_ANDIF_EXPR
)
10414 int_const_or_overflow
= (int_operands
10415 && TREE_CODE (orig_op0
) == INTEGER_CST
10416 && (op0
== truthvalue_false_node
10417 || TREE_CODE (orig_op1
) == INTEGER_CST
));
10418 int_const
= (int_const_or_overflow
10419 && !TREE_OVERFLOW (orig_op0
)
10420 && (op0
== truthvalue_false_node
10421 || !TREE_OVERFLOW (orig_op1
)));
10423 else if (code
== TRUTH_ORIF_EXPR
)
10425 int_const_or_overflow
= (int_operands
10426 && TREE_CODE (orig_op0
) == INTEGER_CST
10427 && (op0
== truthvalue_true_node
10428 || TREE_CODE (orig_op1
) == INTEGER_CST
));
10429 int_const
= (int_const_or_overflow
10430 && !TREE_OVERFLOW (orig_op0
)
10431 && (op0
== truthvalue_true_node
10432 || !TREE_OVERFLOW (orig_op1
)));
10436 /* Shift operations: result has same type as first operand;
10437 always convert second operand to int.
10438 Also set SHORT_SHIFT if shifting rightward. */
10441 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
10442 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
10444 result_type
= type0
;
10447 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10448 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10449 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
10450 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
10452 result_type
= type0
;
10455 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
10456 && code1
== INTEGER_TYPE
)
10458 doing_shift
= true;
10459 if (TREE_CODE (op1
) == INTEGER_CST
)
10461 if (tree_int_cst_sgn (op1
) < 0)
10464 if (c_inhibit_evaluation_warnings
== 0)
10465 warning_at (location
, 0, "right shift count is negative");
10469 if (!integer_zerop (op1
))
10472 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
10475 if (c_inhibit_evaluation_warnings
== 0)
10476 warning_at (location
, 0, "right shift count >= width "
10482 /* Use the type of the value to be shifted. */
10483 result_type
= type0
;
10484 /* Convert the non vector shift-count to an integer, regardless
10485 of size of value being shifted. */
10486 if (TREE_CODE (TREE_TYPE (op1
)) != VECTOR_TYPE
10487 && TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
10488 op1
= convert (integer_type_node
, op1
);
10489 /* Avoid converting op1 to result_type later. */
10495 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
10496 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
10498 result_type
= type0
;
10501 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10502 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10503 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
10504 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
10506 result_type
= type0
;
10509 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
10510 && code1
== INTEGER_TYPE
)
10512 doing_shift
= true;
10513 if (TREE_CODE (op1
) == INTEGER_CST
)
10515 if (tree_int_cst_sgn (op1
) < 0)
10518 if (c_inhibit_evaluation_warnings
== 0)
10519 warning_at (location
, 0, "left shift count is negative");
10522 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
10525 if (c_inhibit_evaluation_warnings
== 0)
10526 warning_at (location
, 0, "left shift count >= width of "
10531 /* Use the type of the value to be shifted. */
10532 result_type
= type0
;
10533 /* Convert the non vector shift-count to an integer, regardless
10534 of size of value being shifted. */
10535 if (TREE_CODE (TREE_TYPE (op1
)) != VECTOR_TYPE
10536 && TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
10537 op1
= convert (integer_type_node
, op1
);
10538 /* Avoid converting op1 to result_type later. */
10545 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
10548 if (!vector_types_compatible_elements_p (type0
, type1
))
10550 error_at (location
, "comparing vectors with different "
10552 return error_mark_node
;
10555 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
10557 error_at (location
, "comparing vectors with different "
10558 "number of elements");
10559 return error_mark_node
;
10562 /* Always construct signed integer vector type. */
10563 intt
= c_common_type_for_size (GET_MODE_BITSIZE
10564 (TYPE_MODE (TREE_TYPE (type0
))), 0);
10565 result_type
= build_opaque_vector_type (intt
,
10566 TYPE_VECTOR_SUBPARTS (type0
));
10570 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
10571 warning_at (location
,
10573 "comparing floating point with == or != is unsafe");
10574 /* Result of comparison is always int,
10575 but don't convert the args to int! */
10576 build_type
= integer_type_node
;
10577 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10578 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
10579 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10580 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
10582 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
10584 if (TREE_CODE (op0
) == ADDR_EXPR
10585 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0)))
10587 if (code
== EQ_EXPR
)
10588 warning_at (location
,
10590 "the comparison will always evaluate as %<false%> "
10591 "for the address of %qD will never be NULL",
10592 TREE_OPERAND (op0
, 0));
10594 warning_at (location
,
10596 "the comparison will always evaluate as %<true%> "
10597 "for the address of %qD will never be NULL",
10598 TREE_OPERAND (op0
, 0));
10600 result_type
= type0
;
10602 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
10604 if (TREE_CODE (op1
) == ADDR_EXPR
10605 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0)))
10607 if (code
== EQ_EXPR
)
10608 warning_at (location
,
10610 "the comparison will always evaluate as %<false%> "
10611 "for the address of %qD will never be NULL",
10612 TREE_OPERAND (op1
, 0));
10614 warning_at (location
,
10616 "the comparison will always evaluate as %<true%> "
10617 "for the address of %qD will never be NULL",
10618 TREE_OPERAND (op1
, 0));
10620 result_type
= type1
;
10622 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
10624 tree tt0
= TREE_TYPE (type0
);
10625 tree tt1
= TREE_TYPE (type1
);
10626 addr_space_t as0
= TYPE_ADDR_SPACE (tt0
);
10627 addr_space_t as1
= TYPE_ADDR_SPACE (tt1
);
10628 addr_space_t as_common
= ADDR_SPACE_GENERIC
;
10630 /* Anything compares with void *. void * compares with anything.
10631 Otherwise, the targets must be compatible
10632 and both must be object or both incomplete. */
10633 if (comp_target_types (location
, type0
, type1
))
10634 result_type
= common_pointer_type (type0
, type1
);
10635 else if (!addr_space_superset (as0
, as1
, &as_common
))
10637 error_at (location
, "comparison of pointers to "
10638 "disjoint address spaces");
10639 return error_mark_node
;
10641 else if (VOID_TYPE_P (tt0
) && !TYPE_ATOMIC (tt0
))
10643 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
)
10644 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10645 "comparison of %<void *%> with function pointer");
10647 else if (VOID_TYPE_P (tt1
) && !TYPE_ATOMIC (tt1
))
10649 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
)
10650 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10651 "comparison of %<void *%> with function pointer");
10654 /* Avoid warning about the volatile ObjC EH puts on decls. */
10656 pedwarn (location
, 0,
10657 "comparison of distinct pointer types lacks a cast");
10659 if (result_type
== NULL_TREE
)
10661 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
10662 result_type
= build_pointer_type
10663 (build_qualified_type (void_type_node
, qual
));
10666 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10668 result_type
= type0
;
10669 pedwarn (location
, 0, "comparison between pointer and integer");
10671 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
10673 result_type
= type1
;
10674 pedwarn (location
, 0, "comparison between pointer and integer");
10682 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
10685 if (!vector_types_compatible_elements_p (type0
, type1
))
10687 error_at (location
, "comparing vectors with different "
10689 return error_mark_node
;
10692 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
10694 error_at (location
, "comparing vectors with different "
10695 "number of elements");
10696 return error_mark_node
;
10699 /* Always construct signed integer vector type. */
10700 intt
= c_common_type_for_size (GET_MODE_BITSIZE
10701 (TYPE_MODE (TREE_TYPE (type0
))), 0);
10702 result_type
= build_opaque_vector_type (intt
,
10703 TYPE_VECTOR_SUBPARTS (type0
));
10707 build_type
= integer_type_node
;
10708 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10709 || code0
== FIXED_POINT_TYPE
)
10710 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10711 || code1
== FIXED_POINT_TYPE
))
10713 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
10715 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (type0
));
10716 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
10717 addr_space_t as_common
;
10719 if (comp_target_types (location
, type0
, type1
))
10721 result_type
= common_pointer_type (type0
, type1
);
10722 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
10723 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
10724 pedwarn (location
, 0,
10725 "comparison of complete and incomplete pointers");
10726 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
10727 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10728 "ordered comparisons of pointers to functions");
10729 else if (null_pointer_constant_p (orig_op0
)
10730 || null_pointer_constant_p (orig_op1
))
10731 warning_at (location
, OPT_Wextra
,
10732 "ordered comparison of pointer with null pointer");
10735 else if (!addr_space_superset (as0
, as1
, &as_common
))
10737 error_at (location
, "comparison of pointers to "
10738 "disjoint address spaces");
10739 return error_mark_node
;
10743 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
10744 result_type
= build_pointer_type
10745 (build_qualified_type (void_type_node
, qual
));
10746 pedwarn (location
, 0,
10747 "comparison of distinct pointer types lacks a cast");
10750 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
10752 result_type
= type0
;
10754 pedwarn (location
, OPT_Wpedantic
,
10755 "ordered comparison of pointer with integer zero");
10756 else if (extra_warnings
)
10757 warning_at (location
, OPT_Wextra
,
10758 "ordered comparison of pointer with integer zero");
10760 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
10762 result_type
= type1
;
10764 pedwarn (location
, OPT_Wpedantic
,
10765 "ordered comparison of pointer with integer zero");
10766 else if (extra_warnings
)
10767 warning_at (location
, OPT_Wextra
,
10768 "ordered comparison of pointer with integer zero");
10770 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10772 result_type
= type0
;
10773 pedwarn (location
, 0, "comparison between pointer and integer");
10775 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
10777 result_type
= type1
;
10778 pedwarn (location
, 0, "comparison between pointer and integer");
10783 gcc_unreachable ();
10786 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10787 return error_mark_node
;
10789 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10790 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
10791 || !vector_types_compatible_elements_p (type0
, type1
)))
10793 binary_op_error (location
, code
, type0
, type1
);
10794 return error_mark_node
;
10797 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
10798 || code0
== FIXED_POINT_TYPE
|| code0
== VECTOR_TYPE
)
10800 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
10801 || code1
== FIXED_POINT_TYPE
|| code1
== VECTOR_TYPE
))
10803 bool first_complex
= (code0
== COMPLEX_TYPE
);
10804 bool second_complex
= (code1
== COMPLEX_TYPE
);
10805 int none_complex
= (!first_complex
&& !second_complex
);
10807 if (shorten
|| common
|| short_compare
)
10809 result_type
= c_common_type (type0
, type1
);
10810 do_warn_double_promotion (result_type
, type0
, type1
,
10811 "implicit conversion from %qT to %qT "
10812 "to match other operand of binary "
10815 if (result_type
== error_mark_node
)
10816 return error_mark_node
;
10819 if (first_complex
!= second_complex
10820 && (code
== PLUS_EXPR
10821 || code
== MINUS_EXPR
10822 || code
== MULT_EXPR
10823 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
10824 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
10825 && flag_signed_zeros
)
10827 /* An operation on mixed real/complex operands must be
10828 handled specially, but the language-independent code can
10829 more easily optimize the plain complex arithmetic if
10830 -fno-signed-zeros. */
10831 tree real_type
= TREE_TYPE (result_type
);
10833 if (type0
!= orig_type0
|| type1
!= orig_type1
)
10835 gcc_assert (may_need_excess_precision
&& common
);
10836 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
10840 if (TREE_TYPE (op0
) != result_type
)
10841 op0
= convert_and_check (location
, result_type
, op0
);
10842 if (TREE_TYPE (op1
) != real_type
)
10843 op1
= convert_and_check (location
, real_type
, op1
);
10847 if (TREE_TYPE (op0
) != real_type
)
10848 op0
= convert_and_check (location
, real_type
, op0
);
10849 if (TREE_TYPE (op1
) != result_type
)
10850 op1
= convert_and_check (location
, result_type
, op1
);
10852 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
10853 return error_mark_node
;
10856 op0
= c_save_expr (op0
);
10857 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
10859 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
10864 case TRUNC_DIV_EXPR
:
10865 op1
= c_save_expr (op1
);
10866 imag
= build2 (resultcode
, real_type
, imag
, op1
);
10867 /* Fall through. */
10870 real
= build2 (resultcode
, real_type
, real
, op1
);
10878 op1
= c_save_expr (op1
);
10879 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
10881 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
10886 op0
= c_save_expr (op0
);
10887 imag
= build2 (resultcode
, real_type
, op0
, imag
);
10888 /* Fall through. */
10890 real
= build2 (resultcode
, real_type
, op0
, real
);
10893 real
= build2 (resultcode
, real_type
, op0
, real
);
10894 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
10900 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
10901 goto return_build_binary_op
;
10904 /* For certain operations (which identify themselves by shorten != 0)
10905 if both args were extended from the same smaller type,
10906 do the arithmetic in that type and then extend.
10908 shorten !=0 and !=1 indicates a bitwise operation.
10909 For them, this optimization is safe only if
10910 both args are zero-extended or both are sign-extended.
10911 Otherwise, we might change the result.
10912 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10913 but calculated in (unsigned short) it would be (unsigned short)-1. */
10915 if (shorten
&& none_complex
)
10917 final_type
= result_type
;
10918 result_type
= shorten_binary_op (result_type
, op0
, op1
,
10922 /* Shifts can be shortened if shifting right. */
10927 tree arg0
= get_narrower (op0
, &unsigned_arg
);
10929 final_type
= result_type
;
10931 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
10932 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
10934 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
10935 && tree_int_cst_sgn (op1
) > 0
10936 /* We can shorten only if the shift count is less than the
10937 number of bits in the smaller type size. */
10938 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
10939 /* We cannot drop an unsigned shift after sign-extension. */
10940 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
10942 /* Do an unsigned shift if the operand was zero-extended. */
10944 = c_common_signed_or_unsigned_type (unsigned_arg
,
10946 /* Convert value-to-be-shifted to that type. */
10947 if (TREE_TYPE (op0
) != result_type
)
10948 op0
= convert (result_type
, op0
);
10953 /* Comparison operations are shortened too but differently.
10954 They identify themselves by setting short_compare = 1. */
10958 /* Don't write &op0, etc., because that would prevent op0
10959 from being kept in a register.
10960 Instead, make copies of the our local variables and
10961 pass the copies by reference, then copy them back afterward. */
10962 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
10963 enum tree_code xresultcode
= resultcode
;
10965 = shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
10971 goto return_build_binary_op
;
10974 op0
= xop0
, op1
= xop1
;
10976 resultcode
= xresultcode
;
10978 if (c_inhibit_evaluation_warnings
== 0)
10980 bool op0_maybe_const
= true;
10981 bool op1_maybe_const
= true;
10982 tree orig_op0_folded
, orig_op1_folded
;
10984 if (in_late_binary_op
)
10986 orig_op0_folded
= orig_op0
;
10987 orig_op1_folded
= orig_op1
;
10991 /* Fold for the sake of possible warnings, as in
10992 build_conditional_expr. This requires the
10993 "original" values to be folded, not just op0 and
10995 c_inhibit_evaluation_warnings
++;
10996 op0
= c_fully_fold (op0
, require_constant_value
,
10998 op1
= c_fully_fold (op1
, require_constant_value
,
11000 c_inhibit_evaluation_warnings
--;
11001 orig_op0_folded
= c_fully_fold (orig_op0
,
11002 require_constant_value
,
11004 orig_op1_folded
= c_fully_fold (orig_op1
,
11005 require_constant_value
,
11009 if (warn_sign_compare
)
11010 warn_for_sign_compare (location
, orig_op0_folded
,
11011 orig_op1_folded
, op0
, op1
,
11012 result_type
, resultcode
);
11013 if (!in_late_binary_op
&& !int_operands
)
11015 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
11016 op0
= c_wrap_maybe_const (op0
, !op0_maybe_const
);
11017 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
11018 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
11024 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11025 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11026 Then the expression will be built.
11027 It will be given type FINAL_TYPE if that is nonzero;
11028 otherwise, it will be given type RESULT_TYPE. */
11032 binary_op_error (location
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
11033 return error_mark_node
;
11036 if (build_type
== NULL_TREE
)
11038 build_type
= result_type
;
11039 if ((type0
!= orig_type0
|| type1
!= orig_type1
)
11042 gcc_assert (may_need_excess_precision
&& common
);
11043 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
11049 op0
= ep_convert_and_check (location
, result_type
, op0
,
11050 semantic_result_type
);
11051 op1
= ep_convert_and_check (location
, result_type
, op1
,
11052 semantic_result_type
);
11054 /* This can happen if one operand has a vector type, and the other
11055 has a different type. */
11056 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
11057 return error_mark_node
;
11060 if ((flag_sanitize
& (SANITIZE_SHIFT
| SANITIZE_DIVIDE
11061 | SANITIZE_FLOAT_DIVIDE
))
11062 && current_function_decl
!= 0
11063 && !lookup_attribute ("no_sanitize_undefined",
11064 DECL_ATTRIBUTES (current_function_decl
))
11065 && (doing_div_or_mod
|| doing_shift
))
11067 /* OP0 and/or OP1 might have side-effects. */
11068 op0
= c_save_expr (op0
);
11069 op1
= c_save_expr (op1
);
11070 op0
= c_fully_fold (op0
, false, NULL
);
11071 op1
= c_fully_fold (op1
, false, NULL
);
11072 if (doing_div_or_mod
&& (flag_sanitize
& (SANITIZE_DIVIDE
11073 | SANITIZE_FLOAT_DIVIDE
)))
11074 instrument_expr
= ubsan_instrument_division (location
, op0
, op1
);
11075 else if (doing_shift
&& (flag_sanitize
& SANITIZE_SHIFT
))
11076 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
11079 /* Treat expressions in initializers specially as they can't trap. */
11080 if (int_const_or_overflow
)
11081 ret
= (require_constant_value
11082 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
11084 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
11086 ret
= build2 (resultcode
, build_type
, op0
, op1
);
11087 if (final_type
!= 0)
11088 ret
= convert (final_type
, ret
);
11090 return_build_binary_op
:
11091 gcc_assert (ret
!= error_mark_node
);
11092 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
11093 ret
= (int_operands
11094 ? note_integer_operands (ret
)
11095 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
11096 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
11097 && !in_late_binary_op
)
11098 ret
= note_integer_operands (ret
);
11099 if (semantic_result_type
)
11100 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
11101 protected_set_expr_location (ret
, location
);
11103 if (instrument_expr
!= NULL
)
11104 ret
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (ret
),
11105 instrument_expr
, ret
);
11111 /* Convert EXPR to be a truth-value, validating its type for this
11112 purpose. LOCATION is the source location for the expression. */
11115 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
11117 bool int_const
, int_operands
;
11119 switch (TREE_CODE (TREE_TYPE (expr
)))
11122 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
11123 return error_mark_node
;
11126 error_at (location
, "used struct type value where scalar is required");
11127 return error_mark_node
;
11130 error_at (location
, "used union type value where scalar is required");
11131 return error_mark_node
;
11134 error_at (location
, "void value not ignored as it ought to be");
11135 return error_mark_node
;
11137 case FUNCTION_TYPE
:
11138 gcc_unreachable ();
11141 error_at (location
, "used vector type where scalar is required");
11142 return error_mark_node
;
11148 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
11149 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
11150 if (int_operands
&& TREE_CODE (expr
) != INTEGER_CST
)
11152 expr
= remove_c_maybe_const_expr (expr
);
11153 expr
= build2 (NE_EXPR
, integer_type_node
, expr
,
11154 convert (TREE_TYPE (expr
), integer_zero_node
));
11155 expr
= note_integer_operands (expr
);
11158 /* ??? Should we also give an error for vectors rather than leaving
11159 those to give errors later? */
11160 expr
= c_common_truthvalue_conversion (location
, expr
);
11162 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
11164 if (TREE_OVERFLOW (expr
))
11167 return note_integer_operands (expr
);
11169 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
11170 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
11175 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11179 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
11181 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
11183 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
11184 /* Executing a compound literal inside a function reinitializes
11186 if (!TREE_STATIC (decl
))
11194 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11197 c_begin_omp_parallel (void)
11201 keep_next_level ();
11202 block
= c_begin_compound_stmt (true);
11207 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11208 statement. LOC is the location of the OMP_PARALLEL. */
11211 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
11215 block
= c_end_compound_stmt (loc
, block
, true);
11217 stmt
= make_node (OMP_PARALLEL
);
11218 TREE_TYPE (stmt
) = void_type_node
;
11219 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
11220 OMP_PARALLEL_BODY (stmt
) = block
;
11221 SET_EXPR_LOCATION (stmt
, loc
);
11223 return add_stmt (stmt
);
11226 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11229 c_begin_omp_task (void)
11233 keep_next_level ();
11234 block
= c_begin_compound_stmt (true);
11239 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11240 statement. LOC is the location of the #pragma. */
11243 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
11247 block
= c_end_compound_stmt (loc
, block
, true);
11249 stmt
= make_node (OMP_TASK
);
11250 TREE_TYPE (stmt
) = void_type_node
;
11251 OMP_TASK_CLAUSES (stmt
) = clauses
;
11252 OMP_TASK_BODY (stmt
) = block
;
11253 SET_EXPR_LOCATION (stmt
, loc
);
11255 return add_stmt (stmt
);
11258 /* Generate GOMP_cancel call for #pragma omp cancel. */
11261 c_finish_omp_cancel (location_t loc
, tree clauses
)
11263 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCEL
);
11265 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
11267 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
11269 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
11271 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
11275 error_at (loc
, "%<#pragma omp cancel must specify one of "
11276 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11280 tree ifc
= find_omp_clause (clauses
, OMP_CLAUSE_IF
);
11281 if (ifc
!= NULL_TREE
)
11283 tree type
= TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc
));
11284 ifc
= fold_build2_loc (OMP_CLAUSE_LOCATION (ifc
), NE_EXPR
,
11285 boolean_type_node
, OMP_CLAUSE_IF_EXPR (ifc
),
11286 build_zero_cst (type
));
11289 ifc
= boolean_true_node
;
11290 tree stmt
= build_call_expr_loc (loc
, fn
, 2,
11291 build_int_cst (integer_type_node
, mask
),
11296 /* Generate GOMP_cancellation_point call for
11297 #pragma omp cancellation point. */
11300 c_finish_omp_cancellation_point (location_t loc
, tree clauses
)
11302 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT
);
11304 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
11306 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
11308 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
11310 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
11314 error_at (loc
, "%<#pragma omp cancellation point must specify one of "
11315 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11319 tree stmt
= build_call_expr_loc (loc
, fn
, 1,
11320 build_int_cst (integer_type_node
, mask
));
11324 /* Helper function for handle_omp_array_sections. Called recursively
11325 to handle multiple array-section-subscripts. C is the clause,
11326 T current expression (initially OMP_CLAUSE_DECL), which is either
11327 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11328 expression if specified, TREE_VALUE length expression if specified,
11329 TREE_CHAIN is what it has been specified after, or some decl.
11330 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11331 set to true if any of the array-section-subscript could have length
11332 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11333 first array-section-subscript which is known not to have length
11335 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11336 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11337 all are or may have length of 1, array-section-subscript [:2] is the
11338 first one knonwn not to have length 1. For array-section-subscript
11339 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11340 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11341 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11342 case though, as some lengths could be zero. */
11345 handle_omp_array_sections_1 (tree c
, tree t
, vec
<tree
> &types
,
11346 bool &maybe_zero_len
, unsigned int &first_non_one
)
11348 tree ret
, low_bound
, length
, type
;
11349 if (TREE_CODE (t
) != TREE_LIST
)
11351 if (t
== error_mark_node
|| TREE_TYPE (t
) == error_mark_node
)
11352 return error_mark_node
;
11353 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11356 error_at (OMP_CLAUSE_LOCATION (c
),
11357 "%qD is not a variable in %qs clause", t
,
11358 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11360 error_at (OMP_CLAUSE_LOCATION (c
),
11361 "%qE is not a variable in %qs clause", t
,
11362 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11363 return error_mark_node
;
11365 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
11366 && TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
11368 error_at (OMP_CLAUSE_LOCATION (c
),
11369 "%qD is threadprivate variable in %qs clause", t
,
11370 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11371 return error_mark_node
;
11376 ret
= handle_omp_array_sections_1 (c
, TREE_CHAIN (t
), types
,
11377 maybe_zero_len
, first_non_one
);
11378 if (ret
== error_mark_node
|| ret
== NULL_TREE
)
11381 type
= TREE_TYPE (ret
);
11382 low_bound
= TREE_PURPOSE (t
);
11383 length
= TREE_VALUE (t
);
11385 if (low_bound
== error_mark_node
|| length
== error_mark_node
)
11386 return error_mark_node
;
11388 if (low_bound
&& !INTEGRAL_TYPE_P (TREE_TYPE (low_bound
)))
11390 error_at (OMP_CLAUSE_LOCATION (c
),
11391 "low bound %qE of array section does not have integral type",
11393 return error_mark_node
;
11395 if (length
&& !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
11397 error_at (OMP_CLAUSE_LOCATION (c
),
11398 "length %qE of array section does not have integral type",
11400 return error_mark_node
;
11403 && TREE_CODE (low_bound
) == INTEGER_CST
11404 && TYPE_PRECISION (TREE_TYPE (low_bound
))
11405 > TYPE_PRECISION (sizetype
))
11406 low_bound
= fold_convert (sizetype
, low_bound
);
11408 && TREE_CODE (length
) == INTEGER_CST
11409 && TYPE_PRECISION (TREE_TYPE (length
))
11410 > TYPE_PRECISION (sizetype
))
11411 length
= fold_convert (sizetype
, length
);
11412 if (low_bound
== NULL_TREE
)
11413 low_bound
= integer_zero_node
;
11415 if (length
!= NULL_TREE
)
11417 if (!integer_nonzerop (length
))
11418 maybe_zero_len
= true;
11419 if (first_non_one
== types
.length ()
11420 && (TREE_CODE (length
) != INTEGER_CST
|| integer_onep (length
)))
11423 if (TREE_CODE (type
) == ARRAY_TYPE
)
11425 if (length
== NULL_TREE
11426 && (TYPE_DOMAIN (type
) == NULL_TREE
11427 || TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
))
11429 error_at (OMP_CLAUSE_LOCATION (c
),
11430 "for unknown bound array type length expression must "
11432 return error_mark_node
;
11434 if (TREE_CODE (low_bound
) == INTEGER_CST
11435 && tree_int_cst_sgn (low_bound
) == -1)
11437 error_at (OMP_CLAUSE_LOCATION (c
),
11438 "negative low bound in array section in %qs clause",
11439 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11440 return error_mark_node
;
11442 if (length
!= NULL_TREE
11443 && TREE_CODE (length
) == INTEGER_CST
11444 && tree_int_cst_sgn (length
) == -1)
11446 error_at (OMP_CLAUSE_LOCATION (c
),
11447 "negative length in array section in %qs clause",
11448 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11449 return error_mark_node
;
11451 if (TYPE_DOMAIN (type
)
11452 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
11453 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
11456 tree size
= size_binop (PLUS_EXPR
,
11457 TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
11459 if (TREE_CODE (low_bound
) == INTEGER_CST
)
11461 if (tree_int_cst_lt (size
, low_bound
))
11463 error_at (OMP_CLAUSE_LOCATION (c
),
11464 "low bound %qE above array section size "
11465 "in %qs clause", low_bound
,
11466 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11467 return error_mark_node
;
11469 if (tree_int_cst_equal (size
, low_bound
))
11470 maybe_zero_len
= true;
11471 else if (length
== NULL_TREE
11472 && first_non_one
== types
.length ()
11473 && tree_int_cst_equal
11474 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
11478 else if (length
== NULL_TREE
)
11480 maybe_zero_len
= true;
11481 if (first_non_one
== types
.length ())
11484 if (length
&& TREE_CODE (length
) == INTEGER_CST
)
11486 if (tree_int_cst_lt (size
, length
))
11488 error_at (OMP_CLAUSE_LOCATION (c
),
11489 "length %qE above array section size "
11490 "in %qs clause", length
,
11491 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11492 return error_mark_node
;
11494 if (TREE_CODE (low_bound
) == INTEGER_CST
)
11497 = size_binop (PLUS_EXPR
,
11498 fold_convert (sizetype
, low_bound
),
11499 fold_convert (sizetype
, length
));
11500 if (TREE_CODE (lbpluslen
) == INTEGER_CST
11501 && tree_int_cst_lt (size
, lbpluslen
))
11503 error_at (OMP_CLAUSE_LOCATION (c
),
11504 "high bound %qE above array section size "
11505 "in %qs clause", lbpluslen
,
11506 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11507 return error_mark_node
;
11512 else if (length
== NULL_TREE
)
11514 maybe_zero_len
= true;
11515 if (first_non_one
== types
.length ())
11519 /* For [lb:] we will need to evaluate lb more than once. */
11520 if (length
== NULL_TREE
&& OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
11522 tree lb
= c_save_expr (low_bound
);
11523 if (lb
!= low_bound
)
11525 TREE_PURPOSE (t
) = lb
;
11530 else if (TREE_CODE (type
) == POINTER_TYPE
)
11532 if (length
== NULL_TREE
)
11534 error_at (OMP_CLAUSE_LOCATION (c
),
11535 "for pointer type length expression must be specified");
11536 return error_mark_node
;
11538 /* If there is a pointer type anywhere but in the very first
11539 array-section-subscript, the array section can't be contiguous. */
11540 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
11541 && TREE_CODE (TREE_CHAIN (t
)) == TREE_LIST
)
11543 error_at (OMP_CLAUSE_LOCATION (c
),
11544 "array section is not contiguous in %qs clause",
11545 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11546 return error_mark_node
;
11551 error_at (OMP_CLAUSE_LOCATION (c
),
11552 "%qE does not have pointer or array type", ret
);
11553 return error_mark_node
;
11555 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
11556 types
.safe_push (TREE_TYPE (ret
));
11557 /* We will need to evaluate lb more than once. */
11558 tree lb
= c_save_expr (low_bound
);
11559 if (lb
!= low_bound
)
11561 TREE_PURPOSE (t
) = lb
;
11564 ret
= build_array_ref (OMP_CLAUSE_LOCATION (c
), ret
, low_bound
);
11568 /* Handle array sections for clause C. */
11571 handle_omp_array_sections (tree c
)
11573 bool maybe_zero_len
= false;
11574 unsigned int first_non_one
= 0;
11575 vec
<tree
> types
= vNULL
;
11576 tree first
= handle_omp_array_sections_1 (c
, OMP_CLAUSE_DECL (c
), types
,
11577 maybe_zero_len
, first_non_one
);
11578 if (first
== error_mark_node
)
11583 if (first
== NULL_TREE
)
11588 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
)
11590 tree t
= OMP_CLAUSE_DECL (c
);
11591 tree tem
= NULL_TREE
;
11593 /* Need to evaluate side effects in the length expressions
11595 while (TREE_CODE (t
) == TREE_LIST
)
11597 if (TREE_VALUE (t
) && TREE_SIDE_EFFECTS (TREE_VALUE (t
)))
11599 if (tem
== NULL_TREE
)
11600 tem
= TREE_VALUE (t
);
11602 tem
= build2 (COMPOUND_EXPR
, TREE_TYPE (tem
),
11603 TREE_VALUE (t
), tem
);
11605 t
= TREE_CHAIN (t
);
11608 first
= build2 (COMPOUND_EXPR
, TREE_TYPE (first
), tem
, first
);
11609 first
= c_fully_fold (first
, false, NULL
);
11610 OMP_CLAUSE_DECL (c
) = first
;
11614 unsigned int num
= types
.length (), i
;
11615 tree t
, side_effects
= NULL_TREE
, size
= NULL_TREE
;
11616 tree condition
= NULL_TREE
;
11618 if (int_size_in_bytes (TREE_TYPE (first
)) <= 0)
11619 maybe_zero_len
= true;
11621 for (i
= num
, t
= OMP_CLAUSE_DECL (c
); i
> 0;
11622 t
= TREE_CHAIN (t
))
11624 tree low_bound
= TREE_PURPOSE (t
);
11625 tree length
= TREE_VALUE (t
);
11629 && TREE_CODE (low_bound
) == INTEGER_CST
11630 && TYPE_PRECISION (TREE_TYPE (low_bound
))
11631 > TYPE_PRECISION (sizetype
))
11632 low_bound
= fold_convert (sizetype
, low_bound
);
11634 && TREE_CODE (length
) == INTEGER_CST
11635 && TYPE_PRECISION (TREE_TYPE (length
))
11636 > TYPE_PRECISION (sizetype
))
11637 length
= fold_convert (sizetype
, length
);
11638 if (low_bound
== NULL_TREE
)
11639 low_bound
= integer_zero_node
;
11640 if (!maybe_zero_len
&& i
> first_non_one
)
11642 if (integer_nonzerop (low_bound
))
11643 goto do_warn_noncontiguous
;
11644 if (length
!= NULL_TREE
11645 && TREE_CODE (length
) == INTEGER_CST
11646 && TYPE_DOMAIN (types
[i
])
11647 && TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
]))
11648 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])))
11652 size
= size_binop (PLUS_EXPR
,
11653 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
11655 if (!tree_int_cst_equal (length
, size
))
11657 do_warn_noncontiguous
:
11658 error_at (OMP_CLAUSE_LOCATION (c
),
11659 "array section is not contiguous in %qs "
11661 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11666 if (length
!= NULL_TREE
11667 && TREE_SIDE_EFFECTS (length
))
11669 if (side_effects
== NULL_TREE
)
11670 side_effects
= length
;
11672 side_effects
= build2 (COMPOUND_EXPR
,
11673 TREE_TYPE (side_effects
),
11674 length
, side_effects
);
11681 if (i
> first_non_one
&& length
&& integer_nonzerop (length
))
11684 l
= fold_convert (sizetype
, length
);
11687 l
= size_binop (PLUS_EXPR
,
11688 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
11690 l
= size_binop (MINUS_EXPR
, l
,
11691 fold_convert (sizetype
, low_bound
));
11693 if (i
> first_non_one
)
11695 l
= fold_build2 (NE_EXPR
, boolean_type_node
, l
,
11697 if (condition
== NULL_TREE
)
11700 condition
= fold_build2 (BIT_AND_EXPR
, boolean_type_node
,
11703 else if (size
== NULL_TREE
)
11705 size
= size_in_bytes (TREE_TYPE (types
[i
]));
11706 size
= size_binop (MULT_EXPR
, size
, l
);
11708 size
= fold_build3 (COND_EXPR
, sizetype
, condition
,
11709 size
, size_zero_node
);
11712 size
= size_binop (MULT_EXPR
, size
, l
);
11717 size
= build2 (COMPOUND_EXPR
, sizetype
, side_effects
, size
);
11718 first
= c_fully_fold (first
, false, NULL
);
11719 OMP_CLAUSE_DECL (c
) = first
;
11721 size
= c_fully_fold (size
, false, NULL
);
11722 OMP_CLAUSE_SIZE (c
) = size
;
11723 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
11725 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_MAP
);
11726 OMP_CLAUSE_MAP_KIND (c2
) = OMP_CLAUSE_MAP_POINTER
;
11727 if (!c_mark_addressable (t
))
11729 OMP_CLAUSE_DECL (c2
) = t
;
11730 t
= build_fold_addr_expr (first
);
11731 t
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), ptrdiff_type_node
, t
);
11732 tree ptr
= OMP_CLAUSE_DECL (c2
);
11733 if (!POINTER_TYPE_P (TREE_TYPE (ptr
)))
11734 ptr
= build_fold_addr_expr (ptr
);
11735 t
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
11736 ptrdiff_type_node
, t
,
11737 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
11738 ptrdiff_type_node
, ptr
));
11739 t
= c_fully_fold (t
, false, NULL
);
11740 OMP_CLAUSE_SIZE (c2
) = t
;
11741 OMP_CLAUSE_CHAIN (c2
) = OMP_CLAUSE_CHAIN (c
);
11742 OMP_CLAUSE_CHAIN (c
) = c2
;
11747 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11748 an inline call. But, remap
11749 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11750 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11753 c_clone_omp_udr (tree stmt
, tree omp_decl1
, tree omp_decl2
,
11754 tree decl
, tree placeholder
)
11757 hash_map
<tree
, tree
> decl_map
;
11759 decl_map
.put (omp_decl1
, placeholder
);
11760 decl_map
.put (omp_decl2
, decl
);
11761 memset (&id
, 0, sizeof (id
));
11762 id
.src_fn
= DECL_CONTEXT (omp_decl1
);
11763 id
.dst_fn
= current_function_decl
;
11764 id
.src_cfun
= DECL_STRUCT_FUNCTION (id
.src_fn
);
11765 id
.decl_map
= &decl_map
;
11767 id
.copy_decl
= copy_decl_no_change
;
11768 id
.transform_call_graph_edges
= CB_CGE_DUPLICATE
;
11769 id
.transform_new_cfg
= true;
11770 id
.transform_return_to_modify
= false;
11771 id
.transform_lang_insert_block
= NULL
;
11773 walk_tree (&stmt
, copy_tree_body_r
, &id
, NULL
);
11777 /* Helper function of c_finish_omp_clauses, called via walk_tree.
11778 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11781 c_find_omp_placeholder_r (tree
*tp
, int *, void *data
)
11783 if (*tp
== (tree
) data
)
11788 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
11789 Remove any elements from the list that are invalid. */
11792 c_finish_omp_clauses (tree clauses
)
11794 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
11795 bitmap_head aligned_head
;
11797 bool branch_seen
= false;
11798 bool copyprivate_seen
= false;
11799 tree
*nowait_clause
= NULL
;
11801 bitmap_obstack_initialize (NULL
);
11802 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
11803 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
11804 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
11805 bitmap_initialize (&aligned_head
, &bitmap_default_obstack
);
11807 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
11809 bool remove
= false;
11810 bool need_complete
= false;
11811 bool need_implicitly_determined
= false;
11813 switch (OMP_CLAUSE_CODE (c
))
11815 case OMP_CLAUSE_SHARED
:
11816 need_implicitly_determined
= true;
11817 goto check_dup_generic
;
11819 case OMP_CLAUSE_PRIVATE
:
11820 need_complete
= true;
11821 need_implicitly_determined
= true;
11822 goto check_dup_generic
;
11824 case OMP_CLAUSE_REDUCTION
:
11825 need_implicitly_determined
= true;
11826 t
= OMP_CLAUSE_DECL (c
);
11827 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == NULL_TREE
11828 && (FLOAT_TYPE_P (TREE_TYPE (t
))
11829 || TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
))
11831 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
11832 const char *r_name
= NULL
;
11841 if (TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
)
11845 if (TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
)
11857 case TRUTH_ANDIF_EXPR
:
11858 if (FLOAT_TYPE_P (TREE_TYPE (t
)))
11861 case TRUTH_ORIF_EXPR
:
11862 if (FLOAT_TYPE_P (TREE_TYPE (t
)))
11866 gcc_unreachable ();
11870 error_at (OMP_CLAUSE_LOCATION (c
),
11871 "%qE has invalid type for %<reduction(%s)%>",
11877 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == error_mark_node
)
11879 error_at (OMP_CLAUSE_LOCATION (c
),
11880 "user defined reduction not found for %qD", t
);
11884 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
11886 tree list
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
);
11887 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (t
));
11888 tree placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
11889 VAR_DECL
, NULL_TREE
, type
);
11890 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = placeholder
;
11891 DECL_ARTIFICIAL (placeholder
) = 1;
11892 DECL_IGNORED_P (placeholder
) = 1;
11893 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 0)))
11894 c_mark_addressable (placeholder
);
11895 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 1)))
11896 c_mark_addressable (OMP_CLAUSE_DECL (c
));
11897 OMP_CLAUSE_REDUCTION_MERGE (c
)
11898 = c_clone_omp_udr (TREE_VEC_ELT (list
, 2),
11899 TREE_VEC_ELT (list
, 0),
11900 TREE_VEC_ELT (list
, 1),
11901 OMP_CLAUSE_DECL (c
), placeholder
);
11902 OMP_CLAUSE_REDUCTION_MERGE (c
)
11903 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
11904 void_type_node
, NULL_TREE
,
11905 OMP_CLAUSE_REDUCTION_MERGE (c
), NULL_TREE
);
11906 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c
)) = 1;
11907 if (TREE_VEC_LENGTH (list
) == 6)
11909 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 3)))
11910 c_mark_addressable (OMP_CLAUSE_DECL (c
));
11911 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 4)))
11912 c_mark_addressable (placeholder
);
11913 tree init
= TREE_VEC_ELT (list
, 5);
11914 if (init
== error_mark_node
)
11915 init
= DECL_INITIAL (TREE_VEC_ELT (list
, 3));
11916 OMP_CLAUSE_REDUCTION_INIT (c
)
11917 = c_clone_omp_udr (init
, TREE_VEC_ELT (list
, 4),
11918 TREE_VEC_ELT (list
, 3),
11919 OMP_CLAUSE_DECL (c
), placeholder
);
11920 if (TREE_VEC_ELT (list
, 5) == error_mark_node
)
11921 OMP_CLAUSE_REDUCTION_INIT (c
)
11922 = build2 (INIT_EXPR
, TREE_TYPE (t
), t
,
11923 OMP_CLAUSE_REDUCTION_INIT (c
));
11924 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
11925 c_find_omp_placeholder_r
,
11926 placeholder
, NULL
))
11927 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c
) = 1;
11932 if (AGGREGATE_TYPE_P (TREE_TYPE (t
)))
11933 init
= build_constructor (TREE_TYPE (t
), NULL
);
11935 init
= fold_convert (TREE_TYPE (t
), integer_zero_node
);
11936 OMP_CLAUSE_REDUCTION_INIT (c
)
11937 = build2 (INIT_EXPR
, TREE_TYPE (t
), t
, init
);
11939 OMP_CLAUSE_REDUCTION_INIT (c
)
11940 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
11941 void_type_node
, NULL_TREE
,
11942 OMP_CLAUSE_REDUCTION_INIT (c
), NULL_TREE
);
11943 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c
)) = 1;
11945 goto check_dup_generic
;
11947 case OMP_CLAUSE_COPYPRIVATE
:
11948 copyprivate_seen
= true;
11951 error_at (OMP_CLAUSE_LOCATION (*nowait_clause
),
11952 "%<nowait%> clause must not be used together "
11953 "with %<copyprivate%>");
11954 *nowait_clause
= OMP_CLAUSE_CHAIN (*nowait_clause
);
11955 nowait_clause
= NULL
;
11957 goto check_dup_generic
;
11959 case OMP_CLAUSE_COPYIN
:
11960 t
= OMP_CLAUSE_DECL (c
);
11961 if (TREE_CODE (t
) != VAR_DECL
|| !DECL_THREAD_LOCAL_P (t
))
11963 error_at (OMP_CLAUSE_LOCATION (c
),
11964 "%qE must be %<threadprivate%> for %<copyin%>", t
);
11968 goto check_dup_generic
;
11970 case OMP_CLAUSE_LINEAR
:
11971 t
= OMP_CLAUSE_DECL (c
);
11972 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
11973 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
11975 error_at (OMP_CLAUSE_LOCATION (c
),
11976 "linear clause applied to non-integral non-pointer "
11977 "variable with type %qT", TREE_TYPE (t
));
11981 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c
))) == POINTER_TYPE
)
11983 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
11984 s
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
), PLUS_EXPR
,
11985 OMP_CLAUSE_DECL (c
), s
);
11986 s
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
11987 sizetype
, s
, OMP_CLAUSE_DECL (c
));
11988 if (s
== error_mark_node
)
11990 OMP_CLAUSE_LINEAR_STEP (c
) = s
;
11993 OMP_CLAUSE_LINEAR_STEP (c
)
11994 = fold_convert (TREE_TYPE (t
), OMP_CLAUSE_LINEAR_STEP (c
));
11995 goto check_dup_generic
;
11998 t
= OMP_CLAUSE_DECL (c
);
11999 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12001 error_at (OMP_CLAUSE_LOCATION (c
),
12002 "%qE is not a variable in clause %qs", t
,
12003 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12006 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
12007 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
12008 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
12010 error_at (OMP_CLAUSE_LOCATION (c
),
12011 "%qE appears more than once in data clauses", t
);
12015 bitmap_set_bit (&generic_head
, DECL_UID (t
));
12018 case OMP_CLAUSE_FIRSTPRIVATE
:
12019 t
= OMP_CLAUSE_DECL (c
);
12020 need_complete
= true;
12021 need_implicitly_determined
= true;
12022 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12024 error_at (OMP_CLAUSE_LOCATION (c
),
12025 "%qE is not a variable in clause %<firstprivate%>", t
);
12028 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
12029 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
12031 error_at (OMP_CLAUSE_LOCATION (c
),
12032 "%qE appears more than once in data clauses", t
);
12036 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
12039 case OMP_CLAUSE_LASTPRIVATE
:
12040 t
= OMP_CLAUSE_DECL (c
);
12041 need_complete
= true;
12042 need_implicitly_determined
= true;
12043 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12045 error_at (OMP_CLAUSE_LOCATION (c
),
12046 "%qE is not a variable in clause %<lastprivate%>", t
);
12049 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
12050 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
12052 error_at (OMP_CLAUSE_LOCATION (c
),
12053 "%qE appears more than once in data clauses", t
);
12057 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
12060 case OMP_CLAUSE_ALIGNED
:
12061 t
= OMP_CLAUSE_DECL (c
);
12062 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12064 error_at (OMP_CLAUSE_LOCATION (c
),
12065 "%qE is not a variable in %<aligned%> clause", t
);
12068 else if (!POINTER_TYPE_P (TREE_TYPE (t
))
12069 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
12071 error_at (OMP_CLAUSE_LOCATION (c
),
12072 "%qE in %<aligned%> clause is neither a pointer nor "
12076 else if (bitmap_bit_p (&aligned_head
, DECL_UID (t
)))
12078 error_at (OMP_CLAUSE_LOCATION (c
),
12079 "%qE appears more than once in %<aligned%> clauses",
12084 bitmap_set_bit (&aligned_head
, DECL_UID (t
));
12087 case OMP_CLAUSE_DEPEND
:
12088 t
= OMP_CLAUSE_DECL (c
);
12089 if (TREE_CODE (t
) == TREE_LIST
)
12091 if (handle_omp_array_sections (c
))
12095 if (t
== error_mark_node
)
12097 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12099 error_at (OMP_CLAUSE_LOCATION (c
),
12100 "%qE is not a variable in %<depend%> clause", t
);
12103 else if (!c_mark_addressable (t
))
12107 case OMP_CLAUSE_MAP
:
12108 case OMP_CLAUSE_TO
:
12109 case OMP_CLAUSE_FROM
:
12110 t
= OMP_CLAUSE_DECL (c
);
12111 if (TREE_CODE (t
) == TREE_LIST
)
12113 if (handle_omp_array_sections (c
))
12117 t
= OMP_CLAUSE_DECL (c
);
12118 if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
12120 error_at (OMP_CLAUSE_LOCATION (c
),
12121 "array section does not have mappable type "
12123 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12129 if (t
== error_mark_node
)
12131 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12133 error_at (OMP_CLAUSE_LOCATION (c
),
12134 "%qE is not a variable in %qs clause", t
,
12135 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12138 else if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
12140 error_at (OMP_CLAUSE_LOCATION (c
),
12141 "%qD is threadprivate variable in %qs clause", t
,
12142 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12145 else if (!c_mark_addressable (t
))
12147 else if (!(OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
12148 && OMP_CLAUSE_MAP_KIND (c
) == OMP_CLAUSE_MAP_POINTER
)
12149 && !lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
12151 error_at (OMP_CLAUSE_LOCATION (c
),
12152 "%qD does not have a mappable type in %qs clause", t
,
12153 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12156 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
)))
12158 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
12159 error ("%qD appears more than once in motion clauses", t
);
12161 error ("%qD appears more than once in map clauses", t
);
12165 bitmap_set_bit (&generic_head
, DECL_UID (t
));
12168 case OMP_CLAUSE_UNIFORM
:
12169 t
= OMP_CLAUSE_DECL (c
);
12170 if (TREE_CODE (t
) != PARM_DECL
)
12173 error_at (OMP_CLAUSE_LOCATION (c
),
12174 "%qD is not an argument in %<uniform%> clause", t
);
12176 error_at (OMP_CLAUSE_LOCATION (c
),
12177 "%qE is not an argument in %<uniform%> clause", t
);
12181 goto check_dup_generic
;
12183 case OMP_CLAUSE_NOWAIT
:
12184 if (copyprivate_seen
)
12186 error_at (OMP_CLAUSE_LOCATION (c
),
12187 "%<nowait%> clause must not be used together "
12188 "with %<copyprivate%>");
12192 nowait_clause
= pc
;
12193 pc
= &OMP_CLAUSE_CHAIN (c
);
12196 case OMP_CLAUSE_IF
:
12197 case OMP_CLAUSE_NUM_THREADS
:
12198 case OMP_CLAUSE_NUM_TEAMS
:
12199 case OMP_CLAUSE_THREAD_LIMIT
:
12200 case OMP_CLAUSE_SCHEDULE
:
12201 case OMP_CLAUSE_ORDERED
:
12202 case OMP_CLAUSE_DEFAULT
:
12203 case OMP_CLAUSE_UNTIED
:
12204 case OMP_CLAUSE_COLLAPSE
:
12205 case OMP_CLAUSE_FINAL
:
12206 case OMP_CLAUSE_MERGEABLE
:
12207 case OMP_CLAUSE_SAFELEN
:
12208 case OMP_CLAUSE_SIMDLEN
:
12209 case OMP_CLAUSE_DEVICE
:
12210 case OMP_CLAUSE_DIST_SCHEDULE
:
12211 case OMP_CLAUSE_PARALLEL
:
12212 case OMP_CLAUSE_FOR
:
12213 case OMP_CLAUSE_SECTIONS
:
12214 case OMP_CLAUSE_TASKGROUP
:
12215 case OMP_CLAUSE_PROC_BIND
:
12216 pc
= &OMP_CLAUSE_CHAIN (c
);
12219 case OMP_CLAUSE_INBRANCH
:
12220 case OMP_CLAUSE_NOTINBRANCH
:
12223 error_at (OMP_CLAUSE_LOCATION (c
),
12224 "%<inbranch%> clause is incompatible with "
12225 "%<notinbranch%>");
12229 branch_seen
= true;
12230 pc
= &OMP_CLAUSE_CHAIN (c
);
12234 gcc_unreachable ();
12239 t
= OMP_CLAUSE_DECL (c
);
12243 t
= require_complete_type (t
);
12244 if (t
== error_mark_node
)
12248 if (need_implicitly_determined
)
12250 const char *share_name
= NULL
;
12252 if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
12253 share_name
= "threadprivate";
12254 else switch (c_omp_predetermined_sharing (t
))
12256 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
12258 case OMP_CLAUSE_DEFAULT_SHARED
:
12259 /* const vars may be specified in firstprivate clause. */
12260 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12261 && TREE_READONLY (t
))
12263 share_name
= "shared";
12265 case OMP_CLAUSE_DEFAULT_PRIVATE
:
12266 share_name
= "private";
12269 gcc_unreachable ();
12273 error_at (OMP_CLAUSE_LOCATION (c
),
12274 "%qE is predetermined %qs for %qs",
12276 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12283 *pc
= OMP_CLAUSE_CHAIN (c
);
12285 pc
= &OMP_CLAUSE_CHAIN (c
);
12288 bitmap_obstack_release (NULL
);
12292 /* Create a transaction node. */
12295 c_finish_transaction (location_t loc
, tree block
, int flags
)
12297 tree stmt
= build_stmt (loc
, TRANSACTION_EXPR
, block
);
12298 if (flags
& TM_STMT_ATTR_OUTER
)
12299 TRANSACTION_EXPR_OUTER (stmt
) = 1;
12300 if (flags
& TM_STMT_ATTR_RELAXED
)
12301 TRANSACTION_EXPR_RELAXED (stmt
) = 1;
12302 return add_stmt (stmt
);
12305 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12306 down to the element type of an array. */
12309 c_build_qualified_type (tree type
, int type_quals
)
12311 if (type
== error_mark_node
)
12314 if (TREE_CODE (type
) == ARRAY_TYPE
)
12317 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
12320 /* See if we already have an identically qualified type. */
12321 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
12323 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
12324 && TYPE_NAME (t
) == TYPE_NAME (type
)
12325 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
12326 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
12327 TYPE_ATTRIBUTES (type
)))
12332 tree domain
= TYPE_DOMAIN (type
);
12334 t
= build_variant_type_copy (type
);
12335 TREE_TYPE (t
) = element_type
;
12337 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
12338 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
12339 SET_TYPE_STRUCTURAL_EQUALITY (t
);
12340 else if (TYPE_CANONICAL (element_type
) != element_type
12341 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
12343 tree unqualified_canon
12344 = build_array_type (TYPE_CANONICAL (element_type
),
12345 domain
? TYPE_CANONICAL (domain
)
12348 = c_build_qualified_type (unqualified_canon
, type_quals
);
12351 TYPE_CANONICAL (t
) = t
;
12356 /* A restrict-qualified pointer type must be a pointer to object or
12357 incomplete type. Note that the use of POINTER_TYPE_P also allows
12358 REFERENCE_TYPEs, which is appropriate for C++. */
12359 if ((type_quals
& TYPE_QUAL_RESTRICT
)
12360 && (!POINTER_TYPE_P (type
)
12361 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
12363 error ("invalid use of %<restrict%>");
12364 type_quals
&= ~TYPE_QUAL_RESTRICT
;
12367 return build_qualified_type (type
, type_quals
);
12370 /* Build a VA_ARG_EXPR for the C parser. */
12373 c_build_va_arg (location_t loc
, tree expr
, tree type
)
12375 if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
12376 warning_at (loc
, OPT_Wc___compat
,
12377 "C++ requires promoted type, not enum type, in %<va_arg%>");
12378 return build_va_arg (loc
, expr
, type
);
12381 /* Return truthvalue of whether T1 is the same tree structure as T2.
12382 Return 1 if they are the same. Return 0 if they are different. */
12385 c_tree_equal (tree t1
, tree t2
)
12387 enum tree_code code1
, code2
;
12394 for (code1
= TREE_CODE (t1
);
12395 CONVERT_EXPR_CODE_P (code1
)
12396 || code1
== NON_LVALUE_EXPR
;
12397 code1
= TREE_CODE (t1
))
12398 t1
= TREE_OPERAND (t1
, 0);
12399 for (code2
= TREE_CODE (t2
);
12400 CONVERT_EXPR_CODE_P (code2
)
12401 || code2
== NON_LVALUE_EXPR
;
12402 code2
= TREE_CODE (t2
))
12403 t2
= TREE_OPERAND (t2
, 0);
12405 /* They might have become equal now. */
12409 if (code1
!= code2
)
12415 return wi::eq_p (t1
, t2
);
12418 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1
), TREE_REAL_CST (t2
));
12421 return TREE_STRING_LENGTH (t1
) == TREE_STRING_LENGTH (t2
)
12422 && !memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
12423 TREE_STRING_LENGTH (t1
));
12426 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1
),
12427 TREE_FIXED_CST (t2
));
12430 return c_tree_equal (TREE_REALPART (t1
), TREE_REALPART (t2
))
12431 && c_tree_equal (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
12434 return operand_equal_p (t1
, t2
, OEP_ONLY_CONST
);
12437 /* We need to do this when determining whether or not two
12438 non-type pointer to member function template arguments
12440 if (!comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))
12441 || CONSTRUCTOR_NELTS (t1
) != CONSTRUCTOR_NELTS (t2
))
12446 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, field
, value
)
12448 constructor_elt
*elt2
= CONSTRUCTOR_ELT (t2
, i
);
12449 if (!c_tree_equal (field
, elt2
->index
)
12450 || !c_tree_equal (value
, elt2
->value
))
12457 if (!c_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)))
12459 if (!c_tree_equal (TREE_VALUE (t1
), TREE_VALUE (t2
)))
12461 return c_tree_equal (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
12464 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
12469 call_expr_arg_iterator iter1
, iter2
;
12470 if (!c_tree_equal (CALL_EXPR_FN (t1
), CALL_EXPR_FN (t2
)))
12472 for (arg1
= first_call_expr_arg (t1
, &iter1
),
12473 arg2
= first_call_expr_arg (t2
, &iter2
);
12475 arg1
= next_call_expr_arg (&iter1
),
12476 arg2
= next_call_expr_arg (&iter2
))
12477 if (!c_tree_equal (arg1
, arg2
))
12486 tree o1
= TREE_OPERAND (t1
, 0);
12487 tree o2
= TREE_OPERAND (t2
, 0);
12489 /* Special case: if either target is an unallocated VAR_DECL,
12490 it means that it's going to be unified with whatever the
12491 TARGET_EXPR is really supposed to initialize, so treat it
12492 as being equivalent to anything. */
12493 if (TREE_CODE (o1
) == VAR_DECL
&& DECL_NAME (o1
) == NULL_TREE
12494 && !DECL_RTL_SET_P (o1
))
12496 else if (TREE_CODE (o2
) == VAR_DECL
&& DECL_NAME (o2
) == NULL_TREE
12497 && !DECL_RTL_SET_P (o2
))
12499 else if (!c_tree_equal (o1
, o2
))
12502 return c_tree_equal (TREE_OPERAND (t1
, 1), TREE_OPERAND (t2
, 1));
12505 case COMPONENT_REF
:
12506 if (TREE_OPERAND (t1
, 1) != TREE_OPERAND (t2
, 1))
12508 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
12514 case FUNCTION_DECL
:
12515 case IDENTIFIER_NODE
:
12522 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
12524 for (ix
= TREE_VEC_LENGTH (t1
); ix
--;)
12525 if (!c_tree_equal (TREE_VEC_ELT (t1
, ix
),
12526 TREE_VEC_ELT (t2
, ix
)))
12535 switch (TREE_CODE_CLASS (code1
))
12539 case tcc_comparison
:
12540 case tcc_expression
:
12542 case tcc_reference
:
12543 case tcc_statement
:
12545 int i
, n
= TREE_OPERAND_LENGTH (t1
);
12549 case PREINCREMENT_EXPR
:
12550 case PREDECREMENT_EXPR
:
12551 case POSTINCREMENT_EXPR
:
12552 case POSTDECREMENT_EXPR
:
12562 if (TREE_CODE_CLASS (code1
) == tcc_vl_exp
12563 && n
!= TREE_OPERAND_LENGTH (t2
))
12566 for (i
= 0; i
< n
; ++i
)
12567 if (!c_tree_equal (TREE_OPERAND (t1
, i
), TREE_OPERAND (t2
, i
)))
12574 return comptypes (t1
, t2
);
12576 gcc_unreachable ();
12578 /* We can get here with --disable-checking. */
12582 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12583 spawn-helper and BODY is the newly created body for FNDECL. */
12586 cilk_install_body_with_frame_cleanup (tree fndecl
, tree body
, void *w
)
12588 tree list
= alloc_stmt_list ();
12589 tree frame
= make_cilk_frame (fndecl
);
12590 tree dtor
= create_cilk_function_exit (frame
, false, true);
12591 add_local_decl (cfun
, frame
);
12593 DECL_SAVED_TREE (fndecl
) = list
;
12594 tree frame_ptr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (frame
)),
12596 tree body_list
= cilk_install_body_pedigree_operations (frame_ptr
);
12597 gcc_assert (TREE_CODE (body_list
) == STATEMENT_LIST
);
12599 tree detach_expr
= build_call_expr (cilk_detach_fndecl
, 1, frame_ptr
);
12600 append_to_statement_list (detach_expr
, &body_list
);
12602 cilk_outline (fndecl
, &body
, (struct wrapper_data
*) w
);
12603 body
= fold_build_cleanup_point_expr (void_type_node
, body
);
12605 append_to_statement_list (body
, &body_list
);
12606 append_to_statement_list (build_stmt (EXPR_LOCATION (body
), TRY_FINALLY_EXPR
,
12607 body_list
, dtor
), &list
);