1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2017 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"
34 #include "gimple-expr.h"
36 #include "stor-layout.h"
37 #include "trans-mem.h"
40 #include "langhooks.h"
43 #include "tree-iterator.h"
45 #include "tree-inline.h"
46 #include "omp-general.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
50 #include "gomp-constants.h"
51 #include "spellcheck-tree.h"
52 #include "gcc-rich-location.h"
55 /* Possible cases of implicit bad conversions. Used to select
56 diagnostic messages in convert_for_assignment. */
64 /* The level of nesting inside "__alignof__". */
67 /* The level of nesting inside "sizeof". */
70 /* The level of nesting inside "typeof". */
73 /* The argument of last parsed sizeof expression, only to be tested
74 if expr.original_code == SIZEOF_EXPR. */
75 tree c_last_sizeof_arg
;
76 location_t c_last_sizeof_loc
;
78 /* Nonzero if we might need to print a "missing braces around
79 initializer" message within this initializer. */
80 static int found_missing_braces
;
82 static int require_constant_value
;
83 static int require_constant_elements
;
85 static bool null_pointer_constant_p (const_tree
);
86 static tree
qualify_type (tree
, tree
);
87 static int tagged_types_tu_compatible_p (const_tree
, const_tree
, bool *,
89 static int comp_target_types (location_t
, tree
, tree
);
90 static int function_types_compatible_p (const_tree
, const_tree
, bool *,
92 static int type_lists_compatible_p (const_tree
, const_tree
, bool *, bool *);
93 static tree
lookup_field (tree
, tree
);
94 static int convert_arguments (location_t
, vec
<location_t
>, tree
,
95 vec
<tree
, va_gc
> *, vec
<tree
, va_gc
> *, tree
,
97 static tree
pointer_diff (location_t
, tree
, tree
);
98 static tree
convert_for_assignment (location_t
, location_t
, tree
, tree
, tree
,
99 enum impl_conv
, bool, tree
, tree
, int);
100 static tree
valid_compound_expr_initializer (tree
, tree
);
101 static void push_string (const char *);
102 static void push_member_name (tree
);
103 static int spelling_length (void);
104 static char *print_spelling (char *);
105 static void warning_init (location_t
, int, const char *);
106 static tree
digest_init (location_t
, tree
, tree
, tree
, bool, bool, int);
107 static void output_init_element (location_t
, tree
, tree
, bool, tree
, tree
, int,
108 bool, struct obstack
*);
109 static void output_pending_init_elements (int, struct obstack
*);
110 static bool set_designator (location_t
, int, struct obstack
*);
111 static void push_range_stack (tree
, struct obstack
*);
112 static void add_pending_init (location_t
, tree
, tree
, tree
, bool,
114 static void set_nonincremental_init (struct obstack
*);
115 static void set_nonincremental_init_from_string (tree
, struct obstack
*);
116 static tree
find_init_member (tree
, struct obstack
*);
117 static void readonly_warning (tree
, enum lvalue_use
);
118 static int lvalue_or_else (location_t
, const_tree
, enum lvalue_use
);
119 static void record_maybe_used_decl (tree
);
120 static int comptypes_internal (const_tree
, const_tree
, bool *, bool *);
122 /* Return true if EXP is a null pointer constant, false otherwise. */
125 null_pointer_constant_p (const_tree expr
)
127 /* This should really operate on c_expr structures, but they aren't
128 yet available everywhere required. */
129 tree type
= TREE_TYPE (expr
);
130 return (TREE_CODE (expr
) == INTEGER_CST
131 && !TREE_OVERFLOW (expr
)
132 && integer_zerop (expr
)
133 && (INTEGRAL_TYPE_P (type
)
134 || (TREE_CODE (type
) == POINTER_TYPE
135 && VOID_TYPE_P (TREE_TYPE (type
))
136 && TYPE_QUALS (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
)));
139 /* EXPR may appear in an unevaluated part of an integer constant
140 expression, but not in an evaluated part. Wrap it in a
141 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
142 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
145 note_integer_operands (tree expr
)
148 if (TREE_CODE (expr
) == INTEGER_CST
&& in_late_binary_op
)
150 ret
= copy_node (expr
);
151 TREE_OVERFLOW (ret
) = 1;
155 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL_TREE
, expr
);
156 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret
) = 1;
161 /* Having checked whether EXPR may appear in an unevaluated part of an
162 integer constant expression and found that it may, remove any
163 C_MAYBE_CONST_EXPR noting this fact and return the resulting
167 remove_c_maybe_const_expr (tree expr
)
169 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
170 return C_MAYBE_CONST_EXPR_EXPR (expr
);
175 \f/* This is a cache to hold if two types are compatible or not. */
177 struct tagged_tu_seen_cache
{
178 const struct tagged_tu_seen_cache
* next
;
181 /* The return value of tagged_types_tu_compatible_p if we had seen
182 these two types already. */
186 static const struct tagged_tu_seen_cache
* tagged_tu_seen_base
;
187 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*);
189 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
190 does not have an incomplete type. (That includes void types.)
191 LOC is the location of the use. */
194 require_complete_type (location_t loc
, tree value
)
196 tree type
= TREE_TYPE (value
);
198 if (error_operand_p (value
))
199 return error_mark_node
;
201 /* First, detect a valid value with a complete type. */
202 if (COMPLETE_TYPE_P (type
))
205 c_incomplete_type_error (loc
, value
, type
);
206 return error_mark_node
;
209 /* Print an error message for invalid use of an incomplete type.
210 VALUE is the expression that was used (or 0 if that isn't known)
211 and TYPE is the type that was invalid. LOC is the location for
215 c_incomplete_type_error (location_t loc
, const_tree value
, const_tree type
)
217 /* Avoid duplicate error message. */
218 if (TREE_CODE (type
) == ERROR_MARK
)
221 if (value
!= NULL_TREE
&& (VAR_P (value
) || TREE_CODE (value
) == PARM_DECL
))
222 error_at (loc
, "%qD has an incomplete type %qT", value
, type
);
226 /* We must print an error message. Be clever about what it says. */
228 switch (TREE_CODE (type
))
236 error_at (loc
, "invalid use of void expression");
240 if (TYPE_DOMAIN (type
))
242 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
244 error_at (loc
, "invalid use of flexible array member");
247 type
= TREE_TYPE (type
);
250 error_at (loc
, "invalid use of array with unspecified bounds");
257 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
258 error_at (loc
, "invalid use of undefined type %qT", type
);
260 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
261 error_at (loc
, "invalid use of incomplete typedef %qT", type
);
265 /* Given a type, apply default promotions wrt unnamed function
266 arguments and return the new type. */
269 c_type_promotes_to (tree type
)
271 tree ret
= NULL_TREE
;
273 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
274 ret
= double_type_node
;
275 else if (c_promoting_integer_type_p (type
))
277 /* Preserve unsignedness if not really getting any wider. */
278 if (TYPE_UNSIGNED (type
)
279 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
280 ret
= unsigned_type_node
;
282 ret
= integer_type_node
;
285 if (ret
!= NULL_TREE
)
286 return (TYPE_ATOMIC (type
)
287 ? c_build_qualified_type (ret
, TYPE_QUAL_ATOMIC
)
293 /* Return true if between two named address spaces, whether there is a superset
294 named address space that encompasses both address spaces. If there is a
295 superset, return which address space is the superset. */
298 addr_space_superset (addr_space_t as1
, addr_space_t as2
, addr_space_t
*common
)
305 else if (targetm
.addr_space
.subset_p (as1
, as2
))
310 else if (targetm
.addr_space
.subset_p (as2
, as1
))
319 /* Return a variant of TYPE which has all the type qualifiers of LIKE
320 as well as those of TYPE. */
323 qualify_type (tree type
, tree like
)
325 addr_space_t as_type
= TYPE_ADDR_SPACE (type
);
326 addr_space_t as_like
= TYPE_ADDR_SPACE (like
);
327 addr_space_t as_common
;
329 /* If the two named address spaces are different, determine the common
330 superset address space. If there isn't one, raise an error. */
331 if (!addr_space_superset (as_type
, as_like
, &as_common
))
334 error ("%qT and %qT are in disjoint named address spaces",
338 return c_build_qualified_type (type
,
339 TYPE_QUALS_NO_ADDR_SPACE (type
)
340 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like
)
341 | ENCODE_QUAL_ADDR_SPACE (as_common
));
344 /* Return true iff the given tree T is a variable length array. */
347 c_vla_type_p (const_tree t
)
349 if (TREE_CODE (t
) == ARRAY_TYPE
350 && C_TYPE_VARIABLE_SIZE (t
))
355 /* Return the composite type of two compatible types.
357 We assume that comptypes has already been done and returned
358 nonzero; if that isn't so, this may crash. In particular, we
359 assume that qualifiers match. */
362 composite_type (tree t1
, tree t2
)
364 enum tree_code code1
;
365 enum tree_code code2
;
368 /* Save time if the two types are the same. */
370 if (t1
== t2
) return t1
;
372 /* If one type is nonsense, use the other. */
373 if (t1
== error_mark_node
)
375 if (t2
== error_mark_node
)
378 code1
= TREE_CODE (t1
);
379 code2
= TREE_CODE (t2
);
381 /* Merge the attributes. */
382 attributes
= targetm
.merge_type_attributes (t1
, t2
);
384 /* If one is an enumerated type and the other is the compatible
385 integer type, the composite type might be either of the two
386 (DR#013 question 3). For consistency, use the enumerated type as
387 the composite type. */
389 if (code1
== ENUMERAL_TYPE
&& code2
== INTEGER_TYPE
)
391 if (code2
== ENUMERAL_TYPE
&& code1
== INTEGER_TYPE
)
394 gcc_assert (code1
== code2
);
399 /* For two pointers, do this recursively on the target type. */
401 tree pointed_to_1
= TREE_TYPE (t1
);
402 tree pointed_to_2
= TREE_TYPE (t2
);
403 tree target
= composite_type (pointed_to_1
, pointed_to_2
);
404 t1
= build_pointer_type_for_mode (target
, TYPE_MODE (t1
), false);
405 t1
= build_type_attribute_variant (t1
, attributes
);
406 return qualify_type (t1
, t2
);
411 tree elt
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
414 tree d1
= TYPE_DOMAIN (t1
);
415 tree d2
= TYPE_DOMAIN (t2
);
416 bool d1_variable
, d2_variable
;
417 bool d1_zero
, d2_zero
;
418 bool t1_complete
, t2_complete
;
420 /* We should not have any type quals on arrays at all. */
421 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1
)
422 && !TYPE_QUALS_NO_ADDR_SPACE (t2
));
424 t1_complete
= COMPLETE_TYPE_P (t1
);
425 t2_complete
= COMPLETE_TYPE_P (t2
);
427 d1_zero
= d1
== NULL_TREE
|| !TYPE_MAX_VALUE (d1
);
428 d2_zero
= d2
== NULL_TREE
|| !TYPE_MAX_VALUE (d2
);
430 d1_variable
= (!d1_zero
431 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
432 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
433 d2_variable
= (!d2_zero
434 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
435 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
436 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
437 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
439 /* Save space: see if the result is identical to one of the args. */
440 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
)
441 && (d2_variable
|| d2_zero
|| !d1_variable
))
442 return build_type_attribute_variant (t1
, attributes
);
443 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
)
444 && (d1_variable
|| d1_zero
|| !d2_variable
))
445 return build_type_attribute_variant (t2
, attributes
);
447 if (elt
== TREE_TYPE (t1
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
448 return build_type_attribute_variant (t1
, attributes
);
449 if (elt
== TREE_TYPE (t2
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
450 return build_type_attribute_variant (t2
, attributes
);
452 /* Merge the element types, and have a size if either arg has
453 one. We may have qualifiers on the element types. To set
454 up TYPE_MAIN_VARIANT correctly, we need to form the
455 composite of the unqualified types and add the qualifiers
457 quals
= TYPE_QUALS (strip_array_types (elt
));
458 unqual_elt
= c_build_qualified_type (elt
, TYPE_UNQUALIFIED
);
459 t1
= build_array_type (unqual_elt
,
460 TYPE_DOMAIN ((TYPE_DOMAIN (t1
)
466 /* Ensure a composite type involving a zero-length array type
467 is a zero-length type not an incomplete type. */
468 if (d1_zero
&& d2_zero
469 && (t1_complete
|| t2_complete
)
470 && !COMPLETE_TYPE_P (t1
))
472 TYPE_SIZE (t1
) = bitsize_zero_node
;
473 TYPE_SIZE_UNIT (t1
) = size_zero_node
;
475 t1
= c_build_qualified_type (t1
, quals
);
476 return build_type_attribute_variant (t1
, attributes
);
482 if (attributes
!= NULL
)
484 /* Try harder not to create a new aggregate type. */
485 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
487 if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
490 return build_type_attribute_variant (t1
, attributes
);
493 /* Function types: prefer the one that specified arg types.
494 If both do, merge the arg types. Also merge the return types. */
496 tree valtype
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
497 tree p1
= TYPE_ARG_TYPES (t1
);
498 tree p2
= TYPE_ARG_TYPES (t2
);
503 /* Save space: see if the result is identical to one of the args. */
504 if (valtype
== TREE_TYPE (t1
) && !TYPE_ARG_TYPES (t2
))
505 return build_type_attribute_variant (t1
, attributes
);
506 if (valtype
== TREE_TYPE (t2
) && !TYPE_ARG_TYPES (t1
))
507 return build_type_attribute_variant (t2
, attributes
);
509 /* Simple way if one arg fails to specify argument types. */
510 if (TYPE_ARG_TYPES (t1
) == NULL_TREE
)
512 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
513 t1
= build_type_attribute_variant (t1
, attributes
);
514 return qualify_type (t1
, t2
);
516 if (TYPE_ARG_TYPES (t2
) == NULL_TREE
)
518 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
519 t1
= build_type_attribute_variant (t1
, attributes
);
520 return qualify_type (t1
, t2
);
523 /* If both args specify argument types, we must merge the two
524 lists, argument by argument. */
526 for (len
= 0, newargs
= p1
;
527 newargs
&& newargs
!= void_list_node
;
528 len
++, newargs
= TREE_CHAIN (newargs
))
531 for (i
= 0; i
< len
; i
++)
532 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
536 for (; p1
&& p1
!= void_list_node
;
537 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
539 /* A null type means arg type is not specified.
540 Take whatever the other function type has. */
541 if (TREE_VALUE (p1
) == NULL_TREE
)
543 TREE_VALUE (n
) = TREE_VALUE (p2
);
546 if (TREE_VALUE (p2
) == NULL_TREE
)
548 TREE_VALUE (n
) = TREE_VALUE (p1
);
552 /* Given wait (union {union wait *u; int *i} *)
553 and wait (union wait *),
554 prefer union wait * as type of parm. */
555 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
556 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
559 tree mv2
= TREE_VALUE (p2
);
560 if (mv2
&& mv2
!= error_mark_node
561 && TREE_CODE (mv2
) != ARRAY_TYPE
)
562 mv2
= TYPE_MAIN_VARIANT (mv2
);
563 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
564 memb
; memb
= DECL_CHAIN (memb
))
566 tree mv3
= TREE_TYPE (memb
);
567 if (mv3
&& mv3
!= error_mark_node
568 && TREE_CODE (mv3
) != ARRAY_TYPE
)
569 mv3
= TYPE_MAIN_VARIANT (mv3
);
570 if (comptypes (mv3
, mv2
))
572 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
574 pedwarn (input_location
, OPT_Wpedantic
,
575 "function types not truly compatible in ISO C");
580 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
581 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
584 tree mv1
= TREE_VALUE (p1
);
585 if (mv1
&& mv1
!= error_mark_node
586 && TREE_CODE (mv1
) != ARRAY_TYPE
)
587 mv1
= TYPE_MAIN_VARIANT (mv1
);
588 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
589 memb
; memb
= DECL_CHAIN (memb
))
591 tree mv3
= TREE_TYPE (memb
);
592 if (mv3
&& mv3
!= error_mark_node
593 && TREE_CODE (mv3
) != ARRAY_TYPE
)
594 mv3
= TYPE_MAIN_VARIANT (mv3
);
595 if (comptypes (mv3
, mv1
))
597 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
599 pedwarn (input_location
, OPT_Wpedantic
,
600 "function types not truly compatible in ISO C");
605 TREE_VALUE (n
) = composite_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
609 t1
= build_function_type (valtype
, newargs
);
610 t1
= qualify_type (t1
, t2
);
615 return build_type_attribute_variant (t1
, attributes
);
620 /* Return the type of a conditional expression between pointers to
621 possibly differently qualified versions of compatible types.
623 We assume that comp_target_types has already been done and returned
624 nonzero; if that isn't so, this may crash. */
627 common_pointer_type (tree t1
, tree t2
)
630 tree pointed_to_1
, mv1
;
631 tree pointed_to_2
, mv2
;
633 unsigned target_quals
;
634 addr_space_t as1
, as2
, as_common
;
637 /* Save time if the two types are the same. */
639 if (t1
== t2
) return t1
;
641 /* If one type is nonsense, use the other. */
642 if (t1
== error_mark_node
)
644 if (t2
== error_mark_node
)
647 gcc_assert (TREE_CODE (t1
) == POINTER_TYPE
648 && TREE_CODE (t2
) == POINTER_TYPE
);
650 /* Merge the attributes. */
651 attributes
= targetm
.merge_type_attributes (t1
, t2
);
653 /* Find the composite type of the target types, and combine the
654 qualifiers of the two types' targets. Do not lose qualifiers on
655 array element types by taking the TYPE_MAIN_VARIANT. */
656 mv1
= pointed_to_1
= TREE_TYPE (t1
);
657 mv2
= pointed_to_2
= TREE_TYPE (t2
);
658 if (TREE_CODE (mv1
) != ARRAY_TYPE
)
659 mv1
= TYPE_MAIN_VARIANT (pointed_to_1
);
660 if (TREE_CODE (mv2
) != ARRAY_TYPE
)
661 mv2
= TYPE_MAIN_VARIANT (pointed_to_2
);
662 target
= composite_type (mv1
, mv2
);
664 /* Strip array types to get correct qualifier for pointers to arrays */
665 quals1
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1
));
666 quals2
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2
));
668 /* For function types do not merge const qualifiers, but drop them
669 if used inconsistently. The middle-end uses these to mark const
670 and noreturn functions. */
671 if (TREE_CODE (pointed_to_1
) == FUNCTION_TYPE
)
672 target_quals
= (quals1
& quals2
);
674 target_quals
= (quals1
| quals2
);
676 /* If the two named address spaces are different, determine the common
677 superset address space. This is guaranteed to exist due to the
678 assumption that comp_target_type returned non-zero. */
679 as1
= TYPE_ADDR_SPACE (pointed_to_1
);
680 as2
= TYPE_ADDR_SPACE (pointed_to_2
);
681 if (!addr_space_superset (as1
, as2
, &as_common
))
684 target_quals
|= ENCODE_QUAL_ADDR_SPACE (as_common
);
686 t1
= build_pointer_type (c_build_qualified_type (target
, target_quals
));
687 return build_type_attribute_variant (t1
, attributes
);
690 /* Return the common type for two arithmetic types under the usual
691 arithmetic conversions. The default conversions have already been
692 applied, and enumerated types converted to their compatible integer
693 types. The resulting type is unqualified and has no attributes.
695 This is the type for the result of most arithmetic operations
696 if the operands have the given two types. */
699 c_common_type (tree t1
, tree t2
)
701 enum tree_code code1
;
702 enum tree_code code2
;
704 /* If one type is nonsense, use the other. */
705 if (t1
== error_mark_node
)
707 if (t2
== error_mark_node
)
710 if (TYPE_QUALS (t1
) != TYPE_UNQUALIFIED
)
711 t1
= TYPE_MAIN_VARIANT (t1
);
713 if (TYPE_QUALS (t2
) != TYPE_UNQUALIFIED
)
714 t2
= TYPE_MAIN_VARIANT (t2
);
716 if (TYPE_ATTRIBUTES (t1
) != NULL_TREE
)
717 t1
= build_type_attribute_variant (t1
, NULL_TREE
);
719 if (TYPE_ATTRIBUTES (t2
) != NULL_TREE
)
720 t2
= build_type_attribute_variant (t2
, NULL_TREE
);
722 /* Save time if the two types are the same. */
724 if (t1
== t2
) return t1
;
726 code1
= TREE_CODE (t1
);
727 code2
= TREE_CODE (t2
);
729 gcc_assert (code1
== VECTOR_TYPE
|| code1
== COMPLEX_TYPE
730 || code1
== FIXED_POINT_TYPE
|| code1
== REAL_TYPE
731 || code1
== INTEGER_TYPE
);
732 gcc_assert (code2
== VECTOR_TYPE
|| code2
== COMPLEX_TYPE
733 || code2
== FIXED_POINT_TYPE
|| code2
== REAL_TYPE
734 || code2
== INTEGER_TYPE
);
736 /* When one operand is a decimal float type, the other operand cannot be
737 a generic float type or a complex type. We also disallow vector types
739 if ((DECIMAL_FLOAT_TYPE_P (t1
) || DECIMAL_FLOAT_TYPE_P (t2
))
740 && !(DECIMAL_FLOAT_TYPE_P (t1
) && DECIMAL_FLOAT_TYPE_P (t2
)))
742 if (code1
== VECTOR_TYPE
|| code2
== VECTOR_TYPE
)
744 error ("can%'t mix operands of decimal float and vector types");
745 return error_mark_node
;
747 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
749 error ("can%'t mix operands of decimal float and complex types");
750 return error_mark_node
;
752 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
754 error ("can%'t mix operands of decimal float and other float types");
755 return error_mark_node
;
759 /* If one type is a vector type, return that type. (How the usual
760 arithmetic conversions apply to the vector types extension is not
761 precisely specified.) */
762 if (code1
== VECTOR_TYPE
)
765 if (code2
== VECTOR_TYPE
)
768 /* If one type is complex, form the common type of the non-complex
769 components, then make that complex. Use T1 or T2 if it is the
771 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
773 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
774 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
775 tree subtype
= c_common_type (subtype1
, subtype2
);
777 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
779 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
782 return build_complex_type (subtype
);
785 /* If only one is real, use it as the result. */
787 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
790 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
793 /* If both are real and either are decimal floating point types, use
794 the decimal floating point type with the greater precision. */
796 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
798 if (TYPE_MAIN_VARIANT (t1
) == dfloat128_type_node
799 || TYPE_MAIN_VARIANT (t2
) == dfloat128_type_node
)
800 return dfloat128_type_node
;
801 else if (TYPE_MAIN_VARIANT (t1
) == dfloat64_type_node
802 || TYPE_MAIN_VARIANT (t2
) == dfloat64_type_node
)
803 return dfloat64_type_node
;
804 else if (TYPE_MAIN_VARIANT (t1
) == dfloat32_type_node
805 || TYPE_MAIN_VARIANT (t2
) == dfloat32_type_node
)
806 return dfloat32_type_node
;
809 /* Deal with fixed-point types. */
810 if (code1
== FIXED_POINT_TYPE
|| code2
== FIXED_POINT_TYPE
)
812 unsigned int unsignedp
= 0, satp
= 0;
814 unsigned int fbit1
, ibit1
, fbit2
, ibit2
, max_fbit
, max_ibit
;
819 /* If one input type is saturating, the result type is saturating. */
820 if (TYPE_SATURATING (t1
) || TYPE_SATURATING (t2
))
823 /* If both fixed-point types are unsigned, the result type is unsigned.
824 When mixing fixed-point and integer types, follow the sign of the
826 Otherwise, the result type is signed. */
827 if ((TYPE_UNSIGNED (t1
) && TYPE_UNSIGNED (t2
)
828 && code1
== FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
)
829 || (code1
== FIXED_POINT_TYPE
&& code2
!= FIXED_POINT_TYPE
830 && TYPE_UNSIGNED (t1
))
831 || (code1
!= FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
832 && TYPE_UNSIGNED (t2
)))
835 /* The result type is signed. */
838 /* If the input type is unsigned, we need to convert to the
840 if (code1
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t1
))
842 enum mode_class mclass
= (enum mode_class
) 0;
843 if (GET_MODE_CLASS (m1
) == MODE_UFRACT
)
845 else if (GET_MODE_CLASS (m1
) == MODE_UACCUM
)
849 m1
= mode_for_size (GET_MODE_PRECISION (m1
), mclass
, 0);
851 if (code2
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t2
))
853 enum mode_class mclass
= (enum mode_class
) 0;
854 if (GET_MODE_CLASS (m2
) == MODE_UFRACT
)
856 else if (GET_MODE_CLASS (m2
) == MODE_UACCUM
)
860 m2
= mode_for_size (GET_MODE_PRECISION (m2
), mclass
, 0);
864 if (code1
== FIXED_POINT_TYPE
)
866 fbit1
= GET_MODE_FBIT (m1
);
867 ibit1
= GET_MODE_IBIT (m1
);
872 /* Signed integers need to subtract one sign bit. */
873 ibit1
= TYPE_PRECISION (t1
) - (!TYPE_UNSIGNED (t1
));
876 if (code2
== FIXED_POINT_TYPE
)
878 fbit2
= GET_MODE_FBIT (m2
);
879 ibit2
= GET_MODE_IBIT (m2
);
884 /* Signed integers need to subtract one sign bit. */
885 ibit2
= TYPE_PRECISION (t2
) - (!TYPE_UNSIGNED (t2
));
888 max_ibit
= ibit1
>= ibit2
? ibit1
: ibit2
;
889 max_fbit
= fbit1
>= fbit2
? fbit1
: fbit2
;
890 return c_common_fixed_point_type_for_size (max_ibit
, max_fbit
, unsignedp
,
894 /* Both real or both integers; use the one with greater precision. */
896 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
898 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
901 /* Same precision. Prefer long longs to longs to ints when the
902 same precision, following the C99 rules on integer type rank
903 (which are equivalent to the C90 rules for C90 types). */
905 if (TYPE_MAIN_VARIANT (t1
) == long_long_unsigned_type_node
906 || TYPE_MAIN_VARIANT (t2
) == long_long_unsigned_type_node
)
907 return long_long_unsigned_type_node
;
909 if (TYPE_MAIN_VARIANT (t1
) == long_long_integer_type_node
910 || TYPE_MAIN_VARIANT (t2
) == long_long_integer_type_node
)
912 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
913 return long_long_unsigned_type_node
;
915 return long_long_integer_type_node
;
918 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
919 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
920 return long_unsigned_type_node
;
922 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
923 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
925 /* But preserve unsignedness from the other type,
926 since long cannot hold all the values of an unsigned int. */
927 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
928 return long_unsigned_type_node
;
930 return long_integer_type_node
;
933 /* For floating types of the same TYPE_PRECISION (which we here
934 assume means either the same set of values, or sets of values
935 neither a subset of the other, with behavior being undefined in
936 the latter case), follow the rules from TS 18661-3: prefer
937 interchange types _FloatN, then standard types long double,
938 double, float, then extended types _FloatNx. For extended types,
939 check them starting with _Float128x as that seems most consistent
940 in spirit with preferring long double to double; for interchange
941 types, also check in that order for consistency although it's not
942 possible for more than one of them to have the same
944 tree mv1
= TYPE_MAIN_VARIANT (t1
);
945 tree mv2
= TYPE_MAIN_VARIANT (t2
);
947 for (int i
= NUM_FLOATN_TYPES
- 1; i
>= 0; i
--)
948 if (mv1
== FLOATN_TYPE_NODE (i
) || mv2
== FLOATN_TYPE_NODE (i
))
949 return FLOATN_TYPE_NODE (i
);
951 /* Likewise, prefer long double to double even if same size. */
952 if (mv1
== long_double_type_node
|| mv2
== long_double_type_node
)
953 return long_double_type_node
;
955 /* Likewise, prefer double to float even if same size.
956 We got a couple of embedded targets with 32 bit doubles, and the
957 pdp11 might have 64 bit floats. */
958 if (mv1
== double_type_node
|| mv2
== double_type_node
)
959 return double_type_node
;
961 if (mv1
== float_type_node
|| mv2
== float_type_node
)
962 return float_type_node
;
964 for (int i
= NUM_FLOATNX_TYPES
- 1; i
>= 0; i
--)
965 if (mv1
== FLOATNX_TYPE_NODE (i
) || mv2
== FLOATNX_TYPE_NODE (i
))
966 return FLOATNX_TYPE_NODE (i
);
968 /* Otherwise prefer the unsigned one. */
970 if (TYPE_UNSIGNED (t1
))
976 /* Wrapper around c_common_type that is used by c-common.c and other
977 front end optimizations that remove promotions. ENUMERAL_TYPEs
978 are allowed here and are converted to their compatible integer types.
979 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
980 preferably a non-Boolean type as the common type. */
982 common_type (tree t1
, tree t2
)
984 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
985 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
986 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
987 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
989 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
990 if (TREE_CODE (t1
) == BOOLEAN_TYPE
991 && TREE_CODE (t2
) == BOOLEAN_TYPE
)
992 return boolean_type_node
;
994 /* If either type is BOOLEAN_TYPE, then return the other. */
995 if (TREE_CODE (t1
) == BOOLEAN_TYPE
)
997 if (TREE_CODE (t2
) == BOOLEAN_TYPE
)
1000 return c_common_type (t1
, t2
);
1003 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1004 or various other operations. Return 2 if they are compatible
1005 but a warning may be needed if you use them together. */
1008 comptypes (tree type1
, tree type2
)
1010 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1013 val
= comptypes_internal (type1
, type2
, NULL
, NULL
);
1014 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1019 /* Like comptypes, but if it returns non-zero because enum and int are
1020 compatible, it sets *ENUM_AND_INT_P to true. */
1023 comptypes_check_enum_int (tree type1
, tree type2
, bool *enum_and_int_p
)
1025 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1028 val
= comptypes_internal (type1
, type2
, enum_and_int_p
, NULL
);
1029 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1034 /* Like comptypes, but if it returns nonzero for different types, it
1035 sets *DIFFERENT_TYPES_P to true. */
1038 comptypes_check_different_types (tree type1
, tree type2
,
1039 bool *different_types_p
)
1041 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1044 val
= comptypes_internal (type1
, type2
, NULL
, different_types_p
);
1045 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1050 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1051 or various other operations. Return 2 if they are compatible
1052 but a warning may be needed if you use them together. If
1053 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1054 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1055 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1056 NULL, and the types are compatible but different enough not to be
1057 permitted in C11 typedef redeclarations, then this sets
1058 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1059 false, but may or may not be set if the types are incompatible.
1060 This differs from comptypes, in that we don't free the seen
1064 comptypes_internal (const_tree type1
, const_tree type2
, bool *enum_and_int_p
,
1065 bool *different_types_p
)
1067 const_tree t1
= type1
;
1068 const_tree t2
= type2
;
1071 /* Suppress errors caused by previously reported errors. */
1073 if (t1
== t2
|| !t1
|| !t2
1074 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
1077 /* Enumerated types are compatible with integer types, but this is
1078 not transitive: two enumerated types in the same translation unit
1079 are compatible with each other only if they are the same type. */
1081 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
1083 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TYPE_UNSIGNED (t1
));
1084 if (TREE_CODE (t2
) != VOID_TYPE
)
1086 if (enum_and_int_p
!= NULL
)
1087 *enum_and_int_p
= true;
1088 if (different_types_p
!= NULL
)
1089 *different_types_p
= true;
1092 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
1094 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TYPE_UNSIGNED (t2
));
1095 if (TREE_CODE (t1
) != VOID_TYPE
)
1097 if (enum_and_int_p
!= NULL
)
1098 *enum_and_int_p
= true;
1099 if (different_types_p
!= NULL
)
1100 *different_types_p
= true;
1107 /* Different classes of types can't be compatible. */
1109 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1112 /* Qualifiers must match. C99 6.7.3p9 */
1114 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1117 /* Allow for two different type nodes which have essentially the same
1118 definition. Note that we already checked for equality of the type
1119 qualifiers (just above). */
1121 if (TREE_CODE (t1
) != ARRAY_TYPE
1122 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1125 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1126 if (!(attrval
= comp_type_attributes (t1
, t2
)))
1129 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1132 switch (TREE_CODE (t1
))
1135 case FIXED_POINT_TYPE
:
1137 /* With these nodes, we can't determine type equivalence by
1138 looking at what is stored in the nodes themselves, because
1139 two nodes might have different TYPE_MAIN_VARIANTs but still
1140 represent the same type. For example, wchar_t and int could
1141 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1142 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1143 and are distinct types. On the other hand, int and the
1146 typedef int INT __attribute((may_alias));
1148 have identical properties, different TYPE_MAIN_VARIANTs, but
1149 represent the same type. The canonical type system keeps
1150 track of equivalence in this case, so we fall back on it. */
1151 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1154 /* Do not remove mode information. */
1155 if (TYPE_MODE (t1
) != TYPE_MODE (t2
))
1157 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
1158 ? 1 : comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1159 enum_and_int_p
, different_types_p
));
1163 val
= function_types_compatible_p (t1
, t2
, enum_and_int_p
,
1169 tree d1
= TYPE_DOMAIN (t1
);
1170 tree d2
= TYPE_DOMAIN (t2
);
1171 bool d1_variable
, d2_variable
;
1172 bool d1_zero
, d2_zero
;
1175 /* Target types must match incl. qualifiers. */
1176 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
1177 && 0 == (val
= comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1179 different_types_p
)))
1182 if (different_types_p
!= NULL
1183 && (d1
== NULL_TREE
) != (d2
== NULL_TREE
))
1184 *different_types_p
= true;
1185 /* Sizes must match unless one is missing or variable. */
1186 if (d1
== NULL_TREE
|| d2
== NULL_TREE
|| d1
== d2
)
1189 d1_zero
= !TYPE_MAX_VALUE (d1
);
1190 d2_zero
= !TYPE_MAX_VALUE (d2
);
1192 d1_variable
= (!d1_zero
1193 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
1194 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
1195 d2_variable
= (!d2_zero
1196 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
1197 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
1198 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
1199 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
1201 if (different_types_p
!= NULL
1202 && d1_variable
!= d2_variable
)
1203 *different_types_p
= true;
1204 if (d1_variable
|| d2_variable
)
1206 if (d1_zero
&& d2_zero
)
1208 if (d1_zero
|| d2_zero
1209 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
1210 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
1219 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
1221 tree a1
= TYPE_ATTRIBUTES (t1
);
1222 tree a2
= TYPE_ATTRIBUTES (t2
);
1224 if (! attribute_list_contained (a1
, a2
)
1225 && ! attribute_list_contained (a2
, a1
))
1229 return tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1231 val
= tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1237 val
= (TYPE_VECTOR_SUBPARTS (t1
) == TYPE_VECTOR_SUBPARTS (t2
)
1238 && comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1239 enum_and_int_p
, different_types_p
));
1245 return attrval
== 2 && val
== 1 ? 2 : val
;
1248 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1249 their qualifiers, except for named address spaces. If the pointers point to
1250 different named addresses, then we must determine if one address space is a
1251 subset of the other. */
1254 comp_target_types (location_t location
, tree ttl
, tree ttr
)
1258 tree mvl
= TREE_TYPE (ttl
);
1259 tree mvr
= TREE_TYPE (ttr
);
1260 addr_space_t asl
= TYPE_ADDR_SPACE (mvl
);
1261 addr_space_t asr
= TYPE_ADDR_SPACE (mvr
);
1262 addr_space_t as_common
;
1263 bool enum_and_int_p
;
1265 /* Fail if pointers point to incompatible address spaces. */
1266 if (!addr_space_superset (asl
, asr
, &as_common
))
1269 /* For pedantic record result of comptypes on arrays before losing
1270 qualifiers on the element type below. */
1273 if (TREE_CODE (mvl
) == ARRAY_TYPE
1274 && TREE_CODE (mvr
) == ARRAY_TYPE
)
1275 val_ped
= comptypes (mvl
, mvr
);
1277 /* Qualifiers on element types of array types that are
1278 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1280 mvl
= (TYPE_ATOMIC (strip_array_types (mvl
))
1281 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
), TYPE_QUAL_ATOMIC
)
1282 : TYPE_MAIN_VARIANT (mvl
));
1284 mvr
= (TYPE_ATOMIC (strip_array_types (mvr
))
1285 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
), TYPE_QUAL_ATOMIC
)
1286 : TYPE_MAIN_VARIANT (mvr
));
1288 enum_and_int_p
= false;
1289 val
= comptypes_check_enum_int (mvl
, mvr
, &enum_and_int_p
);
1291 if (val
== 1 && val_ped
!= 1)
1292 pedwarn (location
, OPT_Wpedantic
, "pointers to arrays with different qualifiers "
1293 "are incompatible in ISO C");
1296 pedwarn (location
, OPT_Wpedantic
, "types are not quite compatible");
1298 if (val
== 1 && enum_and_int_p
&& warn_cxx_compat
)
1299 warning_at (location
, OPT_Wc___compat
,
1300 "pointer target types incompatible in C++");
1305 /* Subroutines of `comptypes'. */
1307 /* Determine whether two trees derive from the same translation unit.
1308 If the CONTEXT chain ends in a null, that tree's context is still
1309 being parsed, so if two trees have context chains ending in null,
1310 they're in the same translation unit. */
1313 same_translation_unit_p (const_tree t1
, const_tree t2
)
1315 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
1316 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
1318 case tcc_declaration
:
1319 t1
= DECL_CONTEXT (t1
); break;
1321 t1
= TYPE_CONTEXT (t1
); break;
1322 case tcc_exceptional
:
1323 t1
= BLOCK_SUPERCONTEXT (t1
); break; /* assume block */
1324 default: gcc_unreachable ();
1327 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
1328 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
1330 case tcc_declaration
:
1331 t2
= DECL_CONTEXT (t2
); break;
1333 t2
= TYPE_CONTEXT (t2
); break;
1334 case tcc_exceptional
:
1335 t2
= BLOCK_SUPERCONTEXT (t2
); break; /* assume block */
1336 default: gcc_unreachable ();
1342 /* Allocate the seen two types, assuming that they are compatible. */
1344 static struct tagged_tu_seen_cache
*
1345 alloc_tagged_tu_seen_cache (const_tree t1
, const_tree t2
)
1347 struct tagged_tu_seen_cache
*tu
= XNEW (struct tagged_tu_seen_cache
);
1348 tu
->next
= tagged_tu_seen_base
;
1352 tagged_tu_seen_base
= tu
;
1354 /* The C standard says that two structures in different translation
1355 units are compatible with each other only if the types of their
1356 fields are compatible (among other things). We assume that they
1357 are compatible until proven otherwise when building the cache.
1358 An example where this can occur is:
1363 If we are comparing this against a similar struct in another TU,
1364 and did not assume they were compatible, we end up with an infinite
1370 /* Free the seen types until we get to TU_TIL. */
1373 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*tu_til
)
1375 const struct tagged_tu_seen_cache
*tu
= tagged_tu_seen_base
;
1376 while (tu
!= tu_til
)
1378 const struct tagged_tu_seen_cache
*const tu1
1379 = (const struct tagged_tu_seen_cache
*) tu
;
1381 free (CONST_CAST (struct tagged_tu_seen_cache
*, tu1
));
1383 tagged_tu_seen_base
= tu_til
;
1386 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1387 compatible. If the two types are not the same (which has been
1388 checked earlier), this can only happen when multiple translation
1389 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1390 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1391 comptypes_internal. */
1394 tagged_types_tu_compatible_p (const_tree t1
, const_tree t2
,
1395 bool *enum_and_int_p
, bool *different_types_p
)
1398 bool needs_warning
= false;
1400 /* We have to verify that the tags of the types are the same. This
1401 is harder than it looks because this may be a typedef, so we have
1402 to go look at the original type. It may even be a typedef of a
1404 In the case of compiler-created builtin structs the TYPE_DECL
1405 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1406 while (TYPE_NAME (t1
)
1407 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1408 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
1409 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
1411 while (TYPE_NAME (t2
)
1412 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1413 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
1414 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
1416 /* C90 didn't have the requirement that the two tags be the same. */
1417 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
1420 /* C90 didn't say what happened if one or both of the types were
1421 incomplete; we choose to follow C99 rules here, which is that they
1423 if (TYPE_SIZE (t1
) == NULL
1424 || TYPE_SIZE (t2
) == NULL
)
1428 const struct tagged_tu_seen_cache
* tts_i
;
1429 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
1430 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
1434 switch (TREE_CODE (t1
))
1438 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1439 /* Speed up the case where the type values are in the same order. */
1440 tree tv1
= TYPE_VALUES (t1
);
1441 tree tv2
= TYPE_VALUES (t2
);
1448 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
1450 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
1452 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
1459 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
1463 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
1469 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
1475 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
1477 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
1479 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
1490 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1491 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
1497 /* Speed up the common case where the fields are in the same order. */
1498 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
); s1
&& s2
;
1499 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1503 if (DECL_NAME (s1
) != DECL_NAME (s2
))
1505 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1506 enum_and_int_p
, different_types_p
);
1508 if (result
!= 1 && !DECL_NAME (s1
))
1516 needs_warning
= true;
1518 if (TREE_CODE (s1
) == FIELD_DECL
1519 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1520 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1528 tu
->val
= needs_warning
? 2 : 1;
1532 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= DECL_CHAIN (s1
))
1536 for (s2
= TYPE_FIELDS (t2
); s2
; s2
= DECL_CHAIN (s2
))
1537 if (DECL_NAME (s1
) == DECL_NAME (s2
))
1541 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1545 if (result
!= 1 && !DECL_NAME (s1
))
1553 needs_warning
= true;
1555 if (TREE_CODE (s1
) == FIELD_DECL
1556 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1557 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1569 tu
->val
= needs_warning
? 2 : 10;
1575 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1577 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
1579 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1582 if (TREE_CODE (s1
) != TREE_CODE (s2
)
1583 || DECL_NAME (s1
) != DECL_NAME (s2
))
1585 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1586 enum_and_int_p
, different_types_p
);
1590 needs_warning
= true;
1592 if (TREE_CODE (s1
) == FIELD_DECL
1593 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1594 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1600 tu
->val
= needs_warning
? 2 : 1;
1609 /* Return 1 if two function types F1 and F2 are compatible.
1610 If either type specifies no argument types,
1611 the other must specify a fixed number of self-promoting arg types.
1612 Otherwise, if one type specifies only the number of arguments,
1613 the other must specify that number of self-promoting arg types.
1614 Otherwise, the argument types must match.
1615 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1618 function_types_compatible_p (const_tree f1
, const_tree f2
,
1619 bool *enum_and_int_p
, bool *different_types_p
)
1622 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1627 ret1
= TREE_TYPE (f1
);
1628 ret2
= TREE_TYPE (f2
);
1630 /* 'volatile' qualifiers on a function's return type used to mean
1631 the function is noreturn. */
1632 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
1633 pedwarn (input_location
, 0, "function return types not compatible due to %<volatile%>");
1634 if (TYPE_VOLATILE (ret1
))
1635 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
1636 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
1637 if (TYPE_VOLATILE (ret2
))
1638 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
1639 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
1640 val
= comptypes_internal (ret1
, ret2
, enum_and_int_p
, different_types_p
);
1644 args1
= TYPE_ARG_TYPES (f1
);
1645 args2
= TYPE_ARG_TYPES (f2
);
1647 if (different_types_p
!= NULL
1648 && (args1
== NULL_TREE
) != (args2
== NULL_TREE
))
1649 *different_types_p
= true;
1651 /* An unspecified parmlist matches any specified parmlist
1652 whose argument types don't need default promotions. */
1654 if (args1
== NULL_TREE
)
1656 if (!self_promoting_args_p (args2
))
1658 /* If one of these types comes from a non-prototype fn definition,
1659 compare that with the other type's arglist.
1660 If they don't match, ask for a warning (but no error). */
1661 if (TYPE_ACTUAL_ARG_TYPES (f1
)
1662 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
1663 enum_and_int_p
, different_types_p
))
1667 if (args2
== NULL_TREE
)
1669 if (!self_promoting_args_p (args1
))
1671 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1672 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
1673 enum_and_int_p
, different_types_p
))
1678 /* Both types have argument lists: compare them and propagate results. */
1679 val1
= type_lists_compatible_p (args1
, args2
, enum_and_int_p
,
1681 return val1
!= 1 ? val1
: val
;
1684 /* Check two lists of types for compatibility, returning 0 for
1685 incompatible, 1 for compatible, or 2 for compatible with
1686 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1687 comptypes_internal. */
1690 type_lists_compatible_p (const_tree args1
, const_tree args2
,
1691 bool *enum_and_int_p
, bool *different_types_p
)
1693 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1699 tree a1
, mv1
, a2
, mv2
;
1700 if (args1
== NULL_TREE
&& args2
== NULL_TREE
)
1702 /* If one list is shorter than the other,
1703 they fail to match. */
1704 if (args1
== NULL_TREE
|| args2
== NULL_TREE
)
1706 mv1
= a1
= TREE_VALUE (args1
);
1707 mv2
= a2
= TREE_VALUE (args2
);
1708 if (mv1
&& mv1
!= error_mark_node
&& TREE_CODE (mv1
) != ARRAY_TYPE
)
1709 mv1
= (TYPE_ATOMIC (mv1
)
1710 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1
),
1712 : TYPE_MAIN_VARIANT (mv1
));
1713 if (mv2
&& mv2
!= error_mark_node
&& TREE_CODE (mv2
) != ARRAY_TYPE
)
1714 mv2
= (TYPE_ATOMIC (mv2
)
1715 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2
),
1717 : TYPE_MAIN_VARIANT (mv2
));
1718 /* A null pointer instead of a type
1719 means there is supposed to be an argument
1720 but nothing is specified about what type it has.
1721 So match anything that self-promotes. */
1722 if (different_types_p
!= NULL
1723 && (a1
== NULL_TREE
) != (a2
== NULL_TREE
))
1724 *different_types_p
= true;
1725 if (a1
== NULL_TREE
)
1727 if (c_type_promotes_to (a2
) != a2
)
1730 else if (a2
== NULL_TREE
)
1732 if (c_type_promotes_to (a1
) != a1
)
1735 /* If one of the lists has an error marker, ignore this arg. */
1736 else if (TREE_CODE (a1
) == ERROR_MARK
1737 || TREE_CODE (a2
) == ERROR_MARK
)
1739 else if (!(newval
= comptypes_internal (mv1
, mv2
, enum_and_int_p
,
1740 different_types_p
)))
1742 if (different_types_p
!= NULL
)
1743 *different_types_p
= true;
1744 /* Allow wait (union {union wait *u; int *i} *)
1745 and wait (union wait *) to be compatible. */
1746 if (TREE_CODE (a1
) == UNION_TYPE
1747 && (TYPE_NAME (a1
) == NULL_TREE
1748 || TYPE_TRANSPARENT_AGGR (a1
))
1749 && TREE_CODE (TYPE_SIZE (a1
)) == INTEGER_CST
1750 && tree_int_cst_equal (TYPE_SIZE (a1
),
1754 for (memb
= TYPE_FIELDS (a1
);
1755 memb
; memb
= DECL_CHAIN (memb
))
1757 tree mv3
= TREE_TYPE (memb
);
1758 if (mv3
&& mv3
!= error_mark_node
1759 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1760 mv3
= (TYPE_ATOMIC (mv3
)
1761 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1763 : TYPE_MAIN_VARIANT (mv3
));
1764 if (comptypes_internal (mv3
, mv2
, enum_and_int_p
,
1768 if (memb
== NULL_TREE
)
1771 else if (TREE_CODE (a2
) == UNION_TYPE
1772 && (TYPE_NAME (a2
) == NULL_TREE
1773 || TYPE_TRANSPARENT_AGGR (a2
))
1774 && TREE_CODE (TYPE_SIZE (a2
)) == INTEGER_CST
1775 && tree_int_cst_equal (TYPE_SIZE (a2
),
1779 for (memb
= TYPE_FIELDS (a2
);
1780 memb
; memb
= DECL_CHAIN (memb
))
1782 tree mv3
= TREE_TYPE (memb
);
1783 if (mv3
&& mv3
!= error_mark_node
1784 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1785 mv3
= (TYPE_ATOMIC (mv3
)
1786 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1788 : TYPE_MAIN_VARIANT (mv3
));
1789 if (comptypes_internal (mv3
, mv1
, enum_and_int_p
,
1793 if (memb
== NULL_TREE
)
1800 /* comptypes said ok, but record if it said to warn. */
1804 args1
= TREE_CHAIN (args1
);
1805 args2
= TREE_CHAIN (args2
);
1809 /* Compute the size to increment a pointer by. When a function type or void
1810 type or incomplete type is passed, size_one_node is returned.
1811 This function does not emit any diagnostics; the caller is responsible
1815 c_size_in_bytes (const_tree type
)
1817 enum tree_code code
= TREE_CODE (type
);
1819 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
1820 || !COMPLETE_TYPE_P (type
))
1821 return size_one_node
;
1823 /* Convert in case a char is more than one unit. */
1824 return size_binop_loc (input_location
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1825 size_int (TYPE_PRECISION (char_type_node
)
1829 /* Return either DECL or its known constant value (if it has one). */
1832 decl_constant_value (tree decl
)
1834 if (/* Don't change a variable array bound or initial value to a constant
1835 in a place where a variable is invalid. Note that DECL_INITIAL
1836 isn't valid for a PARM_DECL. */
1837 current_function_decl
!= NULL_TREE
1838 && TREE_CODE (decl
) != PARM_DECL
1839 && !TREE_THIS_VOLATILE (decl
)
1840 && TREE_READONLY (decl
)
1841 && DECL_INITIAL (decl
) != NULL_TREE
1842 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
1843 /* This is invalid if initial value is not constant.
1844 If it has either a function call, a memory reference,
1845 or a variable, then re-evaluating it could give different results. */
1846 && TREE_CONSTANT (DECL_INITIAL (decl
))
1847 /* Check for cases where this is sub-optimal, even though valid. */
1848 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1849 return DECL_INITIAL (decl
);
1853 /* Convert the array expression EXP to a pointer. */
1855 array_to_pointer_conversion (location_t loc
, tree exp
)
1857 tree orig_exp
= exp
;
1858 tree type
= TREE_TYPE (exp
);
1860 tree restype
= TREE_TYPE (type
);
1863 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1865 STRIP_TYPE_NOPS (exp
);
1867 if (TREE_NO_WARNING (orig_exp
))
1868 TREE_NO_WARNING (exp
) = 1;
1870 ptrtype
= build_pointer_type (restype
);
1872 if (INDIRECT_REF_P (exp
))
1873 return convert (ptrtype
, TREE_OPERAND (exp
, 0));
1875 /* In C++ array compound literals are temporary objects unless they are
1876 const or appear in namespace scope, so they are destroyed too soon
1877 to use them for much of anything (c++/53220). */
1878 if (warn_cxx_compat
&& TREE_CODE (exp
) == COMPOUND_LITERAL_EXPR
)
1880 tree decl
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
1881 if (!TREE_READONLY (decl
) && !TREE_STATIC (decl
))
1882 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
1883 "converting an array compound literal to a pointer "
1884 "is ill-formed in C++");
1887 adr
= build_unary_op (loc
, ADDR_EXPR
, exp
, true);
1888 return convert (ptrtype
, adr
);
1891 /* Convert the function expression EXP to a pointer. */
1893 function_to_pointer_conversion (location_t loc
, tree exp
)
1895 tree orig_exp
= exp
;
1897 gcc_assert (TREE_CODE (TREE_TYPE (exp
)) == FUNCTION_TYPE
);
1899 STRIP_TYPE_NOPS (exp
);
1901 if (TREE_NO_WARNING (orig_exp
))
1902 TREE_NO_WARNING (exp
) = 1;
1904 return build_unary_op (loc
, ADDR_EXPR
, exp
, false);
1907 /* Mark EXP as read, not just set, for set but not used -Wunused
1908 warning purposes. */
1911 mark_exp_read (tree exp
)
1913 switch (TREE_CODE (exp
))
1917 DECL_READ_P (exp
) = 1;
1926 case VIEW_CONVERT_EXPR
:
1927 mark_exp_read (TREE_OPERAND (exp
, 0));
1930 case C_MAYBE_CONST_EXPR
:
1931 mark_exp_read (TREE_OPERAND (exp
, 1));
1938 /* Perform the default conversion of arrays and functions to pointers.
1939 Return the result of converting EXP. For any other expression, just
1942 LOC is the location of the expression. */
1945 default_function_array_conversion (location_t loc
, struct c_expr exp
)
1947 tree orig_exp
= exp
.value
;
1948 tree type
= TREE_TYPE (exp
.value
);
1949 enum tree_code code
= TREE_CODE (type
);
1955 bool not_lvalue
= false;
1956 bool lvalue_array_p
;
1958 while ((TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
1959 || CONVERT_EXPR_P (exp
.value
))
1960 && TREE_TYPE (TREE_OPERAND (exp
.value
, 0)) == type
)
1962 if (TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
)
1964 exp
.value
= TREE_OPERAND (exp
.value
, 0);
1967 if (TREE_NO_WARNING (orig_exp
))
1968 TREE_NO_WARNING (exp
.value
) = 1;
1970 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
.value
);
1971 if (!flag_isoc99
&& !lvalue_array_p
)
1973 /* Before C99, non-lvalue arrays do not decay to pointers.
1974 Normally, using such an array would be invalid; but it can
1975 be used correctly inside sizeof or as a statement expression.
1976 Thus, do not give an error here; an error will result later. */
1980 exp
.value
= array_to_pointer_conversion (loc
, exp
.value
);
1984 exp
.value
= function_to_pointer_conversion (loc
, exp
.value
);
1994 default_function_array_read_conversion (location_t loc
, struct c_expr exp
)
1996 mark_exp_read (exp
.value
);
1997 return default_function_array_conversion (loc
, exp
);
2000 /* Return whether EXPR should be treated as an atomic lvalue for the
2001 purposes of load and store handling. */
2004 really_atomic_lvalue (tree expr
)
2006 if (error_operand_p (expr
))
2008 if (!TYPE_ATOMIC (TREE_TYPE (expr
)))
2010 if (!lvalue_p (expr
))
2013 /* Ignore _Atomic on register variables, since their addresses can't
2014 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2015 sequences wouldn't work. Ignore _Atomic on structures containing
2016 bit-fields, since accessing elements of atomic structures or
2017 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2018 it's undefined at translation time or execution time, and the
2019 normal atomic sequences again wouldn't work. */
2020 while (handled_component_p (expr
))
2022 if (TREE_CODE (expr
) == COMPONENT_REF
2023 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
2025 expr
= TREE_OPERAND (expr
, 0);
2027 if (DECL_P (expr
) && C_DECL_REGISTER (expr
))
2032 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2033 including converting functions and arrays to pointers if CONVERT_P.
2034 If READ_P, also mark the expression as having been read. */
2037 convert_lvalue_to_rvalue (location_t loc
, struct c_expr exp
,
2038 bool convert_p
, bool read_p
)
2041 mark_exp_read (exp
.value
);
2043 exp
= default_function_array_conversion (loc
, exp
);
2044 if (really_atomic_lvalue (exp
.value
))
2046 vec
<tree
, va_gc
> *params
;
2047 tree nonatomic_type
, tmp
, tmp_addr
, fndecl
, func_call
;
2048 tree expr_type
= TREE_TYPE (exp
.value
);
2049 tree expr_addr
= build_unary_op (loc
, ADDR_EXPR
, exp
.value
, false);
2050 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
2052 gcc_assert (TYPE_ATOMIC (expr_type
));
2054 /* Expansion of a generic atomic load may require an addition
2055 element, so allocate enough to prevent a resize. */
2056 vec_alloc (params
, 4);
2058 /* Remove the qualifiers for the rest of the expressions and
2059 create the VAL temp variable to hold the RHS. */
2060 nonatomic_type
= build_qualified_type (expr_type
, TYPE_UNQUALIFIED
);
2061 tmp
= create_tmp_var_raw (nonatomic_type
);
2062 tmp_addr
= build_unary_op (loc
, ADDR_EXPR
, tmp
, false);
2063 TREE_ADDRESSABLE (tmp
) = 1;
2064 TREE_NO_WARNING (tmp
) = 1;
2066 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2067 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
2068 params
->quick_push (expr_addr
);
2069 params
->quick_push (tmp_addr
);
2070 params
->quick_push (seq_cst
);
2071 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
2073 /* EXPR is always read. */
2074 mark_exp_read (exp
.value
);
2076 /* Return tmp which contains the value loaded. */
2077 exp
.value
= build4 (TARGET_EXPR
, nonatomic_type
, tmp
, func_call
,
2078 NULL_TREE
, NULL_TREE
);
2083 /* EXP is an expression of integer type. Apply the integer promotions
2084 to it and return the promoted value. */
2087 perform_integral_promotions (tree exp
)
2089 tree type
= TREE_TYPE (exp
);
2090 enum tree_code code
= TREE_CODE (type
);
2092 gcc_assert (INTEGRAL_TYPE_P (type
));
2094 /* Normally convert enums to int,
2095 but convert wide enums to something wider. */
2096 if (code
== ENUMERAL_TYPE
)
2098 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
2099 TYPE_PRECISION (integer_type_node
)),
2100 ((TYPE_PRECISION (type
)
2101 >= TYPE_PRECISION (integer_type_node
))
2102 && TYPE_UNSIGNED (type
)));
2104 return convert (type
, exp
);
2107 /* ??? This should no longer be needed now bit-fields have their
2109 if (TREE_CODE (exp
) == COMPONENT_REF
2110 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
2111 /* If it's thinner than an int, promote it like a
2112 c_promoting_integer_type_p, otherwise leave it alone. */
2113 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
2114 TYPE_PRECISION (integer_type_node
)))
2115 return convert (integer_type_node
, exp
);
2117 if (c_promoting_integer_type_p (type
))
2119 /* Preserve unsignedness if not really getting any wider. */
2120 if (TYPE_UNSIGNED (type
)
2121 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
2122 return convert (unsigned_type_node
, exp
);
2124 return convert (integer_type_node
, exp
);
2131 /* Perform default promotions for C data used in expressions.
2132 Enumeral types or short or char are converted to int.
2133 In addition, manifest constants symbols are replaced by their values. */
2136 default_conversion (tree exp
)
2139 tree type
= TREE_TYPE (exp
);
2140 enum tree_code code
= TREE_CODE (type
);
2143 mark_exp_read (exp
);
2145 /* Functions and arrays have been converted during parsing. */
2146 gcc_assert (code
!= FUNCTION_TYPE
);
2147 if (code
== ARRAY_TYPE
)
2150 /* Constants can be used directly unless they're not loadable. */
2151 if (TREE_CODE (exp
) == CONST_DECL
)
2152 exp
= DECL_INITIAL (exp
);
2154 /* Strip no-op conversions. */
2156 STRIP_TYPE_NOPS (exp
);
2158 if (TREE_NO_WARNING (orig_exp
))
2159 TREE_NO_WARNING (exp
) = 1;
2161 if (code
== VOID_TYPE
)
2163 error_at (EXPR_LOC_OR_LOC (exp
, input_location
),
2164 "void value not ignored as it ought to be");
2165 return error_mark_node
;
2168 exp
= require_complete_type (EXPR_LOC_OR_LOC (exp
, input_location
), exp
);
2169 if (exp
== error_mark_node
)
2170 return error_mark_node
;
2172 promoted_type
= targetm
.promoted_type (type
);
2174 return convert (promoted_type
, exp
);
2176 if (INTEGRAL_TYPE_P (type
))
2177 return perform_integral_promotions (exp
);
2182 /* Look up COMPONENT in a structure or union TYPE.
2184 If the component name is not found, returns NULL_TREE. Otherwise,
2185 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2186 stepping down the chain to the component, which is in the last
2187 TREE_VALUE of the list. Normally the list is of length one, but if
2188 the component is embedded within (nested) anonymous structures or
2189 unions, the list steps down the chain to the component. */
2192 lookup_field (tree type
, tree component
)
2196 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2197 to the field elements. Use a binary search on this array to quickly
2198 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2199 will always be set for structures which have many elements. */
2201 if (TYPE_LANG_SPECIFIC (type
) && TYPE_LANG_SPECIFIC (type
)->s
)
2204 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
2206 field
= TYPE_FIELDS (type
);
2208 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
2209 while (top
- bot
> 1)
2211 half
= (top
- bot
+ 1) >> 1;
2212 field
= field_array
[bot
+half
];
2214 if (DECL_NAME (field
) == NULL_TREE
)
2216 /* Step through all anon unions in linear fashion. */
2217 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
2219 field
= field_array
[bot
++];
2220 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2222 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2225 return tree_cons (NULL_TREE
, field
, anon
);
2227 /* The Plan 9 compiler permits referring
2228 directly to an anonymous struct/union field
2229 using a typedef name. */
2230 if (flag_plan9_extensions
2231 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2232 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field
)))
2234 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2240 /* Entire record is only anon unions. */
2244 /* Restart the binary search, with new lower bound. */
2248 if (DECL_NAME (field
) == component
)
2250 if (DECL_NAME (field
) < component
)
2256 if (DECL_NAME (field_array
[bot
]) == component
)
2257 field
= field_array
[bot
];
2258 else if (DECL_NAME (field
) != component
)
2263 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2265 if (DECL_NAME (field
) == NULL_TREE
2266 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2268 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2271 return tree_cons (NULL_TREE
, field
, anon
);
2273 /* The Plan 9 compiler permits referring directly to an
2274 anonymous struct/union field using a typedef
2276 if (flag_plan9_extensions
2277 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2278 && TREE_CODE (TYPE_NAME (TREE_TYPE (field
))) == TYPE_DECL
2279 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2284 if (DECL_NAME (field
) == component
)
2288 if (field
== NULL_TREE
)
2292 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
2295 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2298 lookup_field_fuzzy_find_candidates (tree type
, tree component
,
2299 vec
<tree
> *candidates
)
2302 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2304 if (DECL_NAME (field
) == NULL_TREE
2305 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2306 lookup_field_fuzzy_find_candidates (TREE_TYPE (field
), component
,
2309 if (DECL_NAME (field
))
2310 candidates
->safe_push (DECL_NAME (field
));
2314 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2315 rather than returning a TREE_LIST for an exact match. */
2318 lookup_field_fuzzy (tree type
, tree component
)
2320 gcc_assert (TREE_CODE (component
) == IDENTIFIER_NODE
);
2322 /* First, gather a list of candidates. */
2323 auto_vec
<tree
> candidates
;
2325 lookup_field_fuzzy_find_candidates (type
, component
,
2328 return find_closest_identifier (component
, &candidates
);
2331 /* Support function for build_component_ref's error-handling.
2333 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2334 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2337 should_suggest_deref_p (tree datum_type
)
2339 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2340 allows "." for ptrs; we could be handling a failed attempt
2341 to access a property. */
2342 if (c_dialect_objc ())
2345 /* Only suggest it for pointers... */
2346 if (TREE_CODE (datum_type
) != POINTER_TYPE
)
2349 /* ...to structs/unions. */
2350 tree underlying_type
= TREE_TYPE (datum_type
);
2351 enum tree_code code
= TREE_CODE (underlying_type
);
2352 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2358 /* Make an expression to refer to the COMPONENT field of structure or
2359 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2360 location of the COMPONENT_REF. COMPONENT_LOC is the location
2364 build_component_ref (location_t loc
, tree datum
, tree component
,
2365 location_t component_loc
)
2367 tree type
= TREE_TYPE (datum
);
2368 enum tree_code code
= TREE_CODE (type
);
2371 bool datum_lvalue
= lvalue_p (datum
);
2373 if (!objc_is_public (datum
, component
))
2374 return error_mark_node
;
2376 /* Detect Objective-C property syntax object.property. */
2377 if (c_dialect_objc ()
2378 && (ref
= objc_maybe_build_component_ref (datum
, component
)))
2381 /* See if there is a field or component with name COMPONENT. */
2383 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2385 if (!COMPLETE_TYPE_P (type
))
2387 c_incomplete_type_error (loc
, NULL_TREE
, type
);
2388 return error_mark_node
;
2391 field
= lookup_field (type
, component
);
2395 tree guessed_id
= lookup_field_fuzzy (type
, component
);
2398 /* Attempt to provide a fixit replacement hint, if
2399 we have a valid range for the component. */
2400 location_t reported_loc
2401 = (component_loc
!= UNKNOWN_LOCATION
) ? component_loc
: loc
;
2402 gcc_rich_location
rich_loc (reported_loc
);
2403 if (component_loc
!= UNKNOWN_LOCATION
)
2404 rich_loc
.add_fixit_misspelled_id (component_loc
, guessed_id
);
2407 "%qT has no member named %qE; did you mean %qE?",
2408 type
, component
, guessed_id
);
2411 error_at (loc
, "%qT has no member named %qE", type
, component
);
2412 return error_mark_node
;
2415 /* Accessing elements of atomic structures or unions is undefined
2416 behavior (C11 6.5.2.3#5). */
2417 if (TYPE_ATOMIC (type
) && c_inhibit_evaluation_warnings
== 0)
2419 if (code
== RECORD_TYPE
)
2420 warning_at (loc
, 0, "accessing a member %qE of an atomic "
2421 "structure %qE", component
, datum
);
2423 warning_at (loc
, 0, "accessing a member %qE of an atomic "
2424 "union %qE", component
, datum
);
2427 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2428 This might be better solved in future the way the C++ front
2429 end does it - by giving the anonymous entities each a
2430 separate name and type, and then have build_component_ref
2431 recursively call itself. We can't do that here. */
2434 tree subdatum
= TREE_VALUE (field
);
2437 bool use_datum_quals
;
2439 if (TREE_TYPE (subdatum
) == error_mark_node
)
2440 return error_mark_node
;
2442 /* If this is an rvalue, it does not have qualifiers in C
2443 standard terms and we must avoid propagating such
2444 qualifiers down to a non-lvalue array that is then
2445 converted to a pointer. */
2446 use_datum_quals
= (datum_lvalue
2447 || TREE_CODE (TREE_TYPE (subdatum
)) != ARRAY_TYPE
);
2449 quals
= TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum
)));
2450 if (use_datum_quals
)
2451 quals
|= TYPE_QUALS (TREE_TYPE (datum
));
2452 subtype
= c_build_qualified_type (TREE_TYPE (subdatum
), quals
);
2454 ref
= build3 (COMPONENT_REF
, subtype
, datum
, subdatum
,
2456 SET_EXPR_LOCATION (ref
, loc
);
2457 if (TREE_READONLY (subdatum
)
2458 || (use_datum_quals
&& TREE_READONLY (datum
)))
2459 TREE_READONLY (ref
) = 1;
2460 if (TREE_THIS_VOLATILE (subdatum
)
2461 || (use_datum_quals
&& TREE_THIS_VOLATILE (datum
)))
2462 TREE_THIS_VOLATILE (ref
) = 1;
2464 if (TREE_DEPRECATED (subdatum
))
2465 warn_deprecated_use (subdatum
, NULL_TREE
);
2469 field
= TREE_CHAIN (field
);
2475 else if (should_suggest_deref_p (type
))
2477 /* Special-case the error message for "ptr.field" for the case
2478 where the user has confused "." vs "->". */
2479 rich_location
richloc (line_table
, loc
);
2480 /* "loc" should be the "." token. */
2481 richloc
.add_fixit_replace ("->");
2482 error_at_rich_loc (&richloc
,
2483 "%qE is a pointer; did you mean to use %<->%>?",
2485 return error_mark_node
;
2487 else if (code
!= ERROR_MARK
)
2489 "request for member %qE in something not a structure or union",
2492 return error_mark_node
;
2495 /* Given an expression PTR for a pointer, return an expression
2496 for the value pointed to.
2497 ERRORSTRING is the name of the operator to appear in error messages.
2499 LOC is the location to use for the generated tree. */
2502 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errstring
)
2504 tree pointer
= default_conversion (ptr
);
2505 tree type
= TREE_TYPE (pointer
);
2508 if (TREE_CODE (type
) == POINTER_TYPE
)
2510 if (CONVERT_EXPR_P (pointer
)
2511 || TREE_CODE (pointer
) == VIEW_CONVERT_EXPR
)
2513 /* If a warning is issued, mark it to avoid duplicates from
2514 the backend. This only needs to be done at
2515 warn_strict_aliasing > 2. */
2516 if (warn_strict_aliasing
> 2)
2517 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer
, 0)),
2518 type
, TREE_OPERAND (pointer
, 0)))
2519 TREE_NO_WARNING (pointer
) = 1;
2522 if (TREE_CODE (pointer
) == ADDR_EXPR
2523 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
2524 == TREE_TYPE (type
)))
2526 ref
= TREE_OPERAND (pointer
, 0);
2527 protected_set_expr_location (ref
, loc
);
2532 tree t
= TREE_TYPE (type
);
2534 ref
= build1 (INDIRECT_REF
, t
, pointer
);
2536 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
2538 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)))
2540 error_at (loc
, "dereferencing pointer to incomplete type "
2542 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)) = 1;
2544 return error_mark_node
;
2546 if (VOID_TYPE_P (t
) && c_inhibit_evaluation_warnings
== 0)
2547 warning_at (loc
, 0, "dereferencing %<void *%> pointer");
2549 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2550 so that we get the proper error message if the result is used
2551 to assign to. Also, &* is supposed to be a no-op.
2552 And ANSI C seems to specify that the type of the result
2553 should be the const type. */
2554 /* A de-reference of a pointer to const is not a const. It is valid
2555 to change it via some other pointer. */
2556 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2557 TREE_SIDE_EFFECTS (ref
)
2558 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
2559 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2560 protected_set_expr_location (ref
, loc
);
2564 else if (TREE_CODE (pointer
) != ERROR_MARK
)
2565 invalid_indirection_error (loc
, type
, errstring
);
2567 return error_mark_node
;
2570 /* This handles expressions of the form "a[i]", which denotes
2573 This is logically equivalent in C to *(a+i), but we may do it differently.
2574 If A is a variable or a member, we generate a primitive ARRAY_REF.
2575 This avoids forcing the array out of registers, and can work on
2576 arrays that are not lvalues (for example, members of structures returned
2579 For vector types, allow vector[i] but not i[vector], and create
2580 *(((type*)&vectortype) + i) for the expression.
2582 LOC is the location to use for the returned expression. */
2585 build_array_ref (location_t loc
, tree array
, tree index
)
2588 bool swapped
= false;
2589 if (TREE_TYPE (array
) == error_mark_node
2590 || TREE_TYPE (index
) == error_mark_node
)
2591 return error_mark_node
;
2593 if (flag_cilkplus
&& contains_array_notation_expr (index
))
2596 if (!find_rank (loc
, index
, index
, true, &rank
))
2597 return error_mark_node
;
2600 error_at (loc
, "rank of the array's index is greater than 1");
2601 return error_mark_node
;
2604 if (TREE_CODE (TREE_TYPE (array
)) != ARRAY_TYPE
2605 && TREE_CODE (TREE_TYPE (array
)) != POINTER_TYPE
2606 /* Allow vector[index] but not index[vector]. */
2607 && !VECTOR_TYPE_P (TREE_TYPE (array
)))
2609 if (TREE_CODE (TREE_TYPE (index
)) != ARRAY_TYPE
2610 && TREE_CODE (TREE_TYPE (index
)) != POINTER_TYPE
)
2613 "subscripted value is neither array nor pointer nor vector");
2615 return error_mark_node
;
2617 std::swap (array
, index
);
2621 if (!INTEGRAL_TYPE_P (TREE_TYPE (index
)))
2623 error_at (loc
, "array subscript is not an integer");
2624 return error_mark_node
;
2627 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array
))) == FUNCTION_TYPE
)
2629 error_at (loc
, "subscripted value is pointer to function");
2630 return error_mark_node
;
2633 /* ??? Existing practice has been to warn only when the char
2634 index is syntactically the index, not for char[array]. */
2636 warn_array_subscript_with_type_char (loc
, index
);
2638 /* Apply default promotions *after* noticing character types. */
2639 index
= default_conversion (index
);
2640 if (index
== error_mark_node
)
2641 return error_mark_node
;
2643 gcc_assert (TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
);
2645 bool was_vector
= VECTOR_TYPE_P (TREE_TYPE (array
));
2646 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, index
);
2648 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
2652 /* An array that is indexed by a non-constant
2653 cannot be stored in a register; we must be able to do
2654 address arithmetic on its address.
2655 Likewise an array of elements of variable size. */
2656 if (TREE_CODE (index
) != INTEGER_CST
2657 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2658 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2660 if (!c_mark_addressable (array
, true))
2661 return error_mark_node
;
2663 /* An array that is indexed by a constant value which is not within
2664 the array bounds cannot be stored in a register either; because we
2665 would get a crash in store_bit_field/extract_bit_field when trying
2666 to access a non-existent part of the register. */
2667 if (TREE_CODE (index
) == INTEGER_CST
2668 && TYPE_DOMAIN (TREE_TYPE (array
))
2669 && !int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
2671 if (!c_mark_addressable (array
))
2672 return error_mark_node
;
2675 if ((pedantic
|| warn_c90_c99_compat
)
2679 while (TREE_CODE (foo
) == COMPONENT_REF
)
2680 foo
= TREE_OPERAND (foo
, 0);
2681 if (VAR_P (foo
) && C_DECL_REGISTER (foo
))
2682 pedwarn (loc
, OPT_Wpedantic
,
2683 "ISO C forbids subscripting %<register%> array");
2684 else if (!lvalue_p (foo
))
2685 pedwarn_c90 (loc
, OPT_Wpedantic
,
2686 "ISO C90 forbids subscripting non-lvalue "
2690 type
= TREE_TYPE (TREE_TYPE (array
));
2691 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
2692 /* Array ref is const/volatile if the array elements are
2693 or if the array is. */
2694 TREE_READONLY (rval
)
2695 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2696 | TREE_READONLY (array
));
2697 TREE_SIDE_EFFECTS (rval
)
2698 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2699 | TREE_SIDE_EFFECTS (array
));
2700 TREE_THIS_VOLATILE (rval
)
2701 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2702 /* This was added by rms on 16 Nov 91.
2703 It fixes vol struct foo *a; a->elts[1]
2704 in an inline function.
2705 Hope it doesn't break something else. */
2706 | TREE_THIS_VOLATILE (array
));
2707 ret
= require_complete_type (loc
, rval
);
2708 protected_set_expr_location (ret
, loc
);
2710 ret
= non_lvalue_loc (loc
, ret
);
2715 tree ar
= default_conversion (array
);
2717 if (ar
== error_mark_node
)
2720 gcc_assert (TREE_CODE (TREE_TYPE (ar
)) == POINTER_TYPE
);
2721 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) != FUNCTION_TYPE
);
2723 ret
= build_indirect_ref (loc
, build_binary_op (loc
, PLUS_EXPR
, ar
,
2727 ret
= non_lvalue_loc (loc
, ret
);
2732 /* Build an external reference to identifier ID. FUN indicates
2733 whether this will be used for a function call. LOC is the source
2734 location of the identifier. This sets *TYPE to the type of the
2735 identifier, which is not the same as the type of the returned value
2736 for CONST_DECLs defined as enum constants. If the type of the
2737 identifier is not available, *TYPE is set to NULL. */
2739 build_external_ref (location_t loc
, tree id
, int fun
, tree
*type
)
2742 tree decl
= lookup_name (id
);
2744 /* In Objective-C, an instance variable (ivar) may be preferred to
2745 whatever lookup_name() found. */
2746 decl
= objc_lookup_ivar (decl
, id
);
2749 if (decl
&& decl
!= error_mark_node
)
2752 *type
= TREE_TYPE (ref
);
2755 /* Implicit function declaration. */
2756 ref
= implicitly_declare (loc
, id
);
2757 else if (decl
== error_mark_node
)
2758 /* Don't complain about something that's already been
2759 complained about. */
2760 return error_mark_node
;
2763 undeclared_variable (loc
, id
);
2764 return error_mark_node
;
2767 if (TREE_TYPE (ref
) == error_mark_node
)
2768 return error_mark_node
;
2770 if (TREE_DEPRECATED (ref
))
2771 warn_deprecated_use (ref
, NULL_TREE
);
2773 /* Recursive call does not count as usage. */
2774 if (ref
!= current_function_decl
)
2776 TREE_USED (ref
) = 1;
2779 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
2781 if (!in_sizeof
&& !in_typeof
)
2782 C_DECL_USED (ref
) = 1;
2783 else if (DECL_INITIAL (ref
) == NULL_TREE
2784 && DECL_EXTERNAL (ref
)
2785 && !TREE_PUBLIC (ref
))
2786 record_maybe_used_decl (ref
);
2789 if (TREE_CODE (ref
) == CONST_DECL
)
2791 used_types_insert (TREE_TYPE (ref
));
2794 && TREE_CODE (TREE_TYPE (ref
)) == ENUMERAL_TYPE
2795 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref
)))
2797 warning_at (loc
, OPT_Wc___compat
,
2798 ("enum constant defined in struct or union "
2799 "is not visible in C++"));
2800 inform (DECL_SOURCE_LOCATION (ref
), "enum constant defined here");
2803 ref
= DECL_INITIAL (ref
);
2804 TREE_CONSTANT (ref
) = 1;
2806 else if (current_function_decl
!= NULL_TREE
2807 && !DECL_FILE_SCOPE_P (current_function_decl
)
2808 && (VAR_OR_FUNCTION_DECL_P (ref
)
2809 || TREE_CODE (ref
) == PARM_DECL
))
2811 tree context
= decl_function_context (ref
);
2813 if (context
!= NULL_TREE
&& context
!= current_function_decl
)
2814 DECL_NONLOCAL (ref
) = 1;
2816 /* C99 6.7.4p3: An inline definition of a function with external
2817 linkage ... shall not contain a reference to an identifier with
2818 internal linkage. */
2819 else if (current_function_decl
!= NULL_TREE
2820 && DECL_DECLARED_INLINE_P (current_function_decl
)
2821 && DECL_EXTERNAL (current_function_decl
)
2822 && VAR_OR_FUNCTION_DECL_P (ref
)
2823 && (!VAR_P (ref
) || TREE_STATIC (ref
))
2824 && ! TREE_PUBLIC (ref
)
2825 && DECL_CONTEXT (ref
) != current_function_decl
)
2826 record_inline_static (loc
, current_function_decl
, ref
,
2832 /* Record details of decls possibly used inside sizeof or typeof. */
2833 struct maybe_used_decl
2837 /* The level seen at (in_sizeof + in_typeof). */
2839 /* The next one at this level or above, or NULL. */
2840 struct maybe_used_decl
*next
;
2843 static struct maybe_used_decl
*maybe_used_decls
;
2845 /* Record that DECL, an undefined static function reference seen
2846 inside sizeof or typeof, might be used if the operand of sizeof is
2847 a VLA type or the operand of typeof is a variably modified
2851 record_maybe_used_decl (tree decl
)
2853 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
2855 t
->level
= in_sizeof
+ in_typeof
;
2856 t
->next
= maybe_used_decls
;
2857 maybe_used_decls
= t
;
2860 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2861 USED is false, just discard them. If it is true, mark them used
2862 (if no longer inside sizeof or typeof) or move them to the next
2863 level up (if still inside sizeof or typeof). */
2866 pop_maybe_used (bool used
)
2868 struct maybe_used_decl
*p
= maybe_used_decls
;
2869 int cur_level
= in_sizeof
+ in_typeof
;
2870 while (p
&& p
->level
> cur_level
)
2875 C_DECL_USED (p
->decl
) = 1;
2877 p
->level
= cur_level
;
2881 if (!used
|| cur_level
== 0)
2882 maybe_used_decls
= p
;
2885 /* Return the result of sizeof applied to EXPR. */
2888 c_expr_sizeof_expr (location_t loc
, struct c_expr expr
)
2891 if (expr
.value
== error_mark_node
)
2893 ret
.value
= error_mark_node
;
2894 ret
.original_code
= ERROR_MARK
;
2895 ret
.original_type
= NULL
;
2896 pop_maybe_used (false);
2900 bool expr_const_operands
= true;
2902 if (TREE_CODE (expr
.value
) == PARM_DECL
2903 && C_ARRAY_PARAMETER (expr
.value
))
2905 if (warning_at (loc
, OPT_Wsizeof_array_argument
,
2906 "%<sizeof%> on array function parameter %qE will "
2907 "return size of %qT", expr
.value
,
2908 expr
.original_type
))
2909 inform (DECL_SOURCE_LOCATION (expr
.value
), "declared here");
2911 tree folded_expr
= c_fully_fold (expr
.value
, require_constant_value
,
2912 &expr_const_operands
);
2913 ret
.value
= c_sizeof (loc
, TREE_TYPE (folded_expr
));
2914 c_last_sizeof_arg
= expr
.value
;
2915 c_last_sizeof_loc
= loc
;
2916 ret
.original_code
= SIZEOF_EXPR
;
2917 ret
.original_type
= NULL
;
2918 if (c_vla_type_p (TREE_TYPE (folded_expr
)))
2920 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2921 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2922 folded_expr
, ret
.value
);
2923 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !expr_const_operands
;
2924 SET_EXPR_LOCATION (ret
.value
, loc
);
2926 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr
)));
2931 /* Return the result of sizeof applied to T, a structure for the type
2932 name passed to sizeof (rather than the type itself). LOC is the
2933 location of the original expression. */
2936 c_expr_sizeof_type (location_t loc
, struct c_type_name
*t
)
2940 tree type_expr
= NULL_TREE
;
2941 bool type_expr_const
= true;
2942 type
= groktypename (t
, &type_expr
, &type_expr_const
);
2943 ret
.value
= c_sizeof (loc
, type
);
2944 c_last_sizeof_arg
= type
;
2945 c_last_sizeof_loc
= loc
;
2946 ret
.original_code
= SIZEOF_EXPR
;
2947 ret
.original_type
= NULL
;
2948 if ((type_expr
|| TREE_CODE (ret
.value
) == INTEGER_CST
)
2949 && c_vla_type_p (type
))
2951 /* If the type is a [*] array, it is a VLA but is represented as
2952 having a size of zero. In such a case we must ensure that
2953 the result of sizeof does not get folded to a constant by
2954 c_fully_fold, because if the size is evaluated the result is
2955 not constant and so constraints on zero or negative size
2956 arrays must not be applied when this sizeof call is inside
2957 another array declarator. */
2959 type_expr
= integer_zero_node
;
2960 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2961 type_expr
, ret
.value
);
2962 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !type_expr_const
;
2964 pop_maybe_used (type
!= error_mark_node
2965 ? C_TYPE_VARIABLE_SIZE (type
) : false);
2969 /* Build a function call to function FUNCTION with parameters PARAMS.
2970 The function call is at LOC.
2971 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2972 TREE_VALUE of each node is a parameter-expression.
2973 FUNCTION's data type may be a function type or a pointer-to-function. */
2976 build_function_call (location_t loc
, tree function
, tree params
)
2978 vec
<tree
, va_gc
> *v
;
2981 vec_alloc (v
, list_length (params
));
2982 for (; params
; params
= TREE_CHAIN (params
))
2983 v
->quick_push (TREE_VALUE (params
));
2984 ret
= c_build_function_call_vec (loc
, vNULL
, function
, v
, NULL
);
2989 /* Give a note about the location of the declaration of DECL. */
2992 inform_declaration (tree decl
)
2994 if (decl
&& (TREE_CODE (decl
) != FUNCTION_DECL
|| !DECL_IS_BUILTIN (decl
)))
2995 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
2998 /* Build a function call to function FUNCTION with parameters PARAMS.
2999 ORIGTYPES, if not NULL, is a vector of types; each element is
3000 either NULL or the original type of the corresponding element in
3001 PARAMS. The original type may differ from TREE_TYPE of the
3002 parameter for enums. FUNCTION's data type may be a function type
3003 or pointer-to-function. This function changes the elements of
3007 build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
3008 tree function
, vec
<tree
, va_gc
> *params
,
3009 vec
<tree
, va_gc
> *origtypes
)
3011 tree fntype
, fundecl
= NULL_TREE
;
3012 tree name
= NULL_TREE
, result
;
3018 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3019 STRIP_TYPE_NOPS (function
);
3021 /* Convert anything with function type to a pointer-to-function. */
3022 if (TREE_CODE (function
) == FUNCTION_DECL
)
3024 name
= DECL_NAME (function
);
3027 tm_malloc_replacement (function
);
3029 /* Atomic functions have type checking/casting already done. They are
3030 often rewritten and don't match the original parameter list. */
3031 if (name
&& !strncmp (IDENTIFIER_POINTER (name
), "__atomic_", 9))
3035 && is_cilkplus_reduce_builtin (function
))
3038 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
)
3039 function
= function_to_pointer_conversion (loc
, function
);
3041 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3042 expressions, like those used for ObjC messenger dispatches. */
3043 if (params
&& !params
->is_empty ())
3044 function
= objc_rewrite_function_call (function
, (*params
)[0]);
3046 function
= c_fully_fold (function
, false, NULL
);
3048 fntype
= TREE_TYPE (function
);
3050 if (TREE_CODE (fntype
) == ERROR_MARK
)
3051 return error_mark_node
;
3053 if (!(TREE_CODE (fntype
) == POINTER_TYPE
3054 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
3056 if (!flag_diagnostics_show_caret
)
3058 "called object %qE is not a function or function pointer",
3060 else if (DECL_P (function
))
3063 "called object %qD is not a function or function pointer",
3065 inform_declaration (function
);
3069 "called object is not a function or function pointer");
3070 return error_mark_node
;
3073 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
3074 current_function_returns_abnormally
= 1;
3076 /* fntype now gets the type of function pointed to. */
3077 fntype
= TREE_TYPE (fntype
);
3079 /* Convert the parameters to the types declared in the
3080 function prototype, or apply default promotions. */
3082 nargs
= convert_arguments (loc
, arg_loc
, TYPE_ARG_TYPES (fntype
), params
,
3083 origtypes
, function
, fundecl
);
3085 return error_mark_node
;
3087 /* Check that the function is called through a compatible prototype.
3088 If it is not, warn. */
3089 if (CONVERT_EXPR_P (function
)
3090 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
3091 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
3092 && !comptypes (fntype
, TREE_TYPE (tem
)))
3094 tree return_type
= TREE_TYPE (fntype
);
3096 /* This situation leads to run-time undefined behavior. We can't,
3097 therefore, simply error unless we can prove that all possible
3098 executions of the program must execute the code. */
3099 warning_at (loc
, 0, "function called through a non-compatible type");
3101 if (VOID_TYPE_P (return_type
)
3102 && TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
)
3104 "function with qualified void return type called");
3107 argarray
= vec_safe_address (params
);
3109 /* Check that arguments to builtin functions match the expectations. */
3111 && DECL_BUILT_IN (fundecl
)
3112 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
3113 && !check_builtin_function_arguments (loc
, arg_loc
, fundecl
, nargs
,
3115 return error_mark_node
;
3117 /* Check that the arguments to the function are valid. */
3118 bool warned_p
= check_function_arguments (loc
, fundecl
, fntype
,
3121 if (name
!= NULL_TREE
3122 && !strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10))
3124 if (require_constant_value
)
3126 = fold_build_call_array_initializer_loc (loc
, TREE_TYPE (fntype
),
3127 function
, nargs
, argarray
);
3129 result
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
3130 function
, nargs
, argarray
);
3131 if (TREE_CODE (result
) == NOP_EXPR
3132 && TREE_CODE (TREE_OPERAND (result
, 0)) == INTEGER_CST
)
3133 STRIP_TYPE_NOPS (result
);
3136 result
= build_call_array_loc (loc
, TREE_TYPE (fntype
),
3137 function
, nargs
, argarray
);
3138 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3140 if (warned_p
&& TREE_CODE (result
) == CALL_EXPR
)
3141 TREE_NO_WARNING (result
) = 1;
3143 /* In this improbable scenario, a nested function returns a VM type.
3144 Create a TARGET_EXPR so that the call always has a LHS, much as
3145 what the C++ FE does for functions returning non-PODs. */
3146 if (variably_modified_type_p (TREE_TYPE (fntype
), NULL_TREE
))
3148 tree tmp
= create_tmp_var_raw (TREE_TYPE (fntype
));
3149 result
= build4 (TARGET_EXPR
, TREE_TYPE (fntype
), tmp
, result
,
3150 NULL_TREE
, NULL_TREE
);
3153 if (VOID_TYPE_P (TREE_TYPE (result
)))
3155 if (TYPE_QUALS (TREE_TYPE (result
)) != TYPE_UNQUALIFIED
)
3157 "function with qualified void return type called");
3160 return require_complete_type (loc
, result
);
3163 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3166 c_build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
3167 tree function
, vec
<tree
, va_gc
> *params
,
3168 vec
<tree
, va_gc
> *origtypes
)
3170 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3171 STRIP_TYPE_NOPS (function
);
3173 /* Convert anything with function type to a pointer-to-function. */
3174 if (TREE_CODE (function
) == FUNCTION_DECL
)
3176 /* Implement type-directed function overloading for builtins.
3177 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3178 handle all the type checking. The result is a complete expression
3179 that implements this function call. */
3180 tree tem
= resolve_overloaded_builtin (loc
, function
, params
);
3184 return build_function_call_vec (loc
, arg_loc
, function
, params
, origtypes
);
3187 /* Convert the argument expressions in the vector VALUES
3188 to the types in the list TYPELIST.
3190 If TYPELIST is exhausted, or when an element has NULL as its type,
3191 perform the default conversions.
3193 ORIGTYPES is the original types of the expressions in VALUES. This
3194 holds the type of enum values which have been converted to integral
3195 types. It may be NULL.
3197 FUNCTION is a tree for the called function. It is used only for
3198 error messages, where it is formatted with %qE.
3200 This is also where warnings about wrong number of args are generated.
3202 ARG_LOC are locations of function arguments (if any).
3204 Returns the actual number of arguments processed (which may be less
3205 than the length of VALUES in some error situations), or -1 on
3209 convert_arguments (location_t loc
, vec
<location_t
> arg_loc
, tree typelist
,
3210 vec
<tree
, va_gc
> *values
, vec
<tree
, va_gc
> *origtypes
,
3211 tree function
, tree fundecl
)
3214 unsigned int parmnum
;
3215 bool error_args
= false;
3216 const bool type_generic
= fundecl
3217 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl
)));
3218 bool type_generic_remove_excess_precision
= false;
3219 bool type_generic_overflow_p
= false;
3222 /* Change pointer to function to the function itself for
3224 if (TREE_CODE (function
) == ADDR_EXPR
3225 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
3226 function
= TREE_OPERAND (function
, 0);
3228 /* Handle an ObjC selector specially for diagnostics. */
3229 selector
= objc_message_selector ();
3231 /* For type-generic built-in functions, determine whether excess
3232 precision should be removed (classification) or not
3235 && DECL_BUILT_IN (fundecl
)
3236 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
)
3238 switch (DECL_FUNCTION_CODE (fundecl
))
3240 case BUILT_IN_ISFINITE
:
3241 case BUILT_IN_ISINF
:
3242 case BUILT_IN_ISINF_SIGN
:
3243 case BUILT_IN_ISNAN
:
3244 case BUILT_IN_ISNORMAL
:
3245 case BUILT_IN_FPCLASSIFY
:
3246 type_generic_remove_excess_precision
= true;
3249 case BUILT_IN_ADD_OVERFLOW_P
:
3250 case BUILT_IN_SUB_OVERFLOW_P
:
3251 case BUILT_IN_MUL_OVERFLOW_P
:
3252 /* The last argument of these type-generic builtins
3253 should not be promoted. */
3254 type_generic_overflow_p
= true;
3261 if (flag_cilkplus
&& fundecl
&& is_cilkplus_reduce_builtin (fundecl
))
3262 return vec_safe_length (values
);
3264 /* Scan the given expressions and types, producing individual
3265 converted arguments. */
3267 for (typetail
= typelist
, parmnum
= 0;
3268 values
&& values
->iterate (parmnum
, &val
);
3271 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3272 tree valtype
= TREE_TYPE (val
);
3273 tree rname
= function
;
3274 int argnum
= parmnum
+ 1;
3275 const char *invalid_func_diag
;
3276 bool excess_precision
= false;
3279 /* Some __atomic_* builtins have additional hidden argument at
3282 = !arg_loc
.is_empty () && values
->length () == arg_loc
.length ()
3283 ? expansion_point_location_if_in_system_header (arg_loc
[parmnum
])
3286 if (type
== void_type_node
)
3289 error_at (loc
, "too many arguments to method %qE", selector
);
3291 error_at (loc
, "too many arguments to function %qE", function
);
3292 inform_declaration (fundecl
);
3293 return error_args
? -1 : (int) parmnum
;
3296 if (selector
&& argnum
> 2)
3302 npc
= null_pointer_constant_p (val
);
3304 /* If there is excess precision and a prototype, convert once to
3305 the required type rather than converting via the semantic
3306 type. Likewise without a prototype a float value represented
3307 as long double should be converted once to double. But for
3308 type-generic classification functions excess precision must
3310 if (TREE_CODE (val
) == EXCESS_PRECISION_EXPR
3311 && (type
|| !type_generic
|| !type_generic_remove_excess_precision
))
3313 val
= TREE_OPERAND (val
, 0);
3314 excess_precision
= true;
3316 val
= c_fully_fold (val
, false, NULL
);
3317 STRIP_TYPE_NOPS (val
);
3319 val
= require_complete_type (ploc
, val
);
3321 /* Some floating-point arguments must be promoted to double when
3322 no type is specified by a prototype. This applies to
3323 arguments of type float, and to architecture-specific types
3324 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3325 bool promote_float_arg
= false;
3326 if (type
== NULL_TREE
3327 && TREE_CODE (valtype
) == REAL_TYPE
3328 && (TYPE_PRECISION (valtype
)
3329 <= TYPE_PRECISION (double_type_node
))
3330 && TYPE_MAIN_VARIANT (valtype
) != double_type_node
3331 && TYPE_MAIN_VARIANT (valtype
) != long_double_type_node
3332 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype
)))
3334 /* Promote this argument, unless it has a _FloatN or
3336 promote_float_arg
= true;
3337 for (int i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
3338 if (TYPE_MAIN_VARIANT (valtype
) == FLOATN_NX_TYPE_NODE (i
))
3340 promote_float_arg
= false;
3345 if (type
!= NULL_TREE
)
3347 /* Formal parm type is specified by a function prototype. */
3349 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3351 error_at (ploc
, "type of formal parameter %d is incomplete",
3359 /* Optionally warn about conversions that
3360 differ from the default conversions. */
3361 if (warn_traditional_conversion
|| warn_traditional
)
3363 unsigned int formal_prec
= TYPE_PRECISION (type
);
3365 if (INTEGRAL_TYPE_P (type
)
3366 && TREE_CODE (valtype
) == REAL_TYPE
)
3367 warning_at (ploc
, OPT_Wtraditional_conversion
,
3368 "passing argument %d of %qE as integer rather "
3369 "than floating due to prototype",
3371 if (INTEGRAL_TYPE_P (type
)
3372 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3373 warning_at (ploc
, OPT_Wtraditional_conversion
,
3374 "passing argument %d of %qE as integer rather "
3375 "than complex due to prototype",
3377 else if (TREE_CODE (type
) == COMPLEX_TYPE
3378 && TREE_CODE (valtype
) == REAL_TYPE
)
3379 warning_at (ploc
, OPT_Wtraditional_conversion
,
3380 "passing argument %d of %qE as complex rather "
3381 "than floating due to prototype",
3383 else if (TREE_CODE (type
) == REAL_TYPE
3384 && INTEGRAL_TYPE_P (valtype
))
3385 warning_at (ploc
, OPT_Wtraditional_conversion
,
3386 "passing argument %d of %qE as floating rather "
3387 "than integer due to prototype",
3389 else if (TREE_CODE (type
) == COMPLEX_TYPE
3390 && INTEGRAL_TYPE_P (valtype
))
3391 warning_at (ploc
, OPT_Wtraditional_conversion
,
3392 "passing argument %d of %qE as complex rather "
3393 "than integer due to prototype",
3395 else if (TREE_CODE (type
) == REAL_TYPE
3396 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3397 warning_at (ploc
, OPT_Wtraditional_conversion
,
3398 "passing argument %d of %qE as floating rather "
3399 "than complex due to prototype",
3401 /* ??? At some point, messages should be written about
3402 conversions between complex types, but that's too messy
3404 else if (TREE_CODE (type
) == REAL_TYPE
3405 && TREE_CODE (valtype
) == REAL_TYPE
)
3407 /* Warn if any argument is passed as `float',
3408 since without a prototype it would be `double'. */
3409 if (formal_prec
== TYPE_PRECISION (float_type_node
)
3410 && type
!= dfloat32_type_node
)
3411 warning_at (ploc
, 0,
3412 "passing argument %d of %qE as %<float%> "
3413 "rather than %<double%> due to prototype",
3416 /* Warn if mismatch between argument and prototype
3417 for decimal float types. Warn of conversions with
3418 binary float types and of precision narrowing due to
3420 else if (type
!= valtype
3421 && (type
== dfloat32_type_node
3422 || type
== dfloat64_type_node
3423 || type
== dfloat128_type_node
3424 || valtype
== dfloat32_type_node
3425 || valtype
== dfloat64_type_node
3426 || valtype
== dfloat128_type_node
)
3428 <= TYPE_PRECISION (valtype
)
3429 || (type
== dfloat128_type_node
3431 != dfloat64_type_node
3433 != dfloat32_type_node
)))
3434 || (type
== dfloat64_type_node
3436 != dfloat32_type_node
))))
3437 warning_at (ploc
, 0,
3438 "passing argument %d of %qE as %qT "
3439 "rather than %qT due to prototype",
3440 argnum
, rname
, type
, valtype
);
3443 /* Detect integer changing in width or signedness.
3444 These warnings are only activated with
3445 -Wtraditional-conversion, not with -Wtraditional. */
3446 else if (warn_traditional_conversion
3447 && INTEGRAL_TYPE_P (type
)
3448 && INTEGRAL_TYPE_P (valtype
))
3450 tree would_have_been
= default_conversion (val
);
3451 tree type1
= TREE_TYPE (would_have_been
);
3453 if (val
== error_mark_node
)
3454 /* VAL could have been of incomplete type. */;
3455 else if (TREE_CODE (type
) == ENUMERAL_TYPE
3456 && (TYPE_MAIN_VARIANT (type
)
3457 == TYPE_MAIN_VARIANT (valtype
)))
3458 /* No warning if function asks for enum
3459 and the actual arg is that enum type. */
3461 else if (formal_prec
!= TYPE_PRECISION (type1
))
3462 warning_at (ploc
, OPT_Wtraditional_conversion
,
3463 "passing argument %d of %qE "
3464 "with different width due to prototype",
3466 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
3468 /* Don't complain if the formal parameter type
3469 is an enum, because we can't tell now whether
3470 the value was an enum--even the same enum. */
3471 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3473 else if (TREE_CODE (val
) == INTEGER_CST
3474 && int_fits_type_p (val
, type
))
3475 /* Change in signedness doesn't matter
3476 if a constant value is unaffected. */
3478 /* If the value is extended from a narrower
3479 unsigned type, it doesn't matter whether we
3480 pass it as signed or unsigned; the value
3481 certainly is the same either way. */
3482 else if (TYPE_PRECISION (valtype
) < TYPE_PRECISION (type
)
3483 && TYPE_UNSIGNED (valtype
))
3485 else if (TYPE_UNSIGNED (type
))
3486 warning_at (ploc
, OPT_Wtraditional_conversion
,
3487 "passing argument %d of %qE "
3488 "as unsigned due to prototype",
3491 warning_at (ploc
, OPT_Wtraditional_conversion
,
3492 "passing argument %d of %qE "
3493 "as signed due to prototype",
3498 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3499 sake of better warnings from convert_and_check. */
3500 if (excess_precision
)
3501 val
= build1 (EXCESS_PRECISION_EXPR
, valtype
, val
);
3502 origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3503 parmval
= convert_for_assignment (loc
, ploc
, type
,
3504 val
, origtype
, ic_argpass
,
3505 npc
, fundecl
, function
,
3508 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
3509 && INTEGRAL_TYPE_P (type
)
3510 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3511 parmval
= default_conversion (parmval
);
3514 else if (promote_float_arg
)
3520 /* Convert `float' to `double'. */
3521 if (warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
3522 warning_at (ploc
, OPT_Wdouble_promotion
,
3523 "implicit conversion from %qT to %qT when passing "
3524 "argument to function",
3525 valtype
, double_type_node
);
3526 parmval
= convert (double_type_node
, val
);
3529 else if ((excess_precision
&& !type_generic
)
3530 || (type_generic_overflow_p
&& parmnum
== 2))
3531 /* A "double" argument with excess precision being passed
3532 without a prototype or in variable arguments.
3533 The last argument of __builtin_*_overflow_p should not be
3535 parmval
= convert (valtype
, val
);
3536 else if ((invalid_func_diag
=
3537 targetm
.calls
.invalid_arg_for_unprototyped_fn (typelist
, fundecl
, val
)))
3539 error (invalid_func_diag
);
3542 else if (TREE_CODE (val
) == ADDR_EXPR
&& reject_gcc_builtin (val
))
3547 /* Convert `short' and `char' to full-size `int'. */
3548 parmval
= default_conversion (val
);
3550 (*values
)[parmnum
] = parmval
;
3551 if (parmval
== error_mark_node
)
3555 typetail
= TREE_CHAIN (typetail
);
3558 gcc_assert (parmnum
== vec_safe_length (values
));
3560 if (typetail
!= NULL_TREE
&& TREE_VALUE (typetail
) != void_type_node
)
3562 error_at (loc
, "too few arguments to function %qE", function
);
3563 inform_declaration (fundecl
);
3567 return error_args
? -1 : (int) parmnum
;
3570 /* This is the entry point used by the parser to build unary operators
3571 in the input. CODE, a tree_code, specifies the unary operator, and
3572 ARG is the operand. For unary plus, the C parser currently uses
3573 CONVERT_EXPR for code.
3575 LOC is the location to use for the tree generated.
3579 parser_build_unary_op (location_t loc
, enum tree_code code
, struct c_expr arg
)
3581 struct c_expr result
;
3583 result
.original_code
= code
;
3584 result
.original_type
= NULL
;
3586 if (reject_gcc_builtin (arg
.value
))
3588 result
.value
= error_mark_node
;
3592 result
.value
= build_unary_op (loc
, code
, arg
.value
, false);
3594 if (TREE_OVERFLOW_P (result
.value
) && !TREE_OVERFLOW_P (arg
.value
))
3595 overflow_warning (loc
, result
.value
, arg
.value
);
3598 /* We are typically called when parsing a prefix token at LOC acting on
3599 ARG. Reflect this by updating the source range of the result to
3600 start at LOC and end at the end of ARG. */
3601 set_c_expr_source_range (&result
,
3602 loc
, arg
.get_finish ());
3607 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3610 char_type_p (tree type
)
3612 return (type
== char_type_node
3613 || type
== unsigned_char_type_node
3614 || type
== signed_char_type_node
3615 || type
== char16_type_node
3616 || type
== char32_type_node
);
3619 /* This is the entry point used by the parser to build binary operators
3620 in the input. CODE, a tree_code, specifies the binary operator, and
3621 ARG1 and ARG2 are the operands. In addition to constructing the
3622 expression, we check for operands that were written with other binary
3623 operators in a way that is likely to confuse the user.
3625 LOCATION is the location of the binary operator. */
3628 parser_build_binary_op (location_t location
, enum tree_code code
,
3629 struct c_expr arg1
, struct c_expr arg2
)
3631 struct c_expr result
;
3633 enum tree_code code1
= arg1
.original_code
;
3634 enum tree_code code2
= arg2
.original_code
;
3635 tree type1
= (arg1
.original_type
3636 ? arg1
.original_type
3637 : TREE_TYPE (arg1
.value
));
3638 tree type2
= (arg2
.original_type
3639 ? arg2
.original_type
3640 : TREE_TYPE (arg2
.value
));
3642 result
.value
= build_binary_op (location
, code
,
3643 arg1
.value
, arg2
.value
, 1);
3644 result
.original_code
= code
;
3645 result
.original_type
= NULL
;
3647 if (TREE_CODE (result
.value
) == ERROR_MARK
)
3649 set_c_expr_source_range (&result
,
3651 arg2
.get_finish ());
3655 if (location
!= UNKNOWN_LOCATION
)
3656 protected_set_expr_location (result
.value
, location
);
3658 set_c_expr_source_range (&result
,
3660 arg2
.get_finish ());
3662 /* Check for cases such as x+y<<z which users are likely
3664 if (warn_parentheses
)
3665 warn_about_parentheses (location
, code
, code1
, arg1
.value
, code2
,
3668 if (warn_logical_op
)
3669 warn_logical_operator (location
, code
, TREE_TYPE (result
.value
),
3670 code1
, arg1
.value
, code2
, arg2
.value
);
3672 if (warn_tautological_compare
)
3674 tree lhs
= arg1
.value
;
3675 tree rhs
= arg2
.value
;
3676 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
3678 if (C_MAYBE_CONST_EXPR_PRE (lhs
) != NULL_TREE
3679 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs
)))
3682 lhs
= C_MAYBE_CONST_EXPR_EXPR (lhs
);
3684 if (TREE_CODE (rhs
) == C_MAYBE_CONST_EXPR
)
3686 if (C_MAYBE_CONST_EXPR_PRE (rhs
) != NULL_TREE
3687 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs
)))
3690 rhs
= C_MAYBE_CONST_EXPR_EXPR (rhs
);
3692 if (lhs
!= NULL_TREE
&& rhs
!= NULL_TREE
)
3693 warn_tautological_cmp (location
, code
, lhs
, rhs
);
3696 if (warn_logical_not_paren
3697 && TREE_CODE_CLASS (code
) == tcc_comparison
3698 && code1
== TRUTH_NOT_EXPR
3699 && code2
!= TRUTH_NOT_EXPR
3700 /* Avoid warning for !!x == y. */
3701 && (TREE_CODE (arg1
.value
) != NE_EXPR
3702 || !integer_zerop (TREE_OPERAND (arg1
.value
, 1))))
3704 /* Avoid warning for !b == y where b has _Bool type. */
3705 tree t
= integer_zero_node
;
3706 if (TREE_CODE (arg1
.value
) == EQ_EXPR
3707 && integer_zerop (TREE_OPERAND (arg1
.value
, 1))
3708 && TREE_TYPE (TREE_OPERAND (arg1
.value
, 0)) == integer_type_node
)
3710 t
= TREE_OPERAND (arg1
.value
, 0);
3713 if (TREE_TYPE (t
) != integer_type_node
)
3715 if (TREE_CODE (t
) == C_MAYBE_CONST_EXPR
)
3716 t
= C_MAYBE_CONST_EXPR_EXPR (t
);
3717 else if (CONVERT_EXPR_P (t
))
3718 t
= TREE_OPERAND (t
, 0);
3724 if (TREE_CODE (TREE_TYPE (t
)) != BOOLEAN_TYPE
)
3725 warn_logical_not_parentheses (location
, code
, arg1
.value
, arg2
.value
);
3728 /* Warn about comparisons against string literals, with the exception
3729 of testing for equality or inequality of a string literal with NULL. */
3730 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3732 if ((code1
== STRING_CST
3733 && !integer_zerop (tree_strip_nop_conversions (arg2
.value
)))
3734 || (code2
== STRING_CST
3735 && !integer_zerop (tree_strip_nop_conversions (arg1
.value
))))
3736 warning_at (location
, OPT_Waddress
,
3737 "comparison with string literal results in unspecified behavior");
3738 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3739 if (POINTER_TYPE_P (type1
)
3740 && null_pointer_constant_p (arg2
.value
)
3741 && char_type_p (type2
)
3742 && warning_at (location
, OPT_Wpointer_compare
,
3743 "comparison between pointer and zero character "
3745 inform (arg1
.get_start (), "did you mean to dereference the pointer?");
3746 else if (POINTER_TYPE_P (type2
)
3747 && null_pointer_constant_p (arg1
.value
)
3748 && char_type_p (type1
)
3749 && warning_at (location
, OPT_Wpointer_compare
,
3750 "comparison between pointer and zero character "
3752 inform (arg2
.get_start (), "did you mean to dereference the pointer?");
3754 else if (TREE_CODE_CLASS (code
) == tcc_comparison
3755 && (code1
== STRING_CST
|| code2
== STRING_CST
))
3756 warning_at (location
, OPT_Waddress
,
3757 "comparison with string literal results in unspecified behavior");
3759 if (TREE_OVERFLOW_P (result
.value
)
3760 && !TREE_OVERFLOW_P (arg1
.value
)
3761 && !TREE_OVERFLOW_P (arg2
.value
))
3762 overflow_warning (location
, result
.value
);
3764 /* Warn about comparisons of different enum types. */
3765 if (warn_enum_compare
3766 && TREE_CODE_CLASS (code
) == tcc_comparison
3767 && TREE_CODE (type1
) == ENUMERAL_TYPE
3768 && TREE_CODE (type2
) == ENUMERAL_TYPE
3769 && TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
3770 warning_at (location
, OPT_Wenum_compare
,
3771 "comparison between %qT and %qT",
3777 /* Return a tree for the difference of pointers OP0 and OP1.
3778 The resulting tree has type int. */
3781 pointer_diff (location_t loc
, tree op0
, tree op1
)
3783 tree restype
= ptrdiff_type_node
;
3784 tree result
, inttype
;
3786 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0
)));
3787 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1
)));
3788 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3789 tree orig_op1
= op1
;
3791 /* If the operands point into different address spaces, we need to
3792 explicitly convert them to pointers into the common address space
3793 before we can subtract the numerical address values. */
3796 addr_space_t as_common
;
3799 /* Determine the common superset address space. This is guaranteed
3800 to exist because the caller verified that comp_target_types
3801 returned non-zero. */
3802 if (!addr_space_superset (as0
, as1
, &as_common
))
3805 common_type
= common_pointer_type (TREE_TYPE (op0
), TREE_TYPE (op1
));
3806 op0
= convert (common_type
, op0
);
3807 op1
= convert (common_type
, op1
);
3810 /* Determine integer type to perform computations in. This will usually
3811 be the same as the result type (ptrdiff_t), but may need to be a wider
3812 type if pointers for the address space are wider than ptrdiff_t. */
3813 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
3814 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
3818 if (TREE_CODE (target_type
) == VOID_TYPE
)
3819 pedwarn (loc
, OPT_Wpointer_arith
,
3820 "pointer of type %<void *%> used in subtraction");
3821 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3822 pedwarn (loc
, OPT_Wpointer_arith
,
3823 "pointer to a function used in subtraction");
3825 /* First do the subtraction as integers;
3826 then drop through to build the divide operator.
3827 Do not do default conversions on the minus operator
3828 in case restype is a short type. */
3830 op0
= build_binary_op (loc
,
3831 MINUS_EXPR
, convert (inttype
, op0
),
3832 convert (inttype
, op1
), 0);
3833 /* This generates an error if op1 is pointer to incomplete type. */
3834 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
3835 error_at (loc
, "arithmetic on pointer to an incomplete type");
3837 op1
= c_size_in_bytes (target_type
);
3839 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1
)))
3840 error_at (loc
, "arithmetic on pointer to an empty aggregate");
3842 /* Divide by the size, in easiest possible way. */
3843 result
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, inttype
,
3844 op0
, convert (inttype
, op1
));
3846 /* Convert to final result type if necessary. */
3847 return convert (restype
, result
);
3850 /* Expand atomic compound assignments into an appropriate sequence as
3851 specified by the C11 standard section 6.5.16.2.
3857 This sequence is used for all types for which these operations are
3860 In addition, built-in versions of the 'fe' prefixed routines may
3861 need to be invoked for floating point (real, complex or vector) when
3862 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3872 __atomic_load (addr, &old, SEQ_CST);
3873 feholdexcept (&fenv);
3875 newval = old op val;
3876 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3879 feclearexcept (FE_ALL_EXCEPT);
3882 feupdateenv (&fenv);
3884 The compiler will issue the __atomic_fetch_* built-in when possible,
3885 otherwise it will generate the generic form of the atomic operations.
3886 This requires temp(s) and has their address taken. The atomic processing
3887 is smart enough to figure out when the size of an object can utilize
3888 a lock-free version, and convert the built-in call to the appropriate
3889 lock-free routine. The optimizers will then dispose of any temps that
3890 are no longer required, and lock-free implementations are utilized as
3891 long as there is target support for the required size.
3893 If the operator is NOP_EXPR, then this is a simple assignment, and
3894 an __atomic_store is issued to perform the assignment rather than
3897 /* Build an atomic assignment at LOC, expanding into the proper
3898 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3899 the result of the operation, unless RETURN_OLD_P, in which case
3900 return the old value of LHS (this is only for postincrement and
3904 build_atomic_assign (location_t loc
, tree lhs
, enum tree_code modifycode
,
3905 tree rhs
, bool return_old_p
)
3907 tree fndecl
, func_call
;
3908 vec
<tree
, va_gc
> *params
;
3909 tree val
, nonatomic_lhs_type
, nonatomic_rhs_type
, newval
, newval_addr
;
3912 tree stmt
, goto_stmt
;
3913 tree loop_label
, loop_decl
, done_label
, done_decl
;
3915 tree lhs_type
= TREE_TYPE (lhs
);
3916 tree lhs_addr
= build_unary_op (loc
, ADDR_EXPR
, lhs
, false);
3917 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
3918 tree rhs_type
= TREE_TYPE (rhs
);
3920 gcc_assert (TYPE_ATOMIC (lhs_type
));
3923 gcc_assert (modifycode
== PLUS_EXPR
|| modifycode
== MINUS_EXPR
);
3925 /* Allocate enough vector items for a compare_exchange. */
3926 vec_alloc (params
, 6);
3928 /* Create a compound statement to hold the sequence of statements
3930 compound_stmt
= c_begin_compound_stmt (false);
3932 /* Fold the RHS if it hasn't already been folded. */
3933 if (modifycode
!= NOP_EXPR
)
3934 rhs
= c_fully_fold (rhs
, false, NULL
);
3936 /* Remove the qualifiers for the rest of the expressions and create
3937 the VAL temp variable to hold the RHS. */
3938 nonatomic_lhs_type
= build_qualified_type (lhs_type
, TYPE_UNQUALIFIED
);
3939 nonatomic_rhs_type
= build_qualified_type (rhs_type
, TYPE_UNQUALIFIED
);
3940 val
= create_tmp_var_raw (nonatomic_rhs_type
);
3941 TREE_ADDRESSABLE (val
) = 1;
3942 TREE_NO_WARNING (val
) = 1;
3943 rhs
= build4 (TARGET_EXPR
, nonatomic_rhs_type
, val
, rhs
, NULL_TREE
,
3945 SET_EXPR_LOCATION (rhs
, loc
);
3948 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3950 if (modifycode
== NOP_EXPR
)
3952 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3953 rhs
= build_unary_op (loc
, ADDR_EXPR
, val
, false);
3954 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
3955 params
->quick_push (lhs_addr
);
3956 params
->quick_push (rhs
);
3957 params
->quick_push (seq_cst
);
3958 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3959 add_stmt (func_call
);
3961 /* Finish the compound statement. */
3962 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3964 /* VAL is the value which was stored, return a COMPOUND_STMT of
3965 the statement and that value. */
3966 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, val
);
3969 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3970 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3971 isn't applicable for such builtins. ??? Do we want to handle enums? */
3972 if ((TREE_CODE (lhs_type
) == INTEGER_TYPE
|| POINTER_TYPE_P (lhs_type
))
3973 && TREE_CODE (rhs_type
) == INTEGER_TYPE
)
3975 built_in_function fncode
;
3979 case POINTER_PLUS_EXPR
:
3980 fncode
= (return_old_p
3981 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3982 : BUILT_IN_ATOMIC_ADD_FETCH_N
);
3985 fncode
= (return_old_p
3986 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3987 : BUILT_IN_ATOMIC_SUB_FETCH_N
);
3990 fncode
= (return_old_p
3991 ? BUILT_IN_ATOMIC_FETCH_AND_N
3992 : BUILT_IN_ATOMIC_AND_FETCH_N
);
3995 fncode
= (return_old_p
3996 ? BUILT_IN_ATOMIC_FETCH_OR_N
3997 : BUILT_IN_ATOMIC_OR_FETCH_N
);
4000 fncode
= (return_old_p
4001 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4002 : BUILT_IN_ATOMIC_XOR_FETCH_N
);
4008 /* We can only use "_1" through "_16" variants of the atomic fetch
4010 unsigned HOST_WIDE_INT size
= tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type
));
4011 if (size
!= 1 && size
!= 2 && size
!= 4 && size
!= 8 && size
!= 16)
4014 /* If this is a pointer type, we need to multiply by the size of
4015 the pointer target type. */
4016 if (POINTER_TYPE_P (lhs_type
))
4018 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type
))
4019 /* ??? This would introduce -Wdiscarded-qualifiers
4020 warning: __atomic_fetch_* expect volatile void *
4021 type as the first argument. (Assignments between
4022 atomic and non-atomic objects are OK.) */
4023 || TYPE_RESTRICT (lhs_type
))
4025 tree sz
= TYPE_SIZE_UNIT (TREE_TYPE (lhs_type
));
4026 rhs
= fold_build2_loc (loc
, MULT_EXPR
, ptrdiff_type_node
,
4027 convert (ptrdiff_type_node
, rhs
),
4028 convert (ptrdiff_type_node
, sz
));
4031 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4032 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4033 fndecl
= builtin_decl_explicit (fncode
);
4034 params
->quick_push (lhs_addr
);
4035 params
->quick_push (rhs
);
4036 params
->quick_push (seq_cst
);
4037 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4039 newval
= create_tmp_var_raw (nonatomic_lhs_type
);
4040 TREE_ADDRESSABLE (newval
) = 1;
4041 TREE_NO_WARNING (newval
) = 1;
4042 rhs
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, newval
, func_call
,
4043 NULL_TREE
, NULL_TREE
);
4044 SET_EXPR_LOCATION (rhs
, loc
);
4047 /* Finish the compound statement. */
4048 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
4050 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4051 the statement and that value. */
4052 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, newval
);
4056 /* Create the variables and labels required for the op= form. */
4057 old
= create_tmp_var_raw (nonatomic_lhs_type
);
4058 old_addr
= build_unary_op (loc
, ADDR_EXPR
, old
, false);
4059 TREE_ADDRESSABLE (old
) = 1;
4060 TREE_NO_WARNING (old
) = 1;
4062 newval
= create_tmp_var_raw (nonatomic_lhs_type
);
4063 newval_addr
= build_unary_op (loc
, ADDR_EXPR
, newval
, false);
4064 TREE_ADDRESSABLE (newval
) = 1;
4065 TREE_NO_WARNING (newval
) = 1;
4067 loop_decl
= create_artificial_label (loc
);
4068 loop_label
= build1 (LABEL_EXPR
, void_type_node
, loop_decl
);
4070 done_decl
= create_artificial_label (loc
);
4071 done_label
= build1 (LABEL_EXPR
, void_type_node
, done_decl
);
4073 /* __atomic_load (addr, &old, SEQ_CST). */
4074 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
4075 params
->quick_push (lhs_addr
);
4076 params
->quick_push (old_addr
);
4077 params
->quick_push (seq_cst
);
4078 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4079 old
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, old
, func_call
, NULL_TREE
,
4082 params
->truncate (0);
4084 /* Create the expressions for floating-point environment
4085 manipulation, if required. */
4086 bool need_fenv
= (flag_trapping_math
4087 && (FLOAT_TYPE_P (lhs_type
) || FLOAT_TYPE_P (rhs_type
)));
4088 tree hold_call
= NULL_TREE
, clear_call
= NULL_TREE
, update_call
= NULL_TREE
;
4090 targetm
.atomic_assign_expand_fenv (&hold_call
, &clear_call
, &update_call
);
4093 add_stmt (hold_call
);
4096 add_stmt (loop_label
);
4098 /* newval = old + val; */
4099 rhs
= build_binary_op (loc
, modifycode
, old
, val
, 1);
4100 rhs
= c_fully_fold (rhs
, false, NULL
);
4101 rhs
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, nonatomic_lhs_type
,
4102 rhs
, NULL_TREE
, ic_assign
, false, NULL_TREE
,
4104 if (rhs
!= error_mark_node
)
4106 rhs
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, newval
, rhs
, NULL_TREE
,
4108 SET_EXPR_LOCATION (rhs
, loc
);
4112 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4114 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE
);
4115 params
->quick_push (lhs_addr
);
4116 params
->quick_push (old_addr
);
4117 params
->quick_push (newval_addr
);
4118 params
->quick_push (integer_zero_node
);
4119 params
->quick_push (seq_cst
);
4120 params
->quick_push (seq_cst
);
4121 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4123 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, done_decl
);
4124 SET_EXPR_LOCATION (goto_stmt
, loc
);
4126 stmt
= build3 (COND_EXPR
, void_type_node
, func_call
, goto_stmt
, NULL_TREE
);
4127 SET_EXPR_LOCATION (stmt
, loc
);
4131 add_stmt (clear_call
);
4134 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, loop_decl
);
4135 SET_EXPR_LOCATION (goto_stmt
, loc
);
4136 add_stmt (goto_stmt
);
4139 add_stmt (done_label
);
4142 add_stmt (update_call
);
4144 /* Finish the compound statement. */
4145 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
4147 /* NEWVAL is the value that was successfully stored, return a
4148 COMPOUND_EXPR of the statement and the appropriate value. */
4149 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
,
4150 return_old_p
? old
: newval
);
4153 /* Construct and perhaps optimize a tree representation
4154 for a unary operation. CODE, a tree_code, specifies the operation
4155 and XARG is the operand.
4156 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4157 promotions (such as from short to int).
4158 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4159 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4162 LOCATION is the location of the operator. */
4165 build_unary_op (location_t location
, enum tree_code code
, tree xarg
,
4168 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4170 tree argtype
= NULL_TREE
;
4171 enum tree_code typecode
;
4173 tree ret
= error_mark_node
;
4174 tree eptype
= NULL_TREE
;
4175 const char *invalid_op_diag
;
4178 int_operands
= EXPR_INT_CONST_OPERANDS (xarg
);
4180 arg
= remove_c_maybe_const_expr (arg
);
4182 if (code
!= ADDR_EXPR
)
4183 arg
= require_complete_type (location
, arg
);
4185 typecode
= TREE_CODE (TREE_TYPE (arg
));
4186 if (typecode
== ERROR_MARK
)
4187 return error_mark_node
;
4188 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
4189 typecode
= INTEGER_TYPE
;
4191 if ((invalid_op_diag
4192 = targetm
.invalid_unary_op (code
, TREE_TYPE (xarg
))))
4194 error_at (location
, invalid_op_diag
);
4195 return error_mark_node
;
4198 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
4200 eptype
= TREE_TYPE (arg
);
4201 arg
= TREE_OPERAND (arg
, 0);
4207 /* This is used for unary plus, because a CONVERT_EXPR
4208 is enough to prevent anybody from looking inside for
4209 associativity, but won't generate any code. */
4210 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4211 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
4212 || typecode
== VECTOR_TYPE
))
4214 error_at (location
, "wrong type argument to unary plus");
4215 return error_mark_node
;
4217 else if (!noconvert
)
4218 arg
= default_conversion (arg
);
4219 arg
= non_lvalue_loc (location
, arg
);
4223 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4224 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
4225 || typecode
== VECTOR_TYPE
))
4227 error_at (location
, "wrong type argument to unary minus");
4228 return error_mark_node
;
4230 else if (!noconvert
)
4231 arg
= default_conversion (arg
);
4235 /* ~ works on integer types and non float vectors. */
4236 if (typecode
== INTEGER_TYPE
4237 || (typecode
== VECTOR_TYPE
4238 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg
))))
4242 /* Warn if the expression has boolean value. */
4243 while (TREE_CODE (e
) == COMPOUND_EXPR
)
4244 e
= TREE_OPERAND (e
, 1);
4246 if ((TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
4247 || truth_value_p (TREE_CODE (e
)))
4248 && warning_at (location
, OPT_Wbool_operation
,
4249 "%<~%> on a boolean expression"))
4251 gcc_rich_location
richloc (location
);
4252 richloc
.add_fixit_insert_before (location
, "!");
4253 inform_at_rich_loc (&richloc
, "did you mean to use logical "
4257 arg
= default_conversion (arg
);
4259 else if (typecode
== COMPLEX_TYPE
)
4262 pedwarn (location
, OPT_Wpedantic
,
4263 "ISO C does not support %<~%> for complex conjugation");
4265 arg
= default_conversion (arg
);
4269 error_at (location
, "wrong type argument to bit-complement");
4270 return error_mark_node
;
4275 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
4277 error_at (location
, "wrong type argument to abs");
4278 return error_mark_node
;
4280 else if (!noconvert
)
4281 arg
= default_conversion (arg
);
4285 /* Conjugating a real value is a no-op, but allow it anyway. */
4286 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4287 || typecode
== COMPLEX_TYPE
))
4289 error_at (location
, "wrong type argument to conjugation");
4290 return error_mark_node
;
4292 else if (!noconvert
)
4293 arg
= default_conversion (arg
);
4296 case TRUTH_NOT_EXPR
:
4297 if (typecode
!= INTEGER_TYPE
&& typecode
!= FIXED_POINT_TYPE
4298 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
4299 && typecode
!= COMPLEX_TYPE
)
4302 "wrong type argument to unary exclamation mark");
4303 return error_mark_node
;
4307 arg
= c_objc_common_truthvalue_conversion (location
, xarg
);
4308 arg
= remove_c_maybe_const_expr (arg
);
4311 arg
= c_objc_common_truthvalue_conversion (location
, arg
);
4312 ret
= invert_truthvalue_loc (location
, arg
);
4313 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4314 if (EXPR_P (ret
) && EXPR_HAS_LOCATION (ret
))
4315 location
= EXPR_LOCATION (ret
);
4316 goto return_build_unary_op
;
4320 ret
= build_real_imag_expr (location
, code
, arg
);
4321 if (ret
== error_mark_node
)
4322 return error_mark_node
;
4323 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
4324 eptype
= TREE_TYPE (eptype
);
4325 goto return_build_unary_op
;
4327 case PREINCREMENT_EXPR
:
4328 case POSTINCREMENT_EXPR
:
4329 case PREDECREMENT_EXPR
:
4330 case POSTDECREMENT_EXPR
:
4332 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4334 tree inner
= build_unary_op (location
, code
,
4335 C_MAYBE_CONST_EXPR_EXPR (arg
),
4337 if (inner
== error_mark_node
)
4338 return error_mark_node
;
4339 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4340 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4341 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4342 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = 1;
4343 goto return_build_unary_op
;
4346 /* Complain about anything that is not a true lvalue. In
4347 Objective-C, skip this check for property_refs. */
4348 if (!objc_is_property_ref (arg
)
4349 && !lvalue_or_else (location
,
4350 arg
, ((code
== PREINCREMENT_EXPR
4351 || code
== POSTINCREMENT_EXPR
)
4354 return error_mark_node
;
4356 if (warn_cxx_compat
&& TREE_CODE (TREE_TYPE (arg
)) == ENUMERAL_TYPE
)
4358 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4359 warning_at (location
, OPT_Wc___compat
,
4360 "increment of enumeration value is invalid in C++");
4362 warning_at (location
, OPT_Wc___compat
,
4363 "decrement of enumeration value is invalid in C++");
4366 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4368 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4369 warning_at (location
, OPT_Wbool_operation
,
4370 "increment of a boolean expression");
4372 warning_at (location
, OPT_Wbool_operation
,
4373 "decrement of a boolean expression");
4376 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4377 arg
= c_fully_fold (arg
, false, NULL
);
4380 atomic_op
= really_atomic_lvalue (arg
);
4382 /* Increment or decrement the real part of the value,
4383 and don't change the imaginary part. */
4384 if (typecode
== COMPLEX_TYPE
)
4388 pedwarn (location
, OPT_Wpedantic
,
4389 "ISO C does not support %<++%> and %<--%> on complex types");
4393 arg
= stabilize_reference (arg
);
4394 real
= build_unary_op (EXPR_LOCATION (arg
), REALPART_EXPR
, arg
,
4396 imag
= build_unary_op (EXPR_LOCATION (arg
), IMAGPART_EXPR
, arg
,
4398 real
= build_unary_op (EXPR_LOCATION (arg
), code
, real
, true);
4399 if (real
== error_mark_node
|| imag
== error_mark_node
)
4400 return error_mark_node
;
4401 ret
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
4403 goto return_build_unary_op
;
4407 /* Report invalid types. */
4409 if (typecode
!= POINTER_TYPE
&& typecode
!= FIXED_POINT_TYPE
4410 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
4411 && typecode
!= COMPLEX_TYPE
&& typecode
!= VECTOR_TYPE
)
4413 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4414 error_at (location
, "wrong type argument to increment");
4416 error_at (location
, "wrong type argument to decrement");
4418 return error_mark_node
;
4424 argtype
= TREE_TYPE (arg
);
4426 /* Compute the increment. */
4428 if (typecode
== POINTER_TYPE
)
4430 /* If pointer target is an incomplete type,
4431 we just cannot know how to do the arithmetic. */
4432 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype
)))
4434 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4436 "increment of pointer to an incomplete type %qT",
4437 TREE_TYPE (argtype
));
4440 "decrement of pointer to an incomplete type %qT",
4441 TREE_TYPE (argtype
));
4443 else if (TREE_CODE (TREE_TYPE (argtype
)) == FUNCTION_TYPE
4444 || TREE_CODE (TREE_TYPE (argtype
)) == VOID_TYPE
)
4446 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4447 pedwarn (location
, OPT_Wpointer_arith
,
4448 "wrong type argument to increment");
4450 pedwarn (location
, OPT_Wpointer_arith
,
4451 "wrong type argument to decrement");
4454 inc
= c_size_in_bytes (TREE_TYPE (argtype
));
4455 inc
= convert_to_ptrofftype_loc (location
, inc
);
4457 else if (FRACT_MODE_P (TYPE_MODE (argtype
)))
4459 /* For signed fract types, we invert ++ to -- or
4460 -- to ++, and change inc from 1 to -1, because
4461 it is not possible to represent 1 in signed fract constants.
4462 For unsigned fract types, the result always overflows and
4463 we get an undefined (original) or the maximum value. */
4464 if (code
== PREINCREMENT_EXPR
)
4465 code
= PREDECREMENT_EXPR
;
4466 else if (code
== PREDECREMENT_EXPR
)
4467 code
= PREINCREMENT_EXPR
;
4468 else if (code
== POSTINCREMENT_EXPR
)
4469 code
= POSTDECREMENT_EXPR
;
4470 else /* code == POSTDECREMENT_EXPR */
4471 code
= POSTINCREMENT_EXPR
;
4473 inc
= integer_minus_one_node
;
4474 inc
= convert (argtype
, inc
);
4478 inc
= VECTOR_TYPE_P (argtype
)
4479 ? build_one_cst (argtype
)
4481 inc
= convert (argtype
, inc
);
4484 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4485 need to ask Objective-C to build the increment or decrement
4486 expression for it. */
4487 if (objc_is_property_ref (arg
))
4488 return objc_build_incr_expr_for_property_ref (location
, code
,
4491 /* Report a read-only lvalue. */
4492 if (TYPE_READONLY (argtype
))
4494 readonly_error (location
, arg
,
4495 ((code
== PREINCREMENT_EXPR
4496 || code
== POSTINCREMENT_EXPR
)
4497 ? lv_increment
: lv_decrement
));
4498 return error_mark_node
;
4500 else if (TREE_READONLY (arg
))
4501 readonly_warning (arg
,
4502 ((code
== PREINCREMENT_EXPR
4503 || code
== POSTINCREMENT_EXPR
)
4504 ? lv_increment
: lv_decrement
));
4506 /* If the argument is atomic, use the special code sequences for
4507 atomic compound assignment. */
4510 arg
= stabilize_reference (arg
);
4511 ret
= build_atomic_assign (location
, arg
,
4512 ((code
== PREINCREMENT_EXPR
4513 || code
== POSTINCREMENT_EXPR
)
4516 (FRACT_MODE_P (TYPE_MODE (argtype
))
4518 : integer_one_node
),
4519 (code
== POSTINCREMENT_EXPR
4520 || code
== POSTDECREMENT_EXPR
));
4521 goto return_build_unary_op
;
4524 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4525 val
= boolean_increment (code
, arg
);
4527 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
4528 TREE_SIDE_EFFECTS (val
) = 1;
4529 if (TREE_CODE (val
) != code
)
4530 TREE_NO_WARNING (val
) = 1;
4532 goto return_build_unary_op
;
4536 /* Note that this operation never does default_conversion. */
4538 /* The operand of unary '&' must be an lvalue (which excludes
4539 expressions of type void), or, in C99, the result of a [] or
4540 unary '*' operator. */
4541 if (VOID_TYPE_P (TREE_TYPE (arg
))
4542 && TYPE_QUALS (TREE_TYPE (arg
)) == TYPE_UNQUALIFIED
4543 && (!INDIRECT_REF_P (arg
) || !flag_isoc99
))
4544 pedwarn (location
, 0, "taking address of expression of type %<void%>");
4546 /* Let &* cancel out to simplify resulting code. */
4547 if (INDIRECT_REF_P (arg
))
4549 /* Don't let this be an lvalue. */
4550 if (lvalue_p (TREE_OPERAND (arg
, 0)))
4551 return non_lvalue_loc (location
, TREE_OPERAND (arg
, 0));
4552 ret
= TREE_OPERAND (arg
, 0);
4553 goto return_build_unary_op
;
4556 /* Anything not already handled and not a true memory reference
4557 or a non-lvalue array is an error. */
4558 if (typecode
!= FUNCTION_TYPE
&& !noconvert
4559 && !lvalue_or_else (location
, arg
, lv_addressof
))
4560 return error_mark_node
;
4562 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4564 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4566 tree inner
= build_unary_op (location
, code
,
4567 C_MAYBE_CONST_EXPR_EXPR (arg
),
4569 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4570 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4571 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4572 C_MAYBE_CONST_EXPR_NON_CONST (ret
)
4573 = C_MAYBE_CONST_EXPR_NON_CONST (arg
);
4574 goto return_build_unary_op
;
4577 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4578 argtype
= TREE_TYPE (arg
);
4580 /* If the lvalue is const or volatile, merge that into the type
4581 to which the address will point. This is only needed
4582 for function types. */
4583 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
4584 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4585 && TREE_CODE (argtype
) == FUNCTION_TYPE
)
4587 int orig_quals
= TYPE_QUALS (strip_array_types (argtype
));
4588 int quals
= orig_quals
;
4590 if (TREE_READONLY (arg
))
4591 quals
|= TYPE_QUAL_CONST
;
4592 if (TREE_THIS_VOLATILE (arg
))
4593 quals
|= TYPE_QUAL_VOLATILE
;
4595 argtype
= c_build_qualified_type (argtype
, quals
);
4598 switch (TREE_CODE (arg
))
4601 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)))
4603 error_at (location
, "cannot take address of bit-field %qD",
4604 TREE_OPERAND (arg
, 1));
4605 return error_mark_node
;
4611 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg
, 0))))
4613 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg
))
4614 && !VECTOR_TYPE_P (TREE_TYPE (arg
)))
4616 error_at (location
, "cannot take address of scalar with "
4617 "reverse storage order");
4618 return error_mark_node
;
4621 if (TREE_CODE (TREE_TYPE (arg
)) == ARRAY_TYPE
4622 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg
)))
4623 warning_at (location
, OPT_Wscalar_storage_order
,
4624 "address of array with reverse scalar storage "
4632 if (!c_mark_addressable (arg
))
4633 return error_mark_node
;
4635 gcc_assert (TREE_CODE (arg
) != COMPONENT_REF
4636 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)));
4638 argtype
= build_pointer_type (argtype
);
4640 /* ??? Cope with user tricks that amount to offsetof. Delete this
4641 when we have proper support for integer constant expressions. */
4642 val
= get_base_address (arg
);
4643 if (val
&& INDIRECT_REF_P (val
)
4644 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
4646 ret
= fold_convert_loc (location
, argtype
, fold_offsetof_1 (arg
));
4647 goto return_build_unary_op
;
4650 val
= build1 (ADDR_EXPR
, argtype
, arg
);
4653 goto return_build_unary_op
;
4659 if (argtype
== NULL_TREE
)
4660 argtype
= TREE_TYPE (arg
);
4661 if (TREE_CODE (arg
) == INTEGER_CST
)
4662 ret
= (require_constant_value
4663 ? fold_build1_initializer_loc (location
, code
, argtype
, arg
)
4664 : fold_build1_loc (location
, code
, argtype
, arg
));
4666 ret
= build1 (code
, argtype
, arg
);
4667 return_build_unary_op
:
4668 gcc_assert (ret
!= error_mark_node
);
4669 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
)
4670 && !(TREE_CODE (xarg
) == INTEGER_CST
&& !TREE_OVERFLOW (xarg
)))
4671 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
4672 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
)
4673 ret
= note_integer_operands (ret
);
4675 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4676 protected_set_expr_location (ret
, location
);
4680 /* Return nonzero if REF is an lvalue valid for this language.
4681 Lvalues can be assigned, unless their type has TYPE_READONLY.
4682 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4685 lvalue_p (const_tree ref
)
4687 const enum tree_code code
= TREE_CODE (ref
);
4694 return lvalue_p (TREE_OPERAND (ref
, 0));
4696 case C_MAYBE_CONST_EXPR
:
4697 return lvalue_p (TREE_OPERAND (ref
, 1));
4699 case COMPOUND_LITERAL_EXPR
:
4705 case ARRAY_NOTATION_REF
:
4710 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
4711 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
4714 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
4721 /* Give a warning for storing in something that is read-only in GCC
4722 terms but not const in ISO C terms. */
4725 readonly_warning (tree arg
, enum lvalue_use use
)
4730 warning (0, "assignment of read-only location %qE", arg
);
4733 warning (0, "increment of read-only location %qE", arg
);
4736 warning (0, "decrement of read-only location %qE", arg
);
4745 /* Return nonzero if REF is an lvalue valid for this language;
4746 otherwise, print an error message and return zero. USE says
4747 how the lvalue is being used and so selects the error message.
4748 LOCATION is the location at which any error should be reported. */
4751 lvalue_or_else (location_t loc
, const_tree ref
, enum lvalue_use use
)
4753 int win
= lvalue_p (ref
);
4756 lvalue_error (loc
, use
);
4761 /* Mark EXP saying that we need to be able to take the
4762 address of it; it should not be allocated in a register.
4763 Returns true if successful. ARRAY_REF_P is true if this
4764 is for ARRAY_REF construction - in that case we don't want
4765 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4766 it is fine to use ARRAY_REFs for vector subscripts on vector
4767 register variables. */
4770 c_mark_addressable (tree exp
, bool array_ref_p
)
4775 switch (TREE_CODE (x
))
4777 case VIEW_CONVERT_EXPR
:
4779 && TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
4780 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x
, 0))))
4788 x
= TREE_OPERAND (x
, 0);
4791 case COMPOUND_LITERAL_EXPR
:
4793 TREE_ADDRESSABLE (x
) = 1;
4800 if (C_DECL_REGISTER (x
)
4801 && DECL_NONLOCAL (x
))
4803 if (TREE_PUBLIC (x
) || is_global_var (x
))
4806 ("global register variable %qD used in nested function", x
);
4809 pedwarn (input_location
, 0, "register variable %qD used in nested function", x
);
4811 else if (C_DECL_REGISTER (x
))
4813 if (TREE_PUBLIC (x
) || is_global_var (x
))
4814 error ("address of global register variable %qD requested", x
);
4816 error ("address of register variable %qD requested", x
);
4822 TREE_ADDRESSABLE (x
) = 1;
4829 /* Convert EXPR to TYPE, warning about conversion problems with
4830 constants. SEMANTIC_TYPE is the type this conversion would use
4831 without excess precision. If SEMANTIC_TYPE is NULL, this function
4832 is equivalent to convert_and_check. This function is a wrapper that
4833 handles conversions that may be different than
4834 the usual ones because of excess precision. */
4837 ep_convert_and_check (location_t loc
, tree type
, tree expr
,
4840 if (TREE_TYPE (expr
) == type
)
4844 return convert_and_check (loc
, type
, expr
);
4846 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
4847 && TREE_TYPE (expr
) != semantic_type
)
4849 /* For integers, we need to check the real conversion, not
4850 the conversion to the excess precision type. */
4851 expr
= convert_and_check (loc
, semantic_type
, expr
);
4853 /* Result type is the excess precision type, which should be
4854 large enough, so do not check. */
4855 return convert (type
, expr
);
4858 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4859 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4860 if folded to an integer constant then the unselected half may
4861 contain arbitrary operations not normally permitted in constant
4862 expressions. Set the location of the expression to LOC. */
4865 build_conditional_expr (location_t colon_loc
, tree ifexp
, bool ifexp_bcp
,
4866 tree op1
, tree op1_original_type
, tree op2
,
4867 tree op2_original_type
)
4871 enum tree_code code1
;
4872 enum tree_code code2
;
4873 tree result_type
= NULL
;
4874 tree semantic_result_type
= NULL
;
4875 tree orig_op1
= op1
, orig_op2
= op2
;
4876 bool int_const
, op1_int_operands
, op2_int_operands
, int_operands
;
4877 bool ifexp_int_operands
;
4880 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
4881 if (op1_int_operands
)
4882 op1
= remove_c_maybe_const_expr (op1
);
4883 op2_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op2
);
4884 if (op2_int_operands
)
4885 op2
= remove_c_maybe_const_expr (op2
);
4886 ifexp_int_operands
= EXPR_INT_CONST_OPERANDS (ifexp
);
4887 if (ifexp_int_operands
)
4888 ifexp
= remove_c_maybe_const_expr (ifexp
);
4890 /* Promote both alternatives. */
4892 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
4893 op1
= default_conversion (op1
);
4894 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
4895 op2
= default_conversion (op2
);
4897 if (TREE_CODE (ifexp
) == ERROR_MARK
4898 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
4899 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
4900 return error_mark_node
;
4902 type1
= TREE_TYPE (op1
);
4903 code1
= TREE_CODE (type1
);
4904 type2
= TREE_TYPE (op2
);
4905 code2
= TREE_CODE (type2
);
4907 if (code1
== POINTER_TYPE
&& reject_gcc_builtin (op1
))
4908 return error_mark_node
;
4910 if (code2
== POINTER_TYPE
&& reject_gcc_builtin (op2
))
4911 return error_mark_node
;
4913 /* C90 does not permit non-lvalue arrays in conditional expressions.
4914 In C99 they will be pointers by now. */
4915 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
4917 error_at (colon_loc
, "non-lvalue array in conditional expression");
4918 return error_mark_node
;
4921 if ((TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
4922 || TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4923 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4924 || code1
== COMPLEX_TYPE
)
4925 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4926 || code2
== COMPLEX_TYPE
))
4928 semantic_result_type
= c_common_type (type1
, type2
);
4929 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
4931 op1
= TREE_OPERAND (op1
, 0);
4932 type1
= TREE_TYPE (op1
);
4933 gcc_assert (TREE_CODE (type1
) == code1
);
4935 if (TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4937 op2
= TREE_OPERAND (op2
, 0);
4938 type2
= TREE_TYPE (op2
);
4939 gcc_assert (TREE_CODE (type2
) == code2
);
4943 if (warn_cxx_compat
)
4945 tree t1
= op1_original_type
? op1_original_type
: TREE_TYPE (orig_op1
);
4946 tree t2
= op2_original_type
? op2_original_type
: TREE_TYPE (orig_op2
);
4948 if (TREE_CODE (t1
) == ENUMERAL_TYPE
4949 && TREE_CODE (t2
) == ENUMERAL_TYPE
4950 && TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
4951 warning_at (colon_loc
, OPT_Wc___compat
,
4952 ("different enum types in conditional is "
4953 "invalid in C++: %qT vs %qT"),
4957 /* Quickly detect the usual case where op1 and op2 have the same type
4959 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
4962 result_type
= type1
;
4964 result_type
= TYPE_MAIN_VARIANT (type1
);
4966 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4967 || code1
== COMPLEX_TYPE
)
4968 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4969 || code2
== COMPLEX_TYPE
))
4971 result_type
= c_common_type (type1
, type2
);
4972 if (result_type
== error_mark_node
)
4973 return error_mark_node
;
4974 do_warn_double_promotion (result_type
, type1
, type2
,
4975 "implicit conversion from %qT to %qT to "
4976 "match other result of conditional",
4979 /* If -Wsign-compare, warn here if type1 and type2 have
4980 different signedness. We'll promote the signed to unsigned
4981 and later code won't know it used to be different.
4982 Do this check on the original types, so that explicit casts
4983 will be considered, but default promotions won't. */
4984 if (c_inhibit_evaluation_warnings
== 0)
4986 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
4987 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
4989 if (unsigned_op1
^ unsigned_op2
)
4993 /* Do not warn if the result type is signed, since the
4994 signed type will only be chosen if it can represent
4995 all the values of the unsigned type. */
4996 if (!TYPE_UNSIGNED (result_type
))
5000 bool op1_maybe_const
= true;
5001 bool op2_maybe_const
= true;
5003 /* Do not warn if the signed quantity is an
5004 unsuffixed integer literal (or some static
5005 constant expression involving such literals) and
5006 it is non-negative. This warning requires the
5007 operands to be folded for best results, so do
5008 that folding in this case even without
5009 warn_sign_compare to avoid warning options
5010 possibly affecting code generation. */
5011 c_inhibit_evaluation_warnings
5012 += (ifexp
== truthvalue_false_node
);
5013 op1
= c_fully_fold (op1
, require_constant_value
,
5015 c_inhibit_evaluation_warnings
5016 -= (ifexp
== truthvalue_false_node
);
5018 c_inhibit_evaluation_warnings
5019 += (ifexp
== truthvalue_true_node
);
5020 op2
= c_fully_fold (op2
, require_constant_value
,
5022 c_inhibit_evaluation_warnings
5023 -= (ifexp
== truthvalue_true_node
);
5025 if (warn_sign_compare
)
5028 && tree_expr_nonnegative_warnv_p (op1
, &ovf
))
5030 && tree_expr_nonnegative_warnv_p (op2
, &ovf
)))
5033 warning_at (colon_loc
, OPT_Wsign_compare
,
5034 ("signed and unsigned type in "
5035 "conditional expression"));
5037 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
5038 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
5039 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
5040 op2
= c_wrap_maybe_const (op2
, !op2_maybe_const
);
5045 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
5047 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
5048 pedwarn (colon_loc
, OPT_Wpedantic
,
5049 "ISO C forbids conditional expr with only one void side");
5050 result_type
= void_type_node
;
5052 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
5054 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
5055 addr_space_t as2
= TYPE_ADDR_SPACE (TREE_TYPE (type2
));
5056 addr_space_t as_common
;
5058 if (comp_target_types (colon_loc
, type1
, type2
))
5059 result_type
= common_pointer_type (type1
, type2
);
5060 else if (null_pointer_constant_p (orig_op1
))
5061 result_type
= type2
;
5062 else if (null_pointer_constant_p (orig_op2
))
5063 result_type
= type1
;
5064 else if (!addr_space_superset (as1
, as2
, &as_common
))
5066 error_at (colon_loc
, "pointers to disjoint address spaces "
5067 "used in conditional expression");
5068 return error_mark_node
;
5070 else if (VOID_TYPE_P (TREE_TYPE (type1
))
5071 && !TYPE_ATOMIC (TREE_TYPE (type1
)))
5073 if ((TREE_CODE (TREE_TYPE (type2
)) == ARRAY_TYPE
)
5074 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2
)))
5075 & ~TYPE_QUALS (TREE_TYPE (type1
))))
5076 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
5077 "pointer to array loses qualifier "
5078 "in conditional expression");
5080 if (TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
5081 pedwarn (colon_loc
, OPT_Wpedantic
,
5082 "ISO C forbids conditional expr between "
5083 "%<void *%> and function pointer");
5084 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
5085 TREE_TYPE (type2
)));
5087 else if (VOID_TYPE_P (TREE_TYPE (type2
))
5088 && !TYPE_ATOMIC (TREE_TYPE (type2
)))
5090 if ((TREE_CODE (TREE_TYPE (type1
)) == ARRAY_TYPE
)
5091 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1
)))
5092 & ~TYPE_QUALS (TREE_TYPE (type2
))))
5093 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
5094 "pointer to array loses qualifier "
5095 "in conditional expression");
5097 if (TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
5098 pedwarn (colon_loc
, OPT_Wpedantic
,
5099 "ISO C forbids conditional expr between "
5100 "%<void *%> and function pointer");
5101 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
5102 TREE_TYPE (type1
)));
5104 /* Objective-C pointer comparisons are a bit more lenient. */
5105 else if (objc_have_common_type (type1
, type2
, -3, NULL_TREE
))
5106 result_type
= objc_common_type (type1
, type2
);
5109 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
5111 pedwarn (colon_loc
, 0,
5112 "pointer type mismatch in conditional expression");
5113 result_type
= build_pointer_type
5114 (build_qualified_type (void_type_node
, qual
));
5117 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
5119 if (!null_pointer_constant_p (orig_op2
))
5120 pedwarn (colon_loc
, 0,
5121 "pointer/integer type mismatch in conditional expression");
5124 op2
= null_pointer_node
;
5126 result_type
= type1
;
5128 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5130 if (!null_pointer_constant_p (orig_op1
))
5131 pedwarn (colon_loc
, 0,
5132 "pointer/integer type mismatch in conditional expression");
5135 op1
= null_pointer_node
;
5137 result_type
= type2
;
5142 if (flag_cond_mismatch
)
5143 result_type
= void_type_node
;
5146 error_at (colon_loc
, "type mismatch in conditional expression");
5147 return error_mark_node
;
5151 /* Merge const and volatile flags of the incoming types. */
5153 = build_type_variant (result_type
,
5154 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
5155 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
5157 op1
= ep_convert_and_check (colon_loc
, result_type
, op1
,
5158 semantic_result_type
);
5159 op2
= ep_convert_and_check (colon_loc
, result_type
, op2
,
5160 semantic_result_type
);
5162 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
5164 op2_int_operands
= true;
5165 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
5167 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
5169 op1_int_operands
= true;
5170 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
5172 int_const
= int_operands
= (ifexp_int_operands
5174 && op2_int_operands
);
5177 int_const
= ((ifexp
== truthvalue_true_node
5178 && TREE_CODE (orig_op1
) == INTEGER_CST
5179 && !TREE_OVERFLOW (orig_op1
))
5180 || (ifexp
== truthvalue_false_node
5181 && TREE_CODE (orig_op2
) == INTEGER_CST
5182 && !TREE_OVERFLOW (orig_op2
)));
5185 /* Need to convert condition operand into a vector mask. */
5186 if (VECTOR_TYPE_P (TREE_TYPE (ifexp
)))
5188 tree vectype
= TREE_TYPE (ifexp
);
5189 tree elem_type
= TREE_TYPE (vectype
);
5190 tree zero
= build_int_cst (elem_type
, 0);
5191 tree zero_vec
= build_vector_from_val (vectype
, zero
);
5192 tree cmp_type
= build_same_sized_truth_vector_type (vectype
);
5193 ifexp
= build2 (NE_EXPR
, cmp_type
, ifexp
, zero_vec
);
5196 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
5197 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
5202 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5203 nested inside of the expression. */
5204 op1
= c_fully_fold (op1
, false, NULL
);
5205 op2
= c_fully_fold (op2
, false, NULL
);
5207 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
5209 ret
= note_integer_operands (ret
);
5211 if (semantic_result_type
)
5212 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
5214 protected_set_expr_location (ret
, colon_loc
);
5216 /* If the OP1 and OP2 are the same and don't have side-effects,
5217 warn here, because the COND_EXPR will be turned into OP1. */
5218 if (warn_duplicated_branches
5219 && TREE_CODE (ret
) == COND_EXPR
5220 && (op1
== op2
|| operand_equal_p (op1
, op2
, 0)))
5221 warning_at (EXPR_LOCATION (ret
), OPT_Wduplicated_branches
,
5222 "this condition has identical branches");
5227 /* Return a compound expression that performs two expressions and
5228 returns the value of the second of them.
5230 LOC is the location of the COMPOUND_EXPR. */
5233 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
5235 bool expr1_int_operands
, expr2_int_operands
;
5236 tree eptype
= NULL_TREE
;
5240 && (TREE_CODE (expr1
) == CILK_SPAWN_STMT
5241 || TREE_CODE (expr2
) == CILK_SPAWN_STMT
))
5244 "spawned function call cannot be part of a comma expression");
5245 return error_mark_node
;
5247 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
5248 if (expr1_int_operands
)
5249 expr1
= remove_c_maybe_const_expr (expr1
);
5250 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
5251 if (expr2_int_operands
)
5252 expr2
= remove_c_maybe_const_expr (expr2
);
5254 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
5255 expr1
= TREE_OPERAND (expr1
, 0);
5256 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
5258 eptype
= TREE_TYPE (expr2
);
5259 expr2
= TREE_OPERAND (expr2
, 0);
5262 if (!TREE_SIDE_EFFECTS (expr1
))
5264 /* The left-hand operand of a comma expression is like an expression
5265 statement: with -Wunused, we should warn if it doesn't have
5266 any side-effects, unless it was explicitly cast to (void). */
5267 if (warn_unused_value
)
5269 if (VOID_TYPE_P (TREE_TYPE (expr1
))
5270 && CONVERT_EXPR_P (expr1
))
5272 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
5273 && TREE_CODE (expr1
) == COMPOUND_EXPR
5274 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
5275 ; /* (void) a, (void) b, c */
5277 warning_at (loc
, OPT_Wunused_value
,
5278 "left-hand operand of comma expression has no effect");
5281 else if (TREE_CODE (expr1
) == COMPOUND_EXPR
5282 && warn_unused_value
)
5285 location_t cloc
= loc
;
5286 while (TREE_CODE (r
) == COMPOUND_EXPR
)
5288 if (EXPR_HAS_LOCATION (r
))
5289 cloc
= EXPR_LOCATION (r
);
5290 r
= TREE_OPERAND (r
, 1);
5292 if (!TREE_SIDE_EFFECTS (r
)
5293 && !VOID_TYPE_P (TREE_TYPE (r
))
5294 && !CONVERT_EXPR_P (r
))
5295 warning_at (cloc
, OPT_Wunused_value
,
5296 "right-hand operand of comma expression has no effect");
5299 /* With -Wunused, we should also warn if the left-hand operand does have
5300 side-effects, but computes a value which is not used. For example, in
5301 `foo() + bar(), baz()' the result of the `+' operator is not used,
5302 so we should issue a warning. */
5303 else if (warn_unused_value
)
5304 warn_if_unused_value (expr1
, loc
);
5306 if (expr2
== error_mark_node
)
5307 return error_mark_node
;
5309 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
5312 && expr1_int_operands
5313 && expr2_int_operands
)
5314 ret
= note_integer_operands (ret
);
5317 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
5319 protected_set_expr_location (ret
, loc
);
5323 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5324 which we are casting. OTYPE is the type of the expression being
5325 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5326 of the cast. -Wcast-qual appeared on the command line. Named
5327 address space qualifiers are not handled here, because they result
5328 in different warnings. */
5331 handle_warn_cast_qual (location_t loc
, tree type
, tree otype
)
5333 tree in_type
= type
;
5334 tree in_otype
= otype
;
5339 /* Check that the qualifiers on IN_TYPE are a superset of the
5340 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5341 nodes is uninteresting and we stop as soon as we hit a
5342 non-POINTER_TYPE node on either type. */
5345 in_otype
= TREE_TYPE (in_otype
);
5346 in_type
= TREE_TYPE (in_type
);
5348 /* GNU C allows cv-qualified function types. 'const' means the
5349 function is very pure, 'volatile' means it can't return. We
5350 need to warn when such qualifiers are added, not when they're
5352 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
5353 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
5354 added
|= (TYPE_QUALS_NO_ADDR_SPACE (in_type
)
5355 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype
));
5357 discarded
|= (TYPE_QUALS_NO_ADDR_SPACE (in_otype
)
5358 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type
));
5360 while (TREE_CODE (in_type
) == POINTER_TYPE
5361 && TREE_CODE (in_otype
) == POINTER_TYPE
);
5364 warning_at (loc
, OPT_Wcast_qual
,
5365 "cast adds %q#v qualifier to function type", added
);
5368 /* There are qualifiers present in IN_OTYPE that are not present
5370 warning_at (loc
, OPT_Wcast_qual
,
5371 "cast discards %qv qualifier from pointer target type",
5374 if (added
|| discarded
)
5377 /* A cast from **T to const **T is unsafe, because it can cause a
5378 const value to be changed with no additional warning. We only
5379 issue this warning if T is the same on both sides, and we only
5380 issue the warning if there are the same number of pointers on
5381 both sides, as otherwise the cast is clearly unsafe anyhow. A
5382 cast is unsafe when a qualifier is added at one level and const
5383 is not present at all outer levels.
5385 To issue this warning, we check at each level whether the cast
5386 adds new qualifiers not already seen. We don't need to special
5387 case function types, as they won't have the same
5388 TYPE_MAIN_VARIANT. */
5390 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
5392 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
5397 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
5400 in_type
= TREE_TYPE (in_type
);
5401 in_otype
= TREE_TYPE (in_otype
);
5402 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
5405 warning_at (loc
, OPT_Wcast_qual
,
5406 "to be safe all intermediate pointers in cast from "
5407 "%qT to %qT must be %<const%> qualified",
5412 is_const
= TYPE_READONLY (in_type
);
5414 while (TREE_CODE (in_type
) == POINTER_TYPE
);
5417 /* Build an expression representing a cast to type TYPE of expression EXPR.
5418 LOC is the location of the cast-- typically the open paren of the cast. */
5421 build_c_cast (location_t loc
, tree type
, tree expr
)
5425 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
5426 expr
= TREE_OPERAND (expr
, 0);
5430 if (type
== error_mark_node
|| expr
== error_mark_node
)
5431 return error_mark_node
;
5433 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5434 only in <protocol> qualifications. But when constructing cast expressions,
5435 the protocols do matter and must be kept around. */
5436 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
5437 return build1 (NOP_EXPR
, type
, expr
);
5439 type
= TYPE_MAIN_VARIANT (type
);
5441 if (TREE_CODE (type
) == ARRAY_TYPE
)
5443 error_at (loc
, "cast specifies array type");
5444 return error_mark_node
;
5447 if (TREE_CODE (type
) == FUNCTION_TYPE
)
5449 error_at (loc
, "cast specifies function type");
5450 return error_mark_node
;
5453 if (!VOID_TYPE_P (type
))
5455 value
= require_complete_type (loc
, value
);
5456 if (value
== error_mark_node
)
5457 return error_mark_node
;
5460 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
5462 if (RECORD_OR_UNION_TYPE_P (type
))
5463 pedwarn (loc
, OPT_Wpedantic
,
5464 "ISO C forbids casting nonscalar to the same type");
5466 /* Convert to remove any qualifiers from VALUE's type. */
5467 value
= convert (type
, value
);
5469 else if (TREE_CODE (type
) == UNION_TYPE
)
5473 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5474 if (TREE_TYPE (field
) != error_mark_node
5475 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
5476 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
5482 bool maybe_const
= true;
5484 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids casts to union type");
5485 t
= c_fully_fold (value
, false, &maybe_const
);
5486 t
= build_constructor_single (type
, field
, t
);
5488 t
= c_wrap_maybe_const (t
, true);
5489 t
= digest_init (loc
, type
, t
,
5490 NULL_TREE
, false, true, 0);
5491 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
5494 error_at (loc
, "cast to union type from type not present in union");
5495 return error_mark_node
;
5501 if (type
== void_type_node
)
5503 tree t
= build1 (CONVERT_EXPR
, type
, value
);
5504 SET_EXPR_LOCATION (t
, loc
);
5508 otype
= TREE_TYPE (value
);
5510 /* Optionally warn about potentially worrisome casts. */
5512 && TREE_CODE (type
) == POINTER_TYPE
5513 && TREE_CODE (otype
) == POINTER_TYPE
)
5514 handle_warn_cast_qual (loc
, type
, otype
);
5516 /* Warn about conversions between pointers to disjoint
5518 if (TREE_CODE (type
) == POINTER_TYPE
5519 && TREE_CODE (otype
) == POINTER_TYPE
5520 && !null_pointer_constant_p (value
))
5522 addr_space_t as_to
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
5523 addr_space_t as_from
= TYPE_ADDR_SPACE (TREE_TYPE (otype
));
5524 addr_space_t as_common
;
5526 if (!addr_space_superset (as_to
, as_from
, &as_common
))
5528 if (ADDR_SPACE_GENERIC_P (as_from
))
5529 warning_at (loc
, 0, "cast to %s address space pointer "
5530 "from disjoint generic address space pointer",
5531 c_addr_space_name (as_to
));
5533 else if (ADDR_SPACE_GENERIC_P (as_to
))
5534 warning_at (loc
, 0, "cast to generic address space pointer "
5535 "from disjoint %s address space pointer",
5536 c_addr_space_name (as_from
));
5539 warning_at (loc
, 0, "cast to %s address space pointer "
5540 "from disjoint %s address space pointer",
5541 c_addr_space_name (as_to
),
5542 c_addr_space_name (as_from
));
5546 /* Warn about possible alignment problems. */
5547 if (STRICT_ALIGNMENT
5548 && TREE_CODE (type
) == POINTER_TYPE
5549 && TREE_CODE (otype
) == POINTER_TYPE
5550 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5551 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5552 /* Don't warn about opaque types, where the actual alignment
5553 restriction is unknown. */
5554 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype
))
5555 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
5556 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5557 warning_at (loc
, OPT_Wcast_align
,
5558 "cast increases required alignment of target type");
5560 if (TREE_CODE (type
) == INTEGER_TYPE
5561 && TREE_CODE (otype
) == POINTER_TYPE
5562 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5563 /* Unlike conversion of integers to pointers, where the
5564 warning is disabled for converting constants because
5565 of cases such as SIG_*, warn about converting constant
5566 pointers to integers. In some cases it may cause unwanted
5567 sign extension, and a warning is appropriate. */
5568 warning_at (loc
, OPT_Wpointer_to_int_cast
,
5569 "cast from pointer to integer of different size");
5571 if (TREE_CODE (value
) == CALL_EXPR
5572 && TREE_CODE (type
) != TREE_CODE (otype
))
5573 warning_at (loc
, OPT_Wbad_function_cast
,
5574 "cast from function call of type %qT "
5575 "to non-matching type %qT", otype
, type
);
5577 if (TREE_CODE (type
) == POINTER_TYPE
5578 && TREE_CODE (otype
) == INTEGER_TYPE
5579 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5580 /* Don't warn about converting any constant. */
5581 && !TREE_CONSTANT (value
))
5583 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
5584 "of different size");
5586 if (warn_strict_aliasing
<= 2)
5587 strict_aliasing_warning (otype
, type
, expr
);
5589 /* If pedantic, warn for conversions between function and object
5590 pointer types, except for converting a null pointer constant
5591 to function pointer type. */
5593 && TREE_CODE (type
) == POINTER_TYPE
5594 && TREE_CODE (otype
) == POINTER_TYPE
5595 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
5596 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
5597 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5598 "conversion of function pointer to object pointer type");
5601 && TREE_CODE (type
) == POINTER_TYPE
5602 && TREE_CODE (otype
) == POINTER_TYPE
5603 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
5604 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5605 && !null_pointer_constant_p (value
))
5606 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5607 "conversion of object pointer to function pointer type");
5610 value
= convert (type
, value
);
5612 /* Ignore any integer overflow caused by the cast. */
5613 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
5615 if (CONSTANT_CLASS_P (ovalue
) && TREE_OVERFLOW (ovalue
))
5617 if (!TREE_OVERFLOW (value
))
5619 /* Avoid clobbering a shared constant. */
5620 value
= copy_node (value
);
5621 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5624 else if (TREE_OVERFLOW (value
))
5625 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5626 value
= wide_int_to_tree (TREE_TYPE (value
), value
);
5630 /* Don't let a cast be an lvalue. */
5631 if (lvalue_p (value
))
5632 value
= non_lvalue_loc (loc
, value
);
5634 /* Don't allow the results of casting to floating-point or complex
5635 types be confused with actual constants, or casts involving
5636 integer and pointer types other than direct integer-to-integer
5637 and integer-to-pointer be confused with integer constant
5638 expressions and null pointer constants. */
5639 if (TREE_CODE (value
) == REAL_CST
5640 || TREE_CODE (value
) == COMPLEX_CST
5641 || (TREE_CODE (value
) == INTEGER_CST
5642 && !((TREE_CODE (expr
) == INTEGER_CST
5643 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
5644 || TREE_CODE (expr
) == REAL_CST
5645 || TREE_CODE (expr
) == COMPLEX_CST
)))
5646 value
= build1 (NOP_EXPR
, type
, value
);
5648 protected_set_expr_location (value
, loc
);
5652 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5653 location of the open paren of the cast, or the position of the cast
5656 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
5659 tree type_expr
= NULL_TREE
;
5660 bool type_expr_const
= true;
5662 int saved_wsp
= warn_strict_prototypes
;
5664 /* This avoids warnings about unprototyped casts on
5665 integers. E.g. "#define SIG_DFL (void(*)())0". */
5666 if (TREE_CODE (expr
) == INTEGER_CST
)
5667 warn_strict_prototypes
= 0;
5668 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
5669 warn_strict_prototypes
= saved_wsp
;
5671 if (TREE_CODE (expr
) == ADDR_EXPR
&& !VOID_TYPE_P (type
)
5672 && reject_gcc_builtin (expr
))
5673 return error_mark_node
;
5675 ret
= build_c_cast (loc
, type
, expr
);
5678 bool inner_expr_const
= true;
5679 ret
= c_fully_fold (ret
, require_constant_value
, &inner_expr_const
);
5680 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
5681 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !(type_expr_const
5682 && inner_expr_const
);
5683 SET_EXPR_LOCATION (ret
, loc
);
5686 if (!EXPR_HAS_LOCATION (ret
))
5687 protected_set_expr_location (ret
, loc
);
5689 /* C++ does not permits types to be defined in a cast, but it
5690 allows references to incomplete types. */
5691 if (warn_cxx_compat
&& type_name
->specs
->typespec_kind
== ctsk_tagdef
)
5692 warning_at (loc
, OPT_Wc___compat
,
5693 "defining a type in a cast is invalid in C++");
5698 /* Build an assignment expression of lvalue LHS from value RHS.
5699 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5700 may differ from TREE_TYPE (LHS) for an enum bitfield.
5701 MODIFYCODE is the code for a binary operator that we use
5702 to combine the old value of LHS with RHS to get the new value.
5703 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5704 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5705 which may differ from TREE_TYPE (RHS) for an enum value.
5707 LOCATION is the location of the MODIFYCODE operator.
5708 RHS_LOC is the location of the RHS. */
5711 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
5712 enum tree_code modifycode
,
5713 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
5717 tree rhseval
= NULL_TREE
;
5718 tree rhs_semantic_type
= NULL_TREE
;
5719 tree lhstype
= TREE_TYPE (lhs
);
5720 tree olhstype
= lhstype
;
5724 /* Types that aren't fully specified cannot be used in assignments. */
5725 lhs
= require_complete_type (location
, lhs
);
5727 /* Avoid duplicate error messages from operands that had errors. */
5728 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
5729 return error_mark_node
;
5731 /* Ensure an error for assigning a non-lvalue array to an array in
5733 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
5735 error_at (location
, "assignment to expression with array type");
5736 return error_mark_node
;
5739 /* For ObjC properties, defer this check. */
5740 if (!objc_is_property_ref (lhs
) && !lvalue_or_else (location
, lhs
, lv_assign
))
5741 return error_mark_node
;
5743 is_atomic_op
= really_atomic_lvalue (lhs
);
5745 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5747 rhs_semantic_type
= TREE_TYPE (rhs
);
5748 rhs
= TREE_OPERAND (rhs
, 0);
5753 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
5755 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
5756 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
5758 if (inner
== error_mark_node
)
5759 return error_mark_node
;
5760 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
5761 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
5762 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
5763 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
5764 protected_set_expr_location (result
, location
);
5768 /* If a binary op has been requested, combine the old LHS value with the RHS
5769 producing the value we should actually store into the LHS. */
5771 if (modifycode
!= NOP_EXPR
)
5773 lhs
= c_fully_fold (lhs
, false, NULL
);
5774 lhs
= stabilize_reference (lhs
);
5776 /* Construct the RHS for any non-atomic compound assignemnt. */
5779 /* If in LHS op= RHS the RHS has side-effects, ensure they
5780 are preevaluated before the rest of the assignment expression's
5781 side-effects, because RHS could contain e.g. function calls
5783 if (TREE_SIDE_EFFECTS (rhs
))
5785 newrhs
= save_expr (rhs
);
5788 newrhs
= build_binary_op (location
,
5789 modifycode
, lhs
, newrhs
, 1);
5791 /* The original type of the right hand side is no longer
5793 rhs_origtype
= NULL_TREE
;
5797 if (c_dialect_objc ())
5799 /* Check if we are modifying an Objective-C property reference;
5800 if so, we need to generate setter calls. */
5801 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
5805 /* Else, do the check that we postponed for Objective-C. */
5806 if (!lvalue_or_else (location
, lhs
, lv_assign
))
5807 return error_mark_node
;
5810 /* Give an error for storing in something that is 'const'. */
5812 if (TYPE_READONLY (lhstype
)
5813 || (RECORD_OR_UNION_TYPE_P (lhstype
)
5814 && C_TYPE_FIELDS_READONLY (lhstype
)))
5816 readonly_error (location
, lhs
, lv_assign
);
5817 return error_mark_node
;
5819 else if (TREE_READONLY (lhs
))
5820 readonly_warning (lhs
, lv_assign
);
5822 /* If storing into a structure or union member,
5823 it has probably been given type `int'.
5824 Compute the type that would go with
5825 the actual amount of storage the member occupies. */
5827 if (TREE_CODE (lhs
) == COMPONENT_REF
5828 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5829 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
5830 || TREE_CODE (lhstype
) == REAL_TYPE
5831 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5832 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5834 /* If storing in a field that is in actuality a short or narrower than one,
5835 we must store in the field in its actual type. */
5837 if (lhstype
!= TREE_TYPE (lhs
))
5839 lhs
= copy_node (lhs
);
5840 TREE_TYPE (lhs
) = lhstype
;
5843 /* Issue -Wc++-compat warnings about an assignment to an enum type
5844 when LHS does not have its original type. This happens for,
5845 e.g., an enum bitfield in a struct. */
5847 && lhs_origtype
!= NULL_TREE
5848 && lhs_origtype
!= lhstype
5849 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
5851 tree checktype
= (rhs_origtype
!= NULL_TREE
5854 if (checktype
!= error_mark_node
5855 && (TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
)
5856 || (is_atomic_op
&& modifycode
!= NOP_EXPR
)))
5857 warning_at (location
, OPT_Wc___compat
,
5858 "enum conversion in assignment is invalid in C++");
5861 /* If the lhs is atomic, remove that qualifier. */
5864 lhstype
= build_qualified_type (lhstype
,
5865 (TYPE_QUALS (lhstype
)
5866 & ~TYPE_QUAL_ATOMIC
));
5867 olhstype
= build_qualified_type (olhstype
,
5868 (TYPE_QUALS (lhstype
)
5869 & ~TYPE_QUAL_ATOMIC
));
5872 /* Convert new value to destination type. Fold it first, then
5873 restore any excess precision information, for the sake of
5874 conversion warnings. */
5876 if (!(is_atomic_op
&& modifycode
!= NOP_EXPR
))
5878 npc
= null_pointer_constant_p (newrhs
);
5879 newrhs
= c_fully_fold (newrhs
, false, NULL
);
5880 if (rhs_semantic_type
)
5881 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
5882 newrhs
= convert_for_assignment (location
, rhs_loc
, lhstype
, newrhs
,
5883 rhs_origtype
, ic_assign
, npc
,
5884 NULL_TREE
, NULL_TREE
, 0);
5885 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5886 return error_mark_node
;
5889 /* Emit ObjC write barrier, if necessary. */
5890 if (c_dialect_objc () && flag_objc_gc
)
5892 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
5895 protected_set_expr_location (result
, location
);
5900 /* Scan operands. */
5903 result
= build_atomic_assign (location
, lhs
, modifycode
, newrhs
, false);
5906 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
5907 TREE_SIDE_EFFECTS (result
) = 1;
5908 protected_set_expr_location (result
, location
);
5911 /* If we got the LHS in a different type for storing in,
5912 convert the result back to the nominal type of LHS
5913 so that the value we return always has the same type
5914 as the LHS argument. */
5916 if (olhstype
== TREE_TYPE (result
))
5919 result
= convert_for_assignment (location
, rhs_loc
, olhstype
, result
,
5920 rhs_origtype
, ic_assign
, false, NULL_TREE
,
5922 protected_set_expr_location (result
, location
);
5926 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), rhseval
, result
);
5930 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5931 This is used to implement -fplan9-extensions. */
5934 find_anonymous_field_with_type (tree struct_type
, tree type
)
5939 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type
));
5941 for (field
= TYPE_FIELDS (struct_type
);
5943 field
= TREE_CHAIN (field
))
5945 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5946 ? c_build_qualified_type (TREE_TYPE (field
),
5948 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5949 if (DECL_NAME (field
) == NULL
5950 && comptypes (type
, fieldtype
))
5956 else if (DECL_NAME (field
) == NULL
5957 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
))
5958 && find_anonymous_field_with_type (TREE_TYPE (field
), type
))
5968 /* RHS is an expression whose type is pointer to struct. If there is
5969 an anonymous field in RHS with type TYPE, then return a pointer to
5970 that field in RHS. This is used with -fplan9-extensions. This
5971 returns NULL if no conversion could be found. */
5974 convert_to_anonymous_field (location_t location
, tree type
, tree rhs
)
5976 tree rhs_struct_type
, lhs_main_type
;
5977 tree field
, found_field
;
5978 bool found_sub_field
;
5981 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs
)));
5982 rhs_struct_type
= TREE_TYPE (TREE_TYPE (rhs
));
5983 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type
));
5985 gcc_assert (POINTER_TYPE_P (type
));
5986 lhs_main_type
= (TYPE_ATOMIC (TREE_TYPE (type
))
5987 ? c_build_qualified_type (TREE_TYPE (type
),
5989 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
5991 found_field
= NULL_TREE
;
5992 found_sub_field
= false;
5993 for (field
= TYPE_FIELDS (rhs_struct_type
);
5995 field
= TREE_CHAIN (field
))
5997 if (DECL_NAME (field
) != NULL_TREE
5998 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
6000 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
6001 ? c_build_qualified_type (TREE_TYPE (field
),
6003 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
6004 if (comptypes (lhs_main_type
, fieldtype
))
6006 if (found_field
!= NULL_TREE
)
6008 found_field
= field
;
6010 else if (find_anonymous_field_with_type (TREE_TYPE (field
),
6013 if (found_field
!= NULL_TREE
)
6015 found_field
= field
;
6016 found_sub_field
= true;
6020 if (found_field
== NULL_TREE
)
6023 ret
= fold_build3_loc (location
, COMPONENT_REF
, TREE_TYPE (found_field
),
6024 build_fold_indirect_ref (rhs
), found_field
,
6026 ret
= build_fold_addr_expr_loc (location
, ret
);
6028 if (found_sub_field
)
6030 ret
= convert_to_anonymous_field (location
, type
, ret
);
6031 gcc_assert (ret
!= NULL_TREE
);
6037 /* Issue an error message for a bad initializer component.
6038 GMSGID identifies the message.
6039 The component name is taken from the spelling stack. */
6042 error_init (location_t loc
, const char *gmsgid
)
6046 /* The gmsgid may be a format string with %< and %>. */
6047 error_at (loc
, gmsgid
);
6048 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6050 inform (loc
, "(near initialization for %qs)", ofwhat
);
6053 /* Issue a pedantic warning for a bad initializer component. OPT is
6054 the option OPT_* (from options.h) controlling this warning or 0 if
6055 it is unconditionally given. GMSGID identifies the message. The
6056 component name is taken from the spelling stack. */
6059 pedwarn_init (location_t loc
, int opt
, const char *gmsgid
)
6064 /* Use the location where a macro was expanded rather than where
6065 it was defined to make sure macros defined in system headers
6066 but used incorrectly elsewhere are diagnosed. */
6067 source_location exploc
= expansion_point_location_if_in_system_header (loc
);
6069 /* The gmsgid may be a format string with %< and %>. */
6070 warned
= pedwarn (exploc
, opt
, gmsgid
);
6071 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6072 if (*ofwhat
&& warned
)
6073 inform (exploc
, "(near initialization for %qs)", ofwhat
);
6076 /* Issue a warning for a bad initializer component.
6078 OPT is the OPT_W* value corresponding to the warning option that
6079 controls this warning. GMSGID identifies the message. The
6080 component name is taken from the spelling stack. */
6083 warning_init (location_t loc
, int opt
, const char *gmsgid
)
6088 /* Use the location where a macro was expanded rather than where
6089 it was defined to make sure macros defined in system headers
6090 but used incorrectly elsewhere are diagnosed. */
6091 source_location exploc
= expansion_point_location_if_in_system_header (loc
);
6093 /* The gmsgid may be a format string with %< and %>. */
6094 warned
= warning_at (exploc
, opt
, gmsgid
);
6095 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6096 if (*ofwhat
&& warned
)
6097 inform (exploc
, "(near initialization for %qs)", ofwhat
);
6100 /* If TYPE is an array type and EXPR is a parenthesized string
6101 constant, warn if pedantic that EXPR is being used to initialize an
6102 object of type TYPE. */
6105 maybe_warn_string_init (location_t loc
, tree type
, struct c_expr expr
)
6108 && TREE_CODE (type
) == ARRAY_TYPE
6109 && TREE_CODE (expr
.value
) == STRING_CST
6110 && expr
.original_code
!= STRING_CST
)
6111 pedwarn_init (loc
, OPT_Wpedantic
,
6112 "array initialized from parenthesized string constant");
6115 /* Convert value RHS to type TYPE as preparation for an assignment to
6116 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6117 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6118 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6119 constant before any folding.
6120 The real work of conversion is done by `convert'.
6121 The purpose of this function is to generate error messages
6122 for assignments that are not allowed in C.
6123 ERRTYPE says whether it is argument passing, assignment,
6124 initialization or return.
6126 In the following example, '~' denotes where EXPR_LOC and '^' where
6129 f (var); [ic_argpass]
6131 x = var; [ic_assign]
6133 int x = var; [ic_init]
6135 return x; [ic_return]
6138 FUNCTION is a tree for the function being called.
6139 PARMNUM is the number of the argument, for printing in error messages. */
6142 convert_for_assignment (location_t location
, location_t expr_loc
, tree type
,
6143 tree rhs
, tree origtype
, enum impl_conv errtype
,
6144 bool null_pointer_constant
, tree fundecl
,
6145 tree function
, int parmnum
)
6147 enum tree_code codel
= TREE_CODE (type
);
6148 tree orig_rhs
= rhs
;
6150 enum tree_code coder
;
6151 tree rname
= NULL_TREE
;
6152 bool objc_ok
= false;
6154 /* Use the expansion point location to handle cases such as user's
6155 function returning a wrong-type macro defined in a system header. */
6156 location
= expansion_point_location_if_in_system_header (location
);
6158 if (errtype
== ic_argpass
)
6161 /* Change pointer to function to the function itself for
6163 if (TREE_CODE (function
) == ADDR_EXPR
6164 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
6165 function
= TREE_OPERAND (function
, 0);
6167 /* Handle an ObjC selector specially for diagnostics. */
6168 selector
= objc_message_selector ();
6170 if (selector
&& parmnum
> 2)
6177 /* This macro is used to emit diagnostics to ensure that all format
6178 strings are complete sentences, visible to gettext and checked at
6180 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6185 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6186 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6187 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6188 "expected %qT but argument is of type %qT", \
6192 pedwarn (LOCATION, OPT, AS); \
6195 pedwarn_init (LOCATION, OPT, IN); \
6198 pedwarn (LOCATION, OPT, RE); \
6201 gcc_unreachable (); \
6205 /* This macro is used to emit diagnostics to ensure that all format
6206 strings are complete sentences, visible to gettext and checked at
6207 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6208 extra parameter to enumerate qualifiers. */
6209 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6214 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6215 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6216 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6217 "expected %qT but argument is of type %qT", \
6221 pedwarn (LOCATION, OPT, AS, QUALS); \
6224 pedwarn (LOCATION, OPT, IN, QUALS); \
6227 pedwarn (LOCATION, OPT, RE, QUALS); \
6230 gcc_unreachable (); \
6234 /* This macro is used to emit diagnostics to ensure that all format
6235 strings are complete sentences, visible to gettext and checked at
6236 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6237 warning_at instead of pedwarn. */
6238 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6243 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6244 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6245 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6246 "expected %qT but argument is of type %qT", \
6250 warning_at (LOCATION, OPT, AS, QUALS); \
6253 warning_at (LOCATION, OPT, IN, QUALS); \
6256 warning_at (LOCATION, OPT, RE, QUALS); \
6259 gcc_unreachable (); \
6263 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
6264 rhs
= TREE_OPERAND (rhs
, 0);
6266 rhstype
= TREE_TYPE (rhs
);
6267 coder
= TREE_CODE (rhstype
);
6269 if (coder
== ERROR_MARK
)
6270 return error_mark_node
;
6272 if (c_dialect_objc ())
6295 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
6298 if (warn_cxx_compat
)
6300 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
6301 if (checktype
!= error_mark_node
6302 && TREE_CODE (type
) == ENUMERAL_TYPE
6303 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
6305 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wc___compat
,
6306 G_("enum conversion when passing argument "
6307 "%d of %qE is invalid in C++"),
6308 G_("enum conversion in assignment is "
6310 G_("enum conversion in initialization is "
6312 G_("enum conversion in return is "
6317 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
6320 if (coder
== VOID_TYPE
)
6322 /* Except for passing an argument to an unprototyped function,
6323 this is a constraint violation. When passing an argument to
6324 an unprototyped function, it is compile-time undefined;
6325 making it a constraint in that case was rejected in
6327 error_at (location
, "void value not ignored as it ought to be");
6328 return error_mark_node
;
6330 rhs
= require_complete_type (location
, rhs
);
6331 if (rhs
== error_mark_node
)
6332 return error_mark_node
;
6334 if (coder
== POINTER_TYPE
&& reject_gcc_builtin (rhs
))
6335 return error_mark_node
;
6337 /* A non-reference type can convert to a reference. This handles
6338 va_start, va_copy and possibly port built-ins. */
6339 if (codel
== REFERENCE_TYPE
&& coder
!= REFERENCE_TYPE
)
6341 if (!lvalue_p (rhs
))
6343 error_at (location
, "cannot pass rvalue to reference parameter");
6344 return error_mark_node
;
6346 if (!c_mark_addressable (rhs
))
6347 return error_mark_node
;
6348 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
6349 SET_EXPR_LOCATION (rhs
, location
);
6351 rhs
= convert_for_assignment (location
, expr_loc
,
6352 build_pointer_type (TREE_TYPE (type
)),
6353 rhs
, origtype
, errtype
,
6354 null_pointer_constant
, fundecl
, function
,
6356 if (rhs
== error_mark_node
)
6357 return error_mark_node
;
6359 rhs
= build1 (NOP_EXPR
, type
, rhs
);
6360 SET_EXPR_LOCATION (rhs
, location
);
6363 /* Some types can interconvert without explicit casts. */
6364 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
6365 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
6366 return convert (type
, rhs
);
6367 /* Arithmetic types all interconvert, and enum is treated like int. */
6368 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
6369 || codel
== FIXED_POINT_TYPE
6370 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
6371 || codel
== BOOLEAN_TYPE
)
6372 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
6373 || coder
== FIXED_POINT_TYPE
6374 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
6375 || coder
== BOOLEAN_TYPE
))
6378 bool save
= in_late_binary_op
;
6379 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
6380 || (coder
== REAL_TYPE
6381 && (codel
== INTEGER_TYPE
|| codel
== ENUMERAL_TYPE
)
6382 && sanitize_flags_p (SANITIZE_FLOAT_CAST
)))
6383 in_late_binary_op
= true;
6384 ret
= convert_and_check (expr_loc
!= UNKNOWN_LOCATION
6385 ? expr_loc
: location
, type
, orig_rhs
);
6386 in_late_binary_op
= save
;
6390 /* Aggregates in different TUs might need conversion. */
6391 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
6393 && comptypes (type
, rhstype
))
6394 return convert_and_check (expr_loc
!= UNKNOWN_LOCATION
6395 ? expr_loc
: location
, type
, rhs
);
6397 /* Conversion to a transparent union or record from its member types.
6398 This applies only to function arguments. */
6399 if (((codel
== UNION_TYPE
|| codel
== RECORD_TYPE
)
6400 && TYPE_TRANSPARENT_AGGR (type
))
6401 && errtype
== ic_argpass
)
6403 tree memb
, marginal_memb
= NULL_TREE
;
6405 for (memb
= TYPE_FIELDS (type
); memb
; memb
= DECL_CHAIN (memb
))
6407 tree memb_type
= TREE_TYPE (memb
);
6409 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
6410 TYPE_MAIN_VARIANT (rhstype
)))
6413 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
6416 if (coder
== POINTER_TYPE
)
6418 tree ttl
= TREE_TYPE (memb_type
);
6419 tree ttr
= TREE_TYPE (rhstype
);
6421 /* Any non-function converts to a [const][volatile] void *
6422 and vice versa; otherwise, targets must be the same.
6423 Meanwhile, the lhs target must have all the qualifiers of
6425 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
6426 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
6427 || comp_target_types (location
, memb_type
, rhstype
))
6429 int lquals
= TYPE_QUALS (ttl
) & ~TYPE_QUAL_ATOMIC
;
6430 int rquals
= TYPE_QUALS (ttr
) & ~TYPE_QUAL_ATOMIC
;
6431 /* If this type won't generate any warnings, use it. */
6432 if (lquals
== rquals
6433 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
6434 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
6435 ? ((lquals
| rquals
) == rquals
)
6436 : ((lquals
| rquals
) == lquals
)))
6439 /* Keep looking for a better type, but remember this one. */
6441 marginal_memb
= memb
;
6445 /* Can convert integer zero to any pointer type. */
6446 if (null_pointer_constant
)
6448 rhs
= null_pointer_node
;
6453 if (memb
|| marginal_memb
)
6457 /* We have only a marginally acceptable member type;
6458 it needs a warning. */
6459 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
6460 tree ttr
= TREE_TYPE (rhstype
);
6462 /* Const and volatile mean something different for function
6463 types, so the usual warnings are not appropriate. */
6464 if (TREE_CODE (ttr
) == FUNCTION_TYPE
6465 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
6467 /* Because const and volatile on functions are
6468 restrictions that say the function will not do
6469 certain things, it is okay to use a const or volatile
6470 function where an ordinary one is wanted, but not
6472 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6473 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6474 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6475 OPT_Wdiscarded_qualifiers
,
6476 G_("passing argument %d of %qE "
6477 "makes %q#v qualified function "
6478 "pointer from unqualified"),
6479 G_("assignment makes %q#v qualified "
6480 "function pointer from "
6482 G_("initialization makes %q#v qualified "
6483 "function pointer from "
6485 G_("return makes %q#v qualified function "
6486 "pointer from unqualified"),
6487 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6489 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr
)
6490 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl
))
6491 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6492 OPT_Wdiscarded_qualifiers
,
6493 G_("passing argument %d of %qE discards "
6494 "%qv qualifier from pointer target type"),
6495 G_("assignment discards %qv qualifier "
6496 "from pointer target type"),
6497 G_("initialization discards %qv qualifier "
6498 "from pointer target type"),
6499 G_("return discards %qv qualifier from "
6500 "pointer target type"),
6501 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6503 memb
= marginal_memb
;
6506 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
6507 pedwarn (location
, OPT_Wpedantic
,
6508 "ISO C prohibits argument conversion to union type");
6510 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
6511 return build_constructor_single (type
, memb
, rhs
);
6515 /* Conversions among pointers */
6516 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
6517 && (coder
== codel
))
6519 tree ttl
= TREE_TYPE (type
);
6520 tree ttr
= TREE_TYPE (rhstype
);
6523 bool is_opaque_pointer
;
6524 int target_cmp
= 0; /* Cache comp_target_types () result. */
6528 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
6529 mvl
= (TYPE_ATOMIC (mvl
)
6530 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
),
6532 : TYPE_MAIN_VARIANT (mvl
));
6533 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
6534 mvr
= (TYPE_ATOMIC (mvr
)
6535 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
),
6537 : TYPE_MAIN_VARIANT (mvr
));
6538 /* Opaque pointers are treated like void pointers. */
6539 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
6541 /* The Plan 9 compiler permits a pointer to a struct to be
6542 automatically converted into a pointer to an anonymous field
6543 within the struct. */
6544 if (flag_plan9_extensions
6545 && RECORD_OR_UNION_TYPE_P (mvl
)
6546 && RECORD_OR_UNION_TYPE_P (mvr
)
6549 tree new_rhs
= convert_to_anonymous_field (location
, type
, rhs
);
6550 if (new_rhs
!= NULL_TREE
)
6553 rhstype
= TREE_TYPE (rhs
);
6554 coder
= TREE_CODE (rhstype
);
6555 ttr
= TREE_TYPE (rhstype
);
6556 mvr
= TYPE_MAIN_VARIANT (ttr
);
6560 /* C++ does not allow the implicit conversion void* -> T*. However,
6561 for the purpose of reducing the number of false positives, we
6562 tolerate the special case of
6566 where NULL is typically defined in C to be '(void *) 0'. */
6567 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
6568 warning_at (errtype
== ic_argpass
? expr_loc
: location
,
6570 "request for implicit conversion "
6571 "from %qT to %qT not permitted in C++", rhstype
, type
);
6573 /* See if the pointers point to incompatible address spaces. */
6574 asl
= TYPE_ADDR_SPACE (ttl
);
6575 asr
= TYPE_ADDR_SPACE (ttr
);
6576 if (!null_pointer_constant_p (rhs
)
6577 && asr
!= asl
&& !targetm
.addr_space
.subset_p (asr
, asl
))
6582 error_at (expr_loc
, "passing argument %d of %qE from pointer to "
6583 "non-enclosed address space", parmnum
, rname
);
6586 error_at (location
, "assignment from pointer to "
6587 "non-enclosed address space");
6590 error_at (location
, "initialization from pointer to "
6591 "non-enclosed address space");
6594 error_at (location
, "return from pointer to "
6595 "non-enclosed address space");
6600 return error_mark_node
;
6603 /* Check if the right-hand side has a format attribute but the
6604 left-hand side doesn't. */
6605 if (warn_suggest_attribute_format
6606 && check_missing_format_attribute (type
, rhstype
))
6611 warning_at (expr_loc
, OPT_Wsuggest_attribute_format
,
6612 "argument %d of %qE might be "
6613 "a candidate for a format attribute",
6617 warning_at (location
, OPT_Wsuggest_attribute_format
,
6618 "assignment left-hand side might be "
6619 "a candidate for a format attribute");
6622 warning_at (location
, OPT_Wsuggest_attribute_format
,
6623 "initialization left-hand side might be "
6624 "a candidate for a format attribute");
6627 warning_at (location
, OPT_Wsuggest_attribute_format
,
6628 "return type might be "
6629 "a candidate for a format attribute");
6636 /* Any non-function converts to a [const][volatile] void *
6637 and vice versa; otherwise, targets must be the same.
6638 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6639 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
6640 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
6641 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
6642 || is_opaque_pointer
6643 || ((c_common_unsigned_type (mvl
)
6644 == c_common_unsigned_type (mvr
))
6645 && (c_common_signed_type (mvl
)
6646 == c_common_signed_type (mvr
))
6647 && TYPE_ATOMIC (mvl
) == TYPE_ATOMIC (mvr
)))
6649 /* Warn about loss of qualifers from pointers to arrays with
6650 qualifiers on the element type. */
6651 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
6653 ttr
= strip_array_types (ttr
);
6654 ttl
= strip_array_types (ttl
);
6656 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
6657 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
6658 WARNING_FOR_QUALIFIERS (location
, expr_loc
,
6659 OPT_Wdiscarded_array_qualifiers
,
6660 G_("passing argument %d of %qE discards "
6661 "%qv qualifier from pointer target type"),
6662 G_("assignment discards %qv qualifier "
6663 "from pointer target type"),
6664 G_("initialization discards %qv qualifier "
6665 "from pointer target type"),
6666 G_("return discards %qv qualifier from "
6667 "pointer target type"),
6668 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6671 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6674 && !null_pointer_constant
6675 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
6676 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpedantic
,
6677 G_("ISO C forbids passing argument %d of "
6678 "%qE between function pointer "
6680 G_("ISO C forbids assignment between "
6681 "function pointer and %<void *%>"),
6682 G_("ISO C forbids initialization between "
6683 "function pointer and %<void *%>"),
6684 G_("ISO C forbids return between function "
6685 "pointer and %<void *%>"));
6686 /* Const and volatile mean something different for function types,
6687 so the usual warnings are not appropriate. */
6688 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
6689 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
6691 /* Don't warn about loss of qualifier for conversions from
6692 qualified void* to pointers to arrays with corresponding
6693 qualifier on the element type. */
6695 ttl
= strip_array_types (ttl
);
6697 /* Assignments between atomic and non-atomic objects are OK. */
6698 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
6699 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
6701 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6702 OPT_Wdiscarded_qualifiers
,
6703 G_("passing argument %d of %qE discards "
6704 "%qv qualifier from pointer target type"),
6705 G_("assignment discards %qv qualifier "
6706 "from pointer target type"),
6707 G_("initialization discards %qv qualifier "
6708 "from pointer target type"),
6709 G_("return discards %qv qualifier from "
6710 "pointer target type"),
6711 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6713 /* If this is not a case of ignoring a mismatch in signedness,
6715 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
6718 /* If there is a mismatch, do warn. */
6719 else if (warn_pointer_sign
)
6720 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpointer_sign
,
6721 G_("pointer targets in passing argument "
6722 "%d of %qE differ in signedness"),
6723 G_("pointer targets in assignment "
6724 "differ in signedness"),
6725 G_("pointer targets in initialization "
6726 "differ in signedness"),
6727 G_("pointer targets in return differ "
6730 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
6731 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6733 /* Because const and volatile on functions are restrictions
6734 that say the function will not do certain things,
6735 it is okay to use a const or volatile function
6736 where an ordinary one is wanted, but not vice-versa. */
6737 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6738 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6739 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6740 OPT_Wdiscarded_qualifiers
,
6741 G_("passing argument %d of %qE makes "
6742 "%q#v qualified function pointer "
6743 "from unqualified"),
6744 G_("assignment makes %q#v qualified function "
6745 "pointer from unqualified"),
6746 G_("initialization makes %q#v qualified "
6747 "function pointer from unqualified"),
6748 G_("return makes %q#v qualified function "
6749 "pointer from unqualified"),
6750 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6754 /* Avoid warning about the volatile ObjC EH puts on decls. */
6756 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6757 OPT_Wincompatible_pointer_types
,
6758 G_("passing argument %d of %qE from "
6759 "incompatible pointer type"),
6760 G_("assignment from incompatible pointer type"),
6761 G_("initialization from incompatible "
6763 G_("return from incompatible pointer type"));
6765 return convert (type
, rhs
);
6767 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
6769 /* ??? This should not be an error when inlining calls to
6770 unprototyped functions. */
6771 error_at (location
, "invalid use of non-lvalue array");
6772 return error_mark_node
;
6774 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
6776 /* An explicit constant 0 can convert to a pointer,
6777 or one that results from arithmetic, even including
6778 a cast to integer type. */
6779 if (!null_pointer_constant
)
6780 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6781 OPT_Wint_conversion
,
6782 G_("passing argument %d of %qE makes "
6783 "pointer from integer without a cast"),
6784 G_("assignment makes pointer from integer "
6786 G_("initialization makes pointer from "
6787 "integer without a cast"),
6788 G_("return makes pointer from integer "
6791 return convert (type
, rhs
);
6793 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
6795 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6796 OPT_Wint_conversion
,
6797 G_("passing argument %d of %qE makes integer "
6798 "from pointer without a cast"),
6799 G_("assignment makes integer from pointer "
6801 G_("initialization makes integer from pointer "
6803 G_("return makes integer from pointer "
6805 return convert (type
, rhs
);
6807 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
6810 bool save
= in_late_binary_op
;
6811 in_late_binary_op
= true;
6812 ret
= convert (type
, rhs
);
6813 in_late_binary_op
= save
;
6820 error_at (expr_loc
, "incompatible type for argument %d of %qE", parmnum
,
6822 inform ((fundecl
&& !DECL_IS_BUILTIN (fundecl
))
6823 ? DECL_SOURCE_LOCATION (fundecl
) : expr_loc
,
6824 "expected %qT but argument is of type %qT", type
, rhstype
);
6827 error_at (location
, "incompatible types when assigning to type %qT from "
6828 "type %qT", type
, rhstype
);
6832 "incompatible types when initializing type %qT using type %qT",
6837 "incompatible types when returning type %qT but %qT was "
6838 "expected", rhstype
, type
);
6844 return error_mark_node
;
6847 /* If VALUE is a compound expr all of whose expressions are constant, then
6848 return its value. Otherwise, return error_mark_node.
6850 This is for handling COMPOUND_EXPRs as initializer elements
6851 which is allowed with a warning when -pedantic is specified. */
6854 valid_compound_expr_initializer (tree value
, tree endtype
)
6856 if (TREE_CODE (value
) == COMPOUND_EXPR
)
6858 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
6860 return error_mark_node
;
6861 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
6864 else if (!initializer_constant_valid_p (value
, endtype
))
6865 return error_mark_node
;
6870 /* Perform appropriate conversions on the initial value of a variable,
6871 store it in the declaration DECL,
6872 and print any error messages that are appropriate.
6873 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6874 If the init is invalid, store an ERROR_MARK.
6876 INIT_LOC is the location of the initial value. */
6879 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
6884 /* If variable's type was invalidly declared, just ignore it. */
6886 type
= TREE_TYPE (decl
);
6887 if (TREE_CODE (type
) == ERROR_MARK
)
6890 /* Digest the specified initializer into an expression. */
6893 npc
= null_pointer_constant_p (init
);
6894 value
= digest_init (init_loc
, type
, init
, origtype
, npc
,
6895 true, TREE_STATIC (decl
));
6897 /* Store the expression if valid; else report error. */
6899 if (!in_system_header_at (input_location
)
6900 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
6901 warning (OPT_Wtraditional
, "traditional C rejects automatic "
6902 "aggregate initialization");
6904 if (value
!= error_mark_node
|| TREE_CODE (decl
) != FUNCTION_DECL
)
6905 DECL_INITIAL (decl
) = value
;
6907 /* ANSI wants warnings about out-of-range constant initializers. */
6908 STRIP_TYPE_NOPS (value
);
6909 if (TREE_STATIC (decl
))
6910 constant_expression_warning (value
);
6912 /* Check if we need to set array size from compound literal size. */
6913 if (TREE_CODE (type
) == ARRAY_TYPE
6914 && TYPE_DOMAIN (type
) == NULL_TREE
6915 && value
!= error_mark_node
)
6917 tree inside_init
= init
;
6919 STRIP_TYPE_NOPS (inside_init
);
6920 inside_init
= fold (inside_init
);
6922 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6924 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6926 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
6928 /* For int foo[] = (int [3]){1}; we need to set array size
6929 now since later on array initializer will be just the
6930 brace enclosed list of the compound literal. */
6931 tree etype
= strip_array_types (TREE_TYPE (decl
));
6932 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6933 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
6935 layout_decl (cldecl
, 0);
6937 = c_build_qualified_type (type
, TYPE_QUALS (etype
));
6943 /* Methods for storing and printing names for error messages. */
6945 /* Implement a spelling stack that allows components of a name to be pushed
6946 and popped. Each element on the stack is this structure. */
6953 unsigned HOST_WIDE_INT i
;
6958 #define SPELLING_STRING 1
6959 #define SPELLING_MEMBER 2
6960 #define SPELLING_BOUNDS 3
6962 static struct spelling
*spelling
; /* Next stack element (unused). */
6963 static struct spelling
*spelling_base
; /* Spelling stack base. */
6964 static int spelling_size
; /* Size of the spelling stack. */
6966 /* Macros to save and restore the spelling stack around push_... functions.
6967 Alternative to SAVE_SPELLING_STACK. */
6969 #define SPELLING_DEPTH() (spelling - spelling_base)
6970 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6972 /* Push an element on the spelling stack with type KIND and assign VALUE
6975 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6977 int depth = SPELLING_DEPTH (); \
6979 if (depth >= spelling_size) \
6981 spelling_size += 10; \
6982 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6984 RESTORE_SPELLING_DEPTH (depth); \
6987 spelling->kind = (KIND); \
6988 spelling->MEMBER = (VALUE); \
6992 /* Push STRING on the stack. Printed literally. */
6995 push_string (const char *string
)
6997 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
7000 /* Push a member name on the stack. Printed as '.' STRING. */
7003 push_member_name (tree decl
)
7005 const char *const string
7007 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
7008 : _("<anonymous>"));
7009 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
7012 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7015 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
7017 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
7020 /* Compute the maximum size in bytes of the printed spelling. */
7023 spelling_length (void)
7028 for (p
= spelling_base
; p
< spelling
; p
++)
7030 if (p
->kind
== SPELLING_BOUNDS
)
7033 size
+= strlen (p
->u
.s
) + 1;
7039 /* Print the spelling to BUFFER and return it. */
7042 print_spelling (char *buffer
)
7047 for (p
= spelling_base
; p
< spelling
; p
++)
7048 if (p
->kind
== SPELLING_BOUNDS
)
7050 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
7056 if (p
->kind
== SPELLING_MEMBER
)
7058 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
7065 /* Digest the parser output INIT as an initializer for type TYPE.
7066 Return a C expression of type TYPE to represent the initial value.
7068 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7070 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7072 If INIT is a string constant, STRICT_STRING is true if it is
7073 unparenthesized or we should not warn here for it being parenthesized.
7074 For other types of INIT, STRICT_STRING is not used.
7076 INIT_LOC is the location of the INIT.
7078 REQUIRE_CONSTANT requests an error if non-constant initializers or
7079 elements are seen. */
7082 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
7083 bool null_pointer_constant
, bool strict_string
,
7084 int require_constant
)
7086 enum tree_code code
= TREE_CODE (type
);
7087 tree inside_init
= init
;
7088 tree semantic_type
= NULL_TREE
;
7089 bool maybe_const
= true;
7091 if (type
== error_mark_node
7093 || error_operand_p (init
))
7094 return error_mark_node
;
7096 STRIP_TYPE_NOPS (inside_init
);
7098 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
7100 semantic_type
= TREE_TYPE (inside_init
);
7101 inside_init
= TREE_OPERAND (inside_init
, 0);
7103 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
7104 inside_init
= decl_constant_value_for_optimization (inside_init
);
7106 /* Initialization of an array of chars from a string constant
7107 optionally enclosed in braces. */
7109 if (code
== ARRAY_TYPE
&& inside_init
7110 && TREE_CODE (inside_init
) == STRING_CST
)
7113 = (TYPE_ATOMIC (TREE_TYPE (type
))
7114 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
7116 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
7117 /* Note that an array could be both an array of character type
7118 and an array of wchar_t if wchar_t is signed char or unsigned
7120 bool char_array
= (typ1
== char_type_node
7121 || typ1
== signed_char_type_node
7122 || typ1
== unsigned_char_type_node
);
7123 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
7124 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
7125 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
7127 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
7130 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
7131 expr
.value
= inside_init
;
7132 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
7133 expr
.original_type
= NULL
;
7134 maybe_warn_string_init (init_loc
, type
, expr
);
7136 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
7137 pedwarn_init (init_loc
, OPT_Wpedantic
,
7138 "initialization of a flexible array member");
7140 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7141 TYPE_MAIN_VARIANT (type
)))
7146 if (typ2
!= char_type_node
)
7148 error_init (init_loc
, "char-array initialized from wide "
7150 return error_mark_node
;
7155 if (typ2
== char_type_node
)
7157 error_init (init_loc
, "wide character array initialized "
7158 "from non-wide string");
7159 return error_mark_node
;
7161 else if (!comptypes(typ1
, typ2
))
7163 error_init (init_loc
, "wide character array initialized "
7164 "from incompatible wide string");
7165 return error_mark_node
;
7169 TREE_TYPE (inside_init
) = type
;
7170 if (TYPE_DOMAIN (type
) != NULL_TREE
7171 && TYPE_SIZE (type
) != NULL_TREE
7172 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
7174 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
7176 /* Subtract the size of a single (possibly wide) character
7177 because it's ok to ignore the terminating null char
7178 that is counted in the length of the constant. */
7179 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
7181 - (TYPE_PRECISION (typ1
)
7183 pedwarn_init (init_loc
, 0,
7184 ("initializer-string for array of chars "
7186 else if (warn_cxx_compat
7187 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
), len
))
7188 warning_at (init_loc
, OPT_Wc___compat
,
7189 ("initializer-string for array chars "
7190 "is too long for C++"));
7195 else if (INTEGRAL_TYPE_P (typ1
))
7197 error_init (init_loc
, "array of inappropriate type initialized "
7198 "from string constant");
7199 return error_mark_node
;
7203 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7204 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7205 below and handle as a constructor. */
7206 if (code
== VECTOR_TYPE
7207 && VECTOR_TYPE_P (TREE_TYPE (inside_init
))
7208 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
7209 && TREE_CONSTANT (inside_init
))
7211 if (TREE_CODE (inside_init
) == VECTOR_CST
7212 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7213 TYPE_MAIN_VARIANT (type
)))
7216 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
7218 unsigned HOST_WIDE_INT ix
;
7220 bool constant_p
= true;
7222 /* Iterate through elements and check if all constructor
7223 elements are *_CSTs. */
7224 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
7225 if (!CONSTANT_CLASS_P (value
))
7232 return build_vector_from_ctor (type
,
7233 CONSTRUCTOR_ELTS (inside_init
));
7237 if (warn_sequence_point
)
7238 verify_sequence_points (inside_init
);
7240 /* Any type can be initialized
7241 from an expression of the same type, optionally with braces. */
7243 if (inside_init
&& TREE_TYPE (inside_init
) != NULL_TREE
7244 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7245 TYPE_MAIN_VARIANT (type
))
7246 || (code
== ARRAY_TYPE
7247 && comptypes (TREE_TYPE (inside_init
), type
))
7248 || (code
== VECTOR_TYPE
7249 && comptypes (TREE_TYPE (inside_init
), type
))
7250 || (code
== POINTER_TYPE
7251 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
7252 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
7253 TREE_TYPE (type
)))))
7255 if (code
== POINTER_TYPE
)
7257 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
7259 if (TREE_CODE (inside_init
) == STRING_CST
7260 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
7261 inside_init
= array_to_pointer_conversion
7262 (init_loc
, inside_init
);
7265 error_init (init_loc
, "invalid use of non-lvalue array");
7266 return error_mark_node
;
7271 if (code
== VECTOR_TYPE
)
7272 /* Although the types are compatible, we may require a
7274 inside_init
= convert (type
, inside_init
);
7276 if (require_constant
7277 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
7279 /* As an extension, allow initializing objects with static storage
7280 duration with compound literals (which are then treated just as
7281 the brace enclosed list they contain). Also allow this for
7282 vectors, as we can only assign them with compound literals. */
7283 if (flag_isoc99
&& code
!= VECTOR_TYPE
)
7284 pedwarn_init (init_loc
, OPT_Wpedantic
, "initializer element "
7286 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
7287 inside_init
= DECL_INITIAL (decl
);
7290 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
7291 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
7293 error_init (init_loc
, "array initialized from non-constant array "
7295 return error_mark_node
;
7298 /* Compound expressions can only occur here if -Wpedantic or
7299 -pedantic-errors is specified. In the later case, we always want
7300 an error. In the former case, we simply want a warning. */
7301 if (require_constant
&& pedantic
7302 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
7305 = valid_compound_expr_initializer (inside_init
,
7306 TREE_TYPE (inside_init
));
7307 if (inside_init
== error_mark_node
)
7308 error_init (init_loc
, "initializer element is not constant");
7310 pedwarn_init (init_loc
, OPT_Wpedantic
,
7311 "initializer element is not constant");
7312 if (flag_pedantic_errors
)
7313 inside_init
= error_mark_node
;
7315 else if (require_constant
7316 && !initializer_constant_valid_p (inside_init
,
7317 TREE_TYPE (inside_init
)))
7319 error_init (init_loc
, "initializer element is not constant");
7320 inside_init
= error_mark_node
;
7322 else if (require_constant
&& !maybe_const
)
7323 pedwarn_init (init_loc
, OPT_Wpedantic
,
7324 "initializer element is not a constant expression");
7326 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7327 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
7328 inside_init
= convert_for_assignment (init_loc
, UNKNOWN_LOCATION
,
7329 type
, inside_init
, origtype
,
7330 ic_init
, null_pointer_constant
,
7331 NULL_TREE
, NULL_TREE
, 0);
7335 /* Handle scalar types, including conversions. */
7337 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
7338 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
7339 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
)
7341 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
7342 && (TREE_CODE (init
) == STRING_CST
7343 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
7344 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
7346 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
7349 = convert_for_assignment (init_loc
, UNKNOWN_LOCATION
, type
,
7350 inside_init
, origtype
, ic_init
,
7351 null_pointer_constant
, NULL_TREE
, NULL_TREE
,
7354 /* Check to see if we have already given an error message. */
7355 if (inside_init
== error_mark_node
)
7357 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
7359 error_init (init_loc
, "initializer element is not constant");
7360 inside_init
= error_mark_node
;
7362 else if (require_constant
7363 && !initializer_constant_valid_p (inside_init
,
7364 TREE_TYPE (inside_init
)))
7366 error_init (init_loc
, "initializer element is not computable at "
7368 inside_init
= error_mark_node
;
7370 else if (require_constant
&& !maybe_const
)
7371 pedwarn_init (init_loc
, OPT_Wpedantic
,
7372 "initializer element is not a constant expression");
7377 /* Come here only for records and arrays. */
7379 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
7381 error_init (init_loc
, "variable-sized object may not be initialized");
7382 return error_mark_node
;
7385 error_init (init_loc
, "invalid initializer");
7386 return error_mark_node
;
7389 /* Handle initializers that use braces. */
7391 /* Type of object we are accumulating a constructor for.
7392 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7393 static tree constructor_type
;
7395 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7397 static tree constructor_fields
;
7399 /* For an ARRAY_TYPE, this is the specified index
7400 at which to store the next element we get. */
7401 static tree constructor_index
;
7403 /* For an ARRAY_TYPE, this is the maximum index. */
7404 static tree constructor_max_index
;
7406 /* For a RECORD_TYPE, this is the first field not yet written out. */
7407 static tree constructor_unfilled_fields
;
7409 /* For an ARRAY_TYPE, this is the index of the first element
7410 not yet written out. */
7411 static tree constructor_unfilled_index
;
7413 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7414 This is so we can generate gaps between fields, when appropriate. */
7415 static tree constructor_bit_index
;
7417 /* If we are saving up the elements rather than allocating them,
7418 this is the list of elements so far (in reverse order,
7419 most recent first). */
7420 static vec
<constructor_elt
, va_gc
> *constructor_elements
;
7422 /* 1 if constructor should be incrementally stored into a constructor chain,
7423 0 if all the elements should be kept in AVL tree. */
7424 static int constructor_incremental
;
7426 /* 1 if so far this constructor's elements are all compile-time constants. */
7427 static int constructor_constant
;
7429 /* 1 if so far this constructor's elements are all valid address constants. */
7430 static int constructor_simple
;
7432 /* 1 if this constructor has an element that cannot be part of a
7433 constant expression. */
7434 static int constructor_nonconst
;
7436 /* 1 if this constructor is erroneous so far. */
7437 static int constructor_erroneous
;
7439 /* 1 if this constructor is the universal zero initializer { 0 }. */
7440 static int constructor_zeroinit
;
7442 /* Structure for managing pending initializer elements, organized as an
7447 struct init_node
*left
, *right
;
7448 struct init_node
*parent
;
7455 /* Tree of pending elements at this constructor level.
7456 These are elements encountered out of order
7457 which belong at places we haven't reached yet in actually
7459 Will never hold tree nodes across GC runs. */
7460 static struct init_node
*constructor_pending_elts
;
7462 /* The SPELLING_DEPTH of this constructor. */
7463 static int constructor_depth
;
7465 /* DECL node for which an initializer is being read.
7466 0 means we are reading a constructor expression
7467 such as (struct foo) {...}. */
7468 static tree constructor_decl
;
7470 /* Nonzero if this is an initializer for a top-level decl. */
7471 static int constructor_top_level
;
7473 /* Nonzero if there were any member designators in this initializer. */
7474 static int constructor_designated
;
7476 /* Nesting depth of designator list. */
7477 static int designator_depth
;
7479 /* Nonzero if there were diagnosed errors in this designator list. */
7480 static int designator_erroneous
;
7483 /* This stack has a level for each implicit or explicit level of
7484 structuring in the initializer, including the outermost one. It
7485 saves the values of most of the variables above. */
7487 struct constructor_range_stack
;
7489 struct constructor_stack
7491 struct constructor_stack
*next
;
7496 tree unfilled_index
;
7497 tree unfilled_fields
;
7499 vec
<constructor_elt
, va_gc
> *elements
;
7500 struct init_node
*pending_elts
;
7503 /* If value nonzero, this value should replace the entire
7504 constructor at this level. */
7505 struct c_expr replacement_value
;
7506 struct constructor_range_stack
*range_stack
;
7515 int designator_depth
;
7518 static struct constructor_stack
*constructor_stack
;
7520 /* This stack represents designators from some range designator up to
7521 the last designator in the list. */
7523 struct constructor_range_stack
7525 struct constructor_range_stack
*next
, *prev
;
7526 struct constructor_stack
*stack
;
7533 static struct constructor_range_stack
*constructor_range_stack
;
7535 /* This stack records separate initializers that are nested.
7536 Nested initializers can't happen in ANSI C, but GNU C allows them
7537 in cases like { ... (struct foo) { ... } ... }. */
7539 struct initializer_stack
7541 struct initializer_stack
*next
;
7543 struct constructor_stack
*constructor_stack
;
7544 struct constructor_range_stack
*constructor_range_stack
;
7545 vec
<constructor_elt
, va_gc
> *elements
;
7546 struct spelling
*spelling
;
7547 struct spelling
*spelling_base
;
7550 char require_constant_value
;
7551 char require_constant_elements
;
7552 rich_location
*missing_brace_richloc
;
7555 static struct initializer_stack
*initializer_stack
;
7557 /* Prepare to parse and output the initializer for variable DECL. */
7560 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
,
7561 rich_location
*richloc
)
7564 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
7566 p
->decl
= constructor_decl
;
7567 p
->require_constant_value
= require_constant_value
;
7568 p
->require_constant_elements
= require_constant_elements
;
7569 p
->constructor_stack
= constructor_stack
;
7570 p
->constructor_range_stack
= constructor_range_stack
;
7571 p
->elements
= constructor_elements
;
7572 p
->spelling
= spelling
;
7573 p
->spelling_base
= spelling_base
;
7574 p
->spelling_size
= spelling_size
;
7575 p
->top_level
= constructor_top_level
;
7576 p
->next
= initializer_stack
;
7577 p
->missing_brace_richloc
= richloc
;
7578 initializer_stack
= p
;
7580 constructor_decl
= decl
;
7581 constructor_designated
= 0;
7582 constructor_top_level
= top_level
;
7584 if (decl
!= NULL_TREE
&& decl
!= error_mark_node
)
7586 require_constant_value
= TREE_STATIC (decl
);
7587 require_constant_elements
7588 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
7589 /* For a scalar, you can always use any value to initialize,
7590 even within braces. */
7591 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)));
7592 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
7596 require_constant_value
= 0;
7597 require_constant_elements
= 0;
7598 locus
= _("(anonymous)");
7601 constructor_stack
= 0;
7602 constructor_range_stack
= 0;
7604 found_missing_braces
= 0;
7608 RESTORE_SPELLING_DEPTH (0);
7611 push_string (locus
);
7617 struct initializer_stack
*p
= initializer_stack
;
7619 /* Free the whole constructor stack of this initializer. */
7620 while (constructor_stack
)
7622 struct constructor_stack
*q
= constructor_stack
;
7623 constructor_stack
= q
->next
;
7627 gcc_assert (!constructor_range_stack
);
7629 /* Pop back to the data of the outer initializer (if any). */
7630 free (spelling_base
);
7632 constructor_decl
= p
->decl
;
7633 require_constant_value
= p
->require_constant_value
;
7634 require_constant_elements
= p
->require_constant_elements
;
7635 constructor_stack
= p
->constructor_stack
;
7636 constructor_range_stack
= p
->constructor_range_stack
;
7637 constructor_elements
= p
->elements
;
7638 spelling
= p
->spelling
;
7639 spelling_base
= p
->spelling_base
;
7640 spelling_size
= p
->spelling_size
;
7641 constructor_top_level
= p
->top_level
;
7642 initializer_stack
= p
->next
;
7646 /* Call here when we see the initializer is surrounded by braces.
7647 This is instead of a call to push_init_level;
7648 it is matched by a call to pop_init_level.
7650 TYPE is the type to initialize, for a constructor expression.
7651 For an initializer for a decl, TYPE is zero. */
7654 really_start_incremental_init (tree type
)
7656 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
7658 if (type
== NULL_TREE
)
7659 type
= TREE_TYPE (constructor_decl
);
7661 if (VECTOR_TYPE_P (type
)
7662 && TYPE_VECTOR_OPAQUE (type
))
7663 error ("opaque vector types cannot be initialized");
7665 p
->type
= constructor_type
;
7666 p
->fields
= constructor_fields
;
7667 p
->index
= constructor_index
;
7668 p
->max_index
= constructor_max_index
;
7669 p
->unfilled_index
= constructor_unfilled_index
;
7670 p
->unfilled_fields
= constructor_unfilled_fields
;
7671 p
->bit_index
= constructor_bit_index
;
7672 p
->elements
= constructor_elements
;
7673 p
->constant
= constructor_constant
;
7674 p
->simple
= constructor_simple
;
7675 p
->nonconst
= constructor_nonconst
;
7676 p
->erroneous
= constructor_erroneous
;
7677 p
->pending_elts
= constructor_pending_elts
;
7678 p
->depth
= constructor_depth
;
7679 p
->replacement_value
.value
= 0;
7680 p
->replacement_value
.original_code
= ERROR_MARK
;
7681 p
->replacement_value
.original_type
= NULL
;
7685 p
->incremental
= constructor_incremental
;
7686 p
->designated
= constructor_designated
;
7687 p
->designator_depth
= designator_depth
;
7689 constructor_stack
= p
;
7691 constructor_constant
= 1;
7692 constructor_simple
= 1;
7693 constructor_nonconst
= 0;
7694 constructor_depth
= SPELLING_DEPTH ();
7695 constructor_elements
= NULL
;
7696 constructor_pending_elts
= 0;
7697 constructor_type
= type
;
7698 constructor_incremental
= 1;
7699 constructor_designated
= 0;
7700 constructor_zeroinit
= 1;
7701 designator_depth
= 0;
7702 designator_erroneous
= 0;
7704 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
7706 constructor_fields
= TYPE_FIELDS (constructor_type
);
7707 /* Skip any nameless bit fields at the beginning. */
7708 while (constructor_fields
!= NULL_TREE
7709 && DECL_C_BIT_FIELD (constructor_fields
)
7710 && DECL_NAME (constructor_fields
) == NULL_TREE
)
7711 constructor_fields
= DECL_CHAIN (constructor_fields
);
7713 constructor_unfilled_fields
= constructor_fields
;
7714 constructor_bit_index
= bitsize_zero_node
;
7716 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7718 if (TYPE_DOMAIN (constructor_type
))
7720 constructor_max_index
7721 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7723 /* Detect non-empty initializations of zero-length arrays. */
7724 if (constructor_max_index
== NULL_TREE
7725 && TYPE_SIZE (constructor_type
))
7726 constructor_max_index
= integer_minus_one_node
;
7728 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7729 to initialize VLAs will cause a proper error; avoid tree
7730 checking errors as well by setting a safe value. */
7731 if (constructor_max_index
7732 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7733 constructor_max_index
= integer_minus_one_node
;
7736 = convert (bitsizetype
,
7737 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7741 constructor_index
= bitsize_zero_node
;
7742 constructor_max_index
= NULL_TREE
;
7745 constructor_unfilled_index
= constructor_index
;
7747 else if (VECTOR_TYPE_P (constructor_type
))
7749 /* Vectors are like simple fixed-size arrays. */
7750 constructor_max_index
=
7751 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7752 constructor_index
= bitsize_zero_node
;
7753 constructor_unfilled_index
= constructor_index
;
7757 /* Handle the case of int x = {5}; */
7758 constructor_fields
= constructor_type
;
7759 constructor_unfilled_fields
= constructor_type
;
7763 extern location_t last_init_list_comma
;
7765 /* Called when we see an open brace for a nested initializer. Finish
7766 off any pending levels with implicit braces. */
7768 finish_implicit_inits (location_t loc
, struct obstack
*braced_init_obstack
)
7770 while (constructor_stack
->implicit
)
7772 if (RECORD_OR_UNION_TYPE_P (constructor_type
)
7773 && constructor_fields
== NULL_TREE
)
7774 process_init_element (input_location
,
7775 pop_init_level (loc
, 1, braced_init_obstack
,
7776 last_init_list_comma
),
7777 true, braced_init_obstack
);
7778 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
7779 && constructor_max_index
7780 && tree_int_cst_lt (constructor_max_index
,
7782 process_init_element (input_location
,
7783 pop_init_level (loc
, 1, braced_init_obstack
,
7784 last_init_list_comma
),
7785 true, braced_init_obstack
);
7791 /* Push down into a subobject, for initialization.
7792 If this is for an explicit set of braces, IMPLICIT is 0.
7793 If it is because the next element belongs at a lower level,
7794 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7797 push_init_level (location_t loc
, int implicit
,
7798 struct obstack
*braced_init_obstack
)
7800 struct constructor_stack
*p
;
7801 tree value
= NULL_TREE
;
7803 /* Unless this is an explicit brace, we need to preserve previous
7807 if (RECORD_OR_UNION_TYPE_P (constructor_type
) && constructor_fields
)
7808 value
= find_init_member (constructor_fields
, braced_init_obstack
);
7809 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7810 value
= find_init_member (constructor_index
, braced_init_obstack
);
7813 p
= XNEW (struct constructor_stack
);
7814 p
->type
= constructor_type
;
7815 p
->fields
= constructor_fields
;
7816 p
->index
= constructor_index
;
7817 p
->max_index
= constructor_max_index
;
7818 p
->unfilled_index
= constructor_unfilled_index
;
7819 p
->unfilled_fields
= constructor_unfilled_fields
;
7820 p
->bit_index
= constructor_bit_index
;
7821 p
->elements
= constructor_elements
;
7822 p
->constant
= constructor_constant
;
7823 p
->simple
= constructor_simple
;
7824 p
->nonconst
= constructor_nonconst
;
7825 p
->erroneous
= constructor_erroneous
;
7826 p
->pending_elts
= constructor_pending_elts
;
7827 p
->depth
= constructor_depth
;
7828 p
->replacement_value
.value
= NULL_TREE
;
7829 p
->replacement_value
.original_code
= ERROR_MARK
;
7830 p
->replacement_value
.original_type
= NULL
;
7831 p
->implicit
= implicit
;
7833 p
->incremental
= constructor_incremental
;
7834 p
->designated
= constructor_designated
;
7835 p
->designator_depth
= designator_depth
;
7836 p
->next
= constructor_stack
;
7838 constructor_stack
= p
;
7840 constructor_constant
= 1;
7841 constructor_simple
= 1;
7842 constructor_nonconst
= 0;
7843 constructor_depth
= SPELLING_DEPTH ();
7844 constructor_elements
= NULL
;
7845 constructor_incremental
= 1;
7846 constructor_designated
= 0;
7847 constructor_pending_elts
= 0;
7850 p
->range_stack
= constructor_range_stack
;
7851 constructor_range_stack
= 0;
7852 designator_depth
= 0;
7853 designator_erroneous
= 0;
7856 /* Don't die if an entire brace-pair level is superfluous
7857 in the containing level. */
7858 if (constructor_type
== NULL_TREE
)
7860 else if (RECORD_OR_UNION_TYPE_P (constructor_type
))
7862 /* Don't die if there are extra init elts at the end. */
7863 if (constructor_fields
== NULL_TREE
)
7864 constructor_type
= NULL_TREE
;
7867 constructor_type
= TREE_TYPE (constructor_fields
);
7868 push_member_name (constructor_fields
);
7869 constructor_depth
++;
7871 /* If upper initializer is designated, then mark this as
7872 designated too to prevent bogus warnings. */
7873 constructor_designated
= p
->designated
;
7875 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7877 constructor_type
= TREE_TYPE (constructor_type
);
7878 push_array_bounds (tree_to_uhwi (constructor_index
));
7879 constructor_depth
++;
7882 if (constructor_type
== NULL_TREE
)
7884 error_init (loc
, "extra brace group at end of initializer");
7885 constructor_fields
= NULL_TREE
;
7886 constructor_unfilled_fields
= NULL_TREE
;
7890 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
7892 constructor_constant
= TREE_CONSTANT (value
);
7893 constructor_simple
= TREE_STATIC (value
);
7894 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
7895 constructor_elements
= CONSTRUCTOR_ELTS (value
);
7896 if (!vec_safe_is_empty (constructor_elements
)
7897 && (TREE_CODE (constructor_type
) == RECORD_TYPE
7898 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
7899 set_nonincremental_init (braced_init_obstack
);
7904 found_missing_braces
= 1;
7905 if (initializer_stack
->missing_brace_richloc
)
7906 initializer_stack
->missing_brace_richloc
->add_fixit_insert_before
7910 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
7912 constructor_fields
= TYPE_FIELDS (constructor_type
);
7913 /* Skip any nameless bit fields at the beginning. */
7914 while (constructor_fields
!= NULL_TREE
7915 && DECL_C_BIT_FIELD (constructor_fields
)
7916 && DECL_NAME (constructor_fields
) == NULL_TREE
)
7917 constructor_fields
= DECL_CHAIN (constructor_fields
);
7919 constructor_unfilled_fields
= constructor_fields
;
7920 constructor_bit_index
= bitsize_zero_node
;
7922 else if (VECTOR_TYPE_P (constructor_type
))
7924 /* Vectors are like simple fixed-size arrays. */
7925 constructor_max_index
=
7926 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7927 constructor_index
= bitsize_int (0);
7928 constructor_unfilled_index
= constructor_index
;
7930 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7932 if (TYPE_DOMAIN (constructor_type
))
7934 constructor_max_index
7935 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7937 /* Detect non-empty initializations of zero-length arrays. */
7938 if (constructor_max_index
== NULL_TREE
7939 && TYPE_SIZE (constructor_type
))
7940 constructor_max_index
= integer_minus_one_node
;
7942 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7943 to initialize VLAs will cause a proper error; avoid tree
7944 checking errors as well by setting a safe value. */
7945 if (constructor_max_index
7946 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7947 constructor_max_index
= integer_minus_one_node
;
7950 = convert (bitsizetype
,
7951 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7954 constructor_index
= bitsize_zero_node
;
7956 constructor_unfilled_index
= constructor_index
;
7957 if (value
&& TREE_CODE (value
) == STRING_CST
)
7959 /* We need to split the char/wchar array into individual
7960 characters, so that we don't have to special case it
7962 set_nonincremental_init_from_string (value
, braced_init_obstack
);
7967 if (constructor_type
!= error_mark_node
)
7968 warning_init (input_location
, 0, "braces around scalar initializer");
7969 constructor_fields
= constructor_type
;
7970 constructor_unfilled_fields
= constructor_type
;
7974 /* At the end of an implicit or explicit brace level,
7975 finish up that level of constructor. If a single expression
7976 with redundant braces initialized that level, return the
7977 c_expr structure for that expression. Otherwise, the original_code
7978 element is set to ERROR_MARK.
7979 If we were outputting the elements as they are read, return 0 as the value
7980 from inner levels (process_init_element ignores that),
7981 but return error_mark_node as the value from the outermost level
7982 (that's what we want to put in DECL_INITIAL).
7983 Otherwise, return a CONSTRUCTOR expression as the value. */
7986 pop_init_level (location_t loc
, int implicit
,
7987 struct obstack
*braced_init_obstack
,
7988 location_t insert_before
)
7990 struct constructor_stack
*p
;
7992 ret
.value
= NULL_TREE
;
7993 ret
.original_code
= ERROR_MARK
;
7994 ret
.original_type
= NULL
;
7998 /* When we come to an explicit close brace,
7999 pop any inner levels that didn't have explicit braces. */
8000 while (constructor_stack
->implicit
)
8001 process_init_element (input_location
,
8002 pop_init_level (loc
, 1, braced_init_obstack
,
8004 true, braced_init_obstack
);
8005 gcc_assert (!constructor_range_stack
);
8008 if (initializer_stack
->missing_brace_richloc
)
8009 initializer_stack
->missing_brace_richloc
->add_fixit_insert_before
8010 (insert_before
, "}");
8012 /* Now output all pending elements. */
8013 constructor_incremental
= 1;
8014 output_pending_init_elements (1, braced_init_obstack
);
8016 p
= constructor_stack
;
8018 /* Error for initializing a flexible array member, or a zero-length
8019 array member in an inappropriate context. */
8020 if (constructor_type
&& constructor_fields
8021 && TREE_CODE (constructor_type
) == ARRAY_TYPE
8022 && TYPE_DOMAIN (constructor_type
)
8023 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
8025 /* Silently discard empty initializations. The parser will
8026 already have pedwarned for empty brackets. */
8027 if (integer_zerop (constructor_unfilled_index
))
8028 constructor_type
= NULL_TREE
;
8031 gcc_assert (!TYPE_SIZE (constructor_type
));
8033 if (constructor_depth
> 2)
8034 error_init (loc
, "initialization of flexible array member in a nested context");
8036 pedwarn_init (loc
, OPT_Wpedantic
,
8037 "initialization of a flexible array member");
8039 /* We have already issued an error message for the existence
8040 of a flexible array member not at the end of the structure.
8041 Discard the initializer so that we do not die later. */
8042 if (DECL_CHAIN (constructor_fields
) != NULL_TREE
)
8043 constructor_type
= NULL_TREE
;
8047 switch (vec_safe_length (constructor_elements
))
8050 /* Initialization with { } counts as zeroinit. */
8051 constructor_zeroinit
= 1;
8054 /* This might be zeroinit as well. */
8055 if (integer_zerop ((*constructor_elements
)[0].value
))
8056 constructor_zeroinit
= 1;
8059 /* If the constructor has more than one element, it can't be { 0 }. */
8060 constructor_zeroinit
= 0;
8064 /* Warn when some structs are initialized with direct aggregation. */
8065 if (!implicit
&& found_missing_braces
&& warn_missing_braces
8066 && !constructor_zeroinit
)
8068 gcc_assert (initializer_stack
->missing_brace_richloc
);
8069 warning_at_rich_loc (initializer_stack
->missing_brace_richloc
,
8070 OPT_Wmissing_braces
,
8071 "missing braces around initializer");
8074 /* Warn when some struct elements are implicitly initialized to zero. */
8075 if (warn_missing_field_initializers
8077 && TREE_CODE (constructor_type
) == RECORD_TYPE
8078 && constructor_unfilled_fields
)
8080 /* Do not warn for flexible array members or zero-length arrays. */
8081 while (constructor_unfilled_fields
8082 && (!DECL_SIZE (constructor_unfilled_fields
)
8083 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
8084 constructor_unfilled_fields
= DECL_CHAIN (constructor_unfilled_fields
);
8086 if (constructor_unfilled_fields
8087 /* Do not warn if this level of the initializer uses member
8088 designators; it is likely to be deliberate. */
8089 && !constructor_designated
8090 /* Do not warn about initializing with { 0 } or with { }. */
8091 && !constructor_zeroinit
)
8093 if (warning_at (input_location
, OPT_Wmissing_field_initializers
,
8094 "missing initializer for field %qD of %qT",
8095 constructor_unfilled_fields
,
8097 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields
),
8098 "%qD declared here", constructor_unfilled_fields
);
8102 /* Pad out the end of the structure. */
8103 if (p
->replacement_value
.value
)
8104 /* If this closes a superfluous brace pair,
8105 just pass out the element between them. */
8106 ret
= p
->replacement_value
;
8107 else if (constructor_type
== NULL_TREE
)
8109 else if (!RECORD_OR_UNION_TYPE_P (constructor_type
)
8110 && TREE_CODE (constructor_type
) != ARRAY_TYPE
8111 && !VECTOR_TYPE_P (constructor_type
))
8113 /* A nonincremental scalar initializer--just return
8114 the element, after verifying there is just one. */
8115 if (vec_safe_is_empty (constructor_elements
))
8117 if (!constructor_erroneous
)
8118 error_init (loc
, "empty scalar initializer");
8119 ret
.value
= error_mark_node
;
8121 else if (vec_safe_length (constructor_elements
) != 1)
8123 error_init (loc
, "extra elements in scalar initializer");
8124 ret
.value
= (*constructor_elements
)[0].value
;
8127 ret
.value
= (*constructor_elements
)[0].value
;
8131 if (constructor_erroneous
)
8132 ret
.value
= error_mark_node
;
8135 ret
.value
= build_constructor (constructor_type
,
8136 constructor_elements
);
8137 if (constructor_constant
)
8138 TREE_CONSTANT (ret
.value
) = 1;
8139 if (constructor_constant
&& constructor_simple
)
8140 TREE_STATIC (ret
.value
) = 1;
8141 if (constructor_nonconst
)
8142 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
8146 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
8148 if (constructor_nonconst
)
8149 ret
.original_code
= C_MAYBE_CONST_EXPR
;
8150 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
8151 ret
.original_code
= ERROR_MARK
;
8154 constructor_type
= p
->type
;
8155 constructor_fields
= p
->fields
;
8156 constructor_index
= p
->index
;
8157 constructor_max_index
= p
->max_index
;
8158 constructor_unfilled_index
= p
->unfilled_index
;
8159 constructor_unfilled_fields
= p
->unfilled_fields
;
8160 constructor_bit_index
= p
->bit_index
;
8161 constructor_elements
= p
->elements
;
8162 constructor_constant
= p
->constant
;
8163 constructor_simple
= p
->simple
;
8164 constructor_nonconst
= p
->nonconst
;
8165 constructor_erroneous
= p
->erroneous
;
8166 constructor_incremental
= p
->incremental
;
8167 constructor_designated
= p
->designated
;
8168 designator_depth
= p
->designator_depth
;
8169 constructor_pending_elts
= p
->pending_elts
;
8170 constructor_depth
= p
->depth
;
8172 constructor_range_stack
= p
->range_stack
;
8173 RESTORE_SPELLING_DEPTH (constructor_depth
);
8175 constructor_stack
= p
->next
;
8178 if (ret
.value
== NULL_TREE
&& constructor_stack
== 0)
8179 ret
.value
= error_mark_node
;
8183 /* Common handling for both array range and field name designators.
8184 ARRAY argument is nonzero for array ranges. Returns false for success. */
8187 set_designator (location_t loc
, int array
,
8188 struct obstack
*braced_init_obstack
)
8191 enum tree_code subcode
;
8193 /* Don't die if an entire brace-pair level is superfluous
8194 in the containing level. */
8195 if (constructor_type
== NULL_TREE
)
8198 /* If there were errors in this designator list already, bail out
8200 if (designator_erroneous
)
8203 if (!designator_depth
)
8205 gcc_assert (!constructor_range_stack
);
8207 /* Designator list starts at the level of closest explicit
8209 while (constructor_stack
->implicit
)
8210 process_init_element (input_location
,
8211 pop_init_level (loc
, 1, braced_init_obstack
,
8212 last_init_list_comma
),
8213 true, braced_init_obstack
);
8214 constructor_designated
= 1;
8218 switch (TREE_CODE (constructor_type
))
8222 subtype
= TREE_TYPE (constructor_fields
);
8223 if (subtype
!= error_mark_node
)
8224 subtype
= TYPE_MAIN_VARIANT (subtype
);
8227 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8233 subcode
= TREE_CODE (subtype
);
8234 if (array
&& subcode
!= ARRAY_TYPE
)
8236 error_init (loc
, "array index in non-array initializer");
8239 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
8241 error_init (loc
, "field name not in record or union initializer");
8245 constructor_designated
= 1;
8246 finish_implicit_inits (loc
, braced_init_obstack
);
8247 push_init_level (loc
, 2, braced_init_obstack
);
8251 /* If there are range designators in designator list, push a new designator
8252 to constructor_range_stack. RANGE_END is end of such stack range or
8253 NULL_TREE if there is no range designator at this level. */
8256 push_range_stack (tree range_end
, struct obstack
* braced_init_obstack
)
8258 struct constructor_range_stack
*p
;
8260 p
= (struct constructor_range_stack
*)
8261 obstack_alloc (braced_init_obstack
,
8262 sizeof (struct constructor_range_stack
));
8263 p
->prev
= constructor_range_stack
;
8265 p
->fields
= constructor_fields
;
8266 p
->range_start
= constructor_index
;
8267 p
->index
= constructor_index
;
8268 p
->stack
= constructor_stack
;
8269 p
->range_end
= range_end
;
8270 if (constructor_range_stack
)
8271 constructor_range_stack
->next
= p
;
8272 constructor_range_stack
= p
;
8275 /* Within an array initializer, specify the next index to be initialized.
8276 FIRST is that index. If LAST is nonzero, then initialize a range
8277 of indices, running from FIRST through LAST. */
8280 set_init_index (location_t loc
, tree first
, tree last
,
8281 struct obstack
*braced_init_obstack
)
8283 if (set_designator (loc
, 1, braced_init_obstack
))
8286 designator_erroneous
= 1;
8288 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
8289 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
8291 error_init (loc
, "array index in initializer not of integer type");
8295 if (TREE_CODE (first
) != INTEGER_CST
)
8297 first
= c_fully_fold (first
, false, NULL
);
8298 if (TREE_CODE (first
) == INTEGER_CST
)
8299 pedwarn_init (loc
, OPT_Wpedantic
,
8300 "array index in initializer is not "
8301 "an integer constant expression");
8304 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
8306 last
= c_fully_fold (last
, false, NULL
);
8307 if (TREE_CODE (last
) == INTEGER_CST
)
8308 pedwarn_init (loc
, OPT_Wpedantic
,
8309 "array index in initializer is not "
8310 "an integer constant expression");
8313 if (TREE_CODE (first
) != INTEGER_CST
)
8314 error_init (loc
, "nonconstant array index in initializer");
8315 else if (last
!= NULL_TREE
&& TREE_CODE (last
) != INTEGER_CST
)
8316 error_init (loc
, "nonconstant array index in initializer");
8317 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
8318 error_init (loc
, "array index in non-array initializer");
8319 else if (tree_int_cst_sgn (first
) == -1)
8320 error_init (loc
, "array index in initializer exceeds array bounds");
8321 else if (constructor_max_index
8322 && tree_int_cst_lt (constructor_max_index
, first
))
8323 error_init (loc
, "array index in initializer exceeds array bounds");
8326 constant_expression_warning (first
);
8328 constant_expression_warning (last
);
8329 constructor_index
= convert (bitsizetype
, first
);
8330 if (tree_int_cst_lt (constructor_index
, first
))
8332 constructor_index
= copy_node (constructor_index
);
8333 TREE_OVERFLOW (constructor_index
) = 1;
8338 if (tree_int_cst_equal (first
, last
))
8340 else if (tree_int_cst_lt (last
, first
))
8342 error_init (loc
, "empty index range in initializer");
8347 last
= convert (bitsizetype
, last
);
8348 if (constructor_max_index
!= NULL_TREE
8349 && tree_int_cst_lt (constructor_max_index
, last
))
8351 error_init (loc
, "array index range in initializer exceeds "
8359 designator_erroneous
= 0;
8360 if (constructor_range_stack
|| last
)
8361 push_range_stack (last
, braced_init_obstack
);
8365 /* Within a struct initializer, specify the next field to be initialized. */
8368 set_init_label (location_t loc
, tree fieldname
, location_t fieldname_loc
,
8369 struct obstack
*braced_init_obstack
)
8373 if (set_designator (loc
, 0, braced_init_obstack
))
8376 designator_erroneous
= 1;
8378 if (!RECORD_OR_UNION_TYPE_P (constructor_type
))
8380 error_init (loc
, "field name not in record or union initializer");
8384 field
= lookup_field (constructor_type
, fieldname
);
8386 if (field
== NULL_TREE
)
8388 tree guessed_id
= lookup_field_fuzzy (constructor_type
, fieldname
);
8391 gcc_rich_location
rich_loc (fieldname_loc
);
8392 rich_loc
.add_fixit_misspelled_id (fieldname_loc
, guessed_id
);
8395 "%qT has no member named %qE; did you mean %qE?",
8396 constructor_type
, fieldname
, guessed_id
);
8399 error_at (fieldname_loc
, "%qT has no member named %qE",
8400 constructor_type
, fieldname
);
8405 constructor_fields
= TREE_VALUE (field
);
8407 designator_erroneous
= 0;
8408 if (constructor_range_stack
)
8409 push_range_stack (NULL_TREE
, braced_init_obstack
);
8410 field
= TREE_CHAIN (field
);
8413 if (set_designator (loc
, 0, braced_init_obstack
))
8417 while (field
!= NULL_TREE
);
8420 /* Add a new initializer to the tree of pending initializers. PURPOSE
8421 identifies the initializer, either array index or field in a structure.
8422 VALUE is the value of that index or field. If ORIGTYPE is not
8423 NULL_TREE, it is the original type of VALUE.
8425 IMPLICIT is true if value comes from pop_init_level (1),
8426 the new initializer has been merged with the existing one
8427 and thus no warnings should be emitted about overriding an
8428 existing initializer. */
8431 add_pending_init (location_t loc
, tree purpose
, tree value
, tree origtype
,
8432 bool implicit
, struct obstack
*braced_init_obstack
)
8434 struct init_node
*p
, **q
, *r
;
8436 q
= &constructor_pending_elts
;
8439 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8444 if (tree_int_cst_lt (purpose
, p
->purpose
))
8446 else if (tree_int_cst_lt (p
->purpose
, purpose
))
8452 if (TREE_SIDE_EFFECTS (p
->value
))
8453 warning_init (loc
, OPT_Woverride_init_side_effects
,
8454 "initialized field with side-effects "
8456 else if (warn_override_init
)
8457 warning_init (loc
, OPT_Woverride_init
,
8458 "initialized field overwritten");
8461 p
->origtype
= origtype
;
8470 bitpos
= bit_position (purpose
);
8474 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
8476 else if (p
->purpose
!= purpose
)
8482 if (TREE_SIDE_EFFECTS (p
->value
))
8483 warning_init (loc
, OPT_Woverride_init_side_effects
,
8484 "initialized field with side-effects "
8486 else if (warn_override_init
)
8487 warning_init (loc
, OPT_Woverride_init
,
8488 "initialized field overwritten");
8491 p
->origtype
= origtype
;
8497 r
= (struct init_node
*) obstack_alloc (braced_init_obstack
,
8498 sizeof (struct init_node
));
8499 r
->purpose
= purpose
;
8501 r
->origtype
= origtype
;
8511 struct init_node
*s
;
8515 if (p
->balance
== 0)
8517 else if (p
->balance
< 0)
8524 p
->left
->parent
= p
;
8541 constructor_pending_elts
= r
;
8546 struct init_node
*t
= r
->right
;
8550 r
->right
->parent
= r
;
8555 p
->left
->parent
= p
;
8558 p
->balance
= t
->balance
< 0;
8559 r
->balance
= -(t
->balance
> 0);
8574 constructor_pending_elts
= t
;
8580 /* p->balance == +1; growth of left side balances the node. */
8585 else /* r == p->right */
8587 if (p
->balance
== 0)
8588 /* Growth propagation from right side. */
8590 else if (p
->balance
> 0)
8597 p
->right
->parent
= p
;
8614 constructor_pending_elts
= r
;
8616 else /* r->balance == -1 */
8619 struct init_node
*t
= r
->left
;
8623 r
->left
->parent
= r
;
8628 p
->right
->parent
= p
;
8631 r
->balance
= (t
->balance
< 0);
8632 p
->balance
= -(t
->balance
> 0);
8647 constructor_pending_elts
= t
;
8653 /* p->balance == -1; growth of right side balances the node. */
8664 /* Build AVL tree from a sorted chain. */
8667 set_nonincremental_init (struct obstack
* braced_init_obstack
)
8669 unsigned HOST_WIDE_INT ix
;
8672 if (TREE_CODE (constructor_type
) != RECORD_TYPE
8673 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
8676 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
8677 add_pending_init (input_location
, index
, value
, NULL_TREE
, true,
8678 braced_init_obstack
);
8679 constructor_elements
= NULL
;
8680 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8682 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
8683 /* Skip any nameless bit fields at the beginning. */
8684 while (constructor_unfilled_fields
!= NULL_TREE
8685 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8686 && DECL_NAME (constructor_unfilled_fields
) == NULL_TREE
)
8687 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
8690 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8692 if (TYPE_DOMAIN (constructor_type
))
8693 constructor_unfilled_index
8694 = convert (bitsizetype
,
8695 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
8697 constructor_unfilled_index
= bitsize_zero_node
;
8699 constructor_incremental
= 0;
8702 /* Build AVL tree from a string constant. */
8705 set_nonincremental_init_from_string (tree str
,
8706 struct obstack
* braced_init_obstack
)
8708 tree value
, purpose
, type
;
8709 HOST_WIDE_INT val
[2];
8710 const char *p
, *end
;
8711 int byte
, wchar_bytes
, charwidth
, bitpos
;
8713 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
8715 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
8716 charwidth
= TYPE_PRECISION (char_type_node
);
8717 gcc_assert ((size_t) wchar_bytes
* charwidth
8718 <= ARRAY_SIZE (val
) * HOST_BITS_PER_WIDE_INT
);
8719 type
= TREE_TYPE (constructor_type
);
8720 p
= TREE_STRING_POINTER (str
);
8721 end
= p
+ TREE_STRING_LENGTH (str
);
8723 for (purpose
= bitsize_zero_node
;
8725 && !(constructor_max_index
8726 && tree_int_cst_lt (constructor_max_index
, purpose
));
8727 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
8729 if (wchar_bytes
== 1)
8731 val
[0] = (unsigned char) *p
++;
8738 for (byte
= 0; byte
< wchar_bytes
; byte
++)
8740 if (BYTES_BIG_ENDIAN
)
8741 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
8743 bitpos
= byte
* charwidth
;
8744 val
[bitpos
/ HOST_BITS_PER_WIDE_INT
]
8745 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
8746 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
8750 if (!TYPE_UNSIGNED (type
))
8752 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
8753 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
8755 if (val
[0] & (HOST_WIDE_INT_1
<< (bitpos
- 1)))
8757 val
[0] |= HOST_WIDE_INT_M1U
<< bitpos
;
8761 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
8766 else if (val
[1] & (HOST_WIDE_INT_1
8767 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
8768 val
[1] |= HOST_WIDE_INT_M1U
<< (bitpos
- HOST_BITS_PER_WIDE_INT
);
8771 value
= wide_int_to_tree (type
,
8772 wide_int::from_array (val
, 2,
8773 HOST_BITS_PER_WIDE_INT
* 2));
8774 add_pending_init (input_location
, purpose
, value
, NULL_TREE
, true,
8775 braced_init_obstack
);
8778 constructor_incremental
= 0;
8781 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
8782 not initialized yet. */
8785 find_init_member (tree field
, struct obstack
* braced_init_obstack
)
8787 struct init_node
*p
;
8789 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8791 if (constructor_incremental
8792 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8793 set_nonincremental_init (braced_init_obstack
);
8795 p
= constructor_pending_elts
;
8798 if (tree_int_cst_lt (field
, p
->purpose
))
8800 else if (tree_int_cst_lt (p
->purpose
, field
))
8806 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8808 tree bitpos
= bit_position (field
);
8810 if (constructor_incremental
8811 && (!constructor_unfilled_fields
8812 || tree_int_cst_lt (bitpos
,
8813 bit_position (constructor_unfilled_fields
))))
8814 set_nonincremental_init (braced_init_obstack
);
8816 p
= constructor_pending_elts
;
8819 if (field
== p
->purpose
)
8821 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
8827 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8829 if (!vec_safe_is_empty (constructor_elements
)
8830 && (constructor_elements
->last ().index
== field
))
8831 return constructor_elements
->last ().value
;
8836 /* "Output" the next constructor element.
8837 At top level, really output it to assembler code now.
8838 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8839 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8840 TYPE is the data type that the containing data type wants here.
8841 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8842 If VALUE is a string constant, STRICT_STRING is true if it is
8843 unparenthesized or we should not warn here for it being parenthesized.
8844 For other types of VALUE, STRICT_STRING is not used.
8846 PENDING if non-nil means output pending elements that belong
8847 right after this element. (PENDING is normally 1;
8848 it is 0 while outputting pending elements, to avoid recursion.)
8850 IMPLICIT is true if value comes from pop_init_level (1),
8851 the new initializer has been merged with the existing one
8852 and thus no warnings should be emitted about overriding an
8853 existing initializer. */
8856 output_init_element (location_t loc
, tree value
, tree origtype
,
8857 bool strict_string
, tree type
, tree field
, int pending
,
8858 bool implicit
, struct obstack
* braced_init_obstack
)
8860 tree semantic_type
= NULL_TREE
;
8861 bool maybe_const
= true;
8864 if (type
== error_mark_node
|| value
== error_mark_node
)
8866 constructor_erroneous
= 1;
8869 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
8870 && (TREE_CODE (value
) == STRING_CST
8871 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
8872 && !(TREE_CODE (value
) == STRING_CST
8873 && TREE_CODE (type
) == ARRAY_TYPE
8874 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
8875 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
8876 TYPE_MAIN_VARIANT (type
)))
8877 value
= array_to_pointer_conversion (input_location
, value
);
8879 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
8880 && require_constant_value
&& pending
)
8882 /* As an extension, allow initializing objects with static storage
8883 duration with compound literals (which are then treated just as
8884 the brace enclosed list they contain). */
8886 pedwarn_init (loc
, OPT_Wpedantic
, "initializer element is not "
8888 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
8889 value
= DECL_INITIAL (decl
);
8892 npc
= null_pointer_constant_p (value
);
8893 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
8895 semantic_type
= TREE_TYPE (value
);
8896 value
= TREE_OPERAND (value
, 0);
8898 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
8900 if (value
== error_mark_node
)
8901 constructor_erroneous
= 1;
8902 else if (!TREE_CONSTANT (value
))
8903 constructor_constant
= 0;
8904 else if (!initializer_constant_valid_p (value
,
8906 AGGREGATE_TYPE_P (constructor_type
)
8907 && TYPE_REVERSE_STORAGE_ORDER
8909 || (RECORD_OR_UNION_TYPE_P (constructor_type
)
8910 && DECL_C_BIT_FIELD (field
)
8911 && TREE_CODE (value
) != INTEGER_CST
))
8912 constructor_simple
= 0;
8914 constructor_nonconst
= 1;
8916 /* Digest the initializer and issue any errors about incompatible
8917 types before issuing errors about non-constant initializers. */
8918 tree new_value
= value
;
8920 new_value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
8921 new_value
= digest_init (loc
, type
, new_value
, origtype
, npc
, strict_string
,
8922 require_constant_value
);
8923 if (new_value
== error_mark_node
)
8925 constructor_erroneous
= 1;
8928 if (require_constant_value
|| require_constant_elements
)
8929 constant_expression_warning (new_value
);
8931 /* Proceed to check the constness of the original initializer. */
8932 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
8934 if (require_constant_value
)
8936 error_init (loc
, "initializer element is not constant");
8937 value
= error_mark_node
;
8939 else if (require_constant_elements
)
8940 pedwarn (loc
, OPT_Wpedantic
,
8941 "initializer element is not computable at load time");
8943 else if (!maybe_const
8944 && (require_constant_value
|| require_constant_elements
))
8945 pedwarn_init (loc
, OPT_Wpedantic
,
8946 "initializer element is not a constant expression");
8948 /* Issue -Wc++-compat warnings about initializing a bitfield with
8951 && field
!= NULL_TREE
8952 && TREE_CODE (field
) == FIELD_DECL
8953 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
8954 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
8955 != TYPE_MAIN_VARIANT (type
))
8956 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
8958 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
8959 if (checktype
!= error_mark_node
8960 && (TYPE_MAIN_VARIANT (checktype
)
8961 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
8962 warning_init (loc
, OPT_Wc___compat
,
8963 "enum conversion in initialization is invalid in C++");
8966 /* If this field is empty (and not at the end of structure),
8967 don't do anything other than checking the initializer. */
8969 && (TREE_TYPE (field
) == error_mark_node
8970 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
8971 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
8972 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
8973 || DECL_CHAIN (field
)))))
8976 /* Finally, set VALUE to the initializer value digested above. */
8979 /* If this element doesn't come next in sequence,
8980 put it on constructor_pending_elts. */
8981 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
8982 && (!constructor_incremental
8983 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
8985 if (constructor_incremental
8986 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8987 set_nonincremental_init (braced_init_obstack
);
8989 add_pending_init (loc
, field
, value
, origtype
, implicit
,
8990 braced_init_obstack
);
8993 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8994 && (!constructor_incremental
8995 || field
!= constructor_unfilled_fields
))
8997 /* We do this for records but not for unions. In a union,
8998 no matter which field is specified, it can be initialized
8999 right away since it starts at the beginning of the union. */
9000 if (constructor_incremental
)
9002 if (!constructor_unfilled_fields
)
9003 set_nonincremental_init (braced_init_obstack
);
9006 tree bitpos
, unfillpos
;
9008 bitpos
= bit_position (field
);
9009 unfillpos
= bit_position (constructor_unfilled_fields
);
9011 if (tree_int_cst_lt (bitpos
, unfillpos
))
9012 set_nonincremental_init (braced_init_obstack
);
9016 add_pending_init (loc
, field
, value
, origtype
, implicit
,
9017 braced_init_obstack
);
9020 else if (TREE_CODE (constructor_type
) == UNION_TYPE
9021 && !vec_safe_is_empty (constructor_elements
))
9025 if (TREE_SIDE_EFFECTS (constructor_elements
->last ().value
))
9026 warning_init (loc
, OPT_Woverride_init_side_effects
,
9027 "initialized field with side-effects overwritten");
9028 else if (warn_override_init
)
9029 warning_init (loc
, OPT_Woverride_init
,
9030 "initialized field overwritten");
9033 /* We can have just one union field set. */
9034 constructor_elements
= NULL
;
9037 /* Otherwise, output this element either to
9038 constructor_elements or to the assembler file. */
9040 constructor_elt celt
= {field
, value
};
9041 vec_safe_push (constructor_elements
, celt
);
9043 /* Advance the variable that indicates sequential elements output. */
9044 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9045 constructor_unfilled_index
9046 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
9048 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
9050 constructor_unfilled_fields
9051 = DECL_CHAIN (constructor_unfilled_fields
);
9053 /* Skip any nameless bit fields. */
9054 while (constructor_unfilled_fields
!= NULL_TREE
9055 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
9056 && DECL_NAME (constructor_unfilled_fields
) == NULL_TREE
)
9057 constructor_unfilled_fields
=
9058 DECL_CHAIN (constructor_unfilled_fields
);
9060 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
9061 constructor_unfilled_fields
= NULL_TREE
;
9063 /* Now output any pending elements which have become next. */
9065 output_pending_init_elements (0, braced_init_obstack
);
9068 /* Output any pending elements which have become next.
9069 As we output elements, constructor_unfilled_{fields,index}
9070 advances, which may cause other elements to become next;
9071 if so, they too are output.
9073 If ALL is 0, we return when there are
9074 no more pending elements to output now.
9076 If ALL is 1, we output space as necessary so that
9077 we can output all the pending elements. */
9079 output_pending_init_elements (int all
, struct obstack
* braced_init_obstack
)
9081 struct init_node
*elt
= constructor_pending_elts
;
9086 /* Look through the whole pending tree.
9087 If we find an element that should be output now,
9088 output it. Otherwise, set NEXT to the element
9089 that comes first among those still pending. */
9094 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9096 if (tree_int_cst_equal (elt
->purpose
,
9097 constructor_unfilled_index
))
9098 output_init_element (input_location
, elt
->value
, elt
->origtype
,
9099 true, TREE_TYPE (constructor_type
),
9100 constructor_unfilled_index
, 0, false,
9101 braced_init_obstack
);
9102 else if (tree_int_cst_lt (constructor_unfilled_index
,
9105 /* Advance to the next smaller node. */
9110 /* We have reached the smallest node bigger than the
9111 current unfilled index. Fill the space first. */
9112 next
= elt
->purpose
;
9118 /* Advance to the next bigger node. */
9123 /* We have reached the biggest node in a subtree. Find
9124 the parent of it, which is the next bigger node. */
9125 while (elt
->parent
&& elt
->parent
->right
== elt
)
9128 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
9131 next
= elt
->purpose
;
9137 else if (RECORD_OR_UNION_TYPE_P (constructor_type
))
9139 tree ctor_unfilled_bitpos
, elt_bitpos
;
9141 /* If the current record is complete we are done. */
9142 if (constructor_unfilled_fields
== NULL_TREE
)
9145 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
9146 elt_bitpos
= bit_position (elt
->purpose
);
9147 /* We can't compare fields here because there might be empty
9148 fields in between. */
9149 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
9151 constructor_unfilled_fields
= elt
->purpose
;
9152 output_init_element (input_location
, elt
->value
, elt
->origtype
,
9153 true, TREE_TYPE (elt
->purpose
),
9154 elt
->purpose
, 0, false,
9155 braced_init_obstack
);
9157 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
9159 /* Advance to the next smaller node. */
9164 /* We have reached the smallest node bigger than the
9165 current unfilled field. Fill the space first. */
9166 next
= elt
->purpose
;
9172 /* Advance to the next bigger node. */
9177 /* We have reached the biggest node in a subtree. Find
9178 the parent of it, which is the next bigger node. */
9179 while (elt
->parent
&& elt
->parent
->right
== elt
)
9183 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
9184 bit_position (elt
->purpose
))))
9186 next
= elt
->purpose
;
9194 /* Ordinarily return, but not if we want to output all
9195 and there are elements left. */
9196 if (!(all
&& next
!= NULL_TREE
))
9199 /* If it's not incremental, just skip over the gap, so that after
9200 jumping to retry we will output the next successive element. */
9201 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
9202 constructor_unfilled_fields
= next
;
9203 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9204 constructor_unfilled_index
= next
;
9206 /* ELT now points to the node in the pending tree with the next
9207 initializer to output. */
9211 /* Add one non-braced element to the current constructor level.
9212 This adjusts the current position within the constructor's type.
9213 This may also start or terminate implicit levels
9214 to handle a partly-braced initializer.
9216 Once this has found the correct level for the new element,
9217 it calls output_init_element.
9219 IMPLICIT is true if value comes from pop_init_level (1),
9220 the new initializer has been merged with the existing one
9221 and thus no warnings should be emitted about overriding an
9222 existing initializer. */
9225 process_init_element (location_t loc
, struct c_expr value
, bool implicit
,
9226 struct obstack
* braced_init_obstack
)
9228 tree orig_value
= value
.value
;
9230 = (orig_value
!= NULL_TREE
&& TREE_CODE (orig_value
) == STRING_CST
);
9231 bool strict_string
= value
.original_code
== STRING_CST
;
9232 bool was_designated
= designator_depth
!= 0;
9234 designator_depth
= 0;
9235 designator_erroneous
= 0;
9237 if (!implicit
&& value
.value
&& !integer_zerop (value
.value
))
9238 constructor_zeroinit
= 0;
9240 /* Handle superfluous braces around string cst as in
9241 char x[] = {"foo"}; */
9245 && TREE_CODE (constructor_type
) == ARRAY_TYPE
9246 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
9247 && integer_zerop (constructor_unfilled_index
))
9249 if (constructor_stack
->replacement_value
.value
)
9250 error_init (loc
, "excess elements in char array initializer");
9251 constructor_stack
->replacement_value
= value
;
9255 if (constructor_stack
->replacement_value
.value
!= NULL_TREE
)
9257 error_init (loc
, "excess elements in struct initializer");
9261 /* Ignore elements of a brace group if it is entirely superfluous
9262 and has already been diagnosed. */
9263 if (constructor_type
== NULL_TREE
)
9266 if (!implicit
&& warn_designated_init
&& !was_designated
9267 && TREE_CODE (constructor_type
) == RECORD_TYPE
9268 && lookup_attribute ("designated_init",
9269 TYPE_ATTRIBUTES (constructor_type
)))
9271 OPT_Wdesignated_init
,
9272 "positional initialization of field "
9273 "in %<struct%> declared with %<designated_init%> attribute");
9275 /* If we've exhausted any levels that didn't have braces,
9277 while (constructor_stack
->implicit
)
9279 if (RECORD_OR_UNION_TYPE_P (constructor_type
)
9280 && constructor_fields
== NULL_TREE
)
9281 process_init_element (loc
,
9282 pop_init_level (loc
, 1, braced_init_obstack
,
9283 last_init_list_comma
),
9284 true, braced_init_obstack
);
9285 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
9286 || VECTOR_TYPE_P (constructor_type
))
9287 && constructor_max_index
9288 && tree_int_cst_lt (constructor_max_index
,
9290 process_init_element (loc
,
9291 pop_init_level (loc
, 1, braced_init_obstack
,
9292 last_init_list_comma
),
9293 true, braced_init_obstack
);
9298 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9299 if (constructor_range_stack
)
9301 /* If value is a compound literal and we'll be just using its
9302 content, don't put it into a SAVE_EXPR. */
9303 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
9304 || !require_constant_value
)
9306 tree semantic_type
= NULL_TREE
;
9307 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
9309 semantic_type
= TREE_TYPE (value
.value
);
9310 value
.value
= TREE_OPERAND (value
.value
, 0);
9312 value
.value
= save_expr (value
.value
);
9314 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
9321 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
9324 enum tree_code fieldcode
;
9326 if (constructor_fields
== NULL_TREE
)
9328 pedwarn_init (loc
, 0, "excess elements in struct initializer");
9332 fieldtype
= TREE_TYPE (constructor_fields
);
9333 if (fieldtype
!= error_mark_node
)
9334 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
9335 fieldcode
= TREE_CODE (fieldtype
);
9337 /* Error for non-static initialization of a flexible array member. */
9338 if (fieldcode
== ARRAY_TYPE
9339 && !require_constant_value
9340 && TYPE_SIZE (fieldtype
) == NULL_TREE
9341 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
9343 error_init (loc
, "non-static initialization of a flexible "
9348 /* Error for initialization of a flexible array member with
9349 a string constant if the structure is in an array. E.g.:
9350 struct S { int x; char y[]; };
9351 struct S s[] = { { 1, "foo" } };
9354 && fieldcode
== ARRAY_TYPE
9355 && constructor_depth
> 1
9356 && TYPE_SIZE (fieldtype
) == NULL_TREE
9357 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
9359 bool in_array_p
= false;
9360 for (struct constructor_stack
*p
= constructor_stack
;
9361 p
&& p
->type
; p
= p
->next
)
9362 if (TREE_CODE (p
->type
) == ARRAY_TYPE
)
9369 error_init (loc
, "initialization of flexible array "
9370 "member in a nested context");
9375 /* Accept a string constant to initialize a subarray. */
9376 if (value
.value
!= NULL_TREE
9377 && fieldcode
== ARRAY_TYPE
9378 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
9380 value
.value
= orig_value
;
9381 /* Otherwise, if we have come to a subaggregate,
9382 and we don't have an element of its type, push into it. */
9383 else if (value
.value
!= NULL_TREE
9384 && value
.value
!= error_mark_node
9385 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
9386 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
9387 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
9389 push_init_level (loc
, 1, braced_init_obstack
);
9395 push_member_name (constructor_fields
);
9396 output_init_element (loc
, value
.value
, value
.original_type
,
9397 strict_string
, fieldtype
,
9398 constructor_fields
, 1, implicit
,
9399 braced_init_obstack
);
9400 RESTORE_SPELLING_DEPTH (constructor_depth
);
9403 /* Do the bookkeeping for an element that was
9404 directly output as a constructor. */
9406 /* For a record, keep track of end position of last field. */
9407 if (DECL_SIZE (constructor_fields
))
9408 constructor_bit_index
9409 = size_binop_loc (input_location
, PLUS_EXPR
,
9410 bit_position (constructor_fields
),
9411 DECL_SIZE (constructor_fields
));
9413 /* If the current field was the first one not yet written out,
9414 it isn't now, so update. */
9415 if (constructor_unfilled_fields
== constructor_fields
)
9417 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
9418 /* Skip any nameless bit fields. */
9419 while (constructor_unfilled_fields
!= 0
9420 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
9421 && DECL_NAME (constructor_unfilled_fields
) == 0)
9422 constructor_unfilled_fields
=
9423 DECL_CHAIN (constructor_unfilled_fields
);
9427 constructor_fields
= DECL_CHAIN (constructor_fields
);
9428 /* Skip any nameless bit fields at the beginning. */
9429 while (constructor_fields
!= NULL_TREE
9430 && DECL_C_BIT_FIELD (constructor_fields
)
9431 && DECL_NAME (constructor_fields
) == NULL_TREE
)
9432 constructor_fields
= DECL_CHAIN (constructor_fields
);
9434 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
9437 enum tree_code fieldcode
;
9439 if (constructor_fields
== NULL_TREE
)
9441 pedwarn_init (loc
, 0,
9442 "excess elements in union initializer");
9446 fieldtype
= TREE_TYPE (constructor_fields
);
9447 if (fieldtype
!= error_mark_node
)
9448 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
9449 fieldcode
= TREE_CODE (fieldtype
);
9451 /* Warn that traditional C rejects initialization of unions.
9452 We skip the warning if the value is zero. This is done
9453 under the assumption that the zero initializer in user
9454 code appears conditioned on e.g. __STDC__ to avoid
9455 "missing initializer" warnings and relies on default
9456 initialization to zero in the traditional C case.
9457 We also skip the warning if the initializer is designated,
9458 again on the assumption that this must be conditional on
9459 __STDC__ anyway (and we've already complained about the
9460 member-designator already). */
9461 if (!in_system_header_at (input_location
) && !constructor_designated
9462 && !(value
.value
&& (integer_zerop (value
.value
)
9463 || real_zerop (value
.value
))))
9464 warning (OPT_Wtraditional
, "traditional C rejects initialization "
9467 /* Accept a string constant to initialize a subarray. */
9468 if (value
.value
!= NULL_TREE
9469 && fieldcode
== ARRAY_TYPE
9470 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
9472 value
.value
= orig_value
;
9473 /* Otherwise, if we have come to a subaggregate,
9474 and we don't have an element of its type, push into it. */
9475 else if (value
.value
!= NULL_TREE
9476 && value
.value
!= error_mark_node
9477 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
9478 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
9479 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
9481 push_init_level (loc
, 1, braced_init_obstack
);
9487 push_member_name (constructor_fields
);
9488 output_init_element (loc
, value
.value
, value
.original_type
,
9489 strict_string
, fieldtype
,
9490 constructor_fields
, 1, implicit
,
9491 braced_init_obstack
);
9492 RESTORE_SPELLING_DEPTH (constructor_depth
);
9495 /* Do the bookkeeping for an element that was
9496 directly output as a constructor. */
9498 constructor_bit_index
= DECL_SIZE (constructor_fields
);
9499 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
9502 constructor_fields
= NULL_TREE
;
9504 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9506 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
9507 enum tree_code eltcode
= TREE_CODE (elttype
);
9509 /* Accept a string constant to initialize a subarray. */
9510 if (value
.value
!= NULL_TREE
9511 && eltcode
== ARRAY_TYPE
9512 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
9514 value
.value
= orig_value
;
9515 /* Otherwise, if we have come to a subaggregate,
9516 and we don't have an element of its type, push into it. */
9517 else if (value
.value
!= NULL_TREE
9518 && value
.value
!= error_mark_node
9519 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
9520 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
9521 || eltcode
== UNION_TYPE
|| eltcode
== VECTOR_TYPE
))
9523 push_init_level (loc
, 1, braced_init_obstack
);
9527 if (constructor_max_index
!= NULL_TREE
9528 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
9529 || integer_all_onesp (constructor_max_index
)))
9531 pedwarn_init (loc
, 0,
9532 "excess elements in array initializer");
9536 /* Now output the actual element. */
9539 push_array_bounds (tree_to_uhwi (constructor_index
));
9540 output_init_element (loc
, value
.value
, value
.original_type
,
9541 strict_string
, elttype
,
9542 constructor_index
, 1, implicit
,
9543 braced_init_obstack
);
9544 RESTORE_SPELLING_DEPTH (constructor_depth
);
9548 = size_binop_loc (input_location
, PLUS_EXPR
,
9549 constructor_index
, bitsize_one_node
);
9552 /* If we are doing the bookkeeping for an element that was
9553 directly output as a constructor, we must update
9554 constructor_unfilled_index. */
9555 constructor_unfilled_index
= constructor_index
;
9557 else if (VECTOR_TYPE_P (constructor_type
))
9559 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
9561 /* Do a basic check of initializer size. Note that vectors
9562 always have a fixed size derived from their type. */
9563 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
9565 pedwarn_init (loc
, 0,
9566 "excess elements in vector initializer");
9570 /* Now output the actual element. */
9573 if (TREE_CODE (value
.value
) == VECTOR_CST
)
9574 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
9575 output_init_element (loc
, value
.value
, value
.original_type
,
9576 strict_string
, elttype
,
9577 constructor_index
, 1, implicit
,
9578 braced_init_obstack
);
9582 = size_binop_loc (input_location
,
9583 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
9586 /* If we are doing the bookkeeping for an element that was
9587 directly output as a constructor, we must update
9588 constructor_unfilled_index. */
9589 constructor_unfilled_index
= constructor_index
;
9592 /* Handle the sole element allowed in a braced initializer
9593 for a scalar variable. */
9594 else if (constructor_type
!= error_mark_node
9595 && constructor_fields
== NULL_TREE
)
9597 pedwarn_init (loc
, 0,
9598 "excess elements in scalar initializer");
9604 output_init_element (loc
, value
.value
, value
.original_type
,
9605 strict_string
, constructor_type
,
9606 NULL_TREE
, 1, implicit
,
9607 braced_init_obstack
);
9608 constructor_fields
= NULL_TREE
;
9611 /* Handle range initializers either at this level or anywhere higher
9612 in the designator stack. */
9613 if (constructor_range_stack
)
9615 struct constructor_range_stack
*p
, *range_stack
;
9618 range_stack
= constructor_range_stack
;
9619 constructor_range_stack
= 0;
9620 while (constructor_stack
!= range_stack
->stack
)
9622 gcc_assert (constructor_stack
->implicit
);
9623 process_init_element (loc
,
9624 pop_init_level (loc
, 1,
9625 braced_init_obstack
,
9626 last_init_list_comma
),
9627 true, braced_init_obstack
);
9629 for (p
= range_stack
;
9630 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
9633 gcc_assert (constructor_stack
->implicit
);
9634 process_init_element (loc
,
9635 pop_init_level (loc
, 1,
9636 braced_init_obstack
,
9637 last_init_list_comma
),
9638 true, braced_init_obstack
);
9641 p
->index
= size_binop_loc (input_location
,
9642 PLUS_EXPR
, p
->index
, bitsize_one_node
);
9643 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
9648 constructor_index
= p
->index
;
9649 constructor_fields
= p
->fields
;
9650 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
9658 finish_implicit_inits (loc
, braced_init_obstack
);
9659 push_init_level (loc
, 2, braced_init_obstack
);
9660 p
->stack
= constructor_stack
;
9661 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
9662 p
->index
= p
->range_start
;
9666 constructor_range_stack
= range_stack
;
9673 constructor_range_stack
= 0;
9676 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9677 (guaranteed to be 'volatile' or null) and ARGS (represented using
9678 an ASM_EXPR node). */
9680 build_asm_stmt (tree cv_qualifier
, tree args
)
9682 if (!ASM_VOLATILE_P (args
) && cv_qualifier
)
9683 ASM_VOLATILE_P (args
) = 1;
9684 return add_stmt (args
);
9687 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9688 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9689 SIMPLE indicates whether there was anything at all after the
9690 string in the asm expression -- asm("blah") and asm("blah" : )
9691 are subtly different. We use a ASM_EXPR node to represent this. */
9693 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
9694 tree clobbers
, tree labels
, bool simple
)
9699 const char *constraint
;
9700 const char **oconstraints
;
9701 bool allows_mem
, allows_reg
, is_inout
;
9702 int ninputs
, noutputs
;
9704 ninputs
= list_length (inputs
);
9705 noutputs
= list_length (outputs
);
9706 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
9708 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
9710 /* Remove output conversions that change the type but not the mode. */
9711 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
9713 tree output
= TREE_VALUE (tail
);
9715 output
= c_fully_fold (output
, false, NULL
);
9717 /* ??? Really, this should not be here. Users should be using a
9718 proper lvalue, dammit. But there's a long history of using casts
9719 in the output operands. In cases like longlong.h, this becomes a
9720 primitive form of typechecking -- if the cast can be removed, then
9721 the output operand had a type of the proper width; otherwise we'll
9722 get an error. Gross, but ... */
9723 STRIP_NOPS (output
);
9725 if (!lvalue_or_else (loc
, output
, lv_asm
))
9726 output
= error_mark_node
;
9728 if (output
!= error_mark_node
9729 && (TREE_READONLY (output
)
9730 || TYPE_READONLY (TREE_TYPE (output
))
9731 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output
))
9732 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
9733 readonly_error (loc
, output
, lv_asm
);
9735 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9736 oconstraints
[i
] = constraint
;
9738 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
9739 &allows_mem
, &allows_reg
, &is_inout
))
9741 /* If the operand is going to end up in memory,
9742 mark it addressable. */
9743 if (!allows_reg
&& !c_mark_addressable (output
))
9744 output
= error_mark_node
;
9745 if (!(!allows_reg
&& allows_mem
)
9746 && output
!= error_mark_node
9747 && VOID_TYPE_P (TREE_TYPE (output
)))
9749 error_at (loc
, "invalid use of void expression");
9750 output
= error_mark_node
;
9754 output
= error_mark_node
;
9756 TREE_VALUE (tail
) = output
;
9759 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
9763 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9764 input
= TREE_VALUE (tail
);
9766 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
9767 oconstraints
, &allows_mem
, &allows_reg
))
9769 /* If the operand is going to end up in memory,
9770 mark it addressable. */
9771 if (!allows_reg
&& allows_mem
)
9773 input
= c_fully_fold (input
, false, NULL
);
9775 /* Strip the nops as we allow this case. FIXME, this really
9776 should be rejected or made deprecated. */
9778 if (!c_mark_addressable (input
))
9779 input
= error_mark_node
;
9784 memset (&expr
, 0, sizeof (expr
));
9786 expr
= convert_lvalue_to_rvalue (loc
, expr
, true, false);
9787 input
= c_fully_fold (expr
.value
, false, NULL
);
9789 if (input
!= error_mark_node
&& VOID_TYPE_P (TREE_TYPE (input
)))
9791 error_at (loc
, "invalid use of void expression");
9792 input
= error_mark_node
;
9797 input
= error_mark_node
;
9799 TREE_VALUE (tail
) = input
;
9802 /* ASMs with labels cannot have outputs. This should have been
9803 enforced by the parser. */
9804 gcc_assert (outputs
== NULL
|| labels
== NULL
);
9806 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
9808 /* asm statements without outputs, including simple ones, are treated
9810 ASM_INPUT_P (args
) = simple
;
9811 ASM_VOLATILE_P (args
) = (noutputs
== 0);
9816 /* Generate a goto statement to LABEL. LOC is the location of the
9820 c_finish_goto_label (location_t loc
, tree label
)
9822 tree decl
= lookup_label_for_goto (loc
, label
);
9825 TREE_USED (decl
) = 1;
9827 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
9828 SET_EXPR_LOCATION (t
, loc
);
9829 return add_stmt (t
);
9833 /* Generate a computed goto statement to EXPR. LOC is the location of
9837 c_finish_goto_ptr (location_t loc
, tree expr
)
9840 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids %<goto *expr;%>");
9841 expr
= c_fully_fold (expr
, false, NULL
);
9842 expr
= convert (ptr_type_node
, expr
);
9843 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
9844 SET_EXPR_LOCATION (t
, loc
);
9845 return add_stmt (t
);
9848 /* Generate a C `return' statement. RETVAL is the expression for what
9849 to return, or a null pointer for `return;' with no value. LOC is
9850 the location of the return statement, or the location of the expression,
9851 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9852 is the original type of RETVAL. */
9855 c_finish_return (location_t loc
, tree retval
, tree origtype
)
9857 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
9858 bool no_warning
= false;
9862 /* Use the expansion point to handle cases such as returning NULL
9863 in a function returning void. */
9864 source_location xloc
= expansion_point_location_if_in_system_header (loc
);
9866 if (TREE_THIS_VOLATILE (current_function_decl
))
9867 warning_at (xloc
, 0,
9868 "function declared %<noreturn%> has a %<return%> statement");
9870 if (flag_cilkplus
&& contains_array_notation_expr (retval
))
9872 /* Array notations are allowed in a return statement if it is inside a
9873 built-in array notation reduction function. */
9874 if (!find_rank (loc
, retval
, retval
, false, &rank
))
9875 return error_mark_node
;
9878 error_at (loc
, "array notation expression cannot be used as a "
9880 return error_mark_node
;
9883 if (flag_cilkplus
&& retval
&& contains_cilk_spawn_stmt (retval
))
9885 error_at (loc
, "use of %<_Cilk_spawn%> in a return statement is not "
9887 return error_mark_node
;
9891 tree semantic_type
= NULL_TREE
;
9892 npc
= null_pointer_constant_p (retval
);
9893 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
9895 semantic_type
= TREE_TYPE (retval
);
9896 retval
= TREE_OPERAND (retval
, 0);
9898 retval
= c_fully_fold (retval
, false, NULL
);
9900 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
9905 current_function_returns_null
= 1;
9906 if ((warn_return_type
|| flag_isoc99
)
9907 && valtype
!= NULL_TREE
&& TREE_CODE (valtype
) != VOID_TYPE
)
9911 warned_here
= pedwarn
9913 "%<return%> with no value, in function returning non-void");
9915 warned_here
= warning_at
9916 (loc
, OPT_Wreturn_type
,
9917 "%<return%> with no value, in function returning non-void");
9920 inform (DECL_SOURCE_LOCATION (current_function_decl
),
9924 else if (valtype
== NULL_TREE
|| TREE_CODE (valtype
) == VOID_TYPE
)
9926 current_function_returns_null
= 1;
9928 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
9929 warned_here
= pedwarn
9931 "%<return%> with a value, in function returning void");
9933 warned_here
= pedwarn
9934 (xloc
, OPT_Wpedantic
, "ISO C forbids "
9935 "%<return%> with expression, in function returning void");
9937 inform (DECL_SOURCE_LOCATION (current_function_decl
),
9942 tree t
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, valtype
,
9943 retval
, origtype
, ic_return
,
9944 npc
, NULL_TREE
, NULL_TREE
, 0);
9945 tree res
= DECL_RESULT (current_function_decl
);
9949 current_function_returns_value
= 1;
9950 if (t
== error_mark_node
)
9953 save
= in_late_binary_op
;
9954 if (TREE_CODE (TREE_TYPE (res
)) == BOOLEAN_TYPE
9955 || TREE_CODE (TREE_TYPE (res
)) == COMPLEX_TYPE
9956 || (TREE_CODE (TREE_TYPE (t
)) == REAL_TYPE
9957 && (TREE_CODE (TREE_TYPE (res
)) == INTEGER_TYPE
9958 || TREE_CODE (TREE_TYPE (res
)) == ENUMERAL_TYPE
)
9959 && sanitize_flags_p (SANITIZE_FLOAT_CAST
)))
9960 in_late_binary_op
= true;
9961 inner
= t
= convert (TREE_TYPE (res
), t
);
9962 in_late_binary_op
= save
;
9964 /* Strip any conversions, additions, and subtractions, and see if
9965 we are returning the address of a local variable. Warn if so. */
9968 switch (TREE_CODE (inner
))
9971 case NON_LVALUE_EXPR
:
9973 case POINTER_PLUS_EXPR
:
9974 inner
= TREE_OPERAND (inner
, 0);
9978 /* If the second operand of the MINUS_EXPR has a pointer
9979 type (or is converted from it), this may be valid, so
9980 don't give a warning. */
9982 tree op1
= TREE_OPERAND (inner
, 1);
9984 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
9985 && (CONVERT_EXPR_P (op1
)
9986 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
9987 op1
= TREE_OPERAND (op1
, 0);
9989 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
9992 inner
= TREE_OPERAND (inner
, 0);
9997 inner
= TREE_OPERAND (inner
, 0);
9999 while (REFERENCE_CLASS_P (inner
)
10000 && !INDIRECT_REF_P (inner
))
10001 inner
= TREE_OPERAND (inner
, 0);
10004 && !DECL_EXTERNAL (inner
)
10005 && !TREE_STATIC (inner
)
10006 && DECL_CONTEXT (inner
) == current_function_decl
)
10008 if (TREE_CODE (inner
) == LABEL_DECL
)
10009 warning_at (loc
, OPT_Wreturn_local_addr
,
10010 "function returns address of label");
10013 warning_at (loc
, OPT_Wreturn_local_addr
,
10014 "function returns address of local variable");
10015 tree zero
= build_zero_cst (TREE_TYPE (res
));
10016 t
= build2 (COMPOUND_EXPR
, TREE_TYPE (res
), t
, zero
);
10028 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
10029 SET_EXPR_LOCATION (retval
, loc
);
10031 if (warn_sequence_point
)
10032 verify_sequence_points (retval
);
10035 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
10036 TREE_NO_WARNING (ret_stmt
) |= no_warning
;
10037 return add_stmt (ret_stmt
);
10041 /* The SWITCH_EXPR being built. */
10044 /* The original type of the testing expression, i.e. before the
10045 default conversion is applied. */
10048 /* A splay-tree mapping the low element of a case range to the high
10049 element, or NULL_TREE if there is no high element. Used to
10050 determine whether or not a new case label duplicates an old case
10051 label. We need a tree, rather than simply a hash table, because
10052 of the GNU case range extension. */
10055 /* The bindings at the point of the switch. This is used for
10056 warnings crossing decls when branching to a case label. */
10057 struct c_spot_bindings
*bindings
;
10059 /* The next node on the stack. */
10060 struct c_switch
*next
;
10062 /* Remember whether the controlling expression had boolean type
10063 before integer promotions for the sake of -Wswitch-bool. */
10066 /* Remember whether there was a case value that is outside the
10067 range of the ORIG_TYPE. */
10068 bool outside_range_p
;
10071 /* A stack of the currently active switch statements. The innermost
10072 switch statement is on the top of the stack. There is no need to
10073 mark the stack for garbage collection because it is only active
10074 during the processing of the body of a function, and we never
10075 collect at that point. */
10077 struct c_switch
*c_switch_stack
;
10079 /* Start a C switch statement, testing expression EXP. Return the new
10080 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10081 SWITCH_COND_LOC is the location of the switch's condition.
10082 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10085 c_start_case (location_t switch_loc
,
10086 location_t switch_cond_loc
,
10087 tree exp
, bool explicit_cast_p
)
10089 tree orig_type
= error_mark_node
;
10090 bool bool_cond_p
= false;
10091 struct c_switch
*cs
;
10093 if (exp
!= error_mark_node
)
10095 orig_type
= TREE_TYPE (exp
);
10097 if (!INTEGRAL_TYPE_P (orig_type
))
10099 if (orig_type
!= error_mark_node
)
10101 error_at (switch_cond_loc
, "switch quantity not an integer");
10102 orig_type
= error_mark_node
;
10104 exp
= integer_zero_node
;
10108 tree type
= TYPE_MAIN_VARIANT (orig_type
);
10111 /* Warn if the condition has boolean value. */
10112 while (TREE_CODE (e
) == COMPOUND_EXPR
)
10113 e
= TREE_OPERAND (e
, 1);
10115 if ((TREE_CODE (type
) == BOOLEAN_TYPE
10116 || truth_value_p (TREE_CODE (e
)))
10117 /* Explicit cast to int suppresses this warning. */
10118 && !(TREE_CODE (type
) == INTEGER_TYPE
10119 && explicit_cast_p
))
10120 bool_cond_p
= true;
10122 if (!in_system_header_at (input_location
)
10123 && (type
== long_integer_type_node
10124 || type
== long_unsigned_type_node
))
10125 warning_at (switch_cond_loc
,
10126 OPT_Wtraditional
, "%<long%> switch expression not "
10127 "converted to %<int%> in ISO C");
10129 exp
= c_fully_fold (exp
, false, NULL
);
10130 exp
= default_conversion (exp
);
10132 if (warn_sequence_point
)
10133 verify_sequence_points (exp
);
10137 /* Add this new SWITCH_EXPR to the stack. */
10138 cs
= XNEW (struct c_switch
);
10139 cs
->switch_expr
= build3 (SWITCH_EXPR
, orig_type
, exp
, NULL_TREE
, NULL_TREE
);
10140 SET_EXPR_LOCATION (cs
->switch_expr
, switch_loc
);
10141 cs
->orig_type
= orig_type
;
10142 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
10143 cs
->bindings
= c_get_switch_bindings ();
10144 cs
->bool_cond_p
= bool_cond_p
;
10145 cs
->outside_range_p
= false;
10146 cs
->next
= c_switch_stack
;
10147 c_switch_stack
= cs
;
10149 return add_stmt (cs
->switch_expr
);
10152 /* Process a case label at location LOC. */
10155 do_case (location_t loc
, tree low_value
, tree high_value
)
10157 tree label
= NULL_TREE
;
10159 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
10161 low_value
= c_fully_fold (low_value
, false, NULL
);
10162 if (TREE_CODE (low_value
) == INTEGER_CST
)
10163 pedwarn (loc
, OPT_Wpedantic
,
10164 "case label is not an integer constant expression");
10167 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
10169 high_value
= c_fully_fold (high_value
, false, NULL
);
10170 if (TREE_CODE (high_value
) == INTEGER_CST
)
10171 pedwarn (input_location
, OPT_Wpedantic
,
10172 "case label is not an integer constant expression");
10175 if (c_switch_stack
== NULL
)
10178 error_at (loc
, "case label not within a switch statement");
10180 error_at (loc
, "%<default%> label not within a switch statement");
10184 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
10185 EXPR_LOCATION (c_switch_stack
->switch_expr
),
10189 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
10190 SWITCH_COND (c_switch_stack
->switch_expr
),
10191 c_switch_stack
->orig_type
,
10192 low_value
, high_value
,
10193 &c_switch_stack
->outside_range_p
);
10194 if (label
== error_mark_node
)
10199 /* Finish the switch statement. TYPE is the original type of the
10200 controlling expression of the switch, or NULL_TREE. */
10203 c_finish_case (tree body
, tree type
)
10205 struct c_switch
*cs
= c_switch_stack
;
10206 location_t switch_location
;
10208 SWITCH_BODY (cs
->switch_expr
) = body
;
10210 /* Emit warnings as needed. */
10211 switch_location
= EXPR_LOCATION (cs
->switch_expr
);
10212 c_do_switch_warnings (cs
->cases
, switch_location
,
10213 type
? type
: TREE_TYPE (cs
->switch_expr
),
10214 SWITCH_COND (cs
->switch_expr
),
10215 cs
->bool_cond_p
, cs
->outside_range_p
);
10217 /* Pop the stack. */
10218 c_switch_stack
= cs
->next
;
10219 splay_tree_delete (cs
->cases
);
10220 c_release_switch_bindings (cs
->bindings
);
10224 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10225 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10229 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
10234 /* If the condition has array notations, then the rank of the then_block and
10235 else_block must be either 0 or be equal to the rank of the condition. If
10236 the condition does not have array notations then break them up as it is
10237 broken up in a normal expression. */
10238 if (flag_cilkplus
&& contains_array_notation_expr (cond
))
10240 size_t then_rank
= 0, cond_rank
= 0, else_rank
= 0;
10241 if (!find_rank (if_locus
, cond
, cond
, true, &cond_rank
))
10244 && !find_rank (if_locus
, then_block
, then_block
, true, &then_rank
))
10247 && !find_rank (if_locus
, else_block
, else_block
, true, &else_rank
))
10249 if (cond_rank
!= then_rank
&& then_rank
!= 0)
10251 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
10252 " and the then-block");
10255 else if (cond_rank
!= else_rank
&& else_rank
!= 0)
10257 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
10258 " and the else-block");
10263 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
10264 SET_EXPR_LOCATION (stmt
, if_locus
);
10268 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10269 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10270 is false for DO loops. INCR is the FOR increment expression. BODY is
10271 the statement controlled by the loop. BLAB is the break label. CLAB is
10272 the continue label. Everything is allowed to be NULL. */
10275 c_finish_loop (location_t start_locus
, tree cond
, tree incr
, tree body
,
10276 tree blab
, tree clab
, bool cond_is_first
)
10278 tree entry
= NULL
, exit
= NULL
, t
;
10280 /* In theory could forbid cilk spawn for loop increment expression,
10281 but it should work just fine. */
10283 /* If the condition is zero don't generate a loop construct. */
10284 if (cond
&& integer_zerop (cond
))
10288 t
= build_and_jump (&blab
);
10289 SET_EXPR_LOCATION (t
, start_locus
);
10295 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
10297 /* If we have an exit condition, then we build an IF with gotos either
10298 out of the loop, or to the top of it. If there's no exit condition,
10299 then we just build a jump back to the top. */
10300 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
10302 if (cond
&& !integer_nonzerop (cond
))
10304 /* Canonicalize the loop condition to the end. This means
10305 generating a branch to the loop condition. Reuse the
10306 continue label, if possible. */
10311 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
10312 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
10315 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
10316 SET_EXPR_LOCATION (t
, start_locus
);
10320 t
= build_and_jump (&blab
);
10322 exit
= fold_build3_loc (start_locus
,
10323 COND_EXPR
, void_type_node
, cond
, exit
, t
);
10325 exit
= fold_build3_loc (input_location
,
10326 COND_EXPR
, void_type_node
, cond
, exit
, t
);
10330 /* For the backward-goto's location of an unconditional loop
10331 use the beginning of the body, or, if there is none, the
10332 top of the loop. */
10333 location_t loc
= EXPR_LOCATION (expr_first (body
));
10334 if (loc
== UNKNOWN_LOCATION
)
10336 SET_EXPR_LOCATION (exit
, loc
);
10345 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
10353 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
10357 c_finish_bc_stmt (location_t loc
, tree
*label_p
, bool is_break
)
10360 tree label
= *label_p
;
10362 /* In switch statements break is sometimes stylistically used after
10363 a return statement. This can lead to spurious warnings about
10364 control reaching the end of a non-void function when it is
10365 inlined. Note that we are calling block_may_fallthru with
10366 language specific tree nodes; this works because
10367 block_may_fallthru returns true when given something it does not
10369 skip
= !block_may_fallthru (cur_stmt_list
);
10374 *label_p
= label
= create_artificial_label (loc
);
10376 else if (TREE_CODE (label
) == LABEL_DECL
)
10378 else switch (TREE_INT_CST_LOW (label
))
10382 error_at (loc
, "break statement not within loop or switch");
10384 error_at (loc
, "continue statement not within a loop");
10388 gcc_assert (is_break
);
10389 error_at (loc
, "break statement used with OpenMP for loop");
10394 error ("break statement within %<#pragma simd%> loop body");
10396 error ("continue statement within %<#pragma simd%> loop body");
10400 gcc_unreachable ();
10407 add_stmt (build_predict_expr (PRED_CONTINUE
, NOT_TAKEN
));
10409 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
10412 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10415 emit_side_effect_warnings (location_t loc
, tree expr
)
10417 if (expr
== error_mark_node
)
10419 else if (!TREE_SIDE_EFFECTS (expr
))
10421 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
10422 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
10424 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
10427 location_t cloc
= loc
;
10428 while (TREE_CODE (r
) == COMPOUND_EXPR
)
10430 if (EXPR_HAS_LOCATION (r
))
10431 cloc
= EXPR_LOCATION (r
);
10432 r
= TREE_OPERAND (r
, 1);
10434 if (!TREE_SIDE_EFFECTS (r
)
10435 && !VOID_TYPE_P (TREE_TYPE (r
))
10436 && !CONVERT_EXPR_P (r
)
10437 && !TREE_NO_WARNING (r
)
10438 && !TREE_NO_WARNING (expr
))
10439 warning_at (cloc
, OPT_Wunused_value
,
10440 "right-hand operand of comma expression has no effect");
10443 warn_if_unused_value (expr
, loc
);
10446 /* Process an expression as if it were a complete statement. Emit
10447 diagnostics, but do not call ADD_STMT. LOC is the location of the
10451 c_process_expr_stmt (location_t loc
, tree expr
)
10458 expr
= c_fully_fold (expr
, false, NULL
);
10460 if (warn_sequence_point
)
10461 verify_sequence_points (expr
);
10463 if (TREE_TYPE (expr
) != error_mark_node
10464 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
10465 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
10466 error_at (loc
, "expression statement has incomplete type");
10468 /* If we're not processing a statement expression, warn about unused values.
10469 Warnings for statement expressions will be emitted later, once we figure
10470 out which is the result. */
10471 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
10472 && warn_unused_value
)
10473 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr
, loc
), expr
);
10476 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
10477 exprv
= TREE_OPERAND (exprv
, 1);
10478 while (CONVERT_EXPR_P (exprv
))
10479 exprv
= TREE_OPERAND (exprv
, 0);
10481 || handled_component_p (exprv
)
10482 || TREE_CODE (exprv
) == ADDR_EXPR
)
10483 mark_exp_read (exprv
);
10485 /* If the expression is not of a type to which we cannot assign a line
10486 number, wrap the thing in a no-op NOP_EXPR. */
10487 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
10489 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
10490 SET_EXPR_LOCATION (expr
, loc
);
10496 /* Emit an expression as a statement. LOC is the location of the
10500 c_finish_expr_stmt (location_t loc
, tree expr
)
10503 return add_stmt (c_process_expr_stmt (loc
, expr
));
10508 /* Do the opposite and emit a statement as an expression. To begin,
10509 create a new binding level and return it. */
10512 c_begin_stmt_expr (void)
10516 /* We must force a BLOCK for this level so that, if it is not expanded
10517 later, there is a way to turn off the entire subtree of blocks that
10518 are contained in it. */
10519 keep_next_level ();
10520 ret
= c_begin_compound_stmt (true);
10522 c_bindings_start_stmt_expr (c_switch_stack
== NULL
10524 : c_switch_stack
->bindings
);
10526 /* Mark the current statement list as belonging to a statement list. */
10527 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
10532 /* LOC is the location of the compound statement to which this body
10536 c_finish_stmt_expr (location_t loc
, tree body
)
10538 tree last
, type
, tmp
, val
;
10541 body
= c_end_compound_stmt (loc
, body
, true);
10543 c_bindings_end_stmt_expr (c_switch_stack
== NULL
10545 : c_switch_stack
->bindings
);
10547 /* Locate the last statement in BODY. See c_end_compound_stmt
10548 about always returning a BIND_EXPR. */
10549 last_p
= &BIND_EXPR_BODY (body
);
10550 last
= BIND_EXPR_BODY (body
);
10552 continue_searching
:
10553 if (TREE_CODE (last
) == STATEMENT_LIST
)
10555 tree_stmt_iterator i
;
10557 /* This can happen with degenerate cases like ({ }). No value. */
10558 if (!TREE_SIDE_EFFECTS (last
))
10561 /* If we're supposed to generate side effects warnings, process
10562 all of the statements except the last. */
10563 if (warn_unused_value
)
10565 for (i
= tsi_start (last
); !tsi_one_before_end_p (i
); tsi_next (&i
))
10568 tree t
= tsi_stmt (i
);
10570 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
10571 emit_side_effect_warnings (tloc
, t
);
10575 i
= tsi_last (last
);
10576 last_p
= tsi_stmt_ptr (i
);
10580 /* If the end of the list is exception related, then the list was split
10581 by a call to push_cleanup. Continue searching. */
10582 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
10583 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
10585 last_p
= &TREE_OPERAND (last
, 0);
10587 goto continue_searching
;
10590 if (last
== error_mark_node
)
10593 /* In the case that the BIND_EXPR is not necessary, return the
10594 expression out from inside it. */
10595 if (last
== BIND_EXPR_BODY (body
)
10596 && BIND_EXPR_VARS (body
) == NULL
)
10598 /* Even if this looks constant, do not allow it in a constant
10600 last
= c_wrap_maybe_const (last
, true);
10601 /* Do not warn if the return value of a statement expression is
10603 TREE_NO_WARNING (last
) = 1;
10607 /* Extract the type of said expression. */
10608 type
= TREE_TYPE (last
);
10610 /* If we're not returning a value at all, then the BIND_EXPR that
10611 we already have is a fine expression to return. */
10612 if (!type
|| VOID_TYPE_P (type
))
10615 /* Now that we've located the expression containing the value, it seems
10616 silly to make voidify_wrapper_expr repeat the process. Create a
10617 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10618 tmp
= create_tmp_var_raw (type
);
10620 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10621 tree_expr_nonnegative_p giving up immediately. */
10623 if (TREE_CODE (val
) == NOP_EXPR
10624 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
10625 val
= TREE_OPERAND (val
, 0);
10627 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
10628 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
10631 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
10632 SET_EXPR_LOCATION (t
, loc
);
10637 /* Begin and end compound statements. This is as simple as pushing
10638 and popping new statement lists from the tree. */
10641 c_begin_compound_stmt (bool do_scope
)
10643 tree stmt
= push_stmt_list ();
10649 /* End a compound statement. STMT is the statement. LOC is the
10650 location of the compound statement-- this is usually the location
10651 of the opening brace. */
10654 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
10660 if (c_dialect_objc ())
10661 objc_clear_super_receiver ();
10662 block
= pop_scope ();
10665 stmt
= pop_stmt_list (stmt
);
10666 stmt
= c_build_bind_expr (loc
, block
, stmt
);
10668 /* If this compound statement is nested immediately inside a statement
10669 expression, then force a BIND_EXPR to be created. Otherwise we'll
10670 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10671 STATEMENT_LISTs merge, and thus we can lose track of what statement
10672 was really last. */
10673 if (building_stmt_list_p ()
10674 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
10675 && TREE_CODE (stmt
) != BIND_EXPR
)
10677 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
10678 TREE_SIDE_EFFECTS (stmt
) = 1;
10679 SET_EXPR_LOCATION (stmt
, loc
);
10685 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10686 when the current scope is exited. EH_ONLY is true when this is not
10687 meant to apply to normal control flow transfer. */
10690 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
10692 enum tree_code code
;
10696 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
10697 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
10699 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
10700 list
= push_stmt_list ();
10701 TREE_OPERAND (stmt
, 0) = list
;
10702 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
10705 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10706 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10709 build_vec_cmp (tree_code code
, tree type
,
10710 tree arg0
, tree arg1
)
10712 tree zero_vec
= build_zero_cst (type
);
10713 tree minus_one_vec
= build_minus_one_cst (type
);
10714 tree cmp_type
= build_same_sized_truth_vector_type (type
);
10715 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
10716 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
10719 /* Build a binary-operation expression without default conversions.
10720 CODE is the kind of expression to build.
10721 LOCATION is the operator's location.
10722 This function differs from `build' in several ways:
10723 the data type of the result is computed and recorded in it,
10724 warnings are generated if arg data types are invalid,
10725 special handling for addition and subtraction of pointers is known,
10726 and some optimization is done (operations on narrow ints
10727 are done in the narrower type when that gives the same result).
10728 Constant folding is also done before the result is returned.
10730 Note that the operands will never have enumeral types, or function
10731 or array types, because either they will have the default conversions
10732 performed or they have both just been converted to some other type in which
10733 the arithmetic is to be done. */
10736 build_binary_op (location_t location
, enum tree_code code
,
10737 tree orig_op0
, tree orig_op1
, int convert_p
)
10739 tree type0
, type1
, orig_type0
, orig_type1
;
10741 enum tree_code code0
, code1
;
10743 tree ret
= error_mark_node
;
10744 const char *invalid_op_diag
;
10745 bool op0_int_operands
, op1_int_operands
;
10746 bool int_const
, int_const_or_overflow
, int_operands
;
10748 /* Expression code to give to the expression when it is built.
10749 Normally this is CODE, which is what the caller asked for,
10750 but in some special cases we change it. */
10751 enum tree_code resultcode
= code
;
10753 /* Data type in which the computation is to be performed.
10754 In the simplest cases this is the common type of the arguments. */
10755 tree result_type
= NULL
;
10757 /* When the computation is in excess precision, the type of the
10758 final EXCESS_PRECISION_EXPR. */
10759 tree semantic_result_type
= NULL
;
10761 /* Nonzero means operands have already been type-converted
10762 in whatever way is necessary.
10763 Zero means they need to be converted to RESULT_TYPE. */
10766 /* Nonzero means create the expression with this type, rather than
10768 tree build_type
= NULL_TREE
;
10770 /* Nonzero means after finally constructing the expression
10771 convert it to this type. */
10772 tree final_type
= NULL_TREE
;
10774 /* Nonzero if this is an operation like MIN or MAX which can
10775 safely be computed in short if both args are promoted shorts.
10776 Also implies COMMON.
10777 -1 indicates a bitwise operation; this makes a difference
10778 in the exact conditions for when it is safe to do the operation
10779 in a narrower mode. */
10782 /* Nonzero if this is a comparison operation;
10783 if both args are promoted shorts, compare the original shorts.
10784 Also implies COMMON. */
10785 int short_compare
= 0;
10787 /* Nonzero if this is a right-shift operation, which can be computed on the
10788 original short and then promoted if the operand is a promoted short. */
10789 int short_shift
= 0;
10791 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10794 /* True means types are compatible as far as ObjC is concerned. */
10797 /* True means this is an arithmetic operation that may need excess
10799 bool may_need_excess_precision
;
10801 /* True means this is a boolean operation that converts both its
10802 operands to truth-values. */
10803 bool boolean_op
= false;
10805 /* Remember whether we're doing / or %. */
10806 bool doing_div_or_mod
= false;
10808 /* Remember whether we're doing << or >>. */
10809 bool doing_shift
= false;
10811 /* Tree holding instrumentation expression. */
10812 tree instrument_expr
= NULL
;
10814 if (location
== UNKNOWN_LOCATION
)
10815 location
= input_location
;
10820 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
10821 if (op0_int_operands
)
10822 op0
= remove_c_maybe_const_expr (op0
);
10823 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
10824 if (op1_int_operands
)
10825 op1
= remove_c_maybe_const_expr (op1
);
10826 int_operands
= (op0_int_operands
&& op1_int_operands
);
10829 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
10830 && TREE_CODE (orig_op1
) == INTEGER_CST
);
10831 int_const
= (int_const_or_overflow
10832 && !TREE_OVERFLOW (orig_op0
)
10833 && !TREE_OVERFLOW (orig_op1
));
10836 int_const
= int_const_or_overflow
= false;
10838 /* Do not apply default conversion in mixed vector/scalar expression. */
10840 && VECTOR_TYPE_P (TREE_TYPE (op0
)) == VECTOR_TYPE_P (TREE_TYPE (op1
)))
10842 op0
= default_conversion (op0
);
10843 op1
= default_conversion (op1
);
10846 /* When Cilk Plus is enabled and there are array notations inside op0, then
10847 we check to see if there are builtin array notation functions. If
10848 so, then we take on the type of the array notation inside it. */
10849 if (flag_cilkplus
&& contains_array_notation_expr (op0
))
10850 orig_type0
= type0
= find_correct_array_notation_type (op0
);
10852 orig_type0
= type0
= TREE_TYPE (op0
);
10854 if (flag_cilkplus
&& contains_array_notation_expr (op1
))
10855 orig_type1
= type1
= find_correct_array_notation_type (op1
);
10857 orig_type1
= type1
= TREE_TYPE (op1
);
10859 /* The expression codes of the data types of the arguments tell us
10860 whether the arguments are integers, floating, pointers, etc. */
10861 code0
= TREE_CODE (type0
);
10862 code1
= TREE_CODE (type1
);
10864 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10865 STRIP_TYPE_NOPS (op0
);
10866 STRIP_TYPE_NOPS (op1
);
10868 /* If an error was already reported for one of the arguments,
10869 avoid reporting another error. */
10871 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10872 return error_mark_node
;
10874 if (code0
== POINTER_TYPE
10875 && reject_gcc_builtin (op0
, EXPR_LOCATION (orig_op0
)))
10876 return error_mark_node
;
10878 if (code1
== POINTER_TYPE
10879 && reject_gcc_builtin (op1
, EXPR_LOCATION (orig_op1
)))
10880 return error_mark_node
;
10882 if ((invalid_op_diag
10883 = targetm
.invalid_binary_op (code
, type0
, type1
)))
10885 error_at (location
, invalid_op_diag
);
10886 return error_mark_node
;
10894 case TRUNC_DIV_EXPR
:
10895 case CEIL_DIV_EXPR
:
10896 case FLOOR_DIV_EXPR
:
10897 case ROUND_DIV_EXPR
:
10898 case EXACT_DIV_EXPR
:
10899 may_need_excess_precision
= true;
10902 may_need_excess_precision
= false;
10905 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
10907 op0
= TREE_OPERAND (op0
, 0);
10908 type0
= TREE_TYPE (op0
);
10910 else if (may_need_excess_precision
10911 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
10914 op0
= convert (eptype
, op0
);
10916 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
10918 op1
= TREE_OPERAND (op1
, 0);
10919 type1
= TREE_TYPE (op1
);
10921 else if (may_need_excess_precision
10922 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
10925 op1
= convert (eptype
, op1
);
10928 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
10930 /* In case when one of the operands of the binary operation is
10931 a vector and another is a scalar -- convert scalar to vector. */
10932 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
10934 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
10937 switch (convert_flag
)
10940 return error_mark_node
;
10943 bool maybe_const
= true;
10945 sc
= c_fully_fold (op0
, false, &maybe_const
);
10946 sc
= save_expr (sc
);
10947 sc
= convert (TREE_TYPE (type1
), sc
);
10948 op0
= build_vector_from_val (type1
, sc
);
10950 op0
= c_wrap_maybe_const (op0
, true);
10951 orig_type0
= type0
= TREE_TYPE (op0
);
10952 code0
= TREE_CODE (type0
);
10956 case stv_secondarg
:
10958 bool maybe_const
= true;
10960 sc
= c_fully_fold (op1
, false, &maybe_const
);
10961 sc
= save_expr (sc
);
10962 sc
= convert (TREE_TYPE (type0
), sc
);
10963 op1
= build_vector_from_val (type0
, sc
);
10965 op1
= c_wrap_maybe_const (op1
, true);
10966 orig_type1
= type1
= TREE_TYPE (op1
);
10967 code1
= TREE_CODE (type1
);
10979 /* Handle the pointer + int case. */
10980 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10982 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
10983 goto return_build_binary_op
;
10985 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
10987 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
10988 goto return_build_binary_op
;
10995 /* Subtraction of two similar pointers.
10996 We must subtract them as integers, then divide by object size. */
10997 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
10998 && comp_target_types (location
, type0
, type1
))
11000 ret
= pointer_diff (location
, op0
, op1
);
11001 goto return_build_binary_op
;
11003 /* Handle pointer minus int. Just like pointer plus int. */
11004 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
11006 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
11007 goto return_build_binary_op
;
11017 case TRUNC_DIV_EXPR
:
11018 case CEIL_DIV_EXPR
:
11019 case FLOOR_DIV_EXPR
:
11020 case ROUND_DIV_EXPR
:
11021 case EXACT_DIV_EXPR
:
11022 doing_div_or_mod
= true;
11023 warn_for_div_by_zero (location
, op1
);
11025 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
11026 || code0
== FIXED_POINT_TYPE
11027 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
11028 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
11029 || code1
== FIXED_POINT_TYPE
11030 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
11032 enum tree_code tcode0
= code0
, tcode1
= code1
;
11034 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
11035 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
11036 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
11037 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
11039 if (!((tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
)
11040 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
11041 resultcode
= RDIV_EXPR
;
11043 /* Although it would be tempting to shorten always here, that
11044 loses on some targets, since the modulo instruction is
11045 undefined if the quotient can't be represented in the
11046 computation mode. We shorten only if unsigned or if
11047 dividing by something we know != -1. */
11048 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
11049 || (TREE_CODE (op1
) == INTEGER_CST
11050 && !integer_all_onesp (op1
)));
11058 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
11060 /* Allow vector types which are not floating point types. */
11061 else if (code0
== VECTOR_TYPE
11062 && code1
== VECTOR_TYPE
11063 && !VECTOR_FLOAT_TYPE_P (type0
)
11064 && !VECTOR_FLOAT_TYPE_P (type1
))
11068 case TRUNC_MOD_EXPR
:
11069 case FLOOR_MOD_EXPR
:
11070 doing_div_or_mod
= true;
11071 warn_for_div_by_zero (location
, op1
);
11073 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11074 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
11075 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
11077 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
11079 /* Although it would be tempting to shorten always here, that loses
11080 on some targets, since the modulo instruction is undefined if the
11081 quotient can't be represented in the computation mode. We shorten
11082 only if unsigned or if dividing by something we know != -1. */
11083 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
11084 || (TREE_CODE (op1
) == INTEGER_CST
11085 && !integer_all_onesp (op1
)));
11090 case TRUTH_ANDIF_EXPR
:
11091 case TRUTH_ORIF_EXPR
:
11092 case TRUTH_AND_EXPR
:
11093 case TRUTH_OR_EXPR
:
11094 case TRUTH_XOR_EXPR
:
11095 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
11096 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
11097 || code0
== FIXED_POINT_TYPE
)
11098 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
11099 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
11100 || code1
== FIXED_POINT_TYPE
))
11102 /* Result of these operations is always an int,
11103 but that does not mean the operands should be
11104 converted to ints! */
11105 result_type
= integer_type_node
;
11106 if (op0_int_operands
)
11108 op0
= c_objc_common_truthvalue_conversion (location
, orig_op0
);
11109 op0
= remove_c_maybe_const_expr (op0
);
11112 op0
= c_objc_common_truthvalue_conversion (location
, op0
);
11113 if (op1_int_operands
)
11115 op1
= c_objc_common_truthvalue_conversion (location
, orig_op1
);
11116 op1
= remove_c_maybe_const_expr (op1
);
11119 op1
= c_objc_common_truthvalue_conversion (location
, op1
);
11123 if (code
== TRUTH_ANDIF_EXPR
)
11125 int_const_or_overflow
= (int_operands
11126 && TREE_CODE (orig_op0
) == INTEGER_CST
11127 && (op0
== truthvalue_false_node
11128 || TREE_CODE (orig_op1
) == INTEGER_CST
));
11129 int_const
= (int_const_or_overflow
11130 && !TREE_OVERFLOW (orig_op0
)
11131 && (op0
== truthvalue_false_node
11132 || !TREE_OVERFLOW (orig_op1
)));
11134 else if (code
== TRUTH_ORIF_EXPR
)
11136 int_const_or_overflow
= (int_operands
11137 && TREE_CODE (orig_op0
) == INTEGER_CST
11138 && (op0
== truthvalue_true_node
11139 || TREE_CODE (orig_op1
) == INTEGER_CST
));
11140 int_const
= (int_const_or_overflow
11141 && !TREE_OVERFLOW (orig_op0
)
11142 && (op0
== truthvalue_true_node
11143 || !TREE_OVERFLOW (orig_op1
)));
11147 /* Shift operations: result has same type as first operand;
11148 always convert second operand to int.
11149 Also set SHORT_SHIFT if shifting rightward. */
11152 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11153 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
11154 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
11155 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
11157 result_type
= type0
;
11160 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
11161 || code0
== VECTOR_TYPE
)
11162 && code1
== INTEGER_TYPE
)
11164 doing_shift
= true;
11165 if (TREE_CODE (op1
) == INTEGER_CST
)
11167 if (tree_int_cst_sgn (op1
) < 0)
11170 if (c_inhibit_evaluation_warnings
== 0)
11171 warning_at (location
, OPT_Wshift_count_negative
,
11172 "right shift count is negative");
11174 else if (code0
== VECTOR_TYPE
)
11176 if (compare_tree_int (op1
,
11177 TYPE_PRECISION (TREE_TYPE (type0
)))
11181 if (c_inhibit_evaluation_warnings
== 0)
11182 warning_at (location
, OPT_Wshift_count_overflow
,
11183 "right shift count >= width of vector element");
11188 if (!integer_zerop (op1
))
11191 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
11194 if (c_inhibit_evaluation_warnings
== 0)
11195 warning_at (location
, OPT_Wshift_count_overflow
,
11196 "right shift count >= width of type");
11201 /* Use the type of the value to be shifted. */
11202 result_type
= type0
;
11203 /* Avoid converting op1 to result_type later. */
11209 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11210 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
11211 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
11212 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
11214 result_type
= type0
;
11217 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
11218 || code0
== VECTOR_TYPE
)
11219 && code1
== INTEGER_TYPE
)
11221 doing_shift
= true;
11222 if (TREE_CODE (op0
) == INTEGER_CST
11223 && tree_int_cst_sgn (op0
) < 0)
11225 /* Don't reject a left shift of a negative value in a context
11226 where a constant expression is needed in C90. */
11229 if (c_inhibit_evaluation_warnings
== 0)
11230 warning_at (location
, OPT_Wshift_negative_value
,
11231 "left shift of negative value");
11233 if (TREE_CODE (op1
) == INTEGER_CST
)
11235 if (tree_int_cst_sgn (op1
) < 0)
11238 if (c_inhibit_evaluation_warnings
== 0)
11239 warning_at (location
, OPT_Wshift_count_negative
,
11240 "left shift count is negative");
11242 else if (code0
== VECTOR_TYPE
)
11244 if (compare_tree_int (op1
,
11245 TYPE_PRECISION (TREE_TYPE (type0
)))
11249 if (c_inhibit_evaluation_warnings
== 0)
11250 warning_at (location
, OPT_Wshift_count_overflow
,
11251 "left shift count >= width of vector element");
11254 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
11257 if (c_inhibit_evaluation_warnings
== 0)
11258 warning_at (location
, OPT_Wshift_count_overflow
,
11259 "left shift count >= width of type");
11261 else if (TREE_CODE (op0
) == INTEGER_CST
11262 && maybe_warn_shift_overflow (location
, op0
, op1
)
11267 /* Use the type of the value to be shifted. */
11268 result_type
= type0
;
11269 /* Avoid converting op1 to result_type later. */
11276 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
11279 if (!vector_types_compatible_elements_p (type0
, type1
))
11281 error_at (location
, "comparing vectors with different "
11283 return error_mark_node
;
11286 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
11288 error_at (location
, "comparing vectors with different "
11289 "number of elements");
11290 return error_mark_node
;
11293 /* It's not precisely specified how the usual arithmetic
11294 conversions apply to the vector types. Here, we use
11295 the unsigned type if one of the operands is signed and
11296 the other one is unsigned. */
11297 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
11299 if (!TYPE_UNSIGNED (type0
))
11300 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
11302 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
11303 warning_at (location
, OPT_Wsign_compare
, "comparison between "
11304 "types %qT and %qT", type0
, type1
);
11307 /* Always construct signed integer vector type. */
11308 intt
= c_common_type_for_size (GET_MODE_BITSIZE
11309 (TYPE_MODE (TREE_TYPE (type0
))), 0);
11310 result_type
= build_opaque_vector_type (intt
,
11311 TYPE_VECTOR_SUBPARTS (type0
));
11313 ret
= build_vec_cmp (resultcode
, result_type
, op0
, op1
);
11314 goto return_build_binary_op
;
11316 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
11317 warning_at (location
,
11319 "comparing floating point with == or != is unsafe");
11320 /* Result of comparison is always int,
11321 but don't convert the args to int! */
11322 build_type
= integer_type_node
;
11323 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
11324 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
11325 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
11326 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
11328 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
11330 if (TREE_CODE (op0
) == ADDR_EXPR
11331 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0))
11332 && !from_macro_expansion_at (location
))
11334 if (code
== EQ_EXPR
)
11335 warning_at (location
,
11337 "the comparison will always evaluate as %<false%> "
11338 "for the address of %qD will never be NULL",
11339 TREE_OPERAND (op0
, 0));
11341 warning_at (location
,
11343 "the comparison will always evaluate as %<true%> "
11344 "for the address of %qD will never be NULL",
11345 TREE_OPERAND (op0
, 0));
11347 result_type
= type0
;
11349 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
11351 if (TREE_CODE (op1
) == ADDR_EXPR
11352 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0))
11353 && !from_macro_expansion_at (location
))
11355 if (code
== EQ_EXPR
)
11356 warning_at (location
,
11358 "the comparison will always evaluate as %<false%> "
11359 "for the address of %qD will never be NULL",
11360 TREE_OPERAND (op1
, 0));
11362 warning_at (location
,
11364 "the comparison will always evaluate as %<true%> "
11365 "for the address of %qD will never be NULL",
11366 TREE_OPERAND (op1
, 0));
11368 result_type
= type1
;
11370 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
11372 tree tt0
= TREE_TYPE (type0
);
11373 tree tt1
= TREE_TYPE (type1
);
11374 addr_space_t as0
= TYPE_ADDR_SPACE (tt0
);
11375 addr_space_t as1
= TYPE_ADDR_SPACE (tt1
);
11376 addr_space_t as_common
= ADDR_SPACE_GENERIC
;
11378 /* Anything compares with void *. void * compares with anything.
11379 Otherwise, the targets must be compatible
11380 and both must be object or both incomplete. */
11381 if (comp_target_types (location
, type0
, type1
))
11382 result_type
= common_pointer_type (type0
, type1
);
11383 else if (!addr_space_superset (as0
, as1
, &as_common
))
11385 error_at (location
, "comparison of pointers to "
11386 "disjoint address spaces");
11387 return error_mark_node
;
11389 else if (VOID_TYPE_P (tt0
) && !TYPE_ATOMIC (tt0
))
11391 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
)
11392 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
11393 "comparison of %<void *%> with function pointer");
11395 else if (VOID_TYPE_P (tt1
) && !TYPE_ATOMIC (tt1
))
11397 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
)
11398 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
11399 "comparison of %<void *%> with function pointer");
11402 /* Avoid warning about the volatile ObjC EH puts on decls. */
11404 pedwarn (location
, 0,
11405 "comparison of distinct pointer types lacks a cast");
11407 if (result_type
== NULL_TREE
)
11409 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
11410 result_type
= build_pointer_type
11411 (build_qualified_type (void_type_node
, qual
));
11414 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
11416 result_type
= type0
;
11417 pedwarn (location
, 0, "comparison between pointer and integer");
11419 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
11421 result_type
= type1
;
11422 pedwarn (location
, 0, "comparison between pointer and integer");
11424 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
11425 || truth_value_p (TREE_CODE (orig_op0
)))
11426 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
11427 || truth_value_p (TREE_CODE (orig_op1
))))
11428 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
11435 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
11438 if (!vector_types_compatible_elements_p (type0
, type1
))
11440 error_at (location
, "comparing vectors with different "
11442 return error_mark_node
;
11445 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
11447 error_at (location
, "comparing vectors with different "
11448 "number of elements");
11449 return error_mark_node
;
11452 /* It's not precisely specified how the usual arithmetic
11453 conversions apply to the vector types. Here, we use
11454 the unsigned type if one of the operands is signed and
11455 the other one is unsigned. */
11456 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
11458 if (!TYPE_UNSIGNED (type0
))
11459 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
11461 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
11462 warning_at (location
, OPT_Wsign_compare
, "comparison between "
11463 "types %qT and %qT", type0
, type1
);
11466 /* Always construct signed integer vector type. */
11467 intt
= c_common_type_for_size (GET_MODE_BITSIZE
11468 (TYPE_MODE (TREE_TYPE (type0
))), 0);
11469 result_type
= build_opaque_vector_type (intt
,
11470 TYPE_VECTOR_SUBPARTS (type0
));
11472 ret
= build_vec_cmp (resultcode
, result_type
, op0
, op1
);
11473 goto return_build_binary_op
;
11475 build_type
= integer_type_node
;
11476 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
11477 || code0
== FIXED_POINT_TYPE
)
11478 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
11479 || code1
== FIXED_POINT_TYPE
))
11481 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
11483 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (type0
));
11484 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
11485 addr_space_t as_common
;
11487 if (comp_target_types (location
, type0
, type1
))
11489 result_type
= common_pointer_type (type0
, type1
);
11490 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
11491 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
11492 pedwarn (location
, 0,
11493 "comparison of complete and incomplete pointers");
11494 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
11495 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
11496 "ordered comparisons of pointers to functions");
11497 else if (null_pointer_constant_p (orig_op0
)
11498 || null_pointer_constant_p (orig_op1
))
11499 warning_at (location
, OPT_Wextra
,
11500 "ordered comparison of pointer with null pointer");
11503 else if (!addr_space_superset (as0
, as1
, &as_common
))
11505 error_at (location
, "comparison of pointers to "
11506 "disjoint address spaces");
11507 return error_mark_node
;
11511 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
11512 result_type
= build_pointer_type
11513 (build_qualified_type (void_type_node
, qual
));
11514 pedwarn (location
, 0,
11515 "comparison of distinct pointer types lacks a cast");
11518 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
11520 result_type
= type0
;
11522 pedwarn (location
, OPT_Wpedantic
,
11523 "ordered comparison of pointer with integer zero");
11524 else if (extra_warnings
)
11525 warning_at (location
, OPT_Wextra
,
11526 "ordered comparison of pointer with integer zero");
11528 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
11530 result_type
= type1
;
11532 pedwarn (location
, OPT_Wpedantic
,
11533 "ordered comparison of pointer with integer zero");
11534 else if (extra_warnings
)
11535 warning_at (location
, OPT_Wextra
,
11536 "ordered comparison of pointer with integer zero");
11538 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
11540 result_type
= type0
;
11541 pedwarn (location
, 0, "comparison between pointer and integer");
11543 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
11545 result_type
= type1
;
11546 pedwarn (location
, 0, "comparison between pointer and integer");
11548 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
11549 || truth_value_p (TREE_CODE (orig_op0
)))
11550 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
11551 || truth_value_p (TREE_CODE (orig_op1
))))
11552 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
11556 gcc_unreachable ();
11559 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
11560 return error_mark_node
;
11562 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11563 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
11564 || !vector_types_compatible_elements_p (type0
, type1
)))
11566 gcc_rich_location
richloc (location
);
11567 richloc
.maybe_add_expr (orig_op0
);
11568 richloc
.maybe_add_expr (orig_op1
);
11569 binary_op_error (&richloc
, code
, type0
, type1
);
11570 return error_mark_node
;
11573 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
11574 || code0
== FIXED_POINT_TYPE
|| code0
== VECTOR_TYPE
)
11576 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
11577 || code1
== FIXED_POINT_TYPE
|| code1
== VECTOR_TYPE
))
11579 bool first_complex
= (code0
== COMPLEX_TYPE
);
11580 bool second_complex
= (code1
== COMPLEX_TYPE
);
11581 int none_complex
= (!first_complex
&& !second_complex
);
11583 if (shorten
|| common
|| short_compare
)
11585 result_type
= c_common_type (type0
, type1
);
11586 do_warn_double_promotion (result_type
, type0
, type1
,
11587 "implicit conversion from %qT to %qT "
11588 "to match other operand of binary "
11591 if (result_type
== error_mark_node
)
11592 return error_mark_node
;
11595 if (first_complex
!= second_complex
11596 && (code
== PLUS_EXPR
11597 || code
== MINUS_EXPR
11598 || code
== MULT_EXPR
11599 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
11600 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
11601 && flag_signed_zeros
)
11603 /* An operation on mixed real/complex operands must be
11604 handled specially, but the language-independent code can
11605 more easily optimize the plain complex arithmetic if
11606 -fno-signed-zeros. */
11607 tree real_type
= TREE_TYPE (result_type
);
11609 if (type0
!= orig_type0
|| type1
!= orig_type1
)
11611 gcc_assert (may_need_excess_precision
&& common
);
11612 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
11616 if (TREE_TYPE (op0
) != result_type
)
11617 op0
= convert_and_check (location
, result_type
, op0
);
11618 if (TREE_TYPE (op1
) != real_type
)
11619 op1
= convert_and_check (location
, real_type
, op1
);
11623 if (TREE_TYPE (op0
) != real_type
)
11624 op0
= convert_and_check (location
, real_type
, op0
);
11625 if (TREE_TYPE (op1
) != result_type
)
11626 op1
= convert_and_check (location
, result_type
, op1
);
11628 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
11629 return error_mark_node
;
11632 op0
= save_expr (op0
);
11633 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
11635 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
11640 case TRUNC_DIV_EXPR
:
11641 op1
= save_expr (op1
);
11642 imag
= build2 (resultcode
, real_type
, imag
, op1
);
11643 /* Fall through. */
11646 real
= build2 (resultcode
, real_type
, real
, op1
);
11654 op1
= save_expr (op1
);
11655 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
11657 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
11662 op0
= save_expr (op0
);
11663 imag
= build2 (resultcode
, real_type
, op0
, imag
);
11664 /* Fall through. */
11666 real
= build2 (resultcode
, real_type
, op0
, real
);
11669 real
= build2 (resultcode
, real_type
, op0
, real
);
11670 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
11676 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
11677 goto return_build_binary_op
;
11680 /* For certain operations (which identify themselves by shorten != 0)
11681 if both args were extended from the same smaller type,
11682 do the arithmetic in that type and then extend.
11684 shorten !=0 and !=1 indicates a bitwise operation.
11685 For them, this optimization is safe only if
11686 both args are zero-extended or both are sign-extended.
11687 Otherwise, we might change the result.
11688 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11689 but calculated in (unsigned short) it would be (unsigned short)-1. */
11691 if (shorten
&& none_complex
)
11693 final_type
= result_type
;
11694 result_type
= shorten_binary_op (result_type
, op0
, op1
,
11698 /* Shifts can be shortened if shifting right. */
11703 tree arg0
= get_narrower (op0
, &unsigned_arg
);
11705 final_type
= result_type
;
11707 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
11708 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
11710 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
11711 && tree_int_cst_sgn (op1
) > 0
11712 /* We can shorten only if the shift count is less than the
11713 number of bits in the smaller type size. */
11714 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
11715 /* We cannot drop an unsigned shift after sign-extension. */
11716 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
11718 /* Do an unsigned shift if the operand was zero-extended. */
11720 = c_common_signed_or_unsigned_type (unsigned_arg
,
11722 /* Convert value-to-be-shifted to that type. */
11723 if (TREE_TYPE (op0
) != result_type
)
11724 op0
= convert (result_type
, op0
);
11729 /* Comparison operations are shortened too but differently.
11730 They identify themselves by setting short_compare = 1. */
11734 /* Don't write &op0, etc., because that would prevent op0
11735 from being kept in a register.
11736 Instead, make copies of the our local variables and
11737 pass the copies by reference, then copy them back afterward. */
11738 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
11739 enum tree_code xresultcode
= resultcode
;
11741 = shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
11744 if (val
!= NULL_TREE
)
11747 goto return_build_binary_op
;
11750 op0
= xop0
, op1
= xop1
;
11752 resultcode
= xresultcode
;
11754 if (c_inhibit_evaluation_warnings
== 0)
11756 bool op0_maybe_const
= true;
11757 bool op1_maybe_const
= true;
11758 tree orig_op0_folded
, orig_op1_folded
;
11760 if (in_late_binary_op
)
11762 orig_op0_folded
= orig_op0
;
11763 orig_op1_folded
= orig_op1
;
11767 /* Fold for the sake of possible warnings, as in
11768 build_conditional_expr. This requires the
11769 "original" values to be folded, not just op0 and
11771 c_inhibit_evaluation_warnings
++;
11772 op0
= c_fully_fold (op0
, require_constant_value
,
11774 op1
= c_fully_fold (op1
, require_constant_value
,
11776 c_inhibit_evaluation_warnings
--;
11777 orig_op0_folded
= c_fully_fold (orig_op0
,
11778 require_constant_value
,
11780 orig_op1_folded
= c_fully_fold (orig_op1
,
11781 require_constant_value
,
11785 if (warn_sign_compare
)
11786 warn_for_sign_compare (location
, orig_op0_folded
,
11787 orig_op1_folded
, op0
, op1
,
11788 result_type
, resultcode
);
11789 if (!in_late_binary_op
&& !int_operands
)
11791 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
11792 op0
= c_wrap_maybe_const (op0
, !op0_maybe_const
);
11793 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
11794 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
11800 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11801 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11802 Then the expression will be built.
11803 It will be given type FINAL_TYPE if that is nonzero;
11804 otherwise, it will be given type RESULT_TYPE. */
11808 gcc_rich_location
richloc (location
);
11809 richloc
.maybe_add_expr (orig_op0
);
11810 richloc
.maybe_add_expr (orig_op1
);
11811 binary_op_error (&richloc
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
11812 return error_mark_node
;
11815 if (build_type
== NULL_TREE
)
11817 build_type
= result_type
;
11818 if ((type0
!= orig_type0
|| type1
!= orig_type1
)
11821 gcc_assert (may_need_excess_precision
&& common
);
11822 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
11828 op0
= ep_convert_and_check (location
, result_type
, op0
,
11829 semantic_result_type
);
11830 op1
= ep_convert_and_check (location
, result_type
, op1
,
11831 semantic_result_type
);
11833 /* This can happen if one operand has a vector type, and the other
11834 has a different type. */
11835 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
11836 return error_mark_node
;
11839 if (sanitize_flags_p ((SANITIZE_SHIFT
11840 | SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
11841 && current_function_decl
!= NULL_TREE
11842 && (doing_div_or_mod
|| doing_shift
)
11843 && !require_constant_value
)
11845 /* OP0 and/or OP1 might have side-effects. */
11846 op0
= save_expr (op0
);
11847 op1
= save_expr (op1
);
11848 op0
= c_fully_fold (op0
, false, NULL
);
11849 op1
= c_fully_fold (op1
, false, NULL
);
11850 if (doing_div_or_mod
&& (sanitize_flags_p ((SANITIZE_DIVIDE
11851 | SANITIZE_FLOAT_DIVIDE
))))
11852 instrument_expr
= ubsan_instrument_division (location
, op0
, op1
);
11853 else if (doing_shift
&& sanitize_flags_p (SANITIZE_SHIFT
))
11854 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
11857 /* Treat expressions in initializers specially as they can't trap. */
11858 if (int_const_or_overflow
)
11859 ret
= (require_constant_value
11860 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
11862 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
11864 ret
= build2 (resultcode
, build_type
, op0
, op1
);
11865 if (final_type
!= NULL_TREE
)
11866 ret
= convert (final_type
, ret
);
11868 return_build_binary_op
:
11869 gcc_assert (ret
!= error_mark_node
);
11870 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
11871 ret
= (int_operands
11872 ? note_integer_operands (ret
)
11873 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
11874 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
11875 && !in_late_binary_op
)
11876 ret
= note_integer_operands (ret
);
11877 protected_set_expr_location (ret
, location
);
11879 if (instrument_expr
!= NULL
)
11880 ret
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (ret
),
11881 instrument_expr
, ret
);
11883 if (semantic_result_type
)
11884 ret
= build1_loc (location
, EXCESS_PRECISION_EXPR
,
11885 semantic_result_type
, ret
);
11891 /* Convert EXPR to be a truth-value, validating its type for this
11892 purpose. LOCATION is the source location for the expression. */
11895 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
11897 bool int_const
, int_operands
;
11899 switch (TREE_CODE (TREE_TYPE (expr
)))
11902 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
11903 return error_mark_node
;
11906 error_at (location
, "used struct type value where scalar is required");
11907 return error_mark_node
;
11910 error_at (location
, "used union type value where scalar is required");
11911 return error_mark_node
;
11914 error_at (location
, "void value not ignored as it ought to be");
11915 return error_mark_node
;
11918 if (reject_gcc_builtin (expr
))
11919 return error_mark_node
;
11922 case FUNCTION_TYPE
:
11923 gcc_unreachable ();
11926 error_at (location
, "used vector type where scalar is required");
11927 return error_mark_node
;
11933 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
11934 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
11935 if (int_operands
&& TREE_CODE (expr
) != INTEGER_CST
)
11937 expr
= remove_c_maybe_const_expr (expr
);
11938 expr
= build2 (NE_EXPR
, integer_type_node
, expr
,
11939 convert (TREE_TYPE (expr
), integer_zero_node
));
11940 expr
= note_integer_operands (expr
);
11943 /* ??? Should we also give an error for vectors rather than leaving
11944 those to give errors later? */
11945 expr
= c_common_truthvalue_conversion (location
, expr
);
11947 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
11949 if (TREE_OVERFLOW (expr
))
11952 return note_integer_operands (expr
);
11954 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
11955 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
11960 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11964 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
11966 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
11968 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
11969 /* Executing a compound literal inside a function reinitializes
11971 if (!TREE_STATIC (decl
))
11979 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11980 statement. LOC is the location of the construct. */
11983 c_finish_omp_construct (location_t loc
, enum tree_code code
, tree body
,
11986 body
= c_end_compound_stmt (loc
, body
, true);
11988 tree stmt
= make_node (code
);
11989 TREE_TYPE (stmt
) = void_type_node
;
11990 OMP_BODY (stmt
) = body
;
11991 OMP_CLAUSES (stmt
) = clauses
;
11992 SET_EXPR_LOCATION (stmt
, loc
);
11994 return add_stmt (stmt
);
11997 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11998 statement. LOC is the location of the OACC_DATA. */
12001 c_finish_oacc_data (location_t loc
, tree clauses
, tree block
)
12005 block
= c_end_compound_stmt (loc
, block
, true);
12007 stmt
= make_node (OACC_DATA
);
12008 TREE_TYPE (stmt
) = void_type_node
;
12009 OACC_DATA_CLAUSES (stmt
) = clauses
;
12010 OACC_DATA_BODY (stmt
) = block
;
12011 SET_EXPR_LOCATION (stmt
, loc
);
12013 return add_stmt (stmt
);
12016 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12017 statement. LOC is the location of the OACC_HOST_DATA. */
12020 c_finish_oacc_host_data (location_t loc
, tree clauses
, tree block
)
12024 block
= c_end_compound_stmt (loc
, block
, true);
12026 stmt
= make_node (OACC_HOST_DATA
);
12027 TREE_TYPE (stmt
) = void_type_node
;
12028 OACC_HOST_DATA_CLAUSES (stmt
) = clauses
;
12029 OACC_HOST_DATA_BODY (stmt
) = block
;
12030 SET_EXPR_LOCATION (stmt
, loc
);
12032 return add_stmt (stmt
);
12035 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12038 c_begin_omp_parallel (void)
12042 keep_next_level ();
12043 block
= c_begin_compound_stmt (true);
12048 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12049 statement. LOC is the location of the OMP_PARALLEL. */
12052 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
12056 block
= c_end_compound_stmt (loc
, block
, true);
12058 stmt
= make_node (OMP_PARALLEL
);
12059 TREE_TYPE (stmt
) = void_type_node
;
12060 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
12061 OMP_PARALLEL_BODY (stmt
) = block
;
12062 SET_EXPR_LOCATION (stmt
, loc
);
12064 return add_stmt (stmt
);
12067 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12070 c_begin_omp_task (void)
12074 keep_next_level ();
12075 block
= c_begin_compound_stmt (true);
12080 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12081 statement. LOC is the location of the #pragma. */
12084 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
12088 block
= c_end_compound_stmt (loc
, block
, true);
12090 stmt
= make_node (OMP_TASK
);
12091 TREE_TYPE (stmt
) = void_type_node
;
12092 OMP_TASK_CLAUSES (stmt
) = clauses
;
12093 OMP_TASK_BODY (stmt
) = block
;
12094 SET_EXPR_LOCATION (stmt
, loc
);
12096 return add_stmt (stmt
);
12099 /* Generate GOMP_cancel call for #pragma omp cancel. */
12102 c_finish_omp_cancel (location_t loc
, tree clauses
)
12104 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCEL
);
12106 if (omp_find_clause (clauses
, OMP_CLAUSE_PARALLEL
))
12108 else if (omp_find_clause (clauses
, OMP_CLAUSE_FOR
))
12110 else if (omp_find_clause (clauses
, OMP_CLAUSE_SECTIONS
))
12112 else if (omp_find_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
12116 error_at (loc
, "%<#pragma omp cancel%> must specify one of "
12117 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12121 tree ifc
= omp_find_clause (clauses
, OMP_CLAUSE_IF
);
12122 if (ifc
!= NULL_TREE
)
12124 tree type
= TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc
));
12125 ifc
= fold_build2_loc (OMP_CLAUSE_LOCATION (ifc
), NE_EXPR
,
12126 boolean_type_node
, OMP_CLAUSE_IF_EXPR (ifc
),
12127 build_zero_cst (type
));
12130 ifc
= boolean_true_node
;
12131 tree stmt
= build_call_expr_loc (loc
, fn
, 2,
12132 build_int_cst (integer_type_node
, mask
),
12137 /* Generate GOMP_cancellation_point call for
12138 #pragma omp cancellation point. */
12141 c_finish_omp_cancellation_point (location_t loc
, tree clauses
)
12143 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT
);
12145 if (omp_find_clause (clauses
, OMP_CLAUSE_PARALLEL
))
12147 else if (omp_find_clause (clauses
, OMP_CLAUSE_FOR
))
12149 else if (omp_find_clause (clauses
, OMP_CLAUSE_SECTIONS
))
12151 else if (omp_find_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
12155 error_at (loc
, "%<#pragma omp cancellation point%> must specify one of "
12156 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12160 tree stmt
= build_call_expr_loc (loc
, fn
, 1,
12161 build_int_cst (integer_type_node
, mask
));
12165 /* Helper function for handle_omp_array_sections. Called recursively
12166 to handle multiple array-section-subscripts. C is the clause,
12167 T current expression (initially OMP_CLAUSE_DECL), which is either
12168 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12169 expression if specified, TREE_VALUE length expression if specified,
12170 TREE_CHAIN is what it has been specified after, or some decl.
12171 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12172 set to true if any of the array-section-subscript could have length
12173 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12174 first array-section-subscript which is known not to have length
12176 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12177 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12178 all are or may have length of 1, array-section-subscript [:2] is the
12179 first one known not to have length 1. For array-section-subscript
12180 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12181 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12182 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12183 case though, as some lengths could be zero. */
12186 handle_omp_array_sections_1 (tree c
, tree t
, vec
<tree
> &types
,
12187 bool &maybe_zero_len
, unsigned int &first_non_one
,
12188 enum c_omp_region_type ort
)
12190 tree ret
, low_bound
, length
, type
;
12191 if (TREE_CODE (t
) != TREE_LIST
)
12193 if (error_operand_p (t
))
12194 return error_mark_node
;
12196 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12197 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t
))))
12199 error_at (OMP_CLAUSE_LOCATION (c
), "%<_Atomic%> %qE in %qs clause",
12200 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12201 return error_mark_node
;
12203 if (TREE_CODE (t
) == COMPONENT_REF
12204 && ort
== C_ORT_OMP
12205 && (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
12206 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO
12207 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FROM
))
12209 if (DECL_BIT_FIELD (TREE_OPERAND (t
, 1)))
12211 error_at (OMP_CLAUSE_LOCATION (c
),
12212 "bit-field %qE in %qs clause",
12213 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12214 return error_mark_node
;
12216 while (TREE_CODE (t
) == COMPONENT_REF
)
12218 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == UNION_TYPE
)
12220 error_at (OMP_CLAUSE_LOCATION (c
),
12221 "%qE is a member of a union", t
);
12222 return error_mark_node
;
12224 t
= TREE_OPERAND (t
, 0);
12227 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
12230 error_at (OMP_CLAUSE_LOCATION (c
),
12231 "%qD is not a variable in %qs clause", t
,
12232 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12234 error_at (OMP_CLAUSE_LOCATION (c
),
12235 "%qE is not a variable in %qs clause", t
,
12236 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12237 return error_mark_node
;
12239 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12240 && TYPE_ATOMIC (TREE_TYPE (t
)))
12242 error_at (OMP_CLAUSE_LOCATION (c
), "%<_Atomic%> %qD in %qs clause",
12243 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12244 return error_mark_node
;
12246 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12248 && DECL_THREAD_LOCAL_P (t
))
12250 error_at (OMP_CLAUSE_LOCATION (c
),
12251 "%qD is threadprivate variable in %qs clause", t
,
12252 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12253 return error_mark_node
;
12255 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
12256 && TYPE_ATOMIC (TREE_TYPE (t
))
12257 && POINTER_TYPE_P (TREE_TYPE (t
)))
12259 /* If the array section is pointer based and the pointer
12260 itself is _Atomic qualified, we need to atomically load
12263 memset (&expr
, 0, sizeof (expr
));
12265 expr
= convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c
),
12266 expr
, false, false);
12272 ret
= handle_omp_array_sections_1 (c
, TREE_CHAIN (t
), types
,
12273 maybe_zero_len
, first_non_one
, ort
);
12274 if (ret
== error_mark_node
|| ret
== NULL_TREE
)
12277 type
= TREE_TYPE (ret
);
12278 low_bound
= TREE_PURPOSE (t
);
12279 length
= TREE_VALUE (t
);
12281 if (low_bound
== error_mark_node
|| length
== error_mark_node
)
12282 return error_mark_node
;
12284 if (low_bound
&& !INTEGRAL_TYPE_P (TREE_TYPE (low_bound
)))
12286 error_at (OMP_CLAUSE_LOCATION (c
),
12287 "low bound %qE of array section does not have integral type",
12289 return error_mark_node
;
12291 if (length
&& !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
12293 error_at (OMP_CLAUSE_LOCATION (c
),
12294 "length %qE of array section does not have integral type",
12296 return error_mark_node
;
12299 && TREE_CODE (low_bound
) == INTEGER_CST
12300 && TYPE_PRECISION (TREE_TYPE (low_bound
))
12301 > TYPE_PRECISION (sizetype
))
12302 low_bound
= fold_convert (sizetype
, low_bound
);
12304 && TREE_CODE (length
) == INTEGER_CST
12305 && TYPE_PRECISION (TREE_TYPE (length
))
12306 > TYPE_PRECISION (sizetype
))
12307 length
= fold_convert (sizetype
, length
);
12308 if (low_bound
== NULL_TREE
)
12309 low_bound
= integer_zero_node
;
12311 if (length
!= NULL_TREE
)
12313 if (!integer_nonzerop (length
))
12315 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
12316 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
12318 if (integer_zerop (length
))
12320 error_at (OMP_CLAUSE_LOCATION (c
),
12321 "zero length array section in %qs clause",
12322 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12323 return error_mark_node
;
12327 maybe_zero_len
= true;
12329 if (first_non_one
== types
.length ()
12330 && (TREE_CODE (length
) != INTEGER_CST
|| integer_onep (length
)))
12333 if (TREE_CODE (type
) == ARRAY_TYPE
)
12335 if (length
== NULL_TREE
12336 && (TYPE_DOMAIN (type
) == NULL_TREE
12337 || TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
))
12339 error_at (OMP_CLAUSE_LOCATION (c
),
12340 "for unknown bound array type length expression must "
12342 return error_mark_node
;
12344 if (TREE_CODE (low_bound
) == INTEGER_CST
12345 && tree_int_cst_sgn (low_bound
) == -1)
12347 error_at (OMP_CLAUSE_LOCATION (c
),
12348 "negative low bound in array section in %qs clause",
12349 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12350 return error_mark_node
;
12352 if (length
!= NULL_TREE
12353 && TREE_CODE (length
) == INTEGER_CST
12354 && tree_int_cst_sgn (length
) == -1)
12356 error_at (OMP_CLAUSE_LOCATION (c
),
12357 "negative length in array section in %qs clause",
12358 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12359 return error_mark_node
;
12361 if (TYPE_DOMAIN (type
)
12362 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
12363 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
12367 = fold_convert (sizetype
, TYPE_MAX_VALUE (TYPE_DOMAIN (type
)));
12368 size
= size_binop (PLUS_EXPR
, size
, size_one_node
);
12369 if (TREE_CODE (low_bound
) == INTEGER_CST
)
12371 if (tree_int_cst_lt (size
, low_bound
))
12373 error_at (OMP_CLAUSE_LOCATION (c
),
12374 "low bound %qE above array section size "
12375 "in %qs clause", low_bound
,
12376 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12377 return error_mark_node
;
12379 if (tree_int_cst_equal (size
, low_bound
))
12381 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
12382 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
12384 error_at (OMP_CLAUSE_LOCATION (c
),
12385 "zero length array section in %qs clause",
12386 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12387 return error_mark_node
;
12389 maybe_zero_len
= true;
12391 else if (length
== NULL_TREE
12392 && first_non_one
== types
.length ()
12393 && tree_int_cst_equal
12394 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
12398 else if (length
== NULL_TREE
)
12400 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12401 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
)
12402 maybe_zero_len
= true;
12403 if (first_non_one
== types
.length ())
12406 if (length
&& TREE_CODE (length
) == INTEGER_CST
)
12408 if (tree_int_cst_lt (size
, length
))
12410 error_at (OMP_CLAUSE_LOCATION (c
),
12411 "length %qE above array section size "
12412 "in %qs clause", length
,
12413 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12414 return error_mark_node
;
12416 if (TREE_CODE (low_bound
) == INTEGER_CST
)
12419 = size_binop (PLUS_EXPR
,
12420 fold_convert (sizetype
, low_bound
),
12421 fold_convert (sizetype
, length
));
12422 if (TREE_CODE (lbpluslen
) == INTEGER_CST
12423 && tree_int_cst_lt (size
, lbpluslen
))
12425 error_at (OMP_CLAUSE_LOCATION (c
),
12426 "high bound %qE above array section size "
12427 "in %qs clause", lbpluslen
,
12428 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12429 return error_mark_node
;
12434 else if (length
== NULL_TREE
)
12436 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12437 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
)
12438 maybe_zero_len
= true;
12439 if (first_non_one
== types
.length ())
12443 /* For [lb:] we will need to evaluate lb more than once. */
12444 if (length
== NULL_TREE
&& OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
12446 tree lb
= save_expr (low_bound
);
12447 if (lb
!= low_bound
)
12449 TREE_PURPOSE (t
) = lb
;
12454 else if (TREE_CODE (type
) == POINTER_TYPE
)
12456 if (length
== NULL_TREE
)
12458 error_at (OMP_CLAUSE_LOCATION (c
),
12459 "for pointer type length expression must be specified");
12460 return error_mark_node
;
12462 if (length
!= NULL_TREE
12463 && TREE_CODE (length
) == INTEGER_CST
12464 && tree_int_cst_sgn (length
) == -1)
12466 error_at (OMP_CLAUSE_LOCATION (c
),
12467 "negative length in array section in %qs clause",
12468 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12469 return error_mark_node
;
12471 /* If there is a pointer type anywhere but in the very first
12472 array-section-subscript, the array section can't be contiguous. */
12473 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12474 && TREE_CODE (TREE_CHAIN (t
)) == TREE_LIST
)
12476 error_at (OMP_CLAUSE_LOCATION (c
),
12477 "array section is not contiguous in %qs clause",
12478 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12479 return error_mark_node
;
12484 error_at (OMP_CLAUSE_LOCATION (c
),
12485 "%qE does not have pointer or array type", ret
);
12486 return error_mark_node
;
12488 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
12489 types
.safe_push (TREE_TYPE (ret
));
12490 /* We will need to evaluate lb more than once. */
12491 tree lb
= save_expr (low_bound
);
12492 if (lb
!= low_bound
)
12494 TREE_PURPOSE (t
) = lb
;
12497 ret
= build_array_ref (OMP_CLAUSE_LOCATION (c
), ret
, low_bound
);
12501 /* Handle array sections for clause C. */
12504 handle_omp_array_sections (tree c
, enum c_omp_region_type ort
)
12506 bool maybe_zero_len
= false;
12507 unsigned int first_non_one
= 0;
12508 auto_vec
<tree
, 10> types
;
12509 tree first
= handle_omp_array_sections_1 (c
, OMP_CLAUSE_DECL (c
), types
,
12510 maybe_zero_len
, first_non_one
,
12512 if (first
== error_mark_node
)
12514 if (first
== NULL_TREE
)
12516 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
)
12518 tree t
= OMP_CLAUSE_DECL (c
);
12519 tree tem
= NULL_TREE
;
12520 /* Need to evaluate side effects in the length expressions
12522 while (TREE_CODE (t
) == TREE_LIST
)
12524 if (TREE_VALUE (t
) && TREE_SIDE_EFFECTS (TREE_VALUE (t
)))
12526 if (tem
== NULL_TREE
)
12527 tem
= TREE_VALUE (t
);
12529 tem
= build2 (COMPOUND_EXPR
, TREE_TYPE (tem
),
12530 TREE_VALUE (t
), tem
);
12532 t
= TREE_CHAIN (t
);
12535 first
= build2 (COMPOUND_EXPR
, TREE_TYPE (first
), tem
, first
);
12536 first
= c_fully_fold (first
, false, NULL
);
12537 OMP_CLAUSE_DECL (c
) = first
;
12541 unsigned int num
= types
.length (), i
;
12542 tree t
, side_effects
= NULL_TREE
, size
= NULL_TREE
;
12543 tree condition
= NULL_TREE
;
12545 if (int_size_in_bytes (TREE_TYPE (first
)) <= 0)
12546 maybe_zero_len
= true;
12548 for (i
= num
, t
= OMP_CLAUSE_DECL (c
); i
> 0;
12549 t
= TREE_CHAIN (t
))
12551 tree low_bound
= TREE_PURPOSE (t
);
12552 tree length
= TREE_VALUE (t
);
12556 && TREE_CODE (low_bound
) == INTEGER_CST
12557 && TYPE_PRECISION (TREE_TYPE (low_bound
))
12558 > TYPE_PRECISION (sizetype
))
12559 low_bound
= fold_convert (sizetype
, low_bound
);
12561 && TREE_CODE (length
) == INTEGER_CST
12562 && TYPE_PRECISION (TREE_TYPE (length
))
12563 > TYPE_PRECISION (sizetype
))
12564 length
= fold_convert (sizetype
, length
);
12565 if (low_bound
== NULL_TREE
)
12566 low_bound
= integer_zero_node
;
12567 if (!maybe_zero_len
&& i
> first_non_one
)
12569 if (integer_nonzerop (low_bound
))
12570 goto do_warn_noncontiguous
;
12571 if (length
!= NULL_TREE
12572 && TREE_CODE (length
) == INTEGER_CST
12573 && TYPE_DOMAIN (types
[i
])
12574 && TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
]))
12575 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])))
12579 size
= size_binop (PLUS_EXPR
,
12580 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
12582 if (!tree_int_cst_equal (length
, size
))
12584 do_warn_noncontiguous
:
12585 error_at (OMP_CLAUSE_LOCATION (c
),
12586 "array section is not contiguous in %qs "
12588 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12592 if (length
!= NULL_TREE
12593 && TREE_SIDE_EFFECTS (length
))
12595 if (side_effects
== NULL_TREE
)
12596 side_effects
= length
;
12598 side_effects
= build2 (COMPOUND_EXPR
,
12599 TREE_TYPE (side_effects
),
12600 length
, side_effects
);
12607 if (i
> first_non_one
12608 && ((length
&& integer_nonzerop (length
))
12609 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
))
12612 l
= fold_convert (sizetype
, length
);
12615 l
= size_binop (PLUS_EXPR
,
12616 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
12618 l
= size_binop (MINUS_EXPR
, l
,
12619 fold_convert (sizetype
, low_bound
));
12621 if (i
> first_non_one
)
12623 l
= fold_build2 (NE_EXPR
, boolean_type_node
, l
,
12625 if (condition
== NULL_TREE
)
12628 condition
= fold_build2 (BIT_AND_EXPR
, boolean_type_node
,
12631 else if (size
== NULL_TREE
)
12633 size
= size_in_bytes (TREE_TYPE (types
[i
]));
12634 tree eltype
= TREE_TYPE (types
[num
- 1]);
12635 while (TREE_CODE (eltype
) == ARRAY_TYPE
)
12636 eltype
= TREE_TYPE (eltype
);
12637 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
12639 if (integer_zerop (size
)
12640 || integer_zerop (size_in_bytes (eltype
)))
12642 error_at (OMP_CLAUSE_LOCATION (c
),
12643 "zero length array section in %qs clause",
12644 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12645 return error_mark_node
;
12647 size
= size_binop (EXACT_DIV_EXPR
, size
,
12648 size_in_bytes (eltype
));
12650 size
= size_binop (MULT_EXPR
, size
, l
);
12652 size
= fold_build3 (COND_EXPR
, sizetype
, condition
,
12653 size
, size_zero_node
);
12656 size
= size_binop (MULT_EXPR
, size
, l
);
12660 size
= build2 (COMPOUND_EXPR
, sizetype
, side_effects
, size
);
12661 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
12663 size
= size_binop (MINUS_EXPR
, size
, size_one_node
);
12664 size
= c_fully_fold (size
, false, NULL
);
12665 tree index_type
= build_index_type (size
);
12666 tree eltype
= TREE_TYPE (first
);
12667 while (TREE_CODE (eltype
) == ARRAY_TYPE
)
12668 eltype
= TREE_TYPE (eltype
);
12669 tree type
= build_array_type (eltype
, index_type
);
12670 tree ptype
= build_pointer_type (eltype
);
12671 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
12672 t
= build_fold_addr_expr (t
);
12673 tree t2
= build_fold_addr_expr (first
);
12674 t2
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
12675 ptrdiff_type_node
, t2
);
12676 t2
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
12677 ptrdiff_type_node
, t2
,
12678 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
12679 ptrdiff_type_node
, t
));
12680 t2
= c_fully_fold (t2
, false, NULL
);
12681 if (tree_fits_shwi_p (t2
))
12682 t
= build2 (MEM_REF
, type
, t
,
12683 build_int_cst (ptype
, tree_to_shwi (t2
)));
12686 t2
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), sizetype
, t2
);
12687 t
= build2_loc (OMP_CLAUSE_LOCATION (c
), POINTER_PLUS_EXPR
,
12688 TREE_TYPE (t
), t
, t2
);
12689 t
= build2 (MEM_REF
, type
, t
, build_int_cst (ptype
, 0));
12691 OMP_CLAUSE_DECL (c
) = t
;
12694 first
= c_fully_fold (first
, false, NULL
);
12695 OMP_CLAUSE_DECL (c
) = first
;
12697 size
= c_fully_fold (size
, false, NULL
);
12698 OMP_CLAUSE_SIZE (c
) = size
;
12699 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
12700 || (TREE_CODE (t
) == COMPONENT_REF
12701 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
))
12703 gcc_assert (OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_FORCE_DEVICEPTR
);
12704 if (ort
== C_ORT_OMP
|| ort
== C_ORT_ACC
)
12705 switch (OMP_CLAUSE_MAP_KIND (c
))
12707 case GOMP_MAP_ALLOC
:
12709 case GOMP_MAP_FROM
:
12710 case GOMP_MAP_TOFROM
:
12711 case GOMP_MAP_ALWAYS_TO
:
12712 case GOMP_MAP_ALWAYS_FROM
:
12713 case GOMP_MAP_ALWAYS_TOFROM
:
12714 case GOMP_MAP_RELEASE
:
12715 case GOMP_MAP_DELETE
:
12716 case GOMP_MAP_FORCE_TO
:
12717 case GOMP_MAP_FORCE_FROM
:
12718 case GOMP_MAP_FORCE_TOFROM
:
12719 case GOMP_MAP_FORCE_PRESENT
:
12720 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c
) = 1;
12725 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_MAP
);
12726 if (ort
!= C_ORT_OMP
&& ort
!= C_ORT_ACC
)
12727 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_POINTER
);
12728 else if (TREE_CODE (t
) == COMPONENT_REF
)
12729 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_ALWAYS_POINTER
);
12731 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_FIRSTPRIVATE_POINTER
);
12732 if (OMP_CLAUSE_MAP_KIND (c2
) != GOMP_MAP_FIRSTPRIVATE_POINTER
12733 && !c_mark_addressable (t
))
12735 OMP_CLAUSE_DECL (c2
) = t
;
12736 t
= build_fold_addr_expr (first
);
12737 t
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), ptrdiff_type_node
, t
);
12738 tree ptr
= OMP_CLAUSE_DECL (c2
);
12739 if (!POINTER_TYPE_P (TREE_TYPE (ptr
)))
12740 ptr
= build_fold_addr_expr (ptr
);
12741 t
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
12742 ptrdiff_type_node
, t
,
12743 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
12744 ptrdiff_type_node
, ptr
));
12745 t
= c_fully_fold (t
, false, NULL
);
12746 OMP_CLAUSE_SIZE (c2
) = t
;
12747 OMP_CLAUSE_CHAIN (c2
) = OMP_CLAUSE_CHAIN (c
);
12748 OMP_CLAUSE_CHAIN (c
) = c2
;
12753 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12754 an inline call. But, remap
12755 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12756 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12759 c_clone_omp_udr (tree stmt
, tree omp_decl1
, tree omp_decl2
,
12760 tree decl
, tree placeholder
)
12763 hash_map
<tree
, tree
> decl_map
;
12765 decl_map
.put (omp_decl1
, placeholder
);
12766 decl_map
.put (omp_decl2
, decl
);
12767 memset (&id
, 0, sizeof (id
));
12768 id
.src_fn
= DECL_CONTEXT (omp_decl1
);
12769 id
.dst_fn
= current_function_decl
;
12770 id
.src_cfun
= DECL_STRUCT_FUNCTION (id
.src_fn
);
12771 id
.decl_map
= &decl_map
;
12773 id
.copy_decl
= copy_decl_no_change
;
12774 id
.transform_call_graph_edges
= CB_CGE_DUPLICATE
;
12775 id
.transform_new_cfg
= true;
12776 id
.transform_return_to_modify
= false;
12777 id
.transform_lang_insert_block
= NULL
;
12779 walk_tree (&stmt
, copy_tree_body_r
, &id
, NULL
);
12783 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12784 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12787 c_find_omp_placeholder_r (tree
*tp
, int *, void *data
)
12789 if (*tp
== (tree
) data
)
12794 /* For all elements of CLAUSES, validate them against their constraints.
12795 Remove any elements from the list that are invalid. */
12798 c_finish_omp_clauses (tree clauses
, enum c_omp_region_type ort
)
12800 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
12801 bitmap_head aligned_head
, map_head
, map_field_head
, oacc_reduction_head
;
12802 tree c
, t
, type
, *pc
;
12803 tree simdlen
= NULL_TREE
, safelen
= NULL_TREE
;
12804 bool branch_seen
= false;
12805 bool copyprivate_seen
= false;
12806 bool linear_variable_step_check
= false;
12807 tree
*nowait_clause
= NULL
;
12808 bool ordered_seen
= false;
12809 tree schedule_clause
= NULL_TREE
;
12810 bool oacc_async
= false;
12812 bitmap_obstack_initialize (NULL
);
12813 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
12814 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
12815 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
12816 bitmap_initialize (&aligned_head
, &bitmap_default_obstack
);
12817 bitmap_initialize (&map_head
, &bitmap_default_obstack
);
12818 bitmap_initialize (&map_field_head
, &bitmap_default_obstack
);
12819 bitmap_initialize (&oacc_reduction_head
, &bitmap_default_obstack
);
12821 if (ort
& C_ORT_ACC
)
12822 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
12823 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_ASYNC
)
12829 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
12831 bool remove
= false;
12832 bool need_complete
= false;
12833 bool need_implicitly_determined
= false;
12835 switch (OMP_CLAUSE_CODE (c
))
12837 case OMP_CLAUSE_SHARED
:
12838 need_implicitly_determined
= true;
12839 goto check_dup_generic
;
12841 case OMP_CLAUSE_PRIVATE
:
12842 need_complete
= true;
12843 need_implicitly_determined
= true;
12844 goto check_dup_generic
;
12846 case OMP_CLAUSE_REDUCTION
:
12847 need_implicitly_determined
= true;
12848 t
= OMP_CLAUSE_DECL (c
);
12849 if (TREE_CODE (t
) == TREE_LIST
)
12851 if (handle_omp_array_sections (c
, ort
))
12857 t
= OMP_CLAUSE_DECL (c
);
12859 t
= require_complete_type (OMP_CLAUSE_LOCATION (c
), t
);
12860 if (t
== error_mark_node
)
12866 c_mark_addressable (t
);
12867 type
= TREE_TYPE (t
);
12868 if (TREE_CODE (t
) == MEM_REF
)
12869 type
= TREE_TYPE (type
);
12870 if (TREE_CODE (type
) == ARRAY_TYPE
)
12872 tree oatype
= type
;
12873 gcc_assert (TREE_CODE (t
) != MEM_REF
);
12874 while (TREE_CODE (type
) == ARRAY_TYPE
)
12875 type
= TREE_TYPE (type
);
12876 if (integer_zerop (TYPE_SIZE_UNIT (type
)))
12878 error_at (OMP_CLAUSE_LOCATION (c
),
12879 "%qD in %<reduction%> clause is a zero size array",
12884 tree size
= size_binop (EXACT_DIV_EXPR
, TYPE_SIZE_UNIT (oatype
),
12885 TYPE_SIZE_UNIT (type
));
12886 if (integer_zerop (size
))
12888 error_at (OMP_CLAUSE_LOCATION (c
),
12889 "%qD in %<reduction%> clause is a zero size array",
12894 size
= size_binop (MINUS_EXPR
, size
, size_one_node
);
12895 tree index_type
= build_index_type (size
);
12896 tree atype
= build_array_type (type
, index_type
);
12897 tree ptype
= build_pointer_type (type
);
12898 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
12899 t
= build_fold_addr_expr (t
);
12900 t
= build2 (MEM_REF
, atype
, t
, build_int_cst (ptype
, 0));
12901 OMP_CLAUSE_DECL (c
) = t
;
12903 if (TYPE_ATOMIC (type
))
12905 error_at (OMP_CLAUSE_LOCATION (c
),
12906 "%<_Atomic%> %qE in %<reduction%> clause", t
);
12910 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == NULL_TREE
12911 && (FLOAT_TYPE_P (type
)
12912 || TREE_CODE (type
) == COMPLEX_TYPE
))
12914 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
12915 const char *r_name
= NULL
;
12924 if (TREE_CODE (type
) == COMPLEX_TYPE
)
12928 if (TREE_CODE (type
) == COMPLEX_TYPE
)
12940 case TRUTH_ANDIF_EXPR
:
12941 if (FLOAT_TYPE_P (type
))
12944 case TRUTH_ORIF_EXPR
:
12945 if (FLOAT_TYPE_P (type
))
12949 gcc_unreachable ();
12953 error_at (OMP_CLAUSE_LOCATION (c
),
12954 "%qE has invalid type for %<reduction(%s)%>",
12960 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == error_mark_node
)
12962 error_at (OMP_CLAUSE_LOCATION (c
),
12963 "user defined reduction not found for %qE", t
);
12967 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
12969 tree list
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
);
12970 type
= TYPE_MAIN_VARIANT (type
);
12971 tree placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
12972 VAR_DECL
, NULL_TREE
, type
);
12973 tree decl_placeholder
= NULL_TREE
;
12974 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = placeholder
;
12975 DECL_ARTIFICIAL (placeholder
) = 1;
12976 DECL_IGNORED_P (placeholder
) = 1;
12977 if (TREE_CODE (t
) == MEM_REF
)
12979 decl_placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
12980 VAR_DECL
, NULL_TREE
, type
);
12981 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c
) = decl_placeholder
;
12982 DECL_ARTIFICIAL (decl_placeholder
) = 1;
12983 DECL_IGNORED_P (decl_placeholder
) = 1;
12985 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 0)))
12986 c_mark_addressable (placeholder
);
12987 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 1)))
12988 c_mark_addressable (decl_placeholder
? decl_placeholder
12989 : OMP_CLAUSE_DECL (c
));
12990 OMP_CLAUSE_REDUCTION_MERGE (c
)
12991 = c_clone_omp_udr (TREE_VEC_ELT (list
, 2),
12992 TREE_VEC_ELT (list
, 0),
12993 TREE_VEC_ELT (list
, 1),
12994 decl_placeholder
? decl_placeholder
12995 : OMP_CLAUSE_DECL (c
), placeholder
);
12996 OMP_CLAUSE_REDUCTION_MERGE (c
)
12997 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
12998 void_type_node
, NULL_TREE
,
12999 OMP_CLAUSE_REDUCTION_MERGE (c
), NULL_TREE
);
13000 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c
)) = 1;
13001 if (TREE_VEC_LENGTH (list
) == 6)
13003 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 3)))
13004 c_mark_addressable (decl_placeholder
? decl_placeholder
13005 : OMP_CLAUSE_DECL (c
));
13006 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 4)))
13007 c_mark_addressable (placeholder
);
13008 tree init
= TREE_VEC_ELT (list
, 5);
13009 if (init
== error_mark_node
)
13010 init
= DECL_INITIAL (TREE_VEC_ELT (list
, 3));
13011 OMP_CLAUSE_REDUCTION_INIT (c
)
13012 = c_clone_omp_udr (init
, TREE_VEC_ELT (list
, 4),
13013 TREE_VEC_ELT (list
, 3),
13014 decl_placeholder
? decl_placeholder
13015 : OMP_CLAUSE_DECL (c
), placeholder
);
13016 if (TREE_VEC_ELT (list
, 5) == error_mark_node
)
13018 tree v
= decl_placeholder
? decl_placeholder
: t
;
13019 OMP_CLAUSE_REDUCTION_INIT (c
)
13020 = build2 (INIT_EXPR
, TREE_TYPE (v
), v
,
13021 OMP_CLAUSE_REDUCTION_INIT (c
));
13023 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
13024 c_find_omp_placeholder_r
,
13025 placeholder
, NULL
))
13026 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c
) = 1;
13031 tree v
= decl_placeholder
? decl_placeholder
: t
;
13032 if (AGGREGATE_TYPE_P (TREE_TYPE (v
)))
13033 init
= build_constructor (TREE_TYPE (v
), NULL
);
13035 init
= fold_convert (TREE_TYPE (v
), integer_zero_node
);
13036 OMP_CLAUSE_REDUCTION_INIT (c
)
13037 = build2 (INIT_EXPR
, TREE_TYPE (v
), v
, init
);
13039 OMP_CLAUSE_REDUCTION_INIT (c
)
13040 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
13041 void_type_node
, NULL_TREE
,
13042 OMP_CLAUSE_REDUCTION_INIT (c
), NULL_TREE
);
13043 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c
)) = 1;
13045 if (TREE_CODE (t
) == MEM_REF
)
13047 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))) == NULL_TREE
13048 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))))
13051 sorry ("variable length element type in array "
13052 "%<reduction%> clause");
13056 t
= TREE_OPERAND (t
, 0);
13057 if (TREE_CODE (t
) == POINTER_PLUS_EXPR
)
13058 t
= TREE_OPERAND (t
, 0);
13059 if (TREE_CODE (t
) == ADDR_EXPR
)
13060 t
= TREE_OPERAND (t
, 0);
13062 goto check_dup_generic_t
;
13064 case OMP_CLAUSE_COPYPRIVATE
:
13065 copyprivate_seen
= true;
13068 error_at (OMP_CLAUSE_LOCATION (*nowait_clause
),
13069 "%<nowait%> clause must not be used together "
13070 "with %<copyprivate%>");
13071 *nowait_clause
= OMP_CLAUSE_CHAIN (*nowait_clause
);
13072 nowait_clause
= NULL
;
13074 goto check_dup_generic
;
13076 case OMP_CLAUSE_COPYIN
:
13077 t
= OMP_CLAUSE_DECL (c
);
13078 if (!VAR_P (t
) || !DECL_THREAD_LOCAL_P (t
))
13080 error_at (OMP_CLAUSE_LOCATION (c
),
13081 "%qE must be %<threadprivate%> for %<copyin%>", t
);
13085 goto check_dup_generic
;
13087 case OMP_CLAUSE_LINEAR
:
13088 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
13089 need_implicitly_determined
= true;
13090 t
= OMP_CLAUSE_DECL (c
);
13091 if (ort
!= C_ORT_OMP_DECLARE_SIMD
13092 && OMP_CLAUSE_LINEAR_KIND (c
) != OMP_CLAUSE_LINEAR_DEFAULT
)
13094 error_at (OMP_CLAUSE_LOCATION (c
),
13095 "modifier should not be specified in %<linear%> "
13096 "clause on %<simd%> or %<for%> constructs");
13097 OMP_CLAUSE_LINEAR_KIND (c
) = OMP_CLAUSE_LINEAR_DEFAULT
;
13099 if (ort
& C_ORT_CILK
)
13101 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
13102 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t
))
13103 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
13105 error_at (OMP_CLAUSE_LOCATION (c
),
13106 "linear clause applied to non-integral, "
13107 "non-floating, non-pointer variable with type %qT",
13115 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
13116 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
13118 error_at (OMP_CLAUSE_LOCATION (c
),
13119 "linear clause applied to non-integral non-pointer "
13120 "variable with type %qT", TREE_TYPE (t
));
13124 if (TYPE_ATOMIC (TREE_TYPE (t
)))
13126 error_at (OMP_CLAUSE_LOCATION (c
),
13127 "%<_Atomic%> %qD in %<linear%> clause", t
);
13132 if (ort
== C_ORT_OMP_DECLARE_SIMD
)
13134 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
13135 if (TREE_CODE (s
) == PARM_DECL
)
13137 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c
) = 1;
13138 /* map_head bitmap is used as uniform_head if
13140 if (!bitmap_bit_p (&map_head
, DECL_UID (s
)))
13141 linear_variable_step_check
= true;
13142 goto check_dup_generic
;
13144 if (TREE_CODE (s
) != INTEGER_CST
)
13146 error_at (OMP_CLAUSE_LOCATION (c
),
13147 "%<linear%> clause step %qE is neither constant "
13148 "nor a parameter", s
);
13153 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c
))) == POINTER_TYPE
)
13155 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
13156 s
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
), PLUS_EXPR
,
13157 OMP_CLAUSE_DECL (c
), s
);
13158 s
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
13159 sizetype
, fold_convert (sizetype
, s
),
13161 (sizetype
, OMP_CLAUSE_DECL (c
)));
13162 if (s
== error_mark_node
)
13164 OMP_CLAUSE_LINEAR_STEP (c
) = s
;
13167 OMP_CLAUSE_LINEAR_STEP (c
)
13168 = fold_convert (TREE_TYPE (t
), OMP_CLAUSE_LINEAR_STEP (c
));
13169 goto check_dup_generic
;
13172 t
= OMP_CLAUSE_DECL (c
);
13173 check_dup_generic_t
:
13174 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13176 error_at (OMP_CLAUSE_LOCATION (c
),
13177 "%qE is not a variable in clause %qs", t
,
13178 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13181 else if (ort
== C_ORT_ACC
13182 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
13184 if (bitmap_bit_p (&oacc_reduction_head
, DECL_UID (t
)))
13186 error ("%qD appears more than once in reduction clauses", t
);
13190 bitmap_set_bit (&oacc_reduction_head
, DECL_UID (t
));
13192 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13193 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
13194 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
13196 error_at (OMP_CLAUSE_LOCATION (c
),
13197 "%qE appears more than once in data clauses", t
);
13200 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
13201 && bitmap_bit_p (&map_head
, DECL_UID (t
)))
13203 if (ort
== C_ORT_ACC
)
13204 error ("%qD appears more than once in data clauses", t
);
13206 error ("%qD appears both in data and map clauses", t
);
13210 bitmap_set_bit (&generic_head
, DECL_UID (t
));
13213 case OMP_CLAUSE_FIRSTPRIVATE
:
13214 t
= OMP_CLAUSE_DECL (c
);
13215 need_complete
= true;
13216 need_implicitly_determined
= true;
13217 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13219 error_at (OMP_CLAUSE_LOCATION (c
),
13220 "%qE is not a variable in clause %<firstprivate%>", t
);
13223 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13224 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
13226 error_at (OMP_CLAUSE_LOCATION (c
),
13227 "%qE appears more than once in data clauses", t
);
13230 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
13232 if (ort
== C_ORT_ACC
)
13233 error ("%qD appears more than once in data clauses", t
);
13235 error ("%qD appears both in data and map clauses", t
);
13239 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
13242 case OMP_CLAUSE_LASTPRIVATE
:
13243 t
= OMP_CLAUSE_DECL (c
);
13244 need_complete
= true;
13245 need_implicitly_determined
= true;
13246 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13248 error_at (OMP_CLAUSE_LOCATION (c
),
13249 "%qE is not a variable in clause %<lastprivate%>", t
);
13252 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13253 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
13255 error_at (OMP_CLAUSE_LOCATION (c
),
13256 "%qE appears more than once in data clauses", t
);
13260 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
13263 case OMP_CLAUSE_ALIGNED
:
13264 t
= OMP_CLAUSE_DECL (c
);
13265 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13267 error_at (OMP_CLAUSE_LOCATION (c
),
13268 "%qE is not a variable in %<aligned%> clause", t
);
13271 else if (!POINTER_TYPE_P (TREE_TYPE (t
))
13272 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
13274 error_at (OMP_CLAUSE_LOCATION (c
),
13275 "%qE in %<aligned%> clause is neither a pointer nor "
13279 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
13281 error_at (OMP_CLAUSE_LOCATION (c
),
13282 "%<_Atomic%> %qD in %<aligned%> clause", t
);
13286 else if (bitmap_bit_p (&aligned_head
, DECL_UID (t
)))
13288 error_at (OMP_CLAUSE_LOCATION (c
),
13289 "%qE appears more than once in %<aligned%> clauses",
13294 bitmap_set_bit (&aligned_head
, DECL_UID (t
));
13297 case OMP_CLAUSE_DEPEND
:
13298 t
= OMP_CLAUSE_DECL (c
);
13299 if (t
== NULL_TREE
)
13301 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c
)
13302 == OMP_CLAUSE_DEPEND_SOURCE
);
13305 if (OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SINK
)
13307 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13308 for (; t
; t
= TREE_CHAIN (t
))
13310 tree decl
= TREE_VALUE (t
);
13311 if (TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
13313 tree offset
= TREE_PURPOSE (t
);
13314 bool neg
= wi::neg_p ((wide_int
) offset
);
13315 offset
= fold_unary (ABS_EXPR
, TREE_TYPE (offset
), offset
);
13316 tree t2
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
),
13317 neg
? MINUS_EXPR
: PLUS_EXPR
,
13319 t2
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
13321 fold_convert (sizetype
, t2
),
13322 fold_convert (sizetype
, decl
));
13323 if (t2
== error_mark_node
)
13328 TREE_PURPOSE (t
) = t2
;
13333 if (TREE_CODE (t
) == TREE_LIST
)
13335 if (handle_omp_array_sections (c
, ort
))
13339 if (t
== error_mark_node
)
13341 else if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13343 error_at (OMP_CLAUSE_LOCATION (c
),
13344 "%qE is not a variable in %<depend%> clause", t
);
13347 else if (!c_mark_addressable (t
))
13351 case OMP_CLAUSE_MAP
:
13352 case OMP_CLAUSE_TO
:
13353 case OMP_CLAUSE_FROM
:
13354 case OMP_CLAUSE__CACHE_
:
13355 t
= OMP_CLAUSE_DECL (c
);
13356 if (TREE_CODE (t
) == TREE_LIST
)
13358 if (handle_omp_array_sections (c
, ort
))
13362 t
= OMP_CLAUSE_DECL (c
);
13363 if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
13365 error_at (OMP_CLAUSE_LOCATION (c
),
13366 "array section does not have mappable type "
13368 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13371 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
13373 error_at (OMP_CLAUSE_LOCATION (c
),
13374 "%<_Atomic%> %qE in %qs clause", t
,
13375 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13378 while (TREE_CODE (t
) == ARRAY_REF
)
13379 t
= TREE_OPERAND (t
, 0);
13380 if (TREE_CODE (t
) == COMPONENT_REF
13381 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
13383 while (TREE_CODE (t
) == COMPONENT_REF
)
13384 t
= TREE_OPERAND (t
, 0);
13385 if (bitmap_bit_p (&map_field_head
, DECL_UID (t
)))
13387 if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
13389 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
13390 error ("%qD appears more than once in motion"
13392 else if (ort
== C_ORT_ACC
)
13393 error ("%qD appears more than once in data"
13396 error ("%qD appears more than once in map"
13402 bitmap_set_bit (&map_head
, DECL_UID (t
));
13403 bitmap_set_bit (&map_field_head
, DECL_UID (t
));
13409 if (t
== error_mark_node
)
13414 if (TREE_CODE (t
) == COMPONENT_REF
13415 && (ort
& C_ORT_OMP
)
13416 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE__CACHE_
)
13418 if (DECL_BIT_FIELD (TREE_OPERAND (t
, 1)))
13420 error_at (OMP_CLAUSE_LOCATION (c
),
13421 "bit-field %qE in %qs clause",
13422 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13425 else if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
13427 error_at (OMP_CLAUSE_LOCATION (c
),
13428 "%qE does not have a mappable type in %qs clause",
13429 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13432 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
13434 error_at (OMP_CLAUSE_LOCATION (c
),
13435 "%<_Atomic%> %qE in %qs clause", t
,
13436 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13439 while (TREE_CODE (t
) == COMPONENT_REF
)
13441 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0)))
13444 error_at (OMP_CLAUSE_LOCATION (c
),
13445 "%qE is a member of a union", t
);
13449 t
= TREE_OPERAND (t
, 0);
13453 if (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
13455 if (bitmap_bit_p (&map_field_head
, DECL_UID (t
)))
13459 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13461 error_at (OMP_CLAUSE_LOCATION (c
),
13462 "%qE is not a variable in %qs clause", t
,
13463 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13466 else if (VAR_P (t
) && DECL_THREAD_LOCAL_P (t
))
13468 error_at (OMP_CLAUSE_LOCATION (c
),
13469 "%qD is threadprivate variable in %qs clause", t
,
13470 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13473 else if ((OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
13474 || (OMP_CLAUSE_MAP_KIND (c
)
13475 != GOMP_MAP_FIRSTPRIVATE_POINTER
))
13476 && !c_mark_addressable (t
))
13478 else if (!(OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
13479 && (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_POINTER
13480 || (OMP_CLAUSE_MAP_KIND (c
)
13481 == GOMP_MAP_FIRSTPRIVATE_POINTER
)
13482 || (OMP_CLAUSE_MAP_KIND (c
)
13483 == GOMP_MAP_FORCE_DEVICEPTR
)))
13484 && t
== OMP_CLAUSE_DECL (c
)
13485 && !lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
13487 error_at (OMP_CLAUSE_LOCATION (c
),
13488 "%qD does not have a mappable type in %qs clause", t
,
13489 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13492 else if (TREE_TYPE (t
) == error_mark_node
)
13494 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t
))))
13496 error_at (OMP_CLAUSE_LOCATION (c
),
13497 "%<_Atomic%> %qE in %qs clause", t
,
13498 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13501 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
13502 && OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_FIRSTPRIVATE_POINTER
)
13504 if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13505 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
13507 error ("%qD appears more than once in data clauses", t
);
13510 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
13512 if (ort
== C_ORT_ACC
)
13513 error ("%qD appears more than once in data clauses", t
);
13515 error ("%qD appears both in data and map clauses", t
);
13519 bitmap_set_bit (&generic_head
, DECL_UID (t
));
13521 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
13523 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
13524 error ("%qD appears more than once in motion clauses", t
);
13525 else if (ort
== C_ORT_ACC
)
13526 error ("%qD appears more than once in data clauses", t
);
13528 error ("%qD appears more than once in map clauses", t
);
13531 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13532 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
13534 if (ort
== C_ORT_ACC
)
13535 error ("%qD appears more than once in data clauses", t
);
13537 error ("%qD appears both in data and map clauses", t
);
13542 bitmap_set_bit (&map_head
, DECL_UID (t
));
13543 if (t
!= OMP_CLAUSE_DECL (c
)
13544 && TREE_CODE (OMP_CLAUSE_DECL (c
)) == COMPONENT_REF
)
13545 bitmap_set_bit (&map_field_head
, DECL_UID (t
));
13549 case OMP_CLAUSE_TO_DECLARE
:
13550 case OMP_CLAUSE_LINK
:
13551 t
= OMP_CLAUSE_DECL (c
);
13552 if (TREE_CODE (t
) == FUNCTION_DECL
13553 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO_DECLARE
)
13555 else if (!VAR_P (t
))
13557 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO_DECLARE
)
13558 error_at (OMP_CLAUSE_LOCATION (c
),
13559 "%qE is neither a variable nor a function name in "
13561 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13563 error_at (OMP_CLAUSE_LOCATION (c
),
13564 "%qE is not a variable in clause %qs", t
,
13565 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13568 else if (DECL_THREAD_LOCAL_P (t
))
13570 error_at (OMP_CLAUSE_LOCATION (c
),
13571 "%qD is threadprivate variable in %qs clause", t
,
13572 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13575 else if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
13577 error_at (OMP_CLAUSE_LOCATION (c
),
13578 "%qD does not have a mappable type in %qs clause", t
,
13579 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13584 if (bitmap_bit_p (&generic_head
, DECL_UID (t
)))
13586 error_at (OMP_CLAUSE_LOCATION (c
),
13587 "%qE appears more than once on the same "
13588 "%<declare target%> directive", t
);
13592 bitmap_set_bit (&generic_head
, DECL_UID (t
));
13595 case OMP_CLAUSE_UNIFORM
:
13596 t
= OMP_CLAUSE_DECL (c
);
13597 if (TREE_CODE (t
) != PARM_DECL
)
13600 error_at (OMP_CLAUSE_LOCATION (c
),
13601 "%qD is not an argument in %<uniform%> clause", t
);
13603 error_at (OMP_CLAUSE_LOCATION (c
),
13604 "%qE is not an argument in %<uniform%> clause", t
);
13608 /* map_head bitmap is used as uniform_head if declare_simd. */
13609 bitmap_set_bit (&map_head
, DECL_UID (t
));
13610 goto check_dup_generic
;
13612 case OMP_CLAUSE_IS_DEVICE_PTR
:
13613 case OMP_CLAUSE_USE_DEVICE_PTR
:
13614 t
= OMP_CLAUSE_DECL (c
);
13615 if (TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
13616 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
13618 error_at (OMP_CLAUSE_LOCATION (c
),
13619 "%qs variable is neither a pointer nor an array",
13620 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13623 goto check_dup_generic
;
13625 case OMP_CLAUSE_NOWAIT
:
13626 if (copyprivate_seen
)
13628 error_at (OMP_CLAUSE_LOCATION (c
),
13629 "%<nowait%> clause must not be used together "
13630 "with %<copyprivate%>");
13634 nowait_clause
= pc
;
13635 pc
= &OMP_CLAUSE_CHAIN (c
);
13638 case OMP_CLAUSE_IF
:
13639 case OMP_CLAUSE_NUM_THREADS
:
13640 case OMP_CLAUSE_NUM_TEAMS
:
13641 case OMP_CLAUSE_THREAD_LIMIT
:
13642 case OMP_CLAUSE_DEFAULT
:
13643 case OMP_CLAUSE_UNTIED
:
13644 case OMP_CLAUSE_COLLAPSE
:
13645 case OMP_CLAUSE_FINAL
:
13646 case OMP_CLAUSE_MERGEABLE
:
13647 case OMP_CLAUSE_DEVICE
:
13648 case OMP_CLAUSE_DIST_SCHEDULE
:
13649 case OMP_CLAUSE_PARALLEL
:
13650 case OMP_CLAUSE_FOR
:
13651 case OMP_CLAUSE_SECTIONS
:
13652 case OMP_CLAUSE_TASKGROUP
:
13653 case OMP_CLAUSE_PROC_BIND
:
13654 case OMP_CLAUSE_PRIORITY
:
13655 case OMP_CLAUSE_GRAINSIZE
:
13656 case OMP_CLAUSE_NUM_TASKS
:
13657 case OMP_CLAUSE_NOGROUP
:
13658 case OMP_CLAUSE_THREADS
:
13659 case OMP_CLAUSE_SIMD
:
13660 case OMP_CLAUSE_HINT
:
13661 case OMP_CLAUSE_DEFAULTMAP
:
13662 case OMP_CLAUSE__CILK_FOR_COUNT_
:
13663 case OMP_CLAUSE_NUM_GANGS
:
13664 case OMP_CLAUSE_NUM_WORKERS
:
13665 case OMP_CLAUSE_VECTOR_LENGTH
:
13666 case OMP_CLAUSE_ASYNC
:
13667 case OMP_CLAUSE_WAIT
:
13668 case OMP_CLAUSE_AUTO
:
13669 case OMP_CLAUSE_INDEPENDENT
:
13670 case OMP_CLAUSE_SEQ
:
13671 case OMP_CLAUSE_GANG
:
13672 case OMP_CLAUSE_WORKER
:
13673 case OMP_CLAUSE_VECTOR
:
13674 case OMP_CLAUSE_TILE
:
13675 pc
= &OMP_CLAUSE_CHAIN (c
);
13678 case OMP_CLAUSE_SCHEDULE
:
13679 if (OMP_CLAUSE_SCHEDULE_KIND (c
) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC
)
13681 const char *p
= NULL
;
13682 switch (OMP_CLAUSE_SCHEDULE_KIND (c
) & OMP_CLAUSE_SCHEDULE_MASK
)
13684 case OMP_CLAUSE_SCHEDULE_STATIC
: p
= "static"; break;
13685 case OMP_CLAUSE_SCHEDULE_DYNAMIC
: break;
13686 case OMP_CLAUSE_SCHEDULE_GUIDED
: break;
13687 case OMP_CLAUSE_SCHEDULE_AUTO
: p
= "auto"; break;
13688 case OMP_CLAUSE_SCHEDULE_RUNTIME
: p
= "runtime"; break;
13689 default: gcc_unreachable ();
13693 error_at (OMP_CLAUSE_LOCATION (c
),
13694 "%<nonmonotonic%> modifier specified for %qs "
13695 "schedule kind", p
);
13696 OMP_CLAUSE_SCHEDULE_KIND (c
)
13697 = (enum omp_clause_schedule_kind
)
13698 (OMP_CLAUSE_SCHEDULE_KIND (c
)
13699 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC
);
13702 schedule_clause
= c
;
13703 pc
= &OMP_CLAUSE_CHAIN (c
);
13706 case OMP_CLAUSE_ORDERED
:
13707 ordered_seen
= true;
13708 pc
= &OMP_CLAUSE_CHAIN (c
);
13711 case OMP_CLAUSE_SAFELEN
:
13713 pc
= &OMP_CLAUSE_CHAIN (c
);
13715 case OMP_CLAUSE_SIMDLEN
:
13717 pc
= &OMP_CLAUSE_CHAIN (c
);
13720 case OMP_CLAUSE_INBRANCH
:
13721 case OMP_CLAUSE_NOTINBRANCH
:
13724 error_at (OMP_CLAUSE_LOCATION (c
),
13725 "%<inbranch%> clause is incompatible with "
13726 "%<notinbranch%>");
13730 branch_seen
= true;
13731 pc
= &OMP_CLAUSE_CHAIN (c
);
13735 gcc_unreachable ();
13740 t
= OMP_CLAUSE_DECL (c
);
13744 t
= require_complete_type (OMP_CLAUSE_LOCATION (c
), t
);
13745 if (t
== error_mark_node
)
13749 if (need_implicitly_determined
)
13751 const char *share_name
= NULL
;
13753 if (VAR_P (t
) && DECL_THREAD_LOCAL_P (t
))
13754 share_name
= "threadprivate";
13755 else switch (c_omp_predetermined_sharing (t
))
13757 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
13759 case OMP_CLAUSE_DEFAULT_SHARED
:
13760 /* const vars may be specified in firstprivate clause. */
13761 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
13762 && TREE_READONLY (t
))
13764 share_name
= "shared";
13766 case OMP_CLAUSE_DEFAULT_PRIVATE
:
13767 share_name
= "private";
13770 gcc_unreachable ();
13774 error_at (OMP_CLAUSE_LOCATION (c
),
13775 "%qE is predetermined %qs for %qs",
13777 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13784 *pc
= OMP_CLAUSE_CHAIN (c
);
13786 pc
= &OMP_CLAUSE_CHAIN (c
);
13791 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen
),
13792 OMP_CLAUSE_SIMDLEN_EXPR (simdlen
)))
13794 error_at (OMP_CLAUSE_LOCATION (simdlen
),
13795 "%<simdlen%> clause value is bigger than "
13796 "%<safelen%> clause value");
13797 OMP_CLAUSE_SIMDLEN_EXPR (simdlen
)
13798 = OMP_CLAUSE_SAFELEN_EXPR (safelen
);
13803 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
13804 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
13806 error_at (OMP_CLAUSE_LOCATION (schedule_clause
),
13807 "%<nonmonotonic%> schedule modifier specified together "
13808 "with %<ordered%> clause");
13809 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
13810 = (enum omp_clause_schedule_kind
)
13811 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
13812 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC
);
13815 if (linear_variable_step_check
)
13816 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
13818 bool remove
= false;
13819 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
13820 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c
)
13821 && !bitmap_bit_p (&map_head
,
13822 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c
))))
13824 error_at (OMP_CLAUSE_LOCATION (c
),
13825 "%<linear%> clause step is a parameter %qD not "
13826 "specified in %<uniform%> clause",
13827 OMP_CLAUSE_LINEAR_STEP (c
));
13832 *pc
= OMP_CLAUSE_CHAIN (c
);
13834 pc
= &OMP_CLAUSE_CHAIN (c
);
13837 bitmap_obstack_release (NULL
);
13841 /* Return code to initialize DST with a copy constructor from SRC.
13842 C doesn't have copy constructors nor assignment operators, only for
13843 _Atomic vars we need to perform __atomic_load from src into a temporary
13844 followed by __atomic_store of the temporary to dst. */
13847 c_omp_clause_copy_ctor (tree clause
, tree dst
, tree src
)
13849 if (!really_atomic_lvalue (dst
) && !really_atomic_lvalue (src
))
13850 return build2 (MODIFY_EXPR
, TREE_TYPE (dst
), dst
, src
);
13852 location_t loc
= OMP_CLAUSE_LOCATION (clause
);
13853 tree type
= TREE_TYPE (dst
);
13854 tree nonatomic_type
= build_qualified_type (type
, TYPE_UNQUALIFIED
);
13855 tree tmp
= create_tmp_var (nonatomic_type
);
13856 tree tmp_addr
= build_fold_addr_expr (tmp
);
13857 TREE_ADDRESSABLE (tmp
) = 1;
13858 TREE_NO_WARNING (tmp
) = 1;
13859 tree src_addr
= build_fold_addr_expr (src
);
13860 tree dst_addr
= build_fold_addr_expr (dst
);
13861 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
13862 vec
<tree
, va_gc
> *params
;
13863 /* Expansion of a generic atomic load may require an addition
13864 element, so allocate enough to prevent a resize. */
13865 vec_alloc (params
, 4);
13867 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13868 tree fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
13869 params
->quick_push (src_addr
);
13870 params
->quick_push (tmp_addr
);
13871 params
->quick_push (seq_cst
);
13872 tree load
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
13874 vec_alloc (params
, 4);
13876 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
13877 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
13878 params
->quick_push (dst_addr
);
13879 params
->quick_push (tmp_addr
);
13880 params
->quick_push (seq_cst
);
13881 tree store
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
13882 return build2 (COMPOUND_EXPR
, void_type_node
, load
, store
);
13885 /* Create a transaction node. */
13888 c_finish_transaction (location_t loc
, tree block
, int flags
)
13890 tree stmt
= build_stmt (loc
, TRANSACTION_EXPR
, block
);
13891 if (flags
& TM_STMT_ATTR_OUTER
)
13892 TRANSACTION_EXPR_OUTER (stmt
) = 1;
13893 if (flags
& TM_STMT_ATTR_RELAXED
)
13894 TRANSACTION_EXPR_RELAXED (stmt
) = 1;
13895 return add_stmt (stmt
);
13898 /* Make a variant type in the proper way for C/C++, propagating qualifiers
13899 down to the element type of an array. If ORIG_QUAL_TYPE is not
13900 NULL, then it should be used as the qualified type
13901 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13902 preserve information about the typedef name from which an array
13903 type was derived). */
13906 c_build_qualified_type (tree type
, int type_quals
, tree orig_qual_type
,
13907 size_t orig_qual_indirect
)
13909 if (type
== error_mark_node
)
13912 if (TREE_CODE (type
) == ARRAY_TYPE
)
13915 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
13916 type_quals
, orig_qual_type
,
13917 orig_qual_indirect
- 1);
13919 /* See if we already have an identically qualified type. */
13920 if (orig_qual_type
&& orig_qual_indirect
== 0)
13921 t
= orig_qual_type
;
13923 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
13925 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
13926 && TYPE_NAME (t
) == TYPE_NAME (type
)
13927 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
13928 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
13929 TYPE_ATTRIBUTES (type
)))
13934 tree domain
= TYPE_DOMAIN (type
);
13936 t
= build_variant_type_copy (type
);
13937 TREE_TYPE (t
) = element_type
;
13939 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
13940 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
13941 SET_TYPE_STRUCTURAL_EQUALITY (t
);
13942 else if (TYPE_CANONICAL (element_type
) != element_type
13943 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
13945 tree unqualified_canon
13946 = build_array_type (TYPE_CANONICAL (element_type
),
13947 domain
? TYPE_CANONICAL (domain
)
13949 if (TYPE_REVERSE_STORAGE_ORDER (type
))
13952 = build_distinct_type_copy (unqualified_canon
);
13953 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon
) = 1;
13956 = c_build_qualified_type (unqualified_canon
, type_quals
);
13959 TYPE_CANONICAL (t
) = t
;
13964 /* A restrict-qualified pointer type must be a pointer to object or
13965 incomplete type. Note that the use of POINTER_TYPE_P also allows
13966 REFERENCE_TYPEs, which is appropriate for C++. */
13967 if ((type_quals
& TYPE_QUAL_RESTRICT
)
13968 && (!POINTER_TYPE_P (type
)
13969 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
13971 error ("invalid use of %<restrict%>");
13972 type_quals
&= ~TYPE_QUAL_RESTRICT
;
13975 tree var_type
= (orig_qual_type
&& orig_qual_indirect
== 0
13977 : build_qualified_type (type
, type_quals
));
13978 /* A variant type does not inherit the list of incomplete vars from the
13979 type main variant. */
13980 if (RECORD_OR_UNION_TYPE_P (var_type
)
13981 && TYPE_MAIN_VARIANT (var_type
) != var_type
)
13982 C_TYPE_INCOMPLETE_VARS (var_type
) = 0;
13986 /* Build a VA_ARG_EXPR for the C parser. */
13989 c_build_va_arg (location_t loc1
, tree expr
, location_t loc2
, tree type
)
13991 if (error_operand_p (type
))
13992 return error_mark_node
;
13993 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13994 order because it takes the address of the expression. */
13995 else if (handled_component_p (expr
)
13996 && reverse_storage_order_for_component_p (expr
))
13998 error_at (loc1
, "cannot use %<va_arg%> with reverse storage order");
13999 return error_mark_node
;
14001 else if (!COMPLETE_TYPE_P (type
))
14003 error_at (loc2
, "second argument to %<va_arg%> is of incomplete "
14005 return error_mark_node
;
14007 else if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
14008 warning_at (loc2
, OPT_Wc___compat
,
14009 "C++ requires promoted type, not enum type, in %<va_arg%>");
14010 return build_va_arg (loc2
, expr
, type
);
14013 /* Return truthvalue of whether T1 is the same tree structure as T2.
14014 Return 1 if they are the same. Return false if they are different. */
14017 c_tree_equal (tree t1
, tree t2
)
14019 enum tree_code code1
, code2
;
14026 for (code1
= TREE_CODE (t1
);
14027 CONVERT_EXPR_CODE_P (code1
)
14028 || code1
== NON_LVALUE_EXPR
;
14029 code1
= TREE_CODE (t1
))
14030 t1
= TREE_OPERAND (t1
, 0);
14031 for (code2
= TREE_CODE (t2
);
14032 CONVERT_EXPR_CODE_P (code2
)
14033 || code2
== NON_LVALUE_EXPR
;
14034 code2
= TREE_CODE (t2
))
14035 t2
= TREE_OPERAND (t2
, 0);
14037 /* They might have become equal now. */
14041 if (code1
!= code2
)
14047 return wi::eq_p (t1
, t2
);
14050 return real_equal (&TREE_REAL_CST (t1
), &TREE_REAL_CST (t2
));
14053 return TREE_STRING_LENGTH (t1
) == TREE_STRING_LENGTH (t2
)
14054 && !memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
14055 TREE_STRING_LENGTH (t1
));
14058 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1
),
14059 TREE_FIXED_CST (t2
));
14062 return c_tree_equal (TREE_REALPART (t1
), TREE_REALPART (t2
))
14063 && c_tree_equal (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
14066 return operand_equal_p (t1
, t2
, OEP_ONLY_CONST
);
14069 /* We need to do this when determining whether or not two
14070 non-type pointer to member function template arguments
14072 if (!comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))
14073 || CONSTRUCTOR_NELTS (t1
) != CONSTRUCTOR_NELTS (t2
))
14078 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, field
, value
)
14080 constructor_elt
*elt2
= CONSTRUCTOR_ELT (t2
, i
);
14081 if (!c_tree_equal (field
, elt2
->index
)
14082 || !c_tree_equal (value
, elt2
->value
))
14089 if (!c_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)))
14091 if (!c_tree_equal (TREE_VALUE (t1
), TREE_VALUE (t2
)))
14093 return c_tree_equal (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
14096 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
14101 call_expr_arg_iterator iter1
, iter2
;
14102 if (!c_tree_equal (CALL_EXPR_FN (t1
), CALL_EXPR_FN (t2
)))
14104 for (arg1
= first_call_expr_arg (t1
, &iter1
),
14105 arg2
= first_call_expr_arg (t2
, &iter2
);
14107 arg1
= next_call_expr_arg (&iter1
),
14108 arg2
= next_call_expr_arg (&iter2
))
14109 if (!c_tree_equal (arg1
, arg2
))
14118 tree o1
= TREE_OPERAND (t1
, 0);
14119 tree o2
= TREE_OPERAND (t2
, 0);
14121 /* Special case: if either target is an unallocated VAR_DECL,
14122 it means that it's going to be unified with whatever the
14123 TARGET_EXPR is really supposed to initialize, so treat it
14124 as being equivalent to anything. */
14125 if (VAR_P (o1
) && DECL_NAME (o1
) == NULL_TREE
14126 && !DECL_RTL_SET_P (o1
))
14128 else if (VAR_P (o2
) && DECL_NAME (o2
) == NULL_TREE
14129 && !DECL_RTL_SET_P (o2
))
14131 else if (!c_tree_equal (o1
, o2
))
14134 return c_tree_equal (TREE_OPERAND (t1
, 1), TREE_OPERAND (t2
, 1));
14137 case COMPONENT_REF
:
14138 if (TREE_OPERAND (t1
, 1) != TREE_OPERAND (t2
, 1))
14140 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
14146 case FUNCTION_DECL
:
14147 case IDENTIFIER_NODE
:
14154 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
14156 for (ix
= TREE_VEC_LENGTH (t1
); ix
--;)
14157 if (!c_tree_equal (TREE_VEC_ELT (t1
, ix
),
14158 TREE_VEC_ELT (t2
, ix
)))
14167 switch (TREE_CODE_CLASS (code1
))
14171 case tcc_comparison
:
14172 case tcc_expression
:
14174 case tcc_reference
:
14175 case tcc_statement
:
14177 int i
, n
= TREE_OPERAND_LENGTH (t1
);
14181 case PREINCREMENT_EXPR
:
14182 case PREDECREMENT_EXPR
:
14183 case POSTINCREMENT_EXPR
:
14184 case POSTDECREMENT_EXPR
:
14194 if (TREE_CODE_CLASS (code1
) == tcc_vl_exp
14195 && n
!= TREE_OPERAND_LENGTH (t2
))
14198 for (i
= 0; i
< n
; ++i
)
14199 if (!c_tree_equal (TREE_OPERAND (t1
, i
), TREE_OPERAND (t2
, i
)))
14206 return comptypes (t1
, t2
);
14208 gcc_unreachable ();
14210 /* We can get here with --disable-checking. */
14214 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14215 spawn-helper and BODY is the newly created body for FNDECL. */
14218 cilk_install_body_with_frame_cleanup (tree fndecl
, tree body
, void *w
)
14220 tree list
= alloc_stmt_list ();
14221 tree frame
= make_cilk_frame (fndecl
);
14222 tree dtor
= create_cilk_function_exit (frame
, false, true);
14223 add_local_decl (cfun
, frame
);
14225 DECL_SAVED_TREE (fndecl
) = list
;
14227 tree body_list
= alloc_stmt_list ();
14228 cilk_outline (fndecl
, &body
, (struct wrapper_data
*) w
);
14229 body
= fold_build_cleanup_point_expr (void_type_node
, body
);
14231 append_to_statement_list (body
, &body_list
);
14232 append_to_statement_list (build_stmt (EXPR_LOCATION (body
), TRY_FINALLY_EXPR
,
14233 body_list
, dtor
), &list
);
14236 /* Returns true when the function declaration FNDECL is implicit,
14237 introduced as a result of a call to an otherwise undeclared
14238 function, and false otherwise. */
14241 c_decl_implicit (const_tree fndecl
)
14243 return C_DECL_IMPLICIT (fndecl
);