Merge from trunk @222673.
[official-gcc.git] / gcc / c / c-typeck.c
blob5a1eb2761cb6428698d7b5ab8abcc4faf6fae8a1
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
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 "hash-set.h"
31 #include "vec.h"
32 #include "symtab.h"
33 #include "input.h"
34 #include "alias.h"
35 #include "double-int.h"
36 #include "machmode.h"
37 #include "inchash.h"
38 #include "real.h"
39 #include "fixed-value.h"
40 #include "tree.h"
41 #include "fold-const.h"
42 #include "stor-layout.h"
43 #include "trans-mem.h"
44 #include "varasm.h"
45 #include "stmt.h"
46 #include "langhooks.h"
47 #include "c-tree.h"
48 #include "c-lang.h"
49 #include "flags.h"
50 #include "intl.h"
51 #include "target.h"
52 #include "tree-iterator.h"
53 #include "bitmap.h"
54 #include "predict.h"
55 #include "vec.h"
56 #include "hashtab.h"
57 #include "hash-set.h"
58 #include "machmode.h"
59 #include "hard-reg-set.h"
60 #include "input.h"
61 #include "function.h"
62 #include "gimple-expr.h"
63 #include "gimplify.h"
64 #include "tree-inline.h"
65 #include "omp-low.h"
66 #include "c-family/c-objc.h"
67 #include "c-family/c-common.h"
68 #include "c-family/c-ubsan.h"
69 #include "cilk.h"
70 #include "wide-int.h"
71 #include "gomp-constants.h"
73 /* Possible cases of implicit bad conversions. Used to select
74 diagnostic messages in convert_for_assignment. */
75 enum impl_conv {
76 ic_argpass,
77 ic_assign,
78 ic_init,
79 ic_return
82 /* The level of nesting inside "__alignof__". */
83 int in_alignof;
85 /* The level of nesting inside "sizeof". */
86 int in_sizeof;
88 /* The level of nesting inside "typeof". */
89 int in_typeof;
91 /* The argument of last parsed sizeof expression, only to be tested
92 if expr.original_code == SIZEOF_EXPR. */
93 tree c_last_sizeof_arg;
95 /* Nonzero if we might need to print a "missing braces around
96 initializer" message within this initializer. */
97 static int found_missing_braces;
99 static int require_constant_value;
100 static int require_constant_elements;
102 static bool null_pointer_constant_p (const_tree);
103 static tree qualify_type (tree, tree);
104 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
105 bool *);
106 static int comp_target_types (location_t, tree, tree);
107 static int function_types_compatible_p (const_tree, const_tree, bool *,
108 bool *);
109 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
110 static tree lookup_field (tree, tree);
111 static int convert_arguments (location_t, vec<location_t>, tree,
112 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
113 tree);
114 static tree pointer_diff (location_t, tree, tree);
115 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
116 enum impl_conv, bool, tree, tree, int);
117 static tree valid_compound_expr_initializer (tree, tree);
118 static void push_string (const char *);
119 static void push_member_name (tree);
120 static int spelling_length (void);
121 static char *print_spelling (char *);
122 static void warning_init (location_t, int, const char *);
123 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
124 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
125 bool, struct obstack *);
126 static void output_pending_init_elements (int, struct obstack *);
127 static int set_designator (location_t, int, struct obstack *);
128 static void push_range_stack (tree, struct obstack *);
129 static void add_pending_init (location_t, tree, tree, tree, bool,
130 struct obstack *);
131 static void set_nonincremental_init (struct obstack *);
132 static void set_nonincremental_init_from_string (tree, struct obstack *);
133 static tree find_init_member (tree, struct obstack *);
134 static void readonly_warning (tree, enum lvalue_use);
135 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
136 static void record_maybe_used_decl (tree);
137 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
139 /* Return true if EXP is a null pointer constant, false otherwise. */
141 static bool
142 null_pointer_constant_p (const_tree expr)
144 /* This should really operate on c_expr structures, but they aren't
145 yet available everywhere required. */
146 tree type = TREE_TYPE (expr);
147 return (TREE_CODE (expr) == INTEGER_CST
148 && !TREE_OVERFLOW (expr)
149 && integer_zerop (expr)
150 && (INTEGRAL_TYPE_P (type)
151 || (TREE_CODE (type) == POINTER_TYPE
152 && VOID_TYPE_P (TREE_TYPE (type))
153 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
156 /* EXPR may appear in an unevaluated part of an integer constant
157 expression, but not in an evaluated part. Wrap it in a
158 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
159 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
161 static tree
162 note_integer_operands (tree expr)
164 tree ret;
165 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
167 ret = copy_node (expr);
168 TREE_OVERFLOW (ret) = 1;
170 else
172 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
173 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
175 return ret;
178 /* Having checked whether EXPR may appear in an unevaluated part of an
179 integer constant expression and found that it may, remove any
180 C_MAYBE_CONST_EXPR noting this fact and return the resulting
181 expression. */
183 static inline tree
184 remove_c_maybe_const_expr (tree expr)
186 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
187 return C_MAYBE_CONST_EXPR_EXPR (expr);
188 else
189 return expr;
192 \f/* This is a cache to hold if two types are compatible or not. */
194 struct tagged_tu_seen_cache {
195 const struct tagged_tu_seen_cache * next;
196 const_tree t1;
197 const_tree t2;
198 /* The return value of tagged_types_tu_compatible_p if we had seen
199 these two types already. */
200 int val;
203 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
204 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
206 /* Do `exp = require_complete_type (exp);' to make sure exp
207 does not have an incomplete type. (That includes void types.) */
209 tree
210 require_complete_type (tree value)
212 tree type = TREE_TYPE (value);
214 if (error_operand_p (value))
215 return error_mark_node;
217 /* First, detect a valid value with a complete type. */
218 if (COMPLETE_TYPE_P (type))
219 return value;
221 c_incomplete_type_error (value, type);
222 return error_mark_node;
225 /* Print an error message for invalid use of an incomplete type.
226 VALUE is the expression that was used (or 0 if that isn't known)
227 and TYPE is the type that was invalid. */
229 void
230 c_incomplete_type_error (const_tree value, const_tree type)
232 /* Avoid duplicate error message. */
233 if (TREE_CODE (type) == ERROR_MARK)
234 return;
236 if (value != 0 && (TREE_CODE (value) == VAR_DECL
237 || TREE_CODE (value) == PARM_DECL))
238 error ("%qD has an incomplete type %qT", value, type);
239 else
241 retry:
242 /* We must print an error message. Be clever about what it says. */
244 switch (TREE_CODE (type))
246 case RECORD_TYPE:
247 case UNION_TYPE:
248 case ENUMERAL_TYPE:
249 break;
251 case VOID_TYPE:
252 error ("invalid use of void expression");
253 return;
255 case ARRAY_TYPE:
256 if (TYPE_DOMAIN (type))
258 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
260 error ("invalid use of flexible array member");
261 return;
263 type = TREE_TYPE (type);
264 goto retry;
266 error ("invalid use of array with unspecified bounds");
267 return;
269 default:
270 gcc_unreachable ();
273 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
274 error ("invalid use of undefined type %qT", type);
275 else
276 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
277 error ("invalid use of incomplete typedef %qT", type);
281 /* Given a type, apply default promotions wrt unnamed function
282 arguments and return the new type. */
284 tree
285 c_type_promotes_to (tree type)
287 tree ret = NULL_TREE;
289 if (TYPE_MAIN_VARIANT (type) == float_type_node)
290 ret = double_type_node;
291 else if (c_promoting_integer_type_p (type))
293 /* Preserve unsignedness if not really getting any wider. */
294 if (TYPE_UNSIGNED (type)
295 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
296 ret = unsigned_type_node;
297 else
298 ret = integer_type_node;
301 if (ret != NULL_TREE)
302 return (TYPE_ATOMIC (type)
303 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
304 : ret);
306 return type;
309 /* Return true if between two named address spaces, whether there is a superset
310 named address space that encompasses both address spaces. If there is a
311 superset, return which address space is the superset. */
313 static bool
314 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
316 if (as1 == as2)
318 *common = as1;
319 return true;
321 else if (targetm.addr_space.subset_p (as1, as2))
323 *common = as2;
324 return true;
326 else if (targetm.addr_space.subset_p (as2, as1))
328 *common = as1;
329 return true;
331 else
332 return false;
335 /* Return a variant of TYPE which has all the type qualifiers of LIKE
336 as well as those of TYPE. */
338 static tree
339 qualify_type (tree type, tree like)
341 addr_space_t as_type = TYPE_ADDR_SPACE (type);
342 addr_space_t as_like = TYPE_ADDR_SPACE (like);
343 addr_space_t as_common;
345 /* If the two named address spaces are different, determine the common
346 superset address space. If there isn't one, raise an error. */
347 if (!addr_space_superset (as_type, as_like, &as_common))
349 as_common = as_type;
350 error ("%qT and %qT are in disjoint named address spaces",
351 type, like);
354 return c_build_qualified_type (type,
355 TYPE_QUALS_NO_ADDR_SPACE (type)
356 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
357 | ENCODE_QUAL_ADDR_SPACE (as_common));
360 /* Return true iff the given tree T is a variable length array. */
362 bool
363 c_vla_type_p (const_tree t)
365 if (TREE_CODE (t) == ARRAY_TYPE
366 && C_TYPE_VARIABLE_SIZE (t))
367 return true;
368 return false;
371 /* Return the composite type of two compatible types.
373 We assume that comptypes has already been done and returned
374 nonzero; if that isn't so, this may crash. In particular, we
375 assume that qualifiers match. */
377 tree
378 composite_type (tree t1, tree t2)
380 enum tree_code code1;
381 enum tree_code code2;
382 tree attributes;
384 /* Save time if the two types are the same. */
386 if (t1 == t2) return t1;
388 /* If one type is nonsense, use the other. */
389 if (t1 == error_mark_node)
390 return t2;
391 if (t2 == error_mark_node)
392 return t1;
394 code1 = TREE_CODE (t1);
395 code2 = TREE_CODE (t2);
397 /* Merge the attributes. */
398 attributes = targetm.merge_type_attributes (t1, t2);
400 /* If one is an enumerated type and the other is the compatible
401 integer type, the composite type might be either of the two
402 (DR#013 question 3). For consistency, use the enumerated type as
403 the composite type. */
405 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
406 return t1;
407 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
408 return t2;
410 gcc_assert (code1 == code2);
412 switch (code1)
414 case POINTER_TYPE:
415 /* For two pointers, do this recursively on the target type. */
417 tree pointed_to_1 = TREE_TYPE (t1);
418 tree pointed_to_2 = TREE_TYPE (t2);
419 tree target = composite_type (pointed_to_1, pointed_to_2);
420 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
421 t1 = build_type_attribute_variant (t1, attributes);
422 return qualify_type (t1, t2);
425 case ARRAY_TYPE:
427 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
428 int quals;
429 tree unqual_elt;
430 tree d1 = TYPE_DOMAIN (t1);
431 tree d2 = TYPE_DOMAIN (t2);
432 bool d1_variable, d2_variable;
433 bool d1_zero, d2_zero;
434 bool t1_complete, t2_complete;
436 /* We should not have any type quals on arrays at all. */
437 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
438 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
440 t1_complete = COMPLETE_TYPE_P (t1);
441 t2_complete = COMPLETE_TYPE_P (t2);
443 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
444 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
446 d1_variable = (!d1_zero
447 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
448 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
449 d2_variable = (!d2_zero
450 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
451 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
452 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
453 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
455 /* Save space: see if the result is identical to one of the args. */
456 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
457 && (d2_variable || d2_zero || !d1_variable))
458 return build_type_attribute_variant (t1, attributes);
459 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
460 && (d1_variable || d1_zero || !d2_variable))
461 return build_type_attribute_variant (t2, attributes);
463 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
464 return build_type_attribute_variant (t1, attributes);
465 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
466 return build_type_attribute_variant (t2, attributes);
468 /* Merge the element types, and have a size if either arg has
469 one. We may have qualifiers on the element types. To set
470 up TYPE_MAIN_VARIANT correctly, we need to form the
471 composite of the unqualified types and add the qualifiers
472 back at the end. */
473 quals = TYPE_QUALS (strip_array_types (elt));
474 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
475 t1 = build_array_type (unqual_elt,
476 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
477 && (d2_variable
478 || d2_zero
479 || !d1_variable))
480 ? t1
481 : t2));
482 /* Ensure a composite type involving a zero-length array type
483 is a zero-length type not an incomplete type. */
484 if (d1_zero && d2_zero
485 && (t1_complete || t2_complete)
486 && !COMPLETE_TYPE_P (t1))
488 TYPE_SIZE (t1) = bitsize_zero_node;
489 TYPE_SIZE_UNIT (t1) = size_zero_node;
491 t1 = c_build_qualified_type (t1, quals);
492 return build_type_attribute_variant (t1, attributes);
495 case ENUMERAL_TYPE:
496 case RECORD_TYPE:
497 case UNION_TYPE:
498 if (attributes != NULL)
500 /* Try harder not to create a new aggregate type. */
501 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
502 return t1;
503 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
504 return t2;
506 return build_type_attribute_variant (t1, attributes);
508 case FUNCTION_TYPE:
509 /* Function types: prefer the one that specified arg types.
510 If both do, merge the arg types. Also merge the return types. */
512 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
513 tree p1 = TYPE_ARG_TYPES (t1);
514 tree p2 = TYPE_ARG_TYPES (t2);
515 int len;
516 tree newargs, n;
517 int i;
519 /* Save space: see if the result is identical to one of the args. */
520 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
521 return build_type_attribute_variant (t1, attributes);
522 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
523 return build_type_attribute_variant (t2, attributes);
525 /* Simple way if one arg fails to specify argument types. */
526 if (TYPE_ARG_TYPES (t1) == 0)
528 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
529 t1 = build_type_attribute_variant (t1, attributes);
530 return qualify_type (t1, t2);
532 if (TYPE_ARG_TYPES (t2) == 0)
534 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
535 t1 = build_type_attribute_variant (t1, attributes);
536 return qualify_type (t1, t2);
539 /* If both args specify argument types, we must merge the two
540 lists, argument by argument. */
542 len = list_length (p1);
543 newargs = 0;
545 for (i = 0; i < len; i++)
546 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
548 n = newargs;
550 for (; p1;
551 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
553 /* A null type means arg type is not specified.
554 Take whatever the other function type has. */
555 if (TREE_VALUE (p1) == 0)
557 TREE_VALUE (n) = TREE_VALUE (p2);
558 goto parm_done;
560 if (TREE_VALUE (p2) == 0)
562 TREE_VALUE (n) = TREE_VALUE (p1);
563 goto parm_done;
566 /* Given wait (union {union wait *u; int *i} *)
567 and wait (union wait *),
568 prefer union wait * as type of parm. */
569 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
570 && TREE_VALUE (p1) != TREE_VALUE (p2))
572 tree memb;
573 tree mv2 = TREE_VALUE (p2);
574 if (mv2 && mv2 != error_mark_node
575 && TREE_CODE (mv2) != ARRAY_TYPE)
576 mv2 = TYPE_MAIN_VARIANT (mv2);
577 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
578 memb; memb = DECL_CHAIN (memb))
580 tree mv3 = TREE_TYPE (memb);
581 if (mv3 && mv3 != error_mark_node
582 && TREE_CODE (mv3) != ARRAY_TYPE)
583 mv3 = TYPE_MAIN_VARIANT (mv3);
584 if (comptypes (mv3, mv2))
586 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
587 TREE_VALUE (p2));
588 pedwarn (input_location, OPT_Wpedantic,
589 "function types not truly compatible in ISO C");
590 goto parm_done;
594 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
595 && TREE_VALUE (p2) != TREE_VALUE (p1))
597 tree memb;
598 tree mv1 = TREE_VALUE (p1);
599 if (mv1 && mv1 != error_mark_node
600 && TREE_CODE (mv1) != ARRAY_TYPE)
601 mv1 = TYPE_MAIN_VARIANT (mv1);
602 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
603 memb; memb = DECL_CHAIN (memb))
605 tree mv3 = TREE_TYPE (memb);
606 if (mv3 && mv3 != error_mark_node
607 && TREE_CODE (mv3) != ARRAY_TYPE)
608 mv3 = TYPE_MAIN_VARIANT (mv3);
609 if (comptypes (mv3, mv1))
611 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
612 TREE_VALUE (p1));
613 pedwarn (input_location, OPT_Wpedantic,
614 "function types not truly compatible in ISO C");
615 goto parm_done;
619 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
620 parm_done: ;
623 t1 = build_function_type (valtype, newargs);
624 t1 = qualify_type (t1, t2);
625 /* ... falls through ... */
628 default:
629 return build_type_attribute_variant (t1, attributes);
634 /* Return the type of a conditional expression between pointers to
635 possibly differently qualified versions of compatible types.
637 We assume that comp_target_types has already been done and returned
638 nonzero; if that isn't so, this may crash. */
640 static tree
641 common_pointer_type (tree t1, tree t2)
643 tree attributes;
644 tree pointed_to_1, mv1;
645 tree pointed_to_2, mv2;
646 tree target;
647 unsigned target_quals;
648 addr_space_t as1, as2, as_common;
649 int quals1, quals2;
651 /* Save time if the two types are the same. */
653 if (t1 == t2) return t1;
655 /* If one type is nonsense, use the other. */
656 if (t1 == error_mark_node)
657 return t2;
658 if (t2 == error_mark_node)
659 return t1;
661 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
662 && TREE_CODE (t2) == POINTER_TYPE);
664 /* Merge the attributes. */
665 attributes = targetm.merge_type_attributes (t1, t2);
667 /* Find the composite type of the target types, and combine the
668 qualifiers of the two types' targets. Do not lose qualifiers on
669 array element types by taking the TYPE_MAIN_VARIANT. */
670 mv1 = pointed_to_1 = TREE_TYPE (t1);
671 mv2 = pointed_to_2 = TREE_TYPE (t2);
672 if (TREE_CODE (mv1) != ARRAY_TYPE)
673 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
674 if (TREE_CODE (mv2) != ARRAY_TYPE)
675 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
676 target = composite_type (mv1, mv2);
678 /* Strip array types to get correct qualifier for pointers to arrays */
679 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
680 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
682 /* For function types do not merge const qualifiers, but drop them
683 if used inconsistently. The middle-end uses these to mark const
684 and noreturn functions. */
685 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
686 target_quals = (quals1 & quals2);
687 else
688 target_quals = (quals1 | quals2);
690 /* If the two named address spaces are different, determine the common
691 superset address space. This is guaranteed to exist due to the
692 assumption that comp_target_type returned non-zero. */
693 as1 = TYPE_ADDR_SPACE (pointed_to_1);
694 as2 = TYPE_ADDR_SPACE (pointed_to_2);
695 if (!addr_space_superset (as1, as2, &as_common))
696 gcc_unreachable ();
698 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
700 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
701 return build_type_attribute_variant (t1, attributes);
704 /* Return the common type for two arithmetic types under the usual
705 arithmetic conversions. The default conversions have already been
706 applied, and enumerated types converted to their compatible integer
707 types. The resulting type is unqualified and has no attributes.
709 This is the type for the result of most arithmetic operations
710 if the operands have the given two types. */
712 static tree
713 c_common_type (tree t1, tree t2)
715 enum tree_code code1;
716 enum tree_code code2;
718 /* If one type is nonsense, use the other. */
719 if (t1 == error_mark_node)
720 return t2;
721 if (t2 == error_mark_node)
722 return t1;
724 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
725 t1 = TYPE_MAIN_VARIANT (t1);
727 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
728 t2 = TYPE_MAIN_VARIANT (t2);
730 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
731 t1 = build_type_attribute_variant (t1, NULL_TREE);
733 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
734 t2 = build_type_attribute_variant (t2, NULL_TREE);
736 /* Save time if the two types are the same. */
738 if (t1 == t2) return t1;
740 code1 = TREE_CODE (t1);
741 code2 = TREE_CODE (t2);
743 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
744 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
745 || code1 == INTEGER_TYPE);
746 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
747 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
748 || code2 == INTEGER_TYPE);
750 /* When one operand is a decimal float type, the other operand cannot be
751 a generic float type or a complex type. We also disallow vector types
752 here. */
753 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
754 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
756 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
758 error ("can%'t mix operands of decimal float and vector types");
759 return error_mark_node;
761 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
763 error ("can%'t mix operands of decimal float and complex types");
764 return error_mark_node;
766 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
768 error ("can%'t mix operands of decimal float and other float types");
769 return error_mark_node;
773 /* If one type is a vector type, return that type. (How the usual
774 arithmetic conversions apply to the vector types extension is not
775 precisely specified.) */
776 if (code1 == VECTOR_TYPE)
777 return t1;
779 if (code2 == VECTOR_TYPE)
780 return t2;
782 /* If one type is complex, form the common type of the non-complex
783 components, then make that complex. Use T1 or T2 if it is the
784 required type. */
785 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
787 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
788 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
789 tree subtype = c_common_type (subtype1, subtype2);
791 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
792 return t1;
793 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
794 return t2;
795 else
796 return build_complex_type (subtype);
799 /* If only one is real, use it as the result. */
801 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
802 return t1;
804 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
805 return t2;
807 /* If both are real and either are decimal floating point types, use
808 the decimal floating point type with the greater precision. */
810 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
812 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
813 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
814 return dfloat128_type_node;
815 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
816 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
817 return dfloat64_type_node;
818 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
819 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
820 return dfloat32_type_node;
823 /* Deal with fixed-point types. */
824 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
826 unsigned int unsignedp = 0, satp = 0;
827 machine_mode m1, m2;
828 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
830 m1 = TYPE_MODE (t1);
831 m2 = TYPE_MODE (t2);
833 /* If one input type is saturating, the result type is saturating. */
834 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
835 satp = 1;
837 /* If both fixed-point types are unsigned, the result type is unsigned.
838 When mixing fixed-point and integer types, follow the sign of the
839 fixed-point type.
840 Otherwise, the result type is signed. */
841 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
842 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
843 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
844 && TYPE_UNSIGNED (t1))
845 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
846 && TYPE_UNSIGNED (t2)))
847 unsignedp = 1;
849 /* The result type is signed. */
850 if (unsignedp == 0)
852 /* If the input type is unsigned, we need to convert to the
853 signed type. */
854 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
856 enum mode_class mclass = (enum mode_class) 0;
857 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
858 mclass = MODE_FRACT;
859 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
860 mclass = MODE_ACCUM;
861 else
862 gcc_unreachable ();
863 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
865 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
867 enum mode_class mclass = (enum mode_class) 0;
868 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
869 mclass = MODE_FRACT;
870 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
871 mclass = MODE_ACCUM;
872 else
873 gcc_unreachable ();
874 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
878 if (code1 == FIXED_POINT_TYPE)
880 fbit1 = GET_MODE_FBIT (m1);
881 ibit1 = GET_MODE_IBIT (m1);
883 else
885 fbit1 = 0;
886 /* Signed integers need to subtract one sign bit. */
887 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
890 if (code2 == FIXED_POINT_TYPE)
892 fbit2 = GET_MODE_FBIT (m2);
893 ibit2 = GET_MODE_IBIT (m2);
895 else
897 fbit2 = 0;
898 /* Signed integers need to subtract one sign bit. */
899 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
902 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
903 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
904 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
905 satp);
908 /* Both real or both integers; use the one with greater precision. */
910 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
911 return t1;
912 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
913 return t2;
915 /* Same precision. Prefer long longs to longs to ints when the
916 same precision, following the C99 rules on integer type rank
917 (which are equivalent to the C90 rules for C90 types). */
919 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
920 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
921 return long_long_unsigned_type_node;
923 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
924 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
926 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
927 return long_long_unsigned_type_node;
928 else
929 return long_long_integer_type_node;
932 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
933 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
934 return long_unsigned_type_node;
936 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
937 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
939 /* But preserve unsignedness from the other type,
940 since long cannot hold all the values of an unsigned int. */
941 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
942 return long_unsigned_type_node;
943 else
944 return long_integer_type_node;
947 /* Likewise, prefer long double to double even if same size. */
948 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
949 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
950 return long_double_type_node;
952 /* Likewise, prefer double to float even if same size.
953 We got a couple of embedded targets with 32 bit doubles, and the
954 pdp11 might have 64 bit floats. */
955 if (TYPE_MAIN_VARIANT (t1) == double_type_node
956 || TYPE_MAIN_VARIANT (t2) == double_type_node)
957 return double_type_node;
959 /* Otherwise prefer the unsigned one. */
961 if (TYPE_UNSIGNED (t1))
962 return t1;
963 else
964 return t2;
967 /* Wrapper around c_common_type that is used by c-common.c and other
968 front end optimizations that remove promotions. ENUMERAL_TYPEs
969 are allowed here and are converted to their compatible integer types.
970 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
971 preferably a non-Boolean type as the common type. */
972 tree
973 common_type (tree t1, tree t2)
975 if (TREE_CODE (t1) == ENUMERAL_TYPE)
976 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
977 if (TREE_CODE (t2) == ENUMERAL_TYPE)
978 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
980 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
981 if (TREE_CODE (t1) == BOOLEAN_TYPE
982 && TREE_CODE (t2) == BOOLEAN_TYPE)
983 return boolean_type_node;
985 /* If either type is BOOLEAN_TYPE, then return the other. */
986 if (TREE_CODE (t1) == BOOLEAN_TYPE)
987 return t2;
988 if (TREE_CODE (t2) == BOOLEAN_TYPE)
989 return t1;
991 return c_common_type (t1, t2);
994 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
995 or various other operations. Return 2 if they are compatible
996 but a warning may be needed if you use them together. */
999 comptypes (tree type1, tree type2)
1001 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1002 int val;
1004 val = comptypes_internal (type1, type2, NULL, NULL);
1005 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1007 return val;
1010 /* Like comptypes, but if it returns non-zero because enum and int are
1011 compatible, it sets *ENUM_AND_INT_P to true. */
1013 static int
1014 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1016 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1017 int val;
1019 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1020 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1022 return val;
1025 /* Like comptypes, but if it returns nonzero for different types, it
1026 sets *DIFFERENT_TYPES_P to true. */
1029 comptypes_check_different_types (tree type1, tree type2,
1030 bool *different_types_p)
1032 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1033 int val;
1035 val = comptypes_internal (type1, type2, NULL, different_types_p);
1036 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1038 return val;
1041 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1042 or various other operations. Return 2 if they are compatible
1043 but a warning may be needed if you use them together. If
1044 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1045 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1046 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1047 NULL, and the types are compatible but different enough not to be
1048 permitted in C11 typedef redeclarations, then this sets
1049 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1050 false, but may or may not be set if the types are incompatible.
1051 This differs from comptypes, in that we don't free the seen
1052 types. */
1054 static int
1055 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1056 bool *different_types_p)
1058 const_tree t1 = type1;
1059 const_tree t2 = type2;
1060 int attrval, val;
1062 /* Suppress errors caused by previously reported errors. */
1064 if (t1 == t2 || !t1 || !t2
1065 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1066 return 1;
1068 /* Enumerated types are compatible with integer types, but this is
1069 not transitive: two enumerated types in the same translation unit
1070 are compatible with each other only if they are the same type. */
1072 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1074 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1075 if (TREE_CODE (t2) != VOID_TYPE)
1077 if (enum_and_int_p != NULL)
1078 *enum_and_int_p = true;
1079 if (different_types_p != NULL)
1080 *different_types_p = true;
1083 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1085 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1086 if (TREE_CODE (t1) != VOID_TYPE)
1088 if (enum_and_int_p != NULL)
1089 *enum_and_int_p = true;
1090 if (different_types_p != NULL)
1091 *different_types_p = true;
1095 if (t1 == t2)
1096 return 1;
1098 /* Different classes of types can't be compatible. */
1100 if (TREE_CODE (t1) != TREE_CODE (t2))
1101 return 0;
1103 /* Qualifiers must match. C99 6.7.3p9 */
1105 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1106 return 0;
1108 /* Allow for two different type nodes which have essentially the same
1109 definition. Note that we already checked for equality of the type
1110 qualifiers (just above). */
1112 if (TREE_CODE (t1) != ARRAY_TYPE
1113 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1114 return 1;
1116 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1117 if (!(attrval = comp_type_attributes (t1, t2)))
1118 return 0;
1120 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1121 val = 0;
1123 switch (TREE_CODE (t1))
1125 case POINTER_TYPE:
1126 /* Do not remove mode or aliasing information. */
1127 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1128 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1129 break;
1130 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1131 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1132 enum_and_int_p, different_types_p));
1133 break;
1135 case FUNCTION_TYPE:
1136 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1137 different_types_p);
1138 break;
1140 case ARRAY_TYPE:
1142 tree d1 = TYPE_DOMAIN (t1);
1143 tree d2 = TYPE_DOMAIN (t2);
1144 bool d1_variable, d2_variable;
1145 bool d1_zero, d2_zero;
1146 val = 1;
1148 /* Target types must match incl. qualifiers. */
1149 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1150 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1151 enum_and_int_p,
1152 different_types_p)))
1153 return 0;
1155 if (different_types_p != NULL
1156 && (d1 == 0) != (d2 == 0))
1157 *different_types_p = true;
1158 /* Sizes must match unless one is missing or variable. */
1159 if (d1 == 0 || d2 == 0 || d1 == d2)
1160 break;
1162 d1_zero = !TYPE_MAX_VALUE (d1);
1163 d2_zero = !TYPE_MAX_VALUE (d2);
1165 d1_variable = (!d1_zero
1166 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1167 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1168 d2_variable = (!d2_zero
1169 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1170 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1171 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1172 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1174 if (different_types_p != NULL
1175 && d1_variable != d2_variable)
1176 *different_types_p = true;
1177 if (d1_variable || d2_variable)
1178 break;
1179 if (d1_zero && d2_zero)
1180 break;
1181 if (d1_zero || d2_zero
1182 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1183 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1184 val = 0;
1186 break;
1189 case ENUMERAL_TYPE:
1190 case RECORD_TYPE:
1191 case UNION_TYPE:
1192 if (val != 1 && !same_translation_unit_p (t1, t2))
1194 tree a1 = TYPE_ATTRIBUTES (t1);
1195 tree a2 = TYPE_ATTRIBUTES (t2);
1197 if (! attribute_list_contained (a1, a2)
1198 && ! attribute_list_contained (a2, a1))
1199 break;
1201 if (attrval != 2)
1202 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1203 different_types_p);
1204 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1205 different_types_p);
1207 break;
1209 case VECTOR_TYPE:
1210 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1211 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1212 enum_and_int_p, different_types_p));
1213 break;
1215 default:
1216 break;
1218 return attrval == 2 && val == 1 ? 2 : val;
1221 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1222 their qualifiers, except for named address spaces. If the pointers point to
1223 different named addresses, then we must determine if one address space is a
1224 subset of the other. */
1226 static int
1227 comp_target_types (location_t location, tree ttl, tree ttr)
1229 int val;
1230 int val_ped;
1231 tree mvl = TREE_TYPE (ttl);
1232 tree mvr = TREE_TYPE (ttr);
1233 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1234 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1235 addr_space_t as_common;
1236 bool enum_and_int_p;
1238 /* Fail if pointers point to incompatible address spaces. */
1239 if (!addr_space_superset (asl, asr, &as_common))
1240 return 0;
1242 /* For pedantic record result of comptypes on arrays before losing
1243 qualifiers on the element type below. */
1244 val_ped = 1;
1246 if (TREE_CODE (mvl) == ARRAY_TYPE
1247 && TREE_CODE (mvr) == ARRAY_TYPE)
1248 val_ped = comptypes (mvl, mvr);
1250 /* Qualifiers on element types of array types that are
1251 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1253 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1254 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1255 : TYPE_MAIN_VARIANT (mvl));
1257 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1258 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1259 : TYPE_MAIN_VARIANT (mvr));
1261 enum_and_int_p = false;
1262 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1264 if (val == 1 && val_ped != 1)
1265 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1266 "are incompatible in ISO C");
1268 if (val == 2)
1269 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1271 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1272 warning_at (location, OPT_Wc___compat,
1273 "pointer target types incompatible in C++");
1275 return val;
1278 /* Subroutines of `comptypes'. */
1280 /* Determine whether two trees derive from the same translation unit.
1281 If the CONTEXT chain ends in a null, that tree's context is still
1282 being parsed, so if two trees have context chains ending in null,
1283 they're in the same translation unit. */
1285 same_translation_unit_p (const_tree t1, const_tree t2)
1287 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1288 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1290 case tcc_declaration:
1291 t1 = DECL_CONTEXT (t1); break;
1292 case tcc_type:
1293 t1 = TYPE_CONTEXT (t1); break;
1294 case tcc_exceptional:
1295 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1296 default: gcc_unreachable ();
1299 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1300 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1302 case tcc_declaration:
1303 t2 = DECL_CONTEXT (t2); break;
1304 case tcc_type:
1305 t2 = TYPE_CONTEXT (t2); break;
1306 case tcc_exceptional:
1307 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1308 default: gcc_unreachable ();
1311 return t1 == t2;
1314 /* Allocate the seen two types, assuming that they are compatible. */
1316 static struct tagged_tu_seen_cache *
1317 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1319 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1320 tu->next = tagged_tu_seen_base;
1321 tu->t1 = t1;
1322 tu->t2 = t2;
1324 tagged_tu_seen_base = tu;
1326 /* The C standard says that two structures in different translation
1327 units are compatible with each other only if the types of their
1328 fields are compatible (among other things). We assume that they
1329 are compatible until proven otherwise when building the cache.
1330 An example where this can occur is:
1331 struct a
1333 struct a *next;
1335 If we are comparing this against a similar struct in another TU,
1336 and did not assume they were compatible, we end up with an infinite
1337 loop. */
1338 tu->val = 1;
1339 return tu;
1342 /* Free the seen types until we get to TU_TIL. */
1344 static void
1345 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1347 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1348 while (tu != tu_til)
1350 const struct tagged_tu_seen_cache *const tu1
1351 = (const struct tagged_tu_seen_cache *) tu;
1352 tu = tu1->next;
1353 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1355 tagged_tu_seen_base = tu_til;
1358 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1359 compatible. If the two types are not the same (which has been
1360 checked earlier), this can only happen when multiple translation
1361 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1362 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1363 comptypes_internal. */
1365 static int
1366 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1367 bool *enum_and_int_p, bool *different_types_p)
1369 tree s1, s2;
1370 bool needs_warning = false;
1372 /* We have to verify that the tags of the types are the same. This
1373 is harder than it looks because this may be a typedef, so we have
1374 to go look at the original type. It may even be a typedef of a
1375 typedef...
1376 In the case of compiler-created builtin structs the TYPE_DECL
1377 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1378 while (TYPE_NAME (t1)
1379 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1380 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1381 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1383 while (TYPE_NAME (t2)
1384 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1385 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1386 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1388 /* C90 didn't have the requirement that the two tags be the same. */
1389 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1390 return 0;
1392 /* C90 didn't say what happened if one or both of the types were
1393 incomplete; we choose to follow C99 rules here, which is that they
1394 are compatible. */
1395 if (TYPE_SIZE (t1) == NULL
1396 || TYPE_SIZE (t2) == NULL)
1397 return 1;
1400 const struct tagged_tu_seen_cache * tts_i;
1401 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1402 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1403 return tts_i->val;
1406 switch (TREE_CODE (t1))
1408 case ENUMERAL_TYPE:
1410 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1411 /* Speed up the case where the type values are in the same order. */
1412 tree tv1 = TYPE_VALUES (t1);
1413 tree tv2 = TYPE_VALUES (t2);
1415 if (tv1 == tv2)
1417 return 1;
1420 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1422 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1423 break;
1424 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1426 tu->val = 0;
1427 return 0;
1431 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1433 return 1;
1435 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1437 tu->val = 0;
1438 return 0;
1441 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1443 tu->val = 0;
1444 return 0;
1447 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1449 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1450 if (s2 == NULL
1451 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1453 tu->val = 0;
1454 return 0;
1457 return 1;
1460 case UNION_TYPE:
1462 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1463 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1465 tu->val = 0;
1466 return 0;
1469 /* Speed up the common case where the fields are in the same order. */
1470 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1471 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1473 int result;
1475 if (DECL_NAME (s1) != DECL_NAME (s2))
1476 break;
1477 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1478 enum_and_int_p, different_types_p);
1480 if (result != 1 && !DECL_NAME (s1))
1481 break;
1482 if (result == 0)
1484 tu->val = 0;
1485 return 0;
1487 if (result == 2)
1488 needs_warning = true;
1490 if (TREE_CODE (s1) == FIELD_DECL
1491 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1492 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1494 tu->val = 0;
1495 return 0;
1498 if (!s1 && !s2)
1500 tu->val = needs_warning ? 2 : 1;
1501 return tu->val;
1504 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1506 bool ok = false;
1508 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1509 if (DECL_NAME (s1) == DECL_NAME (s2))
1511 int result;
1513 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1514 enum_and_int_p,
1515 different_types_p);
1517 if (result != 1 && !DECL_NAME (s1))
1518 continue;
1519 if (result == 0)
1521 tu->val = 0;
1522 return 0;
1524 if (result == 2)
1525 needs_warning = true;
1527 if (TREE_CODE (s1) == FIELD_DECL
1528 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1529 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1530 break;
1532 ok = true;
1533 break;
1535 if (!ok)
1537 tu->val = 0;
1538 return 0;
1541 tu->val = needs_warning ? 2 : 10;
1542 return tu->val;
1545 case RECORD_TYPE:
1547 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1549 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1550 s1 && s2;
1551 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1553 int result;
1554 if (TREE_CODE (s1) != TREE_CODE (s2)
1555 || DECL_NAME (s1) != DECL_NAME (s2))
1556 break;
1557 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1558 enum_and_int_p, different_types_p);
1559 if (result == 0)
1560 break;
1561 if (result == 2)
1562 needs_warning = true;
1564 if (TREE_CODE (s1) == FIELD_DECL
1565 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1566 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1567 break;
1569 if (s1 && s2)
1570 tu->val = 0;
1571 else
1572 tu->val = needs_warning ? 2 : 1;
1573 return tu->val;
1576 default:
1577 gcc_unreachable ();
1581 /* Return 1 if two function types F1 and F2 are compatible.
1582 If either type specifies no argument types,
1583 the other must specify a fixed number of self-promoting arg types.
1584 Otherwise, if one type specifies only the number of arguments,
1585 the other must specify that number of self-promoting arg types.
1586 Otherwise, the argument types must match.
1587 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1589 static int
1590 function_types_compatible_p (const_tree f1, const_tree f2,
1591 bool *enum_and_int_p, bool *different_types_p)
1593 tree args1, args2;
1594 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1595 int val = 1;
1596 int val1;
1597 tree ret1, ret2;
1599 ret1 = TREE_TYPE (f1);
1600 ret2 = TREE_TYPE (f2);
1602 /* 'volatile' qualifiers on a function's return type used to mean
1603 the function is noreturn. */
1604 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1605 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1606 if (TYPE_VOLATILE (ret1))
1607 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1608 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1609 if (TYPE_VOLATILE (ret2))
1610 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1611 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1612 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1613 if (val == 0)
1614 return 0;
1616 args1 = TYPE_ARG_TYPES (f1);
1617 args2 = TYPE_ARG_TYPES (f2);
1619 if (different_types_p != NULL
1620 && (args1 == 0) != (args2 == 0))
1621 *different_types_p = true;
1623 /* An unspecified parmlist matches any specified parmlist
1624 whose argument types don't need default promotions. */
1626 if (args1 == 0)
1628 if (!self_promoting_args_p (args2))
1629 return 0;
1630 /* If one of these types comes from a non-prototype fn definition,
1631 compare that with the other type's arglist.
1632 If they don't match, ask for a warning (but no error). */
1633 if (TYPE_ACTUAL_ARG_TYPES (f1)
1634 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1635 enum_and_int_p, different_types_p))
1636 val = 2;
1637 return val;
1639 if (args2 == 0)
1641 if (!self_promoting_args_p (args1))
1642 return 0;
1643 if (TYPE_ACTUAL_ARG_TYPES (f2)
1644 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1645 enum_and_int_p, different_types_p))
1646 val = 2;
1647 return val;
1650 /* Both types have argument lists: compare them and propagate results. */
1651 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1652 different_types_p);
1653 return val1 != 1 ? val1 : val;
1656 /* Check two lists of types for compatibility, returning 0 for
1657 incompatible, 1 for compatible, or 2 for compatible with
1658 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1659 comptypes_internal. */
1661 static int
1662 type_lists_compatible_p (const_tree args1, const_tree args2,
1663 bool *enum_and_int_p, bool *different_types_p)
1665 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1666 int val = 1;
1667 int newval = 0;
1669 while (1)
1671 tree a1, mv1, a2, mv2;
1672 if (args1 == 0 && args2 == 0)
1673 return val;
1674 /* If one list is shorter than the other,
1675 they fail to match. */
1676 if (args1 == 0 || args2 == 0)
1677 return 0;
1678 mv1 = a1 = TREE_VALUE (args1);
1679 mv2 = a2 = TREE_VALUE (args2);
1680 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1681 mv1 = (TYPE_ATOMIC (mv1)
1682 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1683 TYPE_QUAL_ATOMIC)
1684 : TYPE_MAIN_VARIANT (mv1));
1685 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1686 mv2 = (TYPE_ATOMIC (mv2)
1687 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1688 TYPE_QUAL_ATOMIC)
1689 : TYPE_MAIN_VARIANT (mv2));
1690 /* A null pointer instead of a type
1691 means there is supposed to be an argument
1692 but nothing is specified about what type it has.
1693 So match anything that self-promotes. */
1694 if (different_types_p != NULL
1695 && (a1 == 0) != (a2 == 0))
1696 *different_types_p = true;
1697 if (a1 == 0)
1699 if (c_type_promotes_to (a2) != a2)
1700 return 0;
1702 else if (a2 == 0)
1704 if (c_type_promotes_to (a1) != a1)
1705 return 0;
1707 /* If one of the lists has an error marker, ignore this arg. */
1708 else if (TREE_CODE (a1) == ERROR_MARK
1709 || TREE_CODE (a2) == ERROR_MARK)
1711 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1712 different_types_p)))
1714 if (different_types_p != NULL)
1715 *different_types_p = true;
1716 /* Allow wait (union {union wait *u; int *i} *)
1717 and wait (union wait *) to be compatible. */
1718 if (TREE_CODE (a1) == UNION_TYPE
1719 && (TYPE_NAME (a1) == 0
1720 || TYPE_TRANSPARENT_AGGR (a1))
1721 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1722 && tree_int_cst_equal (TYPE_SIZE (a1),
1723 TYPE_SIZE (a2)))
1725 tree memb;
1726 for (memb = TYPE_FIELDS (a1);
1727 memb; memb = DECL_CHAIN (memb))
1729 tree mv3 = TREE_TYPE (memb);
1730 if (mv3 && mv3 != error_mark_node
1731 && TREE_CODE (mv3) != ARRAY_TYPE)
1732 mv3 = (TYPE_ATOMIC (mv3)
1733 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1734 TYPE_QUAL_ATOMIC)
1735 : TYPE_MAIN_VARIANT (mv3));
1736 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1737 different_types_p))
1738 break;
1740 if (memb == 0)
1741 return 0;
1743 else if (TREE_CODE (a2) == UNION_TYPE
1744 && (TYPE_NAME (a2) == 0
1745 || TYPE_TRANSPARENT_AGGR (a2))
1746 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1747 && tree_int_cst_equal (TYPE_SIZE (a2),
1748 TYPE_SIZE (a1)))
1750 tree memb;
1751 for (memb = TYPE_FIELDS (a2);
1752 memb; memb = DECL_CHAIN (memb))
1754 tree mv3 = TREE_TYPE (memb);
1755 if (mv3 && mv3 != error_mark_node
1756 && TREE_CODE (mv3) != ARRAY_TYPE)
1757 mv3 = (TYPE_ATOMIC (mv3)
1758 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1759 TYPE_QUAL_ATOMIC)
1760 : TYPE_MAIN_VARIANT (mv3));
1761 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1762 different_types_p))
1763 break;
1765 if (memb == 0)
1766 return 0;
1768 else
1769 return 0;
1772 /* comptypes said ok, but record if it said to warn. */
1773 if (newval > val)
1774 val = newval;
1776 args1 = TREE_CHAIN (args1);
1777 args2 = TREE_CHAIN (args2);
1781 /* Compute the size to increment a pointer by. When a function type or void
1782 type or incomplete type is passed, size_one_node is returned.
1783 This function does not emit any diagnostics; the caller is responsible
1784 for that. */
1786 static tree
1787 c_size_in_bytes (const_tree type)
1789 enum tree_code code = TREE_CODE (type);
1791 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1792 || !COMPLETE_TYPE_P (type))
1793 return size_one_node;
1795 /* Convert in case a char is more than one unit. */
1796 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1797 size_int (TYPE_PRECISION (char_type_node)
1798 / BITS_PER_UNIT));
1801 /* Return either DECL or its known constant value (if it has one). */
1803 tree
1804 decl_constant_value (tree decl)
1806 if (/* Don't change a variable array bound or initial value to a constant
1807 in a place where a variable is invalid. Note that DECL_INITIAL
1808 isn't valid for a PARM_DECL. */
1809 current_function_decl != 0
1810 && TREE_CODE (decl) != PARM_DECL
1811 && !TREE_THIS_VOLATILE (decl)
1812 && TREE_READONLY (decl)
1813 && DECL_INITIAL (decl) != 0
1814 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1815 /* This is invalid if initial value is not constant.
1816 If it has either a function call, a memory reference,
1817 or a variable, then re-evaluating it could give different results. */
1818 && TREE_CONSTANT (DECL_INITIAL (decl))
1819 /* Check for cases where this is sub-optimal, even though valid. */
1820 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1821 return DECL_INITIAL (decl);
1822 return decl;
1825 /* Convert the array expression EXP to a pointer. */
1826 static tree
1827 array_to_pointer_conversion (location_t loc, tree exp)
1829 tree orig_exp = exp;
1830 tree type = TREE_TYPE (exp);
1831 tree adr;
1832 tree restype = TREE_TYPE (type);
1833 tree ptrtype;
1835 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1837 STRIP_TYPE_NOPS (exp);
1839 if (TREE_NO_WARNING (orig_exp))
1840 TREE_NO_WARNING (exp) = 1;
1842 ptrtype = build_pointer_type (restype);
1844 if (TREE_CODE (exp) == INDIRECT_REF)
1845 return convert (ptrtype, TREE_OPERAND (exp, 0));
1847 /* In C++ array compound literals are temporary objects unless they are
1848 const or appear in namespace scope, so they are destroyed too soon
1849 to use them for much of anything (c++/53220). */
1850 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1852 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1853 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1854 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1855 "converting an array compound literal to a pointer "
1856 "is ill-formed in C++");
1859 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1860 return convert (ptrtype, adr);
1863 /* Convert the function expression EXP to a pointer. */
1864 static tree
1865 function_to_pointer_conversion (location_t loc, tree exp)
1867 tree orig_exp = exp;
1869 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1871 STRIP_TYPE_NOPS (exp);
1873 if (TREE_NO_WARNING (orig_exp))
1874 TREE_NO_WARNING (exp) = 1;
1876 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1879 /* Mark EXP as read, not just set, for set but not used -Wunused
1880 warning purposes. */
1882 void
1883 mark_exp_read (tree exp)
1885 switch (TREE_CODE (exp))
1887 case VAR_DECL:
1888 case PARM_DECL:
1889 DECL_READ_P (exp) = 1;
1890 break;
1891 case ARRAY_REF:
1892 case COMPONENT_REF:
1893 case MODIFY_EXPR:
1894 case REALPART_EXPR:
1895 case IMAGPART_EXPR:
1896 CASE_CONVERT:
1897 case ADDR_EXPR:
1898 mark_exp_read (TREE_OPERAND (exp, 0));
1899 break;
1900 case COMPOUND_EXPR:
1901 case C_MAYBE_CONST_EXPR:
1902 mark_exp_read (TREE_OPERAND (exp, 1));
1903 break;
1904 default:
1905 break;
1909 /* Perform the default conversion of arrays and functions to pointers.
1910 Return the result of converting EXP. For any other expression, just
1911 return EXP.
1913 LOC is the location of the expression. */
1915 struct c_expr
1916 default_function_array_conversion (location_t loc, struct c_expr exp)
1918 tree orig_exp = exp.value;
1919 tree type = TREE_TYPE (exp.value);
1920 enum tree_code code = TREE_CODE (type);
1922 switch (code)
1924 case ARRAY_TYPE:
1926 bool not_lvalue = false;
1927 bool lvalue_array_p;
1929 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1930 || CONVERT_EXPR_P (exp.value))
1931 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1933 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1934 not_lvalue = true;
1935 exp.value = TREE_OPERAND (exp.value, 0);
1938 if (TREE_NO_WARNING (orig_exp))
1939 TREE_NO_WARNING (exp.value) = 1;
1941 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1942 if (!flag_isoc99 && !lvalue_array_p)
1944 /* Before C99, non-lvalue arrays do not decay to pointers.
1945 Normally, using such an array would be invalid; but it can
1946 be used correctly inside sizeof or as a statement expression.
1947 Thus, do not give an error here; an error will result later. */
1948 return exp;
1951 exp.value = array_to_pointer_conversion (loc, exp.value);
1953 break;
1954 case FUNCTION_TYPE:
1955 exp.value = function_to_pointer_conversion (loc, exp.value);
1956 break;
1957 default:
1958 break;
1961 return exp;
1964 struct c_expr
1965 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1967 mark_exp_read (exp.value);
1968 return default_function_array_conversion (loc, exp);
1971 /* Return whether EXPR should be treated as an atomic lvalue for the
1972 purposes of load and store handling. */
1974 static bool
1975 really_atomic_lvalue (tree expr)
1977 if (error_operand_p (expr))
1978 return false;
1979 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1980 return false;
1981 if (!lvalue_p (expr))
1982 return false;
1984 /* Ignore _Atomic on register variables, since their addresses can't
1985 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1986 sequences wouldn't work. Ignore _Atomic on structures containing
1987 bit-fields, since accessing elements of atomic structures or
1988 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1989 it's undefined at translation time or execution time, and the
1990 normal atomic sequences again wouldn't work. */
1991 while (handled_component_p (expr))
1993 if (TREE_CODE (expr) == COMPONENT_REF
1994 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1995 return false;
1996 expr = TREE_OPERAND (expr, 0);
1998 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1999 return false;
2000 return true;
2003 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2004 including converting functions and arrays to pointers if CONVERT_P.
2005 If READ_P, also mark the expression as having been read. */
2007 struct c_expr
2008 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2009 bool convert_p, bool read_p)
2011 if (read_p)
2012 mark_exp_read (exp.value);
2013 if (convert_p)
2014 exp = default_function_array_conversion (loc, exp);
2015 if (really_atomic_lvalue (exp.value))
2017 vec<tree, va_gc> *params;
2018 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2019 tree expr_type = TREE_TYPE (exp.value);
2020 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2021 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2023 gcc_assert (TYPE_ATOMIC (expr_type));
2025 /* Expansion of a generic atomic load may require an addition
2026 element, so allocate enough to prevent a resize. */
2027 vec_alloc (params, 4);
2029 /* Remove the qualifiers for the rest of the expressions and
2030 create the VAL temp variable to hold the RHS. */
2031 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2032 tmp = create_tmp_var_raw (nonatomic_type);
2033 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2034 TREE_ADDRESSABLE (tmp) = 1;
2035 TREE_NO_WARNING (tmp) = 1;
2037 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2038 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2039 params->quick_push (expr_addr);
2040 params->quick_push (tmp_addr);
2041 params->quick_push (seq_cst);
2042 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2044 /* EXPR is always read. */
2045 mark_exp_read (exp.value);
2047 /* Return tmp which contains the value loaded. */
2048 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2049 NULL_TREE, NULL_TREE);
2051 return exp;
2054 /* EXP is an expression of integer type. Apply the integer promotions
2055 to it and return the promoted value. */
2057 tree
2058 perform_integral_promotions (tree exp)
2060 tree type = TREE_TYPE (exp);
2061 enum tree_code code = TREE_CODE (type);
2063 gcc_assert (INTEGRAL_TYPE_P (type));
2065 /* Normally convert enums to int,
2066 but convert wide enums to something wider. */
2067 if (code == ENUMERAL_TYPE)
2069 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2070 TYPE_PRECISION (integer_type_node)),
2071 ((TYPE_PRECISION (type)
2072 >= TYPE_PRECISION (integer_type_node))
2073 && TYPE_UNSIGNED (type)));
2075 return convert (type, exp);
2078 /* ??? This should no longer be needed now bit-fields have their
2079 proper types. */
2080 if (TREE_CODE (exp) == COMPONENT_REF
2081 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2082 /* If it's thinner than an int, promote it like a
2083 c_promoting_integer_type_p, otherwise leave it alone. */
2084 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2085 TYPE_PRECISION (integer_type_node)))
2086 return convert (integer_type_node, exp);
2088 if (c_promoting_integer_type_p (type))
2090 /* Preserve unsignedness if not really getting any wider. */
2091 if (TYPE_UNSIGNED (type)
2092 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2093 return convert (unsigned_type_node, exp);
2095 return convert (integer_type_node, exp);
2098 return exp;
2102 /* Perform default promotions for C data used in expressions.
2103 Enumeral types or short or char are converted to int.
2104 In addition, manifest constants symbols are replaced by their values. */
2106 tree
2107 default_conversion (tree exp)
2109 tree orig_exp;
2110 tree type = TREE_TYPE (exp);
2111 enum tree_code code = TREE_CODE (type);
2112 tree promoted_type;
2114 mark_exp_read (exp);
2116 /* Functions and arrays have been converted during parsing. */
2117 gcc_assert (code != FUNCTION_TYPE);
2118 if (code == ARRAY_TYPE)
2119 return exp;
2121 /* Constants can be used directly unless they're not loadable. */
2122 if (TREE_CODE (exp) == CONST_DECL)
2123 exp = DECL_INITIAL (exp);
2125 /* Strip no-op conversions. */
2126 orig_exp = exp;
2127 STRIP_TYPE_NOPS (exp);
2129 if (TREE_NO_WARNING (orig_exp))
2130 TREE_NO_WARNING (exp) = 1;
2132 if (code == VOID_TYPE)
2134 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2135 "void value not ignored as it ought to be");
2136 return error_mark_node;
2139 exp = require_complete_type (exp);
2140 if (exp == error_mark_node)
2141 return error_mark_node;
2143 promoted_type = targetm.promoted_type (type);
2144 if (promoted_type)
2145 return convert (promoted_type, exp);
2147 if (INTEGRAL_TYPE_P (type))
2148 return perform_integral_promotions (exp);
2150 return exp;
2153 /* Look up COMPONENT in a structure or union TYPE.
2155 If the component name is not found, returns NULL_TREE. Otherwise,
2156 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2157 stepping down the chain to the component, which is in the last
2158 TREE_VALUE of the list. Normally the list is of length one, but if
2159 the component is embedded within (nested) anonymous structures or
2160 unions, the list steps down the chain to the component. */
2162 static tree
2163 lookup_field (tree type, tree component)
2165 tree field;
2167 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2168 to the field elements. Use a binary search on this array to quickly
2169 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2170 will always be set for structures which have many elements. */
2172 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2174 int bot, top, half;
2175 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2177 field = TYPE_FIELDS (type);
2178 bot = 0;
2179 top = TYPE_LANG_SPECIFIC (type)->s->len;
2180 while (top - bot > 1)
2182 half = (top - bot + 1) >> 1;
2183 field = field_array[bot+half];
2185 if (DECL_NAME (field) == NULL_TREE)
2187 /* Step through all anon unions in linear fashion. */
2188 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2190 field = field_array[bot++];
2191 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2192 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2194 tree anon = lookup_field (TREE_TYPE (field), component);
2196 if (anon)
2197 return tree_cons (NULL_TREE, field, anon);
2199 /* The Plan 9 compiler permits referring
2200 directly to an anonymous struct/union field
2201 using a typedef name. */
2202 if (flag_plan9_extensions
2203 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2204 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2205 == TYPE_DECL)
2206 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2207 == component))
2208 break;
2212 /* Entire record is only anon unions. */
2213 if (bot > top)
2214 return NULL_TREE;
2216 /* Restart the binary search, with new lower bound. */
2217 continue;
2220 if (DECL_NAME (field) == component)
2221 break;
2222 if (DECL_NAME (field) < component)
2223 bot += half;
2224 else
2225 top = bot + half;
2228 if (DECL_NAME (field_array[bot]) == component)
2229 field = field_array[bot];
2230 else if (DECL_NAME (field) != component)
2231 return NULL_TREE;
2233 else
2235 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2237 if (DECL_NAME (field) == NULL_TREE
2238 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2239 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2241 tree anon = lookup_field (TREE_TYPE (field), component);
2243 if (anon)
2244 return tree_cons (NULL_TREE, field, anon);
2246 /* The Plan 9 compiler permits referring directly to an
2247 anonymous struct/union field using a typedef
2248 name. */
2249 if (flag_plan9_extensions
2250 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2251 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2252 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2253 == component))
2254 break;
2257 if (DECL_NAME (field) == component)
2258 break;
2261 if (field == NULL_TREE)
2262 return NULL_TREE;
2265 return tree_cons (NULL_TREE, field, NULL_TREE);
2268 /* Make an expression to refer to the COMPONENT field of structure or
2269 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2270 location of the COMPONENT_REF. */
2272 tree
2273 build_component_ref (location_t loc, tree datum, tree component)
2275 tree type = TREE_TYPE (datum);
2276 enum tree_code code = TREE_CODE (type);
2277 tree field = NULL;
2278 tree ref;
2279 bool datum_lvalue = lvalue_p (datum);
2281 if (!objc_is_public (datum, component))
2282 return error_mark_node;
2284 /* Detect Objective-C property syntax object.property. */
2285 if (c_dialect_objc ()
2286 && (ref = objc_maybe_build_component_ref (datum, component)))
2287 return ref;
2289 /* See if there is a field or component with name COMPONENT. */
2291 if (code == RECORD_TYPE || code == UNION_TYPE)
2293 if (!COMPLETE_TYPE_P (type))
2295 c_incomplete_type_error (NULL_TREE, type);
2296 return error_mark_node;
2299 field = lookup_field (type, component);
2301 if (!field)
2303 error_at (loc, "%qT has no member named %qE", type, component);
2304 return error_mark_node;
2307 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2308 This might be better solved in future the way the C++ front
2309 end does it - by giving the anonymous entities each a
2310 separate name and type, and then have build_component_ref
2311 recursively call itself. We can't do that here. */
2314 tree subdatum = TREE_VALUE (field);
2315 int quals;
2316 tree subtype;
2317 bool use_datum_quals;
2319 if (TREE_TYPE (subdatum) == error_mark_node)
2320 return error_mark_node;
2322 /* If this is an rvalue, it does not have qualifiers in C
2323 standard terms and we must avoid propagating such
2324 qualifiers down to a non-lvalue array that is then
2325 converted to a pointer. */
2326 use_datum_quals = (datum_lvalue
2327 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2329 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2330 if (use_datum_quals)
2331 quals |= TYPE_QUALS (TREE_TYPE (datum));
2332 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2334 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2335 NULL_TREE);
2336 SET_EXPR_LOCATION (ref, loc);
2337 if (TREE_READONLY (subdatum)
2338 || (use_datum_quals && TREE_READONLY (datum)))
2339 TREE_READONLY (ref) = 1;
2340 if (TREE_THIS_VOLATILE (subdatum)
2341 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2342 TREE_THIS_VOLATILE (ref) = 1;
2344 if (TREE_DEPRECATED (subdatum))
2345 warn_deprecated_use (subdatum, NULL_TREE);
2347 datum = ref;
2349 field = TREE_CHAIN (field);
2351 while (field);
2353 return ref;
2355 else if (code != ERROR_MARK)
2356 error_at (loc,
2357 "request for member %qE in something not a structure or union",
2358 component);
2360 return error_mark_node;
2363 /* Given an expression PTR for a pointer, return an expression
2364 for the value pointed to.
2365 ERRORSTRING is the name of the operator to appear in error messages.
2367 LOC is the location to use for the generated tree. */
2369 tree
2370 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2372 tree pointer = default_conversion (ptr);
2373 tree type = TREE_TYPE (pointer);
2374 tree ref;
2376 if (TREE_CODE (type) == POINTER_TYPE)
2378 if (CONVERT_EXPR_P (pointer)
2379 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2381 /* If a warning is issued, mark it to avoid duplicates from
2382 the backend. This only needs to be done at
2383 warn_strict_aliasing > 2. */
2384 if (warn_strict_aliasing > 2)
2385 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2386 type, TREE_OPERAND (pointer, 0)))
2387 TREE_NO_WARNING (pointer) = 1;
2390 if (TREE_CODE (pointer) == ADDR_EXPR
2391 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2392 == TREE_TYPE (type)))
2394 ref = TREE_OPERAND (pointer, 0);
2395 protected_set_expr_location (ref, loc);
2396 return ref;
2398 else
2400 tree t = TREE_TYPE (type);
2402 ref = build1 (INDIRECT_REF, t, pointer);
2404 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2406 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2408 error_at (loc, "dereferencing pointer to incomplete type "
2409 "%qT", t);
2410 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2412 return error_mark_node;
2414 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2415 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2417 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2418 so that we get the proper error message if the result is used
2419 to assign to. Also, &* is supposed to be a no-op.
2420 And ANSI C seems to specify that the type of the result
2421 should be the const type. */
2422 /* A de-reference of a pointer to const is not a const. It is valid
2423 to change it via some other pointer. */
2424 TREE_READONLY (ref) = TYPE_READONLY (t);
2425 TREE_SIDE_EFFECTS (ref)
2426 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2427 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2428 protected_set_expr_location (ref, loc);
2429 return ref;
2432 else if (TREE_CODE (pointer) != ERROR_MARK)
2433 invalid_indirection_error (loc, type, errstring);
2435 return error_mark_node;
2438 /* This handles expressions of the form "a[i]", which denotes
2439 an array reference.
2441 This is logically equivalent in C to *(a+i), but we may do it differently.
2442 If A is a variable or a member, we generate a primitive ARRAY_REF.
2443 This avoids forcing the array out of registers, and can work on
2444 arrays that are not lvalues (for example, members of structures returned
2445 by functions).
2447 For vector types, allow vector[i] but not i[vector], and create
2448 *(((type*)&vectortype) + i) for the expression.
2450 LOC is the location to use for the returned expression. */
2452 tree
2453 build_array_ref (location_t loc, tree array, tree index)
2455 tree ret;
2456 bool swapped = false;
2457 if (TREE_TYPE (array) == error_mark_node
2458 || TREE_TYPE (index) == error_mark_node)
2459 return error_mark_node;
2461 if (flag_cilkplus && contains_array_notation_expr (index))
2463 size_t rank = 0;
2464 if (!find_rank (loc, index, index, true, &rank))
2465 return error_mark_node;
2466 if (rank > 1)
2468 error_at (loc, "rank of the array's index is greater than 1");
2469 return error_mark_node;
2472 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2473 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2474 /* Allow vector[index] but not index[vector]. */
2475 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2477 tree temp;
2478 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2479 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2481 error_at (loc,
2482 "subscripted value is neither array nor pointer nor vector");
2484 return error_mark_node;
2486 temp = array;
2487 array = index;
2488 index = temp;
2489 swapped = true;
2492 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2494 error_at (loc, "array subscript is not an integer");
2495 return error_mark_node;
2498 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2500 error_at (loc, "subscripted value is pointer to function");
2501 return error_mark_node;
2504 /* ??? Existing practice has been to warn only when the char
2505 index is syntactically the index, not for char[array]. */
2506 if (!swapped)
2507 warn_array_subscript_with_type_char (loc, index);
2509 /* Apply default promotions *after* noticing character types. */
2510 index = default_conversion (index);
2511 if (index == error_mark_node)
2512 return error_mark_node;
2514 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2516 bool non_lvalue
2517 = convert_vector_to_pointer_for_subscript (loc, &array, index);
2519 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2521 tree rval, type;
2523 /* An array that is indexed by a non-constant
2524 cannot be stored in a register; we must be able to do
2525 address arithmetic on its address.
2526 Likewise an array of elements of variable size. */
2527 if (TREE_CODE (index) != INTEGER_CST
2528 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2529 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2531 if (!c_mark_addressable (array))
2532 return error_mark_node;
2534 /* An array that is indexed by a constant value which is not within
2535 the array bounds cannot be stored in a register either; because we
2536 would get a crash in store_bit_field/extract_bit_field when trying
2537 to access a non-existent part of the register. */
2538 if (TREE_CODE (index) == INTEGER_CST
2539 && TYPE_DOMAIN (TREE_TYPE (array))
2540 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2542 if (!c_mark_addressable (array))
2543 return error_mark_node;
2546 if (pedantic || warn_c90_c99_compat)
2548 tree foo = array;
2549 while (TREE_CODE (foo) == COMPONENT_REF)
2550 foo = TREE_OPERAND (foo, 0);
2551 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2552 pedwarn (loc, OPT_Wpedantic,
2553 "ISO C forbids subscripting %<register%> array");
2554 else if (!lvalue_p (foo))
2555 pedwarn_c90 (loc, OPT_Wpedantic,
2556 "ISO C90 forbids subscripting non-lvalue "
2557 "array");
2560 type = TREE_TYPE (TREE_TYPE (array));
2561 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2562 /* Array ref is const/volatile if the array elements are
2563 or if the array is. */
2564 TREE_READONLY (rval)
2565 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2566 | TREE_READONLY (array));
2567 TREE_SIDE_EFFECTS (rval)
2568 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2569 | TREE_SIDE_EFFECTS (array));
2570 TREE_THIS_VOLATILE (rval)
2571 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2572 /* This was added by rms on 16 Nov 91.
2573 It fixes vol struct foo *a; a->elts[1]
2574 in an inline function.
2575 Hope it doesn't break something else. */
2576 | TREE_THIS_VOLATILE (array));
2577 ret = require_complete_type (rval);
2578 protected_set_expr_location (ret, loc);
2579 if (non_lvalue)
2580 ret = non_lvalue_loc (loc, ret);
2581 return ret;
2583 else
2585 tree ar = default_conversion (array);
2587 if (ar == error_mark_node)
2588 return ar;
2590 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2591 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2593 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2594 index, 0),
2595 RO_ARRAY_INDEXING);
2596 if (non_lvalue)
2597 ret = non_lvalue_loc (loc, ret);
2598 return ret;
2602 /* Build an external reference to identifier ID. FUN indicates
2603 whether this will be used for a function call. LOC is the source
2604 location of the identifier. This sets *TYPE to the type of the
2605 identifier, which is not the same as the type of the returned value
2606 for CONST_DECLs defined as enum constants. If the type of the
2607 identifier is not available, *TYPE is set to NULL. */
2608 tree
2609 build_external_ref (location_t loc, tree id, int fun, tree *type)
2611 tree ref;
2612 tree decl = lookup_name (id);
2614 /* In Objective-C, an instance variable (ivar) may be preferred to
2615 whatever lookup_name() found. */
2616 decl = objc_lookup_ivar (decl, id);
2618 *type = NULL;
2619 if (decl && decl != error_mark_node)
2621 ref = decl;
2622 *type = TREE_TYPE (ref);
2624 else if (fun)
2625 /* Implicit function declaration. */
2626 ref = implicitly_declare (loc, id);
2627 else if (decl == error_mark_node)
2628 /* Don't complain about something that's already been
2629 complained about. */
2630 return error_mark_node;
2631 else
2633 undeclared_variable (loc, id);
2634 return error_mark_node;
2637 if (TREE_TYPE (ref) == error_mark_node)
2638 return error_mark_node;
2640 if (TREE_DEPRECATED (ref))
2641 warn_deprecated_use (ref, NULL_TREE);
2643 /* Recursive call does not count as usage. */
2644 if (ref != current_function_decl)
2646 TREE_USED (ref) = 1;
2649 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2651 if (!in_sizeof && !in_typeof)
2652 C_DECL_USED (ref) = 1;
2653 else if (DECL_INITIAL (ref) == 0
2654 && DECL_EXTERNAL (ref)
2655 && !TREE_PUBLIC (ref))
2656 record_maybe_used_decl (ref);
2659 if (TREE_CODE (ref) == CONST_DECL)
2661 used_types_insert (TREE_TYPE (ref));
2663 if (warn_cxx_compat
2664 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2665 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2667 warning_at (loc, OPT_Wc___compat,
2668 ("enum constant defined in struct or union "
2669 "is not visible in C++"));
2670 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2673 ref = DECL_INITIAL (ref);
2674 TREE_CONSTANT (ref) = 1;
2676 else if (current_function_decl != 0
2677 && !DECL_FILE_SCOPE_P (current_function_decl)
2678 && (TREE_CODE (ref) == VAR_DECL
2679 || TREE_CODE (ref) == PARM_DECL
2680 || TREE_CODE (ref) == FUNCTION_DECL))
2682 tree context = decl_function_context (ref);
2684 if (context != 0 && context != current_function_decl)
2685 DECL_NONLOCAL (ref) = 1;
2687 /* C99 6.7.4p3: An inline definition of a function with external
2688 linkage ... shall not contain a reference to an identifier with
2689 internal linkage. */
2690 else if (current_function_decl != 0
2691 && DECL_DECLARED_INLINE_P (current_function_decl)
2692 && DECL_EXTERNAL (current_function_decl)
2693 && VAR_OR_FUNCTION_DECL_P (ref)
2694 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2695 && ! TREE_PUBLIC (ref)
2696 && DECL_CONTEXT (ref) != current_function_decl)
2697 record_inline_static (loc, current_function_decl, ref,
2698 csi_internal);
2700 return ref;
2703 /* Record details of decls possibly used inside sizeof or typeof. */
2704 struct maybe_used_decl
2706 /* The decl. */
2707 tree decl;
2708 /* The level seen at (in_sizeof + in_typeof). */
2709 int level;
2710 /* The next one at this level or above, or NULL. */
2711 struct maybe_used_decl *next;
2714 static struct maybe_used_decl *maybe_used_decls;
2716 /* Record that DECL, an undefined static function reference seen
2717 inside sizeof or typeof, might be used if the operand of sizeof is
2718 a VLA type or the operand of typeof is a variably modified
2719 type. */
2721 static void
2722 record_maybe_used_decl (tree decl)
2724 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2725 t->decl = decl;
2726 t->level = in_sizeof + in_typeof;
2727 t->next = maybe_used_decls;
2728 maybe_used_decls = t;
2731 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2732 USED is false, just discard them. If it is true, mark them used
2733 (if no longer inside sizeof or typeof) or move them to the next
2734 level up (if still inside sizeof or typeof). */
2736 void
2737 pop_maybe_used (bool used)
2739 struct maybe_used_decl *p = maybe_used_decls;
2740 int cur_level = in_sizeof + in_typeof;
2741 while (p && p->level > cur_level)
2743 if (used)
2745 if (cur_level == 0)
2746 C_DECL_USED (p->decl) = 1;
2747 else
2748 p->level = cur_level;
2750 p = p->next;
2752 if (!used || cur_level == 0)
2753 maybe_used_decls = p;
2756 /* Return the result of sizeof applied to EXPR. */
2758 struct c_expr
2759 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2761 struct c_expr ret;
2762 if (expr.value == error_mark_node)
2764 ret.value = error_mark_node;
2765 ret.original_code = ERROR_MARK;
2766 ret.original_type = NULL;
2767 pop_maybe_used (false);
2769 else
2771 bool expr_const_operands = true;
2773 if (TREE_CODE (expr.value) == PARM_DECL
2774 && C_ARRAY_PARAMETER (expr.value))
2776 if (warning_at (loc, OPT_Wsizeof_array_argument,
2777 "%<sizeof%> on array function parameter %qE will "
2778 "return size of %qT", expr.value,
2779 expr.original_type))
2780 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2782 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2783 &expr_const_operands);
2784 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2785 c_last_sizeof_arg = expr.value;
2786 ret.original_code = SIZEOF_EXPR;
2787 ret.original_type = NULL;
2788 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2790 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2791 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2792 folded_expr, ret.value);
2793 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2794 SET_EXPR_LOCATION (ret.value, loc);
2796 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2798 return ret;
2801 /* Return the result of sizeof applied to T, a structure for the type
2802 name passed to sizeof (rather than the type itself). LOC is the
2803 location of the original expression. */
2805 struct c_expr
2806 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2808 tree type;
2809 struct c_expr ret;
2810 tree type_expr = NULL_TREE;
2811 bool type_expr_const = true;
2812 type = groktypename (t, &type_expr, &type_expr_const);
2813 ret.value = c_sizeof (loc, type);
2814 c_last_sizeof_arg = type;
2815 ret.original_code = SIZEOF_EXPR;
2816 ret.original_type = NULL;
2817 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2818 && c_vla_type_p (type))
2820 /* If the type is a [*] array, it is a VLA but is represented as
2821 having a size of zero. In such a case we must ensure that
2822 the result of sizeof does not get folded to a constant by
2823 c_fully_fold, because if the size is evaluated the result is
2824 not constant and so constraints on zero or negative size
2825 arrays must not be applied when this sizeof call is inside
2826 another array declarator. */
2827 if (!type_expr)
2828 type_expr = integer_zero_node;
2829 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2830 type_expr, ret.value);
2831 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2833 pop_maybe_used (type != error_mark_node
2834 ? C_TYPE_VARIABLE_SIZE (type) : false);
2835 return ret;
2838 /* Build a function call to function FUNCTION with parameters PARAMS.
2839 The function call is at LOC.
2840 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2841 TREE_VALUE of each node is a parameter-expression.
2842 FUNCTION's data type may be a function type or a pointer-to-function. */
2844 tree
2845 build_function_call (location_t loc, tree function, tree params)
2847 vec<tree, va_gc> *v;
2848 tree ret;
2850 vec_alloc (v, list_length (params));
2851 for (; params; params = TREE_CHAIN (params))
2852 v->quick_push (TREE_VALUE (params));
2853 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2854 vec_free (v);
2855 return ret;
2858 /* Give a note about the location of the declaration of DECL. */
2860 static void inform_declaration (tree decl)
2862 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2863 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2866 /* Build a function call to function FUNCTION with parameters PARAMS.
2867 ORIGTYPES, if not NULL, is a vector of types; each element is
2868 either NULL or the original type of the corresponding element in
2869 PARAMS. The original type may differ from TREE_TYPE of the
2870 parameter for enums. FUNCTION's data type may be a function type
2871 or pointer-to-function. This function changes the elements of
2872 PARAMS. */
2874 tree
2875 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2876 tree function, vec<tree, va_gc> *params,
2877 vec<tree, va_gc> *origtypes)
2879 tree fntype, fundecl = 0;
2880 tree name = NULL_TREE, result;
2881 tree tem;
2882 int nargs;
2883 tree *argarray;
2886 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2887 STRIP_TYPE_NOPS (function);
2889 /* Convert anything with function type to a pointer-to-function. */
2890 if (TREE_CODE (function) == FUNCTION_DECL)
2892 name = DECL_NAME (function);
2894 if (flag_tm)
2895 tm_malloc_replacement (function);
2896 fundecl = function;
2897 /* Atomic functions have type checking/casting already done. They are
2898 often rewritten and don't match the original parameter list. */
2899 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2900 origtypes = NULL;
2902 if (flag_cilkplus
2903 && is_cilkplus_reduce_builtin (function))
2904 origtypes = NULL;
2906 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2907 function = function_to_pointer_conversion (loc, function);
2909 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2910 expressions, like those used for ObjC messenger dispatches. */
2911 if (params && !params->is_empty ())
2912 function = objc_rewrite_function_call (function, (*params)[0]);
2914 function = c_fully_fold (function, false, NULL);
2916 fntype = TREE_TYPE (function);
2918 if (TREE_CODE (fntype) == ERROR_MARK)
2919 return error_mark_node;
2921 if (!(TREE_CODE (fntype) == POINTER_TYPE
2922 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2924 if (!flag_diagnostics_show_caret)
2925 error_at (loc,
2926 "called object %qE is not a function or function pointer",
2927 function);
2928 else if (DECL_P (function))
2930 error_at (loc,
2931 "called object %qD is not a function or function pointer",
2932 function);
2933 inform_declaration (function);
2935 else
2936 error_at (loc,
2937 "called object is not a function or function pointer");
2938 return error_mark_node;
2941 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2942 current_function_returns_abnormally = 1;
2944 /* fntype now gets the type of function pointed to. */
2945 fntype = TREE_TYPE (fntype);
2947 /* Convert the parameters to the types declared in the
2948 function prototype, or apply default promotions. */
2950 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2951 origtypes, function, fundecl);
2952 if (nargs < 0)
2953 return error_mark_node;
2955 /* Check that the function is called through a compatible prototype.
2956 If it is not, warn. */
2957 if (CONVERT_EXPR_P (function)
2958 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2959 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2960 && !comptypes (fntype, TREE_TYPE (tem)))
2962 tree return_type = TREE_TYPE (fntype);
2964 /* This situation leads to run-time undefined behavior. We can't,
2965 therefore, simply error unless we can prove that all possible
2966 executions of the program must execute the code. */
2967 warning_at (loc, 0, "function called through a non-compatible type");
2969 if (VOID_TYPE_P (return_type)
2970 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2971 pedwarn (loc, 0,
2972 "function with qualified void return type called");
2975 argarray = vec_safe_address (params);
2977 /* Check that arguments to builtin functions match the expectations. */
2978 if (fundecl
2979 && DECL_BUILT_IN (fundecl)
2980 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2981 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2982 return error_mark_node;
2984 /* Check that the arguments to the function are valid. */
2985 check_function_arguments (fntype, nargs, argarray);
2987 if (name != NULL_TREE
2988 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2990 if (require_constant_value)
2991 result =
2992 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2993 function, nargs, argarray);
2994 else
2995 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2996 function, nargs, argarray);
2997 if (TREE_CODE (result) == NOP_EXPR
2998 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2999 STRIP_TYPE_NOPS (result);
3001 else
3002 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3003 function, nargs, argarray);
3005 if (VOID_TYPE_P (TREE_TYPE (result)))
3007 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3008 pedwarn (loc, 0,
3009 "function with qualified void return type called");
3010 return result;
3012 return require_complete_type (result);
3015 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3017 tree
3018 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3019 tree function, vec<tree, va_gc> *params,
3020 vec<tree, va_gc> *origtypes)
3022 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3023 STRIP_TYPE_NOPS (function);
3025 /* Convert anything with function type to a pointer-to-function. */
3026 if (TREE_CODE (function) == FUNCTION_DECL)
3028 /* Implement type-directed function overloading for builtins.
3029 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3030 handle all the type checking. The result is a complete expression
3031 that implements this function call. */
3032 tree tem = resolve_overloaded_builtin (loc, function, params);
3033 if (tem)
3034 return tem;
3036 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3039 /* Convert the argument expressions in the vector VALUES
3040 to the types in the list TYPELIST.
3042 If TYPELIST is exhausted, or when an element has NULL as its type,
3043 perform the default conversions.
3045 ORIGTYPES is the original types of the expressions in VALUES. This
3046 holds the type of enum values which have been converted to integral
3047 types. It may be NULL.
3049 FUNCTION is a tree for the called function. It is used only for
3050 error messages, where it is formatted with %qE.
3052 This is also where warnings about wrong number of args are generated.
3054 ARG_LOC are locations of function arguments (if any).
3056 Returns the actual number of arguments processed (which may be less
3057 than the length of VALUES in some error situations), or -1 on
3058 failure. */
3060 static int
3061 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3062 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3063 tree function, tree fundecl)
3065 tree typetail, val;
3066 unsigned int parmnum;
3067 bool error_args = false;
3068 const bool type_generic = fundecl
3069 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3070 bool type_generic_remove_excess_precision = false;
3071 tree selector;
3073 /* Change pointer to function to the function itself for
3074 diagnostics. */
3075 if (TREE_CODE (function) == ADDR_EXPR
3076 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3077 function = TREE_OPERAND (function, 0);
3079 /* Handle an ObjC selector specially for diagnostics. */
3080 selector = objc_message_selector ();
3082 /* For type-generic built-in functions, determine whether excess
3083 precision should be removed (classification) or not
3084 (comparison). */
3085 if (type_generic
3086 && DECL_BUILT_IN (fundecl)
3087 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3089 switch (DECL_FUNCTION_CODE (fundecl))
3091 case BUILT_IN_ISFINITE:
3092 case BUILT_IN_ISINF:
3093 case BUILT_IN_ISINF_SIGN:
3094 case BUILT_IN_ISNAN:
3095 case BUILT_IN_ISNORMAL:
3096 case BUILT_IN_FPCLASSIFY:
3097 type_generic_remove_excess_precision = true;
3098 break;
3100 default:
3101 type_generic_remove_excess_precision = false;
3102 break;
3105 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3106 return vec_safe_length (values);
3108 /* Scan the given expressions and types, producing individual
3109 converted arguments. */
3111 for (typetail = typelist, parmnum = 0;
3112 values && values->iterate (parmnum, &val);
3113 ++parmnum)
3115 tree type = typetail ? TREE_VALUE (typetail) : 0;
3116 tree valtype = TREE_TYPE (val);
3117 tree rname = function;
3118 int argnum = parmnum + 1;
3119 const char *invalid_func_diag;
3120 bool excess_precision = false;
3121 bool npc;
3122 tree parmval;
3123 /* Some __atomic_* builtins have additional hidden argument at
3124 position 0. */
3125 location_t ploc
3126 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3127 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3128 : input_location;
3130 if (type == void_type_node)
3132 if (selector)
3133 error_at (loc, "too many arguments to method %qE", selector);
3134 else
3135 error_at (loc, "too many arguments to function %qE", function);
3136 inform_declaration (fundecl);
3137 return error_args ? -1 : (int) parmnum;
3140 if (selector && argnum > 2)
3142 rname = selector;
3143 argnum -= 2;
3146 npc = null_pointer_constant_p (val);
3148 /* If there is excess precision and a prototype, convert once to
3149 the required type rather than converting via the semantic
3150 type. Likewise without a prototype a float value represented
3151 as long double should be converted once to double. But for
3152 type-generic classification functions excess precision must
3153 be removed here. */
3154 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3155 && (type || !type_generic || !type_generic_remove_excess_precision))
3157 val = TREE_OPERAND (val, 0);
3158 excess_precision = true;
3160 val = c_fully_fold (val, false, NULL);
3161 STRIP_TYPE_NOPS (val);
3163 val = require_complete_type (val);
3165 if (type != 0)
3167 /* Formal parm type is specified by a function prototype. */
3169 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3171 error_at (ploc, "type of formal parameter %d is incomplete",
3172 parmnum + 1);
3173 parmval = val;
3175 else
3177 tree origtype;
3179 /* Optionally warn about conversions that
3180 differ from the default conversions. */
3181 if (warn_traditional_conversion || warn_traditional)
3183 unsigned int formal_prec = TYPE_PRECISION (type);
3185 if (INTEGRAL_TYPE_P (type)
3186 && TREE_CODE (valtype) == REAL_TYPE)
3187 warning_at (ploc, OPT_Wtraditional_conversion,
3188 "passing argument %d of %qE as integer rather "
3189 "than floating due to prototype",
3190 argnum, rname);
3191 if (INTEGRAL_TYPE_P (type)
3192 && TREE_CODE (valtype) == COMPLEX_TYPE)
3193 warning_at (ploc, OPT_Wtraditional_conversion,
3194 "passing argument %d of %qE as integer rather "
3195 "than complex due to prototype",
3196 argnum, rname);
3197 else if (TREE_CODE (type) == COMPLEX_TYPE
3198 && TREE_CODE (valtype) == REAL_TYPE)
3199 warning_at (ploc, OPT_Wtraditional_conversion,
3200 "passing argument %d of %qE as complex rather "
3201 "than floating due to prototype",
3202 argnum, rname);
3203 else if (TREE_CODE (type) == REAL_TYPE
3204 && INTEGRAL_TYPE_P (valtype))
3205 warning_at (ploc, OPT_Wtraditional_conversion,
3206 "passing argument %d of %qE as floating rather "
3207 "than integer due to prototype",
3208 argnum, rname);
3209 else if (TREE_CODE (type) == COMPLEX_TYPE
3210 && INTEGRAL_TYPE_P (valtype))
3211 warning_at (ploc, OPT_Wtraditional_conversion,
3212 "passing argument %d of %qE as complex rather "
3213 "than integer due to prototype",
3214 argnum, rname);
3215 else if (TREE_CODE (type) == REAL_TYPE
3216 && TREE_CODE (valtype) == COMPLEX_TYPE)
3217 warning_at (ploc, OPT_Wtraditional_conversion,
3218 "passing argument %d of %qE as floating rather "
3219 "than complex due to prototype",
3220 argnum, rname);
3221 /* ??? At some point, messages should be written about
3222 conversions between complex types, but that's too messy
3223 to do now. */
3224 else if (TREE_CODE (type) == REAL_TYPE
3225 && TREE_CODE (valtype) == REAL_TYPE)
3227 /* Warn if any argument is passed as `float',
3228 since without a prototype it would be `double'. */
3229 if (formal_prec == TYPE_PRECISION (float_type_node)
3230 && type != dfloat32_type_node)
3231 warning_at (ploc, 0,
3232 "passing argument %d of %qE as %<float%> "
3233 "rather than %<double%> due to prototype",
3234 argnum, rname);
3236 /* Warn if mismatch between argument and prototype
3237 for decimal float types. Warn of conversions with
3238 binary float types and of precision narrowing due to
3239 prototype. */
3240 else if (type != valtype
3241 && (type == dfloat32_type_node
3242 || type == dfloat64_type_node
3243 || type == dfloat128_type_node
3244 || valtype == dfloat32_type_node
3245 || valtype == dfloat64_type_node
3246 || valtype == dfloat128_type_node)
3247 && (formal_prec
3248 <= TYPE_PRECISION (valtype)
3249 || (type == dfloat128_type_node
3250 && (valtype
3251 != dfloat64_type_node
3252 && (valtype
3253 != dfloat32_type_node)))
3254 || (type == dfloat64_type_node
3255 && (valtype
3256 != dfloat32_type_node))))
3257 warning_at (ploc, 0,
3258 "passing argument %d of %qE as %qT "
3259 "rather than %qT due to prototype",
3260 argnum, rname, type, valtype);
3263 /* Detect integer changing in width or signedness.
3264 These warnings are only activated with
3265 -Wtraditional-conversion, not with -Wtraditional. */
3266 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3267 && INTEGRAL_TYPE_P (valtype))
3269 tree would_have_been = default_conversion (val);
3270 tree type1 = TREE_TYPE (would_have_been);
3272 if (TREE_CODE (type) == ENUMERAL_TYPE
3273 && (TYPE_MAIN_VARIANT (type)
3274 == TYPE_MAIN_VARIANT (valtype)))
3275 /* No warning if function asks for enum
3276 and the actual arg is that enum type. */
3278 else if (formal_prec != TYPE_PRECISION (type1))
3279 warning_at (ploc, OPT_Wtraditional_conversion,
3280 "passing argument %d of %qE "
3281 "with different width due to prototype",
3282 argnum, rname);
3283 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3285 /* Don't complain if the formal parameter type
3286 is an enum, because we can't tell now whether
3287 the value was an enum--even the same enum. */
3288 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3290 else if (TREE_CODE (val) == INTEGER_CST
3291 && int_fits_type_p (val, type))
3292 /* Change in signedness doesn't matter
3293 if a constant value is unaffected. */
3295 /* If the value is extended from a narrower
3296 unsigned type, it doesn't matter whether we
3297 pass it as signed or unsigned; the value
3298 certainly is the same either way. */
3299 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3300 && TYPE_UNSIGNED (valtype))
3302 else if (TYPE_UNSIGNED (type))
3303 warning_at (ploc, OPT_Wtraditional_conversion,
3304 "passing argument %d of %qE "
3305 "as unsigned due to prototype",
3306 argnum, rname);
3307 else
3308 warning_at (ploc, OPT_Wtraditional_conversion,
3309 "passing argument %d of %qE "
3310 "as signed due to prototype",
3311 argnum, rname);
3315 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3316 sake of better warnings from convert_and_check. */
3317 if (excess_precision)
3318 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3319 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3320 parmval = convert_for_assignment (loc, ploc, type,
3321 val, origtype, ic_argpass,
3322 npc, fundecl, function,
3323 parmnum + 1);
3325 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3326 && INTEGRAL_TYPE_P (type)
3327 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3328 parmval = default_conversion (parmval);
3331 else if (TREE_CODE (valtype) == REAL_TYPE
3332 && (TYPE_PRECISION (valtype)
3333 <= TYPE_PRECISION (double_type_node))
3334 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3335 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3336 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3338 if (type_generic)
3339 parmval = val;
3340 else
3342 /* Convert `float' to `double'. */
3343 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3344 warning_at (ploc, OPT_Wdouble_promotion,
3345 "implicit conversion from %qT to %qT when passing "
3346 "argument to function",
3347 valtype, double_type_node);
3348 parmval = convert (double_type_node, val);
3351 else if (excess_precision && !type_generic)
3352 /* A "double" argument with excess precision being passed
3353 without a prototype or in variable arguments. */
3354 parmval = convert (valtype, val);
3355 else if ((invalid_func_diag =
3356 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3358 error (invalid_func_diag);
3359 return -1;
3361 else
3362 /* Convert `short' and `char' to full-size `int'. */
3363 parmval = default_conversion (val);
3365 (*values)[parmnum] = parmval;
3366 if (parmval == error_mark_node)
3367 error_args = true;
3369 if (typetail)
3370 typetail = TREE_CHAIN (typetail);
3373 gcc_assert (parmnum == vec_safe_length (values));
3375 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3377 error_at (loc, "too few arguments to function %qE", function);
3378 inform_declaration (fundecl);
3379 return -1;
3382 return error_args ? -1 : (int) parmnum;
3385 /* This is the entry point used by the parser to build unary operators
3386 in the input. CODE, a tree_code, specifies the unary operator, and
3387 ARG is the operand. For unary plus, the C parser currently uses
3388 CONVERT_EXPR for code.
3390 LOC is the location to use for the tree generated.
3393 struct c_expr
3394 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3396 struct c_expr result;
3398 result.value = build_unary_op (loc, code, arg.value, 0);
3399 result.original_code = code;
3400 result.original_type = NULL;
3402 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3403 overflow_warning (loc, result.value);
3405 return result;
3408 /* This is the entry point used by the parser to build binary operators
3409 in the input. CODE, a tree_code, specifies the binary operator, and
3410 ARG1 and ARG2 are the operands. In addition to constructing the
3411 expression, we check for operands that were written with other binary
3412 operators in a way that is likely to confuse the user.
3414 LOCATION is the location of the binary operator. */
3416 struct c_expr
3417 parser_build_binary_op (location_t location, enum tree_code code,
3418 struct c_expr arg1, struct c_expr arg2)
3420 struct c_expr result;
3422 enum tree_code code1 = arg1.original_code;
3423 enum tree_code code2 = arg2.original_code;
3424 tree type1 = (arg1.original_type
3425 ? arg1.original_type
3426 : TREE_TYPE (arg1.value));
3427 tree type2 = (arg2.original_type
3428 ? arg2.original_type
3429 : TREE_TYPE (arg2.value));
3431 result.value = build_binary_op (location, code,
3432 arg1.value, arg2.value, 1);
3433 result.original_code = code;
3434 result.original_type = NULL;
3436 if (TREE_CODE (result.value) == ERROR_MARK)
3437 return result;
3439 if (location != UNKNOWN_LOCATION)
3440 protected_set_expr_location (result.value, location);
3442 /* Check for cases such as x+y<<z which users are likely
3443 to misinterpret. */
3444 if (warn_parentheses)
3445 warn_about_parentheses (location, code, code1, arg1.value, code2,
3446 arg2.value);
3448 if (warn_logical_op)
3449 warn_logical_operator (location, code, TREE_TYPE (result.value),
3450 code1, arg1.value, code2, arg2.value);
3452 if (warn_logical_not_paren
3453 && TREE_CODE_CLASS (code) == tcc_comparison
3454 && code1 == TRUTH_NOT_EXPR
3455 && code2 != TRUTH_NOT_EXPR
3456 /* Avoid warning for !!x == y. */
3457 && (TREE_CODE (arg1.value) != NE_EXPR
3458 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3460 /* Avoid warning for !b == y where b has _Bool type. */
3461 tree t = integer_zero_node;
3462 if (TREE_CODE (arg1.value) == EQ_EXPR
3463 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3464 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3466 t = TREE_OPERAND (arg1.value, 0);
3469 if (TREE_TYPE (t) != integer_type_node)
3470 break;
3471 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3472 t = C_MAYBE_CONST_EXPR_EXPR (t);
3473 else if (CONVERT_EXPR_P (t))
3474 t = TREE_OPERAND (t, 0);
3475 else
3476 break;
3478 while (1);
3480 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3481 warn_logical_not_parentheses (location, code, arg2.value);
3484 /* Warn about comparisons against string literals, with the exception
3485 of testing for equality or inequality of a string literal with NULL. */
3486 if (code == EQ_EXPR || code == NE_EXPR)
3488 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3489 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3490 warning_at (location, OPT_Waddress,
3491 "comparison with string literal results in unspecified behavior");
3493 else if (TREE_CODE_CLASS (code) == tcc_comparison
3494 && (code1 == STRING_CST || code2 == STRING_CST))
3495 warning_at (location, OPT_Waddress,
3496 "comparison with string literal results in unspecified behavior");
3498 if (TREE_OVERFLOW_P (result.value)
3499 && !TREE_OVERFLOW_P (arg1.value)
3500 && !TREE_OVERFLOW_P (arg2.value))
3501 overflow_warning (location, result.value);
3503 /* Warn about comparisons of different enum types. */
3504 if (warn_enum_compare
3505 && TREE_CODE_CLASS (code) == tcc_comparison
3506 && TREE_CODE (type1) == ENUMERAL_TYPE
3507 && TREE_CODE (type2) == ENUMERAL_TYPE
3508 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3509 warning_at (location, OPT_Wenum_compare,
3510 "comparison between %qT and %qT",
3511 type1, type2);
3513 return result;
3516 /* Return a tree for the difference of pointers OP0 and OP1.
3517 The resulting tree has type int. */
3519 static tree
3520 pointer_diff (location_t loc, tree op0, tree op1)
3522 tree restype = ptrdiff_type_node;
3523 tree result, inttype;
3525 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3526 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3527 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3528 tree orig_op1 = op1;
3530 /* If the operands point into different address spaces, we need to
3531 explicitly convert them to pointers into the common address space
3532 before we can subtract the numerical address values. */
3533 if (as0 != as1)
3535 addr_space_t as_common;
3536 tree common_type;
3538 /* Determine the common superset address space. This is guaranteed
3539 to exist because the caller verified that comp_target_types
3540 returned non-zero. */
3541 if (!addr_space_superset (as0, as1, &as_common))
3542 gcc_unreachable ();
3544 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3545 op0 = convert (common_type, op0);
3546 op1 = convert (common_type, op1);
3549 /* Determine integer type to perform computations in. This will usually
3550 be the same as the result type (ptrdiff_t), but may need to be a wider
3551 type if pointers for the address space are wider than ptrdiff_t. */
3552 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3553 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3554 else
3555 inttype = restype;
3557 if (TREE_CODE (target_type) == VOID_TYPE)
3558 pedwarn (loc, OPT_Wpointer_arith,
3559 "pointer of type %<void *%> used in subtraction");
3560 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3561 pedwarn (loc, OPT_Wpointer_arith,
3562 "pointer to a function used in subtraction");
3564 /* First do the subtraction as integers;
3565 then drop through to build the divide operator.
3566 Do not do default conversions on the minus operator
3567 in case restype is a short type. */
3569 op0 = build_binary_op (loc,
3570 MINUS_EXPR, convert (inttype, op0),
3571 convert (inttype, op1), 0);
3572 /* This generates an error if op1 is pointer to incomplete type. */
3573 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3574 error_at (loc, "arithmetic on pointer to an incomplete type");
3576 op1 = c_size_in_bytes (target_type);
3578 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3579 error_at (loc, "arithmetic on pointer to an empty aggregate");
3581 /* Divide by the size, in easiest possible way. */
3582 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3583 op0, convert (inttype, op1));
3585 /* Convert to final result type if necessary. */
3586 return convert (restype, result);
3589 /* Expand atomic compound assignments into an approriate sequence as
3590 specified by the C11 standard section 6.5.16.2.
3591 given
3592 _Atomic T1 E1
3593 T2 E2
3594 E1 op= E2
3596 This sequence is used for all types for which these operations are
3597 supported.
3599 In addition, built-in versions of the 'fe' prefixed routines may
3600 need to be invoked for floating point (real, complex or vector) when
3601 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3603 T1 newval;
3604 T1 old;
3605 T1 *addr
3606 T2 val
3607 fenv_t fenv
3609 addr = &E1;
3610 val = (E2);
3611 __atomic_load (addr, &old, SEQ_CST);
3612 feholdexcept (&fenv);
3613 loop:
3614 newval = old op val;
3615 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3616 SEQ_CST))
3617 goto done;
3618 feclearexcept (FE_ALL_EXCEPT);
3619 goto loop:
3620 done:
3621 feupdateenv (&fenv);
3623 Also note that the compiler is simply issuing the generic form of
3624 the atomic operations. This requires temp(s) and has their address
3625 taken. The atomic processing is smart enough to figure out when the
3626 size of an object can utilize a lock-free version, and convert the
3627 built-in call to the appropriate lock-free routine. The optimizers
3628 will then dispose of any temps that are no longer required, and
3629 lock-free implementations are utilized as long as there is target
3630 support for the required size.
3632 If the operator is NOP_EXPR, then this is a simple assignment, and
3633 an __atomic_store is issued to perform the assignment rather than
3634 the above loop.
3638 /* Build an atomic assignment at LOC, expanding into the proper
3639 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3640 the result of the operation, unless RETURN_OLD_P in which case
3641 return the old value of LHS (this is only for postincrement and
3642 postdecrement). */
3643 static tree
3644 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3645 tree rhs, bool return_old_p)
3647 tree fndecl, func_call;
3648 vec<tree, va_gc> *params;
3649 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3650 tree old, old_addr;
3651 tree compound_stmt;
3652 tree stmt, goto_stmt;
3653 tree loop_label, loop_decl, done_label, done_decl;
3655 tree lhs_type = TREE_TYPE (lhs);
3656 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3657 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3658 tree rhs_type = TREE_TYPE (rhs);
3660 gcc_assert (TYPE_ATOMIC (lhs_type));
3662 if (return_old_p)
3663 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3665 /* Allocate enough vector items for a compare_exchange. */
3666 vec_alloc (params, 6);
3668 /* Create a compound statement to hold the sequence of statements
3669 with a loop. */
3670 compound_stmt = c_begin_compound_stmt (false);
3672 /* Fold the RHS if it hasn't already been folded. */
3673 if (modifycode != NOP_EXPR)
3674 rhs = c_fully_fold (rhs, false, NULL);
3676 /* Remove the qualifiers for the rest of the expressions and create
3677 the VAL temp variable to hold the RHS. */
3678 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3679 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3680 val = create_tmp_var_raw (nonatomic_rhs_type);
3681 TREE_ADDRESSABLE (val) = 1;
3682 TREE_NO_WARNING (val) = 1;
3683 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3684 NULL_TREE);
3685 SET_EXPR_LOCATION (rhs, loc);
3686 add_stmt (rhs);
3688 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3689 an atomic_store. */
3690 if (modifycode == NOP_EXPR)
3692 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3693 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3694 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3695 params->quick_push (lhs_addr);
3696 params->quick_push (rhs);
3697 params->quick_push (seq_cst);
3698 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3699 add_stmt (func_call);
3701 /* Finish the compound statement. */
3702 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3704 /* VAL is the value which was stored, return a COMPOUND_STMT of
3705 the statement and that value. */
3706 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3709 /* Create the variables and labels required for the op= form. */
3710 old = create_tmp_var_raw (nonatomic_lhs_type);
3711 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3712 TREE_ADDRESSABLE (old) = 1;
3713 TREE_NO_WARNING (old) = 1;
3715 newval = create_tmp_var_raw (nonatomic_lhs_type);
3716 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3717 TREE_ADDRESSABLE (newval) = 1;
3719 loop_decl = create_artificial_label (loc);
3720 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3722 done_decl = create_artificial_label (loc);
3723 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3725 /* __atomic_load (addr, &old, SEQ_CST). */
3726 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3727 params->quick_push (lhs_addr);
3728 params->quick_push (old_addr);
3729 params->quick_push (seq_cst);
3730 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3731 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
3732 NULL_TREE);
3733 add_stmt (old);
3734 params->truncate (0);
3736 /* Create the expressions for floating-point environment
3737 manipulation, if required. */
3738 bool need_fenv = (flag_trapping_math
3739 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3740 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3741 if (need_fenv)
3742 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3744 if (hold_call)
3745 add_stmt (hold_call);
3747 /* loop: */
3748 add_stmt (loop_label);
3750 /* newval = old + val; */
3751 rhs = build_binary_op (loc, modifycode, old, val, 1);
3752 rhs = c_fully_fold (rhs, false, NULL);
3753 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3754 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3755 NULL_TREE, 0);
3756 if (rhs != error_mark_node)
3758 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
3759 NULL_TREE);
3760 SET_EXPR_LOCATION (rhs, loc);
3761 add_stmt (rhs);
3764 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3765 goto done; */
3766 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3767 params->quick_push (lhs_addr);
3768 params->quick_push (old_addr);
3769 params->quick_push (newval_addr);
3770 params->quick_push (integer_zero_node);
3771 params->quick_push (seq_cst);
3772 params->quick_push (seq_cst);
3773 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3775 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3776 SET_EXPR_LOCATION (goto_stmt, loc);
3778 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3779 SET_EXPR_LOCATION (stmt, loc);
3780 add_stmt (stmt);
3782 if (clear_call)
3783 add_stmt (clear_call);
3785 /* goto loop; */
3786 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3787 SET_EXPR_LOCATION (goto_stmt, loc);
3788 add_stmt (goto_stmt);
3790 /* done: */
3791 add_stmt (done_label);
3793 if (update_call)
3794 add_stmt (update_call);
3796 /* Finish the compound statement. */
3797 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3799 /* NEWVAL is the value that was successfully stored, return a
3800 COMPOUND_EXPR of the statement and the appropriate value. */
3801 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3802 return_old_p ? old : newval);
3805 /* Construct and perhaps optimize a tree representation
3806 for a unary operation. CODE, a tree_code, specifies the operation
3807 and XARG is the operand.
3808 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3809 the default promotions (such as from short to int).
3810 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3811 allows non-lvalues; this is only used to handle conversion of non-lvalue
3812 arrays to pointers in C99.
3814 LOCATION is the location of the operator. */
3816 tree
3817 build_unary_op (location_t location,
3818 enum tree_code code, tree xarg, int flag)
3820 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3821 tree arg = xarg;
3822 tree argtype = 0;
3823 enum tree_code typecode;
3824 tree val;
3825 tree ret = error_mark_node;
3826 tree eptype = NULL_TREE;
3827 int noconvert = flag;
3828 const char *invalid_op_diag;
3829 bool int_operands;
3831 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3832 if (int_operands)
3833 arg = remove_c_maybe_const_expr (arg);
3835 if (code != ADDR_EXPR)
3836 arg = require_complete_type (arg);
3838 typecode = TREE_CODE (TREE_TYPE (arg));
3839 if (typecode == ERROR_MARK)
3840 return error_mark_node;
3841 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3842 typecode = INTEGER_TYPE;
3844 if ((invalid_op_diag
3845 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3847 error_at (location, invalid_op_diag);
3848 return error_mark_node;
3851 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3853 eptype = TREE_TYPE (arg);
3854 arg = TREE_OPERAND (arg, 0);
3857 switch (code)
3859 case CONVERT_EXPR:
3860 /* This is used for unary plus, because a CONVERT_EXPR
3861 is enough to prevent anybody from looking inside for
3862 associativity, but won't generate any code. */
3863 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3864 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3865 || typecode == VECTOR_TYPE))
3867 error_at (location, "wrong type argument to unary plus");
3868 return error_mark_node;
3870 else if (!noconvert)
3871 arg = default_conversion (arg);
3872 arg = non_lvalue_loc (location, arg);
3873 break;
3875 case NEGATE_EXPR:
3876 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3877 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3878 || typecode == VECTOR_TYPE))
3880 error_at (location, "wrong type argument to unary minus");
3881 return error_mark_node;
3883 else if (!noconvert)
3884 arg = default_conversion (arg);
3885 break;
3887 case BIT_NOT_EXPR:
3888 /* ~ works on integer types and non float vectors. */
3889 if (typecode == INTEGER_TYPE
3890 || (typecode == VECTOR_TYPE
3891 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3893 if (!noconvert)
3894 arg = default_conversion (arg);
3896 else if (typecode == COMPLEX_TYPE)
3898 code = CONJ_EXPR;
3899 pedwarn (location, OPT_Wpedantic,
3900 "ISO C does not support %<~%> for complex conjugation");
3901 if (!noconvert)
3902 arg = default_conversion (arg);
3904 else
3906 error_at (location, "wrong type argument to bit-complement");
3907 return error_mark_node;
3909 break;
3911 case ABS_EXPR:
3912 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3914 error_at (location, "wrong type argument to abs");
3915 return error_mark_node;
3917 else if (!noconvert)
3918 arg = default_conversion (arg);
3919 break;
3921 case CONJ_EXPR:
3922 /* Conjugating a real value is a no-op, but allow it anyway. */
3923 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3924 || typecode == COMPLEX_TYPE))
3926 error_at (location, "wrong type argument to conjugation");
3927 return error_mark_node;
3929 else if (!noconvert)
3930 arg = default_conversion (arg);
3931 break;
3933 case TRUTH_NOT_EXPR:
3934 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3935 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3936 && typecode != COMPLEX_TYPE)
3938 error_at (location,
3939 "wrong type argument to unary exclamation mark");
3940 return error_mark_node;
3942 if (int_operands)
3944 arg = c_objc_common_truthvalue_conversion (location, xarg);
3945 arg = remove_c_maybe_const_expr (arg);
3947 else
3948 arg = c_objc_common_truthvalue_conversion (location, arg);
3949 ret = invert_truthvalue_loc (location, arg);
3950 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3951 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3952 location = EXPR_LOCATION (ret);
3953 goto return_build_unary_op;
3955 case REALPART_EXPR:
3956 case IMAGPART_EXPR:
3957 ret = build_real_imag_expr (location, code, arg);
3958 if (ret == error_mark_node)
3959 return error_mark_node;
3960 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3961 eptype = TREE_TYPE (eptype);
3962 goto return_build_unary_op;
3964 case PREINCREMENT_EXPR:
3965 case POSTINCREMENT_EXPR:
3966 case PREDECREMENT_EXPR:
3967 case POSTDECREMENT_EXPR:
3969 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3971 tree inner = build_unary_op (location, code,
3972 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3973 if (inner == error_mark_node)
3974 return error_mark_node;
3975 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3976 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3977 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3978 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3979 goto return_build_unary_op;
3982 /* Complain about anything that is not a true lvalue. In
3983 Objective-C, skip this check for property_refs. */
3984 if (!objc_is_property_ref (arg)
3985 && !lvalue_or_else (location,
3986 arg, ((code == PREINCREMENT_EXPR
3987 || code == POSTINCREMENT_EXPR)
3988 ? lv_increment
3989 : lv_decrement)))
3990 return error_mark_node;
3992 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3994 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3995 warning_at (location, OPT_Wc___compat,
3996 "increment of enumeration value is invalid in C++");
3997 else
3998 warning_at (location, OPT_Wc___compat,
3999 "decrement of enumeration value is invalid in C++");
4002 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4003 arg = c_fully_fold (arg, false, NULL);
4005 bool atomic_op;
4006 atomic_op = really_atomic_lvalue (arg);
4008 /* Increment or decrement the real part of the value,
4009 and don't change the imaginary part. */
4010 if (typecode == COMPLEX_TYPE)
4012 tree real, imag;
4014 pedwarn (location, OPT_Wpedantic,
4015 "ISO C does not support %<++%> and %<--%> on complex types");
4017 if (!atomic_op)
4019 arg = stabilize_reference (arg);
4020 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4021 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4022 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4023 if (real == error_mark_node || imag == error_mark_node)
4024 return error_mark_node;
4025 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4026 real, imag);
4027 goto return_build_unary_op;
4031 /* Report invalid types. */
4033 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4034 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4035 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4037 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4038 error_at (location, "wrong type argument to increment");
4039 else
4040 error_at (location, "wrong type argument to decrement");
4042 return error_mark_node;
4046 tree inc;
4048 argtype = TREE_TYPE (arg);
4050 /* Compute the increment. */
4052 if (typecode == POINTER_TYPE)
4054 /* If pointer target is an incomplete type,
4055 we just cannot know how to do the arithmetic. */
4056 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4058 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4059 error_at (location,
4060 "increment of pointer to an incomplete type %qT",
4061 TREE_TYPE (argtype));
4062 else
4063 error_at (location,
4064 "decrement of pointer to an incomplete type %qT",
4065 TREE_TYPE (argtype));
4067 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4068 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4070 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4071 pedwarn (location, OPT_Wpointer_arith,
4072 "wrong type argument to increment");
4073 else
4074 pedwarn (location, OPT_Wpointer_arith,
4075 "wrong type argument to decrement");
4078 inc = c_size_in_bytes (TREE_TYPE (argtype));
4079 inc = convert_to_ptrofftype_loc (location, inc);
4081 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4083 /* For signed fract types, we invert ++ to -- or
4084 -- to ++, and change inc from 1 to -1, because
4085 it is not possible to represent 1 in signed fract constants.
4086 For unsigned fract types, the result always overflows and
4087 we get an undefined (original) or the maximum value. */
4088 if (code == PREINCREMENT_EXPR)
4089 code = PREDECREMENT_EXPR;
4090 else if (code == PREDECREMENT_EXPR)
4091 code = PREINCREMENT_EXPR;
4092 else if (code == POSTINCREMENT_EXPR)
4093 code = POSTDECREMENT_EXPR;
4094 else /* code == POSTDECREMENT_EXPR */
4095 code = POSTINCREMENT_EXPR;
4097 inc = integer_minus_one_node;
4098 inc = convert (argtype, inc);
4100 else
4102 inc = VECTOR_TYPE_P (argtype)
4103 ? build_one_cst (argtype)
4104 : integer_one_node;
4105 inc = convert (argtype, inc);
4108 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4109 need to ask Objective-C to build the increment or decrement
4110 expression for it. */
4111 if (objc_is_property_ref (arg))
4112 return objc_build_incr_expr_for_property_ref (location, code,
4113 arg, inc);
4115 /* Report a read-only lvalue. */
4116 if (TYPE_READONLY (argtype))
4118 readonly_error (location, arg,
4119 ((code == PREINCREMENT_EXPR
4120 || code == POSTINCREMENT_EXPR)
4121 ? lv_increment : lv_decrement));
4122 return error_mark_node;
4124 else if (TREE_READONLY (arg))
4125 readonly_warning (arg,
4126 ((code == PREINCREMENT_EXPR
4127 || code == POSTINCREMENT_EXPR)
4128 ? lv_increment : lv_decrement));
4130 /* If the argument is atomic, use the special code sequences for
4131 atomic compound assignment. */
4132 if (atomic_op)
4134 arg = stabilize_reference (arg);
4135 ret = build_atomic_assign (location, arg,
4136 ((code == PREINCREMENT_EXPR
4137 || code == POSTINCREMENT_EXPR)
4138 ? PLUS_EXPR
4139 : MINUS_EXPR),
4140 (FRACT_MODE_P (TYPE_MODE (argtype))
4141 ? inc
4142 : integer_one_node),
4143 (code == POSTINCREMENT_EXPR
4144 || code == POSTDECREMENT_EXPR));
4145 goto return_build_unary_op;
4148 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4149 val = boolean_increment (code, arg);
4150 else
4151 val = build2 (code, TREE_TYPE (arg), arg, inc);
4152 TREE_SIDE_EFFECTS (val) = 1;
4153 if (TREE_CODE (val) != code)
4154 TREE_NO_WARNING (val) = 1;
4155 ret = val;
4156 goto return_build_unary_op;
4159 case ADDR_EXPR:
4160 /* Note that this operation never does default_conversion. */
4162 /* The operand of unary '&' must be an lvalue (which excludes
4163 expressions of type void), or, in C99, the result of a [] or
4164 unary '*' operator. */
4165 if (VOID_TYPE_P (TREE_TYPE (arg))
4166 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4167 && (TREE_CODE (arg) != INDIRECT_REF
4168 || !flag_isoc99))
4169 pedwarn (location, 0, "taking address of expression of type %<void%>");
4171 /* Let &* cancel out to simplify resulting code. */
4172 if (TREE_CODE (arg) == INDIRECT_REF)
4174 /* Don't let this be an lvalue. */
4175 if (lvalue_p (TREE_OPERAND (arg, 0)))
4176 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4177 ret = TREE_OPERAND (arg, 0);
4178 goto return_build_unary_op;
4181 /* Anything not already handled and not a true memory reference
4182 or a non-lvalue array is an error. */
4183 if (typecode != FUNCTION_TYPE && !flag
4184 && !lvalue_or_else (location, arg, lv_addressof))
4185 return error_mark_node;
4187 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4188 folding later. */
4189 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4191 tree inner = build_unary_op (location, code,
4192 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4193 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4194 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4195 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4196 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4197 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4198 goto return_build_unary_op;
4201 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4202 argtype = TREE_TYPE (arg);
4204 /* If the lvalue is const or volatile, merge that into the type
4205 to which the address will point. This is only needed
4206 for function types. */
4207 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4208 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4209 && TREE_CODE (argtype) == FUNCTION_TYPE)
4211 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4212 int quals = orig_quals;
4214 if (TREE_READONLY (arg))
4215 quals |= TYPE_QUAL_CONST;
4216 if (TREE_THIS_VOLATILE (arg))
4217 quals |= TYPE_QUAL_VOLATILE;
4219 argtype = c_build_qualified_type (argtype, quals);
4222 switch (TREE_CODE (arg))
4224 case COMPONENT_REF:
4225 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4227 error ("cannot take address of bit-field %qD",
4228 TREE_OPERAND (arg, 1));
4229 return error_mark_node;
4232 /* ... fall through ... */
4234 case ARRAY_REF:
4235 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4237 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg)))
4239 error ("cannot take address of scalar with reverse storage "
4240 "order");
4241 return error_mark_node;
4244 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4245 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4246 warning (OPT_Wscalar_storage_order, "address of array with "
4247 "reverse scalar storage order requested");
4250 default:
4251 break;
4254 if (!c_mark_addressable (arg))
4255 return error_mark_node;
4257 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4258 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4260 argtype = build_pointer_type (argtype);
4262 /* ??? Cope with user tricks that amount to offsetof. Delete this
4263 when we have proper support for integer constant expressions. */
4264 val = get_base_address (arg);
4265 if (val && TREE_CODE (val) == INDIRECT_REF
4266 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4268 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4269 goto return_build_unary_op;
4272 val = build1 (ADDR_EXPR, argtype, arg);
4274 ret = val;
4275 goto return_build_unary_op;
4277 default:
4278 gcc_unreachable ();
4281 if (argtype == 0)
4282 argtype = TREE_TYPE (arg);
4283 if (TREE_CODE (arg) == INTEGER_CST)
4284 ret = (require_constant_value
4285 ? fold_build1_initializer_loc (location, code, argtype, arg)
4286 : fold_build1_loc (location, code, argtype, arg));
4287 else
4288 ret = build1 (code, argtype, arg);
4289 return_build_unary_op:
4290 gcc_assert (ret != error_mark_node);
4291 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4292 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4293 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4294 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4295 ret = note_integer_operands (ret);
4296 if (eptype)
4297 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4298 protected_set_expr_location (ret, location);
4299 return ret;
4302 /* Return nonzero if REF is an lvalue valid for this language.
4303 Lvalues can be assigned, unless their type has TYPE_READONLY.
4304 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4306 bool
4307 lvalue_p (const_tree ref)
4309 const enum tree_code code = TREE_CODE (ref);
4311 switch (code)
4313 case REALPART_EXPR:
4314 case IMAGPART_EXPR:
4315 case COMPONENT_REF:
4316 return lvalue_p (TREE_OPERAND (ref, 0));
4318 case C_MAYBE_CONST_EXPR:
4319 return lvalue_p (TREE_OPERAND (ref, 1));
4321 case COMPOUND_LITERAL_EXPR:
4322 case STRING_CST:
4323 return 1;
4325 case INDIRECT_REF:
4326 case ARRAY_REF:
4327 case ARRAY_NOTATION_REF:
4328 case VAR_DECL:
4329 case PARM_DECL:
4330 case RESULT_DECL:
4331 case ERROR_MARK:
4332 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4333 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4335 case BIND_EXPR:
4336 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4338 default:
4339 return 0;
4343 /* Give a warning for storing in something that is read-only in GCC
4344 terms but not const in ISO C terms. */
4346 static void
4347 readonly_warning (tree arg, enum lvalue_use use)
4349 switch (use)
4351 case lv_assign:
4352 warning (0, "assignment of read-only location %qE", arg);
4353 break;
4354 case lv_increment:
4355 warning (0, "increment of read-only location %qE", arg);
4356 break;
4357 case lv_decrement:
4358 warning (0, "decrement of read-only location %qE", arg);
4359 break;
4360 default:
4361 gcc_unreachable ();
4363 return;
4367 /* Return nonzero if REF is an lvalue valid for this language;
4368 otherwise, print an error message and return zero. USE says
4369 how the lvalue is being used and so selects the error message.
4370 LOCATION is the location at which any error should be reported. */
4372 static int
4373 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4375 int win = lvalue_p (ref);
4377 if (!win)
4378 lvalue_error (loc, use);
4380 return win;
4383 /* Mark EXP saying that we need to be able to take the
4384 address of it; it should not be allocated in a register.
4385 Returns true if successful. */
4387 bool
4388 c_mark_addressable (tree exp)
4390 tree x = exp;
4392 while (1)
4393 switch (TREE_CODE (x))
4395 case COMPONENT_REF:
4396 case ADDR_EXPR:
4397 case ARRAY_REF:
4398 case REALPART_EXPR:
4399 case IMAGPART_EXPR:
4400 x = TREE_OPERAND (x, 0);
4401 break;
4403 case COMPOUND_LITERAL_EXPR:
4404 case CONSTRUCTOR:
4405 TREE_ADDRESSABLE (x) = 1;
4406 return true;
4408 case VAR_DECL:
4409 case CONST_DECL:
4410 case PARM_DECL:
4411 case RESULT_DECL:
4412 if (C_DECL_REGISTER (x)
4413 && DECL_NONLOCAL (x))
4415 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4417 error
4418 ("global register variable %qD used in nested function", x);
4419 return false;
4421 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4423 else if (C_DECL_REGISTER (x))
4425 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4426 error ("address of global register variable %qD requested", x);
4427 else
4428 error ("address of register variable %qD requested", x);
4429 return false;
4432 /* drops in */
4433 case FUNCTION_DECL:
4434 TREE_ADDRESSABLE (x) = 1;
4435 /* drops out */
4436 default:
4437 return true;
4441 /* Convert EXPR to TYPE, warning about conversion problems with
4442 constants. SEMANTIC_TYPE is the type this conversion would use
4443 without excess precision. If SEMANTIC_TYPE is NULL, this function
4444 is equivalent to convert_and_check. This function is a wrapper that
4445 handles conversions that may be different than
4446 the usual ones because of excess precision. */
4448 static tree
4449 ep_convert_and_check (location_t loc, tree type, tree expr,
4450 tree semantic_type)
4452 if (TREE_TYPE (expr) == type)
4453 return expr;
4455 if (!semantic_type)
4456 return convert_and_check (loc, type, expr);
4458 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4459 && TREE_TYPE (expr) != semantic_type)
4461 /* For integers, we need to check the real conversion, not
4462 the conversion to the excess precision type. */
4463 expr = convert_and_check (loc, semantic_type, expr);
4465 /* Result type is the excess precision type, which should be
4466 large enough, so do not check. */
4467 return convert (type, expr);
4470 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4471 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4472 if folded to an integer constant then the unselected half may
4473 contain arbitrary operations not normally permitted in constant
4474 expressions. Set the location of the expression to LOC. */
4476 tree
4477 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4478 tree op1, tree op1_original_type, tree op2,
4479 tree op2_original_type)
4481 tree type1;
4482 tree type2;
4483 enum tree_code code1;
4484 enum tree_code code2;
4485 tree result_type = NULL;
4486 tree semantic_result_type = NULL;
4487 tree orig_op1 = op1, orig_op2 = op2;
4488 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4489 bool ifexp_int_operands;
4490 tree ret;
4492 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4493 if (op1_int_operands)
4494 op1 = remove_c_maybe_const_expr (op1);
4495 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4496 if (op2_int_operands)
4497 op2 = remove_c_maybe_const_expr (op2);
4498 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4499 if (ifexp_int_operands)
4500 ifexp = remove_c_maybe_const_expr (ifexp);
4502 /* Promote both alternatives. */
4504 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4505 op1 = default_conversion (op1);
4506 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4507 op2 = default_conversion (op2);
4509 if (TREE_CODE (ifexp) == ERROR_MARK
4510 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4511 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4512 return error_mark_node;
4514 type1 = TREE_TYPE (op1);
4515 code1 = TREE_CODE (type1);
4516 type2 = TREE_TYPE (op2);
4517 code2 = TREE_CODE (type2);
4519 /* C90 does not permit non-lvalue arrays in conditional expressions.
4520 In C99 they will be pointers by now. */
4521 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4523 error_at (colon_loc, "non-lvalue array in conditional expression");
4524 return error_mark_node;
4527 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4528 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4529 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4530 || code1 == COMPLEX_TYPE)
4531 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4532 || code2 == COMPLEX_TYPE))
4534 semantic_result_type = c_common_type (type1, type2);
4535 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4537 op1 = TREE_OPERAND (op1, 0);
4538 type1 = TREE_TYPE (op1);
4539 gcc_assert (TREE_CODE (type1) == code1);
4541 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4543 op2 = TREE_OPERAND (op2, 0);
4544 type2 = TREE_TYPE (op2);
4545 gcc_assert (TREE_CODE (type2) == code2);
4549 if (warn_cxx_compat)
4551 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4552 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4554 if (TREE_CODE (t1) == ENUMERAL_TYPE
4555 && TREE_CODE (t2) == ENUMERAL_TYPE
4556 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4557 warning_at (colon_loc, OPT_Wc___compat,
4558 ("different enum types in conditional is "
4559 "invalid in C++: %qT vs %qT"),
4560 t1, t2);
4563 /* Quickly detect the usual case where op1 and op2 have the same type
4564 after promotion. */
4565 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4567 if (type1 == type2)
4568 result_type = type1;
4569 else
4570 result_type = TYPE_MAIN_VARIANT (type1);
4572 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4573 || code1 == COMPLEX_TYPE)
4574 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4575 || code2 == COMPLEX_TYPE))
4577 result_type = c_common_type (type1, type2);
4578 do_warn_double_promotion (result_type, type1, type2,
4579 "implicit conversion from %qT to %qT to "
4580 "match other result of conditional",
4581 colon_loc);
4583 /* If -Wsign-compare, warn here if type1 and type2 have
4584 different signedness. We'll promote the signed to unsigned
4585 and later code won't know it used to be different.
4586 Do this check on the original types, so that explicit casts
4587 will be considered, but default promotions won't. */
4588 if (c_inhibit_evaluation_warnings == 0)
4590 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4591 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4593 if (unsigned_op1 ^ unsigned_op2)
4595 bool ovf;
4597 /* Do not warn if the result type is signed, since the
4598 signed type will only be chosen if it can represent
4599 all the values of the unsigned type. */
4600 if (!TYPE_UNSIGNED (result_type))
4601 /* OK */;
4602 else
4604 bool op1_maybe_const = true;
4605 bool op2_maybe_const = true;
4607 /* Do not warn if the signed quantity is an
4608 unsuffixed integer literal (or some static
4609 constant expression involving such literals) and
4610 it is non-negative. This warning requires the
4611 operands to be folded for best results, so do
4612 that folding in this case even without
4613 warn_sign_compare to avoid warning options
4614 possibly affecting code generation. */
4615 c_inhibit_evaluation_warnings
4616 += (ifexp == truthvalue_false_node);
4617 op1 = c_fully_fold (op1, require_constant_value,
4618 &op1_maybe_const);
4619 c_inhibit_evaluation_warnings
4620 -= (ifexp == truthvalue_false_node);
4622 c_inhibit_evaluation_warnings
4623 += (ifexp == truthvalue_true_node);
4624 op2 = c_fully_fold (op2, require_constant_value,
4625 &op2_maybe_const);
4626 c_inhibit_evaluation_warnings
4627 -= (ifexp == truthvalue_true_node);
4629 if (warn_sign_compare)
4631 if ((unsigned_op2
4632 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4633 || (unsigned_op1
4634 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4635 /* OK */;
4636 else
4637 warning_at (colon_loc, OPT_Wsign_compare,
4638 ("signed and unsigned type in "
4639 "conditional expression"));
4641 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4642 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4643 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4644 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4649 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4651 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4652 pedwarn (colon_loc, OPT_Wpedantic,
4653 "ISO C forbids conditional expr with only one void side");
4654 result_type = void_type_node;
4656 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4658 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4659 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4660 addr_space_t as_common;
4662 if (comp_target_types (colon_loc, type1, type2))
4663 result_type = common_pointer_type (type1, type2);
4664 else if (null_pointer_constant_p (orig_op1))
4665 result_type = type2;
4666 else if (null_pointer_constant_p (orig_op2))
4667 result_type = type1;
4668 else if (!addr_space_superset (as1, as2, &as_common))
4670 error_at (colon_loc, "pointers to disjoint address spaces "
4671 "used in conditional expression");
4672 return error_mark_node;
4674 else if (VOID_TYPE_P (TREE_TYPE (type1))
4675 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4677 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4678 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4679 & ~TYPE_QUALS (TREE_TYPE (type1))))
4680 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4681 "pointer to array loses qualifier "
4682 "in conditional expression");
4684 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4685 pedwarn (colon_loc, OPT_Wpedantic,
4686 "ISO C forbids conditional expr between "
4687 "%<void *%> and function pointer");
4688 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4689 TREE_TYPE (type2)));
4691 else if (VOID_TYPE_P (TREE_TYPE (type2))
4692 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4694 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4695 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4696 & ~TYPE_QUALS (TREE_TYPE (type2))))
4697 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4698 "pointer to array loses qualifier "
4699 "in conditional expression");
4701 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4702 pedwarn (colon_loc, OPT_Wpedantic,
4703 "ISO C forbids conditional expr between "
4704 "%<void *%> and function pointer");
4705 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4706 TREE_TYPE (type1)));
4708 /* Objective-C pointer comparisons are a bit more lenient. */
4709 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4710 result_type = objc_common_type (type1, type2);
4711 else
4713 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4715 pedwarn (colon_loc, 0,
4716 "pointer type mismatch in conditional expression");
4717 result_type = build_pointer_type
4718 (build_qualified_type (void_type_node, qual));
4721 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4723 if (!null_pointer_constant_p (orig_op2))
4724 pedwarn (colon_loc, 0,
4725 "pointer/integer type mismatch in conditional expression");
4726 else
4728 op2 = null_pointer_node;
4730 result_type = type1;
4732 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4734 if (!null_pointer_constant_p (orig_op1))
4735 pedwarn (colon_loc, 0,
4736 "pointer/integer type mismatch in conditional expression");
4737 else
4739 op1 = null_pointer_node;
4741 result_type = type2;
4744 if (!result_type)
4746 if (flag_cond_mismatch)
4747 result_type = void_type_node;
4748 else
4750 error_at (colon_loc, "type mismatch in conditional expression");
4751 return error_mark_node;
4755 /* Merge const and volatile flags of the incoming types. */
4756 result_type
4757 = build_type_variant (result_type,
4758 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4759 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4761 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4762 semantic_result_type);
4763 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4764 semantic_result_type);
4766 if (ifexp_bcp && ifexp == truthvalue_true_node)
4768 op2_int_operands = true;
4769 op1 = c_fully_fold (op1, require_constant_value, NULL);
4771 if (ifexp_bcp && ifexp == truthvalue_false_node)
4773 op1_int_operands = true;
4774 op2 = c_fully_fold (op2, require_constant_value, NULL);
4776 int_const = int_operands = (ifexp_int_operands
4777 && op1_int_operands
4778 && op2_int_operands);
4779 if (int_operands)
4781 int_const = ((ifexp == truthvalue_true_node
4782 && TREE_CODE (orig_op1) == INTEGER_CST
4783 && !TREE_OVERFLOW (orig_op1))
4784 || (ifexp == truthvalue_false_node
4785 && TREE_CODE (orig_op2) == INTEGER_CST
4786 && !TREE_OVERFLOW (orig_op2)));
4788 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4789 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4790 else
4792 if (int_operands)
4794 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4795 nested inside of the expression. */
4796 op1 = c_fully_fold (op1, false, NULL);
4797 op2 = c_fully_fold (op2, false, NULL);
4799 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4800 if (int_operands)
4801 ret = note_integer_operands (ret);
4803 if (semantic_result_type)
4804 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4806 protected_set_expr_location (ret, colon_loc);
4807 return ret;
4810 /* Return a compound expression that performs two expressions and
4811 returns the value of the second of them.
4813 LOC is the location of the COMPOUND_EXPR. */
4815 tree
4816 build_compound_expr (location_t loc, tree expr1, tree expr2)
4818 bool expr1_int_operands, expr2_int_operands;
4819 tree eptype = NULL_TREE;
4820 tree ret;
4822 if (flag_cilkplus
4823 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4824 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4826 error_at (loc,
4827 "spawned function call cannot be part of a comma expression");
4828 return error_mark_node;
4830 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4831 if (expr1_int_operands)
4832 expr1 = remove_c_maybe_const_expr (expr1);
4833 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4834 if (expr2_int_operands)
4835 expr2 = remove_c_maybe_const_expr (expr2);
4837 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4838 expr1 = TREE_OPERAND (expr1, 0);
4839 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4841 eptype = TREE_TYPE (expr2);
4842 expr2 = TREE_OPERAND (expr2, 0);
4845 if (!TREE_SIDE_EFFECTS (expr1))
4847 /* The left-hand operand of a comma expression is like an expression
4848 statement: with -Wunused, we should warn if it doesn't have
4849 any side-effects, unless it was explicitly cast to (void). */
4850 if (warn_unused_value)
4852 if (VOID_TYPE_P (TREE_TYPE (expr1))
4853 && CONVERT_EXPR_P (expr1))
4854 ; /* (void) a, b */
4855 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4856 && TREE_CODE (expr1) == COMPOUND_EXPR
4857 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4858 ; /* (void) a, (void) b, c */
4859 else
4860 warning_at (loc, OPT_Wunused_value,
4861 "left-hand operand of comma expression has no effect");
4864 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4865 && warn_unused_value)
4867 tree r = expr1;
4868 location_t cloc = loc;
4869 while (TREE_CODE (r) == COMPOUND_EXPR)
4871 if (EXPR_HAS_LOCATION (r))
4872 cloc = EXPR_LOCATION (r);
4873 r = TREE_OPERAND (r, 1);
4875 if (!TREE_SIDE_EFFECTS (r)
4876 && !VOID_TYPE_P (TREE_TYPE (r))
4877 && !CONVERT_EXPR_P (r))
4878 warning_at (cloc, OPT_Wunused_value,
4879 "right-hand operand of comma expression has no effect");
4882 /* With -Wunused, we should also warn if the left-hand operand does have
4883 side-effects, but computes a value which is not used. For example, in
4884 `foo() + bar(), baz()' the result of the `+' operator is not used,
4885 so we should issue a warning. */
4886 else if (warn_unused_value)
4887 warn_if_unused_value (expr1, loc);
4889 if (expr2 == error_mark_node)
4890 return error_mark_node;
4892 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4894 if (flag_isoc99
4895 && expr1_int_operands
4896 && expr2_int_operands)
4897 ret = note_integer_operands (ret);
4899 if (eptype)
4900 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4902 protected_set_expr_location (ret, loc);
4903 return ret;
4906 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4907 which we are casting. OTYPE is the type of the expression being
4908 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4909 of the cast. -Wcast-qual appeared on the command line. Named
4910 address space qualifiers are not handled here, because they result
4911 in different warnings. */
4913 static void
4914 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4916 tree in_type = type;
4917 tree in_otype = otype;
4918 int added = 0;
4919 int discarded = 0;
4920 bool is_const;
4922 /* Check that the qualifiers on IN_TYPE are a superset of the
4923 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4924 nodes is uninteresting and we stop as soon as we hit a
4925 non-POINTER_TYPE node on either type. */
4928 in_otype = TREE_TYPE (in_otype);
4929 in_type = TREE_TYPE (in_type);
4931 /* GNU C allows cv-qualified function types. 'const' means the
4932 function is very pure, 'volatile' means it can't return. We
4933 need to warn when such qualifiers are added, not when they're
4934 taken away. */
4935 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4936 && TREE_CODE (in_type) == FUNCTION_TYPE)
4937 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4938 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4939 else
4940 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4941 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4943 while (TREE_CODE (in_type) == POINTER_TYPE
4944 && TREE_CODE (in_otype) == POINTER_TYPE);
4946 if (added)
4947 warning_at (loc, OPT_Wcast_qual,
4948 "cast adds %q#v qualifier to function type", added);
4950 if (discarded)
4951 /* There are qualifiers present in IN_OTYPE that are not present
4952 in IN_TYPE. */
4953 warning_at (loc, OPT_Wcast_qual,
4954 "cast discards %qv qualifier from pointer target type",
4955 discarded);
4957 if (added || discarded)
4958 return;
4960 /* A cast from **T to const **T is unsafe, because it can cause a
4961 const value to be changed with no additional warning. We only
4962 issue this warning if T is the same on both sides, and we only
4963 issue the warning if there are the same number of pointers on
4964 both sides, as otherwise the cast is clearly unsafe anyhow. A
4965 cast is unsafe when a qualifier is added at one level and const
4966 is not present at all outer levels.
4968 To issue this warning, we check at each level whether the cast
4969 adds new qualifiers not already seen. We don't need to special
4970 case function types, as they won't have the same
4971 TYPE_MAIN_VARIANT. */
4973 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4974 return;
4975 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4976 return;
4978 in_type = type;
4979 in_otype = otype;
4980 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4983 in_type = TREE_TYPE (in_type);
4984 in_otype = TREE_TYPE (in_otype);
4985 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4986 && !is_const)
4988 warning_at (loc, OPT_Wcast_qual,
4989 "to be safe all intermediate pointers in cast from "
4990 "%qT to %qT must be %<const%> qualified",
4991 otype, type);
4992 break;
4994 if (is_const)
4995 is_const = TYPE_READONLY (in_type);
4997 while (TREE_CODE (in_type) == POINTER_TYPE);
5000 /* Build an expression representing a cast to type TYPE of expression EXPR.
5001 LOC is the location of the cast-- typically the open paren of the cast. */
5003 tree
5004 build_c_cast (location_t loc, tree type, tree expr)
5006 tree value;
5008 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5009 expr = TREE_OPERAND (expr, 0);
5011 value = expr;
5013 if (type == error_mark_node || expr == error_mark_node)
5014 return error_mark_node;
5016 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5017 only in <protocol> qualifications. But when constructing cast expressions,
5018 the protocols do matter and must be kept around. */
5019 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5020 return build1 (NOP_EXPR, type, expr);
5022 type = TYPE_MAIN_VARIANT (type);
5024 if (TREE_CODE (type) == ARRAY_TYPE)
5026 error_at (loc, "cast specifies array type");
5027 return error_mark_node;
5030 if (TREE_CODE (type) == FUNCTION_TYPE)
5032 error_at (loc, "cast specifies function type");
5033 return error_mark_node;
5036 if (!VOID_TYPE_P (type))
5038 value = require_complete_type (value);
5039 if (value == error_mark_node)
5040 return error_mark_node;
5043 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5045 if (TREE_CODE (type) == RECORD_TYPE
5046 || TREE_CODE (type) == UNION_TYPE)
5047 pedwarn (loc, OPT_Wpedantic,
5048 "ISO C forbids casting nonscalar to the same type");
5050 /* Convert to remove any qualifiers from VALUE's type. */
5051 value = convert (type, value);
5053 else if (TREE_CODE (type) == UNION_TYPE)
5055 tree field;
5057 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5058 if (TREE_TYPE (field) != error_mark_node
5059 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5060 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5061 break;
5063 if (field)
5065 tree t;
5066 bool maybe_const = true;
5068 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5069 t = c_fully_fold (value, false, &maybe_const);
5070 t = build_constructor_single (type, field, t);
5071 if (!maybe_const)
5072 t = c_wrap_maybe_const (t, true);
5073 t = digest_init (loc, type, t,
5074 NULL_TREE, false, true, 0);
5075 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5076 return t;
5078 error_at (loc, "cast to union type from type not present in union");
5079 return error_mark_node;
5081 else
5083 tree otype, ovalue;
5085 if (type == void_type_node)
5087 tree t = build1 (CONVERT_EXPR, type, value);
5088 SET_EXPR_LOCATION (t, loc);
5089 return t;
5092 otype = TREE_TYPE (value);
5094 /* Optionally warn about potentially worrisome casts. */
5095 if (warn_cast_qual
5096 && TREE_CODE (type) == POINTER_TYPE
5097 && TREE_CODE (otype) == POINTER_TYPE)
5098 handle_warn_cast_qual (loc, type, otype);
5100 /* Warn about conversions between pointers to disjoint
5101 address spaces. */
5102 if (TREE_CODE (type) == POINTER_TYPE
5103 && TREE_CODE (otype) == POINTER_TYPE
5104 && !null_pointer_constant_p (value))
5106 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5107 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5108 addr_space_t as_common;
5110 if (!addr_space_superset (as_to, as_from, &as_common))
5112 if (ADDR_SPACE_GENERIC_P (as_from))
5113 warning_at (loc, 0, "cast to %s address space pointer "
5114 "from disjoint generic address space pointer",
5115 c_addr_space_name (as_to));
5117 else if (ADDR_SPACE_GENERIC_P (as_to))
5118 warning_at (loc, 0, "cast to generic address space pointer "
5119 "from disjoint %s address space pointer",
5120 c_addr_space_name (as_from));
5122 else
5123 warning_at (loc, 0, "cast to %s address space pointer "
5124 "from disjoint %s address space pointer",
5125 c_addr_space_name (as_to),
5126 c_addr_space_name (as_from));
5130 /* Warn about possible alignment problems. */
5131 if (STRICT_ALIGNMENT
5132 && TREE_CODE (type) == POINTER_TYPE
5133 && TREE_CODE (otype) == POINTER_TYPE
5134 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5135 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5136 /* Don't warn about opaque types, where the actual alignment
5137 restriction is unknown. */
5138 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5139 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5140 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5141 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5142 warning_at (loc, OPT_Wcast_align,
5143 "cast increases required alignment of target type");
5145 if (TREE_CODE (type) == INTEGER_TYPE
5146 && TREE_CODE (otype) == POINTER_TYPE
5147 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5148 /* Unlike conversion of integers to pointers, where the
5149 warning is disabled for converting constants because
5150 of cases such as SIG_*, warn about converting constant
5151 pointers to integers. In some cases it may cause unwanted
5152 sign extension, and a warning is appropriate. */
5153 warning_at (loc, OPT_Wpointer_to_int_cast,
5154 "cast from pointer to integer of different size");
5156 if (TREE_CODE (value) == CALL_EXPR
5157 && TREE_CODE (type) != TREE_CODE (otype))
5158 warning_at (loc, OPT_Wbad_function_cast,
5159 "cast from function call of type %qT "
5160 "to non-matching type %qT", otype, type);
5162 if (TREE_CODE (type) == POINTER_TYPE
5163 && TREE_CODE (otype) == INTEGER_TYPE
5164 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5165 /* Don't warn about converting any constant. */
5166 && !TREE_CONSTANT (value))
5167 warning_at (loc,
5168 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5169 "of different size");
5171 if (warn_strict_aliasing <= 2)
5172 strict_aliasing_warning (otype, type, expr);
5174 /* If pedantic, warn for conversions between function and object
5175 pointer types, except for converting a null pointer constant
5176 to function pointer type. */
5177 if (pedantic
5178 && TREE_CODE (type) == POINTER_TYPE
5179 && TREE_CODE (otype) == POINTER_TYPE
5180 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5181 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5182 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5183 "conversion of function pointer to object pointer type");
5185 if (pedantic
5186 && TREE_CODE (type) == POINTER_TYPE
5187 && TREE_CODE (otype) == POINTER_TYPE
5188 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5189 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5190 && !null_pointer_constant_p (value))
5191 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5192 "conversion of object pointer to function pointer type");
5194 ovalue = value;
5195 value = convert (type, value);
5197 /* Ignore any integer overflow caused by the cast. */
5198 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5200 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5202 if (!TREE_OVERFLOW (value))
5204 /* Avoid clobbering a shared constant. */
5205 value = copy_node (value);
5206 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5209 else if (TREE_OVERFLOW (value))
5210 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5211 value = wide_int_to_tree (TREE_TYPE (value), value);
5215 /* Don't let a cast be an lvalue. */
5216 if (value == expr)
5217 value = non_lvalue_loc (loc, value);
5219 /* Don't allow the results of casting to floating-point or complex
5220 types be confused with actual constants, or casts involving
5221 integer and pointer types other than direct integer-to-integer
5222 and integer-to-pointer be confused with integer constant
5223 expressions and null pointer constants. */
5224 if (TREE_CODE (value) == REAL_CST
5225 || TREE_CODE (value) == COMPLEX_CST
5226 || (TREE_CODE (value) == INTEGER_CST
5227 && !((TREE_CODE (expr) == INTEGER_CST
5228 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5229 || TREE_CODE (expr) == REAL_CST
5230 || TREE_CODE (expr) == COMPLEX_CST)))
5231 value = build1 (NOP_EXPR, type, value);
5233 if (CAN_HAVE_LOCATION_P (value))
5234 SET_EXPR_LOCATION (value, loc);
5235 return value;
5238 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5239 location of the open paren of the cast, or the position of the cast
5240 expr. */
5241 tree
5242 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5244 tree type;
5245 tree type_expr = NULL_TREE;
5246 bool type_expr_const = true;
5247 tree ret;
5248 int saved_wsp = warn_strict_prototypes;
5250 /* This avoids warnings about unprototyped casts on
5251 integers. E.g. "#define SIG_DFL (void(*)())0". */
5252 if (TREE_CODE (expr) == INTEGER_CST)
5253 warn_strict_prototypes = 0;
5254 type = groktypename (type_name, &type_expr, &type_expr_const);
5255 warn_strict_prototypes = saved_wsp;
5257 ret = build_c_cast (loc, type, expr);
5258 if (type_expr)
5260 bool inner_expr_const = true;
5261 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5262 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5263 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5264 && inner_expr_const);
5265 SET_EXPR_LOCATION (ret, loc);
5268 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5269 SET_EXPR_LOCATION (ret, loc);
5271 /* C++ does not permits types to be defined in a cast, but it
5272 allows references to incomplete types. */
5273 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5274 warning_at (loc, OPT_Wc___compat,
5275 "defining a type in a cast is invalid in C++");
5277 return ret;
5280 /* Build an assignment expression of lvalue LHS from value RHS.
5281 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5282 may differ from TREE_TYPE (LHS) for an enum bitfield.
5283 MODIFYCODE is the code for a binary operator that we use
5284 to combine the old value of LHS with RHS to get the new value.
5285 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5286 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5287 which may differ from TREE_TYPE (RHS) for an enum value.
5289 LOCATION is the location of the MODIFYCODE operator.
5290 RHS_LOC is the location of the RHS. */
5292 tree
5293 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5294 enum tree_code modifycode,
5295 location_t rhs_loc, tree rhs, tree rhs_origtype)
5297 tree result;
5298 tree newrhs;
5299 tree rhseval = NULL_TREE;
5300 tree rhs_semantic_type = NULL_TREE;
5301 tree lhstype = TREE_TYPE (lhs);
5302 tree olhstype = lhstype;
5303 bool npc;
5304 bool is_atomic_op;
5306 /* Types that aren't fully specified cannot be used in assignments. */
5307 lhs = require_complete_type (lhs);
5309 /* Avoid duplicate error messages from operands that had errors. */
5310 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5311 return error_mark_node;
5313 /* Ensure an error for assigning a non-lvalue array to an array in
5314 C90. */
5315 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5317 error_at (location, "assignment to expression with array type");
5318 return error_mark_node;
5321 /* For ObjC properties, defer this check. */
5322 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5323 return error_mark_node;
5325 is_atomic_op = really_atomic_lvalue (lhs);
5327 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5329 rhs_semantic_type = TREE_TYPE (rhs);
5330 rhs = TREE_OPERAND (rhs, 0);
5333 newrhs = rhs;
5335 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5337 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5338 lhs_origtype, modifycode, rhs_loc, rhs,
5339 rhs_origtype);
5340 if (inner == error_mark_node)
5341 return error_mark_node;
5342 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5343 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5344 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5345 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5346 protected_set_expr_location (result, location);
5347 return result;
5350 /* If a binary op has been requested, combine the old LHS value with the RHS
5351 producing the value we should actually store into the LHS. */
5353 if (modifycode != NOP_EXPR)
5355 lhs = c_fully_fold (lhs, false, NULL);
5356 lhs = stabilize_reference (lhs);
5358 /* Construct the RHS for any non-atomic compound assignemnt. */
5359 if (!is_atomic_op)
5361 /* If in LHS op= RHS the RHS has side-effects, ensure they
5362 are preevaluated before the rest of the assignment expression's
5363 side-effects, because RHS could contain e.g. function calls
5364 that modify LHS. */
5365 if (TREE_SIDE_EFFECTS (rhs))
5367 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5368 rhseval = newrhs;
5370 newrhs = build_binary_op (location,
5371 modifycode, lhs, newrhs, 1);
5373 /* The original type of the right hand side is no longer
5374 meaningful. */
5375 rhs_origtype = NULL_TREE;
5379 if (c_dialect_objc ())
5381 /* Check if we are modifying an Objective-C property reference;
5382 if so, we need to generate setter calls. */
5383 result = objc_maybe_build_modify_expr (lhs, newrhs);
5384 if (result)
5385 goto return_result;
5387 /* Else, do the check that we postponed for Objective-C. */
5388 if (!lvalue_or_else (location, lhs, lv_assign))
5389 return error_mark_node;
5392 /* Give an error for storing in something that is 'const'. */
5394 if (TYPE_READONLY (lhstype)
5395 || ((TREE_CODE (lhstype) == RECORD_TYPE
5396 || TREE_CODE (lhstype) == UNION_TYPE)
5397 && C_TYPE_FIELDS_READONLY (lhstype)))
5399 readonly_error (location, lhs, lv_assign);
5400 return error_mark_node;
5402 else if (TREE_READONLY (lhs))
5403 readonly_warning (lhs, lv_assign);
5405 /* If storing into a structure or union member,
5406 it has probably been given type `int'.
5407 Compute the type that would go with
5408 the actual amount of storage the member occupies. */
5410 if (TREE_CODE (lhs) == COMPONENT_REF
5411 && (TREE_CODE (lhstype) == INTEGER_TYPE
5412 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5413 || TREE_CODE (lhstype) == REAL_TYPE
5414 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5415 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5417 /* If storing in a field that is in actuality a short or narrower than one,
5418 we must store in the field in its actual type. */
5420 if (lhstype != TREE_TYPE (lhs))
5422 lhs = copy_node (lhs);
5423 TREE_TYPE (lhs) = lhstype;
5426 /* Issue -Wc++-compat warnings about an assignment to an enum type
5427 when LHS does not have its original type. This happens for,
5428 e.g., an enum bitfield in a struct. */
5429 if (warn_cxx_compat
5430 && lhs_origtype != NULL_TREE
5431 && lhs_origtype != lhstype
5432 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5434 tree checktype = (rhs_origtype != NULL_TREE
5435 ? rhs_origtype
5436 : TREE_TYPE (rhs));
5437 if (checktype != error_mark_node
5438 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5439 || (is_atomic_op && modifycode != NOP_EXPR)))
5440 warning_at (location, OPT_Wc___compat,
5441 "enum conversion in assignment is invalid in C++");
5444 /* If the lhs is atomic, remove that qualifier. */
5445 if (is_atomic_op)
5447 lhstype = build_qualified_type (lhstype,
5448 (TYPE_QUALS (lhstype)
5449 & ~TYPE_QUAL_ATOMIC));
5450 olhstype = build_qualified_type (olhstype,
5451 (TYPE_QUALS (lhstype)
5452 & ~TYPE_QUAL_ATOMIC));
5455 /* Convert new value to destination type. Fold it first, then
5456 restore any excess precision information, for the sake of
5457 conversion warnings. */
5459 if (!(is_atomic_op && modifycode != NOP_EXPR))
5461 npc = null_pointer_constant_p (newrhs);
5462 newrhs = c_fully_fold (newrhs, false, NULL);
5463 if (rhs_semantic_type)
5464 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5465 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5466 rhs_origtype, ic_assign, npc,
5467 NULL_TREE, NULL_TREE, 0);
5468 if (TREE_CODE (newrhs) == ERROR_MARK)
5469 return error_mark_node;
5472 /* Emit ObjC write barrier, if necessary. */
5473 if (c_dialect_objc () && flag_objc_gc)
5475 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5476 if (result)
5478 protected_set_expr_location (result, location);
5479 goto return_result;
5483 /* Scan operands. */
5485 if (is_atomic_op)
5486 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5487 else
5489 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5490 TREE_SIDE_EFFECTS (result) = 1;
5491 protected_set_expr_location (result, location);
5494 /* If we got the LHS in a different type for storing in,
5495 convert the result back to the nominal type of LHS
5496 so that the value we return always has the same type
5497 as the LHS argument. */
5499 if (olhstype == TREE_TYPE (result))
5500 goto return_result;
5502 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5503 rhs_origtype, ic_assign, false, NULL_TREE,
5504 NULL_TREE, 0);
5505 protected_set_expr_location (result, location);
5507 return_result:
5508 if (rhseval)
5509 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5510 return result;
5513 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5514 This is used to implement -fplan9-extensions. */
5516 static bool
5517 find_anonymous_field_with_type (tree struct_type, tree type)
5519 tree field;
5520 bool found;
5522 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5523 || TREE_CODE (struct_type) == UNION_TYPE);
5524 found = false;
5525 for (field = TYPE_FIELDS (struct_type);
5526 field != NULL_TREE;
5527 field = TREE_CHAIN (field))
5529 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5530 ? c_build_qualified_type (TREE_TYPE (field),
5531 TYPE_QUAL_ATOMIC)
5532 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5533 if (DECL_NAME (field) == NULL
5534 && comptypes (type, fieldtype))
5536 if (found)
5537 return false;
5538 found = true;
5540 else if (DECL_NAME (field) == NULL
5541 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5542 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5543 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5545 if (found)
5546 return false;
5547 found = true;
5550 return found;
5553 /* RHS is an expression whose type is pointer to struct. If there is
5554 an anonymous field in RHS with type TYPE, then return a pointer to
5555 that field in RHS. This is used with -fplan9-extensions. This
5556 returns NULL if no conversion could be found. */
5558 static tree
5559 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5561 tree rhs_struct_type, lhs_main_type;
5562 tree field, found_field;
5563 bool found_sub_field;
5564 tree ret;
5566 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5567 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5568 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5569 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5571 gcc_assert (POINTER_TYPE_P (type));
5572 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5573 ? c_build_qualified_type (TREE_TYPE (type),
5574 TYPE_QUAL_ATOMIC)
5575 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5577 found_field = NULL_TREE;
5578 found_sub_field = false;
5579 for (field = TYPE_FIELDS (rhs_struct_type);
5580 field != NULL_TREE;
5581 field = TREE_CHAIN (field))
5583 if (DECL_NAME (field) != NULL_TREE
5584 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5585 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5586 continue;
5587 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5588 ? c_build_qualified_type (TREE_TYPE (field),
5589 TYPE_QUAL_ATOMIC)
5590 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5591 if (comptypes (lhs_main_type, fieldtype))
5593 if (found_field != NULL_TREE)
5594 return NULL_TREE;
5595 found_field = field;
5597 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5598 lhs_main_type))
5600 if (found_field != NULL_TREE)
5601 return NULL_TREE;
5602 found_field = field;
5603 found_sub_field = true;
5607 if (found_field == NULL_TREE)
5608 return NULL_TREE;
5610 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5611 build_fold_indirect_ref (rhs), found_field,
5612 NULL_TREE);
5613 ret = build_fold_addr_expr_loc (location, ret);
5615 if (found_sub_field)
5617 ret = convert_to_anonymous_field (location, type, ret);
5618 gcc_assert (ret != NULL_TREE);
5621 return ret;
5624 /* Issue an error message for a bad initializer component.
5625 GMSGID identifies the message.
5626 The component name is taken from the spelling stack. */
5628 static void
5629 error_init (location_t loc, const char *gmsgid)
5631 char *ofwhat;
5633 /* The gmsgid may be a format string with %< and %>. */
5634 error_at (loc, gmsgid);
5635 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5636 if (*ofwhat)
5637 inform (loc, "(near initialization for %qs)", ofwhat);
5640 /* Issue a pedantic warning for a bad initializer component. OPT is
5641 the option OPT_* (from options.h) controlling this warning or 0 if
5642 it is unconditionally given. GMSGID identifies the message. The
5643 component name is taken from the spelling stack. */
5645 static void
5646 pedwarn_init (location_t location, int opt, const char *gmsgid)
5648 char *ofwhat;
5649 bool warned;
5651 /* The gmsgid may be a format string with %< and %>. */
5652 warned = pedwarn (location, opt, gmsgid);
5653 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5654 if (*ofwhat && warned)
5655 inform (location, "(near initialization for %qs)", ofwhat);
5658 /* Issue a warning for a bad initializer component.
5660 OPT is the OPT_W* value corresponding to the warning option that
5661 controls this warning. GMSGID identifies the message. The
5662 component name is taken from the spelling stack. */
5664 static void
5665 warning_init (location_t loc, int opt, const char *gmsgid)
5667 char *ofwhat;
5668 bool warned;
5670 /* The gmsgid may be a format string with %< and %>. */
5671 warned = warning_at (loc, opt, gmsgid);
5672 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5673 if (*ofwhat && warned)
5674 inform (loc, "(near initialization for %qs)", ofwhat);
5677 /* If TYPE is an array type and EXPR is a parenthesized string
5678 constant, warn if pedantic that EXPR is being used to initialize an
5679 object of type TYPE. */
5681 void
5682 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5684 if (pedantic
5685 && TREE_CODE (type) == ARRAY_TYPE
5686 && TREE_CODE (expr.value) == STRING_CST
5687 && expr.original_code != STRING_CST)
5688 pedwarn_init (loc, OPT_Wpedantic,
5689 "array initialized from parenthesized string constant");
5692 /* Convert value RHS to type TYPE as preparation for an assignment to
5693 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5694 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5695 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5696 constant before any folding.
5697 The real work of conversion is done by `convert'.
5698 The purpose of this function is to generate error messages
5699 for assignments that are not allowed in C.
5700 ERRTYPE says whether it is argument passing, assignment,
5701 initialization or return.
5703 LOCATION is the location of the assignment, EXPR_LOC is the location of
5704 the RHS or, for a function, location of an argument.
5705 FUNCTION is a tree for the function being called.
5706 PARMNUM is the number of the argument, for printing in error messages. */
5708 static tree
5709 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5710 tree rhs, tree origtype, enum impl_conv errtype,
5711 bool null_pointer_constant, tree fundecl,
5712 tree function, int parmnum)
5714 enum tree_code codel = TREE_CODE (type);
5715 tree orig_rhs = rhs;
5716 tree rhstype;
5717 enum tree_code coder;
5718 tree rname = NULL_TREE;
5719 bool objc_ok = false;
5721 if (errtype == ic_argpass)
5723 tree selector;
5724 /* Change pointer to function to the function itself for
5725 diagnostics. */
5726 if (TREE_CODE (function) == ADDR_EXPR
5727 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5728 function = TREE_OPERAND (function, 0);
5730 /* Handle an ObjC selector specially for diagnostics. */
5731 selector = objc_message_selector ();
5732 rname = function;
5733 if (selector && parmnum > 2)
5735 rname = selector;
5736 parmnum -= 2;
5740 /* This macro is used to emit diagnostics to ensure that all format
5741 strings are complete sentences, visible to gettext and checked at
5742 compile time. */
5743 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5744 do { \
5745 switch (errtype) \
5747 case ic_argpass: \
5748 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5749 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5750 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5751 "expected %qT but argument is of type %qT", \
5752 type, rhstype); \
5753 break; \
5754 case ic_assign: \
5755 pedwarn (LOCATION, OPT, AS); \
5756 break; \
5757 case ic_init: \
5758 pedwarn_init (LOCATION, OPT, IN); \
5759 break; \
5760 case ic_return: \
5761 pedwarn (LOCATION, OPT, RE); \
5762 break; \
5763 default: \
5764 gcc_unreachable (); \
5766 } while (0)
5768 /* This macro is used to emit diagnostics to ensure that all format
5769 strings are complete sentences, visible to gettext and checked at
5770 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
5771 extra parameter to enumerate qualifiers. */
5772 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5773 do { \
5774 switch (errtype) \
5776 case ic_argpass: \
5777 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5778 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5779 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5780 "expected %qT but argument is of type %qT", \
5781 type, rhstype); \
5782 break; \
5783 case ic_assign: \
5784 pedwarn (LOCATION, OPT, AS, QUALS); \
5785 break; \
5786 case ic_init: \
5787 pedwarn (LOCATION, OPT, IN, QUALS); \
5788 break; \
5789 case ic_return: \
5790 pedwarn (LOCATION, OPT, RE, QUALS); \
5791 break; \
5792 default: \
5793 gcc_unreachable (); \
5795 } while (0)
5797 /* This macro is used to emit diagnostics to ensure that all format
5798 strings are complete sentences, visible to gettext and checked at
5799 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5800 warning_at instead of pedwarn. */
5801 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5802 do { \
5803 switch (errtype) \
5805 case ic_argpass: \
5806 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5807 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5808 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5809 "expected %qT but argument is of type %qT", \
5810 type, rhstype); \
5811 break; \
5812 case ic_assign: \
5813 warning_at (LOCATION, OPT, AS, QUALS); \
5814 break; \
5815 case ic_init: \
5816 warning_at (LOCATION, OPT, IN, QUALS); \
5817 break; \
5818 case ic_return: \
5819 warning_at (LOCATION, OPT, RE, QUALS); \
5820 break; \
5821 default: \
5822 gcc_unreachable (); \
5824 } while (0)
5826 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5827 rhs = TREE_OPERAND (rhs, 0);
5829 rhstype = TREE_TYPE (rhs);
5830 coder = TREE_CODE (rhstype);
5832 if (coder == ERROR_MARK)
5833 return error_mark_node;
5835 if (c_dialect_objc ())
5837 int parmno;
5839 switch (errtype)
5841 case ic_return:
5842 parmno = 0;
5843 break;
5845 case ic_assign:
5846 parmno = -1;
5847 break;
5849 case ic_init:
5850 parmno = -2;
5851 break;
5853 default:
5854 parmno = parmnum;
5855 break;
5858 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5861 if (warn_cxx_compat)
5863 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5864 if (checktype != error_mark_node
5865 && TREE_CODE (type) == ENUMERAL_TYPE
5866 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5868 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5869 G_("enum conversion when passing argument "
5870 "%d of %qE is invalid in C++"),
5871 G_("enum conversion in assignment is "
5872 "invalid in C++"),
5873 G_("enum conversion in initialization is "
5874 "invalid in C++"),
5875 G_("enum conversion in return is "
5876 "invalid in C++"));
5880 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5881 return rhs;
5883 if (coder == VOID_TYPE)
5885 /* Except for passing an argument to an unprototyped function,
5886 this is a constraint violation. When passing an argument to
5887 an unprototyped function, it is compile-time undefined;
5888 making it a constraint in that case was rejected in
5889 DR#252. */
5890 error_at (location, "void value not ignored as it ought to be");
5891 return error_mark_node;
5893 rhs = require_complete_type (rhs);
5894 if (rhs == error_mark_node)
5895 return error_mark_node;
5896 /* A non-reference type can convert to a reference. This handles
5897 va_start, va_copy and possibly port built-ins. */
5898 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5900 if (!lvalue_p (rhs))
5902 error_at (location, "cannot pass rvalue to reference parameter");
5903 return error_mark_node;
5905 if (!c_mark_addressable (rhs))
5906 return error_mark_node;
5907 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5908 SET_EXPR_LOCATION (rhs, location);
5910 rhs = convert_for_assignment (location, expr_loc,
5911 build_pointer_type (TREE_TYPE (type)),
5912 rhs, origtype, errtype,
5913 null_pointer_constant, fundecl, function,
5914 parmnum);
5915 if (rhs == error_mark_node)
5916 return error_mark_node;
5918 rhs = build1 (NOP_EXPR, type, rhs);
5919 SET_EXPR_LOCATION (rhs, location);
5920 return rhs;
5922 /* Some types can interconvert without explicit casts. */
5923 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5924 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5925 return convert (type, rhs);
5926 /* Arithmetic types all interconvert, and enum is treated like int. */
5927 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5928 || codel == FIXED_POINT_TYPE
5929 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5930 || codel == BOOLEAN_TYPE)
5931 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5932 || coder == FIXED_POINT_TYPE
5933 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5934 || coder == BOOLEAN_TYPE))
5936 tree ret;
5937 bool save = in_late_binary_op;
5938 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
5939 || (coder == REAL_TYPE
5940 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
5941 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
5942 in_late_binary_op = true;
5943 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5944 ? expr_loc : location, type, orig_rhs);
5945 in_late_binary_op = save;
5946 return ret;
5949 /* Aggregates in different TUs might need conversion. */
5950 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5951 && codel == coder
5952 && comptypes (type, rhstype))
5953 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5954 ? expr_loc : location, type, rhs);
5956 /* Conversion to a transparent union or record from its member types.
5957 This applies only to function arguments. */
5958 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5959 && TYPE_TRANSPARENT_AGGR (type))
5960 && errtype == ic_argpass)
5962 tree memb, marginal_memb = NULL_TREE;
5964 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5966 tree memb_type = TREE_TYPE (memb);
5968 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5969 TYPE_MAIN_VARIANT (rhstype)))
5970 break;
5972 if (TREE_CODE (memb_type) != POINTER_TYPE)
5973 continue;
5975 if (coder == POINTER_TYPE)
5977 tree ttl = TREE_TYPE (memb_type);
5978 tree ttr = TREE_TYPE (rhstype);
5980 /* Any non-function converts to a [const][volatile] void *
5981 and vice versa; otherwise, targets must be the same.
5982 Meanwhile, the lhs target must have all the qualifiers of
5983 the rhs. */
5984 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5985 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5986 || comp_target_types (location, memb_type, rhstype))
5988 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5989 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5990 /* If this type won't generate any warnings, use it. */
5991 if (lquals == rquals
5992 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5993 && TREE_CODE (ttl) == FUNCTION_TYPE)
5994 ? ((lquals | rquals) == rquals)
5995 : ((lquals | rquals) == lquals)))
5996 break;
5998 /* Keep looking for a better type, but remember this one. */
5999 if (!marginal_memb)
6000 marginal_memb = memb;
6004 /* Can convert integer zero to any pointer type. */
6005 if (null_pointer_constant)
6007 rhs = null_pointer_node;
6008 break;
6012 if (memb || marginal_memb)
6014 if (!memb)
6016 /* We have only a marginally acceptable member type;
6017 it needs a warning. */
6018 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6019 tree ttr = TREE_TYPE (rhstype);
6021 /* Const and volatile mean something different for function
6022 types, so the usual warnings are not appropriate. */
6023 if (TREE_CODE (ttr) == FUNCTION_TYPE
6024 && TREE_CODE (ttl) == FUNCTION_TYPE)
6026 /* Because const and volatile on functions are
6027 restrictions that say the function will not do
6028 certain things, it is okay to use a const or volatile
6029 function where an ordinary one is wanted, but not
6030 vice-versa. */
6031 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6032 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6033 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6034 OPT_Wdiscarded_qualifiers,
6035 G_("passing argument %d of %qE "
6036 "makes %q#v qualified function "
6037 "pointer from unqualified"),
6038 G_("assignment makes %q#v qualified "
6039 "function pointer from "
6040 "unqualified"),
6041 G_("initialization makes %q#v qualified "
6042 "function pointer from "
6043 "unqualified"),
6044 G_("return makes %q#v qualified function "
6045 "pointer from unqualified"),
6046 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6048 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6049 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6050 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6051 OPT_Wdiscarded_qualifiers,
6052 G_("passing argument %d of %qE discards "
6053 "%qv qualifier from pointer target type"),
6054 G_("assignment discards %qv qualifier "
6055 "from pointer target type"),
6056 G_("initialization discards %qv qualifier "
6057 "from pointer target type"),
6058 G_("return discards %qv qualifier from "
6059 "pointer target type"),
6060 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6062 memb = marginal_memb;
6065 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6066 pedwarn (location, OPT_Wpedantic,
6067 "ISO C prohibits argument conversion to union type");
6069 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6070 return build_constructor_single (type, memb, rhs);
6074 /* Conversions among pointers */
6075 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6076 && (coder == codel))
6078 tree ttl = TREE_TYPE (type);
6079 tree ttr = TREE_TYPE (rhstype);
6080 tree mvl = ttl;
6081 tree mvr = ttr;
6082 bool is_opaque_pointer;
6083 int target_cmp = 0; /* Cache comp_target_types () result. */
6084 addr_space_t asl;
6085 addr_space_t asr;
6087 if (TREE_CODE (mvl) != ARRAY_TYPE)
6088 mvl = (TYPE_ATOMIC (mvl)
6089 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6090 TYPE_QUAL_ATOMIC)
6091 : TYPE_MAIN_VARIANT (mvl));
6092 if (TREE_CODE (mvr) != ARRAY_TYPE)
6093 mvr = (TYPE_ATOMIC (mvr)
6094 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6095 TYPE_QUAL_ATOMIC)
6096 : TYPE_MAIN_VARIANT (mvr));
6097 /* Opaque pointers are treated like void pointers. */
6098 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6100 /* The Plan 9 compiler permits a pointer to a struct to be
6101 automatically converted into a pointer to an anonymous field
6102 within the struct. */
6103 if (flag_plan9_extensions
6104 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6105 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6106 && mvl != mvr)
6108 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6109 if (new_rhs != NULL_TREE)
6111 rhs = new_rhs;
6112 rhstype = TREE_TYPE (rhs);
6113 coder = TREE_CODE (rhstype);
6114 ttr = TREE_TYPE (rhstype);
6115 mvr = TYPE_MAIN_VARIANT (ttr);
6119 /* C++ does not allow the implicit conversion void* -> T*. However,
6120 for the purpose of reducing the number of false positives, we
6121 tolerate the special case of
6123 int *p = NULL;
6125 where NULL is typically defined in C to be '(void *) 0'. */
6126 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6127 warning_at (errtype == ic_argpass ? expr_loc : location,
6128 OPT_Wc___compat,
6129 "request for implicit conversion "
6130 "from %qT to %qT not permitted in C++", rhstype, type);
6132 /* See if the pointers point to incompatible address spaces. */
6133 asl = TYPE_ADDR_SPACE (ttl);
6134 asr = TYPE_ADDR_SPACE (ttr);
6135 if (!null_pointer_constant_p (rhs)
6136 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6138 switch (errtype)
6140 case ic_argpass:
6141 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6142 "non-enclosed address space", parmnum, rname);
6143 break;
6144 case ic_assign:
6145 error_at (location, "assignment from pointer to "
6146 "non-enclosed address space");
6147 break;
6148 case ic_init:
6149 error_at (location, "initialization from pointer to "
6150 "non-enclosed address space");
6151 break;
6152 case ic_return:
6153 error_at (location, "return from pointer to "
6154 "non-enclosed address space");
6155 break;
6156 default:
6157 gcc_unreachable ();
6159 return error_mark_node;
6162 /* Check if the right-hand side has a format attribute but the
6163 left-hand side doesn't. */
6164 if (warn_suggest_attribute_format
6165 && check_missing_format_attribute (type, rhstype))
6167 switch (errtype)
6169 case ic_argpass:
6170 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6171 "argument %d of %qE might be "
6172 "a candidate for a format attribute",
6173 parmnum, rname);
6174 break;
6175 case ic_assign:
6176 warning_at (location, OPT_Wsuggest_attribute_format,
6177 "assignment left-hand side might be "
6178 "a candidate for a format attribute");
6179 break;
6180 case ic_init:
6181 warning_at (location, OPT_Wsuggest_attribute_format,
6182 "initialization left-hand side might be "
6183 "a candidate for a format attribute");
6184 break;
6185 case ic_return:
6186 warning_at (location, OPT_Wsuggest_attribute_format,
6187 "return type might be "
6188 "a candidate for a format attribute");
6189 break;
6190 default:
6191 gcc_unreachable ();
6195 /* Any non-function converts to a [const][volatile] void *
6196 and vice versa; otherwise, targets must be the same.
6197 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6198 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6199 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6200 || (target_cmp = comp_target_types (location, type, rhstype))
6201 || is_opaque_pointer
6202 || ((c_common_unsigned_type (mvl)
6203 == c_common_unsigned_type (mvr))
6204 && (c_common_signed_type (mvl)
6205 == c_common_signed_type (mvr))
6206 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6208 /* Warn about loss of qualifers from pointers to arrays with
6209 qualifiers on the element type. */
6210 if (TREE_CODE (ttr) == ARRAY_TYPE)
6212 ttr = strip_array_types (ttr);
6213 ttl = strip_array_types (ttl);
6215 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6216 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6217 WARNING_FOR_QUALIFIERS (location, expr_loc,
6218 OPT_Wdiscarded_array_qualifiers,
6219 G_("passing argument %d of %qE discards "
6220 "%qv qualifier from pointer target type"),
6221 G_("assignment discards %qv qualifier "
6222 "from pointer target type"),
6223 G_("initialization discards %qv qualifier "
6224 "from pointer target type"),
6225 G_("return discards %qv qualifier from "
6226 "pointer target type"),
6227 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6229 else if (pedantic
6230 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6232 (VOID_TYPE_P (ttr)
6233 && !null_pointer_constant
6234 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6235 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6236 G_("ISO C forbids passing argument %d of "
6237 "%qE between function pointer "
6238 "and %<void *%>"),
6239 G_("ISO C forbids assignment between "
6240 "function pointer and %<void *%>"),
6241 G_("ISO C forbids initialization between "
6242 "function pointer and %<void *%>"),
6243 G_("ISO C forbids return between function "
6244 "pointer and %<void *%>"));
6245 /* Const and volatile mean something different for function types,
6246 so the usual warnings are not appropriate. */
6247 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6248 && TREE_CODE (ttl) != FUNCTION_TYPE)
6250 /* Don't warn about loss of qualifier for conversions from
6251 qualified void* to pointers to arrays with corresponding
6252 qualifier on the element type. */
6253 if (!pedantic)
6254 ttl = strip_array_types (ttl);
6256 /* Assignments between atomic and non-atomic objects are OK. */
6257 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6258 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6260 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6261 OPT_Wdiscarded_qualifiers,
6262 G_("passing argument %d of %qE discards "
6263 "%qv qualifier from pointer target type"),
6264 G_("assignment discards %qv qualifier "
6265 "from pointer target type"),
6266 G_("initialization discards %qv qualifier "
6267 "from pointer target type"),
6268 G_("return discards %qv qualifier from "
6269 "pointer target type"),
6270 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6272 /* If this is not a case of ignoring a mismatch in signedness,
6273 no warning. */
6274 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6275 || target_cmp)
6277 /* If there is a mismatch, do warn. */
6278 else if (warn_pointer_sign)
6279 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6280 G_("pointer targets in passing argument "
6281 "%d of %qE differ in signedness"),
6282 G_("pointer targets in assignment "
6283 "differ in signedness"),
6284 G_("pointer targets in initialization "
6285 "differ in signedness"),
6286 G_("pointer targets in return differ "
6287 "in signedness"));
6289 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6290 && TREE_CODE (ttr) == FUNCTION_TYPE)
6292 /* Because const and volatile on functions are restrictions
6293 that say the function will not do certain things,
6294 it is okay to use a const or volatile function
6295 where an ordinary one is wanted, but not vice-versa. */
6296 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6297 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6298 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6299 OPT_Wdiscarded_qualifiers,
6300 G_("passing argument %d of %qE makes "
6301 "%q#v qualified function pointer "
6302 "from unqualified"),
6303 G_("assignment makes %q#v qualified function "
6304 "pointer from unqualified"),
6305 G_("initialization makes %q#v qualified "
6306 "function pointer from unqualified"),
6307 G_("return makes %q#v qualified function "
6308 "pointer from unqualified"),
6309 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6312 else
6313 /* Avoid warning about the volatile ObjC EH puts on decls. */
6314 if (!objc_ok)
6315 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6316 OPT_Wincompatible_pointer_types,
6317 G_("passing argument %d of %qE from "
6318 "incompatible pointer type"),
6319 G_("assignment from incompatible pointer type"),
6320 G_("initialization from incompatible "
6321 "pointer type"),
6322 G_("return from incompatible pointer type"));
6324 return convert (type, rhs);
6326 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6328 /* ??? This should not be an error when inlining calls to
6329 unprototyped functions. */
6330 error_at (location, "invalid use of non-lvalue array");
6331 return error_mark_node;
6333 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6335 /* An explicit constant 0 can convert to a pointer,
6336 or one that results from arithmetic, even including
6337 a cast to integer type. */
6338 if (!null_pointer_constant)
6339 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6340 OPT_Wint_conversion,
6341 G_("passing argument %d of %qE makes "
6342 "pointer from integer without a cast"),
6343 G_("assignment makes pointer from integer "
6344 "without a cast"),
6345 G_("initialization makes pointer from "
6346 "integer without a cast"),
6347 G_("return makes pointer from integer "
6348 "without a cast"));
6350 return convert (type, rhs);
6352 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6354 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6355 OPT_Wint_conversion,
6356 G_("passing argument %d of %qE makes integer "
6357 "from pointer without a cast"),
6358 G_("assignment makes integer from pointer "
6359 "without a cast"),
6360 G_("initialization makes integer from pointer "
6361 "without a cast"),
6362 G_("return makes integer from pointer "
6363 "without a cast"));
6364 return convert (type, rhs);
6366 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6368 tree ret;
6369 bool save = in_late_binary_op;
6370 in_late_binary_op = true;
6371 ret = convert (type, rhs);
6372 in_late_binary_op = save;
6373 return ret;
6376 switch (errtype)
6378 case ic_argpass:
6379 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6380 rname);
6381 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6382 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6383 "expected %qT but argument is of type %qT", type, rhstype);
6384 break;
6385 case ic_assign:
6386 error_at (location, "incompatible types when assigning to type %qT from "
6387 "type %qT", type, rhstype);
6388 break;
6389 case ic_init:
6390 error_at (location,
6391 "incompatible types when initializing type %qT using type %qT",
6392 type, rhstype);
6393 break;
6394 case ic_return:
6395 error_at (location,
6396 "incompatible types when returning type %qT but %qT was "
6397 "expected", rhstype, type);
6398 break;
6399 default:
6400 gcc_unreachable ();
6403 return error_mark_node;
6406 /* If VALUE is a compound expr all of whose expressions are constant, then
6407 return its value. Otherwise, return error_mark_node.
6409 This is for handling COMPOUND_EXPRs as initializer elements
6410 which is allowed with a warning when -pedantic is specified. */
6412 static tree
6413 valid_compound_expr_initializer (tree value, tree endtype)
6415 if (TREE_CODE (value) == COMPOUND_EXPR)
6417 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6418 == error_mark_node)
6419 return error_mark_node;
6420 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6421 endtype);
6423 else if (!initializer_constant_valid_p (value, endtype))
6424 return error_mark_node;
6425 else
6426 return value;
6429 /* Perform appropriate conversions on the initial value of a variable,
6430 store it in the declaration DECL,
6431 and print any error messages that are appropriate.
6432 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6433 If the init is invalid, store an ERROR_MARK.
6435 INIT_LOC is the location of the initial value. */
6437 void
6438 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6440 tree value, type;
6441 bool npc = false;
6443 /* If variable's type was invalidly declared, just ignore it. */
6445 type = TREE_TYPE (decl);
6446 if (TREE_CODE (type) == ERROR_MARK)
6447 return;
6449 /* Digest the specified initializer into an expression. */
6451 if (init)
6452 npc = null_pointer_constant_p (init);
6453 value = digest_init (init_loc, type, init, origtype, npc,
6454 true, TREE_STATIC (decl));
6456 /* Store the expression if valid; else report error. */
6458 if (!in_system_header_at (input_location)
6459 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6460 warning (OPT_Wtraditional, "traditional C rejects automatic "
6461 "aggregate initialization");
6463 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6464 DECL_INITIAL (decl) = value;
6466 /* ANSI wants warnings about out-of-range constant initializers. */
6467 STRIP_TYPE_NOPS (value);
6468 if (TREE_STATIC (decl))
6469 constant_expression_warning (value);
6471 /* Check if we need to set array size from compound literal size. */
6472 if (TREE_CODE (type) == ARRAY_TYPE
6473 && TYPE_DOMAIN (type) == 0
6474 && value != error_mark_node)
6476 tree inside_init = init;
6478 STRIP_TYPE_NOPS (inside_init);
6479 inside_init = fold (inside_init);
6481 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6483 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6485 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6487 /* For int foo[] = (int [3]){1}; we need to set array size
6488 now since later on array initializer will be just the
6489 brace enclosed list of the compound literal. */
6490 tree etype = strip_array_types (TREE_TYPE (decl));
6491 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6492 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6493 layout_type (type);
6494 layout_decl (cldecl, 0);
6495 TREE_TYPE (decl)
6496 = c_build_qualified_type (type, TYPE_QUALS (etype));
6502 /* Methods for storing and printing names for error messages. */
6504 /* Implement a spelling stack that allows components of a name to be pushed
6505 and popped. Each element on the stack is this structure. */
6507 struct spelling
6509 int kind;
6510 union
6512 unsigned HOST_WIDE_INT i;
6513 const char *s;
6514 } u;
6517 #define SPELLING_STRING 1
6518 #define SPELLING_MEMBER 2
6519 #define SPELLING_BOUNDS 3
6521 static struct spelling *spelling; /* Next stack element (unused). */
6522 static struct spelling *spelling_base; /* Spelling stack base. */
6523 static int spelling_size; /* Size of the spelling stack. */
6525 /* Macros to save and restore the spelling stack around push_... functions.
6526 Alternative to SAVE_SPELLING_STACK. */
6528 #define SPELLING_DEPTH() (spelling - spelling_base)
6529 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6531 /* Push an element on the spelling stack with type KIND and assign VALUE
6532 to MEMBER. */
6534 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6536 int depth = SPELLING_DEPTH (); \
6538 if (depth >= spelling_size) \
6540 spelling_size += 10; \
6541 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6542 spelling_size); \
6543 RESTORE_SPELLING_DEPTH (depth); \
6546 spelling->kind = (KIND); \
6547 spelling->MEMBER = (VALUE); \
6548 spelling++; \
6551 /* Push STRING on the stack. Printed literally. */
6553 static void
6554 push_string (const char *string)
6556 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6559 /* Push a member name on the stack. Printed as '.' STRING. */
6561 static void
6562 push_member_name (tree decl)
6564 const char *const string
6565 = (DECL_NAME (decl)
6566 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6567 : _("<anonymous>"));
6568 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6571 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6573 static void
6574 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6576 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6579 /* Compute the maximum size in bytes of the printed spelling. */
6581 static int
6582 spelling_length (void)
6584 int size = 0;
6585 struct spelling *p;
6587 for (p = spelling_base; p < spelling; p++)
6589 if (p->kind == SPELLING_BOUNDS)
6590 size += 25;
6591 else
6592 size += strlen (p->u.s) + 1;
6595 return size;
6598 /* Print the spelling to BUFFER and return it. */
6600 static char *
6601 print_spelling (char *buffer)
6603 char *d = buffer;
6604 struct spelling *p;
6606 for (p = spelling_base; p < spelling; p++)
6607 if (p->kind == SPELLING_BOUNDS)
6609 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6610 d += strlen (d);
6612 else
6614 const char *s;
6615 if (p->kind == SPELLING_MEMBER)
6616 *d++ = '.';
6617 for (s = p->u.s; (*d = *s++); d++)
6620 *d++ = '\0';
6621 return buffer;
6624 /* Digest the parser output INIT as an initializer for type TYPE.
6625 Return a C expression of type TYPE to represent the initial value.
6627 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6629 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6631 If INIT is a string constant, STRICT_STRING is true if it is
6632 unparenthesized or we should not warn here for it being parenthesized.
6633 For other types of INIT, STRICT_STRING is not used.
6635 INIT_LOC is the location of the INIT.
6637 REQUIRE_CONSTANT requests an error if non-constant initializers or
6638 elements are seen. */
6640 static tree
6641 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6642 bool null_pointer_constant, bool strict_string,
6643 int require_constant)
6645 enum tree_code code = TREE_CODE (type);
6646 tree inside_init = init;
6647 tree semantic_type = NULL_TREE;
6648 bool maybe_const = true;
6650 if (type == error_mark_node
6651 || !init
6652 || error_operand_p (init))
6653 return error_mark_node;
6655 STRIP_TYPE_NOPS (inside_init);
6657 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6659 semantic_type = TREE_TYPE (inside_init);
6660 inside_init = TREE_OPERAND (inside_init, 0);
6662 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6663 inside_init = decl_constant_value_for_optimization (inside_init);
6665 /* Initialization of an array of chars from a string constant
6666 optionally enclosed in braces. */
6668 if (code == ARRAY_TYPE && inside_init
6669 && TREE_CODE (inside_init) == STRING_CST)
6671 tree typ1
6672 = (TYPE_ATOMIC (TREE_TYPE (type))
6673 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6674 TYPE_QUAL_ATOMIC)
6675 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6676 /* Note that an array could be both an array of character type
6677 and an array of wchar_t if wchar_t is signed char or unsigned
6678 char. */
6679 bool char_array = (typ1 == char_type_node
6680 || typ1 == signed_char_type_node
6681 || typ1 == unsigned_char_type_node);
6682 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6683 bool char16_array = !!comptypes (typ1, char16_type_node);
6684 bool char32_array = !!comptypes (typ1, char32_type_node);
6686 if (char_array || wchar_array || char16_array || char32_array)
6688 struct c_expr expr;
6689 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6690 expr.value = inside_init;
6691 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6692 expr.original_type = NULL;
6693 maybe_warn_string_init (init_loc, type, expr);
6695 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6696 pedwarn_init (init_loc, OPT_Wpedantic,
6697 "initialization of a flexible array member");
6699 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6700 TYPE_MAIN_VARIANT (type)))
6701 return inside_init;
6703 if (char_array)
6705 if (typ2 != char_type_node)
6707 error_init (init_loc, "char-array initialized from wide "
6708 "string");
6709 return error_mark_node;
6712 else
6714 if (typ2 == char_type_node)
6716 error_init (init_loc, "wide character array initialized "
6717 "from non-wide string");
6718 return error_mark_node;
6720 else if (!comptypes(typ1, typ2))
6722 error_init (init_loc, "wide character array initialized "
6723 "from incompatible wide string");
6724 return error_mark_node;
6728 TREE_TYPE (inside_init) = type;
6729 if (TYPE_DOMAIN (type) != 0
6730 && TYPE_SIZE (type) != 0
6731 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6733 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6735 /* Subtract the size of a single (possibly wide) character
6736 because it's ok to ignore the terminating null char
6737 that is counted in the length of the constant. */
6738 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6739 (len
6740 - (TYPE_PRECISION (typ1)
6741 / BITS_PER_UNIT))))
6742 pedwarn_init (init_loc, 0,
6743 ("initializer-string for array of chars "
6744 "is too long"));
6745 else if (warn_cxx_compat
6746 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6747 warning_at (init_loc, OPT_Wc___compat,
6748 ("initializer-string for array chars "
6749 "is too long for C++"));
6752 return inside_init;
6754 else if (INTEGRAL_TYPE_P (typ1))
6756 error_init (init_loc, "array of inappropriate type initialized "
6757 "from string constant");
6758 return error_mark_node;
6762 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6763 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6764 below and handle as a constructor. */
6765 if (code == VECTOR_TYPE
6766 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6767 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6768 && TREE_CONSTANT (inside_init))
6770 if (TREE_CODE (inside_init) == VECTOR_CST
6771 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6772 TYPE_MAIN_VARIANT (type)))
6773 return inside_init;
6775 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6777 unsigned HOST_WIDE_INT ix;
6778 tree value;
6779 bool constant_p = true;
6781 /* Iterate through elements and check if all constructor
6782 elements are *_CSTs. */
6783 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6784 if (!CONSTANT_CLASS_P (value))
6786 constant_p = false;
6787 break;
6790 if (constant_p)
6791 return build_vector_from_ctor (type,
6792 CONSTRUCTOR_ELTS (inside_init));
6796 if (warn_sequence_point)
6797 verify_sequence_points (inside_init);
6799 /* Any type can be initialized
6800 from an expression of the same type, optionally with braces. */
6802 if (inside_init && TREE_TYPE (inside_init) != 0
6803 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6804 TYPE_MAIN_VARIANT (type))
6805 || (code == ARRAY_TYPE
6806 && comptypes (TREE_TYPE (inside_init), type))
6807 || (code == VECTOR_TYPE
6808 && comptypes (TREE_TYPE (inside_init), type))
6809 || (code == POINTER_TYPE
6810 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6811 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6812 TREE_TYPE (type)))))
6814 if (code == POINTER_TYPE)
6816 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6818 if (TREE_CODE (inside_init) == STRING_CST
6819 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6820 inside_init = array_to_pointer_conversion
6821 (init_loc, inside_init);
6822 else
6824 error_init (init_loc, "invalid use of non-lvalue array");
6825 return error_mark_node;
6830 if (code == VECTOR_TYPE)
6831 /* Although the types are compatible, we may require a
6832 conversion. */
6833 inside_init = convert (type, inside_init);
6835 if (require_constant
6836 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6838 /* As an extension, allow initializing objects with static storage
6839 duration with compound literals (which are then treated just as
6840 the brace enclosed list they contain). Also allow this for
6841 vectors, as we can only assign them with compound literals. */
6842 if (flag_isoc99 && code != VECTOR_TYPE)
6843 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6844 "is not constant");
6845 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6846 inside_init = DECL_INITIAL (decl);
6849 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6850 && TREE_CODE (inside_init) != CONSTRUCTOR)
6852 error_init (init_loc, "array initialized from non-constant array "
6853 "expression");
6854 return error_mark_node;
6857 /* Compound expressions can only occur here if -Wpedantic or
6858 -pedantic-errors is specified. In the later case, we always want
6859 an error. In the former case, we simply want a warning. */
6860 if (require_constant && pedantic
6861 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6863 inside_init
6864 = valid_compound_expr_initializer (inside_init,
6865 TREE_TYPE (inside_init));
6866 if (inside_init == error_mark_node)
6867 error_init (init_loc, "initializer element is not constant");
6868 else
6869 pedwarn_init (init_loc, OPT_Wpedantic,
6870 "initializer element is not constant");
6871 if (flag_pedantic_errors)
6872 inside_init = error_mark_node;
6874 else if (require_constant
6875 && !initializer_constant_valid_p (inside_init,
6876 TREE_TYPE (inside_init)))
6878 error_init (init_loc, "initializer element is not constant");
6879 inside_init = error_mark_node;
6881 else if (require_constant && !maybe_const)
6882 pedwarn_init (init_loc, 0,
6883 "initializer element is not a constant expression");
6885 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6886 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6887 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6888 type, inside_init, origtype,
6889 ic_init, null_pointer_constant,
6890 NULL_TREE, NULL_TREE, 0);
6891 return inside_init;
6894 /* Handle scalar types, including conversions. */
6896 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6897 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6898 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6900 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6901 && (TREE_CODE (init) == STRING_CST
6902 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6903 inside_init = init = array_to_pointer_conversion (init_loc, init);
6904 if (semantic_type)
6905 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6906 inside_init);
6907 inside_init
6908 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6909 inside_init, origtype, ic_init,
6910 null_pointer_constant, NULL_TREE, NULL_TREE,
6913 /* Check to see if we have already given an error message. */
6914 if (inside_init == error_mark_node)
6916 else if (require_constant && !TREE_CONSTANT (inside_init))
6918 error_init (init_loc, "initializer element is not constant");
6919 inside_init = error_mark_node;
6921 else if (require_constant
6922 && !initializer_constant_valid_p (inside_init,
6923 TREE_TYPE (inside_init)))
6925 error_init (init_loc, "initializer element is not computable at "
6926 "load time");
6927 inside_init = error_mark_node;
6929 else if (require_constant && !maybe_const)
6930 pedwarn_init (init_loc, 0,
6931 "initializer element is not a constant expression");
6933 return inside_init;
6936 /* Come here only for records and arrays. */
6938 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6940 error_init (init_loc, "variable-sized object may not be initialized");
6941 return error_mark_node;
6944 error_init (init_loc, "invalid initializer");
6945 return error_mark_node;
6948 /* Handle initializers that use braces. */
6950 /* Type of object we are accumulating a constructor for.
6951 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6952 static tree constructor_type;
6954 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6955 left to fill. */
6956 static tree constructor_fields;
6958 /* For an ARRAY_TYPE, this is the specified index
6959 at which to store the next element we get. */
6960 static tree constructor_index;
6962 /* For an ARRAY_TYPE, this is the maximum index. */
6963 static tree constructor_max_index;
6965 /* For a RECORD_TYPE, this is the first field not yet written out. */
6966 static tree constructor_unfilled_fields;
6968 /* For an ARRAY_TYPE, this is the index of the first element
6969 not yet written out. */
6970 static tree constructor_unfilled_index;
6972 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6973 This is so we can generate gaps between fields, when appropriate. */
6974 static tree constructor_bit_index;
6976 /* If we are saving up the elements rather than allocating them,
6977 this is the list of elements so far (in reverse order,
6978 most recent first). */
6979 static vec<constructor_elt, va_gc> *constructor_elements;
6981 /* 1 if constructor should be incrementally stored into a constructor chain,
6982 0 if all the elements should be kept in AVL tree. */
6983 static int constructor_incremental;
6985 /* 1 if so far this constructor's elements are all compile-time constants. */
6986 static int constructor_constant;
6988 /* 1 if so far this constructor's elements are all valid address constants. */
6989 static int constructor_simple;
6991 /* 1 if this constructor has an element that cannot be part of a
6992 constant expression. */
6993 static int constructor_nonconst;
6995 /* 1 if this constructor is erroneous so far. */
6996 static int constructor_erroneous;
6998 /* 1 if this constructor is the universal zero initializer { 0 }. */
6999 static int constructor_zeroinit;
7001 /* Structure for managing pending initializer elements, organized as an
7002 AVL tree. */
7004 struct init_node
7006 struct init_node *left, *right;
7007 struct init_node *parent;
7008 int balance;
7009 tree purpose;
7010 tree value;
7011 tree origtype;
7014 /* Tree of pending elements at this constructor level.
7015 These are elements encountered out of order
7016 which belong at places we haven't reached yet in actually
7017 writing the output.
7018 Will never hold tree nodes across GC runs. */
7019 static struct init_node *constructor_pending_elts;
7021 /* The SPELLING_DEPTH of this constructor. */
7022 static int constructor_depth;
7024 /* DECL node for which an initializer is being read.
7025 0 means we are reading a constructor expression
7026 such as (struct foo) {...}. */
7027 static tree constructor_decl;
7029 /* Nonzero if this is an initializer for a top-level decl. */
7030 static int constructor_top_level;
7032 /* Nonzero if there were any member designators in this initializer. */
7033 static int constructor_designated;
7035 /* Nesting depth of designator list. */
7036 static int designator_depth;
7038 /* Nonzero if there were diagnosed errors in this designator list. */
7039 static int designator_erroneous;
7042 /* This stack has a level for each implicit or explicit level of
7043 structuring in the initializer, including the outermost one. It
7044 saves the values of most of the variables above. */
7046 struct constructor_range_stack;
7048 struct constructor_stack
7050 struct constructor_stack *next;
7051 tree type;
7052 tree fields;
7053 tree index;
7054 tree max_index;
7055 tree unfilled_index;
7056 tree unfilled_fields;
7057 tree bit_index;
7058 vec<constructor_elt, va_gc> *elements;
7059 struct init_node *pending_elts;
7060 int offset;
7061 int depth;
7062 /* If value nonzero, this value should replace the entire
7063 constructor at this level. */
7064 struct c_expr replacement_value;
7065 struct constructor_range_stack *range_stack;
7066 char constant;
7067 char simple;
7068 char nonconst;
7069 char implicit;
7070 char erroneous;
7071 char outer;
7072 char incremental;
7073 char designated;
7074 int designator_depth;
7077 static struct constructor_stack *constructor_stack;
7079 /* This stack represents designators from some range designator up to
7080 the last designator in the list. */
7082 struct constructor_range_stack
7084 struct constructor_range_stack *next, *prev;
7085 struct constructor_stack *stack;
7086 tree range_start;
7087 tree index;
7088 tree range_end;
7089 tree fields;
7092 static struct constructor_range_stack *constructor_range_stack;
7094 /* This stack records separate initializers that are nested.
7095 Nested initializers can't happen in ANSI C, but GNU C allows them
7096 in cases like { ... (struct foo) { ... } ... }. */
7098 struct initializer_stack
7100 struct initializer_stack *next;
7101 tree decl;
7102 struct constructor_stack *constructor_stack;
7103 struct constructor_range_stack *constructor_range_stack;
7104 vec<constructor_elt, va_gc> *elements;
7105 struct spelling *spelling;
7106 struct spelling *spelling_base;
7107 int spelling_size;
7108 char top_level;
7109 char require_constant_value;
7110 char require_constant_elements;
7113 static struct initializer_stack *initializer_stack;
7115 /* Prepare to parse and output the initializer for variable DECL. */
7117 void
7118 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7120 const char *locus;
7121 struct initializer_stack *p = XNEW (struct initializer_stack);
7123 p->decl = constructor_decl;
7124 p->require_constant_value = require_constant_value;
7125 p->require_constant_elements = require_constant_elements;
7126 p->constructor_stack = constructor_stack;
7127 p->constructor_range_stack = constructor_range_stack;
7128 p->elements = constructor_elements;
7129 p->spelling = spelling;
7130 p->spelling_base = spelling_base;
7131 p->spelling_size = spelling_size;
7132 p->top_level = constructor_top_level;
7133 p->next = initializer_stack;
7134 initializer_stack = p;
7136 constructor_decl = decl;
7137 constructor_designated = 0;
7138 constructor_top_level = top_level;
7140 if (decl != 0 && decl != error_mark_node)
7142 require_constant_value = TREE_STATIC (decl);
7143 require_constant_elements
7144 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7145 /* For a scalar, you can always use any value to initialize,
7146 even within braces. */
7147 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7148 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7149 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7150 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
7151 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7153 else
7155 require_constant_value = 0;
7156 require_constant_elements = 0;
7157 locus = _("(anonymous)");
7160 constructor_stack = 0;
7161 constructor_range_stack = 0;
7163 found_missing_braces = 0;
7165 spelling_base = 0;
7166 spelling_size = 0;
7167 RESTORE_SPELLING_DEPTH (0);
7169 if (locus)
7170 push_string (locus);
7173 void
7174 finish_init (void)
7176 struct initializer_stack *p = initializer_stack;
7178 /* Free the whole constructor stack of this initializer. */
7179 while (constructor_stack)
7181 struct constructor_stack *q = constructor_stack;
7182 constructor_stack = q->next;
7183 free (q);
7186 gcc_assert (!constructor_range_stack);
7188 /* Pop back to the data of the outer initializer (if any). */
7189 free (spelling_base);
7191 constructor_decl = p->decl;
7192 require_constant_value = p->require_constant_value;
7193 require_constant_elements = p->require_constant_elements;
7194 constructor_stack = p->constructor_stack;
7195 constructor_range_stack = p->constructor_range_stack;
7196 constructor_elements = p->elements;
7197 spelling = p->spelling;
7198 spelling_base = p->spelling_base;
7199 spelling_size = p->spelling_size;
7200 constructor_top_level = p->top_level;
7201 initializer_stack = p->next;
7202 free (p);
7205 /* Call here when we see the initializer is surrounded by braces.
7206 This is instead of a call to push_init_level;
7207 it is matched by a call to pop_init_level.
7209 TYPE is the type to initialize, for a constructor expression.
7210 For an initializer for a decl, TYPE is zero. */
7212 void
7213 really_start_incremental_init (tree type)
7215 struct constructor_stack *p = XNEW (struct constructor_stack);
7217 if (type == 0)
7218 type = TREE_TYPE (constructor_decl);
7220 if (TREE_CODE (type) == VECTOR_TYPE
7221 && TYPE_VECTOR_OPAQUE (type))
7222 error ("opaque vector types cannot be initialized");
7224 p->type = constructor_type;
7225 p->fields = constructor_fields;
7226 p->index = constructor_index;
7227 p->max_index = constructor_max_index;
7228 p->unfilled_index = constructor_unfilled_index;
7229 p->unfilled_fields = constructor_unfilled_fields;
7230 p->bit_index = constructor_bit_index;
7231 p->elements = constructor_elements;
7232 p->constant = constructor_constant;
7233 p->simple = constructor_simple;
7234 p->nonconst = constructor_nonconst;
7235 p->erroneous = constructor_erroneous;
7236 p->pending_elts = constructor_pending_elts;
7237 p->depth = constructor_depth;
7238 p->replacement_value.value = 0;
7239 p->replacement_value.original_code = ERROR_MARK;
7240 p->replacement_value.original_type = NULL;
7241 p->implicit = 0;
7242 p->range_stack = 0;
7243 p->outer = 0;
7244 p->incremental = constructor_incremental;
7245 p->designated = constructor_designated;
7246 p->designator_depth = designator_depth;
7247 p->next = 0;
7248 constructor_stack = p;
7250 constructor_constant = 1;
7251 constructor_simple = 1;
7252 constructor_nonconst = 0;
7253 constructor_depth = SPELLING_DEPTH ();
7254 constructor_elements = NULL;
7255 constructor_pending_elts = 0;
7256 constructor_type = type;
7257 constructor_incremental = 1;
7258 constructor_designated = 0;
7259 constructor_zeroinit = 1;
7260 designator_depth = 0;
7261 designator_erroneous = 0;
7263 if (TREE_CODE (constructor_type) == RECORD_TYPE
7264 || TREE_CODE (constructor_type) == UNION_TYPE)
7266 constructor_fields = TYPE_FIELDS (constructor_type);
7267 /* Skip any nameless bit fields at the beginning. */
7268 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7269 && DECL_NAME (constructor_fields) == 0)
7270 constructor_fields = DECL_CHAIN (constructor_fields);
7272 constructor_unfilled_fields = constructor_fields;
7273 constructor_bit_index = bitsize_zero_node;
7275 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7277 if (TYPE_DOMAIN (constructor_type))
7279 constructor_max_index
7280 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7282 /* Detect non-empty initializations of zero-length arrays. */
7283 if (constructor_max_index == NULL_TREE
7284 && TYPE_SIZE (constructor_type))
7285 constructor_max_index = integer_minus_one_node;
7287 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7288 to initialize VLAs will cause a proper error; avoid tree
7289 checking errors as well by setting a safe value. */
7290 if (constructor_max_index
7291 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7292 constructor_max_index = integer_minus_one_node;
7294 constructor_index
7295 = convert (bitsizetype,
7296 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7298 else
7300 constructor_index = bitsize_zero_node;
7301 constructor_max_index = NULL_TREE;
7304 constructor_unfilled_index = constructor_index;
7306 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7308 /* Vectors are like simple fixed-size arrays. */
7309 constructor_max_index =
7310 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7311 constructor_index = bitsize_zero_node;
7312 constructor_unfilled_index = constructor_index;
7314 else
7316 /* Handle the case of int x = {5}; */
7317 constructor_fields = constructor_type;
7318 constructor_unfilled_fields = constructor_type;
7322 /* Push down into a subobject, for initialization.
7323 If this is for an explicit set of braces, IMPLICIT is 0.
7324 If it is because the next element belongs at a lower level,
7325 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7327 void
7328 push_init_level (location_t loc, int implicit,
7329 struct obstack *braced_init_obstack)
7331 struct constructor_stack *p;
7332 tree value = NULL_TREE;
7334 /* If we've exhausted any levels that didn't have braces,
7335 pop them now. If implicit == 1, this will have been done in
7336 process_init_element; do not repeat it here because in the case
7337 of excess initializers for an empty aggregate this leads to an
7338 infinite cycle of popping a level and immediately recreating
7339 it. */
7340 if (implicit != 1)
7342 while (constructor_stack->implicit)
7344 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7345 || TREE_CODE (constructor_type) == UNION_TYPE)
7346 && constructor_fields == 0)
7347 process_init_element (input_location,
7348 pop_init_level (loc, 1, braced_init_obstack),
7349 true, braced_init_obstack);
7350 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7351 && constructor_max_index
7352 && tree_int_cst_lt (constructor_max_index,
7353 constructor_index))
7354 process_init_element (input_location,
7355 pop_init_level (loc, 1, braced_init_obstack),
7356 true, braced_init_obstack);
7357 else
7358 break;
7362 /* Unless this is an explicit brace, we need to preserve previous
7363 content if any. */
7364 if (implicit)
7366 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7367 || TREE_CODE (constructor_type) == UNION_TYPE)
7368 && constructor_fields)
7369 value = find_init_member (constructor_fields, braced_init_obstack);
7370 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7371 value = find_init_member (constructor_index, braced_init_obstack);
7374 p = XNEW (struct constructor_stack);
7375 p->type = constructor_type;
7376 p->fields = constructor_fields;
7377 p->index = constructor_index;
7378 p->max_index = constructor_max_index;
7379 p->unfilled_index = constructor_unfilled_index;
7380 p->unfilled_fields = constructor_unfilled_fields;
7381 p->bit_index = constructor_bit_index;
7382 p->elements = constructor_elements;
7383 p->constant = constructor_constant;
7384 p->simple = constructor_simple;
7385 p->nonconst = constructor_nonconst;
7386 p->erroneous = constructor_erroneous;
7387 p->pending_elts = constructor_pending_elts;
7388 p->depth = constructor_depth;
7389 p->replacement_value.value = 0;
7390 p->replacement_value.original_code = ERROR_MARK;
7391 p->replacement_value.original_type = NULL;
7392 p->implicit = implicit;
7393 p->outer = 0;
7394 p->incremental = constructor_incremental;
7395 p->designated = constructor_designated;
7396 p->designator_depth = designator_depth;
7397 p->next = constructor_stack;
7398 p->range_stack = 0;
7399 constructor_stack = p;
7401 constructor_constant = 1;
7402 constructor_simple = 1;
7403 constructor_nonconst = 0;
7404 constructor_depth = SPELLING_DEPTH ();
7405 constructor_elements = NULL;
7406 constructor_incremental = 1;
7407 constructor_designated = 0;
7408 constructor_pending_elts = 0;
7409 if (!implicit)
7411 p->range_stack = constructor_range_stack;
7412 constructor_range_stack = 0;
7413 designator_depth = 0;
7414 designator_erroneous = 0;
7417 /* Don't die if an entire brace-pair level is superfluous
7418 in the containing level. */
7419 if (constructor_type == 0)
7421 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7422 || TREE_CODE (constructor_type) == UNION_TYPE)
7424 /* Don't die if there are extra init elts at the end. */
7425 if (constructor_fields == 0)
7426 constructor_type = 0;
7427 else
7429 constructor_type = TREE_TYPE (constructor_fields);
7430 push_member_name (constructor_fields);
7431 constructor_depth++;
7433 /* If upper initializer is designated, then mark this as
7434 designated too to prevent bogus warnings. */
7435 constructor_designated = p->designated;
7437 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7439 constructor_type = TREE_TYPE (constructor_type);
7440 push_array_bounds (tree_to_uhwi (constructor_index));
7441 constructor_depth++;
7444 if (constructor_type == 0)
7446 error_init (loc, "extra brace group at end of initializer");
7447 constructor_fields = 0;
7448 constructor_unfilled_fields = 0;
7449 return;
7452 if (value && TREE_CODE (value) == CONSTRUCTOR)
7454 constructor_constant = TREE_CONSTANT (value);
7455 constructor_simple = TREE_STATIC (value);
7456 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7457 constructor_elements = CONSTRUCTOR_ELTS (value);
7458 if (!vec_safe_is_empty (constructor_elements)
7459 && (TREE_CODE (constructor_type) == RECORD_TYPE
7460 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7461 set_nonincremental_init (braced_init_obstack);
7464 if (implicit == 1)
7465 found_missing_braces = 1;
7467 if (TREE_CODE (constructor_type) == RECORD_TYPE
7468 || TREE_CODE (constructor_type) == UNION_TYPE)
7470 constructor_fields = TYPE_FIELDS (constructor_type);
7471 /* Skip any nameless bit fields at the beginning. */
7472 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7473 && DECL_NAME (constructor_fields) == 0)
7474 constructor_fields = DECL_CHAIN (constructor_fields);
7476 constructor_unfilled_fields = constructor_fields;
7477 constructor_bit_index = bitsize_zero_node;
7479 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7481 /* Vectors are like simple fixed-size arrays. */
7482 constructor_max_index =
7483 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7484 constructor_index = bitsize_int (0);
7485 constructor_unfilled_index = constructor_index;
7487 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7489 if (TYPE_DOMAIN (constructor_type))
7491 constructor_max_index
7492 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7494 /* Detect non-empty initializations of zero-length arrays. */
7495 if (constructor_max_index == NULL_TREE
7496 && TYPE_SIZE (constructor_type))
7497 constructor_max_index = integer_minus_one_node;
7499 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7500 to initialize VLAs will cause a proper error; avoid tree
7501 checking errors as well by setting a safe value. */
7502 if (constructor_max_index
7503 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7504 constructor_max_index = integer_minus_one_node;
7506 constructor_index
7507 = convert (bitsizetype,
7508 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7510 else
7511 constructor_index = bitsize_zero_node;
7513 constructor_unfilled_index = constructor_index;
7514 if (value && TREE_CODE (value) == STRING_CST)
7516 /* We need to split the char/wchar array into individual
7517 characters, so that we don't have to special case it
7518 everywhere. */
7519 set_nonincremental_init_from_string (value, braced_init_obstack);
7522 else
7524 if (constructor_type != error_mark_node)
7525 warning_init (input_location, 0, "braces around scalar initializer");
7526 constructor_fields = constructor_type;
7527 constructor_unfilled_fields = constructor_type;
7531 /* At the end of an implicit or explicit brace level,
7532 finish up that level of constructor. If a single expression
7533 with redundant braces initialized that level, return the
7534 c_expr structure for that expression. Otherwise, the original_code
7535 element is set to ERROR_MARK.
7536 If we were outputting the elements as they are read, return 0 as the value
7537 from inner levels (process_init_element ignores that),
7538 but return error_mark_node as the value from the outermost level
7539 (that's what we want to put in DECL_INITIAL).
7540 Otherwise, return a CONSTRUCTOR expression as the value. */
7542 struct c_expr
7543 pop_init_level (location_t loc, int implicit,
7544 struct obstack *braced_init_obstack)
7546 struct constructor_stack *p;
7547 struct c_expr ret;
7548 ret.value = 0;
7549 ret.original_code = ERROR_MARK;
7550 ret.original_type = NULL;
7552 if (implicit == 0)
7554 /* When we come to an explicit close brace,
7555 pop any inner levels that didn't have explicit braces. */
7556 while (constructor_stack->implicit)
7557 process_init_element (input_location,
7558 pop_init_level (loc, 1, braced_init_obstack),
7559 true, braced_init_obstack);
7560 gcc_assert (!constructor_range_stack);
7563 /* Now output all pending elements. */
7564 constructor_incremental = 1;
7565 output_pending_init_elements (1, braced_init_obstack);
7567 p = constructor_stack;
7569 /* Error for initializing a flexible array member, or a zero-length
7570 array member in an inappropriate context. */
7571 if (constructor_type && constructor_fields
7572 && TREE_CODE (constructor_type) == ARRAY_TYPE
7573 && TYPE_DOMAIN (constructor_type)
7574 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7576 /* Silently discard empty initializations. The parser will
7577 already have pedwarned for empty brackets. */
7578 if (integer_zerop (constructor_unfilled_index))
7579 constructor_type = NULL_TREE;
7580 else
7582 gcc_assert (!TYPE_SIZE (constructor_type));
7584 if (constructor_depth > 2)
7585 error_init (loc, "initialization of flexible array member in a nested context");
7586 else
7587 pedwarn_init (loc, OPT_Wpedantic,
7588 "initialization of a flexible array member");
7590 /* We have already issued an error message for the existence
7591 of a flexible array member not at the end of the structure.
7592 Discard the initializer so that we do not die later. */
7593 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7594 constructor_type = NULL_TREE;
7598 switch (vec_safe_length (constructor_elements))
7600 case 0:
7601 /* Initialization with { } counts as zeroinit. */
7602 constructor_zeroinit = 1;
7603 break;
7604 case 1:
7605 /* This might be zeroinit as well. */
7606 if (integer_zerop ((*constructor_elements)[0].value))
7607 constructor_zeroinit = 1;
7608 break;
7609 default:
7610 /* If the constructor has more than one element, it can't be { 0 }. */
7611 constructor_zeroinit = 0;
7612 break;
7615 /* Warn when some structs are initialized with direct aggregation. */
7616 if (!implicit && found_missing_braces && warn_missing_braces
7617 && !constructor_zeroinit)
7618 warning_init (loc, OPT_Wmissing_braces,
7619 "missing braces around initializer");
7621 /* Warn when some struct elements are implicitly initialized to zero. */
7622 if (warn_missing_field_initializers
7623 && constructor_type
7624 && TREE_CODE (constructor_type) == RECORD_TYPE
7625 && constructor_unfilled_fields)
7627 /* Do not warn for flexible array members or zero-length arrays. */
7628 while (constructor_unfilled_fields
7629 && (!DECL_SIZE (constructor_unfilled_fields)
7630 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7631 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7633 if (constructor_unfilled_fields
7634 /* Do not warn if this level of the initializer uses member
7635 designators; it is likely to be deliberate. */
7636 && !constructor_designated
7637 /* Do not warn about initializing with { 0 } or with { }. */
7638 && !constructor_zeroinit)
7640 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7641 "missing initializer for field %qD of %qT",
7642 constructor_unfilled_fields,
7643 constructor_type))
7644 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7645 "%qD declared here", constructor_unfilled_fields);
7649 /* Pad out the end of the structure. */
7650 if (p->replacement_value.value)
7651 /* If this closes a superfluous brace pair,
7652 just pass out the element between them. */
7653 ret = p->replacement_value;
7654 else if (constructor_type == 0)
7656 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7657 && TREE_CODE (constructor_type) != UNION_TYPE
7658 && TREE_CODE (constructor_type) != ARRAY_TYPE
7659 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7661 /* A nonincremental scalar initializer--just return
7662 the element, after verifying there is just one. */
7663 if (vec_safe_is_empty (constructor_elements))
7665 if (!constructor_erroneous)
7666 error_init (loc, "empty scalar initializer");
7667 ret.value = error_mark_node;
7669 else if (vec_safe_length (constructor_elements) != 1)
7671 error_init (loc, "extra elements in scalar initializer");
7672 ret.value = (*constructor_elements)[0].value;
7674 else
7675 ret.value = (*constructor_elements)[0].value;
7677 else
7679 if (constructor_erroneous)
7680 ret.value = error_mark_node;
7681 else
7683 ret.value = build_constructor (constructor_type,
7684 constructor_elements);
7685 if (constructor_constant)
7686 TREE_CONSTANT (ret.value) = 1;
7687 if (constructor_constant && constructor_simple)
7688 TREE_STATIC (ret.value) = 1;
7689 if (constructor_nonconst)
7690 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7694 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7696 if (constructor_nonconst)
7697 ret.original_code = C_MAYBE_CONST_EXPR;
7698 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7699 ret.original_code = ERROR_MARK;
7702 constructor_type = p->type;
7703 constructor_fields = p->fields;
7704 constructor_index = p->index;
7705 constructor_max_index = p->max_index;
7706 constructor_unfilled_index = p->unfilled_index;
7707 constructor_unfilled_fields = p->unfilled_fields;
7708 constructor_bit_index = p->bit_index;
7709 constructor_elements = p->elements;
7710 constructor_constant = p->constant;
7711 constructor_simple = p->simple;
7712 constructor_nonconst = p->nonconst;
7713 constructor_erroneous = p->erroneous;
7714 constructor_incremental = p->incremental;
7715 constructor_designated = p->designated;
7716 designator_depth = p->designator_depth;
7717 constructor_pending_elts = p->pending_elts;
7718 constructor_depth = p->depth;
7719 if (!p->implicit)
7720 constructor_range_stack = p->range_stack;
7721 RESTORE_SPELLING_DEPTH (constructor_depth);
7723 constructor_stack = p->next;
7724 free (p);
7726 if (ret.value == 0 && constructor_stack == 0)
7727 ret.value = error_mark_node;
7728 return ret;
7731 /* Common handling for both array range and field name designators.
7732 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7734 static int
7735 set_designator (location_t loc, int array,
7736 struct obstack *braced_init_obstack)
7738 tree subtype;
7739 enum tree_code subcode;
7741 /* Don't die if an entire brace-pair level is superfluous
7742 in the containing level. */
7743 if (constructor_type == 0)
7744 return 1;
7746 /* If there were errors in this designator list already, bail out
7747 silently. */
7748 if (designator_erroneous)
7749 return 1;
7751 if (!designator_depth)
7753 gcc_assert (!constructor_range_stack);
7755 /* Designator list starts at the level of closest explicit
7756 braces. */
7757 while (constructor_stack->implicit)
7758 process_init_element (input_location,
7759 pop_init_level (loc, 1, braced_init_obstack),
7760 true, braced_init_obstack);
7761 constructor_designated = 1;
7762 return 0;
7765 switch (TREE_CODE (constructor_type))
7767 case RECORD_TYPE:
7768 case UNION_TYPE:
7769 subtype = TREE_TYPE (constructor_fields);
7770 if (subtype != error_mark_node)
7771 subtype = TYPE_MAIN_VARIANT (subtype);
7772 break;
7773 case ARRAY_TYPE:
7774 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7775 break;
7776 default:
7777 gcc_unreachable ();
7780 subcode = TREE_CODE (subtype);
7781 if (array && subcode != ARRAY_TYPE)
7783 error_init (loc, "array index in non-array initializer");
7784 return 1;
7786 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7788 error_init (loc, "field name not in record or union initializer");
7789 return 1;
7792 constructor_designated = 1;
7793 push_init_level (loc, 2, braced_init_obstack);
7794 return 0;
7797 /* If there are range designators in designator list, push a new designator
7798 to constructor_range_stack. RANGE_END is end of such stack range or
7799 NULL_TREE if there is no range designator at this level. */
7801 static void
7802 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7804 struct constructor_range_stack *p;
7806 p = (struct constructor_range_stack *)
7807 obstack_alloc (braced_init_obstack,
7808 sizeof (struct constructor_range_stack));
7809 p->prev = constructor_range_stack;
7810 p->next = 0;
7811 p->fields = constructor_fields;
7812 p->range_start = constructor_index;
7813 p->index = constructor_index;
7814 p->stack = constructor_stack;
7815 p->range_end = range_end;
7816 if (constructor_range_stack)
7817 constructor_range_stack->next = p;
7818 constructor_range_stack = p;
7821 /* Within an array initializer, specify the next index to be initialized.
7822 FIRST is that index. If LAST is nonzero, then initialize a range
7823 of indices, running from FIRST through LAST. */
7825 void
7826 set_init_index (location_t loc, tree first, tree last,
7827 struct obstack *braced_init_obstack)
7829 if (set_designator (loc, 1, braced_init_obstack))
7830 return;
7832 designator_erroneous = 1;
7834 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7835 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7837 error_init (loc, "array index in initializer not of integer type");
7838 return;
7841 if (TREE_CODE (first) != INTEGER_CST)
7843 first = c_fully_fold (first, false, NULL);
7844 if (TREE_CODE (first) == INTEGER_CST)
7845 pedwarn_init (loc, OPT_Wpedantic,
7846 "array index in initializer is not "
7847 "an integer constant expression");
7850 if (last && TREE_CODE (last) != INTEGER_CST)
7852 last = c_fully_fold (last, false, NULL);
7853 if (TREE_CODE (last) == INTEGER_CST)
7854 pedwarn_init (loc, OPT_Wpedantic,
7855 "array index in initializer is not "
7856 "an integer constant expression");
7859 if (TREE_CODE (first) != INTEGER_CST)
7860 error_init (loc, "nonconstant array index in initializer");
7861 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7862 error_init (loc, "nonconstant array index in initializer");
7863 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7864 error_init (loc, "array index in non-array initializer");
7865 else if (tree_int_cst_sgn (first) == -1)
7866 error_init (loc, "array index in initializer exceeds array bounds");
7867 else if (constructor_max_index
7868 && tree_int_cst_lt (constructor_max_index, first))
7869 error_init (loc, "array index in initializer exceeds array bounds");
7870 else
7872 constant_expression_warning (first);
7873 if (last)
7874 constant_expression_warning (last);
7875 constructor_index = convert (bitsizetype, first);
7876 if (tree_int_cst_lt (constructor_index, first))
7878 constructor_index = copy_node (constructor_index);
7879 TREE_OVERFLOW (constructor_index) = 1;
7882 if (last)
7884 if (tree_int_cst_equal (first, last))
7885 last = 0;
7886 else if (tree_int_cst_lt (last, first))
7888 error_init (loc, "empty index range in initializer");
7889 last = 0;
7891 else
7893 last = convert (bitsizetype, last);
7894 if (constructor_max_index != 0
7895 && tree_int_cst_lt (constructor_max_index, last))
7897 error_init (loc, "array index range in initializer exceeds "
7898 "array bounds");
7899 last = 0;
7904 designator_depth++;
7905 designator_erroneous = 0;
7906 if (constructor_range_stack || last)
7907 push_range_stack (last, braced_init_obstack);
7911 /* Within a struct initializer, specify the next field to be initialized. */
7913 void
7914 set_init_label (location_t loc, tree fieldname,
7915 struct obstack *braced_init_obstack)
7917 tree field;
7919 if (set_designator (loc, 0, braced_init_obstack))
7920 return;
7922 designator_erroneous = 1;
7924 if (TREE_CODE (constructor_type) != RECORD_TYPE
7925 && TREE_CODE (constructor_type) != UNION_TYPE)
7927 error_init (loc, "field name not in record or union initializer");
7928 return;
7931 field = lookup_field (constructor_type, fieldname);
7933 if (field == 0)
7934 error_at (loc, "unknown field %qE specified in initializer", fieldname);
7935 else
7938 constructor_fields = TREE_VALUE (field);
7939 designator_depth++;
7940 designator_erroneous = 0;
7941 if (constructor_range_stack)
7942 push_range_stack (NULL_TREE, braced_init_obstack);
7943 field = TREE_CHAIN (field);
7944 if (field)
7946 if (set_designator (loc, 0, braced_init_obstack))
7947 return;
7950 while (field != NULL_TREE);
7953 /* Add a new initializer to the tree of pending initializers. PURPOSE
7954 identifies the initializer, either array index or field in a structure.
7955 VALUE is the value of that index or field. If ORIGTYPE is not
7956 NULL_TREE, it is the original type of VALUE.
7958 IMPLICIT is true if value comes from pop_init_level (1),
7959 the new initializer has been merged with the existing one
7960 and thus no warnings should be emitted about overriding an
7961 existing initializer. */
7963 static void
7964 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7965 bool implicit, struct obstack *braced_init_obstack)
7967 struct init_node *p, **q, *r;
7969 q = &constructor_pending_elts;
7970 p = 0;
7972 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7974 while (*q != 0)
7976 p = *q;
7977 if (tree_int_cst_lt (purpose, p->purpose))
7978 q = &p->left;
7979 else if (tree_int_cst_lt (p->purpose, purpose))
7980 q = &p->right;
7981 else
7983 if (!implicit)
7985 if (TREE_SIDE_EFFECTS (p->value))
7986 warning_init (loc, 0,
7987 "initialized field with side-effects "
7988 "overwritten");
7989 else if (warn_override_init)
7990 warning_init (loc, OPT_Woverride_init,
7991 "initialized field overwritten");
7993 p->value = value;
7994 p->origtype = origtype;
7995 return;
7999 else
8001 tree bitpos;
8003 bitpos = bit_position (purpose);
8004 while (*q != NULL)
8006 p = *q;
8007 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8008 q = &p->left;
8009 else if (p->purpose != purpose)
8010 q = &p->right;
8011 else
8013 if (!implicit)
8015 if (TREE_SIDE_EFFECTS (p->value))
8016 warning_init (loc, 0,
8017 "initialized field with side-effects "
8018 "overwritten");
8019 else if (warn_override_init)
8020 warning_init (loc, OPT_Woverride_init,
8021 "initialized field overwritten");
8023 p->value = value;
8024 p->origtype = origtype;
8025 return;
8030 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8031 sizeof (struct init_node));
8032 r->purpose = purpose;
8033 r->value = value;
8034 r->origtype = origtype;
8036 *q = r;
8037 r->parent = p;
8038 r->left = 0;
8039 r->right = 0;
8040 r->balance = 0;
8042 while (p)
8044 struct init_node *s;
8046 if (r == p->left)
8048 if (p->balance == 0)
8049 p->balance = -1;
8050 else if (p->balance < 0)
8052 if (r->balance < 0)
8054 /* L rotation. */
8055 p->left = r->right;
8056 if (p->left)
8057 p->left->parent = p;
8058 r->right = p;
8060 p->balance = 0;
8061 r->balance = 0;
8063 s = p->parent;
8064 p->parent = r;
8065 r->parent = s;
8066 if (s)
8068 if (s->left == p)
8069 s->left = r;
8070 else
8071 s->right = r;
8073 else
8074 constructor_pending_elts = r;
8076 else
8078 /* LR rotation. */
8079 struct init_node *t = r->right;
8081 r->right = t->left;
8082 if (r->right)
8083 r->right->parent = r;
8084 t->left = r;
8086 p->left = t->right;
8087 if (p->left)
8088 p->left->parent = p;
8089 t->right = p;
8091 p->balance = t->balance < 0;
8092 r->balance = -(t->balance > 0);
8093 t->balance = 0;
8095 s = p->parent;
8096 p->parent = t;
8097 r->parent = t;
8098 t->parent = s;
8099 if (s)
8101 if (s->left == p)
8102 s->left = t;
8103 else
8104 s->right = t;
8106 else
8107 constructor_pending_elts = t;
8109 break;
8111 else
8113 /* p->balance == +1; growth of left side balances the node. */
8114 p->balance = 0;
8115 break;
8118 else /* r == p->right */
8120 if (p->balance == 0)
8121 /* Growth propagation from right side. */
8122 p->balance++;
8123 else if (p->balance > 0)
8125 if (r->balance > 0)
8127 /* R rotation. */
8128 p->right = r->left;
8129 if (p->right)
8130 p->right->parent = p;
8131 r->left = p;
8133 p->balance = 0;
8134 r->balance = 0;
8136 s = p->parent;
8137 p->parent = r;
8138 r->parent = s;
8139 if (s)
8141 if (s->left == p)
8142 s->left = r;
8143 else
8144 s->right = r;
8146 else
8147 constructor_pending_elts = r;
8149 else /* r->balance == -1 */
8151 /* RL rotation */
8152 struct init_node *t = r->left;
8154 r->left = t->right;
8155 if (r->left)
8156 r->left->parent = r;
8157 t->right = r;
8159 p->right = t->left;
8160 if (p->right)
8161 p->right->parent = p;
8162 t->left = p;
8164 r->balance = (t->balance < 0);
8165 p->balance = -(t->balance > 0);
8166 t->balance = 0;
8168 s = p->parent;
8169 p->parent = t;
8170 r->parent = t;
8171 t->parent = s;
8172 if (s)
8174 if (s->left == p)
8175 s->left = t;
8176 else
8177 s->right = t;
8179 else
8180 constructor_pending_elts = t;
8182 break;
8184 else
8186 /* p->balance == -1; growth of right side balances the node. */
8187 p->balance = 0;
8188 break;
8192 r = p;
8193 p = p->parent;
8197 /* Build AVL tree from a sorted chain. */
8199 static void
8200 set_nonincremental_init (struct obstack * braced_init_obstack)
8202 unsigned HOST_WIDE_INT ix;
8203 tree index, value;
8205 if (TREE_CODE (constructor_type) != RECORD_TYPE
8206 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8207 return;
8209 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8210 add_pending_init (input_location, index, value, NULL_TREE, true,
8211 braced_init_obstack);
8212 constructor_elements = NULL;
8213 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8215 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8216 /* Skip any nameless bit fields at the beginning. */
8217 while (constructor_unfilled_fields != 0
8218 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8219 && DECL_NAME (constructor_unfilled_fields) == 0)
8220 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8223 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8225 if (TYPE_DOMAIN (constructor_type))
8226 constructor_unfilled_index
8227 = convert (bitsizetype,
8228 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8229 else
8230 constructor_unfilled_index = bitsize_zero_node;
8232 constructor_incremental = 0;
8235 /* Build AVL tree from a string constant. */
8237 static void
8238 set_nonincremental_init_from_string (tree str,
8239 struct obstack * braced_init_obstack)
8241 tree value, purpose, type;
8242 HOST_WIDE_INT val[2];
8243 const char *p, *end;
8244 int byte, wchar_bytes, charwidth, bitpos;
8246 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8248 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8249 charwidth = TYPE_PRECISION (char_type_node);
8250 type = TREE_TYPE (constructor_type);
8251 p = TREE_STRING_POINTER (str);
8252 end = p + TREE_STRING_LENGTH (str);
8254 for (purpose = bitsize_zero_node;
8255 p < end
8256 && !(constructor_max_index
8257 && tree_int_cst_lt (constructor_max_index, purpose));
8258 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8260 if (wchar_bytes == 1)
8262 val[0] = (unsigned char) *p++;
8263 val[1] = 0;
8265 else
8267 val[1] = 0;
8268 val[0] = 0;
8269 for (byte = 0; byte < wchar_bytes; byte++)
8271 if (BYTES_BIG_ENDIAN)
8272 bitpos = (wchar_bytes - byte - 1) * charwidth;
8273 else
8274 bitpos = byte * charwidth;
8275 val[bitpos % HOST_BITS_PER_WIDE_INT]
8276 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8277 << (bitpos % HOST_BITS_PER_WIDE_INT);
8281 if (!TYPE_UNSIGNED (type))
8283 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8284 if (bitpos < HOST_BITS_PER_WIDE_INT)
8286 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8288 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8289 val[1] = -1;
8292 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8294 if (val[0] < 0)
8295 val[1] = -1;
8297 else if (val[1] & (((HOST_WIDE_INT) 1)
8298 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8299 val[1] |= ((HOST_WIDE_INT) -1)
8300 << (bitpos - HOST_BITS_PER_WIDE_INT);
8303 value = wide_int_to_tree (type,
8304 wide_int::from_array (val, 2,
8305 HOST_BITS_PER_WIDE_INT * 2));
8306 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8307 braced_init_obstack);
8310 constructor_incremental = 0;
8313 /* Return value of FIELD in pending initializer or zero if the field was
8314 not initialized yet. */
8316 static tree
8317 find_init_member (tree field, struct obstack * braced_init_obstack)
8319 struct init_node *p;
8321 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8323 if (constructor_incremental
8324 && tree_int_cst_lt (field, constructor_unfilled_index))
8325 set_nonincremental_init (braced_init_obstack);
8327 p = constructor_pending_elts;
8328 while (p)
8330 if (tree_int_cst_lt (field, p->purpose))
8331 p = p->left;
8332 else if (tree_int_cst_lt (p->purpose, field))
8333 p = p->right;
8334 else
8335 return p->value;
8338 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8340 tree bitpos = bit_position (field);
8342 if (constructor_incremental
8343 && (!constructor_unfilled_fields
8344 || tree_int_cst_lt (bitpos,
8345 bit_position (constructor_unfilled_fields))))
8346 set_nonincremental_init (braced_init_obstack);
8348 p = constructor_pending_elts;
8349 while (p)
8351 if (field == p->purpose)
8352 return p->value;
8353 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8354 p = p->left;
8355 else
8356 p = p->right;
8359 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8361 if (!vec_safe_is_empty (constructor_elements)
8362 && (constructor_elements->last ().index == field))
8363 return constructor_elements->last ().value;
8365 return 0;
8368 /* "Output" the next constructor element.
8369 At top level, really output it to assembler code now.
8370 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8371 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8372 TYPE is the data type that the containing data type wants here.
8373 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8374 If VALUE is a string constant, STRICT_STRING is true if it is
8375 unparenthesized or we should not warn here for it being parenthesized.
8376 For other types of VALUE, STRICT_STRING is not used.
8378 PENDING if non-nil means output pending elements that belong
8379 right after this element. (PENDING is normally 1;
8380 it is 0 while outputting pending elements, to avoid recursion.)
8382 IMPLICIT is true if value comes from pop_init_level (1),
8383 the new initializer has been merged with the existing one
8384 and thus no warnings should be emitted about overriding an
8385 existing initializer. */
8387 static void
8388 output_init_element (location_t loc, tree value, tree origtype,
8389 bool strict_string, tree type, tree field, int pending,
8390 bool implicit, struct obstack * braced_init_obstack)
8392 tree semantic_type = NULL_TREE;
8393 bool maybe_const = true;
8394 bool npc;
8396 if (type == error_mark_node || value == error_mark_node)
8398 constructor_erroneous = 1;
8399 return;
8401 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8402 && (TREE_CODE (value) == STRING_CST
8403 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8404 && !(TREE_CODE (value) == STRING_CST
8405 && TREE_CODE (type) == ARRAY_TYPE
8406 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8407 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8408 TYPE_MAIN_VARIANT (type)))
8409 value = array_to_pointer_conversion (input_location, value);
8411 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8412 && require_constant_value && pending)
8414 /* As an extension, allow initializing objects with static storage
8415 duration with compound literals (which are then treated just as
8416 the brace enclosed list they contain). */
8417 if (flag_isoc99)
8418 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8419 "constant");
8420 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8421 value = DECL_INITIAL (decl);
8424 npc = null_pointer_constant_p (value);
8425 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8427 semantic_type = TREE_TYPE (value);
8428 value = TREE_OPERAND (value, 0);
8430 value = c_fully_fold (value, require_constant_value, &maybe_const);
8432 if (value == error_mark_node)
8433 constructor_erroneous = 1;
8434 else if (!TREE_CONSTANT (value))
8435 constructor_constant = 0;
8436 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8437 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8438 || TREE_CODE (constructor_type) == UNION_TYPE)
8439 && DECL_C_BIT_FIELD (field)
8440 && TREE_CODE (value) != INTEGER_CST))
8441 constructor_simple = 0;
8442 if (!maybe_const)
8443 constructor_nonconst = 1;
8445 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8447 if (require_constant_value)
8449 error_init (loc, "initializer element is not constant");
8450 value = error_mark_node;
8452 else if (require_constant_elements)
8453 pedwarn (loc, OPT_Wpedantic,
8454 "initializer element is not computable at load time");
8456 else if (!maybe_const
8457 && (require_constant_value || require_constant_elements))
8458 pedwarn_init (loc, OPT_Wpedantic,
8459 "initializer element is not a constant expression");
8461 /* Issue -Wc++-compat warnings about initializing a bitfield with
8462 enum type. */
8463 if (warn_cxx_compat
8464 && field != NULL_TREE
8465 && TREE_CODE (field) == FIELD_DECL
8466 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8467 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8468 != TYPE_MAIN_VARIANT (type))
8469 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8471 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8472 if (checktype != error_mark_node
8473 && (TYPE_MAIN_VARIANT (checktype)
8474 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8475 warning_init (loc, OPT_Wc___compat,
8476 "enum conversion in initialization is invalid in C++");
8479 /* If this field is empty (and not at the end of structure),
8480 don't do anything other than checking the initializer. */
8481 if (field
8482 && (TREE_TYPE (field) == error_mark_node
8483 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8484 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8485 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8486 || DECL_CHAIN (field)))))
8487 return;
8489 if (semantic_type)
8490 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8491 value = digest_init (loc, type, value, origtype, npc, strict_string,
8492 require_constant_value);
8493 if (value == error_mark_node)
8495 constructor_erroneous = 1;
8496 return;
8498 if (require_constant_value || require_constant_elements)
8499 constant_expression_warning (value);
8501 /* If this element doesn't come next in sequence,
8502 put it on constructor_pending_elts. */
8503 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8504 && (!constructor_incremental
8505 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8507 if (constructor_incremental
8508 && tree_int_cst_lt (field, constructor_unfilled_index))
8509 set_nonincremental_init (braced_init_obstack);
8511 add_pending_init (loc, field, value, origtype, implicit,
8512 braced_init_obstack);
8513 return;
8515 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8516 && (!constructor_incremental
8517 || field != constructor_unfilled_fields))
8519 /* We do this for records but not for unions. In a union,
8520 no matter which field is specified, it can be initialized
8521 right away since it starts at the beginning of the union. */
8522 if (constructor_incremental)
8524 if (!constructor_unfilled_fields)
8525 set_nonincremental_init (braced_init_obstack);
8526 else
8528 tree bitpos, unfillpos;
8530 bitpos = bit_position (field);
8531 unfillpos = bit_position (constructor_unfilled_fields);
8533 if (tree_int_cst_lt (bitpos, unfillpos))
8534 set_nonincremental_init (braced_init_obstack);
8538 add_pending_init (loc, field, value, origtype, implicit,
8539 braced_init_obstack);
8540 return;
8542 else if (TREE_CODE (constructor_type) == UNION_TYPE
8543 && !vec_safe_is_empty (constructor_elements))
8545 if (!implicit)
8547 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8548 warning_init (loc, 0,
8549 "initialized field with side-effects overwritten");
8550 else if (warn_override_init)
8551 warning_init (loc, OPT_Woverride_init,
8552 "initialized field overwritten");
8555 /* We can have just one union field set. */
8556 constructor_elements = NULL;
8559 /* Otherwise, output this element either to
8560 constructor_elements or to the assembler file. */
8562 constructor_elt celt = {field, value};
8563 vec_safe_push (constructor_elements, celt);
8565 /* Advance the variable that indicates sequential elements output. */
8566 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8567 constructor_unfilled_index
8568 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8569 bitsize_one_node);
8570 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8572 constructor_unfilled_fields
8573 = DECL_CHAIN (constructor_unfilled_fields);
8575 /* Skip any nameless bit fields. */
8576 while (constructor_unfilled_fields != 0
8577 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8578 && DECL_NAME (constructor_unfilled_fields) == 0)
8579 constructor_unfilled_fields =
8580 DECL_CHAIN (constructor_unfilled_fields);
8582 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8583 constructor_unfilled_fields = 0;
8585 /* Now output any pending elements which have become next. */
8586 if (pending)
8587 output_pending_init_elements (0, braced_init_obstack);
8590 /* Output any pending elements which have become next.
8591 As we output elements, constructor_unfilled_{fields,index}
8592 advances, which may cause other elements to become next;
8593 if so, they too are output.
8595 If ALL is 0, we return when there are
8596 no more pending elements to output now.
8598 If ALL is 1, we output space as necessary so that
8599 we can output all the pending elements. */
8600 static void
8601 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8603 struct init_node *elt = constructor_pending_elts;
8604 tree next;
8606 retry:
8608 /* Look through the whole pending tree.
8609 If we find an element that should be output now,
8610 output it. Otherwise, set NEXT to the element
8611 that comes first among those still pending. */
8613 next = 0;
8614 while (elt)
8616 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8618 if (tree_int_cst_equal (elt->purpose,
8619 constructor_unfilled_index))
8620 output_init_element (input_location, elt->value, elt->origtype,
8621 true, TREE_TYPE (constructor_type),
8622 constructor_unfilled_index, 0, false,
8623 braced_init_obstack);
8624 else if (tree_int_cst_lt (constructor_unfilled_index,
8625 elt->purpose))
8627 /* Advance to the next smaller node. */
8628 if (elt->left)
8629 elt = elt->left;
8630 else
8632 /* We have reached the smallest node bigger than the
8633 current unfilled index. Fill the space first. */
8634 next = elt->purpose;
8635 break;
8638 else
8640 /* Advance to the next bigger node. */
8641 if (elt->right)
8642 elt = elt->right;
8643 else
8645 /* We have reached the biggest node in a subtree. Find
8646 the parent of it, which is the next bigger node. */
8647 while (elt->parent && elt->parent->right == elt)
8648 elt = elt->parent;
8649 elt = elt->parent;
8650 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8651 elt->purpose))
8653 next = elt->purpose;
8654 break;
8659 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8660 || TREE_CODE (constructor_type) == UNION_TYPE)
8662 tree ctor_unfilled_bitpos, elt_bitpos;
8664 /* If the current record is complete we are done. */
8665 if (constructor_unfilled_fields == 0)
8666 break;
8668 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8669 elt_bitpos = bit_position (elt->purpose);
8670 /* We can't compare fields here because there might be empty
8671 fields in between. */
8672 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8674 constructor_unfilled_fields = elt->purpose;
8675 output_init_element (input_location, elt->value, elt->origtype,
8676 true, TREE_TYPE (elt->purpose),
8677 elt->purpose, 0, false,
8678 braced_init_obstack);
8680 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8682 /* Advance to the next smaller node. */
8683 if (elt->left)
8684 elt = elt->left;
8685 else
8687 /* We have reached the smallest node bigger than the
8688 current unfilled field. Fill the space first. */
8689 next = elt->purpose;
8690 break;
8693 else
8695 /* Advance to the next bigger node. */
8696 if (elt->right)
8697 elt = elt->right;
8698 else
8700 /* We have reached the biggest node in a subtree. Find
8701 the parent of it, which is the next bigger node. */
8702 while (elt->parent && elt->parent->right == elt)
8703 elt = elt->parent;
8704 elt = elt->parent;
8705 if (elt
8706 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8707 bit_position (elt->purpose))))
8709 next = elt->purpose;
8710 break;
8717 /* Ordinarily return, but not if we want to output all
8718 and there are elements left. */
8719 if (!(all && next != 0))
8720 return;
8722 /* If it's not incremental, just skip over the gap, so that after
8723 jumping to retry we will output the next successive element. */
8724 if (TREE_CODE (constructor_type) == RECORD_TYPE
8725 || TREE_CODE (constructor_type) == UNION_TYPE)
8726 constructor_unfilled_fields = next;
8727 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8728 constructor_unfilled_index = next;
8730 /* ELT now points to the node in the pending tree with the next
8731 initializer to output. */
8732 goto retry;
8735 /* Add one non-braced element to the current constructor level.
8736 This adjusts the current position within the constructor's type.
8737 This may also start or terminate implicit levels
8738 to handle a partly-braced initializer.
8740 Once this has found the correct level for the new element,
8741 it calls output_init_element.
8743 IMPLICIT is true if value comes from pop_init_level (1),
8744 the new initializer has been merged with the existing one
8745 and thus no warnings should be emitted about overriding an
8746 existing initializer. */
8748 void
8749 process_init_element (location_t loc, struct c_expr value, bool implicit,
8750 struct obstack * braced_init_obstack)
8752 tree orig_value = value.value;
8753 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8754 bool strict_string = value.original_code == STRING_CST;
8755 bool was_designated = designator_depth != 0;
8757 designator_depth = 0;
8758 designator_erroneous = 0;
8760 if (!implicit && value.value && !integer_zerop (value.value))
8761 constructor_zeroinit = 0;
8763 /* Handle superfluous braces around string cst as in
8764 char x[] = {"foo"}; */
8765 if (string_flag
8766 && constructor_type
8767 && !was_designated
8768 && TREE_CODE (constructor_type) == ARRAY_TYPE
8769 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8770 && integer_zerop (constructor_unfilled_index))
8772 if (constructor_stack->replacement_value.value)
8773 error_init (loc, "excess elements in char array initializer");
8774 constructor_stack->replacement_value = value;
8775 return;
8778 if (constructor_stack->replacement_value.value != 0)
8780 error_init (loc, "excess elements in struct initializer");
8781 return;
8784 /* Ignore elements of a brace group if it is entirely superfluous
8785 and has already been diagnosed. */
8786 if (constructor_type == 0)
8787 return;
8789 if (!implicit && warn_designated_init && !was_designated
8790 && TREE_CODE (constructor_type) == RECORD_TYPE
8791 && lookup_attribute ("designated_init",
8792 TYPE_ATTRIBUTES (constructor_type)))
8793 warning_init (loc,
8794 OPT_Wdesignated_init,
8795 "positional initialization of field "
8796 "in %<struct%> declared with %<designated_init%> attribute");
8798 /* If we've exhausted any levels that didn't have braces,
8799 pop them now. */
8800 while (constructor_stack->implicit)
8802 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8803 || TREE_CODE (constructor_type) == UNION_TYPE)
8804 && constructor_fields == 0)
8805 process_init_element (loc,
8806 pop_init_level (loc, 1, braced_init_obstack),
8807 true, braced_init_obstack);
8808 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8809 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8810 && constructor_max_index
8811 && tree_int_cst_lt (constructor_max_index,
8812 constructor_index))
8813 process_init_element (loc,
8814 pop_init_level (loc, 1, braced_init_obstack),
8815 true, braced_init_obstack);
8816 else
8817 break;
8820 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8821 if (constructor_range_stack)
8823 /* If value is a compound literal and we'll be just using its
8824 content, don't put it into a SAVE_EXPR. */
8825 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8826 || !require_constant_value)
8828 tree semantic_type = NULL_TREE;
8829 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8831 semantic_type = TREE_TYPE (value.value);
8832 value.value = TREE_OPERAND (value.value, 0);
8834 value.value = c_save_expr (value.value);
8835 if (semantic_type)
8836 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8837 value.value);
8841 while (1)
8843 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8845 tree fieldtype;
8846 enum tree_code fieldcode;
8848 if (constructor_fields == 0)
8850 pedwarn_init (loc, 0, "excess elements in struct initializer");
8851 break;
8854 fieldtype = TREE_TYPE (constructor_fields);
8855 if (fieldtype != error_mark_node)
8856 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8857 fieldcode = TREE_CODE (fieldtype);
8859 /* Error for non-static initialization of a flexible array member. */
8860 if (fieldcode == ARRAY_TYPE
8861 && !require_constant_value
8862 && TYPE_SIZE (fieldtype) == NULL_TREE
8863 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8865 error_init (loc, "non-static initialization of a flexible "
8866 "array member");
8867 break;
8870 /* Error for initialization of a flexible array member with
8871 a string constant if the structure is in an array. E.g.:
8872 struct S { int x; char y[]; };
8873 struct S s[] = { { 1, "foo" } };
8874 is invalid. */
8875 if (string_flag
8876 && fieldcode == ARRAY_TYPE
8877 && constructor_depth > 1
8878 && TYPE_SIZE (fieldtype) == NULL_TREE
8879 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8881 bool in_array_p = false;
8882 for (struct constructor_stack *p = constructor_stack;
8883 p && p->type; p = p->next)
8884 if (TREE_CODE (p->type) == ARRAY_TYPE)
8886 in_array_p = true;
8887 break;
8889 if (in_array_p)
8891 error_init (loc, "initialization of flexible array "
8892 "member in a nested context");
8893 break;
8897 /* Accept a string constant to initialize a subarray. */
8898 if (value.value != 0
8899 && fieldcode == ARRAY_TYPE
8900 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8901 && string_flag)
8902 value.value = orig_value;
8903 /* Otherwise, if we have come to a subaggregate,
8904 and we don't have an element of its type, push into it. */
8905 else if (value.value != 0
8906 && value.value != error_mark_node
8907 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8908 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8909 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8911 push_init_level (loc, 1, braced_init_obstack);
8912 continue;
8915 if (value.value)
8917 push_member_name (constructor_fields);
8918 output_init_element (loc, value.value, value.original_type,
8919 strict_string, fieldtype,
8920 constructor_fields, 1, implicit,
8921 braced_init_obstack);
8922 RESTORE_SPELLING_DEPTH (constructor_depth);
8924 else
8925 /* Do the bookkeeping for an element that was
8926 directly output as a constructor. */
8928 /* For a record, keep track of end position of last field. */
8929 if (DECL_SIZE (constructor_fields))
8930 constructor_bit_index
8931 = size_binop_loc (input_location, PLUS_EXPR,
8932 bit_position (constructor_fields),
8933 DECL_SIZE (constructor_fields));
8935 /* If the current field was the first one not yet written out,
8936 it isn't now, so update. */
8937 if (constructor_unfilled_fields == constructor_fields)
8939 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8940 /* Skip any nameless bit fields. */
8941 while (constructor_unfilled_fields != 0
8942 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8943 && DECL_NAME (constructor_unfilled_fields) == 0)
8944 constructor_unfilled_fields =
8945 DECL_CHAIN (constructor_unfilled_fields);
8949 constructor_fields = DECL_CHAIN (constructor_fields);
8950 /* Skip any nameless bit fields at the beginning. */
8951 while (constructor_fields != 0
8952 && DECL_C_BIT_FIELD (constructor_fields)
8953 && DECL_NAME (constructor_fields) == 0)
8954 constructor_fields = DECL_CHAIN (constructor_fields);
8956 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8958 tree fieldtype;
8959 enum tree_code fieldcode;
8961 if (constructor_fields == 0)
8963 pedwarn_init (loc, 0,
8964 "excess elements in union initializer");
8965 break;
8968 fieldtype = TREE_TYPE (constructor_fields);
8969 if (fieldtype != error_mark_node)
8970 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8971 fieldcode = TREE_CODE (fieldtype);
8973 /* Warn that traditional C rejects initialization of unions.
8974 We skip the warning if the value is zero. This is done
8975 under the assumption that the zero initializer in user
8976 code appears conditioned on e.g. __STDC__ to avoid
8977 "missing initializer" warnings and relies on default
8978 initialization to zero in the traditional C case.
8979 We also skip the warning if the initializer is designated,
8980 again on the assumption that this must be conditional on
8981 __STDC__ anyway (and we've already complained about the
8982 member-designator already). */
8983 if (!in_system_header_at (input_location) && !constructor_designated
8984 && !(value.value && (integer_zerop (value.value)
8985 || real_zerop (value.value))))
8986 warning (OPT_Wtraditional, "traditional C rejects initialization "
8987 "of unions");
8989 /* Accept a string constant to initialize a subarray. */
8990 if (value.value != 0
8991 && fieldcode == ARRAY_TYPE
8992 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8993 && string_flag)
8994 value.value = orig_value;
8995 /* Otherwise, if we have come to a subaggregate,
8996 and we don't have an element of its type, push into it. */
8997 else if (value.value != 0
8998 && value.value != error_mark_node
8999 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9000 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9001 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9003 push_init_level (loc, 1, braced_init_obstack);
9004 continue;
9007 if (value.value)
9009 push_member_name (constructor_fields);
9010 output_init_element (loc, value.value, value.original_type,
9011 strict_string, fieldtype,
9012 constructor_fields, 1, implicit,
9013 braced_init_obstack);
9014 RESTORE_SPELLING_DEPTH (constructor_depth);
9016 else
9017 /* Do the bookkeeping for an element that was
9018 directly output as a constructor. */
9020 constructor_bit_index = DECL_SIZE (constructor_fields);
9021 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9024 constructor_fields = 0;
9026 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9028 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9029 enum tree_code eltcode = TREE_CODE (elttype);
9031 /* Accept a string constant to initialize a subarray. */
9032 if (value.value != 0
9033 && eltcode == ARRAY_TYPE
9034 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9035 && string_flag)
9036 value.value = orig_value;
9037 /* Otherwise, if we have come to a subaggregate,
9038 and we don't have an element of its type, push into it. */
9039 else if (value.value != 0
9040 && value.value != error_mark_node
9041 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9042 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9043 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9045 push_init_level (loc, 1, braced_init_obstack);
9046 continue;
9049 if (constructor_max_index != 0
9050 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9051 || integer_all_onesp (constructor_max_index)))
9053 pedwarn_init (loc, 0,
9054 "excess elements in array initializer");
9055 break;
9058 /* Now output the actual element. */
9059 if (value.value)
9061 push_array_bounds (tree_to_uhwi (constructor_index));
9062 output_init_element (loc, value.value, value.original_type,
9063 strict_string, elttype,
9064 constructor_index, 1, implicit,
9065 braced_init_obstack);
9066 RESTORE_SPELLING_DEPTH (constructor_depth);
9069 constructor_index
9070 = size_binop_loc (input_location, PLUS_EXPR,
9071 constructor_index, bitsize_one_node);
9073 if (!value.value)
9074 /* If we are doing the bookkeeping for an element that was
9075 directly output as a constructor, we must update
9076 constructor_unfilled_index. */
9077 constructor_unfilled_index = constructor_index;
9079 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
9081 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9083 /* Do a basic check of initializer size. Note that vectors
9084 always have a fixed size derived from their type. */
9085 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9087 pedwarn_init (loc, 0,
9088 "excess elements in vector initializer");
9089 break;
9092 /* Now output the actual element. */
9093 if (value.value)
9095 if (TREE_CODE (value.value) == VECTOR_CST)
9096 elttype = TYPE_MAIN_VARIANT (constructor_type);
9097 output_init_element (loc, value.value, value.original_type,
9098 strict_string, elttype,
9099 constructor_index, 1, implicit,
9100 braced_init_obstack);
9103 constructor_index
9104 = size_binop_loc (input_location,
9105 PLUS_EXPR, constructor_index, bitsize_one_node);
9107 if (!value.value)
9108 /* If we are doing the bookkeeping for an element that was
9109 directly output as a constructor, we must update
9110 constructor_unfilled_index. */
9111 constructor_unfilled_index = constructor_index;
9114 /* Handle the sole element allowed in a braced initializer
9115 for a scalar variable. */
9116 else if (constructor_type != error_mark_node
9117 && constructor_fields == 0)
9119 pedwarn_init (loc, 0,
9120 "excess elements in scalar initializer");
9121 break;
9123 else
9125 if (value.value)
9126 output_init_element (loc, value.value, value.original_type,
9127 strict_string, constructor_type,
9128 NULL_TREE, 1, implicit,
9129 braced_init_obstack);
9130 constructor_fields = 0;
9133 /* Handle range initializers either at this level or anywhere higher
9134 in the designator stack. */
9135 if (constructor_range_stack)
9137 struct constructor_range_stack *p, *range_stack;
9138 int finish = 0;
9140 range_stack = constructor_range_stack;
9141 constructor_range_stack = 0;
9142 while (constructor_stack != range_stack->stack)
9144 gcc_assert (constructor_stack->implicit);
9145 process_init_element (loc,
9146 pop_init_level (loc, 1,
9147 braced_init_obstack),
9148 true, braced_init_obstack);
9150 for (p = range_stack;
9151 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9152 p = p->prev)
9154 gcc_assert (constructor_stack->implicit);
9155 process_init_element (loc,
9156 pop_init_level (loc, 1,
9157 braced_init_obstack),
9158 true, braced_init_obstack);
9161 p->index = size_binop_loc (input_location,
9162 PLUS_EXPR, p->index, bitsize_one_node);
9163 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9164 finish = 1;
9166 while (1)
9168 constructor_index = p->index;
9169 constructor_fields = p->fields;
9170 if (finish && p->range_end && p->index == p->range_start)
9172 finish = 0;
9173 p->prev = 0;
9175 p = p->next;
9176 if (!p)
9177 break;
9178 push_init_level (loc, 2, braced_init_obstack);
9179 p->stack = constructor_stack;
9180 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9181 p->index = p->range_start;
9184 if (!finish)
9185 constructor_range_stack = range_stack;
9186 continue;
9189 break;
9192 constructor_range_stack = 0;
9195 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9196 (guaranteed to be 'volatile' or null) and ARGS (represented using
9197 an ASM_EXPR node). */
9198 tree
9199 build_asm_stmt (tree cv_qualifier, tree args)
9201 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9202 ASM_VOLATILE_P (args) = 1;
9203 return add_stmt (args);
9206 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9207 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9208 SIMPLE indicates whether there was anything at all after the
9209 string in the asm expression -- asm("blah") and asm("blah" : )
9210 are subtly different. We use a ASM_EXPR node to represent this. */
9211 tree
9212 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9213 tree clobbers, tree labels, bool simple)
9215 tree tail;
9216 tree args;
9217 int i;
9218 const char *constraint;
9219 const char **oconstraints;
9220 bool allows_mem, allows_reg, is_inout;
9221 int ninputs, noutputs;
9223 ninputs = list_length (inputs);
9224 noutputs = list_length (outputs);
9225 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9227 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9229 /* Remove output conversions that change the type but not the mode. */
9230 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9232 tree output = TREE_VALUE (tail);
9234 output = c_fully_fold (output, false, NULL);
9236 /* ??? Really, this should not be here. Users should be using a
9237 proper lvalue, dammit. But there's a long history of using casts
9238 in the output operands. In cases like longlong.h, this becomes a
9239 primitive form of typechecking -- if the cast can be removed, then
9240 the output operand had a type of the proper width; otherwise we'll
9241 get an error. Gross, but ... */
9242 STRIP_NOPS (output);
9244 if (!lvalue_or_else (loc, output, lv_asm))
9245 output = error_mark_node;
9247 if (output != error_mark_node
9248 && (TREE_READONLY (output)
9249 || TYPE_READONLY (TREE_TYPE (output))
9250 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9251 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9252 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9253 readonly_error (loc, output, lv_asm);
9255 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9256 oconstraints[i] = constraint;
9258 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9259 &allows_mem, &allows_reg, &is_inout))
9261 /* If the operand is going to end up in memory,
9262 mark it addressable. */
9263 if (!allows_reg && !c_mark_addressable (output))
9264 output = error_mark_node;
9265 if (!(!allows_reg && allows_mem)
9266 && output != error_mark_node
9267 && VOID_TYPE_P (TREE_TYPE (output)))
9269 error_at (loc, "invalid use of void expression");
9270 output = error_mark_node;
9273 else
9274 output = error_mark_node;
9276 TREE_VALUE (tail) = output;
9279 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9281 tree input;
9283 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9284 input = TREE_VALUE (tail);
9286 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9287 oconstraints, &allows_mem, &allows_reg))
9289 /* If the operand is going to end up in memory,
9290 mark it addressable. */
9291 if (!allows_reg && allows_mem)
9293 input = c_fully_fold (input, false, NULL);
9295 /* Strip the nops as we allow this case. FIXME, this really
9296 should be rejected or made deprecated. */
9297 STRIP_NOPS (input);
9298 if (!c_mark_addressable (input))
9299 input = error_mark_node;
9301 else
9303 struct c_expr expr;
9304 memset (&expr, 0, sizeof (expr));
9305 expr.value = input;
9306 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9307 input = c_fully_fold (expr.value, false, NULL);
9309 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9311 error_at (loc, "invalid use of void expression");
9312 input = error_mark_node;
9316 else
9317 input = error_mark_node;
9319 TREE_VALUE (tail) = input;
9322 /* ASMs with labels cannot have outputs. This should have been
9323 enforced by the parser. */
9324 gcc_assert (outputs == NULL || labels == NULL);
9326 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9328 /* asm statements without outputs, including simple ones, are treated
9329 as volatile. */
9330 ASM_INPUT_P (args) = simple;
9331 ASM_VOLATILE_P (args) = (noutputs == 0);
9333 return args;
9336 /* Generate a goto statement to LABEL. LOC is the location of the
9337 GOTO. */
9339 tree
9340 c_finish_goto_label (location_t loc, tree label)
9342 tree decl = lookup_label_for_goto (loc, label);
9343 if (!decl)
9344 return NULL_TREE;
9345 TREE_USED (decl) = 1;
9347 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9348 SET_EXPR_LOCATION (t, loc);
9349 return add_stmt (t);
9353 /* Generate a computed goto statement to EXPR. LOC is the location of
9354 the GOTO. */
9356 tree
9357 c_finish_goto_ptr (location_t loc, tree expr)
9359 tree t;
9360 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9361 expr = c_fully_fold (expr, false, NULL);
9362 expr = convert (ptr_type_node, expr);
9363 t = build1 (GOTO_EXPR, void_type_node, expr);
9364 SET_EXPR_LOCATION (t, loc);
9365 return add_stmt (t);
9368 /* Generate a C `return' statement. RETVAL is the expression for what
9369 to return, or a null pointer for `return;' with no value. LOC is
9370 the location of the return statement, or the location of the expression,
9371 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9372 is the original type of RETVAL. */
9374 tree
9375 c_finish_return (location_t loc, tree retval, tree origtype)
9377 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9378 bool no_warning = false;
9379 bool npc = false;
9380 size_t rank = 0;
9382 if (TREE_THIS_VOLATILE (current_function_decl))
9383 warning_at (loc, 0,
9384 "function declared %<noreturn%> has a %<return%> statement");
9386 if (flag_cilkplus && contains_array_notation_expr (retval))
9388 /* Array notations are allowed in a return statement if it is inside a
9389 built-in array notation reduction function. */
9390 if (!find_rank (loc, retval, retval, false, &rank))
9391 return error_mark_node;
9392 if (rank >= 1)
9394 error_at (loc, "array notation expression cannot be used as a "
9395 "return value");
9396 return error_mark_node;
9399 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9401 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9402 "allowed");
9403 return error_mark_node;
9405 if (retval)
9407 tree semantic_type = NULL_TREE;
9408 npc = null_pointer_constant_p (retval);
9409 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9411 semantic_type = TREE_TYPE (retval);
9412 retval = TREE_OPERAND (retval, 0);
9414 retval = c_fully_fold (retval, false, NULL);
9415 if (semantic_type)
9416 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9419 if (!retval)
9421 current_function_returns_null = 1;
9422 if ((warn_return_type || flag_isoc99)
9423 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9425 if (flag_isoc99)
9426 pedwarn (loc, 0, "%<return%> with no value, in "
9427 "function returning non-void");
9428 else
9429 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9430 "in function returning non-void");
9431 no_warning = true;
9434 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9436 current_function_returns_null = 1;
9437 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9438 pedwarn (loc, 0,
9439 "%<return%> with a value, in function returning void");
9440 else
9441 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9442 "%<return%> with expression, in function returning void");
9444 else
9446 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9447 retval, origtype, ic_return,
9448 npc, NULL_TREE, NULL_TREE, 0);
9449 tree res = DECL_RESULT (current_function_decl);
9450 tree inner;
9451 bool save;
9453 current_function_returns_value = 1;
9454 if (t == error_mark_node)
9455 return NULL_TREE;
9457 save = in_late_binary_op;
9458 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9459 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9460 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9461 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9462 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9463 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9464 in_late_binary_op = true;
9465 inner = t = convert (TREE_TYPE (res), t);
9466 in_late_binary_op = save;
9468 /* Strip any conversions, additions, and subtractions, and see if
9469 we are returning the address of a local variable. Warn if so. */
9470 while (1)
9472 switch (TREE_CODE (inner))
9474 CASE_CONVERT:
9475 case NON_LVALUE_EXPR:
9476 case PLUS_EXPR:
9477 case POINTER_PLUS_EXPR:
9478 inner = TREE_OPERAND (inner, 0);
9479 continue;
9481 case MINUS_EXPR:
9482 /* If the second operand of the MINUS_EXPR has a pointer
9483 type (or is converted from it), this may be valid, so
9484 don't give a warning. */
9486 tree op1 = TREE_OPERAND (inner, 1);
9488 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9489 && (CONVERT_EXPR_P (op1)
9490 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9491 op1 = TREE_OPERAND (op1, 0);
9493 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9494 break;
9496 inner = TREE_OPERAND (inner, 0);
9497 continue;
9500 case ADDR_EXPR:
9501 inner = TREE_OPERAND (inner, 0);
9503 while (REFERENCE_CLASS_P (inner)
9504 && TREE_CODE (inner) != INDIRECT_REF)
9505 inner = TREE_OPERAND (inner, 0);
9507 if (DECL_P (inner)
9508 && !DECL_EXTERNAL (inner)
9509 && !TREE_STATIC (inner)
9510 && DECL_CONTEXT (inner) == current_function_decl)
9512 if (TREE_CODE (inner) == LABEL_DECL)
9513 warning_at (loc, OPT_Wreturn_local_addr,
9514 "function returns address of label");
9515 else
9517 warning_at (loc, OPT_Wreturn_local_addr,
9518 "function returns address of local variable");
9519 tree zero = build_zero_cst (TREE_TYPE (res));
9520 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9523 break;
9525 default:
9526 break;
9529 break;
9532 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9533 SET_EXPR_LOCATION (retval, loc);
9535 if (warn_sequence_point)
9536 verify_sequence_points (retval);
9539 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9540 TREE_NO_WARNING (ret_stmt) |= no_warning;
9541 return add_stmt (ret_stmt);
9544 struct c_switch {
9545 /* The SWITCH_EXPR being built. */
9546 tree switch_expr;
9548 /* The original type of the testing expression, i.e. before the
9549 default conversion is applied. */
9550 tree orig_type;
9552 /* A splay-tree mapping the low element of a case range to the high
9553 element, or NULL_TREE if there is no high element. Used to
9554 determine whether or not a new case label duplicates an old case
9555 label. We need a tree, rather than simply a hash table, because
9556 of the GNU case range extension. */
9557 splay_tree cases;
9559 /* The bindings at the point of the switch. This is used for
9560 warnings crossing decls when branching to a case label. */
9561 struct c_spot_bindings *bindings;
9563 /* The next node on the stack. */
9564 struct c_switch *next;
9567 /* A stack of the currently active switch statements. The innermost
9568 switch statement is on the top of the stack. There is no need to
9569 mark the stack for garbage collection because it is only active
9570 during the processing of the body of a function, and we never
9571 collect at that point. */
9573 struct c_switch *c_switch_stack;
9575 /* Start a C switch statement, testing expression EXP. Return the new
9576 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9577 SWITCH_COND_LOC is the location of the switch's condition.
9578 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9580 tree
9581 c_start_case (location_t switch_loc,
9582 location_t switch_cond_loc,
9583 tree exp, bool explicit_cast_p)
9585 tree orig_type = error_mark_node;
9586 struct c_switch *cs;
9588 if (exp != error_mark_node)
9590 orig_type = TREE_TYPE (exp);
9592 if (!INTEGRAL_TYPE_P (orig_type))
9594 if (orig_type != error_mark_node)
9596 error_at (switch_cond_loc, "switch quantity not an integer");
9597 orig_type = error_mark_node;
9599 exp = integer_zero_node;
9601 else
9603 tree type = TYPE_MAIN_VARIANT (orig_type);
9604 tree e = exp;
9606 /* Warn if the condition has boolean value. */
9607 while (TREE_CODE (e) == COMPOUND_EXPR)
9608 e = TREE_OPERAND (e, 1);
9610 if ((TREE_CODE (type) == BOOLEAN_TYPE
9611 || truth_value_p (TREE_CODE (e)))
9612 /* Explicit cast to int suppresses this warning. */
9613 && !(TREE_CODE (type) == INTEGER_TYPE
9614 && explicit_cast_p))
9615 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9616 "switch condition has boolean value");
9618 if (!in_system_header_at (input_location)
9619 && (type == long_integer_type_node
9620 || type == long_unsigned_type_node))
9621 warning_at (switch_cond_loc,
9622 OPT_Wtraditional, "%<long%> switch expression not "
9623 "converted to %<int%> in ISO C");
9625 exp = c_fully_fold (exp, false, NULL);
9626 exp = default_conversion (exp);
9628 if (warn_sequence_point)
9629 verify_sequence_points (exp);
9633 /* Add this new SWITCH_EXPR to the stack. */
9634 cs = XNEW (struct c_switch);
9635 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9636 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9637 cs->orig_type = orig_type;
9638 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9639 cs->bindings = c_get_switch_bindings ();
9640 cs->next = c_switch_stack;
9641 c_switch_stack = cs;
9643 return add_stmt (cs->switch_expr);
9646 /* Process a case label at location LOC. */
9648 tree
9649 do_case (location_t loc, tree low_value, tree high_value)
9651 tree label = NULL_TREE;
9653 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9655 low_value = c_fully_fold (low_value, false, NULL);
9656 if (TREE_CODE (low_value) == INTEGER_CST)
9657 pedwarn (loc, OPT_Wpedantic,
9658 "case label is not an integer constant expression");
9661 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9663 high_value = c_fully_fold (high_value, false, NULL);
9664 if (TREE_CODE (high_value) == INTEGER_CST)
9665 pedwarn (input_location, OPT_Wpedantic,
9666 "case label is not an integer constant expression");
9669 if (c_switch_stack == NULL)
9671 if (low_value)
9672 error_at (loc, "case label not within a switch statement");
9673 else
9674 error_at (loc, "%<default%> label not within a switch statement");
9675 return NULL_TREE;
9678 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9679 EXPR_LOCATION (c_switch_stack->switch_expr),
9680 loc))
9681 return NULL_TREE;
9683 label = c_add_case_label (loc, c_switch_stack->cases,
9684 SWITCH_COND (c_switch_stack->switch_expr),
9685 c_switch_stack->orig_type,
9686 low_value, high_value);
9687 if (label == error_mark_node)
9688 label = NULL_TREE;
9689 return label;
9692 /* Finish the switch statement. TYPE is the original type of the
9693 controlling expression of the switch, or NULL_TREE. */
9695 void
9696 c_finish_case (tree body, tree type)
9698 struct c_switch *cs = c_switch_stack;
9699 location_t switch_location;
9701 SWITCH_BODY (cs->switch_expr) = body;
9703 /* Emit warnings as needed. */
9704 switch_location = EXPR_LOCATION (cs->switch_expr);
9705 c_do_switch_warnings (cs->cases, switch_location,
9706 type ? type : TREE_TYPE (cs->switch_expr),
9707 SWITCH_COND (cs->switch_expr));
9709 /* Pop the stack. */
9710 c_switch_stack = cs->next;
9711 splay_tree_delete (cs->cases);
9712 c_release_switch_bindings (cs->bindings);
9713 XDELETE (cs);
9716 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9717 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9718 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9719 statement, and was not surrounded with parenthesis. */
9721 void
9722 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9723 tree else_block, bool nested_if)
9725 tree stmt;
9727 /* If the condition has array notations, then the rank of the then_block and
9728 else_block must be either 0 or be equal to the rank of the condition. If
9729 the condition does not have array notations then break them up as it is
9730 broken up in a normal expression. */
9731 if (flag_cilkplus && contains_array_notation_expr (cond))
9733 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9734 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9735 return;
9736 if (then_block
9737 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9738 return;
9739 if (else_block
9740 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9741 return;
9742 if (cond_rank != then_rank && then_rank != 0)
9744 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9745 " and the then-block");
9746 return;
9748 else if (cond_rank != else_rank && else_rank != 0)
9750 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9751 " and the else-block");
9752 return;
9755 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9756 if (warn_parentheses && nested_if && else_block == NULL)
9758 tree inner_if = then_block;
9760 /* We know from the grammar productions that there is an IF nested
9761 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9762 it might not be exactly THEN_BLOCK, but should be the last
9763 non-container statement within. */
9764 while (1)
9765 switch (TREE_CODE (inner_if))
9767 case COND_EXPR:
9768 goto found;
9769 case BIND_EXPR:
9770 inner_if = BIND_EXPR_BODY (inner_if);
9771 break;
9772 case STATEMENT_LIST:
9773 inner_if = expr_last (then_block);
9774 break;
9775 case TRY_FINALLY_EXPR:
9776 case TRY_CATCH_EXPR:
9777 inner_if = TREE_OPERAND (inner_if, 0);
9778 break;
9779 default:
9780 gcc_unreachable ();
9782 found:
9784 if (COND_EXPR_ELSE (inner_if))
9785 warning_at (if_locus, OPT_Wparentheses,
9786 "suggest explicit braces to avoid ambiguous %<else%>");
9789 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9790 SET_EXPR_LOCATION (stmt, if_locus);
9791 add_stmt (stmt);
9794 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9795 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9796 is false for DO loops. INCR is the FOR increment expression. BODY is
9797 the statement controlled by the loop. BLAB is the break label. CLAB is
9798 the continue label. Everything is allowed to be NULL. */
9800 void
9801 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9802 tree blab, tree clab, bool cond_is_first)
9804 tree entry = NULL, exit = NULL, t;
9806 /* In theory could forbid cilk spawn for loop increment expression,
9807 but it should work just fine. */
9809 /* If the condition is zero don't generate a loop construct. */
9810 if (cond && integer_zerop (cond))
9812 if (cond_is_first)
9814 t = build_and_jump (&blab);
9815 SET_EXPR_LOCATION (t, start_locus);
9816 add_stmt (t);
9819 else
9821 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9823 /* If we have an exit condition, then we build an IF with gotos either
9824 out of the loop, or to the top of it. If there's no exit condition,
9825 then we just build a jump back to the top. */
9826 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9828 if (cond && !integer_nonzerop (cond))
9830 /* Canonicalize the loop condition to the end. This means
9831 generating a branch to the loop condition. Reuse the
9832 continue label, if possible. */
9833 if (cond_is_first)
9835 if (incr || !clab)
9837 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9838 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9840 else
9841 t = build1 (GOTO_EXPR, void_type_node, clab);
9842 SET_EXPR_LOCATION (t, start_locus);
9843 add_stmt (t);
9846 t = build_and_jump (&blab);
9847 if (cond_is_first)
9848 exit = fold_build3_loc (start_locus,
9849 COND_EXPR, void_type_node, cond, exit, t);
9850 else
9851 exit = fold_build3_loc (input_location,
9852 COND_EXPR, void_type_node, cond, exit, t);
9855 add_stmt (top);
9858 if (body)
9859 add_stmt (body);
9860 if (clab)
9861 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9862 if (incr)
9863 add_stmt (incr);
9864 if (entry)
9865 add_stmt (entry);
9866 if (exit)
9867 add_stmt (exit);
9868 if (blab)
9869 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9872 tree
9873 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9875 bool skip;
9876 tree label = *label_p;
9878 /* In switch statements break is sometimes stylistically used after
9879 a return statement. This can lead to spurious warnings about
9880 control reaching the end of a non-void function when it is
9881 inlined. Note that we are calling block_may_fallthru with
9882 language specific tree nodes; this works because
9883 block_may_fallthru returns true when given something it does not
9884 understand. */
9885 skip = !block_may_fallthru (cur_stmt_list);
9887 if (!label)
9889 if (!skip)
9890 *label_p = label = create_artificial_label (loc);
9892 else if (TREE_CODE (label) == LABEL_DECL)
9894 else switch (TREE_INT_CST_LOW (label))
9896 case 0:
9897 if (is_break)
9898 error_at (loc, "break statement not within loop or switch");
9899 else
9900 error_at (loc, "continue statement not within a loop");
9901 return NULL_TREE;
9903 case 1:
9904 gcc_assert (is_break);
9905 error_at (loc, "break statement used with OpenMP for loop");
9906 return NULL_TREE;
9908 case 2:
9909 if (is_break)
9910 error ("break statement within %<#pragma simd%> loop body");
9911 else
9912 error ("continue statement within %<#pragma simd%> loop body");
9913 return NULL_TREE;
9915 default:
9916 gcc_unreachable ();
9919 if (skip)
9920 return NULL_TREE;
9922 if (!is_break)
9923 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9925 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9928 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9930 static void
9931 emit_side_effect_warnings (location_t loc, tree expr)
9933 if (expr == error_mark_node)
9935 else if (!TREE_SIDE_EFFECTS (expr))
9937 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9938 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9940 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9942 tree r = expr;
9943 location_t cloc = loc;
9944 while (TREE_CODE (r) == COMPOUND_EXPR)
9946 if (EXPR_HAS_LOCATION (r))
9947 cloc = EXPR_LOCATION (r);
9948 r = TREE_OPERAND (r, 1);
9950 if (!TREE_SIDE_EFFECTS (r)
9951 && !VOID_TYPE_P (TREE_TYPE (r))
9952 && !CONVERT_EXPR_P (r)
9953 && !TREE_NO_WARNING (r)
9954 && !TREE_NO_WARNING (expr))
9955 warning_at (cloc, OPT_Wunused_value,
9956 "right-hand operand of comma expression has no effect");
9958 else
9959 warn_if_unused_value (expr, loc);
9962 /* Process an expression as if it were a complete statement. Emit
9963 diagnostics, but do not call ADD_STMT. LOC is the location of the
9964 statement. */
9966 tree
9967 c_process_expr_stmt (location_t loc, tree expr)
9969 tree exprv;
9971 if (!expr)
9972 return NULL_TREE;
9974 expr = c_fully_fold (expr, false, NULL);
9976 if (warn_sequence_point)
9977 verify_sequence_points (expr);
9979 if (TREE_TYPE (expr) != error_mark_node
9980 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9981 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9982 error_at (loc, "expression statement has incomplete type");
9984 /* If we're not processing a statement expression, warn about unused values.
9985 Warnings for statement expressions will be emitted later, once we figure
9986 out which is the result. */
9987 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9988 && warn_unused_value)
9989 emit_side_effect_warnings (loc, expr);
9991 exprv = expr;
9992 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9993 exprv = TREE_OPERAND (exprv, 1);
9994 while (CONVERT_EXPR_P (exprv))
9995 exprv = TREE_OPERAND (exprv, 0);
9996 if (DECL_P (exprv)
9997 || handled_component_p (exprv)
9998 || TREE_CODE (exprv) == ADDR_EXPR)
9999 mark_exp_read (exprv);
10001 /* If the expression is not of a type to which we cannot assign a line
10002 number, wrap the thing in a no-op NOP_EXPR. */
10003 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10005 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10006 SET_EXPR_LOCATION (expr, loc);
10009 return expr;
10012 /* Emit an expression as a statement. LOC is the location of the
10013 expression. */
10015 tree
10016 c_finish_expr_stmt (location_t loc, tree expr)
10018 if (expr)
10019 return add_stmt (c_process_expr_stmt (loc, expr));
10020 else
10021 return NULL;
10024 /* Do the opposite and emit a statement as an expression. To begin,
10025 create a new binding level and return it. */
10027 tree
10028 c_begin_stmt_expr (void)
10030 tree ret;
10032 /* We must force a BLOCK for this level so that, if it is not expanded
10033 later, there is a way to turn off the entire subtree of blocks that
10034 are contained in it. */
10035 keep_next_level ();
10036 ret = c_begin_compound_stmt (true);
10038 c_bindings_start_stmt_expr (c_switch_stack == NULL
10039 ? NULL
10040 : c_switch_stack->bindings);
10042 /* Mark the current statement list as belonging to a statement list. */
10043 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10045 return ret;
10048 /* LOC is the location of the compound statement to which this body
10049 belongs. */
10051 tree
10052 c_finish_stmt_expr (location_t loc, tree body)
10054 tree last, type, tmp, val;
10055 tree *last_p;
10057 body = c_end_compound_stmt (loc, body, true);
10059 c_bindings_end_stmt_expr (c_switch_stack == NULL
10060 ? NULL
10061 : c_switch_stack->bindings);
10063 /* Locate the last statement in BODY. See c_end_compound_stmt
10064 about always returning a BIND_EXPR. */
10065 last_p = &BIND_EXPR_BODY (body);
10066 last = BIND_EXPR_BODY (body);
10068 continue_searching:
10069 if (TREE_CODE (last) == STATEMENT_LIST)
10071 tree_stmt_iterator i;
10073 /* This can happen with degenerate cases like ({ }). No value. */
10074 if (!TREE_SIDE_EFFECTS (last))
10075 return body;
10077 /* If we're supposed to generate side effects warnings, process
10078 all of the statements except the last. */
10079 if (warn_unused_value)
10081 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10083 location_t tloc;
10084 tree t = tsi_stmt (i);
10086 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10087 emit_side_effect_warnings (tloc, t);
10090 else
10091 i = tsi_last (last);
10092 last_p = tsi_stmt_ptr (i);
10093 last = *last_p;
10096 /* If the end of the list is exception related, then the list was split
10097 by a call to push_cleanup. Continue searching. */
10098 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10099 || TREE_CODE (last) == TRY_CATCH_EXPR)
10101 last_p = &TREE_OPERAND (last, 0);
10102 last = *last_p;
10103 goto continue_searching;
10106 if (last == error_mark_node)
10107 return last;
10109 /* In the case that the BIND_EXPR is not necessary, return the
10110 expression out from inside it. */
10111 if (last == BIND_EXPR_BODY (body)
10112 && BIND_EXPR_VARS (body) == NULL)
10114 /* Even if this looks constant, do not allow it in a constant
10115 expression. */
10116 last = c_wrap_maybe_const (last, true);
10117 /* Do not warn if the return value of a statement expression is
10118 unused. */
10119 TREE_NO_WARNING (last) = 1;
10120 return last;
10123 /* Extract the type of said expression. */
10124 type = TREE_TYPE (last);
10126 /* If we're not returning a value at all, then the BIND_EXPR that
10127 we already have is a fine expression to return. */
10128 if (!type || VOID_TYPE_P (type))
10129 return body;
10131 /* Now that we've located the expression containing the value, it seems
10132 silly to make voidify_wrapper_expr repeat the process. Create a
10133 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10134 tmp = create_tmp_var_raw (type);
10136 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10137 tree_expr_nonnegative_p giving up immediately. */
10138 val = last;
10139 if (TREE_CODE (val) == NOP_EXPR
10140 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10141 val = TREE_OPERAND (val, 0);
10143 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10144 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10147 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10148 SET_EXPR_LOCATION (t, loc);
10149 return t;
10153 /* Begin and end compound statements. This is as simple as pushing
10154 and popping new statement lists from the tree. */
10156 tree
10157 c_begin_compound_stmt (bool do_scope)
10159 tree stmt = push_stmt_list ();
10160 if (do_scope)
10161 push_scope ();
10162 return stmt;
10165 /* End a compound statement. STMT is the statement. LOC is the
10166 location of the compound statement-- this is usually the location
10167 of the opening brace. */
10169 tree
10170 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10172 tree block = NULL;
10174 if (do_scope)
10176 if (c_dialect_objc ())
10177 objc_clear_super_receiver ();
10178 block = pop_scope ();
10181 stmt = pop_stmt_list (stmt);
10182 stmt = c_build_bind_expr (loc, block, stmt);
10184 /* If this compound statement is nested immediately inside a statement
10185 expression, then force a BIND_EXPR to be created. Otherwise we'll
10186 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10187 STATEMENT_LISTs merge, and thus we can lose track of what statement
10188 was really last. */
10189 if (building_stmt_list_p ()
10190 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10191 && TREE_CODE (stmt) != BIND_EXPR)
10193 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10194 TREE_SIDE_EFFECTS (stmt) = 1;
10195 SET_EXPR_LOCATION (stmt, loc);
10198 return stmt;
10201 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10202 when the current scope is exited. EH_ONLY is true when this is not
10203 meant to apply to normal control flow transfer. */
10205 void
10206 push_cleanup (tree decl, tree cleanup, bool eh_only)
10208 enum tree_code code;
10209 tree stmt, list;
10210 bool stmt_expr;
10212 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10213 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10214 add_stmt (stmt);
10215 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10216 list = push_stmt_list ();
10217 TREE_OPERAND (stmt, 0) = list;
10218 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10221 /* Build a binary-operation expression without default conversions.
10222 CODE is the kind of expression to build.
10223 LOCATION is the operator's location.
10224 This function differs from `build' in several ways:
10225 the data type of the result is computed and recorded in it,
10226 warnings are generated if arg data types are invalid,
10227 special handling for addition and subtraction of pointers is known,
10228 and some optimization is done (operations on narrow ints
10229 are done in the narrower type when that gives the same result).
10230 Constant folding is also done before the result is returned.
10232 Note that the operands will never have enumeral types, or function
10233 or array types, because either they will have the default conversions
10234 performed or they have both just been converted to some other type in which
10235 the arithmetic is to be done. */
10237 tree
10238 build_binary_op (location_t location, enum tree_code code,
10239 tree orig_op0, tree orig_op1, int convert_p)
10241 tree type0, type1, orig_type0, orig_type1;
10242 tree eptype;
10243 enum tree_code code0, code1;
10244 tree op0, op1;
10245 tree ret = error_mark_node;
10246 const char *invalid_op_diag;
10247 bool op0_int_operands, op1_int_operands;
10248 bool int_const, int_const_or_overflow, int_operands;
10250 /* Expression code to give to the expression when it is built.
10251 Normally this is CODE, which is what the caller asked for,
10252 but in some special cases we change it. */
10253 enum tree_code resultcode = code;
10255 /* Data type in which the computation is to be performed.
10256 In the simplest cases this is the common type of the arguments. */
10257 tree result_type = NULL;
10259 /* When the computation is in excess precision, the type of the
10260 final EXCESS_PRECISION_EXPR. */
10261 tree semantic_result_type = NULL;
10263 /* Nonzero means operands have already been type-converted
10264 in whatever way is necessary.
10265 Zero means they need to be converted to RESULT_TYPE. */
10266 int converted = 0;
10268 /* Nonzero means create the expression with this type, rather than
10269 RESULT_TYPE. */
10270 tree build_type = 0;
10272 /* Nonzero means after finally constructing the expression
10273 convert it to this type. */
10274 tree final_type = 0;
10276 /* Nonzero if this is an operation like MIN or MAX which can
10277 safely be computed in short if both args are promoted shorts.
10278 Also implies COMMON.
10279 -1 indicates a bitwise operation; this makes a difference
10280 in the exact conditions for when it is safe to do the operation
10281 in a narrower mode. */
10282 int shorten = 0;
10284 /* Nonzero if this is a comparison operation;
10285 if both args are promoted shorts, compare the original shorts.
10286 Also implies COMMON. */
10287 int short_compare = 0;
10289 /* Nonzero if this is a right-shift operation, which can be computed on the
10290 original short and then promoted if the operand is a promoted short. */
10291 int short_shift = 0;
10293 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10294 int common = 0;
10296 /* True means types are compatible as far as ObjC is concerned. */
10297 bool objc_ok;
10299 /* True means this is an arithmetic operation that may need excess
10300 precision. */
10301 bool may_need_excess_precision;
10303 /* True means this is a boolean operation that converts both its
10304 operands to truth-values. */
10305 bool boolean_op = false;
10307 /* Remember whether we're doing / or %. */
10308 bool doing_div_or_mod = false;
10310 /* Remember whether we're doing << or >>. */
10311 bool doing_shift = false;
10313 /* Tree holding instrumentation expression. */
10314 tree instrument_expr = NULL;
10316 if (location == UNKNOWN_LOCATION)
10317 location = input_location;
10319 op0 = orig_op0;
10320 op1 = orig_op1;
10322 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10323 if (op0_int_operands)
10324 op0 = remove_c_maybe_const_expr (op0);
10325 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10326 if (op1_int_operands)
10327 op1 = remove_c_maybe_const_expr (op1);
10328 int_operands = (op0_int_operands && op1_int_operands);
10329 if (int_operands)
10331 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10332 && TREE_CODE (orig_op1) == INTEGER_CST);
10333 int_const = (int_const_or_overflow
10334 && !TREE_OVERFLOW (orig_op0)
10335 && !TREE_OVERFLOW (orig_op1));
10337 else
10338 int_const = int_const_or_overflow = false;
10340 /* Do not apply default conversion in mixed vector/scalar expression. */
10341 if (convert_p
10342 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10343 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10345 op0 = default_conversion (op0);
10346 op1 = default_conversion (op1);
10349 /* When Cilk Plus is enabled and there are array notations inside op0, then
10350 we check to see if there are builtin array notation functions. If
10351 so, then we take on the type of the array notation inside it. */
10352 if (flag_cilkplus && contains_array_notation_expr (op0))
10353 orig_type0 = type0 = find_correct_array_notation_type (op0);
10354 else
10355 orig_type0 = type0 = TREE_TYPE (op0);
10357 if (flag_cilkplus && contains_array_notation_expr (op1))
10358 orig_type1 = type1 = find_correct_array_notation_type (op1);
10359 else
10360 orig_type1 = type1 = TREE_TYPE (op1);
10362 /* The expression codes of the data types of the arguments tell us
10363 whether the arguments are integers, floating, pointers, etc. */
10364 code0 = TREE_CODE (type0);
10365 code1 = TREE_CODE (type1);
10367 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10368 STRIP_TYPE_NOPS (op0);
10369 STRIP_TYPE_NOPS (op1);
10371 /* If an error was already reported for one of the arguments,
10372 avoid reporting another error. */
10374 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10375 return error_mark_node;
10377 if ((invalid_op_diag
10378 = targetm.invalid_binary_op (code, type0, type1)))
10380 error_at (location, invalid_op_diag);
10381 return error_mark_node;
10384 switch (code)
10386 case PLUS_EXPR:
10387 case MINUS_EXPR:
10388 case MULT_EXPR:
10389 case TRUNC_DIV_EXPR:
10390 case CEIL_DIV_EXPR:
10391 case FLOOR_DIV_EXPR:
10392 case ROUND_DIV_EXPR:
10393 case EXACT_DIV_EXPR:
10394 may_need_excess_precision = true;
10395 break;
10396 default:
10397 may_need_excess_precision = false;
10398 break;
10400 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10402 op0 = TREE_OPERAND (op0, 0);
10403 type0 = TREE_TYPE (op0);
10405 else if (may_need_excess_precision
10406 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10408 type0 = eptype;
10409 op0 = convert (eptype, op0);
10411 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10413 op1 = TREE_OPERAND (op1, 0);
10414 type1 = TREE_TYPE (op1);
10416 else if (may_need_excess_precision
10417 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10419 type1 = eptype;
10420 op1 = convert (eptype, op1);
10423 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10425 /* In case when one of the operands of the binary operation is
10426 a vector and another is a scalar -- convert scalar to vector. */
10427 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10429 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10430 true);
10432 switch (convert_flag)
10434 case stv_error:
10435 return error_mark_node;
10436 case stv_firstarg:
10438 bool maybe_const = true;
10439 tree sc;
10440 sc = c_fully_fold (op0, false, &maybe_const);
10441 sc = save_expr (sc);
10442 sc = convert (TREE_TYPE (type1), sc);
10443 op0 = build_vector_from_val (type1, sc);
10444 if (!maybe_const)
10445 op0 = c_wrap_maybe_const (op0, true);
10446 orig_type0 = type0 = TREE_TYPE (op0);
10447 code0 = TREE_CODE (type0);
10448 converted = 1;
10449 break;
10451 case stv_secondarg:
10453 bool maybe_const = true;
10454 tree sc;
10455 sc = c_fully_fold (op1, false, &maybe_const);
10456 sc = save_expr (sc);
10457 sc = convert (TREE_TYPE (type0), sc);
10458 op1 = build_vector_from_val (type0, sc);
10459 if (!maybe_const)
10460 op1 = c_wrap_maybe_const (op1, true);
10461 orig_type1 = type1 = TREE_TYPE (op1);
10462 code1 = TREE_CODE (type1);
10463 converted = 1;
10464 break;
10466 default:
10467 break;
10471 switch (code)
10473 case PLUS_EXPR:
10474 /* Handle the pointer + int case. */
10475 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10477 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10478 goto return_build_binary_op;
10480 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10482 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10483 goto return_build_binary_op;
10485 else
10486 common = 1;
10487 break;
10489 case MINUS_EXPR:
10490 /* Subtraction of two similar pointers.
10491 We must subtract them as integers, then divide by object size. */
10492 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10493 && comp_target_types (location, type0, type1))
10495 ret = pointer_diff (location, op0, op1);
10496 goto return_build_binary_op;
10498 /* Handle pointer minus int. Just like pointer plus int. */
10499 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10501 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10502 goto return_build_binary_op;
10504 else
10505 common = 1;
10506 break;
10508 case MULT_EXPR:
10509 common = 1;
10510 break;
10512 case TRUNC_DIV_EXPR:
10513 case CEIL_DIV_EXPR:
10514 case FLOOR_DIV_EXPR:
10515 case ROUND_DIV_EXPR:
10516 case EXACT_DIV_EXPR:
10517 doing_div_or_mod = true;
10518 warn_for_div_by_zero (location, op1);
10520 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10521 || code0 == FIXED_POINT_TYPE
10522 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10523 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10524 || code1 == FIXED_POINT_TYPE
10525 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10527 enum tree_code tcode0 = code0, tcode1 = code1;
10529 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10530 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10531 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10532 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10534 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10535 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10536 resultcode = RDIV_EXPR;
10537 else
10538 /* Although it would be tempting to shorten always here, that
10539 loses on some targets, since the modulo instruction is
10540 undefined if the quotient can't be represented in the
10541 computation mode. We shorten only if unsigned or if
10542 dividing by something we know != -1. */
10543 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10544 || (TREE_CODE (op1) == INTEGER_CST
10545 && !integer_all_onesp (op1)));
10546 common = 1;
10548 break;
10550 case BIT_AND_EXPR:
10551 case BIT_IOR_EXPR:
10552 case BIT_XOR_EXPR:
10553 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10554 shorten = -1;
10555 /* Allow vector types which are not floating point types. */
10556 else if (code0 == VECTOR_TYPE
10557 && code1 == VECTOR_TYPE
10558 && !VECTOR_FLOAT_TYPE_P (type0)
10559 && !VECTOR_FLOAT_TYPE_P (type1))
10560 common = 1;
10561 break;
10563 case TRUNC_MOD_EXPR:
10564 case FLOOR_MOD_EXPR:
10565 doing_div_or_mod = true;
10566 warn_for_div_by_zero (location, op1);
10568 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10569 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10570 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10571 common = 1;
10572 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10574 /* Although it would be tempting to shorten always here, that loses
10575 on some targets, since the modulo instruction is undefined if the
10576 quotient can't be represented in the computation mode. We shorten
10577 only if unsigned or if dividing by something we know != -1. */
10578 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10579 || (TREE_CODE (op1) == INTEGER_CST
10580 && !integer_all_onesp (op1)));
10581 common = 1;
10583 break;
10585 case TRUTH_ANDIF_EXPR:
10586 case TRUTH_ORIF_EXPR:
10587 case TRUTH_AND_EXPR:
10588 case TRUTH_OR_EXPR:
10589 case TRUTH_XOR_EXPR:
10590 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10591 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10592 || code0 == FIXED_POINT_TYPE)
10593 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10594 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10595 || code1 == FIXED_POINT_TYPE))
10597 /* Result of these operations is always an int,
10598 but that does not mean the operands should be
10599 converted to ints! */
10600 result_type = integer_type_node;
10601 if (op0_int_operands)
10603 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10604 op0 = remove_c_maybe_const_expr (op0);
10606 else
10607 op0 = c_objc_common_truthvalue_conversion (location, op0);
10608 if (op1_int_operands)
10610 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10611 op1 = remove_c_maybe_const_expr (op1);
10613 else
10614 op1 = c_objc_common_truthvalue_conversion (location, op1);
10615 converted = 1;
10616 boolean_op = true;
10618 if (code == TRUTH_ANDIF_EXPR)
10620 int_const_or_overflow = (int_operands
10621 && TREE_CODE (orig_op0) == INTEGER_CST
10622 && (op0 == truthvalue_false_node
10623 || TREE_CODE (orig_op1) == INTEGER_CST));
10624 int_const = (int_const_or_overflow
10625 && !TREE_OVERFLOW (orig_op0)
10626 && (op0 == truthvalue_false_node
10627 || !TREE_OVERFLOW (orig_op1)));
10629 else if (code == TRUTH_ORIF_EXPR)
10631 int_const_or_overflow = (int_operands
10632 && TREE_CODE (orig_op0) == INTEGER_CST
10633 && (op0 == truthvalue_true_node
10634 || TREE_CODE (orig_op1) == INTEGER_CST));
10635 int_const = (int_const_or_overflow
10636 && !TREE_OVERFLOW (orig_op0)
10637 && (op0 == truthvalue_true_node
10638 || !TREE_OVERFLOW (orig_op1)));
10640 break;
10642 /* Shift operations: result has same type as first operand;
10643 always convert second operand to int.
10644 Also set SHORT_SHIFT if shifting rightward. */
10646 case RSHIFT_EXPR:
10647 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10648 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10650 result_type = type0;
10651 converted = 1;
10653 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10654 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10655 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10656 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10658 result_type = type0;
10659 converted = 1;
10661 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10662 && code1 == INTEGER_TYPE)
10664 doing_shift = true;
10665 if (TREE_CODE (op1) == INTEGER_CST)
10667 if (tree_int_cst_sgn (op1) < 0)
10669 int_const = false;
10670 if (c_inhibit_evaluation_warnings == 0)
10671 warning_at (location, OPT_Wshift_count_negative,
10672 "right shift count is negative");
10674 else
10676 if (!integer_zerop (op1))
10677 short_shift = 1;
10679 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10681 int_const = false;
10682 if (c_inhibit_evaluation_warnings == 0)
10683 warning_at (location, OPT_Wshift_count_overflow,
10684 "right shift count >= width of type");
10689 /* Use the type of the value to be shifted. */
10690 result_type = type0;
10691 /* Avoid converting op1 to result_type later. */
10692 converted = 1;
10694 break;
10696 case LSHIFT_EXPR:
10697 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10698 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10700 result_type = type0;
10701 converted = 1;
10703 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10704 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10705 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10706 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10708 result_type = type0;
10709 converted = 1;
10711 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10712 && code1 == INTEGER_TYPE)
10714 doing_shift = true;
10715 if (TREE_CODE (op1) == INTEGER_CST)
10717 if (tree_int_cst_sgn (op1) < 0)
10719 int_const = false;
10720 if (c_inhibit_evaluation_warnings == 0)
10721 warning_at (location, OPT_Wshift_count_negative,
10722 "left shift count is negative");
10725 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10727 int_const = false;
10728 if (c_inhibit_evaluation_warnings == 0)
10729 warning_at (location, OPT_Wshift_count_overflow,
10730 "left shift count >= width of type");
10734 /* Use the type of the value to be shifted. */
10735 result_type = type0;
10736 /* Avoid converting op1 to result_type later. */
10737 converted = 1;
10739 break;
10741 case EQ_EXPR:
10742 case NE_EXPR:
10743 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10745 tree intt;
10746 if (!vector_types_compatible_elements_p (type0, type1))
10748 error_at (location, "comparing vectors with different "
10749 "element types");
10750 return error_mark_node;
10753 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10755 error_at (location, "comparing vectors with different "
10756 "number of elements");
10757 return error_mark_node;
10760 /* Always construct signed integer vector type. */
10761 intt = c_common_type_for_size (GET_MODE_BITSIZE
10762 (TYPE_MODE (TREE_TYPE (type0))), 0);
10763 result_type = build_opaque_vector_type (intt,
10764 TYPE_VECTOR_SUBPARTS (type0));
10765 converted = 1;
10766 break;
10768 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10769 warning_at (location,
10770 OPT_Wfloat_equal,
10771 "comparing floating point with == or != is unsafe");
10772 /* Result of comparison is always int,
10773 but don't convert the args to int! */
10774 build_type = integer_type_node;
10775 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10776 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10777 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10778 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10779 short_compare = 1;
10780 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10782 if (TREE_CODE (op0) == ADDR_EXPR
10783 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10785 if (code == EQ_EXPR)
10786 warning_at (location,
10787 OPT_Waddress,
10788 "the comparison will always evaluate as %<false%> "
10789 "for the address of %qD will never be NULL",
10790 TREE_OPERAND (op0, 0));
10791 else
10792 warning_at (location,
10793 OPT_Waddress,
10794 "the comparison will always evaluate as %<true%> "
10795 "for the address of %qD will never be NULL",
10796 TREE_OPERAND (op0, 0));
10798 result_type = type0;
10800 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10802 if (TREE_CODE (op1) == ADDR_EXPR
10803 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10805 if (code == EQ_EXPR)
10806 warning_at (location,
10807 OPT_Waddress,
10808 "the comparison will always evaluate as %<false%> "
10809 "for the address of %qD will never be NULL",
10810 TREE_OPERAND (op1, 0));
10811 else
10812 warning_at (location,
10813 OPT_Waddress,
10814 "the comparison will always evaluate as %<true%> "
10815 "for the address of %qD will never be NULL",
10816 TREE_OPERAND (op1, 0));
10818 result_type = type1;
10820 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10822 tree tt0 = TREE_TYPE (type0);
10823 tree tt1 = TREE_TYPE (type1);
10824 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10825 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10826 addr_space_t as_common = ADDR_SPACE_GENERIC;
10828 /* Anything compares with void *. void * compares with anything.
10829 Otherwise, the targets must be compatible
10830 and both must be object or both incomplete. */
10831 if (comp_target_types (location, type0, type1))
10832 result_type = common_pointer_type (type0, type1);
10833 else if (!addr_space_superset (as0, as1, &as_common))
10835 error_at (location, "comparison of pointers to "
10836 "disjoint address spaces");
10837 return error_mark_node;
10839 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10841 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10842 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10843 "comparison of %<void *%> with function pointer");
10845 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10847 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10848 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10849 "comparison of %<void *%> with function pointer");
10851 else
10852 /* Avoid warning about the volatile ObjC EH puts on decls. */
10853 if (!objc_ok)
10854 pedwarn (location, 0,
10855 "comparison of distinct pointer types lacks a cast");
10857 if (result_type == NULL_TREE)
10859 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10860 result_type = build_pointer_type
10861 (build_qualified_type (void_type_node, qual));
10864 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10866 result_type = type0;
10867 pedwarn (location, 0, "comparison between pointer and integer");
10869 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10871 result_type = type1;
10872 pedwarn (location, 0, "comparison between pointer and integer");
10874 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10875 || truth_value_p (TREE_CODE (orig_op0)))
10876 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10877 || truth_value_p (TREE_CODE (orig_op1))))
10878 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10879 break;
10881 case LE_EXPR:
10882 case GE_EXPR:
10883 case LT_EXPR:
10884 case GT_EXPR:
10885 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10887 tree intt;
10888 if (!vector_types_compatible_elements_p (type0, type1))
10890 error_at (location, "comparing vectors with different "
10891 "element types");
10892 return error_mark_node;
10895 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10897 error_at (location, "comparing vectors with different "
10898 "number of elements");
10899 return error_mark_node;
10902 /* Always construct signed integer vector type. */
10903 intt = c_common_type_for_size (GET_MODE_BITSIZE
10904 (TYPE_MODE (TREE_TYPE (type0))), 0);
10905 result_type = build_opaque_vector_type (intt,
10906 TYPE_VECTOR_SUBPARTS (type0));
10907 converted = 1;
10908 break;
10910 build_type = integer_type_node;
10911 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10912 || code0 == FIXED_POINT_TYPE)
10913 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10914 || code1 == FIXED_POINT_TYPE))
10915 short_compare = 1;
10916 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10918 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10919 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10920 addr_space_t as_common;
10922 if (comp_target_types (location, type0, type1))
10924 result_type = common_pointer_type (type0, type1);
10925 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10926 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10927 pedwarn (location, 0,
10928 "comparison of complete and incomplete pointers");
10929 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10930 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10931 "ordered comparisons of pointers to functions");
10932 else if (null_pointer_constant_p (orig_op0)
10933 || null_pointer_constant_p (orig_op1))
10934 warning_at (location, OPT_Wextra,
10935 "ordered comparison of pointer with null pointer");
10938 else if (!addr_space_superset (as0, as1, &as_common))
10940 error_at (location, "comparison of pointers to "
10941 "disjoint address spaces");
10942 return error_mark_node;
10944 else
10946 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10947 result_type = build_pointer_type
10948 (build_qualified_type (void_type_node, qual));
10949 pedwarn (location, 0,
10950 "comparison of distinct pointer types lacks a cast");
10953 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10955 result_type = type0;
10956 if (pedantic)
10957 pedwarn (location, OPT_Wpedantic,
10958 "ordered comparison of pointer with integer zero");
10959 else if (extra_warnings)
10960 warning_at (location, OPT_Wextra,
10961 "ordered comparison of pointer with integer zero");
10963 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10965 result_type = type1;
10966 if (pedantic)
10967 pedwarn (location, OPT_Wpedantic,
10968 "ordered comparison of pointer with integer zero");
10969 else if (extra_warnings)
10970 warning_at (location, OPT_Wextra,
10971 "ordered comparison of pointer with integer zero");
10973 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10975 result_type = type0;
10976 pedwarn (location, 0, "comparison between pointer and integer");
10978 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10980 result_type = type1;
10981 pedwarn (location, 0, "comparison between pointer and integer");
10983 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10984 || truth_value_p (TREE_CODE (orig_op0)))
10985 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10986 || truth_value_p (TREE_CODE (orig_op1))))
10987 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10988 break;
10990 default:
10991 gcc_unreachable ();
10994 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10995 return error_mark_node;
10997 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10998 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10999 || !vector_types_compatible_elements_p (type0, type1)))
11001 binary_op_error (location, code, type0, type1);
11002 return error_mark_node;
11005 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11006 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11008 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11009 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11011 bool first_complex = (code0 == COMPLEX_TYPE);
11012 bool second_complex = (code1 == COMPLEX_TYPE);
11013 int none_complex = (!first_complex && !second_complex);
11015 if (shorten || common || short_compare)
11017 result_type = c_common_type (type0, type1);
11018 do_warn_double_promotion (result_type, type0, type1,
11019 "implicit conversion from %qT to %qT "
11020 "to match other operand of binary "
11021 "expression",
11022 location);
11023 if (result_type == error_mark_node)
11024 return error_mark_node;
11027 if (first_complex != second_complex
11028 && (code == PLUS_EXPR
11029 || code == MINUS_EXPR
11030 || code == MULT_EXPR
11031 || (code == TRUNC_DIV_EXPR && first_complex))
11032 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11033 && flag_signed_zeros)
11035 /* An operation on mixed real/complex operands must be
11036 handled specially, but the language-independent code can
11037 more easily optimize the plain complex arithmetic if
11038 -fno-signed-zeros. */
11039 tree real_type = TREE_TYPE (result_type);
11040 tree real, imag;
11041 if (type0 != orig_type0 || type1 != orig_type1)
11043 gcc_assert (may_need_excess_precision && common);
11044 semantic_result_type = c_common_type (orig_type0, orig_type1);
11046 if (first_complex)
11048 if (TREE_TYPE (op0) != result_type)
11049 op0 = convert_and_check (location, result_type, op0);
11050 if (TREE_TYPE (op1) != real_type)
11051 op1 = convert_and_check (location, real_type, op1);
11053 else
11055 if (TREE_TYPE (op0) != real_type)
11056 op0 = convert_and_check (location, real_type, op0);
11057 if (TREE_TYPE (op1) != result_type)
11058 op1 = convert_and_check (location, result_type, op1);
11060 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11061 return error_mark_node;
11062 if (first_complex)
11064 op0 = c_save_expr (op0);
11065 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11066 op0, 1);
11067 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11068 op0, 1);
11069 switch (code)
11071 case MULT_EXPR:
11072 case TRUNC_DIV_EXPR:
11073 op1 = c_save_expr (op1);
11074 imag = build2 (resultcode, real_type, imag, op1);
11075 /* Fall through. */
11076 case PLUS_EXPR:
11077 case MINUS_EXPR:
11078 real = build2 (resultcode, real_type, real, op1);
11079 break;
11080 default:
11081 gcc_unreachable();
11084 else
11086 op1 = c_save_expr (op1);
11087 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11088 op1, 1);
11089 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11090 op1, 1);
11091 switch (code)
11093 case MULT_EXPR:
11094 op0 = c_save_expr (op0);
11095 imag = build2 (resultcode, real_type, op0, imag);
11096 /* Fall through. */
11097 case PLUS_EXPR:
11098 real = build2 (resultcode, real_type, op0, real);
11099 break;
11100 case MINUS_EXPR:
11101 real = build2 (resultcode, real_type, op0, real);
11102 imag = build1 (NEGATE_EXPR, real_type, imag);
11103 break;
11104 default:
11105 gcc_unreachable();
11108 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11109 goto return_build_binary_op;
11112 /* For certain operations (which identify themselves by shorten != 0)
11113 if both args were extended from the same smaller type,
11114 do the arithmetic in that type and then extend.
11116 shorten !=0 and !=1 indicates a bitwise operation.
11117 For them, this optimization is safe only if
11118 both args are zero-extended or both are sign-extended.
11119 Otherwise, we might change the result.
11120 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11121 but calculated in (unsigned short) it would be (unsigned short)-1. */
11123 if (shorten && none_complex)
11125 final_type = result_type;
11126 result_type = shorten_binary_op (result_type, op0, op1,
11127 shorten == -1);
11130 /* Shifts can be shortened if shifting right. */
11132 if (short_shift)
11134 int unsigned_arg;
11135 tree arg0 = get_narrower (op0, &unsigned_arg);
11137 final_type = result_type;
11139 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11140 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11142 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11143 && tree_int_cst_sgn (op1) > 0
11144 /* We can shorten only if the shift count is less than the
11145 number of bits in the smaller type size. */
11146 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11147 /* We cannot drop an unsigned shift after sign-extension. */
11148 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11150 /* Do an unsigned shift if the operand was zero-extended. */
11151 result_type
11152 = c_common_signed_or_unsigned_type (unsigned_arg,
11153 TREE_TYPE (arg0));
11154 /* Convert value-to-be-shifted to that type. */
11155 if (TREE_TYPE (op0) != result_type)
11156 op0 = convert (result_type, op0);
11157 converted = 1;
11161 /* Comparison operations are shortened too but differently.
11162 They identify themselves by setting short_compare = 1. */
11164 if (short_compare)
11166 /* Don't write &op0, etc., because that would prevent op0
11167 from being kept in a register.
11168 Instead, make copies of the our local variables and
11169 pass the copies by reference, then copy them back afterward. */
11170 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11171 enum tree_code xresultcode = resultcode;
11172 tree val
11173 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11174 &xresultcode);
11176 if (val != 0)
11178 ret = val;
11179 goto return_build_binary_op;
11182 op0 = xop0, op1 = xop1;
11183 converted = 1;
11184 resultcode = xresultcode;
11186 if (c_inhibit_evaluation_warnings == 0)
11188 bool op0_maybe_const = true;
11189 bool op1_maybe_const = true;
11190 tree orig_op0_folded, orig_op1_folded;
11192 if (in_late_binary_op)
11194 orig_op0_folded = orig_op0;
11195 orig_op1_folded = orig_op1;
11197 else
11199 /* Fold for the sake of possible warnings, as in
11200 build_conditional_expr. This requires the
11201 "original" values to be folded, not just op0 and
11202 op1. */
11203 c_inhibit_evaluation_warnings++;
11204 op0 = c_fully_fold (op0, require_constant_value,
11205 &op0_maybe_const);
11206 op1 = c_fully_fold (op1, require_constant_value,
11207 &op1_maybe_const);
11208 c_inhibit_evaluation_warnings--;
11209 orig_op0_folded = c_fully_fold (orig_op0,
11210 require_constant_value,
11211 NULL);
11212 orig_op1_folded = c_fully_fold (orig_op1,
11213 require_constant_value,
11214 NULL);
11217 if (warn_sign_compare)
11218 warn_for_sign_compare (location, orig_op0_folded,
11219 orig_op1_folded, op0, op1,
11220 result_type, resultcode);
11221 if (!in_late_binary_op && !int_operands)
11223 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11224 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11225 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11226 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11232 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11233 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11234 Then the expression will be built.
11235 It will be given type FINAL_TYPE if that is nonzero;
11236 otherwise, it will be given type RESULT_TYPE. */
11238 if (!result_type)
11240 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11241 return error_mark_node;
11244 if (build_type == NULL_TREE)
11246 build_type = result_type;
11247 if ((type0 != orig_type0 || type1 != orig_type1)
11248 && !boolean_op)
11250 gcc_assert (may_need_excess_precision && common);
11251 semantic_result_type = c_common_type (orig_type0, orig_type1);
11255 if (!converted)
11257 op0 = ep_convert_and_check (location, result_type, op0,
11258 semantic_result_type);
11259 op1 = ep_convert_and_check (location, result_type, op1,
11260 semantic_result_type);
11262 /* This can happen if one operand has a vector type, and the other
11263 has a different type. */
11264 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11265 return error_mark_node;
11268 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11269 | SANITIZE_FLOAT_DIVIDE))
11270 && do_ubsan_in_current_function ()
11271 && (doing_div_or_mod || doing_shift))
11273 /* OP0 and/or OP1 might have side-effects. */
11274 op0 = c_save_expr (op0);
11275 op1 = c_save_expr (op1);
11276 op0 = c_fully_fold (op0, false, NULL);
11277 op1 = c_fully_fold (op1, false, NULL);
11278 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11279 | SANITIZE_FLOAT_DIVIDE)))
11280 instrument_expr = ubsan_instrument_division (location, op0, op1);
11281 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11282 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11285 /* Treat expressions in initializers specially as they can't trap. */
11286 if (int_const_or_overflow)
11287 ret = (require_constant_value
11288 ? fold_build2_initializer_loc (location, resultcode, build_type,
11289 op0, op1)
11290 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11291 else
11292 ret = build2 (resultcode, build_type, op0, op1);
11293 if (final_type != 0)
11294 ret = convert (final_type, ret);
11296 return_build_binary_op:
11297 gcc_assert (ret != error_mark_node);
11298 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11299 ret = (int_operands
11300 ? note_integer_operands (ret)
11301 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11302 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11303 && !in_late_binary_op)
11304 ret = note_integer_operands (ret);
11305 if (semantic_result_type)
11306 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11307 protected_set_expr_location (ret, location);
11309 if (instrument_expr != NULL)
11310 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11311 instrument_expr, ret);
11313 return ret;
11317 /* Convert EXPR to be a truth-value, validating its type for this
11318 purpose. LOCATION is the source location for the expression. */
11320 tree
11321 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11323 bool int_const, int_operands;
11325 switch (TREE_CODE (TREE_TYPE (expr)))
11327 case ARRAY_TYPE:
11328 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11329 return error_mark_node;
11331 case RECORD_TYPE:
11332 error_at (location, "used struct type value where scalar is required");
11333 return error_mark_node;
11335 case UNION_TYPE:
11336 error_at (location, "used union type value where scalar is required");
11337 return error_mark_node;
11339 case VOID_TYPE:
11340 error_at (location, "void value not ignored as it ought to be");
11341 return error_mark_node;
11343 case FUNCTION_TYPE:
11344 gcc_unreachable ();
11346 case VECTOR_TYPE:
11347 error_at (location, "used vector type where scalar is required");
11348 return error_mark_node;
11350 default:
11351 break;
11354 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11355 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11356 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11358 expr = remove_c_maybe_const_expr (expr);
11359 expr = build2 (NE_EXPR, integer_type_node, expr,
11360 convert (TREE_TYPE (expr), integer_zero_node));
11361 expr = note_integer_operands (expr);
11363 else
11364 /* ??? Should we also give an error for vectors rather than leaving
11365 those to give errors later? */
11366 expr = c_common_truthvalue_conversion (location, expr);
11368 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11370 if (TREE_OVERFLOW (expr))
11371 return expr;
11372 else
11373 return note_integer_operands (expr);
11375 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11376 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11377 return expr;
11381 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11382 required. */
11384 tree
11385 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11387 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11389 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11390 /* Executing a compound literal inside a function reinitializes
11391 it. */
11392 if (!TREE_STATIC (decl))
11393 *se = true;
11394 return decl;
11396 else
11397 return expr;
11400 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11401 statement. LOC is the location of the OACC_PARALLEL. */
11403 tree
11404 c_finish_oacc_parallel (location_t loc, tree clauses, tree block)
11406 tree stmt;
11408 block = c_end_compound_stmt (loc, block, true);
11410 stmt = make_node (OACC_PARALLEL);
11411 TREE_TYPE (stmt) = void_type_node;
11412 OACC_PARALLEL_CLAUSES (stmt) = clauses;
11413 OACC_PARALLEL_BODY (stmt) = block;
11414 SET_EXPR_LOCATION (stmt, loc);
11416 return add_stmt (stmt);
11419 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11420 statement. LOC is the location of the OACC_KERNELS. */
11422 tree
11423 c_finish_oacc_kernels (location_t loc, tree clauses, tree block)
11425 tree stmt;
11427 block = c_end_compound_stmt (loc, block, true);
11429 stmt = make_node (OACC_KERNELS);
11430 TREE_TYPE (stmt) = void_type_node;
11431 OACC_KERNELS_CLAUSES (stmt) = clauses;
11432 OACC_KERNELS_BODY (stmt) = block;
11433 SET_EXPR_LOCATION (stmt, loc);
11435 return add_stmt (stmt);
11438 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11439 statement. LOC is the location of the OACC_DATA. */
11441 tree
11442 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11444 tree stmt;
11446 block = c_end_compound_stmt (loc, block, true);
11448 stmt = make_node (OACC_DATA);
11449 TREE_TYPE (stmt) = void_type_node;
11450 OACC_DATA_CLAUSES (stmt) = clauses;
11451 OACC_DATA_BODY (stmt) = block;
11452 SET_EXPR_LOCATION (stmt, loc);
11454 return add_stmt (stmt);
11457 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11459 tree
11460 c_begin_omp_parallel (void)
11462 tree block;
11464 keep_next_level ();
11465 block = c_begin_compound_stmt (true);
11467 return block;
11470 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11471 statement. LOC is the location of the OMP_PARALLEL. */
11473 tree
11474 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11476 tree stmt;
11478 block = c_end_compound_stmt (loc, block, true);
11480 stmt = make_node (OMP_PARALLEL);
11481 TREE_TYPE (stmt) = void_type_node;
11482 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11483 OMP_PARALLEL_BODY (stmt) = block;
11484 SET_EXPR_LOCATION (stmt, loc);
11486 return add_stmt (stmt);
11489 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11491 tree
11492 c_begin_omp_task (void)
11494 tree block;
11496 keep_next_level ();
11497 block = c_begin_compound_stmt (true);
11499 return block;
11502 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11503 statement. LOC is the location of the #pragma. */
11505 tree
11506 c_finish_omp_task (location_t loc, tree clauses, tree block)
11508 tree stmt;
11510 block = c_end_compound_stmt (loc, block, true);
11512 stmt = make_node (OMP_TASK);
11513 TREE_TYPE (stmt) = void_type_node;
11514 OMP_TASK_CLAUSES (stmt) = clauses;
11515 OMP_TASK_BODY (stmt) = block;
11516 SET_EXPR_LOCATION (stmt, loc);
11518 return add_stmt (stmt);
11521 /* Generate GOMP_cancel call for #pragma omp cancel. */
11523 void
11524 c_finish_omp_cancel (location_t loc, tree clauses)
11526 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11527 int mask = 0;
11528 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11529 mask = 1;
11530 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11531 mask = 2;
11532 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11533 mask = 4;
11534 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11535 mask = 8;
11536 else
11538 error_at (loc, "%<#pragma omp cancel must specify one of "
11539 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11540 "clauses");
11541 return;
11543 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11544 if (ifc != NULL_TREE)
11546 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11547 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11548 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11549 build_zero_cst (type));
11551 else
11552 ifc = boolean_true_node;
11553 tree stmt = build_call_expr_loc (loc, fn, 2,
11554 build_int_cst (integer_type_node, mask),
11555 ifc);
11556 add_stmt (stmt);
11559 /* Generate GOMP_cancellation_point call for
11560 #pragma omp cancellation point. */
11562 void
11563 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11565 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11566 int mask = 0;
11567 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11568 mask = 1;
11569 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11570 mask = 2;
11571 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11572 mask = 4;
11573 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11574 mask = 8;
11575 else
11577 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11578 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11579 "clauses");
11580 return;
11582 tree stmt = build_call_expr_loc (loc, fn, 1,
11583 build_int_cst (integer_type_node, mask));
11584 add_stmt (stmt);
11587 /* Helper function for handle_omp_array_sections. Called recursively
11588 to handle multiple array-section-subscripts. C is the clause,
11589 T current expression (initially OMP_CLAUSE_DECL), which is either
11590 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11591 expression if specified, TREE_VALUE length expression if specified,
11592 TREE_CHAIN is what it has been specified after, or some decl.
11593 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11594 set to true if any of the array-section-subscript could have length
11595 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11596 first array-section-subscript which is known not to have length
11597 of one. Given say:
11598 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11599 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11600 all are or may have length of 1, array-section-subscript [:2] is the
11601 first one knonwn not to have length 1. For array-section-subscript
11602 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11603 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11604 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11605 case though, as some lengths could be zero. */
11607 static tree
11608 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11609 bool &maybe_zero_len, unsigned int &first_non_one)
11611 tree ret, low_bound, length, type;
11612 if (TREE_CODE (t) != TREE_LIST)
11614 if (error_operand_p (t))
11615 return error_mark_node;
11616 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11618 if (DECL_P (t))
11619 error_at (OMP_CLAUSE_LOCATION (c),
11620 "%qD is not a variable in %qs clause", t,
11621 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11622 else
11623 error_at (OMP_CLAUSE_LOCATION (c),
11624 "%qE is not a variable in %qs clause", t,
11625 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11626 return error_mark_node;
11628 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11629 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11631 error_at (OMP_CLAUSE_LOCATION (c),
11632 "%qD is threadprivate variable in %qs clause", t,
11633 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11634 return error_mark_node;
11636 return t;
11639 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11640 maybe_zero_len, first_non_one);
11641 if (ret == error_mark_node || ret == NULL_TREE)
11642 return ret;
11644 type = TREE_TYPE (ret);
11645 low_bound = TREE_PURPOSE (t);
11646 length = TREE_VALUE (t);
11648 if (low_bound == error_mark_node || length == error_mark_node)
11649 return error_mark_node;
11651 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11653 error_at (OMP_CLAUSE_LOCATION (c),
11654 "low bound %qE of array section does not have integral type",
11655 low_bound);
11656 return error_mark_node;
11658 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11660 error_at (OMP_CLAUSE_LOCATION (c),
11661 "length %qE of array section does not have integral type",
11662 length);
11663 return error_mark_node;
11665 if (low_bound
11666 && TREE_CODE (low_bound) == INTEGER_CST
11667 && TYPE_PRECISION (TREE_TYPE (low_bound))
11668 > TYPE_PRECISION (sizetype))
11669 low_bound = fold_convert (sizetype, low_bound);
11670 if (length
11671 && TREE_CODE (length) == INTEGER_CST
11672 && TYPE_PRECISION (TREE_TYPE (length))
11673 > TYPE_PRECISION (sizetype))
11674 length = fold_convert (sizetype, length);
11675 if (low_bound == NULL_TREE)
11676 low_bound = integer_zero_node;
11678 if (length != NULL_TREE)
11680 if (!integer_nonzerop (length))
11681 maybe_zero_len = true;
11682 if (first_non_one == types.length ()
11683 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11684 first_non_one++;
11686 if (TREE_CODE (type) == ARRAY_TYPE)
11688 if (length == NULL_TREE
11689 && (TYPE_DOMAIN (type) == NULL_TREE
11690 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11692 error_at (OMP_CLAUSE_LOCATION (c),
11693 "for unknown bound array type length expression must "
11694 "be specified");
11695 return error_mark_node;
11697 if (TREE_CODE (low_bound) == INTEGER_CST
11698 && tree_int_cst_sgn (low_bound) == -1)
11700 error_at (OMP_CLAUSE_LOCATION (c),
11701 "negative low bound in array section in %qs clause",
11702 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11703 return error_mark_node;
11705 if (length != NULL_TREE
11706 && TREE_CODE (length) == INTEGER_CST
11707 && tree_int_cst_sgn (length) == -1)
11709 error_at (OMP_CLAUSE_LOCATION (c),
11710 "negative length in array section in %qs clause",
11711 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11712 return error_mark_node;
11714 if (TYPE_DOMAIN (type)
11715 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11716 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11717 == INTEGER_CST)
11719 tree size = size_binop (PLUS_EXPR,
11720 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11721 size_one_node);
11722 if (TREE_CODE (low_bound) == INTEGER_CST)
11724 if (tree_int_cst_lt (size, low_bound))
11726 error_at (OMP_CLAUSE_LOCATION (c),
11727 "low bound %qE above array section size "
11728 "in %qs clause", low_bound,
11729 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11730 return error_mark_node;
11732 if (tree_int_cst_equal (size, low_bound))
11733 maybe_zero_len = true;
11734 else if (length == NULL_TREE
11735 && first_non_one == types.length ()
11736 && tree_int_cst_equal
11737 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11738 low_bound))
11739 first_non_one++;
11741 else if (length == NULL_TREE)
11743 maybe_zero_len = true;
11744 if (first_non_one == types.length ())
11745 first_non_one++;
11747 if (length && TREE_CODE (length) == INTEGER_CST)
11749 if (tree_int_cst_lt (size, length))
11751 error_at (OMP_CLAUSE_LOCATION (c),
11752 "length %qE above array section size "
11753 "in %qs clause", length,
11754 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11755 return error_mark_node;
11757 if (TREE_CODE (low_bound) == INTEGER_CST)
11759 tree lbpluslen
11760 = size_binop (PLUS_EXPR,
11761 fold_convert (sizetype, low_bound),
11762 fold_convert (sizetype, length));
11763 if (TREE_CODE (lbpluslen) == INTEGER_CST
11764 && tree_int_cst_lt (size, lbpluslen))
11766 error_at (OMP_CLAUSE_LOCATION (c),
11767 "high bound %qE above array section size "
11768 "in %qs clause", lbpluslen,
11769 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11770 return error_mark_node;
11775 else if (length == NULL_TREE)
11777 maybe_zero_len = true;
11778 if (first_non_one == types.length ())
11779 first_non_one++;
11782 /* For [lb:] we will need to evaluate lb more than once. */
11783 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11785 tree lb = c_save_expr (low_bound);
11786 if (lb != low_bound)
11788 TREE_PURPOSE (t) = lb;
11789 low_bound = lb;
11793 else if (TREE_CODE (type) == POINTER_TYPE)
11795 if (length == NULL_TREE)
11797 error_at (OMP_CLAUSE_LOCATION (c),
11798 "for pointer type length expression must be specified");
11799 return error_mark_node;
11801 /* If there is a pointer type anywhere but in the very first
11802 array-section-subscript, the array section can't be contiguous. */
11803 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11804 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11806 error_at (OMP_CLAUSE_LOCATION (c),
11807 "array section is not contiguous in %qs clause",
11808 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11809 return error_mark_node;
11812 else
11814 error_at (OMP_CLAUSE_LOCATION (c),
11815 "%qE does not have pointer or array type", ret);
11816 return error_mark_node;
11818 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11819 types.safe_push (TREE_TYPE (ret));
11820 /* We will need to evaluate lb more than once. */
11821 tree lb = c_save_expr (low_bound);
11822 if (lb != low_bound)
11824 TREE_PURPOSE (t) = lb;
11825 low_bound = lb;
11827 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11828 return ret;
11831 /* Handle array sections for clause C. */
11833 static bool
11834 handle_omp_array_sections (tree c)
11836 bool maybe_zero_len = false;
11837 unsigned int first_non_one = 0;
11838 vec<tree> types = vNULL;
11839 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11840 maybe_zero_len, first_non_one);
11841 if (first == error_mark_node)
11843 types.release ();
11844 return true;
11846 if (first == NULL_TREE)
11848 types.release ();
11849 return false;
11851 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11853 tree t = OMP_CLAUSE_DECL (c);
11854 tree tem = NULL_TREE;
11855 types.release ();
11856 /* Need to evaluate side effects in the length expressions
11857 if any. */
11858 while (TREE_CODE (t) == TREE_LIST)
11860 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11862 if (tem == NULL_TREE)
11863 tem = TREE_VALUE (t);
11864 else
11865 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11866 TREE_VALUE (t), tem);
11868 t = TREE_CHAIN (t);
11870 if (tem)
11871 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11872 first = c_fully_fold (first, false, NULL);
11873 OMP_CLAUSE_DECL (c) = first;
11875 else
11877 unsigned int num = types.length (), i;
11878 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11879 tree condition = NULL_TREE;
11881 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11882 maybe_zero_len = true;
11884 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11885 t = TREE_CHAIN (t))
11887 tree low_bound = TREE_PURPOSE (t);
11888 tree length = TREE_VALUE (t);
11890 i--;
11891 if (low_bound
11892 && TREE_CODE (low_bound) == INTEGER_CST
11893 && TYPE_PRECISION (TREE_TYPE (low_bound))
11894 > TYPE_PRECISION (sizetype))
11895 low_bound = fold_convert (sizetype, low_bound);
11896 if (length
11897 && TREE_CODE (length) == INTEGER_CST
11898 && TYPE_PRECISION (TREE_TYPE (length))
11899 > TYPE_PRECISION (sizetype))
11900 length = fold_convert (sizetype, length);
11901 if (low_bound == NULL_TREE)
11902 low_bound = integer_zero_node;
11903 if (!maybe_zero_len && i > first_non_one)
11905 if (integer_nonzerop (low_bound))
11906 goto do_warn_noncontiguous;
11907 if (length != NULL_TREE
11908 && TREE_CODE (length) == INTEGER_CST
11909 && TYPE_DOMAIN (types[i])
11910 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11911 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11912 == INTEGER_CST)
11914 tree size;
11915 size = size_binop (PLUS_EXPR,
11916 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11917 size_one_node);
11918 if (!tree_int_cst_equal (length, size))
11920 do_warn_noncontiguous:
11921 error_at (OMP_CLAUSE_LOCATION (c),
11922 "array section is not contiguous in %qs "
11923 "clause",
11924 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11925 types.release ();
11926 return true;
11929 if (length != NULL_TREE
11930 && TREE_SIDE_EFFECTS (length))
11932 if (side_effects == NULL_TREE)
11933 side_effects = length;
11934 else
11935 side_effects = build2 (COMPOUND_EXPR,
11936 TREE_TYPE (side_effects),
11937 length, side_effects);
11940 else
11942 tree l;
11944 if (i > first_non_one && length && integer_nonzerop (length))
11945 continue;
11946 if (length)
11947 l = fold_convert (sizetype, length);
11948 else
11950 l = size_binop (PLUS_EXPR,
11951 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11952 size_one_node);
11953 l = size_binop (MINUS_EXPR, l,
11954 fold_convert (sizetype, low_bound));
11956 if (i > first_non_one)
11958 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11959 size_zero_node);
11960 if (condition == NULL_TREE)
11961 condition = l;
11962 else
11963 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11964 l, condition);
11966 else if (size == NULL_TREE)
11968 size = size_in_bytes (TREE_TYPE (types[i]));
11969 size = size_binop (MULT_EXPR, size, l);
11970 if (condition)
11971 size = fold_build3 (COND_EXPR, sizetype, condition,
11972 size, size_zero_node);
11974 else
11975 size = size_binop (MULT_EXPR, size, l);
11978 types.release ();
11979 if (side_effects)
11980 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11981 first = c_fully_fold (first, false, NULL);
11982 OMP_CLAUSE_DECL (c) = first;
11983 if (size)
11984 size = c_fully_fold (size, false, NULL);
11985 OMP_CLAUSE_SIZE (c) = size;
11986 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11987 return false;
11988 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
11989 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11990 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
11991 if (!c_mark_addressable (t))
11992 return false;
11993 OMP_CLAUSE_DECL (c2) = t;
11994 t = build_fold_addr_expr (first);
11995 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11996 tree ptr = OMP_CLAUSE_DECL (c2);
11997 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11998 ptr = build_fold_addr_expr (ptr);
11999 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12000 ptrdiff_type_node, t,
12001 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12002 ptrdiff_type_node, ptr));
12003 t = c_fully_fold (t, false, NULL);
12004 OMP_CLAUSE_SIZE (c2) = t;
12005 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12006 OMP_CLAUSE_CHAIN (c) = c2;
12008 return false;
12011 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12012 an inline call. But, remap
12013 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12014 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12016 static tree
12017 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12018 tree decl, tree placeholder)
12020 copy_body_data id;
12021 hash_map<tree, tree> decl_map;
12023 decl_map.put (omp_decl1, placeholder);
12024 decl_map.put (omp_decl2, decl);
12025 memset (&id, 0, sizeof (id));
12026 id.src_fn = DECL_CONTEXT (omp_decl1);
12027 id.dst_fn = current_function_decl;
12028 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12029 id.decl_map = &decl_map;
12031 id.copy_decl = copy_decl_no_change;
12032 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12033 id.transform_new_cfg = true;
12034 id.transform_return_to_modify = false;
12035 id.transform_lang_insert_block = NULL;
12036 id.eh_lp_nr = 0;
12037 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12038 return stmt;
12041 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12042 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12044 static tree
12045 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12047 if (*tp == (tree) data)
12048 return *tp;
12049 return NULL_TREE;
12052 /* For all elements of CLAUSES, validate them against their constraints.
12053 Remove any elements from the list that are invalid. */
12055 tree
12056 c_finish_omp_clauses (tree clauses)
12058 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12059 bitmap_head aligned_head;
12060 tree c, t, *pc;
12061 bool branch_seen = false;
12062 bool copyprivate_seen = false;
12063 tree *nowait_clause = NULL;
12065 bitmap_obstack_initialize (NULL);
12066 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12067 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12068 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12069 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12071 for (pc = &clauses, c = clauses; c ; c = *pc)
12073 bool remove = false;
12074 bool need_complete = false;
12075 bool need_implicitly_determined = false;
12077 switch (OMP_CLAUSE_CODE (c))
12079 case OMP_CLAUSE_SHARED:
12080 need_implicitly_determined = true;
12081 goto check_dup_generic;
12083 case OMP_CLAUSE_PRIVATE:
12084 need_complete = true;
12085 need_implicitly_determined = true;
12086 goto check_dup_generic;
12088 case OMP_CLAUSE_REDUCTION:
12089 need_implicitly_determined = true;
12090 t = OMP_CLAUSE_DECL (c);
12091 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12092 && (FLOAT_TYPE_P (TREE_TYPE (t))
12093 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
12095 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12096 const char *r_name = NULL;
12098 switch (r_code)
12100 case PLUS_EXPR:
12101 case MULT_EXPR:
12102 case MINUS_EXPR:
12103 break;
12104 case MIN_EXPR:
12105 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12106 r_name = "min";
12107 break;
12108 case MAX_EXPR:
12109 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12110 r_name = "max";
12111 break;
12112 case BIT_AND_EXPR:
12113 r_name = "&";
12114 break;
12115 case BIT_XOR_EXPR:
12116 r_name = "^";
12117 break;
12118 case BIT_IOR_EXPR:
12119 r_name = "|";
12120 break;
12121 case TRUTH_ANDIF_EXPR:
12122 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12123 r_name = "&&";
12124 break;
12125 case TRUTH_ORIF_EXPR:
12126 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12127 r_name = "||";
12128 break;
12129 default:
12130 gcc_unreachable ();
12132 if (r_name)
12134 error_at (OMP_CLAUSE_LOCATION (c),
12135 "%qE has invalid type for %<reduction(%s)%>",
12136 t, r_name);
12137 remove = true;
12138 break;
12141 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12143 error_at (OMP_CLAUSE_LOCATION (c),
12144 "user defined reduction not found for %qD", t);
12145 remove = true;
12146 break;
12148 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12150 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12151 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12152 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12153 VAR_DECL, NULL_TREE, type);
12154 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12155 DECL_ARTIFICIAL (placeholder) = 1;
12156 DECL_IGNORED_P (placeholder) = 1;
12157 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12158 c_mark_addressable (placeholder);
12159 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12160 c_mark_addressable (OMP_CLAUSE_DECL (c));
12161 OMP_CLAUSE_REDUCTION_MERGE (c)
12162 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12163 TREE_VEC_ELT (list, 0),
12164 TREE_VEC_ELT (list, 1),
12165 OMP_CLAUSE_DECL (c), placeholder);
12166 OMP_CLAUSE_REDUCTION_MERGE (c)
12167 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12168 void_type_node, NULL_TREE,
12169 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12170 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12171 if (TREE_VEC_LENGTH (list) == 6)
12173 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12174 c_mark_addressable (OMP_CLAUSE_DECL (c));
12175 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12176 c_mark_addressable (placeholder);
12177 tree init = TREE_VEC_ELT (list, 5);
12178 if (init == error_mark_node)
12179 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12180 OMP_CLAUSE_REDUCTION_INIT (c)
12181 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12182 TREE_VEC_ELT (list, 3),
12183 OMP_CLAUSE_DECL (c), placeholder);
12184 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12185 OMP_CLAUSE_REDUCTION_INIT (c)
12186 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12187 OMP_CLAUSE_REDUCTION_INIT (c));
12188 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12189 c_find_omp_placeholder_r,
12190 placeholder, NULL))
12191 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12193 else
12195 tree init;
12196 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12197 init = build_constructor (TREE_TYPE (t), NULL);
12198 else
12199 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12200 OMP_CLAUSE_REDUCTION_INIT (c)
12201 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12203 OMP_CLAUSE_REDUCTION_INIT (c)
12204 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12205 void_type_node, NULL_TREE,
12206 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12207 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12209 goto check_dup_generic;
12211 case OMP_CLAUSE_COPYPRIVATE:
12212 copyprivate_seen = true;
12213 if (nowait_clause)
12215 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12216 "%<nowait%> clause must not be used together "
12217 "with %<copyprivate%>");
12218 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12219 nowait_clause = NULL;
12221 goto check_dup_generic;
12223 case OMP_CLAUSE_COPYIN:
12224 t = OMP_CLAUSE_DECL (c);
12225 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12227 error_at (OMP_CLAUSE_LOCATION (c),
12228 "%qE must be %<threadprivate%> for %<copyin%>", t);
12229 remove = true;
12230 break;
12232 goto check_dup_generic;
12234 case OMP_CLAUSE_LINEAR:
12235 t = OMP_CLAUSE_DECL (c);
12236 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12237 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12239 error_at (OMP_CLAUSE_LOCATION (c),
12240 "linear clause applied to non-integral non-pointer "
12241 "variable with type %qT", TREE_TYPE (t));
12242 remove = true;
12243 break;
12245 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12247 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12248 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12249 OMP_CLAUSE_DECL (c), s);
12250 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12251 sizetype, s, OMP_CLAUSE_DECL (c));
12252 if (s == error_mark_node)
12253 s = size_one_node;
12254 OMP_CLAUSE_LINEAR_STEP (c) = s;
12256 else
12257 OMP_CLAUSE_LINEAR_STEP (c)
12258 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12259 goto check_dup_generic;
12261 check_dup_generic:
12262 t = OMP_CLAUSE_DECL (c);
12263 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12265 error_at (OMP_CLAUSE_LOCATION (c),
12266 "%qE is not a variable in clause %qs", t,
12267 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12268 remove = true;
12270 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12271 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12272 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12274 error_at (OMP_CLAUSE_LOCATION (c),
12275 "%qE appears more than once in data clauses", t);
12276 remove = true;
12278 else
12279 bitmap_set_bit (&generic_head, DECL_UID (t));
12280 break;
12282 case OMP_CLAUSE_FIRSTPRIVATE:
12283 t = OMP_CLAUSE_DECL (c);
12284 need_complete = true;
12285 need_implicitly_determined = true;
12286 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12288 error_at (OMP_CLAUSE_LOCATION (c),
12289 "%qE is not a variable in clause %<firstprivate%>", t);
12290 remove = true;
12292 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12293 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12295 error_at (OMP_CLAUSE_LOCATION (c),
12296 "%qE appears more than once in data clauses", t);
12297 remove = true;
12299 else
12300 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12301 break;
12303 case OMP_CLAUSE_LASTPRIVATE:
12304 t = OMP_CLAUSE_DECL (c);
12305 need_complete = true;
12306 need_implicitly_determined = true;
12307 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12309 error_at (OMP_CLAUSE_LOCATION (c),
12310 "%qE is not a variable in clause %<lastprivate%>", t);
12311 remove = true;
12313 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12314 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12316 error_at (OMP_CLAUSE_LOCATION (c),
12317 "%qE appears more than once in data clauses", t);
12318 remove = true;
12320 else
12321 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12322 break;
12324 case OMP_CLAUSE_ALIGNED:
12325 t = OMP_CLAUSE_DECL (c);
12326 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12328 error_at (OMP_CLAUSE_LOCATION (c),
12329 "%qE is not a variable in %<aligned%> clause", t);
12330 remove = true;
12332 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12333 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12335 error_at (OMP_CLAUSE_LOCATION (c),
12336 "%qE in %<aligned%> clause is neither a pointer nor "
12337 "an array", t);
12338 remove = true;
12340 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12342 error_at (OMP_CLAUSE_LOCATION (c),
12343 "%qE appears more than once in %<aligned%> clauses",
12345 remove = true;
12347 else
12348 bitmap_set_bit (&aligned_head, DECL_UID (t));
12349 break;
12351 case OMP_CLAUSE_DEPEND:
12352 t = OMP_CLAUSE_DECL (c);
12353 if (TREE_CODE (t) == TREE_LIST)
12355 if (handle_omp_array_sections (c))
12356 remove = true;
12357 break;
12359 if (t == error_mark_node)
12360 remove = true;
12361 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12363 error_at (OMP_CLAUSE_LOCATION (c),
12364 "%qE is not a variable in %<depend%> clause", t);
12365 remove = true;
12367 else if (!c_mark_addressable (t))
12368 remove = true;
12369 break;
12371 case OMP_CLAUSE_MAP:
12372 case OMP_CLAUSE_TO:
12373 case OMP_CLAUSE_FROM:
12374 case OMP_CLAUSE__CACHE_:
12375 t = OMP_CLAUSE_DECL (c);
12376 if (TREE_CODE (t) == TREE_LIST)
12378 if (handle_omp_array_sections (c))
12379 remove = true;
12380 else
12382 t = OMP_CLAUSE_DECL (c);
12383 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12385 error_at (OMP_CLAUSE_LOCATION (c),
12386 "array section does not have mappable type "
12387 "in %qs clause",
12388 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12389 remove = true;
12392 break;
12394 if (t == error_mark_node)
12395 remove = true;
12396 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12398 error_at (OMP_CLAUSE_LOCATION (c),
12399 "%qE is not a variable in %qs clause", t,
12400 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12401 remove = true;
12403 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12405 error_at (OMP_CLAUSE_LOCATION (c),
12406 "%qD is threadprivate variable in %qs clause", t,
12407 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12408 remove = true;
12410 else if (!c_mark_addressable (t))
12411 remove = true;
12412 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12413 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12414 || (OMP_CLAUSE_MAP_KIND (c)
12415 == GOMP_MAP_FORCE_DEVICEPTR)))
12416 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12418 error_at (OMP_CLAUSE_LOCATION (c),
12419 "%qD does not have a mappable type in %qs clause", t,
12420 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12421 remove = true;
12423 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12425 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12426 error ("%qD appears more than once in motion clauses", t);
12427 else
12428 error ("%qD appears more than once in map clauses", t);
12429 remove = true;
12431 else
12432 bitmap_set_bit (&generic_head, DECL_UID (t));
12433 break;
12435 case OMP_CLAUSE_UNIFORM:
12436 t = OMP_CLAUSE_DECL (c);
12437 if (TREE_CODE (t) != PARM_DECL)
12439 if (DECL_P (t))
12440 error_at (OMP_CLAUSE_LOCATION (c),
12441 "%qD is not an argument in %<uniform%> clause", t);
12442 else
12443 error_at (OMP_CLAUSE_LOCATION (c),
12444 "%qE is not an argument in %<uniform%> clause", t);
12445 remove = true;
12446 break;
12448 goto check_dup_generic;
12450 case OMP_CLAUSE_NOWAIT:
12451 if (copyprivate_seen)
12453 error_at (OMP_CLAUSE_LOCATION (c),
12454 "%<nowait%> clause must not be used together "
12455 "with %<copyprivate%>");
12456 remove = true;
12457 break;
12459 nowait_clause = pc;
12460 pc = &OMP_CLAUSE_CHAIN (c);
12461 continue;
12463 case OMP_CLAUSE_IF:
12464 case OMP_CLAUSE_NUM_THREADS:
12465 case OMP_CLAUSE_NUM_TEAMS:
12466 case OMP_CLAUSE_THREAD_LIMIT:
12467 case OMP_CLAUSE_SCHEDULE:
12468 case OMP_CLAUSE_ORDERED:
12469 case OMP_CLAUSE_DEFAULT:
12470 case OMP_CLAUSE_UNTIED:
12471 case OMP_CLAUSE_COLLAPSE:
12472 case OMP_CLAUSE_FINAL:
12473 case OMP_CLAUSE_MERGEABLE:
12474 case OMP_CLAUSE_SAFELEN:
12475 case OMP_CLAUSE_SIMDLEN:
12476 case OMP_CLAUSE_DEVICE:
12477 case OMP_CLAUSE_DIST_SCHEDULE:
12478 case OMP_CLAUSE_PARALLEL:
12479 case OMP_CLAUSE_FOR:
12480 case OMP_CLAUSE_SECTIONS:
12481 case OMP_CLAUSE_TASKGROUP:
12482 case OMP_CLAUSE_PROC_BIND:
12483 case OMP_CLAUSE__CILK_FOR_COUNT_:
12484 case OMP_CLAUSE_NUM_GANGS:
12485 case OMP_CLAUSE_NUM_WORKERS:
12486 case OMP_CLAUSE_VECTOR_LENGTH:
12487 case OMP_CLAUSE_ASYNC:
12488 case OMP_CLAUSE_WAIT:
12489 case OMP_CLAUSE_AUTO:
12490 case OMP_CLAUSE_SEQ:
12491 case OMP_CLAUSE_GANG:
12492 case OMP_CLAUSE_WORKER:
12493 case OMP_CLAUSE_VECTOR:
12494 pc = &OMP_CLAUSE_CHAIN (c);
12495 continue;
12497 case OMP_CLAUSE_INBRANCH:
12498 case OMP_CLAUSE_NOTINBRANCH:
12499 if (branch_seen)
12501 error_at (OMP_CLAUSE_LOCATION (c),
12502 "%<inbranch%> clause is incompatible with "
12503 "%<notinbranch%>");
12504 remove = true;
12505 break;
12507 branch_seen = true;
12508 pc = &OMP_CLAUSE_CHAIN (c);
12509 continue;
12511 default:
12512 gcc_unreachable ();
12515 if (!remove)
12517 t = OMP_CLAUSE_DECL (c);
12519 if (need_complete)
12521 t = require_complete_type (t);
12522 if (t == error_mark_node)
12523 remove = true;
12526 if (need_implicitly_determined)
12528 const char *share_name = NULL;
12530 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12531 share_name = "threadprivate";
12532 else switch (c_omp_predetermined_sharing (t))
12534 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12535 break;
12536 case OMP_CLAUSE_DEFAULT_SHARED:
12537 /* const vars may be specified in firstprivate clause. */
12538 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12539 && TREE_READONLY (t))
12540 break;
12541 share_name = "shared";
12542 break;
12543 case OMP_CLAUSE_DEFAULT_PRIVATE:
12544 share_name = "private";
12545 break;
12546 default:
12547 gcc_unreachable ();
12549 if (share_name)
12551 error_at (OMP_CLAUSE_LOCATION (c),
12552 "%qE is predetermined %qs for %qs",
12553 t, share_name,
12554 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12555 remove = true;
12560 if (remove)
12561 *pc = OMP_CLAUSE_CHAIN (c);
12562 else
12563 pc = &OMP_CLAUSE_CHAIN (c);
12566 bitmap_obstack_release (NULL);
12567 return clauses;
12570 /* Create a transaction node. */
12572 tree
12573 c_finish_transaction (location_t loc, tree block, int flags)
12575 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12576 if (flags & TM_STMT_ATTR_OUTER)
12577 TRANSACTION_EXPR_OUTER (stmt) = 1;
12578 if (flags & TM_STMT_ATTR_RELAXED)
12579 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12580 return add_stmt (stmt);
12583 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12584 down to the element type of an array. */
12586 tree
12587 c_build_qualified_type (tree type, int type_quals)
12589 if (type == error_mark_node)
12590 return type;
12592 if (TREE_CODE (type) == ARRAY_TYPE)
12594 tree t;
12595 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12596 type_quals);
12598 /* See if we already have an identically qualified type. */
12599 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12601 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12602 && TYPE_NAME (t) == TYPE_NAME (type)
12603 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12604 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12605 TYPE_ATTRIBUTES (type)))
12606 break;
12608 if (!t)
12610 tree domain = TYPE_DOMAIN (type);
12612 t = build_variant_type_copy (type);
12613 TREE_TYPE (t) = element_type;
12615 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12616 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12617 SET_TYPE_STRUCTURAL_EQUALITY (t);
12618 else if (TYPE_CANONICAL (element_type) != element_type
12619 || (domain && TYPE_CANONICAL (domain) != domain))
12621 tree unqualified_canon
12622 = build_array_type (TYPE_CANONICAL (element_type),
12623 domain? TYPE_CANONICAL (domain)
12624 : NULL_TREE);
12625 TYPE_CANONICAL (t)
12626 = c_build_qualified_type (unqualified_canon, type_quals);
12628 else
12629 TYPE_CANONICAL (t) = t;
12631 return t;
12634 /* A restrict-qualified pointer type must be a pointer to object or
12635 incomplete type. Note that the use of POINTER_TYPE_P also allows
12636 REFERENCE_TYPEs, which is appropriate for C++. */
12637 if ((type_quals & TYPE_QUAL_RESTRICT)
12638 && (!POINTER_TYPE_P (type)
12639 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12641 error ("invalid use of %<restrict%>");
12642 type_quals &= ~TYPE_QUAL_RESTRICT;
12645 return build_qualified_type (type, type_quals);
12648 /* Build a VA_ARG_EXPR for the C parser. */
12650 tree
12651 c_build_va_arg (location_t loc, tree expr, tree type)
12653 if (error_operand_p (type))
12654 return error_mark_node;
12655 else if (!COMPLETE_TYPE_P (type))
12657 error_at (loc, "second argument to %<va_arg%> is of incomplete "
12658 "type %qT", type);
12659 return error_mark_node;
12661 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12662 warning_at (loc, OPT_Wc___compat,
12663 "C++ requires promoted type, not enum type, in %<va_arg%>");
12664 return build_va_arg (loc, expr, type);
12667 /* Return truthvalue of whether T1 is the same tree structure as T2.
12668 Return 1 if they are the same. Return 0 if they are different. */
12670 bool
12671 c_tree_equal (tree t1, tree t2)
12673 enum tree_code code1, code2;
12675 if (t1 == t2)
12676 return true;
12677 if (!t1 || !t2)
12678 return false;
12680 for (code1 = TREE_CODE (t1);
12681 CONVERT_EXPR_CODE_P (code1)
12682 || code1 == NON_LVALUE_EXPR;
12683 code1 = TREE_CODE (t1))
12684 t1 = TREE_OPERAND (t1, 0);
12685 for (code2 = TREE_CODE (t2);
12686 CONVERT_EXPR_CODE_P (code2)
12687 || code2 == NON_LVALUE_EXPR;
12688 code2 = TREE_CODE (t2))
12689 t2 = TREE_OPERAND (t2, 0);
12691 /* They might have become equal now. */
12692 if (t1 == t2)
12693 return true;
12695 if (code1 != code2)
12696 return false;
12698 switch (code1)
12700 case INTEGER_CST:
12701 return wi::eq_p (t1, t2);
12703 case REAL_CST:
12704 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12706 case STRING_CST:
12707 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12708 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12709 TREE_STRING_LENGTH (t1));
12711 case FIXED_CST:
12712 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12713 TREE_FIXED_CST (t2));
12715 case COMPLEX_CST:
12716 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12717 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12719 case VECTOR_CST:
12720 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12722 case CONSTRUCTOR:
12723 /* We need to do this when determining whether or not two
12724 non-type pointer to member function template arguments
12725 are the same. */
12726 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12727 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12728 return false;
12730 tree field, value;
12731 unsigned int i;
12732 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12734 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12735 if (!c_tree_equal (field, elt2->index)
12736 || !c_tree_equal (value, elt2->value))
12737 return false;
12740 return true;
12742 case TREE_LIST:
12743 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12744 return false;
12745 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12746 return false;
12747 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12749 case SAVE_EXPR:
12750 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12752 case CALL_EXPR:
12754 tree arg1, arg2;
12755 call_expr_arg_iterator iter1, iter2;
12756 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12757 return false;
12758 for (arg1 = first_call_expr_arg (t1, &iter1),
12759 arg2 = first_call_expr_arg (t2, &iter2);
12760 arg1 && arg2;
12761 arg1 = next_call_expr_arg (&iter1),
12762 arg2 = next_call_expr_arg (&iter2))
12763 if (!c_tree_equal (arg1, arg2))
12764 return false;
12765 if (arg1 || arg2)
12766 return false;
12767 return true;
12770 case TARGET_EXPR:
12772 tree o1 = TREE_OPERAND (t1, 0);
12773 tree o2 = TREE_OPERAND (t2, 0);
12775 /* Special case: if either target is an unallocated VAR_DECL,
12776 it means that it's going to be unified with whatever the
12777 TARGET_EXPR is really supposed to initialize, so treat it
12778 as being equivalent to anything. */
12779 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12780 && !DECL_RTL_SET_P (o1))
12781 /*Nop*/;
12782 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12783 && !DECL_RTL_SET_P (o2))
12784 /*Nop*/;
12785 else if (!c_tree_equal (o1, o2))
12786 return false;
12788 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12791 case COMPONENT_REF:
12792 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12793 return false;
12794 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12796 case PARM_DECL:
12797 case VAR_DECL:
12798 case CONST_DECL:
12799 case FIELD_DECL:
12800 case FUNCTION_DECL:
12801 case IDENTIFIER_NODE:
12802 case SSA_NAME:
12803 return false;
12805 case TREE_VEC:
12807 unsigned ix;
12808 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12809 return false;
12810 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12811 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12812 TREE_VEC_ELT (t2, ix)))
12813 return false;
12814 return true;
12817 default:
12818 break;
12821 switch (TREE_CODE_CLASS (code1))
12823 case tcc_unary:
12824 case tcc_binary:
12825 case tcc_comparison:
12826 case tcc_expression:
12827 case tcc_vl_exp:
12828 case tcc_reference:
12829 case tcc_statement:
12831 int i, n = TREE_OPERAND_LENGTH (t1);
12833 switch (code1)
12835 case PREINCREMENT_EXPR:
12836 case PREDECREMENT_EXPR:
12837 case POSTINCREMENT_EXPR:
12838 case POSTDECREMENT_EXPR:
12839 n = 1;
12840 break;
12841 case ARRAY_REF:
12842 n = 2;
12843 break;
12844 default:
12845 break;
12848 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12849 && n != TREE_OPERAND_LENGTH (t2))
12850 return false;
12852 for (i = 0; i < n; ++i)
12853 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12854 return false;
12856 return true;
12859 case tcc_type:
12860 return comptypes (t1, t2);
12861 default:
12862 gcc_unreachable ();
12864 /* We can get here with --disable-checking. */
12865 return false;
12868 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12869 spawn-helper and BODY is the newly created body for FNDECL. */
12871 void
12872 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12874 tree list = alloc_stmt_list ();
12875 tree frame = make_cilk_frame (fndecl);
12876 tree dtor = create_cilk_function_exit (frame, false, true);
12877 add_local_decl (cfun, frame);
12879 DECL_SAVED_TREE (fndecl) = list;
12880 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12881 frame);
12882 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12883 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12885 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12886 append_to_statement_list (detach_expr, &body_list);
12888 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12889 body = fold_build_cleanup_point_expr (void_type_node, body);
12891 append_to_statement_list (body, &body_list);
12892 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12893 body_list, dtor), &list);