PR c/66066
[official-gcc.git] / gcc / c / c-typeck.c
blob9b883a22f2ba3e97eb080c482b9c0e9eb6023ba9
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 /* For &x[y], return x+y */
4182 if (TREE_CODE (arg) == ARRAY_REF)
4184 tree op0 = TREE_OPERAND (arg, 0);
4185 if (!c_mark_addressable (op0))
4186 return error_mark_node;
4189 /* Anything not already handled and not a true memory reference
4190 or a non-lvalue array is an error. */
4191 else if (typecode != FUNCTION_TYPE && !flag
4192 && !lvalue_or_else (location, arg, lv_addressof))
4193 return error_mark_node;
4195 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4196 folding later. */
4197 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4199 tree inner = build_unary_op (location, code,
4200 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4201 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4202 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4203 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4204 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4205 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4206 goto return_build_unary_op;
4209 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4210 argtype = TREE_TYPE (arg);
4212 /* If the lvalue is const or volatile, merge that into the type
4213 to which the address will point. This is only needed
4214 for function types. */
4215 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4216 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4217 && TREE_CODE (argtype) == FUNCTION_TYPE)
4219 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4220 int quals = orig_quals;
4222 if (TREE_READONLY (arg))
4223 quals |= TYPE_QUAL_CONST;
4224 if (TREE_THIS_VOLATILE (arg))
4225 quals |= TYPE_QUAL_VOLATILE;
4227 argtype = c_build_qualified_type (argtype, quals);
4230 if (!c_mark_addressable (arg))
4231 return error_mark_node;
4233 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4234 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4236 argtype = build_pointer_type (argtype);
4238 /* ??? Cope with user tricks that amount to offsetof. Delete this
4239 when we have proper support for integer constant expressions. */
4240 val = get_base_address (arg);
4241 if (val && TREE_CODE (val) == INDIRECT_REF
4242 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4244 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4245 goto return_build_unary_op;
4248 val = build1 (ADDR_EXPR, argtype, arg);
4250 ret = val;
4251 goto return_build_unary_op;
4253 default:
4254 gcc_unreachable ();
4257 if (argtype == 0)
4258 argtype = TREE_TYPE (arg);
4259 if (TREE_CODE (arg) == INTEGER_CST)
4260 ret = (require_constant_value
4261 ? fold_build1_initializer_loc (location, code, argtype, arg)
4262 : fold_build1_loc (location, code, argtype, arg));
4263 else
4264 ret = build1 (code, argtype, arg);
4265 return_build_unary_op:
4266 gcc_assert (ret != error_mark_node);
4267 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4268 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4269 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4270 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4271 ret = note_integer_operands (ret);
4272 if (eptype)
4273 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4274 protected_set_expr_location (ret, location);
4275 return ret;
4278 /* Return nonzero if REF is an lvalue valid for this language.
4279 Lvalues can be assigned, unless their type has TYPE_READONLY.
4280 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4282 bool
4283 lvalue_p (const_tree ref)
4285 const enum tree_code code = TREE_CODE (ref);
4287 switch (code)
4289 case REALPART_EXPR:
4290 case IMAGPART_EXPR:
4291 case COMPONENT_REF:
4292 return lvalue_p (TREE_OPERAND (ref, 0));
4294 case C_MAYBE_CONST_EXPR:
4295 return lvalue_p (TREE_OPERAND (ref, 1));
4297 case COMPOUND_LITERAL_EXPR:
4298 case STRING_CST:
4299 return 1;
4301 case INDIRECT_REF:
4302 case ARRAY_REF:
4303 case ARRAY_NOTATION_REF:
4304 case VAR_DECL:
4305 case PARM_DECL:
4306 case RESULT_DECL:
4307 case ERROR_MARK:
4308 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4309 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4311 case BIND_EXPR:
4312 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4314 default:
4315 return 0;
4319 /* Give a warning for storing in something that is read-only in GCC
4320 terms but not const in ISO C terms. */
4322 static void
4323 readonly_warning (tree arg, enum lvalue_use use)
4325 switch (use)
4327 case lv_assign:
4328 warning (0, "assignment of read-only location %qE", arg);
4329 break;
4330 case lv_increment:
4331 warning (0, "increment of read-only location %qE", arg);
4332 break;
4333 case lv_decrement:
4334 warning (0, "decrement of read-only location %qE", arg);
4335 break;
4336 default:
4337 gcc_unreachable ();
4339 return;
4343 /* Return nonzero if REF is an lvalue valid for this language;
4344 otherwise, print an error message and return zero. USE says
4345 how the lvalue is being used and so selects the error message.
4346 LOCATION is the location at which any error should be reported. */
4348 static int
4349 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4351 int win = lvalue_p (ref);
4353 if (!win)
4354 lvalue_error (loc, use);
4356 return win;
4359 /* Mark EXP saying that we need to be able to take the
4360 address of it; it should not be allocated in a register.
4361 Returns true if successful. */
4363 bool
4364 c_mark_addressable (tree exp)
4366 tree x = exp;
4368 while (1)
4369 switch (TREE_CODE (x))
4371 case COMPONENT_REF:
4372 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4374 error
4375 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4376 return false;
4379 /* ... fall through ... */
4381 case ADDR_EXPR:
4382 case ARRAY_REF:
4383 case REALPART_EXPR:
4384 case IMAGPART_EXPR:
4385 x = TREE_OPERAND (x, 0);
4386 break;
4388 case COMPOUND_LITERAL_EXPR:
4389 case CONSTRUCTOR:
4390 TREE_ADDRESSABLE (x) = 1;
4391 return true;
4393 case VAR_DECL:
4394 case CONST_DECL:
4395 case PARM_DECL:
4396 case RESULT_DECL:
4397 if (C_DECL_REGISTER (x)
4398 && DECL_NONLOCAL (x))
4400 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4402 error
4403 ("global register variable %qD used in nested function", x);
4404 return false;
4406 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4408 else if (C_DECL_REGISTER (x))
4410 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4411 error ("address of global register variable %qD requested", x);
4412 else
4413 error ("address of register variable %qD requested", x);
4414 return false;
4417 /* drops in */
4418 case FUNCTION_DECL:
4419 TREE_ADDRESSABLE (x) = 1;
4420 /* drops out */
4421 default:
4422 return true;
4426 /* Convert EXPR to TYPE, warning about conversion problems with
4427 constants. SEMANTIC_TYPE is the type this conversion would use
4428 without excess precision. If SEMANTIC_TYPE is NULL, this function
4429 is equivalent to convert_and_check. This function is a wrapper that
4430 handles conversions that may be different than
4431 the usual ones because of excess precision. */
4433 static tree
4434 ep_convert_and_check (location_t loc, tree type, tree expr,
4435 tree semantic_type)
4437 if (TREE_TYPE (expr) == type)
4438 return expr;
4440 if (!semantic_type)
4441 return convert_and_check (loc, type, expr);
4443 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4444 && TREE_TYPE (expr) != semantic_type)
4446 /* For integers, we need to check the real conversion, not
4447 the conversion to the excess precision type. */
4448 expr = convert_and_check (loc, semantic_type, expr);
4450 /* Result type is the excess precision type, which should be
4451 large enough, so do not check. */
4452 return convert (type, expr);
4455 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4456 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4457 if folded to an integer constant then the unselected half may
4458 contain arbitrary operations not normally permitted in constant
4459 expressions. Set the location of the expression to LOC. */
4461 tree
4462 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4463 tree op1, tree op1_original_type, tree op2,
4464 tree op2_original_type)
4466 tree type1;
4467 tree type2;
4468 enum tree_code code1;
4469 enum tree_code code2;
4470 tree result_type = NULL;
4471 tree semantic_result_type = NULL;
4472 tree orig_op1 = op1, orig_op2 = op2;
4473 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4474 bool ifexp_int_operands;
4475 tree ret;
4477 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4478 if (op1_int_operands)
4479 op1 = remove_c_maybe_const_expr (op1);
4480 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4481 if (op2_int_operands)
4482 op2 = remove_c_maybe_const_expr (op2);
4483 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4484 if (ifexp_int_operands)
4485 ifexp = remove_c_maybe_const_expr (ifexp);
4487 /* Promote both alternatives. */
4489 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4490 op1 = default_conversion (op1);
4491 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4492 op2 = default_conversion (op2);
4494 if (TREE_CODE (ifexp) == ERROR_MARK
4495 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4496 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4497 return error_mark_node;
4499 type1 = TREE_TYPE (op1);
4500 code1 = TREE_CODE (type1);
4501 type2 = TREE_TYPE (op2);
4502 code2 = TREE_CODE (type2);
4504 /* C90 does not permit non-lvalue arrays in conditional expressions.
4505 In C99 they will be pointers by now. */
4506 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4508 error_at (colon_loc, "non-lvalue array in conditional expression");
4509 return error_mark_node;
4512 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4513 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4514 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4515 || code1 == COMPLEX_TYPE)
4516 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4517 || code2 == COMPLEX_TYPE))
4519 semantic_result_type = c_common_type (type1, type2);
4520 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4522 op1 = TREE_OPERAND (op1, 0);
4523 type1 = TREE_TYPE (op1);
4524 gcc_assert (TREE_CODE (type1) == code1);
4526 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4528 op2 = TREE_OPERAND (op2, 0);
4529 type2 = TREE_TYPE (op2);
4530 gcc_assert (TREE_CODE (type2) == code2);
4534 if (warn_cxx_compat)
4536 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4537 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4539 if (TREE_CODE (t1) == ENUMERAL_TYPE
4540 && TREE_CODE (t2) == ENUMERAL_TYPE
4541 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4542 warning_at (colon_loc, OPT_Wc___compat,
4543 ("different enum types in conditional is "
4544 "invalid in C++: %qT vs %qT"),
4545 t1, t2);
4548 /* Quickly detect the usual case where op1 and op2 have the same type
4549 after promotion. */
4550 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4552 if (type1 == type2)
4553 result_type = type1;
4554 else
4555 result_type = TYPE_MAIN_VARIANT (type1);
4557 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4558 || code1 == COMPLEX_TYPE)
4559 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4560 || code2 == COMPLEX_TYPE))
4562 result_type = c_common_type (type1, type2);
4563 do_warn_double_promotion (result_type, type1, type2,
4564 "implicit conversion from %qT to %qT to "
4565 "match other result of conditional",
4566 colon_loc);
4568 /* If -Wsign-compare, warn here if type1 and type2 have
4569 different signedness. We'll promote the signed to unsigned
4570 and later code won't know it used to be different.
4571 Do this check on the original types, so that explicit casts
4572 will be considered, but default promotions won't. */
4573 if (c_inhibit_evaluation_warnings == 0)
4575 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4576 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4578 if (unsigned_op1 ^ unsigned_op2)
4580 bool ovf;
4582 /* Do not warn if the result type is signed, since the
4583 signed type will only be chosen if it can represent
4584 all the values of the unsigned type. */
4585 if (!TYPE_UNSIGNED (result_type))
4586 /* OK */;
4587 else
4589 bool op1_maybe_const = true;
4590 bool op2_maybe_const = true;
4592 /* Do not warn if the signed quantity is an
4593 unsuffixed integer literal (or some static
4594 constant expression involving such literals) and
4595 it is non-negative. This warning requires the
4596 operands to be folded for best results, so do
4597 that folding in this case even without
4598 warn_sign_compare to avoid warning options
4599 possibly affecting code generation. */
4600 c_inhibit_evaluation_warnings
4601 += (ifexp == truthvalue_false_node);
4602 op1 = c_fully_fold (op1, require_constant_value,
4603 &op1_maybe_const);
4604 c_inhibit_evaluation_warnings
4605 -= (ifexp == truthvalue_false_node);
4607 c_inhibit_evaluation_warnings
4608 += (ifexp == truthvalue_true_node);
4609 op2 = c_fully_fold (op2, require_constant_value,
4610 &op2_maybe_const);
4611 c_inhibit_evaluation_warnings
4612 -= (ifexp == truthvalue_true_node);
4614 if (warn_sign_compare)
4616 if ((unsigned_op2
4617 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4618 || (unsigned_op1
4619 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4620 /* OK */;
4621 else
4622 warning_at (colon_loc, OPT_Wsign_compare,
4623 ("signed and unsigned type in "
4624 "conditional expression"));
4626 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4627 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4628 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4629 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4634 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4636 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4637 pedwarn (colon_loc, OPT_Wpedantic,
4638 "ISO C forbids conditional expr with only one void side");
4639 result_type = void_type_node;
4641 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4643 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4644 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4645 addr_space_t as_common;
4647 if (comp_target_types (colon_loc, type1, type2))
4648 result_type = common_pointer_type (type1, type2);
4649 else if (null_pointer_constant_p (orig_op1))
4650 result_type = type2;
4651 else if (null_pointer_constant_p (orig_op2))
4652 result_type = type1;
4653 else if (!addr_space_superset (as1, as2, &as_common))
4655 error_at (colon_loc, "pointers to disjoint address spaces "
4656 "used in conditional expression");
4657 return error_mark_node;
4659 else if (VOID_TYPE_P (TREE_TYPE (type1))
4660 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4662 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4663 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4664 & ~TYPE_QUALS (TREE_TYPE (type1))))
4665 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4666 "pointer to array loses qualifier "
4667 "in conditional expression");
4669 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4670 pedwarn (colon_loc, OPT_Wpedantic,
4671 "ISO C forbids conditional expr between "
4672 "%<void *%> and function pointer");
4673 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4674 TREE_TYPE (type2)));
4676 else if (VOID_TYPE_P (TREE_TYPE (type2))
4677 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4679 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4680 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4681 & ~TYPE_QUALS (TREE_TYPE (type2))))
4682 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4683 "pointer to array loses qualifier "
4684 "in conditional expression");
4686 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4687 pedwarn (colon_loc, OPT_Wpedantic,
4688 "ISO C forbids conditional expr between "
4689 "%<void *%> and function pointer");
4690 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4691 TREE_TYPE (type1)));
4693 /* Objective-C pointer comparisons are a bit more lenient. */
4694 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4695 result_type = objc_common_type (type1, type2);
4696 else
4698 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4700 pedwarn (colon_loc, 0,
4701 "pointer type mismatch in conditional expression");
4702 result_type = build_pointer_type
4703 (build_qualified_type (void_type_node, qual));
4706 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4708 if (!null_pointer_constant_p (orig_op2))
4709 pedwarn (colon_loc, 0,
4710 "pointer/integer type mismatch in conditional expression");
4711 else
4713 op2 = null_pointer_node;
4715 result_type = type1;
4717 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4719 if (!null_pointer_constant_p (orig_op1))
4720 pedwarn (colon_loc, 0,
4721 "pointer/integer type mismatch in conditional expression");
4722 else
4724 op1 = null_pointer_node;
4726 result_type = type2;
4729 if (!result_type)
4731 if (flag_cond_mismatch)
4732 result_type = void_type_node;
4733 else
4735 error_at (colon_loc, "type mismatch in conditional expression");
4736 return error_mark_node;
4740 /* Merge const and volatile flags of the incoming types. */
4741 result_type
4742 = build_type_variant (result_type,
4743 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4744 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4746 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4747 semantic_result_type);
4748 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4749 semantic_result_type);
4751 if (ifexp_bcp && ifexp == truthvalue_true_node)
4753 op2_int_operands = true;
4754 op1 = c_fully_fold (op1, require_constant_value, NULL);
4756 if (ifexp_bcp && ifexp == truthvalue_false_node)
4758 op1_int_operands = true;
4759 op2 = c_fully_fold (op2, require_constant_value, NULL);
4761 int_const = int_operands = (ifexp_int_operands
4762 && op1_int_operands
4763 && op2_int_operands);
4764 if (int_operands)
4766 int_const = ((ifexp == truthvalue_true_node
4767 && TREE_CODE (orig_op1) == INTEGER_CST
4768 && !TREE_OVERFLOW (orig_op1))
4769 || (ifexp == truthvalue_false_node
4770 && TREE_CODE (orig_op2) == INTEGER_CST
4771 && !TREE_OVERFLOW (orig_op2)));
4773 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4774 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4775 else
4777 if (int_operands)
4779 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4780 nested inside of the expression. */
4781 op1 = c_fully_fold (op1, false, NULL);
4782 op2 = c_fully_fold (op2, false, NULL);
4784 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4785 if (int_operands)
4786 ret = note_integer_operands (ret);
4788 if (semantic_result_type)
4789 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4791 protected_set_expr_location (ret, colon_loc);
4792 return ret;
4795 /* Return a compound expression that performs two expressions and
4796 returns the value of the second of them.
4798 LOC is the location of the COMPOUND_EXPR. */
4800 tree
4801 build_compound_expr (location_t loc, tree expr1, tree expr2)
4803 bool expr1_int_operands, expr2_int_operands;
4804 tree eptype = NULL_TREE;
4805 tree ret;
4807 if (flag_cilkplus
4808 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4809 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4811 error_at (loc,
4812 "spawned function call cannot be part of a comma expression");
4813 return error_mark_node;
4815 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4816 if (expr1_int_operands)
4817 expr1 = remove_c_maybe_const_expr (expr1);
4818 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4819 if (expr2_int_operands)
4820 expr2 = remove_c_maybe_const_expr (expr2);
4822 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4823 expr1 = TREE_OPERAND (expr1, 0);
4824 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4826 eptype = TREE_TYPE (expr2);
4827 expr2 = TREE_OPERAND (expr2, 0);
4830 if (!TREE_SIDE_EFFECTS (expr1))
4832 /* The left-hand operand of a comma expression is like an expression
4833 statement: with -Wunused, we should warn if it doesn't have
4834 any side-effects, unless it was explicitly cast to (void). */
4835 if (warn_unused_value)
4837 if (VOID_TYPE_P (TREE_TYPE (expr1))
4838 && CONVERT_EXPR_P (expr1))
4839 ; /* (void) a, b */
4840 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4841 && TREE_CODE (expr1) == COMPOUND_EXPR
4842 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4843 ; /* (void) a, (void) b, c */
4844 else
4845 warning_at (loc, OPT_Wunused_value,
4846 "left-hand operand of comma expression has no effect");
4849 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4850 && warn_unused_value)
4852 tree r = expr1;
4853 location_t cloc = loc;
4854 while (TREE_CODE (r) == COMPOUND_EXPR)
4856 if (EXPR_HAS_LOCATION (r))
4857 cloc = EXPR_LOCATION (r);
4858 r = TREE_OPERAND (r, 1);
4860 if (!TREE_SIDE_EFFECTS (r)
4861 && !VOID_TYPE_P (TREE_TYPE (r))
4862 && !CONVERT_EXPR_P (r))
4863 warning_at (cloc, OPT_Wunused_value,
4864 "right-hand operand of comma expression has no effect");
4867 /* With -Wunused, we should also warn if the left-hand operand does have
4868 side-effects, but computes a value which is not used. For example, in
4869 `foo() + bar(), baz()' the result of the `+' operator is not used,
4870 so we should issue a warning. */
4871 else if (warn_unused_value)
4872 warn_if_unused_value (expr1, loc);
4874 if (expr2 == error_mark_node)
4875 return error_mark_node;
4877 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4879 if (flag_isoc99
4880 && expr1_int_operands
4881 && expr2_int_operands)
4882 ret = note_integer_operands (ret);
4884 if (eptype)
4885 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4887 protected_set_expr_location (ret, loc);
4888 return ret;
4891 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4892 which we are casting. OTYPE is the type of the expression being
4893 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4894 of the cast. -Wcast-qual appeared on the command line. Named
4895 address space qualifiers are not handled here, because they result
4896 in different warnings. */
4898 static void
4899 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4901 tree in_type = type;
4902 tree in_otype = otype;
4903 int added = 0;
4904 int discarded = 0;
4905 bool is_const;
4907 /* Check that the qualifiers on IN_TYPE are a superset of the
4908 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4909 nodes is uninteresting and we stop as soon as we hit a
4910 non-POINTER_TYPE node on either type. */
4913 in_otype = TREE_TYPE (in_otype);
4914 in_type = TREE_TYPE (in_type);
4916 /* GNU C allows cv-qualified function types. 'const' means the
4917 function is very pure, 'volatile' means it can't return. We
4918 need to warn when such qualifiers are added, not when they're
4919 taken away. */
4920 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4921 && TREE_CODE (in_type) == FUNCTION_TYPE)
4922 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4923 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4924 else
4925 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4926 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4928 while (TREE_CODE (in_type) == POINTER_TYPE
4929 && TREE_CODE (in_otype) == POINTER_TYPE);
4931 if (added)
4932 warning_at (loc, OPT_Wcast_qual,
4933 "cast adds %q#v qualifier to function type", added);
4935 if (discarded)
4936 /* There are qualifiers present in IN_OTYPE that are not present
4937 in IN_TYPE. */
4938 warning_at (loc, OPT_Wcast_qual,
4939 "cast discards %qv qualifier from pointer target type",
4940 discarded);
4942 if (added || discarded)
4943 return;
4945 /* A cast from **T to const **T is unsafe, because it can cause a
4946 const value to be changed with no additional warning. We only
4947 issue this warning if T is the same on both sides, and we only
4948 issue the warning if there are the same number of pointers on
4949 both sides, as otherwise the cast is clearly unsafe anyhow. A
4950 cast is unsafe when a qualifier is added at one level and const
4951 is not present at all outer levels.
4953 To issue this warning, we check at each level whether the cast
4954 adds new qualifiers not already seen. We don't need to special
4955 case function types, as they won't have the same
4956 TYPE_MAIN_VARIANT. */
4958 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4959 return;
4960 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4961 return;
4963 in_type = type;
4964 in_otype = otype;
4965 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4968 in_type = TREE_TYPE (in_type);
4969 in_otype = TREE_TYPE (in_otype);
4970 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4971 && !is_const)
4973 warning_at (loc, OPT_Wcast_qual,
4974 "to be safe all intermediate pointers in cast from "
4975 "%qT to %qT must be %<const%> qualified",
4976 otype, type);
4977 break;
4979 if (is_const)
4980 is_const = TYPE_READONLY (in_type);
4982 while (TREE_CODE (in_type) == POINTER_TYPE);
4985 /* Build an expression representing a cast to type TYPE of expression EXPR.
4986 LOC is the location of the cast-- typically the open paren of the cast. */
4988 tree
4989 build_c_cast (location_t loc, tree type, tree expr)
4991 tree value;
4993 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4994 expr = TREE_OPERAND (expr, 0);
4996 value = expr;
4998 if (type == error_mark_node || expr == error_mark_node)
4999 return error_mark_node;
5001 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5002 only in <protocol> qualifications. But when constructing cast expressions,
5003 the protocols do matter and must be kept around. */
5004 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5005 return build1 (NOP_EXPR, type, expr);
5007 type = TYPE_MAIN_VARIANT (type);
5009 if (TREE_CODE (type) == ARRAY_TYPE)
5011 error_at (loc, "cast specifies array type");
5012 return error_mark_node;
5015 if (TREE_CODE (type) == FUNCTION_TYPE)
5017 error_at (loc, "cast specifies function type");
5018 return error_mark_node;
5021 if (!VOID_TYPE_P (type))
5023 value = require_complete_type (value);
5024 if (value == error_mark_node)
5025 return error_mark_node;
5028 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5030 if (TREE_CODE (type) == RECORD_TYPE
5031 || TREE_CODE (type) == UNION_TYPE)
5032 pedwarn (loc, OPT_Wpedantic,
5033 "ISO C forbids casting nonscalar to the same type");
5035 /* Convert to remove any qualifiers from VALUE's type. */
5036 value = convert (type, value);
5038 else if (TREE_CODE (type) == UNION_TYPE)
5040 tree field;
5042 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5043 if (TREE_TYPE (field) != error_mark_node
5044 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5045 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5046 break;
5048 if (field)
5050 tree t;
5051 bool maybe_const = true;
5053 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5054 t = c_fully_fold (value, false, &maybe_const);
5055 t = build_constructor_single (type, field, t);
5056 if (!maybe_const)
5057 t = c_wrap_maybe_const (t, true);
5058 t = digest_init (loc, type, t,
5059 NULL_TREE, false, true, 0);
5060 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5061 return t;
5063 error_at (loc, "cast to union type from type not present in union");
5064 return error_mark_node;
5066 else
5068 tree otype, ovalue;
5070 if (type == void_type_node)
5072 tree t = build1 (CONVERT_EXPR, type, value);
5073 SET_EXPR_LOCATION (t, loc);
5074 return t;
5077 otype = TREE_TYPE (value);
5079 /* Optionally warn about potentially worrisome casts. */
5080 if (warn_cast_qual
5081 && TREE_CODE (type) == POINTER_TYPE
5082 && TREE_CODE (otype) == POINTER_TYPE)
5083 handle_warn_cast_qual (loc, type, otype);
5085 /* Warn about conversions between pointers to disjoint
5086 address spaces. */
5087 if (TREE_CODE (type) == POINTER_TYPE
5088 && TREE_CODE (otype) == POINTER_TYPE
5089 && !null_pointer_constant_p (value))
5091 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5092 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5093 addr_space_t as_common;
5095 if (!addr_space_superset (as_to, as_from, &as_common))
5097 if (ADDR_SPACE_GENERIC_P (as_from))
5098 warning_at (loc, 0, "cast to %s address space pointer "
5099 "from disjoint generic address space pointer",
5100 c_addr_space_name (as_to));
5102 else if (ADDR_SPACE_GENERIC_P (as_to))
5103 warning_at (loc, 0, "cast to generic address space pointer "
5104 "from disjoint %s address space pointer",
5105 c_addr_space_name (as_from));
5107 else
5108 warning_at (loc, 0, "cast to %s address space pointer "
5109 "from disjoint %s address space pointer",
5110 c_addr_space_name (as_to),
5111 c_addr_space_name (as_from));
5115 /* Warn about possible alignment problems. */
5116 if (STRICT_ALIGNMENT
5117 && TREE_CODE (type) == POINTER_TYPE
5118 && TREE_CODE (otype) == POINTER_TYPE
5119 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5120 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5121 /* Don't warn about opaque types, where the actual alignment
5122 restriction is unknown. */
5123 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5124 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5125 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5126 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5127 warning_at (loc, OPT_Wcast_align,
5128 "cast increases required alignment of target type");
5130 if (TREE_CODE (type) == INTEGER_TYPE
5131 && TREE_CODE (otype) == POINTER_TYPE
5132 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5133 /* Unlike conversion of integers to pointers, where the
5134 warning is disabled for converting constants because
5135 of cases such as SIG_*, warn about converting constant
5136 pointers to integers. In some cases it may cause unwanted
5137 sign extension, and a warning is appropriate. */
5138 warning_at (loc, OPT_Wpointer_to_int_cast,
5139 "cast from pointer to integer of different size");
5141 if (TREE_CODE (value) == CALL_EXPR
5142 && TREE_CODE (type) != TREE_CODE (otype))
5143 warning_at (loc, OPT_Wbad_function_cast,
5144 "cast from function call of type %qT "
5145 "to non-matching type %qT", otype, type);
5147 if (TREE_CODE (type) == POINTER_TYPE
5148 && TREE_CODE (otype) == INTEGER_TYPE
5149 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5150 /* Don't warn about converting any constant. */
5151 && !TREE_CONSTANT (value))
5152 warning_at (loc,
5153 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5154 "of different size");
5156 if (warn_strict_aliasing <= 2)
5157 strict_aliasing_warning (otype, type, expr);
5159 /* If pedantic, warn for conversions between function and object
5160 pointer types, except for converting a null pointer constant
5161 to function pointer type. */
5162 if (pedantic
5163 && TREE_CODE (type) == POINTER_TYPE
5164 && TREE_CODE (otype) == POINTER_TYPE
5165 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5166 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5167 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5168 "conversion of function pointer to object pointer type");
5170 if (pedantic
5171 && TREE_CODE (type) == POINTER_TYPE
5172 && TREE_CODE (otype) == POINTER_TYPE
5173 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5174 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5175 && !null_pointer_constant_p (value))
5176 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5177 "conversion of object pointer to function pointer type");
5179 ovalue = value;
5180 value = convert (type, value);
5182 /* Ignore any integer overflow caused by the cast. */
5183 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5185 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5187 if (!TREE_OVERFLOW (value))
5189 /* Avoid clobbering a shared constant. */
5190 value = copy_node (value);
5191 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5194 else if (TREE_OVERFLOW (value))
5195 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5196 value = wide_int_to_tree (TREE_TYPE (value), value);
5200 /* Don't let a cast be an lvalue. */
5201 if (value == expr)
5202 value = non_lvalue_loc (loc, value);
5204 /* Don't allow the results of casting to floating-point or complex
5205 types be confused with actual constants, or casts involving
5206 integer and pointer types other than direct integer-to-integer
5207 and integer-to-pointer be confused with integer constant
5208 expressions and null pointer constants. */
5209 if (TREE_CODE (value) == REAL_CST
5210 || TREE_CODE (value) == COMPLEX_CST
5211 || (TREE_CODE (value) == INTEGER_CST
5212 && !((TREE_CODE (expr) == INTEGER_CST
5213 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5214 || TREE_CODE (expr) == REAL_CST
5215 || TREE_CODE (expr) == COMPLEX_CST)))
5216 value = build1 (NOP_EXPR, type, value);
5218 if (CAN_HAVE_LOCATION_P (value))
5219 SET_EXPR_LOCATION (value, loc);
5220 return value;
5223 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5224 location of the open paren of the cast, or the position of the cast
5225 expr. */
5226 tree
5227 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5229 tree type;
5230 tree type_expr = NULL_TREE;
5231 bool type_expr_const = true;
5232 tree ret;
5233 int saved_wsp = warn_strict_prototypes;
5235 /* This avoids warnings about unprototyped casts on
5236 integers. E.g. "#define SIG_DFL (void(*)())0". */
5237 if (TREE_CODE (expr) == INTEGER_CST)
5238 warn_strict_prototypes = 0;
5239 type = groktypename (type_name, &type_expr, &type_expr_const);
5240 warn_strict_prototypes = saved_wsp;
5242 ret = build_c_cast (loc, type, expr);
5243 if (type_expr)
5245 bool inner_expr_const = true;
5246 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5247 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5248 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5249 && inner_expr_const);
5250 SET_EXPR_LOCATION (ret, loc);
5253 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5254 SET_EXPR_LOCATION (ret, loc);
5256 /* C++ does not permits types to be defined in a cast, but it
5257 allows references to incomplete types. */
5258 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5259 warning_at (loc, OPT_Wc___compat,
5260 "defining a type in a cast is invalid in C++");
5262 return ret;
5265 /* Build an assignment expression of lvalue LHS from value RHS.
5266 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5267 may differ from TREE_TYPE (LHS) for an enum bitfield.
5268 MODIFYCODE is the code for a binary operator that we use
5269 to combine the old value of LHS with RHS to get the new value.
5270 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5271 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5272 which may differ from TREE_TYPE (RHS) for an enum value.
5274 LOCATION is the location of the MODIFYCODE operator.
5275 RHS_LOC is the location of the RHS. */
5277 tree
5278 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5279 enum tree_code modifycode,
5280 location_t rhs_loc, tree rhs, tree rhs_origtype)
5282 tree result;
5283 tree newrhs;
5284 tree rhseval = NULL_TREE;
5285 tree rhs_semantic_type = NULL_TREE;
5286 tree lhstype = TREE_TYPE (lhs);
5287 tree olhstype = lhstype;
5288 bool npc;
5289 bool is_atomic_op;
5291 /* Types that aren't fully specified cannot be used in assignments. */
5292 lhs = require_complete_type (lhs);
5294 /* Avoid duplicate error messages from operands that had errors. */
5295 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5296 return error_mark_node;
5298 /* Ensure an error for assigning a non-lvalue array to an array in
5299 C90. */
5300 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5302 error_at (location, "assignment to expression with array type");
5303 return error_mark_node;
5306 /* For ObjC properties, defer this check. */
5307 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5308 return error_mark_node;
5310 is_atomic_op = really_atomic_lvalue (lhs);
5312 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5314 rhs_semantic_type = TREE_TYPE (rhs);
5315 rhs = TREE_OPERAND (rhs, 0);
5318 newrhs = rhs;
5320 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5322 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5323 lhs_origtype, modifycode, rhs_loc, rhs,
5324 rhs_origtype);
5325 if (inner == error_mark_node)
5326 return error_mark_node;
5327 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5328 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5329 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5330 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5331 protected_set_expr_location (result, location);
5332 return result;
5335 /* If a binary op has been requested, combine the old LHS value with the RHS
5336 producing the value we should actually store into the LHS. */
5338 if (modifycode != NOP_EXPR)
5340 lhs = c_fully_fold (lhs, false, NULL);
5341 lhs = stabilize_reference (lhs);
5343 /* Construct the RHS for any non-atomic compound assignemnt. */
5344 if (!is_atomic_op)
5346 /* If in LHS op= RHS the RHS has side-effects, ensure they
5347 are preevaluated before the rest of the assignment expression's
5348 side-effects, because RHS could contain e.g. function calls
5349 that modify LHS. */
5350 if (TREE_SIDE_EFFECTS (rhs))
5352 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5353 rhseval = newrhs;
5355 newrhs = build_binary_op (location,
5356 modifycode, lhs, newrhs, 1);
5358 /* The original type of the right hand side is no longer
5359 meaningful. */
5360 rhs_origtype = NULL_TREE;
5364 if (c_dialect_objc ())
5366 /* Check if we are modifying an Objective-C property reference;
5367 if so, we need to generate setter calls. */
5368 result = objc_maybe_build_modify_expr (lhs, newrhs);
5369 if (result)
5370 goto return_result;
5372 /* Else, do the check that we postponed for Objective-C. */
5373 if (!lvalue_or_else (location, lhs, lv_assign))
5374 return error_mark_node;
5377 /* Give an error for storing in something that is 'const'. */
5379 if (TYPE_READONLY (lhstype)
5380 || ((TREE_CODE (lhstype) == RECORD_TYPE
5381 || TREE_CODE (lhstype) == UNION_TYPE)
5382 && C_TYPE_FIELDS_READONLY (lhstype)))
5384 readonly_error (location, lhs, lv_assign);
5385 return error_mark_node;
5387 else if (TREE_READONLY (lhs))
5388 readonly_warning (lhs, lv_assign);
5390 /* If storing into a structure or union member,
5391 it has probably been given type `int'.
5392 Compute the type that would go with
5393 the actual amount of storage the member occupies. */
5395 if (TREE_CODE (lhs) == COMPONENT_REF
5396 && (TREE_CODE (lhstype) == INTEGER_TYPE
5397 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5398 || TREE_CODE (lhstype) == REAL_TYPE
5399 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5400 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5402 /* If storing in a field that is in actuality a short or narrower than one,
5403 we must store in the field in its actual type. */
5405 if (lhstype != TREE_TYPE (lhs))
5407 lhs = copy_node (lhs);
5408 TREE_TYPE (lhs) = lhstype;
5411 /* Issue -Wc++-compat warnings about an assignment to an enum type
5412 when LHS does not have its original type. This happens for,
5413 e.g., an enum bitfield in a struct. */
5414 if (warn_cxx_compat
5415 && lhs_origtype != NULL_TREE
5416 && lhs_origtype != lhstype
5417 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5419 tree checktype = (rhs_origtype != NULL_TREE
5420 ? rhs_origtype
5421 : TREE_TYPE (rhs));
5422 if (checktype != error_mark_node
5423 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5424 || (is_atomic_op && modifycode != NOP_EXPR)))
5425 warning_at (location, OPT_Wc___compat,
5426 "enum conversion in assignment is invalid in C++");
5429 /* If the lhs is atomic, remove that qualifier. */
5430 if (is_atomic_op)
5432 lhstype = build_qualified_type (lhstype,
5433 (TYPE_QUALS (lhstype)
5434 & ~TYPE_QUAL_ATOMIC));
5435 olhstype = build_qualified_type (olhstype,
5436 (TYPE_QUALS (lhstype)
5437 & ~TYPE_QUAL_ATOMIC));
5440 /* Convert new value to destination type. Fold it first, then
5441 restore any excess precision information, for the sake of
5442 conversion warnings. */
5444 if (!(is_atomic_op && modifycode != NOP_EXPR))
5446 npc = null_pointer_constant_p (newrhs);
5447 newrhs = c_fully_fold (newrhs, false, NULL);
5448 if (rhs_semantic_type)
5449 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5450 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5451 rhs_origtype, ic_assign, npc,
5452 NULL_TREE, NULL_TREE, 0);
5453 if (TREE_CODE (newrhs) == ERROR_MARK)
5454 return error_mark_node;
5457 /* Emit ObjC write barrier, if necessary. */
5458 if (c_dialect_objc () && flag_objc_gc)
5460 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5461 if (result)
5463 protected_set_expr_location (result, location);
5464 goto return_result;
5468 /* Scan operands. */
5470 if (is_atomic_op)
5471 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5472 else
5474 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5475 TREE_SIDE_EFFECTS (result) = 1;
5476 protected_set_expr_location (result, location);
5479 /* If we got the LHS in a different type for storing in,
5480 convert the result back to the nominal type of LHS
5481 so that the value we return always has the same type
5482 as the LHS argument. */
5484 if (olhstype == TREE_TYPE (result))
5485 goto return_result;
5487 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5488 rhs_origtype, ic_assign, false, NULL_TREE,
5489 NULL_TREE, 0);
5490 protected_set_expr_location (result, location);
5492 return_result:
5493 if (rhseval)
5494 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5495 return result;
5498 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5499 This is used to implement -fplan9-extensions. */
5501 static bool
5502 find_anonymous_field_with_type (tree struct_type, tree type)
5504 tree field;
5505 bool found;
5507 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5508 || TREE_CODE (struct_type) == UNION_TYPE);
5509 found = false;
5510 for (field = TYPE_FIELDS (struct_type);
5511 field != NULL_TREE;
5512 field = TREE_CHAIN (field))
5514 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5515 ? c_build_qualified_type (TREE_TYPE (field),
5516 TYPE_QUAL_ATOMIC)
5517 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5518 if (DECL_NAME (field) == NULL
5519 && comptypes (type, fieldtype))
5521 if (found)
5522 return false;
5523 found = true;
5525 else if (DECL_NAME (field) == NULL
5526 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5527 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5528 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5530 if (found)
5531 return false;
5532 found = true;
5535 return found;
5538 /* RHS is an expression whose type is pointer to struct. If there is
5539 an anonymous field in RHS with type TYPE, then return a pointer to
5540 that field in RHS. This is used with -fplan9-extensions. This
5541 returns NULL if no conversion could be found. */
5543 static tree
5544 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5546 tree rhs_struct_type, lhs_main_type;
5547 tree field, found_field;
5548 bool found_sub_field;
5549 tree ret;
5551 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5552 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5553 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5554 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5556 gcc_assert (POINTER_TYPE_P (type));
5557 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5558 ? c_build_qualified_type (TREE_TYPE (type),
5559 TYPE_QUAL_ATOMIC)
5560 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5562 found_field = NULL_TREE;
5563 found_sub_field = false;
5564 for (field = TYPE_FIELDS (rhs_struct_type);
5565 field != NULL_TREE;
5566 field = TREE_CHAIN (field))
5568 if (DECL_NAME (field) != NULL_TREE
5569 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5570 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5571 continue;
5572 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5573 ? c_build_qualified_type (TREE_TYPE (field),
5574 TYPE_QUAL_ATOMIC)
5575 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5576 if (comptypes (lhs_main_type, fieldtype))
5578 if (found_field != NULL_TREE)
5579 return NULL_TREE;
5580 found_field = field;
5582 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5583 lhs_main_type))
5585 if (found_field != NULL_TREE)
5586 return NULL_TREE;
5587 found_field = field;
5588 found_sub_field = true;
5592 if (found_field == NULL_TREE)
5593 return NULL_TREE;
5595 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5596 build_fold_indirect_ref (rhs), found_field,
5597 NULL_TREE);
5598 ret = build_fold_addr_expr_loc (location, ret);
5600 if (found_sub_field)
5602 ret = convert_to_anonymous_field (location, type, ret);
5603 gcc_assert (ret != NULL_TREE);
5606 return ret;
5609 /* Issue an error message for a bad initializer component.
5610 GMSGID identifies the message.
5611 The component name is taken from the spelling stack. */
5613 static void
5614 error_init (location_t loc, const char *gmsgid)
5616 char *ofwhat;
5618 /* The gmsgid may be a format string with %< and %>. */
5619 error_at (loc, gmsgid);
5620 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5621 if (*ofwhat)
5622 inform (loc, "(near initialization for %qs)", ofwhat);
5625 /* Issue a pedantic warning for a bad initializer component. OPT is
5626 the option OPT_* (from options.h) controlling this warning or 0 if
5627 it is unconditionally given. GMSGID identifies the message. The
5628 component name is taken from the spelling stack. */
5630 static void
5631 pedwarn_init (location_t location, int opt, const char *gmsgid)
5633 char *ofwhat;
5634 bool warned;
5636 /* The gmsgid may be a format string with %< and %>. */
5637 warned = pedwarn (location, opt, gmsgid);
5638 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5639 if (*ofwhat && warned)
5640 inform (location, "(near initialization for %qs)", ofwhat);
5643 /* Issue a warning for a bad initializer component.
5645 OPT is the OPT_W* value corresponding to the warning option that
5646 controls this warning. GMSGID identifies the message. The
5647 component name is taken from the spelling stack. */
5649 static void
5650 warning_init (location_t loc, int opt, const char *gmsgid)
5652 char *ofwhat;
5653 bool warned;
5655 /* The gmsgid may be a format string with %< and %>. */
5656 warned = warning_at (loc, opt, gmsgid);
5657 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5658 if (*ofwhat && warned)
5659 inform (loc, "(near initialization for %qs)", ofwhat);
5662 /* If TYPE is an array type and EXPR is a parenthesized string
5663 constant, warn if pedantic that EXPR is being used to initialize an
5664 object of type TYPE. */
5666 void
5667 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5669 if (pedantic
5670 && TREE_CODE (type) == ARRAY_TYPE
5671 && TREE_CODE (expr.value) == STRING_CST
5672 && expr.original_code != STRING_CST)
5673 pedwarn_init (loc, OPT_Wpedantic,
5674 "array initialized from parenthesized string constant");
5677 /* Convert value RHS to type TYPE as preparation for an assignment to
5678 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5679 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5680 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5681 constant before any folding.
5682 The real work of conversion is done by `convert'.
5683 The purpose of this function is to generate error messages
5684 for assignments that are not allowed in C.
5685 ERRTYPE says whether it is argument passing, assignment,
5686 initialization or return.
5688 LOCATION is the location of the assignment, EXPR_LOC is the location of
5689 the RHS or, for a function, location of an argument.
5690 FUNCTION is a tree for the function being called.
5691 PARMNUM is the number of the argument, for printing in error messages. */
5693 static tree
5694 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5695 tree rhs, tree origtype, enum impl_conv errtype,
5696 bool null_pointer_constant, tree fundecl,
5697 tree function, int parmnum)
5699 enum tree_code codel = TREE_CODE (type);
5700 tree orig_rhs = rhs;
5701 tree rhstype;
5702 enum tree_code coder;
5703 tree rname = NULL_TREE;
5704 bool objc_ok = false;
5706 if (errtype == ic_argpass)
5708 tree selector;
5709 /* Change pointer to function to the function itself for
5710 diagnostics. */
5711 if (TREE_CODE (function) == ADDR_EXPR
5712 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5713 function = TREE_OPERAND (function, 0);
5715 /* Handle an ObjC selector specially for diagnostics. */
5716 selector = objc_message_selector ();
5717 rname = function;
5718 if (selector && parmnum > 2)
5720 rname = selector;
5721 parmnum -= 2;
5725 /* This macro is used to emit diagnostics to ensure that all format
5726 strings are complete sentences, visible to gettext and checked at
5727 compile time. */
5728 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5729 do { \
5730 switch (errtype) \
5732 case ic_argpass: \
5733 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5734 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5735 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5736 "expected %qT but argument is of type %qT", \
5737 type, rhstype); \
5738 break; \
5739 case ic_assign: \
5740 pedwarn (LOCATION, OPT, AS); \
5741 break; \
5742 case ic_init: \
5743 pedwarn_init (LOCATION, OPT, IN); \
5744 break; \
5745 case ic_return: \
5746 pedwarn (LOCATION, OPT, RE); \
5747 break; \
5748 default: \
5749 gcc_unreachable (); \
5751 } while (0)
5753 /* This macro is used to emit diagnostics to ensure that all format
5754 strings are complete sentences, visible to gettext and checked at
5755 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
5756 extra parameter to enumerate qualifiers. */
5757 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5758 do { \
5759 switch (errtype) \
5761 case ic_argpass: \
5762 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5763 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5764 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5765 "expected %qT but argument is of type %qT", \
5766 type, rhstype); \
5767 break; \
5768 case ic_assign: \
5769 pedwarn (LOCATION, OPT, AS, QUALS); \
5770 break; \
5771 case ic_init: \
5772 pedwarn (LOCATION, OPT, IN, QUALS); \
5773 break; \
5774 case ic_return: \
5775 pedwarn (LOCATION, OPT, RE, QUALS); \
5776 break; \
5777 default: \
5778 gcc_unreachable (); \
5780 } while (0)
5782 /* This macro is used to emit diagnostics to ensure that all format
5783 strings are complete sentences, visible to gettext and checked at
5784 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5785 warning_at instead of pedwarn. */
5786 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5787 do { \
5788 switch (errtype) \
5790 case ic_argpass: \
5791 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5792 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5793 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5794 "expected %qT but argument is of type %qT", \
5795 type, rhstype); \
5796 break; \
5797 case ic_assign: \
5798 warning_at (LOCATION, OPT, AS, QUALS); \
5799 break; \
5800 case ic_init: \
5801 warning_at (LOCATION, OPT, IN, QUALS); \
5802 break; \
5803 case ic_return: \
5804 warning_at (LOCATION, OPT, RE, QUALS); \
5805 break; \
5806 default: \
5807 gcc_unreachable (); \
5809 } while (0)
5811 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5812 rhs = TREE_OPERAND (rhs, 0);
5814 rhstype = TREE_TYPE (rhs);
5815 coder = TREE_CODE (rhstype);
5817 if (coder == ERROR_MARK)
5818 return error_mark_node;
5820 if (c_dialect_objc ())
5822 int parmno;
5824 switch (errtype)
5826 case ic_return:
5827 parmno = 0;
5828 break;
5830 case ic_assign:
5831 parmno = -1;
5832 break;
5834 case ic_init:
5835 parmno = -2;
5836 break;
5838 default:
5839 parmno = parmnum;
5840 break;
5843 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5846 if (warn_cxx_compat)
5848 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5849 if (checktype != error_mark_node
5850 && TREE_CODE (type) == ENUMERAL_TYPE
5851 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5853 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5854 G_("enum conversion when passing argument "
5855 "%d of %qE is invalid in C++"),
5856 G_("enum conversion in assignment is "
5857 "invalid in C++"),
5858 G_("enum conversion in initialization is "
5859 "invalid in C++"),
5860 G_("enum conversion in return is "
5861 "invalid in C++"));
5865 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5866 return rhs;
5868 if (coder == VOID_TYPE)
5870 /* Except for passing an argument to an unprototyped function,
5871 this is a constraint violation. When passing an argument to
5872 an unprototyped function, it is compile-time undefined;
5873 making it a constraint in that case was rejected in
5874 DR#252. */
5875 error_at (location, "void value not ignored as it ought to be");
5876 return error_mark_node;
5878 rhs = require_complete_type (rhs);
5879 if (rhs == error_mark_node)
5880 return error_mark_node;
5881 /* A non-reference type can convert to a reference. This handles
5882 va_start, va_copy and possibly port built-ins. */
5883 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5885 if (!lvalue_p (rhs))
5887 error_at (location, "cannot pass rvalue to reference parameter");
5888 return error_mark_node;
5890 if (!c_mark_addressable (rhs))
5891 return error_mark_node;
5892 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5893 SET_EXPR_LOCATION (rhs, location);
5895 rhs = convert_for_assignment (location, expr_loc,
5896 build_pointer_type (TREE_TYPE (type)),
5897 rhs, origtype, errtype,
5898 null_pointer_constant, fundecl, function,
5899 parmnum);
5900 if (rhs == error_mark_node)
5901 return error_mark_node;
5903 rhs = build1 (NOP_EXPR, type, rhs);
5904 SET_EXPR_LOCATION (rhs, location);
5905 return rhs;
5907 /* Some types can interconvert without explicit casts. */
5908 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5909 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5910 return convert (type, rhs);
5911 /* Arithmetic types all interconvert, and enum is treated like int. */
5912 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5913 || codel == FIXED_POINT_TYPE
5914 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5915 || codel == BOOLEAN_TYPE)
5916 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5917 || coder == FIXED_POINT_TYPE
5918 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5919 || coder == BOOLEAN_TYPE))
5921 tree ret;
5922 bool save = in_late_binary_op;
5923 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
5924 || (coder == REAL_TYPE
5925 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
5926 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
5927 in_late_binary_op = true;
5928 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5929 ? expr_loc : location, type, orig_rhs);
5930 in_late_binary_op = save;
5931 return ret;
5934 /* Aggregates in different TUs might need conversion. */
5935 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5936 && codel == coder
5937 && comptypes (type, rhstype))
5938 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5939 ? expr_loc : location, type, rhs);
5941 /* Conversion to a transparent union or record from its member types.
5942 This applies only to function arguments. */
5943 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5944 && TYPE_TRANSPARENT_AGGR (type))
5945 && errtype == ic_argpass)
5947 tree memb, marginal_memb = NULL_TREE;
5949 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5951 tree memb_type = TREE_TYPE (memb);
5953 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5954 TYPE_MAIN_VARIANT (rhstype)))
5955 break;
5957 if (TREE_CODE (memb_type) != POINTER_TYPE)
5958 continue;
5960 if (coder == POINTER_TYPE)
5962 tree ttl = TREE_TYPE (memb_type);
5963 tree ttr = TREE_TYPE (rhstype);
5965 /* Any non-function converts to a [const][volatile] void *
5966 and vice versa; otherwise, targets must be the same.
5967 Meanwhile, the lhs target must have all the qualifiers of
5968 the rhs. */
5969 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5970 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5971 || comp_target_types (location, memb_type, rhstype))
5973 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5974 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5975 /* If this type won't generate any warnings, use it. */
5976 if (lquals == rquals
5977 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5978 && TREE_CODE (ttl) == FUNCTION_TYPE)
5979 ? ((lquals | rquals) == rquals)
5980 : ((lquals | rquals) == lquals)))
5981 break;
5983 /* Keep looking for a better type, but remember this one. */
5984 if (!marginal_memb)
5985 marginal_memb = memb;
5989 /* Can convert integer zero to any pointer type. */
5990 if (null_pointer_constant)
5992 rhs = null_pointer_node;
5993 break;
5997 if (memb || marginal_memb)
5999 if (!memb)
6001 /* We have only a marginally acceptable member type;
6002 it needs a warning. */
6003 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6004 tree ttr = TREE_TYPE (rhstype);
6006 /* Const and volatile mean something different for function
6007 types, so the usual warnings are not appropriate. */
6008 if (TREE_CODE (ttr) == FUNCTION_TYPE
6009 && TREE_CODE (ttl) == FUNCTION_TYPE)
6011 /* Because const and volatile on functions are
6012 restrictions that say the function will not do
6013 certain things, it is okay to use a const or volatile
6014 function where an ordinary one is wanted, but not
6015 vice-versa. */
6016 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6017 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6018 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6019 OPT_Wdiscarded_qualifiers,
6020 G_("passing argument %d of %qE "
6021 "makes %q#v qualified function "
6022 "pointer from unqualified"),
6023 G_("assignment makes %q#v qualified "
6024 "function pointer from "
6025 "unqualified"),
6026 G_("initialization makes %q#v qualified "
6027 "function pointer from "
6028 "unqualified"),
6029 G_("return makes %q#v qualified function "
6030 "pointer from unqualified"),
6031 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6033 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6034 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6035 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6036 OPT_Wdiscarded_qualifiers,
6037 G_("passing argument %d of %qE discards "
6038 "%qv qualifier from pointer target type"),
6039 G_("assignment discards %qv qualifier "
6040 "from pointer target type"),
6041 G_("initialization discards %qv qualifier "
6042 "from pointer target type"),
6043 G_("return discards %qv qualifier from "
6044 "pointer target type"),
6045 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6047 memb = marginal_memb;
6050 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6051 pedwarn (location, OPT_Wpedantic,
6052 "ISO C prohibits argument conversion to union type");
6054 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6055 return build_constructor_single (type, memb, rhs);
6059 /* Conversions among pointers */
6060 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6061 && (coder == codel))
6063 tree ttl = TREE_TYPE (type);
6064 tree ttr = TREE_TYPE (rhstype);
6065 tree mvl = ttl;
6066 tree mvr = ttr;
6067 bool is_opaque_pointer;
6068 int target_cmp = 0; /* Cache comp_target_types () result. */
6069 addr_space_t asl;
6070 addr_space_t asr;
6072 if (TREE_CODE (mvl) != ARRAY_TYPE)
6073 mvl = (TYPE_ATOMIC (mvl)
6074 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6075 TYPE_QUAL_ATOMIC)
6076 : TYPE_MAIN_VARIANT (mvl));
6077 if (TREE_CODE (mvr) != ARRAY_TYPE)
6078 mvr = (TYPE_ATOMIC (mvr)
6079 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6080 TYPE_QUAL_ATOMIC)
6081 : TYPE_MAIN_VARIANT (mvr));
6082 /* Opaque pointers are treated like void pointers. */
6083 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6085 /* The Plan 9 compiler permits a pointer to a struct to be
6086 automatically converted into a pointer to an anonymous field
6087 within the struct. */
6088 if (flag_plan9_extensions
6089 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6090 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6091 && mvl != mvr)
6093 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6094 if (new_rhs != NULL_TREE)
6096 rhs = new_rhs;
6097 rhstype = TREE_TYPE (rhs);
6098 coder = TREE_CODE (rhstype);
6099 ttr = TREE_TYPE (rhstype);
6100 mvr = TYPE_MAIN_VARIANT (ttr);
6104 /* C++ does not allow the implicit conversion void* -> T*. However,
6105 for the purpose of reducing the number of false positives, we
6106 tolerate the special case of
6108 int *p = NULL;
6110 where NULL is typically defined in C to be '(void *) 0'. */
6111 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6112 warning_at (errtype == ic_argpass ? expr_loc : location,
6113 OPT_Wc___compat,
6114 "request for implicit conversion "
6115 "from %qT to %qT not permitted in C++", rhstype, type);
6117 /* See if the pointers point to incompatible address spaces. */
6118 asl = TYPE_ADDR_SPACE (ttl);
6119 asr = TYPE_ADDR_SPACE (ttr);
6120 if (!null_pointer_constant_p (rhs)
6121 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6123 switch (errtype)
6125 case ic_argpass:
6126 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6127 "non-enclosed address space", parmnum, rname);
6128 break;
6129 case ic_assign:
6130 error_at (location, "assignment from pointer to "
6131 "non-enclosed address space");
6132 break;
6133 case ic_init:
6134 error_at (location, "initialization from pointer to "
6135 "non-enclosed address space");
6136 break;
6137 case ic_return:
6138 error_at (location, "return from pointer to "
6139 "non-enclosed address space");
6140 break;
6141 default:
6142 gcc_unreachable ();
6144 return error_mark_node;
6147 /* Check if the right-hand side has a format attribute but the
6148 left-hand side doesn't. */
6149 if (warn_suggest_attribute_format
6150 && check_missing_format_attribute (type, rhstype))
6152 switch (errtype)
6154 case ic_argpass:
6155 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6156 "argument %d of %qE might be "
6157 "a candidate for a format attribute",
6158 parmnum, rname);
6159 break;
6160 case ic_assign:
6161 warning_at (location, OPT_Wsuggest_attribute_format,
6162 "assignment left-hand side might be "
6163 "a candidate for a format attribute");
6164 break;
6165 case ic_init:
6166 warning_at (location, OPT_Wsuggest_attribute_format,
6167 "initialization left-hand side might be "
6168 "a candidate for a format attribute");
6169 break;
6170 case ic_return:
6171 warning_at (location, OPT_Wsuggest_attribute_format,
6172 "return type might be "
6173 "a candidate for a format attribute");
6174 break;
6175 default:
6176 gcc_unreachable ();
6180 /* Any non-function converts to a [const][volatile] void *
6181 and vice versa; otherwise, targets must be the same.
6182 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6183 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6184 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6185 || (target_cmp = comp_target_types (location, type, rhstype))
6186 || is_opaque_pointer
6187 || ((c_common_unsigned_type (mvl)
6188 == c_common_unsigned_type (mvr))
6189 && (c_common_signed_type (mvl)
6190 == c_common_signed_type (mvr))
6191 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6193 /* Warn about loss of qualifers from pointers to arrays with
6194 qualifiers on the element type. */
6195 if (TREE_CODE (ttr) == ARRAY_TYPE)
6197 ttr = strip_array_types (ttr);
6198 ttl = strip_array_types (ttl);
6200 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6201 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6202 WARNING_FOR_QUALIFIERS (location, expr_loc,
6203 OPT_Wdiscarded_array_qualifiers,
6204 G_("passing argument %d of %qE discards "
6205 "%qv qualifier from pointer target type"),
6206 G_("assignment discards %qv qualifier "
6207 "from pointer target type"),
6208 G_("initialization discards %qv qualifier "
6209 "from pointer target type"),
6210 G_("return discards %qv qualifier from "
6211 "pointer target type"),
6212 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6214 else if (pedantic
6215 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6217 (VOID_TYPE_P (ttr)
6218 && !null_pointer_constant
6219 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6220 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6221 G_("ISO C forbids passing argument %d of "
6222 "%qE between function pointer "
6223 "and %<void *%>"),
6224 G_("ISO C forbids assignment between "
6225 "function pointer and %<void *%>"),
6226 G_("ISO C forbids initialization between "
6227 "function pointer and %<void *%>"),
6228 G_("ISO C forbids return between function "
6229 "pointer and %<void *%>"));
6230 /* Const and volatile mean something different for function types,
6231 so the usual warnings are not appropriate. */
6232 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6233 && TREE_CODE (ttl) != FUNCTION_TYPE)
6235 /* Don't warn about loss of qualifier for conversions from
6236 qualified void* to pointers to arrays with corresponding
6237 qualifier on the element type. */
6238 if (!pedantic)
6239 ttl = strip_array_types (ttl);
6241 /* Assignments between atomic and non-atomic objects are OK. */
6242 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6243 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6245 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6246 OPT_Wdiscarded_qualifiers,
6247 G_("passing argument %d of %qE discards "
6248 "%qv qualifier from pointer target type"),
6249 G_("assignment discards %qv qualifier "
6250 "from pointer target type"),
6251 G_("initialization discards %qv qualifier "
6252 "from pointer target type"),
6253 G_("return discards %qv qualifier from "
6254 "pointer target type"),
6255 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6257 /* If this is not a case of ignoring a mismatch in signedness,
6258 no warning. */
6259 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6260 || target_cmp)
6262 /* If there is a mismatch, do warn. */
6263 else if (warn_pointer_sign)
6264 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6265 G_("pointer targets in passing argument "
6266 "%d of %qE differ in signedness"),
6267 G_("pointer targets in assignment "
6268 "differ in signedness"),
6269 G_("pointer targets in initialization "
6270 "differ in signedness"),
6271 G_("pointer targets in return differ "
6272 "in signedness"));
6274 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6275 && TREE_CODE (ttr) == FUNCTION_TYPE)
6277 /* Because const and volatile on functions are restrictions
6278 that say the function will not do certain things,
6279 it is okay to use a const or volatile function
6280 where an ordinary one is wanted, but not vice-versa. */
6281 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6282 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6283 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6284 OPT_Wdiscarded_qualifiers,
6285 G_("passing argument %d of %qE makes "
6286 "%q#v qualified function pointer "
6287 "from unqualified"),
6288 G_("assignment makes %q#v qualified function "
6289 "pointer from unqualified"),
6290 G_("initialization makes %q#v qualified "
6291 "function pointer from unqualified"),
6292 G_("return makes %q#v qualified function "
6293 "pointer from unqualified"),
6294 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6297 else
6298 /* Avoid warning about the volatile ObjC EH puts on decls. */
6299 if (!objc_ok)
6300 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6301 OPT_Wincompatible_pointer_types,
6302 G_("passing argument %d of %qE from "
6303 "incompatible pointer type"),
6304 G_("assignment from incompatible pointer type"),
6305 G_("initialization from incompatible "
6306 "pointer type"),
6307 G_("return from incompatible pointer type"));
6309 return convert (type, rhs);
6311 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6313 /* ??? This should not be an error when inlining calls to
6314 unprototyped functions. */
6315 error_at (location, "invalid use of non-lvalue array");
6316 return error_mark_node;
6318 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6320 /* An explicit constant 0 can convert to a pointer,
6321 or one that results from arithmetic, even including
6322 a cast to integer type. */
6323 if (!null_pointer_constant)
6324 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6325 OPT_Wint_conversion,
6326 G_("passing argument %d of %qE makes "
6327 "pointer from integer without a cast"),
6328 G_("assignment makes pointer from integer "
6329 "without a cast"),
6330 G_("initialization makes pointer from "
6331 "integer without a cast"),
6332 G_("return makes pointer from integer "
6333 "without a cast"));
6335 return convert (type, rhs);
6337 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6339 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6340 OPT_Wint_conversion,
6341 G_("passing argument %d of %qE makes integer "
6342 "from pointer without a cast"),
6343 G_("assignment makes integer from pointer "
6344 "without a cast"),
6345 G_("initialization makes integer from pointer "
6346 "without a cast"),
6347 G_("return makes integer from pointer "
6348 "without a cast"));
6349 return convert (type, rhs);
6351 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6353 tree ret;
6354 bool save = in_late_binary_op;
6355 in_late_binary_op = true;
6356 ret = convert (type, rhs);
6357 in_late_binary_op = save;
6358 return ret;
6361 switch (errtype)
6363 case ic_argpass:
6364 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6365 rname);
6366 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6367 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6368 "expected %qT but argument is of type %qT", type, rhstype);
6369 break;
6370 case ic_assign:
6371 error_at (location, "incompatible types when assigning to type %qT from "
6372 "type %qT", type, rhstype);
6373 break;
6374 case ic_init:
6375 error_at (location,
6376 "incompatible types when initializing type %qT using type %qT",
6377 type, rhstype);
6378 break;
6379 case ic_return:
6380 error_at (location,
6381 "incompatible types when returning type %qT but %qT was "
6382 "expected", rhstype, type);
6383 break;
6384 default:
6385 gcc_unreachable ();
6388 return error_mark_node;
6391 /* If VALUE is a compound expr all of whose expressions are constant, then
6392 return its value. Otherwise, return error_mark_node.
6394 This is for handling COMPOUND_EXPRs as initializer elements
6395 which is allowed with a warning when -pedantic is specified. */
6397 static tree
6398 valid_compound_expr_initializer (tree value, tree endtype)
6400 if (TREE_CODE (value) == COMPOUND_EXPR)
6402 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6403 == error_mark_node)
6404 return error_mark_node;
6405 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6406 endtype);
6408 else if (!initializer_constant_valid_p (value, endtype))
6409 return error_mark_node;
6410 else
6411 return value;
6414 /* Perform appropriate conversions on the initial value of a variable,
6415 store it in the declaration DECL,
6416 and print any error messages that are appropriate.
6417 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6418 If the init is invalid, store an ERROR_MARK.
6420 INIT_LOC is the location of the initial value. */
6422 void
6423 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6425 tree value, type;
6426 bool npc = false;
6428 /* If variable's type was invalidly declared, just ignore it. */
6430 type = TREE_TYPE (decl);
6431 if (TREE_CODE (type) == ERROR_MARK)
6432 return;
6434 /* Digest the specified initializer into an expression. */
6436 if (init)
6437 npc = null_pointer_constant_p (init);
6438 value = digest_init (init_loc, type, init, origtype, npc,
6439 true, TREE_STATIC (decl));
6441 /* Store the expression if valid; else report error. */
6443 if (!in_system_header_at (input_location)
6444 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6445 warning (OPT_Wtraditional, "traditional C rejects automatic "
6446 "aggregate initialization");
6448 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6449 DECL_INITIAL (decl) = value;
6451 /* ANSI wants warnings about out-of-range constant initializers. */
6452 STRIP_TYPE_NOPS (value);
6453 if (TREE_STATIC (decl))
6454 constant_expression_warning (value);
6456 /* Check if we need to set array size from compound literal size. */
6457 if (TREE_CODE (type) == ARRAY_TYPE
6458 && TYPE_DOMAIN (type) == 0
6459 && value != error_mark_node)
6461 tree inside_init = init;
6463 STRIP_TYPE_NOPS (inside_init);
6464 inside_init = fold (inside_init);
6466 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6468 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6470 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6472 /* For int foo[] = (int [3]){1}; we need to set array size
6473 now since later on array initializer will be just the
6474 brace enclosed list of the compound literal. */
6475 tree etype = strip_array_types (TREE_TYPE (decl));
6476 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6477 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6478 layout_type (type);
6479 layout_decl (cldecl, 0);
6480 TREE_TYPE (decl)
6481 = c_build_qualified_type (type, TYPE_QUALS (etype));
6487 /* Methods for storing and printing names for error messages. */
6489 /* Implement a spelling stack that allows components of a name to be pushed
6490 and popped. Each element on the stack is this structure. */
6492 struct spelling
6494 int kind;
6495 union
6497 unsigned HOST_WIDE_INT i;
6498 const char *s;
6499 } u;
6502 #define SPELLING_STRING 1
6503 #define SPELLING_MEMBER 2
6504 #define SPELLING_BOUNDS 3
6506 static struct spelling *spelling; /* Next stack element (unused). */
6507 static struct spelling *spelling_base; /* Spelling stack base. */
6508 static int spelling_size; /* Size of the spelling stack. */
6510 /* Macros to save and restore the spelling stack around push_... functions.
6511 Alternative to SAVE_SPELLING_STACK. */
6513 #define SPELLING_DEPTH() (spelling - spelling_base)
6514 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6516 /* Push an element on the spelling stack with type KIND and assign VALUE
6517 to MEMBER. */
6519 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6521 int depth = SPELLING_DEPTH (); \
6523 if (depth >= spelling_size) \
6525 spelling_size += 10; \
6526 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6527 spelling_size); \
6528 RESTORE_SPELLING_DEPTH (depth); \
6531 spelling->kind = (KIND); \
6532 spelling->MEMBER = (VALUE); \
6533 spelling++; \
6536 /* Push STRING on the stack. Printed literally. */
6538 static void
6539 push_string (const char *string)
6541 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6544 /* Push a member name on the stack. Printed as '.' STRING. */
6546 static void
6547 push_member_name (tree decl)
6549 const char *const string
6550 = (DECL_NAME (decl)
6551 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6552 : _("<anonymous>"));
6553 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6556 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6558 static void
6559 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6561 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6564 /* Compute the maximum size in bytes of the printed spelling. */
6566 static int
6567 spelling_length (void)
6569 int size = 0;
6570 struct spelling *p;
6572 for (p = spelling_base; p < spelling; p++)
6574 if (p->kind == SPELLING_BOUNDS)
6575 size += 25;
6576 else
6577 size += strlen (p->u.s) + 1;
6580 return size;
6583 /* Print the spelling to BUFFER and return it. */
6585 static char *
6586 print_spelling (char *buffer)
6588 char *d = buffer;
6589 struct spelling *p;
6591 for (p = spelling_base; p < spelling; p++)
6592 if (p->kind == SPELLING_BOUNDS)
6594 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6595 d += strlen (d);
6597 else
6599 const char *s;
6600 if (p->kind == SPELLING_MEMBER)
6601 *d++ = '.';
6602 for (s = p->u.s; (*d = *s++); d++)
6605 *d++ = '\0';
6606 return buffer;
6609 /* Digest the parser output INIT as an initializer for type TYPE.
6610 Return a C expression of type TYPE to represent the initial value.
6612 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6614 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6616 If INIT is a string constant, STRICT_STRING is true if it is
6617 unparenthesized or we should not warn here for it being parenthesized.
6618 For other types of INIT, STRICT_STRING is not used.
6620 INIT_LOC is the location of the INIT.
6622 REQUIRE_CONSTANT requests an error if non-constant initializers or
6623 elements are seen. */
6625 static tree
6626 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6627 bool null_pointer_constant, bool strict_string,
6628 int require_constant)
6630 enum tree_code code = TREE_CODE (type);
6631 tree inside_init = init;
6632 tree semantic_type = NULL_TREE;
6633 bool maybe_const = true;
6635 if (type == error_mark_node
6636 || !init
6637 || error_operand_p (init))
6638 return error_mark_node;
6640 STRIP_TYPE_NOPS (inside_init);
6642 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6644 semantic_type = TREE_TYPE (inside_init);
6645 inside_init = TREE_OPERAND (inside_init, 0);
6647 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6648 inside_init = decl_constant_value_for_optimization (inside_init);
6650 /* Initialization of an array of chars from a string constant
6651 optionally enclosed in braces. */
6653 if (code == ARRAY_TYPE && inside_init
6654 && TREE_CODE (inside_init) == STRING_CST)
6656 tree typ1
6657 = (TYPE_ATOMIC (TREE_TYPE (type))
6658 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6659 TYPE_QUAL_ATOMIC)
6660 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6661 /* Note that an array could be both an array of character type
6662 and an array of wchar_t if wchar_t is signed char or unsigned
6663 char. */
6664 bool char_array = (typ1 == char_type_node
6665 || typ1 == signed_char_type_node
6666 || typ1 == unsigned_char_type_node);
6667 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6668 bool char16_array = !!comptypes (typ1, char16_type_node);
6669 bool char32_array = !!comptypes (typ1, char32_type_node);
6671 if (char_array || wchar_array || char16_array || char32_array)
6673 struct c_expr expr;
6674 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6675 expr.value = inside_init;
6676 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6677 expr.original_type = NULL;
6678 maybe_warn_string_init (init_loc, type, expr);
6680 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6681 pedwarn_init (init_loc, OPT_Wpedantic,
6682 "initialization of a flexible array member");
6684 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6685 TYPE_MAIN_VARIANT (type)))
6686 return inside_init;
6688 if (char_array)
6690 if (typ2 != char_type_node)
6692 error_init (init_loc, "char-array initialized from wide "
6693 "string");
6694 return error_mark_node;
6697 else
6699 if (typ2 == char_type_node)
6701 error_init (init_loc, "wide character array initialized "
6702 "from non-wide string");
6703 return error_mark_node;
6705 else if (!comptypes(typ1, typ2))
6707 error_init (init_loc, "wide character array initialized "
6708 "from incompatible wide string");
6709 return error_mark_node;
6713 TREE_TYPE (inside_init) = type;
6714 if (TYPE_DOMAIN (type) != 0
6715 && TYPE_SIZE (type) != 0
6716 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6718 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6720 /* Subtract the size of a single (possibly wide) character
6721 because it's ok to ignore the terminating null char
6722 that is counted in the length of the constant. */
6723 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6724 (len
6725 - (TYPE_PRECISION (typ1)
6726 / BITS_PER_UNIT))))
6727 pedwarn_init (init_loc, 0,
6728 ("initializer-string for array of chars "
6729 "is too long"));
6730 else if (warn_cxx_compat
6731 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6732 warning_at (init_loc, OPT_Wc___compat,
6733 ("initializer-string for array chars "
6734 "is too long for C++"));
6737 return inside_init;
6739 else if (INTEGRAL_TYPE_P (typ1))
6741 error_init (init_loc, "array of inappropriate type initialized "
6742 "from string constant");
6743 return error_mark_node;
6747 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6748 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6749 below and handle as a constructor. */
6750 if (code == VECTOR_TYPE
6751 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6752 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6753 && TREE_CONSTANT (inside_init))
6755 if (TREE_CODE (inside_init) == VECTOR_CST
6756 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6757 TYPE_MAIN_VARIANT (type)))
6758 return inside_init;
6760 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6762 unsigned HOST_WIDE_INT ix;
6763 tree value;
6764 bool constant_p = true;
6766 /* Iterate through elements and check if all constructor
6767 elements are *_CSTs. */
6768 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6769 if (!CONSTANT_CLASS_P (value))
6771 constant_p = false;
6772 break;
6775 if (constant_p)
6776 return build_vector_from_ctor (type,
6777 CONSTRUCTOR_ELTS (inside_init));
6781 if (warn_sequence_point)
6782 verify_sequence_points (inside_init);
6784 /* Any type can be initialized
6785 from an expression of the same type, optionally with braces. */
6787 if (inside_init && TREE_TYPE (inside_init) != 0
6788 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6789 TYPE_MAIN_VARIANT (type))
6790 || (code == ARRAY_TYPE
6791 && comptypes (TREE_TYPE (inside_init), type))
6792 || (code == VECTOR_TYPE
6793 && comptypes (TREE_TYPE (inside_init), type))
6794 || (code == POINTER_TYPE
6795 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6796 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6797 TREE_TYPE (type)))))
6799 if (code == POINTER_TYPE)
6801 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6803 if (TREE_CODE (inside_init) == STRING_CST
6804 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6805 inside_init = array_to_pointer_conversion
6806 (init_loc, inside_init);
6807 else
6809 error_init (init_loc, "invalid use of non-lvalue array");
6810 return error_mark_node;
6815 if (code == VECTOR_TYPE)
6816 /* Although the types are compatible, we may require a
6817 conversion. */
6818 inside_init = convert (type, inside_init);
6820 if (require_constant
6821 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6823 /* As an extension, allow initializing objects with static storage
6824 duration with compound literals (which are then treated just as
6825 the brace enclosed list they contain). Also allow this for
6826 vectors, as we can only assign them with compound literals. */
6827 if (flag_isoc99 && code != VECTOR_TYPE)
6828 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6829 "is not constant");
6830 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6831 inside_init = DECL_INITIAL (decl);
6834 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6835 && TREE_CODE (inside_init) != CONSTRUCTOR)
6837 error_init (init_loc, "array initialized from non-constant array "
6838 "expression");
6839 return error_mark_node;
6842 /* Compound expressions can only occur here if -Wpedantic or
6843 -pedantic-errors is specified. In the later case, we always want
6844 an error. In the former case, we simply want a warning. */
6845 if (require_constant && pedantic
6846 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6848 inside_init
6849 = valid_compound_expr_initializer (inside_init,
6850 TREE_TYPE (inside_init));
6851 if (inside_init == error_mark_node)
6852 error_init (init_loc, "initializer element is not constant");
6853 else
6854 pedwarn_init (init_loc, OPT_Wpedantic,
6855 "initializer element is not constant");
6856 if (flag_pedantic_errors)
6857 inside_init = error_mark_node;
6859 else if (require_constant
6860 && !initializer_constant_valid_p (inside_init,
6861 TREE_TYPE (inside_init)))
6863 error_init (init_loc, "initializer element is not constant");
6864 inside_init = error_mark_node;
6866 else if (require_constant && !maybe_const)
6867 pedwarn_init (init_loc, OPT_Wpedantic,
6868 "initializer element is not a constant expression");
6870 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6871 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6872 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6873 type, inside_init, origtype,
6874 ic_init, null_pointer_constant,
6875 NULL_TREE, NULL_TREE, 0);
6876 return inside_init;
6879 /* Handle scalar types, including conversions. */
6881 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6882 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6883 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6885 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6886 && (TREE_CODE (init) == STRING_CST
6887 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6888 inside_init = init = array_to_pointer_conversion (init_loc, init);
6889 if (semantic_type)
6890 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6891 inside_init);
6892 inside_init
6893 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6894 inside_init, origtype, ic_init,
6895 null_pointer_constant, NULL_TREE, NULL_TREE,
6898 /* Check to see if we have already given an error message. */
6899 if (inside_init == error_mark_node)
6901 else if (require_constant && !TREE_CONSTANT (inside_init))
6903 error_init (init_loc, "initializer element is not constant");
6904 inside_init = error_mark_node;
6906 else if (require_constant
6907 && !initializer_constant_valid_p (inside_init,
6908 TREE_TYPE (inside_init)))
6910 error_init (init_loc, "initializer element is not computable at "
6911 "load time");
6912 inside_init = error_mark_node;
6914 else if (require_constant && !maybe_const)
6915 pedwarn_init (init_loc, 0,
6916 "initializer element is not a constant expression");
6918 return inside_init;
6921 /* Come here only for records and arrays. */
6923 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6925 error_init (init_loc, "variable-sized object may not be initialized");
6926 return error_mark_node;
6929 error_init (init_loc, "invalid initializer");
6930 return error_mark_node;
6933 /* Handle initializers that use braces. */
6935 /* Type of object we are accumulating a constructor for.
6936 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6937 static tree constructor_type;
6939 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6940 left to fill. */
6941 static tree constructor_fields;
6943 /* For an ARRAY_TYPE, this is the specified index
6944 at which to store the next element we get. */
6945 static tree constructor_index;
6947 /* For an ARRAY_TYPE, this is the maximum index. */
6948 static tree constructor_max_index;
6950 /* For a RECORD_TYPE, this is the first field not yet written out. */
6951 static tree constructor_unfilled_fields;
6953 /* For an ARRAY_TYPE, this is the index of the first element
6954 not yet written out. */
6955 static tree constructor_unfilled_index;
6957 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6958 This is so we can generate gaps between fields, when appropriate. */
6959 static tree constructor_bit_index;
6961 /* If we are saving up the elements rather than allocating them,
6962 this is the list of elements so far (in reverse order,
6963 most recent first). */
6964 static vec<constructor_elt, va_gc> *constructor_elements;
6966 /* 1 if constructor should be incrementally stored into a constructor chain,
6967 0 if all the elements should be kept in AVL tree. */
6968 static int constructor_incremental;
6970 /* 1 if so far this constructor's elements are all compile-time constants. */
6971 static int constructor_constant;
6973 /* 1 if so far this constructor's elements are all valid address constants. */
6974 static int constructor_simple;
6976 /* 1 if this constructor has an element that cannot be part of a
6977 constant expression. */
6978 static int constructor_nonconst;
6980 /* 1 if this constructor is erroneous so far. */
6981 static int constructor_erroneous;
6983 /* 1 if this constructor is the universal zero initializer { 0 }. */
6984 static int constructor_zeroinit;
6986 /* Structure for managing pending initializer elements, organized as an
6987 AVL tree. */
6989 struct init_node
6991 struct init_node *left, *right;
6992 struct init_node *parent;
6993 int balance;
6994 tree purpose;
6995 tree value;
6996 tree origtype;
6999 /* Tree of pending elements at this constructor level.
7000 These are elements encountered out of order
7001 which belong at places we haven't reached yet in actually
7002 writing the output.
7003 Will never hold tree nodes across GC runs. */
7004 static struct init_node *constructor_pending_elts;
7006 /* The SPELLING_DEPTH of this constructor. */
7007 static int constructor_depth;
7009 /* DECL node for which an initializer is being read.
7010 0 means we are reading a constructor expression
7011 such as (struct foo) {...}. */
7012 static tree constructor_decl;
7014 /* Nonzero if this is an initializer for a top-level decl. */
7015 static int constructor_top_level;
7017 /* Nonzero if there were any member designators in this initializer. */
7018 static int constructor_designated;
7020 /* Nesting depth of designator list. */
7021 static int designator_depth;
7023 /* Nonzero if there were diagnosed errors in this designator list. */
7024 static int designator_erroneous;
7027 /* This stack has a level for each implicit or explicit level of
7028 structuring in the initializer, including the outermost one. It
7029 saves the values of most of the variables above. */
7031 struct constructor_range_stack;
7033 struct constructor_stack
7035 struct constructor_stack *next;
7036 tree type;
7037 tree fields;
7038 tree index;
7039 tree max_index;
7040 tree unfilled_index;
7041 tree unfilled_fields;
7042 tree bit_index;
7043 vec<constructor_elt, va_gc> *elements;
7044 struct init_node *pending_elts;
7045 int offset;
7046 int depth;
7047 /* If value nonzero, this value should replace the entire
7048 constructor at this level. */
7049 struct c_expr replacement_value;
7050 struct constructor_range_stack *range_stack;
7051 char constant;
7052 char simple;
7053 char nonconst;
7054 char implicit;
7055 char erroneous;
7056 char outer;
7057 char incremental;
7058 char designated;
7059 int designator_depth;
7062 static struct constructor_stack *constructor_stack;
7064 /* This stack represents designators from some range designator up to
7065 the last designator in the list. */
7067 struct constructor_range_stack
7069 struct constructor_range_stack *next, *prev;
7070 struct constructor_stack *stack;
7071 tree range_start;
7072 tree index;
7073 tree range_end;
7074 tree fields;
7077 static struct constructor_range_stack *constructor_range_stack;
7079 /* This stack records separate initializers that are nested.
7080 Nested initializers can't happen in ANSI C, but GNU C allows them
7081 in cases like { ... (struct foo) { ... } ... }. */
7083 struct initializer_stack
7085 struct initializer_stack *next;
7086 tree decl;
7087 struct constructor_stack *constructor_stack;
7088 struct constructor_range_stack *constructor_range_stack;
7089 vec<constructor_elt, va_gc> *elements;
7090 struct spelling *spelling;
7091 struct spelling *spelling_base;
7092 int spelling_size;
7093 char top_level;
7094 char require_constant_value;
7095 char require_constant_elements;
7098 static struct initializer_stack *initializer_stack;
7100 /* Prepare to parse and output the initializer for variable DECL. */
7102 void
7103 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7105 const char *locus;
7106 struct initializer_stack *p = XNEW (struct initializer_stack);
7108 p->decl = constructor_decl;
7109 p->require_constant_value = require_constant_value;
7110 p->require_constant_elements = require_constant_elements;
7111 p->constructor_stack = constructor_stack;
7112 p->constructor_range_stack = constructor_range_stack;
7113 p->elements = constructor_elements;
7114 p->spelling = spelling;
7115 p->spelling_base = spelling_base;
7116 p->spelling_size = spelling_size;
7117 p->top_level = constructor_top_level;
7118 p->next = initializer_stack;
7119 initializer_stack = p;
7121 constructor_decl = decl;
7122 constructor_designated = 0;
7123 constructor_top_level = top_level;
7125 if (decl != 0 && decl != error_mark_node)
7127 require_constant_value = TREE_STATIC (decl);
7128 require_constant_elements
7129 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7130 /* For a scalar, you can always use any value to initialize,
7131 even within braces. */
7132 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7133 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7134 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7135 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
7136 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7138 else
7140 require_constant_value = 0;
7141 require_constant_elements = 0;
7142 locus = _("(anonymous)");
7145 constructor_stack = 0;
7146 constructor_range_stack = 0;
7148 found_missing_braces = 0;
7150 spelling_base = 0;
7151 spelling_size = 0;
7152 RESTORE_SPELLING_DEPTH (0);
7154 if (locus)
7155 push_string (locus);
7158 void
7159 finish_init (void)
7161 struct initializer_stack *p = initializer_stack;
7163 /* Free the whole constructor stack of this initializer. */
7164 while (constructor_stack)
7166 struct constructor_stack *q = constructor_stack;
7167 constructor_stack = q->next;
7168 free (q);
7171 gcc_assert (!constructor_range_stack);
7173 /* Pop back to the data of the outer initializer (if any). */
7174 free (spelling_base);
7176 constructor_decl = p->decl;
7177 require_constant_value = p->require_constant_value;
7178 require_constant_elements = p->require_constant_elements;
7179 constructor_stack = p->constructor_stack;
7180 constructor_range_stack = p->constructor_range_stack;
7181 constructor_elements = p->elements;
7182 spelling = p->spelling;
7183 spelling_base = p->spelling_base;
7184 spelling_size = p->spelling_size;
7185 constructor_top_level = p->top_level;
7186 initializer_stack = p->next;
7187 free (p);
7190 /* Call here when we see the initializer is surrounded by braces.
7191 This is instead of a call to push_init_level;
7192 it is matched by a call to pop_init_level.
7194 TYPE is the type to initialize, for a constructor expression.
7195 For an initializer for a decl, TYPE is zero. */
7197 void
7198 really_start_incremental_init (tree type)
7200 struct constructor_stack *p = XNEW (struct constructor_stack);
7202 if (type == 0)
7203 type = TREE_TYPE (constructor_decl);
7205 if (TREE_CODE (type) == VECTOR_TYPE
7206 && TYPE_VECTOR_OPAQUE (type))
7207 error ("opaque vector types cannot be initialized");
7209 p->type = constructor_type;
7210 p->fields = constructor_fields;
7211 p->index = constructor_index;
7212 p->max_index = constructor_max_index;
7213 p->unfilled_index = constructor_unfilled_index;
7214 p->unfilled_fields = constructor_unfilled_fields;
7215 p->bit_index = constructor_bit_index;
7216 p->elements = constructor_elements;
7217 p->constant = constructor_constant;
7218 p->simple = constructor_simple;
7219 p->nonconst = constructor_nonconst;
7220 p->erroneous = constructor_erroneous;
7221 p->pending_elts = constructor_pending_elts;
7222 p->depth = constructor_depth;
7223 p->replacement_value.value = 0;
7224 p->replacement_value.original_code = ERROR_MARK;
7225 p->replacement_value.original_type = NULL;
7226 p->implicit = 0;
7227 p->range_stack = 0;
7228 p->outer = 0;
7229 p->incremental = constructor_incremental;
7230 p->designated = constructor_designated;
7231 p->designator_depth = designator_depth;
7232 p->next = 0;
7233 constructor_stack = p;
7235 constructor_constant = 1;
7236 constructor_simple = 1;
7237 constructor_nonconst = 0;
7238 constructor_depth = SPELLING_DEPTH ();
7239 constructor_elements = NULL;
7240 constructor_pending_elts = 0;
7241 constructor_type = type;
7242 constructor_incremental = 1;
7243 constructor_designated = 0;
7244 constructor_zeroinit = 1;
7245 designator_depth = 0;
7246 designator_erroneous = 0;
7248 if (TREE_CODE (constructor_type) == RECORD_TYPE
7249 || TREE_CODE (constructor_type) == UNION_TYPE)
7251 constructor_fields = TYPE_FIELDS (constructor_type);
7252 /* Skip any nameless bit fields at the beginning. */
7253 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7254 && DECL_NAME (constructor_fields) == 0)
7255 constructor_fields = DECL_CHAIN (constructor_fields);
7257 constructor_unfilled_fields = constructor_fields;
7258 constructor_bit_index = bitsize_zero_node;
7260 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7262 if (TYPE_DOMAIN (constructor_type))
7264 constructor_max_index
7265 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7267 /* Detect non-empty initializations of zero-length arrays. */
7268 if (constructor_max_index == NULL_TREE
7269 && TYPE_SIZE (constructor_type))
7270 constructor_max_index = integer_minus_one_node;
7272 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7273 to initialize VLAs will cause a proper error; avoid tree
7274 checking errors as well by setting a safe value. */
7275 if (constructor_max_index
7276 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7277 constructor_max_index = integer_minus_one_node;
7279 constructor_index
7280 = convert (bitsizetype,
7281 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7283 else
7285 constructor_index = bitsize_zero_node;
7286 constructor_max_index = NULL_TREE;
7289 constructor_unfilled_index = constructor_index;
7291 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7293 /* Vectors are like simple fixed-size arrays. */
7294 constructor_max_index =
7295 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7296 constructor_index = bitsize_zero_node;
7297 constructor_unfilled_index = constructor_index;
7299 else
7301 /* Handle the case of int x = {5}; */
7302 constructor_fields = constructor_type;
7303 constructor_unfilled_fields = constructor_type;
7307 /* Push down into a subobject, for initialization.
7308 If this is for an explicit set of braces, IMPLICIT is 0.
7309 If it is because the next element belongs at a lower level,
7310 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7312 void
7313 push_init_level (location_t loc, int implicit,
7314 struct obstack *braced_init_obstack)
7316 struct constructor_stack *p;
7317 tree value = NULL_TREE;
7319 /* If we've exhausted any levels that didn't have braces,
7320 pop them now. If implicit == 1, this will have been done in
7321 process_init_element; do not repeat it here because in the case
7322 of excess initializers for an empty aggregate this leads to an
7323 infinite cycle of popping a level and immediately recreating
7324 it. */
7325 if (implicit != 1)
7327 while (constructor_stack->implicit)
7329 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7330 || TREE_CODE (constructor_type) == UNION_TYPE)
7331 && constructor_fields == 0)
7332 process_init_element (input_location,
7333 pop_init_level (loc, 1, braced_init_obstack),
7334 true, braced_init_obstack);
7335 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7336 && constructor_max_index
7337 && tree_int_cst_lt (constructor_max_index,
7338 constructor_index))
7339 process_init_element (input_location,
7340 pop_init_level (loc, 1, braced_init_obstack),
7341 true, braced_init_obstack);
7342 else
7343 break;
7347 /* Unless this is an explicit brace, we need to preserve previous
7348 content if any. */
7349 if (implicit)
7351 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7352 || TREE_CODE (constructor_type) == UNION_TYPE)
7353 && constructor_fields)
7354 value = find_init_member (constructor_fields, braced_init_obstack);
7355 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7356 value = find_init_member (constructor_index, braced_init_obstack);
7359 p = XNEW (struct constructor_stack);
7360 p->type = constructor_type;
7361 p->fields = constructor_fields;
7362 p->index = constructor_index;
7363 p->max_index = constructor_max_index;
7364 p->unfilled_index = constructor_unfilled_index;
7365 p->unfilled_fields = constructor_unfilled_fields;
7366 p->bit_index = constructor_bit_index;
7367 p->elements = constructor_elements;
7368 p->constant = constructor_constant;
7369 p->simple = constructor_simple;
7370 p->nonconst = constructor_nonconst;
7371 p->erroneous = constructor_erroneous;
7372 p->pending_elts = constructor_pending_elts;
7373 p->depth = constructor_depth;
7374 p->replacement_value.value = 0;
7375 p->replacement_value.original_code = ERROR_MARK;
7376 p->replacement_value.original_type = NULL;
7377 p->implicit = implicit;
7378 p->outer = 0;
7379 p->incremental = constructor_incremental;
7380 p->designated = constructor_designated;
7381 p->designator_depth = designator_depth;
7382 p->next = constructor_stack;
7383 p->range_stack = 0;
7384 constructor_stack = p;
7386 constructor_constant = 1;
7387 constructor_simple = 1;
7388 constructor_nonconst = 0;
7389 constructor_depth = SPELLING_DEPTH ();
7390 constructor_elements = NULL;
7391 constructor_incremental = 1;
7392 constructor_designated = 0;
7393 constructor_pending_elts = 0;
7394 if (!implicit)
7396 p->range_stack = constructor_range_stack;
7397 constructor_range_stack = 0;
7398 designator_depth = 0;
7399 designator_erroneous = 0;
7402 /* Don't die if an entire brace-pair level is superfluous
7403 in the containing level. */
7404 if (constructor_type == 0)
7406 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7407 || TREE_CODE (constructor_type) == UNION_TYPE)
7409 /* Don't die if there are extra init elts at the end. */
7410 if (constructor_fields == 0)
7411 constructor_type = 0;
7412 else
7414 constructor_type = TREE_TYPE (constructor_fields);
7415 push_member_name (constructor_fields);
7416 constructor_depth++;
7418 /* If upper initializer is designated, then mark this as
7419 designated too to prevent bogus warnings. */
7420 constructor_designated = p->designated;
7422 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7424 constructor_type = TREE_TYPE (constructor_type);
7425 push_array_bounds (tree_to_uhwi (constructor_index));
7426 constructor_depth++;
7429 if (constructor_type == 0)
7431 error_init (loc, "extra brace group at end of initializer");
7432 constructor_fields = 0;
7433 constructor_unfilled_fields = 0;
7434 return;
7437 if (value && TREE_CODE (value) == CONSTRUCTOR)
7439 constructor_constant = TREE_CONSTANT (value);
7440 constructor_simple = TREE_STATIC (value);
7441 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7442 constructor_elements = CONSTRUCTOR_ELTS (value);
7443 if (!vec_safe_is_empty (constructor_elements)
7444 && (TREE_CODE (constructor_type) == RECORD_TYPE
7445 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7446 set_nonincremental_init (braced_init_obstack);
7449 if (implicit == 1)
7450 found_missing_braces = 1;
7452 if (TREE_CODE (constructor_type) == RECORD_TYPE
7453 || TREE_CODE (constructor_type) == UNION_TYPE)
7455 constructor_fields = TYPE_FIELDS (constructor_type);
7456 /* Skip any nameless bit fields at the beginning. */
7457 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7458 && DECL_NAME (constructor_fields) == 0)
7459 constructor_fields = DECL_CHAIN (constructor_fields);
7461 constructor_unfilled_fields = constructor_fields;
7462 constructor_bit_index = bitsize_zero_node;
7464 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7466 /* Vectors are like simple fixed-size arrays. */
7467 constructor_max_index =
7468 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7469 constructor_index = bitsize_int (0);
7470 constructor_unfilled_index = constructor_index;
7472 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7474 if (TYPE_DOMAIN (constructor_type))
7476 constructor_max_index
7477 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7479 /* Detect non-empty initializations of zero-length arrays. */
7480 if (constructor_max_index == NULL_TREE
7481 && TYPE_SIZE (constructor_type))
7482 constructor_max_index = integer_minus_one_node;
7484 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7485 to initialize VLAs will cause a proper error; avoid tree
7486 checking errors as well by setting a safe value. */
7487 if (constructor_max_index
7488 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7489 constructor_max_index = integer_minus_one_node;
7491 constructor_index
7492 = convert (bitsizetype,
7493 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7495 else
7496 constructor_index = bitsize_zero_node;
7498 constructor_unfilled_index = constructor_index;
7499 if (value && TREE_CODE (value) == STRING_CST)
7501 /* We need to split the char/wchar array into individual
7502 characters, so that we don't have to special case it
7503 everywhere. */
7504 set_nonincremental_init_from_string (value, braced_init_obstack);
7507 else
7509 if (constructor_type != error_mark_node)
7510 warning_init (input_location, 0, "braces around scalar initializer");
7511 constructor_fields = constructor_type;
7512 constructor_unfilled_fields = constructor_type;
7516 /* At the end of an implicit or explicit brace level,
7517 finish up that level of constructor. If a single expression
7518 with redundant braces initialized that level, return the
7519 c_expr structure for that expression. Otherwise, the original_code
7520 element is set to ERROR_MARK.
7521 If we were outputting the elements as they are read, return 0 as the value
7522 from inner levels (process_init_element ignores that),
7523 but return error_mark_node as the value from the outermost level
7524 (that's what we want to put in DECL_INITIAL).
7525 Otherwise, return a CONSTRUCTOR expression as the value. */
7527 struct c_expr
7528 pop_init_level (location_t loc, int implicit,
7529 struct obstack *braced_init_obstack)
7531 struct constructor_stack *p;
7532 struct c_expr ret;
7533 ret.value = 0;
7534 ret.original_code = ERROR_MARK;
7535 ret.original_type = NULL;
7537 if (implicit == 0)
7539 /* When we come to an explicit close brace,
7540 pop any inner levels that didn't have explicit braces. */
7541 while (constructor_stack->implicit)
7542 process_init_element (input_location,
7543 pop_init_level (loc, 1, braced_init_obstack),
7544 true, braced_init_obstack);
7545 gcc_assert (!constructor_range_stack);
7548 /* Now output all pending elements. */
7549 constructor_incremental = 1;
7550 output_pending_init_elements (1, braced_init_obstack);
7552 p = constructor_stack;
7554 /* Error for initializing a flexible array member, or a zero-length
7555 array member in an inappropriate context. */
7556 if (constructor_type && constructor_fields
7557 && TREE_CODE (constructor_type) == ARRAY_TYPE
7558 && TYPE_DOMAIN (constructor_type)
7559 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7561 /* Silently discard empty initializations. The parser will
7562 already have pedwarned for empty brackets. */
7563 if (integer_zerop (constructor_unfilled_index))
7564 constructor_type = NULL_TREE;
7565 else
7567 gcc_assert (!TYPE_SIZE (constructor_type));
7569 if (constructor_depth > 2)
7570 error_init (loc, "initialization of flexible array member in a nested context");
7571 else
7572 pedwarn_init (loc, OPT_Wpedantic,
7573 "initialization of a flexible array member");
7575 /* We have already issued an error message for the existence
7576 of a flexible array member not at the end of the structure.
7577 Discard the initializer so that we do not die later. */
7578 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7579 constructor_type = NULL_TREE;
7583 switch (vec_safe_length (constructor_elements))
7585 case 0:
7586 /* Initialization with { } counts as zeroinit. */
7587 constructor_zeroinit = 1;
7588 break;
7589 case 1:
7590 /* This might be zeroinit as well. */
7591 if (integer_zerop ((*constructor_elements)[0].value))
7592 constructor_zeroinit = 1;
7593 break;
7594 default:
7595 /* If the constructor has more than one element, it can't be { 0 }. */
7596 constructor_zeroinit = 0;
7597 break;
7600 /* Warn when some structs are initialized with direct aggregation. */
7601 if (!implicit && found_missing_braces && warn_missing_braces
7602 && !constructor_zeroinit)
7603 warning_init (loc, OPT_Wmissing_braces,
7604 "missing braces around initializer");
7606 /* Warn when some struct elements are implicitly initialized to zero. */
7607 if (warn_missing_field_initializers
7608 && constructor_type
7609 && TREE_CODE (constructor_type) == RECORD_TYPE
7610 && constructor_unfilled_fields)
7612 /* Do not warn for flexible array members or zero-length arrays. */
7613 while (constructor_unfilled_fields
7614 && (!DECL_SIZE (constructor_unfilled_fields)
7615 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7616 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7618 if (constructor_unfilled_fields
7619 /* Do not warn if this level of the initializer uses member
7620 designators; it is likely to be deliberate. */
7621 && !constructor_designated
7622 /* Do not warn about initializing with { 0 } or with { }. */
7623 && !constructor_zeroinit)
7625 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7626 "missing initializer for field %qD of %qT",
7627 constructor_unfilled_fields,
7628 constructor_type))
7629 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7630 "%qD declared here", constructor_unfilled_fields);
7634 /* Pad out the end of the structure. */
7635 if (p->replacement_value.value)
7636 /* If this closes a superfluous brace pair,
7637 just pass out the element between them. */
7638 ret = p->replacement_value;
7639 else if (constructor_type == 0)
7641 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7642 && TREE_CODE (constructor_type) != UNION_TYPE
7643 && TREE_CODE (constructor_type) != ARRAY_TYPE
7644 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7646 /* A nonincremental scalar initializer--just return
7647 the element, after verifying there is just one. */
7648 if (vec_safe_is_empty (constructor_elements))
7650 if (!constructor_erroneous)
7651 error_init (loc, "empty scalar initializer");
7652 ret.value = error_mark_node;
7654 else if (vec_safe_length (constructor_elements) != 1)
7656 error_init (loc, "extra elements in scalar initializer");
7657 ret.value = (*constructor_elements)[0].value;
7659 else
7660 ret.value = (*constructor_elements)[0].value;
7662 else
7664 if (constructor_erroneous)
7665 ret.value = error_mark_node;
7666 else
7668 ret.value = build_constructor (constructor_type,
7669 constructor_elements);
7670 if (constructor_constant)
7671 TREE_CONSTANT (ret.value) = 1;
7672 if (constructor_constant && constructor_simple)
7673 TREE_STATIC (ret.value) = 1;
7674 if (constructor_nonconst)
7675 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7679 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7681 if (constructor_nonconst)
7682 ret.original_code = C_MAYBE_CONST_EXPR;
7683 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7684 ret.original_code = ERROR_MARK;
7687 constructor_type = p->type;
7688 constructor_fields = p->fields;
7689 constructor_index = p->index;
7690 constructor_max_index = p->max_index;
7691 constructor_unfilled_index = p->unfilled_index;
7692 constructor_unfilled_fields = p->unfilled_fields;
7693 constructor_bit_index = p->bit_index;
7694 constructor_elements = p->elements;
7695 constructor_constant = p->constant;
7696 constructor_simple = p->simple;
7697 constructor_nonconst = p->nonconst;
7698 constructor_erroneous = p->erroneous;
7699 constructor_incremental = p->incremental;
7700 constructor_designated = p->designated;
7701 designator_depth = p->designator_depth;
7702 constructor_pending_elts = p->pending_elts;
7703 constructor_depth = p->depth;
7704 if (!p->implicit)
7705 constructor_range_stack = p->range_stack;
7706 RESTORE_SPELLING_DEPTH (constructor_depth);
7708 constructor_stack = p->next;
7709 free (p);
7711 if (ret.value == 0 && constructor_stack == 0)
7712 ret.value = error_mark_node;
7713 return ret;
7716 /* Common handling for both array range and field name designators.
7717 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7719 static int
7720 set_designator (location_t loc, int array,
7721 struct obstack *braced_init_obstack)
7723 tree subtype;
7724 enum tree_code subcode;
7726 /* Don't die if an entire brace-pair level is superfluous
7727 in the containing level. */
7728 if (constructor_type == 0)
7729 return 1;
7731 /* If there were errors in this designator list already, bail out
7732 silently. */
7733 if (designator_erroneous)
7734 return 1;
7736 if (!designator_depth)
7738 gcc_assert (!constructor_range_stack);
7740 /* Designator list starts at the level of closest explicit
7741 braces. */
7742 while (constructor_stack->implicit)
7743 process_init_element (input_location,
7744 pop_init_level (loc, 1, braced_init_obstack),
7745 true, braced_init_obstack);
7746 constructor_designated = 1;
7747 return 0;
7750 switch (TREE_CODE (constructor_type))
7752 case RECORD_TYPE:
7753 case UNION_TYPE:
7754 subtype = TREE_TYPE (constructor_fields);
7755 if (subtype != error_mark_node)
7756 subtype = TYPE_MAIN_VARIANT (subtype);
7757 break;
7758 case ARRAY_TYPE:
7759 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7760 break;
7761 default:
7762 gcc_unreachable ();
7765 subcode = TREE_CODE (subtype);
7766 if (array && subcode != ARRAY_TYPE)
7768 error_init (loc, "array index in non-array initializer");
7769 return 1;
7771 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7773 error_init (loc, "field name not in record or union initializer");
7774 return 1;
7777 constructor_designated = 1;
7778 push_init_level (loc, 2, braced_init_obstack);
7779 return 0;
7782 /* If there are range designators in designator list, push a new designator
7783 to constructor_range_stack. RANGE_END is end of such stack range or
7784 NULL_TREE if there is no range designator at this level. */
7786 static void
7787 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7789 struct constructor_range_stack *p;
7791 p = (struct constructor_range_stack *)
7792 obstack_alloc (braced_init_obstack,
7793 sizeof (struct constructor_range_stack));
7794 p->prev = constructor_range_stack;
7795 p->next = 0;
7796 p->fields = constructor_fields;
7797 p->range_start = constructor_index;
7798 p->index = constructor_index;
7799 p->stack = constructor_stack;
7800 p->range_end = range_end;
7801 if (constructor_range_stack)
7802 constructor_range_stack->next = p;
7803 constructor_range_stack = p;
7806 /* Within an array initializer, specify the next index to be initialized.
7807 FIRST is that index. If LAST is nonzero, then initialize a range
7808 of indices, running from FIRST through LAST. */
7810 void
7811 set_init_index (location_t loc, tree first, tree last,
7812 struct obstack *braced_init_obstack)
7814 if (set_designator (loc, 1, braced_init_obstack))
7815 return;
7817 designator_erroneous = 1;
7819 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7820 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7822 error_init (loc, "array index in initializer not of integer type");
7823 return;
7826 if (TREE_CODE (first) != INTEGER_CST)
7828 first = c_fully_fold (first, false, NULL);
7829 if (TREE_CODE (first) == INTEGER_CST)
7830 pedwarn_init (loc, OPT_Wpedantic,
7831 "array index in initializer is not "
7832 "an integer constant expression");
7835 if (last && TREE_CODE (last) != INTEGER_CST)
7837 last = c_fully_fold (last, false, NULL);
7838 if (TREE_CODE (last) == INTEGER_CST)
7839 pedwarn_init (loc, OPT_Wpedantic,
7840 "array index in initializer is not "
7841 "an integer constant expression");
7844 if (TREE_CODE (first) != INTEGER_CST)
7845 error_init (loc, "nonconstant array index in initializer");
7846 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7847 error_init (loc, "nonconstant array index in initializer");
7848 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7849 error_init (loc, "array index in non-array initializer");
7850 else if (tree_int_cst_sgn (first) == -1)
7851 error_init (loc, "array index in initializer exceeds array bounds");
7852 else if (constructor_max_index
7853 && tree_int_cst_lt (constructor_max_index, first))
7854 error_init (loc, "array index in initializer exceeds array bounds");
7855 else
7857 constant_expression_warning (first);
7858 if (last)
7859 constant_expression_warning (last);
7860 constructor_index = convert (bitsizetype, first);
7861 if (tree_int_cst_lt (constructor_index, first))
7863 constructor_index = copy_node (constructor_index);
7864 TREE_OVERFLOW (constructor_index) = 1;
7867 if (last)
7869 if (tree_int_cst_equal (first, last))
7870 last = 0;
7871 else if (tree_int_cst_lt (last, first))
7873 error_init (loc, "empty index range in initializer");
7874 last = 0;
7876 else
7878 last = convert (bitsizetype, last);
7879 if (constructor_max_index != 0
7880 && tree_int_cst_lt (constructor_max_index, last))
7882 error_init (loc, "array index range in initializer exceeds "
7883 "array bounds");
7884 last = 0;
7889 designator_depth++;
7890 designator_erroneous = 0;
7891 if (constructor_range_stack || last)
7892 push_range_stack (last, braced_init_obstack);
7896 /* Within a struct initializer, specify the next field to be initialized. */
7898 void
7899 set_init_label (location_t loc, tree fieldname,
7900 struct obstack *braced_init_obstack)
7902 tree field;
7904 if (set_designator (loc, 0, braced_init_obstack))
7905 return;
7907 designator_erroneous = 1;
7909 if (TREE_CODE (constructor_type) != RECORD_TYPE
7910 && TREE_CODE (constructor_type) != UNION_TYPE)
7912 error_init (loc, "field name not in record or union initializer");
7913 return;
7916 field = lookup_field (constructor_type, fieldname);
7918 if (field == 0)
7919 error_at (loc, "unknown field %qE specified in initializer", fieldname);
7920 else
7923 constructor_fields = TREE_VALUE (field);
7924 designator_depth++;
7925 designator_erroneous = 0;
7926 if (constructor_range_stack)
7927 push_range_stack (NULL_TREE, braced_init_obstack);
7928 field = TREE_CHAIN (field);
7929 if (field)
7931 if (set_designator (loc, 0, braced_init_obstack))
7932 return;
7935 while (field != NULL_TREE);
7938 /* Add a new initializer to the tree of pending initializers. PURPOSE
7939 identifies the initializer, either array index or field in a structure.
7940 VALUE is the value of that index or field. If ORIGTYPE is not
7941 NULL_TREE, it is the original type of VALUE.
7943 IMPLICIT is true if value comes from pop_init_level (1),
7944 the new initializer has been merged with the existing one
7945 and thus no warnings should be emitted about overriding an
7946 existing initializer. */
7948 static void
7949 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7950 bool implicit, struct obstack *braced_init_obstack)
7952 struct init_node *p, **q, *r;
7954 q = &constructor_pending_elts;
7955 p = 0;
7957 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7959 while (*q != 0)
7961 p = *q;
7962 if (tree_int_cst_lt (purpose, p->purpose))
7963 q = &p->left;
7964 else if (tree_int_cst_lt (p->purpose, purpose))
7965 q = &p->right;
7966 else
7968 if (!implicit)
7970 if (TREE_SIDE_EFFECTS (p->value))
7971 warning_init (loc, OPT_Woverride_init_side_effects,
7972 "initialized field with side-effects "
7973 "overwritten");
7974 else if (warn_override_init)
7975 warning_init (loc, OPT_Woverride_init,
7976 "initialized field overwritten");
7978 p->value = value;
7979 p->origtype = origtype;
7980 return;
7984 else
7986 tree bitpos;
7988 bitpos = bit_position (purpose);
7989 while (*q != NULL)
7991 p = *q;
7992 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7993 q = &p->left;
7994 else if (p->purpose != purpose)
7995 q = &p->right;
7996 else
7998 if (!implicit)
8000 if (TREE_SIDE_EFFECTS (p->value))
8001 warning_init (loc, OPT_Woverride_init_side_effects,
8002 "initialized field with side-effects "
8003 "overwritten");
8004 else if (warn_override_init)
8005 warning_init (loc, OPT_Woverride_init,
8006 "initialized field overwritten");
8008 p->value = value;
8009 p->origtype = origtype;
8010 return;
8015 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8016 sizeof (struct init_node));
8017 r->purpose = purpose;
8018 r->value = value;
8019 r->origtype = origtype;
8021 *q = r;
8022 r->parent = p;
8023 r->left = 0;
8024 r->right = 0;
8025 r->balance = 0;
8027 while (p)
8029 struct init_node *s;
8031 if (r == p->left)
8033 if (p->balance == 0)
8034 p->balance = -1;
8035 else if (p->balance < 0)
8037 if (r->balance < 0)
8039 /* L rotation. */
8040 p->left = r->right;
8041 if (p->left)
8042 p->left->parent = p;
8043 r->right = p;
8045 p->balance = 0;
8046 r->balance = 0;
8048 s = p->parent;
8049 p->parent = r;
8050 r->parent = s;
8051 if (s)
8053 if (s->left == p)
8054 s->left = r;
8055 else
8056 s->right = r;
8058 else
8059 constructor_pending_elts = r;
8061 else
8063 /* LR rotation. */
8064 struct init_node *t = r->right;
8066 r->right = t->left;
8067 if (r->right)
8068 r->right->parent = r;
8069 t->left = r;
8071 p->left = t->right;
8072 if (p->left)
8073 p->left->parent = p;
8074 t->right = p;
8076 p->balance = t->balance < 0;
8077 r->balance = -(t->balance > 0);
8078 t->balance = 0;
8080 s = p->parent;
8081 p->parent = t;
8082 r->parent = t;
8083 t->parent = s;
8084 if (s)
8086 if (s->left == p)
8087 s->left = t;
8088 else
8089 s->right = t;
8091 else
8092 constructor_pending_elts = t;
8094 break;
8096 else
8098 /* p->balance == +1; growth of left side balances the node. */
8099 p->balance = 0;
8100 break;
8103 else /* r == p->right */
8105 if (p->balance == 0)
8106 /* Growth propagation from right side. */
8107 p->balance++;
8108 else if (p->balance > 0)
8110 if (r->balance > 0)
8112 /* R rotation. */
8113 p->right = r->left;
8114 if (p->right)
8115 p->right->parent = p;
8116 r->left = p;
8118 p->balance = 0;
8119 r->balance = 0;
8121 s = p->parent;
8122 p->parent = r;
8123 r->parent = s;
8124 if (s)
8126 if (s->left == p)
8127 s->left = r;
8128 else
8129 s->right = r;
8131 else
8132 constructor_pending_elts = r;
8134 else /* r->balance == -1 */
8136 /* RL rotation */
8137 struct init_node *t = r->left;
8139 r->left = t->right;
8140 if (r->left)
8141 r->left->parent = r;
8142 t->right = r;
8144 p->right = t->left;
8145 if (p->right)
8146 p->right->parent = p;
8147 t->left = p;
8149 r->balance = (t->balance < 0);
8150 p->balance = -(t->balance > 0);
8151 t->balance = 0;
8153 s = p->parent;
8154 p->parent = t;
8155 r->parent = t;
8156 t->parent = s;
8157 if (s)
8159 if (s->left == p)
8160 s->left = t;
8161 else
8162 s->right = t;
8164 else
8165 constructor_pending_elts = t;
8167 break;
8169 else
8171 /* p->balance == -1; growth of right side balances the node. */
8172 p->balance = 0;
8173 break;
8177 r = p;
8178 p = p->parent;
8182 /* Build AVL tree from a sorted chain. */
8184 static void
8185 set_nonincremental_init (struct obstack * braced_init_obstack)
8187 unsigned HOST_WIDE_INT ix;
8188 tree index, value;
8190 if (TREE_CODE (constructor_type) != RECORD_TYPE
8191 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8192 return;
8194 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8195 add_pending_init (input_location, index, value, NULL_TREE, true,
8196 braced_init_obstack);
8197 constructor_elements = NULL;
8198 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8200 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8201 /* Skip any nameless bit fields at the beginning. */
8202 while (constructor_unfilled_fields != 0
8203 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8204 && DECL_NAME (constructor_unfilled_fields) == 0)
8205 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8208 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8210 if (TYPE_DOMAIN (constructor_type))
8211 constructor_unfilled_index
8212 = convert (bitsizetype,
8213 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8214 else
8215 constructor_unfilled_index = bitsize_zero_node;
8217 constructor_incremental = 0;
8220 /* Build AVL tree from a string constant. */
8222 static void
8223 set_nonincremental_init_from_string (tree str,
8224 struct obstack * braced_init_obstack)
8226 tree value, purpose, type;
8227 HOST_WIDE_INT val[2];
8228 const char *p, *end;
8229 int byte, wchar_bytes, charwidth, bitpos;
8231 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8233 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8234 charwidth = TYPE_PRECISION (char_type_node);
8235 type = TREE_TYPE (constructor_type);
8236 p = TREE_STRING_POINTER (str);
8237 end = p + TREE_STRING_LENGTH (str);
8239 for (purpose = bitsize_zero_node;
8240 p < end
8241 && !(constructor_max_index
8242 && tree_int_cst_lt (constructor_max_index, purpose));
8243 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8245 if (wchar_bytes == 1)
8247 val[0] = (unsigned char) *p++;
8248 val[1] = 0;
8250 else
8252 val[1] = 0;
8253 val[0] = 0;
8254 for (byte = 0; byte < wchar_bytes; byte++)
8256 if (BYTES_BIG_ENDIAN)
8257 bitpos = (wchar_bytes - byte - 1) * charwidth;
8258 else
8259 bitpos = byte * charwidth;
8260 val[bitpos % HOST_BITS_PER_WIDE_INT]
8261 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8262 << (bitpos % HOST_BITS_PER_WIDE_INT);
8266 if (!TYPE_UNSIGNED (type))
8268 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8269 if (bitpos < HOST_BITS_PER_WIDE_INT)
8271 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8273 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8274 val[1] = -1;
8277 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8279 if (val[0] < 0)
8280 val[1] = -1;
8282 else if (val[1] & (((HOST_WIDE_INT) 1)
8283 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8284 val[1] |= ((HOST_WIDE_INT) -1)
8285 << (bitpos - HOST_BITS_PER_WIDE_INT);
8288 value = wide_int_to_tree (type,
8289 wide_int::from_array (val, 2,
8290 HOST_BITS_PER_WIDE_INT * 2));
8291 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8292 braced_init_obstack);
8295 constructor_incremental = 0;
8298 /* Return value of FIELD in pending initializer or zero if the field was
8299 not initialized yet. */
8301 static tree
8302 find_init_member (tree field, struct obstack * braced_init_obstack)
8304 struct init_node *p;
8306 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8308 if (constructor_incremental
8309 && tree_int_cst_lt (field, constructor_unfilled_index))
8310 set_nonincremental_init (braced_init_obstack);
8312 p = constructor_pending_elts;
8313 while (p)
8315 if (tree_int_cst_lt (field, p->purpose))
8316 p = p->left;
8317 else if (tree_int_cst_lt (p->purpose, field))
8318 p = p->right;
8319 else
8320 return p->value;
8323 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8325 tree bitpos = bit_position (field);
8327 if (constructor_incremental
8328 && (!constructor_unfilled_fields
8329 || tree_int_cst_lt (bitpos,
8330 bit_position (constructor_unfilled_fields))))
8331 set_nonincremental_init (braced_init_obstack);
8333 p = constructor_pending_elts;
8334 while (p)
8336 if (field == p->purpose)
8337 return p->value;
8338 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8339 p = p->left;
8340 else
8341 p = p->right;
8344 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8346 if (!vec_safe_is_empty (constructor_elements)
8347 && (constructor_elements->last ().index == field))
8348 return constructor_elements->last ().value;
8350 return 0;
8353 /* "Output" the next constructor element.
8354 At top level, really output it to assembler code now.
8355 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8356 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8357 TYPE is the data type that the containing data type wants here.
8358 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8359 If VALUE is a string constant, STRICT_STRING is true if it is
8360 unparenthesized or we should not warn here for it being parenthesized.
8361 For other types of VALUE, STRICT_STRING is not used.
8363 PENDING if non-nil means output pending elements that belong
8364 right after this element. (PENDING is normally 1;
8365 it is 0 while outputting pending elements, to avoid recursion.)
8367 IMPLICIT is true if value comes from pop_init_level (1),
8368 the new initializer has been merged with the existing one
8369 and thus no warnings should be emitted about overriding an
8370 existing initializer. */
8372 static void
8373 output_init_element (location_t loc, tree value, tree origtype,
8374 bool strict_string, tree type, tree field, int pending,
8375 bool implicit, struct obstack * braced_init_obstack)
8377 tree semantic_type = NULL_TREE;
8378 bool maybe_const = true;
8379 bool npc;
8381 if (type == error_mark_node || value == error_mark_node)
8383 constructor_erroneous = 1;
8384 return;
8386 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8387 && (TREE_CODE (value) == STRING_CST
8388 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8389 && !(TREE_CODE (value) == STRING_CST
8390 && TREE_CODE (type) == ARRAY_TYPE
8391 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8392 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8393 TYPE_MAIN_VARIANT (type)))
8394 value = array_to_pointer_conversion (input_location, value);
8396 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8397 && require_constant_value && pending)
8399 /* As an extension, allow initializing objects with static storage
8400 duration with compound literals (which are then treated just as
8401 the brace enclosed list they contain). */
8402 if (flag_isoc99)
8403 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8404 "constant");
8405 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8406 value = DECL_INITIAL (decl);
8409 npc = null_pointer_constant_p (value);
8410 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8412 semantic_type = TREE_TYPE (value);
8413 value = TREE_OPERAND (value, 0);
8415 value = c_fully_fold (value, require_constant_value, &maybe_const);
8417 if (value == error_mark_node)
8418 constructor_erroneous = 1;
8419 else if (!TREE_CONSTANT (value))
8420 constructor_constant = 0;
8421 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8422 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8423 || TREE_CODE (constructor_type) == UNION_TYPE)
8424 && DECL_C_BIT_FIELD (field)
8425 && TREE_CODE (value) != INTEGER_CST))
8426 constructor_simple = 0;
8427 if (!maybe_const)
8428 constructor_nonconst = 1;
8430 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8432 if (require_constant_value)
8434 error_init (loc, "initializer element is not constant");
8435 value = error_mark_node;
8437 else if (require_constant_elements)
8438 pedwarn (loc, OPT_Wpedantic,
8439 "initializer element is not computable at load time");
8441 else if (!maybe_const
8442 && (require_constant_value || require_constant_elements))
8443 pedwarn_init (loc, OPT_Wpedantic,
8444 "initializer element is not a constant expression");
8446 /* Issue -Wc++-compat warnings about initializing a bitfield with
8447 enum type. */
8448 if (warn_cxx_compat
8449 && field != NULL_TREE
8450 && TREE_CODE (field) == FIELD_DECL
8451 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8452 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8453 != TYPE_MAIN_VARIANT (type))
8454 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8456 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8457 if (checktype != error_mark_node
8458 && (TYPE_MAIN_VARIANT (checktype)
8459 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8460 warning_init (loc, OPT_Wc___compat,
8461 "enum conversion in initialization is invalid in C++");
8464 /* If this field is empty (and not at the end of structure),
8465 don't do anything other than checking the initializer. */
8466 if (field
8467 && (TREE_TYPE (field) == error_mark_node
8468 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8469 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8470 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8471 || DECL_CHAIN (field)))))
8472 return;
8474 if (semantic_type)
8475 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8476 value = digest_init (loc, type, value, origtype, npc, strict_string,
8477 require_constant_value);
8478 if (value == error_mark_node)
8480 constructor_erroneous = 1;
8481 return;
8483 if (require_constant_value || require_constant_elements)
8484 constant_expression_warning (value);
8486 /* If this element doesn't come next in sequence,
8487 put it on constructor_pending_elts. */
8488 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8489 && (!constructor_incremental
8490 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8492 if (constructor_incremental
8493 && tree_int_cst_lt (field, constructor_unfilled_index))
8494 set_nonincremental_init (braced_init_obstack);
8496 add_pending_init (loc, field, value, origtype, implicit,
8497 braced_init_obstack);
8498 return;
8500 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8501 && (!constructor_incremental
8502 || field != constructor_unfilled_fields))
8504 /* We do this for records but not for unions. In a union,
8505 no matter which field is specified, it can be initialized
8506 right away since it starts at the beginning of the union. */
8507 if (constructor_incremental)
8509 if (!constructor_unfilled_fields)
8510 set_nonincremental_init (braced_init_obstack);
8511 else
8513 tree bitpos, unfillpos;
8515 bitpos = bit_position (field);
8516 unfillpos = bit_position (constructor_unfilled_fields);
8518 if (tree_int_cst_lt (bitpos, unfillpos))
8519 set_nonincremental_init (braced_init_obstack);
8523 add_pending_init (loc, field, value, origtype, implicit,
8524 braced_init_obstack);
8525 return;
8527 else if (TREE_CODE (constructor_type) == UNION_TYPE
8528 && !vec_safe_is_empty (constructor_elements))
8530 if (!implicit)
8532 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8533 warning_init (loc, OPT_Woverride_init_side_effects,
8534 "initialized field with side-effects overwritten");
8535 else if (warn_override_init)
8536 warning_init (loc, OPT_Woverride_init,
8537 "initialized field overwritten");
8540 /* We can have just one union field set. */
8541 constructor_elements = NULL;
8544 /* Otherwise, output this element either to
8545 constructor_elements or to the assembler file. */
8547 constructor_elt celt = {field, value};
8548 vec_safe_push (constructor_elements, celt);
8550 /* Advance the variable that indicates sequential elements output. */
8551 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8552 constructor_unfilled_index
8553 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8554 bitsize_one_node);
8555 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8557 constructor_unfilled_fields
8558 = DECL_CHAIN (constructor_unfilled_fields);
8560 /* Skip any nameless bit fields. */
8561 while (constructor_unfilled_fields != 0
8562 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8563 && DECL_NAME (constructor_unfilled_fields) == 0)
8564 constructor_unfilled_fields =
8565 DECL_CHAIN (constructor_unfilled_fields);
8567 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8568 constructor_unfilled_fields = 0;
8570 /* Now output any pending elements which have become next. */
8571 if (pending)
8572 output_pending_init_elements (0, braced_init_obstack);
8575 /* Output any pending elements which have become next.
8576 As we output elements, constructor_unfilled_{fields,index}
8577 advances, which may cause other elements to become next;
8578 if so, they too are output.
8580 If ALL is 0, we return when there are
8581 no more pending elements to output now.
8583 If ALL is 1, we output space as necessary so that
8584 we can output all the pending elements. */
8585 static void
8586 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8588 struct init_node *elt = constructor_pending_elts;
8589 tree next;
8591 retry:
8593 /* Look through the whole pending tree.
8594 If we find an element that should be output now,
8595 output it. Otherwise, set NEXT to the element
8596 that comes first among those still pending. */
8598 next = 0;
8599 while (elt)
8601 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8603 if (tree_int_cst_equal (elt->purpose,
8604 constructor_unfilled_index))
8605 output_init_element (input_location, elt->value, elt->origtype,
8606 true, TREE_TYPE (constructor_type),
8607 constructor_unfilled_index, 0, false,
8608 braced_init_obstack);
8609 else if (tree_int_cst_lt (constructor_unfilled_index,
8610 elt->purpose))
8612 /* Advance to the next smaller node. */
8613 if (elt->left)
8614 elt = elt->left;
8615 else
8617 /* We have reached the smallest node bigger than the
8618 current unfilled index. Fill the space first. */
8619 next = elt->purpose;
8620 break;
8623 else
8625 /* Advance to the next bigger node. */
8626 if (elt->right)
8627 elt = elt->right;
8628 else
8630 /* We have reached the biggest node in a subtree. Find
8631 the parent of it, which is the next bigger node. */
8632 while (elt->parent && elt->parent->right == elt)
8633 elt = elt->parent;
8634 elt = elt->parent;
8635 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8636 elt->purpose))
8638 next = elt->purpose;
8639 break;
8644 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8645 || TREE_CODE (constructor_type) == UNION_TYPE)
8647 tree ctor_unfilled_bitpos, elt_bitpos;
8649 /* If the current record is complete we are done. */
8650 if (constructor_unfilled_fields == 0)
8651 break;
8653 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8654 elt_bitpos = bit_position (elt->purpose);
8655 /* We can't compare fields here because there might be empty
8656 fields in between. */
8657 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8659 constructor_unfilled_fields = elt->purpose;
8660 output_init_element (input_location, elt->value, elt->origtype,
8661 true, TREE_TYPE (elt->purpose),
8662 elt->purpose, 0, false,
8663 braced_init_obstack);
8665 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8667 /* Advance to the next smaller node. */
8668 if (elt->left)
8669 elt = elt->left;
8670 else
8672 /* We have reached the smallest node bigger than the
8673 current unfilled field. Fill the space first. */
8674 next = elt->purpose;
8675 break;
8678 else
8680 /* Advance to the next bigger node. */
8681 if (elt->right)
8682 elt = elt->right;
8683 else
8685 /* We have reached the biggest node in a subtree. Find
8686 the parent of it, which is the next bigger node. */
8687 while (elt->parent && elt->parent->right == elt)
8688 elt = elt->parent;
8689 elt = elt->parent;
8690 if (elt
8691 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8692 bit_position (elt->purpose))))
8694 next = elt->purpose;
8695 break;
8702 /* Ordinarily return, but not if we want to output all
8703 and there are elements left. */
8704 if (!(all && next != 0))
8705 return;
8707 /* If it's not incremental, just skip over the gap, so that after
8708 jumping to retry we will output the next successive element. */
8709 if (TREE_CODE (constructor_type) == RECORD_TYPE
8710 || TREE_CODE (constructor_type) == UNION_TYPE)
8711 constructor_unfilled_fields = next;
8712 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8713 constructor_unfilled_index = next;
8715 /* ELT now points to the node in the pending tree with the next
8716 initializer to output. */
8717 goto retry;
8720 /* Add one non-braced element to the current constructor level.
8721 This adjusts the current position within the constructor's type.
8722 This may also start or terminate implicit levels
8723 to handle a partly-braced initializer.
8725 Once this has found the correct level for the new element,
8726 it calls output_init_element.
8728 IMPLICIT is true if value comes from pop_init_level (1),
8729 the new initializer has been merged with the existing one
8730 and thus no warnings should be emitted about overriding an
8731 existing initializer. */
8733 void
8734 process_init_element (location_t loc, struct c_expr value, bool implicit,
8735 struct obstack * braced_init_obstack)
8737 tree orig_value = value.value;
8738 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8739 bool strict_string = value.original_code == STRING_CST;
8740 bool was_designated = designator_depth != 0;
8742 designator_depth = 0;
8743 designator_erroneous = 0;
8745 if (!implicit && value.value && !integer_zerop (value.value))
8746 constructor_zeroinit = 0;
8748 /* Handle superfluous braces around string cst as in
8749 char x[] = {"foo"}; */
8750 if (string_flag
8751 && constructor_type
8752 && !was_designated
8753 && TREE_CODE (constructor_type) == ARRAY_TYPE
8754 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8755 && integer_zerop (constructor_unfilled_index))
8757 if (constructor_stack->replacement_value.value)
8758 error_init (loc, "excess elements in char array initializer");
8759 constructor_stack->replacement_value = value;
8760 return;
8763 if (constructor_stack->replacement_value.value != 0)
8765 error_init (loc, "excess elements in struct initializer");
8766 return;
8769 /* Ignore elements of a brace group if it is entirely superfluous
8770 and has already been diagnosed. */
8771 if (constructor_type == 0)
8772 return;
8774 if (!implicit && warn_designated_init && !was_designated
8775 && TREE_CODE (constructor_type) == RECORD_TYPE
8776 && lookup_attribute ("designated_init",
8777 TYPE_ATTRIBUTES (constructor_type)))
8778 warning_init (loc,
8779 OPT_Wdesignated_init,
8780 "positional initialization of field "
8781 "in %<struct%> declared with %<designated_init%> attribute");
8783 /* If we've exhausted any levels that didn't have braces,
8784 pop them now. */
8785 while (constructor_stack->implicit)
8787 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8788 || TREE_CODE (constructor_type) == UNION_TYPE)
8789 && constructor_fields == 0)
8790 process_init_element (loc,
8791 pop_init_level (loc, 1, braced_init_obstack),
8792 true, braced_init_obstack);
8793 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8794 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8795 && constructor_max_index
8796 && tree_int_cst_lt (constructor_max_index,
8797 constructor_index))
8798 process_init_element (loc,
8799 pop_init_level (loc, 1, braced_init_obstack),
8800 true, braced_init_obstack);
8801 else
8802 break;
8805 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8806 if (constructor_range_stack)
8808 /* If value is a compound literal and we'll be just using its
8809 content, don't put it into a SAVE_EXPR. */
8810 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8811 || !require_constant_value)
8813 tree semantic_type = NULL_TREE;
8814 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8816 semantic_type = TREE_TYPE (value.value);
8817 value.value = TREE_OPERAND (value.value, 0);
8819 value.value = c_save_expr (value.value);
8820 if (semantic_type)
8821 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8822 value.value);
8826 while (1)
8828 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8830 tree fieldtype;
8831 enum tree_code fieldcode;
8833 if (constructor_fields == 0)
8835 pedwarn_init (loc, 0, "excess elements in struct initializer");
8836 break;
8839 fieldtype = TREE_TYPE (constructor_fields);
8840 if (fieldtype != error_mark_node)
8841 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8842 fieldcode = TREE_CODE (fieldtype);
8844 /* Error for non-static initialization of a flexible array member. */
8845 if (fieldcode == ARRAY_TYPE
8846 && !require_constant_value
8847 && TYPE_SIZE (fieldtype) == NULL_TREE
8848 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8850 error_init (loc, "non-static initialization of a flexible "
8851 "array member");
8852 break;
8855 /* Error for initialization of a flexible array member with
8856 a string constant if the structure is in an array. E.g.:
8857 struct S { int x; char y[]; };
8858 struct S s[] = { { 1, "foo" } };
8859 is invalid. */
8860 if (string_flag
8861 && fieldcode == ARRAY_TYPE
8862 && constructor_depth > 1
8863 && TYPE_SIZE (fieldtype) == NULL_TREE
8864 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8866 bool in_array_p = false;
8867 for (struct constructor_stack *p = constructor_stack;
8868 p && p->type; p = p->next)
8869 if (TREE_CODE (p->type) == ARRAY_TYPE)
8871 in_array_p = true;
8872 break;
8874 if (in_array_p)
8876 error_init (loc, "initialization of flexible array "
8877 "member in a nested context");
8878 break;
8882 /* Accept a string constant to initialize a subarray. */
8883 if (value.value != 0
8884 && fieldcode == ARRAY_TYPE
8885 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8886 && string_flag)
8887 value.value = orig_value;
8888 /* Otherwise, if we have come to a subaggregate,
8889 and we don't have an element of its type, push into it. */
8890 else if (value.value != 0
8891 && value.value != error_mark_node
8892 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8893 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8894 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8896 push_init_level (loc, 1, braced_init_obstack);
8897 continue;
8900 if (value.value)
8902 push_member_name (constructor_fields);
8903 output_init_element (loc, value.value, value.original_type,
8904 strict_string, fieldtype,
8905 constructor_fields, 1, implicit,
8906 braced_init_obstack);
8907 RESTORE_SPELLING_DEPTH (constructor_depth);
8909 else
8910 /* Do the bookkeeping for an element that was
8911 directly output as a constructor. */
8913 /* For a record, keep track of end position of last field. */
8914 if (DECL_SIZE (constructor_fields))
8915 constructor_bit_index
8916 = size_binop_loc (input_location, PLUS_EXPR,
8917 bit_position (constructor_fields),
8918 DECL_SIZE (constructor_fields));
8920 /* If the current field was the first one not yet written out,
8921 it isn't now, so update. */
8922 if (constructor_unfilled_fields == constructor_fields)
8924 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8925 /* Skip any nameless bit fields. */
8926 while (constructor_unfilled_fields != 0
8927 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8928 && DECL_NAME (constructor_unfilled_fields) == 0)
8929 constructor_unfilled_fields =
8930 DECL_CHAIN (constructor_unfilled_fields);
8934 constructor_fields = DECL_CHAIN (constructor_fields);
8935 /* Skip any nameless bit fields at the beginning. */
8936 while (constructor_fields != 0
8937 && DECL_C_BIT_FIELD (constructor_fields)
8938 && DECL_NAME (constructor_fields) == 0)
8939 constructor_fields = DECL_CHAIN (constructor_fields);
8941 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8943 tree fieldtype;
8944 enum tree_code fieldcode;
8946 if (constructor_fields == 0)
8948 pedwarn_init (loc, 0,
8949 "excess elements in union initializer");
8950 break;
8953 fieldtype = TREE_TYPE (constructor_fields);
8954 if (fieldtype != error_mark_node)
8955 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8956 fieldcode = TREE_CODE (fieldtype);
8958 /* Warn that traditional C rejects initialization of unions.
8959 We skip the warning if the value is zero. This is done
8960 under the assumption that the zero initializer in user
8961 code appears conditioned on e.g. __STDC__ to avoid
8962 "missing initializer" warnings and relies on default
8963 initialization to zero in the traditional C case.
8964 We also skip the warning if the initializer is designated,
8965 again on the assumption that this must be conditional on
8966 __STDC__ anyway (and we've already complained about the
8967 member-designator already). */
8968 if (!in_system_header_at (input_location) && !constructor_designated
8969 && !(value.value && (integer_zerop (value.value)
8970 || real_zerop (value.value))))
8971 warning (OPT_Wtraditional, "traditional C rejects initialization "
8972 "of unions");
8974 /* Accept a string constant to initialize a subarray. */
8975 if (value.value != 0
8976 && fieldcode == ARRAY_TYPE
8977 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8978 && string_flag)
8979 value.value = orig_value;
8980 /* Otherwise, if we have come to a subaggregate,
8981 and we don't have an element of its type, push into it. */
8982 else if (value.value != 0
8983 && value.value != error_mark_node
8984 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8985 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8986 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8988 push_init_level (loc, 1, braced_init_obstack);
8989 continue;
8992 if (value.value)
8994 push_member_name (constructor_fields);
8995 output_init_element (loc, value.value, value.original_type,
8996 strict_string, fieldtype,
8997 constructor_fields, 1, implicit,
8998 braced_init_obstack);
8999 RESTORE_SPELLING_DEPTH (constructor_depth);
9001 else
9002 /* Do the bookkeeping for an element that was
9003 directly output as a constructor. */
9005 constructor_bit_index = DECL_SIZE (constructor_fields);
9006 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9009 constructor_fields = 0;
9011 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9013 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9014 enum tree_code eltcode = TREE_CODE (elttype);
9016 /* Accept a string constant to initialize a subarray. */
9017 if (value.value != 0
9018 && eltcode == ARRAY_TYPE
9019 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9020 && string_flag)
9021 value.value = orig_value;
9022 /* Otherwise, if we have come to a subaggregate,
9023 and we don't have an element of its type, push into it. */
9024 else if (value.value != 0
9025 && value.value != error_mark_node
9026 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9027 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9028 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9030 push_init_level (loc, 1, braced_init_obstack);
9031 continue;
9034 if (constructor_max_index != 0
9035 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9036 || integer_all_onesp (constructor_max_index)))
9038 pedwarn_init (loc, 0,
9039 "excess elements in array initializer");
9040 break;
9043 /* Now output the actual element. */
9044 if (value.value)
9046 push_array_bounds (tree_to_uhwi (constructor_index));
9047 output_init_element (loc, value.value, value.original_type,
9048 strict_string, elttype,
9049 constructor_index, 1, implicit,
9050 braced_init_obstack);
9051 RESTORE_SPELLING_DEPTH (constructor_depth);
9054 constructor_index
9055 = size_binop_loc (input_location, PLUS_EXPR,
9056 constructor_index, bitsize_one_node);
9058 if (!value.value)
9059 /* If we are doing the bookkeeping for an element that was
9060 directly output as a constructor, we must update
9061 constructor_unfilled_index. */
9062 constructor_unfilled_index = constructor_index;
9064 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
9066 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9068 /* Do a basic check of initializer size. Note that vectors
9069 always have a fixed size derived from their type. */
9070 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9072 pedwarn_init (loc, 0,
9073 "excess elements in vector initializer");
9074 break;
9077 /* Now output the actual element. */
9078 if (value.value)
9080 if (TREE_CODE (value.value) == VECTOR_CST)
9081 elttype = TYPE_MAIN_VARIANT (constructor_type);
9082 output_init_element (loc, value.value, value.original_type,
9083 strict_string, elttype,
9084 constructor_index, 1, implicit,
9085 braced_init_obstack);
9088 constructor_index
9089 = size_binop_loc (input_location,
9090 PLUS_EXPR, constructor_index, bitsize_one_node);
9092 if (!value.value)
9093 /* If we are doing the bookkeeping for an element that was
9094 directly output as a constructor, we must update
9095 constructor_unfilled_index. */
9096 constructor_unfilled_index = constructor_index;
9099 /* Handle the sole element allowed in a braced initializer
9100 for a scalar variable. */
9101 else if (constructor_type != error_mark_node
9102 && constructor_fields == 0)
9104 pedwarn_init (loc, 0,
9105 "excess elements in scalar initializer");
9106 break;
9108 else
9110 if (value.value)
9111 output_init_element (loc, value.value, value.original_type,
9112 strict_string, constructor_type,
9113 NULL_TREE, 1, implicit,
9114 braced_init_obstack);
9115 constructor_fields = 0;
9118 /* Handle range initializers either at this level or anywhere higher
9119 in the designator stack. */
9120 if (constructor_range_stack)
9122 struct constructor_range_stack *p, *range_stack;
9123 int finish = 0;
9125 range_stack = constructor_range_stack;
9126 constructor_range_stack = 0;
9127 while (constructor_stack != range_stack->stack)
9129 gcc_assert (constructor_stack->implicit);
9130 process_init_element (loc,
9131 pop_init_level (loc, 1,
9132 braced_init_obstack),
9133 true, braced_init_obstack);
9135 for (p = range_stack;
9136 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9137 p = p->prev)
9139 gcc_assert (constructor_stack->implicit);
9140 process_init_element (loc,
9141 pop_init_level (loc, 1,
9142 braced_init_obstack),
9143 true, braced_init_obstack);
9146 p->index = size_binop_loc (input_location,
9147 PLUS_EXPR, p->index, bitsize_one_node);
9148 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9149 finish = 1;
9151 while (1)
9153 constructor_index = p->index;
9154 constructor_fields = p->fields;
9155 if (finish && p->range_end && p->index == p->range_start)
9157 finish = 0;
9158 p->prev = 0;
9160 p = p->next;
9161 if (!p)
9162 break;
9163 push_init_level (loc, 2, braced_init_obstack);
9164 p->stack = constructor_stack;
9165 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9166 p->index = p->range_start;
9169 if (!finish)
9170 constructor_range_stack = range_stack;
9171 continue;
9174 break;
9177 constructor_range_stack = 0;
9180 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9181 (guaranteed to be 'volatile' or null) and ARGS (represented using
9182 an ASM_EXPR node). */
9183 tree
9184 build_asm_stmt (tree cv_qualifier, tree args)
9186 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9187 ASM_VOLATILE_P (args) = 1;
9188 return add_stmt (args);
9191 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9192 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9193 SIMPLE indicates whether there was anything at all after the
9194 string in the asm expression -- asm("blah") and asm("blah" : )
9195 are subtly different. We use a ASM_EXPR node to represent this. */
9196 tree
9197 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9198 tree clobbers, tree labels, bool simple)
9200 tree tail;
9201 tree args;
9202 int i;
9203 const char *constraint;
9204 const char **oconstraints;
9205 bool allows_mem, allows_reg, is_inout;
9206 int ninputs, noutputs;
9208 ninputs = list_length (inputs);
9209 noutputs = list_length (outputs);
9210 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9212 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9214 /* Remove output conversions that change the type but not the mode. */
9215 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9217 tree output = TREE_VALUE (tail);
9219 output = c_fully_fold (output, false, NULL);
9221 /* ??? Really, this should not be here. Users should be using a
9222 proper lvalue, dammit. But there's a long history of using casts
9223 in the output operands. In cases like longlong.h, this becomes a
9224 primitive form of typechecking -- if the cast can be removed, then
9225 the output operand had a type of the proper width; otherwise we'll
9226 get an error. Gross, but ... */
9227 STRIP_NOPS (output);
9229 if (!lvalue_or_else (loc, output, lv_asm))
9230 output = error_mark_node;
9232 if (output != error_mark_node
9233 && (TREE_READONLY (output)
9234 || TYPE_READONLY (TREE_TYPE (output))
9235 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9236 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9237 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9238 readonly_error (loc, output, lv_asm);
9240 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9241 oconstraints[i] = constraint;
9243 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9244 &allows_mem, &allows_reg, &is_inout))
9246 /* If the operand is going to end up in memory,
9247 mark it addressable. */
9248 if (!allows_reg && !c_mark_addressable (output))
9249 output = error_mark_node;
9250 if (!(!allows_reg && allows_mem)
9251 && output != error_mark_node
9252 && VOID_TYPE_P (TREE_TYPE (output)))
9254 error_at (loc, "invalid use of void expression");
9255 output = error_mark_node;
9258 else
9259 output = error_mark_node;
9261 TREE_VALUE (tail) = output;
9264 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9266 tree input;
9268 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9269 input = TREE_VALUE (tail);
9271 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9272 oconstraints, &allows_mem, &allows_reg))
9274 /* If the operand is going to end up in memory,
9275 mark it addressable. */
9276 if (!allows_reg && allows_mem)
9278 input = c_fully_fold (input, false, NULL);
9280 /* Strip the nops as we allow this case. FIXME, this really
9281 should be rejected or made deprecated. */
9282 STRIP_NOPS (input);
9283 if (!c_mark_addressable (input))
9284 input = error_mark_node;
9286 else
9288 struct c_expr expr;
9289 memset (&expr, 0, sizeof (expr));
9290 expr.value = input;
9291 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9292 input = c_fully_fold (expr.value, false, NULL);
9294 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9296 error_at (loc, "invalid use of void expression");
9297 input = error_mark_node;
9301 else
9302 input = error_mark_node;
9304 TREE_VALUE (tail) = input;
9307 /* ASMs with labels cannot have outputs. This should have been
9308 enforced by the parser. */
9309 gcc_assert (outputs == NULL || labels == NULL);
9311 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9313 /* asm statements without outputs, including simple ones, are treated
9314 as volatile. */
9315 ASM_INPUT_P (args) = simple;
9316 ASM_VOLATILE_P (args) = (noutputs == 0);
9318 return args;
9321 /* Generate a goto statement to LABEL. LOC is the location of the
9322 GOTO. */
9324 tree
9325 c_finish_goto_label (location_t loc, tree label)
9327 tree decl = lookup_label_for_goto (loc, label);
9328 if (!decl)
9329 return NULL_TREE;
9330 TREE_USED (decl) = 1;
9332 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9333 SET_EXPR_LOCATION (t, loc);
9334 return add_stmt (t);
9338 /* Generate a computed goto statement to EXPR. LOC is the location of
9339 the GOTO. */
9341 tree
9342 c_finish_goto_ptr (location_t loc, tree expr)
9344 tree t;
9345 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9346 expr = c_fully_fold (expr, false, NULL);
9347 expr = convert (ptr_type_node, expr);
9348 t = build1 (GOTO_EXPR, void_type_node, expr);
9349 SET_EXPR_LOCATION (t, loc);
9350 return add_stmt (t);
9353 /* Generate a C `return' statement. RETVAL is the expression for what
9354 to return, or a null pointer for `return;' with no value. LOC is
9355 the location of the return statement, or the location of the expression,
9356 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9357 is the original type of RETVAL. */
9359 tree
9360 c_finish_return (location_t loc, tree retval, tree origtype)
9362 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9363 bool no_warning = false;
9364 bool npc = false;
9365 size_t rank = 0;
9367 if (TREE_THIS_VOLATILE (current_function_decl))
9368 warning_at (loc, 0,
9369 "function declared %<noreturn%> has a %<return%> statement");
9371 if (flag_cilkplus && contains_array_notation_expr (retval))
9373 /* Array notations are allowed in a return statement if it is inside a
9374 built-in array notation reduction function. */
9375 if (!find_rank (loc, retval, retval, false, &rank))
9376 return error_mark_node;
9377 if (rank >= 1)
9379 error_at (loc, "array notation expression cannot be used as a "
9380 "return value");
9381 return error_mark_node;
9384 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9386 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9387 "allowed");
9388 return error_mark_node;
9390 if (retval)
9392 tree semantic_type = NULL_TREE;
9393 npc = null_pointer_constant_p (retval);
9394 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9396 semantic_type = TREE_TYPE (retval);
9397 retval = TREE_OPERAND (retval, 0);
9399 retval = c_fully_fold (retval, false, NULL);
9400 if (semantic_type)
9401 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9404 if (!retval)
9406 current_function_returns_null = 1;
9407 if ((warn_return_type || flag_isoc99)
9408 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9410 if (flag_isoc99)
9411 pedwarn (loc, 0, "%<return%> with no value, in "
9412 "function returning non-void");
9413 else
9414 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9415 "in function returning non-void");
9416 no_warning = true;
9419 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9421 current_function_returns_null = 1;
9422 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9423 pedwarn (loc, 0,
9424 "%<return%> with a value, in function returning void");
9425 else
9426 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9427 "%<return%> with expression, in function returning void");
9429 else
9431 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9432 retval, origtype, ic_return,
9433 npc, NULL_TREE, NULL_TREE, 0);
9434 tree res = DECL_RESULT (current_function_decl);
9435 tree inner;
9436 bool save;
9438 current_function_returns_value = 1;
9439 if (t == error_mark_node)
9440 return NULL_TREE;
9442 save = in_late_binary_op;
9443 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9444 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9445 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9446 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9447 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9448 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9449 in_late_binary_op = true;
9450 inner = t = convert (TREE_TYPE (res), t);
9451 in_late_binary_op = save;
9453 /* Strip any conversions, additions, and subtractions, and see if
9454 we are returning the address of a local variable. Warn if so. */
9455 while (1)
9457 switch (TREE_CODE (inner))
9459 CASE_CONVERT:
9460 case NON_LVALUE_EXPR:
9461 case PLUS_EXPR:
9462 case POINTER_PLUS_EXPR:
9463 inner = TREE_OPERAND (inner, 0);
9464 continue;
9466 case MINUS_EXPR:
9467 /* If the second operand of the MINUS_EXPR has a pointer
9468 type (or is converted from it), this may be valid, so
9469 don't give a warning. */
9471 tree op1 = TREE_OPERAND (inner, 1);
9473 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9474 && (CONVERT_EXPR_P (op1)
9475 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9476 op1 = TREE_OPERAND (op1, 0);
9478 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9479 break;
9481 inner = TREE_OPERAND (inner, 0);
9482 continue;
9485 case ADDR_EXPR:
9486 inner = TREE_OPERAND (inner, 0);
9488 while (REFERENCE_CLASS_P (inner)
9489 && TREE_CODE (inner) != INDIRECT_REF)
9490 inner = TREE_OPERAND (inner, 0);
9492 if (DECL_P (inner)
9493 && !DECL_EXTERNAL (inner)
9494 && !TREE_STATIC (inner)
9495 && DECL_CONTEXT (inner) == current_function_decl)
9497 if (TREE_CODE (inner) == LABEL_DECL)
9498 warning_at (loc, OPT_Wreturn_local_addr,
9499 "function returns address of label");
9500 else
9502 warning_at (loc, OPT_Wreturn_local_addr,
9503 "function returns address of local variable");
9504 tree zero = build_zero_cst (TREE_TYPE (res));
9505 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9508 break;
9510 default:
9511 break;
9514 break;
9517 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9518 SET_EXPR_LOCATION (retval, loc);
9520 if (warn_sequence_point)
9521 verify_sequence_points (retval);
9524 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9525 TREE_NO_WARNING (ret_stmt) |= no_warning;
9526 return add_stmt (ret_stmt);
9529 struct c_switch {
9530 /* The SWITCH_EXPR being built. */
9531 tree switch_expr;
9533 /* The original type of the testing expression, i.e. before the
9534 default conversion is applied. */
9535 tree orig_type;
9537 /* A splay-tree mapping the low element of a case range to the high
9538 element, or NULL_TREE if there is no high element. Used to
9539 determine whether or not a new case label duplicates an old case
9540 label. We need a tree, rather than simply a hash table, because
9541 of the GNU case range extension. */
9542 splay_tree cases;
9544 /* The bindings at the point of the switch. This is used for
9545 warnings crossing decls when branching to a case label. */
9546 struct c_spot_bindings *bindings;
9548 /* The next node on the stack. */
9549 struct c_switch *next;
9552 /* A stack of the currently active switch statements. The innermost
9553 switch statement is on the top of the stack. There is no need to
9554 mark the stack for garbage collection because it is only active
9555 during the processing of the body of a function, and we never
9556 collect at that point. */
9558 struct c_switch *c_switch_stack;
9560 /* Start a C switch statement, testing expression EXP. Return the new
9561 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9562 SWITCH_COND_LOC is the location of the switch's condition.
9563 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9565 tree
9566 c_start_case (location_t switch_loc,
9567 location_t switch_cond_loc,
9568 tree exp, bool explicit_cast_p)
9570 tree orig_type = error_mark_node;
9571 struct c_switch *cs;
9573 if (exp != error_mark_node)
9575 orig_type = TREE_TYPE (exp);
9577 if (!INTEGRAL_TYPE_P (orig_type))
9579 if (orig_type != error_mark_node)
9581 error_at (switch_cond_loc, "switch quantity not an integer");
9582 orig_type = error_mark_node;
9584 exp = integer_zero_node;
9586 else
9588 tree type = TYPE_MAIN_VARIANT (orig_type);
9589 tree e = exp;
9591 /* Warn if the condition has boolean value. */
9592 while (TREE_CODE (e) == COMPOUND_EXPR)
9593 e = TREE_OPERAND (e, 1);
9595 if ((TREE_CODE (type) == BOOLEAN_TYPE
9596 || truth_value_p (TREE_CODE (e)))
9597 /* Explicit cast to int suppresses this warning. */
9598 && !(TREE_CODE (type) == INTEGER_TYPE
9599 && explicit_cast_p))
9600 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9601 "switch condition has boolean value");
9603 if (!in_system_header_at (input_location)
9604 && (type == long_integer_type_node
9605 || type == long_unsigned_type_node))
9606 warning_at (switch_cond_loc,
9607 OPT_Wtraditional, "%<long%> switch expression not "
9608 "converted to %<int%> in ISO C");
9610 exp = c_fully_fold (exp, false, NULL);
9611 exp = default_conversion (exp);
9613 if (warn_sequence_point)
9614 verify_sequence_points (exp);
9618 /* Add this new SWITCH_EXPR to the stack. */
9619 cs = XNEW (struct c_switch);
9620 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9621 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9622 cs->orig_type = orig_type;
9623 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9624 cs->bindings = c_get_switch_bindings ();
9625 cs->next = c_switch_stack;
9626 c_switch_stack = cs;
9628 return add_stmt (cs->switch_expr);
9631 /* Process a case label at location LOC. */
9633 tree
9634 do_case (location_t loc, tree low_value, tree high_value)
9636 tree label = NULL_TREE;
9638 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9640 low_value = c_fully_fold (low_value, false, NULL);
9641 if (TREE_CODE (low_value) == INTEGER_CST)
9642 pedwarn (loc, OPT_Wpedantic,
9643 "case label is not an integer constant expression");
9646 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9648 high_value = c_fully_fold (high_value, false, NULL);
9649 if (TREE_CODE (high_value) == INTEGER_CST)
9650 pedwarn (input_location, OPT_Wpedantic,
9651 "case label is not an integer constant expression");
9654 if (c_switch_stack == NULL)
9656 if (low_value)
9657 error_at (loc, "case label not within a switch statement");
9658 else
9659 error_at (loc, "%<default%> label not within a switch statement");
9660 return NULL_TREE;
9663 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9664 EXPR_LOCATION (c_switch_stack->switch_expr),
9665 loc))
9666 return NULL_TREE;
9668 label = c_add_case_label (loc, c_switch_stack->cases,
9669 SWITCH_COND (c_switch_stack->switch_expr),
9670 c_switch_stack->orig_type,
9671 low_value, high_value);
9672 if (label == error_mark_node)
9673 label = NULL_TREE;
9674 return label;
9677 /* Finish the switch statement. TYPE is the original type of the
9678 controlling expression of the switch, or NULL_TREE. */
9680 void
9681 c_finish_case (tree body, tree type)
9683 struct c_switch *cs = c_switch_stack;
9684 location_t switch_location;
9686 SWITCH_BODY (cs->switch_expr) = body;
9688 /* Emit warnings as needed. */
9689 switch_location = EXPR_LOCATION (cs->switch_expr);
9690 c_do_switch_warnings (cs->cases, switch_location,
9691 type ? type : TREE_TYPE (cs->switch_expr),
9692 SWITCH_COND (cs->switch_expr));
9694 /* Pop the stack. */
9695 c_switch_stack = cs->next;
9696 splay_tree_delete (cs->cases);
9697 c_release_switch_bindings (cs->bindings);
9698 XDELETE (cs);
9701 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9702 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9703 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9704 statement, and was not surrounded with parenthesis. */
9706 void
9707 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9708 tree else_block, bool nested_if)
9710 tree stmt;
9712 /* If the condition has array notations, then the rank of the then_block and
9713 else_block must be either 0 or be equal to the rank of the condition. If
9714 the condition does not have array notations then break them up as it is
9715 broken up in a normal expression. */
9716 if (flag_cilkplus && contains_array_notation_expr (cond))
9718 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9719 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9720 return;
9721 if (then_block
9722 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9723 return;
9724 if (else_block
9725 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9726 return;
9727 if (cond_rank != then_rank && then_rank != 0)
9729 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9730 " and the then-block");
9731 return;
9733 else if (cond_rank != else_rank && else_rank != 0)
9735 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9736 " and the else-block");
9737 return;
9740 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9741 if (warn_parentheses && nested_if && else_block == NULL)
9743 tree inner_if = then_block;
9745 /* We know from the grammar productions that there is an IF nested
9746 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9747 it might not be exactly THEN_BLOCK, but should be the last
9748 non-container statement within. */
9749 while (1)
9750 switch (TREE_CODE (inner_if))
9752 case COND_EXPR:
9753 goto found;
9754 case BIND_EXPR:
9755 inner_if = BIND_EXPR_BODY (inner_if);
9756 break;
9757 case STATEMENT_LIST:
9758 inner_if = expr_last (then_block);
9759 break;
9760 case TRY_FINALLY_EXPR:
9761 case TRY_CATCH_EXPR:
9762 inner_if = TREE_OPERAND (inner_if, 0);
9763 break;
9764 default:
9765 gcc_unreachable ();
9767 found:
9769 if (COND_EXPR_ELSE (inner_if))
9770 warning_at (if_locus, OPT_Wparentheses,
9771 "suggest explicit braces to avoid ambiguous %<else%>");
9774 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9775 SET_EXPR_LOCATION (stmt, if_locus);
9776 add_stmt (stmt);
9779 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9780 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9781 is false for DO loops. INCR is the FOR increment expression. BODY is
9782 the statement controlled by the loop. BLAB is the break label. CLAB is
9783 the continue label. Everything is allowed to be NULL. */
9785 void
9786 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9787 tree blab, tree clab, bool cond_is_first)
9789 tree entry = NULL, exit = NULL, t;
9791 /* In theory could forbid cilk spawn for loop increment expression,
9792 but it should work just fine. */
9794 /* If the condition is zero don't generate a loop construct. */
9795 if (cond && integer_zerop (cond))
9797 if (cond_is_first)
9799 t = build_and_jump (&blab);
9800 SET_EXPR_LOCATION (t, start_locus);
9801 add_stmt (t);
9804 else
9806 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9808 /* If we have an exit condition, then we build an IF with gotos either
9809 out of the loop, or to the top of it. If there's no exit condition,
9810 then we just build a jump back to the top. */
9811 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9813 if (cond && !integer_nonzerop (cond))
9815 /* Canonicalize the loop condition to the end. This means
9816 generating a branch to the loop condition. Reuse the
9817 continue label, if possible. */
9818 if (cond_is_first)
9820 if (incr || !clab)
9822 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9823 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9825 else
9826 t = build1 (GOTO_EXPR, void_type_node, clab);
9827 SET_EXPR_LOCATION (t, start_locus);
9828 add_stmt (t);
9831 t = build_and_jump (&blab);
9832 if (cond_is_first)
9833 exit = fold_build3_loc (start_locus,
9834 COND_EXPR, void_type_node, cond, exit, t);
9835 else
9836 exit = fold_build3_loc (input_location,
9837 COND_EXPR, void_type_node, cond, exit, t);
9840 add_stmt (top);
9843 if (body)
9844 add_stmt (body);
9845 if (clab)
9846 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9847 if (incr)
9848 add_stmt (incr);
9849 if (entry)
9850 add_stmt (entry);
9851 if (exit)
9852 add_stmt (exit);
9853 if (blab)
9854 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9857 tree
9858 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9860 bool skip;
9861 tree label = *label_p;
9863 /* In switch statements break is sometimes stylistically used after
9864 a return statement. This can lead to spurious warnings about
9865 control reaching the end of a non-void function when it is
9866 inlined. Note that we are calling block_may_fallthru with
9867 language specific tree nodes; this works because
9868 block_may_fallthru returns true when given something it does not
9869 understand. */
9870 skip = !block_may_fallthru (cur_stmt_list);
9872 if (!label)
9874 if (!skip)
9875 *label_p = label = create_artificial_label (loc);
9877 else if (TREE_CODE (label) == LABEL_DECL)
9879 else switch (TREE_INT_CST_LOW (label))
9881 case 0:
9882 if (is_break)
9883 error_at (loc, "break statement not within loop or switch");
9884 else
9885 error_at (loc, "continue statement not within a loop");
9886 return NULL_TREE;
9888 case 1:
9889 gcc_assert (is_break);
9890 error_at (loc, "break statement used with OpenMP for loop");
9891 return NULL_TREE;
9893 case 2:
9894 if (is_break)
9895 error ("break statement within %<#pragma simd%> loop body");
9896 else
9897 error ("continue statement within %<#pragma simd%> loop body");
9898 return NULL_TREE;
9900 default:
9901 gcc_unreachable ();
9904 if (skip)
9905 return NULL_TREE;
9907 if (!is_break)
9908 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9910 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9913 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9915 static void
9916 emit_side_effect_warnings (location_t loc, tree expr)
9918 if (expr == error_mark_node)
9920 else if (!TREE_SIDE_EFFECTS (expr))
9922 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9923 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9925 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9927 tree r = expr;
9928 location_t cloc = loc;
9929 while (TREE_CODE (r) == COMPOUND_EXPR)
9931 if (EXPR_HAS_LOCATION (r))
9932 cloc = EXPR_LOCATION (r);
9933 r = TREE_OPERAND (r, 1);
9935 if (!TREE_SIDE_EFFECTS (r)
9936 && !VOID_TYPE_P (TREE_TYPE (r))
9937 && !CONVERT_EXPR_P (r)
9938 && !TREE_NO_WARNING (r)
9939 && !TREE_NO_WARNING (expr))
9940 warning_at (cloc, OPT_Wunused_value,
9941 "right-hand operand of comma expression has no effect");
9943 else
9944 warn_if_unused_value (expr, loc);
9947 /* Process an expression as if it were a complete statement. Emit
9948 diagnostics, but do not call ADD_STMT. LOC is the location of the
9949 statement. */
9951 tree
9952 c_process_expr_stmt (location_t loc, tree expr)
9954 tree exprv;
9956 if (!expr)
9957 return NULL_TREE;
9959 expr = c_fully_fold (expr, false, NULL);
9961 if (warn_sequence_point)
9962 verify_sequence_points (expr);
9964 if (TREE_TYPE (expr) != error_mark_node
9965 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9966 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9967 error_at (loc, "expression statement has incomplete type");
9969 /* If we're not processing a statement expression, warn about unused values.
9970 Warnings for statement expressions will be emitted later, once we figure
9971 out which is the result. */
9972 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9973 && warn_unused_value)
9974 emit_side_effect_warnings (loc, expr);
9976 exprv = expr;
9977 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9978 exprv = TREE_OPERAND (exprv, 1);
9979 while (CONVERT_EXPR_P (exprv))
9980 exprv = TREE_OPERAND (exprv, 0);
9981 if (DECL_P (exprv)
9982 || handled_component_p (exprv)
9983 || TREE_CODE (exprv) == ADDR_EXPR)
9984 mark_exp_read (exprv);
9986 /* If the expression is not of a type to which we cannot assign a line
9987 number, wrap the thing in a no-op NOP_EXPR. */
9988 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9990 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9991 SET_EXPR_LOCATION (expr, loc);
9994 return expr;
9997 /* Emit an expression as a statement. LOC is the location of the
9998 expression. */
10000 tree
10001 c_finish_expr_stmt (location_t loc, tree expr)
10003 if (expr)
10004 return add_stmt (c_process_expr_stmt (loc, expr));
10005 else
10006 return NULL;
10009 /* Do the opposite and emit a statement as an expression. To begin,
10010 create a new binding level and return it. */
10012 tree
10013 c_begin_stmt_expr (void)
10015 tree ret;
10017 /* We must force a BLOCK for this level so that, if it is not expanded
10018 later, there is a way to turn off the entire subtree of blocks that
10019 are contained in it. */
10020 keep_next_level ();
10021 ret = c_begin_compound_stmt (true);
10023 c_bindings_start_stmt_expr (c_switch_stack == NULL
10024 ? NULL
10025 : c_switch_stack->bindings);
10027 /* Mark the current statement list as belonging to a statement list. */
10028 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10030 return ret;
10033 /* LOC is the location of the compound statement to which this body
10034 belongs. */
10036 tree
10037 c_finish_stmt_expr (location_t loc, tree body)
10039 tree last, type, tmp, val;
10040 tree *last_p;
10042 body = c_end_compound_stmt (loc, body, true);
10044 c_bindings_end_stmt_expr (c_switch_stack == NULL
10045 ? NULL
10046 : c_switch_stack->bindings);
10048 /* Locate the last statement in BODY. See c_end_compound_stmt
10049 about always returning a BIND_EXPR. */
10050 last_p = &BIND_EXPR_BODY (body);
10051 last = BIND_EXPR_BODY (body);
10053 continue_searching:
10054 if (TREE_CODE (last) == STATEMENT_LIST)
10056 tree_stmt_iterator i;
10058 /* This can happen with degenerate cases like ({ }). No value. */
10059 if (!TREE_SIDE_EFFECTS (last))
10060 return body;
10062 /* If we're supposed to generate side effects warnings, process
10063 all of the statements except the last. */
10064 if (warn_unused_value)
10066 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10068 location_t tloc;
10069 tree t = tsi_stmt (i);
10071 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10072 emit_side_effect_warnings (tloc, t);
10075 else
10076 i = tsi_last (last);
10077 last_p = tsi_stmt_ptr (i);
10078 last = *last_p;
10081 /* If the end of the list is exception related, then the list was split
10082 by a call to push_cleanup. Continue searching. */
10083 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10084 || TREE_CODE (last) == TRY_CATCH_EXPR)
10086 last_p = &TREE_OPERAND (last, 0);
10087 last = *last_p;
10088 goto continue_searching;
10091 if (last == error_mark_node)
10092 return last;
10094 /* In the case that the BIND_EXPR is not necessary, return the
10095 expression out from inside it. */
10096 if (last == BIND_EXPR_BODY (body)
10097 && BIND_EXPR_VARS (body) == NULL)
10099 /* Even if this looks constant, do not allow it in a constant
10100 expression. */
10101 last = c_wrap_maybe_const (last, true);
10102 /* Do not warn if the return value of a statement expression is
10103 unused. */
10104 TREE_NO_WARNING (last) = 1;
10105 return last;
10108 /* Extract the type of said expression. */
10109 type = TREE_TYPE (last);
10111 /* If we're not returning a value at all, then the BIND_EXPR that
10112 we already have is a fine expression to return. */
10113 if (!type || VOID_TYPE_P (type))
10114 return body;
10116 /* Now that we've located the expression containing the value, it seems
10117 silly to make voidify_wrapper_expr repeat the process. Create a
10118 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10119 tmp = create_tmp_var_raw (type);
10121 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10122 tree_expr_nonnegative_p giving up immediately. */
10123 val = last;
10124 if (TREE_CODE (val) == NOP_EXPR
10125 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10126 val = TREE_OPERAND (val, 0);
10128 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10129 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10132 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10133 SET_EXPR_LOCATION (t, loc);
10134 return t;
10138 /* Begin and end compound statements. This is as simple as pushing
10139 and popping new statement lists from the tree. */
10141 tree
10142 c_begin_compound_stmt (bool do_scope)
10144 tree stmt = push_stmt_list ();
10145 if (do_scope)
10146 push_scope ();
10147 return stmt;
10150 /* End a compound statement. STMT is the statement. LOC is the
10151 location of the compound statement-- this is usually the location
10152 of the opening brace. */
10154 tree
10155 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10157 tree block = NULL;
10159 if (do_scope)
10161 if (c_dialect_objc ())
10162 objc_clear_super_receiver ();
10163 block = pop_scope ();
10166 stmt = pop_stmt_list (stmt);
10167 stmt = c_build_bind_expr (loc, block, stmt);
10169 /* If this compound statement is nested immediately inside a statement
10170 expression, then force a BIND_EXPR to be created. Otherwise we'll
10171 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10172 STATEMENT_LISTs merge, and thus we can lose track of what statement
10173 was really last. */
10174 if (building_stmt_list_p ()
10175 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10176 && TREE_CODE (stmt) != BIND_EXPR)
10178 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10179 TREE_SIDE_EFFECTS (stmt) = 1;
10180 SET_EXPR_LOCATION (stmt, loc);
10183 return stmt;
10186 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10187 when the current scope is exited. EH_ONLY is true when this is not
10188 meant to apply to normal control flow transfer. */
10190 void
10191 push_cleanup (tree decl, tree cleanup, bool eh_only)
10193 enum tree_code code;
10194 tree stmt, list;
10195 bool stmt_expr;
10197 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10198 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10199 add_stmt (stmt);
10200 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10201 list = push_stmt_list ();
10202 TREE_OPERAND (stmt, 0) = list;
10203 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10206 /* Build a binary-operation expression without default conversions.
10207 CODE is the kind of expression to build.
10208 LOCATION is the operator's location.
10209 This function differs from `build' in several ways:
10210 the data type of the result is computed and recorded in it,
10211 warnings are generated if arg data types are invalid,
10212 special handling for addition and subtraction of pointers is known,
10213 and some optimization is done (operations on narrow ints
10214 are done in the narrower type when that gives the same result).
10215 Constant folding is also done before the result is returned.
10217 Note that the operands will never have enumeral types, or function
10218 or array types, because either they will have the default conversions
10219 performed or they have both just been converted to some other type in which
10220 the arithmetic is to be done. */
10222 tree
10223 build_binary_op (location_t location, enum tree_code code,
10224 tree orig_op0, tree orig_op1, int convert_p)
10226 tree type0, type1, orig_type0, orig_type1;
10227 tree eptype;
10228 enum tree_code code0, code1;
10229 tree op0, op1;
10230 tree ret = error_mark_node;
10231 const char *invalid_op_diag;
10232 bool op0_int_operands, op1_int_operands;
10233 bool int_const, int_const_or_overflow, int_operands;
10235 /* Expression code to give to the expression when it is built.
10236 Normally this is CODE, which is what the caller asked for,
10237 but in some special cases we change it. */
10238 enum tree_code resultcode = code;
10240 /* Data type in which the computation is to be performed.
10241 In the simplest cases this is the common type of the arguments. */
10242 tree result_type = NULL;
10244 /* When the computation is in excess precision, the type of the
10245 final EXCESS_PRECISION_EXPR. */
10246 tree semantic_result_type = NULL;
10248 /* Nonzero means operands have already been type-converted
10249 in whatever way is necessary.
10250 Zero means they need to be converted to RESULT_TYPE. */
10251 int converted = 0;
10253 /* Nonzero means create the expression with this type, rather than
10254 RESULT_TYPE. */
10255 tree build_type = 0;
10257 /* Nonzero means after finally constructing the expression
10258 convert it to this type. */
10259 tree final_type = 0;
10261 /* Nonzero if this is an operation like MIN or MAX which can
10262 safely be computed in short if both args are promoted shorts.
10263 Also implies COMMON.
10264 -1 indicates a bitwise operation; this makes a difference
10265 in the exact conditions for when it is safe to do the operation
10266 in a narrower mode. */
10267 int shorten = 0;
10269 /* Nonzero if this is a comparison operation;
10270 if both args are promoted shorts, compare the original shorts.
10271 Also implies COMMON. */
10272 int short_compare = 0;
10274 /* Nonzero if this is a right-shift operation, which can be computed on the
10275 original short and then promoted if the operand is a promoted short. */
10276 int short_shift = 0;
10278 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10279 int common = 0;
10281 /* True means types are compatible as far as ObjC is concerned. */
10282 bool objc_ok;
10284 /* True means this is an arithmetic operation that may need excess
10285 precision. */
10286 bool may_need_excess_precision;
10288 /* True means this is a boolean operation that converts both its
10289 operands to truth-values. */
10290 bool boolean_op = false;
10292 /* Remember whether we're doing / or %. */
10293 bool doing_div_or_mod = false;
10295 /* Remember whether we're doing << or >>. */
10296 bool doing_shift = false;
10298 /* Tree holding instrumentation expression. */
10299 tree instrument_expr = NULL;
10301 if (location == UNKNOWN_LOCATION)
10302 location = input_location;
10304 op0 = orig_op0;
10305 op1 = orig_op1;
10307 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10308 if (op0_int_operands)
10309 op0 = remove_c_maybe_const_expr (op0);
10310 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10311 if (op1_int_operands)
10312 op1 = remove_c_maybe_const_expr (op1);
10313 int_operands = (op0_int_operands && op1_int_operands);
10314 if (int_operands)
10316 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10317 && TREE_CODE (orig_op1) == INTEGER_CST);
10318 int_const = (int_const_or_overflow
10319 && !TREE_OVERFLOW (orig_op0)
10320 && !TREE_OVERFLOW (orig_op1));
10322 else
10323 int_const = int_const_or_overflow = false;
10325 /* Do not apply default conversion in mixed vector/scalar expression. */
10326 if (convert_p
10327 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10328 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10330 op0 = default_conversion (op0);
10331 op1 = default_conversion (op1);
10334 /* When Cilk Plus is enabled and there are array notations inside op0, then
10335 we check to see if there are builtin array notation functions. If
10336 so, then we take on the type of the array notation inside it. */
10337 if (flag_cilkplus && contains_array_notation_expr (op0))
10338 orig_type0 = type0 = find_correct_array_notation_type (op0);
10339 else
10340 orig_type0 = type0 = TREE_TYPE (op0);
10342 if (flag_cilkplus && contains_array_notation_expr (op1))
10343 orig_type1 = type1 = find_correct_array_notation_type (op1);
10344 else
10345 orig_type1 = type1 = TREE_TYPE (op1);
10347 /* The expression codes of the data types of the arguments tell us
10348 whether the arguments are integers, floating, pointers, etc. */
10349 code0 = TREE_CODE (type0);
10350 code1 = TREE_CODE (type1);
10352 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10353 STRIP_TYPE_NOPS (op0);
10354 STRIP_TYPE_NOPS (op1);
10356 /* If an error was already reported for one of the arguments,
10357 avoid reporting another error. */
10359 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10360 return error_mark_node;
10362 if ((invalid_op_diag
10363 = targetm.invalid_binary_op (code, type0, type1)))
10365 error_at (location, invalid_op_diag);
10366 return error_mark_node;
10369 switch (code)
10371 case PLUS_EXPR:
10372 case MINUS_EXPR:
10373 case MULT_EXPR:
10374 case TRUNC_DIV_EXPR:
10375 case CEIL_DIV_EXPR:
10376 case FLOOR_DIV_EXPR:
10377 case ROUND_DIV_EXPR:
10378 case EXACT_DIV_EXPR:
10379 may_need_excess_precision = true;
10380 break;
10381 default:
10382 may_need_excess_precision = false;
10383 break;
10385 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10387 op0 = TREE_OPERAND (op0, 0);
10388 type0 = TREE_TYPE (op0);
10390 else if (may_need_excess_precision
10391 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10393 type0 = eptype;
10394 op0 = convert (eptype, op0);
10396 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10398 op1 = TREE_OPERAND (op1, 0);
10399 type1 = TREE_TYPE (op1);
10401 else if (may_need_excess_precision
10402 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10404 type1 = eptype;
10405 op1 = convert (eptype, op1);
10408 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10410 /* In case when one of the operands of the binary operation is
10411 a vector and another is a scalar -- convert scalar to vector. */
10412 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10414 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10415 true);
10417 switch (convert_flag)
10419 case stv_error:
10420 return error_mark_node;
10421 case stv_firstarg:
10423 bool maybe_const = true;
10424 tree sc;
10425 sc = c_fully_fold (op0, false, &maybe_const);
10426 sc = save_expr (sc);
10427 sc = convert (TREE_TYPE (type1), sc);
10428 op0 = build_vector_from_val (type1, sc);
10429 if (!maybe_const)
10430 op0 = c_wrap_maybe_const (op0, true);
10431 orig_type0 = type0 = TREE_TYPE (op0);
10432 code0 = TREE_CODE (type0);
10433 converted = 1;
10434 break;
10436 case stv_secondarg:
10438 bool maybe_const = true;
10439 tree sc;
10440 sc = c_fully_fold (op1, false, &maybe_const);
10441 sc = save_expr (sc);
10442 sc = convert (TREE_TYPE (type0), sc);
10443 op1 = build_vector_from_val (type0, sc);
10444 if (!maybe_const)
10445 op1 = c_wrap_maybe_const (op1, true);
10446 orig_type1 = type1 = TREE_TYPE (op1);
10447 code1 = TREE_CODE (type1);
10448 converted = 1;
10449 break;
10451 default:
10452 break;
10456 switch (code)
10458 case PLUS_EXPR:
10459 /* Handle the pointer + int case. */
10460 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10462 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10463 goto return_build_binary_op;
10465 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10467 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10468 goto return_build_binary_op;
10470 else
10471 common = 1;
10472 break;
10474 case MINUS_EXPR:
10475 /* Subtraction of two similar pointers.
10476 We must subtract them as integers, then divide by object size. */
10477 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10478 && comp_target_types (location, type0, type1))
10480 ret = pointer_diff (location, op0, op1);
10481 goto return_build_binary_op;
10483 /* Handle pointer minus int. Just like pointer plus int. */
10484 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10486 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10487 goto return_build_binary_op;
10489 else
10490 common = 1;
10491 break;
10493 case MULT_EXPR:
10494 common = 1;
10495 break;
10497 case TRUNC_DIV_EXPR:
10498 case CEIL_DIV_EXPR:
10499 case FLOOR_DIV_EXPR:
10500 case ROUND_DIV_EXPR:
10501 case EXACT_DIV_EXPR:
10502 doing_div_or_mod = true;
10503 warn_for_div_by_zero (location, op1);
10505 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10506 || code0 == FIXED_POINT_TYPE
10507 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10508 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10509 || code1 == FIXED_POINT_TYPE
10510 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10512 enum tree_code tcode0 = code0, tcode1 = code1;
10514 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10515 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10516 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10517 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10519 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10520 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10521 resultcode = RDIV_EXPR;
10522 else
10523 /* Although it would be tempting to shorten always here, that
10524 loses on some targets, since the modulo instruction is
10525 undefined if the quotient can't be represented in the
10526 computation mode. We shorten only if unsigned or if
10527 dividing by something we know != -1. */
10528 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10529 || (TREE_CODE (op1) == INTEGER_CST
10530 && !integer_all_onesp (op1)));
10531 common = 1;
10533 break;
10535 case BIT_AND_EXPR:
10536 case BIT_IOR_EXPR:
10537 case BIT_XOR_EXPR:
10538 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10539 shorten = -1;
10540 /* Allow vector types which are not floating point types. */
10541 else if (code0 == VECTOR_TYPE
10542 && code1 == VECTOR_TYPE
10543 && !VECTOR_FLOAT_TYPE_P (type0)
10544 && !VECTOR_FLOAT_TYPE_P (type1))
10545 common = 1;
10546 break;
10548 case TRUNC_MOD_EXPR:
10549 case FLOOR_MOD_EXPR:
10550 doing_div_or_mod = true;
10551 warn_for_div_by_zero (location, op1);
10553 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10554 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10555 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10556 common = 1;
10557 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10559 /* Although it would be tempting to shorten always here, that loses
10560 on some targets, since the modulo instruction is undefined if the
10561 quotient can't be represented in the computation mode. We shorten
10562 only if unsigned or if dividing by something we know != -1. */
10563 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10564 || (TREE_CODE (op1) == INTEGER_CST
10565 && !integer_all_onesp (op1)));
10566 common = 1;
10568 break;
10570 case TRUTH_ANDIF_EXPR:
10571 case TRUTH_ORIF_EXPR:
10572 case TRUTH_AND_EXPR:
10573 case TRUTH_OR_EXPR:
10574 case TRUTH_XOR_EXPR:
10575 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10576 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10577 || code0 == FIXED_POINT_TYPE)
10578 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10579 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10580 || code1 == FIXED_POINT_TYPE))
10582 /* Result of these operations is always an int,
10583 but that does not mean the operands should be
10584 converted to ints! */
10585 result_type = integer_type_node;
10586 if (op0_int_operands)
10588 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10589 op0 = remove_c_maybe_const_expr (op0);
10591 else
10592 op0 = c_objc_common_truthvalue_conversion (location, op0);
10593 if (op1_int_operands)
10595 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10596 op1 = remove_c_maybe_const_expr (op1);
10598 else
10599 op1 = c_objc_common_truthvalue_conversion (location, op1);
10600 converted = 1;
10601 boolean_op = true;
10603 if (code == TRUTH_ANDIF_EXPR)
10605 int_const_or_overflow = (int_operands
10606 && TREE_CODE (orig_op0) == INTEGER_CST
10607 && (op0 == truthvalue_false_node
10608 || TREE_CODE (orig_op1) == INTEGER_CST));
10609 int_const = (int_const_or_overflow
10610 && !TREE_OVERFLOW (orig_op0)
10611 && (op0 == truthvalue_false_node
10612 || !TREE_OVERFLOW (orig_op1)));
10614 else if (code == TRUTH_ORIF_EXPR)
10616 int_const_or_overflow = (int_operands
10617 && TREE_CODE (orig_op0) == INTEGER_CST
10618 && (op0 == truthvalue_true_node
10619 || TREE_CODE (orig_op1) == INTEGER_CST));
10620 int_const = (int_const_or_overflow
10621 && !TREE_OVERFLOW (orig_op0)
10622 && (op0 == truthvalue_true_node
10623 || !TREE_OVERFLOW (orig_op1)));
10625 break;
10627 /* Shift operations: result has same type as first operand;
10628 always convert second operand to int.
10629 Also set SHORT_SHIFT if shifting rightward. */
10631 case RSHIFT_EXPR:
10632 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10633 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10635 result_type = type0;
10636 converted = 1;
10638 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10639 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10640 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10641 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10643 result_type = type0;
10644 converted = 1;
10646 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10647 && code1 == INTEGER_TYPE)
10649 doing_shift = true;
10650 if (TREE_CODE (op1) == INTEGER_CST)
10652 if (tree_int_cst_sgn (op1) < 0)
10654 int_const = false;
10655 if (c_inhibit_evaluation_warnings == 0)
10656 warning_at (location, OPT_Wshift_count_negative,
10657 "right shift count is negative");
10659 else
10661 if (!integer_zerop (op1))
10662 short_shift = 1;
10664 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10666 int_const = false;
10667 if (c_inhibit_evaluation_warnings == 0)
10668 warning_at (location, OPT_Wshift_count_overflow,
10669 "right shift count >= width of type");
10674 /* Use the type of the value to be shifted. */
10675 result_type = type0;
10676 /* Avoid converting op1 to result_type later. */
10677 converted = 1;
10679 break;
10681 case LSHIFT_EXPR:
10682 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10683 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10685 result_type = type0;
10686 converted = 1;
10688 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10689 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10690 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10691 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10693 result_type = type0;
10694 converted = 1;
10696 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10697 && code1 == INTEGER_TYPE)
10699 doing_shift = true;
10700 if (TREE_CODE (op0) == INTEGER_CST
10701 && tree_int_cst_sgn (op0) < 0)
10703 /* Don't reject a left shift of a negative value in a context
10704 where a constant expression is needed in C90. */
10705 if (flag_isoc99)
10706 int_const = false;
10707 if (c_inhibit_evaluation_warnings == 0)
10708 warning_at (location, OPT_Wshift_negative_value,
10709 "left shift of negative value");
10711 if (TREE_CODE (op1) == INTEGER_CST)
10713 if (tree_int_cst_sgn (op1) < 0)
10715 int_const = false;
10716 if (c_inhibit_evaluation_warnings == 0)
10717 warning_at (location, OPT_Wshift_count_negative,
10718 "left shift count is negative");
10721 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10723 int_const = false;
10724 if (c_inhibit_evaluation_warnings == 0)
10725 warning_at (location, OPT_Wshift_count_overflow,
10726 "left shift count >= width of type");
10730 /* Use the type of the value to be shifted. */
10731 result_type = type0;
10732 /* Avoid converting op1 to result_type later. */
10733 converted = 1;
10735 break;
10737 case EQ_EXPR:
10738 case NE_EXPR:
10739 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10741 tree intt;
10742 if (!vector_types_compatible_elements_p (type0, type1))
10744 error_at (location, "comparing vectors with different "
10745 "element types");
10746 return error_mark_node;
10749 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10751 error_at (location, "comparing vectors with different "
10752 "number of elements");
10753 return error_mark_node;
10756 /* Always construct signed integer vector type. */
10757 intt = c_common_type_for_size (GET_MODE_BITSIZE
10758 (TYPE_MODE (TREE_TYPE (type0))), 0);
10759 result_type = build_opaque_vector_type (intt,
10760 TYPE_VECTOR_SUBPARTS (type0));
10761 converted = 1;
10762 break;
10764 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10765 warning_at (location,
10766 OPT_Wfloat_equal,
10767 "comparing floating point with == or != is unsafe");
10768 /* Result of comparison is always int,
10769 but don't convert the args to int! */
10770 build_type = integer_type_node;
10771 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10772 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10773 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10774 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10775 short_compare = 1;
10776 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10778 if (TREE_CODE (op0) == ADDR_EXPR
10779 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10781 if (code == EQ_EXPR)
10782 warning_at (location,
10783 OPT_Waddress,
10784 "the comparison will always evaluate as %<false%> "
10785 "for the address of %qD will never be NULL",
10786 TREE_OPERAND (op0, 0));
10787 else
10788 warning_at (location,
10789 OPT_Waddress,
10790 "the comparison will always evaluate as %<true%> "
10791 "for the address of %qD will never be NULL",
10792 TREE_OPERAND (op0, 0));
10794 result_type = type0;
10796 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10798 if (TREE_CODE (op1) == ADDR_EXPR
10799 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10801 if (code == EQ_EXPR)
10802 warning_at (location,
10803 OPT_Waddress,
10804 "the comparison will always evaluate as %<false%> "
10805 "for the address of %qD will never be NULL",
10806 TREE_OPERAND (op1, 0));
10807 else
10808 warning_at (location,
10809 OPT_Waddress,
10810 "the comparison will always evaluate as %<true%> "
10811 "for the address of %qD will never be NULL",
10812 TREE_OPERAND (op1, 0));
10814 result_type = type1;
10816 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10818 tree tt0 = TREE_TYPE (type0);
10819 tree tt1 = TREE_TYPE (type1);
10820 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10821 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10822 addr_space_t as_common = ADDR_SPACE_GENERIC;
10824 /* Anything compares with void *. void * compares with anything.
10825 Otherwise, the targets must be compatible
10826 and both must be object or both incomplete. */
10827 if (comp_target_types (location, type0, type1))
10828 result_type = common_pointer_type (type0, type1);
10829 else if (!addr_space_superset (as0, as1, &as_common))
10831 error_at (location, "comparison of pointers to "
10832 "disjoint address spaces");
10833 return error_mark_node;
10835 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10837 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10838 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10839 "comparison of %<void *%> with function pointer");
10841 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10843 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10844 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10845 "comparison of %<void *%> with function pointer");
10847 else
10848 /* Avoid warning about the volatile ObjC EH puts on decls. */
10849 if (!objc_ok)
10850 pedwarn (location, 0,
10851 "comparison of distinct pointer types lacks a cast");
10853 if (result_type == NULL_TREE)
10855 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10856 result_type = build_pointer_type
10857 (build_qualified_type (void_type_node, qual));
10860 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10862 result_type = type0;
10863 pedwarn (location, 0, "comparison between pointer and integer");
10865 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10867 result_type = type1;
10868 pedwarn (location, 0, "comparison between pointer and integer");
10870 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10871 || truth_value_p (TREE_CODE (orig_op0)))
10872 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10873 || truth_value_p (TREE_CODE (orig_op1))))
10874 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10875 break;
10877 case LE_EXPR:
10878 case GE_EXPR:
10879 case LT_EXPR:
10880 case GT_EXPR:
10881 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10883 tree intt;
10884 if (!vector_types_compatible_elements_p (type0, type1))
10886 error_at (location, "comparing vectors with different "
10887 "element types");
10888 return error_mark_node;
10891 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10893 error_at (location, "comparing vectors with different "
10894 "number of elements");
10895 return error_mark_node;
10898 /* Always construct signed integer vector type. */
10899 intt = c_common_type_for_size (GET_MODE_BITSIZE
10900 (TYPE_MODE (TREE_TYPE (type0))), 0);
10901 result_type = build_opaque_vector_type (intt,
10902 TYPE_VECTOR_SUBPARTS (type0));
10903 converted = 1;
10904 break;
10906 build_type = integer_type_node;
10907 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10908 || code0 == FIXED_POINT_TYPE)
10909 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10910 || code1 == FIXED_POINT_TYPE))
10911 short_compare = 1;
10912 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10914 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10915 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10916 addr_space_t as_common;
10918 if (comp_target_types (location, type0, type1))
10920 result_type = common_pointer_type (type0, type1);
10921 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10922 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10923 pedwarn (location, 0,
10924 "comparison of complete and incomplete pointers");
10925 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10926 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10927 "ordered comparisons of pointers to functions");
10928 else if (null_pointer_constant_p (orig_op0)
10929 || null_pointer_constant_p (orig_op1))
10930 warning_at (location, OPT_Wextra,
10931 "ordered comparison of pointer with null pointer");
10934 else if (!addr_space_superset (as0, as1, &as_common))
10936 error_at (location, "comparison of pointers to "
10937 "disjoint address spaces");
10938 return error_mark_node;
10940 else
10942 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10943 result_type = build_pointer_type
10944 (build_qualified_type (void_type_node, qual));
10945 pedwarn (location, 0,
10946 "comparison of distinct pointer types lacks a cast");
10949 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10951 result_type = type0;
10952 if (pedantic)
10953 pedwarn (location, OPT_Wpedantic,
10954 "ordered comparison of pointer with integer zero");
10955 else if (extra_warnings)
10956 warning_at (location, OPT_Wextra,
10957 "ordered comparison of pointer with integer zero");
10959 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10961 result_type = type1;
10962 if (pedantic)
10963 pedwarn (location, OPT_Wpedantic,
10964 "ordered comparison of pointer with integer zero");
10965 else if (extra_warnings)
10966 warning_at (location, OPT_Wextra,
10967 "ordered comparison of pointer with integer zero");
10969 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10971 result_type = type0;
10972 pedwarn (location, 0, "comparison between pointer and integer");
10974 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10976 result_type = type1;
10977 pedwarn (location, 0, "comparison between pointer and integer");
10979 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10980 || truth_value_p (TREE_CODE (orig_op0)))
10981 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10982 || truth_value_p (TREE_CODE (orig_op1))))
10983 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10984 break;
10986 default:
10987 gcc_unreachable ();
10990 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10991 return error_mark_node;
10993 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10994 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10995 || !vector_types_compatible_elements_p (type0, type1)))
10997 binary_op_error (location, code, type0, type1);
10998 return error_mark_node;
11001 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11002 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11004 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11005 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11007 bool first_complex = (code0 == COMPLEX_TYPE);
11008 bool second_complex = (code1 == COMPLEX_TYPE);
11009 int none_complex = (!first_complex && !second_complex);
11011 if (shorten || common || short_compare)
11013 result_type = c_common_type (type0, type1);
11014 do_warn_double_promotion (result_type, type0, type1,
11015 "implicit conversion from %qT to %qT "
11016 "to match other operand of binary "
11017 "expression",
11018 location);
11019 if (result_type == error_mark_node)
11020 return error_mark_node;
11023 if (first_complex != second_complex
11024 && (code == PLUS_EXPR
11025 || code == MINUS_EXPR
11026 || code == MULT_EXPR
11027 || (code == TRUNC_DIV_EXPR && first_complex))
11028 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11029 && flag_signed_zeros)
11031 /* An operation on mixed real/complex operands must be
11032 handled specially, but the language-independent code can
11033 more easily optimize the plain complex arithmetic if
11034 -fno-signed-zeros. */
11035 tree real_type = TREE_TYPE (result_type);
11036 tree real, imag;
11037 if (type0 != orig_type0 || type1 != orig_type1)
11039 gcc_assert (may_need_excess_precision && common);
11040 semantic_result_type = c_common_type (orig_type0, orig_type1);
11042 if (first_complex)
11044 if (TREE_TYPE (op0) != result_type)
11045 op0 = convert_and_check (location, result_type, op0);
11046 if (TREE_TYPE (op1) != real_type)
11047 op1 = convert_and_check (location, real_type, op1);
11049 else
11051 if (TREE_TYPE (op0) != real_type)
11052 op0 = convert_and_check (location, real_type, op0);
11053 if (TREE_TYPE (op1) != result_type)
11054 op1 = convert_and_check (location, result_type, op1);
11056 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11057 return error_mark_node;
11058 if (first_complex)
11060 op0 = c_save_expr (op0);
11061 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11062 op0, 1);
11063 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11064 op0, 1);
11065 switch (code)
11067 case MULT_EXPR:
11068 case TRUNC_DIV_EXPR:
11069 op1 = c_save_expr (op1);
11070 imag = build2 (resultcode, real_type, imag, op1);
11071 /* Fall through. */
11072 case PLUS_EXPR:
11073 case MINUS_EXPR:
11074 real = build2 (resultcode, real_type, real, op1);
11075 break;
11076 default:
11077 gcc_unreachable();
11080 else
11082 op1 = c_save_expr (op1);
11083 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11084 op1, 1);
11085 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11086 op1, 1);
11087 switch (code)
11089 case MULT_EXPR:
11090 op0 = c_save_expr (op0);
11091 imag = build2 (resultcode, real_type, op0, imag);
11092 /* Fall through. */
11093 case PLUS_EXPR:
11094 real = build2 (resultcode, real_type, op0, real);
11095 break;
11096 case MINUS_EXPR:
11097 real = build2 (resultcode, real_type, op0, real);
11098 imag = build1 (NEGATE_EXPR, real_type, imag);
11099 break;
11100 default:
11101 gcc_unreachable();
11104 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11105 goto return_build_binary_op;
11108 /* For certain operations (which identify themselves by shorten != 0)
11109 if both args were extended from the same smaller type,
11110 do the arithmetic in that type and then extend.
11112 shorten !=0 and !=1 indicates a bitwise operation.
11113 For them, this optimization is safe only if
11114 both args are zero-extended or both are sign-extended.
11115 Otherwise, we might change the result.
11116 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11117 but calculated in (unsigned short) it would be (unsigned short)-1. */
11119 if (shorten && none_complex)
11121 final_type = result_type;
11122 result_type = shorten_binary_op (result_type, op0, op1,
11123 shorten == -1);
11126 /* Shifts can be shortened if shifting right. */
11128 if (short_shift)
11130 int unsigned_arg;
11131 tree arg0 = get_narrower (op0, &unsigned_arg);
11133 final_type = result_type;
11135 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11136 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11138 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11139 && tree_int_cst_sgn (op1) > 0
11140 /* We can shorten only if the shift count is less than the
11141 number of bits in the smaller type size. */
11142 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11143 /* We cannot drop an unsigned shift after sign-extension. */
11144 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11146 /* Do an unsigned shift if the operand was zero-extended. */
11147 result_type
11148 = c_common_signed_or_unsigned_type (unsigned_arg,
11149 TREE_TYPE (arg0));
11150 /* Convert value-to-be-shifted to that type. */
11151 if (TREE_TYPE (op0) != result_type)
11152 op0 = convert (result_type, op0);
11153 converted = 1;
11157 /* Comparison operations are shortened too but differently.
11158 They identify themselves by setting short_compare = 1. */
11160 if (short_compare)
11162 /* Don't write &op0, etc., because that would prevent op0
11163 from being kept in a register.
11164 Instead, make copies of the our local variables and
11165 pass the copies by reference, then copy them back afterward. */
11166 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11167 enum tree_code xresultcode = resultcode;
11168 tree val
11169 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11170 &xresultcode);
11172 if (val != 0)
11174 ret = val;
11175 goto return_build_binary_op;
11178 op0 = xop0, op1 = xop1;
11179 converted = 1;
11180 resultcode = xresultcode;
11182 if (c_inhibit_evaluation_warnings == 0)
11184 bool op0_maybe_const = true;
11185 bool op1_maybe_const = true;
11186 tree orig_op0_folded, orig_op1_folded;
11188 if (in_late_binary_op)
11190 orig_op0_folded = orig_op0;
11191 orig_op1_folded = orig_op1;
11193 else
11195 /* Fold for the sake of possible warnings, as in
11196 build_conditional_expr. This requires the
11197 "original" values to be folded, not just op0 and
11198 op1. */
11199 c_inhibit_evaluation_warnings++;
11200 op0 = c_fully_fold (op0, require_constant_value,
11201 &op0_maybe_const);
11202 op1 = c_fully_fold (op1, require_constant_value,
11203 &op1_maybe_const);
11204 c_inhibit_evaluation_warnings--;
11205 orig_op0_folded = c_fully_fold (orig_op0,
11206 require_constant_value,
11207 NULL);
11208 orig_op1_folded = c_fully_fold (orig_op1,
11209 require_constant_value,
11210 NULL);
11213 if (warn_sign_compare)
11214 warn_for_sign_compare (location, orig_op0_folded,
11215 orig_op1_folded, op0, op1,
11216 result_type, resultcode);
11217 if (!in_late_binary_op && !int_operands)
11219 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11220 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11221 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11222 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11228 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11229 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11230 Then the expression will be built.
11231 It will be given type FINAL_TYPE if that is nonzero;
11232 otherwise, it will be given type RESULT_TYPE. */
11234 if (!result_type)
11236 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11237 return error_mark_node;
11240 if (build_type == NULL_TREE)
11242 build_type = result_type;
11243 if ((type0 != orig_type0 || type1 != orig_type1)
11244 && !boolean_op)
11246 gcc_assert (may_need_excess_precision && common);
11247 semantic_result_type = c_common_type (orig_type0, orig_type1);
11251 if (!converted)
11253 op0 = ep_convert_and_check (location, result_type, op0,
11254 semantic_result_type);
11255 op1 = ep_convert_and_check (location, result_type, op1,
11256 semantic_result_type);
11258 /* This can happen if one operand has a vector type, and the other
11259 has a different type. */
11260 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11261 return error_mark_node;
11264 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11265 | SANITIZE_FLOAT_DIVIDE))
11266 && do_ubsan_in_current_function ()
11267 && (doing_div_or_mod || doing_shift))
11269 /* OP0 and/or OP1 might have side-effects. */
11270 op0 = c_save_expr (op0);
11271 op1 = c_save_expr (op1);
11272 op0 = c_fully_fold (op0, false, NULL);
11273 op1 = c_fully_fold (op1, false, NULL);
11274 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11275 | SANITIZE_FLOAT_DIVIDE)))
11276 instrument_expr = ubsan_instrument_division (location, op0, op1);
11277 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11278 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11281 /* Treat expressions in initializers specially as they can't trap. */
11282 if (int_const_or_overflow)
11283 ret = (require_constant_value
11284 ? fold_build2_initializer_loc (location, resultcode, build_type,
11285 op0, op1)
11286 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11287 else
11288 ret = build2 (resultcode, build_type, op0, op1);
11289 if (final_type != 0)
11290 ret = convert (final_type, ret);
11292 return_build_binary_op:
11293 gcc_assert (ret != error_mark_node);
11294 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11295 ret = (int_operands
11296 ? note_integer_operands (ret)
11297 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11298 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11299 && !in_late_binary_op)
11300 ret = note_integer_operands (ret);
11301 if (semantic_result_type)
11302 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11303 protected_set_expr_location (ret, location);
11305 if (instrument_expr != NULL)
11306 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11307 instrument_expr, ret);
11309 return ret;
11313 /* Convert EXPR to be a truth-value, validating its type for this
11314 purpose. LOCATION is the source location for the expression. */
11316 tree
11317 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11319 bool int_const, int_operands;
11321 switch (TREE_CODE (TREE_TYPE (expr)))
11323 case ARRAY_TYPE:
11324 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11325 return error_mark_node;
11327 case RECORD_TYPE:
11328 error_at (location, "used struct type value where scalar is required");
11329 return error_mark_node;
11331 case UNION_TYPE:
11332 error_at (location, "used union type value where scalar is required");
11333 return error_mark_node;
11335 case VOID_TYPE:
11336 error_at (location, "void value not ignored as it ought to be");
11337 return error_mark_node;
11339 case FUNCTION_TYPE:
11340 gcc_unreachable ();
11342 case VECTOR_TYPE:
11343 error_at (location, "used vector type where scalar is required");
11344 return error_mark_node;
11346 default:
11347 break;
11350 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11351 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11352 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11354 expr = remove_c_maybe_const_expr (expr);
11355 expr = build2 (NE_EXPR, integer_type_node, expr,
11356 convert (TREE_TYPE (expr), integer_zero_node));
11357 expr = note_integer_operands (expr);
11359 else
11360 /* ??? Should we also give an error for vectors rather than leaving
11361 those to give errors later? */
11362 expr = c_common_truthvalue_conversion (location, expr);
11364 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11366 if (TREE_OVERFLOW (expr))
11367 return expr;
11368 else
11369 return note_integer_operands (expr);
11371 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11372 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11373 return expr;
11377 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11378 required. */
11380 tree
11381 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11383 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11385 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11386 /* Executing a compound literal inside a function reinitializes
11387 it. */
11388 if (!TREE_STATIC (decl))
11389 *se = true;
11390 return decl;
11392 else
11393 return expr;
11396 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11397 statement. LOC is the location of the OACC_PARALLEL. */
11399 tree
11400 c_finish_oacc_parallel (location_t loc, tree clauses, tree block)
11402 tree stmt;
11404 block = c_end_compound_stmt (loc, block, true);
11406 stmt = make_node (OACC_PARALLEL);
11407 TREE_TYPE (stmt) = void_type_node;
11408 OACC_PARALLEL_CLAUSES (stmt) = clauses;
11409 OACC_PARALLEL_BODY (stmt) = block;
11410 SET_EXPR_LOCATION (stmt, loc);
11412 return add_stmt (stmt);
11415 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11416 statement. LOC is the location of the OACC_KERNELS. */
11418 tree
11419 c_finish_oacc_kernels (location_t loc, tree clauses, tree block)
11421 tree stmt;
11423 block = c_end_compound_stmt (loc, block, true);
11425 stmt = make_node (OACC_KERNELS);
11426 TREE_TYPE (stmt) = void_type_node;
11427 OACC_KERNELS_CLAUSES (stmt) = clauses;
11428 OACC_KERNELS_BODY (stmt) = block;
11429 SET_EXPR_LOCATION (stmt, loc);
11431 return add_stmt (stmt);
11434 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11435 statement. LOC is the location of the OACC_DATA. */
11437 tree
11438 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11440 tree stmt;
11442 block = c_end_compound_stmt (loc, block, true);
11444 stmt = make_node (OACC_DATA);
11445 TREE_TYPE (stmt) = void_type_node;
11446 OACC_DATA_CLAUSES (stmt) = clauses;
11447 OACC_DATA_BODY (stmt) = block;
11448 SET_EXPR_LOCATION (stmt, loc);
11450 return add_stmt (stmt);
11453 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11455 tree
11456 c_begin_omp_parallel (void)
11458 tree block;
11460 keep_next_level ();
11461 block = c_begin_compound_stmt (true);
11463 return block;
11466 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11467 statement. LOC is the location of the OMP_PARALLEL. */
11469 tree
11470 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11472 tree stmt;
11474 block = c_end_compound_stmt (loc, block, true);
11476 stmt = make_node (OMP_PARALLEL);
11477 TREE_TYPE (stmt) = void_type_node;
11478 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11479 OMP_PARALLEL_BODY (stmt) = block;
11480 SET_EXPR_LOCATION (stmt, loc);
11482 return add_stmt (stmt);
11485 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11487 tree
11488 c_begin_omp_task (void)
11490 tree block;
11492 keep_next_level ();
11493 block = c_begin_compound_stmt (true);
11495 return block;
11498 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11499 statement. LOC is the location of the #pragma. */
11501 tree
11502 c_finish_omp_task (location_t loc, tree clauses, tree block)
11504 tree stmt;
11506 block = c_end_compound_stmt (loc, block, true);
11508 stmt = make_node (OMP_TASK);
11509 TREE_TYPE (stmt) = void_type_node;
11510 OMP_TASK_CLAUSES (stmt) = clauses;
11511 OMP_TASK_BODY (stmt) = block;
11512 SET_EXPR_LOCATION (stmt, loc);
11514 return add_stmt (stmt);
11517 /* Generate GOMP_cancel call for #pragma omp cancel. */
11519 void
11520 c_finish_omp_cancel (location_t loc, tree clauses)
11522 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11523 int mask = 0;
11524 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11525 mask = 1;
11526 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11527 mask = 2;
11528 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11529 mask = 4;
11530 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11531 mask = 8;
11532 else
11534 error_at (loc, "%<#pragma omp cancel must specify one of "
11535 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11536 "clauses");
11537 return;
11539 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11540 if (ifc != NULL_TREE)
11542 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11543 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11544 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11545 build_zero_cst (type));
11547 else
11548 ifc = boolean_true_node;
11549 tree stmt = build_call_expr_loc (loc, fn, 2,
11550 build_int_cst (integer_type_node, mask),
11551 ifc);
11552 add_stmt (stmt);
11555 /* Generate GOMP_cancellation_point call for
11556 #pragma omp cancellation point. */
11558 void
11559 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11561 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11562 int mask = 0;
11563 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11564 mask = 1;
11565 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11566 mask = 2;
11567 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11568 mask = 4;
11569 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11570 mask = 8;
11571 else
11573 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11574 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11575 "clauses");
11576 return;
11578 tree stmt = build_call_expr_loc (loc, fn, 1,
11579 build_int_cst (integer_type_node, mask));
11580 add_stmt (stmt);
11583 /* Helper function for handle_omp_array_sections. Called recursively
11584 to handle multiple array-section-subscripts. C is the clause,
11585 T current expression (initially OMP_CLAUSE_DECL), which is either
11586 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11587 expression if specified, TREE_VALUE length expression if specified,
11588 TREE_CHAIN is what it has been specified after, or some decl.
11589 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11590 set to true if any of the array-section-subscript could have length
11591 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11592 first array-section-subscript which is known not to have length
11593 of one. Given say:
11594 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11595 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11596 all are or may have length of 1, array-section-subscript [:2] is the
11597 first one knonwn not to have length 1. For array-section-subscript
11598 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11599 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11600 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11601 case though, as some lengths could be zero. */
11603 static tree
11604 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11605 bool &maybe_zero_len, unsigned int &first_non_one)
11607 tree ret, low_bound, length, type;
11608 if (TREE_CODE (t) != TREE_LIST)
11610 if (error_operand_p (t))
11611 return error_mark_node;
11612 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11614 if (DECL_P (t))
11615 error_at (OMP_CLAUSE_LOCATION (c),
11616 "%qD is not a variable in %qs clause", t,
11617 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11618 else
11619 error_at (OMP_CLAUSE_LOCATION (c),
11620 "%qE is not a variable in %qs clause", t,
11621 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11622 return error_mark_node;
11624 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11625 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11627 error_at (OMP_CLAUSE_LOCATION (c),
11628 "%qD is threadprivate variable in %qs clause", t,
11629 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11630 return error_mark_node;
11632 return t;
11635 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11636 maybe_zero_len, first_non_one);
11637 if (ret == error_mark_node || ret == NULL_TREE)
11638 return ret;
11640 type = TREE_TYPE (ret);
11641 low_bound = TREE_PURPOSE (t);
11642 length = TREE_VALUE (t);
11644 if (low_bound == error_mark_node || length == error_mark_node)
11645 return error_mark_node;
11647 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11649 error_at (OMP_CLAUSE_LOCATION (c),
11650 "low bound %qE of array section does not have integral type",
11651 low_bound);
11652 return error_mark_node;
11654 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11656 error_at (OMP_CLAUSE_LOCATION (c),
11657 "length %qE of array section does not have integral type",
11658 length);
11659 return error_mark_node;
11661 if (low_bound
11662 && TREE_CODE (low_bound) == INTEGER_CST
11663 && TYPE_PRECISION (TREE_TYPE (low_bound))
11664 > TYPE_PRECISION (sizetype))
11665 low_bound = fold_convert (sizetype, low_bound);
11666 if (length
11667 && TREE_CODE (length) == INTEGER_CST
11668 && TYPE_PRECISION (TREE_TYPE (length))
11669 > TYPE_PRECISION (sizetype))
11670 length = fold_convert (sizetype, length);
11671 if (low_bound == NULL_TREE)
11672 low_bound = integer_zero_node;
11674 if (length != NULL_TREE)
11676 if (!integer_nonzerop (length))
11677 maybe_zero_len = true;
11678 if (first_non_one == types.length ()
11679 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11680 first_non_one++;
11682 if (TREE_CODE (type) == ARRAY_TYPE)
11684 if (length == NULL_TREE
11685 && (TYPE_DOMAIN (type) == NULL_TREE
11686 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11688 error_at (OMP_CLAUSE_LOCATION (c),
11689 "for unknown bound array type length expression must "
11690 "be specified");
11691 return error_mark_node;
11693 if (TREE_CODE (low_bound) == INTEGER_CST
11694 && tree_int_cst_sgn (low_bound) == -1)
11696 error_at (OMP_CLAUSE_LOCATION (c),
11697 "negative low bound in array section in %qs clause",
11698 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11699 return error_mark_node;
11701 if (length != NULL_TREE
11702 && TREE_CODE (length) == INTEGER_CST
11703 && tree_int_cst_sgn (length) == -1)
11705 error_at (OMP_CLAUSE_LOCATION (c),
11706 "negative length in array section in %qs clause",
11707 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11708 return error_mark_node;
11710 if (TYPE_DOMAIN (type)
11711 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11712 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11713 == INTEGER_CST)
11715 tree size = size_binop (PLUS_EXPR,
11716 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11717 size_one_node);
11718 if (TREE_CODE (low_bound) == INTEGER_CST)
11720 if (tree_int_cst_lt (size, low_bound))
11722 error_at (OMP_CLAUSE_LOCATION (c),
11723 "low bound %qE above array section size "
11724 "in %qs clause", low_bound,
11725 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11726 return error_mark_node;
11728 if (tree_int_cst_equal (size, low_bound))
11729 maybe_zero_len = true;
11730 else if (length == NULL_TREE
11731 && first_non_one == types.length ()
11732 && tree_int_cst_equal
11733 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11734 low_bound))
11735 first_non_one++;
11737 else if (length == NULL_TREE)
11739 maybe_zero_len = true;
11740 if (first_non_one == types.length ())
11741 first_non_one++;
11743 if (length && TREE_CODE (length) == INTEGER_CST)
11745 if (tree_int_cst_lt (size, length))
11747 error_at (OMP_CLAUSE_LOCATION (c),
11748 "length %qE above array section size "
11749 "in %qs clause", length,
11750 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11751 return error_mark_node;
11753 if (TREE_CODE (low_bound) == INTEGER_CST)
11755 tree lbpluslen
11756 = size_binop (PLUS_EXPR,
11757 fold_convert (sizetype, low_bound),
11758 fold_convert (sizetype, length));
11759 if (TREE_CODE (lbpluslen) == INTEGER_CST
11760 && tree_int_cst_lt (size, lbpluslen))
11762 error_at (OMP_CLAUSE_LOCATION (c),
11763 "high bound %qE above array section size "
11764 "in %qs clause", lbpluslen,
11765 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11766 return error_mark_node;
11771 else if (length == NULL_TREE)
11773 maybe_zero_len = true;
11774 if (first_non_one == types.length ())
11775 first_non_one++;
11778 /* For [lb:] we will need to evaluate lb more than once. */
11779 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11781 tree lb = c_save_expr (low_bound);
11782 if (lb != low_bound)
11784 TREE_PURPOSE (t) = lb;
11785 low_bound = lb;
11789 else if (TREE_CODE (type) == POINTER_TYPE)
11791 if (length == NULL_TREE)
11793 error_at (OMP_CLAUSE_LOCATION (c),
11794 "for pointer type length expression must be specified");
11795 return error_mark_node;
11797 /* If there is a pointer type anywhere but in the very first
11798 array-section-subscript, the array section can't be contiguous. */
11799 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11800 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11802 error_at (OMP_CLAUSE_LOCATION (c),
11803 "array section is not contiguous in %qs clause",
11804 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11805 return error_mark_node;
11808 else
11810 error_at (OMP_CLAUSE_LOCATION (c),
11811 "%qE does not have pointer or array type", ret);
11812 return error_mark_node;
11814 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11815 types.safe_push (TREE_TYPE (ret));
11816 /* We will need to evaluate lb more than once. */
11817 tree lb = c_save_expr (low_bound);
11818 if (lb != low_bound)
11820 TREE_PURPOSE (t) = lb;
11821 low_bound = lb;
11823 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11824 return ret;
11827 /* Handle array sections for clause C. */
11829 static bool
11830 handle_omp_array_sections (tree c)
11832 bool maybe_zero_len = false;
11833 unsigned int first_non_one = 0;
11834 vec<tree> types = vNULL;
11835 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11836 maybe_zero_len, first_non_one);
11837 if (first == error_mark_node)
11839 types.release ();
11840 return true;
11842 if (first == NULL_TREE)
11844 types.release ();
11845 return false;
11847 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11849 tree t = OMP_CLAUSE_DECL (c);
11850 tree tem = NULL_TREE;
11851 types.release ();
11852 /* Need to evaluate side effects in the length expressions
11853 if any. */
11854 while (TREE_CODE (t) == TREE_LIST)
11856 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11858 if (tem == NULL_TREE)
11859 tem = TREE_VALUE (t);
11860 else
11861 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11862 TREE_VALUE (t), tem);
11864 t = TREE_CHAIN (t);
11866 if (tem)
11867 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11868 first = c_fully_fold (first, false, NULL);
11869 OMP_CLAUSE_DECL (c) = first;
11871 else
11873 unsigned int num = types.length (), i;
11874 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11875 tree condition = NULL_TREE;
11877 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11878 maybe_zero_len = true;
11880 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11881 t = TREE_CHAIN (t))
11883 tree low_bound = TREE_PURPOSE (t);
11884 tree length = TREE_VALUE (t);
11886 i--;
11887 if (low_bound
11888 && TREE_CODE (low_bound) == INTEGER_CST
11889 && TYPE_PRECISION (TREE_TYPE (low_bound))
11890 > TYPE_PRECISION (sizetype))
11891 low_bound = fold_convert (sizetype, low_bound);
11892 if (length
11893 && TREE_CODE (length) == INTEGER_CST
11894 && TYPE_PRECISION (TREE_TYPE (length))
11895 > TYPE_PRECISION (sizetype))
11896 length = fold_convert (sizetype, length);
11897 if (low_bound == NULL_TREE)
11898 low_bound = integer_zero_node;
11899 if (!maybe_zero_len && i > first_non_one)
11901 if (integer_nonzerop (low_bound))
11902 goto do_warn_noncontiguous;
11903 if (length != NULL_TREE
11904 && TREE_CODE (length) == INTEGER_CST
11905 && TYPE_DOMAIN (types[i])
11906 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11907 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11908 == INTEGER_CST)
11910 tree size;
11911 size = size_binop (PLUS_EXPR,
11912 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11913 size_one_node);
11914 if (!tree_int_cst_equal (length, size))
11916 do_warn_noncontiguous:
11917 error_at (OMP_CLAUSE_LOCATION (c),
11918 "array section is not contiguous in %qs "
11919 "clause",
11920 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11921 types.release ();
11922 return true;
11925 if (length != NULL_TREE
11926 && TREE_SIDE_EFFECTS (length))
11928 if (side_effects == NULL_TREE)
11929 side_effects = length;
11930 else
11931 side_effects = build2 (COMPOUND_EXPR,
11932 TREE_TYPE (side_effects),
11933 length, side_effects);
11936 else
11938 tree l;
11940 if (i > first_non_one && length && integer_nonzerop (length))
11941 continue;
11942 if (length)
11943 l = fold_convert (sizetype, length);
11944 else
11946 l = size_binop (PLUS_EXPR,
11947 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11948 size_one_node);
11949 l = size_binop (MINUS_EXPR, l,
11950 fold_convert (sizetype, low_bound));
11952 if (i > first_non_one)
11954 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11955 size_zero_node);
11956 if (condition == NULL_TREE)
11957 condition = l;
11958 else
11959 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11960 l, condition);
11962 else if (size == NULL_TREE)
11964 size = size_in_bytes (TREE_TYPE (types[i]));
11965 size = size_binop (MULT_EXPR, size, l);
11966 if (condition)
11967 size = fold_build3 (COND_EXPR, sizetype, condition,
11968 size, size_zero_node);
11970 else
11971 size = size_binop (MULT_EXPR, size, l);
11974 types.release ();
11975 if (side_effects)
11976 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11977 first = c_fully_fold (first, false, NULL);
11978 OMP_CLAUSE_DECL (c) = first;
11979 if (size)
11980 size = c_fully_fold (size, false, NULL);
11981 OMP_CLAUSE_SIZE (c) = size;
11982 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11983 return false;
11984 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
11985 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11986 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
11987 if (!c_mark_addressable (t))
11988 return false;
11989 OMP_CLAUSE_DECL (c2) = t;
11990 t = build_fold_addr_expr (first);
11991 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11992 tree ptr = OMP_CLAUSE_DECL (c2);
11993 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11994 ptr = build_fold_addr_expr (ptr);
11995 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11996 ptrdiff_type_node, t,
11997 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11998 ptrdiff_type_node, ptr));
11999 t = c_fully_fold (t, false, NULL);
12000 OMP_CLAUSE_SIZE (c2) = t;
12001 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12002 OMP_CLAUSE_CHAIN (c) = c2;
12004 return false;
12007 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12008 an inline call. But, remap
12009 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12010 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12012 static tree
12013 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12014 tree decl, tree placeholder)
12016 copy_body_data id;
12017 hash_map<tree, tree> decl_map;
12019 decl_map.put (omp_decl1, placeholder);
12020 decl_map.put (omp_decl2, decl);
12021 memset (&id, 0, sizeof (id));
12022 id.src_fn = DECL_CONTEXT (omp_decl1);
12023 id.dst_fn = current_function_decl;
12024 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12025 id.decl_map = &decl_map;
12027 id.copy_decl = copy_decl_no_change;
12028 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12029 id.transform_new_cfg = true;
12030 id.transform_return_to_modify = false;
12031 id.transform_lang_insert_block = NULL;
12032 id.eh_lp_nr = 0;
12033 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12034 return stmt;
12037 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12038 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12040 static tree
12041 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12043 if (*tp == (tree) data)
12044 return *tp;
12045 return NULL_TREE;
12048 /* For all elements of CLAUSES, validate them against their constraints.
12049 Remove any elements from the list that are invalid. */
12051 tree
12052 c_finish_omp_clauses (tree clauses)
12054 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12055 bitmap_head aligned_head;
12056 tree c, t, *pc;
12057 bool branch_seen = false;
12058 bool copyprivate_seen = false;
12059 tree *nowait_clause = NULL;
12061 bitmap_obstack_initialize (NULL);
12062 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12063 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12064 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12065 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12067 for (pc = &clauses, c = clauses; c ; c = *pc)
12069 bool remove = false;
12070 bool need_complete = false;
12071 bool need_implicitly_determined = false;
12073 switch (OMP_CLAUSE_CODE (c))
12075 case OMP_CLAUSE_SHARED:
12076 need_implicitly_determined = true;
12077 goto check_dup_generic;
12079 case OMP_CLAUSE_PRIVATE:
12080 need_complete = true;
12081 need_implicitly_determined = true;
12082 goto check_dup_generic;
12084 case OMP_CLAUSE_REDUCTION:
12085 need_implicitly_determined = true;
12086 t = OMP_CLAUSE_DECL (c);
12087 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12088 && (FLOAT_TYPE_P (TREE_TYPE (t))
12089 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
12091 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12092 const char *r_name = NULL;
12094 switch (r_code)
12096 case PLUS_EXPR:
12097 case MULT_EXPR:
12098 case MINUS_EXPR:
12099 break;
12100 case MIN_EXPR:
12101 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12102 r_name = "min";
12103 break;
12104 case MAX_EXPR:
12105 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12106 r_name = "max";
12107 break;
12108 case BIT_AND_EXPR:
12109 r_name = "&";
12110 break;
12111 case BIT_XOR_EXPR:
12112 r_name = "^";
12113 break;
12114 case BIT_IOR_EXPR:
12115 r_name = "|";
12116 break;
12117 case TRUTH_ANDIF_EXPR:
12118 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12119 r_name = "&&";
12120 break;
12121 case TRUTH_ORIF_EXPR:
12122 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12123 r_name = "||";
12124 break;
12125 default:
12126 gcc_unreachable ();
12128 if (r_name)
12130 error_at (OMP_CLAUSE_LOCATION (c),
12131 "%qE has invalid type for %<reduction(%s)%>",
12132 t, r_name);
12133 remove = true;
12134 break;
12137 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12139 error_at (OMP_CLAUSE_LOCATION (c),
12140 "user defined reduction not found for %qD", t);
12141 remove = true;
12142 break;
12144 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12146 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12147 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12148 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12149 VAR_DECL, NULL_TREE, type);
12150 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12151 DECL_ARTIFICIAL (placeholder) = 1;
12152 DECL_IGNORED_P (placeholder) = 1;
12153 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12154 c_mark_addressable (placeholder);
12155 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12156 c_mark_addressable (OMP_CLAUSE_DECL (c));
12157 OMP_CLAUSE_REDUCTION_MERGE (c)
12158 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12159 TREE_VEC_ELT (list, 0),
12160 TREE_VEC_ELT (list, 1),
12161 OMP_CLAUSE_DECL (c), placeholder);
12162 OMP_CLAUSE_REDUCTION_MERGE (c)
12163 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12164 void_type_node, NULL_TREE,
12165 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12166 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12167 if (TREE_VEC_LENGTH (list) == 6)
12169 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12170 c_mark_addressable (OMP_CLAUSE_DECL (c));
12171 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12172 c_mark_addressable (placeholder);
12173 tree init = TREE_VEC_ELT (list, 5);
12174 if (init == error_mark_node)
12175 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12176 OMP_CLAUSE_REDUCTION_INIT (c)
12177 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12178 TREE_VEC_ELT (list, 3),
12179 OMP_CLAUSE_DECL (c), placeholder);
12180 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12181 OMP_CLAUSE_REDUCTION_INIT (c)
12182 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12183 OMP_CLAUSE_REDUCTION_INIT (c));
12184 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12185 c_find_omp_placeholder_r,
12186 placeholder, NULL))
12187 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12189 else
12191 tree init;
12192 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12193 init = build_constructor (TREE_TYPE (t), NULL);
12194 else
12195 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12196 OMP_CLAUSE_REDUCTION_INIT (c)
12197 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12199 OMP_CLAUSE_REDUCTION_INIT (c)
12200 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12201 void_type_node, NULL_TREE,
12202 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12203 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12205 goto check_dup_generic;
12207 case OMP_CLAUSE_COPYPRIVATE:
12208 copyprivate_seen = true;
12209 if (nowait_clause)
12211 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12212 "%<nowait%> clause must not be used together "
12213 "with %<copyprivate%>");
12214 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12215 nowait_clause = NULL;
12217 goto check_dup_generic;
12219 case OMP_CLAUSE_COPYIN:
12220 t = OMP_CLAUSE_DECL (c);
12221 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12223 error_at (OMP_CLAUSE_LOCATION (c),
12224 "%qE must be %<threadprivate%> for %<copyin%>", t);
12225 remove = true;
12226 break;
12228 goto check_dup_generic;
12230 case OMP_CLAUSE_LINEAR:
12231 t = OMP_CLAUSE_DECL (c);
12232 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12233 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12235 error_at (OMP_CLAUSE_LOCATION (c),
12236 "linear clause applied to non-integral non-pointer "
12237 "variable with type %qT", TREE_TYPE (t));
12238 remove = true;
12239 break;
12241 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12243 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12244 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12245 OMP_CLAUSE_DECL (c), s);
12246 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12247 sizetype, s, OMP_CLAUSE_DECL (c));
12248 if (s == error_mark_node)
12249 s = size_one_node;
12250 OMP_CLAUSE_LINEAR_STEP (c) = s;
12252 else
12253 OMP_CLAUSE_LINEAR_STEP (c)
12254 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12255 goto check_dup_generic;
12257 check_dup_generic:
12258 t = OMP_CLAUSE_DECL (c);
12259 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12261 error_at (OMP_CLAUSE_LOCATION (c),
12262 "%qE is not a variable in clause %qs", t,
12263 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12264 remove = true;
12266 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12267 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12268 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12270 error_at (OMP_CLAUSE_LOCATION (c),
12271 "%qE appears more than once in data clauses", t);
12272 remove = true;
12274 else
12275 bitmap_set_bit (&generic_head, DECL_UID (t));
12276 break;
12278 case OMP_CLAUSE_FIRSTPRIVATE:
12279 t = OMP_CLAUSE_DECL (c);
12280 need_complete = true;
12281 need_implicitly_determined = true;
12282 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12284 error_at (OMP_CLAUSE_LOCATION (c),
12285 "%qE is not a variable in clause %<firstprivate%>", t);
12286 remove = true;
12288 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12289 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12291 error_at (OMP_CLAUSE_LOCATION (c),
12292 "%qE appears more than once in data clauses", t);
12293 remove = true;
12295 else
12296 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12297 break;
12299 case OMP_CLAUSE_LASTPRIVATE:
12300 t = OMP_CLAUSE_DECL (c);
12301 need_complete = true;
12302 need_implicitly_determined = true;
12303 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12305 error_at (OMP_CLAUSE_LOCATION (c),
12306 "%qE is not a variable in clause %<lastprivate%>", t);
12307 remove = true;
12309 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12310 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12312 error_at (OMP_CLAUSE_LOCATION (c),
12313 "%qE appears more than once in data clauses", t);
12314 remove = true;
12316 else
12317 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12318 break;
12320 case OMP_CLAUSE_ALIGNED:
12321 t = OMP_CLAUSE_DECL (c);
12322 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12324 error_at (OMP_CLAUSE_LOCATION (c),
12325 "%qE is not a variable in %<aligned%> clause", t);
12326 remove = true;
12328 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12329 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12331 error_at (OMP_CLAUSE_LOCATION (c),
12332 "%qE in %<aligned%> clause is neither a pointer nor "
12333 "an array", t);
12334 remove = true;
12336 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12338 error_at (OMP_CLAUSE_LOCATION (c),
12339 "%qE appears more than once in %<aligned%> clauses",
12341 remove = true;
12343 else
12344 bitmap_set_bit (&aligned_head, DECL_UID (t));
12345 break;
12347 case OMP_CLAUSE_DEPEND:
12348 t = OMP_CLAUSE_DECL (c);
12349 if (TREE_CODE (t) == TREE_LIST)
12351 if (handle_omp_array_sections (c))
12352 remove = true;
12353 break;
12355 if (t == error_mark_node)
12356 remove = true;
12357 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12359 error_at (OMP_CLAUSE_LOCATION (c),
12360 "%qE is not a variable in %<depend%> clause", t);
12361 remove = true;
12363 else if (!c_mark_addressable (t))
12364 remove = true;
12365 break;
12367 case OMP_CLAUSE_MAP:
12368 case OMP_CLAUSE_TO:
12369 case OMP_CLAUSE_FROM:
12370 case OMP_CLAUSE__CACHE_:
12371 t = OMP_CLAUSE_DECL (c);
12372 if (TREE_CODE (t) == TREE_LIST)
12374 if (handle_omp_array_sections (c))
12375 remove = true;
12376 else
12378 t = OMP_CLAUSE_DECL (c);
12379 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12381 error_at (OMP_CLAUSE_LOCATION (c),
12382 "array section does not have mappable type "
12383 "in %qs clause",
12384 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12385 remove = true;
12388 break;
12390 if (t == error_mark_node)
12391 remove = true;
12392 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12394 error_at (OMP_CLAUSE_LOCATION (c),
12395 "%qE is not a variable in %qs clause", t,
12396 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12397 remove = true;
12399 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12401 error_at (OMP_CLAUSE_LOCATION (c),
12402 "%qD is threadprivate variable in %qs clause", t,
12403 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12404 remove = true;
12406 else if (!c_mark_addressable (t))
12407 remove = true;
12408 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12409 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12410 || (OMP_CLAUSE_MAP_KIND (c)
12411 == GOMP_MAP_FORCE_DEVICEPTR)))
12412 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12414 error_at (OMP_CLAUSE_LOCATION (c),
12415 "%qD does not have a mappable type in %qs clause", t,
12416 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12417 remove = true;
12419 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12421 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12422 error ("%qD appears more than once in motion clauses", t);
12423 else
12424 error ("%qD appears more than once in map clauses", t);
12425 remove = true;
12427 else
12428 bitmap_set_bit (&generic_head, DECL_UID (t));
12429 break;
12431 case OMP_CLAUSE_UNIFORM:
12432 t = OMP_CLAUSE_DECL (c);
12433 if (TREE_CODE (t) != PARM_DECL)
12435 if (DECL_P (t))
12436 error_at (OMP_CLAUSE_LOCATION (c),
12437 "%qD is not an argument in %<uniform%> clause", t);
12438 else
12439 error_at (OMP_CLAUSE_LOCATION (c),
12440 "%qE is not an argument in %<uniform%> clause", t);
12441 remove = true;
12442 break;
12444 goto check_dup_generic;
12446 case OMP_CLAUSE_NOWAIT:
12447 if (copyprivate_seen)
12449 error_at (OMP_CLAUSE_LOCATION (c),
12450 "%<nowait%> clause must not be used together "
12451 "with %<copyprivate%>");
12452 remove = true;
12453 break;
12455 nowait_clause = pc;
12456 pc = &OMP_CLAUSE_CHAIN (c);
12457 continue;
12459 case OMP_CLAUSE_IF:
12460 case OMP_CLAUSE_NUM_THREADS:
12461 case OMP_CLAUSE_NUM_TEAMS:
12462 case OMP_CLAUSE_THREAD_LIMIT:
12463 case OMP_CLAUSE_SCHEDULE:
12464 case OMP_CLAUSE_ORDERED:
12465 case OMP_CLAUSE_DEFAULT:
12466 case OMP_CLAUSE_UNTIED:
12467 case OMP_CLAUSE_COLLAPSE:
12468 case OMP_CLAUSE_FINAL:
12469 case OMP_CLAUSE_MERGEABLE:
12470 case OMP_CLAUSE_SAFELEN:
12471 case OMP_CLAUSE_SIMDLEN:
12472 case OMP_CLAUSE_DEVICE:
12473 case OMP_CLAUSE_DIST_SCHEDULE:
12474 case OMP_CLAUSE_PARALLEL:
12475 case OMP_CLAUSE_FOR:
12476 case OMP_CLAUSE_SECTIONS:
12477 case OMP_CLAUSE_TASKGROUP:
12478 case OMP_CLAUSE_PROC_BIND:
12479 case OMP_CLAUSE__CILK_FOR_COUNT_:
12480 case OMP_CLAUSE_NUM_GANGS:
12481 case OMP_CLAUSE_NUM_WORKERS:
12482 case OMP_CLAUSE_VECTOR_LENGTH:
12483 case OMP_CLAUSE_ASYNC:
12484 case OMP_CLAUSE_WAIT:
12485 case OMP_CLAUSE_AUTO:
12486 case OMP_CLAUSE_SEQ:
12487 case OMP_CLAUSE_GANG:
12488 case OMP_CLAUSE_WORKER:
12489 case OMP_CLAUSE_VECTOR:
12490 pc = &OMP_CLAUSE_CHAIN (c);
12491 continue;
12493 case OMP_CLAUSE_INBRANCH:
12494 case OMP_CLAUSE_NOTINBRANCH:
12495 if (branch_seen)
12497 error_at (OMP_CLAUSE_LOCATION (c),
12498 "%<inbranch%> clause is incompatible with "
12499 "%<notinbranch%>");
12500 remove = true;
12501 break;
12503 branch_seen = true;
12504 pc = &OMP_CLAUSE_CHAIN (c);
12505 continue;
12507 default:
12508 gcc_unreachable ();
12511 if (!remove)
12513 t = OMP_CLAUSE_DECL (c);
12515 if (need_complete)
12517 t = require_complete_type (t);
12518 if (t == error_mark_node)
12519 remove = true;
12522 if (need_implicitly_determined)
12524 const char *share_name = NULL;
12526 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12527 share_name = "threadprivate";
12528 else switch (c_omp_predetermined_sharing (t))
12530 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12531 break;
12532 case OMP_CLAUSE_DEFAULT_SHARED:
12533 /* const vars may be specified in firstprivate clause. */
12534 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12535 && TREE_READONLY (t))
12536 break;
12537 share_name = "shared";
12538 break;
12539 case OMP_CLAUSE_DEFAULT_PRIVATE:
12540 share_name = "private";
12541 break;
12542 default:
12543 gcc_unreachable ();
12545 if (share_name)
12547 error_at (OMP_CLAUSE_LOCATION (c),
12548 "%qE is predetermined %qs for %qs",
12549 t, share_name,
12550 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12551 remove = true;
12556 if (remove)
12557 *pc = OMP_CLAUSE_CHAIN (c);
12558 else
12559 pc = &OMP_CLAUSE_CHAIN (c);
12562 bitmap_obstack_release (NULL);
12563 return clauses;
12566 /* Create a transaction node. */
12568 tree
12569 c_finish_transaction (location_t loc, tree block, int flags)
12571 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12572 if (flags & TM_STMT_ATTR_OUTER)
12573 TRANSACTION_EXPR_OUTER (stmt) = 1;
12574 if (flags & TM_STMT_ATTR_RELAXED)
12575 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12576 return add_stmt (stmt);
12579 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12580 down to the element type of an array. */
12582 tree
12583 c_build_qualified_type (tree type, int type_quals)
12585 if (type == error_mark_node)
12586 return type;
12588 if (TREE_CODE (type) == ARRAY_TYPE)
12590 tree t;
12591 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12592 type_quals);
12594 /* See if we already have an identically qualified type. */
12595 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12597 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12598 && TYPE_NAME (t) == TYPE_NAME (type)
12599 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12600 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12601 TYPE_ATTRIBUTES (type)))
12602 break;
12604 if (!t)
12606 tree domain = TYPE_DOMAIN (type);
12608 t = build_variant_type_copy (type);
12609 TREE_TYPE (t) = element_type;
12611 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12612 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12613 SET_TYPE_STRUCTURAL_EQUALITY (t);
12614 else if (TYPE_CANONICAL (element_type) != element_type
12615 || (domain && TYPE_CANONICAL (domain) != domain))
12617 tree unqualified_canon
12618 = build_array_type (TYPE_CANONICAL (element_type),
12619 domain? TYPE_CANONICAL (domain)
12620 : NULL_TREE);
12621 TYPE_CANONICAL (t)
12622 = c_build_qualified_type (unqualified_canon, type_quals);
12624 else
12625 TYPE_CANONICAL (t) = t;
12627 return t;
12630 /* A restrict-qualified pointer type must be a pointer to object or
12631 incomplete type. Note that the use of POINTER_TYPE_P also allows
12632 REFERENCE_TYPEs, which is appropriate for C++. */
12633 if ((type_quals & TYPE_QUAL_RESTRICT)
12634 && (!POINTER_TYPE_P (type)
12635 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12637 error ("invalid use of %<restrict%>");
12638 type_quals &= ~TYPE_QUAL_RESTRICT;
12641 return build_qualified_type (type, type_quals);
12644 /* Build a VA_ARG_EXPR for the C parser. */
12646 tree
12647 c_build_va_arg (location_t loc, tree expr, tree type)
12649 if (error_operand_p (type))
12650 return error_mark_node;
12651 else if (!COMPLETE_TYPE_P (type))
12653 error_at (loc, "second argument to %<va_arg%> is of incomplete "
12654 "type %qT", type);
12655 return error_mark_node;
12657 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12658 warning_at (loc, OPT_Wc___compat,
12659 "C++ requires promoted type, not enum type, in %<va_arg%>");
12660 return build_va_arg (loc, expr, type);
12663 /* Return truthvalue of whether T1 is the same tree structure as T2.
12664 Return 1 if they are the same. Return 0 if they are different. */
12666 bool
12667 c_tree_equal (tree t1, tree t2)
12669 enum tree_code code1, code2;
12671 if (t1 == t2)
12672 return true;
12673 if (!t1 || !t2)
12674 return false;
12676 for (code1 = TREE_CODE (t1);
12677 CONVERT_EXPR_CODE_P (code1)
12678 || code1 == NON_LVALUE_EXPR;
12679 code1 = TREE_CODE (t1))
12680 t1 = TREE_OPERAND (t1, 0);
12681 for (code2 = TREE_CODE (t2);
12682 CONVERT_EXPR_CODE_P (code2)
12683 || code2 == NON_LVALUE_EXPR;
12684 code2 = TREE_CODE (t2))
12685 t2 = TREE_OPERAND (t2, 0);
12687 /* They might have become equal now. */
12688 if (t1 == t2)
12689 return true;
12691 if (code1 != code2)
12692 return false;
12694 switch (code1)
12696 case INTEGER_CST:
12697 return wi::eq_p (t1, t2);
12699 case REAL_CST:
12700 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12702 case STRING_CST:
12703 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12704 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12705 TREE_STRING_LENGTH (t1));
12707 case FIXED_CST:
12708 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12709 TREE_FIXED_CST (t2));
12711 case COMPLEX_CST:
12712 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12713 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12715 case VECTOR_CST:
12716 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12718 case CONSTRUCTOR:
12719 /* We need to do this when determining whether or not two
12720 non-type pointer to member function template arguments
12721 are the same. */
12722 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12723 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12724 return false;
12726 tree field, value;
12727 unsigned int i;
12728 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12730 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12731 if (!c_tree_equal (field, elt2->index)
12732 || !c_tree_equal (value, elt2->value))
12733 return false;
12736 return true;
12738 case TREE_LIST:
12739 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12740 return false;
12741 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12742 return false;
12743 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12745 case SAVE_EXPR:
12746 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12748 case CALL_EXPR:
12750 tree arg1, arg2;
12751 call_expr_arg_iterator iter1, iter2;
12752 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12753 return false;
12754 for (arg1 = first_call_expr_arg (t1, &iter1),
12755 arg2 = first_call_expr_arg (t2, &iter2);
12756 arg1 && arg2;
12757 arg1 = next_call_expr_arg (&iter1),
12758 arg2 = next_call_expr_arg (&iter2))
12759 if (!c_tree_equal (arg1, arg2))
12760 return false;
12761 if (arg1 || arg2)
12762 return false;
12763 return true;
12766 case TARGET_EXPR:
12768 tree o1 = TREE_OPERAND (t1, 0);
12769 tree o2 = TREE_OPERAND (t2, 0);
12771 /* Special case: if either target is an unallocated VAR_DECL,
12772 it means that it's going to be unified with whatever the
12773 TARGET_EXPR is really supposed to initialize, so treat it
12774 as being equivalent to anything. */
12775 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12776 && !DECL_RTL_SET_P (o1))
12777 /*Nop*/;
12778 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12779 && !DECL_RTL_SET_P (o2))
12780 /*Nop*/;
12781 else if (!c_tree_equal (o1, o2))
12782 return false;
12784 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12787 case COMPONENT_REF:
12788 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12789 return false;
12790 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12792 case PARM_DECL:
12793 case VAR_DECL:
12794 case CONST_DECL:
12795 case FIELD_DECL:
12796 case FUNCTION_DECL:
12797 case IDENTIFIER_NODE:
12798 case SSA_NAME:
12799 return false;
12801 case TREE_VEC:
12803 unsigned ix;
12804 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12805 return false;
12806 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12807 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12808 TREE_VEC_ELT (t2, ix)))
12809 return false;
12810 return true;
12813 default:
12814 break;
12817 switch (TREE_CODE_CLASS (code1))
12819 case tcc_unary:
12820 case tcc_binary:
12821 case tcc_comparison:
12822 case tcc_expression:
12823 case tcc_vl_exp:
12824 case tcc_reference:
12825 case tcc_statement:
12827 int i, n = TREE_OPERAND_LENGTH (t1);
12829 switch (code1)
12831 case PREINCREMENT_EXPR:
12832 case PREDECREMENT_EXPR:
12833 case POSTINCREMENT_EXPR:
12834 case POSTDECREMENT_EXPR:
12835 n = 1;
12836 break;
12837 case ARRAY_REF:
12838 n = 2;
12839 break;
12840 default:
12841 break;
12844 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12845 && n != TREE_OPERAND_LENGTH (t2))
12846 return false;
12848 for (i = 0; i < n; ++i)
12849 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12850 return false;
12852 return true;
12855 case tcc_type:
12856 return comptypes (t1, t2);
12857 default:
12858 gcc_unreachable ();
12860 /* We can get here with --disable-checking. */
12861 return false;
12864 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12865 spawn-helper and BODY is the newly created body for FNDECL. */
12867 void
12868 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12870 tree list = alloc_stmt_list ();
12871 tree frame = make_cilk_frame (fndecl);
12872 tree dtor = create_cilk_function_exit (frame, false, true);
12873 add_local_decl (cfun, frame);
12875 DECL_SAVED_TREE (fndecl) = list;
12876 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12877 frame);
12878 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12879 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12881 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12882 append_to_statement_list (detach_expr, &body_list);
12884 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12885 body = fold_build_cleanup_point_expr (void_type_node, body);
12887 append_to_statement_list (body, &body_list);
12888 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12889 body_list, dtor), &list);