* Makefile.in (C_COMMON_OBJS): Depend on c-cilkplus.o.
[official-gcc.git] / gcc / c / c-typeck.c
bloba823f1439f3e77bf7c954724be6fa9a8b02769b8
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2013 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
9 version.
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
14 for more details.
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. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "langhooks.h"
32 #include "c-tree.h"
33 #include "c-lang.h"
34 #include "flags.h"
35 #include "intl.h"
36 #include "target.h"
37 #include "tree-iterator.h"
38 #include "bitmap.h"
39 #include "gimple.h"
40 #include "gimplify.h"
41 #include "tree-inline.h"
42 #include "omp-low.h"
43 #include "c-family/c-objc.h"
44 #include "c-family/c-common.h"
45 #include "c-family/c-ubsan.h"
47 /* Possible cases of implicit bad conversions. Used to select
48 diagnostic messages in convert_for_assignment. */
49 enum impl_conv {
50 ic_argpass,
51 ic_assign,
52 ic_init,
53 ic_return
56 /* The level of nesting inside "__alignof__". */
57 int in_alignof;
59 /* The level of nesting inside "sizeof". */
60 int in_sizeof;
62 /* The level of nesting inside "typeof". */
63 int in_typeof;
65 /* The argument of last parsed sizeof expression, only to be tested
66 if expr.original_code == SIZEOF_EXPR. */
67 tree c_last_sizeof_arg;
69 /* Nonzero if we've already printed a "missing braces around initializer"
70 message within this initializer. */
71 static int missing_braces_mentioned;
73 static int require_constant_value;
74 static int require_constant_elements;
76 static bool null_pointer_constant_p (const_tree);
77 static tree qualify_type (tree, tree);
78 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
79 bool *);
80 static int comp_target_types (location_t, tree, tree);
81 static int function_types_compatible_p (const_tree, const_tree, bool *,
82 bool *);
83 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
84 static tree lookup_field (tree, tree);
85 static int convert_arguments (tree, vec<tree, va_gc> *, vec<tree, va_gc> *,
86 tree, tree);
87 static tree pointer_diff (location_t, tree, tree);
88 static tree convert_for_assignment (location_t, tree, tree, tree,
89 enum impl_conv, bool, tree, tree, int);
90 static tree valid_compound_expr_initializer (tree, tree);
91 static void push_string (const char *);
92 static void push_member_name (tree);
93 static int spelling_length (void);
94 static char *print_spelling (char *);
95 static void warning_init (int, const char *);
96 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
97 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
98 struct obstack *);
99 static void output_pending_init_elements (int, struct obstack *);
100 static int set_designator (int, struct obstack *);
101 static void push_range_stack (tree, struct obstack *);
102 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
103 static void set_nonincremental_init (struct obstack *);
104 static void set_nonincremental_init_from_string (tree, struct obstack *);
105 static tree find_init_member (tree, struct obstack *);
106 static void readonly_warning (tree, enum lvalue_use);
107 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
108 static void record_maybe_used_decl (tree);
109 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
111 /* Return true if EXP is a null pointer constant, false otherwise. */
113 static bool
114 null_pointer_constant_p (const_tree expr)
116 /* This should really operate on c_expr structures, but they aren't
117 yet available everywhere required. */
118 tree type = TREE_TYPE (expr);
119 return (TREE_CODE (expr) == INTEGER_CST
120 && !TREE_OVERFLOW (expr)
121 && integer_zerop (expr)
122 && (INTEGRAL_TYPE_P (type)
123 || (TREE_CODE (type) == POINTER_TYPE
124 && VOID_TYPE_P (TREE_TYPE (type))
125 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
128 /* EXPR may appear in an unevaluated part of an integer constant
129 expression, but not in an evaluated part. Wrap it in a
130 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
131 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
133 static tree
134 note_integer_operands (tree expr)
136 tree ret;
137 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
139 ret = copy_node (expr);
140 TREE_OVERFLOW (ret) = 1;
142 else
144 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
145 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
147 return ret;
150 /* Having checked whether EXPR may appear in an unevaluated part of an
151 integer constant expression and found that it may, remove any
152 C_MAYBE_CONST_EXPR noting this fact and return the resulting
153 expression. */
155 static inline tree
156 remove_c_maybe_const_expr (tree expr)
158 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
159 return C_MAYBE_CONST_EXPR_EXPR (expr);
160 else
161 return expr;
164 \f/* This is a cache to hold if two types are compatible or not. */
166 struct tagged_tu_seen_cache {
167 const struct tagged_tu_seen_cache * next;
168 const_tree t1;
169 const_tree t2;
170 /* The return value of tagged_types_tu_compatible_p if we had seen
171 these two types already. */
172 int val;
175 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
176 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
178 /* Do `exp = require_complete_type (exp);' to make sure exp
179 does not have an incomplete type. (That includes void types.) */
181 tree
182 require_complete_type (tree value)
184 tree type = TREE_TYPE (value);
186 if (value == error_mark_node || type == error_mark_node)
187 return error_mark_node;
189 /* First, detect a valid value with a complete type. */
190 if (COMPLETE_TYPE_P (type))
191 return value;
193 c_incomplete_type_error (value, type);
194 return error_mark_node;
197 /* Print an error message for invalid use of an incomplete type.
198 VALUE is the expression that was used (or 0 if that isn't known)
199 and TYPE is the type that was invalid. */
201 void
202 c_incomplete_type_error (const_tree value, const_tree type)
204 const char *type_code_string;
206 /* Avoid duplicate error message. */
207 if (TREE_CODE (type) == ERROR_MARK)
208 return;
210 if (value != 0 && (TREE_CODE (value) == VAR_DECL
211 || TREE_CODE (value) == PARM_DECL))
212 error ("%qD has an incomplete type", value);
213 else
215 retry:
216 /* We must print an error message. Be clever about what it says. */
218 switch (TREE_CODE (type))
220 case RECORD_TYPE:
221 type_code_string = "struct";
222 break;
224 case UNION_TYPE:
225 type_code_string = "union";
226 break;
228 case ENUMERAL_TYPE:
229 type_code_string = "enum";
230 break;
232 case VOID_TYPE:
233 error ("invalid use of void expression");
234 return;
236 case ARRAY_TYPE:
237 if (TYPE_DOMAIN (type))
239 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
241 error ("invalid use of flexible array member");
242 return;
244 type = TREE_TYPE (type);
245 goto retry;
247 error ("invalid use of array with unspecified bounds");
248 return;
250 default:
251 gcc_unreachable ();
254 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
255 error ("invalid use of undefined type %<%s %E%>",
256 type_code_string, TYPE_NAME (type));
257 else
258 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
259 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
263 /* Given a type, apply default promotions wrt unnamed function
264 arguments and return the new type. */
266 tree
267 c_type_promotes_to (tree type)
269 tree ret = NULL_TREE;
271 if (TYPE_MAIN_VARIANT (type) == float_type_node)
272 ret = double_type_node;
273 else if (c_promoting_integer_type_p (type))
275 /* Preserve unsignedness if not really getting any wider. */
276 if (TYPE_UNSIGNED (type)
277 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
278 ret = unsigned_type_node;
279 else
280 ret = integer_type_node;
283 if (ret != NULL_TREE)
284 return (TYPE_ATOMIC (type)
285 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
286 : ret);
288 return type;
291 /* Return true if between two named address spaces, whether there is a superset
292 named address space that encompasses both address spaces. If there is a
293 superset, return which address space is the superset. */
295 static bool
296 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
298 if (as1 == as2)
300 *common = as1;
301 return true;
303 else if (targetm.addr_space.subset_p (as1, as2))
305 *common = as2;
306 return true;
308 else if (targetm.addr_space.subset_p (as2, as1))
310 *common = as1;
311 return true;
313 else
314 return false;
317 /* Return a variant of TYPE which has all the type qualifiers of LIKE
318 as well as those of TYPE. */
320 static tree
321 qualify_type (tree type, tree like)
323 addr_space_t as_type = TYPE_ADDR_SPACE (type);
324 addr_space_t as_like = TYPE_ADDR_SPACE (like);
325 addr_space_t as_common;
327 /* If the two named address spaces are different, determine the common
328 superset address space. If there isn't one, raise an error. */
329 if (!addr_space_superset (as_type, as_like, &as_common))
331 as_common = as_type;
332 error ("%qT and %qT are in disjoint named address spaces",
333 type, like);
336 return c_build_qualified_type (type,
337 TYPE_QUALS_NO_ADDR_SPACE (type)
338 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
339 | ENCODE_QUAL_ADDR_SPACE (as_common));
342 /* Return true iff the given tree T is a variable length array. */
344 bool
345 c_vla_type_p (const_tree t)
347 if (TREE_CODE (t) == ARRAY_TYPE
348 && C_TYPE_VARIABLE_SIZE (t))
349 return true;
350 return false;
353 /* Return the composite type of two compatible types.
355 We assume that comptypes has already been done and returned
356 nonzero; if that isn't so, this may crash. In particular, we
357 assume that qualifiers match. */
359 tree
360 composite_type (tree t1, tree t2)
362 enum tree_code code1;
363 enum tree_code code2;
364 tree attributes;
366 /* Save time if the two types are the same. */
368 if (t1 == t2) return t1;
370 /* If one type is nonsense, use the other. */
371 if (t1 == error_mark_node)
372 return t2;
373 if (t2 == error_mark_node)
374 return t1;
376 code1 = TREE_CODE (t1);
377 code2 = TREE_CODE (t2);
379 /* Merge the attributes. */
380 attributes = targetm.merge_type_attributes (t1, t2);
382 /* If one is an enumerated type and the other is the compatible
383 integer type, the composite type might be either of the two
384 (DR#013 question 3). For consistency, use the enumerated type as
385 the composite type. */
387 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
388 return t1;
389 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
390 return t2;
392 gcc_assert (code1 == code2);
394 switch (code1)
396 case POINTER_TYPE:
397 /* For two pointers, do this recursively on the target type. */
399 tree pointed_to_1 = TREE_TYPE (t1);
400 tree pointed_to_2 = TREE_TYPE (t2);
401 tree target = composite_type (pointed_to_1, pointed_to_2);
402 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
403 t1 = build_type_attribute_variant (t1, attributes);
404 return qualify_type (t1, t2);
407 case ARRAY_TYPE:
409 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
410 int quals;
411 tree unqual_elt;
412 tree d1 = TYPE_DOMAIN (t1);
413 tree d2 = TYPE_DOMAIN (t2);
414 bool d1_variable, d2_variable;
415 bool d1_zero, d2_zero;
416 bool t1_complete, t2_complete;
418 /* We should not have any type quals on arrays at all. */
419 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
420 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
422 t1_complete = COMPLETE_TYPE_P (t1);
423 t2_complete = COMPLETE_TYPE_P (t2);
425 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
426 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
428 d1_variable = (!d1_zero
429 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
430 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
431 d2_variable = (!d2_zero
432 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
433 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
434 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
435 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
437 /* Save space: see if the result is identical to one of the args. */
438 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
439 && (d2_variable || d2_zero || !d1_variable))
440 return build_type_attribute_variant (t1, attributes);
441 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
442 && (d1_variable || d1_zero || !d2_variable))
443 return build_type_attribute_variant (t2, attributes);
445 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
446 return build_type_attribute_variant (t1, attributes);
447 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
448 return build_type_attribute_variant (t2, attributes);
450 /* Merge the element types, and have a size if either arg has
451 one. We may have qualifiers on the element types. To set
452 up TYPE_MAIN_VARIANT correctly, we need to form the
453 composite of the unqualified types and add the qualifiers
454 back at the end. */
455 quals = TYPE_QUALS (strip_array_types (elt));
456 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
457 t1 = build_array_type (unqual_elt,
458 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
459 && (d2_variable
460 || d2_zero
461 || !d1_variable))
462 ? t1
463 : t2));
464 /* Ensure a composite type involving a zero-length array type
465 is a zero-length type not an incomplete type. */
466 if (d1_zero && d2_zero
467 && (t1_complete || t2_complete)
468 && !COMPLETE_TYPE_P (t1))
470 TYPE_SIZE (t1) = bitsize_zero_node;
471 TYPE_SIZE_UNIT (t1) = size_zero_node;
473 t1 = c_build_qualified_type (t1, quals);
474 return build_type_attribute_variant (t1, attributes);
477 case ENUMERAL_TYPE:
478 case RECORD_TYPE:
479 case UNION_TYPE:
480 if (attributes != NULL)
482 /* Try harder not to create a new aggregate type. */
483 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
484 return t1;
485 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
486 return t2;
488 return build_type_attribute_variant (t1, attributes);
490 case FUNCTION_TYPE:
491 /* Function types: prefer the one that specified arg types.
492 If both do, merge the arg types. Also merge the return types. */
494 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
495 tree p1 = TYPE_ARG_TYPES (t1);
496 tree p2 = TYPE_ARG_TYPES (t2);
497 int len;
498 tree newargs, n;
499 int i;
501 /* Save space: see if the result is identical to one of the args. */
502 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
503 return build_type_attribute_variant (t1, attributes);
504 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
505 return build_type_attribute_variant (t2, attributes);
507 /* Simple way if one arg fails to specify argument types. */
508 if (TYPE_ARG_TYPES (t1) == 0)
510 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
511 t1 = build_type_attribute_variant (t1, attributes);
512 return qualify_type (t1, t2);
514 if (TYPE_ARG_TYPES (t2) == 0)
516 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
517 t1 = build_type_attribute_variant (t1, attributes);
518 return qualify_type (t1, t2);
521 /* If both args specify argument types, we must merge the two
522 lists, argument by argument. */
524 len = list_length (p1);
525 newargs = 0;
527 for (i = 0; i < len; i++)
528 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
530 n = newargs;
532 for (; p1;
533 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
535 /* A null type means arg type is not specified.
536 Take whatever the other function type has. */
537 if (TREE_VALUE (p1) == 0)
539 TREE_VALUE (n) = TREE_VALUE (p2);
540 goto parm_done;
542 if (TREE_VALUE (p2) == 0)
544 TREE_VALUE (n) = TREE_VALUE (p1);
545 goto parm_done;
548 /* Given wait (union {union wait *u; int *i} *)
549 and wait (union wait *),
550 prefer union wait * as type of parm. */
551 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
552 && TREE_VALUE (p1) != TREE_VALUE (p2))
554 tree memb;
555 tree mv2 = TREE_VALUE (p2);
556 if (mv2 && mv2 != error_mark_node
557 && TREE_CODE (mv2) != ARRAY_TYPE)
558 mv2 = TYPE_MAIN_VARIANT (mv2);
559 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
560 memb; memb = DECL_CHAIN (memb))
562 tree mv3 = TREE_TYPE (memb);
563 if (mv3 && mv3 != error_mark_node
564 && TREE_CODE (mv3) != ARRAY_TYPE)
565 mv3 = TYPE_MAIN_VARIANT (mv3);
566 if (comptypes (mv3, mv2))
568 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
569 TREE_VALUE (p2));
570 pedwarn (input_location, OPT_Wpedantic,
571 "function types not truly compatible in ISO C");
572 goto parm_done;
576 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
577 && TREE_VALUE (p2) != TREE_VALUE (p1))
579 tree memb;
580 tree mv1 = TREE_VALUE (p1);
581 if (mv1 && mv1 != error_mark_node
582 && TREE_CODE (mv1) != ARRAY_TYPE)
583 mv1 = TYPE_MAIN_VARIANT (mv1);
584 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
585 memb; memb = DECL_CHAIN (memb))
587 tree mv3 = TREE_TYPE (memb);
588 if (mv3 && mv3 != error_mark_node
589 && TREE_CODE (mv3) != ARRAY_TYPE)
590 mv3 = TYPE_MAIN_VARIANT (mv3);
591 if (comptypes (mv3, mv1))
593 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
594 TREE_VALUE (p1));
595 pedwarn (input_location, OPT_Wpedantic,
596 "function types not truly compatible in ISO C");
597 goto parm_done;
601 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
602 parm_done: ;
605 t1 = build_function_type (valtype, newargs);
606 t1 = qualify_type (t1, t2);
607 /* ... falls through ... */
610 default:
611 return build_type_attribute_variant (t1, attributes);
616 /* Return the type of a conditional expression between pointers to
617 possibly differently qualified versions of compatible types.
619 We assume that comp_target_types has already been done and returned
620 nonzero; if that isn't so, this may crash. */
622 static tree
623 common_pointer_type (tree t1, tree t2)
625 tree attributes;
626 tree pointed_to_1, mv1;
627 tree pointed_to_2, mv2;
628 tree target;
629 unsigned target_quals;
630 addr_space_t as1, as2, as_common;
631 int quals1, quals2;
633 /* Save time if the two types are the same. */
635 if (t1 == t2) return t1;
637 /* If one type is nonsense, use the other. */
638 if (t1 == error_mark_node)
639 return t2;
640 if (t2 == error_mark_node)
641 return t1;
643 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
644 && TREE_CODE (t2) == POINTER_TYPE);
646 /* Merge the attributes. */
647 attributes = targetm.merge_type_attributes (t1, t2);
649 /* Find the composite type of the target types, and combine the
650 qualifiers of the two types' targets. Do not lose qualifiers on
651 array element types by taking the TYPE_MAIN_VARIANT. */
652 mv1 = pointed_to_1 = TREE_TYPE (t1);
653 mv2 = pointed_to_2 = TREE_TYPE (t2);
654 if (TREE_CODE (mv1) != ARRAY_TYPE)
655 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
656 if (TREE_CODE (mv2) != ARRAY_TYPE)
657 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
658 target = composite_type (mv1, mv2);
660 /* For function types do not merge const qualifiers, but drop them
661 if used inconsistently. The middle-end uses these to mark const
662 and noreturn functions. */
663 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
664 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
666 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
667 target_quals = (quals1 & quals2);
668 else
669 target_quals = (quals1 | quals2);
671 /* If the two named address spaces are different, determine the common
672 superset address space. This is guaranteed to exist due to the
673 assumption that comp_target_type returned non-zero. */
674 as1 = TYPE_ADDR_SPACE (pointed_to_1);
675 as2 = TYPE_ADDR_SPACE (pointed_to_2);
676 if (!addr_space_superset (as1, as2, &as_common))
677 gcc_unreachable ();
679 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
681 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
682 return build_type_attribute_variant (t1, attributes);
685 /* Return the common type for two arithmetic types under the usual
686 arithmetic conversions. The default conversions have already been
687 applied, and enumerated types converted to their compatible integer
688 types. The resulting type is unqualified and has no attributes.
690 This is the type for the result of most arithmetic operations
691 if the operands have the given two types. */
693 static tree
694 c_common_type (tree t1, tree t2)
696 enum tree_code code1;
697 enum tree_code code2;
699 /* If one type is nonsense, use the other. */
700 if (t1 == error_mark_node)
701 return t2;
702 if (t2 == error_mark_node)
703 return t1;
705 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
706 t1 = TYPE_MAIN_VARIANT (t1);
708 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
709 t2 = TYPE_MAIN_VARIANT (t2);
711 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
712 t1 = build_type_attribute_variant (t1, NULL_TREE);
714 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
715 t2 = build_type_attribute_variant (t2, NULL_TREE);
717 /* Save time if the two types are the same. */
719 if (t1 == t2) return t1;
721 code1 = TREE_CODE (t1);
722 code2 = TREE_CODE (t2);
724 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
725 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
726 || code1 == INTEGER_TYPE);
727 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
728 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
729 || code2 == INTEGER_TYPE);
731 /* When one operand is a decimal float type, the other operand cannot be
732 a generic float type or a complex type. We also disallow vector types
733 here. */
734 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
735 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
737 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
739 error ("can%'t mix operands of decimal float and vector types");
740 return error_mark_node;
742 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
744 error ("can%'t mix operands of decimal float and complex types");
745 return error_mark_node;
747 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
749 error ("can%'t mix operands of decimal float and other float types");
750 return error_mark_node;
754 /* If one type is a vector type, return that type. (How the usual
755 arithmetic conversions apply to the vector types extension is not
756 precisely specified.) */
757 if (code1 == VECTOR_TYPE)
758 return t1;
760 if (code2 == VECTOR_TYPE)
761 return t2;
763 /* If one type is complex, form the common type of the non-complex
764 components, then make that complex. Use T1 or T2 if it is the
765 required type. */
766 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
768 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
769 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
770 tree subtype = c_common_type (subtype1, subtype2);
772 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
773 return t1;
774 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
775 return t2;
776 else
777 return build_complex_type (subtype);
780 /* If only one is real, use it as the result. */
782 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
783 return t1;
785 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
786 return t2;
788 /* If both are real and either are decimal floating point types, use
789 the decimal floating point type with the greater precision. */
791 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
793 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
794 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
795 return dfloat128_type_node;
796 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
797 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
798 return dfloat64_type_node;
799 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
800 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
801 return dfloat32_type_node;
804 /* Deal with fixed-point types. */
805 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
807 unsigned int unsignedp = 0, satp = 0;
808 enum machine_mode m1, m2;
809 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
811 m1 = TYPE_MODE (t1);
812 m2 = TYPE_MODE (t2);
814 /* If one input type is saturating, the result type is saturating. */
815 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
816 satp = 1;
818 /* If both fixed-point types are unsigned, the result type is unsigned.
819 When mixing fixed-point and integer types, follow the sign of the
820 fixed-point type.
821 Otherwise, the result type is signed. */
822 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
823 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
824 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
825 && TYPE_UNSIGNED (t1))
826 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
827 && TYPE_UNSIGNED (t2)))
828 unsignedp = 1;
830 /* The result type is signed. */
831 if (unsignedp == 0)
833 /* If the input type is unsigned, we need to convert to the
834 signed type. */
835 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
837 enum mode_class mclass = (enum mode_class) 0;
838 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
839 mclass = MODE_FRACT;
840 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
841 mclass = MODE_ACCUM;
842 else
843 gcc_unreachable ();
844 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
846 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
848 enum mode_class mclass = (enum mode_class) 0;
849 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
850 mclass = MODE_FRACT;
851 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
852 mclass = MODE_ACCUM;
853 else
854 gcc_unreachable ();
855 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
859 if (code1 == FIXED_POINT_TYPE)
861 fbit1 = GET_MODE_FBIT (m1);
862 ibit1 = GET_MODE_IBIT (m1);
864 else
866 fbit1 = 0;
867 /* Signed integers need to subtract one sign bit. */
868 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
871 if (code2 == FIXED_POINT_TYPE)
873 fbit2 = GET_MODE_FBIT (m2);
874 ibit2 = GET_MODE_IBIT (m2);
876 else
878 fbit2 = 0;
879 /* Signed integers need to subtract one sign bit. */
880 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
883 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
884 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
885 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
886 satp);
889 /* Both real or both integers; use the one with greater precision. */
891 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
892 return t1;
893 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
894 return t2;
896 /* Same precision. Prefer long longs to longs to ints when the
897 same precision, following the C99 rules on integer type rank
898 (which are equivalent to the C90 rules for C90 types). */
900 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
901 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
902 return long_long_unsigned_type_node;
904 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
905 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
907 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
908 return long_long_unsigned_type_node;
909 else
910 return long_long_integer_type_node;
913 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
914 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
915 return long_unsigned_type_node;
917 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
918 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
920 /* But preserve unsignedness from the other type,
921 since long cannot hold all the values of an unsigned int. */
922 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
923 return long_unsigned_type_node;
924 else
925 return long_integer_type_node;
928 /* Likewise, prefer long double to double even if same size. */
929 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
930 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
931 return long_double_type_node;
933 /* Likewise, prefer double to float even if same size.
934 We got a couple of embedded targets with 32 bit doubles, and the
935 pdp11 might have 64 bit floats. */
936 if (TYPE_MAIN_VARIANT (t1) == double_type_node
937 || TYPE_MAIN_VARIANT (t2) == double_type_node)
938 return double_type_node;
940 /* Otherwise prefer the unsigned one. */
942 if (TYPE_UNSIGNED (t1))
943 return t1;
944 else
945 return t2;
948 /* Wrapper around c_common_type that is used by c-common.c and other
949 front end optimizations that remove promotions. ENUMERAL_TYPEs
950 are allowed here and are converted to their compatible integer types.
951 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
952 preferably a non-Boolean type as the common type. */
953 tree
954 common_type (tree t1, tree t2)
956 if (TREE_CODE (t1) == ENUMERAL_TYPE)
957 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
958 if (TREE_CODE (t2) == ENUMERAL_TYPE)
959 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
961 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
962 if (TREE_CODE (t1) == BOOLEAN_TYPE
963 && TREE_CODE (t2) == BOOLEAN_TYPE)
964 return boolean_type_node;
966 /* If either type is BOOLEAN_TYPE, then return the other. */
967 if (TREE_CODE (t1) == BOOLEAN_TYPE)
968 return t2;
969 if (TREE_CODE (t2) == BOOLEAN_TYPE)
970 return t1;
972 return c_common_type (t1, t2);
975 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
976 or various other operations. Return 2 if they are compatible
977 but a warning may be needed if you use them together. */
980 comptypes (tree type1, tree type2)
982 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
983 int val;
985 val = comptypes_internal (type1, type2, NULL, NULL);
986 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
988 return val;
991 /* Like comptypes, but if it returns non-zero because enum and int are
992 compatible, it sets *ENUM_AND_INT_P to true. */
994 static int
995 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
997 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
998 int val;
1000 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1001 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1003 return val;
1006 /* Like comptypes, but if it returns nonzero for different types, it
1007 sets *DIFFERENT_TYPES_P to true. */
1010 comptypes_check_different_types (tree type1, tree type2,
1011 bool *different_types_p)
1013 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1014 int val;
1016 val = comptypes_internal (type1, type2, NULL, different_types_p);
1017 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1019 return val;
1022 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1023 or various other operations. Return 2 if they are compatible
1024 but a warning may be needed if you use them together. If
1025 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1026 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1027 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1028 NULL, and the types are compatible but different enough not to be
1029 permitted in C11 typedef redeclarations, then this sets
1030 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1031 false, but may or may not be set if the types are incompatible.
1032 This differs from comptypes, in that we don't free the seen
1033 types. */
1035 static int
1036 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1037 bool *different_types_p)
1039 const_tree t1 = type1;
1040 const_tree t2 = type2;
1041 int attrval, val;
1043 /* Suppress errors caused by previously reported errors. */
1045 if (t1 == t2 || !t1 || !t2
1046 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1047 return 1;
1049 /* Enumerated types are compatible with integer types, but this is
1050 not transitive: two enumerated types in the same translation unit
1051 are compatible with each other only if they are the same type. */
1053 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1055 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1056 if (TREE_CODE (t2) != VOID_TYPE)
1058 if (enum_and_int_p != NULL)
1059 *enum_and_int_p = true;
1060 if (different_types_p != NULL)
1061 *different_types_p = true;
1064 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1066 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1067 if (TREE_CODE (t1) != VOID_TYPE)
1069 if (enum_and_int_p != NULL)
1070 *enum_and_int_p = true;
1071 if (different_types_p != NULL)
1072 *different_types_p = true;
1076 if (t1 == t2)
1077 return 1;
1079 /* Different classes of types can't be compatible. */
1081 if (TREE_CODE (t1) != TREE_CODE (t2))
1082 return 0;
1084 /* Qualifiers must match. C99 6.7.3p9 */
1086 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1087 return 0;
1089 /* Allow for two different type nodes which have essentially the same
1090 definition. Note that we already checked for equality of the type
1091 qualifiers (just above). */
1093 if (TREE_CODE (t1) != ARRAY_TYPE
1094 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1095 return 1;
1097 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1098 if (!(attrval = comp_type_attributes (t1, t2)))
1099 return 0;
1101 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1102 val = 0;
1104 switch (TREE_CODE (t1))
1106 case POINTER_TYPE:
1107 /* Do not remove mode or aliasing information. */
1108 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1109 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1110 break;
1111 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1112 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1113 enum_and_int_p, different_types_p));
1114 break;
1116 case FUNCTION_TYPE:
1117 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1118 different_types_p);
1119 break;
1121 case ARRAY_TYPE:
1123 tree d1 = TYPE_DOMAIN (t1);
1124 tree d2 = TYPE_DOMAIN (t2);
1125 bool d1_variable, d2_variable;
1126 bool d1_zero, d2_zero;
1127 val = 1;
1129 /* Target types must match incl. qualifiers. */
1130 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1131 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1132 enum_and_int_p,
1133 different_types_p)))
1134 return 0;
1136 if (different_types_p != NULL
1137 && (d1 == 0) != (d2 == 0))
1138 *different_types_p = true;
1139 /* Sizes must match unless one is missing or variable. */
1140 if (d1 == 0 || d2 == 0 || d1 == d2)
1141 break;
1143 d1_zero = !TYPE_MAX_VALUE (d1);
1144 d2_zero = !TYPE_MAX_VALUE (d2);
1146 d1_variable = (!d1_zero
1147 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1148 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1149 d2_variable = (!d2_zero
1150 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1151 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1152 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1153 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1155 if (different_types_p != NULL
1156 && d1_variable != d2_variable)
1157 *different_types_p = true;
1158 if (d1_variable || d2_variable)
1159 break;
1160 if (d1_zero && d2_zero)
1161 break;
1162 if (d1_zero || d2_zero
1163 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1164 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1165 val = 0;
1167 break;
1170 case ENUMERAL_TYPE:
1171 case RECORD_TYPE:
1172 case UNION_TYPE:
1173 if (val != 1 && !same_translation_unit_p (t1, t2))
1175 tree a1 = TYPE_ATTRIBUTES (t1);
1176 tree a2 = TYPE_ATTRIBUTES (t2);
1178 if (! attribute_list_contained (a1, a2)
1179 && ! attribute_list_contained (a2, a1))
1180 break;
1182 if (attrval != 2)
1183 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1184 different_types_p);
1185 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1186 different_types_p);
1188 break;
1190 case VECTOR_TYPE:
1191 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1192 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1193 enum_and_int_p, different_types_p));
1194 break;
1196 default:
1197 break;
1199 return attrval == 2 && val == 1 ? 2 : val;
1202 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1203 their qualifiers, except for named address spaces. If the pointers point to
1204 different named addresses, then we must determine if one address space is a
1205 subset of the other. */
1207 static int
1208 comp_target_types (location_t location, tree ttl, tree ttr)
1210 int val;
1211 tree mvl = TREE_TYPE (ttl);
1212 tree mvr = TREE_TYPE (ttr);
1213 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1214 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1215 addr_space_t as_common;
1216 bool enum_and_int_p;
1218 /* Fail if pointers point to incompatible address spaces. */
1219 if (!addr_space_superset (asl, asr, &as_common))
1220 return 0;
1222 /* Do not lose qualifiers on element types of array types that are
1223 pointer targets by taking their TYPE_MAIN_VARIANT. */
1224 if (TREE_CODE (mvl) != ARRAY_TYPE)
1225 mvl = (TYPE_ATOMIC (mvl)
1226 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1227 : TYPE_MAIN_VARIANT (mvl));
1228 if (TREE_CODE (mvr) != ARRAY_TYPE)
1229 mvr = (TYPE_ATOMIC (mvr)
1230 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1231 : TYPE_MAIN_VARIANT (mvr));
1232 enum_and_int_p = false;
1233 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1235 if (val == 2)
1236 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1238 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1239 warning_at (location, OPT_Wc___compat,
1240 "pointer target types incompatible in C++");
1242 return val;
1245 /* Subroutines of `comptypes'. */
1247 /* Determine whether two trees derive from the same translation unit.
1248 If the CONTEXT chain ends in a null, that tree's context is still
1249 being parsed, so if two trees have context chains ending in null,
1250 they're in the same translation unit. */
1252 same_translation_unit_p (const_tree t1, const_tree t2)
1254 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1255 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1257 case tcc_declaration:
1258 t1 = DECL_CONTEXT (t1); break;
1259 case tcc_type:
1260 t1 = TYPE_CONTEXT (t1); break;
1261 case tcc_exceptional:
1262 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1263 default: gcc_unreachable ();
1266 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1267 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1269 case tcc_declaration:
1270 t2 = DECL_CONTEXT (t2); break;
1271 case tcc_type:
1272 t2 = TYPE_CONTEXT (t2); break;
1273 case tcc_exceptional:
1274 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1275 default: gcc_unreachable ();
1278 return t1 == t2;
1281 /* Allocate the seen two types, assuming that they are compatible. */
1283 static struct tagged_tu_seen_cache *
1284 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1286 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1287 tu->next = tagged_tu_seen_base;
1288 tu->t1 = t1;
1289 tu->t2 = t2;
1291 tagged_tu_seen_base = tu;
1293 /* The C standard says that two structures in different translation
1294 units are compatible with each other only if the types of their
1295 fields are compatible (among other things). We assume that they
1296 are compatible until proven otherwise when building the cache.
1297 An example where this can occur is:
1298 struct a
1300 struct a *next;
1302 If we are comparing this against a similar struct in another TU,
1303 and did not assume they were compatible, we end up with an infinite
1304 loop. */
1305 tu->val = 1;
1306 return tu;
1309 /* Free the seen types until we get to TU_TIL. */
1311 static void
1312 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1314 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1315 while (tu != tu_til)
1317 const struct tagged_tu_seen_cache *const tu1
1318 = (const struct tagged_tu_seen_cache *) tu;
1319 tu = tu1->next;
1320 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1322 tagged_tu_seen_base = tu_til;
1325 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1326 compatible. If the two types are not the same (which has been
1327 checked earlier), this can only happen when multiple translation
1328 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1329 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1330 comptypes_internal. */
1332 static int
1333 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1334 bool *enum_and_int_p, bool *different_types_p)
1336 tree s1, s2;
1337 bool needs_warning = false;
1339 /* We have to verify that the tags of the types are the same. This
1340 is harder than it looks because this may be a typedef, so we have
1341 to go look at the original type. It may even be a typedef of a
1342 typedef...
1343 In the case of compiler-created builtin structs the TYPE_DECL
1344 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1345 while (TYPE_NAME (t1)
1346 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1347 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1348 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1350 while (TYPE_NAME (t2)
1351 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1352 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1353 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1355 /* C90 didn't have the requirement that the two tags be the same. */
1356 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1357 return 0;
1359 /* C90 didn't say what happened if one or both of the types were
1360 incomplete; we choose to follow C99 rules here, which is that they
1361 are compatible. */
1362 if (TYPE_SIZE (t1) == NULL
1363 || TYPE_SIZE (t2) == NULL)
1364 return 1;
1367 const struct tagged_tu_seen_cache * tts_i;
1368 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1369 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1370 return tts_i->val;
1373 switch (TREE_CODE (t1))
1375 case ENUMERAL_TYPE:
1377 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1378 /* Speed up the case where the type values are in the same order. */
1379 tree tv1 = TYPE_VALUES (t1);
1380 tree tv2 = TYPE_VALUES (t2);
1382 if (tv1 == tv2)
1384 return 1;
1387 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1389 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1390 break;
1391 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1393 tu->val = 0;
1394 return 0;
1398 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1400 return 1;
1402 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1404 tu->val = 0;
1405 return 0;
1408 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1410 tu->val = 0;
1411 return 0;
1414 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1416 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1417 if (s2 == NULL
1418 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1420 tu->val = 0;
1421 return 0;
1424 return 1;
1427 case UNION_TYPE:
1429 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1430 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1432 tu->val = 0;
1433 return 0;
1436 /* Speed up the common case where the fields are in the same order. */
1437 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1438 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1440 int result;
1442 if (DECL_NAME (s1) != DECL_NAME (s2))
1443 break;
1444 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1445 enum_and_int_p, different_types_p);
1447 if (result != 1 && !DECL_NAME (s1))
1448 break;
1449 if (result == 0)
1451 tu->val = 0;
1452 return 0;
1454 if (result == 2)
1455 needs_warning = true;
1457 if (TREE_CODE (s1) == FIELD_DECL
1458 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1459 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1461 tu->val = 0;
1462 return 0;
1465 if (!s1 && !s2)
1467 tu->val = needs_warning ? 2 : 1;
1468 return tu->val;
1471 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1473 bool ok = false;
1475 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1476 if (DECL_NAME (s1) == DECL_NAME (s2))
1478 int result;
1480 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1481 enum_and_int_p,
1482 different_types_p);
1484 if (result != 1 && !DECL_NAME (s1))
1485 continue;
1486 if (result == 0)
1488 tu->val = 0;
1489 return 0;
1491 if (result == 2)
1492 needs_warning = true;
1494 if (TREE_CODE (s1) == FIELD_DECL
1495 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1496 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1497 break;
1499 ok = true;
1500 break;
1502 if (!ok)
1504 tu->val = 0;
1505 return 0;
1508 tu->val = needs_warning ? 2 : 10;
1509 return tu->val;
1512 case RECORD_TYPE:
1514 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1516 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1517 s1 && s2;
1518 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1520 int result;
1521 if (TREE_CODE (s1) != TREE_CODE (s2)
1522 || DECL_NAME (s1) != DECL_NAME (s2))
1523 break;
1524 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1525 enum_and_int_p, different_types_p);
1526 if (result == 0)
1527 break;
1528 if (result == 2)
1529 needs_warning = true;
1531 if (TREE_CODE (s1) == FIELD_DECL
1532 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1533 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1534 break;
1536 if (s1 && s2)
1537 tu->val = 0;
1538 else
1539 tu->val = needs_warning ? 2 : 1;
1540 return tu->val;
1543 default:
1544 gcc_unreachable ();
1548 /* Return 1 if two function types F1 and F2 are compatible.
1549 If either type specifies no argument types,
1550 the other must specify a fixed number of self-promoting arg types.
1551 Otherwise, if one type specifies only the number of arguments,
1552 the other must specify that number of self-promoting arg types.
1553 Otherwise, the argument types must match.
1554 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1556 static int
1557 function_types_compatible_p (const_tree f1, const_tree f2,
1558 bool *enum_and_int_p, bool *different_types_p)
1560 tree args1, args2;
1561 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1562 int val = 1;
1563 int val1;
1564 tree ret1, ret2;
1566 ret1 = TREE_TYPE (f1);
1567 ret2 = TREE_TYPE (f2);
1569 /* 'volatile' qualifiers on a function's return type used to mean
1570 the function is noreturn. */
1571 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1572 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1573 if (TYPE_VOLATILE (ret1))
1574 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1575 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1576 if (TYPE_VOLATILE (ret2))
1577 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1578 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1579 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1580 if (val == 0)
1581 return 0;
1583 args1 = TYPE_ARG_TYPES (f1);
1584 args2 = TYPE_ARG_TYPES (f2);
1586 if (different_types_p != NULL
1587 && (args1 == 0) != (args2 == 0))
1588 *different_types_p = true;
1590 /* An unspecified parmlist matches any specified parmlist
1591 whose argument types don't need default promotions. */
1593 if (args1 == 0)
1595 if (!self_promoting_args_p (args2))
1596 return 0;
1597 /* If one of these types comes from a non-prototype fn definition,
1598 compare that with the other type's arglist.
1599 If they don't match, ask for a warning (but no error). */
1600 if (TYPE_ACTUAL_ARG_TYPES (f1)
1601 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1602 enum_and_int_p, different_types_p))
1603 val = 2;
1604 return val;
1606 if (args2 == 0)
1608 if (!self_promoting_args_p (args1))
1609 return 0;
1610 if (TYPE_ACTUAL_ARG_TYPES (f2)
1611 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1612 enum_and_int_p, different_types_p))
1613 val = 2;
1614 return val;
1617 /* Both types have argument lists: compare them and propagate results. */
1618 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1619 different_types_p);
1620 return val1 != 1 ? val1 : val;
1623 /* Check two lists of types for compatibility, returning 0 for
1624 incompatible, 1 for compatible, or 2 for compatible with
1625 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1626 comptypes_internal. */
1628 static int
1629 type_lists_compatible_p (const_tree args1, const_tree args2,
1630 bool *enum_and_int_p, bool *different_types_p)
1632 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1633 int val = 1;
1634 int newval = 0;
1636 while (1)
1638 tree a1, mv1, a2, mv2;
1639 if (args1 == 0 && args2 == 0)
1640 return val;
1641 /* If one list is shorter than the other,
1642 they fail to match. */
1643 if (args1 == 0 || args2 == 0)
1644 return 0;
1645 mv1 = a1 = TREE_VALUE (args1);
1646 mv2 = a2 = TREE_VALUE (args2);
1647 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1648 mv1 = (TYPE_ATOMIC (mv1)
1649 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1650 TYPE_QUAL_ATOMIC)
1651 : TYPE_MAIN_VARIANT (mv1));
1652 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1653 mv2 = (TYPE_ATOMIC (mv2)
1654 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1655 TYPE_QUAL_ATOMIC)
1656 : TYPE_MAIN_VARIANT (mv2));
1657 /* A null pointer instead of a type
1658 means there is supposed to be an argument
1659 but nothing is specified about what type it has.
1660 So match anything that self-promotes. */
1661 if (different_types_p != NULL
1662 && (a1 == 0) != (a2 == 0))
1663 *different_types_p = true;
1664 if (a1 == 0)
1666 if (c_type_promotes_to (a2) != a2)
1667 return 0;
1669 else if (a2 == 0)
1671 if (c_type_promotes_to (a1) != a1)
1672 return 0;
1674 /* If one of the lists has an error marker, ignore this arg. */
1675 else if (TREE_CODE (a1) == ERROR_MARK
1676 || TREE_CODE (a2) == ERROR_MARK)
1678 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1679 different_types_p)))
1681 if (different_types_p != NULL)
1682 *different_types_p = true;
1683 /* Allow wait (union {union wait *u; int *i} *)
1684 and wait (union wait *) to be compatible. */
1685 if (TREE_CODE (a1) == UNION_TYPE
1686 && (TYPE_NAME (a1) == 0
1687 || TYPE_TRANSPARENT_AGGR (a1))
1688 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1689 && tree_int_cst_equal (TYPE_SIZE (a1),
1690 TYPE_SIZE (a2)))
1692 tree memb;
1693 for (memb = TYPE_FIELDS (a1);
1694 memb; memb = DECL_CHAIN (memb))
1696 tree mv3 = TREE_TYPE (memb);
1697 if (mv3 && mv3 != error_mark_node
1698 && TREE_CODE (mv3) != ARRAY_TYPE)
1699 mv3 = (TYPE_ATOMIC (mv3)
1700 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1701 TYPE_QUAL_ATOMIC)
1702 : TYPE_MAIN_VARIANT (mv3));
1703 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1704 different_types_p))
1705 break;
1707 if (memb == 0)
1708 return 0;
1710 else if (TREE_CODE (a2) == UNION_TYPE
1711 && (TYPE_NAME (a2) == 0
1712 || TYPE_TRANSPARENT_AGGR (a2))
1713 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1714 && tree_int_cst_equal (TYPE_SIZE (a2),
1715 TYPE_SIZE (a1)))
1717 tree memb;
1718 for (memb = TYPE_FIELDS (a2);
1719 memb; memb = DECL_CHAIN (memb))
1721 tree mv3 = TREE_TYPE (memb);
1722 if (mv3 && mv3 != error_mark_node
1723 && TREE_CODE (mv3) != ARRAY_TYPE)
1724 mv3 = (TYPE_ATOMIC (mv3)
1725 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1726 TYPE_QUAL_ATOMIC)
1727 : TYPE_MAIN_VARIANT (mv3));
1728 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1729 different_types_p))
1730 break;
1732 if (memb == 0)
1733 return 0;
1735 else
1736 return 0;
1739 /* comptypes said ok, but record if it said to warn. */
1740 if (newval > val)
1741 val = newval;
1743 args1 = TREE_CHAIN (args1);
1744 args2 = TREE_CHAIN (args2);
1748 /* Compute the size to increment a pointer by. */
1750 static tree
1751 c_size_in_bytes (const_tree type)
1753 enum tree_code code = TREE_CODE (type);
1755 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1756 return size_one_node;
1758 if (!COMPLETE_OR_VOID_TYPE_P (type))
1760 error ("arithmetic on pointer to an incomplete type");
1761 return size_one_node;
1764 /* Convert in case a char is more than one unit. */
1765 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1766 size_int (TYPE_PRECISION (char_type_node)
1767 / BITS_PER_UNIT));
1770 /* Return either DECL or its known constant value (if it has one). */
1772 tree
1773 decl_constant_value (tree decl)
1775 if (/* Don't change a variable array bound or initial value to a constant
1776 in a place where a variable is invalid. Note that DECL_INITIAL
1777 isn't valid for a PARM_DECL. */
1778 current_function_decl != 0
1779 && TREE_CODE (decl) != PARM_DECL
1780 && !TREE_THIS_VOLATILE (decl)
1781 && TREE_READONLY (decl)
1782 && DECL_INITIAL (decl) != 0
1783 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1784 /* This is invalid if initial value is not constant.
1785 If it has either a function call, a memory reference,
1786 or a variable, then re-evaluating it could give different results. */
1787 && TREE_CONSTANT (DECL_INITIAL (decl))
1788 /* Check for cases where this is sub-optimal, even though valid. */
1789 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1790 return DECL_INITIAL (decl);
1791 return decl;
1794 /* Convert the array expression EXP to a pointer. */
1795 static tree
1796 array_to_pointer_conversion (location_t loc, tree exp)
1798 tree orig_exp = exp;
1799 tree type = TREE_TYPE (exp);
1800 tree adr;
1801 tree restype = TREE_TYPE (type);
1802 tree ptrtype;
1804 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1806 STRIP_TYPE_NOPS (exp);
1808 if (TREE_NO_WARNING (orig_exp))
1809 TREE_NO_WARNING (exp) = 1;
1811 ptrtype = build_pointer_type (restype);
1813 if (TREE_CODE (exp) == INDIRECT_REF)
1814 return convert (ptrtype, TREE_OPERAND (exp, 0));
1816 /* In C++ array compound literals are temporary objects unless they are
1817 const or appear in namespace scope, so they are destroyed too soon
1818 to use them for much of anything (c++/53220). */
1819 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1821 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1822 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1823 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1824 "converting an array compound literal to a pointer "
1825 "is ill-formed in C++");
1828 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1829 return convert (ptrtype, adr);
1832 /* Convert the function expression EXP to a pointer. */
1833 static tree
1834 function_to_pointer_conversion (location_t loc, tree exp)
1836 tree orig_exp = exp;
1838 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1840 STRIP_TYPE_NOPS (exp);
1842 if (TREE_NO_WARNING (orig_exp))
1843 TREE_NO_WARNING (exp) = 1;
1845 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1848 /* Mark EXP as read, not just set, for set but not used -Wunused
1849 warning purposes. */
1851 void
1852 mark_exp_read (tree exp)
1854 switch (TREE_CODE (exp))
1856 case VAR_DECL:
1857 case PARM_DECL:
1858 DECL_READ_P (exp) = 1;
1859 break;
1860 case ARRAY_REF:
1861 case COMPONENT_REF:
1862 case MODIFY_EXPR:
1863 case REALPART_EXPR:
1864 case IMAGPART_EXPR:
1865 CASE_CONVERT:
1866 case ADDR_EXPR:
1867 mark_exp_read (TREE_OPERAND (exp, 0));
1868 break;
1869 case COMPOUND_EXPR:
1870 case C_MAYBE_CONST_EXPR:
1871 mark_exp_read (TREE_OPERAND (exp, 1));
1872 break;
1873 default:
1874 break;
1878 /* Perform the default conversion of arrays and functions to pointers.
1879 Return the result of converting EXP. For any other expression, just
1880 return EXP.
1882 LOC is the location of the expression. */
1884 struct c_expr
1885 default_function_array_conversion (location_t loc, struct c_expr exp)
1887 tree orig_exp = exp.value;
1888 tree type = TREE_TYPE (exp.value);
1889 enum tree_code code = TREE_CODE (type);
1891 switch (code)
1893 case ARRAY_TYPE:
1895 bool not_lvalue = false;
1896 bool lvalue_array_p;
1898 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1899 || CONVERT_EXPR_P (exp.value))
1900 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1902 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1903 not_lvalue = true;
1904 exp.value = TREE_OPERAND (exp.value, 0);
1907 if (TREE_NO_WARNING (orig_exp))
1908 TREE_NO_WARNING (exp.value) = 1;
1910 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1911 if (!flag_isoc99 && !lvalue_array_p)
1913 /* Before C99, non-lvalue arrays do not decay to pointers.
1914 Normally, using such an array would be invalid; but it can
1915 be used correctly inside sizeof or as a statement expression.
1916 Thus, do not give an error here; an error will result later. */
1917 return exp;
1920 exp.value = array_to_pointer_conversion (loc, exp.value);
1922 break;
1923 case FUNCTION_TYPE:
1924 exp.value = function_to_pointer_conversion (loc, exp.value);
1925 break;
1926 default:
1927 break;
1930 return exp;
1933 struct c_expr
1934 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1936 mark_exp_read (exp.value);
1937 return default_function_array_conversion (loc, exp);
1940 /* Return whether EXPR should be treated as an atomic lvalue for the
1941 purposes of load and store handling. */
1943 static bool
1944 really_atomic_lvalue (tree expr)
1946 if (expr == error_mark_node || TREE_TYPE (expr) == error_mark_node)
1947 return false;
1948 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1949 return false;
1950 if (!lvalue_p (expr))
1951 return false;
1953 /* Ignore _Atomic on register variables, since their addresses can't
1954 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1955 sequences wouldn't work. Ignore _Atomic on structures containing
1956 bit-fields, since accessing elements of atomic structures or
1957 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1958 it's undefined at translation time or execution time, and the
1959 normal atomic sequences again wouldn't work. */
1960 while (handled_component_p (expr))
1962 if (TREE_CODE (expr) == COMPONENT_REF
1963 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1964 return false;
1965 expr = TREE_OPERAND (expr, 0);
1967 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1968 return false;
1969 return true;
1972 /* Convert expression EXP (location LOC) from lvalue to rvalue,
1973 including converting functions and arrays to pointers if CONVERT_P.
1974 If READ_P, also mark the expression as having been read. */
1976 struct c_expr
1977 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1978 bool convert_p, bool read_p)
1980 if (read_p)
1981 mark_exp_read (exp.value);
1982 if (convert_p)
1983 exp = default_function_array_conversion (loc, exp);
1984 if (really_atomic_lvalue (exp.value))
1986 vec<tree, va_gc> *params;
1987 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
1988 tree expr_type = TREE_TYPE (exp.value);
1989 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
1990 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
1992 gcc_assert (TYPE_ATOMIC (expr_type));
1994 /* Expansion of a generic atomic load may require an addition
1995 element, so allocate enough to prevent a resize. */
1996 vec_alloc (params, 4);
1998 /* Remove the qualifiers for the rest of the expressions and
1999 create the VAL temp variable to hold the RHS. */
2000 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2001 tmp = create_tmp_var (nonatomic_type, NULL);
2002 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2003 TREE_ADDRESSABLE (tmp) = 1;
2005 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2006 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2007 params->quick_push (expr_addr);
2008 params->quick_push (tmp_addr);
2009 params->quick_push (seq_cst);
2010 func_call = build_function_call_vec (loc, fndecl, params, NULL);
2012 /* Return tmp which contains the value loaded. */
2013 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2015 return exp;
2018 /* EXP is an expression of integer type. Apply the integer promotions
2019 to it and return the promoted value. */
2021 tree
2022 perform_integral_promotions (tree exp)
2024 tree type = TREE_TYPE (exp);
2025 enum tree_code code = TREE_CODE (type);
2027 gcc_assert (INTEGRAL_TYPE_P (type));
2029 /* Normally convert enums to int,
2030 but convert wide enums to something wider. */
2031 if (code == ENUMERAL_TYPE)
2033 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2034 TYPE_PRECISION (integer_type_node)),
2035 ((TYPE_PRECISION (type)
2036 >= TYPE_PRECISION (integer_type_node))
2037 && TYPE_UNSIGNED (type)));
2039 return convert (type, exp);
2042 /* ??? This should no longer be needed now bit-fields have their
2043 proper types. */
2044 if (TREE_CODE (exp) == COMPONENT_REF
2045 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2046 /* If it's thinner than an int, promote it like a
2047 c_promoting_integer_type_p, otherwise leave it alone. */
2048 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2049 TYPE_PRECISION (integer_type_node)))
2050 return convert (integer_type_node, exp);
2052 if (c_promoting_integer_type_p (type))
2054 /* Preserve unsignedness if not really getting any wider. */
2055 if (TYPE_UNSIGNED (type)
2056 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2057 return convert (unsigned_type_node, exp);
2059 return convert (integer_type_node, exp);
2062 return exp;
2066 /* Perform default promotions for C data used in expressions.
2067 Enumeral types or short or char are converted to int.
2068 In addition, manifest constants symbols are replaced by their values. */
2070 tree
2071 default_conversion (tree exp)
2073 tree orig_exp;
2074 tree type = TREE_TYPE (exp);
2075 enum tree_code code = TREE_CODE (type);
2076 tree promoted_type;
2078 mark_exp_read (exp);
2080 /* Functions and arrays have been converted during parsing. */
2081 gcc_assert (code != FUNCTION_TYPE);
2082 if (code == ARRAY_TYPE)
2083 return exp;
2085 /* Constants can be used directly unless they're not loadable. */
2086 if (TREE_CODE (exp) == CONST_DECL)
2087 exp = DECL_INITIAL (exp);
2089 /* Strip no-op conversions. */
2090 orig_exp = exp;
2091 STRIP_TYPE_NOPS (exp);
2093 if (TREE_NO_WARNING (orig_exp))
2094 TREE_NO_WARNING (exp) = 1;
2096 if (code == VOID_TYPE)
2098 error ("void value not ignored as it ought to be");
2099 return error_mark_node;
2102 exp = require_complete_type (exp);
2103 if (exp == error_mark_node)
2104 return error_mark_node;
2106 promoted_type = targetm.promoted_type (type);
2107 if (promoted_type)
2108 return convert (promoted_type, exp);
2110 if (INTEGRAL_TYPE_P (type))
2111 return perform_integral_promotions (exp);
2113 return exp;
2116 /* Look up COMPONENT in a structure or union TYPE.
2118 If the component name is not found, returns NULL_TREE. Otherwise,
2119 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2120 stepping down the chain to the component, which is in the last
2121 TREE_VALUE of the list. Normally the list is of length one, but if
2122 the component is embedded within (nested) anonymous structures or
2123 unions, the list steps down the chain to the component. */
2125 static tree
2126 lookup_field (tree type, tree component)
2128 tree field;
2130 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2131 to the field elements. Use a binary search on this array to quickly
2132 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2133 will always be set for structures which have many elements. */
2135 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2137 int bot, top, half;
2138 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2140 field = TYPE_FIELDS (type);
2141 bot = 0;
2142 top = TYPE_LANG_SPECIFIC (type)->s->len;
2143 while (top - bot > 1)
2145 half = (top - bot + 1) >> 1;
2146 field = field_array[bot+half];
2148 if (DECL_NAME (field) == NULL_TREE)
2150 /* Step through all anon unions in linear fashion. */
2151 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2153 field = field_array[bot++];
2154 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2155 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2157 tree anon = lookup_field (TREE_TYPE (field), component);
2159 if (anon)
2160 return tree_cons (NULL_TREE, field, anon);
2162 /* The Plan 9 compiler permits referring
2163 directly to an anonymous struct/union field
2164 using a typedef name. */
2165 if (flag_plan9_extensions
2166 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2167 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2168 == TYPE_DECL)
2169 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2170 == component))
2171 break;
2175 /* Entire record is only anon unions. */
2176 if (bot > top)
2177 return NULL_TREE;
2179 /* Restart the binary search, with new lower bound. */
2180 continue;
2183 if (DECL_NAME (field) == component)
2184 break;
2185 if (DECL_NAME (field) < component)
2186 bot += half;
2187 else
2188 top = bot + half;
2191 if (DECL_NAME (field_array[bot]) == component)
2192 field = field_array[bot];
2193 else if (DECL_NAME (field) != component)
2194 return NULL_TREE;
2196 else
2198 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2200 if (DECL_NAME (field) == NULL_TREE
2201 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2202 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2204 tree anon = lookup_field (TREE_TYPE (field), component);
2206 if (anon)
2207 return tree_cons (NULL_TREE, field, anon);
2209 /* The Plan 9 compiler permits referring directly to an
2210 anonymous struct/union field using a typedef
2211 name. */
2212 if (flag_plan9_extensions
2213 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2214 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2215 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2216 == component))
2217 break;
2220 if (DECL_NAME (field) == component)
2221 break;
2224 if (field == NULL_TREE)
2225 return NULL_TREE;
2228 return tree_cons (NULL_TREE, field, NULL_TREE);
2231 /* Make an expression to refer to the COMPONENT field of structure or
2232 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2233 location of the COMPONENT_REF. */
2235 tree
2236 build_component_ref (location_t loc, tree datum, tree component)
2238 tree type = TREE_TYPE (datum);
2239 enum tree_code code = TREE_CODE (type);
2240 tree field = NULL;
2241 tree ref;
2242 bool datum_lvalue = lvalue_p (datum);
2244 if (!objc_is_public (datum, component))
2245 return error_mark_node;
2247 /* Detect Objective-C property syntax object.property. */
2248 if (c_dialect_objc ()
2249 && (ref = objc_maybe_build_component_ref (datum, component)))
2250 return ref;
2252 /* See if there is a field or component with name COMPONENT. */
2254 if (code == RECORD_TYPE || code == UNION_TYPE)
2256 if (!COMPLETE_TYPE_P (type))
2258 c_incomplete_type_error (NULL_TREE, type);
2259 return error_mark_node;
2262 field = lookup_field (type, component);
2264 if (!field)
2266 error_at (loc, "%qT has no member named %qE", type, component);
2267 return error_mark_node;
2270 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2271 This might be better solved in future the way the C++ front
2272 end does it - by giving the anonymous entities each a
2273 separate name and type, and then have build_component_ref
2274 recursively call itself. We can't do that here. */
2277 tree subdatum = TREE_VALUE (field);
2278 int quals;
2279 tree subtype;
2280 bool use_datum_quals;
2282 if (TREE_TYPE (subdatum) == error_mark_node)
2283 return error_mark_node;
2285 /* If this is an rvalue, it does not have qualifiers in C
2286 standard terms and we must avoid propagating such
2287 qualifiers down to a non-lvalue array that is then
2288 converted to a pointer. */
2289 use_datum_quals = (datum_lvalue
2290 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2292 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2293 if (use_datum_quals)
2294 quals |= TYPE_QUALS (TREE_TYPE (datum));
2295 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2297 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2298 NULL_TREE);
2299 SET_EXPR_LOCATION (ref, loc);
2300 if (TREE_READONLY (subdatum)
2301 || (use_datum_quals && TREE_READONLY (datum)))
2302 TREE_READONLY (ref) = 1;
2303 if (TREE_THIS_VOLATILE (subdatum)
2304 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2305 TREE_THIS_VOLATILE (ref) = 1;
2307 if (TREE_DEPRECATED (subdatum))
2308 warn_deprecated_use (subdatum, NULL_TREE);
2310 datum = ref;
2312 field = TREE_CHAIN (field);
2314 while (field);
2316 return ref;
2318 else if (code != ERROR_MARK)
2319 error_at (loc,
2320 "request for member %qE in something not a structure or union",
2321 component);
2323 return error_mark_node;
2326 /* Given an expression PTR for a pointer, return an expression
2327 for the value pointed to.
2328 ERRORSTRING is the name of the operator to appear in error messages.
2330 LOC is the location to use for the generated tree. */
2332 tree
2333 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2335 tree pointer = default_conversion (ptr);
2336 tree type = TREE_TYPE (pointer);
2337 tree ref;
2339 if (TREE_CODE (type) == POINTER_TYPE)
2341 if (CONVERT_EXPR_P (pointer)
2342 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2344 /* If a warning is issued, mark it to avoid duplicates from
2345 the backend. This only needs to be done at
2346 warn_strict_aliasing > 2. */
2347 if (warn_strict_aliasing > 2)
2348 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2349 type, TREE_OPERAND (pointer, 0)))
2350 TREE_NO_WARNING (pointer) = 1;
2353 if (TREE_CODE (pointer) == ADDR_EXPR
2354 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2355 == TREE_TYPE (type)))
2357 ref = TREE_OPERAND (pointer, 0);
2358 protected_set_expr_location (ref, loc);
2359 return ref;
2361 else
2363 tree t = TREE_TYPE (type);
2365 ref = build1 (INDIRECT_REF, t, pointer);
2367 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2369 error_at (loc, "dereferencing pointer to incomplete type");
2370 return error_mark_node;
2372 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2373 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2375 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2376 so that we get the proper error message if the result is used
2377 to assign to. Also, &* is supposed to be a no-op.
2378 And ANSI C seems to specify that the type of the result
2379 should be the const type. */
2380 /* A de-reference of a pointer to const is not a const. It is valid
2381 to change it via some other pointer. */
2382 TREE_READONLY (ref) = TYPE_READONLY (t);
2383 TREE_SIDE_EFFECTS (ref)
2384 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2385 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2386 protected_set_expr_location (ref, loc);
2387 return ref;
2390 else if (TREE_CODE (pointer) != ERROR_MARK)
2391 invalid_indirection_error (loc, type, errstring);
2393 return error_mark_node;
2396 /* This handles expressions of the form "a[i]", which denotes
2397 an array reference.
2399 This is logically equivalent in C to *(a+i), but we may do it differently.
2400 If A is a variable or a member, we generate a primitive ARRAY_REF.
2401 This avoids forcing the array out of registers, and can work on
2402 arrays that are not lvalues (for example, members of structures returned
2403 by functions).
2405 For vector types, allow vector[i] but not i[vector], and create
2406 *(((type*)&vectortype) + i) for the expression.
2408 LOC is the location to use for the returned expression. */
2410 tree
2411 build_array_ref (location_t loc, tree array, tree index)
2413 tree ret;
2414 bool swapped = false;
2415 if (TREE_TYPE (array) == error_mark_node
2416 || TREE_TYPE (index) == error_mark_node)
2417 return error_mark_node;
2419 if (flag_enable_cilkplus && contains_array_notation_expr (index))
2421 size_t rank = 0;
2422 if (!find_rank (loc, index, index, true, &rank))
2423 return error_mark_node;
2424 if (rank > 1)
2426 error_at (loc, "rank of the array's index is greater than 1");
2427 return error_mark_node;
2430 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2431 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2432 /* Allow vector[index] but not index[vector]. */
2433 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2435 tree temp;
2436 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2437 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2439 error_at (loc,
2440 "subscripted value is neither array nor pointer nor vector");
2442 return error_mark_node;
2444 temp = array;
2445 array = index;
2446 index = temp;
2447 swapped = true;
2450 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2452 error_at (loc, "array subscript is not an integer");
2453 return error_mark_node;
2456 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2458 error_at (loc, "subscripted value is pointer to function");
2459 return error_mark_node;
2462 /* ??? Existing practice has been to warn only when the char
2463 index is syntactically the index, not for char[array]. */
2464 if (!swapped)
2465 warn_array_subscript_with_type_char (index);
2467 /* Apply default promotions *after* noticing character types. */
2468 index = default_conversion (index);
2470 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2472 convert_vector_to_pointer_for_subscript (loc, &array, index);
2474 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2476 tree rval, type;
2478 /* An array that is indexed by a non-constant
2479 cannot be stored in a register; we must be able to do
2480 address arithmetic on its address.
2481 Likewise an array of elements of variable size. */
2482 if (TREE_CODE (index) != INTEGER_CST
2483 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2484 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2486 if (!c_mark_addressable (array))
2487 return error_mark_node;
2489 /* An array that is indexed by a constant value which is not within
2490 the array bounds cannot be stored in a register either; because we
2491 would get a crash in store_bit_field/extract_bit_field when trying
2492 to access a non-existent part of the register. */
2493 if (TREE_CODE (index) == INTEGER_CST
2494 && TYPE_DOMAIN (TREE_TYPE (array))
2495 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2497 if (!c_mark_addressable (array))
2498 return error_mark_node;
2501 if (pedantic)
2503 tree foo = array;
2504 while (TREE_CODE (foo) == COMPONENT_REF)
2505 foo = TREE_OPERAND (foo, 0);
2506 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2507 pedwarn (loc, OPT_Wpedantic,
2508 "ISO C forbids subscripting %<register%> array");
2509 else if (!flag_isoc99 && !lvalue_p (foo))
2510 pedwarn (loc, OPT_Wpedantic,
2511 "ISO C90 forbids subscripting non-lvalue array");
2514 type = TREE_TYPE (TREE_TYPE (array));
2515 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2516 /* Array ref is const/volatile if the array elements are
2517 or if the array is. */
2518 TREE_READONLY (rval)
2519 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2520 | TREE_READONLY (array));
2521 TREE_SIDE_EFFECTS (rval)
2522 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2523 | TREE_SIDE_EFFECTS (array));
2524 TREE_THIS_VOLATILE (rval)
2525 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2526 /* This was added by rms on 16 Nov 91.
2527 It fixes vol struct foo *a; a->elts[1]
2528 in an inline function.
2529 Hope it doesn't break something else. */
2530 | TREE_THIS_VOLATILE (array));
2531 ret = require_complete_type (rval);
2532 protected_set_expr_location (ret, loc);
2533 return ret;
2535 else
2537 tree ar = default_conversion (array);
2539 if (ar == error_mark_node)
2540 return ar;
2542 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2543 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2545 return build_indirect_ref
2546 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2547 RO_ARRAY_INDEXING);
2551 /* Build an external reference to identifier ID. FUN indicates
2552 whether this will be used for a function call. LOC is the source
2553 location of the identifier. This sets *TYPE to the type of the
2554 identifier, which is not the same as the type of the returned value
2555 for CONST_DECLs defined as enum constants. If the type of the
2556 identifier is not available, *TYPE is set to NULL. */
2557 tree
2558 build_external_ref (location_t loc, tree id, int fun, tree *type)
2560 tree ref;
2561 tree decl = lookup_name (id);
2563 /* In Objective-C, an instance variable (ivar) may be preferred to
2564 whatever lookup_name() found. */
2565 decl = objc_lookup_ivar (decl, id);
2567 *type = NULL;
2568 if (decl && decl != error_mark_node)
2570 ref = decl;
2571 *type = TREE_TYPE (ref);
2573 else if (fun)
2574 /* Implicit function declaration. */
2575 ref = implicitly_declare (loc, id);
2576 else if (decl == error_mark_node)
2577 /* Don't complain about something that's already been
2578 complained about. */
2579 return error_mark_node;
2580 else
2582 undeclared_variable (loc, id);
2583 return error_mark_node;
2586 if (TREE_TYPE (ref) == error_mark_node)
2587 return error_mark_node;
2589 if (TREE_DEPRECATED (ref))
2590 warn_deprecated_use (ref, NULL_TREE);
2592 /* Recursive call does not count as usage. */
2593 if (ref != current_function_decl)
2595 TREE_USED (ref) = 1;
2598 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2600 if (!in_sizeof && !in_typeof)
2601 C_DECL_USED (ref) = 1;
2602 else if (DECL_INITIAL (ref) == 0
2603 && DECL_EXTERNAL (ref)
2604 && !TREE_PUBLIC (ref))
2605 record_maybe_used_decl (ref);
2608 if (TREE_CODE (ref) == CONST_DECL)
2610 used_types_insert (TREE_TYPE (ref));
2612 if (warn_cxx_compat
2613 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2614 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2616 warning_at (loc, OPT_Wc___compat,
2617 ("enum constant defined in struct or union "
2618 "is not visible in C++"));
2619 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2622 ref = DECL_INITIAL (ref);
2623 TREE_CONSTANT (ref) = 1;
2625 else if (current_function_decl != 0
2626 && !DECL_FILE_SCOPE_P (current_function_decl)
2627 && (TREE_CODE (ref) == VAR_DECL
2628 || TREE_CODE (ref) == PARM_DECL
2629 || TREE_CODE (ref) == FUNCTION_DECL))
2631 tree context = decl_function_context (ref);
2633 if (context != 0 && context != current_function_decl)
2634 DECL_NONLOCAL (ref) = 1;
2636 /* C99 6.7.4p3: An inline definition of a function with external
2637 linkage ... shall not contain a reference to an identifier with
2638 internal linkage. */
2639 else if (current_function_decl != 0
2640 && DECL_DECLARED_INLINE_P (current_function_decl)
2641 && DECL_EXTERNAL (current_function_decl)
2642 && VAR_OR_FUNCTION_DECL_P (ref)
2643 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2644 && ! TREE_PUBLIC (ref)
2645 && DECL_CONTEXT (ref) != current_function_decl)
2646 record_inline_static (loc, current_function_decl, ref,
2647 csi_internal);
2649 return ref;
2652 /* Record details of decls possibly used inside sizeof or typeof. */
2653 struct maybe_used_decl
2655 /* The decl. */
2656 tree decl;
2657 /* The level seen at (in_sizeof + in_typeof). */
2658 int level;
2659 /* The next one at this level or above, or NULL. */
2660 struct maybe_used_decl *next;
2663 static struct maybe_used_decl *maybe_used_decls;
2665 /* Record that DECL, an undefined static function reference seen
2666 inside sizeof or typeof, might be used if the operand of sizeof is
2667 a VLA type or the operand of typeof is a variably modified
2668 type. */
2670 static void
2671 record_maybe_used_decl (tree decl)
2673 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2674 t->decl = decl;
2675 t->level = in_sizeof + in_typeof;
2676 t->next = maybe_used_decls;
2677 maybe_used_decls = t;
2680 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2681 USED is false, just discard them. If it is true, mark them used
2682 (if no longer inside sizeof or typeof) or move them to the next
2683 level up (if still inside sizeof or typeof). */
2685 void
2686 pop_maybe_used (bool used)
2688 struct maybe_used_decl *p = maybe_used_decls;
2689 int cur_level = in_sizeof + in_typeof;
2690 while (p && p->level > cur_level)
2692 if (used)
2694 if (cur_level == 0)
2695 C_DECL_USED (p->decl) = 1;
2696 else
2697 p->level = cur_level;
2699 p = p->next;
2701 if (!used || cur_level == 0)
2702 maybe_used_decls = p;
2705 /* Return the result of sizeof applied to EXPR. */
2707 struct c_expr
2708 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2710 struct c_expr ret;
2711 if (expr.value == error_mark_node)
2713 ret.value = error_mark_node;
2714 ret.original_code = ERROR_MARK;
2715 ret.original_type = NULL;
2716 pop_maybe_used (false);
2718 else
2720 bool expr_const_operands = true;
2721 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2722 &expr_const_operands);
2723 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2724 c_last_sizeof_arg = expr.value;
2725 ret.original_code = SIZEOF_EXPR;
2726 ret.original_type = NULL;
2727 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2729 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2730 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2731 folded_expr, ret.value);
2732 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2733 SET_EXPR_LOCATION (ret.value, loc);
2735 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2737 return ret;
2740 /* Return the result of sizeof applied to T, a structure for the type
2741 name passed to sizeof (rather than the type itself). LOC is the
2742 location of the original expression. */
2744 struct c_expr
2745 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2747 tree type;
2748 struct c_expr ret;
2749 tree type_expr = NULL_TREE;
2750 bool type_expr_const = true;
2751 type = groktypename (t, &type_expr, &type_expr_const);
2752 ret.value = c_sizeof (loc, type);
2753 c_last_sizeof_arg = type;
2754 ret.original_code = SIZEOF_EXPR;
2755 ret.original_type = NULL;
2756 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2757 && c_vla_type_p (type))
2759 /* If the type is a [*] array, it is a VLA but is represented as
2760 having a size of zero. In such a case we must ensure that
2761 the result of sizeof does not get folded to a constant by
2762 c_fully_fold, because if the size is evaluated the result is
2763 not constant and so constraints on zero or negative size
2764 arrays must not be applied when this sizeof call is inside
2765 another array declarator. */
2766 if (!type_expr)
2767 type_expr = integer_zero_node;
2768 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2769 type_expr, ret.value);
2770 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2772 pop_maybe_used (type != error_mark_node
2773 ? C_TYPE_VARIABLE_SIZE (type) : false);
2774 return ret;
2777 /* Build a function call to function FUNCTION with parameters PARAMS.
2778 The function call is at LOC.
2779 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2780 TREE_VALUE of each node is a parameter-expression.
2781 FUNCTION's data type may be a function type or a pointer-to-function. */
2783 tree
2784 build_function_call (location_t loc, tree function, tree params)
2786 vec<tree, va_gc> *v;
2787 tree ret;
2789 vec_alloc (v, list_length (params));
2790 for (; params; params = TREE_CHAIN (params))
2791 v->quick_push (TREE_VALUE (params));
2792 ret = build_function_call_vec (loc, function, v, NULL);
2793 vec_free (v);
2794 return ret;
2797 /* Give a note about the location of the declaration of DECL. */
2799 static void inform_declaration (tree decl)
2801 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2802 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2805 /* Build a function call to function FUNCTION with parameters PARAMS.
2806 ORIGTYPES, if not NULL, is a vector of types; each element is
2807 either NULL or the original type of the corresponding element in
2808 PARAMS. The original type may differ from TREE_TYPE of the
2809 parameter for enums. FUNCTION's data type may be a function type
2810 or pointer-to-function. This function changes the elements of
2811 PARAMS. */
2813 tree
2814 build_function_call_vec (location_t loc, tree function,
2815 vec<tree, va_gc> *params,
2816 vec<tree, va_gc> *origtypes)
2818 tree fntype, fundecl = 0;
2819 tree name = NULL_TREE, result;
2820 tree tem;
2821 int nargs;
2822 tree *argarray;
2825 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2826 STRIP_TYPE_NOPS (function);
2828 /* Convert anything with function type to a pointer-to-function. */
2829 if (TREE_CODE (function) == FUNCTION_DECL)
2831 /* Implement type-directed function overloading for builtins.
2832 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2833 handle all the type checking. The result is a complete expression
2834 that implements this function call. */
2835 tem = resolve_overloaded_builtin (loc, function, params);
2836 if (tem)
2837 return tem;
2839 name = DECL_NAME (function);
2841 if (flag_tm)
2842 tm_malloc_replacement (function);
2843 fundecl = function;
2844 /* Atomic functions have type checking/casting already done. They are
2845 often rewritten and don't match the original parameter list. */
2846 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2847 origtypes = NULL;
2849 if (flag_enable_cilkplus
2850 && is_cilkplus_reduce_builtin (function))
2851 origtypes = NULL;
2853 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2854 function = function_to_pointer_conversion (loc, function);
2856 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2857 expressions, like those used for ObjC messenger dispatches. */
2858 if (params && !params->is_empty ())
2859 function = objc_rewrite_function_call (function, (*params)[0]);
2861 function = c_fully_fold (function, false, NULL);
2863 fntype = TREE_TYPE (function);
2865 if (TREE_CODE (fntype) == ERROR_MARK)
2866 return error_mark_node;
2868 if (!(TREE_CODE (fntype) == POINTER_TYPE
2869 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2871 if (!flag_diagnostics_show_caret)
2872 error_at (loc,
2873 "called object %qE is not a function or function pointer",
2874 function);
2875 else if (DECL_P (function))
2877 error_at (loc,
2878 "called object %qD is not a function or function pointer",
2879 function);
2880 inform_declaration (function);
2882 else
2883 error_at (loc,
2884 "called object is not a function or function pointer");
2885 return error_mark_node;
2888 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2889 current_function_returns_abnormally = 1;
2891 /* fntype now gets the type of function pointed to. */
2892 fntype = TREE_TYPE (fntype);
2894 /* Convert the parameters to the types declared in the
2895 function prototype, or apply default promotions. */
2897 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2898 function, fundecl);
2899 if (nargs < 0)
2900 return error_mark_node;
2902 /* Check that the function is called through a compatible prototype.
2903 If it is not, replace the call by a trap, wrapped up in a compound
2904 expression if necessary. This has the nice side-effect to prevent
2905 the tree-inliner from generating invalid assignment trees which may
2906 blow up in the RTL expander later. */
2907 if (CONVERT_EXPR_P (function)
2908 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2909 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2910 && !comptypes (fntype, TREE_TYPE (tem)))
2912 tree return_type = TREE_TYPE (fntype);
2913 tree trap = build_function_call (loc,
2914 builtin_decl_explicit (BUILT_IN_TRAP),
2915 NULL_TREE);
2916 int i;
2918 /* This situation leads to run-time undefined behavior. We can't,
2919 therefore, simply error unless we can prove that all possible
2920 executions of the program must execute the code. */
2921 if (warning_at (loc, 0, "function called through a non-compatible type"))
2922 /* We can, however, treat "undefined" any way we please.
2923 Call abort to encourage the user to fix the program. */
2924 inform (loc, "if this code is reached, the program will abort");
2925 /* Before the abort, allow the function arguments to exit or
2926 call longjmp. */
2927 for (i = 0; i < nargs; i++)
2928 trap = build2 (COMPOUND_EXPR, void_type_node, (*params)[i], trap);
2930 if (VOID_TYPE_P (return_type))
2932 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2933 pedwarn (loc, 0,
2934 "function with qualified void return type called");
2935 return trap;
2937 else
2939 tree rhs;
2941 if (AGGREGATE_TYPE_P (return_type))
2942 rhs = build_compound_literal (loc, return_type,
2943 build_constructor (return_type,
2944 NULL),
2945 false);
2946 else
2947 rhs = build_zero_cst (return_type);
2949 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2950 trap, rhs));
2954 argarray = vec_safe_address (params);
2956 /* Check that arguments to builtin functions match the expectations. */
2957 if (fundecl
2958 && DECL_BUILT_IN (fundecl)
2959 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2960 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2961 return error_mark_node;
2963 /* Check that the arguments to the function are valid. */
2964 check_function_arguments (fntype, nargs, argarray);
2966 if (name != NULL_TREE
2967 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2969 if (require_constant_value)
2970 result =
2971 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2972 function, nargs, argarray);
2973 else
2974 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2975 function, nargs, argarray);
2976 if (TREE_CODE (result) == NOP_EXPR
2977 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2978 STRIP_TYPE_NOPS (result);
2980 else
2981 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2982 function, nargs, argarray);
2984 if (VOID_TYPE_P (TREE_TYPE (result)))
2986 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2987 pedwarn (loc, 0,
2988 "function with qualified void return type called");
2989 return result;
2991 return require_complete_type (result);
2994 /* Convert the argument expressions in the vector VALUES
2995 to the types in the list TYPELIST.
2997 If TYPELIST is exhausted, or when an element has NULL as its type,
2998 perform the default conversions.
3000 ORIGTYPES is the original types of the expressions in VALUES. This
3001 holds the type of enum values which have been converted to integral
3002 types. It may be NULL.
3004 FUNCTION is a tree for the called function. It is used only for
3005 error messages, where it is formatted with %qE.
3007 This is also where warnings about wrong number of args are generated.
3009 Returns the actual number of arguments processed (which may be less
3010 than the length of VALUES in some error situations), or -1 on
3011 failure. */
3013 static int
3014 convert_arguments (tree typelist, vec<tree, va_gc> *values,
3015 vec<tree, va_gc> *origtypes, tree function, tree fundecl)
3017 tree typetail, val;
3018 unsigned int parmnum;
3019 bool error_args = false;
3020 const bool type_generic = fundecl
3021 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
3022 bool type_generic_remove_excess_precision = false;
3023 tree selector;
3025 /* Change pointer to function to the function itself for
3026 diagnostics. */
3027 if (TREE_CODE (function) == ADDR_EXPR
3028 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3029 function = TREE_OPERAND (function, 0);
3031 /* Handle an ObjC selector specially for diagnostics. */
3032 selector = objc_message_selector ();
3034 /* For type-generic built-in functions, determine whether excess
3035 precision should be removed (classification) or not
3036 (comparison). */
3037 if (type_generic
3038 && DECL_BUILT_IN (fundecl)
3039 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3041 switch (DECL_FUNCTION_CODE (fundecl))
3043 case BUILT_IN_ISFINITE:
3044 case BUILT_IN_ISINF:
3045 case BUILT_IN_ISINF_SIGN:
3046 case BUILT_IN_ISNAN:
3047 case BUILT_IN_ISNORMAL:
3048 case BUILT_IN_FPCLASSIFY:
3049 type_generic_remove_excess_precision = true;
3050 break;
3052 default:
3053 type_generic_remove_excess_precision = false;
3054 break;
3057 if (flag_enable_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3058 return vec_safe_length (values);
3060 /* Scan the given expressions and types, producing individual
3061 converted arguments. */
3063 for (typetail = typelist, parmnum = 0;
3064 values && values->iterate (parmnum, &val);
3065 ++parmnum)
3067 tree type = typetail ? TREE_VALUE (typetail) : 0;
3068 tree valtype = TREE_TYPE (val);
3069 tree rname = function;
3070 int argnum = parmnum + 1;
3071 const char *invalid_func_diag;
3072 bool excess_precision = false;
3073 bool npc;
3074 tree parmval;
3076 if (type == void_type_node)
3078 if (selector)
3079 error_at (input_location,
3080 "too many arguments to method %qE", selector);
3081 else
3082 error_at (input_location,
3083 "too many arguments to function %qE", function);
3084 inform_declaration (fundecl);
3085 return parmnum;
3088 if (selector && argnum > 2)
3090 rname = selector;
3091 argnum -= 2;
3094 npc = null_pointer_constant_p (val);
3096 /* If there is excess precision and a prototype, convert once to
3097 the required type rather than converting via the semantic
3098 type. Likewise without a prototype a float value represented
3099 as long double should be converted once to double. But for
3100 type-generic classification functions excess precision must
3101 be removed here. */
3102 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3103 && (type || !type_generic || !type_generic_remove_excess_precision))
3105 val = TREE_OPERAND (val, 0);
3106 excess_precision = true;
3108 val = c_fully_fold (val, false, NULL);
3109 STRIP_TYPE_NOPS (val);
3111 val = require_complete_type (val);
3113 if (type != 0)
3115 /* Formal parm type is specified by a function prototype. */
3117 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3119 error ("type of formal parameter %d is incomplete", parmnum + 1);
3120 parmval = val;
3122 else
3124 tree origtype;
3126 /* Optionally warn about conversions that
3127 differ from the default conversions. */
3128 if (warn_traditional_conversion || warn_traditional)
3130 unsigned int formal_prec = TYPE_PRECISION (type);
3132 if (INTEGRAL_TYPE_P (type)
3133 && TREE_CODE (valtype) == REAL_TYPE)
3134 warning (0, "passing argument %d of %qE as integer "
3135 "rather than floating due to prototype",
3136 argnum, rname);
3137 if (INTEGRAL_TYPE_P (type)
3138 && TREE_CODE (valtype) == COMPLEX_TYPE)
3139 warning (0, "passing argument %d of %qE as integer "
3140 "rather than complex due to prototype",
3141 argnum, rname);
3142 else if (TREE_CODE (type) == COMPLEX_TYPE
3143 && TREE_CODE (valtype) == REAL_TYPE)
3144 warning (0, "passing argument %d of %qE as complex "
3145 "rather than floating due to prototype",
3146 argnum, rname);
3147 else if (TREE_CODE (type) == REAL_TYPE
3148 && INTEGRAL_TYPE_P (valtype))
3149 warning (0, "passing argument %d of %qE as floating "
3150 "rather than integer due to prototype",
3151 argnum, rname);
3152 else if (TREE_CODE (type) == COMPLEX_TYPE
3153 && INTEGRAL_TYPE_P (valtype))
3154 warning (0, "passing argument %d of %qE as complex "
3155 "rather than integer due to prototype",
3156 argnum, rname);
3157 else if (TREE_CODE (type) == REAL_TYPE
3158 && TREE_CODE (valtype) == COMPLEX_TYPE)
3159 warning (0, "passing argument %d of %qE as floating "
3160 "rather than complex due to prototype",
3161 argnum, rname);
3162 /* ??? At some point, messages should be written about
3163 conversions between complex types, but that's too messy
3164 to do now. */
3165 else if (TREE_CODE (type) == REAL_TYPE
3166 && TREE_CODE (valtype) == REAL_TYPE)
3168 /* Warn if any argument is passed as `float',
3169 since without a prototype it would be `double'. */
3170 if (formal_prec == TYPE_PRECISION (float_type_node)
3171 && type != dfloat32_type_node)
3172 warning (0, "passing argument %d of %qE as %<float%> "
3173 "rather than %<double%> due to prototype",
3174 argnum, rname);
3176 /* Warn if mismatch between argument and prototype
3177 for decimal float types. Warn of conversions with
3178 binary float types and of precision narrowing due to
3179 prototype. */
3180 else if (type != valtype
3181 && (type == dfloat32_type_node
3182 || type == dfloat64_type_node
3183 || type == dfloat128_type_node
3184 || valtype == dfloat32_type_node
3185 || valtype == dfloat64_type_node
3186 || valtype == dfloat128_type_node)
3187 && (formal_prec
3188 <= TYPE_PRECISION (valtype)
3189 || (type == dfloat128_type_node
3190 && (valtype
3191 != dfloat64_type_node
3192 && (valtype
3193 != dfloat32_type_node)))
3194 || (type == dfloat64_type_node
3195 && (valtype
3196 != dfloat32_type_node))))
3197 warning (0, "passing argument %d of %qE as %qT "
3198 "rather than %qT due to prototype",
3199 argnum, rname, type, valtype);
3202 /* Detect integer changing in width or signedness.
3203 These warnings are only activated with
3204 -Wtraditional-conversion, not with -Wtraditional. */
3205 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3206 && INTEGRAL_TYPE_P (valtype))
3208 tree would_have_been = default_conversion (val);
3209 tree type1 = TREE_TYPE (would_have_been);
3211 if (TREE_CODE (type) == ENUMERAL_TYPE
3212 && (TYPE_MAIN_VARIANT (type)
3213 == TYPE_MAIN_VARIANT (valtype)))
3214 /* No warning if function asks for enum
3215 and the actual arg is that enum type. */
3217 else if (formal_prec != TYPE_PRECISION (type1))
3218 warning (OPT_Wtraditional_conversion,
3219 "passing argument %d of %qE "
3220 "with different width due to prototype",
3221 argnum, rname);
3222 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3224 /* Don't complain if the formal parameter type
3225 is an enum, because we can't tell now whether
3226 the value was an enum--even the same enum. */
3227 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3229 else if (TREE_CODE (val) == INTEGER_CST
3230 && int_fits_type_p (val, type))
3231 /* Change in signedness doesn't matter
3232 if a constant value is unaffected. */
3234 /* If the value is extended from a narrower
3235 unsigned type, it doesn't matter whether we
3236 pass it as signed or unsigned; the value
3237 certainly is the same either way. */
3238 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3239 && TYPE_UNSIGNED (valtype))
3241 else if (TYPE_UNSIGNED (type))
3242 warning (OPT_Wtraditional_conversion,
3243 "passing argument %d of %qE "
3244 "as unsigned due to prototype",
3245 argnum, rname);
3246 else
3247 warning (OPT_Wtraditional_conversion,
3248 "passing argument %d of %qE "
3249 "as signed due to prototype", argnum, rname);
3253 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3254 sake of better warnings from convert_and_check. */
3255 if (excess_precision)
3256 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3257 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3258 parmval = convert_for_assignment (input_location, type, val,
3259 origtype, ic_argpass, npc,
3260 fundecl, function,
3261 parmnum + 1);
3263 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3264 && INTEGRAL_TYPE_P (type)
3265 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3266 parmval = default_conversion (parmval);
3269 else if (TREE_CODE (valtype) == REAL_TYPE
3270 && (TYPE_PRECISION (valtype)
3271 <= TYPE_PRECISION (double_type_node))
3272 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3273 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3274 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3276 if (type_generic)
3277 parmval = val;
3278 else
3280 /* Convert `float' to `double'. */
3281 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3282 warning (OPT_Wdouble_promotion,
3283 "implicit conversion from %qT to %qT when passing "
3284 "argument to function",
3285 valtype, double_type_node);
3286 parmval = convert (double_type_node, val);
3289 else if (excess_precision && !type_generic)
3290 /* A "double" argument with excess precision being passed
3291 without a prototype or in variable arguments. */
3292 parmval = convert (valtype, val);
3293 else if ((invalid_func_diag =
3294 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3296 error (invalid_func_diag);
3297 return -1;
3299 else
3300 /* Convert `short' and `char' to full-size `int'. */
3301 parmval = default_conversion (val);
3303 (*values)[parmnum] = parmval;
3304 if (parmval == error_mark_node)
3305 error_args = true;
3307 if (typetail)
3308 typetail = TREE_CHAIN (typetail);
3311 gcc_assert (parmnum == vec_safe_length (values));
3313 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3315 error_at (input_location,
3316 "too few arguments to function %qE", function);
3317 inform_declaration (fundecl);
3318 return -1;
3321 return error_args ? -1 : (int) parmnum;
3324 /* This is the entry point used by the parser to build unary operators
3325 in the input. CODE, a tree_code, specifies the unary operator, and
3326 ARG is the operand. For unary plus, the C parser currently uses
3327 CONVERT_EXPR for code.
3329 LOC is the location to use for the tree generated.
3332 struct c_expr
3333 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3335 struct c_expr result;
3337 result.value = build_unary_op (loc, code, arg.value, 0);
3338 result.original_code = code;
3339 result.original_type = NULL;
3341 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3342 overflow_warning (loc, result.value);
3344 return result;
3347 /* This is the entry point used by the parser to build binary operators
3348 in the input. CODE, a tree_code, specifies the binary operator, and
3349 ARG1 and ARG2 are the operands. In addition to constructing the
3350 expression, we check for operands that were written with other binary
3351 operators in a way that is likely to confuse the user.
3353 LOCATION is the location of the binary operator. */
3355 struct c_expr
3356 parser_build_binary_op (location_t location, enum tree_code code,
3357 struct c_expr arg1, struct c_expr arg2)
3359 struct c_expr result;
3361 enum tree_code code1 = arg1.original_code;
3362 enum tree_code code2 = arg2.original_code;
3363 tree type1 = (arg1.original_type
3364 ? arg1.original_type
3365 : TREE_TYPE (arg1.value));
3366 tree type2 = (arg2.original_type
3367 ? arg2.original_type
3368 : TREE_TYPE (arg2.value));
3370 result.value = build_binary_op (location, code,
3371 arg1.value, arg2.value, 1);
3372 result.original_code = code;
3373 result.original_type = NULL;
3375 if (TREE_CODE (result.value) == ERROR_MARK)
3376 return result;
3378 if (location != UNKNOWN_LOCATION)
3379 protected_set_expr_location (result.value, location);
3381 /* Check for cases such as x+y<<z which users are likely
3382 to misinterpret. */
3383 if (warn_parentheses)
3384 warn_about_parentheses (input_location, code,
3385 code1, arg1.value, code2, arg2.value);
3387 if (warn_logical_op)
3388 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3389 code1, arg1.value, code2, arg2.value);
3391 /* Warn about comparisons against string literals, with the exception
3392 of testing for equality or inequality of a string literal with NULL. */
3393 if (code == EQ_EXPR || code == NE_EXPR)
3395 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3396 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3397 warning_at (location, OPT_Waddress,
3398 "comparison with string literal results in unspecified behavior");
3400 else if (TREE_CODE_CLASS (code) == tcc_comparison
3401 && (code1 == STRING_CST || code2 == STRING_CST))
3402 warning_at (location, OPT_Waddress,
3403 "comparison with string literal results in unspecified behavior");
3405 if (TREE_OVERFLOW_P (result.value)
3406 && !TREE_OVERFLOW_P (arg1.value)
3407 && !TREE_OVERFLOW_P (arg2.value))
3408 overflow_warning (location, result.value);
3410 /* Warn about comparisons of different enum types. */
3411 if (warn_enum_compare
3412 && TREE_CODE_CLASS (code) == tcc_comparison
3413 && TREE_CODE (type1) == ENUMERAL_TYPE
3414 && TREE_CODE (type2) == ENUMERAL_TYPE
3415 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3416 warning_at (location, OPT_Wenum_compare,
3417 "comparison between %qT and %qT",
3418 type1, type2);
3420 return result;
3423 /* Return a tree for the difference of pointers OP0 and OP1.
3424 The resulting tree has type int. */
3426 static tree
3427 pointer_diff (location_t loc, tree op0, tree op1)
3429 tree restype = ptrdiff_type_node;
3430 tree result, inttype;
3432 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3433 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3434 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3435 tree con0, con1, lit0, lit1;
3436 tree orig_op1 = op1;
3438 /* If the operands point into different address spaces, we need to
3439 explicitly convert them to pointers into the common address space
3440 before we can subtract the numerical address values. */
3441 if (as0 != as1)
3443 addr_space_t as_common;
3444 tree common_type;
3446 /* Determine the common superset address space. This is guaranteed
3447 to exist because the caller verified that comp_target_types
3448 returned non-zero. */
3449 if (!addr_space_superset (as0, as1, &as_common))
3450 gcc_unreachable ();
3452 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3453 op0 = convert (common_type, op0);
3454 op1 = convert (common_type, op1);
3457 /* Determine integer type to perform computations in. This will usually
3458 be the same as the result type (ptrdiff_t), but may need to be a wider
3459 type if pointers for the address space are wider than ptrdiff_t. */
3460 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3461 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3462 else
3463 inttype = restype;
3466 if (TREE_CODE (target_type) == VOID_TYPE)
3467 pedwarn (loc, OPT_Wpointer_arith,
3468 "pointer of type %<void *%> used in subtraction");
3469 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3470 pedwarn (loc, OPT_Wpointer_arith,
3471 "pointer to a function used in subtraction");
3473 /* If the conversion to ptrdiff_type does anything like widening or
3474 converting a partial to an integral mode, we get a convert_expression
3475 that is in the way to do any simplifications.
3476 (fold-const.c doesn't know that the extra bits won't be needed.
3477 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3478 different mode in place.)
3479 So first try to find a common term here 'by hand'; we want to cover
3480 at least the cases that occur in legal static initializers. */
3481 if (CONVERT_EXPR_P (op0)
3482 && (TYPE_PRECISION (TREE_TYPE (op0))
3483 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3484 con0 = TREE_OPERAND (op0, 0);
3485 else
3486 con0 = op0;
3487 if (CONVERT_EXPR_P (op1)
3488 && (TYPE_PRECISION (TREE_TYPE (op1))
3489 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3490 con1 = TREE_OPERAND (op1, 0);
3491 else
3492 con1 = op1;
3494 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3496 lit0 = TREE_OPERAND (con0, 1);
3497 con0 = TREE_OPERAND (con0, 0);
3499 else
3500 lit0 = integer_zero_node;
3502 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
3504 lit1 = TREE_OPERAND (con1, 1);
3505 con1 = TREE_OPERAND (con1, 0);
3507 else
3508 lit1 = integer_zero_node;
3510 if (operand_equal_p (con0, con1, 0))
3512 op0 = lit0;
3513 op1 = lit1;
3517 /* First do the subtraction as integers;
3518 then drop through to build the divide operator.
3519 Do not do default conversions on the minus operator
3520 in case restype is a short type. */
3522 op0 = build_binary_op (loc,
3523 MINUS_EXPR, convert (inttype, op0),
3524 convert (inttype, op1), 0);
3525 /* This generates an error if op1 is pointer to incomplete type. */
3526 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3527 error_at (loc, "arithmetic on pointer to an incomplete type");
3529 /* This generates an error if op0 is pointer to incomplete type. */
3530 op1 = c_size_in_bytes (target_type);
3532 /* Divide by the size, in easiest possible way. */
3533 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3534 op0, convert (inttype, op1));
3536 /* Convert to final result type if necessary. */
3537 return convert (restype, result);
3540 /* Expand atomic compound assignments into an approriate sequence as
3541 specified by the C11 standard section 6.5.16.2.
3542 given
3543 _Atomic T1 E1
3544 T2 E2
3545 E1 op= E2
3547 This sequence is used for all types for which these operations are
3548 supported.
3550 In addition, built-in versions of the 'fe' prefixed routines may
3551 need to be invoked for floating point (real, complex or vector) when
3552 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3554 T1 newval;
3555 T1 old;
3556 T1 *addr
3557 T2 val
3558 fenv_t fenv
3560 addr = &E1;
3561 val = (E2);
3562 __atomic_load (addr, &old, SEQ_CST);
3563 feholdexcept (&fenv);
3564 loop:
3565 newval = old op val;
3566 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3567 SEQ_CST))
3568 goto done;
3569 feclearexcept (FE_ALL_EXCEPT);
3570 goto loop:
3571 done:
3572 feupdateenv (&fenv);
3574 Also note that the compiler is simply issuing the generic form of
3575 the atomic operations. This requires temp(s) and has their address
3576 taken. The atomic processing is smart enough to figure out when the
3577 size of an object can utilize a lock-free version, and convert the
3578 built-in call to the appropriate lock-free routine. The optimizers
3579 will then dispose of any temps that are no longer required, and
3580 lock-free implementations are utilized as long as there is target
3581 support for the required size.
3583 If the operator is NOP_EXPR, then this is a simple assignment, and
3584 an __atomic_store is issued to perform the assignment rather than
3585 the above loop.
3589 /* Build an atomic assignment at LOC, expanding into the proper
3590 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3591 the result of the operation, unless RETURN_OLD_P in which case
3592 return the old value of LHS (this is only for postincrement and
3593 postdecrement). */
3594 static tree
3595 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3596 tree rhs, bool return_old_p)
3598 tree fndecl, func_call;
3599 vec<tree, va_gc> *params;
3600 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3601 tree old, old_addr;
3602 tree compound_stmt;
3603 tree stmt, goto_stmt;
3604 tree loop_label, loop_decl, done_label, done_decl;
3606 tree lhs_type = TREE_TYPE (lhs);
3607 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3608 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3609 tree rhs_type = TREE_TYPE (rhs);
3611 gcc_assert (TYPE_ATOMIC (lhs_type));
3613 if (return_old_p)
3614 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3616 /* Allocate enough vector items for a compare_exchange. */
3617 vec_alloc (params, 6);
3619 /* Create a compound statement to hold the sequence of statements
3620 with a loop. */
3621 compound_stmt = c_begin_compound_stmt (false);
3623 /* Fold the RHS if it hasn't already been folded. */
3624 if (modifycode != NOP_EXPR)
3625 rhs = c_fully_fold (rhs, false, NULL);
3627 /* Remove the qualifiers for the rest of the expressions and create
3628 the VAL temp variable to hold the RHS. */
3629 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3630 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3631 val = create_tmp_var (nonatomic_rhs_type, NULL);
3632 TREE_ADDRESSABLE (val) = 1;
3633 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3634 SET_EXPR_LOCATION (rhs, loc);
3635 add_stmt (rhs);
3637 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3638 an atomic_store. */
3639 if (modifycode == NOP_EXPR)
3641 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3642 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3643 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3644 params->quick_push (lhs_addr);
3645 params->quick_push (rhs);
3646 params->quick_push (seq_cst);
3647 func_call = build_function_call_vec (loc, fndecl, params, NULL);
3648 add_stmt (func_call);
3650 /* Finish the compound statement. */
3651 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3653 /* VAL is the value which was stored, return a COMPOUND_STMT of
3654 the statement and that value. */
3655 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3658 /* Create the variables and labels required for the op= form. */
3659 old = create_tmp_var (nonatomic_lhs_type, NULL);
3660 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3661 TREE_ADDRESSABLE (val) = 1;
3663 newval = create_tmp_var (nonatomic_lhs_type, NULL);
3664 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3665 TREE_ADDRESSABLE (newval) = 1;
3667 loop_decl = create_artificial_label (loc);
3668 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3670 done_decl = create_artificial_label (loc);
3671 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3673 /* __atomic_load (addr, &old, SEQ_CST). */
3674 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3675 params->quick_push (lhs_addr);
3676 params->quick_push (old_addr);
3677 params->quick_push (seq_cst);
3678 func_call = build_function_call_vec (loc, fndecl, params, NULL);
3679 add_stmt (func_call);
3680 params->truncate (0);
3682 /* Create the expressions for floating-point environment
3683 manipulation, if required. */
3684 bool need_fenv = (flag_trapping_math
3685 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3686 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3687 if (need_fenv)
3688 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3690 if (hold_call)
3691 add_stmt (hold_call);
3693 /* loop: */
3694 add_stmt (loop_label);
3696 /* newval = old + val; */
3697 rhs = build_binary_op (loc, modifycode, old, val, 1);
3698 rhs = convert_for_assignment (loc, nonatomic_lhs_type, rhs, NULL_TREE,
3699 ic_assign, false, NULL_TREE,
3700 NULL_TREE, 0);
3701 if (rhs != error_mark_node)
3703 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3704 SET_EXPR_LOCATION (rhs, loc);
3705 add_stmt (rhs);
3708 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3709 goto done; */
3710 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3711 params->quick_push (lhs_addr);
3712 params->quick_push (old_addr);
3713 params->quick_push (newval_addr);
3714 params->quick_push (integer_zero_node);
3715 params->quick_push (seq_cst);
3716 params->quick_push (seq_cst);
3717 func_call = build_function_call_vec (loc, fndecl, params, NULL);
3719 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3720 SET_EXPR_LOCATION (goto_stmt, loc);
3722 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3723 SET_EXPR_LOCATION (stmt, loc);
3724 add_stmt (stmt);
3726 if (clear_call)
3727 add_stmt (clear_call);
3729 /* goto loop; */
3730 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3731 SET_EXPR_LOCATION (goto_stmt, loc);
3732 add_stmt (goto_stmt);
3734 /* done: */
3735 add_stmt (done_label);
3737 if (update_call)
3738 add_stmt (update_call);
3740 /* Finish the compound statement. */
3741 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3743 /* NEWVAL is the value that was successfully stored, return a
3744 COMPOUND_EXPR of the statement and the appropriate value. */
3745 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3746 return_old_p ? old : newval);
3749 /* Construct and perhaps optimize a tree representation
3750 for a unary operation. CODE, a tree_code, specifies the operation
3751 and XARG is the operand.
3752 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3753 the default promotions (such as from short to int).
3754 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3755 allows non-lvalues; this is only used to handle conversion of non-lvalue
3756 arrays to pointers in C99.
3758 LOCATION is the location of the operator. */
3760 tree
3761 build_unary_op (location_t location,
3762 enum tree_code code, tree xarg, int flag)
3764 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3765 tree arg = xarg;
3766 tree argtype = 0;
3767 enum tree_code typecode;
3768 tree val;
3769 tree ret = error_mark_node;
3770 tree eptype = NULL_TREE;
3771 int noconvert = flag;
3772 const char *invalid_op_diag;
3773 bool int_operands;
3775 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3776 if (int_operands)
3777 arg = remove_c_maybe_const_expr (arg);
3779 if (code != ADDR_EXPR)
3780 arg = require_complete_type (arg);
3782 typecode = TREE_CODE (TREE_TYPE (arg));
3783 if (typecode == ERROR_MARK)
3784 return error_mark_node;
3785 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3786 typecode = INTEGER_TYPE;
3788 if ((invalid_op_diag
3789 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3791 error_at (location, invalid_op_diag);
3792 return error_mark_node;
3795 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3797 eptype = TREE_TYPE (arg);
3798 arg = TREE_OPERAND (arg, 0);
3801 switch (code)
3803 case CONVERT_EXPR:
3804 /* This is used for unary plus, because a CONVERT_EXPR
3805 is enough to prevent anybody from looking inside for
3806 associativity, but won't generate any code. */
3807 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3808 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3809 || typecode == VECTOR_TYPE))
3811 error_at (location, "wrong type argument to unary plus");
3812 return error_mark_node;
3814 else if (!noconvert)
3815 arg = default_conversion (arg);
3816 arg = non_lvalue_loc (location, arg);
3817 break;
3819 case NEGATE_EXPR:
3820 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3821 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3822 || typecode == VECTOR_TYPE))
3824 error_at (location, "wrong type argument to unary minus");
3825 return error_mark_node;
3827 else if (!noconvert)
3828 arg = default_conversion (arg);
3829 break;
3831 case BIT_NOT_EXPR:
3832 /* ~ works on integer types and non float vectors. */
3833 if (typecode == INTEGER_TYPE
3834 || (typecode == VECTOR_TYPE
3835 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3837 if (!noconvert)
3838 arg = default_conversion (arg);
3840 else if (typecode == COMPLEX_TYPE)
3842 code = CONJ_EXPR;
3843 pedwarn (location, OPT_Wpedantic,
3844 "ISO C does not support %<~%> for complex conjugation");
3845 if (!noconvert)
3846 arg = default_conversion (arg);
3848 else
3850 error_at (location, "wrong type argument to bit-complement");
3851 return error_mark_node;
3853 break;
3855 case ABS_EXPR:
3856 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3858 error_at (location, "wrong type argument to abs");
3859 return error_mark_node;
3861 else if (!noconvert)
3862 arg = default_conversion (arg);
3863 break;
3865 case CONJ_EXPR:
3866 /* Conjugating a real value is a no-op, but allow it anyway. */
3867 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3868 || typecode == COMPLEX_TYPE))
3870 error_at (location, "wrong type argument to conjugation");
3871 return error_mark_node;
3873 else if (!noconvert)
3874 arg = default_conversion (arg);
3875 break;
3877 case TRUTH_NOT_EXPR:
3878 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3879 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3880 && typecode != COMPLEX_TYPE)
3882 error_at (location,
3883 "wrong type argument to unary exclamation mark");
3884 return error_mark_node;
3886 if (int_operands)
3888 arg = c_objc_common_truthvalue_conversion (location, xarg);
3889 arg = remove_c_maybe_const_expr (arg);
3891 else
3892 arg = c_objc_common_truthvalue_conversion (location, arg);
3893 ret = invert_truthvalue_loc (location, arg);
3894 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3895 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3896 location = EXPR_LOCATION (ret);
3897 goto return_build_unary_op;
3899 case REALPART_EXPR:
3900 case IMAGPART_EXPR:
3901 ret = build_real_imag_expr (location, code, arg);
3902 if (ret == error_mark_node)
3903 return error_mark_node;
3904 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3905 eptype = TREE_TYPE (eptype);
3906 goto return_build_unary_op;
3908 case PREINCREMENT_EXPR:
3909 case POSTINCREMENT_EXPR:
3910 case PREDECREMENT_EXPR:
3911 case POSTDECREMENT_EXPR:
3913 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3915 tree inner = build_unary_op (location, code,
3916 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3917 if (inner == error_mark_node)
3918 return error_mark_node;
3919 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3920 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3921 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3922 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3923 goto return_build_unary_op;
3926 /* Complain about anything that is not a true lvalue. In
3927 Objective-C, skip this check for property_refs. */
3928 if (!objc_is_property_ref (arg)
3929 && !lvalue_or_else (location,
3930 arg, ((code == PREINCREMENT_EXPR
3931 || code == POSTINCREMENT_EXPR)
3932 ? lv_increment
3933 : lv_decrement)))
3934 return error_mark_node;
3936 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3938 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3939 warning_at (location, OPT_Wc___compat,
3940 "increment of enumeration value is invalid in C++");
3941 else
3942 warning_at (location, OPT_Wc___compat,
3943 "decrement of enumeration value is invalid in C++");
3946 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3947 arg = c_fully_fold (arg, false, NULL);
3949 bool atomic_op;
3950 atomic_op = really_atomic_lvalue (arg);
3952 /* Increment or decrement the real part of the value,
3953 and don't change the imaginary part. */
3954 if (typecode == COMPLEX_TYPE)
3956 tree real, imag;
3958 pedwarn (location, OPT_Wpedantic,
3959 "ISO C does not support %<++%> and %<--%> on complex types");
3961 if (!atomic_op)
3963 arg = stabilize_reference (arg);
3964 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3965 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3966 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3967 if (real == error_mark_node || imag == error_mark_node)
3968 return error_mark_node;
3969 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3970 real, imag);
3971 goto return_build_unary_op;
3975 /* Report invalid types. */
3977 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3978 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
3979 && typecode != COMPLEX_TYPE)
3981 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3982 error_at (location, "wrong type argument to increment");
3983 else
3984 error_at (location, "wrong type argument to decrement");
3986 return error_mark_node;
3990 tree inc;
3992 argtype = TREE_TYPE (arg);
3994 /* Compute the increment. */
3996 if (typecode == POINTER_TYPE)
3998 /* If pointer target is an undefined struct,
3999 we just cannot know how to do the arithmetic. */
4000 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4002 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4003 error_at (location,
4004 "increment of pointer to unknown structure");
4005 else
4006 error_at (location,
4007 "decrement of pointer to unknown structure");
4009 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4010 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4012 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4013 pedwarn (location, OPT_Wpointer_arith,
4014 "wrong type argument to increment");
4015 else
4016 pedwarn (location, OPT_Wpointer_arith,
4017 "wrong type argument to decrement");
4020 inc = c_size_in_bytes (TREE_TYPE (argtype));
4021 inc = convert_to_ptrofftype_loc (location, inc);
4023 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4025 /* For signed fract types, we invert ++ to -- or
4026 -- to ++, and change inc from 1 to -1, because
4027 it is not possible to represent 1 in signed fract constants.
4028 For unsigned fract types, the result always overflows and
4029 we get an undefined (original) or the maximum value. */
4030 if (code == PREINCREMENT_EXPR)
4031 code = PREDECREMENT_EXPR;
4032 else if (code == PREDECREMENT_EXPR)
4033 code = PREINCREMENT_EXPR;
4034 else if (code == POSTINCREMENT_EXPR)
4035 code = POSTDECREMENT_EXPR;
4036 else /* code == POSTDECREMENT_EXPR */
4037 code = POSTINCREMENT_EXPR;
4039 inc = integer_minus_one_node;
4040 inc = convert (argtype, inc);
4042 else
4044 inc = integer_one_node;
4045 inc = convert (argtype, inc);
4048 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4049 need to ask Objective-C to build the increment or decrement
4050 expression for it. */
4051 if (objc_is_property_ref (arg))
4052 return objc_build_incr_expr_for_property_ref (location, code,
4053 arg, inc);
4055 /* Report a read-only lvalue. */
4056 if (TYPE_READONLY (argtype))
4058 readonly_error (location, arg,
4059 ((code == PREINCREMENT_EXPR
4060 || code == POSTINCREMENT_EXPR)
4061 ? lv_increment : lv_decrement));
4062 return error_mark_node;
4064 else if (TREE_READONLY (arg))
4065 readonly_warning (arg,
4066 ((code == PREINCREMENT_EXPR
4067 || code == POSTINCREMENT_EXPR)
4068 ? lv_increment : lv_decrement));
4070 /* If the argument is atomic, use the special code sequences for
4071 atomic compound assignment. */
4072 if (atomic_op)
4074 arg = stabilize_reference (arg);
4075 ret = build_atomic_assign (location, arg,
4076 ((code == PREINCREMENT_EXPR
4077 || code == POSTINCREMENT_EXPR)
4078 ? PLUS_EXPR
4079 : MINUS_EXPR),
4080 (FRACT_MODE_P (TYPE_MODE (argtype))
4081 ? inc
4082 : integer_one_node),
4083 (code == POSTINCREMENT_EXPR
4084 || code == POSTDECREMENT_EXPR));
4085 goto return_build_unary_op;
4088 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4089 val = boolean_increment (code, arg);
4090 else
4091 val = build2 (code, TREE_TYPE (arg), arg, inc);
4092 TREE_SIDE_EFFECTS (val) = 1;
4093 if (TREE_CODE (val) != code)
4094 TREE_NO_WARNING (val) = 1;
4095 ret = val;
4096 goto return_build_unary_op;
4099 case ADDR_EXPR:
4100 /* Note that this operation never does default_conversion. */
4102 /* The operand of unary '&' must be an lvalue (which excludes
4103 expressions of type void), or, in C99, the result of a [] or
4104 unary '*' operator. */
4105 if (VOID_TYPE_P (TREE_TYPE (arg))
4106 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4107 && (TREE_CODE (arg) != INDIRECT_REF
4108 || !flag_isoc99))
4109 pedwarn (location, 0, "taking address of expression of type %<void%>");
4111 /* Let &* cancel out to simplify resulting code. */
4112 if (TREE_CODE (arg) == INDIRECT_REF)
4114 /* Don't let this be an lvalue. */
4115 if (lvalue_p (TREE_OPERAND (arg, 0)))
4116 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4117 ret = TREE_OPERAND (arg, 0);
4118 goto return_build_unary_op;
4121 /* For &x[y], return x+y */
4122 if (TREE_CODE (arg) == ARRAY_REF)
4124 tree op0 = TREE_OPERAND (arg, 0);
4125 if (!c_mark_addressable (op0))
4126 return error_mark_node;
4129 /* Anything not already handled and not a true memory reference
4130 or a non-lvalue array is an error. */
4131 else if (typecode != FUNCTION_TYPE && !flag
4132 && !lvalue_or_else (location, arg, lv_addressof))
4133 return error_mark_node;
4135 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4136 folding later. */
4137 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4139 tree inner = build_unary_op (location, code,
4140 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4141 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4142 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4143 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4144 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4145 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4146 goto return_build_unary_op;
4149 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4150 argtype = TREE_TYPE (arg);
4152 /* If the lvalue is const or volatile, merge that into the type
4153 to which the address will point. This is only needed
4154 for function types. */
4155 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4156 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4157 && TREE_CODE (argtype) == FUNCTION_TYPE)
4159 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4160 int quals = orig_quals;
4162 if (TREE_READONLY (arg))
4163 quals |= TYPE_QUAL_CONST;
4164 if (TREE_THIS_VOLATILE (arg))
4165 quals |= TYPE_QUAL_VOLATILE;
4167 argtype = c_build_qualified_type (argtype, quals);
4170 if (!c_mark_addressable (arg))
4171 return error_mark_node;
4173 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4174 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4176 argtype = build_pointer_type (argtype);
4178 /* ??? Cope with user tricks that amount to offsetof. Delete this
4179 when we have proper support for integer constant expressions. */
4180 val = get_base_address (arg);
4181 if (val && TREE_CODE (val) == INDIRECT_REF
4182 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4184 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4185 goto return_build_unary_op;
4188 val = build1 (ADDR_EXPR, argtype, arg);
4190 ret = val;
4191 goto return_build_unary_op;
4193 default:
4194 gcc_unreachable ();
4197 if (argtype == 0)
4198 argtype = TREE_TYPE (arg);
4199 if (TREE_CODE (arg) == INTEGER_CST)
4200 ret = (require_constant_value
4201 ? fold_build1_initializer_loc (location, code, argtype, arg)
4202 : fold_build1_loc (location, code, argtype, arg));
4203 else
4204 ret = build1 (code, argtype, arg);
4205 return_build_unary_op:
4206 gcc_assert (ret != error_mark_node);
4207 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4208 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4209 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4210 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4211 ret = note_integer_operands (ret);
4212 if (eptype)
4213 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4214 protected_set_expr_location (ret, location);
4215 return ret;
4218 /* Return nonzero if REF is an lvalue valid for this language.
4219 Lvalues can be assigned, unless their type has TYPE_READONLY.
4220 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4222 bool
4223 lvalue_p (const_tree ref)
4225 const enum tree_code code = TREE_CODE (ref);
4227 switch (code)
4229 case REALPART_EXPR:
4230 case IMAGPART_EXPR:
4231 case COMPONENT_REF:
4232 return lvalue_p (TREE_OPERAND (ref, 0));
4234 case C_MAYBE_CONST_EXPR:
4235 return lvalue_p (TREE_OPERAND (ref, 1));
4237 case COMPOUND_LITERAL_EXPR:
4238 case STRING_CST:
4239 return 1;
4241 case INDIRECT_REF:
4242 case ARRAY_REF:
4243 case ARRAY_NOTATION_REF:
4244 case VAR_DECL:
4245 case PARM_DECL:
4246 case RESULT_DECL:
4247 case ERROR_MARK:
4248 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4249 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4251 case BIND_EXPR:
4252 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4254 default:
4255 return 0;
4259 /* Give a warning for storing in something that is read-only in GCC
4260 terms but not const in ISO C terms. */
4262 static void
4263 readonly_warning (tree arg, enum lvalue_use use)
4265 switch (use)
4267 case lv_assign:
4268 warning (0, "assignment of read-only location %qE", arg);
4269 break;
4270 case lv_increment:
4271 warning (0, "increment of read-only location %qE", arg);
4272 break;
4273 case lv_decrement:
4274 warning (0, "decrement of read-only location %qE", arg);
4275 break;
4276 default:
4277 gcc_unreachable ();
4279 return;
4283 /* Return nonzero if REF is an lvalue valid for this language;
4284 otherwise, print an error message and return zero. USE says
4285 how the lvalue is being used and so selects the error message.
4286 LOCATION is the location at which any error should be reported. */
4288 static int
4289 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4291 int win = lvalue_p (ref);
4293 if (!win)
4294 lvalue_error (loc, use);
4296 return win;
4299 /* Mark EXP saying that we need to be able to take the
4300 address of it; it should not be allocated in a register.
4301 Returns true if successful. */
4303 bool
4304 c_mark_addressable (tree exp)
4306 tree x = exp;
4308 while (1)
4309 switch (TREE_CODE (x))
4311 case COMPONENT_REF:
4312 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4314 error
4315 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4316 return false;
4319 /* ... fall through ... */
4321 case ADDR_EXPR:
4322 case ARRAY_REF:
4323 case REALPART_EXPR:
4324 case IMAGPART_EXPR:
4325 x = TREE_OPERAND (x, 0);
4326 break;
4328 case COMPOUND_LITERAL_EXPR:
4329 case CONSTRUCTOR:
4330 TREE_ADDRESSABLE (x) = 1;
4331 return true;
4333 case VAR_DECL:
4334 case CONST_DECL:
4335 case PARM_DECL:
4336 case RESULT_DECL:
4337 if (C_DECL_REGISTER (x)
4338 && DECL_NONLOCAL (x))
4340 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4342 error
4343 ("global register variable %qD used in nested function", x);
4344 return false;
4346 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4348 else if (C_DECL_REGISTER (x))
4350 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4351 error ("address of global register variable %qD requested", x);
4352 else
4353 error ("address of register variable %qD requested", x);
4354 return false;
4357 /* drops in */
4358 case FUNCTION_DECL:
4359 TREE_ADDRESSABLE (x) = 1;
4360 /* drops out */
4361 default:
4362 return true;
4366 /* Convert EXPR to TYPE, warning about conversion problems with
4367 constants. SEMANTIC_TYPE is the type this conversion would use
4368 without excess precision. If SEMANTIC_TYPE is NULL, this function
4369 is equivalent to convert_and_check. This function is a wrapper that
4370 handles conversions that may be different than
4371 the usual ones because of excess precision. */
4373 static tree
4374 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4376 if (TREE_TYPE (expr) == type)
4377 return expr;
4379 if (!semantic_type)
4380 return convert_and_check (type, expr);
4382 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4383 && TREE_TYPE (expr) != semantic_type)
4385 /* For integers, we need to check the real conversion, not
4386 the conversion to the excess precision type. */
4387 expr = convert_and_check (semantic_type, expr);
4389 /* Result type is the excess precision type, which should be
4390 large enough, so do not check. */
4391 return convert (type, expr);
4394 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4395 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4396 if folded to an integer constant then the unselected half may
4397 contain arbitrary operations not normally permitted in constant
4398 expressions. Set the location of the expression to LOC. */
4400 tree
4401 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4402 tree op1, tree op1_original_type, tree op2,
4403 tree op2_original_type)
4405 tree type1;
4406 tree type2;
4407 enum tree_code code1;
4408 enum tree_code code2;
4409 tree result_type = NULL;
4410 tree semantic_result_type = NULL;
4411 tree orig_op1 = op1, orig_op2 = op2;
4412 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4413 bool ifexp_int_operands;
4414 tree ret;
4416 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4417 if (op1_int_operands)
4418 op1 = remove_c_maybe_const_expr (op1);
4419 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4420 if (op2_int_operands)
4421 op2 = remove_c_maybe_const_expr (op2);
4422 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4423 if (ifexp_int_operands)
4424 ifexp = remove_c_maybe_const_expr (ifexp);
4426 /* Promote both alternatives. */
4428 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4429 op1 = default_conversion (op1);
4430 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4431 op2 = default_conversion (op2);
4433 if (TREE_CODE (ifexp) == ERROR_MARK
4434 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4435 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4436 return error_mark_node;
4438 type1 = TREE_TYPE (op1);
4439 code1 = TREE_CODE (type1);
4440 type2 = TREE_TYPE (op2);
4441 code2 = TREE_CODE (type2);
4443 /* C90 does not permit non-lvalue arrays in conditional expressions.
4444 In C99 they will be pointers by now. */
4445 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4447 error_at (colon_loc, "non-lvalue array in conditional expression");
4448 return error_mark_node;
4451 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4452 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4453 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4454 || code1 == COMPLEX_TYPE)
4455 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4456 || code2 == COMPLEX_TYPE))
4458 semantic_result_type = c_common_type (type1, type2);
4459 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4461 op1 = TREE_OPERAND (op1, 0);
4462 type1 = TREE_TYPE (op1);
4463 gcc_assert (TREE_CODE (type1) == code1);
4465 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4467 op2 = TREE_OPERAND (op2, 0);
4468 type2 = TREE_TYPE (op2);
4469 gcc_assert (TREE_CODE (type2) == code2);
4473 if (warn_cxx_compat)
4475 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4476 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4478 if (TREE_CODE (t1) == ENUMERAL_TYPE
4479 && TREE_CODE (t2) == ENUMERAL_TYPE
4480 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4481 warning_at (colon_loc, OPT_Wc___compat,
4482 ("different enum types in conditional is "
4483 "invalid in C++: %qT vs %qT"),
4484 t1, t2);
4487 /* Quickly detect the usual case where op1 and op2 have the same type
4488 after promotion. */
4489 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4491 if (type1 == type2)
4492 result_type = type1;
4493 else
4494 result_type = TYPE_MAIN_VARIANT (type1);
4496 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4497 || code1 == COMPLEX_TYPE)
4498 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4499 || code2 == COMPLEX_TYPE))
4501 result_type = c_common_type (type1, type2);
4502 do_warn_double_promotion (result_type, type1, type2,
4503 "implicit conversion from %qT to %qT to "
4504 "match other result of conditional",
4505 colon_loc);
4507 /* If -Wsign-compare, warn here if type1 and type2 have
4508 different signedness. We'll promote the signed to unsigned
4509 and later code won't know it used to be different.
4510 Do this check on the original types, so that explicit casts
4511 will be considered, but default promotions won't. */
4512 if (c_inhibit_evaluation_warnings == 0)
4514 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4515 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4517 if (unsigned_op1 ^ unsigned_op2)
4519 bool ovf;
4521 /* Do not warn if the result type is signed, since the
4522 signed type will only be chosen if it can represent
4523 all the values of the unsigned type. */
4524 if (!TYPE_UNSIGNED (result_type))
4525 /* OK */;
4526 else
4528 bool op1_maybe_const = true;
4529 bool op2_maybe_const = true;
4531 /* Do not warn if the signed quantity is an
4532 unsuffixed integer literal (or some static
4533 constant expression involving such literals) and
4534 it is non-negative. This warning requires the
4535 operands to be folded for best results, so do
4536 that folding in this case even without
4537 warn_sign_compare to avoid warning options
4538 possibly affecting code generation. */
4539 c_inhibit_evaluation_warnings
4540 += (ifexp == truthvalue_false_node);
4541 op1 = c_fully_fold (op1, require_constant_value,
4542 &op1_maybe_const);
4543 c_inhibit_evaluation_warnings
4544 -= (ifexp == truthvalue_false_node);
4546 c_inhibit_evaluation_warnings
4547 += (ifexp == truthvalue_true_node);
4548 op2 = c_fully_fold (op2, require_constant_value,
4549 &op2_maybe_const);
4550 c_inhibit_evaluation_warnings
4551 -= (ifexp == truthvalue_true_node);
4553 if (warn_sign_compare)
4555 if ((unsigned_op2
4556 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4557 || (unsigned_op1
4558 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4559 /* OK */;
4560 else
4561 warning_at (colon_loc, OPT_Wsign_compare,
4562 ("signed and unsigned type in "
4563 "conditional expression"));
4565 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4566 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4567 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4568 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4573 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4575 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4576 pedwarn (colon_loc, OPT_Wpedantic,
4577 "ISO C forbids conditional expr with only one void side");
4578 result_type = void_type_node;
4580 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4582 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4583 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4584 addr_space_t as_common;
4586 if (comp_target_types (colon_loc, type1, type2))
4587 result_type = common_pointer_type (type1, type2);
4588 else if (null_pointer_constant_p (orig_op1))
4589 result_type = type2;
4590 else if (null_pointer_constant_p (orig_op2))
4591 result_type = type1;
4592 else if (!addr_space_superset (as1, as2, &as_common))
4594 error_at (colon_loc, "pointers to disjoint address spaces "
4595 "used in conditional expression");
4596 return error_mark_node;
4598 else if (VOID_TYPE_P (TREE_TYPE (type1))
4599 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4601 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4602 pedwarn (colon_loc, OPT_Wpedantic,
4603 "ISO C forbids conditional expr between "
4604 "%<void *%> and function pointer");
4605 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4606 TREE_TYPE (type2)));
4608 else if (VOID_TYPE_P (TREE_TYPE (type2))
4609 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4611 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4612 pedwarn (colon_loc, OPT_Wpedantic,
4613 "ISO C forbids conditional expr between "
4614 "%<void *%> and function pointer");
4615 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4616 TREE_TYPE (type1)));
4618 /* Objective-C pointer comparisons are a bit more lenient. */
4619 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4620 result_type = objc_common_type (type1, type2);
4621 else
4623 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4625 pedwarn (colon_loc, 0,
4626 "pointer type mismatch in conditional expression");
4627 result_type = build_pointer_type
4628 (build_qualified_type (void_type_node, qual));
4631 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4633 if (!null_pointer_constant_p (orig_op2))
4634 pedwarn (colon_loc, 0,
4635 "pointer/integer type mismatch in conditional expression");
4636 else
4638 op2 = null_pointer_node;
4640 result_type = type1;
4642 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4644 if (!null_pointer_constant_p (orig_op1))
4645 pedwarn (colon_loc, 0,
4646 "pointer/integer type mismatch in conditional expression");
4647 else
4649 op1 = null_pointer_node;
4651 result_type = type2;
4654 if (!result_type)
4656 if (flag_cond_mismatch)
4657 result_type = void_type_node;
4658 else
4660 error_at (colon_loc, "type mismatch in conditional expression");
4661 return error_mark_node;
4665 /* Merge const and volatile flags of the incoming types. */
4666 result_type
4667 = build_type_variant (result_type,
4668 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4669 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4671 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4672 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4674 if (ifexp_bcp && ifexp == truthvalue_true_node)
4676 op2_int_operands = true;
4677 op1 = c_fully_fold (op1, require_constant_value, NULL);
4679 if (ifexp_bcp && ifexp == truthvalue_false_node)
4681 op1_int_operands = true;
4682 op2 = c_fully_fold (op2, require_constant_value, NULL);
4684 int_const = int_operands = (ifexp_int_operands
4685 && op1_int_operands
4686 && op2_int_operands);
4687 if (int_operands)
4689 int_const = ((ifexp == truthvalue_true_node
4690 && TREE_CODE (orig_op1) == INTEGER_CST
4691 && !TREE_OVERFLOW (orig_op1))
4692 || (ifexp == truthvalue_false_node
4693 && TREE_CODE (orig_op2) == INTEGER_CST
4694 && !TREE_OVERFLOW (orig_op2)));
4696 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4697 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4698 else
4700 if (int_operands)
4702 op1 = remove_c_maybe_const_expr (op1);
4703 op2 = remove_c_maybe_const_expr (op2);
4705 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4706 if (int_operands)
4707 ret = note_integer_operands (ret);
4709 if (semantic_result_type)
4710 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4712 protected_set_expr_location (ret, colon_loc);
4713 return ret;
4716 /* Return a compound expression that performs two expressions and
4717 returns the value of the second of them.
4719 LOC is the location of the COMPOUND_EXPR. */
4721 tree
4722 build_compound_expr (location_t loc, tree expr1, tree expr2)
4724 bool expr1_int_operands, expr2_int_operands;
4725 tree eptype = NULL_TREE;
4726 tree ret;
4728 if (flag_enable_cilkplus
4729 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4730 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4732 error_at (loc,
4733 "spawned function call cannot be part of a comma expression");
4734 return error_mark_node;
4736 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4737 if (expr1_int_operands)
4738 expr1 = remove_c_maybe_const_expr (expr1);
4739 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4740 if (expr2_int_operands)
4741 expr2 = remove_c_maybe_const_expr (expr2);
4743 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4744 expr1 = TREE_OPERAND (expr1, 0);
4745 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4747 eptype = TREE_TYPE (expr2);
4748 expr2 = TREE_OPERAND (expr2, 0);
4751 if (!TREE_SIDE_EFFECTS (expr1))
4753 /* The left-hand operand of a comma expression is like an expression
4754 statement: with -Wunused, we should warn if it doesn't have
4755 any side-effects, unless it was explicitly cast to (void). */
4756 if (warn_unused_value)
4758 if (VOID_TYPE_P (TREE_TYPE (expr1))
4759 && CONVERT_EXPR_P (expr1))
4760 ; /* (void) a, b */
4761 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4762 && TREE_CODE (expr1) == COMPOUND_EXPR
4763 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4764 ; /* (void) a, (void) b, c */
4765 else
4766 warning_at (loc, OPT_Wunused_value,
4767 "left-hand operand of comma expression has no effect");
4771 /* With -Wunused, we should also warn if the left-hand operand does have
4772 side-effects, but computes a value which is not used. For example, in
4773 `foo() + bar(), baz()' the result of the `+' operator is not used,
4774 so we should issue a warning. */
4775 else if (warn_unused_value)
4776 warn_if_unused_value (expr1, loc);
4778 if (expr2 == error_mark_node)
4779 return error_mark_node;
4781 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4783 if (flag_isoc99
4784 && expr1_int_operands
4785 && expr2_int_operands)
4786 ret = note_integer_operands (ret);
4788 if (eptype)
4789 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4791 protected_set_expr_location (ret, loc);
4792 return ret;
4795 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4796 which we are casting. OTYPE is the type of the expression being
4797 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4798 of the cast. -Wcast-qual appeared on the command line. Named
4799 address space qualifiers are not handled here, because they result
4800 in different warnings. */
4802 static void
4803 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4805 tree in_type = type;
4806 tree in_otype = otype;
4807 int added = 0;
4808 int discarded = 0;
4809 bool is_const;
4811 /* Check that the qualifiers on IN_TYPE are a superset of the
4812 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4813 nodes is uninteresting and we stop as soon as we hit a
4814 non-POINTER_TYPE node on either type. */
4817 in_otype = TREE_TYPE (in_otype);
4818 in_type = TREE_TYPE (in_type);
4820 /* GNU C allows cv-qualified function types. 'const' means the
4821 function is very pure, 'volatile' means it can't return. We
4822 need to warn when such qualifiers are added, not when they're
4823 taken away. */
4824 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4825 && TREE_CODE (in_type) == FUNCTION_TYPE)
4826 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4827 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4828 else
4829 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4830 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4832 while (TREE_CODE (in_type) == POINTER_TYPE
4833 && TREE_CODE (in_otype) == POINTER_TYPE);
4835 if (added)
4836 warning_at (loc, OPT_Wcast_qual,
4837 "cast adds %q#v qualifier to function type", added);
4839 if (discarded)
4840 /* There are qualifiers present in IN_OTYPE that are not present
4841 in IN_TYPE. */
4842 warning_at (loc, OPT_Wcast_qual,
4843 "cast discards %q#v qualifier from pointer target type",
4844 discarded);
4846 if (added || discarded)
4847 return;
4849 /* A cast from **T to const **T is unsafe, because it can cause a
4850 const value to be changed with no additional warning. We only
4851 issue this warning if T is the same on both sides, and we only
4852 issue the warning if there are the same number of pointers on
4853 both sides, as otherwise the cast is clearly unsafe anyhow. A
4854 cast is unsafe when a qualifier is added at one level and const
4855 is not present at all outer levels.
4857 To issue this warning, we check at each level whether the cast
4858 adds new qualifiers not already seen. We don't need to special
4859 case function types, as they won't have the same
4860 TYPE_MAIN_VARIANT. */
4862 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4863 return;
4864 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4865 return;
4867 in_type = type;
4868 in_otype = otype;
4869 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4872 in_type = TREE_TYPE (in_type);
4873 in_otype = TREE_TYPE (in_otype);
4874 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4875 && !is_const)
4877 warning_at (loc, OPT_Wcast_qual,
4878 "to be safe all intermediate pointers in cast from "
4879 "%qT to %qT must be %<const%> qualified",
4880 otype, type);
4881 break;
4883 if (is_const)
4884 is_const = TYPE_READONLY (in_type);
4886 while (TREE_CODE (in_type) == POINTER_TYPE);
4889 /* Build an expression representing a cast to type TYPE of expression EXPR.
4890 LOC is the location of the cast-- typically the open paren of the cast. */
4892 tree
4893 build_c_cast (location_t loc, tree type, tree expr)
4895 tree value;
4897 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4898 expr = TREE_OPERAND (expr, 0);
4900 value = expr;
4902 if (type == error_mark_node || expr == error_mark_node)
4903 return error_mark_node;
4905 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4906 only in <protocol> qualifications. But when constructing cast expressions,
4907 the protocols do matter and must be kept around. */
4908 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4909 return build1 (NOP_EXPR, type, expr);
4911 type = TYPE_MAIN_VARIANT (type);
4913 if (TREE_CODE (type) == ARRAY_TYPE)
4915 error_at (loc, "cast specifies array type");
4916 return error_mark_node;
4919 if (TREE_CODE (type) == FUNCTION_TYPE)
4921 error_at (loc, "cast specifies function type");
4922 return error_mark_node;
4925 if (!VOID_TYPE_P (type))
4927 value = require_complete_type (value);
4928 if (value == error_mark_node)
4929 return error_mark_node;
4932 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4934 if (TREE_CODE (type) == RECORD_TYPE
4935 || TREE_CODE (type) == UNION_TYPE)
4936 pedwarn (loc, OPT_Wpedantic,
4937 "ISO C forbids casting nonscalar to the same type");
4939 else if (TREE_CODE (type) == UNION_TYPE)
4941 tree field;
4943 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4944 if (TREE_TYPE (field) != error_mark_node
4945 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4946 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4947 break;
4949 if (field)
4951 tree t;
4952 bool maybe_const = true;
4954 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
4955 t = c_fully_fold (value, false, &maybe_const);
4956 t = build_constructor_single (type, field, t);
4957 if (!maybe_const)
4958 t = c_wrap_maybe_const (t, true);
4959 t = digest_init (loc, type, t,
4960 NULL_TREE, false, true, 0);
4961 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4962 return t;
4964 error_at (loc, "cast to union type from type not present in union");
4965 return error_mark_node;
4967 else
4969 tree otype, ovalue;
4971 if (type == void_type_node)
4973 tree t = build1 (CONVERT_EXPR, type, value);
4974 SET_EXPR_LOCATION (t, loc);
4975 return t;
4978 otype = TREE_TYPE (value);
4980 /* Optionally warn about potentially worrisome casts. */
4981 if (warn_cast_qual
4982 && TREE_CODE (type) == POINTER_TYPE
4983 && TREE_CODE (otype) == POINTER_TYPE)
4984 handle_warn_cast_qual (loc, type, otype);
4986 /* Warn about conversions between pointers to disjoint
4987 address spaces. */
4988 if (TREE_CODE (type) == POINTER_TYPE
4989 && TREE_CODE (otype) == POINTER_TYPE
4990 && !null_pointer_constant_p (value))
4992 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4993 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4994 addr_space_t as_common;
4996 if (!addr_space_superset (as_to, as_from, &as_common))
4998 if (ADDR_SPACE_GENERIC_P (as_from))
4999 warning_at (loc, 0, "cast to %s address space pointer "
5000 "from disjoint generic address space pointer",
5001 c_addr_space_name (as_to));
5003 else if (ADDR_SPACE_GENERIC_P (as_to))
5004 warning_at (loc, 0, "cast to generic address space pointer "
5005 "from disjoint %s address space pointer",
5006 c_addr_space_name (as_from));
5008 else
5009 warning_at (loc, 0, "cast to %s address space pointer "
5010 "from disjoint %s address space pointer",
5011 c_addr_space_name (as_to),
5012 c_addr_space_name (as_from));
5016 /* Warn about possible alignment problems. */
5017 if (STRICT_ALIGNMENT
5018 && TREE_CODE (type) == POINTER_TYPE
5019 && TREE_CODE (otype) == POINTER_TYPE
5020 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5021 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5022 /* Don't warn about opaque types, where the actual alignment
5023 restriction is unknown. */
5024 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5025 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5026 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5027 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5028 warning_at (loc, OPT_Wcast_align,
5029 "cast increases required alignment of target type");
5031 if (TREE_CODE (type) == INTEGER_TYPE
5032 && TREE_CODE (otype) == POINTER_TYPE
5033 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5034 /* Unlike conversion of integers to pointers, where the
5035 warning is disabled for converting constants because
5036 of cases such as SIG_*, warn about converting constant
5037 pointers to integers. In some cases it may cause unwanted
5038 sign extension, and a warning is appropriate. */
5039 warning_at (loc, OPT_Wpointer_to_int_cast,
5040 "cast from pointer to integer of different size");
5042 if (TREE_CODE (value) == CALL_EXPR
5043 && TREE_CODE (type) != TREE_CODE (otype))
5044 warning_at (loc, OPT_Wbad_function_cast,
5045 "cast from function call of type %qT "
5046 "to non-matching type %qT", otype, type);
5048 if (TREE_CODE (type) == POINTER_TYPE
5049 && TREE_CODE (otype) == INTEGER_TYPE
5050 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5051 /* Don't warn about converting any constant. */
5052 && !TREE_CONSTANT (value))
5053 warning_at (loc,
5054 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5055 "of different size");
5057 if (warn_strict_aliasing <= 2)
5058 strict_aliasing_warning (otype, type, expr);
5060 /* If pedantic, warn for conversions between function and object
5061 pointer types, except for converting a null pointer constant
5062 to function pointer type. */
5063 if (pedantic
5064 && TREE_CODE (type) == POINTER_TYPE
5065 && TREE_CODE (otype) == POINTER_TYPE
5066 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5067 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5068 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5069 "conversion of function pointer to object pointer type");
5071 if (pedantic
5072 && TREE_CODE (type) == POINTER_TYPE
5073 && TREE_CODE (otype) == POINTER_TYPE
5074 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5075 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5076 && !null_pointer_constant_p (value))
5077 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5078 "conversion of object pointer to function pointer type");
5080 ovalue = value;
5081 value = convert (type, value);
5083 /* Ignore any integer overflow caused by the cast. */
5084 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5086 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5088 if (!TREE_OVERFLOW (value))
5090 /* Avoid clobbering a shared constant. */
5091 value = copy_node (value);
5092 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5095 else if (TREE_OVERFLOW (value))
5096 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5097 value = build_int_cst_wide (TREE_TYPE (value),
5098 TREE_INT_CST_LOW (value),
5099 TREE_INT_CST_HIGH (value));
5103 /* Don't let a cast be an lvalue. */
5104 if (value == expr)
5105 value = non_lvalue_loc (loc, value);
5107 /* Don't allow the results of casting to floating-point or complex
5108 types be confused with actual constants, or casts involving
5109 integer and pointer types other than direct integer-to-integer
5110 and integer-to-pointer be confused with integer constant
5111 expressions and null pointer constants. */
5112 if (TREE_CODE (value) == REAL_CST
5113 || TREE_CODE (value) == COMPLEX_CST
5114 || (TREE_CODE (value) == INTEGER_CST
5115 && !((TREE_CODE (expr) == INTEGER_CST
5116 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5117 || TREE_CODE (expr) == REAL_CST
5118 || TREE_CODE (expr) == COMPLEX_CST)))
5119 value = build1 (NOP_EXPR, type, value);
5121 if (CAN_HAVE_LOCATION_P (value))
5122 SET_EXPR_LOCATION (value, loc);
5123 return value;
5126 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5127 location of the open paren of the cast, or the position of the cast
5128 expr. */
5129 tree
5130 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5132 tree type;
5133 tree type_expr = NULL_TREE;
5134 bool type_expr_const = true;
5135 tree ret;
5136 int saved_wsp = warn_strict_prototypes;
5138 /* This avoids warnings about unprototyped casts on
5139 integers. E.g. "#define SIG_DFL (void(*)())0". */
5140 if (TREE_CODE (expr) == INTEGER_CST)
5141 warn_strict_prototypes = 0;
5142 type = groktypename (type_name, &type_expr, &type_expr_const);
5143 warn_strict_prototypes = saved_wsp;
5145 ret = build_c_cast (loc, type, expr);
5146 if (type_expr)
5148 bool inner_expr_const = true;
5149 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5150 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5151 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5152 && inner_expr_const);
5153 SET_EXPR_LOCATION (ret, loc);
5156 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5157 SET_EXPR_LOCATION (ret, loc);
5159 /* C++ does not permits types to be defined in a cast, but it
5160 allows references to incomplete types. */
5161 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5162 warning_at (loc, OPT_Wc___compat,
5163 "defining a type in a cast is invalid in C++");
5165 return ret;
5168 /* Build an assignment expression of lvalue LHS from value RHS.
5169 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5170 may differ from TREE_TYPE (LHS) for an enum bitfield.
5171 MODIFYCODE is the code for a binary operator that we use
5172 to combine the old value of LHS with RHS to get the new value.
5173 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5174 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5175 which may differ from TREE_TYPE (RHS) for an enum value.
5177 LOCATION is the location of the MODIFYCODE operator.
5178 RHS_LOC is the location of the RHS. */
5180 tree
5181 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5182 enum tree_code modifycode,
5183 location_t rhs_loc, tree rhs, tree rhs_origtype)
5185 tree result;
5186 tree newrhs;
5187 tree rhs_semantic_type = NULL_TREE;
5188 tree lhstype = TREE_TYPE (lhs);
5189 tree olhstype = lhstype;
5190 bool npc;
5191 bool is_atomic_op;
5193 /* Types that aren't fully specified cannot be used in assignments. */
5194 lhs = require_complete_type (lhs);
5196 /* Avoid duplicate error messages from operands that had errors. */
5197 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5198 return error_mark_node;
5200 /* For ObjC properties, defer this check. */
5201 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5202 return error_mark_node;
5204 is_atomic_op = really_atomic_lvalue (lhs);
5206 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5208 rhs_semantic_type = TREE_TYPE (rhs);
5209 rhs = TREE_OPERAND (rhs, 0);
5212 newrhs = rhs;
5214 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5216 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5217 lhs_origtype, modifycode, rhs_loc, rhs,
5218 rhs_origtype);
5219 if (inner == error_mark_node)
5220 return error_mark_node;
5221 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5222 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5223 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5224 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5225 protected_set_expr_location (result, location);
5226 return result;
5229 /* If a binary op has been requested, combine the old LHS value with the RHS
5230 producing the value we should actually store into the LHS. */
5232 if (modifycode != NOP_EXPR)
5234 lhs = c_fully_fold (lhs, false, NULL);
5235 lhs = stabilize_reference (lhs);
5237 /* Construct the RHS for any non-atomic compound assignemnt. */
5238 if (!is_atomic_op)
5240 newrhs = build_binary_op (location,
5241 modifycode, lhs, rhs, 1);
5243 /* The original type of the right hand side is no longer
5244 meaningful. */
5245 rhs_origtype = NULL_TREE;
5249 if (c_dialect_objc ())
5251 /* Check if we are modifying an Objective-C property reference;
5252 if so, we need to generate setter calls. */
5253 result = objc_maybe_build_modify_expr (lhs, newrhs);
5254 if (result)
5255 return result;
5257 /* Else, do the check that we postponed for Objective-C. */
5258 if (!lvalue_or_else (location, lhs, lv_assign))
5259 return error_mark_node;
5262 /* Give an error for storing in something that is 'const'. */
5264 if (TYPE_READONLY (lhstype)
5265 || ((TREE_CODE (lhstype) == RECORD_TYPE
5266 || TREE_CODE (lhstype) == UNION_TYPE)
5267 && C_TYPE_FIELDS_READONLY (lhstype)))
5269 readonly_error (location, lhs, lv_assign);
5270 return error_mark_node;
5272 else if (TREE_READONLY (lhs))
5273 readonly_warning (lhs, lv_assign);
5275 /* If storing into a structure or union member,
5276 it has probably been given type `int'.
5277 Compute the type that would go with
5278 the actual amount of storage the member occupies. */
5280 if (TREE_CODE (lhs) == COMPONENT_REF
5281 && (TREE_CODE (lhstype) == INTEGER_TYPE
5282 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5283 || TREE_CODE (lhstype) == REAL_TYPE
5284 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5285 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5287 /* If storing in a field that is in actuality a short or narrower than one,
5288 we must store in the field in its actual type. */
5290 if (lhstype != TREE_TYPE (lhs))
5292 lhs = copy_node (lhs);
5293 TREE_TYPE (lhs) = lhstype;
5296 /* Issue -Wc++-compat warnings about an assignment to an enum type
5297 when LHS does not have its original type. This happens for,
5298 e.g., an enum bitfield in a struct. */
5299 if (warn_cxx_compat
5300 && lhs_origtype != NULL_TREE
5301 && lhs_origtype != lhstype
5302 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5304 tree checktype = (rhs_origtype != NULL_TREE
5305 ? rhs_origtype
5306 : TREE_TYPE (rhs));
5307 if (checktype != error_mark_node
5308 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5309 || (is_atomic_op && modifycode != NOP_EXPR)))
5310 warning_at (location, OPT_Wc___compat,
5311 "enum conversion in assignment is invalid in C++");
5314 /* If the lhs is atomic, remove that qualifier. */
5315 if (is_atomic_op)
5317 lhstype = build_qualified_type (lhstype,
5318 (TYPE_QUALS (lhstype)
5319 & ~TYPE_QUAL_ATOMIC));
5320 olhstype = build_qualified_type (olhstype,
5321 (TYPE_QUALS (lhstype)
5322 & ~TYPE_QUAL_ATOMIC));
5325 /* Convert new value to destination type. Fold it first, then
5326 restore any excess precision information, for the sake of
5327 conversion warnings. */
5329 if (!(is_atomic_op && modifycode != NOP_EXPR))
5331 npc = null_pointer_constant_p (newrhs);
5332 newrhs = c_fully_fold (newrhs, false, NULL);
5333 if (rhs_semantic_type)
5334 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5335 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
5336 ic_assign, npc, NULL_TREE,
5337 NULL_TREE, 0);
5338 if (TREE_CODE (newrhs) == ERROR_MARK)
5339 return error_mark_node;
5342 /* Emit ObjC write barrier, if necessary. */
5343 if (c_dialect_objc () && flag_objc_gc)
5345 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5346 if (result)
5348 protected_set_expr_location (result, location);
5349 return result;
5353 /* Scan operands. */
5355 if (is_atomic_op)
5356 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5357 else
5359 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5360 TREE_SIDE_EFFECTS (result) = 1;
5361 protected_set_expr_location (result, location);
5364 /* If we got the LHS in a different type for storing in,
5365 convert the result back to the nominal type of LHS
5366 so that the value we return always has the same type
5367 as the LHS argument. */
5369 if (olhstype == TREE_TYPE (result))
5370 return result;
5372 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5373 ic_assign, false, NULL_TREE, NULL_TREE, 0);
5374 protected_set_expr_location (result, location);
5375 return result;
5378 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5379 This is used to implement -fplan9-extensions. */
5381 static bool
5382 find_anonymous_field_with_type (tree struct_type, tree type)
5384 tree field;
5385 bool found;
5387 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5388 || TREE_CODE (struct_type) == UNION_TYPE);
5389 found = false;
5390 for (field = TYPE_FIELDS (struct_type);
5391 field != NULL_TREE;
5392 field = TREE_CHAIN (field))
5394 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5395 ? c_build_qualified_type (TREE_TYPE (field),
5396 TYPE_QUAL_ATOMIC)
5397 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5398 if (DECL_NAME (field) == NULL
5399 && comptypes (type, fieldtype))
5401 if (found)
5402 return false;
5403 found = true;
5405 else if (DECL_NAME (field) == NULL
5406 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5407 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5408 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5410 if (found)
5411 return false;
5412 found = true;
5415 return found;
5418 /* RHS is an expression whose type is pointer to struct. If there is
5419 an anonymous field in RHS with type TYPE, then return a pointer to
5420 that field in RHS. This is used with -fplan9-extensions. This
5421 returns NULL if no conversion could be found. */
5423 static tree
5424 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5426 tree rhs_struct_type, lhs_main_type;
5427 tree field, found_field;
5428 bool found_sub_field;
5429 tree ret;
5431 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5432 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5433 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5434 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5436 gcc_assert (POINTER_TYPE_P (type));
5437 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5438 ? c_build_qualified_type (TREE_TYPE (type),
5439 TYPE_QUAL_ATOMIC)
5440 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5442 found_field = NULL_TREE;
5443 found_sub_field = false;
5444 for (field = TYPE_FIELDS (rhs_struct_type);
5445 field != NULL_TREE;
5446 field = TREE_CHAIN (field))
5448 if (DECL_NAME (field) != NULL_TREE
5449 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5450 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5451 continue;
5452 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5453 ? c_build_qualified_type (TREE_TYPE (field),
5454 TYPE_QUAL_ATOMIC)
5455 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5456 if (comptypes (lhs_main_type, fieldtype))
5458 if (found_field != NULL_TREE)
5459 return NULL_TREE;
5460 found_field = field;
5462 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5463 lhs_main_type))
5465 if (found_field != NULL_TREE)
5466 return NULL_TREE;
5467 found_field = field;
5468 found_sub_field = true;
5472 if (found_field == NULL_TREE)
5473 return NULL_TREE;
5475 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5476 build_fold_indirect_ref (rhs), found_field,
5477 NULL_TREE);
5478 ret = build_fold_addr_expr_loc (location, ret);
5480 if (found_sub_field)
5482 ret = convert_to_anonymous_field (location, type, ret);
5483 gcc_assert (ret != NULL_TREE);
5486 return ret;
5489 /* Convert value RHS to type TYPE as preparation for an assignment to
5490 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5491 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5492 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5493 constant before any folding.
5494 The real work of conversion is done by `convert'.
5495 The purpose of this function is to generate error messages
5496 for assignments that are not allowed in C.
5497 ERRTYPE says whether it is argument passing, assignment,
5498 initialization or return.
5500 LOCATION is the location of the RHS.
5501 FUNCTION is a tree for the function being called.
5502 PARMNUM is the number of the argument, for printing in error messages. */
5504 static tree
5505 convert_for_assignment (location_t location, tree type, tree rhs,
5506 tree origtype, enum impl_conv errtype,
5507 bool null_pointer_constant, tree fundecl,
5508 tree function, int parmnum)
5510 enum tree_code codel = TREE_CODE (type);
5511 tree orig_rhs = rhs;
5512 tree rhstype;
5513 enum tree_code coder;
5514 tree rname = NULL_TREE;
5515 bool objc_ok = false;
5517 if (errtype == ic_argpass)
5519 tree selector;
5520 /* Change pointer to function to the function itself for
5521 diagnostics. */
5522 if (TREE_CODE (function) == ADDR_EXPR
5523 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5524 function = TREE_OPERAND (function, 0);
5526 /* Handle an ObjC selector specially for diagnostics. */
5527 selector = objc_message_selector ();
5528 rname = function;
5529 if (selector && parmnum > 2)
5531 rname = selector;
5532 parmnum -= 2;
5536 /* This macro is used to emit diagnostics to ensure that all format
5537 strings are complete sentences, visible to gettext and checked at
5538 compile time. */
5539 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5540 do { \
5541 switch (errtype) \
5543 case ic_argpass: \
5544 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5545 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5546 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5547 "expected %qT but argument is of type %qT", \
5548 type, rhstype); \
5549 break; \
5550 case ic_assign: \
5551 pedwarn (LOCATION, OPT, AS); \
5552 break; \
5553 case ic_init: \
5554 pedwarn_init (LOCATION, OPT, IN); \
5555 break; \
5556 case ic_return: \
5557 pedwarn (LOCATION, OPT, RE); \
5558 break; \
5559 default: \
5560 gcc_unreachable (); \
5562 } while (0)
5564 /* This macro is used to emit diagnostics to ensure that all format
5565 strings are complete sentences, visible to gettext and checked at
5566 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5567 extra parameter to enumerate qualifiers. */
5569 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5570 do { \
5571 switch (errtype) \
5573 case ic_argpass: \
5574 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5575 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5576 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5577 "expected %qT but argument is of type %qT", \
5578 type, rhstype); \
5579 break; \
5580 case ic_assign: \
5581 pedwarn (LOCATION, OPT, AS, QUALS); \
5582 break; \
5583 case ic_init: \
5584 pedwarn (LOCATION, OPT, IN, QUALS); \
5585 break; \
5586 case ic_return: \
5587 pedwarn (LOCATION, OPT, RE, QUALS); \
5588 break; \
5589 default: \
5590 gcc_unreachable (); \
5592 } while (0)
5594 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5595 rhs = TREE_OPERAND (rhs, 0);
5597 rhstype = TREE_TYPE (rhs);
5598 coder = TREE_CODE (rhstype);
5600 if (coder == ERROR_MARK)
5601 return error_mark_node;
5603 if (c_dialect_objc ())
5605 int parmno;
5607 switch (errtype)
5609 case ic_return:
5610 parmno = 0;
5611 break;
5613 case ic_assign:
5614 parmno = -1;
5615 break;
5617 case ic_init:
5618 parmno = -2;
5619 break;
5621 default:
5622 parmno = parmnum;
5623 break;
5626 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5629 if (warn_cxx_compat)
5631 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5632 if (checktype != error_mark_node
5633 && TREE_CODE (type) == ENUMERAL_TYPE
5634 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5636 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5637 G_("enum conversion when passing argument "
5638 "%d of %qE is invalid in C++"),
5639 G_("enum conversion in assignment is "
5640 "invalid in C++"),
5641 G_("enum conversion in initialization is "
5642 "invalid in C++"),
5643 G_("enum conversion in return is "
5644 "invalid in C++"));
5648 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5649 return rhs;
5651 if (coder == VOID_TYPE)
5653 /* Except for passing an argument to an unprototyped function,
5654 this is a constraint violation. When passing an argument to
5655 an unprototyped function, it is compile-time undefined;
5656 making it a constraint in that case was rejected in
5657 DR#252. */
5658 error_at (location, "void value not ignored as it ought to be");
5659 return error_mark_node;
5661 rhs = require_complete_type (rhs);
5662 if (rhs == error_mark_node)
5663 return error_mark_node;
5664 /* A non-reference type can convert to a reference. This handles
5665 va_start, va_copy and possibly port built-ins. */
5666 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5668 if (!lvalue_p (rhs))
5670 error_at (location, "cannot pass rvalue to reference parameter");
5671 return error_mark_node;
5673 if (!c_mark_addressable (rhs))
5674 return error_mark_node;
5675 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5676 SET_EXPR_LOCATION (rhs, location);
5678 rhs = convert_for_assignment (location, build_pointer_type (TREE_TYPE (type)),
5679 rhs, origtype, errtype, null_pointer_constant,
5680 fundecl, function, parmnum);
5681 if (rhs == error_mark_node)
5682 return error_mark_node;
5684 rhs = build1 (NOP_EXPR, type, rhs);
5685 SET_EXPR_LOCATION (rhs, location);
5686 return rhs;
5688 /* Some types can interconvert without explicit casts. */
5689 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5690 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5691 return convert (type, rhs);
5692 /* Arithmetic types all interconvert, and enum is treated like int. */
5693 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5694 || codel == FIXED_POINT_TYPE
5695 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5696 || codel == BOOLEAN_TYPE)
5697 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5698 || coder == FIXED_POINT_TYPE
5699 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5700 || coder == BOOLEAN_TYPE))
5702 tree ret;
5703 bool save = in_late_binary_op;
5704 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5705 in_late_binary_op = true;
5706 ret = convert_and_check (type, orig_rhs);
5707 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5708 in_late_binary_op = save;
5709 return ret;
5712 /* Aggregates in different TUs might need conversion. */
5713 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5714 && codel == coder
5715 && comptypes (type, rhstype))
5716 return convert_and_check (type, rhs);
5718 /* Conversion to a transparent union or record from its member types.
5719 This applies only to function arguments. */
5720 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5721 && TYPE_TRANSPARENT_AGGR (type))
5722 && errtype == ic_argpass)
5724 tree memb, marginal_memb = NULL_TREE;
5726 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5728 tree memb_type = TREE_TYPE (memb);
5730 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5731 TYPE_MAIN_VARIANT (rhstype)))
5732 break;
5734 if (TREE_CODE (memb_type) != POINTER_TYPE)
5735 continue;
5737 if (coder == POINTER_TYPE)
5739 tree ttl = TREE_TYPE (memb_type);
5740 tree ttr = TREE_TYPE (rhstype);
5742 /* Any non-function converts to a [const][volatile] void *
5743 and vice versa; otherwise, targets must be the same.
5744 Meanwhile, the lhs target must have all the qualifiers of
5745 the rhs. */
5746 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5747 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5748 || comp_target_types (location, memb_type, rhstype))
5750 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5751 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5752 /* If this type won't generate any warnings, use it. */
5753 if (lquals == rquals
5754 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5755 && TREE_CODE (ttl) == FUNCTION_TYPE)
5756 ? ((lquals | rquals) == rquals)
5757 : ((lquals | rquals) == lquals)))
5758 break;
5760 /* Keep looking for a better type, but remember this one. */
5761 if (!marginal_memb)
5762 marginal_memb = memb;
5766 /* Can convert integer zero to any pointer type. */
5767 if (null_pointer_constant)
5769 rhs = null_pointer_node;
5770 break;
5774 if (memb || marginal_memb)
5776 if (!memb)
5778 /* We have only a marginally acceptable member type;
5779 it needs a warning. */
5780 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5781 tree ttr = TREE_TYPE (rhstype);
5783 /* Const and volatile mean something different for function
5784 types, so the usual warnings are not appropriate. */
5785 if (TREE_CODE (ttr) == FUNCTION_TYPE
5786 && TREE_CODE (ttl) == FUNCTION_TYPE)
5788 /* Because const and volatile on functions are
5789 restrictions that say the function will not do
5790 certain things, it is okay to use a const or volatile
5791 function where an ordinary one is wanted, but not
5792 vice-versa. */
5793 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5794 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5795 WARN_FOR_QUALIFIERS (location, 0,
5796 G_("passing argument %d of %qE "
5797 "makes %q#v qualified function "
5798 "pointer from unqualified"),
5799 G_("assignment makes %q#v qualified "
5800 "function pointer from "
5801 "unqualified"),
5802 G_("initialization makes %q#v qualified "
5803 "function pointer from "
5804 "unqualified"),
5805 G_("return makes %q#v qualified function "
5806 "pointer from unqualified"),
5807 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5809 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5810 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5811 WARN_FOR_QUALIFIERS (location, 0,
5812 G_("passing argument %d of %qE discards "
5813 "%qv qualifier from pointer target type"),
5814 G_("assignment discards %qv qualifier "
5815 "from pointer target type"),
5816 G_("initialization discards %qv qualifier "
5817 "from pointer target type"),
5818 G_("return discards %qv qualifier from "
5819 "pointer target type"),
5820 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5822 memb = marginal_memb;
5825 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5826 pedwarn (location, OPT_Wpedantic,
5827 "ISO C prohibits argument conversion to union type");
5829 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5830 return build_constructor_single (type, memb, rhs);
5834 /* Conversions among pointers */
5835 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5836 && (coder == codel))
5838 tree ttl = TREE_TYPE (type);
5839 tree ttr = TREE_TYPE (rhstype);
5840 tree mvl = ttl;
5841 tree mvr = ttr;
5842 bool is_opaque_pointer;
5843 int target_cmp = 0; /* Cache comp_target_types () result. */
5844 addr_space_t asl;
5845 addr_space_t asr;
5847 if (TREE_CODE (mvl) != ARRAY_TYPE)
5848 mvl = (TYPE_ATOMIC (mvl)
5849 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
5850 TYPE_QUAL_ATOMIC)
5851 : TYPE_MAIN_VARIANT (mvl));
5852 if (TREE_CODE (mvr) != ARRAY_TYPE)
5853 mvr = (TYPE_ATOMIC (mvr)
5854 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
5855 TYPE_QUAL_ATOMIC)
5856 : TYPE_MAIN_VARIANT (mvr));
5857 /* Opaque pointers are treated like void pointers. */
5858 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5860 /* The Plan 9 compiler permits a pointer to a struct to be
5861 automatically converted into a pointer to an anonymous field
5862 within the struct. */
5863 if (flag_plan9_extensions
5864 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5865 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5866 && mvl != mvr)
5868 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5869 if (new_rhs != NULL_TREE)
5871 rhs = new_rhs;
5872 rhstype = TREE_TYPE (rhs);
5873 coder = TREE_CODE (rhstype);
5874 ttr = TREE_TYPE (rhstype);
5875 mvr = TYPE_MAIN_VARIANT (ttr);
5879 /* C++ does not allow the implicit conversion void* -> T*. However,
5880 for the purpose of reducing the number of false positives, we
5881 tolerate the special case of
5883 int *p = NULL;
5885 where NULL is typically defined in C to be '(void *) 0'. */
5886 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5887 warning_at (location, OPT_Wc___compat,
5888 "request for implicit conversion "
5889 "from %qT to %qT not permitted in C++", rhstype, type);
5891 /* See if the pointers point to incompatible address spaces. */
5892 asl = TYPE_ADDR_SPACE (ttl);
5893 asr = TYPE_ADDR_SPACE (ttr);
5894 if (!null_pointer_constant_p (rhs)
5895 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5897 switch (errtype)
5899 case ic_argpass:
5900 error_at (location, "passing argument %d of %qE from pointer to "
5901 "non-enclosed address space", parmnum, rname);
5902 break;
5903 case ic_assign:
5904 error_at (location, "assignment from pointer to "
5905 "non-enclosed address space");
5906 break;
5907 case ic_init:
5908 error_at (location, "initialization from pointer to "
5909 "non-enclosed address space");
5910 break;
5911 case ic_return:
5912 error_at (location, "return from pointer to "
5913 "non-enclosed address space");
5914 break;
5915 default:
5916 gcc_unreachable ();
5918 return error_mark_node;
5921 /* Check if the right-hand side has a format attribute but the
5922 left-hand side doesn't. */
5923 if (warn_suggest_attribute_format
5924 && check_missing_format_attribute (type, rhstype))
5926 switch (errtype)
5928 case ic_argpass:
5929 warning_at (location, OPT_Wsuggest_attribute_format,
5930 "argument %d of %qE might be "
5931 "a candidate for a format attribute",
5932 parmnum, rname);
5933 break;
5934 case ic_assign:
5935 warning_at (location, OPT_Wsuggest_attribute_format,
5936 "assignment left-hand side might be "
5937 "a candidate for a format attribute");
5938 break;
5939 case ic_init:
5940 warning_at (location, OPT_Wsuggest_attribute_format,
5941 "initialization left-hand side might be "
5942 "a candidate for a format attribute");
5943 break;
5944 case ic_return:
5945 warning_at (location, OPT_Wsuggest_attribute_format,
5946 "return type might be "
5947 "a candidate for a format attribute");
5948 break;
5949 default:
5950 gcc_unreachable ();
5954 /* Any non-function converts to a [const][volatile] void *
5955 and vice versa; otherwise, targets must be the same.
5956 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5957 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5958 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5959 || (target_cmp = comp_target_types (location, type, rhstype))
5960 || is_opaque_pointer
5961 || ((c_common_unsigned_type (mvl)
5962 == c_common_unsigned_type (mvr))
5963 && (c_common_signed_type (mvl)
5964 == c_common_signed_type (mvr))
5965 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
5967 if (pedantic
5968 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5970 (VOID_TYPE_P (ttr)
5971 && !null_pointer_constant
5972 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5973 WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic,
5974 G_("ISO C forbids passing argument %d of "
5975 "%qE between function pointer "
5976 "and %<void *%>"),
5977 G_("ISO C forbids assignment between "
5978 "function pointer and %<void *%>"),
5979 G_("ISO C forbids initialization between "
5980 "function pointer and %<void *%>"),
5981 G_("ISO C forbids return between function "
5982 "pointer and %<void *%>"));
5983 /* Const and volatile mean something different for function types,
5984 so the usual warnings are not appropriate. */
5985 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5986 && TREE_CODE (ttl) != FUNCTION_TYPE)
5988 /* Assignments between atomic and non-atomic objects are OK. */
5989 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
5990 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
5992 WARN_FOR_QUALIFIERS (location, 0,
5993 G_("passing argument %d of %qE discards "
5994 "%qv qualifier from pointer target type"),
5995 G_("assignment discards %qv qualifier "
5996 "from pointer target type"),
5997 G_("initialization discards %qv qualifier "
5998 "from pointer target type"),
5999 G_("return discards %qv qualifier from "
6000 "pointer target type"),
6001 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6003 /* If this is not a case of ignoring a mismatch in signedness,
6004 no warning. */
6005 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6006 || target_cmp)
6008 /* If there is a mismatch, do warn. */
6009 else if (warn_pointer_sign)
6010 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
6011 G_("pointer targets in passing argument "
6012 "%d of %qE differ in signedness"),
6013 G_("pointer targets in assignment "
6014 "differ in signedness"),
6015 G_("pointer targets in initialization "
6016 "differ in signedness"),
6017 G_("pointer targets in return differ "
6018 "in signedness"));
6020 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6021 && TREE_CODE (ttr) == FUNCTION_TYPE)
6023 /* Because const and volatile on functions are restrictions
6024 that say the function will not do certain things,
6025 it is okay to use a const or volatile function
6026 where an ordinary one is wanted, but not vice-versa. */
6027 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6028 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6029 WARN_FOR_QUALIFIERS (location, 0,
6030 G_("passing argument %d of %qE makes "
6031 "%q#v qualified function pointer "
6032 "from unqualified"),
6033 G_("assignment makes %q#v qualified function "
6034 "pointer from unqualified"),
6035 G_("initialization makes %q#v qualified "
6036 "function pointer from unqualified"),
6037 G_("return makes %q#v qualified function "
6038 "pointer from unqualified"),
6039 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6042 else
6043 /* Avoid warning about the volatile ObjC EH puts on decls. */
6044 if (!objc_ok)
6045 WARN_FOR_ASSIGNMENT (location, 0,
6046 G_("passing argument %d of %qE from "
6047 "incompatible pointer type"),
6048 G_("assignment from incompatible pointer type"),
6049 G_("initialization from incompatible "
6050 "pointer type"),
6051 G_("return from incompatible pointer type"));
6053 return convert (type, rhs);
6055 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6057 /* ??? This should not be an error when inlining calls to
6058 unprototyped functions. */
6059 error_at (location, "invalid use of non-lvalue array");
6060 return error_mark_node;
6062 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6064 /* An explicit constant 0 can convert to a pointer,
6065 or one that results from arithmetic, even including
6066 a cast to integer type. */
6067 if (!null_pointer_constant)
6068 WARN_FOR_ASSIGNMENT (location, 0,
6069 G_("passing argument %d of %qE makes "
6070 "pointer from integer without a cast"),
6071 G_("assignment makes pointer from integer "
6072 "without a cast"),
6073 G_("initialization makes pointer from "
6074 "integer without a cast"),
6075 G_("return makes pointer from integer "
6076 "without a cast"));
6078 return convert (type, rhs);
6080 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6082 WARN_FOR_ASSIGNMENT (location, 0,
6083 G_("passing argument %d of %qE makes integer "
6084 "from pointer without a cast"),
6085 G_("assignment makes integer from pointer "
6086 "without a cast"),
6087 G_("initialization makes integer from pointer "
6088 "without a cast"),
6089 G_("return makes integer from pointer "
6090 "without a cast"));
6091 return convert (type, rhs);
6093 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6095 tree ret;
6096 bool save = in_late_binary_op;
6097 in_late_binary_op = true;
6098 ret = convert (type, rhs);
6099 in_late_binary_op = save;
6100 return ret;
6103 switch (errtype)
6105 case ic_argpass:
6106 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
6107 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6108 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
6109 "expected %qT but argument is of type %qT", type, rhstype);
6110 break;
6111 case ic_assign:
6112 error_at (location, "incompatible types when assigning to type %qT from "
6113 "type %qT", type, rhstype);
6114 break;
6115 case ic_init:
6116 error_at (location,
6117 "incompatible types when initializing type %qT using type %qT",
6118 type, rhstype);
6119 break;
6120 case ic_return:
6121 error_at (location,
6122 "incompatible types when returning type %qT but %qT was "
6123 "expected", rhstype, type);
6124 break;
6125 default:
6126 gcc_unreachable ();
6129 return error_mark_node;
6132 /* If VALUE is a compound expr all of whose expressions are constant, then
6133 return its value. Otherwise, return error_mark_node.
6135 This is for handling COMPOUND_EXPRs as initializer elements
6136 which is allowed with a warning when -pedantic is specified. */
6138 static tree
6139 valid_compound_expr_initializer (tree value, tree endtype)
6141 if (TREE_CODE (value) == COMPOUND_EXPR)
6143 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6144 == error_mark_node)
6145 return error_mark_node;
6146 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6147 endtype);
6149 else if (!initializer_constant_valid_p (value, endtype))
6150 return error_mark_node;
6151 else
6152 return value;
6155 /* Perform appropriate conversions on the initial value of a variable,
6156 store it in the declaration DECL,
6157 and print any error messages that are appropriate.
6158 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6159 If the init is invalid, store an ERROR_MARK.
6161 INIT_LOC is the location of the initial value. */
6163 void
6164 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6166 tree value, type;
6167 bool npc = false;
6169 /* If variable's type was invalidly declared, just ignore it. */
6171 type = TREE_TYPE (decl);
6172 if (TREE_CODE (type) == ERROR_MARK)
6173 return;
6175 /* Digest the specified initializer into an expression. */
6177 if (init)
6178 npc = null_pointer_constant_p (init);
6179 value = digest_init (init_loc, type, init, origtype, npc,
6180 true, TREE_STATIC (decl));
6182 /* Store the expression if valid; else report error. */
6184 if (!in_system_header
6185 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6186 warning (OPT_Wtraditional, "traditional C rejects automatic "
6187 "aggregate initialization");
6189 DECL_INITIAL (decl) = value;
6191 /* ANSI wants warnings about out-of-range constant initializers. */
6192 STRIP_TYPE_NOPS (value);
6193 if (TREE_STATIC (decl))
6194 constant_expression_warning (value);
6196 /* Check if we need to set array size from compound literal size. */
6197 if (TREE_CODE (type) == ARRAY_TYPE
6198 && TYPE_DOMAIN (type) == 0
6199 && value != error_mark_node)
6201 tree inside_init = init;
6203 STRIP_TYPE_NOPS (inside_init);
6204 inside_init = fold (inside_init);
6206 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6208 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6210 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6212 /* For int foo[] = (int [3]){1}; we need to set array size
6213 now since later on array initializer will be just the
6214 brace enclosed list of the compound literal. */
6215 tree etype = strip_array_types (TREE_TYPE (decl));
6216 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6217 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6218 layout_type (type);
6219 layout_decl (cldecl, 0);
6220 TREE_TYPE (decl)
6221 = c_build_qualified_type (type, TYPE_QUALS (etype));
6227 /* Methods for storing and printing names for error messages. */
6229 /* Implement a spelling stack that allows components of a name to be pushed
6230 and popped. Each element on the stack is this structure. */
6232 struct spelling
6234 int kind;
6235 union
6237 unsigned HOST_WIDE_INT i;
6238 const char *s;
6239 } u;
6242 #define SPELLING_STRING 1
6243 #define SPELLING_MEMBER 2
6244 #define SPELLING_BOUNDS 3
6246 static struct spelling *spelling; /* Next stack element (unused). */
6247 static struct spelling *spelling_base; /* Spelling stack base. */
6248 static int spelling_size; /* Size of the spelling stack. */
6250 /* Macros to save and restore the spelling stack around push_... functions.
6251 Alternative to SAVE_SPELLING_STACK. */
6253 #define SPELLING_DEPTH() (spelling - spelling_base)
6254 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6256 /* Push an element on the spelling stack with type KIND and assign VALUE
6257 to MEMBER. */
6259 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6261 int depth = SPELLING_DEPTH (); \
6263 if (depth >= spelling_size) \
6265 spelling_size += 10; \
6266 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6267 spelling_size); \
6268 RESTORE_SPELLING_DEPTH (depth); \
6271 spelling->kind = (KIND); \
6272 spelling->MEMBER = (VALUE); \
6273 spelling++; \
6276 /* Push STRING on the stack. Printed literally. */
6278 static void
6279 push_string (const char *string)
6281 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6284 /* Push a member name on the stack. Printed as '.' STRING. */
6286 static void
6287 push_member_name (tree decl)
6289 const char *const string
6290 = (DECL_NAME (decl)
6291 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6292 : _("<anonymous>"));
6293 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6296 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6298 static void
6299 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6301 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6304 /* Compute the maximum size in bytes of the printed spelling. */
6306 static int
6307 spelling_length (void)
6309 int size = 0;
6310 struct spelling *p;
6312 for (p = spelling_base; p < spelling; p++)
6314 if (p->kind == SPELLING_BOUNDS)
6315 size += 25;
6316 else
6317 size += strlen (p->u.s) + 1;
6320 return size;
6323 /* Print the spelling to BUFFER and return it. */
6325 static char *
6326 print_spelling (char *buffer)
6328 char *d = buffer;
6329 struct spelling *p;
6331 for (p = spelling_base; p < spelling; p++)
6332 if (p->kind == SPELLING_BOUNDS)
6334 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6335 d += strlen (d);
6337 else
6339 const char *s;
6340 if (p->kind == SPELLING_MEMBER)
6341 *d++ = '.';
6342 for (s = p->u.s; (*d = *s++); d++)
6345 *d++ = '\0';
6346 return buffer;
6349 /* Issue an error message for a bad initializer component.
6350 GMSGID identifies the message.
6351 The component name is taken from the spelling stack. */
6353 void
6354 error_init (const char *gmsgid)
6356 char *ofwhat;
6358 /* The gmsgid may be a format string with %< and %>. */
6359 error (gmsgid);
6360 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6361 if (*ofwhat)
6362 error ("(near initialization for %qs)", ofwhat);
6365 /* Issue a pedantic warning for a bad initializer component. OPT is
6366 the option OPT_* (from options.h) controlling this warning or 0 if
6367 it is unconditionally given. GMSGID identifies the message. The
6368 component name is taken from the spelling stack. */
6370 void
6371 pedwarn_init (location_t location, int opt, const char *gmsgid)
6373 char *ofwhat;
6375 /* The gmsgid may be a format string with %< and %>. */
6376 pedwarn (location, opt, gmsgid);
6377 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6378 if (*ofwhat)
6379 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
6382 /* Issue a warning for a bad initializer component.
6384 OPT is the OPT_W* value corresponding to the warning option that
6385 controls this warning. GMSGID identifies the message. The
6386 component name is taken from the spelling stack. */
6388 static void
6389 warning_init (int opt, const char *gmsgid)
6391 char *ofwhat;
6393 /* The gmsgid may be a format string with %< and %>. */
6394 warning (opt, gmsgid);
6395 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6396 if (*ofwhat)
6397 warning (opt, "(near initialization for %qs)", ofwhat);
6400 /* If TYPE is an array type and EXPR is a parenthesized string
6401 constant, warn if pedantic that EXPR is being used to initialize an
6402 object of type TYPE. */
6404 void
6405 maybe_warn_string_init (tree type, struct c_expr expr)
6407 if (pedantic
6408 && TREE_CODE (type) == ARRAY_TYPE
6409 && TREE_CODE (expr.value) == STRING_CST
6410 && expr.original_code != STRING_CST)
6411 pedwarn_init (input_location, OPT_Wpedantic,
6412 "array initialized from parenthesized string constant");
6415 /* Digest the parser output INIT as an initializer for type TYPE.
6416 Return a C expression of type TYPE to represent the initial value.
6418 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6420 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6422 If INIT is a string constant, STRICT_STRING is true if it is
6423 unparenthesized or we should not warn here for it being parenthesized.
6424 For other types of INIT, STRICT_STRING is not used.
6426 INIT_LOC is the location of the INIT.
6428 REQUIRE_CONSTANT requests an error if non-constant initializers or
6429 elements are seen. */
6431 static tree
6432 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6433 bool null_pointer_constant, bool strict_string,
6434 int require_constant)
6436 enum tree_code code = TREE_CODE (type);
6437 tree inside_init = init;
6438 tree semantic_type = NULL_TREE;
6439 bool maybe_const = true;
6441 if (type == error_mark_node
6442 || !init
6443 || init == error_mark_node
6444 || TREE_TYPE (init) == error_mark_node)
6445 return error_mark_node;
6447 STRIP_TYPE_NOPS (inside_init);
6449 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6451 semantic_type = TREE_TYPE (inside_init);
6452 inside_init = TREE_OPERAND (inside_init, 0);
6454 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6455 inside_init = decl_constant_value_for_optimization (inside_init);
6457 /* Initialization of an array of chars from a string constant
6458 optionally enclosed in braces. */
6460 if (code == ARRAY_TYPE && inside_init
6461 && TREE_CODE (inside_init) == STRING_CST)
6463 tree typ1
6464 = (TYPE_ATOMIC (TREE_TYPE (type))
6465 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6466 TYPE_QUAL_ATOMIC)
6467 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6468 /* Note that an array could be both an array of character type
6469 and an array of wchar_t if wchar_t is signed char or unsigned
6470 char. */
6471 bool char_array = (typ1 == char_type_node
6472 || typ1 == signed_char_type_node
6473 || typ1 == unsigned_char_type_node);
6474 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6475 bool char16_array = !!comptypes (typ1, char16_type_node);
6476 bool char32_array = !!comptypes (typ1, char32_type_node);
6478 if (char_array || wchar_array || char16_array || char32_array)
6480 struct c_expr expr;
6481 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6482 expr.value = inside_init;
6483 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6484 expr.original_type = NULL;
6485 maybe_warn_string_init (type, expr);
6487 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6488 pedwarn_init (init_loc, OPT_Wpedantic,
6489 "initialization of a flexible array member");
6491 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6492 TYPE_MAIN_VARIANT (type)))
6493 return inside_init;
6495 if (char_array)
6497 if (typ2 != char_type_node)
6499 error_init ("char-array initialized from wide string");
6500 return error_mark_node;
6503 else
6505 if (typ2 == char_type_node)
6507 error_init ("wide character array initialized from non-wide "
6508 "string");
6509 return error_mark_node;
6511 else if (!comptypes(typ1, typ2))
6513 error_init ("wide character array initialized from "
6514 "incompatible wide string");
6515 return error_mark_node;
6519 TREE_TYPE (inside_init) = type;
6520 if (TYPE_DOMAIN (type) != 0
6521 && TYPE_SIZE (type) != 0
6522 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6524 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6526 /* Subtract the size of a single (possibly wide) character
6527 because it's ok to ignore the terminating null char
6528 that is counted in the length of the constant. */
6529 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6530 (len
6531 - (TYPE_PRECISION (typ1)
6532 / BITS_PER_UNIT))))
6533 pedwarn_init (init_loc, 0,
6534 ("initializer-string for array of chars "
6535 "is too long"));
6536 else if (warn_cxx_compat
6537 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6538 warning_at (init_loc, OPT_Wc___compat,
6539 ("initializer-string for array chars "
6540 "is too long for C++"));
6543 return inside_init;
6545 else if (INTEGRAL_TYPE_P (typ1))
6547 error_init ("array of inappropriate type initialized "
6548 "from string constant");
6549 return error_mark_node;
6553 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6554 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6555 below and handle as a constructor. */
6556 if (code == VECTOR_TYPE
6557 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6558 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6559 && TREE_CONSTANT (inside_init))
6561 if (TREE_CODE (inside_init) == VECTOR_CST
6562 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6563 TYPE_MAIN_VARIANT (type)))
6564 return inside_init;
6566 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6568 unsigned HOST_WIDE_INT ix;
6569 tree value;
6570 bool constant_p = true;
6572 /* Iterate through elements and check if all constructor
6573 elements are *_CSTs. */
6574 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6575 if (!CONSTANT_CLASS_P (value))
6577 constant_p = false;
6578 break;
6581 if (constant_p)
6582 return build_vector_from_ctor (type,
6583 CONSTRUCTOR_ELTS (inside_init));
6587 if (warn_sequence_point)
6588 verify_sequence_points (inside_init);
6590 /* Any type can be initialized
6591 from an expression of the same type, optionally with braces. */
6593 if (inside_init && TREE_TYPE (inside_init) != 0
6594 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6595 TYPE_MAIN_VARIANT (type))
6596 || (code == ARRAY_TYPE
6597 && comptypes (TREE_TYPE (inside_init), type))
6598 || (code == VECTOR_TYPE
6599 && comptypes (TREE_TYPE (inside_init), type))
6600 || (code == POINTER_TYPE
6601 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6602 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6603 TREE_TYPE (type)))))
6605 if (code == POINTER_TYPE)
6607 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6609 if (TREE_CODE (inside_init) == STRING_CST
6610 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6611 inside_init = array_to_pointer_conversion
6612 (init_loc, inside_init);
6613 else
6615 error_init ("invalid use of non-lvalue array");
6616 return error_mark_node;
6621 if (code == VECTOR_TYPE)
6622 /* Although the types are compatible, we may require a
6623 conversion. */
6624 inside_init = convert (type, inside_init);
6626 if (require_constant
6627 && (code == VECTOR_TYPE || !flag_isoc99)
6628 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6630 /* As an extension, allow initializing objects with static storage
6631 duration with compound literals (which are then treated just as
6632 the brace enclosed list they contain). Also allow this for
6633 vectors, as we can only assign them with compound literals. */
6634 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6635 inside_init = DECL_INITIAL (decl);
6638 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6639 && TREE_CODE (inside_init) != CONSTRUCTOR)
6641 error_init ("array initialized from non-constant array expression");
6642 return error_mark_node;
6645 /* Compound expressions can only occur here if -Wpedantic or
6646 -pedantic-errors is specified. In the later case, we always want
6647 an error. In the former case, we simply want a warning. */
6648 if (require_constant && pedantic
6649 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6651 inside_init
6652 = valid_compound_expr_initializer (inside_init,
6653 TREE_TYPE (inside_init));
6654 if (inside_init == error_mark_node)
6655 error_init ("initializer element is not constant");
6656 else
6657 pedwarn_init (init_loc, OPT_Wpedantic,
6658 "initializer element is not constant");
6659 if (flag_pedantic_errors)
6660 inside_init = error_mark_node;
6662 else if (require_constant
6663 && !initializer_constant_valid_p (inside_init,
6664 TREE_TYPE (inside_init)))
6666 error_init ("initializer element is not constant");
6667 inside_init = error_mark_node;
6669 else if (require_constant && !maybe_const)
6670 pedwarn_init (init_loc, 0,
6671 "initializer element is not a constant expression");
6673 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6674 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6675 inside_init = convert_for_assignment (init_loc, type, inside_init,
6676 origtype,
6677 ic_init, null_pointer_constant,
6678 NULL_TREE, NULL_TREE, 0);
6679 return inside_init;
6682 /* Handle scalar types, including conversions. */
6684 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6685 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6686 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6688 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6689 && (TREE_CODE (init) == STRING_CST
6690 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6691 inside_init = init = array_to_pointer_conversion (init_loc, init);
6692 if (semantic_type)
6693 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6694 inside_init);
6695 inside_init
6696 = convert_for_assignment (init_loc, type, inside_init, origtype,
6697 ic_init, null_pointer_constant,
6698 NULL_TREE, NULL_TREE, 0);
6700 /* Check to see if we have already given an error message. */
6701 if (inside_init == error_mark_node)
6703 else if (require_constant && !TREE_CONSTANT (inside_init))
6705 error_init ("initializer element is not constant");
6706 inside_init = error_mark_node;
6708 else if (require_constant
6709 && !initializer_constant_valid_p (inside_init,
6710 TREE_TYPE (inside_init)))
6712 error_init ("initializer element is not computable at load time");
6713 inside_init = error_mark_node;
6715 else if (require_constant && !maybe_const)
6716 pedwarn_init (init_loc, 0,
6717 "initializer element is not a constant expression");
6719 return inside_init;
6722 /* Come here only for records and arrays. */
6724 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6726 error_init ("variable-sized object may not be initialized");
6727 return error_mark_node;
6730 error_init ("invalid initializer");
6731 return error_mark_node;
6734 /* Handle initializers that use braces. */
6736 /* Type of object we are accumulating a constructor for.
6737 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6738 static tree constructor_type;
6740 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6741 left to fill. */
6742 static tree constructor_fields;
6744 /* For an ARRAY_TYPE, this is the specified index
6745 at which to store the next element we get. */
6746 static tree constructor_index;
6748 /* For an ARRAY_TYPE, this is the maximum index. */
6749 static tree constructor_max_index;
6751 /* For a RECORD_TYPE, this is the first field not yet written out. */
6752 static tree constructor_unfilled_fields;
6754 /* For an ARRAY_TYPE, this is the index of the first element
6755 not yet written out. */
6756 static tree constructor_unfilled_index;
6758 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6759 This is so we can generate gaps between fields, when appropriate. */
6760 static tree constructor_bit_index;
6762 /* If we are saving up the elements rather than allocating them,
6763 this is the list of elements so far (in reverse order,
6764 most recent first). */
6765 static vec<constructor_elt, va_gc> *constructor_elements;
6767 /* 1 if constructor should be incrementally stored into a constructor chain,
6768 0 if all the elements should be kept in AVL tree. */
6769 static int constructor_incremental;
6771 /* 1 if so far this constructor's elements are all compile-time constants. */
6772 static int constructor_constant;
6774 /* 1 if so far this constructor's elements are all valid address constants. */
6775 static int constructor_simple;
6777 /* 1 if this constructor has an element that cannot be part of a
6778 constant expression. */
6779 static int constructor_nonconst;
6781 /* 1 if this constructor is erroneous so far. */
6782 static int constructor_erroneous;
6784 /* Structure for managing pending initializer elements, organized as an
6785 AVL tree. */
6787 struct init_node
6789 struct init_node *left, *right;
6790 struct init_node *parent;
6791 int balance;
6792 tree purpose;
6793 tree value;
6794 tree origtype;
6797 /* Tree of pending elements at this constructor level.
6798 These are elements encountered out of order
6799 which belong at places we haven't reached yet in actually
6800 writing the output.
6801 Will never hold tree nodes across GC runs. */
6802 static struct init_node *constructor_pending_elts;
6804 /* The SPELLING_DEPTH of this constructor. */
6805 static int constructor_depth;
6807 /* DECL node for which an initializer is being read.
6808 0 means we are reading a constructor expression
6809 such as (struct foo) {...}. */
6810 static tree constructor_decl;
6812 /* Nonzero if this is an initializer for a top-level decl. */
6813 static int constructor_top_level;
6815 /* Nonzero if there were any member designators in this initializer. */
6816 static int constructor_designated;
6818 /* Nesting depth of designator list. */
6819 static int designator_depth;
6821 /* Nonzero if there were diagnosed errors in this designator list. */
6822 static int designator_erroneous;
6825 /* This stack has a level for each implicit or explicit level of
6826 structuring in the initializer, including the outermost one. It
6827 saves the values of most of the variables above. */
6829 struct constructor_range_stack;
6831 struct constructor_stack
6833 struct constructor_stack *next;
6834 tree type;
6835 tree fields;
6836 tree index;
6837 tree max_index;
6838 tree unfilled_index;
6839 tree unfilled_fields;
6840 tree bit_index;
6841 vec<constructor_elt, va_gc> *elements;
6842 struct init_node *pending_elts;
6843 int offset;
6844 int depth;
6845 /* If value nonzero, this value should replace the entire
6846 constructor at this level. */
6847 struct c_expr replacement_value;
6848 struct constructor_range_stack *range_stack;
6849 char constant;
6850 char simple;
6851 char nonconst;
6852 char implicit;
6853 char erroneous;
6854 char outer;
6855 char incremental;
6856 char designated;
6859 static struct constructor_stack *constructor_stack;
6861 /* This stack represents designators from some range designator up to
6862 the last designator in the list. */
6864 struct constructor_range_stack
6866 struct constructor_range_stack *next, *prev;
6867 struct constructor_stack *stack;
6868 tree range_start;
6869 tree index;
6870 tree range_end;
6871 tree fields;
6874 static struct constructor_range_stack *constructor_range_stack;
6876 /* This stack records separate initializers that are nested.
6877 Nested initializers can't happen in ANSI C, but GNU C allows them
6878 in cases like { ... (struct foo) { ... } ... }. */
6880 struct initializer_stack
6882 struct initializer_stack *next;
6883 tree decl;
6884 struct constructor_stack *constructor_stack;
6885 struct constructor_range_stack *constructor_range_stack;
6886 vec<constructor_elt, va_gc> *elements;
6887 struct spelling *spelling;
6888 struct spelling *spelling_base;
6889 int spelling_size;
6890 char top_level;
6891 char require_constant_value;
6892 char require_constant_elements;
6895 static struct initializer_stack *initializer_stack;
6897 /* Prepare to parse and output the initializer for variable DECL. */
6899 void
6900 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6902 const char *locus;
6903 struct initializer_stack *p = XNEW (struct initializer_stack);
6905 p->decl = constructor_decl;
6906 p->require_constant_value = require_constant_value;
6907 p->require_constant_elements = require_constant_elements;
6908 p->constructor_stack = constructor_stack;
6909 p->constructor_range_stack = constructor_range_stack;
6910 p->elements = constructor_elements;
6911 p->spelling = spelling;
6912 p->spelling_base = spelling_base;
6913 p->spelling_size = spelling_size;
6914 p->top_level = constructor_top_level;
6915 p->next = initializer_stack;
6916 initializer_stack = p;
6918 constructor_decl = decl;
6919 constructor_designated = 0;
6920 constructor_top_level = top_level;
6922 if (decl != 0 && decl != error_mark_node)
6924 require_constant_value = TREE_STATIC (decl);
6925 require_constant_elements
6926 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6927 /* For a scalar, you can always use any value to initialize,
6928 even within braces. */
6929 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6930 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6931 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6932 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6933 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6935 else
6937 require_constant_value = 0;
6938 require_constant_elements = 0;
6939 locus = _("(anonymous)");
6942 constructor_stack = 0;
6943 constructor_range_stack = 0;
6945 missing_braces_mentioned = 0;
6947 spelling_base = 0;
6948 spelling_size = 0;
6949 RESTORE_SPELLING_DEPTH (0);
6951 if (locus)
6952 push_string (locus);
6955 void
6956 finish_init (void)
6958 struct initializer_stack *p = initializer_stack;
6960 /* Free the whole constructor stack of this initializer. */
6961 while (constructor_stack)
6963 struct constructor_stack *q = constructor_stack;
6964 constructor_stack = q->next;
6965 free (q);
6968 gcc_assert (!constructor_range_stack);
6970 /* Pop back to the data of the outer initializer (if any). */
6971 free (spelling_base);
6973 constructor_decl = p->decl;
6974 require_constant_value = p->require_constant_value;
6975 require_constant_elements = p->require_constant_elements;
6976 constructor_stack = p->constructor_stack;
6977 constructor_range_stack = p->constructor_range_stack;
6978 constructor_elements = p->elements;
6979 spelling = p->spelling;
6980 spelling_base = p->spelling_base;
6981 spelling_size = p->spelling_size;
6982 constructor_top_level = p->top_level;
6983 initializer_stack = p->next;
6984 free (p);
6987 /* Call here when we see the initializer is surrounded by braces.
6988 This is instead of a call to push_init_level;
6989 it is matched by a call to pop_init_level.
6991 TYPE is the type to initialize, for a constructor expression.
6992 For an initializer for a decl, TYPE is zero. */
6994 void
6995 really_start_incremental_init (tree type)
6997 struct constructor_stack *p = XNEW (struct constructor_stack);
6999 if (type == 0)
7000 type = TREE_TYPE (constructor_decl);
7002 if (TREE_CODE (type) == VECTOR_TYPE
7003 && TYPE_VECTOR_OPAQUE (type))
7004 error ("opaque vector types cannot be initialized");
7006 p->type = constructor_type;
7007 p->fields = constructor_fields;
7008 p->index = constructor_index;
7009 p->max_index = constructor_max_index;
7010 p->unfilled_index = constructor_unfilled_index;
7011 p->unfilled_fields = constructor_unfilled_fields;
7012 p->bit_index = constructor_bit_index;
7013 p->elements = constructor_elements;
7014 p->constant = constructor_constant;
7015 p->simple = constructor_simple;
7016 p->nonconst = constructor_nonconst;
7017 p->erroneous = constructor_erroneous;
7018 p->pending_elts = constructor_pending_elts;
7019 p->depth = constructor_depth;
7020 p->replacement_value.value = 0;
7021 p->replacement_value.original_code = ERROR_MARK;
7022 p->replacement_value.original_type = NULL;
7023 p->implicit = 0;
7024 p->range_stack = 0;
7025 p->outer = 0;
7026 p->incremental = constructor_incremental;
7027 p->designated = constructor_designated;
7028 p->next = 0;
7029 constructor_stack = p;
7031 constructor_constant = 1;
7032 constructor_simple = 1;
7033 constructor_nonconst = 0;
7034 constructor_depth = SPELLING_DEPTH ();
7035 constructor_elements = NULL;
7036 constructor_pending_elts = 0;
7037 constructor_type = type;
7038 constructor_incremental = 1;
7039 constructor_designated = 0;
7040 designator_depth = 0;
7041 designator_erroneous = 0;
7043 if (TREE_CODE (constructor_type) == RECORD_TYPE
7044 || TREE_CODE (constructor_type) == UNION_TYPE)
7046 constructor_fields = TYPE_FIELDS (constructor_type);
7047 /* Skip any nameless bit fields at the beginning. */
7048 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7049 && DECL_NAME (constructor_fields) == 0)
7050 constructor_fields = DECL_CHAIN (constructor_fields);
7052 constructor_unfilled_fields = constructor_fields;
7053 constructor_bit_index = bitsize_zero_node;
7055 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7057 if (TYPE_DOMAIN (constructor_type))
7059 constructor_max_index
7060 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7062 /* Detect non-empty initializations of zero-length arrays. */
7063 if (constructor_max_index == NULL_TREE
7064 && TYPE_SIZE (constructor_type))
7065 constructor_max_index = integer_minus_one_node;
7067 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7068 to initialize VLAs will cause a proper error; avoid tree
7069 checking errors as well by setting a safe value. */
7070 if (constructor_max_index
7071 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7072 constructor_max_index = integer_minus_one_node;
7074 constructor_index
7075 = convert (bitsizetype,
7076 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7078 else
7080 constructor_index = bitsize_zero_node;
7081 constructor_max_index = NULL_TREE;
7084 constructor_unfilled_index = constructor_index;
7086 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7088 /* Vectors are like simple fixed-size arrays. */
7089 constructor_max_index =
7090 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7091 constructor_index = bitsize_zero_node;
7092 constructor_unfilled_index = constructor_index;
7094 else
7096 /* Handle the case of int x = {5}; */
7097 constructor_fields = constructor_type;
7098 constructor_unfilled_fields = constructor_type;
7102 /* Push down into a subobject, for initialization.
7103 If this is for an explicit set of braces, IMPLICIT is 0.
7104 If it is because the next element belongs at a lower level,
7105 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7107 void
7108 push_init_level (int implicit, struct obstack * braced_init_obstack)
7110 struct constructor_stack *p;
7111 tree value = NULL_TREE;
7113 /* If we've exhausted any levels that didn't have braces,
7114 pop them now. If implicit == 1, this will have been done in
7115 process_init_element; do not repeat it here because in the case
7116 of excess initializers for an empty aggregate this leads to an
7117 infinite cycle of popping a level and immediately recreating
7118 it. */
7119 if (implicit != 1)
7121 while (constructor_stack->implicit)
7123 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7124 || TREE_CODE (constructor_type) == UNION_TYPE)
7125 && constructor_fields == 0)
7126 process_init_element (pop_init_level (1, braced_init_obstack),
7127 true, braced_init_obstack);
7128 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7129 && constructor_max_index
7130 && tree_int_cst_lt (constructor_max_index,
7131 constructor_index))
7132 process_init_element (pop_init_level (1, braced_init_obstack),
7133 true, braced_init_obstack);
7134 else
7135 break;
7139 /* Unless this is an explicit brace, we need to preserve previous
7140 content if any. */
7141 if (implicit)
7143 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7144 || TREE_CODE (constructor_type) == UNION_TYPE)
7145 && constructor_fields)
7146 value = find_init_member (constructor_fields, braced_init_obstack);
7147 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7148 value = find_init_member (constructor_index, braced_init_obstack);
7151 p = XNEW (struct constructor_stack);
7152 p->type = constructor_type;
7153 p->fields = constructor_fields;
7154 p->index = constructor_index;
7155 p->max_index = constructor_max_index;
7156 p->unfilled_index = constructor_unfilled_index;
7157 p->unfilled_fields = constructor_unfilled_fields;
7158 p->bit_index = constructor_bit_index;
7159 p->elements = constructor_elements;
7160 p->constant = constructor_constant;
7161 p->simple = constructor_simple;
7162 p->nonconst = constructor_nonconst;
7163 p->erroneous = constructor_erroneous;
7164 p->pending_elts = constructor_pending_elts;
7165 p->depth = constructor_depth;
7166 p->replacement_value.value = 0;
7167 p->replacement_value.original_code = ERROR_MARK;
7168 p->replacement_value.original_type = NULL;
7169 p->implicit = implicit;
7170 p->outer = 0;
7171 p->incremental = constructor_incremental;
7172 p->designated = constructor_designated;
7173 p->next = constructor_stack;
7174 p->range_stack = 0;
7175 constructor_stack = p;
7177 constructor_constant = 1;
7178 constructor_simple = 1;
7179 constructor_nonconst = 0;
7180 constructor_depth = SPELLING_DEPTH ();
7181 constructor_elements = NULL;
7182 constructor_incremental = 1;
7183 constructor_designated = 0;
7184 constructor_pending_elts = 0;
7185 if (!implicit)
7187 p->range_stack = constructor_range_stack;
7188 constructor_range_stack = 0;
7189 designator_depth = 0;
7190 designator_erroneous = 0;
7193 /* Don't die if an entire brace-pair level is superfluous
7194 in the containing level. */
7195 if (constructor_type == 0)
7197 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7198 || TREE_CODE (constructor_type) == UNION_TYPE)
7200 /* Don't die if there are extra init elts at the end. */
7201 if (constructor_fields == 0)
7202 constructor_type = 0;
7203 else
7205 constructor_type = TREE_TYPE (constructor_fields);
7206 push_member_name (constructor_fields);
7207 constructor_depth++;
7210 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7212 constructor_type = TREE_TYPE (constructor_type);
7213 push_array_bounds (tree_low_cst (constructor_index, 1));
7214 constructor_depth++;
7217 if (constructor_type == 0)
7219 error_init ("extra brace group at end of initializer");
7220 constructor_fields = 0;
7221 constructor_unfilled_fields = 0;
7222 return;
7225 if (value && TREE_CODE (value) == CONSTRUCTOR)
7227 constructor_constant = TREE_CONSTANT (value);
7228 constructor_simple = TREE_STATIC (value);
7229 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7230 constructor_elements = CONSTRUCTOR_ELTS (value);
7231 if (!vec_safe_is_empty (constructor_elements)
7232 && (TREE_CODE (constructor_type) == RECORD_TYPE
7233 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7234 set_nonincremental_init (braced_init_obstack);
7237 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
7239 missing_braces_mentioned = 1;
7240 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
7243 if (TREE_CODE (constructor_type) == RECORD_TYPE
7244 || TREE_CODE (constructor_type) == UNION_TYPE)
7246 constructor_fields = TYPE_FIELDS (constructor_type);
7247 /* Skip any nameless bit fields at the beginning. */
7248 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7249 && DECL_NAME (constructor_fields) == 0)
7250 constructor_fields = DECL_CHAIN (constructor_fields);
7252 constructor_unfilled_fields = constructor_fields;
7253 constructor_bit_index = bitsize_zero_node;
7255 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7257 /* Vectors are like simple fixed-size arrays. */
7258 constructor_max_index =
7259 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7260 constructor_index = bitsize_int (0);
7261 constructor_unfilled_index = constructor_index;
7263 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7265 if (TYPE_DOMAIN (constructor_type))
7267 constructor_max_index
7268 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7270 /* Detect non-empty initializations of zero-length arrays. */
7271 if (constructor_max_index == NULL_TREE
7272 && TYPE_SIZE (constructor_type))
7273 constructor_max_index = integer_minus_one_node;
7275 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7276 to initialize VLAs will cause a proper error; avoid tree
7277 checking errors as well by setting a safe value. */
7278 if (constructor_max_index
7279 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7280 constructor_max_index = integer_minus_one_node;
7282 constructor_index
7283 = convert (bitsizetype,
7284 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7286 else
7287 constructor_index = bitsize_zero_node;
7289 constructor_unfilled_index = constructor_index;
7290 if (value && TREE_CODE (value) == STRING_CST)
7292 /* We need to split the char/wchar array into individual
7293 characters, so that we don't have to special case it
7294 everywhere. */
7295 set_nonincremental_init_from_string (value, braced_init_obstack);
7298 else
7300 if (constructor_type != error_mark_node)
7301 warning_init (0, "braces around scalar initializer");
7302 constructor_fields = constructor_type;
7303 constructor_unfilled_fields = constructor_type;
7307 /* At the end of an implicit or explicit brace level,
7308 finish up that level of constructor. If a single expression
7309 with redundant braces initialized that level, return the
7310 c_expr structure for that expression. Otherwise, the original_code
7311 element is set to ERROR_MARK.
7312 If we were outputting the elements as they are read, return 0 as the value
7313 from inner levels (process_init_element ignores that),
7314 but return error_mark_node as the value from the outermost level
7315 (that's what we want to put in DECL_INITIAL).
7316 Otherwise, return a CONSTRUCTOR expression as the value. */
7318 struct c_expr
7319 pop_init_level (int implicit, struct obstack * braced_init_obstack)
7321 struct constructor_stack *p;
7322 struct c_expr ret;
7323 ret.value = 0;
7324 ret.original_code = ERROR_MARK;
7325 ret.original_type = NULL;
7327 if (implicit == 0)
7329 /* When we come to an explicit close brace,
7330 pop any inner levels that didn't have explicit braces. */
7331 while (constructor_stack->implicit)
7333 process_init_element (pop_init_level (1, braced_init_obstack),
7334 true, braced_init_obstack);
7336 gcc_assert (!constructor_range_stack);
7339 /* Now output all pending elements. */
7340 constructor_incremental = 1;
7341 output_pending_init_elements (1, braced_init_obstack);
7343 p = constructor_stack;
7345 /* Error for initializing a flexible array member, or a zero-length
7346 array member in an inappropriate context. */
7347 if (constructor_type && constructor_fields
7348 && TREE_CODE (constructor_type) == ARRAY_TYPE
7349 && TYPE_DOMAIN (constructor_type)
7350 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7352 /* Silently discard empty initializations. The parser will
7353 already have pedwarned for empty brackets. */
7354 if (integer_zerop (constructor_unfilled_index))
7355 constructor_type = NULL_TREE;
7356 else
7358 gcc_assert (!TYPE_SIZE (constructor_type));
7360 if (constructor_depth > 2)
7361 error_init ("initialization of flexible array member in a nested context");
7362 else
7363 pedwarn_init (input_location, OPT_Wpedantic,
7364 "initialization of a flexible array member");
7366 /* We have already issued an error message for the existence
7367 of a flexible array member not at the end of the structure.
7368 Discard the initializer so that we do not die later. */
7369 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7370 constructor_type = NULL_TREE;
7374 /* Warn when some struct elements are implicitly initialized to zero. */
7375 if (warn_missing_field_initializers
7376 && constructor_type
7377 && TREE_CODE (constructor_type) == RECORD_TYPE
7378 && constructor_unfilled_fields)
7380 bool constructor_zeroinit =
7381 (vec_safe_length (constructor_elements) == 1
7382 && integer_zerop ((*constructor_elements)[0].value));
7384 /* Do not warn for flexible array members or zero-length arrays. */
7385 while (constructor_unfilled_fields
7386 && (!DECL_SIZE (constructor_unfilled_fields)
7387 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7388 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7390 if (constructor_unfilled_fields
7391 /* Do not warn if this level of the initializer uses member
7392 designators; it is likely to be deliberate. */
7393 && !constructor_designated
7394 /* Do not warn about initializing with ` = {0}'. */
7395 && !constructor_zeroinit)
7397 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7398 "missing initializer for field %qD of %qT",
7399 constructor_unfilled_fields,
7400 constructor_type))
7401 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7402 "%qD declared here", constructor_unfilled_fields);
7406 /* Pad out the end of the structure. */
7407 if (p->replacement_value.value)
7408 /* If this closes a superfluous brace pair,
7409 just pass out the element between them. */
7410 ret = p->replacement_value;
7411 else if (constructor_type == 0)
7413 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7414 && TREE_CODE (constructor_type) != UNION_TYPE
7415 && TREE_CODE (constructor_type) != ARRAY_TYPE
7416 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7418 /* A nonincremental scalar initializer--just return
7419 the element, after verifying there is just one. */
7420 if (vec_safe_is_empty (constructor_elements))
7422 if (!constructor_erroneous)
7423 error_init ("empty scalar initializer");
7424 ret.value = error_mark_node;
7426 else if (vec_safe_length (constructor_elements) != 1)
7428 error_init ("extra elements in scalar initializer");
7429 ret.value = (*constructor_elements)[0].value;
7431 else
7432 ret.value = (*constructor_elements)[0].value;
7434 else
7436 if (constructor_erroneous)
7437 ret.value = error_mark_node;
7438 else
7440 ret.value = build_constructor (constructor_type,
7441 constructor_elements);
7442 if (constructor_constant)
7443 TREE_CONSTANT (ret.value) = 1;
7444 if (constructor_constant && constructor_simple)
7445 TREE_STATIC (ret.value) = 1;
7446 if (constructor_nonconst)
7447 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7451 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7453 if (constructor_nonconst)
7454 ret.original_code = C_MAYBE_CONST_EXPR;
7455 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7456 ret.original_code = ERROR_MARK;
7459 constructor_type = p->type;
7460 constructor_fields = p->fields;
7461 constructor_index = p->index;
7462 constructor_max_index = p->max_index;
7463 constructor_unfilled_index = p->unfilled_index;
7464 constructor_unfilled_fields = p->unfilled_fields;
7465 constructor_bit_index = p->bit_index;
7466 constructor_elements = p->elements;
7467 constructor_constant = p->constant;
7468 constructor_simple = p->simple;
7469 constructor_nonconst = p->nonconst;
7470 constructor_erroneous = p->erroneous;
7471 constructor_incremental = p->incremental;
7472 constructor_designated = p->designated;
7473 constructor_pending_elts = p->pending_elts;
7474 constructor_depth = p->depth;
7475 if (!p->implicit)
7476 constructor_range_stack = p->range_stack;
7477 RESTORE_SPELLING_DEPTH (constructor_depth);
7479 constructor_stack = p->next;
7480 free (p);
7482 if (ret.value == 0 && constructor_stack == 0)
7483 ret.value = error_mark_node;
7484 return ret;
7487 /* Common handling for both array range and field name designators.
7488 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7490 static int
7491 set_designator (int array, struct obstack * braced_init_obstack)
7493 tree subtype;
7494 enum tree_code subcode;
7496 /* Don't die if an entire brace-pair level is superfluous
7497 in the containing level. */
7498 if (constructor_type == 0)
7499 return 1;
7501 /* If there were errors in this designator list already, bail out
7502 silently. */
7503 if (designator_erroneous)
7504 return 1;
7506 if (!designator_depth)
7508 gcc_assert (!constructor_range_stack);
7510 /* Designator list starts at the level of closest explicit
7511 braces. */
7512 while (constructor_stack->implicit)
7514 process_init_element (pop_init_level (1, braced_init_obstack),
7515 true, braced_init_obstack);
7517 constructor_designated = 1;
7518 return 0;
7521 switch (TREE_CODE (constructor_type))
7523 case RECORD_TYPE:
7524 case UNION_TYPE:
7525 subtype = TREE_TYPE (constructor_fields);
7526 if (subtype != error_mark_node)
7527 subtype = TYPE_MAIN_VARIANT (subtype);
7528 break;
7529 case ARRAY_TYPE:
7530 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7531 break;
7532 default:
7533 gcc_unreachable ();
7536 subcode = TREE_CODE (subtype);
7537 if (array && subcode != ARRAY_TYPE)
7539 error_init ("array index in non-array initializer");
7540 return 1;
7542 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7544 error_init ("field name not in record or union initializer");
7545 return 1;
7548 constructor_designated = 1;
7549 push_init_level (2, braced_init_obstack);
7550 return 0;
7553 /* If there are range designators in designator list, push a new designator
7554 to constructor_range_stack. RANGE_END is end of such stack range or
7555 NULL_TREE if there is no range designator at this level. */
7557 static void
7558 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7560 struct constructor_range_stack *p;
7562 p = (struct constructor_range_stack *)
7563 obstack_alloc (braced_init_obstack,
7564 sizeof (struct constructor_range_stack));
7565 p->prev = constructor_range_stack;
7566 p->next = 0;
7567 p->fields = constructor_fields;
7568 p->range_start = constructor_index;
7569 p->index = constructor_index;
7570 p->stack = constructor_stack;
7571 p->range_end = range_end;
7572 if (constructor_range_stack)
7573 constructor_range_stack->next = p;
7574 constructor_range_stack = p;
7577 /* Within an array initializer, specify the next index to be initialized.
7578 FIRST is that index. If LAST is nonzero, then initialize a range
7579 of indices, running from FIRST through LAST. */
7581 void
7582 set_init_index (tree first, tree last,
7583 struct obstack * braced_init_obstack)
7585 if (set_designator (1, braced_init_obstack))
7586 return;
7588 designator_erroneous = 1;
7590 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7591 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7593 error_init ("array index in initializer not of integer type");
7594 return;
7597 if (TREE_CODE (first) != INTEGER_CST)
7599 first = c_fully_fold (first, false, NULL);
7600 if (TREE_CODE (first) == INTEGER_CST)
7601 pedwarn_init (input_location, OPT_Wpedantic,
7602 "array index in initializer is not "
7603 "an integer constant expression");
7606 if (last && TREE_CODE (last) != INTEGER_CST)
7608 last = c_fully_fold (last, false, NULL);
7609 if (TREE_CODE (last) == INTEGER_CST)
7610 pedwarn_init (input_location, OPT_Wpedantic,
7611 "array index in initializer is not "
7612 "an integer constant expression");
7615 if (TREE_CODE (first) != INTEGER_CST)
7616 error_init ("nonconstant array index in initializer");
7617 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7618 error_init ("nonconstant array index in initializer");
7619 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7620 error_init ("array index in non-array initializer");
7621 else if (tree_int_cst_sgn (first) == -1)
7622 error_init ("array index in initializer exceeds array bounds");
7623 else if (constructor_max_index
7624 && tree_int_cst_lt (constructor_max_index, first))
7625 error_init ("array index in initializer exceeds array bounds");
7626 else
7628 constant_expression_warning (first);
7629 if (last)
7630 constant_expression_warning (last);
7631 constructor_index = convert (bitsizetype, first);
7632 if (tree_int_cst_lt (constructor_index, first))
7634 constructor_index = copy_node (constructor_index);
7635 TREE_OVERFLOW (constructor_index) = 1;
7638 if (last)
7640 if (tree_int_cst_equal (first, last))
7641 last = 0;
7642 else if (tree_int_cst_lt (last, first))
7644 error_init ("empty index range in initializer");
7645 last = 0;
7647 else
7649 last = convert (bitsizetype, last);
7650 if (constructor_max_index != 0
7651 && tree_int_cst_lt (constructor_max_index, last))
7653 error_init ("array index range in initializer exceeds array bounds");
7654 last = 0;
7659 designator_depth++;
7660 designator_erroneous = 0;
7661 if (constructor_range_stack || last)
7662 push_range_stack (last, braced_init_obstack);
7666 /* Within a struct initializer, specify the next field to be initialized. */
7668 void
7669 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7671 tree field;
7673 if (set_designator (0, braced_init_obstack))
7674 return;
7676 designator_erroneous = 1;
7678 if (TREE_CODE (constructor_type) != RECORD_TYPE
7679 && TREE_CODE (constructor_type) != UNION_TYPE)
7681 error_init ("field name not in record or union initializer");
7682 return;
7685 field = lookup_field (constructor_type, fieldname);
7687 if (field == 0)
7688 error ("unknown field %qE specified in initializer", fieldname);
7689 else
7692 constructor_fields = TREE_VALUE (field);
7693 designator_depth++;
7694 designator_erroneous = 0;
7695 if (constructor_range_stack)
7696 push_range_stack (NULL_TREE, braced_init_obstack);
7697 field = TREE_CHAIN (field);
7698 if (field)
7700 if (set_designator (0, braced_init_obstack))
7701 return;
7704 while (field != NULL_TREE);
7707 /* Add a new initializer to the tree of pending initializers. PURPOSE
7708 identifies the initializer, either array index or field in a structure.
7709 VALUE is the value of that index or field. If ORIGTYPE is not
7710 NULL_TREE, it is the original type of VALUE.
7712 IMPLICIT is true if value comes from pop_init_level (1),
7713 the new initializer has been merged with the existing one
7714 and thus no warnings should be emitted about overriding an
7715 existing initializer. */
7717 static void
7718 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7719 struct obstack * braced_init_obstack)
7721 struct init_node *p, **q, *r;
7723 q = &constructor_pending_elts;
7724 p = 0;
7726 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7728 while (*q != 0)
7730 p = *q;
7731 if (tree_int_cst_lt (purpose, p->purpose))
7732 q = &p->left;
7733 else if (tree_int_cst_lt (p->purpose, purpose))
7734 q = &p->right;
7735 else
7737 if (!implicit)
7739 if (TREE_SIDE_EFFECTS (p->value))
7740 warning_init (0, "initialized field with side-effects overwritten");
7741 else if (warn_override_init)
7742 warning_init (OPT_Woverride_init, "initialized field overwritten");
7744 p->value = value;
7745 p->origtype = origtype;
7746 return;
7750 else
7752 tree bitpos;
7754 bitpos = bit_position (purpose);
7755 while (*q != NULL)
7757 p = *q;
7758 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7759 q = &p->left;
7760 else if (p->purpose != purpose)
7761 q = &p->right;
7762 else
7764 if (!implicit)
7766 if (TREE_SIDE_EFFECTS (p->value))
7767 warning_init (0, "initialized field with side-effects overwritten");
7768 else if (warn_override_init)
7769 warning_init (OPT_Woverride_init, "initialized field overwritten");
7771 p->value = value;
7772 p->origtype = origtype;
7773 return;
7778 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7779 sizeof (struct init_node));
7780 r->purpose = purpose;
7781 r->value = value;
7782 r->origtype = origtype;
7784 *q = r;
7785 r->parent = p;
7786 r->left = 0;
7787 r->right = 0;
7788 r->balance = 0;
7790 while (p)
7792 struct init_node *s;
7794 if (r == p->left)
7796 if (p->balance == 0)
7797 p->balance = -1;
7798 else if (p->balance < 0)
7800 if (r->balance < 0)
7802 /* L rotation. */
7803 p->left = r->right;
7804 if (p->left)
7805 p->left->parent = p;
7806 r->right = p;
7808 p->balance = 0;
7809 r->balance = 0;
7811 s = p->parent;
7812 p->parent = r;
7813 r->parent = s;
7814 if (s)
7816 if (s->left == p)
7817 s->left = r;
7818 else
7819 s->right = r;
7821 else
7822 constructor_pending_elts = r;
7824 else
7826 /* LR rotation. */
7827 struct init_node *t = r->right;
7829 r->right = t->left;
7830 if (r->right)
7831 r->right->parent = r;
7832 t->left = r;
7834 p->left = t->right;
7835 if (p->left)
7836 p->left->parent = p;
7837 t->right = p;
7839 p->balance = t->balance < 0;
7840 r->balance = -(t->balance > 0);
7841 t->balance = 0;
7843 s = p->parent;
7844 p->parent = t;
7845 r->parent = t;
7846 t->parent = s;
7847 if (s)
7849 if (s->left == p)
7850 s->left = t;
7851 else
7852 s->right = t;
7854 else
7855 constructor_pending_elts = t;
7857 break;
7859 else
7861 /* p->balance == +1; growth of left side balances the node. */
7862 p->balance = 0;
7863 break;
7866 else /* r == p->right */
7868 if (p->balance == 0)
7869 /* Growth propagation from right side. */
7870 p->balance++;
7871 else if (p->balance > 0)
7873 if (r->balance > 0)
7875 /* R rotation. */
7876 p->right = r->left;
7877 if (p->right)
7878 p->right->parent = p;
7879 r->left = p;
7881 p->balance = 0;
7882 r->balance = 0;
7884 s = p->parent;
7885 p->parent = r;
7886 r->parent = s;
7887 if (s)
7889 if (s->left == p)
7890 s->left = r;
7891 else
7892 s->right = r;
7894 else
7895 constructor_pending_elts = r;
7897 else /* r->balance == -1 */
7899 /* RL rotation */
7900 struct init_node *t = r->left;
7902 r->left = t->right;
7903 if (r->left)
7904 r->left->parent = r;
7905 t->right = r;
7907 p->right = t->left;
7908 if (p->right)
7909 p->right->parent = p;
7910 t->left = p;
7912 r->balance = (t->balance < 0);
7913 p->balance = -(t->balance > 0);
7914 t->balance = 0;
7916 s = p->parent;
7917 p->parent = t;
7918 r->parent = t;
7919 t->parent = s;
7920 if (s)
7922 if (s->left == p)
7923 s->left = t;
7924 else
7925 s->right = t;
7927 else
7928 constructor_pending_elts = t;
7930 break;
7932 else
7934 /* p->balance == -1; growth of right side balances the node. */
7935 p->balance = 0;
7936 break;
7940 r = p;
7941 p = p->parent;
7945 /* Build AVL tree from a sorted chain. */
7947 static void
7948 set_nonincremental_init (struct obstack * braced_init_obstack)
7950 unsigned HOST_WIDE_INT ix;
7951 tree index, value;
7953 if (TREE_CODE (constructor_type) != RECORD_TYPE
7954 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7955 return;
7957 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7959 add_pending_init (index, value, NULL_TREE, true,
7960 braced_init_obstack);
7962 constructor_elements = NULL;
7963 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7965 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7966 /* Skip any nameless bit fields at the beginning. */
7967 while (constructor_unfilled_fields != 0
7968 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7969 && DECL_NAME (constructor_unfilled_fields) == 0)
7970 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7973 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7975 if (TYPE_DOMAIN (constructor_type))
7976 constructor_unfilled_index
7977 = convert (bitsizetype,
7978 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7979 else
7980 constructor_unfilled_index = bitsize_zero_node;
7982 constructor_incremental = 0;
7985 /* Build AVL tree from a string constant. */
7987 static void
7988 set_nonincremental_init_from_string (tree str,
7989 struct obstack * braced_init_obstack)
7991 tree value, purpose, type;
7992 HOST_WIDE_INT val[2];
7993 const char *p, *end;
7994 int byte, wchar_bytes, charwidth, bitpos;
7996 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7998 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7999 charwidth = TYPE_PRECISION (char_type_node);
8000 type = TREE_TYPE (constructor_type);
8001 p = TREE_STRING_POINTER (str);
8002 end = p + TREE_STRING_LENGTH (str);
8004 for (purpose = bitsize_zero_node;
8005 p < end
8006 && !(constructor_max_index
8007 && tree_int_cst_lt (constructor_max_index, purpose));
8008 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8010 if (wchar_bytes == 1)
8012 val[1] = (unsigned char) *p++;
8013 val[0] = 0;
8015 else
8017 val[0] = 0;
8018 val[1] = 0;
8019 for (byte = 0; byte < wchar_bytes; byte++)
8021 if (BYTES_BIG_ENDIAN)
8022 bitpos = (wchar_bytes - byte - 1) * charwidth;
8023 else
8024 bitpos = byte * charwidth;
8025 val[bitpos < HOST_BITS_PER_WIDE_INT]
8026 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8027 << (bitpos % HOST_BITS_PER_WIDE_INT);
8031 if (!TYPE_UNSIGNED (type))
8033 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8034 if (bitpos < HOST_BITS_PER_WIDE_INT)
8036 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8038 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
8039 val[0] = -1;
8042 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8044 if (val[1] < 0)
8045 val[0] = -1;
8047 else if (val[0] & (((HOST_WIDE_INT) 1)
8048 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8049 val[0] |= ((HOST_WIDE_INT) -1)
8050 << (bitpos - HOST_BITS_PER_WIDE_INT);
8053 value = build_int_cst_wide (type, val[1], val[0]);
8054 add_pending_init (purpose, value, NULL_TREE, true,
8055 braced_init_obstack);
8058 constructor_incremental = 0;
8061 /* Return value of FIELD in pending initializer or zero if the field was
8062 not initialized yet. */
8064 static tree
8065 find_init_member (tree field, struct obstack * braced_init_obstack)
8067 struct init_node *p;
8069 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8071 if (constructor_incremental
8072 && tree_int_cst_lt (field, constructor_unfilled_index))
8073 set_nonincremental_init (braced_init_obstack);
8075 p = constructor_pending_elts;
8076 while (p)
8078 if (tree_int_cst_lt (field, p->purpose))
8079 p = p->left;
8080 else if (tree_int_cst_lt (p->purpose, field))
8081 p = p->right;
8082 else
8083 return p->value;
8086 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8088 tree bitpos = bit_position (field);
8090 if (constructor_incremental
8091 && (!constructor_unfilled_fields
8092 || tree_int_cst_lt (bitpos,
8093 bit_position (constructor_unfilled_fields))))
8094 set_nonincremental_init (braced_init_obstack);
8096 p = constructor_pending_elts;
8097 while (p)
8099 if (field == p->purpose)
8100 return p->value;
8101 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8102 p = p->left;
8103 else
8104 p = p->right;
8107 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8109 if (!vec_safe_is_empty (constructor_elements)
8110 && (constructor_elements->last ().index == field))
8111 return constructor_elements->last ().value;
8113 return 0;
8116 /* "Output" the next constructor element.
8117 At top level, really output it to assembler code now.
8118 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8119 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8120 TYPE is the data type that the containing data type wants here.
8121 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8122 If VALUE is a string constant, STRICT_STRING is true if it is
8123 unparenthesized or we should not warn here for it being parenthesized.
8124 For other types of VALUE, STRICT_STRING is not used.
8126 PENDING if non-nil means output pending elements that belong
8127 right after this element. (PENDING is normally 1;
8128 it is 0 while outputting pending elements, to avoid recursion.)
8130 IMPLICIT is true if value comes from pop_init_level (1),
8131 the new initializer has been merged with the existing one
8132 and thus no warnings should be emitted about overriding an
8133 existing initializer. */
8135 static void
8136 output_init_element (tree value, tree origtype, bool strict_string, tree type,
8137 tree field, int pending, bool implicit,
8138 struct obstack * braced_init_obstack)
8140 tree semantic_type = NULL_TREE;
8141 bool maybe_const = true;
8142 bool npc;
8144 if (type == error_mark_node || value == error_mark_node)
8146 constructor_erroneous = 1;
8147 return;
8149 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8150 && (TREE_CODE (value) == STRING_CST
8151 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8152 && !(TREE_CODE (value) == STRING_CST
8153 && TREE_CODE (type) == ARRAY_TYPE
8154 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8155 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8156 TYPE_MAIN_VARIANT (type)))
8157 value = array_to_pointer_conversion (input_location, value);
8159 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8160 && require_constant_value && !flag_isoc99 && pending)
8162 /* As an extension, allow initializing objects with static storage
8163 duration with compound literals (which are then treated just as
8164 the brace enclosed list they contain). */
8165 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8166 value = DECL_INITIAL (decl);
8169 npc = null_pointer_constant_p (value);
8170 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8172 semantic_type = TREE_TYPE (value);
8173 value = TREE_OPERAND (value, 0);
8175 value = c_fully_fold (value, require_constant_value, &maybe_const);
8177 if (value == error_mark_node)
8178 constructor_erroneous = 1;
8179 else if (!TREE_CONSTANT (value))
8180 constructor_constant = 0;
8181 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8182 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8183 || TREE_CODE (constructor_type) == UNION_TYPE)
8184 && DECL_C_BIT_FIELD (field)
8185 && TREE_CODE (value) != INTEGER_CST))
8186 constructor_simple = 0;
8187 if (!maybe_const)
8188 constructor_nonconst = 1;
8190 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8192 if (require_constant_value)
8194 error_init ("initializer element is not constant");
8195 value = error_mark_node;
8197 else if (require_constant_elements)
8198 pedwarn (input_location, 0,
8199 "initializer element is not computable at load time");
8201 else if (!maybe_const
8202 && (require_constant_value || require_constant_elements))
8203 pedwarn_init (input_location, 0,
8204 "initializer element is not a constant expression");
8206 /* Issue -Wc++-compat warnings about initializing a bitfield with
8207 enum type. */
8208 if (warn_cxx_compat
8209 && field != NULL_TREE
8210 && TREE_CODE (field) == FIELD_DECL
8211 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8212 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8213 != TYPE_MAIN_VARIANT (type))
8214 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8216 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8217 if (checktype != error_mark_node
8218 && (TYPE_MAIN_VARIANT (checktype)
8219 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8220 warning_init (OPT_Wc___compat,
8221 "enum conversion in initialization is invalid in C++");
8224 /* If this field is empty (and not at the end of structure),
8225 don't do anything other than checking the initializer. */
8226 if (field
8227 && (TREE_TYPE (field) == error_mark_node
8228 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8229 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8230 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8231 || DECL_CHAIN (field)))))
8232 return;
8234 if (semantic_type)
8235 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8236 value = digest_init (input_location, type, value, origtype, npc,
8237 strict_string, require_constant_value);
8238 if (value == error_mark_node)
8240 constructor_erroneous = 1;
8241 return;
8243 if (require_constant_value || require_constant_elements)
8244 constant_expression_warning (value);
8246 /* If this element doesn't come next in sequence,
8247 put it on constructor_pending_elts. */
8248 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8249 && (!constructor_incremental
8250 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8252 if (constructor_incremental
8253 && tree_int_cst_lt (field, constructor_unfilled_index))
8254 set_nonincremental_init (braced_init_obstack);
8256 add_pending_init (field, value, origtype, implicit,
8257 braced_init_obstack);
8258 return;
8260 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8261 && (!constructor_incremental
8262 || field != constructor_unfilled_fields))
8264 /* We do this for records but not for unions. In a union,
8265 no matter which field is specified, it can be initialized
8266 right away since it starts at the beginning of the union. */
8267 if (constructor_incremental)
8269 if (!constructor_unfilled_fields)
8270 set_nonincremental_init (braced_init_obstack);
8271 else
8273 tree bitpos, unfillpos;
8275 bitpos = bit_position (field);
8276 unfillpos = bit_position (constructor_unfilled_fields);
8278 if (tree_int_cst_lt (bitpos, unfillpos))
8279 set_nonincremental_init (braced_init_obstack);
8283 add_pending_init (field, value, origtype, implicit,
8284 braced_init_obstack);
8285 return;
8287 else if (TREE_CODE (constructor_type) == UNION_TYPE
8288 && !vec_safe_is_empty (constructor_elements))
8290 if (!implicit)
8292 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8293 warning_init (0,
8294 "initialized field with side-effects overwritten");
8295 else if (warn_override_init)
8296 warning_init (OPT_Woverride_init, "initialized field overwritten");
8299 /* We can have just one union field set. */
8300 constructor_elements = NULL;
8303 /* Otherwise, output this element either to
8304 constructor_elements or to the assembler file. */
8306 constructor_elt celt = {field, value};
8307 vec_safe_push (constructor_elements, celt);
8309 /* Advance the variable that indicates sequential elements output. */
8310 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8311 constructor_unfilled_index
8312 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8313 bitsize_one_node);
8314 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8316 constructor_unfilled_fields
8317 = DECL_CHAIN (constructor_unfilled_fields);
8319 /* Skip any nameless bit fields. */
8320 while (constructor_unfilled_fields != 0
8321 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8322 && DECL_NAME (constructor_unfilled_fields) == 0)
8323 constructor_unfilled_fields =
8324 DECL_CHAIN (constructor_unfilled_fields);
8326 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8327 constructor_unfilled_fields = 0;
8329 /* Now output any pending elements which have become next. */
8330 if (pending)
8331 output_pending_init_elements (0, braced_init_obstack);
8334 /* Output any pending elements which have become next.
8335 As we output elements, constructor_unfilled_{fields,index}
8336 advances, which may cause other elements to become next;
8337 if so, they too are output.
8339 If ALL is 0, we return when there are
8340 no more pending elements to output now.
8342 If ALL is 1, we output space as necessary so that
8343 we can output all the pending elements. */
8344 static void
8345 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8347 struct init_node *elt = constructor_pending_elts;
8348 tree next;
8350 retry:
8352 /* Look through the whole pending tree.
8353 If we find an element that should be output now,
8354 output it. Otherwise, set NEXT to the element
8355 that comes first among those still pending. */
8357 next = 0;
8358 while (elt)
8360 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8362 if (tree_int_cst_equal (elt->purpose,
8363 constructor_unfilled_index))
8364 output_init_element (elt->value, elt->origtype, true,
8365 TREE_TYPE (constructor_type),
8366 constructor_unfilled_index, 0, false,
8367 braced_init_obstack);
8368 else if (tree_int_cst_lt (constructor_unfilled_index,
8369 elt->purpose))
8371 /* Advance to the next smaller node. */
8372 if (elt->left)
8373 elt = elt->left;
8374 else
8376 /* We have reached the smallest node bigger than the
8377 current unfilled index. Fill the space first. */
8378 next = elt->purpose;
8379 break;
8382 else
8384 /* Advance to the next bigger node. */
8385 if (elt->right)
8386 elt = elt->right;
8387 else
8389 /* We have reached the biggest node in a subtree. Find
8390 the parent of it, which is the next bigger node. */
8391 while (elt->parent && elt->parent->right == elt)
8392 elt = elt->parent;
8393 elt = elt->parent;
8394 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8395 elt->purpose))
8397 next = elt->purpose;
8398 break;
8403 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8404 || TREE_CODE (constructor_type) == UNION_TYPE)
8406 tree ctor_unfilled_bitpos, elt_bitpos;
8408 /* If the current record is complete we are done. */
8409 if (constructor_unfilled_fields == 0)
8410 break;
8412 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8413 elt_bitpos = bit_position (elt->purpose);
8414 /* We can't compare fields here because there might be empty
8415 fields in between. */
8416 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8418 constructor_unfilled_fields = elt->purpose;
8419 output_init_element (elt->value, elt->origtype, true,
8420 TREE_TYPE (elt->purpose),
8421 elt->purpose, 0, false,
8422 braced_init_obstack);
8424 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8426 /* Advance to the next smaller node. */
8427 if (elt->left)
8428 elt = elt->left;
8429 else
8431 /* We have reached the smallest node bigger than the
8432 current unfilled field. Fill the space first. */
8433 next = elt->purpose;
8434 break;
8437 else
8439 /* Advance to the next bigger node. */
8440 if (elt->right)
8441 elt = elt->right;
8442 else
8444 /* We have reached the biggest node in a subtree. Find
8445 the parent of it, which is the next bigger node. */
8446 while (elt->parent && elt->parent->right == elt)
8447 elt = elt->parent;
8448 elt = elt->parent;
8449 if (elt
8450 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8451 bit_position (elt->purpose))))
8453 next = elt->purpose;
8454 break;
8461 /* Ordinarily return, but not if we want to output all
8462 and there are elements left. */
8463 if (!(all && next != 0))
8464 return;
8466 /* If it's not incremental, just skip over the gap, so that after
8467 jumping to retry we will output the next successive element. */
8468 if (TREE_CODE (constructor_type) == RECORD_TYPE
8469 || TREE_CODE (constructor_type) == UNION_TYPE)
8470 constructor_unfilled_fields = next;
8471 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8472 constructor_unfilled_index = next;
8474 /* ELT now points to the node in the pending tree with the next
8475 initializer to output. */
8476 goto retry;
8479 /* Add one non-braced element to the current constructor level.
8480 This adjusts the current position within the constructor's type.
8481 This may also start or terminate implicit levels
8482 to handle a partly-braced initializer.
8484 Once this has found the correct level for the new element,
8485 it calls output_init_element.
8487 IMPLICIT is true if value comes from pop_init_level (1),
8488 the new initializer has been merged with the existing one
8489 and thus no warnings should be emitted about overriding an
8490 existing initializer. */
8492 void
8493 process_init_element (struct c_expr value, bool implicit,
8494 struct obstack * braced_init_obstack)
8496 tree orig_value = value.value;
8497 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8498 bool strict_string = value.original_code == STRING_CST;
8500 designator_depth = 0;
8501 designator_erroneous = 0;
8503 /* Handle superfluous braces around string cst as in
8504 char x[] = {"foo"}; */
8505 if (string_flag
8506 && constructor_type
8507 && TREE_CODE (constructor_type) == ARRAY_TYPE
8508 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8509 && integer_zerop (constructor_unfilled_index))
8511 if (constructor_stack->replacement_value.value)
8512 error_init ("excess elements in char array initializer");
8513 constructor_stack->replacement_value = value;
8514 return;
8517 if (constructor_stack->replacement_value.value != 0)
8519 error_init ("excess elements in struct initializer");
8520 return;
8523 /* Ignore elements of a brace group if it is entirely superfluous
8524 and has already been diagnosed. */
8525 if (constructor_type == 0)
8526 return;
8528 /* If we've exhausted any levels that didn't have braces,
8529 pop them now. */
8530 while (constructor_stack->implicit)
8532 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8533 || TREE_CODE (constructor_type) == UNION_TYPE)
8534 && constructor_fields == 0)
8535 process_init_element (pop_init_level (1, braced_init_obstack),
8536 true, braced_init_obstack);
8537 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8538 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8539 && constructor_max_index
8540 && tree_int_cst_lt (constructor_max_index,
8541 constructor_index))
8542 process_init_element (pop_init_level (1, braced_init_obstack),
8543 true, braced_init_obstack);
8544 else
8545 break;
8548 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8549 if (constructor_range_stack)
8551 /* If value is a compound literal and we'll be just using its
8552 content, don't put it into a SAVE_EXPR. */
8553 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8554 || !require_constant_value
8555 || flag_isoc99)
8557 tree semantic_type = NULL_TREE;
8558 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8560 semantic_type = TREE_TYPE (value.value);
8561 value.value = TREE_OPERAND (value.value, 0);
8563 value.value = c_save_expr (value.value);
8564 if (semantic_type)
8565 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8566 value.value);
8570 while (1)
8572 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8574 tree fieldtype;
8575 enum tree_code fieldcode;
8577 if (constructor_fields == 0)
8579 pedwarn_init (input_location, 0,
8580 "excess elements in struct initializer");
8581 break;
8584 fieldtype = TREE_TYPE (constructor_fields);
8585 if (fieldtype != error_mark_node)
8586 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8587 fieldcode = TREE_CODE (fieldtype);
8589 /* Error for non-static initialization of a flexible array member. */
8590 if (fieldcode == ARRAY_TYPE
8591 && !require_constant_value
8592 && TYPE_SIZE (fieldtype) == NULL_TREE
8593 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8595 error_init ("non-static initialization of a flexible array member");
8596 break;
8599 /* Accept a string constant to initialize a subarray. */
8600 if (value.value != 0
8601 && fieldcode == ARRAY_TYPE
8602 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8603 && string_flag)
8604 value.value = orig_value;
8605 /* Otherwise, if we have come to a subaggregate,
8606 and we don't have an element of its type, push into it. */
8607 else if (value.value != 0
8608 && value.value != error_mark_node
8609 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8610 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8611 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8613 push_init_level (1, braced_init_obstack);
8614 continue;
8617 if (value.value)
8619 push_member_name (constructor_fields);
8620 output_init_element (value.value, value.original_type,
8621 strict_string, fieldtype,
8622 constructor_fields, 1, implicit,
8623 braced_init_obstack);
8624 RESTORE_SPELLING_DEPTH (constructor_depth);
8626 else
8627 /* Do the bookkeeping for an element that was
8628 directly output as a constructor. */
8630 /* For a record, keep track of end position of last field. */
8631 if (DECL_SIZE (constructor_fields))
8632 constructor_bit_index
8633 = size_binop_loc (input_location, PLUS_EXPR,
8634 bit_position (constructor_fields),
8635 DECL_SIZE (constructor_fields));
8637 /* If the current field was the first one not yet written out,
8638 it isn't now, so update. */
8639 if (constructor_unfilled_fields == constructor_fields)
8641 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8642 /* Skip any nameless bit fields. */
8643 while (constructor_unfilled_fields != 0
8644 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8645 && DECL_NAME (constructor_unfilled_fields) == 0)
8646 constructor_unfilled_fields =
8647 DECL_CHAIN (constructor_unfilled_fields);
8651 constructor_fields = DECL_CHAIN (constructor_fields);
8652 /* Skip any nameless bit fields at the beginning. */
8653 while (constructor_fields != 0
8654 && DECL_C_BIT_FIELD (constructor_fields)
8655 && DECL_NAME (constructor_fields) == 0)
8656 constructor_fields = DECL_CHAIN (constructor_fields);
8658 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8660 tree fieldtype;
8661 enum tree_code fieldcode;
8663 if (constructor_fields == 0)
8665 pedwarn_init (input_location, 0,
8666 "excess elements in union initializer");
8667 break;
8670 fieldtype = TREE_TYPE (constructor_fields);
8671 if (fieldtype != error_mark_node)
8672 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8673 fieldcode = TREE_CODE (fieldtype);
8675 /* Warn that traditional C rejects initialization of unions.
8676 We skip the warning if the value is zero. This is done
8677 under the assumption that the zero initializer in user
8678 code appears conditioned on e.g. __STDC__ to avoid
8679 "missing initializer" warnings and relies on default
8680 initialization to zero in the traditional C case.
8681 We also skip the warning if the initializer is designated,
8682 again on the assumption that this must be conditional on
8683 __STDC__ anyway (and we've already complained about the
8684 member-designator already). */
8685 if (!in_system_header && !constructor_designated
8686 && !(value.value && (integer_zerop (value.value)
8687 || real_zerop (value.value))))
8688 warning (OPT_Wtraditional, "traditional C rejects initialization "
8689 "of unions");
8691 /* Accept a string constant to initialize a subarray. */
8692 if (value.value != 0
8693 && fieldcode == ARRAY_TYPE
8694 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8695 && string_flag)
8696 value.value = orig_value;
8697 /* Otherwise, if we have come to a subaggregate,
8698 and we don't have an element of its type, push into it. */
8699 else if (value.value != 0
8700 && value.value != error_mark_node
8701 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8702 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8703 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8705 push_init_level (1, braced_init_obstack);
8706 continue;
8709 if (value.value)
8711 push_member_name (constructor_fields);
8712 output_init_element (value.value, value.original_type,
8713 strict_string, fieldtype,
8714 constructor_fields, 1, implicit,
8715 braced_init_obstack);
8716 RESTORE_SPELLING_DEPTH (constructor_depth);
8718 else
8719 /* Do the bookkeeping for an element that was
8720 directly output as a constructor. */
8722 constructor_bit_index = DECL_SIZE (constructor_fields);
8723 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8726 constructor_fields = 0;
8728 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8730 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8731 enum tree_code eltcode = TREE_CODE (elttype);
8733 /* Accept a string constant to initialize a subarray. */
8734 if (value.value != 0
8735 && eltcode == ARRAY_TYPE
8736 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8737 && string_flag)
8738 value.value = orig_value;
8739 /* Otherwise, if we have come to a subaggregate,
8740 and we don't have an element of its type, push into it. */
8741 else if (value.value != 0
8742 && value.value != error_mark_node
8743 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8744 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8745 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8747 push_init_level (1, braced_init_obstack);
8748 continue;
8751 if (constructor_max_index != 0
8752 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8753 || integer_all_onesp (constructor_max_index)))
8755 pedwarn_init (input_location, 0,
8756 "excess elements in array initializer");
8757 break;
8760 /* Now output the actual element. */
8761 if (value.value)
8763 push_array_bounds (tree_low_cst (constructor_index, 1));
8764 output_init_element (value.value, value.original_type,
8765 strict_string, elttype,
8766 constructor_index, 1, implicit,
8767 braced_init_obstack);
8768 RESTORE_SPELLING_DEPTH (constructor_depth);
8771 constructor_index
8772 = size_binop_loc (input_location, PLUS_EXPR,
8773 constructor_index, bitsize_one_node);
8775 if (!value.value)
8776 /* If we are doing the bookkeeping for an element that was
8777 directly output as a constructor, we must update
8778 constructor_unfilled_index. */
8779 constructor_unfilled_index = constructor_index;
8781 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8783 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8785 /* Do a basic check of initializer size. Note that vectors
8786 always have a fixed size derived from their type. */
8787 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8789 pedwarn_init (input_location, 0,
8790 "excess elements in vector initializer");
8791 break;
8794 /* Now output the actual element. */
8795 if (value.value)
8797 if (TREE_CODE (value.value) == VECTOR_CST)
8798 elttype = TYPE_MAIN_VARIANT (constructor_type);
8799 output_init_element (value.value, value.original_type,
8800 strict_string, elttype,
8801 constructor_index, 1, implicit,
8802 braced_init_obstack);
8805 constructor_index
8806 = size_binop_loc (input_location,
8807 PLUS_EXPR, constructor_index, bitsize_one_node);
8809 if (!value.value)
8810 /* If we are doing the bookkeeping for an element that was
8811 directly output as a constructor, we must update
8812 constructor_unfilled_index. */
8813 constructor_unfilled_index = constructor_index;
8816 /* Handle the sole element allowed in a braced initializer
8817 for a scalar variable. */
8818 else if (constructor_type != error_mark_node
8819 && constructor_fields == 0)
8821 pedwarn_init (input_location, 0,
8822 "excess elements in scalar initializer");
8823 break;
8825 else
8827 if (value.value)
8828 output_init_element (value.value, value.original_type,
8829 strict_string, constructor_type,
8830 NULL_TREE, 1, implicit,
8831 braced_init_obstack);
8832 constructor_fields = 0;
8835 /* Handle range initializers either at this level or anywhere higher
8836 in the designator stack. */
8837 if (constructor_range_stack)
8839 struct constructor_range_stack *p, *range_stack;
8840 int finish = 0;
8842 range_stack = constructor_range_stack;
8843 constructor_range_stack = 0;
8844 while (constructor_stack != range_stack->stack)
8846 gcc_assert (constructor_stack->implicit);
8847 process_init_element (pop_init_level (1,
8848 braced_init_obstack),
8849 true, braced_init_obstack);
8851 for (p = range_stack;
8852 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8853 p = p->prev)
8855 gcc_assert (constructor_stack->implicit);
8856 process_init_element (pop_init_level (1, braced_init_obstack),
8857 true, braced_init_obstack);
8860 p->index = size_binop_loc (input_location,
8861 PLUS_EXPR, p->index, bitsize_one_node);
8862 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8863 finish = 1;
8865 while (1)
8867 constructor_index = p->index;
8868 constructor_fields = p->fields;
8869 if (finish && p->range_end && p->index == p->range_start)
8871 finish = 0;
8872 p->prev = 0;
8874 p = p->next;
8875 if (!p)
8876 break;
8877 push_init_level (2, braced_init_obstack);
8878 p->stack = constructor_stack;
8879 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8880 p->index = p->range_start;
8883 if (!finish)
8884 constructor_range_stack = range_stack;
8885 continue;
8888 break;
8891 constructor_range_stack = 0;
8894 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8895 (guaranteed to be 'volatile' or null) and ARGS (represented using
8896 an ASM_EXPR node). */
8897 tree
8898 build_asm_stmt (tree cv_qualifier, tree args)
8900 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8901 ASM_VOLATILE_P (args) = 1;
8902 return add_stmt (args);
8905 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8906 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8907 SIMPLE indicates whether there was anything at all after the
8908 string in the asm expression -- asm("blah") and asm("blah" : )
8909 are subtly different. We use a ASM_EXPR node to represent this. */
8910 tree
8911 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8912 tree clobbers, tree labels, bool simple)
8914 tree tail;
8915 tree args;
8916 int i;
8917 const char *constraint;
8918 const char **oconstraints;
8919 bool allows_mem, allows_reg, is_inout;
8920 int ninputs, noutputs;
8922 ninputs = list_length (inputs);
8923 noutputs = list_length (outputs);
8924 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8926 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8928 /* Remove output conversions that change the type but not the mode. */
8929 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8931 tree output = TREE_VALUE (tail);
8933 output = c_fully_fold (output, false, NULL);
8935 /* ??? Really, this should not be here. Users should be using a
8936 proper lvalue, dammit. But there's a long history of using casts
8937 in the output operands. In cases like longlong.h, this becomes a
8938 primitive form of typechecking -- if the cast can be removed, then
8939 the output operand had a type of the proper width; otherwise we'll
8940 get an error. Gross, but ... */
8941 STRIP_NOPS (output);
8943 if (!lvalue_or_else (loc, output, lv_asm))
8944 output = error_mark_node;
8946 if (output != error_mark_node
8947 && (TREE_READONLY (output)
8948 || TYPE_READONLY (TREE_TYPE (output))
8949 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8950 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8951 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8952 readonly_error (loc, output, lv_asm);
8954 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8955 oconstraints[i] = constraint;
8957 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8958 &allows_mem, &allows_reg, &is_inout))
8960 /* If the operand is going to end up in memory,
8961 mark it addressable. */
8962 if (!allows_reg && !c_mark_addressable (output))
8963 output = error_mark_node;
8964 if (!(!allows_reg && allows_mem)
8965 && output != error_mark_node
8966 && VOID_TYPE_P (TREE_TYPE (output)))
8968 error_at (loc, "invalid use of void expression");
8969 output = error_mark_node;
8972 else
8973 output = error_mark_node;
8975 TREE_VALUE (tail) = output;
8978 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8980 tree input;
8982 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8983 input = TREE_VALUE (tail);
8985 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8986 oconstraints, &allows_mem, &allows_reg))
8988 /* If the operand is going to end up in memory,
8989 mark it addressable. */
8990 if (!allows_reg && allows_mem)
8992 input = c_fully_fold (input, false, NULL);
8994 /* Strip the nops as we allow this case. FIXME, this really
8995 should be rejected or made deprecated. */
8996 STRIP_NOPS (input);
8997 if (!c_mark_addressable (input))
8998 input = error_mark_node;
9000 else
9002 struct c_expr expr;
9003 memset (&expr, 0, sizeof (expr));
9004 expr.value = input;
9005 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9006 input = c_fully_fold (expr.value, false, NULL);
9008 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9010 error_at (loc, "invalid use of void expression");
9011 input = error_mark_node;
9015 else
9016 input = error_mark_node;
9018 TREE_VALUE (tail) = input;
9021 /* ASMs with labels cannot have outputs. This should have been
9022 enforced by the parser. */
9023 gcc_assert (outputs == NULL || labels == NULL);
9025 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9027 /* asm statements without outputs, including simple ones, are treated
9028 as volatile. */
9029 ASM_INPUT_P (args) = simple;
9030 ASM_VOLATILE_P (args) = (noutputs == 0);
9032 return args;
9035 /* Generate a goto statement to LABEL. LOC is the location of the
9036 GOTO. */
9038 tree
9039 c_finish_goto_label (location_t loc, tree label)
9041 tree decl = lookup_label_for_goto (loc, label);
9042 if (!decl)
9043 return NULL_TREE;
9044 TREE_USED (decl) = 1;
9046 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9047 SET_EXPR_LOCATION (t, loc);
9048 return add_stmt (t);
9052 /* Generate a computed goto statement to EXPR. LOC is the location of
9053 the GOTO. */
9055 tree
9056 c_finish_goto_ptr (location_t loc, tree expr)
9058 tree t;
9059 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9060 expr = c_fully_fold (expr, false, NULL);
9061 expr = convert (ptr_type_node, expr);
9062 t = build1 (GOTO_EXPR, void_type_node, expr);
9063 SET_EXPR_LOCATION (t, loc);
9064 return add_stmt (t);
9067 /* Generate a C `return' statement. RETVAL is the expression for what
9068 to return, or a null pointer for `return;' with no value. LOC is
9069 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
9070 is the original type of RETVAL. */
9072 tree
9073 c_finish_return (location_t loc, tree retval, tree origtype)
9075 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9076 bool no_warning = false;
9077 bool npc = false;
9078 size_t rank = 0;
9080 if (TREE_THIS_VOLATILE (current_function_decl))
9081 warning_at (loc, 0,
9082 "function declared %<noreturn%> has a %<return%> statement");
9084 if (flag_enable_cilkplus && contains_array_notation_expr (retval))
9086 /* Array notations are allowed in a return statement if it is inside a
9087 built-in array notation reduction function. */
9088 if (!find_rank (loc, retval, retval, false, &rank))
9089 return error_mark_node;
9090 if (rank >= 1)
9092 error_at (loc, "array notation expression cannot be used as a "
9093 "return value");
9094 return error_mark_node;
9097 if (flag_enable_cilkplus && retval && TREE_CODE (retval) == CILK_SPAWN_STMT)
9099 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9100 "allowed");
9101 return error_mark_node;
9103 if (retval)
9105 tree semantic_type = NULL_TREE;
9106 npc = null_pointer_constant_p (retval);
9107 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9109 semantic_type = TREE_TYPE (retval);
9110 retval = TREE_OPERAND (retval, 0);
9112 retval = c_fully_fold (retval, false, NULL);
9113 if (semantic_type)
9114 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9117 if (!retval)
9119 current_function_returns_null = 1;
9120 if ((warn_return_type || flag_isoc99)
9121 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9123 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
9124 "%<return%> with no value, in "
9125 "function returning non-void");
9126 no_warning = true;
9129 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9131 current_function_returns_null = 1;
9132 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9133 pedwarn (loc, 0,
9134 "%<return%> with a value, in function returning void");
9135 else
9136 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9137 "%<return%> with expression, in function returning void");
9139 else
9141 tree t = convert_for_assignment (loc, valtype, retval, origtype,
9142 ic_return,
9143 npc, NULL_TREE, NULL_TREE, 0);
9144 tree res = DECL_RESULT (current_function_decl);
9145 tree inner;
9146 bool save;
9148 current_function_returns_value = 1;
9149 if (t == error_mark_node)
9150 return NULL_TREE;
9152 save = in_late_binary_op;
9153 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9154 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9155 in_late_binary_op = true;
9156 inner = t = convert (TREE_TYPE (res), t);
9157 in_late_binary_op = save;
9159 /* Strip any conversions, additions, and subtractions, and see if
9160 we are returning the address of a local variable. Warn if so. */
9161 while (1)
9163 switch (TREE_CODE (inner))
9165 CASE_CONVERT:
9166 case NON_LVALUE_EXPR:
9167 case PLUS_EXPR:
9168 case POINTER_PLUS_EXPR:
9169 inner = TREE_OPERAND (inner, 0);
9170 continue;
9172 case MINUS_EXPR:
9173 /* If the second operand of the MINUS_EXPR has a pointer
9174 type (or is converted from it), this may be valid, so
9175 don't give a warning. */
9177 tree op1 = TREE_OPERAND (inner, 1);
9179 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9180 && (CONVERT_EXPR_P (op1)
9181 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9182 op1 = TREE_OPERAND (op1, 0);
9184 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9185 break;
9187 inner = TREE_OPERAND (inner, 0);
9188 continue;
9191 case ADDR_EXPR:
9192 inner = TREE_OPERAND (inner, 0);
9194 while (REFERENCE_CLASS_P (inner)
9195 && TREE_CODE (inner) != INDIRECT_REF)
9196 inner = TREE_OPERAND (inner, 0);
9198 if (DECL_P (inner)
9199 && !DECL_EXTERNAL (inner)
9200 && !TREE_STATIC (inner)
9201 && DECL_CONTEXT (inner) == current_function_decl)
9202 warning_at (loc,
9203 OPT_Wreturn_local_addr, "function returns address "
9204 "of local variable");
9205 break;
9207 default:
9208 break;
9211 break;
9214 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9215 SET_EXPR_LOCATION (retval, loc);
9217 if (warn_sequence_point)
9218 verify_sequence_points (retval);
9221 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9222 TREE_NO_WARNING (ret_stmt) |= no_warning;
9223 return add_stmt (ret_stmt);
9226 struct c_switch {
9227 /* The SWITCH_EXPR being built. */
9228 tree switch_expr;
9230 /* The original type of the testing expression, i.e. before the
9231 default conversion is applied. */
9232 tree orig_type;
9234 /* A splay-tree mapping the low element of a case range to the high
9235 element, or NULL_TREE if there is no high element. Used to
9236 determine whether or not a new case label duplicates an old case
9237 label. We need a tree, rather than simply a hash table, because
9238 of the GNU case range extension. */
9239 splay_tree cases;
9241 /* The bindings at the point of the switch. This is used for
9242 warnings crossing decls when branching to a case label. */
9243 struct c_spot_bindings *bindings;
9245 /* The next node on the stack. */
9246 struct c_switch *next;
9249 /* A stack of the currently active switch statements. The innermost
9250 switch statement is on the top of the stack. There is no need to
9251 mark the stack for garbage collection because it is only active
9252 during the processing of the body of a function, and we never
9253 collect at that point. */
9255 struct c_switch *c_switch_stack;
9257 /* Start a C switch statement, testing expression EXP. Return the new
9258 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9259 SWITCH_COND_LOC is the location of the switch's condition. */
9261 tree
9262 c_start_case (location_t switch_loc,
9263 location_t switch_cond_loc,
9264 tree exp)
9266 tree orig_type = error_mark_node;
9267 struct c_switch *cs;
9269 if (exp != error_mark_node)
9271 orig_type = TREE_TYPE (exp);
9273 if (!INTEGRAL_TYPE_P (orig_type))
9275 if (orig_type != error_mark_node)
9277 error_at (switch_cond_loc, "switch quantity not an integer");
9278 orig_type = error_mark_node;
9280 exp = integer_zero_node;
9282 else
9284 tree type = TYPE_MAIN_VARIANT (orig_type);
9286 if (!in_system_header
9287 && (type == long_integer_type_node
9288 || type == long_unsigned_type_node))
9289 warning_at (switch_cond_loc,
9290 OPT_Wtraditional, "%<long%> switch expression not "
9291 "converted to %<int%> in ISO C");
9293 exp = c_fully_fold (exp, false, NULL);
9294 exp = default_conversion (exp);
9296 if (warn_sequence_point)
9297 verify_sequence_points (exp);
9301 /* Add this new SWITCH_EXPR to the stack. */
9302 cs = XNEW (struct c_switch);
9303 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9304 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9305 cs->orig_type = orig_type;
9306 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9307 cs->bindings = c_get_switch_bindings ();
9308 cs->next = c_switch_stack;
9309 c_switch_stack = cs;
9311 return add_stmt (cs->switch_expr);
9314 /* Process a case label at location LOC. */
9316 tree
9317 do_case (location_t loc, tree low_value, tree high_value)
9319 tree label = NULL_TREE;
9321 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9323 low_value = c_fully_fold (low_value, false, NULL);
9324 if (TREE_CODE (low_value) == INTEGER_CST)
9325 pedwarn (input_location, OPT_Wpedantic,
9326 "case label is not an integer constant expression");
9329 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9331 high_value = c_fully_fold (high_value, false, NULL);
9332 if (TREE_CODE (high_value) == INTEGER_CST)
9333 pedwarn (input_location, OPT_Wpedantic,
9334 "case label is not an integer constant expression");
9337 if (c_switch_stack == NULL)
9339 if (low_value)
9340 error_at (loc, "case label not within a switch statement");
9341 else
9342 error_at (loc, "%<default%> label not within a switch statement");
9343 return NULL_TREE;
9346 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9347 EXPR_LOCATION (c_switch_stack->switch_expr),
9348 loc))
9349 return NULL_TREE;
9351 label = c_add_case_label (loc, c_switch_stack->cases,
9352 SWITCH_COND (c_switch_stack->switch_expr),
9353 c_switch_stack->orig_type,
9354 low_value, high_value);
9355 if (label == error_mark_node)
9356 label = NULL_TREE;
9357 return label;
9360 /* Finish the switch statement. */
9362 void
9363 c_finish_case (tree body)
9365 struct c_switch *cs = c_switch_stack;
9366 location_t switch_location;
9368 SWITCH_BODY (cs->switch_expr) = body;
9370 /* Emit warnings as needed. */
9371 switch_location = EXPR_LOCATION (cs->switch_expr);
9372 c_do_switch_warnings (cs->cases, switch_location,
9373 TREE_TYPE (cs->switch_expr),
9374 SWITCH_COND (cs->switch_expr));
9376 /* Pop the stack. */
9377 c_switch_stack = cs->next;
9378 splay_tree_delete (cs->cases);
9379 c_release_switch_bindings (cs->bindings);
9380 XDELETE (cs);
9383 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9384 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9385 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9386 statement, and was not surrounded with parenthesis. */
9388 void
9389 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9390 tree else_block, bool nested_if)
9392 tree stmt;
9394 /* If the condition has array notations, then the rank of the then_block and
9395 else_block must be either 0 or be equal to the rank of the condition. If
9396 the condition does not have array notations then break them up as it is
9397 broken up in a normal expression. */
9398 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
9400 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9401 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9402 return;
9403 if (then_block
9404 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9405 return;
9406 if (else_block
9407 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9408 return;
9409 if (cond_rank != then_rank && then_rank != 0)
9411 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9412 " and the then-block");
9413 return;
9415 else if (cond_rank != else_rank && else_rank != 0)
9417 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9418 " and the else-block");
9419 return;
9422 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9423 if (warn_parentheses && nested_if && else_block == NULL)
9425 tree inner_if = then_block;
9427 /* We know from the grammar productions that there is an IF nested
9428 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9429 it might not be exactly THEN_BLOCK, but should be the last
9430 non-container statement within. */
9431 while (1)
9432 switch (TREE_CODE (inner_if))
9434 case COND_EXPR:
9435 goto found;
9436 case BIND_EXPR:
9437 inner_if = BIND_EXPR_BODY (inner_if);
9438 break;
9439 case STATEMENT_LIST:
9440 inner_if = expr_last (then_block);
9441 break;
9442 case TRY_FINALLY_EXPR:
9443 case TRY_CATCH_EXPR:
9444 inner_if = TREE_OPERAND (inner_if, 0);
9445 break;
9446 default:
9447 gcc_unreachable ();
9449 found:
9451 if (COND_EXPR_ELSE (inner_if))
9452 warning_at (if_locus, OPT_Wparentheses,
9453 "suggest explicit braces to avoid ambiguous %<else%>");
9456 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9457 SET_EXPR_LOCATION (stmt, if_locus);
9458 add_stmt (stmt);
9461 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9462 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9463 is false for DO loops. INCR is the FOR increment expression. BODY is
9464 the statement controlled by the loop. BLAB is the break label. CLAB is
9465 the continue label. Everything is allowed to be NULL. */
9467 void
9468 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9469 tree blab, tree clab, bool cond_is_first)
9471 tree entry = NULL, exit = NULL, t;
9473 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
9475 error_at (start_locus, "array notation expression cannot be used in a "
9476 "loop%'s condition");
9477 return;
9480 /* If the condition is zero don't generate a loop construct. */
9481 if (cond && integer_zerop (cond))
9483 if (cond_is_first)
9485 t = build_and_jump (&blab);
9486 SET_EXPR_LOCATION (t, start_locus);
9487 add_stmt (t);
9490 else
9492 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9494 /* If we have an exit condition, then we build an IF with gotos either
9495 out of the loop, or to the top of it. If there's no exit condition,
9496 then we just build a jump back to the top. */
9497 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9499 if (cond && !integer_nonzerop (cond))
9501 /* Canonicalize the loop condition to the end. This means
9502 generating a branch to the loop condition. Reuse the
9503 continue label, if possible. */
9504 if (cond_is_first)
9506 if (incr || !clab)
9508 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9509 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9511 else
9512 t = build1 (GOTO_EXPR, void_type_node, clab);
9513 SET_EXPR_LOCATION (t, start_locus);
9514 add_stmt (t);
9517 t = build_and_jump (&blab);
9518 if (cond_is_first)
9519 exit = fold_build3_loc (start_locus,
9520 COND_EXPR, void_type_node, cond, exit, t);
9521 else
9522 exit = fold_build3_loc (input_location,
9523 COND_EXPR, void_type_node, cond, exit, t);
9526 add_stmt (top);
9529 if (body)
9530 add_stmt (body);
9531 if (clab)
9532 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9533 if (incr)
9534 add_stmt (incr);
9535 if (entry)
9536 add_stmt (entry);
9537 if (exit)
9538 add_stmt (exit);
9539 if (blab)
9540 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9543 tree
9544 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9546 bool skip;
9547 tree label = *label_p;
9549 /* In switch statements break is sometimes stylistically used after
9550 a return statement. This can lead to spurious warnings about
9551 control reaching the end of a non-void function when it is
9552 inlined. Note that we are calling block_may_fallthru with
9553 language specific tree nodes; this works because
9554 block_may_fallthru returns true when given something it does not
9555 understand. */
9556 skip = !block_may_fallthru (cur_stmt_list);
9558 if (!label)
9560 if (!skip)
9561 *label_p = label = create_artificial_label (loc);
9563 else if (TREE_CODE (label) == LABEL_DECL)
9565 else switch (TREE_INT_CST_LOW (label))
9567 case 0:
9568 if (is_break)
9569 error_at (loc, "break statement not within loop or switch");
9570 else
9571 error_at (loc, "continue statement not within a loop");
9572 return NULL_TREE;
9574 case 1:
9575 gcc_assert (is_break);
9576 error_at (loc, "break statement used with OpenMP for loop");
9577 return NULL_TREE;
9579 case 2:
9580 if (is_break)
9581 error ("break statement within %<#pragma simd%> loop body");
9582 else
9583 error ("continue statement within %<#pragma simd%> loop body");
9584 return NULL_TREE;
9586 default:
9587 gcc_unreachable ();
9590 if (skip)
9591 return NULL_TREE;
9593 if (!is_break)
9594 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9596 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9599 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9601 static void
9602 emit_side_effect_warnings (location_t loc, tree expr)
9604 if (expr == error_mark_node)
9606 else if (!TREE_SIDE_EFFECTS (expr))
9608 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9609 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9611 else
9612 warn_if_unused_value (expr, loc);
9615 /* Process an expression as if it were a complete statement. Emit
9616 diagnostics, but do not call ADD_STMT. LOC is the location of the
9617 statement. */
9619 tree
9620 c_process_expr_stmt (location_t loc, tree expr)
9622 tree exprv;
9624 if (!expr)
9625 return NULL_TREE;
9627 expr = c_fully_fold (expr, false, NULL);
9629 if (warn_sequence_point)
9630 verify_sequence_points (expr);
9632 if (TREE_TYPE (expr) != error_mark_node
9633 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9634 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9635 error_at (loc, "expression statement has incomplete type");
9637 /* If we're not processing a statement expression, warn about unused values.
9638 Warnings for statement expressions will be emitted later, once we figure
9639 out which is the result. */
9640 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9641 && warn_unused_value)
9642 emit_side_effect_warnings (loc, expr);
9644 exprv = expr;
9645 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9646 exprv = TREE_OPERAND (exprv, 1);
9647 while (CONVERT_EXPR_P (exprv))
9648 exprv = TREE_OPERAND (exprv, 0);
9649 if (DECL_P (exprv)
9650 || handled_component_p (exprv)
9651 || TREE_CODE (exprv) == ADDR_EXPR)
9652 mark_exp_read (exprv);
9654 /* If the expression is not of a type to which we cannot assign a line
9655 number, wrap the thing in a no-op NOP_EXPR. */
9656 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9658 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9659 SET_EXPR_LOCATION (expr, loc);
9662 return expr;
9665 /* Emit an expression as a statement. LOC is the location of the
9666 expression. */
9668 tree
9669 c_finish_expr_stmt (location_t loc, tree expr)
9671 if (expr)
9672 return add_stmt (c_process_expr_stmt (loc, expr));
9673 else
9674 return NULL;
9677 /* Do the opposite and emit a statement as an expression. To begin,
9678 create a new binding level and return it. */
9680 tree
9681 c_begin_stmt_expr (void)
9683 tree ret;
9685 /* We must force a BLOCK for this level so that, if it is not expanded
9686 later, there is a way to turn off the entire subtree of blocks that
9687 are contained in it. */
9688 keep_next_level ();
9689 ret = c_begin_compound_stmt (true);
9691 c_bindings_start_stmt_expr (c_switch_stack == NULL
9692 ? NULL
9693 : c_switch_stack->bindings);
9695 /* Mark the current statement list as belonging to a statement list. */
9696 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9698 return ret;
9701 /* LOC is the location of the compound statement to which this body
9702 belongs. */
9704 tree
9705 c_finish_stmt_expr (location_t loc, tree body)
9707 tree last, type, tmp, val;
9708 tree *last_p;
9710 body = c_end_compound_stmt (loc, body, true);
9712 c_bindings_end_stmt_expr (c_switch_stack == NULL
9713 ? NULL
9714 : c_switch_stack->bindings);
9716 /* Locate the last statement in BODY. See c_end_compound_stmt
9717 about always returning a BIND_EXPR. */
9718 last_p = &BIND_EXPR_BODY (body);
9719 last = BIND_EXPR_BODY (body);
9721 continue_searching:
9722 if (TREE_CODE (last) == STATEMENT_LIST)
9724 tree_stmt_iterator i;
9726 /* This can happen with degenerate cases like ({ }). No value. */
9727 if (!TREE_SIDE_EFFECTS (last))
9728 return body;
9730 /* If we're supposed to generate side effects warnings, process
9731 all of the statements except the last. */
9732 if (warn_unused_value)
9734 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9736 location_t tloc;
9737 tree t = tsi_stmt (i);
9739 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9740 emit_side_effect_warnings (tloc, t);
9743 else
9744 i = tsi_last (last);
9745 last_p = tsi_stmt_ptr (i);
9746 last = *last_p;
9749 /* If the end of the list is exception related, then the list was split
9750 by a call to push_cleanup. Continue searching. */
9751 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9752 || TREE_CODE (last) == TRY_CATCH_EXPR)
9754 last_p = &TREE_OPERAND (last, 0);
9755 last = *last_p;
9756 goto continue_searching;
9759 if (last == error_mark_node)
9760 return last;
9762 /* In the case that the BIND_EXPR is not necessary, return the
9763 expression out from inside it. */
9764 if (last == BIND_EXPR_BODY (body)
9765 && BIND_EXPR_VARS (body) == NULL)
9767 /* Even if this looks constant, do not allow it in a constant
9768 expression. */
9769 last = c_wrap_maybe_const (last, true);
9770 /* Do not warn if the return value of a statement expression is
9771 unused. */
9772 TREE_NO_WARNING (last) = 1;
9773 return last;
9776 /* Extract the type of said expression. */
9777 type = TREE_TYPE (last);
9779 /* If we're not returning a value at all, then the BIND_EXPR that
9780 we already have is a fine expression to return. */
9781 if (!type || VOID_TYPE_P (type))
9782 return body;
9784 /* Now that we've located the expression containing the value, it seems
9785 silly to make voidify_wrapper_expr repeat the process. Create a
9786 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9787 tmp = create_tmp_var_raw (type, NULL);
9789 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9790 tree_expr_nonnegative_p giving up immediately. */
9791 val = last;
9792 if (TREE_CODE (val) == NOP_EXPR
9793 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9794 val = TREE_OPERAND (val, 0);
9796 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9797 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9800 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9801 SET_EXPR_LOCATION (t, loc);
9802 return t;
9806 /* Begin and end compound statements. This is as simple as pushing
9807 and popping new statement lists from the tree. */
9809 tree
9810 c_begin_compound_stmt (bool do_scope)
9812 tree stmt = push_stmt_list ();
9813 if (do_scope)
9814 push_scope ();
9815 return stmt;
9818 /* End a compound statement. STMT is the statement. LOC is the
9819 location of the compound statement-- this is usually the location
9820 of the opening brace. */
9822 tree
9823 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9825 tree block = NULL;
9827 if (do_scope)
9829 if (c_dialect_objc ())
9830 objc_clear_super_receiver ();
9831 block = pop_scope ();
9834 stmt = pop_stmt_list (stmt);
9835 stmt = c_build_bind_expr (loc, block, stmt);
9837 /* If this compound statement is nested immediately inside a statement
9838 expression, then force a BIND_EXPR to be created. Otherwise we'll
9839 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9840 STATEMENT_LISTs merge, and thus we can lose track of what statement
9841 was really last. */
9842 if (building_stmt_list_p ()
9843 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9844 && TREE_CODE (stmt) != BIND_EXPR)
9846 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9847 TREE_SIDE_EFFECTS (stmt) = 1;
9848 SET_EXPR_LOCATION (stmt, loc);
9851 return stmt;
9854 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9855 when the current scope is exited. EH_ONLY is true when this is not
9856 meant to apply to normal control flow transfer. */
9858 void
9859 push_cleanup (tree decl, tree cleanup, bool eh_only)
9861 enum tree_code code;
9862 tree stmt, list;
9863 bool stmt_expr;
9865 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9866 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9867 add_stmt (stmt);
9868 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9869 list = push_stmt_list ();
9870 TREE_OPERAND (stmt, 0) = list;
9871 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9874 /* Build a binary-operation expression without default conversions.
9875 CODE is the kind of expression to build.
9876 LOCATION is the operator's location.
9877 This function differs from `build' in several ways:
9878 the data type of the result is computed and recorded in it,
9879 warnings are generated if arg data types are invalid,
9880 special handling for addition and subtraction of pointers is known,
9881 and some optimization is done (operations on narrow ints
9882 are done in the narrower type when that gives the same result).
9883 Constant folding is also done before the result is returned.
9885 Note that the operands will never have enumeral types, or function
9886 or array types, because either they will have the default conversions
9887 performed or they have both just been converted to some other type in which
9888 the arithmetic is to be done. */
9890 tree
9891 build_binary_op (location_t location, enum tree_code code,
9892 tree orig_op0, tree orig_op1, int convert_p)
9894 tree type0, type1, orig_type0, orig_type1;
9895 tree eptype;
9896 enum tree_code code0, code1;
9897 tree op0, op1;
9898 tree ret = error_mark_node;
9899 const char *invalid_op_diag;
9900 bool op0_int_operands, op1_int_operands;
9901 bool int_const, int_const_or_overflow, int_operands;
9903 /* Expression code to give to the expression when it is built.
9904 Normally this is CODE, which is what the caller asked for,
9905 but in some special cases we change it. */
9906 enum tree_code resultcode = code;
9908 /* Data type in which the computation is to be performed.
9909 In the simplest cases this is the common type of the arguments. */
9910 tree result_type = NULL;
9912 /* When the computation is in excess precision, the type of the
9913 final EXCESS_PRECISION_EXPR. */
9914 tree semantic_result_type = NULL;
9916 /* Nonzero means operands have already been type-converted
9917 in whatever way is necessary.
9918 Zero means they need to be converted to RESULT_TYPE. */
9919 int converted = 0;
9921 /* Nonzero means create the expression with this type, rather than
9922 RESULT_TYPE. */
9923 tree build_type = 0;
9925 /* Nonzero means after finally constructing the expression
9926 convert it to this type. */
9927 tree final_type = 0;
9929 /* Nonzero if this is an operation like MIN or MAX which can
9930 safely be computed in short if both args are promoted shorts.
9931 Also implies COMMON.
9932 -1 indicates a bitwise operation; this makes a difference
9933 in the exact conditions for when it is safe to do the operation
9934 in a narrower mode. */
9935 int shorten = 0;
9937 /* Nonzero if this is a comparison operation;
9938 if both args are promoted shorts, compare the original shorts.
9939 Also implies COMMON. */
9940 int short_compare = 0;
9942 /* Nonzero if this is a right-shift operation, which can be computed on the
9943 original short and then promoted if the operand is a promoted short. */
9944 int short_shift = 0;
9946 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9947 int common = 0;
9949 /* True means types are compatible as far as ObjC is concerned. */
9950 bool objc_ok;
9952 /* True means this is an arithmetic operation that may need excess
9953 precision. */
9954 bool may_need_excess_precision;
9956 /* True means this is a boolean operation that converts both its
9957 operands to truth-values. */
9958 bool boolean_op = false;
9960 /* Remember whether we're doing / or %. */
9961 bool doing_div_or_mod = false;
9963 /* Remember whether we're doing << or >>. */
9964 bool doing_shift = false;
9966 /* Tree holding instrumentation expression. */
9967 tree instrument_expr = NULL;
9969 if (location == UNKNOWN_LOCATION)
9970 location = input_location;
9972 op0 = orig_op0;
9973 op1 = orig_op1;
9975 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9976 if (op0_int_operands)
9977 op0 = remove_c_maybe_const_expr (op0);
9978 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9979 if (op1_int_operands)
9980 op1 = remove_c_maybe_const_expr (op1);
9981 int_operands = (op0_int_operands && op1_int_operands);
9982 if (int_operands)
9984 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9985 && TREE_CODE (orig_op1) == INTEGER_CST);
9986 int_const = (int_const_or_overflow
9987 && !TREE_OVERFLOW (orig_op0)
9988 && !TREE_OVERFLOW (orig_op1));
9990 else
9991 int_const = int_const_or_overflow = false;
9993 /* Do not apply default conversion in mixed vector/scalar expression. */
9994 if (convert_p
9995 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
9996 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
9998 op0 = default_conversion (op0);
9999 op1 = default_conversion (op1);
10002 /* When Cilk Plus is enabled and there are array notations inside op0, then
10003 we check to see if there are builtin array notation functions. If
10004 so, then we take on the type of the array notation inside it. */
10005 if (flag_enable_cilkplus && contains_array_notation_expr (op0))
10006 orig_type0 = type0 = find_correct_array_notation_type (op0);
10007 else
10008 orig_type0 = type0 = TREE_TYPE (op0);
10010 if (flag_enable_cilkplus && contains_array_notation_expr (op1))
10011 orig_type1 = type1 = find_correct_array_notation_type (op1);
10012 else
10013 orig_type1 = type1 = TREE_TYPE (op1);
10015 /* The expression codes of the data types of the arguments tell us
10016 whether the arguments are integers, floating, pointers, etc. */
10017 code0 = TREE_CODE (type0);
10018 code1 = TREE_CODE (type1);
10020 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10021 STRIP_TYPE_NOPS (op0);
10022 STRIP_TYPE_NOPS (op1);
10024 /* If an error was already reported for one of the arguments,
10025 avoid reporting another error. */
10027 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10028 return error_mark_node;
10030 if ((invalid_op_diag
10031 = targetm.invalid_binary_op (code, type0, type1)))
10033 error_at (location, invalid_op_diag);
10034 return error_mark_node;
10037 switch (code)
10039 case PLUS_EXPR:
10040 case MINUS_EXPR:
10041 case MULT_EXPR:
10042 case TRUNC_DIV_EXPR:
10043 case CEIL_DIV_EXPR:
10044 case FLOOR_DIV_EXPR:
10045 case ROUND_DIV_EXPR:
10046 case EXACT_DIV_EXPR:
10047 may_need_excess_precision = true;
10048 break;
10049 default:
10050 may_need_excess_precision = false;
10051 break;
10053 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10055 op0 = TREE_OPERAND (op0, 0);
10056 type0 = TREE_TYPE (op0);
10058 else if (may_need_excess_precision
10059 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10061 type0 = eptype;
10062 op0 = convert (eptype, op0);
10064 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10066 op1 = TREE_OPERAND (op1, 0);
10067 type1 = TREE_TYPE (op1);
10069 else if (may_need_excess_precision
10070 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10072 type1 = eptype;
10073 op1 = convert (eptype, op1);
10076 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10078 /* In case when one of the operands of the binary operation is
10079 a vector and another is a scalar -- convert scalar to vector. */
10080 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10082 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10083 true);
10085 switch (convert_flag)
10087 case stv_error:
10088 return error_mark_node;
10089 case stv_firstarg:
10091 bool maybe_const = true;
10092 tree sc;
10093 sc = c_fully_fold (op0, false, &maybe_const);
10094 sc = save_expr (sc);
10095 sc = convert (TREE_TYPE (type1), sc);
10096 op0 = build_vector_from_val (type1, sc);
10097 if (!maybe_const)
10098 op0 = c_wrap_maybe_const (op0, true);
10099 orig_type0 = type0 = TREE_TYPE (op0);
10100 code0 = TREE_CODE (type0);
10101 converted = 1;
10102 break;
10104 case stv_secondarg:
10106 bool maybe_const = true;
10107 tree sc;
10108 sc = c_fully_fold (op1, false, &maybe_const);
10109 sc = save_expr (sc);
10110 sc = convert (TREE_TYPE (type0), sc);
10111 op1 = build_vector_from_val (type0, sc);
10112 if (!maybe_const)
10113 op1 = c_wrap_maybe_const (op1, true);
10114 orig_type1 = type1 = TREE_TYPE (op1);
10115 code1 = TREE_CODE (type1);
10116 converted = 1;
10117 break;
10119 default:
10120 break;
10124 switch (code)
10126 case PLUS_EXPR:
10127 /* Handle the pointer + int case. */
10128 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10130 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10131 goto return_build_binary_op;
10133 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10135 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10136 goto return_build_binary_op;
10138 else
10139 common = 1;
10140 break;
10142 case MINUS_EXPR:
10143 /* Subtraction of two similar pointers.
10144 We must subtract them as integers, then divide by object size. */
10145 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10146 && comp_target_types (location, type0, type1))
10148 ret = pointer_diff (location, op0, op1);
10149 goto return_build_binary_op;
10151 /* Handle pointer minus int. Just like pointer plus int. */
10152 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10154 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10155 goto return_build_binary_op;
10157 else
10158 common = 1;
10159 break;
10161 case MULT_EXPR:
10162 common = 1;
10163 break;
10165 case TRUNC_DIV_EXPR:
10166 case CEIL_DIV_EXPR:
10167 case FLOOR_DIV_EXPR:
10168 case ROUND_DIV_EXPR:
10169 case EXACT_DIV_EXPR:
10170 doing_div_or_mod = true;
10171 warn_for_div_by_zero (location, op1);
10173 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10174 || code0 == FIXED_POINT_TYPE
10175 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10176 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10177 || code1 == FIXED_POINT_TYPE
10178 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10180 enum tree_code tcode0 = code0, tcode1 = code1;
10182 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10183 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10184 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10185 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10187 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10188 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10189 resultcode = RDIV_EXPR;
10190 else
10191 /* Although it would be tempting to shorten always here, that
10192 loses on some targets, since the modulo instruction is
10193 undefined if the quotient can't be represented in the
10194 computation mode. We shorten only if unsigned or if
10195 dividing by something we know != -1. */
10196 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10197 || (TREE_CODE (op1) == INTEGER_CST
10198 && !integer_all_onesp (op1)));
10199 common = 1;
10201 break;
10203 case BIT_AND_EXPR:
10204 case BIT_IOR_EXPR:
10205 case BIT_XOR_EXPR:
10206 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10207 shorten = -1;
10208 /* Allow vector types which are not floating point types. */
10209 else if (code0 == VECTOR_TYPE
10210 && code1 == VECTOR_TYPE
10211 && !VECTOR_FLOAT_TYPE_P (type0)
10212 && !VECTOR_FLOAT_TYPE_P (type1))
10213 common = 1;
10214 break;
10216 case TRUNC_MOD_EXPR:
10217 case FLOOR_MOD_EXPR:
10218 doing_div_or_mod = true;
10219 warn_for_div_by_zero (location, op1);
10221 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10222 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10223 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10224 common = 1;
10225 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10227 /* Although it would be tempting to shorten always here, that loses
10228 on some targets, since the modulo instruction is undefined if the
10229 quotient can't be represented in the computation mode. We shorten
10230 only if unsigned or if dividing by something we know != -1. */
10231 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10232 || (TREE_CODE (op1) == INTEGER_CST
10233 && !integer_all_onesp (op1)));
10234 common = 1;
10236 break;
10238 case TRUTH_ANDIF_EXPR:
10239 case TRUTH_ORIF_EXPR:
10240 case TRUTH_AND_EXPR:
10241 case TRUTH_OR_EXPR:
10242 case TRUTH_XOR_EXPR:
10243 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10244 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10245 || code0 == FIXED_POINT_TYPE)
10246 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10247 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10248 || code1 == FIXED_POINT_TYPE))
10250 /* Result of these operations is always an int,
10251 but that does not mean the operands should be
10252 converted to ints! */
10253 result_type = integer_type_node;
10254 if (op0_int_operands)
10256 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10257 op0 = remove_c_maybe_const_expr (op0);
10259 else
10260 op0 = c_objc_common_truthvalue_conversion (location, op0);
10261 if (op1_int_operands)
10263 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10264 op1 = remove_c_maybe_const_expr (op1);
10266 else
10267 op1 = c_objc_common_truthvalue_conversion (location, op1);
10268 converted = 1;
10269 boolean_op = true;
10271 if (code == TRUTH_ANDIF_EXPR)
10273 int_const_or_overflow = (int_operands
10274 && TREE_CODE (orig_op0) == INTEGER_CST
10275 && (op0 == truthvalue_false_node
10276 || TREE_CODE (orig_op1) == INTEGER_CST));
10277 int_const = (int_const_or_overflow
10278 && !TREE_OVERFLOW (orig_op0)
10279 && (op0 == truthvalue_false_node
10280 || !TREE_OVERFLOW (orig_op1)));
10282 else if (code == TRUTH_ORIF_EXPR)
10284 int_const_or_overflow = (int_operands
10285 && TREE_CODE (orig_op0) == INTEGER_CST
10286 && (op0 == truthvalue_true_node
10287 || TREE_CODE (orig_op1) == INTEGER_CST));
10288 int_const = (int_const_or_overflow
10289 && !TREE_OVERFLOW (orig_op0)
10290 && (op0 == truthvalue_true_node
10291 || !TREE_OVERFLOW (orig_op1)));
10293 break;
10295 /* Shift operations: result has same type as first operand;
10296 always convert second operand to int.
10297 Also set SHORT_SHIFT if shifting rightward. */
10299 case RSHIFT_EXPR:
10300 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10301 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10303 result_type = type0;
10304 converted = 1;
10306 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10307 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10308 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10309 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10311 result_type = type0;
10312 converted = 1;
10314 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10315 && code1 == INTEGER_TYPE)
10317 doing_shift = true;
10318 if (TREE_CODE (op1) == INTEGER_CST)
10320 if (tree_int_cst_sgn (op1) < 0)
10322 int_const = false;
10323 if (c_inhibit_evaluation_warnings == 0)
10324 warning (0, "right shift count is negative");
10326 else
10328 if (!integer_zerop (op1))
10329 short_shift = 1;
10331 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10333 int_const = false;
10334 if (c_inhibit_evaluation_warnings == 0)
10335 warning (0, "right shift count >= width of type");
10340 /* Use the type of the value to be shifted. */
10341 result_type = type0;
10342 /* Convert the non vector shift-count to an integer, regardless
10343 of size of value being shifted. */
10344 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10345 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10346 op1 = convert (integer_type_node, op1);
10347 /* Avoid converting op1 to result_type later. */
10348 converted = 1;
10350 break;
10352 case LSHIFT_EXPR:
10353 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10354 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10356 result_type = type0;
10357 converted = 1;
10359 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10360 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10361 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10362 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10364 result_type = type0;
10365 converted = 1;
10367 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10368 && code1 == INTEGER_TYPE)
10370 doing_shift = true;
10371 if (TREE_CODE (op1) == INTEGER_CST)
10373 if (tree_int_cst_sgn (op1) < 0)
10375 int_const = false;
10376 if (c_inhibit_evaluation_warnings == 0)
10377 warning (0, "left shift count is negative");
10380 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10382 int_const = false;
10383 if (c_inhibit_evaluation_warnings == 0)
10384 warning (0, "left shift count >= width of type");
10388 /* Use the type of the value to be shifted. */
10389 result_type = type0;
10390 /* Convert the non vector shift-count to an integer, regardless
10391 of size of value being shifted. */
10392 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10393 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10394 op1 = convert (integer_type_node, op1);
10395 /* Avoid converting op1 to result_type later. */
10396 converted = 1;
10398 break;
10400 case EQ_EXPR:
10401 case NE_EXPR:
10402 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10404 tree intt;
10405 if (!vector_types_compatible_elements_p (type0, type1))
10407 error_at (location, "comparing vectors with different "
10408 "element types");
10409 return error_mark_node;
10412 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10414 error_at (location, "comparing vectors with different "
10415 "number of elements");
10416 return error_mark_node;
10419 /* Always construct signed integer vector type. */
10420 intt = c_common_type_for_size (GET_MODE_BITSIZE
10421 (TYPE_MODE (TREE_TYPE (type0))), 0);
10422 result_type = build_opaque_vector_type (intt,
10423 TYPE_VECTOR_SUBPARTS (type0));
10424 converted = 1;
10425 break;
10427 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10428 warning_at (location,
10429 OPT_Wfloat_equal,
10430 "comparing floating point with == or != is unsafe");
10431 /* Result of comparison is always int,
10432 but don't convert the args to int! */
10433 build_type = integer_type_node;
10434 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10435 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10436 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10437 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10438 short_compare = 1;
10439 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10441 if (TREE_CODE (op0) == ADDR_EXPR
10442 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10444 if (code == EQ_EXPR)
10445 warning_at (location,
10446 OPT_Waddress,
10447 "the comparison will always evaluate as %<false%> "
10448 "for the address of %qD will never be NULL",
10449 TREE_OPERAND (op0, 0));
10450 else
10451 warning_at (location,
10452 OPT_Waddress,
10453 "the comparison will always evaluate as %<true%> "
10454 "for the address of %qD will never be NULL",
10455 TREE_OPERAND (op0, 0));
10457 result_type = type0;
10459 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10461 if (TREE_CODE (op1) == ADDR_EXPR
10462 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10464 if (code == EQ_EXPR)
10465 warning_at (location,
10466 OPT_Waddress,
10467 "the comparison will always evaluate as %<false%> "
10468 "for the address of %qD will never be NULL",
10469 TREE_OPERAND (op1, 0));
10470 else
10471 warning_at (location,
10472 OPT_Waddress,
10473 "the comparison will always evaluate as %<true%> "
10474 "for the address of %qD will never be NULL",
10475 TREE_OPERAND (op1, 0));
10477 result_type = type1;
10479 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10481 tree tt0 = TREE_TYPE (type0);
10482 tree tt1 = TREE_TYPE (type1);
10483 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10484 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10485 addr_space_t as_common = ADDR_SPACE_GENERIC;
10487 /* Anything compares with void *. void * compares with anything.
10488 Otherwise, the targets must be compatible
10489 and both must be object or both incomplete. */
10490 if (comp_target_types (location, type0, type1))
10491 result_type = common_pointer_type (type0, type1);
10492 else if (!addr_space_superset (as0, as1, &as_common))
10494 error_at (location, "comparison of pointers to "
10495 "disjoint address spaces");
10496 return error_mark_node;
10498 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10500 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10501 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10502 "comparison of %<void *%> with function pointer");
10504 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10506 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10507 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10508 "comparison of %<void *%> with function pointer");
10510 else
10511 /* Avoid warning about the volatile ObjC EH puts on decls. */
10512 if (!objc_ok)
10513 pedwarn (location, 0,
10514 "comparison of distinct pointer types lacks a cast");
10516 if (result_type == NULL_TREE)
10518 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10519 result_type = build_pointer_type
10520 (build_qualified_type (void_type_node, qual));
10523 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10525 result_type = type0;
10526 pedwarn (location, 0, "comparison between pointer and integer");
10528 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10530 result_type = type1;
10531 pedwarn (location, 0, "comparison between pointer and integer");
10533 break;
10535 case LE_EXPR:
10536 case GE_EXPR:
10537 case LT_EXPR:
10538 case GT_EXPR:
10539 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10541 tree intt;
10542 if (!vector_types_compatible_elements_p (type0, type1))
10544 error_at (location, "comparing vectors with different "
10545 "element types");
10546 return error_mark_node;
10549 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10551 error_at (location, "comparing vectors with different "
10552 "number of elements");
10553 return error_mark_node;
10556 /* Always construct signed integer vector type. */
10557 intt = c_common_type_for_size (GET_MODE_BITSIZE
10558 (TYPE_MODE (TREE_TYPE (type0))), 0);
10559 result_type = build_opaque_vector_type (intt,
10560 TYPE_VECTOR_SUBPARTS (type0));
10561 converted = 1;
10562 break;
10564 build_type = integer_type_node;
10565 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10566 || code0 == FIXED_POINT_TYPE)
10567 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10568 || code1 == FIXED_POINT_TYPE))
10569 short_compare = 1;
10570 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10572 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10573 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10574 addr_space_t as_common;
10576 if (comp_target_types (location, type0, type1))
10578 result_type = common_pointer_type (type0, type1);
10579 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10580 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10581 pedwarn (location, 0,
10582 "comparison of complete and incomplete pointers");
10583 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10584 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10585 "ordered comparisons of pointers to functions");
10586 else if (null_pointer_constant_p (orig_op0)
10587 || null_pointer_constant_p (orig_op1))
10588 warning_at (location, OPT_Wextra,
10589 "ordered comparison of pointer with null pointer");
10592 else if (!addr_space_superset (as0, as1, &as_common))
10594 error_at (location, "comparison of pointers to "
10595 "disjoint address spaces");
10596 return error_mark_node;
10598 else
10600 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10601 result_type = build_pointer_type
10602 (build_qualified_type (void_type_node, qual));
10603 pedwarn (location, 0,
10604 "comparison of distinct pointer types lacks a cast");
10607 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10609 result_type = type0;
10610 if (pedantic)
10611 pedwarn (location, OPT_Wpedantic,
10612 "ordered comparison of pointer with integer zero");
10613 else if (extra_warnings)
10614 warning_at (location, OPT_Wextra,
10615 "ordered comparison of pointer with integer zero");
10617 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10619 result_type = type1;
10620 if (pedantic)
10621 pedwarn (location, OPT_Wpedantic,
10622 "ordered comparison of pointer with integer zero");
10623 else if (extra_warnings)
10624 warning_at (location, OPT_Wextra,
10625 "ordered comparison of pointer with integer zero");
10627 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10629 result_type = type0;
10630 pedwarn (location, 0, "comparison between pointer and integer");
10632 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10634 result_type = type1;
10635 pedwarn (location, 0, "comparison between pointer and integer");
10637 break;
10639 default:
10640 gcc_unreachable ();
10643 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10644 return error_mark_node;
10646 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10647 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10648 || !vector_types_compatible_elements_p (type0, type1)))
10650 binary_op_error (location, code, type0, type1);
10651 return error_mark_node;
10654 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10655 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10657 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10658 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10660 bool first_complex = (code0 == COMPLEX_TYPE);
10661 bool second_complex = (code1 == COMPLEX_TYPE);
10662 int none_complex = (!first_complex && !second_complex);
10664 if (shorten || common || short_compare)
10666 result_type = c_common_type (type0, type1);
10667 do_warn_double_promotion (result_type, type0, type1,
10668 "implicit conversion from %qT to %qT "
10669 "to match other operand of binary "
10670 "expression",
10671 location);
10672 if (result_type == error_mark_node)
10673 return error_mark_node;
10676 if (first_complex != second_complex
10677 && (code == PLUS_EXPR
10678 || code == MINUS_EXPR
10679 || code == MULT_EXPR
10680 || (code == TRUNC_DIV_EXPR && first_complex))
10681 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10682 && flag_signed_zeros)
10684 /* An operation on mixed real/complex operands must be
10685 handled specially, but the language-independent code can
10686 more easily optimize the plain complex arithmetic if
10687 -fno-signed-zeros. */
10688 tree real_type = TREE_TYPE (result_type);
10689 tree real, imag;
10690 if (type0 != orig_type0 || type1 != orig_type1)
10692 gcc_assert (may_need_excess_precision && common);
10693 semantic_result_type = c_common_type (orig_type0, orig_type1);
10695 if (first_complex)
10697 if (TREE_TYPE (op0) != result_type)
10698 op0 = convert_and_check (result_type, op0);
10699 if (TREE_TYPE (op1) != real_type)
10700 op1 = convert_and_check (real_type, op1);
10702 else
10704 if (TREE_TYPE (op0) != real_type)
10705 op0 = convert_and_check (real_type, op0);
10706 if (TREE_TYPE (op1) != result_type)
10707 op1 = convert_and_check (result_type, op1);
10709 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10710 return error_mark_node;
10711 if (first_complex)
10713 op0 = c_save_expr (op0);
10714 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10715 op0, 1);
10716 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10717 op0, 1);
10718 switch (code)
10720 case MULT_EXPR:
10721 case TRUNC_DIV_EXPR:
10722 op1 = c_save_expr (op1);
10723 imag = build2 (resultcode, real_type, imag, op1);
10724 /* Fall through. */
10725 case PLUS_EXPR:
10726 case MINUS_EXPR:
10727 real = build2 (resultcode, real_type, real, op1);
10728 break;
10729 default:
10730 gcc_unreachable();
10733 else
10735 op1 = c_save_expr (op1);
10736 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10737 op1, 1);
10738 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10739 op1, 1);
10740 switch (code)
10742 case MULT_EXPR:
10743 op0 = c_save_expr (op0);
10744 imag = build2 (resultcode, real_type, op0, imag);
10745 /* Fall through. */
10746 case PLUS_EXPR:
10747 real = build2 (resultcode, real_type, op0, real);
10748 break;
10749 case MINUS_EXPR:
10750 real = build2 (resultcode, real_type, op0, real);
10751 imag = build1 (NEGATE_EXPR, real_type, imag);
10752 break;
10753 default:
10754 gcc_unreachable();
10757 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10758 goto return_build_binary_op;
10761 /* For certain operations (which identify themselves by shorten != 0)
10762 if both args were extended from the same smaller type,
10763 do the arithmetic in that type and then extend.
10765 shorten !=0 and !=1 indicates a bitwise operation.
10766 For them, this optimization is safe only if
10767 both args are zero-extended or both are sign-extended.
10768 Otherwise, we might change the result.
10769 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10770 but calculated in (unsigned short) it would be (unsigned short)-1. */
10772 if (shorten && none_complex)
10774 final_type = result_type;
10775 result_type = shorten_binary_op (result_type, op0, op1,
10776 shorten == -1);
10779 /* Shifts can be shortened if shifting right. */
10781 if (short_shift)
10783 int unsigned_arg;
10784 tree arg0 = get_narrower (op0, &unsigned_arg);
10786 final_type = result_type;
10788 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10789 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10791 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10792 && tree_int_cst_sgn (op1) > 0
10793 /* We can shorten only if the shift count is less than the
10794 number of bits in the smaller type size. */
10795 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10796 /* We cannot drop an unsigned shift after sign-extension. */
10797 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10799 /* Do an unsigned shift if the operand was zero-extended. */
10800 result_type
10801 = c_common_signed_or_unsigned_type (unsigned_arg,
10802 TREE_TYPE (arg0));
10803 /* Convert value-to-be-shifted to that type. */
10804 if (TREE_TYPE (op0) != result_type)
10805 op0 = convert (result_type, op0);
10806 converted = 1;
10810 /* Comparison operations are shortened too but differently.
10811 They identify themselves by setting short_compare = 1. */
10813 if (short_compare)
10815 /* Don't write &op0, etc., because that would prevent op0
10816 from being kept in a register.
10817 Instead, make copies of the our local variables and
10818 pass the copies by reference, then copy them back afterward. */
10819 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10820 enum tree_code xresultcode = resultcode;
10821 tree val
10822 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10824 if (val != 0)
10826 ret = val;
10827 goto return_build_binary_op;
10830 op0 = xop0, op1 = xop1;
10831 converted = 1;
10832 resultcode = xresultcode;
10834 if (c_inhibit_evaluation_warnings == 0)
10836 bool op0_maybe_const = true;
10837 bool op1_maybe_const = true;
10838 tree orig_op0_folded, orig_op1_folded;
10840 if (in_late_binary_op)
10842 orig_op0_folded = orig_op0;
10843 orig_op1_folded = orig_op1;
10845 else
10847 /* Fold for the sake of possible warnings, as in
10848 build_conditional_expr. This requires the
10849 "original" values to be folded, not just op0 and
10850 op1. */
10851 c_inhibit_evaluation_warnings++;
10852 op0 = c_fully_fold (op0, require_constant_value,
10853 &op0_maybe_const);
10854 op1 = c_fully_fold (op1, require_constant_value,
10855 &op1_maybe_const);
10856 c_inhibit_evaluation_warnings--;
10857 orig_op0_folded = c_fully_fold (orig_op0,
10858 require_constant_value,
10859 NULL);
10860 orig_op1_folded = c_fully_fold (orig_op1,
10861 require_constant_value,
10862 NULL);
10865 if (warn_sign_compare)
10866 warn_for_sign_compare (location, orig_op0_folded,
10867 orig_op1_folded, op0, op1,
10868 result_type, resultcode);
10869 if (!in_late_binary_op && !int_operands)
10871 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10872 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10873 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10874 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10880 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10881 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10882 Then the expression will be built.
10883 It will be given type FINAL_TYPE if that is nonzero;
10884 otherwise, it will be given type RESULT_TYPE. */
10886 if (!result_type)
10888 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10889 return error_mark_node;
10892 if (build_type == NULL_TREE)
10894 build_type = result_type;
10895 if ((type0 != orig_type0 || type1 != orig_type1)
10896 && !boolean_op)
10898 gcc_assert (may_need_excess_precision && common);
10899 semantic_result_type = c_common_type (orig_type0, orig_type1);
10903 if (!converted)
10905 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10906 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10908 /* This can happen if one operand has a vector type, and the other
10909 has a different type. */
10910 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10911 return error_mark_node;
10914 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE))
10915 && current_function_decl != 0
10916 && !lookup_attribute ("no_sanitize_undefined",
10917 DECL_ATTRIBUTES (current_function_decl))
10918 && (doing_div_or_mod || doing_shift))
10920 /* OP0 and/or OP1 might have side-effects. */
10921 op0 = c_save_expr (op0);
10922 op1 = c_save_expr (op1);
10923 op0 = c_fully_fold (op0, false, NULL);
10924 op1 = c_fully_fold (op1, false, NULL);
10925 if (doing_div_or_mod && (flag_sanitize & SANITIZE_DIVIDE))
10926 instrument_expr = ubsan_instrument_division (location, op0, op1);
10927 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
10928 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
10931 /* Treat expressions in initializers specially as they can't trap. */
10932 if (int_const_or_overflow)
10933 ret = (require_constant_value
10934 ? fold_build2_initializer_loc (location, resultcode, build_type,
10935 op0, op1)
10936 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10937 else
10938 ret = build2 (resultcode, build_type, op0, op1);
10939 if (final_type != 0)
10940 ret = convert (final_type, ret);
10942 return_build_binary_op:
10943 gcc_assert (ret != error_mark_node);
10944 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10945 ret = (int_operands
10946 ? note_integer_operands (ret)
10947 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10948 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10949 && !in_late_binary_op)
10950 ret = note_integer_operands (ret);
10951 if (semantic_result_type)
10952 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10953 protected_set_expr_location (ret, location);
10955 if (instrument_expr != NULL)
10956 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
10957 instrument_expr, ret);
10959 return ret;
10963 /* Convert EXPR to be a truth-value, validating its type for this
10964 purpose. LOCATION is the source location for the expression. */
10966 tree
10967 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10969 bool int_const, int_operands;
10971 switch (TREE_CODE (TREE_TYPE (expr)))
10973 case ARRAY_TYPE:
10974 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10975 return error_mark_node;
10977 case RECORD_TYPE:
10978 error_at (location, "used struct type value where scalar is required");
10979 return error_mark_node;
10981 case UNION_TYPE:
10982 error_at (location, "used union type value where scalar is required");
10983 return error_mark_node;
10985 case VOID_TYPE:
10986 error_at (location, "void value not ignored as it ought to be");
10987 return error_mark_node;
10989 case FUNCTION_TYPE:
10990 gcc_unreachable ();
10992 case VECTOR_TYPE:
10993 error_at (location, "used vector type where scalar is required");
10994 return error_mark_node;
10996 default:
10997 break;
11000 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11001 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11002 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11004 expr = remove_c_maybe_const_expr (expr);
11005 expr = build2 (NE_EXPR, integer_type_node, expr,
11006 convert (TREE_TYPE (expr), integer_zero_node));
11007 expr = note_integer_operands (expr);
11009 else
11010 /* ??? Should we also give an error for vectors rather than leaving
11011 those to give errors later? */
11012 expr = c_common_truthvalue_conversion (location, expr);
11014 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11016 if (TREE_OVERFLOW (expr))
11017 return expr;
11018 else
11019 return note_integer_operands (expr);
11021 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11022 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11023 return expr;
11027 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11028 required. */
11030 tree
11031 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11033 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11035 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11036 /* Executing a compound literal inside a function reinitializes
11037 it. */
11038 if (!TREE_STATIC (decl))
11039 *se = true;
11040 return decl;
11042 else
11043 return expr;
11046 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11048 tree
11049 c_begin_omp_parallel (void)
11051 tree block;
11053 keep_next_level ();
11054 block = c_begin_compound_stmt (true);
11056 return block;
11059 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11060 statement. LOC is the location of the OMP_PARALLEL. */
11062 tree
11063 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11065 tree stmt;
11067 block = c_end_compound_stmt (loc, block, true);
11069 stmt = make_node (OMP_PARALLEL);
11070 TREE_TYPE (stmt) = void_type_node;
11071 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11072 OMP_PARALLEL_BODY (stmt) = block;
11073 SET_EXPR_LOCATION (stmt, loc);
11075 return add_stmt (stmt);
11078 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11080 tree
11081 c_begin_omp_task (void)
11083 tree block;
11085 keep_next_level ();
11086 block = c_begin_compound_stmt (true);
11088 return block;
11091 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11092 statement. LOC is the location of the #pragma. */
11094 tree
11095 c_finish_omp_task (location_t loc, tree clauses, tree block)
11097 tree stmt;
11099 block = c_end_compound_stmt (loc, block, true);
11101 stmt = make_node (OMP_TASK);
11102 TREE_TYPE (stmt) = void_type_node;
11103 OMP_TASK_CLAUSES (stmt) = clauses;
11104 OMP_TASK_BODY (stmt) = block;
11105 SET_EXPR_LOCATION (stmt, loc);
11107 return add_stmt (stmt);
11110 /* Generate GOMP_cancel call for #pragma omp cancel. */
11112 void
11113 c_finish_omp_cancel (location_t loc, tree clauses)
11115 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11116 int mask = 0;
11117 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11118 mask = 1;
11119 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11120 mask = 2;
11121 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11122 mask = 4;
11123 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11124 mask = 8;
11125 else
11127 error_at (loc, "%<#pragma omp cancel must specify one of "
11128 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11129 "clauses");
11130 return;
11132 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11133 if (ifc != NULL_TREE)
11135 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11136 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11137 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11138 build_zero_cst (type));
11140 else
11141 ifc = boolean_true_node;
11142 tree stmt = build_call_expr_loc (loc, fn, 2,
11143 build_int_cst (integer_type_node, mask),
11144 ifc);
11145 add_stmt (stmt);
11148 /* Generate GOMP_cancellation_point call for
11149 #pragma omp cancellation point. */
11151 void
11152 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11154 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11155 int mask = 0;
11156 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11157 mask = 1;
11158 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11159 mask = 2;
11160 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11161 mask = 4;
11162 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11163 mask = 8;
11164 else
11166 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11167 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11168 "clauses");
11169 return;
11171 tree stmt = build_call_expr_loc (loc, fn, 1,
11172 build_int_cst (integer_type_node, mask));
11173 add_stmt (stmt);
11176 /* Helper function for handle_omp_array_sections. Called recursively
11177 to handle multiple array-section-subscripts. C is the clause,
11178 T current expression (initially OMP_CLAUSE_DECL), which is either
11179 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11180 expression if specified, TREE_VALUE length expression if specified,
11181 TREE_CHAIN is what it has been specified after, or some decl.
11182 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11183 set to true if any of the array-section-subscript could have length
11184 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11185 first array-section-subscript which is known not to have length
11186 of one. Given say:
11187 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11188 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11189 all are or may have length of 1, array-section-subscript [:2] is the
11190 first one knonwn not to have length 1. For array-section-subscript
11191 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11192 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11193 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11194 case though, as some lengths could be zero. */
11196 static tree
11197 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11198 bool &maybe_zero_len, unsigned int &first_non_one)
11200 tree ret, low_bound, length, type;
11201 if (TREE_CODE (t) != TREE_LIST)
11203 if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
11204 return error_mark_node;
11205 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11207 if (DECL_P (t))
11208 error_at (OMP_CLAUSE_LOCATION (c),
11209 "%qD is not a variable in %qs clause", t,
11210 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11211 else
11212 error_at (OMP_CLAUSE_LOCATION (c),
11213 "%qE is not a variable in %qs clause", t,
11214 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11215 return error_mark_node;
11217 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11218 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11220 error_at (OMP_CLAUSE_LOCATION (c),
11221 "%qD is threadprivate variable in %qs clause", t,
11222 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11223 return error_mark_node;
11225 return t;
11228 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11229 maybe_zero_len, first_non_one);
11230 if (ret == error_mark_node || ret == NULL_TREE)
11231 return ret;
11233 type = TREE_TYPE (ret);
11234 low_bound = TREE_PURPOSE (t);
11235 length = TREE_VALUE (t);
11237 if (low_bound == error_mark_node || length == error_mark_node)
11238 return error_mark_node;
11240 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11242 error_at (OMP_CLAUSE_LOCATION (c),
11243 "low bound %qE of array section does not have integral type",
11244 low_bound);
11245 return error_mark_node;
11247 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11249 error_at (OMP_CLAUSE_LOCATION (c),
11250 "length %qE of array section does not have integral type",
11251 length);
11252 return error_mark_node;
11254 if (low_bound
11255 && TREE_CODE (low_bound) == INTEGER_CST
11256 && TYPE_PRECISION (TREE_TYPE (low_bound))
11257 > TYPE_PRECISION (sizetype))
11258 low_bound = fold_convert (sizetype, low_bound);
11259 if (length
11260 && TREE_CODE (length) == INTEGER_CST
11261 && TYPE_PRECISION (TREE_TYPE (length))
11262 > TYPE_PRECISION (sizetype))
11263 length = fold_convert (sizetype, length);
11264 if (low_bound == NULL_TREE)
11265 low_bound = integer_zero_node;
11267 if (length != NULL_TREE)
11269 if (!integer_nonzerop (length))
11270 maybe_zero_len = true;
11271 if (first_non_one == types.length ()
11272 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11273 first_non_one++;
11275 if (TREE_CODE (type) == ARRAY_TYPE)
11277 if (length == NULL_TREE
11278 && (TYPE_DOMAIN (type) == NULL_TREE
11279 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11281 error_at (OMP_CLAUSE_LOCATION (c),
11282 "for unknown bound array type length expression must "
11283 "be specified");
11284 return error_mark_node;
11286 if (TREE_CODE (low_bound) == INTEGER_CST
11287 && tree_int_cst_sgn (low_bound) == -1)
11289 error_at (OMP_CLAUSE_LOCATION (c),
11290 "negative low bound in array section in %qs clause",
11291 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11292 return error_mark_node;
11294 if (length != NULL_TREE
11295 && TREE_CODE (length) == INTEGER_CST
11296 && tree_int_cst_sgn (length) == -1)
11298 error_at (OMP_CLAUSE_LOCATION (c),
11299 "negative length in array section in %qs clause",
11300 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11301 return error_mark_node;
11303 if (TYPE_DOMAIN (type)
11304 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11305 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11306 == INTEGER_CST)
11308 tree size = size_binop (PLUS_EXPR,
11309 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11310 size_one_node);
11311 if (TREE_CODE (low_bound) == INTEGER_CST)
11313 if (tree_int_cst_lt (size, low_bound))
11315 error_at (OMP_CLAUSE_LOCATION (c),
11316 "low bound %qE above array section size "
11317 "in %qs clause", low_bound,
11318 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11319 return error_mark_node;
11321 if (tree_int_cst_equal (size, low_bound))
11322 maybe_zero_len = true;
11323 else if (length == NULL_TREE
11324 && first_non_one == types.length ()
11325 && tree_int_cst_equal
11326 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11327 low_bound))
11328 first_non_one++;
11330 else if (length == NULL_TREE)
11332 maybe_zero_len = true;
11333 if (first_non_one == types.length ())
11334 first_non_one++;
11336 if (length && TREE_CODE (length) == INTEGER_CST)
11338 if (tree_int_cst_lt (size, length))
11340 error_at (OMP_CLAUSE_LOCATION (c),
11341 "length %qE above array section size "
11342 "in %qs clause", length,
11343 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11344 return error_mark_node;
11346 if (TREE_CODE (low_bound) == INTEGER_CST)
11348 tree lbpluslen
11349 = size_binop (PLUS_EXPR,
11350 fold_convert (sizetype, low_bound),
11351 fold_convert (sizetype, length));
11352 if (TREE_CODE (lbpluslen) == INTEGER_CST
11353 && tree_int_cst_lt (size, lbpluslen))
11355 error_at (OMP_CLAUSE_LOCATION (c),
11356 "high bound %qE above array section size "
11357 "in %qs clause", lbpluslen,
11358 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11359 return error_mark_node;
11364 else if (length == NULL_TREE)
11366 maybe_zero_len = true;
11367 if (first_non_one == types.length ())
11368 first_non_one++;
11371 /* For [lb:] we will need to evaluate lb more than once. */
11372 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11374 tree lb = c_save_expr (low_bound);
11375 if (lb != low_bound)
11377 TREE_PURPOSE (t) = lb;
11378 low_bound = lb;
11382 else if (TREE_CODE (type) == POINTER_TYPE)
11384 if (length == NULL_TREE)
11386 error_at (OMP_CLAUSE_LOCATION (c),
11387 "for pointer type length expression must be specified");
11388 return error_mark_node;
11390 /* If there is a pointer type anywhere but in the very first
11391 array-section-subscript, the array section can't be contiguous. */
11392 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11393 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11395 error_at (OMP_CLAUSE_LOCATION (c),
11396 "array section is not contiguous in %qs clause",
11397 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11398 return error_mark_node;
11401 else
11403 error_at (OMP_CLAUSE_LOCATION (c),
11404 "%qE does not have pointer or array type", ret);
11405 return error_mark_node;
11407 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11408 types.safe_push (TREE_TYPE (ret));
11409 /* We will need to evaluate lb more than once. */
11410 tree lb = c_save_expr (low_bound);
11411 if (lb != low_bound)
11413 TREE_PURPOSE (t) = lb;
11414 low_bound = lb;
11416 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11417 return ret;
11420 /* Handle array sections for clause C. */
11422 static bool
11423 handle_omp_array_sections (tree c)
11425 bool maybe_zero_len = false;
11426 unsigned int first_non_one = 0;
11427 vec<tree> types = vNULL;
11428 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11429 maybe_zero_len, first_non_one);
11430 if (first == error_mark_node)
11432 types.release ();
11433 return true;
11435 if (first == NULL_TREE)
11437 types.release ();
11438 return false;
11440 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11442 tree t = OMP_CLAUSE_DECL (c);
11443 tree tem = NULL_TREE;
11444 types.release ();
11445 /* Need to evaluate side effects in the length expressions
11446 if any. */
11447 while (TREE_CODE (t) == TREE_LIST)
11449 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11451 if (tem == NULL_TREE)
11452 tem = TREE_VALUE (t);
11453 else
11454 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11455 TREE_VALUE (t), tem);
11457 t = TREE_CHAIN (t);
11459 if (tem)
11460 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11461 first = c_fully_fold (first, false, NULL);
11462 OMP_CLAUSE_DECL (c) = first;
11464 else
11466 unsigned int num = types.length (), i;
11467 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11468 tree condition = NULL_TREE;
11470 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11471 maybe_zero_len = true;
11473 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11474 t = TREE_CHAIN (t))
11476 tree low_bound = TREE_PURPOSE (t);
11477 tree length = TREE_VALUE (t);
11479 i--;
11480 if (low_bound
11481 && TREE_CODE (low_bound) == INTEGER_CST
11482 && TYPE_PRECISION (TREE_TYPE (low_bound))
11483 > TYPE_PRECISION (sizetype))
11484 low_bound = fold_convert (sizetype, low_bound);
11485 if (length
11486 && TREE_CODE (length) == INTEGER_CST
11487 && TYPE_PRECISION (TREE_TYPE (length))
11488 > TYPE_PRECISION (sizetype))
11489 length = fold_convert (sizetype, length);
11490 if (low_bound == NULL_TREE)
11491 low_bound = integer_zero_node;
11492 if (!maybe_zero_len && i > first_non_one)
11494 if (integer_nonzerop (low_bound))
11495 goto do_warn_noncontiguous;
11496 if (length != NULL_TREE
11497 && TREE_CODE (length) == INTEGER_CST
11498 && TYPE_DOMAIN (types[i])
11499 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11500 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11501 == INTEGER_CST)
11503 tree size;
11504 size = size_binop (PLUS_EXPR,
11505 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11506 size_one_node);
11507 if (!tree_int_cst_equal (length, size))
11509 do_warn_noncontiguous:
11510 error_at (OMP_CLAUSE_LOCATION (c),
11511 "array section is not contiguous in %qs "
11512 "clause",
11513 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11514 types.release ();
11515 return true;
11518 if (length != NULL_TREE
11519 && TREE_SIDE_EFFECTS (length))
11521 if (side_effects == NULL_TREE)
11522 side_effects = length;
11523 else
11524 side_effects = build2 (COMPOUND_EXPR,
11525 TREE_TYPE (side_effects),
11526 length, side_effects);
11529 else
11531 tree l;
11533 if (i > first_non_one && length && integer_nonzerop (length))
11534 continue;
11535 if (length)
11536 l = fold_convert (sizetype, length);
11537 else
11539 l = size_binop (PLUS_EXPR,
11540 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11541 size_one_node);
11542 l = size_binop (MINUS_EXPR, l,
11543 fold_convert (sizetype, low_bound));
11545 if (i > first_non_one)
11547 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11548 size_zero_node);
11549 if (condition == NULL_TREE)
11550 condition = l;
11551 else
11552 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11553 l, condition);
11555 else if (size == NULL_TREE)
11557 size = size_in_bytes (TREE_TYPE (types[i]));
11558 size = size_binop (MULT_EXPR, size, l);
11559 if (condition)
11560 size = fold_build3 (COND_EXPR, sizetype, condition,
11561 size, size_zero_node);
11563 else
11564 size = size_binop (MULT_EXPR, size, l);
11567 types.release ();
11568 if (side_effects)
11569 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11570 first = c_fully_fold (first, false, NULL);
11571 OMP_CLAUSE_DECL (c) = first;
11572 if (size)
11573 size = c_fully_fold (size, false, NULL);
11574 OMP_CLAUSE_SIZE (c) = size;
11575 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11576 return false;
11577 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11578 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11579 if (!c_mark_addressable (t))
11580 return false;
11581 OMP_CLAUSE_DECL (c2) = t;
11582 t = build_fold_addr_expr (first);
11583 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11584 tree ptr = OMP_CLAUSE_DECL (c2);
11585 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11586 ptr = build_fold_addr_expr (ptr);
11587 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11588 ptrdiff_type_node, t,
11589 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11590 ptrdiff_type_node, ptr));
11591 t = c_fully_fold (t, false, NULL);
11592 OMP_CLAUSE_SIZE (c2) = t;
11593 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11594 OMP_CLAUSE_CHAIN (c) = c2;
11596 return false;
11599 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11600 an inline call. But, remap
11601 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11602 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11604 static tree
11605 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11606 tree decl, tree placeholder)
11608 copy_body_data id;
11609 struct pointer_map_t *decl_map = pointer_map_create ();
11611 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
11612 *pointer_map_insert (decl_map, omp_decl2) = decl;
11613 memset (&id, 0, sizeof (id));
11614 id.src_fn = DECL_CONTEXT (omp_decl1);
11615 id.dst_fn = current_function_decl;
11616 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11617 id.decl_map = decl_map;
11619 id.copy_decl = copy_decl_no_change;
11620 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11621 id.transform_new_cfg = true;
11622 id.transform_return_to_modify = false;
11623 id.transform_lang_insert_block = NULL;
11624 id.eh_lp_nr = 0;
11625 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11626 pointer_map_destroy (decl_map);
11627 return stmt;
11630 /* Helper function of c_finish_omp_clauses, called via walk_tree.
11631 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11633 static tree
11634 c_find_omp_placeholder_r (tree *tp, int *, void *data)
11636 if (*tp == (tree) data)
11637 return *tp;
11638 return NULL_TREE;
11641 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
11642 Remove any elements from the list that are invalid. */
11644 tree
11645 c_finish_omp_clauses (tree clauses)
11647 bitmap_head generic_head, firstprivate_head, lastprivate_head;
11648 bitmap_head aligned_head;
11649 tree c, t, *pc = &clauses;
11650 bool branch_seen = false;
11651 bool copyprivate_seen = false;
11652 tree *nowait_clause = NULL;
11654 bitmap_obstack_initialize (NULL);
11655 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11656 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11657 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
11658 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
11660 for (pc = &clauses, c = clauses; c ; c = *pc)
11662 bool remove = false;
11663 bool need_complete = false;
11664 bool need_implicitly_determined = false;
11666 switch (OMP_CLAUSE_CODE (c))
11668 case OMP_CLAUSE_SHARED:
11669 need_implicitly_determined = true;
11670 goto check_dup_generic;
11672 case OMP_CLAUSE_PRIVATE:
11673 need_complete = true;
11674 need_implicitly_determined = true;
11675 goto check_dup_generic;
11677 case OMP_CLAUSE_REDUCTION:
11678 need_implicitly_determined = true;
11679 t = OMP_CLAUSE_DECL (c);
11680 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
11681 && FLOAT_TYPE_P (TREE_TYPE (t)))
11683 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11684 const char *r_name = NULL;
11686 switch (r_code)
11688 case PLUS_EXPR:
11689 case MULT_EXPR:
11690 case MINUS_EXPR:
11691 case MIN_EXPR:
11692 case MAX_EXPR:
11693 break;
11694 case BIT_AND_EXPR:
11695 r_name = "&";
11696 break;
11697 case BIT_XOR_EXPR:
11698 r_name = "^";
11699 break;
11700 case BIT_IOR_EXPR:
11701 r_name = "|";
11702 break;
11703 case TRUTH_ANDIF_EXPR:
11704 r_name = "&&";
11705 break;
11706 case TRUTH_ORIF_EXPR:
11707 r_name = "||";
11708 break;
11709 default:
11710 gcc_unreachable ();
11712 if (r_name)
11714 error_at (OMP_CLAUSE_LOCATION (c),
11715 "%qE has invalid type for %<reduction(%s)%>",
11716 t, r_name);
11717 remove = true;
11718 break;
11721 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11723 error_at (OMP_CLAUSE_LOCATION (c),
11724 "user defined reduction not found for %qD", t);
11725 remove = true;
11726 break;
11728 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11730 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11731 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11732 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11733 VAR_DECL, NULL_TREE, type);
11734 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11735 DECL_ARTIFICIAL (placeholder) = 1;
11736 DECL_IGNORED_P (placeholder) = 1;
11737 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11738 c_mark_addressable (placeholder);
11739 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11740 c_mark_addressable (OMP_CLAUSE_DECL (c));
11741 OMP_CLAUSE_REDUCTION_MERGE (c)
11742 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11743 TREE_VEC_ELT (list, 0),
11744 TREE_VEC_ELT (list, 1),
11745 OMP_CLAUSE_DECL (c), placeholder);
11746 OMP_CLAUSE_REDUCTION_MERGE (c)
11747 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11748 void_type_node, NULL_TREE,
11749 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11750 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11751 if (TREE_VEC_LENGTH (list) == 6)
11753 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11754 c_mark_addressable (OMP_CLAUSE_DECL (c));
11755 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11756 c_mark_addressable (placeholder);
11757 tree init = TREE_VEC_ELT (list, 5);
11758 if (init == error_mark_node)
11759 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11760 OMP_CLAUSE_REDUCTION_INIT (c)
11761 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11762 TREE_VEC_ELT (list, 3),
11763 OMP_CLAUSE_DECL (c), placeholder);
11764 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11765 OMP_CLAUSE_REDUCTION_INIT (c)
11766 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11767 OMP_CLAUSE_REDUCTION_INIT (c));
11768 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11769 c_find_omp_placeholder_r,
11770 placeholder, NULL))
11771 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11773 else
11775 tree init;
11776 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11777 init = build_constructor (TREE_TYPE (t), NULL);
11778 else
11779 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11780 OMP_CLAUSE_REDUCTION_INIT (c)
11781 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11783 OMP_CLAUSE_REDUCTION_INIT (c)
11784 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11785 void_type_node, NULL_TREE,
11786 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11787 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11789 goto check_dup_generic;
11791 case OMP_CLAUSE_COPYPRIVATE:
11792 copyprivate_seen = true;
11793 if (nowait_clause)
11795 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11796 "%<nowait%> clause must not be used together "
11797 "with %<copyprivate%>");
11798 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
11799 nowait_clause = NULL;
11801 goto check_dup_generic;
11803 case OMP_CLAUSE_COPYIN:
11804 t = OMP_CLAUSE_DECL (c);
11805 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
11807 error_at (OMP_CLAUSE_LOCATION (c),
11808 "%qE must be %<threadprivate%> for %<copyin%>", t);
11809 remove = true;
11810 break;
11812 goto check_dup_generic;
11814 case OMP_CLAUSE_LINEAR:
11815 t = OMP_CLAUSE_DECL (c);
11816 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11817 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
11819 error_at (OMP_CLAUSE_LOCATION (c),
11820 "linear clause applied to non-integral non-pointer "
11821 "variable with type %qT", TREE_TYPE (t));
11822 remove = true;
11823 break;
11825 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
11827 tree s = OMP_CLAUSE_LINEAR_STEP (c);
11828 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
11829 OMP_CLAUSE_DECL (c), s);
11830 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11831 sizetype, s, OMP_CLAUSE_DECL (c));
11832 if (s == error_mark_node)
11833 s = size_one_node;
11834 OMP_CLAUSE_LINEAR_STEP (c) = s;
11836 goto check_dup_generic;
11838 check_dup_generic:
11839 t = OMP_CLAUSE_DECL (c);
11840 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11842 error_at (OMP_CLAUSE_LOCATION (c),
11843 "%qE is not a variable in clause %qs", t,
11844 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11845 remove = true;
11847 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11848 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
11849 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11851 error_at (OMP_CLAUSE_LOCATION (c),
11852 "%qE appears more than once in data clauses", t);
11853 remove = true;
11855 else
11856 bitmap_set_bit (&generic_head, DECL_UID (t));
11857 break;
11859 case OMP_CLAUSE_FIRSTPRIVATE:
11860 t = OMP_CLAUSE_DECL (c);
11861 need_complete = true;
11862 need_implicitly_determined = true;
11863 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11865 error_at (OMP_CLAUSE_LOCATION (c),
11866 "%qE is not a variable in clause %<firstprivate%>", t);
11867 remove = true;
11869 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11870 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
11872 error_at (OMP_CLAUSE_LOCATION (c),
11873 "%qE appears more than once in data clauses", t);
11874 remove = true;
11876 else
11877 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
11878 break;
11880 case OMP_CLAUSE_LASTPRIVATE:
11881 t = OMP_CLAUSE_DECL (c);
11882 need_complete = true;
11883 need_implicitly_determined = true;
11884 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11886 error_at (OMP_CLAUSE_LOCATION (c),
11887 "%qE is not a variable in clause %<lastprivate%>", t);
11888 remove = true;
11890 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11891 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11893 error_at (OMP_CLAUSE_LOCATION (c),
11894 "%qE appears more than once in data clauses", t);
11895 remove = true;
11897 else
11898 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
11899 break;
11901 case OMP_CLAUSE_ALIGNED:
11902 t = OMP_CLAUSE_DECL (c);
11903 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11905 error_at (OMP_CLAUSE_LOCATION (c),
11906 "%qE is not a variable in %<aligned%> clause", t);
11907 remove = true;
11909 else if (!POINTER_TYPE_P (TREE_TYPE (t))
11910 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
11912 error_at (OMP_CLAUSE_LOCATION (c),
11913 "%qE in %<aligned%> clause is neither a pointer nor "
11914 "an array", t);
11915 remove = true;
11917 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
11919 error_at (OMP_CLAUSE_LOCATION (c),
11920 "%qE appears more than once in %<aligned%> clauses",
11922 remove = true;
11924 else
11925 bitmap_set_bit (&aligned_head, DECL_UID (t));
11926 break;
11928 case OMP_CLAUSE_DEPEND:
11929 t = OMP_CLAUSE_DECL (c);
11930 if (TREE_CODE (t) == TREE_LIST)
11932 if (handle_omp_array_sections (c))
11933 remove = true;
11934 break;
11936 if (t == error_mark_node)
11937 remove = true;
11938 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11940 error_at (OMP_CLAUSE_LOCATION (c),
11941 "%qE is not a variable in %<depend%> clause", t);
11942 remove = true;
11944 else if (!c_mark_addressable (t))
11945 remove = true;
11946 break;
11948 case OMP_CLAUSE_MAP:
11949 case OMP_CLAUSE_TO:
11950 case OMP_CLAUSE_FROM:
11951 t = OMP_CLAUSE_DECL (c);
11952 if (TREE_CODE (t) == TREE_LIST)
11954 if (handle_omp_array_sections (c))
11955 remove = true;
11956 else
11958 t = OMP_CLAUSE_DECL (c);
11959 if (!COMPLETE_TYPE_P (TREE_TYPE (t)))
11961 error_at (OMP_CLAUSE_LOCATION (c),
11962 "array section does not have mappable type "
11963 "in %qs clause",
11964 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11965 remove = true;
11968 break;
11970 if (t == error_mark_node)
11971 remove = true;
11972 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11974 error_at (OMP_CLAUSE_LOCATION (c),
11975 "%qE is not a variable in %qs clause", t,
11976 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11977 remove = true;
11979 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11981 error_at (OMP_CLAUSE_LOCATION (c),
11982 "%qD is threadprivate variable in %qs clause", t,
11983 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11984 remove = true;
11986 else if (!c_mark_addressable (t))
11987 remove = true;
11988 else if (!COMPLETE_TYPE_P (TREE_TYPE (t))
11989 && !(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
11990 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER))
11992 error_at (OMP_CLAUSE_LOCATION (c),
11993 "%qD does not have a mappable type in %qs clause", t,
11994 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11995 remove = true;
11997 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
11999 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12000 error ("%qD appears more than once in motion clauses", t);
12001 else
12002 error ("%qD appears more than once in map clauses", t);
12003 remove = true;
12005 else
12006 bitmap_set_bit (&generic_head, DECL_UID (t));
12007 break;
12009 case OMP_CLAUSE_UNIFORM:
12010 t = OMP_CLAUSE_DECL (c);
12011 if (TREE_CODE (t) != PARM_DECL)
12013 if (DECL_P (t))
12014 error_at (OMP_CLAUSE_LOCATION (c),
12015 "%qD is not an argument in %<uniform%> clause", t);
12016 else
12017 error_at (OMP_CLAUSE_LOCATION (c),
12018 "%qE is not an argument in %<uniform%> clause", t);
12019 remove = true;
12020 break;
12022 goto check_dup_generic;
12024 case OMP_CLAUSE_NOWAIT:
12025 if (copyprivate_seen)
12027 error_at (OMP_CLAUSE_LOCATION (c),
12028 "%<nowait%> clause must not be used together "
12029 "with %<copyprivate%>");
12030 remove = true;
12031 break;
12033 nowait_clause = pc;
12034 pc = &OMP_CLAUSE_CHAIN (c);
12035 continue;
12037 case OMP_CLAUSE_IF:
12038 case OMP_CLAUSE_NUM_THREADS:
12039 case OMP_CLAUSE_NUM_TEAMS:
12040 case OMP_CLAUSE_THREAD_LIMIT:
12041 case OMP_CLAUSE_SCHEDULE:
12042 case OMP_CLAUSE_ORDERED:
12043 case OMP_CLAUSE_DEFAULT:
12044 case OMP_CLAUSE_UNTIED:
12045 case OMP_CLAUSE_COLLAPSE:
12046 case OMP_CLAUSE_FINAL:
12047 case OMP_CLAUSE_MERGEABLE:
12048 case OMP_CLAUSE_SAFELEN:
12049 case OMP_CLAUSE_SIMDLEN:
12050 case OMP_CLAUSE_DEVICE:
12051 case OMP_CLAUSE_DIST_SCHEDULE:
12052 case OMP_CLAUSE_PARALLEL:
12053 case OMP_CLAUSE_FOR:
12054 case OMP_CLAUSE_SECTIONS:
12055 case OMP_CLAUSE_TASKGROUP:
12056 case OMP_CLAUSE_PROC_BIND:
12057 pc = &OMP_CLAUSE_CHAIN (c);
12058 continue;
12060 case OMP_CLAUSE_INBRANCH:
12061 case OMP_CLAUSE_NOTINBRANCH:
12062 if (branch_seen)
12064 error_at (OMP_CLAUSE_LOCATION (c),
12065 "%<inbranch%> clause is incompatible with "
12066 "%<notinbranch%>");
12067 remove = true;
12068 break;
12070 branch_seen = true;
12071 pc = &OMP_CLAUSE_CHAIN (c);
12072 continue;
12074 default:
12075 gcc_unreachable ();
12078 if (!remove)
12080 t = OMP_CLAUSE_DECL (c);
12082 if (need_complete)
12084 t = require_complete_type (t);
12085 if (t == error_mark_node)
12086 remove = true;
12089 if (need_implicitly_determined)
12091 const char *share_name = NULL;
12093 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12094 share_name = "threadprivate";
12095 else switch (c_omp_predetermined_sharing (t))
12097 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12098 break;
12099 case OMP_CLAUSE_DEFAULT_SHARED:
12100 /* const vars may be specified in firstprivate clause. */
12101 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12102 && TREE_READONLY (t))
12103 break;
12104 share_name = "shared";
12105 break;
12106 case OMP_CLAUSE_DEFAULT_PRIVATE:
12107 share_name = "private";
12108 break;
12109 default:
12110 gcc_unreachable ();
12112 if (share_name)
12114 error_at (OMP_CLAUSE_LOCATION (c),
12115 "%qE is predetermined %qs for %qs",
12116 t, share_name,
12117 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12118 remove = true;
12123 if (remove)
12124 *pc = OMP_CLAUSE_CHAIN (c);
12125 else
12126 pc = &OMP_CLAUSE_CHAIN (c);
12129 bitmap_obstack_release (NULL);
12130 return clauses;
12133 /* Create a transaction node. */
12135 tree
12136 c_finish_transaction (location_t loc, tree block, int flags)
12138 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12139 if (flags & TM_STMT_ATTR_OUTER)
12140 TRANSACTION_EXPR_OUTER (stmt) = 1;
12141 if (flags & TM_STMT_ATTR_RELAXED)
12142 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12143 return add_stmt (stmt);
12146 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12147 down to the element type of an array. */
12149 tree
12150 c_build_qualified_type (tree type, int type_quals)
12152 if (type == error_mark_node)
12153 return type;
12155 if (TREE_CODE (type) == ARRAY_TYPE)
12157 tree t;
12158 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12159 type_quals);
12161 /* See if we already have an identically qualified type. */
12162 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12164 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12165 && TYPE_NAME (t) == TYPE_NAME (type)
12166 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12167 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12168 TYPE_ATTRIBUTES (type)))
12169 break;
12171 if (!t)
12173 tree domain = TYPE_DOMAIN (type);
12175 t = build_variant_type_copy (type);
12176 TREE_TYPE (t) = element_type;
12178 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12179 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12180 SET_TYPE_STRUCTURAL_EQUALITY (t);
12181 else if (TYPE_CANONICAL (element_type) != element_type
12182 || (domain && TYPE_CANONICAL (domain) != domain))
12184 tree unqualified_canon
12185 = build_array_type (TYPE_CANONICAL (element_type),
12186 domain? TYPE_CANONICAL (domain)
12187 : NULL_TREE);
12188 TYPE_CANONICAL (t)
12189 = c_build_qualified_type (unqualified_canon, type_quals);
12191 else
12192 TYPE_CANONICAL (t) = t;
12194 return t;
12197 /* A restrict-qualified pointer type must be a pointer to object or
12198 incomplete type. Note that the use of POINTER_TYPE_P also allows
12199 REFERENCE_TYPEs, which is appropriate for C++. */
12200 if ((type_quals & TYPE_QUAL_RESTRICT)
12201 && (!POINTER_TYPE_P (type)
12202 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12204 error ("invalid use of %<restrict%>");
12205 type_quals &= ~TYPE_QUAL_RESTRICT;
12208 return build_qualified_type (type, type_quals);
12211 /* Build a VA_ARG_EXPR for the C parser. */
12213 tree
12214 c_build_va_arg (location_t loc, tree expr, tree type)
12216 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12217 warning_at (loc, OPT_Wc___compat,
12218 "C++ requires promoted type, not enum type, in %<va_arg%>");
12219 return build_va_arg (loc, expr, type);
12222 /* Return truthvalue of whether T1 is the same tree structure as T2.
12223 Return 1 if they are the same. Return 0 if they are different. */
12225 bool
12226 c_tree_equal (tree t1, tree t2)
12228 enum tree_code code1, code2;
12230 if (t1 == t2)
12231 return true;
12232 if (!t1 || !t2)
12233 return false;
12235 for (code1 = TREE_CODE (t1);
12236 CONVERT_EXPR_CODE_P (code1)
12237 || code1 == NON_LVALUE_EXPR;
12238 code1 = TREE_CODE (t1))
12239 t1 = TREE_OPERAND (t1, 0);
12240 for (code2 = TREE_CODE (t2);
12241 CONVERT_EXPR_CODE_P (code2)
12242 || code2 == NON_LVALUE_EXPR;
12243 code2 = TREE_CODE (t2))
12244 t2 = TREE_OPERAND (t2, 0);
12246 /* They might have become equal now. */
12247 if (t1 == t2)
12248 return true;
12250 if (code1 != code2)
12251 return false;
12253 switch (code1)
12255 case INTEGER_CST:
12256 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
12257 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
12259 case REAL_CST:
12260 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12262 case STRING_CST:
12263 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12264 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12265 TREE_STRING_LENGTH (t1));
12267 case FIXED_CST:
12268 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12269 TREE_FIXED_CST (t2));
12271 case COMPLEX_CST:
12272 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12273 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12275 case VECTOR_CST:
12276 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12278 case CONSTRUCTOR:
12279 /* We need to do this when determining whether or not two
12280 non-type pointer to member function template arguments
12281 are the same. */
12282 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12283 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12284 return false;
12286 tree field, value;
12287 unsigned int i;
12288 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12290 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12291 if (!c_tree_equal (field, elt2->index)
12292 || !c_tree_equal (value, elt2->value))
12293 return false;
12296 return true;
12298 case TREE_LIST:
12299 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12300 return false;
12301 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12302 return false;
12303 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12305 case SAVE_EXPR:
12306 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12308 case CALL_EXPR:
12310 tree arg1, arg2;
12311 call_expr_arg_iterator iter1, iter2;
12312 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12313 return false;
12314 for (arg1 = first_call_expr_arg (t1, &iter1),
12315 arg2 = first_call_expr_arg (t2, &iter2);
12316 arg1 && arg2;
12317 arg1 = next_call_expr_arg (&iter1),
12318 arg2 = next_call_expr_arg (&iter2))
12319 if (!c_tree_equal (arg1, arg2))
12320 return false;
12321 if (arg1 || arg2)
12322 return false;
12323 return true;
12326 case TARGET_EXPR:
12328 tree o1 = TREE_OPERAND (t1, 0);
12329 tree o2 = TREE_OPERAND (t2, 0);
12331 /* Special case: if either target is an unallocated VAR_DECL,
12332 it means that it's going to be unified with whatever the
12333 TARGET_EXPR is really supposed to initialize, so treat it
12334 as being equivalent to anything. */
12335 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12336 && !DECL_RTL_SET_P (o1))
12337 /*Nop*/;
12338 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12339 && !DECL_RTL_SET_P (o2))
12340 /*Nop*/;
12341 else if (!c_tree_equal (o1, o2))
12342 return false;
12344 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12347 case COMPONENT_REF:
12348 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12349 return false;
12350 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12352 case PARM_DECL:
12353 case VAR_DECL:
12354 case CONST_DECL:
12355 case FIELD_DECL:
12356 case FUNCTION_DECL:
12357 case IDENTIFIER_NODE:
12358 case SSA_NAME:
12359 return false;
12361 case TREE_VEC:
12363 unsigned ix;
12364 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12365 return false;
12366 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12367 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12368 TREE_VEC_ELT (t2, ix)))
12369 return false;
12370 return true;
12373 default:
12374 break;
12377 switch (TREE_CODE_CLASS (code1))
12379 case tcc_unary:
12380 case tcc_binary:
12381 case tcc_comparison:
12382 case tcc_expression:
12383 case tcc_vl_exp:
12384 case tcc_reference:
12385 case tcc_statement:
12387 int i, n = TREE_OPERAND_LENGTH (t1);
12389 switch (code1)
12391 case PREINCREMENT_EXPR:
12392 case PREDECREMENT_EXPR:
12393 case POSTINCREMENT_EXPR:
12394 case POSTDECREMENT_EXPR:
12395 n = 1;
12396 break;
12397 case ARRAY_REF:
12398 n = 2;
12399 break;
12400 default:
12401 break;
12404 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12405 && n != TREE_OPERAND_LENGTH (t2))
12406 return false;
12408 for (i = 0; i < n; ++i)
12409 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12410 return false;
12412 return true;
12415 case tcc_type:
12416 return comptypes (t1, t2);
12417 default:
12418 gcc_unreachable ();
12420 /* We can get here with --disable-checking. */
12421 return false;