1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
28 #include "coretypes.h"
31 #include "stor-layout.h"
32 #include "trans-mem.h"
35 #include "langhooks.h"
41 #include "tree-iterator.h"
43 #include "pointer-set.h"
44 #include "basic-block.h"
45 #include "gimple-expr.h"
47 #include "tree-inline.h"
49 #include "c-family/c-objc.h"
50 #include "c-family/c-common.h"
51 #include "c-family/c-ubsan.h"
54 /* Possible cases of implicit bad conversions. Used to select
55 diagnostic messages in convert_for_assignment. */
63 /* The level of nesting inside "__alignof__". */
66 /* The level of nesting inside "sizeof". */
69 /* The level of nesting inside "typeof". */
72 /* The argument of last parsed sizeof expression, only to be tested
73 if expr.original_code == SIZEOF_EXPR. */
74 tree c_last_sizeof_arg
;
76 /* Nonzero if we've already printed a "missing braces around initializer"
77 message within this initializer. */
78 static int missing_braces_mentioned
;
80 static int require_constant_value
;
81 static int require_constant_elements
;
83 static bool null_pointer_constant_p (const_tree
);
84 static tree
qualify_type (tree
, tree
);
85 static int tagged_types_tu_compatible_p (const_tree
, const_tree
, bool *,
87 static int comp_target_types (location_t
, tree
, tree
);
88 static int function_types_compatible_p (const_tree
, const_tree
, bool *,
90 static int type_lists_compatible_p (const_tree
, const_tree
, bool *, bool *);
91 static tree
lookup_field (tree
, tree
);
92 static int convert_arguments (location_t
, vec
<location_t
>, tree
,
93 vec
<tree
, va_gc
> *, vec
<tree
, va_gc
> *, tree
,
95 static tree
pointer_diff (location_t
, tree
, tree
);
96 static tree
convert_for_assignment (location_t
, location_t
, tree
, tree
, tree
,
97 enum impl_conv
, bool, tree
, tree
, int);
98 static tree
valid_compound_expr_initializer (tree
, tree
);
99 static void push_string (const char *);
100 static void push_member_name (tree
);
101 static int spelling_length (void);
102 static char *print_spelling (char *);
103 static void warning_init (int, const char *);
104 static tree
digest_init (location_t
, tree
, tree
, tree
, bool, bool, int);
105 static void output_init_element (tree
, tree
, bool, tree
, tree
, int, bool,
107 static void output_pending_init_elements (int, struct obstack
*);
108 static int set_designator (int, struct obstack
*);
109 static void push_range_stack (tree
, struct obstack
*);
110 static void add_pending_init (tree
, tree
, tree
, bool, struct obstack
*);
111 static void set_nonincremental_init (struct obstack
*);
112 static void set_nonincremental_init_from_string (tree
, struct obstack
*);
113 static tree
find_init_member (tree
, struct obstack
*);
114 static void readonly_warning (tree
, enum lvalue_use
);
115 static int lvalue_or_else (location_t
, const_tree
, enum lvalue_use
);
116 static void record_maybe_used_decl (tree
);
117 static int comptypes_internal (const_tree
, const_tree
, bool *, bool *);
119 /* Return true if EXP is a null pointer constant, false otherwise. */
122 null_pointer_constant_p (const_tree expr
)
124 /* This should really operate on c_expr structures, but they aren't
125 yet available everywhere required. */
126 tree type
= TREE_TYPE (expr
);
127 return (TREE_CODE (expr
) == INTEGER_CST
128 && !TREE_OVERFLOW (expr
)
129 && integer_zerop (expr
)
130 && (INTEGRAL_TYPE_P (type
)
131 || (TREE_CODE (type
) == POINTER_TYPE
132 && VOID_TYPE_P (TREE_TYPE (type
))
133 && TYPE_QUALS (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
)));
136 /* EXPR may appear in an unevaluated part of an integer constant
137 expression, but not in an evaluated part. Wrap it in a
138 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
139 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
142 note_integer_operands (tree expr
)
145 if (TREE_CODE (expr
) == INTEGER_CST
&& in_late_binary_op
)
147 ret
= copy_node (expr
);
148 TREE_OVERFLOW (ret
) = 1;
152 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL_TREE
, expr
);
153 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret
) = 1;
158 /* Having checked whether EXPR may appear in an unevaluated part of an
159 integer constant expression and found that it may, remove any
160 C_MAYBE_CONST_EXPR noting this fact and return the resulting
164 remove_c_maybe_const_expr (tree expr
)
166 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
167 return C_MAYBE_CONST_EXPR_EXPR (expr
);
172 \f/* This is a cache to hold if two types are compatible or not. */
174 struct tagged_tu_seen_cache
{
175 const struct tagged_tu_seen_cache
* next
;
178 /* The return value of tagged_types_tu_compatible_p if we had seen
179 these two types already. */
183 static const struct tagged_tu_seen_cache
* tagged_tu_seen_base
;
184 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*);
186 /* Do `exp = require_complete_type (exp);' to make sure exp
187 does not have an incomplete type. (That includes void types.) */
190 require_complete_type (tree value
)
192 tree type
= TREE_TYPE (value
);
194 if (value
== error_mark_node
|| type
== error_mark_node
)
195 return error_mark_node
;
197 /* First, detect a valid value with a complete type. */
198 if (COMPLETE_TYPE_P (type
))
201 c_incomplete_type_error (value
, type
);
202 return error_mark_node
;
205 /* Print an error message for invalid use of an incomplete type.
206 VALUE is the expression that was used (or 0 if that isn't known)
207 and TYPE is the type that was invalid. */
210 c_incomplete_type_error (const_tree value
, const_tree type
)
212 const char *type_code_string
;
214 /* Avoid duplicate error message. */
215 if (TREE_CODE (type
) == ERROR_MARK
)
218 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
219 || TREE_CODE (value
) == PARM_DECL
))
220 error ("%qD has an incomplete type", value
);
224 /* We must print an error message. Be clever about what it says. */
226 switch (TREE_CODE (type
))
229 type_code_string
= "struct";
233 type_code_string
= "union";
237 type_code_string
= "enum";
241 error ("invalid use of void expression");
245 if (TYPE_DOMAIN (type
))
247 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
249 error ("invalid use of flexible array member");
252 type
= TREE_TYPE (type
);
255 error ("invalid use of array with unspecified bounds");
262 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
263 error ("invalid use of undefined type %<%s %E%>",
264 type_code_string
, TYPE_NAME (type
));
266 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
267 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type
));
271 /* Given a type, apply default promotions wrt unnamed function
272 arguments and return the new type. */
275 c_type_promotes_to (tree type
)
277 tree ret
= NULL_TREE
;
279 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
280 ret
= double_type_node
;
281 else if (c_promoting_integer_type_p (type
))
283 /* Preserve unsignedness if not really getting any wider. */
284 if (TYPE_UNSIGNED (type
)
285 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
286 ret
= unsigned_type_node
;
288 ret
= integer_type_node
;
291 if (ret
!= NULL_TREE
)
292 return (TYPE_ATOMIC (type
)
293 ? c_build_qualified_type (ret
, TYPE_QUAL_ATOMIC
)
299 /* Return true if between two named address spaces, whether there is a superset
300 named address space that encompasses both address spaces. If there is a
301 superset, return which address space is the superset. */
304 addr_space_superset (addr_space_t as1
, addr_space_t as2
, addr_space_t
*common
)
311 else if (targetm
.addr_space
.subset_p (as1
, as2
))
316 else if (targetm
.addr_space
.subset_p (as2
, as1
))
325 /* Return a variant of TYPE which has all the type qualifiers of LIKE
326 as well as those of TYPE. */
329 qualify_type (tree type
, tree like
)
331 addr_space_t as_type
= TYPE_ADDR_SPACE (type
);
332 addr_space_t as_like
= TYPE_ADDR_SPACE (like
);
333 addr_space_t as_common
;
335 /* If the two named address spaces are different, determine the common
336 superset address space. If there isn't one, raise an error. */
337 if (!addr_space_superset (as_type
, as_like
, &as_common
))
340 error ("%qT and %qT are in disjoint named address spaces",
344 return c_build_qualified_type (type
,
345 TYPE_QUALS_NO_ADDR_SPACE (type
)
346 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like
)
347 | ENCODE_QUAL_ADDR_SPACE (as_common
));
350 /* Return true iff the given tree T is a variable length array. */
353 c_vla_type_p (const_tree t
)
355 if (TREE_CODE (t
) == ARRAY_TYPE
356 && C_TYPE_VARIABLE_SIZE (t
))
361 /* Return the composite type of two compatible types.
363 We assume that comptypes has already been done and returned
364 nonzero; if that isn't so, this may crash. In particular, we
365 assume that qualifiers match. */
368 composite_type (tree t1
, tree t2
)
370 enum tree_code code1
;
371 enum tree_code code2
;
374 /* Save time if the two types are the same. */
376 if (t1
== t2
) return t1
;
378 /* If one type is nonsense, use the other. */
379 if (t1
== error_mark_node
)
381 if (t2
== error_mark_node
)
384 code1
= TREE_CODE (t1
);
385 code2
= TREE_CODE (t2
);
387 /* Merge the attributes. */
388 attributes
= targetm
.merge_type_attributes (t1
, t2
);
390 /* If one is an enumerated type and the other is the compatible
391 integer type, the composite type might be either of the two
392 (DR#013 question 3). For consistency, use the enumerated type as
393 the composite type. */
395 if (code1
== ENUMERAL_TYPE
&& code2
== INTEGER_TYPE
)
397 if (code2
== ENUMERAL_TYPE
&& code1
== INTEGER_TYPE
)
400 gcc_assert (code1
== code2
);
405 /* For two pointers, do this recursively on the target type. */
407 tree pointed_to_1
= TREE_TYPE (t1
);
408 tree pointed_to_2
= TREE_TYPE (t2
);
409 tree target
= composite_type (pointed_to_1
, pointed_to_2
);
410 t1
= build_pointer_type_for_mode (target
, TYPE_MODE (t1
), false);
411 t1
= build_type_attribute_variant (t1
, attributes
);
412 return qualify_type (t1
, t2
);
417 tree elt
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
420 tree d1
= TYPE_DOMAIN (t1
);
421 tree d2
= TYPE_DOMAIN (t2
);
422 bool d1_variable
, d2_variable
;
423 bool d1_zero
, d2_zero
;
424 bool t1_complete
, t2_complete
;
426 /* We should not have any type quals on arrays at all. */
427 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1
)
428 && !TYPE_QUALS_NO_ADDR_SPACE (t2
));
430 t1_complete
= COMPLETE_TYPE_P (t1
);
431 t2_complete
= COMPLETE_TYPE_P (t2
);
433 d1_zero
= d1
== 0 || !TYPE_MAX_VALUE (d1
);
434 d2_zero
= d2
== 0 || !TYPE_MAX_VALUE (d2
);
436 d1_variable
= (!d1_zero
437 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
438 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
439 d2_variable
= (!d2_zero
440 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
441 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
442 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
443 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
445 /* Save space: see if the result is identical to one of the args. */
446 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
)
447 && (d2_variable
|| d2_zero
|| !d1_variable
))
448 return build_type_attribute_variant (t1
, attributes
);
449 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
)
450 && (d1_variable
|| d1_zero
|| !d2_variable
))
451 return build_type_attribute_variant (t2
, attributes
);
453 if (elt
== TREE_TYPE (t1
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
454 return build_type_attribute_variant (t1
, attributes
);
455 if (elt
== TREE_TYPE (t2
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
456 return build_type_attribute_variant (t2
, attributes
);
458 /* Merge the element types, and have a size if either arg has
459 one. We may have qualifiers on the element types. To set
460 up TYPE_MAIN_VARIANT correctly, we need to form the
461 composite of the unqualified types and add the qualifiers
463 quals
= TYPE_QUALS (strip_array_types (elt
));
464 unqual_elt
= c_build_qualified_type (elt
, TYPE_UNQUALIFIED
);
465 t1
= build_array_type (unqual_elt
,
466 TYPE_DOMAIN ((TYPE_DOMAIN (t1
)
472 /* Ensure a composite type involving a zero-length array type
473 is a zero-length type not an incomplete type. */
474 if (d1_zero
&& d2_zero
475 && (t1_complete
|| t2_complete
)
476 && !COMPLETE_TYPE_P (t1
))
478 TYPE_SIZE (t1
) = bitsize_zero_node
;
479 TYPE_SIZE_UNIT (t1
) = size_zero_node
;
481 t1
= c_build_qualified_type (t1
, quals
);
482 return build_type_attribute_variant (t1
, attributes
);
488 if (attributes
!= NULL
)
490 /* Try harder not to create a new aggregate type. */
491 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
493 if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
496 return build_type_attribute_variant (t1
, attributes
);
499 /* Function types: prefer the one that specified arg types.
500 If both do, merge the arg types. Also merge the return types. */
502 tree valtype
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
503 tree p1
= TYPE_ARG_TYPES (t1
);
504 tree p2
= TYPE_ARG_TYPES (t2
);
509 /* Save space: see if the result is identical to one of the args. */
510 if (valtype
== TREE_TYPE (t1
) && !TYPE_ARG_TYPES (t2
))
511 return build_type_attribute_variant (t1
, attributes
);
512 if (valtype
== TREE_TYPE (t2
) && !TYPE_ARG_TYPES (t1
))
513 return build_type_attribute_variant (t2
, attributes
);
515 /* Simple way if one arg fails to specify argument types. */
516 if (TYPE_ARG_TYPES (t1
) == 0)
518 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
519 t1
= build_type_attribute_variant (t1
, attributes
);
520 return qualify_type (t1
, t2
);
522 if (TYPE_ARG_TYPES (t2
) == 0)
524 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
525 t1
= build_type_attribute_variant (t1
, attributes
);
526 return qualify_type (t1
, t2
);
529 /* If both args specify argument types, we must merge the two
530 lists, argument by argument. */
532 len
= list_length (p1
);
535 for (i
= 0; i
< len
; i
++)
536 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
541 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
543 /* A null type means arg type is not specified.
544 Take whatever the other function type has. */
545 if (TREE_VALUE (p1
) == 0)
547 TREE_VALUE (n
) = TREE_VALUE (p2
);
550 if (TREE_VALUE (p2
) == 0)
552 TREE_VALUE (n
) = TREE_VALUE (p1
);
556 /* Given wait (union {union wait *u; int *i} *)
557 and wait (union wait *),
558 prefer union wait * as type of parm. */
559 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
560 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
563 tree mv2
= TREE_VALUE (p2
);
564 if (mv2
&& mv2
!= error_mark_node
565 && TREE_CODE (mv2
) != ARRAY_TYPE
)
566 mv2
= TYPE_MAIN_VARIANT (mv2
);
567 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
568 memb
; memb
= DECL_CHAIN (memb
))
570 tree mv3
= TREE_TYPE (memb
);
571 if (mv3
&& mv3
!= error_mark_node
572 && TREE_CODE (mv3
) != ARRAY_TYPE
)
573 mv3
= TYPE_MAIN_VARIANT (mv3
);
574 if (comptypes (mv3
, mv2
))
576 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
578 pedwarn (input_location
, OPT_Wpedantic
,
579 "function types not truly compatible in ISO C");
584 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
585 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
588 tree mv1
= TREE_VALUE (p1
);
589 if (mv1
&& mv1
!= error_mark_node
590 && TREE_CODE (mv1
) != ARRAY_TYPE
)
591 mv1
= TYPE_MAIN_VARIANT (mv1
);
592 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
593 memb
; memb
= DECL_CHAIN (memb
))
595 tree mv3
= TREE_TYPE (memb
);
596 if (mv3
&& mv3
!= error_mark_node
597 && TREE_CODE (mv3
) != ARRAY_TYPE
)
598 mv3
= TYPE_MAIN_VARIANT (mv3
);
599 if (comptypes (mv3
, mv1
))
601 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
603 pedwarn (input_location
, OPT_Wpedantic
,
604 "function types not truly compatible in ISO C");
609 TREE_VALUE (n
) = composite_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
613 t1
= build_function_type (valtype
, newargs
);
614 t1
= qualify_type (t1
, t2
);
615 /* ... falls through ... */
619 return build_type_attribute_variant (t1
, attributes
);
624 /* Return the type of a conditional expression between pointers to
625 possibly differently qualified versions of compatible types.
627 We assume that comp_target_types has already been done and returned
628 nonzero; if that isn't so, this may crash. */
631 common_pointer_type (tree t1
, tree t2
)
634 tree pointed_to_1
, mv1
;
635 tree pointed_to_2
, mv2
;
637 unsigned target_quals
;
638 addr_space_t as1
, as2
, as_common
;
641 /* Save time if the two types are the same. */
643 if (t1
== t2
) return t1
;
645 /* If one type is nonsense, use the other. */
646 if (t1
== error_mark_node
)
648 if (t2
== error_mark_node
)
651 gcc_assert (TREE_CODE (t1
) == POINTER_TYPE
652 && TREE_CODE (t2
) == POINTER_TYPE
);
654 /* Merge the attributes. */
655 attributes
= targetm
.merge_type_attributes (t1
, t2
);
657 /* Find the composite type of the target types, and combine the
658 qualifiers of the two types' targets. Do not lose qualifiers on
659 array element types by taking the TYPE_MAIN_VARIANT. */
660 mv1
= pointed_to_1
= TREE_TYPE (t1
);
661 mv2
= pointed_to_2
= TREE_TYPE (t2
);
662 if (TREE_CODE (mv1
) != ARRAY_TYPE
)
663 mv1
= TYPE_MAIN_VARIANT (pointed_to_1
);
664 if (TREE_CODE (mv2
) != ARRAY_TYPE
)
665 mv2
= TYPE_MAIN_VARIANT (pointed_to_2
);
666 target
= composite_type (mv1
, mv2
);
668 /* For function types do not merge const qualifiers, but drop them
669 if used inconsistently. The middle-end uses these to mark const
670 and noreturn functions. */
671 quals1
= TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1
);
672 quals2
= TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2
);
674 if (TREE_CODE (pointed_to_1
) == FUNCTION_TYPE
)
675 target_quals
= (quals1
& quals2
);
677 target_quals
= (quals1
| quals2
);
679 /* If the two named address spaces are different, determine the common
680 superset address space. This is guaranteed to exist due to the
681 assumption that comp_target_type returned non-zero. */
682 as1
= TYPE_ADDR_SPACE (pointed_to_1
);
683 as2
= TYPE_ADDR_SPACE (pointed_to_2
);
684 if (!addr_space_superset (as1
, as2
, &as_common
))
687 target_quals
|= ENCODE_QUAL_ADDR_SPACE (as_common
);
689 t1
= build_pointer_type (c_build_qualified_type (target
, target_quals
));
690 return build_type_attribute_variant (t1
, attributes
);
693 /* Return the common type for two arithmetic types under the usual
694 arithmetic conversions. The default conversions have already been
695 applied, and enumerated types converted to their compatible integer
696 types. The resulting type is unqualified and has no attributes.
698 This is the type for the result of most arithmetic operations
699 if the operands have the given two types. */
702 c_common_type (tree t1
, tree t2
)
704 enum tree_code code1
;
705 enum tree_code code2
;
707 /* If one type is nonsense, use the other. */
708 if (t1
== error_mark_node
)
710 if (t2
== error_mark_node
)
713 if (TYPE_QUALS (t1
) != TYPE_UNQUALIFIED
)
714 t1
= TYPE_MAIN_VARIANT (t1
);
716 if (TYPE_QUALS (t2
) != TYPE_UNQUALIFIED
)
717 t2
= TYPE_MAIN_VARIANT (t2
);
719 if (TYPE_ATTRIBUTES (t1
) != NULL_TREE
)
720 t1
= build_type_attribute_variant (t1
, NULL_TREE
);
722 if (TYPE_ATTRIBUTES (t2
) != NULL_TREE
)
723 t2
= build_type_attribute_variant (t2
, NULL_TREE
);
725 /* Save time if the two types are the same. */
727 if (t1
== t2
) return t1
;
729 code1
= TREE_CODE (t1
);
730 code2
= TREE_CODE (t2
);
732 gcc_assert (code1
== VECTOR_TYPE
|| code1
== COMPLEX_TYPE
733 || code1
== FIXED_POINT_TYPE
|| code1
== REAL_TYPE
734 || code1
== INTEGER_TYPE
);
735 gcc_assert (code2
== VECTOR_TYPE
|| code2
== COMPLEX_TYPE
736 || code2
== FIXED_POINT_TYPE
|| code2
== REAL_TYPE
737 || code2
== INTEGER_TYPE
);
739 /* When one operand is a decimal float type, the other operand cannot be
740 a generic float type or a complex type. We also disallow vector types
742 if ((DECIMAL_FLOAT_TYPE_P (t1
) || DECIMAL_FLOAT_TYPE_P (t2
))
743 && !(DECIMAL_FLOAT_TYPE_P (t1
) && DECIMAL_FLOAT_TYPE_P (t2
)))
745 if (code1
== VECTOR_TYPE
|| code2
== VECTOR_TYPE
)
747 error ("can%'t mix operands of decimal float and vector types");
748 return error_mark_node
;
750 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
752 error ("can%'t mix operands of decimal float and complex types");
753 return error_mark_node
;
755 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
757 error ("can%'t mix operands of decimal float and other float types");
758 return error_mark_node
;
762 /* If one type is a vector type, return that type. (How the usual
763 arithmetic conversions apply to the vector types extension is not
764 precisely specified.) */
765 if (code1
== VECTOR_TYPE
)
768 if (code2
== VECTOR_TYPE
)
771 /* If one type is complex, form the common type of the non-complex
772 components, then make that complex. Use T1 or T2 if it is the
774 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
776 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
777 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
778 tree subtype
= c_common_type (subtype1
, subtype2
);
780 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
782 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
785 return build_complex_type (subtype
);
788 /* If only one is real, use it as the result. */
790 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
793 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
796 /* If both are real and either are decimal floating point types, use
797 the decimal floating point type with the greater precision. */
799 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
801 if (TYPE_MAIN_VARIANT (t1
) == dfloat128_type_node
802 || TYPE_MAIN_VARIANT (t2
) == dfloat128_type_node
)
803 return dfloat128_type_node
;
804 else if (TYPE_MAIN_VARIANT (t1
) == dfloat64_type_node
805 || TYPE_MAIN_VARIANT (t2
) == dfloat64_type_node
)
806 return dfloat64_type_node
;
807 else if (TYPE_MAIN_VARIANT (t1
) == dfloat32_type_node
808 || TYPE_MAIN_VARIANT (t2
) == dfloat32_type_node
)
809 return dfloat32_type_node
;
812 /* Deal with fixed-point types. */
813 if (code1
== FIXED_POINT_TYPE
|| code2
== FIXED_POINT_TYPE
)
815 unsigned int unsignedp
= 0, satp
= 0;
816 enum machine_mode m1
, m2
;
817 unsigned int fbit1
, ibit1
, fbit2
, ibit2
, max_fbit
, max_ibit
;
822 /* If one input type is saturating, the result type is saturating. */
823 if (TYPE_SATURATING (t1
) || TYPE_SATURATING (t2
))
826 /* If both fixed-point types are unsigned, the result type is unsigned.
827 When mixing fixed-point and integer types, follow the sign of the
829 Otherwise, the result type is signed. */
830 if ((TYPE_UNSIGNED (t1
) && TYPE_UNSIGNED (t2
)
831 && code1
== FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
)
832 || (code1
== FIXED_POINT_TYPE
&& code2
!= FIXED_POINT_TYPE
833 && TYPE_UNSIGNED (t1
))
834 || (code1
!= FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
835 && TYPE_UNSIGNED (t2
)))
838 /* The result type is signed. */
841 /* If the input type is unsigned, we need to convert to the
843 if (code1
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t1
))
845 enum mode_class mclass
= (enum mode_class
) 0;
846 if (GET_MODE_CLASS (m1
) == MODE_UFRACT
)
848 else if (GET_MODE_CLASS (m1
) == MODE_UACCUM
)
852 m1
= mode_for_size (GET_MODE_PRECISION (m1
), mclass
, 0);
854 if (code2
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t2
))
856 enum mode_class mclass
= (enum mode_class
) 0;
857 if (GET_MODE_CLASS (m2
) == MODE_UFRACT
)
859 else if (GET_MODE_CLASS (m2
) == MODE_UACCUM
)
863 m2
= mode_for_size (GET_MODE_PRECISION (m2
), mclass
, 0);
867 if (code1
== FIXED_POINT_TYPE
)
869 fbit1
= GET_MODE_FBIT (m1
);
870 ibit1
= GET_MODE_IBIT (m1
);
875 /* Signed integers need to subtract one sign bit. */
876 ibit1
= TYPE_PRECISION (t1
) - (!TYPE_UNSIGNED (t1
));
879 if (code2
== FIXED_POINT_TYPE
)
881 fbit2
= GET_MODE_FBIT (m2
);
882 ibit2
= GET_MODE_IBIT (m2
);
887 /* Signed integers need to subtract one sign bit. */
888 ibit2
= TYPE_PRECISION (t2
) - (!TYPE_UNSIGNED (t2
));
891 max_ibit
= ibit1
>= ibit2
? ibit1
: ibit2
;
892 max_fbit
= fbit1
>= fbit2
? fbit1
: fbit2
;
893 return c_common_fixed_point_type_for_size (max_ibit
, max_fbit
, unsignedp
,
897 /* Both real or both integers; use the one with greater precision. */
899 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
901 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
904 /* Same precision. Prefer long longs to longs to ints when the
905 same precision, following the C99 rules on integer type rank
906 (which are equivalent to the C90 rules for C90 types). */
908 if (TYPE_MAIN_VARIANT (t1
) == long_long_unsigned_type_node
909 || TYPE_MAIN_VARIANT (t2
) == long_long_unsigned_type_node
)
910 return long_long_unsigned_type_node
;
912 if (TYPE_MAIN_VARIANT (t1
) == long_long_integer_type_node
913 || TYPE_MAIN_VARIANT (t2
) == long_long_integer_type_node
)
915 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
916 return long_long_unsigned_type_node
;
918 return long_long_integer_type_node
;
921 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
922 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
923 return long_unsigned_type_node
;
925 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
926 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
928 /* But preserve unsignedness from the other type,
929 since long cannot hold all the values of an unsigned int. */
930 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
931 return long_unsigned_type_node
;
933 return long_integer_type_node
;
936 /* Likewise, prefer long double to double even if same size. */
937 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
938 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
939 return long_double_type_node
;
941 /* Likewise, prefer double to float even if same size.
942 We got a couple of embedded targets with 32 bit doubles, and the
943 pdp11 might have 64 bit floats. */
944 if (TYPE_MAIN_VARIANT (t1
) == double_type_node
945 || TYPE_MAIN_VARIANT (t2
) == double_type_node
)
946 return double_type_node
;
948 /* Otherwise prefer the unsigned one. */
950 if (TYPE_UNSIGNED (t1
))
956 /* Wrapper around c_common_type that is used by c-common.c and other
957 front end optimizations that remove promotions. ENUMERAL_TYPEs
958 are allowed here and are converted to their compatible integer types.
959 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
960 preferably a non-Boolean type as the common type. */
962 common_type (tree t1
, tree t2
)
964 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
965 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
966 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
967 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
969 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
970 if (TREE_CODE (t1
) == BOOLEAN_TYPE
971 && TREE_CODE (t2
) == BOOLEAN_TYPE
)
972 return boolean_type_node
;
974 /* If either type is BOOLEAN_TYPE, then return the other. */
975 if (TREE_CODE (t1
) == BOOLEAN_TYPE
)
977 if (TREE_CODE (t2
) == BOOLEAN_TYPE
)
980 return c_common_type (t1
, t2
);
983 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
984 or various other operations. Return 2 if they are compatible
985 but a warning may be needed if you use them together. */
988 comptypes (tree type1
, tree type2
)
990 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
993 val
= comptypes_internal (type1
, type2
, NULL
, NULL
);
994 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
999 /* Like comptypes, but if it returns non-zero because enum and int are
1000 compatible, it sets *ENUM_AND_INT_P to true. */
1003 comptypes_check_enum_int (tree type1
, tree type2
, bool *enum_and_int_p
)
1005 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1008 val
= comptypes_internal (type1
, type2
, enum_and_int_p
, NULL
);
1009 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1014 /* Like comptypes, but if it returns nonzero for different types, it
1015 sets *DIFFERENT_TYPES_P to true. */
1018 comptypes_check_different_types (tree type1
, tree type2
,
1019 bool *different_types_p
)
1021 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1024 val
= comptypes_internal (type1
, type2
, NULL
, different_types_p
);
1025 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1030 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1031 or various other operations. Return 2 if they are compatible
1032 but a warning may be needed if you use them together. If
1033 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1034 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1035 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1036 NULL, and the types are compatible but different enough not to be
1037 permitted in C11 typedef redeclarations, then this sets
1038 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1039 false, but may or may not be set if the types are incompatible.
1040 This differs from comptypes, in that we don't free the seen
1044 comptypes_internal (const_tree type1
, const_tree type2
, bool *enum_and_int_p
,
1045 bool *different_types_p
)
1047 const_tree t1
= type1
;
1048 const_tree t2
= type2
;
1051 /* Suppress errors caused by previously reported errors. */
1053 if (t1
== t2
|| !t1
|| !t2
1054 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
1057 /* Enumerated types are compatible with integer types, but this is
1058 not transitive: two enumerated types in the same translation unit
1059 are compatible with each other only if they are the same type. */
1061 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
1063 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TYPE_UNSIGNED (t1
));
1064 if (TREE_CODE (t2
) != VOID_TYPE
)
1066 if (enum_and_int_p
!= NULL
)
1067 *enum_and_int_p
= true;
1068 if (different_types_p
!= NULL
)
1069 *different_types_p
= true;
1072 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
1074 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TYPE_UNSIGNED (t2
));
1075 if (TREE_CODE (t1
) != VOID_TYPE
)
1077 if (enum_and_int_p
!= NULL
)
1078 *enum_and_int_p
= true;
1079 if (different_types_p
!= NULL
)
1080 *different_types_p
= true;
1087 /* Different classes of types can't be compatible. */
1089 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1092 /* Qualifiers must match. C99 6.7.3p9 */
1094 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1097 /* Allow for two different type nodes which have essentially the same
1098 definition. Note that we already checked for equality of the type
1099 qualifiers (just above). */
1101 if (TREE_CODE (t1
) != ARRAY_TYPE
1102 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1105 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1106 if (!(attrval
= comp_type_attributes (t1
, t2
)))
1109 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1112 switch (TREE_CODE (t1
))
1115 /* Do not remove mode or aliasing information. */
1116 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1117 || TYPE_REF_CAN_ALIAS_ALL (t1
) != TYPE_REF_CAN_ALIAS_ALL (t2
))
1119 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
1120 ? 1 : comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1121 enum_and_int_p
, different_types_p
));
1125 val
= function_types_compatible_p (t1
, t2
, enum_and_int_p
,
1131 tree d1
= TYPE_DOMAIN (t1
);
1132 tree d2
= TYPE_DOMAIN (t2
);
1133 bool d1_variable
, d2_variable
;
1134 bool d1_zero
, d2_zero
;
1137 /* Target types must match incl. qualifiers. */
1138 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
1139 && 0 == (val
= comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1141 different_types_p
)))
1144 if (different_types_p
!= NULL
1145 && (d1
== 0) != (d2
== 0))
1146 *different_types_p
= true;
1147 /* Sizes must match unless one is missing or variable. */
1148 if (d1
== 0 || d2
== 0 || d1
== d2
)
1151 d1_zero
= !TYPE_MAX_VALUE (d1
);
1152 d2_zero
= !TYPE_MAX_VALUE (d2
);
1154 d1_variable
= (!d1_zero
1155 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
1156 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
1157 d2_variable
= (!d2_zero
1158 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
1159 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
1160 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
1161 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
1163 if (different_types_p
!= NULL
1164 && d1_variable
!= d2_variable
)
1165 *different_types_p
= true;
1166 if (d1_variable
|| d2_variable
)
1168 if (d1_zero
&& d2_zero
)
1170 if (d1_zero
|| d2_zero
1171 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
1172 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
1181 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
1183 tree a1
= TYPE_ATTRIBUTES (t1
);
1184 tree a2
= TYPE_ATTRIBUTES (t2
);
1186 if (! attribute_list_contained (a1
, a2
)
1187 && ! attribute_list_contained (a2
, a1
))
1191 return tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1193 val
= tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1199 val
= (TYPE_VECTOR_SUBPARTS (t1
) == TYPE_VECTOR_SUBPARTS (t2
)
1200 && comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1201 enum_and_int_p
, different_types_p
));
1207 return attrval
== 2 && val
== 1 ? 2 : val
;
1210 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1211 their qualifiers, except for named address spaces. If the pointers point to
1212 different named addresses, then we must determine if one address space is a
1213 subset of the other. */
1216 comp_target_types (location_t location
, tree ttl
, tree ttr
)
1219 tree mvl
= TREE_TYPE (ttl
);
1220 tree mvr
= TREE_TYPE (ttr
);
1221 addr_space_t asl
= TYPE_ADDR_SPACE (mvl
);
1222 addr_space_t asr
= TYPE_ADDR_SPACE (mvr
);
1223 addr_space_t as_common
;
1224 bool enum_and_int_p
;
1226 /* Fail if pointers point to incompatible address spaces. */
1227 if (!addr_space_superset (asl
, asr
, &as_common
))
1230 /* Do not lose qualifiers on element types of array types that are
1231 pointer targets by taking their TYPE_MAIN_VARIANT. */
1232 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
1233 mvl
= (TYPE_ATOMIC (mvl
)
1234 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
), TYPE_QUAL_ATOMIC
)
1235 : TYPE_MAIN_VARIANT (mvl
));
1236 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
1237 mvr
= (TYPE_ATOMIC (mvr
)
1238 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
), TYPE_QUAL_ATOMIC
)
1239 : TYPE_MAIN_VARIANT (mvr
));
1240 enum_and_int_p
= false;
1241 val
= comptypes_check_enum_int (mvl
, mvr
, &enum_and_int_p
);
1244 pedwarn (location
, OPT_Wpedantic
, "types are not quite compatible");
1246 if (val
== 1 && enum_and_int_p
&& warn_cxx_compat
)
1247 warning_at (location
, OPT_Wc___compat
,
1248 "pointer target types incompatible in C++");
1253 /* Subroutines of `comptypes'. */
1255 /* Determine whether two trees derive from the same translation unit.
1256 If the CONTEXT chain ends in a null, that tree's context is still
1257 being parsed, so if two trees have context chains ending in null,
1258 they're in the same translation unit. */
1260 same_translation_unit_p (const_tree t1
, const_tree t2
)
1262 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
1263 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
1265 case tcc_declaration
:
1266 t1
= DECL_CONTEXT (t1
); break;
1268 t1
= TYPE_CONTEXT (t1
); break;
1269 case tcc_exceptional
:
1270 t1
= BLOCK_SUPERCONTEXT (t1
); break; /* assume block */
1271 default: gcc_unreachable ();
1274 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
1275 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
1277 case tcc_declaration
:
1278 t2
= DECL_CONTEXT (t2
); break;
1280 t2
= TYPE_CONTEXT (t2
); break;
1281 case tcc_exceptional
:
1282 t2
= BLOCK_SUPERCONTEXT (t2
); break; /* assume block */
1283 default: gcc_unreachable ();
1289 /* Allocate the seen two types, assuming that they are compatible. */
1291 static struct tagged_tu_seen_cache
*
1292 alloc_tagged_tu_seen_cache (const_tree t1
, const_tree t2
)
1294 struct tagged_tu_seen_cache
*tu
= XNEW (struct tagged_tu_seen_cache
);
1295 tu
->next
= tagged_tu_seen_base
;
1299 tagged_tu_seen_base
= tu
;
1301 /* The C standard says that two structures in different translation
1302 units are compatible with each other only if the types of their
1303 fields are compatible (among other things). We assume that they
1304 are compatible until proven otherwise when building the cache.
1305 An example where this can occur is:
1310 If we are comparing this against a similar struct in another TU,
1311 and did not assume they were compatible, we end up with an infinite
1317 /* Free the seen types until we get to TU_TIL. */
1320 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*tu_til
)
1322 const struct tagged_tu_seen_cache
*tu
= tagged_tu_seen_base
;
1323 while (tu
!= tu_til
)
1325 const struct tagged_tu_seen_cache
*const tu1
1326 = (const struct tagged_tu_seen_cache
*) tu
;
1328 free (CONST_CAST (struct tagged_tu_seen_cache
*, tu1
));
1330 tagged_tu_seen_base
= tu_til
;
1333 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1334 compatible. If the two types are not the same (which has been
1335 checked earlier), this can only happen when multiple translation
1336 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1337 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1338 comptypes_internal. */
1341 tagged_types_tu_compatible_p (const_tree t1
, const_tree t2
,
1342 bool *enum_and_int_p
, bool *different_types_p
)
1345 bool needs_warning
= false;
1347 /* We have to verify that the tags of the types are the same. This
1348 is harder than it looks because this may be a typedef, so we have
1349 to go look at the original type. It may even be a typedef of a
1351 In the case of compiler-created builtin structs the TYPE_DECL
1352 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1353 while (TYPE_NAME (t1
)
1354 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1355 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
1356 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
1358 while (TYPE_NAME (t2
)
1359 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1360 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
1361 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
1363 /* C90 didn't have the requirement that the two tags be the same. */
1364 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
1367 /* C90 didn't say what happened if one or both of the types were
1368 incomplete; we choose to follow C99 rules here, which is that they
1370 if (TYPE_SIZE (t1
) == NULL
1371 || TYPE_SIZE (t2
) == NULL
)
1375 const struct tagged_tu_seen_cache
* tts_i
;
1376 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
1377 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
1381 switch (TREE_CODE (t1
))
1385 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1386 /* Speed up the case where the type values are in the same order. */
1387 tree tv1
= TYPE_VALUES (t1
);
1388 tree tv2
= TYPE_VALUES (t2
);
1395 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
1397 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
1399 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
1406 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
1410 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
1416 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
1422 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
1424 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
1426 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
1437 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1438 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
1444 /* Speed up the common case where the fields are in the same order. */
1445 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
); s1
&& s2
;
1446 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1450 if (DECL_NAME (s1
) != DECL_NAME (s2
))
1452 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1453 enum_and_int_p
, different_types_p
);
1455 if (result
!= 1 && !DECL_NAME (s1
))
1463 needs_warning
= true;
1465 if (TREE_CODE (s1
) == FIELD_DECL
1466 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1467 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1475 tu
->val
= needs_warning
? 2 : 1;
1479 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= DECL_CHAIN (s1
))
1483 for (s2
= TYPE_FIELDS (t2
); s2
; s2
= DECL_CHAIN (s2
))
1484 if (DECL_NAME (s1
) == DECL_NAME (s2
))
1488 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1492 if (result
!= 1 && !DECL_NAME (s1
))
1500 needs_warning
= true;
1502 if (TREE_CODE (s1
) == FIELD_DECL
1503 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1504 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1516 tu
->val
= needs_warning
? 2 : 10;
1522 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1524 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
1526 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1529 if (TREE_CODE (s1
) != TREE_CODE (s2
)
1530 || DECL_NAME (s1
) != DECL_NAME (s2
))
1532 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1533 enum_and_int_p
, different_types_p
);
1537 needs_warning
= true;
1539 if (TREE_CODE (s1
) == FIELD_DECL
1540 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1541 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1547 tu
->val
= needs_warning
? 2 : 1;
1556 /* Return 1 if two function types F1 and F2 are compatible.
1557 If either type specifies no argument types,
1558 the other must specify a fixed number of self-promoting arg types.
1559 Otherwise, if one type specifies only the number of arguments,
1560 the other must specify that number of self-promoting arg types.
1561 Otherwise, the argument types must match.
1562 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1565 function_types_compatible_p (const_tree f1
, const_tree f2
,
1566 bool *enum_and_int_p
, bool *different_types_p
)
1569 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1574 ret1
= TREE_TYPE (f1
);
1575 ret2
= TREE_TYPE (f2
);
1577 /* 'volatile' qualifiers on a function's return type used to mean
1578 the function is noreturn. */
1579 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
1580 pedwarn (input_location
, 0, "function return types not compatible due to %<volatile%>");
1581 if (TYPE_VOLATILE (ret1
))
1582 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
1583 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
1584 if (TYPE_VOLATILE (ret2
))
1585 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
1586 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
1587 val
= comptypes_internal (ret1
, ret2
, enum_and_int_p
, different_types_p
);
1591 args1
= TYPE_ARG_TYPES (f1
);
1592 args2
= TYPE_ARG_TYPES (f2
);
1594 if (different_types_p
!= NULL
1595 && (args1
== 0) != (args2
== 0))
1596 *different_types_p
= true;
1598 /* An unspecified parmlist matches any specified parmlist
1599 whose argument types don't need default promotions. */
1603 if (!self_promoting_args_p (args2
))
1605 /* If one of these types comes from a non-prototype fn definition,
1606 compare that with the other type's arglist.
1607 If they don't match, ask for a warning (but no error). */
1608 if (TYPE_ACTUAL_ARG_TYPES (f1
)
1609 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
1610 enum_and_int_p
, different_types_p
))
1616 if (!self_promoting_args_p (args1
))
1618 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1619 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
1620 enum_and_int_p
, different_types_p
))
1625 /* Both types have argument lists: compare them and propagate results. */
1626 val1
= type_lists_compatible_p (args1
, args2
, enum_and_int_p
,
1628 return val1
!= 1 ? val1
: val
;
1631 /* Check two lists of types for compatibility, returning 0 for
1632 incompatible, 1 for compatible, or 2 for compatible with
1633 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1634 comptypes_internal. */
1637 type_lists_compatible_p (const_tree args1
, const_tree args2
,
1638 bool *enum_and_int_p
, bool *different_types_p
)
1640 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1646 tree a1
, mv1
, a2
, mv2
;
1647 if (args1
== 0 && args2
== 0)
1649 /* If one list is shorter than the other,
1650 they fail to match. */
1651 if (args1
== 0 || args2
== 0)
1653 mv1
= a1
= TREE_VALUE (args1
);
1654 mv2
= a2
= TREE_VALUE (args2
);
1655 if (mv1
&& mv1
!= error_mark_node
&& TREE_CODE (mv1
) != ARRAY_TYPE
)
1656 mv1
= (TYPE_ATOMIC (mv1
)
1657 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1
),
1659 : TYPE_MAIN_VARIANT (mv1
));
1660 if (mv2
&& mv2
!= error_mark_node
&& TREE_CODE (mv2
) != ARRAY_TYPE
)
1661 mv2
= (TYPE_ATOMIC (mv2
)
1662 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2
),
1664 : TYPE_MAIN_VARIANT (mv2
));
1665 /* A null pointer instead of a type
1666 means there is supposed to be an argument
1667 but nothing is specified about what type it has.
1668 So match anything that self-promotes. */
1669 if (different_types_p
!= NULL
1670 && (a1
== 0) != (a2
== 0))
1671 *different_types_p
= true;
1674 if (c_type_promotes_to (a2
) != a2
)
1679 if (c_type_promotes_to (a1
) != a1
)
1682 /* If one of the lists has an error marker, ignore this arg. */
1683 else if (TREE_CODE (a1
) == ERROR_MARK
1684 || TREE_CODE (a2
) == ERROR_MARK
)
1686 else if (!(newval
= comptypes_internal (mv1
, mv2
, enum_and_int_p
,
1687 different_types_p
)))
1689 if (different_types_p
!= NULL
)
1690 *different_types_p
= true;
1691 /* Allow wait (union {union wait *u; int *i} *)
1692 and wait (union wait *) to be compatible. */
1693 if (TREE_CODE (a1
) == UNION_TYPE
1694 && (TYPE_NAME (a1
) == 0
1695 || TYPE_TRANSPARENT_AGGR (a1
))
1696 && TREE_CODE (TYPE_SIZE (a1
)) == INTEGER_CST
1697 && tree_int_cst_equal (TYPE_SIZE (a1
),
1701 for (memb
= TYPE_FIELDS (a1
);
1702 memb
; memb
= DECL_CHAIN (memb
))
1704 tree mv3
= TREE_TYPE (memb
);
1705 if (mv3
&& mv3
!= error_mark_node
1706 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1707 mv3
= (TYPE_ATOMIC (mv3
)
1708 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1710 : TYPE_MAIN_VARIANT (mv3
));
1711 if (comptypes_internal (mv3
, mv2
, enum_and_int_p
,
1718 else if (TREE_CODE (a2
) == UNION_TYPE
1719 && (TYPE_NAME (a2
) == 0
1720 || TYPE_TRANSPARENT_AGGR (a2
))
1721 && TREE_CODE (TYPE_SIZE (a2
)) == INTEGER_CST
1722 && tree_int_cst_equal (TYPE_SIZE (a2
),
1726 for (memb
= TYPE_FIELDS (a2
);
1727 memb
; memb
= DECL_CHAIN (memb
))
1729 tree mv3
= TREE_TYPE (memb
);
1730 if (mv3
&& mv3
!= error_mark_node
1731 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1732 mv3
= (TYPE_ATOMIC (mv3
)
1733 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1735 : TYPE_MAIN_VARIANT (mv3
));
1736 if (comptypes_internal (mv3
, mv1
, enum_and_int_p
,
1747 /* comptypes said ok, but record if it said to warn. */
1751 args1
= TREE_CHAIN (args1
);
1752 args2
= TREE_CHAIN (args2
);
1756 /* Compute the size to increment a pointer by. */
1759 c_size_in_bytes (const_tree type
)
1761 enum tree_code code
= TREE_CODE (type
);
1763 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
)
1764 return size_one_node
;
1766 if (!COMPLETE_OR_VOID_TYPE_P (type
))
1768 error ("arithmetic on pointer to an incomplete type");
1769 return size_one_node
;
1772 /* Convert in case a char is more than one unit. */
1773 return size_binop_loc (input_location
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1774 size_int (TYPE_PRECISION (char_type_node
)
1778 /* Return either DECL or its known constant value (if it has one). */
1781 decl_constant_value (tree decl
)
1783 if (/* Don't change a variable array bound or initial value to a constant
1784 in a place where a variable is invalid. Note that DECL_INITIAL
1785 isn't valid for a PARM_DECL. */
1786 current_function_decl
!= 0
1787 && TREE_CODE (decl
) != PARM_DECL
1788 && !TREE_THIS_VOLATILE (decl
)
1789 && TREE_READONLY (decl
)
1790 && DECL_INITIAL (decl
) != 0
1791 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
1792 /* This is invalid if initial value is not constant.
1793 If it has either a function call, a memory reference,
1794 or a variable, then re-evaluating it could give different results. */
1795 && TREE_CONSTANT (DECL_INITIAL (decl
))
1796 /* Check for cases where this is sub-optimal, even though valid. */
1797 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1798 return DECL_INITIAL (decl
);
1802 /* Convert the array expression EXP to a pointer. */
1804 array_to_pointer_conversion (location_t loc
, tree exp
)
1806 tree orig_exp
= exp
;
1807 tree type
= TREE_TYPE (exp
);
1809 tree restype
= TREE_TYPE (type
);
1812 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1814 STRIP_TYPE_NOPS (exp
);
1816 if (TREE_NO_WARNING (orig_exp
))
1817 TREE_NO_WARNING (exp
) = 1;
1819 ptrtype
= build_pointer_type (restype
);
1821 if (TREE_CODE (exp
) == INDIRECT_REF
)
1822 return convert (ptrtype
, TREE_OPERAND (exp
, 0));
1824 /* In C++ array compound literals are temporary objects unless they are
1825 const or appear in namespace scope, so they are destroyed too soon
1826 to use them for much of anything (c++/53220). */
1827 if (warn_cxx_compat
&& TREE_CODE (exp
) == COMPOUND_LITERAL_EXPR
)
1829 tree decl
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
1830 if (!TREE_READONLY (decl
) && !TREE_STATIC (decl
))
1831 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
1832 "converting an array compound literal to a pointer "
1833 "is ill-formed in C++");
1836 adr
= build_unary_op (loc
, ADDR_EXPR
, exp
, 1);
1837 return convert (ptrtype
, adr
);
1840 /* Convert the function expression EXP to a pointer. */
1842 function_to_pointer_conversion (location_t loc
, tree exp
)
1844 tree orig_exp
= exp
;
1846 gcc_assert (TREE_CODE (TREE_TYPE (exp
)) == FUNCTION_TYPE
);
1848 STRIP_TYPE_NOPS (exp
);
1850 if (TREE_NO_WARNING (orig_exp
))
1851 TREE_NO_WARNING (exp
) = 1;
1853 return build_unary_op (loc
, ADDR_EXPR
, exp
, 0);
1856 /* Mark EXP as read, not just set, for set but not used -Wunused
1857 warning purposes. */
1860 mark_exp_read (tree exp
)
1862 switch (TREE_CODE (exp
))
1866 DECL_READ_P (exp
) = 1;
1875 mark_exp_read (TREE_OPERAND (exp
, 0));
1878 case C_MAYBE_CONST_EXPR
:
1879 mark_exp_read (TREE_OPERAND (exp
, 1));
1886 /* Perform the default conversion of arrays and functions to pointers.
1887 Return the result of converting EXP. For any other expression, just
1890 LOC is the location of the expression. */
1893 default_function_array_conversion (location_t loc
, struct c_expr exp
)
1895 tree orig_exp
= exp
.value
;
1896 tree type
= TREE_TYPE (exp
.value
);
1897 enum tree_code code
= TREE_CODE (type
);
1903 bool not_lvalue
= false;
1904 bool lvalue_array_p
;
1906 while ((TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
1907 || CONVERT_EXPR_P (exp
.value
))
1908 && TREE_TYPE (TREE_OPERAND (exp
.value
, 0)) == type
)
1910 if (TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
)
1912 exp
.value
= TREE_OPERAND (exp
.value
, 0);
1915 if (TREE_NO_WARNING (orig_exp
))
1916 TREE_NO_WARNING (exp
.value
) = 1;
1918 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
.value
);
1919 if (!flag_isoc99
&& !lvalue_array_p
)
1921 /* Before C99, non-lvalue arrays do not decay to pointers.
1922 Normally, using such an array would be invalid; but it can
1923 be used correctly inside sizeof or as a statement expression.
1924 Thus, do not give an error here; an error will result later. */
1928 exp
.value
= array_to_pointer_conversion (loc
, exp
.value
);
1932 exp
.value
= function_to_pointer_conversion (loc
, exp
.value
);
1942 default_function_array_read_conversion (location_t loc
, struct c_expr exp
)
1944 mark_exp_read (exp
.value
);
1945 return default_function_array_conversion (loc
, exp
);
1948 /* Return whether EXPR should be treated as an atomic lvalue for the
1949 purposes of load and store handling. */
1952 really_atomic_lvalue (tree expr
)
1954 if (expr
== error_mark_node
|| TREE_TYPE (expr
) == error_mark_node
)
1956 if (!TYPE_ATOMIC (TREE_TYPE (expr
)))
1958 if (!lvalue_p (expr
))
1961 /* Ignore _Atomic on register variables, since their addresses can't
1962 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1963 sequences wouldn't work. Ignore _Atomic on structures containing
1964 bit-fields, since accessing elements of atomic structures or
1965 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1966 it's undefined at translation time or execution time, and the
1967 normal atomic sequences again wouldn't work. */
1968 while (handled_component_p (expr
))
1970 if (TREE_CODE (expr
) == COMPONENT_REF
1971 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
1973 expr
= TREE_OPERAND (expr
, 0);
1975 if (DECL_P (expr
) && C_DECL_REGISTER (expr
))
1980 /* Convert expression EXP (location LOC) from lvalue to rvalue,
1981 including converting functions and arrays to pointers if CONVERT_P.
1982 If READ_P, also mark the expression as having been read. */
1985 convert_lvalue_to_rvalue (location_t loc
, struct c_expr exp
,
1986 bool convert_p
, bool read_p
)
1989 mark_exp_read (exp
.value
);
1991 exp
= default_function_array_conversion (loc
, exp
);
1992 if (really_atomic_lvalue (exp
.value
))
1994 vec
<tree
, va_gc
> *params
;
1995 tree nonatomic_type
, tmp
, tmp_addr
, fndecl
, func_call
;
1996 tree expr_type
= TREE_TYPE (exp
.value
);
1997 tree expr_addr
= build_unary_op (loc
, ADDR_EXPR
, exp
.value
, 0);
1998 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
2000 gcc_assert (TYPE_ATOMIC (expr_type
));
2002 /* Expansion of a generic atomic load may require an addition
2003 element, so allocate enough to prevent a resize. */
2004 vec_alloc (params
, 4);
2006 /* Remove the qualifiers for the rest of the expressions and
2007 create the VAL temp variable to hold the RHS. */
2008 nonatomic_type
= build_qualified_type (expr_type
, TYPE_UNQUALIFIED
);
2009 tmp
= create_tmp_var (nonatomic_type
, NULL
);
2010 tmp_addr
= build_unary_op (loc
, ADDR_EXPR
, tmp
, 0);
2011 TREE_ADDRESSABLE (tmp
) = 1;
2012 TREE_NO_WARNING (tmp
) = 1;
2014 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2015 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
2016 params
->quick_push (expr_addr
);
2017 params
->quick_push (tmp_addr
);
2018 params
->quick_push (seq_cst
);
2019 func_call
= build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
2021 /* EXPR is always read. */
2022 mark_exp_read (exp
.value
);
2024 /* Return tmp which contains the value loaded. */
2025 exp
.value
= build2 (COMPOUND_EXPR
, nonatomic_type
, func_call
, tmp
);
2030 /* EXP is an expression of integer type. Apply the integer promotions
2031 to it and return the promoted value. */
2034 perform_integral_promotions (tree exp
)
2036 tree type
= TREE_TYPE (exp
);
2037 enum tree_code code
= TREE_CODE (type
);
2039 gcc_assert (INTEGRAL_TYPE_P (type
));
2041 /* Normally convert enums to int,
2042 but convert wide enums to something wider. */
2043 if (code
== ENUMERAL_TYPE
)
2045 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
2046 TYPE_PRECISION (integer_type_node
)),
2047 ((TYPE_PRECISION (type
)
2048 >= TYPE_PRECISION (integer_type_node
))
2049 && TYPE_UNSIGNED (type
)));
2051 return convert (type
, exp
);
2054 /* ??? This should no longer be needed now bit-fields have their
2056 if (TREE_CODE (exp
) == COMPONENT_REF
2057 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
2058 /* If it's thinner than an int, promote it like a
2059 c_promoting_integer_type_p, otherwise leave it alone. */
2060 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
2061 TYPE_PRECISION (integer_type_node
)))
2062 return convert (integer_type_node
, exp
);
2064 if (c_promoting_integer_type_p (type
))
2066 /* Preserve unsignedness if not really getting any wider. */
2067 if (TYPE_UNSIGNED (type
)
2068 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
2069 return convert (unsigned_type_node
, exp
);
2071 return convert (integer_type_node
, exp
);
2078 /* Perform default promotions for C data used in expressions.
2079 Enumeral types or short or char are converted to int.
2080 In addition, manifest constants symbols are replaced by their values. */
2083 default_conversion (tree exp
)
2086 tree type
= TREE_TYPE (exp
);
2087 enum tree_code code
= TREE_CODE (type
);
2090 mark_exp_read (exp
);
2092 /* Functions and arrays have been converted during parsing. */
2093 gcc_assert (code
!= FUNCTION_TYPE
);
2094 if (code
== ARRAY_TYPE
)
2097 /* Constants can be used directly unless they're not loadable. */
2098 if (TREE_CODE (exp
) == CONST_DECL
)
2099 exp
= DECL_INITIAL (exp
);
2101 /* Strip no-op conversions. */
2103 STRIP_TYPE_NOPS (exp
);
2105 if (TREE_NO_WARNING (orig_exp
))
2106 TREE_NO_WARNING (exp
) = 1;
2108 if (code
== VOID_TYPE
)
2110 error ("void value not ignored as it ought to be");
2111 return error_mark_node
;
2114 exp
= require_complete_type (exp
);
2115 if (exp
== error_mark_node
)
2116 return error_mark_node
;
2118 promoted_type
= targetm
.promoted_type (type
);
2120 return convert (promoted_type
, exp
);
2122 if (INTEGRAL_TYPE_P (type
))
2123 return perform_integral_promotions (exp
);
2128 /* Look up COMPONENT in a structure or union TYPE.
2130 If the component name is not found, returns NULL_TREE. Otherwise,
2131 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2132 stepping down the chain to the component, which is in the last
2133 TREE_VALUE of the list. Normally the list is of length one, but if
2134 the component is embedded within (nested) anonymous structures or
2135 unions, the list steps down the chain to the component. */
2138 lookup_field (tree type
, tree component
)
2142 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2143 to the field elements. Use a binary search on this array to quickly
2144 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2145 will always be set for structures which have many elements. */
2147 if (TYPE_LANG_SPECIFIC (type
) && TYPE_LANG_SPECIFIC (type
)->s
)
2150 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
2152 field
= TYPE_FIELDS (type
);
2154 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
2155 while (top
- bot
> 1)
2157 half
= (top
- bot
+ 1) >> 1;
2158 field
= field_array
[bot
+half
];
2160 if (DECL_NAME (field
) == NULL_TREE
)
2162 /* Step through all anon unions in linear fashion. */
2163 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
2165 field
= field_array
[bot
++];
2166 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
2167 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
2169 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2172 return tree_cons (NULL_TREE
, field
, anon
);
2174 /* The Plan 9 compiler permits referring
2175 directly to an anonymous struct/union field
2176 using a typedef name. */
2177 if (flag_plan9_extensions
2178 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2179 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field
)))
2181 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2187 /* Entire record is only anon unions. */
2191 /* Restart the binary search, with new lower bound. */
2195 if (DECL_NAME (field
) == component
)
2197 if (DECL_NAME (field
) < component
)
2203 if (DECL_NAME (field_array
[bot
]) == component
)
2204 field
= field_array
[bot
];
2205 else if (DECL_NAME (field
) != component
)
2210 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2212 if (DECL_NAME (field
) == NULL_TREE
2213 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
2214 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
))
2216 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2219 return tree_cons (NULL_TREE
, field
, anon
);
2221 /* The Plan 9 compiler permits referring directly to an
2222 anonymous struct/union field using a typedef
2224 if (flag_plan9_extensions
2225 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2226 && TREE_CODE (TYPE_NAME (TREE_TYPE (field
))) == TYPE_DECL
2227 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2232 if (DECL_NAME (field
) == component
)
2236 if (field
== NULL_TREE
)
2240 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
2243 /* Make an expression to refer to the COMPONENT field of structure or
2244 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2245 location of the COMPONENT_REF. */
2248 build_component_ref (location_t loc
, tree datum
, tree component
)
2250 tree type
= TREE_TYPE (datum
);
2251 enum tree_code code
= TREE_CODE (type
);
2254 bool datum_lvalue
= lvalue_p (datum
);
2256 if (!objc_is_public (datum
, component
))
2257 return error_mark_node
;
2259 /* Detect Objective-C property syntax object.property. */
2260 if (c_dialect_objc ()
2261 && (ref
= objc_maybe_build_component_ref (datum
, component
)))
2264 /* See if there is a field or component with name COMPONENT. */
2266 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2268 if (!COMPLETE_TYPE_P (type
))
2270 c_incomplete_type_error (NULL_TREE
, type
);
2271 return error_mark_node
;
2274 field
= lookup_field (type
, component
);
2278 error_at (loc
, "%qT has no member named %qE", type
, component
);
2279 return error_mark_node
;
2282 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2283 This might be better solved in future the way the C++ front
2284 end does it - by giving the anonymous entities each a
2285 separate name and type, and then have build_component_ref
2286 recursively call itself. We can't do that here. */
2289 tree subdatum
= TREE_VALUE (field
);
2292 bool use_datum_quals
;
2294 if (TREE_TYPE (subdatum
) == error_mark_node
)
2295 return error_mark_node
;
2297 /* If this is an rvalue, it does not have qualifiers in C
2298 standard terms and we must avoid propagating such
2299 qualifiers down to a non-lvalue array that is then
2300 converted to a pointer. */
2301 use_datum_quals
= (datum_lvalue
2302 || TREE_CODE (TREE_TYPE (subdatum
)) != ARRAY_TYPE
);
2304 quals
= TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum
)));
2305 if (use_datum_quals
)
2306 quals
|= TYPE_QUALS (TREE_TYPE (datum
));
2307 subtype
= c_build_qualified_type (TREE_TYPE (subdatum
), quals
);
2309 ref
= build3 (COMPONENT_REF
, subtype
, datum
, subdatum
,
2311 SET_EXPR_LOCATION (ref
, loc
);
2312 if (TREE_READONLY (subdatum
)
2313 || (use_datum_quals
&& TREE_READONLY (datum
)))
2314 TREE_READONLY (ref
) = 1;
2315 if (TREE_THIS_VOLATILE (subdatum
)
2316 || (use_datum_quals
&& TREE_THIS_VOLATILE (datum
)))
2317 TREE_THIS_VOLATILE (ref
) = 1;
2319 if (TREE_DEPRECATED (subdatum
))
2320 warn_deprecated_use (subdatum
, NULL_TREE
);
2324 field
= TREE_CHAIN (field
);
2330 else if (code
!= ERROR_MARK
)
2332 "request for member %qE in something not a structure or union",
2335 return error_mark_node
;
2338 /* Given an expression PTR for a pointer, return an expression
2339 for the value pointed to.
2340 ERRORSTRING is the name of the operator to appear in error messages.
2342 LOC is the location to use for the generated tree. */
2345 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errstring
)
2347 tree pointer
= default_conversion (ptr
);
2348 tree type
= TREE_TYPE (pointer
);
2351 if (TREE_CODE (type
) == POINTER_TYPE
)
2353 if (CONVERT_EXPR_P (pointer
)
2354 || TREE_CODE (pointer
) == VIEW_CONVERT_EXPR
)
2356 /* If a warning is issued, mark it to avoid duplicates from
2357 the backend. This only needs to be done at
2358 warn_strict_aliasing > 2. */
2359 if (warn_strict_aliasing
> 2)
2360 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer
, 0)),
2361 type
, TREE_OPERAND (pointer
, 0)))
2362 TREE_NO_WARNING (pointer
) = 1;
2365 if (TREE_CODE (pointer
) == ADDR_EXPR
2366 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
2367 == TREE_TYPE (type
)))
2369 ref
= TREE_OPERAND (pointer
, 0);
2370 protected_set_expr_location (ref
, loc
);
2375 tree t
= TREE_TYPE (type
);
2377 ref
= build1 (INDIRECT_REF
, t
, pointer
);
2379 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
2381 error_at (loc
, "dereferencing pointer to incomplete type");
2382 return error_mark_node
;
2384 if (VOID_TYPE_P (t
) && c_inhibit_evaluation_warnings
== 0)
2385 warning_at (loc
, 0, "dereferencing %<void *%> pointer");
2387 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2388 so that we get the proper error message if the result is used
2389 to assign to. Also, &* is supposed to be a no-op.
2390 And ANSI C seems to specify that the type of the result
2391 should be the const type. */
2392 /* A de-reference of a pointer to const is not a const. It is valid
2393 to change it via some other pointer. */
2394 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2395 TREE_SIDE_EFFECTS (ref
)
2396 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
2397 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2398 protected_set_expr_location (ref
, loc
);
2402 else if (TREE_CODE (pointer
) != ERROR_MARK
)
2403 invalid_indirection_error (loc
, type
, errstring
);
2405 return error_mark_node
;
2408 /* This handles expressions of the form "a[i]", which denotes
2411 This is logically equivalent in C to *(a+i), but we may do it differently.
2412 If A is a variable or a member, we generate a primitive ARRAY_REF.
2413 This avoids forcing the array out of registers, and can work on
2414 arrays that are not lvalues (for example, members of structures returned
2417 For vector types, allow vector[i] but not i[vector], and create
2418 *(((type*)&vectortype) + i) for the expression.
2420 LOC is the location to use for the returned expression. */
2423 build_array_ref (location_t loc
, tree array
, tree index
)
2426 bool swapped
= false;
2427 if (TREE_TYPE (array
) == error_mark_node
2428 || TREE_TYPE (index
) == error_mark_node
)
2429 return error_mark_node
;
2431 if (flag_cilkplus
&& contains_array_notation_expr (index
))
2434 if (!find_rank (loc
, index
, index
, true, &rank
))
2435 return error_mark_node
;
2438 error_at (loc
, "rank of the array's index is greater than 1");
2439 return error_mark_node
;
2442 if (TREE_CODE (TREE_TYPE (array
)) != ARRAY_TYPE
2443 && TREE_CODE (TREE_TYPE (array
)) != POINTER_TYPE
2444 /* Allow vector[index] but not index[vector]. */
2445 && TREE_CODE (TREE_TYPE (array
)) != VECTOR_TYPE
)
2448 if (TREE_CODE (TREE_TYPE (index
)) != ARRAY_TYPE
2449 && TREE_CODE (TREE_TYPE (index
)) != POINTER_TYPE
)
2452 "subscripted value is neither array nor pointer nor vector");
2454 return error_mark_node
;
2462 if (!INTEGRAL_TYPE_P (TREE_TYPE (index
)))
2464 error_at (loc
, "array subscript is not an integer");
2465 return error_mark_node
;
2468 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array
))) == FUNCTION_TYPE
)
2470 error_at (loc
, "subscripted value is pointer to function");
2471 return error_mark_node
;
2474 /* ??? Existing practice has been to warn only when the char
2475 index is syntactically the index, not for char[array]. */
2477 warn_array_subscript_with_type_char (index
);
2479 /* Apply default promotions *after* noticing character types. */
2480 index
= default_conversion (index
);
2482 gcc_assert (TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
);
2484 convert_vector_to_pointer_for_subscript (loc
, &array
, index
);
2486 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
2490 /* An array that is indexed by a non-constant
2491 cannot be stored in a register; we must be able to do
2492 address arithmetic on its address.
2493 Likewise an array of elements of variable size. */
2494 if (TREE_CODE (index
) != INTEGER_CST
2495 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2496 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2498 if (!c_mark_addressable (array
))
2499 return error_mark_node
;
2501 /* An array that is indexed by a constant value which is not within
2502 the array bounds cannot be stored in a register either; because we
2503 would get a crash in store_bit_field/extract_bit_field when trying
2504 to access a non-existent part of the register. */
2505 if (TREE_CODE (index
) == INTEGER_CST
2506 && TYPE_DOMAIN (TREE_TYPE (array
))
2507 && !int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
2509 if (!c_mark_addressable (array
))
2510 return error_mark_node
;
2516 while (TREE_CODE (foo
) == COMPONENT_REF
)
2517 foo
= TREE_OPERAND (foo
, 0);
2518 if (TREE_CODE (foo
) == VAR_DECL
&& C_DECL_REGISTER (foo
))
2519 pedwarn (loc
, OPT_Wpedantic
,
2520 "ISO C forbids subscripting %<register%> array");
2521 else if (!flag_isoc99
&& !lvalue_p (foo
))
2522 pedwarn (loc
, OPT_Wpedantic
,
2523 "ISO C90 forbids subscripting non-lvalue array");
2526 type
= TREE_TYPE (TREE_TYPE (array
));
2527 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
2528 /* Array ref is const/volatile if the array elements are
2529 or if the array is. */
2530 TREE_READONLY (rval
)
2531 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2532 | TREE_READONLY (array
));
2533 TREE_SIDE_EFFECTS (rval
)
2534 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2535 | TREE_SIDE_EFFECTS (array
));
2536 TREE_THIS_VOLATILE (rval
)
2537 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2538 /* This was added by rms on 16 Nov 91.
2539 It fixes vol struct foo *a; a->elts[1]
2540 in an inline function.
2541 Hope it doesn't break something else. */
2542 | TREE_THIS_VOLATILE (array
));
2543 ret
= require_complete_type (rval
);
2544 protected_set_expr_location (ret
, loc
);
2549 tree ar
= default_conversion (array
);
2551 if (ar
== error_mark_node
)
2554 gcc_assert (TREE_CODE (TREE_TYPE (ar
)) == POINTER_TYPE
);
2555 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) != FUNCTION_TYPE
);
2557 return build_indirect_ref
2558 (loc
, build_binary_op (loc
, PLUS_EXPR
, ar
, index
, 0),
2563 /* Build an external reference to identifier ID. FUN indicates
2564 whether this will be used for a function call. LOC is the source
2565 location of the identifier. This sets *TYPE to the type of the
2566 identifier, which is not the same as the type of the returned value
2567 for CONST_DECLs defined as enum constants. If the type of the
2568 identifier is not available, *TYPE is set to NULL. */
2570 build_external_ref (location_t loc
, tree id
, int fun
, tree
*type
)
2573 tree decl
= lookup_name (id
);
2575 /* In Objective-C, an instance variable (ivar) may be preferred to
2576 whatever lookup_name() found. */
2577 decl
= objc_lookup_ivar (decl
, id
);
2580 if (decl
&& decl
!= error_mark_node
)
2583 *type
= TREE_TYPE (ref
);
2586 /* Implicit function declaration. */
2587 ref
= implicitly_declare (loc
, id
);
2588 else if (decl
== error_mark_node
)
2589 /* Don't complain about something that's already been
2590 complained about. */
2591 return error_mark_node
;
2594 undeclared_variable (loc
, id
);
2595 return error_mark_node
;
2598 if (TREE_TYPE (ref
) == error_mark_node
)
2599 return error_mark_node
;
2601 if (TREE_DEPRECATED (ref
))
2602 warn_deprecated_use (ref
, NULL_TREE
);
2604 /* Recursive call does not count as usage. */
2605 if (ref
!= current_function_decl
)
2607 TREE_USED (ref
) = 1;
2610 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
2612 if (!in_sizeof
&& !in_typeof
)
2613 C_DECL_USED (ref
) = 1;
2614 else if (DECL_INITIAL (ref
) == 0
2615 && DECL_EXTERNAL (ref
)
2616 && !TREE_PUBLIC (ref
))
2617 record_maybe_used_decl (ref
);
2620 if (TREE_CODE (ref
) == CONST_DECL
)
2622 used_types_insert (TREE_TYPE (ref
));
2625 && TREE_CODE (TREE_TYPE (ref
)) == ENUMERAL_TYPE
2626 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref
)))
2628 warning_at (loc
, OPT_Wc___compat
,
2629 ("enum constant defined in struct or union "
2630 "is not visible in C++"));
2631 inform (DECL_SOURCE_LOCATION (ref
), "enum constant defined here");
2634 ref
= DECL_INITIAL (ref
);
2635 TREE_CONSTANT (ref
) = 1;
2637 else if (current_function_decl
!= 0
2638 && !DECL_FILE_SCOPE_P (current_function_decl
)
2639 && (TREE_CODE (ref
) == VAR_DECL
2640 || TREE_CODE (ref
) == PARM_DECL
2641 || TREE_CODE (ref
) == FUNCTION_DECL
))
2643 tree context
= decl_function_context (ref
);
2645 if (context
!= 0 && context
!= current_function_decl
)
2646 DECL_NONLOCAL (ref
) = 1;
2648 /* C99 6.7.4p3: An inline definition of a function with external
2649 linkage ... shall not contain a reference to an identifier with
2650 internal linkage. */
2651 else if (current_function_decl
!= 0
2652 && DECL_DECLARED_INLINE_P (current_function_decl
)
2653 && DECL_EXTERNAL (current_function_decl
)
2654 && VAR_OR_FUNCTION_DECL_P (ref
)
2655 && (TREE_CODE (ref
) != VAR_DECL
|| TREE_STATIC (ref
))
2656 && ! TREE_PUBLIC (ref
)
2657 && DECL_CONTEXT (ref
) != current_function_decl
)
2658 record_inline_static (loc
, current_function_decl
, ref
,
2664 /* Record details of decls possibly used inside sizeof or typeof. */
2665 struct maybe_used_decl
2669 /* The level seen at (in_sizeof + in_typeof). */
2671 /* The next one at this level or above, or NULL. */
2672 struct maybe_used_decl
*next
;
2675 static struct maybe_used_decl
*maybe_used_decls
;
2677 /* Record that DECL, an undefined static function reference seen
2678 inside sizeof or typeof, might be used if the operand of sizeof is
2679 a VLA type or the operand of typeof is a variably modified
2683 record_maybe_used_decl (tree decl
)
2685 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
2687 t
->level
= in_sizeof
+ in_typeof
;
2688 t
->next
= maybe_used_decls
;
2689 maybe_used_decls
= t
;
2692 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2693 USED is false, just discard them. If it is true, mark them used
2694 (if no longer inside sizeof or typeof) or move them to the next
2695 level up (if still inside sizeof or typeof). */
2698 pop_maybe_used (bool used
)
2700 struct maybe_used_decl
*p
= maybe_used_decls
;
2701 int cur_level
= in_sizeof
+ in_typeof
;
2702 while (p
&& p
->level
> cur_level
)
2707 C_DECL_USED (p
->decl
) = 1;
2709 p
->level
= cur_level
;
2713 if (!used
|| cur_level
== 0)
2714 maybe_used_decls
= p
;
2717 /* Return the result of sizeof applied to EXPR. */
2720 c_expr_sizeof_expr (location_t loc
, struct c_expr expr
)
2723 if (expr
.value
== error_mark_node
)
2725 ret
.value
= error_mark_node
;
2726 ret
.original_code
= ERROR_MARK
;
2727 ret
.original_type
= NULL
;
2728 pop_maybe_used (false);
2732 bool expr_const_operands
= true;
2733 tree folded_expr
= c_fully_fold (expr
.value
, require_constant_value
,
2734 &expr_const_operands
);
2735 ret
.value
= c_sizeof (loc
, TREE_TYPE (folded_expr
));
2736 c_last_sizeof_arg
= expr
.value
;
2737 ret
.original_code
= SIZEOF_EXPR
;
2738 ret
.original_type
= NULL
;
2739 if (c_vla_type_p (TREE_TYPE (folded_expr
)))
2741 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2742 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2743 folded_expr
, ret
.value
);
2744 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !expr_const_operands
;
2745 SET_EXPR_LOCATION (ret
.value
, loc
);
2747 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr
)));
2752 /* Return the result of sizeof applied to T, a structure for the type
2753 name passed to sizeof (rather than the type itself). LOC is the
2754 location of the original expression. */
2757 c_expr_sizeof_type (location_t loc
, struct c_type_name
*t
)
2761 tree type_expr
= NULL_TREE
;
2762 bool type_expr_const
= true;
2763 type
= groktypename (t
, &type_expr
, &type_expr_const
);
2764 ret
.value
= c_sizeof (loc
, type
);
2765 c_last_sizeof_arg
= type
;
2766 ret
.original_code
= SIZEOF_EXPR
;
2767 ret
.original_type
= NULL
;
2768 if ((type_expr
|| TREE_CODE (ret
.value
) == INTEGER_CST
)
2769 && c_vla_type_p (type
))
2771 /* If the type is a [*] array, it is a VLA but is represented as
2772 having a size of zero. In such a case we must ensure that
2773 the result of sizeof does not get folded to a constant by
2774 c_fully_fold, because if the size is evaluated the result is
2775 not constant and so constraints on zero or negative size
2776 arrays must not be applied when this sizeof call is inside
2777 another array declarator. */
2779 type_expr
= integer_zero_node
;
2780 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2781 type_expr
, ret
.value
);
2782 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !type_expr_const
;
2784 pop_maybe_used (type
!= error_mark_node
2785 ? C_TYPE_VARIABLE_SIZE (type
) : false);
2789 /* Build a function call to function FUNCTION with parameters PARAMS.
2790 The function call is at LOC.
2791 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2792 TREE_VALUE of each node is a parameter-expression.
2793 FUNCTION's data type may be a function type or a pointer-to-function. */
2796 build_function_call (location_t loc
, tree function
, tree params
)
2798 vec
<tree
, va_gc
> *v
;
2801 vec_alloc (v
, list_length (params
));
2802 for (; params
; params
= TREE_CHAIN (params
))
2803 v
->quick_push (TREE_VALUE (params
));
2804 ret
= build_function_call_vec (loc
, vNULL
, function
, v
, NULL
);
2809 /* Give a note about the location of the declaration of DECL. */
2811 static void inform_declaration (tree decl
)
2813 if (decl
&& (TREE_CODE (decl
) != FUNCTION_DECL
|| !DECL_BUILT_IN (decl
)))
2814 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
2817 /* Build a function call to function FUNCTION with parameters PARAMS.
2818 ORIGTYPES, if not NULL, is a vector of types; each element is
2819 either NULL or the original type of the corresponding element in
2820 PARAMS. The original type may differ from TREE_TYPE of the
2821 parameter for enums. FUNCTION's data type may be a function type
2822 or pointer-to-function. This function changes the elements of
2826 build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
2827 tree function
, vec
<tree
, va_gc
> *params
,
2828 vec
<tree
, va_gc
> *origtypes
)
2830 tree fntype
, fundecl
= 0;
2831 tree name
= NULL_TREE
, result
;
2837 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2838 STRIP_TYPE_NOPS (function
);
2840 /* Convert anything with function type to a pointer-to-function. */
2841 if (TREE_CODE (function
) == FUNCTION_DECL
)
2843 /* Implement type-directed function overloading for builtins.
2844 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2845 handle all the type checking. The result is a complete expression
2846 that implements this function call. */
2847 tem
= resolve_overloaded_builtin (loc
, function
, params
);
2851 name
= DECL_NAME (function
);
2854 tm_malloc_replacement (function
);
2856 /* Atomic functions have type checking/casting already done. They are
2857 often rewritten and don't match the original parameter list. */
2858 if (name
&& !strncmp (IDENTIFIER_POINTER (name
), "__atomic_", 9))
2862 && is_cilkplus_reduce_builtin (function
))
2865 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
)
2866 function
= function_to_pointer_conversion (loc
, function
);
2868 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2869 expressions, like those used for ObjC messenger dispatches. */
2870 if (params
&& !params
->is_empty ())
2871 function
= objc_rewrite_function_call (function
, (*params
)[0]);
2873 function
= c_fully_fold (function
, false, NULL
);
2875 fntype
= TREE_TYPE (function
);
2877 if (TREE_CODE (fntype
) == ERROR_MARK
)
2878 return error_mark_node
;
2880 if (!(TREE_CODE (fntype
) == POINTER_TYPE
2881 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
2883 if (!flag_diagnostics_show_caret
)
2885 "called object %qE is not a function or function pointer",
2887 else if (DECL_P (function
))
2890 "called object %qD is not a function or function pointer",
2892 inform_declaration (function
);
2896 "called object is not a function or function pointer");
2897 return error_mark_node
;
2900 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
2901 current_function_returns_abnormally
= 1;
2903 /* fntype now gets the type of function pointed to. */
2904 fntype
= TREE_TYPE (fntype
);
2906 /* Convert the parameters to the types declared in the
2907 function prototype, or apply default promotions. */
2909 nargs
= convert_arguments (loc
, arg_loc
, TYPE_ARG_TYPES (fntype
), params
,
2910 origtypes
, function
, fundecl
);
2912 return error_mark_node
;
2914 /* Check that the function is called through a compatible prototype.
2915 If it is not, warn. */
2916 if (CONVERT_EXPR_P (function
)
2917 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
2918 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
2919 && !comptypes (fntype
, TREE_TYPE (tem
)))
2921 tree return_type
= TREE_TYPE (fntype
);
2923 /* This situation leads to run-time undefined behavior. We can't,
2924 therefore, simply error unless we can prove that all possible
2925 executions of the program must execute the code. */
2926 warning_at (loc
, 0, "function called through a non-compatible type");
2928 if (VOID_TYPE_P (return_type
)
2929 && TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
)
2931 "function with qualified void return type called");
2934 argarray
= vec_safe_address (params
);
2936 /* Check that arguments to builtin functions match the expectations. */
2938 && DECL_BUILT_IN (fundecl
)
2939 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
2940 && !check_builtin_function_arguments (fundecl
, nargs
, argarray
))
2941 return error_mark_node
;
2943 /* Check that the arguments to the function are valid. */
2944 check_function_arguments (fntype
, nargs
, argarray
);
2946 if (name
!= NULL_TREE
2947 && !strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10))
2949 if (require_constant_value
)
2951 fold_build_call_array_initializer_loc (loc
, TREE_TYPE (fntype
),
2952 function
, nargs
, argarray
);
2954 result
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
2955 function
, nargs
, argarray
);
2956 if (TREE_CODE (result
) == NOP_EXPR
2957 && TREE_CODE (TREE_OPERAND (result
, 0)) == INTEGER_CST
)
2958 STRIP_TYPE_NOPS (result
);
2961 result
= build_call_array_loc (loc
, TREE_TYPE (fntype
),
2962 function
, nargs
, argarray
);
2964 if (VOID_TYPE_P (TREE_TYPE (result
)))
2966 if (TYPE_QUALS (TREE_TYPE (result
)) != TYPE_UNQUALIFIED
)
2968 "function with qualified void return type called");
2971 return require_complete_type (result
);
2974 /* Convert the argument expressions in the vector VALUES
2975 to the types in the list TYPELIST.
2977 If TYPELIST is exhausted, or when an element has NULL as its type,
2978 perform the default conversions.
2980 ORIGTYPES is the original types of the expressions in VALUES. This
2981 holds the type of enum values which have been converted to integral
2982 types. It may be NULL.
2984 FUNCTION is a tree for the called function. It is used only for
2985 error messages, where it is formatted with %qE.
2987 This is also where warnings about wrong number of args are generated.
2989 ARG_LOC are locations of function arguments (if any).
2991 Returns the actual number of arguments processed (which may be less
2992 than the length of VALUES in some error situations), or -1 on
2996 convert_arguments (location_t loc
, vec
<location_t
> arg_loc
, tree typelist
,
2997 vec
<tree
, va_gc
> *values
, vec
<tree
, va_gc
> *origtypes
,
2998 tree function
, tree fundecl
)
3001 unsigned int parmnum
;
3002 bool error_args
= false;
3003 const bool type_generic
= fundecl
3004 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl
)));
3005 bool type_generic_remove_excess_precision
= false;
3008 /* Change pointer to function to the function itself for
3010 if (TREE_CODE (function
) == ADDR_EXPR
3011 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
3012 function
= TREE_OPERAND (function
, 0);
3014 /* Handle an ObjC selector specially for diagnostics. */
3015 selector
= objc_message_selector ();
3017 /* For type-generic built-in functions, determine whether excess
3018 precision should be removed (classification) or not
3021 && DECL_BUILT_IN (fundecl
)
3022 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
)
3024 switch (DECL_FUNCTION_CODE (fundecl
))
3026 case BUILT_IN_ISFINITE
:
3027 case BUILT_IN_ISINF
:
3028 case BUILT_IN_ISINF_SIGN
:
3029 case BUILT_IN_ISNAN
:
3030 case BUILT_IN_ISNORMAL
:
3031 case BUILT_IN_FPCLASSIFY
:
3032 type_generic_remove_excess_precision
= true;
3036 type_generic_remove_excess_precision
= false;
3040 if (flag_cilkplus
&& fundecl
&& is_cilkplus_reduce_builtin (fundecl
))
3041 return vec_safe_length (values
);
3043 /* Scan the given expressions and types, producing individual
3044 converted arguments. */
3046 for (typetail
= typelist
, parmnum
= 0;
3047 values
&& values
->iterate (parmnum
, &val
);
3050 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3051 tree valtype
= TREE_TYPE (val
);
3052 tree rname
= function
;
3053 int argnum
= parmnum
+ 1;
3054 const char *invalid_func_diag
;
3055 bool excess_precision
= false;
3059 if (type
== void_type_node
)
3062 error_at (loc
, "too many arguments to method %qE", selector
);
3064 error_at (loc
, "too many arguments to function %qE", function
);
3065 inform_declaration (fundecl
);
3069 if (selector
&& argnum
> 2)
3075 npc
= null_pointer_constant_p (val
);
3077 /* If there is excess precision and a prototype, convert once to
3078 the required type rather than converting via the semantic
3079 type. Likewise without a prototype a float value represented
3080 as long double should be converted once to double. But for
3081 type-generic classification functions excess precision must
3083 if (TREE_CODE (val
) == EXCESS_PRECISION_EXPR
3084 && (type
|| !type_generic
|| !type_generic_remove_excess_precision
))
3086 val
= TREE_OPERAND (val
, 0);
3087 excess_precision
= true;
3089 val
= c_fully_fold (val
, false, NULL
);
3090 STRIP_TYPE_NOPS (val
);
3092 val
= require_complete_type (val
);
3096 /* Formal parm type is specified by a function prototype. */
3098 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3100 error ("type of formal parameter %d is incomplete", parmnum
+ 1);
3107 /* Optionally warn about conversions that
3108 differ from the default conversions. */
3109 if (warn_traditional_conversion
|| warn_traditional
)
3111 unsigned int formal_prec
= TYPE_PRECISION (type
);
3113 if (INTEGRAL_TYPE_P (type
)
3114 && TREE_CODE (valtype
) == REAL_TYPE
)
3115 warning (0, "passing argument %d of %qE as integer "
3116 "rather than floating due to prototype",
3118 if (INTEGRAL_TYPE_P (type
)
3119 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3120 warning (0, "passing argument %d of %qE as integer "
3121 "rather than complex due to prototype",
3123 else if (TREE_CODE (type
) == COMPLEX_TYPE
3124 && TREE_CODE (valtype
) == REAL_TYPE
)
3125 warning (0, "passing argument %d of %qE as complex "
3126 "rather than floating due to prototype",
3128 else if (TREE_CODE (type
) == REAL_TYPE
3129 && INTEGRAL_TYPE_P (valtype
))
3130 warning (0, "passing argument %d of %qE as floating "
3131 "rather than integer due to prototype",
3133 else if (TREE_CODE (type
) == COMPLEX_TYPE
3134 && INTEGRAL_TYPE_P (valtype
))
3135 warning (0, "passing argument %d of %qE as complex "
3136 "rather than integer due to prototype",
3138 else if (TREE_CODE (type
) == REAL_TYPE
3139 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3140 warning (0, "passing argument %d of %qE as floating "
3141 "rather than complex due to prototype",
3143 /* ??? At some point, messages should be written about
3144 conversions between complex types, but that's too messy
3146 else if (TREE_CODE (type
) == REAL_TYPE
3147 && TREE_CODE (valtype
) == REAL_TYPE
)
3149 /* Warn if any argument is passed as `float',
3150 since without a prototype it would be `double'. */
3151 if (formal_prec
== TYPE_PRECISION (float_type_node
)
3152 && type
!= dfloat32_type_node
)
3153 warning (0, "passing argument %d of %qE as %<float%> "
3154 "rather than %<double%> due to prototype",
3157 /* Warn if mismatch between argument and prototype
3158 for decimal float types. Warn of conversions with
3159 binary float types and of precision narrowing due to
3161 else if (type
!= valtype
3162 && (type
== dfloat32_type_node
3163 || type
== dfloat64_type_node
3164 || type
== dfloat128_type_node
3165 || valtype
== dfloat32_type_node
3166 || valtype
== dfloat64_type_node
3167 || valtype
== dfloat128_type_node
)
3169 <= TYPE_PRECISION (valtype
)
3170 || (type
== dfloat128_type_node
3172 != dfloat64_type_node
3174 != dfloat32_type_node
)))
3175 || (type
== dfloat64_type_node
3177 != dfloat32_type_node
))))
3178 warning (0, "passing argument %d of %qE as %qT "
3179 "rather than %qT due to prototype",
3180 argnum
, rname
, type
, valtype
);
3183 /* Detect integer changing in width or signedness.
3184 These warnings are only activated with
3185 -Wtraditional-conversion, not with -Wtraditional. */
3186 else if (warn_traditional_conversion
&& INTEGRAL_TYPE_P (type
)
3187 && INTEGRAL_TYPE_P (valtype
))
3189 tree would_have_been
= default_conversion (val
);
3190 tree type1
= TREE_TYPE (would_have_been
);
3192 if (TREE_CODE (type
) == ENUMERAL_TYPE
3193 && (TYPE_MAIN_VARIANT (type
)
3194 == TYPE_MAIN_VARIANT (valtype
)))
3195 /* No warning if function asks for enum
3196 and the actual arg is that enum type. */
3198 else if (formal_prec
!= TYPE_PRECISION (type1
))
3199 warning (OPT_Wtraditional_conversion
,
3200 "passing argument %d of %qE "
3201 "with different width due to prototype",
3203 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
3205 /* Don't complain if the formal parameter type
3206 is an enum, because we can't tell now whether
3207 the value was an enum--even the same enum. */
3208 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3210 else if (TREE_CODE (val
) == INTEGER_CST
3211 && int_fits_type_p (val
, type
))
3212 /* Change in signedness doesn't matter
3213 if a constant value is unaffected. */
3215 /* If the value is extended from a narrower
3216 unsigned type, it doesn't matter whether we
3217 pass it as signed or unsigned; the value
3218 certainly is the same either way. */
3219 else if (TYPE_PRECISION (valtype
) < TYPE_PRECISION (type
)
3220 && TYPE_UNSIGNED (valtype
))
3222 else if (TYPE_UNSIGNED (type
))
3223 warning (OPT_Wtraditional_conversion
,
3224 "passing argument %d of %qE "
3225 "as unsigned due to prototype",
3228 warning (OPT_Wtraditional_conversion
,
3229 "passing argument %d of %qE "
3230 "as signed due to prototype", argnum
, rname
);
3234 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3235 sake of better warnings from convert_and_check. */
3236 if (excess_precision
)
3237 val
= build1 (EXCESS_PRECISION_EXPR
, valtype
, val
);
3238 origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3239 bool arg_loc_ok
= !arg_loc
.is_empty ()
3240 /* Some __atomic_* builtins have additional
3241 hidden argument at position 0. */
3242 && values
->length () == arg_loc
.length ();
3243 parmval
= convert_for_assignment (loc
,
3244 arg_loc_ok
? arg_loc
[parmnum
]
3245 : UNKNOWN_LOCATION
, type
,
3246 val
, origtype
, ic_argpass
,
3247 npc
, fundecl
, function
,
3250 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
3251 && INTEGRAL_TYPE_P (type
)
3252 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3253 parmval
= default_conversion (parmval
);
3256 else if (TREE_CODE (valtype
) == REAL_TYPE
3257 && (TYPE_PRECISION (valtype
)
3258 <= TYPE_PRECISION (double_type_node
))
3259 && TYPE_MAIN_VARIANT (valtype
) != double_type_node
3260 && TYPE_MAIN_VARIANT (valtype
) != long_double_type_node
3261 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype
)))
3267 /* Convert `float' to `double'. */
3268 if (warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
3269 warning_at (arg_loc
[parmnum
], OPT_Wdouble_promotion
,
3270 "implicit conversion from %qT to %qT when passing "
3271 "argument to function",
3272 valtype
, double_type_node
);
3273 parmval
= convert (double_type_node
, val
);
3276 else if (excess_precision
&& !type_generic
)
3277 /* A "double" argument with excess precision being passed
3278 without a prototype or in variable arguments. */
3279 parmval
= convert (valtype
, val
);
3280 else if ((invalid_func_diag
=
3281 targetm
.calls
.invalid_arg_for_unprototyped_fn (typelist
, fundecl
, val
)))
3283 error (invalid_func_diag
);
3287 /* Convert `short' and `char' to full-size `int'. */
3288 parmval
= default_conversion (val
);
3290 (*values
)[parmnum
] = parmval
;
3291 if (parmval
== error_mark_node
)
3295 typetail
= TREE_CHAIN (typetail
);
3298 gcc_assert (parmnum
== vec_safe_length (values
));
3300 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
3302 error_at (loc
, "too few arguments to function %qE", function
);
3303 inform_declaration (fundecl
);
3307 return error_args
? -1 : (int) parmnum
;
3310 /* This is the entry point used by the parser to build unary operators
3311 in the input. CODE, a tree_code, specifies the unary operator, and
3312 ARG is the operand. For unary plus, the C parser currently uses
3313 CONVERT_EXPR for code.
3315 LOC is the location to use for the tree generated.
3319 parser_build_unary_op (location_t loc
, enum tree_code code
, struct c_expr arg
)
3321 struct c_expr result
;
3323 result
.value
= build_unary_op (loc
, code
, arg
.value
, 0);
3324 result
.original_code
= code
;
3325 result
.original_type
= NULL
;
3327 if (TREE_OVERFLOW_P (result
.value
) && !TREE_OVERFLOW_P (arg
.value
))
3328 overflow_warning (loc
, result
.value
);
3333 /* This is the entry point used by the parser to build binary operators
3334 in the input. CODE, a tree_code, specifies the binary operator, and
3335 ARG1 and ARG2 are the operands. In addition to constructing the
3336 expression, we check for operands that were written with other binary
3337 operators in a way that is likely to confuse the user.
3339 LOCATION is the location of the binary operator. */
3342 parser_build_binary_op (location_t location
, enum tree_code code
,
3343 struct c_expr arg1
, struct c_expr arg2
)
3345 struct c_expr result
;
3347 enum tree_code code1
= arg1
.original_code
;
3348 enum tree_code code2
= arg2
.original_code
;
3349 tree type1
= (arg1
.original_type
3350 ? arg1
.original_type
3351 : TREE_TYPE (arg1
.value
));
3352 tree type2
= (arg2
.original_type
3353 ? arg2
.original_type
3354 : TREE_TYPE (arg2
.value
));
3356 result
.value
= build_binary_op (location
, code
,
3357 arg1
.value
, arg2
.value
, 1);
3358 result
.original_code
= code
;
3359 result
.original_type
= NULL
;
3361 if (TREE_CODE (result
.value
) == ERROR_MARK
)
3364 if (location
!= UNKNOWN_LOCATION
)
3365 protected_set_expr_location (result
.value
, location
);
3367 /* Check for cases such as x+y<<z which users are likely
3369 if (warn_parentheses
)
3370 warn_about_parentheses (location
, code
, code1
, arg1
.value
, code2
,
3373 if (warn_logical_op
)
3374 warn_logical_operator (location
, code
, TREE_TYPE (result
.value
),
3375 code1
, arg1
.value
, code2
, arg2
.value
);
3377 /* Warn about comparisons against string literals, with the exception
3378 of testing for equality or inequality of a string literal with NULL. */
3379 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3381 if ((code1
== STRING_CST
&& !integer_zerop (arg2
.value
))
3382 || (code2
== STRING_CST
&& !integer_zerop (arg1
.value
)))
3383 warning_at (location
, OPT_Waddress
,
3384 "comparison with string literal results in unspecified behavior");
3386 else if (TREE_CODE_CLASS (code
) == tcc_comparison
3387 && (code1
== STRING_CST
|| code2
== STRING_CST
))
3388 warning_at (location
, OPT_Waddress
,
3389 "comparison with string literal results in unspecified behavior");
3391 if (TREE_OVERFLOW_P (result
.value
)
3392 && !TREE_OVERFLOW_P (arg1
.value
)
3393 && !TREE_OVERFLOW_P (arg2
.value
))
3394 overflow_warning (location
, result
.value
);
3396 /* Warn about comparisons of different enum types. */
3397 if (warn_enum_compare
3398 && TREE_CODE_CLASS (code
) == tcc_comparison
3399 && TREE_CODE (type1
) == ENUMERAL_TYPE
3400 && TREE_CODE (type2
) == ENUMERAL_TYPE
3401 && TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
3402 warning_at (location
, OPT_Wenum_compare
,
3403 "comparison between %qT and %qT",
3409 /* Return a tree for the difference of pointers OP0 and OP1.
3410 The resulting tree has type int. */
3413 pointer_diff (location_t loc
, tree op0
, tree op1
)
3415 tree restype
= ptrdiff_type_node
;
3416 tree result
, inttype
;
3418 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0
)));
3419 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1
)));
3420 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3421 tree con0
, con1
, lit0
, lit1
;
3422 tree orig_op1
= op1
;
3424 /* If the operands point into different address spaces, we need to
3425 explicitly convert them to pointers into the common address space
3426 before we can subtract the numerical address values. */
3429 addr_space_t as_common
;
3432 /* Determine the common superset address space. This is guaranteed
3433 to exist because the caller verified that comp_target_types
3434 returned non-zero. */
3435 if (!addr_space_superset (as0
, as1
, &as_common
))
3438 common_type
= common_pointer_type (TREE_TYPE (op0
), TREE_TYPE (op1
));
3439 op0
= convert (common_type
, op0
);
3440 op1
= convert (common_type
, op1
);
3443 /* Determine integer type to perform computations in. This will usually
3444 be the same as the result type (ptrdiff_t), but may need to be a wider
3445 type if pointers for the address space are wider than ptrdiff_t. */
3446 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
3447 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
3452 if (TREE_CODE (target_type
) == VOID_TYPE
)
3453 pedwarn (loc
, OPT_Wpointer_arith
,
3454 "pointer of type %<void *%> used in subtraction");
3455 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3456 pedwarn (loc
, OPT_Wpointer_arith
,
3457 "pointer to a function used in subtraction");
3459 /* If the conversion to ptrdiff_type does anything like widening or
3460 converting a partial to an integral mode, we get a convert_expression
3461 that is in the way to do any simplifications.
3462 (fold-const.c doesn't know that the extra bits won't be needed.
3463 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3464 different mode in place.)
3465 So first try to find a common term here 'by hand'; we want to cover
3466 at least the cases that occur in legal static initializers. */
3467 if (CONVERT_EXPR_P (op0
)
3468 && (TYPE_PRECISION (TREE_TYPE (op0
))
3469 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0)))))
3470 con0
= TREE_OPERAND (op0
, 0);
3473 if (CONVERT_EXPR_P (op1
)
3474 && (TYPE_PRECISION (TREE_TYPE (op1
))
3475 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1
, 0)))))
3476 con1
= TREE_OPERAND (op1
, 0);
3480 if (TREE_CODE (con0
) == POINTER_PLUS_EXPR
)
3482 lit0
= TREE_OPERAND (con0
, 1);
3483 con0
= TREE_OPERAND (con0
, 0);
3486 lit0
= integer_zero_node
;
3488 if (TREE_CODE (con1
) == POINTER_PLUS_EXPR
)
3490 lit1
= TREE_OPERAND (con1
, 1);
3491 con1
= TREE_OPERAND (con1
, 0);
3494 lit1
= integer_zero_node
;
3496 if (operand_equal_p (con0
, con1
, 0))
3503 /* First do the subtraction as integers;
3504 then drop through to build the divide operator.
3505 Do not do default conversions on the minus operator
3506 in case restype is a short type. */
3508 op0
= build_binary_op (loc
,
3509 MINUS_EXPR
, convert (inttype
, op0
),
3510 convert (inttype
, op1
), 0);
3511 /* This generates an error if op1 is pointer to incomplete type. */
3512 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
3513 error_at (loc
, "arithmetic on pointer to an incomplete type");
3515 /* This generates an error if op0 is pointer to incomplete type. */
3516 op1
= c_size_in_bytes (target_type
);
3518 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1
)))
3519 error_at (loc
, "arithmetic on pointer to an empty aggregate");
3521 /* Divide by the size, in easiest possible way. */
3522 result
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, inttype
,
3523 op0
, convert (inttype
, op1
));
3525 /* Convert to final result type if necessary. */
3526 return convert (restype
, result
);
3529 /* Expand atomic compound assignments into an approriate sequence as
3530 specified by the C11 standard section 6.5.16.2.
3536 This sequence is used for all types for which these operations are
3539 In addition, built-in versions of the 'fe' prefixed routines may
3540 need to be invoked for floating point (real, complex or vector) when
3541 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3551 __atomic_load (addr, &old, SEQ_CST);
3552 feholdexcept (&fenv);
3554 newval = old op val;
3555 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3558 feclearexcept (FE_ALL_EXCEPT);
3561 feupdateenv (&fenv);
3563 Also note that the compiler is simply issuing the generic form of
3564 the atomic operations. This requires temp(s) and has their address
3565 taken. The atomic processing is smart enough to figure out when the
3566 size of an object can utilize a lock-free version, and convert the
3567 built-in call to the appropriate lock-free routine. The optimizers
3568 will then dispose of any temps that are no longer required, and
3569 lock-free implementations are utilized as long as there is target
3570 support for the required size.
3572 If the operator is NOP_EXPR, then this is a simple assignment, and
3573 an __atomic_store is issued to perform the assignment rather than
3578 /* Build an atomic assignment at LOC, expanding into the proper
3579 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3580 the result of the operation, unless RETURN_OLD_P in which case
3581 return the old value of LHS (this is only for postincrement and
3584 build_atomic_assign (location_t loc
, tree lhs
, enum tree_code modifycode
,
3585 tree rhs
, bool return_old_p
)
3587 tree fndecl
, func_call
;
3588 vec
<tree
, va_gc
> *params
;
3589 tree val
, nonatomic_lhs_type
, nonatomic_rhs_type
, newval
, newval_addr
;
3592 tree stmt
, goto_stmt
;
3593 tree loop_label
, loop_decl
, done_label
, done_decl
;
3595 tree lhs_type
= TREE_TYPE (lhs
);
3596 tree lhs_addr
= build_unary_op (loc
, ADDR_EXPR
, lhs
, 0);
3597 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
3598 tree rhs_type
= TREE_TYPE (rhs
);
3600 gcc_assert (TYPE_ATOMIC (lhs_type
));
3603 gcc_assert (modifycode
== PLUS_EXPR
|| modifycode
== MINUS_EXPR
);
3605 /* Allocate enough vector items for a compare_exchange. */
3606 vec_alloc (params
, 6);
3608 /* Create a compound statement to hold the sequence of statements
3610 compound_stmt
= c_begin_compound_stmt (false);
3612 /* Fold the RHS if it hasn't already been folded. */
3613 if (modifycode
!= NOP_EXPR
)
3614 rhs
= c_fully_fold (rhs
, false, NULL
);
3616 /* Remove the qualifiers for the rest of the expressions and create
3617 the VAL temp variable to hold the RHS. */
3618 nonatomic_lhs_type
= build_qualified_type (lhs_type
, TYPE_UNQUALIFIED
);
3619 nonatomic_rhs_type
= build_qualified_type (rhs_type
, TYPE_UNQUALIFIED
);
3620 val
= create_tmp_var (nonatomic_rhs_type
, NULL
);
3621 TREE_ADDRESSABLE (val
) = 1;
3622 TREE_NO_WARNING (val
) = 1;
3623 rhs
= build2 (MODIFY_EXPR
, nonatomic_rhs_type
, val
, rhs
);
3624 SET_EXPR_LOCATION (rhs
, loc
);
3627 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3629 if (modifycode
== NOP_EXPR
)
3631 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3632 rhs
= build_unary_op (loc
, ADDR_EXPR
, val
, 0);
3633 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
3634 params
->quick_push (lhs_addr
);
3635 params
->quick_push (rhs
);
3636 params
->quick_push (seq_cst
);
3637 func_call
= build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3638 add_stmt (func_call
);
3640 /* Finish the compound statement. */
3641 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3643 /* VAL is the value which was stored, return a COMPOUND_STMT of
3644 the statement and that value. */
3645 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, val
);
3648 /* Create the variables and labels required for the op= form. */
3649 old
= create_tmp_var (nonatomic_lhs_type
, NULL
);
3650 old_addr
= build_unary_op (loc
, ADDR_EXPR
, old
, 0);
3651 TREE_ADDRESSABLE (old
) = 1;
3652 TREE_NO_WARNING (old
) = 1;
3654 newval
= create_tmp_var (nonatomic_lhs_type
, NULL
);
3655 newval_addr
= build_unary_op (loc
, ADDR_EXPR
, newval
, 0);
3656 TREE_ADDRESSABLE (newval
) = 1;
3658 loop_decl
= create_artificial_label (loc
);
3659 loop_label
= build1 (LABEL_EXPR
, void_type_node
, loop_decl
);
3661 done_decl
= create_artificial_label (loc
);
3662 done_label
= build1 (LABEL_EXPR
, void_type_node
, done_decl
);
3664 /* __atomic_load (addr, &old, SEQ_CST). */
3665 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
3666 params
->quick_push (lhs_addr
);
3667 params
->quick_push (old_addr
);
3668 params
->quick_push (seq_cst
);
3669 func_call
= build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3670 add_stmt (func_call
);
3671 params
->truncate (0);
3673 /* Create the expressions for floating-point environment
3674 manipulation, if required. */
3675 bool need_fenv
= (flag_trapping_math
3676 && (FLOAT_TYPE_P (lhs_type
) || FLOAT_TYPE_P (rhs_type
)));
3677 tree hold_call
= NULL_TREE
, clear_call
= NULL_TREE
, update_call
= NULL_TREE
;
3679 targetm
.atomic_assign_expand_fenv (&hold_call
, &clear_call
, &update_call
);
3682 add_stmt (hold_call
);
3685 add_stmt (loop_label
);
3687 /* newval = old + val; */
3688 rhs
= build_binary_op (loc
, modifycode
, old
, val
, 1);
3689 rhs
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, nonatomic_lhs_type
,
3690 rhs
, NULL_TREE
, ic_assign
, false, NULL_TREE
,
3692 if (rhs
!= error_mark_node
)
3694 rhs
= build2 (MODIFY_EXPR
, nonatomic_lhs_type
, newval
, rhs
);
3695 SET_EXPR_LOCATION (rhs
, loc
);
3699 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3701 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE
);
3702 params
->quick_push (lhs_addr
);
3703 params
->quick_push (old_addr
);
3704 params
->quick_push (newval_addr
);
3705 params
->quick_push (integer_zero_node
);
3706 params
->quick_push (seq_cst
);
3707 params
->quick_push (seq_cst
);
3708 func_call
= build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3710 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, done_decl
);
3711 SET_EXPR_LOCATION (goto_stmt
, loc
);
3713 stmt
= build3 (COND_EXPR
, void_type_node
, func_call
, goto_stmt
, NULL_TREE
);
3714 SET_EXPR_LOCATION (stmt
, loc
);
3718 add_stmt (clear_call
);
3721 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, loop_decl
);
3722 SET_EXPR_LOCATION (goto_stmt
, loc
);
3723 add_stmt (goto_stmt
);
3726 add_stmt (done_label
);
3729 add_stmt (update_call
);
3731 /* Finish the compound statement. */
3732 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3734 /* NEWVAL is the value that was successfully stored, return a
3735 COMPOUND_EXPR of the statement and the appropriate value. */
3736 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
,
3737 return_old_p
? old
: newval
);
3740 /* Construct and perhaps optimize a tree representation
3741 for a unary operation. CODE, a tree_code, specifies the operation
3742 and XARG is the operand.
3743 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3744 the default promotions (such as from short to int).
3745 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3746 allows non-lvalues; this is only used to handle conversion of non-lvalue
3747 arrays to pointers in C99.
3749 LOCATION is the location of the operator. */
3752 build_unary_op (location_t location
,
3753 enum tree_code code
, tree xarg
, int flag
)
3755 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3758 enum tree_code typecode
;
3760 tree ret
= error_mark_node
;
3761 tree eptype
= NULL_TREE
;
3762 int noconvert
= flag
;
3763 const char *invalid_op_diag
;
3766 int_operands
= EXPR_INT_CONST_OPERANDS (xarg
);
3768 arg
= remove_c_maybe_const_expr (arg
);
3770 if (code
!= ADDR_EXPR
)
3771 arg
= require_complete_type (arg
);
3773 typecode
= TREE_CODE (TREE_TYPE (arg
));
3774 if (typecode
== ERROR_MARK
)
3775 return error_mark_node
;
3776 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
3777 typecode
= INTEGER_TYPE
;
3779 if ((invalid_op_diag
3780 = targetm
.invalid_unary_op (code
, TREE_TYPE (xarg
))))
3782 error_at (location
, invalid_op_diag
);
3783 return error_mark_node
;
3786 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
3788 eptype
= TREE_TYPE (arg
);
3789 arg
= TREE_OPERAND (arg
, 0);
3795 /* This is used for unary plus, because a CONVERT_EXPR
3796 is enough to prevent anybody from looking inside for
3797 associativity, but won't generate any code. */
3798 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3799 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3800 || typecode
== VECTOR_TYPE
))
3802 error_at (location
, "wrong type argument to unary plus");
3803 return error_mark_node
;
3805 else if (!noconvert
)
3806 arg
= default_conversion (arg
);
3807 arg
= non_lvalue_loc (location
, arg
);
3811 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3812 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3813 || typecode
== VECTOR_TYPE
))
3815 error_at (location
, "wrong type argument to unary minus");
3816 return error_mark_node
;
3818 else if (!noconvert
)
3819 arg
= default_conversion (arg
);
3823 /* ~ works on integer types and non float vectors. */
3824 if (typecode
== INTEGER_TYPE
3825 || (typecode
== VECTOR_TYPE
3826 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg
))))
3829 arg
= default_conversion (arg
);
3831 else if (typecode
== COMPLEX_TYPE
)
3834 pedwarn (location
, OPT_Wpedantic
,
3835 "ISO C does not support %<~%> for complex conjugation");
3837 arg
= default_conversion (arg
);
3841 error_at (location
, "wrong type argument to bit-complement");
3842 return error_mark_node
;
3847 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
3849 error_at (location
, "wrong type argument to abs");
3850 return error_mark_node
;
3852 else if (!noconvert
)
3853 arg
= default_conversion (arg
);
3857 /* Conjugating a real value is a no-op, but allow it anyway. */
3858 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3859 || typecode
== COMPLEX_TYPE
))
3861 error_at (location
, "wrong type argument to conjugation");
3862 return error_mark_node
;
3864 else if (!noconvert
)
3865 arg
= default_conversion (arg
);
3868 case TRUTH_NOT_EXPR
:
3869 if (typecode
!= INTEGER_TYPE
&& typecode
!= FIXED_POINT_TYPE
3870 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
3871 && typecode
!= COMPLEX_TYPE
)
3874 "wrong type argument to unary exclamation mark");
3875 return error_mark_node
;
3879 arg
= c_objc_common_truthvalue_conversion (location
, xarg
);
3880 arg
= remove_c_maybe_const_expr (arg
);
3883 arg
= c_objc_common_truthvalue_conversion (location
, arg
);
3884 ret
= invert_truthvalue_loc (location
, arg
);
3885 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3886 if (EXPR_P (ret
) && EXPR_HAS_LOCATION (ret
))
3887 location
= EXPR_LOCATION (ret
);
3888 goto return_build_unary_op
;
3892 ret
= build_real_imag_expr (location
, code
, arg
);
3893 if (ret
== error_mark_node
)
3894 return error_mark_node
;
3895 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
3896 eptype
= TREE_TYPE (eptype
);
3897 goto return_build_unary_op
;
3899 case PREINCREMENT_EXPR
:
3900 case POSTINCREMENT_EXPR
:
3901 case PREDECREMENT_EXPR
:
3902 case POSTDECREMENT_EXPR
:
3904 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
3906 tree inner
= build_unary_op (location
, code
,
3907 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
3908 if (inner
== error_mark_node
)
3909 return error_mark_node
;
3910 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
3911 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
3912 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
3913 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = 1;
3914 goto return_build_unary_op
;
3917 /* Complain about anything that is not a true lvalue. In
3918 Objective-C, skip this check for property_refs. */
3919 if (!objc_is_property_ref (arg
)
3920 && !lvalue_or_else (location
,
3921 arg
, ((code
== PREINCREMENT_EXPR
3922 || code
== POSTINCREMENT_EXPR
)
3925 return error_mark_node
;
3927 if (warn_cxx_compat
&& TREE_CODE (TREE_TYPE (arg
)) == ENUMERAL_TYPE
)
3929 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3930 warning_at (location
, OPT_Wc___compat
,
3931 "increment of enumeration value is invalid in C++");
3933 warning_at (location
, OPT_Wc___compat
,
3934 "decrement of enumeration value is invalid in C++");
3937 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3938 arg
= c_fully_fold (arg
, false, NULL
);
3941 atomic_op
= really_atomic_lvalue (arg
);
3943 /* Increment or decrement the real part of the value,
3944 and don't change the imaginary part. */
3945 if (typecode
== COMPLEX_TYPE
)
3949 pedwarn (location
, OPT_Wpedantic
,
3950 "ISO C does not support %<++%> and %<--%> on complex types");
3954 arg
= stabilize_reference (arg
);
3955 real
= build_unary_op (EXPR_LOCATION (arg
), REALPART_EXPR
, arg
, 1);
3956 imag
= build_unary_op (EXPR_LOCATION (arg
), IMAGPART_EXPR
, arg
, 1);
3957 real
= build_unary_op (EXPR_LOCATION (arg
), code
, real
, 1);
3958 if (real
== error_mark_node
|| imag
== error_mark_node
)
3959 return error_mark_node
;
3960 ret
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
3962 goto return_build_unary_op
;
3966 /* Report invalid types. */
3968 if (typecode
!= POINTER_TYPE
&& typecode
!= FIXED_POINT_TYPE
3969 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
3970 && typecode
!= COMPLEX_TYPE
&& typecode
!= VECTOR_TYPE
)
3972 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3973 error_at (location
, "wrong type argument to increment");
3975 error_at (location
, "wrong type argument to decrement");
3977 return error_mark_node
;
3983 argtype
= TREE_TYPE (arg
);
3985 /* Compute the increment. */
3987 if (typecode
== POINTER_TYPE
)
3989 /* If pointer target is an undefined struct,
3990 we just cannot know how to do the arithmetic. */
3991 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype
)))
3993 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3995 "increment of pointer to unknown structure");
3998 "decrement of pointer to unknown structure");
4000 else if (TREE_CODE (TREE_TYPE (argtype
)) == FUNCTION_TYPE
4001 || TREE_CODE (TREE_TYPE (argtype
)) == VOID_TYPE
)
4003 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4004 pedwarn (location
, OPT_Wpointer_arith
,
4005 "wrong type argument to increment");
4007 pedwarn (location
, OPT_Wpointer_arith
,
4008 "wrong type argument to decrement");
4011 inc
= c_size_in_bytes (TREE_TYPE (argtype
));
4012 inc
= convert_to_ptrofftype_loc (location
, inc
);
4014 else if (FRACT_MODE_P (TYPE_MODE (argtype
)))
4016 /* For signed fract types, we invert ++ to -- or
4017 -- to ++, and change inc from 1 to -1, because
4018 it is not possible to represent 1 in signed fract constants.
4019 For unsigned fract types, the result always overflows and
4020 we get an undefined (original) or the maximum value. */
4021 if (code
== PREINCREMENT_EXPR
)
4022 code
= PREDECREMENT_EXPR
;
4023 else if (code
== PREDECREMENT_EXPR
)
4024 code
= PREINCREMENT_EXPR
;
4025 else if (code
== POSTINCREMENT_EXPR
)
4026 code
= POSTDECREMENT_EXPR
;
4027 else /* code == POSTDECREMENT_EXPR */
4028 code
= POSTINCREMENT_EXPR
;
4030 inc
= integer_minus_one_node
;
4031 inc
= convert (argtype
, inc
);
4035 inc
= VECTOR_TYPE_P (argtype
)
4036 ? build_one_cst (argtype
)
4038 inc
= convert (argtype
, inc
);
4041 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4042 need to ask Objective-C to build the increment or decrement
4043 expression for it. */
4044 if (objc_is_property_ref (arg
))
4045 return objc_build_incr_expr_for_property_ref (location
, code
,
4048 /* Report a read-only lvalue. */
4049 if (TYPE_READONLY (argtype
))
4051 readonly_error (location
, arg
,
4052 ((code
== PREINCREMENT_EXPR
4053 || code
== POSTINCREMENT_EXPR
)
4054 ? lv_increment
: lv_decrement
));
4055 return error_mark_node
;
4057 else if (TREE_READONLY (arg
))
4058 readonly_warning (arg
,
4059 ((code
== PREINCREMENT_EXPR
4060 || code
== POSTINCREMENT_EXPR
)
4061 ? lv_increment
: lv_decrement
));
4063 /* If the argument is atomic, use the special code sequences for
4064 atomic compound assignment. */
4067 arg
= stabilize_reference (arg
);
4068 ret
= build_atomic_assign (location
, arg
,
4069 ((code
== PREINCREMENT_EXPR
4070 || code
== POSTINCREMENT_EXPR
)
4073 (FRACT_MODE_P (TYPE_MODE (argtype
))
4075 : integer_one_node
),
4076 (code
== POSTINCREMENT_EXPR
4077 || code
== POSTDECREMENT_EXPR
));
4078 goto return_build_unary_op
;
4081 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4082 val
= boolean_increment (code
, arg
);
4084 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
4085 TREE_SIDE_EFFECTS (val
) = 1;
4086 if (TREE_CODE (val
) != code
)
4087 TREE_NO_WARNING (val
) = 1;
4089 goto return_build_unary_op
;
4093 /* Note that this operation never does default_conversion. */
4095 /* The operand of unary '&' must be an lvalue (which excludes
4096 expressions of type void), or, in C99, the result of a [] or
4097 unary '*' operator. */
4098 if (VOID_TYPE_P (TREE_TYPE (arg
))
4099 && TYPE_QUALS (TREE_TYPE (arg
)) == TYPE_UNQUALIFIED
4100 && (TREE_CODE (arg
) != INDIRECT_REF
4102 pedwarn (location
, 0, "taking address of expression of type %<void%>");
4104 /* Let &* cancel out to simplify resulting code. */
4105 if (TREE_CODE (arg
) == INDIRECT_REF
)
4107 /* Don't let this be an lvalue. */
4108 if (lvalue_p (TREE_OPERAND (arg
, 0)))
4109 return non_lvalue_loc (location
, TREE_OPERAND (arg
, 0));
4110 ret
= TREE_OPERAND (arg
, 0);
4111 goto return_build_unary_op
;
4114 /* For &x[y], return x+y */
4115 if (TREE_CODE (arg
) == ARRAY_REF
)
4117 tree op0
= TREE_OPERAND (arg
, 0);
4118 if (!c_mark_addressable (op0
))
4119 return error_mark_node
;
4122 /* Anything not already handled and not a true memory reference
4123 or a non-lvalue array is an error. */
4124 else if (typecode
!= FUNCTION_TYPE
&& !flag
4125 && !lvalue_or_else (location
, arg
, lv_addressof
))
4126 return error_mark_node
;
4128 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4130 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4132 tree inner
= build_unary_op (location
, code
,
4133 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
4134 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4135 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4136 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4137 C_MAYBE_CONST_EXPR_NON_CONST (ret
)
4138 = C_MAYBE_CONST_EXPR_NON_CONST (arg
);
4139 goto return_build_unary_op
;
4142 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4143 argtype
= TREE_TYPE (arg
);
4145 /* If the lvalue is const or volatile, merge that into the type
4146 to which the address will point. This is only needed
4147 for function types. */
4148 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
4149 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4150 && TREE_CODE (argtype
) == FUNCTION_TYPE
)
4152 int orig_quals
= TYPE_QUALS (strip_array_types (argtype
));
4153 int quals
= orig_quals
;
4155 if (TREE_READONLY (arg
))
4156 quals
|= TYPE_QUAL_CONST
;
4157 if (TREE_THIS_VOLATILE (arg
))
4158 quals
|= TYPE_QUAL_VOLATILE
;
4160 argtype
= c_build_qualified_type (argtype
, quals
);
4163 if (!c_mark_addressable (arg
))
4164 return error_mark_node
;
4166 gcc_assert (TREE_CODE (arg
) != COMPONENT_REF
4167 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)));
4169 argtype
= build_pointer_type (argtype
);
4171 /* ??? Cope with user tricks that amount to offsetof. Delete this
4172 when we have proper support for integer constant expressions. */
4173 val
= get_base_address (arg
);
4174 if (val
&& TREE_CODE (val
) == INDIRECT_REF
4175 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
4177 ret
= fold_convert_loc (location
, argtype
, fold_offsetof_1 (arg
));
4178 goto return_build_unary_op
;
4181 val
= build1 (ADDR_EXPR
, argtype
, arg
);
4184 goto return_build_unary_op
;
4191 argtype
= TREE_TYPE (arg
);
4192 if (TREE_CODE (arg
) == INTEGER_CST
)
4193 ret
= (require_constant_value
4194 ? fold_build1_initializer_loc (location
, code
, argtype
, arg
)
4195 : fold_build1_loc (location
, code
, argtype
, arg
));
4197 ret
= build1 (code
, argtype
, arg
);
4198 return_build_unary_op
:
4199 gcc_assert (ret
!= error_mark_node
);
4200 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
)
4201 && !(TREE_CODE (xarg
) == INTEGER_CST
&& !TREE_OVERFLOW (xarg
)))
4202 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
4203 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
)
4204 ret
= note_integer_operands (ret
);
4206 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4207 protected_set_expr_location (ret
, location
);
4211 /* Return nonzero if REF is an lvalue valid for this language.
4212 Lvalues can be assigned, unless their type has TYPE_READONLY.
4213 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4216 lvalue_p (const_tree ref
)
4218 const enum tree_code code
= TREE_CODE (ref
);
4225 return lvalue_p (TREE_OPERAND (ref
, 0));
4227 case C_MAYBE_CONST_EXPR
:
4228 return lvalue_p (TREE_OPERAND (ref
, 1));
4230 case COMPOUND_LITERAL_EXPR
:
4236 case ARRAY_NOTATION_REF
:
4241 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
4242 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
4245 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
4252 /* Give a warning for storing in something that is read-only in GCC
4253 terms but not const in ISO C terms. */
4256 readonly_warning (tree arg
, enum lvalue_use use
)
4261 warning (0, "assignment of read-only location %qE", arg
);
4264 warning (0, "increment of read-only location %qE", arg
);
4267 warning (0, "decrement of read-only location %qE", arg
);
4276 /* Return nonzero if REF is an lvalue valid for this language;
4277 otherwise, print an error message and return zero. USE says
4278 how the lvalue is being used and so selects the error message.
4279 LOCATION is the location at which any error should be reported. */
4282 lvalue_or_else (location_t loc
, const_tree ref
, enum lvalue_use use
)
4284 int win
= lvalue_p (ref
);
4287 lvalue_error (loc
, use
);
4292 /* Mark EXP saying that we need to be able to take the
4293 address of it; it should not be allocated in a register.
4294 Returns true if successful. */
4297 c_mark_addressable (tree exp
)
4302 switch (TREE_CODE (x
))
4305 if (DECL_C_BIT_FIELD (TREE_OPERAND (x
, 1)))
4308 ("cannot take address of bit-field %qD", TREE_OPERAND (x
, 1));
4312 /* ... fall through ... */
4318 x
= TREE_OPERAND (x
, 0);
4321 case COMPOUND_LITERAL_EXPR
:
4323 TREE_ADDRESSABLE (x
) = 1;
4330 if (C_DECL_REGISTER (x
)
4331 && DECL_NONLOCAL (x
))
4333 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
4336 ("global register variable %qD used in nested function", x
);
4339 pedwarn (input_location
, 0, "register variable %qD used in nested function", x
);
4341 else if (C_DECL_REGISTER (x
))
4343 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
4344 error ("address of global register variable %qD requested", x
);
4346 error ("address of register variable %qD requested", x
);
4352 TREE_ADDRESSABLE (x
) = 1;
4359 /* Convert EXPR to TYPE, warning about conversion problems with
4360 constants. SEMANTIC_TYPE is the type this conversion would use
4361 without excess precision. If SEMANTIC_TYPE is NULL, this function
4362 is equivalent to convert_and_check. This function is a wrapper that
4363 handles conversions that may be different than
4364 the usual ones because of excess precision. */
4367 ep_convert_and_check (location_t loc
, tree type
, tree expr
,
4370 if (TREE_TYPE (expr
) == type
)
4374 return convert_and_check (loc
, type
, expr
);
4376 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
4377 && TREE_TYPE (expr
) != semantic_type
)
4379 /* For integers, we need to check the real conversion, not
4380 the conversion to the excess precision type. */
4381 expr
= convert_and_check (loc
, semantic_type
, expr
);
4383 /* Result type is the excess precision type, which should be
4384 large enough, so do not check. */
4385 return convert (type
, expr
);
4388 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4389 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4390 if folded to an integer constant then the unselected half may
4391 contain arbitrary operations not normally permitted in constant
4392 expressions. Set the location of the expression to LOC. */
4395 build_conditional_expr (location_t colon_loc
, tree ifexp
, bool ifexp_bcp
,
4396 tree op1
, tree op1_original_type
, tree op2
,
4397 tree op2_original_type
)
4401 enum tree_code code1
;
4402 enum tree_code code2
;
4403 tree result_type
= NULL
;
4404 tree semantic_result_type
= NULL
;
4405 tree orig_op1
= op1
, orig_op2
= op2
;
4406 bool int_const
, op1_int_operands
, op2_int_operands
, int_operands
;
4407 bool ifexp_int_operands
;
4410 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
4411 if (op1_int_operands
)
4412 op1
= remove_c_maybe_const_expr (op1
);
4413 op2_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op2
);
4414 if (op2_int_operands
)
4415 op2
= remove_c_maybe_const_expr (op2
);
4416 ifexp_int_operands
= EXPR_INT_CONST_OPERANDS (ifexp
);
4417 if (ifexp_int_operands
)
4418 ifexp
= remove_c_maybe_const_expr (ifexp
);
4420 /* Promote both alternatives. */
4422 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
4423 op1
= default_conversion (op1
);
4424 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
4425 op2
= default_conversion (op2
);
4427 if (TREE_CODE (ifexp
) == ERROR_MARK
4428 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
4429 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
4430 return error_mark_node
;
4432 type1
= TREE_TYPE (op1
);
4433 code1
= TREE_CODE (type1
);
4434 type2
= TREE_TYPE (op2
);
4435 code2
= TREE_CODE (type2
);
4437 /* C90 does not permit non-lvalue arrays in conditional expressions.
4438 In C99 they will be pointers by now. */
4439 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
4441 error_at (colon_loc
, "non-lvalue array in conditional expression");
4442 return error_mark_node
;
4445 if ((TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
4446 || TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4447 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4448 || code1
== COMPLEX_TYPE
)
4449 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4450 || code2
== COMPLEX_TYPE
))
4452 semantic_result_type
= c_common_type (type1
, type2
);
4453 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
4455 op1
= TREE_OPERAND (op1
, 0);
4456 type1
= TREE_TYPE (op1
);
4457 gcc_assert (TREE_CODE (type1
) == code1
);
4459 if (TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4461 op2
= TREE_OPERAND (op2
, 0);
4462 type2
= TREE_TYPE (op2
);
4463 gcc_assert (TREE_CODE (type2
) == code2
);
4467 if (warn_cxx_compat
)
4469 tree t1
= op1_original_type
? op1_original_type
: TREE_TYPE (orig_op1
);
4470 tree t2
= op2_original_type
? op2_original_type
: TREE_TYPE (orig_op2
);
4472 if (TREE_CODE (t1
) == ENUMERAL_TYPE
4473 && TREE_CODE (t2
) == ENUMERAL_TYPE
4474 && TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
4475 warning_at (colon_loc
, OPT_Wc___compat
,
4476 ("different enum types in conditional is "
4477 "invalid in C++: %qT vs %qT"),
4481 /* Quickly detect the usual case where op1 and op2 have the same type
4483 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
4486 result_type
= type1
;
4488 result_type
= TYPE_MAIN_VARIANT (type1
);
4490 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4491 || code1
== COMPLEX_TYPE
)
4492 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4493 || code2
== COMPLEX_TYPE
))
4495 result_type
= c_common_type (type1
, type2
);
4496 do_warn_double_promotion (result_type
, type1
, type2
,
4497 "implicit conversion from %qT to %qT to "
4498 "match other result of conditional",
4501 /* If -Wsign-compare, warn here if type1 and type2 have
4502 different signedness. We'll promote the signed to unsigned
4503 and later code won't know it used to be different.
4504 Do this check on the original types, so that explicit casts
4505 will be considered, but default promotions won't. */
4506 if (c_inhibit_evaluation_warnings
== 0)
4508 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
4509 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
4511 if (unsigned_op1
^ unsigned_op2
)
4515 /* Do not warn if the result type is signed, since the
4516 signed type will only be chosen if it can represent
4517 all the values of the unsigned type. */
4518 if (!TYPE_UNSIGNED (result_type
))
4522 bool op1_maybe_const
= true;
4523 bool op2_maybe_const
= true;
4525 /* Do not warn if the signed quantity is an
4526 unsuffixed integer literal (or some static
4527 constant expression involving such literals) and
4528 it is non-negative. This warning requires the
4529 operands to be folded for best results, so do
4530 that folding in this case even without
4531 warn_sign_compare to avoid warning options
4532 possibly affecting code generation. */
4533 c_inhibit_evaluation_warnings
4534 += (ifexp
== truthvalue_false_node
);
4535 op1
= c_fully_fold (op1
, require_constant_value
,
4537 c_inhibit_evaluation_warnings
4538 -= (ifexp
== truthvalue_false_node
);
4540 c_inhibit_evaluation_warnings
4541 += (ifexp
== truthvalue_true_node
);
4542 op2
= c_fully_fold (op2
, require_constant_value
,
4544 c_inhibit_evaluation_warnings
4545 -= (ifexp
== truthvalue_true_node
);
4547 if (warn_sign_compare
)
4550 && tree_expr_nonnegative_warnv_p (op1
, &ovf
))
4552 && tree_expr_nonnegative_warnv_p (op2
, &ovf
)))
4555 warning_at (colon_loc
, OPT_Wsign_compare
,
4556 ("signed and unsigned type in "
4557 "conditional expression"));
4559 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
4560 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
4561 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
4562 op2
= c_wrap_maybe_const (op2
, !op2_maybe_const
);
4567 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
4569 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
4570 pedwarn (colon_loc
, OPT_Wpedantic
,
4571 "ISO C forbids conditional expr with only one void side");
4572 result_type
= void_type_node
;
4574 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
4576 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
4577 addr_space_t as2
= TYPE_ADDR_SPACE (TREE_TYPE (type2
));
4578 addr_space_t as_common
;
4580 if (comp_target_types (colon_loc
, type1
, type2
))
4581 result_type
= common_pointer_type (type1
, type2
);
4582 else if (null_pointer_constant_p (orig_op1
))
4583 result_type
= type2
;
4584 else if (null_pointer_constant_p (orig_op2
))
4585 result_type
= type1
;
4586 else if (!addr_space_superset (as1
, as2
, &as_common
))
4588 error_at (colon_loc
, "pointers to disjoint address spaces "
4589 "used in conditional expression");
4590 return error_mark_node
;
4592 else if (VOID_TYPE_P (TREE_TYPE (type1
))
4593 && !TYPE_ATOMIC (TREE_TYPE (type1
)))
4595 if (TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
4596 pedwarn (colon_loc
, OPT_Wpedantic
,
4597 "ISO C forbids conditional expr between "
4598 "%<void *%> and function pointer");
4599 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
4600 TREE_TYPE (type2
)));
4602 else if (VOID_TYPE_P (TREE_TYPE (type2
))
4603 && !TYPE_ATOMIC (TREE_TYPE (type2
)))
4605 if (TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
4606 pedwarn (colon_loc
, OPT_Wpedantic
,
4607 "ISO C forbids conditional expr between "
4608 "%<void *%> and function pointer");
4609 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
4610 TREE_TYPE (type1
)));
4612 /* Objective-C pointer comparisons are a bit more lenient. */
4613 else if (objc_have_common_type (type1
, type2
, -3, NULL_TREE
))
4614 result_type
= objc_common_type (type1
, type2
);
4617 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
4619 pedwarn (colon_loc
, 0,
4620 "pointer type mismatch in conditional expression");
4621 result_type
= build_pointer_type
4622 (build_qualified_type (void_type_node
, qual
));
4625 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
4627 if (!null_pointer_constant_p (orig_op2
))
4628 pedwarn (colon_loc
, 0,
4629 "pointer/integer type mismatch in conditional expression");
4632 op2
= null_pointer_node
;
4634 result_type
= type1
;
4636 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4638 if (!null_pointer_constant_p (orig_op1
))
4639 pedwarn (colon_loc
, 0,
4640 "pointer/integer type mismatch in conditional expression");
4643 op1
= null_pointer_node
;
4645 result_type
= type2
;
4650 if (flag_cond_mismatch
)
4651 result_type
= void_type_node
;
4654 error_at (colon_loc
, "type mismatch in conditional expression");
4655 return error_mark_node
;
4659 /* Merge const and volatile flags of the incoming types. */
4661 = build_type_variant (result_type
,
4662 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
4663 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
4665 op1
= ep_convert_and_check (colon_loc
, result_type
, op1
,
4666 semantic_result_type
);
4667 op2
= ep_convert_and_check (colon_loc
, result_type
, op2
,
4668 semantic_result_type
);
4670 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
4672 op2_int_operands
= true;
4673 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
4675 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
4677 op1_int_operands
= true;
4678 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
4680 int_const
= int_operands
= (ifexp_int_operands
4682 && op2_int_operands
);
4685 int_const
= ((ifexp
== truthvalue_true_node
4686 && TREE_CODE (orig_op1
) == INTEGER_CST
4687 && !TREE_OVERFLOW (orig_op1
))
4688 || (ifexp
== truthvalue_false_node
4689 && TREE_CODE (orig_op2
) == INTEGER_CST
4690 && !TREE_OVERFLOW (orig_op2
)));
4692 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
4693 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4698 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4699 nested inside of the expression. */
4700 op1
= c_fully_fold (op1
, false, NULL
);
4701 op2
= c_fully_fold (op2
, false, NULL
);
4703 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4705 ret
= note_integer_operands (ret
);
4707 if (semantic_result_type
)
4708 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
4710 protected_set_expr_location (ret
, colon_loc
);
4714 /* Return a compound expression that performs two expressions and
4715 returns the value of the second of them.
4717 LOC is the location of the COMPOUND_EXPR. */
4720 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
4722 bool expr1_int_operands
, expr2_int_operands
;
4723 tree eptype
= NULL_TREE
;
4727 && (TREE_CODE (expr1
) == CILK_SPAWN_STMT
4728 || TREE_CODE (expr2
) == CILK_SPAWN_STMT
))
4731 "spawned function call cannot be part of a comma expression");
4732 return error_mark_node
;
4734 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
4735 if (expr1_int_operands
)
4736 expr1
= remove_c_maybe_const_expr (expr1
);
4737 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
4738 if (expr2_int_operands
)
4739 expr2
= remove_c_maybe_const_expr (expr2
);
4741 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
4742 expr1
= TREE_OPERAND (expr1
, 0);
4743 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
4745 eptype
= TREE_TYPE (expr2
);
4746 expr2
= TREE_OPERAND (expr2
, 0);
4749 if (!TREE_SIDE_EFFECTS (expr1
))
4751 /* The left-hand operand of a comma expression is like an expression
4752 statement: with -Wunused, we should warn if it doesn't have
4753 any side-effects, unless it was explicitly cast to (void). */
4754 if (warn_unused_value
)
4756 if (VOID_TYPE_P (TREE_TYPE (expr1
))
4757 && CONVERT_EXPR_P (expr1
))
4759 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
4760 && TREE_CODE (expr1
) == COMPOUND_EXPR
4761 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
4762 ; /* (void) a, (void) b, c */
4764 warning_at (loc
, OPT_Wunused_value
,
4765 "left-hand operand of comma expression has no effect");
4768 else if (TREE_CODE (expr1
) == COMPOUND_EXPR
4769 && warn_unused_value
)
4772 location_t cloc
= loc
;
4773 while (TREE_CODE (r
) == COMPOUND_EXPR
)
4775 if (EXPR_HAS_LOCATION (r
))
4776 cloc
= EXPR_LOCATION (r
);
4777 r
= TREE_OPERAND (r
, 1);
4779 if (!TREE_SIDE_EFFECTS (r
)
4780 && !VOID_TYPE_P (TREE_TYPE (r
))
4781 && !CONVERT_EXPR_P (r
))
4782 warning_at (cloc
, OPT_Wunused_value
,
4783 "right-hand operand of comma expression has no effect");
4786 /* With -Wunused, we should also warn if the left-hand operand does have
4787 side-effects, but computes a value which is not used. For example, in
4788 `foo() + bar(), baz()' the result of the `+' operator is not used,
4789 so we should issue a warning. */
4790 else if (warn_unused_value
)
4791 warn_if_unused_value (expr1
, loc
);
4793 if (expr2
== error_mark_node
)
4794 return error_mark_node
;
4796 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
4799 && expr1_int_operands
4800 && expr2_int_operands
)
4801 ret
= note_integer_operands (ret
);
4804 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4806 protected_set_expr_location (ret
, loc
);
4810 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4811 which we are casting. OTYPE is the type of the expression being
4812 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4813 of the cast. -Wcast-qual appeared on the command line. Named
4814 address space qualifiers are not handled here, because they result
4815 in different warnings. */
4818 handle_warn_cast_qual (location_t loc
, tree type
, tree otype
)
4820 tree in_type
= type
;
4821 tree in_otype
= otype
;
4826 /* Check that the qualifiers on IN_TYPE are a superset of the
4827 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4828 nodes is uninteresting and we stop as soon as we hit a
4829 non-POINTER_TYPE node on either type. */
4832 in_otype
= TREE_TYPE (in_otype
);
4833 in_type
= TREE_TYPE (in_type
);
4835 /* GNU C allows cv-qualified function types. 'const' means the
4836 function is very pure, 'volatile' means it can't return. We
4837 need to warn when such qualifiers are added, not when they're
4839 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
4840 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
4841 added
|= (TYPE_QUALS_NO_ADDR_SPACE (in_type
)
4842 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype
));
4844 discarded
|= (TYPE_QUALS_NO_ADDR_SPACE (in_otype
)
4845 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type
));
4847 while (TREE_CODE (in_type
) == POINTER_TYPE
4848 && TREE_CODE (in_otype
) == POINTER_TYPE
);
4851 warning_at (loc
, OPT_Wcast_qual
,
4852 "cast adds %q#v qualifier to function type", added
);
4855 /* There are qualifiers present in IN_OTYPE that are not present
4857 warning_at (loc
, OPT_Wcast_qual
,
4858 "cast discards %q#v qualifier from pointer target type",
4861 if (added
|| discarded
)
4864 /* A cast from **T to const **T is unsafe, because it can cause a
4865 const value to be changed with no additional warning. We only
4866 issue this warning if T is the same on both sides, and we only
4867 issue the warning if there are the same number of pointers on
4868 both sides, as otherwise the cast is clearly unsafe anyhow. A
4869 cast is unsafe when a qualifier is added at one level and const
4870 is not present at all outer levels.
4872 To issue this warning, we check at each level whether the cast
4873 adds new qualifiers not already seen. We don't need to special
4874 case function types, as they won't have the same
4875 TYPE_MAIN_VARIANT. */
4877 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
4879 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
4884 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
4887 in_type
= TREE_TYPE (in_type
);
4888 in_otype
= TREE_TYPE (in_otype
);
4889 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
4892 warning_at (loc
, OPT_Wcast_qual
,
4893 "to be safe all intermediate pointers in cast from "
4894 "%qT to %qT must be %<const%> qualified",
4899 is_const
= TYPE_READONLY (in_type
);
4901 while (TREE_CODE (in_type
) == POINTER_TYPE
);
4904 /* Build an expression representing a cast to type TYPE of expression EXPR.
4905 LOC is the location of the cast-- typically the open paren of the cast. */
4908 build_c_cast (location_t loc
, tree type
, tree expr
)
4912 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
4913 expr
= TREE_OPERAND (expr
, 0);
4917 if (type
== error_mark_node
|| expr
== error_mark_node
)
4918 return error_mark_node
;
4920 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4921 only in <protocol> qualifications. But when constructing cast expressions,
4922 the protocols do matter and must be kept around. */
4923 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
4924 return build1 (NOP_EXPR
, type
, expr
);
4926 type
= TYPE_MAIN_VARIANT (type
);
4928 if (TREE_CODE (type
) == ARRAY_TYPE
)
4930 error_at (loc
, "cast specifies array type");
4931 return error_mark_node
;
4934 if (TREE_CODE (type
) == FUNCTION_TYPE
)
4936 error_at (loc
, "cast specifies function type");
4937 return error_mark_node
;
4940 if (!VOID_TYPE_P (type
))
4942 value
= require_complete_type (value
);
4943 if (value
== error_mark_node
)
4944 return error_mark_node
;
4947 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
4949 if (TREE_CODE (type
) == RECORD_TYPE
4950 || TREE_CODE (type
) == UNION_TYPE
)
4951 pedwarn (loc
, OPT_Wpedantic
,
4952 "ISO C forbids casting nonscalar to the same type");
4954 else if (TREE_CODE (type
) == UNION_TYPE
)
4958 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4959 if (TREE_TYPE (field
) != error_mark_node
4960 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
4961 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
4967 bool maybe_const
= true;
4969 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids casts to union type");
4970 t
= c_fully_fold (value
, false, &maybe_const
);
4971 t
= build_constructor_single (type
, field
, t
);
4973 t
= c_wrap_maybe_const (t
, true);
4974 t
= digest_init (loc
, type
, t
,
4975 NULL_TREE
, false, true, 0);
4976 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
4979 error_at (loc
, "cast to union type from type not present in union");
4980 return error_mark_node
;
4986 if (type
== void_type_node
)
4988 tree t
= build1 (CONVERT_EXPR
, type
, value
);
4989 SET_EXPR_LOCATION (t
, loc
);
4993 otype
= TREE_TYPE (value
);
4995 /* Optionally warn about potentially worrisome casts. */
4997 && TREE_CODE (type
) == POINTER_TYPE
4998 && TREE_CODE (otype
) == POINTER_TYPE
)
4999 handle_warn_cast_qual (loc
, type
, otype
);
5001 /* Warn about conversions between pointers to disjoint
5003 if (TREE_CODE (type
) == POINTER_TYPE
5004 && TREE_CODE (otype
) == POINTER_TYPE
5005 && !null_pointer_constant_p (value
))
5007 addr_space_t as_to
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
5008 addr_space_t as_from
= TYPE_ADDR_SPACE (TREE_TYPE (otype
));
5009 addr_space_t as_common
;
5011 if (!addr_space_superset (as_to
, as_from
, &as_common
))
5013 if (ADDR_SPACE_GENERIC_P (as_from
))
5014 warning_at (loc
, 0, "cast to %s address space pointer "
5015 "from disjoint generic address space pointer",
5016 c_addr_space_name (as_to
));
5018 else if (ADDR_SPACE_GENERIC_P (as_to
))
5019 warning_at (loc
, 0, "cast to generic address space pointer "
5020 "from disjoint %s address space pointer",
5021 c_addr_space_name (as_from
));
5024 warning_at (loc
, 0, "cast to %s address space pointer "
5025 "from disjoint %s address space pointer",
5026 c_addr_space_name (as_to
),
5027 c_addr_space_name (as_from
));
5031 /* Warn about possible alignment problems. */
5032 if (STRICT_ALIGNMENT
5033 && TREE_CODE (type
) == POINTER_TYPE
5034 && TREE_CODE (otype
) == POINTER_TYPE
5035 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5036 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5037 /* Don't warn about opaque types, where the actual alignment
5038 restriction is unknown. */
5039 && !((TREE_CODE (TREE_TYPE (otype
)) == UNION_TYPE
5040 || TREE_CODE (TREE_TYPE (otype
)) == RECORD_TYPE
)
5041 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
5042 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5043 warning_at (loc
, OPT_Wcast_align
,
5044 "cast increases required alignment of target type");
5046 if (TREE_CODE (type
) == INTEGER_TYPE
5047 && TREE_CODE (otype
) == POINTER_TYPE
5048 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5049 /* Unlike conversion of integers to pointers, where the
5050 warning is disabled for converting constants because
5051 of cases such as SIG_*, warn about converting constant
5052 pointers to integers. In some cases it may cause unwanted
5053 sign extension, and a warning is appropriate. */
5054 warning_at (loc
, OPT_Wpointer_to_int_cast
,
5055 "cast from pointer to integer of different size");
5057 if (TREE_CODE (value
) == CALL_EXPR
5058 && TREE_CODE (type
) != TREE_CODE (otype
))
5059 warning_at (loc
, OPT_Wbad_function_cast
,
5060 "cast from function call of type %qT "
5061 "to non-matching type %qT", otype
, type
);
5063 if (TREE_CODE (type
) == POINTER_TYPE
5064 && TREE_CODE (otype
) == INTEGER_TYPE
5065 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5066 /* Don't warn about converting any constant. */
5067 && !TREE_CONSTANT (value
))
5069 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
5070 "of different size");
5072 if (warn_strict_aliasing
<= 2)
5073 strict_aliasing_warning (otype
, type
, expr
);
5075 /* If pedantic, warn for conversions between function and object
5076 pointer types, except for converting a null pointer constant
5077 to function pointer type. */
5079 && TREE_CODE (type
) == POINTER_TYPE
5080 && TREE_CODE (otype
) == POINTER_TYPE
5081 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
5082 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
5083 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5084 "conversion of function pointer to object pointer type");
5087 && TREE_CODE (type
) == POINTER_TYPE
5088 && TREE_CODE (otype
) == POINTER_TYPE
5089 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
5090 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5091 && !null_pointer_constant_p (value
))
5092 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5093 "conversion of object pointer to function pointer type");
5096 value
= convert (type
, value
);
5098 /* Ignore any integer overflow caused by the cast. */
5099 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
5101 if (CONSTANT_CLASS_P (ovalue
) && TREE_OVERFLOW (ovalue
))
5103 if (!TREE_OVERFLOW (value
))
5105 /* Avoid clobbering a shared constant. */
5106 value
= copy_node (value
);
5107 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5110 else if (TREE_OVERFLOW (value
))
5111 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5112 value
= build_int_cst_wide (TREE_TYPE (value
),
5113 TREE_INT_CST_LOW (value
),
5114 TREE_INT_CST_HIGH (value
));
5118 /* Don't let a cast be an lvalue. */
5120 value
= non_lvalue_loc (loc
, value
);
5122 /* Don't allow the results of casting to floating-point or complex
5123 types be confused with actual constants, or casts involving
5124 integer and pointer types other than direct integer-to-integer
5125 and integer-to-pointer be confused with integer constant
5126 expressions and null pointer constants. */
5127 if (TREE_CODE (value
) == REAL_CST
5128 || TREE_CODE (value
) == COMPLEX_CST
5129 || (TREE_CODE (value
) == INTEGER_CST
5130 && !((TREE_CODE (expr
) == INTEGER_CST
5131 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
5132 || TREE_CODE (expr
) == REAL_CST
5133 || TREE_CODE (expr
) == COMPLEX_CST
)))
5134 value
= build1 (NOP_EXPR
, type
, value
);
5136 if (CAN_HAVE_LOCATION_P (value
))
5137 SET_EXPR_LOCATION (value
, loc
);
5141 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5142 location of the open paren of the cast, or the position of the cast
5145 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
5148 tree type_expr
= NULL_TREE
;
5149 bool type_expr_const
= true;
5151 int saved_wsp
= warn_strict_prototypes
;
5153 /* This avoids warnings about unprototyped casts on
5154 integers. E.g. "#define SIG_DFL (void(*)())0". */
5155 if (TREE_CODE (expr
) == INTEGER_CST
)
5156 warn_strict_prototypes
= 0;
5157 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
5158 warn_strict_prototypes
= saved_wsp
;
5160 ret
= build_c_cast (loc
, type
, expr
);
5163 bool inner_expr_const
= true;
5164 ret
= c_fully_fold (ret
, require_constant_value
, &inner_expr_const
);
5165 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
5166 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !(type_expr_const
5167 && inner_expr_const
);
5168 SET_EXPR_LOCATION (ret
, loc
);
5171 if (CAN_HAVE_LOCATION_P (ret
) && !EXPR_HAS_LOCATION (ret
))
5172 SET_EXPR_LOCATION (ret
, loc
);
5174 /* C++ does not permits types to be defined in a cast, but it
5175 allows references to incomplete types. */
5176 if (warn_cxx_compat
&& type_name
->specs
->typespec_kind
== ctsk_tagdef
)
5177 warning_at (loc
, OPT_Wc___compat
,
5178 "defining a type in a cast is invalid in C++");
5183 /* Build an assignment expression of lvalue LHS from value RHS.
5184 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5185 may differ from TREE_TYPE (LHS) for an enum bitfield.
5186 MODIFYCODE is the code for a binary operator that we use
5187 to combine the old value of LHS with RHS to get the new value.
5188 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5189 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5190 which may differ from TREE_TYPE (RHS) for an enum value.
5192 LOCATION is the location of the MODIFYCODE operator.
5193 RHS_LOC is the location of the RHS. */
5196 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
5197 enum tree_code modifycode
,
5198 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
5202 tree rhseval
= NULL_TREE
;
5203 tree rhs_semantic_type
= NULL_TREE
;
5204 tree lhstype
= TREE_TYPE (lhs
);
5205 tree olhstype
= lhstype
;
5209 /* Types that aren't fully specified cannot be used in assignments. */
5210 lhs
= require_complete_type (lhs
);
5212 /* Avoid duplicate error messages from operands that had errors. */
5213 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
5214 return error_mark_node
;
5216 /* Ensure an error for assigning a non-lvalue array to an array in
5218 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
5220 error_at (location
, "assignment to expression with array type");
5221 return error_mark_node
;
5224 /* For ObjC properties, defer this check. */
5225 if (!objc_is_property_ref (lhs
) && !lvalue_or_else (location
, lhs
, lv_assign
))
5226 return error_mark_node
;
5228 is_atomic_op
= really_atomic_lvalue (lhs
);
5230 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5232 rhs_semantic_type
= TREE_TYPE (rhs
);
5233 rhs
= TREE_OPERAND (rhs
, 0);
5238 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
5240 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
5241 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
5243 if (inner
== error_mark_node
)
5244 return error_mark_node
;
5245 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
5246 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
5247 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
5248 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
5249 protected_set_expr_location (result
, location
);
5253 /* If a binary op has been requested, combine the old LHS value with the RHS
5254 producing the value we should actually store into the LHS. */
5256 if (modifycode
!= NOP_EXPR
)
5258 lhs
= c_fully_fold (lhs
, false, NULL
);
5259 lhs
= stabilize_reference (lhs
);
5261 /* Construct the RHS for any non-atomic compound assignemnt. */
5264 /* If in LHS op= RHS the RHS has side-effects, ensure they
5265 are preevaluated before the rest of the assignment expression's
5266 side-effects, because RHS could contain e.g. function calls
5268 if (TREE_SIDE_EFFECTS (rhs
))
5270 newrhs
= in_late_binary_op
? save_expr (rhs
) : c_save_expr (rhs
);
5273 newrhs
= build_binary_op (location
,
5274 modifycode
, lhs
, newrhs
, 1);
5276 /* The original type of the right hand side is no longer
5278 rhs_origtype
= NULL_TREE
;
5282 if (c_dialect_objc ())
5284 /* Check if we are modifying an Objective-C property reference;
5285 if so, we need to generate setter calls. */
5286 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
5290 /* Else, do the check that we postponed for Objective-C. */
5291 if (!lvalue_or_else (location
, lhs
, lv_assign
))
5292 return error_mark_node
;
5295 /* Give an error for storing in something that is 'const'. */
5297 if (TYPE_READONLY (lhstype
)
5298 || ((TREE_CODE (lhstype
) == RECORD_TYPE
5299 || TREE_CODE (lhstype
) == UNION_TYPE
)
5300 && C_TYPE_FIELDS_READONLY (lhstype
)))
5302 readonly_error (location
, lhs
, lv_assign
);
5303 return error_mark_node
;
5305 else if (TREE_READONLY (lhs
))
5306 readonly_warning (lhs
, lv_assign
);
5308 /* If storing into a structure or union member,
5309 it has probably been given type `int'.
5310 Compute the type that would go with
5311 the actual amount of storage the member occupies. */
5313 if (TREE_CODE (lhs
) == COMPONENT_REF
5314 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5315 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
5316 || TREE_CODE (lhstype
) == REAL_TYPE
5317 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5318 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5320 /* If storing in a field that is in actuality a short or narrower than one,
5321 we must store in the field in its actual type. */
5323 if (lhstype
!= TREE_TYPE (lhs
))
5325 lhs
= copy_node (lhs
);
5326 TREE_TYPE (lhs
) = lhstype
;
5329 /* Issue -Wc++-compat warnings about an assignment to an enum type
5330 when LHS does not have its original type. This happens for,
5331 e.g., an enum bitfield in a struct. */
5333 && lhs_origtype
!= NULL_TREE
5334 && lhs_origtype
!= lhstype
5335 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
5337 tree checktype
= (rhs_origtype
!= NULL_TREE
5340 if (checktype
!= error_mark_node
5341 && (TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
)
5342 || (is_atomic_op
&& modifycode
!= NOP_EXPR
)))
5343 warning_at (location
, OPT_Wc___compat
,
5344 "enum conversion in assignment is invalid in C++");
5347 /* If the lhs is atomic, remove that qualifier. */
5350 lhstype
= build_qualified_type (lhstype
,
5351 (TYPE_QUALS (lhstype
)
5352 & ~TYPE_QUAL_ATOMIC
));
5353 olhstype
= build_qualified_type (olhstype
,
5354 (TYPE_QUALS (lhstype
)
5355 & ~TYPE_QUAL_ATOMIC
));
5358 /* Convert new value to destination type. Fold it first, then
5359 restore any excess precision information, for the sake of
5360 conversion warnings. */
5362 if (!(is_atomic_op
&& modifycode
!= NOP_EXPR
))
5364 npc
= null_pointer_constant_p (newrhs
);
5365 newrhs
= c_fully_fold (newrhs
, false, NULL
);
5366 if (rhs_semantic_type
)
5367 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
5368 newrhs
= convert_for_assignment (location
, rhs_loc
, lhstype
, newrhs
,
5369 rhs_origtype
, ic_assign
, npc
,
5370 NULL_TREE
, NULL_TREE
, 0);
5371 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5372 return error_mark_node
;
5375 /* Emit ObjC write barrier, if necessary. */
5376 if (c_dialect_objc () && flag_objc_gc
)
5378 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
5381 protected_set_expr_location (result
, location
);
5386 /* Scan operands. */
5389 result
= build_atomic_assign (location
, lhs
, modifycode
, newrhs
, false);
5392 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
5393 TREE_SIDE_EFFECTS (result
) = 1;
5394 protected_set_expr_location (result
, location
);
5397 /* If we got the LHS in a different type for storing in,
5398 convert the result back to the nominal type of LHS
5399 so that the value we return always has the same type
5400 as the LHS argument. */
5402 if (olhstype
== TREE_TYPE (result
))
5405 result
= convert_for_assignment (location
, rhs_loc
, olhstype
, result
,
5406 rhs_origtype
, ic_assign
, false, NULL_TREE
,
5408 protected_set_expr_location (result
, location
);
5412 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), rhseval
, result
);
5416 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5417 This is used to implement -fplan9-extensions. */
5420 find_anonymous_field_with_type (tree struct_type
, tree type
)
5425 gcc_assert (TREE_CODE (struct_type
) == RECORD_TYPE
5426 || TREE_CODE (struct_type
) == UNION_TYPE
);
5428 for (field
= TYPE_FIELDS (struct_type
);
5430 field
= TREE_CHAIN (field
))
5432 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5433 ? c_build_qualified_type (TREE_TYPE (field
),
5435 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5436 if (DECL_NAME (field
) == NULL
5437 && comptypes (type
, fieldtype
))
5443 else if (DECL_NAME (field
) == NULL
5444 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
5445 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
5446 && find_anonymous_field_with_type (TREE_TYPE (field
), type
))
5456 /* RHS is an expression whose type is pointer to struct. If there is
5457 an anonymous field in RHS with type TYPE, then return a pointer to
5458 that field in RHS. This is used with -fplan9-extensions. This
5459 returns NULL if no conversion could be found. */
5462 convert_to_anonymous_field (location_t location
, tree type
, tree rhs
)
5464 tree rhs_struct_type
, lhs_main_type
;
5465 tree field
, found_field
;
5466 bool found_sub_field
;
5469 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs
)));
5470 rhs_struct_type
= TREE_TYPE (TREE_TYPE (rhs
));
5471 gcc_assert (TREE_CODE (rhs_struct_type
) == RECORD_TYPE
5472 || TREE_CODE (rhs_struct_type
) == UNION_TYPE
);
5474 gcc_assert (POINTER_TYPE_P (type
));
5475 lhs_main_type
= (TYPE_ATOMIC (TREE_TYPE (type
))
5476 ? c_build_qualified_type (TREE_TYPE (type
),
5478 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
5480 found_field
= NULL_TREE
;
5481 found_sub_field
= false;
5482 for (field
= TYPE_FIELDS (rhs_struct_type
);
5484 field
= TREE_CHAIN (field
))
5486 if (DECL_NAME (field
) != NULL_TREE
5487 || (TREE_CODE (TREE_TYPE (field
)) != RECORD_TYPE
5488 && TREE_CODE (TREE_TYPE (field
)) != UNION_TYPE
))
5490 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5491 ? c_build_qualified_type (TREE_TYPE (field
),
5493 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5494 if (comptypes (lhs_main_type
, fieldtype
))
5496 if (found_field
!= NULL_TREE
)
5498 found_field
= field
;
5500 else if (find_anonymous_field_with_type (TREE_TYPE (field
),
5503 if (found_field
!= NULL_TREE
)
5505 found_field
= field
;
5506 found_sub_field
= true;
5510 if (found_field
== NULL_TREE
)
5513 ret
= fold_build3_loc (location
, COMPONENT_REF
, TREE_TYPE (found_field
),
5514 build_fold_indirect_ref (rhs
), found_field
,
5516 ret
= build_fold_addr_expr_loc (location
, ret
);
5518 if (found_sub_field
)
5520 ret
= convert_to_anonymous_field (location
, type
, ret
);
5521 gcc_assert (ret
!= NULL_TREE
);
5527 /* Convert value RHS to type TYPE as preparation for an assignment to
5528 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5529 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5530 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5531 constant before any folding.
5532 The real work of conversion is done by `convert'.
5533 The purpose of this function is to generate error messages
5534 for assignments that are not allowed in C.
5535 ERRTYPE says whether it is argument passing, assignment,
5536 initialization or return.
5538 LOCATION is the location of the assignment, EXPR_LOC is the location of
5539 the RHS or, for a function, location of an argument.
5540 FUNCTION is a tree for the function being called.
5541 PARMNUM is the number of the argument, for printing in error messages. */
5544 convert_for_assignment (location_t location
, location_t expr_loc
, tree type
,
5545 tree rhs
, tree origtype
, enum impl_conv errtype
,
5546 bool null_pointer_constant
, tree fundecl
,
5547 tree function
, int parmnum
)
5549 enum tree_code codel
= TREE_CODE (type
);
5550 tree orig_rhs
= rhs
;
5552 enum tree_code coder
;
5553 tree rname
= NULL_TREE
;
5554 bool objc_ok
= false;
5556 if (errtype
== ic_argpass
)
5559 /* Change pointer to function to the function itself for
5561 if (TREE_CODE (function
) == ADDR_EXPR
5562 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
5563 function
= TREE_OPERAND (function
, 0);
5565 /* Handle an ObjC selector specially for diagnostics. */
5566 selector
= objc_message_selector ();
5568 if (selector
&& parmnum
> 2)
5575 /* This macro is used to emit diagnostics to ensure that all format
5576 strings are complete sentences, visible to gettext and checked at
5578 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5583 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5584 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5585 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5586 "expected %qT but argument is of type %qT", \
5590 pedwarn (LOCATION, OPT, AS); \
5593 pedwarn_init (LOCATION, OPT, IN); \
5596 pedwarn (LOCATION, OPT, RE); \
5599 gcc_unreachable (); \
5603 /* This macro is used to emit diagnostics to ensure that all format
5604 strings are complete sentences, visible to gettext and checked at
5605 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5606 extra parameter to enumerate qualifiers. */
5608 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5613 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5614 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5615 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5616 "expected %qT but argument is of type %qT", \
5620 pedwarn (LOCATION, OPT, AS, QUALS); \
5623 pedwarn (LOCATION, OPT, IN, QUALS); \
5626 pedwarn (LOCATION, OPT, RE, QUALS); \
5629 gcc_unreachable (); \
5633 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5634 rhs
= TREE_OPERAND (rhs
, 0);
5636 rhstype
= TREE_TYPE (rhs
);
5637 coder
= TREE_CODE (rhstype
);
5639 if (coder
== ERROR_MARK
)
5640 return error_mark_node
;
5642 if (c_dialect_objc ())
5665 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
5668 if (warn_cxx_compat
)
5670 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
5671 if (checktype
!= error_mark_node
5672 && TREE_CODE (type
) == ENUMERAL_TYPE
5673 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
5675 WARN_FOR_ASSIGNMENT (input_location
, OPT_Wc___compat
,
5676 G_("enum conversion when passing argument "
5677 "%d of %qE is invalid in C++"),
5678 G_("enum conversion in assignment is "
5680 G_("enum conversion in initialization is "
5682 G_("enum conversion in return is "
5687 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
5690 if (coder
== VOID_TYPE
)
5692 /* Except for passing an argument to an unprototyped function,
5693 this is a constraint violation. When passing an argument to
5694 an unprototyped function, it is compile-time undefined;
5695 making it a constraint in that case was rejected in
5697 error_at (location
, "void value not ignored as it ought to be");
5698 return error_mark_node
;
5700 rhs
= require_complete_type (rhs
);
5701 if (rhs
== error_mark_node
)
5702 return error_mark_node
;
5703 /* A non-reference type can convert to a reference. This handles
5704 va_start, va_copy and possibly port built-ins. */
5705 if (codel
== REFERENCE_TYPE
&& coder
!= REFERENCE_TYPE
)
5707 if (!lvalue_p (rhs
))
5709 error_at (location
, "cannot pass rvalue to reference parameter");
5710 return error_mark_node
;
5712 if (!c_mark_addressable (rhs
))
5713 return error_mark_node
;
5714 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
5715 SET_EXPR_LOCATION (rhs
, location
);
5717 rhs
= convert_for_assignment (location
, expr_loc
,
5718 build_pointer_type (TREE_TYPE (type
)),
5719 rhs
, origtype
, errtype
,
5720 null_pointer_constant
, fundecl
, function
,
5722 if (rhs
== error_mark_node
)
5723 return error_mark_node
;
5725 rhs
= build1 (NOP_EXPR
, type
, rhs
);
5726 SET_EXPR_LOCATION (rhs
, location
);
5729 /* Some types can interconvert without explicit casts. */
5730 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
5731 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
5732 return convert (type
, rhs
);
5733 /* Arithmetic types all interconvert, and enum is treated like int. */
5734 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
5735 || codel
== FIXED_POINT_TYPE
5736 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
5737 || codel
== BOOLEAN_TYPE
)
5738 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
5739 || coder
== FIXED_POINT_TYPE
5740 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
5741 || coder
== BOOLEAN_TYPE
))
5744 bool save
= in_late_binary_op
;
5745 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
)
5746 in_late_binary_op
= true;
5747 ret
= convert_and_check (expr_loc
!= UNKNOWN_LOCATION
5748 ? expr_loc
: location
, type
, orig_rhs
);
5749 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
)
5750 in_late_binary_op
= save
;
5754 /* Aggregates in different TUs might need conversion. */
5755 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
5757 && comptypes (type
, rhstype
))
5758 return convert_and_check (expr_loc
!= UNKNOWN_LOCATION
5759 ? expr_loc
: location
, type
, rhs
);
5761 /* Conversion to a transparent union or record from its member types.
5762 This applies only to function arguments. */
5763 if (((codel
== UNION_TYPE
|| codel
== RECORD_TYPE
)
5764 && TYPE_TRANSPARENT_AGGR (type
))
5765 && errtype
== ic_argpass
)
5767 tree memb
, marginal_memb
= NULL_TREE
;
5769 for (memb
= TYPE_FIELDS (type
); memb
; memb
= DECL_CHAIN (memb
))
5771 tree memb_type
= TREE_TYPE (memb
);
5773 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
5774 TYPE_MAIN_VARIANT (rhstype
)))
5777 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
5780 if (coder
== POINTER_TYPE
)
5782 tree ttl
= TREE_TYPE (memb_type
);
5783 tree ttr
= TREE_TYPE (rhstype
);
5785 /* Any non-function converts to a [const][volatile] void *
5786 and vice versa; otherwise, targets must be the same.
5787 Meanwhile, the lhs target must have all the qualifiers of
5789 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
5790 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
5791 || comp_target_types (location
, memb_type
, rhstype
))
5793 int lquals
= TYPE_QUALS (ttl
) & ~TYPE_QUAL_ATOMIC
;
5794 int rquals
= TYPE_QUALS (ttr
) & ~TYPE_QUAL_ATOMIC
;
5795 /* If this type won't generate any warnings, use it. */
5796 if (lquals
== rquals
5797 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
5798 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
5799 ? ((lquals
| rquals
) == rquals
)
5800 : ((lquals
| rquals
) == lquals
)))
5803 /* Keep looking for a better type, but remember this one. */
5805 marginal_memb
= memb
;
5809 /* Can convert integer zero to any pointer type. */
5810 if (null_pointer_constant
)
5812 rhs
= null_pointer_node
;
5817 if (memb
|| marginal_memb
)
5821 /* We have only a marginally acceptable member type;
5822 it needs a warning. */
5823 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
5824 tree ttr
= TREE_TYPE (rhstype
);
5826 /* Const and volatile mean something different for function
5827 types, so the usual warnings are not appropriate. */
5828 if (TREE_CODE (ttr
) == FUNCTION_TYPE
5829 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
5831 /* Because const and volatile on functions are
5832 restrictions that say the function will not do
5833 certain things, it is okay to use a const or volatile
5834 function where an ordinary one is wanted, but not
5836 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
5837 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
5838 WARN_FOR_QUALIFIERS (location
, 0,
5839 G_("passing argument %d of %qE "
5840 "makes %q#v qualified function "
5841 "pointer from unqualified"),
5842 G_("assignment makes %q#v qualified "
5843 "function pointer from "
5845 G_("initialization makes %q#v qualified "
5846 "function pointer from "
5848 G_("return makes %q#v qualified function "
5849 "pointer from unqualified"),
5850 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
5852 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr
)
5853 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl
))
5854 WARN_FOR_QUALIFIERS (location
, 0,
5855 G_("passing argument %d of %qE discards "
5856 "%qv qualifier from pointer target type"),
5857 G_("assignment discards %qv qualifier "
5858 "from pointer target type"),
5859 G_("initialization discards %qv qualifier "
5860 "from pointer target type"),
5861 G_("return discards %qv qualifier from "
5862 "pointer target type"),
5863 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
5865 memb
= marginal_memb
;
5868 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
5869 pedwarn (location
, OPT_Wpedantic
,
5870 "ISO C prohibits argument conversion to union type");
5872 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
5873 return build_constructor_single (type
, memb
, rhs
);
5877 /* Conversions among pointers */
5878 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
5879 && (coder
== codel
))
5881 tree ttl
= TREE_TYPE (type
);
5882 tree ttr
= TREE_TYPE (rhstype
);
5885 bool is_opaque_pointer
;
5886 int target_cmp
= 0; /* Cache comp_target_types () result. */
5890 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
5891 mvl
= (TYPE_ATOMIC (mvl
)
5892 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
),
5894 : TYPE_MAIN_VARIANT (mvl
));
5895 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
5896 mvr
= (TYPE_ATOMIC (mvr
)
5897 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
),
5899 : TYPE_MAIN_VARIANT (mvr
));
5900 /* Opaque pointers are treated like void pointers. */
5901 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
5903 /* The Plan 9 compiler permits a pointer to a struct to be
5904 automatically converted into a pointer to an anonymous field
5905 within the struct. */
5906 if (flag_plan9_extensions
5907 && (TREE_CODE (mvl
) == RECORD_TYPE
|| TREE_CODE(mvl
) == UNION_TYPE
)
5908 && (TREE_CODE (mvr
) == RECORD_TYPE
|| TREE_CODE(mvr
) == UNION_TYPE
)
5911 tree new_rhs
= convert_to_anonymous_field (location
, type
, rhs
);
5912 if (new_rhs
!= NULL_TREE
)
5915 rhstype
= TREE_TYPE (rhs
);
5916 coder
= TREE_CODE (rhstype
);
5917 ttr
= TREE_TYPE (rhstype
);
5918 mvr
= TYPE_MAIN_VARIANT (ttr
);
5922 /* C++ does not allow the implicit conversion void* -> T*. However,
5923 for the purpose of reducing the number of false positives, we
5924 tolerate the special case of
5928 where NULL is typically defined in C to be '(void *) 0'. */
5929 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
5930 warning_at (location
, OPT_Wc___compat
,
5931 "request for implicit conversion "
5932 "from %qT to %qT not permitted in C++", rhstype
, type
);
5934 /* See if the pointers point to incompatible address spaces. */
5935 asl
= TYPE_ADDR_SPACE (ttl
);
5936 asr
= TYPE_ADDR_SPACE (ttr
);
5937 if (!null_pointer_constant_p (rhs
)
5938 && asr
!= asl
&& !targetm
.addr_space
.subset_p (asr
, asl
))
5943 error_at (location
, "passing argument %d of %qE from pointer to "
5944 "non-enclosed address space", parmnum
, rname
);
5947 error_at (location
, "assignment from pointer to "
5948 "non-enclosed address space");
5951 error_at (location
, "initialization from pointer to "
5952 "non-enclosed address space");
5955 error_at (location
, "return from pointer to "
5956 "non-enclosed address space");
5961 return error_mark_node
;
5964 /* Check if the right-hand side has a format attribute but the
5965 left-hand side doesn't. */
5966 if (warn_suggest_attribute_format
5967 && check_missing_format_attribute (type
, rhstype
))
5972 warning_at (location
, OPT_Wsuggest_attribute_format
,
5973 "argument %d of %qE might be "
5974 "a candidate for a format attribute",
5978 warning_at (location
, OPT_Wsuggest_attribute_format
,
5979 "assignment left-hand side might be "
5980 "a candidate for a format attribute");
5983 warning_at (location
, OPT_Wsuggest_attribute_format
,
5984 "initialization left-hand side might be "
5985 "a candidate for a format attribute");
5988 warning_at (location
, OPT_Wsuggest_attribute_format
,
5989 "return type might be "
5990 "a candidate for a format attribute");
5997 /* Any non-function converts to a [const][volatile] void *
5998 and vice versa; otherwise, targets must be the same.
5999 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6000 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
6001 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
6002 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
6003 || is_opaque_pointer
6004 || ((c_common_unsigned_type (mvl
)
6005 == c_common_unsigned_type (mvr
))
6006 && (c_common_signed_type (mvl
)
6007 == c_common_signed_type (mvr
))
6008 && TYPE_ATOMIC (mvl
) == TYPE_ATOMIC (mvr
)))
6011 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6014 && !null_pointer_constant
6015 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
6016 WARN_FOR_ASSIGNMENT (location
, OPT_Wpedantic
,
6017 G_("ISO C forbids passing argument %d of "
6018 "%qE between function pointer "
6020 G_("ISO C forbids assignment between "
6021 "function pointer and %<void *%>"),
6022 G_("ISO C forbids initialization between "
6023 "function pointer and %<void *%>"),
6024 G_("ISO C forbids return between function "
6025 "pointer and %<void *%>"));
6026 /* Const and volatile mean something different for function types,
6027 so the usual warnings are not appropriate. */
6028 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
6029 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
6031 /* Assignments between atomic and non-atomic objects are OK. */
6032 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
6033 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
6035 WARN_FOR_QUALIFIERS (location
, 0,
6036 G_("passing argument %d of %qE discards "
6037 "%qv qualifier from pointer target type"),
6038 G_("assignment discards %qv qualifier "
6039 "from pointer target type"),
6040 G_("initialization discards %qv qualifier "
6041 "from pointer target type"),
6042 G_("return discards %qv qualifier from "
6043 "pointer target type"),
6044 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6046 /* If this is not a case of ignoring a mismatch in signedness,
6048 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
6051 /* If there is a mismatch, do warn. */
6052 else if (warn_pointer_sign
)
6053 WARN_FOR_ASSIGNMENT (location
, OPT_Wpointer_sign
,
6054 G_("pointer targets in passing argument "
6055 "%d of %qE differ in signedness"),
6056 G_("pointer targets in assignment "
6057 "differ in signedness"),
6058 G_("pointer targets in initialization "
6059 "differ in signedness"),
6060 G_("pointer targets in return differ "
6063 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
6064 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6066 /* Because const and volatile on functions are restrictions
6067 that say the function will not do certain things,
6068 it is okay to use a const or volatile function
6069 where an ordinary one is wanted, but not vice-versa. */
6070 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6071 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6072 WARN_FOR_QUALIFIERS (location
, 0,
6073 G_("passing argument %d of %qE makes "
6074 "%q#v qualified function pointer "
6075 "from unqualified"),
6076 G_("assignment makes %q#v qualified function "
6077 "pointer from unqualified"),
6078 G_("initialization makes %q#v qualified "
6079 "function pointer from unqualified"),
6080 G_("return makes %q#v qualified function "
6081 "pointer from unqualified"),
6082 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6086 /* Avoid warning about the volatile ObjC EH puts on decls. */
6088 WARN_FOR_ASSIGNMENT (location
, 0,
6089 G_("passing argument %d of %qE from "
6090 "incompatible pointer type"),
6091 G_("assignment from incompatible pointer type"),
6092 G_("initialization from incompatible "
6094 G_("return from incompatible pointer type"));
6096 return convert (type
, rhs
);
6098 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
6100 /* ??? This should not be an error when inlining calls to
6101 unprototyped functions. */
6102 error_at (location
, "invalid use of non-lvalue array");
6103 return error_mark_node
;
6105 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
6107 /* An explicit constant 0 can convert to a pointer,
6108 or one that results from arithmetic, even including
6109 a cast to integer type. */
6110 if (!null_pointer_constant
)
6111 WARN_FOR_ASSIGNMENT (location
, 0,
6112 G_("passing argument %d of %qE makes "
6113 "pointer from integer without a cast"),
6114 G_("assignment makes pointer from integer "
6116 G_("initialization makes pointer from "
6117 "integer without a cast"),
6118 G_("return makes pointer from integer "
6121 return convert (type
, rhs
);
6123 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
6125 WARN_FOR_ASSIGNMENT (location
, 0,
6126 G_("passing argument %d of %qE makes integer "
6127 "from pointer without a cast"),
6128 G_("assignment makes integer from pointer "
6130 G_("initialization makes integer from pointer "
6132 G_("return makes integer from pointer "
6134 return convert (type
, rhs
);
6136 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
6139 bool save
= in_late_binary_op
;
6140 in_late_binary_op
= true;
6141 ret
= convert (type
, rhs
);
6142 in_late_binary_op
= save
;
6149 error_at (location
, "incompatible type for argument %d of %qE", parmnum
, rname
);
6150 inform ((fundecl
&& !DECL_IS_BUILTIN (fundecl
))
6151 ? DECL_SOURCE_LOCATION (fundecl
) : input_location
,
6152 "expected %qT but argument is of type %qT", type
, rhstype
);
6155 error_at (location
, "incompatible types when assigning to type %qT from "
6156 "type %qT", type
, rhstype
);
6160 "incompatible types when initializing type %qT using type %qT",
6165 "incompatible types when returning type %qT but %qT was "
6166 "expected", rhstype
, type
);
6172 return error_mark_node
;
6175 /* If VALUE is a compound expr all of whose expressions are constant, then
6176 return its value. Otherwise, return error_mark_node.
6178 This is for handling COMPOUND_EXPRs as initializer elements
6179 which is allowed with a warning when -pedantic is specified. */
6182 valid_compound_expr_initializer (tree value
, tree endtype
)
6184 if (TREE_CODE (value
) == COMPOUND_EXPR
)
6186 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
6188 return error_mark_node
;
6189 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
6192 else if (!initializer_constant_valid_p (value
, endtype
))
6193 return error_mark_node
;
6198 /* Perform appropriate conversions on the initial value of a variable,
6199 store it in the declaration DECL,
6200 and print any error messages that are appropriate.
6201 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6202 If the init is invalid, store an ERROR_MARK.
6204 INIT_LOC is the location of the initial value. */
6207 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
6212 /* If variable's type was invalidly declared, just ignore it. */
6214 type
= TREE_TYPE (decl
);
6215 if (TREE_CODE (type
) == ERROR_MARK
)
6218 /* Digest the specified initializer into an expression. */
6221 npc
= null_pointer_constant_p (init
);
6222 value
= digest_init (init_loc
, type
, init
, origtype
, npc
,
6223 true, TREE_STATIC (decl
));
6225 /* Store the expression if valid; else report error. */
6227 if (!in_system_header_at (input_location
)
6228 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
6229 warning (OPT_Wtraditional
, "traditional C rejects automatic "
6230 "aggregate initialization");
6232 DECL_INITIAL (decl
) = value
;
6234 /* ANSI wants warnings about out-of-range constant initializers. */
6235 STRIP_TYPE_NOPS (value
);
6236 if (TREE_STATIC (decl
))
6237 constant_expression_warning (value
);
6239 /* Check if we need to set array size from compound literal size. */
6240 if (TREE_CODE (type
) == ARRAY_TYPE
6241 && TYPE_DOMAIN (type
) == 0
6242 && value
!= error_mark_node
)
6244 tree inside_init
= init
;
6246 STRIP_TYPE_NOPS (inside_init
);
6247 inside_init
= fold (inside_init
);
6249 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6251 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6253 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
6255 /* For int foo[] = (int [3]){1}; we need to set array size
6256 now since later on array initializer will be just the
6257 brace enclosed list of the compound literal. */
6258 tree etype
= strip_array_types (TREE_TYPE (decl
));
6259 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6260 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
6262 layout_decl (cldecl
, 0);
6264 = c_build_qualified_type (type
, TYPE_QUALS (etype
));
6270 /* Methods for storing and printing names for error messages. */
6272 /* Implement a spelling stack that allows components of a name to be pushed
6273 and popped. Each element on the stack is this structure. */
6280 unsigned HOST_WIDE_INT i
;
6285 #define SPELLING_STRING 1
6286 #define SPELLING_MEMBER 2
6287 #define SPELLING_BOUNDS 3
6289 static struct spelling
*spelling
; /* Next stack element (unused). */
6290 static struct spelling
*spelling_base
; /* Spelling stack base. */
6291 static int spelling_size
; /* Size of the spelling stack. */
6293 /* Macros to save and restore the spelling stack around push_... functions.
6294 Alternative to SAVE_SPELLING_STACK. */
6296 #define SPELLING_DEPTH() (spelling - spelling_base)
6297 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6299 /* Push an element on the spelling stack with type KIND and assign VALUE
6302 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6304 int depth = SPELLING_DEPTH (); \
6306 if (depth >= spelling_size) \
6308 spelling_size += 10; \
6309 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6311 RESTORE_SPELLING_DEPTH (depth); \
6314 spelling->kind = (KIND); \
6315 spelling->MEMBER = (VALUE); \
6319 /* Push STRING on the stack. Printed literally. */
6322 push_string (const char *string
)
6324 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
6327 /* Push a member name on the stack. Printed as '.' STRING. */
6330 push_member_name (tree decl
)
6332 const char *const string
6334 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
6335 : _("<anonymous>"));
6336 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
6339 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6342 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
6344 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
6347 /* Compute the maximum size in bytes of the printed spelling. */
6350 spelling_length (void)
6355 for (p
= spelling_base
; p
< spelling
; p
++)
6357 if (p
->kind
== SPELLING_BOUNDS
)
6360 size
+= strlen (p
->u
.s
) + 1;
6366 /* Print the spelling to BUFFER and return it. */
6369 print_spelling (char *buffer
)
6374 for (p
= spelling_base
; p
< spelling
; p
++)
6375 if (p
->kind
== SPELLING_BOUNDS
)
6377 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
6383 if (p
->kind
== SPELLING_MEMBER
)
6385 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
6392 /* Issue an error message for a bad initializer component.
6393 GMSGID identifies the message.
6394 The component name is taken from the spelling stack. */
6397 error_init (const char *gmsgid
)
6401 /* The gmsgid may be a format string with %< and %>. */
6403 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6405 error ("(near initialization for %qs)", ofwhat
);
6408 /* Issue a pedantic warning for a bad initializer component. OPT is
6409 the option OPT_* (from options.h) controlling this warning or 0 if
6410 it is unconditionally given. GMSGID identifies the message. The
6411 component name is taken from the spelling stack. */
6414 pedwarn_init (location_t location
, int opt
, const char *gmsgid
)
6418 /* The gmsgid may be a format string with %< and %>. */
6419 pedwarn (location
, opt
, gmsgid
);
6420 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6422 pedwarn (location
, opt
, "(near initialization for %qs)", ofwhat
);
6425 /* Issue a warning for a bad initializer component.
6427 OPT is the OPT_W* value corresponding to the warning option that
6428 controls this warning. GMSGID identifies the message. The
6429 component name is taken from the spelling stack. */
6432 warning_init (int opt
, const char *gmsgid
)
6436 /* The gmsgid may be a format string with %< and %>. */
6437 warning (opt
, gmsgid
);
6438 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6440 warning (opt
, "(near initialization for %qs)", ofwhat
);
6443 /* If TYPE is an array type and EXPR is a parenthesized string
6444 constant, warn if pedantic that EXPR is being used to initialize an
6445 object of type TYPE. */
6448 maybe_warn_string_init (tree type
, struct c_expr expr
)
6451 && TREE_CODE (type
) == ARRAY_TYPE
6452 && TREE_CODE (expr
.value
) == STRING_CST
6453 && expr
.original_code
!= STRING_CST
)
6454 pedwarn_init (input_location
, OPT_Wpedantic
,
6455 "array initialized from parenthesized string constant");
6458 /* Digest the parser output INIT as an initializer for type TYPE.
6459 Return a C expression of type TYPE to represent the initial value.
6461 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6463 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6465 If INIT is a string constant, STRICT_STRING is true if it is
6466 unparenthesized or we should not warn here for it being parenthesized.
6467 For other types of INIT, STRICT_STRING is not used.
6469 INIT_LOC is the location of the INIT.
6471 REQUIRE_CONSTANT requests an error if non-constant initializers or
6472 elements are seen. */
6475 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
6476 bool null_pointer_constant
, bool strict_string
,
6477 int require_constant
)
6479 enum tree_code code
= TREE_CODE (type
);
6480 tree inside_init
= init
;
6481 tree semantic_type
= NULL_TREE
;
6482 bool maybe_const
= true;
6484 if (type
== error_mark_node
6486 || init
== error_mark_node
6487 || TREE_TYPE (init
) == error_mark_node
)
6488 return error_mark_node
;
6490 STRIP_TYPE_NOPS (inside_init
);
6492 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
6494 semantic_type
= TREE_TYPE (inside_init
);
6495 inside_init
= TREE_OPERAND (inside_init
, 0);
6497 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
6498 inside_init
= decl_constant_value_for_optimization (inside_init
);
6500 /* Initialization of an array of chars from a string constant
6501 optionally enclosed in braces. */
6503 if (code
== ARRAY_TYPE
&& inside_init
6504 && TREE_CODE (inside_init
) == STRING_CST
)
6507 = (TYPE_ATOMIC (TREE_TYPE (type
))
6508 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
6510 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
6511 /* Note that an array could be both an array of character type
6512 and an array of wchar_t if wchar_t is signed char or unsigned
6514 bool char_array
= (typ1
== char_type_node
6515 || typ1
== signed_char_type_node
6516 || typ1
== unsigned_char_type_node
);
6517 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
6518 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
6519 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
6521 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
6524 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
6525 expr
.value
= inside_init
;
6526 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
6527 expr
.original_type
= NULL
;
6528 maybe_warn_string_init (type
, expr
);
6530 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
6531 pedwarn_init (init_loc
, OPT_Wpedantic
,
6532 "initialization of a flexible array member");
6534 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6535 TYPE_MAIN_VARIANT (type
)))
6540 if (typ2
!= char_type_node
)
6542 error_init ("char-array initialized from wide string");
6543 return error_mark_node
;
6548 if (typ2
== char_type_node
)
6550 error_init ("wide character array initialized from non-wide "
6552 return error_mark_node
;
6554 else if (!comptypes(typ1
, typ2
))
6556 error_init ("wide character array initialized from "
6557 "incompatible wide string");
6558 return error_mark_node
;
6562 TREE_TYPE (inside_init
) = type
;
6563 if (TYPE_DOMAIN (type
) != 0
6564 && TYPE_SIZE (type
) != 0
6565 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
6567 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
6569 /* Subtract the size of a single (possibly wide) character
6570 because it's ok to ignore the terminating null char
6571 that is counted in the length of the constant. */
6572 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
6574 - (TYPE_PRECISION (typ1
)
6576 pedwarn_init (init_loc
, 0,
6577 ("initializer-string for array of chars "
6579 else if (warn_cxx_compat
6580 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
), len
))
6581 warning_at (init_loc
, OPT_Wc___compat
,
6582 ("initializer-string for array chars "
6583 "is too long for C++"));
6588 else if (INTEGRAL_TYPE_P (typ1
))
6590 error_init ("array of inappropriate type initialized "
6591 "from string constant");
6592 return error_mark_node
;
6596 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6597 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6598 below and handle as a constructor. */
6599 if (code
== VECTOR_TYPE
6600 && TREE_CODE (TREE_TYPE (inside_init
)) == VECTOR_TYPE
6601 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
6602 && TREE_CONSTANT (inside_init
))
6604 if (TREE_CODE (inside_init
) == VECTOR_CST
6605 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6606 TYPE_MAIN_VARIANT (type
)))
6609 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
6611 unsigned HOST_WIDE_INT ix
;
6613 bool constant_p
= true;
6615 /* Iterate through elements and check if all constructor
6616 elements are *_CSTs. */
6617 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
6618 if (!CONSTANT_CLASS_P (value
))
6625 return build_vector_from_ctor (type
,
6626 CONSTRUCTOR_ELTS (inside_init
));
6630 if (warn_sequence_point
)
6631 verify_sequence_points (inside_init
);
6633 /* Any type can be initialized
6634 from an expression of the same type, optionally with braces. */
6636 if (inside_init
&& TREE_TYPE (inside_init
) != 0
6637 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6638 TYPE_MAIN_VARIANT (type
))
6639 || (code
== ARRAY_TYPE
6640 && comptypes (TREE_TYPE (inside_init
), type
))
6641 || (code
== VECTOR_TYPE
6642 && comptypes (TREE_TYPE (inside_init
), type
))
6643 || (code
== POINTER_TYPE
6644 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
6645 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
6646 TREE_TYPE (type
)))))
6648 if (code
== POINTER_TYPE
)
6650 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
6652 if (TREE_CODE (inside_init
) == STRING_CST
6653 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6654 inside_init
= array_to_pointer_conversion
6655 (init_loc
, inside_init
);
6658 error_init ("invalid use of non-lvalue array");
6659 return error_mark_node
;
6664 if (code
== VECTOR_TYPE
)
6665 /* Although the types are compatible, we may require a
6667 inside_init
= convert (type
, inside_init
);
6669 if (require_constant
6670 && (code
== VECTOR_TYPE
|| !flag_isoc99
)
6671 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6673 /* As an extension, allow initializing objects with static storage
6674 duration with compound literals (which are then treated just as
6675 the brace enclosed list they contain). Also allow this for
6676 vectors, as we can only assign them with compound literals. */
6677 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6678 inside_init
= DECL_INITIAL (decl
);
6681 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
6682 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
6684 error_init ("array initialized from non-constant array expression");
6685 return error_mark_node
;
6688 /* Compound expressions can only occur here if -Wpedantic or
6689 -pedantic-errors is specified. In the later case, we always want
6690 an error. In the former case, we simply want a warning. */
6691 if (require_constant
&& pedantic
6692 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
6695 = valid_compound_expr_initializer (inside_init
,
6696 TREE_TYPE (inside_init
));
6697 if (inside_init
== error_mark_node
)
6698 error_init ("initializer element is not constant");
6700 pedwarn_init (init_loc
, OPT_Wpedantic
,
6701 "initializer element is not constant");
6702 if (flag_pedantic_errors
)
6703 inside_init
= error_mark_node
;
6705 else if (require_constant
6706 && !initializer_constant_valid_p (inside_init
,
6707 TREE_TYPE (inside_init
)))
6709 error_init ("initializer element is not constant");
6710 inside_init
= error_mark_node
;
6712 else if (require_constant
&& !maybe_const
)
6713 pedwarn_init (init_loc
, 0,
6714 "initializer element is not a constant expression");
6716 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6717 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
6718 inside_init
= convert_for_assignment (init_loc
, UNKNOWN_LOCATION
,
6719 type
, inside_init
, origtype
,
6720 ic_init
, null_pointer_constant
,
6721 NULL_TREE
, NULL_TREE
, 0);
6725 /* Handle scalar types, including conversions. */
6727 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
6728 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
6729 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
)
6731 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
6732 && (TREE_CODE (init
) == STRING_CST
6733 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
6734 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
6736 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
6739 = convert_for_assignment (init_loc
, UNKNOWN_LOCATION
, type
,
6740 inside_init
, origtype
, ic_init
,
6741 null_pointer_constant
, NULL_TREE
, NULL_TREE
,
6744 /* Check to see if we have already given an error message. */
6745 if (inside_init
== error_mark_node
)
6747 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
6749 error_init ("initializer element is not constant");
6750 inside_init
= error_mark_node
;
6752 else if (require_constant
6753 && !initializer_constant_valid_p (inside_init
,
6754 TREE_TYPE (inside_init
)))
6756 error_init ("initializer element is not computable at load time");
6757 inside_init
= error_mark_node
;
6759 else if (require_constant
&& !maybe_const
)
6760 pedwarn_init (init_loc
, 0,
6761 "initializer element is not a constant expression");
6766 /* Come here only for records and arrays. */
6768 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
6770 error_init ("variable-sized object may not be initialized");
6771 return error_mark_node
;
6774 error_init ("invalid initializer");
6775 return error_mark_node
;
6778 /* Handle initializers that use braces. */
6780 /* Type of object we are accumulating a constructor for.
6781 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6782 static tree constructor_type
;
6784 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6786 static tree constructor_fields
;
6788 /* For an ARRAY_TYPE, this is the specified index
6789 at which to store the next element we get. */
6790 static tree constructor_index
;
6792 /* For an ARRAY_TYPE, this is the maximum index. */
6793 static tree constructor_max_index
;
6795 /* For a RECORD_TYPE, this is the first field not yet written out. */
6796 static tree constructor_unfilled_fields
;
6798 /* For an ARRAY_TYPE, this is the index of the first element
6799 not yet written out. */
6800 static tree constructor_unfilled_index
;
6802 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6803 This is so we can generate gaps between fields, when appropriate. */
6804 static tree constructor_bit_index
;
6806 /* If we are saving up the elements rather than allocating them,
6807 this is the list of elements so far (in reverse order,
6808 most recent first). */
6809 static vec
<constructor_elt
, va_gc
> *constructor_elements
;
6811 /* 1 if constructor should be incrementally stored into a constructor chain,
6812 0 if all the elements should be kept in AVL tree. */
6813 static int constructor_incremental
;
6815 /* 1 if so far this constructor's elements are all compile-time constants. */
6816 static int constructor_constant
;
6818 /* 1 if so far this constructor's elements are all valid address constants. */
6819 static int constructor_simple
;
6821 /* 1 if this constructor has an element that cannot be part of a
6822 constant expression. */
6823 static int constructor_nonconst
;
6825 /* 1 if this constructor is erroneous so far. */
6826 static int constructor_erroneous
;
6828 /* Structure for managing pending initializer elements, organized as an
6833 struct init_node
*left
, *right
;
6834 struct init_node
*parent
;
6841 /* Tree of pending elements at this constructor level.
6842 These are elements encountered out of order
6843 which belong at places we haven't reached yet in actually
6845 Will never hold tree nodes across GC runs. */
6846 static struct init_node
*constructor_pending_elts
;
6848 /* The SPELLING_DEPTH of this constructor. */
6849 static int constructor_depth
;
6851 /* DECL node for which an initializer is being read.
6852 0 means we are reading a constructor expression
6853 such as (struct foo) {...}. */
6854 static tree constructor_decl
;
6856 /* Nonzero if this is an initializer for a top-level decl. */
6857 static int constructor_top_level
;
6859 /* Nonzero if there were any member designators in this initializer. */
6860 static int constructor_designated
;
6862 /* Nesting depth of designator list. */
6863 static int designator_depth
;
6865 /* Nonzero if there were diagnosed errors in this designator list. */
6866 static int designator_erroneous
;
6869 /* This stack has a level for each implicit or explicit level of
6870 structuring in the initializer, including the outermost one. It
6871 saves the values of most of the variables above. */
6873 struct constructor_range_stack
;
6875 struct constructor_stack
6877 struct constructor_stack
*next
;
6882 tree unfilled_index
;
6883 tree unfilled_fields
;
6885 vec
<constructor_elt
, va_gc
> *elements
;
6886 struct init_node
*pending_elts
;
6889 /* If value nonzero, this value should replace the entire
6890 constructor at this level. */
6891 struct c_expr replacement_value
;
6892 struct constructor_range_stack
*range_stack
;
6903 static struct constructor_stack
*constructor_stack
;
6905 /* This stack represents designators from some range designator up to
6906 the last designator in the list. */
6908 struct constructor_range_stack
6910 struct constructor_range_stack
*next
, *prev
;
6911 struct constructor_stack
*stack
;
6918 static struct constructor_range_stack
*constructor_range_stack
;
6920 /* This stack records separate initializers that are nested.
6921 Nested initializers can't happen in ANSI C, but GNU C allows them
6922 in cases like { ... (struct foo) { ... } ... }. */
6924 struct initializer_stack
6926 struct initializer_stack
*next
;
6928 struct constructor_stack
*constructor_stack
;
6929 struct constructor_range_stack
*constructor_range_stack
;
6930 vec
<constructor_elt
, va_gc
> *elements
;
6931 struct spelling
*spelling
;
6932 struct spelling
*spelling_base
;
6935 char require_constant_value
;
6936 char require_constant_elements
;
6939 static struct initializer_stack
*initializer_stack
;
6941 /* Prepare to parse and output the initializer for variable DECL. */
6944 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
)
6947 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
6949 p
->decl
= constructor_decl
;
6950 p
->require_constant_value
= require_constant_value
;
6951 p
->require_constant_elements
= require_constant_elements
;
6952 p
->constructor_stack
= constructor_stack
;
6953 p
->constructor_range_stack
= constructor_range_stack
;
6954 p
->elements
= constructor_elements
;
6955 p
->spelling
= spelling
;
6956 p
->spelling_base
= spelling_base
;
6957 p
->spelling_size
= spelling_size
;
6958 p
->top_level
= constructor_top_level
;
6959 p
->next
= initializer_stack
;
6960 initializer_stack
= p
;
6962 constructor_decl
= decl
;
6963 constructor_designated
= 0;
6964 constructor_top_level
= top_level
;
6966 if (decl
!= 0 && decl
!= error_mark_node
)
6968 require_constant_value
= TREE_STATIC (decl
);
6969 require_constant_elements
6970 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
6971 /* For a scalar, you can always use any value to initialize,
6972 even within braces. */
6973 && (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
6974 || TREE_CODE (TREE_TYPE (decl
)) == RECORD_TYPE
6975 || TREE_CODE (TREE_TYPE (decl
)) == UNION_TYPE
6976 || TREE_CODE (TREE_TYPE (decl
)) == QUAL_UNION_TYPE
));
6977 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
6981 require_constant_value
= 0;
6982 require_constant_elements
= 0;
6983 locus
= _("(anonymous)");
6986 constructor_stack
= 0;
6987 constructor_range_stack
= 0;
6989 missing_braces_mentioned
= 0;
6993 RESTORE_SPELLING_DEPTH (0);
6996 push_string (locus
);
7002 struct initializer_stack
*p
= initializer_stack
;
7004 /* Free the whole constructor stack of this initializer. */
7005 while (constructor_stack
)
7007 struct constructor_stack
*q
= constructor_stack
;
7008 constructor_stack
= q
->next
;
7012 gcc_assert (!constructor_range_stack
);
7014 /* Pop back to the data of the outer initializer (if any). */
7015 free (spelling_base
);
7017 constructor_decl
= p
->decl
;
7018 require_constant_value
= p
->require_constant_value
;
7019 require_constant_elements
= p
->require_constant_elements
;
7020 constructor_stack
= p
->constructor_stack
;
7021 constructor_range_stack
= p
->constructor_range_stack
;
7022 constructor_elements
= p
->elements
;
7023 spelling
= p
->spelling
;
7024 spelling_base
= p
->spelling_base
;
7025 spelling_size
= p
->spelling_size
;
7026 constructor_top_level
= p
->top_level
;
7027 initializer_stack
= p
->next
;
7031 /* Call here when we see the initializer is surrounded by braces.
7032 This is instead of a call to push_init_level;
7033 it is matched by a call to pop_init_level.
7035 TYPE is the type to initialize, for a constructor expression.
7036 For an initializer for a decl, TYPE is zero. */
7039 really_start_incremental_init (tree type
)
7041 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
7044 type
= TREE_TYPE (constructor_decl
);
7046 if (TREE_CODE (type
) == VECTOR_TYPE
7047 && TYPE_VECTOR_OPAQUE (type
))
7048 error ("opaque vector types cannot be initialized");
7050 p
->type
= constructor_type
;
7051 p
->fields
= constructor_fields
;
7052 p
->index
= constructor_index
;
7053 p
->max_index
= constructor_max_index
;
7054 p
->unfilled_index
= constructor_unfilled_index
;
7055 p
->unfilled_fields
= constructor_unfilled_fields
;
7056 p
->bit_index
= constructor_bit_index
;
7057 p
->elements
= constructor_elements
;
7058 p
->constant
= constructor_constant
;
7059 p
->simple
= constructor_simple
;
7060 p
->nonconst
= constructor_nonconst
;
7061 p
->erroneous
= constructor_erroneous
;
7062 p
->pending_elts
= constructor_pending_elts
;
7063 p
->depth
= constructor_depth
;
7064 p
->replacement_value
.value
= 0;
7065 p
->replacement_value
.original_code
= ERROR_MARK
;
7066 p
->replacement_value
.original_type
= NULL
;
7070 p
->incremental
= constructor_incremental
;
7071 p
->designated
= constructor_designated
;
7073 constructor_stack
= p
;
7075 constructor_constant
= 1;
7076 constructor_simple
= 1;
7077 constructor_nonconst
= 0;
7078 constructor_depth
= SPELLING_DEPTH ();
7079 constructor_elements
= NULL
;
7080 constructor_pending_elts
= 0;
7081 constructor_type
= type
;
7082 constructor_incremental
= 1;
7083 constructor_designated
= 0;
7084 designator_depth
= 0;
7085 designator_erroneous
= 0;
7087 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7088 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7090 constructor_fields
= TYPE_FIELDS (constructor_type
);
7091 /* Skip any nameless bit fields at the beginning. */
7092 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7093 && DECL_NAME (constructor_fields
) == 0)
7094 constructor_fields
= DECL_CHAIN (constructor_fields
);
7096 constructor_unfilled_fields
= constructor_fields
;
7097 constructor_bit_index
= bitsize_zero_node
;
7099 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7101 if (TYPE_DOMAIN (constructor_type
))
7103 constructor_max_index
7104 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7106 /* Detect non-empty initializations of zero-length arrays. */
7107 if (constructor_max_index
== NULL_TREE
7108 && TYPE_SIZE (constructor_type
))
7109 constructor_max_index
= integer_minus_one_node
;
7111 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7112 to initialize VLAs will cause a proper error; avoid tree
7113 checking errors as well by setting a safe value. */
7114 if (constructor_max_index
7115 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7116 constructor_max_index
= integer_minus_one_node
;
7119 = convert (bitsizetype
,
7120 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7124 constructor_index
= bitsize_zero_node
;
7125 constructor_max_index
= NULL_TREE
;
7128 constructor_unfilled_index
= constructor_index
;
7130 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7132 /* Vectors are like simple fixed-size arrays. */
7133 constructor_max_index
=
7134 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7135 constructor_index
= bitsize_zero_node
;
7136 constructor_unfilled_index
= constructor_index
;
7140 /* Handle the case of int x = {5}; */
7141 constructor_fields
= constructor_type
;
7142 constructor_unfilled_fields
= constructor_type
;
7146 /* Push down into a subobject, for initialization.
7147 If this is for an explicit set of braces, IMPLICIT is 0.
7148 If it is because the next element belongs at a lower level,
7149 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7152 push_init_level (int implicit
, struct obstack
* braced_init_obstack
)
7154 struct constructor_stack
*p
;
7155 tree value
= NULL_TREE
;
7157 /* If we've exhausted any levels that didn't have braces,
7158 pop them now. If implicit == 1, this will have been done in
7159 process_init_element; do not repeat it here because in the case
7160 of excess initializers for an empty aggregate this leads to an
7161 infinite cycle of popping a level and immediately recreating
7165 while (constructor_stack
->implicit
)
7167 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7168 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7169 && constructor_fields
== 0)
7170 process_init_element (pop_init_level (1, braced_init_obstack
),
7171 true, braced_init_obstack
);
7172 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
7173 && constructor_max_index
7174 && tree_int_cst_lt (constructor_max_index
,
7176 process_init_element (pop_init_level (1, braced_init_obstack
),
7177 true, braced_init_obstack
);
7183 /* Unless this is an explicit brace, we need to preserve previous
7187 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7188 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7189 && constructor_fields
)
7190 value
= find_init_member (constructor_fields
, braced_init_obstack
);
7191 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7192 value
= find_init_member (constructor_index
, braced_init_obstack
);
7195 p
= XNEW (struct constructor_stack
);
7196 p
->type
= constructor_type
;
7197 p
->fields
= constructor_fields
;
7198 p
->index
= constructor_index
;
7199 p
->max_index
= constructor_max_index
;
7200 p
->unfilled_index
= constructor_unfilled_index
;
7201 p
->unfilled_fields
= constructor_unfilled_fields
;
7202 p
->bit_index
= constructor_bit_index
;
7203 p
->elements
= constructor_elements
;
7204 p
->constant
= constructor_constant
;
7205 p
->simple
= constructor_simple
;
7206 p
->nonconst
= constructor_nonconst
;
7207 p
->erroneous
= constructor_erroneous
;
7208 p
->pending_elts
= constructor_pending_elts
;
7209 p
->depth
= constructor_depth
;
7210 p
->replacement_value
.value
= 0;
7211 p
->replacement_value
.original_code
= ERROR_MARK
;
7212 p
->replacement_value
.original_type
= NULL
;
7213 p
->implicit
= implicit
;
7215 p
->incremental
= constructor_incremental
;
7216 p
->designated
= constructor_designated
;
7217 p
->next
= constructor_stack
;
7219 constructor_stack
= p
;
7221 constructor_constant
= 1;
7222 constructor_simple
= 1;
7223 constructor_nonconst
= 0;
7224 constructor_depth
= SPELLING_DEPTH ();
7225 constructor_elements
= NULL
;
7226 constructor_incremental
= 1;
7227 constructor_designated
= 0;
7228 constructor_pending_elts
= 0;
7231 p
->range_stack
= constructor_range_stack
;
7232 constructor_range_stack
= 0;
7233 designator_depth
= 0;
7234 designator_erroneous
= 0;
7237 /* Don't die if an entire brace-pair level is superfluous
7238 in the containing level. */
7239 if (constructor_type
== 0)
7241 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
7242 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7244 /* Don't die if there are extra init elts at the end. */
7245 if (constructor_fields
== 0)
7246 constructor_type
= 0;
7249 constructor_type
= TREE_TYPE (constructor_fields
);
7250 push_member_name (constructor_fields
);
7251 constructor_depth
++;
7254 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7256 constructor_type
= TREE_TYPE (constructor_type
);
7257 push_array_bounds (tree_to_uhwi (constructor_index
));
7258 constructor_depth
++;
7261 if (constructor_type
== 0)
7263 error_init ("extra brace group at end of initializer");
7264 constructor_fields
= 0;
7265 constructor_unfilled_fields
= 0;
7269 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
7271 constructor_constant
= TREE_CONSTANT (value
);
7272 constructor_simple
= TREE_STATIC (value
);
7273 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
7274 constructor_elements
= CONSTRUCTOR_ELTS (value
);
7275 if (!vec_safe_is_empty (constructor_elements
)
7276 && (TREE_CODE (constructor_type
) == RECORD_TYPE
7277 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
7278 set_nonincremental_init (braced_init_obstack
);
7281 if (implicit
== 1 && warn_missing_braces
&& !missing_braces_mentioned
)
7283 missing_braces_mentioned
= 1;
7284 warning_init (OPT_Wmissing_braces
, "missing braces around initializer");
7287 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7288 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7290 constructor_fields
= TYPE_FIELDS (constructor_type
);
7291 /* Skip any nameless bit fields at the beginning. */
7292 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7293 && DECL_NAME (constructor_fields
) == 0)
7294 constructor_fields
= DECL_CHAIN (constructor_fields
);
7296 constructor_unfilled_fields
= constructor_fields
;
7297 constructor_bit_index
= bitsize_zero_node
;
7299 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7301 /* Vectors are like simple fixed-size arrays. */
7302 constructor_max_index
=
7303 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7304 constructor_index
= bitsize_int (0);
7305 constructor_unfilled_index
= constructor_index
;
7307 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7309 if (TYPE_DOMAIN (constructor_type
))
7311 constructor_max_index
7312 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7314 /* Detect non-empty initializations of zero-length arrays. */
7315 if (constructor_max_index
== NULL_TREE
7316 && TYPE_SIZE (constructor_type
))
7317 constructor_max_index
= integer_minus_one_node
;
7319 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7320 to initialize VLAs will cause a proper error; avoid tree
7321 checking errors as well by setting a safe value. */
7322 if (constructor_max_index
7323 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7324 constructor_max_index
= integer_minus_one_node
;
7327 = convert (bitsizetype
,
7328 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7331 constructor_index
= bitsize_zero_node
;
7333 constructor_unfilled_index
= constructor_index
;
7334 if (value
&& TREE_CODE (value
) == STRING_CST
)
7336 /* We need to split the char/wchar array into individual
7337 characters, so that we don't have to special case it
7339 set_nonincremental_init_from_string (value
, braced_init_obstack
);
7344 if (constructor_type
!= error_mark_node
)
7345 warning_init (0, "braces around scalar initializer");
7346 constructor_fields
= constructor_type
;
7347 constructor_unfilled_fields
= constructor_type
;
7351 /* At the end of an implicit or explicit brace level,
7352 finish up that level of constructor. If a single expression
7353 with redundant braces initialized that level, return the
7354 c_expr structure for that expression. Otherwise, the original_code
7355 element is set to ERROR_MARK.
7356 If we were outputting the elements as they are read, return 0 as the value
7357 from inner levels (process_init_element ignores that),
7358 but return error_mark_node as the value from the outermost level
7359 (that's what we want to put in DECL_INITIAL).
7360 Otherwise, return a CONSTRUCTOR expression as the value. */
7363 pop_init_level (int implicit
, struct obstack
* braced_init_obstack
)
7365 struct constructor_stack
*p
;
7368 ret
.original_code
= ERROR_MARK
;
7369 ret
.original_type
= NULL
;
7373 /* When we come to an explicit close brace,
7374 pop any inner levels that didn't have explicit braces. */
7375 while (constructor_stack
->implicit
)
7377 process_init_element (pop_init_level (1, braced_init_obstack
),
7378 true, braced_init_obstack
);
7380 gcc_assert (!constructor_range_stack
);
7383 /* Now output all pending elements. */
7384 constructor_incremental
= 1;
7385 output_pending_init_elements (1, braced_init_obstack
);
7387 p
= constructor_stack
;
7389 /* Error for initializing a flexible array member, or a zero-length
7390 array member in an inappropriate context. */
7391 if (constructor_type
&& constructor_fields
7392 && TREE_CODE (constructor_type
) == ARRAY_TYPE
7393 && TYPE_DOMAIN (constructor_type
)
7394 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
7396 /* Silently discard empty initializations. The parser will
7397 already have pedwarned for empty brackets. */
7398 if (integer_zerop (constructor_unfilled_index
))
7399 constructor_type
= NULL_TREE
;
7402 gcc_assert (!TYPE_SIZE (constructor_type
));
7404 if (constructor_depth
> 2)
7405 error_init ("initialization of flexible array member in a nested context");
7407 pedwarn_init (input_location
, OPT_Wpedantic
,
7408 "initialization of a flexible array member");
7410 /* We have already issued an error message for the existence
7411 of a flexible array member not at the end of the structure.
7412 Discard the initializer so that we do not die later. */
7413 if (DECL_CHAIN (constructor_fields
) != NULL_TREE
)
7414 constructor_type
= NULL_TREE
;
7418 /* Warn when some struct elements are implicitly initialized to zero. */
7419 if (warn_missing_field_initializers
7421 && TREE_CODE (constructor_type
) == RECORD_TYPE
7422 && constructor_unfilled_fields
)
7424 bool constructor_zeroinit
=
7425 (vec_safe_length (constructor_elements
) == 1
7426 && integer_zerop ((*constructor_elements
)[0].value
));
7428 /* Do not warn for flexible array members or zero-length arrays. */
7429 while (constructor_unfilled_fields
7430 && (!DECL_SIZE (constructor_unfilled_fields
)
7431 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
7432 constructor_unfilled_fields
= DECL_CHAIN (constructor_unfilled_fields
);
7434 if (constructor_unfilled_fields
7435 /* Do not warn if this level of the initializer uses member
7436 designators; it is likely to be deliberate. */
7437 && !constructor_designated
7438 /* Do not warn about initializing with ` = {0}'. */
7439 && !constructor_zeroinit
)
7441 if (warning_at (input_location
, OPT_Wmissing_field_initializers
,
7442 "missing initializer for field %qD of %qT",
7443 constructor_unfilled_fields
,
7445 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields
),
7446 "%qD declared here", constructor_unfilled_fields
);
7450 /* Pad out the end of the structure. */
7451 if (p
->replacement_value
.value
)
7452 /* If this closes a superfluous brace pair,
7453 just pass out the element between them. */
7454 ret
= p
->replacement_value
;
7455 else if (constructor_type
== 0)
7457 else if (TREE_CODE (constructor_type
) != RECORD_TYPE
7458 && TREE_CODE (constructor_type
) != UNION_TYPE
7459 && TREE_CODE (constructor_type
) != ARRAY_TYPE
7460 && TREE_CODE (constructor_type
) != VECTOR_TYPE
)
7462 /* A nonincremental scalar initializer--just return
7463 the element, after verifying there is just one. */
7464 if (vec_safe_is_empty (constructor_elements
))
7466 if (!constructor_erroneous
)
7467 error_init ("empty scalar initializer");
7468 ret
.value
= error_mark_node
;
7470 else if (vec_safe_length (constructor_elements
) != 1)
7472 error_init ("extra elements in scalar initializer");
7473 ret
.value
= (*constructor_elements
)[0].value
;
7476 ret
.value
= (*constructor_elements
)[0].value
;
7480 if (constructor_erroneous
)
7481 ret
.value
= error_mark_node
;
7484 ret
.value
= build_constructor (constructor_type
,
7485 constructor_elements
);
7486 if (constructor_constant
)
7487 TREE_CONSTANT (ret
.value
) = 1;
7488 if (constructor_constant
&& constructor_simple
)
7489 TREE_STATIC (ret
.value
) = 1;
7490 if (constructor_nonconst
)
7491 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
7495 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
7497 if (constructor_nonconst
)
7498 ret
.original_code
= C_MAYBE_CONST_EXPR
;
7499 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
7500 ret
.original_code
= ERROR_MARK
;
7503 constructor_type
= p
->type
;
7504 constructor_fields
= p
->fields
;
7505 constructor_index
= p
->index
;
7506 constructor_max_index
= p
->max_index
;
7507 constructor_unfilled_index
= p
->unfilled_index
;
7508 constructor_unfilled_fields
= p
->unfilled_fields
;
7509 constructor_bit_index
= p
->bit_index
;
7510 constructor_elements
= p
->elements
;
7511 constructor_constant
= p
->constant
;
7512 constructor_simple
= p
->simple
;
7513 constructor_nonconst
= p
->nonconst
;
7514 constructor_erroneous
= p
->erroneous
;
7515 constructor_incremental
= p
->incremental
;
7516 constructor_designated
= p
->designated
;
7517 constructor_pending_elts
= p
->pending_elts
;
7518 constructor_depth
= p
->depth
;
7520 constructor_range_stack
= p
->range_stack
;
7521 RESTORE_SPELLING_DEPTH (constructor_depth
);
7523 constructor_stack
= p
->next
;
7526 if (ret
.value
== 0 && constructor_stack
== 0)
7527 ret
.value
= error_mark_node
;
7531 /* Common handling for both array range and field name designators.
7532 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7535 set_designator (int array
, struct obstack
* braced_init_obstack
)
7538 enum tree_code subcode
;
7540 /* Don't die if an entire brace-pair level is superfluous
7541 in the containing level. */
7542 if (constructor_type
== 0)
7545 /* If there were errors in this designator list already, bail out
7547 if (designator_erroneous
)
7550 if (!designator_depth
)
7552 gcc_assert (!constructor_range_stack
);
7554 /* Designator list starts at the level of closest explicit
7556 while (constructor_stack
->implicit
)
7558 process_init_element (pop_init_level (1, braced_init_obstack
),
7559 true, braced_init_obstack
);
7561 constructor_designated
= 1;
7565 switch (TREE_CODE (constructor_type
))
7569 subtype
= TREE_TYPE (constructor_fields
);
7570 if (subtype
!= error_mark_node
)
7571 subtype
= TYPE_MAIN_VARIANT (subtype
);
7574 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
7580 subcode
= TREE_CODE (subtype
);
7581 if (array
&& subcode
!= ARRAY_TYPE
)
7583 error_init ("array index in non-array initializer");
7586 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
7588 error_init ("field name not in record or union initializer");
7592 constructor_designated
= 1;
7593 push_init_level (2, braced_init_obstack
);
7597 /* If there are range designators in designator list, push a new designator
7598 to constructor_range_stack. RANGE_END is end of such stack range or
7599 NULL_TREE if there is no range designator at this level. */
7602 push_range_stack (tree range_end
, struct obstack
* braced_init_obstack
)
7604 struct constructor_range_stack
*p
;
7606 p
= (struct constructor_range_stack
*)
7607 obstack_alloc (braced_init_obstack
,
7608 sizeof (struct constructor_range_stack
));
7609 p
->prev
= constructor_range_stack
;
7611 p
->fields
= constructor_fields
;
7612 p
->range_start
= constructor_index
;
7613 p
->index
= constructor_index
;
7614 p
->stack
= constructor_stack
;
7615 p
->range_end
= range_end
;
7616 if (constructor_range_stack
)
7617 constructor_range_stack
->next
= p
;
7618 constructor_range_stack
= p
;
7621 /* Within an array initializer, specify the next index to be initialized.
7622 FIRST is that index. If LAST is nonzero, then initialize a range
7623 of indices, running from FIRST through LAST. */
7626 set_init_index (tree first
, tree last
,
7627 struct obstack
* braced_init_obstack
)
7629 if (set_designator (1, braced_init_obstack
))
7632 designator_erroneous
= 1;
7634 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
7635 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
7637 error_init ("array index in initializer not of integer type");
7641 if (TREE_CODE (first
) != INTEGER_CST
)
7643 first
= c_fully_fold (first
, false, NULL
);
7644 if (TREE_CODE (first
) == INTEGER_CST
)
7645 pedwarn_init (input_location
, OPT_Wpedantic
,
7646 "array index in initializer is not "
7647 "an integer constant expression");
7650 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
7652 last
= c_fully_fold (last
, false, NULL
);
7653 if (TREE_CODE (last
) == INTEGER_CST
)
7654 pedwarn_init (input_location
, OPT_Wpedantic
,
7655 "array index in initializer is not "
7656 "an integer constant expression");
7659 if (TREE_CODE (first
) != INTEGER_CST
)
7660 error_init ("nonconstant array index in initializer");
7661 else if (last
!= 0 && TREE_CODE (last
) != INTEGER_CST
)
7662 error_init ("nonconstant array index in initializer");
7663 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
7664 error_init ("array index in non-array initializer");
7665 else if (tree_int_cst_sgn (first
) == -1)
7666 error_init ("array index in initializer exceeds array bounds");
7667 else if (constructor_max_index
7668 && tree_int_cst_lt (constructor_max_index
, first
))
7669 error_init ("array index in initializer exceeds array bounds");
7672 constant_expression_warning (first
);
7674 constant_expression_warning (last
);
7675 constructor_index
= convert (bitsizetype
, first
);
7676 if (tree_int_cst_lt (constructor_index
, first
))
7678 constructor_index
= copy_node (constructor_index
);
7679 TREE_OVERFLOW (constructor_index
) = 1;
7684 if (tree_int_cst_equal (first
, last
))
7686 else if (tree_int_cst_lt (last
, first
))
7688 error_init ("empty index range in initializer");
7693 last
= convert (bitsizetype
, last
);
7694 if (constructor_max_index
!= 0
7695 && tree_int_cst_lt (constructor_max_index
, last
))
7697 error_init ("array index range in initializer exceeds array bounds");
7704 designator_erroneous
= 0;
7705 if (constructor_range_stack
|| last
)
7706 push_range_stack (last
, braced_init_obstack
);
7710 /* Within a struct initializer, specify the next field to be initialized. */
7713 set_init_label (tree fieldname
, struct obstack
* braced_init_obstack
)
7717 if (set_designator (0, braced_init_obstack
))
7720 designator_erroneous
= 1;
7722 if (TREE_CODE (constructor_type
) != RECORD_TYPE
7723 && TREE_CODE (constructor_type
) != UNION_TYPE
)
7725 error_init ("field name not in record or union initializer");
7729 field
= lookup_field (constructor_type
, fieldname
);
7732 error ("unknown field %qE specified in initializer", fieldname
);
7736 constructor_fields
= TREE_VALUE (field
);
7738 designator_erroneous
= 0;
7739 if (constructor_range_stack
)
7740 push_range_stack (NULL_TREE
, braced_init_obstack
);
7741 field
= TREE_CHAIN (field
);
7744 if (set_designator (0, braced_init_obstack
))
7748 while (field
!= NULL_TREE
);
7751 /* Add a new initializer to the tree of pending initializers. PURPOSE
7752 identifies the initializer, either array index or field in a structure.
7753 VALUE is the value of that index or field. If ORIGTYPE is not
7754 NULL_TREE, it is the original type of VALUE.
7756 IMPLICIT is true if value comes from pop_init_level (1),
7757 the new initializer has been merged with the existing one
7758 and thus no warnings should be emitted about overriding an
7759 existing initializer. */
7762 add_pending_init (tree purpose
, tree value
, tree origtype
, bool implicit
,
7763 struct obstack
* braced_init_obstack
)
7765 struct init_node
*p
, **q
, *r
;
7767 q
= &constructor_pending_elts
;
7770 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7775 if (tree_int_cst_lt (purpose
, p
->purpose
))
7777 else if (tree_int_cst_lt (p
->purpose
, purpose
))
7783 if (TREE_SIDE_EFFECTS (p
->value
))
7784 warning_init (0, "initialized field with side-effects overwritten");
7785 else if (warn_override_init
)
7786 warning_init (OPT_Woverride_init
, "initialized field overwritten");
7789 p
->origtype
= origtype
;
7798 bitpos
= bit_position (purpose
);
7802 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
7804 else if (p
->purpose
!= purpose
)
7810 if (TREE_SIDE_EFFECTS (p
->value
))
7811 warning_init (0, "initialized field with side-effects overwritten");
7812 else if (warn_override_init
)
7813 warning_init (OPT_Woverride_init
, "initialized field overwritten");
7816 p
->origtype
= origtype
;
7822 r
= (struct init_node
*) obstack_alloc (braced_init_obstack
,
7823 sizeof (struct init_node
));
7824 r
->purpose
= purpose
;
7826 r
->origtype
= origtype
;
7836 struct init_node
*s
;
7840 if (p
->balance
== 0)
7842 else if (p
->balance
< 0)
7849 p
->left
->parent
= p
;
7866 constructor_pending_elts
= r
;
7871 struct init_node
*t
= r
->right
;
7875 r
->right
->parent
= r
;
7880 p
->left
->parent
= p
;
7883 p
->balance
= t
->balance
< 0;
7884 r
->balance
= -(t
->balance
> 0);
7899 constructor_pending_elts
= t
;
7905 /* p->balance == +1; growth of left side balances the node. */
7910 else /* r == p->right */
7912 if (p
->balance
== 0)
7913 /* Growth propagation from right side. */
7915 else if (p
->balance
> 0)
7922 p
->right
->parent
= p
;
7939 constructor_pending_elts
= r
;
7941 else /* r->balance == -1 */
7944 struct init_node
*t
= r
->left
;
7948 r
->left
->parent
= r
;
7953 p
->right
->parent
= p
;
7956 r
->balance
= (t
->balance
< 0);
7957 p
->balance
= -(t
->balance
> 0);
7972 constructor_pending_elts
= t
;
7978 /* p->balance == -1; growth of right side balances the node. */
7989 /* Build AVL tree from a sorted chain. */
7992 set_nonincremental_init (struct obstack
* braced_init_obstack
)
7994 unsigned HOST_WIDE_INT ix
;
7997 if (TREE_CODE (constructor_type
) != RECORD_TYPE
7998 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
8001 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
8003 add_pending_init (index
, value
, NULL_TREE
, true,
8004 braced_init_obstack
);
8006 constructor_elements
= NULL
;
8007 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8009 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
8010 /* Skip any nameless bit fields at the beginning. */
8011 while (constructor_unfilled_fields
!= 0
8012 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8013 && DECL_NAME (constructor_unfilled_fields
) == 0)
8014 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
8017 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8019 if (TYPE_DOMAIN (constructor_type
))
8020 constructor_unfilled_index
8021 = convert (bitsizetype
,
8022 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
8024 constructor_unfilled_index
= bitsize_zero_node
;
8026 constructor_incremental
= 0;
8029 /* Build AVL tree from a string constant. */
8032 set_nonincremental_init_from_string (tree str
,
8033 struct obstack
* braced_init_obstack
)
8035 tree value
, purpose
, type
;
8036 HOST_WIDE_INT val
[2];
8037 const char *p
, *end
;
8038 int byte
, wchar_bytes
, charwidth
, bitpos
;
8040 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
8042 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
8043 charwidth
= TYPE_PRECISION (char_type_node
);
8044 type
= TREE_TYPE (constructor_type
);
8045 p
= TREE_STRING_POINTER (str
);
8046 end
= p
+ TREE_STRING_LENGTH (str
);
8048 for (purpose
= bitsize_zero_node
;
8050 && !(constructor_max_index
8051 && tree_int_cst_lt (constructor_max_index
, purpose
));
8052 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
8054 if (wchar_bytes
== 1)
8056 val
[1] = (unsigned char) *p
++;
8063 for (byte
= 0; byte
< wchar_bytes
; byte
++)
8065 if (BYTES_BIG_ENDIAN
)
8066 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
8068 bitpos
= byte
* charwidth
;
8069 val
[bitpos
< HOST_BITS_PER_WIDE_INT
]
8070 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
8071 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
8075 if (!TYPE_UNSIGNED (type
))
8077 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
8078 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
8080 if (val
[1] & (((HOST_WIDE_INT
) 1) << (bitpos
- 1)))
8082 val
[1] |= ((HOST_WIDE_INT
) -1) << bitpos
;
8086 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
8091 else if (val
[0] & (((HOST_WIDE_INT
) 1)
8092 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
8093 val
[0] |= ((HOST_WIDE_INT
) -1)
8094 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
8097 value
= build_int_cst_wide (type
, val
[1], val
[0]);
8098 add_pending_init (purpose
, value
, NULL_TREE
, true,
8099 braced_init_obstack
);
8102 constructor_incremental
= 0;
8105 /* Return value of FIELD in pending initializer or zero if the field was
8106 not initialized yet. */
8109 find_init_member (tree field
, struct obstack
* braced_init_obstack
)
8111 struct init_node
*p
;
8113 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8115 if (constructor_incremental
8116 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8117 set_nonincremental_init (braced_init_obstack
);
8119 p
= constructor_pending_elts
;
8122 if (tree_int_cst_lt (field
, p
->purpose
))
8124 else if (tree_int_cst_lt (p
->purpose
, field
))
8130 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8132 tree bitpos
= bit_position (field
);
8134 if (constructor_incremental
8135 && (!constructor_unfilled_fields
8136 || tree_int_cst_lt (bitpos
,
8137 bit_position (constructor_unfilled_fields
))))
8138 set_nonincremental_init (braced_init_obstack
);
8140 p
= constructor_pending_elts
;
8143 if (field
== p
->purpose
)
8145 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
8151 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8153 if (!vec_safe_is_empty (constructor_elements
)
8154 && (constructor_elements
->last ().index
== field
))
8155 return constructor_elements
->last ().value
;
8160 /* "Output" the next constructor element.
8161 At top level, really output it to assembler code now.
8162 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8163 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8164 TYPE is the data type that the containing data type wants here.
8165 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8166 If VALUE is a string constant, STRICT_STRING is true if it is
8167 unparenthesized or we should not warn here for it being parenthesized.
8168 For other types of VALUE, STRICT_STRING is not used.
8170 PENDING if non-nil means output pending elements that belong
8171 right after this element. (PENDING is normally 1;
8172 it is 0 while outputting pending elements, to avoid recursion.)
8174 IMPLICIT is true if value comes from pop_init_level (1),
8175 the new initializer has been merged with the existing one
8176 and thus no warnings should be emitted about overriding an
8177 existing initializer. */
8180 output_init_element (tree value
, tree origtype
, bool strict_string
, tree type
,
8181 tree field
, int pending
, bool implicit
,
8182 struct obstack
* braced_init_obstack
)
8184 tree semantic_type
= NULL_TREE
;
8185 bool maybe_const
= true;
8188 if (type
== error_mark_node
|| value
== error_mark_node
)
8190 constructor_erroneous
= 1;
8193 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
8194 && (TREE_CODE (value
) == STRING_CST
8195 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
8196 && !(TREE_CODE (value
) == STRING_CST
8197 && TREE_CODE (type
) == ARRAY_TYPE
8198 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
8199 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
8200 TYPE_MAIN_VARIANT (type
)))
8201 value
= array_to_pointer_conversion (input_location
, value
);
8203 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
8204 && require_constant_value
&& !flag_isoc99
&& pending
)
8206 /* As an extension, allow initializing objects with static storage
8207 duration with compound literals (which are then treated just as
8208 the brace enclosed list they contain). */
8209 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
8210 value
= DECL_INITIAL (decl
);
8213 npc
= null_pointer_constant_p (value
);
8214 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
8216 semantic_type
= TREE_TYPE (value
);
8217 value
= TREE_OPERAND (value
, 0);
8219 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
8221 if (value
== error_mark_node
)
8222 constructor_erroneous
= 1;
8223 else if (!TREE_CONSTANT (value
))
8224 constructor_constant
= 0;
8225 else if (!initializer_constant_valid_p (value
, TREE_TYPE (value
))
8226 || ((TREE_CODE (constructor_type
) == RECORD_TYPE
8227 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8228 && DECL_C_BIT_FIELD (field
)
8229 && TREE_CODE (value
) != INTEGER_CST
))
8230 constructor_simple
= 0;
8232 constructor_nonconst
= 1;
8234 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
8236 if (require_constant_value
)
8238 error_init ("initializer element is not constant");
8239 value
= error_mark_node
;
8241 else if (require_constant_elements
)
8242 pedwarn (input_location
, 0,
8243 "initializer element is not computable at load time");
8245 else if (!maybe_const
8246 && (require_constant_value
|| require_constant_elements
))
8247 pedwarn_init (input_location
, 0,
8248 "initializer element is not a constant expression");
8250 /* Issue -Wc++-compat warnings about initializing a bitfield with
8253 && field
!= NULL_TREE
8254 && TREE_CODE (field
) == FIELD_DECL
8255 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
8256 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
8257 != TYPE_MAIN_VARIANT (type
))
8258 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
8260 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
8261 if (checktype
!= error_mark_node
8262 && (TYPE_MAIN_VARIANT (checktype
)
8263 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
8264 warning_init (OPT_Wc___compat
,
8265 "enum conversion in initialization is invalid in C++");
8268 /* If this field is empty (and not at the end of structure),
8269 don't do anything other than checking the initializer. */
8271 && (TREE_TYPE (field
) == error_mark_node
8272 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
8273 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
8274 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
8275 || DECL_CHAIN (field
)))))
8279 value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
8280 value
= digest_init (input_location
, type
, value
, origtype
, npc
,
8281 strict_string
, require_constant_value
);
8282 if (value
== error_mark_node
)
8284 constructor_erroneous
= 1;
8287 if (require_constant_value
|| require_constant_elements
)
8288 constant_expression_warning (value
);
8290 /* If this element doesn't come next in sequence,
8291 put it on constructor_pending_elts. */
8292 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
8293 && (!constructor_incremental
8294 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
8296 if (constructor_incremental
8297 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8298 set_nonincremental_init (braced_init_obstack
);
8300 add_pending_init (field
, value
, origtype
, implicit
,
8301 braced_init_obstack
);
8304 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8305 && (!constructor_incremental
8306 || field
!= constructor_unfilled_fields
))
8308 /* We do this for records but not for unions. In a union,
8309 no matter which field is specified, it can be initialized
8310 right away since it starts at the beginning of the union. */
8311 if (constructor_incremental
)
8313 if (!constructor_unfilled_fields
)
8314 set_nonincremental_init (braced_init_obstack
);
8317 tree bitpos
, unfillpos
;
8319 bitpos
= bit_position (field
);
8320 unfillpos
= bit_position (constructor_unfilled_fields
);
8322 if (tree_int_cst_lt (bitpos
, unfillpos
))
8323 set_nonincremental_init (braced_init_obstack
);
8327 add_pending_init (field
, value
, origtype
, implicit
,
8328 braced_init_obstack
);
8331 else if (TREE_CODE (constructor_type
) == UNION_TYPE
8332 && !vec_safe_is_empty (constructor_elements
))
8336 if (TREE_SIDE_EFFECTS (constructor_elements
->last ().value
))
8338 "initialized field with side-effects overwritten");
8339 else if (warn_override_init
)
8340 warning_init (OPT_Woverride_init
, "initialized field overwritten");
8343 /* We can have just one union field set. */
8344 constructor_elements
= NULL
;
8347 /* Otherwise, output this element either to
8348 constructor_elements or to the assembler file. */
8350 constructor_elt celt
= {field
, value
};
8351 vec_safe_push (constructor_elements
, celt
);
8353 /* Advance the variable that indicates sequential elements output. */
8354 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8355 constructor_unfilled_index
8356 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
8358 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8360 constructor_unfilled_fields
8361 = DECL_CHAIN (constructor_unfilled_fields
);
8363 /* Skip any nameless bit fields. */
8364 while (constructor_unfilled_fields
!= 0
8365 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8366 && DECL_NAME (constructor_unfilled_fields
) == 0)
8367 constructor_unfilled_fields
=
8368 DECL_CHAIN (constructor_unfilled_fields
);
8370 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8371 constructor_unfilled_fields
= 0;
8373 /* Now output any pending elements which have become next. */
8375 output_pending_init_elements (0, braced_init_obstack
);
8378 /* Output any pending elements which have become next.
8379 As we output elements, constructor_unfilled_{fields,index}
8380 advances, which may cause other elements to become next;
8381 if so, they too are output.
8383 If ALL is 0, we return when there are
8384 no more pending elements to output now.
8386 If ALL is 1, we output space as necessary so that
8387 we can output all the pending elements. */
8389 output_pending_init_elements (int all
, struct obstack
* braced_init_obstack
)
8391 struct init_node
*elt
= constructor_pending_elts
;
8396 /* Look through the whole pending tree.
8397 If we find an element that should be output now,
8398 output it. Otherwise, set NEXT to the element
8399 that comes first among those still pending. */
8404 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8406 if (tree_int_cst_equal (elt
->purpose
,
8407 constructor_unfilled_index
))
8408 output_init_element (elt
->value
, elt
->origtype
, true,
8409 TREE_TYPE (constructor_type
),
8410 constructor_unfilled_index
, 0, false,
8411 braced_init_obstack
);
8412 else if (tree_int_cst_lt (constructor_unfilled_index
,
8415 /* Advance to the next smaller node. */
8420 /* We have reached the smallest node bigger than the
8421 current unfilled index. Fill the space first. */
8422 next
= elt
->purpose
;
8428 /* Advance to the next bigger node. */
8433 /* We have reached the biggest node in a subtree. Find
8434 the parent of it, which is the next bigger node. */
8435 while (elt
->parent
&& elt
->parent
->right
== elt
)
8438 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
8441 next
= elt
->purpose
;
8447 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8448 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8450 tree ctor_unfilled_bitpos
, elt_bitpos
;
8452 /* If the current record is complete we are done. */
8453 if (constructor_unfilled_fields
== 0)
8456 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
8457 elt_bitpos
= bit_position (elt
->purpose
);
8458 /* We can't compare fields here because there might be empty
8459 fields in between. */
8460 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
8462 constructor_unfilled_fields
= elt
->purpose
;
8463 output_init_element (elt
->value
, elt
->origtype
, true,
8464 TREE_TYPE (elt
->purpose
),
8465 elt
->purpose
, 0, false,
8466 braced_init_obstack
);
8468 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
8470 /* Advance to the next smaller node. */
8475 /* We have reached the smallest node bigger than the
8476 current unfilled field. Fill the space first. */
8477 next
= elt
->purpose
;
8483 /* Advance to the next bigger node. */
8488 /* We have reached the biggest node in a subtree. Find
8489 the parent of it, which is the next bigger node. */
8490 while (elt
->parent
&& elt
->parent
->right
== elt
)
8494 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
8495 bit_position (elt
->purpose
))))
8497 next
= elt
->purpose
;
8505 /* Ordinarily return, but not if we want to output all
8506 and there are elements left. */
8507 if (!(all
&& next
!= 0))
8510 /* If it's not incremental, just skip over the gap, so that after
8511 jumping to retry we will output the next successive element. */
8512 if (TREE_CODE (constructor_type
) == RECORD_TYPE
8513 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8514 constructor_unfilled_fields
= next
;
8515 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8516 constructor_unfilled_index
= next
;
8518 /* ELT now points to the node in the pending tree with the next
8519 initializer to output. */
8523 /* Add one non-braced element to the current constructor level.
8524 This adjusts the current position within the constructor's type.
8525 This may also start or terminate implicit levels
8526 to handle a partly-braced initializer.
8528 Once this has found the correct level for the new element,
8529 it calls output_init_element.
8531 IMPLICIT is true if value comes from pop_init_level (1),
8532 the new initializer has been merged with the existing one
8533 and thus no warnings should be emitted about overriding an
8534 existing initializer. */
8537 process_init_element (struct c_expr value
, bool implicit
,
8538 struct obstack
* braced_init_obstack
)
8540 tree orig_value
= value
.value
;
8541 int string_flag
= orig_value
!= 0 && TREE_CODE (orig_value
) == STRING_CST
;
8542 bool strict_string
= value
.original_code
== STRING_CST
;
8543 bool was_designated
= designator_depth
!= 0;
8545 designator_depth
= 0;
8546 designator_erroneous
= 0;
8548 /* Handle superfluous braces around string cst as in
8549 char x[] = {"foo"}; */
8553 && TREE_CODE (constructor_type
) == ARRAY_TYPE
8554 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
8555 && integer_zerop (constructor_unfilled_index
))
8557 if (constructor_stack
->replacement_value
.value
)
8558 error_init ("excess elements in char array initializer");
8559 constructor_stack
->replacement_value
= value
;
8563 if (constructor_stack
->replacement_value
.value
!= 0)
8565 error_init ("excess elements in struct initializer");
8569 /* Ignore elements of a brace group if it is entirely superfluous
8570 and has already been diagnosed. */
8571 if (constructor_type
== 0)
8574 /* If we've exhausted any levels that didn't have braces,
8576 while (constructor_stack
->implicit
)
8578 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
8579 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8580 && constructor_fields
== 0)
8581 process_init_element (pop_init_level (1, braced_init_obstack
),
8582 true, braced_init_obstack
);
8583 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
8584 || TREE_CODE (constructor_type
) == VECTOR_TYPE
)
8585 && constructor_max_index
8586 && tree_int_cst_lt (constructor_max_index
,
8588 process_init_element (pop_init_level (1, braced_init_obstack
),
8589 true, braced_init_obstack
);
8594 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8595 if (constructor_range_stack
)
8597 /* If value is a compound literal and we'll be just using its
8598 content, don't put it into a SAVE_EXPR. */
8599 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
8600 || !require_constant_value
8603 tree semantic_type
= NULL_TREE
;
8604 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
8606 semantic_type
= TREE_TYPE (value
.value
);
8607 value
.value
= TREE_OPERAND (value
.value
, 0);
8609 value
.value
= c_save_expr (value
.value
);
8611 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
8618 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8621 enum tree_code fieldcode
;
8623 if (constructor_fields
== 0)
8625 pedwarn_init (input_location
, 0,
8626 "excess elements in struct initializer");
8630 fieldtype
= TREE_TYPE (constructor_fields
);
8631 if (fieldtype
!= error_mark_node
)
8632 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
8633 fieldcode
= TREE_CODE (fieldtype
);
8635 /* Error for non-static initialization of a flexible array member. */
8636 if (fieldcode
== ARRAY_TYPE
8637 && !require_constant_value
8638 && TYPE_SIZE (fieldtype
) == NULL_TREE
8639 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
8641 error_init ("non-static initialization of a flexible array member");
8645 /* Accept a string constant to initialize a subarray. */
8646 if (value
.value
!= 0
8647 && fieldcode
== ARRAY_TYPE
8648 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
8650 value
.value
= orig_value
;
8651 /* Otherwise, if we have come to a subaggregate,
8652 and we don't have an element of its type, push into it. */
8653 else if (value
.value
!= 0
8654 && value
.value
!= error_mark_node
8655 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
8656 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
8657 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
8659 push_init_level (1, braced_init_obstack
);
8665 push_member_name (constructor_fields
);
8666 output_init_element (value
.value
, value
.original_type
,
8667 strict_string
, fieldtype
,
8668 constructor_fields
, 1, implicit
,
8669 braced_init_obstack
);
8670 RESTORE_SPELLING_DEPTH (constructor_depth
);
8673 /* Do the bookkeeping for an element that was
8674 directly output as a constructor. */
8676 /* For a record, keep track of end position of last field. */
8677 if (DECL_SIZE (constructor_fields
))
8678 constructor_bit_index
8679 = size_binop_loc (input_location
, PLUS_EXPR
,
8680 bit_position (constructor_fields
),
8681 DECL_SIZE (constructor_fields
));
8683 /* If the current field was the first one not yet written out,
8684 it isn't now, so update. */
8685 if (constructor_unfilled_fields
== constructor_fields
)
8687 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
8688 /* Skip any nameless bit fields. */
8689 while (constructor_unfilled_fields
!= 0
8690 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8691 && DECL_NAME (constructor_unfilled_fields
) == 0)
8692 constructor_unfilled_fields
=
8693 DECL_CHAIN (constructor_unfilled_fields
);
8697 constructor_fields
= DECL_CHAIN (constructor_fields
);
8698 /* Skip any nameless bit fields at the beginning. */
8699 while (constructor_fields
!= 0
8700 && DECL_C_BIT_FIELD (constructor_fields
)
8701 && DECL_NAME (constructor_fields
) == 0)
8702 constructor_fields
= DECL_CHAIN (constructor_fields
);
8704 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8707 enum tree_code fieldcode
;
8709 if (constructor_fields
== 0)
8711 pedwarn_init (input_location
, 0,
8712 "excess elements in union initializer");
8716 fieldtype
= TREE_TYPE (constructor_fields
);
8717 if (fieldtype
!= error_mark_node
)
8718 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
8719 fieldcode
= TREE_CODE (fieldtype
);
8721 /* Warn that traditional C rejects initialization of unions.
8722 We skip the warning if the value is zero. This is done
8723 under the assumption that the zero initializer in user
8724 code appears conditioned on e.g. __STDC__ to avoid
8725 "missing initializer" warnings and relies on default
8726 initialization to zero in the traditional C case.
8727 We also skip the warning if the initializer is designated,
8728 again on the assumption that this must be conditional on
8729 __STDC__ anyway (and we've already complained about the
8730 member-designator already). */
8731 if (!in_system_header_at (input_location
) && !constructor_designated
8732 && !(value
.value
&& (integer_zerop (value
.value
)
8733 || real_zerop (value
.value
))))
8734 warning (OPT_Wtraditional
, "traditional C rejects initialization "
8737 /* Accept a string constant to initialize a subarray. */
8738 if (value
.value
!= 0
8739 && fieldcode
== ARRAY_TYPE
8740 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
8742 value
.value
= orig_value
;
8743 /* Otherwise, if we have come to a subaggregate,
8744 and we don't have an element of its type, push into it. */
8745 else if (value
.value
!= 0
8746 && value
.value
!= error_mark_node
8747 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
8748 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
8749 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
8751 push_init_level (1, braced_init_obstack
);
8757 push_member_name (constructor_fields
);
8758 output_init_element (value
.value
, value
.original_type
,
8759 strict_string
, fieldtype
,
8760 constructor_fields
, 1, implicit
,
8761 braced_init_obstack
);
8762 RESTORE_SPELLING_DEPTH (constructor_depth
);
8765 /* Do the bookkeeping for an element that was
8766 directly output as a constructor. */
8768 constructor_bit_index
= DECL_SIZE (constructor_fields
);
8769 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
8772 constructor_fields
= 0;
8774 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8776 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8777 enum tree_code eltcode
= TREE_CODE (elttype
);
8779 /* Accept a string constant to initialize a subarray. */
8780 if (value
.value
!= 0
8781 && eltcode
== ARRAY_TYPE
8782 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
8784 value
.value
= orig_value
;
8785 /* Otherwise, if we have come to a subaggregate,
8786 and we don't have an element of its type, push into it. */
8787 else if (value
.value
!= 0
8788 && value
.value
!= error_mark_node
8789 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
8790 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
8791 || eltcode
== UNION_TYPE
|| eltcode
== VECTOR_TYPE
))
8793 push_init_level (1, braced_init_obstack
);
8797 if (constructor_max_index
!= 0
8798 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
8799 || integer_all_onesp (constructor_max_index
)))
8801 pedwarn_init (input_location
, 0,
8802 "excess elements in array initializer");
8806 /* Now output the actual element. */
8809 push_array_bounds (tree_to_uhwi (constructor_index
));
8810 output_init_element (value
.value
, value
.original_type
,
8811 strict_string
, elttype
,
8812 constructor_index
, 1, implicit
,
8813 braced_init_obstack
);
8814 RESTORE_SPELLING_DEPTH (constructor_depth
);
8818 = size_binop_loc (input_location
, PLUS_EXPR
,
8819 constructor_index
, bitsize_one_node
);
8822 /* If we are doing the bookkeeping for an element that was
8823 directly output as a constructor, we must update
8824 constructor_unfilled_index. */
8825 constructor_unfilled_index
= constructor_index
;
8827 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
8829 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8831 /* Do a basic check of initializer size. Note that vectors
8832 always have a fixed size derived from their type. */
8833 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
8835 pedwarn_init (input_location
, 0,
8836 "excess elements in vector initializer");
8840 /* Now output the actual element. */
8843 if (TREE_CODE (value
.value
) == VECTOR_CST
)
8844 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
8845 output_init_element (value
.value
, value
.original_type
,
8846 strict_string
, elttype
,
8847 constructor_index
, 1, implicit
,
8848 braced_init_obstack
);
8852 = size_binop_loc (input_location
,
8853 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
8856 /* If we are doing the bookkeeping for an element that was
8857 directly output as a constructor, we must update
8858 constructor_unfilled_index. */
8859 constructor_unfilled_index
= constructor_index
;
8862 /* Handle the sole element allowed in a braced initializer
8863 for a scalar variable. */
8864 else if (constructor_type
!= error_mark_node
8865 && constructor_fields
== 0)
8867 pedwarn_init (input_location
, 0,
8868 "excess elements in scalar initializer");
8874 output_init_element (value
.value
, value
.original_type
,
8875 strict_string
, constructor_type
,
8876 NULL_TREE
, 1, implicit
,
8877 braced_init_obstack
);
8878 constructor_fields
= 0;
8881 /* Handle range initializers either at this level or anywhere higher
8882 in the designator stack. */
8883 if (constructor_range_stack
)
8885 struct constructor_range_stack
*p
, *range_stack
;
8888 range_stack
= constructor_range_stack
;
8889 constructor_range_stack
= 0;
8890 while (constructor_stack
!= range_stack
->stack
)
8892 gcc_assert (constructor_stack
->implicit
);
8893 process_init_element (pop_init_level (1,
8894 braced_init_obstack
),
8895 true, braced_init_obstack
);
8897 for (p
= range_stack
;
8898 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
8901 gcc_assert (constructor_stack
->implicit
);
8902 process_init_element (pop_init_level (1, braced_init_obstack
),
8903 true, braced_init_obstack
);
8906 p
->index
= size_binop_loc (input_location
,
8907 PLUS_EXPR
, p
->index
, bitsize_one_node
);
8908 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
8913 constructor_index
= p
->index
;
8914 constructor_fields
= p
->fields
;
8915 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
8923 push_init_level (2, braced_init_obstack
);
8924 p
->stack
= constructor_stack
;
8925 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
8926 p
->index
= p
->range_start
;
8930 constructor_range_stack
= range_stack
;
8937 constructor_range_stack
= 0;
8940 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8941 (guaranteed to be 'volatile' or null) and ARGS (represented using
8942 an ASM_EXPR node). */
8944 build_asm_stmt (tree cv_qualifier
, tree args
)
8946 if (!ASM_VOLATILE_P (args
) && cv_qualifier
)
8947 ASM_VOLATILE_P (args
) = 1;
8948 return add_stmt (args
);
8951 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8952 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8953 SIMPLE indicates whether there was anything at all after the
8954 string in the asm expression -- asm("blah") and asm("blah" : )
8955 are subtly different. We use a ASM_EXPR node to represent this. */
8957 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
8958 tree clobbers
, tree labels
, bool simple
)
8963 const char *constraint
;
8964 const char **oconstraints
;
8965 bool allows_mem
, allows_reg
, is_inout
;
8966 int ninputs
, noutputs
;
8968 ninputs
= list_length (inputs
);
8969 noutputs
= list_length (outputs
);
8970 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
8972 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
8974 /* Remove output conversions that change the type but not the mode. */
8975 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
8977 tree output
= TREE_VALUE (tail
);
8979 output
= c_fully_fold (output
, false, NULL
);
8981 /* ??? Really, this should not be here. Users should be using a
8982 proper lvalue, dammit. But there's a long history of using casts
8983 in the output operands. In cases like longlong.h, this becomes a
8984 primitive form of typechecking -- if the cast can be removed, then
8985 the output operand had a type of the proper width; otherwise we'll
8986 get an error. Gross, but ... */
8987 STRIP_NOPS (output
);
8989 if (!lvalue_or_else (loc
, output
, lv_asm
))
8990 output
= error_mark_node
;
8992 if (output
!= error_mark_node
8993 && (TREE_READONLY (output
)
8994 || TYPE_READONLY (TREE_TYPE (output
))
8995 || ((TREE_CODE (TREE_TYPE (output
)) == RECORD_TYPE
8996 || TREE_CODE (TREE_TYPE (output
)) == UNION_TYPE
)
8997 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
8998 readonly_error (loc
, output
, lv_asm
);
9000 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9001 oconstraints
[i
] = constraint
;
9003 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
9004 &allows_mem
, &allows_reg
, &is_inout
))
9006 /* If the operand is going to end up in memory,
9007 mark it addressable. */
9008 if (!allows_reg
&& !c_mark_addressable (output
))
9009 output
= error_mark_node
;
9010 if (!(!allows_reg
&& allows_mem
)
9011 && output
!= error_mark_node
9012 && VOID_TYPE_P (TREE_TYPE (output
)))
9014 error_at (loc
, "invalid use of void expression");
9015 output
= error_mark_node
;
9019 output
= error_mark_node
;
9021 TREE_VALUE (tail
) = output
;
9024 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
9028 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9029 input
= TREE_VALUE (tail
);
9031 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
9032 oconstraints
, &allows_mem
, &allows_reg
))
9034 /* If the operand is going to end up in memory,
9035 mark it addressable. */
9036 if (!allows_reg
&& allows_mem
)
9038 input
= c_fully_fold (input
, false, NULL
);
9040 /* Strip the nops as we allow this case. FIXME, this really
9041 should be rejected or made deprecated. */
9043 if (!c_mark_addressable (input
))
9044 input
= error_mark_node
;
9049 memset (&expr
, 0, sizeof (expr
));
9051 expr
= convert_lvalue_to_rvalue (loc
, expr
, true, false);
9052 input
= c_fully_fold (expr
.value
, false, NULL
);
9054 if (input
!= error_mark_node
&& VOID_TYPE_P (TREE_TYPE (input
)))
9056 error_at (loc
, "invalid use of void expression");
9057 input
= error_mark_node
;
9062 input
= error_mark_node
;
9064 TREE_VALUE (tail
) = input
;
9067 /* ASMs with labels cannot have outputs. This should have been
9068 enforced by the parser. */
9069 gcc_assert (outputs
== NULL
|| labels
== NULL
);
9071 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
9073 /* asm statements without outputs, including simple ones, are treated
9075 ASM_INPUT_P (args
) = simple
;
9076 ASM_VOLATILE_P (args
) = (noutputs
== 0);
9081 /* Generate a goto statement to LABEL. LOC is the location of the
9085 c_finish_goto_label (location_t loc
, tree label
)
9087 tree decl
= lookup_label_for_goto (loc
, label
);
9090 TREE_USED (decl
) = 1;
9092 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
9093 SET_EXPR_LOCATION (t
, loc
);
9094 return add_stmt (t
);
9098 /* Generate a computed goto statement to EXPR. LOC is the location of
9102 c_finish_goto_ptr (location_t loc
, tree expr
)
9105 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids %<goto *expr;%>");
9106 expr
= c_fully_fold (expr
, false, NULL
);
9107 expr
= convert (ptr_type_node
, expr
);
9108 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
9109 SET_EXPR_LOCATION (t
, loc
);
9110 return add_stmt (t
);
9113 /* Generate a C `return' statement. RETVAL is the expression for what
9114 to return, or a null pointer for `return;' with no value. LOC is
9115 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
9116 is the original type of RETVAL. */
9119 c_finish_return (location_t loc
, tree retval
, tree origtype
)
9121 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
9122 bool no_warning
= false;
9126 if (TREE_THIS_VOLATILE (current_function_decl
))
9128 "function declared %<noreturn%> has a %<return%> statement");
9130 if (flag_cilkplus
&& contains_array_notation_expr (retval
))
9132 /* Array notations are allowed in a return statement if it is inside a
9133 built-in array notation reduction function. */
9134 if (!find_rank (loc
, retval
, retval
, false, &rank
))
9135 return error_mark_node
;
9138 error_at (loc
, "array notation expression cannot be used as a "
9140 return error_mark_node
;
9143 if (flag_cilkplus
&& retval
&& contains_cilk_spawn_stmt (retval
))
9145 error_at (loc
, "use of %<_Cilk_spawn%> in a return statement is not "
9147 return error_mark_node
;
9151 tree semantic_type
= NULL_TREE
;
9152 npc
= null_pointer_constant_p (retval
);
9153 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
9155 semantic_type
= TREE_TYPE (retval
);
9156 retval
= TREE_OPERAND (retval
, 0);
9158 retval
= c_fully_fold (retval
, false, NULL
);
9160 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
9165 current_function_returns_null
= 1;
9166 if ((warn_return_type
|| flag_isoc99
)
9167 && valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
9169 pedwarn_c99 (loc
, flag_isoc99
? 0 : OPT_Wreturn_type
,
9170 "%<return%> with no value, in "
9171 "function returning non-void");
9175 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
9177 current_function_returns_null
= 1;
9178 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
9180 "%<return%> with a value, in function returning void");
9182 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
9183 "%<return%> with expression, in function returning void");
9187 tree t
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, valtype
,
9188 retval
, origtype
, ic_return
,
9189 npc
, NULL_TREE
, NULL_TREE
, 0);
9190 tree res
= DECL_RESULT (current_function_decl
);
9194 current_function_returns_value
= 1;
9195 if (t
== error_mark_node
)
9198 save
= in_late_binary_op
;
9199 if (TREE_CODE (TREE_TYPE (res
)) == BOOLEAN_TYPE
9200 || TREE_CODE (TREE_TYPE (res
)) == COMPLEX_TYPE
)
9201 in_late_binary_op
= true;
9202 inner
= t
= convert (TREE_TYPE (res
), t
);
9203 in_late_binary_op
= save
;
9205 /* Strip any conversions, additions, and subtractions, and see if
9206 we are returning the address of a local variable. Warn if so. */
9209 switch (TREE_CODE (inner
))
9212 case NON_LVALUE_EXPR
:
9214 case POINTER_PLUS_EXPR
:
9215 inner
= TREE_OPERAND (inner
, 0);
9219 /* If the second operand of the MINUS_EXPR has a pointer
9220 type (or is converted from it), this may be valid, so
9221 don't give a warning. */
9223 tree op1
= TREE_OPERAND (inner
, 1);
9225 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
9226 && (CONVERT_EXPR_P (op1
)
9227 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
9228 op1
= TREE_OPERAND (op1
, 0);
9230 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
9233 inner
= TREE_OPERAND (inner
, 0);
9238 inner
= TREE_OPERAND (inner
, 0);
9240 while (REFERENCE_CLASS_P (inner
)
9241 && TREE_CODE (inner
) != INDIRECT_REF
)
9242 inner
= TREE_OPERAND (inner
, 0);
9245 && !DECL_EXTERNAL (inner
)
9246 && !TREE_STATIC (inner
)
9247 && DECL_CONTEXT (inner
) == current_function_decl
)
9249 OPT_Wreturn_local_addr
, "function returns address "
9250 "of local variable");
9260 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
9261 SET_EXPR_LOCATION (retval
, loc
);
9263 if (warn_sequence_point
)
9264 verify_sequence_points (retval
);
9267 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
9268 TREE_NO_WARNING (ret_stmt
) |= no_warning
;
9269 return add_stmt (ret_stmt
);
9273 /* The SWITCH_EXPR being built. */
9276 /* The original type of the testing expression, i.e. before the
9277 default conversion is applied. */
9280 /* A splay-tree mapping the low element of a case range to the high
9281 element, or NULL_TREE if there is no high element. Used to
9282 determine whether or not a new case label duplicates an old case
9283 label. We need a tree, rather than simply a hash table, because
9284 of the GNU case range extension. */
9287 /* The bindings at the point of the switch. This is used for
9288 warnings crossing decls when branching to a case label. */
9289 struct c_spot_bindings
*bindings
;
9291 /* The next node on the stack. */
9292 struct c_switch
*next
;
9295 /* A stack of the currently active switch statements. The innermost
9296 switch statement is on the top of the stack. There is no need to
9297 mark the stack for garbage collection because it is only active
9298 during the processing of the body of a function, and we never
9299 collect at that point. */
9301 struct c_switch
*c_switch_stack
;
9303 /* Start a C switch statement, testing expression EXP. Return the new
9304 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9305 SWITCH_COND_LOC is the location of the switch's condition. */
9308 c_start_case (location_t switch_loc
,
9309 location_t switch_cond_loc
,
9312 tree orig_type
= error_mark_node
;
9313 struct c_switch
*cs
;
9315 if (exp
!= error_mark_node
)
9317 orig_type
= TREE_TYPE (exp
);
9319 if (!INTEGRAL_TYPE_P (orig_type
))
9321 if (orig_type
!= error_mark_node
)
9323 error_at (switch_cond_loc
, "switch quantity not an integer");
9324 orig_type
= error_mark_node
;
9326 exp
= integer_zero_node
;
9330 tree type
= TYPE_MAIN_VARIANT (orig_type
);
9332 if (!in_system_header_at (input_location
)
9333 && (type
== long_integer_type_node
9334 || type
== long_unsigned_type_node
))
9335 warning_at (switch_cond_loc
,
9336 OPT_Wtraditional
, "%<long%> switch expression not "
9337 "converted to %<int%> in ISO C");
9339 exp
= c_fully_fold (exp
, false, NULL
);
9340 exp
= default_conversion (exp
);
9342 if (warn_sequence_point
)
9343 verify_sequence_points (exp
);
9347 /* Add this new SWITCH_EXPR to the stack. */
9348 cs
= XNEW (struct c_switch
);
9349 cs
->switch_expr
= build3 (SWITCH_EXPR
, orig_type
, exp
, NULL_TREE
, NULL_TREE
);
9350 SET_EXPR_LOCATION (cs
->switch_expr
, switch_loc
);
9351 cs
->orig_type
= orig_type
;
9352 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
9353 cs
->bindings
= c_get_switch_bindings ();
9354 cs
->next
= c_switch_stack
;
9355 c_switch_stack
= cs
;
9357 return add_stmt (cs
->switch_expr
);
9360 /* Process a case label at location LOC. */
9363 do_case (location_t loc
, tree low_value
, tree high_value
)
9365 tree label
= NULL_TREE
;
9367 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
9369 low_value
= c_fully_fold (low_value
, false, NULL
);
9370 if (TREE_CODE (low_value
) == INTEGER_CST
)
9371 pedwarn (input_location
, OPT_Wpedantic
,
9372 "case label is not an integer constant expression");
9375 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
9377 high_value
= c_fully_fold (high_value
, false, NULL
);
9378 if (TREE_CODE (high_value
) == INTEGER_CST
)
9379 pedwarn (input_location
, OPT_Wpedantic
,
9380 "case label is not an integer constant expression");
9383 if (c_switch_stack
== NULL
)
9386 error_at (loc
, "case label not within a switch statement");
9388 error_at (loc
, "%<default%> label not within a switch statement");
9392 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
9393 EXPR_LOCATION (c_switch_stack
->switch_expr
),
9397 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
9398 SWITCH_COND (c_switch_stack
->switch_expr
),
9399 c_switch_stack
->orig_type
,
9400 low_value
, high_value
);
9401 if (label
== error_mark_node
)
9406 /* Finish the switch statement. */
9409 c_finish_case (tree body
)
9411 struct c_switch
*cs
= c_switch_stack
;
9412 location_t switch_location
;
9414 SWITCH_BODY (cs
->switch_expr
) = body
;
9416 /* Emit warnings as needed. */
9417 switch_location
= EXPR_LOCATION (cs
->switch_expr
);
9418 c_do_switch_warnings (cs
->cases
, switch_location
,
9419 TREE_TYPE (cs
->switch_expr
),
9420 SWITCH_COND (cs
->switch_expr
));
9422 /* Pop the stack. */
9423 c_switch_stack
= cs
->next
;
9424 splay_tree_delete (cs
->cases
);
9425 c_release_switch_bindings (cs
->bindings
);
9429 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9430 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9431 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9432 statement, and was not surrounded with parenthesis. */
9435 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
9436 tree else_block
, bool nested_if
)
9440 /* If the condition has array notations, then the rank of the then_block and
9441 else_block must be either 0 or be equal to the rank of the condition. If
9442 the condition does not have array notations then break them up as it is
9443 broken up in a normal expression. */
9444 if (flag_cilkplus
&& contains_array_notation_expr (cond
))
9446 size_t then_rank
= 0, cond_rank
= 0, else_rank
= 0;
9447 if (!find_rank (if_locus
, cond
, cond
, true, &cond_rank
))
9450 && !find_rank (if_locus
, then_block
, then_block
, true, &then_rank
))
9453 && !find_rank (if_locus
, else_block
, else_block
, true, &else_rank
))
9455 if (cond_rank
!= then_rank
&& then_rank
!= 0)
9457 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
9458 " and the then-block");
9461 else if (cond_rank
!= else_rank
&& else_rank
!= 0)
9463 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
9464 " and the else-block");
9468 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9469 if (warn_parentheses
&& nested_if
&& else_block
== NULL
)
9471 tree inner_if
= then_block
;
9473 /* We know from the grammar productions that there is an IF nested
9474 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9475 it might not be exactly THEN_BLOCK, but should be the last
9476 non-container statement within. */
9478 switch (TREE_CODE (inner_if
))
9483 inner_if
= BIND_EXPR_BODY (inner_if
);
9485 case STATEMENT_LIST
:
9486 inner_if
= expr_last (then_block
);
9488 case TRY_FINALLY_EXPR
:
9489 case TRY_CATCH_EXPR
:
9490 inner_if
= TREE_OPERAND (inner_if
, 0);
9497 if (COND_EXPR_ELSE (inner_if
))
9498 warning_at (if_locus
, OPT_Wparentheses
,
9499 "suggest explicit braces to avoid ambiguous %<else%>");
9502 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
9503 SET_EXPR_LOCATION (stmt
, if_locus
);
9507 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9508 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9509 is false for DO loops. INCR is the FOR increment expression. BODY is
9510 the statement controlled by the loop. BLAB is the break label. CLAB is
9511 the continue label. Everything is allowed to be NULL. */
9514 c_finish_loop (location_t start_locus
, tree cond
, tree incr
, tree body
,
9515 tree blab
, tree clab
, bool cond_is_first
)
9517 tree entry
= NULL
, exit
= NULL
, t
;
9519 if (flag_cilkplus
&& contains_array_notation_expr (cond
))
9521 error_at (start_locus
, "array notation expression cannot be used in a "
9522 "loop%'s condition");
9526 /* If the condition is zero don't generate a loop construct. */
9527 if (cond
&& integer_zerop (cond
))
9531 t
= build_and_jump (&blab
);
9532 SET_EXPR_LOCATION (t
, start_locus
);
9538 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
9540 /* If we have an exit condition, then we build an IF with gotos either
9541 out of the loop, or to the top of it. If there's no exit condition,
9542 then we just build a jump back to the top. */
9543 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
9545 if (cond
&& !integer_nonzerop (cond
))
9547 /* Canonicalize the loop condition to the end. This means
9548 generating a branch to the loop condition. Reuse the
9549 continue label, if possible. */
9554 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
9555 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
9558 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
9559 SET_EXPR_LOCATION (t
, start_locus
);
9563 t
= build_and_jump (&blab
);
9565 exit
= fold_build3_loc (start_locus
,
9566 COND_EXPR
, void_type_node
, cond
, exit
, t
);
9568 exit
= fold_build3_loc (input_location
,
9569 COND_EXPR
, void_type_node
, cond
, exit
, t
);
9578 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
9586 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
9590 c_finish_bc_stmt (location_t loc
, tree
*label_p
, bool is_break
)
9593 tree label
= *label_p
;
9595 /* In switch statements break is sometimes stylistically used after
9596 a return statement. This can lead to spurious warnings about
9597 control reaching the end of a non-void function when it is
9598 inlined. Note that we are calling block_may_fallthru with
9599 language specific tree nodes; this works because
9600 block_may_fallthru returns true when given something it does not
9602 skip
= !block_may_fallthru (cur_stmt_list
);
9607 *label_p
= label
= create_artificial_label (loc
);
9609 else if (TREE_CODE (label
) == LABEL_DECL
)
9611 else switch (TREE_INT_CST_LOW (label
))
9615 error_at (loc
, "break statement not within loop or switch");
9617 error_at (loc
, "continue statement not within a loop");
9621 gcc_assert (is_break
);
9622 error_at (loc
, "break statement used with OpenMP for loop");
9627 error ("break statement within %<#pragma simd%> loop body");
9629 error ("continue statement within %<#pragma simd%> loop body");
9640 add_stmt (build_predict_expr (PRED_CONTINUE
, NOT_TAKEN
));
9642 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
9645 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9648 emit_side_effect_warnings (location_t loc
, tree expr
)
9650 if (expr
== error_mark_node
)
9652 else if (!TREE_SIDE_EFFECTS (expr
))
9654 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
9655 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
9657 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
9660 location_t cloc
= loc
;
9661 while (TREE_CODE (r
) == COMPOUND_EXPR
)
9663 if (EXPR_HAS_LOCATION (r
))
9664 cloc
= EXPR_LOCATION (r
);
9665 r
= TREE_OPERAND (r
, 1);
9667 if (!TREE_SIDE_EFFECTS (r
)
9668 && !VOID_TYPE_P (TREE_TYPE (r
))
9669 && !CONVERT_EXPR_P (r
)
9670 && !TREE_NO_WARNING (r
)
9671 && !TREE_NO_WARNING (expr
))
9672 warning_at (cloc
, OPT_Wunused_value
,
9673 "right-hand operand of comma expression has no effect");
9676 warn_if_unused_value (expr
, loc
);
9679 /* Process an expression as if it were a complete statement. Emit
9680 diagnostics, but do not call ADD_STMT. LOC is the location of the
9684 c_process_expr_stmt (location_t loc
, tree expr
)
9691 expr
= c_fully_fold (expr
, false, NULL
);
9693 if (warn_sequence_point
)
9694 verify_sequence_points (expr
);
9696 if (TREE_TYPE (expr
) != error_mark_node
9697 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
9698 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
9699 error_at (loc
, "expression statement has incomplete type");
9701 /* If we're not processing a statement expression, warn about unused values.
9702 Warnings for statement expressions will be emitted later, once we figure
9703 out which is the result. */
9704 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
9705 && warn_unused_value
)
9706 emit_side_effect_warnings (loc
, expr
);
9709 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
9710 exprv
= TREE_OPERAND (exprv
, 1);
9711 while (CONVERT_EXPR_P (exprv
))
9712 exprv
= TREE_OPERAND (exprv
, 0);
9714 || handled_component_p (exprv
)
9715 || TREE_CODE (exprv
) == ADDR_EXPR
)
9716 mark_exp_read (exprv
);
9718 /* If the expression is not of a type to which we cannot assign a line
9719 number, wrap the thing in a no-op NOP_EXPR. */
9720 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
9722 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
9723 SET_EXPR_LOCATION (expr
, loc
);
9729 /* Emit an expression as a statement. LOC is the location of the
9733 c_finish_expr_stmt (location_t loc
, tree expr
)
9736 return add_stmt (c_process_expr_stmt (loc
, expr
));
9741 /* Do the opposite and emit a statement as an expression. To begin,
9742 create a new binding level and return it. */
9745 c_begin_stmt_expr (void)
9749 /* We must force a BLOCK for this level so that, if it is not expanded
9750 later, there is a way to turn off the entire subtree of blocks that
9751 are contained in it. */
9753 ret
= c_begin_compound_stmt (true);
9755 c_bindings_start_stmt_expr (c_switch_stack
== NULL
9757 : c_switch_stack
->bindings
);
9759 /* Mark the current statement list as belonging to a statement list. */
9760 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
9765 /* LOC is the location of the compound statement to which this body
9769 c_finish_stmt_expr (location_t loc
, tree body
)
9771 tree last
, type
, tmp
, val
;
9774 body
= c_end_compound_stmt (loc
, body
, true);
9776 c_bindings_end_stmt_expr (c_switch_stack
== NULL
9778 : c_switch_stack
->bindings
);
9780 /* Locate the last statement in BODY. See c_end_compound_stmt
9781 about always returning a BIND_EXPR. */
9782 last_p
= &BIND_EXPR_BODY (body
);
9783 last
= BIND_EXPR_BODY (body
);
9786 if (TREE_CODE (last
) == STATEMENT_LIST
)
9788 tree_stmt_iterator i
;
9790 /* This can happen with degenerate cases like ({ }). No value. */
9791 if (!TREE_SIDE_EFFECTS (last
))
9794 /* If we're supposed to generate side effects warnings, process
9795 all of the statements except the last. */
9796 if (warn_unused_value
)
9798 for (i
= tsi_start (last
); !tsi_one_before_end_p (i
); tsi_next (&i
))
9801 tree t
= tsi_stmt (i
);
9803 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
9804 emit_side_effect_warnings (tloc
, t
);
9808 i
= tsi_last (last
);
9809 last_p
= tsi_stmt_ptr (i
);
9813 /* If the end of the list is exception related, then the list was split
9814 by a call to push_cleanup. Continue searching. */
9815 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
9816 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
9818 last_p
= &TREE_OPERAND (last
, 0);
9820 goto continue_searching
;
9823 if (last
== error_mark_node
)
9826 /* In the case that the BIND_EXPR is not necessary, return the
9827 expression out from inside it. */
9828 if (last
== BIND_EXPR_BODY (body
)
9829 && BIND_EXPR_VARS (body
) == NULL
)
9831 /* Even if this looks constant, do not allow it in a constant
9833 last
= c_wrap_maybe_const (last
, true);
9834 /* Do not warn if the return value of a statement expression is
9836 TREE_NO_WARNING (last
) = 1;
9840 /* Extract the type of said expression. */
9841 type
= TREE_TYPE (last
);
9843 /* If we're not returning a value at all, then the BIND_EXPR that
9844 we already have is a fine expression to return. */
9845 if (!type
|| VOID_TYPE_P (type
))
9848 /* Now that we've located the expression containing the value, it seems
9849 silly to make voidify_wrapper_expr repeat the process. Create a
9850 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9851 tmp
= create_tmp_var_raw (type
, NULL
);
9853 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9854 tree_expr_nonnegative_p giving up immediately. */
9856 if (TREE_CODE (val
) == NOP_EXPR
9857 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
9858 val
= TREE_OPERAND (val
, 0);
9860 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
9861 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
9864 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
9865 SET_EXPR_LOCATION (t
, loc
);
9870 /* Begin and end compound statements. This is as simple as pushing
9871 and popping new statement lists from the tree. */
9874 c_begin_compound_stmt (bool do_scope
)
9876 tree stmt
= push_stmt_list ();
9882 /* End a compound statement. STMT is the statement. LOC is the
9883 location of the compound statement-- this is usually the location
9884 of the opening brace. */
9887 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
9893 if (c_dialect_objc ())
9894 objc_clear_super_receiver ();
9895 block
= pop_scope ();
9898 stmt
= pop_stmt_list (stmt
);
9899 stmt
= c_build_bind_expr (loc
, block
, stmt
);
9901 /* If this compound statement is nested immediately inside a statement
9902 expression, then force a BIND_EXPR to be created. Otherwise we'll
9903 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9904 STATEMENT_LISTs merge, and thus we can lose track of what statement
9906 if (building_stmt_list_p ()
9907 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
9908 && TREE_CODE (stmt
) != BIND_EXPR
)
9910 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
9911 TREE_SIDE_EFFECTS (stmt
) = 1;
9912 SET_EXPR_LOCATION (stmt
, loc
);
9918 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9919 when the current scope is exited. EH_ONLY is true when this is not
9920 meant to apply to normal control flow transfer. */
9923 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
9925 enum tree_code code
;
9929 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
9930 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
9932 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
9933 list
= push_stmt_list ();
9934 TREE_OPERAND (stmt
, 0) = list
;
9935 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
9938 /* Build a binary-operation expression without default conversions.
9939 CODE is the kind of expression to build.
9940 LOCATION is the operator's location.
9941 This function differs from `build' in several ways:
9942 the data type of the result is computed and recorded in it,
9943 warnings are generated if arg data types are invalid,
9944 special handling for addition and subtraction of pointers is known,
9945 and some optimization is done (operations on narrow ints
9946 are done in the narrower type when that gives the same result).
9947 Constant folding is also done before the result is returned.
9949 Note that the operands will never have enumeral types, or function
9950 or array types, because either they will have the default conversions
9951 performed or they have both just been converted to some other type in which
9952 the arithmetic is to be done. */
9955 build_binary_op (location_t location
, enum tree_code code
,
9956 tree orig_op0
, tree orig_op1
, int convert_p
)
9958 tree type0
, type1
, orig_type0
, orig_type1
;
9960 enum tree_code code0
, code1
;
9962 tree ret
= error_mark_node
;
9963 const char *invalid_op_diag
;
9964 bool op0_int_operands
, op1_int_operands
;
9965 bool int_const
, int_const_or_overflow
, int_operands
;
9967 /* Expression code to give to the expression when it is built.
9968 Normally this is CODE, which is what the caller asked for,
9969 but in some special cases we change it. */
9970 enum tree_code resultcode
= code
;
9972 /* Data type in which the computation is to be performed.
9973 In the simplest cases this is the common type of the arguments. */
9974 tree result_type
= NULL
;
9976 /* When the computation is in excess precision, the type of the
9977 final EXCESS_PRECISION_EXPR. */
9978 tree semantic_result_type
= NULL
;
9980 /* Nonzero means operands have already been type-converted
9981 in whatever way is necessary.
9982 Zero means they need to be converted to RESULT_TYPE. */
9985 /* Nonzero means create the expression with this type, rather than
9987 tree build_type
= 0;
9989 /* Nonzero means after finally constructing the expression
9990 convert it to this type. */
9991 tree final_type
= 0;
9993 /* Nonzero if this is an operation like MIN or MAX which can
9994 safely be computed in short if both args are promoted shorts.
9995 Also implies COMMON.
9996 -1 indicates a bitwise operation; this makes a difference
9997 in the exact conditions for when it is safe to do the operation
9998 in a narrower mode. */
10001 /* Nonzero if this is a comparison operation;
10002 if both args are promoted shorts, compare the original shorts.
10003 Also implies COMMON. */
10004 int short_compare
= 0;
10006 /* Nonzero if this is a right-shift operation, which can be computed on the
10007 original short and then promoted if the operand is a promoted short. */
10008 int short_shift
= 0;
10010 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10013 /* True means types are compatible as far as ObjC is concerned. */
10016 /* True means this is an arithmetic operation that may need excess
10018 bool may_need_excess_precision
;
10020 /* True means this is a boolean operation that converts both its
10021 operands to truth-values. */
10022 bool boolean_op
= false;
10024 /* Remember whether we're doing / or %. */
10025 bool doing_div_or_mod
= false;
10027 /* Remember whether we're doing << or >>. */
10028 bool doing_shift
= false;
10030 /* Tree holding instrumentation expression. */
10031 tree instrument_expr
= NULL
;
10033 if (location
== UNKNOWN_LOCATION
)
10034 location
= input_location
;
10039 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
10040 if (op0_int_operands
)
10041 op0
= remove_c_maybe_const_expr (op0
);
10042 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
10043 if (op1_int_operands
)
10044 op1
= remove_c_maybe_const_expr (op1
);
10045 int_operands
= (op0_int_operands
&& op1_int_operands
);
10048 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
10049 && TREE_CODE (orig_op1
) == INTEGER_CST
);
10050 int_const
= (int_const_or_overflow
10051 && !TREE_OVERFLOW (orig_op0
)
10052 && !TREE_OVERFLOW (orig_op1
));
10055 int_const
= int_const_or_overflow
= false;
10057 /* Do not apply default conversion in mixed vector/scalar expression. */
10059 && !((TREE_CODE (TREE_TYPE (op0
)) == VECTOR_TYPE
)
10060 != (TREE_CODE (TREE_TYPE (op1
)) == VECTOR_TYPE
)))
10062 op0
= default_conversion (op0
);
10063 op1
= default_conversion (op1
);
10066 /* When Cilk Plus is enabled and there are array notations inside op0, then
10067 we check to see if there are builtin array notation functions. If
10068 so, then we take on the type of the array notation inside it. */
10069 if (flag_cilkplus
&& contains_array_notation_expr (op0
))
10070 orig_type0
= type0
= find_correct_array_notation_type (op0
);
10072 orig_type0
= type0
= TREE_TYPE (op0
);
10074 if (flag_cilkplus
&& contains_array_notation_expr (op1
))
10075 orig_type1
= type1
= find_correct_array_notation_type (op1
);
10077 orig_type1
= type1
= TREE_TYPE (op1
);
10079 /* The expression codes of the data types of the arguments tell us
10080 whether the arguments are integers, floating, pointers, etc. */
10081 code0
= TREE_CODE (type0
);
10082 code1
= TREE_CODE (type1
);
10084 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10085 STRIP_TYPE_NOPS (op0
);
10086 STRIP_TYPE_NOPS (op1
);
10088 /* If an error was already reported for one of the arguments,
10089 avoid reporting another error. */
10091 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10092 return error_mark_node
;
10094 if ((invalid_op_diag
10095 = targetm
.invalid_binary_op (code
, type0
, type1
)))
10097 error_at (location
, invalid_op_diag
);
10098 return error_mark_node
;
10106 case TRUNC_DIV_EXPR
:
10107 case CEIL_DIV_EXPR
:
10108 case FLOOR_DIV_EXPR
:
10109 case ROUND_DIV_EXPR
:
10110 case EXACT_DIV_EXPR
:
10111 may_need_excess_precision
= true;
10114 may_need_excess_precision
= false;
10117 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
10119 op0
= TREE_OPERAND (op0
, 0);
10120 type0
= TREE_TYPE (op0
);
10122 else if (may_need_excess_precision
10123 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
10126 op0
= convert (eptype
, op0
);
10128 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
10130 op1
= TREE_OPERAND (op1
, 0);
10131 type1
= TREE_TYPE (op1
);
10133 else if (may_need_excess_precision
10134 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
10137 op1
= convert (eptype
, op1
);
10140 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
10142 /* In case when one of the operands of the binary operation is
10143 a vector and another is a scalar -- convert scalar to vector. */
10144 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
10146 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
10149 switch (convert_flag
)
10152 return error_mark_node
;
10155 bool maybe_const
= true;
10157 sc
= c_fully_fold (op0
, false, &maybe_const
);
10158 sc
= save_expr (sc
);
10159 sc
= convert (TREE_TYPE (type1
), sc
);
10160 op0
= build_vector_from_val (type1
, sc
);
10162 op0
= c_wrap_maybe_const (op0
, true);
10163 orig_type0
= type0
= TREE_TYPE (op0
);
10164 code0
= TREE_CODE (type0
);
10168 case stv_secondarg
:
10170 bool maybe_const
= true;
10172 sc
= c_fully_fold (op1
, false, &maybe_const
);
10173 sc
= save_expr (sc
);
10174 sc
= convert (TREE_TYPE (type0
), sc
);
10175 op1
= build_vector_from_val (type0
, sc
);
10177 op1
= c_wrap_maybe_const (op1
, true);
10178 orig_type1
= type1
= TREE_TYPE (op1
);
10179 code1
= TREE_CODE (type1
);
10191 /* Handle the pointer + int case. */
10192 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10194 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
10195 goto return_build_binary_op
;
10197 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
10199 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
10200 goto return_build_binary_op
;
10207 /* Subtraction of two similar pointers.
10208 We must subtract them as integers, then divide by object size. */
10209 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
10210 && comp_target_types (location
, type0
, type1
))
10212 ret
= pointer_diff (location
, op0
, op1
);
10213 goto return_build_binary_op
;
10215 /* Handle pointer minus int. Just like pointer plus int. */
10216 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10218 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
10219 goto return_build_binary_op
;
10229 case TRUNC_DIV_EXPR
:
10230 case CEIL_DIV_EXPR
:
10231 case FLOOR_DIV_EXPR
:
10232 case ROUND_DIV_EXPR
:
10233 case EXACT_DIV_EXPR
:
10234 doing_div_or_mod
= true;
10235 warn_for_div_by_zero (location
, op1
);
10237 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10238 || code0
== FIXED_POINT_TYPE
10239 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10240 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10241 || code1
== FIXED_POINT_TYPE
10242 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
10244 enum tree_code tcode0
= code0
, tcode1
= code1
;
10246 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10247 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
10248 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
10249 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
10251 if (!((tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
)
10252 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
10253 resultcode
= RDIV_EXPR
;
10255 /* Although it would be tempting to shorten always here, that
10256 loses on some targets, since the modulo instruction is
10257 undefined if the quotient can't be represented in the
10258 computation mode. We shorten only if unsigned or if
10259 dividing by something we know != -1. */
10260 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10261 || (TREE_CODE (op1
) == INTEGER_CST
10262 && !integer_all_onesp (op1
)));
10270 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10272 /* Allow vector types which are not floating point types. */
10273 else if (code0
== VECTOR_TYPE
10274 && code1
== VECTOR_TYPE
10275 && !VECTOR_FLOAT_TYPE_P (type0
)
10276 && !VECTOR_FLOAT_TYPE_P (type1
))
10280 case TRUNC_MOD_EXPR
:
10281 case FLOOR_MOD_EXPR
:
10282 doing_div_or_mod
= true;
10283 warn_for_div_by_zero (location
, op1
);
10285 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10286 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10287 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
10289 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10291 /* Although it would be tempting to shorten always here, that loses
10292 on some targets, since the modulo instruction is undefined if the
10293 quotient can't be represented in the computation mode. We shorten
10294 only if unsigned or if dividing by something we know != -1. */
10295 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10296 || (TREE_CODE (op1
) == INTEGER_CST
10297 && !integer_all_onesp (op1
)));
10302 case TRUTH_ANDIF_EXPR
:
10303 case TRUTH_ORIF_EXPR
:
10304 case TRUTH_AND_EXPR
:
10305 case TRUTH_OR_EXPR
:
10306 case TRUTH_XOR_EXPR
:
10307 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
10308 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
10309 || code0
== FIXED_POINT_TYPE
)
10310 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
10311 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
10312 || code1
== FIXED_POINT_TYPE
))
10314 /* Result of these operations is always an int,
10315 but that does not mean the operands should be
10316 converted to ints! */
10317 result_type
= integer_type_node
;
10318 if (op0_int_operands
)
10320 op0
= c_objc_common_truthvalue_conversion (location
, orig_op0
);
10321 op0
= remove_c_maybe_const_expr (op0
);
10324 op0
= c_objc_common_truthvalue_conversion (location
, op0
);
10325 if (op1_int_operands
)
10327 op1
= c_objc_common_truthvalue_conversion (location
, orig_op1
);
10328 op1
= remove_c_maybe_const_expr (op1
);
10331 op1
= c_objc_common_truthvalue_conversion (location
, op1
);
10335 if (code
== TRUTH_ANDIF_EXPR
)
10337 int_const_or_overflow
= (int_operands
10338 && TREE_CODE (orig_op0
) == INTEGER_CST
10339 && (op0
== truthvalue_false_node
10340 || TREE_CODE (orig_op1
) == INTEGER_CST
));
10341 int_const
= (int_const_or_overflow
10342 && !TREE_OVERFLOW (orig_op0
)
10343 && (op0
== truthvalue_false_node
10344 || !TREE_OVERFLOW (orig_op1
)));
10346 else if (code
== TRUTH_ORIF_EXPR
)
10348 int_const_or_overflow
= (int_operands
10349 && TREE_CODE (orig_op0
) == INTEGER_CST
10350 && (op0
== truthvalue_true_node
10351 || TREE_CODE (orig_op1
) == INTEGER_CST
));
10352 int_const
= (int_const_or_overflow
10353 && !TREE_OVERFLOW (orig_op0
)
10354 && (op0
== truthvalue_true_node
10355 || !TREE_OVERFLOW (orig_op1
)));
10359 /* Shift operations: result has same type as first operand;
10360 always convert second operand to int.
10361 Also set SHORT_SHIFT if shifting rightward. */
10364 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
10365 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
10367 result_type
= type0
;
10370 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10371 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10372 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
10373 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
10375 result_type
= type0
;
10378 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
10379 && code1
== INTEGER_TYPE
)
10381 doing_shift
= true;
10382 if (TREE_CODE (op1
) == INTEGER_CST
)
10384 if (tree_int_cst_sgn (op1
) < 0)
10387 if (c_inhibit_evaluation_warnings
== 0)
10388 warning (0, "right shift count is negative");
10392 if (!integer_zerop (op1
))
10395 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
10398 if (c_inhibit_evaluation_warnings
== 0)
10399 warning (0, "right shift count >= width of type");
10404 /* Use the type of the value to be shifted. */
10405 result_type
= type0
;
10406 /* Convert the non vector shift-count to an integer, regardless
10407 of size of value being shifted. */
10408 if (TREE_CODE (TREE_TYPE (op1
)) != VECTOR_TYPE
10409 && TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
10410 op1
= convert (integer_type_node
, op1
);
10411 /* Avoid converting op1 to result_type later. */
10417 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
10418 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
10420 result_type
= type0
;
10423 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10424 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10425 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
10426 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
10428 result_type
= type0
;
10431 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
10432 && code1
== INTEGER_TYPE
)
10434 doing_shift
= true;
10435 if (TREE_CODE (op1
) == INTEGER_CST
)
10437 if (tree_int_cst_sgn (op1
) < 0)
10440 if (c_inhibit_evaluation_warnings
== 0)
10441 warning (0, "left shift count is negative");
10444 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
10447 if (c_inhibit_evaluation_warnings
== 0)
10448 warning (0, "left shift count >= width of type");
10452 /* Use the type of the value to be shifted. */
10453 result_type
= type0
;
10454 /* Convert the non vector shift-count to an integer, regardless
10455 of size of value being shifted. */
10456 if (TREE_CODE (TREE_TYPE (op1
)) != VECTOR_TYPE
10457 && TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
10458 op1
= convert (integer_type_node
, op1
);
10459 /* Avoid converting op1 to result_type later. */
10466 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
10469 if (!vector_types_compatible_elements_p (type0
, type1
))
10471 error_at (location
, "comparing vectors with different "
10473 return error_mark_node
;
10476 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
10478 error_at (location
, "comparing vectors with different "
10479 "number of elements");
10480 return error_mark_node
;
10483 /* Always construct signed integer vector type. */
10484 intt
= c_common_type_for_size (GET_MODE_BITSIZE
10485 (TYPE_MODE (TREE_TYPE (type0
))), 0);
10486 result_type
= build_opaque_vector_type (intt
,
10487 TYPE_VECTOR_SUBPARTS (type0
));
10491 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
10492 warning_at (location
,
10494 "comparing floating point with == or != is unsafe");
10495 /* Result of comparison is always int,
10496 but don't convert the args to int! */
10497 build_type
= integer_type_node
;
10498 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10499 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
10500 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10501 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
10503 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
10505 if (TREE_CODE (op0
) == ADDR_EXPR
10506 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0)))
10508 if (code
== EQ_EXPR
)
10509 warning_at (location
,
10511 "the comparison will always evaluate as %<false%> "
10512 "for the address of %qD will never be NULL",
10513 TREE_OPERAND (op0
, 0));
10515 warning_at (location
,
10517 "the comparison will always evaluate as %<true%> "
10518 "for the address of %qD will never be NULL",
10519 TREE_OPERAND (op0
, 0));
10521 result_type
= type0
;
10523 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
10525 if (TREE_CODE (op1
) == ADDR_EXPR
10526 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0)))
10528 if (code
== EQ_EXPR
)
10529 warning_at (location
,
10531 "the comparison will always evaluate as %<false%> "
10532 "for the address of %qD will never be NULL",
10533 TREE_OPERAND (op1
, 0));
10535 warning_at (location
,
10537 "the comparison will always evaluate as %<true%> "
10538 "for the address of %qD will never be NULL",
10539 TREE_OPERAND (op1
, 0));
10541 result_type
= type1
;
10543 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
10545 tree tt0
= TREE_TYPE (type0
);
10546 tree tt1
= TREE_TYPE (type1
);
10547 addr_space_t as0
= TYPE_ADDR_SPACE (tt0
);
10548 addr_space_t as1
= TYPE_ADDR_SPACE (tt1
);
10549 addr_space_t as_common
= ADDR_SPACE_GENERIC
;
10551 /* Anything compares with void *. void * compares with anything.
10552 Otherwise, the targets must be compatible
10553 and both must be object or both incomplete. */
10554 if (comp_target_types (location
, type0
, type1
))
10555 result_type
= common_pointer_type (type0
, type1
);
10556 else if (!addr_space_superset (as0
, as1
, &as_common
))
10558 error_at (location
, "comparison of pointers to "
10559 "disjoint address spaces");
10560 return error_mark_node
;
10562 else if (VOID_TYPE_P (tt0
) && !TYPE_ATOMIC (tt0
))
10564 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
)
10565 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10566 "comparison of %<void *%> with function pointer");
10568 else if (VOID_TYPE_P (tt1
) && !TYPE_ATOMIC (tt1
))
10570 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
)
10571 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10572 "comparison of %<void *%> with function pointer");
10575 /* Avoid warning about the volatile ObjC EH puts on decls. */
10577 pedwarn (location
, 0,
10578 "comparison of distinct pointer types lacks a cast");
10580 if (result_type
== NULL_TREE
)
10582 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
10583 result_type
= build_pointer_type
10584 (build_qualified_type (void_type_node
, qual
));
10587 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10589 result_type
= type0
;
10590 pedwarn (location
, 0, "comparison between pointer and integer");
10592 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
10594 result_type
= type1
;
10595 pedwarn (location
, 0, "comparison between pointer and integer");
10603 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
10606 if (!vector_types_compatible_elements_p (type0
, type1
))
10608 error_at (location
, "comparing vectors with different "
10610 return error_mark_node
;
10613 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
10615 error_at (location
, "comparing vectors with different "
10616 "number of elements");
10617 return error_mark_node
;
10620 /* Always construct signed integer vector type. */
10621 intt
= c_common_type_for_size (GET_MODE_BITSIZE
10622 (TYPE_MODE (TREE_TYPE (type0
))), 0);
10623 result_type
= build_opaque_vector_type (intt
,
10624 TYPE_VECTOR_SUBPARTS (type0
));
10628 build_type
= integer_type_node
;
10629 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10630 || code0
== FIXED_POINT_TYPE
)
10631 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10632 || code1
== FIXED_POINT_TYPE
))
10634 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
10636 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (type0
));
10637 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
10638 addr_space_t as_common
;
10640 if (comp_target_types (location
, type0
, type1
))
10642 result_type
= common_pointer_type (type0
, type1
);
10643 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
10644 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
10645 pedwarn (location
, 0,
10646 "comparison of complete and incomplete pointers");
10647 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
10648 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10649 "ordered comparisons of pointers to functions");
10650 else if (null_pointer_constant_p (orig_op0
)
10651 || null_pointer_constant_p (orig_op1
))
10652 warning_at (location
, OPT_Wextra
,
10653 "ordered comparison of pointer with null pointer");
10656 else if (!addr_space_superset (as0
, as1
, &as_common
))
10658 error_at (location
, "comparison of pointers to "
10659 "disjoint address spaces");
10660 return error_mark_node
;
10664 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
10665 result_type
= build_pointer_type
10666 (build_qualified_type (void_type_node
, qual
));
10667 pedwarn (location
, 0,
10668 "comparison of distinct pointer types lacks a cast");
10671 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
10673 result_type
= type0
;
10675 pedwarn (location
, OPT_Wpedantic
,
10676 "ordered comparison of pointer with integer zero");
10677 else if (extra_warnings
)
10678 warning_at (location
, OPT_Wextra
,
10679 "ordered comparison of pointer with integer zero");
10681 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
10683 result_type
= type1
;
10685 pedwarn (location
, OPT_Wpedantic
,
10686 "ordered comparison of pointer with integer zero");
10687 else if (extra_warnings
)
10688 warning_at (location
, OPT_Wextra
,
10689 "ordered comparison of pointer with integer zero");
10691 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10693 result_type
= type0
;
10694 pedwarn (location
, 0, "comparison between pointer and integer");
10696 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
10698 result_type
= type1
;
10699 pedwarn (location
, 0, "comparison between pointer and integer");
10704 gcc_unreachable ();
10707 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10708 return error_mark_node
;
10710 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10711 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
10712 || !vector_types_compatible_elements_p (type0
, type1
)))
10714 binary_op_error (location
, code
, type0
, type1
);
10715 return error_mark_node
;
10718 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
10719 || code0
== FIXED_POINT_TYPE
|| code0
== VECTOR_TYPE
)
10721 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
10722 || code1
== FIXED_POINT_TYPE
|| code1
== VECTOR_TYPE
))
10724 bool first_complex
= (code0
== COMPLEX_TYPE
);
10725 bool second_complex
= (code1
== COMPLEX_TYPE
);
10726 int none_complex
= (!first_complex
&& !second_complex
);
10728 if (shorten
|| common
|| short_compare
)
10730 result_type
= c_common_type (type0
, type1
);
10731 do_warn_double_promotion (result_type
, type0
, type1
,
10732 "implicit conversion from %qT to %qT "
10733 "to match other operand of binary "
10736 if (result_type
== error_mark_node
)
10737 return error_mark_node
;
10740 if (first_complex
!= second_complex
10741 && (code
== PLUS_EXPR
10742 || code
== MINUS_EXPR
10743 || code
== MULT_EXPR
10744 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
10745 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
10746 && flag_signed_zeros
)
10748 /* An operation on mixed real/complex operands must be
10749 handled specially, but the language-independent code can
10750 more easily optimize the plain complex arithmetic if
10751 -fno-signed-zeros. */
10752 tree real_type
= TREE_TYPE (result_type
);
10754 if (type0
!= orig_type0
|| type1
!= orig_type1
)
10756 gcc_assert (may_need_excess_precision
&& common
);
10757 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
10761 if (TREE_TYPE (op0
) != result_type
)
10762 op0
= convert_and_check (location
, result_type
, op0
);
10763 if (TREE_TYPE (op1
) != real_type
)
10764 op1
= convert_and_check (location
, real_type
, op1
);
10768 if (TREE_TYPE (op0
) != real_type
)
10769 op0
= convert_and_check (location
, real_type
, op0
);
10770 if (TREE_TYPE (op1
) != result_type
)
10771 op1
= convert_and_check (location
, result_type
, op1
);
10773 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
10774 return error_mark_node
;
10777 op0
= c_save_expr (op0
);
10778 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
10780 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
10785 case TRUNC_DIV_EXPR
:
10786 op1
= c_save_expr (op1
);
10787 imag
= build2 (resultcode
, real_type
, imag
, op1
);
10788 /* Fall through. */
10791 real
= build2 (resultcode
, real_type
, real
, op1
);
10799 op1
= c_save_expr (op1
);
10800 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
10802 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
10807 op0
= c_save_expr (op0
);
10808 imag
= build2 (resultcode
, real_type
, op0
, imag
);
10809 /* Fall through. */
10811 real
= build2 (resultcode
, real_type
, op0
, real
);
10814 real
= build2 (resultcode
, real_type
, op0
, real
);
10815 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
10821 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
10822 goto return_build_binary_op
;
10825 /* For certain operations (which identify themselves by shorten != 0)
10826 if both args were extended from the same smaller type,
10827 do the arithmetic in that type and then extend.
10829 shorten !=0 and !=1 indicates a bitwise operation.
10830 For them, this optimization is safe only if
10831 both args are zero-extended or both are sign-extended.
10832 Otherwise, we might change the result.
10833 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10834 but calculated in (unsigned short) it would be (unsigned short)-1. */
10836 if (shorten
&& none_complex
)
10838 final_type
= result_type
;
10839 result_type
= shorten_binary_op (result_type
, op0
, op1
,
10843 /* Shifts can be shortened if shifting right. */
10848 tree arg0
= get_narrower (op0
, &unsigned_arg
);
10850 final_type
= result_type
;
10852 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
10853 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
10855 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
10856 && tree_int_cst_sgn (op1
) > 0
10857 /* We can shorten only if the shift count is less than the
10858 number of bits in the smaller type size. */
10859 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
10860 /* We cannot drop an unsigned shift after sign-extension. */
10861 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
10863 /* Do an unsigned shift if the operand was zero-extended. */
10865 = c_common_signed_or_unsigned_type (unsigned_arg
,
10867 /* Convert value-to-be-shifted to that type. */
10868 if (TREE_TYPE (op0
) != result_type
)
10869 op0
= convert (result_type
, op0
);
10874 /* Comparison operations are shortened too but differently.
10875 They identify themselves by setting short_compare = 1. */
10879 /* Don't write &op0, etc., because that would prevent op0
10880 from being kept in a register.
10881 Instead, make copies of the our local variables and
10882 pass the copies by reference, then copy them back afterward. */
10883 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
10884 enum tree_code xresultcode
= resultcode
;
10886 = shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
10892 goto return_build_binary_op
;
10895 op0
= xop0
, op1
= xop1
;
10897 resultcode
= xresultcode
;
10899 if (c_inhibit_evaluation_warnings
== 0)
10901 bool op0_maybe_const
= true;
10902 bool op1_maybe_const
= true;
10903 tree orig_op0_folded
, orig_op1_folded
;
10905 if (in_late_binary_op
)
10907 orig_op0_folded
= orig_op0
;
10908 orig_op1_folded
= orig_op1
;
10912 /* Fold for the sake of possible warnings, as in
10913 build_conditional_expr. This requires the
10914 "original" values to be folded, not just op0 and
10916 c_inhibit_evaluation_warnings
++;
10917 op0
= c_fully_fold (op0
, require_constant_value
,
10919 op1
= c_fully_fold (op1
, require_constant_value
,
10921 c_inhibit_evaluation_warnings
--;
10922 orig_op0_folded
= c_fully_fold (orig_op0
,
10923 require_constant_value
,
10925 orig_op1_folded
= c_fully_fold (orig_op1
,
10926 require_constant_value
,
10930 if (warn_sign_compare
)
10931 warn_for_sign_compare (location
, orig_op0_folded
,
10932 orig_op1_folded
, op0
, op1
,
10933 result_type
, resultcode
);
10934 if (!in_late_binary_op
&& !int_operands
)
10936 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
10937 op0
= c_wrap_maybe_const (op0
, !op0_maybe_const
);
10938 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
10939 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
10945 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10946 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10947 Then the expression will be built.
10948 It will be given type FINAL_TYPE if that is nonzero;
10949 otherwise, it will be given type RESULT_TYPE. */
10953 binary_op_error (location
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
10954 return error_mark_node
;
10957 if (build_type
== NULL_TREE
)
10959 build_type
= result_type
;
10960 if ((type0
!= orig_type0
|| type1
!= orig_type1
)
10963 gcc_assert (may_need_excess_precision
&& common
);
10964 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
10970 op0
= ep_convert_and_check (location
, result_type
, op0
,
10971 semantic_result_type
);
10972 op1
= ep_convert_and_check (location
, result_type
, op1
,
10973 semantic_result_type
);
10975 /* This can happen if one operand has a vector type, and the other
10976 has a different type. */
10977 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
10978 return error_mark_node
;
10981 if ((flag_sanitize
& (SANITIZE_SHIFT
| SANITIZE_DIVIDE
))
10982 && current_function_decl
!= 0
10983 && !lookup_attribute ("no_sanitize_undefined",
10984 DECL_ATTRIBUTES (current_function_decl
))
10985 && (doing_div_or_mod
|| doing_shift
))
10987 /* OP0 and/or OP1 might have side-effects. */
10988 op0
= c_save_expr (op0
);
10989 op1
= c_save_expr (op1
);
10990 op0
= c_fully_fold (op0
, false, NULL
);
10991 op1
= c_fully_fold (op1
, false, NULL
);
10992 if (doing_div_or_mod
&& (flag_sanitize
& SANITIZE_DIVIDE
))
10993 instrument_expr
= ubsan_instrument_division (location
, op0
, op1
);
10994 else if (doing_shift
&& (flag_sanitize
& SANITIZE_SHIFT
))
10995 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
10998 /* Treat expressions in initializers specially as they can't trap. */
10999 if (int_const_or_overflow
)
11000 ret
= (require_constant_value
11001 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
11003 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
11005 ret
= build2 (resultcode
, build_type
, op0
, op1
);
11006 if (final_type
!= 0)
11007 ret
= convert (final_type
, ret
);
11009 return_build_binary_op
:
11010 gcc_assert (ret
!= error_mark_node
);
11011 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
11012 ret
= (int_operands
11013 ? note_integer_operands (ret
)
11014 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
11015 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
11016 && !in_late_binary_op
)
11017 ret
= note_integer_operands (ret
);
11018 if (semantic_result_type
)
11019 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
11020 protected_set_expr_location (ret
, location
);
11022 if (instrument_expr
!= NULL
)
11023 ret
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (ret
),
11024 instrument_expr
, ret
);
11030 /* Convert EXPR to be a truth-value, validating its type for this
11031 purpose. LOCATION is the source location for the expression. */
11034 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
11036 bool int_const
, int_operands
;
11038 switch (TREE_CODE (TREE_TYPE (expr
)))
11041 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
11042 return error_mark_node
;
11045 error_at (location
, "used struct type value where scalar is required");
11046 return error_mark_node
;
11049 error_at (location
, "used union type value where scalar is required");
11050 return error_mark_node
;
11053 error_at (location
, "void value not ignored as it ought to be");
11054 return error_mark_node
;
11056 case FUNCTION_TYPE
:
11057 gcc_unreachable ();
11060 error_at (location
, "used vector type where scalar is required");
11061 return error_mark_node
;
11067 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
11068 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
11069 if (int_operands
&& TREE_CODE (expr
) != INTEGER_CST
)
11071 expr
= remove_c_maybe_const_expr (expr
);
11072 expr
= build2 (NE_EXPR
, integer_type_node
, expr
,
11073 convert (TREE_TYPE (expr
), integer_zero_node
));
11074 expr
= note_integer_operands (expr
);
11077 /* ??? Should we also give an error for vectors rather than leaving
11078 those to give errors later? */
11079 expr
= c_common_truthvalue_conversion (location
, expr
);
11081 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
11083 if (TREE_OVERFLOW (expr
))
11086 return note_integer_operands (expr
);
11088 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
11089 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
11094 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11098 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
11100 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
11102 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
11103 /* Executing a compound literal inside a function reinitializes
11105 if (!TREE_STATIC (decl
))
11113 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11116 c_begin_omp_parallel (void)
11120 keep_next_level ();
11121 block
= c_begin_compound_stmt (true);
11126 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11127 statement. LOC is the location of the OMP_PARALLEL. */
11130 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
11134 block
= c_end_compound_stmt (loc
, block
, true);
11136 stmt
= make_node (OMP_PARALLEL
);
11137 TREE_TYPE (stmt
) = void_type_node
;
11138 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
11139 OMP_PARALLEL_BODY (stmt
) = block
;
11140 SET_EXPR_LOCATION (stmt
, loc
);
11142 return add_stmt (stmt
);
11145 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11148 c_begin_omp_task (void)
11152 keep_next_level ();
11153 block
= c_begin_compound_stmt (true);
11158 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11159 statement. LOC is the location of the #pragma. */
11162 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
11166 block
= c_end_compound_stmt (loc
, block
, true);
11168 stmt
= make_node (OMP_TASK
);
11169 TREE_TYPE (stmt
) = void_type_node
;
11170 OMP_TASK_CLAUSES (stmt
) = clauses
;
11171 OMP_TASK_BODY (stmt
) = block
;
11172 SET_EXPR_LOCATION (stmt
, loc
);
11174 return add_stmt (stmt
);
11177 /* Generate GOMP_cancel call for #pragma omp cancel. */
11180 c_finish_omp_cancel (location_t loc
, tree clauses
)
11182 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCEL
);
11184 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
11186 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
11188 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
11190 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
11194 error_at (loc
, "%<#pragma omp cancel must specify one of "
11195 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11199 tree ifc
= find_omp_clause (clauses
, OMP_CLAUSE_IF
);
11200 if (ifc
!= NULL_TREE
)
11202 tree type
= TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc
));
11203 ifc
= fold_build2_loc (OMP_CLAUSE_LOCATION (ifc
), NE_EXPR
,
11204 boolean_type_node
, OMP_CLAUSE_IF_EXPR (ifc
),
11205 build_zero_cst (type
));
11208 ifc
= boolean_true_node
;
11209 tree stmt
= build_call_expr_loc (loc
, fn
, 2,
11210 build_int_cst (integer_type_node
, mask
),
11215 /* Generate GOMP_cancellation_point call for
11216 #pragma omp cancellation point. */
11219 c_finish_omp_cancellation_point (location_t loc
, tree clauses
)
11221 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT
);
11223 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
11225 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
11227 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
11229 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
11233 error_at (loc
, "%<#pragma omp cancellation point must specify one of "
11234 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11238 tree stmt
= build_call_expr_loc (loc
, fn
, 1,
11239 build_int_cst (integer_type_node
, mask
));
11243 /* Helper function for handle_omp_array_sections. Called recursively
11244 to handle multiple array-section-subscripts. C is the clause,
11245 T current expression (initially OMP_CLAUSE_DECL), which is either
11246 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11247 expression if specified, TREE_VALUE length expression if specified,
11248 TREE_CHAIN is what it has been specified after, or some decl.
11249 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11250 set to true if any of the array-section-subscript could have length
11251 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11252 first array-section-subscript which is known not to have length
11254 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11255 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11256 all are or may have length of 1, array-section-subscript [:2] is the
11257 first one knonwn not to have length 1. For array-section-subscript
11258 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11259 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11260 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11261 case though, as some lengths could be zero. */
11264 handle_omp_array_sections_1 (tree c
, tree t
, vec
<tree
> &types
,
11265 bool &maybe_zero_len
, unsigned int &first_non_one
)
11267 tree ret
, low_bound
, length
, type
;
11268 if (TREE_CODE (t
) != TREE_LIST
)
11270 if (t
== error_mark_node
|| TREE_TYPE (t
) == error_mark_node
)
11271 return error_mark_node
;
11272 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11275 error_at (OMP_CLAUSE_LOCATION (c
),
11276 "%qD is not a variable in %qs clause", t
,
11277 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11279 error_at (OMP_CLAUSE_LOCATION (c
),
11280 "%qE is not a variable in %qs clause", t
,
11281 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11282 return error_mark_node
;
11284 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
11285 && TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
11287 error_at (OMP_CLAUSE_LOCATION (c
),
11288 "%qD is threadprivate variable in %qs clause", t
,
11289 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11290 return error_mark_node
;
11295 ret
= handle_omp_array_sections_1 (c
, TREE_CHAIN (t
), types
,
11296 maybe_zero_len
, first_non_one
);
11297 if (ret
== error_mark_node
|| ret
== NULL_TREE
)
11300 type
= TREE_TYPE (ret
);
11301 low_bound
= TREE_PURPOSE (t
);
11302 length
= TREE_VALUE (t
);
11304 if (low_bound
== error_mark_node
|| length
== error_mark_node
)
11305 return error_mark_node
;
11307 if (low_bound
&& !INTEGRAL_TYPE_P (TREE_TYPE (low_bound
)))
11309 error_at (OMP_CLAUSE_LOCATION (c
),
11310 "low bound %qE of array section does not have integral type",
11312 return error_mark_node
;
11314 if (length
&& !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
11316 error_at (OMP_CLAUSE_LOCATION (c
),
11317 "length %qE of array section does not have integral type",
11319 return error_mark_node
;
11322 && TREE_CODE (low_bound
) == INTEGER_CST
11323 && TYPE_PRECISION (TREE_TYPE (low_bound
))
11324 > TYPE_PRECISION (sizetype
))
11325 low_bound
= fold_convert (sizetype
, low_bound
);
11327 && TREE_CODE (length
) == INTEGER_CST
11328 && TYPE_PRECISION (TREE_TYPE (length
))
11329 > TYPE_PRECISION (sizetype
))
11330 length
= fold_convert (sizetype
, length
);
11331 if (low_bound
== NULL_TREE
)
11332 low_bound
= integer_zero_node
;
11334 if (length
!= NULL_TREE
)
11336 if (!integer_nonzerop (length
))
11337 maybe_zero_len
= true;
11338 if (first_non_one
== types
.length ()
11339 && (TREE_CODE (length
) != INTEGER_CST
|| integer_onep (length
)))
11342 if (TREE_CODE (type
) == ARRAY_TYPE
)
11344 if (length
== NULL_TREE
11345 && (TYPE_DOMAIN (type
) == NULL_TREE
11346 || TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
))
11348 error_at (OMP_CLAUSE_LOCATION (c
),
11349 "for unknown bound array type length expression must "
11351 return error_mark_node
;
11353 if (TREE_CODE (low_bound
) == INTEGER_CST
11354 && tree_int_cst_sgn (low_bound
) == -1)
11356 error_at (OMP_CLAUSE_LOCATION (c
),
11357 "negative low bound in array section in %qs clause",
11358 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11359 return error_mark_node
;
11361 if (length
!= NULL_TREE
11362 && TREE_CODE (length
) == INTEGER_CST
11363 && tree_int_cst_sgn (length
) == -1)
11365 error_at (OMP_CLAUSE_LOCATION (c
),
11366 "negative length in array section in %qs clause",
11367 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11368 return error_mark_node
;
11370 if (TYPE_DOMAIN (type
)
11371 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
11372 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
11375 tree size
= size_binop (PLUS_EXPR
,
11376 TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
11378 if (TREE_CODE (low_bound
) == INTEGER_CST
)
11380 if (tree_int_cst_lt (size
, low_bound
))
11382 error_at (OMP_CLAUSE_LOCATION (c
),
11383 "low bound %qE above array section size "
11384 "in %qs clause", low_bound
,
11385 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11386 return error_mark_node
;
11388 if (tree_int_cst_equal (size
, low_bound
))
11389 maybe_zero_len
= true;
11390 else if (length
== NULL_TREE
11391 && first_non_one
== types
.length ()
11392 && tree_int_cst_equal
11393 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
11397 else if (length
== NULL_TREE
)
11399 maybe_zero_len
= true;
11400 if (first_non_one
== types
.length ())
11403 if (length
&& TREE_CODE (length
) == INTEGER_CST
)
11405 if (tree_int_cst_lt (size
, length
))
11407 error_at (OMP_CLAUSE_LOCATION (c
),
11408 "length %qE above array section size "
11409 "in %qs clause", length
,
11410 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11411 return error_mark_node
;
11413 if (TREE_CODE (low_bound
) == INTEGER_CST
)
11416 = size_binop (PLUS_EXPR
,
11417 fold_convert (sizetype
, low_bound
),
11418 fold_convert (sizetype
, length
));
11419 if (TREE_CODE (lbpluslen
) == INTEGER_CST
11420 && tree_int_cst_lt (size
, lbpluslen
))
11422 error_at (OMP_CLAUSE_LOCATION (c
),
11423 "high bound %qE above array section size "
11424 "in %qs clause", lbpluslen
,
11425 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11426 return error_mark_node
;
11431 else if (length
== NULL_TREE
)
11433 maybe_zero_len
= true;
11434 if (first_non_one
== types
.length ())
11438 /* For [lb:] we will need to evaluate lb more than once. */
11439 if (length
== NULL_TREE
&& OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
11441 tree lb
= c_save_expr (low_bound
);
11442 if (lb
!= low_bound
)
11444 TREE_PURPOSE (t
) = lb
;
11449 else if (TREE_CODE (type
) == POINTER_TYPE
)
11451 if (length
== NULL_TREE
)
11453 error_at (OMP_CLAUSE_LOCATION (c
),
11454 "for pointer type length expression must be specified");
11455 return error_mark_node
;
11457 /* If there is a pointer type anywhere but in the very first
11458 array-section-subscript, the array section can't be contiguous. */
11459 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
11460 && TREE_CODE (TREE_CHAIN (t
)) == TREE_LIST
)
11462 error_at (OMP_CLAUSE_LOCATION (c
),
11463 "array section is not contiguous in %qs clause",
11464 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11465 return error_mark_node
;
11470 error_at (OMP_CLAUSE_LOCATION (c
),
11471 "%qE does not have pointer or array type", ret
);
11472 return error_mark_node
;
11474 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
11475 types
.safe_push (TREE_TYPE (ret
));
11476 /* We will need to evaluate lb more than once. */
11477 tree lb
= c_save_expr (low_bound
);
11478 if (lb
!= low_bound
)
11480 TREE_PURPOSE (t
) = lb
;
11483 ret
= build_array_ref (OMP_CLAUSE_LOCATION (c
), ret
, low_bound
);
11487 /* Handle array sections for clause C. */
11490 handle_omp_array_sections (tree c
)
11492 bool maybe_zero_len
= false;
11493 unsigned int first_non_one
= 0;
11494 vec
<tree
> types
= vNULL
;
11495 tree first
= handle_omp_array_sections_1 (c
, OMP_CLAUSE_DECL (c
), types
,
11496 maybe_zero_len
, first_non_one
);
11497 if (first
== error_mark_node
)
11502 if (first
== NULL_TREE
)
11507 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
)
11509 tree t
= OMP_CLAUSE_DECL (c
);
11510 tree tem
= NULL_TREE
;
11512 /* Need to evaluate side effects in the length expressions
11514 while (TREE_CODE (t
) == TREE_LIST
)
11516 if (TREE_VALUE (t
) && TREE_SIDE_EFFECTS (TREE_VALUE (t
)))
11518 if (tem
== NULL_TREE
)
11519 tem
= TREE_VALUE (t
);
11521 tem
= build2 (COMPOUND_EXPR
, TREE_TYPE (tem
),
11522 TREE_VALUE (t
), tem
);
11524 t
= TREE_CHAIN (t
);
11527 first
= build2 (COMPOUND_EXPR
, TREE_TYPE (first
), tem
, first
);
11528 first
= c_fully_fold (first
, false, NULL
);
11529 OMP_CLAUSE_DECL (c
) = first
;
11533 unsigned int num
= types
.length (), i
;
11534 tree t
, side_effects
= NULL_TREE
, size
= NULL_TREE
;
11535 tree condition
= NULL_TREE
;
11537 if (int_size_in_bytes (TREE_TYPE (first
)) <= 0)
11538 maybe_zero_len
= true;
11540 for (i
= num
, t
= OMP_CLAUSE_DECL (c
); i
> 0;
11541 t
= TREE_CHAIN (t
))
11543 tree low_bound
= TREE_PURPOSE (t
);
11544 tree length
= TREE_VALUE (t
);
11548 && TREE_CODE (low_bound
) == INTEGER_CST
11549 && TYPE_PRECISION (TREE_TYPE (low_bound
))
11550 > TYPE_PRECISION (sizetype
))
11551 low_bound
= fold_convert (sizetype
, low_bound
);
11553 && TREE_CODE (length
) == INTEGER_CST
11554 && TYPE_PRECISION (TREE_TYPE (length
))
11555 > TYPE_PRECISION (sizetype
))
11556 length
= fold_convert (sizetype
, length
);
11557 if (low_bound
== NULL_TREE
)
11558 low_bound
= integer_zero_node
;
11559 if (!maybe_zero_len
&& i
> first_non_one
)
11561 if (integer_nonzerop (low_bound
))
11562 goto do_warn_noncontiguous
;
11563 if (length
!= NULL_TREE
11564 && TREE_CODE (length
) == INTEGER_CST
11565 && TYPE_DOMAIN (types
[i
])
11566 && TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
]))
11567 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])))
11571 size
= size_binop (PLUS_EXPR
,
11572 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
11574 if (!tree_int_cst_equal (length
, size
))
11576 do_warn_noncontiguous
:
11577 error_at (OMP_CLAUSE_LOCATION (c
),
11578 "array section is not contiguous in %qs "
11580 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11585 if (length
!= NULL_TREE
11586 && TREE_SIDE_EFFECTS (length
))
11588 if (side_effects
== NULL_TREE
)
11589 side_effects
= length
;
11591 side_effects
= build2 (COMPOUND_EXPR
,
11592 TREE_TYPE (side_effects
),
11593 length
, side_effects
);
11600 if (i
> first_non_one
&& length
&& integer_nonzerop (length
))
11603 l
= fold_convert (sizetype
, length
);
11606 l
= size_binop (PLUS_EXPR
,
11607 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
11609 l
= size_binop (MINUS_EXPR
, l
,
11610 fold_convert (sizetype
, low_bound
));
11612 if (i
> first_non_one
)
11614 l
= fold_build2 (NE_EXPR
, boolean_type_node
, l
,
11616 if (condition
== NULL_TREE
)
11619 condition
= fold_build2 (BIT_AND_EXPR
, boolean_type_node
,
11622 else if (size
== NULL_TREE
)
11624 size
= size_in_bytes (TREE_TYPE (types
[i
]));
11625 size
= size_binop (MULT_EXPR
, size
, l
);
11627 size
= fold_build3 (COND_EXPR
, sizetype
, condition
,
11628 size
, size_zero_node
);
11631 size
= size_binop (MULT_EXPR
, size
, l
);
11636 size
= build2 (COMPOUND_EXPR
, sizetype
, side_effects
, size
);
11637 first
= c_fully_fold (first
, false, NULL
);
11638 OMP_CLAUSE_DECL (c
) = first
;
11640 size
= c_fully_fold (size
, false, NULL
);
11641 OMP_CLAUSE_SIZE (c
) = size
;
11642 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
11644 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_MAP
);
11645 OMP_CLAUSE_MAP_KIND (c2
) = OMP_CLAUSE_MAP_POINTER
;
11646 if (!c_mark_addressable (t
))
11648 OMP_CLAUSE_DECL (c2
) = t
;
11649 t
= build_fold_addr_expr (first
);
11650 t
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), ptrdiff_type_node
, t
);
11651 tree ptr
= OMP_CLAUSE_DECL (c2
);
11652 if (!POINTER_TYPE_P (TREE_TYPE (ptr
)))
11653 ptr
= build_fold_addr_expr (ptr
);
11654 t
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
11655 ptrdiff_type_node
, t
,
11656 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
11657 ptrdiff_type_node
, ptr
));
11658 t
= c_fully_fold (t
, false, NULL
);
11659 OMP_CLAUSE_SIZE (c2
) = t
;
11660 OMP_CLAUSE_CHAIN (c2
) = OMP_CLAUSE_CHAIN (c
);
11661 OMP_CLAUSE_CHAIN (c
) = c2
;
11666 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11667 an inline call. But, remap
11668 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11669 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11672 c_clone_omp_udr (tree stmt
, tree omp_decl1
, tree omp_decl2
,
11673 tree decl
, tree placeholder
)
11676 struct pointer_map_t
*decl_map
= pointer_map_create ();
11678 *pointer_map_insert (decl_map
, omp_decl1
) = placeholder
;
11679 *pointer_map_insert (decl_map
, omp_decl2
) = decl
;
11680 memset (&id
, 0, sizeof (id
));
11681 id
.src_fn
= DECL_CONTEXT (omp_decl1
);
11682 id
.dst_fn
= current_function_decl
;
11683 id
.src_cfun
= DECL_STRUCT_FUNCTION (id
.src_fn
);
11684 id
.decl_map
= decl_map
;
11686 id
.copy_decl
= copy_decl_no_change
;
11687 id
.transform_call_graph_edges
= CB_CGE_DUPLICATE
;
11688 id
.transform_new_cfg
= true;
11689 id
.transform_return_to_modify
= false;
11690 id
.transform_lang_insert_block
= NULL
;
11692 walk_tree (&stmt
, copy_tree_body_r
, &id
, NULL
);
11693 pointer_map_destroy (decl_map
);
11697 /* Helper function of c_finish_omp_clauses, called via walk_tree.
11698 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11701 c_find_omp_placeholder_r (tree
*tp
, int *, void *data
)
11703 if (*tp
== (tree
) data
)
11708 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
11709 Remove any elements from the list that are invalid. */
11712 c_finish_omp_clauses (tree clauses
)
11714 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
11715 bitmap_head aligned_head
;
11716 tree c
, t
, *pc
= &clauses
;
11717 bool branch_seen
= false;
11718 bool copyprivate_seen
= false;
11719 tree
*nowait_clause
= NULL
;
11721 bitmap_obstack_initialize (NULL
);
11722 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
11723 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
11724 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
11725 bitmap_initialize (&aligned_head
, &bitmap_default_obstack
);
11727 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
11729 bool remove
= false;
11730 bool need_complete
= false;
11731 bool need_implicitly_determined
= false;
11733 switch (OMP_CLAUSE_CODE (c
))
11735 case OMP_CLAUSE_SHARED
:
11736 need_implicitly_determined
= true;
11737 goto check_dup_generic
;
11739 case OMP_CLAUSE_PRIVATE
:
11740 need_complete
= true;
11741 need_implicitly_determined
= true;
11742 goto check_dup_generic
;
11744 case OMP_CLAUSE_REDUCTION
:
11745 need_implicitly_determined
= true;
11746 t
= OMP_CLAUSE_DECL (c
);
11747 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == NULL_TREE
11748 && (FLOAT_TYPE_P (TREE_TYPE (t
))
11749 || TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
))
11751 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
11752 const char *r_name
= NULL
;
11761 if (TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
)
11765 if (TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
)
11777 case TRUTH_ANDIF_EXPR
:
11778 if (FLOAT_TYPE_P (TREE_TYPE (t
)))
11781 case TRUTH_ORIF_EXPR
:
11782 if (FLOAT_TYPE_P (TREE_TYPE (t
)))
11786 gcc_unreachable ();
11790 error_at (OMP_CLAUSE_LOCATION (c
),
11791 "%qE has invalid type for %<reduction(%s)%>",
11797 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == error_mark_node
)
11799 error_at (OMP_CLAUSE_LOCATION (c
),
11800 "user defined reduction not found for %qD", t
);
11804 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
11806 tree list
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
);
11807 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (t
));
11808 tree placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
11809 VAR_DECL
, NULL_TREE
, type
);
11810 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = placeholder
;
11811 DECL_ARTIFICIAL (placeholder
) = 1;
11812 DECL_IGNORED_P (placeholder
) = 1;
11813 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 0)))
11814 c_mark_addressable (placeholder
);
11815 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 1)))
11816 c_mark_addressable (OMP_CLAUSE_DECL (c
));
11817 OMP_CLAUSE_REDUCTION_MERGE (c
)
11818 = c_clone_omp_udr (TREE_VEC_ELT (list
, 2),
11819 TREE_VEC_ELT (list
, 0),
11820 TREE_VEC_ELT (list
, 1),
11821 OMP_CLAUSE_DECL (c
), placeholder
);
11822 OMP_CLAUSE_REDUCTION_MERGE (c
)
11823 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
11824 void_type_node
, NULL_TREE
,
11825 OMP_CLAUSE_REDUCTION_MERGE (c
), NULL_TREE
);
11826 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c
)) = 1;
11827 if (TREE_VEC_LENGTH (list
) == 6)
11829 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 3)))
11830 c_mark_addressable (OMP_CLAUSE_DECL (c
));
11831 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 4)))
11832 c_mark_addressable (placeholder
);
11833 tree init
= TREE_VEC_ELT (list
, 5);
11834 if (init
== error_mark_node
)
11835 init
= DECL_INITIAL (TREE_VEC_ELT (list
, 3));
11836 OMP_CLAUSE_REDUCTION_INIT (c
)
11837 = c_clone_omp_udr (init
, TREE_VEC_ELT (list
, 4),
11838 TREE_VEC_ELT (list
, 3),
11839 OMP_CLAUSE_DECL (c
), placeholder
);
11840 if (TREE_VEC_ELT (list
, 5) == error_mark_node
)
11841 OMP_CLAUSE_REDUCTION_INIT (c
)
11842 = build2 (INIT_EXPR
, TREE_TYPE (t
), t
,
11843 OMP_CLAUSE_REDUCTION_INIT (c
));
11844 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
11845 c_find_omp_placeholder_r
,
11846 placeholder
, NULL
))
11847 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c
) = 1;
11852 if (AGGREGATE_TYPE_P (TREE_TYPE (t
)))
11853 init
= build_constructor (TREE_TYPE (t
), NULL
);
11855 init
= fold_convert (TREE_TYPE (t
), integer_zero_node
);
11856 OMP_CLAUSE_REDUCTION_INIT (c
)
11857 = build2 (INIT_EXPR
, TREE_TYPE (t
), t
, init
);
11859 OMP_CLAUSE_REDUCTION_INIT (c
)
11860 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
11861 void_type_node
, NULL_TREE
,
11862 OMP_CLAUSE_REDUCTION_INIT (c
), NULL_TREE
);
11863 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c
)) = 1;
11865 goto check_dup_generic
;
11867 case OMP_CLAUSE_COPYPRIVATE
:
11868 copyprivate_seen
= true;
11871 error_at (OMP_CLAUSE_LOCATION (*nowait_clause
),
11872 "%<nowait%> clause must not be used together "
11873 "with %<copyprivate%>");
11874 *nowait_clause
= OMP_CLAUSE_CHAIN (*nowait_clause
);
11875 nowait_clause
= NULL
;
11877 goto check_dup_generic
;
11879 case OMP_CLAUSE_COPYIN
:
11880 t
= OMP_CLAUSE_DECL (c
);
11881 if (TREE_CODE (t
) != VAR_DECL
|| !DECL_THREAD_LOCAL_P (t
))
11883 error_at (OMP_CLAUSE_LOCATION (c
),
11884 "%qE must be %<threadprivate%> for %<copyin%>", t
);
11888 goto check_dup_generic
;
11890 case OMP_CLAUSE_LINEAR
:
11891 t
= OMP_CLAUSE_DECL (c
);
11892 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
11893 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
11895 error_at (OMP_CLAUSE_LOCATION (c
),
11896 "linear clause applied to non-integral non-pointer "
11897 "variable with type %qT", TREE_TYPE (t
));
11901 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c
))) == POINTER_TYPE
)
11903 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
11904 s
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
), PLUS_EXPR
,
11905 OMP_CLAUSE_DECL (c
), s
);
11906 s
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
11907 sizetype
, s
, OMP_CLAUSE_DECL (c
));
11908 if (s
== error_mark_node
)
11910 OMP_CLAUSE_LINEAR_STEP (c
) = s
;
11912 goto check_dup_generic
;
11915 t
= OMP_CLAUSE_DECL (c
);
11916 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11918 error_at (OMP_CLAUSE_LOCATION (c
),
11919 "%qE is not a variable in clause %qs", t
,
11920 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11923 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
11924 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
11925 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
11927 error_at (OMP_CLAUSE_LOCATION (c
),
11928 "%qE appears more than once in data clauses", t
);
11932 bitmap_set_bit (&generic_head
, DECL_UID (t
));
11935 case OMP_CLAUSE_FIRSTPRIVATE
:
11936 t
= OMP_CLAUSE_DECL (c
);
11937 need_complete
= true;
11938 need_implicitly_determined
= true;
11939 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11941 error_at (OMP_CLAUSE_LOCATION (c
),
11942 "%qE is not a variable in clause %<firstprivate%>", t
);
11945 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
11946 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
11948 error_at (OMP_CLAUSE_LOCATION (c
),
11949 "%qE appears more than once in data clauses", t
);
11953 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
11956 case OMP_CLAUSE_LASTPRIVATE
:
11957 t
= OMP_CLAUSE_DECL (c
);
11958 need_complete
= true;
11959 need_implicitly_determined
= true;
11960 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11962 error_at (OMP_CLAUSE_LOCATION (c
),
11963 "%qE is not a variable in clause %<lastprivate%>", t
);
11966 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
11967 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
11969 error_at (OMP_CLAUSE_LOCATION (c
),
11970 "%qE appears more than once in data clauses", t
);
11974 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
11977 case OMP_CLAUSE_ALIGNED
:
11978 t
= OMP_CLAUSE_DECL (c
);
11979 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11981 error_at (OMP_CLAUSE_LOCATION (c
),
11982 "%qE is not a variable in %<aligned%> clause", t
);
11985 else if (!POINTER_TYPE_P (TREE_TYPE (t
))
11986 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
11988 error_at (OMP_CLAUSE_LOCATION (c
),
11989 "%qE in %<aligned%> clause is neither a pointer nor "
11993 else if (bitmap_bit_p (&aligned_head
, DECL_UID (t
)))
11995 error_at (OMP_CLAUSE_LOCATION (c
),
11996 "%qE appears more than once in %<aligned%> clauses",
12001 bitmap_set_bit (&aligned_head
, DECL_UID (t
));
12004 case OMP_CLAUSE_DEPEND
:
12005 t
= OMP_CLAUSE_DECL (c
);
12006 if (TREE_CODE (t
) == TREE_LIST
)
12008 if (handle_omp_array_sections (c
))
12012 if (t
== error_mark_node
)
12014 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12016 error_at (OMP_CLAUSE_LOCATION (c
),
12017 "%qE is not a variable in %<depend%> clause", t
);
12020 else if (!c_mark_addressable (t
))
12024 case OMP_CLAUSE_MAP
:
12025 case OMP_CLAUSE_TO
:
12026 case OMP_CLAUSE_FROM
:
12027 t
= OMP_CLAUSE_DECL (c
);
12028 if (TREE_CODE (t
) == TREE_LIST
)
12030 if (handle_omp_array_sections (c
))
12034 t
= OMP_CLAUSE_DECL (c
);
12035 if (!COMPLETE_TYPE_P (TREE_TYPE (t
)))
12037 error_at (OMP_CLAUSE_LOCATION (c
),
12038 "array section does not have mappable type "
12040 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12046 if (t
== error_mark_node
)
12048 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12050 error_at (OMP_CLAUSE_LOCATION (c
),
12051 "%qE is not a variable in %qs clause", t
,
12052 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12055 else if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
12057 error_at (OMP_CLAUSE_LOCATION (c
),
12058 "%qD is threadprivate variable in %qs clause", t
,
12059 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12062 else if (!c_mark_addressable (t
))
12064 else if (!COMPLETE_TYPE_P (TREE_TYPE (t
))
12065 && !(OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
12066 && OMP_CLAUSE_MAP_KIND (c
) == OMP_CLAUSE_MAP_POINTER
))
12068 error_at (OMP_CLAUSE_LOCATION (c
),
12069 "%qD does not have a mappable type in %qs clause", t
,
12070 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12073 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
)))
12075 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
12076 error ("%qD appears more than once in motion clauses", t
);
12078 error ("%qD appears more than once in map clauses", t
);
12082 bitmap_set_bit (&generic_head
, DECL_UID (t
));
12085 case OMP_CLAUSE_UNIFORM
:
12086 t
= OMP_CLAUSE_DECL (c
);
12087 if (TREE_CODE (t
) != PARM_DECL
)
12090 error_at (OMP_CLAUSE_LOCATION (c
),
12091 "%qD is not an argument in %<uniform%> clause", t
);
12093 error_at (OMP_CLAUSE_LOCATION (c
),
12094 "%qE is not an argument in %<uniform%> clause", t
);
12098 goto check_dup_generic
;
12100 case OMP_CLAUSE_NOWAIT
:
12101 if (copyprivate_seen
)
12103 error_at (OMP_CLAUSE_LOCATION (c
),
12104 "%<nowait%> clause must not be used together "
12105 "with %<copyprivate%>");
12109 nowait_clause
= pc
;
12110 pc
= &OMP_CLAUSE_CHAIN (c
);
12113 case OMP_CLAUSE_IF
:
12114 case OMP_CLAUSE_NUM_THREADS
:
12115 case OMP_CLAUSE_NUM_TEAMS
:
12116 case OMP_CLAUSE_THREAD_LIMIT
:
12117 case OMP_CLAUSE_SCHEDULE
:
12118 case OMP_CLAUSE_ORDERED
:
12119 case OMP_CLAUSE_DEFAULT
:
12120 case OMP_CLAUSE_UNTIED
:
12121 case OMP_CLAUSE_COLLAPSE
:
12122 case OMP_CLAUSE_FINAL
:
12123 case OMP_CLAUSE_MERGEABLE
:
12124 case OMP_CLAUSE_SAFELEN
:
12125 case OMP_CLAUSE_SIMDLEN
:
12126 case OMP_CLAUSE_DEVICE
:
12127 case OMP_CLAUSE_DIST_SCHEDULE
:
12128 case OMP_CLAUSE_PARALLEL
:
12129 case OMP_CLAUSE_FOR
:
12130 case OMP_CLAUSE_SECTIONS
:
12131 case OMP_CLAUSE_TASKGROUP
:
12132 case OMP_CLAUSE_PROC_BIND
:
12133 pc
= &OMP_CLAUSE_CHAIN (c
);
12136 case OMP_CLAUSE_INBRANCH
:
12137 case OMP_CLAUSE_NOTINBRANCH
:
12140 error_at (OMP_CLAUSE_LOCATION (c
),
12141 "%<inbranch%> clause is incompatible with "
12142 "%<notinbranch%>");
12146 branch_seen
= true;
12147 pc
= &OMP_CLAUSE_CHAIN (c
);
12151 gcc_unreachable ();
12156 t
= OMP_CLAUSE_DECL (c
);
12160 t
= require_complete_type (t
);
12161 if (t
== error_mark_node
)
12165 if (need_implicitly_determined
)
12167 const char *share_name
= NULL
;
12169 if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
12170 share_name
= "threadprivate";
12171 else switch (c_omp_predetermined_sharing (t
))
12173 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
12175 case OMP_CLAUSE_DEFAULT_SHARED
:
12176 /* const vars may be specified in firstprivate clause. */
12177 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12178 && TREE_READONLY (t
))
12180 share_name
= "shared";
12182 case OMP_CLAUSE_DEFAULT_PRIVATE
:
12183 share_name
= "private";
12186 gcc_unreachable ();
12190 error_at (OMP_CLAUSE_LOCATION (c
),
12191 "%qE is predetermined %qs for %qs",
12193 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12200 *pc
= OMP_CLAUSE_CHAIN (c
);
12202 pc
= &OMP_CLAUSE_CHAIN (c
);
12205 bitmap_obstack_release (NULL
);
12209 /* Create a transaction node. */
12212 c_finish_transaction (location_t loc
, tree block
, int flags
)
12214 tree stmt
= build_stmt (loc
, TRANSACTION_EXPR
, block
);
12215 if (flags
& TM_STMT_ATTR_OUTER
)
12216 TRANSACTION_EXPR_OUTER (stmt
) = 1;
12217 if (flags
& TM_STMT_ATTR_RELAXED
)
12218 TRANSACTION_EXPR_RELAXED (stmt
) = 1;
12219 return add_stmt (stmt
);
12222 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12223 down to the element type of an array. */
12226 c_build_qualified_type (tree type
, int type_quals
)
12228 if (type
== error_mark_node
)
12231 if (TREE_CODE (type
) == ARRAY_TYPE
)
12234 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
12237 /* See if we already have an identically qualified type. */
12238 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
12240 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
12241 && TYPE_NAME (t
) == TYPE_NAME (type
)
12242 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
12243 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
12244 TYPE_ATTRIBUTES (type
)))
12249 tree domain
= TYPE_DOMAIN (type
);
12251 t
= build_variant_type_copy (type
);
12252 TREE_TYPE (t
) = element_type
;
12254 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
12255 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
12256 SET_TYPE_STRUCTURAL_EQUALITY (t
);
12257 else if (TYPE_CANONICAL (element_type
) != element_type
12258 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
12260 tree unqualified_canon
12261 = build_array_type (TYPE_CANONICAL (element_type
),
12262 domain
? TYPE_CANONICAL (domain
)
12265 = c_build_qualified_type (unqualified_canon
, type_quals
);
12268 TYPE_CANONICAL (t
) = t
;
12273 /* A restrict-qualified pointer type must be a pointer to object or
12274 incomplete type. Note that the use of POINTER_TYPE_P also allows
12275 REFERENCE_TYPEs, which is appropriate for C++. */
12276 if ((type_quals
& TYPE_QUAL_RESTRICT
)
12277 && (!POINTER_TYPE_P (type
)
12278 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
12280 error ("invalid use of %<restrict%>");
12281 type_quals
&= ~TYPE_QUAL_RESTRICT
;
12284 return build_qualified_type (type
, type_quals
);
12287 /* Build a VA_ARG_EXPR for the C parser. */
12290 c_build_va_arg (location_t loc
, tree expr
, tree type
)
12292 if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
12293 warning_at (loc
, OPT_Wc___compat
,
12294 "C++ requires promoted type, not enum type, in %<va_arg%>");
12295 return build_va_arg (loc
, expr
, type
);
12298 /* Return truthvalue of whether T1 is the same tree structure as T2.
12299 Return 1 if they are the same. Return 0 if they are different. */
12302 c_tree_equal (tree t1
, tree t2
)
12304 enum tree_code code1
, code2
;
12311 for (code1
= TREE_CODE (t1
);
12312 CONVERT_EXPR_CODE_P (code1
)
12313 || code1
== NON_LVALUE_EXPR
;
12314 code1
= TREE_CODE (t1
))
12315 t1
= TREE_OPERAND (t1
, 0);
12316 for (code2
= TREE_CODE (t2
);
12317 CONVERT_EXPR_CODE_P (code2
)
12318 || code2
== NON_LVALUE_EXPR
;
12319 code2
= TREE_CODE (t2
))
12320 t2
= TREE_OPERAND (t2
, 0);
12322 /* They might have become equal now. */
12326 if (code1
!= code2
)
12332 return TREE_INT_CST_LOW (t1
) == TREE_INT_CST_LOW (t2
)
12333 && TREE_INT_CST_HIGH (t1
) == TREE_INT_CST_HIGH (t2
);
12336 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1
), TREE_REAL_CST (t2
));
12339 return TREE_STRING_LENGTH (t1
) == TREE_STRING_LENGTH (t2
)
12340 && !memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
12341 TREE_STRING_LENGTH (t1
));
12344 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1
),
12345 TREE_FIXED_CST (t2
));
12348 return c_tree_equal (TREE_REALPART (t1
), TREE_REALPART (t2
))
12349 && c_tree_equal (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
12352 return operand_equal_p (t1
, t2
, OEP_ONLY_CONST
);
12355 /* We need to do this when determining whether or not two
12356 non-type pointer to member function template arguments
12358 if (!comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))
12359 || CONSTRUCTOR_NELTS (t1
) != CONSTRUCTOR_NELTS (t2
))
12364 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, field
, value
)
12366 constructor_elt
*elt2
= CONSTRUCTOR_ELT (t2
, i
);
12367 if (!c_tree_equal (field
, elt2
->index
)
12368 || !c_tree_equal (value
, elt2
->value
))
12375 if (!c_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)))
12377 if (!c_tree_equal (TREE_VALUE (t1
), TREE_VALUE (t2
)))
12379 return c_tree_equal (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
12382 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
12387 call_expr_arg_iterator iter1
, iter2
;
12388 if (!c_tree_equal (CALL_EXPR_FN (t1
), CALL_EXPR_FN (t2
)))
12390 for (arg1
= first_call_expr_arg (t1
, &iter1
),
12391 arg2
= first_call_expr_arg (t2
, &iter2
);
12393 arg1
= next_call_expr_arg (&iter1
),
12394 arg2
= next_call_expr_arg (&iter2
))
12395 if (!c_tree_equal (arg1
, arg2
))
12404 tree o1
= TREE_OPERAND (t1
, 0);
12405 tree o2
= TREE_OPERAND (t2
, 0);
12407 /* Special case: if either target is an unallocated VAR_DECL,
12408 it means that it's going to be unified with whatever the
12409 TARGET_EXPR is really supposed to initialize, so treat it
12410 as being equivalent to anything. */
12411 if (TREE_CODE (o1
) == VAR_DECL
&& DECL_NAME (o1
) == NULL_TREE
12412 && !DECL_RTL_SET_P (o1
))
12414 else if (TREE_CODE (o2
) == VAR_DECL
&& DECL_NAME (o2
) == NULL_TREE
12415 && !DECL_RTL_SET_P (o2
))
12417 else if (!c_tree_equal (o1
, o2
))
12420 return c_tree_equal (TREE_OPERAND (t1
, 1), TREE_OPERAND (t2
, 1));
12423 case COMPONENT_REF
:
12424 if (TREE_OPERAND (t1
, 1) != TREE_OPERAND (t2
, 1))
12426 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
12432 case FUNCTION_DECL
:
12433 case IDENTIFIER_NODE
:
12440 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
12442 for (ix
= TREE_VEC_LENGTH (t1
); ix
--;)
12443 if (!c_tree_equal (TREE_VEC_ELT (t1
, ix
),
12444 TREE_VEC_ELT (t2
, ix
)))
12453 switch (TREE_CODE_CLASS (code1
))
12457 case tcc_comparison
:
12458 case tcc_expression
:
12460 case tcc_reference
:
12461 case tcc_statement
:
12463 int i
, n
= TREE_OPERAND_LENGTH (t1
);
12467 case PREINCREMENT_EXPR
:
12468 case PREDECREMENT_EXPR
:
12469 case POSTINCREMENT_EXPR
:
12470 case POSTDECREMENT_EXPR
:
12480 if (TREE_CODE_CLASS (code1
) == tcc_vl_exp
12481 && n
!= TREE_OPERAND_LENGTH (t2
))
12484 for (i
= 0; i
< n
; ++i
)
12485 if (!c_tree_equal (TREE_OPERAND (t1
, i
), TREE_OPERAND (t2
, i
)))
12492 return comptypes (t1
, t2
);
12494 gcc_unreachable ();
12496 /* We can get here with --disable-checking. */
12500 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12501 spawn-helper and BODY is the newly created body for FNDECL. */
12504 cilk_install_body_with_frame_cleanup (tree fndecl
, tree body
, void *w
)
12506 tree list
= alloc_stmt_list ();
12507 tree frame
= make_cilk_frame (fndecl
);
12508 tree dtor
= create_cilk_function_exit (frame
, false, true);
12509 add_local_decl (cfun
, frame
);
12511 DECL_SAVED_TREE (fndecl
) = list
;
12512 tree frame_ptr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (frame
)),
12514 tree body_list
= cilk_install_body_pedigree_operations (frame_ptr
);
12515 gcc_assert (TREE_CODE (body_list
) == STATEMENT_LIST
);
12517 tree detach_expr
= build_call_expr (cilk_detach_fndecl
, 1, frame_ptr
);
12518 append_to_statement_list (detach_expr
, &body_list
);
12520 cilk_outline (fndecl
, &body
, (struct wrapper_data
*) w
);
12521 body
= fold_build_cleanup_point_expr (void_type_node
, body
);
12523 append_to_statement_list (body
, &body_list
);
12524 append_to_statement_list (build_stmt (EXPR_LOCATION (body
), TRY_FINALLY_EXPR
,
12525 body_list
, dtor
), &list
);