1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2015 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"
35 #include "double-int.h"
39 #include "fixed-value.h"
41 #include "fold-const.h"
42 #include "stor-layout.h"
43 #include "trans-mem.h"
46 #include "langhooks.h"
52 #include "tree-iterator.h"
59 #include "hard-reg-set.h"
62 #include "gimple-expr.h"
64 #include "tree-inline.h"
66 #include "c-family/c-objc.h"
67 #include "c-family/c-common.h"
68 #include "c-family/c-ubsan.h"
71 #include "gomp-constants.h"
73 /* Possible cases of implicit bad conversions. Used to select
74 diagnostic messages in convert_for_assignment. */
82 /* The level of nesting inside "__alignof__". */
85 /* The level of nesting inside "sizeof". */
88 /* The level of nesting inside "typeof". */
91 /* The argument of last parsed sizeof expression, only to be tested
92 if expr.original_code == SIZEOF_EXPR. */
93 tree c_last_sizeof_arg;
95 /* Nonzero if we might need to print a "missing braces around
96 initializer" message within this initializer. */
97 static int found_missing_braces;
99 static int require_constant_value;
100 static int require_constant_elements;
102 static bool null_pointer_constant_p (const_tree);
103 static tree qualify_type (tree, tree);
104 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
106 static int comp_target_types (location_t, tree, tree);
107 static int function_types_compatible_p (const_tree, const_tree, bool *,
109 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
110 static tree lookup_field (tree, tree);
111 static int convert_arguments (location_t, vec<location_t>, tree,
112 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
114 static tree pointer_diff (location_t, tree, tree);
115 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
116 enum impl_conv, bool, tree, tree, int);
117 static tree valid_compound_expr_initializer (tree, tree);
118 static void push_string (const char *);
119 static void push_member_name (tree);
120 static int spelling_length (void);
121 static char *print_spelling (char *);
122 static void warning_init (location_t, int, const char *);
123 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
124 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
125 bool, struct obstack *);
126 static void output_pending_init_elements (int, struct obstack *);
127 static int set_designator (location_t, int, struct obstack *);
128 static void push_range_stack (tree, struct obstack *);
129 static void add_pending_init (location_t, tree, tree, tree, bool,
131 static void set_nonincremental_init (struct obstack *);
132 static void set_nonincremental_init_from_string (tree, struct obstack *);
133 static tree find_init_member (tree, struct obstack *);
134 static void readonly_warning (tree, enum lvalue_use);
135 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
136 static void record_maybe_used_decl (tree);
137 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
139 /* Return true if EXP is a null pointer constant, false otherwise. */
142 null_pointer_constant_p (const_tree expr)
144 /* This should really operate on c_expr structures, but they aren't
145 yet available everywhere required. */
146 tree type = TREE_TYPE (expr);
147 return (TREE_CODE (expr) == INTEGER_CST
148 && !TREE_OVERFLOW (expr)
149 && integer_zerop (expr)
150 && (INTEGRAL_TYPE_P (type)
151 || (TREE_CODE (type) == POINTER_TYPE
152 && VOID_TYPE_P (TREE_TYPE (type))
153 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
156 /* EXPR may appear in an unevaluated part of an integer constant
157 expression, but not in an evaluated part. Wrap it in a
158 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
159 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
162 note_integer_operands (tree expr)
165 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
167 ret = copy_node (expr);
168 TREE_OVERFLOW (ret) = 1;
172 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
173 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
178 /* Having checked whether EXPR may appear in an unevaluated part of an
179 integer constant expression and found that it may, remove any
180 C_MAYBE_CONST_EXPR noting this fact and return the resulting
184 remove_c_maybe_const_expr (tree expr)
186 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
187 return C_MAYBE_CONST_EXPR_EXPR (expr);
192 \f/* This is a cache to hold if two types are compatible or not. */
194 struct tagged_tu_seen_cache {
195 const struct tagged_tu_seen_cache * next;
198 /* The return value of tagged_types_tu_compatible_p if we had seen
199 these two types already. */
203 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
204 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
206 /* Do `exp = require_complete_type (exp);' to make sure exp
207 does not have an incomplete type. (That includes void types.) */
210 require_complete_type (tree value)
212 tree type = TREE_TYPE (value);
214 if (error_operand_p (value))
215 return error_mark_node;
217 /* First, detect a valid value with a complete type. */
218 if (COMPLETE_TYPE_P (type))
221 c_incomplete_type_error (value, type);
222 return error_mark_node;
225 /* Print an error message for invalid use of an incomplete type.
226 VALUE is the expression that was used (or 0 if that isn't known)
227 and TYPE is the type that was invalid. */
230 c_incomplete_type_error (const_tree value, const_tree type)
232 const char *type_code_string;
234 /* Avoid duplicate error message. */
235 if (TREE_CODE (type) == ERROR_MARK)
238 if (value != 0 && (TREE_CODE (value) == VAR_DECL
239 || TREE_CODE (value) == PARM_DECL))
240 error ("%qD has an incomplete type", value);
244 /* We must print an error message. Be clever about what it says. */
246 switch (TREE_CODE (type))
249 type_code_string = "struct";
253 type_code_string = "union";
257 type_code_string = "enum";
261 error ("invalid use of void expression");
265 if (TYPE_DOMAIN (type))
267 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
269 error ("invalid use of flexible array member");
272 type = TREE_TYPE (type);
275 error ("invalid use of array with unspecified bounds");
282 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
283 error ("invalid use of undefined type %<%s %E%>",
284 type_code_string, TYPE_NAME (type));
286 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
287 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
291 /* Given a type, apply default promotions wrt unnamed function
292 arguments and return the new type. */
295 c_type_promotes_to (tree type)
297 tree ret = NULL_TREE;
299 if (TYPE_MAIN_VARIANT (type) == float_type_node)
300 ret = double_type_node;
301 else if (c_promoting_integer_type_p (type))
303 /* Preserve unsignedness if not really getting any wider. */
304 if (TYPE_UNSIGNED (type)
305 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
306 ret = unsigned_type_node;
308 ret = integer_type_node;
311 if (ret != NULL_TREE)
312 return (TYPE_ATOMIC (type)
313 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
319 /* Return true if between two named address spaces, whether there is a superset
320 named address space that encompasses both address spaces. If there is a
321 superset, return which address space is the superset. */
324 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
331 else if (targetm.addr_space.subset_p (as1, as2))
336 else if (targetm.addr_space.subset_p (as2, as1))
345 /* Return a variant of TYPE which has all the type qualifiers of LIKE
346 as well as those of TYPE. */
349 qualify_type (tree type, tree like)
351 addr_space_t as_type = TYPE_ADDR_SPACE (type);
352 addr_space_t as_like = TYPE_ADDR_SPACE (like);
353 addr_space_t as_common;
355 /* If the two named address spaces are different, determine the common
356 superset address space. If there isn't one, raise an error. */
357 if (!addr_space_superset (as_type, as_like, &as_common))
360 error ("%qT and %qT are in disjoint named address spaces",
364 return c_build_qualified_type (type,
365 TYPE_QUALS_NO_ADDR_SPACE (type)
366 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
367 | ENCODE_QUAL_ADDR_SPACE (as_common));
370 /* Return true iff the given tree T is a variable length array. */
373 c_vla_type_p (const_tree t)
375 if (TREE_CODE (t) == ARRAY_TYPE
376 && C_TYPE_VARIABLE_SIZE (t))
381 /* Return the composite type of two compatible types.
383 We assume that comptypes has already been done and returned
384 nonzero; if that isn't so, this may crash. In particular, we
385 assume that qualifiers match. */
388 composite_type (tree t1, tree t2)
390 enum tree_code code1;
391 enum tree_code code2;
394 /* Save time if the two types are the same. */
396 if (t1 == t2) return t1;
398 /* If one type is nonsense, use the other. */
399 if (t1 == error_mark_node)
401 if (t2 == error_mark_node)
404 code1 = TREE_CODE (t1);
405 code2 = TREE_CODE (t2);
407 /* Merge the attributes. */
408 attributes = targetm.merge_type_attributes (t1, t2);
410 /* If one is an enumerated type and the other is the compatible
411 integer type, the composite type might be either of the two
412 (DR#013 question 3). For consistency, use the enumerated type as
413 the composite type. */
415 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
417 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
420 gcc_assert (code1 == code2);
425 /* For two pointers, do this recursively on the target type. */
427 tree pointed_to_1 = TREE_TYPE (t1);
428 tree pointed_to_2 = TREE_TYPE (t2);
429 tree target = composite_type (pointed_to_1, pointed_to_2);
430 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
431 t1 = build_type_attribute_variant (t1, attributes);
432 return qualify_type (t1, t2);
437 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
440 tree d1 = TYPE_DOMAIN (t1);
441 tree d2 = TYPE_DOMAIN (t2);
442 bool d1_variable, d2_variable;
443 bool d1_zero, d2_zero;
444 bool t1_complete, t2_complete;
446 /* We should not have any type quals on arrays at all. */
447 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
448 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
450 t1_complete = COMPLETE_TYPE_P (t1);
451 t2_complete = COMPLETE_TYPE_P (t2);
453 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
454 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
456 d1_variable = (!d1_zero
457 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
458 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
459 d2_variable = (!d2_zero
460 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
461 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
462 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
463 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
465 /* Save space: see if the result is identical to one of the args. */
466 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
467 && (d2_variable || d2_zero || !d1_variable))
468 return build_type_attribute_variant (t1, attributes);
469 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
470 && (d1_variable || d1_zero || !d2_variable))
471 return build_type_attribute_variant (t2, attributes);
473 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
474 return build_type_attribute_variant (t1, attributes);
475 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
476 return build_type_attribute_variant (t2, attributes);
478 /* Merge the element types, and have a size if either arg has
479 one. We may have qualifiers on the element types. To set
480 up TYPE_MAIN_VARIANT correctly, we need to form the
481 composite of the unqualified types and add the qualifiers
483 quals = TYPE_QUALS (strip_array_types (elt));
484 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
485 t1 = build_array_type (unqual_elt,
486 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
492 /* Ensure a composite type involving a zero-length array type
493 is a zero-length type not an incomplete type. */
494 if (d1_zero && d2_zero
495 && (t1_complete || t2_complete)
496 && !COMPLETE_TYPE_P (t1))
498 TYPE_SIZE (t1) = bitsize_zero_node;
499 TYPE_SIZE_UNIT (t1) = size_zero_node;
501 t1 = c_build_qualified_type (t1, quals);
502 return build_type_attribute_variant (t1, attributes);
508 if (attributes != NULL)
510 /* Try harder not to create a new aggregate type. */
511 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
513 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
516 return build_type_attribute_variant (t1, attributes);
519 /* Function types: prefer the one that specified arg types.
520 If both do, merge the arg types. Also merge the return types. */
522 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
523 tree p1 = TYPE_ARG_TYPES (t1);
524 tree p2 = TYPE_ARG_TYPES (t2);
529 /* Save space: see if the result is identical to one of the args. */
530 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
531 return build_type_attribute_variant (t1, attributes);
532 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
533 return build_type_attribute_variant (t2, attributes);
535 /* Simple way if one arg fails to specify argument types. */
536 if (TYPE_ARG_TYPES (t1) == 0)
538 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
539 t1 = build_type_attribute_variant (t1, attributes);
540 return qualify_type (t1, t2);
542 if (TYPE_ARG_TYPES (t2) == 0)
544 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
545 t1 = build_type_attribute_variant (t1, attributes);
546 return qualify_type (t1, t2);
549 /* If both args specify argument types, we must merge the two
550 lists, argument by argument. */
552 len = list_length (p1);
555 for (i = 0; i < len; i++)
556 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
561 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
563 /* A null type means arg type is not specified.
564 Take whatever the other function type has. */
565 if (TREE_VALUE (p1) == 0)
567 TREE_VALUE (n) = TREE_VALUE (p2);
570 if (TREE_VALUE (p2) == 0)
572 TREE_VALUE (n) = TREE_VALUE (p1);
576 /* Given wait (union {union wait *u; int *i} *)
577 and wait (union wait *),
578 prefer union wait * as type of parm. */
579 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
580 && TREE_VALUE (p1) != TREE_VALUE (p2))
583 tree mv2 = TREE_VALUE (p2);
584 if (mv2 && mv2 != error_mark_node
585 && TREE_CODE (mv2) != ARRAY_TYPE)
586 mv2 = TYPE_MAIN_VARIANT (mv2);
587 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
588 memb; memb = DECL_CHAIN (memb))
590 tree mv3 = TREE_TYPE (memb);
591 if (mv3 && mv3 != error_mark_node
592 && TREE_CODE (mv3) != ARRAY_TYPE)
593 mv3 = TYPE_MAIN_VARIANT (mv3);
594 if (comptypes (mv3, mv2))
596 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
598 pedwarn (input_location, OPT_Wpedantic,
599 "function types not truly compatible in ISO C");
604 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
605 && TREE_VALUE (p2) != TREE_VALUE (p1))
608 tree mv1 = TREE_VALUE (p1);
609 if (mv1 && mv1 != error_mark_node
610 && TREE_CODE (mv1) != ARRAY_TYPE)
611 mv1 = TYPE_MAIN_VARIANT (mv1);
612 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
613 memb; memb = DECL_CHAIN (memb))
615 tree mv3 = TREE_TYPE (memb);
616 if (mv3 && mv3 != error_mark_node
617 && TREE_CODE (mv3) != ARRAY_TYPE)
618 mv3 = TYPE_MAIN_VARIANT (mv3);
619 if (comptypes (mv3, mv1))
621 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
623 pedwarn (input_location, OPT_Wpedantic,
624 "function types not truly compatible in ISO C");
629 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
633 t1 = build_function_type (valtype, newargs);
634 t1 = qualify_type (t1, t2);
635 /* ... falls through ... */
639 return build_type_attribute_variant (t1, attributes);
644 /* Return the type of a conditional expression between pointers to
645 possibly differently qualified versions of compatible types.
647 We assume that comp_target_types has already been done and returned
648 nonzero; if that isn't so, this may crash. */
651 common_pointer_type (tree t1, tree t2)
654 tree pointed_to_1, mv1;
655 tree pointed_to_2, mv2;
657 unsigned target_quals;
658 addr_space_t as1, as2, as_common;
661 /* Save time if the two types are the same. */
663 if (t1 == t2) return t1;
665 /* If one type is nonsense, use the other. */
666 if (t1 == error_mark_node)
668 if (t2 == error_mark_node)
671 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
672 && TREE_CODE (t2) == POINTER_TYPE);
674 /* Merge the attributes. */
675 attributes = targetm.merge_type_attributes (t1, t2);
677 /* Find the composite type of the target types, and combine the
678 qualifiers of the two types' targets. Do not lose qualifiers on
679 array element types by taking the TYPE_MAIN_VARIANT. */
680 mv1 = pointed_to_1 = TREE_TYPE (t1);
681 mv2 = pointed_to_2 = TREE_TYPE (t2);
682 if (TREE_CODE (mv1) != ARRAY_TYPE)
683 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
684 if (TREE_CODE (mv2) != ARRAY_TYPE)
685 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
686 target = composite_type (mv1, mv2);
688 /* Strip array types to get correct qualifier for pointers to arrays */
689 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
690 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
692 /* For function types do not merge const qualifiers, but drop them
693 if used inconsistently. The middle-end uses these to mark const
694 and noreturn functions. */
695 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
696 target_quals = (quals1 & quals2);
698 target_quals = (quals1 | quals2);
700 /* If the two named address spaces are different, determine the common
701 superset address space. This is guaranteed to exist due to the
702 assumption that comp_target_type returned non-zero. */
703 as1 = TYPE_ADDR_SPACE (pointed_to_1);
704 as2 = TYPE_ADDR_SPACE (pointed_to_2);
705 if (!addr_space_superset (as1, as2, &as_common))
708 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
710 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
711 return build_type_attribute_variant (t1, attributes);
714 /* Return the common type for two arithmetic types under the usual
715 arithmetic conversions. The default conversions have already been
716 applied, and enumerated types converted to their compatible integer
717 types. The resulting type is unqualified and has no attributes.
719 This is the type for the result of most arithmetic operations
720 if the operands have the given two types. */
723 c_common_type (tree t1, tree t2)
725 enum tree_code code1;
726 enum tree_code code2;
728 /* If one type is nonsense, use the other. */
729 if (t1 == error_mark_node)
731 if (t2 == error_mark_node)
734 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
735 t1 = TYPE_MAIN_VARIANT (t1);
737 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
738 t2 = TYPE_MAIN_VARIANT (t2);
740 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
741 t1 = build_type_attribute_variant (t1, NULL_TREE);
743 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
744 t2 = build_type_attribute_variant (t2, NULL_TREE);
746 /* Save time if the two types are the same. */
748 if (t1 == t2) return t1;
750 code1 = TREE_CODE (t1);
751 code2 = TREE_CODE (t2);
753 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
754 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
755 || code1 == INTEGER_TYPE);
756 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
757 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
758 || code2 == INTEGER_TYPE);
760 /* When one operand is a decimal float type, the other operand cannot be
761 a generic float type or a complex type. We also disallow vector types
763 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
764 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
766 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
768 error ("can%'t mix operands of decimal float and vector types");
769 return error_mark_node;
771 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
773 error ("can%'t mix operands of decimal float and complex types");
774 return error_mark_node;
776 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
778 error ("can%'t mix operands of decimal float and other float types");
779 return error_mark_node;
783 /* If one type is a vector type, return that type. (How the usual
784 arithmetic conversions apply to the vector types extension is not
785 precisely specified.) */
786 if (code1 == VECTOR_TYPE)
789 if (code2 == VECTOR_TYPE)
792 /* If one type is complex, form the common type of the non-complex
793 components, then make that complex. Use T1 or T2 if it is the
795 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
797 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
798 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
799 tree subtype = c_common_type (subtype1, subtype2);
801 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
803 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
806 return build_complex_type (subtype);
809 /* If only one is real, use it as the result. */
811 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
814 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
817 /* If both are real and either are decimal floating point types, use
818 the decimal floating point type with the greater precision. */
820 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
822 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
823 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
824 return dfloat128_type_node;
825 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
826 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
827 return dfloat64_type_node;
828 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
829 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
830 return dfloat32_type_node;
833 /* Deal with fixed-point types. */
834 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
836 unsigned int unsignedp = 0, satp = 0;
838 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
843 /* If one input type is saturating, the result type is saturating. */
844 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
847 /* If both fixed-point types are unsigned, the result type is unsigned.
848 When mixing fixed-point and integer types, follow the sign of the
850 Otherwise, the result type is signed. */
851 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
852 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
853 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
854 && TYPE_UNSIGNED (t1))
855 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
856 && TYPE_UNSIGNED (t2)))
859 /* The result type is signed. */
862 /* If the input type is unsigned, we need to convert to the
864 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
866 enum mode_class mclass = (enum mode_class) 0;
867 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
869 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
873 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
875 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
877 enum mode_class mclass = (enum mode_class) 0;
878 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
880 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
884 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
888 if (code1 == FIXED_POINT_TYPE)
890 fbit1 = GET_MODE_FBIT (m1);
891 ibit1 = GET_MODE_IBIT (m1);
896 /* Signed integers need to subtract one sign bit. */
897 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
900 if (code2 == FIXED_POINT_TYPE)
902 fbit2 = GET_MODE_FBIT (m2);
903 ibit2 = GET_MODE_IBIT (m2);
908 /* Signed integers need to subtract one sign bit. */
909 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
912 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
913 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
914 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
918 /* Both real or both integers; use the one with greater precision. */
920 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
922 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
925 /* Same precision. Prefer long longs to longs to ints when the
926 same precision, following the C99 rules on integer type rank
927 (which are equivalent to the C90 rules for C90 types). */
929 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
930 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
931 return long_long_unsigned_type_node;
933 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
934 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
936 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
937 return long_long_unsigned_type_node;
939 return long_long_integer_type_node;
942 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
943 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
944 return long_unsigned_type_node;
946 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
947 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
949 /* But preserve unsignedness from the other type,
950 since long cannot hold all the values of an unsigned int. */
951 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
952 return long_unsigned_type_node;
954 return long_integer_type_node;
957 /* Likewise, prefer long double to double even if same size. */
958 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
959 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
960 return long_double_type_node;
962 /* Likewise, prefer double to float even if same size.
963 We got a couple of embedded targets with 32 bit doubles, and the
964 pdp11 might have 64 bit floats. */
965 if (TYPE_MAIN_VARIANT (t1) == double_type_node
966 || TYPE_MAIN_VARIANT (t2) == double_type_node)
967 return double_type_node;
969 /* Otherwise prefer the unsigned one. */
971 if (TYPE_UNSIGNED (t1))
977 /* Wrapper around c_common_type that is used by c-common.c and other
978 front end optimizations that remove promotions. ENUMERAL_TYPEs
979 are allowed here and are converted to their compatible integer types.
980 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
981 preferably a non-Boolean type as the common type. */
983 common_type (tree t1, tree t2)
985 if (TREE_CODE (t1) == ENUMERAL_TYPE)
986 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
987 if (TREE_CODE (t2) == ENUMERAL_TYPE)
988 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
990 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
991 if (TREE_CODE (t1) == BOOLEAN_TYPE
992 && TREE_CODE (t2) == BOOLEAN_TYPE)
993 return boolean_type_node;
995 /* If either type is BOOLEAN_TYPE, then return the other. */
996 if (TREE_CODE (t1) == BOOLEAN_TYPE)
998 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1001 return c_common_type (t1, t2);
1004 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1005 or various other operations. Return 2 if they are compatible
1006 but a warning may be needed if you use them together. */
1009 comptypes (tree type1, tree type2)
1011 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1014 val = comptypes_internal (type1, type2, NULL, NULL);
1015 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1020 /* Like comptypes, but if it returns non-zero because enum and int are
1021 compatible, it sets *ENUM_AND_INT_P to true. */
1024 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1026 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1029 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1030 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1035 /* Like comptypes, but if it returns nonzero for different types, it
1036 sets *DIFFERENT_TYPES_P to true. */
1039 comptypes_check_different_types (tree type1, tree type2,
1040 bool *different_types_p)
1042 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1045 val = comptypes_internal (type1, type2, NULL, different_types_p);
1046 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1051 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1052 or various other operations. Return 2 if they are compatible
1053 but a warning may be needed if you use them together. If
1054 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1055 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1056 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1057 NULL, and the types are compatible but different enough not to be
1058 permitted in C11 typedef redeclarations, then this sets
1059 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1060 false, but may or may not be set if the types are incompatible.
1061 This differs from comptypes, in that we don't free the seen
1065 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1066 bool *different_types_p)
1068 const_tree t1 = type1;
1069 const_tree t2 = type2;
1072 /* Suppress errors caused by previously reported errors. */
1074 if (t1 == t2 || !t1 || !t2
1075 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1078 /* Enumerated types are compatible with integer types, but this is
1079 not transitive: two enumerated types in the same translation unit
1080 are compatible with each other only if they are the same type. */
1082 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1084 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1085 if (TREE_CODE (t2) != VOID_TYPE)
1087 if (enum_and_int_p != NULL)
1088 *enum_and_int_p = true;
1089 if (different_types_p != NULL)
1090 *different_types_p = true;
1093 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1095 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1096 if (TREE_CODE (t1) != VOID_TYPE)
1098 if (enum_and_int_p != NULL)
1099 *enum_and_int_p = true;
1100 if (different_types_p != NULL)
1101 *different_types_p = true;
1108 /* Different classes of types can't be compatible. */
1110 if (TREE_CODE (t1) != TREE_CODE (t2))
1113 /* Qualifiers must match. C99 6.7.3p9 */
1115 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1118 /* Allow for two different type nodes which have essentially the same
1119 definition. Note that we already checked for equality of the type
1120 qualifiers (just above). */
1122 if (TREE_CODE (t1) != ARRAY_TYPE
1123 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1126 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1127 if (!(attrval = comp_type_attributes (t1, t2)))
1130 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1133 switch (TREE_CODE (t1))
1136 /* Do not remove mode or aliasing information. */
1137 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1138 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1140 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1141 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1142 enum_and_int_p, different_types_p));
1146 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1152 tree d1 = TYPE_DOMAIN (t1);
1153 tree d2 = TYPE_DOMAIN (t2);
1154 bool d1_variable, d2_variable;
1155 bool d1_zero, d2_zero;
1158 /* Target types must match incl. qualifiers. */
1159 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1160 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1162 different_types_p)))
1165 if (different_types_p != NULL
1166 && (d1 == 0) != (d2 == 0))
1167 *different_types_p = true;
1168 /* Sizes must match unless one is missing or variable. */
1169 if (d1 == 0 || d2 == 0 || d1 == d2)
1172 d1_zero = !TYPE_MAX_VALUE (d1);
1173 d2_zero = !TYPE_MAX_VALUE (d2);
1175 d1_variable = (!d1_zero
1176 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1177 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1178 d2_variable = (!d2_zero
1179 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1180 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1181 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1182 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1184 if (different_types_p != NULL
1185 && d1_variable != d2_variable)
1186 *different_types_p = true;
1187 if (d1_variable || d2_variable)
1189 if (d1_zero && d2_zero)
1191 if (d1_zero || d2_zero
1192 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1193 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1202 if (val != 1 && !same_translation_unit_p (t1, t2))
1204 tree a1 = TYPE_ATTRIBUTES (t1);
1205 tree a2 = TYPE_ATTRIBUTES (t2);
1207 if (! attribute_list_contained (a1, a2)
1208 && ! attribute_list_contained (a2, a1))
1212 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1214 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1220 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1221 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1222 enum_and_int_p, different_types_p));
1228 return attrval == 2 && val == 1 ? 2 : val;
1231 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1232 their qualifiers, except for named address spaces. If the pointers point to
1233 different named addresses, then we must determine if one address space is a
1234 subset of the other. */
1237 comp_target_types (location_t location, tree ttl, tree ttr)
1241 tree mvl = TREE_TYPE (ttl);
1242 tree mvr = TREE_TYPE (ttr);
1243 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1244 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1245 addr_space_t as_common;
1246 bool enum_and_int_p;
1248 /* Fail if pointers point to incompatible address spaces. */
1249 if (!addr_space_superset (asl, asr, &as_common))
1252 /* For pedantic record result of comptypes on arrays before losing
1253 qualifiers on the element type below. */
1256 if (TREE_CODE (mvl) == ARRAY_TYPE
1257 && TREE_CODE (mvr) == ARRAY_TYPE)
1258 val_ped = comptypes (mvl, mvr);
1260 /* Qualifiers on element types of array types that are
1261 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1263 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1264 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1265 : TYPE_MAIN_VARIANT (mvl));
1267 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1268 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1269 : TYPE_MAIN_VARIANT (mvr));
1271 enum_and_int_p = false;
1272 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1274 if (val == 1 && val_ped != 1)
1275 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1276 "are incompatible in ISO C");
1279 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1281 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1282 warning_at (location, OPT_Wc___compat,
1283 "pointer target types incompatible in C++");
1288 /* Subroutines of `comptypes'. */
1290 /* Determine whether two trees derive from the same translation unit.
1291 If the CONTEXT chain ends in a null, that tree's context is still
1292 being parsed, so if two trees have context chains ending in null,
1293 they're in the same translation unit. */
1295 same_translation_unit_p (const_tree t1, const_tree t2)
1297 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1298 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1300 case tcc_declaration:
1301 t1 = DECL_CONTEXT (t1); break;
1303 t1 = TYPE_CONTEXT (t1); break;
1304 case tcc_exceptional:
1305 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1306 default: gcc_unreachable ();
1309 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1310 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1312 case tcc_declaration:
1313 t2 = DECL_CONTEXT (t2); break;
1315 t2 = TYPE_CONTEXT (t2); break;
1316 case tcc_exceptional:
1317 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1318 default: gcc_unreachable ();
1324 /* Allocate the seen two types, assuming that they are compatible. */
1326 static struct tagged_tu_seen_cache *
1327 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1329 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1330 tu->next = tagged_tu_seen_base;
1334 tagged_tu_seen_base = tu;
1336 /* The C standard says that two structures in different translation
1337 units are compatible with each other only if the types of their
1338 fields are compatible (among other things). We assume that they
1339 are compatible until proven otherwise when building the cache.
1340 An example where this can occur is:
1345 If we are comparing this against a similar struct in another TU,
1346 and did not assume they were compatible, we end up with an infinite
1352 /* Free the seen types until we get to TU_TIL. */
1355 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1357 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1358 while (tu != tu_til)
1360 const struct tagged_tu_seen_cache *const tu1
1361 = (const struct tagged_tu_seen_cache *) tu;
1363 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1365 tagged_tu_seen_base = tu_til;
1368 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1369 compatible. If the two types are not the same (which has been
1370 checked earlier), this can only happen when multiple translation
1371 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1372 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1373 comptypes_internal. */
1376 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1377 bool *enum_and_int_p, bool *different_types_p)
1380 bool needs_warning = false;
1382 /* We have to verify that the tags of the types are the same. This
1383 is harder than it looks because this may be a typedef, so we have
1384 to go look at the original type. It may even be a typedef of a
1386 In the case of compiler-created builtin structs the TYPE_DECL
1387 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1388 while (TYPE_NAME (t1)
1389 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1390 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1391 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1393 while (TYPE_NAME (t2)
1394 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1395 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1396 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1398 /* C90 didn't have the requirement that the two tags be the same. */
1399 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1402 /* C90 didn't say what happened if one or both of the types were
1403 incomplete; we choose to follow C99 rules here, which is that they
1405 if (TYPE_SIZE (t1) == NULL
1406 || TYPE_SIZE (t2) == NULL)
1410 const struct tagged_tu_seen_cache * tts_i;
1411 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1412 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1416 switch (TREE_CODE (t1))
1420 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1421 /* Speed up the case where the type values are in the same order. */
1422 tree tv1 = TYPE_VALUES (t1);
1423 tree tv2 = TYPE_VALUES (t2);
1430 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1432 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1434 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1441 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1445 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1451 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1457 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1459 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1461 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1472 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1473 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1479 /* Speed up the common case where the fields are in the same order. */
1480 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1481 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1485 if (DECL_NAME (s1) != DECL_NAME (s2))
1487 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1488 enum_and_int_p, different_types_p);
1490 if (result != 1 && !DECL_NAME (s1))
1498 needs_warning = true;
1500 if (TREE_CODE (s1) == FIELD_DECL
1501 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1502 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1510 tu->val = needs_warning ? 2 : 1;
1514 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1518 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1519 if (DECL_NAME (s1) == DECL_NAME (s2))
1523 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1527 if (result != 1 && !DECL_NAME (s1))
1535 needs_warning = true;
1537 if (TREE_CODE (s1) == FIELD_DECL
1538 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1539 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1551 tu->val = needs_warning ? 2 : 10;
1557 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1559 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1561 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1564 if (TREE_CODE (s1) != TREE_CODE (s2)
1565 || DECL_NAME (s1) != DECL_NAME (s2))
1567 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1568 enum_and_int_p, different_types_p);
1572 needs_warning = true;
1574 if (TREE_CODE (s1) == FIELD_DECL
1575 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1576 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1582 tu->val = needs_warning ? 2 : 1;
1591 /* Return 1 if two function types F1 and F2 are compatible.
1592 If either type specifies no argument types,
1593 the other must specify a fixed number of self-promoting arg types.
1594 Otherwise, if one type specifies only the number of arguments,
1595 the other must specify that number of self-promoting arg types.
1596 Otherwise, the argument types must match.
1597 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1600 function_types_compatible_p (const_tree f1, const_tree f2,
1601 bool *enum_and_int_p, bool *different_types_p)
1604 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1609 ret1 = TREE_TYPE (f1);
1610 ret2 = TREE_TYPE (f2);
1612 /* 'volatile' qualifiers on a function's return type used to mean
1613 the function is noreturn. */
1614 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1615 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1616 if (TYPE_VOLATILE (ret1))
1617 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1618 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1619 if (TYPE_VOLATILE (ret2))
1620 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1621 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1622 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1626 args1 = TYPE_ARG_TYPES (f1);
1627 args2 = TYPE_ARG_TYPES (f2);
1629 if (different_types_p != NULL
1630 && (args1 == 0) != (args2 == 0))
1631 *different_types_p = true;
1633 /* An unspecified parmlist matches any specified parmlist
1634 whose argument types don't need default promotions. */
1638 if (!self_promoting_args_p (args2))
1640 /* If one of these types comes from a non-prototype fn definition,
1641 compare that with the other type's arglist.
1642 If they don't match, ask for a warning (but no error). */
1643 if (TYPE_ACTUAL_ARG_TYPES (f1)
1644 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1645 enum_and_int_p, different_types_p))
1651 if (!self_promoting_args_p (args1))
1653 if (TYPE_ACTUAL_ARG_TYPES (f2)
1654 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1655 enum_and_int_p, different_types_p))
1660 /* Both types have argument lists: compare them and propagate results. */
1661 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1663 return val1 != 1 ? val1 : val;
1666 /* Check two lists of types for compatibility, returning 0 for
1667 incompatible, 1 for compatible, or 2 for compatible with
1668 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1669 comptypes_internal. */
1672 type_lists_compatible_p (const_tree args1, const_tree args2,
1673 bool *enum_and_int_p, bool *different_types_p)
1675 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1681 tree a1, mv1, a2, mv2;
1682 if (args1 == 0 && args2 == 0)
1684 /* If one list is shorter than the other,
1685 they fail to match. */
1686 if (args1 == 0 || args2 == 0)
1688 mv1 = a1 = TREE_VALUE (args1);
1689 mv2 = a2 = TREE_VALUE (args2);
1690 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1691 mv1 = (TYPE_ATOMIC (mv1)
1692 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1694 : TYPE_MAIN_VARIANT (mv1));
1695 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1696 mv2 = (TYPE_ATOMIC (mv2)
1697 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1699 : TYPE_MAIN_VARIANT (mv2));
1700 /* A null pointer instead of a type
1701 means there is supposed to be an argument
1702 but nothing is specified about what type it has.
1703 So match anything that self-promotes. */
1704 if (different_types_p != NULL
1705 && (a1 == 0) != (a2 == 0))
1706 *different_types_p = true;
1709 if (c_type_promotes_to (a2) != a2)
1714 if (c_type_promotes_to (a1) != a1)
1717 /* If one of the lists has an error marker, ignore this arg. */
1718 else if (TREE_CODE (a1) == ERROR_MARK
1719 || TREE_CODE (a2) == ERROR_MARK)
1721 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1722 different_types_p)))
1724 if (different_types_p != NULL)
1725 *different_types_p = true;
1726 /* Allow wait (union {union wait *u; int *i} *)
1727 and wait (union wait *) to be compatible. */
1728 if (TREE_CODE (a1) == UNION_TYPE
1729 && (TYPE_NAME (a1) == 0
1730 || TYPE_TRANSPARENT_AGGR (a1))
1731 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1732 && tree_int_cst_equal (TYPE_SIZE (a1),
1736 for (memb = TYPE_FIELDS (a1);
1737 memb; memb = DECL_CHAIN (memb))
1739 tree mv3 = TREE_TYPE (memb);
1740 if (mv3 && mv3 != error_mark_node
1741 && TREE_CODE (mv3) != ARRAY_TYPE)
1742 mv3 = (TYPE_ATOMIC (mv3)
1743 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1745 : TYPE_MAIN_VARIANT (mv3));
1746 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1753 else if (TREE_CODE (a2) == UNION_TYPE
1754 && (TYPE_NAME (a2) == 0
1755 || TYPE_TRANSPARENT_AGGR (a2))
1756 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1757 && tree_int_cst_equal (TYPE_SIZE (a2),
1761 for (memb = TYPE_FIELDS (a2);
1762 memb; memb = DECL_CHAIN (memb))
1764 tree mv3 = TREE_TYPE (memb);
1765 if (mv3 && mv3 != error_mark_node
1766 && TREE_CODE (mv3) != ARRAY_TYPE)
1767 mv3 = (TYPE_ATOMIC (mv3)
1768 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1770 : TYPE_MAIN_VARIANT (mv3));
1771 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1782 /* comptypes said ok, but record if it said to warn. */
1786 args1 = TREE_CHAIN (args1);
1787 args2 = TREE_CHAIN (args2);
1791 /* Compute the size to increment a pointer by. When a function type or void
1792 type or incomplete type is passed, size_one_node is returned.
1793 This function does not emit any diagnostics; the caller is responsible
1797 c_size_in_bytes (const_tree type)
1799 enum tree_code code = TREE_CODE (type);
1801 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1802 || !COMPLETE_TYPE_P (type))
1803 return size_one_node;
1805 /* Convert in case a char is more than one unit. */
1806 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1807 size_int (TYPE_PRECISION (char_type_node)
1811 /* Return either DECL or its known constant value (if it has one). */
1814 decl_constant_value (tree decl)
1816 if (/* Don't change a variable array bound or initial value to a constant
1817 in a place where a variable is invalid. Note that DECL_INITIAL
1818 isn't valid for a PARM_DECL. */
1819 current_function_decl != 0
1820 && TREE_CODE (decl) != PARM_DECL
1821 && !TREE_THIS_VOLATILE (decl)
1822 && TREE_READONLY (decl)
1823 && DECL_INITIAL (decl) != 0
1824 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1825 /* This is invalid if initial value is not constant.
1826 If it has either a function call, a memory reference,
1827 or a variable, then re-evaluating it could give different results. */
1828 && TREE_CONSTANT (DECL_INITIAL (decl))
1829 /* Check for cases where this is sub-optimal, even though valid. */
1830 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1831 return DECL_INITIAL (decl);
1835 /* Convert the array expression EXP to a pointer. */
1837 array_to_pointer_conversion (location_t loc, tree exp)
1839 tree orig_exp = exp;
1840 tree type = TREE_TYPE (exp);
1842 tree restype = TREE_TYPE (type);
1845 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1847 STRIP_TYPE_NOPS (exp);
1849 if (TREE_NO_WARNING (orig_exp))
1850 TREE_NO_WARNING (exp) = 1;
1852 ptrtype = build_pointer_type (restype);
1854 if (TREE_CODE (exp) == INDIRECT_REF)
1855 return convert (ptrtype, TREE_OPERAND (exp, 0));
1857 /* In C++ array compound literals are temporary objects unless they are
1858 const or appear in namespace scope, so they are destroyed too soon
1859 to use them for much of anything (c++/53220). */
1860 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1862 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1863 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1864 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1865 "converting an array compound literal to a pointer "
1866 "is ill-formed in C++");
1869 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1870 return convert (ptrtype, adr);
1873 /* Convert the function expression EXP to a pointer. */
1875 function_to_pointer_conversion (location_t loc, tree exp)
1877 tree orig_exp = exp;
1879 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1881 STRIP_TYPE_NOPS (exp);
1883 if (TREE_NO_WARNING (orig_exp))
1884 TREE_NO_WARNING (exp) = 1;
1886 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1889 /* Mark EXP as read, not just set, for set but not used -Wunused
1890 warning purposes. */
1893 mark_exp_read (tree exp)
1895 switch (TREE_CODE (exp))
1899 DECL_READ_P (exp) = 1;
1908 mark_exp_read (TREE_OPERAND (exp, 0));
1911 case C_MAYBE_CONST_EXPR:
1912 mark_exp_read (TREE_OPERAND (exp, 1));
1919 /* Perform the default conversion of arrays and functions to pointers.
1920 Return the result of converting EXP. For any other expression, just
1923 LOC is the location of the expression. */
1926 default_function_array_conversion (location_t loc, struct c_expr exp)
1928 tree orig_exp = exp.value;
1929 tree type = TREE_TYPE (exp.value);
1930 enum tree_code code = TREE_CODE (type);
1936 bool not_lvalue = false;
1937 bool lvalue_array_p;
1939 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1940 || CONVERT_EXPR_P (exp.value))
1941 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1943 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1945 exp.value = TREE_OPERAND (exp.value, 0);
1948 if (TREE_NO_WARNING (orig_exp))
1949 TREE_NO_WARNING (exp.value) = 1;
1951 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1952 if (!flag_isoc99 && !lvalue_array_p)
1954 /* Before C99, non-lvalue arrays do not decay to pointers.
1955 Normally, using such an array would be invalid; but it can
1956 be used correctly inside sizeof or as a statement expression.
1957 Thus, do not give an error here; an error will result later. */
1961 exp.value = array_to_pointer_conversion (loc, exp.value);
1965 exp.value = function_to_pointer_conversion (loc, exp.value);
1975 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1977 mark_exp_read (exp.value);
1978 return default_function_array_conversion (loc, exp);
1981 /* Return whether EXPR should be treated as an atomic lvalue for the
1982 purposes of load and store handling. */
1985 really_atomic_lvalue (tree expr)
1987 if (error_operand_p (expr))
1989 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1991 if (!lvalue_p (expr))
1994 /* Ignore _Atomic on register variables, since their addresses can't
1995 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1996 sequences wouldn't work. Ignore _Atomic on structures containing
1997 bit-fields, since accessing elements of atomic structures or
1998 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1999 it's undefined at translation time or execution time, and the
2000 normal atomic sequences again wouldn't work. */
2001 while (handled_component_p (expr))
2003 if (TREE_CODE (expr) == COMPONENT_REF
2004 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2006 expr = TREE_OPERAND (expr, 0);
2008 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2013 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2014 including converting functions and arrays to pointers if CONVERT_P.
2015 If READ_P, also mark the expression as having been read. */
2018 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2019 bool convert_p, bool read_p)
2022 mark_exp_read (exp.value);
2024 exp = default_function_array_conversion (loc, exp);
2025 if (really_atomic_lvalue (exp.value))
2027 vec<tree, va_gc> *params;
2028 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2029 tree expr_type = TREE_TYPE (exp.value);
2030 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2031 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2033 gcc_assert (TYPE_ATOMIC (expr_type));
2035 /* Expansion of a generic atomic load may require an addition
2036 element, so allocate enough to prevent a resize. */
2037 vec_alloc (params, 4);
2039 /* Remove the qualifiers for the rest of the expressions and
2040 create the VAL temp variable to hold the RHS. */
2041 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2042 tmp = create_tmp_var (nonatomic_type);
2043 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2044 TREE_ADDRESSABLE (tmp) = 1;
2045 TREE_NO_WARNING (tmp) = 1;
2047 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2048 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2049 params->quick_push (expr_addr);
2050 params->quick_push (tmp_addr);
2051 params->quick_push (seq_cst);
2052 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2054 /* EXPR is always read. */
2055 mark_exp_read (exp.value);
2057 /* Return tmp which contains the value loaded. */
2058 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2063 /* EXP is an expression of integer type. Apply the integer promotions
2064 to it and return the promoted value. */
2067 perform_integral_promotions (tree exp)
2069 tree type = TREE_TYPE (exp);
2070 enum tree_code code = TREE_CODE (type);
2072 gcc_assert (INTEGRAL_TYPE_P (type));
2074 /* Normally convert enums to int,
2075 but convert wide enums to something wider. */
2076 if (code == ENUMERAL_TYPE)
2078 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2079 TYPE_PRECISION (integer_type_node)),
2080 ((TYPE_PRECISION (type)
2081 >= TYPE_PRECISION (integer_type_node))
2082 && TYPE_UNSIGNED (type)));
2084 return convert (type, exp);
2087 /* ??? This should no longer be needed now bit-fields have their
2089 if (TREE_CODE (exp) == COMPONENT_REF
2090 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2091 /* If it's thinner than an int, promote it like a
2092 c_promoting_integer_type_p, otherwise leave it alone. */
2093 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2094 TYPE_PRECISION (integer_type_node)))
2095 return convert (integer_type_node, exp);
2097 if (c_promoting_integer_type_p (type))
2099 /* Preserve unsignedness if not really getting any wider. */
2100 if (TYPE_UNSIGNED (type)
2101 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2102 return convert (unsigned_type_node, exp);
2104 return convert (integer_type_node, exp);
2111 /* Perform default promotions for C data used in expressions.
2112 Enumeral types or short or char are converted to int.
2113 In addition, manifest constants symbols are replaced by their values. */
2116 default_conversion (tree exp)
2119 tree type = TREE_TYPE (exp);
2120 enum tree_code code = TREE_CODE (type);
2123 mark_exp_read (exp);
2125 /* Functions and arrays have been converted during parsing. */
2126 gcc_assert (code != FUNCTION_TYPE);
2127 if (code == ARRAY_TYPE)
2130 /* Constants can be used directly unless they're not loadable. */
2131 if (TREE_CODE (exp) == CONST_DECL)
2132 exp = DECL_INITIAL (exp);
2134 /* Strip no-op conversions. */
2136 STRIP_TYPE_NOPS (exp);
2138 if (TREE_NO_WARNING (orig_exp))
2139 TREE_NO_WARNING (exp) = 1;
2141 if (code == VOID_TYPE)
2143 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2144 "void value not ignored as it ought to be");
2145 return error_mark_node;
2148 exp = require_complete_type (exp);
2149 if (exp == error_mark_node)
2150 return error_mark_node;
2152 promoted_type = targetm.promoted_type (type);
2154 return convert (promoted_type, exp);
2156 if (INTEGRAL_TYPE_P (type))
2157 return perform_integral_promotions (exp);
2162 /* Look up COMPONENT in a structure or union TYPE.
2164 If the component name is not found, returns NULL_TREE. Otherwise,
2165 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2166 stepping down the chain to the component, which is in the last
2167 TREE_VALUE of the list. Normally the list is of length one, but if
2168 the component is embedded within (nested) anonymous structures or
2169 unions, the list steps down the chain to the component. */
2172 lookup_field (tree type, tree component)
2176 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2177 to the field elements. Use a binary search on this array to quickly
2178 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2179 will always be set for structures which have many elements. */
2181 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2184 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2186 field = TYPE_FIELDS (type);
2188 top = TYPE_LANG_SPECIFIC (type)->s->len;
2189 while (top - bot > 1)
2191 half = (top - bot + 1) >> 1;
2192 field = field_array[bot+half];
2194 if (DECL_NAME (field) == NULL_TREE)
2196 /* Step through all anon unions in linear fashion. */
2197 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2199 field = field_array[bot++];
2200 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2201 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2203 tree anon = lookup_field (TREE_TYPE (field), component);
2206 return tree_cons (NULL_TREE, field, anon);
2208 /* The Plan 9 compiler permits referring
2209 directly to an anonymous struct/union field
2210 using a typedef name. */
2211 if (flag_plan9_extensions
2212 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2213 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2215 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2221 /* Entire record is only anon unions. */
2225 /* Restart the binary search, with new lower bound. */
2229 if (DECL_NAME (field) == component)
2231 if (DECL_NAME (field) < component)
2237 if (DECL_NAME (field_array[bot]) == component)
2238 field = field_array[bot];
2239 else if (DECL_NAME (field) != component)
2244 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2246 if (DECL_NAME (field) == NULL_TREE
2247 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2248 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2250 tree anon = lookup_field (TREE_TYPE (field), component);
2253 return tree_cons (NULL_TREE, field, anon);
2255 /* The Plan 9 compiler permits referring directly to an
2256 anonymous struct/union field using a typedef
2258 if (flag_plan9_extensions
2259 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2260 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2261 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2266 if (DECL_NAME (field) == component)
2270 if (field == NULL_TREE)
2274 return tree_cons (NULL_TREE, field, NULL_TREE);
2277 /* Make an expression to refer to the COMPONENT field of structure or
2278 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2279 location of the COMPONENT_REF. */
2282 build_component_ref (location_t loc, tree datum, tree component)
2284 tree type = TREE_TYPE (datum);
2285 enum tree_code code = TREE_CODE (type);
2288 bool datum_lvalue = lvalue_p (datum);
2290 if (!objc_is_public (datum, component))
2291 return error_mark_node;
2293 /* Detect Objective-C property syntax object.property. */
2294 if (c_dialect_objc ()
2295 && (ref = objc_maybe_build_component_ref (datum, component)))
2298 /* See if there is a field or component with name COMPONENT. */
2300 if (code == RECORD_TYPE || code == UNION_TYPE)
2302 if (!COMPLETE_TYPE_P (type))
2304 c_incomplete_type_error (NULL_TREE, type);
2305 return error_mark_node;
2308 field = lookup_field (type, component);
2312 error_at (loc, "%qT has no member named %qE", type, component);
2313 return error_mark_node;
2316 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2317 This might be better solved in future the way the C++ front
2318 end does it - by giving the anonymous entities each a
2319 separate name and type, and then have build_component_ref
2320 recursively call itself. We can't do that here. */
2323 tree subdatum = TREE_VALUE (field);
2326 bool use_datum_quals;
2328 if (TREE_TYPE (subdatum) == error_mark_node)
2329 return error_mark_node;
2331 /* If this is an rvalue, it does not have qualifiers in C
2332 standard terms and we must avoid propagating such
2333 qualifiers down to a non-lvalue array that is then
2334 converted to a pointer. */
2335 use_datum_quals = (datum_lvalue
2336 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2338 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2339 if (use_datum_quals)
2340 quals |= TYPE_QUALS (TREE_TYPE (datum));
2341 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2343 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2345 SET_EXPR_LOCATION (ref, loc);
2346 if (TREE_READONLY (subdatum)
2347 || (use_datum_quals && TREE_READONLY (datum)))
2348 TREE_READONLY (ref) = 1;
2349 if (TREE_THIS_VOLATILE (subdatum)
2350 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2351 TREE_THIS_VOLATILE (ref) = 1;
2353 if (TREE_DEPRECATED (subdatum))
2354 warn_deprecated_use (subdatum, NULL_TREE);
2358 field = TREE_CHAIN (field);
2364 else if (code != ERROR_MARK)
2366 "request for member %qE in something not a structure or union",
2369 return error_mark_node;
2372 /* Given an expression PTR for a pointer, return an expression
2373 for the value pointed to.
2374 ERRORSTRING is the name of the operator to appear in error messages.
2376 LOC is the location to use for the generated tree. */
2379 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2381 tree pointer = default_conversion (ptr);
2382 tree type = TREE_TYPE (pointer);
2385 if (TREE_CODE (type) == POINTER_TYPE)
2387 if (CONVERT_EXPR_P (pointer)
2388 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2390 /* If a warning is issued, mark it to avoid duplicates from
2391 the backend. This only needs to be done at
2392 warn_strict_aliasing > 2. */
2393 if (warn_strict_aliasing > 2)
2394 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2395 type, TREE_OPERAND (pointer, 0)))
2396 TREE_NO_WARNING (pointer) = 1;
2399 if (TREE_CODE (pointer) == ADDR_EXPR
2400 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2401 == TREE_TYPE (type)))
2403 ref = TREE_OPERAND (pointer, 0);
2404 protected_set_expr_location (ref, loc);
2409 tree t = TREE_TYPE (type);
2411 ref = build1 (INDIRECT_REF, t, pointer);
2413 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2415 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2417 error_at (loc, "dereferencing pointer to incomplete type "
2419 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2421 return error_mark_node;
2423 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2424 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2426 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2427 so that we get the proper error message if the result is used
2428 to assign to. Also, &* is supposed to be a no-op.
2429 And ANSI C seems to specify that the type of the result
2430 should be the const type. */
2431 /* A de-reference of a pointer to const is not a const. It is valid
2432 to change it via some other pointer. */
2433 TREE_READONLY (ref) = TYPE_READONLY (t);
2434 TREE_SIDE_EFFECTS (ref)
2435 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2436 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2437 protected_set_expr_location (ref, loc);
2441 else if (TREE_CODE (pointer) != ERROR_MARK)
2442 invalid_indirection_error (loc, type, errstring);
2444 return error_mark_node;
2447 /* This handles expressions of the form "a[i]", which denotes
2450 This is logically equivalent in C to *(a+i), but we may do it differently.
2451 If A is a variable or a member, we generate a primitive ARRAY_REF.
2452 This avoids forcing the array out of registers, and can work on
2453 arrays that are not lvalues (for example, members of structures returned
2456 For vector types, allow vector[i] but not i[vector], and create
2457 *(((type*)&vectortype) + i) for the expression.
2459 LOC is the location to use for the returned expression. */
2462 build_array_ref (location_t loc, tree array, tree index)
2465 bool swapped = false;
2466 if (TREE_TYPE (array) == error_mark_node
2467 || TREE_TYPE (index) == error_mark_node)
2468 return error_mark_node;
2470 if (flag_cilkplus && contains_array_notation_expr (index))
2473 if (!find_rank (loc, index, index, true, &rank))
2474 return error_mark_node;
2477 error_at (loc, "rank of the array's index is greater than 1");
2478 return error_mark_node;
2481 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2482 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2483 /* Allow vector[index] but not index[vector]. */
2484 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2487 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2488 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2491 "subscripted value is neither array nor pointer nor vector");
2493 return error_mark_node;
2501 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2503 error_at (loc, "array subscript is not an integer");
2504 return error_mark_node;
2507 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2509 error_at (loc, "subscripted value is pointer to function");
2510 return error_mark_node;
2513 /* ??? Existing practice has been to warn only when the char
2514 index is syntactically the index, not for char[array]. */
2516 warn_array_subscript_with_type_char (loc, index);
2518 /* Apply default promotions *after* noticing character types. */
2519 index = default_conversion (index);
2520 if (index == error_mark_node)
2521 return error_mark_node;
2523 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2526 = convert_vector_to_pointer_for_subscript (loc, &array, index);
2528 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2532 /* An array that is indexed by a non-constant
2533 cannot be stored in a register; we must be able to do
2534 address arithmetic on its address.
2535 Likewise an array of elements of variable size. */
2536 if (TREE_CODE (index) != INTEGER_CST
2537 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2538 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2540 if (!c_mark_addressable (array))
2541 return error_mark_node;
2543 /* An array that is indexed by a constant value which is not within
2544 the array bounds cannot be stored in a register either; because we
2545 would get a crash in store_bit_field/extract_bit_field when trying
2546 to access a non-existent part of the register. */
2547 if (TREE_CODE (index) == INTEGER_CST
2548 && TYPE_DOMAIN (TREE_TYPE (array))
2549 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2551 if (!c_mark_addressable (array))
2552 return error_mark_node;
2555 if (pedantic || warn_c90_c99_compat)
2558 while (TREE_CODE (foo) == COMPONENT_REF)
2559 foo = TREE_OPERAND (foo, 0);
2560 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2561 pedwarn (loc, OPT_Wpedantic,
2562 "ISO C forbids subscripting %<register%> array");
2563 else if (!lvalue_p (foo))
2564 pedwarn_c90 (loc, OPT_Wpedantic,
2565 "ISO C90 forbids subscripting non-lvalue "
2569 type = TREE_TYPE (TREE_TYPE (array));
2570 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2571 /* Array ref is const/volatile if the array elements are
2572 or if the array is. */
2573 TREE_READONLY (rval)
2574 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2575 | TREE_READONLY (array));
2576 TREE_SIDE_EFFECTS (rval)
2577 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2578 | TREE_SIDE_EFFECTS (array));
2579 TREE_THIS_VOLATILE (rval)
2580 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2581 /* This was added by rms on 16 Nov 91.
2582 It fixes vol struct foo *a; a->elts[1]
2583 in an inline function.
2584 Hope it doesn't break something else. */
2585 | TREE_THIS_VOLATILE (array));
2586 ret = require_complete_type (rval);
2587 protected_set_expr_location (ret, loc);
2589 ret = non_lvalue_loc (loc, ret);
2594 tree ar = default_conversion (array);
2596 if (ar == error_mark_node)
2599 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2600 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2602 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2606 ret = non_lvalue_loc (loc, ret);
2611 /* Build an external reference to identifier ID. FUN indicates
2612 whether this will be used for a function call. LOC is the source
2613 location of the identifier. This sets *TYPE to the type of the
2614 identifier, which is not the same as the type of the returned value
2615 for CONST_DECLs defined as enum constants. If the type of the
2616 identifier is not available, *TYPE is set to NULL. */
2618 build_external_ref (location_t loc, tree id, int fun, tree *type)
2621 tree decl = lookup_name (id);
2623 /* In Objective-C, an instance variable (ivar) may be preferred to
2624 whatever lookup_name() found. */
2625 decl = objc_lookup_ivar (decl, id);
2628 if (decl && decl != error_mark_node)
2631 *type = TREE_TYPE (ref);
2634 /* Implicit function declaration. */
2635 ref = implicitly_declare (loc, id);
2636 else if (decl == error_mark_node)
2637 /* Don't complain about something that's already been
2638 complained about. */
2639 return error_mark_node;
2642 undeclared_variable (loc, id);
2643 return error_mark_node;
2646 if (TREE_TYPE (ref) == error_mark_node)
2647 return error_mark_node;
2649 if (TREE_DEPRECATED (ref))
2650 warn_deprecated_use (ref, NULL_TREE);
2652 /* Recursive call does not count as usage. */
2653 if (ref != current_function_decl)
2655 TREE_USED (ref) = 1;
2658 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2660 if (!in_sizeof && !in_typeof)
2661 C_DECL_USED (ref) = 1;
2662 else if (DECL_INITIAL (ref) == 0
2663 && DECL_EXTERNAL (ref)
2664 && !TREE_PUBLIC (ref))
2665 record_maybe_used_decl (ref);
2668 if (TREE_CODE (ref) == CONST_DECL)
2670 used_types_insert (TREE_TYPE (ref));
2673 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2674 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2676 warning_at (loc, OPT_Wc___compat,
2677 ("enum constant defined in struct or union "
2678 "is not visible in C++"));
2679 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2682 ref = DECL_INITIAL (ref);
2683 TREE_CONSTANT (ref) = 1;
2685 else if (current_function_decl != 0
2686 && !DECL_FILE_SCOPE_P (current_function_decl)
2687 && (TREE_CODE (ref) == VAR_DECL
2688 || TREE_CODE (ref) == PARM_DECL
2689 || TREE_CODE (ref) == FUNCTION_DECL))
2691 tree context = decl_function_context (ref);
2693 if (context != 0 && context != current_function_decl)
2694 DECL_NONLOCAL (ref) = 1;
2696 /* C99 6.7.4p3: An inline definition of a function with external
2697 linkage ... shall not contain a reference to an identifier with
2698 internal linkage. */
2699 else if (current_function_decl != 0
2700 && DECL_DECLARED_INLINE_P (current_function_decl)
2701 && DECL_EXTERNAL (current_function_decl)
2702 && VAR_OR_FUNCTION_DECL_P (ref)
2703 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2704 && ! TREE_PUBLIC (ref)
2705 && DECL_CONTEXT (ref) != current_function_decl)
2706 record_inline_static (loc, current_function_decl, ref,
2712 /* Record details of decls possibly used inside sizeof or typeof. */
2713 struct maybe_used_decl
2717 /* The level seen at (in_sizeof + in_typeof). */
2719 /* The next one at this level or above, or NULL. */
2720 struct maybe_used_decl *next;
2723 static struct maybe_used_decl *maybe_used_decls;
2725 /* Record that DECL, an undefined static function reference seen
2726 inside sizeof or typeof, might be used if the operand of sizeof is
2727 a VLA type or the operand of typeof is a variably modified
2731 record_maybe_used_decl (tree decl)
2733 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2735 t->level = in_sizeof + in_typeof;
2736 t->next = maybe_used_decls;
2737 maybe_used_decls = t;
2740 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2741 USED is false, just discard them. If it is true, mark them used
2742 (if no longer inside sizeof or typeof) or move them to the next
2743 level up (if still inside sizeof or typeof). */
2746 pop_maybe_used (bool used)
2748 struct maybe_used_decl *p = maybe_used_decls;
2749 int cur_level = in_sizeof + in_typeof;
2750 while (p && p->level > cur_level)
2755 C_DECL_USED (p->decl) = 1;
2757 p->level = cur_level;
2761 if (!used || cur_level == 0)
2762 maybe_used_decls = p;
2765 /* Return the result of sizeof applied to EXPR. */
2768 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2771 if (expr.value == error_mark_node)
2773 ret.value = error_mark_node;
2774 ret.original_code = ERROR_MARK;
2775 ret.original_type = NULL;
2776 pop_maybe_used (false);
2780 bool expr_const_operands = true;
2782 if (TREE_CODE (expr.value) == PARM_DECL
2783 && C_ARRAY_PARAMETER (expr.value))
2785 if (warning_at (loc, OPT_Wsizeof_array_argument,
2786 "%<sizeof%> on array function parameter %qE will "
2787 "return size of %qT", expr.value,
2788 expr.original_type))
2789 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2791 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2792 &expr_const_operands);
2793 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2794 c_last_sizeof_arg = expr.value;
2795 ret.original_code = SIZEOF_EXPR;
2796 ret.original_type = NULL;
2797 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2799 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2800 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2801 folded_expr, ret.value);
2802 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2803 SET_EXPR_LOCATION (ret.value, loc);
2805 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2810 /* Return the result of sizeof applied to T, a structure for the type
2811 name passed to sizeof (rather than the type itself). LOC is the
2812 location of the original expression. */
2815 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2819 tree type_expr = NULL_TREE;
2820 bool type_expr_const = true;
2821 type = groktypename (t, &type_expr, &type_expr_const);
2822 ret.value = c_sizeof (loc, type);
2823 c_last_sizeof_arg = type;
2824 ret.original_code = SIZEOF_EXPR;
2825 ret.original_type = NULL;
2826 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2827 && c_vla_type_p (type))
2829 /* If the type is a [*] array, it is a VLA but is represented as
2830 having a size of zero. In such a case we must ensure that
2831 the result of sizeof does not get folded to a constant by
2832 c_fully_fold, because if the size is evaluated the result is
2833 not constant and so constraints on zero or negative size
2834 arrays must not be applied when this sizeof call is inside
2835 another array declarator. */
2837 type_expr = integer_zero_node;
2838 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2839 type_expr, ret.value);
2840 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2842 pop_maybe_used (type != error_mark_node
2843 ? C_TYPE_VARIABLE_SIZE (type) : false);
2847 /* Build a function call to function FUNCTION with parameters PARAMS.
2848 The function call is at LOC.
2849 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2850 TREE_VALUE of each node is a parameter-expression.
2851 FUNCTION's data type may be a function type or a pointer-to-function. */
2854 build_function_call (location_t loc, tree function, tree params)
2856 vec<tree, va_gc> *v;
2859 vec_alloc (v, list_length (params));
2860 for (; params; params = TREE_CHAIN (params))
2861 v->quick_push (TREE_VALUE (params));
2862 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2867 /* Give a note about the location of the declaration of DECL. */
2869 static void inform_declaration (tree decl)
2871 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2872 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2875 /* Build a function call to function FUNCTION with parameters PARAMS.
2876 ORIGTYPES, if not NULL, is a vector of types; each element is
2877 either NULL or the original type of the corresponding element in
2878 PARAMS. The original type may differ from TREE_TYPE of the
2879 parameter for enums. FUNCTION's data type may be a function type
2880 or pointer-to-function. This function changes the elements of
2884 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2885 tree function, vec<tree, va_gc> *params,
2886 vec<tree, va_gc> *origtypes)
2888 tree fntype, fundecl = 0;
2889 tree name = NULL_TREE, result;
2895 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2896 STRIP_TYPE_NOPS (function);
2898 /* Convert anything with function type to a pointer-to-function. */
2899 if (TREE_CODE (function) == FUNCTION_DECL)
2901 name = DECL_NAME (function);
2904 tm_malloc_replacement (function);
2906 /* Atomic functions have type checking/casting already done. They are
2907 often rewritten and don't match the original parameter list. */
2908 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2912 && is_cilkplus_reduce_builtin (function))
2915 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2916 function = function_to_pointer_conversion (loc, function);
2918 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2919 expressions, like those used for ObjC messenger dispatches. */
2920 if (params && !params->is_empty ())
2921 function = objc_rewrite_function_call (function, (*params)[0]);
2923 function = c_fully_fold (function, false, NULL);
2925 fntype = TREE_TYPE (function);
2927 if (TREE_CODE (fntype) == ERROR_MARK)
2928 return error_mark_node;
2930 if (!(TREE_CODE (fntype) == POINTER_TYPE
2931 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2933 if (!flag_diagnostics_show_caret)
2935 "called object %qE is not a function or function pointer",
2937 else if (DECL_P (function))
2940 "called object %qD is not a function or function pointer",
2942 inform_declaration (function);
2946 "called object is not a function or function pointer");
2947 return error_mark_node;
2950 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2951 current_function_returns_abnormally = 1;
2953 /* fntype now gets the type of function pointed to. */
2954 fntype = TREE_TYPE (fntype);
2956 /* Convert the parameters to the types declared in the
2957 function prototype, or apply default promotions. */
2959 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2960 origtypes, function, fundecl);
2962 return error_mark_node;
2964 /* Check that the function is called through a compatible prototype.
2965 If it is not, warn. */
2966 if (CONVERT_EXPR_P (function)
2967 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2968 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2969 && !comptypes (fntype, TREE_TYPE (tem)))
2971 tree return_type = TREE_TYPE (fntype);
2973 /* This situation leads to run-time undefined behavior. We can't,
2974 therefore, simply error unless we can prove that all possible
2975 executions of the program must execute the code. */
2976 warning_at (loc, 0, "function called through a non-compatible type");
2978 if (VOID_TYPE_P (return_type)
2979 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2981 "function with qualified void return type called");
2984 argarray = vec_safe_address (params);
2986 /* Check that arguments to builtin functions match the expectations. */
2988 && DECL_BUILT_IN (fundecl)
2989 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2990 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2991 return error_mark_node;
2993 /* Check that the arguments to the function are valid. */
2994 check_function_arguments (fntype, nargs, argarray);
2996 if (name != NULL_TREE
2997 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2999 if (require_constant_value)
3001 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3002 function, nargs, argarray);
3004 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3005 function, nargs, argarray);
3006 if (TREE_CODE (result) == NOP_EXPR
3007 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3008 STRIP_TYPE_NOPS (result);
3011 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3012 function, nargs, argarray);
3014 if (VOID_TYPE_P (TREE_TYPE (result)))
3016 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3018 "function with qualified void return type called");
3021 return require_complete_type (result);
3024 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3027 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3028 tree function, vec<tree, va_gc> *params,
3029 vec<tree, va_gc> *origtypes)
3031 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3032 STRIP_TYPE_NOPS (function);
3034 /* Convert anything with function type to a pointer-to-function. */
3035 if (TREE_CODE (function) == FUNCTION_DECL)
3037 /* Implement type-directed function overloading for builtins.
3038 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3039 handle all the type checking. The result is a complete expression
3040 that implements this function call. */
3041 tree tem = resolve_overloaded_builtin (loc, function, params);
3045 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3048 /* Convert the argument expressions in the vector VALUES
3049 to the types in the list TYPELIST.
3051 If TYPELIST is exhausted, or when an element has NULL as its type,
3052 perform the default conversions.
3054 ORIGTYPES is the original types of the expressions in VALUES. This
3055 holds the type of enum values which have been converted to integral
3056 types. It may be NULL.
3058 FUNCTION is a tree for the called function. It is used only for
3059 error messages, where it is formatted with %qE.
3061 This is also where warnings about wrong number of args are generated.
3063 ARG_LOC are locations of function arguments (if any).
3065 Returns the actual number of arguments processed (which may be less
3066 than the length of VALUES in some error situations), or -1 on
3070 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3071 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3072 tree function, tree fundecl)
3075 unsigned int parmnum;
3076 bool error_args = false;
3077 const bool type_generic = fundecl
3078 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3079 bool type_generic_remove_excess_precision = false;
3082 /* Change pointer to function to the function itself for
3084 if (TREE_CODE (function) == ADDR_EXPR
3085 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3086 function = TREE_OPERAND (function, 0);
3088 /* Handle an ObjC selector specially for diagnostics. */
3089 selector = objc_message_selector ();
3091 /* For type-generic built-in functions, determine whether excess
3092 precision should be removed (classification) or not
3095 && DECL_BUILT_IN (fundecl)
3096 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3098 switch (DECL_FUNCTION_CODE (fundecl))
3100 case BUILT_IN_ISFINITE:
3101 case BUILT_IN_ISINF:
3102 case BUILT_IN_ISINF_SIGN:
3103 case BUILT_IN_ISNAN:
3104 case BUILT_IN_ISNORMAL:
3105 case BUILT_IN_FPCLASSIFY:
3106 type_generic_remove_excess_precision = true;
3110 type_generic_remove_excess_precision = false;
3114 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3115 return vec_safe_length (values);
3117 /* Scan the given expressions and types, producing individual
3118 converted arguments. */
3120 for (typetail = typelist, parmnum = 0;
3121 values && values->iterate (parmnum, &val);
3124 tree type = typetail ? TREE_VALUE (typetail) : 0;
3125 tree valtype = TREE_TYPE (val);
3126 tree rname = function;
3127 int argnum = parmnum + 1;
3128 const char *invalid_func_diag;
3129 bool excess_precision = false;
3132 /* Some __atomic_* builtins have additional hidden argument at
3135 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3136 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3139 if (type == void_type_node)
3142 error_at (loc, "too many arguments to method %qE", selector);
3144 error_at (loc, "too many arguments to function %qE", function);
3145 inform_declaration (fundecl);
3146 return error_args ? -1 : (int) parmnum;
3149 if (selector && argnum > 2)
3155 npc = null_pointer_constant_p (val);
3157 /* If there is excess precision and a prototype, convert once to
3158 the required type rather than converting via the semantic
3159 type. Likewise without a prototype a float value represented
3160 as long double should be converted once to double. But for
3161 type-generic classification functions excess precision must
3163 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3164 && (type || !type_generic || !type_generic_remove_excess_precision))
3166 val = TREE_OPERAND (val, 0);
3167 excess_precision = true;
3169 val = c_fully_fold (val, false, NULL);
3170 STRIP_TYPE_NOPS (val);
3172 val = require_complete_type (val);
3176 /* Formal parm type is specified by a function prototype. */
3178 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3180 error_at (ploc, "type of formal parameter %d is incomplete",
3188 /* Optionally warn about conversions that
3189 differ from the default conversions. */
3190 if (warn_traditional_conversion || warn_traditional)
3192 unsigned int formal_prec = TYPE_PRECISION (type);
3194 if (INTEGRAL_TYPE_P (type)
3195 && TREE_CODE (valtype) == REAL_TYPE)
3196 warning_at (ploc, OPT_Wtraditional_conversion,
3197 "passing argument %d of %qE as integer rather "
3198 "than floating due to prototype",
3200 if (INTEGRAL_TYPE_P (type)
3201 && TREE_CODE (valtype) == COMPLEX_TYPE)
3202 warning_at (ploc, OPT_Wtraditional_conversion,
3203 "passing argument %d of %qE as integer rather "
3204 "than complex due to prototype",
3206 else if (TREE_CODE (type) == COMPLEX_TYPE
3207 && TREE_CODE (valtype) == REAL_TYPE)
3208 warning_at (ploc, OPT_Wtraditional_conversion,
3209 "passing argument %d of %qE as complex rather "
3210 "than floating due to prototype",
3212 else if (TREE_CODE (type) == REAL_TYPE
3213 && INTEGRAL_TYPE_P (valtype))
3214 warning_at (ploc, OPT_Wtraditional_conversion,
3215 "passing argument %d of %qE as floating rather "
3216 "than integer due to prototype",
3218 else if (TREE_CODE (type) == COMPLEX_TYPE
3219 && INTEGRAL_TYPE_P (valtype))
3220 warning_at (ploc, OPT_Wtraditional_conversion,
3221 "passing argument %d of %qE as complex rather "
3222 "than integer due to prototype",
3224 else if (TREE_CODE (type) == REAL_TYPE
3225 && TREE_CODE (valtype) == COMPLEX_TYPE)
3226 warning_at (ploc, OPT_Wtraditional_conversion,
3227 "passing argument %d of %qE as floating rather "
3228 "than complex due to prototype",
3230 /* ??? At some point, messages should be written about
3231 conversions between complex types, but that's too messy
3233 else if (TREE_CODE (type) == REAL_TYPE
3234 && TREE_CODE (valtype) == REAL_TYPE)
3236 /* Warn if any argument is passed as `float',
3237 since without a prototype it would be `double'. */
3238 if (formal_prec == TYPE_PRECISION (float_type_node)
3239 && type != dfloat32_type_node)
3240 warning_at (ploc, 0,
3241 "passing argument %d of %qE as %<float%> "
3242 "rather than %<double%> due to prototype",
3245 /* Warn if mismatch between argument and prototype
3246 for decimal float types. Warn of conversions with
3247 binary float types and of precision narrowing due to
3249 else if (type != valtype
3250 && (type == dfloat32_type_node
3251 || type == dfloat64_type_node
3252 || type == dfloat128_type_node
3253 || valtype == dfloat32_type_node
3254 || valtype == dfloat64_type_node
3255 || valtype == dfloat128_type_node)
3257 <= TYPE_PRECISION (valtype)
3258 || (type == dfloat128_type_node
3260 != dfloat64_type_node
3262 != dfloat32_type_node)))
3263 || (type == dfloat64_type_node
3265 != dfloat32_type_node))))
3266 warning_at (ploc, 0,
3267 "passing argument %d of %qE as %qT "
3268 "rather than %qT due to prototype",
3269 argnum, rname, type, valtype);
3272 /* Detect integer changing in width or signedness.
3273 These warnings are only activated with
3274 -Wtraditional-conversion, not with -Wtraditional. */
3275 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3276 && INTEGRAL_TYPE_P (valtype))
3278 tree would_have_been = default_conversion (val);
3279 tree type1 = TREE_TYPE (would_have_been);
3281 if (TREE_CODE (type) == ENUMERAL_TYPE
3282 && (TYPE_MAIN_VARIANT (type)
3283 == TYPE_MAIN_VARIANT (valtype)))
3284 /* No warning if function asks for enum
3285 and the actual arg is that enum type. */
3287 else if (formal_prec != TYPE_PRECISION (type1))
3288 warning_at (ploc, OPT_Wtraditional_conversion,
3289 "passing argument %d of %qE "
3290 "with different width due to prototype",
3292 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3294 /* Don't complain if the formal parameter type
3295 is an enum, because we can't tell now whether
3296 the value was an enum--even the same enum. */
3297 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3299 else if (TREE_CODE (val) == INTEGER_CST
3300 && int_fits_type_p (val, type))
3301 /* Change in signedness doesn't matter
3302 if a constant value is unaffected. */
3304 /* If the value is extended from a narrower
3305 unsigned type, it doesn't matter whether we
3306 pass it as signed or unsigned; the value
3307 certainly is the same either way. */
3308 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3309 && TYPE_UNSIGNED (valtype))
3311 else if (TYPE_UNSIGNED (type))
3312 warning_at (ploc, OPT_Wtraditional_conversion,
3313 "passing argument %d of %qE "
3314 "as unsigned due to prototype",
3317 warning_at (ploc, OPT_Wtraditional_conversion,
3318 "passing argument %d of %qE "
3319 "as signed due to prototype",
3324 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3325 sake of better warnings from convert_and_check. */
3326 if (excess_precision)
3327 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3328 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3329 parmval = convert_for_assignment (loc, ploc, type,
3330 val, origtype, ic_argpass,
3331 npc, fundecl, function,
3334 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3335 && INTEGRAL_TYPE_P (type)
3336 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3337 parmval = default_conversion (parmval);
3340 else if (TREE_CODE (valtype) == REAL_TYPE
3341 && (TYPE_PRECISION (valtype)
3342 <= TYPE_PRECISION (double_type_node))
3343 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3344 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3345 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3351 /* Convert `float' to `double'. */
3352 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3353 warning_at (ploc, OPT_Wdouble_promotion,
3354 "implicit conversion from %qT to %qT when passing "
3355 "argument to function",
3356 valtype, double_type_node);
3357 parmval = convert (double_type_node, val);
3360 else if (excess_precision && !type_generic)
3361 /* A "double" argument with excess precision being passed
3362 without a prototype or in variable arguments. */
3363 parmval = convert (valtype, val);
3364 else if ((invalid_func_diag =
3365 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3367 error (invalid_func_diag);
3371 /* Convert `short' and `char' to full-size `int'. */
3372 parmval = default_conversion (val);
3374 (*values)[parmnum] = parmval;
3375 if (parmval == error_mark_node)
3379 typetail = TREE_CHAIN (typetail);
3382 gcc_assert (parmnum == vec_safe_length (values));
3384 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3386 error_at (loc, "too few arguments to function %qE", function);
3387 inform_declaration (fundecl);
3391 return error_args ? -1 : (int) parmnum;
3394 /* This is the entry point used by the parser to build unary operators
3395 in the input. CODE, a tree_code, specifies the unary operator, and
3396 ARG is the operand. For unary plus, the C parser currently uses
3397 CONVERT_EXPR for code.
3399 LOC is the location to use for the tree generated.
3403 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3405 struct c_expr result;
3407 result.value = build_unary_op (loc, code, arg.value, 0);
3408 result.original_code = code;
3409 result.original_type = NULL;
3411 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3412 overflow_warning (loc, result.value);
3417 /* This is the entry point used by the parser to build binary operators
3418 in the input. CODE, a tree_code, specifies the binary operator, and
3419 ARG1 and ARG2 are the operands. In addition to constructing the
3420 expression, we check for operands that were written with other binary
3421 operators in a way that is likely to confuse the user.
3423 LOCATION is the location of the binary operator. */
3426 parser_build_binary_op (location_t location, enum tree_code code,
3427 struct c_expr arg1, struct c_expr arg2)
3429 struct c_expr result;
3431 enum tree_code code1 = arg1.original_code;
3432 enum tree_code code2 = arg2.original_code;
3433 tree type1 = (arg1.original_type
3434 ? arg1.original_type
3435 : TREE_TYPE (arg1.value));
3436 tree type2 = (arg2.original_type
3437 ? arg2.original_type
3438 : TREE_TYPE (arg2.value));
3440 result.value = build_binary_op (location, code,
3441 arg1.value, arg2.value, 1);
3442 result.original_code = code;
3443 result.original_type = NULL;
3445 if (TREE_CODE (result.value) == ERROR_MARK)
3448 if (location != UNKNOWN_LOCATION)
3449 protected_set_expr_location (result.value, location);
3451 /* Check for cases such as x+y<<z which users are likely
3453 if (warn_parentheses)
3454 warn_about_parentheses (location, code, code1, arg1.value, code2,
3457 if (warn_logical_op)
3458 warn_logical_operator (location, code, TREE_TYPE (result.value),
3459 code1, arg1.value, code2, arg2.value);
3461 if (warn_logical_not_paren
3462 && TREE_CODE_CLASS (code) == tcc_comparison
3463 && code1 == TRUTH_NOT_EXPR
3464 && code2 != TRUTH_NOT_EXPR
3465 /* Avoid warning for !!x == y. */
3466 && (TREE_CODE (arg1.value) != NE_EXPR
3467 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3469 /* Avoid warning for !b == y where b has _Bool type. */
3470 tree t = integer_zero_node;
3471 if (TREE_CODE (arg1.value) == EQ_EXPR
3472 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3473 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3475 t = TREE_OPERAND (arg1.value, 0);
3478 if (TREE_TYPE (t) != integer_type_node)
3480 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3481 t = C_MAYBE_CONST_EXPR_EXPR (t);
3482 else if (CONVERT_EXPR_P (t))
3483 t = TREE_OPERAND (t, 0);
3489 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3490 warn_logical_not_parentheses (location, code, arg2.value);
3493 /* Warn about comparisons against string literals, with the exception
3494 of testing for equality or inequality of a string literal with NULL. */
3495 if (code == EQ_EXPR || code == NE_EXPR)
3497 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3498 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3499 warning_at (location, OPT_Waddress,
3500 "comparison with string literal results in unspecified behavior");
3502 else if (TREE_CODE_CLASS (code) == tcc_comparison
3503 && (code1 == STRING_CST || code2 == STRING_CST))
3504 warning_at (location, OPT_Waddress,
3505 "comparison with string literal results in unspecified behavior");
3507 if (TREE_OVERFLOW_P (result.value)
3508 && !TREE_OVERFLOW_P (arg1.value)
3509 && !TREE_OVERFLOW_P (arg2.value))
3510 overflow_warning (location, result.value);
3512 /* Warn about comparisons of different enum types. */
3513 if (warn_enum_compare
3514 && TREE_CODE_CLASS (code) == tcc_comparison
3515 && TREE_CODE (type1) == ENUMERAL_TYPE
3516 && TREE_CODE (type2) == ENUMERAL_TYPE
3517 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3518 warning_at (location, OPT_Wenum_compare,
3519 "comparison between %qT and %qT",
3525 /* Return a tree for the difference of pointers OP0 and OP1.
3526 The resulting tree has type int. */
3529 pointer_diff (location_t loc, tree op0, tree op1)
3531 tree restype = ptrdiff_type_node;
3532 tree result, inttype;
3534 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3535 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3536 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3537 tree orig_op1 = op1;
3539 /* If the operands point into different address spaces, we need to
3540 explicitly convert them to pointers into the common address space
3541 before we can subtract the numerical address values. */
3544 addr_space_t as_common;
3547 /* Determine the common superset address space. This is guaranteed
3548 to exist because the caller verified that comp_target_types
3549 returned non-zero. */
3550 if (!addr_space_superset (as0, as1, &as_common))
3553 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3554 op0 = convert (common_type, op0);
3555 op1 = convert (common_type, op1);
3558 /* Determine integer type to perform computations in. This will usually
3559 be the same as the result type (ptrdiff_t), but may need to be a wider
3560 type if pointers for the address space are wider than ptrdiff_t. */
3561 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3562 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3566 if (TREE_CODE (target_type) == VOID_TYPE)
3567 pedwarn (loc, OPT_Wpointer_arith,
3568 "pointer of type %<void *%> used in subtraction");
3569 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3570 pedwarn (loc, OPT_Wpointer_arith,
3571 "pointer to a function used in subtraction");
3573 /* First do the subtraction as integers;
3574 then drop through to build the divide operator.
3575 Do not do default conversions on the minus operator
3576 in case restype is a short type. */
3578 op0 = build_binary_op (loc,
3579 MINUS_EXPR, convert (inttype, op0),
3580 convert (inttype, op1), 0);
3581 /* This generates an error if op1 is pointer to incomplete type. */
3582 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3583 error_at (loc, "arithmetic on pointer to an incomplete type");
3585 op1 = c_size_in_bytes (target_type);
3587 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3588 error_at (loc, "arithmetic on pointer to an empty aggregate");
3590 /* Divide by the size, in easiest possible way. */
3591 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3592 op0, convert (inttype, op1));
3594 /* Convert to final result type if necessary. */
3595 return convert (restype, result);
3598 /* Expand atomic compound assignments into an approriate sequence as
3599 specified by the C11 standard section 6.5.16.2.
3605 This sequence is used for all types for which these operations are
3608 In addition, built-in versions of the 'fe' prefixed routines may
3609 need to be invoked for floating point (real, complex or vector) when
3610 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3620 __atomic_load (addr, &old, SEQ_CST);
3621 feholdexcept (&fenv);
3623 newval = old op val;
3624 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3627 feclearexcept (FE_ALL_EXCEPT);
3630 feupdateenv (&fenv);
3632 Also note that the compiler is simply issuing the generic form of
3633 the atomic operations. This requires temp(s) and has their address
3634 taken. The atomic processing is smart enough to figure out when the
3635 size of an object can utilize a lock-free version, and convert the
3636 built-in call to the appropriate lock-free routine. The optimizers
3637 will then dispose of any temps that are no longer required, and
3638 lock-free implementations are utilized as long as there is target
3639 support for the required size.
3641 If the operator is NOP_EXPR, then this is a simple assignment, and
3642 an __atomic_store is issued to perform the assignment rather than
3647 /* Build an atomic assignment at LOC, expanding into the proper
3648 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3649 the result of the operation, unless RETURN_OLD_P in which case
3650 return the old value of LHS (this is only for postincrement and
3653 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3654 tree rhs, bool return_old_p)
3656 tree fndecl, func_call;
3657 vec<tree, va_gc> *params;
3658 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3661 tree stmt, goto_stmt;
3662 tree loop_label, loop_decl, done_label, done_decl;
3664 tree lhs_type = TREE_TYPE (lhs);
3665 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3666 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3667 tree rhs_type = TREE_TYPE (rhs);
3669 gcc_assert (TYPE_ATOMIC (lhs_type));
3672 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3674 /* Allocate enough vector items for a compare_exchange. */
3675 vec_alloc (params, 6);
3677 /* Create a compound statement to hold the sequence of statements
3679 compound_stmt = c_begin_compound_stmt (false);
3681 /* Fold the RHS if it hasn't already been folded. */
3682 if (modifycode != NOP_EXPR)
3683 rhs = c_fully_fold (rhs, false, NULL);
3685 /* Remove the qualifiers for the rest of the expressions and create
3686 the VAL temp variable to hold the RHS. */
3687 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3688 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3689 val = create_tmp_var (nonatomic_rhs_type);
3690 TREE_ADDRESSABLE (val) = 1;
3691 TREE_NO_WARNING (val) = 1;
3692 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3693 SET_EXPR_LOCATION (rhs, loc);
3696 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3698 if (modifycode == NOP_EXPR)
3700 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3701 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3702 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3703 params->quick_push (lhs_addr);
3704 params->quick_push (rhs);
3705 params->quick_push (seq_cst);
3706 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3707 add_stmt (func_call);
3709 /* Finish the compound statement. */
3710 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3712 /* VAL is the value which was stored, return a COMPOUND_STMT of
3713 the statement and that value. */
3714 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3717 /* Create the variables and labels required for the op= form. */
3718 old = create_tmp_var (nonatomic_lhs_type);
3719 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3720 TREE_ADDRESSABLE (old) = 1;
3721 TREE_NO_WARNING (old) = 1;
3723 newval = create_tmp_var (nonatomic_lhs_type);
3724 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3725 TREE_ADDRESSABLE (newval) = 1;
3727 loop_decl = create_artificial_label (loc);
3728 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3730 done_decl = create_artificial_label (loc);
3731 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3733 /* __atomic_load (addr, &old, SEQ_CST). */
3734 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3735 params->quick_push (lhs_addr);
3736 params->quick_push (old_addr);
3737 params->quick_push (seq_cst);
3738 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3739 add_stmt (func_call);
3740 params->truncate (0);
3742 /* Create the expressions for floating-point environment
3743 manipulation, if required. */
3744 bool need_fenv = (flag_trapping_math
3745 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3746 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3748 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3751 add_stmt (hold_call);
3754 add_stmt (loop_label);
3756 /* newval = old + val; */
3757 rhs = build_binary_op (loc, modifycode, old, val, 1);
3758 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3759 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3761 if (rhs != error_mark_node)
3763 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3764 SET_EXPR_LOCATION (rhs, loc);
3768 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3770 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3771 params->quick_push (lhs_addr);
3772 params->quick_push (old_addr);
3773 params->quick_push (newval_addr);
3774 params->quick_push (integer_zero_node);
3775 params->quick_push (seq_cst);
3776 params->quick_push (seq_cst);
3777 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3779 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3780 SET_EXPR_LOCATION (goto_stmt, loc);
3782 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3783 SET_EXPR_LOCATION (stmt, loc);
3787 add_stmt (clear_call);
3790 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3791 SET_EXPR_LOCATION (goto_stmt, loc);
3792 add_stmt (goto_stmt);
3795 add_stmt (done_label);
3798 add_stmt (update_call);
3800 /* Finish the compound statement. */
3801 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3803 /* NEWVAL is the value that was successfully stored, return a
3804 COMPOUND_EXPR of the statement and the appropriate value. */
3805 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3806 return_old_p ? old : newval);
3809 /* Construct and perhaps optimize a tree representation
3810 for a unary operation. CODE, a tree_code, specifies the operation
3811 and XARG is the operand.
3812 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3813 the default promotions (such as from short to int).
3814 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3815 allows non-lvalues; this is only used to handle conversion of non-lvalue
3816 arrays to pointers in C99.
3818 LOCATION is the location of the operator. */
3821 build_unary_op (location_t location,
3822 enum tree_code code, tree xarg, int flag)
3824 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3827 enum tree_code typecode;
3829 tree ret = error_mark_node;
3830 tree eptype = NULL_TREE;
3831 int noconvert = flag;
3832 const char *invalid_op_diag;
3835 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3837 arg = remove_c_maybe_const_expr (arg);
3839 if (code != ADDR_EXPR)
3840 arg = require_complete_type (arg);
3842 typecode = TREE_CODE (TREE_TYPE (arg));
3843 if (typecode == ERROR_MARK)
3844 return error_mark_node;
3845 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3846 typecode = INTEGER_TYPE;
3848 if ((invalid_op_diag
3849 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3851 error_at (location, invalid_op_diag);
3852 return error_mark_node;
3855 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3857 eptype = TREE_TYPE (arg);
3858 arg = TREE_OPERAND (arg, 0);
3864 /* This is used for unary plus, because a CONVERT_EXPR
3865 is enough to prevent anybody from looking inside for
3866 associativity, but won't generate any code. */
3867 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3868 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3869 || typecode == VECTOR_TYPE))
3871 error_at (location, "wrong type argument to unary plus");
3872 return error_mark_node;
3874 else if (!noconvert)
3875 arg = default_conversion (arg);
3876 arg = non_lvalue_loc (location, arg);
3880 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3881 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3882 || typecode == VECTOR_TYPE))
3884 error_at (location, "wrong type argument to unary minus");
3885 return error_mark_node;
3887 else if (!noconvert)
3888 arg = default_conversion (arg);
3892 /* ~ works on integer types and non float vectors. */
3893 if (typecode == INTEGER_TYPE
3894 || (typecode == VECTOR_TYPE
3895 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3898 arg = default_conversion (arg);
3900 else if (typecode == COMPLEX_TYPE)
3903 pedwarn (location, OPT_Wpedantic,
3904 "ISO C does not support %<~%> for complex conjugation");
3906 arg = default_conversion (arg);
3910 error_at (location, "wrong type argument to bit-complement");
3911 return error_mark_node;
3916 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3918 error_at (location, "wrong type argument to abs");
3919 return error_mark_node;
3921 else if (!noconvert)
3922 arg = default_conversion (arg);
3926 /* Conjugating a real value is a no-op, but allow it anyway. */
3927 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3928 || typecode == COMPLEX_TYPE))
3930 error_at (location, "wrong type argument to conjugation");
3931 return error_mark_node;
3933 else if (!noconvert)
3934 arg = default_conversion (arg);
3937 case TRUTH_NOT_EXPR:
3938 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3939 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3940 && typecode != COMPLEX_TYPE)
3943 "wrong type argument to unary exclamation mark");
3944 return error_mark_node;
3948 arg = c_objc_common_truthvalue_conversion (location, xarg);
3949 arg = remove_c_maybe_const_expr (arg);
3952 arg = c_objc_common_truthvalue_conversion (location, arg);
3953 ret = invert_truthvalue_loc (location, arg);
3954 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3955 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3956 location = EXPR_LOCATION (ret);
3957 goto return_build_unary_op;
3961 ret = build_real_imag_expr (location, code, arg);
3962 if (ret == error_mark_node)
3963 return error_mark_node;
3964 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3965 eptype = TREE_TYPE (eptype);
3966 goto return_build_unary_op;
3968 case PREINCREMENT_EXPR:
3969 case POSTINCREMENT_EXPR:
3970 case PREDECREMENT_EXPR:
3971 case POSTDECREMENT_EXPR:
3973 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3975 tree inner = build_unary_op (location, code,
3976 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3977 if (inner == error_mark_node)
3978 return error_mark_node;
3979 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3980 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3981 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3982 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3983 goto return_build_unary_op;
3986 /* Complain about anything that is not a true lvalue. In
3987 Objective-C, skip this check for property_refs. */
3988 if (!objc_is_property_ref (arg)
3989 && !lvalue_or_else (location,
3990 arg, ((code == PREINCREMENT_EXPR
3991 || code == POSTINCREMENT_EXPR)
3994 return error_mark_node;
3996 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3998 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3999 warning_at (location, OPT_Wc___compat,
4000 "increment of enumeration value is invalid in C++");
4002 warning_at (location, OPT_Wc___compat,
4003 "decrement of enumeration value is invalid in C++");
4006 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4007 arg = c_fully_fold (arg, false, NULL);
4010 atomic_op = really_atomic_lvalue (arg);
4012 /* Increment or decrement the real part of the value,
4013 and don't change the imaginary part. */
4014 if (typecode == COMPLEX_TYPE)
4018 pedwarn (location, OPT_Wpedantic,
4019 "ISO C does not support %<++%> and %<--%> on complex types");
4023 arg = stabilize_reference (arg);
4024 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4025 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4026 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4027 if (real == error_mark_node || imag == error_mark_node)
4028 return error_mark_node;
4029 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4031 goto return_build_unary_op;
4035 /* Report invalid types. */
4037 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4038 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4039 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4041 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4042 error_at (location, "wrong type argument to increment");
4044 error_at (location, "wrong type argument to decrement");
4046 return error_mark_node;
4052 argtype = TREE_TYPE (arg);
4054 /* Compute the increment. */
4056 if (typecode == POINTER_TYPE)
4058 /* If pointer target is an incomplete type,
4059 we just cannot know how to do the arithmetic. */
4060 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4062 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4064 "increment of pointer to an incomplete type %qT",
4065 TREE_TYPE (argtype));
4068 "decrement of pointer to an incomplete type %qT",
4069 TREE_TYPE (argtype));
4071 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4072 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4074 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4075 pedwarn (location, OPT_Wpointer_arith,
4076 "wrong type argument to increment");
4078 pedwarn (location, OPT_Wpointer_arith,
4079 "wrong type argument to decrement");
4082 inc = c_size_in_bytes (TREE_TYPE (argtype));
4083 inc = convert_to_ptrofftype_loc (location, inc);
4085 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4087 /* For signed fract types, we invert ++ to -- or
4088 -- to ++, and change inc from 1 to -1, because
4089 it is not possible to represent 1 in signed fract constants.
4090 For unsigned fract types, the result always overflows and
4091 we get an undefined (original) or the maximum value. */
4092 if (code == PREINCREMENT_EXPR)
4093 code = PREDECREMENT_EXPR;
4094 else if (code == PREDECREMENT_EXPR)
4095 code = PREINCREMENT_EXPR;
4096 else if (code == POSTINCREMENT_EXPR)
4097 code = POSTDECREMENT_EXPR;
4098 else /* code == POSTDECREMENT_EXPR */
4099 code = POSTINCREMENT_EXPR;
4101 inc = integer_minus_one_node;
4102 inc = convert (argtype, inc);
4106 inc = VECTOR_TYPE_P (argtype)
4107 ? build_one_cst (argtype)
4109 inc = convert (argtype, inc);
4112 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4113 need to ask Objective-C to build the increment or decrement
4114 expression for it. */
4115 if (objc_is_property_ref (arg))
4116 return objc_build_incr_expr_for_property_ref (location, code,
4119 /* Report a read-only lvalue. */
4120 if (TYPE_READONLY (argtype))
4122 readonly_error (location, arg,
4123 ((code == PREINCREMENT_EXPR
4124 || code == POSTINCREMENT_EXPR)
4125 ? lv_increment : lv_decrement));
4126 return error_mark_node;
4128 else if (TREE_READONLY (arg))
4129 readonly_warning (arg,
4130 ((code == PREINCREMENT_EXPR
4131 || code == POSTINCREMENT_EXPR)
4132 ? lv_increment : lv_decrement));
4134 /* If the argument is atomic, use the special code sequences for
4135 atomic compound assignment. */
4138 arg = stabilize_reference (arg);
4139 ret = build_atomic_assign (location, arg,
4140 ((code == PREINCREMENT_EXPR
4141 || code == POSTINCREMENT_EXPR)
4144 (FRACT_MODE_P (TYPE_MODE (argtype))
4146 : integer_one_node),
4147 (code == POSTINCREMENT_EXPR
4148 || code == POSTDECREMENT_EXPR));
4149 goto return_build_unary_op;
4152 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4153 val = boolean_increment (code, arg);
4155 val = build2 (code, TREE_TYPE (arg), arg, inc);
4156 TREE_SIDE_EFFECTS (val) = 1;
4157 if (TREE_CODE (val) != code)
4158 TREE_NO_WARNING (val) = 1;
4160 goto return_build_unary_op;
4164 /* Note that this operation never does default_conversion. */
4166 /* The operand of unary '&' must be an lvalue (which excludes
4167 expressions of type void), or, in C99, the result of a [] or
4168 unary '*' operator. */
4169 if (VOID_TYPE_P (TREE_TYPE (arg))
4170 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4171 && (TREE_CODE (arg) != INDIRECT_REF
4173 pedwarn (location, 0, "taking address of expression of type %<void%>");
4175 /* Let &* cancel out to simplify resulting code. */
4176 if (TREE_CODE (arg) == INDIRECT_REF)
4178 /* Don't let this be an lvalue. */
4179 if (lvalue_p (TREE_OPERAND (arg, 0)))
4180 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4181 ret = TREE_OPERAND (arg, 0);
4182 goto return_build_unary_op;
4185 /* For &x[y], return x+y */
4186 if (TREE_CODE (arg) == ARRAY_REF)
4188 tree op0 = TREE_OPERAND (arg, 0);
4189 if (!c_mark_addressable (op0))
4190 return error_mark_node;
4193 /* Anything not already handled and not a true memory reference
4194 or a non-lvalue array is an error. */
4195 else if (typecode != FUNCTION_TYPE && !flag
4196 && !lvalue_or_else (location, arg, lv_addressof))
4197 return error_mark_node;
4199 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4201 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4203 tree inner = build_unary_op (location, code,
4204 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4205 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4206 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4207 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4208 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4209 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4210 goto return_build_unary_op;
4213 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4214 argtype = TREE_TYPE (arg);
4216 /* If the lvalue is const or volatile, merge that into the type
4217 to which the address will point. This is only needed
4218 for function types. */
4219 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4220 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4221 && TREE_CODE (argtype) == FUNCTION_TYPE)
4223 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4224 int quals = orig_quals;
4226 if (TREE_READONLY (arg))
4227 quals |= TYPE_QUAL_CONST;
4228 if (TREE_THIS_VOLATILE (arg))
4229 quals |= TYPE_QUAL_VOLATILE;
4231 argtype = c_build_qualified_type (argtype, quals);
4234 if (!c_mark_addressable (arg))
4235 return error_mark_node;
4237 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4238 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4240 argtype = build_pointer_type (argtype);
4242 /* ??? Cope with user tricks that amount to offsetof. Delete this
4243 when we have proper support for integer constant expressions. */
4244 val = get_base_address (arg);
4245 if (val && TREE_CODE (val) == INDIRECT_REF
4246 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4248 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4249 goto return_build_unary_op;
4252 val = build1 (ADDR_EXPR, argtype, arg);
4255 goto return_build_unary_op;
4262 argtype = TREE_TYPE (arg);
4263 if (TREE_CODE (arg) == INTEGER_CST)
4264 ret = (require_constant_value
4265 ? fold_build1_initializer_loc (location, code, argtype, arg)
4266 : fold_build1_loc (location, code, argtype, arg));
4268 ret = build1 (code, argtype, arg);
4269 return_build_unary_op:
4270 gcc_assert (ret != error_mark_node);
4271 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4272 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4273 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4274 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4275 ret = note_integer_operands (ret);
4277 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4278 protected_set_expr_location (ret, location);
4282 /* Return nonzero if REF is an lvalue valid for this language.
4283 Lvalues can be assigned, unless their type has TYPE_READONLY.
4284 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4287 lvalue_p (const_tree ref)
4289 const enum tree_code code = TREE_CODE (ref);
4296 return lvalue_p (TREE_OPERAND (ref, 0));
4298 case C_MAYBE_CONST_EXPR:
4299 return lvalue_p (TREE_OPERAND (ref, 1));
4301 case COMPOUND_LITERAL_EXPR:
4307 case ARRAY_NOTATION_REF:
4312 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4313 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4316 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4323 /* Give a warning for storing in something that is read-only in GCC
4324 terms but not const in ISO C terms. */
4327 readonly_warning (tree arg, enum lvalue_use use)
4332 warning (0, "assignment of read-only location %qE", arg);
4335 warning (0, "increment of read-only location %qE", arg);
4338 warning (0, "decrement of read-only location %qE", arg);
4347 /* Return nonzero if REF is an lvalue valid for this language;
4348 otherwise, print an error message and return zero. USE says
4349 how the lvalue is being used and so selects the error message.
4350 LOCATION is the location at which any error should be reported. */
4353 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4355 int win = lvalue_p (ref);
4358 lvalue_error (loc, use);
4363 /* Mark EXP saying that we need to be able to take the
4364 address of it; it should not be allocated in a register.
4365 Returns true if successful. */
4368 c_mark_addressable (tree exp)
4373 switch (TREE_CODE (x))
4376 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4379 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4383 /* ... fall through ... */
4389 x = TREE_OPERAND (x, 0);
4392 case COMPOUND_LITERAL_EXPR:
4394 TREE_ADDRESSABLE (x) = 1;
4401 if (C_DECL_REGISTER (x)
4402 && DECL_NONLOCAL (x))
4404 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4407 ("global register variable %qD used in nested function", x);
4410 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4412 else if (C_DECL_REGISTER (x))
4414 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4415 error ("address of global register variable %qD requested", x);
4417 error ("address of register variable %qD requested", x);
4423 TREE_ADDRESSABLE (x) = 1;
4430 /* Convert EXPR to TYPE, warning about conversion problems with
4431 constants. SEMANTIC_TYPE is the type this conversion would use
4432 without excess precision. If SEMANTIC_TYPE is NULL, this function
4433 is equivalent to convert_and_check. This function is a wrapper that
4434 handles conversions that may be different than
4435 the usual ones because of excess precision. */
4438 ep_convert_and_check (location_t loc, tree type, tree expr,
4441 if (TREE_TYPE (expr) == type)
4445 return convert_and_check (loc, type, expr);
4447 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4448 && TREE_TYPE (expr) != semantic_type)
4450 /* For integers, we need to check the real conversion, not
4451 the conversion to the excess precision type. */
4452 expr = convert_and_check (loc, semantic_type, expr);
4454 /* Result type is the excess precision type, which should be
4455 large enough, so do not check. */
4456 return convert (type, expr);
4459 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4460 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4461 if folded to an integer constant then the unselected half may
4462 contain arbitrary operations not normally permitted in constant
4463 expressions. Set the location of the expression to LOC. */
4466 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4467 tree op1, tree op1_original_type, tree op2,
4468 tree op2_original_type)
4472 enum tree_code code1;
4473 enum tree_code code2;
4474 tree result_type = NULL;
4475 tree semantic_result_type = NULL;
4476 tree orig_op1 = op1, orig_op2 = op2;
4477 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4478 bool ifexp_int_operands;
4481 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4482 if (op1_int_operands)
4483 op1 = remove_c_maybe_const_expr (op1);
4484 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4485 if (op2_int_operands)
4486 op2 = remove_c_maybe_const_expr (op2);
4487 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4488 if (ifexp_int_operands)
4489 ifexp = remove_c_maybe_const_expr (ifexp);
4491 /* Promote both alternatives. */
4493 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4494 op1 = default_conversion (op1);
4495 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4496 op2 = default_conversion (op2);
4498 if (TREE_CODE (ifexp) == ERROR_MARK
4499 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4500 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4501 return error_mark_node;
4503 type1 = TREE_TYPE (op1);
4504 code1 = TREE_CODE (type1);
4505 type2 = TREE_TYPE (op2);
4506 code2 = TREE_CODE (type2);
4508 /* C90 does not permit non-lvalue arrays in conditional expressions.
4509 In C99 they will be pointers by now. */
4510 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4512 error_at (colon_loc, "non-lvalue array in conditional expression");
4513 return error_mark_node;
4516 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4517 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4518 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4519 || code1 == COMPLEX_TYPE)
4520 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4521 || code2 == COMPLEX_TYPE))
4523 semantic_result_type = c_common_type (type1, type2);
4524 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4526 op1 = TREE_OPERAND (op1, 0);
4527 type1 = TREE_TYPE (op1);
4528 gcc_assert (TREE_CODE (type1) == code1);
4530 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4532 op2 = TREE_OPERAND (op2, 0);
4533 type2 = TREE_TYPE (op2);
4534 gcc_assert (TREE_CODE (type2) == code2);
4538 if (warn_cxx_compat)
4540 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4541 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4543 if (TREE_CODE (t1) == ENUMERAL_TYPE
4544 && TREE_CODE (t2) == ENUMERAL_TYPE
4545 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4546 warning_at (colon_loc, OPT_Wc___compat,
4547 ("different enum types in conditional is "
4548 "invalid in C++: %qT vs %qT"),
4552 /* Quickly detect the usual case where op1 and op2 have the same type
4554 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4557 result_type = type1;
4559 result_type = TYPE_MAIN_VARIANT (type1);
4561 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4562 || code1 == COMPLEX_TYPE)
4563 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4564 || code2 == COMPLEX_TYPE))
4566 result_type = c_common_type (type1, type2);
4567 do_warn_double_promotion (result_type, type1, type2,
4568 "implicit conversion from %qT to %qT to "
4569 "match other result of conditional",
4572 /* If -Wsign-compare, warn here if type1 and type2 have
4573 different signedness. We'll promote the signed to unsigned
4574 and later code won't know it used to be different.
4575 Do this check on the original types, so that explicit casts
4576 will be considered, but default promotions won't. */
4577 if (c_inhibit_evaluation_warnings == 0)
4579 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4580 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4582 if (unsigned_op1 ^ unsigned_op2)
4586 /* Do not warn if the result type is signed, since the
4587 signed type will only be chosen if it can represent
4588 all the values of the unsigned type. */
4589 if (!TYPE_UNSIGNED (result_type))
4593 bool op1_maybe_const = true;
4594 bool op2_maybe_const = true;
4596 /* Do not warn if the signed quantity is an
4597 unsuffixed integer literal (or some static
4598 constant expression involving such literals) and
4599 it is non-negative. This warning requires the
4600 operands to be folded for best results, so do
4601 that folding in this case even without
4602 warn_sign_compare to avoid warning options
4603 possibly affecting code generation. */
4604 c_inhibit_evaluation_warnings
4605 += (ifexp == truthvalue_false_node);
4606 op1 = c_fully_fold (op1, require_constant_value,
4608 c_inhibit_evaluation_warnings
4609 -= (ifexp == truthvalue_false_node);
4611 c_inhibit_evaluation_warnings
4612 += (ifexp == truthvalue_true_node);
4613 op2 = c_fully_fold (op2, require_constant_value,
4615 c_inhibit_evaluation_warnings
4616 -= (ifexp == truthvalue_true_node);
4618 if (warn_sign_compare)
4621 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4623 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4626 warning_at (colon_loc, OPT_Wsign_compare,
4627 ("signed and unsigned type in "
4628 "conditional expression"));
4630 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4631 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4632 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4633 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4638 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4640 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4641 pedwarn (colon_loc, OPT_Wpedantic,
4642 "ISO C forbids conditional expr with only one void side");
4643 result_type = void_type_node;
4645 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4647 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4648 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4649 addr_space_t as_common;
4651 if (comp_target_types (colon_loc, type1, type2))
4652 result_type = common_pointer_type (type1, type2);
4653 else if (null_pointer_constant_p (orig_op1))
4654 result_type = type2;
4655 else if (null_pointer_constant_p (orig_op2))
4656 result_type = type1;
4657 else if (!addr_space_superset (as1, as2, &as_common))
4659 error_at (colon_loc, "pointers to disjoint address spaces "
4660 "used in conditional expression");
4661 return error_mark_node;
4663 else if (VOID_TYPE_P (TREE_TYPE (type1))
4664 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4666 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4667 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4668 & ~TYPE_QUALS (TREE_TYPE (type1))))
4669 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4670 "pointer to array loses qualifier "
4671 "in conditional expression");
4673 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4674 pedwarn (colon_loc, OPT_Wpedantic,
4675 "ISO C forbids conditional expr between "
4676 "%<void *%> and function pointer");
4677 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4678 TREE_TYPE (type2)));
4680 else if (VOID_TYPE_P (TREE_TYPE (type2))
4681 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4683 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4684 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4685 & ~TYPE_QUALS (TREE_TYPE (type2))))
4686 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4687 "pointer to array loses qualifier "
4688 "in conditional expression");
4690 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4691 pedwarn (colon_loc, OPT_Wpedantic,
4692 "ISO C forbids conditional expr between "
4693 "%<void *%> and function pointer");
4694 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4695 TREE_TYPE (type1)));
4697 /* Objective-C pointer comparisons are a bit more lenient. */
4698 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4699 result_type = objc_common_type (type1, type2);
4702 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4704 pedwarn (colon_loc, 0,
4705 "pointer type mismatch in conditional expression");
4706 result_type = build_pointer_type
4707 (build_qualified_type (void_type_node, qual));
4710 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4712 if (!null_pointer_constant_p (orig_op2))
4713 pedwarn (colon_loc, 0,
4714 "pointer/integer type mismatch in conditional expression");
4717 op2 = null_pointer_node;
4719 result_type = type1;
4721 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4723 if (!null_pointer_constant_p (orig_op1))
4724 pedwarn (colon_loc, 0,
4725 "pointer/integer type mismatch in conditional expression");
4728 op1 = null_pointer_node;
4730 result_type = type2;
4735 if (flag_cond_mismatch)
4736 result_type = void_type_node;
4739 error_at (colon_loc, "type mismatch in conditional expression");
4740 return error_mark_node;
4744 /* Merge const and volatile flags of the incoming types. */
4746 = build_type_variant (result_type,
4747 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4748 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4750 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4751 semantic_result_type);
4752 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4753 semantic_result_type);
4755 if (ifexp_bcp && ifexp == truthvalue_true_node)
4757 op2_int_operands = true;
4758 op1 = c_fully_fold (op1, require_constant_value, NULL);
4760 if (ifexp_bcp && ifexp == truthvalue_false_node)
4762 op1_int_operands = true;
4763 op2 = c_fully_fold (op2, require_constant_value, NULL);
4765 int_const = int_operands = (ifexp_int_operands
4767 && op2_int_operands);
4770 int_const = ((ifexp == truthvalue_true_node
4771 && TREE_CODE (orig_op1) == INTEGER_CST
4772 && !TREE_OVERFLOW (orig_op1))
4773 || (ifexp == truthvalue_false_node
4774 && TREE_CODE (orig_op2) == INTEGER_CST
4775 && !TREE_OVERFLOW (orig_op2)));
4777 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4778 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4783 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4784 nested inside of the expression. */
4785 op1 = c_fully_fold (op1, false, NULL);
4786 op2 = c_fully_fold (op2, false, NULL);
4788 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4790 ret = note_integer_operands (ret);
4792 if (semantic_result_type)
4793 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4795 protected_set_expr_location (ret, colon_loc);
4799 /* Return a compound expression that performs two expressions and
4800 returns the value of the second of them.
4802 LOC is the location of the COMPOUND_EXPR. */
4805 build_compound_expr (location_t loc, tree expr1, tree expr2)
4807 bool expr1_int_operands, expr2_int_operands;
4808 tree eptype = NULL_TREE;
4812 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4813 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4816 "spawned function call cannot be part of a comma expression");
4817 return error_mark_node;
4819 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4820 if (expr1_int_operands)
4821 expr1 = remove_c_maybe_const_expr (expr1);
4822 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4823 if (expr2_int_operands)
4824 expr2 = remove_c_maybe_const_expr (expr2);
4826 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4827 expr1 = TREE_OPERAND (expr1, 0);
4828 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4830 eptype = TREE_TYPE (expr2);
4831 expr2 = TREE_OPERAND (expr2, 0);
4834 if (!TREE_SIDE_EFFECTS (expr1))
4836 /* The left-hand operand of a comma expression is like an expression
4837 statement: with -Wunused, we should warn if it doesn't have
4838 any side-effects, unless it was explicitly cast to (void). */
4839 if (warn_unused_value)
4841 if (VOID_TYPE_P (TREE_TYPE (expr1))
4842 && CONVERT_EXPR_P (expr1))
4844 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4845 && TREE_CODE (expr1) == COMPOUND_EXPR
4846 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4847 ; /* (void) a, (void) b, c */
4849 warning_at (loc, OPT_Wunused_value,
4850 "left-hand operand of comma expression has no effect");
4853 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4854 && warn_unused_value)
4857 location_t cloc = loc;
4858 while (TREE_CODE (r) == COMPOUND_EXPR)
4860 if (EXPR_HAS_LOCATION (r))
4861 cloc = EXPR_LOCATION (r);
4862 r = TREE_OPERAND (r, 1);
4864 if (!TREE_SIDE_EFFECTS (r)
4865 && !VOID_TYPE_P (TREE_TYPE (r))
4866 && !CONVERT_EXPR_P (r))
4867 warning_at (cloc, OPT_Wunused_value,
4868 "right-hand operand of comma expression has no effect");
4871 /* With -Wunused, we should also warn if the left-hand operand does have
4872 side-effects, but computes a value which is not used. For example, in
4873 `foo() + bar(), baz()' the result of the `+' operator is not used,
4874 so we should issue a warning. */
4875 else if (warn_unused_value)
4876 warn_if_unused_value (expr1, loc);
4878 if (expr2 == error_mark_node)
4879 return error_mark_node;
4881 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4884 && expr1_int_operands
4885 && expr2_int_operands)
4886 ret = note_integer_operands (ret);
4889 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4891 protected_set_expr_location (ret, loc);
4895 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4896 which we are casting. OTYPE is the type of the expression being
4897 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4898 of the cast. -Wcast-qual appeared on the command line. Named
4899 address space qualifiers are not handled here, because they result
4900 in different warnings. */
4903 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4905 tree in_type = type;
4906 tree in_otype = otype;
4911 /* Check that the qualifiers on IN_TYPE are a superset of the
4912 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4913 nodes is uninteresting and we stop as soon as we hit a
4914 non-POINTER_TYPE node on either type. */
4917 in_otype = TREE_TYPE (in_otype);
4918 in_type = TREE_TYPE (in_type);
4920 /* GNU C allows cv-qualified function types. 'const' means the
4921 function is very pure, 'volatile' means it can't return. We
4922 need to warn when such qualifiers are added, not when they're
4924 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4925 && TREE_CODE (in_type) == FUNCTION_TYPE)
4926 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4927 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4929 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4930 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4932 while (TREE_CODE (in_type) == POINTER_TYPE
4933 && TREE_CODE (in_otype) == POINTER_TYPE);
4936 warning_at (loc, OPT_Wcast_qual,
4937 "cast adds %q#v qualifier to function type", added);
4940 /* There are qualifiers present in IN_OTYPE that are not present
4942 warning_at (loc, OPT_Wcast_qual,
4943 "cast discards %qv qualifier from pointer target type",
4946 if (added || discarded)
4949 /* A cast from **T to const **T is unsafe, because it can cause a
4950 const value to be changed with no additional warning. We only
4951 issue this warning if T is the same on both sides, and we only
4952 issue the warning if there are the same number of pointers on
4953 both sides, as otherwise the cast is clearly unsafe anyhow. A
4954 cast is unsafe when a qualifier is added at one level and const
4955 is not present at all outer levels.
4957 To issue this warning, we check at each level whether the cast
4958 adds new qualifiers not already seen. We don't need to special
4959 case function types, as they won't have the same
4960 TYPE_MAIN_VARIANT. */
4962 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4964 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4969 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4972 in_type = TREE_TYPE (in_type);
4973 in_otype = TREE_TYPE (in_otype);
4974 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4977 warning_at (loc, OPT_Wcast_qual,
4978 "to be safe all intermediate pointers in cast from "
4979 "%qT to %qT must be %<const%> qualified",
4984 is_const = TYPE_READONLY (in_type);
4986 while (TREE_CODE (in_type) == POINTER_TYPE);
4989 /* Build an expression representing a cast to type TYPE of expression EXPR.
4990 LOC is the location of the cast-- typically the open paren of the cast. */
4993 build_c_cast (location_t loc, tree type, tree expr)
4997 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4998 expr = TREE_OPERAND (expr, 0);
5002 if (type == error_mark_node || expr == error_mark_node)
5003 return error_mark_node;
5005 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5006 only in <protocol> qualifications. But when constructing cast expressions,
5007 the protocols do matter and must be kept around. */
5008 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5009 return build1 (NOP_EXPR, type, expr);
5011 type = TYPE_MAIN_VARIANT (type);
5013 if (TREE_CODE (type) == ARRAY_TYPE)
5015 error_at (loc, "cast specifies array type");
5016 return error_mark_node;
5019 if (TREE_CODE (type) == FUNCTION_TYPE)
5021 error_at (loc, "cast specifies function type");
5022 return error_mark_node;
5025 if (!VOID_TYPE_P (type))
5027 value = require_complete_type (value);
5028 if (value == error_mark_node)
5029 return error_mark_node;
5032 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5034 if (TREE_CODE (type) == RECORD_TYPE
5035 || TREE_CODE (type) == UNION_TYPE)
5036 pedwarn (loc, OPT_Wpedantic,
5037 "ISO C forbids casting nonscalar to the same type");
5039 /* Convert to remove any qualifiers from VALUE's type. */
5040 value = convert (type, value);
5042 else if (TREE_CODE (type) == UNION_TYPE)
5046 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5047 if (TREE_TYPE (field) != error_mark_node
5048 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5049 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5055 bool maybe_const = true;
5057 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5058 t = c_fully_fold (value, false, &maybe_const);
5059 t = build_constructor_single (type, field, t);
5061 t = c_wrap_maybe_const (t, true);
5062 t = digest_init (loc, type, t,
5063 NULL_TREE, false, true, 0);
5064 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5067 error_at (loc, "cast to union type from type not present in union");
5068 return error_mark_node;
5074 if (type == void_type_node)
5076 tree t = build1 (CONVERT_EXPR, type, value);
5077 SET_EXPR_LOCATION (t, loc);
5081 otype = TREE_TYPE (value);
5083 /* Optionally warn about potentially worrisome casts. */
5085 && TREE_CODE (type) == POINTER_TYPE
5086 && TREE_CODE (otype) == POINTER_TYPE)
5087 handle_warn_cast_qual (loc, type, otype);
5089 /* Warn about conversions between pointers to disjoint
5091 if (TREE_CODE (type) == POINTER_TYPE
5092 && TREE_CODE (otype) == POINTER_TYPE
5093 && !null_pointer_constant_p (value))
5095 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5096 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5097 addr_space_t as_common;
5099 if (!addr_space_superset (as_to, as_from, &as_common))
5101 if (ADDR_SPACE_GENERIC_P (as_from))
5102 warning_at (loc, 0, "cast to %s address space pointer "
5103 "from disjoint generic address space pointer",
5104 c_addr_space_name (as_to));
5106 else if (ADDR_SPACE_GENERIC_P (as_to))
5107 warning_at (loc, 0, "cast to generic address space pointer "
5108 "from disjoint %s address space pointer",
5109 c_addr_space_name (as_from));
5112 warning_at (loc, 0, "cast to %s address space pointer "
5113 "from disjoint %s address space pointer",
5114 c_addr_space_name (as_to),
5115 c_addr_space_name (as_from));
5119 /* Warn about possible alignment problems. */
5120 if (STRICT_ALIGNMENT
5121 && TREE_CODE (type) == POINTER_TYPE
5122 && TREE_CODE (otype) == POINTER_TYPE
5123 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5124 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5125 /* Don't warn about opaque types, where the actual alignment
5126 restriction is unknown. */
5127 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5128 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5129 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5130 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5131 warning_at (loc, OPT_Wcast_align,
5132 "cast increases required alignment of target type");
5134 if (TREE_CODE (type) == INTEGER_TYPE
5135 && TREE_CODE (otype) == POINTER_TYPE
5136 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5137 /* Unlike conversion of integers to pointers, where the
5138 warning is disabled for converting constants because
5139 of cases such as SIG_*, warn about converting constant
5140 pointers to integers. In some cases it may cause unwanted
5141 sign extension, and a warning is appropriate. */
5142 warning_at (loc, OPT_Wpointer_to_int_cast,
5143 "cast from pointer to integer of different size");
5145 if (TREE_CODE (value) == CALL_EXPR
5146 && TREE_CODE (type) != TREE_CODE (otype))
5147 warning_at (loc, OPT_Wbad_function_cast,
5148 "cast from function call of type %qT "
5149 "to non-matching type %qT", otype, type);
5151 if (TREE_CODE (type) == POINTER_TYPE
5152 && TREE_CODE (otype) == INTEGER_TYPE
5153 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5154 /* Don't warn about converting any constant. */
5155 && !TREE_CONSTANT (value))
5157 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5158 "of different size");
5160 if (warn_strict_aliasing <= 2)
5161 strict_aliasing_warning (otype, type, expr);
5163 /* If pedantic, warn for conversions between function and object
5164 pointer types, except for converting a null pointer constant
5165 to function pointer type. */
5167 && TREE_CODE (type) == POINTER_TYPE
5168 && TREE_CODE (otype) == POINTER_TYPE
5169 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5170 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5171 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5172 "conversion of function pointer to object pointer type");
5175 && TREE_CODE (type) == POINTER_TYPE
5176 && TREE_CODE (otype) == POINTER_TYPE
5177 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5178 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5179 && !null_pointer_constant_p (value))
5180 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5181 "conversion of object pointer to function pointer type");
5184 value = convert (type, value);
5186 /* Ignore any integer overflow caused by the cast. */
5187 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5189 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5191 if (!TREE_OVERFLOW (value))
5193 /* Avoid clobbering a shared constant. */
5194 value = copy_node (value);
5195 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5198 else if (TREE_OVERFLOW (value))
5199 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5200 value = wide_int_to_tree (TREE_TYPE (value), value);
5204 /* Don't let a cast be an lvalue. */
5206 value = non_lvalue_loc (loc, value);
5208 /* Don't allow the results of casting to floating-point or complex
5209 types be confused with actual constants, or casts involving
5210 integer and pointer types other than direct integer-to-integer
5211 and integer-to-pointer be confused with integer constant
5212 expressions and null pointer constants. */
5213 if (TREE_CODE (value) == REAL_CST
5214 || TREE_CODE (value) == COMPLEX_CST
5215 || (TREE_CODE (value) == INTEGER_CST
5216 && !((TREE_CODE (expr) == INTEGER_CST
5217 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5218 || TREE_CODE (expr) == REAL_CST
5219 || TREE_CODE (expr) == COMPLEX_CST)))
5220 value = build1 (NOP_EXPR, type, value);
5222 if (CAN_HAVE_LOCATION_P (value))
5223 SET_EXPR_LOCATION (value, loc);
5227 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5228 location of the open paren of the cast, or the position of the cast
5231 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5234 tree type_expr = NULL_TREE;
5235 bool type_expr_const = true;
5237 int saved_wsp = warn_strict_prototypes;
5239 /* This avoids warnings about unprototyped casts on
5240 integers. E.g. "#define SIG_DFL (void(*)())0". */
5241 if (TREE_CODE (expr) == INTEGER_CST)
5242 warn_strict_prototypes = 0;
5243 type = groktypename (type_name, &type_expr, &type_expr_const);
5244 warn_strict_prototypes = saved_wsp;
5246 ret = build_c_cast (loc, type, expr);
5249 bool inner_expr_const = true;
5250 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5251 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5252 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5253 && inner_expr_const);
5254 SET_EXPR_LOCATION (ret, loc);
5257 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5258 SET_EXPR_LOCATION (ret, loc);
5260 /* C++ does not permits types to be defined in a cast, but it
5261 allows references to incomplete types. */
5262 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5263 warning_at (loc, OPT_Wc___compat,
5264 "defining a type in a cast is invalid in C++");
5269 /* Build an assignment expression of lvalue LHS from value RHS.
5270 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5271 may differ from TREE_TYPE (LHS) for an enum bitfield.
5272 MODIFYCODE is the code for a binary operator that we use
5273 to combine the old value of LHS with RHS to get the new value.
5274 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5275 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5276 which may differ from TREE_TYPE (RHS) for an enum value.
5278 LOCATION is the location of the MODIFYCODE operator.
5279 RHS_LOC is the location of the RHS. */
5282 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5283 enum tree_code modifycode,
5284 location_t rhs_loc, tree rhs, tree rhs_origtype)
5288 tree rhseval = NULL_TREE;
5289 tree rhs_semantic_type = NULL_TREE;
5290 tree lhstype = TREE_TYPE (lhs);
5291 tree olhstype = lhstype;
5295 /* Types that aren't fully specified cannot be used in assignments. */
5296 lhs = require_complete_type (lhs);
5298 /* Avoid duplicate error messages from operands that had errors. */
5299 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5300 return error_mark_node;
5302 /* Ensure an error for assigning a non-lvalue array to an array in
5304 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5306 error_at (location, "assignment to expression with array type");
5307 return error_mark_node;
5310 /* For ObjC properties, defer this check. */
5311 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5312 return error_mark_node;
5314 is_atomic_op = really_atomic_lvalue (lhs);
5316 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5318 rhs_semantic_type = TREE_TYPE (rhs);
5319 rhs = TREE_OPERAND (rhs, 0);
5324 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5326 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5327 lhs_origtype, modifycode, rhs_loc, rhs,
5329 if (inner == error_mark_node)
5330 return error_mark_node;
5331 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5332 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5333 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5334 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5335 protected_set_expr_location (result, location);
5339 /* If a binary op has been requested, combine the old LHS value with the RHS
5340 producing the value we should actually store into the LHS. */
5342 if (modifycode != NOP_EXPR)
5344 lhs = c_fully_fold (lhs, false, NULL);
5345 lhs = stabilize_reference (lhs);
5347 /* Construct the RHS for any non-atomic compound assignemnt. */
5350 /* If in LHS op= RHS the RHS has side-effects, ensure they
5351 are preevaluated before the rest of the assignment expression's
5352 side-effects, because RHS could contain e.g. function calls
5354 if (TREE_SIDE_EFFECTS (rhs))
5356 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5359 newrhs = build_binary_op (location,
5360 modifycode, lhs, newrhs, 1);
5362 /* The original type of the right hand side is no longer
5364 rhs_origtype = NULL_TREE;
5368 if (c_dialect_objc ())
5370 /* Check if we are modifying an Objective-C property reference;
5371 if so, we need to generate setter calls. */
5372 result = objc_maybe_build_modify_expr (lhs, newrhs);
5376 /* Else, do the check that we postponed for Objective-C. */
5377 if (!lvalue_or_else (location, lhs, lv_assign))
5378 return error_mark_node;
5381 /* Give an error for storing in something that is 'const'. */
5383 if (TYPE_READONLY (lhstype)
5384 || ((TREE_CODE (lhstype) == RECORD_TYPE
5385 || TREE_CODE (lhstype) == UNION_TYPE)
5386 && C_TYPE_FIELDS_READONLY (lhstype)))
5388 readonly_error (location, lhs, lv_assign);
5389 return error_mark_node;
5391 else if (TREE_READONLY (lhs))
5392 readonly_warning (lhs, lv_assign);
5394 /* If storing into a structure or union member,
5395 it has probably been given type `int'.
5396 Compute the type that would go with
5397 the actual amount of storage the member occupies. */
5399 if (TREE_CODE (lhs) == COMPONENT_REF
5400 && (TREE_CODE (lhstype) == INTEGER_TYPE
5401 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5402 || TREE_CODE (lhstype) == REAL_TYPE
5403 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5404 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5406 /* If storing in a field that is in actuality a short or narrower than one,
5407 we must store in the field in its actual type. */
5409 if (lhstype != TREE_TYPE (lhs))
5411 lhs = copy_node (lhs);
5412 TREE_TYPE (lhs) = lhstype;
5415 /* Issue -Wc++-compat warnings about an assignment to an enum type
5416 when LHS does not have its original type. This happens for,
5417 e.g., an enum bitfield in a struct. */
5419 && lhs_origtype != NULL_TREE
5420 && lhs_origtype != lhstype
5421 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5423 tree checktype = (rhs_origtype != NULL_TREE
5426 if (checktype != error_mark_node
5427 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5428 || (is_atomic_op && modifycode != NOP_EXPR)))
5429 warning_at (location, OPT_Wc___compat,
5430 "enum conversion in assignment is invalid in C++");
5433 /* If the lhs is atomic, remove that qualifier. */
5436 lhstype = build_qualified_type (lhstype,
5437 (TYPE_QUALS (lhstype)
5438 & ~TYPE_QUAL_ATOMIC));
5439 olhstype = build_qualified_type (olhstype,
5440 (TYPE_QUALS (lhstype)
5441 & ~TYPE_QUAL_ATOMIC));
5444 /* Convert new value to destination type. Fold it first, then
5445 restore any excess precision information, for the sake of
5446 conversion warnings. */
5448 if (!(is_atomic_op && modifycode != NOP_EXPR))
5450 npc = null_pointer_constant_p (newrhs);
5451 newrhs = c_fully_fold (newrhs, false, NULL);
5452 if (rhs_semantic_type)
5453 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5454 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5455 rhs_origtype, ic_assign, npc,
5456 NULL_TREE, NULL_TREE, 0);
5457 if (TREE_CODE (newrhs) == ERROR_MARK)
5458 return error_mark_node;
5461 /* Emit ObjC write barrier, if necessary. */
5462 if (c_dialect_objc () && flag_objc_gc)
5464 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5467 protected_set_expr_location (result, location);
5472 /* Scan operands. */
5475 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5478 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5479 TREE_SIDE_EFFECTS (result) = 1;
5480 protected_set_expr_location (result, location);
5483 /* If we got the LHS in a different type for storing in,
5484 convert the result back to the nominal type of LHS
5485 so that the value we return always has the same type
5486 as the LHS argument. */
5488 if (olhstype == TREE_TYPE (result))
5491 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5492 rhs_origtype, ic_assign, false, NULL_TREE,
5494 protected_set_expr_location (result, location);
5498 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5502 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5503 This is used to implement -fplan9-extensions. */
5506 find_anonymous_field_with_type (tree struct_type, tree type)
5511 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5512 || TREE_CODE (struct_type) == UNION_TYPE);
5514 for (field = TYPE_FIELDS (struct_type);
5516 field = TREE_CHAIN (field))
5518 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5519 ? c_build_qualified_type (TREE_TYPE (field),
5521 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5522 if (DECL_NAME (field) == NULL
5523 && comptypes (type, fieldtype))
5529 else if (DECL_NAME (field) == NULL
5530 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5531 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5532 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5542 /* RHS is an expression whose type is pointer to struct. If there is
5543 an anonymous field in RHS with type TYPE, then return a pointer to
5544 that field in RHS. This is used with -fplan9-extensions. This
5545 returns NULL if no conversion could be found. */
5548 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5550 tree rhs_struct_type, lhs_main_type;
5551 tree field, found_field;
5552 bool found_sub_field;
5555 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5556 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5557 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5558 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5560 gcc_assert (POINTER_TYPE_P (type));
5561 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5562 ? c_build_qualified_type (TREE_TYPE (type),
5564 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5566 found_field = NULL_TREE;
5567 found_sub_field = false;
5568 for (field = TYPE_FIELDS (rhs_struct_type);
5570 field = TREE_CHAIN (field))
5572 if (DECL_NAME (field) != NULL_TREE
5573 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5574 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5576 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5577 ? c_build_qualified_type (TREE_TYPE (field),
5579 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5580 if (comptypes (lhs_main_type, fieldtype))
5582 if (found_field != NULL_TREE)
5584 found_field = field;
5586 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5589 if (found_field != NULL_TREE)
5591 found_field = field;
5592 found_sub_field = true;
5596 if (found_field == NULL_TREE)
5599 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5600 build_fold_indirect_ref (rhs), found_field,
5602 ret = build_fold_addr_expr_loc (location, ret);
5604 if (found_sub_field)
5606 ret = convert_to_anonymous_field (location, type, ret);
5607 gcc_assert (ret != NULL_TREE);
5613 /* Issue an error message for a bad initializer component.
5614 GMSGID identifies the message.
5615 The component name is taken from the spelling stack. */
5618 error_init (location_t loc, const char *gmsgid)
5622 /* The gmsgid may be a format string with %< and %>. */
5623 error_at (loc, gmsgid);
5624 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5626 inform (loc, "(near initialization for %qs)", ofwhat);
5629 /* Issue a pedantic warning for a bad initializer component. OPT is
5630 the option OPT_* (from options.h) controlling this warning or 0 if
5631 it is unconditionally given. GMSGID identifies the message. The
5632 component name is taken from the spelling stack. */
5635 pedwarn_init (location_t location, int opt, const char *gmsgid)
5640 /* The gmsgid may be a format string with %< and %>. */
5641 warned = pedwarn (location, opt, gmsgid);
5642 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5643 if (*ofwhat && warned)
5644 inform (location, "(near initialization for %qs)", ofwhat);
5647 /* Issue a warning for a bad initializer component.
5649 OPT is the OPT_W* value corresponding to the warning option that
5650 controls this warning. GMSGID identifies the message. The
5651 component name is taken from the spelling stack. */
5654 warning_init (location_t loc, int opt, const char *gmsgid)
5659 /* The gmsgid may be a format string with %< and %>. */
5660 warned = warning_at (loc, opt, gmsgid);
5661 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5662 if (*ofwhat && warned)
5663 inform (loc, "(near initialization for %qs)", ofwhat);
5666 /* If TYPE is an array type and EXPR is a parenthesized string
5667 constant, warn if pedantic that EXPR is being used to initialize an
5668 object of type TYPE. */
5671 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5674 && TREE_CODE (type) == ARRAY_TYPE
5675 && TREE_CODE (expr.value) == STRING_CST
5676 && expr.original_code != STRING_CST)
5677 pedwarn_init (loc, OPT_Wpedantic,
5678 "array initialized from parenthesized string constant");
5681 /* Convert value RHS to type TYPE as preparation for an assignment to
5682 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5683 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5684 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5685 constant before any folding.
5686 The real work of conversion is done by `convert'.
5687 The purpose of this function is to generate error messages
5688 for assignments that are not allowed in C.
5689 ERRTYPE says whether it is argument passing, assignment,
5690 initialization or return.
5692 LOCATION is the location of the assignment, EXPR_LOC is the location of
5693 the RHS or, for a function, location of an argument.
5694 FUNCTION is a tree for the function being called.
5695 PARMNUM is the number of the argument, for printing in error messages. */
5698 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5699 tree rhs, tree origtype, enum impl_conv errtype,
5700 bool null_pointer_constant, tree fundecl,
5701 tree function, int parmnum)
5703 enum tree_code codel = TREE_CODE (type);
5704 tree orig_rhs = rhs;
5706 enum tree_code coder;
5707 tree rname = NULL_TREE;
5708 bool objc_ok = false;
5710 /* Use the expansion point location to handle cases such as user's
5711 function returning a wrong-type macro defined in a system header. */
5712 location = expansion_point_location_if_in_system_header (location);
5714 if (errtype == ic_argpass)
5717 /* Change pointer to function to the function itself for
5719 if (TREE_CODE (function) == ADDR_EXPR
5720 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5721 function = TREE_OPERAND (function, 0);
5723 /* Handle an ObjC selector specially for diagnostics. */
5724 selector = objc_message_selector ();
5726 if (selector && parmnum > 2)
5733 /* This macro is used to emit diagnostics to ensure that all format
5734 strings are complete sentences, visible to gettext and checked at
5736 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5741 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5742 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5743 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5744 "expected %qT but argument is of type %qT", \
5748 pedwarn (LOCATION, OPT, AS); \
5751 pedwarn_init (LOCATION, OPT, IN); \
5754 pedwarn (LOCATION, OPT, RE); \
5757 gcc_unreachable (); \
5761 /* This macro is used to emit diagnostics to ensure that all format
5762 strings are complete sentences, visible to gettext and checked at
5763 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
5764 extra parameter to enumerate qualifiers. */
5765 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5770 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5771 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5772 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5773 "expected %qT but argument is of type %qT", \
5777 pedwarn (LOCATION, OPT, AS, QUALS); \
5780 pedwarn (LOCATION, OPT, IN, QUALS); \
5783 pedwarn (LOCATION, OPT, RE, QUALS); \
5786 gcc_unreachable (); \
5790 /* This macro is used to emit diagnostics to ensure that all format
5791 strings are complete sentences, visible to gettext and checked at
5792 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5793 warning_at instead of pedwarn. */
5794 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5799 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5800 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5801 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5802 "expected %qT but argument is of type %qT", \
5806 warning_at (LOCATION, OPT, AS, QUALS); \
5809 warning_at (LOCATION, OPT, IN, QUALS); \
5812 warning_at (LOCATION, OPT, RE, QUALS); \
5815 gcc_unreachable (); \
5819 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5820 rhs = TREE_OPERAND (rhs, 0);
5822 rhstype = TREE_TYPE (rhs);
5823 coder = TREE_CODE (rhstype);
5825 if (coder == ERROR_MARK)
5826 return error_mark_node;
5828 if (c_dialect_objc ())
5851 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5854 if (warn_cxx_compat)
5856 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5857 if (checktype != error_mark_node
5858 && TREE_CODE (type) == ENUMERAL_TYPE
5859 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5861 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5862 G_("enum conversion when passing argument "
5863 "%d of %qE is invalid in C++"),
5864 G_("enum conversion in assignment is "
5866 G_("enum conversion in initialization is "
5868 G_("enum conversion in return is "
5873 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5876 if (coder == VOID_TYPE)
5878 /* Except for passing an argument to an unprototyped function,
5879 this is a constraint violation. When passing an argument to
5880 an unprototyped function, it is compile-time undefined;
5881 making it a constraint in that case was rejected in
5883 error_at (location, "void value not ignored as it ought to be");
5884 return error_mark_node;
5886 rhs = require_complete_type (rhs);
5887 if (rhs == error_mark_node)
5888 return error_mark_node;
5889 /* A non-reference type can convert to a reference. This handles
5890 va_start, va_copy and possibly port built-ins. */
5891 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5893 if (!lvalue_p (rhs))
5895 error_at (location, "cannot pass rvalue to reference parameter");
5896 return error_mark_node;
5898 if (!c_mark_addressable (rhs))
5899 return error_mark_node;
5900 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5901 SET_EXPR_LOCATION (rhs, location);
5903 rhs = convert_for_assignment (location, expr_loc,
5904 build_pointer_type (TREE_TYPE (type)),
5905 rhs, origtype, errtype,
5906 null_pointer_constant, fundecl, function,
5908 if (rhs == error_mark_node)
5909 return error_mark_node;
5911 rhs = build1 (NOP_EXPR, type, rhs);
5912 SET_EXPR_LOCATION (rhs, location);
5915 /* Some types can interconvert without explicit casts. */
5916 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5917 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5918 return convert (type, rhs);
5919 /* Arithmetic types all interconvert, and enum is treated like int. */
5920 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5921 || codel == FIXED_POINT_TYPE
5922 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5923 || codel == BOOLEAN_TYPE)
5924 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5925 || coder == FIXED_POINT_TYPE
5926 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5927 || coder == BOOLEAN_TYPE))
5930 bool save = in_late_binary_op;
5931 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
5932 || (coder == REAL_TYPE
5933 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
5934 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
5935 in_late_binary_op = true;
5936 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5937 ? expr_loc : location, type, orig_rhs);
5938 in_late_binary_op = save;
5942 /* Aggregates in different TUs might need conversion. */
5943 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5945 && comptypes (type, rhstype))
5946 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5947 ? expr_loc : location, type, rhs);
5949 /* Conversion to a transparent union or record from its member types.
5950 This applies only to function arguments. */
5951 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5952 && TYPE_TRANSPARENT_AGGR (type))
5953 && errtype == ic_argpass)
5955 tree memb, marginal_memb = NULL_TREE;
5957 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5959 tree memb_type = TREE_TYPE (memb);
5961 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5962 TYPE_MAIN_VARIANT (rhstype)))
5965 if (TREE_CODE (memb_type) != POINTER_TYPE)
5968 if (coder == POINTER_TYPE)
5970 tree ttl = TREE_TYPE (memb_type);
5971 tree ttr = TREE_TYPE (rhstype);
5973 /* Any non-function converts to a [const][volatile] void *
5974 and vice versa; otherwise, targets must be the same.
5975 Meanwhile, the lhs target must have all the qualifiers of
5977 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5978 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5979 || comp_target_types (location, memb_type, rhstype))
5981 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5982 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5983 /* If this type won't generate any warnings, use it. */
5984 if (lquals == rquals
5985 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5986 && TREE_CODE (ttl) == FUNCTION_TYPE)
5987 ? ((lquals | rquals) == rquals)
5988 : ((lquals | rquals) == lquals)))
5991 /* Keep looking for a better type, but remember this one. */
5993 marginal_memb = memb;
5997 /* Can convert integer zero to any pointer type. */
5998 if (null_pointer_constant)
6000 rhs = null_pointer_node;
6005 if (memb || marginal_memb)
6009 /* We have only a marginally acceptable member type;
6010 it needs a warning. */
6011 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6012 tree ttr = TREE_TYPE (rhstype);
6014 /* Const and volatile mean something different for function
6015 types, so the usual warnings are not appropriate. */
6016 if (TREE_CODE (ttr) == FUNCTION_TYPE
6017 && TREE_CODE (ttl) == FUNCTION_TYPE)
6019 /* Because const and volatile on functions are
6020 restrictions that say the function will not do
6021 certain things, it is okay to use a const or volatile
6022 function where an ordinary one is wanted, but not
6024 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6025 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6026 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6027 OPT_Wdiscarded_qualifiers,
6028 G_("passing argument %d of %qE "
6029 "makes %q#v qualified function "
6030 "pointer from unqualified"),
6031 G_("assignment makes %q#v qualified "
6032 "function pointer from "
6034 G_("initialization makes %q#v qualified "
6035 "function pointer from "
6037 G_("return makes %q#v qualified function "
6038 "pointer from unqualified"),
6039 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6041 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6042 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6043 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6044 OPT_Wdiscarded_qualifiers,
6045 G_("passing argument %d of %qE discards "
6046 "%qv qualifier from pointer target type"),
6047 G_("assignment discards %qv qualifier "
6048 "from pointer target type"),
6049 G_("initialization discards %qv qualifier "
6050 "from pointer target type"),
6051 G_("return discards %qv qualifier from "
6052 "pointer target type"),
6053 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6055 memb = marginal_memb;
6058 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6059 pedwarn (location, OPT_Wpedantic,
6060 "ISO C prohibits argument conversion to union type");
6062 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6063 return build_constructor_single (type, memb, rhs);
6067 /* Conversions among pointers */
6068 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6069 && (coder == codel))
6071 tree ttl = TREE_TYPE (type);
6072 tree ttr = TREE_TYPE (rhstype);
6075 bool is_opaque_pointer;
6076 int target_cmp = 0; /* Cache comp_target_types () result. */
6080 if (TREE_CODE (mvl) != ARRAY_TYPE)
6081 mvl = (TYPE_ATOMIC (mvl)
6082 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6084 : TYPE_MAIN_VARIANT (mvl));
6085 if (TREE_CODE (mvr) != ARRAY_TYPE)
6086 mvr = (TYPE_ATOMIC (mvr)
6087 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6089 : TYPE_MAIN_VARIANT (mvr));
6090 /* Opaque pointers are treated like void pointers. */
6091 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6093 /* The Plan 9 compiler permits a pointer to a struct to be
6094 automatically converted into a pointer to an anonymous field
6095 within the struct. */
6096 if (flag_plan9_extensions
6097 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6098 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6101 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6102 if (new_rhs != NULL_TREE)
6105 rhstype = TREE_TYPE (rhs);
6106 coder = TREE_CODE (rhstype);
6107 ttr = TREE_TYPE (rhstype);
6108 mvr = TYPE_MAIN_VARIANT (ttr);
6112 /* C++ does not allow the implicit conversion void* -> T*. However,
6113 for the purpose of reducing the number of false positives, we
6114 tolerate the special case of
6118 where NULL is typically defined in C to be '(void *) 0'. */
6119 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6120 warning_at (errtype == ic_argpass ? expr_loc : location,
6122 "request for implicit conversion "
6123 "from %qT to %qT not permitted in C++", rhstype, type);
6125 /* See if the pointers point to incompatible address spaces. */
6126 asl = TYPE_ADDR_SPACE (ttl);
6127 asr = TYPE_ADDR_SPACE (ttr);
6128 if (!null_pointer_constant_p (rhs)
6129 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6134 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6135 "non-enclosed address space", parmnum, rname);
6138 error_at (location, "assignment from pointer to "
6139 "non-enclosed address space");
6142 error_at (location, "initialization from pointer to "
6143 "non-enclosed address space");
6146 error_at (location, "return from pointer to "
6147 "non-enclosed address space");
6152 return error_mark_node;
6155 /* Check if the right-hand side has a format attribute but the
6156 left-hand side doesn't. */
6157 if (warn_suggest_attribute_format
6158 && check_missing_format_attribute (type, rhstype))
6163 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6164 "argument %d of %qE might be "
6165 "a candidate for a format attribute",
6169 warning_at (location, OPT_Wsuggest_attribute_format,
6170 "assignment left-hand side might be "
6171 "a candidate for a format attribute");
6174 warning_at (location, OPT_Wsuggest_attribute_format,
6175 "initialization left-hand side might be "
6176 "a candidate for a format attribute");
6179 warning_at (location, OPT_Wsuggest_attribute_format,
6180 "return type might be "
6181 "a candidate for a format attribute");
6188 /* Any non-function converts to a [const][volatile] void *
6189 and vice versa; otherwise, targets must be the same.
6190 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6191 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6192 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6193 || (target_cmp = comp_target_types (location, type, rhstype))
6194 || is_opaque_pointer
6195 || ((c_common_unsigned_type (mvl)
6196 == c_common_unsigned_type (mvr))
6197 && (c_common_signed_type (mvl)
6198 == c_common_signed_type (mvr))
6199 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6201 /* Warn about loss of qualifers from pointers to arrays with
6202 qualifiers on the element type. */
6203 if (TREE_CODE (ttr) == ARRAY_TYPE)
6205 ttr = strip_array_types (ttr);
6206 ttl = strip_array_types (ttl);
6208 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6209 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6210 WARNING_FOR_QUALIFIERS (location, expr_loc,
6211 OPT_Wdiscarded_array_qualifiers,
6212 G_("passing argument %d of %qE discards "
6213 "%qv qualifier from pointer target type"),
6214 G_("assignment discards %qv qualifier "
6215 "from pointer target type"),
6216 G_("initialization discards %qv qualifier "
6217 "from pointer target type"),
6218 G_("return discards %qv qualifier from "
6219 "pointer target type"),
6220 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6223 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6226 && !null_pointer_constant
6227 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6228 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6229 G_("ISO C forbids passing argument %d of "
6230 "%qE between function pointer "
6232 G_("ISO C forbids assignment between "
6233 "function pointer and %<void *%>"),
6234 G_("ISO C forbids initialization between "
6235 "function pointer and %<void *%>"),
6236 G_("ISO C forbids return between function "
6237 "pointer and %<void *%>"));
6238 /* Const and volatile mean something different for function types,
6239 so the usual warnings are not appropriate. */
6240 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6241 && TREE_CODE (ttl) != FUNCTION_TYPE)
6243 /* Don't warn about loss of qualifier for conversions from
6244 qualified void* to pointers to arrays with corresponding
6245 qualifier on the element type. */
6247 ttl = strip_array_types (ttl);
6249 /* Assignments between atomic and non-atomic objects are OK. */
6250 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6251 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6253 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6254 OPT_Wdiscarded_qualifiers,
6255 G_("passing argument %d of %qE discards "
6256 "%qv qualifier from pointer target type"),
6257 G_("assignment discards %qv qualifier "
6258 "from pointer target type"),
6259 G_("initialization discards %qv qualifier "
6260 "from pointer target type"),
6261 G_("return discards %qv qualifier from "
6262 "pointer target type"),
6263 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6265 /* If this is not a case of ignoring a mismatch in signedness,
6267 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6270 /* If there is a mismatch, do warn. */
6271 else if (warn_pointer_sign)
6272 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6273 G_("pointer targets in passing argument "
6274 "%d of %qE differ in signedness"),
6275 G_("pointer targets in assignment "
6276 "differ in signedness"),
6277 G_("pointer targets in initialization "
6278 "differ in signedness"),
6279 G_("pointer targets in return differ "
6282 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6283 && TREE_CODE (ttr) == FUNCTION_TYPE)
6285 /* Because const and volatile on functions are restrictions
6286 that say the function will not do certain things,
6287 it is okay to use a const or volatile function
6288 where an ordinary one is wanted, but not vice-versa. */
6289 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6290 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6291 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6292 OPT_Wdiscarded_qualifiers,
6293 G_("passing argument %d of %qE makes "
6294 "%q#v qualified function pointer "
6295 "from unqualified"),
6296 G_("assignment makes %q#v qualified function "
6297 "pointer from unqualified"),
6298 G_("initialization makes %q#v qualified "
6299 "function pointer from unqualified"),
6300 G_("return makes %q#v qualified function "
6301 "pointer from unqualified"),
6302 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6306 /* Avoid warning about the volatile ObjC EH puts on decls. */
6308 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6309 OPT_Wincompatible_pointer_types,
6310 G_("passing argument %d of %qE from "
6311 "incompatible pointer type"),
6312 G_("assignment from incompatible pointer type"),
6313 G_("initialization from incompatible "
6315 G_("return from incompatible pointer type"));
6317 return convert (type, rhs);
6319 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6321 /* ??? This should not be an error when inlining calls to
6322 unprototyped functions. */
6323 error_at (location, "invalid use of non-lvalue array");
6324 return error_mark_node;
6326 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6328 /* An explicit constant 0 can convert to a pointer,
6329 or one that results from arithmetic, even including
6330 a cast to integer type. */
6331 if (!null_pointer_constant)
6332 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6333 OPT_Wint_conversion,
6334 G_("passing argument %d of %qE makes "
6335 "pointer from integer without a cast"),
6336 G_("assignment makes pointer from integer "
6338 G_("initialization makes pointer from "
6339 "integer without a cast"),
6340 G_("return makes pointer from integer "
6343 return convert (type, rhs);
6345 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6347 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6348 OPT_Wint_conversion,
6349 G_("passing argument %d of %qE makes integer "
6350 "from pointer without a cast"),
6351 G_("assignment makes integer from pointer "
6353 G_("initialization makes integer from pointer "
6355 G_("return makes integer from pointer "
6357 return convert (type, rhs);
6359 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6362 bool save = in_late_binary_op;
6363 in_late_binary_op = true;
6364 ret = convert (type, rhs);
6365 in_late_binary_op = save;
6372 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6374 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6375 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6376 "expected %qT but argument is of type %qT", type, rhstype);
6379 error_at (location, "incompatible types when assigning to type %qT from "
6380 "type %qT", type, rhstype);
6384 "incompatible types when initializing type %qT using type %qT",
6389 "incompatible types when returning type %qT but %qT was "
6390 "expected", rhstype, type);
6396 return error_mark_node;
6399 /* If VALUE is a compound expr all of whose expressions are constant, then
6400 return its value. Otherwise, return error_mark_node.
6402 This is for handling COMPOUND_EXPRs as initializer elements
6403 which is allowed with a warning when -pedantic is specified. */
6406 valid_compound_expr_initializer (tree value, tree endtype)
6408 if (TREE_CODE (value) == COMPOUND_EXPR)
6410 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6412 return error_mark_node;
6413 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6416 else if (!initializer_constant_valid_p (value, endtype))
6417 return error_mark_node;
6422 /* Perform appropriate conversions on the initial value of a variable,
6423 store it in the declaration DECL,
6424 and print any error messages that are appropriate.
6425 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6426 If the init is invalid, store an ERROR_MARK.
6428 INIT_LOC is the location of the initial value. */
6431 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6436 /* If variable's type was invalidly declared, just ignore it. */
6438 type = TREE_TYPE (decl);
6439 if (TREE_CODE (type) == ERROR_MARK)
6442 /* Digest the specified initializer into an expression. */
6445 npc = null_pointer_constant_p (init);
6446 value = digest_init (init_loc, type, init, origtype, npc,
6447 true, TREE_STATIC (decl));
6449 /* Store the expression if valid; else report error. */
6451 if (!in_system_header_at (input_location)
6452 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6453 warning (OPT_Wtraditional, "traditional C rejects automatic "
6454 "aggregate initialization");
6456 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6457 DECL_INITIAL (decl) = value;
6459 /* ANSI wants warnings about out-of-range constant initializers. */
6460 STRIP_TYPE_NOPS (value);
6461 if (TREE_STATIC (decl))
6462 constant_expression_warning (value);
6464 /* Check if we need to set array size from compound literal size. */
6465 if (TREE_CODE (type) == ARRAY_TYPE
6466 && TYPE_DOMAIN (type) == 0
6467 && value != error_mark_node)
6469 tree inside_init = init;
6471 STRIP_TYPE_NOPS (inside_init);
6472 inside_init = fold (inside_init);
6474 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6476 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6478 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6480 /* For int foo[] = (int [3]){1}; we need to set array size
6481 now since later on array initializer will be just the
6482 brace enclosed list of the compound literal. */
6483 tree etype = strip_array_types (TREE_TYPE (decl));
6484 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6485 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6487 layout_decl (cldecl, 0);
6489 = c_build_qualified_type (type, TYPE_QUALS (etype));
6495 /* Methods for storing and printing names for error messages. */
6497 /* Implement a spelling stack that allows components of a name to be pushed
6498 and popped. Each element on the stack is this structure. */
6505 unsigned HOST_WIDE_INT i;
6510 #define SPELLING_STRING 1
6511 #define SPELLING_MEMBER 2
6512 #define SPELLING_BOUNDS 3
6514 static struct spelling *spelling; /* Next stack element (unused). */
6515 static struct spelling *spelling_base; /* Spelling stack base. */
6516 static int spelling_size; /* Size of the spelling stack. */
6518 /* Macros to save and restore the spelling stack around push_... functions.
6519 Alternative to SAVE_SPELLING_STACK. */
6521 #define SPELLING_DEPTH() (spelling - spelling_base)
6522 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6524 /* Push an element on the spelling stack with type KIND and assign VALUE
6527 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6529 int depth = SPELLING_DEPTH (); \
6531 if (depth >= spelling_size) \
6533 spelling_size += 10; \
6534 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6536 RESTORE_SPELLING_DEPTH (depth); \
6539 spelling->kind = (KIND); \
6540 spelling->MEMBER = (VALUE); \
6544 /* Push STRING on the stack. Printed literally. */
6547 push_string (const char *string)
6549 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6552 /* Push a member name on the stack. Printed as '.' STRING. */
6555 push_member_name (tree decl)
6557 const char *const string
6559 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6560 : _("<anonymous>"));
6561 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6564 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6567 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6569 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6572 /* Compute the maximum size in bytes of the printed spelling. */
6575 spelling_length (void)
6580 for (p = spelling_base; p < spelling; p++)
6582 if (p->kind == SPELLING_BOUNDS)
6585 size += strlen (p->u.s) + 1;
6591 /* Print the spelling to BUFFER and return it. */
6594 print_spelling (char *buffer)
6599 for (p = spelling_base; p < spelling; p++)
6600 if (p->kind == SPELLING_BOUNDS)
6602 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6608 if (p->kind == SPELLING_MEMBER)
6610 for (s = p->u.s; (*d = *s++); d++)
6617 /* Digest the parser output INIT as an initializer for type TYPE.
6618 Return a C expression of type TYPE to represent the initial value.
6620 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6622 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6624 If INIT is a string constant, STRICT_STRING is true if it is
6625 unparenthesized or we should not warn here for it being parenthesized.
6626 For other types of INIT, STRICT_STRING is not used.
6628 INIT_LOC is the location of the INIT.
6630 REQUIRE_CONSTANT requests an error if non-constant initializers or
6631 elements are seen. */
6634 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6635 bool null_pointer_constant, bool strict_string,
6636 int require_constant)
6638 enum tree_code code = TREE_CODE (type);
6639 tree inside_init = init;
6640 tree semantic_type = NULL_TREE;
6641 bool maybe_const = true;
6643 if (type == error_mark_node
6645 || error_operand_p (init))
6646 return error_mark_node;
6648 STRIP_TYPE_NOPS (inside_init);
6650 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6652 semantic_type = TREE_TYPE (inside_init);
6653 inside_init = TREE_OPERAND (inside_init, 0);
6655 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6656 inside_init = decl_constant_value_for_optimization (inside_init);
6658 /* Initialization of an array of chars from a string constant
6659 optionally enclosed in braces. */
6661 if (code == ARRAY_TYPE && inside_init
6662 && TREE_CODE (inside_init) == STRING_CST)
6665 = (TYPE_ATOMIC (TREE_TYPE (type))
6666 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6668 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6669 /* Note that an array could be both an array of character type
6670 and an array of wchar_t if wchar_t is signed char or unsigned
6672 bool char_array = (typ1 == char_type_node
6673 || typ1 == signed_char_type_node
6674 || typ1 == unsigned_char_type_node);
6675 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6676 bool char16_array = !!comptypes (typ1, char16_type_node);
6677 bool char32_array = !!comptypes (typ1, char32_type_node);
6679 if (char_array || wchar_array || char16_array || char32_array)
6682 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6683 expr.value = inside_init;
6684 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6685 expr.original_type = NULL;
6686 maybe_warn_string_init (init_loc, type, expr);
6688 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6689 pedwarn_init (init_loc, OPT_Wpedantic,
6690 "initialization of a flexible array member");
6692 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6693 TYPE_MAIN_VARIANT (type)))
6698 if (typ2 != char_type_node)
6700 error_init (init_loc, "char-array initialized from wide "
6702 return error_mark_node;
6707 if (typ2 == char_type_node)
6709 error_init (init_loc, "wide character array initialized "
6710 "from non-wide string");
6711 return error_mark_node;
6713 else if (!comptypes(typ1, typ2))
6715 error_init (init_loc, "wide character array initialized "
6716 "from incompatible wide string");
6717 return error_mark_node;
6721 TREE_TYPE (inside_init) = type;
6722 if (TYPE_DOMAIN (type) != 0
6723 && TYPE_SIZE (type) != 0
6724 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6726 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6728 /* Subtract the size of a single (possibly wide) character
6729 because it's ok to ignore the terminating null char
6730 that is counted in the length of the constant. */
6731 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6733 - (TYPE_PRECISION (typ1)
6735 pedwarn_init (init_loc, 0,
6736 ("initializer-string for array of chars "
6738 else if (warn_cxx_compat
6739 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6740 warning_at (init_loc, OPT_Wc___compat,
6741 ("initializer-string for array chars "
6742 "is too long for C++"));
6747 else if (INTEGRAL_TYPE_P (typ1))
6749 error_init (init_loc, "array of inappropriate type initialized "
6750 "from string constant");
6751 return error_mark_node;
6755 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6756 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6757 below and handle as a constructor. */
6758 if (code == VECTOR_TYPE
6759 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6760 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6761 && TREE_CONSTANT (inside_init))
6763 if (TREE_CODE (inside_init) == VECTOR_CST
6764 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6765 TYPE_MAIN_VARIANT (type)))
6768 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6770 unsigned HOST_WIDE_INT ix;
6772 bool constant_p = true;
6774 /* Iterate through elements and check if all constructor
6775 elements are *_CSTs. */
6776 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6777 if (!CONSTANT_CLASS_P (value))
6784 return build_vector_from_ctor (type,
6785 CONSTRUCTOR_ELTS (inside_init));
6789 if (warn_sequence_point)
6790 verify_sequence_points (inside_init);
6792 /* Any type can be initialized
6793 from an expression of the same type, optionally with braces. */
6795 if (inside_init && TREE_TYPE (inside_init) != 0
6796 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6797 TYPE_MAIN_VARIANT (type))
6798 || (code == ARRAY_TYPE
6799 && comptypes (TREE_TYPE (inside_init), type))
6800 || (code == VECTOR_TYPE
6801 && comptypes (TREE_TYPE (inside_init), type))
6802 || (code == POINTER_TYPE
6803 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6804 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6805 TREE_TYPE (type)))))
6807 if (code == POINTER_TYPE)
6809 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6811 if (TREE_CODE (inside_init) == STRING_CST
6812 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6813 inside_init = array_to_pointer_conversion
6814 (init_loc, inside_init);
6817 error_init (init_loc, "invalid use of non-lvalue array");
6818 return error_mark_node;
6823 if (code == VECTOR_TYPE)
6824 /* Although the types are compatible, we may require a
6826 inside_init = convert (type, inside_init);
6828 if (require_constant
6829 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6831 /* As an extension, allow initializing objects with static storage
6832 duration with compound literals (which are then treated just as
6833 the brace enclosed list they contain). Also allow this for
6834 vectors, as we can only assign them with compound literals. */
6835 if (flag_isoc99 && code != VECTOR_TYPE)
6836 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6838 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6839 inside_init = DECL_INITIAL (decl);
6842 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6843 && TREE_CODE (inside_init) != CONSTRUCTOR)
6845 error_init (init_loc, "array initialized from non-constant array "
6847 return error_mark_node;
6850 /* Compound expressions can only occur here if -Wpedantic or
6851 -pedantic-errors is specified. In the later case, we always want
6852 an error. In the former case, we simply want a warning. */
6853 if (require_constant && pedantic
6854 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6857 = valid_compound_expr_initializer (inside_init,
6858 TREE_TYPE (inside_init));
6859 if (inside_init == error_mark_node)
6860 error_init (init_loc, "initializer element is not constant");
6862 pedwarn_init (init_loc, OPT_Wpedantic,
6863 "initializer element is not constant");
6864 if (flag_pedantic_errors)
6865 inside_init = error_mark_node;
6867 else if (require_constant
6868 && !initializer_constant_valid_p (inside_init,
6869 TREE_TYPE (inside_init)))
6871 error_init (init_loc, "initializer element is not constant");
6872 inside_init = error_mark_node;
6874 else if (require_constant && !maybe_const)
6875 pedwarn_init (init_loc, 0,
6876 "initializer element is not a constant expression");
6878 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6879 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6880 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6881 type, inside_init, origtype,
6882 ic_init, null_pointer_constant,
6883 NULL_TREE, NULL_TREE, 0);
6887 /* Handle scalar types, including conversions. */
6889 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6890 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6891 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6893 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6894 && (TREE_CODE (init) == STRING_CST
6895 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6896 inside_init = init = array_to_pointer_conversion (init_loc, init);
6898 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6901 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6902 inside_init, origtype, ic_init,
6903 null_pointer_constant, NULL_TREE, NULL_TREE,
6906 /* Check to see if we have already given an error message. */
6907 if (inside_init == error_mark_node)
6909 else if (require_constant && !TREE_CONSTANT (inside_init))
6911 error_init (init_loc, "initializer element is not constant");
6912 inside_init = error_mark_node;
6914 else if (require_constant
6915 && !initializer_constant_valid_p (inside_init,
6916 TREE_TYPE (inside_init)))
6918 error_init (init_loc, "initializer element is not computable at "
6920 inside_init = error_mark_node;
6922 else if (require_constant && !maybe_const)
6923 pedwarn_init (init_loc, 0,
6924 "initializer element is not a constant expression");
6929 /* Come here only for records and arrays. */
6931 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6933 error_init (init_loc, "variable-sized object may not be initialized");
6934 return error_mark_node;
6937 error_init (init_loc, "invalid initializer");
6938 return error_mark_node;
6941 /* Handle initializers that use braces. */
6943 /* Type of object we are accumulating a constructor for.
6944 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6945 static tree constructor_type;
6947 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6949 static tree constructor_fields;
6951 /* For an ARRAY_TYPE, this is the specified index
6952 at which to store the next element we get. */
6953 static tree constructor_index;
6955 /* For an ARRAY_TYPE, this is the maximum index. */
6956 static tree constructor_max_index;
6958 /* For a RECORD_TYPE, this is the first field not yet written out. */
6959 static tree constructor_unfilled_fields;
6961 /* For an ARRAY_TYPE, this is the index of the first element
6962 not yet written out. */
6963 static tree constructor_unfilled_index;
6965 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6966 This is so we can generate gaps between fields, when appropriate. */
6967 static tree constructor_bit_index;
6969 /* If we are saving up the elements rather than allocating them,
6970 this is the list of elements so far (in reverse order,
6971 most recent first). */
6972 static vec<constructor_elt, va_gc> *constructor_elements;
6974 /* 1 if constructor should be incrementally stored into a constructor chain,
6975 0 if all the elements should be kept in AVL tree. */
6976 static int constructor_incremental;
6978 /* 1 if so far this constructor's elements are all compile-time constants. */
6979 static int constructor_constant;
6981 /* 1 if so far this constructor's elements are all valid address constants. */
6982 static int constructor_simple;
6984 /* 1 if this constructor has an element that cannot be part of a
6985 constant expression. */
6986 static int constructor_nonconst;
6988 /* 1 if this constructor is erroneous so far. */
6989 static int constructor_erroneous;
6991 /* 1 if this constructor is the universal zero initializer { 0 }. */
6992 static int constructor_zeroinit;
6994 /* Structure for managing pending initializer elements, organized as an
6999 struct init_node *left, *right;
7000 struct init_node *parent;
7007 /* Tree of pending elements at this constructor level.
7008 These are elements encountered out of order
7009 which belong at places we haven't reached yet in actually
7011 Will never hold tree nodes across GC runs. */
7012 static struct init_node *constructor_pending_elts;
7014 /* The SPELLING_DEPTH of this constructor. */
7015 static int constructor_depth;
7017 /* DECL node for which an initializer is being read.
7018 0 means we are reading a constructor expression
7019 such as (struct foo) {...}. */
7020 static tree constructor_decl;
7022 /* Nonzero if this is an initializer for a top-level decl. */
7023 static int constructor_top_level;
7025 /* Nonzero if there were any member designators in this initializer. */
7026 static int constructor_designated;
7028 /* Nesting depth of designator list. */
7029 static int designator_depth;
7031 /* Nonzero if there were diagnosed errors in this designator list. */
7032 static int designator_erroneous;
7035 /* This stack has a level for each implicit or explicit level of
7036 structuring in the initializer, including the outermost one. It
7037 saves the values of most of the variables above. */
7039 struct constructor_range_stack;
7041 struct constructor_stack
7043 struct constructor_stack *next;
7048 tree unfilled_index;
7049 tree unfilled_fields;
7051 vec<constructor_elt, va_gc> *elements;
7052 struct init_node *pending_elts;
7055 /* If value nonzero, this value should replace the entire
7056 constructor at this level. */
7057 struct c_expr replacement_value;
7058 struct constructor_range_stack *range_stack;
7067 int designator_depth;
7070 static struct constructor_stack *constructor_stack;
7072 /* This stack represents designators from some range designator up to
7073 the last designator in the list. */
7075 struct constructor_range_stack
7077 struct constructor_range_stack *next, *prev;
7078 struct constructor_stack *stack;
7085 static struct constructor_range_stack *constructor_range_stack;
7087 /* This stack records separate initializers that are nested.
7088 Nested initializers can't happen in ANSI C, but GNU C allows them
7089 in cases like { ... (struct foo) { ... } ... }. */
7091 struct initializer_stack
7093 struct initializer_stack *next;
7095 struct constructor_stack *constructor_stack;
7096 struct constructor_range_stack *constructor_range_stack;
7097 vec<constructor_elt, va_gc> *elements;
7098 struct spelling *spelling;
7099 struct spelling *spelling_base;
7102 char require_constant_value;
7103 char require_constant_elements;
7106 static struct initializer_stack *initializer_stack;
7108 /* Prepare to parse and output the initializer for variable DECL. */
7111 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7114 struct initializer_stack *p = XNEW (struct initializer_stack);
7116 p->decl = constructor_decl;
7117 p->require_constant_value = require_constant_value;
7118 p->require_constant_elements = require_constant_elements;
7119 p->constructor_stack = constructor_stack;
7120 p->constructor_range_stack = constructor_range_stack;
7121 p->elements = constructor_elements;
7122 p->spelling = spelling;
7123 p->spelling_base = spelling_base;
7124 p->spelling_size = spelling_size;
7125 p->top_level = constructor_top_level;
7126 p->next = initializer_stack;
7127 initializer_stack = p;
7129 constructor_decl = decl;
7130 constructor_designated = 0;
7131 constructor_top_level = top_level;
7133 if (decl != 0 && decl != error_mark_node)
7135 require_constant_value = TREE_STATIC (decl);
7136 require_constant_elements
7137 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7138 /* For a scalar, you can always use any value to initialize,
7139 even within braces. */
7140 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7141 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7142 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7143 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
7144 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7148 require_constant_value = 0;
7149 require_constant_elements = 0;
7150 locus = _("(anonymous)");
7153 constructor_stack = 0;
7154 constructor_range_stack = 0;
7156 found_missing_braces = 0;
7160 RESTORE_SPELLING_DEPTH (0);
7163 push_string (locus);
7169 struct initializer_stack *p = initializer_stack;
7171 /* Free the whole constructor stack of this initializer. */
7172 while (constructor_stack)
7174 struct constructor_stack *q = constructor_stack;
7175 constructor_stack = q->next;
7179 gcc_assert (!constructor_range_stack);
7181 /* Pop back to the data of the outer initializer (if any). */
7182 free (spelling_base);
7184 constructor_decl = p->decl;
7185 require_constant_value = p->require_constant_value;
7186 require_constant_elements = p->require_constant_elements;
7187 constructor_stack = p->constructor_stack;
7188 constructor_range_stack = p->constructor_range_stack;
7189 constructor_elements = p->elements;
7190 spelling = p->spelling;
7191 spelling_base = p->spelling_base;
7192 spelling_size = p->spelling_size;
7193 constructor_top_level = p->top_level;
7194 initializer_stack = p->next;
7198 /* Call here when we see the initializer is surrounded by braces.
7199 This is instead of a call to push_init_level;
7200 it is matched by a call to pop_init_level.
7202 TYPE is the type to initialize, for a constructor expression.
7203 For an initializer for a decl, TYPE is zero. */
7206 really_start_incremental_init (tree type)
7208 struct constructor_stack *p = XNEW (struct constructor_stack);
7211 type = TREE_TYPE (constructor_decl);
7213 if (TREE_CODE (type) == VECTOR_TYPE
7214 && TYPE_VECTOR_OPAQUE (type))
7215 error ("opaque vector types cannot be initialized");
7217 p->type = constructor_type;
7218 p->fields = constructor_fields;
7219 p->index = constructor_index;
7220 p->max_index = constructor_max_index;
7221 p->unfilled_index = constructor_unfilled_index;
7222 p->unfilled_fields = constructor_unfilled_fields;
7223 p->bit_index = constructor_bit_index;
7224 p->elements = constructor_elements;
7225 p->constant = constructor_constant;
7226 p->simple = constructor_simple;
7227 p->nonconst = constructor_nonconst;
7228 p->erroneous = constructor_erroneous;
7229 p->pending_elts = constructor_pending_elts;
7230 p->depth = constructor_depth;
7231 p->replacement_value.value = 0;
7232 p->replacement_value.original_code = ERROR_MARK;
7233 p->replacement_value.original_type = NULL;
7237 p->incremental = constructor_incremental;
7238 p->designated = constructor_designated;
7239 p->designator_depth = designator_depth;
7241 constructor_stack = p;
7243 constructor_constant = 1;
7244 constructor_simple = 1;
7245 constructor_nonconst = 0;
7246 constructor_depth = SPELLING_DEPTH ();
7247 constructor_elements = NULL;
7248 constructor_pending_elts = 0;
7249 constructor_type = type;
7250 constructor_incremental = 1;
7251 constructor_designated = 0;
7252 constructor_zeroinit = 1;
7253 designator_depth = 0;
7254 designator_erroneous = 0;
7256 if (TREE_CODE (constructor_type) == RECORD_TYPE
7257 || TREE_CODE (constructor_type) == UNION_TYPE)
7259 constructor_fields = TYPE_FIELDS (constructor_type);
7260 /* Skip any nameless bit fields at the beginning. */
7261 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7262 && DECL_NAME (constructor_fields) == 0)
7263 constructor_fields = DECL_CHAIN (constructor_fields);
7265 constructor_unfilled_fields = constructor_fields;
7266 constructor_bit_index = bitsize_zero_node;
7268 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7270 if (TYPE_DOMAIN (constructor_type))
7272 constructor_max_index
7273 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7275 /* Detect non-empty initializations of zero-length arrays. */
7276 if (constructor_max_index == NULL_TREE
7277 && TYPE_SIZE (constructor_type))
7278 constructor_max_index = integer_minus_one_node;
7280 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7281 to initialize VLAs will cause a proper error; avoid tree
7282 checking errors as well by setting a safe value. */
7283 if (constructor_max_index
7284 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7285 constructor_max_index = integer_minus_one_node;
7288 = convert (bitsizetype,
7289 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7293 constructor_index = bitsize_zero_node;
7294 constructor_max_index = NULL_TREE;
7297 constructor_unfilled_index = constructor_index;
7299 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7301 /* Vectors are like simple fixed-size arrays. */
7302 constructor_max_index =
7303 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7304 constructor_index = bitsize_zero_node;
7305 constructor_unfilled_index = constructor_index;
7309 /* Handle the case of int x = {5}; */
7310 constructor_fields = constructor_type;
7311 constructor_unfilled_fields = constructor_type;
7315 /* Called when we see an open brace for a nested initializer. Finish
7316 off any pending levels with implicit braces. */
7318 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7320 while (constructor_stack->implicit)
7322 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7323 || TREE_CODE (constructor_type) == UNION_TYPE)
7324 && constructor_fields == 0)
7325 process_init_element (input_location,
7326 pop_init_level (loc, 1, braced_init_obstack),
7327 true, braced_init_obstack);
7328 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7329 && constructor_max_index
7330 && tree_int_cst_lt (constructor_max_index,
7332 process_init_element (input_location,
7333 pop_init_level (loc, 1, braced_init_obstack),
7334 true, braced_init_obstack);
7340 /* Push down into a subobject, for initialization.
7341 If this is for an explicit set of braces, IMPLICIT is 0.
7342 If it is because the next element belongs at a lower level,
7343 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7346 push_init_level (location_t loc, int implicit,
7347 struct obstack *braced_init_obstack)
7349 struct constructor_stack *p;
7350 tree value = NULL_TREE;
7352 /* Unless this is an explicit brace, we need to preserve previous
7356 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7357 || TREE_CODE (constructor_type) == UNION_TYPE)
7358 && constructor_fields)
7359 value = find_init_member (constructor_fields, braced_init_obstack);
7360 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7361 value = find_init_member (constructor_index, braced_init_obstack);
7364 p = XNEW (struct constructor_stack);
7365 p->type = constructor_type;
7366 p->fields = constructor_fields;
7367 p->index = constructor_index;
7368 p->max_index = constructor_max_index;
7369 p->unfilled_index = constructor_unfilled_index;
7370 p->unfilled_fields = constructor_unfilled_fields;
7371 p->bit_index = constructor_bit_index;
7372 p->elements = constructor_elements;
7373 p->constant = constructor_constant;
7374 p->simple = constructor_simple;
7375 p->nonconst = constructor_nonconst;
7376 p->erroneous = constructor_erroneous;
7377 p->pending_elts = constructor_pending_elts;
7378 p->depth = constructor_depth;
7379 p->replacement_value.value = 0;
7380 p->replacement_value.original_code = ERROR_MARK;
7381 p->replacement_value.original_type = NULL;
7382 p->implicit = implicit;
7384 p->incremental = constructor_incremental;
7385 p->designated = constructor_designated;
7386 p->designator_depth = designator_depth;
7387 p->next = constructor_stack;
7389 constructor_stack = p;
7391 constructor_constant = 1;
7392 constructor_simple = 1;
7393 constructor_nonconst = 0;
7394 constructor_depth = SPELLING_DEPTH ();
7395 constructor_elements = NULL;
7396 constructor_incremental = 1;
7397 constructor_designated = 0;
7398 constructor_pending_elts = 0;
7401 p->range_stack = constructor_range_stack;
7402 constructor_range_stack = 0;
7403 designator_depth = 0;
7404 designator_erroneous = 0;
7407 /* Don't die if an entire brace-pair level is superfluous
7408 in the containing level. */
7409 if (constructor_type == 0)
7411 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7412 || TREE_CODE (constructor_type) == UNION_TYPE)
7414 /* Don't die if there are extra init elts at the end. */
7415 if (constructor_fields == 0)
7416 constructor_type = 0;
7419 constructor_type = TREE_TYPE (constructor_fields);
7420 push_member_name (constructor_fields);
7421 constructor_depth++;
7423 /* If upper initializer is designated, then mark this as
7424 designated too to prevent bogus warnings. */
7425 constructor_designated = p->designated;
7427 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7429 constructor_type = TREE_TYPE (constructor_type);
7430 push_array_bounds (tree_to_uhwi (constructor_index));
7431 constructor_depth++;
7434 if (constructor_type == 0)
7436 error_init (loc, "extra brace group at end of initializer");
7437 constructor_fields = 0;
7438 constructor_unfilled_fields = 0;
7442 if (value && TREE_CODE (value) == CONSTRUCTOR)
7444 constructor_constant = TREE_CONSTANT (value);
7445 constructor_simple = TREE_STATIC (value);
7446 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7447 constructor_elements = CONSTRUCTOR_ELTS (value);
7448 if (!vec_safe_is_empty (constructor_elements)
7449 && (TREE_CODE (constructor_type) == RECORD_TYPE
7450 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7451 set_nonincremental_init (braced_init_obstack);
7455 found_missing_braces = 1;
7457 if (TREE_CODE (constructor_type) == RECORD_TYPE
7458 || TREE_CODE (constructor_type) == UNION_TYPE)
7460 constructor_fields = TYPE_FIELDS (constructor_type);
7461 /* Skip any nameless bit fields at the beginning. */
7462 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7463 && DECL_NAME (constructor_fields) == 0)
7464 constructor_fields = DECL_CHAIN (constructor_fields);
7466 constructor_unfilled_fields = constructor_fields;
7467 constructor_bit_index = bitsize_zero_node;
7469 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7471 /* Vectors are like simple fixed-size arrays. */
7472 constructor_max_index =
7473 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7474 constructor_index = bitsize_int (0);
7475 constructor_unfilled_index = constructor_index;
7477 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7479 if (TYPE_DOMAIN (constructor_type))
7481 constructor_max_index
7482 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7484 /* Detect non-empty initializations of zero-length arrays. */
7485 if (constructor_max_index == NULL_TREE
7486 && TYPE_SIZE (constructor_type))
7487 constructor_max_index = integer_minus_one_node;
7489 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7490 to initialize VLAs will cause a proper error; avoid tree
7491 checking errors as well by setting a safe value. */
7492 if (constructor_max_index
7493 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7494 constructor_max_index = integer_minus_one_node;
7497 = convert (bitsizetype,
7498 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7501 constructor_index = bitsize_zero_node;
7503 constructor_unfilled_index = constructor_index;
7504 if (value && TREE_CODE (value) == STRING_CST)
7506 /* We need to split the char/wchar array into individual
7507 characters, so that we don't have to special case it
7509 set_nonincremental_init_from_string (value, braced_init_obstack);
7514 if (constructor_type != error_mark_node)
7515 warning_init (input_location, 0, "braces around scalar initializer");
7516 constructor_fields = constructor_type;
7517 constructor_unfilled_fields = constructor_type;
7521 /* At the end of an implicit or explicit brace level,
7522 finish up that level of constructor. If a single expression
7523 with redundant braces initialized that level, return the
7524 c_expr structure for that expression. Otherwise, the original_code
7525 element is set to ERROR_MARK.
7526 If we were outputting the elements as they are read, return 0 as the value
7527 from inner levels (process_init_element ignores that),
7528 but return error_mark_node as the value from the outermost level
7529 (that's what we want to put in DECL_INITIAL).
7530 Otherwise, return a CONSTRUCTOR expression as the value. */
7533 pop_init_level (location_t loc, int implicit,
7534 struct obstack *braced_init_obstack)
7536 struct constructor_stack *p;
7539 ret.original_code = ERROR_MARK;
7540 ret.original_type = NULL;
7544 /* When we come to an explicit close brace,
7545 pop any inner levels that didn't have explicit braces. */
7546 while (constructor_stack->implicit)
7547 process_init_element (input_location,
7548 pop_init_level (loc, 1, braced_init_obstack),
7549 true, braced_init_obstack);
7550 gcc_assert (!constructor_range_stack);
7553 /* Now output all pending elements. */
7554 constructor_incremental = 1;
7555 output_pending_init_elements (1, braced_init_obstack);
7557 p = constructor_stack;
7559 /* Error for initializing a flexible array member, or a zero-length
7560 array member in an inappropriate context. */
7561 if (constructor_type && constructor_fields
7562 && TREE_CODE (constructor_type) == ARRAY_TYPE
7563 && TYPE_DOMAIN (constructor_type)
7564 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7566 /* Silently discard empty initializations. The parser will
7567 already have pedwarned for empty brackets. */
7568 if (integer_zerop (constructor_unfilled_index))
7569 constructor_type = NULL_TREE;
7572 gcc_assert (!TYPE_SIZE (constructor_type));
7574 if (constructor_depth > 2)
7575 error_init (loc, "initialization of flexible array member in a nested context");
7577 pedwarn_init (loc, OPT_Wpedantic,
7578 "initialization of a flexible array member");
7580 /* We have already issued an error message for the existence
7581 of a flexible array member not at the end of the structure.
7582 Discard the initializer so that we do not die later. */
7583 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7584 constructor_type = NULL_TREE;
7588 switch (vec_safe_length (constructor_elements))
7591 /* Initialization with { } counts as zeroinit. */
7592 constructor_zeroinit = 1;
7595 /* This might be zeroinit as well. */
7596 if (integer_zerop ((*constructor_elements)[0].value))
7597 constructor_zeroinit = 1;
7600 /* If the constructor has more than one element, it can't be { 0 }. */
7601 constructor_zeroinit = 0;
7605 /* Warn when some structs are initialized with direct aggregation. */
7606 if (!implicit && found_missing_braces && warn_missing_braces
7607 && !constructor_zeroinit)
7608 warning_init (loc, OPT_Wmissing_braces,
7609 "missing braces around initializer");
7611 /* Warn when some struct elements are implicitly initialized to zero. */
7612 if (warn_missing_field_initializers
7614 && TREE_CODE (constructor_type) == RECORD_TYPE
7615 && constructor_unfilled_fields)
7617 /* Do not warn for flexible array members or zero-length arrays. */
7618 while (constructor_unfilled_fields
7619 && (!DECL_SIZE (constructor_unfilled_fields)
7620 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7621 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7623 if (constructor_unfilled_fields
7624 /* Do not warn if this level of the initializer uses member
7625 designators; it is likely to be deliberate. */
7626 && !constructor_designated
7627 /* Do not warn about initializing with { 0 } or with { }. */
7628 && !constructor_zeroinit)
7630 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7631 "missing initializer for field %qD of %qT",
7632 constructor_unfilled_fields,
7634 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7635 "%qD declared here", constructor_unfilled_fields);
7639 /* Pad out the end of the structure. */
7640 if (p->replacement_value.value)
7641 /* If this closes a superfluous brace pair,
7642 just pass out the element between them. */
7643 ret = p->replacement_value;
7644 else if (constructor_type == 0)
7646 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7647 && TREE_CODE (constructor_type) != UNION_TYPE
7648 && TREE_CODE (constructor_type) != ARRAY_TYPE
7649 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7651 /* A nonincremental scalar initializer--just return
7652 the element, after verifying there is just one. */
7653 if (vec_safe_is_empty (constructor_elements))
7655 if (!constructor_erroneous)
7656 error_init (loc, "empty scalar initializer");
7657 ret.value = error_mark_node;
7659 else if (vec_safe_length (constructor_elements) != 1)
7661 error_init (loc, "extra elements in scalar initializer");
7662 ret.value = (*constructor_elements)[0].value;
7665 ret.value = (*constructor_elements)[0].value;
7669 if (constructor_erroneous)
7670 ret.value = error_mark_node;
7673 ret.value = build_constructor (constructor_type,
7674 constructor_elements);
7675 if (constructor_constant)
7676 TREE_CONSTANT (ret.value) = 1;
7677 if (constructor_constant && constructor_simple)
7678 TREE_STATIC (ret.value) = 1;
7679 if (constructor_nonconst)
7680 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7684 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7686 if (constructor_nonconst)
7687 ret.original_code = C_MAYBE_CONST_EXPR;
7688 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7689 ret.original_code = ERROR_MARK;
7692 constructor_type = p->type;
7693 constructor_fields = p->fields;
7694 constructor_index = p->index;
7695 constructor_max_index = p->max_index;
7696 constructor_unfilled_index = p->unfilled_index;
7697 constructor_unfilled_fields = p->unfilled_fields;
7698 constructor_bit_index = p->bit_index;
7699 constructor_elements = p->elements;
7700 constructor_constant = p->constant;
7701 constructor_simple = p->simple;
7702 constructor_nonconst = p->nonconst;
7703 constructor_erroneous = p->erroneous;
7704 constructor_incremental = p->incremental;
7705 constructor_designated = p->designated;
7706 designator_depth = p->designator_depth;
7707 constructor_pending_elts = p->pending_elts;
7708 constructor_depth = p->depth;
7710 constructor_range_stack = p->range_stack;
7711 RESTORE_SPELLING_DEPTH (constructor_depth);
7713 constructor_stack = p->next;
7716 if (ret.value == 0 && constructor_stack == 0)
7717 ret.value = error_mark_node;
7721 /* Common handling for both array range and field name designators.
7722 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7725 set_designator (location_t loc, int array,
7726 struct obstack *braced_init_obstack)
7729 enum tree_code subcode;
7731 /* Don't die if an entire brace-pair level is superfluous
7732 in the containing level. */
7733 if (constructor_type == 0)
7736 /* If there were errors in this designator list already, bail out
7738 if (designator_erroneous)
7741 if (!designator_depth)
7743 gcc_assert (!constructor_range_stack);
7745 /* Designator list starts at the level of closest explicit
7747 while (constructor_stack->implicit)
7748 process_init_element (input_location,
7749 pop_init_level (loc, 1, braced_init_obstack),
7750 true, braced_init_obstack);
7751 constructor_designated = 1;
7755 switch (TREE_CODE (constructor_type))
7759 subtype = TREE_TYPE (constructor_fields);
7760 if (subtype != error_mark_node)
7761 subtype = TYPE_MAIN_VARIANT (subtype);
7764 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7770 subcode = TREE_CODE (subtype);
7771 if (array && subcode != ARRAY_TYPE)
7773 error_init (loc, "array index in non-array initializer");
7776 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7778 error_init (loc, "field name not in record or union initializer");
7782 constructor_designated = 1;
7783 finish_implicit_inits (loc, braced_init_obstack);
7784 push_init_level (loc, 2, braced_init_obstack);
7788 /* If there are range designators in designator list, push a new designator
7789 to constructor_range_stack. RANGE_END is end of such stack range or
7790 NULL_TREE if there is no range designator at this level. */
7793 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7795 struct constructor_range_stack *p;
7797 p = (struct constructor_range_stack *)
7798 obstack_alloc (braced_init_obstack,
7799 sizeof (struct constructor_range_stack));
7800 p->prev = constructor_range_stack;
7802 p->fields = constructor_fields;
7803 p->range_start = constructor_index;
7804 p->index = constructor_index;
7805 p->stack = constructor_stack;
7806 p->range_end = range_end;
7807 if (constructor_range_stack)
7808 constructor_range_stack->next = p;
7809 constructor_range_stack = p;
7812 /* Within an array initializer, specify the next index to be initialized.
7813 FIRST is that index. If LAST is nonzero, then initialize a range
7814 of indices, running from FIRST through LAST. */
7817 set_init_index (location_t loc, tree first, tree last,
7818 struct obstack *braced_init_obstack)
7820 if (set_designator (loc, 1, braced_init_obstack))
7823 designator_erroneous = 1;
7825 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7826 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7828 error_init (loc, "array index in initializer not of integer type");
7832 if (TREE_CODE (first) != INTEGER_CST)
7834 first = c_fully_fold (first, false, NULL);
7835 if (TREE_CODE (first) == INTEGER_CST)
7836 pedwarn_init (loc, OPT_Wpedantic,
7837 "array index in initializer is not "
7838 "an integer constant expression");
7841 if (last && TREE_CODE (last) != INTEGER_CST)
7843 last = c_fully_fold (last, false, NULL);
7844 if (TREE_CODE (last) == INTEGER_CST)
7845 pedwarn_init (loc, OPT_Wpedantic,
7846 "array index in initializer is not "
7847 "an integer constant expression");
7850 if (TREE_CODE (first) != INTEGER_CST)
7851 error_init (loc, "nonconstant array index in initializer");
7852 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7853 error_init (loc, "nonconstant array index in initializer");
7854 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7855 error_init (loc, "array index in non-array initializer");
7856 else if (tree_int_cst_sgn (first) == -1)
7857 error_init (loc, "array index in initializer exceeds array bounds");
7858 else if (constructor_max_index
7859 && tree_int_cst_lt (constructor_max_index, first))
7860 error_init (loc, "array index in initializer exceeds array bounds");
7863 constant_expression_warning (first);
7865 constant_expression_warning (last);
7866 constructor_index = convert (bitsizetype, first);
7867 if (tree_int_cst_lt (constructor_index, first))
7869 constructor_index = copy_node (constructor_index);
7870 TREE_OVERFLOW (constructor_index) = 1;
7875 if (tree_int_cst_equal (first, last))
7877 else if (tree_int_cst_lt (last, first))
7879 error_init (loc, "empty index range in initializer");
7884 last = convert (bitsizetype, last);
7885 if (constructor_max_index != 0
7886 && tree_int_cst_lt (constructor_max_index, last))
7888 error_init (loc, "array index range in initializer exceeds "
7896 designator_erroneous = 0;
7897 if (constructor_range_stack || last)
7898 push_range_stack (last, braced_init_obstack);
7902 /* Within a struct initializer, specify the next field to be initialized. */
7905 set_init_label (location_t loc, tree fieldname,
7906 struct obstack *braced_init_obstack)
7910 if (set_designator (loc, 0, braced_init_obstack))
7913 designator_erroneous = 1;
7915 if (TREE_CODE (constructor_type) != RECORD_TYPE
7916 && TREE_CODE (constructor_type) != UNION_TYPE)
7918 error_init (loc, "field name not in record or union initializer");
7922 field = lookup_field (constructor_type, fieldname);
7925 error ("unknown field %qE specified in initializer", fieldname);
7929 constructor_fields = TREE_VALUE (field);
7931 designator_erroneous = 0;
7932 if (constructor_range_stack)
7933 push_range_stack (NULL_TREE, braced_init_obstack);
7934 field = TREE_CHAIN (field);
7937 if (set_designator (loc, 0, braced_init_obstack))
7941 while (field != NULL_TREE);
7944 /* Add a new initializer to the tree of pending initializers. PURPOSE
7945 identifies the initializer, either array index or field in a structure.
7946 VALUE is the value of that index or field. If ORIGTYPE is not
7947 NULL_TREE, it is the original type of VALUE.
7949 IMPLICIT is true if value comes from pop_init_level (1),
7950 the new initializer has been merged with the existing one
7951 and thus no warnings should be emitted about overriding an
7952 existing initializer. */
7955 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7956 bool implicit, struct obstack *braced_init_obstack)
7958 struct init_node *p, **q, *r;
7960 q = &constructor_pending_elts;
7963 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7968 if (tree_int_cst_lt (purpose, p->purpose))
7970 else if (tree_int_cst_lt (p->purpose, purpose))
7976 if (TREE_SIDE_EFFECTS (p->value))
7977 warning_init (loc, 0,
7978 "initialized field with side-effects "
7980 else if (warn_override_init)
7981 warning_init (loc, OPT_Woverride_init,
7982 "initialized field overwritten");
7985 p->origtype = origtype;
7994 bitpos = bit_position (purpose);
7998 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8000 else if (p->purpose != purpose)
8006 if (TREE_SIDE_EFFECTS (p->value))
8007 warning_init (loc, 0,
8008 "initialized field with side-effects "
8010 else if (warn_override_init)
8011 warning_init (loc, OPT_Woverride_init,
8012 "initialized field overwritten");
8015 p->origtype = origtype;
8021 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8022 sizeof (struct init_node));
8023 r->purpose = purpose;
8025 r->origtype = origtype;
8035 struct init_node *s;
8039 if (p->balance == 0)
8041 else if (p->balance < 0)
8048 p->left->parent = p;
8065 constructor_pending_elts = r;
8070 struct init_node *t = r->right;
8074 r->right->parent = r;
8079 p->left->parent = p;
8082 p->balance = t->balance < 0;
8083 r->balance = -(t->balance > 0);
8098 constructor_pending_elts = t;
8104 /* p->balance == +1; growth of left side balances the node. */
8109 else /* r == p->right */
8111 if (p->balance == 0)
8112 /* Growth propagation from right side. */
8114 else if (p->balance > 0)
8121 p->right->parent = p;
8138 constructor_pending_elts = r;
8140 else /* r->balance == -1 */
8143 struct init_node *t = r->left;
8147 r->left->parent = r;
8152 p->right->parent = p;
8155 r->balance = (t->balance < 0);
8156 p->balance = -(t->balance > 0);
8171 constructor_pending_elts = t;
8177 /* p->balance == -1; growth of right side balances the node. */
8188 /* Build AVL tree from a sorted chain. */
8191 set_nonincremental_init (struct obstack * braced_init_obstack)
8193 unsigned HOST_WIDE_INT ix;
8196 if (TREE_CODE (constructor_type) != RECORD_TYPE
8197 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8200 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8201 add_pending_init (input_location, index, value, NULL_TREE, true,
8202 braced_init_obstack);
8203 constructor_elements = NULL;
8204 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8206 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8207 /* Skip any nameless bit fields at the beginning. */
8208 while (constructor_unfilled_fields != 0
8209 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8210 && DECL_NAME (constructor_unfilled_fields) == 0)
8211 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8214 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8216 if (TYPE_DOMAIN (constructor_type))
8217 constructor_unfilled_index
8218 = convert (bitsizetype,
8219 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8221 constructor_unfilled_index = bitsize_zero_node;
8223 constructor_incremental = 0;
8226 /* Build AVL tree from a string constant. */
8229 set_nonincremental_init_from_string (tree str,
8230 struct obstack * braced_init_obstack)
8232 tree value, purpose, type;
8233 HOST_WIDE_INT val[2];
8234 const char *p, *end;
8235 int byte, wchar_bytes, charwidth, bitpos;
8237 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8239 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8240 charwidth = TYPE_PRECISION (char_type_node);
8241 type = TREE_TYPE (constructor_type);
8242 p = TREE_STRING_POINTER (str);
8243 end = p + TREE_STRING_LENGTH (str);
8245 for (purpose = bitsize_zero_node;
8247 && !(constructor_max_index
8248 && tree_int_cst_lt (constructor_max_index, purpose));
8249 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8251 if (wchar_bytes == 1)
8253 val[0] = (unsigned char) *p++;
8260 for (byte = 0; byte < wchar_bytes; byte++)
8262 if (BYTES_BIG_ENDIAN)
8263 bitpos = (wchar_bytes - byte - 1) * charwidth;
8265 bitpos = byte * charwidth;
8266 val[bitpos % HOST_BITS_PER_WIDE_INT]
8267 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8268 << (bitpos % HOST_BITS_PER_WIDE_INT);
8272 if (!TYPE_UNSIGNED (type))
8274 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8275 if (bitpos < HOST_BITS_PER_WIDE_INT)
8277 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8279 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8283 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8288 else if (val[1] & (((HOST_WIDE_INT) 1)
8289 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8290 val[1] |= ((HOST_WIDE_INT) -1)
8291 << (bitpos - HOST_BITS_PER_WIDE_INT);
8294 value = wide_int_to_tree (type,
8295 wide_int::from_array (val, 2,
8296 HOST_BITS_PER_WIDE_INT * 2));
8297 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8298 braced_init_obstack);
8301 constructor_incremental = 0;
8304 /* Return value of FIELD in pending initializer or zero if the field was
8305 not initialized yet. */
8308 find_init_member (tree field, struct obstack * braced_init_obstack)
8310 struct init_node *p;
8312 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8314 if (constructor_incremental
8315 && tree_int_cst_lt (field, constructor_unfilled_index))
8316 set_nonincremental_init (braced_init_obstack);
8318 p = constructor_pending_elts;
8321 if (tree_int_cst_lt (field, p->purpose))
8323 else if (tree_int_cst_lt (p->purpose, field))
8329 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8331 tree bitpos = bit_position (field);
8333 if (constructor_incremental
8334 && (!constructor_unfilled_fields
8335 || tree_int_cst_lt (bitpos,
8336 bit_position (constructor_unfilled_fields))))
8337 set_nonincremental_init (braced_init_obstack);
8339 p = constructor_pending_elts;
8342 if (field == p->purpose)
8344 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8350 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8352 if (!vec_safe_is_empty (constructor_elements)
8353 && (constructor_elements->last ().index == field))
8354 return constructor_elements->last ().value;
8359 /* "Output" the next constructor element.
8360 At top level, really output it to assembler code now.
8361 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8362 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8363 TYPE is the data type that the containing data type wants here.
8364 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8365 If VALUE is a string constant, STRICT_STRING is true if it is
8366 unparenthesized or we should not warn here for it being parenthesized.
8367 For other types of VALUE, STRICT_STRING is not used.
8369 PENDING if non-nil means output pending elements that belong
8370 right after this element. (PENDING is normally 1;
8371 it is 0 while outputting pending elements, to avoid recursion.)
8373 IMPLICIT is true if value comes from pop_init_level (1),
8374 the new initializer has been merged with the existing one
8375 and thus no warnings should be emitted about overriding an
8376 existing initializer. */
8379 output_init_element (location_t loc, tree value, tree origtype,
8380 bool strict_string, tree type, tree field, int pending,
8381 bool implicit, struct obstack * braced_init_obstack)
8383 tree semantic_type = NULL_TREE;
8384 bool maybe_const = true;
8387 if (type == error_mark_node || value == error_mark_node)
8389 constructor_erroneous = 1;
8392 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8393 && (TREE_CODE (value) == STRING_CST
8394 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8395 && !(TREE_CODE (value) == STRING_CST
8396 && TREE_CODE (type) == ARRAY_TYPE
8397 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8398 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8399 TYPE_MAIN_VARIANT (type)))
8400 value = array_to_pointer_conversion (input_location, value);
8402 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8403 && require_constant_value && pending)
8405 /* As an extension, allow initializing objects with static storage
8406 duration with compound literals (which are then treated just as
8407 the brace enclosed list they contain). */
8409 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8411 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8412 value = DECL_INITIAL (decl);
8415 npc = null_pointer_constant_p (value);
8416 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8418 semantic_type = TREE_TYPE (value);
8419 value = TREE_OPERAND (value, 0);
8421 value = c_fully_fold (value, require_constant_value, &maybe_const);
8423 if (value == error_mark_node)
8424 constructor_erroneous = 1;
8425 else if (!TREE_CONSTANT (value))
8426 constructor_constant = 0;
8427 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8428 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8429 || TREE_CODE (constructor_type) == UNION_TYPE)
8430 && DECL_C_BIT_FIELD (field)
8431 && TREE_CODE (value) != INTEGER_CST))
8432 constructor_simple = 0;
8434 constructor_nonconst = 1;
8436 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8438 if (require_constant_value)
8440 error_init (loc, "initializer element is not constant");
8441 value = error_mark_node;
8443 else if (require_constant_elements)
8444 pedwarn (loc, OPT_Wpedantic,
8445 "initializer element is not computable at load time");
8447 else if (!maybe_const
8448 && (require_constant_value || require_constant_elements))
8449 pedwarn_init (loc, OPT_Wpedantic,
8450 "initializer element is not a constant expression");
8452 /* Issue -Wc++-compat warnings about initializing a bitfield with
8455 && field != NULL_TREE
8456 && TREE_CODE (field) == FIELD_DECL
8457 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8458 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8459 != TYPE_MAIN_VARIANT (type))
8460 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8462 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8463 if (checktype != error_mark_node
8464 && (TYPE_MAIN_VARIANT (checktype)
8465 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8466 warning_init (loc, OPT_Wc___compat,
8467 "enum conversion in initialization is invalid in C++");
8470 /* If this field is empty (and not at the end of structure),
8471 don't do anything other than checking the initializer. */
8473 && (TREE_TYPE (field) == error_mark_node
8474 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8475 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8476 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8477 || DECL_CHAIN (field)))))
8481 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8482 value = digest_init (loc, type, value, origtype, npc, strict_string,
8483 require_constant_value);
8484 if (value == error_mark_node)
8486 constructor_erroneous = 1;
8489 if (require_constant_value || require_constant_elements)
8490 constant_expression_warning (value);
8492 /* If this element doesn't come next in sequence,
8493 put it on constructor_pending_elts. */
8494 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8495 && (!constructor_incremental
8496 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8498 if (constructor_incremental
8499 && tree_int_cst_lt (field, constructor_unfilled_index))
8500 set_nonincremental_init (braced_init_obstack);
8502 add_pending_init (loc, field, value, origtype, implicit,
8503 braced_init_obstack);
8506 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8507 && (!constructor_incremental
8508 || field != constructor_unfilled_fields))
8510 /* We do this for records but not for unions. In a union,
8511 no matter which field is specified, it can be initialized
8512 right away since it starts at the beginning of the union. */
8513 if (constructor_incremental)
8515 if (!constructor_unfilled_fields)
8516 set_nonincremental_init (braced_init_obstack);
8519 tree bitpos, unfillpos;
8521 bitpos = bit_position (field);
8522 unfillpos = bit_position (constructor_unfilled_fields);
8524 if (tree_int_cst_lt (bitpos, unfillpos))
8525 set_nonincremental_init (braced_init_obstack);
8529 add_pending_init (loc, field, value, origtype, implicit,
8530 braced_init_obstack);
8533 else if (TREE_CODE (constructor_type) == UNION_TYPE
8534 && !vec_safe_is_empty (constructor_elements))
8538 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8539 warning_init (loc, 0,
8540 "initialized field with side-effects overwritten");
8541 else if (warn_override_init)
8542 warning_init (loc, OPT_Woverride_init,
8543 "initialized field overwritten");
8546 /* We can have just one union field set. */
8547 constructor_elements = NULL;
8550 /* Otherwise, output this element either to
8551 constructor_elements or to the assembler file. */
8553 constructor_elt celt = {field, value};
8554 vec_safe_push (constructor_elements, celt);
8556 /* Advance the variable that indicates sequential elements output. */
8557 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8558 constructor_unfilled_index
8559 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8561 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8563 constructor_unfilled_fields
8564 = DECL_CHAIN (constructor_unfilled_fields);
8566 /* Skip any nameless bit fields. */
8567 while (constructor_unfilled_fields != 0
8568 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8569 && DECL_NAME (constructor_unfilled_fields) == 0)
8570 constructor_unfilled_fields =
8571 DECL_CHAIN (constructor_unfilled_fields);
8573 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8574 constructor_unfilled_fields = 0;
8576 /* Now output any pending elements which have become next. */
8578 output_pending_init_elements (0, braced_init_obstack);
8581 /* Output any pending elements which have become next.
8582 As we output elements, constructor_unfilled_{fields,index}
8583 advances, which may cause other elements to become next;
8584 if so, they too are output.
8586 If ALL is 0, we return when there are
8587 no more pending elements to output now.
8589 If ALL is 1, we output space as necessary so that
8590 we can output all the pending elements. */
8592 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8594 struct init_node *elt = constructor_pending_elts;
8599 /* Look through the whole pending tree.
8600 If we find an element that should be output now,
8601 output it. Otherwise, set NEXT to the element
8602 that comes first among those still pending. */
8607 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8609 if (tree_int_cst_equal (elt->purpose,
8610 constructor_unfilled_index))
8611 output_init_element (input_location, elt->value, elt->origtype,
8612 true, TREE_TYPE (constructor_type),
8613 constructor_unfilled_index, 0, false,
8614 braced_init_obstack);
8615 else if (tree_int_cst_lt (constructor_unfilled_index,
8618 /* Advance to the next smaller node. */
8623 /* We have reached the smallest node bigger than the
8624 current unfilled index. Fill the space first. */
8625 next = elt->purpose;
8631 /* Advance to the next bigger node. */
8636 /* We have reached the biggest node in a subtree. Find
8637 the parent of it, which is the next bigger node. */
8638 while (elt->parent && elt->parent->right == elt)
8641 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8644 next = elt->purpose;
8650 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8651 || TREE_CODE (constructor_type) == UNION_TYPE)
8653 tree ctor_unfilled_bitpos, elt_bitpos;
8655 /* If the current record is complete we are done. */
8656 if (constructor_unfilled_fields == 0)
8659 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8660 elt_bitpos = bit_position (elt->purpose);
8661 /* We can't compare fields here because there might be empty
8662 fields in between. */
8663 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8665 constructor_unfilled_fields = elt->purpose;
8666 output_init_element (input_location, elt->value, elt->origtype,
8667 true, TREE_TYPE (elt->purpose),
8668 elt->purpose, 0, false,
8669 braced_init_obstack);
8671 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8673 /* Advance to the next smaller node. */
8678 /* We have reached the smallest node bigger than the
8679 current unfilled field. Fill the space first. */
8680 next = elt->purpose;
8686 /* Advance to the next bigger node. */
8691 /* We have reached the biggest node in a subtree. Find
8692 the parent of it, which is the next bigger node. */
8693 while (elt->parent && elt->parent->right == elt)
8697 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8698 bit_position (elt->purpose))))
8700 next = elt->purpose;
8708 /* Ordinarily return, but not if we want to output all
8709 and there are elements left. */
8710 if (!(all && next != 0))
8713 /* If it's not incremental, just skip over the gap, so that after
8714 jumping to retry we will output the next successive element. */
8715 if (TREE_CODE (constructor_type) == RECORD_TYPE
8716 || TREE_CODE (constructor_type) == UNION_TYPE)
8717 constructor_unfilled_fields = next;
8718 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8719 constructor_unfilled_index = next;
8721 /* ELT now points to the node in the pending tree with the next
8722 initializer to output. */
8726 /* Add one non-braced element to the current constructor level.
8727 This adjusts the current position within the constructor's type.
8728 This may also start or terminate implicit levels
8729 to handle a partly-braced initializer.
8731 Once this has found the correct level for the new element,
8732 it calls output_init_element.
8734 IMPLICIT is true if value comes from pop_init_level (1),
8735 the new initializer has been merged with the existing one
8736 and thus no warnings should be emitted about overriding an
8737 existing initializer. */
8740 process_init_element (location_t loc, struct c_expr value, bool implicit,
8741 struct obstack * braced_init_obstack)
8743 tree orig_value = value.value;
8744 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8745 bool strict_string = value.original_code == STRING_CST;
8746 bool was_designated = designator_depth != 0;
8748 designator_depth = 0;
8749 designator_erroneous = 0;
8751 if (!implicit && value.value && !integer_zerop (value.value))
8752 constructor_zeroinit = 0;
8754 /* Handle superfluous braces around string cst as in
8755 char x[] = {"foo"}; */
8759 && TREE_CODE (constructor_type) == ARRAY_TYPE
8760 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8761 && integer_zerop (constructor_unfilled_index))
8763 if (constructor_stack->replacement_value.value)
8764 error_init (loc, "excess elements in char array initializer");
8765 constructor_stack->replacement_value = value;
8769 if (constructor_stack->replacement_value.value != 0)
8771 error_init (loc, "excess elements in struct initializer");
8775 /* Ignore elements of a brace group if it is entirely superfluous
8776 and has already been diagnosed. */
8777 if (constructor_type == 0)
8780 if (!implicit && warn_designated_init && !was_designated
8781 && TREE_CODE (constructor_type) == RECORD_TYPE
8782 && lookup_attribute ("designated_init",
8783 TYPE_ATTRIBUTES (constructor_type)))
8785 OPT_Wdesignated_init,
8786 "positional initialization of field "
8787 "in %<struct%> declared with %<designated_init%> attribute");
8789 /* If we've exhausted any levels that didn't have braces,
8791 while (constructor_stack->implicit)
8793 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8794 || TREE_CODE (constructor_type) == UNION_TYPE)
8795 && constructor_fields == 0)
8796 process_init_element (loc,
8797 pop_init_level (loc, 1, braced_init_obstack),
8798 true, braced_init_obstack);
8799 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8800 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8801 && constructor_max_index
8802 && tree_int_cst_lt (constructor_max_index,
8804 process_init_element (loc,
8805 pop_init_level (loc, 1, braced_init_obstack),
8806 true, braced_init_obstack);
8811 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8812 if (constructor_range_stack)
8814 /* If value is a compound literal and we'll be just using its
8815 content, don't put it into a SAVE_EXPR. */
8816 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8817 || !require_constant_value)
8819 tree semantic_type = NULL_TREE;
8820 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8822 semantic_type = TREE_TYPE (value.value);
8823 value.value = TREE_OPERAND (value.value, 0);
8825 value.value = c_save_expr (value.value);
8827 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8834 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8837 enum tree_code fieldcode;
8839 if (constructor_fields == 0)
8841 pedwarn_init (loc, 0, "excess elements in struct initializer");
8845 fieldtype = TREE_TYPE (constructor_fields);
8846 if (fieldtype != error_mark_node)
8847 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8848 fieldcode = TREE_CODE (fieldtype);
8850 /* Error for non-static initialization of a flexible array member. */
8851 if (fieldcode == ARRAY_TYPE
8852 && !require_constant_value
8853 && TYPE_SIZE (fieldtype) == NULL_TREE
8854 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8856 error_init (loc, "non-static initialization of a flexible "
8861 /* Error for initialization of a flexible array member with
8862 a string constant if the structure is in an array. E.g.:
8863 struct S { int x; char y[]; };
8864 struct S s[] = { { 1, "foo" } };
8867 && fieldcode == ARRAY_TYPE
8868 && constructor_depth > 1
8869 && TYPE_SIZE (fieldtype) == NULL_TREE
8870 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8872 bool in_array_p = false;
8873 for (struct constructor_stack *p = constructor_stack;
8874 p && p->type; p = p->next)
8875 if (TREE_CODE (p->type) == ARRAY_TYPE)
8882 error_init (loc, "initialization of flexible array "
8883 "member in a nested context");
8888 /* Accept a string constant to initialize a subarray. */
8889 if (value.value != 0
8890 && fieldcode == ARRAY_TYPE
8891 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8893 value.value = orig_value;
8894 /* Otherwise, if we have come to a subaggregate,
8895 and we don't have an element of its type, push into it. */
8896 else if (value.value != 0
8897 && value.value != error_mark_node
8898 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8899 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8900 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8902 push_init_level (loc, 1, braced_init_obstack);
8908 push_member_name (constructor_fields);
8909 output_init_element (loc, value.value, value.original_type,
8910 strict_string, fieldtype,
8911 constructor_fields, 1, implicit,
8912 braced_init_obstack);
8913 RESTORE_SPELLING_DEPTH (constructor_depth);
8916 /* Do the bookkeeping for an element that was
8917 directly output as a constructor. */
8919 /* For a record, keep track of end position of last field. */
8920 if (DECL_SIZE (constructor_fields))
8921 constructor_bit_index
8922 = size_binop_loc (input_location, PLUS_EXPR,
8923 bit_position (constructor_fields),
8924 DECL_SIZE (constructor_fields));
8926 /* If the current field was the first one not yet written out,
8927 it isn't now, so update. */
8928 if (constructor_unfilled_fields == constructor_fields)
8930 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8931 /* Skip any nameless bit fields. */
8932 while (constructor_unfilled_fields != 0
8933 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8934 && DECL_NAME (constructor_unfilled_fields) == 0)
8935 constructor_unfilled_fields =
8936 DECL_CHAIN (constructor_unfilled_fields);
8940 constructor_fields = DECL_CHAIN (constructor_fields);
8941 /* Skip any nameless bit fields at the beginning. */
8942 while (constructor_fields != 0
8943 && DECL_C_BIT_FIELD (constructor_fields)
8944 && DECL_NAME (constructor_fields) == 0)
8945 constructor_fields = DECL_CHAIN (constructor_fields);
8947 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8950 enum tree_code fieldcode;
8952 if (constructor_fields == 0)
8954 pedwarn_init (loc, 0,
8955 "excess elements in union initializer");
8959 fieldtype = TREE_TYPE (constructor_fields);
8960 if (fieldtype != error_mark_node)
8961 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8962 fieldcode = TREE_CODE (fieldtype);
8964 /* Warn that traditional C rejects initialization of unions.
8965 We skip the warning if the value is zero. This is done
8966 under the assumption that the zero initializer in user
8967 code appears conditioned on e.g. __STDC__ to avoid
8968 "missing initializer" warnings and relies on default
8969 initialization to zero in the traditional C case.
8970 We also skip the warning if the initializer is designated,
8971 again on the assumption that this must be conditional on
8972 __STDC__ anyway (and we've already complained about the
8973 member-designator already). */
8974 if (!in_system_header_at (input_location) && !constructor_designated
8975 && !(value.value && (integer_zerop (value.value)
8976 || real_zerop (value.value))))
8977 warning (OPT_Wtraditional, "traditional C rejects initialization "
8980 /* Accept a string constant to initialize a subarray. */
8981 if (value.value != 0
8982 && fieldcode == ARRAY_TYPE
8983 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8985 value.value = orig_value;
8986 /* Otherwise, if we have come to a subaggregate,
8987 and we don't have an element of its type, push into it. */
8988 else if (value.value != 0
8989 && value.value != error_mark_node
8990 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8991 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8992 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8994 push_init_level (loc, 1, braced_init_obstack);
9000 push_member_name (constructor_fields);
9001 output_init_element (loc, value.value, value.original_type,
9002 strict_string, fieldtype,
9003 constructor_fields, 1, implicit,
9004 braced_init_obstack);
9005 RESTORE_SPELLING_DEPTH (constructor_depth);
9008 /* Do the bookkeeping for an element that was
9009 directly output as a constructor. */
9011 constructor_bit_index = DECL_SIZE (constructor_fields);
9012 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9015 constructor_fields = 0;
9017 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9019 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9020 enum tree_code eltcode = TREE_CODE (elttype);
9022 /* Accept a string constant to initialize a subarray. */
9023 if (value.value != 0
9024 && eltcode == ARRAY_TYPE
9025 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9027 value.value = orig_value;
9028 /* Otherwise, if we have come to a subaggregate,
9029 and we don't have an element of its type, push into it. */
9030 else if (value.value != 0
9031 && value.value != error_mark_node
9032 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9033 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9034 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9036 push_init_level (loc, 1, braced_init_obstack);
9040 if (constructor_max_index != 0
9041 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9042 || integer_all_onesp (constructor_max_index)))
9044 pedwarn_init (loc, 0,
9045 "excess elements in array initializer");
9049 /* Now output the actual element. */
9052 push_array_bounds (tree_to_uhwi (constructor_index));
9053 output_init_element (loc, value.value, value.original_type,
9054 strict_string, elttype,
9055 constructor_index, 1, implicit,
9056 braced_init_obstack);
9057 RESTORE_SPELLING_DEPTH (constructor_depth);
9061 = size_binop_loc (input_location, PLUS_EXPR,
9062 constructor_index, bitsize_one_node);
9065 /* If we are doing the bookkeeping for an element that was
9066 directly output as a constructor, we must update
9067 constructor_unfilled_index. */
9068 constructor_unfilled_index = constructor_index;
9070 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
9072 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9074 /* Do a basic check of initializer size. Note that vectors
9075 always have a fixed size derived from their type. */
9076 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9078 pedwarn_init (loc, 0,
9079 "excess elements in vector initializer");
9083 /* Now output the actual element. */
9086 if (TREE_CODE (value.value) == VECTOR_CST)
9087 elttype = TYPE_MAIN_VARIANT (constructor_type);
9088 output_init_element (loc, value.value, value.original_type,
9089 strict_string, elttype,
9090 constructor_index, 1, implicit,
9091 braced_init_obstack);
9095 = size_binop_loc (input_location,
9096 PLUS_EXPR, constructor_index, bitsize_one_node);
9099 /* If we are doing the bookkeeping for an element that was
9100 directly output as a constructor, we must update
9101 constructor_unfilled_index. */
9102 constructor_unfilled_index = constructor_index;
9105 /* Handle the sole element allowed in a braced initializer
9106 for a scalar variable. */
9107 else if (constructor_type != error_mark_node
9108 && constructor_fields == 0)
9110 pedwarn_init (loc, 0,
9111 "excess elements in scalar initializer");
9117 output_init_element (loc, value.value, value.original_type,
9118 strict_string, constructor_type,
9119 NULL_TREE, 1, implicit,
9120 braced_init_obstack);
9121 constructor_fields = 0;
9124 /* Handle range initializers either at this level or anywhere higher
9125 in the designator stack. */
9126 if (constructor_range_stack)
9128 struct constructor_range_stack *p, *range_stack;
9131 range_stack = constructor_range_stack;
9132 constructor_range_stack = 0;
9133 while (constructor_stack != range_stack->stack)
9135 gcc_assert (constructor_stack->implicit);
9136 process_init_element (loc,
9137 pop_init_level (loc, 1,
9138 braced_init_obstack),
9139 true, braced_init_obstack);
9141 for (p = range_stack;
9142 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9145 gcc_assert (constructor_stack->implicit);
9146 process_init_element (loc,
9147 pop_init_level (loc, 1,
9148 braced_init_obstack),
9149 true, braced_init_obstack);
9152 p->index = size_binop_loc (input_location,
9153 PLUS_EXPR, p->index, bitsize_one_node);
9154 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9159 constructor_index = p->index;
9160 constructor_fields = p->fields;
9161 if (finish && p->range_end && p->index == p->range_start)
9169 finish_implicit_inits (loc, braced_init_obstack);
9170 push_init_level (loc, 2, braced_init_obstack);
9171 p->stack = constructor_stack;
9172 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9173 p->index = p->range_start;
9177 constructor_range_stack = range_stack;
9184 constructor_range_stack = 0;
9187 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9188 (guaranteed to be 'volatile' or null) and ARGS (represented using
9189 an ASM_EXPR node). */
9191 build_asm_stmt (tree cv_qualifier, tree args)
9193 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9194 ASM_VOLATILE_P (args) = 1;
9195 return add_stmt (args);
9198 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9199 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9200 SIMPLE indicates whether there was anything at all after the
9201 string in the asm expression -- asm("blah") and asm("blah" : )
9202 are subtly different. We use a ASM_EXPR node to represent this. */
9204 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9205 tree clobbers, tree labels, bool simple)
9210 const char *constraint;
9211 const char **oconstraints;
9212 bool allows_mem, allows_reg, is_inout;
9213 int ninputs, noutputs;
9215 ninputs = list_length (inputs);
9216 noutputs = list_length (outputs);
9217 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9219 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9221 /* Remove output conversions that change the type but not the mode. */
9222 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9224 tree output = TREE_VALUE (tail);
9226 output = c_fully_fold (output, false, NULL);
9228 /* ??? Really, this should not be here. Users should be using a
9229 proper lvalue, dammit. But there's a long history of using casts
9230 in the output operands. In cases like longlong.h, this becomes a
9231 primitive form of typechecking -- if the cast can be removed, then
9232 the output operand had a type of the proper width; otherwise we'll
9233 get an error. Gross, but ... */
9234 STRIP_NOPS (output);
9236 if (!lvalue_or_else (loc, output, lv_asm))
9237 output = error_mark_node;
9239 if (output != error_mark_node
9240 && (TREE_READONLY (output)
9241 || TYPE_READONLY (TREE_TYPE (output))
9242 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9243 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9244 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9245 readonly_error (loc, output, lv_asm);
9247 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9248 oconstraints[i] = constraint;
9250 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9251 &allows_mem, &allows_reg, &is_inout))
9253 /* If the operand is going to end up in memory,
9254 mark it addressable. */
9255 if (!allows_reg && !c_mark_addressable (output))
9256 output = error_mark_node;
9257 if (!(!allows_reg && allows_mem)
9258 && output != error_mark_node
9259 && VOID_TYPE_P (TREE_TYPE (output)))
9261 error_at (loc, "invalid use of void expression");
9262 output = error_mark_node;
9266 output = error_mark_node;
9268 TREE_VALUE (tail) = output;
9271 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9275 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9276 input = TREE_VALUE (tail);
9278 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9279 oconstraints, &allows_mem, &allows_reg))
9281 /* If the operand is going to end up in memory,
9282 mark it addressable. */
9283 if (!allows_reg && allows_mem)
9285 input = c_fully_fold (input, false, NULL);
9287 /* Strip the nops as we allow this case. FIXME, this really
9288 should be rejected or made deprecated. */
9290 if (!c_mark_addressable (input))
9291 input = error_mark_node;
9296 memset (&expr, 0, sizeof (expr));
9298 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9299 input = c_fully_fold (expr.value, false, NULL);
9301 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9303 error_at (loc, "invalid use of void expression");
9304 input = error_mark_node;
9309 input = error_mark_node;
9311 TREE_VALUE (tail) = input;
9314 /* ASMs with labels cannot have outputs. This should have been
9315 enforced by the parser. */
9316 gcc_assert (outputs == NULL || labels == NULL);
9318 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9320 /* asm statements without outputs, including simple ones, are treated
9322 ASM_INPUT_P (args) = simple;
9323 ASM_VOLATILE_P (args) = (noutputs == 0);
9328 /* Generate a goto statement to LABEL. LOC is the location of the
9332 c_finish_goto_label (location_t loc, tree label)
9334 tree decl = lookup_label_for_goto (loc, label);
9337 TREE_USED (decl) = 1;
9339 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9340 SET_EXPR_LOCATION (t, loc);
9341 return add_stmt (t);
9345 /* Generate a computed goto statement to EXPR. LOC is the location of
9349 c_finish_goto_ptr (location_t loc, tree expr)
9352 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9353 expr = c_fully_fold (expr, false, NULL);
9354 expr = convert (ptr_type_node, expr);
9355 t = build1 (GOTO_EXPR, void_type_node, expr);
9356 SET_EXPR_LOCATION (t, loc);
9357 return add_stmt (t);
9360 /* Generate a C `return' statement. RETVAL is the expression for what
9361 to return, or a null pointer for `return;' with no value. LOC is
9362 the location of the return statement, or the location of the expression,
9363 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9364 is the original type of RETVAL. */
9367 c_finish_return (location_t loc, tree retval, tree origtype)
9369 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9370 bool no_warning = false;
9374 /* Use the expansion point to handle cases such as returning NULL
9375 in a function returning void. */
9376 source_location xloc = expansion_point_location_if_in_system_header (loc);
9378 if (TREE_THIS_VOLATILE (current_function_decl))
9379 warning_at (xloc, 0,
9380 "function declared %<noreturn%> has a %<return%> statement");
9382 if (flag_cilkplus && contains_array_notation_expr (retval))
9384 /* Array notations are allowed in a return statement if it is inside a
9385 built-in array notation reduction function. */
9386 if (!find_rank (loc, retval, retval, false, &rank))
9387 return error_mark_node;
9390 error_at (loc, "array notation expression cannot be used as a "
9392 return error_mark_node;
9395 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9397 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9399 return error_mark_node;
9403 tree semantic_type = NULL_TREE;
9404 npc = null_pointer_constant_p (retval);
9405 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9407 semantic_type = TREE_TYPE (retval);
9408 retval = TREE_OPERAND (retval, 0);
9410 retval = c_fully_fold (retval, false, NULL);
9412 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9417 current_function_returns_null = 1;
9418 if ((warn_return_type || flag_isoc99)
9419 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9422 pedwarn (loc, 0, "%<return%> with no value, in "
9423 "function returning non-void");
9425 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9426 "in function returning non-void");
9430 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9432 current_function_returns_null = 1;
9433 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9435 "%<return%> with a value, in function returning void");
9437 pedwarn (xloc, OPT_Wpedantic, "ISO C forbids "
9438 "%<return%> with expression, in function returning void");
9442 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9443 retval, origtype, ic_return,
9444 npc, NULL_TREE, NULL_TREE, 0);
9445 tree res = DECL_RESULT (current_function_decl);
9449 current_function_returns_value = 1;
9450 if (t == error_mark_node)
9453 save = in_late_binary_op;
9454 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9455 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9456 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9457 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9458 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9459 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9460 in_late_binary_op = true;
9461 inner = t = convert (TREE_TYPE (res), t);
9462 in_late_binary_op = save;
9464 /* Strip any conversions, additions, and subtractions, and see if
9465 we are returning the address of a local variable. Warn if so. */
9468 switch (TREE_CODE (inner))
9471 case NON_LVALUE_EXPR:
9473 case POINTER_PLUS_EXPR:
9474 inner = TREE_OPERAND (inner, 0);
9478 /* If the second operand of the MINUS_EXPR has a pointer
9479 type (or is converted from it), this may be valid, so
9480 don't give a warning. */
9482 tree op1 = TREE_OPERAND (inner, 1);
9484 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9485 && (CONVERT_EXPR_P (op1)
9486 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9487 op1 = TREE_OPERAND (op1, 0);
9489 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9492 inner = TREE_OPERAND (inner, 0);
9497 inner = TREE_OPERAND (inner, 0);
9499 while (REFERENCE_CLASS_P (inner)
9500 && TREE_CODE (inner) != INDIRECT_REF)
9501 inner = TREE_OPERAND (inner, 0);
9504 && !DECL_EXTERNAL (inner)
9505 && !TREE_STATIC (inner)
9506 && DECL_CONTEXT (inner) == current_function_decl)
9508 if (TREE_CODE (inner) == LABEL_DECL)
9509 warning_at (loc, OPT_Wreturn_local_addr,
9510 "function returns address of label");
9513 warning_at (loc, OPT_Wreturn_local_addr,
9514 "function returns address of local variable");
9515 tree zero = build_zero_cst (TREE_TYPE (res));
9516 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9528 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9529 SET_EXPR_LOCATION (retval, loc);
9531 if (warn_sequence_point)
9532 verify_sequence_points (retval);
9535 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9536 TREE_NO_WARNING (ret_stmt) |= no_warning;
9537 return add_stmt (ret_stmt);
9541 /* The SWITCH_EXPR being built. */
9544 /* The original type of the testing expression, i.e. before the
9545 default conversion is applied. */
9548 /* A splay-tree mapping the low element of a case range to the high
9549 element, or NULL_TREE if there is no high element. Used to
9550 determine whether or not a new case label duplicates an old case
9551 label. We need a tree, rather than simply a hash table, because
9552 of the GNU case range extension. */
9555 /* The bindings at the point of the switch. This is used for
9556 warnings crossing decls when branching to a case label. */
9557 struct c_spot_bindings *bindings;
9559 /* The next node on the stack. */
9560 struct c_switch *next;
9563 /* A stack of the currently active switch statements. The innermost
9564 switch statement is on the top of the stack. There is no need to
9565 mark the stack for garbage collection because it is only active
9566 during the processing of the body of a function, and we never
9567 collect at that point. */
9569 struct c_switch *c_switch_stack;
9571 /* Start a C switch statement, testing expression EXP. Return the new
9572 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9573 SWITCH_COND_LOC is the location of the switch's condition.
9574 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9577 c_start_case (location_t switch_loc,
9578 location_t switch_cond_loc,
9579 tree exp, bool explicit_cast_p)
9581 tree orig_type = error_mark_node;
9582 struct c_switch *cs;
9584 if (exp != error_mark_node)
9586 orig_type = TREE_TYPE (exp);
9588 if (!INTEGRAL_TYPE_P (orig_type))
9590 if (orig_type != error_mark_node)
9592 error_at (switch_cond_loc, "switch quantity not an integer");
9593 orig_type = error_mark_node;
9595 exp = integer_zero_node;
9599 tree type = TYPE_MAIN_VARIANT (orig_type);
9602 /* Warn if the condition has boolean value. */
9603 while (TREE_CODE (e) == COMPOUND_EXPR)
9604 e = TREE_OPERAND (e, 1);
9606 if ((TREE_CODE (type) == BOOLEAN_TYPE
9607 || truth_value_p (TREE_CODE (e)))
9608 /* Explicit cast to int suppresses this warning. */
9609 && !(TREE_CODE (type) == INTEGER_TYPE
9610 && explicit_cast_p))
9611 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9612 "switch condition has boolean value");
9614 if (!in_system_header_at (input_location)
9615 && (type == long_integer_type_node
9616 || type == long_unsigned_type_node))
9617 warning_at (switch_cond_loc,
9618 OPT_Wtraditional, "%<long%> switch expression not "
9619 "converted to %<int%> in ISO C");
9621 exp = c_fully_fold (exp, false, NULL);
9622 exp = default_conversion (exp);
9624 if (warn_sequence_point)
9625 verify_sequence_points (exp);
9629 /* Add this new SWITCH_EXPR to the stack. */
9630 cs = XNEW (struct c_switch);
9631 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9632 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9633 cs->orig_type = orig_type;
9634 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9635 cs->bindings = c_get_switch_bindings ();
9636 cs->next = c_switch_stack;
9637 c_switch_stack = cs;
9639 return add_stmt (cs->switch_expr);
9642 /* Process a case label at location LOC. */
9645 do_case (location_t loc, tree low_value, tree high_value)
9647 tree label = NULL_TREE;
9649 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9651 low_value = c_fully_fold (low_value, false, NULL);
9652 if (TREE_CODE (low_value) == INTEGER_CST)
9653 pedwarn (loc, OPT_Wpedantic,
9654 "case label is not an integer constant expression");
9657 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9659 high_value = c_fully_fold (high_value, false, NULL);
9660 if (TREE_CODE (high_value) == INTEGER_CST)
9661 pedwarn (input_location, OPT_Wpedantic,
9662 "case label is not an integer constant expression");
9665 if (c_switch_stack == NULL)
9668 error_at (loc, "case label not within a switch statement");
9670 error_at (loc, "%<default%> label not within a switch statement");
9674 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9675 EXPR_LOCATION (c_switch_stack->switch_expr),
9679 label = c_add_case_label (loc, c_switch_stack->cases,
9680 SWITCH_COND (c_switch_stack->switch_expr),
9681 c_switch_stack->orig_type,
9682 low_value, high_value);
9683 if (label == error_mark_node)
9688 /* Finish the switch statement. TYPE is the original type of the
9689 controlling expression of the switch, or NULL_TREE. */
9692 c_finish_case (tree body, tree type)
9694 struct c_switch *cs = c_switch_stack;
9695 location_t switch_location;
9697 SWITCH_BODY (cs->switch_expr) = body;
9699 /* Emit warnings as needed. */
9700 switch_location = EXPR_LOCATION (cs->switch_expr);
9701 c_do_switch_warnings (cs->cases, switch_location,
9702 type ? type : TREE_TYPE (cs->switch_expr),
9703 SWITCH_COND (cs->switch_expr));
9705 /* Pop the stack. */
9706 c_switch_stack = cs->next;
9707 splay_tree_delete (cs->cases);
9708 c_release_switch_bindings (cs->bindings);
9712 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9713 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9714 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9715 statement, and was not surrounded with parenthesis. */
9718 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9719 tree else_block, bool nested_if)
9723 /* If the condition has array notations, then the rank of the then_block and
9724 else_block must be either 0 or be equal to the rank of the condition. If
9725 the condition does not have array notations then break them up as it is
9726 broken up in a normal expression. */
9727 if (flag_cilkplus && contains_array_notation_expr (cond))
9729 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9730 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9733 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9736 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9738 if (cond_rank != then_rank && then_rank != 0)
9740 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9741 " and the then-block");
9744 else if (cond_rank != else_rank && else_rank != 0)
9746 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9747 " and the else-block");
9751 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9752 if (warn_parentheses && nested_if && else_block == NULL)
9754 tree inner_if = then_block;
9756 /* We know from the grammar productions that there is an IF nested
9757 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9758 it might not be exactly THEN_BLOCK, but should be the last
9759 non-container statement within. */
9761 switch (TREE_CODE (inner_if))
9766 inner_if = BIND_EXPR_BODY (inner_if);
9768 case STATEMENT_LIST:
9769 inner_if = expr_last (then_block);
9771 case TRY_FINALLY_EXPR:
9772 case TRY_CATCH_EXPR:
9773 inner_if = TREE_OPERAND (inner_if, 0);
9780 if (COND_EXPR_ELSE (inner_if))
9781 warning_at (if_locus, OPT_Wparentheses,
9782 "suggest explicit braces to avoid ambiguous %<else%>");
9785 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9786 SET_EXPR_LOCATION (stmt, if_locus);
9790 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9791 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9792 is false for DO loops. INCR is the FOR increment expression. BODY is
9793 the statement controlled by the loop. BLAB is the break label. CLAB is
9794 the continue label. Everything is allowed to be NULL. */
9797 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9798 tree blab, tree clab, bool cond_is_first)
9800 tree entry = NULL, exit = NULL, t;
9802 /* In theory could forbid cilk spawn for loop increment expression,
9803 but it should work just fine. */
9805 /* If the condition is zero don't generate a loop construct. */
9806 if (cond && integer_zerop (cond))
9810 t = build_and_jump (&blab);
9811 SET_EXPR_LOCATION (t, start_locus);
9817 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9819 /* If we have an exit condition, then we build an IF with gotos either
9820 out of the loop, or to the top of it. If there's no exit condition,
9821 then we just build a jump back to the top. */
9822 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9824 if (cond && !integer_nonzerop (cond))
9826 /* Canonicalize the loop condition to the end. This means
9827 generating a branch to the loop condition. Reuse the
9828 continue label, if possible. */
9833 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9834 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9837 t = build1 (GOTO_EXPR, void_type_node, clab);
9838 SET_EXPR_LOCATION (t, start_locus);
9842 t = build_and_jump (&blab);
9844 exit = fold_build3_loc (start_locus,
9845 COND_EXPR, void_type_node, cond, exit, t);
9847 exit = fold_build3_loc (input_location,
9848 COND_EXPR, void_type_node, cond, exit, t);
9857 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9865 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9869 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9872 tree label = *label_p;
9874 /* In switch statements break is sometimes stylistically used after
9875 a return statement. This can lead to spurious warnings about
9876 control reaching the end of a non-void function when it is
9877 inlined. Note that we are calling block_may_fallthru with
9878 language specific tree nodes; this works because
9879 block_may_fallthru returns true when given something it does not
9881 skip = !block_may_fallthru (cur_stmt_list);
9886 *label_p = label = create_artificial_label (loc);
9888 else if (TREE_CODE (label) == LABEL_DECL)
9890 else switch (TREE_INT_CST_LOW (label))
9894 error_at (loc, "break statement not within loop or switch");
9896 error_at (loc, "continue statement not within a loop");
9900 gcc_assert (is_break);
9901 error_at (loc, "break statement used with OpenMP for loop");
9906 error ("break statement within %<#pragma simd%> loop body");
9908 error ("continue statement within %<#pragma simd%> loop body");
9919 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9921 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9924 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9927 emit_side_effect_warnings (location_t loc, tree expr)
9929 if (expr == error_mark_node)
9931 else if (!TREE_SIDE_EFFECTS (expr))
9933 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9934 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9936 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9939 location_t cloc = loc;
9940 while (TREE_CODE (r) == COMPOUND_EXPR)
9942 if (EXPR_HAS_LOCATION (r))
9943 cloc = EXPR_LOCATION (r);
9944 r = TREE_OPERAND (r, 1);
9946 if (!TREE_SIDE_EFFECTS (r)
9947 && !VOID_TYPE_P (TREE_TYPE (r))
9948 && !CONVERT_EXPR_P (r)
9949 && !TREE_NO_WARNING (r)
9950 && !TREE_NO_WARNING (expr))
9951 warning_at (cloc, OPT_Wunused_value,
9952 "right-hand operand of comma expression has no effect");
9955 warn_if_unused_value (expr, loc);
9958 /* Process an expression as if it were a complete statement. Emit
9959 diagnostics, but do not call ADD_STMT. LOC is the location of the
9963 c_process_expr_stmt (location_t loc, tree expr)
9970 expr = c_fully_fold (expr, false, NULL);
9972 if (warn_sequence_point)
9973 verify_sequence_points (expr);
9975 if (TREE_TYPE (expr) != error_mark_node
9976 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9977 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9978 error_at (loc, "expression statement has incomplete type");
9980 /* If we're not processing a statement expression, warn about unused values.
9981 Warnings for statement expressions will be emitted later, once we figure
9982 out which is the result. */
9983 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9984 && warn_unused_value)
9985 emit_side_effect_warnings (loc, expr);
9988 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9989 exprv = TREE_OPERAND (exprv, 1);
9990 while (CONVERT_EXPR_P (exprv))
9991 exprv = TREE_OPERAND (exprv, 0);
9993 || handled_component_p (exprv)
9994 || TREE_CODE (exprv) == ADDR_EXPR)
9995 mark_exp_read (exprv);
9997 /* If the expression is not of a type to which we cannot assign a line
9998 number, wrap the thing in a no-op NOP_EXPR. */
9999 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10001 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10002 SET_EXPR_LOCATION (expr, loc);
10008 /* Emit an expression as a statement. LOC is the location of the
10012 c_finish_expr_stmt (location_t loc, tree expr)
10015 return add_stmt (c_process_expr_stmt (loc, expr));
10020 /* Do the opposite and emit a statement as an expression. To begin,
10021 create a new binding level and return it. */
10024 c_begin_stmt_expr (void)
10028 /* We must force a BLOCK for this level so that, if it is not expanded
10029 later, there is a way to turn off the entire subtree of blocks that
10030 are contained in it. */
10031 keep_next_level ();
10032 ret = c_begin_compound_stmt (true);
10034 c_bindings_start_stmt_expr (c_switch_stack == NULL
10036 : c_switch_stack->bindings);
10038 /* Mark the current statement list as belonging to a statement list. */
10039 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10044 /* LOC is the location of the compound statement to which this body
10048 c_finish_stmt_expr (location_t loc, tree body)
10050 tree last, type, tmp, val;
10053 body = c_end_compound_stmt (loc, body, true);
10055 c_bindings_end_stmt_expr (c_switch_stack == NULL
10057 : c_switch_stack->bindings);
10059 /* Locate the last statement in BODY. See c_end_compound_stmt
10060 about always returning a BIND_EXPR. */
10061 last_p = &BIND_EXPR_BODY (body);
10062 last = BIND_EXPR_BODY (body);
10064 continue_searching:
10065 if (TREE_CODE (last) == STATEMENT_LIST)
10067 tree_stmt_iterator i;
10069 /* This can happen with degenerate cases like ({ }). No value. */
10070 if (!TREE_SIDE_EFFECTS (last))
10073 /* If we're supposed to generate side effects warnings, process
10074 all of the statements except the last. */
10075 if (warn_unused_value)
10077 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10080 tree t = tsi_stmt (i);
10082 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10083 emit_side_effect_warnings (tloc, t);
10087 i = tsi_last (last);
10088 last_p = tsi_stmt_ptr (i);
10092 /* If the end of the list is exception related, then the list was split
10093 by a call to push_cleanup. Continue searching. */
10094 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10095 || TREE_CODE (last) == TRY_CATCH_EXPR)
10097 last_p = &TREE_OPERAND (last, 0);
10099 goto continue_searching;
10102 if (last == error_mark_node)
10105 /* In the case that the BIND_EXPR is not necessary, return the
10106 expression out from inside it. */
10107 if (last == BIND_EXPR_BODY (body)
10108 && BIND_EXPR_VARS (body) == NULL)
10110 /* Even if this looks constant, do not allow it in a constant
10112 last = c_wrap_maybe_const (last, true);
10113 /* Do not warn if the return value of a statement expression is
10115 TREE_NO_WARNING (last) = 1;
10119 /* Extract the type of said expression. */
10120 type = TREE_TYPE (last);
10122 /* If we're not returning a value at all, then the BIND_EXPR that
10123 we already have is a fine expression to return. */
10124 if (!type || VOID_TYPE_P (type))
10127 /* Now that we've located the expression containing the value, it seems
10128 silly to make voidify_wrapper_expr repeat the process. Create a
10129 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10130 tmp = create_tmp_var_raw (type);
10132 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10133 tree_expr_nonnegative_p giving up immediately. */
10135 if (TREE_CODE (val) == NOP_EXPR
10136 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10137 val = TREE_OPERAND (val, 0);
10139 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10140 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10143 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10144 SET_EXPR_LOCATION (t, loc);
10149 /* Begin and end compound statements. This is as simple as pushing
10150 and popping new statement lists from the tree. */
10153 c_begin_compound_stmt (bool do_scope)
10155 tree stmt = push_stmt_list ();
10161 /* End a compound statement. STMT is the statement. LOC is the
10162 location of the compound statement-- this is usually the location
10163 of the opening brace. */
10166 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10172 if (c_dialect_objc ())
10173 objc_clear_super_receiver ();
10174 block = pop_scope ();
10177 stmt = pop_stmt_list (stmt);
10178 stmt = c_build_bind_expr (loc, block, stmt);
10180 /* If this compound statement is nested immediately inside a statement
10181 expression, then force a BIND_EXPR to be created. Otherwise we'll
10182 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10183 STATEMENT_LISTs merge, and thus we can lose track of what statement
10184 was really last. */
10185 if (building_stmt_list_p ()
10186 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10187 && TREE_CODE (stmt) != BIND_EXPR)
10189 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10190 TREE_SIDE_EFFECTS (stmt) = 1;
10191 SET_EXPR_LOCATION (stmt, loc);
10197 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10198 when the current scope is exited. EH_ONLY is true when this is not
10199 meant to apply to normal control flow transfer. */
10202 push_cleanup (tree decl, tree cleanup, bool eh_only)
10204 enum tree_code code;
10208 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10209 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10211 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10212 list = push_stmt_list ();
10213 TREE_OPERAND (stmt, 0) = list;
10214 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10217 /* Build a binary-operation expression without default conversions.
10218 CODE is the kind of expression to build.
10219 LOCATION is the operator's location.
10220 This function differs from `build' in several ways:
10221 the data type of the result is computed and recorded in it,
10222 warnings are generated if arg data types are invalid,
10223 special handling for addition and subtraction of pointers is known,
10224 and some optimization is done (operations on narrow ints
10225 are done in the narrower type when that gives the same result).
10226 Constant folding is also done before the result is returned.
10228 Note that the operands will never have enumeral types, or function
10229 or array types, because either they will have the default conversions
10230 performed or they have both just been converted to some other type in which
10231 the arithmetic is to be done. */
10234 build_binary_op (location_t location, enum tree_code code,
10235 tree orig_op0, tree orig_op1, int convert_p)
10237 tree type0, type1, orig_type0, orig_type1;
10239 enum tree_code code0, code1;
10241 tree ret = error_mark_node;
10242 const char *invalid_op_diag;
10243 bool op0_int_operands, op1_int_operands;
10244 bool int_const, int_const_or_overflow, int_operands;
10246 /* Expression code to give to the expression when it is built.
10247 Normally this is CODE, which is what the caller asked for,
10248 but in some special cases we change it. */
10249 enum tree_code resultcode = code;
10251 /* Data type in which the computation is to be performed.
10252 In the simplest cases this is the common type of the arguments. */
10253 tree result_type = NULL;
10255 /* When the computation is in excess precision, the type of the
10256 final EXCESS_PRECISION_EXPR. */
10257 tree semantic_result_type = NULL;
10259 /* Nonzero means operands have already been type-converted
10260 in whatever way is necessary.
10261 Zero means they need to be converted to RESULT_TYPE. */
10264 /* Nonzero means create the expression with this type, rather than
10266 tree build_type = 0;
10268 /* Nonzero means after finally constructing the expression
10269 convert it to this type. */
10270 tree final_type = 0;
10272 /* Nonzero if this is an operation like MIN or MAX which can
10273 safely be computed in short if both args are promoted shorts.
10274 Also implies COMMON.
10275 -1 indicates a bitwise operation; this makes a difference
10276 in the exact conditions for when it is safe to do the operation
10277 in a narrower mode. */
10280 /* Nonzero if this is a comparison operation;
10281 if both args are promoted shorts, compare the original shorts.
10282 Also implies COMMON. */
10283 int short_compare = 0;
10285 /* Nonzero if this is a right-shift operation, which can be computed on the
10286 original short and then promoted if the operand is a promoted short. */
10287 int short_shift = 0;
10289 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10292 /* True means types are compatible as far as ObjC is concerned. */
10295 /* True means this is an arithmetic operation that may need excess
10297 bool may_need_excess_precision;
10299 /* True means this is a boolean operation that converts both its
10300 operands to truth-values. */
10301 bool boolean_op = false;
10303 /* Remember whether we're doing / or %. */
10304 bool doing_div_or_mod = false;
10306 /* Remember whether we're doing << or >>. */
10307 bool doing_shift = false;
10309 /* Tree holding instrumentation expression. */
10310 tree instrument_expr = NULL;
10312 if (location == UNKNOWN_LOCATION)
10313 location = input_location;
10318 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10319 if (op0_int_operands)
10320 op0 = remove_c_maybe_const_expr (op0);
10321 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10322 if (op1_int_operands)
10323 op1 = remove_c_maybe_const_expr (op1);
10324 int_operands = (op0_int_operands && op1_int_operands);
10327 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10328 && TREE_CODE (orig_op1) == INTEGER_CST);
10329 int_const = (int_const_or_overflow
10330 && !TREE_OVERFLOW (orig_op0)
10331 && !TREE_OVERFLOW (orig_op1));
10334 int_const = int_const_or_overflow = false;
10336 /* Do not apply default conversion in mixed vector/scalar expression. */
10338 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10339 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10341 op0 = default_conversion (op0);
10342 op1 = default_conversion (op1);
10345 /* When Cilk Plus is enabled and there are array notations inside op0, then
10346 we check to see if there are builtin array notation functions. If
10347 so, then we take on the type of the array notation inside it. */
10348 if (flag_cilkplus && contains_array_notation_expr (op0))
10349 orig_type0 = type0 = find_correct_array_notation_type (op0);
10351 orig_type0 = type0 = TREE_TYPE (op0);
10353 if (flag_cilkplus && contains_array_notation_expr (op1))
10354 orig_type1 = type1 = find_correct_array_notation_type (op1);
10356 orig_type1 = type1 = TREE_TYPE (op1);
10358 /* The expression codes of the data types of the arguments tell us
10359 whether the arguments are integers, floating, pointers, etc. */
10360 code0 = TREE_CODE (type0);
10361 code1 = TREE_CODE (type1);
10363 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10364 STRIP_TYPE_NOPS (op0);
10365 STRIP_TYPE_NOPS (op1);
10367 /* If an error was already reported for one of the arguments,
10368 avoid reporting another error. */
10370 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10371 return error_mark_node;
10373 if ((invalid_op_diag
10374 = targetm.invalid_binary_op (code, type0, type1)))
10376 error_at (location, invalid_op_diag);
10377 return error_mark_node;
10385 case TRUNC_DIV_EXPR:
10386 case CEIL_DIV_EXPR:
10387 case FLOOR_DIV_EXPR:
10388 case ROUND_DIV_EXPR:
10389 case EXACT_DIV_EXPR:
10390 may_need_excess_precision = true;
10393 may_need_excess_precision = false;
10396 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10398 op0 = TREE_OPERAND (op0, 0);
10399 type0 = TREE_TYPE (op0);
10401 else if (may_need_excess_precision
10402 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10405 op0 = convert (eptype, op0);
10407 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10409 op1 = TREE_OPERAND (op1, 0);
10410 type1 = TREE_TYPE (op1);
10412 else if (may_need_excess_precision
10413 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10416 op1 = convert (eptype, op1);
10419 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10421 /* In case when one of the operands of the binary operation is
10422 a vector and another is a scalar -- convert scalar to vector. */
10423 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10425 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10428 switch (convert_flag)
10431 return error_mark_node;
10434 bool maybe_const = true;
10436 sc = c_fully_fold (op0, false, &maybe_const);
10437 sc = save_expr (sc);
10438 sc = convert (TREE_TYPE (type1), sc);
10439 op0 = build_vector_from_val (type1, sc);
10441 op0 = c_wrap_maybe_const (op0, true);
10442 orig_type0 = type0 = TREE_TYPE (op0);
10443 code0 = TREE_CODE (type0);
10447 case stv_secondarg:
10449 bool maybe_const = true;
10451 sc = c_fully_fold (op1, false, &maybe_const);
10452 sc = save_expr (sc);
10453 sc = convert (TREE_TYPE (type0), sc);
10454 op1 = build_vector_from_val (type0, sc);
10456 op1 = c_wrap_maybe_const (op1, true);
10457 orig_type1 = type1 = TREE_TYPE (op1);
10458 code1 = TREE_CODE (type1);
10470 /* Handle the pointer + int case. */
10471 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10473 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10474 goto return_build_binary_op;
10476 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10478 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10479 goto return_build_binary_op;
10486 /* Subtraction of two similar pointers.
10487 We must subtract them as integers, then divide by object size. */
10488 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10489 && comp_target_types (location, type0, type1))
10491 ret = pointer_diff (location, op0, op1);
10492 goto return_build_binary_op;
10494 /* Handle pointer minus int. Just like pointer plus int. */
10495 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10497 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10498 goto return_build_binary_op;
10508 case TRUNC_DIV_EXPR:
10509 case CEIL_DIV_EXPR:
10510 case FLOOR_DIV_EXPR:
10511 case ROUND_DIV_EXPR:
10512 case EXACT_DIV_EXPR:
10513 doing_div_or_mod = true;
10514 warn_for_div_by_zero (location, op1);
10516 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10517 || code0 == FIXED_POINT_TYPE
10518 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10519 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10520 || code1 == FIXED_POINT_TYPE
10521 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10523 enum tree_code tcode0 = code0, tcode1 = code1;
10525 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10526 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10527 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10528 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10530 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10531 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10532 resultcode = RDIV_EXPR;
10534 /* Although it would be tempting to shorten always here, that
10535 loses on some targets, since the modulo instruction is
10536 undefined if the quotient can't be represented in the
10537 computation mode. We shorten only if unsigned or if
10538 dividing by something we know != -1. */
10539 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10540 || (TREE_CODE (op1) == INTEGER_CST
10541 && !integer_all_onesp (op1)));
10549 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10551 /* Allow vector types which are not floating point types. */
10552 else if (code0 == VECTOR_TYPE
10553 && code1 == VECTOR_TYPE
10554 && !VECTOR_FLOAT_TYPE_P (type0)
10555 && !VECTOR_FLOAT_TYPE_P (type1))
10559 case TRUNC_MOD_EXPR:
10560 case FLOOR_MOD_EXPR:
10561 doing_div_or_mod = true;
10562 warn_for_div_by_zero (location, op1);
10564 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10565 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10566 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10568 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10570 /* Although it would be tempting to shorten always here, that loses
10571 on some targets, since the modulo instruction is undefined if the
10572 quotient can't be represented in the computation mode. We shorten
10573 only if unsigned or if dividing by something we know != -1. */
10574 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10575 || (TREE_CODE (op1) == INTEGER_CST
10576 && !integer_all_onesp (op1)));
10581 case TRUTH_ANDIF_EXPR:
10582 case TRUTH_ORIF_EXPR:
10583 case TRUTH_AND_EXPR:
10584 case TRUTH_OR_EXPR:
10585 case TRUTH_XOR_EXPR:
10586 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10587 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10588 || code0 == FIXED_POINT_TYPE)
10589 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10590 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10591 || code1 == FIXED_POINT_TYPE))
10593 /* Result of these operations is always an int,
10594 but that does not mean the operands should be
10595 converted to ints! */
10596 result_type = integer_type_node;
10597 if (op0_int_operands)
10599 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10600 op0 = remove_c_maybe_const_expr (op0);
10603 op0 = c_objc_common_truthvalue_conversion (location, op0);
10604 if (op1_int_operands)
10606 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10607 op1 = remove_c_maybe_const_expr (op1);
10610 op1 = c_objc_common_truthvalue_conversion (location, op1);
10614 if (code == TRUTH_ANDIF_EXPR)
10616 int_const_or_overflow = (int_operands
10617 && TREE_CODE (orig_op0) == INTEGER_CST
10618 && (op0 == truthvalue_false_node
10619 || TREE_CODE (orig_op1) == INTEGER_CST));
10620 int_const = (int_const_or_overflow
10621 && !TREE_OVERFLOW (orig_op0)
10622 && (op0 == truthvalue_false_node
10623 || !TREE_OVERFLOW (orig_op1)));
10625 else if (code == TRUTH_ORIF_EXPR)
10627 int_const_or_overflow = (int_operands
10628 && TREE_CODE (orig_op0) == INTEGER_CST
10629 && (op0 == truthvalue_true_node
10630 || TREE_CODE (orig_op1) == INTEGER_CST));
10631 int_const = (int_const_or_overflow
10632 && !TREE_OVERFLOW (orig_op0)
10633 && (op0 == truthvalue_true_node
10634 || !TREE_OVERFLOW (orig_op1)));
10638 /* Shift operations: result has same type as first operand;
10639 always convert second operand to int.
10640 Also set SHORT_SHIFT if shifting rightward. */
10643 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10644 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10646 result_type = type0;
10649 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10650 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10651 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10652 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10654 result_type = type0;
10657 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10658 && code1 == INTEGER_TYPE)
10660 doing_shift = true;
10661 if (TREE_CODE (op1) == INTEGER_CST)
10663 if (tree_int_cst_sgn (op1) < 0)
10666 if (c_inhibit_evaluation_warnings == 0)
10667 warning_at (location, OPT_Wshift_count_negative,
10668 "right shift count is negative");
10672 if (!integer_zerop (op1))
10675 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10678 if (c_inhibit_evaluation_warnings == 0)
10679 warning_at (location, OPT_Wshift_count_overflow,
10680 "right shift count >= width of type");
10685 /* Use the type of the value to be shifted. */
10686 result_type = type0;
10687 /* Avoid converting op1 to result_type later. */
10693 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10694 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10696 result_type = type0;
10699 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10700 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10701 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10702 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10704 result_type = type0;
10707 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10708 && code1 == INTEGER_TYPE)
10710 doing_shift = true;
10711 if (TREE_CODE (op1) == INTEGER_CST)
10713 if (tree_int_cst_sgn (op1) < 0)
10716 if (c_inhibit_evaluation_warnings == 0)
10717 warning_at (location, OPT_Wshift_count_negative,
10718 "left shift count is negative");
10721 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10724 if (c_inhibit_evaluation_warnings == 0)
10725 warning_at (location, OPT_Wshift_count_overflow,
10726 "left shift count >= width of type");
10730 /* Use the type of the value to be shifted. */
10731 result_type = type0;
10732 /* Avoid converting op1 to result_type later. */
10739 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10742 if (!vector_types_compatible_elements_p (type0, type1))
10744 error_at (location, "comparing vectors with different "
10746 return error_mark_node;
10749 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10751 error_at (location, "comparing vectors with different "
10752 "number of elements");
10753 return error_mark_node;
10756 /* It's not precisely specified how the usual arithmetic
10757 conversions apply to the vector types. Here, we use
10758 the unsigned type if one of the operands is signed and
10759 the other one is unsigned. */
10760 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
10762 if (!TYPE_UNSIGNED (type0))
10763 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
10765 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
10766 warning_at (location, OPT_Wsign_compare, "comparison between "
10767 "types %qT and %qT", type0, type1);
10770 /* Always construct signed integer vector type. */
10771 intt = c_common_type_for_size (GET_MODE_BITSIZE
10772 (TYPE_MODE (TREE_TYPE (type0))), 0);
10773 result_type = build_opaque_vector_type (intt,
10774 TYPE_VECTOR_SUBPARTS (type0));
10778 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10779 warning_at (location,
10781 "comparing floating point with == or != is unsafe");
10782 /* Result of comparison is always int,
10783 but don't convert the args to int! */
10784 build_type = integer_type_node;
10785 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10786 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10787 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10788 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10790 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10792 if (TREE_CODE (op0) == ADDR_EXPR
10793 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10795 if (code == EQ_EXPR)
10796 warning_at (location,
10798 "the comparison will always evaluate as %<false%> "
10799 "for the address of %qD will never be NULL",
10800 TREE_OPERAND (op0, 0));
10802 warning_at (location,
10804 "the comparison will always evaluate as %<true%> "
10805 "for the address of %qD will never be NULL",
10806 TREE_OPERAND (op0, 0));
10808 result_type = type0;
10810 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10812 if (TREE_CODE (op1) == ADDR_EXPR
10813 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10815 if (code == EQ_EXPR)
10816 warning_at (location,
10818 "the comparison will always evaluate as %<false%> "
10819 "for the address of %qD will never be NULL",
10820 TREE_OPERAND (op1, 0));
10822 warning_at (location,
10824 "the comparison will always evaluate as %<true%> "
10825 "for the address of %qD will never be NULL",
10826 TREE_OPERAND (op1, 0));
10828 result_type = type1;
10830 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10832 tree tt0 = TREE_TYPE (type0);
10833 tree tt1 = TREE_TYPE (type1);
10834 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10835 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10836 addr_space_t as_common = ADDR_SPACE_GENERIC;
10838 /* Anything compares with void *. void * compares with anything.
10839 Otherwise, the targets must be compatible
10840 and both must be object or both incomplete. */
10841 if (comp_target_types (location, type0, type1))
10842 result_type = common_pointer_type (type0, type1);
10843 else if (!addr_space_superset (as0, as1, &as_common))
10845 error_at (location, "comparison of pointers to "
10846 "disjoint address spaces");
10847 return error_mark_node;
10849 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10851 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10852 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10853 "comparison of %<void *%> with function pointer");
10855 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10857 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10858 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10859 "comparison of %<void *%> with function pointer");
10862 /* Avoid warning about the volatile ObjC EH puts on decls. */
10864 pedwarn (location, 0,
10865 "comparison of distinct pointer types lacks a cast");
10867 if (result_type == NULL_TREE)
10869 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10870 result_type = build_pointer_type
10871 (build_qualified_type (void_type_node, qual));
10874 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10876 result_type = type0;
10877 pedwarn (location, 0, "comparison between pointer and integer");
10879 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10881 result_type = type1;
10882 pedwarn (location, 0, "comparison between pointer and integer");
10884 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10885 || truth_value_p (TREE_CODE (orig_op0)))
10886 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10887 || truth_value_p (TREE_CODE (orig_op1))))
10888 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10895 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10898 if (!vector_types_compatible_elements_p (type0, type1))
10900 error_at (location, "comparing vectors with different "
10902 return error_mark_node;
10905 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10907 error_at (location, "comparing vectors with different "
10908 "number of elements");
10909 return error_mark_node;
10912 /* It's not precisely specified how the usual arithmetic
10913 conversions apply to the vector types. Here, we use
10914 the unsigned type if one of the operands is signed and
10915 the other one is unsigned. */
10916 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
10918 if (!TYPE_UNSIGNED (type0))
10919 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
10921 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
10922 warning_at (location, OPT_Wsign_compare, "comparison between "
10923 "types %qT and %qT", type0, type1);
10926 /* Always construct signed integer vector type. */
10927 intt = c_common_type_for_size (GET_MODE_BITSIZE
10928 (TYPE_MODE (TREE_TYPE (type0))), 0);
10929 result_type = build_opaque_vector_type (intt,
10930 TYPE_VECTOR_SUBPARTS (type0));
10934 build_type = integer_type_node;
10935 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10936 || code0 == FIXED_POINT_TYPE)
10937 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10938 || code1 == FIXED_POINT_TYPE))
10940 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10942 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10943 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10944 addr_space_t as_common;
10946 if (comp_target_types (location, type0, type1))
10948 result_type = common_pointer_type (type0, type1);
10949 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10950 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10951 pedwarn (location, 0,
10952 "comparison of complete and incomplete pointers");
10953 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10954 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10955 "ordered comparisons of pointers to functions");
10956 else if (null_pointer_constant_p (orig_op0)
10957 || null_pointer_constant_p (orig_op1))
10958 warning_at (location, OPT_Wextra,
10959 "ordered comparison of pointer with null pointer");
10962 else if (!addr_space_superset (as0, as1, &as_common))
10964 error_at (location, "comparison of pointers to "
10965 "disjoint address spaces");
10966 return error_mark_node;
10970 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10971 result_type = build_pointer_type
10972 (build_qualified_type (void_type_node, qual));
10973 pedwarn (location, 0,
10974 "comparison of distinct pointer types lacks a cast");
10977 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10979 result_type = type0;
10981 pedwarn (location, OPT_Wpedantic,
10982 "ordered comparison of pointer with integer zero");
10983 else if (extra_warnings)
10984 warning_at (location, OPT_Wextra,
10985 "ordered comparison of pointer with integer zero");
10987 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10989 result_type = type1;
10991 pedwarn (location, OPT_Wpedantic,
10992 "ordered comparison of pointer with integer zero");
10993 else if (extra_warnings)
10994 warning_at (location, OPT_Wextra,
10995 "ordered comparison of pointer with integer zero");
10997 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10999 result_type = type0;
11000 pedwarn (location, 0, "comparison between pointer and integer");
11002 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11004 result_type = type1;
11005 pedwarn (location, 0, "comparison between pointer and integer");
11007 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11008 || truth_value_p (TREE_CODE (orig_op0)))
11009 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11010 || truth_value_p (TREE_CODE (orig_op1))))
11011 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11015 gcc_unreachable ();
11018 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11019 return error_mark_node;
11021 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11022 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11023 || !vector_types_compatible_elements_p (type0, type1)))
11025 binary_op_error (location, code, type0, type1);
11026 return error_mark_node;
11029 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11030 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11032 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11033 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11035 bool first_complex = (code0 == COMPLEX_TYPE);
11036 bool second_complex = (code1 == COMPLEX_TYPE);
11037 int none_complex = (!first_complex && !second_complex);
11039 if (shorten || common || short_compare)
11041 result_type = c_common_type (type0, type1);
11042 do_warn_double_promotion (result_type, type0, type1,
11043 "implicit conversion from %qT to %qT "
11044 "to match other operand of binary "
11047 if (result_type == error_mark_node)
11048 return error_mark_node;
11051 if (first_complex != second_complex
11052 && (code == PLUS_EXPR
11053 || code == MINUS_EXPR
11054 || code == MULT_EXPR
11055 || (code == TRUNC_DIV_EXPR && first_complex))
11056 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11057 && flag_signed_zeros)
11059 /* An operation on mixed real/complex operands must be
11060 handled specially, but the language-independent code can
11061 more easily optimize the plain complex arithmetic if
11062 -fno-signed-zeros. */
11063 tree real_type = TREE_TYPE (result_type);
11065 if (type0 != orig_type0 || type1 != orig_type1)
11067 gcc_assert (may_need_excess_precision && common);
11068 semantic_result_type = c_common_type (orig_type0, orig_type1);
11072 if (TREE_TYPE (op0) != result_type)
11073 op0 = convert_and_check (location, result_type, op0);
11074 if (TREE_TYPE (op1) != real_type)
11075 op1 = convert_and_check (location, real_type, op1);
11079 if (TREE_TYPE (op0) != real_type)
11080 op0 = convert_and_check (location, real_type, op0);
11081 if (TREE_TYPE (op1) != result_type)
11082 op1 = convert_and_check (location, result_type, op1);
11084 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11085 return error_mark_node;
11088 op0 = c_save_expr (op0);
11089 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11091 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11096 case TRUNC_DIV_EXPR:
11097 op1 = c_save_expr (op1);
11098 imag = build2 (resultcode, real_type, imag, op1);
11099 /* Fall through. */
11102 real = build2 (resultcode, real_type, real, op1);
11110 op1 = c_save_expr (op1);
11111 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11113 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11118 op0 = c_save_expr (op0);
11119 imag = build2 (resultcode, real_type, op0, imag);
11120 /* Fall through. */
11122 real = build2 (resultcode, real_type, op0, real);
11125 real = build2 (resultcode, real_type, op0, real);
11126 imag = build1 (NEGATE_EXPR, real_type, imag);
11132 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11133 goto return_build_binary_op;
11136 /* For certain operations (which identify themselves by shorten != 0)
11137 if both args were extended from the same smaller type,
11138 do the arithmetic in that type and then extend.
11140 shorten !=0 and !=1 indicates a bitwise operation.
11141 For them, this optimization is safe only if
11142 both args are zero-extended or both are sign-extended.
11143 Otherwise, we might change the result.
11144 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11145 but calculated in (unsigned short) it would be (unsigned short)-1. */
11147 if (shorten && none_complex)
11149 final_type = result_type;
11150 result_type = shorten_binary_op (result_type, op0, op1,
11154 /* Shifts can be shortened if shifting right. */
11159 tree arg0 = get_narrower (op0, &unsigned_arg);
11161 final_type = result_type;
11163 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11164 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11166 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11167 && tree_int_cst_sgn (op1) > 0
11168 /* We can shorten only if the shift count is less than the
11169 number of bits in the smaller type size. */
11170 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11171 /* We cannot drop an unsigned shift after sign-extension. */
11172 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11174 /* Do an unsigned shift if the operand was zero-extended. */
11176 = c_common_signed_or_unsigned_type (unsigned_arg,
11178 /* Convert value-to-be-shifted to that type. */
11179 if (TREE_TYPE (op0) != result_type)
11180 op0 = convert (result_type, op0);
11185 /* Comparison operations are shortened too but differently.
11186 They identify themselves by setting short_compare = 1. */
11190 /* Don't write &op0, etc., because that would prevent op0
11191 from being kept in a register.
11192 Instead, make copies of the our local variables and
11193 pass the copies by reference, then copy them back afterward. */
11194 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11195 enum tree_code xresultcode = resultcode;
11197 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11203 goto return_build_binary_op;
11206 op0 = xop0, op1 = xop1;
11208 resultcode = xresultcode;
11210 if (c_inhibit_evaluation_warnings == 0)
11212 bool op0_maybe_const = true;
11213 bool op1_maybe_const = true;
11214 tree orig_op0_folded, orig_op1_folded;
11216 if (in_late_binary_op)
11218 orig_op0_folded = orig_op0;
11219 orig_op1_folded = orig_op1;
11223 /* Fold for the sake of possible warnings, as in
11224 build_conditional_expr. This requires the
11225 "original" values to be folded, not just op0 and
11227 c_inhibit_evaluation_warnings++;
11228 op0 = c_fully_fold (op0, require_constant_value,
11230 op1 = c_fully_fold (op1, require_constant_value,
11232 c_inhibit_evaluation_warnings--;
11233 orig_op0_folded = c_fully_fold (orig_op0,
11234 require_constant_value,
11236 orig_op1_folded = c_fully_fold (orig_op1,
11237 require_constant_value,
11241 if (warn_sign_compare)
11242 warn_for_sign_compare (location, orig_op0_folded,
11243 orig_op1_folded, op0, op1,
11244 result_type, resultcode);
11245 if (!in_late_binary_op && !int_operands)
11247 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11248 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11249 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11250 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11256 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11257 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11258 Then the expression will be built.
11259 It will be given type FINAL_TYPE if that is nonzero;
11260 otherwise, it will be given type RESULT_TYPE. */
11264 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11265 return error_mark_node;
11268 if (build_type == NULL_TREE)
11270 build_type = result_type;
11271 if ((type0 != orig_type0 || type1 != orig_type1)
11274 gcc_assert (may_need_excess_precision && common);
11275 semantic_result_type = c_common_type (orig_type0, orig_type1);
11281 op0 = ep_convert_and_check (location, result_type, op0,
11282 semantic_result_type);
11283 op1 = ep_convert_and_check (location, result_type, op1,
11284 semantic_result_type);
11286 /* This can happen if one operand has a vector type, and the other
11287 has a different type. */
11288 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11289 return error_mark_node;
11292 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11293 | SANITIZE_FLOAT_DIVIDE))
11294 && do_ubsan_in_current_function ()
11295 && (doing_div_or_mod || doing_shift))
11297 /* OP0 and/or OP1 might have side-effects. */
11298 op0 = c_save_expr (op0);
11299 op1 = c_save_expr (op1);
11300 op0 = c_fully_fold (op0, false, NULL);
11301 op1 = c_fully_fold (op1, false, NULL);
11302 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11303 | SANITIZE_FLOAT_DIVIDE)))
11304 instrument_expr = ubsan_instrument_division (location, op0, op1);
11305 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11306 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11309 /* Treat expressions in initializers specially as they can't trap. */
11310 if (int_const_or_overflow)
11311 ret = (require_constant_value
11312 ? fold_build2_initializer_loc (location, resultcode, build_type,
11314 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11316 ret = build2 (resultcode, build_type, op0, op1);
11317 if (final_type != 0)
11318 ret = convert (final_type, ret);
11320 return_build_binary_op:
11321 gcc_assert (ret != error_mark_node);
11322 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11323 ret = (int_operands
11324 ? note_integer_operands (ret)
11325 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11326 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11327 && !in_late_binary_op)
11328 ret = note_integer_operands (ret);
11329 if (semantic_result_type)
11330 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11331 protected_set_expr_location (ret, location);
11333 if (instrument_expr != NULL)
11334 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11335 instrument_expr, ret);
11341 /* Convert EXPR to be a truth-value, validating its type for this
11342 purpose. LOCATION is the source location for the expression. */
11345 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11347 bool int_const, int_operands;
11349 switch (TREE_CODE (TREE_TYPE (expr)))
11352 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11353 return error_mark_node;
11356 error_at (location, "used struct type value where scalar is required");
11357 return error_mark_node;
11360 error_at (location, "used union type value where scalar is required");
11361 return error_mark_node;
11364 error_at (location, "void value not ignored as it ought to be");
11365 return error_mark_node;
11367 case FUNCTION_TYPE:
11368 gcc_unreachable ();
11371 error_at (location, "used vector type where scalar is required");
11372 return error_mark_node;
11378 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11379 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11380 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11382 expr = remove_c_maybe_const_expr (expr);
11383 expr = build2 (NE_EXPR, integer_type_node, expr,
11384 convert (TREE_TYPE (expr), integer_zero_node));
11385 expr = note_integer_operands (expr);
11388 /* ??? Should we also give an error for vectors rather than leaving
11389 those to give errors later? */
11390 expr = c_common_truthvalue_conversion (location, expr);
11392 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11394 if (TREE_OVERFLOW (expr))
11397 return note_integer_operands (expr);
11399 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11400 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11405 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11409 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11411 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11413 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11414 /* Executing a compound literal inside a function reinitializes
11416 if (!TREE_STATIC (decl))
11424 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11425 statement. LOC is the location of the OACC_PARALLEL. */
11428 c_finish_oacc_parallel (location_t loc, tree clauses, tree block)
11432 block = c_end_compound_stmt (loc, block, true);
11434 stmt = make_node (OACC_PARALLEL);
11435 TREE_TYPE (stmt) = void_type_node;
11436 OACC_PARALLEL_CLAUSES (stmt) = clauses;
11437 OACC_PARALLEL_BODY (stmt) = block;
11438 SET_EXPR_LOCATION (stmt, loc);
11440 return add_stmt (stmt);
11443 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11444 statement. LOC is the location of the OACC_KERNELS. */
11447 c_finish_oacc_kernels (location_t loc, tree clauses, tree block)
11451 block = c_end_compound_stmt (loc, block, true);
11453 stmt = make_node (OACC_KERNELS);
11454 TREE_TYPE (stmt) = void_type_node;
11455 OACC_KERNELS_CLAUSES (stmt) = clauses;
11456 OACC_KERNELS_BODY (stmt) = block;
11457 SET_EXPR_LOCATION (stmt, loc);
11459 return add_stmt (stmt);
11462 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11463 statement. LOC is the location of the OACC_DATA. */
11466 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11470 block = c_end_compound_stmt (loc, block, true);
11472 stmt = make_node (OACC_DATA);
11473 TREE_TYPE (stmt) = void_type_node;
11474 OACC_DATA_CLAUSES (stmt) = clauses;
11475 OACC_DATA_BODY (stmt) = block;
11476 SET_EXPR_LOCATION (stmt, loc);
11478 return add_stmt (stmt);
11481 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11484 c_begin_omp_parallel (void)
11488 keep_next_level ();
11489 block = c_begin_compound_stmt (true);
11494 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11495 statement. LOC is the location of the OMP_PARALLEL. */
11498 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11502 block = c_end_compound_stmt (loc, block, true);
11504 stmt = make_node (OMP_PARALLEL);
11505 TREE_TYPE (stmt) = void_type_node;
11506 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11507 OMP_PARALLEL_BODY (stmt) = block;
11508 SET_EXPR_LOCATION (stmt, loc);
11510 return add_stmt (stmt);
11513 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11516 c_begin_omp_task (void)
11520 keep_next_level ();
11521 block = c_begin_compound_stmt (true);
11526 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11527 statement. LOC is the location of the #pragma. */
11530 c_finish_omp_task (location_t loc, tree clauses, tree block)
11534 block = c_end_compound_stmt (loc, block, true);
11536 stmt = make_node (OMP_TASK);
11537 TREE_TYPE (stmt) = void_type_node;
11538 OMP_TASK_CLAUSES (stmt) = clauses;
11539 OMP_TASK_BODY (stmt) = block;
11540 SET_EXPR_LOCATION (stmt, loc);
11542 return add_stmt (stmt);
11545 /* Generate GOMP_cancel call for #pragma omp cancel. */
11548 c_finish_omp_cancel (location_t loc, tree clauses)
11550 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11552 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11554 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11556 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11558 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11562 error_at (loc, "%<#pragma omp cancel must specify one of "
11563 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11567 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11568 if (ifc != NULL_TREE)
11570 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11571 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11572 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11573 build_zero_cst (type));
11576 ifc = boolean_true_node;
11577 tree stmt = build_call_expr_loc (loc, fn, 2,
11578 build_int_cst (integer_type_node, mask),
11583 /* Generate GOMP_cancellation_point call for
11584 #pragma omp cancellation point. */
11587 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11589 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11591 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11593 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11595 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11597 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11601 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11602 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11606 tree stmt = build_call_expr_loc (loc, fn, 1,
11607 build_int_cst (integer_type_node, mask));
11611 /* Helper function for handle_omp_array_sections. Called recursively
11612 to handle multiple array-section-subscripts. C is the clause,
11613 T current expression (initially OMP_CLAUSE_DECL), which is either
11614 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11615 expression if specified, TREE_VALUE length expression if specified,
11616 TREE_CHAIN is what it has been specified after, or some decl.
11617 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11618 set to true if any of the array-section-subscript could have length
11619 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11620 first array-section-subscript which is known not to have length
11622 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11623 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11624 all are or may have length of 1, array-section-subscript [:2] is the
11625 first one knonwn not to have length 1. For array-section-subscript
11626 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11627 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11628 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11629 case though, as some lengths could be zero. */
11632 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11633 bool &maybe_zero_len, unsigned int &first_non_one)
11635 tree ret, low_bound, length, type;
11636 if (TREE_CODE (t) != TREE_LIST)
11638 if (error_operand_p (t))
11639 return error_mark_node;
11640 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11643 error_at (OMP_CLAUSE_LOCATION (c),
11644 "%qD is not a variable in %qs clause", t,
11645 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11647 error_at (OMP_CLAUSE_LOCATION (c),
11648 "%qE is not a variable in %qs clause", t,
11649 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11650 return error_mark_node;
11652 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11653 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11655 error_at (OMP_CLAUSE_LOCATION (c),
11656 "%qD is threadprivate variable in %qs clause", t,
11657 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11658 return error_mark_node;
11663 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11664 maybe_zero_len, first_non_one);
11665 if (ret == error_mark_node || ret == NULL_TREE)
11668 type = TREE_TYPE (ret);
11669 low_bound = TREE_PURPOSE (t);
11670 length = TREE_VALUE (t);
11672 if (low_bound == error_mark_node || length == error_mark_node)
11673 return error_mark_node;
11675 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11677 error_at (OMP_CLAUSE_LOCATION (c),
11678 "low bound %qE of array section does not have integral type",
11680 return error_mark_node;
11682 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11684 error_at (OMP_CLAUSE_LOCATION (c),
11685 "length %qE of array section does not have integral type",
11687 return error_mark_node;
11690 && TREE_CODE (low_bound) == INTEGER_CST
11691 && TYPE_PRECISION (TREE_TYPE (low_bound))
11692 > TYPE_PRECISION (sizetype))
11693 low_bound = fold_convert (sizetype, low_bound);
11695 && TREE_CODE (length) == INTEGER_CST
11696 && TYPE_PRECISION (TREE_TYPE (length))
11697 > TYPE_PRECISION (sizetype))
11698 length = fold_convert (sizetype, length);
11699 if (low_bound == NULL_TREE)
11700 low_bound = integer_zero_node;
11702 if (length != NULL_TREE)
11704 if (!integer_nonzerop (length))
11705 maybe_zero_len = true;
11706 if (first_non_one == types.length ()
11707 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11710 if (TREE_CODE (type) == ARRAY_TYPE)
11712 if (length == NULL_TREE
11713 && (TYPE_DOMAIN (type) == NULL_TREE
11714 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11716 error_at (OMP_CLAUSE_LOCATION (c),
11717 "for unknown bound array type length expression must "
11719 return error_mark_node;
11721 if (TREE_CODE (low_bound) == INTEGER_CST
11722 && tree_int_cst_sgn (low_bound) == -1)
11724 error_at (OMP_CLAUSE_LOCATION (c),
11725 "negative low bound in array section in %qs clause",
11726 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11727 return error_mark_node;
11729 if (length != NULL_TREE
11730 && TREE_CODE (length) == INTEGER_CST
11731 && tree_int_cst_sgn (length) == -1)
11733 error_at (OMP_CLAUSE_LOCATION (c),
11734 "negative length in array section in %qs clause",
11735 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11736 return error_mark_node;
11738 if (TYPE_DOMAIN (type)
11739 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11740 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11743 tree size = size_binop (PLUS_EXPR,
11744 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11746 if (TREE_CODE (low_bound) == INTEGER_CST)
11748 if (tree_int_cst_lt (size, low_bound))
11750 error_at (OMP_CLAUSE_LOCATION (c),
11751 "low bound %qE above array section size "
11752 "in %qs clause", low_bound,
11753 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11754 return error_mark_node;
11756 if (tree_int_cst_equal (size, low_bound))
11757 maybe_zero_len = true;
11758 else if (length == NULL_TREE
11759 && first_non_one == types.length ()
11760 && tree_int_cst_equal
11761 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11765 else if (length == NULL_TREE)
11767 maybe_zero_len = true;
11768 if (first_non_one == types.length ())
11771 if (length && TREE_CODE (length) == INTEGER_CST)
11773 if (tree_int_cst_lt (size, length))
11775 error_at (OMP_CLAUSE_LOCATION (c),
11776 "length %qE above array section size "
11777 "in %qs clause", length,
11778 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11779 return error_mark_node;
11781 if (TREE_CODE (low_bound) == INTEGER_CST)
11784 = size_binop (PLUS_EXPR,
11785 fold_convert (sizetype, low_bound),
11786 fold_convert (sizetype, length));
11787 if (TREE_CODE (lbpluslen) == INTEGER_CST
11788 && tree_int_cst_lt (size, lbpluslen))
11790 error_at (OMP_CLAUSE_LOCATION (c),
11791 "high bound %qE above array section size "
11792 "in %qs clause", lbpluslen,
11793 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11794 return error_mark_node;
11799 else if (length == NULL_TREE)
11801 maybe_zero_len = true;
11802 if (first_non_one == types.length ())
11806 /* For [lb:] we will need to evaluate lb more than once. */
11807 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11809 tree lb = c_save_expr (low_bound);
11810 if (lb != low_bound)
11812 TREE_PURPOSE (t) = lb;
11817 else if (TREE_CODE (type) == POINTER_TYPE)
11819 if (length == NULL_TREE)
11821 error_at (OMP_CLAUSE_LOCATION (c),
11822 "for pointer type length expression must be specified");
11823 return error_mark_node;
11825 /* If there is a pointer type anywhere but in the very first
11826 array-section-subscript, the array section can't be contiguous. */
11827 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11828 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11830 error_at (OMP_CLAUSE_LOCATION (c),
11831 "array section is not contiguous in %qs clause",
11832 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11833 return error_mark_node;
11838 error_at (OMP_CLAUSE_LOCATION (c),
11839 "%qE does not have pointer or array type", ret);
11840 return error_mark_node;
11842 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11843 types.safe_push (TREE_TYPE (ret));
11844 /* We will need to evaluate lb more than once. */
11845 tree lb = c_save_expr (low_bound);
11846 if (lb != low_bound)
11848 TREE_PURPOSE (t) = lb;
11851 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11855 /* Handle array sections for clause C. */
11858 handle_omp_array_sections (tree c)
11860 bool maybe_zero_len = false;
11861 unsigned int first_non_one = 0;
11862 vec<tree> types = vNULL;
11863 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11864 maybe_zero_len, first_non_one);
11865 if (first == error_mark_node)
11870 if (first == NULL_TREE)
11875 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11877 tree t = OMP_CLAUSE_DECL (c);
11878 tree tem = NULL_TREE;
11880 /* Need to evaluate side effects in the length expressions
11882 while (TREE_CODE (t) == TREE_LIST)
11884 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11886 if (tem == NULL_TREE)
11887 tem = TREE_VALUE (t);
11889 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11890 TREE_VALUE (t), tem);
11892 t = TREE_CHAIN (t);
11895 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11896 first = c_fully_fold (first, false, NULL);
11897 OMP_CLAUSE_DECL (c) = first;
11901 unsigned int num = types.length (), i;
11902 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11903 tree condition = NULL_TREE;
11905 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11906 maybe_zero_len = true;
11908 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11909 t = TREE_CHAIN (t))
11911 tree low_bound = TREE_PURPOSE (t);
11912 tree length = TREE_VALUE (t);
11916 && TREE_CODE (low_bound) == INTEGER_CST
11917 && TYPE_PRECISION (TREE_TYPE (low_bound))
11918 > TYPE_PRECISION (sizetype))
11919 low_bound = fold_convert (sizetype, low_bound);
11921 && TREE_CODE (length) == INTEGER_CST
11922 && TYPE_PRECISION (TREE_TYPE (length))
11923 > TYPE_PRECISION (sizetype))
11924 length = fold_convert (sizetype, length);
11925 if (low_bound == NULL_TREE)
11926 low_bound = integer_zero_node;
11927 if (!maybe_zero_len && i > first_non_one)
11929 if (integer_nonzerop (low_bound))
11930 goto do_warn_noncontiguous;
11931 if (length != NULL_TREE
11932 && TREE_CODE (length) == INTEGER_CST
11933 && TYPE_DOMAIN (types[i])
11934 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11935 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11939 size = size_binop (PLUS_EXPR,
11940 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11942 if (!tree_int_cst_equal (length, size))
11944 do_warn_noncontiguous:
11945 error_at (OMP_CLAUSE_LOCATION (c),
11946 "array section is not contiguous in %qs "
11948 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11953 if (length != NULL_TREE
11954 && TREE_SIDE_EFFECTS (length))
11956 if (side_effects == NULL_TREE)
11957 side_effects = length;
11959 side_effects = build2 (COMPOUND_EXPR,
11960 TREE_TYPE (side_effects),
11961 length, side_effects);
11968 if (i > first_non_one && length && integer_nonzerop (length))
11971 l = fold_convert (sizetype, length);
11974 l = size_binop (PLUS_EXPR,
11975 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11977 l = size_binop (MINUS_EXPR, l,
11978 fold_convert (sizetype, low_bound));
11980 if (i > first_non_one)
11982 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11984 if (condition == NULL_TREE)
11987 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11990 else if (size == NULL_TREE)
11992 size = size_in_bytes (TREE_TYPE (types[i]));
11993 size = size_binop (MULT_EXPR, size, l);
11995 size = fold_build3 (COND_EXPR, sizetype, condition,
11996 size, size_zero_node);
11999 size = size_binop (MULT_EXPR, size, l);
12004 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12005 first = c_fully_fold (first, false, NULL);
12006 OMP_CLAUSE_DECL (c) = first;
12008 size = c_fully_fold (size, false, NULL);
12009 OMP_CLAUSE_SIZE (c) = size;
12010 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12012 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12013 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12014 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12015 if (!c_mark_addressable (t))
12017 OMP_CLAUSE_DECL (c2) = t;
12018 t = build_fold_addr_expr (first);
12019 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12020 tree ptr = OMP_CLAUSE_DECL (c2);
12021 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12022 ptr = build_fold_addr_expr (ptr);
12023 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12024 ptrdiff_type_node, t,
12025 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12026 ptrdiff_type_node, ptr));
12027 t = c_fully_fold (t, false, NULL);
12028 OMP_CLAUSE_SIZE (c2) = t;
12029 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12030 OMP_CLAUSE_CHAIN (c) = c2;
12035 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12036 an inline call. But, remap
12037 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12038 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12041 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12042 tree decl, tree placeholder)
12045 hash_map<tree, tree> decl_map;
12047 decl_map.put (omp_decl1, placeholder);
12048 decl_map.put (omp_decl2, decl);
12049 memset (&id, 0, sizeof (id));
12050 id.src_fn = DECL_CONTEXT (omp_decl1);
12051 id.dst_fn = current_function_decl;
12052 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12053 id.decl_map = &decl_map;
12055 id.copy_decl = copy_decl_no_change;
12056 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12057 id.transform_new_cfg = true;
12058 id.transform_return_to_modify = false;
12059 id.transform_lang_insert_block = NULL;
12061 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12065 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12066 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12069 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12071 if (*tp == (tree) data)
12076 /* For all elements of CLAUSES, validate them against their constraints.
12077 Remove any elements from the list that are invalid. */
12080 c_finish_omp_clauses (tree clauses)
12082 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12083 bitmap_head aligned_head;
12085 bool branch_seen = false;
12086 bool copyprivate_seen = false;
12087 tree *nowait_clause = NULL;
12089 bitmap_obstack_initialize (NULL);
12090 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12091 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12092 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12093 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12095 for (pc = &clauses, c = clauses; c ; c = *pc)
12097 bool remove = false;
12098 bool need_complete = false;
12099 bool need_implicitly_determined = false;
12101 switch (OMP_CLAUSE_CODE (c))
12103 case OMP_CLAUSE_SHARED:
12104 need_implicitly_determined = true;
12105 goto check_dup_generic;
12107 case OMP_CLAUSE_PRIVATE:
12108 need_complete = true;
12109 need_implicitly_determined = true;
12110 goto check_dup_generic;
12112 case OMP_CLAUSE_REDUCTION:
12113 need_implicitly_determined = true;
12114 t = OMP_CLAUSE_DECL (c);
12115 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12116 && (FLOAT_TYPE_P (TREE_TYPE (t))
12117 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
12119 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12120 const char *r_name = NULL;
12129 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12133 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12145 case TRUTH_ANDIF_EXPR:
12146 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12149 case TRUTH_ORIF_EXPR:
12150 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12154 gcc_unreachable ();
12158 error_at (OMP_CLAUSE_LOCATION (c),
12159 "%qE has invalid type for %<reduction(%s)%>",
12165 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12167 error_at (OMP_CLAUSE_LOCATION (c),
12168 "user defined reduction not found for %qD", t);
12172 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12174 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12175 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12176 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12177 VAR_DECL, NULL_TREE, type);
12178 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12179 DECL_ARTIFICIAL (placeholder) = 1;
12180 DECL_IGNORED_P (placeholder) = 1;
12181 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12182 c_mark_addressable (placeholder);
12183 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12184 c_mark_addressable (OMP_CLAUSE_DECL (c));
12185 OMP_CLAUSE_REDUCTION_MERGE (c)
12186 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12187 TREE_VEC_ELT (list, 0),
12188 TREE_VEC_ELT (list, 1),
12189 OMP_CLAUSE_DECL (c), placeholder);
12190 OMP_CLAUSE_REDUCTION_MERGE (c)
12191 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12192 void_type_node, NULL_TREE,
12193 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12194 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12195 if (TREE_VEC_LENGTH (list) == 6)
12197 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12198 c_mark_addressable (OMP_CLAUSE_DECL (c));
12199 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12200 c_mark_addressable (placeholder);
12201 tree init = TREE_VEC_ELT (list, 5);
12202 if (init == error_mark_node)
12203 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12204 OMP_CLAUSE_REDUCTION_INIT (c)
12205 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12206 TREE_VEC_ELT (list, 3),
12207 OMP_CLAUSE_DECL (c), placeholder);
12208 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12209 OMP_CLAUSE_REDUCTION_INIT (c)
12210 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12211 OMP_CLAUSE_REDUCTION_INIT (c));
12212 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12213 c_find_omp_placeholder_r,
12214 placeholder, NULL))
12215 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12220 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12221 init = build_constructor (TREE_TYPE (t), NULL);
12223 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12224 OMP_CLAUSE_REDUCTION_INIT (c)
12225 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12227 OMP_CLAUSE_REDUCTION_INIT (c)
12228 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12229 void_type_node, NULL_TREE,
12230 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12231 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12233 goto check_dup_generic;
12235 case OMP_CLAUSE_COPYPRIVATE:
12236 copyprivate_seen = true;
12239 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12240 "%<nowait%> clause must not be used together "
12241 "with %<copyprivate%>");
12242 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12243 nowait_clause = NULL;
12245 goto check_dup_generic;
12247 case OMP_CLAUSE_COPYIN:
12248 t = OMP_CLAUSE_DECL (c);
12249 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12251 error_at (OMP_CLAUSE_LOCATION (c),
12252 "%qE must be %<threadprivate%> for %<copyin%>", t);
12256 goto check_dup_generic;
12258 case OMP_CLAUSE_LINEAR:
12259 t = OMP_CLAUSE_DECL (c);
12260 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12261 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12263 error_at (OMP_CLAUSE_LOCATION (c),
12264 "linear clause applied to non-integral non-pointer "
12265 "variable with type %qT", TREE_TYPE (t));
12269 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12271 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12272 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12273 OMP_CLAUSE_DECL (c), s);
12274 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12275 sizetype, s, OMP_CLAUSE_DECL (c));
12276 if (s == error_mark_node)
12278 OMP_CLAUSE_LINEAR_STEP (c) = s;
12281 OMP_CLAUSE_LINEAR_STEP (c)
12282 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12283 goto check_dup_generic;
12286 t = OMP_CLAUSE_DECL (c);
12287 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12289 error_at (OMP_CLAUSE_LOCATION (c),
12290 "%qE is not a variable in clause %qs", t,
12291 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12294 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12295 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12296 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12298 error_at (OMP_CLAUSE_LOCATION (c),
12299 "%qE appears more than once in data clauses", t);
12303 bitmap_set_bit (&generic_head, DECL_UID (t));
12306 case OMP_CLAUSE_FIRSTPRIVATE:
12307 t = OMP_CLAUSE_DECL (c);
12308 need_complete = true;
12309 need_implicitly_determined = true;
12310 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12312 error_at (OMP_CLAUSE_LOCATION (c),
12313 "%qE is not a variable in clause %<firstprivate%>", t);
12316 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12317 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12319 error_at (OMP_CLAUSE_LOCATION (c),
12320 "%qE appears more than once in data clauses", t);
12324 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12327 case OMP_CLAUSE_LASTPRIVATE:
12328 t = OMP_CLAUSE_DECL (c);
12329 need_complete = true;
12330 need_implicitly_determined = true;
12331 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12333 error_at (OMP_CLAUSE_LOCATION (c),
12334 "%qE is not a variable in clause %<lastprivate%>", t);
12337 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12338 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12340 error_at (OMP_CLAUSE_LOCATION (c),
12341 "%qE appears more than once in data clauses", t);
12345 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12348 case OMP_CLAUSE_ALIGNED:
12349 t = OMP_CLAUSE_DECL (c);
12350 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12352 error_at (OMP_CLAUSE_LOCATION (c),
12353 "%qE is not a variable in %<aligned%> clause", t);
12356 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12357 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12359 error_at (OMP_CLAUSE_LOCATION (c),
12360 "%qE in %<aligned%> clause is neither a pointer nor "
12364 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12366 error_at (OMP_CLAUSE_LOCATION (c),
12367 "%qE appears more than once in %<aligned%> clauses",
12372 bitmap_set_bit (&aligned_head, DECL_UID (t));
12375 case OMP_CLAUSE_DEPEND:
12376 t = OMP_CLAUSE_DECL (c);
12377 if (TREE_CODE (t) == TREE_LIST)
12379 if (handle_omp_array_sections (c))
12383 if (t == error_mark_node)
12385 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12387 error_at (OMP_CLAUSE_LOCATION (c),
12388 "%qE is not a variable in %<depend%> clause", t);
12391 else if (!c_mark_addressable (t))
12395 case OMP_CLAUSE_MAP:
12396 case OMP_CLAUSE_TO:
12397 case OMP_CLAUSE_FROM:
12398 case OMP_CLAUSE__CACHE_:
12399 t = OMP_CLAUSE_DECL (c);
12400 if (TREE_CODE (t) == TREE_LIST)
12402 if (handle_omp_array_sections (c))
12406 t = OMP_CLAUSE_DECL (c);
12407 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12409 error_at (OMP_CLAUSE_LOCATION (c),
12410 "array section does not have mappable type "
12412 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12418 if (t == error_mark_node)
12420 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12422 error_at (OMP_CLAUSE_LOCATION (c),
12423 "%qE is not a variable in %qs clause", t,
12424 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12427 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12429 error_at (OMP_CLAUSE_LOCATION (c),
12430 "%qD is threadprivate variable in %qs clause", t,
12431 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12434 else if (!c_mark_addressable (t))
12436 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12437 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12438 || (OMP_CLAUSE_MAP_KIND (c)
12439 == GOMP_MAP_FORCE_DEVICEPTR)))
12440 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12442 error_at (OMP_CLAUSE_LOCATION (c),
12443 "%qD does not have a mappable type in %qs clause", t,
12444 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12447 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12449 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12450 error ("%qD appears more than once in motion clauses", t);
12452 error ("%qD appears more than once in map clauses", t);
12456 bitmap_set_bit (&generic_head, DECL_UID (t));
12459 case OMP_CLAUSE_UNIFORM:
12460 t = OMP_CLAUSE_DECL (c);
12461 if (TREE_CODE (t) != PARM_DECL)
12464 error_at (OMP_CLAUSE_LOCATION (c),
12465 "%qD is not an argument in %<uniform%> clause", t);
12467 error_at (OMP_CLAUSE_LOCATION (c),
12468 "%qE is not an argument in %<uniform%> clause", t);
12472 goto check_dup_generic;
12474 case OMP_CLAUSE_NOWAIT:
12475 if (copyprivate_seen)
12477 error_at (OMP_CLAUSE_LOCATION (c),
12478 "%<nowait%> clause must not be used together "
12479 "with %<copyprivate%>");
12483 nowait_clause = pc;
12484 pc = &OMP_CLAUSE_CHAIN (c);
12487 case OMP_CLAUSE_IF:
12488 case OMP_CLAUSE_NUM_THREADS:
12489 case OMP_CLAUSE_NUM_TEAMS:
12490 case OMP_CLAUSE_THREAD_LIMIT:
12491 case OMP_CLAUSE_SCHEDULE:
12492 case OMP_CLAUSE_ORDERED:
12493 case OMP_CLAUSE_DEFAULT:
12494 case OMP_CLAUSE_UNTIED:
12495 case OMP_CLAUSE_COLLAPSE:
12496 case OMP_CLAUSE_FINAL:
12497 case OMP_CLAUSE_MERGEABLE:
12498 case OMP_CLAUSE_SAFELEN:
12499 case OMP_CLAUSE_SIMDLEN:
12500 case OMP_CLAUSE_DEVICE:
12501 case OMP_CLAUSE_DIST_SCHEDULE:
12502 case OMP_CLAUSE_PARALLEL:
12503 case OMP_CLAUSE_FOR:
12504 case OMP_CLAUSE_SECTIONS:
12505 case OMP_CLAUSE_TASKGROUP:
12506 case OMP_CLAUSE_PROC_BIND:
12507 case OMP_CLAUSE__CILK_FOR_COUNT_:
12508 case OMP_CLAUSE_NUM_GANGS:
12509 case OMP_CLAUSE_NUM_WORKERS:
12510 case OMP_CLAUSE_VECTOR_LENGTH:
12511 case OMP_CLAUSE_ASYNC:
12512 case OMP_CLAUSE_WAIT:
12513 case OMP_CLAUSE_AUTO:
12514 case OMP_CLAUSE_SEQ:
12515 case OMP_CLAUSE_GANG:
12516 case OMP_CLAUSE_WORKER:
12517 case OMP_CLAUSE_VECTOR:
12518 pc = &OMP_CLAUSE_CHAIN (c);
12521 case OMP_CLAUSE_INBRANCH:
12522 case OMP_CLAUSE_NOTINBRANCH:
12525 error_at (OMP_CLAUSE_LOCATION (c),
12526 "%<inbranch%> clause is incompatible with "
12527 "%<notinbranch%>");
12531 branch_seen = true;
12532 pc = &OMP_CLAUSE_CHAIN (c);
12536 gcc_unreachable ();
12541 t = OMP_CLAUSE_DECL (c);
12545 t = require_complete_type (t);
12546 if (t == error_mark_node)
12550 if (need_implicitly_determined)
12552 const char *share_name = NULL;
12554 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12555 share_name = "threadprivate";
12556 else switch (c_omp_predetermined_sharing (t))
12558 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12560 case OMP_CLAUSE_DEFAULT_SHARED:
12561 /* const vars may be specified in firstprivate clause. */
12562 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12563 && TREE_READONLY (t))
12565 share_name = "shared";
12567 case OMP_CLAUSE_DEFAULT_PRIVATE:
12568 share_name = "private";
12571 gcc_unreachable ();
12575 error_at (OMP_CLAUSE_LOCATION (c),
12576 "%qE is predetermined %qs for %qs",
12578 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12585 *pc = OMP_CLAUSE_CHAIN (c);
12587 pc = &OMP_CLAUSE_CHAIN (c);
12590 bitmap_obstack_release (NULL);
12594 /* Create a transaction node. */
12597 c_finish_transaction (location_t loc, tree block, int flags)
12599 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12600 if (flags & TM_STMT_ATTR_OUTER)
12601 TRANSACTION_EXPR_OUTER (stmt) = 1;
12602 if (flags & TM_STMT_ATTR_RELAXED)
12603 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12604 return add_stmt (stmt);
12607 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12608 down to the element type of an array. If ORIG_QUAL_TYPE is not
12609 NULL, then it should be used as the qualified type
12610 ORIG_QUAL_INDIRECT levels down in array type derivation (to
12611 preserve information about the typedef name from which an array
12612 type was derived). */
12615 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
12616 size_t orig_qual_indirect)
12618 if (type == error_mark_node)
12621 if (TREE_CODE (type) == ARRAY_TYPE)
12624 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12625 type_quals, orig_qual_type,
12626 orig_qual_indirect - 1);
12628 /* See if we already have an identically qualified type. */
12629 if (orig_qual_type && orig_qual_indirect == 0)
12630 t = orig_qual_type;
12632 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12634 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12635 && TYPE_NAME (t) == TYPE_NAME (type)
12636 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12637 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12638 TYPE_ATTRIBUTES (type)))
12643 tree domain = TYPE_DOMAIN (type);
12645 t = build_variant_type_copy (type);
12646 TREE_TYPE (t) = element_type;
12648 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12649 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12650 SET_TYPE_STRUCTURAL_EQUALITY (t);
12651 else if (TYPE_CANONICAL (element_type) != element_type
12652 || (domain && TYPE_CANONICAL (domain) != domain))
12654 tree unqualified_canon
12655 = build_array_type (TYPE_CANONICAL (element_type),
12656 domain? TYPE_CANONICAL (domain)
12659 = c_build_qualified_type (unqualified_canon, type_quals);
12662 TYPE_CANONICAL (t) = t;
12667 /* A restrict-qualified pointer type must be a pointer to object or
12668 incomplete type. Note that the use of POINTER_TYPE_P also allows
12669 REFERENCE_TYPEs, which is appropriate for C++. */
12670 if ((type_quals & TYPE_QUAL_RESTRICT)
12671 && (!POINTER_TYPE_P (type)
12672 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12674 error ("invalid use of %<restrict%>");
12675 type_quals &= ~TYPE_QUAL_RESTRICT;
12678 tree var_type = (orig_qual_type && orig_qual_indirect == 0
12680 : build_qualified_type (type, type_quals));
12684 /* Build a VA_ARG_EXPR for the C parser. */
12687 c_build_va_arg (location_t loc, tree expr, tree type)
12689 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12690 warning_at (loc, OPT_Wc___compat,
12691 "C++ requires promoted type, not enum type, in %<va_arg%>");
12692 return build_va_arg (loc, expr, type);
12695 /* Return truthvalue of whether T1 is the same tree structure as T2.
12696 Return 1 if they are the same. Return 0 if they are different. */
12699 c_tree_equal (tree t1, tree t2)
12701 enum tree_code code1, code2;
12708 for (code1 = TREE_CODE (t1);
12709 CONVERT_EXPR_CODE_P (code1)
12710 || code1 == NON_LVALUE_EXPR;
12711 code1 = TREE_CODE (t1))
12712 t1 = TREE_OPERAND (t1, 0);
12713 for (code2 = TREE_CODE (t2);
12714 CONVERT_EXPR_CODE_P (code2)
12715 || code2 == NON_LVALUE_EXPR;
12716 code2 = TREE_CODE (t2))
12717 t2 = TREE_OPERAND (t2, 0);
12719 /* They might have become equal now. */
12723 if (code1 != code2)
12729 return wi::eq_p (t1, t2);
12732 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12735 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12736 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12737 TREE_STRING_LENGTH (t1));
12740 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12741 TREE_FIXED_CST (t2));
12744 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12745 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12748 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12751 /* We need to do this when determining whether or not two
12752 non-type pointer to member function template arguments
12754 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12755 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12760 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12762 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12763 if (!c_tree_equal (field, elt2->index)
12764 || !c_tree_equal (value, elt2->value))
12771 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12773 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12775 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12778 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12783 call_expr_arg_iterator iter1, iter2;
12784 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12786 for (arg1 = first_call_expr_arg (t1, &iter1),
12787 arg2 = first_call_expr_arg (t2, &iter2);
12789 arg1 = next_call_expr_arg (&iter1),
12790 arg2 = next_call_expr_arg (&iter2))
12791 if (!c_tree_equal (arg1, arg2))
12800 tree o1 = TREE_OPERAND (t1, 0);
12801 tree o2 = TREE_OPERAND (t2, 0);
12803 /* Special case: if either target is an unallocated VAR_DECL,
12804 it means that it's going to be unified with whatever the
12805 TARGET_EXPR is really supposed to initialize, so treat it
12806 as being equivalent to anything. */
12807 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12808 && !DECL_RTL_SET_P (o1))
12810 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12811 && !DECL_RTL_SET_P (o2))
12813 else if (!c_tree_equal (o1, o2))
12816 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12819 case COMPONENT_REF:
12820 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12822 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12828 case FUNCTION_DECL:
12829 case IDENTIFIER_NODE:
12836 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12838 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12839 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12840 TREE_VEC_ELT (t2, ix)))
12849 switch (TREE_CODE_CLASS (code1))
12853 case tcc_comparison:
12854 case tcc_expression:
12856 case tcc_reference:
12857 case tcc_statement:
12859 int i, n = TREE_OPERAND_LENGTH (t1);
12863 case PREINCREMENT_EXPR:
12864 case PREDECREMENT_EXPR:
12865 case POSTINCREMENT_EXPR:
12866 case POSTDECREMENT_EXPR:
12876 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12877 && n != TREE_OPERAND_LENGTH (t2))
12880 for (i = 0; i < n; ++i)
12881 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12888 return comptypes (t1, t2);
12890 gcc_unreachable ();
12892 /* We can get here with --disable-checking. */
12896 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12897 spawn-helper and BODY is the newly created body for FNDECL. */
12900 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12902 tree list = alloc_stmt_list ();
12903 tree frame = make_cilk_frame (fndecl);
12904 tree dtor = create_cilk_function_exit (frame, false, true);
12905 add_local_decl (cfun, frame);
12907 DECL_SAVED_TREE (fndecl) = list;
12908 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12910 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12911 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12913 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12914 append_to_statement_list (detach_expr, &body_list);
12916 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12917 body = fold_build_cleanup_point_expr (void_type_node, body);
12919 append_to_statement_list (body, &body_list);
12920 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12921 body_list, dtor), &list);