* ipa-devirt.c (type_pair, default_hashset_traits): New types.
[official-gcc.git] / gcc / c / c-typeck.c
blobda71ab263f9e3f6a69e09f32e058d186ee5a745a
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
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 "stor-layout.h"
32 #include "trans-mem.h"
33 #include "varasm.h"
34 #include "stmt.h"
35 #include "langhooks.h"
36 #include "c-tree.h"
37 #include "c-lang.h"
38 #include "flags.h"
39 #include "intl.h"
40 #include "target.h"
41 #include "tree-iterator.h"
42 #include "bitmap.h"
43 #include "basic-block.h"
44 #include "gimple-expr.h"
45 #include "gimplify.h"
46 #include "tree-inline.h"
47 #include "omp-low.h"
48 #include "c-family/c-objc.h"
49 #include "c-family/c-common.h"
50 #include "c-family/c-ubsan.h"
51 #include "cilk.h"
52 #include "wide-int.h"
54 /* Possible cases of implicit bad conversions. Used to select
55 diagnostic messages in convert_for_assignment. */
56 enum impl_conv {
57 ic_argpass,
58 ic_assign,
59 ic_init,
60 ic_return
63 /* The level of nesting inside "__alignof__". */
64 int in_alignof;
66 /* The level of nesting inside "sizeof". */
67 int in_sizeof;
69 /* The level of nesting inside "typeof". */
70 int in_typeof;
72 /* The argument of last parsed sizeof expression, only to be tested
73 if expr.original_code == SIZEOF_EXPR. */
74 tree c_last_sizeof_arg;
76 /* Nonzero if we might need to print a "missing braces around
77 initializer" message within this initializer. */
78 static int found_missing_braces;
80 static int require_constant_value;
81 static int require_constant_elements;
83 static bool null_pointer_constant_p (const_tree);
84 static tree qualify_type (tree, tree);
85 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
86 bool *);
87 static int comp_target_types (location_t, tree, tree);
88 static int function_types_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
91 static tree lookup_field (tree, tree);
92 static int convert_arguments (location_t, vec<location_t>, tree,
93 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
94 tree);
95 static tree pointer_diff (location_t, tree, tree);
96 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
97 enum impl_conv, bool, tree, tree, int);
98 static tree valid_compound_expr_initializer (tree, tree);
99 static void push_string (const char *);
100 static void push_member_name (tree);
101 static int spelling_length (void);
102 static char *print_spelling (char *);
103 static void warning_init (location_t, int, const char *);
104 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
105 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
106 bool, struct obstack *);
107 static void output_pending_init_elements (int, struct obstack *);
108 static int set_designator (location_t, int, struct obstack *);
109 static void push_range_stack (tree, struct obstack *);
110 static void add_pending_init (location_t, tree, tree, tree, bool,
111 struct obstack *);
112 static void set_nonincremental_init (struct obstack *);
113 static void set_nonincremental_init_from_string (tree, struct obstack *);
114 static tree find_init_member (tree, struct obstack *);
115 static void readonly_warning (tree, enum lvalue_use);
116 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
117 static void record_maybe_used_decl (tree);
118 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
120 /* Return true if EXP is a null pointer constant, false otherwise. */
122 static bool
123 null_pointer_constant_p (const_tree expr)
125 /* This should really operate on c_expr structures, but they aren't
126 yet available everywhere required. */
127 tree type = TREE_TYPE (expr);
128 return (TREE_CODE (expr) == INTEGER_CST
129 && !TREE_OVERFLOW (expr)
130 && integer_zerop (expr)
131 && (INTEGRAL_TYPE_P (type)
132 || (TREE_CODE (type) == POINTER_TYPE
133 && VOID_TYPE_P (TREE_TYPE (type))
134 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
137 /* EXPR may appear in an unevaluated part of an integer constant
138 expression, but not in an evaluated part. Wrap it in a
139 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
140 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
142 static tree
143 note_integer_operands (tree expr)
145 tree ret;
146 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
148 ret = copy_node (expr);
149 TREE_OVERFLOW (ret) = 1;
151 else
153 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
154 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
156 return ret;
159 /* Having checked whether EXPR may appear in an unevaluated part of an
160 integer constant expression and found that it may, remove any
161 C_MAYBE_CONST_EXPR noting this fact and return the resulting
162 expression. */
164 static inline tree
165 remove_c_maybe_const_expr (tree expr)
167 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
168 return C_MAYBE_CONST_EXPR_EXPR (expr);
169 else
170 return expr;
173 \f/* This is a cache to hold if two types are compatible or not. */
175 struct tagged_tu_seen_cache {
176 const struct tagged_tu_seen_cache * next;
177 const_tree t1;
178 const_tree t2;
179 /* The return value of tagged_types_tu_compatible_p if we had seen
180 these two types already. */
181 int val;
184 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
185 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
187 /* Do `exp = require_complete_type (exp);' to make sure exp
188 does not have an incomplete type. (That includes void types.) */
190 tree
191 require_complete_type (tree value)
193 tree type = TREE_TYPE (value);
195 if (value == error_mark_node || type == error_mark_node)
196 return error_mark_node;
198 /* First, detect a valid value with a complete type. */
199 if (COMPLETE_TYPE_P (type))
200 return value;
202 c_incomplete_type_error (value, type);
203 return error_mark_node;
206 /* Print an error message for invalid use of an incomplete type.
207 VALUE is the expression that was used (or 0 if that isn't known)
208 and TYPE is the type that was invalid. */
210 void
211 c_incomplete_type_error (const_tree value, const_tree type)
213 const char *type_code_string;
215 /* Avoid duplicate error message. */
216 if (TREE_CODE (type) == ERROR_MARK)
217 return;
219 if (value != 0 && (TREE_CODE (value) == VAR_DECL
220 || TREE_CODE (value) == PARM_DECL))
221 error ("%qD has an incomplete type", value);
222 else
224 retry:
225 /* We must print an error message. Be clever about what it says. */
227 switch (TREE_CODE (type))
229 case RECORD_TYPE:
230 type_code_string = "struct";
231 break;
233 case UNION_TYPE:
234 type_code_string = "union";
235 break;
237 case ENUMERAL_TYPE:
238 type_code_string = "enum";
239 break;
241 case VOID_TYPE:
242 error ("invalid use of void expression");
243 return;
245 case ARRAY_TYPE:
246 if (TYPE_DOMAIN (type))
248 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
250 error ("invalid use of flexible array member");
251 return;
253 type = TREE_TYPE (type);
254 goto retry;
256 error ("invalid use of array with unspecified bounds");
257 return;
259 default:
260 gcc_unreachable ();
263 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
264 error ("invalid use of undefined type %<%s %E%>",
265 type_code_string, TYPE_NAME (type));
266 else
267 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
268 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
272 /* Given a type, apply default promotions wrt unnamed function
273 arguments and return the new type. */
275 tree
276 c_type_promotes_to (tree type)
278 tree ret = NULL_TREE;
280 if (TYPE_MAIN_VARIANT (type) == float_type_node)
281 ret = double_type_node;
282 else if (c_promoting_integer_type_p (type))
284 /* Preserve unsignedness if not really getting any wider. */
285 if (TYPE_UNSIGNED (type)
286 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
287 ret = unsigned_type_node;
288 else
289 ret = integer_type_node;
292 if (ret != NULL_TREE)
293 return (TYPE_ATOMIC (type)
294 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
295 : ret);
297 return type;
300 /* Return true if between two named address spaces, whether there is a superset
301 named address space that encompasses both address spaces. If there is a
302 superset, return which address space is the superset. */
304 static bool
305 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
307 if (as1 == as2)
309 *common = as1;
310 return true;
312 else if (targetm.addr_space.subset_p (as1, as2))
314 *common = as2;
315 return true;
317 else if (targetm.addr_space.subset_p (as2, as1))
319 *common = as1;
320 return true;
322 else
323 return false;
326 /* Return a variant of TYPE which has all the type qualifiers of LIKE
327 as well as those of TYPE. */
329 static tree
330 qualify_type (tree type, tree like)
332 addr_space_t as_type = TYPE_ADDR_SPACE (type);
333 addr_space_t as_like = TYPE_ADDR_SPACE (like);
334 addr_space_t as_common;
336 /* If the two named address spaces are different, determine the common
337 superset address space. If there isn't one, raise an error. */
338 if (!addr_space_superset (as_type, as_like, &as_common))
340 as_common = as_type;
341 error ("%qT and %qT are in disjoint named address spaces",
342 type, like);
345 return c_build_qualified_type (type,
346 TYPE_QUALS_NO_ADDR_SPACE (type)
347 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
348 | ENCODE_QUAL_ADDR_SPACE (as_common));
351 /* Return true iff the given tree T is a variable length array. */
353 bool
354 c_vla_type_p (const_tree t)
356 if (TREE_CODE (t) == ARRAY_TYPE
357 && C_TYPE_VARIABLE_SIZE (t))
358 return true;
359 return false;
362 /* Return the composite type of two compatible types.
364 We assume that comptypes has already been done and returned
365 nonzero; if that isn't so, this may crash. In particular, we
366 assume that qualifiers match. */
368 tree
369 composite_type (tree t1, tree t2)
371 enum tree_code code1;
372 enum tree_code code2;
373 tree attributes;
375 /* Save time if the two types are the same. */
377 if (t1 == t2) return t1;
379 /* If one type is nonsense, use the other. */
380 if (t1 == error_mark_node)
381 return t2;
382 if (t2 == error_mark_node)
383 return t1;
385 code1 = TREE_CODE (t1);
386 code2 = TREE_CODE (t2);
388 /* Merge the attributes. */
389 attributes = targetm.merge_type_attributes (t1, t2);
391 /* If one is an enumerated type and the other is the compatible
392 integer type, the composite type might be either of the two
393 (DR#013 question 3). For consistency, use the enumerated type as
394 the composite type. */
396 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
397 return t1;
398 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
399 return t2;
401 gcc_assert (code1 == code2);
403 switch (code1)
405 case POINTER_TYPE:
406 /* For two pointers, do this recursively on the target type. */
408 tree pointed_to_1 = TREE_TYPE (t1);
409 tree pointed_to_2 = TREE_TYPE (t2);
410 tree target = composite_type (pointed_to_1, pointed_to_2);
411 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
412 t1 = build_type_attribute_variant (t1, attributes);
413 return qualify_type (t1, t2);
416 case ARRAY_TYPE:
418 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
419 int quals;
420 tree unqual_elt;
421 tree d1 = TYPE_DOMAIN (t1);
422 tree d2 = TYPE_DOMAIN (t2);
423 bool d1_variable, d2_variable;
424 bool d1_zero, d2_zero;
425 bool t1_complete, t2_complete;
427 /* We should not have any type quals on arrays at all. */
428 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
429 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
431 t1_complete = COMPLETE_TYPE_P (t1);
432 t2_complete = COMPLETE_TYPE_P (t2);
434 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
435 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
437 d1_variable = (!d1_zero
438 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
439 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
440 d2_variable = (!d2_zero
441 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
442 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
443 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
444 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
446 /* Save space: see if the result is identical to one of the args. */
447 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
448 && (d2_variable || d2_zero || !d1_variable))
449 return build_type_attribute_variant (t1, attributes);
450 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
451 && (d1_variable || d1_zero || !d2_variable))
452 return build_type_attribute_variant (t2, attributes);
454 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
455 return build_type_attribute_variant (t1, attributes);
456 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
457 return build_type_attribute_variant (t2, attributes);
459 /* Merge the element types, and have a size if either arg has
460 one. We may have qualifiers on the element types. To set
461 up TYPE_MAIN_VARIANT correctly, we need to form the
462 composite of the unqualified types and add the qualifiers
463 back at the end. */
464 quals = TYPE_QUALS (strip_array_types (elt));
465 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
466 t1 = build_array_type (unqual_elt,
467 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
468 && (d2_variable
469 || d2_zero
470 || !d1_variable))
471 ? t1
472 : t2));
473 /* Ensure a composite type involving a zero-length array type
474 is a zero-length type not an incomplete type. */
475 if (d1_zero && d2_zero
476 && (t1_complete || t2_complete)
477 && !COMPLETE_TYPE_P (t1))
479 TYPE_SIZE (t1) = bitsize_zero_node;
480 TYPE_SIZE_UNIT (t1) = size_zero_node;
482 t1 = c_build_qualified_type (t1, quals);
483 return build_type_attribute_variant (t1, attributes);
486 case ENUMERAL_TYPE:
487 case RECORD_TYPE:
488 case UNION_TYPE:
489 if (attributes != NULL)
491 /* Try harder not to create a new aggregate type. */
492 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
493 return t1;
494 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
495 return t2;
497 return build_type_attribute_variant (t1, attributes);
499 case FUNCTION_TYPE:
500 /* Function types: prefer the one that specified arg types.
501 If both do, merge the arg types. Also merge the return types. */
503 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
504 tree p1 = TYPE_ARG_TYPES (t1);
505 tree p2 = TYPE_ARG_TYPES (t2);
506 int len;
507 tree newargs, n;
508 int i;
510 /* Save space: see if the result is identical to one of the args. */
511 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
512 return build_type_attribute_variant (t1, attributes);
513 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
514 return build_type_attribute_variant (t2, attributes);
516 /* Simple way if one arg fails to specify argument types. */
517 if (TYPE_ARG_TYPES (t1) == 0)
519 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
520 t1 = build_type_attribute_variant (t1, attributes);
521 return qualify_type (t1, t2);
523 if (TYPE_ARG_TYPES (t2) == 0)
525 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
526 t1 = build_type_attribute_variant (t1, attributes);
527 return qualify_type (t1, t2);
530 /* If both args specify argument types, we must merge the two
531 lists, argument by argument. */
533 len = list_length (p1);
534 newargs = 0;
536 for (i = 0; i < len; i++)
537 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
539 n = newargs;
541 for (; p1;
542 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
544 /* A null type means arg type is not specified.
545 Take whatever the other function type has. */
546 if (TREE_VALUE (p1) == 0)
548 TREE_VALUE (n) = TREE_VALUE (p2);
549 goto parm_done;
551 if (TREE_VALUE (p2) == 0)
553 TREE_VALUE (n) = TREE_VALUE (p1);
554 goto parm_done;
557 /* Given wait (union {union wait *u; int *i} *)
558 and wait (union wait *),
559 prefer union wait * as type of parm. */
560 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
561 && TREE_VALUE (p1) != TREE_VALUE (p2))
563 tree memb;
564 tree mv2 = TREE_VALUE (p2);
565 if (mv2 && mv2 != error_mark_node
566 && TREE_CODE (mv2) != ARRAY_TYPE)
567 mv2 = TYPE_MAIN_VARIANT (mv2);
568 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
569 memb; memb = DECL_CHAIN (memb))
571 tree mv3 = TREE_TYPE (memb);
572 if (mv3 && mv3 != error_mark_node
573 && TREE_CODE (mv3) != ARRAY_TYPE)
574 mv3 = TYPE_MAIN_VARIANT (mv3);
575 if (comptypes (mv3, mv2))
577 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
578 TREE_VALUE (p2));
579 pedwarn (input_location, OPT_Wpedantic,
580 "function types not truly compatible in ISO C");
581 goto parm_done;
585 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
586 && TREE_VALUE (p2) != TREE_VALUE (p1))
588 tree memb;
589 tree mv1 = TREE_VALUE (p1);
590 if (mv1 && mv1 != error_mark_node
591 && TREE_CODE (mv1) != ARRAY_TYPE)
592 mv1 = TYPE_MAIN_VARIANT (mv1);
593 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
594 memb; memb = DECL_CHAIN (memb))
596 tree mv3 = TREE_TYPE (memb);
597 if (mv3 && mv3 != error_mark_node
598 && TREE_CODE (mv3) != ARRAY_TYPE)
599 mv3 = TYPE_MAIN_VARIANT (mv3);
600 if (comptypes (mv3, mv1))
602 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
603 TREE_VALUE (p1));
604 pedwarn (input_location, OPT_Wpedantic,
605 "function types not truly compatible in ISO C");
606 goto parm_done;
610 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
611 parm_done: ;
614 t1 = build_function_type (valtype, newargs);
615 t1 = qualify_type (t1, t2);
616 /* ... falls through ... */
619 default:
620 return build_type_attribute_variant (t1, attributes);
625 /* Return the type of a conditional expression between pointers to
626 possibly differently qualified versions of compatible types.
628 We assume that comp_target_types has already been done and returned
629 nonzero; if that isn't so, this may crash. */
631 static tree
632 common_pointer_type (tree t1, tree t2)
634 tree attributes;
635 tree pointed_to_1, mv1;
636 tree pointed_to_2, mv2;
637 tree target;
638 unsigned target_quals;
639 addr_space_t as1, as2, as_common;
640 int quals1, quals2;
642 /* Save time if the two types are the same. */
644 if (t1 == t2) return t1;
646 /* If one type is nonsense, use the other. */
647 if (t1 == error_mark_node)
648 return t2;
649 if (t2 == error_mark_node)
650 return t1;
652 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
653 && TREE_CODE (t2) == POINTER_TYPE);
655 /* Merge the attributes. */
656 attributes = targetm.merge_type_attributes (t1, t2);
658 /* Find the composite type of the target types, and combine the
659 qualifiers of the two types' targets. Do not lose qualifiers on
660 array element types by taking the TYPE_MAIN_VARIANT. */
661 mv1 = pointed_to_1 = TREE_TYPE (t1);
662 mv2 = pointed_to_2 = TREE_TYPE (t2);
663 if (TREE_CODE (mv1) != ARRAY_TYPE)
664 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
665 if (TREE_CODE (mv2) != ARRAY_TYPE)
666 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
667 target = composite_type (mv1, mv2);
669 /* For function types do not merge const qualifiers, but drop them
670 if used inconsistently. The middle-end uses these to mark const
671 and noreturn functions. */
672 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
673 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
675 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
676 target_quals = (quals1 & quals2);
677 else
678 target_quals = (quals1 | quals2);
680 /* If the two named address spaces are different, determine the common
681 superset address space. This is guaranteed to exist due to the
682 assumption that comp_target_type returned non-zero. */
683 as1 = TYPE_ADDR_SPACE (pointed_to_1);
684 as2 = TYPE_ADDR_SPACE (pointed_to_2);
685 if (!addr_space_superset (as1, as2, &as_common))
686 gcc_unreachable ();
688 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
690 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
691 return build_type_attribute_variant (t1, attributes);
694 /* Return the common type for two arithmetic types under the usual
695 arithmetic conversions. The default conversions have already been
696 applied, and enumerated types converted to their compatible integer
697 types. The resulting type is unqualified and has no attributes.
699 This is the type for the result of most arithmetic operations
700 if the operands have the given two types. */
702 static tree
703 c_common_type (tree t1, tree t2)
705 enum tree_code code1;
706 enum tree_code code2;
708 /* If one type is nonsense, use the other. */
709 if (t1 == error_mark_node)
710 return t2;
711 if (t2 == error_mark_node)
712 return t1;
714 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
715 t1 = TYPE_MAIN_VARIANT (t1);
717 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
718 t2 = TYPE_MAIN_VARIANT (t2);
720 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
721 t1 = build_type_attribute_variant (t1, NULL_TREE);
723 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
724 t2 = build_type_attribute_variant (t2, NULL_TREE);
726 /* Save time if the two types are the same. */
728 if (t1 == t2) return t1;
730 code1 = TREE_CODE (t1);
731 code2 = TREE_CODE (t2);
733 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
734 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
735 || code1 == INTEGER_TYPE);
736 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
737 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
738 || code2 == INTEGER_TYPE);
740 /* When one operand is a decimal float type, the other operand cannot be
741 a generic float type or a complex type. We also disallow vector types
742 here. */
743 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
744 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
746 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
748 error ("can%'t mix operands of decimal float and vector types");
749 return error_mark_node;
751 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
753 error ("can%'t mix operands of decimal float and complex types");
754 return error_mark_node;
756 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
758 error ("can%'t mix operands of decimal float and other float types");
759 return error_mark_node;
763 /* If one type is a vector type, return that type. (How the usual
764 arithmetic conversions apply to the vector types extension is not
765 precisely specified.) */
766 if (code1 == VECTOR_TYPE)
767 return t1;
769 if (code2 == VECTOR_TYPE)
770 return t2;
772 /* If one type is complex, form the common type of the non-complex
773 components, then make that complex. Use T1 or T2 if it is the
774 required type. */
775 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
777 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
778 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
779 tree subtype = c_common_type (subtype1, subtype2);
781 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
782 return t1;
783 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
784 return t2;
785 else
786 return build_complex_type (subtype);
789 /* If only one is real, use it as the result. */
791 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
792 return t1;
794 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
795 return t2;
797 /* If both are real and either are decimal floating point types, use
798 the decimal floating point type with the greater precision. */
800 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
802 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
803 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
804 return dfloat128_type_node;
805 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
806 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
807 return dfloat64_type_node;
808 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
809 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
810 return dfloat32_type_node;
813 /* Deal with fixed-point types. */
814 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
816 unsigned int unsignedp = 0, satp = 0;
817 enum machine_mode m1, m2;
818 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
820 m1 = TYPE_MODE (t1);
821 m2 = TYPE_MODE (t2);
823 /* If one input type is saturating, the result type is saturating. */
824 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
825 satp = 1;
827 /* If both fixed-point types are unsigned, the result type is unsigned.
828 When mixing fixed-point and integer types, follow the sign of the
829 fixed-point type.
830 Otherwise, the result type is signed. */
831 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
832 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
833 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
834 && TYPE_UNSIGNED (t1))
835 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
836 && TYPE_UNSIGNED (t2)))
837 unsignedp = 1;
839 /* The result type is signed. */
840 if (unsignedp == 0)
842 /* If the input type is unsigned, we need to convert to the
843 signed type. */
844 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
846 enum mode_class mclass = (enum mode_class) 0;
847 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
848 mclass = MODE_FRACT;
849 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
850 mclass = MODE_ACCUM;
851 else
852 gcc_unreachable ();
853 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
855 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
857 enum mode_class mclass = (enum mode_class) 0;
858 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
859 mclass = MODE_FRACT;
860 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
861 mclass = MODE_ACCUM;
862 else
863 gcc_unreachable ();
864 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
868 if (code1 == FIXED_POINT_TYPE)
870 fbit1 = GET_MODE_FBIT (m1);
871 ibit1 = GET_MODE_IBIT (m1);
873 else
875 fbit1 = 0;
876 /* Signed integers need to subtract one sign bit. */
877 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
880 if (code2 == FIXED_POINT_TYPE)
882 fbit2 = GET_MODE_FBIT (m2);
883 ibit2 = GET_MODE_IBIT (m2);
885 else
887 fbit2 = 0;
888 /* Signed integers need to subtract one sign bit. */
889 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
892 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
893 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
894 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
895 satp);
898 /* Both real or both integers; use the one with greater precision. */
900 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
901 return t1;
902 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
903 return t2;
905 /* Same precision. Prefer long longs to longs to ints when the
906 same precision, following the C99 rules on integer type rank
907 (which are equivalent to the C90 rules for C90 types). */
909 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
910 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
911 return long_long_unsigned_type_node;
913 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
914 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
916 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
917 return long_long_unsigned_type_node;
918 else
919 return long_long_integer_type_node;
922 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
923 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
924 return long_unsigned_type_node;
926 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
927 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
929 /* But preserve unsignedness from the other type,
930 since long cannot hold all the values of an unsigned int. */
931 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
932 return long_unsigned_type_node;
933 else
934 return long_integer_type_node;
937 /* Likewise, prefer long double to double even if same size. */
938 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
939 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
940 return long_double_type_node;
942 /* Likewise, prefer double to float even if same size.
943 We got a couple of embedded targets with 32 bit doubles, and the
944 pdp11 might have 64 bit floats. */
945 if (TYPE_MAIN_VARIANT (t1) == double_type_node
946 || TYPE_MAIN_VARIANT (t2) == double_type_node)
947 return double_type_node;
949 /* Otherwise prefer the unsigned one. */
951 if (TYPE_UNSIGNED (t1))
952 return t1;
953 else
954 return t2;
957 /* Wrapper around c_common_type that is used by c-common.c and other
958 front end optimizations that remove promotions. ENUMERAL_TYPEs
959 are allowed here and are converted to their compatible integer types.
960 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
961 preferably a non-Boolean type as the common type. */
962 tree
963 common_type (tree t1, tree t2)
965 if (TREE_CODE (t1) == ENUMERAL_TYPE)
966 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
967 if (TREE_CODE (t2) == ENUMERAL_TYPE)
968 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
970 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
971 if (TREE_CODE (t1) == BOOLEAN_TYPE
972 && TREE_CODE (t2) == BOOLEAN_TYPE)
973 return boolean_type_node;
975 /* If either type is BOOLEAN_TYPE, then return the other. */
976 if (TREE_CODE (t1) == BOOLEAN_TYPE)
977 return t2;
978 if (TREE_CODE (t2) == BOOLEAN_TYPE)
979 return t1;
981 return c_common_type (t1, t2);
984 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
985 or various other operations. Return 2 if they are compatible
986 but a warning may be needed if you use them together. */
989 comptypes (tree type1, tree type2)
991 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
992 int val;
994 val = comptypes_internal (type1, type2, NULL, NULL);
995 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
997 return val;
1000 /* Like comptypes, but if it returns non-zero because enum and int are
1001 compatible, it sets *ENUM_AND_INT_P to true. */
1003 static int
1004 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1006 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1007 int val;
1009 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1010 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1012 return val;
1015 /* Like comptypes, but if it returns nonzero for different types, it
1016 sets *DIFFERENT_TYPES_P to true. */
1019 comptypes_check_different_types (tree type1, tree type2,
1020 bool *different_types_p)
1022 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1023 int val;
1025 val = comptypes_internal (type1, type2, NULL, different_types_p);
1026 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1028 return val;
1031 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1032 or various other operations. Return 2 if they are compatible
1033 but a warning may be needed if you use them together. If
1034 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1035 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1036 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1037 NULL, and the types are compatible but different enough not to be
1038 permitted in C11 typedef redeclarations, then this sets
1039 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1040 false, but may or may not be set if the types are incompatible.
1041 This differs from comptypes, in that we don't free the seen
1042 types. */
1044 static int
1045 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1046 bool *different_types_p)
1048 const_tree t1 = type1;
1049 const_tree t2 = type2;
1050 int attrval, val;
1052 /* Suppress errors caused by previously reported errors. */
1054 if (t1 == t2 || !t1 || !t2
1055 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1056 return 1;
1058 /* Enumerated types are compatible with integer types, but this is
1059 not transitive: two enumerated types in the same translation unit
1060 are compatible with each other only if they are the same type. */
1062 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1064 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1065 if (TREE_CODE (t2) != VOID_TYPE)
1067 if (enum_and_int_p != NULL)
1068 *enum_and_int_p = true;
1069 if (different_types_p != NULL)
1070 *different_types_p = true;
1073 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1075 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1076 if (TREE_CODE (t1) != VOID_TYPE)
1078 if (enum_and_int_p != NULL)
1079 *enum_and_int_p = true;
1080 if (different_types_p != NULL)
1081 *different_types_p = true;
1085 if (t1 == t2)
1086 return 1;
1088 /* Different classes of types can't be compatible. */
1090 if (TREE_CODE (t1) != TREE_CODE (t2))
1091 return 0;
1093 /* Qualifiers must match. C99 6.7.3p9 */
1095 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1096 return 0;
1098 /* Allow for two different type nodes which have essentially the same
1099 definition. Note that we already checked for equality of the type
1100 qualifiers (just above). */
1102 if (TREE_CODE (t1) != ARRAY_TYPE
1103 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1104 return 1;
1106 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1107 if (!(attrval = comp_type_attributes (t1, t2)))
1108 return 0;
1110 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1111 val = 0;
1113 switch (TREE_CODE (t1))
1115 case POINTER_TYPE:
1116 /* Do not remove mode or aliasing information. */
1117 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1118 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1119 break;
1120 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1121 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1122 enum_and_int_p, different_types_p));
1123 break;
1125 case FUNCTION_TYPE:
1126 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1127 different_types_p);
1128 break;
1130 case ARRAY_TYPE:
1132 tree d1 = TYPE_DOMAIN (t1);
1133 tree d2 = TYPE_DOMAIN (t2);
1134 bool d1_variable, d2_variable;
1135 bool d1_zero, d2_zero;
1136 val = 1;
1138 /* Target types must match incl. qualifiers. */
1139 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1140 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1141 enum_and_int_p,
1142 different_types_p)))
1143 return 0;
1145 if (different_types_p != NULL
1146 && (d1 == 0) != (d2 == 0))
1147 *different_types_p = true;
1148 /* Sizes must match unless one is missing or variable. */
1149 if (d1 == 0 || d2 == 0 || d1 == d2)
1150 break;
1152 d1_zero = !TYPE_MAX_VALUE (d1);
1153 d2_zero = !TYPE_MAX_VALUE (d2);
1155 d1_variable = (!d1_zero
1156 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1157 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1158 d2_variable = (!d2_zero
1159 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1160 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1161 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1162 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1164 if (different_types_p != NULL
1165 && d1_variable != d2_variable)
1166 *different_types_p = true;
1167 if (d1_variable || d2_variable)
1168 break;
1169 if (d1_zero && d2_zero)
1170 break;
1171 if (d1_zero || d2_zero
1172 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1173 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1174 val = 0;
1176 break;
1179 case ENUMERAL_TYPE:
1180 case RECORD_TYPE:
1181 case UNION_TYPE:
1182 if (val != 1 && !same_translation_unit_p (t1, t2))
1184 tree a1 = TYPE_ATTRIBUTES (t1);
1185 tree a2 = TYPE_ATTRIBUTES (t2);
1187 if (! attribute_list_contained (a1, a2)
1188 && ! attribute_list_contained (a2, a1))
1189 break;
1191 if (attrval != 2)
1192 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1193 different_types_p);
1194 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1195 different_types_p);
1197 break;
1199 case VECTOR_TYPE:
1200 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1201 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1202 enum_and_int_p, different_types_p));
1203 break;
1205 default:
1206 break;
1208 return attrval == 2 && val == 1 ? 2 : val;
1211 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1212 their qualifiers, except for named address spaces. If the pointers point to
1213 different named addresses, then we must determine if one address space is a
1214 subset of the other. */
1216 static int
1217 comp_target_types (location_t location, tree ttl, tree ttr)
1219 int val;
1220 tree mvl = TREE_TYPE (ttl);
1221 tree mvr = TREE_TYPE (ttr);
1222 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1223 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1224 addr_space_t as_common;
1225 bool enum_and_int_p;
1227 /* Fail if pointers point to incompatible address spaces. */
1228 if (!addr_space_superset (asl, asr, &as_common))
1229 return 0;
1231 /* Do not lose qualifiers on element types of array types that are
1232 pointer targets by taking their TYPE_MAIN_VARIANT. */
1233 if (TREE_CODE (mvl) != ARRAY_TYPE)
1234 mvl = (TYPE_ATOMIC (mvl)
1235 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1236 : TYPE_MAIN_VARIANT (mvl));
1237 if (TREE_CODE (mvr) != ARRAY_TYPE)
1238 mvr = (TYPE_ATOMIC (mvr)
1239 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1240 : TYPE_MAIN_VARIANT (mvr));
1241 enum_and_int_p = false;
1242 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1244 if (val == 2)
1245 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1247 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1248 warning_at (location, OPT_Wc___compat,
1249 "pointer target types incompatible in C++");
1251 return val;
1254 /* Subroutines of `comptypes'. */
1256 /* Determine whether two trees derive from the same translation unit.
1257 If the CONTEXT chain ends in a null, that tree's context is still
1258 being parsed, so if two trees have context chains ending in null,
1259 they're in the same translation unit. */
1261 same_translation_unit_p (const_tree t1, const_tree t2)
1263 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1264 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1266 case tcc_declaration:
1267 t1 = DECL_CONTEXT (t1); break;
1268 case tcc_type:
1269 t1 = TYPE_CONTEXT (t1); break;
1270 case tcc_exceptional:
1271 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1272 default: gcc_unreachable ();
1275 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1276 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1278 case tcc_declaration:
1279 t2 = DECL_CONTEXT (t2); break;
1280 case tcc_type:
1281 t2 = TYPE_CONTEXT (t2); break;
1282 case tcc_exceptional:
1283 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1284 default: gcc_unreachable ();
1287 return t1 == t2;
1290 /* Allocate the seen two types, assuming that they are compatible. */
1292 static struct tagged_tu_seen_cache *
1293 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1295 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1296 tu->next = tagged_tu_seen_base;
1297 tu->t1 = t1;
1298 tu->t2 = t2;
1300 tagged_tu_seen_base = tu;
1302 /* The C standard says that two structures in different translation
1303 units are compatible with each other only if the types of their
1304 fields are compatible (among other things). We assume that they
1305 are compatible until proven otherwise when building the cache.
1306 An example where this can occur is:
1307 struct a
1309 struct a *next;
1311 If we are comparing this against a similar struct in another TU,
1312 and did not assume they were compatible, we end up with an infinite
1313 loop. */
1314 tu->val = 1;
1315 return tu;
1318 /* Free the seen types until we get to TU_TIL. */
1320 static void
1321 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1323 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1324 while (tu != tu_til)
1326 const struct tagged_tu_seen_cache *const tu1
1327 = (const struct tagged_tu_seen_cache *) tu;
1328 tu = tu1->next;
1329 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1331 tagged_tu_seen_base = tu_til;
1334 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1335 compatible. If the two types are not the same (which has been
1336 checked earlier), this can only happen when multiple translation
1337 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1338 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1339 comptypes_internal. */
1341 static int
1342 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1343 bool *enum_and_int_p, bool *different_types_p)
1345 tree s1, s2;
1346 bool needs_warning = false;
1348 /* We have to verify that the tags of the types are the same. This
1349 is harder than it looks because this may be a typedef, so we have
1350 to go look at the original type. It may even be a typedef of a
1351 typedef...
1352 In the case of compiler-created builtin structs the TYPE_DECL
1353 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1354 while (TYPE_NAME (t1)
1355 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1356 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1357 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1359 while (TYPE_NAME (t2)
1360 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1361 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1362 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1364 /* C90 didn't have the requirement that the two tags be the same. */
1365 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1366 return 0;
1368 /* C90 didn't say what happened if one or both of the types were
1369 incomplete; we choose to follow C99 rules here, which is that they
1370 are compatible. */
1371 if (TYPE_SIZE (t1) == NULL
1372 || TYPE_SIZE (t2) == NULL)
1373 return 1;
1376 const struct tagged_tu_seen_cache * tts_i;
1377 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1378 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1379 return tts_i->val;
1382 switch (TREE_CODE (t1))
1384 case ENUMERAL_TYPE:
1386 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1387 /* Speed up the case where the type values are in the same order. */
1388 tree tv1 = TYPE_VALUES (t1);
1389 tree tv2 = TYPE_VALUES (t2);
1391 if (tv1 == tv2)
1393 return 1;
1396 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1398 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1399 break;
1400 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1402 tu->val = 0;
1403 return 0;
1407 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1409 return 1;
1411 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1413 tu->val = 0;
1414 return 0;
1417 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1419 tu->val = 0;
1420 return 0;
1423 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1425 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1426 if (s2 == NULL
1427 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1429 tu->val = 0;
1430 return 0;
1433 return 1;
1436 case UNION_TYPE:
1438 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1439 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1441 tu->val = 0;
1442 return 0;
1445 /* Speed up the common case where the fields are in the same order. */
1446 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1447 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1449 int result;
1451 if (DECL_NAME (s1) != DECL_NAME (s2))
1452 break;
1453 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1454 enum_and_int_p, different_types_p);
1456 if (result != 1 && !DECL_NAME (s1))
1457 break;
1458 if (result == 0)
1460 tu->val = 0;
1461 return 0;
1463 if (result == 2)
1464 needs_warning = true;
1466 if (TREE_CODE (s1) == FIELD_DECL
1467 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1468 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1470 tu->val = 0;
1471 return 0;
1474 if (!s1 && !s2)
1476 tu->val = needs_warning ? 2 : 1;
1477 return tu->val;
1480 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1482 bool ok = false;
1484 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1485 if (DECL_NAME (s1) == DECL_NAME (s2))
1487 int result;
1489 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1490 enum_and_int_p,
1491 different_types_p);
1493 if (result != 1 && !DECL_NAME (s1))
1494 continue;
1495 if (result == 0)
1497 tu->val = 0;
1498 return 0;
1500 if (result == 2)
1501 needs_warning = true;
1503 if (TREE_CODE (s1) == FIELD_DECL
1504 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1505 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1506 break;
1508 ok = true;
1509 break;
1511 if (!ok)
1513 tu->val = 0;
1514 return 0;
1517 tu->val = needs_warning ? 2 : 10;
1518 return tu->val;
1521 case RECORD_TYPE:
1523 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1525 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1526 s1 && s2;
1527 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1529 int result;
1530 if (TREE_CODE (s1) != TREE_CODE (s2)
1531 || DECL_NAME (s1) != DECL_NAME (s2))
1532 break;
1533 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1534 enum_and_int_p, different_types_p);
1535 if (result == 0)
1536 break;
1537 if (result == 2)
1538 needs_warning = true;
1540 if (TREE_CODE (s1) == FIELD_DECL
1541 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1542 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1543 break;
1545 if (s1 && s2)
1546 tu->val = 0;
1547 else
1548 tu->val = needs_warning ? 2 : 1;
1549 return tu->val;
1552 default:
1553 gcc_unreachable ();
1557 /* Return 1 if two function types F1 and F2 are compatible.
1558 If either type specifies no argument types,
1559 the other must specify a fixed number of self-promoting arg types.
1560 Otherwise, if one type specifies only the number of arguments,
1561 the other must specify that number of self-promoting arg types.
1562 Otherwise, the argument types must match.
1563 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1565 static int
1566 function_types_compatible_p (const_tree f1, const_tree f2,
1567 bool *enum_and_int_p, bool *different_types_p)
1569 tree args1, args2;
1570 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1571 int val = 1;
1572 int val1;
1573 tree ret1, ret2;
1575 ret1 = TREE_TYPE (f1);
1576 ret2 = TREE_TYPE (f2);
1578 /* 'volatile' qualifiers on a function's return type used to mean
1579 the function is noreturn. */
1580 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1581 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1582 if (TYPE_VOLATILE (ret1))
1583 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1584 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1585 if (TYPE_VOLATILE (ret2))
1586 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1587 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1588 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1589 if (val == 0)
1590 return 0;
1592 args1 = TYPE_ARG_TYPES (f1);
1593 args2 = TYPE_ARG_TYPES (f2);
1595 if (different_types_p != NULL
1596 && (args1 == 0) != (args2 == 0))
1597 *different_types_p = true;
1599 /* An unspecified parmlist matches any specified parmlist
1600 whose argument types don't need default promotions. */
1602 if (args1 == 0)
1604 if (!self_promoting_args_p (args2))
1605 return 0;
1606 /* If one of these types comes from a non-prototype fn definition,
1607 compare that with the other type's arglist.
1608 If they don't match, ask for a warning (but no error). */
1609 if (TYPE_ACTUAL_ARG_TYPES (f1)
1610 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1611 enum_and_int_p, different_types_p))
1612 val = 2;
1613 return val;
1615 if (args2 == 0)
1617 if (!self_promoting_args_p (args1))
1618 return 0;
1619 if (TYPE_ACTUAL_ARG_TYPES (f2)
1620 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1621 enum_and_int_p, different_types_p))
1622 val = 2;
1623 return val;
1626 /* Both types have argument lists: compare them and propagate results. */
1627 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1628 different_types_p);
1629 return val1 != 1 ? val1 : val;
1632 /* Check two lists of types for compatibility, returning 0 for
1633 incompatible, 1 for compatible, or 2 for compatible with
1634 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1635 comptypes_internal. */
1637 static int
1638 type_lists_compatible_p (const_tree args1, const_tree args2,
1639 bool *enum_and_int_p, bool *different_types_p)
1641 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1642 int val = 1;
1643 int newval = 0;
1645 while (1)
1647 tree a1, mv1, a2, mv2;
1648 if (args1 == 0 && args2 == 0)
1649 return val;
1650 /* If one list is shorter than the other,
1651 they fail to match. */
1652 if (args1 == 0 || args2 == 0)
1653 return 0;
1654 mv1 = a1 = TREE_VALUE (args1);
1655 mv2 = a2 = TREE_VALUE (args2);
1656 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1657 mv1 = (TYPE_ATOMIC (mv1)
1658 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1659 TYPE_QUAL_ATOMIC)
1660 : TYPE_MAIN_VARIANT (mv1));
1661 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1662 mv2 = (TYPE_ATOMIC (mv2)
1663 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1664 TYPE_QUAL_ATOMIC)
1665 : TYPE_MAIN_VARIANT (mv2));
1666 /* A null pointer instead of a type
1667 means there is supposed to be an argument
1668 but nothing is specified about what type it has.
1669 So match anything that self-promotes. */
1670 if (different_types_p != NULL
1671 && (a1 == 0) != (a2 == 0))
1672 *different_types_p = true;
1673 if (a1 == 0)
1675 if (c_type_promotes_to (a2) != a2)
1676 return 0;
1678 else if (a2 == 0)
1680 if (c_type_promotes_to (a1) != a1)
1681 return 0;
1683 /* If one of the lists has an error marker, ignore this arg. */
1684 else if (TREE_CODE (a1) == ERROR_MARK
1685 || TREE_CODE (a2) == ERROR_MARK)
1687 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1688 different_types_p)))
1690 if (different_types_p != NULL)
1691 *different_types_p = true;
1692 /* Allow wait (union {union wait *u; int *i} *)
1693 and wait (union wait *) to be compatible. */
1694 if (TREE_CODE (a1) == UNION_TYPE
1695 && (TYPE_NAME (a1) == 0
1696 || TYPE_TRANSPARENT_AGGR (a1))
1697 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1698 && tree_int_cst_equal (TYPE_SIZE (a1),
1699 TYPE_SIZE (a2)))
1701 tree memb;
1702 for (memb = TYPE_FIELDS (a1);
1703 memb; memb = DECL_CHAIN (memb))
1705 tree mv3 = TREE_TYPE (memb);
1706 if (mv3 && mv3 != error_mark_node
1707 && TREE_CODE (mv3) != ARRAY_TYPE)
1708 mv3 = (TYPE_ATOMIC (mv3)
1709 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1710 TYPE_QUAL_ATOMIC)
1711 : TYPE_MAIN_VARIANT (mv3));
1712 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1713 different_types_p))
1714 break;
1716 if (memb == 0)
1717 return 0;
1719 else if (TREE_CODE (a2) == UNION_TYPE
1720 && (TYPE_NAME (a2) == 0
1721 || TYPE_TRANSPARENT_AGGR (a2))
1722 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1723 && tree_int_cst_equal (TYPE_SIZE (a2),
1724 TYPE_SIZE (a1)))
1726 tree memb;
1727 for (memb = TYPE_FIELDS (a2);
1728 memb; memb = DECL_CHAIN (memb))
1730 tree mv3 = TREE_TYPE (memb);
1731 if (mv3 && mv3 != error_mark_node
1732 && TREE_CODE (mv3) != ARRAY_TYPE)
1733 mv3 = (TYPE_ATOMIC (mv3)
1734 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1735 TYPE_QUAL_ATOMIC)
1736 : TYPE_MAIN_VARIANT (mv3));
1737 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1738 different_types_p))
1739 break;
1741 if (memb == 0)
1742 return 0;
1744 else
1745 return 0;
1748 /* comptypes said ok, but record if it said to warn. */
1749 if (newval > val)
1750 val = newval;
1752 args1 = TREE_CHAIN (args1);
1753 args2 = TREE_CHAIN (args2);
1757 /* Compute the size to increment a pointer by. When a function type or void
1758 type or incomplete type is passed, size_one_node is returned.
1759 This function does not emit any diagnostics; the caller is responsible
1760 for that. */
1762 static tree
1763 c_size_in_bytes (const_tree type)
1765 enum tree_code code = TREE_CODE (type);
1767 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1768 || !COMPLETE_TYPE_P (type))
1769 return size_one_node;
1771 /* Convert in case a char is more than one unit. */
1772 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1773 size_int (TYPE_PRECISION (char_type_node)
1774 / BITS_PER_UNIT));
1777 /* Return either DECL or its known constant value (if it has one). */
1779 tree
1780 decl_constant_value (tree decl)
1782 if (/* Don't change a variable array bound or initial value to a constant
1783 in a place where a variable is invalid. Note that DECL_INITIAL
1784 isn't valid for a PARM_DECL. */
1785 current_function_decl != 0
1786 && TREE_CODE (decl) != PARM_DECL
1787 && !TREE_THIS_VOLATILE (decl)
1788 && TREE_READONLY (decl)
1789 && DECL_INITIAL (decl) != 0
1790 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1791 /* This is invalid if initial value is not constant.
1792 If it has either a function call, a memory reference,
1793 or a variable, then re-evaluating it could give different results. */
1794 && TREE_CONSTANT (DECL_INITIAL (decl))
1795 /* Check for cases where this is sub-optimal, even though valid. */
1796 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1797 return DECL_INITIAL (decl);
1798 return decl;
1801 /* Convert the array expression EXP to a pointer. */
1802 static tree
1803 array_to_pointer_conversion (location_t loc, tree exp)
1805 tree orig_exp = exp;
1806 tree type = TREE_TYPE (exp);
1807 tree adr;
1808 tree restype = TREE_TYPE (type);
1809 tree ptrtype;
1811 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1813 STRIP_TYPE_NOPS (exp);
1815 if (TREE_NO_WARNING (orig_exp))
1816 TREE_NO_WARNING (exp) = 1;
1818 ptrtype = build_pointer_type (restype);
1820 if (TREE_CODE (exp) == INDIRECT_REF)
1821 return convert (ptrtype, TREE_OPERAND (exp, 0));
1823 /* In C++ array compound literals are temporary objects unless they are
1824 const or appear in namespace scope, so they are destroyed too soon
1825 to use them for much of anything (c++/53220). */
1826 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1828 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1829 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1830 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1831 "converting an array compound literal to a pointer "
1832 "is ill-formed in C++");
1835 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1836 return convert (ptrtype, adr);
1839 /* Convert the function expression EXP to a pointer. */
1840 static tree
1841 function_to_pointer_conversion (location_t loc, tree exp)
1843 tree orig_exp = exp;
1845 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1847 STRIP_TYPE_NOPS (exp);
1849 if (TREE_NO_WARNING (orig_exp))
1850 TREE_NO_WARNING (exp) = 1;
1852 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1855 /* Mark EXP as read, not just set, for set but not used -Wunused
1856 warning purposes. */
1858 void
1859 mark_exp_read (tree exp)
1861 switch (TREE_CODE (exp))
1863 case VAR_DECL:
1864 case PARM_DECL:
1865 DECL_READ_P (exp) = 1;
1866 break;
1867 case ARRAY_REF:
1868 case COMPONENT_REF:
1869 case MODIFY_EXPR:
1870 case REALPART_EXPR:
1871 case IMAGPART_EXPR:
1872 CASE_CONVERT:
1873 case ADDR_EXPR:
1874 mark_exp_read (TREE_OPERAND (exp, 0));
1875 break;
1876 case COMPOUND_EXPR:
1877 case C_MAYBE_CONST_EXPR:
1878 mark_exp_read (TREE_OPERAND (exp, 1));
1879 break;
1880 default:
1881 break;
1885 /* Perform the default conversion of arrays and functions to pointers.
1886 Return the result of converting EXP. For any other expression, just
1887 return EXP.
1889 LOC is the location of the expression. */
1891 struct c_expr
1892 default_function_array_conversion (location_t loc, struct c_expr exp)
1894 tree orig_exp = exp.value;
1895 tree type = TREE_TYPE (exp.value);
1896 enum tree_code code = TREE_CODE (type);
1898 switch (code)
1900 case ARRAY_TYPE:
1902 bool not_lvalue = false;
1903 bool lvalue_array_p;
1905 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1906 || CONVERT_EXPR_P (exp.value))
1907 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1909 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1910 not_lvalue = true;
1911 exp.value = TREE_OPERAND (exp.value, 0);
1914 if (TREE_NO_WARNING (orig_exp))
1915 TREE_NO_WARNING (exp.value) = 1;
1917 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1918 if (!flag_isoc99 && !lvalue_array_p)
1920 /* Before C99, non-lvalue arrays do not decay to pointers.
1921 Normally, using such an array would be invalid; but it can
1922 be used correctly inside sizeof or as a statement expression.
1923 Thus, do not give an error here; an error will result later. */
1924 return exp;
1927 exp.value = array_to_pointer_conversion (loc, exp.value);
1929 break;
1930 case FUNCTION_TYPE:
1931 exp.value = function_to_pointer_conversion (loc, exp.value);
1932 break;
1933 default:
1934 break;
1937 return exp;
1940 struct c_expr
1941 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1943 mark_exp_read (exp.value);
1944 return default_function_array_conversion (loc, exp);
1947 /* Return whether EXPR should be treated as an atomic lvalue for the
1948 purposes of load and store handling. */
1950 static bool
1951 really_atomic_lvalue (tree expr)
1953 if (expr == error_mark_node || TREE_TYPE (expr) == error_mark_node)
1954 return false;
1955 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1956 return false;
1957 if (!lvalue_p (expr))
1958 return false;
1960 /* Ignore _Atomic on register variables, since their addresses can't
1961 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1962 sequences wouldn't work. Ignore _Atomic on structures containing
1963 bit-fields, since accessing elements of atomic structures or
1964 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1965 it's undefined at translation time or execution time, and the
1966 normal atomic sequences again wouldn't work. */
1967 while (handled_component_p (expr))
1969 if (TREE_CODE (expr) == COMPONENT_REF
1970 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1971 return false;
1972 expr = TREE_OPERAND (expr, 0);
1974 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1975 return false;
1976 return true;
1979 /* Convert expression EXP (location LOC) from lvalue to rvalue,
1980 including converting functions and arrays to pointers if CONVERT_P.
1981 If READ_P, also mark the expression as having been read. */
1983 struct c_expr
1984 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1985 bool convert_p, bool read_p)
1987 if (read_p)
1988 mark_exp_read (exp.value);
1989 if (convert_p)
1990 exp = default_function_array_conversion (loc, exp);
1991 if (really_atomic_lvalue (exp.value))
1993 vec<tree, va_gc> *params;
1994 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
1995 tree expr_type = TREE_TYPE (exp.value);
1996 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
1997 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
1999 gcc_assert (TYPE_ATOMIC (expr_type));
2001 /* Expansion of a generic atomic load may require an addition
2002 element, so allocate enough to prevent a resize. */
2003 vec_alloc (params, 4);
2005 /* Remove the qualifiers for the rest of the expressions and
2006 create the VAL temp variable to hold the RHS. */
2007 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2008 tmp = create_tmp_var (nonatomic_type, NULL);
2009 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2010 TREE_ADDRESSABLE (tmp) = 1;
2011 TREE_NO_WARNING (tmp) = 1;
2013 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2014 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2015 params->quick_push (expr_addr);
2016 params->quick_push (tmp_addr);
2017 params->quick_push (seq_cst);
2018 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2020 /* EXPR is always read. */
2021 mark_exp_read (exp.value);
2023 /* Return tmp which contains the value loaded. */
2024 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2026 return exp;
2029 /* EXP is an expression of integer type. Apply the integer promotions
2030 to it and return the promoted value. */
2032 tree
2033 perform_integral_promotions (tree exp)
2035 tree type = TREE_TYPE (exp);
2036 enum tree_code code = TREE_CODE (type);
2038 gcc_assert (INTEGRAL_TYPE_P (type));
2040 /* Normally convert enums to int,
2041 but convert wide enums to something wider. */
2042 if (code == ENUMERAL_TYPE)
2044 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2045 TYPE_PRECISION (integer_type_node)),
2046 ((TYPE_PRECISION (type)
2047 >= TYPE_PRECISION (integer_type_node))
2048 && TYPE_UNSIGNED (type)));
2050 return convert (type, exp);
2053 /* ??? This should no longer be needed now bit-fields have their
2054 proper types. */
2055 if (TREE_CODE (exp) == COMPONENT_REF
2056 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2057 /* If it's thinner than an int, promote it like a
2058 c_promoting_integer_type_p, otherwise leave it alone. */
2059 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2060 TYPE_PRECISION (integer_type_node)))
2061 return convert (integer_type_node, exp);
2063 if (c_promoting_integer_type_p (type))
2065 /* Preserve unsignedness if not really getting any wider. */
2066 if (TYPE_UNSIGNED (type)
2067 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2068 return convert (unsigned_type_node, exp);
2070 return convert (integer_type_node, exp);
2073 return exp;
2077 /* Perform default promotions for C data used in expressions.
2078 Enumeral types or short or char are converted to int.
2079 In addition, manifest constants symbols are replaced by their values. */
2081 tree
2082 default_conversion (tree exp)
2084 tree orig_exp;
2085 tree type = TREE_TYPE (exp);
2086 enum tree_code code = TREE_CODE (type);
2087 tree promoted_type;
2089 mark_exp_read (exp);
2091 /* Functions and arrays have been converted during parsing. */
2092 gcc_assert (code != FUNCTION_TYPE);
2093 if (code == ARRAY_TYPE)
2094 return exp;
2096 /* Constants can be used directly unless they're not loadable. */
2097 if (TREE_CODE (exp) == CONST_DECL)
2098 exp = DECL_INITIAL (exp);
2100 /* Strip no-op conversions. */
2101 orig_exp = exp;
2102 STRIP_TYPE_NOPS (exp);
2104 if (TREE_NO_WARNING (orig_exp))
2105 TREE_NO_WARNING (exp) = 1;
2107 if (code == VOID_TYPE)
2109 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2110 "void value not ignored as it ought to be");
2111 return error_mark_node;
2114 exp = require_complete_type (exp);
2115 if (exp == error_mark_node)
2116 return error_mark_node;
2118 promoted_type = targetm.promoted_type (type);
2119 if (promoted_type)
2120 return convert (promoted_type, exp);
2122 if (INTEGRAL_TYPE_P (type))
2123 return perform_integral_promotions (exp);
2125 return exp;
2128 /* Look up COMPONENT in a structure or union TYPE.
2130 If the component name is not found, returns NULL_TREE. Otherwise,
2131 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2132 stepping down the chain to the component, which is in the last
2133 TREE_VALUE of the list. Normally the list is of length one, but if
2134 the component is embedded within (nested) anonymous structures or
2135 unions, the list steps down the chain to the component. */
2137 static tree
2138 lookup_field (tree type, tree component)
2140 tree field;
2142 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2143 to the field elements. Use a binary search on this array to quickly
2144 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2145 will always be set for structures which have many elements. */
2147 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2149 int bot, top, half;
2150 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2152 field = TYPE_FIELDS (type);
2153 bot = 0;
2154 top = TYPE_LANG_SPECIFIC (type)->s->len;
2155 while (top - bot > 1)
2157 half = (top - bot + 1) >> 1;
2158 field = field_array[bot+half];
2160 if (DECL_NAME (field) == NULL_TREE)
2162 /* Step through all anon unions in linear fashion. */
2163 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2165 field = field_array[bot++];
2166 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2167 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2169 tree anon = lookup_field (TREE_TYPE (field), component);
2171 if (anon)
2172 return tree_cons (NULL_TREE, field, anon);
2174 /* The Plan 9 compiler permits referring
2175 directly to an anonymous struct/union field
2176 using a typedef name. */
2177 if (flag_plan9_extensions
2178 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2179 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2180 == TYPE_DECL)
2181 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2182 == component))
2183 break;
2187 /* Entire record is only anon unions. */
2188 if (bot > top)
2189 return NULL_TREE;
2191 /* Restart the binary search, with new lower bound. */
2192 continue;
2195 if (DECL_NAME (field) == component)
2196 break;
2197 if (DECL_NAME (field) < component)
2198 bot += half;
2199 else
2200 top = bot + half;
2203 if (DECL_NAME (field_array[bot]) == component)
2204 field = field_array[bot];
2205 else if (DECL_NAME (field) != component)
2206 return NULL_TREE;
2208 else
2210 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2212 if (DECL_NAME (field) == NULL_TREE
2213 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2214 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2216 tree anon = lookup_field (TREE_TYPE (field), component);
2218 if (anon)
2219 return tree_cons (NULL_TREE, field, anon);
2221 /* The Plan 9 compiler permits referring directly to an
2222 anonymous struct/union field using a typedef
2223 name. */
2224 if (flag_plan9_extensions
2225 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2226 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2227 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2228 == component))
2229 break;
2232 if (DECL_NAME (field) == component)
2233 break;
2236 if (field == NULL_TREE)
2237 return NULL_TREE;
2240 return tree_cons (NULL_TREE, field, NULL_TREE);
2243 /* Make an expression to refer to the COMPONENT field of structure or
2244 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2245 location of the COMPONENT_REF. */
2247 tree
2248 build_component_ref (location_t loc, tree datum, tree component)
2250 tree type = TREE_TYPE (datum);
2251 enum tree_code code = TREE_CODE (type);
2252 tree field = NULL;
2253 tree ref;
2254 bool datum_lvalue = lvalue_p (datum);
2256 if (!objc_is_public (datum, component))
2257 return error_mark_node;
2259 /* Detect Objective-C property syntax object.property. */
2260 if (c_dialect_objc ()
2261 && (ref = objc_maybe_build_component_ref (datum, component)))
2262 return ref;
2264 /* See if there is a field or component with name COMPONENT. */
2266 if (code == RECORD_TYPE || code == UNION_TYPE)
2268 if (!COMPLETE_TYPE_P (type))
2270 c_incomplete_type_error (NULL_TREE, type);
2271 return error_mark_node;
2274 field = lookup_field (type, component);
2276 if (!field)
2278 error_at (loc, "%qT has no member named %qE", type, component);
2279 return error_mark_node;
2282 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2283 This might be better solved in future the way the C++ front
2284 end does it - by giving the anonymous entities each a
2285 separate name and type, and then have build_component_ref
2286 recursively call itself. We can't do that here. */
2289 tree subdatum = TREE_VALUE (field);
2290 int quals;
2291 tree subtype;
2292 bool use_datum_quals;
2294 if (TREE_TYPE (subdatum) == error_mark_node)
2295 return error_mark_node;
2297 /* If this is an rvalue, it does not have qualifiers in C
2298 standard terms and we must avoid propagating such
2299 qualifiers down to a non-lvalue array that is then
2300 converted to a pointer. */
2301 use_datum_quals = (datum_lvalue
2302 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2304 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2305 if (use_datum_quals)
2306 quals |= TYPE_QUALS (TREE_TYPE (datum));
2307 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2309 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2310 NULL_TREE);
2311 SET_EXPR_LOCATION (ref, loc);
2312 if (TREE_READONLY (subdatum)
2313 || (use_datum_quals && TREE_READONLY (datum)))
2314 TREE_READONLY (ref) = 1;
2315 if (TREE_THIS_VOLATILE (subdatum)
2316 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2317 TREE_THIS_VOLATILE (ref) = 1;
2319 if (TREE_DEPRECATED (subdatum))
2320 warn_deprecated_use (subdatum, NULL_TREE);
2322 datum = ref;
2324 field = TREE_CHAIN (field);
2326 while (field);
2328 return ref;
2330 else if (code != ERROR_MARK)
2331 error_at (loc,
2332 "request for member %qE in something not a structure or union",
2333 component);
2335 return error_mark_node;
2338 /* Given an expression PTR for a pointer, return an expression
2339 for the value pointed to.
2340 ERRORSTRING is the name of the operator to appear in error messages.
2342 LOC is the location to use for the generated tree. */
2344 tree
2345 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2347 tree pointer = default_conversion (ptr);
2348 tree type = TREE_TYPE (pointer);
2349 tree ref;
2351 if (TREE_CODE (type) == POINTER_TYPE)
2353 if (CONVERT_EXPR_P (pointer)
2354 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2356 /* If a warning is issued, mark it to avoid duplicates from
2357 the backend. This only needs to be done at
2358 warn_strict_aliasing > 2. */
2359 if (warn_strict_aliasing > 2)
2360 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2361 type, TREE_OPERAND (pointer, 0)))
2362 TREE_NO_WARNING (pointer) = 1;
2365 if (TREE_CODE (pointer) == ADDR_EXPR
2366 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2367 == TREE_TYPE (type)))
2369 ref = TREE_OPERAND (pointer, 0);
2370 protected_set_expr_location (ref, loc);
2371 return ref;
2373 else
2375 tree t = TREE_TYPE (type);
2377 ref = build1 (INDIRECT_REF, t, pointer);
2379 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2381 error_at (loc, "dereferencing pointer to incomplete type");
2382 return error_mark_node;
2384 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2385 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2387 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2388 so that we get the proper error message if the result is used
2389 to assign to. Also, &* is supposed to be a no-op.
2390 And ANSI C seems to specify that the type of the result
2391 should be the const type. */
2392 /* A de-reference of a pointer to const is not a const. It is valid
2393 to change it via some other pointer. */
2394 TREE_READONLY (ref) = TYPE_READONLY (t);
2395 TREE_SIDE_EFFECTS (ref)
2396 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2397 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2398 protected_set_expr_location (ref, loc);
2399 return ref;
2402 else if (TREE_CODE (pointer) != ERROR_MARK)
2403 invalid_indirection_error (loc, type, errstring);
2405 return error_mark_node;
2408 /* This handles expressions of the form "a[i]", which denotes
2409 an array reference.
2411 This is logically equivalent in C to *(a+i), but we may do it differently.
2412 If A is a variable or a member, we generate a primitive ARRAY_REF.
2413 This avoids forcing the array out of registers, and can work on
2414 arrays that are not lvalues (for example, members of structures returned
2415 by functions).
2417 For vector types, allow vector[i] but not i[vector], and create
2418 *(((type*)&vectortype) + i) for the expression.
2420 LOC is the location to use for the returned expression. */
2422 tree
2423 build_array_ref (location_t loc, tree array, tree index)
2425 tree ret;
2426 bool swapped = false;
2427 if (TREE_TYPE (array) == error_mark_node
2428 || TREE_TYPE (index) == error_mark_node)
2429 return error_mark_node;
2431 if (flag_cilkplus && contains_array_notation_expr (index))
2433 size_t rank = 0;
2434 if (!find_rank (loc, index, index, true, &rank))
2435 return error_mark_node;
2436 if (rank > 1)
2438 error_at (loc, "rank of the array's index is greater than 1");
2439 return error_mark_node;
2442 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2443 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2444 /* Allow vector[index] but not index[vector]. */
2445 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2447 tree temp;
2448 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2449 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2451 error_at (loc,
2452 "subscripted value is neither array nor pointer nor vector");
2454 return error_mark_node;
2456 temp = array;
2457 array = index;
2458 index = temp;
2459 swapped = true;
2462 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2464 error_at (loc, "array subscript is not an integer");
2465 return error_mark_node;
2468 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2470 error_at (loc, "subscripted value is pointer to function");
2471 return error_mark_node;
2474 /* ??? Existing practice has been to warn only when the char
2475 index is syntactically the index, not for char[array]. */
2476 if (!swapped)
2477 warn_array_subscript_with_type_char (index);
2479 /* Apply default promotions *after* noticing character types. */
2480 index = default_conversion (index);
2482 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2484 convert_vector_to_pointer_for_subscript (loc, &array, index);
2486 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2488 tree rval, type;
2490 /* An array that is indexed by a non-constant
2491 cannot be stored in a register; we must be able to do
2492 address arithmetic on its address.
2493 Likewise an array of elements of variable size. */
2494 if (TREE_CODE (index) != INTEGER_CST
2495 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2496 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2498 if (!c_mark_addressable (array))
2499 return error_mark_node;
2501 /* An array that is indexed by a constant value which is not within
2502 the array bounds cannot be stored in a register either; because we
2503 would get a crash in store_bit_field/extract_bit_field when trying
2504 to access a non-existent part of the register. */
2505 if (TREE_CODE (index) == INTEGER_CST
2506 && TYPE_DOMAIN (TREE_TYPE (array))
2507 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2509 if (!c_mark_addressable (array))
2510 return error_mark_node;
2513 if (pedantic || warn_c90_c99_compat)
2515 tree foo = array;
2516 while (TREE_CODE (foo) == COMPONENT_REF)
2517 foo = TREE_OPERAND (foo, 0);
2518 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2519 pedwarn (loc, OPT_Wpedantic,
2520 "ISO C forbids subscripting %<register%> array");
2521 else if (!lvalue_p (foo))
2522 pedwarn_c90 (loc, OPT_Wpedantic,
2523 "ISO C90 forbids subscripting non-lvalue "
2524 "array");
2527 type = TREE_TYPE (TREE_TYPE (array));
2528 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2529 /* Array ref is const/volatile if the array elements are
2530 or if the array is. */
2531 TREE_READONLY (rval)
2532 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2533 | TREE_READONLY (array));
2534 TREE_SIDE_EFFECTS (rval)
2535 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2536 | TREE_SIDE_EFFECTS (array));
2537 TREE_THIS_VOLATILE (rval)
2538 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2539 /* This was added by rms on 16 Nov 91.
2540 It fixes vol struct foo *a; a->elts[1]
2541 in an inline function.
2542 Hope it doesn't break something else. */
2543 | TREE_THIS_VOLATILE (array));
2544 ret = require_complete_type (rval);
2545 protected_set_expr_location (ret, loc);
2546 return ret;
2548 else
2550 tree ar = default_conversion (array);
2552 if (ar == error_mark_node)
2553 return ar;
2555 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2556 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2558 return build_indirect_ref
2559 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2560 RO_ARRAY_INDEXING);
2564 /* Build an external reference to identifier ID. FUN indicates
2565 whether this will be used for a function call. LOC is the source
2566 location of the identifier. This sets *TYPE to the type of the
2567 identifier, which is not the same as the type of the returned value
2568 for CONST_DECLs defined as enum constants. If the type of the
2569 identifier is not available, *TYPE is set to NULL. */
2570 tree
2571 build_external_ref (location_t loc, tree id, int fun, tree *type)
2573 tree ref;
2574 tree decl = lookup_name (id);
2576 /* In Objective-C, an instance variable (ivar) may be preferred to
2577 whatever lookup_name() found. */
2578 decl = objc_lookup_ivar (decl, id);
2580 *type = NULL;
2581 if (decl && decl != error_mark_node)
2583 ref = decl;
2584 *type = TREE_TYPE (ref);
2586 else if (fun)
2587 /* Implicit function declaration. */
2588 ref = implicitly_declare (loc, id);
2589 else if (decl == error_mark_node)
2590 /* Don't complain about something that's already been
2591 complained about. */
2592 return error_mark_node;
2593 else
2595 undeclared_variable (loc, id);
2596 return error_mark_node;
2599 if (TREE_TYPE (ref) == error_mark_node)
2600 return error_mark_node;
2602 if (TREE_DEPRECATED (ref))
2603 warn_deprecated_use (ref, NULL_TREE);
2605 /* Recursive call does not count as usage. */
2606 if (ref != current_function_decl)
2608 TREE_USED (ref) = 1;
2611 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2613 if (!in_sizeof && !in_typeof)
2614 C_DECL_USED (ref) = 1;
2615 else if (DECL_INITIAL (ref) == 0
2616 && DECL_EXTERNAL (ref)
2617 && !TREE_PUBLIC (ref))
2618 record_maybe_used_decl (ref);
2621 if (TREE_CODE (ref) == CONST_DECL)
2623 used_types_insert (TREE_TYPE (ref));
2625 if (warn_cxx_compat
2626 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2627 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2629 warning_at (loc, OPT_Wc___compat,
2630 ("enum constant defined in struct or union "
2631 "is not visible in C++"));
2632 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2635 ref = DECL_INITIAL (ref);
2636 TREE_CONSTANT (ref) = 1;
2638 else if (current_function_decl != 0
2639 && !DECL_FILE_SCOPE_P (current_function_decl)
2640 && (TREE_CODE (ref) == VAR_DECL
2641 || TREE_CODE (ref) == PARM_DECL
2642 || TREE_CODE (ref) == FUNCTION_DECL))
2644 tree context = decl_function_context (ref);
2646 if (context != 0 && context != current_function_decl)
2647 DECL_NONLOCAL (ref) = 1;
2649 /* C99 6.7.4p3: An inline definition of a function with external
2650 linkage ... shall not contain a reference to an identifier with
2651 internal linkage. */
2652 else if (current_function_decl != 0
2653 && DECL_DECLARED_INLINE_P (current_function_decl)
2654 && DECL_EXTERNAL (current_function_decl)
2655 && VAR_OR_FUNCTION_DECL_P (ref)
2656 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2657 && ! TREE_PUBLIC (ref)
2658 && DECL_CONTEXT (ref) != current_function_decl)
2659 record_inline_static (loc, current_function_decl, ref,
2660 csi_internal);
2662 return ref;
2665 /* Record details of decls possibly used inside sizeof or typeof. */
2666 struct maybe_used_decl
2668 /* The decl. */
2669 tree decl;
2670 /* The level seen at (in_sizeof + in_typeof). */
2671 int level;
2672 /* The next one at this level or above, or NULL. */
2673 struct maybe_used_decl *next;
2676 static struct maybe_used_decl *maybe_used_decls;
2678 /* Record that DECL, an undefined static function reference seen
2679 inside sizeof or typeof, might be used if the operand of sizeof is
2680 a VLA type or the operand of typeof is a variably modified
2681 type. */
2683 static void
2684 record_maybe_used_decl (tree decl)
2686 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2687 t->decl = decl;
2688 t->level = in_sizeof + in_typeof;
2689 t->next = maybe_used_decls;
2690 maybe_used_decls = t;
2693 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2694 USED is false, just discard them. If it is true, mark them used
2695 (if no longer inside sizeof or typeof) or move them to the next
2696 level up (if still inside sizeof or typeof). */
2698 void
2699 pop_maybe_used (bool used)
2701 struct maybe_used_decl *p = maybe_used_decls;
2702 int cur_level = in_sizeof + in_typeof;
2703 while (p && p->level > cur_level)
2705 if (used)
2707 if (cur_level == 0)
2708 C_DECL_USED (p->decl) = 1;
2709 else
2710 p->level = cur_level;
2712 p = p->next;
2714 if (!used || cur_level == 0)
2715 maybe_used_decls = p;
2718 /* Return the result of sizeof applied to EXPR. */
2720 struct c_expr
2721 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2723 struct c_expr ret;
2724 if (expr.value == error_mark_node)
2726 ret.value = error_mark_node;
2727 ret.original_code = ERROR_MARK;
2728 ret.original_type = NULL;
2729 pop_maybe_used (false);
2731 else
2733 bool expr_const_operands = true;
2735 if (TREE_CODE (expr.value) == PARM_DECL
2736 && C_ARRAY_PARAMETER (expr.value))
2738 if (warning_at (loc, OPT_Wsizeof_array_argument,
2739 "%<sizeof%> on array function parameter %qE will "
2740 "return size of %qT", expr.value,
2741 expr.original_type))
2742 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2744 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2745 &expr_const_operands);
2746 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2747 c_last_sizeof_arg = expr.value;
2748 ret.original_code = SIZEOF_EXPR;
2749 ret.original_type = NULL;
2750 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2752 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2753 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2754 folded_expr, ret.value);
2755 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2756 SET_EXPR_LOCATION (ret.value, loc);
2758 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2760 return ret;
2763 /* Return the result of sizeof applied to T, a structure for the type
2764 name passed to sizeof (rather than the type itself). LOC is the
2765 location of the original expression. */
2767 struct c_expr
2768 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2770 tree type;
2771 struct c_expr ret;
2772 tree type_expr = NULL_TREE;
2773 bool type_expr_const = true;
2774 type = groktypename (t, &type_expr, &type_expr_const);
2775 ret.value = c_sizeof (loc, type);
2776 c_last_sizeof_arg = type;
2777 ret.original_code = SIZEOF_EXPR;
2778 ret.original_type = NULL;
2779 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2780 && c_vla_type_p (type))
2782 /* If the type is a [*] array, it is a VLA but is represented as
2783 having a size of zero. In such a case we must ensure that
2784 the result of sizeof does not get folded to a constant by
2785 c_fully_fold, because if the size is evaluated the result is
2786 not constant and so constraints on zero or negative size
2787 arrays must not be applied when this sizeof call is inside
2788 another array declarator. */
2789 if (!type_expr)
2790 type_expr = integer_zero_node;
2791 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2792 type_expr, ret.value);
2793 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2795 pop_maybe_used (type != error_mark_node
2796 ? C_TYPE_VARIABLE_SIZE (type) : false);
2797 return ret;
2800 /* Build a function call to function FUNCTION with parameters PARAMS.
2801 The function call is at LOC.
2802 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2803 TREE_VALUE of each node is a parameter-expression.
2804 FUNCTION's data type may be a function type or a pointer-to-function. */
2806 tree
2807 build_function_call (location_t loc, tree function, tree params)
2809 vec<tree, va_gc> *v;
2810 tree ret;
2812 vec_alloc (v, list_length (params));
2813 for (; params; params = TREE_CHAIN (params))
2814 v->quick_push (TREE_VALUE (params));
2815 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2816 vec_free (v);
2817 return ret;
2820 /* Give a note about the location of the declaration of DECL. */
2822 static void inform_declaration (tree decl)
2824 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2825 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2828 /* Build a function call to function FUNCTION with parameters PARAMS.
2829 ORIGTYPES, if not NULL, is a vector of types; each element is
2830 either NULL or the original type of the corresponding element in
2831 PARAMS. The original type may differ from TREE_TYPE of the
2832 parameter for enums. FUNCTION's data type may be a function type
2833 or pointer-to-function. This function changes the elements of
2834 PARAMS. */
2836 tree
2837 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2838 tree function, vec<tree, va_gc> *params,
2839 vec<tree, va_gc> *origtypes)
2841 tree fntype, fundecl = 0;
2842 tree name = NULL_TREE, result;
2843 tree tem;
2844 int nargs;
2845 tree *argarray;
2848 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2849 STRIP_TYPE_NOPS (function);
2851 /* Convert anything with function type to a pointer-to-function. */
2852 if (TREE_CODE (function) == FUNCTION_DECL)
2854 name = DECL_NAME (function);
2856 if (flag_tm)
2857 tm_malloc_replacement (function);
2858 fundecl = function;
2859 /* Atomic functions have type checking/casting already done. They are
2860 often rewritten and don't match the original parameter list. */
2861 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2862 origtypes = NULL;
2864 if (flag_cilkplus
2865 && is_cilkplus_reduce_builtin (function))
2866 origtypes = NULL;
2868 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2869 function = function_to_pointer_conversion (loc, function);
2871 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2872 expressions, like those used for ObjC messenger dispatches. */
2873 if (params && !params->is_empty ())
2874 function = objc_rewrite_function_call (function, (*params)[0]);
2876 function = c_fully_fold (function, false, NULL);
2878 fntype = TREE_TYPE (function);
2880 if (TREE_CODE (fntype) == ERROR_MARK)
2881 return error_mark_node;
2883 if (!(TREE_CODE (fntype) == POINTER_TYPE
2884 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2886 if (!flag_diagnostics_show_caret)
2887 error_at (loc,
2888 "called object %qE is not a function or function pointer",
2889 function);
2890 else if (DECL_P (function))
2892 error_at (loc,
2893 "called object %qD is not a function or function pointer",
2894 function);
2895 inform_declaration (function);
2897 else
2898 error_at (loc,
2899 "called object is not a function or function pointer");
2900 return error_mark_node;
2903 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2904 current_function_returns_abnormally = 1;
2906 /* fntype now gets the type of function pointed to. */
2907 fntype = TREE_TYPE (fntype);
2909 /* Convert the parameters to the types declared in the
2910 function prototype, or apply default promotions. */
2912 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2913 origtypes, function, fundecl);
2914 if (nargs < 0)
2915 return error_mark_node;
2917 /* Check that the function is called through a compatible prototype.
2918 If it is not, warn. */
2919 if (CONVERT_EXPR_P (function)
2920 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2921 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2922 && !comptypes (fntype, TREE_TYPE (tem)))
2924 tree return_type = TREE_TYPE (fntype);
2926 /* This situation leads to run-time undefined behavior. We can't,
2927 therefore, simply error unless we can prove that all possible
2928 executions of the program must execute the code. */
2929 warning_at (loc, 0, "function called through a non-compatible type");
2931 if (VOID_TYPE_P (return_type)
2932 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2933 pedwarn (loc, 0,
2934 "function with qualified void return type called");
2937 argarray = vec_safe_address (params);
2939 /* Check that arguments to builtin functions match the expectations. */
2940 if (fundecl
2941 && DECL_BUILT_IN (fundecl)
2942 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2943 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2944 return error_mark_node;
2946 /* Check that the arguments to the function are valid. */
2947 check_function_arguments (fntype, nargs, argarray);
2949 if (name != NULL_TREE
2950 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2952 if (require_constant_value)
2953 result =
2954 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2955 function, nargs, argarray);
2956 else
2957 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2958 function, nargs, argarray);
2959 if (TREE_CODE (result) == NOP_EXPR
2960 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2961 STRIP_TYPE_NOPS (result);
2963 else
2964 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2965 function, nargs, argarray);
2967 if (VOID_TYPE_P (TREE_TYPE (result)))
2969 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2970 pedwarn (loc, 0,
2971 "function with qualified void return type called");
2972 return result;
2974 return require_complete_type (result);
2977 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2979 tree
2980 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2981 tree function, vec<tree, va_gc> *params,
2982 vec<tree, va_gc> *origtypes)
2984 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2985 STRIP_TYPE_NOPS (function);
2987 /* Convert anything with function type to a pointer-to-function. */
2988 if (TREE_CODE (function) == FUNCTION_DECL)
2990 /* Implement type-directed function overloading for builtins.
2991 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2992 handle all the type checking. The result is a complete expression
2993 that implements this function call. */
2994 tree tem = resolve_overloaded_builtin (loc, function, params);
2995 if (tem)
2996 return tem;
2998 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3001 /* Convert the argument expressions in the vector VALUES
3002 to the types in the list TYPELIST.
3004 If TYPELIST is exhausted, or when an element has NULL as its type,
3005 perform the default conversions.
3007 ORIGTYPES is the original types of the expressions in VALUES. This
3008 holds the type of enum values which have been converted to integral
3009 types. It may be NULL.
3011 FUNCTION is a tree for the called function. It is used only for
3012 error messages, where it is formatted with %qE.
3014 This is also where warnings about wrong number of args are generated.
3016 ARG_LOC are locations of function arguments (if any).
3018 Returns the actual number of arguments processed (which may be less
3019 than the length of VALUES in some error situations), or -1 on
3020 failure. */
3022 static int
3023 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3024 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3025 tree function, tree fundecl)
3027 tree typetail, val;
3028 unsigned int parmnum;
3029 bool error_args = false;
3030 const bool type_generic = fundecl
3031 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3032 bool type_generic_remove_excess_precision = false;
3033 tree selector;
3035 /* Change pointer to function to the function itself for
3036 diagnostics. */
3037 if (TREE_CODE (function) == ADDR_EXPR
3038 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3039 function = TREE_OPERAND (function, 0);
3041 /* Handle an ObjC selector specially for diagnostics. */
3042 selector = objc_message_selector ();
3044 /* For type-generic built-in functions, determine whether excess
3045 precision should be removed (classification) or not
3046 (comparison). */
3047 if (type_generic
3048 && DECL_BUILT_IN (fundecl)
3049 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3051 switch (DECL_FUNCTION_CODE (fundecl))
3053 case BUILT_IN_ISFINITE:
3054 case BUILT_IN_ISINF:
3055 case BUILT_IN_ISINF_SIGN:
3056 case BUILT_IN_ISNAN:
3057 case BUILT_IN_ISNORMAL:
3058 case BUILT_IN_FPCLASSIFY:
3059 type_generic_remove_excess_precision = true;
3060 break;
3062 default:
3063 type_generic_remove_excess_precision = false;
3064 break;
3067 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3068 return vec_safe_length (values);
3070 /* Scan the given expressions and types, producing individual
3071 converted arguments. */
3073 for (typetail = typelist, parmnum = 0;
3074 values && values->iterate (parmnum, &val);
3075 ++parmnum)
3077 tree type = typetail ? TREE_VALUE (typetail) : 0;
3078 tree valtype = TREE_TYPE (val);
3079 tree rname = function;
3080 int argnum = parmnum + 1;
3081 const char *invalid_func_diag;
3082 bool excess_precision = false;
3083 bool npc;
3084 tree parmval;
3085 /* Some __atomic_* builtins have additional hidden argument at
3086 position 0. */
3087 location_t ploc
3088 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3089 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3090 : input_location;
3092 if (type == void_type_node)
3094 if (selector)
3095 error_at (loc, "too many arguments to method %qE", selector);
3096 else
3097 error_at (loc, "too many arguments to function %qE", function);
3098 inform_declaration (fundecl);
3099 return parmnum;
3102 if (selector && argnum > 2)
3104 rname = selector;
3105 argnum -= 2;
3108 npc = null_pointer_constant_p (val);
3110 /* If there is excess precision and a prototype, convert once to
3111 the required type rather than converting via the semantic
3112 type. Likewise without a prototype a float value represented
3113 as long double should be converted once to double. But for
3114 type-generic classification functions excess precision must
3115 be removed here. */
3116 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3117 && (type || !type_generic || !type_generic_remove_excess_precision))
3119 val = TREE_OPERAND (val, 0);
3120 excess_precision = true;
3122 val = c_fully_fold (val, false, NULL);
3123 STRIP_TYPE_NOPS (val);
3125 val = require_complete_type (val);
3127 if (type != 0)
3129 /* Formal parm type is specified by a function prototype. */
3131 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3133 error_at (ploc, "type of formal parameter %d is incomplete",
3134 parmnum + 1);
3135 parmval = val;
3137 else
3139 tree origtype;
3141 /* Optionally warn about conversions that
3142 differ from the default conversions. */
3143 if (warn_traditional_conversion || warn_traditional)
3145 unsigned int formal_prec = TYPE_PRECISION (type);
3147 if (INTEGRAL_TYPE_P (type)
3148 && TREE_CODE (valtype) == REAL_TYPE)
3149 warning_at (ploc, OPT_Wtraditional_conversion,
3150 "passing argument %d of %qE as integer rather "
3151 "than floating due to prototype",
3152 argnum, rname);
3153 if (INTEGRAL_TYPE_P (type)
3154 && TREE_CODE (valtype) == COMPLEX_TYPE)
3155 warning_at (ploc, OPT_Wtraditional_conversion,
3156 "passing argument %d of %qE as integer rather "
3157 "than complex due to prototype",
3158 argnum, rname);
3159 else if (TREE_CODE (type) == COMPLEX_TYPE
3160 && TREE_CODE (valtype) == REAL_TYPE)
3161 warning_at (ploc, OPT_Wtraditional_conversion,
3162 "passing argument %d of %qE as complex rather "
3163 "than floating due to prototype",
3164 argnum, rname);
3165 else if (TREE_CODE (type) == REAL_TYPE
3166 && INTEGRAL_TYPE_P (valtype))
3167 warning_at (ploc, OPT_Wtraditional_conversion,
3168 "passing argument %d of %qE as floating rather "
3169 "than integer due to prototype",
3170 argnum, rname);
3171 else if (TREE_CODE (type) == COMPLEX_TYPE
3172 && INTEGRAL_TYPE_P (valtype))
3173 warning_at (ploc, OPT_Wtraditional_conversion,
3174 "passing argument %d of %qE as complex rather "
3175 "than integer due to prototype",
3176 argnum, rname);
3177 else if (TREE_CODE (type) == REAL_TYPE
3178 && TREE_CODE (valtype) == COMPLEX_TYPE)
3179 warning_at (ploc, OPT_Wtraditional_conversion,
3180 "passing argument %d of %qE as floating rather "
3181 "than complex due to prototype",
3182 argnum, rname);
3183 /* ??? At some point, messages should be written about
3184 conversions between complex types, but that's too messy
3185 to do now. */
3186 else if (TREE_CODE (type) == REAL_TYPE
3187 && TREE_CODE (valtype) == REAL_TYPE)
3189 /* Warn if any argument is passed as `float',
3190 since without a prototype it would be `double'. */
3191 if (formal_prec == TYPE_PRECISION (float_type_node)
3192 && type != dfloat32_type_node)
3193 warning_at (ploc, 0,
3194 "passing argument %d of %qE as %<float%> "
3195 "rather than %<double%> due to prototype",
3196 argnum, rname);
3198 /* Warn if mismatch between argument and prototype
3199 for decimal float types. Warn of conversions with
3200 binary float types and of precision narrowing due to
3201 prototype. */
3202 else if (type != valtype
3203 && (type == dfloat32_type_node
3204 || type == dfloat64_type_node
3205 || type == dfloat128_type_node
3206 || valtype == dfloat32_type_node
3207 || valtype == dfloat64_type_node
3208 || valtype == dfloat128_type_node)
3209 && (formal_prec
3210 <= TYPE_PRECISION (valtype)
3211 || (type == dfloat128_type_node
3212 && (valtype
3213 != dfloat64_type_node
3214 && (valtype
3215 != dfloat32_type_node)))
3216 || (type == dfloat64_type_node
3217 && (valtype
3218 != dfloat32_type_node))))
3219 warning_at (ploc, 0,
3220 "passing argument %d of %qE as %qT "
3221 "rather than %qT due to prototype",
3222 argnum, rname, type, valtype);
3225 /* Detect integer changing in width or signedness.
3226 These warnings are only activated with
3227 -Wtraditional-conversion, not with -Wtraditional. */
3228 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3229 && INTEGRAL_TYPE_P (valtype))
3231 tree would_have_been = default_conversion (val);
3232 tree type1 = TREE_TYPE (would_have_been);
3234 if (TREE_CODE (type) == ENUMERAL_TYPE
3235 && (TYPE_MAIN_VARIANT (type)
3236 == TYPE_MAIN_VARIANT (valtype)))
3237 /* No warning if function asks for enum
3238 and the actual arg is that enum type. */
3240 else if (formal_prec != TYPE_PRECISION (type1))
3241 warning_at (ploc, OPT_Wtraditional_conversion,
3242 "passing argument %d of %qE "
3243 "with different width due to prototype",
3244 argnum, rname);
3245 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3247 /* Don't complain if the formal parameter type
3248 is an enum, because we can't tell now whether
3249 the value was an enum--even the same enum. */
3250 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3252 else if (TREE_CODE (val) == INTEGER_CST
3253 && int_fits_type_p (val, type))
3254 /* Change in signedness doesn't matter
3255 if a constant value is unaffected. */
3257 /* If the value is extended from a narrower
3258 unsigned type, it doesn't matter whether we
3259 pass it as signed or unsigned; the value
3260 certainly is the same either way. */
3261 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3262 && TYPE_UNSIGNED (valtype))
3264 else if (TYPE_UNSIGNED (type))
3265 warning_at (ploc, OPT_Wtraditional_conversion,
3266 "passing argument %d of %qE "
3267 "as unsigned due to prototype",
3268 argnum, rname);
3269 else
3270 warning_at (ploc, OPT_Wtraditional_conversion,
3271 "passing argument %d of %qE "
3272 "as signed due to prototype",
3273 argnum, rname);
3277 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3278 sake of better warnings from convert_and_check. */
3279 if (excess_precision)
3280 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3281 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3282 parmval = convert_for_assignment (loc, ploc, type,
3283 val, origtype, ic_argpass,
3284 npc, fundecl, function,
3285 parmnum + 1);
3287 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3288 && INTEGRAL_TYPE_P (type)
3289 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3290 parmval = default_conversion (parmval);
3293 else if (TREE_CODE (valtype) == REAL_TYPE
3294 && (TYPE_PRECISION (valtype)
3295 <= TYPE_PRECISION (double_type_node))
3296 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3297 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3298 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3300 if (type_generic)
3301 parmval = val;
3302 else
3304 /* Convert `float' to `double'. */
3305 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3306 warning_at (ploc, OPT_Wdouble_promotion,
3307 "implicit conversion from %qT to %qT when passing "
3308 "argument to function",
3309 valtype, double_type_node);
3310 parmval = convert (double_type_node, val);
3313 else if (excess_precision && !type_generic)
3314 /* A "double" argument with excess precision being passed
3315 without a prototype or in variable arguments. */
3316 parmval = convert (valtype, val);
3317 else if ((invalid_func_diag =
3318 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3320 error (invalid_func_diag);
3321 return -1;
3323 else
3324 /* Convert `short' and `char' to full-size `int'. */
3325 parmval = default_conversion (val);
3327 (*values)[parmnum] = parmval;
3328 if (parmval == error_mark_node)
3329 error_args = true;
3331 if (typetail)
3332 typetail = TREE_CHAIN (typetail);
3335 gcc_assert (parmnum == vec_safe_length (values));
3337 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3339 error_at (loc, "too few arguments to function %qE", function);
3340 inform_declaration (fundecl);
3341 return -1;
3344 return error_args ? -1 : (int) parmnum;
3347 /* This is the entry point used by the parser to build unary operators
3348 in the input. CODE, a tree_code, specifies the unary operator, and
3349 ARG is the operand. For unary plus, the C parser currently uses
3350 CONVERT_EXPR for code.
3352 LOC is the location to use for the tree generated.
3355 struct c_expr
3356 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3358 struct c_expr result;
3360 result.value = build_unary_op (loc, code, arg.value, 0);
3361 result.original_code = code;
3362 result.original_type = NULL;
3364 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3365 overflow_warning (loc, result.value);
3367 return result;
3370 /* This is the entry point used by the parser to build binary operators
3371 in the input. CODE, a tree_code, specifies the binary operator, and
3372 ARG1 and ARG2 are the operands. In addition to constructing the
3373 expression, we check for operands that were written with other binary
3374 operators in a way that is likely to confuse the user.
3376 LOCATION is the location of the binary operator. */
3378 struct c_expr
3379 parser_build_binary_op (location_t location, enum tree_code code,
3380 struct c_expr arg1, struct c_expr arg2)
3382 struct c_expr result;
3384 enum tree_code code1 = arg1.original_code;
3385 enum tree_code code2 = arg2.original_code;
3386 tree type1 = (arg1.original_type
3387 ? arg1.original_type
3388 : TREE_TYPE (arg1.value));
3389 tree type2 = (arg2.original_type
3390 ? arg2.original_type
3391 : TREE_TYPE (arg2.value));
3393 result.value = build_binary_op (location, code,
3394 arg1.value, arg2.value, 1);
3395 result.original_code = code;
3396 result.original_type = NULL;
3398 if (TREE_CODE (result.value) == ERROR_MARK)
3399 return result;
3401 if (location != UNKNOWN_LOCATION)
3402 protected_set_expr_location (result.value, location);
3404 /* Check for cases such as x+y<<z which users are likely
3405 to misinterpret. */
3406 if (warn_parentheses)
3407 warn_about_parentheses (location, code, code1, arg1.value, code2,
3408 arg2.value);
3410 if (warn_logical_op)
3411 warn_logical_operator (location, code, TREE_TYPE (result.value),
3412 code1, arg1.value, code2, arg2.value);
3414 if (warn_logical_not_paren
3415 && code1 == TRUTH_NOT_EXPR
3416 && code2 != TRUTH_NOT_EXPR)
3417 warn_logical_not_parentheses (location, code, arg2.value);
3419 /* Warn about comparisons against string literals, with the exception
3420 of testing for equality or inequality of a string literal with NULL. */
3421 if (code == EQ_EXPR || code == NE_EXPR)
3423 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3424 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3425 warning_at (location, OPT_Waddress,
3426 "comparison with string literal results in unspecified behavior");
3428 else if (TREE_CODE_CLASS (code) == tcc_comparison
3429 && (code1 == STRING_CST || code2 == STRING_CST))
3430 warning_at (location, OPT_Waddress,
3431 "comparison with string literal results in unspecified behavior");
3433 if (TREE_OVERFLOW_P (result.value)
3434 && !TREE_OVERFLOW_P (arg1.value)
3435 && !TREE_OVERFLOW_P (arg2.value))
3436 overflow_warning (location, result.value);
3438 /* Warn about comparisons of different enum types. */
3439 if (warn_enum_compare
3440 && TREE_CODE_CLASS (code) == tcc_comparison
3441 && TREE_CODE (type1) == ENUMERAL_TYPE
3442 && TREE_CODE (type2) == ENUMERAL_TYPE
3443 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3444 warning_at (location, OPT_Wenum_compare,
3445 "comparison between %qT and %qT",
3446 type1, type2);
3448 return result;
3451 /* Return a tree for the difference of pointers OP0 and OP1.
3452 The resulting tree has type int. */
3454 static tree
3455 pointer_diff (location_t loc, tree op0, tree op1)
3457 tree restype = ptrdiff_type_node;
3458 tree result, inttype;
3460 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3461 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3462 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3463 tree orig_op1 = op1;
3465 /* If the operands point into different address spaces, we need to
3466 explicitly convert them to pointers into the common address space
3467 before we can subtract the numerical address values. */
3468 if (as0 != as1)
3470 addr_space_t as_common;
3471 tree common_type;
3473 /* Determine the common superset address space. This is guaranteed
3474 to exist because the caller verified that comp_target_types
3475 returned non-zero. */
3476 if (!addr_space_superset (as0, as1, &as_common))
3477 gcc_unreachable ();
3479 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3480 op0 = convert (common_type, op0);
3481 op1 = convert (common_type, op1);
3484 /* Determine integer type to perform computations in. This will usually
3485 be the same as the result type (ptrdiff_t), but may need to be a wider
3486 type if pointers for the address space are wider than ptrdiff_t. */
3487 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3488 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3489 else
3490 inttype = restype;
3492 if (TREE_CODE (target_type) == VOID_TYPE)
3493 pedwarn (loc, OPT_Wpointer_arith,
3494 "pointer of type %<void *%> used in subtraction");
3495 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3496 pedwarn (loc, OPT_Wpointer_arith,
3497 "pointer to a function used in subtraction");
3499 /* First do the subtraction as integers;
3500 then drop through to build the divide operator.
3501 Do not do default conversions on the minus operator
3502 in case restype is a short type. */
3504 op0 = build_binary_op (loc,
3505 MINUS_EXPR, convert (inttype, op0),
3506 convert (inttype, op1), 0);
3507 /* This generates an error if op1 is pointer to incomplete type. */
3508 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3509 error_at (loc, "arithmetic on pointer to an incomplete type");
3511 op1 = c_size_in_bytes (target_type);
3513 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3514 error_at (loc, "arithmetic on pointer to an empty aggregate");
3516 /* Divide by the size, in easiest possible way. */
3517 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3518 op0, convert (inttype, op1));
3520 /* Convert to final result type if necessary. */
3521 return convert (restype, result);
3524 /* Expand atomic compound assignments into an approriate sequence as
3525 specified by the C11 standard section 6.5.16.2.
3526 given
3527 _Atomic T1 E1
3528 T2 E2
3529 E1 op= E2
3531 This sequence is used for all types for which these operations are
3532 supported.
3534 In addition, built-in versions of the 'fe' prefixed routines may
3535 need to be invoked for floating point (real, complex or vector) when
3536 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3538 T1 newval;
3539 T1 old;
3540 T1 *addr
3541 T2 val
3542 fenv_t fenv
3544 addr = &E1;
3545 val = (E2);
3546 __atomic_load (addr, &old, SEQ_CST);
3547 feholdexcept (&fenv);
3548 loop:
3549 newval = old op val;
3550 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3551 SEQ_CST))
3552 goto done;
3553 feclearexcept (FE_ALL_EXCEPT);
3554 goto loop:
3555 done:
3556 feupdateenv (&fenv);
3558 Also note that the compiler is simply issuing the generic form of
3559 the atomic operations. This requires temp(s) and has their address
3560 taken. The atomic processing is smart enough to figure out when the
3561 size of an object can utilize a lock-free version, and convert the
3562 built-in call to the appropriate lock-free routine. The optimizers
3563 will then dispose of any temps that are no longer required, and
3564 lock-free implementations are utilized as long as there is target
3565 support for the required size.
3567 If the operator is NOP_EXPR, then this is a simple assignment, and
3568 an __atomic_store is issued to perform the assignment rather than
3569 the above loop.
3573 /* Build an atomic assignment at LOC, expanding into the proper
3574 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3575 the result of the operation, unless RETURN_OLD_P in which case
3576 return the old value of LHS (this is only for postincrement and
3577 postdecrement). */
3578 static tree
3579 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3580 tree rhs, bool return_old_p)
3582 tree fndecl, func_call;
3583 vec<tree, va_gc> *params;
3584 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3585 tree old, old_addr;
3586 tree compound_stmt;
3587 tree stmt, goto_stmt;
3588 tree loop_label, loop_decl, done_label, done_decl;
3590 tree lhs_type = TREE_TYPE (lhs);
3591 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3592 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3593 tree rhs_type = TREE_TYPE (rhs);
3595 gcc_assert (TYPE_ATOMIC (lhs_type));
3597 if (return_old_p)
3598 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3600 /* Allocate enough vector items for a compare_exchange. */
3601 vec_alloc (params, 6);
3603 /* Create a compound statement to hold the sequence of statements
3604 with a loop. */
3605 compound_stmt = c_begin_compound_stmt (false);
3607 /* Fold the RHS if it hasn't already been folded. */
3608 if (modifycode != NOP_EXPR)
3609 rhs = c_fully_fold (rhs, false, NULL);
3611 /* Remove the qualifiers for the rest of the expressions and create
3612 the VAL temp variable to hold the RHS. */
3613 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3614 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3615 val = create_tmp_var (nonatomic_rhs_type, NULL);
3616 TREE_ADDRESSABLE (val) = 1;
3617 TREE_NO_WARNING (val) = 1;
3618 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3619 SET_EXPR_LOCATION (rhs, loc);
3620 add_stmt (rhs);
3622 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3623 an atomic_store. */
3624 if (modifycode == NOP_EXPR)
3626 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3627 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3628 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3629 params->quick_push (lhs_addr);
3630 params->quick_push (rhs);
3631 params->quick_push (seq_cst);
3632 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3633 add_stmt (func_call);
3635 /* Finish the compound statement. */
3636 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3638 /* VAL is the value which was stored, return a COMPOUND_STMT of
3639 the statement and that value. */
3640 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3643 /* Create the variables and labels required for the op= form. */
3644 old = create_tmp_var (nonatomic_lhs_type, NULL);
3645 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3646 TREE_ADDRESSABLE (old) = 1;
3647 TREE_NO_WARNING (old) = 1;
3649 newval = create_tmp_var (nonatomic_lhs_type, NULL);
3650 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3651 TREE_ADDRESSABLE (newval) = 1;
3653 loop_decl = create_artificial_label (loc);
3654 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3656 done_decl = create_artificial_label (loc);
3657 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3659 /* __atomic_load (addr, &old, SEQ_CST). */
3660 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3661 params->quick_push (lhs_addr);
3662 params->quick_push (old_addr);
3663 params->quick_push (seq_cst);
3664 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3665 add_stmt (func_call);
3666 params->truncate (0);
3668 /* Create the expressions for floating-point environment
3669 manipulation, if required. */
3670 bool need_fenv = (flag_trapping_math
3671 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3672 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3673 if (need_fenv)
3674 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3676 if (hold_call)
3677 add_stmt (hold_call);
3679 /* loop: */
3680 add_stmt (loop_label);
3682 /* newval = old + val; */
3683 rhs = build_binary_op (loc, modifycode, old, val, 1);
3684 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3685 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3686 NULL_TREE, 0);
3687 if (rhs != error_mark_node)
3689 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3690 SET_EXPR_LOCATION (rhs, loc);
3691 add_stmt (rhs);
3694 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3695 goto done; */
3696 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3697 params->quick_push (lhs_addr);
3698 params->quick_push (old_addr);
3699 params->quick_push (newval_addr);
3700 params->quick_push (integer_zero_node);
3701 params->quick_push (seq_cst);
3702 params->quick_push (seq_cst);
3703 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3705 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3706 SET_EXPR_LOCATION (goto_stmt, loc);
3708 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3709 SET_EXPR_LOCATION (stmt, loc);
3710 add_stmt (stmt);
3712 if (clear_call)
3713 add_stmt (clear_call);
3715 /* goto loop; */
3716 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3717 SET_EXPR_LOCATION (goto_stmt, loc);
3718 add_stmt (goto_stmt);
3720 /* done: */
3721 add_stmt (done_label);
3723 if (update_call)
3724 add_stmt (update_call);
3726 /* Finish the compound statement. */
3727 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3729 /* NEWVAL is the value that was successfully stored, return a
3730 COMPOUND_EXPR of the statement and the appropriate value. */
3731 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3732 return_old_p ? old : newval);
3735 /* Construct and perhaps optimize a tree representation
3736 for a unary operation. CODE, a tree_code, specifies the operation
3737 and XARG is the operand.
3738 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3739 the default promotions (such as from short to int).
3740 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3741 allows non-lvalues; this is only used to handle conversion of non-lvalue
3742 arrays to pointers in C99.
3744 LOCATION is the location of the operator. */
3746 tree
3747 build_unary_op (location_t location,
3748 enum tree_code code, tree xarg, int flag)
3750 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3751 tree arg = xarg;
3752 tree argtype = 0;
3753 enum tree_code typecode;
3754 tree val;
3755 tree ret = error_mark_node;
3756 tree eptype = NULL_TREE;
3757 int noconvert = flag;
3758 const char *invalid_op_diag;
3759 bool int_operands;
3761 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3762 if (int_operands)
3763 arg = remove_c_maybe_const_expr (arg);
3765 if (code != ADDR_EXPR)
3766 arg = require_complete_type (arg);
3768 typecode = TREE_CODE (TREE_TYPE (arg));
3769 if (typecode == ERROR_MARK)
3770 return error_mark_node;
3771 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3772 typecode = INTEGER_TYPE;
3774 if ((invalid_op_diag
3775 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3777 error_at (location, invalid_op_diag);
3778 return error_mark_node;
3781 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3783 eptype = TREE_TYPE (arg);
3784 arg = TREE_OPERAND (arg, 0);
3787 switch (code)
3789 case CONVERT_EXPR:
3790 /* This is used for unary plus, because a CONVERT_EXPR
3791 is enough to prevent anybody from looking inside for
3792 associativity, but won't generate any code. */
3793 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3794 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3795 || typecode == VECTOR_TYPE))
3797 error_at (location, "wrong type argument to unary plus");
3798 return error_mark_node;
3800 else if (!noconvert)
3801 arg = default_conversion (arg);
3802 arg = non_lvalue_loc (location, arg);
3803 break;
3805 case NEGATE_EXPR:
3806 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3807 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3808 || typecode == VECTOR_TYPE))
3810 error_at (location, "wrong type argument to unary minus");
3811 return error_mark_node;
3813 else if (!noconvert)
3814 arg = default_conversion (arg);
3815 break;
3817 case BIT_NOT_EXPR:
3818 /* ~ works on integer types and non float vectors. */
3819 if (typecode == INTEGER_TYPE
3820 || (typecode == VECTOR_TYPE
3821 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3823 if (!noconvert)
3824 arg = default_conversion (arg);
3826 else if (typecode == COMPLEX_TYPE)
3828 code = CONJ_EXPR;
3829 pedwarn (location, OPT_Wpedantic,
3830 "ISO C does not support %<~%> for complex conjugation");
3831 if (!noconvert)
3832 arg = default_conversion (arg);
3834 else
3836 error_at (location, "wrong type argument to bit-complement");
3837 return error_mark_node;
3839 break;
3841 case ABS_EXPR:
3842 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3844 error_at (location, "wrong type argument to abs");
3845 return error_mark_node;
3847 else if (!noconvert)
3848 arg = default_conversion (arg);
3849 break;
3851 case CONJ_EXPR:
3852 /* Conjugating a real value is a no-op, but allow it anyway. */
3853 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3854 || typecode == COMPLEX_TYPE))
3856 error_at (location, "wrong type argument to conjugation");
3857 return error_mark_node;
3859 else if (!noconvert)
3860 arg = default_conversion (arg);
3861 break;
3863 case TRUTH_NOT_EXPR:
3864 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3865 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3866 && typecode != COMPLEX_TYPE)
3868 error_at (location,
3869 "wrong type argument to unary exclamation mark");
3870 return error_mark_node;
3872 if (int_operands)
3874 arg = c_objc_common_truthvalue_conversion (location, xarg);
3875 arg = remove_c_maybe_const_expr (arg);
3877 else
3878 arg = c_objc_common_truthvalue_conversion (location, arg);
3879 ret = invert_truthvalue_loc (location, arg);
3880 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3881 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3882 location = EXPR_LOCATION (ret);
3883 goto return_build_unary_op;
3885 case REALPART_EXPR:
3886 case IMAGPART_EXPR:
3887 ret = build_real_imag_expr (location, code, arg);
3888 if (ret == error_mark_node)
3889 return error_mark_node;
3890 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3891 eptype = TREE_TYPE (eptype);
3892 goto return_build_unary_op;
3894 case PREINCREMENT_EXPR:
3895 case POSTINCREMENT_EXPR:
3896 case PREDECREMENT_EXPR:
3897 case POSTDECREMENT_EXPR:
3899 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3901 tree inner = build_unary_op (location, code,
3902 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3903 if (inner == error_mark_node)
3904 return error_mark_node;
3905 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3906 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3907 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3908 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3909 goto return_build_unary_op;
3912 /* Complain about anything that is not a true lvalue. In
3913 Objective-C, skip this check for property_refs. */
3914 if (!objc_is_property_ref (arg)
3915 && !lvalue_or_else (location,
3916 arg, ((code == PREINCREMENT_EXPR
3917 || code == POSTINCREMENT_EXPR)
3918 ? lv_increment
3919 : lv_decrement)))
3920 return error_mark_node;
3922 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3924 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3925 warning_at (location, OPT_Wc___compat,
3926 "increment of enumeration value is invalid in C++");
3927 else
3928 warning_at (location, OPT_Wc___compat,
3929 "decrement of enumeration value is invalid in C++");
3932 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3933 arg = c_fully_fold (arg, false, NULL);
3935 bool atomic_op;
3936 atomic_op = really_atomic_lvalue (arg);
3938 /* Increment or decrement the real part of the value,
3939 and don't change the imaginary part. */
3940 if (typecode == COMPLEX_TYPE)
3942 tree real, imag;
3944 pedwarn (location, OPT_Wpedantic,
3945 "ISO C does not support %<++%> and %<--%> on complex types");
3947 if (!atomic_op)
3949 arg = stabilize_reference (arg);
3950 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3951 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3952 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3953 if (real == error_mark_node || imag == error_mark_node)
3954 return error_mark_node;
3955 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3956 real, imag);
3957 goto return_build_unary_op;
3961 /* Report invalid types. */
3963 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3964 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
3965 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
3967 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3968 error_at (location, "wrong type argument to increment");
3969 else
3970 error_at (location, "wrong type argument to decrement");
3972 return error_mark_node;
3976 tree inc;
3978 argtype = TREE_TYPE (arg);
3980 /* Compute the increment. */
3982 if (typecode == POINTER_TYPE)
3984 /* If pointer target is an incomplete type,
3985 we just cannot know how to do the arithmetic. */
3986 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3988 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3989 error_at (location,
3990 "increment of pointer to an incomplete type %qT",
3991 TREE_TYPE (argtype));
3992 else
3993 error_at (location,
3994 "decrement of pointer to an incomplete type %qT",
3995 TREE_TYPE (argtype));
3997 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3998 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4000 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4001 pedwarn (location, OPT_Wpointer_arith,
4002 "wrong type argument to increment");
4003 else
4004 pedwarn (location, OPT_Wpointer_arith,
4005 "wrong type argument to decrement");
4008 inc = c_size_in_bytes (TREE_TYPE (argtype));
4009 inc = convert_to_ptrofftype_loc (location, inc);
4011 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4013 /* For signed fract types, we invert ++ to -- or
4014 -- to ++, and change inc from 1 to -1, because
4015 it is not possible to represent 1 in signed fract constants.
4016 For unsigned fract types, the result always overflows and
4017 we get an undefined (original) or the maximum value. */
4018 if (code == PREINCREMENT_EXPR)
4019 code = PREDECREMENT_EXPR;
4020 else if (code == PREDECREMENT_EXPR)
4021 code = PREINCREMENT_EXPR;
4022 else if (code == POSTINCREMENT_EXPR)
4023 code = POSTDECREMENT_EXPR;
4024 else /* code == POSTDECREMENT_EXPR */
4025 code = POSTINCREMENT_EXPR;
4027 inc = integer_minus_one_node;
4028 inc = convert (argtype, inc);
4030 else
4032 inc = VECTOR_TYPE_P (argtype)
4033 ? build_one_cst (argtype)
4034 : integer_one_node;
4035 inc = convert (argtype, inc);
4038 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4039 need to ask Objective-C to build the increment or decrement
4040 expression for it. */
4041 if (objc_is_property_ref (arg))
4042 return objc_build_incr_expr_for_property_ref (location, code,
4043 arg, inc);
4045 /* Report a read-only lvalue. */
4046 if (TYPE_READONLY (argtype))
4048 readonly_error (location, arg,
4049 ((code == PREINCREMENT_EXPR
4050 || code == POSTINCREMENT_EXPR)
4051 ? lv_increment : lv_decrement));
4052 return error_mark_node;
4054 else if (TREE_READONLY (arg))
4055 readonly_warning (arg,
4056 ((code == PREINCREMENT_EXPR
4057 || code == POSTINCREMENT_EXPR)
4058 ? lv_increment : lv_decrement));
4060 /* If the argument is atomic, use the special code sequences for
4061 atomic compound assignment. */
4062 if (atomic_op)
4064 arg = stabilize_reference (arg);
4065 ret = build_atomic_assign (location, arg,
4066 ((code == PREINCREMENT_EXPR
4067 || code == POSTINCREMENT_EXPR)
4068 ? PLUS_EXPR
4069 : MINUS_EXPR),
4070 (FRACT_MODE_P (TYPE_MODE (argtype))
4071 ? inc
4072 : integer_one_node),
4073 (code == POSTINCREMENT_EXPR
4074 || code == POSTDECREMENT_EXPR));
4075 goto return_build_unary_op;
4078 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4079 val = boolean_increment (code, arg);
4080 else
4081 val = build2 (code, TREE_TYPE (arg), arg, inc);
4082 TREE_SIDE_EFFECTS (val) = 1;
4083 if (TREE_CODE (val) != code)
4084 TREE_NO_WARNING (val) = 1;
4085 ret = val;
4086 goto return_build_unary_op;
4089 case ADDR_EXPR:
4090 /* Note that this operation never does default_conversion. */
4092 /* The operand of unary '&' must be an lvalue (which excludes
4093 expressions of type void), or, in C99, the result of a [] or
4094 unary '*' operator. */
4095 if (VOID_TYPE_P (TREE_TYPE (arg))
4096 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4097 && (TREE_CODE (arg) != INDIRECT_REF
4098 || !flag_isoc99))
4099 pedwarn (location, 0, "taking address of expression of type %<void%>");
4101 /* Let &* cancel out to simplify resulting code. */
4102 if (TREE_CODE (arg) == INDIRECT_REF)
4104 /* Don't let this be an lvalue. */
4105 if (lvalue_p (TREE_OPERAND (arg, 0)))
4106 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4107 ret = TREE_OPERAND (arg, 0);
4108 goto return_build_unary_op;
4111 /* For &x[y], return x+y */
4112 if (TREE_CODE (arg) == ARRAY_REF)
4114 tree op0 = TREE_OPERAND (arg, 0);
4115 if (!c_mark_addressable (op0))
4116 return error_mark_node;
4119 /* Anything not already handled and not a true memory reference
4120 or a non-lvalue array is an error. */
4121 else if (typecode != FUNCTION_TYPE && !flag
4122 && !lvalue_or_else (location, arg, lv_addressof))
4123 return error_mark_node;
4125 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4126 folding later. */
4127 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4129 tree inner = build_unary_op (location, code,
4130 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4131 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4132 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4133 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4134 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4135 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4136 goto return_build_unary_op;
4139 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4140 argtype = TREE_TYPE (arg);
4142 /* If the lvalue is const or volatile, merge that into the type
4143 to which the address will point. This is only needed
4144 for function types. */
4145 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4146 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4147 && TREE_CODE (argtype) == FUNCTION_TYPE)
4149 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4150 int quals = orig_quals;
4152 if (TREE_READONLY (arg))
4153 quals |= TYPE_QUAL_CONST;
4154 if (TREE_THIS_VOLATILE (arg))
4155 quals |= TYPE_QUAL_VOLATILE;
4157 argtype = c_build_qualified_type (argtype, quals);
4160 if (!c_mark_addressable (arg))
4161 return error_mark_node;
4163 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4164 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4166 argtype = build_pointer_type (argtype);
4168 /* ??? Cope with user tricks that amount to offsetof. Delete this
4169 when we have proper support for integer constant expressions. */
4170 val = get_base_address (arg);
4171 if (val && TREE_CODE (val) == INDIRECT_REF
4172 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4174 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4175 goto return_build_unary_op;
4178 val = build1 (ADDR_EXPR, argtype, arg);
4180 ret = val;
4181 goto return_build_unary_op;
4183 default:
4184 gcc_unreachable ();
4187 if (argtype == 0)
4188 argtype = TREE_TYPE (arg);
4189 if (TREE_CODE (arg) == INTEGER_CST)
4190 ret = (require_constant_value
4191 ? fold_build1_initializer_loc (location, code, argtype, arg)
4192 : fold_build1_loc (location, code, argtype, arg));
4193 else
4194 ret = build1 (code, argtype, arg);
4195 return_build_unary_op:
4196 gcc_assert (ret != error_mark_node);
4197 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4198 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4199 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4200 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4201 ret = note_integer_operands (ret);
4202 if (eptype)
4203 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4204 protected_set_expr_location (ret, location);
4205 return ret;
4208 /* Return nonzero if REF is an lvalue valid for this language.
4209 Lvalues can be assigned, unless their type has TYPE_READONLY.
4210 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4212 bool
4213 lvalue_p (const_tree ref)
4215 const enum tree_code code = TREE_CODE (ref);
4217 switch (code)
4219 case REALPART_EXPR:
4220 case IMAGPART_EXPR:
4221 case COMPONENT_REF:
4222 return lvalue_p (TREE_OPERAND (ref, 0));
4224 case C_MAYBE_CONST_EXPR:
4225 return lvalue_p (TREE_OPERAND (ref, 1));
4227 case COMPOUND_LITERAL_EXPR:
4228 case STRING_CST:
4229 return 1;
4231 case INDIRECT_REF:
4232 case ARRAY_REF:
4233 case ARRAY_NOTATION_REF:
4234 case VAR_DECL:
4235 case PARM_DECL:
4236 case RESULT_DECL:
4237 case ERROR_MARK:
4238 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4239 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4241 case BIND_EXPR:
4242 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4244 default:
4245 return 0;
4249 /* Give a warning for storing in something that is read-only in GCC
4250 terms but not const in ISO C terms. */
4252 static void
4253 readonly_warning (tree arg, enum lvalue_use use)
4255 switch (use)
4257 case lv_assign:
4258 warning (0, "assignment of read-only location %qE", arg);
4259 break;
4260 case lv_increment:
4261 warning (0, "increment of read-only location %qE", arg);
4262 break;
4263 case lv_decrement:
4264 warning (0, "decrement of read-only location %qE", arg);
4265 break;
4266 default:
4267 gcc_unreachable ();
4269 return;
4273 /* Return nonzero if REF is an lvalue valid for this language;
4274 otherwise, print an error message and return zero. USE says
4275 how the lvalue is being used and so selects the error message.
4276 LOCATION is the location at which any error should be reported. */
4278 static int
4279 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4281 int win = lvalue_p (ref);
4283 if (!win)
4284 lvalue_error (loc, use);
4286 return win;
4289 /* Mark EXP saying that we need to be able to take the
4290 address of it; it should not be allocated in a register.
4291 Returns true if successful. */
4293 bool
4294 c_mark_addressable (tree exp)
4296 tree x = exp;
4298 while (1)
4299 switch (TREE_CODE (x))
4301 case COMPONENT_REF:
4302 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4304 error
4305 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4306 return false;
4309 /* ... fall through ... */
4311 case ADDR_EXPR:
4312 case ARRAY_REF:
4313 case REALPART_EXPR:
4314 case IMAGPART_EXPR:
4315 x = TREE_OPERAND (x, 0);
4316 break;
4318 case COMPOUND_LITERAL_EXPR:
4319 case CONSTRUCTOR:
4320 TREE_ADDRESSABLE (x) = 1;
4321 return true;
4323 case VAR_DECL:
4324 case CONST_DECL:
4325 case PARM_DECL:
4326 case RESULT_DECL:
4327 if (C_DECL_REGISTER (x)
4328 && DECL_NONLOCAL (x))
4330 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4332 error
4333 ("global register variable %qD used in nested function", x);
4334 return false;
4336 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4338 else if (C_DECL_REGISTER (x))
4340 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4341 error ("address of global register variable %qD requested", x);
4342 else
4343 error ("address of register variable %qD requested", x);
4344 return false;
4347 /* drops in */
4348 case FUNCTION_DECL:
4349 TREE_ADDRESSABLE (x) = 1;
4350 /* drops out */
4351 default:
4352 return true;
4356 /* Convert EXPR to TYPE, warning about conversion problems with
4357 constants. SEMANTIC_TYPE is the type this conversion would use
4358 without excess precision. If SEMANTIC_TYPE is NULL, this function
4359 is equivalent to convert_and_check. This function is a wrapper that
4360 handles conversions that may be different than
4361 the usual ones because of excess precision. */
4363 static tree
4364 ep_convert_and_check (location_t loc, tree type, tree expr,
4365 tree semantic_type)
4367 if (TREE_TYPE (expr) == type)
4368 return expr;
4370 if (!semantic_type)
4371 return convert_and_check (loc, type, expr);
4373 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4374 && TREE_TYPE (expr) != semantic_type)
4376 /* For integers, we need to check the real conversion, not
4377 the conversion to the excess precision type. */
4378 expr = convert_and_check (loc, semantic_type, expr);
4380 /* Result type is the excess precision type, which should be
4381 large enough, so do not check. */
4382 return convert (type, expr);
4385 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4386 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4387 if folded to an integer constant then the unselected half may
4388 contain arbitrary operations not normally permitted in constant
4389 expressions. Set the location of the expression to LOC. */
4391 tree
4392 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4393 tree op1, tree op1_original_type, tree op2,
4394 tree op2_original_type)
4396 tree type1;
4397 tree type2;
4398 enum tree_code code1;
4399 enum tree_code code2;
4400 tree result_type = NULL;
4401 tree semantic_result_type = NULL;
4402 tree orig_op1 = op1, orig_op2 = op2;
4403 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4404 bool ifexp_int_operands;
4405 tree ret;
4407 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4408 if (op1_int_operands)
4409 op1 = remove_c_maybe_const_expr (op1);
4410 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4411 if (op2_int_operands)
4412 op2 = remove_c_maybe_const_expr (op2);
4413 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4414 if (ifexp_int_operands)
4415 ifexp = remove_c_maybe_const_expr (ifexp);
4417 /* Promote both alternatives. */
4419 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4420 op1 = default_conversion (op1);
4421 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4422 op2 = default_conversion (op2);
4424 if (TREE_CODE (ifexp) == ERROR_MARK
4425 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4426 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4427 return error_mark_node;
4429 type1 = TREE_TYPE (op1);
4430 code1 = TREE_CODE (type1);
4431 type2 = TREE_TYPE (op2);
4432 code2 = TREE_CODE (type2);
4434 /* C90 does not permit non-lvalue arrays in conditional expressions.
4435 In C99 they will be pointers by now. */
4436 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4438 error_at (colon_loc, "non-lvalue array in conditional expression");
4439 return error_mark_node;
4442 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4443 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4444 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4445 || code1 == COMPLEX_TYPE)
4446 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4447 || code2 == COMPLEX_TYPE))
4449 semantic_result_type = c_common_type (type1, type2);
4450 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4452 op1 = TREE_OPERAND (op1, 0);
4453 type1 = TREE_TYPE (op1);
4454 gcc_assert (TREE_CODE (type1) == code1);
4456 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4458 op2 = TREE_OPERAND (op2, 0);
4459 type2 = TREE_TYPE (op2);
4460 gcc_assert (TREE_CODE (type2) == code2);
4464 if (warn_cxx_compat)
4466 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4467 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4469 if (TREE_CODE (t1) == ENUMERAL_TYPE
4470 && TREE_CODE (t2) == ENUMERAL_TYPE
4471 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4472 warning_at (colon_loc, OPT_Wc___compat,
4473 ("different enum types in conditional is "
4474 "invalid in C++: %qT vs %qT"),
4475 t1, t2);
4478 /* Quickly detect the usual case where op1 and op2 have the same type
4479 after promotion. */
4480 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4482 if (type1 == type2)
4483 result_type = type1;
4484 else
4485 result_type = TYPE_MAIN_VARIANT (type1);
4487 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4488 || code1 == COMPLEX_TYPE)
4489 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4490 || code2 == COMPLEX_TYPE))
4492 result_type = c_common_type (type1, type2);
4493 do_warn_double_promotion (result_type, type1, type2,
4494 "implicit conversion from %qT to %qT to "
4495 "match other result of conditional",
4496 colon_loc);
4498 /* If -Wsign-compare, warn here if type1 and type2 have
4499 different signedness. We'll promote the signed to unsigned
4500 and later code won't know it used to be different.
4501 Do this check on the original types, so that explicit casts
4502 will be considered, but default promotions won't. */
4503 if (c_inhibit_evaluation_warnings == 0)
4505 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4506 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4508 if (unsigned_op1 ^ unsigned_op2)
4510 bool ovf;
4512 /* Do not warn if the result type is signed, since the
4513 signed type will only be chosen if it can represent
4514 all the values of the unsigned type. */
4515 if (!TYPE_UNSIGNED (result_type))
4516 /* OK */;
4517 else
4519 bool op1_maybe_const = true;
4520 bool op2_maybe_const = true;
4522 /* Do not warn if the signed quantity is an
4523 unsuffixed integer literal (or some static
4524 constant expression involving such literals) and
4525 it is non-negative. This warning requires the
4526 operands to be folded for best results, so do
4527 that folding in this case even without
4528 warn_sign_compare to avoid warning options
4529 possibly affecting code generation. */
4530 c_inhibit_evaluation_warnings
4531 += (ifexp == truthvalue_false_node);
4532 op1 = c_fully_fold (op1, require_constant_value,
4533 &op1_maybe_const);
4534 c_inhibit_evaluation_warnings
4535 -= (ifexp == truthvalue_false_node);
4537 c_inhibit_evaluation_warnings
4538 += (ifexp == truthvalue_true_node);
4539 op2 = c_fully_fold (op2, require_constant_value,
4540 &op2_maybe_const);
4541 c_inhibit_evaluation_warnings
4542 -= (ifexp == truthvalue_true_node);
4544 if (warn_sign_compare)
4546 if ((unsigned_op2
4547 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4548 || (unsigned_op1
4549 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4550 /* OK */;
4551 else
4552 warning_at (colon_loc, OPT_Wsign_compare,
4553 ("signed and unsigned type in "
4554 "conditional expression"));
4556 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4557 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4558 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4559 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4564 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4566 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4567 pedwarn (colon_loc, OPT_Wpedantic,
4568 "ISO C forbids conditional expr with only one void side");
4569 result_type = void_type_node;
4571 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4573 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4574 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4575 addr_space_t as_common;
4577 if (comp_target_types (colon_loc, type1, type2))
4578 result_type = common_pointer_type (type1, type2);
4579 else if (null_pointer_constant_p (orig_op1))
4580 result_type = type2;
4581 else if (null_pointer_constant_p (orig_op2))
4582 result_type = type1;
4583 else if (!addr_space_superset (as1, as2, &as_common))
4585 error_at (colon_loc, "pointers to disjoint address spaces "
4586 "used in conditional expression");
4587 return error_mark_node;
4589 else if (VOID_TYPE_P (TREE_TYPE (type1))
4590 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4592 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4593 pedwarn (colon_loc, OPT_Wpedantic,
4594 "ISO C forbids conditional expr between "
4595 "%<void *%> and function pointer");
4596 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4597 TREE_TYPE (type2)));
4599 else if (VOID_TYPE_P (TREE_TYPE (type2))
4600 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4602 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4603 pedwarn (colon_loc, OPT_Wpedantic,
4604 "ISO C forbids conditional expr between "
4605 "%<void *%> and function pointer");
4606 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4607 TREE_TYPE (type1)));
4609 /* Objective-C pointer comparisons are a bit more lenient. */
4610 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4611 result_type = objc_common_type (type1, type2);
4612 else
4614 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4616 pedwarn (colon_loc, 0,
4617 "pointer type mismatch in conditional expression");
4618 result_type = build_pointer_type
4619 (build_qualified_type (void_type_node, qual));
4622 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4624 if (!null_pointer_constant_p (orig_op2))
4625 pedwarn (colon_loc, 0,
4626 "pointer/integer type mismatch in conditional expression");
4627 else
4629 op2 = null_pointer_node;
4631 result_type = type1;
4633 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4635 if (!null_pointer_constant_p (orig_op1))
4636 pedwarn (colon_loc, 0,
4637 "pointer/integer type mismatch in conditional expression");
4638 else
4640 op1 = null_pointer_node;
4642 result_type = type2;
4645 if (!result_type)
4647 if (flag_cond_mismatch)
4648 result_type = void_type_node;
4649 else
4651 error_at (colon_loc, "type mismatch in conditional expression");
4652 return error_mark_node;
4656 /* Merge const and volatile flags of the incoming types. */
4657 result_type
4658 = build_type_variant (result_type,
4659 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4660 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4662 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4663 semantic_result_type);
4664 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4665 semantic_result_type);
4667 if (ifexp_bcp && ifexp == truthvalue_true_node)
4669 op2_int_operands = true;
4670 op1 = c_fully_fold (op1, require_constant_value, NULL);
4672 if (ifexp_bcp && ifexp == truthvalue_false_node)
4674 op1_int_operands = true;
4675 op2 = c_fully_fold (op2, require_constant_value, NULL);
4677 int_const = int_operands = (ifexp_int_operands
4678 && op1_int_operands
4679 && op2_int_operands);
4680 if (int_operands)
4682 int_const = ((ifexp == truthvalue_true_node
4683 && TREE_CODE (orig_op1) == INTEGER_CST
4684 && !TREE_OVERFLOW (orig_op1))
4685 || (ifexp == truthvalue_false_node
4686 && TREE_CODE (orig_op2) == INTEGER_CST
4687 && !TREE_OVERFLOW (orig_op2)));
4689 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4690 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4691 else
4693 if (int_operands)
4695 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4696 nested inside of the expression. */
4697 op1 = c_fully_fold (op1, false, NULL);
4698 op2 = c_fully_fold (op2, false, NULL);
4700 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4701 if (int_operands)
4702 ret = note_integer_operands (ret);
4704 if (semantic_result_type)
4705 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4707 protected_set_expr_location (ret, colon_loc);
4708 return ret;
4711 /* Return a compound expression that performs two expressions and
4712 returns the value of the second of them.
4714 LOC is the location of the COMPOUND_EXPR. */
4716 tree
4717 build_compound_expr (location_t loc, tree expr1, tree expr2)
4719 bool expr1_int_operands, expr2_int_operands;
4720 tree eptype = NULL_TREE;
4721 tree ret;
4723 if (flag_cilkplus
4724 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4725 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4727 error_at (loc,
4728 "spawned function call cannot be part of a comma expression");
4729 return error_mark_node;
4731 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4732 if (expr1_int_operands)
4733 expr1 = remove_c_maybe_const_expr (expr1);
4734 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4735 if (expr2_int_operands)
4736 expr2 = remove_c_maybe_const_expr (expr2);
4738 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4739 expr1 = TREE_OPERAND (expr1, 0);
4740 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4742 eptype = TREE_TYPE (expr2);
4743 expr2 = TREE_OPERAND (expr2, 0);
4746 if (!TREE_SIDE_EFFECTS (expr1))
4748 /* The left-hand operand of a comma expression is like an expression
4749 statement: with -Wunused, we should warn if it doesn't have
4750 any side-effects, unless it was explicitly cast to (void). */
4751 if (warn_unused_value)
4753 if (VOID_TYPE_P (TREE_TYPE (expr1))
4754 && CONVERT_EXPR_P (expr1))
4755 ; /* (void) a, b */
4756 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4757 && TREE_CODE (expr1) == COMPOUND_EXPR
4758 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4759 ; /* (void) a, (void) b, c */
4760 else
4761 warning_at (loc, OPT_Wunused_value,
4762 "left-hand operand of comma expression has no effect");
4765 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4766 && warn_unused_value)
4768 tree r = expr1;
4769 location_t cloc = loc;
4770 while (TREE_CODE (r) == COMPOUND_EXPR)
4772 if (EXPR_HAS_LOCATION (r))
4773 cloc = EXPR_LOCATION (r);
4774 r = TREE_OPERAND (r, 1);
4776 if (!TREE_SIDE_EFFECTS (r)
4777 && !VOID_TYPE_P (TREE_TYPE (r))
4778 && !CONVERT_EXPR_P (r))
4779 warning_at (cloc, OPT_Wunused_value,
4780 "right-hand operand of comma expression has no effect");
4783 /* With -Wunused, we should also warn if the left-hand operand does have
4784 side-effects, but computes a value which is not used. For example, in
4785 `foo() + bar(), baz()' the result of the `+' operator is not used,
4786 so we should issue a warning. */
4787 else if (warn_unused_value)
4788 warn_if_unused_value (expr1, loc);
4790 if (expr2 == error_mark_node)
4791 return error_mark_node;
4793 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4795 if (flag_isoc99
4796 && expr1_int_operands
4797 && expr2_int_operands)
4798 ret = note_integer_operands (ret);
4800 if (eptype)
4801 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4803 protected_set_expr_location (ret, loc);
4804 return ret;
4807 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4808 which we are casting. OTYPE is the type of the expression being
4809 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4810 of the cast. -Wcast-qual appeared on the command line. Named
4811 address space qualifiers are not handled here, because they result
4812 in different warnings. */
4814 static void
4815 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4817 tree in_type = type;
4818 tree in_otype = otype;
4819 int added = 0;
4820 int discarded = 0;
4821 bool is_const;
4823 /* Check that the qualifiers on IN_TYPE are a superset of the
4824 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4825 nodes is uninteresting and we stop as soon as we hit a
4826 non-POINTER_TYPE node on either type. */
4829 in_otype = TREE_TYPE (in_otype);
4830 in_type = TREE_TYPE (in_type);
4832 /* GNU C allows cv-qualified function types. 'const' means the
4833 function is very pure, 'volatile' means it can't return. We
4834 need to warn when such qualifiers are added, not when they're
4835 taken away. */
4836 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4837 && TREE_CODE (in_type) == FUNCTION_TYPE)
4838 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4839 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4840 else
4841 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4842 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4844 while (TREE_CODE (in_type) == POINTER_TYPE
4845 && TREE_CODE (in_otype) == POINTER_TYPE);
4847 if (added)
4848 warning_at (loc, OPT_Wcast_qual,
4849 "cast adds %q#v qualifier to function type", added);
4851 if (discarded)
4852 /* There are qualifiers present in IN_OTYPE that are not present
4853 in IN_TYPE. */
4854 warning_at (loc, OPT_Wcast_qual,
4855 "cast discards %qv qualifier from pointer target type",
4856 discarded);
4858 if (added || discarded)
4859 return;
4861 /* A cast from **T to const **T is unsafe, because it can cause a
4862 const value to be changed with no additional warning. We only
4863 issue this warning if T is the same on both sides, and we only
4864 issue the warning if there are the same number of pointers on
4865 both sides, as otherwise the cast is clearly unsafe anyhow. A
4866 cast is unsafe when a qualifier is added at one level and const
4867 is not present at all outer levels.
4869 To issue this warning, we check at each level whether the cast
4870 adds new qualifiers not already seen. We don't need to special
4871 case function types, as they won't have the same
4872 TYPE_MAIN_VARIANT. */
4874 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4875 return;
4876 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4877 return;
4879 in_type = type;
4880 in_otype = otype;
4881 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4884 in_type = TREE_TYPE (in_type);
4885 in_otype = TREE_TYPE (in_otype);
4886 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4887 && !is_const)
4889 warning_at (loc, OPT_Wcast_qual,
4890 "to be safe all intermediate pointers in cast from "
4891 "%qT to %qT must be %<const%> qualified",
4892 otype, type);
4893 break;
4895 if (is_const)
4896 is_const = TYPE_READONLY (in_type);
4898 while (TREE_CODE (in_type) == POINTER_TYPE);
4901 /* Build an expression representing a cast to type TYPE of expression EXPR.
4902 LOC is the location of the cast-- typically the open paren of the cast. */
4904 tree
4905 build_c_cast (location_t loc, tree type, tree expr)
4907 tree value;
4909 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4910 expr = TREE_OPERAND (expr, 0);
4912 value = expr;
4914 if (type == error_mark_node || expr == error_mark_node)
4915 return error_mark_node;
4917 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4918 only in <protocol> qualifications. But when constructing cast expressions,
4919 the protocols do matter and must be kept around. */
4920 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4921 return build1 (NOP_EXPR, type, expr);
4923 type = TYPE_MAIN_VARIANT (type);
4925 if (TREE_CODE (type) == ARRAY_TYPE)
4927 error_at (loc, "cast specifies array type");
4928 return error_mark_node;
4931 if (TREE_CODE (type) == FUNCTION_TYPE)
4933 error_at (loc, "cast specifies function type");
4934 return error_mark_node;
4937 if (!VOID_TYPE_P (type))
4939 value = require_complete_type (value);
4940 if (value == error_mark_node)
4941 return error_mark_node;
4944 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4946 if (TREE_CODE (type) == RECORD_TYPE
4947 || TREE_CODE (type) == UNION_TYPE)
4948 pedwarn (loc, OPT_Wpedantic,
4949 "ISO C forbids casting nonscalar to the same type");
4951 /* Convert to remove any qualifiers from VALUE's type. */
4952 value = convert (type, value);
4954 else if (TREE_CODE (type) == UNION_TYPE)
4956 tree field;
4958 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4959 if (TREE_TYPE (field) != error_mark_node
4960 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4961 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4962 break;
4964 if (field)
4966 tree t;
4967 bool maybe_const = true;
4969 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
4970 t = c_fully_fold (value, false, &maybe_const);
4971 t = build_constructor_single (type, field, t);
4972 if (!maybe_const)
4973 t = c_wrap_maybe_const (t, true);
4974 t = digest_init (loc, type, t,
4975 NULL_TREE, false, true, 0);
4976 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4977 return t;
4979 error_at (loc, "cast to union type from type not present in union");
4980 return error_mark_node;
4982 else
4984 tree otype, ovalue;
4986 if (type == void_type_node)
4988 tree t = build1 (CONVERT_EXPR, type, value);
4989 SET_EXPR_LOCATION (t, loc);
4990 return t;
4993 otype = TREE_TYPE (value);
4995 /* Optionally warn about potentially worrisome casts. */
4996 if (warn_cast_qual
4997 && TREE_CODE (type) == POINTER_TYPE
4998 && TREE_CODE (otype) == POINTER_TYPE)
4999 handle_warn_cast_qual (loc, type, otype);
5001 /* Warn about conversions between pointers to disjoint
5002 address spaces. */
5003 if (TREE_CODE (type) == POINTER_TYPE
5004 && TREE_CODE (otype) == POINTER_TYPE
5005 && !null_pointer_constant_p (value))
5007 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5008 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5009 addr_space_t as_common;
5011 if (!addr_space_superset (as_to, as_from, &as_common))
5013 if (ADDR_SPACE_GENERIC_P (as_from))
5014 warning_at (loc, 0, "cast to %s address space pointer "
5015 "from disjoint generic address space pointer",
5016 c_addr_space_name (as_to));
5018 else if (ADDR_SPACE_GENERIC_P (as_to))
5019 warning_at (loc, 0, "cast to generic address space pointer "
5020 "from disjoint %s address space pointer",
5021 c_addr_space_name (as_from));
5023 else
5024 warning_at (loc, 0, "cast to %s address space pointer "
5025 "from disjoint %s address space pointer",
5026 c_addr_space_name (as_to),
5027 c_addr_space_name (as_from));
5031 /* Warn about possible alignment problems. */
5032 if (STRICT_ALIGNMENT
5033 && TREE_CODE (type) == POINTER_TYPE
5034 && TREE_CODE (otype) == POINTER_TYPE
5035 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5036 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5037 /* Don't warn about opaque types, where the actual alignment
5038 restriction is unknown. */
5039 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5040 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5041 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5042 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5043 warning_at (loc, OPT_Wcast_align,
5044 "cast increases required alignment of target type");
5046 if (TREE_CODE (type) == INTEGER_TYPE
5047 && TREE_CODE (otype) == POINTER_TYPE
5048 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5049 /* Unlike conversion of integers to pointers, where the
5050 warning is disabled for converting constants because
5051 of cases such as SIG_*, warn about converting constant
5052 pointers to integers. In some cases it may cause unwanted
5053 sign extension, and a warning is appropriate. */
5054 warning_at (loc, OPT_Wpointer_to_int_cast,
5055 "cast from pointer to integer of different size");
5057 if (TREE_CODE (value) == CALL_EXPR
5058 && TREE_CODE (type) != TREE_CODE (otype))
5059 warning_at (loc, OPT_Wbad_function_cast,
5060 "cast from function call of type %qT "
5061 "to non-matching type %qT", otype, type);
5063 if (TREE_CODE (type) == POINTER_TYPE
5064 && TREE_CODE (otype) == INTEGER_TYPE
5065 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5066 /* Don't warn about converting any constant. */
5067 && !TREE_CONSTANT (value))
5068 warning_at (loc,
5069 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5070 "of different size");
5072 if (warn_strict_aliasing <= 2)
5073 strict_aliasing_warning (otype, type, expr);
5075 /* If pedantic, warn for conversions between function and object
5076 pointer types, except for converting a null pointer constant
5077 to function pointer type. */
5078 if (pedantic
5079 && TREE_CODE (type) == POINTER_TYPE
5080 && TREE_CODE (otype) == POINTER_TYPE
5081 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5082 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5083 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5084 "conversion of function pointer to object pointer type");
5086 if (pedantic
5087 && TREE_CODE (type) == POINTER_TYPE
5088 && TREE_CODE (otype) == POINTER_TYPE
5089 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5090 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5091 && !null_pointer_constant_p (value))
5092 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5093 "conversion of object pointer to function pointer type");
5095 ovalue = value;
5096 value = convert (type, value);
5098 /* Ignore any integer overflow caused by the cast. */
5099 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5101 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5103 if (!TREE_OVERFLOW (value))
5105 /* Avoid clobbering a shared constant. */
5106 value = copy_node (value);
5107 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5110 else if (TREE_OVERFLOW (value))
5111 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5112 value = wide_int_to_tree (TREE_TYPE (value), value);
5116 /* Don't let a cast be an lvalue. */
5117 if (value == expr)
5118 value = non_lvalue_loc (loc, value);
5120 /* Don't allow the results of casting to floating-point or complex
5121 types be confused with actual constants, or casts involving
5122 integer and pointer types other than direct integer-to-integer
5123 and integer-to-pointer be confused with integer constant
5124 expressions and null pointer constants. */
5125 if (TREE_CODE (value) == REAL_CST
5126 || TREE_CODE (value) == COMPLEX_CST
5127 || (TREE_CODE (value) == INTEGER_CST
5128 && !((TREE_CODE (expr) == INTEGER_CST
5129 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5130 || TREE_CODE (expr) == REAL_CST
5131 || TREE_CODE (expr) == COMPLEX_CST)))
5132 value = build1 (NOP_EXPR, type, value);
5134 if (CAN_HAVE_LOCATION_P (value))
5135 SET_EXPR_LOCATION (value, loc);
5136 return value;
5139 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5140 location of the open paren of the cast, or the position of the cast
5141 expr. */
5142 tree
5143 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5145 tree type;
5146 tree type_expr = NULL_TREE;
5147 bool type_expr_const = true;
5148 tree ret;
5149 int saved_wsp = warn_strict_prototypes;
5151 /* This avoids warnings about unprototyped casts on
5152 integers. E.g. "#define SIG_DFL (void(*)())0". */
5153 if (TREE_CODE (expr) == INTEGER_CST)
5154 warn_strict_prototypes = 0;
5155 type = groktypename (type_name, &type_expr, &type_expr_const);
5156 warn_strict_prototypes = saved_wsp;
5158 ret = build_c_cast (loc, type, expr);
5159 if (type_expr)
5161 bool inner_expr_const = true;
5162 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5163 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5164 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5165 && inner_expr_const);
5166 SET_EXPR_LOCATION (ret, loc);
5169 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5170 SET_EXPR_LOCATION (ret, loc);
5172 /* C++ does not permits types to be defined in a cast, but it
5173 allows references to incomplete types. */
5174 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5175 warning_at (loc, OPT_Wc___compat,
5176 "defining a type in a cast is invalid in C++");
5178 return ret;
5181 /* Build an assignment expression of lvalue LHS from value RHS.
5182 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5183 may differ from TREE_TYPE (LHS) for an enum bitfield.
5184 MODIFYCODE is the code for a binary operator that we use
5185 to combine the old value of LHS with RHS to get the new value.
5186 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5187 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5188 which may differ from TREE_TYPE (RHS) for an enum value.
5190 LOCATION is the location of the MODIFYCODE operator.
5191 RHS_LOC is the location of the RHS. */
5193 tree
5194 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5195 enum tree_code modifycode,
5196 location_t rhs_loc, tree rhs, tree rhs_origtype)
5198 tree result;
5199 tree newrhs;
5200 tree rhseval = NULL_TREE;
5201 tree rhs_semantic_type = NULL_TREE;
5202 tree lhstype = TREE_TYPE (lhs);
5203 tree olhstype = lhstype;
5204 bool npc;
5205 bool is_atomic_op;
5207 /* Types that aren't fully specified cannot be used in assignments. */
5208 lhs = require_complete_type (lhs);
5210 /* Avoid duplicate error messages from operands that had errors. */
5211 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5212 return error_mark_node;
5214 /* Ensure an error for assigning a non-lvalue array to an array in
5215 C90. */
5216 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5218 error_at (location, "assignment to expression with array type");
5219 return error_mark_node;
5222 /* For ObjC properties, defer this check. */
5223 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5224 return error_mark_node;
5226 is_atomic_op = really_atomic_lvalue (lhs);
5228 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5230 rhs_semantic_type = TREE_TYPE (rhs);
5231 rhs = TREE_OPERAND (rhs, 0);
5234 newrhs = rhs;
5236 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5238 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5239 lhs_origtype, modifycode, rhs_loc, rhs,
5240 rhs_origtype);
5241 if (inner == error_mark_node)
5242 return error_mark_node;
5243 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5244 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5245 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5246 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5247 protected_set_expr_location (result, location);
5248 return result;
5251 /* If a binary op has been requested, combine the old LHS value with the RHS
5252 producing the value we should actually store into the LHS. */
5254 if (modifycode != NOP_EXPR)
5256 lhs = c_fully_fold (lhs, false, NULL);
5257 lhs = stabilize_reference (lhs);
5259 /* Construct the RHS for any non-atomic compound assignemnt. */
5260 if (!is_atomic_op)
5262 /* If in LHS op= RHS the RHS has side-effects, ensure they
5263 are preevaluated before the rest of the assignment expression's
5264 side-effects, because RHS could contain e.g. function calls
5265 that modify LHS. */
5266 if (TREE_SIDE_EFFECTS (rhs))
5268 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5269 rhseval = newrhs;
5271 newrhs = build_binary_op (location,
5272 modifycode, lhs, newrhs, 1);
5274 /* The original type of the right hand side is no longer
5275 meaningful. */
5276 rhs_origtype = NULL_TREE;
5280 if (c_dialect_objc ())
5282 /* Check if we are modifying an Objective-C property reference;
5283 if so, we need to generate setter calls. */
5284 result = objc_maybe_build_modify_expr (lhs, newrhs);
5285 if (result)
5286 goto return_result;
5288 /* Else, do the check that we postponed for Objective-C. */
5289 if (!lvalue_or_else (location, lhs, lv_assign))
5290 return error_mark_node;
5293 /* Give an error for storing in something that is 'const'. */
5295 if (TYPE_READONLY (lhstype)
5296 || ((TREE_CODE (lhstype) == RECORD_TYPE
5297 || TREE_CODE (lhstype) == UNION_TYPE)
5298 && C_TYPE_FIELDS_READONLY (lhstype)))
5300 readonly_error (location, lhs, lv_assign);
5301 return error_mark_node;
5303 else if (TREE_READONLY (lhs))
5304 readonly_warning (lhs, lv_assign);
5306 /* If storing into a structure or union member,
5307 it has probably been given type `int'.
5308 Compute the type that would go with
5309 the actual amount of storage the member occupies. */
5311 if (TREE_CODE (lhs) == COMPONENT_REF
5312 && (TREE_CODE (lhstype) == INTEGER_TYPE
5313 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5314 || TREE_CODE (lhstype) == REAL_TYPE
5315 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5316 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5318 /* If storing in a field that is in actuality a short or narrower than one,
5319 we must store in the field in its actual type. */
5321 if (lhstype != TREE_TYPE (lhs))
5323 lhs = copy_node (lhs);
5324 TREE_TYPE (lhs) = lhstype;
5327 /* Issue -Wc++-compat warnings about an assignment to an enum type
5328 when LHS does not have its original type. This happens for,
5329 e.g., an enum bitfield in a struct. */
5330 if (warn_cxx_compat
5331 && lhs_origtype != NULL_TREE
5332 && lhs_origtype != lhstype
5333 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5335 tree checktype = (rhs_origtype != NULL_TREE
5336 ? rhs_origtype
5337 : TREE_TYPE (rhs));
5338 if (checktype != error_mark_node
5339 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5340 || (is_atomic_op && modifycode != NOP_EXPR)))
5341 warning_at (location, OPT_Wc___compat,
5342 "enum conversion in assignment is invalid in C++");
5345 /* If the lhs is atomic, remove that qualifier. */
5346 if (is_atomic_op)
5348 lhstype = build_qualified_type (lhstype,
5349 (TYPE_QUALS (lhstype)
5350 & ~TYPE_QUAL_ATOMIC));
5351 olhstype = build_qualified_type (olhstype,
5352 (TYPE_QUALS (lhstype)
5353 & ~TYPE_QUAL_ATOMIC));
5356 /* Convert new value to destination type. Fold it first, then
5357 restore any excess precision information, for the sake of
5358 conversion warnings. */
5360 if (!(is_atomic_op && modifycode != NOP_EXPR))
5362 npc = null_pointer_constant_p (newrhs);
5363 newrhs = c_fully_fold (newrhs, false, NULL);
5364 if (rhs_semantic_type)
5365 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5366 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5367 rhs_origtype, ic_assign, npc,
5368 NULL_TREE, NULL_TREE, 0);
5369 if (TREE_CODE (newrhs) == ERROR_MARK)
5370 return error_mark_node;
5373 /* Emit ObjC write barrier, if necessary. */
5374 if (c_dialect_objc () && flag_objc_gc)
5376 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5377 if (result)
5379 protected_set_expr_location (result, location);
5380 goto return_result;
5384 /* Scan operands. */
5386 if (is_atomic_op)
5387 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5388 else
5390 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5391 TREE_SIDE_EFFECTS (result) = 1;
5392 protected_set_expr_location (result, location);
5395 /* If we got the LHS in a different type for storing in,
5396 convert the result back to the nominal type of LHS
5397 so that the value we return always has the same type
5398 as the LHS argument. */
5400 if (olhstype == TREE_TYPE (result))
5401 goto return_result;
5403 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5404 rhs_origtype, ic_assign, false, NULL_TREE,
5405 NULL_TREE, 0);
5406 protected_set_expr_location (result, location);
5408 return_result:
5409 if (rhseval)
5410 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5411 return result;
5414 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5415 This is used to implement -fplan9-extensions. */
5417 static bool
5418 find_anonymous_field_with_type (tree struct_type, tree type)
5420 tree field;
5421 bool found;
5423 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5424 || TREE_CODE (struct_type) == UNION_TYPE);
5425 found = false;
5426 for (field = TYPE_FIELDS (struct_type);
5427 field != NULL_TREE;
5428 field = TREE_CHAIN (field))
5430 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5431 ? c_build_qualified_type (TREE_TYPE (field),
5432 TYPE_QUAL_ATOMIC)
5433 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5434 if (DECL_NAME (field) == NULL
5435 && comptypes (type, fieldtype))
5437 if (found)
5438 return false;
5439 found = true;
5441 else if (DECL_NAME (field) == NULL
5442 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5443 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5444 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5446 if (found)
5447 return false;
5448 found = true;
5451 return found;
5454 /* RHS is an expression whose type is pointer to struct. If there is
5455 an anonymous field in RHS with type TYPE, then return a pointer to
5456 that field in RHS. This is used with -fplan9-extensions. This
5457 returns NULL if no conversion could be found. */
5459 static tree
5460 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5462 tree rhs_struct_type, lhs_main_type;
5463 tree field, found_field;
5464 bool found_sub_field;
5465 tree ret;
5467 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5468 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5469 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5470 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5472 gcc_assert (POINTER_TYPE_P (type));
5473 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5474 ? c_build_qualified_type (TREE_TYPE (type),
5475 TYPE_QUAL_ATOMIC)
5476 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5478 found_field = NULL_TREE;
5479 found_sub_field = false;
5480 for (field = TYPE_FIELDS (rhs_struct_type);
5481 field != NULL_TREE;
5482 field = TREE_CHAIN (field))
5484 if (DECL_NAME (field) != NULL_TREE
5485 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5486 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5487 continue;
5488 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5489 ? c_build_qualified_type (TREE_TYPE (field),
5490 TYPE_QUAL_ATOMIC)
5491 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5492 if (comptypes (lhs_main_type, fieldtype))
5494 if (found_field != NULL_TREE)
5495 return NULL_TREE;
5496 found_field = field;
5498 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5499 lhs_main_type))
5501 if (found_field != NULL_TREE)
5502 return NULL_TREE;
5503 found_field = field;
5504 found_sub_field = true;
5508 if (found_field == NULL_TREE)
5509 return NULL_TREE;
5511 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5512 build_fold_indirect_ref (rhs), found_field,
5513 NULL_TREE);
5514 ret = build_fold_addr_expr_loc (location, ret);
5516 if (found_sub_field)
5518 ret = convert_to_anonymous_field (location, type, ret);
5519 gcc_assert (ret != NULL_TREE);
5522 return ret;
5525 /* Issue an error message for a bad initializer component.
5526 GMSGID identifies the message.
5527 The component name is taken from the spelling stack. */
5529 static void
5530 error_init (location_t loc, const char *gmsgid)
5532 char *ofwhat;
5534 /* The gmsgid may be a format string with %< and %>. */
5535 error_at (loc, gmsgid);
5536 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5537 if (*ofwhat)
5538 inform (loc, "(near initialization for %qs)", ofwhat);
5541 /* Issue a pedantic warning for a bad initializer component. OPT is
5542 the option OPT_* (from options.h) controlling this warning or 0 if
5543 it is unconditionally given. GMSGID identifies the message. The
5544 component name is taken from the spelling stack. */
5546 static void
5547 pedwarn_init (location_t location, int opt, const char *gmsgid)
5549 char *ofwhat;
5550 bool warned;
5552 /* The gmsgid may be a format string with %< and %>. */
5553 warned = pedwarn (location, opt, gmsgid);
5554 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5555 if (*ofwhat && warned)
5556 inform (location, "(near initialization for %qs)", ofwhat);
5559 /* Issue a warning for a bad initializer component.
5561 OPT is the OPT_W* value corresponding to the warning option that
5562 controls this warning. GMSGID identifies the message. The
5563 component name is taken from the spelling stack. */
5565 static void
5566 warning_init (location_t loc, int opt, const char *gmsgid)
5568 char *ofwhat;
5569 bool warned;
5571 /* The gmsgid may be a format string with %< and %>. */
5572 warned = warning_at (loc, opt, gmsgid);
5573 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5574 if (*ofwhat && warned)
5575 inform (loc, "(near initialization for %qs)", ofwhat);
5578 /* If TYPE is an array type and EXPR is a parenthesized string
5579 constant, warn if pedantic that EXPR is being used to initialize an
5580 object of type TYPE. */
5582 void
5583 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5585 if (pedantic
5586 && TREE_CODE (type) == ARRAY_TYPE
5587 && TREE_CODE (expr.value) == STRING_CST
5588 && expr.original_code != STRING_CST)
5589 pedwarn_init (loc, OPT_Wpedantic,
5590 "array initialized from parenthesized string constant");
5593 /* Convert value RHS to type TYPE as preparation for an assignment to
5594 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5595 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5596 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5597 constant before any folding.
5598 The real work of conversion is done by `convert'.
5599 The purpose of this function is to generate error messages
5600 for assignments that are not allowed in C.
5601 ERRTYPE says whether it is argument passing, assignment,
5602 initialization or return.
5604 LOCATION is the location of the assignment, EXPR_LOC is the location of
5605 the RHS or, for a function, location of an argument.
5606 FUNCTION is a tree for the function being called.
5607 PARMNUM is the number of the argument, for printing in error messages. */
5609 static tree
5610 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5611 tree rhs, tree origtype, enum impl_conv errtype,
5612 bool null_pointer_constant, tree fundecl,
5613 tree function, int parmnum)
5615 enum tree_code codel = TREE_CODE (type);
5616 tree orig_rhs = rhs;
5617 tree rhstype;
5618 enum tree_code coder;
5619 tree rname = NULL_TREE;
5620 bool objc_ok = false;
5622 if (errtype == ic_argpass)
5624 tree selector;
5625 /* Change pointer to function to the function itself for
5626 diagnostics. */
5627 if (TREE_CODE (function) == ADDR_EXPR
5628 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5629 function = TREE_OPERAND (function, 0);
5631 /* Handle an ObjC selector specially for diagnostics. */
5632 selector = objc_message_selector ();
5633 rname = function;
5634 if (selector && parmnum > 2)
5636 rname = selector;
5637 parmnum -= 2;
5641 /* This macro is used to emit diagnostics to ensure that all format
5642 strings are complete sentences, visible to gettext and checked at
5643 compile time. */
5644 #define WARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5645 do { \
5646 switch (errtype) \
5648 case ic_argpass: \
5649 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5650 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5651 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5652 "expected %qT but argument is of type %qT", \
5653 type, rhstype); \
5654 break; \
5655 case ic_assign: \
5656 pedwarn (LOCATION, OPT, AS); \
5657 break; \
5658 case ic_init: \
5659 pedwarn_init (LOCATION, OPT, IN); \
5660 break; \
5661 case ic_return: \
5662 pedwarn (LOCATION, OPT, RE); \
5663 break; \
5664 default: \
5665 gcc_unreachable (); \
5667 } while (0)
5669 /* This macro is used to emit diagnostics to ensure that all format
5670 strings are complete sentences, visible to gettext and checked at
5671 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5672 extra parameter to enumerate qualifiers. */
5674 #define WARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5675 do { \
5676 switch (errtype) \
5678 case ic_argpass: \
5679 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5680 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5681 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5682 "expected %qT but argument is of type %qT", \
5683 type, rhstype); \
5684 break; \
5685 case ic_assign: \
5686 pedwarn (LOCATION, OPT, AS, QUALS); \
5687 break; \
5688 case ic_init: \
5689 pedwarn (LOCATION, OPT, IN, QUALS); \
5690 break; \
5691 case ic_return: \
5692 pedwarn (LOCATION, OPT, RE, QUALS); \
5693 break; \
5694 default: \
5695 gcc_unreachable (); \
5697 } while (0)
5699 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5700 rhs = TREE_OPERAND (rhs, 0);
5702 rhstype = TREE_TYPE (rhs);
5703 coder = TREE_CODE (rhstype);
5705 if (coder == ERROR_MARK)
5706 return error_mark_node;
5708 if (c_dialect_objc ())
5710 int parmno;
5712 switch (errtype)
5714 case ic_return:
5715 parmno = 0;
5716 break;
5718 case ic_assign:
5719 parmno = -1;
5720 break;
5722 case ic_init:
5723 parmno = -2;
5724 break;
5726 default:
5727 parmno = parmnum;
5728 break;
5731 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5734 if (warn_cxx_compat)
5736 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5737 if (checktype != error_mark_node
5738 && TREE_CODE (type) == ENUMERAL_TYPE
5739 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5741 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5742 G_("enum conversion when passing argument "
5743 "%d of %qE is invalid in C++"),
5744 G_("enum conversion in assignment is "
5745 "invalid in C++"),
5746 G_("enum conversion in initialization is "
5747 "invalid in C++"),
5748 G_("enum conversion in return is "
5749 "invalid in C++"));
5753 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5754 return rhs;
5756 if (coder == VOID_TYPE)
5758 /* Except for passing an argument to an unprototyped function,
5759 this is a constraint violation. When passing an argument to
5760 an unprototyped function, it is compile-time undefined;
5761 making it a constraint in that case was rejected in
5762 DR#252. */
5763 error_at (location, "void value not ignored as it ought to be");
5764 return error_mark_node;
5766 rhs = require_complete_type (rhs);
5767 if (rhs == error_mark_node)
5768 return error_mark_node;
5769 /* A non-reference type can convert to a reference. This handles
5770 va_start, va_copy and possibly port built-ins. */
5771 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5773 if (!lvalue_p (rhs))
5775 error_at (location, "cannot pass rvalue to reference parameter");
5776 return error_mark_node;
5778 if (!c_mark_addressable (rhs))
5779 return error_mark_node;
5780 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5781 SET_EXPR_LOCATION (rhs, location);
5783 rhs = convert_for_assignment (location, expr_loc,
5784 build_pointer_type (TREE_TYPE (type)),
5785 rhs, origtype, errtype,
5786 null_pointer_constant, fundecl, function,
5787 parmnum);
5788 if (rhs == error_mark_node)
5789 return error_mark_node;
5791 rhs = build1 (NOP_EXPR, type, rhs);
5792 SET_EXPR_LOCATION (rhs, location);
5793 return rhs;
5795 /* Some types can interconvert without explicit casts. */
5796 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5797 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5798 return convert (type, rhs);
5799 /* Arithmetic types all interconvert, and enum is treated like int. */
5800 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5801 || codel == FIXED_POINT_TYPE
5802 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5803 || codel == BOOLEAN_TYPE)
5804 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5805 || coder == FIXED_POINT_TYPE
5806 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5807 || coder == BOOLEAN_TYPE))
5809 tree ret;
5810 bool save = in_late_binary_op;
5811 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5812 in_late_binary_op = true;
5813 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5814 ? expr_loc : location, type, orig_rhs);
5815 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5816 in_late_binary_op = save;
5817 return ret;
5820 /* Aggregates in different TUs might need conversion. */
5821 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5822 && codel == coder
5823 && comptypes (type, rhstype))
5824 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5825 ? expr_loc : location, type, rhs);
5827 /* Conversion to a transparent union or record from its member types.
5828 This applies only to function arguments. */
5829 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5830 && TYPE_TRANSPARENT_AGGR (type))
5831 && errtype == ic_argpass)
5833 tree memb, marginal_memb = NULL_TREE;
5835 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5837 tree memb_type = TREE_TYPE (memb);
5839 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5840 TYPE_MAIN_VARIANT (rhstype)))
5841 break;
5843 if (TREE_CODE (memb_type) != POINTER_TYPE)
5844 continue;
5846 if (coder == POINTER_TYPE)
5848 tree ttl = TREE_TYPE (memb_type);
5849 tree ttr = TREE_TYPE (rhstype);
5851 /* Any non-function converts to a [const][volatile] void *
5852 and vice versa; otherwise, targets must be the same.
5853 Meanwhile, the lhs target must have all the qualifiers of
5854 the rhs. */
5855 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5856 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5857 || comp_target_types (location, memb_type, rhstype))
5859 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5860 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5861 /* If this type won't generate any warnings, use it. */
5862 if (lquals == rquals
5863 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5864 && TREE_CODE (ttl) == FUNCTION_TYPE)
5865 ? ((lquals | rquals) == rquals)
5866 : ((lquals | rquals) == lquals)))
5867 break;
5869 /* Keep looking for a better type, but remember this one. */
5870 if (!marginal_memb)
5871 marginal_memb = memb;
5875 /* Can convert integer zero to any pointer type. */
5876 if (null_pointer_constant)
5878 rhs = null_pointer_node;
5879 break;
5883 if (memb || marginal_memb)
5885 if (!memb)
5887 /* We have only a marginally acceptable member type;
5888 it needs a warning. */
5889 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5890 tree ttr = TREE_TYPE (rhstype);
5892 /* Const and volatile mean something different for function
5893 types, so the usual warnings are not appropriate. */
5894 if (TREE_CODE (ttr) == FUNCTION_TYPE
5895 && TREE_CODE (ttl) == FUNCTION_TYPE)
5897 /* Because const and volatile on functions are
5898 restrictions that say the function will not do
5899 certain things, it is okay to use a const or volatile
5900 function where an ordinary one is wanted, but not
5901 vice-versa. */
5902 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5903 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5904 WARN_FOR_QUALIFIERS (location, expr_loc,
5905 OPT_Wdiscarded_qualifiers,
5906 G_("passing argument %d of %qE "
5907 "makes %q#v qualified function "
5908 "pointer from unqualified"),
5909 G_("assignment makes %q#v qualified "
5910 "function pointer from "
5911 "unqualified"),
5912 G_("initialization makes %q#v qualified "
5913 "function pointer from "
5914 "unqualified"),
5915 G_("return makes %q#v qualified function "
5916 "pointer from unqualified"),
5917 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5919 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5920 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5921 WARN_FOR_QUALIFIERS (location, expr_loc,
5922 OPT_Wdiscarded_qualifiers,
5923 G_("passing argument %d of %qE discards "
5924 "%qv qualifier from pointer target type"),
5925 G_("assignment discards %qv qualifier "
5926 "from pointer target type"),
5927 G_("initialization discards %qv qualifier "
5928 "from pointer target type"),
5929 G_("return discards %qv qualifier from "
5930 "pointer target type"),
5931 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5933 memb = marginal_memb;
5936 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5937 pedwarn (location, OPT_Wpedantic,
5938 "ISO C prohibits argument conversion to union type");
5940 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5941 return build_constructor_single (type, memb, rhs);
5945 /* Conversions among pointers */
5946 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5947 && (coder == codel))
5949 tree ttl = TREE_TYPE (type);
5950 tree ttr = TREE_TYPE (rhstype);
5951 tree mvl = ttl;
5952 tree mvr = ttr;
5953 bool is_opaque_pointer;
5954 int target_cmp = 0; /* Cache comp_target_types () result. */
5955 addr_space_t asl;
5956 addr_space_t asr;
5958 if (TREE_CODE (mvl) != ARRAY_TYPE)
5959 mvl = (TYPE_ATOMIC (mvl)
5960 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
5961 TYPE_QUAL_ATOMIC)
5962 : TYPE_MAIN_VARIANT (mvl));
5963 if (TREE_CODE (mvr) != ARRAY_TYPE)
5964 mvr = (TYPE_ATOMIC (mvr)
5965 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
5966 TYPE_QUAL_ATOMIC)
5967 : TYPE_MAIN_VARIANT (mvr));
5968 /* Opaque pointers are treated like void pointers. */
5969 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5971 /* The Plan 9 compiler permits a pointer to a struct to be
5972 automatically converted into a pointer to an anonymous field
5973 within the struct. */
5974 if (flag_plan9_extensions
5975 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5976 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5977 && mvl != mvr)
5979 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5980 if (new_rhs != NULL_TREE)
5982 rhs = new_rhs;
5983 rhstype = TREE_TYPE (rhs);
5984 coder = TREE_CODE (rhstype);
5985 ttr = TREE_TYPE (rhstype);
5986 mvr = TYPE_MAIN_VARIANT (ttr);
5990 /* C++ does not allow the implicit conversion void* -> T*. However,
5991 for the purpose of reducing the number of false positives, we
5992 tolerate the special case of
5994 int *p = NULL;
5996 where NULL is typically defined in C to be '(void *) 0'. */
5997 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5998 warning_at (errtype == ic_argpass ? expr_loc : location,
5999 OPT_Wc___compat,
6000 "request for implicit conversion "
6001 "from %qT to %qT not permitted in C++", rhstype, type);
6003 /* See if the pointers point to incompatible address spaces. */
6004 asl = TYPE_ADDR_SPACE (ttl);
6005 asr = TYPE_ADDR_SPACE (ttr);
6006 if (!null_pointer_constant_p (rhs)
6007 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6009 switch (errtype)
6011 case ic_argpass:
6012 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6013 "non-enclosed address space", parmnum, rname);
6014 break;
6015 case ic_assign:
6016 error_at (location, "assignment from pointer to "
6017 "non-enclosed address space");
6018 break;
6019 case ic_init:
6020 error_at (location, "initialization from pointer to "
6021 "non-enclosed address space");
6022 break;
6023 case ic_return:
6024 error_at (location, "return from pointer to "
6025 "non-enclosed address space");
6026 break;
6027 default:
6028 gcc_unreachable ();
6030 return error_mark_node;
6033 /* Check if the right-hand side has a format attribute but the
6034 left-hand side doesn't. */
6035 if (warn_suggest_attribute_format
6036 && check_missing_format_attribute (type, rhstype))
6038 switch (errtype)
6040 case ic_argpass:
6041 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6042 "argument %d of %qE might be "
6043 "a candidate for a format attribute",
6044 parmnum, rname);
6045 break;
6046 case ic_assign:
6047 warning_at (location, OPT_Wsuggest_attribute_format,
6048 "assignment left-hand side might be "
6049 "a candidate for a format attribute");
6050 break;
6051 case ic_init:
6052 warning_at (location, OPT_Wsuggest_attribute_format,
6053 "initialization left-hand side might be "
6054 "a candidate for a format attribute");
6055 break;
6056 case ic_return:
6057 warning_at (location, OPT_Wsuggest_attribute_format,
6058 "return type might be "
6059 "a candidate for a format attribute");
6060 break;
6061 default:
6062 gcc_unreachable ();
6066 /* Any non-function converts to a [const][volatile] void *
6067 and vice versa; otherwise, targets must be the same.
6068 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6069 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6070 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6071 || (target_cmp = comp_target_types (location, type, rhstype))
6072 || is_opaque_pointer
6073 || ((c_common_unsigned_type (mvl)
6074 == c_common_unsigned_type (mvr))
6075 && (c_common_signed_type (mvl)
6076 == c_common_signed_type (mvr))
6077 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6079 if (pedantic
6080 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6082 (VOID_TYPE_P (ttr)
6083 && !null_pointer_constant
6084 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6085 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6086 G_("ISO C forbids passing argument %d of "
6087 "%qE between function pointer "
6088 "and %<void *%>"),
6089 G_("ISO C forbids assignment between "
6090 "function pointer and %<void *%>"),
6091 G_("ISO C forbids initialization between "
6092 "function pointer and %<void *%>"),
6093 G_("ISO C forbids return between function "
6094 "pointer and %<void *%>"));
6095 /* Const and volatile mean something different for function types,
6096 so the usual warnings are not appropriate. */
6097 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6098 && TREE_CODE (ttl) != FUNCTION_TYPE)
6100 /* Assignments between atomic and non-atomic objects are OK. */
6101 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6102 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6104 WARN_FOR_QUALIFIERS (location, expr_loc,
6105 OPT_Wdiscarded_qualifiers,
6106 G_("passing argument %d of %qE discards "
6107 "%qv qualifier from pointer target type"),
6108 G_("assignment discards %qv qualifier "
6109 "from pointer target type"),
6110 G_("initialization discards %qv qualifier "
6111 "from pointer target type"),
6112 G_("return discards %qv qualifier from "
6113 "pointer target type"),
6114 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6116 /* If this is not a case of ignoring a mismatch in signedness,
6117 no warning. */
6118 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6119 || target_cmp)
6121 /* If there is a mismatch, do warn. */
6122 else if (warn_pointer_sign)
6123 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6124 G_("pointer targets in passing argument "
6125 "%d of %qE differ in signedness"),
6126 G_("pointer targets in assignment "
6127 "differ in signedness"),
6128 G_("pointer targets in initialization "
6129 "differ in signedness"),
6130 G_("pointer targets in return differ "
6131 "in signedness"));
6133 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6134 && TREE_CODE (ttr) == FUNCTION_TYPE)
6136 /* Because const and volatile on functions are restrictions
6137 that say the function will not do certain things,
6138 it is okay to use a const or volatile function
6139 where an ordinary one is wanted, but not vice-versa. */
6140 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6141 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6142 WARN_FOR_QUALIFIERS (location, expr_loc,
6143 OPT_Wdiscarded_qualifiers,
6144 G_("passing argument %d of %qE makes "
6145 "%q#v qualified function pointer "
6146 "from unqualified"),
6147 G_("assignment makes %q#v qualified function "
6148 "pointer from unqualified"),
6149 G_("initialization makes %q#v qualified "
6150 "function pointer from unqualified"),
6151 G_("return makes %q#v qualified function "
6152 "pointer from unqualified"),
6153 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6156 else
6157 /* Avoid warning about the volatile ObjC EH puts on decls. */
6158 if (!objc_ok)
6159 WARN_FOR_ASSIGNMENT (location, expr_loc,
6160 OPT_Wincompatible_pointer_types,
6161 G_("passing argument %d of %qE from "
6162 "incompatible pointer type"),
6163 G_("assignment from incompatible pointer type"),
6164 G_("initialization from incompatible "
6165 "pointer type"),
6166 G_("return from incompatible pointer type"));
6168 return convert (type, rhs);
6170 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6172 /* ??? This should not be an error when inlining calls to
6173 unprototyped functions. */
6174 error_at (location, "invalid use of non-lvalue array");
6175 return error_mark_node;
6177 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6179 /* An explicit constant 0 can convert to a pointer,
6180 or one that results from arithmetic, even including
6181 a cast to integer type. */
6182 if (!null_pointer_constant)
6183 WARN_FOR_ASSIGNMENT (location, expr_loc,
6184 OPT_Wint_conversion,
6185 G_("passing argument %d of %qE makes "
6186 "pointer from integer without a cast"),
6187 G_("assignment makes pointer from integer "
6188 "without a cast"),
6189 G_("initialization makes pointer from "
6190 "integer without a cast"),
6191 G_("return makes pointer from integer "
6192 "without a cast"));
6194 return convert (type, rhs);
6196 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6198 WARN_FOR_ASSIGNMENT (location, expr_loc,
6199 OPT_Wint_conversion,
6200 G_("passing argument %d of %qE makes integer "
6201 "from pointer without a cast"),
6202 G_("assignment makes integer from pointer "
6203 "without a cast"),
6204 G_("initialization makes integer from pointer "
6205 "without a cast"),
6206 G_("return makes integer from pointer "
6207 "without a cast"));
6208 return convert (type, rhs);
6210 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6212 tree ret;
6213 bool save = in_late_binary_op;
6214 in_late_binary_op = true;
6215 ret = convert (type, rhs);
6216 in_late_binary_op = save;
6217 return ret;
6220 switch (errtype)
6222 case ic_argpass:
6223 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6224 rname);
6225 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6226 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6227 "expected %qT but argument is of type %qT", type, rhstype);
6228 break;
6229 case ic_assign:
6230 error_at (location, "incompatible types when assigning to type %qT from "
6231 "type %qT", type, rhstype);
6232 break;
6233 case ic_init:
6234 error_at (location,
6235 "incompatible types when initializing type %qT using type %qT",
6236 type, rhstype);
6237 break;
6238 case ic_return:
6239 error_at (location,
6240 "incompatible types when returning type %qT but %qT was "
6241 "expected", rhstype, type);
6242 break;
6243 default:
6244 gcc_unreachable ();
6247 return error_mark_node;
6250 /* If VALUE is a compound expr all of whose expressions are constant, then
6251 return its value. Otherwise, return error_mark_node.
6253 This is for handling COMPOUND_EXPRs as initializer elements
6254 which is allowed with a warning when -pedantic is specified. */
6256 static tree
6257 valid_compound_expr_initializer (tree value, tree endtype)
6259 if (TREE_CODE (value) == COMPOUND_EXPR)
6261 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6262 == error_mark_node)
6263 return error_mark_node;
6264 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6265 endtype);
6267 else if (!initializer_constant_valid_p (value, endtype))
6268 return error_mark_node;
6269 else
6270 return value;
6273 /* Perform appropriate conversions on the initial value of a variable,
6274 store it in the declaration DECL,
6275 and print any error messages that are appropriate.
6276 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6277 If the init is invalid, store an ERROR_MARK.
6279 INIT_LOC is the location of the initial value. */
6281 void
6282 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6284 tree value, type;
6285 bool npc = false;
6287 /* If variable's type was invalidly declared, just ignore it. */
6289 type = TREE_TYPE (decl);
6290 if (TREE_CODE (type) == ERROR_MARK)
6291 return;
6293 /* Digest the specified initializer into an expression. */
6295 if (init)
6296 npc = null_pointer_constant_p (init);
6297 value = digest_init (init_loc, type, init, origtype, npc,
6298 true, TREE_STATIC (decl));
6300 /* Store the expression if valid; else report error. */
6302 if (!in_system_header_at (input_location)
6303 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6304 warning (OPT_Wtraditional, "traditional C rejects automatic "
6305 "aggregate initialization");
6307 DECL_INITIAL (decl) = value;
6309 /* ANSI wants warnings about out-of-range constant initializers. */
6310 STRIP_TYPE_NOPS (value);
6311 if (TREE_STATIC (decl))
6312 constant_expression_warning (value);
6314 /* Check if we need to set array size from compound literal size. */
6315 if (TREE_CODE (type) == ARRAY_TYPE
6316 && TYPE_DOMAIN (type) == 0
6317 && value != error_mark_node)
6319 tree inside_init = init;
6321 STRIP_TYPE_NOPS (inside_init);
6322 inside_init = fold (inside_init);
6324 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6326 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6328 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6330 /* For int foo[] = (int [3]){1}; we need to set array size
6331 now since later on array initializer will be just the
6332 brace enclosed list of the compound literal. */
6333 tree etype = strip_array_types (TREE_TYPE (decl));
6334 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6335 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6336 layout_type (type);
6337 layout_decl (cldecl, 0);
6338 TREE_TYPE (decl)
6339 = c_build_qualified_type (type, TYPE_QUALS (etype));
6345 /* Methods for storing and printing names for error messages. */
6347 /* Implement a spelling stack that allows components of a name to be pushed
6348 and popped. Each element on the stack is this structure. */
6350 struct spelling
6352 int kind;
6353 union
6355 unsigned HOST_WIDE_INT i;
6356 const char *s;
6357 } u;
6360 #define SPELLING_STRING 1
6361 #define SPELLING_MEMBER 2
6362 #define SPELLING_BOUNDS 3
6364 static struct spelling *spelling; /* Next stack element (unused). */
6365 static struct spelling *spelling_base; /* Spelling stack base. */
6366 static int spelling_size; /* Size of the spelling stack. */
6368 /* Macros to save and restore the spelling stack around push_... functions.
6369 Alternative to SAVE_SPELLING_STACK. */
6371 #define SPELLING_DEPTH() (spelling - spelling_base)
6372 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6374 /* Push an element on the spelling stack with type KIND and assign VALUE
6375 to MEMBER. */
6377 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6379 int depth = SPELLING_DEPTH (); \
6381 if (depth >= spelling_size) \
6383 spelling_size += 10; \
6384 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6385 spelling_size); \
6386 RESTORE_SPELLING_DEPTH (depth); \
6389 spelling->kind = (KIND); \
6390 spelling->MEMBER = (VALUE); \
6391 spelling++; \
6394 /* Push STRING on the stack. Printed literally. */
6396 static void
6397 push_string (const char *string)
6399 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6402 /* Push a member name on the stack. Printed as '.' STRING. */
6404 static void
6405 push_member_name (tree decl)
6407 const char *const string
6408 = (DECL_NAME (decl)
6409 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6410 : _("<anonymous>"));
6411 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6414 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6416 static void
6417 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6419 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6422 /* Compute the maximum size in bytes of the printed spelling. */
6424 static int
6425 spelling_length (void)
6427 int size = 0;
6428 struct spelling *p;
6430 for (p = spelling_base; p < spelling; p++)
6432 if (p->kind == SPELLING_BOUNDS)
6433 size += 25;
6434 else
6435 size += strlen (p->u.s) + 1;
6438 return size;
6441 /* Print the spelling to BUFFER and return it. */
6443 static char *
6444 print_spelling (char *buffer)
6446 char *d = buffer;
6447 struct spelling *p;
6449 for (p = spelling_base; p < spelling; p++)
6450 if (p->kind == SPELLING_BOUNDS)
6452 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6453 d += strlen (d);
6455 else
6457 const char *s;
6458 if (p->kind == SPELLING_MEMBER)
6459 *d++ = '.';
6460 for (s = p->u.s; (*d = *s++); d++)
6463 *d++ = '\0';
6464 return buffer;
6467 /* Digest the parser output INIT as an initializer for type TYPE.
6468 Return a C expression of type TYPE to represent the initial value.
6470 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6472 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6474 If INIT is a string constant, STRICT_STRING is true if it is
6475 unparenthesized or we should not warn here for it being parenthesized.
6476 For other types of INIT, STRICT_STRING is not used.
6478 INIT_LOC is the location of the INIT.
6480 REQUIRE_CONSTANT requests an error if non-constant initializers or
6481 elements are seen. */
6483 static tree
6484 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6485 bool null_pointer_constant, bool strict_string,
6486 int require_constant)
6488 enum tree_code code = TREE_CODE (type);
6489 tree inside_init = init;
6490 tree semantic_type = NULL_TREE;
6491 bool maybe_const = true;
6493 if (type == error_mark_node
6494 || !init
6495 || init == error_mark_node
6496 || TREE_TYPE (init) == error_mark_node)
6497 return error_mark_node;
6499 STRIP_TYPE_NOPS (inside_init);
6501 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6503 semantic_type = TREE_TYPE (inside_init);
6504 inside_init = TREE_OPERAND (inside_init, 0);
6506 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6507 inside_init = decl_constant_value_for_optimization (inside_init);
6509 /* Initialization of an array of chars from a string constant
6510 optionally enclosed in braces. */
6512 if (code == ARRAY_TYPE && inside_init
6513 && TREE_CODE (inside_init) == STRING_CST)
6515 tree typ1
6516 = (TYPE_ATOMIC (TREE_TYPE (type))
6517 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6518 TYPE_QUAL_ATOMIC)
6519 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6520 /* Note that an array could be both an array of character type
6521 and an array of wchar_t if wchar_t is signed char or unsigned
6522 char. */
6523 bool char_array = (typ1 == char_type_node
6524 || typ1 == signed_char_type_node
6525 || typ1 == unsigned_char_type_node);
6526 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6527 bool char16_array = !!comptypes (typ1, char16_type_node);
6528 bool char32_array = !!comptypes (typ1, char32_type_node);
6530 if (char_array || wchar_array || char16_array || char32_array)
6532 struct c_expr expr;
6533 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6534 expr.value = inside_init;
6535 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6536 expr.original_type = NULL;
6537 maybe_warn_string_init (init_loc, type, expr);
6539 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6540 pedwarn_init (init_loc, OPT_Wpedantic,
6541 "initialization of a flexible array member");
6543 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6544 TYPE_MAIN_VARIANT (type)))
6545 return inside_init;
6547 if (char_array)
6549 if (typ2 != char_type_node)
6551 error_init (init_loc, "char-array initialized from wide "
6552 "string");
6553 return error_mark_node;
6556 else
6558 if (typ2 == char_type_node)
6560 error_init (init_loc, "wide character array initialized "
6561 "from non-wide string");
6562 return error_mark_node;
6564 else if (!comptypes(typ1, typ2))
6566 error_init (init_loc, "wide character array initialized "
6567 "from incompatible wide string");
6568 return error_mark_node;
6572 TREE_TYPE (inside_init) = type;
6573 if (TYPE_DOMAIN (type) != 0
6574 && TYPE_SIZE (type) != 0
6575 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6577 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6579 /* Subtract the size of a single (possibly wide) character
6580 because it's ok to ignore the terminating null char
6581 that is counted in the length of the constant. */
6582 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6583 (len
6584 - (TYPE_PRECISION (typ1)
6585 / BITS_PER_UNIT))))
6586 pedwarn_init (init_loc, 0,
6587 ("initializer-string for array of chars "
6588 "is too long"));
6589 else if (warn_cxx_compat
6590 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6591 warning_at (init_loc, OPT_Wc___compat,
6592 ("initializer-string for array chars "
6593 "is too long for C++"));
6596 return inside_init;
6598 else if (INTEGRAL_TYPE_P (typ1))
6600 error_init (init_loc, "array of inappropriate type initialized "
6601 "from string constant");
6602 return error_mark_node;
6606 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6607 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6608 below and handle as a constructor. */
6609 if (code == VECTOR_TYPE
6610 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6611 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6612 && TREE_CONSTANT (inside_init))
6614 if (TREE_CODE (inside_init) == VECTOR_CST
6615 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6616 TYPE_MAIN_VARIANT (type)))
6617 return inside_init;
6619 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6621 unsigned HOST_WIDE_INT ix;
6622 tree value;
6623 bool constant_p = true;
6625 /* Iterate through elements and check if all constructor
6626 elements are *_CSTs. */
6627 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6628 if (!CONSTANT_CLASS_P (value))
6630 constant_p = false;
6631 break;
6634 if (constant_p)
6635 return build_vector_from_ctor (type,
6636 CONSTRUCTOR_ELTS (inside_init));
6640 if (warn_sequence_point)
6641 verify_sequence_points (inside_init);
6643 /* Any type can be initialized
6644 from an expression of the same type, optionally with braces. */
6646 if (inside_init && TREE_TYPE (inside_init) != 0
6647 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6648 TYPE_MAIN_VARIANT (type))
6649 || (code == ARRAY_TYPE
6650 && comptypes (TREE_TYPE (inside_init), type))
6651 || (code == VECTOR_TYPE
6652 && comptypes (TREE_TYPE (inside_init), type))
6653 || (code == POINTER_TYPE
6654 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6655 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6656 TREE_TYPE (type)))))
6658 if (code == POINTER_TYPE)
6660 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6662 if (TREE_CODE (inside_init) == STRING_CST
6663 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6664 inside_init = array_to_pointer_conversion
6665 (init_loc, inside_init);
6666 else
6668 error_init (init_loc, "invalid use of non-lvalue array");
6669 return error_mark_node;
6674 if (code == VECTOR_TYPE)
6675 /* Although the types are compatible, we may require a
6676 conversion. */
6677 inside_init = convert (type, inside_init);
6679 if (require_constant
6680 && (code == VECTOR_TYPE || !flag_isoc99)
6681 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6683 /* As an extension, allow initializing objects with static storage
6684 duration with compound literals (which are then treated just as
6685 the brace enclosed list they contain). Also allow this for
6686 vectors, as we can only assign them with compound literals. */
6687 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6688 inside_init = DECL_INITIAL (decl);
6691 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6692 && TREE_CODE (inside_init) != CONSTRUCTOR)
6694 error_init (init_loc, "array initialized from non-constant array "
6695 "expression");
6696 return error_mark_node;
6699 /* Compound expressions can only occur here if -Wpedantic or
6700 -pedantic-errors is specified. In the later case, we always want
6701 an error. In the former case, we simply want a warning. */
6702 if (require_constant && pedantic
6703 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6705 inside_init
6706 = valid_compound_expr_initializer (inside_init,
6707 TREE_TYPE (inside_init));
6708 if (inside_init == error_mark_node)
6709 error_init (init_loc, "initializer element is not constant");
6710 else
6711 pedwarn_init (init_loc, OPT_Wpedantic,
6712 "initializer element is not constant");
6713 if (flag_pedantic_errors)
6714 inside_init = error_mark_node;
6716 else if (require_constant
6717 && !initializer_constant_valid_p (inside_init,
6718 TREE_TYPE (inside_init)))
6720 error_init (init_loc, "initializer element is not constant");
6721 inside_init = error_mark_node;
6723 else if (require_constant && !maybe_const)
6724 pedwarn_init (init_loc, 0,
6725 "initializer element is not a constant expression");
6727 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6728 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6729 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6730 type, inside_init, origtype,
6731 ic_init, null_pointer_constant,
6732 NULL_TREE, NULL_TREE, 0);
6733 return inside_init;
6736 /* Handle scalar types, including conversions. */
6738 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6739 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6740 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6742 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6743 && (TREE_CODE (init) == STRING_CST
6744 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6745 inside_init = init = array_to_pointer_conversion (init_loc, init);
6746 if (semantic_type)
6747 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6748 inside_init);
6749 inside_init
6750 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6751 inside_init, origtype, ic_init,
6752 null_pointer_constant, NULL_TREE, NULL_TREE,
6755 /* Check to see if we have already given an error message. */
6756 if (inside_init == error_mark_node)
6758 else if (require_constant && !TREE_CONSTANT (inside_init))
6760 error_init (init_loc, "initializer element is not constant");
6761 inside_init = error_mark_node;
6763 else if (require_constant
6764 && !initializer_constant_valid_p (inside_init,
6765 TREE_TYPE (inside_init)))
6767 error_init (init_loc, "initializer element is not computable at "
6768 "load time");
6769 inside_init = error_mark_node;
6771 else if (require_constant && !maybe_const)
6772 pedwarn_init (init_loc, 0,
6773 "initializer element is not a constant expression");
6775 return inside_init;
6778 /* Come here only for records and arrays. */
6780 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6782 error_init (init_loc, "variable-sized object may not be initialized");
6783 return error_mark_node;
6786 error_init (init_loc, "invalid initializer");
6787 return error_mark_node;
6790 /* Handle initializers that use braces. */
6792 /* Type of object we are accumulating a constructor for.
6793 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6794 static tree constructor_type;
6796 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6797 left to fill. */
6798 static tree constructor_fields;
6800 /* For an ARRAY_TYPE, this is the specified index
6801 at which to store the next element we get. */
6802 static tree constructor_index;
6804 /* For an ARRAY_TYPE, this is the maximum index. */
6805 static tree constructor_max_index;
6807 /* For a RECORD_TYPE, this is the first field not yet written out. */
6808 static tree constructor_unfilled_fields;
6810 /* For an ARRAY_TYPE, this is the index of the first element
6811 not yet written out. */
6812 static tree constructor_unfilled_index;
6814 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6815 This is so we can generate gaps between fields, when appropriate. */
6816 static tree constructor_bit_index;
6818 /* If we are saving up the elements rather than allocating them,
6819 this is the list of elements so far (in reverse order,
6820 most recent first). */
6821 static vec<constructor_elt, va_gc> *constructor_elements;
6823 /* 1 if constructor should be incrementally stored into a constructor chain,
6824 0 if all the elements should be kept in AVL tree. */
6825 static int constructor_incremental;
6827 /* 1 if so far this constructor's elements are all compile-time constants. */
6828 static int constructor_constant;
6830 /* 1 if so far this constructor's elements are all valid address constants. */
6831 static int constructor_simple;
6833 /* 1 if this constructor has an element that cannot be part of a
6834 constant expression. */
6835 static int constructor_nonconst;
6837 /* 1 if this constructor is erroneous so far. */
6838 static int constructor_erroneous;
6840 /* 1 if this constructor is the universal zero initializer { 0 }. */
6841 static int constructor_zeroinit;
6843 /* Structure for managing pending initializer elements, organized as an
6844 AVL tree. */
6846 struct init_node
6848 struct init_node *left, *right;
6849 struct init_node *parent;
6850 int balance;
6851 tree purpose;
6852 tree value;
6853 tree origtype;
6856 /* Tree of pending elements at this constructor level.
6857 These are elements encountered out of order
6858 which belong at places we haven't reached yet in actually
6859 writing the output.
6860 Will never hold tree nodes across GC runs. */
6861 static struct init_node *constructor_pending_elts;
6863 /* The SPELLING_DEPTH of this constructor. */
6864 static int constructor_depth;
6866 /* DECL node for which an initializer is being read.
6867 0 means we are reading a constructor expression
6868 such as (struct foo) {...}. */
6869 static tree constructor_decl;
6871 /* Nonzero if this is an initializer for a top-level decl. */
6872 static int constructor_top_level;
6874 /* Nonzero if there were any member designators in this initializer. */
6875 static int constructor_designated;
6877 /* Nesting depth of designator list. */
6878 static int designator_depth;
6880 /* Nonzero if there were diagnosed errors in this designator list. */
6881 static int designator_erroneous;
6884 /* This stack has a level for each implicit or explicit level of
6885 structuring in the initializer, including the outermost one. It
6886 saves the values of most of the variables above. */
6888 struct constructor_range_stack;
6890 struct constructor_stack
6892 struct constructor_stack *next;
6893 tree type;
6894 tree fields;
6895 tree index;
6896 tree max_index;
6897 tree unfilled_index;
6898 tree unfilled_fields;
6899 tree bit_index;
6900 vec<constructor_elt, va_gc> *elements;
6901 struct init_node *pending_elts;
6902 int offset;
6903 int depth;
6904 /* If value nonzero, this value should replace the entire
6905 constructor at this level. */
6906 struct c_expr replacement_value;
6907 struct constructor_range_stack *range_stack;
6908 char constant;
6909 char simple;
6910 char nonconst;
6911 char implicit;
6912 char erroneous;
6913 char outer;
6914 char incremental;
6915 char designated;
6916 int designator_depth;
6919 static struct constructor_stack *constructor_stack;
6921 /* This stack represents designators from some range designator up to
6922 the last designator in the list. */
6924 struct constructor_range_stack
6926 struct constructor_range_stack *next, *prev;
6927 struct constructor_stack *stack;
6928 tree range_start;
6929 tree index;
6930 tree range_end;
6931 tree fields;
6934 static struct constructor_range_stack *constructor_range_stack;
6936 /* This stack records separate initializers that are nested.
6937 Nested initializers can't happen in ANSI C, but GNU C allows them
6938 in cases like { ... (struct foo) { ... } ... }. */
6940 struct initializer_stack
6942 struct initializer_stack *next;
6943 tree decl;
6944 struct constructor_stack *constructor_stack;
6945 struct constructor_range_stack *constructor_range_stack;
6946 vec<constructor_elt, va_gc> *elements;
6947 struct spelling *spelling;
6948 struct spelling *spelling_base;
6949 int spelling_size;
6950 char top_level;
6951 char require_constant_value;
6952 char require_constant_elements;
6955 static struct initializer_stack *initializer_stack;
6957 /* Prepare to parse and output the initializer for variable DECL. */
6959 void
6960 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6962 const char *locus;
6963 struct initializer_stack *p = XNEW (struct initializer_stack);
6965 p->decl = constructor_decl;
6966 p->require_constant_value = require_constant_value;
6967 p->require_constant_elements = require_constant_elements;
6968 p->constructor_stack = constructor_stack;
6969 p->constructor_range_stack = constructor_range_stack;
6970 p->elements = constructor_elements;
6971 p->spelling = spelling;
6972 p->spelling_base = spelling_base;
6973 p->spelling_size = spelling_size;
6974 p->top_level = constructor_top_level;
6975 p->next = initializer_stack;
6976 initializer_stack = p;
6978 constructor_decl = decl;
6979 constructor_designated = 0;
6980 constructor_top_level = top_level;
6982 if (decl != 0 && decl != error_mark_node)
6984 require_constant_value = TREE_STATIC (decl);
6985 require_constant_elements
6986 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6987 /* For a scalar, you can always use any value to initialize,
6988 even within braces. */
6989 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6990 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6991 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6992 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6993 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6995 else
6997 require_constant_value = 0;
6998 require_constant_elements = 0;
6999 locus = _("(anonymous)");
7002 constructor_stack = 0;
7003 constructor_range_stack = 0;
7005 found_missing_braces = 0;
7007 spelling_base = 0;
7008 spelling_size = 0;
7009 RESTORE_SPELLING_DEPTH (0);
7011 if (locus)
7012 push_string (locus);
7015 void
7016 finish_init (void)
7018 struct initializer_stack *p = initializer_stack;
7020 /* Free the whole constructor stack of this initializer. */
7021 while (constructor_stack)
7023 struct constructor_stack *q = constructor_stack;
7024 constructor_stack = q->next;
7025 free (q);
7028 gcc_assert (!constructor_range_stack);
7030 /* Pop back to the data of the outer initializer (if any). */
7031 free (spelling_base);
7033 constructor_decl = p->decl;
7034 require_constant_value = p->require_constant_value;
7035 require_constant_elements = p->require_constant_elements;
7036 constructor_stack = p->constructor_stack;
7037 constructor_range_stack = p->constructor_range_stack;
7038 constructor_elements = p->elements;
7039 spelling = p->spelling;
7040 spelling_base = p->spelling_base;
7041 spelling_size = p->spelling_size;
7042 constructor_top_level = p->top_level;
7043 initializer_stack = p->next;
7044 free (p);
7047 /* Call here when we see the initializer is surrounded by braces.
7048 This is instead of a call to push_init_level;
7049 it is matched by a call to pop_init_level.
7051 TYPE is the type to initialize, for a constructor expression.
7052 For an initializer for a decl, TYPE is zero. */
7054 void
7055 really_start_incremental_init (tree type)
7057 struct constructor_stack *p = XNEW (struct constructor_stack);
7059 if (type == 0)
7060 type = TREE_TYPE (constructor_decl);
7062 if (TREE_CODE (type) == VECTOR_TYPE
7063 && TYPE_VECTOR_OPAQUE (type))
7064 error ("opaque vector types cannot be initialized");
7066 p->type = constructor_type;
7067 p->fields = constructor_fields;
7068 p->index = constructor_index;
7069 p->max_index = constructor_max_index;
7070 p->unfilled_index = constructor_unfilled_index;
7071 p->unfilled_fields = constructor_unfilled_fields;
7072 p->bit_index = constructor_bit_index;
7073 p->elements = constructor_elements;
7074 p->constant = constructor_constant;
7075 p->simple = constructor_simple;
7076 p->nonconst = constructor_nonconst;
7077 p->erroneous = constructor_erroneous;
7078 p->pending_elts = constructor_pending_elts;
7079 p->depth = constructor_depth;
7080 p->replacement_value.value = 0;
7081 p->replacement_value.original_code = ERROR_MARK;
7082 p->replacement_value.original_type = NULL;
7083 p->implicit = 0;
7084 p->range_stack = 0;
7085 p->outer = 0;
7086 p->incremental = constructor_incremental;
7087 p->designated = constructor_designated;
7088 p->designator_depth = designator_depth;
7089 p->next = 0;
7090 constructor_stack = p;
7092 constructor_constant = 1;
7093 constructor_simple = 1;
7094 constructor_nonconst = 0;
7095 constructor_depth = SPELLING_DEPTH ();
7096 constructor_elements = NULL;
7097 constructor_pending_elts = 0;
7098 constructor_type = type;
7099 constructor_incremental = 1;
7100 constructor_designated = 0;
7101 constructor_zeroinit = 1;
7102 designator_depth = 0;
7103 designator_erroneous = 0;
7105 if (TREE_CODE (constructor_type) == RECORD_TYPE
7106 || TREE_CODE (constructor_type) == UNION_TYPE)
7108 constructor_fields = TYPE_FIELDS (constructor_type);
7109 /* Skip any nameless bit fields at the beginning. */
7110 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7111 && DECL_NAME (constructor_fields) == 0)
7112 constructor_fields = DECL_CHAIN (constructor_fields);
7114 constructor_unfilled_fields = constructor_fields;
7115 constructor_bit_index = bitsize_zero_node;
7117 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7119 if (TYPE_DOMAIN (constructor_type))
7121 constructor_max_index
7122 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7124 /* Detect non-empty initializations of zero-length arrays. */
7125 if (constructor_max_index == NULL_TREE
7126 && TYPE_SIZE (constructor_type))
7127 constructor_max_index = integer_minus_one_node;
7129 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7130 to initialize VLAs will cause a proper error; avoid tree
7131 checking errors as well by setting a safe value. */
7132 if (constructor_max_index
7133 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7134 constructor_max_index = integer_minus_one_node;
7136 constructor_index
7137 = convert (bitsizetype,
7138 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7140 else
7142 constructor_index = bitsize_zero_node;
7143 constructor_max_index = NULL_TREE;
7146 constructor_unfilled_index = constructor_index;
7148 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7150 /* Vectors are like simple fixed-size arrays. */
7151 constructor_max_index =
7152 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7153 constructor_index = bitsize_zero_node;
7154 constructor_unfilled_index = constructor_index;
7156 else
7158 /* Handle the case of int x = {5}; */
7159 constructor_fields = constructor_type;
7160 constructor_unfilled_fields = constructor_type;
7164 /* Push down into a subobject, for initialization.
7165 If this is for an explicit set of braces, IMPLICIT is 0.
7166 If it is because the next element belongs at a lower level,
7167 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7169 void
7170 push_init_level (location_t loc, int implicit,
7171 struct obstack *braced_init_obstack)
7173 struct constructor_stack *p;
7174 tree value = NULL_TREE;
7176 /* If we've exhausted any levels that didn't have braces,
7177 pop them now. If implicit == 1, this will have been done in
7178 process_init_element; do not repeat it here because in the case
7179 of excess initializers for an empty aggregate this leads to an
7180 infinite cycle of popping a level and immediately recreating
7181 it. */
7182 if (implicit != 1)
7184 while (constructor_stack->implicit)
7186 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7187 || TREE_CODE (constructor_type) == UNION_TYPE)
7188 && constructor_fields == 0)
7189 process_init_element (input_location,
7190 pop_init_level (loc, 1, braced_init_obstack),
7191 true, braced_init_obstack);
7192 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7193 && constructor_max_index
7194 && tree_int_cst_lt (constructor_max_index,
7195 constructor_index))
7196 process_init_element (input_location,
7197 pop_init_level (loc, 1, braced_init_obstack),
7198 true, braced_init_obstack);
7199 else
7200 break;
7204 /* Unless this is an explicit brace, we need to preserve previous
7205 content if any. */
7206 if (implicit)
7208 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7209 || TREE_CODE (constructor_type) == UNION_TYPE)
7210 && constructor_fields)
7211 value = find_init_member (constructor_fields, braced_init_obstack);
7212 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7213 value = find_init_member (constructor_index, braced_init_obstack);
7216 p = XNEW (struct constructor_stack);
7217 p->type = constructor_type;
7218 p->fields = constructor_fields;
7219 p->index = constructor_index;
7220 p->max_index = constructor_max_index;
7221 p->unfilled_index = constructor_unfilled_index;
7222 p->unfilled_fields = constructor_unfilled_fields;
7223 p->bit_index = constructor_bit_index;
7224 p->elements = constructor_elements;
7225 p->constant = constructor_constant;
7226 p->simple = constructor_simple;
7227 p->nonconst = constructor_nonconst;
7228 p->erroneous = constructor_erroneous;
7229 p->pending_elts = constructor_pending_elts;
7230 p->depth = constructor_depth;
7231 p->replacement_value.value = 0;
7232 p->replacement_value.original_code = ERROR_MARK;
7233 p->replacement_value.original_type = NULL;
7234 p->implicit = implicit;
7235 p->outer = 0;
7236 p->incremental = constructor_incremental;
7237 p->designated = constructor_designated;
7238 p->designator_depth = designator_depth;
7239 p->next = constructor_stack;
7240 p->range_stack = 0;
7241 constructor_stack = p;
7243 constructor_constant = 1;
7244 constructor_simple = 1;
7245 constructor_nonconst = 0;
7246 constructor_depth = SPELLING_DEPTH ();
7247 constructor_elements = NULL;
7248 constructor_incremental = 1;
7249 constructor_designated = 0;
7250 constructor_pending_elts = 0;
7251 if (!implicit)
7253 p->range_stack = constructor_range_stack;
7254 constructor_range_stack = 0;
7255 designator_depth = 0;
7256 designator_erroneous = 0;
7259 /* Don't die if an entire brace-pair level is superfluous
7260 in the containing level. */
7261 if (constructor_type == 0)
7263 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7264 || TREE_CODE (constructor_type) == UNION_TYPE)
7266 /* Don't die if there are extra init elts at the end. */
7267 if (constructor_fields == 0)
7268 constructor_type = 0;
7269 else
7271 constructor_type = TREE_TYPE (constructor_fields);
7272 push_member_name (constructor_fields);
7273 constructor_depth++;
7275 /* If upper initializer is designated, then mark this as
7276 designated too to prevent bogus warnings. */
7277 constructor_designated = p->designated;
7279 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7281 constructor_type = TREE_TYPE (constructor_type);
7282 push_array_bounds (tree_to_uhwi (constructor_index));
7283 constructor_depth++;
7286 if (constructor_type == 0)
7288 error_init (loc, "extra brace group at end of initializer");
7289 constructor_fields = 0;
7290 constructor_unfilled_fields = 0;
7291 return;
7294 if (value && TREE_CODE (value) == CONSTRUCTOR)
7296 constructor_constant = TREE_CONSTANT (value);
7297 constructor_simple = TREE_STATIC (value);
7298 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7299 constructor_elements = CONSTRUCTOR_ELTS (value);
7300 if (!vec_safe_is_empty (constructor_elements)
7301 && (TREE_CODE (constructor_type) == RECORD_TYPE
7302 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7303 set_nonincremental_init (braced_init_obstack);
7306 if (implicit == 1)
7307 found_missing_braces = 1;
7309 if (TREE_CODE (constructor_type) == RECORD_TYPE
7310 || TREE_CODE (constructor_type) == UNION_TYPE)
7312 constructor_fields = TYPE_FIELDS (constructor_type);
7313 /* Skip any nameless bit fields at the beginning. */
7314 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7315 && DECL_NAME (constructor_fields) == 0)
7316 constructor_fields = DECL_CHAIN (constructor_fields);
7318 constructor_unfilled_fields = constructor_fields;
7319 constructor_bit_index = bitsize_zero_node;
7321 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7323 /* Vectors are like simple fixed-size arrays. */
7324 constructor_max_index =
7325 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7326 constructor_index = bitsize_int (0);
7327 constructor_unfilled_index = constructor_index;
7329 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7331 if (TYPE_DOMAIN (constructor_type))
7333 constructor_max_index
7334 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7336 /* Detect non-empty initializations of zero-length arrays. */
7337 if (constructor_max_index == NULL_TREE
7338 && TYPE_SIZE (constructor_type))
7339 constructor_max_index = integer_minus_one_node;
7341 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7342 to initialize VLAs will cause a proper error; avoid tree
7343 checking errors as well by setting a safe value. */
7344 if (constructor_max_index
7345 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7346 constructor_max_index = integer_minus_one_node;
7348 constructor_index
7349 = convert (bitsizetype,
7350 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7352 else
7353 constructor_index = bitsize_zero_node;
7355 constructor_unfilled_index = constructor_index;
7356 if (value && TREE_CODE (value) == STRING_CST)
7358 /* We need to split the char/wchar array into individual
7359 characters, so that we don't have to special case it
7360 everywhere. */
7361 set_nonincremental_init_from_string (value, braced_init_obstack);
7364 else
7366 if (constructor_type != error_mark_node)
7367 warning_init (input_location, 0, "braces around scalar initializer");
7368 constructor_fields = constructor_type;
7369 constructor_unfilled_fields = constructor_type;
7373 /* At the end of an implicit or explicit brace level,
7374 finish up that level of constructor. If a single expression
7375 with redundant braces initialized that level, return the
7376 c_expr structure for that expression. Otherwise, the original_code
7377 element is set to ERROR_MARK.
7378 If we were outputting the elements as they are read, return 0 as the value
7379 from inner levels (process_init_element ignores that),
7380 but return error_mark_node as the value from the outermost level
7381 (that's what we want to put in DECL_INITIAL).
7382 Otherwise, return a CONSTRUCTOR expression as the value. */
7384 struct c_expr
7385 pop_init_level (location_t loc, int implicit,
7386 struct obstack *braced_init_obstack)
7388 struct constructor_stack *p;
7389 struct c_expr ret;
7390 ret.value = 0;
7391 ret.original_code = ERROR_MARK;
7392 ret.original_type = NULL;
7394 if (implicit == 0)
7396 /* When we come to an explicit close brace,
7397 pop any inner levels that didn't have explicit braces. */
7398 while (constructor_stack->implicit)
7399 process_init_element (input_location,
7400 pop_init_level (loc, 1, braced_init_obstack),
7401 true, braced_init_obstack);
7402 gcc_assert (!constructor_range_stack);
7405 /* Now output all pending elements. */
7406 constructor_incremental = 1;
7407 output_pending_init_elements (1, braced_init_obstack);
7409 p = constructor_stack;
7411 /* Error for initializing a flexible array member, or a zero-length
7412 array member in an inappropriate context. */
7413 if (constructor_type && constructor_fields
7414 && TREE_CODE (constructor_type) == ARRAY_TYPE
7415 && TYPE_DOMAIN (constructor_type)
7416 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7418 /* Silently discard empty initializations. The parser will
7419 already have pedwarned for empty brackets. */
7420 if (integer_zerop (constructor_unfilled_index))
7421 constructor_type = NULL_TREE;
7422 else
7424 gcc_assert (!TYPE_SIZE (constructor_type));
7426 if (constructor_depth > 2)
7427 error_init (loc, "initialization of flexible array member in a nested context");
7428 else
7429 pedwarn_init (loc, OPT_Wpedantic,
7430 "initialization of a flexible array member");
7432 /* We have already issued an error message for the existence
7433 of a flexible array member not at the end of the structure.
7434 Discard the initializer so that we do not die later. */
7435 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7436 constructor_type = NULL_TREE;
7440 if (vec_safe_length (constructor_elements) != 1)
7441 constructor_zeroinit = 0;
7443 /* Warn when some structs are initialized with direct aggregation. */
7444 if (!implicit && found_missing_braces && warn_missing_braces
7445 && !constructor_zeroinit)
7447 warning_init (loc, OPT_Wmissing_braces,
7448 "missing braces around initializer");
7451 /* Warn when some struct elements are implicitly initialized to zero. */
7452 if (warn_missing_field_initializers
7453 && constructor_type
7454 && TREE_CODE (constructor_type) == RECORD_TYPE
7455 && constructor_unfilled_fields)
7457 /* Do not warn for flexible array members or zero-length arrays. */
7458 while (constructor_unfilled_fields
7459 && (!DECL_SIZE (constructor_unfilled_fields)
7460 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7461 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7463 if (constructor_unfilled_fields
7464 /* Do not warn if this level of the initializer uses member
7465 designators; it is likely to be deliberate. */
7466 && !constructor_designated
7467 /* Do not warn about initializing with ` = {0}'. */
7468 && !constructor_zeroinit)
7470 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7471 "missing initializer for field %qD of %qT",
7472 constructor_unfilled_fields,
7473 constructor_type))
7474 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7475 "%qD declared here", constructor_unfilled_fields);
7479 /* Pad out the end of the structure. */
7480 if (p->replacement_value.value)
7481 /* If this closes a superfluous brace pair,
7482 just pass out the element between them. */
7483 ret = p->replacement_value;
7484 else if (constructor_type == 0)
7486 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7487 && TREE_CODE (constructor_type) != UNION_TYPE
7488 && TREE_CODE (constructor_type) != ARRAY_TYPE
7489 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7491 /* A nonincremental scalar initializer--just return
7492 the element, after verifying there is just one. */
7493 if (vec_safe_is_empty (constructor_elements))
7495 if (!constructor_erroneous)
7496 error_init (loc, "empty scalar initializer");
7497 ret.value = error_mark_node;
7499 else if (vec_safe_length (constructor_elements) != 1)
7501 error_init (loc, "extra elements in scalar initializer");
7502 ret.value = (*constructor_elements)[0].value;
7504 else
7505 ret.value = (*constructor_elements)[0].value;
7507 else
7509 if (constructor_erroneous)
7510 ret.value = error_mark_node;
7511 else
7513 ret.value = build_constructor (constructor_type,
7514 constructor_elements);
7515 if (constructor_constant)
7516 TREE_CONSTANT (ret.value) = 1;
7517 if (constructor_constant && constructor_simple)
7518 TREE_STATIC (ret.value) = 1;
7519 if (constructor_nonconst)
7520 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7524 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7526 if (constructor_nonconst)
7527 ret.original_code = C_MAYBE_CONST_EXPR;
7528 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7529 ret.original_code = ERROR_MARK;
7532 constructor_type = p->type;
7533 constructor_fields = p->fields;
7534 constructor_index = p->index;
7535 constructor_max_index = p->max_index;
7536 constructor_unfilled_index = p->unfilled_index;
7537 constructor_unfilled_fields = p->unfilled_fields;
7538 constructor_bit_index = p->bit_index;
7539 constructor_elements = p->elements;
7540 constructor_constant = p->constant;
7541 constructor_simple = p->simple;
7542 constructor_nonconst = p->nonconst;
7543 constructor_erroneous = p->erroneous;
7544 constructor_incremental = p->incremental;
7545 constructor_designated = p->designated;
7546 designator_depth = p->designator_depth;
7547 constructor_pending_elts = p->pending_elts;
7548 constructor_depth = p->depth;
7549 if (!p->implicit)
7550 constructor_range_stack = p->range_stack;
7551 RESTORE_SPELLING_DEPTH (constructor_depth);
7553 constructor_stack = p->next;
7554 free (p);
7556 if (ret.value == 0 && constructor_stack == 0)
7557 ret.value = error_mark_node;
7558 return ret;
7561 /* Common handling for both array range and field name designators.
7562 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7564 static int
7565 set_designator (location_t loc, int array,
7566 struct obstack *braced_init_obstack)
7568 tree subtype;
7569 enum tree_code subcode;
7571 /* Don't die if an entire brace-pair level is superfluous
7572 in the containing level. */
7573 if (constructor_type == 0)
7574 return 1;
7576 /* If there were errors in this designator list already, bail out
7577 silently. */
7578 if (designator_erroneous)
7579 return 1;
7581 if (!designator_depth)
7583 gcc_assert (!constructor_range_stack);
7585 /* Designator list starts at the level of closest explicit
7586 braces. */
7587 while (constructor_stack->implicit)
7588 process_init_element (input_location,
7589 pop_init_level (loc, 1, braced_init_obstack),
7590 true, braced_init_obstack);
7591 constructor_designated = 1;
7592 return 0;
7595 switch (TREE_CODE (constructor_type))
7597 case RECORD_TYPE:
7598 case UNION_TYPE:
7599 subtype = TREE_TYPE (constructor_fields);
7600 if (subtype != error_mark_node)
7601 subtype = TYPE_MAIN_VARIANT (subtype);
7602 break;
7603 case ARRAY_TYPE:
7604 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7605 break;
7606 default:
7607 gcc_unreachable ();
7610 subcode = TREE_CODE (subtype);
7611 if (array && subcode != ARRAY_TYPE)
7613 error_init (loc, "array index in non-array initializer");
7614 return 1;
7616 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7618 error_init (loc, "field name not in record or union initializer");
7619 return 1;
7622 constructor_designated = 1;
7623 push_init_level (loc, 2, braced_init_obstack);
7624 return 0;
7627 /* If there are range designators in designator list, push a new designator
7628 to constructor_range_stack. RANGE_END is end of such stack range or
7629 NULL_TREE if there is no range designator at this level. */
7631 static void
7632 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7634 struct constructor_range_stack *p;
7636 p = (struct constructor_range_stack *)
7637 obstack_alloc (braced_init_obstack,
7638 sizeof (struct constructor_range_stack));
7639 p->prev = constructor_range_stack;
7640 p->next = 0;
7641 p->fields = constructor_fields;
7642 p->range_start = constructor_index;
7643 p->index = constructor_index;
7644 p->stack = constructor_stack;
7645 p->range_end = range_end;
7646 if (constructor_range_stack)
7647 constructor_range_stack->next = p;
7648 constructor_range_stack = p;
7651 /* Within an array initializer, specify the next index to be initialized.
7652 FIRST is that index. If LAST is nonzero, then initialize a range
7653 of indices, running from FIRST through LAST. */
7655 void
7656 set_init_index (location_t loc, tree first, tree last,
7657 struct obstack *braced_init_obstack)
7659 if (set_designator (loc, 1, braced_init_obstack))
7660 return;
7662 designator_erroneous = 1;
7664 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7665 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7667 error_init (loc, "array index in initializer not of integer type");
7668 return;
7671 if (TREE_CODE (first) != INTEGER_CST)
7673 first = c_fully_fold (first, false, NULL);
7674 if (TREE_CODE (first) == INTEGER_CST)
7675 pedwarn_init (loc, OPT_Wpedantic,
7676 "array index in initializer is not "
7677 "an integer constant expression");
7680 if (last && TREE_CODE (last) != INTEGER_CST)
7682 last = c_fully_fold (last, false, NULL);
7683 if (TREE_CODE (last) == INTEGER_CST)
7684 pedwarn_init (loc, OPT_Wpedantic,
7685 "array index in initializer is not "
7686 "an integer constant expression");
7689 if (TREE_CODE (first) != INTEGER_CST)
7690 error_init (loc, "nonconstant array index in initializer");
7691 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7692 error_init (loc, "nonconstant array index in initializer");
7693 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7694 error_init (loc, "array index in non-array initializer");
7695 else if (tree_int_cst_sgn (first) == -1)
7696 error_init (loc, "array index in initializer exceeds array bounds");
7697 else if (constructor_max_index
7698 && tree_int_cst_lt (constructor_max_index, first))
7699 error_init (loc, "array index in initializer exceeds array bounds");
7700 else
7702 constant_expression_warning (first);
7703 if (last)
7704 constant_expression_warning (last);
7705 constructor_index = convert (bitsizetype, first);
7706 if (tree_int_cst_lt (constructor_index, first))
7708 constructor_index = copy_node (constructor_index);
7709 TREE_OVERFLOW (constructor_index) = 1;
7712 if (last)
7714 if (tree_int_cst_equal (first, last))
7715 last = 0;
7716 else if (tree_int_cst_lt (last, first))
7718 error_init (loc, "empty index range in initializer");
7719 last = 0;
7721 else
7723 last = convert (bitsizetype, last);
7724 if (constructor_max_index != 0
7725 && tree_int_cst_lt (constructor_max_index, last))
7727 error_init (loc, "array index range in initializer exceeds "
7728 "array bounds");
7729 last = 0;
7734 designator_depth++;
7735 designator_erroneous = 0;
7736 if (constructor_range_stack || last)
7737 push_range_stack (last, braced_init_obstack);
7741 /* Within a struct initializer, specify the next field to be initialized. */
7743 void
7744 set_init_label (location_t loc, tree fieldname,
7745 struct obstack *braced_init_obstack)
7747 tree field;
7749 if (set_designator (loc, 0, braced_init_obstack))
7750 return;
7752 designator_erroneous = 1;
7754 if (TREE_CODE (constructor_type) != RECORD_TYPE
7755 && TREE_CODE (constructor_type) != UNION_TYPE)
7757 error_init (loc, "field name not in record or union initializer");
7758 return;
7761 field = lookup_field (constructor_type, fieldname);
7763 if (field == 0)
7764 error ("unknown field %qE specified in initializer", fieldname);
7765 else
7768 constructor_fields = TREE_VALUE (field);
7769 designator_depth++;
7770 designator_erroneous = 0;
7771 if (constructor_range_stack)
7772 push_range_stack (NULL_TREE, braced_init_obstack);
7773 field = TREE_CHAIN (field);
7774 if (field)
7776 if (set_designator (loc, 0, braced_init_obstack))
7777 return;
7780 while (field != NULL_TREE);
7783 /* Add a new initializer to the tree of pending initializers. PURPOSE
7784 identifies the initializer, either array index or field in a structure.
7785 VALUE is the value of that index or field. If ORIGTYPE is not
7786 NULL_TREE, it is the original type of VALUE.
7788 IMPLICIT is true if value comes from pop_init_level (1),
7789 the new initializer has been merged with the existing one
7790 and thus no warnings should be emitted about overriding an
7791 existing initializer. */
7793 static void
7794 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7795 bool implicit, struct obstack *braced_init_obstack)
7797 struct init_node *p, **q, *r;
7799 q = &constructor_pending_elts;
7800 p = 0;
7802 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7804 while (*q != 0)
7806 p = *q;
7807 if (tree_int_cst_lt (purpose, p->purpose))
7808 q = &p->left;
7809 else if (tree_int_cst_lt (p->purpose, purpose))
7810 q = &p->right;
7811 else
7813 if (!implicit)
7815 if (TREE_SIDE_EFFECTS (p->value))
7816 warning_init (loc, 0,
7817 "initialized field with side-effects "
7818 "overwritten");
7819 else if (warn_override_init)
7820 warning_init (loc, OPT_Woverride_init,
7821 "initialized field overwritten");
7823 p->value = value;
7824 p->origtype = origtype;
7825 return;
7829 else
7831 tree bitpos;
7833 bitpos = bit_position (purpose);
7834 while (*q != NULL)
7836 p = *q;
7837 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7838 q = &p->left;
7839 else if (p->purpose != purpose)
7840 q = &p->right;
7841 else
7843 if (!implicit)
7845 if (TREE_SIDE_EFFECTS (p->value))
7846 warning_init (loc, 0,
7847 "initialized field with side-effects "
7848 "overwritten");
7849 else if (warn_override_init)
7850 warning_init (loc, OPT_Woverride_init,
7851 "initialized field overwritten");
7853 p->value = value;
7854 p->origtype = origtype;
7855 return;
7860 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7861 sizeof (struct init_node));
7862 r->purpose = purpose;
7863 r->value = value;
7864 r->origtype = origtype;
7866 *q = r;
7867 r->parent = p;
7868 r->left = 0;
7869 r->right = 0;
7870 r->balance = 0;
7872 while (p)
7874 struct init_node *s;
7876 if (r == p->left)
7878 if (p->balance == 0)
7879 p->balance = -1;
7880 else if (p->balance < 0)
7882 if (r->balance < 0)
7884 /* L rotation. */
7885 p->left = r->right;
7886 if (p->left)
7887 p->left->parent = p;
7888 r->right = p;
7890 p->balance = 0;
7891 r->balance = 0;
7893 s = p->parent;
7894 p->parent = r;
7895 r->parent = s;
7896 if (s)
7898 if (s->left == p)
7899 s->left = r;
7900 else
7901 s->right = r;
7903 else
7904 constructor_pending_elts = r;
7906 else
7908 /* LR rotation. */
7909 struct init_node *t = r->right;
7911 r->right = t->left;
7912 if (r->right)
7913 r->right->parent = r;
7914 t->left = r;
7916 p->left = t->right;
7917 if (p->left)
7918 p->left->parent = p;
7919 t->right = p;
7921 p->balance = t->balance < 0;
7922 r->balance = -(t->balance > 0);
7923 t->balance = 0;
7925 s = p->parent;
7926 p->parent = t;
7927 r->parent = t;
7928 t->parent = s;
7929 if (s)
7931 if (s->left == p)
7932 s->left = t;
7933 else
7934 s->right = t;
7936 else
7937 constructor_pending_elts = t;
7939 break;
7941 else
7943 /* p->balance == +1; growth of left side balances the node. */
7944 p->balance = 0;
7945 break;
7948 else /* r == p->right */
7950 if (p->balance == 0)
7951 /* Growth propagation from right side. */
7952 p->balance++;
7953 else if (p->balance > 0)
7955 if (r->balance > 0)
7957 /* R rotation. */
7958 p->right = r->left;
7959 if (p->right)
7960 p->right->parent = p;
7961 r->left = p;
7963 p->balance = 0;
7964 r->balance = 0;
7966 s = p->parent;
7967 p->parent = r;
7968 r->parent = s;
7969 if (s)
7971 if (s->left == p)
7972 s->left = r;
7973 else
7974 s->right = r;
7976 else
7977 constructor_pending_elts = r;
7979 else /* r->balance == -1 */
7981 /* RL rotation */
7982 struct init_node *t = r->left;
7984 r->left = t->right;
7985 if (r->left)
7986 r->left->parent = r;
7987 t->right = r;
7989 p->right = t->left;
7990 if (p->right)
7991 p->right->parent = p;
7992 t->left = p;
7994 r->balance = (t->balance < 0);
7995 p->balance = -(t->balance > 0);
7996 t->balance = 0;
7998 s = p->parent;
7999 p->parent = t;
8000 r->parent = t;
8001 t->parent = s;
8002 if (s)
8004 if (s->left == p)
8005 s->left = t;
8006 else
8007 s->right = t;
8009 else
8010 constructor_pending_elts = t;
8012 break;
8014 else
8016 /* p->balance == -1; growth of right side balances the node. */
8017 p->balance = 0;
8018 break;
8022 r = p;
8023 p = p->parent;
8027 /* Build AVL tree from a sorted chain. */
8029 static void
8030 set_nonincremental_init (struct obstack * braced_init_obstack)
8032 unsigned HOST_WIDE_INT ix;
8033 tree index, value;
8035 if (TREE_CODE (constructor_type) != RECORD_TYPE
8036 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8037 return;
8039 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8040 add_pending_init (input_location, index, value, NULL_TREE, true,
8041 braced_init_obstack);
8042 constructor_elements = NULL;
8043 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8045 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8046 /* Skip any nameless bit fields at the beginning. */
8047 while (constructor_unfilled_fields != 0
8048 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8049 && DECL_NAME (constructor_unfilled_fields) == 0)
8050 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8053 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8055 if (TYPE_DOMAIN (constructor_type))
8056 constructor_unfilled_index
8057 = convert (bitsizetype,
8058 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8059 else
8060 constructor_unfilled_index = bitsize_zero_node;
8062 constructor_incremental = 0;
8065 /* Build AVL tree from a string constant. */
8067 static void
8068 set_nonincremental_init_from_string (tree str,
8069 struct obstack * braced_init_obstack)
8071 tree value, purpose, type;
8072 HOST_WIDE_INT val[2];
8073 const char *p, *end;
8074 int byte, wchar_bytes, charwidth, bitpos;
8076 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8078 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8079 charwidth = TYPE_PRECISION (char_type_node);
8080 type = TREE_TYPE (constructor_type);
8081 p = TREE_STRING_POINTER (str);
8082 end = p + TREE_STRING_LENGTH (str);
8084 for (purpose = bitsize_zero_node;
8085 p < end
8086 && !(constructor_max_index
8087 && tree_int_cst_lt (constructor_max_index, purpose));
8088 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8090 if (wchar_bytes == 1)
8092 val[0] = (unsigned char) *p++;
8093 val[1] = 0;
8095 else
8097 val[1] = 0;
8098 val[0] = 0;
8099 for (byte = 0; byte < wchar_bytes; byte++)
8101 if (BYTES_BIG_ENDIAN)
8102 bitpos = (wchar_bytes - byte - 1) * charwidth;
8103 else
8104 bitpos = byte * charwidth;
8105 val[bitpos % HOST_BITS_PER_WIDE_INT]
8106 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8107 << (bitpos % HOST_BITS_PER_WIDE_INT);
8111 if (!TYPE_UNSIGNED (type))
8113 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8114 if (bitpos < HOST_BITS_PER_WIDE_INT)
8116 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8118 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8119 val[1] = -1;
8122 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8124 if (val[0] < 0)
8125 val[1] = -1;
8127 else if (val[1] & (((HOST_WIDE_INT) 1)
8128 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8129 val[1] |= ((HOST_WIDE_INT) -1)
8130 << (bitpos - HOST_BITS_PER_WIDE_INT);
8133 value = wide_int_to_tree (type,
8134 wide_int::from_array (val, 2,
8135 HOST_BITS_PER_WIDE_INT * 2));
8136 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8137 braced_init_obstack);
8140 constructor_incremental = 0;
8143 /* Return value of FIELD in pending initializer or zero if the field was
8144 not initialized yet. */
8146 static tree
8147 find_init_member (tree field, struct obstack * braced_init_obstack)
8149 struct init_node *p;
8151 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8153 if (constructor_incremental
8154 && tree_int_cst_lt (field, constructor_unfilled_index))
8155 set_nonincremental_init (braced_init_obstack);
8157 p = constructor_pending_elts;
8158 while (p)
8160 if (tree_int_cst_lt (field, p->purpose))
8161 p = p->left;
8162 else if (tree_int_cst_lt (p->purpose, field))
8163 p = p->right;
8164 else
8165 return p->value;
8168 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8170 tree bitpos = bit_position (field);
8172 if (constructor_incremental
8173 && (!constructor_unfilled_fields
8174 || tree_int_cst_lt (bitpos,
8175 bit_position (constructor_unfilled_fields))))
8176 set_nonincremental_init (braced_init_obstack);
8178 p = constructor_pending_elts;
8179 while (p)
8181 if (field == p->purpose)
8182 return p->value;
8183 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8184 p = p->left;
8185 else
8186 p = p->right;
8189 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8191 if (!vec_safe_is_empty (constructor_elements)
8192 && (constructor_elements->last ().index == field))
8193 return constructor_elements->last ().value;
8195 return 0;
8198 /* "Output" the next constructor element.
8199 At top level, really output it to assembler code now.
8200 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8201 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8202 TYPE is the data type that the containing data type wants here.
8203 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8204 If VALUE is a string constant, STRICT_STRING is true if it is
8205 unparenthesized or we should not warn here for it being parenthesized.
8206 For other types of VALUE, STRICT_STRING is not used.
8208 PENDING if non-nil means output pending elements that belong
8209 right after this element. (PENDING is normally 1;
8210 it is 0 while outputting pending elements, to avoid recursion.)
8212 IMPLICIT is true if value comes from pop_init_level (1),
8213 the new initializer has been merged with the existing one
8214 and thus no warnings should be emitted about overriding an
8215 existing initializer. */
8217 static void
8218 output_init_element (location_t loc, tree value, tree origtype,
8219 bool strict_string, tree type, tree field, int pending,
8220 bool implicit, struct obstack * braced_init_obstack)
8222 tree semantic_type = NULL_TREE;
8223 bool maybe_const = true;
8224 bool npc;
8226 if (type == error_mark_node || value == error_mark_node)
8228 constructor_erroneous = 1;
8229 return;
8231 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8232 && (TREE_CODE (value) == STRING_CST
8233 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8234 && !(TREE_CODE (value) == STRING_CST
8235 && TREE_CODE (type) == ARRAY_TYPE
8236 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8237 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8238 TYPE_MAIN_VARIANT (type)))
8239 value = array_to_pointer_conversion (input_location, value);
8241 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8242 && require_constant_value && !flag_isoc99 && pending)
8244 /* As an extension, allow initializing objects with static storage
8245 duration with compound literals (which are then treated just as
8246 the brace enclosed list they contain). */
8247 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8248 value = DECL_INITIAL (decl);
8251 npc = null_pointer_constant_p (value);
8252 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8254 semantic_type = TREE_TYPE (value);
8255 value = TREE_OPERAND (value, 0);
8257 value = c_fully_fold (value, require_constant_value, &maybe_const);
8259 if (value == error_mark_node)
8260 constructor_erroneous = 1;
8261 else if (!TREE_CONSTANT (value))
8262 constructor_constant = 0;
8263 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8264 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8265 || TREE_CODE (constructor_type) == UNION_TYPE)
8266 && DECL_C_BIT_FIELD (field)
8267 && TREE_CODE (value) != INTEGER_CST))
8268 constructor_simple = 0;
8269 if (!maybe_const)
8270 constructor_nonconst = 1;
8272 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8274 if (require_constant_value)
8276 error_init (loc, "initializer element is not constant");
8277 value = error_mark_node;
8279 else if (require_constant_elements)
8280 pedwarn (loc, OPT_Wpedantic,
8281 "initializer element is not computable at load time");
8283 else if (!maybe_const
8284 && (require_constant_value || require_constant_elements))
8285 pedwarn_init (loc, OPT_Wpedantic,
8286 "initializer element is not a constant expression");
8288 /* Issue -Wc++-compat warnings about initializing a bitfield with
8289 enum type. */
8290 if (warn_cxx_compat
8291 && field != NULL_TREE
8292 && TREE_CODE (field) == FIELD_DECL
8293 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8294 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8295 != TYPE_MAIN_VARIANT (type))
8296 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8298 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8299 if (checktype != error_mark_node
8300 && (TYPE_MAIN_VARIANT (checktype)
8301 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8302 warning_init (loc, OPT_Wc___compat,
8303 "enum conversion in initialization is invalid in C++");
8306 /* If this field is empty (and not at the end of structure),
8307 don't do anything other than checking the initializer. */
8308 if (field
8309 && (TREE_TYPE (field) == error_mark_node
8310 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8311 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8312 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8313 || DECL_CHAIN (field)))))
8314 return;
8316 if (semantic_type)
8317 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8318 value = digest_init (loc, type, value, origtype, npc, strict_string,
8319 require_constant_value);
8320 if (value == error_mark_node)
8322 constructor_erroneous = 1;
8323 return;
8325 if (require_constant_value || require_constant_elements)
8326 constant_expression_warning (value);
8328 /* If this element doesn't come next in sequence,
8329 put it on constructor_pending_elts. */
8330 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8331 && (!constructor_incremental
8332 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8334 if (constructor_incremental
8335 && tree_int_cst_lt (field, constructor_unfilled_index))
8336 set_nonincremental_init (braced_init_obstack);
8338 add_pending_init (loc, field, value, origtype, implicit,
8339 braced_init_obstack);
8340 return;
8342 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8343 && (!constructor_incremental
8344 || field != constructor_unfilled_fields))
8346 /* We do this for records but not for unions. In a union,
8347 no matter which field is specified, it can be initialized
8348 right away since it starts at the beginning of the union. */
8349 if (constructor_incremental)
8351 if (!constructor_unfilled_fields)
8352 set_nonincremental_init (braced_init_obstack);
8353 else
8355 tree bitpos, unfillpos;
8357 bitpos = bit_position (field);
8358 unfillpos = bit_position (constructor_unfilled_fields);
8360 if (tree_int_cst_lt (bitpos, unfillpos))
8361 set_nonincremental_init (braced_init_obstack);
8365 add_pending_init (loc, field, value, origtype, implicit,
8366 braced_init_obstack);
8367 return;
8369 else if (TREE_CODE (constructor_type) == UNION_TYPE
8370 && !vec_safe_is_empty (constructor_elements))
8372 if (!implicit)
8374 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8375 warning_init (loc, 0,
8376 "initialized field with side-effects overwritten");
8377 else if (warn_override_init)
8378 warning_init (loc, OPT_Woverride_init,
8379 "initialized field overwritten");
8382 /* We can have just one union field set. */
8383 constructor_elements = NULL;
8386 /* Otherwise, output this element either to
8387 constructor_elements or to the assembler file. */
8389 constructor_elt celt = {field, value};
8390 vec_safe_push (constructor_elements, celt);
8392 /* Advance the variable that indicates sequential elements output. */
8393 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8394 constructor_unfilled_index
8395 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8396 bitsize_one_node);
8397 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8399 constructor_unfilled_fields
8400 = DECL_CHAIN (constructor_unfilled_fields);
8402 /* Skip any nameless bit fields. */
8403 while (constructor_unfilled_fields != 0
8404 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8405 && DECL_NAME (constructor_unfilled_fields) == 0)
8406 constructor_unfilled_fields =
8407 DECL_CHAIN (constructor_unfilled_fields);
8409 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8410 constructor_unfilled_fields = 0;
8412 /* Now output any pending elements which have become next. */
8413 if (pending)
8414 output_pending_init_elements (0, braced_init_obstack);
8417 /* Output any pending elements which have become next.
8418 As we output elements, constructor_unfilled_{fields,index}
8419 advances, which may cause other elements to become next;
8420 if so, they too are output.
8422 If ALL is 0, we return when there are
8423 no more pending elements to output now.
8425 If ALL is 1, we output space as necessary so that
8426 we can output all the pending elements. */
8427 static void
8428 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8430 struct init_node *elt = constructor_pending_elts;
8431 tree next;
8433 retry:
8435 /* Look through the whole pending tree.
8436 If we find an element that should be output now,
8437 output it. Otherwise, set NEXT to the element
8438 that comes first among those still pending. */
8440 next = 0;
8441 while (elt)
8443 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8445 if (tree_int_cst_equal (elt->purpose,
8446 constructor_unfilled_index))
8447 output_init_element (input_location, elt->value, elt->origtype,
8448 true, TREE_TYPE (constructor_type),
8449 constructor_unfilled_index, 0, false,
8450 braced_init_obstack);
8451 else if (tree_int_cst_lt (constructor_unfilled_index,
8452 elt->purpose))
8454 /* Advance to the next smaller node. */
8455 if (elt->left)
8456 elt = elt->left;
8457 else
8459 /* We have reached the smallest node bigger than the
8460 current unfilled index. Fill the space first. */
8461 next = elt->purpose;
8462 break;
8465 else
8467 /* Advance to the next bigger node. */
8468 if (elt->right)
8469 elt = elt->right;
8470 else
8472 /* We have reached the biggest node in a subtree. Find
8473 the parent of it, which is the next bigger node. */
8474 while (elt->parent && elt->parent->right == elt)
8475 elt = elt->parent;
8476 elt = elt->parent;
8477 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8478 elt->purpose))
8480 next = elt->purpose;
8481 break;
8486 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8487 || TREE_CODE (constructor_type) == UNION_TYPE)
8489 tree ctor_unfilled_bitpos, elt_bitpos;
8491 /* If the current record is complete we are done. */
8492 if (constructor_unfilled_fields == 0)
8493 break;
8495 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8496 elt_bitpos = bit_position (elt->purpose);
8497 /* We can't compare fields here because there might be empty
8498 fields in between. */
8499 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8501 constructor_unfilled_fields = elt->purpose;
8502 output_init_element (input_location, elt->value, elt->origtype,
8503 true, TREE_TYPE (elt->purpose),
8504 elt->purpose, 0, false,
8505 braced_init_obstack);
8507 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8509 /* Advance to the next smaller node. */
8510 if (elt->left)
8511 elt = elt->left;
8512 else
8514 /* We have reached the smallest node bigger than the
8515 current unfilled field. Fill the space first. */
8516 next = elt->purpose;
8517 break;
8520 else
8522 /* Advance to the next bigger node. */
8523 if (elt->right)
8524 elt = elt->right;
8525 else
8527 /* We have reached the biggest node in a subtree. Find
8528 the parent of it, which is the next bigger node. */
8529 while (elt->parent && elt->parent->right == elt)
8530 elt = elt->parent;
8531 elt = elt->parent;
8532 if (elt
8533 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8534 bit_position (elt->purpose))))
8536 next = elt->purpose;
8537 break;
8544 /* Ordinarily return, but not if we want to output all
8545 and there are elements left. */
8546 if (!(all && next != 0))
8547 return;
8549 /* If it's not incremental, just skip over the gap, so that after
8550 jumping to retry we will output the next successive element. */
8551 if (TREE_CODE (constructor_type) == RECORD_TYPE
8552 || TREE_CODE (constructor_type) == UNION_TYPE)
8553 constructor_unfilled_fields = next;
8554 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8555 constructor_unfilled_index = next;
8557 /* ELT now points to the node in the pending tree with the next
8558 initializer to output. */
8559 goto retry;
8562 /* Add one non-braced element to the current constructor level.
8563 This adjusts the current position within the constructor's type.
8564 This may also start or terminate implicit levels
8565 to handle a partly-braced initializer.
8567 Once this has found the correct level for the new element,
8568 it calls output_init_element.
8570 IMPLICIT is true if value comes from pop_init_level (1),
8571 the new initializer has been merged with the existing one
8572 and thus no warnings should be emitted about overriding an
8573 existing initializer. */
8575 void
8576 process_init_element (location_t loc, struct c_expr value, bool implicit,
8577 struct obstack * braced_init_obstack)
8579 tree orig_value = value.value;
8580 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8581 bool strict_string = value.original_code == STRING_CST;
8582 bool was_designated = designator_depth != 0;
8584 designator_depth = 0;
8585 designator_erroneous = 0;
8587 if (!implicit && value.value && !integer_zerop (value.value))
8588 constructor_zeroinit = 0;
8590 /* Handle superfluous braces around string cst as in
8591 char x[] = {"foo"}; */
8592 if (string_flag
8593 && constructor_type
8594 && !was_designated
8595 && TREE_CODE (constructor_type) == ARRAY_TYPE
8596 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8597 && integer_zerop (constructor_unfilled_index))
8599 if (constructor_stack->replacement_value.value)
8600 error_init (loc, "excess elements in char array initializer");
8601 constructor_stack->replacement_value = value;
8602 return;
8605 if (constructor_stack->replacement_value.value != 0)
8607 error_init (loc, "excess elements in struct initializer");
8608 return;
8611 /* Ignore elements of a brace group if it is entirely superfluous
8612 and has already been diagnosed. */
8613 if (constructor_type == 0)
8614 return;
8616 if (!implicit && warn_designated_init && !was_designated
8617 && TREE_CODE (constructor_type) == RECORD_TYPE
8618 && lookup_attribute ("designated_init",
8619 TYPE_ATTRIBUTES (constructor_type)))
8620 warning_init (loc,
8621 OPT_Wdesignated_init,
8622 "positional initialization of field "
8623 "in %<struct%> declared with %<designated_init%> attribute");
8625 /* If we've exhausted any levels that didn't have braces,
8626 pop them now. */
8627 while (constructor_stack->implicit)
8629 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8630 || TREE_CODE (constructor_type) == UNION_TYPE)
8631 && constructor_fields == 0)
8632 process_init_element (loc,
8633 pop_init_level (loc, 1, braced_init_obstack),
8634 true, braced_init_obstack);
8635 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8636 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8637 && constructor_max_index
8638 && tree_int_cst_lt (constructor_max_index,
8639 constructor_index))
8640 process_init_element (loc,
8641 pop_init_level (loc, 1, braced_init_obstack),
8642 true, braced_init_obstack);
8643 else
8644 break;
8647 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8648 if (constructor_range_stack)
8650 /* If value is a compound literal and we'll be just using its
8651 content, don't put it into a SAVE_EXPR. */
8652 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8653 || !require_constant_value
8654 || flag_isoc99)
8656 tree semantic_type = NULL_TREE;
8657 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8659 semantic_type = TREE_TYPE (value.value);
8660 value.value = TREE_OPERAND (value.value, 0);
8662 value.value = c_save_expr (value.value);
8663 if (semantic_type)
8664 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8665 value.value);
8669 while (1)
8671 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8673 tree fieldtype;
8674 enum tree_code fieldcode;
8676 if (constructor_fields == 0)
8678 pedwarn_init (loc, 0, "excess elements in struct initializer");
8679 break;
8682 fieldtype = TREE_TYPE (constructor_fields);
8683 if (fieldtype != error_mark_node)
8684 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8685 fieldcode = TREE_CODE (fieldtype);
8687 /* Error for non-static initialization of a flexible array member. */
8688 if (fieldcode == ARRAY_TYPE
8689 && !require_constant_value
8690 && TYPE_SIZE (fieldtype) == NULL_TREE
8691 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8693 error_init (loc, "non-static initialization of a flexible "
8694 "array member");
8695 break;
8698 /* Accept a string constant to initialize a subarray. */
8699 if (value.value != 0
8700 && fieldcode == ARRAY_TYPE
8701 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8702 && string_flag)
8703 value.value = orig_value;
8704 /* Otherwise, if we have come to a subaggregate,
8705 and we don't have an element of its type, push into it. */
8706 else if (value.value != 0
8707 && value.value != error_mark_node
8708 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8709 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8710 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8712 push_init_level (loc, 1, braced_init_obstack);
8713 continue;
8716 if (value.value)
8718 push_member_name (constructor_fields);
8719 output_init_element (loc, value.value, value.original_type,
8720 strict_string, fieldtype,
8721 constructor_fields, 1, implicit,
8722 braced_init_obstack);
8723 RESTORE_SPELLING_DEPTH (constructor_depth);
8725 else
8726 /* Do the bookkeeping for an element that was
8727 directly output as a constructor. */
8729 /* For a record, keep track of end position of last field. */
8730 if (DECL_SIZE (constructor_fields))
8731 constructor_bit_index
8732 = size_binop_loc (input_location, PLUS_EXPR,
8733 bit_position (constructor_fields),
8734 DECL_SIZE (constructor_fields));
8736 /* If the current field was the first one not yet written out,
8737 it isn't now, so update. */
8738 if (constructor_unfilled_fields == constructor_fields)
8740 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8741 /* Skip any nameless bit fields. */
8742 while (constructor_unfilled_fields != 0
8743 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8744 && DECL_NAME (constructor_unfilled_fields) == 0)
8745 constructor_unfilled_fields =
8746 DECL_CHAIN (constructor_unfilled_fields);
8750 constructor_fields = DECL_CHAIN (constructor_fields);
8751 /* Skip any nameless bit fields at the beginning. */
8752 while (constructor_fields != 0
8753 && DECL_C_BIT_FIELD (constructor_fields)
8754 && DECL_NAME (constructor_fields) == 0)
8755 constructor_fields = DECL_CHAIN (constructor_fields);
8757 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8759 tree fieldtype;
8760 enum tree_code fieldcode;
8762 if (constructor_fields == 0)
8764 pedwarn_init (loc, 0,
8765 "excess elements in union initializer");
8766 break;
8769 fieldtype = TREE_TYPE (constructor_fields);
8770 if (fieldtype != error_mark_node)
8771 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8772 fieldcode = TREE_CODE (fieldtype);
8774 /* Warn that traditional C rejects initialization of unions.
8775 We skip the warning if the value is zero. This is done
8776 under the assumption that the zero initializer in user
8777 code appears conditioned on e.g. __STDC__ to avoid
8778 "missing initializer" warnings and relies on default
8779 initialization to zero in the traditional C case.
8780 We also skip the warning if the initializer is designated,
8781 again on the assumption that this must be conditional on
8782 __STDC__ anyway (and we've already complained about the
8783 member-designator already). */
8784 if (!in_system_header_at (input_location) && !constructor_designated
8785 && !(value.value && (integer_zerop (value.value)
8786 || real_zerop (value.value))))
8787 warning (OPT_Wtraditional, "traditional C rejects initialization "
8788 "of unions");
8790 /* Accept a string constant to initialize a subarray. */
8791 if (value.value != 0
8792 && fieldcode == ARRAY_TYPE
8793 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8794 && string_flag)
8795 value.value = orig_value;
8796 /* Otherwise, if we have come to a subaggregate,
8797 and we don't have an element of its type, push into it. */
8798 else if (value.value != 0
8799 && value.value != error_mark_node
8800 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8801 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8802 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8804 push_init_level (loc, 1, braced_init_obstack);
8805 continue;
8808 if (value.value)
8810 push_member_name (constructor_fields);
8811 output_init_element (loc, value.value, value.original_type,
8812 strict_string, fieldtype,
8813 constructor_fields, 1, implicit,
8814 braced_init_obstack);
8815 RESTORE_SPELLING_DEPTH (constructor_depth);
8817 else
8818 /* Do the bookkeeping for an element that was
8819 directly output as a constructor. */
8821 constructor_bit_index = DECL_SIZE (constructor_fields);
8822 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8825 constructor_fields = 0;
8827 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8829 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8830 enum tree_code eltcode = TREE_CODE (elttype);
8832 /* Accept a string constant to initialize a subarray. */
8833 if (value.value != 0
8834 && eltcode == ARRAY_TYPE
8835 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8836 && string_flag)
8837 value.value = orig_value;
8838 /* Otherwise, if we have come to a subaggregate,
8839 and we don't have an element of its type, push into it. */
8840 else if (value.value != 0
8841 && value.value != error_mark_node
8842 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8843 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8844 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8846 push_init_level (loc, 1, braced_init_obstack);
8847 continue;
8850 if (constructor_max_index != 0
8851 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8852 || integer_all_onesp (constructor_max_index)))
8854 pedwarn_init (loc, 0,
8855 "excess elements in array initializer");
8856 break;
8859 /* Now output the actual element. */
8860 if (value.value)
8862 push_array_bounds (tree_to_uhwi (constructor_index));
8863 output_init_element (loc, value.value, value.original_type,
8864 strict_string, elttype,
8865 constructor_index, 1, implicit,
8866 braced_init_obstack);
8867 RESTORE_SPELLING_DEPTH (constructor_depth);
8870 constructor_index
8871 = size_binop_loc (input_location, PLUS_EXPR,
8872 constructor_index, bitsize_one_node);
8874 if (!value.value)
8875 /* If we are doing the bookkeeping for an element that was
8876 directly output as a constructor, we must update
8877 constructor_unfilled_index. */
8878 constructor_unfilled_index = constructor_index;
8880 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8882 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8884 /* Do a basic check of initializer size. Note that vectors
8885 always have a fixed size derived from their type. */
8886 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8888 pedwarn_init (loc, 0,
8889 "excess elements in vector initializer");
8890 break;
8893 /* Now output the actual element. */
8894 if (value.value)
8896 if (TREE_CODE (value.value) == VECTOR_CST)
8897 elttype = TYPE_MAIN_VARIANT (constructor_type);
8898 output_init_element (loc, value.value, value.original_type,
8899 strict_string, elttype,
8900 constructor_index, 1, implicit,
8901 braced_init_obstack);
8904 constructor_index
8905 = size_binop_loc (input_location,
8906 PLUS_EXPR, constructor_index, bitsize_one_node);
8908 if (!value.value)
8909 /* If we are doing the bookkeeping for an element that was
8910 directly output as a constructor, we must update
8911 constructor_unfilled_index. */
8912 constructor_unfilled_index = constructor_index;
8915 /* Handle the sole element allowed in a braced initializer
8916 for a scalar variable. */
8917 else if (constructor_type != error_mark_node
8918 && constructor_fields == 0)
8920 pedwarn_init (loc, 0,
8921 "excess elements in scalar initializer");
8922 break;
8924 else
8926 if (value.value)
8927 output_init_element (loc, value.value, value.original_type,
8928 strict_string, constructor_type,
8929 NULL_TREE, 1, implicit,
8930 braced_init_obstack);
8931 constructor_fields = 0;
8934 /* Handle range initializers either at this level or anywhere higher
8935 in the designator stack. */
8936 if (constructor_range_stack)
8938 struct constructor_range_stack *p, *range_stack;
8939 int finish = 0;
8941 range_stack = constructor_range_stack;
8942 constructor_range_stack = 0;
8943 while (constructor_stack != range_stack->stack)
8945 gcc_assert (constructor_stack->implicit);
8946 process_init_element (loc,
8947 pop_init_level (loc, 1,
8948 braced_init_obstack),
8949 true, braced_init_obstack);
8951 for (p = range_stack;
8952 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8953 p = p->prev)
8955 gcc_assert (constructor_stack->implicit);
8956 process_init_element (loc,
8957 pop_init_level (loc, 1,
8958 braced_init_obstack),
8959 true, braced_init_obstack);
8962 p->index = size_binop_loc (input_location,
8963 PLUS_EXPR, p->index, bitsize_one_node);
8964 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8965 finish = 1;
8967 while (1)
8969 constructor_index = p->index;
8970 constructor_fields = p->fields;
8971 if (finish && p->range_end && p->index == p->range_start)
8973 finish = 0;
8974 p->prev = 0;
8976 p = p->next;
8977 if (!p)
8978 break;
8979 push_init_level (loc, 2, braced_init_obstack);
8980 p->stack = constructor_stack;
8981 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8982 p->index = p->range_start;
8985 if (!finish)
8986 constructor_range_stack = range_stack;
8987 continue;
8990 break;
8993 constructor_range_stack = 0;
8996 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8997 (guaranteed to be 'volatile' or null) and ARGS (represented using
8998 an ASM_EXPR node). */
8999 tree
9000 build_asm_stmt (tree cv_qualifier, tree args)
9002 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9003 ASM_VOLATILE_P (args) = 1;
9004 return add_stmt (args);
9007 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9008 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9009 SIMPLE indicates whether there was anything at all after the
9010 string in the asm expression -- asm("blah") and asm("blah" : )
9011 are subtly different. We use a ASM_EXPR node to represent this. */
9012 tree
9013 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9014 tree clobbers, tree labels, bool simple)
9016 tree tail;
9017 tree args;
9018 int i;
9019 const char *constraint;
9020 const char **oconstraints;
9021 bool allows_mem, allows_reg, is_inout;
9022 int ninputs, noutputs;
9024 ninputs = list_length (inputs);
9025 noutputs = list_length (outputs);
9026 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9028 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9030 /* Remove output conversions that change the type but not the mode. */
9031 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9033 tree output = TREE_VALUE (tail);
9035 output = c_fully_fold (output, false, NULL);
9037 /* ??? Really, this should not be here. Users should be using a
9038 proper lvalue, dammit. But there's a long history of using casts
9039 in the output operands. In cases like longlong.h, this becomes a
9040 primitive form of typechecking -- if the cast can be removed, then
9041 the output operand had a type of the proper width; otherwise we'll
9042 get an error. Gross, but ... */
9043 STRIP_NOPS (output);
9045 if (!lvalue_or_else (loc, output, lv_asm))
9046 output = error_mark_node;
9048 if (output != error_mark_node
9049 && (TREE_READONLY (output)
9050 || TYPE_READONLY (TREE_TYPE (output))
9051 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9052 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9053 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9054 readonly_error (loc, output, lv_asm);
9056 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9057 oconstraints[i] = constraint;
9059 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9060 &allows_mem, &allows_reg, &is_inout))
9062 /* If the operand is going to end up in memory,
9063 mark it addressable. */
9064 if (!allows_reg && !c_mark_addressable (output))
9065 output = error_mark_node;
9066 if (!(!allows_reg && allows_mem)
9067 && output != error_mark_node
9068 && VOID_TYPE_P (TREE_TYPE (output)))
9070 error_at (loc, "invalid use of void expression");
9071 output = error_mark_node;
9074 else
9075 output = error_mark_node;
9077 TREE_VALUE (tail) = output;
9080 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9082 tree input;
9084 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9085 input = TREE_VALUE (tail);
9087 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9088 oconstraints, &allows_mem, &allows_reg))
9090 /* If the operand is going to end up in memory,
9091 mark it addressable. */
9092 if (!allows_reg && allows_mem)
9094 input = c_fully_fold (input, false, NULL);
9096 /* Strip the nops as we allow this case. FIXME, this really
9097 should be rejected or made deprecated. */
9098 STRIP_NOPS (input);
9099 if (!c_mark_addressable (input))
9100 input = error_mark_node;
9102 else
9104 struct c_expr expr;
9105 memset (&expr, 0, sizeof (expr));
9106 expr.value = input;
9107 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9108 input = c_fully_fold (expr.value, false, NULL);
9110 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9112 error_at (loc, "invalid use of void expression");
9113 input = error_mark_node;
9117 else
9118 input = error_mark_node;
9120 TREE_VALUE (tail) = input;
9123 /* ASMs with labels cannot have outputs. This should have been
9124 enforced by the parser. */
9125 gcc_assert (outputs == NULL || labels == NULL);
9127 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9129 /* asm statements without outputs, including simple ones, are treated
9130 as volatile. */
9131 ASM_INPUT_P (args) = simple;
9132 ASM_VOLATILE_P (args) = (noutputs == 0);
9134 return args;
9137 /* Generate a goto statement to LABEL. LOC is the location of the
9138 GOTO. */
9140 tree
9141 c_finish_goto_label (location_t loc, tree label)
9143 tree decl = lookup_label_for_goto (loc, label);
9144 if (!decl)
9145 return NULL_TREE;
9146 TREE_USED (decl) = 1;
9148 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9149 SET_EXPR_LOCATION (t, loc);
9150 return add_stmt (t);
9154 /* Generate a computed goto statement to EXPR. LOC is the location of
9155 the GOTO. */
9157 tree
9158 c_finish_goto_ptr (location_t loc, tree expr)
9160 tree t;
9161 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9162 expr = c_fully_fold (expr, false, NULL);
9163 expr = convert (ptr_type_node, expr);
9164 t = build1 (GOTO_EXPR, void_type_node, expr);
9165 SET_EXPR_LOCATION (t, loc);
9166 return add_stmt (t);
9169 /* Generate a C `return' statement. RETVAL is the expression for what
9170 to return, or a null pointer for `return;' with no value. LOC is
9171 the location of the return statement, or the location of the expression,
9172 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9173 is the original type of RETVAL. */
9175 tree
9176 c_finish_return (location_t loc, tree retval, tree origtype)
9178 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9179 bool no_warning = false;
9180 bool npc = false;
9181 size_t rank = 0;
9183 if (TREE_THIS_VOLATILE (current_function_decl))
9184 warning_at (loc, 0,
9185 "function declared %<noreturn%> has a %<return%> statement");
9187 if (flag_cilkplus && contains_array_notation_expr (retval))
9189 /* Array notations are allowed in a return statement if it is inside a
9190 built-in array notation reduction function. */
9191 if (!find_rank (loc, retval, retval, false, &rank))
9192 return error_mark_node;
9193 if (rank >= 1)
9195 error_at (loc, "array notation expression cannot be used as a "
9196 "return value");
9197 return error_mark_node;
9200 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9202 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9203 "allowed");
9204 return error_mark_node;
9206 if (retval)
9208 tree semantic_type = NULL_TREE;
9209 npc = null_pointer_constant_p (retval);
9210 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9212 semantic_type = TREE_TYPE (retval);
9213 retval = TREE_OPERAND (retval, 0);
9215 retval = c_fully_fold (retval, false, NULL);
9216 if (semantic_type)
9217 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9220 if (!retval)
9222 current_function_returns_null = 1;
9223 if ((warn_return_type || flag_isoc99)
9224 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9226 if (flag_isoc99)
9227 pedwarn (loc, 0, "%<return%> with no value, in "
9228 "function returning non-void");
9229 else
9230 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9231 "in function returning non-void");
9232 no_warning = true;
9235 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9237 current_function_returns_null = 1;
9238 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9239 pedwarn (loc, 0,
9240 "%<return%> with a value, in function returning void");
9241 else
9242 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9243 "%<return%> with expression, in function returning void");
9245 else
9247 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9248 retval, origtype, ic_return,
9249 npc, NULL_TREE, NULL_TREE, 0);
9250 tree res = DECL_RESULT (current_function_decl);
9251 tree inner;
9252 bool save;
9254 current_function_returns_value = 1;
9255 if (t == error_mark_node)
9256 return NULL_TREE;
9258 save = in_late_binary_op;
9259 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9260 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9261 in_late_binary_op = true;
9262 inner = t = convert (TREE_TYPE (res), t);
9263 in_late_binary_op = save;
9265 /* Strip any conversions, additions, and subtractions, and see if
9266 we are returning the address of a local variable. Warn if so. */
9267 while (1)
9269 switch (TREE_CODE (inner))
9271 CASE_CONVERT:
9272 case NON_LVALUE_EXPR:
9273 case PLUS_EXPR:
9274 case POINTER_PLUS_EXPR:
9275 inner = TREE_OPERAND (inner, 0);
9276 continue;
9278 case MINUS_EXPR:
9279 /* If the second operand of the MINUS_EXPR has a pointer
9280 type (or is converted from it), this may be valid, so
9281 don't give a warning. */
9283 tree op1 = TREE_OPERAND (inner, 1);
9285 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9286 && (CONVERT_EXPR_P (op1)
9287 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9288 op1 = TREE_OPERAND (op1, 0);
9290 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9291 break;
9293 inner = TREE_OPERAND (inner, 0);
9294 continue;
9297 case ADDR_EXPR:
9298 inner = TREE_OPERAND (inner, 0);
9300 while (REFERENCE_CLASS_P (inner)
9301 && TREE_CODE (inner) != INDIRECT_REF)
9302 inner = TREE_OPERAND (inner, 0);
9304 if (DECL_P (inner)
9305 && !DECL_EXTERNAL (inner)
9306 && !TREE_STATIC (inner)
9307 && DECL_CONTEXT (inner) == current_function_decl)
9309 if (TREE_CODE (inner) == LABEL_DECL)
9310 warning_at (loc, OPT_Wreturn_local_addr,
9311 "function returns address of label");
9312 else
9314 warning_at (loc, OPT_Wreturn_local_addr,
9315 "function returns address of local variable");
9316 tree zero = build_zero_cst (TREE_TYPE (res));
9317 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9320 break;
9322 default:
9323 break;
9326 break;
9329 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9330 SET_EXPR_LOCATION (retval, loc);
9332 if (warn_sequence_point)
9333 verify_sequence_points (retval);
9336 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9337 TREE_NO_WARNING (ret_stmt) |= no_warning;
9338 return add_stmt (ret_stmt);
9341 struct c_switch {
9342 /* The SWITCH_EXPR being built. */
9343 tree switch_expr;
9345 /* The original type of the testing expression, i.e. before the
9346 default conversion is applied. */
9347 tree orig_type;
9349 /* A splay-tree mapping the low element of a case range to the high
9350 element, or NULL_TREE if there is no high element. Used to
9351 determine whether or not a new case label duplicates an old case
9352 label. We need a tree, rather than simply a hash table, because
9353 of the GNU case range extension. */
9354 splay_tree cases;
9356 /* The bindings at the point of the switch. This is used for
9357 warnings crossing decls when branching to a case label. */
9358 struct c_spot_bindings *bindings;
9360 /* The next node on the stack. */
9361 struct c_switch *next;
9364 /* A stack of the currently active switch statements. The innermost
9365 switch statement is on the top of the stack. There is no need to
9366 mark the stack for garbage collection because it is only active
9367 during the processing of the body of a function, and we never
9368 collect at that point. */
9370 struct c_switch *c_switch_stack;
9372 /* Start a C switch statement, testing expression EXP. Return the new
9373 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9374 SWITCH_COND_LOC is the location of the switch's condition.
9375 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9377 tree
9378 c_start_case (location_t switch_loc,
9379 location_t switch_cond_loc,
9380 tree exp, bool explicit_cast_p)
9382 tree orig_type = error_mark_node;
9383 struct c_switch *cs;
9385 if (exp != error_mark_node)
9387 orig_type = TREE_TYPE (exp);
9389 if (!INTEGRAL_TYPE_P (orig_type))
9391 if (orig_type != error_mark_node)
9393 error_at (switch_cond_loc, "switch quantity not an integer");
9394 orig_type = error_mark_node;
9396 exp = integer_zero_node;
9398 else
9400 tree type = TYPE_MAIN_VARIANT (orig_type);
9401 tree e = exp;
9403 /* Warn if the condition has boolean value. */
9404 while (TREE_CODE (e) == COMPOUND_EXPR)
9405 e = TREE_OPERAND (e, 1);
9407 if ((TREE_CODE (type) == BOOLEAN_TYPE
9408 || truth_value_p (TREE_CODE (e)))
9409 /* Explicit cast to int suppresses this warning. */
9410 && !(TREE_CODE (type) == INTEGER_TYPE
9411 && explicit_cast_p))
9412 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9413 "switch condition has boolean value");
9415 if (!in_system_header_at (input_location)
9416 && (type == long_integer_type_node
9417 || type == long_unsigned_type_node))
9418 warning_at (switch_cond_loc,
9419 OPT_Wtraditional, "%<long%> switch expression not "
9420 "converted to %<int%> in ISO C");
9422 exp = c_fully_fold (exp, false, NULL);
9423 exp = default_conversion (exp);
9425 if (warn_sequence_point)
9426 verify_sequence_points (exp);
9430 /* Add this new SWITCH_EXPR to the stack. */
9431 cs = XNEW (struct c_switch);
9432 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9433 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9434 cs->orig_type = orig_type;
9435 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9436 cs->bindings = c_get_switch_bindings ();
9437 cs->next = c_switch_stack;
9438 c_switch_stack = cs;
9440 return add_stmt (cs->switch_expr);
9443 /* Process a case label at location LOC. */
9445 tree
9446 do_case (location_t loc, tree low_value, tree high_value)
9448 tree label = NULL_TREE;
9450 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9452 low_value = c_fully_fold (low_value, false, NULL);
9453 if (TREE_CODE (low_value) == INTEGER_CST)
9454 pedwarn (loc, OPT_Wpedantic,
9455 "case label is not an integer constant expression");
9458 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9460 high_value = c_fully_fold (high_value, false, NULL);
9461 if (TREE_CODE (high_value) == INTEGER_CST)
9462 pedwarn (input_location, OPT_Wpedantic,
9463 "case label is not an integer constant expression");
9466 if (c_switch_stack == NULL)
9468 if (low_value)
9469 error_at (loc, "case label not within a switch statement");
9470 else
9471 error_at (loc, "%<default%> label not within a switch statement");
9472 return NULL_TREE;
9475 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9476 EXPR_LOCATION (c_switch_stack->switch_expr),
9477 loc))
9478 return NULL_TREE;
9480 label = c_add_case_label (loc, c_switch_stack->cases,
9481 SWITCH_COND (c_switch_stack->switch_expr),
9482 c_switch_stack->orig_type,
9483 low_value, high_value);
9484 if (label == error_mark_node)
9485 label = NULL_TREE;
9486 return label;
9489 /* Finish the switch statement. */
9491 void
9492 c_finish_case (tree body)
9494 struct c_switch *cs = c_switch_stack;
9495 location_t switch_location;
9497 SWITCH_BODY (cs->switch_expr) = body;
9499 /* Emit warnings as needed. */
9500 switch_location = EXPR_LOCATION (cs->switch_expr);
9501 c_do_switch_warnings (cs->cases, switch_location,
9502 TREE_TYPE (cs->switch_expr),
9503 SWITCH_COND (cs->switch_expr));
9505 /* Pop the stack. */
9506 c_switch_stack = cs->next;
9507 splay_tree_delete (cs->cases);
9508 c_release_switch_bindings (cs->bindings);
9509 XDELETE (cs);
9512 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9513 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9514 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9515 statement, and was not surrounded with parenthesis. */
9517 void
9518 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9519 tree else_block, bool nested_if)
9521 tree stmt;
9523 /* If the condition has array notations, then the rank of the then_block and
9524 else_block must be either 0 or be equal to the rank of the condition. If
9525 the condition does not have array notations then break them up as it is
9526 broken up in a normal expression. */
9527 if (flag_cilkplus && contains_array_notation_expr (cond))
9529 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9530 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9531 return;
9532 if (then_block
9533 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9534 return;
9535 if (else_block
9536 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9537 return;
9538 if (cond_rank != then_rank && then_rank != 0)
9540 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9541 " and the then-block");
9542 return;
9544 else if (cond_rank != else_rank && else_rank != 0)
9546 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9547 " and the else-block");
9548 return;
9551 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9552 if (warn_parentheses && nested_if && else_block == NULL)
9554 tree inner_if = then_block;
9556 /* We know from the grammar productions that there is an IF nested
9557 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9558 it might not be exactly THEN_BLOCK, but should be the last
9559 non-container statement within. */
9560 while (1)
9561 switch (TREE_CODE (inner_if))
9563 case COND_EXPR:
9564 goto found;
9565 case BIND_EXPR:
9566 inner_if = BIND_EXPR_BODY (inner_if);
9567 break;
9568 case STATEMENT_LIST:
9569 inner_if = expr_last (then_block);
9570 break;
9571 case TRY_FINALLY_EXPR:
9572 case TRY_CATCH_EXPR:
9573 inner_if = TREE_OPERAND (inner_if, 0);
9574 break;
9575 default:
9576 gcc_unreachable ();
9578 found:
9580 if (COND_EXPR_ELSE (inner_if))
9581 warning_at (if_locus, OPT_Wparentheses,
9582 "suggest explicit braces to avoid ambiguous %<else%>");
9585 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9586 SET_EXPR_LOCATION (stmt, if_locus);
9587 add_stmt (stmt);
9590 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9591 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9592 is false for DO loops. INCR is the FOR increment expression. BODY is
9593 the statement controlled by the loop. BLAB is the break label. CLAB is
9594 the continue label. Everything is allowed to be NULL. */
9596 void
9597 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9598 tree blab, tree clab, bool cond_is_first)
9600 tree entry = NULL, exit = NULL, t;
9602 if (flag_cilkplus && contains_array_notation_expr (cond))
9604 error_at (start_locus, "array notation expression cannot be used in a "
9605 "loop%'s condition");
9606 return;
9609 /* If the condition is zero don't generate a loop construct. */
9610 if (cond && integer_zerop (cond))
9612 if (cond_is_first)
9614 t = build_and_jump (&blab);
9615 SET_EXPR_LOCATION (t, start_locus);
9616 add_stmt (t);
9619 else
9621 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9623 /* If we have an exit condition, then we build an IF with gotos either
9624 out of the loop, or to the top of it. If there's no exit condition,
9625 then we just build a jump back to the top. */
9626 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9628 if (cond && !integer_nonzerop (cond))
9630 /* Canonicalize the loop condition to the end. This means
9631 generating a branch to the loop condition. Reuse the
9632 continue label, if possible. */
9633 if (cond_is_first)
9635 if (incr || !clab)
9637 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9638 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9640 else
9641 t = build1 (GOTO_EXPR, void_type_node, clab);
9642 SET_EXPR_LOCATION (t, start_locus);
9643 add_stmt (t);
9646 t = build_and_jump (&blab);
9647 if (cond_is_first)
9648 exit = fold_build3_loc (start_locus,
9649 COND_EXPR, void_type_node, cond, exit, t);
9650 else
9651 exit = fold_build3_loc (input_location,
9652 COND_EXPR, void_type_node, cond, exit, t);
9655 add_stmt (top);
9658 if (body)
9659 add_stmt (body);
9660 if (clab)
9661 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9662 if (incr)
9663 add_stmt (incr);
9664 if (entry)
9665 add_stmt (entry);
9666 if (exit)
9667 add_stmt (exit);
9668 if (blab)
9669 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9672 tree
9673 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9675 bool skip;
9676 tree label = *label_p;
9678 /* In switch statements break is sometimes stylistically used after
9679 a return statement. This can lead to spurious warnings about
9680 control reaching the end of a non-void function when it is
9681 inlined. Note that we are calling block_may_fallthru with
9682 language specific tree nodes; this works because
9683 block_may_fallthru returns true when given something it does not
9684 understand. */
9685 skip = !block_may_fallthru (cur_stmt_list);
9687 if (!label)
9689 if (!skip)
9690 *label_p = label = create_artificial_label (loc);
9692 else if (TREE_CODE (label) == LABEL_DECL)
9694 else switch (TREE_INT_CST_LOW (label))
9696 case 0:
9697 if (is_break)
9698 error_at (loc, "break statement not within loop or switch");
9699 else
9700 error_at (loc, "continue statement not within a loop");
9701 return NULL_TREE;
9703 case 1:
9704 gcc_assert (is_break);
9705 error_at (loc, "break statement used with OpenMP for loop");
9706 return NULL_TREE;
9708 case 2:
9709 if (is_break)
9710 error ("break statement within %<#pragma simd%> loop body");
9711 else
9712 error ("continue statement within %<#pragma simd%> loop body");
9713 return NULL_TREE;
9715 default:
9716 gcc_unreachable ();
9719 if (skip)
9720 return NULL_TREE;
9722 if (!is_break)
9723 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9725 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9728 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9730 static void
9731 emit_side_effect_warnings (location_t loc, tree expr)
9733 if (expr == error_mark_node)
9735 else if (!TREE_SIDE_EFFECTS (expr))
9737 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9738 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9740 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9742 tree r = expr;
9743 location_t cloc = loc;
9744 while (TREE_CODE (r) == COMPOUND_EXPR)
9746 if (EXPR_HAS_LOCATION (r))
9747 cloc = EXPR_LOCATION (r);
9748 r = TREE_OPERAND (r, 1);
9750 if (!TREE_SIDE_EFFECTS (r)
9751 && !VOID_TYPE_P (TREE_TYPE (r))
9752 && !CONVERT_EXPR_P (r)
9753 && !TREE_NO_WARNING (r)
9754 && !TREE_NO_WARNING (expr))
9755 warning_at (cloc, OPT_Wunused_value,
9756 "right-hand operand of comma expression has no effect");
9758 else
9759 warn_if_unused_value (expr, loc);
9762 /* Process an expression as if it were a complete statement. Emit
9763 diagnostics, but do not call ADD_STMT. LOC is the location of the
9764 statement. */
9766 tree
9767 c_process_expr_stmt (location_t loc, tree expr)
9769 tree exprv;
9771 if (!expr)
9772 return NULL_TREE;
9774 expr = c_fully_fold (expr, false, NULL);
9776 if (warn_sequence_point)
9777 verify_sequence_points (expr);
9779 if (TREE_TYPE (expr) != error_mark_node
9780 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9781 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9782 error_at (loc, "expression statement has incomplete type");
9784 /* If we're not processing a statement expression, warn about unused values.
9785 Warnings for statement expressions will be emitted later, once we figure
9786 out which is the result. */
9787 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9788 && warn_unused_value)
9789 emit_side_effect_warnings (loc, expr);
9791 exprv = expr;
9792 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9793 exprv = TREE_OPERAND (exprv, 1);
9794 while (CONVERT_EXPR_P (exprv))
9795 exprv = TREE_OPERAND (exprv, 0);
9796 if (DECL_P (exprv)
9797 || handled_component_p (exprv)
9798 || TREE_CODE (exprv) == ADDR_EXPR)
9799 mark_exp_read (exprv);
9801 /* If the expression is not of a type to which we cannot assign a line
9802 number, wrap the thing in a no-op NOP_EXPR. */
9803 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9805 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9806 SET_EXPR_LOCATION (expr, loc);
9809 return expr;
9812 /* Emit an expression as a statement. LOC is the location of the
9813 expression. */
9815 tree
9816 c_finish_expr_stmt (location_t loc, tree expr)
9818 if (expr)
9819 return add_stmt (c_process_expr_stmt (loc, expr));
9820 else
9821 return NULL;
9824 /* Do the opposite and emit a statement as an expression. To begin,
9825 create a new binding level and return it. */
9827 tree
9828 c_begin_stmt_expr (void)
9830 tree ret;
9832 /* We must force a BLOCK for this level so that, if it is not expanded
9833 later, there is a way to turn off the entire subtree of blocks that
9834 are contained in it. */
9835 keep_next_level ();
9836 ret = c_begin_compound_stmt (true);
9838 c_bindings_start_stmt_expr (c_switch_stack == NULL
9839 ? NULL
9840 : c_switch_stack->bindings);
9842 /* Mark the current statement list as belonging to a statement list. */
9843 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9845 return ret;
9848 /* LOC is the location of the compound statement to which this body
9849 belongs. */
9851 tree
9852 c_finish_stmt_expr (location_t loc, tree body)
9854 tree last, type, tmp, val;
9855 tree *last_p;
9857 body = c_end_compound_stmt (loc, body, true);
9859 c_bindings_end_stmt_expr (c_switch_stack == NULL
9860 ? NULL
9861 : c_switch_stack->bindings);
9863 /* Locate the last statement in BODY. See c_end_compound_stmt
9864 about always returning a BIND_EXPR. */
9865 last_p = &BIND_EXPR_BODY (body);
9866 last = BIND_EXPR_BODY (body);
9868 continue_searching:
9869 if (TREE_CODE (last) == STATEMENT_LIST)
9871 tree_stmt_iterator i;
9873 /* This can happen with degenerate cases like ({ }). No value. */
9874 if (!TREE_SIDE_EFFECTS (last))
9875 return body;
9877 /* If we're supposed to generate side effects warnings, process
9878 all of the statements except the last. */
9879 if (warn_unused_value)
9881 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9883 location_t tloc;
9884 tree t = tsi_stmt (i);
9886 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9887 emit_side_effect_warnings (tloc, t);
9890 else
9891 i = tsi_last (last);
9892 last_p = tsi_stmt_ptr (i);
9893 last = *last_p;
9896 /* If the end of the list is exception related, then the list was split
9897 by a call to push_cleanup. Continue searching. */
9898 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9899 || TREE_CODE (last) == TRY_CATCH_EXPR)
9901 last_p = &TREE_OPERAND (last, 0);
9902 last = *last_p;
9903 goto continue_searching;
9906 if (last == error_mark_node)
9907 return last;
9909 /* In the case that the BIND_EXPR is not necessary, return the
9910 expression out from inside it. */
9911 if (last == BIND_EXPR_BODY (body)
9912 && BIND_EXPR_VARS (body) == NULL)
9914 /* Even if this looks constant, do not allow it in a constant
9915 expression. */
9916 last = c_wrap_maybe_const (last, true);
9917 /* Do not warn if the return value of a statement expression is
9918 unused. */
9919 TREE_NO_WARNING (last) = 1;
9920 return last;
9923 /* Extract the type of said expression. */
9924 type = TREE_TYPE (last);
9926 /* If we're not returning a value at all, then the BIND_EXPR that
9927 we already have is a fine expression to return. */
9928 if (!type || VOID_TYPE_P (type))
9929 return body;
9931 /* Now that we've located the expression containing the value, it seems
9932 silly to make voidify_wrapper_expr repeat the process. Create a
9933 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9934 tmp = create_tmp_var_raw (type, NULL);
9936 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9937 tree_expr_nonnegative_p giving up immediately. */
9938 val = last;
9939 if (TREE_CODE (val) == NOP_EXPR
9940 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9941 val = TREE_OPERAND (val, 0);
9943 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9944 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9947 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9948 SET_EXPR_LOCATION (t, loc);
9949 return t;
9953 /* Begin and end compound statements. This is as simple as pushing
9954 and popping new statement lists from the tree. */
9956 tree
9957 c_begin_compound_stmt (bool do_scope)
9959 tree stmt = push_stmt_list ();
9960 if (do_scope)
9961 push_scope ();
9962 return stmt;
9965 /* End a compound statement. STMT is the statement. LOC is the
9966 location of the compound statement-- this is usually the location
9967 of the opening brace. */
9969 tree
9970 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9972 tree block = NULL;
9974 if (do_scope)
9976 if (c_dialect_objc ())
9977 objc_clear_super_receiver ();
9978 block = pop_scope ();
9981 stmt = pop_stmt_list (stmt);
9982 stmt = c_build_bind_expr (loc, block, stmt);
9984 /* If this compound statement is nested immediately inside a statement
9985 expression, then force a BIND_EXPR to be created. Otherwise we'll
9986 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9987 STATEMENT_LISTs merge, and thus we can lose track of what statement
9988 was really last. */
9989 if (building_stmt_list_p ()
9990 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9991 && TREE_CODE (stmt) != BIND_EXPR)
9993 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9994 TREE_SIDE_EFFECTS (stmt) = 1;
9995 SET_EXPR_LOCATION (stmt, loc);
9998 return stmt;
10001 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10002 when the current scope is exited. EH_ONLY is true when this is not
10003 meant to apply to normal control flow transfer. */
10005 void
10006 push_cleanup (tree decl, tree cleanup, bool eh_only)
10008 enum tree_code code;
10009 tree stmt, list;
10010 bool stmt_expr;
10012 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10013 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10014 add_stmt (stmt);
10015 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10016 list = push_stmt_list ();
10017 TREE_OPERAND (stmt, 0) = list;
10018 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10021 /* Build a binary-operation expression without default conversions.
10022 CODE is the kind of expression to build.
10023 LOCATION is the operator's location.
10024 This function differs from `build' in several ways:
10025 the data type of the result is computed and recorded in it,
10026 warnings are generated if arg data types are invalid,
10027 special handling for addition and subtraction of pointers is known,
10028 and some optimization is done (operations on narrow ints
10029 are done in the narrower type when that gives the same result).
10030 Constant folding is also done before the result is returned.
10032 Note that the operands will never have enumeral types, or function
10033 or array types, because either they will have the default conversions
10034 performed or they have both just been converted to some other type in which
10035 the arithmetic is to be done. */
10037 tree
10038 build_binary_op (location_t location, enum tree_code code,
10039 tree orig_op0, tree orig_op1, int convert_p)
10041 tree type0, type1, orig_type0, orig_type1;
10042 tree eptype;
10043 enum tree_code code0, code1;
10044 tree op0, op1;
10045 tree ret = error_mark_node;
10046 const char *invalid_op_diag;
10047 bool op0_int_operands, op1_int_operands;
10048 bool int_const, int_const_or_overflow, int_operands;
10050 /* Expression code to give to the expression when it is built.
10051 Normally this is CODE, which is what the caller asked for,
10052 but in some special cases we change it. */
10053 enum tree_code resultcode = code;
10055 /* Data type in which the computation is to be performed.
10056 In the simplest cases this is the common type of the arguments. */
10057 tree result_type = NULL;
10059 /* When the computation is in excess precision, the type of the
10060 final EXCESS_PRECISION_EXPR. */
10061 tree semantic_result_type = NULL;
10063 /* Nonzero means operands have already been type-converted
10064 in whatever way is necessary.
10065 Zero means they need to be converted to RESULT_TYPE. */
10066 int converted = 0;
10068 /* Nonzero means create the expression with this type, rather than
10069 RESULT_TYPE. */
10070 tree build_type = 0;
10072 /* Nonzero means after finally constructing the expression
10073 convert it to this type. */
10074 tree final_type = 0;
10076 /* Nonzero if this is an operation like MIN or MAX which can
10077 safely be computed in short if both args are promoted shorts.
10078 Also implies COMMON.
10079 -1 indicates a bitwise operation; this makes a difference
10080 in the exact conditions for when it is safe to do the operation
10081 in a narrower mode. */
10082 int shorten = 0;
10084 /* Nonzero if this is a comparison operation;
10085 if both args are promoted shorts, compare the original shorts.
10086 Also implies COMMON. */
10087 int short_compare = 0;
10089 /* Nonzero if this is a right-shift operation, which can be computed on the
10090 original short and then promoted if the operand is a promoted short. */
10091 int short_shift = 0;
10093 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10094 int common = 0;
10096 /* True means types are compatible as far as ObjC is concerned. */
10097 bool objc_ok;
10099 /* True means this is an arithmetic operation that may need excess
10100 precision. */
10101 bool may_need_excess_precision;
10103 /* True means this is a boolean operation that converts both its
10104 operands to truth-values. */
10105 bool boolean_op = false;
10107 /* Remember whether we're doing / or %. */
10108 bool doing_div_or_mod = false;
10110 /* Remember whether we're doing << or >>. */
10111 bool doing_shift = false;
10113 /* Tree holding instrumentation expression. */
10114 tree instrument_expr = NULL;
10116 if (location == UNKNOWN_LOCATION)
10117 location = input_location;
10119 op0 = orig_op0;
10120 op1 = orig_op1;
10122 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10123 if (op0_int_operands)
10124 op0 = remove_c_maybe_const_expr (op0);
10125 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10126 if (op1_int_operands)
10127 op1 = remove_c_maybe_const_expr (op1);
10128 int_operands = (op0_int_operands && op1_int_operands);
10129 if (int_operands)
10131 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10132 && TREE_CODE (orig_op1) == INTEGER_CST);
10133 int_const = (int_const_or_overflow
10134 && !TREE_OVERFLOW (orig_op0)
10135 && !TREE_OVERFLOW (orig_op1));
10137 else
10138 int_const = int_const_or_overflow = false;
10140 /* Do not apply default conversion in mixed vector/scalar expression. */
10141 if (convert_p
10142 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10143 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10145 op0 = default_conversion (op0);
10146 op1 = default_conversion (op1);
10149 /* When Cilk Plus is enabled and there are array notations inside op0, then
10150 we check to see if there are builtin array notation functions. If
10151 so, then we take on the type of the array notation inside it. */
10152 if (flag_cilkplus && contains_array_notation_expr (op0))
10153 orig_type0 = type0 = find_correct_array_notation_type (op0);
10154 else
10155 orig_type0 = type0 = TREE_TYPE (op0);
10157 if (flag_cilkplus && contains_array_notation_expr (op1))
10158 orig_type1 = type1 = find_correct_array_notation_type (op1);
10159 else
10160 orig_type1 = type1 = TREE_TYPE (op1);
10162 /* The expression codes of the data types of the arguments tell us
10163 whether the arguments are integers, floating, pointers, etc. */
10164 code0 = TREE_CODE (type0);
10165 code1 = TREE_CODE (type1);
10167 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10168 STRIP_TYPE_NOPS (op0);
10169 STRIP_TYPE_NOPS (op1);
10171 /* If an error was already reported for one of the arguments,
10172 avoid reporting another error. */
10174 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10175 return error_mark_node;
10177 if ((invalid_op_diag
10178 = targetm.invalid_binary_op (code, type0, type1)))
10180 error_at (location, invalid_op_diag);
10181 return error_mark_node;
10184 switch (code)
10186 case PLUS_EXPR:
10187 case MINUS_EXPR:
10188 case MULT_EXPR:
10189 case TRUNC_DIV_EXPR:
10190 case CEIL_DIV_EXPR:
10191 case FLOOR_DIV_EXPR:
10192 case ROUND_DIV_EXPR:
10193 case EXACT_DIV_EXPR:
10194 may_need_excess_precision = true;
10195 break;
10196 default:
10197 may_need_excess_precision = false;
10198 break;
10200 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10202 op0 = TREE_OPERAND (op0, 0);
10203 type0 = TREE_TYPE (op0);
10205 else if (may_need_excess_precision
10206 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10208 type0 = eptype;
10209 op0 = convert (eptype, op0);
10211 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10213 op1 = TREE_OPERAND (op1, 0);
10214 type1 = TREE_TYPE (op1);
10216 else if (may_need_excess_precision
10217 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10219 type1 = eptype;
10220 op1 = convert (eptype, op1);
10223 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10225 /* In case when one of the operands of the binary operation is
10226 a vector and another is a scalar -- convert scalar to vector. */
10227 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10229 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10230 true);
10232 switch (convert_flag)
10234 case stv_error:
10235 return error_mark_node;
10236 case stv_firstarg:
10238 bool maybe_const = true;
10239 tree sc;
10240 sc = c_fully_fold (op0, false, &maybe_const);
10241 sc = save_expr (sc);
10242 sc = convert (TREE_TYPE (type1), sc);
10243 op0 = build_vector_from_val (type1, sc);
10244 if (!maybe_const)
10245 op0 = c_wrap_maybe_const (op0, true);
10246 orig_type0 = type0 = TREE_TYPE (op0);
10247 code0 = TREE_CODE (type0);
10248 converted = 1;
10249 break;
10251 case stv_secondarg:
10253 bool maybe_const = true;
10254 tree sc;
10255 sc = c_fully_fold (op1, false, &maybe_const);
10256 sc = save_expr (sc);
10257 sc = convert (TREE_TYPE (type0), sc);
10258 op1 = build_vector_from_val (type0, sc);
10259 if (!maybe_const)
10260 op1 = c_wrap_maybe_const (op1, true);
10261 orig_type1 = type1 = TREE_TYPE (op1);
10262 code1 = TREE_CODE (type1);
10263 converted = 1;
10264 break;
10266 default:
10267 break;
10271 switch (code)
10273 case PLUS_EXPR:
10274 /* Handle the pointer + int case. */
10275 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10277 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10278 goto return_build_binary_op;
10280 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10282 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10283 goto return_build_binary_op;
10285 else
10286 common = 1;
10287 break;
10289 case MINUS_EXPR:
10290 /* Subtraction of two similar pointers.
10291 We must subtract them as integers, then divide by object size. */
10292 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10293 && comp_target_types (location, type0, type1))
10295 ret = pointer_diff (location, op0, op1);
10296 goto return_build_binary_op;
10298 /* Handle pointer minus int. Just like pointer plus int. */
10299 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10301 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10302 goto return_build_binary_op;
10304 else
10305 common = 1;
10306 break;
10308 case MULT_EXPR:
10309 common = 1;
10310 break;
10312 case TRUNC_DIV_EXPR:
10313 case CEIL_DIV_EXPR:
10314 case FLOOR_DIV_EXPR:
10315 case ROUND_DIV_EXPR:
10316 case EXACT_DIV_EXPR:
10317 doing_div_or_mod = true;
10318 warn_for_div_by_zero (location, op1);
10320 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10321 || code0 == FIXED_POINT_TYPE
10322 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10323 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10324 || code1 == FIXED_POINT_TYPE
10325 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10327 enum tree_code tcode0 = code0, tcode1 = code1;
10329 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10330 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10331 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10332 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10334 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10335 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10336 resultcode = RDIV_EXPR;
10337 else
10338 /* Although it would be tempting to shorten always here, that
10339 loses on some targets, since the modulo instruction is
10340 undefined if the quotient can't be represented in the
10341 computation mode. We shorten only if unsigned or if
10342 dividing by something we know != -1. */
10343 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10344 || (TREE_CODE (op1) == INTEGER_CST
10345 && !integer_all_onesp (op1)));
10346 common = 1;
10348 break;
10350 case BIT_AND_EXPR:
10351 case BIT_IOR_EXPR:
10352 case BIT_XOR_EXPR:
10353 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10354 shorten = -1;
10355 /* Allow vector types which are not floating point types. */
10356 else if (code0 == VECTOR_TYPE
10357 && code1 == VECTOR_TYPE
10358 && !VECTOR_FLOAT_TYPE_P (type0)
10359 && !VECTOR_FLOAT_TYPE_P (type1))
10360 common = 1;
10361 break;
10363 case TRUNC_MOD_EXPR:
10364 case FLOOR_MOD_EXPR:
10365 doing_div_or_mod = true;
10366 warn_for_div_by_zero (location, op1);
10368 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10369 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10370 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10371 common = 1;
10372 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10374 /* Although it would be tempting to shorten always here, that loses
10375 on some targets, since the modulo instruction is undefined if the
10376 quotient can't be represented in the computation mode. We shorten
10377 only if unsigned or if dividing by something we know != -1. */
10378 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10379 || (TREE_CODE (op1) == INTEGER_CST
10380 && !integer_all_onesp (op1)));
10381 common = 1;
10383 break;
10385 case TRUTH_ANDIF_EXPR:
10386 case TRUTH_ORIF_EXPR:
10387 case TRUTH_AND_EXPR:
10388 case TRUTH_OR_EXPR:
10389 case TRUTH_XOR_EXPR:
10390 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10391 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10392 || code0 == FIXED_POINT_TYPE)
10393 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10394 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10395 || code1 == FIXED_POINT_TYPE))
10397 /* Result of these operations is always an int,
10398 but that does not mean the operands should be
10399 converted to ints! */
10400 result_type = integer_type_node;
10401 if (op0_int_operands)
10403 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10404 op0 = remove_c_maybe_const_expr (op0);
10406 else
10407 op0 = c_objc_common_truthvalue_conversion (location, op0);
10408 if (op1_int_operands)
10410 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10411 op1 = remove_c_maybe_const_expr (op1);
10413 else
10414 op1 = c_objc_common_truthvalue_conversion (location, op1);
10415 converted = 1;
10416 boolean_op = true;
10418 if (code == TRUTH_ANDIF_EXPR)
10420 int_const_or_overflow = (int_operands
10421 && TREE_CODE (orig_op0) == INTEGER_CST
10422 && (op0 == truthvalue_false_node
10423 || TREE_CODE (orig_op1) == INTEGER_CST));
10424 int_const = (int_const_or_overflow
10425 && !TREE_OVERFLOW (orig_op0)
10426 && (op0 == truthvalue_false_node
10427 || !TREE_OVERFLOW (orig_op1)));
10429 else if (code == TRUTH_ORIF_EXPR)
10431 int_const_or_overflow = (int_operands
10432 && TREE_CODE (orig_op0) == INTEGER_CST
10433 && (op0 == truthvalue_true_node
10434 || TREE_CODE (orig_op1) == INTEGER_CST));
10435 int_const = (int_const_or_overflow
10436 && !TREE_OVERFLOW (orig_op0)
10437 && (op0 == truthvalue_true_node
10438 || !TREE_OVERFLOW (orig_op1)));
10440 break;
10442 /* Shift operations: result has same type as first operand;
10443 always convert second operand to int.
10444 Also set SHORT_SHIFT if shifting rightward. */
10446 case RSHIFT_EXPR:
10447 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10448 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10450 result_type = type0;
10451 converted = 1;
10453 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10454 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10455 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10456 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10458 result_type = type0;
10459 converted = 1;
10461 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10462 && code1 == INTEGER_TYPE)
10464 doing_shift = true;
10465 if (TREE_CODE (op1) == INTEGER_CST)
10467 if (tree_int_cst_sgn (op1) < 0)
10469 int_const = false;
10470 if (c_inhibit_evaluation_warnings == 0)
10471 warning_at (location, 0, "right shift count is negative");
10473 else
10475 if (!integer_zerop (op1))
10476 short_shift = 1;
10478 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10480 int_const = false;
10481 if (c_inhibit_evaluation_warnings == 0)
10482 warning_at (location, 0, "right shift count >= width "
10483 "of type");
10488 /* Use the type of the value to be shifted. */
10489 result_type = type0;
10490 /* Convert the non vector shift-count to an integer, regardless
10491 of size of value being shifted. */
10492 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10493 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10494 op1 = convert (integer_type_node, op1);
10495 /* Avoid converting op1 to result_type later. */
10496 converted = 1;
10498 break;
10500 case LSHIFT_EXPR:
10501 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10502 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10504 result_type = type0;
10505 converted = 1;
10507 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10508 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10509 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10510 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10512 result_type = type0;
10513 converted = 1;
10515 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10516 && code1 == INTEGER_TYPE)
10518 doing_shift = true;
10519 if (TREE_CODE (op1) == INTEGER_CST)
10521 if (tree_int_cst_sgn (op1) < 0)
10523 int_const = false;
10524 if (c_inhibit_evaluation_warnings == 0)
10525 warning_at (location, 0, "left shift count is negative");
10528 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10530 int_const = false;
10531 if (c_inhibit_evaluation_warnings == 0)
10532 warning_at (location, 0, "left shift count >= width of "
10533 "type");
10537 /* Use the type of the value to be shifted. */
10538 result_type = type0;
10539 /* Convert the non vector shift-count to an integer, regardless
10540 of size of value being shifted. */
10541 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10542 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10543 op1 = convert (integer_type_node, op1);
10544 /* Avoid converting op1 to result_type later. */
10545 converted = 1;
10547 break;
10549 case EQ_EXPR:
10550 case NE_EXPR:
10551 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10553 tree intt;
10554 if (!vector_types_compatible_elements_p (type0, type1))
10556 error_at (location, "comparing vectors with different "
10557 "element types");
10558 return error_mark_node;
10561 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10563 error_at (location, "comparing vectors with different "
10564 "number of elements");
10565 return error_mark_node;
10568 /* Always construct signed integer vector type. */
10569 intt = c_common_type_for_size (GET_MODE_BITSIZE
10570 (TYPE_MODE (TREE_TYPE (type0))), 0);
10571 result_type = build_opaque_vector_type (intt,
10572 TYPE_VECTOR_SUBPARTS (type0));
10573 converted = 1;
10574 break;
10576 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10577 warning_at (location,
10578 OPT_Wfloat_equal,
10579 "comparing floating point with == or != is unsafe");
10580 /* Result of comparison is always int,
10581 but don't convert the args to int! */
10582 build_type = integer_type_node;
10583 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10584 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10585 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10586 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10587 short_compare = 1;
10588 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10590 if (TREE_CODE (op0) == ADDR_EXPR
10591 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10593 if (code == EQ_EXPR)
10594 warning_at (location,
10595 OPT_Waddress,
10596 "the comparison will always evaluate as %<false%> "
10597 "for the address of %qD will never be NULL",
10598 TREE_OPERAND (op0, 0));
10599 else
10600 warning_at (location,
10601 OPT_Waddress,
10602 "the comparison will always evaluate as %<true%> "
10603 "for the address of %qD will never be NULL",
10604 TREE_OPERAND (op0, 0));
10606 result_type = type0;
10608 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10610 if (TREE_CODE (op1) == ADDR_EXPR
10611 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10613 if (code == EQ_EXPR)
10614 warning_at (location,
10615 OPT_Waddress,
10616 "the comparison will always evaluate as %<false%> "
10617 "for the address of %qD will never be NULL",
10618 TREE_OPERAND (op1, 0));
10619 else
10620 warning_at (location,
10621 OPT_Waddress,
10622 "the comparison will always evaluate as %<true%> "
10623 "for the address of %qD will never be NULL",
10624 TREE_OPERAND (op1, 0));
10626 result_type = type1;
10628 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10630 tree tt0 = TREE_TYPE (type0);
10631 tree tt1 = TREE_TYPE (type1);
10632 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10633 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10634 addr_space_t as_common = ADDR_SPACE_GENERIC;
10636 /* Anything compares with void *. void * compares with anything.
10637 Otherwise, the targets must be compatible
10638 and both must be object or both incomplete. */
10639 if (comp_target_types (location, type0, type1))
10640 result_type = common_pointer_type (type0, type1);
10641 else if (!addr_space_superset (as0, as1, &as_common))
10643 error_at (location, "comparison of pointers to "
10644 "disjoint address spaces");
10645 return error_mark_node;
10647 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10649 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10650 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10651 "comparison of %<void *%> with function pointer");
10653 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10655 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10656 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10657 "comparison of %<void *%> with function pointer");
10659 else
10660 /* Avoid warning about the volatile ObjC EH puts on decls. */
10661 if (!objc_ok)
10662 pedwarn (location, 0,
10663 "comparison of distinct pointer types lacks a cast");
10665 if (result_type == NULL_TREE)
10667 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10668 result_type = build_pointer_type
10669 (build_qualified_type (void_type_node, qual));
10672 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10674 result_type = type0;
10675 pedwarn (location, 0, "comparison between pointer and integer");
10677 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10679 result_type = type1;
10680 pedwarn (location, 0, "comparison between pointer and integer");
10682 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10683 || truth_value_p (TREE_CODE (orig_op0)))
10684 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10685 || truth_value_p (TREE_CODE (orig_op1))))
10686 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10687 break;
10689 case LE_EXPR:
10690 case GE_EXPR:
10691 case LT_EXPR:
10692 case GT_EXPR:
10693 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10695 tree intt;
10696 if (!vector_types_compatible_elements_p (type0, type1))
10698 error_at (location, "comparing vectors with different "
10699 "element types");
10700 return error_mark_node;
10703 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10705 error_at (location, "comparing vectors with different "
10706 "number of elements");
10707 return error_mark_node;
10710 /* Always construct signed integer vector type. */
10711 intt = c_common_type_for_size (GET_MODE_BITSIZE
10712 (TYPE_MODE (TREE_TYPE (type0))), 0);
10713 result_type = build_opaque_vector_type (intt,
10714 TYPE_VECTOR_SUBPARTS (type0));
10715 converted = 1;
10716 break;
10718 build_type = integer_type_node;
10719 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10720 || code0 == FIXED_POINT_TYPE)
10721 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10722 || code1 == FIXED_POINT_TYPE))
10723 short_compare = 1;
10724 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10726 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10727 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10728 addr_space_t as_common;
10730 if (comp_target_types (location, type0, type1))
10732 result_type = common_pointer_type (type0, type1);
10733 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10734 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10735 pedwarn (location, 0,
10736 "comparison of complete and incomplete pointers");
10737 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10738 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10739 "ordered comparisons of pointers to functions");
10740 else if (null_pointer_constant_p (orig_op0)
10741 || null_pointer_constant_p (orig_op1))
10742 warning_at (location, OPT_Wextra,
10743 "ordered comparison of pointer with null pointer");
10746 else if (!addr_space_superset (as0, as1, &as_common))
10748 error_at (location, "comparison of pointers to "
10749 "disjoint address spaces");
10750 return error_mark_node;
10752 else
10754 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10755 result_type = build_pointer_type
10756 (build_qualified_type (void_type_node, qual));
10757 pedwarn (location, 0,
10758 "comparison of distinct pointer types lacks a cast");
10761 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10763 result_type = type0;
10764 if (pedantic)
10765 pedwarn (location, OPT_Wpedantic,
10766 "ordered comparison of pointer with integer zero");
10767 else if (extra_warnings)
10768 warning_at (location, OPT_Wextra,
10769 "ordered comparison of pointer with integer zero");
10771 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10773 result_type = type1;
10774 if (pedantic)
10775 pedwarn (location, OPT_Wpedantic,
10776 "ordered comparison of pointer with integer zero");
10777 else if (extra_warnings)
10778 warning_at (location, OPT_Wextra,
10779 "ordered comparison of pointer with integer zero");
10781 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10783 result_type = type0;
10784 pedwarn (location, 0, "comparison between pointer and integer");
10786 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10788 result_type = type1;
10789 pedwarn (location, 0, "comparison between pointer and integer");
10791 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10792 || truth_value_p (TREE_CODE (orig_op0)))
10793 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10794 || truth_value_p (TREE_CODE (orig_op1))))
10795 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10796 break;
10798 default:
10799 gcc_unreachable ();
10802 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10803 return error_mark_node;
10805 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10806 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10807 || !vector_types_compatible_elements_p (type0, type1)))
10809 binary_op_error (location, code, type0, type1);
10810 return error_mark_node;
10813 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10814 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10816 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10817 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10819 bool first_complex = (code0 == COMPLEX_TYPE);
10820 bool second_complex = (code1 == COMPLEX_TYPE);
10821 int none_complex = (!first_complex && !second_complex);
10823 if (shorten || common || short_compare)
10825 result_type = c_common_type (type0, type1);
10826 do_warn_double_promotion (result_type, type0, type1,
10827 "implicit conversion from %qT to %qT "
10828 "to match other operand of binary "
10829 "expression",
10830 location);
10831 if (result_type == error_mark_node)
10832 return error_mark_node;
10835 if (first_complex != second_complex
10836 && (code == PLUS_EXPR
10837 || code == MINUS_EXPR
10838 || code == MULT_EXPR
10839 || (code == TRUNC_DIV_EXPR && first_complex))
10840 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10841 && flag_signed_zeros)
10843 /* An operation on mixed real/complex operands must be
10844 handled specially, but the language-independent code can
10845 more easily optimize the plain complex arithmetic if
10846 -fno-signed-zeros. */
10847 tree real_type = TREE_TYPE (result_type);
10848 tree real, imag;
10849 if (type0 != orig_type0 || type1 != orig_type1)
10851 gcc_assert (may_need_excess_precision && common);
10852 semantic_result_type = c_common_type (orig_type0, orig_type1);
10854 if (first_complex)
10856 if (TREE_TYPE (op0) != result_type)
10857 op0 = convert_and_check (location, result_type, op0);
10858 if (TREE_TYPE (op1) != real_type)
10859 op1 = convert_and_check (location, real_type, op1);
10861 else
10863 if (TREE_TYPE (op0) != real_type)
10864 op0 = convert_and_check (location, real_type, op0);
10865 if (TREE_TYPE (op1) != result_type)
10866 op1 = convert_and_check (location, result_type, op1);
10868 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10869 return error_mark_node;
10870 if (first_complex)
10872 op0 = c_save_expr (op0);
10873 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10874 op0, 1);
10875 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10876 op0, 1);
10877 switch (code)
10879 case MULT_EXPR:
10880 case TRUNC_DIV_EXPR:
10881 op1 = c_save_expr (op1);
10882 imag = build2 (resultcode, real_type, imag, op1);
10883 /* Fall through. */
10884 case PLUS_EXPR:
10885 case MINUS_EXPR:
10886 real = build2 (resultcode, real_type, real, op1);
10887 break;
10888 default:
10889 gcc_unreachable();
10892 else
10894 op1 = c_save_expr (op1);
10895 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10896 op1, 1);
10897 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10898 op1, 1);
10899 switch (code)
10901 case MULT_EXPR:
10902 op0 = c_save_expr (op0);
10903 imag = build2 (resultcode, real_type, op0, imag);
10904 /* Fall through. */
10905 case PLUS_EXPR:
10906 real = build2 (resultcode, real_type, op0, real);
10907 break;
10908 case MINUS_EXPR:
10909 real = build2 (resultcode, real_type, op0, real);
10910 imag = build1 (NEGATE_EXPR, real_type, imag);
10911 break;
10912 default:
10913 gcc_unreachable();
10916 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10917 goto return_build_binary_op;
10920 /* For certain operations (which identify themselves by shorten != 0)
10921 if both args were extended from the same smaller type,
10922 do the arithmetic in that type and then extend.
10924 shorten !=0 and !=1 indicates a bitwise operation.
10925 For them, this optimization is safe only if
10926 both args are zero-extended or both are sign-extended.
10927 Otherwise, we might change the result.
10928 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10929 but calculated in (unsigned short) it would be (unsigned short)-1. */
10931 if (shorten && none_complex)
10933 final_type = result_type;
10934 result_type = shorten_binary_op (result_type, op0, op1,
10935 shorten == -1);
10938 /* Shifts can be shortened if shifting right. */
10940 if (short_shift)
10942 int unsigned_arg;
10943 tree arg0 = get_narrower (op0, &unsigned_arg);
10945 final_type = result_type;
10947 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10948 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10950 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10951 && tree_int_cst_sgn (op1) > 0
10952 /* We can shorten only if the shift count is less than the
10953 number of bits in the smaller type size. */
10954 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10955 /* We cannot drop an unsigned shift after sign-extension. */
10956 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10958 /* Do an unsigned shift if the operand was zero-extended. */
10959 result_type
10960 = c_common_signed_or_unsigned_type (unsigned_arg,
10961 TREE_TYPE (arg0));
10962 /* Convert value-to-be-shifted to that type. */
10963 if (TREE_TYPE (op0) != result_type)
10964 op0 = convert (result_type, op0);
10965 converted = 1;
10969 /* Comparison operations are shortened too but differently.
10970 They identify themselves by setting short_compare = 1. */
10972 if (short_compare)
10974 /* Don't write &op0, etc., because that would prevent op0
10975 from being kept in a register.
10976 Instead, make copies of the our local variables and
10977 pass the copies by reference, then copy them back afterward. */
10978 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10979 enum tree_code xresultcode = resultcode;
10980 tree val
10981 = shorten_compare (location, &xop0, &xop1, &xresult_type,
10982 &xresultcode);
10984 if (val != 0)
10986 ret = val;
10987 goto return_build_binary_op;
10990 op0 = xop0, op1 = xop1;
10991 converted = 1;
10992 resultcode = xresultcode;
10994 if (c_inhibit_evaluation_warnings == 0)
10996 bool op0_maybe_const = true;
10997 bool op1_maybe_const = true;
10998 tree orig_op0_folded, orig_op1_folded;
11000 if (in_late_binary_op)
11002 orig_op0_folded = orig_op0;
11003 orig_op1_folded = orig_op1;
11005 else
11007 /* Fold for the sake of possible warnings, as in
11008 build_conditional_expr. This requires the
11009 "original" values to be folded, not just op0 and
11010 op1. */
11011 c_inhibit_evaluation_warnings++;
11012 op0 = c_fully_fold (op0, require_constant_value,
11013 &op0_maybe_const);
11014 op1 = c_fully_fold (op1, require_constant_value,
11015 &op1_maybe_const);
11016 c_inhibit_evaluation_warnings--;
11017 orig_op0_folded = c_fully_fold (orig_op0,
11018 require_constant_value,
11019 NULL);
11020 orig_op1_folded = c_fully_fold (orig_op1,
11021 require_constant_value,
11022 NULL);
11025 if (warn_sign_compare)
11026 warn_for_sign_compare (location, orig_op0_folded,
11027 orig_op1_folded, op0, op1,
11028 result_type, resultcode);
11029 if (!in_late_binary_op && !int_operands)
11031 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11032 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11033 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11034 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11040 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11041 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11042 Then the expression will be built.
11043 It will be given type FINAL_TYPE if that is nonzero;
11044 otherwise, it will be given type RESULT_TYPE. */
11046 if (!result_type)
11048 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11049 return error_mark_node;
11052 if (build_type == NULL_TREE)
11054 build_type = result_type;
11055 if ((type0 != orig_type0 || type1 != orig_type1)
11056 && !boolean_op)
11058 gcc_assert (may_need_excess_precision && common);
11059 semantic_result_type = c_common_type (orig_type0, orig_type1);
11063 if (!converted)
11065 op0 = ep_convert_and_check (location, result_type, op0,
11066 semantic_result_type);
11067 op1 = ep_convert_and_check (location, result_type, op1,
11068 semantic_result_type);
11070 /* This can happen if one operand has a vector type, and the other
11071 has a different type. */
11072 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11073 return error_mark_node;
11076 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11077 | SANITIZE_FLOAT_DIVIDE))
11078 && current_function_decl != 0
11079 && !lookup_attribute ("no_sanitize_undefined",
11080 DECL_ATTRIBUTES (current_function_decl))
11081 && (doing_div_or_mod || doing_shift))
11083 /* OP0 and/or OP1 might have side-effects. */
11084 op0 = c_save_expr (op0);
11085 op1 = c_save_expr (op1);
11086 op0 = c_fully_fold (op0, false, NULL);
11087 op1 = c_fully_fold (op1, false, NULL);
11088 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11089 | SANITIZE_FLOAT_DIVIDE)))
11090 instrument_expr = ubsan_instrument_division (location, op0, op1);
11091 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11092 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11095 /* Treat expressions in initializers specially as they can't trap. */
11096 if (int_const_or_overflow)
11097 ret = (require_constant_value
11098 ? fold_build2_initializer_loc (location, resultcode, build_type,
11099 op0, op1)
11100 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11101 else
11102 ret = build2 (resultcode, build_type, op0, op1);
11103 if (final_type != 0)
11104 ret = convert (final_type, ret);
11106 return_build_binary_op:
11107 gcc_assert (ret != error_mark_node);
11108 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11109 ret = (int_operands
11110 ? note_integer_operands (ret)
11111 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11112 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11113 && !in_late_binary_op)
11114 ret = note_integer_operands (ret);
11115 if (semantic_result_type)
11116 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11117 protected_set_expr_location (ret, location);
11119 if (instrument_expr != NULL)
11120 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11121 instrument_expr, ret);
11123 return ret;
11127 /* Convert EXPR to be a truth-value, validating its type for this
11128 purpose. LOCATION is the source location for the expression. */
11130 tree
11131 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11133 bool int_const, int_operands;
11135 switch (TREE_CODE (TREE_TYPE (expr)))
11137 case ARRAY_TYPE:
11138 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11139 return error_mark_node;
11141 case RECORD_TYPE:
11142 error_at (location, "used struct type value where scalar is required");
11143 return error_mark_node;
11145 case UNION_TYPE:
11146 error_at (location, "used union type value where scalar is required");
11147 return error_mark_node;
11149 case VOID_TYPE:
11150 error_at (location, "void value not ignored as it ought to be");
11151 return error_mark_node;
11153 case FUNCTION_TYPE:
11154 gcc_unreachable ();
11156 case VECTOR_TYPE:
11157 error_at (location, "used vector type where scalar is required");
11158 return error_mark_node;
11160 default:
11161 break;
11164 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11165 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11166 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11168 expr = remove_c_maybe_const_expr (expr);
11169 expr = build2 (NE_EXPR, integer_type_node, expr,
11170 convert (TREE_TYPE (expr), integer_zero_node));
11171 expr = note_integer_operands (expr);
11173 else
11174 /* ??? Should we also give an error for vectors rather than leaving
11175 those to give errors later? */
11176 expr = c_common_truthvalue_conversion (location, expr);
11178 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11180 if (TREE_OVERFLOW (expr))
11181 return expr;
11182 else
11183 return note_integer_operands (expr);
11185 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11186 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11187 return expr;
11191 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11192 required. */
11194 tree
11195 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11197 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11199 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11200 /* Executing a compound literal inside a function reinitializes
11201 it. */
11202 if (!TREE_STATIC (decl))
11203 *se = true;
11204 return decl;
11206 else
11207 return expr;
11210 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11212 tree
11213 c_begin_omp_parallel (void)
11215 tree block;
11217 keep_next_level ();
11218 block = c_begin_compound_stmt (true);
11220 return block;
11223 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11224 statement. LOC is the location of the OMP_PARALLEL. */
11226 tree
11227 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11229 tree stmt;
11231 block = c_end_compound_stmt (loc, block, true);
11233 stmt = make_node (OMP_PARALLEL);
11234 TREE_TYPE (stmt) = void_type_node;
11235 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11236 OMP_PARALLEL_BODY (stmt) = block;
11237 SET_EXPR_LOCATION (stmt, loc);
11239 return add_stmt (stmt);
11242 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11244 tree
11245 c_begin_omp_task (void)
11247 tree block;
11249 keep_next_level ();
11250 block = c_begin_compound_stmt (true);
11252 return block;
11255 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11256 statement. LOC is the location of the #pragma. */
11258 tree
11259 c_finish_omp_task (location_t loc, tree clauses, tree block)
11261 tree stmt;
11263 block = c_end_compound_stmt (loc, block, true);
11265 stmt = make_node (OMP_TASK);
11266 TREE_TYPE (stmt) = void_type_node;
11267 OMP_TASK_CLAUSES (stmt) = clauses;
11268 OMP_TASK_BODY (stmt) = block;
11269 SET_EXPR_LOCATION (stmt, loc);
11271 return add_stmt (stmt);
11274 /* Generate GOMP_cancel call for #pragma omp cancel. */
11276 void
11277 c_finish_omp_cancel (location_t loc, tree clauses)
11279 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11280 int mask = 0;
11281 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11282 mask = 1;
11283 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11284 mask = 2;
11285 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11286 mask = 4;
11287 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11288 mask = 8;
11289 else
11291 error_at (loc, "%<#pragma omp cancel must specify one of "
11292 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11293 "clauses");
11294 return;
11296 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11297 if (ifc != NULL_TREE)
11299 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11300 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11301 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11302 build_zero_cst (type));
11304 else
11305 ifc = boolean_true_node;
11306 tree stmt = build_call_expr_loc (loc, fn, 2,
11307 build_int_cst (integer_type_node, mask),
11308 ifc);
11309 add_stmt (stmt);
11312 /* Generate GOMP_cancellation_point call for
11313 #pragma omp cancellation point. */
11315 void
11316 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11318 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11319 int mask = 0;
11320 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11321 mask = 1;
11322 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11323 mask = 2;
11324 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11325 mask = 4;
11326 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11327 mask = 8;
11328 else
11330 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11331 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11332 "clauses");
11333 return;
11335 tree stmt = build_call_expr_loc (loc, fn, 1,
11336 build_int_cst (integer_type_node, mask));
11337 add_stmt (stmt);
11340 /* Helper function for handle_omp_array_sections. Called recursively
11341 to handle multiple array-section-subscripts. C is the clause,
11342 T current expression (initially OMP_CLAUSE_DECL), which is either
11343 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11344 expression if specified, TREE_VALUE length expression if specified,
11345 TREE_CHAIN is what it has been specified after, or some decl.
11346 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11347 set to true if any of the array-section-subscript could have length
11348 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11349 first array-section-subscript which is known not to have length
11350 of one. Given say:
11351 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11352 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11353 all are or may have length of 1, array-section-subscript [:2] is the
11354 first one knonwn not to have length 1. For array-section-subscript
11355 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11356 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11357 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11358 case though, as some lengths could be zero. */
11360 static tree
11361 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11362 bool &maybe_zero_len, unsigned int &first_non_one)
11364 tree ret, low_bound, length, type;
11365 if (TREE_CODE (t) != TREE_LIST)
11367 if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
11368 return error_mark_node;
11369 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11371 if (DECL_P (t))
11372 error_at (OMP_CLAUSE_LOCATION (c),
11373 "%qD is not a variable in %qs clause", t,
11374 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11375 else
11376 error_at (OMP_CLAUSE_LOCATION (c),
11377 "%qE is not a variable in %qs clause", t,
11378 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11379 return error_mark_node;
11381 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11382 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11384 error_at (OMP_CLAUSE_LOCATION (c),
11385 "%qD is threadprivate variable in %qs clause", t,
11386 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11387 return error_mark_node;
11389 return t;
11392 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11393 maybe_zero_len, first_non_one);
11394 if (ret == error_mark_node || ret == NULL_TREE)
11395 return ret;
11397 type = TREE_TYPE (ret);
11398 low_bound = TREE_PURPOSE (t);
11399 length = TREE_VALUE (t);
11401 if (low_bound == error_mark_node || length == error_mark_node)
11402 return error_mark_node;
11404 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11406 error_at (OMP_CLAUSE_LOCATION (c),
11407 "low bound %qE of array section does not have integral type",
11408 low_bound);
11409 return error_mark_node;
11411 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11413 error_at (OMP_CLAUSE_LOCATION (c),
11414 "length %qE of array section does not have integral type",
11415 length);
11416 return error_mark_node;
11418 if (low_bound
11419 && TREE_CODE (low_bound) == INTEGER_CST
11420 && TYPE_PRECISION (TREE_TYPE (low_bound))
11421 > TYPE_PRECISION (sizetype))
11422 low_bound = fold_convert (sizetype, low_bound);
11423 if (length
11424 && TREE_CODE (length) == INTEGER_CST
11425 && TYPE_PRECISION (TREE_TYPE (length))
11426 > TYPE_PRECISION (sizetype))
11427 length = fold_convert (sizetype, length);
11428 if (low_bound == NULL_TREE)
11429 low_bound = integer_zero_node;
11431 if (length != NULL_TREE)
11433 if (!integer_nonzerop (length))
11434 maybe_zero_len = true;
11435 if (first_non_one == types.length ()
11436 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11437 first_non_one++;
11439 if (TREE_CODE (type) == ARRAY_TYPE)
11441 if (length == NULL_TREE
11442 && (TYPE_DOMAIN (type) == NULL_TREE
11443 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11445 error_at (OMP_CLAUSE_LOCATION (c),
11446 "for unknown bound array type length expression must "
11447 "be specified");
11448 return error_mark_node;
11450 if (TREE_CODE (low_bound) == INTEGER_CST
11451 && tree_int_cst_sgn (low_bound) == -1)
11453 error_at (OMP_CLAUSE_LOCATION (c),
11454 "negative low bound in array section in %qs clause",
11455 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11456 return error_mark_node;
11458 if (length != NULL_TREE
11459 && TREE_CODE (length) == INTEGER_CST
11460 && tree_int_cst_sgn (length) == -1)
11462 error_at (OMP_CLAUSE_LOCATION (c),
11463 "negative length in array section in %qs clause",
11464 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11465 return error_mark_node;
11467 if (TYPE_DOMAIN (type)
11468 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11469 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11470 == INTEGER_CST)
11472 tree size = size_binop (PLUS_EXPR,
11473 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11474 size_one_node);
11475 if (TREE_CODE (low_bound) == INTEGER_CST)
11477 if (tree_int_cst_lt (size, low_bound))
11479 error_at (OMP_CLAUSE_LOCATION (c),
11480 "low bound %qE above array section size "
11481 "in %qs clause", low_bound,
11482 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11483 return error_mark_node;
11485 if (tree_int_cst_equal (size, low_bound))
11486 maybe_zero_len = true;
11487 else if (length == NULL_TREE
11488 && first_non_one == types.length ()
11489 && tree_int_cst_equal
11490 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11491 low_bound))
11492 first_non_one++;
11494 else if (length == NULL_TREE)
11496 maybe_zero_len = true;
11497 if (first_non_one == types.length ())
11498 first_non_one++;
11500 if (length && TREE_CODE (length) == INTEGER_CST)
11502 if (tree_int_cst_lt (size, length))
11504 error_at (OMP_CLAUSE_LOCATION (c),
11505 "length %qE above array section size "
11506 "in %qs clause", length,
11507 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11508 return error_mark_node;
11510 if (TREE_CODE (low_bound) == INTEGER_CST)
11512 tree lbpluslen
11513 = size_binop (PLUS_EXPR,
11514 fold_convert (sizetype, low_bound),
11515 fold_convert (sizetype, length));
11516 if (TREE_CODE (lbpluslen) == INTEGER_CST
11517 && tree_int_cst_lt (size, lbpluslen))
11519 error_at (OMP_CLAUSE_LOCATION (c),
11520 "high bound %qE above array section size "
11521 "in %qs clause", lbpluslen,
11522 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11523 return error_mark_node;
11528 else if (length == NULL_TREE)
11530 maybe_zero_len = true;
11531 if (first_non_one == types.length ())
11532 first_non_one++;
11535 /* For [lb:] we will need to evaluate lb more than once. */
11536 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11538 tree lb = c_save_expr (low_bound);
11539 if (lb != low_bound)
11541 TREE_PURPOSE (t) = lb;
11542 low_bound = lb;
11546 else if (TREE_CODE (type) == POINTER_TYPE)
11548 if (length == NULL_TREE)
11550 error_at (OMP_CLAUSE_LOCATION (c),
11551 "for pointer type length expression must be specified");
11552 return error_mark_node;
11554 /* If there is a pointer type anywhere but in the very first
11555 array-section-subscript, the array section can't be contiguous. */
11556 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11557 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11559 error_at (OMP_CLAUSE_LOCATION (c),
11560 "array section is not contiguous in %qs clause",
11561 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11562 return error_mark_node;
11565 else
11567 error_at (OMP_CLAUSE_LOCATION (c),
11568 "%qE does not have pointer or array type", ret);
11569 return error_mark_node;
11571 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11572 types.safe_push (TREE_TYPE (ret));
11573 /* We will need to evaluate lb more than once. */
11574 tree lb = c_save_expr (low_bound);
11575 if (lb != low_bound)
11577 TREE_PURPOSE (t) = lb;
11578 low_bound = lb;
11580 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11581 return ret;
11584 /* Handle array sections for clause C. */
11586 static bool
11587 handle_omp_array_sections (tree c)
11589 bool maybe_zero_len = false;
11590 unsigned int first_non_one = 0;
11591 vec<tree> types = vNULL;
11592 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11593 maybe_zero_len, first_non_one);
11594 if (first == error_mark_node)
11596 types.release ();
11597 return true;
11599 if (first == NULL_TREE)
11601 types.release ();
11602 return false;
11604 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11606 tree t = OMP_CLAUSE_DECL (c);
11607 tree tem = NULL_TREE;
11608 types.release ();
11609 /* Need to evaluate side effects in the length expressions
11610 if any. */
11611 while (TREE_CODE (t) == TREE_LIST)
11613 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11615 if (tem == NULL_TREE)
11616 tem = TREE_VALUE (t);
11617 else
11618 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11619 TREE_VALUE (t), tem);
11621 t = TREE_CHAIN (t);
11623 if (tem)
11624 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11625 first = c_fully_fold (first, false, NULL);
11626 OMP_CLAUSE_DECL (c) = first;
11628 else
11630 unsigned int num = types.length (), i;
11631 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11632 tree condition = NULL_TREE;
11634 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11635 maybe_zero_len = true;
11637 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11638 t = TREE_CHAIN (t))
11640 tree low_bound = TREE_PURPOSE (t);
11641 tree length = TREE_VALUE (t);
11643 i--;
11644 if (low_bound
11645 && TREE_CODE (low_bound) == INTEGER_CST
11646 && TYPE_PRECISION (TREE_TYPE (low_bound))
11647 > TYPE_PRECISION (sizetype))
11648 low_bound = fold_convert (sizetype, low_bound);
11649 if (length
11650 && TREE_CODE (length) == INTEGER_CST
11651 && TYPE_PRECISION (TREE_TYPE (length))
11652 > TYPE_PRECISION (sizetype))
11653 length = fold_convert (sizetype, length);
11654 if (low_bound == NULL_TREE)
11655 low_bound = integer_zero_node;
11656 if (!maybe_zero_len && i > first_non_one)
11658 if (integer_nonzerop (low_bound))
11659 goto do_warn_noncontiguous;
11660 if (length != NULL_TREE
11661 && TREE_CODE (length) == INTEGER_CST
11662 && TYPE_DOMAIN (types[i])
11663 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11664 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11665 == INTEGER_CST)
11667 tree size;
11668 size = size_binop (PLUS_EXPR,
11669 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11670 size_one_node);
11671 if (!tree_int_cst_equal (length, size))
11673 do_warn_noncontiguous:
11674 error_at (OMP_CLAUSE_LOCATION (c),
11675 "array section is not contiguous in %qs "
11676 "clause",
11677 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11678 types.release ();
11679 return true;
11682 if (length != NULL_TREE
11683 && TREE_SIDE_EFFECTS (length))
11685 if (side_effects == NULL_TREE)
11686 side_effects = length;
11687 else
11688 side_effects = build2 (COMPOUND_EXPR,
11689 TREE_TYPE (side_effects),
11690 length, side_effects);
11693 else
11695 tree l;
11697 if (i > first_non_one && length && integer_nonzerop (length))
11698 continue;
11699 if (length)
11700 l = fold_convert (sizetype, length);
11701 else
11703 l = size_binop (PLUS_EXPR,
11704 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11705 size_one_node);
11706 l = size_binop (MINUS_EXPR, l,
11707 fold_convert (sizetype, low_bound));
11709 if (i > first_non_one)
11711 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11712 size_zero_node);
11713 if (condition == NULL_TREE)
11714 condition = l;
11715 else
11716 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11717 l, condition);
11719 else if (size == NULL_TREE)
11721 size = size_in_bytes (TREE_TYPE (types[i]));
11722 size = size_binop (MULT_EXPR, size, l);
11723 if (condition)
11724 size = fold_build3 (COND_EXPR, sizetype, condition,
11725 size, size_zero_node);
11727 else
11728 size = size_binop (MULT_EXPR, size, l);
11731 types.release ();
11732 if (side_effects)
11733 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11734 first = c_fully_fold (first, false, NULL);
11735 OMP_CLAUSE_DECL (c) = first;
11736 if (size)
11737 size = c_fully_fold (size, false, NULL);
11738 OMP_CLAUSE_SIZE (c) = size;
11739 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11740 return false;
11741 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11742 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11743 if (!c_mark_addressable (t))
11744 return false;
11745 OMP_CLAUSE_DECL (c2) = t;
11746 t = build_fold_addr_expr (first);
11747 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11748 tree ptr = OMP_CLAUSE_DECL (c2);
11749 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11750 ptr = build_fold_addr_expr (ptr);
11751 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11752 ptrdiff_type_node, t,
11753 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11754 ptrdiff_type_node, ptr));
11755 t = c_fully_fold (t, false, NULL);
11756 OMP_CLAUSE_SIZE (c2) = t;
11757 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11758 OMP_CLAUSE_CHAIN (c) = c2;
11760 return false;
11763 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11764 an inline call. But, remap
11765 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11766 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11768 static tree
11769 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11770 tree decl, tree placeholder)
11772 copy_body_data id;
11773 hash_map<tree, tree> decl_map;
11775 decl_map.put (omp_decl1, placeholder);
11776 decl_map.put (omp_decl2, decl);
11777 memset (&id, 0, sizeof (id));
11778 id.src_fn = DECL_CONTEXT (omp_decl1);
11779 id.dst_fn = current_function_decl;
11780 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11781 id.decl_map = &decl_map;
11783 id.copy_decl = copy_decl_no_change;
11784 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11785 id.transform_new_cfg = true;
11786 id.transform_return_to_modify = false;
11787 id.transform_lang_insert_block = NULL;
11788 id.eh_lp_nr = 0;
11789 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11790 return stmt;
11793 /* Helper function of c_finish_omp_clauses, called via walk_tree.
11794 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11796 static tree
11797 c_find_omp_placeholder_r (tree *tp, int *, void *data)
11799 if (*tp == (tree) data)
11800 return *tp;
11801 return NULL_TREE;
11804 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
11805 Remove any elements from the list that are invalid. */
11807 tree
11808 c_finish_omp_clauses (tree clauses)
11810 bitmap_head generic_head, firstprivate_head, lastprivate_head;
11811 bitmap_head aligned_head;
11812 tree c, t, *pc;
11813 bool branch_seen = false;
11814 bool copyprivate_seen = false;
11815 tree *nowait_clause = NULL;
11817 bitmap_obstack_initialize (NULL);
11818 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11819 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11820 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
11821 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
11823 for (pc = &clauses, c = clauses; c ; c = *pc)
11825 bool remove = false;
11826 bool need_complete = false;
11827 bool need_implicitly_determined = false;
11829 switch (OMP_CLAUSE_CODE (c))
11831 case OMP_CLAUSE_SHARED:
11832 need_implicitly_determined = true;
11833 goto check_dup_generic;
11835 case OMP_CLAUSE_PRIVATE:
11836 need_complete = true;
11837 need_implicitly_determined = true;
11838 goto check_dup_generic;
11840 case OMP_CLAUSE_REDUCTION:
11841 need_implicitly_determined = true;
11842 t = OMP_CLAUSE_DECL (c);
11843 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
11844 && (FLOAT_TYPE_P (TREE_TYPE (t))
11845 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
11847 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11848 const char *r_name = NULL;
11850 switch (r_code)
11852 case PLUS_EXPR:
11853 case MULT_EXPR:
11854 case MINUS_EXPR:
11855 break;
11856 case MIN_EXPR:
11857 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11858 r_name = "min";
11859 break;
11860 case MAX_EXPR:
11861 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11862 r_name = "max";
11863 break;
11864 case BIT_AND_EXPR:
11865 r_name = "&";
11866 break;
11867 case BIT_XOR_EXPR:
11868 r_name = "^";
11869 break;
11870 case BIT_IOR_EXPR:
11871 r_name = "|";
11872 break;
11873 case TRUTH_ANDIF_EXPR:
11874 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11875 r_name = "&&";
11876 break;
11877 case TRUTH_ORIF_EXPR:
11878 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11879 r_name = "||";
11880 break;
11881 default:
11882 gcc_unreachable ();
11884 if (r_name)
11886 error_at (OMP_CLAUSE_LOCATION (c),
11887 "%qE has invalid type for %<reduction(%s)%>",
11888 t, r_name);
11889 remove = true;
11890 break;
11893 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11895 error_at (OMP_CLAUSE_LOCATION (c),
11896 "user defined reduction not found for %qD", t);
11897 remove = true;
11898 break;
11900 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11902 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11903 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11904 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11905 VAR_DECL, NULL_TREE, type);
11906 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11907 DECL_ARTIFICIAL (placeholder) = 1;
11908 DECL_IGNORED_P (placeholder) = 1;
11909 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11910 c_mark_addressable (placeholder);
11911 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11912 c_mark_addressable (OMP_CLAUSE_DECL (c));
11913 OMP_CLAUSE_REDUCTION_MERGE (c)
11914 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11915 TREE_VEC_ELT (list, 0),
11916 TREE_VEC_ELT (list, 1),
11917 OMP_CLAUSE_DECL (c), placeholder);
11918 OMP_CLAUSE_REDUCTION_MERGE (c)
11919 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11920 void_type_node, NULL_TREE,
11921 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11922 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11923 if (TREE_VEC_LENGTH (list) == 6)
11925 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11926 c_mark_addressable (OMP_CLAUSE_DECL (c));
11927 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11928 c_mark_addressable (placeholder);
11929 tree init = TREE_VEC_ELT (list, 5);
11930 if (init == error_mark_node)
11931 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11932 OMP_CLAUSE_REDUCTION_INIT (c)
11933 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11934 TREE_VEC_ELT (list, 3),
11935 OMP_CLAUSE_DECL (c), placeholder);
11936 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11937 OMP_CLAUSE_REDUCTION_INIT (c)
11938 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11939 OMP_CLAUSE_REDUCTION_INIT (c));
11940 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11941 c_find_omp_placeholder_r,
11942 placeholder, NULL))
11943 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11945 else
11947 tree init;
11948 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11949 init = build_constructor (TREE_TYPE (t), NULL);
11950 else
11951 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11952 OMP_CLAUSE_REDUCTION_INIT (c)
11953 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11955 OMP_CLAUSE_REDUCTION_INIT (c)
11956 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11957 void_type_node, NULL_TREE,
11958 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11959 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11961 goto check_dup_generic;
11963 case OMP_CLAUSE_COPYPRIVATE:
11964 copyprivate_seen = true;
11965 if (nowait_clause)
11967 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11968 "%<nowait%> clause must not be used together "
11969 "with %<copyprivate%>");
11970 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
11971 nowait_clause = NULL;
11973 goto check_dup_generic;
11975 case OMP_CLAUSE_COPYIN:
11976 t = OMP_CLAUSE_DECL (c);
11977 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
11979 error_at (OMP_CLAUSE_LOCATION (c),
11980 "%qE must be %<threadprivate%> for %<copyin%>", t);
11981 remove = true;
11982 break;
11984 goto check_dup_generic;
11986 case OMP_CLAUSE_LINEAR:
11987 t = OMP_CLAUSE_DECL (c);
11988 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11989 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
11991 error_at (OMP_CLAUSE_LOCATION (c),
11992 "linear clause applied to non-integral non-pointer "
11993 "variable with type %qT", TREE_TYPE (t));
11994 remove = true;
11995 break;
11997 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
11999 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12000 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12001 OMP_CLAUSE_DECL (c), s);
12002 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12003 sizetype, s, OMP_CLAUSE_DECL (c));
12004 if (s == error_mark_node)
12005 s = size_one_node;
12006 OMP_CLAUSE_LINEAR_STEP (c) = s;
12008 else
12009 OMP_CLAUSE_LINEAR_STEP (c)
12010 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12011 goto check_dup_generic;
12013 check_dup_generic:
12014 t = OMP_CLAUSE_DECL (c);
12015 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12017 error_at (OMP_CLAUSE_LOCATION (c),
12018 "%qE is not a variable in clause %qs", t,
12019 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12020 remove = true;
12022 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12023 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12024 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12026 error_at (OMP_CLAUSE_LOCATION (c),
12027 "%qE appears more than once in data clauses", t);
12028 remove = true;
12030 else
12031 bitmap_set_bit (&generic_head, DECL_UID (t));
12032 break;
12034 case OMP_CLAUSE_FIRSTPRIVATE:
12035 t = OMP_CLAUSE_DECL (c);
12036 need_complete = true;
12037 need_implicitly_determined = true;
12038 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12040 error_at (OMP_CLAUSE_LOCATION (c),
12041 "%qE is not a variable in clause %<firstprivate%>", t);
12042 remove = true;
12044 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12045 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12047 error_at (OMP_CLAUSE_LOCATION (c),
12048 "%qE appears more than once in data clauses", t);
12049 remove = true;
12051 else
12052 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12053 break;
12055 case OMP_CLAUSE_LASTPRIVATE:
12056 t = OMP_CLAUSE_DECL (c);
12057 need_complete = true;
12058 need_implicitly_determined = true;
12059 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12061 error_at (OMP_CLAUSE_LOCATION (c),
12062 "%qE is not a variable in clause %<lastprivate%>", t);
12063 remove = true;
12065 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12066 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12068 error_at (OMP_CLAUSE_LOCATION (c),
12069 "%qE appears more than once in data clauses", t);
12070 remove = true;
12072 else
12073 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12074 break;
12076 case OMP_CLAUSE_ALIGNED:
12077 t = OMP_CLAUSE_DECL (c);
12078 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12080 error_at (OMP_CLAUSE_LOCATION (c),
12081 "%qE is not a variable in %<aligned%> clause", t);
12082 remove = true;
12084 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12085 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12087 error_at (OMP_CLAUSE_LOCATION (c),
12088 "%qE in %<aligned%> clause is neither a pointer nor "
12089 "an array", t);
12090 remove = true;
12092 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12094 error_at (OMP_CLAUSE_LOCATION (c),
12095 "%qE appears more than once in %<aligned%> clauses",
12097 remove = true;
12099 else
12100 bitmap_set_bit (&aligned_head, DECL_UID (t));
12101 break;
12103 case OMP_CLAUSE_DEPEND:
12104 t = OMP_CLAUSE_DECL (c);
12105 if (TREE_CODE (t) == TREE_LIST)
12107 if (handle_omp_array_sections (c))
12108 remove = true;
12109 break;
12111 if (t == error_mark_node)
12112 remove = true;
12113 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12115 error_at (OMP_CLAUSE_LOCATION (c),
12116 "%qE is not a variable in %<depend%> clause", t);
12117 remove = true;
12119 else if (!c_mark_addressable (t))
12120 remove = true;
12121 break;
12123 case OMP_CLAUSE_MAP:
12124 case OMP_CLAUSE_TO:
12125 case OMP_CLAUSE_FROM:
12126 t = OMP_CLAUSE_DECL (c);
12127 if (TREE_CODE (t) == TREE_LIST)
12129 if (handle_omp_array_sections (c))
12130 remove = true;
12131 else
12133 t = OMP_CLAUSE_DECL (c);
12134 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12136 error_at (OMP_CLAUSE_LOCATION (c),
12137 "array section does not have mappable type "
12138 "in %qs clause",
12139 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12140 remove = true;
12143 break;
12145 if (t == error_mark_node)
12146 remove = true;
12147 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12149 error_at (OMP_CLAUSE_LOCATION (c),
12150 "%qE is not a variable in %qs clause", t,
12151 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12152 remove = true;
12154 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12156 error_at (OMP_CLAUSE_LOCATION (c),
12157 "%qD is threadprivate variable in %qs clause", t,
12158 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12159 remove = true;
12161 else if (!c_mark_addressable (t))
12162 remove = true;
12163 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12164 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
12165 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12167 error_at (OMP_CLAUSE_LOCATION (c),
12168 "%qD does not have a mappable type in %qs clause", t,
12169 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12170 remove = true;
12172 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12174 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12175 error ("%qD appears more than once in motion clauses", t);
12176 else
12177 error ("%qD appears more than once in map clauses", t);
12178 remove = true;
12180 else
12181 bitmap_set_bit (&generic_head, DECL_UID (t));
12182 break;
12184 case OMP_CLAUSE_UNIFORM:
12185 t = OMP_CLAUSE_DECL (c);
12186 if (TREE_CODE (t) != PARM_DECL)
12188 if (DECL_P (t))
12189 error_at (OMP_CLAUSE_LOCATION (c),
12190 "%qD is not an argument in %<uniform%> clause", t);
12191 else
12192 error_at (OMP_CLAUSE_LOCATION (c),
12193 "%qE is not an argument in %<uniform%> clause", t);
12194 remove = true;
12195 break;
12197 goto check_dup_generic;
12199 case OMP_CLAUSE_NOWAIT:
12200 if (copyprivate_seen)
12202 error_at (OMP_CLAUSE_LOCATION (c),
12203 "%<nowait%> clause must not be used together "
12204 "with %<copyprivate%>");
12205 remove = true;
12206 break;
12208 nowait_clause = pc;
12209 pc = &OMP_CLAUSE_CHAIN (c);
12210 continue;
12212 case OMP_CLAUSE_IF:
12213 case OMP_CLAUSE_NUM_THREADS:
12214 case OMP_CLAUSE_NUM_TEAMS:
12215 case OMP_CLAUSE_THREAD_LIMIT:
12216 case OMP_CLAUSE_SCHEDULE:
12217 case OMP_CLAUSE_ORDERED:
12218 case OMP_CLAUSE_DEFAULT:
12219 case OMP_CLAUSE_UNTIED:
12220 case OMP_CLAUSE_COLLAPSE:
12221 case OMP_CLAUSE_FINAL:
12222 case OMP_CLAUSE_MERGEABLE:
12223 case OMP_CLAUSE_SAFELEN:
12224 case OMP_CLAUSE_SIMDLEN:
12225 case OMP_CLAUSE_DEVICE:
12226 case OMP_CLAUSE_DIST_SCHEDULE:
12227 case OMP_CLAUSE_PARALLEL:
12228 case OMP_CLAUSE_FOR:
12229 case OMP_CLAUSE_SECTIONS:
12230 case OMP_CLAUSE_TASKGROUP:
12231 case OMP_CLAUSE_PROC_BIND:
12232 case OMP_CLAUSE__CILK_FOR_COUNT_:
12233 pc = &OMP_CLAUSE_CHAIN (c);
12234 continue;
12236 case OMP_CLAUSE_INBRANCH:
12237 case OMP_CLAUSE_NOTINBRANCH:
12238 if (branch_seen)
12240 error_at (OMP_CLAUSE_LOCATION (c),
12241 "%<inbranch%> clause is incompatible with "
12242 "%<notinbranch%>");
12243 remove = true;
12244 break;
12246 branch_seen = true;
12247 pc = &OMP_CLAUSE_CHAIN (c);
12248 continue;
12250 default:
12251 gcc_unreachable ();
12254 if (!remove)
12256 t = OMP_CLAUSE_DECL (c);
12258 if (need_complete)
12260 t = require_complete_type (t);
12261 if (t == error_mark_node)
12262 remove = true;
12265 if (need_implicitly_determined)
12267 const char *share_name = NULL;
12269 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12270 share_name = "threadprivate";
12271 else switch (c_omp_predetermined_sharing (t))
12273 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12274 break;
12275 case OMP_CLAUSE_DEFAULT_SHARED:
12276 /* const vars may be specified in firstprivate clause. */
12277 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12278 && TREE_READONLY (t))
12279 break;
12280 share_name = "shared";
12281 break;
12282 case OMP_CLAUSE_DEFAULT_PRIVATE:
12283 share_name = "private";
12284 break;
12285 default:
12286 gcc_unreachable ();
12288 if (share_name)
12290 error_at (OMP_CLAUSE_LOCATION (c),
12291 "%qE is predetermined %qs for %qs",
12292 t, share_name,
12293 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12294 remove = true;
12299 if (remove)
12300 *pc = OMP_CLAUSE_CHAIN (c);
12301 else
12302 pc = &OMP_CLAUSE_CHAIN (c);
12305 bitmap_obstack_release (NULL);
12306 return clauses;
12309 /* Create a transaction node. */
12311 tree
12312 c_finish_transaction (location_t loc, tree block, int flags)
12314 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12315 if (flags & TM_STMT_ATTR_OUTER)
12316 TRANSACTION_EXPR_OUTER (stmt) = 1;
12317 if (flags & TM_STMT_ATTR_RELAXED)
12318 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12319 return add_stmt (stmt);
12322 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12323 down to the element type of an array. */
12325 tree
12326 c_build_qualified_type (tree type, int type_quals)
12328 if (type == error_mark_node)
12329 return type;
12331 if (TREE_CODE (type) == ARRAY_TYPE)
12333 tree t;
12334 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12335 type_quals);
12337 /* See if we already have an identically qualified type. */
12338 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12340 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12341 && TYPE_NAME (t) == TYPE_NAME (type)
12342 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12343 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12344 TYPE_ATTRIBUTES (type)))
12345 break;
12347 if (!t)
12349 tree domain = TYPE_DOMAIN (type);
12351 t = build_variant_type_copy (type);
12352 TREE_TYPE (t) = element_type;
12354 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12355 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12356 SET_TYPE_STRUCTURAL_EQUALITY (t);
12357 else if (TYPE_CANONICAL (element_type) != element_type
12358 || (domain && TYPE_CANONICAL (domain) != domain))
12360 tree unqualified_canon
12361 = build_array_type (TYPE_CANONICAL (element_type),
12362 domain? TYPE_CANONICAL (domain)
12363 : NULL_TREE);
12364 TYPE_CANONICAL (t)
12365 = c_build_qualified_type (unqualified_canon, type_quals);
12367 else
12368 TYPE_CANONICAL (t) = t;
12370 return t;
12373 /* A restrict-qualified pointer type must be a pointer to object or
12374 incomplete type. Note that the use of POINTER_TYPE_P also allows
12375 REFERENCE_TYPEs, which is appropriate for C++. */
12376 if ((type_quals & TYPE_QUAL_RESTRICT)
12377 && (!POINTER_TYPE_P (type)
12378 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12380 error ("invalid use of %<restrict%>");
12381 type_quals &= ~TYPE_QUAL_RESTRICT;
12384 return build_qualified_type (type, type_quals);
12387 /* Build a VA_ARG_EXPR for the C parser. */
12389 tree
12390 c_build_va_arg (location_t loc, tree expr, tree type)
12392 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12393 warning_at (loc, OPT_Wc___compat,
12394 "C++ requires promoted type, not enum type, in %<va_arg%>");
12395 return build_va_arg (loc, expr, type);
12398 /* Return truthvalue of whether T1 is the same tree structure as T2.
12399 Return 1 if they are the same. Return 0 if they are different. */
12401 bool
12402 c_tree_equal (tree t1, tree t2)
12404 enum tree_code code1, code2;
12406 if (t1 == t2)
12407 return true;
12408 if (!t1 || !t2)
12409 return false;
12411 for (code1 = TREE_CODE (t1);
12412 CONVERT_EXPR_CODE_P (code1)
12413 || code1 == NON_LVALUE_EXPR;
12414 code1 = TREE_CODE (t1))
12415 t1 = TREE_OPERAND (t1, 0);
12416 for (code2 = TREE_CODE (t2);
12417 CONVERT_EXPR_CODE_P (code2)
12418 || code2 == NON_LVALUE_EXPR;
12419 code2 = TREE_CODE (t2))
12420 t2 = TREE_OPERAND (t2, 0);
12422 /* They might have become equal now. */
12423 if (t1 == t2)
12424 return true;
12426 if (code1 != code2)
12427 return false;
12429 switch (code1)
12431 case INTEGER_CST:
12432 return wi::eq_p (t1, t2);
12434 case REAL_CST:
12435 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12437 case STRING_CST:
12438 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12439 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12440 TREE_STRING_LENGTH (t1));
12442 case FIXED_CST:
12443 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12444 TREE_FIXED_CST (t2));
12446 case COMPLEX_CST:
12447 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12448 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12450 case VECTOR_CST:
12451 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12453 case CONSTRUCTOR:
12454 /* We need to do this when determining whether or not two
12455 non-type pointer to member function template arguments
12456 are the same. */
12457 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12458 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12459 return false;
12461 tree field, value;
12462 unsigned int i;
12463 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12465 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12466 if (!c_tree_equal (field, elt2->index)
12467 || !c_tree_equal (value, elt2->value))
12468 return false;
12471 return true;
12473 case TREE_LIST:
12474 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12475 return false;
12476 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12477 return false;
12478 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12480 case SAVE_EXPR:
12481 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12483 case CALL_EXPR:
12485 tree arg1, arg2;
12486 call_expr_arg_iterator iter1, iter2;
12487 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12488 return false;
12489 for (arg1 = first_call_expr_arg (t1, &iter1),
12490 arg2 = first_call_expr_arg (t2, &iter2);
12491 arg1 && arg2;
12492 arg1 = next_call_expr_arg (&iter1),
12493 arg2 = next_call_expr_arg (&iter2))
12494 if (!c_tree_equal (arg1, arg2))
12495 return false;
12496 if (arg1 || arg2)
12497 return false;
12498 return true;
12501 case TARGET_EXPR:
12503 tree o1 = TREE_OPERAND (t1, 0);
12504 tree o2 = TREE_OPERAND (t2, 0);
12506 /* Special case: if either target is an unallocated VAR_DECL,
12507 it means that it's going to be unified with whatever the
12508 TARGET_EXPR is really supposed to initialize, so treat it
12509 as being equivalent to anything. */
12510 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12511 && !DECL_RTL_SET_P (o1))
12512 /*Nop*/;
12513 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12514 && !DECL_RTL_SET_P (o2))
12515 /*Nop*/;
12516 else if (!c_tree_equal (o1, o2))
12517 return false;
12519 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12522 case COMPONENT_REF:
12523 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12524 return false;
12525 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12527 case PARM_DECL:
12528 case VAR_DECL:
12529 case CONST_DECL:
12530 case FIELD_DECL:
12531 case FUNCTION_DECL:
12532 case IDENTIFIER_NODE:
12533 case SSA_NAME:
12534 return false;
12536 case TREE_VEC:
12538 unsigned ix;
12539 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12540 return false;
12541 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12542 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12543 TREE_VEC_ELT (t2, ix)))
12544 return false;
12545 return true;
12548 default:
12549 break;
12552 switch (TREE_CODE_CLASS (code1))
12554 case tcc_unary:
12555 case tcc_binary:
12556 case tcc_comparison:
12557 case tcc_expression:
12558 case tcc_vl_exp:
12559 case tcc_reference:
12560 case tcc_statement:
12562 int i, n = TREE_OPERAND_LENGTH (t1);
12564 switch (code1)
12566 case PREINCREMENT_EXPR:
12567 case PREDECREMENT_EXPR:
12568 case POSTINCREMENT_EXPR:
12569 case POSTDECREMENT_EXPR:
12570 n = 1;
12571 break;
12572 case ARRAY_REF:
12573 n = 2;
12574 break;
12575 default:
12576 break;
12579 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12580 && n != TREE_OPERAND_LENGTH (t2))
12581 return false;
12583 for (i = 0; i < n; ++i)
12584 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12585 return false;
12587 return true;
12590 case tcc_type:
12591 return comptypes (t1, t2);
12592 default:
12593 gcc_unreachable ();
12595 /* We can get here with --disable-checking. */
12596 return false;
12599 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12600 spawn-helper and BODY is the newly created body for FNDECL. */
12602 void
12603 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12605 tree list = alloc_stmt_list ();
12606 tree frame = make_cilk_frame (fndecl);
12607 tree dtor = create_cilk_function_exit (frame, false, true);
12608 add_local_decl (cfun, frame);
12610 DECL_SAVED_TREE (fndecl) = list;
12611 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12612 frame);
12613 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12614 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12616 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12617 append_to_statement_list (detach_expr, &body_list);
12619 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12620 body = fold_build_cleanup_point_expr (void_type_node, body);
12622 append_to_statement_list (body, &body_list);
12623 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12624 body_list, dtor), &list);