1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2016 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"
33 #include "gimple-expr.h"
35 #include "stor-layout.h"
36 #include "trans-mem.h"
39 #include "langhooks.h"
42 #include "tree-iterator.h"
44 #include "tree-inline.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-ubsan.h"
49 #include "gomp-constants.h"
50 #include "spellcheck-tree.h"
51 #include "gcc-rich-location.h"
53 /* Possible cases of implicit bad conversions. Used to select
54 diagnostic messages in convert_for_assignment. */
62 /* The level of nesting inside "__alignof__". */
65 /* The level of nesting inside "sizeof". */
68 /* The level of nesting inside "typeof". */
71 /* The argument of last parsed sizeof expression, only to be tested
72 if expr.original_code == SIZEOF_EXPR. */
73 tree c_last_sizeof_arg
;
75 /* Nonzero if we might need to print a "missing braces around
76 initializer" message within this initializer. */
77 static int found_missing_braces
;
79 static int require_constant_value
;
80 static int require_constant_elements
;
82 static bool null_pointer_constant_p (const_tree
);
83 static tree
qualify_type (tree
, tree
);
84 static int tagged_types_tu_compatible_p (const_tree
, const_tree
, bool *,
86 static int comp_target_types (location_t
, tree
, tree
);
87 static int function_types_compatible_p (const_tree
, const_tree
, bool *,
89 static int type_lists_compatible_p (const_tree
, const_tree
, bool *, bool *);
90 static tree
lookup_field (tree
, tree
);
91 static int convert_arguments (location_t
, vec
<location_t
>, tree
,
92 vec
<tree
, va_gc
> *, vec
<tree
, va_gc
> *, tree
,
94 static tree
pointer_diff (location_t
, tree
, tree
);
95 static tree
convert_for_assignment (location_t
, location_t
, tree
, tree
, tree
,
96 enum impl_conv
, bool, tree
, tree
, int);
97 static tree
valid_compound_expr_initializer (tree
, tree
);
98 static void push_string (const char *);
99 static void push_member_name (tree
);
100 static int spelling_length (void);
101 static char *print_spelling (char *);
102 static void warning_init (location_t
, int, const char *);
103 static tree
digest_init (location_t
, tree
, tree
, tree
, bool, bool, int);
104 static void output_init_element (location_t
, tree
, tree
, bool, tree
, tree
, int,
105 bool, struct obstack
*);
106 static void output_pending_init_elements (int, struct obstack
*);
107 static int set_designator (location_t
, int, struct obstack
*);
108 static void push_range_stack (tree
, struct obstack
*);
109 static void add_pending_init (location_t
, tree
, tree
, tree
, bool,
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 (loc, exp);' to make sure exp
187 does not have an incomplete type. (That includes void types.)
188 LOC is the location of the use. */
191 require_complete_type (location_t loc
, tree value
)
193 tree type
= TREE_TYPE (value
);
195 if (error_operand_p (value
))
196 return error_mark_node
;
198 /* First, detect a valid value with a complete type. */
199 if (COMPLETE_TYPE_P (type
))
202 c_incomplete_type_error (loc
, value
, type
);
203 return error_mark_node
;
206 /* Print an error message for invalid use of an incomplete type.
207 VALUE is the expression that was used (or 0 if that isn't known)
208 and TYPE is the type that was invalid. LOC is the location for
212 c_incomplete_type_error (location_t loc
, const_tree value
, const_tree type
)
214 /* Avoid duplicate error message. */
215 if (TREE_CODE (type
) == ERROR_MARK
)
218 if (value
!= 0 && (VAR_P (value
) || TREE_CODE (value
) == PARM_DECL
))
219 error_at (loc
, "%qD has an incomplete type %qT", value
, type
);
223 /* We must print an error message. Be clever about what it says. */
225 switch (TREE_CODE (type
))
233 error_at (loc
, "invalid use of void expression");
237 if (TYPE_DOMAIN (type
))
239 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
241 error_at (loc
, "invalid use of flexible array member");
244 type
= TREE_TYPE (type
);
247 error_at (loc
, "invalid use of array with unspecified bounds");
254 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
255 error_at (loc
, "invalid use of undefined type %qT", type
);
257 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
258 error_at (loc
, "invalid use of incomplete typedef %qT", type
);
262 /* Given a type, apply default promotions wrt unnamed function
263 arguments and return the new type. */
266 c_type_promotes_to (tree type
)
268 tree ret
= NULL_TREE
;
270 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
271 ret
= double_type_node
;
272 else if (c_promoting_integer_type_p (type
))
274 /* Preserve unsignedness if not really getting any wider. */
275 if (TYPE_UNSIGNED (type
)
276 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
277 ret
= unsigned_type_node
;
279 ret
= integer_type_node
;
282 if (ret
!= NULL_TREE
)
283 return (TYPE_ATOMIC (type
)
284 ? c_build_qualified_type (ret
, TYPE_QUAL_ATOMIC
)
290 /* Return true if between two named address spaces, whether there is a superset
291 named address space that encompasses both address spaces. If there is a
292 superset, return which address space is the superset. */
295 addr_space_superset (addr_space_t as1
, addr_space_t as2
, addr_space_t
*common
)
302 else if (targetm
.addr_space
.subset_p (as1
, as2
))
307 else if (targetm
.addr_space
.subset_p (as2
, as1
))
316 /* Return a variant of TYPE which has all the type qualifiers of LIKE
317 as well as those of TYPE. */
320 qualify_type (tree type
, tree like
)
322 addr_space_t as_type
= TYPE_ADDR_SPACE (type
);
323 addr_space_t as_like
= TYPE_ADDR_SPACE (like
);
324 addr_space_t as_common
;
326 /* If the two named address spaces are different, determine the common
327 superset address space. If there isn't one, raise an error. */
328 if (!addr_space_superset (as_type
, as_like
, &as_common
))
331 error ("%qT and %qT are in disjoint named address spaces",
335 return c_build_qualified_type (type
,
336 TYPE_QUALS_NO_ADDR_SPACE (type
)
337 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like
)
338 | ENCODE_QUAL_ADDR_SPACE (as_common
));
341 /* Return true iff the given tree T is a variable length array. */
344 c_vla_type_p (const_tree t
)
346 if (TREE_CODE (t
) == ARRAY_TYPE
347 && C_TYPE_VARIABLE_SIZE (t
))
352 /* Return the composite type of two compatible types.
354 We assume that comptypes has already been done and returned
355 nonzero; if that isn't so, this may crash. In particular, we
356 assume that qualifiers match. */
359 composite_type (tree t1
, tree t2
)
361 enum tree_code code1
;
362 enum tree_code code2
;
365 /* Save time if the two types are the same. */
367 if (t1
== t2
) return t1
;
369 /* If one type is nonsense, use the other. */
370 if (t1
== error_mark_node
)
372 if (t2
== error_mark_node
)
375 code1
= TREE_CODE (t1
);
376 code2
= TREE_CODE (t2
);
378 /* Merge the attributes. */
379 attributes
= targetm
.merge_type_attributes (t1
, t2
);
381 /* If one is an enumerated type and the other is the compatible
382 integer type, the composite type might be either of the two
383 (DR#013 question 3). For consistency, use the enumerated type as
384 the composite type. */
386 if (code1
== ENUMERAL_TYPE
&& code2
== INTEGER_TYPE
)
388 if (code2
== ENUMERAL_TYPE
&& code1
== INTEGER_TYPE
)
391 gcc_assert (code1
== code2
);
396 /* For two pointers, do this recursively on the target type. */
398 tree pointed_to_1
= TREE_TYPE (t1
);
399 tree pointed_to_2
= TREE_TYPE (t2
);
400 tree target
= composite_type (pointed_to_1
, pointed_to_2
);
401 t1
= build_pointer_type_for_mode (target
, TYPE_MODE (t1
), false);
402 t1
= build_type_attribute_variant (t1
, attributes
);
403 return qualify_type (t1
, t2
);
408 tree elt
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
411 tree d1
= TYPE_DOMAIN (t1
);
412 tree d2
= TYPE_DOMAIN (t2
);
413 bool d1_variable
, d2_variable
;
414 bool d1_zero
, d2_zero
;
415 bool t1_complete
, t2_complete
;
417 /* We should not have any type quals on arrays at all. */
418 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1
)
419 && !TYPE_QUALS_NO_ADDR_SPACE (t2
));
421 t1_complete
= COMPLETE_TYPE_P (t1
);
422 t2_complete
= COMPLETE_TYPE_P (t2
);
424 d1_zero
= d1
== 0 || !TYPE_MAX_VALUE (d1
);
425 d2_zero
= d2
== 0 || !TYPE_MAX_VALUE (d2
);
427 d1_variable
= (!d1_zero
428 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
429 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
430 d2_variable
= (!d2_zero
431 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
432 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
433 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
434 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
436 /* Save space: see if the result is identical to one of the args. */
437 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
)
438 && (d2_variable
|| d2_zero
|| !d1_variable
))
439 return build_type_attribute_variant (t1
, attributes
);
440 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
)
441 && (d1_variable
|| d1_zero
|| !d2_variable
))
442 return build_type_attribute_variant (t2
, attributes
);
444 if (elt
== TREE_TYPE (t1
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
445 return build_type_attribute_variant (t1
, attributes
);
446 if (elt
== TREE_TYPE (t2
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
447 return build_type_attribute_variant (t2
, attributes
);
449 /* Merge the element types, and have a size if either arg has
450 one. We may have qualifiers on the element types. To set
451 up TYPE_MAIN_VARIANT correctly, we need to form the
452 composite of the unqualified types and add the qualifiers
454 quals
= TYPE_QUALS (strip_array_types (elt
));
455 unqual_elt
= c_build_qualified_type (elt
, TYPE_UNQUALIFIED
);
456 t1
= build_array_type (unqual_elt
,
457 TYPE_DOMAIN ((TYPE_DOMAIN (t1
)
463 /* Ensure a composite type involving a zero-length array type
464 is a zero-length type not an incomplete type. */
465 if (d1_zero
&& d2_zero
466 && (t1_complete
|| t2_complete
)
467 && !COMPLETE_TYPE_P (t1
))
469 TYPE_SIZE (t1
) = bitsize_zero_node
;
470 TYPE_SIZE_UNIT (t1
) = size_zero_node
;
472 t1
= c_build_qualified_type (t1
, quals
);
473 return build_type_attribute_variant (t1
, attributes
);
479 if (attributes
!= NULL
)
481 /* Try harder not to create a new aggregate type. */
482 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
484 if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
487 return build_type_attribute_variant (t1
, attributes
);
490 /* Function types: prefer the one that specified arg types.
491 If both do, merge the arg types. Also merge the return types. */
493 tree valtype
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
494 tree p1
= TYPE_ARG_TYPES (t1
);
495 tree p2
= TYPE_ARG_TYPES (t2
);
500 /* Save space: see if the result is identical to one of the args. */
501 if (valtype
== TREE_TYPE (t1
) && !TYPE_ARG_TYPES (t2
))
502 return build_type_attribute_variant (t1
, attributes
);
503 if (valtype
== TREE_TYPE (t2
) && !TYPE_ARG_TYPES (t1
))
504 return build_type_attribute_variant (t2
, attributes
);
506 /* Simple way if one arg fails to specify argument types. */
507 if (TYPE_ARG_TYPES (t1
) == 0)
509 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
510 t1
= build_type_attribute_variant (t1
, attributes
);
511 return qualify_type (t1
, t2
);
513 if (TYPE_ARG_TYPES (t2
) == 0)
515 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
516 t1
= build_type_attribute_variant (t1
, attributes
);
517 return qualify_type (t1
, t2
);
520 /* If both args specify argument types, we must merge the two
521 lists, argument by argument. */
523 for (len
= 0, newargs
= p1
;
524 newargs
&& newargs
!= void_list_node
;
525 len
++, newargs
= TREE_CHAIN (newargs
))
528 for (i
= 0; i
< len
; i
++)
529 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
533 for (; p1
&& p1
!= void_list_node
;
534 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
536 /* A null type means arg type is not specified.
537 Take whatever the other function type has. */
538 if (TREE_VALUE (p1
) == 0)
540 TREE_VALUE (n
) = TREE_VALUE (p2
);
543 if (TREE_VALUE (p2
) == 0)
545 TREE_VALUE (n
) = TREE_VALUE (p1
);
549 /* Given wait (union {union wait *u; int *i} *)
550 and wait (union wait *),
551 prefer union wait * as type of parm. */
552 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
553 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
556 tree mv2
= TREE_VALUE (p2
);
557 if (mv2
&& mv2
!= error_mark_node
558 && TREE_CODE (mv2
) != ARRAY_TYPE
)
559 mv2
= TYPE_MAIN_VARIANT (mv2
);
560 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
561 memb
; memb
= DECL_CHAIN (memb
))
563 tree mv3
= TREE_TYPE (memb
);
564 if (mv3
&& mv3
!= error_mark_node
565 && TREE_CODE (mv3
) != ARRAY_TYPE
)
566 mv3
= TYPE_MAIN_VARIANT (mv3
);
567 if (comptypes (mv3
, mv2
))
569 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
571 pedwarn (input_location
, OPT_Wpedantic
,
572 "function types not truly compatible in ISO C");
577 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
578 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
581 tree mv1
= TREE_VALUE (p1
);
582 if (mv1
&& mv1
!= error_mark_node
583 && TREE_CODE (mv1
) != ARRAY_TYPE
)
584 mv1
= TYPE_MAIN_VARIANT (mv1
);
585 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
586 memb
; memb
= DECL_CHAIN (memb
))
588 tree mv3
= TREE_TYPE (memb
);
589 if (mv3
&& mv3
!= error_mark_node
590 && TREE_CODE (mv3
) != ARRAY_TYPE
)
591 mv3
= TYPE_MAIN_VARIANT (mv3
);
592 if (comptypes (mv3
, mv1
))
594 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
596 pedwarn (input_location
, OPT_Wpedantic
,
597 "function types not truly compatible in ISO C");
602 TREE_VALUE (n
) = composite_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
606 t1
= build_function_type (valtype
, newargs
);
607 t1
= qualify_type (t1
, t2
);
612 return build_type_attribute_variant (t1
, attributes
);
617 /* Return the type of a conditional expression between pointers to
618 possibly differently qualified versions of compatible types.
620 We assume that comp_target_types has already been done and returned
621 nonzero; if that isn't so, this may crash. */
624 common_pointer_type (tree t1
, tree t2
)
627 tree pointed_to_1
, mv1
;
628 tree pointed_to_2
, mv2
;
630 unsigned target_quals
;
631 addr_space_t as1
, as2
, as_common
;
634 /* Save time if the two types are the same. */
636 if (t1
== t2
) return t1
;
638 /* If one type is nonsense, use the other. */
639 if (t1
== error_mark_node
)
641 if (t2
== error_mark_node
)
644 gcc_assert (TREE_CODE (t1
) == POINTER_TYPE
645 && TREE_CODE (t2
) == POINTER_TYPE
);
647 /* Merge the attributes. */
648 attributes
= targetm
.merge_type_attributes (t1
, t2
);
650 /* Find the composite type of the target types, and combine the
651 qualifiers of the two types' targets. Do not lose qualifiers on
652 array element types by taking the TYPE_MAIN_VARIANT. */
653 mv1
= pointed_to_1
= TREE_TYPE (t1
);
654 mv2
= pointed_to_2
= TREE_TYPE (t2
);
655 if (TREE_CODE (mv1
) != ARRAY_TYPE
)
656 mv1
= TYPE_MAIN_VARIANT (pointed_to_1
);
657 if (TREE_CODE (mv2
) != ARRAY_TYPE
)
658 mv2
= TYPE_MAIN_VARIANT (pointed_to_2
);
659 target
= composite_type (mv1
, mv2
);
661 /* Strip array types to get correct qualifier for pointers to arrays */
662 quals1
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1
));
663 quals2
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2
));
665 /* For function types do not merge const qualifiers, but drop them
666 if used inconsistently. The middle-end uses these to mark const
667 and noreturn functions. */
668 if (TREE_CODE (pointed_to_1
) == FUNCTION_TYPE
)
669 target_quals
= (quals1
& quals2
);
671 target_quals
= (quals1
| quals2
);
673 /* If the two named address spaces are different, determine the common
674 superset address space. This is guaranteed to exist due to the
675 assumption that comp_target_type returned non-zero. */
676 as1
= TYPE_ADDR_SPACE (pointed_to_1
);
677 as2
= TYPE_ADDR_SPACE (pointed_to_2
);
678 if (!addr_space_superset (as1
, as2
, &as_common
))
681 target_quals
|= ENCODE_QUAL_ADDR_SPACE (as_common
);
683 t1
= build_pointer_type (c_build_qualified_type (target
, target_quals
));
684 return build_type_attribute_variant (t1
, attributes
);
687 /* Return the common type for two arithmetic types under the usual
688 arithmetic conversions. The default conversions have already been
689 applied, and enumerated types converted to their compatible integer
690 types. The resulting type is unqualified and has no attributes.
692 This is the type for the result of most arithmetic operations
693 if the operands have the given two types. */
696 c_common_type (tree t1
, tree t2
)
698 enum tree_code code1
;
699 enum tree_code code2
;
701 /* If one type is nonsense, use the other. */
702 if (t1
== error_mark_node
)
704 if (t2
== error_mark_node
)
707 if (TYPE_QUALS (t1
) != TYPE_UNQUALIFIED
)
708 t1
= TYPE_MAIN_VARIANT (t1
);
710 if (TYPE_QUALS (t2
) != TYPE_UNQUALIFIED
)
711 t2
= TYPE_MAIN_VARIANT (t2
);
713 if (TYPE_ATTRIBUTES (t1
) != NULL_TREE
)
714 t1
= build_type_attribute_variant (t1
, NULL_TREE
);
716 if (TYPE_ATTRIBUTES (t2
) != NULL_TREE
)
717 t2
= build_type_attribute_variant (t2
, NULL_TREE
);
719 /* Save time if the two types are the same. */
721 if (t1
== t2
) return t1
;
723 code1
= TREE_CODE (t1
);
724 code2
= TREE_CODE (t2
);
726 gcc_assert (code1
== VECTOR_TYPE
|| code1
== COMPLEX_TYPE
727 || code1
== FIXED_POINT_TYPE
|| code1
== REAL_TYPE
728 || code1
== INTEGER_TYPE
);
729 gcc_assert (code2
== VECTOR_TYPE
|| code2
== COMPLEX_TYPE
730 || code2
== FIXED_POINT_TYPE
|| code2
== REAL_TYPE
731 || code2
== INTEGER_TYPE
);
733 /* When one operand is a decimal float type, the other operand cannot be
734 a generic float type or a complex type. We also disallow vector types
736 if ((DECIMAL_FLOAT_TYPE_P (t1
) || DECIMAL_FLOAT_TYPE_P (t2
))
737 && !(DECIMAL_FLOAT_TYPE_P (t1
) && DECIMAL_FLOAT_TYPE_P (t2
)))
739 if (code1
== VECTOR_TYPE
|| code2
== VECTOR_TYPE
)
741 error ("can%'t mix operands of decimal float and vector types");
742 return error_mark_node
;
744 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
746 error ("can%'t mix operands of decimal float and complex types");
747 return error_mark_node
;
749 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
751 error ("can%'t mix operands of decimal float and other float types");
752 return error_mark_node
;
756 /* If one type is a vector type, return that type. (How the usual
757 arithmetic conversions apply to the vector types extension is not
758 precisely specified.) */
759 if (code1
== VECTOR_TYPE
)
762 if (code2
== VECTOR_TYPE
)
765 /* If one type is complex, form the common type of the non-complex
766 components, then make that complex. Use T1 or T2 if it is the
768 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
770 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
771 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
772 tree subtype
= c_common_type (subtype1
, subtype2
);
774 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
776 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
779 return build_complex_type (subtype
);
782 /* If only one is real, use it as the result. */
784 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
787 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
790 /* If both are real and either are decimal floating point types, use
791 the decimal floating point type with the greater precision. */
793 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
795 if (TYPE_MAIN_VARIANT (t1
) == dfloat128_type_node
796 || TYPE_MAIN_VARIANT (t2
) == dfloat128_type_node
)
797 return dfloat128_type_node
;
798 else if (TYPE_MAIN_VARIANT (t1
) == dfloat64_type_node
799 || TYPE_MAIN_VARIANT (t2
) == dfloat64_type_node
)
800 return dfloat64_type_node
;
801 else if (TYPE_MAIN_VARIANT (t1
) == dfloat32_type_node
802 || TYPE_MAIN_VARIANT (t2
) == dfloat32_type_node
)
803 return dfloat32_type_node
;
806 /* Deal with fixed-point types. */
807 if (code1
== FIXED_POINT_TYPE
|| code2
== FIXED_POINT_TYPE
)
809 unsigned int unsignedp
= 0, satp
= 0;
811 unsigned int fbit1
, ibit1
, fbit2
, ibit2
, max_fbit
, max_ibit
;
816 /* If one input type is saturating, the result type is saturating. */
817 if (TYPE_SATURATING (t1
) || TYPE_SATURATING (t2
))
820 /* If both fixed-point types are unsigned, the result type is unsigned.
821 When mixing fixed-point and integer types, follow the sign of the
823 Otherwise, the result type is signed. */
824 if ((TYPE_UNSIGNED (t1
) && TYPE_UNSIGNED (t2
)
825 && code1
== FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
)
826 || (code1
== FIXED_POINT_TYPE
&& code2
!= FIXED_POINT_TYPE
827 && TYPE_UNSIGNED (t1
))
828 || (code1
!= FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
829 && TYPE_UNSIGNED (t2
)))
832 /* The result type is signed. */
835 /* If the input type is unsigned, we need to convert to the
837 if (code1
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t1
))
839 enum mode_class mclass
= (enum mode_class
) 0;
840 if (GET_MODE_CLASS (m1
) == MODE_UFRACT
)
842 else if (GET_MODE_CLASS (m1
) == MODE_UACCUM
)
846 m1
= mode_for_size (GET_MODE_PRECISION (m1
), mclass
, 0);
848 if (code2
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t2
))
850 enum mode_class mclass
= (enum mode_class
) 0;
851 if (GET_MODE_CLASS (m2
) == MODE_UFRACT
)
853 else if (GET_MODE_CLASS (m2
) == MODE_UACCUM
)
857 m2
= mode_for_size (GET_MODE_PRECISION (m2
), mclass
, 0);
861 if (code1
== FIXED_POINT_TYPE
)
863 fbit1
= GET_MODE_FBIT (m1
);
864 ibit1
= GET_MODE_IBIT (m1
);
869 /* Signed integers need to subtract one sign bit. */
870 ibit1
= TYPE_PRECISION (t1
) - (!TYPE_UNSIGNED (t1
));
873 if (code2
== FIXED_POINT_TYPE
)
875 fbit2
= GET_MODE_FBIT (m2
);
876 ibit2
= GET_MODE_IBIT (m2
);
881 /* Signed integers need to subtract one sign bit. */
882 ibit2
= TYPE_PRECISION (t2
) - (!TYPE_UNSIGNED (t2
));
885 max_ibit
= ibit1
>= ibit2
? ibit1
: ibit2
;
886 max_fbit
= fbit1
>= fbit2
? fbit1
: fbit2
;
887 return c_common_fixed_point_type_for_size (max_ibit
, max_fbit
, unsignedp
,
891 /* Both real or both integers; use the one with greater precision. */
893 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
895 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
898 /* Same precision. Prefer long longs to longs to ints when the
899 same precision, following the C99 rules on integer type rank
900 (which are equivalent to the C90 rules for C90 types). */
902 if (TYPE_MAIN_VARIANT (t1
) == long_long_unsigned_type_node
903 || TYPE_MAIN_VARIANT (t2
) == long_long_unsigned_type_node
)
904 return long_long_unsigned_type_node
;
906 if (TYPE_MAIN_VARIANT (t1
) == long_long_integer_type_node
907 || TYPE_MAIN_VARIANT (t2
) == long_long_integer_type_node
)
909 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
910 return long_long_unsigned_type_node
;
912 return long_long_integer_type_node
;
915 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
916 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
917 return long_unsigned_type_node
;
919 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
920 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
922 /* But preserve unsignedness from the other type,
923 since long cannot hold all the values of an unsigned int. */
924 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
925 return long_unsigned_type_node
;
927 return long_integer_type_node
;
930 /* For floating types of the same TYPE_PRECISION (which we here
931 assume means either the same set of values, or sets of values
932 neither a subset of the other, with behavior being undefined in
933 the latter case), follow the rules from TS 18661-3: prefer
934 interchange types _FloatN, then standard types long double,
935 double, float, then extended types _FloatNx. For extended types,
936 check them starting with _Float128x as that seems most consistent
937 in spirit with preferring long double to double; for interchange
938 types, also check in that order for consistency although it's not
939 possible for more than one of them to have the same
941 tree mv1
= TYPE_MAIN_VARIANT (t1
);
942 tree mv2
= TYPE_MAIN_VARIANT (t2
);
944 for (int i
= NUM_FLOATN_TYPES
- 1; i
>= 0; i
--)
945 if (mv1
== FLOATN_TYPE_NODE (i
) || mv2
== FLOATN_TYPE_NODE (i
))
946 return FLOATN_TYPE_NODE (i
);
948 /* Likewise, prefer long double to double even if same size. */
949 if (mv1
== long_double_type_node
|| mv2
== long_double_type_node
)
950 return long_double_type_node
;
952 /* Likewise, prefer double to float even if same size.
953 We got a couple of embedded targets with 32 bit doubles, and the
954 pdp11 might have 64 bit floats. */
955 if (mv1
== double_type_node
|| mv2
== double_type_node
)
956 return double_type_node
;
958 if (mv1
== float_type_node
|| mv2
== float_type_node
)
959 return float_type_node
;
961 for (int i
= NUM_FLOATNX_TYPES
- 1; i
>= 0; i
--)
962 if (mv1
== FLOATNX_TYPE_NODE (i
) || mv2
== FLOATNX_TYPE_NODE (i
))
963 return FLOATNX_TYPE_NODE (i
);
965 /* Otherwise prefer the unsigned one. */
967 if (TYPE_UNSIGNED (t1
))
973 /* Wrapper around c_common_type that is used by c-common.c and other
974 front end optimizations that remove promotions. ENUMERAL_TYPEs
975 are allowed here and are converted to their compatible integer types.
976 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
977 preferably a non-Boolean type as the common type. */
979 common_type (tree t1
, tree t2
)
981 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
982 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
983 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
984 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
986 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
987 if (TREE_CODE (t1
) == BOOLEAN_TYPE
988 && TREE_CODE (t2
) == BOOLEAN_TYPE
)
989 return boolean_type_node
;
991 /* If either type is BOOLEAN_TYPE, then return the other. */
992 if (TREE_CODE (t1
) == BOOLEAN_TYPE
)
994 if (TREE_CODE (t2
) == BOOLEAN_TYPE
)
997 return c_common_type (t1
, t2
);
1000 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1001 or various other operations. Return 2 if they are compatible
1002 but a warning may be needed if you use them together. */
1005 comptypes (tree type1
, tree type2
)
1007 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1010 val
= comptypes_internal (type1
, type2
, NULL
, NULL
);
1011 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1016 /* Like comptypes, but if it returns non-zero because enum and int are
1017 compatible, it sets *ENUM_AND_INT_P to true. */
1020 comptypes_check_enum_int (tree type1
, tree type2
, bool *enum_and_int_p
)
1022 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1025 val
= comptypes_internal (type1
, type2
, enum_and_int_p
, NULL
);
1026 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1031 /* Like comptypes, but if it returns nonzero for different types, it
1032 sets *DIFFERENT_TYPES_P to true. */
1035 comptypes_check_different_types (tree type1
, tree type2
,
1036 bool *different_types_p
)
1038 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1041 val
= comptypes_internal (type1
, type2
, NULL
, different_types_p
);
1042 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1047 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1048 or various other operations. Return 2 if they are compatible
1049 but a warning may be needed if you use them together. If
1050 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1051 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1052 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1053 NULL, and the types are compatible but different enough not to be
1054 permitted in C11 typedef redeclarations, then this sets
1055 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1056 false, but may or may not be set if the types are incompatible.
1057 This differs from comptypes, in that we don't free the seen
1061 comptypes_internal (const_tree type1
, const_tree type2
, bool *enum_and_int_p
,
1062 bool *different_types_p
)
1064 const_tree t1
= type1
;
1065 const_tree t2
= type2
;
1068 /* Suppress errors caused by previously reported errors. */
1070 if (t1
== t2
|| !t1
|| !t2
1071 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
1074 /* Enumerated types are compatible with integer types, but this is
1075 not transitive: two enumerated types in the same translation unit
1076 are compatible with each other only if they are the same type. */
1078 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
1080 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TYPE_UNSIGNED (t1
));
1081 if (TREE_CODE (t2
) != VOID_TYPE
)
1083 if (enum_and_int_p
!= NULL
)
1084 *enum_and_int_p
= true;
1085 if (different_types_p
!= NULL
)
1086 *different_types_p
= true;
1089 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
1091 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TYPE_UNSIGNED (t2
));
1092 if (TREE_CODE (t1
) != VOID_TYPE
)
1094 if (enum_and_int_p
!= NULL
)
1095 *enum_and_int_p
= true;
1096 if (different_types_p
!= NULL
)
1097 *different_types_p
= true;
1104 /* Different classes of types can't be compatible. */
1106 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1109 /* Qualifiers must match. C99 6.7.3p9 */
1111 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1114 /* Allow for two different type nodes which have essentially the same
1115 definition. Note that we already checked for equality of the type
1116 qualifiers (just above). */
1118 if (TREE_CODE (t1
) != ARRAY_TYPE
1119 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1122 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1123 if (!(attrval
= comp_type_attributes (t1
, t2
)))
1126 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1129 switch (TREE_CODE (t1
))
1132 case FIXED_POINT_TYPE
:
1134 /* With these nodes, we can't determine type equivalence by
1135 looking at what is stored in the nodes themselves, because
1136 two nodes might have different TYPE_MAIN_VARIANTs but still
1137 represent the same type. For example, wchar_t and int could
1138 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1139 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1140 and are distinct types. On the other hand, int and the
1143 typedef int INT __attribute((may_alias));
1145 have identical properties, different TYPE_MAIN_VARIANTs, but
1146 represent the same type. The canonical type system keeps
1147 track of equivalence in this case, so we fall back on it. */
1148 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1151 /* Do not remove mode information. */
1152 if (TYPE_MODE (t1
) != TYPE_MODE (t2
))
1154 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
1155 ? 1 : comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1156 enum_and_int_p
, different_types_p
));
1160 val
= function_types_compatible_p (t1
, t2
, enum_and_int_p
,
1166 tree d1
= TYPE_DOMAIN (t1
);
1167 tree d2
= TYPE_DOMAIN (t2
);
1168 bool d1_variable
, d2_variable
;
1169 bool d1_zero
, d2_zero
;
1172 /* Target types must match incl. qualifiers. */
1173 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
1174 && 0 == (val
= comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1176 different_types_p
)))
1179 if (different_types_p
!= NULL
1180 && (d1
== 0) != (d2
== 0))
1181 *different_types_p
= true;
1182 /* Sizes must match unless one is missing or variable. */
1183 if (d1
== 0 || d2
== 0 || d1
== d2
)
1186 d1_zero
= !TYPE_MAX_VALUE (d1
);
1187 d2_zero
= !TYPE_MAX_VALUE (d2
);
1189 d1_variable
= (!d1_zero
1190 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
1191 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
1192 d2_variable
= (!d2_zero
1193 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
1194 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
1195 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
1196 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
1198 if (different_types_p
!= NULL
1199 && d1_variable
!= d2_variable
)
1200 *different_types_p
= true;
1201 if (d1_variable
|| d2_variable
)
1203 if (d1_zero
&& d2_zero
)
1205 if (d1_zero
|| d2_zero
1206 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
1207 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
1216 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
1218 tree a1
= TYPE_ATTRIBUTES (t1
);
1219 tree a2
= TYPE_ATTRIBUTES (t2
);
1221 if (! attribute_list_contained (a1
, a2
)
1222 && ! attribute_list_contained (a2
, a1
))
1226 return tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1228 val
= tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1234 val
= (TYPE_VECTOR_SUBPARTS (t1
) == TYPE_VECTOR_SUBPARTS (t2
)
1235 && comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1236 enum_and_int_p
, different_types_p
));
1242 return attrval
== 2 && val
== 1 ? 2 : val
;
1245 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1246 their qualifiers, except for named address spaces. If the pointers point to
1247 different named addresses, then we must determine if one address space is a
1248 subset of the other. */
1251 comp_target_types (location_t location
, tree ttl
, tree ttr
)
1255 tree mvl
= TREE_TYPE (ttl
);
1256 tree mvr
= TREE_TYPE (ttr
);
1257 addr_space_t asl
= TYPE_ADDR_SPACE (mvl
);
1258 addr_space_t asr
= TYPE_ADDR_SPACE (mvr
);
1259 addr_space_t as_common
;
1260 bool enum_and_int_p
;
1262 /* Fail if pointers point to incompatible address spaces. */
1263 if (!addr_space_superset (asl
, asr
, &as_common
))
1266 /* For pedantic record result of comptypes on arrays before losing
1267 qualifiers on the element type below. */
1270 if (TREE_CODE (mvl
) == ARRAY_TYPE
1271 && TREE_CODE (mvr
) == ARRAY_TYPE
)
1272 val_ped
= comptypes (mvl
, mvr
);
1274 /* Qualifiers on element types of array types that are
1275 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1277 mvl
= (TYPE_ATOMIC (strip_array_types (mvl
))
1278 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
), TYPE_QUAL_ATOMIC
)
1279 : TYPE_MAIN_VARIANT (mvl
));
1281 mvr
= (TYPE_ATOMIC (strip_array_types (mvr
))
1282 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
), TYPE_QUAL_ATOMIC
)
1283 : TYPE_MAIN_VARIANT (mvr
));
1285 enum_and_int_p
= false;
1286 val
= comptypes_check_enum_int (mvl
, mvr
, &enum_and_int_p
);
1288 if (val
== 1 && val_ped
!= 1)
1289 pedwarn (location
, OPT_Wpedantic
, "pointers to arrays with different qualifiers "
1290 "are incompatible in ISO C");
1293 pedwarn (location
, OPT_Wpedantic
, "types are not quite compatible");
1295 if (val
== 1 && enum_and_int_p
&& warn_cxx_compat
)
1296 warning_at (location
, OPT_Wc___compat
,
1297 "pointer target types incompatible in C++");
1302 /* Subroutines of `comptypes'. */
1304 /* Determine whether two trees derive from the same translation unit.
1305 If the CONTEXT chain ends in a null, that tree's context is still
1306 being parsed, so if two trees have context chains ending in null,
1307 they're in the same translation unit. */
1309 same_translation_unit_p (const_tree t1
, const_tree t2
)
1311 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
1312 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
1314 case tcc_declaration
:
1315 t1
= DECL_CONTEXT (t1
); break;
1317 t1
= TYPE_CONTEXT (t1
); break;
1318 case tcc_exceptional
:
1319 t1
= BLOCK_SUPERCONTEXT (t1
); break; /* assume block */
1320 default: gcc_unreachable ();
1323 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
1324 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
1326 case tcc_declaration
:
1327 t2
= DECL_CONTEXT (t2
); break;
1329 t2
= TYPE_CONTEXT (t2
); break;
1330 case tcc_exceptional
:
1331 t2
= BLOCK_SUPERCONTEXT (t2
); break; /* assume block */
1332 default: gcc_unreachable ();
1338 /* Allocate the seen two types, assuming that they are compatible. */
1340 static struct tagged_tu_seen_cache
*
1341 alloc_tagged_tu_seen_cache (const_tree t1
, const_tree t2
)
1343 struct tagged_tu_seen_cache
*tu
= XNEW (struct tagged_tu_seen_cache
);
1344 tu
->next
= tagged_tu_seen_base
;
1348 tagged_tu_seen_base
= tu
;
1350 /* The C standard says that two structures in different translation
1351 units are compatible with each other only if the types of their
1352 fields are compatible (among other things). We assume that they
1353 are compatible until proven otherwise when building the cache.
1354 An example where this can occur is:
1359 If we are comparing this against a similar struct in another TU,
1360 and did not assume they were compatible, we end up with an infinite
1366 /* Free the seen types until we get to TU_TIL. */
1369 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*tu_til
)
1371 const struct tagged_tu_seen_cache
*tu
= tagged_tu_seen_base
;
1372 while (tu
!= tu_til
)
1374 const struct tagged_tu_seen_cache
*const tu1
1375 = (const struct tagged_tu_seen_cache
*) tu
;
1377 free (CONST_CAST (struct tagged_tu_seen_cache
*, tu1
));
1379 tagged_tu_seen_base
= tu_til
;
1382 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1383 compatible. If the two types are not the same (which has been
1384 checked earlier), this can only happen when multiple translation
1385 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1386 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1387 comptypes_internal. */
1390 tagged_types_tu_compatible_p (const_tree t1
, const_tree t2
,
1391 bool *enum_and_int_p
, bool *different_types_p
)
1394 bool needs_warning
= false;
1396 /* We have to verify that the tags of the types are the same. This
1397 is harder than it looks because this may be a typedef, so we have
1398 to go look at the original type. It may even be a typedef of a
1400 In the case of compiler-created builtin structs the TYPE_DECL
1401 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1402 while (TYPE_NAME (t1
)
1403 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1404 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
1405 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
1407 while (TYPE_NAME (t2
)
1408 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1409 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
1410 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
1412 /* C90 didn't have the requirement that the two tags be the same. */
1413 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
1416 /* C90 didn't say what happened if one or both of the types were
1417 incomplete; we choose to follow C99 rules here, which is that they
1419 if (TYPE_SIZE (t1
) == NULL
1420 || TYPE_SIZE (t2
) == NULL
)
1424 const struct tagged_tu_seen_cache
* tts_i
;
1425 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
1426 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
1430 switch (TREE_CODE (t1
))
1434 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1435 /* Speed up the case where the type values are in the same order. */
1436 tree tv1
= TYPE_VALUES (t1
);
1437 tree tv2
= TYPE_VALUES (t2
);
1444 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
1446 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
1448 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
1455 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
1459 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
1465 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
1471 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
1473 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
1475 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
1486 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1487 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
1493 /* Speed up the common case where the fields are in the same order. */
1494 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
); s1
&& s2
;
1495 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1499 if (DECL_NAME (s1
) != DECL_NAME (s2
))
1501 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1502 enum_and_int_p
, different_types_p
);
1504 if (result
!= 1 && !DECL_NAME (s1
))
1512 needs_warning
= true;
1514 if (TREE_CODE (s1
) == FIELD_DECL
1515 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1516 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1524 tu
->val
= needs_warning
? 2 : 1;
1528 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= DECL_CHAIN (s1
))
1532 for (s2
= TYPE_FIELDS (t2
); s2
; s2
= DECL_CHAIN (s2
))
1533 if (DECL_NAME (s1
) == DECL_NAME (s2
))
1537 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1541 if (result
!= 1 && !DECL_NAME (s1
))
1549 needs_warning
= true;
1551 if (TREE_CODE (s1
) == FIELD_DECL
1552 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1553 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1565 tu
->val
= needs_warning
? 2 : 10;
1571 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1573 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
1575 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1578 if (TREE_CODE (s1
) != TREE_CODE (s2
)
1579 || DECL_NAME (s1
) != DECL_NAME (s2
))
1581 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1582 enum_and_int_p
, different_types_p
);
1586 needs_warning
= true;
1588 if (TREE_CODE (s1
) == FIELD_DECL
1589 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1590 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1596 tu
->val
= needs_warning
? 2 : 1;
1605 /* Return 1 if two function types F1 and F2 are compatible.
1606 If either type specifies no argument types,
1607 the other must specify a fixed number of self-promoting arg types.
1608 Otherwise, if one type specifies only the number of arguments,
1609 the other must specify that number of self-promoting arg types.
1610 Otherwise, the argument types must match.
1611 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1614 function_types_compatible_p (const_tree f1
, const_tree f2
,
1615 bool *enum_and_int_p
, bool *different_types_p
)
1618 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1623 ret1
= TREE_TYPE (f1
);
1624 ret2
= TREE_TYPE (f2
);
1626 /* 'volatile' qualifiers on a function's return type used to mean
1627 the function is noreturn. */
1628 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
1629 pedwarn (input_location
, 0, "function return types not compatible due to %<volatile%>");
1630 if (TYPE_VOLATILE (ret1
))
1631 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
1632 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
1633 if (TYPE_VOLATILE (ret2
))
1634 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
1635 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
1636 val
= comptypes_internal (ret1
, ret2
, enum_and_int_p
, different_types_p
);
1640 args1
= TYPE_ARG_TYPES (f1
);
1641 args2
= TYPE_ARG_TYPES (f2
);
1643 if (different_types_p
!= NULL
1644 && (args1
== 0) != (args2
== 0))
1645 *different_types_p
= true;
1647 /* An unspecified parmlist matches any specified parmlist
1648 whose argument types don't need default promotions. */
1652 if (!self_promoting_args_p (args2
))
1654 /* If one of these types comes from a non-prototype fn definition,
1655 compare that with the other type's arglist.
1656 If they don't match, ask for a warning (but no error). */
1657 if (TYPE_ACTUAL_ARG_TYPES (f1
)
1658 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
1659 enum_and_int_p
, different_types_p
))
1665 if (!self_promoting_args_p (args1
))
1667 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1668 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
1669 enum_and_int_p
, different_types_p
))
1674 /* Both types have argument lists: compare them and propagate results. */
1675 val1
= type_lists_compatible_p (args1
, args2
, enum_and_int_p
,
1677 return val1
!= 1 ? val1
: val
;
1680 /* Check two lists of types for compatibility, returning 0 for
1681 incompatible, 1 for compatible, or 2 for compatible with
1682 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1683 comptypes_internal. */
1686 type_lists_compatible_p (const_tree args1
, const_tree args2
,
1687 bool *enum_and_int_p
, bool *different_types_p
)
1689 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1695 tree a1
, mv1
, a2
, mv2
;
1696 if (args1
== 0 && args2
== 0)
1698 /* If one list is shorter than the other,
1699 they fail to match. */
1700 if (args1
== 0 || args2
== 0)
1702 mv1
= a1
= TREE_VALUE (args1
);
1703 mv2
= a2
= TREE_VALUE (args2
);
1704 if (mv1
&& mv1
!= error_mark_node
&& TREE_CODE (mv1
) != ARRAY_TYPE
)
1705 mv1
= (TYPE_ATOMIC (mv1
)
1706 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1
),
1708 : TYPE_MAIN_VARIANT (mv1
));
1709 if (mv2
&& mv2
!= error_mark_node
&& TREE_CODE (mv2
) != ARRAY_TYPE
)
1710 mv2
= (TYPE_ATOMIC (mv2
)
1711 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2
),
1713 : TYPE_MAIN_VARIANT (mv2
));
1714 /* A null pointer instead of a type
1715 means there is supposed to be an argument
1716 but nothing is specified about what type it has.
1717 So match anything that self-promotes. */
1718 if (different_types_p
!= NULL
1719 && (a1
== 0) != (a2
== 0))
1720 *different_types_p
= true;
1723 if (c_type_promotes_to (a2
) != a2
)
1728 if (c_type_promotes_to (a1
) != a1
)
1731 /* If one of the lists has an error marker, ignore this arg. */
1732 else if (TREE_CODE (a1
) == ERROR_MARK
1733 || TREE_CODE (a2
) == ERROR_MARK
)
1735 else if (!(newval
= comptypes_internal (mv1
, mv2
, enum_and_int_p
,
1736 different_types_p
)))
1738 if (different_types_p
!= NULL
)
1739 *different_types_p
= true;
1740 /* Allow wait (union {union wait *u; int *i} *)
1741 and wait (union wait *) to be compatible. */
1742 if (TREE_CODE (a1
) == UNION_TYPE
1743 && (TYPE_NAME (a1
) == 0
1744 || TYPE_TRANSPARENT_AGGR (a1
))
1745 && TREE_CODE (TYPE_SIZE (a1
)) == INTEGER_CST
1746 && tree_int_cst_equal (TYPE_SIZE (a1
),
1750 for (memb
= TYPE_FIELDS (a1
);
1751 memb
; memb
= DECL_CHAIN (memb
))
1753 tree mv3
= TREE_TYPE (memb
);
1754 if (mv3
&& mv3
!= error_mark_node
1755 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1756 mv3
= (TYPE_ATOMIC (mv3
)
1757 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1759 : TYPE_MAIN_VARIANT (mv3
));
1760 if (comptypes_internal (mv3
, mv2
, enum_and_int_p
,
1767 else if (TREE_CODE (a2
) == UNION_TYPE
1768 && (TYPE_NAME (a2
) == 0
1769 || TYPE_TRANSPARENT_AGGR (a2
))
1770 && TREE_CODE (TYPE_SIZE (a2
)) == INTEGER_CST
1771 && tree_int_cst_equal (TYPE_SIZE (a2
),
1775 for (memb
= TYPE_FIELDS (a2
);
1776 memb
; memb
= DECL_CHAIN (memb
))
1778 tree mv3
= TREE_TYPE (memb
);
1779 if (mv3
&& mv3
!= error_mark_node
1780 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1781 mv3
= (TYPE_ATOMIC (mv3
)
1782 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1784 : TYPE_MAIN_VARIANT (mv3
));
1785 if (comptypes_internal (mv3
, mv1
, enum_and_int_p
,
1796 /* comptypes said ok, but record if it said to warn. */
1800 args1
= TREE_CHAIN (args1
);
1801 args2
= TREE_CHAIN (args2
);
1805 /* Compute the size to increment a pointer by. When a function type or void
1806 type or incomplete type is passed, size_one_node is returned.
1807 This function does not emit any diagnostics; the caller is responsible
1811 c_size_in_bytes (const_tree type
)
1813 enum tree_code code
= TREE_CODE (type
);
1815 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
1816 || !COMPLETE_TYPE_P (type
))
1817 return size_one_node
;
1819 /* Convert in case a char is more than one unit. */
1820 return size_binop_loc (input_location
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1821 size_int (TYPE_PRECISION (char_type_node
)
1825 /* Return either DECL or its known constant value (if it has one). */
1828 decl_constant_value (tree decl
)
1830 if (/* Don't change a variable array bound or initial value to a constant
1831 in a place where a variable is invalid. Note that DECL_INITIAL
1832 isn't valid for a PARM_DECL. */
1833 current_function_decl
!= 0
1834 && TREE_CODE (decl
) != PARM_DECL
1835 && !TREE_THIS_VOLATILE (decl
)
1836 && TREE_READONLY (decl
)
1837 && DECL_INITIAL (decl
) != 0
1838 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
1839 /* This is invalid if initial value is not constant.
1840 If it has either a function call, a memory reference,
1841 or a variable, then re-evaluating it could give different results. */
1842 && TREE_CONSTANT (DECL_INITIAL (decl
))
1843 /* Check for cases where this is sub-optimal, even though valid. */
1844 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1845 return DECL_INITIAL (decl
);
1849 /* Convert the array expression EXP to a pointer. */
1851 array_to_pointer_conversion (location_t loc
, tree exp
)
1853 tree orig_exp
= exp
;
1854 tree type
= TREE_TYPE (exp
);
1856 tree restype
= TREE_TYPE (type
);
1859 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1861 STRIP_TYPE_NOPS (exp
);
1863 if (TREE_NO_WARNING (orig_exp
))
1864 TREE_NO_WARNING (exp
) = 1;
1866 ptrtype
= build_pointer_type (restype
);
1868 if (INDIRECT_REF_P (exp
))
1869 return convert (ptrtype
, TREE_OPERAND (exp
, 0));
1871 /* In C++ array compound literals are temporary objects unless they are
1872 const or appear in namespace scope, so they are destroyed too soon
1873 to use them for much of anything (c++/53220). */
1874 if (warn_cxx_compat
&& TREE_CODE (exp
) == COMPOUND_LITERAL_EXPR
)
1876 tree decl
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
1877 if (!TREE_READONLY (decl
) && !TREE_STATIC (decl
))
1878 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
1879 "converting an array compound literal to a pointer "
1880 "is ill-formed in C++");
1883 adr
= build_unary_op (loc
, ADDR_EXPR
, exp
, true);
1884 return convert (ptrtype
, adr
);
1887 /* Convert the function expression EXP to a pointer. */
1889 function_to_pointer_conversion (location_t loc
, tree exp
)
1891 tree orig_exp
= exp
;
1893 gcc_assert (TREE_CODE (TREE_TYPE (exp
)) == FUNCTION_TYPE
);
1895 STRIP_TYPE_NOPS (exp
);
1897 if (TREE_NO_WARNING (orig_exp
))
1898 TREE_NO_WARNING (exp
) = 1;
1900 return build_unary_op (loc
, ADDR_EXPR
, exp
, false);
1903 /* Mark EXP as read, not just set, for set but not used -Wunused
1904 warning purposes. */
1907 mark_exp_read (tree exp
)
1909 switch (TREE_CODE (exp
))
1913 DECL_READ_P (exp
) = 1;
1922 case VIEW_CONVERT_EXPR
:
1923 mark_exp_read (TREE_OPERAND (exp
, 0));
1926 case C_MAYBE_CONST_EXPR
:
1927 mark_exp_read (TREE_OPERAND (exp
, 1));
1934 /* Perform the default conversion of arrays and functions to pointers.
1935 Return the result of converting EXP. For any other expression, just
1938 LOC is the location of the expression. */
1941 default_function_array_conversion (location_t loc
, struct c_expr exp
)
1943 tree orig_exp
= exp
.value
;
1944 tree type
= TREE_TYPE (exp
.value
);
1945 enum tree_code code
= TREE_CODE (type
);
1951 bool not_lvalue
= false;
1952 bool lvalue_array_p
;
1954 while ((TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
1955 || CONVERT_EXPR_P (exp
.value
))
1956 && TREE_TYPE (TREE_OPERAND (exp
.value
, 0)) == type
)
1958 if (TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
)
1960 exp
.value
= TREE_OPERAND (exp
.value
, 0);
1963 if (TREE_NO_WARNING (orig_exp
))
1964 TREE_NO_WARNING (exp
.value
) = 1;
1966 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
.value
);
1967 if (!flag_isoc99
&& !lvalue_array_p
)
1969 /* Before C99, non-lvalue arrays do not decay to pointers.
1970 Normally, using such an array would be invalid; but it can
1971 be used correctly inside sizeof or as a statement expression.
1972 Thus, do not give an error here; an error will result later. */
1976 exp
.value
= array_to_pointer_conversion (loc
, exp
.value
);
1980 exp
.value
= function_to_pointer_conversion (loc
, exp
.value
);
1990 default_function_array_read_conversion (location_t loc
, struct c_expr exp
)
1992 mark_exp_read (exp
.value
);
1993 return default_function_array_conversion (loc
, exp
);
1996 /* Return whether EXPR should be treated as an atomic lvalue for the
1997 purposes of load and store handling. */
2000 really_atomic_lvalue (tree expr
)
2002 if (error_operand_p (expr
))
2004 if (!TYPE_ATOMIC (TREE_TYPE (expr
)))
2006 if (!lvalue_p (expr
))
2009 /* Ignore _Atomic on register variables, since their addresses can't
2010 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2011 sequences wouldn't work. Ignore _Atomic on structures containing
2012 bit-fields, since accessing elements of atomic structures or
2013 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2014 it's undefined at translation time or execution time, and the
2015 normal atomic sequences again wouldn't work. */
2016 while (handled_component_p (expr
))
2018 if (TREE_CODE (expr
) == COMPONENT_REF
2019 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
2021 expr
= TREE_OPERAND (expr
, 0);
2023 if (DECL_P (expr
) && C_DECL_REGISTER (expr
))
2028 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2029 including converting functions and arrays to pointers if CONVERT_P.
2030 If READ_P, also mark the expression as having been read. */
2033 convert_lvalue_to_rvalue (location_t loc
, struct c_expr exp
,
2034 bool convert_p
, bool read_p
)
2037 mark_exp_read (exp
.value
);
2039 exp
= default_function_array_conversion (loc
, exp
);
2040 if (really_atomic_lvalue (exp
.value
))
2042 vec
<tree
, va_gc
> *params
;
2043 tree nonatomic_type
, tmp
, tmp_addr
, fndecl
, func_call
;
2044 tree expr_type
= TREE_TYPE (exp
.value
);
2045 tree expr_addr
= build_unary_op (loc
, ADDR_EXPR
, exp
.value
, false);
2046 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
2048 gcc_assert (TYPE_ATOMIC (expr_type
));
2050 /* Expansion of a generic atomic load may require an addition
2051 element, so allocate enough to prevent a resize. */
2052 vec_alloc (params
, 4);
2054 /* Remove the qualifiers for the rest of the expressions and
2055 create the VAL temp variable to hold the RHS. */
2056 nonatomic_type
= build_qualified_type (expr_type
, TYPE_UNQUALIFIED
);
2057 tmp
= create_tmp_var_raw (nonatomic_type
);
2058 tmp_addr
= build_unary_op (loc
, ADDR_EXPR
, tmp
, false);
2059 TREE_ADDRESSABLE (tmp
) = 1;
2060 TREE_NO_WARNING (tmp
) = 1;
2062 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2063 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
2064 params
->quick_push (expr_addr
);
2065 params
->quick_push (tmp_addr
);
2066 params
->quick_push (seq_cst
);
2067 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
2069 /* EXPR is always read. */
2070 mark_exp_read (exp
.value
);
2072 /* Return tmp which contains the value loaded. */
2073 exp
.value
= build4 (TARGET_EXPR
, nonatomic_type
, tmp
, func_call
,
2074 NULL_TREE
, NULL_TREE
);
2079 /* EXP is an expression of integer type. Apply the integer promotions
2080 to it and return the promoted value. */
2083 perform_integral_promotions (tree exp
)
2085 tree type
= TREE_TYPE (exp
);
2086 enum tree_code code
= TREE_CODE (type
);
2088 gcc_assert (INTEGRAL_TYPE_P (type
));
2090 /* Normally convert enums to int,
2091 but convert wide enums to something wider. */
2092 if (code
== ENUMERAL_TYPE
)
2094 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
2095 TYPE_PRECISION (integer_type_node
)),
2096 ((TYPE_PRECISION (type
)
2097 >= TYPE_PRECISION (integer_type_node
))
2098 && TYPE_UNSIGNED (type
)));
2100 return convert (type
, exp
);
2103 /* ??? This should no longer be needed now bit-fields have their
2105 if (TREE_CODE (exp
) == COMPONENT_REF
2106 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
2107 /* If it's thinner than an int, promote it like a
2108 c_promoting_integer_type_p, otherwise leave it alone. */
2109 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
2110 TYPE_PRECISION (integer_type_node
)))
2111 return convert (integer_type_node
, exp
);
2113 if (c_promoting_integer_type_p (type
))
2115 /* Preserve unsignedness if not really getting any wider. */
2116 if (TYPE_UNSIGNED (type
)
2117 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
2118 return convert (unsigned_type_node
, exp
);
2120 return convert (integer_type_node
, exp
);
2127 /* Perform default promotions for C data used in expressions.
2128 Enumeral types or short or char are converted to int.
2129 In addition, manifest constants symbols are replaced by their values. */
2132 default_conversion (tree exp
)
2135 tree type
= TREE_TYPE (exp
);
2136 enum tree_code code
= TREE_CODE (type
);
2139 mark_exp_read (exp
);
2141 /* Functions and arrays have been converted during parsing. */
2142 gcc_assert (code
!= FUNCTION_TYPE
);
2143 if (code
== ARRAY_TYPE
)
2146 /* Constants can be used directly unless they're not loadable. */
2147 if (TREE_CODE (exp
) == CONST_DECL
)
2148 exp
= DECL_INITIAL (exp
);
2150 /* Strip no-op conversions. */
2152 STRIP_TYPE_NOPS (exp
);
2154 if (TREE_NO_WARNING (orig_exp
))
2155 TREE_NO_WARNING (exp
) = 1;
2157 if (code
== VOID_TYPE
)
2159 error_at (EXPR_LOC_OR_LOC (exp
, input_location
),
2160 "void value not ignored as it ought to be");
2161 return error_mark_node
;
2164 exp
= require_complete_type (EXPR_LOC_OR_LOC (exp
, input_location
), exp
);
2165 if (exp
== error_mark_node
)
2166 return error_mark_node
;
2168 promoted_type
= targetm
.promoted_type (type
);
2170 return convert (promoted_type
, exp
);
2172 if (INTEGRAL_TYPE_P (type
))
2173 return perform_integral_promotions (exp
);
2178 /* Look up COMPONENT in a structure or union TYPE.
2180 If the component name is not found, returns NULL_TREE. Otherwise,
2181 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2182 stepping down the chain to the component, which is in the last
2183 TREE_VALUE of the list. Normally the list is of length one, but if
2184 the component is embedded within (nested) anonymous structures or
2185 unions, the list steps down the chain to the component. */
2188 lookup_field (tree type
, tree component
)
2192 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2193 to the field elements. Use a binary search on this array to quickly
2194 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2195 will always be set for structures which have many elements. */
2197 if (TYPE_LANG_SPECIFIC (type
) && TYPE_LANG_SPECIFIC (type
)->s
)
2200 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
2202 field
= TYPE_FIELDS (type
);
2204 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
2205 while (top
- bot
> 1)
2207 half
= (top
- bot
+ 1) >> 1;
2208 field
= field_array
[bot
+half
];
2210 if (DECL_NAME (field
) == NULL_TREE
)
2212 /* Step through all anon unions in linear fashion. */
2213 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
2215 field
= field_array
[bot
++];
2216 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2218 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2221 return tree_cons (NULL_TREE
, field
, anon
);
2223 /* The Plan 9 compiler permits referring
2224 directly to an anonymous struct/union field
2225 using a typedef name. */
2226 if (flag_plan9_extensions
2227 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2228 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field
)))
2230 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2236 /* Entire record is only anon unions. */
2240 /* Restart the binary search, with new lower bound. */
2244 if (DECL_NAME (field
) == component
)
2246 if (DECL_NAME (field
) < component
)
2252 if (DECL_NAME (field_array
[bot
]) == component
)
2253 field
= field_array
[bot
];
2254 else if (DECL_NAME (field
) != component
)
2259 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2261 if (DECL_NAME (field
) == NULL_TREE
2262 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2264 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2267 return tree_cons (NULL_TREE
, field
, anon
);
2269 /* The Plan 9 compiler permits referring directly to an
2270 anonymous struct/union field using a typedef
2272 if (flag_plan9_extensions
2273 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2274 && TREE_CODE (TYPE_NAME (TREE_TYPE (field
))) == TYPE_DECL
2275 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2280 if (DECL_NAME (field
) == component
)
2284 if (field
== NULL_TREE
)
2288 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
2291 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2294 lookup_field_fuzzy_find_candidates (tree type
, tree component
,
2295 vec
<tree
> *candidates
)
2298 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2300 if (DECL_NAME (field
) == NULL_TREE
2301 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2302 lookup_field_fuzzy_find_candidates (TREE_TYPE (field
), component
,
2305 if (DECL_NAME (field
))
2306 candidates
->safe_push (DECL_NAME (field
));
2310 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2311 rather than returning a TREE_LIST for an exact match. */
2314 lookup_field_fuzzy (tree type
, tree component
)
2316 gcc_assert (TREE_CODE (component
) == IDENTIFIER_NODE
);
2318 /* First, gather a list of candidates. */
2319 auto_vec
<tree
> candidates
;
2321 lookup_field_fuzzy_find_candidates (type
, component
,
2324 return find_closest_identifier (component
, &candidates
);
2327 /* Support function for build_component_ref's error-handling.
2329 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2330 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2333 should_suggest_deref_p (tree datum_type
)
2335 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2336 allows "." for ptrs; we could be handling a failed attempt
2337 to access a property. */
2338 if (c_dialect_objc ())
2341 /* Only suggest it for pointers... */
2342 if (TREE_CODE (datum_type
) != POINTER_TYPE
)
2345 /* ...to structs/unions. */
2346 tree underlying_type
= TREE_TYPE (datum_type
);
2347 enum tree_code code
= TREE_CODE (underlying_type
);
2348 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2354 /* Make an expression to refer to the COMPONENT field of structure or
2355 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2356 location of the COMPONENT_REF. COMPONENT_LOC is the location
2360 build_component_ref (location_t loc
, tree datum
, tree component
,
2361 location_t component_loc
)
2363 tree type
= TREE_TYPE (datum
);
2364 enum tree_code code
= TREE_CODE (type
);
2367 bool datum_lvalue
= lvalue_p (datum
);
2369 if (!objc_is_public (datum
, component
))
2370 return error_mark_node
;
2372 /* Detect Objective-C property syntax object.property. */
2373 if (c_dialect_objc ()
2374 && (ref
= objc_maybe_build_component_ref (datum
, component
)))
2377 /* See if there is a field or component with name COMPONENT. */
2379 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2381 if (!COMPLETE_TYPE_P (type
))
2383 c_incomplete_type_error (loc
, NULL_TREE
, type
);
2384 return error_mark_node
;
2387 field
= lookup_field (type
, component
);
2391 tree guessed_id
= lookup_field_fuzzy (type
, component
);
2394 /* Attempt to provide a fixit replacement hint, if
2395 we have a valid range for the component. */
2396 location_t reported_loc
2397 = (component_loc
!= UNKNOWN_LOCATION
) ? component_loc
: loc
;
2398 gcc_rich_location
rich_loc (reported_loc
);
2399 if (component_loc
!= UNKNOWN_LOCATION
)
2400 rich_loc
.add_fixit_misspelled_id (component_loc
, guessed_id
);
2403 "%qT has no member named %qE; did you mean %qE?",
2404 type
, component
, guessed_id
);
2407 error_at (loc
, "%qT has no member named %qE", type
, component
);
2408 return error_mark_node
;
2411 /* Accessing elements of atomic structures or unions is undefined
2412 behavior (C11 6.5.2.3#5). */
2413 if (TYPE_ATOMIC (type
) && c_inhibit_evaluation_warnings
== 0)
2415 if (code
== RECORD_TYPE
)
2416 warning_at (loc
, 0, "accessing a member %qE of an atomic "
2417 "structure %qE", component
, datum
);
2419 warning_at (loc
, 0, "accessing a member %qE of an atomic "
2420 "union %qE", component
, datum
);
2423 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2424 This might be better solved in future the way the C++ front
2425 end does it - by giving the anonymous entities each a
2426 separate name and type, and then have build_component_ref
2427 recursively call itself. We can't do that here. */
2430 tree subdatum
= TREE_VALUE (field
);
2433 bool use_datum_quals
;
2435 if (TREE_TYPE (subdatum
) == error_mark_node
)
2436 return error_mark_node
;
2438 /* If this is an rvalue, it does not have qualifiers in C
2439 standard terms and we must avoid propagating such
2440 qualifiers down to a non-lvalue array that is then
2441 converted to a pointer. */
2442 use_datum_quals
= (datum_lvalue
2443 || TREE_CODE (TREE_TYPE (subdatum
)) != ARRAY_TYPE
);
2445 quals
= TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum
)));
2446 if (use_datum_quals
)
2447 quals
|= TYPE_QUALS (TREE_TYPE (datum
));
2448 subtype
= c_build_qualified_type (TREE_TYPE (subdatum
), quals
);
2450 ref
= build3 (COMPONENT_REF
, subtype
, datum
, subdatum
,
2452 SET_EXPR_LOCATION (ref
, loc
);
2453 if (TREE_READONLY (subdatum
)
2454 || (use_datum_quals
&& TREE_READONLY (datum
)))
2455 TREE_READONLY (ref
) = 1;
2456 if (TREE_THIS_VOLATILE (subdatum
)
2457 || (use_datum_quals
&& TREE_THIS_VOLATILE (datum
)))
2458 TREE_THIS_VOLATILE (ref
) = 1;
2460 if (TREE_DEPRECATED (subdatum
))
2461 warn_deprecated_use (subdatum
, NULL_TREE
);
2465 field
= TREE_CHAIN (field
);
2471 else if (should_suggest_deref_p (type
))
2473 /* Special-case the error message for "ptr.field" for the case
2474 where the user has confused "." vs "->". */
2475 rich_location
richloc (line_table
, loc
);
2476 /* "loc" should be the "." token. */
2477 richloc
.add_fixit_replace ("->");
2478 error_at_rich_loc (&richloc
,
2479 "%qE is a pointer; did you mean to use %<->%>?",
2481 return error_mark_node
;
2483 else if (code
!= ERROR_MARK
)
2485 "request for member %qE in something not a structure or union",
2488 return error_mark_node
;
2491 /* Given an expression PTR for a pointer, return an expression
2492 for the value pointed to.
2493 ERRORSTRING is the name of the operator to appear in error messages.
2495 LOC is the location to use for the generated tree. */
2498 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errstring
)
2500 tree pointer
= default_conversion (ptr
);
2501 tree type
= TREE_TYPE (pointer
);
2504 if (TREE_CODE (type
) == POINTER_TYPE
)
2506 if (CONVERT_EXPR_P (pointer
)
2507 || TREE_CODE (pointer
) == VIEW_CONVERT_EXPR
)
2509 /* If a warning is issued, mark it to avoid duplicates from
2510 the backend. This only needs to be done at
2511 warn_strict_aliasing > 2. */
2512 if (warn_strict_aliasing
> 2)
2513 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer
, 0)),
2514 type
, TREE_OPERAND (pointer
, 0)))
2515 TREE_NO_WARNING (pointer
) = 1;
2518 if (TREE_CODE (pointer
) == ADDR_EXPR
2519 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
2520 == TREE_TYPE (type
)))
2522 ref
= TREE_OPERAND (pointer
, 0);
2523 protected_set_expr_location (ref
, loc
);
2528 tree t
= TREE_TYPE (type
);
2530 ref
= build1 (INDIRECT_REF
, t
, pointer
);
2532 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
2534 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)))
2536 error_at (loc
, "dereferencing pointer to incomplete type "
2538 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)) = 1;
2540 return error_mark_node
;
2542 if (VOID_TYPE_P (t
) && c_inhibit_evaluation_warnings
== 0)
2543 warning_at (loc
, 0, "dereferencing %<void *%> pointer");
2545 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2546 so that we get the proper error message if the result is used
2547 to assign to. Also, &* is supposed to be a no-op.
2548 And ANSI C seems to specify that the type of the result
2549 should be the const type. */
2550 /* A de-reference of a pointer to const is not a const. It is valid
2551 to change it via some other pointer. */
2552 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2553 TREE_SIDE_EFFECTS (ref
)
2554 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
2555 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2556 protected_set_expr_location (ref
, loc
);
2560 else if (TREE_CODE (pointer
) != ERROR_MARK
)
2561 invalid_indirection_error (loc
, type
, errstring
);
2563 return error_mark_node
;
2566 /* This handles expressions of the form "a[i]", which denotes
2569 This is logically equivalent in C to *(a+i), but we may do it differently.
2570 If A is a variable or a member, we generate a primitive ARRAY_REF.
2571 This avoids forcing the array out of registers, and can work on
2572 arrays that are not lvalues (for example, members of structures returned
2575 For vector types, allow vector[i] but not i[vector], and create
2576 *(((type*)&vectortype) + i) for the expression.
2578 LOC is the location to use for the returned expression. */
2581 build_array_ref (location_t loc
, tree array
, tree index
)
2584 bool swapped
= false;
2585 if (TREE_TYPE (array
) == error_mark_node
2586 || TREE_TYPE (index
) == error_mark_node
)
2587 return error_mark_node
;
2589 if (flag_cilkplus
&& contains_array_notation_expr (index
))
2592 if (!find_rank (loc
, index
, index
, true, &rank
))
2593 return error_mark_node
;
2596 error_at (loc
, "rank of the array's index is greater than 1");
2597 return error_mark_node
;
2600 if (TREE_CODE (TREE_TYPE (array
)) != ARRAY_TYPE
2601 && TREE_CODE (TREE_TYPE (array
)) != POINTER_TYPE
2602 /* Allow vector[index] but not index[vector]. */
2603 && !VECTOR_TYPE_P (TREE_TYPE (array
)))
2605 if (TREE_CODE (TREE_TYPE (index
)) != ARRAY_TYPE
2606 && TREE_CODE (TREE_TYPE (index
)) != POINTER_TYPE
)
2609 "subscripted value is neither array nor pointer nor vector");
2611 return error_mark_node
;
2613 std::swap (array
, index
);
2617 if (!INTEGRAL_TYPE_P (TREE_TYPE (index
)))
2619 error_at (loc
, "array subscript is not an integer");
2620 return error_mark_node
;
2623 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array
))) == FUNCTION_TYPE
)
2625 error_at (loc
, "subscripted value is pointer to function");
2626 return error_mark_node
;
2629 /* ??? Existing practice has been to warn only when the char
2630 index is syntactically the index, not for char[array]. */
2632 warn_array_subscript_with_type_char (loc
, index
);
2634 /* Apply default promotions *after* noticing character types. */
2635 index
= default_conversion (index
);
2636 if (index
== error_mark_node
)
2637 return error_mark_node
;
2639 gcc_assert (TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
);
2641 bool was_vector
= VECTOR_TYPE_P (TREE_TYPE (array
));
2642 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, index
);
2644 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
2648 /* An array that is indexed by a non-constant
2649 cannot be stored in a register; we must be able to do
2650 address arithmetic on its address.
2651 Likewise an array of elements of variable size. */
2652 if (TREE_CODE (index
) != INTEGER_CST
2653 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2654 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2656 if (!c_mark_addressable (array
))
2657 return error_mark_node
;
2659 /* An array that is indexed by a constant value which is not within
2660 the array bounds cannot be stored in a register either; because we
2661 would get a crash in store_bit_field/extract_bit_field when trying
2662 to access a non-existent part of the register. */
2663 if (TREE_CODE (index
) == INTEGER_CST
2664 && TYPE_DOMAIN (TREE_TYPE (array
))
2665 && !int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
2667 if (!c_mark_addressable (array
))
2668 return error_mark_node
;
2671 if ((pedantic
|| warn_c90_c99_compat
)
2675 while (TREE_CODE (foo
) == COMPONENT_REF
)
2676 foo
= TREE_OPERAND (foo
, 0);
2677 if (VAR_P (foo
) && C_DECL_REGISTER (foo
))
2678 pedwarn (loc
, OPT_Wpedantic
,
2679 "ISO C forbids subscripting %<register%> array");
2680 else if (!lvalue_p (foo
))
2681 pedwarn_c90 (loc
, OPT_Wpedantic
,
2682 "ISO C90 forbids subscripting non-lvalue "
2686 type
= TREE_TYPE (TREE_TYPE (array
));
2687 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
2688 /* Array ref is const/volatile if the array elements are
2689 or if the array is. */
2690 TREE_READONLY (rval
)
2691 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2692 | TREE_READONLY (array
));
2693 TREE_SIDE_EFFECTS (rval
)
2694 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2695 | TREE_SIDE_EFFECTS (array
));
2696 TREE_THIS_VOLATILE (rval
)
2697 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2698 /* This was added by rms on 16 Nov 91.
2699 It fixes vol struct foo *a; a->elts[1]
2700 in an inline function.
2701 Hope it doesn't break something else. */
2702 | TREE_THIS_VOLATILE (array
));
2703 ret
= require_complete_type (loc
, rval
);
2704 protected_set_expr_location (ret
, loc
);
2706 ret
= non_lvalue_loc (loc
, ret
);
2711 tree ar
= default_conversion (array
);
2713 if (ar
== error_mark_node
)
2716 gcc_assert (TREE_CODE (TREE_TYPE (ar
)) == POINTER_TYPE
);
2717 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) != FUNCTION_TYPE
);
2719 ret
= build_indirect_ref (loc
, build_binary_op (loc
, PLUS_EXPR
, ar
,
2723 ret
= non_lvalue_loc (loc
, ret
);
2728 /* Build an external reference to identifier ID. FUN indicates
2729 whether this will be used for a function call. LOC is the source
2730 location of the identifier. This sets *TYPE to the type of the
2731 identifier, which is not the same as the type of the returned value
2732 for CONST_DECLs defined as enum constants. If the type of the
2733 identifier is not available, *TYPE is set to NULL. */
2735 build_external_ref (location_t loc
, tree id
, int fun
, tree
*type
)
2738 tree decl
= lookup_name (id
);
2740 /* In Objective-C, an instance variable (ivar) may be preferred to
2741 whatever lookup_name() found. */
2742 decl
= objc_lookup_ivar (decl
, id
);
2745 if (decl
&& decl
!= error_mark_node
)
2748 *type
= TREE_TYPE (ref
);
2751 /* Implicit function declaration. */
2752 ref
= implicitly_declare (loc
, id
);
2753 else if (decl
== error_mark_node
)
2754 /* Don't complain about something that's already been
2755 complained about. */
2756 return error_mark_node
;
2759 undeclared_variable (loc
, id
);
2760 return error_mark_node
;
2763 if (TREE_TYPE (ref
) == error_mark_node
)
2764 return error_mark_node
;
2766 if (TREE_DEPRECATED (ref
))
2767 warn_deprecated_use (ref
, NULL_TREE
);
2769 /* Recursive call does not count as usage. */
2770 if (ref
!= current_function_decl
)
2772 TREE_USED (ref
) = 1;
2775 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
2777 if (!in_sizeof
&& !in_typeof
)
2778 C_DECL_USED (ref
) = 1;
2779 else if (DECL_INITIAL (ref
) == 0
2780 && DECL_EXTERNAL (ref
)
2781 && !TREE_PUBLIC (ref
))
2782 record_maybe_used_decl (ref
);
2785 if (TREE_CODE (ref
) == CONST_DECL
)
2787 used_types_insert (TREE_TYPE (ref
));
2790 && TREE_CODE (TREE_TYPE (ref
)) == ENUMERAL_TYPE
2791 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref
)))
2793 warning_at (loc
, OPT_Wc___compat
,
2794 ("enum constant defined in struct or union "
2795 "is not visible in C++"));
2796 inform (DECL_SOURCE_LOCATION (ref
), "enum constant defined here");
2799 ref
= DECL_INITIAL (ref
);
2800 TREE_CONSTANT (ref
) = 1;
2802 else if (current_function_decl
!= 0
2803 && !DECL_FILE_SCOPE_P (current_function_decl
)
2804 && (VAR_OR_FUNCTION_DECL_P (ref
)
2805 || TREE_CODE (ref
) == PARM_DECL
))
2807 tree context
= decl_function_context (ref
);
2809 if (context
!= 0 && context
!= current_function_decl
)
2810 DECL_NONLOCAL (ref
) = 1;
2812 /* C99 6.7.4p3: An inline definition of a function with external
2813 linkage ... shall not contain a reference to an identifier with
2814 internal linkage. */
2815 else if (current_function_decl
!= 0
2816 && DECL_DECLARED_INLINE_P (current_function_decl
)
2817 && DECL_EXTERNAL (current_function_decl
)
2818 && VAR_OR_FUNCTION_DECL_P (ref
)
2819 && (!VAR_P (ref
) || TREE_STATIC (ref
))
2820 && ! TREE_PUBLIC (ref
)
2821 && DECL_CONTEXT (ref
) != current_function_decl
)
2822 record_inline_static (loc
, current_function_decl
, ref
,
2828 /* Record details of decls possibly used inside sizeof or typeof. */
2829 struct maybe_used_decl
2833 /* The level seen at (in_sizeof + in_typeof). */
2835 /* The next one at this level or above, or NULL. */
2836 struct maybe_used_decl
*next
;
2839 static struct maybe_used_decl
*maybe_used_decls
;
2841 /* Record that DECL, an undefined static function reference seen
2842 inside sizeof or typeof, might be used if the operand of sizeof is
2843 a VLA type or the operand of typeof is a variably modified
2847 record_maybe_used_decl (tree decl
)
2849 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
2851 t
->level
= in_sizeof
+ in_typeof
;
2852 t
->next
= maybe_used_decls
;
2853 maybe_used_decls
= t
;
2856 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2857 USED is false, just discard them. If it is true, mark them used
2858 (if no longer inside sizeof or typeof) or move them to the next
2859 level up (if still inside sizeof or typeof). */
2862 pop_maybe_used (bool used
)
2864 struct maybe_used_decl
*p
= maybe_used_decls
;
2865 int cur_level
= in_sizeof
+ in_typeof
;
2866 while (p
&& p
->level
> cur_level
)
2871 C_DECL_USED (p
->decl
) = 1;
2873 p
->level
= cur_level
;
2877 if (!used
|| cur_level
== 0)
2878 maybe_used_decls
= p
;
2881 /* Return the result of sizeof applied to EXPR. */
2884 c_expr_sizeof_expr (location_t loc
, struct c_expr expr
)
2887 if (expr
.value
== error_mark_node
)
2889 ret
.value
= error_mark_node
;
2890 ret
.original_code
= ERROR_MARK
;
2891 ret
.original_type
= NULL
;
2892 pop_maybe_used (false);
2896 bool expr_const_operands
= true;
2898 if (TREE_CODE (expr
.value
) == PARM_DECL
2899 && C_ARRAY_PARAMETER (expr
.value
))
2901 if (warning_at (loc
, OPT_Wsizeof_array_argument
,
2902 "%<sizeof%> on array function parameter %qE will "
2903 "return size of %qT", expr
.value
,
2904 expr
.original_type
))
2905 inform (DECL_SOURCE_LOCATION (expr
.value
), "declared here");
2907 tree folded_expr
= c_fully_fold (expr
.value
, require_constant_value
,
2908 &expr_const_operands
);
2909 ret
.value
= c_sizeof (loc
, TREE_TYPE (folded_expr
));
2910 c_last_sizeof_arg
= expr
.value
;
2911 ret
.original_code
= SIZEOF_EXPR
;
2912 ret
.original_type
= NULL
;
2913 if (c_vla_type_p (TREE_TYPE (folded_expr
)))
2915 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2916 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2917 folded_expr
, ret
.value
);
2918 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !expr_const_operands
;
2919 SET_EXPR_LOCATION (ret
.value
, loc
);
2921 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr
)));
2926 /* Return the result of sizeof applied to T, a structure for the type
2927 name passed to sizeof (rather than the type itself). LOC is the
2928 location of the original expression. */
2931 c_expr_sizeof_type (location_t loc
, struct c_type_name
*t
)
2935 tree type_expr
= NULL_TREE
;
2936 bool type_expr_const
= true;
2937 type
= groktypename (t
, &type_expr
, &type_expr_const
);
2938 ret
.value
= c_sizeof (loc
, type
);
2939 c_last_sizeof_arg
= type
;
2940 ret
.original_code
= SIZEOF_EXPR
;
2941 ret
.original_type
= NULL
;
2942 if ((type_expr
|| TREE_CODE (ret
.value
) == INTEGER_CST
)
2943 && c_vla_type_p (type
))
2945 /* If the type is a [*] array, it is a VLA but is represented as
2946 having a size of zero. In such a case we must ensure that
2947 the result of sizeof does not get folded to a constant by
2948 c_fully_fold, because if the size is evaluated the result is
2949 not constant and so constraints on zero or negative size
2950 arrays must not be applied when this sizeof call is inside
2951 another array declarator. */
2953 type_expr
= integer_zero_node
;
2954 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2955 type_expr
, ret
.value
);
2956 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !type_expr_const
;
2958 pop_maybe_used (type
!= error_mark_node
2959 ? C_TYPE_VARIABLE_SIZE (type
) : false);
2963 /* Build a function call to function FUNCTION with parameters PARAMS.
2964 The function call is at LOC.
2965 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2966 TREE_VALUE of each node is a parameter-expression.
2967 FUNCTION's data type may be a function type or a pointer-to-function. */
2970 build_function_call (location_t loc
, tree function
, tree params
)
2972 vec
<tree
, va_gc
> *v
;
2975 vec_alloc (v
, list_length (params
));
2976 for (; params
; params
= TREE_CHAIN (params
))
2977 v
->quick_push (TREE_VALUE (params
));
2978 ret
= c_build_function_call_vec (loc
, vNULL
, function
, v
, NULL
);
2983 /* Give a note about the location of the declaration of DECL. */
2986 inform_declaration (tree decl
)
2988 if (decl
&& (TREE_CODE (decl
) != FUNCTION_DECL
|| !DECL_IS_BUILTIN (decl
)))
2989 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
2992 /* Build a function call to function FUNCTION with parameters PARAMS.
2993 ORIGTYPES, if not NULL, is a vector of types; each element is
2994 either NULL or the original type of the corresponding element in
2995 PARAMS. The original type may differ from TREE_TYPE of the
2996 parameter for enums. FUNCTION's data type may be a function type
2997 or pointer-to-function. This function changes the elements of
3001 build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
3002 tree function
, vec
<tree
, va_gc
> *params
,
3003 vec
<tree
, va_gc
> *origtypes
)
3005 tree fntype
, fundecl
= 0;
3006 tree name
= NULL_TREE
, result
;
3012 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3013 STRIP_TYPE_NOPS (function
);
3015 /* Convert anything with function type to a pointer-to-function. */
3016 if (TREE_CODE (function
) == FUNCTION_DECL
)
3018 name
= DECL_NAME (function
);
3021 tm_malloc_replacement (function
);
3023 /* Atomic functions have type checking/casting already done. They are
3024 often rewritten and don't match the original parameter list. */
3025 if (name
&& !strncmp (IDENTIFIER_POINTER (name
), "__atomic_", 9))
3029 && is_cilkplus_reduce_builtin (function
))
3032 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
)
3033 function
= function_to_pointer_conversion (loc
, function
);
3035 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3036 expressions, like those used for ObjC messenger dispatches. */
3037 if (params
&& !params
->is_empty ())
3038 function
= objc_rewrite_function_call (function
, (*params
)[0]);
3040 function
= c_fully_fold (function
, false, NULL
);
3042 fntype
= TREE_TYPE (function
);
3044 if (TREE_CODE (fntype
) == ERROR_MARK
)
3045 return error_mark_node
;
3047 if (!(TREE_CODE (fntype
) == POINTER_TYPE
3048 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
3050 if (!flag_diagnostics_show_caret
)
3052 "called object %qE is not a function or function pointer",
3054 else if (DECL_P (function
))
3057 "called object %qD is not a function or function pointer",
3059 inform_declaration (function
);
3063 "called object is not a function or function pointer");
3064 return error_mark_node
;
3067 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
3068 current_function_returns_abnormally
= 1;
3070 /* fntype now gets the type of function pointed to. */
3071 fntype
= TREE_TYPE (fntype
);
3073 /* Convert the parameters to the types declared in the
3074 function prototype, or apply default promotions. */
3076 nargs
= convert_arguments (loc
, arg_loc
, TYPE_ARG_TYPES (fntype
), params
,
3077 origtypes
, function
, fundecl
);
3079 return error_mark_node
;
3081 /* Check that the function is called through a compatible prototype.
3082 If it is not, warn. */
3083 if (CONVERT_EXPR_P (function
)
3084 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
3085 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
3086 && !comptypes (fntype
, TREE_TYPE (tem
)))
3088 tree return_type
= TREE_TYPE (fntype
);
3090 /* This situation leads to run-time undefined behavior. We can't,
3091 therefore, simply error unless we can prove that all possible
3092 executions of the program must execute the code. */
3093 warning_at (loc
, 0, "function called through a non-compatible type");
3095 if (VOID_TYPE_P (return_type
)
3096 && TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
)
3098 "function with qualified void return type called");
3101 argarray
= vec_safe_address (params
);
3103 /* Check that arguments to builtin functions match the expectations. */
3105 && DECL_BUILT_IN (fundecl
)
3106 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
3107 && !check_builtin_function_arguments (loc
, arg_loc
, fundecl
, nargs
,
3109 return error_mark_node
;
3111 /* Check that the arguments to the function are valid. */
3112 check_function_arguments (loc
, fntype
, nargs
, argarray
);
3114 if (name
!= NULL_TREE
3115 && !strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10))
3117 if (require_constant_value
)
3119 fold_build_call_array_initializer_loc (loc
, TREE_TYPE (fntype
),
3120 function
, nargs
, argarray
);
3122 result
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
3123 function
, nargs
, argarray
);
3124 if (TREE_CODE (result
) == NOP_EXPR
3125 && TREE_CODE (TREE_OPERAND (result
, 0)) == INTEGER_CST
)
3126 STRIP_TYPE_NOPS (result
);
3129 result
= build_call_array_loc (loc
, TREE_TYPE (fntype
),
3130 function
, nargs
, argarray
);
3132 /* In this improbable scenario, a nested function returns a VM type.
3133 Create a TARGET_EXPR so that the call always has a LHS, much as
3134 what the C++ FE does for functions returning non-PODs. */
3135 if (variably_modified_type_p (TREE_TYPE (fntype
), NULL_TREE
))
3137 tree tmp
= create_tmp_var_raw (TREE_TYPE (fntype
));
3138 result
= build4 (TARGET_EXPR
, TREE_TYPE (fntype
), tmp
, result
,
3139 NULL_TREE
, NULL_TREE
);
3142 if (VOID_TYPE_P (TREE_TYPE (result
)))
3144 if (TYPE_QUALS (TREE_TYPE (result
)) != TYPE_UNQUALIFIED
)
3146 "function with qualified void return type called");
3149 return require_complete_type (loc
, result
);
3152 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3155 c_build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
3156 tree function
, vec
<tree
, va_gc
> *params
,
3157 vec
<tree
, va_gc
> *origtypes
)
3159 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3160 STRIP_TYPE_NOPS (function
);
3162 /* Convert anything with function type to a pointer-to-function. */
3163 if (TREE_CODE (function
) == FUNCTION_DECL
)
3165 /* Implement type-directed function overloading for builtins.
3166 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3167 handle all the type checking. The result is a complete expression
3168 that implements this function call. */
3169 tree tem
= resolve_overloaded_builtin (loc
, function
, params
);
3173 return build_function_call_vec (loc
, arg_loc
, function
, params
, origtypes
);
3176 /* Convert the argument expressions in the vector VALUES
3177 to the types in the list TYPELIST.
3179 If TYPELIST is exhausted, or when an element has NULL as its type,
3180 perform the default conversions.
3182 ORIGTYPES is the original types of the expressions in VALUES. This
3183 holds the type of enum values which have been converted to integral
3184 types. It may be NULL.
3186 FUNCTION is a tree for the called function. It is used only for
3187 error messages, where it is formatted with %qE.
3189 This is also where warnings about wrong number of args are generated.
3191 ARG_LOC are locations of function arguments (if any).
3193 Returns the actual number of arguments processed (which may be less
3194 than the length of VALUES in some error situations), or -1 on
3198 convert_arguments (location_t loc
, vec
<location_t
> arg_loc
, tree typelist
,
3199 vec
<tree
, va_gc
> *values
, vec
<tree
, va_gc
> *origtypes
,
3200 tree function
, tree fundecl
)
3203 unsigned int parmnum
;
3204 bool error_args
= false;
3205 const bool type_generic
= fundecl
3206 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl
)));
3207 bool type_generic_remove_excess_precision
= false;
3208 bool type_generic_overflow_p
= false;
3211 /* Change pointer to function to the function itself for
3213 if (TREE_CODE (function
) == ADDR_EXPR
3214 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
3215 function
= TREE_OPERAND (function
, 0);
3217 /* Handle an ObjC selector specially for diagnostics. */
3218 selector
= objc_message_selector ();
3220 /* For type-generic built-in functions, determine whether excess
3221 precision should be removed (classification) or not
3224 && DECL_BUILT_IN (fundecl
)
3225 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
)
3227 switch (DECL_FUNCTION_CODE (fundecl
))
3229 case BUILT_IN_ISFINITE
:
3230 case BUILT_IN_ISINF
:
3231 case BUILT_IN_ISINF_SIGN
:
3232 case BUILT_IN_ISNAN
:
3233 case BUILT_IN_ISNORMAL
:
3234 case BUILT_IN_FPCLASSIFY
:
3235 type_generic_remove_excess_precision
= true;
3238 case BUILT_IN_ADD_OVERFLOW_P
:
3239 case BUILT_IN_SUB_OVERFLOW_P
:
3240 case BUILT_IN_MUL_OVERFLOW_P
:
3241 /* The last argument of these type-generic builtins
3242 should not be promoted. */
3243 type_generic_overflow_p
= true;
3250 if (flag_cilkplus
&& fundecl
&& is_cilkplus_reduce_builtin (fundecl
))
3251 return vec_safe_length (values
);
3253 /* Scan the given expressions and types, producing individual
3254 converted arguments. */
3256 for (typetail
= typelist
, parmnum
= 0;
3257 values
&& values
->iterate (parmnum
, &val
);
3260 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3261 tree valtype
= TREE_TYPE (val
);
3262 tree rname
= function
;
3263 int argnum
= parmnum
+ 1;
3264 const char *invalid_func_diag
;
3265 bool excess_precision
= false;
3268 /* Some __atomic_* builtins have additional hidden argument at
3271 = !arg_loc
.is_empty () && values
->length () == arg_loc
.length ()
3272 ? expansion_point_location_if_in_system_header (arg_loc
[parmnum
])
3275 if (type
== void_type_node
)
3278 error_at (loc
, "too many arguments to method %qE", selector
);
3280 error_at (loc
, "too many arguments to function %qE", function
);
3281 inform_declaration (fundecl
);
3282 return error_args
? -1 : (int) parmnum
;
3285 if (selector
&& argnum
> 2)
3291 npc
= null_pointer_constant_p (val
);
3293 /* If there is excess precision and a prototype, convert once to
3294 the required type rather than converting via the semantic
3295 type. Likewise without a prototype a float value represented
3296 as long double should be converted once to double. But for
3297 type-generic classification functions excess precision must
3299 if (TREE_CODE (val
) == EXCESS_PRECISION_EXPR
3300 && (type
|| !type_generic
|| !type_generic_remove_excess_precision
))
3302 val
= TREE_OPERAND (val
, 0);
3303 excess_precision
= true;
3305 val
= c_fully_fold (val
, false, NULL
);
3306 STRIP_TYPE_NOPS (val
);
3308 val
= require_complete_type (ploc
, val
);
3310 /* Some floating-point arguments must be promoted to double when
3311 no type is specified by a prototype. This applies to
3312 arguments of type float, and to architecture-specific types
3313 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3314 bool promote_float_arg
= false;
3315 if (type
== NULL_TREE
3316 && TREE_CODE (valtype
) == REAL_TYPE
3317 && (TYPE_PRECISION (valtype
)
3318 <= TYPE_PRECISION (double_type_node
))
3319 && TYPE_MAIN_VARIANT (valtype
) != double_type_node
3320 && TYPE_MAIN_VARIANT (valtype
) != long_double_type_node
3321 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype
)))
3323 /* Promote this argument, unless it has a _FloatN or
3325 promote_float_arg
= true;
3326 for (int i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
3327 if (TYPE_MAIN_VARIANT (valtype
) == FLOATN_NX_TYPE_NODE (i
))
3329 promote_float_arg
= false;
3336 /* Formal parm type is specified by a function prototype. */
3338 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3340 error_at (ploc
, "type of formal parameter %d is incomplete",
3348 /* Optionally warn about conversions that
3349 differ from the default conversions. */
3350 if (warn_traditional_conversion
|| warn_traditional
)
3352 unsigned int formal_prec
= TYPE_PRECISION (type
);
3354 if (INTEGRAL_TYPE_P (type
)
3355 && TREE_CODE (valtype
) == REAL_TYPE
)
3356 warning_at (ploc
, OPT_Wtraditional_conversion
,
3357 "passing argument %d of %qE as integer rather "
3358 "than floating due to prototype",
3360 if (INTEGRAL_TYPE_P (type
)
3361 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3362 warning_at (ploc
, OPT_Wtraditional_conversion
,
3363 "passing argument %d of %qE as integer rather "
3364 "than complex due to prototype",
3366 else if (TREE_CODE (type
) == COMPLEX_TYPE
3367 && TREE_CODE (valtype
) == REAL_TYPE
)
3368 warning_at (ploc
, OPT_Wtraditional_conversion
,
3369 "passing argument %d of %qE as complex rather "
3370 "than floating due to prototype",
3372 else if (TREE_CODE (type
) == REAL_TYPE
3373 && INTEGRAL_TYPE_P (valtype
))
3374 warning_at (ploc
, OPT_Wtraditional_conversion
,
3375 "passing argument %d of %qE as floating rather "
3376 "than integer due to prototype",
3378 else if (TREE_CODE (type
) == COMPLEX_TYPE
3379 && INTEGRAL_TYPE_P (valtype
))
3380 warning_at (ploc
, OPT_Wtraditional_conversion
,
3381 "passing argument %d of %qE as complex rather "
3382 "than integer due to prototype",
3384 else if (TREE_CODE (type
) == REAL_TYPE
3385 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3386 warning_at (ploc
, OPT_Wtraditional_conversion
,
3387 "passing argument %d of %qE as floating rather "
3388 "than complex due to prototype",
3390 /* ??? At some point, messages should be written about
3391 conversions between complex types, but that's too messy
3393 else if (TREE_CODE (type
) == REAL_TYPE
3394 && TREE_CODE (valtype
) == REAL_TYPE
)
3396 /* Warn if any argument is passed as `float',
3397 since without a prototype it would be `double'. */
3398 if (formal_prec
== TYPE_PRECISION (float_type_node
)
3399 && type
!= dfloat32_type_node
)
3400 warning_at (ploc
, 0,
3401 "passing argument %d of %qE as %<float%> "
3402 "rather than %<double%> due to prototype",
3405 /* Warn if mismatch between argument and prototype
3406 for decimal float types. Warn of conversions with
3407 binary float types and of precision narrowing due to
3409 else if (type
!= valtype
3410 && (type
== dfloat32_type_node
3411 || type
== dfloat64_type_node
3412 || type
== dfloat128_type_node
3413 || valtype
== dfloat32_type_node
3414 || valtype
== dfloat64_type_node
3415 || valtype
== dfloat128_type_node
)
3417 <= TYPE_PRECISION (valtype
)
3418 || (type
== dfloat128_type_node
3420 != dfloat64_type_node
3422 != dfloat32_type_node
)))
3423 || (type
== dfloat64_type_node
3425 != dfloat32_type_node
))))
3426 warning_at (ploc
, 0,
3427 "passing argument %d of %qE as %qT "
3428 "rather than %qT due to prototype",
3429 argnum
, rname
, type
, valtype
);
3432 /* Detect integer changing in width or signedness.
3433 These warnings are only activated with
3434 -Wtraditional-conversion, not with -Wtraditional. */
3435 else if (warn_traditional_conversion
&& INTEGRAL_TYPE_P (type
)
3436 && INTEGRAL_TYPE_P (valtype
))
3438 tree would_have_been
= default_conversion (val
);
3439 tree type1
= TREE_TYPE (would_have_been
);
3441 if (TREE_CODE (type
) == ENUMERAL_TYPE
3442 && (TYPE_MAIN_VARIANT (type
)
3443 == TYPE_MAIN_VARIANT (valtype
)))
3444 /* No warning if function asks for enum
3445 and the actual arg is that enum type. */
3447 else if (formal_prec
!= TYPE_PRECISION (type1
))
3448 warning_at (ploc
, OPT_Wtraditional_conversion
,
3449 "passing argument %d of %qE "
3450 "with different width due to prototype",
3452 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
3454 /* Don't complain if the formal parameter type
3455 is an enum, because we can't tell now whether
3456 the value was an enum--even the same enum. */
3457 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3459 else if (TREE_CODE (val
) == INTEGER_CST
3460 && int_fits_type_p (val
, type
))
3461 /* Change in signedness doesn't matter
3462 if a constant value is unaffected. */
3464 /* If the value is extended from a narrower
3465 unsigned type, it doesn't matter whether we
3466 pass it as signed or unsigned; the value
3467 certainly is the same either way. */
3468 else if (TYPE_PRECISION (valtype
) < TYPE_PRECISION (type
)
3469 && TYPE_UNSIGNED (valtype
))
3471 else if (TYPE_UNSIGNED (type
))
3472 warning_at (ploc
, OPT_Wtraditional_conversion
,
3473 "passing argument %d of %qE "
3474 "as unsigned due to prototype",
3477 warning_at (ploc
, OPT_Wtraditional_conversion
,
3478 "passing argument %d of %qE "
3479 "as signed due to prototype",
3484 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3485 sake of better warnings from convert_and_check. */
3486 if (excess_precision
)
3487 val
= build1 (EXCESS_PRECISION_EXPR
, valtype
, val
);
3488 origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3489 parmval
= convert_for_assignment (loc
, ploc
, type
,
3490 val
, origtype
, ic_argpass
,
3491 npc
, fundecl
, function
,
3494 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
3495 && INTEGRAL_TYPE_P (type
)
3496 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3497 parmval
= default_conversion (parmval
);
3500 else if (promote_float_arg
)
3506 /* Convert `float' to `double'. */
3507 if (warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
3508 warning_at (ploc
, OPT_Wdouble_promotion
,
3509 "implicit conversion from %qT to %qT when passing "
3510 "argument to function",
3511 valtype
, double_type_node
);
3512 parmval
= convert (double_type_node
, val
);
3515 else if ((excess_precision
&& !type_generic
)
3516 || (type_generic_overflow_p
&& parmnum
== 2))
3517 /* A "double" argument with excess precision being passed
3518 without a prototype or in variable arguments.
3519 The last argument of __builtin_*_overflow_p should not be
3521 parmval
= convert (valtype
, val
);
3522 else if ((invalid_func_diag
=
3523 targetm
.calls
.invalid_arg_for_unprototyped_fn (typelist
, fundecl
, val
)))
3525 error (invalid_func_diag
);
3528 else if (TREE_CODE (val
) == ADDR_EXPR
&& reject_gcc_builtin (val
))
3533 /* Convert `short' and `char' to full-size `int'. */
3534 parmval
= default_conversion (val
);
3536 (*values
)[parmnum
] = parmval
;
3537 if (parmval
== error_mark_node
)
3541 typetail
= TREE_CHAIN (typetail
);
3544 gcc_assert (parmnum
== vec_safe_length (values
));
3546 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
3548 error_at (loc
, "too few arguments to function %qE", function
);
3549 inform_declaration (fundecl
);
3553 return error_args
? -1 : (int) parmnum
;
3556 /* This is the entry point used by the parser to build unary operators
3557 in the input. CODE, a tree_code, specifies the unary operator, and
3558 ARG is the operand. For unary plus, the C parser currently uses
3559 CONVERT_EXPR for code.
3561 LOC is the location to use for the tree generated.
3565 parser_build_unary_op (location_t loc
, enum tree_code code
, struct c_expr arg
)
3567 struct c_expr result
;
3569 result
.original_code
= code
;
3570 result
.original_type
= NULL
;
3572 if (reject_gcc_builtin (arg
.value
))
3574 result
.value
= error_mark_node
;
3578 result
.value
= build_unary_op (loc
, code
, arg
.value
, false);
3580 if (TREE_OVERFLOW_P (result
.value
) && !TREE_OVERFLOW_P (arg
.value
))
3581 overflow_warning (loc
, result
.value
);
3584 /* We are typically called when parsing a prefix token at LOC acting on
3585 ARG. Reflect this by updating the source range of the result to
3586 start at LOC and end at the end of ARG. */
3587 set_c_expr_source_range (&result
,
3588 loc
, arg
.get_finish ());
3593 /* This is the entry point used by the parser to build binary operators
3594 in the input. CODE, a tree_code, specifies the binary operator, and
3595 ARG1 and ARG2 are the operands. In addition to constructing the
3596 expression, we check for operands that were written with other binary
3597 operators in a way that is likely to confuse the user.
3599 LOCATION is the location of the binary operator. */
3602 parser_build_binary_op (location_t location
, enum tree_code code
,
3603 struct c_expr arg1
, struct c_expr arg2
)
3605 struct c_expr result
;
3607 enum tree_code code1
= arg1
.original_code
;
3608 enum tree_code code2
= arg2
.original_code
;
3609 tree type1
= (arg1
.original_type
3610 ? arg1
.original_type
3611 : TREE_TYPE (arg1
.value
));
3612 tree type2
= (arg2
.original_type
3613 ? arg2
.original_type
3614 : TREE_TYPE (arg2
.value
));
3616 result
.value
= build_binary_op (location
, code
,
3617 arg1
.value
, arg2
.value
, 1);
3618 result
.original_code
= code
;
3619 result
.original_type
= NULL
;
3621 if (TREE_CODE (result
.value
) == ERROR_MARK
)
3623 set_c_expr_source_range (&result
,
3625 arg2
.get_finish ());
3629 if (location
!= UNKNOWN_LOCATION
)
3630 protected_set_expr_location (result
.value
, location
);
3632 set_c_expr_source_range (&result
,
3634 arg2
.get_finish ());
3636 /* Check for cases such as x+y<<z which users are likely
3638 if (warn_parentheses
)
3639 warn_about_parentheses (location
, code
, code1
, arg1
.value
, code2
,
3642 if (warn_logical_op
)
3643 warn_logical_operator (location
, code
, TREE_TYPE (result
.value
),
3644 code1
, arg1
.value
, code2
, arg2
.value
);
3646 if (warn_tautological_compare
)
3648 tree lhs
= arg1
.value
;
3649 tree rhs
= arg2
.value
;
3650 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
3652 if (C_MAYBE_CONST_EXPR_PRE (lhs
) != NULL_TREE
3653 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs
)))
3656 lhs
= C_MAYBE_CONST_EXPR_EXPR (lhs
);
3658 if (TREE_CODE (rhs
) == C_MAYBE_CONST_EXPR
)
3660 if (C_MAYBE_CONST_EXPR_PRE (rhs
) != NULL_TREE
3661 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs
)))
3664 rhs
= C_MAYBE_CONST_EXPR_EXPR (rhs
);
3666 if (lhs
!= NULL_TREE
&& rhs
!= NULL_TREE
)
3667 warn_tautological_cmp (location
, code
, lhs
, rhs
);
3670 if (warn_logical_not_paren
3671 && TREE_CODE_CLASS (code
) == tcc_comparison
3672 && code1
== TRUTH_NOT_EXPR
3673 && code2
!= TRUTH_NOT_EXPR
3674 /* Avoid warning for !!x == y. */
3675 && (TREE_CODE (arg1
.value
) != NE_EXPR
3676 || !integer_zerop (TREE_OPERAND (arg1
.value
, 1))))
3678 /* Avoid warning for !b == y where b has _Bool type. */
3679 tree t
= integer_zero_node
;
3680 if (TREE_CODE (arg1
.value
) == EQ_EXPR
3681 && integer_zerop (TREE_OPERAND (arg1
.value
, 1))
3682 && TREE_TYPE (TREE_OPERAND (arg1
.value
, 0)) == integer_type_node
)
3684 t
= TREE_OPERAND (arg1
.value
, 0);
3687 if (TREE_TYPE (t
) != integer_type_node
)
3689 if (TREE_CODE (t
) == C_MAYBE_CONST_EXPR
)
3690 t
= C_MAYBE_CONST_EXPR_EXPR (t
);
3691 else if (CONVERT_EXPR_P (t
))
3692 t
= TREE_OPERAND (t
, 0);
3698 if (TREE_CODE (TREE_TYPE (t
)) != BOOLEAN_TYPE
)
3699 warn_logical_not_parentheses (location
, code
, arg1
.value
, arg2
.value
);
3702 /* Warn about comparisons against string literals, with the exception
3703 of testing for equality or inequality of a string literal with NULL. */
3704 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3706 if ((code1
== STRING_CST
3707 && !integer_zerop (tree_strip_nop_conversions (arg2
.value
)))
3708 || (code2
== STRING_CST
3709 && !integer_zerop (tree_strip_nop_conversions (arg1
.value
))))
3710 warning_at (location
, OPT_Waddress
,
3711 "comparison with string literal results in unspecified behavior");
3713 else if (TREE_CODE_CLASS (code
) == tcc_comparison
3714 && (code1
== STRING_CST
|| code2
== STRING_CST
))
3715 warning_at (location
, OPT_Waddress
,
3716 "comparison with string literal results in unspecified behavior");
3718 if (TREE_OVERFLOW_P (result
.value
)
3719 && !TREE_OVERFLOW_P (arg1
.value
)
3720 && !TREE_OVERFLOW_P (arg2
.value
))
3721 overflow_warning (location
, result
.value
);
3723 /* Warn about comparisons of different enum types. */
3724 if (warn_enum_compare
3725 && TREE_CODE_CLASS (code
) == tcc_comparison
3726 && TREE_CODE (type1
) == ENUMERAL_TYPE
3727 && TREE_CODE (type2
) == ENUMERAL_TYPE
3728 && TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
3729 warning_at (location
, OPT_Wenum_compare
,
3730 "comparison between %qT and %qT",
3736 /* Return a tree for the difference of pointers OP0 and OP1.
3737 The resulting tree has type int. */
3740 pointer_diff (location_t loc
, tree op0
, tree op1
)
3742 tree restype
= ptrdiff_type_node
;
3743 tree result
, inttype
;
3745 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0
)));
3746 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1
)));
3747 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3748 tree orig_op1
= op1
;
3750 /* If the operands point into different address spaces, we need to
3751 explicitly convert them to pointers into the common address space
3752 before we can subtract the numerical address values. */
3755 addr_space_t as_common
;
3758 /* Determine the common superset address space. This is guaranteed
3759 to exist because the caller verified that comp_target_types
3760 returned non-zero. */
3761 if (!addr_space_superset (as0
, as1
, &as_common
))
3764 common_type
= common_pointer_type (TREE_TYPE (op0
), TREE_TYPE (op1
));
3765 op0
= convert (common_type
, op0
);
3766 op1
= convert (common_type
, op1
);
3769 /* Determine integer type to perform computations in. This will usually
3770 be the same as the result type (ptrdiff_t), but may need to be a wider
3771 type if pointers for the address space are wider than ptrdiff_t. */
3772 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
3773 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
3777 if (TREE_CODE (target_type
) == VOID_TYPE
)
3778 pedwarn (loc
, OPT_Wpointer_arith
,
3779 "pointer of type %<void *%> used in subtraction");
3780 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3781 pedwarn (loc
, OPT_Wpointer_arith
,
3782 "pointer to a function used in subtraction");
3784 /* First do the subtraction as integers;
3785 then drop through to build the divide operator.
3786 Do not do default conversions on the minus operator
3787 in case restype is a short type. */
3789 op0
= build_binary_op (loc
,
3790 MINUS_EXPR
, convert (inttype
, op0
),
3791 convert (inttype
, op1
), 0);
3792 /* This generates an error if op1 is pointer to incomplete type. */
3793 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
3794 error_at (loc
, "arithmetic on pointer to an incomplete type");
3796 op1
= c_size_in_bytes (target_type
);
3798 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1
)))
3799 error_at (loc
, "arithmetic on pointer to an empty aggregate");
3801 /* Divide by the size, in easiest possible way. */
3802 result
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, inttype
,
3803 op0
, convert (inttype
, op1
));
3805 /* Convert to final result type if necessary. */
3806 return convert (restype
, result
);
3809 /* Expand atomic compound assignments into an appropriate sequence as
3810 specified by the C11 standard section 6.5.16.2.
3816 This sequence is used for all types for which these operations are
3819 In addition, built-in versions of the 'fe' prefixed routines may
3820 need to be invoked for floating point (real, complex or vector) when
3821 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3831 __atomic_load (addr, &old, SEQ_CST);
3832 feholdexcept (&fenv);
3834 newval = old op val;
3835 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3838 feclearexcept (FE_ALL_EXCEPT);
3841 feupdateenv (&fenv);
3843 The compiler will issue the __atomic_fetch_* built-in when possible,
3844 otherwise it will generate the generic form of the atomic operations.
3845 This requires temp(s) and has their address taken. The atomic processing
3846 is smart enough to figure out when the size of an object can utilize
3847 a lock-free version, and convert the built-in call to the appropriate
3848 lock-free routine. The optimizers will then dispose of any temps that
3849 are no longer required, and lock-free implementations are utilized as
3850 long as there is target support for the required size.
3852 If the operator is NOP_EXPR, then this is a simple assignment, and
3853 an __atomic_store is issued to perform the assignment rather than
3856 /* Build an atomic assignment at LOC, expanding into the proper
3857 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3858 the result of the operation, unless RETURN_OLD_P, in which case
3859 return the old value of LHS (this is only for postincrement and
3863 build_atomic_assign (location_t loc
, tree lhs
, enum tree_code modifycode
,
3864 tree rhs
, bool return_old_p
)
3866 tree fndecl
, func_call
;
3867 vec
<tree
, va_gc
> *params
;
3868 tree val
, nonatomic_lhs_type
, nonatomic_rhs_type
, newval
, newval_addr
;
3871 tree stmt
, goto_stmt
;
3872 tree loop_label
, loop_decl
, done_label
, done_decl
;
3874 tree lhs_type
= TREE_TYPE (lhs
);
3875 tree lhs_addr
= build_unary_op (loc
, ADDR_EXPR
, lhs
, false);
3876 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
3877 tree rhs_type
= TREE_TYPE (rhs
);
3879 gcc_assert (TYPE_ATOMIC (lhs_type
));
3882 gcc_assert (modifycode
== PLUS_EXPR
|| modifycode
== MINUS_EXPR
);
3884 /* Allocate enough vector items for a compare_exchange. */
3885 vec_alloc (params
, 6);
3887 /* Create a compound statement to hold the sequence of statements
3889 compound_stmt
= c_begin_compound_stmt (false);
3891 /* Fold the RHS if it hasn't already been folded. */
3892 if (modifycode
!= NOP_EXPR
)
3893 rhs
= c_fully_fold (rhs
, false, NULL
);
3895 /* Remove the qualifiers for the rest of the expressions and create
3896 the VAL temp variable to hold the RHS. */
3897 nonatomic_lhs_type
= build_qualified_type (lhs_type
, TYPE_UNQUALIFIED
);
3898 nonatomic_rhs_type
= build_qualified_type (rhs_type
, TYPE_UNQUALIFIED
);
3899 val
= create_tmp_var_raw (nonatomic_rhs_type
);
3900 TREE_ADDRESSABLE (val
) = 1;
3901 TREE_NO_WARNING (val
) = 1;
3902 rhs
= build4 (TARGET_EXPR
, nonatomic_rhs_type
, val
, rhs
, NULL_TREE
,
3904 SET_EXPR_LOCATION (rhs
, loc
);
3907 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3909 if (modifycode
== NOP_EXPR
)
3911 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3912 rhs
= build_unary_op (loc
, ADDR_EXPR
, val
, false);
3913 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
3914 params
->quick_push (lhs_addr
);
3915 params
->quick_push (rhs
);
3916 params
->quick_push (seq_cst
);
3917 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3918 add_stmt (func_call
);
3920 /* Finish the compound statement. */
3921 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3923 /* VAL is the value which was stored, return a COMPOUND_STMT of
3924 the statement and that value. */
3925 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, val
);
3928 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3929 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3930 isn't applicable for such builtins. ??? Do we want to handle enums? */
3931 if ((TREE_CODE (lhs_type
) == INTEGER_TYPE
|| POINTER_TYPE_P (lhs_type
))
3932 && TREE_CODE (rhs_type
) == INTEGER_TYPE
)
3934 built_in_function fncode
;
3938 case POINTER_PLUS_EXPR
:
3939 fncode
= (return_old_p
3940 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3941 : BUILT_IN_ATOMIC_ADD_FETCH_N
);
3944 fncode
= (return_old_p
3945 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3946 : BUILT_IN_ATOMIC_SUB_FETCH_N
);
3949 fncode
= (return_old_p
3950 ? BUILT_IN_ATOMIC_FETCH_AND_N
3951 : BUILT_IN_ATOMIC_AND_FETCH_N
);
3954 fncode
= (return_old_p
3955 ? BUILT_IN_ATOMIC_FETCH_OR_N
3956 : BUILT_IN_ATOMIC_OR_FETCH_N
);
3959 fncode
= (return_old_p
3960 ? BUILT_IN_ATOMIC_FETCH_XOR_N
3961 : BUILT_IN_ATOMIC_XOR_FETCH_N
);
3967 /* We can only use "_1" through "_16" variants of the atomic fetch
3969 unsigned HOST_WIDE_INT size
= tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type
));
3970 if (size
!= 1 && size
!= 2 && size
!= 4 && size
!= 8 && size
!= 16)
3973 /* If this is a pointer type, we need to multiply by the size of
3974 the pointer target type. */
3975 if (POINTER_TYPE_P (lhs_type
))
3977 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type
))
3978 /* ??? This would introduce -Wdiscarded-qualifiers
3979 warning: __atomic_fetch_* expect volatile void *
3980 type as the first argument. (Assignments between
3981 atomic and non-atomic objects are OK.) */
3982 || TYPE_RESTRICT (lhs_type
))
3984 tree sz
= TYPE_SIZE_UNIT (TREE_TYPE (lhs_type
));
3985 rhs
= fold_build2_loc (loc
, MULT_EXPR
, ptrdiff_type_node
,
3986 convert (ptrdiff_type_node
, rhs
),
3987 convert (ptrdiff_type_node
, sz
));
3990 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
3991 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
3992 fndecl
= builtin_decl_explicit (fncode
);
3993 params
->quick_push (lhs_addr
);
3994 params
->quick_push (rhs
);
3995 params
->quick_push (seq_cst
);
3996 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3998 newval
= create_tmp_var_raw (nonatomic_lhs_type
);
3999 TREE_ADDRESSABLE (newval
) = 1;
4000 TREE_NO_WARNING (newval
) = 1;
4001 rhs
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, newval
, func_call
,
4002 NULL_TREE
, NULL_TREE
);
4003 SET_EXPR_LOCATION (rhs
, loc
);
4006 /* Finish the compound statement. */
4007 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
4009 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4010 the statement and that value. */
4011 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, newval
);
4015 /* Create the variables and labels required for the op= form. */
4016 old
= create_tmp_var_raw (nonatomic_lhs_type
);
4017 old_addr
= build_unary_op (loc
, ADDR_EXPR
, old
, false);
4018 TREE_ADDRESSABLE (old
) = 1;
4019 TREE_NO_WARNING (old
) = 1;
4021 newval
= create_tmp_var_raw (nonatomic_lhs_type
);
4022 newval_addr
= build_unary_op (loc
, ADDR_EXPR
, newval
, false);
4023 TREE_ADDRESSABLE (newval
) = 1;
4024 TREE_NO_WARNING (newval
) = 1;
4026 loop_decl
= create_artificial_label (loc
);
4027 loop_label
= build1 (LABEL_EXPR
, void_type_node
, loop_decl
);
4029 done_decl
= create_artificial_label (loc
);
4030 done_label
= build1 (LABEL_EXPR
, void_type_node
, done_decl
);
4032 /* __atomic_load (addr, &old, SEQ_CST). */
4033 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
4034 params
->quick_push (lhs_addr
);
4035 params
->quick_push (old_addr
);
4036 params
->quick_push (seq_cst
);
4037 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4038 old
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, old
, func_call
, NULL_TREE
,
4041 params
->truncate (0);
4043 /* Create the expressions for floating-point environment
4044 manipulation, if required. */
4045 bool need_fenv
= (flag_trapping_math
4046 && (FLOAT_TYPE_P (lhs_type
) || FLOAT_TYPE_P (rhs_type
)));
4047 tree hold_call
= NULL_TREE
, clear_call
= NULL_TREE
, update_call
= NULL_TREE
;
4049 targetm
.atomic_assign_expand_fenv (&hold_call
, &clear_call
, &update_call
);
4052 add_stmt (hold_call
);
4055 add_stmt (loop_label
);
4057 /* newval = old + val; */
4058 rhs
= build_binary_op (loc
, modifycode
, old
, val
, 1);
4059 rhs
= c_fully_fold (rhs
, false, NULL
);
4060 rhs
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, nonatomic_lhs_type
,
4061 rhs
, NULL_TREE
, ic_assign
, false, NULL_TREE
,
4063 if (rhs
!= error_mark_node
)
4065 rhs
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, newval
, rhs
, NULL_TREE
,
4067 SET_EXPR_LOCATION (rhs
, loc
);
4071 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4073 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE
);
4074 params
->quick_push (lhs_addr
);
4075 params
->quick_push (old_addr
);
4076 params
->quick_push (newval_addr
);
4077 params
->quick_push (integer_zero_node
);
4078 params
->quick_push (seq_cst
);
4079 params
->quick_push (seq_cst
);
4080 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4082 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, done_decl
);
4083 SET_EXPR_LOCATION (goto_stmt
, loc
);
4085 stmt
= build3 (COND_EXPR
, void_type_node
, func_call
, goto_stmt
, NULL_TREE
);
4086 SET_EXPR_LOCATION (stmt
, loc
);
4090 add_stmt (clear_call
);
4093 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, loop_decl
);
4094 SET_EXPR_LOCATION (goto_stmt
, loc
);
4095 add_stmt (goto_stmt
);
4098 add_stmt (done_label
);
4101 add_stmt (update_call
);
4103 /* Finish the compound statement. */
4104 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
4106 /* NEWVAL is the value that was successfully stored, return a
4107 COMPOUND_EXPR of the statement and the appropriate value. */
4108 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
,
4109 return_old_p
? old
: newval
);
4112 /* Construct and perhaps optimize a tree representation
4113 for a unary operation. CODE, a tree_code, specifies the operation
4114 and XARG is the operand.
4115 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4116 promotions (such as from short to int).
4117 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4118 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4121 LOCATION is the location of the operator. */
4124 build_unary_op (location_t location
, enum tree_code code
, tree xarg
,
4127 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4130 enum tree_code typecode
;
4132 tree ret
= error_mark_node
;
4133 tree eptype
= NULL_TREE
;
4134 const char *invalid_op_diag
;
4137 int_operands
= EXPR_INT_CONST_OPERANDS (xarg
);
4139 arg
= remove_c_maybe_const_expr (arg
);
4141 if (code
!= ADDR_EXPR
)
4142 arg
= require_complete_type (location
, arg
);
4144 typecode
= TREE_CODE (TREE_TYPE (arg
));
4145 if (typecode
== ERROR_MARK
)
4146 return error_mark_node
;
4147 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
4148 typecode
= INTEGER_TYPE
;
4150 if ((invalid_op_diag
4151 = targetm
.invalid_unary_op (code
, TREE_TYPE (xarg
))))
4153 error_at (location
, invalid_op_diag
);
4154 return error_mark_node
;
4157 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
4159 eptype
= TREE_TYPE (arg
);
4160 arg
= TREE_OPERAND (arg
, 0);
4166 /* This is used for unary plus, because a CONVERT_EXPR
4167 is enough to prevent anybody from looking inside for
4168 associativity, but won't generate any code. */
4169 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4170 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
4171 || typecode
== VECTOR_TYPE
))
4173 error_at (location
, "wrong type argument to unary plus");
4174 return error_mark_node
;
4176 else if (!noconvert
)
4177 arg
= default_conversion (arg
);
4178 arg
= non_lvalue_loc (location
, arg
);
4182 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4183 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
4184 || typecode
== VECTOR_TYPE
))
4186 error_at (location
, "wrong type argument to unary minus");
4187 return error_mark_node
;
4189 else if (!noconvert
)
4190 arg
= default_conversion (arg
);
4194 /* ~ works on integer types and non float vectors. */
4195 if (typecode
== INTEGER_TYPE
4196 || (typecode
== VECTOR_TYPE
4197 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg
))))
4201 /* Warn if the expression has boolean value. */
4202 while (TREE_CODE (e
) == COMPOUND_EXPR
)
4203 e
= TREE_OPERAND (e
, 1);
4205 if ((TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
4206 || truth_value_p (TREE_CODE (e
)))
4207 && warning_at (location
, OPT_Wbool_operation
,
4208 "%<~%> on a boolean expression"))
4210 gcc_rich_location
richloc (location
);
4211 richloc
.add_fixit_insert_before (location
, "!");
4212 inform_at_rich_loc (&richloc
, "did you mean to use logical "
4216 arg
= default_conversion (arg
);
4218 else if (typecode
== COMPLEX_TYPE
)
4221 pedwarn (location
, OPT_Wpedantic
,
4222 "ISO C does not support %<~%> for complex conjugation");
4224 arg
= default_conversion (arg
);
4228 error_at (location
, "wrong type argument to bit-complement");
4229 return error_mark_node
;
4234 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
4236 error_at (location
, "wrong type argument to abs");
4237 return error_mark_node
;
4239 else if (!noconvert
)
4240 arg
= default_conversion (arg
);
4244 /* Conjugating a real value is a no-op, but allow it anyway. */
4245 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4246 || typecode
== COMPLEX_TYPE
))
4248 error_at (location
, "wrong type argument to conjugation");
4249 return error_mark_node
;
4251 else if (!noconvert
)
4252 arg
= default_conversion (arg
);
4255 case TRUTH_NOT_EXPR
:
4256 if (typecode
!= INTEGER_TYPE
&& typecode
!= FIXED_POINT_TYPE
4257 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
4258 && typecode
!= COMPLEX_TYPE
)
4261 "wrong type argument to unary exclamation mark");
4262 return error_mark_node
;
4266 arg
= c_objc_common_truthvalue_conversion (location
, xarg
);
4267 arg
= remove_c_maybe_const_expr (arg
);
4270 arg
= c_objc_common_truthvalue_conversion (location
, arg
);
4271 ret
= invert_truthvalue_loc (location
, arg
);
4272 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4273 if (EXPR_P (ret
) && EXPR_HAS_LOCATION (ret
))
4274 location
= EXPR_LOCATION (ret
);
4275 goto return_build_unary_op
;
4279 ret
= build_real_imag_expr (location
, code
, arg
);
4280 if (ret
== error_mark_node
)
4281 return error_mark_node
;
4282 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
4283 eptype
= TREE_TYPE (eptype
);
4284 goto return_build_unary_op
;
4286 case PREINCREMENT_EXPR
:
4287 case POSTINCREMENT_EXPR
:
4288 case PREDECREMENT_EXPR
:
4289 case POSTDECREMENT_EXPR
:
4291 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4293 tree inner
= build_unary_op (location
, code
,
4294 C_MAYBE_CONST_EXPR_EXPR (arg
),
4296 if (inner
== error_mark_node
)
4297 return error_mark_node
;
4298 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4299 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4300 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4301 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = 1;
4302 goto return_build_unary_op
;
4305 /* Complain about anything that is not a true lvalue. In
4306 Objective-C, skip this check for property_refs. */
4307 if (!objc_is_property_ref (arg
)
4308 && !lvalue_or_else (location
,
4309 arg
, ((code
== PREINCREMENT_EXPR
4310 || code
== POSTINCREMENT_EXPR
)
4313 return error_mark_node
;
4315 if (warn_cxx_compat
&& TREE_CODE (TREE_TYPE (arg
)) == ENUMERAL_TYPE
)
4317 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4318 warning_at (location
, OPT_Wc___compat
,
4319 "increment of enumeration value is invalid in C++");
4321 warning_at (location
, OPT_Wc___compat
,
4322 "decrement of enumeration value is invalid in C++");
4325 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4327 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4328 warning_at (location
, OPT_Wbool_operation
,
4329 "increment of a boolean expression");
4331 warning_at (location
, OPT_Wbool_operation
,
4332 "decrement of a boolean expression");
4335 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4336 arg
= c_fully_fold (arg
, false, NULL
);
4339 atomic_op
= really_atomic_lvalue (arg
);
4341 /* Increment or decrement the real part of the value,
4342 and don't change the imaginary part. */
4343 if (typecode
== COMPLEX_TYPE
)
4347 pedwarn (location
, OPT_Wpedantic
,
4348 "ISO C does not support %<++%> and %<--%> on complex types");
4352 arg
= stabilize_reference (arg
);
4353 real
= build_unary_op (EXPR_LOCATION (arg
), REALPART_EXPR
, arg
,
4355 imag
= build_unary_op (EXPR_LOCATION (arg
), IMAGPART_EXPR
, arg
,
4357 real
= build_unary_op (EXPR_LOCATION (arg
), code
, real
, true);
4358 if (real
== error_mark_node
|| imag
== error_mark_node
)
4359 return error_mark_node
;
4360 ret
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
4362 goto return_build_unary_op
;
4366 /* Report invalid types. */
4368 if (typecode
!= POINTER_TYPE
&& typecode
!= FIXED_POINT_TYPE
4369 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
4370 && typecode
!= COMPLEX_TYPE
&& typecode
!= VECTOR_TYPE
)
4372 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4373 error_at (location
, "wrong type argument to increment");
4375 error_at (location
, "wrong type argument to decrement");
4377 return error_mark_node
;
4383 argtype
= TREE_TYPE (arg
);
4385 /* Compute the increment. */
4387 if (typecode
== POINTER_TYPE
)
4389 /* If pointer target is an incomplete type,
4390 we just cannot know how to do the arithmetic. */
4391 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype
)))
4393 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4395 "increment of pointer to an incomplete type %qT",
4396 TREE_TYPE (argtype
));
4399 "decrement of pointer to an incomplete type %qT",
4400 TREE_TYPE (argtype
));
4402 else if (TREE_CODE (TREE_TYPE (argtype
)) == FUNCTION_TYPE
4403 || TREE_CODE (TREE_TYPE (argtype
)) == VOID_TYPE
)
4405 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4406 pedwarn (location
, OPT_Wpointer_arith
,
4407 "wrong type argument to increment");
4409 pedwarn (location
, OPT_Wpointer_arith
,
4410 "wrong type argument to decrement");
4413 inc
= c_size_in_bytes (TREE_TYPE (argtype
));
4414 inc
= convert_to_ptrofftype_loc (location
, inc
);
4416 else if (FRACT_MODE_P (TYPE_MODE (argtype
)))
4418 /* For signed fract types, we invert ++ to -- or
4419 -- to ++, and change inc from 1 to -1, because
4420 it is not possible to represent 1 in signed fract constants.
4421 For unsigned fract types, the result always overflows and
4422 we get an undefined (original) or the maximum value. */
4423 if (code
== PREINCREMENT_EXPR
)
4424 code
= PREDECREMENT_EXPR
;
4425 else if (code
== PREDECREMENT_EXPR
)
4426 code
= PREINCREMENT_EXPR
;
4427 else if (code
== POSTINCREMENT_EXPR
)
4428 code
= POSTDECREMENT_EXPR
;
4429 else /* code == POSTDECREMENT_EXPR */
4430 code
= POSTINCREMENT_EXPR
;
4432 inc
= integer_minus_one_node
;
4433 inc
= convert (argtype
, inc
);
4437 inc
= VECTOR_TYPE_P (argtype
)
4438 ? build_one_cst (argtype
)
4440 inc
= convert (argtype
, inc
);
4443 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4444 need to ask Objective-C to build the increment or decrement
4445 expression for it. */
4446 if (objc_is_property_ref (arg
))
4447 return objc_build_incr_expr_for_property_ref (location
, code
,
4450 /* Report a read-only lvalue. */
4451 if (TYPE_READONLY (argtype
))
4453 readonly_error (location
, arg
,
4454 ((code
== PREINCREMENT_EXPR
4455 || code
== POSTINCREMENT_EXPR
)
4456 ? lv_increment
: lv_decrement
));
4457 return error_mark_node
;
4459 else if (TREE_READONLY (arg
))
4460 readonly_warning (arg
,
4461 ((code
== PREINCREMENT_EXPR
4462 || code
== POSTINCREMENT_EXPR
)
4463 ? lv_increment
: lv_decrement
));
4465 /* If the argument is atomic, use the special code sequences for
4466 atomic compound assignment. */
4469 arg
= stabilize_reference (arg
);
4470 ret
= build_atomic_assign (location
, arg
,
4471 ((code
== PREINCREMENT_EXPR
4472 || code
== POSTINCREMENT_EXPR
)
4475 (FRACT_MODE_P (TYPE_MODE (argtype
))
4477 : integer_one_node
),
4478 (code
== POSTINCREMENT_EXPR
4479 || code
== POSTDECREMENT_EXPR
));
4480 goto return_build_unary_op
;
4483 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4484 val
= boolean_increment (code
, arg
);
4486 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
4487 TREE_SIDE_EFFECTS (val
) = 1;
4488 if (TREE_CODE (val
) != code
)
4489 TREE_NO_WARNING (val
) = 1;
4491 goto return_build_unary_op
;
4495 /* Note that this operation never does default_conversion. */
4497 /* The operand of unary '&' must be an lvalue (which excludes
4498 expressions of type void), or, in C99, the result of a [] or
4499 unary '*' operator. */
4500 if (VOID_TYPE_P (TREE_TYPE (arg
))
4501 && TYPE_QUALS (TREE_TYPE (arg
)) == TYPE_UNQUALIFIED
4502 && (!INDIRECT_REF_P (arg
) || !flag_isoc99
))
4503 pedwarn (location
, 0, "taking address of expression of type %<void%>");
4505 /* Let &* cancel out to simplify resulting code. */
4506 if (INDIRECT_REF_P (arg
))
4508 /* Don't let this be an lvalue. */
4509 if (lvalue_p (TREE_OPERAND (arg
, 0)))
4510 return non_lvalue_loc (location
, TREE_OPERAND (arg
, 0));
4511 ret
= TREE_OPERAND (arg
, 0);
4512 goto return_build_unary_op
;
4515 /* Anything not already handled and not a true memory reference
4516 or a non-lvalue array is an error. */
4517 if (typecode
!= FUNCTION_TYPE
&& !noconvert
4518 && !lvalue_or_else (location
, arg
, lv_addressof
))
4519 return error_mark_node
;
4521 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4523 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4525 tree inner
= build_unary_op (location
, code
,
4526 C_MAYBE_CONST_EXPR_EXPR (arg
),
4528 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4529 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4530 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4531 C_MAYBE_CONST_EXPR_NON_CONST (ret
)
4532 = C_MAYBE_CONST_EXPR_NON_CONST (arg
);
4533 goto return_build_unary_op
;
4536 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4537 argtype
= TREE_TYPE (arg
);
4539 /* If the lvalue is const or volatile, merge that into the type
4540 to which the address will point. This is only needed
4541 for function types. */
4542 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
4543 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4544 && TREE_CODE (argtype
) == FUNCTION_TYPE
)
4546 int orig_quals
= TYPE_QUALS (strip_array_types (argtype
));
4547 int quals
= orig_quals
;
4549 if (TREE_READONLY (arg
))
4550 quals
|= TYPE_QUAL_CONST
;
4551 if (TREE_THIS_VOLATILE (arg
))
4552 quals
|= TYPE_QUAL_VOLATILE
;
4554 argtype
= c_build_qualified_type (argtype
, quals
);
4557 switch (TREE_CODE (arg
))
4560 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)))
4562 error_at (location
, "cannot take address of bit-field %qD",
4563 TREE_OPERAND (arg
, 1));
4564 return error_mark_node
;
4570 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg
, 0))))
4572 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg
))
4573 && !VECTOR_TYPE_P (TREE_TYPE (arg
)))
4575 error_at (location
, "cannot take address of scalar with "
4576 "reverse storage order");
4577 return error_mark_node
;
4580 if (TREE_CODE (TREE_TYPE (arg
)) == ARRAY_TYPE
4581 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg
)))
4582 warning_at (location
, OPT_Wscalar_storage_order
,
4583 "address of array with reverse scalar storage "
4591 if (!c_mark_addressable (arg
))
4592 return error_mark_node
;
4594 gcc_assert (TREE_CODE (arg
) != COMPONENT_REF
4595 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)));
4597 argtype
= build_pointer_type (argtype
);
4599 /* ??? Cope with user tricks that amount to offsetof. Delete this
4600 when we have proper support for integer constant expressions. */
4601 val
= get_base_address (arg
);
4602 if (val
&& INDIRECT_REF_P (val
)
4603 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
4605 ret
= fold_convert_loc (location
, argtype
, fold_offsetof_1 (arg
));
4606 goto return_build_unary_op
;
4609 val
= build1 (ADDR_EXPR
, argtype
, arg
);
4612 goto return_build_unary_op
;
4619 argtype
= TREE_TYPE (arg
);
4620 if (TREE_CODE (arg
) == INTEGER_CST
)
4621 ret
= (require_constant_value
4622 ? fold_build1_initializer_loc (location
, code
, argtype
, arg
)
4623 : fold_build1_loc (location
, code
, argtype
, arg
));
4625 ret
= build1 (code
, argtype
, arg
);
4626 return_build_unary_op
:
4627 gcc_assert (ret
!= error_mark_node
);
4628 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
)
4629 && !(TREE_CODE (xarg
) == INTEGER_CST
&& !TREE_OVERFLOW (xarg
)))
4630 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
4631 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
)
4632 ret
= note_integer_operands (ret
);
4634 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4635 protected_set_expr_location (ret
, location
);
4639 /* Return nonzero if REF is an lvalue valid for this language.
4640 Lvalues can be assigned, unless their type has TYPE_READONLY.
4641 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4644 lvalue_p (const_tree ref
)
4646 const enum tree_code code
= TREE_CODE (ref
);
4653 return lvalue_p (TREE_OPERAND (ref
, 0));
4655 case C_MAYBE_CONST_EXPR
:
4656 return lvalue_p (TREE_OPERAND (ref
, 1));
4658 case COMPOUND_LITERAL_EXPR
:
4664 case ARRAY_NOTATION_REF
:
4669 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
4670 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
4673 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
4680 /* Give a warning for storing in something that is read-only in GCC
4681 terms but not const in ISO C terms. */
4684 readonly_warning (tree arg
, enum lvalue_use use
)
4689 warning (0, "assignment of read-only location %qE", arg
);
4692 warning (0, "increment of read-only location %qE", arg
);
4695 warning (0, "decrement of read-only location %qE", arg
);
4704 /* Return nonzero if REF is an lvalue valid for this language;
4705 otherwise, print an error message and return zero. USE says
4706 how the lvalue is being used and so selects the error message.
4707 LOCATION is the location at which any error should be reported. */
4710 lvalue_or_else (location_t loc
, const_tree ref
, enum lvalue_use use
)
4712 int win
= lvalue_p (ref
);
4715 lvalue_error (loc
, use
);
4720 /* Mark EXP saying that we need to be able to take the
4721 address of it; it should not be allocated in a register.
4722 Returns true if successful. */
4725 c_mark_addressable (tree exp
)
4730 switch (TREE_CODE (x
))
4737 x
= TREE_OPERAND (x
, 0);
4740 case COMPOUND_LITERAL_EXPR
:
4742 TREE_ADDRESSABLE (x
) = 1;
4749 if (C_DECL_REGISTER (x
)
4750 && DECL_NONLOCAL (x
))
4752 if (TREE_PUBLIC (x
) || is_global_var (x
))
4755 ("global register variable %qD used in nested function", x
);
4758 pedwarn (input_location
, 0, "register variable %qD used in nested function", x
);
4760 else if (C_DECL_REGISTER (x
))
4762 if (TREE_PUBLIC (x
) || is_global_var (x
))
4763 error ("address of global register variable %qD requested", x
);
4765 error ("address of register variable %qD requested", x
);
4771 TREE_ADDRESSABLE (x
) = 1;
4778 /* Convert EXPR to TYPE, warning about conversion problems with
4779 constants. SEMANTIC_TYPE is the type this conversion would use
4780 without excess precision. If SEMANTIC_TYPE is NULL, this function
4781 is equivalent to convert_and_check. This function is a wrapper that
4782 handles conversions that may be different than
4783 the usual ones because of excess precision. */
4786 ep_convert_and_check (location_t loc
, tree type
, tree expr
,
4789 if (TREE_TYPE (expr
) == type
)
4793 return convert_and_check (loc
, type
, expr
);
4795 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
4796 && TREE_TYPE (expr
) != semantic_type
)
4798 /* For integers, we need to check the real conversion, not
4799 the conversion to the excess precision type. */
4800 expr
= convert_and_check (loc
, semantic_type
, expr
);
4802 /* Result type is the excess precision type, which should be
4803 large enough, so do not check. */
4804 return convert (type
, expr
);
4807 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4808 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4809 if folded to an integer constant then the unselected half may
4810 contain arbitrary operations not normally permitted in constant
4811 expressions. Set the location of the expression to LOC. */
4814 build_conditional_expr (location_t colon_loc
, tree ifexp
, bool ifexp_bcp
,
4815 tree op1
, tree op1_original_type
, tree op2
,
4816 tree op2_original_type
)
4820 enum tree_code code1
;
4821 enum tree_code code2
;
4822 tree result_type
= NULL
;
4823 tree semantic_result_type
= NULL
;
4824 tree orig_op1
= op1
, orig_op2
= op2
;
4825 bool int_const
, op1_int_operands
, op2_int_operands
, int_operands
;
4826 bool ifexp_int_operands
;
4829 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
4830 if (op1_int_operands
)
4831 op1
= remove_c_maybe_const_expr (op1
);
4832 op2_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op2
);
4833 if (op2_int_operands
)
4834 op2
= remove_c_maybe_const_expr (op2
);
4835 ifexp_int_operands
= EXPR_INT_CONST_OPERANDS (ifexp
);
4836 if (ifexp_int_operands
)
4837 ifexp
= remove_c_maybe_const_expr (ifexp
);
4839 /* Promote both alternatives. */
4841 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
4842 op1
= default_conversion (op1
);
4843 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
4844 op2
= default_conversion (op2
);
4846 if (TREE_CODE (ifexp
) == ERROR_MARK
4847 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
4848 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
4849 return error_mark_node
;
4851 type1
= TREE_TYPE (op1
);
4852 code1
= TREE_CODE (type1
);
4853 type2
= TREE_TYPE (op2
);
4854 code2
= TREE_CODE (type2
);
4856 if (code1
== POINTER_TYPE
&& reject_gcc_builtin (op1
))
4857 return error_mark_node
;
4859 if (code2
== POINTER_TYPE
&& reject_gcc_builtin (op2
))
4860 return error_mark_node
;
4862 /* C90 does not permit non-lvalue arrays in conditional expressions.
4863 In C99 they will be pointers by now. */
4864 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
4866 error_at (colon_loc
, "non-lvalue array in conditional expression");
4867 return error_mark_node
;
4870 if ((TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
4871 || TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4872 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4873 || code1
== COMPLEX_TYPE
)
4874 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4875 || code2
== COMPLEX_TYPE
))
4877 semantic_result_type
= c_common_type (type1
, type2
);
4878 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
4880 op1
= TREE_OPERAND (op1
, 0);
4881 type1
= TREE_TYPE (op1
);
4882 gcc_assert (TREE_CODE (type1
) == code1
);
4884 if (TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4886 op2
= TREE_OPERAND (op2
, 0);
4887 type2
= TREE_TYPE (op2
);
4888 gcc_assert (TREE_CODE (type2
) == code2
);
4892 if (warn_cxx_compat
)
4894 tree t1
= op1_original_type
? op1_original_type
: TREE_TYPE (orig_op1
);
4895 tree t2
= op2_original_type
? op2_original_type
: TREE_TYPE (orig_op2
);
4897 if (TREE_CODE (t1
) == ENUMERAL_TYPE
4898 && TREE_CODE (t2
) == ENUMERAL_TYPE
4899 && TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
4900 warning_at (colon_loc
, OPT_Wc___compat
,
4901 ("different enum types in conditional is "
4902 "invalid in C++: %qT vs %qT"),
4906 /* Quickly detect the usual case where op1 and op2 have the same type
4908 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
4911 result_type
= type1
;
4913 result_type
= TYPE_MAIN_VARIANT (type1
);
4915 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4916 || code1
== COMPLEX_TYPE
)
4917 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4918 || code2
== COMPLEX_TYPE
))
4920 result_type
= c_common_type (type1
, type2
);
4921 if (result_type
== error_mark_node
)
4922 return error_mark_node
;
4923 do_warn_double_promotion (result_type
, type1
, type2
,
4924 "implicit conversion from %qT to %qT to "
4925 "match other result of conditional",
4928 /* If -Wsign-compare, warn here if type1 and type2 have
4929 different signedness. We'll promote the signed to unsigned
4930 and later code won't know it used to be different.
4931 Do this check on the original types, so that explicit casts
4932 will be considered, but default promotions won't. */
4933 if (c_inhibit_evaluation_warnings
== 0)
4935 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
4936 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
4938 if (unsigned_op1
^ unsigned_op2
)
4942 /* Do not warn if the result type is signed, since the
4943 signed type will only be chosen if it can represent
4944 all the values of the unsigned type. */
4945 if (!TYPE_UNSIGNED (result_type
))
4949 bool op1_maybe_const
= true;
4950 bool op2_maybe_const
= true;
4952 /* Do not warn if the signed quantity is an
4953 unsuffixed integer literal (or some static
4954 constant expression involving such literals) and
4955 it is non-negative. This warning requires the
4956 operands to be folded for best results, so do
4957 that folding in this case even without
4958 warn_sign_compare to avoid warning options
4959 possibly affecting code generation. */
4960 c_inhibit_evaluation_warnings
4961 += (ifexp
== truthvalue_false_node
);
4962 op1
= c_fully_fold (op1
, require_constant_value
,
4964 c_inhibit_evaluation_warnings
4965 -= (ifexp
== truthvalue_false_node
);
4967 c_inhibit_evaluation_warnings
4968 += (ifexp
== truthvalue_true_node
);
4969 op2
= c_fully_fold (op2
, require_constant_value
,
4971 c_inhibit_evaluation_warnings
4972 -= (ifexp
== truthvalue_true_node
);
4974 if (warn_sign_compare
)
4977 && tree_expr_nonnegative_warnv_p (op1
, &ovf
))
4979 && tree_expr_nonnegative_warnv_p (op2
, &ovf
)))
4982 warning_at (colon_loc
, OPT_Wsign_compare
,
4983 ("signed and unsigned type in "
4984 "conditional expression"));
4986 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
4987 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
4988 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
4989 op2
= c_wrap_maybe_const (op2
, !op2_maybe_const
);
4994 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
4996 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
4997 pedwarn (colon_loc
, OPT_Wpedantic
,
4998 "ISO C forbids conditional expr with only one void side");
4999 result_type
= void_type_node
;
5001 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
5003 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
5004 addr_space_t as2
= TYPE_ADDR_SPACE (TREE_TYPE (type2
));
5005 addr_space_t as_common
;
5007 if (comp_target_types (colon_loc
, type1
, type2
))
5008 result_type
= common_pointer_type (type1
, type2
);
5009 else if (null_pointer_constant_p (orig_op1
))
5010 result_type
= type2
;
5011 else if (null_pointer_constant_p (orig_op2
))
5012 result_type
= type1
;
5013 else if (!addr_space_superset (as1
, as2
, &as_common
))
5015 error_at (colon_loc
, "pointers to disjoint address spaces "
5016 "used in conditional expression");
5017 return error_mark_node
;
5019 else if (VOID_TYPE_P (TREE_TYPE (type1
))
5020 && !TYPE_ATOMIC (TREE_TYPE (type1
)))
5022 if ((TREE_CODE (TREE_TYPE (type2
)) == ARRAY_TYPE
)
5023 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2
)))
5024 & ~TYPE_QUALS (TREE_TYPE (type1
))))
5025 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
5026 "pointer to array loses qualifier "
5027 "in conditional expression");
5029 if (TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
5030 pedwarn (colon_loc
, OPT_Wpedantic
,
5031 "ISO C forbids conditional expr between "
5032 "%<void *%> and function pointer");
5033 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
5034 TREE_TYPE (type2
)));
5036 else if (VOID_TYPE_P (TREE_TYPE (type2
))
5037 && !TYPE_ATOMIC (TREE_TYPE (type2
)))
5039 if ((TREE_CODE (TREE_TYPE (type1
)) == ARRAY_TYPE
)
5040 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1
)))
5041 & ~TYPE_QUALS (TREE_TYPE (type2
))))
5042 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
5043 "pointer to array loses qualifier "
5044 "in conditional expression");
5046 if (TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
5047 pedwarn (colon_loc
, OPT_Wpedantic
,
5048 "ISO C forbids conditional expr between "
5049 "%<void *%> and function pointer");
5050 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
5051 TREE_TYPE (type1
)));
5053 /* Objective-C pointer comparisons are a bit more lenient. */
5054 else if (objc_have_common_type (type1
, type2
, -3, NULL_TREE
))
5055 result_type
= objc_common_type (type1
, type2
);
5058 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
5060 pedwarn (colon_loc
, 0,
5061 "pointer type mismatch in conditional expression");
5062 result_type
= build_pointer_type
5063 (build_qualified_type (void_type_node
, qual
));
5066 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
5068 if (!null_pointer_constant_p (orig_op2
))
5069 pedwarn (colon_loc
, 0,
5070 "pointer/integer type mismatch in conditional expression");
5073 op2
= null_pointer_node
;
5075 result_type
= type1
;
5077 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5079 if (!null_pointer_constant_p (orig_op1
))
5080 pedwarn (colon_loc
, 0,
5081 "pointer/integer type mismatch in conditional expression");
5084 op1
= null_pointer_node
;
5086 result_type
= type2
;
5091 if (flag_cond_mismatch
)
5092 result_type
= void_type_node
;
5095 error_at (colon_loc
, "type mismatch in conditional expression");
5096 return error_mark_node
;
5100 /* Merge const and volatile flags of the incoming types. */
5102 = build_type_variant (result_type
,
5103 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
5104 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
5106 op1
= ep_convert_and_check (colon_loc
, result_type
, op1
,
5107 semantic_result_type
);
5108 op2
= ep_convert_and_check (colon_loc
, result_type
, op2
,
5109 semantic_result_type
);
5111 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
5113 op2_int_operands
= true;
5114 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
5116 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
5118 op1_int_operands
= true;
5119 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
5121 int_const
= int_operands
= (ifexp_int_operands
5123 && op2_int_operands
);
5126 int_const
= ((ifexp
== truthvalue_true_node
5127 && TREE_CODE (orig_op1
) == INTEGER_CST
5128 && !TREE_OVERFLOW (orig_op1
))
5129 || (ifexp
== truthvalue_false_node
5130 && TREE_CODE (orig_op2
) == INTEGER_CST
5131 && !TREE_OVERFLOW (orig_op2
)));
5134 /* Need to convert condition operand into a vector mask. */
5135 if (VECTOR_TYPE_P (TREE_TYPE (ifexp
)))
5137 tree vectype
= TREE_TYPE (ifexp
);
5138 tree elem_type
= TREE_TYPE (vectype
);
5139 tree zero
= build_int_cst (elem_type
, 0);
5140 tree zero_vec
= build_vector_from_val (vectype
, zero
);
5141 tree cmp_type
= build_same_sized_truth_vector_type (vectype
);
5142 ifexp
= build2 (NE_EXPR
, cmp_type
, ifexp
, zero_vec
);
5145 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
5146 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
5151 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5152 nested inside of the expression. */
5153 op1
= c_fully_fold (op1
, false, NULL
);
5154 op2
= c_fully_fold (op2
, false, NULL
);
5156 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
5158 ret
= note_integer_operands (ret
);
5160 if (semantic_result_type
)
5161 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
5163 protected_set_expr_location (ret
, colon_loc
);
5167 /* Return a compound expression that performs two expressions and
5168 returns the value of the second of them.
5170 LOC is the location of the COMPOUND_EXPR. */
5173 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
5175 bool expr1_int_operands
, expr2_int_operands
;
5176 tree eptype
= NULL_TREE
;
5180 && (TREE_CODE (expr1
) == CILK_SPAWN_STMT
5181 || TREE_CODE (expr2
) == CILK_SPAWN_STMT
))
5184 "spawned function call cannot be part of a comma expression");
5185 return error_mark_node
;
5187 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
5188 if (expr1_int_operands
)
5189 expr1
= remove_c_maybe_const_expr (expr1
);
5190 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
5191 if (expr2_int_operands
)
5192 expr2
= remove_c_maybe_const_expr (expr2
);
5194 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
5195 expr1
= TREE_OPERAND (expr1
, 0);
5196 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
5198 eptype
= TREE_TYPE (expr2
);
5199 expr2
= TREE_OPERAND (expr2
, 0);
5202 if (!TREE_SIDE_EFFECTS (expr1
))
5204 /* The left-hand operand of a comma expression is like an expression
5205 statement: with -Wunused, we should warn if it doesn't have
5206 any side-effects, unless it was explicitly cast to (void). */
5207 if (warn_unused_value
)
5209 if (VOID_TYPE_P (TREE_TYPE (expr1
))
5210 && CONVERT_EXPR_P (expr1
))
5212 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
5213 && TREE_CODE (expr1
) == COMPOUND_EXPR
5214 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
5215 ; /* (void) a, (void) b, c */
5217 warning_at (loc
, OPT_Wunused_value
,
5218 "left-hand operand of comma expression has no effect");
5221 else if (TREE_CODE (expr1
) == COMPOUND_EXPR
5222 && warn_unused_value
)
5225 location_t cloc
= loc
;
5226 while (TREE_CODE (r
) == COMPOUND_EXPR
)
5228 if (EXPR_HAS_LOCATION (r
))
5229 cloc
= EXPR_LOCATION (r
);
5230 r
= TREE_OPERAND (r
, 1);
5232 if (!TREE_SIDE_EFFECTS (r
)
5233 && !VOID_TYPE_P (TREE_TYPE (r
))
5234 && !CONVERT_EXPR_P (r
))
5235 warning_at (cloc
, OPT_Wunused_value
,
5236 "right-hand operand of comma expression has no effect");
5239 /* With -Wunused, we should also warn if the left-hand operand does have
5240 side-effects, but computes a value which is not used. For example, in
5241 `foo() + bar(), baz()' the result of the `+' operator is not used,
5242 so we should issue a warning. */
5243 else if (warn_unused_value
)
5244 warn_if_unused_value (expr1
, loc
);
5246 if (expr2
== error_mark_node
)
5247 return error_mark_node
;
5249 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
5252 && expr1_int_operands
5253 && expr2_int_operands
)
5254 ret
= note_integer_operands (ret
);
5257 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
5259 protected_set_expr_location (ret
, loc
);
5263 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5264 which we are casting. OTYPE is the type of the expression being
5265 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5266 of the cast. -Wcast-qual appeared on the command line. Named
5267 address space qualifiers are not handled here, because they result
5268 in different warnings. */
5271 handle_warn_cast_qual (location_t loc
, tree type
, tree otype
)
5273 tree in_type
= type
;
5274 tree in_otype
= otype
;
5279 /* Check that the qualifiers on IN_TYPE are a superset of the
5280 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5281 nodes is uninteresting and we stop as soon as we hit a
5282 non-POINTER_TYPE node on either type. */
5285 in_otype
= TREE_TYPE (in_otype
);
5286 in_type
= TREE_TYPE (in_type
);
5288 /* GNU C allows cv-qualified function types. 'const' means the
5289 function is very pure, 'volatile' means it can't return. We
5290 need to warn when such qualifiers are added, not when they're
5292 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
5293 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
5294 added
|= (TYPE_QUALS_NO_ADDR_SPACE (in_type
)
5295 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype
));
5297 discarded
|= (TYPE_QUALS_NO_ADDR_SPACE (in_otype
)
5298 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type
));
5300 while (TREE_CODE (in_type
) == POINTER_TYPE
5301 && TREE_CODE (in_otype
) == POINTER_TYPE
);
5304 warning_at (loc
, OPT_Wcast_qual
,
5305 "cast adds %q#v qualifier to function type", added
);
5308 /* There are qualifiers present in IN_OTYPE that are not present
5310 warning_at (loc
, OPT_Wcast_qual
,
5311 "cast discards %qv qualifier from pointer target type",
5314 if (added
|| discarded
)
5317 /* A cast from **T to const **T is unsafe, because it can cause a
5318 const value to be changed with no additional warning. We only
5319 issue this warning if T is the same on both sides, and we only
5320 issue the warning if there are the same number of pointers on
5321 both sides, as otherwise the cast is clearly unsafe anyhow. A
5322 cast is unsafe when a qualifier is added at one level and const
5323 is not present at all outer levels.
5325 To issue this warning, we check at each level whether the cast
5326 adds new qualifiers not already seen. We don't need to special
5327 case function types, as they won't have the same
5328 TYPE_MAIN_VARIANT. */
5330 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
5332 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
5337 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
5340 in_type
= TREE_TYPE (in_type
);
5341 in_otype
= TREE_TYPE (in_otype
);
5342 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
5345 warning_at (loc
, OPT_Wcast_qual
,
5346 "to be safe all intermediate pointers in cast from "
5347 "%qT to %qT must be %<const%> qualified",
5352 is_const
= TYPE_READONLY (in_type
);
5354 while (TREE_CODE (in_type
) == POINTER_TYPE
);
5357 /* Build an expression representing a cast to type TYPE of expression EXPR.
5358 LOC is the location of the cast-- typically the open paren of the cast. */
5361 build_c_cast (location_t loc
, tree type
, tree expr
)
5365 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
5366 expr
= TREE_OPERAND (expr
, 0);
5370 if (type
== error_mark_node
|| expr
== error_mark_node
)
5371 return error_mark_node
;
5373 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5374 only in <protocol> qualifications. But when constructing cast expressions,
5375 the protocols do matter and must be kept around. */
5376 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
5377 return build1 (NOP_EXPR
, type
, expr
);
5379 type
= TYPE_MAIN_VARIANT (type
);
5381 if (TREE_CODE (type
) == ARRAY_TYPE
)
5383 error_at (loc
, "cast specifies array type");
5384 return error_mark_node
;
5387 if (TREE_CODE (type
) == FUNCTION_TYPE
)
5389 error_at (loc
, "cast specifies function type");
5390 return error_mark_node
;
5393 if (!VOID_TYPE_P (type
))
5395 value
= require_complete_type (loc
, value
);
5396 if (value
== error_mark_node
)
5397 return error_mark_node
;
5400 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
5402 if (RECORD_OR_UNION_TYPE_P (type
))
5403 pedwarn (loc
, OPT_Wpedantic
,
5404 "ISO C forbids casting nonscalar to the same type");
5406 /* Convert to remove any qualifiers from VALUE's type. */
5407 value
= convert (type
, value
);
5409 else if (TREE_CODE (type
) == UNION_TYPE
)
5413 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5414 if (TREE_TYPE (field
) != error_mark_node
5415 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
5416 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
5422 bool maybe_const
= true;
5424 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids casts to union type");
5425 t
= c_fully_fold (value
, false, &maybe_const
);
5426 t
= build_constructor_single (type
, field
, t
);
5428 t
= c_wrap_maybe_const (t
, true);
5429 t
= digest_init (loc
, type
, t
,
5430 NULL_TREE
, false, true, 0);
5431 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
5434 error_at (loc
, "cast to union type from type not present in union");
5435 return error_mark_node
;
5441 if (type
== void_type_node
)
5443 tree t
= build1 (CONVERT_EXPR
, type
, value
);
5444 SET_EXPR_LOCATION (t
, loc
);
5448 otype
= TREE_TYPE (value
);
5450 /* Optionally warn about potentially worrisome casts. */
5452 && TREE_CODE (type
) == POINTER_TYPE
5453 && TREE_CODE (otype
) == POINTER_TYPE
)
5454 handle_warn_cast_qual (loc
, type
, otype
);
5456 /* Warn about conversions between pointers to disjoint
5458 if (TREE_CODE (type
) == POINTER_TYPE
5459 && TREE_CODE (otype
) == POINTER_TYPE
5460 && !null_pointer_constant_p (value
))
5462 addr_space_t as_to
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
5463 addr_space_t as_from
= TYPE_ADDR_SPACE (TREE_TYPE (otype
));
5464 addr_space_t as_common
;
5466 if (!addr_space_superset (as_to
, as_from
, &as_common
))
5468 if (ADDR_SPACE_GENERIC_P (as_from
))
5469 warning_at (loc
, 0, "cast to %s address space pointer "
5470 "from disjoint generic address space pointer",
5471 c_addr_space_name (as_to
));
5473 else if (ADDR_SPACE_GENERIC_P (as_to
))
5474 warning_at (loc
, 0, "cast to generic address space pointer "
5475 "from disjoint %s address space pointer",
5476 c_addr_space_name (as_from
));
5479 warning_at (loc
, 0, "cast to %s address space pointer "
5480 "from disjoint %s address space pointer",
5481 c_addr_space_name (as_to
),
5482 c_addr_space_name (as_from
));
5486 /* Warn about possible alignment problems. */
5487 if (STRICT_ALIGNMENT
5488 && TREE_CODE (type
) == POINTER_TYPE
5489 && TREE_CODE (otype
) == POINTER_TYPE
5490 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5491 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5492 /* Don't warn about opaque types, where the actual alignment
5493 restriction is unknown. */
5494 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype
))
5495 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
5496 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5497 warning_at (loc
, OPT_Wcast_align
,
5498 "cast increases required alignment of target type");
5500 if (TREE_CODE (type
) == INTEGER_TYPE
5501 && TREE_CODE (otype
) == POINTER_TYPE
5502 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5503 /* Unlike conversion of integers to pointers, where the
5504 warning is disabled for converting constants because
5505 of cases such as SIG_*, warn about converting constant
5506 pointers to integers. In some cases it may cause unwanted
5507 sign extension, and a warning is appropriate. */
5508 warning_at (loc
, OPT_Wpointer_to_int_cast
,
5509 "cast from pointer to integer of different size");
5511 if (TREE_CODE (value
) == CALL_EXPR
5512 && TREE_CODE (type
) != TREE_CODE (otype
))
5513 warning_at (loc
, OPT_Wbad_function_cast
,
5514 "cast from function call of type %qT "
5515 "to non-matching type %qT", otype
, type
);
5517 if (TREE_CODE (type
) == POINTER_TYPE
5518 && TREE_CODE (otype
) == INTEGER_TYPE
5519 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5520 /* Don't warn about converting any constant. */
5521 && !TREE_CONSTANT (value
))
5523 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
5524 "of different size");
5526 if (warn_strict_aliasing
<= 2)
5527 strict_aliasing_warning (otype
, type
, expr
);
5529 /* If pedantic, warn for conversions between function and object
5530 pointer types, except for converting a null pointer constant
5531 to function pointer type. */
5533 && TREE_CODE (type
) == POINTER_TYPE
5534 && TREE_CODE (otype
) == POINTER_TYPE
5535 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
5536 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
5537 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5538 "conversion of function pointer to object pointer type");
5541 && TREE_CODE (type
) == POINTER_TYPE
5542 && TREE_CODE (otype
) == POINTER_TYPE
5543 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
5544 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5545 && !null_pointer_constant_p (value
))
5546 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5547 "conversion of object pointer to function pointer type");
5550 value
= convert (type
, value
);
5552 /* Ignore any integer overflow caused by the cast. */
5553 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
5555 if (CONSTANT_CLASS_P (ovalue
) && TREE_OVERFLOW (ovalue
))
5557 if (!TREE_OVERFLOW (value
))
5559 /* Avoid clobbering a shared constant. */
5560 value
= copy_node (value
);
5561 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5564 else if (TREE_OVERFLOW (value
))
5565 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5566 value
= wide_int_to_tree (TREE_TYPE (value
), value
);
5570 /* Don't let a cast be an lvalue. */
5571 if (lvalue_p (value
))
5572 value
= non_lvalue_loc (loc
, value
);
5574 /* Don't allow the results of casting to floating-point or complex
5575 types be confused with actual constants, or casts involving
5576 integer and pointer types other than direct integer-to-integer
5577 and integer-to-pointer be confused with integer constant
5578 expressions and null pointer constants. */
5579 if (TREE_CODE (value
) == REAL_CST
5580 || TREE_CODE (value
) == COMPLEX_CST
5581 || (TREE_CODE (value
) == INTEGER_CST
5582 && !((TREE_CODE (expr
) == INTEGER_CST
5583 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
5584 || TREE_CODE (expr
) == REAL_CST
5585 || TREE_CODE (expr
) == COMPLEX_CST
)))
5586 value
= build1 (NOP_EXPR
, type
, value
);
5588 protected_set_expr_location (value
, loc
);
5592 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5593 location of the open paren of the cast, or the position of the cast
5596 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
5599 tree type_expr
= NULL_TREE
;
5600 bool type_expr_const
= true;
5602 int saved_wsp
= warn_strict_prototypes
;
5604 /* This avoids warnings about unprototyped casts on
5605 integers. E.g. "#define SIG_DFL (void(*)())0". */
5606 if (TREE_CODE (expr
) == INTEGER_CST
)
5607 warn_strict_prototypes
= 0;
5608 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
5609 warn_strict_prototypes
= saved_wsp
;
5611 if (TREE_CODE (expr
) == ADDR_EXPR
&& !VOID_TYPE_P (type
)
5612 && reject_gcc_builtin (expr
))
5613 return error_mark_node
;
5615 ret
= build_c_cast (loc
, type
, expr
);
5618 bool inner_expr_const
= true;
5619 ret
= c_fully_fold (ret
, require_constant_value
, &inner_expr_const
);
5620 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
5621 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !(type_expr_const
5622 && inner_expr_const
);
5623 SET_EXPR_LOCATION (ret
, loc
);
5626 if (!EXPR_HAS_LOCATION (ret
))
5627 protected_set_expr_location (ret
, loc
);
5629 /* C++ does not permits types to be defined in a cast, but it
5630 allows references to incomplete types. */
5631 if (warn_cxx_compat
&& type_name
->specs
->typespec_kind
== ctsk_tagdef
)
5632 warning_at (loc
, OPT_Wc___compat
,
5633 "defining a type in a cast is invalid in C++");
5638 /* Build an assignment expression of lvalue LHS from value RHS.
5639 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5640 may differ from TREE_TYPE (LHS) for an enum bitfield.
5641 MODIFYCODE is the code for a binary operator that we use
5642 to combine the old value of LHS with RHS to get the new value.
5643 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5644 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5645 which may differ from TREE_TYPE (RHS) for an enum value.
5647 LOCATION is the location of the MODIFYCODE operator.
5648 RHS_LOC is the location of the RHS. */
5651 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
5652 enum tree_code modifycode
,
5653 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
5657 tree rhseval
= NULL_TREE
;
5658 tree rhs_semantic_type
= NULL_TREE
;
5659 tree lhstype
= TREE_TYPE (lhs
);
5660 tree olhstype
= lhstype
;
5664 /* Types that aren't fully specified cannot be used in assignments. */
5665 lhs
= require_complete_type (location
, lhs
);
5667 /* Avoid duplicate error messages from operands that had errors. */
5668 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
5669 return error_mark_node
;
5671 /* Ensure an error for assigning a non-lvalue array to an array in
5673 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
5675 error_at (location
, "assignment to expression with array type");
5676 return error_mark_node
;
5679 /* For ObjC properties, defer this check. */
5680 if (!objc_is_property_ref (lhs
) && !lvalue_or_else (location
, lhs
, lv_assign
))
5681 return error_mark_node
;
5683 is_atomic_op
= really_atomic_lvalue (lhs
);
5685 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5687 rhs_semantic_type
= TREE_TYPE (rhs
);
5688 rhs
= TREE_OPERAND (rhs
, 0);
5693 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
5695 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
5696 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
5698 if (inner
== error_mark_node
)
5699 return error_mark_node
;
5700 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
5701 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
5702 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
5703 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
5704 protected_set_expr_location (result
, location
);
5708 /* If a binary op has been requested, combine the old LHS value with the RHS
5709 producing the value we should actually store into the LHS. */
5711 if (modifycode
!= NOP_EXPR
)
5713 lhs
= c_fully_fold (lhs
, false, NULL
);
5714 lhs
= stabilize_reference (lhs
);
5716 /* Construct the RHS for any non-atomic compound assignemnt. */
5719 /* If in LHS op= RHS the RHS has side-effects, ensure they
5720 are preevaluated before the rest of the assignment expression's
5721 side-effects, because RHS could contain e.g. function calls
5723 if (TREE_SIDE_EFFECTS (rhs
))
5725 newrhs
= in_late_binary_op
? save_expr (rhs
) : c_save_expr (rhs
);
5728 newrhs
= build_binary_op (location
,
5729 modifycode
, lhs
, newrhs
, 1);
5731 /* The original type of the right hand side is no longer
5733 rhs_origtype
= NULL_TREE
;
5737 if (c_dialect_objc ())
5739 /* Check if we are modifying an Objective-C property reference;
5740 if so, we need to generate setter calls. */
5741 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
5745 /* Else, do the check that we postponed for Objective-C. */
5746 if (!lvalue_or_else (location
, lhs
, lv_assign
))
5747 return error_mark_node
;
5750 /* Give an error for storing in something that is 'const'. */
5752 if (TYPE_READONLY (lhstype
)
5753 || (RECORD_OR_UNION_TYPE_P (lhstype
)
5754 && C_TYPE_FIELDS_READONLY (lhstype
)))
5756 readonly_error (location
, lhs
, lv_assign
);
5757 return error_mark_node
;
5759 else if (TREE_READONLY (lhs
))
5760 readonly_warning (lhs
, lv_assign
);
5762 /* If storing into a structure or union member,
5763 it has probably been given type `int'.
5764 Compute the type that would go with
5765 the actual amount of storage the member occupies. */
5767 if (TREE_CODE (lhs
) == COMPONENT_REF
5768 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5769 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
5770 || TREE_CODE (lhstype
) == REAL_TYPE
5771 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5772 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5774 /* If storing in a field that is in actuality a short or narrower than one,
5775 we must store in the field in its actual type. */
5777 if (lhstype
!= TREE_TYPE (lhs
))
5779 lhs
= copy_node (lhs
);
5780 TREE_TYPE (lhs
) = lhstype
;
5783 /* Issue -Wc++-compat warnings about an assignment to an enum type
5784 when LHS does not have its original type. This happens for,
5785 e.g., an enum bitfield in a struct. */
5787 && lhs_origtype
!= NULL_TREE
5788 && lhs_origtype
!= lhstype
5789 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
5791 tree checktype
= (rhs_origtype
!= NULL_TREE
5794 if (checktype
!= error_mark_node
5795 && (TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
)
5796 || (is_atomic_op
&& modifycode
!= NOP_EXPR
)))
5797 warning_at (location
, OPT_Wc___compat
,
5798 "enum conversion in assignment is invalid in C++");
5801 /* If the lhs is atomic, remove that qualifier. */
5804 lhstype
= build_qualified_type (lhstype
,
5805 (TYPE_QUALS (lhstype
)
5806 & ~TYPE_QUAL_ATOMIC
));
5807 olhstype
= build_qualified_type (olhstype
,
5808 (TYPE_QUALS (lhstype
)
5809 & ~TYPE_QUAL_ATOMIC
));
5812 /* Convert new value to destination type. Fold it first, then
5813 restore any excess precision information, for the sake of
5814 conversion warnings. */
5816 if (!(is_atomic_op
&& modifycode
!= NOP_EXPR
))
5818 npc
= null_pointer_constant_p (newrhs
);
5819 newrhs
= c_fully_fold (newrhs
, false, NULL
);
5820 if (rhs_semantic_type
)
5821 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
5822 newrhs
= convert_for_assignment (location
, rhs_loc
, lhstype
, newrhs
,
5823 rhs_origtype
, ic_assign
, npc
,
5824 NULL_TREE
, NULL_TREE
, 0);
5825 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5826 return error_mark_node
;
5829 /* Emit ObjC write barrier, if necessary. */
5830 if (c_dialect_objc () && flag_objc_gc
)
5832 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
5835 protected_set_expr_location (result
, location
);
5840 /* Scan operands. */
5843 result
= build_atomic_assign (location
, lhs
, modifycode
, newrhs
, false);
5846 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
5847 TREE_SIDE_EFFECTS (result
) = 1;
5848 protected_set_expr_location (result
, location
);
5851 /* If we got the LHS in a different type for storing in,
5852 convert the result back to the nominal type of LHS
5853 so that the value we return always has the same type
5854 as the LHS argument. */
5856 if (olhstype
== TREE_TYPE (result
))
5859 result
= convert_for_assignment (location
, rhs_loc
, olhstype
, result
,
5860 rhs_origtype
, ic_assign
, false, NULL_TREE
,
5862 protected_set_expr_location (result
, location
);
5866 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), rhseval
, result
);
5870 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5871 This is used to implement -fplan9-extensions. */
5874 find_anonymous_field_with_type (tree struct_type
, tree type
)
5879 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type
));
5881 for (field
= TYPE_FIELDS (struct_type
);
5883 field
= TREE_CHAIN (field
))
5885 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5886 ? c_build_qualified_type (TREE_TYPE (field
),
5888 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5889 if (DECL_NAME (field
) == NULL
5890 && comptypes (type
, fieldtype
))
5896 else if (DECL_NAME (field
) == NULL
5897 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
))
5898 && find_anonymous_field_with_type (TREE_TYPE (field
), type
))
5908 /* RHS is an expression whose type is pointer to struct. If there is
5909 an anonymous field in RHS with type TYPE, then return a pointer to
5910 that field in RHS. This is used with -fplan9-extensions. This
5911 returns NULL if no conversion could be found. */
5914 convert_to_anonymous_field (location_t location
, tree type
, tree rhs
)
5916 tree rhs_struct_type
, lhs_main_type
;
5917 tree field
, found_field
;
5918 bool found_sub_field
;
5921 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs
)));
5922 rhs_struct_type
= TREE_TYPE (TREE_TYPE (rhs
));
5923 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type
));
5925 gcc_assert (POINTER_TYPE_P (type
));
5926 lhs_main_type
= (TYPE_ATOMIC (TREE_TYPE (type
))
5927 ? c_build_qualified_type (TREE_TYPE (type
),
5929 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
5931 found_field
= NULL_TREE
;
5932 found_sub_field
= false;
5933 for (field
= TYPE_FIELDS (rhs_struct_type
);
5935 field
= TREE_CHAIN (field
))
5937 if (DECL_NAME (field
) != NULL_TREE
5938 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
5940 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5941 ? c_build_qualified_type (TREE_TYPE (field
),
5943 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5944 if (comptypes (lhs_main_type
, fieldtype
))
5946 if (found_field
!= NULL_TREE
)
5948 found_field
= field
;
5950 else if (find_anonymous_field_with_type (TREE_TYPE (field
),
5953 if (found_field
!= NULL_TREE
)
5955 found_field
= field
;
5956 found_sub_field
= true;
5960 if (found_field
== NULL_TREE
)
5963 ret
= fold_build3_loc (location
, COMPONENT_REF
, TREE_TYPE (found_field
),
5964 build_fold_indirect_ref (rhs
), found_field
,
5966 ret
= build_fold_addr_expr_loc (location
, ret
);
5968 if (found_sub_field
)
5970 ret
= convert_to_anonymous_field (location
, type
, ret
);
5971 gcc_assert (ret
!= NULL_TREE
);
5977 /* Issue an error message for a bad initializer component.
5978 GMSGID identifies the message.
5979 The component name is taken from the spelling stack. */
5982 error_init (location_t loc
, const char *gmsgid
)
5986 /* The gmsgid may be a format string with %< and %>. */
5987 error_at (loc
, gmsgid
);
5988 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5990 inform (loc
, "(near initialization for %qs)", ofwhat
);
5993 /* Issue a pedantic warning for a bad initializer component. OPT is
5994 the option OPT_* (from options.h) controlling this warning or 0 if
5995 it is unconditionally given. GMSGID identifies the message. The
5996 component name is taken from the spelling stack. */
5999 pedwarn_init (location_t loc
, int opt
, const char *gmsgid
)
6004 /* Use the location where a macro was expanded rather than where
6005 it was defined to make sure macros defined in system headers
6006 but used incorrectly elsewhere are diagnosed. */
6007 source_location exploc
= expansion_point_location_if_in_system_header (loc
);
6009 /* The gmsgid may be a format string with %< and %>. */
6010 warned
= pedwarn (exploc
, opt
, gmsgid
);
6011 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6012 if (*ofwhat
&& warned
)
6013 inform (exploc
, "(near initialization for %qs)", ofwhat
);
6016 /* Issue a warning for a bad initializer component.
6018 OPT is the OPT_W* value corresponding to the warning option that
6019 controls this warning. GMSGID identifies the message. The
6020 component name is taken from the spelling stack. */
6023 warning_init (location_t loc
, int opt
, const char *gmsgid
)
6028 /* Use the location where a macro was expanded rather than where
6029 it was defined to make sure macros defined in system headers
6030 but used incorrectly elsewhere are diagnosed. */
6031 source_location exploc
= expansion_point_location_if_in_system_header (loc
);
6033 /* The gmsgid may be a format string with %< and %>. */
6034 warned
= warning_at (exploc
, opt
, gmsgid
);
6035 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6036 if (*ofwhat
&& warned
)
6037 inform (exploc
, "(near initialization for %qs)", ofwhat
);
6040 /* If TYPE is an array type and EXPR is a parenthesized string
6041 constant, warn if pedantic that EXPR is being used to initialize an
6042 object of type TYPE. */
6045 maybe_warn_string_init (location_t loc
, tree type
, struct c_expr expr
)
6048 && TREE_CODE (type
) == ARRAY_TYPE
6049 && TREE_CODE (expr
.value
) == STRING_CST
6050 && expr
.original_code
!= STRING_CST
)
6051 pedwarn_init (loc
, OPT_Wpedantic
,
6052 "array initialized from parenthesized string constant");
6055 /* Convert value RHS to type TYPE as preparation for an assignment to
6056 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6057 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6058 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6059 constant before any folding.
6060 The real work of conversion is done by `convert'.
6061 The purpose of this function is to generate error messages
6062 for assignments that are not allowed in C.
6063 ERRTYPE says whether it is argument passing, assignment,
6064 initialization or return.
6066 In the following example, '~' denotes where EXPR_LOC and '^' where
6069 f (var); [ic_argpass]
6071 x = var; [ic_assign]
6073 int x = var; [ic_init]
6075 return x; [ic_return]
6078 FUNCTION is a tree for the function being called.
6079 PARMNUM is the number of the argument, for printing in error messages. */
6082 convert_for_assignment (location_t location
, location_t expr_loc
, tree type
,
6083 tree rhs
, tree origtype
, enum impl_conv errtype
,
6084 bool null_pointer_constant
, tree fundecl
,
6085 tree function
, int parmnum
)
6087 enum tree_code codel
= TREE_CODE (type
);
6088 tree orig_rhs
= rhs
;
6090 enum tree_code coder
;
6091 tree rname
= NULL_TREE
;
6092 bool objc_ok
= false;
6094 /* Use the expansion point location to handle cases such as user's
6095 function returning a wrong-type macro defined in a system header. */
6096 location
= expansion_point_location_if_in_system_header (location
);
6098 if (errtype
== ic_argpass
)
6101 /* Change pointer to function to the function itself for
6103 if (TREE_CODE (function
) == ADDR_EXPR
6104 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
6105 function
= TREE_OPERAND (function
, 0);
6107 /* Handle an ObjC selector specially for diagnostics. */
6108 selector
= objc_message_selector ();
6110 if (selector
&& parmnum
> 2)
6117 /* This macro is used to emit diagnostics to ensure that all format
6118 strings are complete sentences, visible to gettext and checked at
6120 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6125 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6126 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6127 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6128 "expected %qT but argument is of type %qT", \
6132 pedwarn (LOCATION, OPT, AS); \
6135 pedwarn_init (LOCATION, OPT, IN); \
6138 pedwarn (LOCATION, OPT, RE); \
6141 gcc_unreachable (); \
6145 /* This macro is used to emit diagnostics to ensure that all format
6146 strings are complete sentences, visible to gettext and checked at
6147 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6148 extra parameter to enumerate qualifiers. */
6149 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6154 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6155 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6156 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6157 "expected %qT but argument is of type %qT", \
6161 pedwarn (LOCATION, OPT, AS, QUALS); \
6164 pedwarn (LOCATION, OPT, IN, QUALS); \
6167 pedwarn (LOCATION, OPT, RE, QUALS); \
6170 gcc_unreachable (); \
6174 /* This macro is used to emit diagnostics to ensure that all format
6175 strings are complete sentences, visible to gettext and checked at
6176 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6177 warning_at instead of pedwarn. */
6178 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6183 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6184 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6185 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6186 "expected %qT but argument is of type %qT", \
6190 warning_at (LOCATION, OPT, AS, QUALS); \
6193 warning_at (LOCATION, OPT, IN, QUALS); \
6196 warning_at (LOCATION, OPT, RE, QUALS); \
6199 gcc_unreachable (); \
6203 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
6204 rhs
= TREE_OPERAND (rhs
, 0);
6206 rhstype
= TREE_TYPE (rhs
);
6207 coder
= TREE_CODE (rhstype
);
6209 if (coder
== ERROR_MARK
)
6210 return error_mark_node
;
6212 if (c_dialect_objc ())
6235 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
6238 if (warn_cxx_compat
)
6240 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
6241 if (checktype
!= error_mark_node
6242 && TREE_CODE (type
) == ENUMERAL_TYPE
6243 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
6245 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wc___compat
,
6246 G_("enum conversion when passing argument "
6247 "%d of %qE is invalid in C++"),
6248 G_("enum conversion in assignment is "
6250 G_("enum conversion in initialization is "
6252 G_("enum conversion in return is "
6257 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
6260 if (coder
== VOID_TYPE
)
6262 /* Except for passing an argument to an unprototyped function,
6263 this is a constraint violation. When passing an argument to
6264 an unprototyped function, it is compile-time undefined;
6265 making it a constraint in that case was rejected in
6267 error_at (location
, "void value not ignored as it ought to be");
6268 return error_mark_node
;
6270 rhs
= require_complete_type (location
, rhs
);
6271 if (rhs
== error_mark_node
)
6272 return error_mark_node
;
6274 if (coder
== POINTER_TYPE
&& reject_gcc_builtin (rhs
))
6275 return error_mark_node
;
6277 /* A non-reference type can convert to a reference. This handles
6278 va_start, va_copy and possibly port built-ins. */
6279 if (codel
== REFERENCE_TYPE
&& coder
!= REFERENCE_TYPE
)
6281 if (!lvalue_p (rhs
))
6283 error_at (location
, "cannot pass rvalue to reference parameter");
6284 return error_mark_node
;
6286 if (!c_mark_addressable (rhs
))
6287 return error_mark_node
;
6288 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
6289 SET_EXPR_LOCATION (rhs
, location
);
6291 rhs
= convert_for_assignment (location
, expr_loc
,
6292 build_pointer_type (TREE_TYPE (type
)),
6293 rhs
, origtype
, errtype
,
6294 null_pointer_constant
, fundecl
, function
,
6296 if (rhs
== error_mark_node
)
6297 return error_mark_node
;
6299 rhs
= build1 (NOP_EXPR
, type
, rhs
);
6300 SET_EXPR_LOCATION (rhs
, location
);
6303 /* Some types can interconvert without explicit casts. */
6304 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
6305 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
6306 return convert (type
, rhs
);
6307 /* Arithmetic types all interconvert, and enum is treated like int. */
6308 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
6309 || codel
== FIXED_POINT_TYPE
6310 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
6311 || codel
== BOOLEAN_TYPE
)
6312 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
6313 || coder
== FIXED_POINT_TYPE
6314 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
6315 || coder
== BOOLEAN_TYPE
))
6318 bool save
= in_late_binary_op
;
6319 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
6320 || (coder
== REAL_TYPE
6321 && (codel
== INTEGER_TYPE
|| codel
== ENUMERAL_TYPE
)
6322 && (flag_sanitize
& SANITIZE_FLOAT_CAST
)))
6323 in_late_binary_op
= true;
6324 ret
= convert_and_check (expr_loc
!= UNKNOWN_LOCATION
6325 ? expr_loc
: location
, type
, orig_rhs
);
6326 in_late_binary_op
= save
;
6330 /* Aggregates in different TUs might need conversion. */
6331 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
6333 && comptypes (type
, rhstype
))
6334 return convert_and_check (expr_loc
!= UNKNOWN_LOCATION
6335 ? expr_loc
: location
, type
, rhs
);
6337 /* Conversion to a transparent union or record from its member types.
6338 This applies only to function arguments. */
6339 if (((codel
== UNION_TYPE
|| codel
== RECORD_TYPE
)
6340 && TYPE_TRANSPARENT_AGGR (type
))
6341 && errtype
== ic_argpass
)
6343 tree memb
, marginal_memb
= NULL_TREE
;
6345 for (memb
= TYPE_FIELDS (type
); memb
; memb
= DECL_CHAIN (memb
))
6347 tree memb_type
= TREE_TYPE (memb
);
6349 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
6350 TYPE_MAIN_VARIANT (rhstype
)))
6353 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
6356 if (coder
== POINTER_TYPE
)
6358 tree ttl
= TREE_TYPE (memb_type
);
6359 tree ttr
= TREE_TYPE (rhstype
);
6361 /* Any non-function converts to a [const][volatile] void *
6362 and vice versa; otherwise, targets must be the same.
6363 Meanwhile, the lhs target must have all the qualifiers of
6365 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
6366 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
6367 || comp_target_types (location
, memb_type
, rhstype
))
6369 int lquals
= TYPE_QUALS (ttl
) & ~TYPE_QUAL_ATOMIC
;
6370 int rquals
= TYPE_QUALS (ttr
) & ~TYPE_QUAL_ATOMIC
;
6371 /* If this type won't generate any warnings, use it. */
6372 if (lquals
== rquals
6373 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
6374 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
6375 ? ((lquals
| rquals
) == rquals
)
6376 : ((lquals
| rquals
) == lquals
)))
6379 /* Keep looking for a better type, but remember this one. */
6381 marginal_memb
= memb
;
6385 /* Can convert integer zero to any pointer type. */
6386 if (null_pointer_constant
)
6388 rhs
= null_pointer_node
;
6393 if (memb
|| marginal_memb
)
6397 /* We have only a marginally acceptable member type;
6398 it needs a warning. */
6399 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
6400 tree ttr
= TREE_TYPE (rhstype
);
6402 /* Const and volatile mean something different for function
6403 types, so the usual warnings are not appropriate. */
6404 if (TREE_CODE (ttr
) == FUNCTION_TYPE
6405 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
6407 /* Because const and volatile on functions are
6408 restrictions that say the function will not do
6409 certain things, it is okay to use a const or volatile
6410 function where an ordinary one is wanted, but not
6412 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6413 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6414 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6415 OPT_Wdiscarded_qualifiers
,
6416 G_("passing argument %d of %qE "
6417 "makes %q#v qualified function "
6418 "pointer from unqualified"),
6419 G_("assignment makes %q#v qualified "
6420 "function pointer from "
6422 G_("initialization makes %q#v qualified "
6423 "function pointer from "
6425 G_("return makes %q#v qualified function "
6426 "pointer from unqualified"),
6427 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6429 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr
)
6430 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl
))
6431 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6432 OPT_Wdiscarded_qualifiers
,
6433 G_("passing argument %d of %qE discards "
6434 "%qv qualifier from pointer target type"),
6435 G_("assignment discards %qv qualifier "
6436 "from pointer target type"),
6437 G_("initialization discards %qv qualifier "
6438 "from pointer target type"),
6439 G_("return discards %qv qualifier from "
6440 "pointer target type"),
6441 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6443 memb
= marginal_memb
;
6446 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
6447 pedwarn (location
, OPT_Wpedantic
,
6448 "ISO C prohibits argument conversion to union type");
6450 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
6451 return build_constructor_single (type
, memb
, rhs
);
6455 /* Conversions among pointers */
6456 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
6457 && (coder
== codel
))
6459 tree ttl
= TREE_TYPE (type
);
6460 tree ttr
= TREE_TYPE (rhstype
);
6463 bool is_opaque_pointer
;
6464 int target_cmp
= 0; /* Cache comp_target_types () result. */
6468 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
6469 mvl
= (TYPE_ATOMIC (mvl
)
6470 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
),
6472 : TYPE_MAIN_VARIANT (mvl
));
6473 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
6474 mvr
= (TYPE_ATOMIC (mvr
)
6475 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
),
6477 : TYPE_MAIN_VARIANT (mvr
));
6478 /* Opaque pointers are treated like void pointers. */
6479 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
6481 /* The Plan 9 compiler permits a pointer to a struct to be
6482 automatically converted into a pointer to an anonymous field
6483 within the struct. */
6484 if (flag_plan9_extensions
6485 && RECORD_OR_UNION_TYPE_P (mvl
)
6486 && RECORD_OR_UNION_TYPE_P (mvr
)
6489 tree new_rhs
= convert_to_anonymous_field (location
, type
, rhs
);
6490 if (new_rhs
!= NULL_TREE
)
6493 rhstype
= TREE_TYPE (rhs
);
6494 coder
= TREE_CODE (rhstype
);
6495 ttr
= TREE_TYPE (rhstype
);
6496 mvr
= TYPE_MAIN_VARIANT (ttr
);
6500 /* C++ does not allow the implicit conversion void* -> T*. However,
6501 for the purpose of reducing the number of false positives, we
6502 tolerate the special case of
6506 where NULL is typically defined in C to be '(void *) 0'. */
6507 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
6508 warning_at (errtype
== ic_argpass
? expr_loc
: location
,
6510 "request for implicit conversion "
6511 "from %qT to %qT not permitted in C++", rhstype
, type
);
6513 /* See if the pointers point to incompatible address spaces. */
6514 asl
= TYPE_ADDR_SPACE (ttl
);
6515 asr
= TYPE_ADDR_SPACE (ttr
);
6516 if (!null_pointer_constant_p (rhs
)
6517 && asr
!= asl
&& !targetm
.addr_space
.subset_p (asr
, asl
))
6522 error_at (expr_loc
, "passing argument %d of %qE from pointer to "
6523 "non-enclosed address space", parmnum
, rname
);
6526 error_at (location
, "assignment from pointer to "
6527 "non-enclosed address space");
6530 error_at (location
, "initialization from pointer to "
6531 "non-enclosed address space");
6534 error_at (location
, "return from pointer to "
6535 "non-enclosed address space");
6540 return error_mark_node
;
6543 /* Check if the right-hand side has a format attribute but the
6544 left-hand side doesn't. */
6545 if (warn_suggest_attribute_format
6546 && check_missing_format_attribute (type
, rhstype
))
6551 warning_at (expr_loc
, OPT_Wsuggest_attribute_format
,
6552 "argument %d of %qE might be "
6553 "a candidate for a format attribute",
6557 warning_at (location
, OPT_Wsuggest_attribute_format
,
6558 "assignment left-hand side might be "
6559 "a candidate for a format attribute");
6562 warning_at (location
, OPT_Wsuggest_attribute_format
,
6563 "initialization left-hand side might be "
6564 "a candidate for a format attribute");
6567 warning_at (location
, OPT_Wsuggest_attribute_format
,
6568 "return type might be "
6569 "a candidate for a format attribute");
6576 /* Any non-function converts to a [const][volatile] void *
6577 and vice versa; otherwise, targets must be the same.
6578 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6579 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
6580 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
6581 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
6582 || is_opaque_pointer
6583 || ((c_common_unsigned_type (mvl
)
6584 == c_common_unsigned_type (mvr
))
6585 && (c_common_signed_type (mvl
)
6586 == c_common_signed_type (mvr
))
6587 && TYPE_ATOMIC (mvl
) == TYPE_ATOMIC (mvr
)))
6589 /* Warn about loss of qualifers from pointers to arrays with
6590 qualifiers on the element type. */
6591 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
6593 ttr
= strip_array_types (ttr
);
6594 ttl
= strip_array_types (ttl
);
6596 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
6597 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
6598 WARNING_FOR_QUALIFIERS (location
, expr_loc
,
6599 OPT_Wdiscarded_array_qualifiers
,
6600 G_("passing argument %d of %qE discards "
6601 "%qv qualifier from pointer target type"),
6602 G_("assignment discards %qv qualifier "
6603 "from pointer target type"),
6604 G_("initialization discards %qv qualifier "
6605 "from pointer target type"),
6606 G_("return discards %qv qualifier from "
6607 "pointer target type"),
6608 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6611 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6614 && !null_pointer_constant
6615 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
6616 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpedantic
,
6617 G_("ISO C forbids passing argument %d of "
6618 "%qE between function pointer "
6620 G_("ISO C forbids assignment between "
6621 "function pointer and %<void *%>"),
6622 G_("ISO C forbids initialization between "
6623 "function pointer and %<void *%>"),
6624 G_("ISO C forbids return between function "
6625 "pointer and %<void *%>"));
6626 /* Const and volatile mean something different for function types,
6627 so the usual warnings are not appropriate. */
6628 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
6629 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
6631 /* Don't warn about loss of qualifier for conversions from
6632 qualified void* to pointers to arrays with corresponding
6633 qualifier on the element type. */
6635 ttl
= strip_array_types (ttl
);
6637 /* Assignments between atomic and non-atomic objects are OK. */
6638 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
6639 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
6641 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6642 OPT_Wdiscarded_qualifiers
,
6643 G_("passing argument %d of %qE discards "
6644 "%qv qualifier from pointer target type"),
6645 G_("assignment discards %qv qualifier "
6646 "from pointer target type"),
6647 G_("initialization discards %qv qualifier "
6648 "from pointer target type"),
6649 G_("return discards %qv qualifier from "
6650 "pointer target type"),
6651 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6653 /* If this is not a case of ignoring a mismatch in signedness,
6655 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
6658 /* If there is a mismatch, do warn. */
6659 else if (warn_pointer_sign
)
6660 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpointer_sign
,
6661 G_("pointer targets in passing argument "
6662 "%d of %qE differ in signedness"),
6663 G_("pointer targets in assignment "
6664 "differ in signedness"),
6665 G_("pointer targets in initialization "
6666 "differ in signedness"),
6667 G_("pointer targets in return differ "
6670 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
6671 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6673 /* Because const and volatile on functions are restrictions
6674 that say the function will not do certain things,
6675 it is okay to use a const or volatile function
6676 where an ordinary one is wanted, but not vice-versa. */
6677 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6678 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6679 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6680 OPT_Wdiscarded_qualifiers
,
6681 G_("passing argument %d of %qE makes "
6682 "%q#v qualified function pointer "
6683 "from unqualified"),
6684 G_("assignment makes %q#v qualified function "
6685 "pointer from unqualified"),
6686 G_("initialization makes %q#v qualified "
6687 "function pointer from unqualified"),
6688 G_("return makes %q#v qualified function "
6689 "pointer from unqualified"),
6690 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6694 /* Avoid warning about the volatile ObjC EH puts on decls. */
6696 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6697 OPT_Wincompatible_pointer_types
,
6698 G_("passing argument %d of %qE from "
6699 "incompatible pointer type"),
6700 G_("assignment from incompatible pointer type"),
6701 G_("initialization from incompatible "
6703 G_("return from incompatible pointer type"));
6705 return convert (type
, rhs
);
6707 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
6709 /* ??? This should not be an error when inlining calls to
6710 unprototyped functions. */
6711 error_at (location
, "invalid use of non-lvalue array");
6712 return error_mark_node
;
6714 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
6716 /* An explicit constant 0 can convert to a pointer,
6717 or one that results from arithmetic, even including
6718 a cast to integer type. */
6719 if (!null_pointer_constant
)
6720 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6721 OPT_Wint_conversion
,
6722 G_("passing argument %d of %qE makes "
6723 "pointer from integer without a cast"),
6724 G_("assignment makes pointer from integer "
6726 G_("initialization makes pointer from "
6727 "integer without a cast"),
6728 G_("return makes pointer from integer "
6731 return convert (type
, rhs
);
6733 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
6735 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6736 OPT_Wint_conversion
,
6737 G_("passing argument %d of %qE makes integer "
6738 "from pointer without a cast"),
6739 G_("assignment makes integer from pointer "
6741 G_("initialization makes integer from pointer "
6743 G_("return makes integer from pointer "
6745 return convert (type
, rhs
);
6747 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
6750 bool save
= in_late_binary_op
;
6751 in_late_binary_op
= true;
6752 ret
= convert (type
, rhs
);
6753 in_late_binary_op
= save
;
6760 error_at (expr_loc
, "incompatible type for argument %d of %qE", parmnum
,
6762 inform ((fundecl
&& !DECL_IS_BUILTIN (fundecl
))
6763 ? DECL_SOURCE_LOCATION (fundecl
) : expr_loc
,
6764 "expected %qT but argument is of type %qT", type
, rhstype
);
6767 error_at (location
, "incompatible types when assigning to type %qT from "
6768 "type %qT", type
, rhstype
);
6772 "incompatible types when initializing type %qT using type %qT",
6777 "incompatible types when returning type %qT but %qT was "
6778 "expected", rhstype
, type
);
6784 return error_mark_node
;
6787 /* If VALUE is a compound expr all of whose expressions are constant, then
6788 return its value. Otherwise, return error_mark_node.
6790 This is for handling COMPOUND_EXPRs as initializer elements
6791 which is allowed with a warning when -pedantic is specified. */
6794 valid_compound_expr_initializer (tree value
, tree endtype
)
6796 if (TREE_CODE (value
) == COMPOUND_EXPR
)
6798 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
6800 return error_mark_node
;
6801 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
6804 else if (!initializer_constant_valid_p (value
, endtype
))
6805 return error_mark_node
;
6810 /* Perform appropriate conversions on the initial value of a variable,
6811 store it in the declaration DECL,
6812 and print any error messages that are appropriate.
6813 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6814 If the init is invalid, store an ERROR_MARK.
6816 INIT_LOC is the location of the initial value. */
6819 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
6824 /* If variable's type was invalidly declared, just ignore it. */
6826 type
= TREE_TYPE (decl
);
6827 if (TREE_CODE (type
) == ERROR_MARK
)
6830 /* Digest the specified initializer into an expression. */
6833 npc
= null_pointer_constant_p (init
);
6834 value
= digest_init (init_loc
, type
, init
, origtype
, npc
,
6835 true, TREE_STATIC (decl
));
6837 /* Store the expression if valid; else report error. */
6839 if (!in_system_header_at (input_location
)
6840 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
6841 warning (OPT_Wtraditional
, "traditional C rejects automatic "
6842 "aggregate initialization");
6844 if (value
!= error_mark_node
|| TREE_CODE (decl
) != FUNCTION_DECL
)
6845 DECL_INITIAL (decl
) = value
;
6847 /* ANSI wants warnings about out-of-range constant initializers. */
6848 STRIP_TYPE_NOPS (value
);
6849 if (TREE_STATIC (decl
))
6850 constant_expression_warning (value
);
6852 /* Check if we need to set array size from compound literal size. */
6853 if (TREE_CODE (type
) == ARRAY_TYPE
6854 && TYPE_DOMAIN (type
) == 0
6855 && value
!= error_mark_node
)
6857 tree inside_init
= init
;
6859 STRIP_TYPE_NOPS (inside_init
);
6860 inside_init
= fold (inside_init
);
6862 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6864 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6866 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
6868 /* For int foo[] = (int [3]){1}; we need to set array size
6869 now since later on array initializer will be just the
6870 brace enclosed list of the compound literal. */
6871 tree etype
= strip_array_types (TREE_TYPE (decl
));
6872 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6873 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
6875 layout_decl (cldecl
, 0);
6877 = c_build_qualified_type (type
, TYPE_QUALS (etype
));
6883 /* Methods for storing and printing names for error messages. */
6885 /* Implement a spelling stack that allows components of a name to be pushed
6886 and popped. Each element on the stack is this structure. */
6893 unsigned HOST_WIDE_INT i
;
6898 #define SPELLING_STRING 1
6899 #define SPELLING_MEMBER 2
6900 #define SPELLING_BOUNDS 3
6902 static struct spelling
*spelling
; /* Next stack element (unused). */
6903 static struct spelling
*spelling_base
; /* Spelling stack base. */
6904 static int spelling_size
; /* Size of the spelling stack. */
6906 /* Macros to save and restore the spelling stack around push_... functions.
6907 Alternative to SAVE_SPELLING_STACK. */
6909 #define SPELLING_DEPTH() (spelling - spelling_base)
6910 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6912 /* Push an element on the spelling stack with type KIND and assign VALUE
6915 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6917 int depth = SPELLING_DEPTH (); \
6919 if (depth >= spelling_size) \
6921 spelling_size += 10; \
6922 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6924 RESTORE_SPELLING_DEPTH (depth); \
6927 spelling->kind = (KIND); \
6928 spelling->MEMBER = (VALUE); \
6932 /* Push STRING on the stack. Printed literally. */
6935 push_string (const char *string
)
6937 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
6940 /* Push a member name on the stack. Printed as '.' STRING. */
6943 push_member_name (tree decl
)
6945 const char *const string
6947 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
6948 : _("<anonymous>"));
6949 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
6952 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6955 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
6957 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
6960 /* Compute the maximum size in bytes of the printed spelling. */
6963 spelling_length (void)
6968 for (p
= spelling_base
; p
< spelling
; p
++)
6970 if (p
->kind
== SPELLING_BOUNDS
)
6973 size
+= strlen (p
->u
.s
) + 1;
6979 /* Print the spelling to BUFFER and return it. */
6982 print_spelling (char *buffer
)
6987 for (p
= spelling_base
; p
< spelling
; p
++)
6988 if (p
->kind
== SPELLING_BOUNDS
)
6990 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
6996 if (p
->kind
== SPELLING_MEMBER
)
6998 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
7005 /* Digest the parser output INIT as an initializer for type TYPE.
7006 Return a C expression of type TYPE to represent the initial value.
7008 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7010 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7012 If INIT is a string constant, STRICT_STRING is true if it is
7013 unparenthesized or we should not warn here for it being parenthesized.
7014 For other types of INIT, STRICT_STRING is not used.
7016 INIT_LOC is the location of the INIT.
7018 REQUIRE_CONSTANT requests an error if non-constant initializers or
7019 elements are seen. */
7022 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
7023 bool null_pointer_constant
, bool strict_string
,
7024 int require_constant
)
7026 enum tree_code code
= TREE_CODE (type
);
7027 tree inside_init
= init
;
7028 tree semantic_type
= NULL_TREE
;
7029 bool maybe_const
= true;
7031 if (type
== error_mark_node
7033 || error_operand_p (init
))
7034 return error_mark_node
;
7036 STRIP_TYPE_NOPS (inside_init
);
7038 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
7040 semantic_type
= TREE_TYPE (inside_init
);
7041 inside_init
= TREE_OPERAND (inside_init
, 0);
7043 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
7044 inside_init
= decl_constant_value_for_optimization (inside_init
);
7046 /* Initialization of an array of chars from a string constant
7047 optionally enclosed in braces. */
7049 if (code
== ARRAY_TYPE
&& inside_init
7050 && TREE_CODE (inside_init
) == STRING_CST
)
7053 = (TYPE_ATOMIC (TREE_TYPE (type
))
7054 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
7056 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
7057 /* Note that an array could be both an array of character type
7058 and an array of wchar_t if wchar_t is signed char or unsigned
7060 bool char_array
= (typ1
== char_type_node
7061 || typ1
== signed_char_type_node
7062 || typ1
== unsigned_char_type_node
);
7063 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
7064 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
7065 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
7067 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
7070 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
7071 expr
.value
= inside_init
;
7072 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
7073 expr
.original_type
= NULL
;
7074 maybe_warn_string_init (init_loc
, type
, expr
);
7076 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
7077 pedwarn_init (init_loc
, OPT_Wpedantic
,
7078 "initialization of a flexible array member");
7080 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7081 TYPE_MAIN_VARIANT (type
)))
7086 if (typ2
!= char_type_node
)
7088 error_init (init_loc
, "char-array initialized from wide "
7090 return error_mark_node
;
7095 if (typ2
== char_type_node
)
7097 error_init (init_loc
, "wide character array initialized "
7098 "from non-wide string");
7099 return error_mark_node
;
7101 else if (!comptypes(typ1
, typ2
))
7103 error_init (init_loc
, "wide character array initialized "
7104 "from incompatible wide string");
7105 return error_mark_node
;
7109 TREE_TYPE (inside_init
) = type
;
7110 if (TYPE_DOMAIN (type
) != 0
7111 && TYPE_SIZE (type
) != 0
7112 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
7114 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
7116 /* Subtract the size of a single (possibly wide) character
7117 because it's ok to ignore the terminating null char
7118 that is counted in the length of the constant. */
7119 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
7121 - (TYPE_PRECISION (typ1
)
7123 pedwarn_init (init_loc
, 0,
7124 ("initializer-string for array of chars "
7126 else if (warn_cxx_compat
7127 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
), len
))
7128 warning_at (init_loc
, OPT_Wc___compat
,
7129 ("initializer-string for array chars "
7130 "is too long for C++"));
7135 else if (INTEGRAL_TYPE_P (typ1
))
7137 error_init (init_loc
, "array of inappropriate type initialized "
7138 "from string constant");
7139 return error_mark_node
;
7143 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7144 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7145 below and handle as a constructor. */
7146 if (code
== VECTOR_TYPE
7147 && VECTOR_TYPE_P (TREE_TYPE (inside_init
))
7148 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
7149 && TREE_CONSTANT (inside_init
))
7151 if (TREE_CODE (inside_init
) == VECTOR_CST
7152 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7153 TYPE_MAIN_VARIANT (type
)))
7156 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
7158 unsigned HOST_WIDE_INT ix
;
7160 bool constant_p
= true;
7162 /* Iterate through elements and check if all constructor
7163 elements are *_CSTs. */
7164 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
7165 if (!CONSTANT_CLASS_P (value
))
7172 return build_vector_from_ctor (type
,
7173 CONSTRUCTOR_ELTS (inside_init
));
7177 if (warn_sequence_point
)
7178 verify_sequence_points (inside_init
);
7180 /* Any type can be initialized
7181 from an expression of the same type, optionally with braces. */
7183 if (inside_init
&& TREE_TYPE (inside_init
) != 0
7184 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7185 TYPE_MAIN_VARIANT (type
))
7186 || (code
== ARRAY_TYPE
7187 && comptypes (TREE_TYPE (inside_init
), type
))
7188 || (code
== VECTOR_TYPE
7189 && comptypes (TREE_TYPE (inside_init
), type
))
7190 || (code
== POINTER_TYPE
7191 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
7192 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
7193 TREE_TYPE (type
)))))
7195 if (code
== POINTER_TYPE
)
7197 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
7199 if (TREE_CODE (inside_init
) == STRING_CST
7200 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
7201 inside_init
= array_to_pointer_conversion
7202 (init_loc
, inside_init
);
7205 error_init (init_loc
, "invalid use of non-lvalue array");
7206 return error_mark_node
;
7211 if (code
== VECTOR_TYPE
)
7212 /* Although the types are compatible, we may require a
7214 inside_init
= convert (type
, inside_init
);
7216 if (require_constant
7217 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
7219 /* As an extension, allow initializing objects with static storage
7220 duration with compound literals (which are then treated just as
7221 the brace enclosed list they contain). Also allow this for
7222 vectors, as we can only assign them with compound literals. */
7223 if (flag_isoc99
&& code
!= VECTOR_TYPE
)
7224 pedwarn_init (init_loc
, OPT_Wpedantic
, "initializer element "
7226 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
7227 inside_init
= DECL_INITIAL (decl
);
7230 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
7231 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
7233 error_init (init_loc
, "array initialized from non-constant array "
7235 return error_mark_node
;
7238 /* Compound expressions can only occur here if -Wpedantic or
7239 -pedantic-errors is specified. In the later case, we always want
7240 an error. In the former case, we simply want a warning. */
7241 if (require_constant
&& pedantic
7242 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
7245 = valid_compound_expr_initializer (inside_init
,
7246 TREE_TYPE (inside_init
));
7247 if (inside_init
== error_mark_node
)
7248 error_init (init_loc
, "initializer element is not constant");
7250 pedwarn_init (init_loc
, OPT_Wpedantic
,
7251 "initializer element is not constant");
7252 if (flag_pedantic_errors
)
7253 inside_init
= error_mark_node
;
7255 else if (require_constant
7256 && !initializer_constant_valid_p (inside_init
,
7257 TREE_TYPE (inside_init
)))
7259 error_init (init_loc
, "initializer element is not constant");
7260 inside_init
= error_mark_node
;
7262 else if (require_constant
&& !maybe_const
)
7263 pedwarn_init (init_loc
, OPT_Wpedantic
,
7264 "initializer element is not a constant expression");
7266 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7267 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
7268 inside_init
= convert_for_assignment (init_loc
, UNKNOWN_LOCATION
,
7269 type
, inside_init
, origtype
,
7270 ic_init
, null_pointer_constant
,
7271 NULL_TREE
, NULL_TREE
, 0);
7275 /* Handle scalar types, including conversions. */
7277 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
7278 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
7279 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
)
7281 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
7282 && (TREE_CODE (init
) == STRING_CST
7283 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
7284 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
7286 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
7289 = convert_for_assignment (init_loc
, UNKNOWN_LOCATION
, type
,
7290 inside_init
, origtype
, ic_init
,
7291 null_pointer_constant
, NULL_TREE
, NULL_TREE
,
7294 /* Check to see if we have already given an error message. */
7295 if (inside_init
== error_mark_node
)
7297 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
7299 error_init (init_loc
, "initializer element is not constant");
7300 inside_init
= error_mark_node
;
7302 else if (require_constant
7303 && !initializer_constant_valid_p (inside_init
,
7304 TREE_TYPE (inside_init
)))
7306 error_init (init_loc
, "initializer element is not computable at "
7308 inside_init
= error_mark_node
;
7310 else if (require_constant
&& !maybe_const
)
7311 pedwarn_init (init_loc
, OPT_Wpedantic
,
7312 "initializer element is not a constant expression");
7317 /* Come here only for records and arrays. */
7319 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
7321 error_init (init_loc
, "variable-sized object may not be initialized");
7322 return error_mark_node
;
7325 error_init (init_loc
, "invalid initializer");
7326 return error_mark_node
;
7329 /* Handle initializers that use braces. */
7331 /* Type of object we are accumulating a constructor for.
7332 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7333 static tree constructor_type
;
7335 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7337 static tree constructor_fields
;
7339 /* For an ARRAY_TYPE, this is the specified index
7340 at which to store the next element we get. */
7341 static tree constructor_index
;
7343 /* For an ARRAY_TYPE, this is the maximum index. */
7344 static tree constructor_max_index
;
7346 /* For a RECORD_TYPE, this is the first field not yet written out. */
7347 static tree constructor_unfilled_fields
;
7349 /* For an ARRAY_TYPE, this is the index of the first element
7350 not yet written out. */
7351 static tree constructor_unfilled_index
;
7353 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7354 This is so we can generate gaps between fields, when appropriate. */
7355 static tree constructor_bit_index
;
7357 /* If we are saving up the elements rather than allocating them,
7358 this is the list of elements so far (in reverse order,
7359 most recent first). */
7360 static vec
<constructor_elt
, va_gc
> *constructor_elements
;
7362 /* 1 if constructor should be incrementally stored into a constructor chain,
7363 0 if all the elements should be kept in AVL tree. */
7364 static int constructor_incremental
;
7366 /* 1 if so far this constructor's elements are all compile-time constants. */
7367 static int constructor_constant
;
7369 /* 1 if so far this constructor's elements are all valid address constants. */
7370 static int constructor_simple
;
7372 /* 1 if this constructor has an element that cannot be part of a
7373 constant expression. */
7374 static int constructor_nonconst
;
7376 /* 1 if this constructor is erroneous so far. */
7377 static int constructor_erroneous
;
7379 /* 1 if this constructor is the universal zero initializer { 0 }. */
7380 static int constructor_zeroinit
;
7382 /* Structure for managing pending initializer elements, organized as an
7387 struct init_node
*left
, *right
;
7388 struct init_node
*parent
;
7395 /* Tree of pending elements at this constructor level.
7396 These are elements encountered out of order
7397 which belong at places we haven't reached yet in actually
7399 Will never hold tree nodes across GC runs. */
7400 static struct init_node
*constructor_pending_elts
;
7402 /* The SPELLING_DEPTH of this constructor. */
7403 static int constructor_depth
;
7405 /* DECL node for which an initializer is being read.
7406 0 means we are reading a constructor expression
7407 such as (struct foo) {...}. */
7408 static tree constructor_decl
;
7410 /* Nonzero if this is an initializer for a top-level decl. */
7411 static int constructor_top_level
;
7413 /* Nonzero if there were any member designators in this initializer. */
7414 static int constructor_designated
;
7416 /* Nesting depth of designator list. */
7417 static int designator_depth
;
7419 /* Nonzero if there were diagnosed errors in this designator list. */
7420 static int designator_erroneous
;
7423 /* This stack has a level for each implicit or explicit level of
7424 structuring in the initializer, including the outermost one. It
7425 saves the values of most of the variables above. */
7427 struct constructor_range_stack
;
7429 struct constructor_stack
7431 struct constructor_stack
*next
;
7436 tree unfilled_index
;
7437 tree unfilled_fields
;
7439 vec
<constructor_elt
, va_gc
> *elements
;
7440 struct init_node
*pending_elts
;
7443 /* If value nonzero, this value should replace the entire
7444 constructor at this level. */
7445 struct c_expr replacement_value
;
7446 struct constructor_range_stack
*range_stack
;
7455 int designator_depth
;
7458 static struct constructor_stack
*constructor_stack
;
7460 /* This stack represents designators from some range designator up to
7461 the last designator in the list. */
7463 struct constructor_range_stack
7465 struct constructor_range_stack
*next
, *prev
;
7466 struct constructor_stack
*stack
;
7473 static struct constructor_range_stack
*constructor_range_stack
;
7475 /* This stack records separate initializers that are nested.
7476 Nested initializers can't happen in ANSI C, but GNU C allows them
7477 in cases like { ... (struct foo) { ... } ... }. */
7479 struct initializer_stack
7481 struct initializer_stack
*next
;
7483 struct constructor_stack
*constructor_stack
;
7484 struct constructor_range_stack
*constructor_range_stack
;
7485 vec
<constructor_elt
, va_gc
> *elements
;
7486 struct spelling
*spelling
;
7487 struct spelling
*spelling_base
;
7490 char require_constant_value
;
7491 char require_constant_elements
;
7494 static struct initializer_stack
*initializer_stack
;
7496 /* Prepare to parse and output the initializer for variable DECL. */
7499 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
)
7502 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
7504 p
->decl
= constructor_decl
;
7505 p
->require_constant_value
= require_constant_value
;
7506 p
->require_constant_elements
= require_constant_elements
;
7507 p
->constructor_stack
= constructor_stack
;
7508 p
->constructor_range_stack
= constructor_range_stack
;
7509 p
->elements
= constructor_elements
;
7510 p
->spelling
= spelling
;
7511 p
->spelling_base
= spelling_base
;
7512 p
->spelling_size
= spelling_size
;
7513 p
->top_level
= constructor_top_level
;
7514 p
->next
= initializer_stack
;
7515 initializer_stack
= p
;
7517 constructor_decl
= decl
;
7518 constructor_designated
= 0;
7519 constructor_top_level
= top_level
;
7521 if (decl
!= 0 && decl
!= error_mark_node
)
7523 require_constant_value
= TREE_STATIC (decl
);
7524 require_constant_elements
7525 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
7526 /* For a scalar, you can always use any value to initialize,
7527 even within braces. */
7528 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)));
7529 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
7533 require_constant_value
= 0;
7534 require_constant_elements
= 0;
7535 locus
= _("(anonymous)");
7538 constructor_stack
= 0;
7539 constructor_range_stack
= 0;
7541 found_missing_braces
= 0;
7545 RESTORE_SPELLING_DEPTH (0);
7548 push_string (locus
);
7554 struct initializer_stack
*p
= initializer_stack
;
7556 /* Free the whole constructor stack of this initializer. */
7557 while (constructor_stack
)
7559 struct constructor_stack
*q
= constructor_stack
;
7560 constructor_stack
= q
->next
;
7564 gcc_assert (!constructor_range_stack
);
7566 /* Pop back to the data of the outer initializer (if any). */
7567 free (spelling_base
);
7569 constructor_decl
= p
->decl
;
7570 require_constant_value
= p
->require_constant_value
;
7571 require_constant_elements
= p
->require_constant_elements
;
7572 constructor_stack
= p
->constructor_stack
;
7573 constructor_range_stack
= p
->constructor_range_stack
;
7574 constructor_elements
= p
->elements
;
7575 spelling
= p
->spelling
;
7576 spelling_base
= p
->spelling_base
;
7577 spelling_size
= p
->spelling_size
;
7578 constructor_top_level
= p
->top_level
;
7579 initializer_stack
= p
->next
;
7583 /* Call here when we see the initializer is surrounded by braces.
7584 This is instead of a call to push_init_level;
7585 it is matched by a call to pop_init_level.
7587 TYPE is the type to initialize, for a constructor expression.
7588 For an initializer for a decl, TYPE is zero. */
7591 really_start_incremental_init (tree type
)
7593 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
7596 type
= TREE_TYPE (constructor_decl
);
7598 if (VECTOR_TYPE_P (type
)
7599 && TYPE_VECTOR_OPAQUE (type
))
7600 error ("opaque vector types cannot be initialized");
7602 p
->type
= constructor_type
;
7603 p
->fields
= constructor_fields
;
7604 p
->index
= constructor_index
;
7605 p
->max_index
= constructor_max_index
;
7606 p
->unfilled_index
= constructor_unfilled_index
;
7607 p
->unfilled_fields
= constructor_unfilled_fields
;
7608 p
->bit_index
= constructor_bit_index
;
7609 p
->elements
= constructor_elements
;
7610 p
->constant
= constructor_constant
;
7611 p
->simple
= constructor_simple
;
7612 p
->nonconst
= constructor_nonconst
;
7613 p
->erroneous
= constructor_erroneous
;
7614 p
->pending_elts
= constructor_pending_elts
;
7615 p
->depth
= constructor_depth
;
7616 p
->replacement_value
.value
= 0;
7617 p
->replacement_value
.original_code
= ERROR_MARK
;
7618 p
->replacement_value
.original_type
= NULL
;
7622 p
->incremental
= constructor_incremental
;
7623 p
->designated
= constructor_designated
;
7624 p
->designator_depth
= designator_depth
;
7626 constructor_stack
= p
;
7628 constructor_constant
= 1;
7629 constructor_simple
= 1;
7630 constructor_nonconst
= 0;
7631 constructor_depth
= SPELLING_DEPTH ();
7632 constructor_elements
= NULL
;
7633 constructor_pending_elts
= 0;
7634 constructor_type
= type
;
7635 constructor_incremental
= 1;
7636 constructor_designated
= 0;
7637 constructor_zeroinit
= 1;
7638 designator_depth
= 0;
7639 designator_erroneous
= 0;
7641 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
7643 constructor_fields
= TYPE_FIELDS (constructor_type
);
7644 /* Skip any nameless bit fields at the beginning. */
7645 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7646 && DECL_NAME (constructor_fields
) == 0)
7647 constructor_fields
= DECL_CHAIN (constructor_fields
);
7649 constructor_unfilled_fields
= constructor_fields
;
7650 constructor_bit_index
= bitsize_zero_node
;
7652 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7654 if (TYPE_DOMAIN (constructor_type
))
7656 constructor_max_index
7657 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7659 /* Detect non-empty initializations of zero-length arrays. */
7660 if (constructor_max_index
== NULL_TREE
7661 && TYPE_SIZE (constructor_type
))
7662 constructor_max_index
= integer_minus_one_node
;
7664 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7665 to initialize VLAs will cause a proper error; avoid tree
7666 checking errors as well by setting a safe value. */
7667 if (constructor_max_index
7668 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7669 constructor_max_index
= integer_minus_one_node
;
7672 = convert (bitsizetype
,
7673 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7677 constructor_index
= bitsize_zero_node
;
7678 constructor_max_index
= NULL_TREE
;
7681 constructor_unfilled_index
= constructor_index
;
7683 else if (VECTOR_TYPE_P (constructor_type
))
7685 /* Vectors are like simple fixed-size arrays. */
7686 constructor_max_index
=
7687 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7688 constructor_index
= bitsize_zero_node
;
7689 constructor_unfilled_index
= constructor_index
;
7693 /* Handle the case of int x = {5}; */
7694 constructor_fields
= constructor_type
;
7695 constructor_unfilled_fields
= constructor_type
;
7699 /* Called when we see an open brace for a nested initializer. Finish
7700 off any pending levels with implicit braces. */
7702 finish_implicit_inits (location_t loc
, struct obstack
*braced_init_obstack
)
7704 while (constructor_stack
->implicit
)
7706 if (RECORD_OR_UNION_TYPE_P (constructor_type
)
7707 && constructor_fields
== 0)
7708 process_init_element (input_location
,
7709 pop_init_level (loc
, 1, braced_init_obstack
),
7710 true, braced_init_obstack
);
7711 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
7712 && constructor_max_index
7713 && tree_int_cst_lt (constructor_max_index
,
7715 process_init_element (input_location
,
7716 pop_init_level (loc
, 1, braced_init_obstack
),
7717 true, braced_init_obstack
);
7723 /* Push down into a subobject, for initialization.
7724 If this is for an explicit set of braces, IMPLICIT is 0.
7725 If it is because the next element belongs at a lower level,
7726 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7729 push_init_level (location_t loc
, int implicit
,
7730 struct obstack
*braced_init_obstack
)
7732 struct constructor_stack
*p
;
7733 tree value
= NULL_TREE
;
7735 /* Unless this is an explicit brace, we need to preserve previous
7739 if (RECORD_OR_UNION_TYPE_P (constructor_type
) && constructor_fields
)
7740 value
= find_init_member (constructor_fields
, braced_init_obstack
);
7741 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7742 value
= find_init_member (constructor_index
, braced_init_obstack
);
7745 p
= XNEW (struct constructor_stack
);
7746 p
->type
= constructor_type
;
7747 p
->fields
= constructor_fields
;
7748 p
->index
= constructor_index
;
7749 p
->max_index
= constructor_max_index
;
7750 p
->unfilled_index
= constructor_unfilled_index
;
7751 p
->unfilled_fields
= constructor_unfilled_fields
;
7752 p
->bit_index
= constructor_bit_index
;
7753 p
->elements
= constructor_elements
;
7754 p
->constant
= constructor_constant
;
7755 p
->simple
= constructor_simple
;
7756 p
->nonconst
= constructor_nonconst
;
7757 p
->erroneous
= constructor_erroneous
;
7758 p
->pending_elts
= constructor_pending_elts
;
7759 p
->depth
= constructor_depth
;
7760 p
->replacement_value
.value
= 0;
7761 p
->replacement_value
.original_code
= ERROR_MARK
;
7762 p
->replacement_value
.original_type
= NULL
;
7763 p
->implicit
= implicit
;
7765 p
->incremental
= constructor_incremental
;
7766 p
->designated
= constructor_designated
;
7767 p
->designator_depth
= designator_depth
;
7768 p
->next
= constructor_stack
;
7770 constructor_stack
= p
;
7772 constructor_constant
= 1;
7773 constructor_simple
= 1;
7774 constructor_nonconst
= 0;
7775 constructor_depth
= SPELLING_DEPTH ();
7776 constructor_elements
= NULL
;
7777 constructor_incremental
= 1;
7778 constructor_designated
= 0;
7779 constructor_pending_elts
= 0;
7782 p
->range_stack
= constructor_range_stack
;
7783 constructor_range_stack
= 0;
7784 designator_depth
= 0;
7785 designator_erroneous
= 0;
7788 /* Don't die if an entire brace-pair level is superfluous
7789 in the containing level. */
7790 if (constructor_type
== 0)
7792 else if (RECORD_OR_UNION_TYPE_P (constructor_type
))
7794 /* Don't die if there are extra init elts at the end. */
7795 if (constructor_fields
== 0)
7796 constructor_type
= 0;
7799 constructor_type
= TREE_TYPE (constructor_fields
);
7800 push_member_name (constructor_fields
);
7801 constructor_depth
++;
7803 /* If upper initializer is designated, then mark this as
7804 designated too to prevent bogus warnings. */
7805 constructor_designated
= p
->designated
;
7807 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7809 constructor_type
= TREE_TYPE (constructor_type
);
7810 push_array_bounds (tree_to_uhwi (constructor_index
));
7811 constructor_depth
++;
7814 if (constructor_type
== 0)
7816 error_init (loc
, "extra brace group at end of initializer");
7817 constructor_fields
= 0;
7818 constructor_unfilled_fields
= 0;
7822 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
7824 constructor_constant
= TREE_CONSTANT (value
);
7825 constructor_simple
= TREE_STATIC (value
);
7826 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
7827 constructor_elements
= CONSTRUCTOR_ELTS (value
);
7828 if (!vec_safe_is_empty (constructor_elements
)
7829 && (TREE_CODE (constructor_type
) == RECORD_TYPE
7830 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
7831 set_nonincremental_init (braced_init_obstack
);
7835 found_missing_braces
= 1;
7837 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
7839 constructor_fields
= TYPE_FIELDS (constructor_type
);
7840 /* Skip any nameless bit fields at the beginning. */
7841 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7842 && DECL_NAME (constructor_fields
) == 0)
7843 constructor_fields
= DECL_CHAIN (constructor_fields
);
7845 constructor_unfilled_fields
= constructor_fields
;
7846 constructor_bit_index
= bitsize_zero_node
;
7848 else if (VECTOR_TYPE_P (constructor_type
))
7850 /* Vectors are like simple fixed-size arrays. */
7851 constructor_max_index
=
7852 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7853 constructor_index
= bitsize_int (0);
7854 constructor_unfilled_index
= constructor_index
;
7856 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7858 if (TYPE_DOMAIN (constructor_type
))
7860 constructor_max_index
7861 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7863 /* Detect non-empty initializations of zero-length arrays. */
7864 if (constructor_max_index
== NULL_TREE
7865 && TYPE_SIZE (constructor_type
))
7866 constructor_max_index
= integer_minus_one_node
;
7868 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7869 to initialize VLAs will cause a proper error; avoid tree
7870 checking errors as well by setting a safe value. */
7871 if (constructor_max_index
7872 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7873 constructor_max_index
= integer_minus_one_node
;
7876 = convert (bitsizetype
,
7877 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7880 constructor_index
= bitsize_zero_node
;
7882 constructor_unfilled_index
= constructor_index
;
7883 if (value
&& TREE_CODE (value
) == STRING_CST
)
7885 /* We need to split the char/wchar array into individual
7886 characters, so that we don't have to special case it
7888 set_nonincremental_init_from_string (value
, braced_init_obstack
);
7893 if (constructor_type
!= error_mark_node
)
7894 warning_init (input_location
, 0, "braces around scalar initializer");
7895 constructor_fields
= constructor_type
;
7896 constructor_unfilled_fields
= constructor_type
;
7900 /* At the end of an implicit or explicit brace level,
7901 finish up that level of constructor. If a single expression
7902 with redundant braces initialized that level, return the
7903 c_expr structure for that expression. Otherwise, the original_code
7904 element is set to ERROR_MARK.
7905 If we were outputting the elements as they are read, return 0 as the value
7906 from inner levels (process_init_element ignores that),
7907 but return error_mark_node as the value from the outermost level
7908 (that's what we want to put in DECL_INITIAL).
7909 Otherwise, return a CONSTRUCTOR expression as the value. */
7912 pop_init_level (location_t loc
, int implicit
,
7913 struct obstack
*braced_init_obstack
)
7915 struct constructor_stack
*p
;
7918 ret
.original_code
= ERROR_MARK
;
7919 ret
.original_type
= NULL
;
7923 /* When we come to an explicit close brace,
7924 pop any inner levels that didn't have explicit braces. */
7925 while (constructor_stack
->implicit
)
7926 process_init_element (input_location
,
7927 pop_init_level (loc
, 1, braced_init_obstack
),
7928 true, braced_init_obstack
);
7929 gcc_assert (!constructor_range_stack
);
7932 /* Now output all pending elements. */
7933 constructor_incremental
= 1;
7934 output_pending_init_elements (1, braced_init_obstack
);
7936 p
= constructor_stack
;
7938 /* Error for initializing a flexible array member, or a zero-length
7939 array member in an inappropriate context. */
7940 if (constructor_type
&& constructor_fields
7941 && TREE_CODE (constructor_type
) == ARRAY_TYPE
7942 && TYPE_DOMAIN (constructor_type
)
7943 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
7945 /* Silently discard empty initializations. The parser will
7946 already have pedwarned for empty brackets. */
7947 if (integer_zerop (constructor_unfilled_index
))
7948 constructor_type
= NULL_TREE
;
7951 gcc_assert (!TYPE_SIZE (constructor_type
));
7953 if (constructor_depth
> 2)
7954 error_init (loc
, "initialization of flexible array member in a nested context");
7956 pedwarn_init (loc
, OPT_Wpedantic
,
7957 "initialization of a flexible array member");
7959 /* We have already issued an error message for the existence
7960 of a flexible array member not at the end of the structure.
7961 Discard the initializer so that we do not die later. */
7962 if (DECL_CHAIN (constructor_fields
) != NULL_TREE
)
7963 constructor_type
= NULL_TREE
;
7967 switch (vec_safe_length (constructor_elements
))
7970 /* Initialization with { } counts as zeroinit. */
7971 constructor_zeroinit
= 1;
7974 /* This might be zeroinit as well. */
7975 if (integer_zerop ((*constructor_elements
)[0].value
))
7976 constructor_zeroinit
= 1;
7979 /* If the constructor has more than one element, it can't be { 0 }. */
7980 constructor_zeroinit
= 0;
7984 /* Warn when some structs are initialized with direct aggregation. */
7985 if (!implicit
&& found_missing_braces
&& warn_missing_braces
7986 && !constructor_zeroinit
)
7987 warning_init (loc
, OPT_Wmissing_braces
,
7988 "missing braces around initializer");
7990 /* Warn when some struct elements are implicitly initialized to zero. */
7991 if (warn_missing_field_initializers
7993 && TREE_CODE (constructor_type
) == RECORD_TYPE
7994 && constructor_unfilled_fields
)
7996 /* Do not warn for flexible array members or zero-length arrays. */
7997 while (constructor_unfilled_fields
7998 && (!DECL_SIZE (constructor_unfilled_fields
)
7999 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
8000 constructor_unfilled_fields
= DECL_CHAIN (constructor_unfilled_fields
);
8002 if (constructor_unfilled_fields
8003 /* Do not warn if this level of the initializer uses member
8004 designators; it is likely to be deliberate. */
8005 && !constructor_designated
8006 /* Do not warn about initializing with { 0 } or with { }. */
8007 && !constructor_zeroinit
)
8009 if (warning_at (input_location
, OPT_Wmissing_field_initializers
,
8010 "missing initializer for field %qD of %qT",
8011 constructor_unfilled_fields
,
8013 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields
),
8014 "%qD declared here", constructor_unfilled_fields
);
8018 /* Pad out the end of the structure. */
8019 if (p
->replacement_value
.value
)
8020 /* If this closes a superfluous brace pair,
8021 just pass out the element between them. */
8022 ret
= p
->replacement_value
;
8023 else if (constructor_type
== 0)
8025 else if (!RECORD_OR_UNION_TYPE_P (constructor_type
)
8026 && TREE_CODE (constructor_type
) != ARRAY_TYPE
8027 && !VECTOR_TYPE_P (constructor_type
))
8029 /* A nonincremental scalar initializer--just return
8030 the element, after verifying there is just one. */
8031 if (vec_safe_is_empty (constructor_elements
))
8033 if (!constructor_erroneous
)
8034 error_init (loc
, "empty scalar initializer");
8035 ret
.value
= error_mark_node
;
8037 else if (vec_safe_length (constructor_elements
) != 1)
8039 error_init (loc
, "extra elements in scalar initializer");
8040 ret
.value
= (*constructor_elements
)[0].value
;
8043 ret
.value
= (*constructor_elements
)[0].value
;
8047 if (constructor_erroneous
)
8048 ret
.value
= error_mark_node
;
8051 ret
.value
= build_constructor (constructor_type
,
8052 constructor_elements
);
8053 if (constructor_constant
)
8054 TREE_CONSTANT (ret
.value
) = 1;
8055 if (constructor_constant
&& constructor_simple
)
8056 TREE_STATIC (ret
.value
) = 1;
8057 if (constructor_nonconst
)
8058 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
8062 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
8064 if (constructor_nonconst
)
8065 ret
.original_code
= C_MAYBE_CONST_EXPR
;
8066 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
8067 ret
.original_code
= ERROR_MARK
;
8070 constructor_type
= p
->type
;
8071 constructor_fields
= p
->fields
;
8072 constructor_index
= p
->index
;
8073 constructor_max_index
= p
->max_index
;
8074 constructor_unfilled_index
= p
->unfilled_index
;
8075 constructor_unfilled_fields
= p
->unfilled_fields
;
8076 constructor_bit_index
= p
->bit_index
;
8077 constructor_elements
= p
->elements
;
8078 constructor_constant
= p
->constant
;
8079 constructor_simple
= p
->simple
;
8080 constructor_nonconst
= p
->nonconst
;
8081 constructor_erroneous
= p
->erroneous
;
8082 constructor_incremental
= p
->incremental
;
8083 constructor_designated
= p
->designated
;
8084 designator_depth
= p
->designator_depth
;
8085 constructor_pending_elts
= p
->pending_elts
;
8086 constructor_depth
= p
->depth
;
8088 constructor_range_stack
= p
->range_stack
;
8089 RESTORE_SPELLING_DEPTH (constructor_depth
);
8091 constructor_stack
= p
->next
;
8094 if (ret
.value
== 0 && constructor_stack
== 0)
8095 ret
.value
= error_mark_node
;
8099 /* Common handling for both array range and field name designators.
8100 ARRAY argument is nonzero for array ranges. Returns zero for success. */
8103 set_designator (location_t loc
, int array
,
8104 struct obstack
*braced_init_obstack
)
8107 enum tree_code subcode
;
8109 /* Don't die if an entire brace-pair level is superfluous
8110 in the containing level. */
8111 if (constructor_type
== 0)
8114 /* If there were errors in this designator list already, bail out
8116 if (designator_erroneous
)
8119 if (!designator_depth
)
8121 gcc_assert (!constructor_range_stack
);
8123 /* Designator list starts at the level of closest explicit
8125 while (constructor_stack
->implicit
)
8126 process_init_element (input_location
,
8127 pop_init_level (loc
, 1, braced_init_obstack
),
8128 true, braced_init_obstack
);
8129 constructor_designated
= 1;
8133 switch (TREE_CODE (constructor_type
))
8137 subtype
= TREE_TYPE (constructor_fields
);
8138 if (subtype
!= error_mark_node
)
8139 subtype
= TYPE_MAIN_VARIANT (subtype
);
8142 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8148 subcode
= TREE_CODE (subtype
);
8149 if (array
&& subcode
!= ARRAY_TYPE
)
8151 error_init (loc
, "array index in non-array initializer");
8154 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
8156 error_init (loc
, "field name not in record or union initializer");
8160 constructor_designated
= 1;
8161 finish_implicit_inits (loc
, braced_init_obstack
);
8162 push_init_level (loc
, 2, braced_init_obstack
);
8166 /* If there are range designators in designator list, push a new designator
8167 to constructor_range_stack. RANGE_END is end of such stack range or
8168 NULL_TREE if there is no range designator at this level. */
8171 push_range_stack (tree range_end
, struct obstack
* braced_init_obstack
)
8173 struct constructor_range_stack
*p
;
8175 p
= (struct constructor_range_stack
*)
8176 obstack_alloc (braced_init_obstack
,
8177 sizeof (struct constructor_range_stack
));
8178 p
->prev
= constructor_range_stack
;
8180 p
->fields
= constructor_fields
;
8181 p
->range_start
= constructor_index
;
8182 p
->index
= constructor_index
;
8183 p
->stack
= constructor_stack
;
8184 p
->range_end
= range_end
;
8185 if (constructor_range_stack
)
8186 constructor_range_stack
->next
= p
;
8187 constructor_range_stack
= p
;
8190 /* Within an array initializer, specify the next index to be initialized.
8191 FIRST is that index. If LAST is nonzero, then initialize a range
8192 of indices, running from FIRST through LAST. */
8195 set_init_index (location_t loc
, tree first
, tree last
,
8196 struct obstack
*braced_init_obstack
)
8198 if (set_designator (loc
, 1, braced_init_obstack
))
8201 designator_erroneous
= 1;
8203 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
8204 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
8206 error_init (loc
, "array index in initializer not of integer type");
8210 if (TREE_CODE (first
) != INTEGER_CST
)
8212 first
= c_fully_fold (first
, false, NULL
);
8213 if (TREE_CODE (first
) == INTEGER_CST
)
8214 pedwarn_init (loc
, OPT_Wpedantic
,
8215 "array index in initializer is not "
8216 "an integer constant expression");
8219 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
8221 last
= c_fully_fold (last
, false, NULL
);
8222 if (TREE_CODE (last
) == INTEGER_CST
)
8223 pedwarn_init (loc
, OPT_Wpedantic
,
8224 "array index in initializer is not "
8225 "an integer constant expression");
8228 if (TREE_CODE (first
) != INTEGER_CST
)
8229 error_init (loc
, "nonconstant array index in initializer");
8230 else if (last
!= 0 && TREE_CODE (last
) != INTEGER_CST
)
8231 error_init (loc
, "nonconstant array index in initializer");
8232 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
8233 error_init (loc
, "array index in non-array initializer");
8234 else if (tree_int_cst_sgn (first
) == -1)
8235 error_init (loc
, "array index in initializer exceeds array bounds");
8236 else if (constructor_max_index
8237 && tree_int_cst_lt (constructor_max_index
, first
))
8238 error_init (loc
, "array index in initializer exceeds array bounds");
8241 constant_expression_warning (first
);
8243 constant_expression_warning (last
);
8244 constructor_index
= convert (bitsizetype
, first
);
8245 if (tree_int_cst_lt (constructor_index
, first
))
8247 constructor_index
= copy_node (constructor_index
);
8248 TREE_OVERFLOW (constructor_index
) = 1;
8253 if (tree_int_cst_equal (first
, last
))
8255 else if (tree_int_cst_lt (last
, first
))
8257 error_init (loc
, "empty index range in initializer");
8262 last
= convert (bitsizetype
, last
);
8263 if (constructor_max_index
!= 0
8264 && tree_int_cst_lt (constructor_max_index
, last
))
8266 error_init (loc
, "array index range in initializer exceeds "
8274 designator_erroneous
= 0;
8275 if (constructor_range_stack
|| last
)
8276 push_range_stack (last
, braced_init_obstack
);
8280 /* Within a struct initializer, specify the next field to be initialized. */
8283 set_init_label (location_t loc
, tree fieldname
, location_t fieldname_loc
,
8284 struct obstack
*braced_init_obstack
)
8288 if (set_designator (loc
, 0, braced_init_obstack
))
8291 designator_erroneous
= 1;
8293 if (!RECORD_OR_UNION_TYPE_P (constructor_type
))
8295 error_init (loc
, "field name not in record or union initializer");
8299 field
= lookup_field (constructor_type
, fieldname
);
8303 tree guessed_id
= lookup_field_fuzzy (constructor_type
, fieldname
);
8306 gcc_rich_location
rich_loc (fieldname_loc
);
8307 rich_loc
.add_fixit_misspelled_id (fieldname_loc
, guessed_id
);
8310 "%qT has no member named %qE; did you mean %qE?",
8311 constructor_type
, fieldname
, guessed_id
);
8314 error_at (fieldname_loc
, "%qT has no member named %qE",
8315 constructor_type
, fieldname
);
8320 constructor_fields
= TREE_VALUE (field
);
8322 designator_erroneous
= 0;
8323 if (constructor_range_stack
)
8324 push_range_stack (NULL_TREE
, braced_init_obstack
);
8325 field
= TREE_CHAIN (field
);
8328 if (set_designator (loc
, 0, braced_init_obstack
))
8332 while (field
!= NULL_TREE
);
8335 /* Add a new initializer to the tree of pending initializers. PURPOSE
8336 identifies the initializer, either array index or field in a structure.
8337 VALUE is the value of that index or field. If ORIGTYPE is not
8338 NULL_TREE, it is the original type of VALUE.
8340 IMPLICIT is true if value comes from pop_init_level (1),
8341 the new initializer has been merged with the existing one
8342 and thus no warnings should be emitted about overriding an
8343 existing initializer. */
8346 add_pending_init (location_t loc
, tree purpose
, tree value
, tree origtype
,
8347 bool implicit
, struct obstack
*braced_init_obstack
)
8349 struct init_node
*p
, **q
, *r
;
8351 q
= &constructor_pending_elts
;
8354 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8359 if (tree_int_cst_lt (purpose
, p
->purpose
))
8361 else if (tree_int_cst_lt (p
->purpose
, purpose
))
8367 if (TREE_SIDE_EFFECTS (p
->value
))
8368 warning_init (loc
, OPT_Woverride_init_side_effects
,
8369 "initialized field with side-effects "
8371 else if (warn_override_init
)
8372 warning_init (loc
, OPT_Woverride_init
,
8373 "initialized field overwritten");
8376 p
->origtype
= origtype
;
8385 bitpos
= bit_position (purpose
);
8389 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
8391 else if (p
->purpose
!= purpose
)
8397 if (TREE_SIDE_EFFECTS (p
->value
))
8398 warning_init (loc
, OPT_Woverride_init_side_effects
,
8399 "initialized field with side-effects "
8401 else if (warn_override_init
)
8402 warning_init (loc
, OPT_Woverride_init
,
8403 "initialized field overwritten");
8406 p
->origtype
= origtype
;
8412 r
= (struct init_node
*) obstack_alloc (braced_init_obstack
,
8413 sizeof (struct init_node
));
8414 r
->purpose
= purpose
;
8416 r
->origtype
= origtype
;
8426 struct init_node
*s
;
8430 if (p
->balance
== 0)
8432 else if (p
->balance
< 0)
8439 p
->left
->parent
= p
;
8456 constructor_pending_elts
= r
;
8461 struct init_node
*t
= r
->right
;
8465 r
->right
->parent
= r
;
8470 p
->left
->parent
= p
;
8473 p
->balance
= t
->balance
< 0;
8474 r
->balance
= -(t
->balance
> 0);
8489 constructor_pending_elts
= t
;
8495 /* p->balance == +1; growth of left side balances the node. */
8500 else /* r == p->right */
8502 if (p
->balance
== 0)
8503 /* Growth propagation from right side. */
8505 else if (p
->balance
> 0)
8512 p
->right
->parent
= p
;
8529 constructor_pending_elts
= r
;
8531 else /* r->balance == -1 */
8534 struct init_node
*t
= r
->left
;
8538 r
->left
->parent
= r
;
8543 p
->right
->parent
= p
;
8546 r
->balance
= (t
->balance
< 0);
8547 p
->balance
= -(t
->balance
> 0);
8562 constructor_pending_elts
= t
;
8568 /* p->balance == -1; growth of right side balances the node. */
8579 /* Build AVL tree from a sorted chain. */
8582 set_nonincremental_init (struct obstack
* braced_init_obstack
)
8584 unsigned HOST_WIDE_INT ix
;
8587 if (TREE_CODE (constructor_type
) != RECORD_TYPE
8588 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
8591 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
8592 add_pending_init (input_location
, index
, value
, NULL_TREE
, true,
8593 braced_init_obstack
);
8594 constructor_elements
= NULL
;
8595 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8597 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
8598 /* Skip any nameless bit fields at the beginning. */
8599 while (constructor_unfilled_fields
!= 0
8600 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8601 && DECL_NAME (constructor_unfilled_fields
) == 0)
8602 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
8605 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8607 if (TYPE_DOMAIN (constructor_type
))
8608 constructor_unfilled_index
8609 = convert (bitsizetype
,
8610 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
8612 constructor_unfilled_index
= bitsize_zero_node
;
8614 constructor_incremental
= 0;
8617 /* Build AVL tree from a string constant. */
8620 set_nonincremental_init_from_string (tree str
,
8621 struct obstack
* braced_init_obstack
)
8623 tree value
, purpose
, type
;
8624 HOST_WIDE_INT val
[2];
8625 const char *p
, *end
;
8626 int byte
, wchar_bytes
, charwidth
, bitpos
;
8628 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
8630 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
8631 charwidth
= TYPE_PRECISION (char_type_node
);
8632 gcc_assert ((size_t) wchar_bytes
* charwidth
8633 <= ARRAY_SIZE (val
) * HOST_BITS_PER_WIDE_INT
);
8634 type
= TREE_TYPE (constructor_type
);
8635 p
= TREE_STRING_POINTER (str
);
8636 end
= p
+ TREE_STRING_LENGTH (str
);
8638 for (purpose
= bitsize_zero_node
;
8640 && !(constructor_max_index
8641 && tree_int_cst_lt (constructor_max_index
, purpose
));
8642 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
8644 if (wchar_bytes
== 1)
8646 val
[0] = (unsigned char) *p
++;
8653 for (byte
= 0; byte
< wchar_bytes
; byte
++)
8655 if (BYTES_BIG_ENDIAN
)
8656 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
8658 bitpos
= byte
* charwidth
;
8659 val
[bitpos
/ HOST_BITS_PER_WIDE_INT
]
8660 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
8661 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
8665 if (!TYPE_UNSIGNED (type
))
8667 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
8668 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
8670 if (val
[0] & (HOST_WIDE_INT_1
<< (bitpos
- 1)))
8672 val
[0] |= HOST_WIDE_INT_M1U
<< bitpos
;
8676 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
8681 else if (val
[1] & (HOST_WIDE_INT_1
8682 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
8683 val
[1] |= HOST_WIDE_INT_M1U
<< (bitpos
- HOST_BITS_PER_WIDE_INT
);
8686 value
= wide_int_to_tree (type
,
8687 wide_int::from_array (val
, 2,
8688 HOST_BITS_PER_WIDE_INT
* 2));
8689 add_pending_init (input_location
, purpose
, value
, NULL_TREE
, true,
8690 braced_init_obstack
);
8693 constructor_incremental
= 0;
8696 /* Return value of FIELD in pending initializer or zero if the field was
8697 not initialized yet. */
8700 find_init_member (tree field
, struct obstack
* braced_init_obstack
)
8702 struct init_node
*p
;
8704 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8706 if (constructor_incremental
8707 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8708 set_nonincremental_init (braced_init_obstack
);
8710 p
= constructor_pending_elts
;
8713 if (tree_int_cst_lt (field
, p
->purpose
))
8715 else if (tree_int_cst_lt (p
->purpose
, field
))
8721 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8723 tree bitpos
= bit_position (field
);
8725 if (constructor_incremental
8726 && (!constructor_unfilled_fields
8727 || tree_int_cst_lt (bitpos
,
8728 bit_position (constructor_unfilled_fields
))))
8729 set_nonincremental_init (braced_init_obstack
);
8731 p
= constructor_pending_elts
;
8734 if (field
== p
->purpose
)
8736 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
8742 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8744 if (!vec_safe_is_empty (constructor_elements
)
8745 && (constructor_elements
->last ().index
== field
))
8746 return constructor_elements
->last ().value
;
8751 /* "Output" the next constructor element.
8752 At top level, really output it to assembler code now.
8753 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8754 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8755 TYPE is the data type that the containing data type wants here.
8756 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8757 If VALUE is a string constant, STRICT_STRING is true if it is
8758 unparenthesized or we should not warn here for it being parenthesized.
8759 For other types of VALUE, STRICT_STRING is not used.
8761 PENDING if non-nil means output pending elements that belong
8762 right after this element. (PENDING is normally 1;
8763 it is 0 while outputting pending elements, to avoid recursion.)
8765 IMPLICIT is true if value comes from pop_init_level (1),
8766 the new initializer has been merged with the existing one
8767 and thus no warnings should be emitted about overriding an
8768 existing initializer. */
8771 output_init_element (location_t loc
, tree value
, tree origtype
,
8772 bool strict_string
, tree type
, tree field
, int pending
,
8773 bool implicit
, struct obstack
* braced_init_obstack
)
8775 tree semantic_type
= NULL_TREE
;
8776 bool maybe_const
= true;
8779 if (type
== error_mark_node
|| value
== error_mark_node
)
8781 constructor_erroneous
= 1;
8784 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
8785 && (TREE_CODE (value
) == STRING_CST
8786 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
8787 && !(TREE_CODE (value
) == STRING_CST
8788 && TREE_CODE (type
) == ARRAY_TYPE
8789 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
8790 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
8791 TYPE_MAIN_VARIANT (type
)))
8792 value
= array_to_pointer_conversion (input_location
, value
);
8794 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
8795 && require_constant_value
&& pending
)
8797 /* As an extension, allow initializing objects with static storage
8798 duration with compound literals (which are then treated just as
8799 the brace enclosed list they contain). */
8801 pedwarn_init (loc
, OPT_Wpedantic
, "initializer element is not "
8803 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
8804 value
= DECL_INITIAL (decl
);
8807 npc
= null_pointer_constant_p (value
);
8808 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
8810 semantic_type
= TREE_TYPE (value
);
8811 value
= TREE_OPERAND (value
, 0);
8813 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
8815 if (value
== error_mark_node
)
8816 constructor_erroneous
= 1;
8817 else if (!TREE_CONSTANT (value
))
8818 constructor_constant
= 0;
8819 else if (!initializer_constant_valid_p (value
,
8821 AGGREGATE_TYPE_P (constructor_type
)
8822 && TYPE_REVERSE_STORAGE_ORDER
8824 || (RECORD_OR_UNION_TYPE_P (constructor_type
)
8825 && DECL_C_BIT_FIELD (field
)
8826 && TREE_CODE (value
) != INTEGER_CST
))
8827 constructor_simple
= 0;
8829 constructor_nonconst
= 1;
8831 /* Digest the initializer and issue any errors about incompatible
8832 types before issuing errors about non-constant initializers. */
8833 tree new_value
= value
;
8835 new_value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
8836 new_value
= digest_init (loc
, type
, new_value
, origtype
, npc
, strict_string
,
8837 require_constant_value
);
8838 if (new_value
== error_mark_node
)
8840 constructor_erroneous
= 1;
8843 if (require_constant_value
|| require_constant_elements
)
8844 constant_expression_warning (new_value
);
8846 /* Proceed to check the constness of the original initializer. */
8847 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
8849 if (require_constant_value
)
8851 error_init (loc
, "initializer element is not constant");
8852 value
= error_mark_node
;
8854 else if (require_constant_elements
)
8855 pedwarn (loc
, OPT_Wpedantic
,
8856 "initializer element is not computable at load time");
8858 else if (!maybe_const
8859 && (require_constant_value
|| require_constant_elements
))
8860 pedwarn_init (loc
, OPT_Wpedantic
,
8861 "initializer element is not a constant expression");
8863 /* Issue -Wc++-compat warnings about initializing a bitfield with
8866 && field
!= NULL_TREE
8867 && TREE_CODE (field
) == FIELD_DECL
8868 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
8869 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
8870 != TYPE_MAIN_VARIANT (type
))
8871 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
8873 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
8874 if (checktype
!= error_mark_node
8875 && (TYPE_MAIN_VARIANT (checktype
)
8876 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
8877 warning_init (loc
, OPT_Wc___compat
,
8878 "enum conversion in initialization is invalid in C++");
8881 /* If this field is empty (and not at the end of structure),
8882 don't do anything other than checking the initializer. */
8884 && (TREE_TYPE (field
) == error_mark_node
8885 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
8886 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
8887 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
8888 || DECL_CHAIN (field
)))))
8891 /* Finally, set VALUE to the initializer value digested above. */
8894 /* If this element doesn't come next in sequence,
8895 put it on constructor_pending_elts. */
8896 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
8897 && (!constructor_incremental
8898 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
8900 if (constructor_incremental
8901 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8902 set_nonincremental_init (braced_init_obstack
);
8904 add_pending_init (loc
, field
, value
, origtype
, implicit
,
8905 braced_init_obstack
);
8908 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8909 && (!constructor_incremental
8910 || field
!= constructor_unfilled_fields
))
8912 /* We do this for records but not for unions. In a union,
8913 no matter which field is specified, it can be initialized
8914 right away since it starts at the beginning of the union. */
8915 if (constructor_incremental
)
8917 if (!constructor_unfilled_fields
)
8918 set_nonincremental_init (braced_init_obstack
);
8921 tree bitpos
, unfillpos
;
8923 bitpos
= bit_position (field
);
8924 unfillpos
= bit_position (constructor_unfilled_fields
);
8926 if (tree_int_cst_lt (bitpos
, unfillpos
))
8927 set_nonincremental_init (braced_init_obstack
);
8931 add_pending_init (loc
, field
, value
, origtype
, implicit
,
8932 braced_init_obstack
);
8935 else if (TREE_CODE (constructor_type
) == UNION_TYPE
8936 && !vec_safe_is_empty (constructor_elements
))
8940 if (TREE_SIDE_EFFECTS (constructor_elements
->last ().value
))
8941 warning_init (loc
, OPT_Woverride_init_side_effects
,
8942 "initialized field with side-effects overwritten");
8943 else if (warn_override_init
)
8944 warning_init (loc
, OPT_Woverride_init
,
8945 "initialized field overwritten");
8948 /* We can have just one union field set. */
8949 constructor_elements
= NULL
;
8952 /* Otherwise, output this element either to
8953 constructor_elements or to the assembler file. */
8955 constructor_elt celt
= {field
, value
};
8956 vec_safe_push (constructor_elements
, celt
);
8958 /* Advance the variable that indicates sequential elements output. */
8959 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8960 constructor_unfilled_index
8961 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
8963 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8965 constructor_unfilled_fields
8966 = DECL_CHAIN (constructor_unfilled_fields
);
8968 /* Skip any nameless bit fields. */
8969 while (constructor_unfilled_fields
!= 0
8970 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8971 && DECL_NAME (constructor_unfilled_fields
) == 0)
8972 constructor_unfilled_fields
=
8973 DECL_CHAIN (constructor_unfilled_fields
);
8975 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8976 constructor_unfilled_fields
= 0;
8978 /* Now output any pending elements which have become next. */
8980 output_pending_init_elements (0, braced_init_obstack
);
8983 /* Output any pending elements which have become next.
8984 As we output elements, constructor_unfilled_{fields,index}
8985 advances, which may cause other elements to become next;
8986 if so, they too are output.
8988 If ALL is 0, we return when there are
8989 no more pending elements to output now.
8991 If ALL is 1, we output space as necessary so that
8992 we can output all the pending elements. */
8994 output_pending_init_elements (int all
, struct obstack
* braced_init_obstack
)
8996 struct init_node
*elt
= constructor_pending_elts
;
9001 /* Look through the whole pending tree.
9002 If we find an element that should be output now,
9003 output it. Otherwise, set NEXT to the element
9004 that comes first among those still pending. */
9009 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9011 if (tree_int_cst_equal (elt
->purpose
,
9012 constructor_unfilled_index
))
9013 output_init_element (input_location
, elt
->value
, elt
->origtype
,
9014 true, TREE_TYPE (constructor_type
),
9015 constructor_unfilled_index
, 0, false,
9016 braced_init_obstack
);
9017 else if (tree_int_cst_lt (constructor_unfilled_index
,
9020 /* Advance to the next smaller node. */
9025 /* We have reached the smallest node bigger than the
9026 current unfilled index. Fill the space first. */
9027 next
= elt
->purpose
;
9033 /* Advance to the next bigger node. */
9038 /* We have reached the biggest node in a subtree. Find
9039 the parent of it, which is the next bigger node. */
9040 while (elt
->parent
&& elt
->parent
->right
== elt
)
9043 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
9046 next
= elt
->purpose
;
9052 else if (RECORD_OR_UNION_TYPE_P (constructor_type
))
9054 tree ctor_unfilled_bitpos
, elt_bitpos
;
9056 /* If the current record is complete we are done. */
9057 if (constructor_unfilled_fields
== 0)
9060 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
9061 elt_bitpos
= bit_position (elt
->purpose
);
9062 /* We can't compare fields here because there might be empty
9063 fields in between. */
9064 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
9066 constructor_unfilled_fields
= elt
->purpose
;
9067 output_init_element (input_location
, elt
->value
, elt
->origtype
,
9068 true, TREE_TYPE (elt
->purpose
),
9069 elt
->purpose
, 0, false,
9070 braced_init_obstack
);
9072 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
9074 /* Advance to the next smaller node. */
9079 /* We have reached the smallest node bigger than the
9080 current unfilled field. Fill the space first. */
9081 next
= elt
->purpose
;
9087 /* Advance to the next bigger node. */
9092 /* We have reached the biggest node in a subtree. Find
9093 the parent of it, which is the next bigger node. */
9094 while (elt
->parent
&& elt
->parent
->right
== elt
)
9098 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
9099 bit_position (elt
->purpose
))))
9101 next
= elt
->purpose
;
9109 /* Ordinarily return, but not if we want to output all
9110 and there are elements left. */
9111 if (!(all
&& next
!= 0))
9114 /* If it's not incremental, just skip over the gap, so that after
9115 jumping to retry we will output the next successive element. */
9116 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
9117 constructor_unfilled_fields
= next
;
9118 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9119 constructor_unfilled_index
= next
;
9121 /* ELT now points to the node in the pending tree with the next
9122 initializer to output. */
9126 /* Add one non-braced element to the current constructor level.
9127 This adjusts the current position within the constructor's type.
9128 This may also start or terminate implicit levels
9129 to handle a partly-braced initializer.
9131 Once this has found the correct level for the new element,
9132 it calls output_init_element.
9134 IMPLICIT is true if value comes from pop_init_level (1),
9135 the new initializer has been merged with the existing one
9136 and thus no warnings should be emitted about overriding an
9137 existing initializer. */
9140 process_init_element (location_t loc
, struct c_expr value
, bool implicit
,
9141 struct obstack
* braced_init_obstack
)
9143 tree orig_value
= value
.value
;
9144 int string_flag
= orig_value
!= 0 && TREE_CODE (orig_value
) == STRING_CST
;
9145 bool strict_string
= value
.original_code
== STRING_CST
;
9146 bool was_designated
= designator_depth
!= 0;
9148 designator_depth
= 0;
9149 designator_erroneous
= 0;
9151 if (!implicit
&& value
.value
&& !integer_zerop (value
.value
))
9152 constructor_zeroinit
= 0;
9154 /* Handle superfluous braces around string cst as in
9155 char x[] = {"foo"}; */
9159 && TREE_CODE (constructor_type
) == ARRAY_TYPE
9160 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
9161 && integer_zerop (constructor_unfilled_index
))
9163 if (constructor_stack
->replacement_value
.value
)
9164 error_init (loc
, "excess elements in char array initializer");
9165 constructor_stack
->replacement_value
= value
;
9169 if (constructor_stack
->replacement_value
.value
!= 0)
9171 error_init (loc
, "excess elements in struct initializer");
9175 /* Ignore elements of a brace group if it is entirely superfluous
9176 and has already been diagnosed. */
9177 if (constructor_type
== 0)
9180 if (!implicit
&& warn_designated_init
&& !was_designated
9181 && TREE_CODE (constructor_type
) == RECORD_TYPE
9182 && lookup_attribute ("designated_init",
9183 TYPE_ATTRIBUTES (constructor_type
)))
9185 OPT_Wdesignated_init
,
9186 "positional initialization of field "
9187 "in %<struct%> declared with %<designated_init%> attribute");
9189 /* If we've exhausted any levels that didn't have braces,
9191 while (constructor_stack
->implicit
)
9193 if (RECORD_OR_UNION_TYPE_P (constructor_type
)
9194 && constructor_fields
== 0)
9195 process_init_element (loc
,
9196 pop_init_level (loc
, 1, braced_init_obstack
),
9197 true, braced_init_obstack
);
9198 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
9199 || VECTOR_TYPE_P (constructor_type
))
9200 && constructor_max_index
9201 && tree_int_cst_lt (constructor_max_index
,
9203 process_init_element (loc
,
9204 pop_init_level (loc
, 1, braced_init_obstack
),
9205 true, braced_init_obstack
);
9210 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9211 if (constructor_range_stack
)
9213 /* If value is a compound literal and we'll be just using its
9214 content, don't put it into a SAVE_EXPR. */
9215 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
9216 || !require_constant_value
)
9218 tree semantic_type
= NULL_TREE
;
9219 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
9221 semantic_type
= TREE_TYPE (value
.value
);
9222 value
.value
= TREE_OPERAND (value
.value
, 0);
9224 value
.value
= c_save_expr (value
.value
);
9226 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
9233 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
9236 enum tree_code fieldcode
;
9238 if (constructor_fields
== 0)
9240 pedwarn_init (loc
, 0, "excess elements in struct initializer");
9244 fieldtype
= TREE_TYPE (constructor_fields
);
9245 if (fieldtype
!= error_mark_node
)
9246 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
9247 fieldcode
= TREE_CODE (fieldtype
);
9249 /* Error for non-static initialization of a flexible array member. */
9250 if (fieldcode
== ARRAY_TYPE
9251 && !require_constant_value
9252 && TYPE_SIZE (fieldtype
) == NULL_TREE
9253 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
9255 error_init (loc
, "non-static initialization of a flexible "
9260 /* Error for initialization of a flexible array member with
9261 a string constant if the structure is in an array. E.g.:
9262 struct S { int x; char y[]; };
9263 struct S s[] = { { 1, "foo" } };
9266 && fieldcode
== ARRAY_TYPE
9267 && constructor_depth
> 1
9268 && TYPE_SIZE (fieldtype
) == NULL_TREE
9269 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
9271 bool in_array_p
= false;
9272 for (struct constructor_stack
*p
= constructor_stack
;
9273 p
&& p
->type
; p
= p
->next
)
9274 if (TREE_CODE (p
->type
) == ARRAY_TYPE
)
9281 error_init (loc
, "initialization of flexible array "
9282 "member in a nested context");
9287 /* Accept a string constant to initialize a subarray. */
9288 if (value
.value
!= 0
9289 && fieldcode
== ARRAY_TYPE
9290 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
9292 value
.value
= orig_value
;
9293 /* Otherwise, if we have come to a subaggregate,
9294 and we don't have an element of its type, push into it. */
9295 else if (value
.value
!= 0
9296 && value
.value
!= error_mark_node
9297 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
9298 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
9299 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
9301 push_init_level (loc
, 1, braced_init_obstack
);
9307 push_member_name (constructor_fields
);
9308 output_init_element (loc
, value
.value
, value
.original_type
,
9309 strict_string
, fieldtype
,
9310 constructor_fields
, 1, implicit
,
9311 braced_init_obstack
);
9312 RESTORE_SPELLING_DEPTH (constructor_depth
);
9315 /* Do the bookkeeping for an element that was
9316 directly output as a constructor. */
9318 /* For a record, keep track of end position of last field. */
9319 if (DECL_SIZE (constructor_fields
))
9320 constructor_bit_index
9321 = size_binop_loc (input_location
, PLUS_EXPR
,
9322 bit_position (constructor_fields
),
9323 DECL_SIZE (constructor_fields
));
9325 /* If the current field was the first one not yet written out,
9326 it isn't now, so update. */
9327 if (constructor_unfilled_fields
== constructor_fields
)
9329 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
9330 /* Skip any nameless bit fields. */
9331 while (constructor_unfilled_fields
!= 0
9332 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
9333 && DECL_NAME (constructor_unfilled_fields
) == 0)
9334 constructor_unfilled_fields
=
9335 DECL_CHAIN (constructor_unfilled_fields
);
9339 constructor_fields
= DECL_CHAIN (constructor_fields
);
9340 /* Skip any nameless bit fields at the beginning. */
9341 while (constructor_fields
!= 0
9342 && DECL_C_BIT_FIELD (constructor_fields
)
9343 && DECL_NAME (constructor_fields
) == 0)
9344 constructor_fields
= DECL_CHAIN (constructor_fields
);
9346 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
9349 enum tree_code fieldcode
;
9351 if (constructor_fields
== 0)
9353 pedwarn_init (loc
, 0,
9354 "excess elements in union initializer");
9358 fieldtype
= TREE_TYPE (constructor_fields
);
9359 if (fieldtype
!= error_mark_node
)
9360 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
9361 fieldcode
= TREE_CODE (fieldtype
);
9363 /* Warn that traditional C rejects initialization of unions.
9364 We skip the warning if the value is zero. This is done
9365 under the assumption that the zero initializer in user
9366 code appears conditioned on e.g. __STDC__ to avoid
9367 "missing initializer" warnings and relies on default
9368 initialization to zero in the traditional C case.
9369 We also skip the warning if the initializer is designated,
9370 again on the assumption that this must be conditional on
9371 __STDC__ anyway (and we've already complained about the
9372 member-designator already). */
9373 if (!in_system_header_at (input_location
) && !constructor_designated
9374 && !(value
.value
&& (integer_zerop (value
.value
)
9375 || real_zerop (value
.value
))))
9376 warning (OPT_Wtraditional
, "traditional C rejects initialization "
9379 /* Accept a string constant to initialize a subarray. */
9380 if (value
.value
!= 0
9381 && fieldcode
== ARRAY_TYPE
9382 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
9384 value
.value
= orig_value
;
9385 /* Otherwise, if we have come to a subaggregate,
9386 and we don't have an element of its type, push into it. */
9387 else if (value
.value
!= 0
9388 && value
.value
!= error_mark_node
9389 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
9390 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
9391 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
9393 push_init_level (loc
, 1, braced_init_obstack
);
9399 push_member_name (constructor_fields
);
9400 output_init_element (loc
, value
.value
, value
.original_type
,
9401 strict_string
, fieldtype
,
9402 constructor_fields
, 1, implicit
,
9403 braced_init_obstack
);
9404 RESTORE_SPELLING_DEPTH (constructor_depth
);
9407 /* Do the bookkeeping for an element that was
9408 directly output as a constructor. */
9410 constructor_bit_index
= DECL_SIZE (constructor_fields
);
9411 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
9414 constructor_fields
= 0;
9416 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9418 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
9419 enum tree_code eltcode
= TREE_CODE (elttype
);
9421 /* Accept a string constant to initialize a subarray. */
9422 if (value
.value
!= 0
9423 && eltcode
== ARRAY_TYPE
9424 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
9426 value
.value
= orig_value
;
9427 /* Otherwise, if we have come to a subaggregate,
9428 and we don't have an element of its type, push into it. */
9429 else if (value
.value
!= 0
9430 && value
.value
!= error_mark_node
9431 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
9432 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
9433 || eltcode
== UNION_TYPE
|| eltcode
== VECTOR_TYPE
))
9435 push_init_level (loc
, 1, braced_init_obstack
);
9439 if (constructor_max_index
!= 0
9440 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
9441 || integer_all_onesp (constructor_max_index
)))
9443 pedwarn_init (loc
, 0,
9444 "excess elements in array initializer");
9448 /* Now output the actual element. */
9451 push_array_bounds (tree_to_uhwi (constructor_index
));
9452 output_init_element (loc
, value
.value
, value
.original_type
,
9453 strict_string
, elttype
,
9454 constructor_index
, 1, implicit
,
9455 braced_init_obstack
);
9456 RESTORE_SPELLING_DEPTH (constructor_depth
);
9460 = size_binop_loc (input_location
, PLUS_EXPR
,
9461 constructor_index
, bitsize_one_node
);
9464 /* If we are doing the bookkeeping for an element that was
9465 directly output as a constructor, we must update
9466 constructor_unfilled_index. */
9467 constructor_unfilled_index
= constructor_index
;
9469 else if (VECTOR_TYPE_P (constructor_type
))
9471 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
9473 /* Do a basic check of initializer size. Note that vectors
9474 always have a fixed size derived from their type. */
9475 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
9477 pedwarn_init (loc
, 0,
9478 "excess elements in vector initializer");
9482 /* Now output the actual element. */
9485 if (TREE_CODE (value
.value
) == VECTOR_CST
)
9486 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
9487 output_init_element (loc
, value
.value
, value
.original_type
,
9488 strict_string
, elttype
,
9489 constructor_index
, 1, implicit
,
9490 braced_init_obstack
);
9494 = size_binop_loc (input_location
,
9495 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
9498 /* If we are doing the bookkeeping for an element that was
9499 directly output as a constructor, we must update
9500 constructor_unfilled_index. */
9501 constructor_unfilled_index
= constructor_index
;
9504 /* Handle the sole element allowed in a braced initializer
9505 for a scalar variable. */
9506 else if (constructor_type
!= error_mark_node
9507 && constructor_fields
== 0)
9509 pedwarn_init (loc
, 0,
9510 "excess elements in scalar initializer");
9516 output_init_element (loc
, value
.value
, value
.original_type
,
9517 strict_string
, constructor_type
,
9518 NULL_TREE
, 1, implicit
,
9519 braced_init_obstack
);
9520 constructor_fields
= 0;
9523 /* Handle range initializers either at this level or anywhere higher
9524 in the designator stack. */
9525 if (constructor_range_stack
)
9527 struct constructor_range_stack
*p
, *range_stack
;
9530 range_stack
= constructor_range_stack
;
9531 constructor_range_stack
= 0;
9532 while (constructor_stack
!= range_stack
->stack
)
9534 gcc_assert (constructor_stack
->implicit
);
9535 process_init_element (loc
,
9536 pop_init_level (loc
, 1,
9537 braced_init_obstack
),
9538 true, braced_init_obstack
);
9540 for (p
= range_stack
;
9541 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
9544 gcc_assert (constructor_stack
->implicit
);
9545 process_init_element (loc
,
9546 pop_init_level (loc
, 1,
9547 braced_init_obstack
),
9548 true, braced_init_obstack
);
9551 p
->index
= size_binop_loc (input_location
,
9552 PLUS_EXPR
, p
->index
, bitsize_one_node
);
9553 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
9558 constructor_index
= p
->index
;
9559 constructor_fields
= p
->fields
;
9560 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
9568 finish_implicit_inits (loc
, braced_init_obstack
);
9569 push_init_level (loc
, 2, braced_init_obstack
);
9570 p
->stack
= constructor_stack
;
9571 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
9572 p
->index
= p
->range_start
;
9576 constructor_range_stack
= range_stack
;
9583 constructor_range_stack
= 0;
9586 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9587 (guaranteed to be 'volatile' or null) and ARGS (represented using
9588 an ASM_EXPR node). */
9590 build_asm_stmt (tree cv_qualifier
, tree args
)
9592 if (!ASM_VOLATILE_P (args
) && cv_qualifier
)
9593 ASM_VOLATILE_P (args
) = 1;
9594 return add_stmt (args
);
9597 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9598 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9599 SIMPLE indicates whether there was anything at all after the
9600 string in the asm expression -- asm("blah") and asm("blah" : )
9601 are subtly different. We use a ASM_EXPR node to represent this. */
9603 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
9604 tree clobbers
, tree labels
, bool simple
)
9609 const char *constraint
;
9610 const char **oconstraints
;
9611 bool allows_mem
, allows_reg
, is_inout
;
9612 int ninputs
, noutputs
;
9614 ninputs
= list_length (inputs
);
9615 noutputs
= list_length (outputs
);
9616 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
9618 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
9620 /* Remove output conversions that change the type but not the mode. */
9621 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
9623 tree output
= TREE_VALUE (tail
);
9625 output
= c_fully_fold (output
, false, NULL
);
9627 /* ??? Really, this should not be here. Users should be using a
9628 proper lvalue, dammit. But there's a long history of using casts
9629 in the output operands. In cases like longlong.h, this becomes a
9630 primitive form of typechecking -- if the cast can be removed, then
9631 the output operand had a type of the proper width; otherwise we'll
9632 get an error. Gross, but ... */
9633 STRIP_NOPS (output
);
9635 if (!lvalue_or_else (loc
, output
, lv_asm
))
9636 output
= error_mark_node
;
9638 if (output
!= error_mark_node
9639 && (TREE_READONLY (output
)
9640 || TYPE_READONLY (TREE_TYPE (output
))
9641 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output
))
9642 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
9643 readonly_error (loc
, output
, lv_asm
);
9645 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9646 oconstraints
[i
] = constraint
;
9648 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
9649 &allows_mem
, &allows_reg
, &is_inout
))
9651 /* If the operand is going to end up in memory,
9652 mark it addressable. */
9653 if (!allows_reg
&& !c_mark_addressable (output
))
9654 output
= error_mark_node
;
9655 if (!(!allows_reg
&& allows_mem
)
9656 && output
!= error_mark_node
9657 && VOID_TYPE_P (TREE_TYPE (output
)))
9659 error_at (loc
, "invalid use of void expression");
9660 output
= error_mark_node
;
9664 output
= error_mark_node
;
9666 TREE_VALUE (tail
) = output
;
9669 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
9673 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9674 input
= TREE_VALUE (tail
);
9676 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
9677 oconstraints
, &allows_mem
, &allows_reg
))
9679 /* If the operand is going to end up in memory,
9680 mark it addressable. */
9681 if (!allows_reg
&& allows_mem
)
9683 input
= c_fully_fold (input
, false, NULL
);
9685 /* Strip the nops as we allow this case. FIXME, this really
9686 should be rejected or made deprecated. */
9688 if (!c_mark_addressable (input
))
9689 input
= error_mark_node
;
9694 memset (&expr
, 0, sizeof (expr
));
9696 expr
= convert_lvalue_to_rvalue (loc
, expr
, true, false);
9697 input
= c_fully_fold (expr
.value
, false, NULL
);
9699 if (input
!= error_mark_node
&& VOID_TYPE_P (TREE_TYPE (input
)))
9701 error_at (loc
, "invalid use of void expression");
9702 input
= error_mark_node
;
9707 input
= error_mark_node
;
9709 TREE_VALUE (tail
) = input
;
9712 /* ASMs with labels cannot have outputs. This should have been
9713 enforced by the parser. */
9714 gcc_assert (outputs
== NULL
|| labels
== NULL
);
9716 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
9718 /* asm statements without outputs, including simple ones, are treated
9720 ASM_INPUT_P (args
) = simple
;
9721 ASM_VOLATILE_P (args
) = (noutputs
== 0);
9726 /* Generate a goto statement to LABEL. LOC is the location of the
9730 c_finish_goto_label (location_t loc
, tree label
)
9732 tree decl
= lookup_label_for_goto (loc
, label
);
9735 TREE_USED (decl
) = 1;
9737 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
9738 SET_EXPR_LOCATION (t
, loc
);
9739 return add_stmt (t
);
9743 /* Generate a computed goto statement to EXPR. LOC is the location of
9747 c_finish_goto_ptr (location_t loc
, tree expr
)
9750 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids %<goto *expr;%>");
9751 expr
= c_fully_fold (expr
, false, NULL
);
9752 expr
= convert (ptr_type_node
, expr
);
9753 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
9754 SET_EXPR_LOCATION (t
, loc
);
9755 return add_stmt (t
);
9758 /* Generate a C `return' statement. RETVAL is the expression for what
9759 to return, or a null pointer for `return;' with no value. LOC is
9760 the location of the return statement, or the location of the expression,
9761 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9762 is the original type of RETVAL. */
9765 c_finish_return (location_t loc
, tree retval
, tree origtype
)
9767 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
9768 bool no_warning
= false;
9772 /* Use the expansion point to handle cases such as returning NULL
9773 in a function returning void. */
9774 source_location xloc
= expansion_point_location_if_in_system_header (loc
);
9776 if (TREE_THIS_VOLATILE (current_function_decl
))
9777 warning_at (xloc
, 0,
9778 "function declared %<noreturn%> has a %<return%> statement");
9780 if (flag_cilkplus
&& contains_array_notation_expr (retval
))
9782 /* Array notations are allowed in a return statement if it is inside a
9783 built-in array notation reduction function. */
9784 if (!find_rank (loc
, retval
, retval
, false, &rank
))
9785 return error_mark_node
;
9788 error_at (loc
, "array notation expression cannot be used as a "
9790 return error_mark_node
;
9793 if (flag_cilkplus
&& retval
&& contains_cilk_spawn_stmt (retval
))
9795 error_at (loc
, "use of %<_Cilk_spawn%> in a return statement is not "
9797 return error_mark_node
;
9801 tree semantic_type
= NULL_TREE
;
9802 npc
= null_pointer_constant_p (retval
);
9803 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
9805 semantic_type
= TREE_TYPE (retval
);
9806 retval
= TREE_OPERAND (retval
, 0);
9808 retval
= c_fully_fold (retval
, false, NULL
);
9810 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
9815 current_function_returns_null
= 1;
9816 if ((warn_return_type
|| flag_isoc99
)
9817 && valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
9821 warned_here
= pedwarn
9823 "%<return%> with no value, in function returning non-void");
9825 warned_here
= warning_at
9826 (loc
, OPT_Wreturn_type
,
9827 "%<return%> with no value, in function returning non-void");
9830 inform (DECL_SOURCE_LOCATION (current_function_decl
),
9834 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
9836 current_function_returns_null
= 1;
9838 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
9839 warned_here
= pedwarn
9841 "%<return%> with a value, in function returning void");
9843 warned_here
= pedwarn
9844 (xloc
, OPT_Wpedantic
, "ISO C forbids "
9845 "%<return%> with expression, in function returning void");
9847 inform (DECL_SOURCE_LOCATION (current_function_decl
),
9852 tree t
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, valtype
,
9853 retval
, origtype
, ic_return
,
9854 npc
, NULL_TREE
, NULL_TREE
, 0);
9855 tree res
= DECL_RESULT (current_function_decl
);
9859 current_function_returns_value
= 1;
9860 if (t
== error_mark_node
)
9863 save
= in_late_binary_op
;
9864 if (TREE_CODE (TREE_TYPE (res
)) == BOOLEAN_TYPE
9865 || TREE_CODE (TREE_TYPE (res
)) == COMPLEX_TYPE
9866 || (TREE_CODE (TREE_TYPE (t
)) == REAL_TYPE
9867 && (TREE_CODE (TREE_TYPE (res
)) == INTEGER_TYPE
9868 || TREE_CODE (TREE_TYPE (res
)) == ENUMERAL_TYPE
)
9869 && (flag_sanitize
& SANITIZE_FLOAT_CAST
)))
9870 in_late_binary_op
= true;
9871 inner
= t
= convert (TREE_TYPE (res
), t
);
9872 in_late_binary_op
= save
;
9874 /* Strip any conversions, additions, and subtractions, and see if
9875 we are returning the address of a local variable. Warn if so. */
9878 switch (TREE_CODE (inner
))
9881 case NON_LVALUE_EXPR
:
9883 case POINTER_PLUS_EXPR
:
9884 inner
= TREE_OPERAND (inner
, 0);
9888 /* If the second operand of the MINUS_EXPR has a pointer
9889 type (or is converted from it), this may be valid, so
9890 don't give a warning. */
9892 tree op1
= TREE_OPERAND (inner
, 1);
9894 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
9895 && (CONVERT_EXPR_P (op1
)
9896 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
9897 op1
= TREE_OPERAND (op1
, 0);
9899 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
9902 inner
= TREE_OPERAND (inner
, 0);
9907 inner
= TREE_OPERAND (inner
, 0);
9909 while (REFERENCE_CLASS_P (inner
)
9910 && !INDIRECT_REF_P (inner
))
9911 inner
= TREE_OPERAND (inner
, 0);
9914 && !DECL_EXTERNAL (inner
)
9915 && !TREE_STATIC (inner
)
9916 && DECL_CONTEXT (inner
) == current_function_decl
)
9918 if (TREE_CODE (inner
) == LABEL_DECL
)
9919 warning_at (loc
, OPT_Wreturn_local_addr
,
9920 "function returns address of label");
9923 warning_at (loc
, OPT_Wreturn_local_addr
,
9924 "function returns address of local variable");
9925 tree zero
= build_zero_cst (TREE_TYPE (res
));
9926 t
= build2 (COMPOUND_EXPR
, TREE_TYPE (res
), t
, zero
);
9938 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
9939 SET_EXPR_LOCATION (retval
, loc
);
9941 if (warn_sequence_point
)
9942 verify_sequence_points (retval
);
9945 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
9946 TREE_NO_WARNING (ret_stmt
) |= no_warning
;
9947 return add_stmt (ret_stmt
);
9951 /* The SWITCH_EXPR being built. */
9954 /* The original type of the testing expression, i.e. before the
9955 default conversion is applied. */
9958 /* A splay-tree mapping the low element of a case range to the high
9959 element, or NULL_TREE if there is no high element. Used to
9960 determine whether or not a new case label duplicates an old case
9961 label. We need a tree, rather than simply a hash table, because
9962 of the GNU case range extension. */
9965 /* The bindings at the point of the switch. This is used for
9966 warnings crossing decls when branching to a case label. */
9967 struct c_spot_bindings
*bindings
;
9969 /* The next node on the stack. */
9970 struct c_switch
*next
;
9972 /* Remember whether the controlling expression had boolean type
9973 before integer promotions for the sake of -Wswitch-bool. */
9976 /* Remember whether there was a case value that is outside the
9977 range of the ORIG_TYPE. */
9978 bool outside_range_p
;
9981 /* A stack of the currently active switch statements. The innermost
9982 switch statement is on the top of the stack. There is no need to
9983 mark the stack for garbage collection because it is only active
9984 during the processing of the body of a function, and we never
9985 collect at that point. */
9987 struct c_switch
*c_switch_stack
;
9989 /* Start a C switch statement, testing expression EXP. Return the new
9990 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9991 SWITCH_COND_LOC is the location of the switch's condition.
9992 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
9995 c_start_case (location_t switch_loc
,
9996 location_t switch_cond_loc
,
9997 tree exp
, bool explicit_cast_p
)
9999 tree orig_type
= error_mark_node
;
10000 bool bool_cond_p
= false;
10001 struct c_switch
*cs
;
10003 if (exp
!= error_mark_node
)
10005 orig_type
= TREE_TYPE (exp
);
10007 if (!INTEGRAL_TYPE_P (orig_type
))
10009 if (orig_type
!= error_mark_node
)
10011 error_at (switch_cond_loc
, "switch quantity not an integer");
10012 orig_type
= error_mark_node
;
10014 exp
= integer_zero_node
;
10018 tree type
= TYPE_MAIN_VARIANT (orig_type
);
10021 /* Warn if the condition has boolean value. */
10022 while (TREE_CODE (e
) == COMPOUND_EXPR
)
10023 e
= TREE_OPERAND (e
, 1);
10025 if ((TREE_CODE (type
) == BOOLEAN_TYPE
10026 || truth_value_p (TREE_CODE (e
)))
10027 /* Explicit cast to int suppresses this warning. */
10028 && !(TREE_CODE (type
) == INTEGER_TYPE
10029 && explicit_cast_p
))
10030 bool_cond_p
= true;
10032 if (!in_system_header_at (input_location
)
10033 && (type
== long_integer_type_node
10034 || type
== long_unsigned_type_node
))
10035 warning_at (switch_cond_loc
,
10036 OPT_Wtraditional
, "%<long%> switch expression not "
10037 "converted to %<int%> in ISO C");
10039 exp
= c_fully_fold (exp
, false, NULL
);
10040 exp
= default_conversion (exp
);
10042 if (warn_sequence_point
)
10043 verify_sequence_points (exp
);
10047 /* Add this new SWITCH_EXPR to the stack. */
10048 cs
= XNEW (struct c_switch
);
10049 cs
->switch_expr
= build3 (SWITCH_EXPR
, orig_type
, exp
, NULL_TREE
, NULL_TREE
);
10050 SET_EXPR_LOCATION (cs
->switch_expr
, switch_loc
);
10051 cs
->orig_type
= orig_type
;
10052 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
10053 cs
->bindings
= c_get_switch_bindings ();
10054 cs
->bool_cond_p
= bool_cond_p
;
10055 cs
->outside_range_p
= false;
10056 cs
->next
= c_switch_stack
;
10057 c_switch_stack
= cs
;
10059 return add_stmt (cs
->switch_expr
);
10062 /* Process a case label at location LOC. */
10065 do_case (location_t loc
, tree low_value
, tree high_value
)
10067 tree label
= NULL_TREE
;
10069 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
10071 low_value
= c_fully_fold (low_value
, false, NULL
);
10072 if (TREE_CODE (low_value
) == INTEGER_CST
)
10073 pedwarn (loc
, OPT_Wpedantic
,
10074 "case label is not an integer constant expression");
10077 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
10079 high_value
= c_fully_fold (high_value
, false, NULL
);
10080 if (TREE_CODE (high_value
) == INTEGER_CST
)
10081 pedwarn (input_location
, OPT_Wpedantic
,
10082 "case label is not an integer constant expression");
10085 if (c_switch_stack
== NULL
)
10088 error_at (loc
, "case label not within a switch statement");
10090 error_at (loc
, "%<default%> label not within a switch statement");
10094 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
10095 EXPR_LOCATION (c_switch_stack
->switch_expr
),
10099 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
10100 SWITCH_COND (c_switch_stack
->switch_expr
),
10101 c_switch_stack
->orig_type
,
10102 low_value
, high_value
,
10103 &c_switch_stack
->outside_range_p
);
10104 if (label
== error_mark_node
)
10109 /* Finish the switch statement. TYPE is the original type of the
10110 controlling expression of the switch, or NULL_TREE. */
10113 c_finish_case (tree body
, tree type
)
10115 struct c_switch
*cs
= c_switch_stack
;
10116 location_t switch_location
;
10118 SWITCH_BODY (cs
->switch_expr
) = body
;
10120 /* Emit warnings as needed. */
10121 switch_location
= EXPR_LOCATION (cs
->switch_expr
);
10122 c_do_switch_warnings (cs
->cases
, switch_location
,
10123 type
? type
: TREE_TYPE (cs
->switch_expr
),
10124 SWITCH_COND (cs
->switch_expr
),
10125 cs
->bool_cond_p
, cs
->outside_range_p
);
10127 /* Pop the stack. */
10128 c_switch_stack
= cs
->next
;
10129 splay_tree_delete (cs
->cases
);
10130 c_release_switch_bindings (cs
->bindings
);
10134 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10135 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10139 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
10144 /* If the condition has array notations, then the rank of the then_block and
10145 else_block must be either 0 or be equal to the rank of the condition. If
10146 the condition does not have array notations then break them up as it is
10147 broken up in a normal expression. */
10148 if (flag_cilkplus
&& contains_array_notation_expr (cond
))
10150 size_t then_rank
= 0, cond_rank
= 0, else_rank
= 0;
10151 if (!find_rank (if_locus
, cond
, cond
, true, &cond_rank
))
10154 && !find_rank (if_locus
, then_block
, then_block
, true, &then_rank
))
10157 && !find_rank (if_locus
, else_block
, else_block
, true, &else_rank
))
10159 if (cond_rank
!= then_rank
&& then_rank
!= 0)
10161 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
10162 " and the then-block");
10165 else if (cond_rank
!= else_rank
&& else_rank
!= 0)
10167 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
10168 " and the else-block");
10173 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
10174 SET_EXPR_LOCATION (stmt
, if_locus
);
10178 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10179 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10180 is false for DO loops. INCR is the FOR increment expression. BODY is
10181 the statement controlled by the loop. BLAB is the break label. CLAB is
10182 the continue label. Everything is allowed to be NULL. */
10185 c_finish_loop (location_t start_locus
, tree cond
, tree incr
, tree body
,
10186 tree blab
, tree clab
, bool cond_is_first
)
10188 tree entry
= NULL
, exit
= NULL
, t
;
10190 /* In theory could forbid cilk spawn for loop increment expression,
10191 but it should work just fine. */
10193 /* If the condition is zero don't generate a loop construct. */
10194 if (cond
&& integer_zerop (cond
))
10198 t
= build_and_jump (&blab
);
10199 SET_EXPR_LOCATION (t
, start_locus
);
10205 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
10207 /* If we have an exit condition, then we build an IF with gotos either
10208 out of the loop, or to the top of it. If there's no exit condition,
10209 then we just build a jump back to the top. */
10210 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
10212 if (cond
&& !integer_nonzerop (cond
))
10214 /* Canonicalize the loop condition to the end. This means
10215 generating a branch to the loop condition. Reuse the
10216 continue label, if possible. */
10221 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
10222 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
10225 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
10226 SET_EXPR_LOCATION (t
, start_locus
);
10230 t
= build_and_jump (&blab
);
10232 exit
= fold_build3_loc (start_locus
,
10233 COND_EXPR
, void_type_node
, cond
, exit
, t
);
10235 exit
= fold_build3_loc (input_location
,
10236 COND_EXPR
, void_type_node
, cond
, exit
, t
);
10240 /* For the backward-goto's location of an unconditional loop
10241 use the beginning of the body, or, if there is none, the
10242 top of the loop. */
10243 location_t loc
= EXPR_LOCATION (expr_first (body
));
10244 if (loc
== UNKNOWN_LOCATION
)
10246 SET_EXPR_LOCATION (exit
, loc
);
10255 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
10263 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
10267 c_finish_bc_stmt (location_t loc
, tree
*label_p
, bool is_break
)
10270 tree label
= *label_p
;
10272 /* In switch statements break is sometimes stylistically used after
10273 a return statement. This can lead to spurious warnings about
10274 control reaching the end of a non-void function when it is
10275 inlined. Note that we are calling block_may_fallthru with
10276 language specific tree nodes; this works because
10277 block_may_fallthru returns true when given something it does not
10279 skip
= !block_may_fallthru (cur_stmt_list
);
10284 *label_p
= label
= create_artificial_label (loc
);
10286 else if (TREE_CODE (label
) == LABEL_DECL
)
10288 else switch (TREE_INT_CST_LOW (label
))
10292 error_at (loc
, "break statement not within loop or switch");
10294 error_at (loc
, "continue statement not within a loop");
10298 gcc_assert (is_break
);
10299 error_at (loc
, "break statement used with OpenMP for loop");
10304 error ("break statement within %<#pragma simd%> loop body");
10306 error ("continue statement within %<#pragma simd%> loop body");
10310 gcc_unreachable ();
10317 add_stmt (build_predict_expr (PRED_CONTINUE
, NOT_TAKEN
));
10319 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
10322 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10325 emit_side_effect_warnings (location_t loc
, tree expr
)
10327 if (expr
== error_mark_node
)
10329 else if (!TREE_SIDE_EFFECTS (expr
))
10331 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
10332 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
10334 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
10337 location_t cloc
= loc
;
10338 while (TREE_CODE (r
) == COMPOUND_EXPR
)
10340 if (EXPR_HAS_LOCATION (r
))
10341 cloc
= EXPR_LOCATION (r
);
10342 r
= TREE_OPERAND (r
, 1);
10344 if (!TREE_SIDE_EFFECTS (r
)
10345 && !VOID_TYPE_P (TREE_TYPE (r
))
10346 && !CONVERT_EXPR_P (r
)
10347 && !TREE_NO_WARNING (r
)
10348 && !TREE_NO_WARNING (expr
))
10349 warning_at (cloc
, OPT_Wunused_value
,
10350 "right-hand operand of comma expression has no effect");
10353 warn_if_unused_value (expr
, loc
);
10356 /* Process an expression as if it were a complete statement. Emit
10357 diagnostics, but do not call ADD_STMT. LOC is the location of the
10361 c_process_expr_stmt (location_t loc
, tree expr
)
10368 expr
= c_fully_fold (expr
, false, NULL
);
10370 if (warn_sequence_point
)
10371 verify_sequence_points (expr
);
10373 if (TREE_TYPE (expr
) != error_mark_node
10374 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
10375 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
10376 error_at (loc
, "expression statement has incomplete type");
10378 /* If we're not processing a statement expression, warn about unused values.
10379 Warnings for statement expressions will be emitted later, once we figure
10380 out which is the result. */
10381 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
10382 && warn_unused_value
)
10383 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr
, loc
), expr
);
10386 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
10387 exprv
= TREE_OPERAND (exprv
, 1);
10388 while (CONVERT_EXPR_P (exprv
))
10389 exprv
= TREE_OPERAND (exprv
, 0);
10391 || handled_component_p (exprv
)
10392 || TREE_CODE (exprv
) == ADDR_EXPR
)
10393 mark_exp_read (exprv
);
10395 /* If the expression is not of a type to which we cannot assign a line
10396 number, wrap the thing in a no-op NOP_EXPR. */
10397 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
10399 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
10400 SET_EXPR_LOCATION (expr
, loc
);
10406 /* Emit an expression as a statement. LOC is the location of the
10410 c_finish_expr_stmt (location_t loc
, tree expr
)
10413 return add_stmt (c_process_expr_stmt (loc
, expr
));
10418 /* Do the opposite and emit a statement as an expression. To begin,
10419 create a new binding level and return it. */
10422 c_begin_stmt_expr (void)
10426 /* We must force a BLOCK for this level so that, if it is not expanded
10427 later, there is a way to turn off the entire subtree of blocks that
10428 are contained in it. */
10429 keep_next_level ();
10430 ret
= c_begin_compound_stmt (true);
10432 c_bindings_start_stmt_expr (c_switch_stack
== NULL
10434 : c_switch_stack
->bindings
);
10436 /* Mark the current statement list as belonging to a statement list. */
10437 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
10442 /* LOC is the location of the compound statement to which this body
10446 c_finish_stmt_expr (location_t loc
, tree body
)
10448 tree last
, type
, tmp
, val
;
10451 body
= c_end_compound_stmt (loc
, body
, true);
10453 c_bindings_end_stmt_expr (c_switch_stack
== NULL
10455 : c_switch_stack
->bindings
);
10457 /* Locate the last statement in BODY. See c_end_compound_stmt
10458 about always returning a BIND_EXPR. */
10459 last_p
= &BIND_EXPR_BODY (body
);
10460 last
= BIND_EXPR_BODY (body
);
10462 continue_searching
:
10463 if (TREE_CODE (last
) == STATEMENT_LIST
)
10465 tree_stmt_iterator i
;
10467 /* This can happen with degenerate cases like ({ }). No value. */
10468 if (!TREE_SIDE_EFFECTS (last
))
10471 /* If we're supposed to generate side effects warnings, process
10472 all of the statements except the last. */
10473 if (warn_unused_value
)
10475 for (i
= tsi_start (last
); !tsi_one_before_end_p (i
); tsi_next (&i
))
10478 tree t
= tsi_stmt (i
);
10480 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
10481 emit_side_effect_warnings (tloc
, t
);
10485 i
= tsi_last (last
);
10486 last_p
= tsi_stmt_ptr (i
);
10490 /* If the end of the list is exception related, then the list was split
10491 by a call to push_cleanup. Continue searching. */
10492 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
10493 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
10495 last_p
= &TREE_OPERAND (last
, 0);
10497 goto continue_searching
;
10500 if (last
== error_mark_node
)
10503 /* In the case that the BIND_EXPR is not necessary, return the
10504 expression out from inside it. */
10505 if (last
== BIND_EXPR_BODY (body
)
10506 && BIND_EXPR_VARS (body
) == NULL
)
10508 /* Even if this looks constant, do not allow it in a constant
10510 last
= c_wrap_maybe_const (last
, true);
10511 /* Do not warn if the return value of a statement expression is
10513 TREE_NO_WARNING (last
) = 1;
10517 /* Extract the type of said expression. */
10518 type
= TREE_TYPE (last
);
10520 /* If we're not returning a value at all, then the BIND_EXPR that
10521 we already have is a fine expression to return. */
10522 if (!type
|| VOID_TYPE_P (type
))
10525 /* Now that we've located the expression containing the value, it seems
10526 silly to make voidify_wrapper_expr repeat the process. Create a
10527 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10528 tmp
= create_tmp_var_raw (type
);
10530 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10531 tree_expr_nonnegative_p giving up immediately. */
10533 if (TREE_CODE (val
) == NOP_EXPR
10534 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
10535 val
= TREE_OPERAND (val
, 0);
10537 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
10538 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
10541 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
10542 SET_EXPR_LOCATION (t
, loc
);
10547 /* Begin and end compound statements. This is as simple as pushing
10548 and popping new statement lists from the tree. */
10551 c_begin_compound_stmt (bool do_scope
)
10553 tree stmt
= push_stmt_list ();
10559 /* End a compound statement. STMT is the statement. LOC is the
10560 location of the compound statement-- this is usually the location
10561 of the opening brace. */
10564 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
10570 if (c_dialect_objc ())
10571 objc_clear_super_receiver ();
10572 block
= pop_scope ();
10575 stmt
= pop_stmt_list (stmt
);
10576 stmt
= c_build_bind_expr (loc
, block
, stmt
);
10578 /* If this compound statement is nested immediately inside a statement
10579 expression, then force a BIND_EXPR to be created. Otherwise we'll
10580 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10581 STATEMENT_LISTs merge, and thus we can lose track of what statement
10582 was really last. */
10583 if (building_stmt_list_p ()
10584 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
10585 && TREE_CODE (stmt
) != BIND_EXPR
)
10587 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
10588 TREE_SIDE_EFFECTS (stmt
) = 1;
10589 SET_EXPR_LOCATION (stmt
, loc
);
10595 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10596 when the current scope is exited. EH_ONLY is true when this is not
10597 meant to apply to normal control flow transfer. */
10600 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
10602 enum tree_code code
;
10606 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
10607 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
10609 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
10610 list
= push_stmt_list ();
10611 TREE_OPERAND (stmt
, 0) = list
;
10612 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
10615 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10616 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10619 build_vec_cmp (tree_code code
, tree type
,
10620 tree arg0
, tree arg1
)
10622 tree zero_vec
= build_zero_cst (type
);
10623 tree minus_one_vec
= build_minus_one_cst (type
);
10624 tree cmp_type
= build_same_sized_truth_vector_type (type
);
10625 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
10626 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
10629 /* Build a binary-operation expression without default conversions.
10630 CODE is the kind of expression to build.
10631 LOCATION is the operator's location.
10632 This function differs from `build' in several ways:
10633 the data type of the result is computed and recorded in it,
10634 warnings are generated if arg data types are invalid,
10635 special handling for addition and subtraction of pointers is known,
10636 and some optimization is done (operations on narrow ints
10637 are done in the narrower type when that gives the same result).
10638 Constant folding is also done before the result is returned.
10640 Note that the operands will never have enumeral types, or function
10641 or array types, because either they will have the default conversions
10642 performed or they have both just been converted to some other type in which
10643 the arithmetic is to be done. */
10646 build_binary_op (location_t location
, enum tree_code code
,
10647 tree orig_op0
, tree orig_op1
, int convert_p
)
10649 tree type0
, type1
, orig_type0
, orig_type1
;
10651 enum tree_code code0
, code1
;
10653 tree ret
= error_mark_node
;
10654 const char *invalid_op_diag
;
10655 bool op0_int_operands
, op1_int_operands
;
10656 bool int_const
, int_const_or_overflow
, int_operands
;
10658 /* Expression code to give to the expression when it is built.
10659 Normally this is CODE, which is what the caller asked for,
10660 but in some special cases we change it. */
10661 enum tree_code resultcode
= code
;
10663 /* Data type in which the computation is to be performed.
10664 In the simplest cases this is the common type of the arguments. */
10665 tree result_type
= NULL
;
10667 /* When the computation is in excess precision, the type of the
10668 final EXCESS_PRECISION_EXPR. */
10669 tree semantic_result_type
= NULL
;
10671 /* Nonzero means operands have already been type-converted
10672 in whatever way is necessary.
10673 Zero means they need to be converted to RESULT_TYPE. */
10676 /* Nonzero means create the expression with this type, rather than
10678 tree build_type
= 0;
10680 /* Nonzero means after finally constructing the expression
10681 convert it to this type. */
10682 tree final_type
= 0;
10684 /* Nonzero if this is an operation like MIN or MAX which can
10685 safely be computed in short if both args are promoted shorts.
10686 Also implies COMMON.
10687 -1 indicates a bitwise operation; this makes a difference
10688 in the exact conditions for when it is safe to do the operation
10689 in a narrower mode. */
10692 /* Nonzero if this is a comparison operation;
10693 if both args are promoted shorts, compare the original shorts.
10694 Also implies COMMON. */
10695 int short_compare
= 0;
10697 /* Nonzero if this is a right-shift operation, which can be computed on the
10698 original short and then promoted if the operand is a promoted short. */
10699 int short_shift
= 0;
10701 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10704 /* True means types are compatible as far as ObjC is concerned. */
10707 /* True means this is an arithmetic operation that may need excess
10709 bool may_need_excess_precision
;
10711 /* True means this is a boolean operation that converts both its
10712 operands to truth-values. */
10713 bool boolean_op
= false;
10715 /* Remember whether we're doing / or %. */
10716 bool doing_div_or_mod
= false;
10718 /* Remember whether we're doing << or >>. */
10719 bool doing_shift
= false;
10721 /* Tree holding instrumentation expression. */
10722 tree instrument_expr
= NULL
;
10724 if (location
== UNKNOWN_LOCATION
)
10725 location
= input_location
;
10730 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
10731 if (op0_int_operands
)
10732 op0
= remove_c_maybe_const_expr (op0
);
10733 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
10734 if (op1_int_operands
)
10735 op1
= remove_c_maybe_const_expr (op1
);
10736 int_operands
= (op0_int_operands
&& op1_int_operands
);
10739 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
10740 && TREE_CODE (orig_op1
) == INTEGER_CST
);
10741 int_const
= (int_const_or_overflow
10742 && !TREE_OVERFLOW (orig_op0
)
10743 && !TREE_OVERFLOW (orig_op1
));
10746 int_const
= int_const_or_overflow
= false;
10748 /* Do not apply default conversion in mixed vector/scalar expression. */
10750 && VECTOR_TYPE_P (TREE_TYPE (op0
)) == VECTOR_TYPE_P (TREE_TYPE (op1
)))
10752 op0
= default_conversion (op0
);
10753 op1
= default_conversion (op1
);
10756 /* When Cilk Plus is enabled and there are array notations inside op0, then
10757 we check to see if there are builtin array notation functions. If
10758 so, then we take on the type of the array notation inside it. */
10759 if (flag_cilkplus
&& contains_array_notation_expr (op0
))
10760 orig_type0
= type0
= find_correct_array_notation_type (op0
);
10762 orig_type0
= type0
= TREE_TYPE (op0
);
10764 if (flag_cilkplus
&& contains_array_notation_expr (op1
))
10765 orig_type1
= type1
= find_correct_array_notation_type (op1
);
10767 orig_type1
= type1
= TREE_TYPE (op1
);
10769 /* The expression codes of the data types of the arguments tell us
10770 whether the arguments are integers, floating, pointers, etc. */
10771 code0
= TREE_CODE (type0
);
10772 code1
= TREE_CODE (type1
);
10774 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10775 STRIP_TYPE_NOPS (op0
);
10776 STRIP_TYPE_NOPS (op1
);
10778 /* If an error was already reported for one of the arguments,
10779 avoid reporting another error. */
10781 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10782 return error_mark_node
;
10784 if (code0
== POINTER_TYPE
10785 && reject_gcc_builtin (op0
, EXPR_LOCATION (orig_op0
)))
10786 return error_mark_node
;
10788 if (code1
== POINTER_TYPE
10789 && reject_gcc_builtin (op1
, EXPR_LOCATION (orig_op1
)))
10790 return error_mark_node
;
10792 if ((invalid_op_diag
10793 = targetm
.invalid_binary_op (code
, type0
, type1
)))
10795 error_at (location
, invalid_op_diag
);
10796 return error_mark_node
;
10804 case TRUNC_DIV_EXPR
:
10805 case CEIL_DIV_EXPR
:
10806 case FLOOR_DIV_EXPR
:
10807 case ROUND_DIV_EXPR
:
10808 case EXACT_DIV_EXPR
:
10809 may_need_excess_precision
= true;
10812 may_need_excess_precision
= false;
10815 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
10817 op0
= TREE_OPERAND (op0
, 0);
10818 type0
= TREE_TYPE (op0
);
10820 else if (may_need_excess_precision
10821 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
10824 op0
= convert (eptype
, op0
);
10826 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
10828 op1
= TREE_OPERAND (op1
, 0);
10829 type1
= TREE_TYPE (op1
);
10831 else if (may_need_excess_precision
10832 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
10835 op1
= convert (eptype
, op1
);
10838 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
10840 /* In case when one of the operands of the binary operation is
10841 a vector and another is a scalar -- convert scalar to vector. */
10842 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
10844 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
10847 switch (convert_flag
)
10850 return error_mark_node
;
10853 bool maybe_const
= true;
10855 sc
= c_fully_fold (op0
, false, &maybe_const
);
10856 sc
= save_expr (sc
);
10857 sc
= convert (TREE_TYPE (type1
), sc
);
10858 op0
= build_vector_from_val (type1
, sc
);
10860 op0
= c_wrap_maybe_const (op0
, true);
10861 orig_type0
= type0
= TREE_TYPE (op0
);
10862 code0
= TREE_CODE (type0
);
10866 case stv_secondarg
:
10868 bool maybe_const
= true;
10870 sc
= c_fully_fold (op1
, false, &maybe_const
);
10871 sc
= save_expr (sc
);
10872 sc
= convert (TREE_TYPE (type0
), sc
);
10873 op1
= build_vector_from_val (type0
, sc
);
10875 op1
= c_wrap_maybe_const (op1
, true);
10876 orig_type1
= type1
= TREE_TYPE (op1
);
10877 code1
= TREE_CODE (type1
);
10889 /* Handle the pointer + int case. */
10890 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10892 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
10893 goto return_build_binary_op
;
10895 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
10897 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
10898 goto return_build_binary_op
;
10905 /* Subtraction of two similar pointers.
10906 We must subtract them as integers, then divide by object size. */
10907 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
10908 && comp_target_types (location
, type0
, type1
))
10910 ret
= pointer_diff (location
, op0
, op1
);
10911 goto return_build_binary_op
;
10913 /* Handle pointer minus int. Just like pointer plus int. */
10914 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10916 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
10917 goto return_build_binary_op
;
10927 case TRUNC_DIV_EXPR
:
10928 case CEIL_DIV_EXPR
:
10929 case FLOOR_DIV_EXPR
:
10930 case ROUND_DIV_EXPR
:
10931 case EXACT_DIV_EXPR
:
10932 doing_div_or_mod
= true;
10933 warn_for_div_by_zero (location
, op1
);
10935 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10936 || code0
== FIXED_POINT_TYPE
10937 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10938 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10939 || code1
== FIXED_POINT_TYPE
10940 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
10942 enum tree_code tcode0
= code0
, tcode1
= code1
;
10944 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10945 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
10946 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
10947 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
10949 if (!((tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
)
10950 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
10951 resultcode
= RDIV_EXPR
;
10953 /* Although it would be tempting to shorten always here, that
10954 loses on some targets, since the modulo instruction is
10955 undefined if the quotient can't be represented in the
10956 computation mode. We shorten only if unsigned or if
10957 dividing by something we know != -1. */
10958 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10959 || (TREE_CODE (op1
) == INTEGER_CST
10960 && !integer_all_onesp (op1
)));
10968 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10970 /* Allow vector types which are not floating point types. */
10971 else if (code0
== VECTOR_TYPE
10972 && code1
== VECTOR_TYPE
10973 && !VECTOR_FLOAT_TYPE_P (type0
)
10974 && !VECTOR_FLOAT_TYPE_P (type1
))
10978 case TRUNC_MOD_EXPR
:
10979 case FLOOR_MOD_EXPR
:
10980 doing_div_or_mod
= true;
10981 warn_for_div_by_zero (location
, op1
);
10983 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10984 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10985 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
10987 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10989 /* Although it would be tempting to shorten always here, that loses
10990 on some targets, since the modulo instruction is undefined if the
10991 quotient can't be represented in the computation mode. We shorten
10992 only if unsigned or if dividing by something we know != -1. */
10993 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10994 || (TREE_CODE (op1
) == INTEGER_CST
10995 && !integer_all_onesp (op1
)));
11000 case TRUTH_ANDIF_EXPR
:
11001 case TRUTH_ORIF_EXPR
:
11002 case TRUTH_AND_EXPR
:
11003 case TRUTH_OR_EXPR
:
11004 case TRUTH_XOR_EXPR
:
11005 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
11006 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
11007 || code0
== FIXED_POINT_TYPE
)
11008 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
11009 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
11010 || code1
== FIXED_POINT_TYPE
))
11012 /* Result of these operations is always an int,
11013 but that does not mean the operands should be
11014 converted to ints! */
11015 result_type
= integer_type_node
;
11016 if (op0_int_operands
)
11018 op0
= c_objc_common_truthvalue_conversion (location
, orig_op0
);
11019 op0
= remove_c_maybe_const_expr (op0
);
11022 op0
= c_objc_common_truthvalue_conversion (location
, op0
);
11023 if (op1_int_operands
)
11025 op1
= c_objc_common_truthvalue_conversion (location
, orig_op1
);
11026 op1
= remove_c_maybe_const_expr (op1
);
11029 op1
= c_objc_common_truthvalue_conversion (location
, op1
);
11033 if (code
== TRUTH_ANDIF_EXPR
)
11035 int_const_or_overflow
= (int_operands
11036 && TREE_CODE (orig_op0
) == INTEGER_CST
11037 && (op0
== truthvalue_false_node
11038 || TREE_CODE (orig_op1
) == INTEGER_CST
));
11039 int_const
= (int_const_or_overflow
11040 && !TREE_OVERFLOW (orig_op0
)
11041 && (op0
== truthvalue_false_node
11042 || !TREE_OVERFLOW (orig_op1
)));
11044 else if (code
== TRUTH_ORIF_EXPR
)
11046 int_const_or_overflow
= (int_operands
11047 && TREE_CODE (orig_op0
) == INTEGER_CST
11048 && (op0
== truthvalue_true_node
11049 || TREE_CODE (orig_op1
) == INTEGER_CST
));
11050 int_const
= (int_const_or_overflow
11051 && !TREE_OVERFLOW (orig_op0
)
11052 && (op0
== truthvalue_true_node
11053 || !TREE_OVERFLOW (orig_op1
)));
11057 /* Shift operations: result has same type as first operand;
11058 always convert second operand to int.
11059 Also set SHORT_SHIFT if shifting rightward. */
11062 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11063 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
11064 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
11065 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
11067 result_type
= type0
;
11070 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
11071 || code0
== VECTOR_TYPE
)
11072 && code1
== INTEGER_TYPE
)
11074 doing_shift
= true;
11075 if (TREE_CODE (op1
) == INTEGER_CST
)
11077 if (tree_int_cst_sgn (op1
) < 0)
11080 if (c_inhibit_evaluation_warnings
== 0)
11081 warning_at (location
, OPT_Wshift_count_negative
,
11082 "right shift count is negative");
11084 else if (code0
== VECTOR_TYPE
)
11086 if (compare_tree_int (op1
,
11087 TYPE_PRECISION (TREE_TYPE (type0
)))
11091 if (c_inhibit_evaluation_warnings
== 0)
11092 warning_at (location
, OPT_Wshift_count_overflow
,
11093 "right shift count >= width of vector element");
11098 if (!integer_zerop (op1
))
11101 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
11104 if (c_inhibit_evaluation_warnings
== 0)
11105 warning_at (location
, OPT_Wshift_count_overflow
,
11106 "right shift count >= width of type");
11111 /* Use the type of the value to be shifted. */
11112 result_type
= type0
;
11113 /* Avoid converting op1 to result_type later. */
11119 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11120 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
11121 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
11122 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
11124 result_type
= type0
;
11127 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
11128 || code0
== VECTOR_TYPE
)
11129 && code1
== INTEGER_TYPE
)
11131 doing_shift
= true;
11132 if (TREE_CODE (op0
) == INTEGER_CST
11133 && tree_int_cst_sgn (op0
) < 0)
11135 /* Don't reject a left shift of a negative value in a context
11136 where a constant expression is needed in C90. */
11139 if (c_inhibit_evaluation_warnings
== 0)
11140 warning_at (location
, OPT_Wshift_negative_value
,
11141 "left shift of negative value");
11143 if (TREE_CODE (op1
) == INTEGER_CST
)
11145 if (tree_int_cst_sgn (op1
) < 0)
11148 if (c_inhibit_evaluation_warnings
== 0)
11149 warning_at (location
, OPT_Wshift_count_negative
,
11150 "left shift count is negative");
11152 else if (code0
== VECTOR_TYPE
)
11154 if (compare_tree_int (op1
,
11155 TYPE_PRECISION (TREE_TYPE (type0
)))
11159 if (c_inhibit_evaluation_warnings
== 0)
11160 warning_at (location
, OPT_Wshift_count_overflow
,
11161 "left shift count >= width of vector element");
11164 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
11167 if (c_inhibit_evaluation_warnings
== 0)
11168 warning_at (location
, OPT_Wshift_count_overflow
,
11169 "left shift count >= width of type");
11171 else if (TREE_CODE (op0
) == INTEGER_CST
11172 && maybe_warn_shift_overflow (location
, op0
, op1
)
11177 /* Use the type of the value to be shifted. */
11178 result_type
= type0
;
11179 /* Avoid converting op1 to result_type later. */
11186 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
11189 if (!vector_types_compatible_elements_p (type0
, type1
))
11191 error_at (location
, "comparing vectors with different "
11193 return error_mark_node
;
11196 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
11198 error_at (location
, "comparing vectors with different "
11199 "number of elements");
11200 return error_mark_node
;
11203 /* It's not precisely specified how the usual arithmetic
11204 conversions apply to the vector types. Here, we use
11205 the unsigned type if one of the operands is signed and
11206 the other one is unsigned. */
11207 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
11209 if (!TYPE_UNSIGNED (type0
))
11210 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
11212 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
11213 warning_at (location
, OPT_Wsign_compare
, "comparison between "
11214 "types %qT and %qT", type0
, type1
);
11217 /* Always construct signed integer vector type. */
11218 intt
= c_common_type_for_size (GET_MODE_BITSIZE
11219 (TYPE_MODE (TREE_TYPE (type0
))), 0);
11220 result_type
= build_opaque_vector_type (intt
,
11221 TYPE_VECTOR_SUBPARTS (type0
));
11223 ret
= build_vec_cmp (resultcode
, result_type
, op0
, op1
);
11224 goto return_build_binary_op
;
11226 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
11227 warning_at (location
,
11229 "comparing floating point with == or != is unsafe");
11230 /* Result of comparison is always int,
11231 but don't convert the args to int! */
11232 build_type
= integer_type_node
;
11233 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
11234 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
11235 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
11236 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
11238 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
11240 if (TREE_CODE (op0
) == ADDR_EXPR
11241 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0))
11242 && !from_macro_expansion_at (location
))
11244 if (code
== EQ_EXPR
)
11245 warning_at (location
,
11247 "the comparison will always evaluate as %<false%> "
11248 "for the address of %qD will never be NULL",
11249 TREE_OPERAND (op0
, 0));
11251 warning_at (location
,
11253 "the comparison will always evaluate as %<true%> "
11254 "for the address of %qD will never be NULL",
11255 TREE_OPERAND (op0
, 0));
11257 result_type
= type0
;
11259 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
11261 if (TREE_CODE (op1
) == ADDR_EXPR
11262 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0))
11263 && !from_macro_expansion_at (location
))
11265 if (code
== EQ_EXPR
)
11266 warning_at (location
,
11268 "the comparison will always evaluate as %<false%> "
11269 "for the address of %qD will never be NULL",
11270 TREE_OPERAND (op1
, 0));
11272 warning_at (location
,
11274 "the comparison will always evaluate as %<true%> "
11275 "for the address of %qD will never be NULL",
11276 TREE_OPERAND (op1
, 0));
11278 result_type
= type1
;
11280 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
11282 tree tt0
= TREE_TYPE (type0
);
11283 tree tt1
= TREE_TYPE (type1
);
11284 addr_space_t as0
= TYPE_ADDR_SPACE (tt0
);
11285 addr_space_t as1
= TYPE_ADDR_SPACE (tt1
);
11286 addr_space_t as_common
= ADDR_SPACE_GENERIC
;
11288 /* Anything compares with void *. void * compares with anything.
11289 Otherwise, the targets must be compatible
11290 and both must be object or both incomplete. */
11291 if (comp_target_types (location
, type0
, type1
))
11292 result_type
= common_pointer_type (type0
, type1
);
11293 else if (!addr_space_superset (as0
, as1
, &as_common
))
11295 error_at (location
, "comparison of pointers to "
11296 "disjoint address spaces");
11297 return error_mark_node
;
11299 else if (VOID_TYPE_P (tt0
) && !TYPE_ATOMIC (tt0
))
11301 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
)
11302 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
11303 "comparison of %<void *%> with function pointer");
11305 else if (VOID_TYPE_P (tt1
) && !TYPE_ATOMIC (tt1
))
11307 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
)
11308 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
11309 "comparison of %<void *%> with function pointer");
11312 /* Avoid warning about the volatile ObjC EH puts on decls. */
11314 pedwarn (location
, 0,
11315 "comparison of distinct pointer types lacks a cast");
11317 if (result_type
== NULL_TREE
)
11319 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
11320 result_type
= build_pointer_type
11321 (build_qualified_type (void_type_node
, qual
));
11324 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
11326 result_type
= type0
;
11327 pedwarn (location
, 0, "comparison between pointer and integer");
11329 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
11331 result_type
= type1
;
11332 pedwarn (location
, 0, "comparison between pointer and integer");
11334 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
11335 || truth_value_p (TREE_CODE (orig_op0
)))
11336 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
11337 || truth_value_p (TREE_CODE (orig_op1
))))
11338 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
11345 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
11348 if (!vector_types_compatible_elements_p (type0
, type1
))
11350 error_at (location
, "comparing vectors with different "
11352 return error_mark_node
;
11355 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
11357 error_at (location
, "comparing vectors with different "
11358 "number of elements");
11359 return error_mark_node
;
11362 /* It's not precisely specified how the usual arithmetic
11363 conversions apply to the vector types. Here, we use
11364 the unsigned type if one of the operands is signed and
11365 the other one is unsigned. */
11366 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
11368 if (!TYPE_UNSIGNED (type0
))
11369 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
11371 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
11372 warning_at (location
, OPT_Wsign_compare
, "comparison between "
11373 "types %qT and %qT", type0
, type1
);
11376 /* Always construct signed integer vector type. */
11377 intt
= c_common_type_for_size (GET_MODE_BITSIZE
11378 (TYPE_MODE (TREE_TYPE (type0
))), 0);
11379 result_type
= build_opaque_vector_type (intt
,
11380 TYPE_VECTOR_SUBPARTS (type0
));
11382 ret
= build_vec_cmp (resultcode
, result_type
, op0
, op1
);
11383 goto return_build_binary_op
;
11385 build_type
= integer_type_node
;
11386 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
11387 || code0
== FIXED_POINT_TYPE
)
11388 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
11389 || code1
== FIXED_POINT_TYPE
))
11391 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
11393 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (type0
));
11394 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
11395 addr_space_t as_common
;
11397 if (comp_target_types (location
, type0
, type1
))
11399 result_type
= common_pointer_type (type0
, type1
);
11400 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
11401 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
11402 pedwarn (location
, 0,
11403 "comparison of complete and incomplete pointers");
11404 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
11405 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
11406 "ordered comparisons of pointers to functions");
11407 else if (null_pointer_constant_p (orig_op0
)
11408 || null_pointer_constant_p (orig_op1
))
11409 warning_at (location
, OPT_Wextra
,
11410 "ordered comparison of pointer with null pointer");
11413 else if (!addr_space_superset (as0
, as1
, &as_common
))
11415 error_at (location
, "comparison of pointers to "
11416 "disjoint address spaces");
11417 return error_mark_node
;
11421 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
11422 result_type
= build_pointer_type
11423 (build_qualified_type (void_type_node
, qual
));
11424 pedwarn (location
, 0,
11425 "comparison of distinct pointer types lacks a cast");
11428 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
11430 result_type
= type0
;
11432 pedwarn (location
, OPT_Wpedantic
,
11433 "ordered comparison of pointer with integer zero");
11434 else if (extra_warnings
)
11435 warning_at (location
, OPT_Wextra
,
11436 "ordered comparison of pointer with integer zero");
11438 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
11440 result_type
= type1
;
11442 pedwarn (location
, OPT_Wpedantic
,
11443 "ordered comparison of pointer with integer zero");
11444 else if (extra_warnings
)
11445 warning_at (location
, OPT_Wextra
,
11446 "ordered comparison of pointer with integer zero");
11448 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
11450 result_type
= type0
;
11451 pedwarn (location
, 0, "comparison between pointer and integer");
11453 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
11455 result_type
= type1
;
11456 pedwarn (location
, 0, "comparison between pointer and integer");
11458 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
11459 || truth_value_p (TREE_CODE (orig_op0
)))
11460 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
11461 || truth_value_p (TREE_CODE (orig_op1
))))
11462 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
11466 gcc_unreachable ();
11469 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
11470 return error_mark_node
;
11472 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11473 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
11474 || !vector_types_compatible_elements_p (type0
, type1
)))
11476 gcc_rich_location
richloc (location
);
11477 richloc
.maybe_add_expr (orig_op0
);
11478 richloc
.maybe_add_expr (orig_op1
);
11479 binary_op_error (&richloc
, code
, type0
, type1
);
11480 return error_mark_node
;
11483 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
11484 || code0
== FIXED_POINT_TYPE
|| code0
== VECTOR_TYPE
)
11486 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
11487 || code1
== FIXED_POINT_TYPE
|| code1
== VECTOR_TYPE
))
11489 bool first_complex
= (code0
== COMPLEX_TYPE
);
11490 bool second_complex
= (code1
== COMPLEX_TYPE
);
11491 int none_complex
= (!first_complex
&& !second_complex
);
11493 if (shorten
|| common
|| short_compare
)
11495 result_type
= c_common_type (type0
, type1
);
11496 do_warn_double_promotion (result_type
, type0
, type1
,
11497 "implicit conversion from %qT to %qT "
11498 "to match other operand of binary "
11501 if (result_type
== error_mark_node
)
11502 return error_mark_node
;
11505 if (first_complex
!= second_complex
11506 && (code
== PLUS_EXPR
11507 || code
== MINUS_EXPR
11508 || code
== MULT_EXPR
11509 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
11510 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
11511 && flag_signed_zeros
)
11513 /* An operation on mixed real/complex operands must be
11514 handled specially, but the language-independent code can
11515 more easily optimize the plain complex arithmetic if
11516 -fno-signed-zeros. */
11517 tree real_type
= TREE_TYPE (result_type
);
11519 if (type0
!= orig_type0
|| type1
!= orig_type1
)
11521 gcc_assert (may_need_excess_precision
&& common
);
11522 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
11526 if (TREE_TYPE (op0
) != result_type
)
11527 op0
= convert_and_check (location
, result_type
, op0
);
11528 if (TREE_TYPE (op1
) != real_type
)
11529 op1
= convert_and_check (location
, real_type
, op1
);
11533 if (TREE_TYPE (op0
) != real_type
)
11534 op0
= convert_and_check (location
, real_type
, op0
);
11535 if (TREE_TYPE (op1
) != result_type
)
11536 op1
= convert_and_check (location
, result_type
, op1
);
11538 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
11539 return error_mark_node
;
11542 op0
= c_save_expr (op0
);
11543 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
11545 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
11550 case TRUNC_DIV_EXPR
:
11551 op1
= c_save_expr (op1
);
11552 imag
= build2 (resultcode
, real_type
, imag
, op1
);
11553 /* Fall through. */
11556 real
= build2 (resultcode
, real_type
, real
, op1
);
11564 op1
= c_save_expr (op1
);
11565 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
11567 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
11572 op0
= c_save_expr (op0
);
11573 imag
= build2 (resultcode
, real_type
, op0
, imag
);
11574 /* Fall through. */
11576 real
= build2 (resultcode
, real_type
, op0
, real
);
11579 real
= build2 (resultcode
, real_type
, op0
, real
);
11580 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
11586 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
11587 goto return_build_binary_op
;
11590 /* For certain operations (which identify themselves by shorten != 0)
11591 if both args were extended from the same smaller type,
11592 do the arithmetic in that type and then extend.
11594 shorten !=0 and !=1 indicates a bitwise operation.
11595 For them, this optimization is safe only if
11596 both args are zero-extended or both are sign-extended.
11597 Otherwise, we might change the result.
11598 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11599 but calculated in (unsigned short) it would be (unsigned short)-1. */
11601 if (shorten
&& none_complex
)
11603 final_type
= result_type
;
11604 result_type
= shorten_binary_op (result_type
, op0
, op1
,
11608 /* Shifts can be shortened if shifting right. */
11613 tree arg0
= get_narrower (op0
, &unsigned_arg
);
11615 final_type
= result_type
;
11617 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
11618 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
11620 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
11621 && tree_int_cst_sgn (op1
) > 0
11622 /* We can shorten only if the shift count is less than the
11623 number of bits in the smaller type size. */
11624 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
11625 /* We cannot drop an unsigned shift after sign-extension. */
11626 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
11628 /* Do an unsigned shift if the operand was zero-extended. */
11630 = c_common_signed_or_unsigned_type (unsigned_arg
,
11632 /* Convert value-to-be-shifted to that type. */
11633 if (TREE_TYPE (op0
) != result_type
)
11634 op0
= convert (result_type
, op0
);
11639 /* Comparison operations are shortened too but differently.
11640 They identify themselves by setting short_compare = 1. */
11644 /* Don't write &op0, etc., because that would prevent op0
11645 from being kept in a register.
11646 Instead, make copies of the our local variables and
11647 pass the copies by reference, then copy them back afterward. */
11648 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
11649 enum tree_code xresultcode
= resultcode
;
11651 = shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
11657 goto return_build_binary_op
;
11660 op0
= xop0
, op1
= xop1
;
11662 resultcode
= xresultcode
;
11664 if (c_inhibit_evaluation_warnings
== 0)
11666 bool op0_maybe_const
= true;
11667 bool op1_maybe_const
= true;
11668 tree orig_op0_folded
, orig_op1_folded
;
11670 if (in_late_binary_op
)
11672 orig_op0_folded
= orig_op0
;
11673 orig_op1_folded
= orig_op1
;
11677 /* Fold for the sake of possible warnings, as in
11678 build_conditional_expr. This requires the
11679 "original" values to be folded, not just op0 and
11681 c_inhibit_evaluation_warnings
++;
11682 op0
= c_fully_fold (op0
, require_constant_value
,
11684 op1
= c_fully_fold (op1
, require_constant_value
,
11686 c_inhibit_evaluation_warnings
--;
11687 orig_op0_folded
= c_fully_fold (orig_op0
,
11688 require_constant_value
,
11690 orig_op1_folded
= c_fully_fold (orig_op1
,
11691 require_constant_value
,
11695 if (warn_sign_compare
)
11696 warn_for_sign_compare (location
, orig_op0_folded
,
11697 orig_op1_folded
, op0
, op1
,
11698 result_type
, resultcode
);
11699 if (!in_late_binary_op
&& !int_operands
)
11701 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
11702 op0
= c_wrap_maybe_const (op0
, !op0_maybe_const
);
11703 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
11704 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
11710 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11711 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11712 Then the expression will be built.
11713 It will be given type FINAL_TYPE if that is nonzero;
11714 otherwise, it will be given type RESULT_TYPE. */
11718 gcc_rich_location
richloc (location
);
11719 richloc
.maybe_add_expr (orig_op0
);
11720 richloc
.maybe_add_expr (orig_op1
);
11721 binary_op_error (&richloc
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
11722 return error_mark_node
;
11725 if (build_type
== NULL_TREE
)
11727 build_type
= result_type
;
11728 if ((type0
!= orig_type0
|| type1
!= orig_type1
)
11731 gcc_assert (may_need_excess_precision
&& common
);
11732 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
11738 op0
= ep_convert_and_check (location
, result_type
, op0
,
11739 semantic_result_type
);
11740 op1
= ep_convert_and_check (location
, result_type
, op1
,
11741 semantic_result_type
);
11743 /* This can happen if one operand has a vector type, and the other
11744 has a different type. */
11745 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
11746 return error_mark_node
;
11749 if ((flag_sanitize
& (SANITIZE_SHIFT
| SANITIZE_DIVIDE
11750 | SANITIZE_FLOAT_DIVIDE
))
11751 && do_ubsan_in_current_function ()
11752 && (doing_div_or_mod
|| doing_shift
)
11753 && !require_constant_value
)
11755 /* OP0 and/or OP1 might have side-effects. */
11756 op0
= c_save_expr (op0
);
11757 op1
= c_save_expr (op1
);
11758 op0
= c_fully_fold (op0
, false, NULL
);
11759 op1
= c_fully_fold (op1
, false, NULL
);
11760 if (doing_div_or_mod
&& (flag_sanitize
& (SANITIZE_DIVIDE
11761 | SANITIZE_FLOAT_DIVIDE
)))
11762 instrument_expr
= ubsan_instrument_division (location
, op0
, op1
);
11763 else if (doing_shift
&& (flag_sanitize
& SANITIZE_SHIFT
))
11764 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
11767 /* Treat expressions in initializers specially as they can't trap. */
11768 if (int_const_or_overflow
)
11769 ret
= (require_constant_value
11770 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
11772 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
11774 ret
= build2 (resultcode
, build_type
, op0
, op1
);
11775 if (final_type
!= 0)
11776 ret
= convert (final_type
, ret
);
11778 return_build_binary_op
:
11779 gcc_assert (ret
!= error_mark_node
);
11780 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
11781 ret
= (int_operands
11782 ? note_integer_operands (ret
)
11783 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
11784 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
11785 && !in_late_binary_op
)
11786 ret
= note_integer_operands (ret
);
11787 if (semantic_result_type
)
11788 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
11789 protected_set_expr_location (ret
, location
);
11791 if (instrument_expr
!= NULL
)
11792 ret
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (ret
),
11793 instrument_expr
, ret
);
11799 /* Convert EXPR to be a truth-value, validating its type for this
11800 purpose. LOCATION is the source location for the expression. */
11803 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
11805 bool int_const
, int_operands
;
11807 switch (TREE_CODE (TREE_TYPE (expr
)))
11810 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
11811 return error_mark_node
;
11814 error_at (location
, "used struct type value where scalar is required");
11815 return error_mark_node
;
11818 error_at (location
, "used union type value where scalar is required");
11819 return error_mark_node
;
11822 error_at (location
, "void value not ignored as it ought to be");
11823 return error_mark_node
;
11826 if (reject_gcc_builtin (expr
))
11827 return error_mark_node
;
11830 case FUNCTION_TYPE
:
11831 gcc_unreachable ();
11834 error_at (location
, "used vector type where scalar is required");
11835 return error_mark_node
;
11841 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
11842 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
11843 if (int_operands
&& TREE_CODE (expr
) != INTEGER_CST
)
11845 expr
= remove_c_maybe_const_expr (expr
);
11846 expr
= build2 (NE_EXPR
, integer_type_node
, expr
,
11847 convert (TREE_TYPE (expr
), integer_zero_node
));
11848 expr
= note_integer_operands (expr
);
11851 /* ??? Should we also give an error for vectors rather than leaving
11852 those to give errors later? */
11853 expr
= c_common_truthvalue_conversion (location
, expr
);
11855 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
11857 if (TREE_OVERFLOW (expr
))
11860 return note_integer_operands (expr
);
11862 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
11863 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
11868 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11872 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
11874 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
11876 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
11877 /* Executing a compound literal inside a function reinitializes
11879 if (!TREE_STATIC (decl
))
11887 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11888 statement. LOC is the location of the construct. */
11891 c_finish_omp_construct (location_t loc
, enum tree_code code
, tree body
,
11894 body
= c_end_compound_stmt (loc
, body
, true);
11896 tree stmt
= make_node (code
);
11897 TREE_TYPE (stmt
) = void_type_node
;
11898 OMP_BODY (stmt
) = body
;
11899 OMP_CLAUSES (stmt
) = clauses
;
11900 SET_EXPR_LOCATION (stmt
, loc
);
11902 return add_stmt (stmt
);
11905 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11906 statement. LOC is the location of the OACC_DATA. */
11909 c_finish_oacc_data (location_t loc
, tree clauses
, tree block
)
11913 block
= c_end_compound_stmt (loc
, block
, true);
11915 stmt
= make_node (OACC_DATA
);
11916 TREE_TYPE (stmt
) = void_type_node
;
11917 OACC_DATA_CLAUSES (stmt
) = clauses
;
11918 OACC_DATA_BODY (stmt
) = block
;
11919 SET_EXPR_LOCATION (stmt
, loc
);
11921 return add_stmt (stmt
);
11924 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
11925 statement. LOC is the location of the OACC_HOST_DATA. */
11928 c_finish_oacc_host_data (location_t loc
, tree clauses
, tree block
)
11932 block
= c_end_compound_stmt (loc
, block
, true);
11934 stmt
= make_node (OACC_HOST_DATA
);
11935 TREE_TYPE (stmt
) = void_type_node
;
11936 OACC_HOST_DATA_CLAUSES (stmt
) = clauses
;
11937 OACC_HOST_DATA_BODY (stmt
) = block
;
11938 SET_EXPR_LOCATION (stmt
, loc
);
11940 return add_stmt (stmt
);
11943 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11946 c_begin_omp_parallel (void)
11950 keep_next_level ();
11951 block
= c_begin_compound_stmt (true);
11956 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11957 statement. LOC is the location of the OMP_PARALLEL. */
11960 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
11964 block
= c_end_compound_stmt (loc
, block
, true);
11966 stmt
= make_node (OMP_PARALLEL
);
11967 TREE_TYPE (stmt
) = void_type_node
;
11968 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
11969 OMP_PARALLEL_BODY (stmt
) = block
;
11970 SET_EXPR_LOCATION (stmt
, loc
);
11972 return add_stmt (stmt
);
11975 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11978 c_begin_omp_task (void)
11982 keep_next_level ();
11983 block
= c_begin_compound_stmt (true);
11988 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11989 statement. LOC is the location of the #pragma. */
11992 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
11996 block
= c_end_compound_stmt (loc
, block
, true);
11998 stmt
= make_node (OMP_TASK
);
11999 TREE_TYPE (stmt
) = void_type_node
;
12000 OMP_TASK_CLAUSES (stmt
) = clauses
;
12001 OMP_TASK_BODY (stmt
) = block
;
12002 SET_EXPR_LOCATION (stmt
, loc
);
12004 return add_stmt (stmt
);
12007 /* Generate GOMP_cancel call for #pragma omp cancel. */
12010 c_finish_omp_cancel (location_t loc
, tree clauses
)
12012 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCEL
);
12014 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
12016 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
12018 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
12020 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
12024 error_at (loc
, "%<#pragma omp cancel%> must specify one of "
12025 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12029 tree ifc
= find_omp_clause (clauses
, OMP_CLAUSE_IF
);
12030 if (ifc
!= NULL_TREE
)
12032 tree type
= TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc
));
12033 ifc
= fold_build2_loc (OMP_CLAUSE_LOCATION (ifc
), NE_EXPR
,
12034 boolean_type_node
, OMP_CLAUSE_IF_EXPR (ifc
),
12035 build_zero_cst (type
));
12038 ifc
= boolean_true_node
;
12039 tree stmt
= build_call_expr_loc (loc
, fn
, 2,
12040 build_int_cst (integer_type_node
, mask
),
12045 /* Generate GOMP_cancellation_point call for
12046 #pragma omp cancellation point. */
12049 c_finish_omp_cancellation_point (location_t loc
, tree clauses
)
12051 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT
);
12053 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
12055 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
12057 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
12059 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
12063 error_at (loc
, "%<#pragma omp cancellation point%> must specify one of "
12064 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12068 tree stmt
= build_call_expr_loc (loc
, fn
, 1,
12069 build_int_cst (integer_type_node
, mask
));
12073 /* Helper function for handle_omp_array_sections. Called recursively
12074 to handle multiple array-section-subscripts. C is the clause,
12075 T current expression (initially OMP_CLAUSE_DECL), which is either
12076 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12077 expression if specified, TREE_VALUE length expression if specified,
12078 TREE_CHAIN is what it has been specified after, or some decl.
12079 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12080 set to true if any of the array-section-subscript could have length
12081 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12082 first array-section-subscript which is known not to have length
12084 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12085 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12086 all are or may have length of 1, array-section-subscript [:2] is the
12087 first one known not to have length 1. For array-section-subscript
12088 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12089 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12090 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12091 case though, as some lengths could be zero. */
12094 handle_omp_array_sections_1 (tree c
, tree t
, vec
<tree
> &types
,
12095 bool &maybe_zero_len
, unsigned int &first_non_one
,
12096 enum c_omp_region_type ort
)
12098 tree ret
, low_bound
, length
, type
;
12099 if (TREE_CODE (t
) != TREE_LIST
)
12101 if (error_operand_p (t
))
12102 return error_mark_node
;
12104 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12105 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t
))))
12107 error_at (OMP_CLAUSE_LOCATION (c
), "%<_Atomic%> %qE in %qs clause",
12108 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12109 return error_mark_node
;
12111 if (TREE_CODE (t
) == COMPONENT_REF
12112 && ort
== C_ORT_OMP
12113 && (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
12114 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO
12115 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FROM
))
12117 if (DECL_BIT_FIELD (TREE_OPERAND (t
, 1)))
12119 error_at (OMP_CLAUSE_LOCATION (c
),
12120 "bit-field %qE in %qs clause",
12121 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12122 return error_mark_node
;
12124 while (TREE_CODE (t
) == COMPONENT_REF
)
12126 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == UNION_TYPE
)
12128 error_at (OMP_CLAUSE_LOCATION (c
),
12129 "%qE is a member of a union", t
);
12130 return error_mark_node
;
12132 t
= TREE_OPERAND (t
, 0);
12135 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
12138 error_at (OMP_CLAUSE_LOCATION (c
),
12139 "%qD is not a variable in %qs clause", t
,
12140 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12142 error_at (OMP_CLAUSE_LOCATION (c
),
12143 "%qE is not a variable in %qs clause", t
,
12144 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12145 return error_mark_node
;
12147 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12148 && TYPE_ATOMIC (TREE_TYPE (t
)))
12150 error_at (OMP_CLAUSE_LOCATION (c
), "%<_Atomic%> %qD in %qs clause",
12151 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12152 return error_mark_node
;
12154 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12156 && DECL_THREAD_LOCAL_P (t
))
12158 error_at (OMP_CLAUSE_LOCATION (c
),
12159 "%qD is threadprivate variable in %qs clause", t
,
12160 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12161 return error_mark_node
;
12163 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
12164 && TYPE_ATOMIC (TREE_TYPE (t
))
12165 && POINTER_TYPE_P (TREE_TYPE (t
)))
12167 /* If the array section is pointer based and the pointer
12168 itself is _Atomic qualified, we need to atomically load
12171 memset (&expr
, 0, sizeof (expr
));
12173 expr
= convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c
),
12174 expr
, false, false);
12180 ret
= handle_omp_array_sections_1 (c
, TREE_CHAIN (t
), types
,
12181 maybe_zero_len
, first_non_one
, ort
);
12182 if (ret
== error_mark_node
|| ret
== NULL_TREE
)
12185 type
= TREE_TYPE (ret
);
12186 low_bound
= TREE_PURPOSE (t
);
12187 length
= TREE_VALUE (t
);
12189 if (low_bound
== error_mark_node
|| length
== error_mark_node
)
12190 return error_mark_node
;
12192 if (low_bound
&& !INTEGRAL_TYPE_P (TREE_TYPE (low_bound
)))
12194 error_at (OMP_CLAUSE_LOCATION (c
),
12195 "low bound %qE of array section does not have integral type",
12197 return error_mark_node
;
12199 if (length
&& !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
12201 error_at (OMP_CLAUSE_LOCATION (c
),
12202 "length %qE of array section does not have integral type",
12204 return error_mark_node
;
12207 && TREE_CODE (low_bound
) == INTEGER_CST
12208 && TYPE_PRECISION (TREE_TYPE (low_bound
))
12209 > TYPE_PRECISION (sizetype
))
12210 low_bound
= fold_convert (sizetype
, low_bound
);
12212 && TREE_CODE (length
) == INTEGER_CST
12213 && TYPE_PRECISION (TREE_TYPE (length
))
12214 > TYPE_PRECISION (sizetype
))
12215 length
= fold_convert (sizetype
, length
);
12216 if (low_bound
== NULL_TREE
)
12217 low_bound
= integer_zero_node
;
12219 if (length
!= NULL_TREE
)
12221 if (!integer_nonzerop (length
))
12223 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
12224 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
12226 if (integer_zerop (length
))
12228 error_at (OMP_CLAUSE_LOCATION (c
),
12229 "zero length array section in %qs clause",
12230 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12231 return error_mark_node
;
12235 maybe_zero_len
= true;
12237 if (first_non_one
== types
.length ()
12238 && (TREE_CODE (length
) != INTEGER_CST
|| integer_onep (length
)))
12241 if (TREE_CODE (type
) == ARRAY_TYPE
)
12243 if (length
== NULL_TREE
12244 && (TYPE_DOMAIN (type
) == NULL_TREE
12245 || TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
))
12247 error_at (OMP_CLAUSE_LOCATION (c
),
12248 "for unknown bound array type length expression must "
12250 return error_mark_node
;
12252 if (TREE_CODE (low_bound
) == INTEGER_CST
12253 && tree_int_cst_sgn (low_bound
) == -1)
12255 error_at (OMP_CLAUSE_LOCATION (c
),
12256 "negative low bound in array section in %qs clause",
12257 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12258 return error_mark_node
;
12260 if (length
!= NULL_TREE
12261 && TREE_CODE (length
) == INTEGER_CST
12262 && tree_int_cst_sgn (length
) == -1)
12264 error_at (OMP_CLAUSE_LOCATION (c
),
12265 "negative length in array section in %qs clause",
12266 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12267 return error_mark_node
;
12269 if (TYPE_DOMAIN (type
)
12270 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
12271 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
12274 tree size
= size_binop (PLUS_EXPR
,
12275 TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
12277 if (TREE_CODE (low_bound
) == INTEGER_CST
)
12279 if (tree_int_cst_lt (size
, low_bound
))
12281 error_at (OMP_CLAUSE_LOCATION (c
),
12282 "low bound %qE above array section size "
12283 "in %qs clause", low_bound
,
12284 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12285 return error_mark_node
;
12287 if (tree_int_cst_equal (size
, low_bound
))
12289 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
12290 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
12292 error_at (OMP_CLAUSE_LOCATION (c
),
12293 "zero length array section in %qs clause",
12294 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12295 return error_mark_node
;
12297 maybe_zero_len
= true;
12299 else if (length
== NULL_TREE
12300 && first_non_one
== types
.length ()
12301 && tree_int_cst_equal
12302 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
12306 else if (length
== NULL_TREE
)
12308 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12309 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
)
12310 maybe_zero_len
= true;
12311 if (first_non_one
== types
.length ())
12314 if (length
&& TREE_CODE (length
) == INTEGER_CST
)
12316 if (tree_int_cst_lt (size
, length
))
12318 error_at (OMP_CLAUSE_LOCATION (c
),
12319 "length %qE above array section size "
12320 "in %qs clause", length
,
12321 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12322 return error_mark_node
;
12324 if (TREE_CODE (low_bound
) == INTEGER_CST
)
12327 = size_binop (PLUS_EXPR
,
12328 fold_convert (sizetype
, low_bound
),
12329 fold_convert (sizetype
, length
));
12330 if (TREE_CODE (lbpluslen
) == INTEGER_CST
12331 && tree_int_cst_lt (size
, lbpluslen
))
12333 error_at (OMP_CLAUSE_LOCATION (c
),
12334 "high bound %qE above array section size "
12335 "in %qs clause", lbpluslen
,
12336 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12337 return error_mark_node
;
12342 else if (length
== NULL_TREE
)
12344 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12345 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
)
12346 maybe_zero_len
= true;
12347 if (first_non_one
== types
.length ())
12351 /* For [lb:] we will need to evaluate lb more than once. */
12352 if (length
== NULL_TREE
&& OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
12354 tree lb
= c_save_expr (low_bound
);
12355 if (lb
!= low_bound
)
12357 TREE_PURPOSE (t
) = lb
;
12362 else if (TREE_CODE (type
) == POINTER_TYPE
)
12364 if (length
== NULL_TREE
)
12366 error_at (OMP_CLAUSE_LOCATION (c
),
12367 "for pointer type length expression must be specified");
12368 return error_mark_node
;
12370 if (length
!= NULL_TREE
12371 && TREE_CODE (length
) == INTEGER_CST
12372 && tree_int_cst_sgn (length
) == -1)
12374 error_at (OMP_CLAUSE_LOCATION (c
),
12375 "negative length in array section in %qs clause",
12376 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12377 return error_mark_node
;
12379 /* If there is a pointer type anywhere but in the very first
12380 array-section-subscript, the array section can't be contiguous. */
12381 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12382 && TREE_CODE (TREE_CHAIN (t
)) == TREE_LIST
)
12384 error_at (OMP_CLAUSE_LOCATION (c
),
12385 "array section is not contiguous in %qs clause",
12386 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12387 return error_mark_node
;
12392 error_at (OMP_CLAUSE_LOCATION (c
),
12393 "%qE does not have pointer or array type", ret
);
12394 return error_mark_node
;
12396 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
12397 types
.safe_push (TREE_TYPE (ret
));
12398 /* We will need to evaluate lb more than once. */
12399 tree lb
= c_save_expr (low_bound
);
12400 if (lb
!= low_bound
)
12402 TREE_PURPOSE (t
) = lb
;
12405 ret
= build_array_ref (OMP_CLAUSE_LOCATION (c
), ret
, low_bound
);
12409 /* Handle array sections for clause C. */
12412 handle_omp_array_sections (tree c
, enum c_omp_region_type ort
)
12414 bool maybe_zero_len
= false;
12415 unsigned int first_non_one
= 0;
12416 auto_vec
<tree
, 10> types
;
12417 tree first
= handle_omp_array_sections_1 (c
, OMP_CLAUSE_DECL (c
), types
,
12418 maybe_zero_len
, first_non_one
,
12420 if (first
== error_mark_node
)
12422 if (first
== NULL_TREE
)
12424 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
)
12426 tree t
= OMP_CLAUSE_DECL (c
);
12427 tree tem
= NULL_TREE
;
12428 /* Need to evaluate side effects in the length expressions
12430 while (TREE_CODE (t
) == TREE_LIST
)
12432 if (TREE_VALUE (t
) && TREE_SIDE_EFFECTS (TREE_VALUE (t
)))
12434 if (tem
== NULL_TREE
)
12435 tem
= TREE_VALUE (t
);
12437 tem
= build2 (COMPOUND_EXPR
, TREE_TYPE (tem
),
12438 TREE_VALUE (t
), tem
);
12440 t
= TREE_CHAIN (t
);
12443 first
= build2 (COMPOUND_EXPR
, TREE_TYPE (first
), tem
, first
);
12444 first
= c_fully_fold (first
, false, NULL
);
12445 OMP_CLAUSE_DECL (c
) = first
;
12449 unsigned int num
= types
.length (), i
;
12450 tree t
, side_effects
= NULL_TREE
, size
= NULL_TREE
;
12451 tree condition
= NULL_TREE
;
12453 if (int_size_in_bytes (TREE_TYPE (first
)) <= 0)
12454 maybe_zero_len
= true;
12456 for (i
= num
, t
= OMP_CLAUSE_DECL (c
); i
> 0;
12457 t
= TREE_CHAIN (t
))
12459 tree low_bound
= TREE_PURPOSE (t
);
12460 tree length
= TREE_VALUE (t
);
12464 && TREE_CODE (low_bound
) == INTEGER_CST
12465 && TYPE_PRECISION (TREE_TYPE (low_bound
))
12466 > TYPE_PRECISION (sizetype
))
12467 low_bound
= fold_convert (sizetype
, low_bound
);
12469 && TREE_CODE (length
) == INTEGER_CST
12470 && TYPE_PRECISION (TREE_TYPE (length
))
12471 > TYPE_PRECISION (sizetype
))
12472 length
= fold_convert (sizetype
, length
);
12473 if (low_bound
== NULL_TREE
)
12474 low_bound
= integer_zero_node
;
12475 if (!maybe_zero_len
&& i
> first_non_one
)
12477 if (integer_nonzerop (low_bound
))
12478 goto do_warn_noncontiguous
;
12479 if (length
!= NULL_TREE
12480 && TREE_CODE (length
) == INTEGER_CST
12481 && TYPE_DOMAIN (types
[i
])
12482 && TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
]))
12483 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])))
12487 size
= size_binop (PLUS_EXPR
,
12488 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
12490 if (!tree_int_cst_equal (length
, size
))
12492 do_warn_noncontiguous
:
12493 error_at (OMP_CLAUSE_LOCATION (c
),
12494 "array section is not contiguous in %qs "
12496 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12500 if (length
!= NULL_TREE
12501 && TREE_SIDE_EFFECTS (length
))
12503 if (side_effects
== NULL_TREE
)
12504 side_effects
= length
;
12506 side_effects
= build2 (COMPOUND_EXPR
,
12507 TREE_TYPE (side_effects
),
12508 length
, side_effects
);
12515 if (i
> first_non_one
12516 && ((length
&& integer_nonzerop (length
))
12517 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
))
12520 l
= fold_convert (sizetype
, length
);
12523 l
= size_binop (PLUS_EXPR
,
12524 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
12526 l
= size_binop (MINUS_EXPR
, l
,
12527 fold_convert (sizetype
, low_bound
));
12529 if (i
> first_non_one
)
12531 l
= fold_build2 (NE_EXPR
, boolean_type_node
, l
,
12533 if (condition
== NULL_TREE
)
12536 condition
= fold_build2 (BIT_AND_EXPR
, boolean_type_node
,
12539 else if (size
== NULL_TREE
)
12541 size
= size_in_bytes (TREE_TYPE (types
[i
]));
12542 tree eltype
= TREE_TYPE (types
[num
- 1]);
12543 while (TREE_CODE (eltype
) == ARRAY_TYPE
)
12544 eltype
= TREE_TYPE (eltype
);
12545 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
12547 if (integer_zerop (size
)
12548 || integer_zerop (size_in_bytes (eltype
)))
12550 error_at (OMP_CLAUSE_LOCATION (c
),
12551 "zero length array section in %qs clause",
12552 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12553 return error_mark_node
;
12555 size
= size_binop (EXACT_DIV_EXPR
, size
,
12556 size_in_bytes (eltype
));
12558 size
= size_binop (MULT_EXPR
, size
, l
);
12560 size
= fold_build3 (COND_EXPR
, sizetype
, condition
,
12561 size
, size_zero_node
);
12564 size
= size_binop (MULT_EXPR
, size
, l
);
12568 size
= build2 (COMPOUND_EXPR
, sizetype
, side_effects
, size
);
12569 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
12571 size
= size_binop (MINUS_EXPR
, size
, size_one_node
);
12572 size
= c_fully_fold (size
, false, NULL
);
12573 tree index_type
= build_index_type (size
);
12574 tree eltype
= TREE_TYPE (first
);
12575 while (TREE_CODE (eltype
) == ARRAY_TYPE
)
12576 eltype
= TREE_TYPE (eltype
);
12577 tree type
= build_array_type (eltype
, index_type
);
12578 tree ptype
= build_pointer_type (eltype
);
12579 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
12580 t
= build_fold_addr_expr (t
);
12581 tree t2
= build_fold_addr_expr (first
);
12582 t2
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
12583 ptrdiff_type_node
, t2
);
12584 t2
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
12585 ptrdiff_type_node
, t2
,
12586 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
12587 ptrdiff_type_node
, t
));
12588 t2
= c_fully_fold (t2
, false, NULL
);
12589 if (tree_fits_shwi_p (t2
))
12590 t
= build2 (MEM_REF
, type
, t
,
12591 build_int_cst (ptype
, tree_to_shwi (t2
)));
12594 t2
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), sizetype
, t2
);
12595 t
= build2_loc (OMP_CLAUSE_LOCATION (c
), POINTER_PLUS_EXPR
,
12596 TREE_TYPE (t
), t
, t2
);
12597 t
= build2 (MEM_REF
, type
, t
, build_int_cst (ptype
, 0));
12599 OMP_CLAUSE_DECL (c
) = t
;
12602 first
= c_fully_fold (first
, false, NULL
);
12603 OMP_CLAUSE_DECL (c
) = first
;
12605 size
= c_fully_fold (size
, false, NULL
);
12606 OMP_CLAUSE_SIZE (c
) = size
;
12607 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
12608 || (TREE_CODE (t
) == COMPONENT_REF
12609 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
))
12611 gcc_assert (OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_FORCE_DEVICEPTR
);
12612 if (ort
== C_ORT_OMP
|| ort
== C_ORT_ACC
)
12613 switch (OMP_CLAUSE_MAP_KIND (c
))
12615 case GOMP_MAP_ALLOC
:
12617 case GOMP_MAP_FROM
:
12618 case GOMP_MAP_TOFROM
:
12619 case GOMP_MAP_ALWAYS_TO
:
12620 case GOMP_MAP_ALWAYS_FROM
:
12621 case GOMP_MAP_ALWAYS_TOFROM
:
12622 case GOMP_MAP_RELEASE
:
12623 case GOMP_MAP_DELETE
:
12624 case GOMP_MAP_FORCE_TO
:
12625 case GOMP_MAP_FORCE_FROM
:
12626 case GOMP_MAP_FORCE_TOFROM
:
12627 case GOMP_MAP_FORCE_PRESENT
:
12628 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c
) = 1;
12633 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_MAP
);
12634 if (ort
!= C_ORT_OMP
&& ort
!= C_ORT_ACC
)
12635 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_POINTER
);
12636 else if (TREE_CODE (t
) == COMPONENT_REF
)
12637 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_ALWAYS_POINTER
);
12639 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_FIRSTPRIVATE_POINTER
);
12640 if (OMP_CLAUSE_MAP_KIND (c2
) != GOMP_MAP_FIRSTPRIVATE_POINTER
12641 && !c_mark_addressable (t
))
12643 OMP_CLAUSE_DECL (c2
) = t
;
12644 t
= build_fold_addr_expr (first
);
12645 t
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), ptrdiff_type_node
, t
);
12646 tree ptr
= OMP_CLAUSE_DECL (c2
);
12647 if (!POINTER_TYPE_P (TREE_TYPE (ptr
)))
12648 ptr
= build_fold_addr_expr (ptr
);
12649 t
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
12650 ptrdiff_type_node
, t
,
12651 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
12652 ptrdiff_type_node
, ptr
));
12653 t
= c_fully_fold (t
, false, NULL
);
12654 OMP_CLAUSE_SIZE (c2
) = t
;
12655 OMP_CLAUSE_CHAIN (c2
) = OMP_CLAUSE_CHAIN (c
);
12656 OMP_CLAUSE_CHAIN (c
) = c2
;
12661 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12662 an inline call. But, remap
12663 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12664 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12667 c_clone_omp_udr (tree stmt
, tree omp_decl1
, tree omp_decl2
,
12668 tree decl
, tree placeholder
)
12671 hash_map
<tree
, tree
> decl_map
;
12673 decl_map
.put (omp_decl1
, placeholder
);
12674 decl_map
.put (omp_decl2
, decl
);
12675 memset (&id
, 0, sizeof (id
));
12676 id
.src_fn
= DECL_CONTEXT (omp_decl1
);
12677 id
.dst_fn
= current_function_decl
;
12678 id
.src_cfun
= DECL_STRUCT_FUNCTION (id
.src_fn
);
12679 id
.decl_map
= &decl_map
;
12681 id
.copy_decl
= copy_decl_no_change
;
12682 id
.transform_call_graph_edges
= CB_CGE_DUPLICATE
;
12683 id
.transform_new_cfg
= true;
12684 id
.transform_return_to_modify
= false;
12685 id
.transform_lang_insert_block
= NULL
;
12687 walk_tree (&stmt
, copy_tree_body_r
, &id
, NULL
);
12691 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12692 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12695 c_find_omp_placeholder_r (tree
*tp
, int *, void *data
)
12697 if (*tp
== (tree
) data
)
12702 /* For all elements of CLAUSES, validate them against their constraints.
12703 Remove any elements from the list that are invalid. */
12706 c_finish_omp_clauses (tree clauses
, enum c_omp_region_type ort
)
12708 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
12709 bitmap_head aligned_head
, map_head
, map_field_head
, oacc_reduction_head
;
12710 tree c
, t
, type
, *pc
;
12711 tree simdlen
= NULL_TREE
, safelen
= NULL_TREE
;
12712 bool branch_seen
= false;
12713 bool copyprivate_seen
= false;
12714 bool linear_variable_step_check
= false;
12715 tree
*nowait_clause
= NULL
;
12716 bool ordered_seen
= false;
12717 tree schedule_clause
= NULL_TREE
;
12718 bool oacc_async
= false;
12720 bitmap_obstack_initialize (NULL
);
12721 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
12722 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
12723 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
12724 bitmap_initialize (&aligned_head
, &bitmap_default_obstack
);
12725 bitmap_initialize (&map_head
, &bitmap_default_obstack
);
12726 bitmap_initialize (&map_field_head
, &bitmap_default_obstack
);
12727 bitmap_initialize (&oacc_reduction_head
, &bitmap_default_obstack
);
12729 if (ort
& C_ORT_ACC
)
12730 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
12731 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_ASYNC
)
12737 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
12739 bool remove
= false;
12740 bool need_complete
= false;
12741 bool need_implicitly_determined
= false;
12743 switch (OMP_CLAUSE_CODE (c
))
12745 case OMP_CLAUSE_SHARED
:
12746 need_implicitly_determined
= true;
12747 goto check_dup_generic
;
12749 case OMP_CLAUSE_PRIVATE
:
12750 need_complete
= true;
12751 need_implicitly_determined
= true;
12752 goto check_dup_generic
;
12754 case OMP_CLAUSE_REDUCTION
:
12755 need_implicitly_determined
= true;
12756 t
= OMP_CLAUSE_DECL (c
);
12757 if (TREE_CODE (t
) == TREE_LIST
)
12759 if (handle_omp_array_sections (c
, ort
))
12765 t
= OMP_CLAUSE_DECL (c
);
12767 t
= require_complete_type (OMP_CLAUSE_LOCATION (c
), t
);
12768 if (t
== error_mark_node
)
12774 c_mark_addressable (t
);
12775 type
= TREE_TYPE (t
);
12776 if (TREE_CODE (t
) == MEM_REF
)
12777 type
= TREE_TYPE (type
);
12778 if (TREE_CODE (type
) == ARRAY_TYPE
)
12780 tree oatype
= type
;
12781 gcc_assert (TREE_CODE (t
) != MEM_REF
);
12782 while (TREE_CODE (type
) == ARRAY_TYPE
)
12783 type
= TREE_TYPE (type
);
12784 if (integer_zerop (TYPE_SIZE_UNIT (type
)))
12786 error_at (OMP_CLAUSE_LOCATION (c
),
12787 "%qD in %<reduction%> clause is a zero size array",
12792 tree size
= size_binop (EXACT_DIV_EXPR
, TYPE_SIZE_UNIT (oatype
),
12793 TYPE_SIZE_UNIT (type
));
12794 if (integer_zerop (size
))
12796 error_at (OMP_CLAUSE_LOCATION (c
),
12797 "%qD in %<reduction%> clause is a zero size array",
12802 size
= size_binop (MINUS_EXPR
, size
, size_one_node
);
12803 tree index_type
= build_index_type (size
);
12804 tree atype
= build_array_type (type
, index_type
);
12805 tree ptype
= build_pointer_type (type
);
12806 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
12807 t
= build_fold_addr_expr (t
);
12808 t
= build2 (MEM_REF
, atype
, t
, build_int_cst (ptype
, 0));
12809 OMP_CLAUSE_DECL (c
) = t
;
12811 if (TYPE_ATOMIC (type
))
12813 error_at (OMP_CLAUSE_LOCATION (c
),
12814 "%<_Atomic%> %qE in %<reduction%> clause", t
);
12818 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == NULL_TREE
12819 && (FLOAT_TYPE_P (type
)
12820 || TREE_CODE (type
) == COMPLEX_TYPE
))
12822 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
12823 const char *r_name
= NULL
;
12832 if (TREE_CODE (type
) == COMPLEX_TYPE
)
12836 if (TREE_CODE (type
) == COMPLEX_TYPE
)
12848 case TRUTH_ANDIF_EXPR
:
12849 if (FLOAT_TYPE_P (type
))
12852 case TRUTH_ORIF_EXPR
:
12853 if (FLOAT_TYPE_P (type
))
12857 gcc_unreachable ();
12861 error_at (OMP_CLAUSE_LOCATION (c
),
12862 "%qE has invalid type for %<reduction(%s)%>",
12868 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == error_mark_node
)
12870 error_at (OMP_CLAUSE_LOCATION (c
),
12871 "user defined reduction not found for %qE", t
);
12875 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
12877 tree list
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
);
12878 type
= TYPE_MAIN_VARIANT (type
);
12879 tree placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
12880 VAR_DECL
, NULL_TREE
, type
);
12881 tree decl_placeholder
= NULL_TREE
;
12882 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = placeholder
;
12883 DECL_ARTIFICIAL (placeholder
) = 1;
12884 DECL_IGNORED_P (placeholder
) = 1;
12885 if (TREE_CODE (t
) == MEM_REF
)
12887 decl_placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
12888 VAR_DECL
, NULL_TREE
, type
);
12889 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c
) = decl_placeholder
;
12890 DECL_ARTIFICIAL (decl_placeholder
) = 1;
12891 DECL_IGNORED_P (decl_placeholder
) = 1;
12893 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 0)))
12894 c_mark_addressable (placeholder
);
12895 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 1)))
12896 c_mark_addressable (decl_placeholder
? decl_placeholder
12897 : OMP_CLAUSE_DECL (c
));
12898 OMP_CLAUSE_REDUCTION_MERGE (c
)
12899 = c_clone_omp_udr (TREE_VEC_ELT (list
, 2),
12900 TREE_VEC_ELT (list
, 0),
12901 TREE_VEC_ELT (list
, 1),
12902 decl_placeholder
? decl_placeholder
12903 : OMP_CLAUSE_DECL (c
), placeholder
);
12904 OMP_CLAUSE_REDUCTION_MERGE (c
)
12905 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
12906 void_type_node
, NULL_TREE
,
12907 OMP_CLAUSE_REDUCTION_MERGE (c
), NULL_TREE
);
12908 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c
)) = 1;
12909 if (TREE_VEC_LENGTH (list
) == 6)
12911 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 3)))
12912 c_mark_addressable (decl_placeholder
? decl_placeholder
12913 : OMP_CLAUSE_DECL (c
));
12914 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 4)))
12915 c_mark_addressable (placeholder
);
12916 tree init
= TREE_VEC_ELT (list
, 5);
12917 if (init
== error_mark_node
)
12918 init
= DECL_INITIAL (TREE_VEC_ELT (list
, 3));
12919 OMP_CLAUSE_REDUCTION_INIT (c
)
12920 = c_clone_omp_udr (init
, TREE_VEC_ELT (list
, 4),
12921 TREE_VEC_ELT (list
, 3),
12922 decl_placeholder
? decl_placeholder
12923 : OMP_CLAUSE_DECL (c
), placeholder
);
12924 if (TREE_VEC_ELT (list
, 5) == error_mark_node
)
12926 tree v
= decl_placeholder
? decl_placeholder
: t
;
12927 OMP_CLAUSE_REDUCTION_INIT (c
)
12928 = build2 (INIT_EXPR
, TREE_TYPE (v
), v
,
12929 OMP_CLAUSE_REDUCTION_INIT (c
));
12931 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
12932 c_find_omp_placeholder_r
,
12933 placeholder
, NULL
))
12934 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c
) = 1;
12939 tree v
= decl_placeholder
? decl_placeholder
: t
;
12940 if (AGGREGATE_TYPE_P (TREE_TYPE (v
)))
12941 init
= build_constructor (TREE_TYPE (v
), NULL
);
12943 init
= fold_convert (TREE_TYPE (v
), integer_zero_node
);
12944 OMP_CLAUSE_REDUCTION_INIT (c
)
12945 = build2 (INIT_EXPR
, TREE_TYPE (v
), v
, init
);
12947 OMP_CLAUSE_REDUCTION_INIT (c
)
12948 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
12949 void_type_node
, NULL_TREE
,
12950 OMP_CLAUSE_REDUCTION_INIT (c
), NULL_TREE
);
12951 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c
)) = 1;
12953 if (TREE_CODE (t
) == MEM_REF
)
12955 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))) == NULL_TREE
12956 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))))
12959 sorry ("variable length element type in array "
12960 "%<reduction%> clause");
12964 t
= TREE_OPERAND (t
, 0);
12965 if (TREE_CODE (t
) == POINTER_PLUS_EXPR
)
12966 t
= TREE_OPERAND (t
, 0);
12967 if (TREE_CODE (t
) == ADDR_EXPR
)
12968 t
= TREE_OPERAND (t
, 0);
12970 goto check_dup_generic_t
;
12972 case OMP_CLAUSE_COPYPRIVATE
:
12973 copyprivate_seen
= true;
12976 error_at (OMP_CLAUSE_LOCATION (*nowait_clause
),
12977 "%<nowait%> clause must not be used together "
12978 "with %<copyprivate%>");
12979 *nowait_clause
= OMP_CLAUSE_CHAIN (*nowait_clause
);
12980 nowait_clause
= NULL
;
12982 goto check_dup_generic
;
12984 case OMP_CLAUSE_COPYIN
:
12985 t
= OMP_CLAUSE_DECL (c
);
12986 if (!VAR_P (t
) || !DECL_THREAD_LOCAL_P (t
))
12988 error_at (OMP_CLAUSE_LOCATION (c
),
12989 "%qE must be %<threadprivate%> for %<copyin%>", t
);
12993 goto check_dup_generic
;
12995 case OMP_CLAUSE_LINEAR
:
12996 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
12997 need_implicitly_determined
= true;
12998 t
= OMP_CLAUSE_DECL (c
);
12999 if (ort
!= C_ORT_OMP_DECLARE_SIMD
13000 && OMP_CLAUSE_LINEAR_KIND (c
) != OMP_CLAUSE_LINEAR_DEFAULT
)
13002 error_at (OMP_CLAUSE_LOCATION (c
),
13003 "modifier should not be specified in %<linear%> "
13004 "clause on %<simd%> or %<for%> constructs");
13005 OMP_CLAUSE_LINEAR_KIND (c
) = OMP_CLAUSE_LINEAR_DEFAULT
;
13007 if (ort
& C_ORT_CILK
)
13009 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
13010 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t
))
13011 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
13013 error_at (OMP_CLAUSE_LOCATION (c
),
13014 "linear clause applied to non-integral, "
13015 "non-floating, non-pointer variable with type %qT",
13023 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
13024 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
13026 error_at (OMP_CLAUSE_LOCATION (c
),
13027 "linear clause applied to non-integral non-pointer "
13028 "variable with type %qT", TREE_TYPE (t
));
13032 if (TYPE_ATOMIC (TREE_TYPE (t
)))
13034 error_at (OMP_CLAUSE_LOCATION (c
),
13035 "%<_Atomic%> %qD in %<linear%> clause", t
);
13040 if (ort
== C_ORT_OMP_DECLARE_SIMD
)
13042 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
13043 if (TREE_CODE (s
) == PARM_DECL
)
13045 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c
) = 1;
13046 /* map_head bitmap is used as uniform_head if
13048 if (!bitmap_bit_p (&map_head
, DECL_UID (s
)))
13049 linear_variable_step_check
= true;
13050 goto check_dup_generic
;
13052 if (TREE_CODE (s
) != INTEGER_CST
)
13054 error_at (OMP_CLAUSE_LOCATION (c
),
13055 "%<linear%> clause step %qE is neither constant "
13056 "nor a parameter", s
);
13061 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c
))) == POINTER_TYPE
)
13063 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
13064 s
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
), PLUS_EXPR
,
13065 OMP_CLAUSE_DECL (c
), s
);
13066 s
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
13067 sizetype
, fold_convert (sizetype
, s
),
13069 (sizetype
, OMP_CLAUSE_DECL (c
)));
13070 if (s
== error_mark_node
)
13072 OMP_CLAUSE_LINEAR_STEP (c
) = s
;
13075 OMP_CLAUSE_LINEAR_STEP (c
)
13076 = fold_convert (TREE_TYPE (t
), OMP_CLAUSE_LINEAR_STEP (c
));
13077 goto check_dup_generic
;
13080 t
= OMP_CLAUSE_DECL (c
);
13081 check_dup_generic_t
:
13082 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13084 error_at (OMP_CLAUSE_LOCATION (c
),
13085 "%qE is not a variable in clause %qs", t
,
13086 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13089 else if (ort
== C_ORT_ACC
13090 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
13092 if (bitmap_bit_p (&oacc_reduction_head
, DECL_UID (t
)))
13094 error ("%qD appears more than once in reduction clauses", t
);
13098 bitmap_set_bit (&oacc_reduction_head
, DECL_UID (t
));
13100 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13101 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
13102 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
13104 error_at (OMP_CLAUSE_LOCATION (c
),
13105 "%qE appears more than once in data clauses", t
);
13108 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
13109 && bitmap_bit_p (&map_head
, DECL_UID (t
)))
13111 if (ort
== C_ORT_ACC
)
13112 error ("%qD appears more than once in data clauses", t
);
13114 error ("%qD appears both in data and map clauses", t
);
13118 bitmap_set_bit (&generic_head
, DECL_UID (t
));
13121 case OMP_CLAUSE_FIRSTPRIVATE
:
13122 t
= OMP_CLAUSE_DECL (c
);
13123 need_complete
= true;
13124 need_implicitly_determined
= true;
13125 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13127 error_at (OMP_CLAUSE_LOCATION (c
),
13128 "%qE is not a variable in clause %<firstprivate%>", t
);
13131 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13132 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
13134 error_at (OMP_CLAUSE_LOCATION (c
),
13135 "%qE appears more than once in data clauses", t
);
13138 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
13140 if (ort
== C_ORT_ACC
)
13141 error ("%qD appears more than once in data clauses", t
);
13143 error ("%qD appears both in data and map clauses", t
);
13147 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
13150 case OMP_CLAUSE_LASTPRIVATE
:
13151 t
= OMP_CLAUSE_DECL (c
);
13152 need_complete
= true;
13153 need_implicitly_determined
= true;
13154 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13156 error_at (OMP_CLAUSE_LOCATION (c
),
13157 "%qE is not a variable in clause %<lastprivate%>", t
);
13160 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13161 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
13163 error_at (OMP_CLAUSE_LOCATION (c
),
13164 "%qE appears more than once in data clauses", t
);
13168 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
13171 case OMP_CLAUSE_ALIGNED
:
13172 t
= OMP_CLAUSE_DECL (c
);
13173 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13175 error_at (OMP_CLAUSE_LOCATION (c
),
13176 "%qE is not a variable in %<aligned%> clause", t
);
13179 else if (!POINTER_TYPE_P (TREE_TYPE (t
))
13180 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
13182 error_at (OMP_CLAUSE_LOCATION (c
),
13183 "%qE in %<aligned%> clause is neither a pointer nor "
13187 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
13189 error_at (OMP_CLAUSE_LOCATION (c
),
13190 "%<_Atomic%> %qD in %<aligned%> clause", t
);
13194 else if (bitmap_bit_p (&aligned_head
, DECL_UID (t
)))
13196 error_at (OMP_CLAUSE_LOCATION (c
),
13197 "%qE appears more than once in %<aligned%> clauses",
13202 bitmap_set_bit (&aligned_head
, DECL_UID (t
));
13205 case OMP_CLAUSE_DEPEND
:
13206 t
= OMP_CLAUSE_DECL (c
);
13207 if (t
== NULL_TREE
)
13209 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c
)
13210 == OMP_CLAUSE_DEPEND_SOURCE
);
13213 if (OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SINK
)
13215 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13216 for (; t
; t
= TREE_CHAIN (t
))
13218 tree decl
= TREE_VALUE (t
);
13219 if (TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
13221 tree offset
= TREE_PURPOSE (t
);
13222 bool neg
= wi::neg_p ((wide_int
) offset
);
13223 offset
= fold_unary (ABS_EXPR
, TREE_TYPE (offset
), offset
);
13224 tree t2
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
),
13225 neg
? MINUS_EXPR
: PLUS_EXPR
,
13227 t2
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
13229 fold_convert (sizetype
, t2
),
13230 fold_convert (sizetype
, decl
));
13231 if (t2
== error_mark_node
)
13236 TREE_PURPOSE (t
) = t2
;
13241 if (TREE_CODE (t
) == TREE_LIST
)
13243 if (handle_omp_array_sections (c
, ort
))
13247 if (t
== error_mark_node
)
13249 else if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13251 error_at (OMP_CLAUSE_LOCATION (c
),
13252 "%qE is not a variable in %<depend%> clause", t
);
13255 else if (!c_mark_addressable (t
))
13259 case OMP_CLAUSE_MAP
:
13260 case OMP_CLAUSE_TO
:
13261 case OMP_CLAUSE_FROM
:
13262 case OMP_CLAUSE__CACHE_
:
13263 t
= OMP_CLAUSE_DECL (c
);
13264 if (TREE_CODE (t
) == TREE_LIST
)
13266 if (handle_omp_array_sections (c
, ort
))
13270 t
= OMP_CLAUSE_DECL (c
);
13271 if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
13273 error_at (OMP_CLAUSE_LOCATION (c
),
13274 "array section does not have mappable type "
13276 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13279 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
13281 error_at (OMP_CLAUSE_LOCATION (c
),
13282 "%<_Atomic%> %qE in %qs clause", t
,
13283 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13286 while (TREE_CODE (t
) == ARRAY_REF
)
13287 t
= TREE_OPERAND (t
, 0);
13288 if (TREE_CODE (t
) == COMPONENT_REF
13289 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
13291 while (TREE_CODE (t
) == COMPONENT_REF
)
13292 t
= TREE_OPERAND (t
, 0);
13293 if (bitmap_bit_p (&map_field_head
, DECL_UID (t
)))
13295 if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
13297 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
13298 error ("%qD appears more than once in motion"
13300 else if (ort
== C_ORT_ACC
)
13301 error ("%qD appears more than once in data"
13304 error ("%qD appears more than once in map"
13310 bitmap_set_bit (&map_head
, DECL_UID (t
));
13311 bitmap_set_bit (&map_field_head
, DECL_UID (t
));
13317 if (t
== error_mark_node
)
13322 if (TREE_CODE (t
) == COMPONENT_REF
13323 && (ort
& C_ORT_OMP
)
13324 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE__CACHE_
)
13326 if (DECL_BIT_FIELD (TREE_OPERAND (t
, 1)))
13328 error_at (OMP_CLAUSE_LOCATION (c
),
13329 "bit-field %qE in %qs clause",
13330 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13333 else if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
13335 error_at (OMP_CLAUSE_LOCATION (c
),
13336 "%qE does not have a mappable type in %qs clause",
13337 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13340 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
13342 error_at (OMP_CLAUSE_LOCATION (c
),
13343 "%<_Atomic%> %qE in %qs clause", t
,
13344 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13347 while (TREE_CODE (t
) == COMPONENT_REF
)
13349 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0)))
13352 error_at (OMP_CLAUSE_LOCATION (c
),
13353 "%qE is a member of a union", t
);
13357 t
= TREE_OPERAND (t
, 0);
13361 if (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
13363 if (bitmap_bit_p (&map_field_head
, DECL_UID (t
)))
13367 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13369 error_at (OMP_CLAUSE_LOCATION (c
),
13370 "%qE is not a variable in %qs clause", t
,
13371 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13374 else if (VAR_P (t
) && DECL_THREAD_LOCAL_P (t
))
13376 error_at (OMP_CLAUSE_LOCATION (c
),
13377 "%qD is threadprivate variable in %qs clause", t
,
13378 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13381 else if ((OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
13382 || (OMP_CLAUSE_MAP_KIND (c
)
13383 != GOMP_MAP_FIRSTPRIVATE_POINTER
))
13384 && !c_mark_addressable (t
))
13386 else if (!(OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
13387 && (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_POINTER
13388 || (OMP_CLAUSE_MAP_KIND (c
)
13389 == GOMP_MAP_FIRSTPRIVATE_POINTER
)
13390 || (OMP_CLAUSE_MAP_KIND (c
)
13391 == GOMP_MAP_FORCE_DEVICEPTR
)))
13392 && t
== OMP_CLAUSE_DECL (c
)
13393 && !lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
13395 error_at (OMP_CLAUSE_LOCATION (c
),
13396 "%qD does not have a mappable type in %qs clause", t
,
13397 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13400 else if (TREE_TYPE (t
) == error_mark_node
)
13402 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t
))))
13404 error_at (OMP_CLAUSE_LOCATION (c
),
13405 "%<_Atomic%> %qE in %qs clause", t
,
13406 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13409 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
13410 && OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_FIRSTPRIVATE_POINTER
)
13412 if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13413 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
13415 error ("%qD appears more than once in data clauses", t
);
13418 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
13420 if (ort
== C_ORT_ACC
)
13421 error ("%qD appears more than once in data clauses", t
);
13423 error ("%qD appears both in data and map clauses", t
);
13427 bitmap_set_bit (&generic_head
, DECL_UID (t
));
13429 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
13431 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
13432 error ("%qD appears more than once in motion clauses", t
);
13433 else if (ort
== C_ORT_ACC
)
13434 error ("%qD appears more than once in data clauses", t
);
13436 error ("%qD appears more than once in map clauses", t
);
13439 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13440 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
13442 if (ort
== C_ORT_ACC
)
13443 error ("%qD appears more than once in data clauses", t
);
13445 error ("%qD appears both in data and map clauses", t
);
13450 bitmap_set_bit (&map_head
, DECL_UID (t
));
13451 if (t
!= OMP_CLAUSE_DECL (c
)
13452 && TREE_CODE (OMP_CLAUSE_DECL (c
)) == COMPONENT_REF
)
13453 bitmap_set_bit (&map_field_head
, DECL_UID (t
));
13457 case OMP_CLAUSE_TO_DECLARE
:
13458 case OMP_CLAUSE_LINK
:
13459 t
= OMP_CLAUSE_DECL (c
);
13460 if (TREE_CODE (t
) == FUNCTION_DECL
13461 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO_DECLARE
)
13463 else if (!VAR_P (t
))
13465 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO_DECLARE
)
13466 error_at (OMP_CLAUSE_LOCATION (c
),
13467 "%qE is neither a variable nor a function name in "
13469 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13471 error_at (OMP_CLAUSE_LOCATION (c
),
13472 "%qE is not a variable in clause %qs", t
,
13473 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13476 else if (DECL_THREAD_LOCAL_P (t
))
13478 error_at (OMP_CLAUSE_LOCATION (c
),
13479 "%qD is threadprivate variable in %qs clause", t
,
13480 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13483 else if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
13485 error_at (OMP_CLAUSE_LOCATION (c
),
13486 "%qD does not have a mappable type in %qs clause", t
,
13487 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13492 if (bitmap_bit_p (&generic_head
, DECL_UID (t
)))
13494 error_at (OMP_CLAUSE_LOCATION (c
),
13495 "%qE appears more than once on the same "
13496 "%<declare target%> directive", t
);
13500 bitmap_set_bit (&generic_head
, DECL_UID (t
));
13503 case OMP_CLAUSE_UNIFORM
:
13504 t
= OMP_CLAUSE_DECL (c
);
13505 if (TREE_CODE (t
) != PARM_DECL
)
13508 error_at (OMP_CLAUSE_LOCATION (c
),
13509 "%qD is not an argument in %<uniform%> clause", t
);
13511 error_at (OMP_CLAUSE_LOCATION (c
),
13512 "%qE is not an argument in %<uniform%> clause", t
);
13516 /* map_head bitmap is used as uniform_head if declare_simd. */
13517 bitmap_set_bit (&map_head
, DECL_UID (t
));
13518 goto check_dup_generic
;
13520 case OMP_CLAUSE_IS_DEVICE_PTR
:
13521 case OMP_CLAUSE_USE_DEVICE_PTR
:
13522 t
= OMP_CLAUSE_DECL (c
);
13523 if (TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
13524 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
13526 error_at (OMP_CLAUSE_LOCATION (c
),
13527 "%qs variable is neither a pointer nor an array",
13528 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13531 goto check_dup_generic
;
13533 case OMP_CLAUSE_NOWAIT
:
13534 if (copyprivate_seen
)
13536 error_at (OMP_CLAUSE_LOCATION (c
),
13537 "%<nowait%> clause must not be used together "
13538 "with %<copyprivate%>");
13542 nowait_clause
= pc
;
13543 pc
= &OMP_CLAUSE_CHAIN (c
);
13546 case OMP_CLAUSE_IF
:
13547 case OMP_CLAUSE_NUM_THREADS
:
13548 case OMP_CLAUSE_NUM_TEAMS
:
13549 case OMP_CLAUSE_THREAD_LIMIT
:
13550 case OMP_CLAUSE_DEFAULT
:
13551 case OMP_CLAUSE_UNTIED
:
13552 case OMP_CLAUSE_COLLAPSE
:
13553 case OMP_CLAUSE_FINAL
:
13554 case OMP_CLAUSE_MERGEABLE
:
13555 case OMP_CLAUSE_DEVICE
:
13556 case OMP_CLAUSE_DIST_SCHEDULE
:
13557 case OMP_CLAUSE_PARALLEL
:
13558 case OMP_CLAUSE_FOR
:
13559 case OMP_CLAUSE_SECTIONS
:
13560 case OMP_CLAUSE_TASKGROUP
:
13561 case OMP_CLAUSE_PROC_BIND
:
13562 case OMP_CLAUSE_PRIORITY
:
13563 case OMP_CLAUSE_GRAINSIZE
:
13564 case OMP_CLAUSE_NUM_TASKS
:
13565 case OMP_CLAUSE_NOGROUP
:
13566 case OMP_CLAUSE_THREADS
:
13567 case OMP_CLAUSE_SIMD
:
13568 case OMP_CLAUSE_HINT
:
13569 case OMP_CLAUSE_DEFAULTMAP
:
13570 case OMP_CLAUSE__CILK_FOR_COUNT_
:
13571 case OMP_CLAUSE_NUM_GANGS
:
13572 case OMP_CLAUSE_NUM_WORKERS
:
13573 case OMP_CLAUSE_VECTOR_LENGTH
:
13574 case OMP_CLAUSE_ASYNC
:
13575 case OMP_CLAUSE_WAIT
:
13576 case OMP_CLAUSE_AUTO
:
13577 case OMP_CLAUSE_INDEPENDENT
:
13578 case OMP_CLAUSE_SEQ
:
13579 case OMP_CLAUSE_GANG
:
13580 case OMP_CLAUSE_WORKER
:
13581 case OMP_CLAUSE_VECTOR
:
13582 case OMP_CLAUSE_TILE
:
13583 pc
= &OMP_CLAUSE_CHAIN (c
);
13586 case OMP_CLAUSE_SCHEDULE
:
13587 if (OMP_CLAUSE_SCHEDULE_KIND (c
) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC
)
13589 const char *p
= NULL
;
13590 switch (OMP_CLAUSE_SCHEDULE_KIND (c
) & OMP_CLAUSE_SCHEDULE_MASK
)
13592 case OMP_CLAUSE_SCHEDULE_STATIC
: p
= "static"; break;
13593 case OMP_CLAUSE_SCHEDULE_DYNAMIC
: break;
13594 case OMP_CLAUSE_SCHEDULE_GUIDED
: break;
13595 case OMP_CLAUSE_SCHEDULE_AUTO
: p
= "auto"; break;
13596 case OMP_CLAUSE_SCHEDULE_RUNTIME
: p
= "runtime"; break;
13597 default: gcc_unreachable ();
13601 error_at (OMP_CLAUSE_LOCATION (c
),
13602 "%<nonmonotonic%> modifier specified for %qs "
13603 "schedule kind", p
);
13604 OMP_CLAUSE_SCHEDULE_KIND (c
)
13605 = (enum omp_clause_schedule_kind
)
13606 (OMP_CLAUSE_SCHEDULE_KIND (c
)
13607 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC
);
13610 schedule_clause
= c
;
13611 pc
= &OMP_CLAUSE_CHAIN (c
);
13614 case OMP_CLAUSE_ORDERED
:
13615 ordered_seen
= true;
13616 pc
= &OMP_CLAUSE_CHAIN (c
);
13619 case OMP_CLAUSE_SAFELEN
:
13621 pc
= &OMP_CLAUSE_CHAIN (c
);
13623 case OMP_CLAUSE_SIMDLEN
:
13625 pc
= &OMP_CLAUSE_CHAIN (c
);
13628 case OMP_CLAUSE_INBRANCH
:
13629 case OMP_CLAUSE_NOTINBRANCH
:
13632 error_at (OMP_CLAUSE_LOCATION (c
),
13633 "%<inbranch%> clause is incompatible with "
13634 "%<notinbranch%>");
13638 branch_seen
= true;
13639 pc
= &OMP_CLAUSE_CHAIN (c
);
13643 gcc_unreachable ();
13648 t
= OMP_CLAUSE_DECL (c
);
13652 t
= require_complete_type (OMP_CLAUSE_LOCATION (c
), t
);
13653 if (t
== error_mark_node
)
13657 if (need_implicitly_determined
)
13659 const char *share_name
= NULL
;
13661 if (VAR_P (t
) && DECL_THREAD_LOCAL_P (t
))
13662 share_name
= "threadprivate";
13663 else switch (c_omp_predetermined_sharing (t
))
13665 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
13667 case OMP_CLAUSE_DEFAULT_SHARED
:
13668 /* const vars may be specified in firstprivate clause. */
13669 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
13670 && TREE_READONLY (t
))
13672 share_name
= "shared";
13674 case OMP_CLAUSE_DEFAULT_PRIVATE
:
13675 share_name
= "private";
13678 gcc_unreachable ();
13682 error_at (OMP_CLAUSE_LOCATION (c
),
13683 "%qE is predetermined %qs for %qs",
13685 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13692 *pc
= OMP_CLAUSE_CHAIN (c
);
13694 pc
= &OMP_CLAUSE_CHAIN (c
);
13699 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen
),
13700 OMP_CLAUSE_SIMDLEN_EXPR (simdlen
)))
13702 error_at (OMP_CLAUSE_LOCATION (simdlen
),
13703 "%<simdlen%> clause value is bigger than "
13704 "%<safelen%> clause value");
13705 OMP_CLAUSE_SIMDLEN_EXPR (simdlen
)
13706 = OMP_CLAUSE_SAFELEN_EXPR (safelen
);
13711 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
13712 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
13714 error_at (OMP_CLAUSE_LOCATION (schedule_clause
),
13715 "%<nonmonotonic%> schedule modifier specified together "
13716 "with %<ordered%> clause");
13717 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
13718 = (enum omp_clause_schedule_kind
)
13719 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
13720 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC
);
13723 if (linear_variable_step_check
)
13724 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
13726 bool remove
= false;
13727 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
13728 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c
)
13729 && !bitmap_bit_p (&map_head
,
13730 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c
))))
13732 error_at (OMP_CLAUSE_LOCATION (c
),
13733 "%<linear%> clause step is a parameter %qD not "
13734 "specified in %<uniform%> clause",
13735 OMP_CLAUSE_LINEAR_STEP (c
));
13740 *pc
= OMP_CLAUSE_CHAIN (c
);
13742 pc
= &OMP_CLAUSE_CHAIN (c
);
13745 bitmap_obstack_release (NULL
);
13749 /* Return code to initialize DST with a copy constructor from SRC.
13750 C doesn't have copy constructors nor assignment operators, only for
13751 _Atomic vars we need to perform __atomic_load from src into a temporary
13752 followed by __atomic_store of the temporary to dst. */
13755 c_omp_clause_copy_ctor (tree clause
, tree dst
, tree src
)
13757 if (!really_atomic_lvalue (dst
) && !really_atomic_lvalue (src
))
13758 return build2 (MODIFY_EXPR
, TREE_TYPE (dst
), dst
, src
);
13760 location_t loc
= OMP_CLAUSE_LOCATION (clause
);
13761 tree type
= TREE_TYPE (dst
);
13762 tree nonatomic_type
= build_qualified_type (type
, TYPE_UNQUALIFIED
);
13763 tree tmp
= create_tmp_var (nonatomic_type
);
13764 tree tmp_addr
= build_fold_addr_expr (tmp
);
13765 TREE_ADDRESSABLE (tmp
) = 1;
13766 TREE_NO_WARNING (tmp
) = 1;
13767 tree src_addr
= build_fold_addr_expr (src
);
13768 tree dst_addr
= build_fold_addr_expr (dst
);
13769 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
13770 vec
<tree
, va_gc
> *params
;
13771 /* Expansion of a generic atomic load may require an addition
13772 element, so allocate enough to prevent a resize. */
13773 vec_alloc (params
, 4);
13775 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13776 tree fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
13777 params
->quick_push (src_addr
);
13778 params
->quick_push (tmp_addr
);
13779 params
->quick_push (seq_cst
);
13780 tree load
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
13782 vec_alloc (params
, 4);
13784 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
13785 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
13786 params
->quick_push (dst_addr
);
13787 params
->quick_push (tmp_addr
);
13788 params
->quick_push (seq_cst
);
13789 tree store
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
13790 return build2 (COMPOUND_EXPR
, void_type_node
, load
, store
);
13793 /* Create a transaction node. */
13796 c_finish_transaction (location_t loc
, tree block
, int flags
)
13798 tree stmt
= build_stmt (loc
, TRANSACTION_EXPR
, block
);
13799 if (flags
& TM_STMT_ATTR_OUTER
)
13800 TRANSACTION_EXPR_OUTER (stmt
) = 1;
13801 if (flags
& TM_STMT_ATTR_RELAXED
)
13802 TRANSACTION_EXPR_RELAXED (stmt
) = 1;
13803 return add_stmt (stmt
);
13806 /* Make a variant type in the proper way for C/C++, propagating qualifiers
13807 down to the element type of an array. If ORIG_QUAL_TYPE is not
13808 NULL, then it should be used as the qualified type
13809 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13810 preserve information about the typedef name from which an array
13811 type was derived). */
13814 c_build_qualified_type (tree type
, int type_quals
, tree orig_qual_type
,
13815 size_t orig_qual_indirect
)
13817 if (type
== error_mark_node
)
13820 if (TREE_CODE (type
) == ARRAY_TYPE
)
13823 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
13824 type_quals
, orig_qual_type
,
13825 orig_qual_indirect
- 1);
13827 /* See if we already have an identically qualified type. */
13828 if (orig_qual_type
&& orig_qual_indirect
== 0)
13829 t
= orig_qual_type
;
13831 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
13833 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
13834 && TYPE_NAME (t
) == TYPE_NAME (type
)
13835 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
13836 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
13837 TYPE_ATTRIBUTES (type
)))
13842 tree domain
= TYPE_DOMAIN (type
);
13844 t
= build_variant_type_copy (type
);
13845 TREE_TYPE (t
) = element_type
;
13847 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
13848 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
13849 SET_TYPE_STRUCTURAL_EQUALITY (t
);
13850 else if (TYPE_CANONICAL (element_type
) != element_type
13851 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
13853 tree unqualified_canon
13854 = build_array_type (TYPE_CANONICAL (element_type
),
13855 domain
? TYPE_CANONICAL (domain
)
13857 if (TYPE_REVERSE_STORAGE_ORDER (type
))
13860 = build_distinct_type_copy (unqualified_canon
);
13861 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon
) = 1;
13864 = c_build_qualified_type (unqualified_canon
, type_quals
);
13867 TYPE_CANONICAL (t
) = t
;
13872 /* A restrict-qualified pointer type must be a pointer to object or
13873 incomplete type. Note that the use of POINTER_TYPE_P also allows
13874 REFERENCE_TYPEs, which is appropriate for C++. */
13875 if ((type_quals
& TYPE_QUAL_RESTRICT
)
13876 && (!POINTER_TYPE_P (type
)
13877 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
13879 error ("invalid use of %<restrict%>");
13880 type_quals
&= ~TYPE_QUAL_RESTRICT
;
13883 tree var_type
= (orig_qual_type
&& orig_qual_indirect
== 0
13885 : build_qualified_type (type
, type_quals
));
13886 /* A variant type does not inherit the list of incomplete vars from the
13887 type main variant. */
13888 if (RECORD_OR_UNION_TYPE_P (var_type
)
13889 && TYPE_MAIN_VARIANT (var_type
) != var_type
)
13890 C_TYPE_INCOMPLETE_VARS (var_type
) = 0;
13894 /* Build a VA_ARG_EXPR for the C parser. */
13897 c_build_va_arg (location_t loc1
, tree expr
, location_t loc2
, tree type
)
13899 if (error_operand_p (type
))
13900 return error_mark_node
;
13901 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13902 order because it takes the address of the expression. */
13903 else if (handled_component_p (expr
)
13904 && reverse_storage_order_for_component_p (expr
))
13906 error_at (loc1
, "cannot use %<va_arg%> with reverse storage order");
13907 return error_mark_node
;
13909 else if (!COMPLETE_TYPE_P (type
))
13911 error_at (loc2
, "second argument to %<va_arg%> is of incomplete "
13913 return error_mark_node
;
13915 else if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
13916 warning_at (loc2
, OPT_Wc___compat
,
13917 "C++ requires promoted type, not enum type, in %<va_arg%>");
13918 return build_va_arg (loc2
, expr
, type
);
13921 /* Return truthvalue of whether T1 is the same tree structure as T2.
13922 Return 1 if they are the same. Return 0 if they are different. */
13925 c_tree_equal (tree t1
, tree t2
)
13927 enum tree_code code1
, code2
;
13934 for (code1
= TREE_CODE (t1
);
13935 CONVERT_EXPR_CODE_P (code1
)
13936 || code1
== NON_LVALUE_EXPR
;
13937 code1
= TREE_CODE (t1
))
13938 t1
= TREE_OPERAND (t1
, 0);
13939 for (code2
= TREE_CODE (t2
);
13940 CONVERT_EXPR_CODE_P (code2
)
13941 || code2
== NON_LVALUE_EXPR
;
13942 code2
= TREE_CODE (t2
))
13943 t2
= TREE_OPERAND (t2
, 0);
13945 /* They might have become equal now. */
13949 if (code1
!= code2
)
13955 return wi::eq_p (t1
, t2
);
13958 return real_equal (&TREE_REAL_CST (t1
), &TREE_REAL_CST (t2
));
13961 return TREE_STRING_LENGTH (t1
) == TREE_STRING_LENGTH (t2
)
13962 && !memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
13963 TREE_STRING_LENGTH (t1
));
13966 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1
),
13967 TREE_FIXED_CST (t2
));
13970 return c_tree_equal (TREE_REALPART (t1
), TREE_REALPART (t2
))
13971 && c_tree_equal (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
13974 return operand_equal_p (t1
, t2
, OEP_ONLY_CONST
);
13977 /* We need to do this when determining whether or not two
13978 non-type pointer to member function template arguments
13980 if (!comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))
13981 || CONSTRUCTOR_NELTS (t1
) != CONSTRUCTOR_NELTS (t2
))
13986 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, field
, value
)
13988 constructor_elt
*elt2
= CONSTRUCTOR_ELT (t2
, i
);
13989 if (!c_tree_equal (field
, elt2
->index
)
13990 || !c_tree_equal (value
, elt2
->value
))
13997 if (!c_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)))
13999 if (!c_tree_equal (TREE_VALUE (t1
), TREE_VALUE (t2
)))
14001 return c_tree_equal (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
14004 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
14009 call_expr_arg_iterator iter1
, iter2
;
14010 if (!c_tree_equal (CALL_EXPR_FN (t1
), CALL_EXPR_FN (t2
)))
14012 for (arg1
= first_call_expr_arg (t1
, &iter1
),
14013 arg2
= first_call_expr_arg (t2
, &iter2
);
14015 arg1
= next_call_expr_arg (&iter1
),
14016 arg2
= next_call_expr_arg (&iter2
))
14017 if (!c_tree_equal (arg1
, arg2
))
14026 tree o1
= TREE_OPERAND (t1
, 0);
14027 tree o2
= TREE_OPERAND (t2
, 0);
14029 /* Special case: if either target is an unallocated VAR_DECL,
14030 it means that it's going to be unified with whatever the
14031 TARGET_EXPR is really supposed to initialize, so treat it
14032 as being equivalent to anything. */
14033 if (VAR_P (o1
) && DECL_NAME (o1
) == NULL_TREE
14034 && !DECL_RTL_SET_P (o1
))
14036 else if (VAR_P (o2
) && DECL_NAME (o2
) == NULL_TREE
14037 && !DECL_RTL_SET_P (o2
))
14039 else if (!c_tree_equal (o1
, o2
))
14042 return c_tree_equal (TREE_OPERAND (t1
, 1), TREE_OPERAND (t2
, 1));
14045 case COMPONENT_REF
:
14046 if (TREE_OPERAND (t1
, 1) != TREE_OPERAND (t2
, 1))
14048 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
14054 case FUNCTION_DECL
:
14055 case IDENTIFIER_NODE
:
14062 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
14064 for (ix
= TREE_VEC_LENGTH (t1
); ix
--;)
14065 if (!c_tree_equal (TREE_VEC_ELT (t1
, ix
),
14066 TREE_VEC_ELT (t2
, ix
)))
14075 switch (TREE_CODE_CLASS (code1
))
14079 case tcc_comparison
:
14080 case tcc_expression
:
14082 case tcc_reference
:
14083 case tcc_statement
:
14085 int i
, n
= TREE_OPERAND_LENGTH (t1
);
14089 case PREINCREMENT_EXPR
:
14090 case PREDECREMENT_EXPR
:
14091 case POSTINCREMENT_EXPR
:
14092 case POSTDECREMENT_EXPR
:
14102 if (TREE_CODE_CLASS (code1
) == tcc_vl_exp
14103 && n
!= TREE_OPERAND_LENGTH (t2
))
14106 for (i
= 0; i
< n
; ++i
)
14107 if (!c_tree_equal (TREE_OPERAND (t1
, i
), TREE_OPERAND (t2
, i
)))
14114 return comptypes (t1
, t2
);
14116 gcc_unreachable ();
14118 /* We can get here with --disable-checking. */
14122 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14123 spawn-helper and BODY is the newly created body for FNDECL. */
14126 cilk_install_body_with_frame_cleanup (tree fndecl
, tree body
, void *w
)
14128 tree list
= alloc_stmt_list ();
14129 tree frame
= make_cilk_frame (fndecl
);
14130 tree dtor
= create_cilk_function_exit (frame
, false, true);
14131 add_local_decl (cfun
, frame
);
14133 DECL_SAVED_TREE (fndecl
) = list
;
14134 tree frame_ptr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (frame
)),
14136 tree body_list
= cilk_install_body_pedigree_operations (frame_ptr
);
14137 gcc_assert (TREE_CODE (body_list
) == STATEMENT_LIST
);
14139 tree detach_expr
= build_call_expr (cilk_detach_fndecl
, 1, frame_ptr
);
14140 append_to_statement_list (detach_expr
, &body_list
);
14142 cilk_outline (fndecl
, &body
, (struct wrapper_data
*) w
);
14143 body
= fold_build_cleanup_point_expr (void_type_node
, body
);
14145 append_to_statement_list (body
, &body_list
);
14146 append_to_statement_list (build_stmt (EXPR_LOCATION (body
), TRY_FINALLY_EXPR
,
14147 body_list
, dtor
), &list
);
14150 /* Returns true when the function declaration FNDECL is implicit,
14151 introduced as a result of a call to an otherwise undeclared
14152 function, and false otherwise. */
14155 c_decl_implicit (const_tree fndecl
)
14157 return C_DECL_IMPLICIT (fndecl
);