1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
28 #include "coretypes.h"
31 #include "stor-layout.h"
32 #include "trans-mem.h"
35 #include "langhooks.h"
41 #include "tree-iterator.h"
43 #include "pointer-set.h"
44 #include "basic-block.h"
45 #include "gimple-expr.h"
47 #include "tree-inline.h"
49 #include "c-family/c-objc.h"
50 #include "c-family/c-common.h"
51 #include "c-family/c-ubsan.h"
54 /* Possible cases of implicit bad conversions. Used to select
55 diagnostic messages in convert_for_assignment. */
63 /* The level of nesting inside "__alignof__". */
66 /* The level of nesting inside "sizeof". */
69 /* The level of nesting inside "typeof". */
72 /* The argument of last parsed sizeof expression, only to be tested
73 if expr.original_code == SIZEOF_EXPR. */
74 tree c_last_sizeof_arg;
76 /* Nonzero if we've already printed a "missing braces around initializer"
77 message within this initializer. */
78 static int missing_braces_mentioned;
80 static int require_constant_value;
81 static int require_constant_elements;
83 static bool null_pointer_constant_p (const_tree);
84 static tree qualify_type (tree, tree);
85 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
87 static int comp_target_types (location_t, tree, tree);
88 static int function_types_compatible_p (const_tree, const_tree, bool *,
90 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
91 static tree lookup_field (tree, tree);
92 static int convert_arguments (location_t, vec<location_t>, tree,
93 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
95 static tree pointer_diff (location_t, tree, tree);
96 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
97 enum impl_conv, bool, tree, tree, int);
98 static tree valid_compound_expr_initializer (tree, tree);
99 static void push_string (const char *);
100 static void push_member_name (tree);
101 static int spelling_length (void);
102 static char *print_spelling (char *);
103 static void warning_init (location_t, int, const char *);
104 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
105 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
106 bool, struct obstack *);
107 static void output_pending_init_elements (int, struct obstack *);
108 static int set_designator (int, struct obstack *);
109 static void push_range_stack (tree, struct obstack *);
110 static void add_pending_init (location_t, tree, tree, tree, bool,
112 static void set_nonincremental_init (struct obstack *);
113 static void set_nonincremental_init_from_string (tree, struct obstack *);
114 static tree find_init_member (tree, struct obstack *);
115 static void readonly_warning (tree, enum lvalue_use);
116 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
117 static void record_maybe_used_decl (tree);
118 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
120 /* Return true if EXP is a null pointer constant, false otherwise. */
123 null_pointer_constant_p (const_tree expr)
125 /* This should really operate on c_expr structures, but they aren't
126 yet available everywhere required. */
127 tree type = TREE_TYPE (expr);
128 return (TREE_CODE (expr) == INTEGER_CST
129 && !TREE_OVERFLOW (expr)
130 && integer_zerop (expr)
131 && (INTEGRAL_TYPE_P (type)
132 || (TREE_CODE (type) == POINTER_TYPE
133 && VOID_TYPE_P (TREE_TYPE (type))
134 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
137 /* EXPR may appear in an unevaluated part of an integer constant
138 expression, but not in an evaluated part. Wrap it in a
139 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
140 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
143 note_integer_operands (tree expr)
146 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
148 ret = copy_node (expr);
149 TREE_OVERFLOW (ret) = 1;
153 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
154 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
159 /* Having checked whether EXPR may appear in an unevaluated part of an
160 integer constant expression and found that it may, remove any
161 C_MAYBE_CONST_EXPR noting this fact and return the resulting
165 remove_c_maybe_const_expr (tree expr)
167 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
168 return C_MAYBE_CONST_EXPR_EXPR (expr);
173 \f/* This is a cache to hold if two types are compatible or not. */
175 struct tagged_tu_seen_cache {
176 const struct tagged_tu_seen_cache * next;
179 /* The return value of tagged_types_tu_compatible_p if we had seen
180 these two types already. */
184 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
185 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
187 /* Do `exp = require_complete_type (exp);' to make sure exp
188 does not have an incomplete type. (That includes void types.) */
191 require_complete_type (tree value)
193 tree type = TREE_TYPE (value);
195 if (value == error_mark_node || type == error_mark_node)
196 return error_mark_node;
198 /* First, detect a valid value with a complete type. */
199 if (COMPLETE_TYPE_P (type))
202 c_incomplete_type_error (value, type);
203 return error_mark_node;
206 /* Print an error message for invalid use of an incomplete type.
207 VALUE is the expression that was used (or 0 if that isn't known)
208 and TYPE is the type that was invalid. */
211 c_incomplete_type_error (const_tree value, const_tree type)
213 const char *type_code_string;
215 /* Avoid duplicate error message. */
216 if (TREE_CODE (type) == ERROR_MARK)
219 if (value != 0 && (TREE_CODE (value) == VAR_DECL
220 || TREE_CODE (value) == PARM_DECL))
221 error ("%qD has an incomplete type", value);
225 /* We must print an error message. Be clever about what it says. */
227 switch (TREE_CODE (type))
230 type_code_string = "struct";
234 type_code_string = "union";
238 type_code_string = "enum";
242 error ("invalid use of void expression");
246 if (TYPE_DOMAIN (type))
248 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
250 error ("invalid use of flexible array member");
253 type = TREE_TYPE (type);
256 error ("invalid use of array with unspecified bounds");
263 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
264 error ("invalid use of undefined type %<%s %E%>",
265 type_code_string, TYPE_NAME (type));
267 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
268 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
272 /* Given a type, apply default promotions wrt unnamed function
273 arguments and return the new type. */
276 c_type_promotes_to (tree type)
278 tree ret = NULL_TREE;
280 if (TYPE_MAIN_VARIANT (type) == float_type_node)
281 ret = double_type_node;
282 else if (c_promoting_integer_type_p (type))
284 /* Preserve unsignedness if not really getting any wider. */
285 if (TYPE_UNSIGNED (type)
286 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
287 ret = unsigned_type_node;
289 ret = integer_type_node;
292 if (ret != NULL_TREE)
293 return (TYPE_ATOMIC (type)
294 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
300 /* Return true if between two named address spaces, whether there is a superset
301 named address space that encompasses both address spaces. If there is a
302 superset, return which address space is the superset. */
305 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
312 else if (targetm.addr_space.subset_p (as1, as2))
317 else if (targetm.addr_space.subset_p (as2, as1))
326 /* Return a variant of TYPE which has all the type qualifiers of LIKE
327 as well as those of TYPE. */
330 qualify_type (tree type, tree like)
332 addr_space_t as_type = TYPE_ADDR_SPACE (type);
333 addr_space_t as_like = TYPE_ADDR_SPACE (like);
334 addr_space_t as_common;
336 /* If the two named address spaces are different, determine the common
337 superset address space. If there isn't one, raise an error. */
338 if (!addr_space_superset (as_type, as_like, &as_common))
341 error ("%qT and %qT are in disjoint named address spaces",
345 return c_build_qualified_type (type,
346 TYPE_QUALS_NO_ADDR_SPACE (type)
347 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
348 | ENCODE_QUAL_ADDR_SPACE (as_common));
351 /* Return true iff the given tree T is a variable length array. */
354 c_vla_type_p (const_tree t)
356 if (TREE_CODE (t) == ARRAY_TYPE
357 && C_TYPE_VARIABLE_SIZE (t))
362 /* Return the composite type of two compatible types.
364 We assume that comptypes has already been done and returned
365 nonzero; if that isn't so, this may crash. In particular, we
366 assume that qualifiers match. */
369 composite_type (tree t1, tree t2)
371 enum tree_code code1;
372 enum tree_code code2;
375 /* Save time if the two types are the same. */
377 if (t1 == t2) return t1;
379 /* If one type is nonsense, use the other. */
380 if (t1 == error_mark_node)
382 if (t2 == error_mark_node)
385 code1 = TREE_CODE (t1);
386 code2 = TREE_CODE (t2);
388 /* Merge the attributes. */
389 attributes = targetm.merge_type_attributes (t1, t2);
391 /* If one is an enumerated type and the other is the compatible
392 integer type, the composite type might be either of the two
393 (DR#013 question 3). For consistency, use the enumerated type as
394 the composite type. */
396 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
398 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
401 gcc_assert (code1 == code2);
406 /* For two pointers, do this recursively on the target type. */
408 tree pointed_to_1 = TREE_TYPE (t1);
409 tree pointed_to_2 = TREE_TYPE (t2);
410 tree target = composite_type (pointed_to_1, pointed_to_2);
411 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
412 t1 = build_type_attribute_variant (t1, attributes);
413 return qualify_type (t1, t2);
418 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
421 tree d1 = TYPE_DOMAIN (t1);
422 tree d2 = TYPE_DOMAIN (t2);
423 bool d1_variable, d2_variable;
424 bool d1_zero, d2_zero;
425 bool t1_complete, t2_complete;
427 /* We should not have any type quals on arrays at all. */
428 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
429 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
431 t1_complete = COMPLETE_TYPE_P (t1);
432 t2_complete = COMPLETE_TYPE_P (t2);
434 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
435 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
437 d1_variable = (!d1_zero
438 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
439 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
440 d2_variable = (!d2_zero
441 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
442 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
443 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
444 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
446 /* Save space: see if the result is identical to one of the args. */
447 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
448 && (d2_variable || d2_zero || !d1_variable))
449 return build_type_attribute_variant (t1, attributes);
450 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
451 && (d1_variable || d1_zero || !d2_variable))
452 return build_type_attribute_variant (t2, attributes);
454 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
455 return build_type_attribute_variant (t1, attributes);
456 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
457 return build_type_attribute_variant (t2, attributes);
459 /* Merge the element types, and have a size if either arg has
460 one. We may have qualifiers on the element types. To set
461 up TYPE_MAIN_VARIANT correctly, we need to form the
462 composite of the unqualified types and add the qualifiers
464 quals = TYPE_QUALS (strip_array_types (elt));
465 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
466 t1 = build_array_type (unqual_elt,
467 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
473 /* Ensure a composite type involving a zero-length array type
474 is a zero-length type not an incomplete type. */
475 if (d1_zero && d2_zero
476 && (t1_complete || t2_complete)
477 && !COMPLETE_TYPE_P (t1))
479 TYPE_SIZE (t1) = bitsize_zero_node;
480 TYPE_SIZE_UNIT (t1) = size_zero_node;
482 t1 = c_build_qualified_type (t1, quals);
483 return build_type_attribute_variant (t1, attributes);
489 if (attributes != NULL)
491 /* Try harder not to create a new aggregate type. */
492 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
494 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
497 return build_type_attribute_variant (t1, attributes);
500 /* Function types: prefer the one that specified arg types.
501 If both do, merge the arg types. Also merge the return types. */
503 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
504 tree p1 = TYPE_ARG_TYPES (t1);
505 tree p2 = TYPE_ARG_TYPES (t2);
510 /* Save space: see if the result is identical to one of the args. */
511 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
512 return build_type_attribute_variant (t1, attributes);
513 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
514 return build_type_attribute_variant (t2, attributes);
516 /* Simple way if one arg fails to specify argument types. */
517 if (TYPE_ARG_TYPES (t1) == 0)
519 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
520 t1 = build_type_attribute_variant (t1, attributes);
521 return qualify_type (t1, t2);
523 if (TYPE_ARG_TYPES (t2) == 0)
525 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
526 t1 = build_type_attribute_variant (t1, attributes);
527 return qualify_type (t1, t2);
530 /* If both args specify argument types, we must merge the two
531 lists, argument by argument. */
533 len = list_length (p1);
536 for (i = 0; i < len; i++)
537 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
542 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
544 /* A null type means arg type is not specified.
545 Take whatever the other function type has. */
546 if (TREE_VALUE (p1) == 0)
548 TREE_VALUE (n) = TREE_VALUE (p2);
551 if (TREE_VALUE (p2) == 0)
553 TREE_VALUE (n) = TREE_VALUE (p1);
557 /* Given wait (union {union wait *u; int *i} *)
558 and wait (union wait *),
559 prefer union wait * as type of parm. */
560 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
561 && TREE_VALUE (p1) != TREE_VALUE (p2))
564 tree mv2 = TREE_VALUE (p2);
565 if (mv2 && mv2 != error_mark_node
566 && TREE_CODE (mv2) != ARRAY_TYPE)
567 mv2 = TYPE_MAIN_VARIANT (mv2);
568 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
569 memb; memb = DECL_CHAIN (memb))
571 tree mv3 = TREE_TYPE (memb);
572 if (mv3 && mv3 != error_mark_node
573 && TREE_CODE (mv3) != ARRAY_TYPE)
574 mv3 = TYPE_MAIN_VARIANT (mv3);
575 if (comptypes (mv3, mv2))
577 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
579 pedwarn (input_location, OPT_Wpedantic,
580 "function types not truly compatible in ISO C");
585 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
586 && TREE_VALUE (p2) != TREE_VALUE (p1))
589 tree mv1 = TREE_VALUE (p1);
590 if (mv1 && mv1 != error_mark_node
591 && TREE_CODE (mv1) != ARRAY_TYPE)
592 mv1 = TYPE_MAIN_VARIANT (mv1);
593 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
594 memb; memb = DECL_CHAIN (memb))
596 tree mv3 = TREE_TYPE (memb);
597 if (mv3 && mv3 != error_mark_node
598 && TREE_CODE (mv3) != ARRAY_TYPE)
599 mv3 = TYPE_MAIN_VARIANT (mv3);
600 if (comptypes (mv3, mv1))
602 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
604 pedwarn (input_location, OPT_Wpedantic,
605 "function types not truly compatible in ISO C");
610 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
614 t1 = build_function_type (valtype, newargs);
615 t1 = qualify_type (t1, t2);
616 /* ... falls through ... */
620 return build_type_attribute_variant (t1, attributes);
625 /* Return the type of a conditional expression between pointers to
626 possibly differently qualified versions of compatible types.
628 We assume that comp_target_types has already been done and returned
629 nonzero; if that isn't so, this may crash. */
632 common_pointer_type (tree t1, tree t2)
635 tree pointed_to_1, mv1;
636 tree pointed_to_2, mv2;
638 unsigned target_quals;
639 addr_space_t as1, as2, as_common;
642 /* Save time if the two types are the same. */
644 if (t1 == t2) return t1;
646 /* If one type is nonsense, use the other. */
647 if (t1 == error_mark_node)
649 if (t2 == error_mark_node)
652 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
653 && TREE_CODE (t2) == POINTER_TYPE);
655 /* Merge the attributes. */
656 attributes = targetm.merge_type_attributes (t1, t2);
658 /* Find the composite type of the target types, and combine the
659 qualifiers of the two types' targets. Do not lose qualifiers on
660 array element types by taking the TYPE_MAIN_VARIANT. */
661 mv1 = pointed_to_1 = TREE_TYPE (t1);
662 mv2 = pointed_to_2 = TREE_TYPE (t2);
663 if (TREE_CODE (mv1) != ARRAY_TYPE)
664 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
665 if (TREE_CODE (mv2) != ARRAY_TYPE)
666 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
667 target = composite_type (mv1, mv2);
669 /* For function types do not merge const qualifiers, but drop them
670 if used inconsistently. The middle-end uses these to mark const
671 and noreturn functions. */
672 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
673 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
675 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
676 target_quals = (quals1 & quals2);
678 target_quals = (quals1 | quals2);
680 /* If the two named address spaces are different, determine the common
681 superset address space. This is guaranteed to exist due to the
682 assumption that comp_target_type returned non-zero. */
683 as1 = TYPE_ADDR_SPACE (pointed_to_1);
684 as2 = TYPE_ADDR_SPACE (pointed_to_2);
685 if (!addr_space_superset (as1, as2, &as_common))
688 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
690 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
691 return build_type_attribute_variant (t1, attributes);
694 /* Return the common type for two arithmetic types under the usual
695 arithmetic conversions. The default conversions have already been
696 applied, and enumerated types converted to their compatible integer
697 types. The resulting type is unqualified and has no attributes.
699 This is the type for the result of most arithmetic operations
700 if the operands have the given two types. */
703 c_common_type (tree t1, tree t2)
705 enum tree_code code1;
706 enum tree_code code2;
708 /* If one type is nonsense, use the other. */
709 if (t1 == error_mark_node)
711 if (t2 == error_mark_node)
714 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
715 t1 = TYPE_MAIN_VARIANT (t1);
717 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
718 t2 = TYPE_MAIN_VARIANT (t2);
720 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
721 t1 = build_type_attribute_variant (t1, NULL_TREE);
723 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
724 t2 = build_type_attribute_variant (t2, NULL_TREE);
726 /* Save time if the two types are the same. */
728 if (t1 == t2) return t1;
730 code1 = TREE_CODE (t1);
731 code2 = TREE_CODE (t2);
733 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
734 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
735 || code1 == INTEGER_TYPE);
736 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
737 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
738 || code2 == INTEGER_TYPE);
740 /* When one operand is a decimal float type, the other operand cannot be
741 a generic float type or a complex type. We also disallow vector types
743 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
744 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
746 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
748 error ("can%'t mix operands of decimal float and vector types");
749 return error_mark_node;
751 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
753 error ("can%'t mix operands of decimal float and complex types");
754 return error_mark_node;
756 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
758 error ("can%'t mix operands of decimal float and other float types");
759 return error_mark_node;
763 /* If one type is a vector type, return that type. (How the usual
764 arithmetic conversions apply to the vector types extension is not
765 precisely specified.) */
766 if (code1 == VECTOR_TYPE)
769 if (code2 == VECTOR_TYPE)
772 /* If one type is complex, form the common type of the non-complex
773 components, then make that complex. Use T1 or T2 if it is the
775 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
777 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
778 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
779 tree subtype = c_common_type (subtype1, subtype2);
781 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
783 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
786 return build_complex_type (subtype);
789 /* If only one is real, use it as the result. */
791 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
794 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
797 /* If both are real and either are decimal floating point types, use
798 the decimal floating point type with the greater precision. */
800 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
802 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
803 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
804 return dfloat128_type_node;
805 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
806 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
807 return dfloat64_type_node;
808 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
809 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
810 return dfloat32_type_node;
813 /* Deal with fixed-point types. */
814 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
816 unsigned int unsignedp = 0, satp = 0;
817 enum machine_mode m1, m2;
818 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
823 /* If one input type is saturating, the result type is saturating. */
824 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
827 /* If both fixed-point types are unsigned, the result type is unsigned.
828 When mixing fixed-point and integer types, follow the sign of the
830 Otherwise, the result type is signed. */
831 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
832 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
833 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
834 && TYPE_UNSIGNED (t1))
835 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
836 && TYPE_UNSIGNED (t2)))
839 /* The result type is signed. */
842 /* If the input type is unsigned, we need to convert to the
844 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
846 enum mode_class mclass = (enum mode_class) 0;
847 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
849 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
853 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
855 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
857 enum mode_class mclass = (enum mode_class) 0;
858 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
860 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
864 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
868 if (code1 == FIXED_POINT_TYPE)
870 fbit1 = GET_MODE_FBIT (m1);
871 ibit1 = GET_MODE_IBIT (m1);
876 /* Signed integers need to subtract one sign bit. */
877 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
880 if (code2 == FIXED_POINT_TYPE)
882 fbit2 = GET_MODE_FBIT (m2);
883 ibit2 = GET_MODE_IBIT (m2);
888 /* Signed integers need to subtract one sign bit. */
889 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
892 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
893 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
894 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
898 /* Both real or both integers; use the one with greater precision. */
900 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
902 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
905 /* Same precision. Prefer long longs to longs to ints when the
906 same precision, following the C99 rules on integer type rank
907 (which are equivalent to the C90 rules for C90 types). */
909 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
910 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
911 return long_long_unsigned_type_node;
913 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
914 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
916 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
917 return long_long_unsigned_type_node;
919 return long_long_integer_type_node;
922 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
923 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
924 return long_unsigned_type_node;
926 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
927 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
929 /* But preserve unsignedness from the other type,
930 since long cannot hold all the values of an unsigned int. */
931 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
932 return long_unsigned_type_node;
934 return long_integer_type_node;
937 /* Likewise, prefer long double to double even if same size. */
938 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
939 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
940 return long_double_type_node;
942 /* Likewise, prefer double to float even if same size.
943 We got a couple of embedded targets with 32 bit doubles, and the
944 pdp11 might have 64 bit floats. */
945 if (TYPE_MAIN_VARIANT (t1) == double_type_node
946 || TYPE_MAIN_VARIANT (t2) == double_type_node)
947 return double_type_node;
949 /* Otherwise prefer the unsigned one. */
951 if (TYPE_UNSIGNED (t1))
957 /* Wrapper around c_common_type that is used by c-common.c and other
958 front end optimizations that remove promotions. ENUMERAL_TYPEs
959 are allowed here and are converted to their compatible integer types.
960 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
961 preferably a non-Boolean type as the common type. */
963 common_type (tree t1, tree t2)
965 if (TREE_CODE (t1) == ENUMERAL_TYPE)
966 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
967 if (TREE_CODE (t2) == ENUMERAL_TYPE)
968 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
970 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
971 if (TREE_CODE (t1) == BOOLEAN_TYPE
972 && TREE_CODE (t2) == BOOLEAN_TYPE)
973 return boolean_type_node;
975 /* If either type is BOOLEAN_TYPE, then return the other. */
976 if (TREE_CODE (t1) == BOOLEAN_TYPE)
978 if (TREE_CODE (t2) == BOOLEAN_TYPE)
981 return c_common_type (t1, t2);
984 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
985 or various other operations. Return 2 if they are compatible
986 but a warning may be needed if you use them together. */
989 comptypes (tree type1, tree type2)
991 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
994 val = comptypes_internal (type1, type2, NULL, NULL);
995 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1000 /* Like comptypes, but if it returns non-zero because enum and int are
1001 compatible, it sets *ENUM_AND_INT_P to true. */
1004 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1006 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1009 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1010 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1015 /* Like comptypes, but if it returns nonzero for different types, it
1016 sets *DIFFERENT_TYPES_P to true. */
1019 comptypes_check_different_types (tree type1, tree type2,
1020 bool *different_types_p)
1022 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1025 val = comptypes_internal (type1, type2, NULL, different_types_p);
1026 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1031 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1032 or various other operations. Return 2 if they are compatible
1033 but a warning may be needed if you use them together. If
1034 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1035 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1036 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1037 NULL, and the types are compatible but different enough not to be
1038 permitted in C11 typedef redeclarations, then this sets
1039 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1040 false, but may or may not be set if the types are incompatible.
1041 This differs from comptypes, in that we don't free the seen
1045 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1046 bool *different_types_p)
1048 const_tree t1 = type1;
1049 const_tree t2 = type2;
1052 /* Suppress errors caused by previously reported errors. */
1054 if (t1 == t2 || !t1 || !t2
1055 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1058 /* Enumerated types are compatible with integer types, but this is
1059 not transitive: two enumerated types in the same translation unit
1060 are compatible with each other only if they are the same type. */
1062 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1064 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1065 if (TREE_CODE (t2) != VOID_TYPE)
1067 if (enum_and_int_p != NULL)
1068 *enum_and_int_p = true;
1069 if (different_types_p != NULL)
1070 *different_types_p = true;
1073 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1075 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1076 if (TREE_CODE (t1) != VOID_TYPE)
1078 if (enum_and_int_p != NULL)
1079 *enum_and_int_p = true;
1080 if (different_types_p != NULL)
1081 *different_types_p = true;
1088 /* Different classes of types can't be compatible. */
1090 if (TREE_CODE (t1) != TREE_CODE (t2))
1093 /* Qualifiers must match. C99 6.7.3p9 */
1095 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1098 /* Allow for two different type nodes which have essentially the same
1099 definition. Note that we already checked for equality of the type
1100 qualifiers (just above). */
1102 if (TREE_CODE (t1) != ARRAY_TYPE
1103 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1106 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1107 if (!(attrval = comp_type_attributes (t1, t2)))
1110 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1113 switch (TREE_CODE (t1))
1116 /* Do not remove mode or aliasing information. */
1117 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1118 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1120 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1121 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1122 enum_and_int_p, different_types_p));
1126 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1132 tree d1 = TYPE_DOMAIN (t1);
1133 tree d2 = TYPE_DOMAIN (t2);
1134 bool d1_variable, d2_variable;
1135 bool d1_zero, d2_zero;
1138 /* Target types must match incl. qualifiers. */
1139 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1140 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1142 different_types_p)))
1145 if (different_types_p != NULL
1146 && (d1 == 0) != (d2 == 0))
1147 *different_types_p = true;
1148 /* Sizes must match unless one is missing or variable. */
1149 if (d1 == 0 || d2 == 0 || d1 == d2)
1152 d1_zero = !TYPE_MAX_VALUE (d1);
1153 d2_zero = !TYPE_MAX_VALUE (d2);
1155 d1_variable = (!d1_zero
1156 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1157 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1158 d2_variable = (!d2_zero
1159 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1160 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1161 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1162 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1164 if (different_types_p != NULL
1165 && d1_variable != d2_variable)
1166 *different_types_p = true;
1167 if (d1_variable || d2_variable)
1169 if (d1_zero && d2_zero)
1171 if (d1_zero || d2_zero
1172 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1173 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1182 if (val != 1 && !same_translation_unit_p (t1, t2))
1184 tree a1 = TYPE_ATTRIBUTES (t1);
1185 tree a2 = TYPE_ATTRIBUTES (t2);
1187 if (! attribute_list_contained (a1, a2)
1188 && ! attribute_list_contained (a2, a1))
1192 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1194 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1200 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1201 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1202 enum_and_int_p, different_types_p));
1208 return attrval == 2 && val == 1 ? 2 : val;
1211 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1212 their qualifiers, except for named address spaces. If the pointers point to
1213 different named addresses, then we must determine if one address space is a
1214 subset of the other. */
1217 comp_target_types (location_t location, tree ttl, tree ttr)
1220 tree mvl = TREE_TYPE (ttl);
1221 tree mvr = TREE_TYPE (ttr);
1222 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1223 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1224 addr_space_t as_common;
1225 bool enum_and_int_p;
1227 /* Fail if pointers point to incompatible address spaces. */
1228 if (!addr_space_superset (asl, asr, &as_common))
1231 /* Do not lose qualifiers on element types of array types that are
1232 pointer targets by taking their TYPE_MAIN_VARIANT. */
1233 if (TREE_CODE (mvl) != ARRAY_TYPE)
1234 mvl = (TYPE_ATOMIC (mvl)
1235 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1236 : TYPE_MAIN_VARIANT (mvl));
1237 if (TREE_CODE (mvr) != ARRAY_TYPE)
1238 mvr = (TYPE_ATOMIC (mvr)
1239 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1240 : TYPE_MAIN_VARIANT (mvr));
1241 enum_and_int_p = false;
1242 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1245 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1247 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1248 warning_at (location, OPT_Wc___compat,
1249 "pointer target types incompatible in C++");
1254 /* Subroutines of `comptypes'. */
1256 /* Determine whether two trees derive from the same translation unit.
1257 If the CONTEXT chain ends in a null, that tree's context is still
1258 being parsed, so if two trees have context chains ending in null,
1259 they're in the same translation unit. */
1261 same_translation_unit_p (const_tree t1, const_tree t2)
1263 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1264 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1266 case tcc_declaration:
1267 t1 = DECL_CONTEXT (t1); break;
1269 t1 = TYPE_CONTEXT (t1); break;
1270 case tcc_exceptional:
1271 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1272 default: gcc_unreachable ();
1275 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1276 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1278 case tcc_declaration:
1279 t2 = DECL_CONTEXT (t2); break;
1281 t2 = TYPE_CONTEXT (t2); break;
1282 case tcc_exceptional:
1283 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1284 default: gcc_unreachable ();
1290 /* Allocate the seen two types, assuming that they are compatible. */
1292 static struct tagged_tu_seen_cache *
1293 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1295 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1296 tu->next = tagged_tu_seen_base;
1300 tagged_tu_seen_base = tu;
1302 /* The C standard says that two structures in different translation
1303 units are compatible with each other only if the types of their
1304 fields are compatible (among other things). We assume that they
1305 are compatible until proven otherwise when building the cache.
1306 An example where this can occur is:
1311 If we are comparing this against a similar struct in another TU,
1312 and did not assume they were compatible, we end up with an infinite
1318 /* Free the seen types until we get to TU_TIL. */
1321 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1323 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1324 while (tu != tu_til)
1326 const struct tagged_tu_seen_cache *const tu1
1327 = (const struct tagged_tu_seen_cache *) tu;
1329 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1331 tagged_tu_seen_base = tu_til;
1334 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1335 compatible. If the two types are not the same (which has been
1336 checked earlier), this can only happen when multiple translation
1337 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1338 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1339 comptypes_internal. */
1342 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1343 bool *enum_and_int_p, bool *different_types_p)
1346 bool needs_warning = false;
1348 /* We have to verify that the tags of the types are the same. This
1349 is harder than it looks because this may be a typedef, so we have
1350 to go look at the original type. It may even be a typedef of a
1352 In the case of compiler-created builtin structs the TYPE_DECL
1353 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1354 while (TYPE_NAME (t1)
1355 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1356 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1357 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1359 while (TYPE_NAME (t2)
1360 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1361 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1362 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1364 /* C90 didn't have the requirement that the two tags be the same. */
1365 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1368 /* C90 didn't say what happened if one or both of the types were
1369 incomplete; we choose to follow C99 rules here, which is that they
1371 if (TYPE_SIZE (t1) == NULL
1372 || TYPE_SIZE (t2) == NULL)
1376 const struct tagged_tu_seen_cache * tts_i;
1377 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1378 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1382 switch (TREE_CODE (t1))
1386 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1387 /* Speed up the case where the type values are in the same order. */
1388 tree tv1 = TYPE_VALUES (t1);
1389 tree tv2 = TYPE_VALUES (t2);
1396 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1398 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1400 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1407 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1411 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1417 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1423 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1425 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1427 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1438 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1439 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1445 /* Speed up the common case where the fields are in the same order. */
1446 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1447 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1451 if (DECL_NAME (s1) != DECL_NAME (s2))
1453 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1454 enum_and_int_p, different_types_p);
1456 if (result != 1 && !DECL_NAME (s1))
1464 needs_warning = true;
1466 if (TREE_CODE (s1) == FIELD_DECL
1467 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1468 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1476 tu->val = needs_warning ? 2 : 1;
1480 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1484 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1485 if (DECL_NAME (s1) == DECL_NAME (s2))
1489 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1493 if (result != 1 && !DECL_NAME (s1))
1501 needs_warning = true;
1503 if (TREE_CODE (s1) == FIELD_DECL
1504 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1505 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1517 tu->val = needs_warning ? 2 : 10;
1523 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1525 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1527 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1530 if (TREE_CODE (s1) != TREE_CODE (s2)
1531 || DECL_NAME (s1) != DECL_NAME (s2))
1533 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1534 enum_and_int_p, different_types_p);
1538 needs_warning = true;
1540 if (TREE_CODE (s1) == FIELD_DECL
1541 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1542 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1548 tu->val = needs_warning ? 2 : 1;
1557 /* Return 1 if two function types F1 and F2 are compatible.
1558 If either type specifies no argument types,
1559 the other must specify a fixed number of self-promoting arg types.
1560 Otherwise, if one type specifies only the number of arguments,
1561 the other must specify that number of self-promoting arg types.
1562 Otherwise, the argument types must match.
1563 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1566 function_types_compatible_p (const_tree f1, const_tree f2,
1567 bool *enum_and_int_p, bool *different_types_p)
1570 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1575 ret1 = TREE_TYPE (f1);
1576 ret2 = TREE_TYPE (f2);
1578 /* 'volatile' qualifiers on a function's return type used to mean
1579 the function is noreturn. */
1580 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1581 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1582 if (TYPE_VOLATILE (ret1))
1583 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1584 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1585 if (TYPE_VOLATILE (ret2))
1586 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1587 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1588 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1592 args1 = TYPE_ARG_TYPES (f1);
1593 args2 = TYPE_ARG_TYPES (f2);
1595 if (different_types_p != NULL
1596 && (args1 == 0) != (args2 == 0))
1597 *different_types_p = true;
1599 /* An unspecified parmlist matches any specified parmlist
1600 whose argument types don't need default promotions. */
1604 if (!self_promoting_args_p (args2))
1606 /* If one of these types comes from a non-prototype fn definition,
1607 compare that with the other type's arglist.
1608 If they don't match, ask for a warning (but no error). */
1609 if (TYPE_ACTUAL_ARG_TYPES (f1)
1610 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1611 enum_and_int_p, different_types_p))
1617 if (!self_promoting_args_p (args1))
1619 if (TYPE_ACTUAL_ARG_TYPES (f2)
1620 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1621 enum_and_int_p, different_types_p))
1626 /* Both types have argument lists: compare them and propagate results. */
1627 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1629 return val1 != 1 ? val1 : val;
1632 /* Check two lists of types for compatibility, returning 0 for
1633 incompatible, 1 for compatible, or 2 for compatible with
1634 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1635 comptypes_internal. */
1638 type_lists_compatible_p (const_tree args1, const_tree args2,
1639 bool *enum_and_int_p, bool *different_types_p)
1641 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1647 tree a1, mv1, a2, mv2;
1648 if (args1 == 0 && args2 == 0)
1650 /* If one list is shorter than the other,
1651 they fail to match. */
1652 if (args1 == 0 || args2 == 0)
1654 mv1 = a1 = TREE_VALUE (args1);
1655 mv2 = a2 = TREE_VALUE (args2);
1656 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1657 mv1 = (TYPE_ATOMIC (mv1)
1658 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1660 : TYPE_MAIN_VARIANT (mv1));
1661 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1662 mv2 = (TYPE_ATOMIC (mv2)
1663 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1665 : TYPE_MAIN_VARIANT (mv2));
1666 /* A null pointer instead of a type
1667 means there is supposed to be an argument
1668 but nothing is specified about what type it has.
1669 So match anything that self-promotes. */
1670 if (different_types_p != NULL
1671 && (a1 == 0) != (a2 == 0))
1672 *different_types_p = true;
1675 if (c_type_promotes_to (a2) != a2)
1680 if (c_type_promotes_to (a1) != a1)
1683 /* If one of the lists has an error marker, ignore this arg. */
1684 else if (TREE_CODE (a1) == ERROR_MARK
1685 || TREE_CODE (a2) == ERROR_MARK)
1687 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1688 different_types_p)))
1690 if (different_types_p != NULL)
1691 *different_types_p = true;
1692 /* Allow wait (union {union wait *u; int *i} *)
1693 and wait (union wait *) to be compatible. */
1694 if (TREE_CODE (a1) == UNION_TYPE
1695 && (TYPE_NAME (a1) == 0
1696 || TYPE_TRANSPARENT_AGGR (a1))
1697 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1698 && tree_int_cst_equal (TYPE_SIZE (a1),
1702 for (memb = TYPE_FIELDS (a1);
1703 memb; memb = DECL_CHAIN (memb))
1705 tree mv3 = TREE_TYPE (memb);
1706 if (mv3 && mv3 != error_mark_node
1707 && TREE_CODE (mv3) != ARRAY_TYPE)
1708 mv3 = (TYPE_ATOMIC (mv3)
1709 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1711 : TYPE_MAIN_VARIANT (mv3));
1712 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1719 else if (TREE_CODE (a2) == UNION_TYPE
1720 && (TYPE_NAME (a2) == 0
1721 || TYPE_TRANSPARENT_AGGR (a2))
1722 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1723 && tree_int_cst_equal (TYPE_SIZE (a2),
1727 for (memb = TYPE_FIELDS (a2);
1728 memb; memb = DECL_CHAIN (memb))
1730 tree mv3 = TREE_TYPE (memb);
1731 if (mv3 && mv3 != error_mark_node
1732 && TREE_CODE (mv3) != ARRAY_TYPE)
1733 mv3 = (TYPE_ATOMIC (mv3)
1734 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1736 : TYPE_MAIN_VARIANT (mv3));
1737 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1748 /* comptypes said ok, but record if it said to warn. */
1752 args1 = TREE_CHAIN (args1);
1753 args2 = TREE_CHAIN (args2);
1757 /* Compute the size to increment a pointer by. */
1760 c_size_in_bytes (const_tree type)
1762 enum tree_code code = TREE_CODE (type);
1764 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1765 return size_one_node;
1767 if (!COMPLETE_OR_VOID_TYPE_P (type))
1769 error ("arithmetic on pointer to an incomplete type");
1770 return size_one_node;
1773 /* Convert in case a char is more than one unit. */
1774 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1775 size_int (TYPE_PRECISION (char_type_node)
1779 /* Return either DECL or its known constant value (if it has one). */
1782 decl_constant_value (tree decl)
1784 if (/* Don't change a variable array bound or initial value to a constant
1785 in a place where a variable is invalid. Note that DECL_INITIAL
1786 isn't valid for a PARM_DECL. */
1787 current_function_decl != 0
1788 && TREE_CODE (decl) != PARM_DECL
1789 && !TREE_THIS_VOLATILE (decl)
1790 && TREE_READONLY (decl)
1791 && DECL_INITIAL (decl) != 0
1792 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1793 /* This is invalid if initial value is not constant.
1794 If it has either a function call, a memory reference,
1795 or a variable, then re-evaluating it could give different results. */
1796 && TREE_CONSTANT (DECL_INITIAL (decl))
1797 /* Check for cases where this is sub-optimal, even though valid. */
1798 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1799 return DECL_INITIAL (decl);
1803 /* Convert the array expression EXP to a pointer. */
1805 array_to_pointer_conversion (location_t loc, tree exp)
1807 tree orig_exp = exp;
1808 tree type = TREE_TYPE (exp);
1810 tree restype = TREE_TYPE (type);
1813 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1815 STRIP_TYPE_NOPS (exp);
1817 if (TREE_NO_WARNING (orig_exp))
1818 TREE_NO_WARNING (exp) = 1;
1820 ptrtype = build_pointer_type (restype);
1822 if (TREE_CODE (exp) == INDIRECT_REF)
1823 return convert (ptrtype, TREE_OPERAND (exp, 0));
1825 /* In C++ array compound literals are temporary objects unless they are
1826 const or appear in namespace scope, so they are destroyed too soon
1827 to use them for much of anything (c++/53220). */
1828 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1830 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1831 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1832 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1833 "converting an array compound literal to a pointer "
1834 "is ill-formed in C++");
1837 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1838 return convert (ptrtype, adr);
1841 /* Convert the function expression EXP to a pointer. */
1843 function_to_pointer_conversion (location_t loc, tree exp)
1845 tree orig_exp = exp;
1847 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1849 STRIP_TYPE_NOPS (exp);
1851 if (TREE_NO_WARNING (orig_exp))
1852 TREE_NO_WARNING (exp) = 1;
1854 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1857 /* Mark EXP as read, not just set, for set but not used -Wunused
1858 warning purposes. */
1861 mark_exp_read (tree exp)
1863 switch (TREE_CODE (exp))
1867 DECL_READ_P (exp) = 1;
1876 mark_exp_read (TREE_OPERAND (exp, 0));
1879 case C_MAYBE_CONST_EXPR:
1880 mark_exp_read (TREE_OPERAND (exp, 1));
1887 /* Perform the default conversion of arrays and functions to pointers.
1888 Return the result of converting EXP. For any other expression, just
1891 LOC is the location of the expression. */
1894 default_function_array_conversion (location_t loc, struct c_expr exp)
1896 tree orig_exp = exp.value;
1897 tree type = TREE_TYPE (exp.value);
1898 enum tree_code code = TREE_CODE (type);
1904 bool not_lvalue = false;
1905 bool lvalue_array_p;
1907 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1908 || CONVERT_EXPR_P (exp.value))
1909 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1911 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1913 exp.value = TREE_OPERAND (exp.value, 0);
1916 if (TREE_NO_WARNING (orig_exp))
1917 TREE_NO_WARNING (exp.value) = 1;
1919 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1920 if (!flag_isoc99 && !lvalue_array_p)
1922 /* Before C99, non-lvalue arrays do not decay to pointers.
1923 Normally, using such an array would be invalid; but it can
1924 be used correctly inside sizeof or as a statement expression.
1925 Thus, do not give an error here; an error will result later. */
1929 exp.value = array_to_pointer_conversion (loc, exp.value);
1933 exp.value = function_to_pointer_conversion (loc, exp.value);
1943 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1945 mark_exp_read (exp.value);
1946 return default_function_array_conversion (loc, exp);
1949 /* Return whether EXPR should be treated as an atomic lvalue for the
1950 purposes of load and store handling. */
1953 really_atomic_lvalue (tree expr)
1955 if (expr == error_mark_node || TREE_TYPE (expr) == error_mark_node)
1957 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1959 if (!lvalue_p (expr))
1962 /* Ignore _Atomic on register variables, since their addresses can't
1963 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1964 sequences wouldn't work. Ignore _Atomic on structures containing
1965 bit-fields, since accessing elements of atomic structures or
1966 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1967 it's undefined at translation time or execution time, and the
1968 normal atomic sequences again wouldn't work. */
1969 while (handled_component_p (expr))
1971 if (TREE_CODE (expr) == COMPONENT_REF
1972 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1974 expr = TREE_OPERAND (expr, 0);
1976 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1981 /* Convert expression EXP (location LOC) from lvalue to rvalue,
1982 including converting functions and arrays to pointers if CONVERT_P.
1983 If READ_P, also mark the expression as having been read. */
1986 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1987 bool convert_p, bool read_p)
1990 mark_exp_read (exp.value);
1992 exp = default_function_array_conversion (loc, exp);
1993 if (really_atomic_lvalue (exp.value))
1995 vec<tree, va_gc> *params;
1996 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
1997 tree expr_type = TREE_TYPE (exp.value);
1998 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
1999 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2001 gcc_assert (TYPE_ATOMIC (expr_type));
2003 /* Expansion of a generic atomic load may require an addition
2004 element, so allocate enough to prevent a resize. */
2005 vec_alloc (params, 4);
2007 /* Remove the qualifiers for the rest of the expressions and
2008 create the VAL temp variable to hold the RHS. */
2009 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2010 tmp = create_tmp_var (nonatomic_type, NULL);
2011 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2012 TREE_ADDRESSABLE (tmp) = 1;
2013 TREE_NO_WARNING (tmp) = 1;
2015 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2016 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2017 params->quick_push (expr_addr);
2018 params->quick_push (tmp_addr);
2019 params->quick_push (seq_cst);
2020 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2022 /* EXPR is always read. */
2023 mark_exp_read (exp.value);
2025 /* Return tmp which contains the value loaded. */
2026 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2031 /* EXP is an expression of integer type. Apply the integer promotions
2032 to it and return the promoted value. */
2035 perform_integral_promotions (tree exp)
2037 tree type = TREE_TYPE (exp);
2038 enum tree_code code = TREE_CODE (type);
2040 gcc_assert (INTEGRAL_TYPE_P (type));
2042 /* Normally convert enums to int,
2043 but convert wide enums to something wider. */
2044 if (code == ENUMERAL_TYPE)
2046 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2047 TYPE_PRECISION (integer_type_node)),
2048 ((TYPE_PRECISION (type)
2049 >= TYPE_PRECISION (integer_type_node))
2050 && TYPE_UNSIGNED (type)));
2052 return convert (type, exp);
2055 /* ??? This should no longer be needed now bit-fields have their
2057 if (TREE_CODE (exp) == COMPONENT_REF
2058 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2059 /* If it's thinner than an int, promote it like a
2060 c_promoting_integer_type_p, otherwise leave it alone. */
2061 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2062 TYPE_PRECISION (integer_type_node)))
2063 return convert (integer_type_node, exp);
2065 if (c_promoting_integer_type_p (type))
2067 /* Preserve unsignedness if not really getting any wider. */
2068 if (TYPE_UNSIGNED (type)
2069 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2070 return convert (unsigned_type_node, exp);
2072 return convert (integer_type_node, exp);
2079 /* Perform default promotions for C data used in expressions.
2080 Enumeral types or short or char are converted to int.
2081 In addition, manifest constants symbols are replaced by their values. */
2084 default_conversion (tree exp)
2087 tree type = TREE_TYPE (exp);
2088 enum tree_code code = TREE_CODE (type);
2091 mark_exp_read (exp);
2093 /* Functions and arrays have been converted during parsing. */
2094 gcc_assert (code != FUNCTION_TYPE);
2095 if (code == ARRAY_TYPE)
2098 /* Constants can be used directly unless they're not loadable. */
2099 if (TREE_CODE (exp) == CONST_DECL)
2100 exp = DECL_INITIAL (exp);
2102 /* Strip no-op conversions. */
2104 STRIP_TYPE_NOPS (exp);
2106 if (TREE_NO_WARNING (orig_exp))
2107 TREE_NO_WARNING (exp) = 1;
2109 if (code == VOID_TYPE)
2111 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2112 "void value not ignored as it ought to be");
2113 return error_mark_node;
2116 exp = require_complete_type (exp);
2117 if (exp == error_mark_node)
2118 return error_mark_node;
2120 promoted_type = targetm.promoted_type (type);
2122 return convert (promoted_type, exp);
2124 if (INTEGRAL_TYPE_P (type))
2125 return perform_integral_promotions (exp);
2130 /* Look up COMPONENT in a structure or union TYPE.
2132 If the component name is not found, returns NULL_TREE. Otherwise,
2133 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2134 stepping down the chain to the component, which is in the last
2135 TREE_VALUE of the list. Normally the list is of length one, but if
2136 the component is embedded within (nested) anonymous structures or
2137 unions, the list steps down the chain to the component. */
2140 lookup_field (tree type, tree component)
2144 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2145 to the field elements. Use a binary search on this array to quickly
2146 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2147 will always be set for structures which have many elements. */
2149 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2152 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2154 field = TYPE_FIELDS (type);
2156 top = TYPE_LANG_SPECIFIC (type)->s->len;
2157 while (top - bot > 1)
2159 half = (top - bot + 1) >> 1;
2160 field = field_array[bot+half];
2162 if (DECL_NAME (field) == NULL_TREE)
2164 /* Step through all anon unions in linear fashion. */
2165 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2167 field = field_array[bot++];
2168 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2169 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2171 tree anon = lookup_field (TREE_TYPE (field), component);
2174 return tree_cons (NULL_TREE, field, anon);
2176 /* The Plan 9 compiler permits referring
2177 directly to an anonymous struct/union field
2178 using a typedef name. */
2179 if (flag_plan9_extensions
2180 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2181 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2183 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2189 /* Entire record is only anon unions. */
2193 /* Restart the binary search, with new lower bound. */
2197 if (DECL_NAME (field) == component)
2199 if (DECL_NAME (field) < component)
2205 if (DECL_NAME (field_array[bot]) == component)
2206 field = field_array[bot];
2207 else if (DECL_NAME (field) != component)
2212 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2214 if (DECL_NAME (field) == NULL_TREE
2215 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2216 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2218 tree anon = lookup_field (TREE_TYPE (field), component);
2221 return tree_cons (NULL_TREE, field, anon);
2223 /* The Plan 9 compiler permits referring directly to an
2224 anonymous struct/union field using a typedef
2226 if (flag_plan9_extensions
2227 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2228 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2229 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2234 if (DECL_NAME (field) == component)
2238 if (field == NULL_TREE)
2242 return tree_cons (NULL_TREE, field, NULL_TREE);
2245 /* Make an expression to refer to the COMPONENT field of structure or
2246 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2247 location of the COMPONENT_REF. */
2250 build_component_ref (location_t loc, tree datum, tree component)
2252 tree type = TREE_TYPE (datum);
2253 enum tree_code code = TREE_CODE (type);
2256 bool datum_lvalue = lvalue_p (datum);
2258 if (!objc_is_public (datum, component))
2259 return error_mark_node;
2261 /* Detect Objective-C property syntax object.property. */
2262 if (c_dialect_objc ()
2263 && (ref = objc_maybe_build_component_ref (datum, component)))
2266 /* See if there is a field or component with name COMPONENT. */
2268 if (code == RECORD_TYPE || code == UNION_TYPE)
2270 if (!COMPLETE_TYPE_P (type))
2272 c_incomplete_type_error (NULL_TREE, type);
2273 return error_mark_node;
2276 field = lookup_field (type, component);
2280 error_at (loc, "%qT has no member named %qE", type, component);
2281 return error_mark_node;
2284 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2285 This might be better solved in future the way the C++ front
2286 end does it - by giving the anonymous entities each a
2287 separate name and type, and then have build_component_ref
2288 recursively call itself. We can't do that here. */
2291 tree subdatum = TREE_VALUE (field);
2294 bool use_datum_quals;
2296 if (TREE_TYPE (subdatum) == error_mark_node)
2297 return error_mark_node;
2299 /* If this is an rvalue, it does not have qualifiers in C
2300 standard terms and we must avoid propagating such
2301 qualifiers down to a non-lvalue array that is then
2302 converted to a pointer. */
2303 use_datum_quals = (datum_lvalue
2304 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2306 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2307 if (use_datum_quals)
2308 quals |= TYPE_QUALS (TREE_TYPE (datum));
2309 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2311 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2313 SET_EXPR_LOCATION (ref, loc);
2314 if (TREE_READONLY (subdatum)
2315 || (use_datum_quals && TREE_READONLY (datum)))
2316 TREE_READONLY (ref) = 1;
2317 if (TREE_THIS_VOLATILE (subdatum)
2318 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2319 TREE_THIS_VOLATILE (ref) = 1;
2321 if (TREE_DEPRECATED (subdatum))
2322 warn_deprecated_use (subdatum, NULL_TREE);
2326 field = TREE_CHAIN (field);
2332 else if (code != ERROR_MARK)
2334 "request for member %qE in something not a structure or union",
2337 return error_mark_node;
2340 /* Given an expression PTR for a pointer, return an expression
2341 for the value pointed to.
2342 ERRORSTRING is the name of the operator to appear in error messages.
2344 LOC is the location to use for the generated tree. */
2347 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2349 tree pointer = default_conversion (ptr);
2350 tree type = TREE_TYPE (pointer);
2353 if (TREE_CODE (type) == POINTER_TYPE)
2355 if (CONVERT_EXPR_P (pointer)
2356 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2358 /* If a warning is issued, mark it to avoid duplicates from
2359 the backend. This only needs to be done at
2360 warn_strict_aliasing > 2. */
2361 if (warn_strict_aliasing > 2)
2362 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2363 type, TREE_OPERAND (pointer, 0)))
2364 TREE_NO_WARNING (pointer) = 1;
2367 if (TREE_CODE (pointer) == ADDR_EXPR
2368 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2369 == TREE_TYPE (type)))
2371 ref = TREE_OPERAND (pointer, 0);
2372 protected_set_expr_location (ref, loc);
2377 tree t = TREE_TYPE (type);
2379 ref = build1 (INDIRECT_REF, t, pointer);
2381 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2383 error_at (loc, "dereferencing pointer to incomplete type");
2384 return error_mark_node;
2386 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2387 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2389 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2390 so that we get the proper error message if the result is used
2391 to assign to. Also, &* is supposed to be a no-op.
2392 And ANSI C seems to specify that the type of the result
2393 should be the const type. */
2394 /* A de-reference of a pointer to const is not a const. It is valid
2395 to change it via some other pointer. */
2396 TREE_READONLY (ref) = TYPE_READONLY (t);
2397 TREE_SIDE_EFFECTS (ref)
2398 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2399 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2400 protected_set_expr_location (ref, loc);
2404 else if (TREE_CODE (pointer) != ERROR_MARK)
2405 invalid_indirection_error (loc, type, errstring);
2407 return error_mark_node;
2410 /* This handles expressions of the form "a[i]", which denotes
2413 This is logically equivalent in C to *(a+i), but we may do it differently.
2414 If A is a variable or a member, we generate a primitive ARRAY_REF.
2415 This avoids forcing the array out of registers, and can work on
2416 arrays that are not lvalues (for example, members of structures returned
2419 For vector types, allow vector[i] but not i[vector], and create
2420 *(((type*)&vectortype) + i) for the expression.
2422 LOC is the location to use for the returned expression. */
2425 build_array_ref (location_t loc, tree array, tree index)
2428 bool swapped = false;
2429 if (TREE_TYPE (array) == error_mark_node
2430 || TREE_TYPE (index) == error_mark_node)
2431 return error_mark_node;
2433 if (flag_cilkplus && contains_array_notation_expr (index))
2436 if (!find_rank (loc, index, index, true, &rank))
2437 return error_mark_node;
2440 error_at (loc, "rank of the array's index is greater than 1");
2441 return error_mark_node;
2444 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2445 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2446 /* Allow vector[index] but not index[vector]. */
2447 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2450 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2451 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2454 "subscripted value is neither array nor pointer nor vector");
2456 return error_mark_node;
2464 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2466 error_at (loc, "array subscript is not an integer");
2467 return error_mark_node;
2470 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2472 error_at (loc, "subscripted value is pointer to function");
2473 return error_mark_node;
2476 /* ??? Existing practice has been to warn only when the char
2477 index is syntactically the index, not for char[array]. */
2479 warn_array_subscript_with_type_char (index);
2481 /* Apply default promotions *after* noticing character types. */
2482 index = default_conversion (index);
2484 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2486 convert_vector_to_pointer_for_subscript (loc, &array, index);
2488 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2492 /* An array that is indexed by a non-constant
2493 cannot be stored in a register; we must be able to do
2494 address arithmetic on its address.
2495 Likewise an array of elements of variable size. */
2496 if (TREE_CODE (index) != INTEGER_CST
2497 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2498 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2500 if (!c_mark_addressable (array))
2501 return error_mark_node;
2503 /* An array that is indexed by a constant value which is not within
2504 the array bounds cannot be stored in a register either; because we
2505 would get a crash in store_bit_field/extract_bit_field when trying
2506 to access a non-existent part of the register. */
2507 if (TREE_CODE (index) == INTEGER_CST
2508 && TYPE_DOMAIN (TREE_TYPE (array))
2509 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2511 if (!c_mark_addressable (array))
2512 return error_mark_node;
2518 while (TREE_CODE (foo) == COMPONENT_REF)
2519 foo = TREE_OPERAND (foo, 0);
2520 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2521 pedwarn (loc, OPT_Wpedantic,
2522 "ISO C forbids subscripting %<register%> array");
2523 else if (!flag_isoc99 && !lvalue_p (foo))
2524 pedwarn (loc, OPT_Wpedantic,
2525 "ISO C90 forbids subscripting non-lvalue array");
2528 type = TREE_TYPE (TREE_TYPE (array));
2529 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2530 /* Array ref is const/volatile if the array elements are
2531 or if the array is. */
2532 TREE_READONLY (rval)
2533 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2534 | TREE_READONLY (array));
2535 TREE_SIDE_EFFECTS (rval)
2536 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2537 | TREE_SIDE_EFFECTS (array));
2538 TREE_THIS_VOLATILE (rval)
2539 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2540 /* This was added by rms on 16 Nov 91.
2541 It fixes vol struct foo *a; a->elts[1]
2542 in an inline function.
2543 Hope it doesn't break something else. */
2544 | TREE_THIS_VOLATILE (array));
2545 ret = require_complete_type (rval);
2546 protected_set_expr_location (ret, loc);
2551 tree ar = default_conversion (array);
2553 if (ar == error_mark_node)
2556 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2557 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2559 return build_indirect_ref
2560 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2565 /* Build an external reference to identifier ID. FUN indicates
2566 whether this will be used for a function call. LOC is the source
2567 location of the identifier. This sets *TYPE to the type of the
2568 identifier, which is not the same as the type of the returned value
2569 for CONST_DECLs defined as enum constants. If the type of the
2570 identifier is not available, *TYPE is set to NULL. */
2572 build_external_ref (location_t loc, tree id, int fun, tree *type)
2575 tree decl = lookup_name (id);
2577 /* In Objective-C, an instance variable (ivar) may be preferred to
2578 whatever lookup_name() found. */
2579 decl = objc_lookup_ivar (decl, id);
2582 if (decl && decl != error_mark_node)
2585 *type = TREE_TYPE (ref);
2588 /* Implicit function declaration. */
2589 ref = implicitly_declare (loc, id);
2590 else if (decl == error_mark_node)
2591 /* Don't complain about something that's already been
2592 complained about. */
2593 return error_mark_node;
2596 undeclared_variable (loc, id);
2597 return error_mark_node;
2600 if (TREE_TYPE (ref) == error_mark_node)
2601 return error_mark_node;
2603 if (TREE_DEPRECATED (ref))
2604 warn_deprecated_use (ref, NULL_TREE);
2606 /* Recursive call does not count as usage. */
2607 if (ref != current_function_decl)
2609 TREE_USED (ref) = 1;
2612 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2614 if (!in_sizeof && !in_typeof)
2615 C_DECL_USED (ref) = 1;
2616 else if (DECL_INITIAL (ref) == 0
2617 && DECL_EXTERNAL (ref)
2618 && !TREE_PUBLIC (ref))
2619 record_maybe_used_decl (ref);
2622 if (TREE_CODE (ref) == CONST_DECL)
2624 used_types_insert (TREE_TYPE (ref));
2627 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2628 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2630 warning_at (loc, OPT_Wc___compat,
2631 ("enum constant defined in struct or union "
2632 "is not visible in C++"));
2633 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2636 ref = DECL_INITIAL (ref);
2637 TREE_CONSTANT (ref) = 1;
2639 else if (current_function_decl != 0
2640 && !DECL_FILE_SCOPE_P (current_function_decl)
2641 && (TREE_CODE (ref) == VAR_DECL
2642 || TREE_CODE (ref) == PARM_DECL
2643 || TREE_CODE (ref) == FUNCTION_DECL))
2645 tree context = decl_function_context (ref);
2647 if (context != 0 && context != current_function_decl)
2648 DECL_NONLOCAL (ref) = 1;
2650 /* C99 6.7.4p3: An inline definition of a function with external
2651 linkage ... shall not contain a reference to an identifier with
2652 internal linkage. */
2653 else if (current_function_decl != 0
2654 && DECL_DECLARED_INLINE_P (current_function_decl)
2655 && DECL_EXTERNAL (current_function_decl)
2656 && VAR_OR_FUNCTION_DECL_P (ref)
2657 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2658 && ! TREE_PUBLIC (ref)
2659 && DECL_CONTEXT (ref) != current_function_decl)
2660 record_inline_static (loc, current_function_decl, ref,
2666 /* Record details of decls possibly used inside sizeof or typeof. */
2667 struct maybe_used_decl
2671 /* The level seen at (in_sizeof + in_typeof). */
2673 /* The next one at this level or above, or NULL. */
2674 struct maybe_used_decl *next;
2677 static struct maybe_used_decl *maybe_used_decls;
2679 /* Record that DECL, an undefined static function reference seen
2680 inside sizeof or typeof, might be used if the operand of sizeof is
2681 a VLA type or the operand of typeof is a variably modified
2685 record_maybe_used_decl (tree decl)
2687 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2689 t->level = in_sizeof + in_typeof;
2690 t->next = maybe_used_decls;
2691 maybe_used_decls = t;
2694 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2695 USED is false, just discard them. If it is true, mark them used
2696 (if no longer inside sizeof or typeof) or move them to the next
2697 level up (if still inside sizeof or typeof). */
2700 pop_maybe_used (bool used)
2702 struct maybe_used_decl *p = maybe_used_decls;
2703 int cur_level = in_sizeof + in_typeof;
2704 while (p && p->level > cur_level)
2709 C_DECL_USED (p->decl) = 1;
2711 p->level = cur_level;
2715 if (!used || cur_level == 0)
2716 maybe_used_decls = p;
2719 /* Return the result of sizeof applied to EXPR. */
2722 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2725 if (expr.value == error_mark_node)
2727 ret.value = error_mark_node;
2728 ret.original_code = ERROR_MARK;
2729 ret.original_type = NULL;
2730 pop_maybe_used (false);
2734 bool expr_const_operands = true;
2735 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2736 &expr_const_operands);
2737 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2738 c_last_sizeof_arg = expr.value;
2739 ret.original_code = SIZEOF_EXPR;
2740 ret.original_type = NULL;
2741 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2743 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2744 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2745 folded_expr, ret.value);
2746 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2747 SET_EXPR_LOCATION (ret.value, loc);
2749 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2754 /* Return the result of sizeof applied to T, a structure for the type
2755 name passed to sizeof (rather than the type itself). LOC is the
2756 location of the original expression. */
2759 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2763 tree type_expr = NULL_TREE;
2764 bool type_expr_const = true;
2765 type = groktypename (t, &type_expr, &type_expr_const);
2766 ret.value = c_sizeof (loc, type);
2767 c_last_sizeof_arg = type;
2768 ret.original_code = SIZEOF_EXPR;
2769 ret.original_type = NULL;
2770 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2771 && c_vla_type_p (type))
2773 /* If the type is a [*] array, it is a VLA but is represented as
2774 having a size of zero. In such a case we must ensure that
2775 the result of sizeof does not get folded to a constant by
2776 c_fully_fold, because if the size is evaluated the result is
2777 not constant and so constraints on zero or negative size
2778 arrays must not be applied when this sizeof call is inside
2779 another array declarator. */
2781 type_expr = integer_zero_node;
2782 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2783 type_expr, ret.value);
2784 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2786 pop_maybe_used (type != error_mark_node
2787 ? C_TYPE_VARIABLE_SIZE (type) : false);
2791 /* Build a function call to function FUNCTION with parameters PARAMS.
2792 The function call is at LOC.
2793 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2794 TREE_VALUE of each node is a parameter-expression.
2795 FUNCTION's data type may be a function type or a pointer-to-function. */
2798 build_function_call (location_t loc, tree function, tree params)
2800 vec<tree, va_gc> *v;
2803 vec_alloc (v, list_length (params));
2804 for (; params; params = TREE_CHAIN (params))
2805 v->quick_push (TREE_VALUE (params));
2806 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2811 /* Give a note about the location of the declaration of DECL. */
2813 static void inform_declaration (tree decl)
2815 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2816 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2819 /* Build a function call to function FUNCTION with parameters PARAMS.
2820 ORIGTYPES, if not NULL, is a vector of types; each element is
2821 either NULL or the original type of the corresponding element in
2822 PARAMS. The original type may differ from TREE_TYPE of the
2823 parameter for enums. FUNCTION's data type may be a function type
2824 or pointer-to-function. This function changes the elements of
2828 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2829 tree function, vec<tree, va_gc> *params,
2830 vec<tree, va_gc> *origtypes)
2832 tree fntype, fundecl = 0;
2833 tree name = NULL_TREE, result;
2839 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2840 STRIP_TYPE_NOPS (function);
2842 /* Convert anything with function type to a pointer-to-function. */
2843 if (TREE_CODE (function) == FUNCTION_DECL)
2845 name = DECL_NAME (function);
2848 tm_malloc_replacement (function);
2850 /* Atomic functions have type checking/casting already done. They are
2851 often rewritten and don't match the original parameter list. */
2852 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2856 && is_cilkplus_reduce_builtin (function))
2859 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2860 function = function_to_pointer_conversion (loc, function);
2862 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2863 expressions, like those used for ObjC messenger dispatches. */
2864 if (params && !params->is_empty ())
2865 function = objc_rewrite_function_call (function, (*params)[0]);
2867 function = c_fully_fold (function, false, NULL);
2869 fntype = TREE_TYPE (function);
2871 if (TREE_CODE (fntype) == ERROR_MARK)
2872 return error_mark_node;
2874 if (!(TREE_CODE (fntype) == POINTER_TYPE
2875 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2877 if (!flag_diagnostics_show_caret)
2879 "called object %qE is not a function or function pointer",
2881 else if (DECL_P (function))
2884 "called object %qD is not a function or function pointer",
2886 inform_declaration (function);
2890 "called object is not a function or function pointer");
2891 return error_mark_node;
2894 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2895 current_function_returns_abnormally = 1;
2897 /* fntype now gets the type of function pointed to. */
2898 fntype = TREE_TYPE (fntype);
2900 /* Convert the parameters to the types declared in the
2901 function prototype, or apply default promotions. */
2903 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2904 origtypes, function, fundecl);
2906 return error_mark_node;
2908 /* Check that the function is called through a compatible prototype.
2909 If it is not, warn. */
2910 if (CONVERT_EXPR_P (function)
2911 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2912 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2913 && !comptypes (fntype, TREE_TYPE (tem)))
2915 tree return_type = TREE_TYPE (fntype);
2917 /* This situation leads to run-time undefined behavior. We can't,
2918 therefore, simply error unless we can prove that all possible
2919 executions of the program must execute the code. */
2920 warning_at (loc, 0, "function called through a non-compatible type");
2922 if (VOID_TYPE_P (return_type)
2923 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2925 "function with qualified void return type called");
2928 argarray = vec_safe_address (params);
2930 /* Check that arguments to builtin functions match the expectations. */
2932 && DECL_BUILT_IN (fundecl)
2933 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2934 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2935 return error_mark_node;
2937 /* Check that the arguments to the function are valid. */
2938 check_function_arguments (fntype, nargs, argarray);
2940 if (name != NULL_TREE
2941 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2943 if (require_constant_value)
2945 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2946 function, nargs, argarray);
2948 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2949 function, nargs, argarray);
2950 if (TREE_CODE (result) == NOP_EXPR
2951 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2952 STRIP_TYPE_NOPS (result);
2955 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2956 function, nargs, argarray);
2958 if (VOID_TYPE_P (TREE_TYPE (result)))
2960 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2962 "function with qualified void return type called");
2965 return require_complete_type (result);
2968 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2971 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2972 tree function, vec<tree, va_gc> *params,
2973 vec<tree, va_gc> *origtypes)
2975 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2976 STRIP_TYPE_NOPS (function);
2978 /* Convert anything with function type to a pointer-to-function. */
2979 if (TREE_CODE (function) == FUNCTION_DECL)
2981 /* Implement type-directed function overloading for builtins.
2982 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2983 handle all the type checking. The result is a complete expression
2984 that implements this function call. */
2985 tree tem = resolve_overloaded_builtin (loc, function, params);
2989 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
2992 /* Convert the argument expressions in the vector VALUES
2993 to the types in the list TYPELIST.
2995 If TYPELIST is exhausted, or when an element has NULL as its type,
2996 perform the default conversions.
2998 ORIGTYPES is the original types of the expressions in VALUES. This
2999 holds the type of enum values which have been converted to integral
3000 types. It may be NULL.
3002 FUNCTION is a tree for the called function. It is used only for
3003 error messages, where it is formatted with %qE.
3005 This is also where warnings about wrong number of args are generated.
3007 ARG_LOC are locations of function arguments (if any).
3009 Returns the actual number of arguments processed (which may be less
3010 than the length of VALUES in some error situations), or -1 on
3014 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3015 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3016 tree function, tree fundecl)
3019 unsigned int parmnum;
3020 bool error_args = false;
3021 const bool type_generic = fundecl
3022 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3023 bool type_generic_remove_excess_precision = false;
3026 /* Change pointer to function to the function itself for
3028 if (TREE_CODE (function) == ADDR_EXPR
3029 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3030 function = TREE_OPERAND (function, 0);
3032 /* Handle an ObjC selector specially for diagnostics. */
3033 selector = objc_message_selector ();
3035 /* For type-generic built-in functions, determine whether excess
3036 precision should be removed (classification) or not
3039 && DECL_BUILT_IN (fundecl)
3040 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3042 switch (DECL_FUNCTION_CODE (fundecl))
3044 case BUILT_IN_ISFINITE:
3045 case BUILT_IN_ISINF:
3046 case BUILT_IN_ISINF_SIGN:
3047 case BUILT_IN_ISNAN:
3048 case BUILT_IN_ISNORMAL:
3049 case BUILT_IN_FPCLASSIFY:
3050 type_generic_remove_excess_precision = true;
3054 type_generic_remove_excess_precision = false;
3058 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3059 return vec_safe_length (values);
3061 /* Scan the given expressions and types, producing individual
3062 converted arguments. */
3064 for (typetail = typelist, parmnum = 0;
3065 values && values->iterate (parmnum, &val);
3068 tree type = typetail ? TREE_VALUE (typetail) : 0;
3069 tree valtype = TREE_TYPE (val);
3070 tree rname = function;
3071 int argnum = parmnum + 1;
3072 const char *invalid_func_diag;
3073 bool excess_precision = false;
3077 if (type == void_type_node)
3080 error_at (loc, "too many arguments to method %qE", selector);
3082 error_at (loc, "too many arguments to function %qE", function);
3083 inform_declaration (fundecl);
3087 if (selector && argnum > 2)
3093 npc = null_pointer_constant_p (val);
3095 /* If there is excess precision and a prototype, convert once to
3096 the required type rather than converting via the semantic
3097 type. Likewise without a prototype a float value represented
3098 as long double should be converted once to double. But for
3099 type-generic classification functions excess precision must
3101 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3102 && (type || !type_generic || !type_generic_remove_excess_precision))
3104 val = TREE_OPERAND (val, 0);
3105 excess_precision = true;
3107 val = c_fully_fold (val, false, NULL);
3108 STRIP_TYPE_NOPS (val);
3110 val = require_complete_type (val);
3114 /* Formal parm type is specified by a function prototype. */
3116 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3118 error ("type of formal parameter %d is incomplete", parmnum + 1);
3125 /* Optionally warn about conversions that
3126 differ from the default conversions. */
3127 if (warn_traditional_conversion || warn_traditional)
3129 unsigned int formal_prec = TYPE_PRECISION (type);
3131 if (INTEGRAL_TYPE_P (type)
3132 && TREE_CODE (valtype) == REAL_TYPE)
3133 warning (0, "passing argument %d of %qE as integer "
3134 "rather than floating due to prototype",
3136 if (INTEGRAL_TYPE_P (type)
3137 && TREE_CODE (valtype) == COMPLEX_TYPE)
3138 warning (0, "passing argument %d of %qE as integer "
3139 "rather than complex due to prototype",
3141 else if (TREE_CODE (type) == COMPLEX_TYPE
3142 && TREE_CODE (valtype) == REAL_TYPE)
3143 warning (0, "passing argument %d of %qE as complex "
3144 "rather than floating due to prototype",
3146 else if (TREE_CODE (type) == REAL_TYPE
3147 && INTEGRAL_TYPE_P (valtype))
3148 warning (0, "passing argument %d of %qE as floating "
3149 "rather than integer due to prototype",
3151 else if (TREE_CODE (type) == COMPLEX_TYPE
3152 && INTEGRAL_TYPE_P (valtype))
3153 warning (0, "passing argument %d of %qE as complex "
3154 "rather than integer due to prototype",
3156 else if (TREE_CODE (type) == REAL_TYPE
3157 && TREE_CODE (valtype) == COMPLEX_TYPE)
3158 warning (0, "passing argument %d of %qE as floating "
3159 "rather than complex due to prototype",
3161 /* ??? At some point, messages should be written about
3162 conversions between complex types, but that's too messy
3164 else if (TREE_CODE (type) == REAL_TYPE
3165 && TREE_CODE (valtype) == REAL_TYPE)
3167 /* Warn if any argument is passed as `float',
3168 since without a prototype it would be `double'. */
3169 if (formal_prec == TYPE_PRECISION (float_type_node)
3170 && type != dfloat32_type_node)
3171 warning (0, "passing argument %d of %qE as %<float%> "
3172 "rather than %<double%> due to prototype",
3175 /* Warn if mismatch between argument and prototype
3176 for decimal float types. Warn of conversions with
3177 binary float types and of precision narrowing due to
3179 else if (type != valtype
3180 && (type == dfloat32_type_node
3181 || type == dfloat64_type_node
3182 || type == dfloat128_type_node
3183 || valtype == dfloat32_type_node
3184 || valtype == dfloat64_type_node
3185 || valtype == dfloat128_type_node)
3187 <= TYPE_PRECISION (valtype)
3188 || (type == dfloat128_type_node
3190 != dfloat64_type_node
3192 != dfloat32_type_node)))
3193 || (type == dfloat64_type_node
3195 != dfloat32_type_node))))
3196 warning (0, "passing argument %d of %qE as %qT "
3197 "rather than %qT due to prototype",
3198 argnum, rname, type, valtype);
3201 /* Detect integer changing in width or signedness.
3202 These warnings are only activated with
3203 -Wtraditional-conversion, not with -Wtraditional. */
3204 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3205 && INTEGRAL_TYPE_P (valtype))
3207 tree would_have_been = default_conversion (val);
3208 tree type1 = TREE_TYPE (would_have_been);
3210 if (TREE_CODE (type) == ENUMERAL_TYPE
3211 && (TYPE_MAIN_VARIANT (type)
3212 == TYPE_MAIN_VARIANT (valtype)))
3213 /* No warning if function asks for enum
3214 and the actual arg is that enum type. */
3216 else if (formal_prec != TYPE_PRECISION (type1))
3217 warning (OPT_Wtraditional_conversion,
3218 "passing argument %d of %qE "
3219 "with different width due to prototype",
3221 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3223 /* Don't complain if the formal parameter type
3224 is an enum, because we can't tell now whether
3225 the value was an enum--even the same enum. */
3226 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3228 else if (TREE_CODE (val) == INTEGER_CST
3229 && int_fits_type_p (val, type))
3230 /* Change in signedness doesn't matter
3231 if a constant value is unaffected. */
3233 /* If the value is extended from a narrower
3234 unsigned type, it doesn't matter whether we
3235 pass it as signed or unsigned; the value
3236 certainly is the same either way. */
3237 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3238 && TYPE_UNSIGNED (valtype))
3240 else if (TYPE_UNSIGNED (type))
3241 warning (OPT_Wtraditional_conversion,
3242 "passing argument %d of %qE "
3243 "as unsigned due to prototype",
3246 warning (OPT_Wtraditional_conversion,
3247 "passing argument %d of %qE "
3248 "as signed due to prototype", argnum, rname);
3252 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3253 sake of better warnings from convert_and_check. */
3254 if (excess_precision)
3255 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3256 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3257 bool arg_loc_ok = !arg_loc.is_empty ()
3258 /* Some __atomic_* builtins have additional
3259 hidden argument at position 0. */
3260 && values->length () == arg_loc.length ();
3261 parmval = convert_for_assignment (loc,
3262 arg_loc_ok ? arg_loc[parmnum]
3263 : UNKNOWN_LOCATION, type,
3264 val, origtype, ic_argpass,
3265 npc, fundecl, function,
3268 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3269 && INTEGRAL_TYPE_P (type)
3270 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3271 parmval = default_conversion (parmval);
3274 else if (TREE_CODE (valtype) == REAL_TYPE
3275 && (TYPE_PRECISION (valtype)
3276 <= TYPE_PRECISION (double_type_node))
3277 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3278 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3279 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3285 /* Convert `float' to `double'. */
3286 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3287 warning_at (arg_loc[parmnum], OPT_Wdouble_promotion,
3288 "implicit conversion from %qT to %qT when passing "
3289 "argument to function",
3290 valtype, double_type_node);
3291 parmval = convert (double_type_node, val);
3294 else if (excess_precision && !type_generic)
3295 /* A "double" argument with excess precision being passed
3296 without a prototype or in variable arguments. */
3297 parmval = convert (valtype, val);
3298 else if ((invalid_func_diag =
3299 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3301 error (invalid_func_diag);
3305 /* Convert `short' and `char' to full-size `int'. */
3306 parmval = default_conversion (val);
3308 (*values)[parmnum] = parmval;
3309 if (parmval == error_mark_node)
3313 typetail = TREE_CHAIN (typetail);
3316 gcc_assert (parmnum == vec_safe_length (values));
3318 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3320 error_at (loc, "too few arguments to function %qE", function);
3321 inform_declaration (fundecl);
3325 return error_args ? -1 : (int) parmnum;
3328 /* This is the entry point used by the parser to build unary operators
3329 in the input. CODE, a tree_code, specifies the unary operator, and
3330 ARG is the operand. For unary plus, the C parser currently uses
3331 CONVERT_EXPR for code.
3333 LOC is the location to use for the tree generated.
3337 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3339 struct c_expr result;
3341 result.value = build_unary_op (loc, code, arg.value, 0);
3342 result.original_code = code;
3343 result.original_type = NULL;
3345 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3346 overflow_warning (loc, result.value);
3351 /* This is the entry point used by the parser to build binary operators
3352 in the input. CODE, a tree_code, specifies the binary operator, and
3353 ARG1 and ARG2 are the operands. In addition to constructing the
3354 expression, we check for operands that were written with other binary
3355 operators in a way that is likely to confuse the user.
3357 LOCATION is the location of the binary operator. */
3360 parser_build_binary_op (location_t location, enum tree_code code,
3361 struct c_expr arg1, struct c_expr arg2)
3363 struct c_expr result;
3365 enum tree_code code1 = arg1.original_code;
3366 enum tree_code code2 = arg2.original_code;
3367 tree type1 = (arg1.original_type
3368 ? arg1.original_type
3369 : TREE_TYPE (arg1.value));
3370 tree type2 = (arg2.original_type
3371 ? arg2.original_type
3372 : TREE_TYPE (arg2.value));
3374 result.value = build_binary_op (location, code,
3375 arg1.value, arg2.value, 1);
3376 result.original_code = code;
3377 result.original_type = NULL;
3379 if (TREE_CODE (result.value) == ERROR_MARK)
3382 if (location != UNKNOWN_LOCATION)
3383 protected_set_expr_location (result.value, location);
3385 /* Check for cases such as x+y<<z which users are likely
3387 if (warn_parentheses)
3388 warn_about_parentheses (location, code, code1, arg1.value, code2,
3391 if (warn_logical_op)
3392 warn_logical_operator (location, code, TREE_TYPE (result.value),
3393 code1, arg1.value, code2, arg2.value);
3395 /* Warn about comparisons against string literals, with the exception
3396 of testing for equality or inequality of a string literal with NULL. */
3397 if (code == EQ_EXPR || code == NE_EXPR)
3399 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3400 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3401 warning_at (location, OPT_Waddress,
3402 "comparison with string literal results in unspecified behavior");
3404 else if (TREE_CODE_CLASS (code) == tcc_comparison
3405 && (code1 == STRING_CST || code2 == STRING_CST))
3406 warning_at (location, OPT_Waddress,
3407 "comparison with string literal results in unspecified behavior");
3409 if (TREE_OVERFLOW_P (result.value)
3410 && !TREE_OVERFLOW_P (arg1.value)
3411 && !TREE_OVERFLOW_P (arg2.value))
3412 overflow_warning (location, result.value);
3414 /* Warn about comparisons of different enum types. */
3415 if (warn_enum_compare
3416 && TREE_CODE_CLASS (code) == tcc_comparison
3417 && TREE_CODE (type1) == ENUMERAL_TYPE
3418 && TREE_CODE (type2) == ENUMERAL_TYPE
3419 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3420 warning_at (location, OPT_Wenum_compare,
3421 "comparison between %qT and %qT",
3427 /* Return a tree for the difference of pointers OP0 and OP1.
3428 The resulting tree has type int. */
3431 pointer_diff (location_t loc, tree op0, tree op1)
3433 tree restype = ptrdiff_type_node;
3434 tree result, inttype;
3436 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3437 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3438 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3439 tree con0, con1, lit0, lit1;
3440 tree orig_op1 = op1;
3442 /* If the operands point into different address spaces, we need to
3443 explicitly convert them to pointers into the common address space
3444 before we can subtract the numerical address values. */
3447 addr_space_t as_common;
3450 /* Determine the common superset address space. This is guaranteed
3451 to exist because the caller verified that comp_target_types
3452 returned non-zero. */
3453 if (!addr_space_superset (as0, as1, &as_common))
3456 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3457 op0 = convert (common_type, op0);
3458 op1 = convert (common_type, op1);
3461 /* Determine integer type to perform computations in. This will usually
3462 be the same as the result type (ptrdiff_t), but may need to be a wider
3463 type if pointers for the address space are wider than ptrdiff_t. */
3464 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3465 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3470 if (TREE_CODE (target_type) == VOID_TYPE)
3471 pedwarn (loc, OPT_Wpointer_arith,
3472 "pointer of type %<void *%> used in subtraction");
3473 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3474 pedwarn (loc, OPT_Wpointer_arith,
3475 "pointer to a function used in subtraction");
3477 /* If the conversion to ptrdiff_type does anything like widening or
3478 converting a partial to an integral mode, we get a convert_expression
3479 that is in the way to do any simplifications.
3480 (fold-const.c doesn't know that the extra bits won't be needed.
3481 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3482 different mode in place.)
3483 So first try to find a common term here 'by hand'; we want to cover
3484 at least the cases that occur in legal static initializers. */
3485 if (CONVERT_EXPR_P (op0)
3486 && (TYPE_PRECISION (TREE_TYPE (op0))
3487 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3488 con0 = TREE_OPERAND (op0, 0);
3491 if (CONVERT_EXPR_P (op1)
3492 && (TYPE_PRECISION (TREE_TYPE (op1))
3493 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3494 con1 = TREE_OPERAND (op1, 0);
3498 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3500 lit0 = TREE_OPERAND (con0, 1);
3501 con0 = TREE_OPERAND (con0, 0);
3504 lit0 = integer_zero_node;
3506 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
3508 lit1 = TREE_OPERAND (con1, 1);
3509 con1 = TREE_OPERAND (con1, 0);
3512 lit1 = integer_zero_node;
3514 if (operand_equal_p (con0, con1, 0))
3521 /* First do the subtraction as integers;
3522 then drop through to build the divide operator.
3523 Do not do default conversions on the minus operator
3524 in case restype is a short type. */
3526 op0 = build_binary_op (loc,
3527 MINUS_EXPR, convert (inttype, op0),
3528 convert (inttype, op1), 0);
3529 /* This generates an error if op1 is pointer to incomplete type. */
3530 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3531 error_at (loc, "arithmetic on pointer to an incomplete type");
3533 /* This generates an error if op0 is pointer to incomplete type. */
3534 op1 = c_size_in_bytes (target_type);
3536 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3537 error_at (loc, "arithmetic on pointer to an empty aggregate");
3539 /* Divide by the size, in easiest possible way. */
3540 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3541 op0, convert (inttype, op1));
3543 /* Convert to final result type if necessary. */
3544 return convert (restype, result);
3547 /* Expand atomic compound assignments into an approriate sequence as
3548 specified by the C11 standard section 6.5.16.2.
3554 This sequence is used for all types for which these operations are
3557 In addition, built-in versions of the 'fe' prefixed routines may
3558 need to be invoked for floating point (real, complex or vector) when
3559 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3569 __atomic_load (addr, &old, SEQ_CST);
3570 feholdexcept (&fenv);
3572 newval = old op val;
3573 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3576 feclearexcept (FE_ALL_EXCEPT);
3579 feupdateenv (&fenv);
3581 Also note that the compiler is simply issuing the generic form of
3582 the atomic operations. This requires temp(s) and has their address
3583 taken. The atomic processing is smart enough to figure out when the
3584 size of an object can utilize a lock-free version, and convert the
3585 built-in call to the appropriate lock-free routine. The optimizers
3586 will then dispose of any temps that are no longer required, and
3587 lock-free implementations are utilized as long as there is target
3588 support for the required size.
3590 If the operator is NOP_EXPR, then this is a simple assignment, and
3591 an __atomic_store is issued to perform the assignment rather than
3596 /* Build an atomic assignment at LOC, expanding into the proper
3597 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3598 the result of the operation, unless RETURN_OLD_P in which case
3599 return the old value of LHS (this is only for postincrement and
3602 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3603 tree rhs, bool return_old_p)
3605 tree fndecl, func_call;
3606 vec<tree, va_gc> *params;
3607 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3610 tree stmt, goto_stmt;
3611 tree loop_label, loop_decl, done_label, done_decl;
3613 tree lhs_type = TREE_TYPE (lhs);
3614 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3615 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3616 tree rhs_type = TREE_TYPE (rhs);
3618 gcc_assert (TYPE_ATOMIC (lhs_type));
3621 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3623 /* Allocate enough vector items for a compare_exchange. */
3624 vec_alloc (params, 6);
3626 /* Create a compound statement to hold the sequence of statements
3628 compound_stmt = c_begin_compound_stmt (false);
3630 /* Fold the RHS if it hasn't already been folded. */
3631 if (modifycode != NOP_EXPR)
3632 rhs = c_fully_fold (rhs, false, NULL);
3634 /* Remove the qualifiers for the rest of the expressions and create
3635 the VAL temp variable to hold the RHS. */
3636 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3637 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3638 val = create_tmp_var (nonatomic_rhs_type, NULL);
3639 TREE_ADDRESSABLE (val) = 1;
3640 TREE_NO_WARNING (val) = 1;
3641 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3642 SET_EXPR_LOCATION (rhs, loc);
3645 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3647 if (modifycode == NOP_EXPR)
3649 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3650 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3651 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3652 params->quick_push (lhs_addr);
3653 params->quick_push (rhs);
3654 params->quick_push (seq_cst);
3655 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3656 add_stmt (func_call);
3658 /* Finish the compound statement. */
3659 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3661 /* VAL is the value which was stored, return a COMPOUND_STMT of
3662 the statement and that value. */
3663 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3666 /* Create the variables and labels required for the op= form. */
3667 old = create_tmp_var (nonatomic_lhs_type, NULL);
3668 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3669 TREE_ADDRESSABLE (old) = 1;
3670 TREE_NO_WARNING (old) = 1;
3672 newval = create_tmp_var (nonatomic_lhs_type, NULL);
3673 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3674 TREE_ADDRESSABLE (newval) = 1;
3676 loop_decl = create_artificial_label (loc);
3677 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3679 done_decl = create_artificial_label (loc);
3680 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3682 /* __atomic_load (addr, &old, SEQ_CST). */
3683 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3684 params->quick_push (lhs_addr);
3685 params->quick_push (old_addr);
3686 params->quick_push (seq_cst);
3687 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3688 add_stmt (func_call);
3689 params->truncate (0);
3691 /* Create the expressions for floating-point environment
3692 manipulation, if required. */
3693 bool need_fenv = (flag_trapping_math
3694 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3695 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3697 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3700 add_stmt (hold_call);
3703 add_stmt (loop_label);
3705 /* newval = old + val; */
3706 rhs = build_binary_op (loc, modifycode, old, val, 1);
3707 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3708 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3710 if (rhs != error_mark_node)
3712 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3713 SET_EXPR_LOCATION (rhs, loc);
3717 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3719 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3720 params->quick_push (lhs_addr);
3721 params->quick_push (old_addr);
3722 params->quick_push (newval_addr);
3723 params->quick_push (integer_zero_node);
3724 params->quick_push (seq_cst);
3725 params->quick_push (seq_cst);
3726 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3728 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3729 SET_EXPR_LOCATION (goto_stmt, loc);
3731 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3732 SET_EXPR_LOCATION (stmt, loc);
3736 add_stmt (clear_call);
3739 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3740 SET_EXPR_LOCATION (goto_stmt, loc);
3741 add_stmt (goto_stmt);
3744 add_stmt (done_label);
3747 add_stmt (update_call);
3749 /* Finish the compound statement. */
3750 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3752 /* NEWVAL is the value that was successfully stored, return a
3753 COMPOUND_EXPR of the statement and the appropriate value. */
3754 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3755 return_old_p ? old : newval);
3758 /* Construct and perhaps optimize a tree representation
3759 for a unary operation. CODE, a tree_code, specifies the operation
3760 and XARG is the operand.
3761 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3762 the default promotions (such as from short to int).
3763 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3764 allows non-lvalues; this is only used to handle conversion of non-lvalue
3765 arrays to pointers in C99.
3767 LOCATION is the location of the operator. */
3770 build_unary_op (location_t location,
3771 enum tree_code code, tree xarg, int flag)
3773 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3776 enum tree_code typecode;
3778 tree ret = error_mark_node;
3779 tree eptype = NULL_TREE;
3780 int noconvert = flag;
3781 const char *invalid_op_diag;
3784 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3786 arg = remove_c_maybe_const_expr (arg);
3788 if (code != ADDR_EXPR)
3789 arg = require_complete_type (arg);
3791 typecode = TREE_CODE (TREE_TYPE (arg));
3792 if (typecode == ERROR_MARK)
3793 return error_mark_node;
3794 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3795 typecode = INTEGER_TYPE;
3797 if ((invalid_op_diag
3798 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3800 error_at (location, invalid_op_diag);
3801 return error_mark_node;
3804 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3806 eptype = TREE_TYPE (arg);
3807 arg = TREE_OPERAND (arg, 0);
3813 /* This is used for unary plus, because a CONVERT_EXPR
3814 is enough to prevent anybody from looking inside for
3815 associativity, but won't generate any code. */
3816 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3817 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3818 || typecode == VECTOR_TYPE))
3820 error_at (location, "wrong type argument to unary plus");
3821 return error_mark_node;
3823 else if (!noconvert)
3824 arg = default_conversion (arg);
3825 arg = non_lvalue_loc (location, arg);
3829 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3830 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3831 || typecode == VECTOR_TYPE))
3833 error_at (location, "wrong type argument to unary minus");
3834 return error_mark_node;
3836 else if (!noconvert)
3837 arg = default_conversion (arg);
3841 /* ~ works on integer types and non float vectors. */
3842 if (typecode == INTEGER_TYPE
3843 || (typecode == VECTOR_TYPE
3844 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3847 arg = default_conversion (arg);
3849 else if (typecode == COMPLEX_TYPE)
3852 pedwarn (location, OPT_Wpedantic,
3853 "ISO C does not support %<~%> for complex conjugation");
3855 arg = default_conversion (arg);
3859 error_at (location, "wrong type argument to bit-complement");
3860 return error_mark_node;
3865 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3867 error_at (location, "wrong type argument to abs");
3868 return error_mark_node;
3870 else if (!noconvert)
3871 arg = default_conversion (arg);
3875 /* Conjugating a real value is a no-op, but allow it anyway. */
3876 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3877 || typecode == COMPLEX_TYPE))
3879 error_at (location, "wrong type argument to conjugation");
3880 return error_mark_node;
3882 else if (!noconvert)
3883 arg = default_conversion (arg);
3886 case TRUTH_NOT_EXPR:
3887 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3888 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3889 && typecode != COMPLEX_TYPE)
3892 "wrong type argument to unary exclamation mark");
3893 return error_mark_node;
3897 arg = c_objc_common_truthvalue_conversion (location, xarg);
3898 arg = remove_c_maybe_const_expr (arg);
3901 arg = c_objc_common_truthvalue_conversion (location, arg);
3902 ret = invert_truthvalue_loc (location, arg);
3903 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3904 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3905 location = EXPR_LOCATION (ret);
3906 goto return_build_unary_op;
3910 ret = build_real_imag_expr (location, code, arg);
3911 if (ret == error_mark_node)
3912 return error_mark_node;
3913 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3914 eptype = TREE_TYPE (eptype);
3915 goto return_build_unary_op;
3917 case PREINCREMENT_EXPR:
3918 case POSTINCREMENT_EXPR:
3919 case PREDECREMENT_EXPR:
3920 case POSTDECREMENT_EXPR:
3922 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3924 tree inner = build_unary_op (location, code,
3925 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3926 if (inner == error_mark_node)
3927 return error_mark_node;
3928 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3929 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3930 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3931 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3932 goto return_build_unary_op;
3935 /* Complain about anything that is not a true lvalue. In
3936 Objective-C, skip this check for property_refs. */
3937 if (!objc_is_property_ref (arg)
3938 && !lvalue_or_else (location,
3939 arg, ((code == PREINCREMENT_EXPR
3940 || code == POSTINCREMENT_EXPR)
3943 return error_mark_node;
3945 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3947 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3948 warning_at (location, OPT_Wc___compat,
3949 "increment of enumeration value is invalid in C++");
3951 warning_at (location, OPT_Wc___compat,
3952 "decrement of enumeration value is invalid in C++");
3955 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3956 arg = c_fully_fold (arg, false, NULL);
3959 atomic_op = really_atomic_lvalue (arg);
3961 /* Increment or decrement the real part of the value,
3962 and don't change the imaginary part. */
3963 if (typecode == COMPLEX_TYPE)
3967 pedwarn (location, OPT_Wpedantic,
3968 "ISO C does not support %<++%> and %<--%> on complex types");
3972 arg = stabilize_reference (arg);
3973 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3974 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3975 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3976 if (real == error_mark_node || imag == error_mark_node)
3977 return error_mark_node;
3978 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3980 goto return_build_unary_op;
3984 /* Report invalid types. */
3986 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3987 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
3988 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
3990 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3991 error_at (location, "wrong type argument to increment");
3993 error_at (location, "wrong type argument to decrement");
3995 return error_mark_node;
4001 argtype = TREE_TYPE (arg);
4003 /* Compute the increment. */
4005 if (typecode == POINTER_TYPE)
4007 /* If pointer target is an undefined struct,
4008 we just cannot know how to do the arithmetic. */
4009 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4011 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4013 "increment of pointer to unknown structure");
4016 "decrement of pointer to unknown structure");
4018 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4019 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4021 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4022 pedwarn (location, OPT_Wpointer_arith,
4023 "wrong type argument to increment");
4025 pedwarn (location, OPT_Wpointer_arith,
4026 "wrong type argument to decrement");
4029 inc = c_size_in_bytes (TREE_TYPE (argtype));
4030 inc = convert_to_ptrofftype_loc (location, inc);
4032 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4034 /* For signed fract types, we invert ++ to -- or
4035 -- to ++, and change inc from 1 to -1, because
4036 it is not possible to represent 1 in signed fract constants.
4037 For unsigned fract types, the result always overflows and
4038 we get an undefined (original) or the maximum value. */
4039 if (code == PREINCREMENT_EXPR)
4040 code = PREDECREMENT_EXPR;
4041 else if (code == PREDECREMENT_EXPR)
4042 code = PREINCREMENT_EXPR;
4043 else if (code == POSTINCREMENT_EXPR)
4044 code = POSTDECREMENT_EXPR;
4045 else /* code == POSTDECREMENT_EXPR */
4046 code = POSTINCREMENT_EXPR;
4048 inc = integer_minus_one_node;
4049 inc = convert (argtype, inc);
4053 inc = VECTOR_TYPE_P (argtype)
4054 ? build_one_cst (argtype)
4056 inc = convert (argtype, inc);
4059 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4060 need to ask Objective-C to build the increment or decrement
4061 expression for it. */
4062 if (objc_is_property_ref (arg))
4063 return objc_build_incr_expr_for_property_ref (location, code,
4066 /* Report a read-only lvalue. */
4067 if (TYPE_READONLY (argtype))
4069 readonly_error (location, arg,
4070 ((code == PREINCREMENT_EXPR
4071 || code == POSTINCREMENT_EXPR)
4072 ? lv_increment : lv_decrement));
4073 return error_mark_node;
4075 else if (TREE_READONLY (arg))
4076 readonly_warning (arg,
4077 ((code == PREINCREMENT_EXPR
4078 || code == POSTINCREMENT_EXPR)
4079 ? lv_increment : lv_decrement));
4081 /* If the argument is atomic, use the special code sequences for
4082 atomic compound assignment. */
4085 arg = stabilize_reference (arg);
4086 ret = build_atomic_assign (location, arg,
4087 ((code == PREINCREMENT_EXPR
4088 || code == POSTINCREMENT_EXPR)
4091 (FRACT_MODE_P (TYPE_MODE (argtype))
4093 : integer_one_node),
4094 (code == POSTINCREMENT_EXPR
4095 || code == POSTDECREMENT_EXPR));
4096 goto return_build_unary_op;
4099 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4100 val = boolean_increment (code, arg);
4102 val = build2 (code, TREE_TYPE (arg), arg, inc);
4103 TREE_SIDE_EFFECTS (val) = 1;
4104 if (TREE_CODE (val) != code)
4105 TREE_NO_WARNING (val) = 1;
4107 goto return_build_unary_op;
4111 /* Note that this operation never does default_conversion. */
4113 /* The operand of unary '&' must be an lvalue (which excludes
4114 expressions of type void), or, in C99, the result of a [] or
4115 unary '*' operator. */
4116 if (VOID_TYPE_P (TREE_TYPE (arg))
4117 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4118 && (TREE_CODE (arg) != INDIRECT_REF
4120 pedwarn (location, 0, "taking address of expression of type %<void%>");
4122 /* Let &* cancel out to simplify resulting code. */
4123 if (TREE_CODE (arg) == INDIRECT_REF)
4125 /* Don't let this be an lvalue. */
4126 if (lvalue_p (TREE_OPERAND (arg, 0)))
4127 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4128 ret = TREE_OPERAND (arg, 0);
4129 goto return_build_unary_op;
4132 /* For &x[y], return x+y */
4133 if (TREE_CODE (arg) == ARRAY_REF)
4135 tree op0 = TREE_OPERAND (arg, 0);
4136 if (!c_mark_addressable (op0))
4137 return error_mark_node;
4140 /* Anything not already handled and not a true memory reference
4141 or a non-lvalue array is an error. */
4142 else if (typecode != FUNCTION_TYPE && !flag
4143 && !lvalue_or_else (location, arg, lv_addressof))
4144 return error_mark_node;
4146 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4148 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4150 tree inner = build_unary_op (location, code,
4151 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4152 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4153 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4154 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4155 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4156 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4157 goto return_build_unary_op;
4160 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4161 argtype = TREE_TYPE (arg);
4163 /* If the lvalue is const or volatile, merge that into the type
4164 to which the address will point. This is only needed
4165 for function types. */
4166 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4167 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4168 && TREE_CODE (argtype) == FUNCTION_TYPE)
4170 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4171 int quals = orig_quals;
4173 if (TREE_READONLY (arg))
4174 quals |= TYPE_QUAL_CONST;
4175 if (TREE_THIS_VOLATILE (arg))
4176 quals |= TYPE_QUAL_VOLATILE;
4178 argtype = c_build_qualified_type (argtype, quals);
4181 if (!c_mark_addressable (arg))
4182 return error_mark_node;
4184 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4185 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4187 argtype = build_pointer_type (argtype);
4189 /* ??? Cope with user tricks that amount to offsetof. Delete this
4190 when we have proper support for integer constant expressions. */
4191 val = get_base_address (arg);
4192 if (val && TREE_CODE (val) == INDIRECT_REF
4193 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4195 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4196 goto return_build_unary_op;
4199 val = build1 (ADDR_EXPR, argtype, arg);
4202 goto return_build_unary_op;
4209 argtype = TREE_TYPE (arg);
4210 if (TREE_CODE (arg) == INTEGER_CST)
4211 ret = (require_constant_value
4212 ? fold_build1_initializer_loc (location, code, argtype, arg)
4213 : fold_build1_loc (location, code, argtype, arg));
4215 ret = build1 (code, argtype, arg);
4216 return_build_unary_op:
4217 gcc_assert (ret != error_mark_node);
4218 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4219 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4220 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4221 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4222 ret = note_integer_operands (ret);
4224 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4225 protected_set_expr_location (ret, location);
4229 /* Return nonzero if REF is an lvalue valid for this language.
4230 Lvalues can be assigned, unless their type has TYPE_READONLY.
4231 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4234 lvalue_p (const_tree ref)
4236 const enum tree_code code = TREE_CODE (ref);
4243 return lvalue_p (TREE_OPERAND (ref, 0));
4245 case C_MAYBE_CONST_EXPR:
4246 return lvalue_p (TREE_OPERAND (ref, 1));
4248 case COMPOUND_LITERAL_EXPR:
4254 case ARRAY_NOTATION_REF:
4259 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4260 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4263 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4270 /* Give a warning for storing in something that is read-only in GCC
4271 terms but not const in ISO C terms. */
4274 readonly_warning (tree arg, enum lvalue_use use)
4279 warning (0, "assignment of read-only location %qE", arg);
4282 warning (0, "increment of read-only location %qE", arg);
4285 warning (0, "decrement of read-only location %qE", arg);
4294 /* Return nonzero if REF is an lvalue valid for this language;
4295 otherwise, print an error message and return zero. USE says
4296 how the lvalue is being used and so selects the error message.
4297 LOCATION is the location at which any error should be reported. */
4300 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4302 int win = lvalue_p (ref);
4305 lvalue_error (loc, use);
4310 /* Mark EXP saying that we need to be able to take the
4311 address of it; it should not be allocated in a register.
4312 Returns true if successful. */
4315 c_mark_addressable (tree exp)
4320 switch (TREE_CODE (x))
4323 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4326 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4330 /* ... fall through ... */
4336 x = TREE_OPERAND (x, 0);
4339 case COMPOUND_LITERAL_EXPR:
4341 TREE_ADDRESSABLE (x) = 1;
4348 if (C_DECL_REGISTER (x)
4349 && DECL_NONLOCAL (x))
4351 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4354 ("global register variable %qD used in nested function", x);
4357 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4359 else if (C_DECL_REGISTER (x))
4361 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4362 error ("address of global register variable %qD requested", x);
4364 error ("address of register variable %qD requested", x);
4370 TREE_ADDRESSABLE (x) = 1;
4377 /* Convert EXPR to TYPE, warning about conversion problems with
4378 constants. SEMANTIC_TYPE is the type this conversion would use
4379 without excess precision. If SEMANTIC_TYPE is NULL, this function
4380 is equivalent to convert_and_check. This function is a wrapper that
4381 handles conversions that may be different than
4382 the usual ones because of excess precision. */
4385 ep_convert_and_check (location_t loc, tree type, tree expr,
4388 if (TREE_TYPE (expr) == type)
4392 return convert_and_check (loc, type, expr);
4394 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4395 && TREE_TYPE (expr) != semantic_type)
4397 /* For integers, we need to check the real conversion, not
4398 the conversion to the excess precision type. */
4399 expr = convert_and_check (loc, semantic_type, expr);
4401 /* Result type is the excess precision type, which should be
4402 large enough, so do not check. */
4403 return convert (type, expr);
4406 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4407 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4408 if folded to an integer constant then the unselected half may
4409 contain arbitrary operations not normally permitted in constant
4410 expressions. Set the location of the expression to LOC. */
4413 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4414 tree op1, tree op1_original_type, tree op2,
4415 tree op2_original_type)
4419 enum tree_code code1;
4420 enum tree_code code2;
4421 tree result_type = NULL;
4422 tree semantic_result_type = NULL;
4423 tree orig_op1 = op1, orig_op2 = op2;
4424 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4425 bool ifexp_int_operands;
4428 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4429 if (op1_int_operands)
4430 op1 = remove_c_maybe_const_expr (op1);
4431 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4432 if (op2_int_operands)
4433 op2 = remove_c_maybe_const_expr (op2);
4434 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4435 if (ifexp_int_operands)
4436 ifexp = remove_c_maybe_const_expr (ifexp);
4438 /* Promote both alternatives. */
4440 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4441 op1 = default_conversion (op1);
4442 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4443 op2 = default_conversion (op2);
4445 if (TREE_CODE (ifexp) == ERROR_MARK
4446 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4447 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4448 return error_mark_node;
4450 type1 = TREE_TYPE (op1);
4451 code1 = TREE_CODE (type1);
4452 type2 = TREE_TYPE (op2);
4453 code2 = TREE_CODE (type2);
4455 /* C90 does not permit non-lvalue arrays in conditional expressions.
4456 In C99 they will be pointers by now. */
4457 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4459 error_at (colon_loc, "non-lvalue array in conditional expression");
4460 return error_mark_node;
4463 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4464 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4465 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4466 || code1 == COMPLEX_TYPE)
4467 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4468 || code2 == COMPLEX_TYPE))
4470 semantic_result_type = c_common_type (type1, type2);
4471 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4473 op1 = TREE_OPERAND (op1, 0);
4474 type1 = TREE_TYPE (op1);
4475 gcc_assert (TREE_CODE (type1) == code1);
4477 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4479 op2 = TREE_OPERAND (op2, 0);
4480 type2 = TREE_TYPE (op2);
4481 gcc_assert (TREE_CODE (type2) == code2);
4485 if (warn_cxx_compat)
4487 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4488 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4490 if (TREE_CODE (t1) == ENUMERAL_TYPE
4491 && TREE_CODE (t2) == ENUMERAL_TYPE
4492 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4493 warning_at (colon_loc, OPT_Wc___compat,
4494 ("different enum types in conditional is "
4495 "invalid in C++: %qT vs %qT"),
4499 /* Quickly detect the usual case where op1 and op2 have the same type
4501 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4504 result_type = type1;
4506 result_type = TYPE_MAIN_VARIANT (type1);
4508 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4509 || code1 == COMPLEX_TYPE)
4510 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4511 || code2 == COMPLEX_TYPE))
4513 result_type = c_common_type (type1, type2);
4514 do_warn_double_promotion (result_type, type1, type2,
4515 "implicit conversion from %qT to %qT to "
4516 "match other result of conditional",
4519 /* If -Wsign-compare, warn here if type1 and type2 have
4520 different signedness. We'll promote the signed to unsigned
4521 and later code won't know it used to be different.
4522 Do this check on the original types, so that explicit casts
4523 will be considered, but default promotions won't. */
4524 if (c_inhibit_evaluation_warnings == 0)
4526 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4527 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4529 if (unsigned_op1 ^ unsigned_op2)
4533 /* Do not warn if the result type is signed, since the
4534 signed type will only be chosen if it can represent
4535 all the values of the unsigned type. */
4536 if (!TYPE_UNSIGNED (result_type))
4540 bool op1_maybe_const = true;
4541 bool op2_maybe_const = true;
4543 /* Do not warn if the signed quantity is an
4544 unsuffixed integer literal (or some static
4545 constant expression involving such literals) and
4546 it is non-negative. This warning requires the
4547 operands to be folded for best results, so do
4548 that folding in this case even without
4549 warn_sign_compare to avoid warning options
4550 possibly affecting code generation. */
4551 c_inhibit_evaluation_warnings
4552 += (ifexp == truthvalue_false_node);
4553 op1 = c_fully_fold (op1, require_constant_value,
4555 c_inhibit_evaluation_warnings
4556 -= (ifexp == truthvalue_false_node);
4558 c_inhibit_evaluation_warnings
4559 += (ifexp == truthvalue_true_node);
4560 op2 = c_fully_fold (op2, require_constant_value,
4562 c_inhibit_evaluation_warnings
4563 -= (ifexp == truthvalue_true_node);
4565 if (warn_sign_compare)
4568 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4570 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4573 warning_at (colon_loc, OPT_Wsign_compare,
4574 ("signed and unsigned type in "
4575 "conditional expression"));
4577 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4578 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4579 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4580 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4585 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4587 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4588 pedwarn (colon_loc, OPT_Wpedantic,
4589 "ISO C forbids conditional expr with only one void side");
4590 result_type = void_type_node;
4592 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4594 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4595 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4596 addr_space_t as_common;
4598 if (comp_target_types (colon_loc, type1, type2))
4599 result_type = common_pointer_type (type1, type2);
4600 else if (null_pointer_constant_p (orig_op1))
4601 result_type = type2;
4602 else if (null_pointer_constant_p (orig_op2))
4603 result_type = type1;
4604 else if (!addr_space_superset (as1, as2, &as_common))
4606 error_at (colon_loc, "pointers to disjoint address spaces "
4607 "used in conditional expression");
4608 return error_mark_node;
4610 else if (VOID_TYPE_P (TREE_TYPE (type1))
4611 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4613 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4614 pedwarn (colon_loc, OPT_Wpedantic,
4615 "ISO C forbids conditional expr between "
4616 "%<void *%> and function pointer");
4617 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4618 TREE_TYPE (type2)));
4620 else if (VOID_TYPE_P (TREE_TYPE (type2))
4621 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4623 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4624 pedwarn (colon_loc, OPT_Wpedantic,
4625 "ISO C forbids conditional expr between "
4626 "%<void *%> and function pointer");
4627 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4628 TREE_TYPE (type1)));
4630 /* Objective-C pointer comparisons are a bit more lenient. */
4631 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4632 result_type = objc_common_type (type1, type2);
4635 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4637 pedwarn (colon_loc, 0,
4638 "pointer type mismatch in conditional expression");
4639 result_type = build_pointer_type
4640 (build_qualified_type (void_type_node, qual));
4643 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4645 if (!null_pointer_constant_p (orig_op2))
4646 pedwarn (colon_loc, 0,
4647 "pointer/integer type mismatch in conditional expression");
4650 op2 = null_pointer_node;
4652 result_type = type1;
4654 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4656 if (!null_pointer_constant_p (orig_op1))
4657 pedwarn (colon_loc, 0,
4658 "pointer/integer type mismatch in conditional expression");
4661 op1 = null_pointer_node;
4663 result_type = type2;
4668 if (flag_cond_mismatch)
4669 result_type = void_type_node;
4672 error_at (colon_loc, "type mismatch in conditional expression");
4673 return error_mark_node;
4677 /* Merge const and volatile flags of the incoming types. */
4679 = build_type_variant (result_type,
4680 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4681 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4683 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4684 semantic_result_type);
4685 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4686 semantic_result_type);
4688 if (ifexp_bcp && ifexp == truthvalue_true_node)
4690 op2_int_operands = true;
4691 op1 = c_fully_fold (op1, require_constant_value, NULL);
4693 if (ifexp_bcp && ifexp == truthvalue_false_node)
4695 op1_int_operands = true;
4696 op2 = c_fully_fold (op2, require_constant_value, NULL);
4698 int_const = int_operands = (ifexp_int_operands
4700 && op2_int_operands);
4703 int_const = ((ifexp == truthvalue_true_node
4704 && TREE_CODE (orig_op1) == INTEGER_CST
4705 && !TREE_OVERFLOW (orig_op1))
4706 || (ifexp == truthvalue_false_node
4707 && TREE_CODE (orig_op2) == INTEGER_CST
4708 && !TREE_OVERFLOW (orig_op2)));
4710 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4711 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4716 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4717 nested inside of the expression. */
4718 op1 = c_fully_fold (op1, false, NULL);
4719 op2 = c_fully_fold (op2, false, NULL);
4721 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4723 ret = note_integer_operands (ret);
4725 if (semantic_result_type)
4726 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4728 protected_set_expr_location (ret, colon_loc);
4732 /* Return a compound expression that performs two expressions and
4733 returns the value of the second of them.
4735 LOC is the location of the COMPOUND_EXPR. */
4738 build_compound_expr (location_t loc, tree expr1, tree expr2)
4740 bool expr1_int_operands, expr2_int_operands;
4741 tree eptype = NULL_TREE;
4745 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4746 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4749 "spawned function call cannot be part of a comma expression");
4750 return error_mark_node;
4752 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4753 if (expr1_int_operands)
4754 expr1 = remove_c_maybe_const_expr (expr1);
4755 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4756 if (expr2_int_operands)
4757 expr2 = remove_c_maybe_const_expr (expr2);
4759 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4760 expr1 = TREE_OPERAND (expr1, 0);
4761 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4763 eptype = TREE_TYPE (expr2);
4764 expr2 = TREE_OPERAND (expr2, 0);
4767 if (!TREE_SIDE_EFFECTS (expr1))
4769 /* The left-hand operand of a comma expression is like an expression
4770 statement: with -Wunused, we should warn if it doesn't have
4771 any side-effects, unless it was explicitly cast to (void). */
4772 if (warn_unused_value)
4774 if (VOID_TYPE_P (TREE_TYPE (expr1))
4775 && CONVERT_EXPR_P (expr1))
4777 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4778 && TREE_CODE (expr1) == COMPOUND_EXPR
4779 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4780 ; /* (void) a, (void) b, c */
4782 warning_at (loc, OPT_Wunused_value,
4783 "left-hand operand of comma expression has no effect");
4786 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4787 && warn_unused_value)
4790 location_t cloc = loc;
4791 while (TREE_CODE (r) == COMPOUND_EXPR)
4793 if (EXPR_HAS_LOCATION (r))
4794 cloc = EXPR_LOCATION (r);
4795 r = TREE_OPERAND (r, 1);
4797 if (!TREE_SIDE_EFFECTS (r)
4798 && !VOID_TYPE_P (TREE_TYPE (r))
4799 && !CONVERT_EXPR_P (r))
4800 warning_at (cloc, OPT_Wunused_value,
4801 "right-hand operand of comma expression has no effect");
4804 /* With -Wunused, we should also warn if the left-hand operand does have
4805 side-effects, but computes a value which is not used. For example, in
4806 `foo() + bar(), baz()' the result of the `+' operator is not used,
4807 so we should issue a warning. */
4808 else if (warn_unused_value)
4809 warn_if_unused_value (expr1, loc);
4811 if (expr2 == error_mark_node)
4812 return error_mark_node;
4814 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4817 && expr1_int_operands
4818 && expr2_int_operands)
4819 ret = note_integer_operands (ret);
4822 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4824 protected_set_expr_location (ret, loc);
4828 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4829 which we are casting. OTYPE is the type of the expression being
4830 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4831 of the cast. -Wcast-qual appeared on the command line. Named
4832 address space qualifiers are not handled here, because they result
4833 in different warnings. */
4836 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4838 tree in_type = type;
4839 tree in_otype = otype;
4844 /* Check that the qualifiers on IN_TYPE are a superset of the
4845 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4846 nodes is uninteresting and we stop as soon as we hit a
4847 non-POINTER_TYPE node on either type. */
4850 in_otype = TREE_TYPE (in_otype);
4851 in_type = TREE_TYPE (in_type);
4853 /* GNU C allows cv-qualified function types. 'const' means the
4854 function is very pure, 'volatile' means it can't return. We
4855 need to warn when such qualifiers are added, not when they're
4857 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4858 && TREE_CODE (in_type) == FUNCTION_TYPE)
4859 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4860 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4862 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4863 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4865 while (TREE_CODE (in_type) == POINTER_TYPE
4866 && TREE_CODE (in_otype) == POINTER_TYPE);
4869 warning_at (loc, OPT_Wcast_qual,
4870 "cast adds %q#v qualifier to function type", added);
4873 /* There are qualifiers present in IN_OTYPE that are not present
4875 warning_at (loc, OPT_Wcast_qual,
4876 "cast discards %qv qualifier from pointer target type",
4879 if (added || discarded)
4882 /* A cast from **T to const **T is unsafe, because it can cause a
4883 const value to be changed with no additional warning. We only
4884 issue this warning if T is the same on both sides, and we only
4885 issue the warning if there are the same number of pointers on
4886 both sides, as otherwise the cast is clearly unsafe anyhow. A
4887 cast is unsafe when a qualifier is added at one level and const
4888 is not present at all outer levels.
4890 To issue this warning, we check at each level whether the cast
4891 adds new qualifiers not already seen. We don't need to special
4892 case function types, as they won't have the same
4893 TYPE_MAIN_VARIANT. */
4895 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4897 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4902 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4905 in_type = TREE_TYPE (in_type);
4906 in_otype = TREE_TYPE (in_otype);
4907 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4910 warning_at (loc, OPT_Wcast_qual,
4911 "to be safe all intermediate pointers in cast from "
4912 "%qT to %qT must be %<const%> qualified",
4917 is_const = TYPE_READONLY (in_type);
4919 while (TREE_CODE (in_type) == POINTER_TYPE);
4922 /* Build an expression representing a cast to type TYPE of expression EXPR.
4923 LOC is the location of the cast-- typically the open paren of the cast. */
4926 build_c_cast (location_t loc, tree type, tree expr)
4930 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4931 expr = TREE_OPERAND (expr, 0);
4935 if (type == error_mark_node || expr == error_mark_node)
4936 return error_mark_node;
4938 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4939 only in <protocol> qualifications. But when constructing cast expressions,
4940 the protocols do matter and must be kept around. */
4941 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4942 return build1 (NOP_EXPR, type, expr);
4944 type = TYPE_MAIN_VARIANT (type);
4946 if (TREE_CODE (type) == ARRAY_TYPE)
4948 error_at (loc, "cast specifies array type");
4949 return error_mark_node;
4952 if (TREE_CODE (type) == FUNCTION_TYPE)
4954 error_at (loc, "cast specifies function type");
4955 return error_mark_node;
4958 if (!VOID_TYPE_P (type))
4960 value = require_complete_type (value);
4961 if (value == error_mark_node)
4962 return error_mark_node;
4965 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4967 if (TREE_CODE (type) == RECORD_TYPE
4968 || TREE_CODE (type) == UNION_TYPE)
4969 pedwarn (loc, OPT_Wpedantic,
4970 "ISO C forbids casting nonscalar to the same type");
4972 else if (TREE_CODE (type) == UNION_TYPE)
4976 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4977 if (TREE_TYPE (field) != error_mark_node
4978 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4979 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4985 bool maybe_const = true;
4987 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
4988 t = c_fully_fold (value, false, &maybe_const);
4989 t = build_constructor_single (type, field, t);
4991 t = c_wrap_maybe_const (t, true);
4992 t = digest_init (loc, type, t,
4993 NULL_TREE, false, true, 0);
4994 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4997 error_at (loc, "cast to union type from type not present in union");
4998 return error_mark_node;
5004 if (type == void_type_node)
5006 tree t = build1 (CONVERT_EXPR, type, value);
5007 SET_EXPR_LOCATION (t, loc);
5011 otype = TREE_TYPE (value);
5013 /* Optionally warn about potentially worrisome casts. */
5015 && TREE_CODE (type) == POINTER_TYPE
5016 && TREE_CODE (otype) == POINTER_TYPE)
5017 handle_warn_cast_qual (loc, type, otype);
5019 /* Warn about conversions between pointers to disjoint
5021 if (TREE_CODE (type) == POINTER_TYPE
5022 && TREE_CODE (otype) == POINTER_TYPE
5023 && !null_pointer_constant_p (value))
5025 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5026 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5027 addr_space_t as_common;
5029 if (!addr_space_superset (as_to, as_from, &as_common))
5031 if (ADDR_SPACE_GENERIC_P (as_from))
5032 warning_at (loc, 0, "cast to %s address space pointer "
5033 "from disjoint generic address space pointer",
5034 c_addr_space_name (as_to));
5036 else if (ADDR_SPACE_GENERIC_P (as_to))
5037 warning_at (loc, 0, "cast to generic address space pointer "
5038 "from disjoint %s address space pointer",
5039 c_addr_space_name (as_from));
5042 warning_at (loc, 0, "cast to %s address space pointer "
5043 "from disjoint %s address space pointer",
5044 c_addr_space_name (as_to),
5045 c_addr_space_name (as_from));
5049 /* Warn about possible alignment problems. */
5050 if (STRICT_ALIGNMENT
5051 && TREE_CODE (type) == POINTER_TYPE
5052 && TREE_CODE (otype) == POINTER_TYPE
5053 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5054 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5055 /* Don't warn about opaque types, where the actual alignment
5056 restriction is unknown. */
5057 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5058 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5059 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5060 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5061 warning_at (loc, OPT_Wcast_align,
5062 "cast increases required alignment of target type");
5064 if (TREE_CODE (type) == INTEGER_TYPE
5065 && TREE_CODE (otype) == POINTER_TYPE
5066 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5067 /* Unlike conversion of integers to pointers, where the
5068 warning is disabled for converting constants because
5069 of cases such as SIG_*, warn about converting constant
5070 pointers to integers. In some cases it may cause unwanted
5071 sign extension, and a warning is appropriate. */
5072 warning_at (loc, OPT_Wpointer_to_int_cast,
5073 "cast from pointer to integer of different size");
5075 if (TREE_CODE (value) == CALL_EXPR
5076 && TREE_CODE (type) != TREE_CODE (otype))
5077 warning_at (loc, OPT_Wbad_function_cast,
5078 "cast from function call of type %qT "
5079 "to non-matching type %qT", otype, type);
5081 if (TREE_CODE (type) == POINTER_TYPE
5082 && TREE_CODE (otype) == INTEGER_TYPE
5083 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5084 /* Don't warn about converting any constant. */
5085 && !TREE_CONSTANT (value))
5087 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5088 "of different size");
5090 if (warn_strict_aliasing <= 2)
5091 strict_aliasing_warning (otype, type, expr);
5093 /* If pedantic, warn for conversions between function and object
5094 pointer types, except for converting a null pointer constant
5095 to function pointer type. */
5097 && TREE_CODE (type) == POINTER_TYPE
5098 && TREE_CODE (otype) == POINTER_TYPE
5099 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5100 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5101 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5102 "conversion of function pointer to object pointer type");
5105 && TREE_CODE (type) == POINTER_TYPE
5106 && TREE_CODE (otype) == POINTER_TYPE
5107 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5108 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5109 && !null_pointer_constant_p (value))
5110 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5111 "conversion of object pointer to function pointer type");
5114 value = convert (type, value);
5116 /* Ignore any integer overflow caused by the cast. */
5117 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5119 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5121 if (!TREE_OVERFLOW (value))
5123 /* Avoid clobbering a shared constant. */
5124 value = copy_node (value);
5125 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5128 else if (TREE_OVERFLOW (value))
5129 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5130 value = build_int_cst_wide (TREE_TYPE (value),
5131 TREE_INT_CST_LOW (value),
5132 TREE_INT_CST_HIGH (value));
5136 /* Don't let a cast be an lvalue. */
5138 value = non_lvalue_loc (loc, value);
5140 /* Don't allow the results of casting to floating-point or complex
5141 types be confused with actual constants, or casts involving
5142 integer and pointer types other than direct integer-to-integer
5143 and integer-to-pointer be confused with integer constant
5144 expressions and null pointer constants. */
5145 if (TREE_CODE (value) == REAL_CST
5146 || TREE_CODE (value) == COMPLEX_CST
5147 || (TREE_CODE (value) == INTEGER_CST
5148 && !((TREE_CODE (expr) == INTEGER_CST
5149 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5150 || TREE_CODE (expr) == REAL_CST
5151 || TREE_CODE (expr) == COMPLEX_CST)))
5152 value = build1 (NOP_EXPR, type, value);
5154 if (CAN_HAVE_LOCATION_P (value))
5155 SET_EXPR_LOCATION (value, loc);
5159 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5160 location of the open paren of the cast, or the position of the cast
5163 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5166 tree type_expr = NULL_TREE;
5167 bool type_expr_const = true;
5169 int saved_wsp = warn_strict_prototypes;
5171 /* This avoids warnings about unprototyped casts on
5172 integers. E.g. "#define SIG_DFL (void(*)())0". */
5173 if (TREE_CODE (expr) == INTEGER_CST)
5174 warn_strict_prototypes = 0;
5175 type = groktypename (type_name, &type_expr, &type_expr_const);
5176 warn_strict_prototypes = saved_wsp;
5178 ret = build_c_cast (loc, type, expr);
5181 bool inner_expr_const = true;
5182 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5183 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5184 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5185 && inner_expr_const);
5186 SET_EXPR_LOCATION (ret, loc);
5189 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5190 SET_EXPR_LOCATION (ret, loc);
5192 /* C++ does not permits types to be defined in a cast, but it
5193 allows references to incomplete types. */
5194 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5195 warning_at (loc, OPT_Wc___compat,
5196 "defining a type in a cast is invalid in C++");
5201 /* Build an assignment expression of lvalue LHS from value RHS.
5202 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5203 may differ from TREE_TYPE (LHS) for an enum bitfield.
5204 MODIFYCODE is the code for a binary operator that we use
5205 to combine the old value of LHS with RHS to get the new value.
5206 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5207 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5208 which may differ from TREE_TYPE (RHS) for an enum value.
5210 LOCATION is the location of the MODIFYCODE operator.
5211 RHS_LOC is the location of the RHS. */
5214 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5215 enum tree_code modifycode,
5216 location_t rhs_loc, tree rhs, tree rhs_origtype)
5220 tree rhseval = NULL_TREE;
5221 tree rhs_semantic_type = NULL_TREE;
5222 tree lhstype = TREE_TYPE (lhs);
5223 tree olhstype = lhstype;
5227 /* Types that aren't fully specified cannot be used in assignments. */
5228 lhs = require_complete_type (lhs);
5230 /* Avoid duplicate error messages from operands that had errors. */
5231 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5232 return error_mark_node;
5234 /* Ensure an error for assigning a non-lvalue array to an array in
5236 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5238 error_at (location, "assignment to expression with array type");
5239 return error_mark_node;
5242 /* For ObjC properties, defer this check. */
5243 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5244 return error_mark_node;
5246 is_atomic_op = really_atomic_lvalue (lhs);
5248 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5250 rhs_semantic_type = TREE_TYPE (rhs);
5251 rhs = TREE_OPERAND (rhs, 0);
5256 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5258 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5259 lhs_origtype, modifycode, rhs_loc, rhs,
5261 if (inner == error_mark_node)
5262 return error_mark_node;
5263 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5264 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5265 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5266 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5267 protected_set_expr_location (result, location);
5271 /* If a binary op has been requested, combine the old LHS value with the RHS
5272 producing the value we should actually store into the LHS. */
5274 if (modifycode != NOP_EXPR)
5276 lhs = c_fully_fold (lhs, false, NULL);
5277 lhs = stabilize_reference (lhs);
5279 /* Construct the RHS for any non-atomic compound assignemnt. */
5282 /* If in LHS op= RHS the RHS has side-effects, ensure they
5283 are preevaluated before the rest of the assignment expression's
5284 side-effects, because RHS could contain e.g. function calls
5286 if (TREE_SIDE_EFFECTS (rhs))
5288 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5291 newrhs = build_binary_op (location,
5292 modifycode, lhs, newrhs, 1);
5294 /* The original type of the right hand side is no longer
5296 rhs_origtype = NULL_TREE;
5300 if (c_dialect_objc ())
5302 /* Check if we are modifying an Objective-C property reference;
5303 if so, we need to generate setter calls. */
5304 result = objc_maybe_build_modify_expr (lhs, newrhs);
5308 /* Else, do the check that we postponed for Objective-C. */
5309 if (!lvalue_or_else (location, lhs, lv_assign))
5310 return error_mark_node;
5313 /* Give an error for storing in something that is 'const'. */
5315 if (TYPE_READONLY (lhstype)
5316 || ((TREE_CODE (lhstype) == RECORD_TYPE
5317 || TREE_CODE (lhstype) == UNION_TYPE)
5318 && C_TYPE_FIELDS_READONLY (lhstype)))
5320 readonly_error (location, lhs, lv_assign);
5321 return error_mark_node;
5323 else if (TREE_READONLY (lhs))
5324 readonly_warning (lhs, lv_assign);
5326 /* If storing into a structure or union member,
5327 it has probably been given type `int'.
5328 Compute the type that would go with
5329 the actual amount of storage the member occupies. */
5331 if (TREE_CODE (lhs) == COMPONENT_REF
5332 && (TREE_CODE (lhstype) == INTEGER_TYPE
5333 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5334 || TREE_CODE (lhstype) == REAL_TYPE
5335 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5336 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5338 /* If storing in a field that is in actuality a short or narrower than one,
5339 we must store in the field in its actual type. */
5341 if (lhstype != TREE_TYPE (lhs))
5343 lhs = copy_node (lhs);
5344 TREE_TYPE (lhs) = lhstype;
5347 /* Issue -Wc++-compat warnings about an assignment to an enum type
5348 when LHS does not have its original type. This happens for,
5349 e.g., an enum bitfield in a struct. */
5351 && lhs_origtype != NULL_TREE
5352 && lhs_origtype != lhstype
5353 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5355 tree checktype = (rhs_origtype != NULL_TREE
5358 if (checktype != error_mark_node
5359 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5360 || (is_atomic_op && modifycode != NOP_EXPR)))
5361 warning_at (location, OPT_Wc___compat,
5362 "enum conversion in assignment is invalid in C++");
5365 /* If the lhs is atomic, remove that qualifier. */
5368 lhstype = build_qualified_type (lhstype,
5369 (TYPE_QUALS (lhstype)
5370 & ~TYPE_QUAL_ATOMIC));
5371 olhstype = build_qualified_type (olhstype,
5372 (TYPE_QUALS (lhstype)
5373 & ~TYPE_QUAL_ATOMIC));
5376 /* Convert new value to destination type. Fold it first, then
5377 restore any excess precision information, for the sake of
5378 conversion warnings. */
5380 if (!(is_atomic_op && modifycode != NOP_EXPR))
5382 npc = null_pointer_constant_p (newrhs);
5383 newrhs = c_fully_fold (newrhs, false, NULL);
5384 if (rhs_semantic_type)
5385 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5386 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5387 rhs_origtype, ic_assign, npc,
5388 NULL_TREE, NULL_TREE, 0);
5389 if (TREE_CODE (newrhs) == ERROR_MARK)
5390 return error_mark_node;
5393 /* Emit ObjC write barrier, if necessary. */
5394 if (c_dialect_objc () && flag_objc_gc)
5396 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5399 protected_set_expr_location (result, location);
5404 /* Scan operands. */
5407 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5410 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5411 TREE_SIDE_EFFECTS (result) = 1;
5412 protected_set_expr_location (result, location);
5415 /* If we got the LHS in a different type for storing in,
5416 convert the result back to the nominal type of LHS
5417 so that the value we return always has the same type
5418 as the LHS argument. */
5420 if (olhstype == TREE_TYPE (result))
5423 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5424 rhs_origtype, ic_assign, false, NULL_TREE,
5426 protected_set_expr_location (result, location);
5430 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5434 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5435 This is used to implement -fplan9-extensions. */
5438 find_anonymous_field_with_type (tree struct_type, tree type)
5443 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5444 || TREE_CODE (struct_type) == UNION_TYPE);
5446 for (field = TYPE_FIELDS (struct_type);
5448 field = TREE_CHAIN (field))
5450 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5451 ? c_build_qualified_type (TREE_TYPE (field),
5453 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5454 if (DECL_NAME (field) == NULL
5455 && comptypes (type, fieldtype))
5461 else if (DECL_NAME (field) == NULL
5462 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5463 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5464 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5474 /* RHS is an expression whose type is pointer to struct. If there is
5475 an anonymous field in RHS with type TYPE, then return a pointer to
5476 that field in RHS. This is used with -fplan9-extensions. This
5477 returns NULL if no conversion could be found. */
5480 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5482 tree rhs_struct_type, lhs_main_type;
5483 tree field, found_field;
5484 bool found_sub_field;
5487 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5488 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5489 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5490 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5492 gcc_assert (POINTER_TYPE_P (type));
5493 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5494 ? c_build_qualified_type (TREE_TYPE (type),
5496 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5498 found_field = NULL_TREE;
5499 found_sub_field = false;
5500 for (field = TYPE_FIELDS (rhs_struct_type);
5502 field = TREE_CHAIN (field))
5504 if (DECL_NAME (field) != NULL_TREE
5505 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5506 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5508 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5509 ? c_build_qualified_type (TREE_TYPE (field),
5511 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5512 if (comptypes (lhs_main_type, fieldtype))
5514 if (found_field != NULL_TREE)
5516 found_field = field;
5518 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5521 if (found_field != NULL_TREE)
5523 found_field = field;
5524 found_sub_field = true;
5528 if (found_field == NULL_TREE)
5531 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5532 build_fold_indirect_ref (rhs), found_field,
5534 ret = build_fold_addr_expr_loc (location, ret);
5536 if (found_sub_field)
5538 ret = convert_to_anonymous_field (location, type, ret);
5539 gcc_assert (ret != NULL_TREE);
5545 /* Issue an error message for a bad initializer component.
5546 GMSGID identifies the message.
5547 The component name is taken from the spelling stack. */
5550 error_init (const char *gmsgid)
5554 /* The gmsgid may be a format string with %< and %>. */
5556 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5558 error ("(near initialization for %qs)", ofwhat);
5561 /* Issue a pedantic warning for a bad initializer component. OPT is
5562 the option OPT_* (from options.h) controlling this warning or 0 if
5563 it is unconditionally given. GMSGID identifies the message. The
5564 component name is taken from the spelling stack. */
5567 pedwarn_init (location_t location, int opt, const char *gmsgid)
5571 /* The gmsgid may be a format string with %< and %>. */
5572 pedwarn (location, opt, gmsgid);
5573 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5575 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5578 /* Issue a warning for a bad initializer component.
5580 OPT is the OPT_W* value corresponding to the warning option that
5581 controls this warning. GMSGID identifies the message. The
5582 component name is taken from the spelling stack. */
5585 warning_init (location_t loc, int opt, const char *gmsgid)
5589 /* The gmsgid may be a format string with %< and %>. */
5590 warning_at (loc, opt, gmsgid);
5591 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5593 warning_at (loc, opt, "(near initialization for %qs)", ofwhat);
5596 /* If TYPE is an array type and EXPR is a parenthesized string
5597 constant, warn if pedantic that EXPR is being used to initialize an
5598 object of type TYPE. */
5601 maybe_warn_string_init (tree type, struct c_expr expr)
5604 && TREE_CODE (type) == ARRAY_TYPE
5605 && TREE_CODE (expr.value) == STRING_CST
5606 && expr.original_code != STRING_CST)
5607 pedwarn_init (input_location, OPT_Wpedantic,
5608 "array initialized from parenthesized string constant");
5611 /* Convert value RHS to type TYPE as preparation for an assignment to
5612 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5613 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5614 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5615 constant before any folding.
5616 The real work of conversion is done by `convert'.
5617 The purpose of this function is to generate error messages
5618 for assignments that are not allowed in C.
5619 ERRTYPE says whether it is argument passing, assignment,
5620 initialization or return.
5622 LOCATION is the location of the assignment, EXPR_LOC is the location of
5623 the RHS or, for a function, location of an argument.
5624 FUNCTION is a tree for the function being called.
5625 PARMNUM is the number of the argument, for printing in error messages. */
5628 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5629 tree rhs, tree origtype, enum impl_conv errtype,
5630 bool null_pointer_constant, tree fundecl,
5631 tree function, int parmnum)
5633 enum tree_code codel = TREE_CODE (type);
5634 tree orig_rhs = rhs;
5636 enum tree_code coder;
5637 tree rname = NULL_TREE;
5638 bool objc_ok = false;
5640 if (errtype == ic_argpass)
5643 /* Change pointer to function to the function itself for
5645 if (TREE_CODE (function) == ADDR_EXPR
5646 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5647 function = TREE_OPERAND (function, 0);
5649 /* Handle an ObjC selector specially for diagnostics. */
5650 selector = objc_message_selector ();
5652 if (selector && parmnum > 2)
5659 /* This macro is used to emit diagnostics to ensure that all format
5660 strings are complete sentences, visible to gettext and checked at
5662 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5667 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5668 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5669 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5670 "expected %qT but argument is of type %qT", \
5674 pedwarn (LOCATION, OPT, AS); \
5677 pedwarn_init (LOCATION, OPT, IN); \
5680 pedwarn (LOCATION, OPT, RE); \
5683 gcc_unreachable (); \
5687 /* This macro is used to emit diagnostics to ensure that all format
5688 strings are complete sentences, visible to gettext and checked at
5689 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5690 extra parameter to enumerate qualifiers. */
5692 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5697 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5698 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5699 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5700 "expected %qT but argument is of type %qT", \
5704 pedwarn (LOCATION, OPT, AS, QUALS); \
5707 pedwarn (LOCATION, OPT, IN, QUALS); \
5710 pedwarn (LOCATION, OPT, RE, QUALS); \
5713 gcc_unreachable (); \
5717 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5718 rhs = TREE_OPERAND (rhs, 0);
5720 rhstype = TREE_TYPE (rhs);
5721 coder = TREE_CODE (rhstype);
5723 if (coder == ERROR_MARK)
5724 return error_mark_node;
5726 if (c_dialect_objc ())
5749 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5752 if (warn_cxx_compat)
5754 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5755 if (checktype != error_mark_node
5756 && TREE_CODE (type) == ENUMERAL_TYPE
5757 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5759 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5760 G_("enum conversion when passing argument "
5761 "%d of %qE is invalid in C++"),
5762 G_("enum conversion in assignment is "
5764 G_("enum conversion in initialization is "
5766 G_("enum conversion in return is "
5771 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5774 if (coder == VOID_TYPE)
5776 /* Except for passing an argument to an unprototyped function,
5777 this is a constraint violation. When passing an argument to
5778 an unprototyped function, it is compile-time undefined;
5779 making it a constraint in that case was rejected in
5781 error_at (location, "void value not ignored as it ought to be");
5782 return error_mark_node;
5784 rhs = require_complete_type (rhs);
5785 if (rhs == error_mark_node)
5786 return error_mark_node;
5787 /* A non-reference type can convert to a reference. This handles
5788 va_start, va_copy and possibly port built-ins. */
5789 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5791 if (!lvalue_p (rhs))
5793 error_at (location, "cannot pass rvalue to reference parameter");
5794 return error_mark_node;
5796 if (!c_mark_addressable (rhs))
5797 return error_mark_node;
5798 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5799 SET_EXPR_LOCATION (rhs, location);
5801 rhs = convert_for_assignment (location, expr_loc,
5802 build_pointer_type (TREE_TYPE (type)),
5803 rhs, origtype, errtype,
5804 null_pointer_constant, fundecl, function,
5806 if (rhs == error_mark_node)
5807 return error_mark_node;
5809 rhs = build1 (NOP_EXPR, type, rhs);
5810 SET_EXPR_LOCATION (rhs, location);
5813 /* Some types can interconvert without explicit casts. */
5814 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5815 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5816 return convert (type, rhs);
5817 /* Arithmetic types all interconvert, and enum is treated like int. */
5818 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5819 || codel == FIXED_POINT_TYPE
5820 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5821 || codel == BOOLEAN_TYPE)
5822 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5823 || coder == FIXED_POINT_TYPE
5824 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5825 || coder == BOOLEAN_TYPE))
5828 bool save = in_late_binary_op;
5829 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5830 in_late_binary_op = true;
5831 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5832 ? expr_loc : location, type, orig_rhs);
5833 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5834 in_late_binary_op = save;
5838 /* Aggregates in different TUs might need conversion. */
5839 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5841 && comptypes (type, rhstype))
5842 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5843 ? expr_loc : location, type, rhs);
5845 /* Conversion to a transparent union or record from its member types.
5846 This applies only to function arguments. */
5847 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5848 && TYPE_TRANSPARENT_AGGR (type))
5849 && errtype == ic_argpass)
5851 tree memb, marginal_memb = NULL_TREE;
5853 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5855 tree memb_type = TREE_TYPE (memb);
5857 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5858 TYPE_MAIN_VARIANT (rhstype)))
5861 if (TREE_CODE (memb_type) != POINTER_TYPE)
5864 if (coder == POINTER_TYPE)
5866 tree ttl = TREE_TYPE (memb_type);
5867 tree ttr = TREE_TYPE (rhstype);
5869 /* Any non-function converts to a [const][volatile] void *
5870 and vice versa; otherwise, targets must be the same.
5871 Meanwhile, the lhs target must have all the qualifiers of
5873 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5874 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5875 || comp_target_types (location, memb_type, rhstype))
5877 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5878 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5879 /* If this type won't generate any warnings, use it. */
5880 if (lquals == rquals
5881 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5882 && TREE_CODE (ttl) == FUNCTION_TYPE)
5883 ? ((lquals | rquals) == rquals)
5884 : ((lquals | rquals) == lquals)))
5887 /* Keep looking for a better type, but remember this one. */
5889 marginal_memb = memb;
5893 /* Can convert integer zero to any pointer type. */
5894 if (null_pointer_constant)
5896 rhs = null_pointer_node;
5901 if (memb || marginal_memb)
5905 /* We have only a marginally acceptable member type;
5906 it needs a warning. */
5907 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5908 tree ttr = TREE_TYPE (rhstype);
5910 /* Const and volatile mean something different for function
5911 types, so the usual warnings are not appropriate. */
5912 if (TREE_CODE (ttr) == FUNCTION_TYPE
5913 && TREE_CODE (ttl) == FUNCTION_TYPE)
5915 /* Because const and volatile on functions are
5916 restrictions that say the function will not do
5917 certain things, it is okay to use a const or volatile
5918 function where an ordinary one is wanted, but not
5920 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5921 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5922 WARN_FOR_QUALIFIERS (location, OPT_Wdiscarded_qualifiers,
5923 G_("passing argument %d of %qE "
5924 "makes %q#v qualified function "
5925 "pointer from unqualified"),
5926 G_("assignment makes %q#v qualified "
5927 "function pointer from "
5929 G_("initialization makes %q#v qualified "
5930 "function pointer from "
5932 G_("return makes %q#v qualified function "
5933 "pointer from unqualified"),
5934 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5936 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5937 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5938 WARN_FOR_QUALIFIERS (location, OPT_Wdiscarded_qualifiers,
5939 G_("passing argument %d of %qE discards "
5940 "%qv qualifier from pointer target type"),
5941 G_("assignment discards %qv qualifier "
5942 "from pointer target type"),
5943 G_("initialization discards %qv qualifier "
5944 "from pointer target type"),
5945 G_("return discards %qv qualifier from "
5946 "pointer target type"),
5947 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5949 memb = marginal_memb;
5952 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5953 pedwarn (location, OPT_Wpedantic,
5954 "ISO C prohibits argument conversion to union type");
5956 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5957 return build_constructor_single (type, memb, rhs);
5961 /* Conversions among pointers */
5962 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5963 && (coder == codel))
5965 tree ttl = TREE_TYPE (type);
5966 tree ttr = TREE_TYPE (rhstype);
5969 bool is_opaque_pointer;
5970 int target_cmp = 0; /* Cache comp_target_types () result. */
5974 if (TREE_CODE (mvl) != ARRAY_TYPE)
5975 mvl = (TYPE_ATOMIC (mvl)
5976 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
5978 : TYPE_MAIN_VARIANT (mvl));
5979 if (TREE_CODE (mvr) != ARRAY_TYPE)
5980 mvr = (TYPE_ATOMIC (mvr)
5981 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
5983 : TYPE_MAIN_VARIANT (mvr));
5984 /* Opaque pointers are treated like void pointers. */
5985 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5987 /* The Plan 9 compiler permits a pointer to a struct to be
5988 automatically converted into a pointer to an anonymous field
5989 within the struct. */
5990 if (flag_plan9_extensions
5991 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5992 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5995 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5996 if (new_rhs != NULL_TREE)
5999 rhstype = TREE_TYPE (rhs);
6000 coder = TREE_CODE (rhstype);
6001 ttr = TREE_TYPE (rhstype);
6002 mvr = TYPE_MAIN_VARIANT (ttr);
6006 /* C++ does not allow the implicit conversion void* -> T*. However,
6007 for the purpose of reducing the number of false positives, we
6008 tolerate the special case of
6012 where NULL is typically defined in C to be '(void *) 0'. */
6013 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6014 warning_at (location, OPT_Wc___compat,
6015 "request for implicit conversion "
6016 "from %qT to %qT not permitted in C++", rhstype, type);
6018 /* See if the pointers point to incompatible address spaces. */
6019 asl = TYPE_ADDR_SPACE (ttl);
6020 asr = TYPE_ADDR_SPACE (ttr);
6021 if (!null_pointer_constant_p (rhs)
6022 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6027 error_at (location, "passing argument %d of %qE from pointer to "
6028 "non-enclosed address space", parmnum, rname);
6031 error_at (location, "assignment from pointer to "
6032 "non-enclosed address space");
6035 error_at (location, "initialization from pointer to "
6036 "non-enclosed address space");
6039 error_at (location, "return from pointer to "
6040 "non-enclosed address space");
6045 return error_mark_node;
6048 /* Check if the right-hand side has a format attribute but the
6049 left-hand side doesn't. */
6050 if (warn_suggest_attribute_format
6051 && check_missing_format_attribute (type, rhstype))
6056 warning_at (location, OPT_Wsuggest_attribute_format,
6057 "argument %d of %qE might be "
6058 "a candidate for a format attribute",
6062 warning_at (location, OPT_Wsuggest_attribute_format,
6063 "assignment left-hand side might be "
6064 "a candidate for a format attribute");
6067 warning_at (location, OPT_Wsuggest_attribute_format,
6068 "initialization left-hand side might be "
6069 "a candidate for a format attribute");
6072 warning_at (location, OPT_Wsuggest_attribute_format,
6073 "return type might be "
6074 "a candidate for a format attribute");
6081 /* Any non-function converts to a [const][volatile] void *
6082 and vice versa; otherwise, targets must be the same.
6083 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6084 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6085 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6086 || (target_cmp = comp_target_types (location, type, rhstype))
6087 || is_opaque_pointer
6088 || ((c_common_unsigned_type (mvl)
6089 == c_common_unsigned_type (mvr))
6090 && (c_common_signed_type (mvl)
6091 == c_common_signed_type (mvr))
6092 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6095 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6098 && !null_pointer_constant
6099 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6100 WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic,
6101 G_("ISO C forbids passing argument %d of "
6102 "%qE between function pointer "
6104 G_("ISO C forbids assignment between "
6105 "function pointer and %<void *%>"),
6106 G_("ISO C forbids initialization between "
6107 "function pointer and %<void *%>"),
6108 G_("ISO C forbids return between function "
6109 "pointer and %<void *%>"));
6110 /* Const and volatile mean something different for function types,
6111 so the usual warnings are not appropriate. */
6112 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6113 && TREE_CODE (ttl) != FUNCTION_TYPE)
6115 /* Assignments between atomic and non-atomic objects are OK. */
6116 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6117 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6119 WARN_FOR_QUALIFIERS (location, OPT_Wdiscarded_qualifiers,
6120 G_("passing argument %d of %qE discards "
6121 "%qv qualifier from pointer target type"),
6122 G_("assignment discards %qv qualifier "
6123 "from pointer target type"),
6124 G_("initialization discards %qv qualifier "
6125 "from pointer target type"),
6126 G_("return discards %qv qualifier from "
6127 "pointer target type"),
6128 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6130 /* If this is not a case of ignoring a mismatch in signedness,
6132 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6135 /* If there is a mismatch, do warn. */
6136 else if (warn_pointer_sign)
6137 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
6138 G_("pointer targets in passing argument "
6139 "%d of %qE differ in signedness"),
6140 G_("pointer targets in assignment "
6141 "differ in signedness"),
6142 G_("pointer targets in initialization "
6143 "differ in signedness"),
6144 G_("pointer targets in return differ "
6147 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6148 && TREE_CODE (ttr) == FUNCTION_TYPE)
6150 /* Because const and volatile on functions are restrictions
6151 that say the function will not do certain things,
6152 it is okay to use a const or volatile function
6153 where an ordinary one is wanted, but not vice-versa. */
6154 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6155 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6156 WARN_FOR_QUALIFIERS (location, OPT_Wdiscarded_qualifiers,
6157 G_("passing argument %d of %qE makes "
6158 "%q#v qualified function pointer "
6159 "from unqualified"),
6160 G_("assignment makes %q#v qualified function "
6161 "pointer from unqualified"),
6162 G_("initialization makes %q#v qualified "
6163 "function pointer from unqualified"),
6164 G_("return makes %q#v qualified function "
6165 "pointer from unqualified"),
6166 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6170 /* Avoid warning about the volatile ObjC EH puts on decls. */
6172 WARN_FOR_ASSIGNMENT (location, 0,
6173 G_("passing argument %d of %qE from "
6174 "incompatible pointer type"),
6175 G_("assignment from incompatible pointer type"),
6176 G_("initialization from incompatible "
6178 G_("return from incompatible pointer type"));
6180 return convert (type, rhs);
6182 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6184 /* ??? This should not be an error when inlining calls to
6185 unprototyped functions. */
6186 error_at (location, "invalid use of non-lvalue array");
6187 return error_mark_node;
6189 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6191 /* An explicit constant 0 can convert to a pointer,
6192 or one that results from arithmetic, even including
6193 a cast to integer type. */
6194 if (!null_pointer_constant)
6195 WARN_FOR_ASSIGNMENT (location, 0,
6196 G_("passing argument %d of %qE makes "
6197 "pointer from integer without a cast"),
6198 G_("assignment makes pointer from integer "
6200 G_("initialization makes pointer from "
6201 "integer without a cast"),
6202 G_("return makes pointer from integer "
6205 return convert (type, rhs);
6207 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6209 WARN_FOR_ASSIGNMENT (location, 0,
6210 G_("passing argument %d of %qE makes integer "
6211 "from pointer without a cast"),
6212 G_("assignment makes integer from pointer "
6214 G_("initialization makes integer from pointer "
6216 G_("return makes integer from pointer "
6218 return convert (type, rhs);
6220 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6223 bool save = in_late_binary_op;
6224 in_late_binary_op = true;
6225 ret = convert (type, rhs);
6226 in_late_binary_op = save;
6233 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
6234 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6235 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
6236 "expected %qT but argument is of type %qT", type, rhstype);
6239 error_at (location, "incompatible types when assigning to type %qT from "
6240 "type %qT", type, rhstype);
6244 "incompatible types when initializing type %qT using type %qT",
6249 "incompatible types when returning type %qT but %qT was "
6250 "expected", rhstype, type);
6256 return error_mark_node;
6259 /* If VALUE is a compound expr all of whose expressions are constant, then
6260 return its value. Otherwise, return error_mark_node.
6262 This is for handling COMPOUND_EXPRs as initializer elements
6263 which is allowed with a warning when -pedantic is specified. */
6266 valid_compound_expr_initializer (tree value, tree endtype)
6268 if (TREE_CODE (value) == COMPOUND_EXPR)
6270 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6272 return error_mark_node;
6273 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6276 else if (!initializer_constant_valid_p (value, endtype))
6277 return error_mark_node;
6282 /* Perform appropriate conversions on the initial value of a variable,
6283 store it in the declaration DECL,
6284 and print any error messages that are appropriate.
6285 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6286 If the init is invalid, store an ERROR_MARK.
6288 INIT_LOC is the location of the initial value. */
6291 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6296 /* If variable's type was invalidly declared, just ignore it. */
6298 type = TREE_TYPE (decl);
6299 if (TREE_CODE (type) == ERROR_MARK)
6302 /* Digest the specified initializer into an expression. */
6305 npc = null_pointer_constant_p (init);
6306 value = digest_init (init_loc, type, init, origtype, npc,
6307 true, TREE_STATIC (decl));
6309 /* Store the expression if valid; else report error. */
6311 if (!in_system_header_at (input_location)
6312 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6313 warning (OPT_Wtraditional, "traditional C rejects automatic "
6314 "aggregate initialization");
6316 DECL_INITIAL (decl) = value;
6318 /* ANSI wants warnings about out-of-range constant initializers. */
6319 STRIP_TYPE_NOPS (value);
6320 if (TREE_STATIC (decl))
6321 constant_expression_warning (value);
6323 /* Check if we need to set array size from compound literal size. */
6324 if (TREE_CODE (type) == ARRAY_TYPE
6325 && TYPE_DOMAIN (type) == 0
6326 && value != error_mark_node)
6328 tree inside_init = init;
6330 STRIP_TYPE_NOPS (inside_init);
6331 inside_init = fold (inside_init);
6333 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6335 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6337 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6339 /* For int foo[] = (int [3]){1}; we need to set array size
6340 now since later on array initializer will be just the
6341 brace enclosed list of the compound literal. */
6342 tree etype = strip_array_types (TREE_TYPE (decl));
6343 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6344 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6346 layout_decl (cldecl, 0);
6348 = c_build_qualified_type (type, TYPE_QUALS (etype));
6354 /* Methods for storing and printing names for error messages. */
6356 /* Implement a spelling stack that allows components of a name to be pushed
6357 and popped. Each element on the stack is this structure. */
6364 unsigned HOST_WIDE_INT i;
6369 #define SPELLING_STRING 1
6370 #define SPELLING_MEMBER 2
6371 #define SPELLING_BOUNDS 3
6373 static struct spelling *spelling; /* Next stack element (unused). */
6374 static struct spelling *spelling_base; /* Spelling stack base. */
6375 static int spelling_size; /* Size of the spelling stack. */
6377 /* Macros to save and restore the spelling stack around push_... functions.
6378 Alternative to SAVE_SPELLING_STACK. */
6380 #define SPELLING_DEPTH() (spelling - spelling_base)
6381 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6383 /* Push an element on the spelling stack with type KIND and assign VALUE
6386 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6388 int depth = SPELLING_DEPTH (); \
6390 if (depth >= spelling_size) \
6392 spelling_size += 10; \
6393 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6395 RESTORE_SPELLING_DEPTH (depth); \
6398 spelling->kind = (KIND); \
6399 spelling->MEMBER = (VALUE); \
6403 /* Push STRING on the stack. Printed literally. */
6406 push_string (const char *string)
6408 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6411 /* Push a member name on the stack. Printed as '.' STRING. */
6414 push_member_name (tree decl)
6416 const char *const string
6418 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6419 : _("<anonymous>"));
6420 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6423 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6426 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6428 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6431 /* Compute the maximum size in bytes of the printed spelling. */
6434 spelling_length (void)
6439 for (p = spelling_base; p < spelling; p++)
6441 if (p->kind == SPELLING_BOUNDS)
6444 size += strlen (p->u.s) + 1;
6450 /* Print the spelling to BUFFER and return it. */
6453 print_spelling (char *buffer)
6458 for (p = spelling_base; p < spelling; p++)
6459 if (p->kind == SPELLING_BOUNDS)
6461 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6467 if (p->kind == SPELLING_MEMBER)
6469 for (s = p->u.s; (*d = *s++); d++)
6476 /* Digest the parser output INIT as an initializer for type TYPE.
6477 Return a C expression of type TYPE to represent the initial value.
6479 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6481 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6483 If INIT is a string constant, STRICT_STRING is true if it is
6484 unparenthesized or we should not warn here for it being parenthesized.
6485 For other types of INIT, STRICT_STRING is not used.
6487 INIT_LOC is the location of the INIT.
6489 REQUIRE_CONSTANT requests an error if non-constant initializers or
6490 elements are seen. */
6493 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6494 bool null_pointer_constant, bool strict_string,
6495 int require_constant)
6497 enum tree_code code = TREE_CODE (type);
6498 tree inside_init = init;
6499 tree semantic_type = NULL_TREE;
6500 bool maybe_const = true;
6502 if (type == error_mark_node
6504 || init == error_mark_node
6505 || TREE_TYPE (init) == error_mark_node)
6506 return error_mark_node;
6508 STRIP_TYPE_NOPS (inside_init);
6510 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6512 semantic_type = TREE_TYPE (inside_init);
6513 inside_init = TREE_OPERAND (inside_init, 0);
6515 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6516 inside_init = decl_constant_value_for_optimization (inside_init);
6518 /* Initialization of an array of chars from a string constant
6519 optionally enclosed in braces. */
6521 if (code == ARRAY_TYPE && inside_init
6522 && TREE_CODE (inside_init) == STRING_CST)
6525 = (TYPE_ATOMIC (TREE_TYPE (type))
6526 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6528 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6529 /* Note that an array could be both an array of character type
6530 and an array of wchar_t if wchar_t is signed char or unsigned
6532 bool char_array = (typ1 == char_type_node
6533 || typ1 == signed_char_type_node
6534 || typ1 == unsigned_char_type_node);
6535 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6536 bool char16_array = !!comptypes (typ1, char16_type_node);
6537 bool char32_array = !!comptypes (typ1, char32_type_node);
6539 if (char_array || wchar_array || char16_array || char32_array)
6542 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6543 expr.value = inside_init;
6544 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6545 expr.original_type = NULL;
6546 maybe_warn_string_init (type, expr);
6548 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6549 pedwarn_init (init_loc, OPT_Wpedantic,
6550 "initialization of a flexible array member");
6552 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6553 TYPE_MAIN_VARIANT (type)))
6558 if (typ2 != char_type_node)
6560 error_init ("char-array initialized from wide string");
6561 return error_mark_node;
6566 if (typ2 == char_type_node)
6568 error_init ("wide character array initialized from non-wide "
6570 return error_mark_node;
6572 else if (!comptypes(typ1, typ2))
6574 error_init ("wide character array initialized from "
6575 "incompatible wide string");
6576 return error_mark_node;
6580 TREE_TYPE (inside_init) = type;
6581 if (TYPE_DOMAIN (type) != 0
6582 && TYPE_SIZE (type) != 0
6583 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6585 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6587 /* Subtract the size of a single (possibly wide) character
6588 because it's ok to ignore the terminating null char
6589 that is counted in the length of the constant. */
6590 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6592 - (TYPE_PRECISION (typ1)
6594 pedwarn_init (init_loc, 0,
6595 ("initializer-string for array of chars "
6597 else if (warn_cxx_compat
6598 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6599 warning_at (init_loc, OPT_Wc___compat,
6600 ("initializer-string for array chars "
6601 "is too long for C++"));
6606 else if (INTEGRAL_TYPE_P (typ1))
6608 error_init ("array of inappropriate type initialized "
6609 "from string constant");
6610 return error_mark_node;
6614 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6615 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6616 below and handle as a constructor. */
6617 if (code == VECTOR_TYPE
6618 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6619 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6620 && TREE_CONSTANT (inside_init))
6622 if (TREE_CODE (inside_init) == VECTOR_CST
6623 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6624 TYPE_MAIN_VARIANT (type)))
6627 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6629 unsigned HOST_WIDE_INT ix;
6631 bool constant_p = true;
6633 /* Iterate through elements and check if all constructor
6634 elements are *_CSTs. */
6635 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6636 if (!CONSTANT_CLASS_P (value))
6643 return build_vector_from_ctor (type,
6644 CONSTRUCTOR_ELTS (inside_init));
6648 if (warn_sequence_point)
6649 verify_sequence_points (inside_init);
6651 /* Any type can be initialized
6652 from an expression of the same type, optionally with braces. */
6654 if (inside_init && TREE_TYPE (inside_init) != 0
6655 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6656 TYPE_MAIN_VARIANT (type))
6657 || (code == ARRAY_TYPE
6658 && comptypes (TREE_TYPE (inside_init), type))
6659 || (code == VECTOR_TYPE
6660 && comptypes (TREE_TYPE (inside_init), type))
6661 || (code == POINTER_TYPE
6662 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6663 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6664 TREE_TYPE (type)))))
6666 if (code == POINTER_TYPE)
6668 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6670 if (TREE_CODE (inside_init) == STRING_CST
6671 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6672 inside_init = array_to_pointer_conversion
6673 (init_loc, inside_init);
6676 error_init ("invalid use of non-lvalue array");
6677 return error_mark_node;
6682 if (code == VECTOR_TYPE)
6683 /* Although the types are compatible, we may require a
6685 inside_init = convert (type, inside_init);
6687 if (require_constant
6688 && (code == VECTOR_TYPE || !flag_isoc99)
6689 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6691 /* As an extension, allow initializing objects with static storage
6692 duration with compound literals (which are then treated just as
6693 the brace enclosed list they contain). Also allow this for
6694 vectors, as we can only assign them with compound literals. */
6695 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6696 inside_init = DECL_INITIAL (decl);
6699 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6700 && TREE_CODE (inside_init) != CONSTRUCTOR)
6702 error_init ("array initialized from non-constant array expression");
6703 return error_mark_node;
6706 /* Compound expressions can only occur here if -Wpedantic or
6707 -pedantic-errors is specified. In the later case, we always want
6708 an error. In the former case, we simply want a warning. */
6709 if (require_constant && pedantic
6710 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6713 = valid_compound_expr_initializer (inside_init,
6714 TREE_TYPE (inside_init));
6715 if (inside_init == error_mark_node)
6716 error_init ("initializer element is not constant");
6718 pedwarn_init (init_loc, OPT_Wpedantic,
6719 "initializer element is not constant");
6720 if (flag_pedantic_errors)
6721 inside_init = error_mark_node;
6723 else if (require_constant
6724 && !initializer_constant_valid_p (inside_init,
6725 TREE_TYPE (inside_init)))
6727 error_init ("initializer element is not constant");
6728 inside_init = error_mark_node;
6730 else if (require_constant && !maybe_const)
6731 pedwarn_init (init_loc, 0,
6732 "initializer element is not a constant expression");
6734 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6735 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6736 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6737 type, inside_init, origtype,
6738 ic_init, null_pointer_constant,
6739 NULL_TREE, NULL_TREE, 0);
6743 /* Handle scalar types, including conversions. */
6745 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6746 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6747 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6749 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6750 && (TREE_CODE (init) == STRING_CST
6751 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6752 inside_init = init = array_to_pointer_conversion (init_loc, init);
6754 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6757 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6758 inside_init, origtype, ic_init,
6759 null_pointer_constant, NULL_TREE, NULL_TREE,
6762 /* Check to see if we have already given an error message. */
6763 if (inside_init == error_mark_node)
6765 else if (require_constant && !TREE_CONSTANT (inside_init))
6767 error_init ("initializer element is not constant");
6768 inside_init = error_mark_node;
6770 else if (require_constant
6771 && !initializer_constant_valid_p (inside_init,
6772 TREE_TYPE (inside_init)))
6774 error_init ("initializer element is not computable at load time");
6775 inside_init = error_mark_node;
6777 else if (require_constant && !maybe_const)
6778 pedwarn_init (init_loc, 0,
6779 "initializer element is not a constant expression");
6784 /* Come here only for records and arrays. */
6786 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6788 error_init ("variable-sized object may not be initialized");
6789 return error_mark_node;
6792 error_init ("invalid initializer");
6793 return error_mark_node;
6796 /* Handle initializers that use braces. */
6798 /* Type of object we are accumulating a constructor for.
6799 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6800 static tree constructor_type;
6802 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6804 static tree constructor_fields;
6806 /* For an ARRAY_TYPE, this is the specified index
6807 at which to store the next element we get. */
6808 static tree constructor_index;
6810 /* For an ARRAY_TYPE, this is the maximum index. */
6811 static tree constructor_max_index;
6813 /* For a RECORD_TYPE, this is the first field not yet written out. */
6814 static tree constructor_unfilled_fields;
6816 /* For an ARRAY_TYPE, this is the index of the first element
6817 not yet written out. */
6818 static tree constructor_unfilled_index;
6820 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6821 This is so we can generate gaps between fields, when appropriate. */
6822 static tree constructor_bit_index;
6824 /* If we are saving up the elements rather than allocating them,
6825 this is the list of elements so far (in reverse order,
6826 most recent first). */
6827 static vec<constructor_elt, va_gc> *constructor_elements;
6829 /* 1 if constructor should be incrementally stored into a constructor chain,
6830 0 if all the elements should be kept in AVL tree. */
6831 static int constructor_incremental;
6833 /* 1 if so far this constructor's elements are all compile-time constants. */
6834 static int constructor_constant;
6836 /* 1 if so far this constructor's elements are all valid address constants. */
6837 static int constructor_simple;
6839 /* 1 if this constructor has an element that cannot be part of a
6840 constant expression. */
6841 static int constructor_nonconst;
6843 /* 1 if this constructor is erroneous so far. */
6844 static int constructor_erroneous;
6846 /* Structure for managing pending initializer elements, organized as an
6851 struct init_node *left, *right;
6852 struct init_node *parent;
6859 /* Tree of pending elements at this constructor level.
6860 These are elements encountered out of order
6861 which belong at places we haven't reached yet in actually
6863 Will never hold tree nodes across GC runs. */
6864 static struct init_node *constructor_pending_elts;
6866 /* The SPELLING_DEPTH of this constructor. */
6867 static int constructor_depth;
6869 /* DECL node for which an initializer is being read.
6870 0 means we are reading a constructor expression
6871 such as (struct foo) {...}. */
6872 static tree constructor_decl;
6874 /* Nonzero if this is an initializer for a top-level decl. */
6875 static int constructor_top_level;
6877 /* Nonzero if there were any member designators in this initializer. */
6878 static int constructor_designated;
6880 /* Nesting depth of designator list. */
6881 static int designator_depth;
6883 /* Nonzero if there were diagnosed errors in this designator list. */
6884 static int designator_erroneous;
6887 /* This stack has a level for each implicit or explicit level of
6888 structuring in the initializer, including the outermost one. It
6889 saves the values of most of the variables above. */
6891 struct constructor_range_stack;
6893 struct constructor_stack
6895 struct constructor_stack *next;
6900 tree unfilled_index;
6901 tree unfilled_fields;
6903 vec<constructor_elt, va_gc> *elements;
6904 struct init_node *pending_elts;
6907 /* If value nonzero, this value should replace the entire
6908 constructor at this level. */
6909 struct c_expr replacement_value;
6910 struct constructor_range_stack *range_stack;
6921 static struct constructor_stack *constructor_stack;
6923 /* This stack represents designators from some range designator up to
6924 the last designator in the list. */
6926 struct constructor_range_stack
6928 struct constructor_range_stack *next, *prev;
6929 struct constructor_stack *stack;
6936 static struct constructor_range_stack *constructor_range_stack;
6938 /* This stack records separate initializers that are nested.
6939 Nested initializers can't happen in ANSI C, but GNU C allows them
6940 in cases like { ... (struct foo) { ... } ... }. */
6942 struct initializer_stack
6944 struct initializer_stack *next;
6946 struct constructor_stack *constructor_stack;
6947 struct constructor_range_stack *constructor_range_stack;
6948 vec<constructor_elt, va_gc> *elements;
6949 struct spelling *spelling;
6950 struct spelling *spelling_base;
6953 char require_constant_value;
6954 char require_constant_elements;
6957 static struct initializer_stack *initializer_stack;
6959 /* Prepare to parse and output the initializer for variable DECL. */
6962 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6965 struct initializer_stack *p = XNEW (struct initializer_stack);
6967 p->decl = constructor_decl;
6968 p->require_constant_value = require_constant_value;
6969 p->require_constant_elements = require_constant_elements;
6970 p->constructor_stack = constructor_stack;
6971 p->constructor_range_stack = constructor_range_stack;
6972 p->elements = constructor_elements;
6973 p->spelling = spelling;
6974 p->spelling_base = spelling_base;
6975 p->spelling_size = spelling_size;
6976 p->top_level = constructor_top_level;
6977 p->next = initializer_stack;
6978 initializer_stack = p;
6980 constructor_decl = decl;
6981 constructor_designated = 0;
6982 constructor_top_level = top_level;
6984 if (decl != 0 && decl != error_mark_node)
6986 require_constant_value = TREE_STATIC (decl);
6987 require_constant_elements
6988 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6989 /* For a scalar, you can always use any value to initialize,
6990 even within braces. */
6991 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6992 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6993 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6994 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6995 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6999 require_constant_value = 0;
7000 require_constant_elements = 0;
7001 locus = _("(anonymous)");
7004 constructor_stack = 0;
7005 constructor_range_stack = 0;
7007 missing_braces_mentioned = 0;
7011 RESTORE_SPELLING_DEPTH (0);
7014 push_string (locus);
7020 struct initializer_stack *p = initializer_stack;
7022 /* Free the whole constructor stack of this initializer. */
7023 while (constructor_stack)
7025 struct constructor_stack *q = constructor_stack;
7026 constructor_stack = q->next;
7030 gcc_assert (!constructor_range_stack);
7032 /* Pop back to the data of the outer initializer (if any). */
7033 free (spelling_base);
7035 constructor_decl = p->decl;
7036 require_constant_value = p->require_constant_value;
7037 require_constant_elements = p->require_constant_elements;
7038 constructor_stack = p->constructor_stack;
7039 constructor_range_stack = p->constructor_range_stack;
7040 constructor_elements = p->elements;
7041 spelling = p->spelling;
7042 spelling_base = p->spelling_base;
7043 spelling_size = p->spelling_size;
7044 constructor_top_level = p->top_level;
7045 initializer_stack = p->next;
7049 /* Call here when we see the initializer is surrounded by braces.
7050 This is instead of a call to push_init_level;
7051 it is matched by a call to pop_init_level.
7053 TYPE is the type to initialize, for a constructor expression.
7054 For an initializer for a decl, TYPE is zero. */
7057 really_start_incremental_init (tree type)
7059 struct constructor_stack *p = XNEW (struct constructor_stack);
7062 type = TREE_TYPE (constructor_decl);
7064 if (TREE_CODE (type) == VECTOR_TYPE
7065 && TYPE_VECTOR_OPAQUE (type))
7066 error ("opaque vector types cannot be initialized");
7068 p->type = constructor_type;
7069 p->fields = constructor_fields;
7070 p->index = constructor_index;
7071 p->max_index = constructor_max_index;
7072 p->unfilled_index = constructor_unfilled_index;
7073 p->unfilled_fields = constructor_unfilled_fields;
7074 p->bit_index = constructor_bit_index;
7075 p->elements = constructor_elements;
7076 p->constant = constructor_constant;
7077 p->simple = constructor_simple;
7078 p->nonconst = constructor_nonconst;
7079 p->erroneous = constructor_erroneous;
7080 p->pending_elts = constructor_pending_elts;
7081 p->depth = constructor_depth;
7082 p->replacement_value.value = 0;
7083 p->replacement_value.original_code = ERROR_MARK;
7084 p->replacement_value.original_type = NULL;
7088 p->incremental = constructor_incremental;
7089 p->designated = constructor_designated;
7091 constructor_stack = p;
7093 constructor_constant = 1;
7094 constructor_simple = 1;
7095 constructor_nonconst = 0;
7096 constructor_depth = SPELLING_DEPTH ();
7097 constructor_elements = NULL;
7098 constructor_pending_elts = 0;
7099 constructor_type = type;
7100 constructor_incremental = 1;
7101 constructor_designated = 0;
7102 designator_depth = 0;
7103 designator_erroneous = 0;
7105 if (TREE_CODE (constructor_type) == RECORD_TYPE
7106 || TREE_CODE (constructor_type) == UNION_TYPE)
7108 constructor_fields = TYPE_FIELDS (constructor_type);
7109 /* Skip any nameless bit fields at the beginning. */
7110 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7111 && DECL_NAME (constructor_fields) == 0)
7112 constructor_fields = DECL_CHAIN (constructor_fields);
7114 constructor_unfilled_fields = constructor_fields;
7115 constructor_bit_index = bitsize_zero_node;
7117 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7119 if (TYPE_DOMAIN (constructor_type))
7121 constructor_max_index
7122 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7124 /* Detect non-empty initializations of zero-length arrays. */
7125 if (constructor_max_index == NULL_TREE
7126 && TYPE_SIZE (constructor_type))
7127 constructor_max_index = integer_minus_one_node;
7129 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7130 to initialize VLAs will cause a proper error; avoid tree
7131 checking errors as well by setting a safe value. */
7132 if (constructor_max_index
7133 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7134 constructor_max_index = integer_minus_one_node;
7137 = convert (bitsizetype,
7138 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7142 constructor_index = bitsize_zero_node;
7143 constructor_max_index = NULL_TREE;
7146 constructor_unfilled_index = constructor_index;
7148 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7150 /* Vectors are like simple fixed-size arrays. */
7151 constructor_max_index =
7152 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7153 constructor_index = bitsize_zero_node;
7154 constructor_unfilled_index = constructor_index;
7158 /* Handle the case of int x = {5}; */
7159 constructor_fields = constructor_type;
7160 constructor_unfilled_fields = constructor_type;
7164 /* Push down into a subobject, for initialization.
7165 If this is for an explicit set of braces, IMPLICIT is 0.
7166 If it is because the next element belongs at a lower level,
7167 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7170 push_init_level (int implicit, struct obstack * braced_init_obstack)
7172 struct constructor_stack *p;
7173 tree value = NULL_TREE;
7175 /* If we've exhausted any levels that didn't have braces,
7176 pop them now. If implicit == 1, this will have been done in
7177 process_init_element; do not repeat it here because in the case
7178 of excess initializers for an empty aggregate this leads to an
7179 infinite cycle of popping a level and immediately recreating
7183 while (constructor_stack->implicit)
7185 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7186 || TREE_CODE (constructor_type) == UNION_TYPE)
7187 && constructor_fields == 0)
7188 process_init_element (input_location,
7189 pop_init_level (1, braced_init_obstack),
7190 true, braced_init_obstack);
7191 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7192 && constructor_max_index
7193 && tree_int_cst_lt (constructor_max_index,
7195 process_init_element (input_location,
7196 pop_init_level (1, braced_init_obstack),
7197 true, braced_init_obstack);
7203 /* Unless this is an explicit brace, we need to preserve previous
7207 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7208 || TREE_CODE (constructor_type) == UNION_TYPE)
7209 && constructor_fields)
7210 value = find_init_member (constructor_fields, braced_init_obstack);
7211 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7212 value = find_init_member (constructor_index, braced_init_obstack);
7215 p = XNEW (struct constructor_stack);
7216 p->type = constructor_type;
7217 p->fields = constructor_fields;
7218 p->index = constructor_index;
7219 p->max_index = constructor_max_index;
7220 p->unfilled_index = constructor_unfilled_index;
7221 p->unfilled_fields = constructor_unfilled_fields;
7222 p->bit_index = constructor_bit_index;
7223 p->elements = constructor_elements;
7224 p->constant = constructor_constant;
7225 p->simple = constructor_simple;
7226 p->nonconst = constructor_nonconst;
7227 p->erroneous = constructor_erroneous;
7228 p->pending_elts = constructor_pending_elts;
7229 p->depth = constructor_depth;
7230 p->replacement_value.value = 0;
7231 p->replacement_value.original_code = ERROR_MARK;
7232 p->replacement_value.original_type = NULL;
7233 p->implicit = implicit;
7235 p->incremental = constructor_incremental;
7236 p->designated = constructor_designated;
7237 p->next = constructor_stack;
7239 constructor_stack = p;
7241 constructor_constant = 1;
7242 constructor_simple = 1;
7243 constructor_nonconst = 0;
7244 constructor_depth = SPELLING_DEPTH ();
7245 constructor_elements = NULL;
7246 constructor_incremental = 1;
7247 constructor_designated = 0;
7248 constructor_pending_elts = 0;
7251 p->range_stack = constructor_range_stack;
7252 constructor_range_stack = 0;
7253 designator_depth = 0;
7254 designator_erroneous = 0;
7257 /* Don't die if an entire brace-pair level is superfluous
7258 in the containing level. */
7259 if (constructor_type == 0)
7261 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7262 || TREE_CODE (constructor_type) == UNION_TYPE)
7264 /* Don't die if there are extra init elts at the end. */
7265 if (constructor_fields == 0)
7266 constructor_type = 0;
7269 constructor_type = TREE_TYPE (constructor_fields);
7270 push_member_name (constructor_fields);
7271 constructor_depth++;
7273 /* If upper initializer is designated, then mark this as
7274 designated too to prevent bogus warnings. */
7275 constructor_designated = p->designated;
7277 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7279 constructor_type = TREE_TYPE (constructor_type);
7280 push_array_bounds (tree_to_uhwi (constructor_index));
7281 constructor_depth++;
7284 if (constructor_type == 0)
7286 error_init ("extra brace group at end of initializer");
7287 constructor_fields = 0;
7288 constructor_unfilled_fields = 0;
7292 if (value && TREE_CODE (value) == CONSTRUCTOR)
7294 constructor_constant = TREE_CONSTANT (value);
7295 constructor_simple = TREE_STATIC (value);
7296 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7297 constructor_elements = CONSTRUCTOR_ELTS (value);
7298 if (!vec_safe_is_empty (constructor_elements)
7299 && (TREE_CODE (constructor_type) == RECORD_TYPE
7300 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7301 set_nonincremental_init (braced_init_obstack);
7304 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
7306 missing_braces_mentioned = 1;
7307 warning_init (input_location, OPT_Wmissing_braces,
7308 "missing braces around initializer");
7311 if (TREE_CODE (constructor_type) == RECORD_TYPE
7312 || TREE_CODE (constructor_type) == UNION_TYPE)
7314 constructor_fields = TYPE_FIELDS (constructor_type);
7315 /* Skip any nameless bit fields at the beginning. */
7316 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7317 && DECL_NAME (constructor_fields) == 0)
7318 constructor_fields = DECL_CHAIN (constructor_fields);
7320 constructor_unfilled_fields = constructor_fields;
7321 constructor_bit_index = bitsize_zero_node;
7323 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7325 /* Vectors are like simple fixed-size arrays. */
7326 constructor_max_index =
7327 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7328 constructor_index = bitsize_int (0);
7329 constructor_unfilled_index = constructor_index;
7331 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7333 if (TYPE_DOMAIN (constructor_type))
7335 constructor_max_index
7336 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7338 /* Detect non-empty initializations of zero-length arrays. */
7339 if (constructor_max_index == NULL_TREE
7340 && TYPE_SIZE (constructor_type))
7341 constructor_max_index = integer_minus_one_node;
7343 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7344 to initialize VLAs will cause a proper error; avoid tree
7345 checking errors as well by setting a safe value. */
7346 if (constructor_max_index
7347 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7348 constructor_max_index = integer_minus_one_node;
7351 = convert (bitsizetype,
7352 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7355 constructor_index = bitsize_zero_node;
7357 constructor_unfilled_index = constructor_index;
7358 if (value && TREE_CODE (value) == STRING_CST)
7360 /* We need to split the char/wchar array into individual
7361 characters, so that we don't have to special case it
7363 set_nonincremental_init_from_string (value, braced_init_obstack);
7368 if (constructor_type != error_mark_node)
7369 warning_init (input_location, 0, "braces around scalar initializer");
7370 constructor_fields = constructor_type;
7371 constructor_unfilled_fields = constructor_type;
7375 /* At the end of an implicit or explicit brace level,
7376 finish up that level of constructor. If a single expression
7377 with redundant braces initialized that level, return the
7378 c_expr structure for that expression. Otherwise, the original_code
7379 element is set to ERROR_MARK.
7380 If we were outputting the elements as they are read, return 0 as the value
7381 from inner levels (process_init_element ignores that),
7382 but return error_mark_node as the value from the outermost level
7383 (that's what we want to put in DECL_INITIAL).
7384 Otherwise, return a CONSTRUCTOR expression as the value. */
7387 pop_init_level (int implicit, struct obstack * braced_init_obstack)
7389 struct constructor_stack *p;
7392 ret.original_code = ERROR_MARK;
7393 ret.original_type = NULL;
7397 /* When we come to an explicit close brace,
7398 pop any inner levels that didn't have explicit braces. */
7399 while (constructor_stack->implicit)
7400 process_init_element (input_location,
7401 pop_init_level (1, braced_init_obstack),
7402 true, braced_init_obstack);
7403 gcc_assert (!constructor_range_stack);
7406 /* Now output all pending elements. */
7407 constructor_incremental = 1;
7408 output_pending_init_elements (1, braced_init_obstack);
7410 p = constructor_stack;
7412 /* Error for initializing a flexible array member, or a zero-length
7413 array member in an inappropriate context. */
7414 if (constructor_type && constructor_fields
7415 && TREE_CODE (constructor_type) == ARRAY_TYPE
7416 && TYPE_DOMAIN (constructor_type)
7417 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7419 /* Silently discard empty initializations. The parser will
7420 already have pedwarned for empty brackets. */
7421 if (integer_zerop (constructor_unfilled_index))
7422 constructor_type = NULL_TREE;
7425 gcc_assert (!TYPE_SIZE (constructor_type));
7427 if (constructor_depth > 2)
7428 error_init ("initialization of flexible array member in a nested context");
7430 pedwarn_init (input_location, OPT_Wpedantic,
7431 "initialization of a flexible array member");
7433 /* We have already issued an error message for the existence
7434 of a flexible array member not at the end of the structure.
7435 Discard the initializer so that we do not die later. */
7436 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7437 constructor_type = NULL_TREE;
7441 /* Warn when some struct elements are implicitly initialized to zero. */
7442 if (warn_missing_field_initializers
7444 && TREE_CODE (constructor_type) == RECORD_TYPE
7445 && constructor_unfilled_fields)
7447 bool constructor_zeroinit =
7448 (vec_safe_length (constructor_elements) == 1
7449 && integer_zerop ((*constructor_elements)[0].value));
7451 /* Do not warn for flexible array members or zero-length arrays. */
7452 while (constructor_unfilled_fields
7453 && (!DECL_SIZE (constructor_unfilled_fields)
7454 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7455 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7457 if (constructor_unfilled_fields
7458 /* Do not warn if this level of the initializer uses member
7459 designators; it is likely to be deliberate. */
7460 && !constructor_designated
7461 /* Do not warn about initializing with ` = {0}'. */
7462 && !constructor_zeroinit)
7464 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7465 "missing initializer for field %qD of %qT",
7466 constructor_unfilled_fields,
7468 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7469 "%qD declared here", constructor_unfilled_fields);
7473 /* Pad out the end of the structure. */
7474 if (p->replacement_value.value)
7475 /* If this closes a superfluous brace pair,
7476 just pass out the element between them. */
7477 ret = p->replacement_value;
7478 else if (constructor_type == 0)
7480 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7481 && TREE_CODE (constructor_type) != UNION_TYPE
7482 && TREE_CODE (constructor_type) != ARRAY_TYPE
7483 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7485 /* A nonincremental scalar initializer--just return
7486 the element, after verifying there is just one. */
7487 if (vec_safe_is_empty (constructor_elements))
7489 if (!constructor_erroneous)
7490 error_init ("empty scalar initializer");
7491 ret.value = error_mark_node;
7493 else if (vec_safe_length (constructor_elements) != 1)
7495 error_init ("extra elements in scalar initializer");
7496 ret.value = (*constructor_elements)[0].value;
7499 ret.value = (*constructor_elements)[0].value;
7503 if (constructor_erroneous)
7504 ret.value = error_mark_node;
7507 ret.value = build_constructor (constructor_type,
7508 constructor_elements);
7509 if (constructor_constant)
7510 TREE_CONSTANT (ret.value) = 1;
7511 if (constructor_constant && constructor_simple)
7512 TREE_STATIC (ret.value) = 1;
7513 if (constructor_nonconst)
7514 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7518 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7520 if (constructor_nonconst)
7521 ret.original_code = C_MAYBE_CONST_EXPR;
7522 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7523 ret.original_code = ERROR_MARK;
7526 constructor_type = p->type;
7527 constructor_fields = p->fields;
7528 constructor_index = p->index;
7529 constructor_max_index = p->max_index;
7530 constructor_unfilled_index = p->unfilled_index;
7531 constructor_unfilled_fields = p->unfilled_fields;
7532 constructor_bit_index = p->bit_index;
7533 constructor_elements = p->elements;
7534 constructor_constant = p->constant;
7535 constructor_simple = p->simple;
7536 constructor_nonconst = p->nonconst;
7537 constructor_erroneous = p->erroneous;
7538 constructor_incremental = p->incremental;
7539 constructor_designated = p->designated;
7540 constructor_pending_elts = p->pending_elts;
7541 constructor_depth = p->depth;
7543 constructor_range_stack = p->range_stack;
7544 RESTORE_SPELLING_DEPTH (constructor_depth);
7546 constructor_stack = p->next;
7549 if (ret.value == 0 && constructor_stack == 0)
7550 ret.value = error_mark_node;
7554 /* Common handling for both array range and field name designators.
7555 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7558 set_designator (int array, struct obstack * braced_init_obstack)
7561 enum tree_code subcode;
7563 /* Don't die if an entire brace-pair level is superfluous
7564 in the containing level. */
7565 if (constructor_type == 0)
7568 /* If there were errors in this designator list already, bail out
7570 if (designator_erroneous)
7573 if (!designator_depth)
7575 gcc_assert (!constructor_range_stack);
7577 /* Designator list starts at the level of closest explicit
7579 while (constructor_stack->implicit)
7580 process_init_element (input_location,
7581 pop_init_level (1, braced_init_obstack),
7582 true, braced_init_obstack);
7583 constructor_designated = 1;
7587 switch (TREE_CODE (constructor_type))
7591 subtype = TREE_TYPE (constructor_fields);
7592 if (subtype != error_mark_node)
7593 subtype = TYPE_MAIN_VARIANT (subtype);
7596 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7602 subcode = TREE_CODE (subtype);
7603 if (array && subcode != ARRAY_TYPE)
7605 error_init ("array index in non-array initializer");
7608 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7610 error_init ("field name not in record or union initializer");
7614 constructor_designated = 1;
7615 push_init_level (2, braced_init_obstack);
7619 /* If there are range designators in designator list, push a new designator
7620 to constructor_range_stack. RANGE_END is end of such stack range or
7621 NULL_TREE if there is no range designator at this level. */
7624 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7626 struct constructor_range_stack *p;
7628 p = (struct constructor_range_stack *)
7629 obstack_alloc (braced_init_obstack,
7630 sizeof (struct constructor_range_stack));
7631 p->prev = constructor_range_stack;
7633 p->fields = constructor_fields;
7634 p->range_start = constructor_index;
7635 p->index = constructor_index;
7636 p->stack = constructor_stack;
7637 p->range_end = range_end;
7638 if (constructor_range_stack)
7639 constructor_range_stack->next = p;
7640 constructor_range_stack = p;
7643 /* Within an array initializer, specify the next index to be initialized.
7644 FIRST is that index. If LAST is nonzero, then initialize a range
7645 of indices, running from FIRST through LAST. */
7648 set_init_index (tree first, tree last,
7649 struct obstack * braced_init_obstack)
7651 if (set_designator (1, braced_init_obstack))
7654 designator_erroneous = 1;
7656 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7657 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7659 error_init ("array index in initializer not of integer type");
7663 if (TREE_CODE (first) != INTEGER_CST)
7665 first = c_fully_fold (first, false, NULL);
7666 if (TREE_CODE (first) == INTEGER_CST)
7667 pedwarn_init (input_location, OPT_Wpedantic,
7668 "array index in initializer is not "
7669 "an integer constant expression");
7672 if (last && TREE_CODE (last) != INTEGER_CST)
7674 last = c_fully_fold (last, false, NULL);
7675 if (TREE_CODE (last) == INTEGER_CST)
7676 pedwarn_init (input_location, OPT_Wpedantic,
7677 "array index in initializer is not "
7678 "an integer constant expression");
7681 if (TREE_CODE (first) != INTEGER_CST)
7682 error_init ("nonconstant array index in initializer");
7683 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7684 error_init ("nonconstant array index in initializer");
7685 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7686 error_init ("array index in non-array initializer");
7687 else if (tree_int_cst_sgn (first) == -1)
7688 error_init ("array index in initializer exceeds array bounds");
7689 else if (constructor_max_index
7690 && tree_int_cst_lt (constructor_max_index, first))
7691 error_init ("array index in initializer exceeds array bounds");
7694 constant_expression_warning (first);
7696 constant_expression_warning (last);
7697 constructor_index = convert (bitsizetype, first);
7698 if (tree_int_cst_lt (constructor_index, first))
7700 constructor_index = copy_node (constructor_index);
7701 TREE_OVERFLOW (constructor_index) = 1;
7706 if (tree_int_cst_equal (first, last))
7708 else if (tree_int_cst_lt (last, first))
7710 error_init ("empty index range in initializer");
7715 last = convert (bitsizetype, last);
7716 if (constructor_max_index != 0
7717 && tree_int_cst_lt (constructor_max_index, last))
7719 error_init ("array index range in initializer exceeds array bounds");
7726 designator_erroneous = 0;
7727 if (constructor_range_stack || last)
7728 push_range_stack (last, braced_init_obstack);
7732 /* Within a struct initializer, specify the next field to be initialized. */
7735 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7739 if (set_designator (0, braced_init_obstack))
7742 designator_erroneous = 1;
7744 if (TREE_CODE (constructor_type) != RECORD_TYPE
7745 && TREE_CODE (constructor_type) != UNION_TYPE)
7747 error_init ("field name not in record or union initializer");
7751 field = lookup_field (constructor_type, fieldname);
7754 error ("unknown field %qE specified in initializer", fieldname);
7758 constructor_fields = TREE_VALUE (field);
7760 designator_erroneous = 0;
7761 if (constructor_range_stack)
7762 push_range_stack (NULL_TREE, braced_init_obstack);
7763 field = TREE_CHAIN (field);
7766 if (set_designator (0, braced_init_obstack))
7770 while (field != NULL_TREE);
7773 /* Add a new initializer to the tree of pending initializers. PURPOSE
7774 identifies the initializer, either array index or field in a structure.
7775 VALUE is the value of that index or field. If ORIGTYPE is not
7776 NULL_TREE, it is the original type of VALUE.
7778 IMPLICIT is true if value comes from pop_init_level (1),
7779 the new initializer has been merged with the existing one
7780 and thus no warnings should be emitted about overriding an
7781 existing initializer. */
7784 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7785 bool implicit, struct obstack *braced_init_obstack)
7787 struct init_node *p, **q, *r;
7789 q = &constructor_pending_elts;
7792 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7797 if (tree_int_cst_lt (purpose, p->purpose))
7799 else if (tree_int_cst_lt (p->purpose, purpose))
7805 if (TREE_SIDE_EFFECTS (p->value))
7806 warning_init (loc, 0,
7807 "initialized field with side-effects "
7809 else if (warn_override_init)
7810 warning_init (loc, OPT_Woverride_init,
7811 "initialized field overwritten");
7814 p->origtype = origtype;
7823 bitpos = bit_position (purpose);
7827 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7829 else if (p->purpose != purpose)
7835 if (TREE_SIDE_EFFECTS (p->value))
7836 warning_init (loc, 0,
7837 "initialized field with side-effects "
7839 else if (warn_override_init)
7840 warning_init (loc, OPT_Woverride_init,
7841 "initialized field overwritten");
7844 p->origtype = origtype;
7850 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7851 sizeof (struct init_node));
7852 r->purpose = purpose;
7854 r->origtype = origtype;
7864 struct init_node *s;
7868 if (p->balance == 0)
7870 else if (p->balance < 0)
7877 p->left->parent = p;
7894 constructor_pending_elts = r;
7899 struct init_node *t = r->right;
7903 r->right->parent = r;
7908 p->left->parent = p;
7911 p->balance = t->balance < 0;
7912 r->balance = -(t->balance > 0);
7927 constructor_pending_elts = t;
7933 /* p->balance == +1; growth of left side balances the node. */
7938 else /* r == p->right */
7940 if (p->balance == 0)
7941 /* Growth propagation from right side. */
7943 else if (p->balance > 0)
7950 p->right->parent = p;
7967 constructor_pending_elts = r;
7969 else /* r->balance == -1 */
7972 struct init_node *t = r->left;
7976 r->left->parent = r;
7981 p->right->parent = p;
7984 r->balance = (t->balance < 0);
7985 p->balance = -(t->balance > 0);
8000 constructor_pending_elts = t;
8006 /* p->balance == -1; growth of right side balances the node. */
8017 /* Build AVL tree from a sorted chain. */
8020 set_nonincremental_init (struct obstack * braced_init_obstack)
8022 unsigned HOST_WIDE_INT ix;
8025 if (TREE_CODE (constructor_type) != RECORD_TYPE
8026 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8029 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8030 add_pending_init (input_location, index, value, NULL_TREE, true,
8031 braced_init_obstack);
8032 constructor_elements = NULL;
8033 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8035 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8036 /* Skip any nameless bit fields at the beginning. */
8037 while (constructor_unfilled_fields != 0
8038 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8039 && DECL_NAME (constructor_unfilled_fields) == 0)
8040 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8043 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8045 if (TYPE_DOMAIN (constructor_type))
8046 constructor_unfilled_index
8047 = convert (bitsizetype,
8048 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8050 constructor_unfilled_index = bitsize_zero_node;
8052 constructor_incremental = 0;
8055 /* Build AVL tree from a string constant. */
8058 set_nonincremental_init_from_string (tree str,
8059 struct obstack * braced_init_obstack)
8061 tree value, purpose, type;
8062 HOST_WIDE_INT val[2];
8063 const char *p, *end;
8064 int byte, wchar_bytes, charwidth, bitpos;
8066 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8068 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8069 charwidth = TYPE_PRECISION (char_type_node);
8070 type = TREE_TYPE (constructor_type);
8071 p = TREE_STRING_POINTER (str);
8072 end = p + TREE_STRING_LENGTH (str);
8074 for (purpose = bitsize_zero_node;
8076 && !(constructor_max_index
8077 && tree_int_cst_lt (constructor_max_index, purpose));
8078 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8080 if (wchar_bytes == 1)
8082 val[1] = (unsigned char) *p++;
8089 for (byte = 0; byte < wchar_bytes; byte++)
8091 if (BYTES_BIG_ENDIAN)
8092 bitpos = (wchar_bytes - byte - 1) * charwidth;
8094 bitpos = byte * charwidth;
8095 val[bitpos < HOST_BITS_PER_WIDE_INT]
8096 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8097 << (bitpos % HOST_BITS_PER_WIDE_INT);
8101 if (!TYPE_UNSIGNED (type))
8103 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8104 if (bitpos < HOST_BITS_PER_WIDE_INT)
8106 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8108 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
8112 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8117 else if (val[0] & (((HOST_WIDE_INT) 1)
8118 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8119 val[0] |= ((HOST_WIDE_INT) -1)
8120 << (bitpos - HOST_BITS_PER_WIDE_INT);
8123 value = build_int_cst_wide (type, val[1], val[0]);
8124 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8125 braced_init_obstack);
8128 constructor_incremental = 0;
8131 /* Return value of FIELD in pending initializer or zero if the field was
8132 not initialized yet. */
8135 find_init_member (tree field, struct obstack * braced_init_obstack)
8137 struct init_node *p;
8139 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8141 if (constructor_incremental
8142 && tree_int_cst_lt (field, constructor_unfilled_index))
8143 set_nonincremental_init (braced_init_obstack);
8145 p = constructor_pending_elts;
8148 if (tree_int_cst_lt (field, p->purpose))
8150 else if (tree_int_cst_lt (p->purpose, field))
8156 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8158 tree bitpos = bit_position (field);
8160 if (constructor_incremental
8161 && (!constructor_unfilled_fields
8162 || tree_int_cst_lt (bitpos,
8163 bit_position (constructor_unfilled_fields))))
8164 set_nonincremental_init (braced_init_obstack);
8166 p = constructor_pending_elts;
8169 if (field == p->purpose)
8171 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8177 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8179 if (!vec_safe_is_empty (constructor_elements)
8180 && (constructor_elements->last ().index == field))
8181 return constructor_elements->last ().value;
8186 /* "Output" the next constructor element.
8187 At top level, really output it to assembler code now.
8188 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8189 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8190 TYPE is the data type that the containing data type wants here.
8191 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8192 If VALUE is a string constant, STRICT_STRING is true if it is
8193 unparenthesized or we should not warn here for it being parenthesized.
8194 For other types of VALUE, STRICT_STRING is not used.
8196 PENDING if non-nil means output pending elements that belong
8197 right after this element. (PENDING is normally 1;
8198 it is 0 while outputting pending elements, to avoid recursion.)
8200 IMPLICIT is true if value comes from pop_init_level (1),
8201 the new initializer has been merged with the existing one
8202 and thus no warnings should be emitted about overriding an
8203 existing initializer. */
8206 output_init_element (location_t loc, tree value, tree origtype,
8207 bool strict_string, tree type, tree field, int pending,
8208 bool implicit, struct obstack * braced_init_obstack)
8210 tree semantic_type = NULL_TREE;
8211 bool maybe_const = true;
8214 if (type == error_mark_node || value == error_mark_node)
8216 constructor_erroneous = 1;
8219 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8220 && (TREE_CODE (value) == STRING_CST
8221 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8222 && !(TREE_CODE (value) == STRING_CST
8223 && TREE_CODE (type) == ARRAY_TYPE
8224 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8225 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8226 TYPE_MAIN_VARIANT (type)))
8227 value = array_to_pointer_conversion (input_location, value);
8229 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8230 && require_constant_value && !flag_isoc99 && pending)
8232 /* As an extension, allow initializing objects with static storage
8233 duration with compound literals (which are then treated just as
8234 the brace enclosed list they contain). */
8235 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8236 value = DECL_INITIAL (decl);
8239 npc = null_pointer_constant_p (value);
8240 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8242 semantic_type = TREE_TYPE (value);
8243 value = TREE_OPERAND (value, 0);
8245 value = c_fully_fold (value, require_constant_value, &maybe_const);
8247 if (value == error_mark_node)
8248 constructor_erroneous = 1;
8249 else if (!TREE_CONSTANT (value))
8250 constructor_constant = 0;
8251 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8252 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8253 || TREE_CODE (constructor_type) == UNION_TYPE)
8254 && DECL_C_BIT_FIELD (field)
8255 && TREE_CODE (value) != INTEGER_CST))
8256 constructor_simple = 0;
8258 constructor_nonconst = 1;
8260 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8262 if (require_constant_value)
8264 error_init ("initializer element is not constant");
8265 value = error_mark_node;
8267 else if (require_constant_elements)
8268 pedwarn (loc, OPT_Wpedantic,
8269 "initializer element is not computable at load time");
8271 else if (!maybe_const
8272 && (require_constant_value || require_constant_elements))
8273 pedwarn_init (loc, OPT_Wpedantic,
8274 "initializer element is not a constant expression");
8276 /* Issue -Wc++-compat warnings about initializing a bitfield with
8279 && field != NULL_TREE
8280 && TREE_CODE (field) == FIELD_DECL
8281 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8282 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8283 != TYPE_MAIN_VARIANT (type))
8284 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8286 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8287 if (checktype != error_mark_node
8288 && (TYPE_MAIN_VARIANT (checktype)
8289 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8290 warning_init (loc, OPT_Wc___compat,
8291 "enum conversion in initialization is invalid in C++");
8294 /* If this field is empty (and not at the end of structure),
8295 don't do anything other than checking the initializer. */
8297 && (TREE_TYPE (field) == error_mark_node
8298 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8299 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8300 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8301 || DECL_CHAIN (field)))))
8305 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8306 value = digest_init (loc, type, value, origtype, npc, strict_string,
8307 require_constant_value);
8308 if (value == error_mark_node)
8310 constructor_erroneous = 1;
8313 if (require_constant_value || require_constant_elements)
8314 constant_expression_warning (value);
8316 /* If this element doesn't come next in sequence,
8317 put it on constructor_pending_elts. */
8318 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8319 && (!constructor_incremental
8320 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8322 if (constructor_incremental
8323 && tree_int_cst_lt (field, constructor_unfilled_index))
8324 set_nonincremental_init (braced_init_obstack);
8326 add_pending_init (loc, field, value, origtype, implicit,
8327 braced_init_obstack);
8330 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8331 && (!constructor_incremental
8332 || field != constructor_unfilled_fields))
8334 /* We do this for records but not for unions. In a union,
8335 no matter which field is specified, it can be initialized
8336 right away since it starts at the beginning of the union. */
8337 if (constructor_incremental)
8339 if (!constructor_unfilled_fields)
8340 set_nonincremental_init (braced_init_obstack);
8343 tree bitpos, unfillpos;
8345 bitpos = bit_position (field);
8346 unfillpos = bit_position (constructor_unfilled_fields);
8348 if (tree_int_cst_lt (bitpos, unfillpos))
8349 set_nonincremental_init (braced_init_obstack);
8353 add_pending_init (loc, field, value, origtype, implicit,
8354 braced_init_obstack);
8357 else if (TREE_CODE (constructor_type) == UNION_TYPE
8358 && !vec_safe_is_empty (constructor_elements))
8362 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8363 warning_init (loc, 0,
8364 "initialized field with side-effects overwritten");
8365 else if (warn_override_init)
8366 warning_init (loc, OPT_Woverride_init,
8367 "initialized field overwritten");
8370 /* We can have just one union field set. */
8371 constructor_elements = NULL;
8374 /* Otherwise, output this element either to
8375 constructor_elements or to the assembler file. */
8377 constructor_elt celt = {field, value};
8378 vec_safe_push (constructor_elements, celt);
8380 /* Advance the variable that indicates sequential elements output. */
8381 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8382 constructor_unfilled_index
8383 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8385 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8387 constructor_unfilled_fields
8388 = DECL_CHAIN (constructor_unfilled_fields);
8390 /* Skip any nameless bit fields. */
8391 while (constructor_unfilled_fields != 0
8392 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8393 && DECL_NAME (constructor_unfilled_fields) == 0)
8394 constructor_unfilled_fields =
8395 DECL_CHAIN (constructor_unfilled_fields);
8397 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8398 constructor_unfilled_fields = 0;
8400 /* Now output any pending elements which have become next. */
8402 output_pending_init_elements (0, braced_init_obstack);
8405 /* Output any pending elements which have become next.
8406 As we output elements, constructor_unfilled_{fields,index}
8407 advances, which may cause other elements to become next;
8408 if so, they too are output.
8410 If ALL is 0, we return when there are
8411 no more pending elements to output now.
8413 If ALL is 1, we output space as necessary so that
8414 we can output all the pending elements. */
8416 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8418 struct init_node *elt = constructor_pending_elts;
8423 /* Look through the whole pending tree.
8424 If we find an element that should be output now,
8425 output it. Otherwise, set NEXT to the element
8426 that comes first among those still pending. */
8431 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8433 if (tree_int_cst_equal (elt->purpose,
8434 constructor_unfilled_index))
8435 output_init_element (input_location, elt->value, elt->origtype,
8436 true, TREE_TYPE (constructor_type),
8437 constructor_unfilled_index, 0, false,
8438 braced_init_obstack);
8439 else if (tree_int_cst_lt (constructor_unfilled_index,
8442 /* Advance to the next smaller node. */
8447 /* We have reached the smallest node bigger than the
8448 current unfilled index. Fill the space first. */
8449 next = elt->purpose;
8455 /* Advance to the next bigger node. */
8460 /* We have reached the biggest node in a subtree. Find
8461 the parent of it, which is the next bigger node. */
8462 while (elt->parent && elt->parent->right == elt)
8465 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8468 next = elt->purpose;
8474 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8475 || TREE_CODE (constructor_type) == UNION_TYPE)
8477 tree ctor_unfilled_bitpos, elt_bitpos;
8479 /* If the current record is complete we are done. */
8480 if (constructor_unfilled_fields == 0)
8483 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8484 elt_bitpos = bit_position (elt->purpose);
8485 /* We can't compare fields here because there might be empty
8486 fields in between. */
8487 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8489 constructor_unfilled_fields = elt->purpose;
8490 output_init_element (input_location, elt->value, elt->origtype,
8491 true, TREE_TYPE (elt->purpose),
8492 elt->purpose, 0, false,
8493 braced_init_obstack);
8495 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8497 /* Advance to the next smaller node. */
8502 /* We have reached the smallest node bigger than the
8503 current unfilled field. Fill the space first. */
8504 next = elt->purpose;
8510 /* Advance to the next bigger node. */
8515 /* We have reached the biggest node in a subtree. Find
8516 the parent of it, which is the next bigger node. */
8517 while (elt->parent && elt->parent->right == elt)
8521 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8522 bit_position (elt->purpose))))
8524 next = elt->purpose;
8532 /* Ordinarily return, but not if we want to output all
8533 and there are elements left. */
8534 if (!(all && next != 0))
8537 /* If it's not incremental, just skip over the gap, so that after
8538 jumping to retry we will output the next successive element. */
8539 if (TREE_CODE (constructor_type) == RECORD_TYPE
8540 || TREE_CODE (constructor_type) == UNION_TYPE)
8541 constructor_unfilled_fields = next;
8542 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8543 constructor_unfilled_index = next;
8545 /* ELT now points to the node in the pending tree with the next
8546 initializer to output. */
8550 /* Add one non-braced element to the current constructor level.
8551 This adjusts the current position within the constructor's type.
8552 This may also start or terminate implicit levels
8553 to handle a partly-braced initializer.
8555 Once this has found the correct level for the new element,
8556 it calls output_init_element.
8558 IMPLICIT is true if value comes from pop_init_level (1),
8559 the new initializer has been merged with the existing one
8560 and thus no warnings should be emitted about overriding an
8561 existing initializer. */
8564 process_init_element (location_t loc, struct c_expr value, bool implicit,
8565 struct obstack * braced_init_obstack)
8567 tree orig_value = value.value;
8568 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8569 bool strict_string = value.original_code == STRING_CST;
8570 bool was_designated = designator_depth != 0;
8572 designator_depth = 0;
8573 designator_erroneous = 0;
8575 /* Handle superfluous braces around string cst as in
8576 char x[] = {"foo"}; */
8580 && TREE_CODE (constructor_type) == ARRAY_TYPE
8581 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8582 && integer_zerop (constructor_unfilled_index))
8584 if (constructor_stack->replacement_value.value)
8585 error_init ("excess elements in char array initializer");
8586 constructor_stack->replacement_value = value;
8590 if (constructor_stack->replacement_value.value != 0)
8592 error_init ("excess elements in struct initializer");
8596 /* Ignore elements of a brace group if it is entirely superfluous
8597 and has already been diagnosed. */
8598 if (constructor_type == 0)
8601 /* If we've exhausted any levels that didn't have braces,
8603 while (constructor_stack->implicit)
8605 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8606 || TREE_CODE (constructor_type) == UNION_TYPE)
8607 && constructor_fields == 0)
8608 process_init_element (loc, pop_init_level (1, braced_init_obstack),
8609 true, braced_init_obstack);
8610 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8611 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8612 && constructor_max_index
8613 && tree_int_cst_lt (constructor_max_index,
8615 process_init_element (loc, pop_init_level (1, braced_init_obstack),
8616 true, braced_init_obstack);
8621 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8622 if (constructor_range_stack)
8624 /* If value is a compound literal and we'll be just using its
8625 content, don't put it into a SAVE_EXPR. */
8626 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8627 || !require_constant_value
8630 tree semantic_type = NULL_TREE;
8631 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8633 semantic_type = TREE_TYPE (value.value);
8634 value.value = TREE_OPERAND (value.value, 0);
8636 value.value = c_save_expr (value.value);
8638 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8645 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8648 enum tree_code fieldcode;
8650 if (constructor_fields == 0)
8652 pedwarn_init (input_location, 0,
8653 "excess elements in struct initializer");
8657 fieldtype = TREE_TYPE (constructor_fields);
8658 if (fieldtype != error_mark_node)
8659 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8660 fieldcode = TREE_CODE (fieldtype);
8662 /* Error for non-static initialization of a flexible array member. */
8663 if (fieldcode == ARRAY_TYPE
8664 && !require_constant_value
8665 && TYPE_SIZE (fieldtype) == NULL_TREE
8666 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8668 error_init ("non-static initialization of a flexible array member");
8672 /* Accept a string constant to initialize a subarray. */
8673 if (value.value != 0
8674 && fieldcode == ARRAY_TYPE
8675 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8677 value.value = orig_value;
8678 /* Otherwise, if we have come to a subaggregate,
8679 and we don't have an element of its type, push into it. */
8680 else if (value.value != 0
8681 && value.value != error_mark_node
8682 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8683 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8684 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8686 push_init_level (1, braced_init_obstack);
8692 push_member_name (constructor_fields);
8693 output_init_element (loc, value.value, value.original_type,
8694 strict_string, fieldtype,
8695 constructor_fields, 1, implicit,
8696 braced_init_obstack);
8697 RESTORE_SPELLING_DEPTH (constructor_depth);
8700 /* Do the bookkeeping for an element that was
8701 directly output as a constructor. */
8703 /* For a record, keep track of end position of last field. */
8704 if (DECL_SIZE (constructor_fields))
8705 constructor_bit_index
8706 = size_binop_loc (input_location, PLUS_EXPR,
8707 bit_position (constructor_fields),
8708 DECL_SIZE (constructor_fields));
8710 /* If the current field was the first one not yet written out,
8711 it isn't now, so update. */
8712 if (constructor_unfilled_fields == constructor_fields)
8714 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8715 /* Skip any nameless bit fields. */
8716 while (constructor_unfilled_fields != 0
8717 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8718 && DECL_NAME (constructor_unfilled_fields) == 0)
8719 constructor_unfilled_fields =
8720 DECL_CHAIN (constructor_unfilled_fields);
8724 constructor_fields = DECL_CHAIN (constructor_fields);
8725 /* Skip any nameless bit fields at the beginning. */
8726 while (constructor_fields != 0
8727 && DECL_C_BIT_FIELD (constructor_fields)
8728 && DECL_NAME (constructor_fields) == 0)
8729 constructor_fields = DECL_CHAIN (constructor_fields);
8731 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8734 enum tree_code fieldcode;
8736 if (constructor_fields == 0)
8738 pedwarn_init (input_location, 0,
8739 "excess elements in union initializer");
8743 fieldtype = TREE_TYPE (constructor_fields);
8744 if (fieldtype != error_mark_node)
8745 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8746 fieldcode = TREE_CODE (fieldtype);
8748 /* Warn that traditional C rejects initialization of unions.
8749 We skip the warning if the value is zero. This is done
8750 under the assumption that the zero initializer in user
8751 code appears conditioned on e.g. __STDC__ to avoid
8752 "missing initializer" warnings and relies on default
8753 initialization to zero in the traditional C case.
8754 We also skip the warning if the initializer is designated,
8755 again on the assumption that this must be conditional on
8756 __STDC__ anyway (and we've already complained about the
8757 member-designator already). */
8758 if (!in_system_header_at (input_location) && !constructor_designated
8759 && !(value.value && (integer_zerop (value.value)
8760 || real_zerop (value.value))))
8761 warning (OPT_Wtraditional, "traditional C rejects initialization "
8764 /* Accept a string constant to initialize a subarray. */
8765 if (value.value != 0
8766 && fieldcode == ARRAY_TYPE
8767 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8769 value.value = orig_value;
8770 /* Otherwise, if we have come to a subaggregate,
8771 and we don't have an element of its type, push into it. */
8772 else if (value.value != 0
8773 && value.value != error_mark_node
8774 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8775 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8776 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8778 push_init_level (1, braced_init_obstack);
8784 push_member_name (constructor_fields);
8785 output_init_element (loc, value.value, value.original_type,
8786 strict_string, fieldtype,
8787 constructor_fields, 1, implicit,
8788 braced_init_obstack);
8789 RESTORE_SPELLING_DEPTH (constructor_depth);
8792 /* Do the bookkeeping for an element that was
8793 directly output as a constructor. */
8795 constructor_bit_index = DECL_SIZE (constructor_fields);
8796 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8799 constructor_fields = 0;
8801 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8803 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8804 enum tree_code eltcode = TREE_CODE (elttype);
8806 /* Accept a string constant to initialize a subarray. */
8807 if (value.value != 0
8808 && eltcode == ARRAY_TYPE
8809 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8811 value.value = orig_value;
8812 /* Otherwise, if we have come to a subaggregate,
8813 and we don't have an element of its type, push into it. */
8814 else if (value.value != 0
8815 && value.value != error_mark_node
8816 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8817 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8818 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8820 push_init_level (1, braced_init_obstack);
8824 if (constructor_max_index != 0
8825 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8826 || integer_all_onesp (constructor_max_index)))
8828 pedwarn_init (input_location, 0,
8829 "excess elements in array initializer");
8833 /* Now output the actual element. */
8836 push_array_bounds (tree_to_uhwi (constructor_index));
8837 output_init_element (loc, value.value, value.original_type,
8838 strict_string, elttype,
8839 constructor_index, 1, implicit,
8840 braced_init_obstack);
8841 RESTORE_SPELLING_DEPTH (constructor_depth);
8845 = size_binop_loc (input_location, PLUS_EXPR,
8846 constructor_index, bitsize_one_node);
8849 /* If we are doing the bookkeeping for an element that was
8850 directly output as a constructor, we must update
8851 constructor_unfilled_index. */
8852 constructor_unfilled_index = constructor_index;
8854 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8856 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8858 /* Do a basic check of initializer size. Note that vectors
8859 always have a fixed size derived from their type. */
8860 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8862 pedwarn_init (input_location, 0,
8863 "excess elements in vector initializer");
8867 /* Now output the actual element. */
8870 if (TREE_CODE (value.value) == VECTOR_CST)
8871 elttype = TYPE_MAIN_VARIANT (constructor_type);
8872 output_init_element (loc, value.value, value.original_type,
8873 strict_string, elttype,
8874 constructor_index, 1, implicit,
8875 braced_init_obstack);
8879 = size_binop_loc (input_location,
8880 PLUS_EXPR, constructor_index, bitsize_one_node);
8883 /* If we are doing the bookkeeping for an element that was
8884 directly output as a constructor, we must update
8885 constructor_unfilled_index. */
8886 constructor_unfilled_index = constructor_index;
8889 /* Handle the sole element allowed in a braced initializer
8890 for a scalar variable. */
8891 else if (constructor_type != error_mark_node
8892 && constructor_fields == 0)
8894 pedwarn_init (input_location, 0,
8895 "excess elements in scalar initializer");
8901 output_init_element (loc, value.value, value.original_type,
8902 strict_string, constructor_type,
8903 NULL_TREE, 1, implicit,
8904 braced_init_obstack);
8905 constructor_fields = 0;
8908 /* Handle range initializers either at this level or anywhere higher
8909 in the designator stack. */
8910 if (constructor_range_stack)
8912 struct constructor_range_stack *p, *range_stack;
8915 range_stack = constructor_range_stack;
8916 constructor_range_stack = 0;
8917 while (constructor_stack != range_stack->stack)
8919 gcc_assert (constructor_stack->implicit);
8920 process_init_element (loc,
8921 pop_init_level (1, braced_init_obstack),
8922 true, braced_init_obstack);
8924 for (p = range_stack;
8925 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8928 gcc_assert (constructor_stack->implicit);
8929 process_init_element (loc,
8930 pop_init_level (1, braced_init_obstack),
8931 true, braced_init_obstack);
8934 p->index = size_binop_loc (input_location,
8935 PLUS_EXPR, p->index, bitsize_one_node);
8936 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8941 constructor_index = p->index;
8942 constructor_fields = p->fields;
8943 if (finish && p->range_end && p->index == p->range_start)
8951 push_init_level (2, braced_init_obstack);
8952 p->stack = constructor_stack;
8953 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8954 p->index = p->range_start;
8958 constructor_range_stack = range_stack;
8965 constructor_range_stack = 0;
8968 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8969 (guaranteed to be 'volatile' or null) and ARGS (represented using
8970 an ASM_EXPR node). */
8972 build_asm_stmt (tree cv_qualifier, tree args)
8974 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8975 ASM_VOLATILE_P (args) = 1;
8976 return add_stmt (args);
8979 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8980 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8981 SIMPLE indicates whether there was anything at all after the
8982 string in the asm expression -- asm("blah") and asm("blah" : )
8983 are subtly different. We use a ASM_EXPR node to represent this. */
8985 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8986 tree clobbers, tree labels, bool simple)
8991 const char *constraint;
8992 const char **oconstraints;
8993 bool allows_mem, allows_reg, is_inout;
8994 int ninputs, noutputs;
8996 ninputs = list_length (inputs);
8997 noutputs = list_length (outputs);
8998 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9000 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9002 /* Remove output conversions that change the type but not the mode. */
9003 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9005 tree output = TREE_VALUE (tail);
9007 output = c_fully_fold (output, false, NULL);
9009 /* ??? Really, this should not be here. Users should be using a
9010 proper lvalue, dammit. But there's a long history of using casts
9011 in the output operands. In cases like longlong.h, this becomes a
9012 primitive form of typechecking -- if the cast can be removed, then
9013 the output operand had a type of the proper width; otherwise we'll
9014 get an error. Gross, but ... */
9015 STRIP_NOPS (output);
9017 if (!lvalue_or_else (loc, output, lv_asm))
9018 output = error_mark_node;
9020 if (output != error_mark_node
9021 && (TREE_READONLY (output)
9022 || TYPE_READONLY (TREE_TYPE (output))
9023 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9024 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9025 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9026 readonly_error (loc, output, lv_asm);
9028 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9029 oconstraints[i] = constraint;
9031 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9032 &allows_mem, &allows_reg, &is_inout))
9034 /* If the operand is going to end up in memory,
9035 mark it addressable. */
9036 if (!allows_reg && !c_mark_addressable (output))
9037 output = error_mark_node;
9038 if (!(!allows_reg && allows_mem)
9039 && output != error_mark_node
9040 && VOID_TYPE_P (TREE_TYPE (output)))
9042 error_at (loc, "invalid use of void expression");
9043 output = error_mark_node;
9047 output = error_mark_node;
9049 TREE_VALUE (tail) = output;
9052 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9056 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9057 input = TREE_VALUE (tail);
9059 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9060 oconstraints, &allows_mem, &allows_reg))
9062 /* If the operand is going to end up in memory,
9063 mark it addressable. */
9064 if (!allows_reg && allows_mem)
9066 input = c_fully_fold (input, false, NULL);
9068 /* Strip the nops as we allow this case. FIXME, this really
9069 should be rejected or made deprecated. */
9071 if (!c_mark_addressable (input))
9072 input = error_mark_node;
9077 memset (&expr, 0, sizeof (expr));
9079 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9080 input = c_fully_fold (expr.value, false, NULL);
9082 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9084 error_at (loc, "invalid use of void expression");
9085 input = error_mark_node;
9090 input = error_mark_node;
9092 TREE_VALUE (tail) = input;
9095 /* ASMs with labels cannot have outputs. This should have been
9096 enforced by the parser. */
9097 gcc_assert (outputs == NULL || labels == NULL);
9099 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9101 /* asm statements without outputs, including simple ones, are treated
9103 ASM_INPUT_P (args) = simple;
9104 ASM_VOLATILE_P (args) = (noutputs == 0);
9109 /* Generate a goto statement to LABEL. LOC is the location of the
9113 c_finish_goto_label (location_t loc, tree label)
9115 tree decl = lookup_label_for_goto (loc, label);
9118 TREE_USED (decl) = 1;
9120 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9121 SET_EXPR_LOCATION (t, loc);
9122 return add_stmt (t);
9126 /* Generate a computed goto statement to EXPR. LOC is the location of
9130 c_finish_goto_ptr (location_t loc, tree expr)
9133 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9134 expr = c_fully_fold (expr, false, NULL);
9135 expr = convert (ptr_type_node, expr);
9136 t = build1 (GOTO_EXPR, void_type_node, expr);
9137 SET_EXPR_LOCATION (t, loc);
9138 return add_stmt (t);
9141 /* Generate a C `return' statement. RETVAL is the expression for what
9142 to return, or a null pointer for `return;' with no value. LOC is
9143 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
9144 is the original type of RETVAL. */
9147 c_finish_return (location_t loc, tree retval, tree origtype)
9149 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9150 bool no_warning = false;
9154 if (TREE_THIS_VOLATILE (current_function_decl))
9156 "function declared %<noreturn%> has a %<return%> statement");
9158 if (flag_cilkplus && contains_array_notation_expr (retval))
9160 /* Array notations are allowed in a return statement if it is inside a
9161 built-in array notation reduction function. */
9162 if (!find_rank (loc, retval, retval, false, &rank))
9163 return error_mark_node;
9166 error_at (loc, "array notation expression cannot be used as a "
9168 return error_mark_node;
9171 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9173 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9175 return error_mark_node;
9179 tree semantic_type = NULL_TREE;
9180 npc = null_pointer_constant_p (retval);
9181 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9183 semantic_type = TREE_TYPE (retval);
9184 retval = TREE_OPERAND (retval, 0);
9186 retval = c_fully_fold (retval, false, NULL);
9188 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9193 current_function_returns_null = 1;
9194 if ((warn_return_type || flag_isoc99)
9195 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9197 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
9198 "%<return%> with no value, in "
9199 "function returning non-void");
9203 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9205 current_function_returns_null = 1;
9206 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9208 "%<return%> with a value, in function returning void");
9210 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9211 "%<return%> with expression, in function returning void");
9215 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9216 retval, origtype, ic_return,
9217 npc, NULL_TREE, NULL_TREE, 0);
9218 tree res = DECL_RESULT (current_function_decl);
9222 current_function_returns_value = 1;
9223 if (t == error_mark_node)
9226 save = in_late_binary_op;
9227 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9228 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9229 in_late_binary_op = true;
9230 inner = t = convert (TREE_TYPE (res), t);
9231 in_late_binary_op = save;
9233 /* Strip any conversions, additions, and subtractions, and see if
9234 we are returning the address of a local variable. Warn if so. */
9237 switch (TREE_CODE (inner))
9240 case NON_LVALUE_EXPR:
9242 case POINTER_PLUS_EXPR:
9243 inner = TREE_OPERAND (inner, 0);
9247 /* If the second operand of the MINUS_EXPR has a pointer
9248 type (or is converted from it), this may be valid, so
9249 don't give a warning. */
9251 tree op1 = TREE_OPERAND (inner, 1);
9253 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9254 && (CONVERT_EXPR_P (op1)
9255 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9256 op1 = TREE_OPERAND (op1, 0);
9258 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9261 inner = TREE_OPERAND (inner, 0);
9266 inner = TREE_OPERAND (inner, 0);
9268 while (REFERENCE_CLASS_P (inner)
9269 && TREE_CODE (inner) != INDIRECT_REF)
9270 inner = TREE_OPERAND (inner, 0);
9273 && !DECL_EXTERNAL (inner)
9274 && !TREE_STATIC (inner)
9275 && DECL_CONTEXT (inner) == current_function_decl)
9277 if (TREE_CODE (inner) == LABEL_DECL)
9278 warning_at (loc, OPT_Wreturn_local_addr,
9279 "function returns address of label");
9281 warning_at (loc, OPT_Wreturn_local_addr,
9282 "function returns address of local variable");
9293 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9294 SET_EXPR_LOCATION (retval, loc);
9296 if (warn_sequence_point)
9297 verify_sequence_points (retval);
9300 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9301 TREE_NO_WARNING (ret_stmt) |= no_warning;
9302 return add_stmt (ret_stmt);
9306 /* The SWITCH_EXPR being built. */
9309 /* The original type of the testing expression, i.e. before the
9310 default conversion is applied. */
9313 /* A splay-tree mapping the low element of a case range to the high
9314 element, or NULL_TREE if there is no high element. Used to
9315 determine whether or not a new case label duplicates an old case
9316 label. We need a tree, rather than simply a hash table, because
9317 of the GNU case range extension. */
9320 /* The bindings at the point of the switch. This is used for
9321 warnings crossing decls when branching to a case label. */
9322 struct c_spot_bindings *bindings;
9324 /* The next node on the stack. */
9325 struct c_switch *next;
9328 /* A stack of the currently active switch statements. The innermost
9329 switch statement is on the top of the stack. There is no need to
9330 mark the stack for garbage collection because it is only active
9331 during the processing of the body of a function, and we never
9332 collect at that point. */
9334 struct c_switch *c_switch_stack;
9336 /* Start a C switch statement, testing expression EXP. Return the new
9337 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9338 SWITCH_COND_LOC is the location of the switch's condition. */
9341 c_start_case (location_t switch_loc,
9342 location_t switch_cond_loc,
9345 tree orig_type = error_mark_node;
9346 struct c_switch *cs;
9348 if (exp != error_mark_node)
9350 orig_type = TREE_TYPE (exp);
9352 if (!INTEGRAL_TYPE_P (orig_type))
9354 if (orig_type != error_mark_node)
9356 error_at (switch_cond_loc, "switch quantity not an integer");
9357 orig_type = error_mark_node;
9359 exp = integer_zero_node;
9363 tree type = TYPE_MAIN_VARIANT (orig_type);
9365 if (!in_system_header_at (input_location)
9366 && (type == long_integer_type_node
9367 || type == long_unsigned_type_node))
9368 warning_at (switch_cond_loc,
9369 OPT_Wtraditional, "%<long%> switch expression not "
9370 "converted to %<int%> in ISO C");
9372 exp = c_fully_fold (exp, false, NULL);
9373 exp = default_conversion (exp);
9375 if (warn_sequence_point)
9376 verify_sequence_points (exp);
9380 /* Add this new SWITCH_EXPR to the stack. */
9381 cs = XNEW (struct c_switch);
9382 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9383 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9384 cs->orig_type = orig_type;
9385 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9386 cs->bindings = c_get_switch_bindings ();
9387 cs->next = c_switch_stack;
9388 c_switch_stack = cs;
9390 return add_stmt (cs->switch_expr);
9393 /* Process a case label at location LOC. */
9396 do_case (location_t loc, tree low_value, tree high_value)
9398 tree label = NULL_TREE;
9400 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9402 low_value = c_fully_fold (low_value, false, NULL);
9403 if (TREE_CODE (low_value) == INTEGER_CST)
9404 pedwarn (input_location, OPT_Wpedantic,
9405 "case label is not an integer constant expression");
9408 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9410 high_value = c_fully_fold (high_value, false, NULL);
9411 if (TREE_CODE (high_value) == INTEGER_CST)
9412 pedwarn (input_location, OPT_Wpedantic,
9413 "case label is not an integer constant expression");
9416 if (c_switch_stack == NULL)
9419 error_at (loc, "case label not within a switch statement");
9421 error_at (loc, "%<default%> label not within a switch statement");
9425 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9426 EXPR_LOCATION (c_switch_stack->switch_expr),
9430 label = c_add_case_label (loc, c_switch_stack->cases,
9431 SWITCH_COND (c_switch_stack->switch_expr),
9432 c_switch_stack->orig_type,
9433 low_value, high_value);
9434 if (label == error_mark_node)
9439 /* Finish the switch statement. */
9442 c_finish_case (tree body)
9444 struct c_switch *cs = c_switch_stack;
9445 location_t switch_location;
9447 SWITCH_BODY (cs->switch_expr) = body;
9449 /* Emit warnings as needed. */
9450 switch_location = EXPR_LOCATION (cs->switch_expr);
9451 c_do_switch_warnings (cs->cases, switch_location,
9452 TREE_TYPE (cs->switch_expr),
9453 SWITCH_COND (cs->switch_expr));
9455 /* Pop the stack. */
9456 c_switch_stack = cs->next;
9457 splay_tree_delete (cs->cases);
9458 c_release_switch_bindings (cs->bindings);
9462 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9463 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9464 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9465 statement, and was not surrounded with parenthesis. */
9468 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9469 tree else_block, bool nested_if)
9473 /* If the condition has array notations, then the rank of the then_block and
9474 else_block must be either 0 or be equal to the rank of the condition. If
9475 the condition does not have array notations then break them up as it is
9476 broken up in a normal expression. */
9477 if (flag_cilkplus && contains_array_notation_expr (cond))
9479 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9480 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9483 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9486 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9488 if (cond_rank != then_rank && then_rank != 0)
9490 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9491 " and the then-block");
9494 else if (cond_rank != else_rank && else_rank != 0)
9496 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9497 " and the else-block");
9501 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9502 if (warn_parentheses && nested_if && else_block == NULL)
9504 tree inner_if = then_block;
9506 /* We know from the grammar productions that there is an IF nested
9507 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9508 it might not be exactly THEN_BLOCK, but should be the last
9509 non-container statement within. */
9511 switch (TREE_CODE (inner_if))
9516 inner_if = BIND_EXPR_BODY (inner_if);
9518 case STATEMENT_LIST:
9519 inner_if = expr_last (then_block);
9521 case TRY_FINALLY_EXPR:
9522 case TRY_CATCH_EXPR:
9523 inner_if = TREE_OPERAND (inner_if, 0);
9530 if (COND_EXPR_ELSE (inner_if))
9531 warning_at (if_locus, OPT_Wparentheses,
9532 "suggest explicit braces to avoid ambiguous %<else%>");
9535 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9536 SET_EXPR_LOCATION (stmt, if_locus);
9540 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9541 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9542 is false for DO loops. INCR is the FOR increment expression. BODY is
9543 the statement controlled by the loop. BLAB is the break label. CLAB is
9544 the continue label. Everything is allowed to be NULL. */
9547 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9548 tree blab, tree clab, bool cond_is_first)
9550 tree entry = NULL, exit = NULL, t;
9552 if (flag_cilkplus && contains_array_notation_expr (cond))
9554 error_at (start_locus, "array notation expression cannot be used in a "
9555 "loop%'s condition");
9559 /* If the condition is zero don't generate a loop construct. */
9560 if (cond && integer_zerop (cond))
9564 t = build_and_jump (&blab);
9565 SET_EXPR_LOCATION (t, start_locus);
9571 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9573 /* If we have an exit condition, then we build an IF with gotos either
9574 out of the loop, or to the top of it. If there's no exit condition,
9575 then we just build a jump back to the top. */
9576 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9578 if (cond && !integer_nonzerop (cond))
9580 /* Canonicalize the loop condition to the end. This means
9581 generating a branch to the loop condition. Reuse the
9582 continue label, if possible. */
9587 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9588 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9591 t = build1 (GOTO_EXPR, void_type_node, clab);
9592 SET_EXPR_LOCATION (t, start_locus);
9596 t = build_and_jump (&blab);
9598 exit = fold_build3_loc (start_locus,
9599 COND_EXPR, void_type_node, cond, exit, t);
9601 exit = fold_build3_loc (input_location,
9602 COND_EXPR, void_type_node, cond, exit, t);
9611 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9619 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9623 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9626 tree label = *label_p;
9628 /* In switch statements break is sometimes stylistically used after
9629 a return statement. This can lead to spurious warnings about
9630 control reaching the end of a non-void function when it is
9631 inlined. Note that we are calling block_may_fallthru with
9632 language specific tree nodes; this works because
9633 block_may_fallthru returns true when given something it does not
9635 skip = !block_may_fallthru (cur_stmt_list);
9640 *label_p = label = create_artificial_label (loc);
9642 else if (TREE_CODE (label) == LABEL_DECL)
9644 else switch (TREE_INT_CST_LOW (label))
9648 error_at (loc, "break statement not within loop or switch");
9650 error_at (loc, "continue statement not within a loop");
9654 gcc_assert (is_break);
9655 error_at (loc, "break statement used with OpenMP for loop");
9660 error ("break statement within %<#pragma simd%> loop body");
9662 error ("continue statement within %<#pragma simd%> loop body");
9673 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9675 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9678 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9681 emit_side_effect_warnings (location_t loc, tree expr)
9683 if (expr == error_mark_node)
9685 else if (!TREE_SIDE_EFFECTS (expr))
9687 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9688 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9690 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9693 location_t cloc = loc;
9694 while (TREE_CODE (r) == COMPOUND_EXPR)
9696 if (EXPR_HAS_LOCATION (r))
9697 cloc = EXPR_LOCATION (r);
9698 r = TREE_OPERAND (r, 1);
9700 if (!TREE_SIDE_EFFECTS (r)
9701 && !VOID_TYPE_P (TREE_TYPE (r))
9702 && !CONVERT_EXPR_P (r)
9703 && !TREE_NO_WARNING (r)
9704 && !TREE_NO_WARNING (expr))
9705 warning_at (cloc, OPT_Wunused_value,
9706 "right-hand operand of comma expression has no effect");
9709 warn_if_unused_value (expr, loc);
9712 /* Process an expression as if it were a complete statement. Emit
9713 diagnostics, but do not call ADD_STMT. LOC is the location of the
9717 c_process_expr_stmt (location_t loc, tree expr)
9724 expr = c_fully_fold (expr, false, NULL);
9726 if (warn_sequence_point)
9727 verify_sequence_points (expr);
9729 if (TREE_TYPE (expr) != error_mark_node
9730 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9731 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9732 error_at (loc, "expression statement has incomplete type");
9734 /* If we're not processing a statement expression, warn about unused values.
9735 Warnings for statement expressions will be emitted later, once we figure
9736 out which is the result. */
9737 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9738 && warn_unused_value)
9739 emit_side_effect_warnings (loc, expr);
9742 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9743 exprv = TREE_OPERAND (exprv, 1);
9744 while (CONVERT_EXPR_P (exprv))
9745 exprv = TREE_OPERAND (exprv, 0);
9747 || handled_component_p (exprv)
9748 || TREE_CODE (exprv) == ADDR_EXPR)
9749 mark_exp_read (exprv);
9751 /* If the expression is not of a type to which we cannot assign a line
9752 number, wrap the thing in a no-op NOP_EXPR. */
9753 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9755 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9756 SET_EXPR_LOCATION (expr, loc);
9762 /* Emit an expression as a statement. LOC is the location of the
9766 c_finish_expr_stmt (location_t loc, tree expr)
9769 return add_stmt (c_process_expr_stmt (loc, expr));
9774 /* Do the opposite and emit a statement as an expression. To begin,
9775 create a new binding level and return it. */
9778 c_begin_stmt_expr (void)
9782 /* We must force a BLOCK for this level so that, if it is not expanded
9783 later, there is a way to turn off the entire subtree of blocks that
9784 are contained in it. */
9786 ret = c_begin_compound_stmt (true);
9788 c_bindings_start_stmt_expr (c_switch_stack == NULL
9790 : c_switch_stack->bindings);
9792 /* Mark the current statement list as belonging to a statement list. */
9793 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9798 /* LOC is the location of the compound statement to which this body
9802 c_finish_stmt_expr (location_t loc, tree body)
9804 tree last, type, tmp, val;
9807 body = c_end_compound_stmt (loc, body, true);
9809 c_bindings_end_stmt_expr (c_switch_stack == NULL
9811 : c_switch_stack->bindings);
9813 /* Locate the last statement in BODY. See c_end_compound_stmt
9814 about always returning a BIND_EXPR. */
9815 last_p = &BIND_EXPR_BODY (body);
9816 last = BIND_EXPR_BODY (body);
9819 if (TREE_CODE (last) == STATEMENT_LIST)
9821 tree_stmt_iterator i;
9823 /* This can happen with degenerate cases like ({ }). No value. */
9824 if (!TREE_SIDE_EFFECTS (last))
9827 /* If we're supposed to generate side effects warnings, process
9828 all of the statements except the last. */
9829 if (warn_unused_value)
9831 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9834 tree t = tsi_stmt (i);
9836 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9837 emit_side_effect_warnings (tloc, t);
9841 i = tsi_last (last);
9842 last_p = tsi_stmt_ptr (i);
9846 /* If the end of the list is exception related, then the list was split
9847 by a call to push_cleanup. Continue searching. */
9848 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9849 || TREE_CODE (last) == TRY_CATCH_EXPR)
9851 last_p = &TREE_OPERAND (last, 0);
9853 goto continue_searching;
9856 if (last == error_mark_node)
9859 /* In the case that the BIND_EXPR is not necessary, return the
9860 expression out from inside it. */
9861 if (last == BIND_EXPR_BODY (body)
9862 && BIND_EXPR_VARS (body) == NULL)
9864 /* Even if this looks constant, do not allow it in a constant
9866 last = c_wrap_maybe_const (last, true);
9867 /* Do not warn if the return value of a statement expression is
9869 TREE_NO_WARNING (last) = 1;
9873 /* Extract the type of said expression. */
9874 type = TREE_TYPE (last);
9876 /* If we're not returning a value at all, then the BIND_EXPR that
9877 we already have is a fine expression to return. */
9878 if (!type || VOID_TYPE_P (type))
9881 /* Now that we've located the expression containing the value, it seems
9882 silly to make voidify_wrapper_expr repeat the process. Create a
9883 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9884 tmp = create_tmp_var_raw (type, NULL);
9886 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9887 tree_expr_nonnegative_p giving up immediately. */
9889 if (TREE_CODE (val) == NOP_EXPR
9890 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9891 val = TREE_OPERAND (val, 0);
9893 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9894 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9897 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9898 SET_EXPR_LOCATION (t, loc);
9903 /* Begin and end compound statements. This is as simple as pushing
9904 and popping new statement lists from the tree. */
9907 c_begin_compound_stmt (bool do_scope)
9909 tree stmt = push_stmt_list ();
9915 /* End a compound statement. STMT is the statement. LOC is the
9916 location of the compound statement-- this is usually the location
9917 of the opening brace. */
9920 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9926 if (c_dialect_objc ())
9927 objc_clear_super_receiver ();
9928 block = pop_scope ();
9931 stmt = pop_stmt_list (stmt);
9932 stmt = c_build_bind_expr (loc, block, stmt);
9934 /* If this compound statement is nested immediately inside a statement
9935 expression, then force a BIND_EXPR to be created. Otherwise we'll
9936 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9937 STATEMENT_LISTs merge, and thus we can lose track of what statement
9939 if (building_stmt_list_p ()
9940 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9941 && TREE_CODE (stmt) != BIND_EXPR)
9943 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9944 TREE_SIDE_EFFECTS (stmt) = 1;
9945 SET_EXPR_LOCATION (stmt, loc);
9951 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9952 when the current scope is exited. EH_ONLY is true when this is not
9953 meant to apply to normal control flow transfer. */
9956 push_cleanup (tree decl, tree cleanup, bool eh_only)
9958 enum tree_code code;
9962 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9963 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9965 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9966 list = push_stmt_list ();
9967 TREE_OPERAND (stmt, 0) = list;
9968 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9971 /* Build a binary-operation expression without default conversions.
9972 CODE is the kind of expression to build.
9973 LOCATION is the operator's location.
9974 This function differs from `build' in several ways:
9975 the data type of the result is computed and recorded in it,
9976 warnings are generated if arg data types are invalid,
9977 special handling for addition and subtraction of pointers is known,
9978 and some optimization is done (operations on narrow ints
9979 are done in the narrower type when that gives the same result).
9980 Constant folding is also done before the result is returned.
9982 Note that the operands will never have enumeral types, or function
9983 or array types, because either they will have the default conversions
9984 performed or they have both just been converted to some other type in which
9985 the arithmetic is to be done. */
9988 build_binary_op (location_t location, enum tree_code code,
9989 tree orig_op0, tree orig_op1, int convert_p)
9991 tree type0, type1, orig_type0, orig_type1;
9993 enum tree_code code0, code1;
9995 tree ret = error_mark_node;
9996 const char *invalid_op_diag;
9997 bool op0_int_operands, op1_int_operands;
9998 bool int_const, int_const_or_overflow, int_operands;
10000 /* Expression code to give to the expression when it is built.
10001 Normally this is CODE, which is what the caller asked for,
10002 but in some special cases we change it. */
10003 enum tree_code resultcode = code;
10005 /* Data type in which the computation is to be performed.
10006 In the simplest cases this is the common type of the arguments. */
10007 tree result_type = NULL;
10009 /* When the computation is in excess precision, the type of the
10010 final EXCESS_PRECISION_EXPR. */
10011 tree semantic_result_type = NULL;
10013 /* Nonzero means operands have already been type-converted
10014 in whatever way is necessary.
10015 Zero means they need to be converted to RESULT_TYPE. */
10018 /* Nonzero means create the expression with this type, rather than
10020 tree build_type = 0;
10022 /* Nonzero means after finally constructing the expression
10023 convert it to this type. */
10024 tree final_type = 0;
10026 /* Nonzero if this is an operation like MIN or MAX which can
10027 safely be computed in short if both args are promoted shorts.
10028 Also implies COMMON.
10029 -1 indicates a bitwise operation; this makes a difference
10030 in the exact conditions for when it is safe to do the operation
10031 in a narrower mode. */
10034 /* Nonzero if this is a comparison operation;
10035 if both args are promoted shorts, compare the original shorts.
10036 Also implies COMMON. */
10037 int short_compare = 0;
10039 /* Nonzero if this is a right-shift operation, which can be computed on the
10040 original short and then promoted if the operand is a promoted short. */
10041 int short_shift = 0;
10043 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10046 /* True means types are compatible as far as ObjC is concerned. */
10049 /* True means this is an arithmetic operation that may need excess
10051 bool may_need_excess_precision;
10053 /* True means this is a boolean operation that converts both its
10054 operands to truth-values. */
10055 bool boolean_op = false;
10057 /* Remember whether we're doing / or %. */
10058 bool doing_div_or_mod = false;
10060 /* Remember whether we're doing << or >>. */
10061 bool doing_shift = false;
10063 /* Tree holding instrumentation expression. */
10064 tree instrument_expr = NULL;
10066 if (location == UNKNOWN_LOCATION)
10067 location = input_location;
10072 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10073 if (op0_int_operands)
10074 op0 = remove_c_maybe_const_expr (op0);
10075 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10076 if (op1_int_operands)
10077 op1 = remove_c_maybe_const_expr (op1);
10078 int_operands = (op0_int_operands && op1_int_operands);
10081 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10082 && TREE_CODE (orig_op1) == INTEGER_CST);
10083 int_const = (int_const_or_overflow
10084 && !TREE_OVERFLOW (orig_op0)
10085 && !TREE_OVERFLOW (orig_op1));
10088 int_const = int_const_or_overflow = false;
10090 /* Do not apply default conversion in mixed vector/scalar expression. */
10092 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10093 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10095 op0 = default_conversion (op0);
10096 op1 = default_conversion (op1);
10099 /* When Cilk Plus is enabled and there are array notations inside op0, then
10100 we check to see if there are builtin array notation functions. If
10101 so, then we take on the type of the array notation inside it. */
10102 if (flag_cilkplus && contains_array_notation_expr (op0))
10103 orig_type0 = type0 = find_correct_array_notation_type (op0);
10105 orig_type0 = type0 = TREE_TYPE (op0);
10107 if (flag_cilkplus && contains_array_notation_expr (op1))
10108 orig_type1 = type1 = find_correct_array_notation_type (op1);
10110 orig_type1 = type1 = TREE_TYPE (op1);
10112 /* The expression codes of the data types of the arguments tell us
10113 whether the arguments are integers, floating, pointers, etc. */
10114 code0 = TREE_CODE (type0);
10115 code1 = TREE_CODE (type1);
10117 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10118 STRIP_TYPE_NOPS (op0);
10119 STRIP_TYPE_NOPS (op1);
10121 /* If an error was already reported for one of the arguments,
10122 avoid reporting another error. */
10124 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10125 return error_mark_node;
10127 if ((invalid_op_diag
10128 = targetm.invalid_binary_op (code, type0, type1)))
10130 error_at (location, invalid_op_diag);
10131 return error_mark_node;
10139 case TRUNC_DIV_EXPR:
10140 case CEIL_DIV_EXPR:
10141 case FLOOR_DIV_EXPR:
10142 case ROUND_DIV_EXPR:
10143 case EXACT_DIV_EXPR:
10144 may_need_excess_precision = true;
10147 may_need_excess_precision = false;
10150 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10152 op0 = TREE_OPERAND (op0, 0);
10153 type0 = TREE_TYPE (op0);
10155 else if (may_need_excess_precision
10156 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10159 op0 = convert (eptype, op0);
10161 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10163 op1 = TREE_OPERAND (op1, 0);
10164 type1 = TREE_TYPE (op1);
10166 else if (may_need_excess_precision
10167 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10170 op1 = convert (eptype, op1);
10173 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10175 /* In case when one of the operands of the binary operation is
10176 a vector and another is a scalar -- convert scalar to vector. */
10177 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10179 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10182 switch (convert_flag)
10185 return error_mark_node;
10188 bool maybe_const = true;
10190 sc = c_fully_fold (op0, false, &maybe_const);
10191 sc = save_expr (sc);
10192 sc = convert (TREE_TYPE (type1), sc);
10193 op0 = build_vector_from_val (type1, sc);
10195 op0 = c_wrap_maybe_const (op0, true);
10196 orig_type0 = type0 = TREE_TYPE (op0);
10197 code0 = TREE_CODE (type0);
10201 case stv_secondarg:
10203 bool maybe_const = true;
10205 sc = c_fully_fold (op1, false, &maybe_const);
10206 sc = save_expr (sc);
10207 sc = convert (TREE_TYPE (type0), sc);
10208 op1 = build_vector_from_val (type0, sc);
10210 op1 = c_wrap_maybe_const (op1, true);
10211 orig_type1 = type1 = TREE_TYPE (op1);
10212 code1 = TREE_CODE (type1);
10224 /* Handle the pointer + int case. */
10225 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10227 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10228 goto return_build_binary_op;
10230 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10232 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10233 goto return_build_binary_op;
10240 /* Subtraction of two similar pointers.
10241 We must subtract them as integers, then divide by object size. */
10242 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10243 && comp_target_types (location, type0, type1))
10245 ret = pointer_diff (location, op0, op1);
10246 goto return_build_binary_op;
10248 /* Handle pointer minus int. Just like pointer plus int. */
10249 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10251 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10252 goto return_build_binary_op;
10262 case TRUNC_DIV_EXPR:
10263 case CEIL_DIV_EXPR:
10264 case FLOOR_DIV_EXPR:
10265 case ROUND_DIV_EXPR:
10266 case EXACT_DIV_EXPR:
10267 doing_div_or_mod = true;
10268 warn_for_div_by_zero (location, op1);
10270 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10271 || code0 == FIXED_POINT_TYPE
10272 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10273 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10274 || code1 == FIXED_POINT_TYPE
10275 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10277 enum tree_code tcode0 = code0, tcode1 = code1;
10279 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10280 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10281 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10282 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10284 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10285 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10286 resultcode = RDIV_EXPR;
10288 /* Although it would be tempting to shorten always here, that
10289 loses on some targets, since the modulo instruction is
10290 undefined if the quotient can't be represented in the
10291 computation mode. We shorten only if unsigned or if
10292 dividing by something we know != -1. */
10293 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10294 || (TREE_CODE (op1) == INTEGER_CST
10295 && !integer_all_onesp (op1)));
10303 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10305 /* Allow vector types which are not floating point types. */
10306 else if (code0 == VECTOR_TYPE
10307 && code1 == VECTOR_TYPE
10308 && !VECTOR_FLOAT_TYPE_P (type0)
10309 && !VECTOR_FLOAT_TYPE_P (type1))
10313 case TRUNC_MOD_EXPR:
10314 case FLOOR_MOD_EXPR:
10315 doing_div_or_mod = true;
10316 warn_for_div_by_zero (location, op1);
10318 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10319 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10320 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10322 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10324 /* Although it would be tempting to shorten always here, that loses
10325 on some targets, since the modulo instruction is undefined if the
10326 quotient can't be represented in the computation mode. We shorten
10327 only if unsigned or if dividing by something we know != -1. */
10328 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10329 || (TREE_CODE (op1) == INTEGER_CST
10330 && !integer_all_onesp (op1)));
10335 case TRUTH_ANDIF_EXPR:
10336 case TRUTH_ORIF_EXPR:
10337 case TRUTH_AND_EXPR:
10338 case TRUTH_OR_EXPR:
10339 case TRUTH_XOR_EXPR:
10340 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10341 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10342 || code0 == FIXED_POINT_TYPE)
10343 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10344 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10345 || code1 == FIXED_POINT_TYPE))
10347 /* Result of these operations is always an int,
10348 but that does not mean the operands should be
10349 converted to ints! */
10350 result_type = integer_type_node;
10351 if (op0_int_operands)
10353 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10354 op0 = remove_c_maybe_const_expr (op0);
10357 op0 = c_objc_common_truthvalue_conversion (location, op0);
10358 if (op1_int_operands)
10360 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10361 op1 = remove_c_maybe_const_expr (op1);
10364 op1 = c_objc_common_truthvalue_conversion (location, op1);
10368 if (code == TRUTH_ANDIF_EXPR)
10370 int_const_or_overflow = (int_operands
10371 && TREE_CODE (orig_op0) == INTEGER_CST
10372 && (op0 == truthvalue_false_node
10373 || TREE_CODE (orig_op1) == INTEGER_CST));
10374 int_const = (int_const_or_overflow
10375 && !TREE_OVERFLOW (orig_op0)
10376 && (op0 == truthvalue_false_node
10377 || !TREE_OVERFLOW (orig_op1)));
10379 else if (code == TRUTH_ORIF_EXPR)
10381 int_const_or_overflow = (int_operands
10382 && TREE_CODE (orig_op0) == INTEGER_CST
10383 && (op0 == truthvalue_true_node
10384 || TREE_CODE (orig_op1) == INTEGER_CST));
10385 int_const = (int_const_or_overflow
10386 && !TREE_OVERFLOW (orig_op0)
10387 && (op0 == truthvalue_true_node
10388 || !TREE_OVERFLOW (orig_op1)));
10392 /* Shift operations: result has same type as first operand;
10393 always convert second operand to int.
10394 Also set SHORT_SHIFT if shifting rightward. */
10397 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10398 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10400 result_type = type0;
10403 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10404 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10405 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10406 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10408 result_type = type0;
10411 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10412 && code1 == INTEGER_TYPE)
10414 doing_shift = true;
10415 if (TREE_CODE (op1) == INTEGER_CST)
10417 if (tree_int_cst_sgn (op1) < 0)
10420 if (c_inhibit_evaluation_warnings == 0)
10421 warning_at (location, 0, "right shift count is negative");
10425 if (!integer_zerop (op1))
10428 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10431 if (c_inhibit_evaluation_warnings == 0)
10432 warning_at (location, 0, "right shift count >= width "
10438 /* Use the type of the value to be shifted. */
10439 result_type = type0;
10440 /* Convert the non vector shift-count to an integer, regardless
10441 of size of value being shifted. */
10442 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10443 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10444 op1 = convert (integer_type_node, op1);
10445 /* Avoid converting op1 to result_type later. */
10451 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10452 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10454 result_type = type0;
10457 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10458 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10459 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10460 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10462 result_type = type0;
10465 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10466 && code1 == INTEGER_TYPE)
10468 doing_shift = true;
10469 if (TREE_CODE (op1) == INTEGER_CST)
10471 if (tree_int_cst_sgn (op1) < 0)
10474 if (c_inhibit_evaluation_warnings == 0)
10475 warning_at (location, 0, "left shift count is negative");
10478 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10481 if (c_inhibit_evaluation_warnings == 0)
10482 warning_at (location, 0, "left shift count >= width of "
10487 /* Use the type of the value to be shifted. */
10488 result_type = type0;
10489 /* Convert the non vector shift-count to an integer, regardless
10490 of size of value being shifted. */
10491 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10492 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10493 op1 = convert (integer_type_node, op1);
10494 /* Avoid converting op1 to result_type later. */
10501 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10504 if (!vector_types_compatible_elements_p (type0, type1))
10506 error_at (location, "comparing vectors with different "
10508 return error_mark_node;
10511 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10513 error_at (location, "comparing vectors with different "
10514 "number of elements");
10515 return error_mark_node;
10518 /* Always construct signed integer vector type. */
10519 intt = c_common_type_for_size (GET_MODE_BITSIZE
10520 (TYPE_MODE (TREE_TYPE (type0))), 0);
10521 result_type = build_opaque_vector_type (intt,
10522 TYPE_VECTOR_SUBPARTS (type0));
10526 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10527 warning_at (location,
10529 "comparing floating point with == or != is unsafe");
10530 /* Result of comparison is always int,
10531 but don't convert the args to int! */
10532 build_type = integer_type_node;
10533 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10534 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10535 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10536 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10538 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10540 if (TREE_CODE (op0) == ADDR_EXPR
10541 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10543 if (code == EQ_EXPR)
10544 warning_at (location,
10546 "the comparison will always evaluate as %<false%> "
10547 "for the address of %qD will never be NULL",
10548 TREE_OPERAND (op0, 0));
10550 warning_at (location,
10552 "the comparison will always evaluate as %<true%> "
10553 "for the address of %qD will never be NULL",
10554 TREE_OPERAND (op0, 0));
10556 result_type = type0;
10558 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10560 if (TREE_CODE (op1) == ADDR_EXPR
10561 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10563 if (code == EQ_EXPR)
10564 warning_at (location,
10566 "the comparison will always evaluate as %<false%> "
10567 "for the address of %qD will never be NULL",
10568 TREE_OPERAND (op1, 0));
10570 warning_at (location,
10572 "the comparison will always evaluate as %<true%> "
10573 "for the address of %qD will never be NULL",
10574 TREE_OPERAND (op1, 0));
10576 result_type = type1;
10578 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10580 tree tt0 = TREE_TYPE (type0);
10581 tree tt1 = TREE_TYPE (type1);
10582 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10583 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10584 addr_space_t as_common = ADDR_SPACE_GENERIC;
10586 /* Anything compares with void *. void * compares with anything.
10587 Otherwise, the targets must be compatible
10588 and both must be object or both incomplete. */
10589 if (comp_target_types (location, type0, type1))
10590 result_type = common_pointer_type (type0, type1);
10591 else if (!addr_space_superset (as0, as1, &as_common))
10593 error_at (location, "comparison of pointers to "
10594 "disjoint address spaces");
10595 return error_mark_node;
10597 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10599 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10600 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10601 "comparison of %<void *%> with function pointer");
10603 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10605 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10606 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10607 "comparison of %<void *%> with function pointer");
10610 /* Avoid warning about the volatile ObjC EH puts on decls. */
10612 pedwarn (location, 0,
10613 "comparison of distinct pointer types lacks a cast");
10615 if (result_type == NULL_TREE)
10617 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10618 result_type = build_pointer_type
10619 (build_qualified_type (void_type_node, qual));
10622 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10624 result_type = type0;
10625 pedwarn (location, 0, "comparison between pointer and integer");
10627 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10629 result_type = type1;
10630 pedwarn (location, 0, "comparison between pointer and integer");
10638 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10641 if (!vector_types_compatible_elements_p (type0, type1))
10643 error_at (location, "comparing vectors with different "
10645 return error_mark_node;
10648 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10650 error_at (location, "comparing vectors with different "
10651 "number of elements");
10652 return error_mark_node;
10655 /* Always construct signed integer vector type. */
10656 intt = c_common_type_for_size (GET_MODE_BITSIZE
10657 (TYPE_MODE (TREE_TYPE (type0))), 0);
10658 result_type = build_opaque_vector_type (intt,
10659 TYPE_VECTOR_SUBPARTS (type0));
10663 build_type = integer_type_node;
10664 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10665 || code0 == FIXED_POINT_TYPE)
10666 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10667 || code1 == FIXED_POINT_TYPE))
10669 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10671 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10672 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10673 addr_space_t as_common;
10675 if (comp_target_types (location, type0, type1))
10677 result_type = common_pointer_type (type0, type1);
10678 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10679 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10680 pedwarn (location, 0,
10681 "comparison of complete and incomplete pointers");
10682 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10683 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10684 "ordered comparisons of pointers to functions");
10685 else if (null_pointer_constant_p (orig_op0)
10686 || null_pointer_constant_p (orig_op1))
10687 warning_at (location, OPT_Wextra,
10688 "ordered comparison of pointer with null pointer");
10691 else if (!addr_space_superset (as0, as1, &as_common))
10693 error_at (location, "comparison of pointers to "
10694 "disjoint address spaces");
10695 return error_mark_node;
10699 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10700 result_type = build_pointer_type
10701 (build_qualified_type (void_type_node, qual));
10702 pedwarn (location, 0,
10703 "comparison of distinct pointer types lacks a cast");
10706 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10708 result_type = type0;
10710 pedwarn (location, OPT_Wpedantic,
10711 "ordered comparison of pointer with integer zero");
10712 else if (extra_warnings)
10713 warning_at (location, OPT_Wextra,
10714 "ordered comparison of pointer with integer zero");
10716 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10718 result_type = type1;
10720 pedwarn (location, OPT_Wpedantic,
10721 "ordered comparison of pointer with integer zero");
10722 else if (extra_warnings)
10723 warning_at (location, OPT_Wextra,
10724 "ordered comparison of pointer with integer zero");
10726 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10728 result_type = type0;
10729 pedwarn (location, 0, "comparison between pointer and integer");
10731 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10733 result_type = type1;
10734 pedwarn (location, 0, "comparison between pointer and integer");
10739 gcc_unreachable ();
10742 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10743 return error_mark_node;
10745 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10746 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10747 || !vector_types_compatible_elements_p (type0, type1)))
10749 binary_op_error (location, code, type0, type1);
10750 return error_mark_node;
10753 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10754 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10756 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10757 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10759 bool first_complex = (code0 == COMPLEX_TYPE);
10760 bool second_complex = (code1 == COMPLEX_TYPE);
10761 int none_complex = (!first_complex && !second_complex);
10763 if (shorten || common || short_compare)
10765 result_type = c_common_type (type0, type1);
10766 do_warn_double_promotion (result_type, type0, type1,
10767 "implicit conversion from %qT to %qT "
10768 "to match other operand of binary "
10771 if (result_type == error_mark_node)
10772 return error_mark_node;
10775 if (first_complex != second_complex
10776 && (code == PLUS_EXPR
10777 || code == MINUS_EXPR
10778 || code == MULT_EXPR
10779 || (code == TRUNC_DIV_EXPR && first_complex))
10780 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10781 && flag_signed_zeros)
10783 /* An operation on mixed real/complex operands must be
10784 handled specially, but the language-independent code can
10785 more easily optimize the plain complex arithmetic if
10786 -fno-signed-zeros. */
10787 tree real_type = TREE_TYPE (result_type);
10789 if (type0 != orig_type0 || type1 != orig_type1)
10791 gcc_assert (may_need_excess_precision && common);
10792 semantic_result_type = c_common_type (orig_type0, orig_type1);
10796 if (TREE_TYPE (op0) != result_type)
10797 op0 = convert_and_check (location, result_type, op0);
10798 if (TREE_TYPE (op1) != real_type)
10799 op1 = convert_and_check (location, real_type, op1);
10803 if (TREE_TYPE (op0) != real_type)
10804 op0 = convert_and_check (location, real_type, op0);
10805 if (TREE_TYPE (op1) != result_type)
10806 op1 = convert_and_check (location, result_type, op1);
10808 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10809 return error_mark_node;
10812 op0 = c_save_expr (op0);
10813 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10815 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10820 case TRUNC_DIV_EXPR:
10821 op1 = c_save_expr (op1);
10822 imag = build2 (resultcode, real_type, imag, op1);
10823 /* Fall through. */
10826 real = build2 (resultcode, real_type, real, op1);
10834 op1 = c_save_expr (op1);
10835 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10837 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10842 op0 = c_save_expr (op0);
10843 imag = build2 (resultcode, real_type, op0, imag);
10844 /* Fall through. */
10846 real = build2 (resultcode, real_type, op0, real);
10849 real = build2 (resultcode, real_type, op0, real);
10850 imag = build1 (NEGATE_EXPR, real_type, imag);
10856 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10857 goto return_build_binary_op;
10860 /* For certain operations (which identify themselves by shorten != 0)
10861 if both args were extended from the same smaller type,
10862 do the arithmetic in that type and then extend.
10864 shorten !=0 and !=1 indicates a bitwise operation.
10865 For them, this optimization is safe only if
10866 both args are zero-extended or both are sign-extended.
10867 Otherwise, we might change the result.
10868 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10869 but calculated in (unsigned short) it would be (unsigned short)-1. */
10871 if (shorten && none_complex)
10873 final_type = result_type;
10874 result_type = shorten_binary_op (result_type, op0, op1,
10878 /* Shifts can be shortened if shifting right. */
10883 tree arg0 = get_narrower (op0, &unsigned_arg);
10885 final_type = result_type;
10887 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10888 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10890 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10891 && tree_int_cst_sgn (op1) > 0
10892 /* We can shorten only if the shift count is less than the
10893 number of bits in the smaller type size. */
10894 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10895 /* We cannot drop an unsigned shift after sign-extension. */
10896 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10898 /* Do an unsigned shift if the operand was zero-extended. */
10900 = c_common_signed_or_unsigned_type (unsigned_arg,
10902 /* Convert value-to-be-shifted to that type. */
10903 if (TREE_TYPE (op0) != result_type)
10904 op0 = convert (result_type, op0);
10909 /* Comparison operations are shortened too but differently.
10910 They identify themselves by setting short_compare = 1. */
10914 /* Don't write &op0, etc., because that would prevent op0
10915 from being kept in a register.
10916 Instead, make copies of the our local variables and
10917 pass the copies by reference, then copy them back afterward. */
10918 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10919 enum tree_code xresultcode = resultcode;
10921 = shorten_compare (location, &xop0, &xop1, &xresult_type,
10927 goto return_build_binary_op;
10930 op0 = xop0, op1 = xop1;
10932 resultcode = xresultcode;
10934 if (c_inhibit_evaluation_warnings == 0)
10936 bool op0_maybe_const = true;
10937 bool op1_maybe_const = true;
10938 tree orig_op0_folded, orig_op1_folded;
10940 if (in_late_binary_op)
10942 orig_op0_folded = orig_op0;
10943 orig_op1_folded = orig_op1;
10947 /* Fold for the sake of possible warnings, as in
10948 build_conditional_expr. This requires the
10949 "original" values to be folded, not just op0 and
10951 c_inhibit_evaluation_warnings++;
10952 op0 = c_fully_fold (op0, require_constant_value,
10954 op1 = c_fully_fold (op1, require_constant_value,
10956 c_inhibit_evaluation_warnings--;
10957 orig_op0_folded = c_fully_fold (orig_op0,
10958 require_constant_value,
10960 orig_op1_folded = c_fully_fold (orig_op1,
10961 require_constant_value,
10965 if (warn_sign_compare)
10966 warn_for_sign_compare (location, orig_op0_folded,
10967 orig_op1_folded, op0, op1,
10968 result_type, resultcode);
10969 if (!in_late_binary_op && !int_operands)
10971 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10972 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10973 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10974 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10980 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10981 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10982 Then the expression will be built.
10983 It will be given type FINAL_TYPE if that is nonzero;
10984 otherwise, it will be given type RESULT_TYPE. */
10988 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10989 return error_mark_node;
10992 if (build_type == NULL_TREE)
10994 build_type = result_type;
10995 if ((type0 != orig_type0 || type1 != orig_type1)
10998 gcc_assert (may_need_excess_precision && common);
10999 semantic_result_type = c_common_type (orig_type0, orig_type1);
11005 op0 = ep_convert_and_check (location, result_type, op0,
11006 semantic_result_type);
11007 op1 = ep_convert_and_check (location, result_type, op1,
11008 semantic_result_type);
11010 /* This can happen if one operand has a vector type, and the other
11011 has a different type. */
11012 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11013 return error_mark_node;
11016 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11017 | SANITIZE_FLOAT_DIVIDE))
11018 && current_function_decl != 0
11019 && !lookup_attribute ("no_sanitize_undefined",
11020 DECL_ATTRIBUTES (current_function_decl))
11021 && (doing_div_or_mod || doing_shift))
11023 /* OP0 and/or OP1 might have side-effects. */
11024 op0 = c_save_expr (op0);
11025 op1 = c_save_expr (op1);
11026 op0 = c_fully_fold (op0, false, NULL);
11027 op1 = c_fully_fold (op1, false, NULL);
11028 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11029 | SANITIZE_FLOAT_DIVIDE)))
11030 instrument_expr = ubsan_instrument_division (location, op0, op1);
11031 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11032 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11035 /* Treat expressions in initializers specially as they can't trap. */
11036 if (int_const_or_overflow)
11037 ret = (require_constant_value
11038 ? fold_build2_initializer_loc (location, resultcode, build_type,
11040 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11042 ret = build2 (resultcode, build_type, op0, op1);
11043 if (final_type != 0)
11044 ret = convert (final_type, ret);
11046 return_build_binary_op:
11047 gcc_assert (ret != error_mark_node);
11048 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11049 ret = (int_operands
11050 ? note_integer_operands (ret)
11051 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11052 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11053 && !in_late_binary_op)
11054 ret = note_integer_operands (ret);
11055 if (semantic_result_type)
11056 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11057 protected_set_expr_location (ret, location);
11059 if (instrument_expr != NULL)
11060 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11061 instrument_expr, ret);
11067 /* Convert EXPR to be a truth-value, validating its type for this
11068 purpose. LOCATION is the source location for the expression. */
11071 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11073 bool int_const, int_operands;
11075 switch (TREE_CODE (TREE_TYPE (expr)))
11078 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11079 return error_mark_node;
11082 error_at (location, "used struct type value where scalar is required");
11083 return error_mark_node;
11086 error_at (location, "used union type value where scalar is required");
11087 return error_mark_node;
11090 error_at (location, "void value not ignored as it ought to be");
11091 return error_mark_node;
11093 case FUNCTION_TYPE:
11094 gcc_unreachable ();
11097 error_at (location, "used vector type where scalar is required");
11098 return error_mark_node;
11104 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11105 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11106 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11108 expr = remove_c_maybe_const_expr (expr);
11109 expr = build2 (NE_EXPR, integer_type_node, expr,
11110 convert (TREE_TYPE (expr), integer_zero_node));
11111 expr = note_integer_operands (expr);
11114 /* ??? Should we also give an error for vectors rather than leaving
11115 those to give errors later? */
11116 expr = c_common_truthvalue_conversion (location, expr);
11118 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11120 if (TREE_OVERFLOW (expr))
11123 return note_integer_operands (expr);
11125 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11126 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11131 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11135 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11137 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11139 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11140 /* Executing a compound literal inside a function reinitializes
11142 if (!TREE_STATIC (decl))
11150 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11153 c_begin_omp_parallel (void)
11157 keep_next_level ();
11158 block = c_begin_compound_stmt (true);
11163 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11164 statement. LOC is the location of the OMP_PARALLEL. */
11167 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11171 block = c_end_compound_stmt (loc, block, true);
11173 stmt = make_node (OMP_PARALLEL);
11174 TREE_TYPE (stmt) = void_type_node;
11175 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11176 OMP_PARALLEL_BODY (stmt) = block;
11177 SET_EXPR_LOCATION (stmt, loc);
11179 return add_stmt (stmt);
11182 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11185 c_begin_omp_task (void)
11189 keep_next_level ();
11190 block = c_begin_compound_stmt (true);
11195 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11196 statement. LOC is the location of the #pragma. */
11199 c_finish_omp_task (location_t loc, tree clauses, tree block)
11203 block = c_end_compound_stmt (loc, block, true);
11205 stmt = make_node (OMP_TASK);
11206 TREE_TYPE (stmt) = void_type_node;
11207 OMP_TASK_CLAUSES (stmt) = clauses;
11208 OMP_TASK_BODY (stmt) = block;
11209 SET_EXPR_LOCATION (stmt, loc);
11211 return add_stmt (stmt);
11214 /* Generate GOMP_cancel call for #pragma omp cancel. */
11217 c_finish_omp_cancel (location_t loc, tree clauses)
11219 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11221 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11223 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11225 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11227 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11231 error_at (loc, "%<#pragma omp cancel must specify one of "
11232 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11236 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11237 if (ifc != NULL_TREE)
11239 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11240 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11241 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11242 build_zero_cst (type));
11245 ifc = boolean_true_node;
11246 tree stmt = build_call_expr_loc (loc, fn, 2,
11247 build_int_cst (integer_type_node, mask),
11252 /* Generate GOMP_cancellation_point call for
11253 #pragma omp cancellation point. */
11256 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11258 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11260 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11262 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11264 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11266 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11270 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11271 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11275 tree stmt = build_call_expr_loc (loc, fn, 1,
11276 build_int_cst (integer_type_node, mask));
11280 /* Helper function for handle_omp_array_sections. Called recursively
11281 to handle multiple array-section-subscripts. C is the clause,
11282 T current expression (initially OMP_CLAUSE_DECL), which is either
11283 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11284 expression if specified, TREE_VALUE length expression if specified,
11285 TREE_CHAIN is what it has been specified after, or some decl.
11286 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11287 set to true if any of the array-section-subscript could have length
11288 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11289 first array-section-subscript which is known not to have length
11291 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11292 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11293 all are or may have length of 1, array-section-subscript [:2] is the
11294 first one knonwn not to have length 1. For array-section-subscript
11295 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11296 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11297 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11298 case though, as some lengths could be zero. */
11301 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11302 bool &maybe_zero_len, unsigned int &first_non_one)
11304 tree ret, low_bound, length, type;
11305 if (TREE_CODE (t) != TREE_LIST)
11307 if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
11308 return error_mark_node;
11309 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11312 error_at (OMP_CLAUSE_LOCATION (c),
11313 "%qD is not a variable in %qs clause", t,
11314 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11316 error_at (OMP_CLAUSE_LOCATION (c),
11317 "%qE is not a variable in %qs clause", t,
11318 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11319 return error_mark_node;
11321 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11322 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11324 error_at (OMP_CLAUSE_LOCATION (c),
11325 "%qD is threadprivate variable in %qs clause", t,
11326 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11327 return error_mark_node;
11332 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11333 maybe_zero_len, first_non_one);
11334 if (ret == error_mark_node || ret == NULL_TREE)
11337 type = TREE_TYPE (ret);
11338 low_bound = TREE_PURPOSE (t);
11339 length = TREE_VALUE (t);
11341 if (low_bound == error_mark_node || length == error_mark_node)
11342 return error_mark_node;
11344 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11346 error_at (OMP_CLAUSE_LOCATION (c),
11347 "low bound %qE of array section does not have integral type",
11349 return error_mark_node;
11351 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11353 error_at (OMP_CLAUSE_LOCATION (c),
11354 "length %qE of array section does not have integral type",
11356 return error_mark_node;
11359 && TREE_CODE (low_bound) == INTEGER_CST
11360 && TYPE_PRECISION (TREE_TYPE (low_bound))
11361 > TYPE_PRECISION (sizetype))
11362 low_bound = fold_convert (sizetype, low_bound);
11364 && TREE_CODE (length) == INTEGER_CST
11365 && TYPE_PRECISION (TREE_TYPE (length))
11366 > TYPE_PRECISION (sizetype))
11367 length = fold_convert (sizetype, length);
11368 if (low_bound == NULL_TREE)
11369 low_bound = integer_zero_node;
11371 if (length != NULL_TREE)
11373 if (!integer_nonzerop (length))
11374 maybe_zero_len = true;
11375 if (first_non_one == types.length ()
11376 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11379 if (TREE_CODE (type) == ARRAY_TYPE)
11381 if (length == NULL_TREE
11382 && (TYPE_DOMAIN (type) == NULL_TREE
11383 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11385 error_at (OMP_CLAUSE_LOCATION (c),
11386 "for unknown bound array type length expression must "
11388 return error_mark_node;
11390 if (TREE_CODE (low_bound) == INTEGER_CST
11391 && tree_int_cst_sgn (low_bound) == -1)
11393 error_at (OMP_CLAUSE_LOCATION (c),
11394 "negative low bound in array section in %qs clause",
11395 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11396 return error_mark_node;
11398 if (length != NULL_TREE
11399 && TREE_CODE (length) == INTEGER_CST
11400 && tree_int_cst_sgn (length) == -1)
11402 error_at (OMP_CLAUSE_LOCATION (c),
11403 "negative length in array section in %qs clause",
11404 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11405 return error_mark_node;
11407 if (TYPE_DOMAIN (type)
11408 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11409 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11412 tree size = size_binop (PLUS_EXPR,
11413 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11415 if (TREE_CODE (low_bound) == INTEGER_CST)
11417 if (tree_int_cst_lt (size, low_bound))
11419 error_at (OMP_CLAUSE_LOCATION (c),
11420 "low bound %qE above array section size "
11421 "in %qs clause", low_bound,
11422 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11423 return error_mark_node;
11425 if (tree_int_cst_equal (size, low_bound))
11426 maybe_zero_len = true;
11427 else if (length == NULL_TREE
11428 && first_non_one == types.length ()
11429 && tree_int_cst_equal
11430 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11434 else if (length == NULL_TREE)
11436 maybe_zero_len = true;
11437 if (first_non_one == types.length ())
11440 if (length && TREE_CODE (length) == INTEGER_CST)
11442 if (tree_int_cst_lt (size, length))
11444 error_at (OMP_CLAUSE_LOCATION (c),
11445 "length %qE above array section size "
11446 "in %qs clause", length,
11447 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11448 return error_mark_node;
11450 if (TREE_CODE (low_bound) == INTEGER_CST)
11453 = size_binop (PLUS_EXPR,
11454 fold_convert (sizetype, low_bound),
11455 fold_convert (sizetype, length));
11456 if (TREE_CODE (lbpluslen) == INTEGER_CST
11457 && tree_int_cst_lt (size, lbpluslen))
11459 error_at (OMP_CLAUSE_LOCATION (c),
11460 "high bound %qE above array section size "
11461 "in %qs clause", lbpluslen,
11462 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11463 return error_mark_node;
11468 else if (length == NULL_TREE)
11470 maybe_zero_len = true;
11471 if (first_non_one == types.length ())
11475 /* For [lb:] we will need to evaluate lb more than once. */
11476 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11478 tree lb = c_save_expr (low_bound);
11479 if (lb != low_bound)
11481 TREE_PURPOSE (t) = lb;
11486 else if (TREE_CODE (type) == POINTER_TYPE)
11488 if (length == NULL_TREE)
11490 error_at (OMP_CLAUSE_LOCATION (c),
11491 "for pointer type length expression must be specified");
11492 return error_mark_node;
11494 /* If there is a pointer type anywhere but in the very first
11495 array-section-subscript, the array section can't be contiguous. */
11496 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11497 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11499 error_at (OMP_CLAUSE_LOCATION (c),
11500 "array section is not contiguous in %qs clause",
11501 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11502 return error_mark_node;
11507 error_at (OMP_CLAUSE_LOCATION (c),
11508 "%qE does not have pointer or array type", ret);
11509 return error_mark_node;
11511 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11512 types.safe_push (TREE_TYPE (ret));
11513 /* We will need to evaluate lb more than once. */
11514 tree lb = c_save_expr (low_bound);
11515 if (lb != low_bound)
11517 TREE_PURPOSE (t) = lb;
11520 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11524 /* Handle array sections for clause C. */
11527 handle_omp_array_sections (tree c)
11529 bool maybe_zero_len = false;
11530 unsigned int first_non_one = 0;
11531 vec<tree> types = vNULL;
11532 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11533 maybe_zero_len, first_non_one);
11534 if (first == error_mark_node)
11539 if (first == NULL_TREE)
11544 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11546 tree t = OMP_CLAUSE_DECL (c);
11547 tree tem = NULL_TREE;
11549 /* Need to evaluate side effects in the length expressions
11551 while (TREE_CODE (t) == TREE_LIST)
11553 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11555 if (tem == NULL_TREE)
11556 tem = TREE_VALUE (t);
11558 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11559 TREE_VALUE (t), tem);
11561 t = TREE_CHAIN (t);
11564 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11565 first = c_fully_fold (first, false, NULL);
11566 OMP_CLAUSE_DECL (c) = first;
11570 unsigned int num = types.length (), i;
11571 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11572 tree condition = NULL_TREE;
11574 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11575 maybe_zero_len = true;
11577 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11578 t = TREE_CHAIN (t))
11580 tree low_bound = TREE_PURPOSE (t);
11581 tree length = TREE_VALUE (t);
11585 && TREE_CODE (low_bound) == INTEGER_CST
11586 && TYPE_PRECISION (TREE_TYPE (low_bound))
11587 > TYPE_PRECISION (sizetype))
11588 low_bound = fold_convert (sizetype, low_bound);
11590 && TREE_CODE (length) == INTEGER_CST
11591 && TYPE_PRECISION (TREE_TYPE (length))
11592 > TYPE_PRECISION (sizetype))
11593 length = fold_convert (sizetype, length);
11594 if (low_bound == NULL_TREE)
11595 low_bound = integer_zero_node;
11596 if (!maybe_zero_len && i > first_non_one)
11598 if (integer_nonzerop (low_bound))
11599 goto do_warn_noncontiguous;
11600 if (length != NULL_TREE
11601 && TREE_CODE (length) == INTEGER_CST
11602 && TYPE_DOMAIN (types[i])
11603 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11604 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11608 size = size_binop (PLUS_EXPR,
11609 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11611 if (!tree_int_cst_equal (length, size))
11613 do_warn_noncontiguous:
11614 error_at (OMP_CLAUSE_LOCATION (c),
11615 "array section is not contiguous in %qs "
11617 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11622 if (length != NULL_TREE
11623 && TREE_SIDE_EFFECTS (length))
11625 if (side_effects == NULL_TREE)
11626 side_effects = length;
11628 side_effects = build2 (COMPOUND_EXPR,
11629 TREE_TYPE (side_effects),
11630 length, side_effects);
11637 if (i > first_non_one && length && integer_nonzerop (length))
11640 l = fold_convert (sizetype, length);
11643 l = size_binop (PLUS_EXPR,
11644 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11646 l = size_binop (MINUS_EXPR, l,
11647 fold_convert (sizetype, low_bound));
11649 if (i > first_non_one)
11651 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11653 if (condition == NULL_TREE)
11656 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11659 else if (size == NULL_TREE)
11661 size = size_in_bytes (TREE_TYPE (types[i]));
11662 size = size_binop (MULT_EXPR, size, l);
11664 size = fold_build3 (COND_EXPR, sizetype, condition,
11665 size, size_zero_node);
11668 size = size_binop (MULT_EXPR, size, l);
11673 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11674 first = c_fully_fold (first, false, NULL);
11675 OMP_CLAUSE_DECL (c) = first;
11677 size = c_fully_fold (size, false, NULL);
11678 OMP_CLAUSE_SIZE (c) = size;
11679 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11681 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11682 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11683 if (!c_mark_addressable (t))
11685 OMP_CLAUSE_DECL (c2) = t;
11686 t = build_fold_addr_expr (first);
11687 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11688 tree ptr = OMP_CLAUSE_DECL (c2);
11689 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11690 ptr = build_fold_addr_expr (ptr);
11691 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11692 ptrdiff_type_node, t,
11693 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11694 ptrdiff_type_node, ptr));
11695 t = c_fully_fold (t, false, NULL);
11696 OMP_CLAUSE_SIZE (c2) = t;
11697 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11698 OMP_CLAUSE_CHAIN (c) = c2;
11703 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11704 an inline call. But, remap
11705 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11706 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11709 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11710 tree decl, tree placeholder)
11713 struct pointer_map_t *decl_map = pointer_map_create ();
11715 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
11716 *pointer_map_insert (decl_map, omp_decl2) = decl;
11717 memset (&id, 0, sizeof (id));
11718 id.src_fn = DECL_CONTEXT (omp_decl1);
11719 id.dst_fn = current_function_decl;
11720 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11721 id.decl_map = decl_map;
11723 id.copy_decl = copy_decl_no_change;
11724 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11725 id.transform_new_cfg = true;
11726 id.transform_return_to_modify = false;
11727 id.transform_lang_insert_block = NULL;
11729 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11730 pointer_map_destroy (decl_map);
11734 /* Helper function of c_finish_omp_clauses, called via walk_tree.
11735 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11738 c_find_omp_placeholder_r (tree *tp, int *, void *data)
11740 if (*tp == (tree) data)
11745 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
11746 Remove any elements from the list that are invalid. */
11749 c_finish_omp_clauses (tree clauses)
11751 bitmap_head generic_head, firstprivate_head, lastprivate_head;
11752 bitmap_head aligned_head;
11753 tree c, t, *pc = &clauses;
11754 bool branch_seen = false;
11755 bool copyprivate_seen = false;
11756 tree *nowait_clause = NULL;
11758 bitmap_obstack_initialize (NULL);
11759 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11760 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11761 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
11762 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
11764 for (pc = &clauses, c = clauses; c ; c = *pc)
11766 bool remove = false;
11767 bool need_complete = false;
11768 bool need_implicitly_determined = false;
11770 switch (OMP_CLAUSE_CODE (c))
11772 case OMP_CLAUSE_SHARED:
11773 need_implicitly_determined = true;
11774 goto check_dup_generic;
11776 case OMP_CLAUSE_PRIVATE:
11777 need_complete = true;
11778 need_implicitly_determined = true;
11779 goto check_dup_generic;
11781 case OMP_CLAUSE_REDUCTION:
11782 need_implicitly_determined = true;
11783 t = OMP_CLAUSE_DECL (c);
11784 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
11785 && (FLOAT_TYPE_P (TREE_TYPE (t))
11786 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
11788 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11789 const char *r_name = NULL;
11798 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11802 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11814 case TRUTH_ANDIF_EXPR:
11815 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11818 case TRUTH_ORIF_EXPR:
11819 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11823 gcc_unreachable ();
11827 error_at (OMP_CLAUSE_LOCATION (c),
11828 "%qE has invalid type for %<reduction(%s)%>",
11834 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11836 error_at (OMP_CLAUSE_LOCATION (c),
11837 "user defined reduction not found for %qD", t);
11841 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11843 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11844 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11845 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11846 VAR_DECL, NULL_TREE, type);
11847 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11848 DECL_ARTIFICIAL (placeholder) = 1;
11849 DECL_IGNORED_P (placeholder) = 1;
11850 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11851 c_mark_addressable (placeholder);
11852 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11853 c_mark_addressable (OMP_CLAUSE_DECL (c));
11854 OMP_CLAUSE_REDUCTION_MERGE (c)
11855 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11856 TREE_VEC_ELT (list, 0),
11857 TREE_VEC_ELT (list, 1),
11858 OMP_CLAUSE_DECL (c), placeholder);
11859 OMP_CLAUSE_REDUCTION_MERGE (c)
11860 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11861 void_type_node, NULL_TREE,
11862 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11863 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11864 if (TREE_VEC_LENGTH (list) == 6)
11866 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11867 c_mark_addressable (OMP_CLAUSE_DECL (c));
11868 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11869 c_mark_addressable (placeholder);
11870 tree init = TREE_VEC_ELT (list, 5);
11871 if (init == error_mark_node)
11872 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11873 OMP_CLAUSE_REDUCTION_INIT (c)
11874 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11875 TREE_VEC_ELT (list, 3),
11876 OMP_CLAUSE_DECL (c), placeholder);
11877 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11878 OMP_CLAUSE_REDUCTION_INIT (c)
11879 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11880 OMP_CLAUSE_REDUCTION_INIT (c));
11881 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11882 c_find_omp_placeholder_r,
11883 placeholder, NULL))
11884 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11889 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11890 init = build_constructor (TREE_TYPE (t), NULL);
11892 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11893 OMP_CLAUSE_REDUCTION_INIT (c)
11894 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11896 OMP_CLAUSE_REDUCTION_INIT (c)
11897 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11898 void_type_node, NULL_TREE,
11899 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11900 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11902 goto check_dup_generic;
11904 case OMP_CLAUSE_COPYPRIVATE:
11905 copyprivate_seen = true;
11908 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11909 "%<nowait%> clause must not be used together "
11910 "with %<copyprivate%>");
11911 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
11912 nowait_clause = NULL;
11914 goto check_dup_generic;
11916 case OMP_CLAUSE_COPYIN:
11917 t = OMP_CLAUSE_DECL (c);
11918 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
11920 error_at (OMP_CLAUSE_LOCATION (c),
11921 "%qE must be %<threadprivate%> for %<copyin%>", t);
11925 goto check_dup_generic;
11927 case OMP_CLAUSE_LINEAR:
11928 t = OMP_CLAUSE_DECL (c);
11929 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11930 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
11932 error_at (OMP_CLAUSE_LOCATION (c),
11933 "linear clause applied to non-integral non-pointer "
11934 "variable with type %qT", TREE_TYPE (t));
11938 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
11940 tree s = OMP_CLAUSE_LINEAR_STEP (c);
11941 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
11942 OMP_CLAUSE_DECL (c), s);
11943 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11944 sizetype, s, OMP_CLAUSE_DECL (c));
11945 if (s == error_mark_node)
11947 OMP_CLAUSE_LINEAR_STEP (c) = s;
11949 goto check_dup_generic;
11952 t = OMP_CLAUSE_DECL (c);
11953 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11955 error_at (OMP_CLAUSE_LOCATION (c),
11956 "%qE is not a variable in clause %qs", t,
11957 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11960 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11961 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
11962 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11964 error_at (OMP_CLAUSE_LOCATION (c),
11965 "%qE appears more than once in data clauses", t);
11969 bitmap_set_bit (&generic_head, DECL_UID (t));
11972 case OMP_CLAUSE_FIRSTPRIVATE:
11973 t = OMP_CLAUSE_DECL (c);
11974 need_complete = true;
11975 need_implicitly_determined = true;
11976 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11978 error_at (OMP_CLAUSE_LOCATION (c),
11979 "%qE is not a variable in clause %<firstprivate%>", t);
11982 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11983 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
11985 error_at (OMP_CLAUSE_LOCATION (c),
11986 "%qE appears more than once in data clauses", t);
11990 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
11993 case OMP_CLAUSE_LASTPRIVATE:
11994 t = OMP_CLAUSE_DECL (c);
11995 need_complete = true;
11996 need_implicitly_determined = true;
11997 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11999 error_at (OMP_CLAUSE_LOCATION (c),
12000 "%qE is not a variable in clause %<lastprivate%>", t);
12003 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12004 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12006 error_at (OMP_CLAUSE_LOCATION (c),
12007 "%qE appears more than once in data clauses", t);
12011 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12014 case OMP_CLAUSE_ALIGNED:
12015 t = OMP_CLAUSE_DECL (c);
12016 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12018 error_at (OMP_CLAUSE_LOCATION (c),
12019 "%qE is not a variable in %<aligned%> clause", t);
12022 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12023 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12025 error_at (OMP_CLAUSE_LOCATION (c),
12026 "%qE in %<aligned%> clause is neither a pointer nor "
12030 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12032 error_at (OMP_CLAUSE_LOCATION (c),
12033 "%qE appears more than once in %<aligned%> clauses",
12038 bitmap_set_bit (&aligned_head, DECL_UID (t));
12041 case OMP_CLAUSE_DEPEND:
12042 t = OMP_CLAUSE_DECL (c);
12043 if (TREE_CODE (t) == TREE_LIST)
12045 if (handle_omp_array_sections (c))
12049 if (t == error_mark_node)
12051 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12053 error_at (OMP_CLAUSE_LOCATION (c),
12054 "%qE is not a variable in %<depend%> clause", t);
12057 else if (!c_mark_addressable (t))
12061 case OMP_CLAUSE_MAP:
12062 case OMP_CLAUSE_TO:
12063 case OMP_CLAUSE_FROM:
12064 t = OMP_CLAUSE_DECL (c);
12065 if (TREE_CODE (t) == TREE_LIST)
12067 if (handle_omp_array_sections (c))
12071 t = OMP_CLAUSE_DECL (c);
12072 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12074 error_at (OMP_CLAUSE_LOCATION (c),
12075 "array section does not have mappable type "
12077 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12083 if (t == error_mark_node)
12085 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12087 error_at (OMP_CLAUSE_LOCATION (c),
12088 "%qE is not a variable in %qs clause", t,
12089 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12092 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12094 error_at (OMP_CLAUSE_LOCATION (c),
12095 "%qD is threadprivate variable in %qs clause", t,
12096 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12099 else if (!c_mark_addressable (t))
12101 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12102 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
12103 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12105 error_at (OMP_CLAUSE_LOCATION (c),
12106 "%qD does not have a mappable type in %qs clause", t,
12107 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12110 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12112 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12113 error ("%qD appears more than once in motion clauses", t);
12115 error ("%qD appears more than once in map clauses", t);
12119 bitmap_set_bit (&generic_head, DECL_UID (t));
12122 case OMP_CLAUSE_UNIFORM:
12123 t = OMP_CLAUSE_DECL (c);
12124 if (TREE_CODE (t) != PARM_DECL)
12127 error_at (OMP_CLAUSE_LOCATION (c),
12128 "%qD is not an argument in %<uniform%> clause", t);
12130 error_at (OMP_CLAUSE_LOCATION (c),
12131 "%qE is not an argument in %<uniform%> clause", t);
12135 goto check_dup_generic;
12137 case OMP_CLAUSE_NOWAIT:
12138 if (copyprivate_seen)
12140 error_at (OMP_CLAUSE_LOCATION (c),
12141 "%<nowait%> clause must not be used together "
12142 "with %<copyprivate%>");
12146 nowait_clause = pc;
12147 pc = &OMP_CLAUSE_CHAIN (c);
12150 case OMP_CLAUSE_IF:
12151 case OMP_CLAUSE_NUM_THREADS:
12152 case OMP_CLAUSE_NUM_TEAMS:
12153 case OMP_CLAUSE_THREAD_LIMIT:
12154 case OMP_CLAUSE_SCHEDULE:
12155 case OMP_CLAUSE_ORDERED:
12156 case OMP_CLAUSE_DEFAULT:
12157 case OMP_CLAUSE_UNTIED:
12158 case OMP_CLAUSE_COLLAPSE:
12159 case OMP_CLAUSE_FINAL:
12160 case OMP_CLAUSE_MERGEABLE:
12161 case OMP_CLAUSE_SAFELEN:
12162 case OMP_CLAUSE_SIMDLEN:
12163 case OMP_CLAUSE_DEVICE:
12164 case OMP_CLAUSE_DIST_SCHEDULE:
12165 case OMP_CLAUSE_PARALLEL:
12166 case OMP_CLAUSE_FOR:
12167 case OMP_CLAUSE_SECTIONS:
12168 case OMP_CLAUSE_TASKGROUP:
12169 case OMP_CLAUSE_PROC_BIND:
12170 pc = &OMP_CLAUSE_CHAIN (c);
12173 case OMP_CLAUSE_INBRANCH:
12174 case OMP_CLAUSE_NOTINBRANCH:
12177 error_at (OMP_CLAUSE_LOCATION (c),
12178 "%<inbranch%> clause is incompatible with "
12179 "%<notinbranch%>");
12183 branch_seen = true;
12184 pc = &OMP_CLAUSE_CHAIN (c);
12188 gcc_unreachable ();
12193 t = OMP_CLAUSE_DECL (c);
12197 t = require_complete_type (t);
12198 if (t == error_mark_node)
12202 if (need_implicitly_determined)
12204 const char *share_name = NULL;
12206 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12207 share_name = "threadprivate";
12208 else switch (c_omp_predetermined_sharing (t))
12210 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12212 case OMP_CLAUSE_DEFAULT_SHARED:
12213 /* const vars may be specified in firstprivate clause. */
12214 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12215 && TREE_READONLY (t))
12217 share_name = "shared";
12219 case OMP_CLAUSE_DEFAULT_PRIVATE:
12220 share_name = "private";
12223 gcc_unreachable ();
12227 error_at (OMP_CLAUSE_LOCATION (c),
12228 "%qE is predetermined %qs for %qs",
12230 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12237 *pc = OMP_CLAUSE_CHAIN (c);
12239 pc = &OMP_CLAUSE_CHAIN (c);
12242 bitmap_obstack_release (NULL);
12246 /* Create a transaction node. */
12249 c_finish_transaction (location_t loc, tree block, int flags)
12251 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12252 if (flags & TM_STMT_ATTR_OUTER)
12253 TRANSACTION_EXPR_OUTER (stmt) = 1;
12254 if (flags & TM_STMT_ATTR_RELAXED)
12255 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12256 return add_stmt (stmt);
12259 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12260 down to the element type of an array. */
12263 c_build_qualified_type (tree type, int type_quals)
12265 if (type == error_mark_node)
12268 if (TREE_CODE (type) == ARRAY_TYPE)
12271 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12274 /* See if we already have an identically qualified type. */
12275 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12277 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12278 && TYPE_NAME (t) == TYPE_NAME (type)
12279 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12280 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12281 TYPE_ATTRIBUTES (type)))
12286 tree domain = TYPE_DOMAIN (type);
12288 t = build_variant_type_copy (type);
12289 TREE_TYPE (t) = element_type;
12291 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12292 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12293 SET_TYPE_STRUCTURAL_EQUALITY (t);
12294 else if (TYPE_CANONICAL (element_type) != element_type
12295 || (domain && TYPE_CANONICAL (domain) != domain))
12297 tree unqualified_canon
12298 = build_array_type (TYPE_CANONICAL (element_type),
12299 domain? TYPE_CANONICAL (domain)
12302 = c_build_qualified_type (unqualified_canon, type_quals);
12305 TYPE_CANONICAL (t) = t;
12310 /* A restrict-qualified pointer type must be a pointer to object or
12311 incomplete type. Note that the use of POINTER_TYPE_P also allows
12312 REFERENCE_TYPEs, which is appropriate for C++. */
12313 if ((type_quals & TYPE_QUAL_RESTRICT)
12314 && (!POINTER_TYPE_P (type)
12315 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12317 error ("invalid use of %<restrict%>");
12318 type_quals &= ~TYPE_QUAL_RESTRICT;
12321 return build_qualified_type (type, type_quals);
12324 /* Build a VA_ARG_EXPR for the C parser. */
12327 c_build_va_arg (location_t loc, tree expr, tree type)
12329 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12330 warning_at (loc, OPT_Wc___compat,
12331 "C++ requires promoted type, not enum type, in %<va_arg%>");
12332 return build_va_arg (loc, expr, type);
12335 /* Return truthvalue of whether T1 is the same tree structure as T2.
12336 Return 1 if they are the same. Return 0 if they are different. */
12339 c_tree_equal (tree t1, tree t2)
12341 enum tree_code code1, code2;
12348 for (code1 = TREE_CODE (t1);
12349 CONVERT_EXPR_CODE_P (code1)
12350 || code1 == NON_LVALUE_EXPR;
12351 code1 = TREE_CODE (t1))
12352 t1 = TREE_OPERAND (t1, 0);
12353 for (code2 = TREE_CODE (t2);
12354 CONVERT_EXPR_CODE_P (code2)
12355 || code2 == NON_LVALUE_EXPR;
12356 code2 = TREE_CODE (t2))
12357 t2 = TREE_OPERAND (t2, 0);
12359 /* They might have become equal now. */
12363 if (code1 != code2)
12369 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
12370 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
12373 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12376 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12377 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12378 TREE_STRING_LENGTH (t1));
12381 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12382 TREE_FIXED_CST (t2));
12385 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12386 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12389 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12392 /* We need to do this when determining whether or not two
12393 non-type pointer to member function template arguments
12395 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12396 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12401 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12403 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12404 if (!c_tree_equal (field, elt2->index)
12405 || !c_tree_equal (value, elt2->value))
12412 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12414 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12416 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12419 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12424 call_expr_arg_iterator iter1, iter2;
12425 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12427 for (arg1 = first_call_expr_arg (t1, &iter1),
12428 arg2 = first_call_expr_arg (t2, &iter2);
12430 arg1 = next_call_expr_arg (&iter1),
12431 arg2 = next_call_expr_arg (&iter2))
12432 if (!c_tree_equal (arg1, arg2))
12441 tree o1 = TREE_OPERAND (t1, 0);
12442 tree o2 = TREE_OPERAND (t2, 0);
12444 /* Special case: if either target is an unallocated VAR_DECL,
12445 it means that it's going to be unified with whatever the
12446 TARGET_EXPR is really supposed to initialize, so treat it
12447 as being equivalent to anything. */
12448 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12449 && !DECL_RTL_SET_P (o1))
12451 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12452 && !DECL_RTL_SET_P (o2))
12454 else if (!c_tree_equal (o1, o2))
12457 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12460 case COMPONENT_REF:
12461 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12463 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12469 case FUNCTION_DECL:
12470 case IDENTIFIER_NODE:
12477 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12479 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12480 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12481 TREE_VEC_ELT (t2, ix)))
12490 switch (TREE_CODE_CLASS (code1))
12494 case tcc_comparison:
12495 case tcc_expression:
12497 case tcc_reference:
12498 case tcc_statement:
12500 int i, n = TREE_OPERAND_LENGTH (t1);
12504 case PREINCREMENT_EXPR:
12505 case PREDECREMENT_EXPR:
12506 case POSTINCREMENT_EXPR:
12507 case POSTDECREMENT_EXPR:
12517 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12518 && n != TREE_OPERAND_LENGTH (t2))
12521 for (i = 0; i < n; ++i)
12522 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12529 return comptypes (t1, t2);
12531 gcc_unreachable ();
12533 /* We can get here with --disable-checking. */
12537 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12538 spawn-helper and BODY is the newly created body for FNDECL. */
12541 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12543 tree list = alloc_stmt_list ();
12544 tree frame = make_cilk_frame (fndecl);
12545 tree dtor = create_cilk_function_exit (frame, false, true);
12546 add_local_decl (cfun, frame);
12548 DECL_SAVED_TREE (fndecl) = list;
12549 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12551 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12552 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12554 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12555 append_to_statement_list (detach_expr, &body_list);
12557 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12558 body = fold_build_cleanup_point_expr (void_type_node, body);
12560 append_to_statement_list (body, &body_list);
12561 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12562 body_list, dtor), &list);