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"
48 #include "hard-reg-set.h"
51 #include "gimple-expr.h"
53 #include "tree-inline.h"
55 #include "c-family/c-objc.h"
56 #include "c-family/c-common.h"
57 #include "c-family/c-ubsan.h"
61 /* Possible cases of implicit bad conversions. Used to select
62 diagnostic messages in convert_for_assignment. */
70 /* The level of nesting inside "__alignof__". */
73 /* The level of nesting inside "sizeof". */
76 /* The level of nesting inside "typeof". */
79 /* The argument of last parsed sizeof expression, only to be tested
80 if expr.original_code == SIZEOF_EXPR. */
81 tree c_last_sizeof_arg
;
83 /* Nonzero if we might need to print a "missing braces around
84 initializer" message within this initializer. */
85 static int found_missing_braces
;
87 static int require_constant_value
;
88 static int require_constant_elements
;
90 static bool null_pointer_constant_p (const_tree
);
91 static tree
qualify_type (tree
, tree
);
92 static int tagged_types_tu_compatible_p (const_tree
, const_tree
, bool *,
94 static int comp_target_types (location_t
, tree
, tree
);
95 static int function_types_compatible_p (const_tree
, const_tree
, bool *,
97 static int type_lists_compatible_p (const_tree
, const_tree
, bool *, bool *);
98 static tree
lookup_field (tree
, tree
);
99 static int convert_arguments (location_t
, vec
<location_t
>, tree
,
100 vec
<tree
, va_gc
> *, vec
<tree
, va_gc
> *, tree
,
102 static tree
pointer_diff (location_t
, tree
, tree
);
103 static tree
convert_for_assignment (location_t
, location_t
, tree
, tree
, tree
,
104 enum impl_conv
, bool, tree
, tree
, int);
105 static tree
valid_compound_expr_initializer (tree
, tree
);
106 static void push_string (const char *);
107 static void push_member_name (tree
);
108 static int spelling_length (void);
109 static char *print_spelling (char *);
110 static void warning_init (location_t
, int, const char *);
111 static tree
digest_init (location_t
, tree
, tree
, tree
, bool, bool, int);
112 static void output_init_element (location_t
, tree
, tree
, bool, tree
, tree
, int,
113 bool, struct obstack
*);
114 static void output_pending_init_elements (int, struct obstack
*);
115 static int set_designator (location_t
, int, struct obstack
*);
116 static void push_range_stack (tree
, struct obstack
*);
117 static void add_pending_init (location_t
, tree
, tree
, tree
, bool,
119 static void set_nonincremental_init (struct obstack
*);
120 static void set_nonincremental_init_from_string (tree
, struct obstack
*);
121 static tree
find_init_member (tree
, struct obstack
*);
122 static void readonly_warning (tree
, enum lvalue_use
);
123 static int lvalue_or_else (location_t
, const_tree
, enum lvalue_use
);
124 static void record_maybe_used_decl (tree
);
125 static int comptypes_internal (const_tree
, const_tree
, bool *, bool *);
127 /* Return true if EXP is a null pointer constant, false otherwise. */
130 null_pointer_constant_p (const_tree expr
)
132 /* This should really operate on c_expr structures, but they aren't
133 yet available everywhere required. */
134 tree type
= TREE_TYPE (expr
);
135 return (TREE_CODE (expr
) == INTEGER_CST
136 && !TREE_OVERFLOW (expr
)
137 && integer_zerop (expr
)
138 && (INTEGRAL_TYPE_P (type
)
139 || (TREE_CODE (type
) == POINTER_TYPE
140 && VOID_TYPE_P (TREE_TYPE (type
))
141 && TYPE_QUALS (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
)));
144 /* EXPR may appear in an unevaluated part of an integer constant
145 expression, but not in an evaluated part. Wrap it in a
146 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
147 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
150 note_integer_operands (tree expr
)
153 if (TREE_CODE (expr
) == INTEGER_CST
&& in_late_binary_op
)
155 ret
= copy_node (expr
);
156 TREE_OVERFLOW (ret
) = 1;
160 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL_TREE
, expr
);
161 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret
) = 1;
166 /* Having checked whether EXPR may appear in an unevaluated part of an
167 integer constant expression and found that it may, remove any
168 C_MAYBE_CONST_EXPR noting this fact and return the resulting
172 remove_c_maybe_const_expr (tree expr
)
174 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
175 return C_MAYBE_CONST_EXPR_EXPR (expr
);
180 \f/* This is a cache to hold if two types are compatible or not. */
182 struct tagged_tu_seen_cache
{
183 const struct tagged_tu_seen_cache
* next
;
186 /* The return value of tagged_types_tu_compatible_p if we had seen
187 these two types already. */
191 static const struct tagged_tu_seen_cache
* tagged_tu_seen_base
;
192 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*);
194 /* Do `exp = require_complete_type (exp);' to make sure exp
195 does not have an incomplete type. (That includes void types.) */
198 require_complete_type (tree value
)
200 tree type
= TREE_TYPE (value
);
202 if (error_operand_p (value
))
203 return error_mark_node
;
205 /* First, detect a valid value with a complete type. */
206 if (COMPLETE_TYPE_P (type
))
209 c_incomplete_type_error (value
, type
);
210 return error_mark_node
;
213 /* Print an error message for invalid use of an incomplete type.
214 VALUE is the expression that was used (or 0 if that isn't known)
215 and TYPE is the type that was invalid. */
218 c_incomplete_type_error (const_tree value
, const_tree type
)
220 const char *type_code_string
;
222 /* Avoid duplicate error message. */
223 if (TREE_CODE (type
) == ERROR_MARK
)
226 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
227 || TREE_CODE (value
) == PARM_DECL
))
228 error ("%qD has an incomplete type", value
);
232 /* We must print an error message. Be clever about what it says. */
234 switch (TREE_CODE (type
))
237 type_code_string
= "struct";
241 type_code_string
= "union";
245 type_code_string
= "enum";
249 error ("invalid use of void expression");
253 if (TYPE_DOMAIN (type
))
255 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
257 error ("invalid use of flexible array member");
260 type
= TREE_TYPE (type
);
263 error ("invalid use of array with unspecified bounds");
270 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
271 error ("invalid use of undefined type %<%s %E%>",
272 type_code_string
, TYPE_NAME (type
));
274 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
275 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type
));
279 /* Given a type, apply default promotions wrt unnamed function
280 arguments and return the new type. */
283 c_type_promotes_to (tree type
)
285 tree ret
= NULL_TREE
;
287 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
288 ret
= double_type_node
;
289 else if (c_promoting_integer_type_p (type
))
291 /* Preserve unsignedness if not really getting any wider. */
292 if (TYPE_UNSIGNED (type
)
293 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
294 ret
= unsigned_type_node
;
296 ret
= integer_type_node
;
299 if (ret
!= NULL_TREE
)
300 return (TYPE_ATOMIC (type
)
301 ? c_build_qualified_type (ret
, TYPE_QUAL_ATOMIC
)
307 /* Return true if between two named address spaces, whether there is a superset
308 named address space that encompasses both address spaces. If there is a
309 superset, return which address space is the superset. */
312 addr_space_superset (addr_space_t as1
, addr_space_t as2
, addr_space_t
*common
)
319 else if (targetm
.addr_space
.subset_p (as1
, as2
))
324 else if (targetm
.addr_space
.subset_p (as2
, as1
))
333 /* Return a variant of TYPE which has all the type qualifiers of LIKE
334 as well as those of TYPE. */
337 qualify_type (tree type
, tree like
)
339 addr_space_t as_type
= TYPE_ADDR_SPACE (type
);
340 addr_space_t as_like
= TYPE_ADDR_SPACE (like
);
341 addr_space_t as_common
;
343 /* If the two named address spaces are different, determine the common
344 superset address space. If there isn't one, raise an error. */
345 if (!addr_space_superset (as_type
, as_like
, &as_common
))
348 error ("%qT and %qT are in disjoint named address spaces",
352 return c_build_qualified_type (type
,
353 TYPE_QUALS_NO_ADDR_SPACE (type
)
354 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like
)
355 | ENCODE_QUAL_ADDR_SPACE (as_common
));
358 /* Return true iff the given tree T is a variable length array. */
361 c_vla_type_p (const_tree t
)
363 if (TREE_CODE (t
) == ARRAY_TYPE
364 && C_TYPE_VARIABLE_SIZE (t
))
369 /* Return the composite type of two compatible types.
371 We assume that comptypes has already been done and returned
372 nonzero; if that isn't so, this may crash. In particular, we
373 assume that qualifiers match. */
376 composite_type (tree t1
, tree t2
)
378 enum tree_code code1
;
379 enum tree_code code2
;
382 /* Save time if the two types are the same. */
384 if (t1
== t2
) return t1
;
386 /* If one type is nonsense, use the other. */
387 if (t1
== error_mark_node
)
389 if (t2
== error_mark_node
)
392 code1
= TREE_CODE (t1
);
393 code2
= TREE_CODE (t2
);
395 /* Merge the attributes. */
396 attributes
= targetm
.merge_type_attributes (t1
, t2
);
398 /* If one is an enumerated type and the other is the compatible
399 integer type, the composite type might be either of the two
400 (DR#013 question 3). For consistency, use the enumerated type as
401 the composite type. */
403 if (code1
== ENUMERAL_TYPE
&& code2
== INTEGER_TYPE
)
405 if (code2
== ENUMERAL_TYPE
&& code1
== INTEGER_TYPE
)
408 gcc_assert (code1
== code2
);
413 /* For two pointers, do this recursively on the target type. */
415 tree pointed_to_1
= TREE_TYPE (t1
);
416 tree pointed_to_2
= TREE_TYPE (t2
);
417 tree target
= composite_type (pointed_to_1
, pointed_to_2
);
418 t1
= build_pointer_type_for_mode (target
, TYPE_MODE (t1
), false);
419 t1
= build_type_attribute_variant (t1
, attributes
);
420 return qualify_type (t1
, t2
);
425 tree elt
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
428 tree d1
= TYPE_DOMAIN (t1
);
429 tree d2
= TYPE_DOMAIN (t2
);
430 bool d1_variable
, d2_variable
;
431 bool d1_zero
, d2_zero
;
432 bool t1_complete
, t2_complete
;
434 /* We should not have any type quals on arrays at all. */
435 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1
)
436 && !TYPE_QUALS_NO_ADDR_SPACE (t2
));
438 t1_complete
= COMPLETE_TYPE_P (t1
);
439 t2_complete
= COMPLETE_TYPE_P (t2
);
441 d1_zero
= d1
== 0 || !TYPE_MAX_VALUE (d1
);
442 d2_zero
= d2
== 0 || !TYPE_MAX_VALUE (d2
);
444 d1_variable
= (!d1_zero
445 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
446 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
447 d2_variable
= (!d2_zero
448 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
449 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
450 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
451 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
453 /* Save space: see if the result is identical to one of the args. */
454 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
)
455 && (d2_variable
|| d2_zero
|| !d1_variable
))
456 return build_type_attribute_variant (t1
, attributes
);
457 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
)
458 && (d1_variable
|| d1_zero
|| !d2_variable
))
459 return build_type_attribute_variant (t2
, attributes
);
461 if (elt
== TREE_TYPE (t1
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
462 return build_type_attribute_variant (t1
, attributes
);
463 if (elt
== TREE_TYPE (t2
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
464 return build_type_attribute_variant (t2
, attributes
);
466 /* Merge the element types, and have a size if either arg has
467 one. We may have qualifiers on the element types. To set
468 up TYPE_MAIN_VARIANT correctly, we need to form the
469 composite of the unqualified types and add the qualifiers
471 quals
= TYPE_QUALS (strip_array_types (elt
));
472 unqual_elt
= c_build_qualified_type (elt
, TYPE_UNQUALIFIED
);
473 t1
= build_array_type (unqual_elt
,
474 TYPE_DOMAIN ((TYPE_DOMAIN (t1
)
480 /* Ensure a composite type involving a zero-length array type
481 is a zero-length type not an incomplete type. */
482 if (d1_zero
&& d2_zero
483 && (t1_complete
|| t2_complete
)
484 && !COMPLETE_TYPE_P (t1
))
486 TYPE_SIZE (t1
) = bitsize_zero_node
;
487 TYPE_SIZE_UNIT (t1
) = size_zero_node
;
489 t1
= c_build_qualified_type (t1
, quals
);
490 return build_type_attribute_variant (t1
, attributes
);
496 if (attributes
!= NULL
)
498 /* Try harder not to create a new aggregate type. */
499 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
501 if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
504 return build_type_attribute_variant (t1
, attributes
);
507 /* Function types: prefer the one that specified arg types.
508 If both do, merge the arg types. Also merge the return types. */
510 tree valtype
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
511 tree p1
= TYPE_ARG_TYPES (t1
);
512 tree p2
= TYPE_ARG_TYPES (t2
);
517 /* Save space: see if the result is identical to one of the args. */
518 if (valtype
== TREE_TYPE (t1
) && !TYPE_ARG_TYPES (t2
))
519 return build_type_attribute_variant (t1
, attributes
);
520 if (valtype
== TREE_TYPE (t2
) && !TYPE_ARG_TYPES (t1
))
521 return build_type_attribute_variant (t2
, attributes
);
523 /* Simple way if one arg fails to specify argument types. */
524 if (TYPE_ARG_TYPES (t1
) == 0)
526 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
527 t1
= build_type_attribute_variant (t1
, attributes
);
528 return qualify_type (t1
, t2
);
530 if (TYPE_ARG_TYPES (t2
) == 0)
532 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
533 t1
= build_type_attribute_variant (t1
, attributes
);
534 return qualify_type (t1
, t2
);
537 /* If both args specify argument types, we must merge the two
538 lists, argument by argument. */
540 len
= list_length (p1
);
543 for (i
= 0; i
< len
; i
++)
544 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
549 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
551 /* A null type means arg type is not specified.
552 Take whatever the other function type has. */
553 if (TREE_VALUE (p1
) == 0)
555 TREE_VALUE (n
) = TREE_VALUE (p2
);
558 if (TREE_VALUE (p2
) == 0)
560 TREE_VALUE (n
) = TREE_VALUE (p1
);
564 /* Given wait (union {union wait *u; int *i} *)
565 and wait (union wait *),
566 prefer union wait * as type of parm. */
567 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
568 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
571 tree mv2
= TREE_VALUE (p2
);
572 if (mv2
&& mv2
!= error_mark_node
573 && TREE_CODE (mv2
) != ARRAY_TYPE
)
574 mv2
= TYPE_MAIN_VARIANT (mv2
);
575 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
576 memb
; memb
= DECL_CHAIN (memb
))
578 tree mv3
= TREE_TYPE (memb
);
579 if (mv3
&& mv3
!= error_mark_node
580 && TREE_CODE (mv3
) != ARRAY_TYPE
)
581 mv3
= TYPE_MAIN_VARIANT (mv3
);
582 if (comptypes (mv3
, mv2
))
584 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
586 pedwarn (input_location
, OPT_Wpedantic
,
587 "function types not truly compatible in ISO C");
592 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
593 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
596 tree mv1
= TREE_VALUE (p1
);
597 if (mv1
&& mv1
!= error_mark_node
598 && TREE_CODE (mv1
) != ARRAY_TYPE
)
599 mv1
= TYPE_MAIN_VARIANT (mv1
);
600 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
601 memb
; memb
= DECL_CHAIN (memb
))
603 tree mv3
= TREE_TYPE (memb
);
604 if (mv3
&& mv3
!= error_mark_node
605 && TREE_CODE (mv3
) != ARRAY_TYPE
)
606 mv3
= TYPE_MAIN_VARIANT (mv3
);
607 if (comptypes (mv3
, mv1
))
609 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
611 pedwarn (input_location
, OPT_Wpedantic
,
612 "function types not truly compatible in ISO C");
617 TREE_VALUE (n
) = composite_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
621 t1
= build_function_type (valtype
, newargs
);
622 t1
= qualify_type (t1
, t2
);
623 /* ... falls through ... */
627 return build_type_attribute_variant (t1
, attributes
);
632 /* Return the type of a conditional expression between pointers to
633 possibly differently qualified versions of compatible types.
635 We assume that comp_target_types has already been done and returned
636 nonzero; if that isn't so, this may crash. */
639 common_pointer_type (tree t1
, tree t2
)
642 tree pointed_to_1
, mv1
;
643 tree pointed_to_2
, mv2
;
645 unsigned target_quals
;
646 addr_space_t as1
, as2
, as_common
;
649 /* Save time if the two types are the same. */
651 if (t1
== t2
) return t1
;
653 /* If one type is nonsense, use the other. */
654 if (t1
== error_mark_node
)
656 if (t2
== error_mark_node
)
659 gcc_assert (TREE_CODE (t1
) == POINTER_TYPE
660 && TREE_CODE (t2
) == POINTER_TYPE
);
662 /* Merge the attributes. */
663 attributes
= targetm
.merge_type_attributes (t1
, t2
);
665 /* Find the composite type of the target types, and combine the
666 qualifiers of the two types' targets. Do not lose qualifiers on
667 array element types by taking the TYPE_MAIN_VARIANT. */
668 mv1
= pointed_to_1
= TREE_TYPE (t1
);
669 mv2
= pointed_to_2
= TREE_TYPE (t2
);
670 if (TREE_CODE (mv1
) != ARRAY_TYPE
)
671 mv1
= TYPE_MAIN_VARIANT (pointed_to_1
);
672 if (TREE_CODE (mv2
) != ARRAY_TYPE
)
673 mv2
= TYPE_MAIN_VARIANT (pointed_to_2
);
674 target
= composite_type (mv1
, mv2
);
676 /* For function types do not merge const qualifiers, but drop them
677 if used inconsistently. The middle-end uses these to mark const
678 and noreturn functions. */
679 quals1
= TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1
);
680 quals2
= TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2
);
682 if (TREE_CODE (pointed_to_1
) == FUNCTION_TYPE
)
683 target_quals
= (quals1
& quals2
);
685 target_quals
= (quals1
| quals2
);
687 /* If the two named address spaces are different, determine the common
688 superset address space. This is guaranteed to exist due to the
689 assumption that comp_target_type returned non-zero. */
690 as1
= TYPE_ADDR_SPACE (pointed_to_1
);
691 as2
= TYPE_ADDR_SPACE (pointed_to_2
);
692 if (!addr_space_superset (as1
, as2
, &as_common
))
695 target_quals
|= ENCODE_QUAL_ADDR_SPACE (as_common
);
697 t1
= build_pointer_type (c_build_qualified_type (target
, target_quals
));
698 return build_type_attribute_variant (t1
, attributes
);
701 /* Return the common type for two arithmetic types under the usual
702 arithmetic conversions. The default conversions have already been
703 applied, and enumerated types converted to their compatible integer
704 types. The resulting type is unqualified and has no attributes.
706 This is the type for the result of most arithmetic operations
707 if the operands have the given two types. */
710 c_common_type (tree t1
, tree t2
)
712 enum tree_code code1
;
713 enum tree_code code2
;
715 /* If one type is nonsense, use the other. */
716 if (t1
== error_mark_node
)
718 if (t2
== error_mark_node
)
721 if (TYPE_QUALS (t1
) != TYPE_UNQUALIFIED
)
722 t1
= TYPE_MAIN_VARIANT (t1
);
724 if (TYPE_QUALS (t2
) != TYPE_UNQUALIFIED
)
725 t2
= TYPE_MAIN_VARIANT (t2
);
727 if (TYPE_ATTRIBUTES (t1
) != NULL_TREE
)
728 t1
= build_type_attribute_variant (t1
, NULL_TREE
);
730 if (TYPE_ATTRIBUTES (t2
) != NULL_TREE
)
731 t2
= build_type_attribute_variant (t2
, NULL_TREE
);
733 /* Save time if the two types are the same. */
735 if (t1
== t2
) return t1
;
737 code1
= TREE_CODE (t1
);
738 code2
= TREE_CODE (t2
);
740 gcc_assert (code1
== VECTOR_TYPE
|| code1
== COMPLEX_TYPE
741 || code1
== FIXED_POINT_TYPE
|| code1
== REAL_TYPE
742 || code1
== INTEGER_TYPE
);
743 gcc_assert (code2
== VECTOR_TYPE
|| code2
== COMPLEX_TYPE
744 || code2
== FIXED_POINT_TYPE
|| code2
== REAL_TYPE
745 || code2
== INTEGER_TYPE
);
747 /* When one operand is a decimal float type, the other operand cannot be
748 a generic float type or a complex type. We also disallow vector types
750 if ((DECIMAL_FLOAT_TYPE_P (t1
) || DECIMAL_FLOAT_TYPE_P (t2
))
751 && !(DECIMAL_FLOAT_TYPE_P (t1
) && DECIMAL_FLOAT_TYPE_P (t2
)))
753 if (code1
== VECTOR_TYPE
|| code2
== VECTOR_TYPE
)
755 error ("can%'t mix operands of decimal float and vector types");
756 return error_mark_node
;
758 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
760 error ("can%'t mix operands of decimal float and complex types");
761 return error_mark_node
;
763 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
765 error ("can%'t mix operands of decimal float and other float types");
766 return error_mark_node
;
770 /* If one type is a vector type, return that type. (How the usual
771 arithmetic conversions apply to the vector types extension is not
772 precisely specified.) */
773 if (code1
== VECTOR_TYPE
)
776 if (code2
== VECTOR_TYPE
)
779 /* If one type is complex, form the common type of the non-complex
780 components, then make that complex. Use T1 or T2 if it is the
782 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
784 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
785 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
786 tree subtype
= c_common_type (subtype1
, subtype2
);
788 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
790 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
793 return build_complex_type (subtype
);
796 /* If only one is real, use it as the result. */
798 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
801 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
804 /* If both are real and either are decimal floating point types, use
805 the decimal floating point type with the greater precision. */
807 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
809 if (TYPE_MAIN_VARIANT (t1
) == dfloat128_type_node
810 || TYPE_MAIN_VARIANT (t2
) == dfloat128_type_node
)
811 return dfloat128_type_node
;
812 else if (TYPE_MAIN_VARIANT (t1
) == dfloat64_type_node
813 || TYPE_MAIN_VARIANT (t2
) == dfloat64_type_node
)
814 return dfloat64_type_node
;
815 else if (TYPE_MAIN_VARIANT (t1
) == dfloat32_type_node
816 || TYPE_MAIN_VARIANT (t2
) == dfloat32_type_node
)
817 return dfloat32_type_node
;
820 /* Deal with fixed-point types. */
821 if (code1
== FIXED_POINT_TYPE
|| code2
== FIXED_POINT_TYPE
)
823 unsigned int unsignedp
= 0, satp
= 0;
825 unsigned int fbit1
, ibit1
, fbit2
, ibit2
, max_fbit
, max_ibit
;
830 /* If one input type is saturating, the result type is saturating. */
831 if (TYPE_SATURATING (t1
) || TYPE_SATURATING (t2
))
834 /* If both fixed-point types are unsigned, the result type is unsigned.
835 When mixing fixed-point and integer types, follow the sign of the
837 Otherwise, the result type is signed. */
838 if ((TYPE_UNSIGNED (t1
) && TYPE_UNSIGNED (t2
)
839 && code1
== FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
)
840 || (code1
== FIXED_POINT_TYPE
&& code2
!= FIXED_POINT_TYPE
841 && TYPE_UNSIGNED (t1
))
842 || (code1
!= FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
843 && TYPE_UNSIGNED (t2
)))
846 /* The result type is signed. */
849 /* If the input type is unsigned, we need to convert to the
851 if (code1
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t1
))
853 enum mode_class mclass
= (enum mode_class
) 0;
854 if (GET_MODE_CLASS (m1
) == MODE_UFRACT
)
856 else if (GET_MODE_CLASS (m1
) == MODE_UACCUM
)
860 m1
= mode_for_size (GET_MODE_PRECISION (m1
), mclass
, 0);
862 if (code2
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t2
))
864 enum mode_class mclass
= (enum mode_class
) 0;
865 if (GET_MODE_CLASS (m2
) == MODE_UFRACT
)
867 else if (GET_MODE_CLASS (m2
) == MODE_UACCUM
)
871 m2
= mode_for_size (GET_MODE_PRECISION (m2
), mclass
, 0);
875 if (code1
== FIXED_POINT_TYPE
)
877 fbit1
= GET_MODE_FBIT (m1
);
878 ibit1
= GET_MODE_IBIT (m1
);
883 /* Signed integers need to subtract one sign bit. */
884 ibit1
= TYPE_PRECISION (t1
) - (!TYPE_UNSIGNED (t1
));
887 if (code2
== FIXED_POINT_TYPE
)
889 fbit2
= GET_MODE_FBIT (m2
);
890 ibit2
= GET_MODE_IBIT (m2
);
895 /* Signed integers need to subtract one sign bit. */
896 ibit2
= TYPE_PRECISION (t2
) - (!TYPE_UNSIGNED (t2
));
899 max_ibit
= ibit1
>= ibit2
? ibit1
: ibit2
;
900 max_fbit
= fbit1
>= fbit2
? fbit1
: fbit2
;
901 return c_common_fixed_point_type_for_size (max_ibit
, max_fbit
, unsignedp
,
905 /* Both real or both integers; use the one with greater precision. */
907 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
909 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
912 /* Same precision. Prefer long longs to longs to ints when the
913 same precision, following the C99 rules on integer type rank
914 (which are equivalent to the C90 rules for C90 types). */
916 if (TYPE_MAIN_VARIANT (t1
) == long_long_unsigned_type_node
917 || TYPE_MAIN_VARIANT (t2
) == long_long_unsigned_type_node
)
918 return long_long_unsigned_type_node
;
920 if (TYPE_MAIN_VARIANT (t1
) == long_long_integer_type_node
921 || TYPE_MAIN_VARIANT (t2
) == long_long_integer_type_node
)
923 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
924 return long_long_unsigned_type_node
;
926 return long_long_integer_type_node
;
929 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
930 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
931 return long_unsigned_type_node
;
933 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
934 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
936 /* But preserve unsignedness from the other type,
937 since long cannot hold all the values of an unsigned int. */
938 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
939 return long_unsigned_type_node
;
941 return long_integer_type_node
;
944 /* Likewise, prefer long double to double even if same size. */
945 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
946 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
947 return long_double_type_node
;
949 /* Likewise, prefer double to float even if same size.
950 We got a couple of embedded targets with 32 bit doubles, and the
951 pdp11 might have 64 bit floats. */
952 if (TYPE_MAIN_VARIANT (t1
) == double_type_node
953 || TYPE_MAIN_VARIANT (t2
) == double_type_node
)
954 return double_type_node
;
956 /* Otherwise prefer the unsigned one. */
958 if (TYPE_UNSIGNED (t1
))
964 /* Wrapper around c_common_type that is used by c-common.c and other
965 front end optimizations that remove promotions. ENUMERAL_TYPEs
966 are allowed here and are converted to their compatible integer types.
967 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
968 preferably a non-Boolean type as the common type. */
970 common_type (tree t1
, tree t2
)
972 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
973 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
974 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
975 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
977 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
978 if (TREE_CODE (t1
) == BOOLEAN_TYPE
979 && TREE_CODE (t2
) == BOOLEAN_TYPE
)
980 return boolean_type_node
;
982 /* If either type is BOOLEAN_TYPE, then return the other. */
983 if (TREE_CODE (t1
) == BOOLEAN_TYPE
)
985 if (TREE_CODE (t2
) == BOOLEAN_TYPE
)
988 return c_common_type (t1
, t2
);
991 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
992 or various other operations. Return 2 if they are compatible
993 but a warning may be needed if you use them together. */
996 comptypes (tree type1
, tree type2
)
998 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1001 val
= comptypes_internal (type1
, type2
, NULL
, NULL
);
1002 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1007 /* Like comptypes, but if it returns non-zero because enum and int are
1008 compatible, it sets *ENUM_AND_INT_P to true. */
1011 comptypes_check_enum_int (tree type1
, tree type2
, bool *enum_and_int_p
)
1013 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1016 val
= comptypes_internal (type1
, type2
, enum_and_int_p
, NULL
);
1017 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1022 /* Like comptypes, but if it returns nonzero for different types, it
1023 sets *DIFFERENT_TYPES_P to true. */
1026 comptypes_check_different_types (tree type1
, tree type2
,
1027 bool *different_types_p
)
1029 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1032 val
= comptypes_internal (type1
, type2
, NULL
, different_types_p
);
1033 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1038 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1039 or various other operations. Return 2 if they are compatible
1040 but a warning may be needed if you use them together. If
1041 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1042 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1043 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1044 NULL, and the types are compatible but different enough not to be
1045 permitted in C11 typedef redeclarations, then this sets
1046 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1047 false, but may or may not be set if the types are incompatible.
1048 This differs from comptypes, in that we don't free the seen
1052 comptypes_internal (const_tree type1
, const_tree type2
, bool *enum_and_int_p
,
1053 bool *different_types_p
)
1055 const_tree t1
= type1
;
1056 const_tree t2
= type2
;
1059 /* Suppress errors caused by previously reported errors. */
1061 if (t1
== t2
|| !t1
|| !t2
1062 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
1065 /* Enumerated types are compatible with integer types, but this is
1066 not transitive: two enumerated types in the same translation unit
1067 are compatible with each other only if they are the same type. */
1069 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
1071 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TYPE_UNSIGNED (t1
));
1072 if (TREE_CODE (t2
) != VOID_TYPE
)
1074 if (enum_and_int_p
!= NULL
)
1075 *enum_and_int_p
= true;
1076 if (different_types_p
!= NULL
)
1077 *different_types_p
= true;
1080 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
1082 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TYPE_UNSIGNED (t2
));
1083 if (TREE_CODE (t1
) != VOID_TYPE
)
1085 if (enum_and_int_p
!= NULL
)
1086 *enum_and_int_p
= true;
1087 if (different_types_p
!= NULL
)
1088 *different_types_p
= true;
1095 /* Different classes of types can't be compatible. */
1097 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1100 /* Qualifiers must match. C99 6.7.3p9 */
1102 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1105 /* Allow for two different type nodes which have essentially the same
1106 definition. Note that we already checked for equality of the type
1107 qualifiers (just above). */
1109 if (TREE_CODE (t1
) != ARRAY_TYPE
1110 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1113 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1114 if (!(attrval
= comp_type_attributes (t1
, t2
)))
1117 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1120 switch (TREE_CODE (t1
))
1123 /* Do not remove mode or aliasing information. */
1124 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1125 || TYPE_REF_CAN_ALIAS_ALL (t1
) != TYPE_REF_CAN_ALIAS_ALL (t2
))
1127 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
1128 ? 1 : comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1129 enum_and_int_p
, different_types_p
));
1133 val
= function_types_compatible_p (t1
, t2
, enum_and_int_p
,
1139 tree d1
= TYPE_DOMAIN (t1
);
1140 tree d2
= TYPE_DOMAIN (t2
);
1141 bool d1_variable
, d2_variable
;
1142 bool d1_zero
, d2_zero
;
1145 /* Target types must match incl. qualifiers. */
1146 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
1147 && 0 == (val
= comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1149 different_types_p
)))
1152 if (different_types_p
!= NULL
1153 && (d1
== 0) != (d2
== 0))
1154 *different_types_p
= true;
1155 /* Sizes must match unless one is missing or variable. */
1156 if (d1
== 0 || d2
== 0 || d1
== d2
)
1159 d1_zero
= !TYPE_MAX_VALUE (d1
);
1160 d2_zero
= !TYPE_MAX_VALUE (d2
);
1162 d1_variable
= (!d1_zero
1163 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
1164 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
1165 d2_variable
= (!d2_zero
1166 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
1167 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
1168 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
1169 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
1171 if (different_types_p
!= NULL
1172 && d1_variable
!= d2_variable
)
1173 *different_types_p
= true;
1174 if (d1_variable
|| d2_variable
)
1176 if (d1_zero
&& d2_zero
)
1178 if (d1_zero
|| d2_zero
1179 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
1180 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
1189 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
1191 tree a1
= TYPE_ATTRIBUTES (t1
);
1192 tree a2
= TYPE_ATTRIBUTES (t2
);
1194 if (! attribute_list_contained (a1
, a2
)
1195 && ! attribute_list_contained (a2
, a1
))
1199 return tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1201 val
= tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1207 val
= (TYPE_VECTOR_SUBPARTS (t1
) == TYPE_VECTOR_SUBPARTS (t2
)
1208 && comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1209 enum_and_int_p
, different_types_p
));
1215 return attrval
== 2 && val
== 1 ? 2 : val
;
1218 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1219 their qualifiers, except for named address spaces. If the pointers point to
1220 different named addresses, then we must determine if one address space is a
1221 subset of the other. */
1224 comp_target_types (location_t location
, tree ttl
, tree ttr
)
1227 tree mvl
= TREE_TYPE (ttl
);
1228 tree mvr
= TREE_TYPE (ttr
);
1229 addr_space_t asl
= TYPE_ADDR_SPACE (mvl
);
1230 addr_space_t asr
= TYPE_ADDR_SPACE (mvr
);
1231 addr_space_t as_common
;
1232 bool enum_and_int_p
;
1234 /* Fail if pointers point to incompatible address spaces. */
1235 if (!addr_space_superset (asl
, asr
, &as_common
))
1238 /* Do not lose qualifiers on element types of array types that are
1239 pointer targets by taking their TYPE_MAIN_VARIANT. */
1240 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
1241 mvl
= (TYPE_ATOMIC (mvl
)
1242 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
), TYPE_QUAL_ATOMIC
)
1243 : TYPE_MAIN_VARIANT (mvl
));
1244 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
1245 mvr
= (TYPE_ATOMIC (mvr
)
1246 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
), TYPE_QUAL_ATOMIC
)
1247 : TYPE_MAIN_VARIANT (mvr
));
1248 enum_and_int_p
= false;
1249 val
= comptypes_check_enum_int (mvl
, mvr
, &enum_and_int_p
);
1252 pedwarn (location
, OPT_Wpedantic
, "types are not quite compatible");
1254 if (val
== 1 && enum_and_int_p
&& warn_cxx_compat
)
1255 warning_at (location
, OPT_Wc___compat
,
1256 "pointer target types incompatible in C++");
1261 /* Subroutines of `comptypes'. */
1263 /* Determine whether two trees derive from the same translation unit.
1264 If the CONTEXT chain ends in a null, that tree's context is still
1265 being parsed, so if two trees have context chains ending in null,
1266 they're in the same translation unit. */
1268 same_translation_unit_p (const_tree t1
, const_tree t2
)
1270 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
1271 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
1273 case tcc_declaration
:
1274 t1
= DECL_CONTEXT (t1
); break;
1276 t1
= TYPE_CONTEXT (t1
); break;
1277 case tcc_exceptional
:
1278 t1
= BLOCK_SUPERCONTEXT (t1
); break; /* assume block */
1279 default: gcc_unreachable ();
1282 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
1283 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
1285 case tcc_declaration
:
1286 t2
= DECL_CONTEXT (t2
); break;
1288 t2
= TYPE_CONTEXT (t2
); break;
1289 case tcc_exceptional
:
1290 t2
= BLOCK_SUPERCONTEXT (t2
); break; /* assume block */
1291 default: gcc_unreachable ();
1297 /* Allocate the seen two types, assuming that they are compatible. */
1299 static struct tagged_tu_seen_cache
*
1300 alloc_tagged_tu_seen_cache (const_tree t1
, const_tree t2
)
1302 struct tagged_tu_seen_cache
*tu
= XNEW (struct tagged_tu_seen_cache
);
1303 tu
->next
= tagged_tu_seen_base
;
1307 tagged_tu_seen_base
= tu
;
1309 /* The C standard says that two structures in different translation
1310 units are compatible with each other only if the types of their
1311 fields are compatible (among other things). We assume that they
1312 are compatible until proven otherwise when building the cache.
1313 An example where this can occur is:
1318 If we are comparing this against a similar struct in another TU,
1319 and did not assume they were compatible, we end up with an infinite
1325 /* Free the seen types until we get to TU_TIL. */
1328 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*tu_til
)
1330 const struct tagged_tu_seen_cache
*tu
= tagged_tu_seen_base
;
1331 while (tu
!= tu_til
)
1333 const struct tagged_tu_seen_cache
*const tu1
1334 = (const struct tagged_tu_seen_cache
*) tu
;
1336 free (CONST_CAST (struct tagged_tu_seen_cache
*, tu1
));
1338 tagged_tu_seen_base
= tu_til
;
1341 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1342 compatible. If the two types are not the same (which has been
1343 checked earlier), this can only happen when multiple translation
1344 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1345 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1346 comptypes_internal. */
1349 tagged_types_tu_compatible_p (const_tree t1
, const_tree t2
,
1350 bool *enum_and_int_p
, bool *different_types_p
)
1353 bool needs_warning
= false;
1355 /* We have to verify that the tags of the types are the same. This
1356 is harder than it looks because this may be a typedef, so we have
1357 to go look at the original type. It may even be a typedef of a
1359 In the case of compiler-created builtin structs the TYPE_DECL
1360 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1361 while (TYPE_NAME (t1
)
1362 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1363 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
1364 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
1366 while (TYPE_NAME (t2
)
1367 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1368 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
1369 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
1371 /* C90 didn't have the requirement that the two tags be the same. */
1372 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
1375 /* C90 didn't say what happened if one or both of the types were
1376 incomplete; we choose to follow C99 rules here, which is that they
1378 if (TYPE_SIZE (t1
) == NULL
1379 || TYPE_SIZE (t2
) == NULL
)
1383 const struct tagged_tu_seen_cache
* tts_i
;
1384 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
1385 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
1389 switch (TREE_CODE (t1
))
1393 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1394 /* Speed up the case where the type values are in the same order. */
1395 tree tv1
= TYPE_VALUES (t1
);
1396 tree tv2
= TYPE_VALUES (t2
);
1403 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
1405 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
1407 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
1414 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
1418 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
1424 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
1430 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
1432 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
1434 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
1445 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1446 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
1452 /* Speed up the common case where the fields are in the same order. */
1453 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
); s1
&& s2
;
1454 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1458 if (DECL_NAME (s1
) != DECL_NAME (s2
))
1460 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1461 enum_and_int_p
, different_types_p
);
1463 if (result
!= 1 && !DECL_NAME (s1
))
1471 needs_warning
= true;
1473 if (TREE_CODE (s1
) == FIELD_DECL
1474 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1475 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1483 tu
->val
= needs_warning
? 2 : 1;
1487 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= DECL_CHAIN (s1
))
1491 for (s2
= TYPE_FIELDS (t2
); s2
; s2
= DECL_CHAIN (s2
))
1492 if (DECL_NAME (s1
) == DECL_NAME (s2
))
1496 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1500 if (result
!= 1 && !DECL_NAME (s1
))
1508 needs_warning
= true;
1510 if (TREE_CODE (s1
) == FIELD_DECL
1511 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1512 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1524 tu
->val
= needs_warning
? 2 : 10;
1530 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1532 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
1534 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1537 if (TREE_CODE (s1
) != TREE_CODE (s2
)
1538 || DECL_NAME (s1
) != DECL_NAME (s2
))
1540 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1541 enum_and_int_p
, different_types_p
);
1545 needs_warning
= true;
1547 if (TREE_CODE (s1
) == FIELD_DECL
1548 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1549 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1555 tu
->val
= needs_warning
? 2 : 1;
1564 /* Return 1 if two function types F1 and F2 are compatible.
1565 If either type specifies no argument types,
1566 the other must specify a fixed number of self-promoting arg types.
1567 Otherwise, if one type specifies only the number of arguments,
1568 the other must specify that number of self-promoting arg types.
1569 Otherwise, the argument types must match.
1570 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1573 function_types_compatible_p (const_tree f1
, const_tree f2
,
1574 bool *enum_and_int_p
, bool *different_types_p
)
1577 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1582 ret1
= TREE_TYPE (f1
);
1583 ret2
= TREE_TYPE (f2
);
1585 /* 'volatile' qualifiers on a function's return type used to mean
1586 the function is noreturn. */
1587 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
1588 pedwarn (input_location
, 0, "function return types not compatible due to %<volatile%>");
1589 if (TYPE_VOLATILE (ret1
))
1590 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
1591 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
1592 if (TYPE_VOLATILE (ret2
))
1593 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
1594 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
1595 val
= comptypes_internal (ret1
, ret2
, enum_and_int_p
, different_types_p
);
1599 args1
= TYPE_ARG_TYPES (f1
);
1600 args2
= TYPE_ARG_TYPES (f2
);
1602 if (different_types_p
!= NULL
1603 && (args1
== 0) != (args2
== 0))
1604 *different_types_p
= true;
1606 /* An unspecified parmlist matches any specified parmlist
1607 whose argument types don't need default promotions. */
1611 if (!self_promoting_args_p (args2
))
1613 /* If one of these types comes from a non-prototype fn definition,
1614 compare that with the other type's arglist.
1615 If they don't match, ask for a warning (but no error). */
1616 if (TYPE_ACTUAL_ARG_TYPES (f1
)
1617 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
1618 enum_and_int_p
, different_types_p
))
1624 if (!self_promoting_args_p (args1
))
1626 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1627 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
1628 enum_and_int_p
, different_types_p
))
1633 /* Both types have argument lists: compare them and propagate results. */
1634 val1
= type_lists_compatible_p (args1
, args2
, enum_and_int_p
,
1636 return val1
!= 1 ? val1
: val
;
1639 /* Check two lists of types for compatibility, returning 0 for
1640 incompatible, 1 for compatible, or 2 for compatible with
1641 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1642 comptypes_internal. */
1645 type_lists_compatible_p (const_tree args1
, const_tree args2
,
1646 bool *enum_and_int_p
, bool *different_types_p
)
1648 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1654 tree a1
, mv1
, a2
, mv2
;
1655 if (args1
== 0 && args2
== 0)
1657 /* If one list is shorter than the other,
1658 they fail to match. */
1659 if (args1
== 0 || args2
== 0)
1661 mv1
= a1
= TREE_VALUE (args1
);
1662 mv2
= a2
= TREE_VALUE (args2
);
1663 if (mv1
&& mv1
!= error_mark_node
&& TREE_CODE (mv1
) != ARRAY_TYPE
)
1664 mv1
= (TYPE_ATOMIC (mv1
)
1665 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1
),
1667 : TYPE_MAIN_VARIANT (mv1
));
1668 if (mv2
&& mv2
!= error_mark_node
&& TREE_CODE (mv2
) != ARRAY_TYPE
)
1669 mv2
= (TYPE_ATOMIC (mv2
)
1670 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2
),
1672 : TYPE_MAIN_VARIANT (mv2
));
1673 /* A null pointer instead of a type
1674 means there is supposed to be an argument
1675 but nothing is specified about what type it has.
1676 So match anything that self-promotes. */
1677 if (different_types_p
!= NULL
1678 && (a1
== 0) != (a2
== 0))
1679 *different_types_p
= true;
1682 if (c_type_promotes_to (a2
) != a2
)
1687 if (c_type_promotes_to (a1
) != a1
)
1690 /* If one of the lists has an error marker, ignore this arg. */
1691 else if (TREE_CODE (a1
) == ERROR_MARK
1692 || TREE_CODE (a2
) == ERROR_MARK
)
1694 else if (!(newval
= comptypes_internal (mv1
, mv2
, enum_and_int_p
,
1695 different_types_p
)))
1697 if (different_types_p
!= NULL
)
1698 *different_types_p
= true;
1699 /* Allow wait (union {union wait *u; int *i} *)
1700 and wait (union wait *) to be compatible. */
1701 if (TREE_CODE (a1
) == UNION_TYPE
1702 && (TYPE_NAME (a1
) == 0
1703 || TYPE_TRANSPARENT_AGGR (a1
))
1704 && TREE_CODE (TYPE_SIZE (a1
)) == INTEGER_CST
1705 && tree_int_cst_equal (TYPE_SIZE (a1
),
1709 for (memb
= TYPE_FIELDS (a1
);
1710 memb
; memb
= DECL_CHAIN (memb
))
1712 tree mv3
= TREE_TYPE (memb
);
1713 if (mv3
&& mv3
!= error_mark_node
1714 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1715 mv3
= (TYPE_ATOMIC (mv3
)
1716 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1718 : TYPE_MAIN_VARIANT (mv3
));
1719 if (comptypes_internal (mv3
, mv2
, enum_and_int_p
,
1726 else if (TREE_CODE (a2
) == UNION_TYPE
1727 && (TYPE_NAME (a2
) == 0
1728 || TYPE_TRANSPARENT_AGGR (a2
))
1729 && TREE_CODE (TYPE_SIZE (a2
)) == INTEGER_CST
1730 && tree_int_cst_equal (TYPE_SIZE (a2
),
1734 for (memb
= TYPE_FIELDS (a2
);
1735 memb
; memb
= DECL_CHAIN (memb
))
1737 tree mv3
= TREE_TYPE (memb
);
1738 if (mv3
&& mv3
!= error_mark_node
1739 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1740 mv3
= (TYPE_ATOMIC (mv3
)
1741 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1743 : TYPE_MAIN_VARIANT (mv3
));
1744 if (comptypes_internal (mv3
, mv1
, enum_and_int_p
,
1755 /* comptypes said ok, but record if it said to warn. */
1759 args1
= TREE_CHAIN (args1
);
1760 args2
= TREE_CHAIN (args2
);
1764 /* Compute the size to increment a pointer by. When a function type or void
1765 type or incomplete type is passed, size_one_node is returned.
1766 This function does not emit any diagnostics; the caller is responsible
1770 c_size_in_bytes (const_tree type
)
1772 enum tree_code code
= TREE_CODE (type
);
1774 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
1775 || !COMPLETE_TYPE_P (type
))
1776 return size_one_node
;
1778 /* Convert in case a char is more than one unit. */
1779 return size_binop_loc (input_location
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1780 size_int (TYPE_PRECISION (char_type_node
)
1784 /* Return either DECL or its known constant value (if it has one). */
1787 decl_constant_value (tree decl
)
1789 if (/* Don't change a variable array bound or initial value to a constant
1790 in a place where a variable is invalid. Note that DECL_INITIAL
1791 isn't valid for a PARM_DECL. */
1792 current_function_decl
!= 0
1793 && TREE_CODE (decl
) != PARM_DECL
1794 && !TREE_THIS_VOLATILE (decl
)
1795 && TREE_READONLY (decl
)
1796 && DECL_INITIAL (decl
) != 0
1797 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
1798 /* This is invalid if initial value is not constant.
1799 If it has either a function call, a memory reference,
1800 or a variable, then re-evaluating it could give different results. */
1801 && TREE_CONSTANT (DECL_INITIAL (decl
))
1802 /* Check for cases where this is sub-optimal, even though valid. */
1803 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1804 return DECL_INITIAL (decl
);
1808 /* Convert the array expression EXP to a pointer. */
1810 array_to_pointer_conversion (location_t loc
, tree exp
)
1812 tree orig_exp
= exp
;
1813 tree type
= TREE_TYPE (exp
);
1815 tree restype
= TREE_TYPE (type
);
1818 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1820 STRIP_TYPE_NOPS (exp
);
1822 if (TREE_NO_WARNING (orig_exp
))
1823 TREE_NO_WARNING (exp
) = 1;
1825 ptrtype
= build_pointer_type (restype
);
1827 if (TREE_CODE (exp
) == INDIRECT_REF
)
1828 return convert (ptrtype
, TREE_OPERAND (exp
, 0));
1830 /* In C++ array compound literals are temporary objects unless they are
1831 const or appear in namespace scope, so they are destroyed too soon
1832 to use them for much of anything (c++/53220). */
1833 if (warn_cxx_compat
&& TREE_CODE (exp
) == COMPOUND_LITERAL_EXPR
)
1835 tree decl
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
1836 if (!TREE_READONLY (decl
) && !TREE_STATIC (decl
))
1837 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
1838 "converting an array compound literal to a pointer "
1839 "is ill-formed in C++");
1842 adr
= build_unary_op (loc
, ADDR_EXPR
, exp
, 1);
1843 return convert (ptrtype
, adr
);
1846 /* Convert the function expression EXP to a pointer. */
1848 function_to_pointer_conversion (location_t loc
, tree exp
)
1850 tree orig_exp
= exp
;
1852 gcc_assert (TREE_CODE (TREE_TYPE (exp
)) == FUNCTION_TYPE
);
1854 STRIP_TYPE_NOPS (exp
);
1856 if (TREE_NO_WARNING (orig_exp
))
1857 TREE_NO_WARNING (exp
) = 1;
1859 return build_unary_op (loc
, ADDR_EXPR
, exp
, 0);
1862 /* Mark EXP as read, not just set, for set but not used -Wunused
1863 warning purposes. */
1866 mark_exp_read (tree exp
)
1868 switch (TREE_CODE (exp
))
1872 DECL_READ_P (exp
) = 1;
1881 mark_exp_read (TREE_OPERAND (exp
, 0));
1884 case C_MAYBE_CONST_EXPR
:
1885 mark_exp_read (TREE_OPERAND (exp
, 1));
1892 /* Perform the default conversion of arrays and functions to pointers.
1893 Return the result of converting EXP. For any other expression, just
1896 LOC is the location of the expression. */
1899 default_function_array_conversion (location_t loc
, struct c_expr exp
)
1901 tree orig_exp
= exp
.value
;
1902 tree type
= TREE_TYPE (exp
.value
);
1903 enum tree_code code
= TREE_CODE (type
);
1909 bool not_lvalue
= false;
1910 bool lvalue_array_p
;
1912 while ((TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
1913 || CONVERT_EXPR_P (exp
.value
))
1914 && TREE_TYPE (TREE_OPERAND (exp
.value
, 0)) == type
)
1916 if (TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
)
1918 exp
.value
= TREE_OPERAND (exp
.value
, 0);
1921 if (TREE_NO_WARNING (orig_exp
))
1922 TREE_NO_WARNING (exp
.value
) = 1;
1924 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
.value
);
1925 if (!flag_isoc99
&& !lvalue_array_p
)
1927 /* Before C99, non-lvalue arrays do not decay to pointers.
1928 Normally, using such an array would be invalid; but it can
1929 be used correctly inside sizeof or as a statement expression.
1930 Thus, do not give an error here; an error will result later. */
1934 exp
.value
= array_to_pointer_conversion (loc
, exp
.value
);
1938 exp
.value
= function_to_pointer_conversion (loc
, exp
.value
);
1948 default_function_array_read_conversion (location_t loc
, struct c_expr exp
)
1950 mark_exp_read (exp
.value
);
1951 return default_function_array_conversion (loc
, exp
);
1954 /* Return whether EXPR should be treated as an atomic lvalue for the
1955 purposes of load and store handling. */
1958 really_atomic_lvalue (tree expr
)
1960 if (error_operand_p (expr
))
1962 if (!TYPE_ATOMIC (TREE_TYPE (expr
)))
1964 if (!lvalue_p (expr
))
1967 /* Ignore _Atomic on register variables, since their addresses can't
1968 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1969 sequences wouldn't work. Ignore _Atomic on structures containing
1970 bit-fields, since accessing elements of atomic structures or
1971 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1972 it's undefined at translation time or execution time, and the
1973 normal atomic sequences again wouldn't work. */
1974 while (handled_component_p (expr
))
1976 if (TREE_CODE (expr
) == COMPONENT_REF
1977 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
1979 expr
= TREE_OPERAND (expr
, 0);
1981 if (DECL_P (expr
) && C_DECL_REGISTER (expr
))
1986 /* Convert expression EXP (location LOC) from lvalue to rvalue,
1987 including converting functions and arrays to pointers if CONVERT_P.
1988 If READ_P, also mark the expression as having been read. */
1991 convert_lvalue_to_rvalue (location_t loc
, struct c_expr exp
,
1992 bool convert_p
, bool read_p
)
1995 mark_exp_read (exp
.value
);
1997 exp
= default_function_array_conversion (loc
, exp
);
1998 if (really_atomic_lvalue (exp
.value
))
2000 vec
<tree
, va_gc
> *params
;
2001 tree nonatomic_type
, tmp
, tmp_addr
, fndecl
, func_call
;
2002 tree expr_type
= TREE_TYPE (exp
.value
);
2003 tree expr_addr
= build_unary_op (loc
, ADDR_EXPR
, exp
.value
, 0);
2004 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
2006 gcc_assert (TYPE_ATOMIC (expr_type
));
2008 /* Expansion of a generic atomic load may require an addition
2009 element, so allocate enough to prevent a resize. */
2010 vec_alloc (params
, 4);
2012 /* Remove the qualifiers for the rest of the expressions and
2013 create the VAL temp variable to hold the RHS. */
2014 nonatomic_type
= build_qualified_type (expr_type
, TYPE_UNQUALIFIED
);
2015 tmp
= create_tmp_var (nonatomic_type
, NULL
);
2016 tmp_addr
= build_unary_op (loc
, ADDR_EXPR
, tmp
, 0);
2017 TREE_ADDRESSABLE (tmp
) = 1;
2018 TREE_NO_WARNING (tmp
) = 1;
2020 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2021 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
2022 params
->quick_push (expr_addr
);
2023 params
->quick_push (tmp_addr
);
2024 params
->quick_push (seq_cst
);
2025 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
2027 /* EXPR is always read. */
2028 mark_exp_read (exp
.value
);
2030 /* Return tmp which contains the value loaded. */
2031 exp
.value
= build2 (COMPOUND_EXPR
, nonatomic_type
, func_call
, tmp
);
2036 /* EXP is an expression of integer type. Apply the integer promotions
2037 to it and return the promoted value. */
2040 perform_integral_promotions (tree exp
)
2042 tree type
= TREE_TYPE (exp
);
2043 enum tree_code code
= TREE_CODE (type
);
2045 gcc_assert (INTEGRAL_TYPE_P (type
));
2047 /* Normally convert enums to int,
2048 but convert wide enums to something wider. */
2049 if (code
== ENUMERAL_TYPE
)
2051 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
2052 TYPE_PRECISION (integer_type_node
)),
2053 ((TYPE_PRECISION (type
)
2054 >= TYPE_PRECISION (integer_type_node
))
2055 && TYPE_UNSIGNED (type
)));
2057 return convert (type
, exp
);
2060 /* ??? This should no longer be needed now bit-fields have their
2062 if (TREE_CODE (exp
) == COMPONENT_REF
2063 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
2064 /* If it's thinner than an int, promote it like a
2065 c_promoting_integer_type_p, otherwise leave it alone. */
2066 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
2067 TYPE_PRECISION (integer_type_node
)))
2068 return convert (integer_type_node
, exp
);
2070 if (c_promoting_integer_type_p (type
))
2072 /* Preserve unsignedness if not really getting any wider. */
2073 if (TYPE_UNSIGNED (type
)
2074 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
2075 return convert (unsigned_type_node
, exp
);
2077 return convert (integer_type_node
, exp
);
2084 /* Perform default promotions for C data used in expressions.
2085 Enumeral types or short or char are converted to int.
2086 In addition, manifest constants symbols are replaced by their values. */
2089 default_conversion (tree exp
)
2092 tree type
= TREE_TYPE (exp
);
2093 enum tree_code code
= TREE_CODE (type
);
2096 mark_exp_read (exp
);
2098 /* Functions and arrays have been converted during parsing. */
2099 gcc_assert (code
!= FUNCTION_TYPE
);
2100 if (code
== ARRAY_TYPE
)
2103 /* Constants can be used directly unless they're not loadable. */
2104 if (TREE_CODE (exp
) == CONST_DECL
)
2105 exp
= DECL_INITIAL (exp
);
2107 /* Strip no-op conversions. */
2109 STRIP_TYPE_NOPS (exp
);
2111 if (TREE_NO_WARNING (orig_exp
))
2112 TREE_NO_WARNING (exp
) = 1;
2114 if (code
== VOID_TYPE
)
2116 error_at (EXPR_LOC_OR_LOC (exp
, input_location
),
2117 "void value not ignored as it ought to be");
2118 return error_mark_node
;
2121 exp
= require_complete_type (exp
);
2122 if (exp
== error_mark_node
)
2123 return error_mark_node
;
2125 promoted_type
= targetm
.promoted_type (type
);
2127 return convert (promoted_type
, exp
);
2129 if (INTEGRAL_TYPE_P (type
))
2130 return perform_integral_promotions (exp
);
2135 /* Look up COMPONENT in a structure or union TYPE.
2137 If the component name is not found, returns NULL_TREE. Otherwise,
2138 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2139 stepping down the chain to the component, which is in the last
2140 TREE_VALUE of the list. Normally the list is of length one, but if
2141 the component is embedded within (nested) anonymous structures or
2142 unions, the list steps down the chain to the component. */
2145 lookup_field (tree type
, tree component
)
2149 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2150 to the field elements. Use a binary search on this array to quickly
2151 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2152 will always be set for structures which have many elements. */
2154 if (TYPE_LANG_SPECIFIC (type
) && TYPE_LANG_SPECIFIC (type
)->s
)
2157 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
2159 field
= TYPE_FIELDS (type
);
2161 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
2162 while (top
- bot
> 1)
2164 half
= (top
- bot
+ 1) >> 1;
2165 field
= field_array
[bot
+half
];
2167 if (DECL_NAME (field
) == NULL_TREE
)
2169 /* Step through all anon unions in linear fashion. */
2170 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
2172 field
= field_array
[bot
++];
2173 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
2174 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
2176 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2179 return tree_cons (NULL_TREE
, field
, anon
);
2181 /* The Plan 9 compiler permits referring
2182 directly to an anonymous struct/union field
2183 using a typedef name. */
2184 if (flag_plan9_extensions
2185 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2186 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field
)))
2188 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2194 /* Entire record is only anon unions. */
2198 /* Restart the binary search, with new lower bound. */
2202 if (DECL_NAME (field
) == component
)
2204 if (DECL_NAME (field
) < component
)
2210 if (DECL_NAME (field_array
[bot
]) == component
)
2211 field
= field_array
[bot
];
2212 else if (DECL_NAME (field
) != component
)
2217 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2219 if (DECL_NAME (field
) == NULL_TREE
2220 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
2221 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
))
2223 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2226 return tree_cons (NULL_TREE
, field
, anon
);
2228 /* The Plan 9 compiler permits referring directly to an
2229 anonymous struct/union field using a typedef
2231 if (flag_plan9_extensions
2232 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2233 && TREE_CODE (TYPE_NAME (TREE_TYPE (field
))) == TYPE_DECL
2234 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2239 if (DECL_NAME (field
) == component
)
2243 if (field
== NULL_TREE
)
2247 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
2250 /* Make an expression to refer to the COMPONENT field of structure or
2251 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2252 location of the COMPONENT_REF. */
2255 build_component_ref (location_t loc
, tree datum
, tree component
)
2257 tree type
= TREE_TYPE (datum
);
2258 enum tree_code code
= TREE_CODE (type
);
2261 bool datum_lvalue
= lvalue_p (datum
);
2263 if (!objc_is_public (datum
, component
))
2264 return error_mark_node
;
2266 /* Detect Objective-C property syntax object.property. */
2267 if (c_dialect_objc ()
2268 && (ref
= objc_maybe_build_component_ref (datum
, component
)))
2271 /* See if there is a field or component with name COMPONENT. */
2273 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2275 if (!COMPLETE_TYPE_P (type
))
2277 c_incomplete_type_error (NULL_TREE
, type
);
2278 return error_mark_node
;
2281 field
= lookup_field (type
, component
);
2285 error_at (loc
, "%qT has no member named %qE", type
, component
);
2286 return error_mark_node
;
2289 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2290 This might be better solved in future the way the C++ front
2291 end does it - by giving the anonymous entities each a
2292 separate name and type, and then have build_component_ref
2293 recursively call itself. We can't do that here. */
2296 tree subdatum
= TREE_VALUE (field
);
2299 bool use_datum_quals
;
2301 if (TREE_TYPE (subdatum
) == error_mark_node
)
2302 return error_mark_node
;
2304 /* If this is an rvalue, it does not have qualifiers in C
2305 standard terms and we must avoid propagating such
2306 qualifiers down to a non-lvalue array that is then
2307 converted to a pointer. */
2308 use_datum_quals
= (datum_lvalue
2309 || TREE_CODE (TREE_TYPE (subdatum
)) != ARRAY_TYPE
);
2311 quals
= TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum
)));
2312 if (use_datum_quals
)
2313 quals
|= TYPE_QUALS (TREE_TYPE (datum
));
2314 subtype
= c_build_qualified_type (TREE_TYPE (subdatum
), quals
);
2316 ref
= build3 (COMPONENT_REF
, subtype
, datum
, subdatum
,
2318 SET_EXPR_LOCATION (ref
, loc
);
2319 if (TREE_READONLY (subdatum
)
2320 || (use_datum_quals
&& TREE_READONLY (datum
)))
2321 TREE_READONLY (ref
) = 1;
2322 if (TREE_THIS_VOLATILE (subdatum
)
2323 || (use_datum_quals
&& TREE_THIS_VOLATILE (datum
)))
2324 TREE_THIS_VOLATILE (ref
) = 1;
2326 if (TREE_DEPRECATED (subdatum
))
2327 warn_deprecated_use (subdatum
, NULL_TREE
);
2331 field
= TREE_CHAIN (field
);
2337 else if (code
!= ERROR_MARK
)
2339 "request for member %qE in something not a structure or union",
2342 return error_mark_node
;
2345 /* Given an expression PTR for a pointer, return an expression
2346 for the value pointed to.
2347 ERRORSTRING is the name of the operator to appear in error messages.
2349 LOC is the location to use for the generated tree. */
2352 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errstring
)
2354 tree pointer
= default_conversion (ptr
);
2355 tree type
= TREE_TYPE (pointer
);
2358 if (TREE_CODE (type
) == POINTER_TYPE
)
2360 if (CONVERT_EXPR_P (pointer
)
2361 || TREE_CODE (pointer
) == VIEW_CONVERT_EXPR
)
2363 /* If a warning is issued, mark it to avoid duplicates from
2364 the backend. This only needs to be done at
2365 warn_strict_aliasing > 2. */
2366 if (warn_strict_aliasing
> 2)
2367 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer
, 0)),
2368 type
, TREE_OPERAND (pointer
, 0)))
2369 TREE_NO_WARNING (pointer
) = 1;
2372 if (TREE_CODE (pointer
) == ADDR_EXPR
2373 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
2374 == TREE_TYPE (type
)))
2376 ref
= TREE_OPERAND (pointer
, 0);
2377 protected_set_expr_location (ref
, loc
);
2382 tree t
= TREE_TYPE (type
);
2384 ref
= build1 (INDIRECT_REF
, t
, pointer
);
2386 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
2388 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)))
2390 error_at (loc
, "dereferencing pointer to incomplete type "
2392 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)) = 1;
2394 return error_mark_node
;
2396 if (VOID_TYPE_P (t
) && c_inhibit_evaluation_warnings
== 0)
2397 warning_at (loc
, 0, "dereferencing %<void *%> pointer");
2399 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2400 so that we get the proper error message if the result is used
2401 to assign to. Also, &* is supposed to be a no-op.
2402 And ANSI C seems to specify that the type of the result
2403 should be the const type. */
2404 /* A de-reference of a pointer to const is not a const. It is valid
2405 to change it via some other pointer. */
2406 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2407 TREE_SIDE_EFFECTS (ref
)
2408 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
2409 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2410 protected_set_expr_location (ref
, loc
);
2414 else if (TREE_CODE (pointer
) != ERROR_MARK
)
2415 invalid_indirection_error (loc
, type
, errstring
);
2417 return error_mark_node
;
2420 /* This handles expressions of the form "a[i]", which denotes
2423 This is logically equivalent in C to *(a+i), but we may do it differently.
2424 If A is a variable or a member, we generate a primitive ARRAY_REF.
2425 This avoids forcing the array out of registers, and can work on
2426 arrays that are not lvalues (for example, members of structures returned
2429 For vector types, allow vector[i] but not i[vector], and create
2430 *(((type*)&vectortype) + i) for the expression.
2432 LOC is the location to use for the returned expression. */
2435 build_array_ref (location_t loc
, tree array
, tree index
)
2438 bool swapped
= false;
2439 if (TREE_TYPE (array
) == error_mark_node
2440 || TREE_TYPE (index
) == error_mark_node
)
2441 return error_mark_node
;
2443 if (flag_cilkplus
&& contains_array_notation_expr (index
))
2446 if (!find_rank (loc
, index
, index
, true, &rank
))
2447 return error_mark_node
;
2450 error_at (loc
, "rank of the array's index is greater than 1");
2451 return error_mark_node
;
2454 if (TREE_CODE (TREE_TYPE (array
)) != ARRAY_TYPE
2455 && TREE_CODE (TREE_TYPE (array
)) != POINTER_TYPE
2456 /* Allow vector[index] but not index[vector]. */
2457 && TREE_CODE (TREE_TYPE (array
)) != VECTOR_TYPE
)
2460 if (TREE_CODE (TREE_TYPE (index
)) != ARRAY_TYPE
2461 && TREE_CODE (TREE_TYPE (index
)) != POINTER_TYPE
)
2464 "subscripted value is neither array nor pointer nor vector");
2466 return error_mark_node
;
2474 if (!INTEGRAL_TYPE_P (TREE_TYPE (index
)))
2476 error_at (loc
, "array subscript is not an integer");
2477 return error_mark_node
;
2480 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array
))) == FUNCTION_TYPE
)
2482 error_at (loc
, "subscripted value is pointer to function");
2483 return error_mark_node
;
2486 /* ??? Existing practice has been to warn only when the char
2487 index is syntactically the index, not for char[array]. */
2489 warn_array_subscript_with_type_char (index
);
2491 /* Apply default promotions *after* noticing character types. */
2492 index
= default_conversion (index
);
2493 if (index
== error_mark_node
)
2494 return error_mark_node
;
2496 gcc_assert (TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
);
2498 convert_vector_to_pointer_for_subscript (loc
, &array
, index
);
2500 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
2504 /* An array that is indexed by a non-constant
2505 cannot be stored in a register; we must be able to do
2506 address arithmetic on its address.
2507 Likewise an array of elements of variable size. */
2508 if (TREE_CODE (index
) != INTEGER_CST
2509 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2510 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2512 if (!c_mark_addressable (array
))
2513 return error_mark_node
;
2515 /* An array that is indexed by a constant value which is not within
2516 the array bounds cannot be stored in a register either; because we
2517 would get a crash in store_bit_field/extract_bit_field when trying
2518 to access a non-existent part of the register. */
2519 if (TREE_CODE (index
) == INTEGER_CST
2520 && TYPE_DOMAIN (TREE_TYPE (array
))
2521 && !int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
2523 if (!c_mark_addressable (array
))
2524 return error_mark_node
;
2527 if (pedantic
|| warn_c90_c99_compat
)
2530 while (TREE_CODE (foo
) == COMPONENT_REF
)
2531 foo
= TREE_OPERAND (foo
, 0);
2532 if (TREE_CODE (foo
) == VAR_DECL
&& C_DECL_REGISTER (foo
))
2533 pedwarn (loc
, OPT_Wpedantic
,
2534 "ISO C forbids subscripting %<register%> array");
2535 else if (!lvalue_p (foo
))
2536 pedwarn_c90 (loc
, OPT_Wpedantic
,
2537 "ISO C90 forbids subscripting non-lvalue "
2541 type
= TREE_TYPE (TREE_TYPE (array
));
2542 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
2543 /* Array ref is const/volatile if the array elements are
2544 or if the array is. */
2545 TREE_READONLY (rval
)
2546 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2547 | TREE_READONLY (array
));
2548 TREE_SIDE_EFFECTS (rval
)
2549 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2550 | TREE_SIDE_EFFECTS (array
));
2551 TREE_THIS_VOLATILE (rval
)
2552 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2553 /* This was added by rms on 16 Nov 91.
2554 It fixes vol struct foo *a; a->elts[1]
2555 in an inline function.
2556 Hope it doesn't break something else. */
2557 | TREE_THIS_VOLATILE (array
));
2558 ret
= require_complete_type (rval
);
2559 protected_set_expr_location (ret
, loc
);
2564 tree ar
= default_conversion (array
);
2566 if (ar
== error_mark_node
)
2569 gcc_assert (TREE_CODE (TREE_TYPE (ar
)) == POINTER_TYPE
);
2570 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) != FUNCTION_TYPE
);
2572 return build_indirect_ref
2573 (loc
, build_binary_op (loc
, PLUS_EXPR
, ar
, index
, 0),
2578 /* Build an external reference to identifier ID. FUN indicates
2579 whether this will be used for a function call. LOC is the source
2580 location of the identifier. This sets *TYPE to the type of the
2581 identifier, which is not the same as the type of the returned value
2582 for CONST_DECLs defined as enum constants. If the type of the
2583 identifier is not available, *TYPE is set to NULL. */
2585 build_external_ref (location_t loc
, tree id
, int fun
, tree
*type
)
2588 tree decl
= lookup_name (id
);
2590 /* In Objective-C, an instance variable (ivar) may be preferred to
2591 whatever lookup_name() found. */
2592 decl
= objc_lookup_ivar (decl
, id
);
2595 if (decl
&& decl
!= error_mark_node
)
2598 *type
= TREE_TYPE (ref
);
2601 /* Implicit function declaration. */
2602 ref
= implicitly_declare (loc
, id
);
2603 else if (decl
== error_mark_node
)
2604 /* Don't complain about something that's already been
2605 complained about. */
2606 return error_mark_node
;
2609 undeclared_variable (loc
, id
);
2610 return error_mark_node
;
2613 if (TREE_TYPE (ref
) == error_mark_node
)
2614 return error_mark_node
;
2616 if (TREE_DEPRECATED (ref
))
2617 warn_deprecated_use (ref
, NULL_TREE
);
2619 /* Recursive call does not count as usage. */
2620 if (ref
!= current_function_decl
)
2622 TREE_USED (ref
) = 1;
2625 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
2627 if (!in_sizeof
&& !in_typeof
)
2628 C_DECL_USED (ref
) = 1;
2629 else if (DECL_INITIAL (ref
) == 0
2630 && DECL_EXTERNAL (ref
)
2631 && !TREE_PUBLIC (ref
))
2632 record_maybe_used_decl (ref
);
2635 if (TREE_CODE (ref
) == CONST_DECL
)
2637 used_types_insert (TREE_TYPE (ref
));
2640 && TREE_CODE (TREE_TYPE (ref
)) == ENUMERAL_TYPE
2641 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref
)))
2643 warning_at (loc
, OPT_Wc___compat
,
2644 ("enum constant defined in struct or union "
2645 "is not visible in C++"));
2646 inform (DECL_SOURCE_LOCATION (ref
), "enum constant defined here");
2649 ref
= DECL_INITIAL (ref
);
2650 TREE_CONSTANT (ref
) = 1;
2652 else if (current_function_decl
!= 0
2653 && !DECL_FILE_SCOPE_P (current_function_decl
)
2654 && (TREE_CODE (ref
) == VAR_DECL
2655 || TREE_CODE (ref
) == PARM_DECL
2656 || TREE_CODE (ref
) == FUNCTION_DECL
))
2658 tree context
= decl_function_context (ref
);
2660 if (context
!= 0 && context
!= current_function_decl
)
2661 DECL_NONLOCAL (ref
) = 1;
2663 /* C99 6.7.4p3: An inline definition of a function with external
2664 linkage ... shall not contain a reference to an identifier with
2665 internal linkage. */
2666 else if (current_function_decl
!= 0
2667 && DECL_DECLARED_INLINE_P (current_function_decl
)
2668 && DECL_EXTERNAL (current_function_decl
)
2669 && VAR_OR_FUNCTION_DECL_P (ref
)
2670 && (TREE_CODE (ref
) != VAR_DECL
|| TREE_STATIC (ref
))
2671 && ! TREE_PUBLIC (ref
)
2672 && DECL_CONTEXT (ref
) != current_function_decl
)
2673 record_inline_static (loc
, current_function_decl
, ref
,
2679 /* Record details of decls possibly used inside sizeof or typeof. */
2680 struct maybe_used_decl
2684 /* The level seen at (in_sizeof + in_typeof). */
2686 /* The next one at this level or above, or NULL. */
2687 struct maybe_used_decl
*next
;
2690 static struct maybe_used_decl
*maybe_used_decls
;
2692 /* Record that DECL, an undefined static function reference seen
2693 inside sizeof or typeof, might be used if the operand of sizeof is
2694 a VLA type or the operand of typeof is a variably modified
2698 record_maybe_used_decl (tree decl
)
2700 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
2702 t
->level
= in_sizeof
+ in_typeof
;
2703 t
->next
= maybe_used_decls
;
2704 maybe_used_decls
= t
;
2707 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2708 USED is false, just discard them. If it is true, mark them used
2709 (if no longer inside sizeof or typeof) or move them to the next
2710 level up (if still inside sizeof or typeof). */
2713 pop_maybe_used (bool used
)
2715 struct maybe_used_decl
*p
= maybe_used_decls
;
2716 int cur_level
= in_sizeof
+ in_typeof
;
2717 while (p
&& p
->level
> cur_level
)
2722 C_DECL_USED (p
->decl
) = 1;
2724 p
->level
= cur_level
;
2728 if (!used
|| cur_level
== 0)
2729 maybe_used_decls
= p
;
2732 /* Return the result of sizeof applied to EXPR. */
2735 c_expr_sizeof_expr (location_t loc
, struct c_expr expr
)
2738 if (expr
.value
== error_mark_node
)
2740 ret
.value
= error_mark_node
;
2741 ret
.original_code
= ERROR_MARK
;
2742 ret
.original_type
= NULL
;
2743 pop_maybe_used (false);
2747 bool expr_const_operands
= true;
2749 if (TREE_CODE (expr
.value
) == PARM_DECL
2750 && C_ARRAY_PARAMETER (expr
.value
))
2752 if (warning_at (loc
, OPT_Wsizeof_array_argument
,
2753 "%<sizeof%> on array function parameter %qE will "
2754 "return size of %qT", expr
.value
,
2755 expr
.original_type
))
2756 inform (DECL_SOURCE_LOCATION (expr
.value
), "declared here");
2758 tree folded_expr
= c_fully_fold (expr
.value
, require_constant_value
,
2759 &expr_const_operands
);
2760 ret
.value
= c_sizeof (loc
, TREE_TYPE (folded_expr
));
2761 c_last_sizeof_arg
= expr
.value
;
2762 ret
.original_code
= SIZEOF_EXPR
;
2763 ret
.original_type
= NULL
;
2764 if (c_vla_type_p (TREE_TYPE (folded_expr
)))
2766 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2767 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2768 folded_expr
, ret
.value
);
2769 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !expr_const_operands
;
2770 SET_EXPR_LOCATION (ret
.value
, loc
);
2772 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr
)));
2777 /* Return the result of sizeof applied to T, a structure for the type
2778 name passed to sizeof (rather than the type itself). LOC is the
2779 location of the original expression. */
2782 c_expr_sizeof_type (location_t loc
, struct c_type_name
*t
)
2786 tree type_expr
= NULL_TREE
;
2787 bool type_expr_const
= true;
2788 type
= groktypename (t
, &type_expr
, &type_expr_const
);
2789 ret
.value
= c_sizeof (loc
, type
);
2790 c_last_sizeof_arg
= type
;
2791 ret
.original_code
= SIZEOF_EXPR
;
2792 ret
.original_type
= NULL
;
2793 if ((type_expr
|| TREE_CODE (ret
.value
) == INTEGER_CST
)
2794 && c_vla_type_p (type
))
2796 /* If the type is a [*] array, it is a VLA but is represented as
2797 having a size of zero. In such a case we must ensure that
2798 the result of sizeof does not get folded to a constant by
2799 c_fully_fold, because if the size is evaluated the result is
2800 not constant and so constraints on zero or negative size
2801 arrays must not be applied when this sizeof call is inside
2802 another array declarator. */
2804 type_expr
= integer_zero_node
;
2805 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2806 type_expr
, ret
.value
);
2807 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !type_expr_const
;
2809 pop_maybe_used (type
!= error_mark_node
2810 ? C_TYPE_VARIABLE_SIZE (type
) : false);
2814 /* Build a function call to function FUNCTION with parameters PARAMS.
2815 The function call is at LOC.
2816 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2817 TREE_VALUE of each node is a parameter-expression.
2818 FUNCTION's data type may be a function type or a pointer-to-function. */
2821 build_function_call (location_t loc
, tree function
, tree params
)
2823 vec
<tree
, va_gc
> *v
;
2826 vec_alloc (v
, list_length (params
));
2827 for (; params
; params
= TREE_CHAIN (params
))
2828 v
->quick_push (TREE_VALUE (params
));
2829 ret
= c_build_function_call_vec (loc
, vNULL
, function
, v
, NULL
);
2834 /* Give a note about the location of the declaration of DECL. */
2836 static void inform_declaration (tree decl
)
2838 if (decl
&& (TREE_CODE (decl
) != FUNCTION_DECL
|| !DECL_BUILT_IN (decl
)))
2839 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
2842 /* Build a function call to function FUNCTION with parameters PARAMS.
2843 ORIGTYPES, if not NULL, is a vector of types; each element is
2844 either NULL or the original type of the corresponding element in
2845 PARAMS. The original type may differ from TREE_TYPE of the
2846 parameter for enums. FUNCTION's data type may be a function type
2847 or pointer-to-function. This function changes the elements of
2851 build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
2852 tree function
, vec
<tree
, va_gc
> *params
,
2853 vec
<tree
, va_gc
> *origtypes
)
2855 tree fntype
, fundecl
= 0;
2856 tree name
= NULL_TREE
, result
;
2862 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2863 STRIP_TYPE_NOPS (function
);
2865 /* Convert anything with function type to a pointer-to-function. */
2866 if (TREE_CODE (function
) == FUNCTION_DECL
)
2868 name
= DECL_NAME (function
);
2871 tm_malloc_replacement (function
);
2873 /* Atomic functions have type checking/casting already done. They are
2874 often rewritten and don't match the original parameter list. */
2875 if (name
&& !strncmp (IDENTIFIER_POINTER (name
), "__atomic_", 9))
2879 && is_cilkplus_reduce_builtin (function
))
2882 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
)
2883 function
= function_to_pointer_conversion (loc
, function
);
2885 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2886 expressions, like those used for ObjC messenger dispatches. */
2887 if (params
&& !params
->is_empty ())
2888 function
= objc_rewrite_function_call (function
, (*params
)[0]);
2890 function
= c_fully_fold (function
, false, NULL
);
2892 fntype
= TREE_TYPE (function
);
2894 if (TREE_CODE (fntype
) == ERROR_MARK
)
2895 return error_mark_node
;
2897 if (!(TREE_CODE (fntype
) == POINTER_TYPE
2898 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
2900 if (!flag_diagnostics_show_caret
)
2902 "called object %qE is not a function or function pointer",
2904 else if (DECL_P (function
))
2907 "called object %qD is not a function or function pointer",
2909 inform_declaration (function
);
2913 "called object is not a function or function pointer");
2914 return error_mark_node
;
2917 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
2918 current_function_returns_abnormally
= 1;
2920 /* fntype now gets the type of function pointed to. */
2921 fntype
= TREE_TYPE (fntype
);
2923 /* Convert the parameters to the types declared in the
2924 function prototype, or apply default promotions. */
2926 nargs
= convert_arguments (loc
, arg_loc
, TYPE_ARG_TYPES (fntype
), params
,
2927 origtypes
, function
, fundecl
);
2929 return error_mark_node
;
2931 /* Check that the function is called through a compatible prototype.
2932 If it is not, warn. */
2933 if (CONVERT_EXPR_P (function
)
2934 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
2935 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
2936 && !comptypes (fntype
, TREE_TYPE (tem
)))
2938 tree return_type
= TREE_TYPE (fntype
);
2940 /* This situation leads to run-time undefined behavior. We can't,
2941 therefore, simply error unless we can prove that all possible
2942 executions of the program must execute the code. */
2943 warning_at (loc
, 0, "function called through a non-compatible type");
2945 if (VOID_TYPE_P (return_type
)
2946 && TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
)
2948 "function with qualified void return type called");
2951 argarray
= vec_safe_address (params
);
2953 /* Check that arguments to builtin functions match the expectations. */
2955 && DECL_BUILT_IN (fundecl
)
2956 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
2957 && !check_builtin_function_arguments (fundecl
, nargs
, argarray
))
2958 return error_mark_node
;
2960 /* Check that the arguments to the function are valid. */
2961 check_function_arguments (fntype
, nargs
, argarray
);
2963 if (name
!= NULL_TREE
2964 && !strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10))
2966 if (require_constant_value
)
2968 fold_build_call_array_initializer_loc (loc
, TREE_TYPE (fntype
),
2969 function
, nargs
, argarray
);
2971 result
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
2972 function
, nargs
, argarray
);
2973 if (TREE_CODE (result
) == NOP_EXPR
2974 && TREE_CODE (TREE_OPERAND (result
, 0)) == INTEGER_CST
)
2975 STRIP_TYPE_NOPS (result
);
2978 result
= build_call_array_loc (loc
, TREE_TYPE (fntype
),
2979 function
, nargs
, argarray
);
2981 if (VOID_TYPE_P (TREE_TYPE (result
)))
2983 if (TYPE_QUALS (TREE_TYPE (result
)) != TYPE_UNQUALIFIED
)
2985 "function with qualified void return type called");
2988 return require_complete_type (result
);
2991 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2994 c_build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
2995 tree function
, vec
<tree
, va_gc
> *params
,
2996 vec
<tree
, va_gc
> *origtypes
)
2998 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2999 STRIP_TYPE_NOPS (function
);
3001 /* Convert anything with function type to a pointer-to-function. */
3002 if (TREE_CODE (function
) == FUNCTION_DECL
)
3004 /* Implement type-directed function overloading for builtins.
3005 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3006 handle all the type checking. The result is a complete expression
3007 that implements this function call. */
3008 tree tem
= resolve_overloaded_builtin (loc
, function
, params
);
3012 return build_function_call_vec (loc
, arg_loc
, function
, params
, origtypes
);
3015 /* Convert the argument expressions in the vector VALUES
3016 to the types in the list TYPELIST.
3018 If TYPELIST is exhausted, or when an element has NULL as its type,
3019 perform the default conversions.
3021 ORIGTYPES is the original types of the expressions in VALUES. This
3022 holds the type of enum values which have been converted to integral
3023 types. It may be NULL.
3025 FUNCTION is a tree for the called function. It is used only for
3026 error messages, where it is formatted with %qE.
3028 This is also where warnings about wrong number of args are generated.
3030 ARG_LOC are locations of function arguments (if any).
3032 Returns the actual number of arguments processed (which may be less
3033 than the length of VALUES in some error situations), or -1 on
3037 convert_arguments (location_t loc
, vec
<location_t
> arg_loc
, tree typelist
,
3038 vec
<tree
, va_gc
> *values
, vec
<tree
, va_gc
> *origtypes
,
3039 tree function
, tree fundecl
)
3042 unsigned int parmnum
;
3043 bool error_args
= false;
3044 const bool type_generic
= fundecl
3045 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl
)));
3046 bool type_generic_remove_excess_precision
= false;
3049 /* Change pointer to function to the function itself for
3051 if (TREE_CODE (function
) == ADDR_EXPR
3052 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
3053 function
= TREE_OPERAND (function
, 0);
3055 /* Handle an ObjC selector specially for diagnostics. */
3056 selector
= objc_message_selector ();
3058 /* For type-generic built-in functions, determine whether excess
3059 precision should be removed (classification) or not
3062 && DECL_BUILT_IN (fundecl
)
3063 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
)
3065 switch (DECL_FUNCTION_CODE (fundecl
))
3067 case BUILT_IN_ISFINITE
:
3068 case BUILT_IN_ISINF
:
3069 case BUILT_IN_ISINF_SIGN
:
3070 case BUILT_IN_ISNAN
:
3071 case BUILT_IN_ISNORMAL
:
3072 case BUILT_IN_FPCLASSIFY
:
3073 type_generic_remove_excess_precision
= true;
3077 type_generic_remove_excess_precision
= false;
3081 if (flag_cilkplus
&& fundecl
&& is_cilkplus_reduce_builtin (fundecl
))
3082 return vec_safe_length (values
);
3084 /* Scan the given expressions and types, producing individual
3085 converted arguments. */
3087 for (typetail
= typelist
, parmnum
= 0;
3088 values
&& values
->iterate (parmnum
, &val
);
3091 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3092 tree valtype
= TREE_TYPE (val
);
3093 tree rname
= function
;
3094 int argnum
= parmnum
+ 1;
3095 const char *invalid_func_diag
;
3096 bool excess_precision
= false;
3099 /* Some __atomic_* builtins have additional hidden argument at
3102 = !arg_loc
.is_empty () && values
->length () == arg_loc
.length ()
3103 ? expansion_point_location_if_in_system_header (arg_loc
[parmnum
])
3106 if (type
== void_type_node
)
3109 error_at (loc
, "too many arguments to method %qE", selector
);
3111 error_at (loc
, "too many arguments to function %qE", function
);
3112 inform_declaration (fundecl
);
3116 if (selector
&& argnum
> 2)
3122 npc
= null_pointer_constant_p (val
);
3124 /* If there is excess precision and a prototype, convert once to
3125 the required type rather than converting via the semantic
3126 type. Likewise without a prototype a float value represented
3127 as long double should be converted once to double. But for
3128 type-generic classification functions excess precision must
3130 if (TREE_CODE (val
) == EXCESS_PRECISION_EXPR
3131 && (type
|| !type_generic
|| !type_generic_remove_excess_precision
))
3133 val
= TREE_OPERAND (val
, 0);
3134 excess_precision
= true;
3136 val
= c_fully_fold (val
, false, NULL
);
3137 STRIP_TYPE_NOPS (val
);
3139 val
= require_complete_type (val
);
3143 /* Formal parm type is specified by a function prototype. */
3145 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3147 error_at (ploc
, "type of formal parameter %d is incomplete",
3155 /* Optionally warn about conversions that
3156 differ from the default conversions. */
3157 if (warn_traditional_conversion
|| warn_traditional
)
3159 unsigned int formal_prec
= TYPE_PRECISION (type
);
3161 if (INTEGRAL_TYPE_P (type
)
3162 && TREE_CODE (valtype
) == REAL_TYPE
)
3163 warning_at (ploc
, OPT_Wtraditional_conversion
,
3164 "passing argument %d of %qE as integer rather "
3165 "than floating due to prototype",
3167 if (INTEGRAL_TYPE_P (type
)
3168 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3169 warning_at (ploc
, OPT_Wtraditional_conversion
,
3170 "passing argument %d of %qE as integer rather "
3171 "than complex due to prototype",
3173 else if (TREE_CODE (type
) == COMPLEX_TYPE
3174 && TREE_CODE (valtype
) == REAL_TYPE
)
3175 warning_at (ploc
, OPT_Wtraditional_conversion
,
3176 "passing argument %d of %qE as complex rather "
3177 "than floating due to prototype",
3179 else if (TREE_CODE (type
) == REAL_TYPE
3180 && INTEGRAL_TYPE_P (valtype
))
3181 warning_at (ploc
, OPT_Wtraditional_conversion
,
3182 "passing argument %d of %qE as floating rather "
3183 "than integer due to prototype",
3185 else if (TREE_CODE (type
) == COMPLEX_TYPE
3186 && INTEGRAL_TYPE_P (valtype
))
3187 warning_at (ploc
, OPT_Wtraditional_conversion
,
3188 "passing argument %d of %qE as complex rather "
3189 "than integer due to prototype",
3191 else if (TREE_CODE (type
) == REAL_TYPE
3192 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3193 warning_at (ploc
, OPT_Wtraditional_conversion
,
3194 "passing argument %d of %qE as floating rather "
3195 "than complex due to prototype",
3197 /* ??? At some point, messages should be written about
3198 conversions between complex types, but that's too messy
3200 else if (TREE_CODE (type
) == REAL_TYPE
3201 && TREE_CODE (valtype
) == REAL_TYPE
)
3203 /* Warn if any argument is passed as `float',
3204 since without a prototype it would be `double'. */
3205 if (formal_prec
== TYPE_PRECISION (float_type_node
)
3206 && type
!= dfloat32_type_node
)
3207 warning_at (ploc
, 0,
3208 "passing argument %d of %qE as %<float%> "
3209 "rather than %<double%> due to prototype",
3212 /* Warn if mismatch between argument and prototype
3213 for decimal float types. Warn of conversions with
3214 binary float types and of precision narrowing due to
3216 else if (type
!= valtype
3217 && (type
== dfloat32_type_node
3218 || type
== dfloat64_type_node
3219 || type
== dfloat128_type_node
3220 || valtype
== dfloat32_type_node
3221 || valtype
== dfloat64_type_node
3222 || valtype
== dfloat128_type_node
)
3224 <= TYPE_PRECISION (valtype
)
3225 || (type
== dfloat128_type_node
3227 != dfloat64_type_node
3229 != dfloat32_type_node
)))
3230 || (type
== dfloat64_type_node
3232 != dfloat32_type_node
))))
3233 warning_at (ploc
, 0,
3234 "passing argument %d of %qE as %qT "
3235 "rather than %qT due to prototype",
3236 argnum
, rname
, type
, valtype
);
3239 /* Detect integer changing in width or signedness.
3240 These warnings are only activated with
3241 -Wtraditional-conversion, not with -Wtraditional. */
3242 else if (warn_traditional_conversion
&& INTEGRAL_TYPE_P (type
)
3243 && INTEGRAL_TYPE_P (valtype
))
3245 tree would_have_been
= default_conversion (val
);
3246 tree type1
= TREE_TYPE (would_have_been
);
3248 if (TREE_CODE (type
) == ENUMERAL_TYPE
3249 && (TYPE_MAIN_VARIANT (type
)
3250 == TYPE_MAIN_VARIANT (valtype
)))
3251 /* No warning if function asks for enum
3252 and the actual arg is that enum type. */
3254 else if (formal_prec
!= TYPE_PRECISION (type1
))
3255 warning_at (ploc
, OPT_Wtraditional_conversion
,
3256 "passing argument %d of %qE "
3257 "with different width due to prototype",
3259 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
3261 /* Don't complain if the formal parameter type
3262 is an enum, because we can't tell now whether
3263 the value was an enum--even the same enum. */
3264 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3266 else if (TREE_CODE (val
) == INTEGER_CST
3267 && int_fits_type_p (val
, type
))
3268 /* Change in signedness doesn't matter
3269 if a constant value is unaffected. */
3271 /* If the value is extended from a narrower
3272 unsigned type, it doesn't matter whether we
3273 pass it as signed or unsigned; the value
3274 certainly is the same either way. */
3275 else if (TYPE_PRECISION (valtype
) < TYPE_PRECISION (type
)
3276 && TYPE_UNSIGNED (valtype
))
3278 else if (TYPE_UNSIGNED (type
))
3279 warning_at (ploc
, OPT_Wtraditional_conversion
,
3280 "passing argument %d of %qE "
3281 "as unsigned due to prototype",
3284 warning_at (ploc
, OPT_Wtraditional_conversion
,
3285 "passing argument %d of %qE "
3286 "as signed due to prototype",
3291 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3292 sake of better warnings from convert_and_check. */
3293 if (excess_precision
)
3294 val
= build1 (EXCESS_PRECISION_EXPR
, valtype
, val
);
3295 origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3296 parmval
= convert_for_assignment (loc
, ploc
, type
,
3297 val
, origtype
, ic_argpass
,
3298 npc
, fundecl
, function
,
3301 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
3302 && INTEGRAL_TYPE_P (type
)
3303 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3304 parmval
= default_conversion (parmval
);
3307 else if (TREE_CODE (valtype
) == REAL_TYPE
3308 && (TYPE_PRECISION (valtype
)
3309 <= TYPE_PRECISION (double_type_node
))
3310 && TYPE_MAIN_VARIANT (valtype
) != double_type_node
3311 && TYPE_MAIN_VARIANT (valtype
) != long_double_type_node
3312 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype
)))
3318 /* Convert `float' to `double'. */
3319 if (warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
3320 warning_at (ploc
, OPT_Wdouble_promotion
,
3321 "implicit conversion from %qT to %qT when passing "
3322 "argument to function",
3323 valtype
, double_type_node
);
3324 parmval
= convert (double_type_node
, val
);
3327 else if (excess_precision
&& !type_generic
)
3328 /* A "double" argument with excess precision being passed
3329 without a prototype or in variable arguments. */
3330 parmval
= convert (valtype
, val
);
3331 else if ((invalid_func_diag
=
3332 targetm
.calls
.invalid_arg_for_unprototyped_fn (typelist
, fundecl
, val
)))
3334 error (invalid_func_diag
);
3338 /* Convert `short' and `char' to full-size `int'. */
3339 parmval
= default_conversion (val
);
3341 (*values
)[parmnum
] = parmval
;
3342 if (parmval
== error_mark_node
)
3346 typetail
= TREE_CHAIN (typetail
);
3349 gcc_assert (parmnum
== vec_safe_length (values
));
3351 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
3353 error_at (loc
, "too few arguments to function %qE", function
);
3354 inform_declaration (fundecl
);
3358 return error_args
? -1 : (int) parmnum
;
3361 /* This is the entry point used by the parser to build unary operators
3362 in the input. CODE, a tree_code, specifies the unary operator, and
3363 ARG is the operand. For unary plus, the C parser currently uses
3364 CONVERT_EXPR for code.
3366 LOC is the location to use for the tree generated.
3370 parser_build_unary_op (location_t loc
, enum tree_code code
, struct c_expr arg
)
3372 struct c_expr result
;
3374 result
.value
= build_unary_op (loc
, code
, arg
.value
, 0);
3375 result
.original_code
= code
;
3376 result
.original_type
= NULL
;
3378 if (TREE_OVERFLOW_P (result
.value
) && !TREE_OVERFLOW_P (arg
.value
))
3379 overflow_warning (loc
, result
.value
);
3384 /* This is the entry point used by the parser to build binary operators
3385 in the input. CODE, a tree_code, specifies the binary operator, and
3386 ARG1 and ARG2 are the operands. In addition to constructing the
3387 expression, we check for operands that were written with other binary
3388 operators in a way that is likely to confuse the user.
3390 LOCATION is the location of the binary operator. */
3393 parser_build_binary_op (location_t location
, enum tree_code code
,
3394 struct c_expr arg1
, struct c_expr arg2
)
3396 struct c_expr result
;
3398 enum tree_code code1
= arg1
.original_code
;
3399 enum tree_code code2
= arg2
.original_code
;
3400 tree type1
= (arg1
.original_type
3401 ? arg1
.original_type
3402 : TREE_TYPE (arg1
.value
));
3403 tree type2
= (arg2
.original_type
3404 ? arg2
.original_type
3405 : TREE_TYPE (arg2
.value
));
3407 result
.value
= build_binary_op (location
, code
,
3408 arg1
.value
, arg2
.value
, 1);
3409 result
.original_code
= code
;
3410 result
.original_type
= NULL
;
3412 if (TREE_CODE (result
.value
) == ERROR_MARK
)
3415 if (location
!= UNKNOWN_LOCATION
)
3416 protected_set_expr_location (result
.value
, location
);
3418 /* Check for cases such as x+y<<z which users are likely
3420 if (warn_parentheses
)
3421 warn_about_parentheses (location
, code
, code1
, arg1
.value
, code2
,
3424 if (warn_logical_op
)
3425 warn_logical_operator (location
, code
, TREE_TYPE (result
.value
),
3426 code1
, arg1
.value
, code2
, arg2
.value
);
3428 if (warn_logical_not_paren
3429 && code1
== TRUTH_NOT_EXPR
3430 && code2
!= TRUTH_NOT_EXPR
)
3431 warn_logical_not_parentheses (location
, code
, arg2
.value
);
3433 /* Warn about comparisons against string literals, with the exception
3434 of testing for equality or inequality of a string literal with NULL. */
3435 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3437 if ((code1
== STRING_CST
&& !integer_zerop (arg2
.value
))
3438 || (code2
== STRING_CST
&& !integer_zerop (arg1
.value
)))
3439 warning_at (location
, OPT_Waddress
,
3440 "comparison with string literal results in unspecified behavior");
3442 else if (TREE_CODE_CLASS (code
) == tcc_comparison
3443 && (code1
== STRING_CST
|| code2
== STRING_CST
))
3444 warning_at (location
, OPT_Waddress
,
3445 "comparison with string literal results in unspecified behavior");
3447 if (TREE_OVERFLOW_P (result
.value
)
3448 && !TREE_OVERFLOW_P (arg1
.value
)
3449 && !TREE_OVERFLOW_P (arg2
.value
))
3450 overflow_warning (location
, result
.value
);
3452 /* Warn about comparisons of different enum types. */
3453 if (warn_enum_compare
3454 && TREE_CODE_CLASS (code
) == tcc_comparison
3455 && TREE_CODE (type1
) == ENUMERAL_TYPE
3456 && TREE_CODE (type2
) == ENUMERAL_TYPE
3457 && TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
3458 warning_at (location
, OPT_Wenum_compare
,
3459 "comparison between %qT and %qT",
3465 /* Return a tree for the difference of pointers OP0 and OP1.
3466 The resulting tree has type int. */
3469 pointer_diff (location_t loc
, tree op0
, tree op1
)
3471 tree restype
= ptrdiff_type_node
;
3472 tree result
, inttype
;
3474 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0
)));
3475 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1
)));
3476 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3477 tree orig_op1
= op1
;
3479 /* If the operands point into different address spaces, we need to
3480 explicitly convert them to pointers into the common address space
3481 before we can subtract the numerical address values. */
3484 addr_space_t as_common
;
3487 /* Determine the common superset address space. This is guaranteed
3488 to exist because the caller verified that comp_target_types
3489 returned non-zero. */
3490 if (!addr_space_superset (as0
, as1
, &as_common
))
3493 common_type
= common_pointer_type (TREE_TYPE (op0
), TREE_TYPE (op1
));
3494 op0
= convert (common_type
, op0
);
3495 op1
= convert (common_type
, op1
);
3498 /* Determine integer type to perform computations in. This will usually
3499 be the same as the result type (ptrdiff_t), but may need to be a wider
3500 type if pointers for the address space are wider than ptrdiff_t. */
3501 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
3502 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
3506 if (TREE_CODE (target_type
) == VOID_TYPE
)
3507 pedwarn (loc
, OPT_Wpointer_arith
,
3508 "pointer of type %<void *%> used in subtraction");
3509 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3510 pedwarn (loc
, OPT_Wpointer_arith
,
3511 "pointer to a function used in subtraction");
3513 /* First do the subtraction as integers;
3514 then drop through to build the divide operator.
3515 Do not do default conversions on the minus operator
3516 in case restype is a short type. */
3518 op0
= build_binary_op (loc
,
3519 MINUS_EXPR
, convert (inttype
, op0
),
3520 convert (inttype
, op1
), 0);
3521 /* This generates an error if op1 is pointer to incomplete type. */
3522 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
3523 error_at (loc
, "arithmetic on pointer to an incomplete type");
3525 op1
= c_size_in_bytes (target_type
);
3527 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1
)))
3528 error_at (loc
, "arithmetic on pointer to an empty aggregate");
3530 /* Divide by the size, in easiest possible way. */
3531 result
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, inttype
,
3532 op0
, convert (inttype
, op1
));
3534 /* Convert to final result type if necessary. */
3535 return convert (restype
, result
);
3538 /* Expand atomic compound assignments into an approriate sequence as
3539 specified by the C11 standard section 6.5.16.2.
3545 This sequence is used for all types for which these operations are
3548 In addition, built-in versions of the 'fe' prefixed routines may
3549 need to be invoked for floating point (real, complex or vector) when
3550 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3560 __atomic_load (addr, &old, SEQ_CST);
3561 feholdexcept (&fenv);
3563 newval = old op val;
3564 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3567 feclearexcept (FE_ALL_EXCEPT);
3570 feupdateenv (&fenv);
3572 Also note that the compiler is simply issuing the generic form of
3573 the atomic operations. This requires temp(s) and has their address
3574 taken. The atomic processing is smart enough to figure out when the
3575 size of an object can utilize a lock-free version, and convert the
3576 built-in call to the appropriate lock-free routine. The optimizers
3577 will then dispose of any temps that are no longer required, and
3578 lock-free implementations are utilized as long as there is target
3579 support for the required size.
3581 If the operator is NOP_EXPR, then this is a simple assignment, and
3582 an __atomic_store is issued to perform the assignment rather than
3587 /* Build an atomic assignment at LOC, expanding into the proper
3588 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3589 the result of the operation, unless RETURN_OLD_P in which case
3590 return the old value of LHS (this is only for postincrement and
3593 build_atomic_assign (location_t loc
, tree lhs
, enum tree_code modifycode
,
3594 tree rhs
, bool return_old_p
)
3596 tree fndecl
, func_call
;
3597 vec
<tree
, va_gc
> *params
;
3598 tree val
, nonatomic_lhs_type
, nonatomic_rhs_type
, newval
, newval_addr
;
3601 tree stmt
, goto_stmt
;
3602 tree loop_label
, loop_decl
, done_label
, done_decl
;
3604 tree lhs_type
= TREE_TYPE (lhs
);
3605 tree lhs_addr
= build_unary_op (loc
, ADDR_EXPR
, lhs
, 0);
3606 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
3607 tree rhs_type
= TREE_TYPE (rhs
);
3609 gcc_assert (TYPE_ATOMIC (lhs_type
));
3612 gcc_assert (modifycode
== PLUS_EXPR
|| modifycode
== MINUS_EXPR
);
3614 /* Allocate enough vector items for a compare_exchange. */
3615 vec_alloc (params
, 6);
3617 /* Create a compound statement to hold the sequence of statements
3619 compound_stmt
= c_begin_compound_stmt (false);
3621 /* Fold the RHS if it hasn't already been folded. */
3622 if (modifycode
!= NOP_EXPR
)
3623 rhs
= c_fully_fold (rhs
, false, NULL
);
3625 /* Remove the qualifiers for the rest of the expressions and create
3626 the VAL temp variable to hold the RHS. */
3627 nonatomic_lhs_type
= build_qualified_type (lhs_type
, TYPE_UNQUALIFIED
);
3628 nonatomic_rhs_type
= build_qualified_type (rhs_type
, TYPE_UNQUALIFIED
);
3629 val
= create_tmp_var (nonatomic_rhs_type
, NULL
);
3630 TREE_ADDRESSABLE (val
) = 1;
3631 TREE_NO_WARNING (val
) = 1;
3632 rhs
= build2 (MODIFY_EXPR
, nonatomic_rhs_type
, val
, rhs
);
3633 SET_EXPR_LOCATION (rhs
, loc
);
3636 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3638 if (modifycode
== NOP_EXPR
)
3640 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3641 rhs
= build_unary_op (loc
, ADDR_EXPR
, val
, 0);
3642 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
3643 params
->quick_push (lhs_addr
);
3644 params
->quick_push (rhs
);
3645 params
->quick_push (seq_cst
);
3646 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3647 add_stmt (func_call
);
3649 /* Finish the compound statement. */
3650 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3652 /* VAL is the value which was stored, return a COMPOUND_STMT of
3653 the statement and that value. */
3654 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, val
);
3657 /* Create the variables and labels required for the op= form. */
3658 old
= create_tmp_var (nonatomic_lhs_type
, NULL
);
3659 old_addr
= build_unary_op (loc
, ADDR_EXPR
, old
, 0);
3660 TREE_ADDRESSABLE (old
) = 1;
3661 TREE_NO_WARNING (old
) = 1;
3663 newval
= create_tmp_var (nonatomic_lhs_type
, NULL
);
3664 newval_addr
= build_unary_op (loc
, ADDR_EXPR
, newval
, 0);
3665 TREE_ADDRESSABLE (newval
) = 1;
3667 loop_decl
= create_artificial_label (loc
);
3668 loop_label
= build1 (LABEL_EXPR
, void_type_node
, loop_decl
);
3670 done_decl
= create_artificial_label (loc
);
3671 done_label
= build1 (LABEL_EXPR
, void_type_node
, done_decl
);
3673 /* __atomic_load (addr, &old, SEQ_CST). */
3674 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
3675 params
->quick_push (lhs_addr
);
3676 params
->quick_push (old_addr
);
3677 params
->quick_push (seq_cst
);
3678 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3679 add_stmt (func_call
);
3680 params
->truncate (0);
3682 /* Create the expressions for floating-point environment
3683 manipulation, if required. */
3684 bool need_fenv
= (flag_trapping_math
3685 && (FLOAT_TYPE_P (lhs_type
) || FLOAT_TYPE_P (rhs_type
)));
3686 tree hold_call
= NULL_TREE
, clear_call
= NULL_TREE
, update_call
= NULL_TREE
;
3688 targetm
.atomic_assign_expand_fenv (&hold_call
, &clear_call
, &update_call
);
3691 add_stmt (hold_call
);
3694 add_stmt (loop_label
);
3696 /* newval = old + val; */
3697 rhs
= build_binary_op (loc
, modifycode
, old
, val
, 1);
3698 rhs
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, nonatomic_lhs_type
,
3699 rhs
, NULL_TREE
, ic_assign
, false, NULL_TREE
,
3701 if (rhs
!= error_mark_node
)
3703 rhs
= build2 (MODIFY_EXPR
, nonatomic_lhs_type
, newval
, rhs
);
3704 SET_EXPR_LOCATION (rhs
, loc
);
3708 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3710 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE
);
3711 params
->quick_push (lhs_addr
);
3712 params
->quick_push (old_addr
);
3713 params
->quick_push (newval_addr
);
3714 params
->quick_push (integer_zero_node
);
3715 params
->quick_push (seq_cst
);
3716 params
->quick_push (seq_cst
);
3717 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3719 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, done_decl
);
3720 SET_EXPR_LOCATION (goto_stmt
, loc
);
3722 stmt
= build3 (COND_EXPR
, void_type_node
, func_call
, goto_stmt
, NULL_TREE
);
3723 SET_EXPR_LOCATION (stmt
, loc
);
3727 add_stmt (clear_call
);
3730 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, loop_decl
);
3731 SET_EXPR_LOCATION (goto_stmt
, loc
);
3732 add_stmt (goto_stmt
);
3735 add_stmt (done_label
);
3738 add_stmt (update_call
);
3740 /* Finish the compound statement. */
3741 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3743 /* NEWVAL is the value that was successfully stored, return a
3744 COMPOUND_EXPR of the statement and the appropriate value. */
3745 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
,
3746 return_old_p
? old
: newval
);
3749 /* Construct and perhaps optimize a tree representation
3750 for a unary operation. CODE, a tree_code, specifies the operation
3751 and XARG is the operand.
3752 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3753 the default promotions (such as from short to int).
3754 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3755 allows non-lvalues; this is only used to handle conversion of non-lvalue
3756 arrays to pointers in C99.
3758 LOCATION is the location of the operator. */
3761 build_unary_op (location_t location
,
3762 enum tree_code code
, tree xarg
, int flag
)
3764 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3767 enum tree_code typecode
;
3769 tree ret
= error_mark_node
;
3770 tree eptype
= NULL_TREE
;
3771 int noconvert
= flag
;
3772 const char *invalid_op_diag
;
3775 int_operands
= EXPR_INT_CONST_OPERANDS (xarg
);
3777 arg
= remove_c_maybe_const_expr (arg
);
3779 if (code
!= ADDR_EXPR
)
3780 arg
= require_complete_type (arg
);
3782 typecode
= TREE_CODE (TREE_TYPE (arg
));
3783 if (typecode
== ERROR_MARK
)
3784 return error_mark_node
;
3785 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
3786 typecode
= INTEGER_TYPE
;
3788 if ((invalid_op_diag
3789 = targetm
.invalid_unary_op (code
, TREE_TYPE (xarg
))))
3791 error_at (location
, invalid_op_diag
);
3792 return error_mark_node
;
3795 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
3797 eptype
= TREE_TYPE (arg
);
3798 arg
= TREE_OPERAND (arg
, 0);
3804 /* This is used for unary plus, because a CONVERT_EXPR
3805 is enough to prevent anybody from looking inside for
3806 associativity, but won't generate any code. */
3807 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3808 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3809 || typecode
== VECTOR_TYPE
))
3811 error_at (location
, "wrong type argument to unary plus");
3812 return error_mark_node
;
3814 else if (!noconvert
)
3815 arg
= default_conversion (arg
);
3816 arg
= non_lvalue_loc (location
, arg
);
3820 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3821 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3822 || typecode
== VECTOR_TYPE
))
3824 error_at (location
, "wrong type argument to unary minus");
3825 return error_mark_node
;
3827 else if (!noconvert
)
3828 arg
= default_conversion (arg
);
3832 /* ~ works on integer types and non float vectors. */
3833 if (typecode
== INTEGER_TYPE
3834 || (typecode
== VECTOR_TYPE
3835 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg
))))
3838 arg
= default_conversion (arg
);
3840 else if (typecode
== COMPLEX_TYPE
)
3843 pedwarn (location
, OPT_Wpedantic
,
3844 "ISO C does not support %<~%> for complex conjugation");
3846 arg
= default_conversion (arg
);
3850 error_at (location
, "wrong type argument to bit-complement");
3851 return error_mark_node
;
3856 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
3858 error_at (location
, "wrong type argument to abs");
3859 return error_mark_node
;
3861 else if (!noconvert
)
3862 arg
= default_conversion (arg
);
3866 /* Conjugating a real value is a no-op, but allow it anyway. */
3867 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3868 || typecode
== COMPLEX_TYPE
))
3870 error_at (location
, "wrong type argument to conjugation");
3871 return error_mark_node
;
3873 else if (!noconvert
)
3874 arg
= default_conversion (arg
);
3877 case TRUTH_NOT_EXPR
:
3878 if (typecode
!= INTEGER_TYPE
&& typecode
!= FIXED_POINT_TYPE
3879 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
3880 && typecode
!= COMPLEX_TYPE
)
3883 "wrong type argument to unary exclamation mark");
3884 return error_mark_node
;
3888 arg
= c_objc_common_truthvalue_conversion (location
, xarg
);
3889 arg
= remove_c_maybe_const_expr (arg
);
3892 arg
= c_objc_common_truthvalue_conversion (location
, arg
);
3893 ret
= invert_truthvalue_loc (location
, arg
);
3894 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3895 if (EXPR_P (ret
) && EXPR_HAS_LOCATION (ret
))
3896 location
= EXPR_LOCATION (ret
);
3897 goto return_build_unary_op
;
3901 ret
= build_real_imag_expr (location
, code
, arg
);
3902 if (ret
== error_mark_node
)
3903 return error_mark_node
;
3904 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
3905 eptype
= TREE_TYPE (eptype
);
3906 goto return_build_unary_op
;
3908 case PREINCREMENT_EXPR
:
3909 case POSTINCREMENT_EXPR
:
3910 case PREDECREMENT_EXPR
:
3911 case POSTDECREMENT_EXPR
:
3913 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
3915 tree inner
= build_unary_op (location
, code
,
3916 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
3917 if (inner
== error_mark_node
)
3918 return error_mark_node
;
3919 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
3920 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
3921 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
3922 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = 1;
3923 goto return_build_unary_op
;
3926 /* Complain about anything that is not a true lvalue. In
3927 Objective-C, skip this check for property_refs. */
3928 if (!objc_is_property_ref (arg
)
3929 && !lvalue_or_else (location
,
3930 arg
, ((code
== PREINCREMENT_EXPR
3931 || code
== POSTINCREMENT_EXPR
)
3934 return error_mark_node
;
3936 if (warn_cxx_compat
&& TREE_CODE (TREE_TYPE (arg
)) == ENUMERAL_TYPE
)
3938 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3939 warning_at (location
, OPT_Wc___compat
,
3940 "increment of enumeration value is invalid in C++");
3942 warning_at (location
, OPT_Wc___compat
,
3943 "decrement of enumeration value is invalid in C++");
3946 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3947 arg
= c_fully_fold (arg
, false, NULL
);
3950 atomic_op
= really_atomic_lvalue (arg
);
3952 /* Increment or decrement the real part of the value,
3953 and don't change the imaginary part. */
3954 if (typecode
== COMPLEX_TYPE
)
3958 pedwarn (location
, OPT_Wpedantic
,
3959 "ISO C does not support %<++%> and %<--%> on complex types");
3963 arg
= stabilize_reference (arg
);
3964 real
= build_unary_op (EXPR_LOCATION (arg
), REALPART_EXPR
, arg
, 1);
3965 imag
= build_unary_op (EXPR_LOCATION (arg
), IMAGPART_EXPR
, arg
, 1);
3966 real
= build_unary_op (EXPR_LOCATION (arg
), code
, real
, 1);
3967 if (real
== error_mark_node
|| imag
== error_mark_node
)
3968 return error_mark_node
;
3969 ret
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
3971 goto return_build_unary_op
;
3975 /* Report invalid types. */
3977 if (typecode
!= POINTER_TYPE
&& typecode
!= FIXED_POINT_TYPE
3978 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
3979 && typecode
!= COMPLEX_TYPE
&& typecode
!= VECTOR_TYPE
)
3981 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3982 error_at (location
, "wrong type argument to increment");
3984 error_at (location
, "wrong type argument to decrement");
3986 return error_mark_node
;
3992 argtype
= TREE_TYPE (arg
);
3994 /* Compute the increment. */
3996 if (typecode
== POINTER_TYPE
)
3998 /* If pointer target is an incomplete type,
3999 we just cannot know how to do the arithmetic. */
4000 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype
)))
4002 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4004 "increment of pointer to an incomplete type %qT",
4005 TREE_TYPE (argtype
));
4008 "decrement of pointer to an incomplete type %qT",
4009 TREE_TYPE (argtype
));
4011 else if (TREE_CODE (TREE_TYPE (argtype
)) == FUNCTION_TYPE
4012 || TREE_CODE (TREE_TYPE (argtype
)) == VOID_TYPE
)
4014 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4015 pedwarn (location
, OPT_Wpointer_arith
,
4016 "wrong type argument to increment");
4018 pedwarn (location
, OPT_Wpointer_arith
,
4019 "wrong type argument to decrement");
4022 inc
= c_size_in_bytes (TREE_TYPE (argtype
));
4023 inc
= convert_to_ptrofftype_loc (location
, inc
);
4025 else if (FRACT_MODE_P (TYPE_MODE (argtype
)))
4027 /* For signed fract types, we invert ++ to -- or
4028 -- to ++, and change inc from 1 to -1, because
4029 it is not possible to represent 1 in signed fract constants.
4030 For unsigned fract types, the result always overflows and
4031 we get an undefined (original) or the maximum value. */
4032 if (code
== PREINCREMENT_EXPR
)
4033 code
= PREDECREMENT_EXPR
;
4034 else if (code
== PREDECREMENT_EXPR
)
4035 code
= PREINCREMENT_EXPR
;
4036 else if (code
== POSTINCREMENT_EXPR
)
4037 code
= POSTDECREMENT_EXPR
;
4038 else /* code == POSTDECREMENT_EXPR */
4039 code
= POSTINCREMENT_EXPR
;
4041 inc
= integer_minus_one_node
;
4042 inc
= convert (argtype
, inc
);
4046 inc
= VECTOR_TYPE_P (argtype
)
4047 ? build_one_cst (argtype
)
4049 inc
= convert (argtype
, inc
);
4052 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4053 need to ask Objective-C to build the increment or decrement
4054 expression for it. */
4055 if (objc_is_property_ref (arg
))
4056 return objc_build_incr_expr_for_property_ref (location
, code
,
4059 /* Report a read-only lvalue. */
4060 if (TYPE_READONLY (argtype
))
4062 readonly_error (location
, arg
,
4063 ((code
== PREINCREMENT_EXPR
4064 || code
== POSTINCREMENT_EXPR
)
4065 ? lv_increment
: lv_decrement
));
4066 return error_mark_node
;
4068 else if (TREE_READONLY (arg
))
4069 readonly_warning (arg
,
4070 ((code
== PREINCREMENT_EXPR
4071 || code
== POSTINCREMENT_EXPR
)
4072 ? lv_increment
: lv_decrement
));
4074 /* If the argument is atomic, use the special code sequences for
4075 atomic compound assignment. */
4078 arg
= stabilize_reference (arg
);
4079 ret
= build_atomic_assign (location
, arg
,
4080 ((code
== PREINCREMENT_EXPR
4081 || code
== POSTINCREMENT_EXPR
)
4084 (FRACT_MODE_P (TYPE_MODE (argtype
))
4086 : integer_one_node
),
4087 (code
== POSTINCREMENT_EXPR
4088 || code
== POSTDECREMENT_EXPR
));
4089 goto return_build_unary_op
;
4092 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4093 val
= boolean_increment (code
, arg
);
4095 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
4096 TREE_SIDE_EFFECTS (val
) = 1;
4097 if (TREE_CODE (val
) != code
)
4098 TREE_NO_WARNING (val
) = 1;
4100 goto return_build_unary_op
;
4104 /* Note that this operation never does default_conversion. */
4106 /* The operand of unary '&' must be an lvalue (which excludes
4107 expressions of type void), or, in C99, the result of a [] or
4108 unary '*' operator. */
4109 if (VOID_TYPE_P (TREE_TYPE (arg
))
4110 && TYPE_QUALS (TREE_TYPE (arg
)) == TYPE_UNQUALIFIED
4111 && (TREE_CODE (arg
) != INDIRECT_REF
4113 pedwarn (location
, 0, "taking address of expression of type %<void%>");
4115 /* Let &* cancel out to simplify resulting code. */
4116 if (TREE_CODE (arg
) == INDIRECT_REF
)
4118 /* Don't let this be an lvalue. */
4119 if (lvalue_p (TREE_OPERAND (arg
, 0)))
4120 return non_lvalue_loc (location
, TREE_OPERAND (arg
, 0));
4121 ret
= TREE_OPERAND (arg
, 0);
4122 goto return_build_unary_op
;
4125 /* For &x[y], return x+y */
4126 if (TREE_CODE (arg
) == ARRAY_REF
)
4128 tree op0
= TREE_OPERAND (arg
, 0);
4129 if (!c_mark_addressable (op0
))
4130 return error_mark_node
;
4133 /* Anything not already handled and not a true memory reference
4134 or a non-lvalue array is an error. */
4135 else if (typecode
!= FUNCTION_TYPE
&& !flag
4136 && !lvalue_or_else (location
, arg
, lv_addressof
))
4137 return error_mark_node
;
4139 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4141 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4143 tree inner
= build_unary_op (location
, code
,
4144 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
4145 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4146 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4147 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4148 C_MAYBE_CONST_EXPR_NON_CONST (ret
)
4149 = C_MAYBE_CONST_EXPR_NON_CONST (arg
);
4150 goto return_build_unary_op
;
4153 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4154 argtype
= TREE_TYPE (arg
);
4156 /* If the lvalue is const or volatile, merge that into the type
4157 to which the address will point. This is only needed
4158 for function types. */
4159 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
4160 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4161 && TREE_CODE (argtype
) == FUNCTION_TYPE
)
4163 int orig_quals
= TYPE_QUALS (strip_array_types (argtype
));
4164 int quals
= orig_quals
;
4166 if (TREE_READONLY (arg
))
4167 quals
|= TYPE_QUAL_CONST
;
4168 if (TREE_THIS_VOLATILE (arg
))
4169 quals
|= TYPE_QUAL_VOLATILE
;
4171 argtype
= c_build_qualified_type (argtype
, quals
);
4174 if (!c_mark_addressable (arg
))
4175 return error_mark_node
;
4177 gcc_assert (TREE_CODE (arg
) != COMPONENT_REF
4178 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)));
4180 argtype
= build_pointer_type (argtype
);
4182 /* ??? Cope with user tricks that amount to offsetof. Delete this
4183 when we have proper support for integer constant expressions. */
4184 val
= get_base_address (arg
);
4185 if (val
&& TREE_CODE (val
) == INDIRECT_REF
4186 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
4188 ret
= fold_convert_loc (location
, argtype
, fold_offsetof_1 (arg
));
4189 goto return_build_unary_op
;
4192 val
= build1 (ADDR_EXPR
, argtype
, arg
);
4195 goto return_build_unary_op
;
4202 argtype
= TREE_TYPE (arg
);
4203 if (TREE_CODE (arg
) == INTEGER_CST
)
4204 ret
= (require_constant_value
4205 ? fold_build1_initializer_loc (location
, code
, argtype
, arg
)
4206 : fold_build1_loc (location
, code
, argtype
, arg
));
4208 ret
= build1 (code
, argtype
, arg
);
4209 return_build_unary_op
:
4210 gcc_assert (ret
!= error_mark_node
);
4211 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
)
4212 && !(TREE_CODE (xarg
) == INTEGER_CST
&& !TREE_OVERFLOW (xarg
)))
4213 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
4214 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
)
4215 ret
= note_integer_operands (ret
);
4217 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4218 protected_set_expr_location (ret
, location
);
4222 /* Return nonzero if REF is an lvalue valid for this language.
4223 Lvalues can be assigned, unless their type has TYPE_READONLY.
4224 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4227 lvalue_p (const_tree ref
)
4229 const enum tree_code code
= TREE_CODE (ref
);
4236 return lvalue_p (TREE_OPERAND (ref
, 0));
4238 case C_MAYBE_CONST_EXPR
:
4239 return lvalue_p (TREE_OPERAND (ref
, 1));
4241 case COMPOUND_LITERAL_EXPR
:
4247 case ARRAY_NOTATION_REF
:
4252 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
4253 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
4256 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
4263 /* Give a warning for storing in something that is read-only in GCC
4264 terms but not const in ISO C terms. */
4267 readonly_warning (tree arg
, enum lvalue_use use
)
4272 warning (0, "assignment of read-only location %qE", arg
);
4275 warning (0, "increment of read-only location %qE", arg
);
4278 warning (0, "decrement of read-only location %qE", arg
);
4287 /* Return nonzero if REF is an lvalue valid for this language;
4288 otherwise, print an error message and return zero. USE says
4289 how the lvalue is being used and so selects the error message.
4290 LOCATION is the location at which any error should be reported. */
4293 lvalue_or_else (location_t loc
, const_tree ref
, enum lvalue_use use
)
4295 int win
= lvalue_p (ref
);
4298 lvalue_error (loc
, use
);
4303 /* Mark EXP saying that we need to be able to take the
4304 address of it; it should not be allocated in a register.
4305 Returns true if successful. */
4308 c_mark_addressable (tree exp
)
4313 switch (TREE_CODE (x
))
4316 if (DECL_C_BIT_FIELD (TREE_OPERAND (x
, 1)))
4319 ("cannot take address of bit-field %qD", TREE_OPERAND (x
, 1));
4323 /* ... fall through ... */
4329 x
= TREE_OPERAND (x
, 0);
4332 case COMPOUND_LITERAL_EXPR
:
4334 TREE_ADDRESSABLE (x
) = 1;
4341 if (C_DECL_REGISTER (x
)
4342 && DECL_NONLOCAL (x
))
4344 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
4347 ("global register variable %qD used in nested function", x
);
4350 pedwarn (input_location
, 0, "register variable %qD used in nested function", x
);
4352 else if (C_DECL_REGISTER (x
))
4354 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
4355 error ("address of global register variable %qD requested", x
);
4357 error ("address of register variable %qD requested", x
);
4363 TREE_ADDRESSABLE (x
) = 1;
4370 /* Convert EXPR to TYPE, warning about conversion problems with
4371 constants. SEMANTIC_TYPE is the type this conversion would use
4372 without excess precision. If SEMANTIC_TYPE is NULL, this function
4373 is equivalent to convert_and_check. This function is a wrapper that
4374 handles conversions that may be different than
4375 the usual ones because of excess precision. */
4378 ep_convert_and_check (location_t loc
, tree type
, tree expr
,
4381 if (TREE_TYPE (expr
) == type
)
4385 return convert_and_check (loc
, type
, expr
);
4387 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
4388 && TREE_TYPE (expr
) != semantic_type
)
4390 /* For integers, we need to check the real conversion, not
4391 the conversion to the excess precision type. */
4392 expr
= convert_and_check (loc
, semantic_type
, expr
);
4394 /* Result type is the excess precision type, which should be
4395 large enough, so do not check. */
4396 return convert (type
, expr
);
4399 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4400 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4401 if folded to an integer constant then the unselected half may
4402 contain arbitrary operations not normally permitted in constant
4403 expressions. Set the location of the expression to LOC. */
4406 build_conditional_expr (location_t colon_loc
, tree ifexp
, bool ifexp_bcp
,
4407 tree op1
, tree op1_original_type
, tree op2
,
4408 tree op2_original_type
)
4412 enum tree_code code1
;
4413 enum tree_code code2
;
4414 tree result_type
= NULL
;
4415 tree semantic_result_type
= NULL
;
4416 tree orig_op1
= op1
, orig_op2
= op2
;
4417 bool int_const
, op1_int_operands
, op2_int_operands
, int_operands
;
4418 bool ifexp_int_operands
;
4421 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
4422 if (op1_int_operands
)
4423 op1
= remove_c_maybe_const_expr (op1
);
4424 op2_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op2
);
4425 if (op2_int_operands
)
4426 op2
= remove_c_maybe_const_expr (op2
);
4427 ifexp_int_operands
= EXPR_INT_CONST_OPERANDS (ifexp
);
4428 if (ifexp_int_operands
)
4429 ifexp
= remove_c_maybe_const_expr (ifexp
);
4431 /* Promote both alternatives. */
4433 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
4434 op1
= default_conversion (op1
);
4435 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
4436 op2
= default_conversion (op2
);
4438 if (TREE_CODE (ifexp
) == ERROR_MARK
4439 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
4440 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
4441 return error_mark_node
;
4443 type1
= TREE_TYPE (op1
);
4444 code1
= TREE_CODE (type1
);
4445 type2
= TREE_TYPE (op2
);
4446 code2
= TREE_CODE (type2
);
4448 /* C90 does not permit non-lvalue arrays in conditional expressions.
4449 In C99 they will be pointers by now. */
4450 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
4452 error_at (colon_loc
, "non-lvalue array in conditional expression");
4453 return error_mark_node
;
4456 if ((TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
4457 || TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4458 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4459 || code1
== COMPLEX_TYPE
)
4460 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4461 || code2
== COMPLEX_TYPE
))
4463 semantic_result_type
= c_common_type (type1
, type2
);
4464 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
4466 op1
= TREE_OPERAND (op1
, 0);
4467 type1
= TREE_TYPE (op1
);
4468 gcc_assert (TREE_CODE (type1
) == code1
);
4470 if (TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4472 op2
= TREE_OPERAND (op2
, 0);
4473 type2
= TREE_TYPE (op2
);
4474 gcc_assert (TREE_CODE (type2
) == code2
);
4478 if (warn_cxx_compat
)
4480 tree t1
= op1_original_type
? op1_original_type
: TREE_TYPE (orig_op1
);
4481 tree t2
= op2_original_type
? op2_original_type
: TREE_TYPE (orig_op2
);
4483 if (TREE_CODE (t1
) == ENUMERAL_TYPE
4484 && TREE_CODE (t2
) == ENUMERAL_TYPE
4485 && TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
4486 warning_at (colon_loc
, OPT_Wc___compat
,
4487 ("different enum types in conditional is "
4488 "invalid in C++: %qT vs %qT"),
4492 /* Quickly detect the usual case where op1 and op2 have the same type
4494 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
4497 result_type
= type1
;
4499 result_type
= TYPE_MAIN_VARIANT (type1
);
4501 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4502 || code1
== COMPLEX_TYPE
)
4503 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4504 || code2
== COMPLEX_TYPE
))
4506 result_type
= c_common_type (type1
, type2
);
4507 do_warn_double_promotion (result_type
, type1
, type2
,
4508 "implicit conversion from %qT to %qT to "
4509 "match other result of conditional",
4512 /* If -Wsign-compare, warn here if type1 and type2 have
4513 different signedness. We'll promote the signed to unsigned
4514 and later code won't know it used to be different.
4515 Do this check on the original types, so that explicit casts
4516 will be considered, but default promotions won't. */
4517 if (c_inhibit_evaluation_warnings
== 0)
4519 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
4520 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
4522 if (unsigned_op1
^ unsigned_op2
)
4526 /* Do not warn if the result type is signed, since the
4527 signed type will only be chosen if it can represent
4528 all the values of the unsigned type. */
4529 if (!TYPE_UNSIGNED (result_type
))
4533 bool op1_maybe_const
= true;
4534 bool op2_maybe_const
= true;
4536 /* Do not warn if the signed quantity is an
4537 unsuffixed integer literal (or some static
4538 constant expression involving such literals) and
4539 it is non-negative. This warning requires the
4540 operands to be folded for best results, so do
4541 that folding in this case even without
4542 warn_sign_compare to avoid warning options
4543 possibly affecting code generation. */
4544 c_inhibit_evaluation_warnings
4545 += (ifexp
== truthvalue_false_node
);
4546 op1
= c_fully_fold (op1
, require_constant_value
,
4548 c_inhibit_evaluation_warnings
4549 -= (ifexp
== truthvalue_false_node
);
4551 c_inhibit_evaluation_warnings
4552 += (ifexp
== truthvalue_true_node
);
4553 op2
= c_fully_fold (op2
, require_constant_value
,
4555 c_inhibit_evaluation_warnings
4556 -= (ifexp
== truthvalue_true_node
);
4558 if (warn_sign_compare
)
4561 && tree_expr_nonnegative_warnv_p (op1
, &ovf
))
4563 && tree_expr_nonnegative_warnv_p (op2
, &ovf
)))
4566 warning_at (colon_loc
, OPT_Wsign_compare
,
4567 ("signed and unsigned type in "
4568 "conditional expression"));
4570 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
4571 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
4572 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
4573 op2
= c_wrap_maybe_const (op2
, !op2_maybe_const
);
4578 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
4580 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
4581 pedwarn (colon_loc
, OPT_Wpedantic
,
4582 "ISO C forbids conditional expr with only one void side");
4583 result_type
= void_type_node
;
4585 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
4587 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
4588 addr_space_t as2
= TYPE_ADDR_SPACE (TREE_TYPE (type2
));
4589 addr_space_t as_common
;
4591 if (comp_target_types (colon_loc
, type1
, type2
))
4592 result_type
= common_pointer_type (type1
, type2
);
4593 else if (null_pointer_constant_p (orig_op1
))
4594 result_type
= type2
;
4595 else if (null_pointer_constant_p (orig_op2
))
4596 result_type
= type1
;
4597 else if (!addr_space_superset (as1
, as2
, &as_common
))
4599 error_at (colon_loc
, "pointers to disjoint address spaces "
4600 "used in conditional expression");
4601 return error_mark_node
;
4603 else if (VOID_TYPE_P (TREE_TYPE (type1
))
4604 && !TYPE_ATOMIC (TREE_TYPE (type1
)))
4606 if (TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
4607 pedwarn (colon_loc
, OPT_Wpedantic
,
4608 "ISO C forbids conditional expr between "
4609 "%<void *%> and function pointer");
4610 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
4611 TREE_TYPE (type2
)));
4613 else if (VOID_TYPE_P (TREE_TYPE (type2
))
4614 && !TYPE_ATOMIC (TREE_TYPE (type2
)))
4616 if (TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
4617 pedwarn (colon_loc
, OPT_Wpedantic
,
4618 "ISO C forbids conditional expr between "
4619 "%<void *%> and function pointer");
4620 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
4621 TREE_TYPE (type1
)));
4623 /* Objective-C pointer comparisons are a bit more lenient. */
4624 else if (objc_have_common_type (type1
, type2
, -3, NULL_TREE
))
4625 result_type
= objc_common_type (type1
, type2
);
4628 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
4630 pedwarn (colon_loc
, 0,
4631 "pointer type mismatch in conditional expression");
4632 result_type
= build_pointer_type
4633 (build_qualified_type (void_type_node
, qual
));
4636 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
4638 if (!null_pointer_constant_p (orig_op2
))
4639 pedwarn (colon_loc
, 0,
4640 "pointer/integer type mismatch in conditional expression");
4643 op2
= null_pointer_node
;
4645 result_type
= type1
;
4647 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4649 if (!null_pointer_constant_p (orig_op1
))
4650 pedwarn (colon_loc
, 0,
4651 "pointer/integer type mismatch in conditional expression");
4654 op1
= null_pointer_node
;
4656 result_type
= type2
;
4661 if (flag_cond_mismatch
)
4662 result_type
= void_type_node
;
4665 error_at (colon_loc
, "type mismatch in conditional expression");
4666 return error_mark_node
;
4670 /* Merge const and volatile flags of the incoming types. */
4672 = build_type_variant (result_type
,
4673 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
4674 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
4676 op1
= ep_convert_and_check (colon_loc
, result_type
, op1
,
4677 semantic_result_type
);
4678 op2
= ep_convert_and_check (colon_loc
, result_type
, op2
,
4679 semantic_result_type
);
4681 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
4683 op2_int_operands
= true;
4684 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
4686 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
4688 op1_int_operands
= true;
4689 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
4691 int_const
= int_operands
= (ifexp_int_operands
4693 && op2_int_operands
);
4696 int_const
= ((ifexp
== truthvalue_true_node
4697 && TREE_CODE (orig_op1
) == INTEGER_CST
4698 && !TREE_OVERFLOW (orig_op1
))
4699 || (ifexp
== truthvalue_false_node
4700 && TREE_CODE (orig_op2
) == INTEGER_CST
4701 && !TREE_OVERFLOW (orig_op2
)));
4703 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
4704 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4709 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4710 nested inside of the expression. */
4711 op1
= c_fully_fold (op1
, false, NULL
);
4712 op2
= c_fully_fold (op2
, false, NULL
);
4714 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4716 ret
= note_integer_operands (ret
);
4718 if (semantic_result_type
)
4719 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
4721 protected_set_expr_location (ret
, colon_loc
);
4725 /* Return a compound expression that performs two expressions and
4726 returns the value of the second of them.
4728 LOC is the location of the COMPOUND_EXPR. */
4731 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
4733 bool expr1_int_operands
, expr2_int_operands
;
4734 tree eptype
= NULL_TREE
;
4738 && (TREE_CODE (expr1
) == CILK_SPAWN_STMT
4739 || TREE_CODE (expr2
) == CILK_SPAWN_STMT
))
4742 "spawned function call cannot be part of a comma expression");
4743 return error_mark_node
;
4745 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
4746 if (expr1_int_operands
)
4747 expr1
= remove_c_maybe_const_expr (expr1
);
4748 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
4749 if (expr2_int_operands
)
4750 expr2
= remove_c_maybe_const_expr (expr2
);
4752 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
4753 expr1
= TREE_OPERAND (expr1
, 0);
4754 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
4756 eptype
= TREE_TYPE (expr2
);
4757 expr2
= TREE_OPERAND (expr2
, 0);
4760 if (!TREE_SIDE_EFFECTS (expr1
))
4762 /* The left-hand operand of a comma expression is like an expression
4763 statement: with -Wunused, we should warn if it doesn't have
4764 any side-effects, unless it was explicitly cast to (void). */
4765 if (warn_unused_value
)
4767 if (VOID_TYPE_P (TREE_TYPE (expr1
))
4768 && CONVERT_EXPR_P (expr1
))
4770 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
4771 && TREE_CODE (expr1
) == COMPOUND_EXPR
4772 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
4773 ; /* (void) a, (void) b, c */
4775 warning_at (loc
, OPT_Wunused_value
,
4776 "left-hand operand of comma expression has no effect");
4779 else if (TREE_CODE (expr1
) == COMPOUND_EXPR
4780 && warn_unused_value
)
4783 location_t cloc
= loc
;
4784 while (TREE_CODE (r
) == COMPOUND_EXPR
)
4786 if (EXPR_HAS_LOCATION (r
))
4787 cloc
= EXPR_LOCATION (r
);
4788 r
= TREE_OPERAND (r
, 1);
4790 if (!TREE_SIDE_EFFECTS (r
)
4791 && !VOID_TYPE_P (TREE_TYPE (r
))
4792 && !CONVERT_EXPR_P (r
))
4793 warning_at (cloc
, OPT_Wunused_value
,
4794 "right-hand operand of comma expression has no effect");
4797 /* With -Wunused, we should also warn if the left-hand operand does have
4798 side-effects, but computes a value which is not used. For example, in
4799 `foo() + bar(), baz()' the result of the `+' operator is not used,
4800 so we should issue a warning. */
4801 else if (warn_unused_value
)
4802 warn_if_unused_value (expr1
, loc
);
4804 if (expr2
== error_mark_node
)
4805 return error_mark_node
;
4807 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
4810 && expr1_int_operands
4811 && expr2_int_operands
)
4812 ret
= note_integer_operands (ret
);
4815 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4817 protected_set_expr_location (ret
, loc
);
4821 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4822 which we are casting. OTYPE is the type of the expression being
4823 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4824 of the cast. -Wcast-qual appeared on the command line. Named
4825 address space qualifiers are not handled here, because they result
4826 in different warnings. */
4829 handle_warn_cast_qual (location_t loc
, tree type
, tree otype
)
4831 tree in_type
= type
;
4832 tree in_otype
= otype
;
4837 /* Check that the qualifiers on IN_TYPE are a superset of the
4838 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4839 nodes is uninteresting and we stop as soon as we hit a
4840 non-POINTER_TYPE node on either type. */
4843 in_otype
= TREE_TYPE (in_otype
);
4844 in_type
= TREE_TYPE (in_type
);
4846 /* GNU C allows cv-qualified function types. 'const' means the
4847 function is very pure, 'volatile' means it can't return. We
4848 need to warn when such qualifiers are added, not when they're
4850 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
4851 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
4852 added
|= (TYPE_QUALS_NO_ADDR_SPACE (in_type
)
4853 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype
));
4855 discarded
|= (TYPE_QUALS_NO_ADDR_SPACE (in_otype
)
4856 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type
));
4858 while (TREE_CODE (in_type
) == POINTER_TYPE
4859 && TREE_CODE (in_otype
) == POINTER_TYPE
);
4862 warning_at (loc
, OPT_Wcast_qual
,
4863 "cast adds %q#v qualifier to function type", added
);
4866 /* There are qualifiers present in IN_OTYPE that are not present
4868 warning_at (loc
, OPT_Wcast_qual
,
4869 "cast discards %qv qualifier from pointer target type",
4872 if (added
|| discarded
)
4875 /* A cast from **T to const **T is unsafe, because it can cause a
4876 const value to be changed with no additional warning. We only
4877 issue this warning if T is the same on both sides, and we only
4878 issue the warning if there are the same number of pointers on
4879 both sides, as otherwise the cast is clearly unsafe anyhow. A
4880 cast is unsafe when a qualifier is added at one level and const
4881 is not present at all outer levels.
4883 To issue this warning, we check at each level whether the cast
4884 adds new qualifiers not already seen. We don't need to special
4885 case function types, as they won't have the same
4886 TYPE_MAIN_VARIANT. */
4888 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
4890 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
4895 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
4898 in_type
= TREE_TYPE (in_type
);
4899 in_otype
= TREE_TYPE (in_otype
);
4900 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
4903 warning_at (loc
, OPT_Wcast_qual
,
4904 "to be safe all intermediate pointers in cast from "
4905 "%qT to %qT must be %<const%> qualified",
4910 is_const
= TYPE_READONLY (in_type
);
4912 while (TREE_CODE (in_type
) == POINTER_TYPE
);
4915 /* Build an expression representing a cast to type TYPE of expression EXPR.
4916 LOC is the location of the cast-- typically the open paren of the cast. */
4919 build_c_cast (location_t loc
, tree type
, tree expr
)
4923 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
4924 expr
= TREE_OPERAND (expr
, 0);
4928 if (type
== error_mark_node
|| expr
== error_mark_node
)
4929 return error_mark_node
;
4931 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4932 only in <protocol> qualifications. But when constructing cast expressions,
4933 the protocols do matter and must be kept around. */
4934 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
4935 return build1 (NOP_EXPR
, type
, expr
);
4937 type
= TYPE_MAIN_VARIANT (type
);
4939 if (TREE_CODE (type
) == ARRAY_TYPE
)
4941 error_at (loc
, "cast specifies array type");
4942 return error_mark_node
;
4945 if (TREE_CODE (type
) == FUNCTION_TYPE
)
4947 error_at (loc
, "cast specifies function type");
4948 return error_mark_node
;
4951 if (!VOID_TYPE_P (type
))
4953 value
= require_complete_type (value
);
4954 if (value
== error_mark_node
)
4955 return error_mark_node
;
4958 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
4960 if (TREE_CODE (type
) == RECORD_TYPE
4961 || TREE_CODE (type
) == UNION_TYPE
)
4962 pedwarn (loc
, OPT_Wpedantic
,
4963 "ISO C forbids casting nonscalar to the same type");
4965 /* Convert to remove any qualifiers from VALUE's type. */
4966 value
= convert (type
, value
);
4968 else if (TREE_CODE (type
) == UNION_TYPE
)
4972 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4973 if (TREE_TYPE (field
) != error_mark_node
4974 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
4975 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
4981 bool maybe_const
= true;
4983 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids casts to union type");
4984 t
= c_fully_fold (value
, false, &maybe_const
);
4985 t
= build_constructor_single (type
, field
, t
);
4987 t
= c_wrap_maybe_const (t
, true);
4988 t
= digest_init (loc
, type
, t
,
4989 NULL_TREE
, false, true, 0);
4990 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
4993 error_at (loc
, "cast to union type from type not present in union");
4994 return error_mark_node
;
5000 if (type
== void_type_node
)
5002 tree t
= build1 (CONVERT_EXPR
, type
, value
);
5003 SET_EXPR_LOCATION (t
, loc
);
5007 otype
= TREE_TYPE (value
);
5009 /* Optionally warn about potentially worrisome casts. */
5011 && TREE_CODE (type
) == POINTER_TYPE
5012 && TREE_CODE (otype
) == POINTER_TYPE
)
5013 handle_warn_cast_qual (loc
, type
, otype
);
5015 /* Warn about conversions between pointers to disjoint
5017 if (TREE_CODE (type
) == POINTER_TYPE
5018 && TREE_CODE (otype
) == POINTER_TYPE
5019 && !null_pointer_constant_p (value
))
5021 addr_space_t as_to
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
5022 addr_space_t as_from
= TYPE_ADDR_SPACE (TREE_TYPE (otype
));
5023 addr_space_t as_common
;
5025 if (!addr_space_superset (as_to
, as_from
, &as_common
))
5027 if (ADDR_SPACE_GENERIC_P (as_from
))
5028 warning_at (loc
, 0, "cast to %s address space pointer "
5029 "from disjoint generic address space pointer",
5030 c_addr_space_name (as_to
));
5032 else if (ADDR_SPACE_GENERIC_P (as_to
))
5033 warning_at (loc
, 0, "cast to generic address space pointer "
5034 "from disjoint %s address space pointer",
5035 c_addr_space_name (as_from
));
5038 warning_at (loc
, 0, "cast to %s address space pointer "
5039 "from disjoint %s address space pointer",
5040 c_addr_space_name (as_to
),
5041 c_addr_space_name (as_from
));
5045 /* Warn about possible alignment problems. */
5046 if (STRICT_ALIGNMENT
5047 && TREE_CODE (type
) == POINTER_TYPE
5048 && TREE_CODE (otype
) == POINTER_TYPE
5049 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5050 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5051 /* Don't warn about opaque types, where the actual alignment
5052 restriction is unknown. */
5053 && !((TREE_CODE (TREE_TYPE (otype
)) == UNION_TYPE
5054 || TREE_CODE (TREE_TYPE (otype
)) == RECORD_TYPE
)
5055 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
5056 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5057 warning_at (loc
, OPT_Wcast_align
,
5058 "cast increases required alignment of target type");
5060 if (TREE_CODE (type
) == INTEGER_TYPE
5061 && TREE_CODE (otype
) == POINTER_TYPE
5062 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5063 /* Unlike conversion of integers to pointers, where the
5064 warning is disabled for converting constants because
5065 of cases such as SIG_*, warn about converting constant
5066 pointers to integers. In some cases it may cause unwanted
5067 sign extension, and a warning is appropriate. */
5068 warning_at (loc
, OPT_Wpointer_to_int_cast
,
5069 "cast from pointer to integer of different size");
5071 if (TREE_CODE (value
) == CALL_EXPR
5072 && TREE_CODE (type
) != TREE_CODE (otype
))
5073 warning_at (loc
, OPT_Wbad_function_cast
,
5074 "cast from function call of type %qT "
5075 "to non-matching type %qT", otype
, type
);
5077 if (TREE_CODE (type
) == POINTER_TYPE
5078 && TREE_CODE (otype
) == INTEGER_TYPE
5079 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5080 /* Don't warn about converting any constant. */
5081 && !TREE_CONSTANT (value
))
5083 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
5084 "of different size");
5086 if (warn_strict_aliasing
<= 2)
5087 strict_aliasing_warning (otype
, type
, expr
);
5089 /* If pedantic, warn for conversions between function and object
5090 pointer types, except for converting a null pointer constant
5091 to function pointer type. */
5093 && TREE_CODE (type
) == POINTER_TYPE
5094 && TREE_CODE (otype
) == POINTER_TYPE
5095 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
5096 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
5097 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5098 "conversion of function pointer to object pointer type");
5101 && TREE_CODE (type
) == POINTER_TYPE
5102 && TREE_CODE (otype
) == POINTER_TYPE
5103 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
5104 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5105 && !null_pointer_constant_p (value
))
5106 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5107 "conversion of object pointer to function pointer type");
5110 value
= convert (type
, value
);
5112 /* Ignore any integer overflow caused by the cast. */
5113 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
5115 if (CONSTANT_CLASS_P (ovalue
) && TREE_OVERFLOW (ovalue
))
5117 if (!TREE_OVERFLOW (value
))
5119 /* Avoid clobbering a shared constant. */
5120 value
= copy_node (value
);
5121 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5124 else if (TREE_OVERFLOW (value
))
5125 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5126 value
= wide_int_to_tree (TREE_TYPE (value
), value
);
5130 /* Don't let a cast be an lvalue. */
5132 value
= non_lvalue_loc (loc
, value
);
5134 /* Don't allow the results of casting to floating-point or complex
5135 types be confused with actual constants, or casts involving
5136 integer and pointer types other than direct integer-to-integer
5137 and integer-to-pointer be confused with integer constant
5138 expressions and null pointer constants. */
5139 if (TREE_CODE (value
) == REAL_CST
5140 || TREE_CODE (value
) == COMPLEX_CST
5141 || (TREE_CODE (value
) == INTEGER_CST
5142 && !((TREE_CODE (expr
) == INTEGER_CST
5143 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
5144 || TREE_CODE (expr
) == REAL_CST
5145 || TREE_CODE (expr
) == COMPLEX_CST
)))
5146 value
= build1 (NOP_EXPR
, type
, value
);
5148 if (CAN_HAVE_LOCATION_P (value
))
5149 SET_EXPR_LOCATION (value
, loc
);
5153 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5154 location of the open paren of the cast, or the position of the cast
5157 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
5160 tree type_expr
= NULL_TREE
;
5161 bool type_expr_const
= true;
5163 int saved_wsp
= warn_strict_prototypes
;
5165 /* This avoids warnings about unprototyped casts on
5166 integers. E.g. "#define SIG_DFL (void(*)())0". */
5167 if (TREE_CODE (expr
) == INTEGER_CST
)
5168 warn_strict_prototypes
= 0;
5169 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
5170 warn_strict_prototypes
= saved_wsp
;
5172 ret
= build_c_cast (loc
, type
, expr
);
5175 bool inner_expr_const
= true;
5176 ret
= c_fully_fold (ret
, require_constant_value
, &inner_expr_const
);
5177 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
5178 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !(type_expr_const
5179 && inner_expr_const
);
5180 SET_EXPR_LOCATION (ret
, loc
);
5183 if (CAN_HAVE_LOCATION_P (ret
) && !EXPR_HAS_LOCATION (ret
))
5184 SET_EXPR_LOCATION (ret
, loc
);
5186 /* C++ does not permits types to be defined in a cast, but it
5187 allows references to incomplete types. */
5188 if (warn_cxx_compat
&& type_name
->specs
->typespec_kind
== ctsk_tagdef
)
5189 warning_at (loc
, OPT_Wc___compat
,
5190 "defining a type in a cast is invalid in C++");
5195 /* Build an assignment expression of lvalue LHS from value RHS.
5196 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5197 may differ from TREE_TYPE (LHS) for an enum bitfield.
5198 MODIFYCODE is the code for a binary operator that we use
5199 to combine the old value of LHS with RHS to get the new value.
5200 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5201 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5202 which may differ from TREE_TYPE (RHS) for an enum value.
5204 LOCATION is the location of the MODIFYCODE operator.
5205 RHS_LOC is the location of the RHS. */
5208 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
5209 enum tree_code modifycode
,
5210 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
5214 tree rhseval
= NULL_TREE
;
5215 tree rhs_semantic_type
= NULL_TREE
;
5216 tree lhstype
= TREE_TYPE (lhs
);
5217 tree olhstype
= lhstype
;
5221 /* Types that aren't fully specified cannot be used in assignments. */
5222 lhs
= require_complete_type (lhs
);
5224 /* Avoid duplicate error messages from operands that had errors. */
5225 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
5226 return error_mark_node
;
5228 /* Ensure an error for assigning a non-lvalue array to an array in
5230 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
5232 error_at (location
, "assignment to expression with array type");
5233 return error_mark_node
;
5236 /* For ObjC properties, defer this check. */
5237 if (!objc_is_property_ref (lhs
) && !lvalue_or_else (location
, lhs
, lv_assign
))
5238 return error_mark_node
;
5240 is_atomic_op
= really_atomic_lvalue (lhs
);
5242 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5244 rhs_semantic_type
= TREE_TYPE (rhs
);
5245 rhs
= TREE_OPERAND (rhs
, 0);
5250 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
5252 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
5253 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
5255 if (inner
== error_mark_node
)
5256 return error_mark_node
;
5257 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
5258 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
5259 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
5260 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
5261 protected_set_expr_location (result
, location
);
5265 /* If a binary op has been requested, combine the old LHS value with the RHS
5266 producing the value we should actually store into the LHS. */
5268 if (modifycode
!= NOP_EXPR
)
5270 lhs
= c_fully_fold (lhs
, false, NULL
);
5271 lhs
= stabilize_reference (lhs
);
5273 /* Construct the RHS for any non-atomic compound assignemnt. */
5276 /* If in LHS op= RHS the RHS has side-effects, ensure they
5277 are preevaluated before the rest of the assignment expression's
5278 side-effects, because RHS could contain e.g. function calls
5280 if (TREE_SIDE_EFFECTS (rhs
))
5282 newrhs
= in_late_binary_op
? save_expr (rhs
) : c_save_expr (rhs
);
5285 newrhs
= build_binary_op (location
,
5286 modifycode
, lhs
, newrhs
, 1);
5288 /* The original type of the right hand side is no longer
5290 rhs_origtype
= NULL_TREE
;
5294 if (c_dialect_objc ())
5296 /* Check if we are modifying an Objective-C property reference;
5297 if so, we need to generate setter calls. */
5298 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
5302 /* Else, do the check that we postponed for Objective-C. */
5303 if (!lvalue_or_else (location
, lhs
, lv_assign
))
5304 return error_mark_node
;
5307 /* Give an error for storing in something that is 'const'. */
5309 if (TYPE_READONLY (lhstype
)
5310 || ((TREE_CODE (lhstype
) == RECORD_TYPE
5311 || TREE_CODE (lhstype
) == UNION_TYPE
)
5312 && C_TYPE_FIELDS_READONLY (lhstype
)))
5314 readonly_error (location
, lhs
, lv_assign
);
5315 return error_mark_node
;
5317 else if (TREE_READONLY (lhs
))
5318 readonly_warning (lhs
, lv_assign
);
5320 /* If storing into a structure or union member,
5321 it has probably been given type `int'.
5322 Compute the type that would go with
5323 the actual amount of storage the member occupies. */
5325 if (TREE_CODE (lhs
) == COMPONENT_REF
5326 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5327 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
5328 || TREE_CODE (lhstype
) == REAL_TYPE
5329 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5330 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5332 /* If storing in a field that is in actuality a short or narrower than one,
5333 we must store in the field in its actual type. */
5335 if (lhstype
!= TREE_TYPE (lhs
))
5337 lhs
= copy_node (lhs
);
5338 TREE_TYPE (lhs
) = lhstype
;
5341 /* Issue -Wc++-compat warnings about an assignment to an enum type
5342 when LHS does not have its original type. This happens for,
5343 e.g., an enum bitfield in a struct. */
5345 && lhs_origtype
!= NULL_TREE
5346 && lhs_origtype
!= lhstype
5347 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
5349 tree checktype
= (rhs_origtype
!= NULL_TREE
5352 if (checktype
!= error_mark_node
5353 && (TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
)
5354 || (is_atomic_op
&& modifycode
!= NOP_EXPR
)))
5355 warning_at (location
, OPT_Wc___compat
,
5356 "enum conversion in assignment is invalid in C++");
5359 /* If the lhs is atomic, remove that qualifier. */
5362 lhstype
= build_qualified_type (lhstype
,
5363 (TYPE_QUALS (lhstype
)
5364 & ~TYPE_QUAL_ATOMIC
));
5365 olhstype
= build_qualified_type (olhstype
,
5366 (TYPE_QUALS (lhstype
)
5367 & ~TYPE_QUAL_ATOMIC
));
5370 /* Convert new value to destination type. Fold it first, then
5371 restore any excess precision information, for the sake of
5372 conversion warnings. */
5374 if (!(is_atomic_op
&& modifycode
!= NOP_EXPR
))
5376 npc
= null_pointer_constant_p (newrhs
);
5377 newrhs
= c_fully_fold (newrhs
, false, NULL
);
5378 if (rhs_semantic_type
)
5379 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
5380 newrhs
= convert_for_assignment (location
, rhs_loc
, lhstype
, newrhs
,
5381 rhs_origtype
, ic_assign
, npc
,
5382 NULL_TREE
, NULL_TREE
, 0);
5383 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5384 return error_mark_node
;
5387 /* Emit ObjC write barrier, if necessary. */
5388 if (c_dialect_objc () && flag_objc_gc
)
5390 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
5393 protected_set_expr_location (result
, location
);
5398 /* Scan operands. */
5401 result
= build_atomic_assign (location
, lhs
, modifycode
, newrhs
, false);
5404 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
5405 TREE_SIDE_EFFECTS (result
) = 1;
5406 protected_set_expr_location (result
, location
);
5409 /* If we got the LHS in a different type for storing in,
5410 convert the result back to the nominal type of LHS
5411 so that the value we return always has the same type
5412 as the LHS argument. */
5414 if (olhstype
== TREE_TYPE (result
))
5417 result
= convert_for_assignment (location
, rhs_loc
, olhstype
, result
,
5418 rhs_origtype
, ic_assign
, false, NULL_TREE
,
5420 protected_set_expr_location (result
, location
);
5424 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), rhseval
, result
);
5428 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5429 This is used to implement -fplan9-extensions. */
5432 find_anonymous_field_with_type (tree struct_type
, tree type
)
5437 gcc_assert (TREE_CODE (struct_type
) == RECORD_TYPE
5438 || TREE_CODE (struct_type
) == UNION_TYPE
);
5440 for (field
= TYPE_FIELDS (struct_type
);
5442 field
= TREE_CHAIN (field
))
5444 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5445 ? c_build_qualified_type (TREE_TYPE (field
),
5447 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5448 if (DECL_NAME (field
) == NULL
5449 && comptypes (type
, fieldtype
))
5455 else if (DECL_NAME (field
) == NULL
5456 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
5457 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
5458 && find_anonymous_field_with_type (TREE_TYPE (field
), type
))
5468 /* RHS is an expression whose type is pointer to struct. If there is
5469 an anonymous field in RHS with type TYPE, then return a pointer to
5470 that field in RHS. This is used with -fplan9-extensions. This
5471 returns NULL if no conversion could be found. */
5474 convert_to_anonymous_field (location_t location
, tree type
, tree rhs
)
5476 tree rhs_struct_type
, lhs_main_type
;
5477 tree field
, found_field
;
5478 bool found_sub_field
;
5481 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs
)));
5482 rhs_struct_type
= TREE_TYPE (TREE_TYPE (rhs
));
5483 gcc_assert (TREE_CODE (rhs_struct_type
) == RECORD_TYPE
5484 || TREE_CODE (rhs_struct_type
) == UNION_TYPE
);
5486 gcc_assert (POINTER_TYPE_P (type
));
5487 lhs_main_type
= (TYPE_ATOMIC (TREE_TYPE (type
))
5488 ? c_build_qualified_type (TREE_TYPE (type
),
5490 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
5492 found_field
= NULL_TREE
;
5493 found_sub_field
= false;
5494 for (field
= TYPE_FIELDS (rhs_struct_type
);
5496 field
= TREE_CHAIN (field
))
5498 if (DECL_NAME (field
) != NULL_TREE
5499 || (TREE_CODE (TREE_TYPE (field
)) != RECORD_TYPE
5500 && TREE_CODE (TREE_TYPE (field
)) != UNION_TYPE
))
5502 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5503 ? c_build_qualified_type (TREE_TYPE (field
),
5505 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5506 if (comptypes (lhs_main_type
, fieldtype
))
5508 if (found_field
!= NULL_TREE
)
5510 found_field
= field
;
5512 else if (find_anonymous_field_with_type (TREE_TYPE (field
),
5515 if (found_field
!= NULL_TREE
)
5517 found_field
= field
;
5518 found_sub_field
= true;
5522 if (found_field
== NULL_TREE
)
5525 ret
= fold_build3_loc (location
, COMPONENT_REF
, TREE_TYPE (found_field
),
5526 build_fold_indirect_ref (rhs
), found_field
,
5528 ret
= build_fold_addr_expr_loc (location
, ret
);
5530 if (found_sub_field
)
5532 ret
= convert_to_anonymous_field (location
, type
, ret
);
5533 gcc_assert (ret
!= NULL_TREE
);
5539 /* Issue an error message for a bad initializer component.
5540 GMSGID identifies the message.
5541 The component name is taken from the spelling stack. */
5544 error_init (location_t loc
, const char *gmsgid
)
5548 /* The gmsgid may be a format string with %< and %>. */
5549 error_at (loc
, gmsgid
);
5550 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5552 inform (loc
, "(near initialization for %qs)", ofwhat
);
5555 /* Issue a pedantic warning for a bad initializer component. OPT is
5556 the option OPT_* (from options.h) controlling this warning or 0 if
5557 it is unconditionally given. GMSGID identifies the message. The
5558 component name is taken from the spelling stack. */
5561 pedwarn_init (location_t location
, int opt
, const char *gmsgid
)
5566 /* The gmsgid may be a format string with %< and %>. */
5567 warned
= pedwarn (location
, opt
, gmsgid
);
5568 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5569 if (*ofwhat
&& warned
)
5570 inform (location
, "(near initialization for %qs)", ofwhat
);
5573 /* Issue a warning for a bad initializer component.
5575 OPT is the OPT_W* value corresponding to the warning option that
5576 controls this warning. GMSGID identifies the message. The
5577 component name is taken from the spelling stack. */
5580 warning_init (location_t loc
, int opt
, const char *gmsgid
)
5585 /* The gmsgid may be a format string with %< and %>. */
5586 warned
= warning_at (loc
, opt
, gmsgid
);
5587 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5588 if (*ofwhat
&& warned
)
5589 inform (loc
, "(near initialization for %qs)", ofwhat
);
5592 /* If TYPE is an array type and EXPR is a parenthesized string
5593 constant, warn if pedantic that EXPR is being used to initialize an
5594 object of type TYPE. */
5597 maybe_warn_string_init (location_t loc
, tree type
, struct c_expr expr
)
5600 && TREE_CODE (type
) == ARRAY_TYPE
5601 && TREE_CODE (expr
.value
) == STRING_CST
5602 && expr
.original_code
!= STRING_CST
)
5603 pedwarn_init (loc
, OPT_Wpedantic
,
5604 "array initialized from parenthesized string constant");
5607 /* Convert value RHS to type TYPE as preparation for an assignment to
5608 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5609 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5610 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5611 constant before any folding.
5612 The real work of conversion is done by `convert'.
5613 The purpose of this function is to generate error messages
5614 for assignments that are not allowed in C.
5615 ERRTYPE says whether it is argument passing, assignment,
5616 initialization or return.
5618 LOCATION is the location of the assignment, EXPR_LOC is the location of
5619 the RHS or, for a function, location of an argument.
5620 FUNCTION is a tree for the function being called.
5621 PARMNUM is the number of the argument, for printing in error messages. */
5624 convert_for_assignment (location_t location
, location_t expr_loc
, tree type
,
5625 tree rhs
, tree origtype
, enum impl_conv errtype
,
5626 bool null_pointer_constant
, tree fundecl
,
5627 tree function
, int parmnum
)
5629 enum tree_code codel
= TREE_CODE (type
);
5630 tree orig_rhs
= rhs
;
5632 enum tree_code coder
;
5633 tree rname
= NULL_TREE
;
5634 bool objc_ok
= false;
5636 if (errtype
== ic_argpass
)
5639 /* Change pointer to function to the function itself for
5641 if (TREE_CODE (function
) == ADDR_EXPR
5642 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
5643 function
= TREE_OPERAND (function
, 0);
5645 /* Handle an ObjC selector specially for diagnostics. */
5646 selector
= objc_message_selector ();
5648 if (selector
&& parmnum
> 2)
5655 /* This macro is used to emit diagnostics to ensure that all format
5656 strings are complete sentences, visible to gettext and checked at
5658 #define WARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5663 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5664 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5665 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5666 "expected %qT but argument is of type %qT", \
5670 pedwarn (LOCATION, OPT, AS); \
5673 pedwarn_init (LOCATION, OPT, IN); \
5676 pedwarn (LOCATION, OPT, RE); \
5679 gcc_unreachable (); \
5683 /* This macro is used to emit diagnostics to ensure that all format
5684 strings are complete sentences, visible to gettext and checked at
5685 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5686 extra parameter to enumerate qualifiers. */
5688 #define WARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5693 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5694 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5695 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5696 "expected %qT but argument is of type %qT", \
5700 pedwarn (LOCATION, OPT, AS, QUALS); \
5703 pedwarn (LOCATION, OPT, IN, QUALS); \
5706 pedwarn (LOCATION, OPT, RE, QUALS); \
5709 gcc_unreachable (); \
5713 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5714 rhs
= TREE_OPERAND (rhs
, 0);
5716 rhstype
= TREE_TYPE (rhs
);
5717 coder
= TREE_CODE (rhstype
);
5719 if (coder
== ERROR_MARK
)
5720 return error_mark_node
;
5722 if (c_dialect_objc ())
5745 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
5748 if (warn_cxx_compat
)
5750 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
5751 if (checktype
!= error_mark_node
5752 && TREE_CODE (type
) == ENUMERAL_TYPE
5753 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
5755 WARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wc___compat
,
5756 G_("enum conversion when passing argument "
5757 "%d of %qE is invalid in C++"),
5758 G_("enum conversion in assignment is "
5760 G_("enum conversion in initialization is "
5762 G_("enum conversion in return is "
5767 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
5770 if (coder
== VOID_TYPE
)
5772 /* Except for passing an argument to an unprototyped function,
5773 this is a constraint violation. When passing an argument to
5774 an unprototyped function, it is compile-time undefined;
5775 making it a constraint in that case was rejected in
5777 error_at (location
, "void value not ignored as it ought to be");
5778 return error_mark_node
;
5780 rhs
= require_complete_type (rhs
);
5781 if (rhs
== error_mark_node
)
5782 return error_mark_node
;
5783 /* A non-reference type can convert to a reference. This handles
5784 va_start, va_copy and possibly port built-ins. */
5785 if (codel
== REFERENCE_TYPE
&& coder
!= REFERENCE_TYPE
)
5787 if (!lvalue_p (rhs
))
5789 error_at (location
, "cannot pass rvalue to reference parameter");
5790 return error_mark_node
;
5792 if (!c_mark_addressable (rhs
))
5793 return error_mark_node
;
5794 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
5795 SET_EXPR_LOCATION (rhs
, location
);
5797 rhs
= convert_for_assignment (location
, expr_loc
,
5798 build_pointer_type (TREE_TYPE (type
)),
5799 rhs
, origtype
, errtype
,
5800 null_pointer_constant
, fundecl
, function
,
5802 if (rhs
== error_mark_node
)
5803 return error_mark_node
;
5805 rhs
= build1 (NOP_EXPR
, type
, rhs
);
5806 SET_EXPR_LOCATION (rhs
, location
);
5809 /* Some types can interconvert without explicit casts. */
5810 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
5811 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
5812 return convert (type
, rhs
);
5813 /* Arithmetic types all interconvert, and enum is treated like int. */
5814 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
5815 || codel
== FIXED_POINT_TYPE
5816 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
5817 || codel
== BOOLEAN_TYPE
)
5818 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
5819 || coder
== FIXED_POINT_TYPE
5820 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
5821 || coder
== BOOLEAN_TYPE
))
5824 bool save
= in_late_binary_op
;
5825 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
)
5826 in_late_binary_op
= true;
5827 ret
= convert_and_check (expr_loc
!= UNKNOWN_LOCATION
5828 ? expr_loc
: location
, type
, orig_rhs
);
5829 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
)
5830 in_late_binary_op
= save
;
5834 /* Aggregates in different TUs might need conversion. */
5835 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
5837 && comptypes (type
, rhstype
))
5838 return convert_and_check (expr_loc
!= UNKNOWN_LOCATION
5839 ? expr_loc
: location
, type
, rhs
);
5841 /* Conversion to a transparent union or record from its member types.
5842 This applies only to function arguments. */
5843 if (((codel
== UNION_TYPE
|| codel
== RECORD_TYPE
)
5844 && TYPE_TRANSPARENT_AGGR (type
))
5845 && errtype
== ic_argpass
)
5847 tree memb
, marginal_memb
= NULL_TREE
;
5849 for (memb
= TYPE_FIELDS (type
); memb
; memb
= DECL_CHAIN (memb
))
5851 tree memb_type
= TREE_TYPE (memb
);
5853 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
5854 TYPE_MAIN_VARIANT (rhstype
)))
5857 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
5860 if (coder
== POINTER_TYPE
)
5862 tree ttl
= TREE_TYPE (memb_type
);
5863 tree ttr
= TREE_TYPE (rhstype
);
5865 /* Any non-function converts to a [const][volatile] void *
5866 and vice versa; otherwise, targets must be the same.
5867 Meanwhile, the lhs target must have all the qualifiers of
5869 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
5870 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
5871 || comp_target_types (location
, memb_type
, rhstype
))
5873 int lquals
= TYPE_QUALS (ttl
) & ~TYPE_QUAL_ATOMIC
;
5874 int rquals
= TYPE_QUALS (ttr
) & ~TYPE_QUAL_ATOMIC
;
5875 /* If this type won't generate any warnings, use it. */
5876 if (lquals
== rquals
5877 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
5878 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
5879 ? ((lquals
| rquals
) == rquals
)
5880 : ((lquals
| rquals
) == lquals
)))
5883 /* Keep looking for a better type, but remember this one. */
5885 marginal_memb
= memb
;
5889 /* Can convert integer zero to any pointer type. */
5890 if (null_pointer_constant
)
5892 rhs
= null_pointer_node
;
5897 if (memb
|| marginal_memb
)
5901 /* We have only a marginally acceptable member type;
5902 it needs a warning. */
5903 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
5904 tree ttr
= TREE_TYPE (rhstype
);
5906 /* Const and volatile mean something different for function
5907 types, so the usual warnings are not appropriate. */
5908 if (TREE_CODE (ttr
) == FUNCTION_TYPE
5909 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
5911 /* Because const and volatile on functions are
5912 restrictions that say the function will not do
5913 certain things, it is okay to use a const or volatile
5914 function where an ordinary one is wanted, but not
5916 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
5917 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
5918 WARN_FOR_QUALIFIERS (location
, expr_loc
,
5919 OPT_Wdiscarded_qualifiers
,
5920 G_("passing argument %d of %qE "
5921 "makes %q#v qualified function "
5922 "pointer from unqualified"),
5923 G_("assignment makes %q#v qualified "
5924 "function pointer from "
5926 G_("initialization makes %q#v qualified "
5927 "function pointer from "
5929 G_("return makes %q#v qualified function "
5930 "pointer from unqualified"),
5931 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
5933 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr
)
5934 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl
))
5935 WARN_FOR_QUALIFIERS (location
, expr_loc
,
5936 OPT_Wdiscarded_qualifiers
,
5937 G_("passing argument %d of %qE discards "
5938 "%qv qualifier from pointer target type"),
5939 G_("assignment discards %qv qualifier "
5940 "from pointer target type"),
5941 G_("initialization discards %qv qualifier "
5942 "from pointer target type"),
5943 G_("return discards %qv qualifier from "
5944 "pointer target type"),
5945 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
5947 memb
= marginal_memb
;
5950 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
5951 pedwarn (location
, OPT_Wpedantic
,
5952 "ISO C prohibits argument conversion to union type");
5954 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
5955 return build_constructor_single (type
, memb
, rhs
);
5959 /* Conversions among pointers */
5960 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
5961 && (coder
== codel
))
5963 tree ttl
= TREE_TYPE (type
);
5964 tree ttr
= TREE_TYPE (rhstype
);
5967 bool is_opaque_pointer
;
5968 int target_cmp
= 0; /* Cache comp_target_types () result. */
5972 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
5973 mvl
= (TYPE_ATOMIC (mvl
)
5974 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
),
5976 : TYPE_MAIN_VARIANT (mvl
));
5977 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
5978 mvr
= (TYPE_ATOMIC (mvr
)
5979 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
),
5981 : TYPE_MAIN_VARIANT (mvr
));
5982 /* Opaque pointers are treated like void pointers. */
5983 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
5985 /* The Plan 9 compiler permits a pointer to a struct to be
5986 automatically converted into a pointer to an anonymous field
5987 within the struct. */
5988 if (flag_plan9_extensions
5989 && (TREE_CODE (mvl
) == RECORD_TYPE
|| TREE_CODE(mvl
) == UNION_TYPE
)
5990 && (TREE_CODE (mvr
) == RECORD_TYPE
|| TREE_CODE(mvr
) == UNION_TYPE
)
5993 tree new_rhs
= convert_to_anonymous_field (location
, type
, rhs
);
5994 if (new_rhs
!= NULL_TREE
)
5997 rhstype
= TREE_TYPE (rhs
);
5998 coder
= TREE_CODE (rhstype
);
5999 ttr
= TREE_TYPE (rhstype
);
6000 mvr
= TYPE_MAIN_VARIANT (ttr
);
6004 /* C++ does not allow the implicit conversion void* -> T*. However,
6005 for the purpose of reducing the number of false positives, we
6006 tolerate the special case of
6010 where NULL is typically defined in C to be '(void *) 0'. */
6011 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
6012 warning_at (errtype
== ic_argpass
? expr_loc
: location
,
6014 "request for implicit conversion "
6015 "from %qT to %qT not permitted in C++", rhstype
, type
);
6017 /* See if the pointers point to incompatible address spaces. */
6018 asl
= TYPE_ADDR_SPACE (ttl
);
6019 asr
= TYPE_ADDR_SPACE (ttr
);
6020 if (!null_pointer_constant_p (rhs
)
6021 && asr
!= asl
&& !targetm
.addr_space
.subset_p (asr
, asl
))
6026 error_at (expr_loc
, "passing argument %d of %qE from pointer to "
6027 "non-enclosed address space", parmnum
, rname
);
6030 error_at (location
, "assignment from pointer to "
6031 "non-enclosed address space");
6034 error_at (location
, "initialization from pointer to "
6035 "non-enclosed address space");
6038 error_at (location
, "return from pointer to "
6039 "non-enclosed address space");
6044 return error_mark_node
;
6047 /* Check if the right-hand side has a format attribute but the
6048 left-hand side doesn't. */
6049 if (warn_suggest_attribute_format
6050 && check_missing_format_attribute (type
, rhstype
))
6055 warning_at (expr_loc
, OPT_Wsuggest_attribute_format
,
6056 "argument %d of %qE might be "
6057 "a candidate for a format attribute",
6061 warning_at (location
, OPT_Wsuggest_attribute_format
,
6062 "assignment left-hand side might be "
6063 "a candidate for a format attribute");
6066 warning_at (location
, OPT_Wsuggest_attribute_format
,
6067 "initialization left-hand side might be "
6068 "a candidate for a format attribute");
6071 warning_at (location
, OPT_Wsuggest_attribute_format
,
6072 "return type might be "
6073 "a candidate for a format attribute");
6080 /* Any non-function converts to a [const][volatile] void *
6081 and vice versa; otherwise, targets must be the same.
6082 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6083 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
6084 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
6085 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
6086 || is_opaque_pointer
6087 || ((c_common_unsigned_type (mvl
)
6088 == c_common_unsigned_type (mvr
))
6089 && (c_common_signed_type (mvl
)
6090 == c_common_signed_type (mvr
))
6091 && TYPE_ATOMIC (mvl
) == TYPE_ATOMIC (mvr
)))
6094 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6097 && !null_pointer_constant
6098 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
6099 WARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpedantic
,
6100 G_("ISO C forbids passing argument %d of "
6101 "%qE between function pointer "
6103 G_("ISO C forbids assignment between "
6104 "function pointer and %<void *%>"),
6105 G_("ISO C forbids initialization between "
6106 "function pointer and %<void *%>"),
6107 G_("ISO C forbids return between function "
6108 "pointer and %<void *%>"));
6109 /* Const and volatile mean something different for function types,
6110 so the usual warnings are not appropriate. */
6111 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
6112 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
6114 /* Assignments between atomic and non-atomic objects are OK. */
6115 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
6116 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
6118 WARN_FOR_QUALIFIERS (location
, expr_loc
,
6119 OPT_Wdiscarded_qualifiers
,
6120 G_("passing argument %d of %qE discards "
6121 "%qv qualifier from pointer target type"),
6122 G_("assignment discards %qv qualifier "
6123 "from pointer target type"),
6124 G_("initialization discards %qv qualifier "
6125 "from pointer target type"),
6126 G_("return discards %qv qualifier from "
6127 "pointer target type"),
6128 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6130 /* If this is not a case of ignoring a mismatch in signedness,
6132 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
6135 /* If there is a mismatch, do warn. */
6136 else if (warn_pointer_sign
)
6137 WARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpointer_sign
,
6138 G_("pointer targets in passing argument "
6139 "%d of %qE differ in signedness"),
6140 G_("pointer targets in assignment "
6141 "differ in signedness"),
6142 G_("pointer targets in initialization "
6143 "differ in signedness"),
6144 G_("pointer targets in return differ "
6147 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
6148 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6150 /* Because const and volatile on functions are restrictions
6151 that say the function will not do certain things,
6152 it is okay to use a const or volatile function
6153 where an ordinary one is wanted, but not vice-versa. */
6154 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6155 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6156 WARN_FOR_QUALIFIERS (location
, expr_loc
,
6157 OPT_Wdiscarded_qualifiers
,
6158 G_("passing argument %d of %qE makes "
6159 "%q#v qualified function pointer "
6160 "from unqualified"),
6161 G_("assignment makes %q#v qualified function "
6162 "pointer from unqualified"),
6163 G_("initialization makes %q#v qualified "
6164 "function pointer from unqualified"),
6165 G_("return makes %q#v qualified function "
6166 "pointer from unqualified"),
6167 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6171 /* Avoid warning about the volatile ObjC EH puts on decls. */
6173 WARN_FOR_ASSIGNMENT (location
, expr_loc
,
6174 OPT_Wincompatible_pointer_types
,
6175 G_("passing argument %d of %qE from "
6176 "incompatible pointer type"),
6177 G_("assignment from incompatible pointer type"),
6178 G_("initialization from incompatible "
6180 G_("return from incompatible pointer type"));
6182 return convert (type
, rhs
);
6184 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
6186 /* ??? This should not be an error when inlining calls to
6187 unprototyped functions. */
6188 error_at (location
, "invalid use of non-lvalue array");
6189 return error_mark_node
;
6191 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
6193 /* An explicit constant 0 can convert to a pointer,
6194 or one that results from arithmetic, even including
6195 a cast to integer type. */
6196 if (!null_pointer_constant
)
6197 WARN_FOR_ASSIGNMENT (location
, expr_loc
,
6198 OPT_Wint_conversion
,
6199 G_("passing argument %d of %qE makes "
6200 "pointer from integer without a cast"),
6201 G_("assignment makes pointer from integer "
6203 G_("initialization makes pointer from "
6204 "integer without a cast"),
6205 G_("return makes pointer from integer "
6208 return convert (type
, rhs
);
6210 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
6212 WARN_FOR_ASSIGNMENT (location
, expr_loc
,
6213 OPT_Wint_conversion
,
6214 G_("passing argument %d of %qE makes integer "
6215 "from pointer without a cast"),
6216 G_("assignment makes integer from pointer "
6218 G_("initialization makes integer from pointer "
6220 G_("return makes integer from pointer "
6222 return convert (type
, rhs
);
6224 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
6227 bool save
= in_late_binary_op
;
6228 in_late_binary_op
= true;
6229 ret
= convert (type
, rhs
);
6230 in_late_binary_op
= save
;
6237 error_at (expr_loc
, "incompatible type for argument %d of %qE", parmnum
,
6239 inform ((fundecl
&& !DECL_IS_BUILTIN (fundecl
))
6240 ? DECL_SOURCE_LOCATION (fundecl
) : expr_loc
,
6241 "expected %qT but argument is of type %qT", type
, rhstype
);
6244 error_at (location
, "incompatible types when assigning to type %qT from "
6245 "type %qT", type
, rhstype
);
6249 "incompatible types when initializing type %qT using type %qT",
6254 "incompatible types when returning type %qT but %qT was "
6255 "expected", rhstype
, type
);
6261 return error_mark_node
;
6264 /* If VALUE is a compound expr all of whose expressions are constant, then
6265 return its value. Otherwise, return error_mark_node.
6267 This is for handling COMPOUND_EXPRs as initializer elements
6268 which is allowed with a warning when -pedantic is specified. */
6271 valid_compound_expr_initializer (tree value
, tree endtype
)
6273 if (TREE_CODE (value
) == COMPOUND_EXPR
)
6275 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
6277 return error_mark_node
;
6278 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
6281 else if (!initializer_constant_valid_p (value
, endtype
))
6282 return error_mark_node
;
6287 /* Perform appropriate conversions on the initial value of a variable,
6288 store it in the declaration DECL,
6289 and print any error messages that are appropriate.
6290 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6291 If the init is invalid, store an ERROR_MARK.
6293 INIT_LOC is the location of the initial value. */
6296 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
6301 /* If variable's type was invalidly declared, just ignore it. */
6303 type
= TREE_TYPE (decl
);
6304 if (TREE_CODE (type
) == ERROR_MARK
)
6307 /* Digest the specified initializer into an expression. */
6310 npc
= null_pointer_constant_p (init
);
6311 value
= digest_init (init_loc
, type
, init
, origtype
, npc
,
6312 true, TREE_STATIC (decl
));
6314 /* Store the expression if valid; else report error. */
6316 if (!in_system_header_at (input_location
)
6317 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
6318 warning (OPT_Wtraditional
, "traditional C rejects automatic "
6319 "aggregate initialization");
6321 DECL_INITIAL (decl
) = value
;
6323 /* ANSI wants warnings about out-of-range constant initializers. */
6324 STRIP_TYPE_NOPS (value
);
6325 if (TREE_STATIC (decl
))
6326 constant_expression_warning (value
);
6328 /* Check if we need to set array size from compound literal size. */
6329 if (TREE_CODE (type
) == ARRAY_TYPE
6330 && TYPE_DOMAIN (type
) == 0
6331 && value
!= error_mark_node
)
6333 tree inside_init
= init
;
6335 STRIP_TYPE_NOPS (inside_init
);
6336 inside_init
= fold (inside_init
);
6338 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6340 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6342 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
6344 /* For int foo[] = (int [3]){1}; we need to set array size
6345 now since later on array initializer will be just the
6346 brace enclosed list of the compound literal. */
6347 tree etype
= strip_array_types (TREE_TYPE (decl
));
6348 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6349 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
6351 layout_decl (cldecl
, 0);
6353 = c_build_qualified_type (type
, TYPE_QUALS (etype
));
6359 /* Methods for storing and printing names for error messages. */
6361 /* Implement a spelling stack that allows components of a name to be pushed
6362 and popped. Each element on the stack is this structure. */
6369 unsigned HOST_WIDE_INT i
;
6374 #define SPELLING_STRING 1
6375 #define SPELLING_MEMBER 2
6376 #define SPELLING_BOUNDS 3
6378 static struct spelling
*spelling
; /* Next stack element (unused). */
6379 static struct spelling
*spelling_base
; /* Spelling stack base. */
6380 static int spelling_size
; /* Size of the spelling stack. */
6382 /* Macros to save and restore the spelling stack around push_... functions.
6383 Alternative to SAVE_SPELLING_STACK. */
6385 #define SPELLING_DEPTH() (spelling - spelling_base)
6386 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6388 /* Push an element on the spelling stack with type KIND and assign VALUE
6391 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6393 int depth = SPELLING_DEPTH (); \
6395 if (depth >= spelling_size) \
6397 spelling_size += 10; \
6398 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6400 RESTORE_SPELLING_DEPTH (depth); \
6403 spelling->kind = (KIND); \
6404 spelling->MEMBER = (VALUE); \
6408 /* Push STRING on the stack. Printed literally. */
6411 push_string (const char *string
)
6413 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
6416 /* Push a member name on the stack. Printed as '.' STRING. */
6419 push_member_name (tree decl
)
6421 const char *const string
6423 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
6424 : _("<anonymous>"));
6425 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
6428 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6431 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
6433 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
6436 /* Compute the maximum size in bytes of the printed spelling. */
6439 spelling_length (void)
6444 for (p
= spelling_base
; p
< spelling
; p
++)
6446 if (p
->kind
== SPELLING_BOUNDS
)
6449 size
+= strlen (p
->u
.s
) + 1;
6455 /* Print the spelling to BUFFER and return it. */
6458 print_spelling (char *buffer
)
6463 for (p
= spelling_base
; p
< spelling
; p
++)
6464 if (p
->kind
== SPELLING_BOUNDS
)
6466 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
6472 if (p
->kind
== SPELLING_MEMBER
)
6474 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
6481 /* Digest the parser output INIT as an initializer for type TYPE.
6482 Return a C expression of type TYPE to represent the initial value.
6484 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6486 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6488 If INIT is a string constant, STRICT_STRING is true if it is
6489 unparenthesized or we should not warn here for it being parenthesized.
6490 For other types of INIT, STRICT_STRING is not used.
6492 INIT_LOC is the location of the INIT.
6494 REQUIRE_CONSTANT requests an error if non-constant initializers or
6495 elements are seen. */
6498 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
6499 bool null_pointer_constant
, bool strict_string
,
6500 int require_constant
)
6502 enum tree_code code
= TREE_CODE (type
);
6503 tree inside_init
= init
;
6504 tree semantic_type
= NULL_TREE
;
6505 bool maybe_const
= true;
6507 if (type
== error_mark_node
6509 || error_operand_p (init
))
6510 return error_mark_node
;
6512 STRIP_TYPE_NOPS (inside_init
);
6514 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
6516 semantic_type
= TREE_TYPE (inside_init
);
6517 inside_init
= TREE_OPERAND (inside_init
, 0);
6519 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
6520 inside_init
= decl_constant_value_for_optimization (inside_init
);
6522 /* Initialization of an array of chars from a string constant
6523 optionally enclosed in braces. */
6525 if (code
== ARRAY_TYPE
&& inside_init
6526 && TREE_CODE (inside_init
) == STRING_CST
)
6529 = (TYPE_ATOMIC (TREE_TYPE (type
))
6530 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
6532 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
6533 /* Note that an array could be both an array of character type
6534 and an array of wchar_t if wchar_t is signed char or unsigned
6536 bool char_array
= (typ1
== char_type_node
6537 || typ1
== signed_char_type_node
6538 || typ1
== unsigned_char_type_node
);
6539 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
6540 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
6541 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
6543 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
6546 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
6547 expr
.value
= inside_init
;
6548 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
6549 expr
.original_type
= NULL
;
6550 maybe_warn_string_init (init_loc
, type
, expr
);
6552 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
6553 pedwarn_init (init_loc
, OPT_Wpedantic
,
6554 "initialization of a flexible array member");
6556 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6557 TYPE_MAIN_VARIANT (type
)))
6562 if (typ2
!= char_type_node
)
6564 error_init (init_loc
, "char-array initialized from wide "
6566 return error_mark_node
;
6571 if (typ2
== char_type_node
)
6573 error_init (init_loc
, "wide character array initialized "
6574 "from non-wide string");
6575 return error_mark_node
;
6577 else if (!comptypes(typ1
, typ2
))
6579 error_init (init_loc
, "wide character array initialized "
6580 "from incompatible wide string");
6581 return error_mark_node
;
6585 TREE_TYPE (inside_init
) = type
;
6586 if (TYPE_DOMAIN (type
) != 0
6587 && TYPE_SIZE (type
) != 0
6588 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
6590 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
6592 /* Subtract the size of a single (possibly wide) character
6593 because it's ok to ignore the terminating null char
6594 that is counted in the length of the constant. */
6595 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
6597 - (TYPE_PRECISION (typ1
)
6599 pedwarn_init (init_loc
, 0,
6600 ("initializer-string for array of chars "
6602 else if (warn_cxx_compat
6603 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
), len
))
6604 warning_at (init_loc
, OPT_Wc___compat
,
6605 ("initializer-string for array chars "
6606 "is too long for C++"));
6611 else if (INTEGRAL_TYPE_P (typ1
))
6613 error_init (init_loc
, "array of inappropriate type initialized "
6614 "from string constant");
6615 return error_mark_node
;
6619 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6620 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6621 below and handle as a constructor. */
6622 if (code
== VECTOR_TYPE
6623 && TREE_CODE (TREE_TYPE (inside_init
)) == VECTOR_TYPE
6624 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
6625 && TREE_CONSTANT (inside_init
))
6627 if (TREE_CODE (inside_init
) == VECTOR_CST
6628 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6629 TYPE_MAIN_VARIANT (type
)))
6632 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
6634 unsigned HOST_WIDE_INT ix
;
6636 bool constant_p
= true;
6638 /* Iterate through elements and check if all constructor
6639 elements are *_CSTs. */
6640 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
6641 if (!CONSTANT_CLASS_P (value
))
6648 return build_vector_from_ctor (type
,
6649 CONSTRUCTOR_ELTS (inside_init
));
6653 if (warn_sequence_point
)
6654 verify_sequence_points (inside_init
);
6656 /* Any type can be initialized
6657 from an expression of the same type, optionally with braces. */
6659 if (inside_init
&& TREE_TYPE (inside_init
) != 0
6660 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6661 TYPE_MAIN_VARIANT (type
))
6662 || (code
== ARRAY_TYPE
6663 && comptypes (TREE_TYPE (inside_init
), type
))
6664 || (code
== VECTOR_TYPE
6665 && comptypes (TREE_TYPE (inside_init
), type
))
6666 || (code
== POINTER_TYPE
6667 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
6668 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
6669 TREE_TYPE (type
)))))
6671 if (code
== POINTER_TYPE
)
6673 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
6675 if (TREE_CODE (inside_init
) == STRING_CST
6676 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6677 inside_init
= array_to_pointer_conversion
6678 (init_loc
, inside_init
);
6681 error_init (init_loc
, "invalid use of non-lvalue array");
6682 return error_mark_node
;
6687 if (code
== VECTOR_TYPE
)
6688 /* Although the types are compatible, we may require a
6690 inside_init
= convert (type
, inside_init
);
6692 if (require_constant
6693 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6695 /* As an extension, allow initializing objects with static storage
6696 duration with compound literals (which are then treated just as
6697 the brace enclosed list they contain). Also allow this for
6698 vectors, as we can only assign them with compound literals. */
6699 if (flag_isoc99
&& code
!= VECTOR_TYPE
)
6700 pedwarn_init (init_loc
, OPT_Wpedantic
, "initializer element "
6702 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6703 inside_init
= DECL_INITIAL (decl
);
6706 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
6707 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
6709 error_init (init_loc
, "array initialized from non-constant array "
6711 return error_mark_node
;
6714 /* Compound expressions can only occur here if -Wpedantic or
6715 -pedantic-errors is specified. In the later case, we always want
6716 an error. In the former case, we simply want a warning. */
6717 if (require_constant
&& pedantic
6718 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
6721 = valid_compound_expr_initializer (inside_init
,
6722 TREE_TYPE (inside_init
));
6723 if (inside_init
== error_mark_node
)
6724 error_init (init_loc
, "initializer element is not constant");
6726 pedwarn_init (init_loc
, OPT_Wpedantic
,
6727 "initializer element is not constant");
6728 if (flag_pedantic_errors
)
6729 inside_init
= error_mark_node
;
6731 else if (require_constant
6732 && !initializer_constant_valid_p (inside_init
,
6733 TREE_TYPE (inside_init
)))
6735 error_init (init_loc
, "initializer element is not constant");
6736 inside_init
= error_mark_node
;
6738 else if (require_constant
&& !maybe_const
)
6739 pedwarn_init (init_loc
, 0,
6740 "initializer element is not a constant expression");
6742 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6743 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
6744 inside_init
= convert_for_assignment (init_loc
, UNKNOWN_LOCATION
,
6745 type
, inside_init
, origtype
,
6746 ic_init
, null_pointer_constant
,
6747 NULL_TREE
, NULL_TREE
, 0);
6751 /* Handle scalar types, including conversions. */
6753 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
6754 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
6755 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
)
6757 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
6758 && (TREE_CODE (init
) == STRING_CST
6759 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
6760 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
6762 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
6765 = convert_for_assignment (init_loc
, UNKNOWN_LOCATION
, type
,
6766 inside_init
, origtype
, ic_init
,
6767 null_pointer_constant
, NULL_TREE
, NULL_TREE
,
6770 /* Check to see if we have already given an error message. */
6771 if (inside_init
== error_mark_node
)
6773 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
6775 error_init (init_loc
, "initializer element is not constant");
6776 inside_init
= error_mark_node
;
6778 else if (require_constant
6779 && !initializer_constant_valid_p (inside_init
,
6780 TREE_TYPE (inside_init
)))
6782 error_init (init_loc
, "initializer element is not computable at "
6784 inside_init
= error_mark_node
;
6786 else if (require_constant
&& !maybe_const
)
6787 pedwarn_init (init_loc
, 0,
6788 "initializer element is not a constant expression");
6793 /* Come here only for records and arrays. */
6795 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
6797 error_init (init_loc
, "variable-sized object may not be initialized");
6798 return error_mark_node
;
6801 error_init (init_loc
, "invalid initializer");
6802 return error_mark_node
;
6805 /* Handle initializers that use braces. */
6807 /* Type of object we are accumulating a constructor for.
6808 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6809 static tree constructor_type
;
6811 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6813 static tree constructor_fields
;
6815 /* For an ARRAY_TYPE, this is the specified index
6816 at which to store the next element we get. */
6817 static tree constructor_index
;
6819 /* For an ARRAY_TYPE, this is the maximum index. */
6820 static tree constructor_max_index
;
6822 /* For a RECORD_TYPE, this is the first field not yet written out. */
6823 static tree constructor_unfilled_fields
;
6825 /* For an ARRAY_TYPE, this is the index of the first element
6826 not yet written out. */
6827 static tree constructor_unfilled_index
;
6829 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6830 This is so we can generate gaps between fields, when appropriate. */
6831 static tree constructor_bit_index
;
6833 /* If we are saving up the elements rather than allocating them,
6834 this is the list of elements so far (in reverse order,
6835 most recent first). */
6836 static vec
<constructor_elt
, va_gc
> *constructor_elements
;
6838 /* 1 if constructor should be incrementally stored into a constructor chain,
6839 0 if all the elements should be kept in AVL tree. */
6840 static int constructor_incremental
;
6842 /* 1 if so far this constructor's elements are all compile-time constants. */
6843 static int constructor_constant
;
6845 /* 1 if so far this constructor's elements are all valid address constants. */
6846 static int constructor_simple
;
6848 /* 1 if this constructor has an element that cannot be part of a
6849 constant expression. */
6850 static int constructor_nonconst
;
6852 /* 1 if this constructor is erroneous so far. */
6853 static int constructor_erroneous
;
6855 /* 1 if this constructor is the universal zero initializer { 0 }. */
6856 static int constructor_zeroinit
;
6858 /* Structure for managing pending initializer elements, organized as an
6863 struct init_node
*left
, *right
;
6864 struct init_node
*parent
;
6871 /* Tree of pending elements at this constructor level.
6872 These are elements encountered out of order
6873 which belong at places we haven't reached yet in actually
6875 Will never hold tree nodes across GC runs. */
6876 static struct init_node
*constructor_pending_elts
;
6878 /* The SPELLING_DEPTH of this constructor. */
6879 static int constructor_depth
;
6881 /* DECL node for which an initializer is being read.
6882 0 means we are reading a constructor expression
6883 such as (struct foo) {...}. */
6884 static tree constructor_decl
;
6886 /* Nonzero if this is an initializer for a top-level decl. */
6887 static int constructor_top_level
;
6889 /* Nonzero if there were any member designators in this initializer. */
6890 static int constructor_designated
;
6892 /* Nesting depth of designator list. */
6893 static int designator_depth
;
6895 /* Nonzero if there were diagnosed errors in this designator list. */
6896 static int designator_erroneous
;
6899 /* This stack has a level for each implicit or explicit level of
6900 structuring in the initializer, including the outermost one. It
6901 saves the values of most of the variables above. */
6903 struct constructor_range_stack
;
6905 struct constructor_stack
6907 struct constructor_stack
*next
;
6912 tree unfilled_index
;
6913 tree unfilled_fields
;
6915 vec
<constructor_elt
, va_gc
> *elements
;
6916 struct init_node
*pending_elts
;
6919 /* If value nonzero, this value should replace the entire
6920 constructor at this level. */
6921 struct c_expr replacement_value
;
6922 struct constructor_range_stack
*range_stack
;
6931 int designator_depth
;
6934 static struct constructor_stack
*constructor_stack
;
6936 /* This stack represents designators from some range designator up to
6937 the last designator in the list. */
6939 struct constructor_range_stack
6941 struct constructor_range_stack
*next
, *prev
;
6942 struct constructor_stack
*stack
;
6949 static struct constructor_range_stack
*constructor_range_stack
;
6951 /* This stack records separate initializers that are nested.
6952 Nested initializers can't happen in ANSI C, but GNU C allows them
6953 in cases like { ... (struct foo) { ... } ... }. */
6955 struct initializer_stack
6957 struct initializer_stack
*next
;
6959 struct constructor_stack
*constructor_stack
;
6960 struct constructor_range_stack
*constructor_range_stack
;
6961 vec
<constructor_elt
, va_gc
> *elements
;
6962 struct spelling
*spelling
;
6963 struct spelling
*spelling_base
;
6966 char require_constant_value
;
6967 char require_constant_elements
;
6970 static struct initializer_stack
*initializer_stack
;
6972 /* Prepare to parse and output the initializer for variable DECL. */
6975 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
)
6978 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
6980 p
->decl
= constructor_decl
;
6981 p
->require_constant_value
= require_constant_value
;
6982 p
->require_constant_elements
= require_constant_elements
;
6983 p
->constructor_stack
= constructor_stack
;
6984 p
->constructor_range_stack
= constructor_range_stack
;
6985 p
->elements
= constructor_elements
;
6986 p
->spelling
= spelling
;
6987 p
->spelling_base
= spelling_base
;
6988 p
->spelling_size
= spelling_size
;
6989 p
->top_level
= constructor_top_level
;
6990 p
->next
= initializer_stack
;
6991 initializer_stack
= p
;
6993 constructor_decl
= decl
;
6994 constructor_designated
= 0;
6995 constructor_top_level
= top_level
;
6997 if (decl
!= 0 && decl
!= error_mark_node
)
6999 require_constant_value
= TREE_STATIC (decl
);
7000 require_constant_elements
7001 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
7002 /* For a scalar, you can always use any value to initialize,
7003 even within braces. */
7004 && (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
7005 || TREE_CODE (TREE_TYPE (decl
)) == RECORD_TYPE
7006 || TREE_CODE (TREE_TYPE (decl
)) == UNION_TYPE
7007 || TREE_CODE (TREE_TYPE (decl
)) == QUAL_UNION_TYPE
));
7008 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
7012 require_constant_value
= 0;
7013 require_constant_elements
= 0;
7014 locus
= _("(anonymous)");
7017 constructor_stack
= 0;
7018 constructor_range_stack
= 0;
7020 found_missing_braces
= 0;
7024 RESTORE_SPELLING_DEPTH (0);
7027 push_string (locus
);
7033 struct initializer_stack
*p
= initializer_stack
;
7035 /* Free the whole constructor stack of this initializer. */
7036 while (constructor_stack
)
7038 struct constructor_stack
*q
= constructor_stack
;
7039 constructor_stack
= q
->next
;
7043 gcc_assert (!constructor_range_stack
);
7045 /* Pop back to the data of the outer initializer (if any). */
7046 free (spelling_base
);
7048 constructor_decl
= p
->decl
;
7049 require_constant_value
= p
->require_constant_value
;
7050 require_constant_elements
= p
->require_constant_elements
;
7051 constructor_stack
= p
->constructor_stack
;
7052 constructor_range_stack
= p
->constructor_range_stack
;
7053 constructor_elements
= p
->elements
;
7054 spelling
= p
->spelling
;
7055 spelling_base
= p
->spelling_base
;
7056 spelling_size
= p
->spelling_size
;
7057 constructor_top_level
= p
->top_level
;
7058 initializer_stack
= p
->next
;
7062 /* Call here when we see the initializer is surrounded by braces.
7063 This is instead of a call to push_init_level;
7064 it is matched by a call to pop_init_level.
7066 TYPE is the type to initialize, for a constructor expression.
7067 For an initializer for a decl, TYPE is zero. */
7070 really_start_incremental_init (tree type
)
7072 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
7075 type
= TREE_TYPE (constructor_decl
);
7077 if (TREE_CODE (type
) == VECTOR_TYPE
7078 && TYPE_VECTOR_OPAQUE (type
))
7079 error ("opaque vector types cannot be initialized");
7081 p
->type
= constructor_type
;
7082 p
->fields
= constructor_fields
;
7083 p
->index
= constructor_index
;
7084 p
->max_index
= constructor_max_index
;
7085 p
->unfilled_index
= constructor_unfilled_index
;
7086 p
->unfilled_fields
= constructor_unfilled_fields
;
7087 p
->bit_index
= constructor_bit_index
;
7088 p
->elements
= constructor_elements
;
7089 p
->constant
= constructor_constant
;
7090 p
->simple
= constructor_simple
;
7091 p
->nonconst
= constructor_nonconst
;
7092 p
->erroneous
= constructor_erroneous
;
7093 p
->pending_elts
= constructor_pending_elts
;
7094 p
->depth
= constructor_depth
;
7095 p
->replacement_value
.value
= 0;
7096 p
->replacement_value
.original_code
= ERROR_MARK
;
7097 p
->replacement_value
.original_type
= NULL
;
7101 p
->incremental
= constructor_incremental
;
7102 p
->designated
= constructor_designated
;
7103 p
->designator_depth
= designator_depth
;
7105 constructor_stack
= p
;
7107 constructor_constant
= 1;
7108 constructor_simple
= 1;
7109 constructor_nonconst
= 0;
7110 constructor_depth
= SPELLING_DEPTH ();
7111 constructor_elements
= NULL
;
7112 constructor_pending_elts
= 0;
7113 constructor_type
= type
;
7114 constructor_incremental
= 1;
7115 constructor_designated
= 0;
7116 constructor_zeroinit
= 1;
7117 designator_depth
= 0;
7118 designator_erroneous
= 0;
7120 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7121 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7123 constructor_fields
= TYPE_FIELDS (constructor_type
);
7124 /* Skip any nameless bit fields at the beginning. */
7125 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7126 && DECL_NAME (constructor_fields
) == 0)
7127 constructor_fields
= DECL_CHAIN (constructor_fields
);
7129 constructor_unfilled_fields
= constructor_fields
;
7130 constructor_bit_index
= bitsize_zero_node
;
7132 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7134 if (TYPE_DOMAIN (constructor_type
))
7136 constructor_max_index
7137 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7139 /* Detect non-empty initializations of zero-length arrays. */
7140 if (constructor_max_index
== NULL_TREE
7141 && TYPE_SIZE (constructor_type
))
7142 constructor_max_index
= integer_minus_one_node
;
7144 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7145 to initialize VLAs will cause a proper error; avoid tree
7146 checking errors as well by setting a safe value. */
7147 if (constructor_max_index
7148 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7149 constructor_max_index
= integer_minus_one_node
;
7152 = convert (bitsizetype
,
7153 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7157 constructor_index
= bitsize_zero_node
;
7158 constructor_max_index
= NULL_TREE
;
7161 constructor_unfilled_index
= constructor_index
;
7163 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7165 /* Vectors are like simple fixed-size arrays. */
7166 constructor_max_index
=
7167 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7168 constructor_index
= bitsize_zero_node
;
7169 constructor_unfilled_index
= constructor_index
;
7173 /* Handle the case of int x = {5}; */
7174 constructor_fields
= constructor_type
;
7175 constructor_unfilled_fields
= constructor_type
;
7179 /* Push down into a subobject, for initialization.
7180 If this is for an explicit set of braces, IMPLICIT is 0.
7181 If it is because the next element belongs at a lower level,
7182 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7185 push_init_level (location_t loc
, int implicit
,
7186 struct obstack
*braced_init_obstack
)
7188 struct constructor_stack
*p
;
7189 tree value
= NULL_TREE
;
7191 /* If we've exhausted any levels that didn't have braces,
7192 pop them now. If implicit == 1, this will have been done in
7193 process_init_element; do not repeat it here because in the case
7194 of excess initializers for an empty aggregate this leads to an
7195 infinite cycle of popping a level and immediately recreating
7199 while (constructor_stack
->implicit
)
7201 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7202 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7203 && constructor_fields
== 0)
7204 process_init_element (input_location
,
7205 pop_init_level (loc
, 1, braced_init_obstack
),
7206 true, braced_init_obstack
);
7207 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
7208 && constructor_max_index
7209 && tree_int_cst_lt (constructor_max_index
,
7211 process_init_element (input_location
,
7212 pop_init_level (loc
, 1, braced_init_obstack
),
7213 true, braced_init_obstack
);
7219 /* Unless this is an explicit brace, we need to preserve previous
7223 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7224 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7225 && constructor_fields
)
7226 value
= find_init_member (constructor_fields
, braced_init_obstack
);
7227 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7228 value
= find_init_member (constructor_index
, braced_init_obstack
);
7231 p
= XNEW (struct constructor_stack
);
7232 p
->type
= constructor_type
;
7233 p
->fields
= constructor_fields
;
7234 p
->index
= constructor_index
;
7235 p
->max_index
= constructor_max_index
;
7236 p
->unfilled_index
= constructor_unfilled_index
;
7237 p
->unfilled_fields
= constructor_unfilled_fields
;
7238 p
->bit_index
= constructor_bit_index
;
7239 p
->elements
= constructor_elements
;
7240 p
->constant
= constructor_constant
;
7241 p
->simple
= constructor_simple
;
7242 p
->nonconst
= constructor_nonconst
;
7243 p
->erroneous
= constructor_erroneous
;
7244 p
->pending_elts
= constructor_pending_elts
;
7245 p
->depth
= constructor_depth
;
7246 p
->replacement_value
.value
= 0;
7247 p
->replacement_value
.original_code
= ERROR_MARK
;
7248 p
->replacement_value
.original_type
= NULL
;
7249 p
->implicit
= implicit
;
7251 p
->incremental
= constructor_incremental
;
7252 p
->designated
= constructor_designated
;
7253 p
->designator_depth
= designator_depth
;
7254 p
->next
= constructor_stack
;
7256 constructor_stack
= p
;
7258 constructor_constant
= 1;
7259 constructor_simple
= 1;
7260 constructor_nonconst
= 0;
7261 constructor_depth
= SPELLING_DEPTH ();
7262 constructor_elements
= NULL
;
7263 constructor_incremental
= 1;
7264 constructor_designated
= 0;
7265 constructor_pending_elts
= 0;
7268 p
->range_stack
= constructor_range_stack
;
7269 constructor_range_stack
= 0;
7270 designator_depth
= 0;
7271 designator_erroneous
= 0;
7274 /* Don't die if an entire brace-pair level is superfluous
7275 in the containing level. */
7276 if (constructor_type
== 0)
7278 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
7279 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7281 /* Don't die if there are extra init elts at the end. */
7282 if (constructor_fields
== 0)
7283 constructor_type
= 0;
7286 constructor_type
= TREE_TYPE (constructor_fields
);
7287 push_member_name (constructor_fields
);
7288 constructor_depth
++;
7290 /* If upper initializer is designated, then mark this as
7291 designated too to prevent bogus warnings. */
7292 constructor_designated
= p
->designated
;
7294 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7296 constructor_type
= TREE_TYPE (constructor_type
);
7297 push_array_bounds (tree_to_uhwi (constructor_index
));
7298 constructor_depth
++;
7301 if (constructor_type
== 0)
7303 error_init (loc
, "extra brace group at end of initializer");
7304 constructor_fields
= 0;
7305 constructor_unfilled_fields
= 0;
7309 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
7311 constructor_constant
= TREE_CONSTANT (value
);
7312 constructor_simple
= TREE_STATIC (value
);
7313 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
7314 constructor_elements
= CONSTRUCTOR_ELTS (value
);
7315 if (!vec_safe_is_empty (constructor_elements
)
7316 && (TREE_CODE (constructor_type
) == RECORD_TYPE
7317 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
7318 set_nonincremental_init (braced_init_obstack
);
7322 found_missing_braces
= 1;
7324 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7325 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7327 constructor_fields
= TYPE_FIELDS (constructor_type
);
7328 /* Skip any nameless bit fields at the beginning. */
7329 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7330 && DECL_NAME (constructor_fields
) == 0)
7331 constructor_fields
= DECL_CHAIN (constructor_fields
);
7333 constructor_unfilled_fields
= constructor_fields
;
7334 constructor_bit_index
= bitsize_zero_node
;
7336 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7338 /* Vectors are like simple fixed-size arrays. */
7339 constructor_max_index
=
7340 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7341 constructor_index
= bitsize_int (0);
7342 constructor_unfilled_index
= constructor_index
;
7344 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7346 if (TYPE_DOMAIN (constructor_type
))
7348 constructor_max_index
7349 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7351 /* Detect non-empty initializations of zero-length arrays. */
7352 if (constructor_max_index
== NULL_TREE
7353 && TYPE_SIZE (constructor_type
))
7354 constructor_max_index
= integer_minus_one_node
;
7356 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7357 to initialize VLAs will cause a proper error; avoid tree
7358 checking errors as well by setting a safe value. */
7359 if (constructor_max_index
7360 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7361 constructor_max_index
= integer_minus_one_node
;
7364 = convert (bitsizetype
,
7365 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7368 constructor_index
= bitsize_zero_node
;
7370 constructor_unfilled_index
= constructor_index
;
7371 if (value
&& TREE_CODE (value
) == STRING_CST
)
7373 /* We need to split the char/wchar array into individual
7374 characters, so that we don't have to special case it
7376 set_nonincremental_init_from_string (value
, braced_init_obstack
);
7381 if (constructor_type
!= error_mark_node
)
7382 warning_init (input_location
, 0, "braces around scalar initializer");
7383 constructor_fields
= constructor_type
;
7384 constructor_unfilled_fields
= constructor_type
;
7388 /* At the end of an implicit or explicit brace level,
7389 finish up that level of constructor. If a single expression
7390 with redundant braces initialized that level, return the
7391 c_expr structure for that expression. Otherwise, the original_code
7392 element is set to ERROR_MARK.
7393 If we were outputting the elements as they are read, return 0 as the value
7394 from inner levels (process_init_element ignores that),
7395 but return error_mark_node as the value from the outermost level
7396 (that's what we want to put in DECL_INITIAL).
7397 Otherwise, return a CONSTRUCTOR expression as the value. */
7400 pop_init_level (location_t loc
, int implicit
,
7401 struct obstack
*braced_init_obstack
)
7403 struct constructor_stack
*p
;
7406 ret
.original_code
= ERROR_MARK
;
7407 ret
.original_type
= NULL
;
7411 /* When we come to an explicit close brace,
7412 pop any inner levels that didn't have explicit braces. */
7413 while (constructor_stack
->implicit
)
7414 process_init_element (input_location
,
7415 pop_init_level (loc
, 1, braced_init_obstack
),
7416 true, braced_init_obstack
);
7417 gcc_assert (!constructor_range_stack
);
7420 /* Now output all pending elements. */
7421 constructor_incremental
= 1;
7422 output_pending_init_elements (1, braced_init_obstack
);
7424 p
= constructor_stack
;
7426 /* Error for initializing a flexible array member, or a zero-length
7427 array member in an inappropriate context. */
7428 if (constructor_type
&& constructor_fields
7429 && TREE_CODE (constructor_type
) == ARRAY_TYPE
7430 && TYPE_DOMAIN (constructor_type
)
7431 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
7433 /* Silently discard empty initializations. The parser will
7434 already have pedwarned for empty brackets. */
7435 if (integer_zerop (constructor_unfilled_index
))
7436 constructor_type
= NULL_TREE
;
7439 gcc_assert (!TYPE_SIZE (constructor_type
));
7441 if (constructor_depth
> 2)
7442 error_init (loc
, "initialization of flexible array member in a nested context");
7444 pedwarn_init (loc
, OPT_Wpedantic
,
7445 "initialization of a flexible array member");
7447 /* We have already issued an error message for the existence
7448 of a flexible array member not at the end of the structure.
7449 Discard the initializer so that we do not die later. */
7450 if (DECL_CHAIN (constructor_fields
) != NULL_TREE
)
7451 constructor_type
= NULL_TREE
;
7455 /* Initialization with { } counts as zeroinit. */
7456 if (vec_safe_length (constructor_elements
) == 0)
7457 constructor_zeroinit
= 1;
7458 /* If the constructor has more than one element, it can't be { 0 }. */
7459 else if (vec_safe_length (constructor_elements
) != 1)
7460 constructor_zeroinit
= 0;
7462 /* Warn when some structs are initialized with direct aggregation. */
7463 if (!implicit
&& found_missing_braces
&& warn_missing_braces
7464 && !constructor_zeroinit
)
7466 warning_init (loc
, OPT_Wmissing_braces
,
7467 "missing braces around initializer");
7470 /* Warn when some struct elements are implicitly initialized to zero. */
7471 if (warn_missing_field_initializers
7473 && TREE_CODE (constructor_type
) == RECORD_TYPE
7474 && constructor_unfilled_fields
)
7476 /* Do not warn for flexible array members or zero-length arrays. */
7477 while (constructor_unfilled_fields
7478 && (!DECL_SIZE (constructor_unfilled_fields
)
7479 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
7480 constructor_unfilled_fields
= DECL_CHAIN (constructor_unfilled_fields
);
7482 if (constructor_unfilled_fields
7483 /* Do not warn if this level of the initializer uses member
7484 designators; it is likely to be deliberate. */
7485 && !constructor_designated
7486 /* Do not warn about initializing with { 0 } or with { }. */
7487 && !constructor_zeroinit
)
7489 if (warning_at (input_location
, OPT_Wmissing_field_initializers
,
7490 "missing initializer for field %qD of %qT",
7491 constructor_unfilled_fields
,
7493 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields
),
7494 "%qD declared here", constructor_unfilled_fields
);
7498 /* Pad out the end of the structure. */
7499 if (p
->replacement_value
.value
)
7500 /* If this closes a superfluous brace pair,
7501 just pass out the element between them. */
7502 ret
= p
->replacement_value
;
7503 else if (constructor_type
== 0)
7505 else if (TREE_CODE (constructor_type
) != RECORD_TYPE
7506 && TREE_CODE (constructor_type
) != UNION_TYPE
7507 && TREE_CODE (constructor_type
) != ARRAY_TYPE
7508 && TREE_CODE (constructor_type
) != VECTOR_TYPE
)
7510 /* A nonincremental scalar initializer--just return
7511 the element, after verifying there is just one. */
7512 if (vec_safe_is_empty (constructor_elements
))
7514 if (!constructor_erroneous
)
7515 error_init (loc
, "empty scalar initializer");
7516 ret
.value
= error_mark_node
;
7518 else if (vec_safe_length (constructor_elements
) != 1)
7520 error_init (loc
, "extra elements in scalar initializer");
7521 ret
.value
= (*constructor_elements
)[0].value
;
7524 ret
.value
= (*constructor_elements
)[0].value
;
7528 if (constructor_erroneous
)
7529 ret
.value
= error_mark_node
;
7532 ret
.value
= build_constructor (constructor_type
,
7533 constructor_elements
);
7534 if (constructor_constant
)
7535 TREE_CONSTANT (ret
.value
) = 1;
7536 if (constructor_constant
&& constructor_simple
)
7537 TREE_STATIC (ret
.value
) = 1;
7538 if (constructor_nonconst
)
7539 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
7543 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
7545 if (constructor_nonconst
)
7546 ret
.original_code
= C_MAYBE_CONST_EXPR
;
7547 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
7548 ret
.original_code
= ERROR_MARK
;
7551 constructor_type
= p
->type
;
7552 constructor_fields
= p
->fields
;
7553 constructor_index
= p
->index
;
7554 constructor_max_index
= p
->max_index
;
7555 constructor_unfilled_index
= p
->unfilled_index
;
7556 constructor_unfilled_fields
= p
->unfilled_fields
;
7557 constructor_bit_index
= p
->bit_index
;
7558 constructor_elements
= p
->elements
;
7559 constructor_constant
= p
->constant
;
7560 constructor_simple
= p
->simple
;
7561 constructor_nonconst
= p
->nonconst
;
7562 constructor_erroneous
= p
->erroneous
;
7563 constructor_incremental
= p
->incremental
;
7564 constructor_designated
= p
->designated
;
7565 designator_depth
= p
->designator_depth
;
7566 constructor_pending_elts
= p
->pending_elts
;
7567 constructor_depth
= p
->depth
;
7569 constructor_range_stack
= p
->range_stack
;
7570 RESTORE_SPELLING_DEPTH (constructor_depth
);
7572 constructor_stack
= p
->next
;
7575 if (ret
.value
== 0 && constructor_stack
== 0)
7576 ret
.value
= error_mark_node
;
7580 /* Common handling for both array range and field name designators.
7581 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7584 set_designator (location_t loc
, int array
,
7585 struct obstack
*braced_init_obstack
)
7588 enum tree_code subcode
;
7590 /* Don't die if an entire brace-pair level is superfluous
7591 in the containing level. */
7592 if (constructor_type
== 0)
7595 /* If there were errors in this designator list already, bail out
7597 if (designator_erroneous
)
7600 if (!designator_depth
)
7602 gcc_assert (!constructor_range_stack
);
7604 /* Designator list starts at the level of closest explicit
7606 while (constructor_stack
->implicit
)
7607 process_init_element (input_location
,
7608 pop_init_level (loc
, 1, braced_init_obstack
),
7609 true, braced_init_obstack
);
7610 constructor_designated
= 1;
7614 switch (TREE_CODE (constructor_type
))
7618 subtype
= TREE_TYPE (constructor_fields
);
7619 if (subtype
!= error_mark_node
)
7620 subtype
= TYPE_MAIN_VARIANT (subtype
);
7623 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
7629 subcode
= TREE_CODE (subtype
);
7630 if (array
&& subcode
!= ARRAY_TYPE
)
7632 error_init (loc
, "array index in non-array initializer");
7635 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
7637 error_init (loc
, "field name not in record or union initializer");
7641 constructor_designated
= 1;
7642 push_init_level (loc
, 2, braced_init_obstack
);
7646 /* If there are range designators in designator list, push a new designator
7647 to constructor_range_stack. RANGE_END is end of such stack range or
7648 NULL_TREE if there is no range designator at this level. */
7651 push_range_stack (tree range_end
, struct obstack
* braced_init_obstack
)
7653 struct constructor_range_stack
*p
;
7655 p
= (struct constructor_range_stack
*)
7656 obstack_alloc (braced_init_obstack
,
7657 sizeof (struct constructor_range_stack
));
7658 p
->prev
= constructor_range_stack
;
7660 p
->fields
= constructor_fields
;
7661 p
->range_start
= constructor_index
;
7662 p
->index
= constructor_index
;
7663 p
->stack
= constructor_stack
;
7664 p
->range_end
= range_end
;
7665 if (constructor_range_stack
)
7666 constructor_range_stack
->next
= p
;
7667 constructor_range_stack
= p
;
7670 /* Within an array initializer, specify the next index to be initialized.
7671 FIRST is that index. If LAST is nonzero, then initialize a range
7672 of indices, running from FIRST through LAST. */
7675 set_init_index (location_t loc
, tree first
, tree last
,
7676 struct obstack
*braced_init_obstack
)
7678 if (set_designator (loc
, 1, braced_init_obstack
))
7681 designator_erroneous
= 1;
7683 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
7684 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
7686 error_init (loc
, "array index in initializer not of integer type");
7690 if (TREE_CODE (first
) != INTEGER_CST
)
7692 first
= c_fully_fold (first
, false, NULL
);
7693 if (TREE_CODE (first
) == INTEGER_CST
)
7694 pedwarn_init (loc
, OPT_Wpedantic
,
7695 "array index in initializer is not "
7696 "an integer constant expression");
7699 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
7701 last
= c_fully_fold (last
, false, NULL
);
7702 if (TREE_CODE (last
) == INTEGER_CST
)
7703 pedwarn_init (loc
, OPT_Wpedantic
,
7704 "array index in initializer is not "
7705 "an integer constant expression");
7708 if (TREE_CODE (first
) != INTEGER_CST
)
7709 error_init (loc
, "nonconstant array index in initializer");
7710 else if (last
!= 0 && TREE_CODE (last
) != INTEGER_CST
)
7711 error_init (loc
, "nonconstant array index in initializer");
7712 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
7713 error_init (loc
, "array index in non-array initializer");
7714 else if (tree_int_cst_sgn (first
) == -1)
7715 error_init (loc
, "array index in initializer exceeds array bounds");
7716 else if (constructor_max_index
7717 && tree_int_cst_lt (constructor_max_index
, first
))
7718 error_init (loc
, "array index in initializer exceeds array bounds");
7721 constant_expression_warning (first
);
7723 constant_expression_warning (last
);
7724 constructor_index
= convert (bitsizetype
, first
);
7725 if (tree_int_cst_lt (constructor_index
, first
))
7727 constructor_index
= copy_node (constructor_index
);
7728 TREE_OVERFLOW (constructor_index
) = 1;
7733 if (tree_int_cst_equal (first
, last
))
7735 else if (tree_int_cst_lt (last
, first
))
7737 error_init (loc
, "empty index range in initializer");
7742 last
= convert (bitsizetype
, last
);
7743 if (constructor_max_index
!= 0
7744 && tree_int_cst_lt (constructor_max_index
, last
))
7746 error_init (loc
, "array index range in initializer exceeds "
7754 designator_erroneous
= 0;
7755 if (constructor_range_stack
|| last
)
7756 push_range_stack (last
, braced_init_obstack
);
7760 /* Within a struct initializer, specify the next field to be initialized. */
7763 set_init_label (location_t loc
, tree fieldname
,
7764 struct obstack
*braced_init_obstack
)
7768 if (set_designator (loc
, 0, braced_init_obstack
))
7771 designator_erroneous
= 1;
7773 if (TREE_CODE (constructor_type
) != RECORD_TYPE
7774 && TREE_CODE (constructor_type
) != UNION_TYPE
)
7776 error_init (loc
, "field name not in record or union initializer");
7780 field
= lookup_field (constructor_type
, fieldname
);
7783 error ("unknown field %qE specified in initializer", fieldname
);
7787 constructor_fields
= TREE_VALUE (field
);
7789 designator_erroneous
= 0;
7790 if (constructor_range_stack
)
7791 push_range_stack (NULL_TREE
, braced_init_obstack
);
7792 field
= TREE_CHAIN (field
);
7795 if (set_designator (loc
, 0, braced_init_obstack
))
7799 while (field
!= NULL_TREE
);
7802 /* Add a new initializer to the tree of pending initializers. PURPOSE
7803 identifies the initializer, either array index or field in a structure.
7804 VALUE is the value of that index or field. If ORIGTYPE is not
7805 NULL_TREE, it is the original type of VALUE.
7807 IMPLICIT is true if value comes from pop_init_level (1),
7808 the new initializer has been merged with the existing one
7809 and thus no warnings should be emitted about overriding an
7810 existing initializer. */
7813 add_pending_init (location_t loc
, tree purpose
, tree value
, tree origtype
,
7814 bool implicit
, struct obstack
*braced_init_obstack
)
7816 struct init_node
*p
, **q
, *r
;
7818 q
= &constructor_pending_elts
;
7821 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7826 if (tree_int_cst_lt (purpose
, p
->purpose
))
7828 else if (tree_int_cst_lt (p
->purpose
, purpose
))
7834 if (TREE_SIDE_EFFECTS (p
->value
))
7835 warning_init (loc
, 0,
7836 "initialized field with side-effects "
7838 else if (warn_override_init
)
7839 warning_init (loc
, OPT_Woverride_init
,
7840 "initialized field overwritten");
7843 p
->origtype
= origtype
;
7852 bitpos
= bit_position (purpose
);
7856 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
7858 else if (p
->purpose
!= purpose
)
7864 if (TREE_SIDE_EFFECTS (p
->value
))
7865 warning_init (loc
, 0,
7866 "initialized field with side-effects "
7868 else if (warn_override_init
)
7869 warning_init (loc
, OPT_Woverride_init
,
7870 "initialized field overwritten");
7873 p
->origtype
= origtype
;
7879 r
= (struct init_node
*) obstack_alloc (braced_init_obstack
,
7880 sizeof (struct init_node
));
7881 r
->purpose
= purpose
;
7883 r
->origtype
= origtype
;
7893 struct init_node
*s
;
7897 if (p
->balance
== 0)
7899 else if (p
->balance
< 0)
7906 p
->left
->parent
= p
;
7923 constructor_pending_elts
= r
;
7928 struct init_node
*t
= r
->right
;
7932 r
->right
->parent
= r
;
7937 p
->left
->parent
= p
;
7940 p
->balance
= t
->balance
< 0;
7941 r
->balance
= -(t
->balance
> 0);
7956 constructor_pending_elts
= t
;
7962 /* p->balance == +1; growth of left side balances the node. */
7967 else /* r == p->right */
7969 if (p
->balance
== 0)
7970 /* Growth propagation from right side. */
7972 else if (p
->balance
> 0)
7979 p
->right
->parent
= p
;
7996 constructor_pending_elts
= r
;
7998 else /* r->balance == -1 */
8001 struct init_node
*t
= r
->left
;
8005 r
->left
->parent
= r
;
8010 p
->right
->parent
= p
;
8013 r
->balance
= (t
->balance
< 0);
8014 p
->balance
= -(t
->balance
> 0);
8029 constructor_pending_elts
= t
;
8035 /* p->balance == -1; growth of right side balances the node. */
8046 /* Build AVL tree from a sorted chain. */
8049 set_nonincremental_init (struct obstack
* braced_init_obstack
)
8051 unsigned HOST_WIDE_INT ix
;
8054 if (TREE_CODE (constructor_type
) != RECORD_TYPE
8055 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
8058 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
8059 add_pending_init (input_location
, index
, value
, NULL_TREE
, true,
8060 braced_init_obstack
);
8061 constructor_elements
= NULL
;
8062 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8064 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
8065 /* Skip any nameless bit fields at the beginning. */
8066 while (constructor_unfilled_fields
!= 0
8067 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8068 && DECL_NAME (constructor_unfilled_fields
) == 0)
8069 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
8072 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8074 if (TYPE_DOMAIN (constructor_type
))
8075 constructor_unfilled_index
8076 = convert (bitsizetype
,
8077 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
8079 constructor_unfilled_index
= bitsize_zero_node
;
8081 constructor_incremental
= 0;
8084 /* Build AVL tree from a string constant. */
8087 set_nonincremental_init_from_string (tree str
,
8088 struct obstack
* braced_init_obstack
)
8090 tree value
, purpose
, type
;
8091 HOST_WIDE_INT val
[2];
8092 const char *p
, *end
;
8093 int byte
, wchar_bytes
, charwidth
, bitpos
;
8095 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
8097 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
8098 charwidth
= TYPE_PRECISION (char_type_node
);
8099 type
= TREE_TYPE (constructor_type
);
8100 p
= TREE_STRING_POINTER (str
);
8101 end
= p
+ TREE_STRING_LENGTH (str
);
8103 for (purpose
= bitsize_zero_node
;
8105 && !(constructor_max_index
8106 && tree_int_cst_lt (constructor_max_index
, purpose
));
8107 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
8109 if (wchar_bytes
== 1)
8111 val
[0] = (unsigned char) *p
++;
8118 for (byte
= 0; byte
< wchar_bytes
; byte
++)
8120 if (BYTES_BIG_ENDIAN
)
8121 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
8123 bitpos
= byte
* charwidth
;
8124 val
[bitpos
% HOST_BITS_PER_WIDE_INT
]
8125 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
8126 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
8130 if (!TYPE_UNSIGNED (type
))
8132 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
8133 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
8135 if (val
[0] & (((HOST_WIDE_INT
) 1) << (bitpos
- 1)))
8137 val
[0] |= ((HOST_WIDE_INT
) -1) << bitpos
;
8141 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
8146 else if (val
[1] & (((HOST_WIDE_INT
) 1)
8147 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
8148 val
[1] |= ((HOST_WIDE_INT
) -1)
8149 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
8152 value
= wide_int_to_tree (type
,
8153 wide_int::from_array (val
, 2,
8154 HOST_BITS_PER_WIDE_INT
* 2));
8155 add_pending_init (input_location
, purpose
, value
, NULL_TREE
, true,
8156 braced_init_obstack
);
8159 constructor_incremental
= 0;
8162 /* Return value of FIELD in pending initializer or zero if the field was
8163 not initialized yet. */
8166 find_init_member (tree field
, struct obstack
* braced_init_obstack
)
8168 struct init_node
*p
;
8170 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8172 if (constructor_incremental
8173 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8174 set_nonincremental_init (braced_init_obstack
);
8176 p
= constructor_pending_elts
;
8179 if (tree_int_cst_lt (field
, p
->purpose
))
8181 else if (tree_int_cst_lt (p
->purpose
, field
))
8187 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8189 tree bitpos
= bit_position (field
);
8191 if (constructor_incremental
8192 && (!constructor_unfilled_fields
8193 || tree_int_cst_lt (bitpos
,
8194 bit_position (constructor_unfilled_fields
))))
8195 set_nonincremental_init (braced_init_obstack
);
8197 p
= constructor_pending_elts
;
8200 if (field
== p
->purpose
)
8202 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
8208 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8210 if (!vec_safe_is_empty (constructor_elements
)
8211 && (constructor_elements
->last ().index
== field
))
8212 return constructor_elements
->last ().value
;
8217 /* "Output" the next constructor element.
8218 At top level, really output it to assembler code now.
8219 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8220 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8221 TYPE is the data type that the containing data type wants here.
8222 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8223 If VALUE is a string constant, STRICT_STRING is true if it is
8224 unparenthesized or we should not warn here for it being parenthesized.
8225 For other types of VALUE, STRICT_STRING is not used.
8227 PENDING if non-nil means output pending elements that belong
8228 right after this element. (PENDING is normally 1;
8229 it is 0 while outputting pending elements, to avoid recursion.)
8231 IMPLICIT is true if value comes from pop_init_level (1),
8232 the new initializer has been merged with the existing one
8233 and thus no warnings should be emitted about overriding an
8234 existing initializer. */
8237 output_init_element (location_t loc
, tree value
, tree origtype
,
8238 bool strict_string
, tree type
, tree field
, int pending
,
8239 bool implicit
, struct obstack
* braced_init_obstack
)
8241 tree semantic_type
= NULL_TREE
;
8242 bool maybe_const
= true;
8245 if (type
== error_mark_node
|| value
== error_mark_node
)
8247 constructor_erroneous
= 1;
8250 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
8251 && (TREE_CODE (value
) == STRING_CST
8252 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
8253 && !(TREE_CODE (value
) == STRING_CST
8254 && TREE_CODE (type
) == ARRAY_TYPE
8255 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
8256 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
8257 TYPE_MAIN_VARIANT (type
)))
8258 value
= array_to_pointer_conversion (input_location
, value
);
8260 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
8261 && require_constant_value
&& pending
)
8263 /* As an extension, allow initializing objects with static storage
8264 duration with compound literals (which are then treated just as
8265 the brace enclosed list they contain). */
8267 pedwarn_init (loc
, OPT_Wpedantic
, "initializer element is not "
8269 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
8270 value
= DECL_INITIAL (decl
);
8273 npc
= null_pointer_constant_p (value
);
8274 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
8276 semantic_type
= TREE_TYPE (value
);
8277 value
= TREE_OPERAND (value
, 0);
8279 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
8281 if (value
== error_mark_node
)
8282 constructor_erroneous
= 1;
8283 else if (!TREE_CONSTANT (value
))
8284 constructor_constant
= 0;
8285 else if (!initializer_constant_valid_p (value
, TREE_TYPE (value
))
8286 || ((TREE_CODE (constructor_type
) == RECORD_TYPE
8287 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8288 && DECL_C_BIT_FIELD (field
)
8289 && TREE_CODE (value
) != INTEGER_CST
))
8290 constructor_simple
= 0;
8292 constructor_nonconst
= 1;
8294 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
8296 if (require_constant_value
)
8298 error_init (loc
, "initializer element is not constant");
8299 value
= error_mark_node
;
8301 else if (require_constant_elements
)
8302 pedwarn (loc
, OPT_Wpedantic
,
8303 "initializer element is not computable at load time");
8305 else if (!maybe_const
8306 && (require_constant_value
|| require_constant_elements
))
8307 pedwarn_init (loc
, OPT_Wpedantic
,
8308 "initializer element is not a constant expression");
8310 /* Issue -Wc++-compat warnings about initializing a bitfield with
8313 && field
!= NULL_TREE
8314 && TREE_CODE (field
) == FIELD_DECL
8315 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
8316 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
8317 != TYPE_MAIN_VARIANT (type
))
8318 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
8320 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
8321 if (checktype
!= error_mark_node
8322 && (TYPE_MAIN_VARIANT (checktype
)
8323 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
8324 warning_init (loc
, OPT_Wc___compat
,
8325 "enum conversion in initialization is invalid in C++");
8328 /* If this field is empty (and not at the end of structure),
8329 don't do anything other than checking the initializer. */
8331 && (TREE_TYPE (field
) == error_mark_node
8332 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
8333 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
8334 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
8335 || DECL_CHAIN (field
)))))
8339 value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
8340 value
= digest_init (loc
, type
, value
, origtype
, npc
, strict_string
,
8341 require_constant_value
);
8342 if (value
== error_mark_node
)
8344 constructor_erroneous
= 1;
8347 if (require_constant_value
|| require_constant_elements
)
8348 constant_expression_warning (value
);
8350 /* If this element doesn't come next in sequence,
8351 put it on constructor_pending_elts. */
8352 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
8353 && (!constructor_incremental
8354 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
8356 if (constructor_incremental
8357 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8358 set_nonincremental_init (braced_init_obstack
);
8360 add_pending_init (loc
, field
, value
, origtype
, implicit
,
8361 braced_init_obstack
);
8364 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8365 && (!constructor_incremental
8366 || field
!= constructor_unfilled_fields
))
8368 /* We do this for records but not for unions. In a union,
8369 no matter which field is specified, it can be initialized
8370 right away since it starts at the beginning of the union. */
8371 if (constructor_incremental
)
8373 if (!constructor_unfilled_fields
)
8374 set_nonincremental_init (braced_init_obstack
);
8377 tree bitpos
, unfillpos
;
8379 bitpos
= bit_position (field
);
8380 unfillpos
= bit_position (constructor_unfilled_fields
);
8382 if (tree_int_cst_lt (bitpos
, unfillpos
))
8383 set_nonincremental_init (braced_init_obstack
);
8387 add_pending_init (loc
, field
, value
, origtype
, implicit
,
8388 braced_init_obstack
);
8391 else if (TREE_CODE (constructor_type
) == UNION_TYPE
8392 && !vec_safe_is_empty (constructor_elements
))
8396 if (TREE_SIDE_EFFECTS (constructor_elements
->last ().value
))
8397 warning_init (loc
, 0,
8398 "initialized field with side-effects overwritten");
8399 else if (warn_override_init
)
8400 warning_init (loc
, OPT_Woverride_init
,
8401 "initialized field overwritten");
8404 /* We can have just one union field set. */
8405 constructor_elements
= NULL
;
8408 /* Otherwise, output this element either to
8409 constructor_elements or to the assembler file. */
8411 constructor_elt celt
= {field
, value
};
8412 vec_safe_push (constructor_elements
, celt
);
8414 /* Advance the variable that indicates sequential elements output. */
8415 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8416 constructor_unfilled_index
8417 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
8419 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8421 constructor_unfilled_fields
8422 = DECL_CHAIN (constructor_unfilled_fields
);
8424 /* Skip any nameless bit fields. */
8425 while (constructor_unfilled_fields
!= 0
8426 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8427 && DECL_NAME (constructor_unfilled_fields
) == 0)
8428 constructor_unfilled_fields
=
8429 DECL_CHAIN (constructor_unfilled_fields
);
8431 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8432 constructor_unfilled_fields
= 0;
8434 /* Now output any pending elements which have become next. */
8436 output_pending_init_elements (0, braced_init_obstack
);
8439 /* Output any pending elements which have become next.
8440 As we output elements, constructor_unfilled_{fields,index}
8441 advances, which may cause other elements to become next;
8442 if so, they too are output.
8444 If ALL is 0, we return when there are
8445 no more pending elements to output now.
8447 If ALL is 1, we output space as necessary so that
8448 we can output all the pending elements. */
8450 output_pending_init_elements (int all
, struct obstack
* braced_init_obstack
)
8452 struct init_node
*elt
= constructor_pending_elts
;
8457 /* Look through the whole pending tree.
8458 If we find an element that should be output now,
8459 output it. Otherwise, set NEXT to the element
8460 that comes first among those still pending. */
8465 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8467 if (tree_int_cst_equal (elt
->purpose
,
8468 constructor_unfilled_index
))
8469 output_init_element (input_location
, elt
->value
, elt
->origtype
,
8470 true, TREE_TYPE (constructor_type
),
8471 constructor_unfilled_index
, 0, false,
8472 braced_init_obstack
);
8473 else if (tree_int_cst_lt (constructor_unfilled_index
,
8476 /* Advance to the next smaller node. */
8481 /* We have reached the smallest node bigger than the
8482 current unfilled index. Fill the space first. */
8483 next
= elt
->purpose
;
8489 /* Advance to the next bigger node. */
8494 /* We have reached the biggest node in a subtree. Find
8495 the parent of it, which is the next bigger node. */
8496 while (elt
->parent
&& elt
->parent
->right
== elt
)
8499 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
8502 next
= elt
->purpose
;
8508 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8509 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8511 tree ctor_unfilled_bitpos
, elt_bitpos
;
8513 /* If the current record is complete we are done. */
8514 if (constructor_unfilled_fields
== 0)
8517 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
8518 elt_bitpos
= bit_position (elt
->purpose
);
8519 /* We can't compare fields here because there might be empty
8520 fields in between. */
8521 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
8523 constructor_unfilled_fields
= elt
->purpose
;
8524 output_init_element (input_location
, elt
->value
, elt
->origtype
,
8525 true, TREE_TYPE (elt
->purpose
),
8526 elt
->purpose
, 0, false,
8527 braced_init_obstack
);
8529 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
8531 /* Advance to the next smaller node. */
8536 /* We have reached the smallest node bigger than the
8537 current unfilled field. Fill the space first. */
8538 next
= elt
->purpose
;
8544 /* Advance to the next bigger node. */
8549 /* We have reached the biggest node in a subtree. Find
8550 the parent of it, which is the next bigger node. */
8551 while (elt
->parent
&& elt
->parent
->right
== elt
)
8555 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
8556 bit_position (elt
->purpose
))))
8558 next
= elt
->purpose
;
8566 /* Ordinarily return, but not if we want to output all
8567 and there are elements left. */
8568 if (!(all
&& next
!= 0))
8571 /* If it's not incremental, just skip over the gap, so that after
8572 jumping to retry we will output the next successive element. */
8573 if (TREE_CODE (constructor_type
) == RECORD_TYPE
8574 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8575 constructor_unfilled_fields
= next
;
8576 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8577 constructor_unfilled_index
= next
;
8579 /* ELT now points to the node in the pending tree with the next
8580 initializer to output. */
8584 /* Add one non-braced element to the current constructor level.
8585 This adjusts the current position within the constructor's type.
8586 This may also start or terminate implicit levels
8587 to handle a partly-braced initializer.
8589 Once this has found the correct level for the new element,
8590 it calls output_init_element.
8592 IMPLICIT is true if value comes from pop_init_level (1),
8593 the new initializer has been merged with the existing one
8594 and thus no warnings should be emitted about overriding an
8595 existing initializer. */
8598 process_init_element (location_t loc
, struct c_expr value
, bool implicit
,
8599 struct obstack
* braced_init_obstack
)
8601 tree orig_value
= value
.value
;
8602 int string_flag
= orig_value
!= 0 && TREE_CODE (orig_value
) == STRING_CST
;
8603 bool strict_string
= value
.original_code
== STRING_CST
;
8604 bool was_designated
= designator_depth
!= 0;
8606 designator_depth
= 0;
8607 designator_erroneous
= 0;
8609 if (!implicit
&& value
.value
&& !integer_zerop (value
.value
))
8610 constructor_zeroinit
= 0;
8612 /* Handle superfluous braces around string cst as in
8613 char x[] = {"foo"}; */
8617 && TREE_CODE (constructor_type
) == ARRAY_TYPE
8618 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
8619 && integer_zerop (constructor_unfilled_index
))
8621 if (constructor_stack
->replacement_value
.value
)
8622 error_init (loc
, "excess elements in char array initializer");
8623 constructor_stack
->replacement_value
= value
;
8627 if (constructor_stack
->replacement_value
.value
!= 0)
8629 error_init (loc
, "excess elements in struct initializer");
8633 /* Ignore elements of a brace group if it is entirely superfluous
8634 and has already been diagnosed. */
8635 if (constructor_type
== 0)
8638 if (!implicit
&& warn_designated_init
&& !was_designated
8639 && TREE_CODE (constructor_type
) == RECORD_TYPE
8640 && lookup_attribute ("designated_init",
8641 TYPE_ATTRIBUTES (constructor_type
)))
8643 OPT_Wdesignated_init
,
8644 "positional initialization of field "
8645 "in %<struct%> declared with %<designated_init%> attribute");
8647 /* If we've exhausted any levels that didn't have braces,
8649 while (constructor_stack
->implicit
)
8651 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
8652 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8653 && constructor_fields
== 0)
8654 process_init_element (loc
,
8655 pop_init_level (loc
, 1, braced_init_obstack
),
8656 true, braced_init_obstack
);
8657 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
8658 || TREE_CODE (constructor_type
) == VECTOR_TYPE
)
8659 && constructor_max_index
8660 && tree_int_cst_lt (constructor_max_index
,
8662 process_init_element (loc
,
8663 pop_init_level (loc
, 1, braced_init_obstack
),
8664 true, braced_init_obstack
);
8669 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8670 if (constructor_range_stack
)
8672 /* If value is a compound literal and we'll be just using its
8673 content, don't put it into a SAVE_EXPR. */
8674 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
8675 || !require_constant_value
8678 tree semantic_type
= NULL_TREE
;
8679 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
8681 semantic_type
= TREE_TYPE (value
.value
);
8682 value
.value
= TREE_OPERAND (value
.value
, 0);
8684 value
.value
= c_save_expr (value
.value
);
8686 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
8693 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8696 enum tree_code fieldcode
;
8698 if (constructor_fields
== 0)
8700 pedwarn_init (loc
, 0, "excess elements in struct initializer");
8704 fieldtype
= TREE_TYPE (constructor_fields
);
8705 if (fieldtype
!= error_mark_node
)
8706 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
8707 fieldcode
= TREE_CODE (fieldtype
);
8709 /* Error for non-static initialization of a flexible array member. */
8710 if (fieldcode
== ARRAY_TYPE
8711 && !require_constant_value
8712 && TYPE_SIZE (fieldtype
) == NULL_TREE
8713 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
8715 error_init (loc
, "non-static initialization of a flexible "
8720 /* Accept a string constant to initialize a subarray. */
8721 if (value
.value
!= 0
8722 && fieldcode
== ARRAY_TYPE
8723 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
8725 value
.value
= orig_value
;
8726 /* Otherwise, if we have come to a subaggregate,
8727 and we don't have an element of its type, push into it. */
8728 else if (value
.value
!= 0
8729 && value
.value
!= error_mark_node
8730 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
8731 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
8732 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
8734 push_init_level (loc
, 1, braced_init_obstack
);
8740 push_member_name (constructor_fields
);
8741 output_init_element (loc
, value
.value
, value
.original_type
,
8742 strict_string
, fieldtype
,
8743 constructor_fields
, 1, implicit
,
8744 braced_init_obstack
);
8745 RESTORE_SPELLING_DEPTH (constructor_depth
);
8748 /* Do the bookkeeping for an element that was
8749 directly output as a constructor. */
8751 /* For a record, keep track of end position of last field. */
8752 if (DECL_SIZE (constructor_fields
))
8753 constructor_bit_index
8754 = size_binop_loc (input_location
, PLUS_EXPR
,
8755 bit_position (constructor_fields
),
8756 DECL_SIZE (constructor_fields
));
8758 /* If the current field was the first one not yet written out,
8759 it isn't now, so update. */
8760 if (constructor_unfilled_fields
== constructor_fields
)
8762 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
8763 /* Skip any nameless bit fields. */
8764 while (constructor_unfilled_fields
!= 0
8765 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8766 && DECL_NAME (constructor_unfilled_fields
) == 0)
8767 constructor_unfilled_fields
=
8768 DECL_CHAIN (constructor_unfilled_fields
);
8772 constructor_fields
= DECL_CHAIN (constructor_fields
);
8773 /* Skip any nameless bit fields at the beginning. */
8774 while (constructor_fields
!= 0
8775 && DECL_C_BIT_FIELD (constructor_fields
)
8776 && DECL_NAME (constructor_fields
) == 0)
8777 constructor_fields
= DECL_CHAIN (constructor_fields
);
8779 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8782 enum tree_code fieldcode
;
8784 if (constructor_fields
== 0)
8786 pedwarn_init (loc
, 0,
8787 "excess elements in union initializer");
8791 fieldtype
= TREE_TYPE (constructor_fields
);
8792 if (fieldtype
!= error_mark_node
)
8793 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
8794 fieldcode
= TREE_CODE (fieldtype
);
8796 /* Warn that traditional C rejects initialization of unions.
8797 We skip the warning if the value is zero. This is done
8798 under the assumption that the zero initializer in user
8799 code appears conditioned on e.g. __STDC__ to avoid
8800 "missing initializer" warnings and relies on default
8801 initialization to zero in the traditional C case.
8802 We also skip the warning if the initializer is designated,
8803 again on the assumption that this must be conditional on
8804 __STDC__ anyway (and we've already complained about the
8805 member-designator already). */
8806 if (!in_system_header_at (input_location
) && !constructor_designated
8807 && !(value
.value
&& (integer_zerop (value
.value
)
8808 || real_zerop (value
.value
))))
8809 warning (OPT_Wtraditional
, "traditional C rejects initialization "
8812 /* Accept a string constant to initialize a subarray. */
8813 if (value
.value
!= 0
8814 && fieldcode
== ARRAY_TYPE
8815 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
8817 value
.value
= orig_value
;
8818 /* Otherwise, if we have come to a subaggregate,
8819 and we don't have an element of its type, push into it. */
8820 else if (value
.value
!= 0
8821 && value
.value
!= error_mark_node
8822 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
8823 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
8824 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
8826 push_init_level (loc
, 1, braced_init_obstack
);
8832 push_member_name (constructor_fields
);
8833 output_init_element (loc
, value
.value
, value
.original_type
,
8834 strict_string
, fieldtype
,
8835 constructor_fields
, 1, implicit
,
8836 braced_init_obstack
);
8837 RESTORE_SPELLING_DEPTH (constructor_depth
);
8840 /* Do the bookkeeping for an element that was
8841 directly output as a constructor. */
8843 constructor_bit_index
= DECL_SIZE (constructor_fields
);
8844 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
8847 constructor_fields
= 0;
8849 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8851 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8852 enum tree_code eltcode
= TREE_CODE (elttype
);
8854 /* Accept a string constant to initialize a subarray. */
8855 if (value
.value
!= 0
8856 && eltcode
== ARRAY_TYPE
8857 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
8859 value
.value
= orig_value
;
8860 /* Otherwise, if we have come to a subaggregate,
8861 and we don't have an element of its type, push into it. */
8862 else if (value
.value
!= 0
8863 && value
.value
!= error_mark_node
8864 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
8865 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
8866 || eltcode
== UNION_TYPE
|| eltcode
== VECTOR_TYPE
))
8868 push_init_level (loc
, 1, braced_init_obstack
);
8872 if (constructor_max_index
!= 0
8873 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
8874 || integer_all_onesp (constructor_max_index
)))
8876 pedwarn_init (loc
, 0,
8877 "excess elements in array initializer");
8881 /* Now output the actual element. */
8884 push_array_bounds (tree_to_uhwi (constructor_index
));
8885 output_init_element (loc
, value
.value
, value
.original_type
,
8886 strict_string
, elttype
,
8887 constructor_index
, 1, implicit
,
8888 braced_init_obstack
);
8889 RESTORE_SPELLING_DEPTH (constructor_depth
);
8893 = size_binop_loc (input_location
, PLUS_EXPR
,
8894 constructor_index
, bitsize_one_node
);
8897 /* If we are doing the bookkeeping for an element that was
8898 directly output as a constructor, we must update
8899 constructor_unfilled_index. */
8900 constructor_unfilled_index
= constructor_index
;
8902 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
8904 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8906 /* Do a basic check of initializer size. Note that vectors
8907 always have a fixed size derived from their type. */
8908 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
8910 pedwarn_init (loc
, 0,
8911 "excess elements in vector initializer");
8915 /* Now output the actual element. */
8918 if (TREE_CODE (value
.value
) == VECTOR_CST
)
8919 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
8920 output_init_element (loc
, value
.value
, value
.original_type
,
8921 strict_string
, elttype
,
8922 constructor_index
, 1, implicit
,
8923 braced_init_obstack
);
8927 = size_binop_loc (input_location
,
8928 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
8931 /* If we are doing the bookkeeping for an element that was
8932 directly output as a constructor, we must update
8933 constructor_unfilled_index. */
8934 constructor_unfilled_index
= constructor_index
;
8937 /* Handle the sole element allowed in a braced initializer
8938 for a scalar variable. */
8939 else if (constructor_type
!= error_mark_node
8940 && constructor_fields
== 0)
8942 pedwarn_init (loc
, 0,
8943 "excess elements in scalar initializer");
8949 output_init_element (loc
, value
.value
, value
.original_type
,
8950 strict_string
, constructor_type
,
8951 NULL_TREE
, 1, implicit
,
8952 braced_init_obstack
);
8953 constructor_fields
= 0;
8956 /* Handle range initializers either at this level or anywhere higher
8957 in the designator stack. */
8958 if (constructor_range_stack
)
8960 struct constructor_range_stack
*p
, *range_stack
;
8963 range_stack
= constructor_range_stack
;
8964 constructor_range_stack
= 0;
8965 while (constructor_stack
!= range_stack
->stack
)
8967 gcc_assert (constructor_stack
->implicit
);
8968 process_init_element (loc
,
8969 pop_init_level (loc
, 1,
8970 braced_init_obstack
),
8971 true, braced_init_obstack
);
8973 for (p
= range_stack
;
8974 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
8977 gcc_assert (constructor_stack
->implicit
);
8978 process_init_element (loc
,
8979 pop_init_level (loc
, 1,
8980 braced_init_obstack
),
8981 true, braced_init_obstack
);
8984 p
->index
= size_binop_loc (input_location
,
8985 PLUS_EXPR
, p
->index
, bitsize_one_node
);
8986 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
8991 constructor_index
= p
->index
;
8992 constructor_fields
= p
->fields
;
8993 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
9001 push_init_level (loc
, 2, braced_init_obstack
);
9002 p
->stack
= constructor_stack
;
9003 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
9004 p
->index
= p
->range_start
;
9008 constructor_range_stack
= range_stack
;
9015 constructor_range_stack
= 0;
9018 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9019 (guaranteed to be 'volatile' or null) and ARGS (represented using
9020 an ASM_EXPR node). */
9022 build_asm_stmt (tree cv_qualifier
, tree args
)
9024 if (!ASM_VOLATILE_P (args
) && cv_qualifier
)
9025 ASM_VOLATILE_P (args
) = 1;
9026 return add_stmt (args
);
9029 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9030 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9031 SIMPLE indicates whether there was anything at all after the
9032 string in the asm expression -- asm("blah") and asm("blah" : )
9033 are subtly different. We use a ASM_EXPR node to represent this. */
9035 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
9036 tree clobbers
, tree labels
, bool simple
)
9041 const char *constraint
;
9042 const char **oconstraints
;
9043 bool allows_mem
, allows_reg
, is_inout
;
9044 int ninputs
, noutputs
;
9046 ninputs
= list_length (inputs
);
9047 noutputs
= list_length (outputs
);
9048 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
9050 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
9052 /* Remove output conversions that change the type but not the mode. */
9053 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
9055 tree output
= TREE_VALUE (tail
);
9057 output
= c_fully_fold (output
, false, NULL
);
9059 /* ??? Really, this should not be here. Users should be using a
9060 proper lvalue, dammit. But there's a long history of using casts
9061 in the output operands. In cases like longlong.h, this becomes a
9062 primitive form of typechecking -- if the cast can be removed, then
9063 the output operand had a type of the proper width; otherwise we'll
9064 get an error. Gross, but ... */
9065 STRIP_NOPS (output
);
9067 if (!lvalue_or_else (loc
, output
, lv_asm
))
9068 output
= error_mark_node
;
9070 if (output
!= error_mark_node
9071 && (TREE_READONLY (output
)
9072 || TYPE_READONLY (TREE_TYPE (output
))
9073 || ((TREE_CODE (TREE_TYPE (output
)) == RECORD_TYPE
9074 || TREE_CODE (TREE_TYPE (output
)) == UNION_TYPE
)
9075 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
9076 readonly_error (loc
, output
, lv_asm
);
9078 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9079 oconstraints
[i
] = constraint
;
9081 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
9082 &allows_mem
, &allows_reg
, &is_inout
))
9084 /* If the operand is going to end up in memory,
9085 mark it addressable. */
9086 if (!allows_reg
&& !c_mark_addressable (output
))
9087 output
= error_mark_node
;
9088 if (!(!allows_reg
&& allows_mem
)
9089 && output
!= error_mark_node
9090 && VOID_TYPE_P (TREE_TYPE (output
)))
9092 error_at (loc
, "invalid use of void expression");
9093 output
= error_mark_node
;
9097 output
= error_mark_node
;
9099 TREE_VALUE (tail
) = output
;
9102 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
9106 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9107 input
= TREE_VALUE (tail
);
9109 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
9110 oconstraints
, &allows_mem
, &allows_reg
))
9112 /* If the operand is going to end up in memory,
9113 mark it addressable. */
9114 if (!allows_reg
&& allows_mem
)
9116 input
= c_fully_fold (input
, false, NULL
);
9118 /* Strip the nops as we allow this case. FIXME, this really
9119 should be rejected or made deprecated. */
9121 if (!c_mark_addressable (input
))
9122 input
= error_mark_node
;
9127 memset (&expr
, 0, sizeof (expr
));
9129 expr
= convert_lvalue_to_rvalue (loc
, expr
, true, false);
9130 input
= c_fully_fold (expr
.value
, false, NULL
);
9132 if (input
!= error_mark_node
&& VOID_TYPE_P (TREE_TYPE (input
)))
9134 error_at (loc
, "invalid use of void expression");
9135 input
= error_mark_node
;
9140 input
= error_mark_node
;
9142 TREE_VALUE (tail
) = input
;
9145 /* ASMs with labels cannot have outputs. This should have been
9146 enforced by the parser. */
9147 gcc_assert (outputs
== NULL
|| labels
== NULL
);
9149 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
9151 /* asm statements without outputs, including simple ones, are treated
9153 ASM_INPUT_P (args
) = simple
;
9154 ASM_VOLATILE_P (args
) = (noutputs
== 0);
9159 /* Generate a goto statement to LABEL. LOC is the location of the
9163 c_finish_goto_label (location_t loc
, tree label
)
9165 tree decl
= lookup_label_for_goto (loc
, label
);
9168 TREE_USED (decl
) = 1;
9170 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
9171 SET_EXPR_LOCATION (t
, loc
);
9172 return add_stmt (t
);
9176 /* Generate a computed goto statement to EXPR. LOC is the location of
9180 c_finish_goto_ptr (location_t loc
, tree expr
)
9183 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids %<goto *expr;%>");
9184 expr
= c_fully_fold (expr
, false, NULL
);
9185 expr
= convert (ptr_type_node
, expr
);
9186 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
9187 SET_EXPR_LOCATION (t
, loc
);
9188 return add_stmt (t
);
9191 /* Generate a C `return' statement. RETVAL is the expression for what
9192 to return, or a null pointer for `return;' with no value. LOC is
9193 the location of the return statement, or the location of the expression,
9194 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9195 is the original type of RETVAL. */
9198 c_finish_return (location_t loc
, tree retval
, tree origtype
)
9200 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
9201 bool no_warning
= false;
9205 if (TREE_THIS_VOLATILE (current_function_decl
))
9207 "function declared %<noreturn%> has a %<return%> statement");
9209 if (flag_cilkplus
&& contains_array_notation_expr (retval
))
9211 /* Array notations are allowed in a return statement if it is inside a
9212 built-in array notation reduction function. */
9213 if (!find_rank (loc
, retval
, retval
, false, &rank
))
9214 return error_mark_node
;
9217 error_at (loc
, "array notation expression cannot be used as a "
9219 return error_mark_node
;
9222 if (flag_cilkplus
&& retval
&& contains_cilk_spawn_stmt (retval
))
9224 error_at (loc
, "use of %<_Cilk_spawn%> in a return statement is not "
9226 return error_mark_node
;
9230 tree semantic_type
= NULL_TREE
;
9231 npc
= null_pointer_constant_p (retval
);
9232 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
9234 semantic_type
= TREE_TYPE (retval
);
9235 retval
= TREE_OPERAND (retval
, 0);
9237 retval
= c_fully_fold (retval
, false, NULL
);
9239 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
9244 current_function_returns_null
= 1;
9245 if ((warn_return_type
|| flag_isoc99
)
9246 && valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
9249 pedwarn (loc
, 0, "%<return%> with no value, in "
9250 "function returning non-void");
9252 warning_at (loc
, OPT_Wreturn_type
, "%<return%> with no value, "
9253 "in function returning non-void");
9257 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
9259 current_function_returns_null
= 1;
9260 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
9262 "%<return%> with a value, in function returning void");
9264 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
9265 "%<return%> with expression, in function returning void");
9269 tree t
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, valtype
,
9270 retval
, origtype
, ic_return
,
9271 npc
, NULL_TREE
, NULL_TREE
, 0);
9272 tree res
= DECL_RESULT (current_function_decl
);
9276 current_function_returns_value
= 1;
9277 if (t
== error_mark_node
)
9280 save
= in_late_binary_op
;
9281 if (TREE_CODE (TREE_TYPE (res
)) == BOOLEAN_TYPE
9282 || TREE_CODE (TREE_TYPE (res
)) == COMPLEX_TYPE
)
9283 in_late_binary_op
= true;
9284 inner
= t
= convert (TREE_TYPE (res
), t
);
9285 in_late_binary_op
= save
;
9287 /* Strip any conversions, additions, and subtractions, and see if
9288 we are returning the address of a local variable. Warn if so. */
9291 switch (TREE_CODE (inner
))
9294 case NON_LVALUE_EXPR
:
9296 case POINTER_PLUS_EXPR
:
9297 inner
= TREE_OPERAND (inner
, 0);
9301 /* If the second operand of the MINUS_EXPR has a pointer
9302 type (or is converted from it), this may be valid, so
9303 don't give a warning. */
9305 tree op1
= TREE_OPERAND (inner
, 1);
9307 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
9308 && (CONVERT_EXPR_P (op1
)
9309 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
9310 op1
= TREE_OPERAND (op1
, 0);
9312 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
9315 inner
= TREE_OPERAND (inner
, 0);
9320 inner
= TREE_OPERAND (inner
, 0);
9322 while (REFERENCE_CLASS_P (inner
)
9323 && TREE_CODE (inner
) != INDIRECT_REF
)
9324 inner
= TREE_OPERAND (inner
, 0);
9327 && !DECL_EXTERNAL (inner
)
9328 && !TREE_STATIC (inner
)
9329 && DECL_CONTEXT (inner
) == current_function_decl
)
9331 if (TREE_CODE (inner
) == LABEL_DECL
)
9332 warning_at (loc
, OPT_Wreturn_local_addr
,
9333 "function returns address of label");
9336 warning_at (loc
, OPT_Wreturn_local_addr
,
9337 "function returns address of local variable");
9338 tree zero
= build_zero_cst (TREE_TYPE (res
));
9339 t
= build2 (COMPOUND_EXPR
, TREE_TYPE (res
), t
, zero
);
9351 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
9352 SET_EXPR_LOCATION (retval
, loc
);
9354 if (warn_sequence_point
)
9355 verify_sequence_points (retval
);
9358 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
9359 TREE_NO_WARNING (ret_stmt
) |= no_warning
;
9360 return add_stmt (ret_stmt
);
9364 /* The SWITCH_EXPR being built. */
9367 /* The original type of the testing expression, i.e. before the
9368 default conversion is applied. */
9371 /* A splay-tree mapping the low element of a case range to the high
9372 element, or NULL_TREE if there is no high element. Used to
9373 determine whether or not a new case label duplicates an old case
9374 label. We need a tree, rather than simply a hash table, because
9375 of the GNU case range extension. */
9378 /* The bindings at the point of the switch. This is used for
9379 warnings crossing decls when branching to a case label. */
9380 struct c_spot_bindings
*bindings
;
9382 /* The next node on the stack. */
9383 struct c_switch
*next
;
9386 /* A stack of the currently active switch statements. The innermost
9387 switch statement is on the top of the stack. There is no need to
9388 mark the stack for garbage collection because it is only active
9389 during the processing of the body of a function, and we never
9390 collect at that point. */
9392 struct c_switch
*c_switch_stack
;
9394 /* Start a C switch statement, testing expression EXP. Return the new
9395 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9396 SWITCH_COND_LOC is the location of the switch's condition.
9397 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9400 c_start_case (location_t switch_loc
,
9401 location_t switch_cond_loc
,
9402 tree exp
, bool explicit_cast_p
)
9404 tree orig_type
= error_mark_node
;
9405 struct c_switch
*cs
;
9407 if (exp
!= error_mark_node
)
9409 orig_type
= TREE_TYPE (exp
);
9411 if (!INTEGRAL_TYPE_P (orig_type
))
9413 if (orig_type
!= error_mark_node
)
9415 error_at (switch_cond_loc
, "switch quantity not an integer");
9416 orig_type
= error_mark_node
;
9418 exp
= integer_zero_node
;
9422 tree type
= TYPE_MAIN_VARIANT (orig_type
);
9425 /* Warn if the condition has boolean value. */
9426 while (TREE_CODE (e
) == COMPOUND_EXPR
)
9427 e
= TREE_OPERAND (e
, 1);
9429 if ((TREE_CODE (type
) == BOOLEAN_TYPE
9430 || truth_value_p (TREE_CODE (e
)))
9431 /* Explicit cast to int suppresses this warning. */
9432 && !(TREE_CODE (type
) == INTEGER_TYPE
9433 && explicit_cast_p
))
9434 warning_at (switch_cond_loc
, OPT_Wswitch_bool
,
9435 "switch condition has boolean value");
9437 if (!in_system_header_at (input_location
)
9438 && (type
== long_integer_type_node
9439 || type
== long_unsigned_type_node
))
9440 warning_at (switch_cond_loc
,
9441 OPT_Wtraditional
, "%<long%> switch expression not "
9442 "converted to %<int%> in ISO C");
9444 exp
= c_fully_fold (exp
, false, NULL
);
9445 exp
= default_conversion (exp
);
9447 if (warn_sequence_point
)
9448 verify_sequence_points (exp
);
9452 /* Add this new SWITCH_EXPR to the stack. */
9453 cs
= XNEW (struct c_switch
);
9454 cs
->switch_expr
= build3 (SWITCH_EXPR
, orig_type
, exp
, NULL_TREE
, NULL_TREE
);
9455 SET_EXPR_LOCATION (cs
->switch_expr
, switch_loc
);
9456 cs
->orig_type
= orig_type
;
9457 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
9458 cs
->bindings
= c_get_switch_bindings ();
9459 cs
->next
= c_switch_stack
;
9460 c_switch_stack
= cs
;
9462 return add_stmt (cs
->switch_expr
);
9465 /* Process a case label at location LOC. */
9468 do_case (location_t loc
, tree low_value
, tree high_value
)
9470 tree label
= NULL_TREE
;
9472 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
9474 low_value
= c_fully_fold (low_value
, false, NULL
);
9475 if (TREE_CODE (low_value
) == INTEGER_CST
)
9476 pedwarn (loc
, OPT_Wpedantic
,
9477 "case label is not an integer constant expression");
9480 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
9482 high_value
= c_fully_fold (high_value
, false, NULL
);
9483 if (TREE_CODE (high_value
) == INTEGER_CST
)
9484 pedwarn (input_location
, OPT_Wpedantic
,
9485 "case label is not an integer constant expression");
9488 if (c_switch_stack
== NULL
)
9491 error_at (loc
, "case label not within a switch statement");
9493 error_at (loc
, "%<default%> label not within a switch statement");
9497 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
9498 EXPR_LOCATION (c_switch_stack
->switch_expr
),
9502 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
9503 SWITCH_COND (c_switch_stack
->switch_expr
),
9504 c_switch_stack
->orig_type
,
9505 low_value
, high_value
);
9506 if (label
== error_mark_node
)
9511 /* Finish the switch statement. TYPE is the original type of the
9512 controlling expression of the switch, or NULL_TREE. */
9515 c_finish_case (tree body
, tree type
)
9517 struct c_switch
*cs
= c_switch_stack
;
9518 location_t switch_location
;
9520 SWITCH_BODY (cs
->switch_expr
) = body
;
9522 /* Emit warnings as needed. */
9523 switch_location
= EXPR_LOCATION (cs
->switch_expr
);
9524 c_do_switch_warnings (cs
->cases
, switch_location
,
9525 type
? type
: TREE_TYPE (cs
->switch_expr
),
9526 SWITCH_COND (cs
->switch_expr
));
9528 /* Pop the stack. */
9529 c_switch_stack
= cs
->next
;
9530 splay_tree_delete (cs
->cases
);
9531 c_release_switch_bindings (cs
->bindings
);
9535 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9536 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9537 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9538 statement, and was not surrounded with parenthesis. */
9541 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
9542 tree else_block
, bool nested_if
)
9546 /* If the condition has array notations, then the rank of the then_block and
9547 else_block must be either 0 or be equal to the rank of the condition. If
9548 the condition does not have array notations then break them up as it is
9549 broken up in a normal expression. */
9550 if (flag_cilkplus
&& contains_array_notation_expr (cond
))
9552 size_t then_rank
= 0, cond_rank
= 0, else_rank
= 0;
9553 if (!find_rank (if_locus
, cond
, cond
, true, &cond_rank
))
9556 && !find_rank (if_locus
, then_block
, then_block
, true, &then_rank
))
9559 && !find_rank (if_locus
, else_block
, else_block
, true, &else_rank
))
9561 if (cond_rank
!= then_rank
&& then_rank
!= 0)
9563 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
9564 " and the then-block");
9567 else if (cond_rank
!= else_rank
&& else_rank
!= 0)
9569 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
9570 " and the else-block");
9574 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9575 if (warn_parentheses
&& nested_if
&& else_block
== NULL
)
9577 tree inner_if
= then_block
;
9579 /* We know from the grammar productions that there is an IF nested
9580 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9581 it might not be exactly THEN_BLOCK, but should be the last
9582 non-container statement within. */
9584 switch (TREE_CODE (inner_if
))
9589 inner_if
= BIND_EXPR_BODY (inner_if
);
9591 case STATEMENT_LIST
:
9592 inner_if
= expr_last (then_block
);
9594 case TRY_FINALLY_EXPR
:
9595 case TRY_CATCH_EXPR
:
9596 inner_if
= TREE_OPERAND (inner_if
, 0);
9603 if (COND_EXPR_ELSE (inner_if
))
9604 warning_at (if_locus
, OPT_Wparentheses
,
9605 "suggest explicit braces to avoid ambiguous %<else%>");
9608 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
9609 SET_EXPR_LOCATION (stmt
, if_locus
);
9613 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9614 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9615 is false for DO loops. INCR is the FOR increment expression. BODY is
9616 the statement controlled by the loop. BLAB is the break label. CLAB is
9617 the continue label. Everything is allowed to be NULL. */
9620 c_finish_loop (location_t start_locus
, tree cond
, tree incr
, tree body
,
9621 tree blab
, tree clab
, bool cond_is_first
)
9623 tree entry
= NULL
, exit
= NULL
, t
;
9625 if (flag_cilkplus
&& contains_array_notation_expr (cond
))
9627 error_at (start_locus
, "array notation expression cannot be used in a "
9628 "loop%'s condition");
9632 /* If the condition is zero don't generate a loop construct. */
9633 if (cond
&& integer_zerop (cond
))
9637 t
= build_and_jump (&blab
);
9638 SET_EXPR_LOCATION (t
, start_locus
);
9644 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
9646 /* If we have an exit condition, then we build an IF with gotos either
9647 out of the loop, or to the top of it. If there's no exit condition,
9648 then we just build a jump back to the top. */
9649 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
9651 if (cond
&& !integer_nonzerop (cond
))
9653 /* Canonicalize the loop condition to the end. This means
9654 generating a branch to the loop condition. Reuse the
9655 continue label, if possible. */
9660 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
9661 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
9664 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
9665 SET_EXPR_LOCATION (t
, start_locus
);
9669 t
= build_and_jump (&blab
);
9671 exit
= fold_build3_loc (start_locus
,
9672 COND_EXPR
, void_type_node
, cond
, exit
, t
);
9674 exit
= fold_build3_loc (input_location
,
9675 COND_EXPR
, void_type_node
, cond
, exit
, t
);
9684 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
9692 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
9696 c_finish_bc_stmt (location_t loc
, tree
*label_p
, bool is_break
)
9699 tree label
= *label_p
;
9701 /* In switch statements break is sometimes stylistically used after
9702 a return statement. This can lead to spurious warnings about
9703 control reaching the end of a non-void function when it is
9704 inlined. Note that we are calling block_may_fallthru with
9705 language specific tree nodes; this works because
9706 block_may_fallthru returns true when given something it does not
9708 skip
= !block_may_fallthru (cur_stmt_list
);
9713 *label_p
= label
= create_artificial_label (loc
);
9715 else if (TREE_CODE (label
) == LABEL_DECL
)
9717 else switch (TREE_INT_CST_LOW (label
))
9721 error_at (loc
, "break statement not within loop or switch");
9723 error_at (loc
, "continue statement not within a loop");
9727 gcc_assert (is_break
);
9728 error_at (loc
, "break statement used with OpenMP for loop");
9733 error ("break statement within %<#pragma simd%> loop body");
9735 error ("continue statement within %<#pragma simd%> loop body");
9746 add_stmt (build_predict_expr (PRED_CONTINUE
, NOT_TAKEN
));
9748 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
9751 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9754 emit_side_effect_warnings (location_t loc
, tree expr
)
9756 if (expr
== error_mark_node
)
9758 else if (!TREE_SIDE_EFFECTS (expr
))
9760 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
9761 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
9763 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
9766 location_t cloc
= loc
;
9767 while (TREE_CODE (r
) == COMPOUND_EXPR
)
9769 if (EXPR_HAS_LOCATION (r
))
9770 cloc
= EXPR_LOCATION (r
);
9771 r
= TREE_OPERAND (r
, 1);
9773 if (!TREE_SIDE_EFFECTS (r
)
9774 && !VOID_TYPE_P (TREE_TYPE (r
))
9775 && !CONVERT_EXPR_P (r
)
9776 && !TREE_NO_WARNING (r
)
9777 && !TREE_NO_WARNING (expr
))
9778 warning_at (cloc
, OPT_Wunused_value
,
9779 "right-hand operand of comma expression has no effect");
9782 warn_if_unused_value (expr
, loc
);
9785 /* Process an expression as if it were a complete statement. Emit
9786 diagnostics, but do not call ADD_STMT. LOC is the location of the
9790 c_process_expr_stmt (location_t loc
, tree expr
)
9797 expr
= c_fully_fold (expr
, false, NULL
);
9799 if (warn_sequence_point
)
9800 verify_sequence_points (expr
);
9802 if (TREE_TYPE (expr
) != error_mark_node
9803 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
9804 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
9805 error_at (loc
, "expression statement has incomplete type");
9807 /* If we're not processing a statement expression, warn about unused values.
9808 Warnings for statement expressions will be emitted later, once we figure
9809 out which is the result. */
9810 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
9811 && warn_unused_value
)
9812 emit_side_effect_warnings (loc
, expr
);
9815 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
9816 exprv
= TREE_OPERAND (exprv
, 1);
9817 while (CONVERT_EXPR_P (exprv
))
9818 exprv
= TREE_OPERAND (exprv
, 0);
9820 || handled_component_p (exprv
)
9821 || TREE_CODE (exprv
) == ADDR_EXPR
)
9822 mark_exp_read (exprv
);
9824 /* If the expression is not of a type to which we cannot assign a line
9825 number, wrap the thing in a no-op NOP_EXPR. */
9826 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
9828 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
9829 SET_EXPR_LOCATION (expr
, loc
);
9835 /* Emit an expression as a statement. LOC is the location of the
9839 c_finish_expr_stmt (location_t loc
, tree expr
)
9842 return add_stmt (c_process_expr_stmt (loc
, expr
));
9847 /* Do the opposite and emit a statement as an expression. To begin,
9848 create a new binding level and return it. */
9851 c_begin_stmt_expr (void)
9855 /* We must force a BLOCK for this level so that, if it is not expanded
9856 later, there is a way to turn off the entire subtree of blocks that
9857 are contained in it. */
9859 ret
= c_begin_compound_stmt (true);
9861 c_bindings_start_stmt_expr (c_switch_stack
== NULL
9863 : c_switch_stack
->bindings
);
9865 /* Mark the current statement list as belonging to a statement list. */
9866 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
9871 /* LOC is the location of the compound statement to which this body
9875 c_finish_stmt_expr (location_t loc
, tree body
)
9877 tree last
, type
, tmp
, val
;
9880 body
= c_end_compound_stmt (loc
, body
, true);
9882 c_bindings_end_stmt_expr (c_switch_stack
== NULL
9884 : c_switch_stack
->bindings
);
9886 /* Locate the last statement in BODY. See c_end_compound_stmt
9887 about always returning a BIND_EXPR. */
9888 last_p
= &BIND_EXPR_BODY (body
);
9889 last
= BIND_EXPR_BODY (body
);
9892 if (TREE_CODE (last
) == STATEMENT_LIST
)
9894 tree_stmt_iterator i
;
9896 /* This can happen with degenerate cases like ({ }). No value. */
9897 if (!TREE_SIDE_EFFECTS (last
))
9900 /* If we're supposed to generate side effects warnings, process
9901 all of the statements except the last. */
9902 if (warn_unused_value
)
9904 for (i
= tsi_start (last
); !tsi_one_before_end_p (i
); tsi_next (&i
))
9907 tree t
= tsi_stmt (i
);
9909 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
9910 emit_side_effect_warnings (tloc
, t
);
9914 i
= tsi_last (last
);
9915 last_p
= tsi_stmt_ptr (i
);
9919 /* If the end of the list is exception related, then the list was split
9920 by a call to push_cleanup. Continue searching. */
9921 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
9922 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
9924 last_p
= &TREE_OPERAND (last
, 0);
9926 goto continue_searching
;
9929 if (last
== error_mark_node
)
9932 /* In the case that the BIND_EXPR is not necessary, return the
9933 expression out from inside it. */
9934 if (last
== BIND_EXPR_BODY (body
)
9935 && BIND_EXPR_VARS (body
) == NULL
)
9937 /* Even if this looks constant, do not allow it in a constant
9939 last
= c_wrap_maybe_const (last
, true);
9940 /* Do not warn if the return value of a statement expression is
9942 TREE_NO_WARNING (last
) = 1;
9946 /* Extract the type of said expression. */
9947 type
= TREE_TYPE (last
);
9949 /* If we're not returning a value at all, then the BIND_EXPR that
9950 we already have is a fine expression to return. */
9951 if (!type
|| VOID_TYPE_P (type
))
9954 /* Now that we've located the expression containing the value, it seems
9955 silly to make voidify_wrapper_expr repeat the process. Create a
9956 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9957 tmp
= create_tmp_var_raw (type
, NULL
);
9959 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9960 tree_expr_nonnegative_p giving up immediately. */
9962 if (TREE_CODE (val
) == NOP_EXPR
9963 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
9964 val
= TREE_OPERAND (val
, 0);
9966 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
9967 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
9970 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
9971 SET_EXPR_LOCATION (t
, loc
);
9976 /* Begin and end compound statements. This is as simple as pushing
9977 and popping new statement lists from the tree. */
9980 c_begin_compound_stmt (bool do_scope
)
9982 tree stmt
= push_stmt_list ();
9988 /* End a compound statement. STMT is the statement. LOC is the
9989 location of the compound statement-- this is usually the location
9990 of the opening brace. */
9993 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
9999 if (c_dialect_objc ())
10000 objc_clear_super_receiver ();
10001 block
= pop_scope ();
10004 stmt
= pop_stmt_list (stmt
);
10005 stmt
= c_build_bind_expr (loc
, block
, stmt
);
10007 /* If this compound statement is nested immediately inside a statement
10008 expression, then force a BIND_EXPR to be created. Otherwise we'll
10009 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10010 STATEMENT_LISTs merge, and thus we can lose track of what statement
10011 was really last. */
10012 if (building_stmt_list_p ()
10013 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
10014 && TREE_CODE (stmt
) != BIND_EXPR
)
10016 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
10017 TREE_SIDE_EFFECTS (stmt
) = 1;
10018 SET_EXPR_LOCATION (stmt
, loc
);
10024 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10025 when the current scope is exited. EH_ONLY is true when this is not
10026 meant to apply to normal control flow transfer. */
10029 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
10031 enum tree_code code
;
10035 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
10036 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
10038 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
10039 list
= push_stmt_list ();
10040 TREE_OPERAND (stmt
, 0) = list
;
10041 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
10044 /* Build a binary-operation expression without default conversions.
10045 CODE is the kind of expression to build.
10046 LOCATION is the operator's location.
10047 This function differs from `build' in several ways:
10048 the data type of the result is computed and recorded in it,
10049 warnings are generated if arg data types are invalid,
10050 special handling for addition and subtraction of pointers is known,
10051 and some optimization is done (operations on narrow ints
10052 are done in the narrower type when that gives the same result).
10053 Constant folding is also done before the result is returned.
10055 Note that the operands will never have enumeral types, or function
10056 or array types, because either they will have the default conversions
10057 performed or they have both just been converted to some other type in which
10058 the arithmetic is to be done. */
10061 build_binary_op (location_t location
, enum tree_code code
,
10062 tree orig_op0
, tree orig_op1
, int convert_p
)
10064 tree type0
, type1
, orig_type0
, orig_type1
;
10066 enum tree_code code0
, code1
;
10068 tree ret
= error_mark_node
;
10069 const char *invalid_op_diag
;
10070 bool op0_int_operands
, op1_int_operands
;
10071 bool int_const
, int_const_or_overflow
, int_operands
;
10073 /* Expression code to give to the expression when it is built.
10074 Normally this is CODE, which is what the caller asked for,
10075 but in some special cases we change it. */
10076 enum tree_code resultcode
= code
;
10078 /* Data type in which the computation is to be performed.
10079 In the simplest cases this is the common type of the arguments. */
10080 tree result_type
= NULL
;
10082 /* When the computation is in excess precision, the type of the
10083 final EXCESS_PRECISION_EXPR. */
10084 tree semantic_result_type
= NULL
;
10086 /* Nonzero means operands have already been type-converted
10087 in whatever way is necessary.
10088 Zero means they need to be converted to RESULT_TYPE. */
10091 /* Nonzero means create the expression with this type, rather than
10093 tree build_type
= 0;
10095 /* Nonzero means after finally constructing the expression
10096 convert it to this type. */
10097 tree final_type
= 0;
10099 /* Nonzero if this is an operation like MIN or MAX which can
10100 safely be computed in short if both args are promoted shorts.
10101 Also implies COMMON.
10102 -1 indicates a bitwise operation; this makes a difference
10103 in the exact conditions for when it is safe to do the operation
10104 in a narrower mode. */
10107 /* Nonzero if this is a comparison operation;
10108 if both args are promoted shorts, compare the original shorts.
10109 Also implies COMMON. */
10110 int short_compare
= 0;
10112 /* Nonzero if this is a right-shift operation, which can be computed on the
10113 original short and then promoted if the operand is a promoted short. */
10114 int short_shift
= 0;
10116 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10119 /* True means types are compatible as far as ObjC is concerned. */
10122 /* True means this is an arithmetic operation that may need excess
10124 bool may_need_excess_precision
;
10126 /* True means this is a boolean operation that converts both its
10127 operands to truth-values. */
10128 bool boolean_op
= false;
10130 /* Remember whether we're doing / or %. */
10131 bool doing_div_or_mod
= false;
10133 /* Remember whether we're doing << or >>. */
10134 bool doing_shift
= false;
10136 /* Tree holding instrumentation expression. */
10137 tree instrument_expr
= NULL
;
10139 if (location
== UNKNOWN_LOCATION
)
10140 location
= input_location
;
10145 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
10146 if (op0_int_operands
)
10147 op0
= remove_c_maybe_const_expr (op0
);
10148 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
10149 if (op1_int_operands
)
10150 op1
= remove_c_maybe_const_expr (op1
);
10151 int_operands
= (op0_int_operands
&& op1_int_operands
);
10154 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
10155 && TREE_CODE (orig_op1
) == INTEGER_CST
);
10156 int_const
= (int_const_or_overflow
10157 && !TREE_OVERFLOW (orig_op0
)
10158 && !TREE_OVERFLOW (orig_op1
));
10161 int_const
= int_const_or_overflow
= false;
10163 /* Do not apply default conversion in mixed vector/scalar expression. */
10165 && !((TREE_CODE (TREE_TYPE (op0
)) == VECTOR_TYPE
)
10166 != (TREE_CODE (TREE_TYPE (op1
)) == VECTOR_TYPE
)))
10168 op0
= default_conversion (op0
);
10169 op1
= default_conversion (op1
);
10172 /* When Cilk Plus is enabled and there are array notations inside op0, then
10173 we check to see if there are builtin array notation functions. If
10174 so, then we take on the type of the array notation inside it. */
10175 if (flag_cilkplus
&& contains_array_notation_expr (op0
))
10176 orig_type0
= type0
= find_correct_array_notation_type (op0
);
10178 orig_type0
= type0
= TREE_TYPE (op0
);
10180 if (flag_cilkplus
&& contains_array_notation_expr (op1
))
10181 orig_type1
= type1
= find_correct_array_notation_type (op1
);
10183 orig_type1
= type1
= TREE_TYPE (op1
);
10185 /* The expression codes of the data types of the arguments tell us
10186 whether the arguments are integers, floating, pointers, etc. */
10187 code0
= TREE_CODE (type0
);
10188 code1
= TREE_CODE (type1
);
10190 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10191 STRIP_TYPE_NOPS (op0
);
10192 STRIP_TYPE_NOPS (op1
);
10194 /* If an error was already reported for one of the arguments,
10195 avoid reporting another error. */
10197 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10198 return error_mark_node
;
10200 if ((invalid_op_diag
10201 = targetm
.invalid_binary_op (code
, type0
, type1
)))
10203 error_at (location
, invalid_op_diag
);
10204 return error_mark_node
;
10212 case TRUNC_DIV_EXPR
:
10213 case CEIL_DIV_EXPR
:
10214 case FLOOR_DIV_EXPR
:
10215 case ROUND_DIV_EXPR
:
10216 case EXACT_DIV_EXPR
:
10217 may_need_excess_precision
= true;
10220 may_need_excess_precision
= false;
10223 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
10225 op0
= TREE_OPERAND (op0
, 0);
10226 type0
= TREE_TYPE (op0
);
10228 else if (may_need_excess_precision
10229 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
10232 op0
= convert (eptype
, op0
);
10234 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
10236 op1
= TREE_OPERAND (op1
, 0);
10237 type1
= TREE_TYPE (op1
);
10239 else if (may_need_excess_precision
10240 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
10243 op1
= convert (eptype
, op1
);
10246 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
10248 /* In case when one of the operands of the binary operation is
10249 a vector and another is a scalar -- convert scalar to vector. */
10250 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
10252 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
10255 switch (convert_flag
)
10258 return error_mark_node
;
10261 bool maybe_const
= true;
10263 sc
= c_fully_fold (op0
, false, &maybe_const
);
10264 sc
= save_expr (sc
);
10265 sc
= convert (TREE_TYPE (type1
), sc
);
10266 op0
= build_vector_from_val (type1
, sc
);
10268 op0
= c_wrap_maybe_const (op0
, true);
10269 orig_type0
= type0
= TREE_TYPE (op0
);
10270 code0
= TREE_CODE (type0
);
10274 case stv_secondarg
:
10276 bool maybe_const
= true;
10278 sc
= c_fully_fold (op1
, false, &maybe_const
);
10279 sc
= save_expr (sc
);
10280 sc
= convert (TREE_TYPE (type0
), sc
);
10281 op1
= build_vector_from_val (type0
, sc
);
10283 op1
= c_wrap_maybe_const (op1
, true);
10284 orig_type1
= type1
= TREE_TYPE (op1
);
10285 code1
= TREE_CODE (type1
);
10297 /* Handle the pointer + int case. */
10298 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10300 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
10301 goto return_build_binary_op
;
10303 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
10305 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
10306 goto return_build_binary_op
;
10313 /* Subtraction of two similar pointers.
10314 We must subtract them as integers, then divide by object size. */
10315 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
10316 && comp_target_types (location
, type0
, type1
))
10318 ret
= pointer_diff (location
, op0
, op1
);
10319 goto return_build_binary_op
;
10321 /* Handle pointer minus int. Just like pointer plus int. */
10322 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10324 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
10325 goto return_build_binary_op
;
10335 case TRUNC_DIV_EXPR
:
10336 case CEIL_DIV_EXPR
:
10337 case FLOOR_DIV_EXPR
:
10338 case ROUND_DIV_EXPR
:
10339 case EXACT_DIV_EXPR
:
10340 doing_div_or_mod
= true;
10341 warn_for_div_by_zero (location
, op1
);
10343 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10344 || code0
== FIXED_POINT_TYPE
10345 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10346 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10347 || code1
== FIXED_POINT_TYPE
10348 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
10350 enum tree_code tcode0
= code0
, tcode1
= code1
;
10352 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10353 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
10354 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
10355 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
10357 if (!((tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
)
10358 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
10359 resultcode
= RDIV_EXPR
;
10361 /* Although it would be tempting to shorten always here, that
10362 loses on some targets, since the modulo instruction is
10363 undefined if the quotient can't be represented in the
10364 computation mode. We shorten only if unsigned or if
10365 dividing by something we know != -1. */
10366 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10367 || (TREE_CODE (op1
) == INTEGER_CST
10368 && !integer_all_onesp (op1
)));
10376 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10378 /* Allow vector types which are not floating point types. */
10379 else if (code0
== VECTOR_TYPE
10380 && code1
== VECTOR_TYPE
10381 && !VECTOR_FLOAT_TYPE_P (type0
)
10382 && !VECTOR_FLOAT_TYPE_P (type1
))
10386 case TRUNC_MOD_EXPR
:
10387 case FLOOR_MOD_EXPR
:
10388 doing_div_or_mod
= true;
10389 warn_for_div_by_zero (location
, op1
);
10391 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10392 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10393 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
10395 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10397 /* Although it would be tempting to shorten always here, that loses
10398 on some targets, since the modulo instruction is undefined if the
10399 quotient can't be represented in the computation mode. We shorten
10400 only if unsigned or if dividing by something we know != -1. */
10401 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10402 || (TREE_CODE (op1
) == INTEGER_CST
10403 && !integer_all_onesp (op1
)));
10408 case TRUTH_ANDIF_EXPR
:
10409 case TRUTH_ORIF_EXPR
:
10410 case TRUTH_AND_EXPR
:
10411 case TRUTH_OR_EXPR
:
10412 case TRUTH_XOR_EXPR
:
10413 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
10414 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
10415 || code0
== FIXED_POINT_TYPE
)
10416 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
10417 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
10418 || code1
== FIXED_POINT_TYPE
))
10420 /* Result of these operations is always an int,
10421 but that does not mean the operands should be
10422 converted to ints! */
10423 result_type
= integer_type_node
;
10424 if (op0_int_operands
)
10426 op0
= c_objc_common_truthvalue_conversion (location
, orig_op0
);
10427 op0
= remove_c_maybe_const_expr (op0
);
10430 op0
= c_objc_common_truthvalue_conversion (location
, op0
);
10431 if (op1_int_operands
)
10433 op1
= c_objc_common_truthvalue_conversion (location
, orig_op1
);
10434 op1
= remove_c_maybe_const_expr (op1
);
10437 op1
= c_objc_common_truthvalue_conversion (location
, op1
);
10441 if (code
== TRUTH_ANDIF_EXPR
)
10443 int_const_or_overflow
= (int_operands
10444 && TREE_CODE (orig_op0
) == INTEGER_CST
10445 && (op0
== truthvalue_false_node
10446 || TREE_CODE (orig_op1
) == INTEGER_CST
));
10447 int_const
= (int_const_or_overflow
10448 && !TREE_OVERFLOW (orig_op0
)
10449 && (op0
== truthvalue_false_node
10450 || !TREE_OVERFLOW (orig_op1
)));
10452 else if (code
== TRUTH_ORIF_EXPR
)
10454 int_const_or_overflow
= (int_operands
10455 && TREE_CODE (orig_op0
) == INTEGER_CST
10456 && (op0
== truthvalue_true_node
10457 || TREE_CODE (orig_op1
) == INTEGER_CST
));
10458 int_const
= (int_const_or_overflow
10459 && !TREE_OVERFLOW (orig_op0
)
10460 && (op0
== truthvalue_true_node
10461 || !TREE_OVERFLOW (orig_op1
)));
10465 /* Shift operations: result has same type as first operand;
10466 always convert second operand to int.
10467 Also set SHORT_SHIFT if shifting rightward. */
10470 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
10471 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
10473 result_type
= type0
;
10476 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10477 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10478 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
10479 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
10481 result_type
= type0
;
10484 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
10485 && code1
== INTEGER_TYPE
)
10487 doing_shift
= true;
10488 if (TREE_CODE (op1
) == INTEGER_CST
)
10490 if (tree_int_cst_sgn (op1
) < 0)
10493 if (c_inhibit_evaluation_warnings
== 0)
10494 warning_at (location
, 0, "right shift count is negative");
10498 if (!integer_zerop (op1
))
10501 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
10504 if (c_inhibit_evaluation_warnings
== 0)
10505 warning_at (location
, 0, "right shift count >= width "
10511 /* Use the type of the value to be shifted. */
10512 result_type
= type0
;
10513 /* Convert the non vector shift-count to an integer, regardless
10514 of size of value being shifted. */
10515 if (TREE_CODE (TREE_TYPE (op1
)) != VECTOR_TYPE
10516 && TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
10517 op1
= convert (integer_type_node
, op1
);
10518 /* Avoid converting op1 to result_type later. */
10524 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
10525 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
10527 result_type
= type0
;
10530 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10531 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10532 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
10533 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
10535 result_type
= type0
;
10538 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
10539 && code1
== INTEGER_TYPE
)
10541 doing_shift
= true;
10542 if (TREE_CODE (op1
) == INTEGER_CST
)
10544 if (tree_int_cst_sgn (op1
) < 0)
10547 if (c_inhibit_evaluation_warnings
== 0)
10548 warning_at (location
, 0, "left shift count is negative");
10551 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
10554 if (c_inhibit_evaluation_warnings
== 0)
10555 warning_at (location
, 0, "left shift count >= width of "
10560 /* Use the type of the value to be shifted. */
10561 result_type
= type0
;
10562 /* Convert the non vector shift-count to an integer, regardless
10563 of size of value being shifted. */
10564 if (TREE_CODE (TREE_TYPE (op1
)) != VECTOR_TYPE
10565 && TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
10566 op1
= convert (integer_type_node
, op1
);
10567 /* Avoid converting op1 to result_type later. */
10574 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
10577 if (!vector_types_compatible_elements_p (type0
, type1
))
10579 error_at (location
, "comparing vectors with different "
10581 return error_mark_node
;
10584 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
10586 error_at (location
, "comparing vectors with different "
10587 "number of elements");
10588 return error_mark_node
;
10591 /* Always construct signed integer vector type. */
10592 intt
= c_common_type_for_size (GET_MODE_BITSIZE
10593 (TYPE_MODE (TREE_TYPE (type0
))), 0);
10594 result_type
= build_opaque_vector_type (intt
,
10595 TYPE_VECTOR_SUBPARTS (type0
));
10599 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
10600 warning_at (location
,
10602 "comparing floating point with == or != is unsafe");
10603 /* Result of comparison is always int,
10604 but don't convert the args to int! */
10605 build_type
= integer_type_node
;
10606 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10607 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
10608 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10609 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
10611 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
10613 if (TREE_CODE (op0
) == ADDR_EXPR
10614 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0)))
10616 if (code
== EQ_EXPR
)
10617 warning_at (location
,
10619 "the comparison will always evaluate as %<false%> "
10620 "for the address of %qD will never be NULL",
10621 TREE_OPERAND (op0
, 0));
10623 warning_at (location
,
10625 "the comparison will always evaluate as %<true%> "
10626 "for the address of %qD will never be NULL",
10627 TREE_OPERAND (op0
, 0));
10629 result_type
= type0
;
10631 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
10633 if (TREE_CODE (op1
) == ADDR_EXPR
10634 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0)))
10636 if (code
== EQ_EXPR
)
10637 warning_at (location
,
10639 "the comparison will always evaluate as %<false%> "
10640 "for the address of %qD will never be NULL",
10641 TREE_OPERAND (op1
, 0));
10643 warning_at (location
,
10645 "the comparison will always evaluate as %<true%> "
10646 "for the address of %qD will never be NULL",
10647 TREE_OPERAND (op1
, 0));
10649 result_type
= type1
;
10651 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
10653 tree tt0
= TREE_TYPE (type0
);
10654 tree tt1
= TREE_TYPE (type1
);
10655 addr_space_t as0
= TYPE_ADDR_SPACE (tt0
);
10656 addr_space_t as1
= TYPE_ADDR_SPACE (tt1
);
10657 addr_space_t as_common
= ADDR_SPACE_GENERIC
;
10659 /* Anything compares with void *. void * compares with anything.
10660 Otherwise, the targets must be compatible
10661 and both must be object or both incomplete. */
10662 if (comp_target_types (location
, type0
, type1
))
10663 result_type
= common_pointer_type (type0
, type1
);
10664 else if (!addr_space_superset (as0
, as1
, &as_common
))
10666 error_at (location
, "comparison of pointers to "
10667 "disjoint address spaces");
10668 return error_mark_node
;
10670 else if (VOID_TYPE_P (tt0
) && !TYPE_ATOMIC (tt0
))
10672 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
)
10673 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10674 "comparison of %<void *%> with function pointer");
10676 else if (VOID_TYPE_P (tt1
) && !TYPE_ATOMIC (tt1
))
10678 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
)
10679 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10680 "comparison of %<void *%> with function pointer");
10683 /* Avoid warning about the volatile ObjC EH puts on decls. */
10685 pedwarn (location
, 0,
10686 "comparison of distinct pointer types lacks a cast");
10688 if (result_type
== NULL_TREE
)
10690 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
10691 result_type
= build_pointer_type
10692 (build_qualified_type (void_type_node
, qual
));
10695 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10697 result_type
= type0
;
10698 pedwarn (location
, 0, "comparison between pointer and integer");
10700 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
10702 result_type
= type1
;
10703 pedwarn (location
, 0, "comparison between pointer and integer");
10705 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
10706 || truth_value_p (TREE_CODE (orig_op0
)))
10707 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
10708 || truth_value_p (TREE_CODE (orig_op1
))))
10709 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
10716 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
10719 if (!vector_types_compatible_elements_p (type0
, type1
))
10721 error_at (location
, "comparing vectors with different "
10723 return error_mark_node
;
10726 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
10728 error_at (location
, "comparing vectors with different "
10729 "number of elements");
10730 return error_mark_node
;
10733 /* Always construct signed integer vector type. */
10734 intt
= c_common_type_for_size (GET_MODE_BITSIZE
10735 (TYPE_MODE (TREE_TYPE (type0
))), 0);
10736 result_type
= build_opaque_vector_type (intt
,
10737 TYPE_VECTOR_SUBPARTS (type0
));
10741 build_type
= integer_type_node
;
10742 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10743 || code0
== FIXED_POINT_TYPE
)
10744 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10745 || code1
== FIXED_POINT_TYPE
))
10747 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
10749 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (type0
));
10750 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
10751 addr_space_t as_common
;
10753 if (comp_target_types (location
, type0
, type1
))
10755 result_type
= common_pointer_type (type0
, type1
);
10756 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
10757 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
10758 pedwarn (location
, 0,
10759 "comparison of complete and incomplete pointers");
10760 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
10761 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10762 "ordered comparisons of pointers to functions");
10763 else if (null_pointer_constant_p (orig_op0
)
10764 || null_pointer_constant_p (orig_op1
))
10765 warning_at (location
, OPT_Wextra
,
10766 "ordered comparison of pointer with null pointer");
10769 else if (!addr_space_superset (as0
, as1
, &as_common
))
10771 error_at (location
, "comparison of pointers to "
10772 "disjoint address spaces");
10773 return error_mark_node
;
10777 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
10778 result_type
= build_pointer_type
10779 (build_qualified_type (void_type_node
, qual
));
10780 pedwarn (location
, 0,
10781 "comparison of distinct pointer types lacks a cast");
10784 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
10786 result_type
= type0
;
10788 pedwarn (location
, OPT_Wpedantic
,
10789 "ordered comparison of pointer with integer zero");
10790 else if (extra_warnings
)
10791 warning_at (location
, OPT_Wextra
,
10792 "ordered comparison of pointer with integer zero");
10794 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
10796 result_type
= type1
;
10798 pedwarn (location
, OPT_Wpedantic
,
10799 "ordered comparison of pointer with integer zero");
10800 else if (extra_warnings
)
10801 warning_at (location
, OPT_Wextra
,
10802 "ordered comparison of pointer with integer zero");
10804 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10806 result_type
= type0
;
10807 pedwarn (location
, 0, "comparison between pointer and integer");
10809 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
10811 result_type
= type1
;
10812 pedwarn (location
, 0, "comparison between pointer and integer");
10814 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
10815 || truth_value_p (TREE_CODE (orig_op0
)))
10816 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
10817 || truth_value_p (TREE_CODE (orig_op1
))))
10818 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
10822 gcc_unreachable ();
10825 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10826 return error_mark_node
;
10828 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10829 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
10830 || !vector_types_compatible_elements_p (type0
, type1
)))
10832 binary_op_error (location
, code
, type0
, type1
);
10833 return error_mark_node
;
10836 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
10837 || code0
== FIXED_POINT_TYPE
|| code0
== VECTOR_TYPE
)
10839 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
10840 || code1
== FIXED_POINT_TYPE
|| code1
== VECTOR_TYPE
))
10842 bool first_complex
= (code0
== COMPLEX_TYPE
);
10843 bool second_complex
= (code1
== COMPLEX_TYPE
);
10844 int none_complex
= (!first_complex
&& !second_complex
);
10846 if (shorten
|| common
|| short_compare
)
10848 result_type
= c_common_type (type0
, type1
);
10849 do_warn_double_promotion (result_type
, type0
, type1
,
10850 "implicit conversion from %qT to %qT "
10851 "to match other operand of binary "
10854 if (result_type
== error_mark_node
)
10855 return error_mark_node
;
10858 if (first_complex
!= second_complex
10859 && (code
== PLUS_EXPR
10860 || code
== MINUS_EXPR
10861 || code
== MULT_EXPR
10862 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
10863 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
10864 && flag_signed_zeros
)
10866 /* An operation on mixed real/complex operands must be
10867 handled specially, but the language-independent code can
10868 more easily optimize the plain complex arithmetic if
10869 -fno-signed-zeros. */
10870 tree real_type
= TREE_TYPE (result_type
);
10872 if (type0
!= orig_type0
|| type1
!= orig_type1
)
10874 gcc_assert (may_need_excess_precision
&& common
);
10875 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
10879 if (TREE_TYPE (op0
) != result_type
)
10880 op0
= convert_and_check (location
, result_type
, op0
);
10881 if (TREE_TYPE (op1
) != real_type
)
10882 op1
= convert_and_check (location
, real_type
, op1
);
10886 if (TREE_TYPE (op0
) != real_type
)
10887 op0
= convert_and_check (location
, real_type
, op0
);
10888 if (TREE_TYPE (op1
) != result_type
)
10889 op1
= convert_and_check (location
, result_type
, op1
);
10891 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
10892 return error_mark_node
;
10895 op0
= c_save_expr (op0
);
10896 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
10898 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
10903 case TRUNC_DIV_EXPR
:
10904 op1
= c_save_expr (op1
);
10905 imag
= build2 (resultcode
, real_type
, imag
, op1
);
10906 /* Fall through. */
10909 real
= build2 (resultcode
, real_type
, real
, op1
);
10917 op1
= c_save_expr (op1
);
10918 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
10920 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
10925 op0
= c_save_expr (op0
);
10926 imag
= build2 (resultcode
, real_type
, op0
, imag
);
10927 /* Fall through. */
10929 real
= build2 (resultcode
, real_type
, op0
, real
);
10932 real
= build2 (resultcode
, real_type
, op0
, real
);
10933 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
10939 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
10940 goto return_build_binary_op
;
10943 /* For certain operations (which identify themselves by shorten != 0)
10944 if both args were extended from the same smaller type,
10945 do the arithmetic in that type and then extend.
10947 shorten !=0 and !=1 indicates a bitwise operation.
10948 For them, this optimization is safe only if
10949 both args are zero-extended or both are sign-extended.
10950 Otherwise, we might change the result.
10951 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10952 but calculated in (unsigned short) it would be (unsigned short)-1. */
10954 if (shorten
&& none_complex
)
10956 final_type
= result_type
;
10957 result_type
= shorten_binary_op (result_type
, op0
, op1
,
10961 /* Shifts can be shortened if shifting right. */
10966 tree arg0
= get_narrower (op0
, &unsigned_arg
);
10968 final_type
= result_type
;
10970 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
10971 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
10973 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
10974 && tree_int_cst_sgn (op1
) > 0
10975 /* We can shorten only if the shift count is less than the
10976 number of bits in the smaller type size. */
10977 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
10978 /* We cannot drop an unsigned shift after sign-extension. */
10979 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
10981 /* Do an unsigned shift if the operand was zero-extended. */
10983 = c_common_signed_or_unsigned_type (unsigned_arg
,
10985 /* Convert value-to-be-shifted to that type. */
10986 if (TREE_TYPE (op0
) != result_type
)
10987 op0
= convert (result_type
, op0
);
10992 /* Comparison operations are shortened too but differently.
10993 They identify themselves by setting short_compare = 1. */
10997 /* Don't write &op0, etc., because that would prevent op0
10998 from being kept in a register.
10999 Instead, make copies of the our local variables and
11000 pass the copies by reference, then copy them back afterward. */
11001 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
11002 enum tree_code xresultcode
= resultcode
;
11004 = shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
11010 goto return_build_binary_op
;
11013 op0
= xop0
, op1
= xop1
;
11015 resultcode
= xresultcode
;
11017 if (c_inhibit_evaluation_warnings
== 0)
11019 bool op0_maybe_const
= true;
11020 bool op1_maybe_const
= true;
11021 tree orig_op0_folded
, orig_op1_folded
;
11023 if (in_late_binary_op
)
11025 orig_op0_folded
= orig_op0
;
11026 orig_op1_folded
= orig_op1
;
11030 /* Fold for the sake of possible warnings, as in
11031 build_conditional_expr. This requires the
11032 "original" values to be folded, not just op0 and
11034 c_inhibit_evaluation_warnings
++;
11035 op0
= c_fully_fold (op0
, require_constant_value
,
11037 op1
= c_fully_fold (op1
, require_constant_value
,
11039 c_inhibit_evaluation_warnings
--;
11040 orig_op0_folded
= c_fully_fold (orig_op0
,
11041 require_constant_value
,
11043 orig_op1_folded
= c_fully_fold (orig_op1
,
11044 require_constant_value
,
11048 if (warn_sign_compare
)
11049 warn_for_sign_compare (location
, orig_op0_folded
,
11050 orig_op1_folded
, op0
, op1
,
11051 result_type
, resultcode
);
11052 if (!in_late_binary_op
&& !int_operands
)
11054 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
11055 op0
= c_wrap_maybe_const (op0
, !op0_maybe_const
);
11056 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
11057 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
11063 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11064 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11065 Then the expression will be built.
11066 It will be given type FINAL_TYPE if that is nonzero;
11067 otherwise, it will be given type RESULT_TYPE. */
11071 binary_op_error (location
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
11072 return error_mark_node
;
11075 if (build_type
== NULL_TREE
)
11077 build_type
= result_type
;
11078 if ((type0
!= orig_type0
|| type1
!= orig_type1
)
11081 gcc_assert (may_need_excess_precision
&& common
);
11082 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
11088 op0
= ep_convert_and_check (location
, result_type
, op0
,
11089 semantic_result_type
);
11090 op1
= ep_convert_and_check (location
, result_type
, op1
,
11091 semantic_result_type
);
11093 /* This can happen if one operand has a vector type, and the other
11094 has a different type. */
11095 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
11096 return error_mark_node
;
11099 if ((flag_sanitize
& (SANITIZE_SHIFT
| SANITIZE_DIVIDE
11100 | SANITIZE_FLOAT_DIVIDE
))
11101 && current_function_decl
!= 0
11102 && !lookup_attribute ("no_sanitize_undefined",
11103 DECL_ATTRIBUTES (current_function_decl
))
11104 && (doing_div_or_mod
|| doing_shift
))
11106 /* OP0 and/or OP1 might have side-effects. */
11107 op0
= c_save_expr (op0
);
11108 op1
= c_save_expr (op1
);
11109 op0
= c_fully_fold (op0
, false, NULL
);
11110 op1
= c_fully_fold (op1
, false, NULL
);
11111 if (doing_div_or_mod
&& (flag_sanitize
& (SANITIZE_DIVIDE
11112 | SANITIZE_FLOAT_DIVIDE
)))
11113 instrument_expr
= ubsan_instrument_division (location
, op0
, op1
);
11114 else if (doing_shift
&& (flag_sanitize
& SANITIZE_SHIFT
))
11115 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
11118 /* Treat expressions in initializers specially as they can't trap. */
11119 if (int_const_or_overflow
)
11120 ret
= (require_constant_value
11121 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
11123 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
11125 ret
= build2 (resultcode
, build_type
, op0
, op1
);
11126 if (final_type
!= 0)
11127 ret
= convert (final_type
, ret
);
11129 return_build_binary_op
:
11130 gcc_assert (ret
!= error_mark_node
);
11131 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
11132 ret
= (int_operands
11133 ? note_integer_operands (ret
)
11134 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
11135 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
11136 && !in_late_binary_op
)
11137 ret
= note_integer_operands (ret
);
11138 if (semantic_result_type
)
11139 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
11140 protected_set_expr_location (ret
, location
);
11142 if (instrument_expr
!= NULL
)
11143 ret
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (ret
),
11144 instrument_expr
, ret
);
11150 /* Convert EXPR to be a truth-value, validating its type for this
11151 purpose. LOCATION is the source location for the expression. */
11154 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
11156 bool int_const
, int_operands
;
11158 switch (TREE_CODE (TREE_TYPE (expr
)))
11161 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
11162 return error_mark_node
;
11165 error_at (location
, "used struct type value where scalar is required");
11166 return error_mark_node
;
11169 error_at (location
, "used union type value where scalar is required");
11170 return error_mark_node
;
11173 error_at (location
, "void value not ignored as it ought to be");
11174 return error_mark_node
;
11176 case FUNCTION_TYPE
:
11177 gcc_unreachable ();
11180 error_at (location
, "used vector type where scalar is required");
11181 return error_mark_node
;
11187 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
11188 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
11189 if (int_operands
&& TREE_CODE (expr
) != INTEGER_CST
)
11191 expr
= remove_c_maybe_const_expr (expr
);
11192 expr
= build2 (NE_EXPR
, integer_type_node
, expr
,
11193 convert (TREE_TYPE (expr
), integer_zero_node
));
11194 expr
= note_integer_operands (expr
);
11197 /* ??? Should we also give an error for vectors rather than leaving
11198 those to give errors later? */
11199 expr
= c_common_truthvalue_conversion (location
, expr
);
11201 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
11203 if (TREE_OVERFLOW (expr
))
11206 return note_integer_operands (expr
);
11208 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
11209 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
11214 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11218 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
11220 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
11222 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
11223 /* Executing a compound literal inside a function reinitializes
11225 if (!TREE_STATIC (decl
))
11233 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11234 statement. LOC is the location of the OACC_PARALLEL. */
11237 c_finish_oacc_parallel (location_t loc
, tree clauses
, tree block
)
11241 block
= c_end_compound_stmt (loc
, block
, true);
11243 stmt
= make_node (OACC_PARALLEL
);
11244 TREE_TYPE (stmt
) = void_type_node
;
11245 OACC_PARALLEL_CLAUSES (stmt
) = clauses
;
11246 OACC_PARALLEL_BODY (stmt
) = block
;
11247 SET_EXPR_LOCATION (stmt
, loc
);
11249 return add_stmt (stmt
);
11252 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11253 statement. LOC is the location of the OACC_KERNELS. */
11256 c_finish_oacc_kernels (location_t loc
, tree clauses
, tree block
)
11260 block
= c_end_compound_stmt (loc
, block
, true);
11262 stmt
= make_node (OACC_KERNELS
);
11263 TREE_TYPE (stmt
) = void_type_node
;
11264 OACC_KERNELS_CLAUSES (stmt
) = clauses
;
11265 OACC_KERNELS_BODY (stmt
) = block
;
11266 SET_EXPR_LOCATION (stmt
, loc
);
11268 return add_stmt (stmt
);
11271 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11272 statement. LOC is the location of the OACC_DATA. */
11275 c_finish_oacc_data (location_t loc
, tree clauses
, tree block
)
11279 block
= c_end_compound_stmt (loc
, block
, true);
11281 stmt
= make_node (OACC_DATA
);
11282 TREE_TYPE (stmt
) = void_type_node
;
11283 OACC_DATA_CLAUSES (stmt
) = clauses
;
11284 OACC_DATA_BODY (stmt
) = block
;
11285 SET_EXPR_LOCATION (stmt
, loc
);
11287 return add_stmt (stmt
);
11290 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11293 c_begin_omp_parallel (void)
11297 keep_next_level ();
11298 block
= c_begin_compound_stmt (true);
11303 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11304 statement. LOC is the location of the OMP_PARALLEL. */
11307 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
11311 block
= c_end_compound_stmt (loc
, block
, true);
11313 stmt
= make_node (OMP_PARALLEL
);
11314 TREE_TYPE (stmt
) = void_type_node
;
11315 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
11316 OMP_PARALLEL_BODY (stmt
) = block
;
11317 SET_EXPR_LOCATION (stmt
, loc
);
11319 return add_stmt (stmt
);
11322 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11325 c_begin_omp_task (void)
11329 keep_next_level ();
11330 block
= c_begin_compound_stmt (true);
11335 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11336 statement. LOC is the location of the #pragma. */
11339 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
11343 block
= c_end_compound_stmt (loc
, block
, true);
11345 stmt
= make_node (OMP_TASK
);
11346 TREE_TYPE (stmt
) = void_type_node
;
11347 OMP_TASK_CLAUSES (stmt
) = clauses
;
11348 OMP_TASK_BODY (stmt
) = block
;
11349 SET_EXPR_LOCATION (stmt
, loc
);
11351 return add_stmt (stmt
);
11354 /* Generate GOMP_cancel call for #pragma omp cancel. */
11357 c_finish_omp_cancel (location_t loc
, tree clauses
)
11359 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCEL
);
11361 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
11363 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
11365 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
11367 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
11371 error_at (loc
, "%<#pragma omp cancel must specify one of "
11372 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11376 tree ifc
= find_omp_clause (clauses
, OMP_CLAUSE_IF
);
11377 if (ifc
!= NULL_TREE
)
11379 tree type
= TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc
));
11380 ifc
= fold_build2_loc (OMP_CLAUSE_LOCATION (ifc
), NE_EXPR
,
11381 boolean_type_node
, OMP_CLAUSE_IF_EXPR (ifc
),
11382 build_zero_cst (type
));
11385 ifc
= boolean_true_node
;
11386 tree stmt
= build_call_expr_loc (loc
, fn
, 2,
11387 build_int_cst (integer_type_node
, mask
),
11392 /* Generate GOMP_cancellation_point call for
11393 #pragma omp cancellation point. */
11396 c_finish_omp_cancellation_point (location_t loc
, tree clauses
)
11398 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT
);
11400 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
11402 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
11404 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
11406 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
11410 error_at (loc
, "%<#pragma omp cancellation point must specify one of "
11411 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11415 tree stmt
= build_call_expr_loc (loc
, fn
, 1,
11416 build_int_cst (integer_type_node
, mask
));
11420 /* Helper function for handle_omp_array_sections. Called recursively
11421 to handle multiple array-section-subscripts. C is the clause,
11422 T current expression (initially OMP_CLAUSE_DECL), which is either
11423 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11424 expression if specified, TREE_VALUE length expression if specified,
11425 TREE_CHAIN is what it has been specified after, or some decl.
11426 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11427 set to true if any of the array-section-subscript could have length
11428 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11429 first array-section-subscript which is known not to have length
11431 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11432 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11433 all are or may have length of 1, array-section-subscript [:2] is the
11434 first one knonwn not to have length 1. For array-section-subscript
11435 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11436 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11437 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11438 case though, as some lengths could be zero. */
11441 handle_omp_array_sections_1 (tree c
, tree t
, vec
<tree
> &types
,
11442 bool &maybe_zero_len
, unsigned int &first_non_one
)
11444 tree ret
, low_bound
, length
, type
;
11445 if (TREE_CODE (t
) != TREE_LIST
)
11447 if (error_operand_p (t
))
11448 return error_mark_node
;
11449 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11452 error_at (OMP_CLAUSE_LOCATION (c
),
11453 "%qD is not a variable in %qs clause", t
,
11454 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11456 error_at (OMP_CLAUSE_LOCATION (c
),
11457 "%qE is not a variable in %qs clause", t
,
11458 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11459 return error_mark_node
;
11461 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
11462 && TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
11464 error_at (OMP_CLAUSE_LOCATION (c
),
11465 "%qD is threadprivate variable in %qs clause", t
,
11466 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11467 return error_mark_node
;
11472 ret
= handle_omp_array_sections_1 (c
, TREE_CHAIN (t
), types
,
11473 maybe_zero_len
, first_non_one
);
11474 if (ret
== error_mark_node
|| ret
== NULL_TREE
)
11477 type
= TREE_TYPE (ret
);
11478 low_bound
= TREE_PURPOSE (t
);
11479 length
= TREE_VALUE (t
);
11481 if (low_bound
== error_mark_node
|| length
== error_mark_node
)
11482 return error_mark_node
;
11484 if (low_bound
&& !INTEGRAL_TYPE_P (TREE_TYPE (low_bound
)))
11486 error_at (OMP_CLAUSE_LOCATION (c
),
11487 "low bound %qE of array section does not have integral type",
11489 return error_mark_node
;
11491 if (length
&& !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
11493 error_at (OMP_CLAUSE_LOCATION (c
),
11494 "length %qE of array section does not have integral type",
11496 return error_mark_node
;
11499 && TREE_CODE (low_bound
) == INTEGER_CST
11500 && TYPE_PRECISION (TREE_TYPE (low_bound
))
11501 > TYPE_PRECISION (sizetype
))
11502 low_bound
= fold_convert (sizetype
, low_bound
);
11504 && TREE_CODE (length
) == INTEGER_CST
11505 && TYPE_PRECISION (TREE_TYPE (length
))
11506 > TYPE_PRECISION (sizetype
))
11507 length
= fold_convert (sizetype
, length
);
11508 if (low_bound
== NULL_TREE
)
11509 low_bound
= integer_zero_node
;
11511 if (length
!= NULL_TREE
)
11513 if (!integer_nonzerop (length
))
11514 maybe_zero_len
= true;
11515 if (first_non_one
== types
.length ()
11516 && (TREE_CODE (length
) != INTEGER_CST
|| integer_onep (length
)))
11519 if (TREE_CODE (type
) == ARRAY_TYPE
)
11521 if (length
== NULL_TREE
11522 && (TYPE_DOMAIN (type
) == NULL_TREE
11523 || TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
))
11525 error_at (OMP_CLAUSE_LOCATION (c
),
11526 "for unknown bound array type length expression must "
11528 return error_mark_node
;
11530 if (TREE_CODE (low_bound
) == INTEGER_CST
11531 && tree_int_cst_sgn (low_bound
) == -1)
11533 error_at (OMP_CLAUSE_LOCATION (c
),
11534 "negative low bound in array section in %qs clause",
11535 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11536 return error_mark_node
;
11538 if (length
!= NULL_TREE
11539 && TREE_CODE (length
) == INTEGER_CST
11540 && tree_int_cst_sgn (length
) == -1)
11542 error_at (OMP_CLAUSE_LOCATION (c
),
11543 "negative length in array section in %qs clause",
11544 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11545 return error_mark_node
;
11547 if (TYPE_DOMAIN (type
)
11548 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
11549 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
11552 tree size
= size_binop (PLUS_EXPR
,
11553 TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
11555 if (TREE_CODE (low_bound
) == INTEGER_CST
)
11557 if (tree_int_cst_lt (size
, low_bound
))
11559 error_at (OMP_CLAUSE_LOCATION (c
),
11560 "low bound %qE above array section size "
11561 "in %qs clause", low_bound
,
11562 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11563 return error_mark_node
;
11565 if (tree_int_cst_equal (size
, low_bound
))
11566 maybe_zero_len
= true;
11567 else if (length
== NULL_TREE
11568 && first_non_one
== types
.length ()
11569 && tree_int_cst_equal
11570 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
11574 else if (length
== NULL_TREE
)
11576 maybe_zero_len
= true;
11577 if (first_non_one
== types
.length ())
11580 if (length
&& TREE_CODE (length
) == INTEGER_CST
)
11582 if (tree_int_cst_lt (size
, length
))
11584 error_at (OMP_CLAUSE_LOCATION (c
),
11585 "length %qE above array section size "
11586 "in %qs clause", length
,
11587 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11588 return error_mark_node
;
11590 if (TREE_CODE (low_bound
) == INTEGER_CST
)
11593 = size_binop (PLUS_EXPR
,
11594 fold_convert (sizetype
, low_bound
),
11595 fold_convert (sizetype
, length
));
11596 if (TREE_CODE (lbpluslen
) == INTEGER_CST
11597 && tree_int_cst_lt (size
, lbpluslen
))
11599 error_at (OMP_CLAUSE_LOCATION (c
),
11600 "high bound %qE above array section size "
11601 "in %qs clause", lbpluslen
,
11602 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11603 return error_mark_node
;
11608 else if (length
== NULL_TREE
)
11610 maybe_zero_len
= true;
11611 if (first_non_one
== types
.length ())
11615 /* For [lb:] we will need to evaluate lb more than once. */
11616 if (length
== NULL_TREE
&& OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
11618 tree lb
= c_save_expr (low_bound
);
11619 if (lb
!= low_bound
)
11621 TREE_PURPOSE (t
) = lb
;
11626 else if (TREE_CODE (type
) == POINTER_TYPE
)
11628 if (length
== NULL_TREE
)
11630 error_at (OMP_CLAUSE_LOCATION (c
),
11631 "for pointer type length expression must be specified");
11632 return error_mark_node
;
11634 /* If there is a pointer type anywhere but in the very first
11635 array-section-subscript, the array section can't be contiguous. */
11636 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
11637 && TREE_CODE (TREE_CHAIN (t
)) == TREE_LIST
)
11639 error_at (OMP_CLAUSE_LOCATION (c
),
11640 "array section is not contiguous in %qs clause",
11641 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11642 return error_mark_node
;
11647 error_at (OMP_CLAUSE_LOCATION (c
),
11648 "%qE does not have pointer or array type", ret
);
11649 return error_mark_node
;
11651 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
11652 types
.safe_push (TREE_TYPE (ret
));
11653 /* We will need to evaluate lb more than once. */
11654 tree lb
= c_save_expr (low_bound
);
11655 if (lb
!= low_bound
)
11657 TREE_PURPOSE (t
) = lb
;
11660 ret
= build_array_ref (OMP_CLAUSE_LOCATION (c
), ret
, low_bound
);
11664 /* Handle array sections for clause C. */
11667 handle_omp_array_sections (tree c
)
11669 bool maybe_zero_len
= false;
11670 unsigned int first_non_one
= 0;
11671 vec
<tree
> types
= vNULL
;
11672 tree first
= handle_omp_array_sections_1 (c
, OMP_CLAUSE_DECL (c
), types
,
11673 maybe_zero_len
, first_non_one
);
11674 if (first
== error_mark_node
)
11679 if (first
== NULL_TREE
)
11684 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
)
11686 tree t
= OMP_CLAUSE_DECL (c
);
11687 tree tem
= NULL_TREE
;
11689 /* Need to evaluate side effects in the length expressions
11691 while (TREE_CODE (t
) == TREE_LIST
)
11693 if (TREE_VALUE (t
) && TREE_SIDE_EFFECTS (TREE_VALUE (t
)))
11695 if (tem
== NULL_TREE
)
11696 tem
= TREE_VALUE (t
);
11698 tem
= build2 (COMPOUND_EXPR
, TREE_TYPE (tem
),
11699 TREE_VALUE (t
), tem
);
11701 t
= TREE_CHAIN (t
);
11704 first
= build2 (COMPOUND_EXPR
, TREE_TYPE (first
), tem
, first
);
11705 first
= c_fully_fold (first
, false, NULL
);
11706 OMP_CLAUSE_DECL (c
) = first
;
11710 unsigned int num
= types
.length (), i
;
11711 tree t
, side_effects
= NULL_TREE
, size
= NULL_TREE
;
11712 tree condition
= NULL_TREE
;
11714 if (int_size_in_bytes (TREE_TYPE (first
)) <= 0)
11715 maybe_zero_len
= true;
11717 for (i
= num
, t
= OMP_CLAUSE_DECL (c
); i
> 0;
11718 t
= TREE_CHAIN (t
))
11720 tree low_bound
= TREE_PURPOSE (t
);
11721 tree length
= TREE_VALUE (t
);
11725 && TREE_CODE (low_bound
) == INTEGER_CST
11726 && TYPE_PRECISION (TREE_TYPE (low_bound
))
11727 > TYPE_PRECISION (sizetype
))
11728 low_bound
= fold_convert (sizetype
, low_bound
);
11730 && TREE_CODE (length
) == INTEGER_CST
11731 && TYPE_PRECISION (TREE_TYPE (length
))
11732 > TYPE_PRECISION (sizetype
))
11733 length
= fold_convert (sizetype
, length
);
11734 if (low_bound
== NULL_TREE
)
11735 low_bound
= integer_zero_node
;
11736 if (!maybe_zero_len
&& i
> first_non_one
)
11738 if (integer_nonzerop (low_bound
))
11739 goto do_warn_noncontiguous
;
11740 if (length
!= NULL_TREE
11741 && TREE_CODE (length
) == INTEGER_CST
11742 && TYPE_DOMAIN (types
[i
])
11743 && TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
]))
11744 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])))
11748 size
= size_binop (PLUS_EXPR
,
11749 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
11751 if (!tree_int_cst_equal (length
, size
))
11753 do_warn_noncontiguous
:
11754 error_at (OMP_CLAUSE_LOCATION (c
),
11755 "array section is not contiguous in %qs "
11757 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11762 if (length
!= NULL_TREE
11763 && TREE_SIDE_EFFECTS (length
))
11765 if (side_effects
== NULL_TREE
)
11766 side_effects
= length
;
11768 side_effects
= build2 (COMPOUND_EXPR
,
11769 TREE_TYPE (side_effects
),
11770 length
, side_effects
);
11777 if (i
> first_non_one
&& length
&& integer_nonzerop (length
))
11780 l
= fold_convert (sizetype
, length
);
11783 l
= size_binop (PLUS_EXPR
,
11784 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
11786 l
= size_binop (MINUS_EXPR
, l
,
11787 fold_convert (sizetype
, low_bound
));
11789 if (i
> first_non_one
)
11791 l
= fold_build2 (NE_EXPR
, boolean_type_node
, l
,
11793 if (condition
== NULL_TREE
)
11796 condition
= fold_build2 (BIT_AND_EXPR
, boolean_type_node
,
11799 else if (size
== NULL_TREE
)
11801 size
= size_in_bytes (TREE_TYPE (types
[i
]));
11802 size
= size_binop (MULT_EXPR
, size
, l
);
11804 size
= fold_build3 (COND_EXPR
, sizetype
, condition
,
11805 size
, size_zero_node
);
11808 size
= size_binop (MULT_EXPR
, size
, l
);
11813 size
= build2 (COMPOUND_EXPR
, sizetype
, side_effects
, size
);
11814 first
= c_fully_fold (first
, false, NULL
);
11815 OMP_CLAUSE_DECL (c
) = first
;
11817 size
= c_fully_fold (size
, false, NULL
);
11818 OMP_CLAUSE_SIZE (c
) = size
;
11819 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
11821 gcc_assert (OMP_CLAUSE_MAP_KIND (c
) != OMP_CLAUSE_MAP_FORCE_DEVICEPTR
);
11822 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_MAP
);
11823 OMP_CLAUSE_MAP_KIND (c2
) = OMP_CLAUSE_MAP_POINTER
;
11824 if (!c_mark_addressable (t
))
11826 OMP_CLAUSE_DECL (c2
) = t
;
11827 t
= build_fold_addr_expr (first
);
11828 t
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), ptrdiff_type_node
, t
);
11829 tree ptr
= OMP_CLAUSE_DECL (c2
);
11830 if (!POINTER_TYPE_P (TREE_TYPE (ptr
)))
11831 ptr
= build_fold_addr_expr (ptr
);
11832 t
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
11833 ptrdiff_type_node
, t
,
11834 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
11835 ptrdiff_type_node
, ptr
));
11836 t
= c_fully_fold (t
, false, NULL
);
11837 OMP_CLAUSE_SIZE (c2
) = t
;
11838 OMP_CLAUSE_CHAIN (c2
) = OMP_CLAUSE_CHAIN (c
);
11839 OMP_CLAUSE_CHAIN (c
) = c2
;
11844 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11845 an inline call. But, remap
11846 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11847 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11850 c_clone_omp_udr (tree stmt
, tree omp_decl1
, tree omp_decl2
,
11851 tree decl
, tree placeholder
)
11854 hash_map
<tree
, tree
> decl_map
;
11856 decl_map
.put (omp_decl1
, placeholder
);
11857 decl_map
.put (omp_decl2
, decl
);
11858 memset (&id
, 0, sizeof (id
));
11859 id
.src_fn
= DECL_CONTEXT (omp_decl1
);
11860 id
.dst_fn
= current_function_decl
;
11861 id
.src_cfun
= DECL_STRUCT_FUNCTION (id
.src_fn
);
11862 id
.decl_map
= &decl_map
;
11864 id
.copy_decl
= copy_decl_no_change
;
11865 id
.transform_call_graph_edges
= CB_CGE_DUPLICATE
;
11866 id
.transform_new_cfg
= true;
11867 id
.transform_return_to_modify
= false;
11868 id
.transform_lang_insert_block
= NULL
;
11870 walk_tree (&stmt
, copy_tree_body_r
, &id
, NULL
);
11874 /* Helper function of c_finish_omp_clauses, called via walk_tree.
11875 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11878 c_find_omp_placeholder_r (tree
*tp
, int *, void *data
)
11880 if (*tp
== (tree
) data
)
11885 /* For all elements of CLAUSES, validate them against their constraints.
11886 Remove any elements from the list that are invalid. */
11889 c_finish_omp_clauses (tree clauses
)
11891 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
11892 bitmap_head aligned_head
;
11894 bool branch_seen
= false;
11895 bool copyprivate_seen
= false;
11896 tree
*nowait_clause
= NULL
;
11898 bitmap_obstack_initialize (NULL
);
11899 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
11900 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
11901 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
11902 bitmap_initialize (&aligned_head
, &bitmap_default_obstack
);
11904 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
11906 bool remove
= false;
11907 bool need_complete
= false;
11908 bool need_implicitly_determined
= false;
11910 switch (OMP_CLAUSE_CODE (c
))
11912 case OMP_CLAUSE_SHARED
:
11913 need_implicitly_determined
= true;
11914 goto check_dup_generic
;
11916 case OMP_CLAUSE_PRIVATE
:
11917 need_complete
= true;
11918 need_implicitly_determined
= true;
11919 goto check_dup_generic
;
11921 case OMP_CLAUSE_REDUCTION
:
11922 need_implicitly_determined
= true;
11923 t
= OMP_CLAUSE_DECL (c
);
11924 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == NULL_TREE
11925 && (FLOAT_TYPE_P (TREE_TYPE (t
))
11926 || TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
))
11928 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
11929 const char *r_name
= NULL
;
11938 if (TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
)
11942 if (TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
)
11954 case TRUTH_ANDIF_EXPR
:
11955 if (FLOAT_TYPE_P (TREE_TYPE (t
)))
11958 case TRUTH_ORIF_EXPR
:
11959 if (FLOAT_TYPE_P (TREE_TYPE (t
)))
11963 gcc_unreachable ();
11967 error_at (OMP_CLAUSE_LOCATION (c
),
11968 "%qE has invalid type for %<reduction(%s)%>",
11974 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == error_mark_node
)
11976 error_at (OMP_CLAUSE_LOCATION (c
),
11977 "user defined reduction not found for %qD", t
);
11981 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
11983 tree list
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
);
11984 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (t
));
11985 tree placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
11986 VAR_DECL
, NULL_TREE
, type
);
11987 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = placeholder
;
11988 DECL_ARTIFICIAL (placeholder
) = 1;
11989 DECL_IGNORED_P (placeholder
) = 1;
11990 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 0)))
11991 c_mark_addressable (placeholder
);
11992 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 1)))
11993 c_mark_addressable (OMP_CLAUSE_DECL (c
));
11994 OMP_CLAUSE_REDUCTION_MERGE (c
)
11995 = c_clone_omp_udr (TREE_VEC_ELT (list
, 2),
11996 TREE_VEC_ELT (list
, 0),
11997 TREE_VEC_ELT (list
, 1),
11998 OMP_CLAUSE_DECL (c
), placeholder
);
11999 OMP_CLAUSE_REDUCTION_MERGE (c
)
12000 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
12001 void_type_node
, NULL_TREE
,
12002 OMP_CLAUSE_REDUCTION_MERGE (c
), NULL_TREE
);
12003 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c
)) = 1;
12004 if (TREE_VEC_LENGTH (list
) == 6)
12006 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 3)))
12007 c_mark_addressable (OMP_CLAUSE_DECL (c
));
12008 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 4)))
12009 c_mark_addressable (placeholder
);
12010 tree init
= TREE_VEC_ELT (list
, 5);
12011 if (init
== error_mark_node
)
12012 init
= DECL_INITIAL (TREE_VEC_ELT (list
, 3));
12013 OMP_CLAUSE_REDUCTION_INIT (c
)
12014 = c_clone_omp_udr (init
, TREE_VEC_ELT (list
, 4),
12015 TREE_VEC_ELT (list
, 3),
12016 OMP_CLAUSE_DECL (c
), placeholder
);
12017 if (TREE_VEC_ELT (list
, 5) == error_mark_node
)
12018 OMP_CLAUSE_REDUCTION_INIT (c
)
12019 = build2 (INIT_EXPR
, TREE_TYPE (t
), t
,
12020 OMP_CLAUSE_REDUCTION_INIT (c
));
12021 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
12022 c_find_omp_placeholder_r
,
12023 placeholder
, NULL
))
12024 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c
) = 1;
12029 if (AGGREGATE_TYPE_P (TREE_TYPE (t
)))
12030 init
= build_constructor (TREE_TYPE (t
), NULL
);
12032 init
= fold_convert (TREE_TYPE (t
), integer_zero_node
);
12033 OMP_CLAUSE_REDUCTION_INIT (c
)
12034 = build2 (INIT_EXPR
, TREE_TYPE (t
), t
, init
);
12036 OMP_CLAUSE_REDUCTION_INIT (c
)
12037 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
12038 void_type_node
, NULL_TREE
,
12039 OMP_CLAUSE_REDUCTION_INIT (c
), NULL_TREE
);
12040 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c
)) = 1;
12042 goto check_dup_generic
;
12044 case OMP_CLAUSE_COPYPRIVATE
:
12045 copyprivate_seen
= true;
12048 error_at (OMP_CLAUSE_LOCATION (*nowait_clause
),
12049 "%<nowait%> clause must not be used together "
12050 "with %<copyprivate%>");
12051 *nowait_clause
= OMP_CLAUSE_CHAIN (*nowait_clause
);
12052 nowait_clause
= NULL
;
12054 goto check_dup_generic
;
12056 case OMP_CLAUSE_COPYIN
:
12057 t
= OMP_CLAUSE_DECL (c
);
12058 if (TREE_CODE (t
) != VAR_DECL
|| !DECL_THREAD_LOCAL_P (t
))
12060 error_at (OMP_CLAUSE_LOCATION (c
),
12061 "%qE must be %<threadprivate%> for %<copyin%>", t
);
12065 goto check_dup_generic
;
12067 case OMP_CLAUSE_LINEAR
:
12068 t
= OMP_CLAUSE_DECL (c
);
12069 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
12070 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
12072 error_at (OMP_CLAUSE_LOCATION (c
),
12073 "linear clause applied to non-integral non-pointer "
12074 "variable with type %qT", TREE_TYPE (t
));
12078 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c
))) == POINTER_TYPE
)
12080 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
12081 s
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
), PLUS_EXPR
,
12082 OMP_CLAUSE_DECL (c
), s
);
12083 s
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
12084 sizetype
, s
, OMP_CLAUSE_DECL (c
));
12085 if (s
== error_mark_node
)
12087 OMP_CLAUSE_LINEAR_STEP (c
) = s
;
12090 OMP_CLAUSE_LINEAR_STEP (c
)
12091 = fold_convert (TREE_TYPE (t
), OMP_CLAUSE_LINEAR_STEP (c
));
12092 goto check_dup_generic
;
12095 t
= OMP_CLAUSE_DECL (c
);
12096 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12098 error_at (OMP_CLAUSE_LOCATION (c
),
12099 "%qE is not a variable in clause %qs", t
,
12100 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12103 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
12104 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
12105 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
12107 error_at (OMP_CLAUSE_LOCATION (c
),
12108 "%qE appears more than once in data clauses", t
);
12112 bitmap_set_bit (&generic_head
, DECL_UID (t
));
12115 case OMP_CLAUSE_FIRSTPRIVATE
:
12116 t
= OMP_CLAUSE_DECL (c
);
12117 need_complete
= true;
12118 need_implicitly_determined
= true;
12119 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12121 error_at (OMP_CLAUSE_LOCATION (c
),
12122 "%qE is not a variable in clause %<firstprivate%>", t
);
12125 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
12126 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
12128 error_at (OMP_CLAUSE_LOCATION (c
),
12129 "%qE appears more than once in data clauses", t
);
12133 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
12136 case OMP_CLAUSE_LASTPRIVATE
:
12137 t
= OMP_CLAUSE_DECL (c
);
12138 need_complete
= true;
12139 need_implicitly_determined
= true;
12140 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12142 error_at (OMP_CLAUSE_LOCATION (c
),
12143 "%qE is not a variable in clause %<lastprivate%>", t
);
12146 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
12147 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
12149 error_at (OMP_CLAUSE_LOCATION (c
),
12150 "%qE appears more than once in data clauses", t
);
12154 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
12157 case OMP_CLAUSE_ALIGNED
:
12158 t
= OMP_CLAUSE_DECL (c
);
12159 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12161 error_at (OMP_CLAUSE_LOCATION (c
),
12162 "%qE is not a variable in %<aligned%> clause", t
);
12165 else if (!POINTER_TYPE_P (TREE_TYPE (t
))
12166 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
12168 error_at (OMP_CLAUSE_LOCATION (c
),
12169 "%qE in %<aligned%> clause is neither a pointer nor "
12173 else if (bitmap_bit_p (&aligned_head
, DECL_UID (t
)))
12175 error_at (OMP_CLAUSE_LOCATION (c
),
12176 "%qE appears more than once in %<aligned%> clauses",
12181 bitmap_set_bit (&aligned_head
, DECL_UID (t
));
12184 case OMP_CLAUSE_DEPEND
:
12185 t
= OMP_CLAUSE_DECL (c
);
12186 if (TREE_CODE (t
) == TREE_LIST
)
12188 if (handle_omp_array_sections (c
))
12192 if (t
== error_mark_node
)
12194 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12196 error_at (OMP_CLAUSE_LOCATION (c
),
12197 "%qE is not a variable in %<depend%> clause", t
);
12200 else if (!c_mark_addressable (t
))
12204 case OMP_CLAUSE_MAP
:
12205 case OMP_CLAUSE_TO
:
12206 case OMP_CLAUSE_FROM
:
12207 t
= OMP_CLAUSE_DECL (c
);
12208 if (TREE_CODE (t
) == TREE_LIST
)
12210 if (handle_omp_array_sections (c
))
12214 t
= OMP_CLAUSE_DECL (c
);
12215 if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
12217 error_at (OMP_CLAUSE_LOCATION (c
),
12218 "array section does not have mappable type "
12220 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12226 if (t
== error_mark_node
)
12228 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12230 error_at (OMP_CLAUSE_LOCATION (c
),
12231 "%qE is not a variable in %qs clause", t
,
12232 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12235 else if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
12237 error_at (OMP_CLAUSE_LOCATION (c
),
12238 "%qD is threadprivate variable in %qs clause", t
,
12239 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12242 else if (!c_mark_addressable (t
))
12244 else if (!(OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
12245 && (OMP_CLAUSE_MAP_KIND (c
) == OMP_CLAUSE_MAP_POINTER
12246 || (OMP_CLAUSE_MAP_KIND (c
)
12247 == OMP_CLAUSE_MAP_FORCE_DEVICEPTR
)))
12248 && !lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
12250 error_at (OMP_CLAUSE_LOCATION (c
),
12251 "%qD does not have a mappable type in %qs clause", t
,
12252 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12255 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
)))
12257 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
12258 error ("%qD appears more than once in motion clauses", t
);
12260 error ("%qD appears more than once in map clauses", t
);
12264 bitmap_set_bit (&generic_head
, DECL_UID (t
));
12267 case OMP_CLAUSE_UNIFORM
:
12268 t
= OMP_CLAUSE_DECL (c
);
12269 if (TREE_CODE (t
) != PARM_DECL
)
12272 error_at (OMP_CLAUSE_LOCATION (c
),
12273 "%qD is not an argument in %<uniform%> clause", t
);
12275 error_at (OMP_CLAUSE_LOCATION (c
),
12276 "%qE is not an argument in %<uniform%> clause", t
);
12280 goto check_dup_generic
;
12282 case OMP_CLAUSE_NOWAIT
:
12283 if (copyprivate_seen
)
12285 error_at (OMP_CLAUSE_LOCATION (c
),
12286 "%<nowait%> clause must not be used together "
12287 "with %<copyprivate%>");
12291 nowait_clause
= pc
;
12292 pc
= &OMP_CLAUSE_CHAIN (c
);
12295 case OMP_CLAUSE_IF
:
12296 case OMP_CLAUSE_NUM_THREADS
:
12297 case OMP_CLAUSE_NUM_TEAMS
:
12298 case OMP_CLAUSE_THREAD_LIMIT
:
12299 case OMP_CLAUSE_SCHEDULE
:
12300 case OMP_CLAUSE_ORDERED
:
12301 case OMP_CLAUSE_DEFAULT
:
12302 case OMP_CLAUSE_UNTIED
:
12303 case OMP_CLAUSE_COLLAPSE
:
12304 case OMP_CLAUSE_FINAL
:
12305 case OMP_CLAUSE_MERGEABLE
:
12306 case OMP_CLAUSE_SAFELEN
:
12307 case OMP_CLAUSE_SIMDLEN
:
12308 case OMP_CLAUSE_DEVICE
:
12309 case OMP_CLAUSE_DIST_SCHEDULE
:
12310 case OMP_CLAUSE_PARALLEL
:
12311 case OMP_CLAUSE_FOR
:
12312 case OMP_CLAUSE_SECTIONS
:
12313 case OMP_CLAUSE_TASKGROUP
:
12314 case OMP_CLAUSE_PROC_BIND
:
12315 case OMP_CLAUSE__CILK_FOR_COUNT_
:
12316 case OMP_CLAUSE_NUM_GANGS
:
12317 case OMP_CLAUSE_NUM_WORKERS
:
12318 case OMP_CLAUSE_VECTOR_LENGTH
:
12319 case OMP_CLAUSE_ASYNC
:
12320 case OMP_CLAUSE_WAIT
:
12321 pc
= &OMP_CLAUSE_CHAIN (c
);
12324 case OMP_CLAUSE_INBRANCH
:
12325 case OMP_CLAUSE_NOTINBRANCH
:
12328 error_at (OMP_CLAUSE_LOCATION (c
),
12329 "%<inbranch%> clause is incompatible with "
12330 "%<notinbranch%>");
12334 branch_seen
= true;
12335 pc
= &OMP_CLAUSE_CHAIN (c
);
12339 gcc_unreachable ();
12344 t
= OMP_CLAUSE_DECL (c
);
12348 t
= require_complete_type (t
);
12349 if (t
== error_mark_node
)
12353 if (need_implicitly_determined
)
12355 const char *share_name
= NULL
;
12357 if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
12358 share_name
= "threadprivate";
12359 else switch (c_omp_predetermined_sharing (t
))
12361 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
12363 case OMP_CLAUSE_DEFAULT_SHARED
:
12364 /* const vars may be specified in firstprivate clause. */
12365 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12366 && TREE_READONLY (t
))
12368 share_name
= "shared";
12370 case OMP_CLAUSE_DEFAULT_PRIVATE
:
12371 share_name
= "private";
12374 gcc_unreachable ();
12378 error_at (OMP_CLAUSE_LOCATION (c
),
12379 "%qE is predetermined %qs for %qs",
12381 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12388 *pc
= OMP_CLAUSE_CHAIN (c
);
12390 pc
= &OMP_CLAUSE_CHAIN (c
);
12393 bitmap_obstack_release (NULL
);
12397 /* Create a transaction node. */
12400 c_finish_transaction (location_t loc
, tree block
, int flags
)
12402 tree stmt
= build_stmt (loc
, TRANSACTION_EXPR
, block
);
12403 if (flags
& TM_STMT_ATTR_OUTER
)
12404 TRANSACTION_EXPR_OUTER (stmt
) = 1;
12405 if (flags
& TM_STMT_ATTR_RELAXED
)
12406 TRANSACTION_EXPR_RELAXED (stmt
) = 1;
12407 return add_stmt (stmt
);
12410 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12411 down to the element type of an array. */
12414 c_build_qualified_type (tree type
, int type_quals
)
12416 if (type
== error_mark_node
)
12419 if (TREE_CODE (type
) == ARRAY_TYPE
)
12422 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
12425 /* See if we already have an identically qualified type. */
12426 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
12428 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
12429 && TYPE_NAME (t
) == TYPE_NAME (type
)
12430 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
12431 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
12432 TYPE_ATTRIBUTES (type
)))
12437 tree domain
= TYPE_DOMAIN (type
);
12439 t
= build_variant_type_copy (type
);
12440 TREE_TYPE (t
) = element_type
;
12442 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
12443 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
12444 SET_TYPE_STRUCTURAL_EQUALITY (t
);
12445 else if (TYPE_CANONICAL (element_type
) != element_type
12446 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
12448 tree unqualified_canon
12449 = build_array_type (TYPE_CANONICAL (element_type
),
12450 domain
? TYPE_CANONICAL (domain
)
12453 = c_build_qualified_type (unqualified_canon
, type_quals
);
12456 TYPE_CANONICAL (t
) = t
;
12461 /* A restrict-qualified pointer type must be a pointer to object or
12462 incomplete type. Note that the use of POINTER_TYPE_P also allows
12463 REFERENCE_TYPEs, which is appropriate for C++. */
12464 if ((type_quals
& TYPE_QUAL_RESTRICT
)
12465 && (!POINTER_TYPE_P (type
)
12466 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
12468 error ("invalid use of %<restrict%>");
12469 type_quals
&= ~TYPE_QUAL_RESTRICT
;
12472 return build_qualified_type (type
, type_quals
);
12475 /* Build a VA_ARG_EXPR for the C parser. */
12478 c_build_va_arg (location_t loc
, tree expr
, tree type
)
12480 if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
12481 warning_at (loc
, OPT_Wc___compat
,
12482 "C++ requires promoted type, not enum type, in %<va_arg%>");
12483 return build_va_arg (loc
, expr
, type
);
12486 /* Return truthvalue of whether T1 is the same tree structure as T2.
12487 Return 1 if they are the same. Return 0 if they are different. */
12490 c_tree_equal (tree t1
, tree t2
)
12492 enum tree_code code1
, code2
;
12499 for (code1
= TREE_CODE (t1
);
12500 CONVERT_EXPR_CODE_P (code1
)
12501 || code1
== NON_LVALUE_EXPR
;
12502 code1
= TREE_CODE (t1
))
12503 t1
= TREE_OPERAND (t1
, 0);
12504 for (code2
= TREE_CODE (t2
);
12505 CONVERT_EXPR_CODE_P (code2
)
12506 || code2
== NON_LVALUE_EXPR
;
12507 code2
= TREE_CODE (t2
))
12508 t2
= TREE_OPERAND (t2
, 0);
12510 /* They might have become equal now. */
12514 if (code1
!= code2
)
12520 return wi::eq_p (t1
, t2
);
12523 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1
), TREE_REAL_CST (t2
));
12526 return TREE_STRING_LENGTH (t1
) == TREE_STRING_LENGTH (t2
)
12527 && !memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
12528 TREE_STRING_LENGTH (t1
));
12531 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1
),
12532 TREE_FIXED_CST (t2
));
12535 return c_tree_equal (TREE_REALPART (t1
), TREE_REALPART (t2
))
12536 && c_tree_equal (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
12539 return operand_equal_p (t1
, t2
, OEP_ONLY_CONST
);
12542 /* We need to do this when determining whether or not two
12543 non-type pointer to member function template arguments
12545 if (!comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))
12546 || CONSTRUCTOR_NELTS (t1
) != CONSTRUCTOR_NELTS (t2
))
12551 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, field
, value
)
12553 constructor_elt
*elt2
= CONSTRUCTOR_ELT (t2
, i
);
12554 if (!c_tree_equal (field
, elt2
->index
)
12555 || !c_tree_equal (value
, elt2
->value
))
12562 if (!c_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)))
12564 if (!c_tree_equal (TREE_VALUE (t1
), TREE_VALUE (t2
)))
12566 return c_tree_equal (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
12569 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
12574 call_expr_arg_iterator iter1
, iter2
;
12575 if (!c_tree_equal (CALL_EXPR_FN (t1
), CALL_EXPR_FN (t2
)))
12577 for (arg1
= first_call_expr_arg (t1
, &iter1
),
12578 arg2
= first_call_expr_arg (t2
, &iter2
);
12580 arg1
= next_call_expr_arg (&iter1
),
12581 arg2
= next_call_expr_arg (&iter2
))
12582 if (!c_tree_equal (arg1
, arg2
))
12591 tree o1
= TREE_OPERAND (t1
, 0);
12592 tree o2
= TREE_OPERAND (t2
, 0);
12594 /* Special case: if either target is an unallocated VAR_DECL,
12595 it means that it's going to be unified with whatever the
12596 TARGET_EXPR is really supposed to initialize, so treat it
12597 as being equivalent to anything. */
12598 if (TREE_CODE (o1
) == VAR_DECL
&& DECL_NAME (o1
) == NULL_TREE
12599 && !DECL_RTL_SET_P (o1
))
12601 else if (TREE_CODE (o2
) == VAR_DECL
&& DECL_NAME (o2
) == NULL_TREE
12602 && !DECL_RTL_SET_P (o2
))
12604 else if (!c_tree_equal (o1
, o2
))
12607 return c_tree_equal (TREE_OPERAND (t1
, 1), TREE_OPERAND (t2
, 1));
12610 case COMPONENT_REF
:
12611 if (TREE_OPERAND (t1
, 1) != TREE_OPERAND (t2
, 1))
12613 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
12619 case FUNCTION_DECL
:
12620 case IDENTIFIER_NODE
:
12627 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
12629 for (ix
= TREE_VEC_LENGTH (t1
); ix
--;)
12630 if (!c_tree_equal (TREE_VEC_ELT (t1
, ix
),
12631 TREE_VEC_ELT (t2
, ix
)))
12640 switch (TREE_CODE_CLASS (code1
))
12644 case tcc_comparison
:
12645 case tcc_expression
:
12647 case tcc_reference
:
12648 case tcc_statement
:
12650 int i
, n
= TREE_OPERAND_LENGTH (t1
);
12654 case PREINCREMENT_EXPR
:
12655 case PREDECREMENT_EXPR
:
12656 case POSTINCREMENT_EXPR
:
12657 case POSTDECREMENT_EXPR
:
12667 if (TREE_CODE_CLASS (code1
) == tcc_vl_exp
12668 && n
!= TREE_OPERAND_LENGTH (t2
))
12671 for (i
= 0; i
< n
; ++i
)
12672 if (!c_tree_equal (TREE_OPERAND (t1
, i
), TREE_OPERAND (t2
, i
)))
12679 return comptypes (t1
, t2
);
12681 gcc_unreachable ();
12683 /* We can get here with --disable-checking. */
12687 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12688 spawn-helper and BODY is the newly created body for FNDECL. */
12691 cilk_install_body_with_frame_cleanup (tree fndecl
, tree body
, void *w
)
12693 tree list
= alloc_stmt_list ();
12694 tree frame
= make_cilk_frame (fndecl
);
12695 tree dtor
= create_cilk_function_exit (frame
, false, true);
12696 add_local_decl (cfun
, frame
);
12698 DECL_SAVED_TREE (fndecl
) = list
;
12699 tree frame_ptr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (frame
)),
12701 tree body_list
= cilk_install_body_pedigree_operations (frame_ptr
);
12702 gcc_assert (TREE_CODE (body_list
) == STATEMENT_LIST
);
12704 tree detach_expr
= build_call_expr (cilk_detach_fndecl
, 1, frame_ptr
);
12705 append_to_statement_list (detach_expr
, &body_list
);
12707 cilk_outline (fndecl
, &body
, (struct wrapper_data
*) w
);
12708 body
= fold_build_cleanup_point_expr (void_type_node
, body
);
12710 append_to_statement_list (body
, &body_list
);
12711 append_to_statement_list (build_stmt (EXPR_LOCATION (body
), TRY_FINALLY_EXPR
,
12712 body_list
, dtor
), &list
);