2014-12-20 Martin Uecker <uecker@eecs.berkeley.edu>
[official-gcc.git] / gcc / c / c-typeck.c
blobabd452aed39cf05e262db2054811bf0338af7863
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "stor-layout.h"
32 #include "trans-mem.h"
33 #include "varasm.h"
34 #include "stmt.h"
35 #include "langhooks.h"
36 #include "c-tree.h"
37 #include "c-lang.h"
38 #include "flags.h"
39 #include "intl.h"
40 #include "target.h"
41 #include "tree-iterator.h"
42 #include "bitmap.h"
43 #include "predict.h"
44 #include "vec.h"
45 #include "hashtab.h"
46 #include "hash-set.h"
47 #include "machmode.h"
48 #include "hard-reg-set.h"
49 #include "input.h"
50 #include "function.h"
51 #include "gimple-expr.h"
52 #include "gimplify.h"
53 #include "tree-inline.h"
54 #include "omp-low.h"
55 #include "c-family/c-objc.h"
56 #include "c-family/c-common.h"
57 #include "c-family/c-ubsan.h"
58 #include "cilk.h"
59 #include "wide-int.h"
61 /* Possible cases of implicit bad conversions. Used to select
62 diagnostic messages in convert_for_assignment. */
63 enum impl_conv {
64 ic_argpass,
65 ic_assign,
66 ic_init,
67 ic_return
70 /* The level of nesting inside "__alignof__". */
71 int in_alignof;
73 /* The level of nesting inside "sizeof". */
74 int in_sizeof;
76 /* The level of nesting inside "typeof". */
77 int in_typeof;
79 /* The argument of last parsed sizeof expression, only to be tested
80 if expr.original_code == SIZEOF_EXPR. */
81 tree c_last_sizeof_arg;
83 /* Nonzero if we might need to print a "missing braces around
84 initializer" message within this initializer. */
85 static int found_missing_braces;
87 static int require_constant_value;
88 static int require_constant_elements;
90 static bool null_pointer_constant_p (const_tree);
91 static tree qualify_type (tree, tree);
92 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
93 bool *);
94 static int comp_target_types (location_t, tree, tree);
95 static int function_types_compatible_p (const_tree, const_tree, bool *,
96 bool *);
97 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
98 static tree lookup_field (tree, tree);
99 static int convert_arguments (location_t, vec<location_t>, tree,
100 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
101 tree);
102 static tree pointer_diff (location_t, tree, tree);
103 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
104 enum impl_conv, bool, tree, tree, int);
105 static tree valid_compound_expr_initializer (tree, tree);
106 static void push_string (const char *);
107 static void push_member_name (tree);
108 static int spelling_length (void);
109 static char *print_spelling (char *);
110 static void warning_init (location_t, int, const char *);
111 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
112 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
113 bool, struct obstack *);
114 static void output_pending_init_elements (int, struct obstack *);
115 static int set_designator (location_t, int, struct obstack *);
116 static void push_range_stack (tree, struct obstack *);
117 static void add_pending_init (location_t, tree, tree, tree, bool,
118 struct obstack *);
119 static void set_nonincremental_init (struct obstack *);
120 static void set_nonincremental_init_from_string (tree, struct obstack *);
121 static tree find_init_member (tree, struct obstack *);
122 static void readonly_warning (tree, enum lvalue_use);
123 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
124 static void record_maybe_used_decl (tree);
125 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
127 /* Return true if EXP is a null pointer constant, false otherwise. */
129 static bool
130 null_pointer_constant_p (const_tree expr)
132 /* This should really operate on c_expr structures, but they aren't
133 yet available everywhere required. */
134 tree type = TREE_TYPE (expr);
135 return (TREE_CODE (expr) == INTEGER_CST
136 && !TREE_OVERFLOW (expr)
137 && integer_zerop (expr)
138 && (INTEGRAL_TYPE_P (type)
139 || (TREE_CODE (type) == POINTER_TYPE
140 && VOID_TYPE_P (TREE_TYPE (type))
141 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
144 /* EXPR may appear in an unevaluated part of an integer constant
145 expression, but not in an evaluated part. Wrap it in a
146 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
147 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
149 static tree
150 note_integer_operands (tree expr)
152 tree ret;
153 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
155 ret = copy_node (expr);
156 TREE_OVERFLOW (ret) = 1;
158 else
160 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
161 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
163 return ret;
166 /* Having checked whether EXPR may appear in an unevaluated part of an
167 integer constant expression and found that it may, remove any
168 C_MAYBE_CONST_EXPR noting this fact and return the resulting
169 expression. */
171 static inline tree
172 remove_c_maybe_const_expr (tree expr)
174 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
175 return C_MAYBE_CONST_EXPR_EXPR (expr);
176 else
177 return expr;
180 \f/* This is a cache to hold if two types are compatible or not. */
182 struct tagged_tu_seen_cache {
183 const struct tagged_tu_seen_cache * next;
184 const_tree t1;
185 const_tree t2;
186 /* The return value of tagged_types_tu_compatible_p if we had seen
187 these two types already. */
188 int val;
191 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
192 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
194 /* Do `exp = require_complete_type (exp);' to make sure exp
195 does not have an incomplete type. (That includes void types.) */
197 tree
198 require_complete_type (tree value)
200 tree type = TREE_TYPE (value);
202 if (error_operand_p (value))
203 return error_mark_node;
205 /* First, detect a valid value with a complete type. */
206 if (COMPLETE_TYPE_P (type))
207 return value;
209 c_incomplete_type_error (value, type);
210 return error_mark_node;
213 /* Print an error message for invalid use of an incomplete type.
214 VALUE is the expression that was used (or 0 if that isn't known)
215 and TYPE is the type that was invalid. */
217 void
218 c_incomplete_type_error (const_tree value, const_tree type)
220 const char *type_code_string;
222 /* Avoid duplicate error message. */
223 if (TREE_CODE (type) == ERROR_MARK)
224 return;
226 if (value != 0 && (TREE_CODE (value) == VAR_DECL
227 || TREE_CODE (value) == PARM_DECL))
228 error ("%qD has an incomplete type", value);
229 else
231 retry:
232 /* We must print an error message. Be clever about what it says. */
234 switch (TREE_CODE (type))
236 case RECORD_TYPE:
237 type_code_string = "struct";
238 break;
240 case UNION_TYPE:
241 type_code_string = "union";
242 break;
244 case ENUMERAL_TYPE:
245 type_code_string = "enum";
246 break;
248 case VOID_TYPE:
249 error ("invalid use of void expression");
250 return;
252 case ARRAY_TYPE:
253 if (TYPE_DOMAIN (type))
255 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
257 error ("invalid use of flexible array member");
258 return;
260 type = TREE_TYPE (type);
261 goto retry;
263 error ("invalid use of array with unspecified bounds");
264 return;
266 default:
267 gcc_unreachable ();
270 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
271 error ("invalid use of undefined type %<%s %E%>",
272 type_code_string, TYPE_NAME (type));
273 else
274 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
275 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
279 /* Given a type, apply default promotions wrt unnamed function
280 arguments and return the new type. */
282 tree
283 c_type_promotes_to (tree type)
285 tree ret = NULL_TREE;
287 if (TYPE_MAIN_VARIANT (type) == float_type_node)
288 ret = double_type_node;
289 else if (c_promoting_integer_type_p (type))
291 /* Preserve unsignedness if not really getting any wider. */
292 if (TYPE_UNSIGNED (type)
293 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
294 ret = unsigned_type_node;
295 else
296 ret = integer_type_node;
299 if (ret != NULL_TREE)
300 return (TYPE_ATOMIC (type)
301 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
302 : ret);
304 return type;
307 /* Return true if between two named address spaces, whether there is a superset
308 named address space that encompasses both address spaces. If there is a
309 superset, return which address space is the superset. */
311 static bool
312 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
314 if (as1 == as2)
316 *common = as1;
317 return true;
319 else if (targetm.addr_space.subset_p (as1, as2))
321 *common = as2;
322 return true;
324 else if (targetm.addr_space.subset_p (as2, as1))
326 *common = as1;
327 return true;
329 else
330 return false;
333 /* Return a variant of TYPE which has all the type qualifiers of LIKE
334 as well as those of TYPE. */
336 static tree
337 qualify_type (tree type, tree like)
339 addr_space_t as_type = TYPE_ADDR_SPACE (type);
340 addr_space_t as_like = TYPE_ADDR_SPACE (like);
341 addr_space_t as_common;
343 /* If the two named address spaces are different, determine the common
344 superset address space. If there isn't one, raise an error. */
345 if (!addr_space_superset (as_type, as_like, &as_common))
347 as_common = as_type;
348 error ("%qT and %qT are in disjoint named address spaces",
349 type, like);
352 return c_build_qualified_type (type,
353 TYPE_QUALS_NO_ADDR_SPACE (type)
354 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
355 | ENCODE_QUAL_ADDR_SPACE (as_common));
358 /* Return true iff the given tree T is a variable length array. */
360 bool
361 c_vla_type_p (const_tree t)
363 if (TREE_CODE (t) == ARRAY_TYPE
364 && C_TYPE_VARIABLE_SIZE (t))
365 return true;
366 return false;
369 /* Return the composite type of two compatible types.
371 We assume that comptypes has already been done and returned
372 nonzero; if that isn't so, this may crash. In particular, we
373 assume that qualifiers match. */
375 tree
376 composite_type (tree t1, tree t2)
378 enum tree_code code1;
379 enum tree_code code2;
380 tree attributes;
382 /* Save time if the two types are the same. */
384 if (t1 == t2) return t1;
386 /* If one type is nonsense, use the other. */
387 if (t1 == error_mark_node)
388 return t2;
389 if (t2 == error_mark_node)
390 return t1;
392 code1 = TREE_CODE (t1);
393 code2 = TREE_CODE (t2);
395 /* Merge the attributes. */
396 attributes = targetm.merge_type_attributes (t1, t2);
398 /* If one is an enumerated type and the other is the compatible
399 integer type, the composite type might be either of the two
400 (DR#013 question 3). For consistency, use the enumerated type as
401 the composite type. */
403 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
404 return t1;
405 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
406 return t2;
408 gcc_assert (code1 == code2);
410 switch (code1)
412 case POINTER_TYPE:
413 /* For two pointers, do this recursively on the target type. */
415 tree pointed_to_1 = TREE_TYPE (t1);
416 tree pointed_to_2 = TREE_TYPE (t2);
417 tree target = composite_type (pointed_to_1, pointed_to_2);
418 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
419 t1 = build_type_attribute_variant (t1, attributes);
420 return qualify_type (t1, t2);
423 case ARRAY_TYPE:
425 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
426 int quals;
427 tree unqual_elt;
428 tree d1 = TYPE_DOMAIN (t1);
429 tree d2 = TYPE_DOMAIN (t2);
430 bool d1_variable, d2_variable;
431 bool d1_zero, d2_zero;
432 bool t1_complete, t2_complete;
434 /* We should not have any type quals on arrays at all. */
435 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
436 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
438 t1_complete = COMPLETE_TYPE_P (t1);
439 t2_complete = COMPLETE_TYPE_P (t2);
441 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
442 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
444 d1_variable = (!d1_zero
445 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
446 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
447 d2_variable = (!d2_zero
448 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
449 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
450 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
451 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
453 /* Save space: see if the result is identical to one of the args. */
454 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
455 && (d2_variable || d2_zero || !d1_variable))
456 return build_type_attribute_variant (t1, attributes);
457 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
458 && (d1_variable || d1_zero || !d2_variable))
459 return build_type_attribute_variant (t2, attributes);
461 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
462 return build_type_attribute_variant (t1, attributes);
463 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
464 return build_type_attribute_variant (t2, attributes);
466 /* Merge the element types, and have a size if either arg has
467 one. We may have qualifiers on the element types. To set
468 up TYPE_MAIN_VARIANT correctly, we need to form the
469 composite of the unqualified types and add the qualifiers
470 back at the end. */
471 quals = TYPE_QUALS (strip_array_types (elt));
472 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
473 t1 = build_array_type (unqual_elt,
474 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
475 && (d2_variable
476 || d2_zero
477 || !d1_variable))
478 ? t1
479 : t2));
480 /* Ensure a composite type involving a zero-length array type
481 is a zero-length type not an incomplete type. */
482 if (d1_zero && d2_zero
483 && (t1_complete || t2_complete)
484 && !COMPLETE_TYPE_P (t1))
486 TYPE_SIZE (t1) = bitsize_zero_node;
487 TYPE_SIZE_UNIT (t1) = size_zero_node;
489 t1 = c_build_qualified_type (t1, quals);
490 return build_type_attribute_variant (t1, attributes);
493 case ENUMERAL_TYPE:
494 case RECORD_TYPE:
495 case UNION_TYPE:
496 if (attributes != NULL)
498 /* Try harder not to create a new aggregate type. */
499 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
500 return t1;
501 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
502 return t2;
504 return build_type_attribute_variant (t1, attributes);
506 case FUNCTION_TYPE:
507 /* Function types: prefer the one that specified arg types.
508 If both do, merge the arg types. Also merge the return types. */
510 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
511 tree p1 = TYPE_ARG_TYPES (t1);
512 tree p2 = TYPE_ARG_TYPES (t2);
513 int len;
514 tree newargs, n;
515 int i;
517 /* Save space: see if the result is identical to one of the args. */
518 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
519 return build_type_attribute_variant (t1, attributes);
520 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
521 return build_type_attribute_variant (t2, attributes);
523 /* Simple way if one arg fails to specify argument types. */
524 if (TYPE_ARG_TYPES (t1) == 0)
526 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
527 t1 = build_type_attribute_variant (t1, attributes);
528 return qualify_type (t1, t2);
530 if (TYPE_ARG_TYPES (t2) == 0)
532 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
533 t1 = build_type_attribute_variant (t1, attributes);
534 return qualify_type (t1, t2);
537 /* If both args specify argument types, we must merge the two
538 lists, argument by argument. */
540 len = list_length (p1);
541 newargs = 0;
543 for (i = 0; i < len; i++)
544 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
546 n = newargs;
548 for (; p1;
549 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
551 /* A null type means arg type is not specified.
552 Take whatever the other function type has. */
553 if (TREE_VALUE (p1) == 0)
555 TREE_VALUE (n) = TREE_VALUE (p2);
556 goto parm_done;
558 if (TREE_VALUE (p2) == 0)
560 TREE_VALUE (n) = TREE_VALUE (p1);
561 goto parm_done;
564 /* Given wait (union {union wait *u; int *i} *)
565 and wait (union wait *),
566 prefer union wait * as type of parm. */
567 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
568 && TREE_VALUE (p1) != TREE_VALUE (p2))
570 tree memb;
571 tree mv2 = TREE_VALUE (p2);
572 if (mv2 && mv2 != error_mark_node
573 && TREE_CODE (mv2) != ARRAY_TYPE)
574 mv2 = TYPE_MAIN_VARIANT (mv2);
575 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
576 memb; memb = DECL_CHAIN (memb))
578 tree mv3 = TREE_TYPE (memb);
579 if (mv3 && mv3 != error_mark_node
580 && TREE_CODE (mv3) != ARRAY_TYPE)
581 mv3 = TYPE_MAIN_VARIANT (mv3);
582 if (comptypes (mv3, mv2))
584 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
585 TREE_VALUE (p2));
586 pedwarn (input_location, OPT_Wpedantic,
587 "function types not truly compatible in ISO C");
588 goto parm_done;
592 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
593 && TREE_VALUE (p2) != TREE_VALUE (p1))
595 tree memb;
596 tree mv1 = TREE_VALUE (p1);
597 if (mv1 && mv1 != error_mark_node
598 && TREE_CODE (mv1) != ARRAY_TYPE)
599 mv1 = TYPE_MAIN_VARIANT (mv1);
600 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
601 memb; memb = DECL_CHAIN (memb))
603 tree mv3 = TREE_TYPE (memb);
604 if (mv3 && mv3 != error_mark_node
605 && TREE_CODE (mv3) != ARRAY_TYPE)
606 mv3 = TYPE_MAIN_VARIANT (mv3);
607 if (comptypes (mv3, mv1))
609 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
610 TREE_VALUE (p1));
611 pedwarn (input_location, OPT_Wpedantic,
612 "function types not truly compatible in ISO C");
613 goto parm_done;
617 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
618 parm_done: ;
621 t1 = build_function_type (valtype, newargs);
622 t1 = qualify_type (t1, t2);
623 /* ... falls through ... */
626 default:
627 return build_type_attribute_variant (t1, attributes);
632 /* Return the type of a conditional expression between pointers to
633 possibly differently qualified versions of compatible types.
635 We assume that comp_target_types has already been done and returned
636 nonzero; if that isn't so, this may crash. */
638 static tree
639 common_pointer_type (tree t1, tree t2)
641 tree attributes;
642 tree pointed_to_1, mv1;
643 tree pointed_to_2, mv2;
644 tree target;
645 unsigned target_quals;
646 addr_space_t as1, as2, as_common;
647 int quals1, quals2;
649 /* Save time if the two types are the same. */
651 if (t1 == t2) return t1;
653 /* If one type is nonsense, use the other. */
654 if (t1 == error_mark_node)
655 return t2;
656 if (t2 == error_mark_node)
657 return t1;
659 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
660 && TREE_CODE (t2) == POINTER_TYPE);
662 /* Merge the attributes. */
663 attributes = targetm.merge_type_attributes (t1, t2);
665 /* Find the composite type of the target types, and combine the
666 qualifiers of the two types' targets. Do not lose qualifiers on
667 array element types by taking the TYPE_MAIN_VARIANT. */
668 mv1 = pointed_to_1 = TREE_TYPE (t1);
669 mv2 = pointed_to_2 = TREE_TYPE (t2);
670 if (TREE_CODE (mv1) != ARRAY_TYPE)
671 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
672 if (TREE_CODE (mv2) != ARRAY_TYPE)
673 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
674 target = composite_type (mv1, mv2);
676 /* Strip array types to get correct qualifier for pointers to arrays */
677 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
678 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
680 /* For function types do not merge const qualifiers, but drop them
681 if used inconsistently. The middle-end uses these to mark const
682 and noreturn functions. */
683 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
684 target_quals = (quals1 & quals2);
685 else
686 target_quals = (quals1 | quals2);
688 /* If the two named address spaces are different, determine the common
689 superset address space. This is guaranteed to exist due to the
690 assumption that comp_target_type returned non-zero. */
691 as1 = TYPE_ADDR_SPACE (pointed_to_1);
692 as2 = TYPE_ADDR_SPACE (pointed_to_2);
693 if (!addr_space_superset (as1, as2, &as_common))
694 gcc_unreachable ();
696 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
698 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
699 return build_type_attribute_variant (t1, attributes);
702 /* Return the common type for two arithmetic types under the usual
703 arithmetic conversions. The default conversions have already been
704 applied, and enumerated types converted to their compatible integer
705 types. The resulting type is unqualified and has no attributes.
707 This is the type for the result of most arithmetic operations
708 if the operands have the given two types. */
710 static tree
711 c_common_type (tree t1, tree t2)
713 enum tree_code code1;
714 enum tree_code code2;
716 /* If one type is nonsense, use the other. */
717 if (t1 == error_mark_node)
718 return t2;
719 if (t2 == error_mark_node)
720 return t1;
722 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
723 t1 = TYPE_MAIN_VARIANT (t1);
725 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
726 t2 = TYPE_MAIN_VARIANT (t2);
728 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
729 t1 = build_type_attribute_variant (t1, NULL_TREE);
731 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
732 t2 = build_type_attribute_variant (t2, NULL_TREE);
734 /* Save time if the two types are the same. */
736 if (t1 == t2) return t1;
738 code1 = TREE_CODE (t1);
739 code2 = TREE_CODE (t2);
741 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
742 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
743 || code1 == INTEGER_TYPE);
744 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
745 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
746 || code2 == INTEGER_TYPE);
748 /* When one operand is a decimal float type, the other operand cannot be
749 a generic float type or a complex type. We also disallow vector types
750 here. */
751 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
752 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
754 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
756 error ("can%'t mix operands of decimal float and vector types");
757 return error_mark_node;
759 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
761 error ("can%'t mix operands of decimal float and complex types");
762 return error_mark_node;
764 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
766 error ("can%'t mix operands of decimal float and other float types");
767 return error_mark_node;
771 /* If one type is a vector type, return that type. (How the usual
772 arithmetic conversions apply to the vector types extension is not
773 precisely specified.) */
774 if (code1 == VECTOR_TYPE)
775 return t1;
777 if (code2 == VECTOR_TYPE)
778 return t2;
780 /* If one type is complex, form the common type of the non-complex
781 components, then make that complex. Use T1 or T2 if it is the
782 required type. */
783 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
785 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
786 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
787 tree subtype = c_common_type (subtype1, subtype2);
789 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
790 return t1;
791 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
792 return t2;
793 else
794 return build_complex_type (subtype);
797 /* If only one is real, use it as the result. */
799 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
800 return t1;
802 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
803 return t2;
805 /* If both are real and either are decimal floating point types, use
806 the decimal floating point type with the greater precision. */
808 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
810 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
811 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
812 return dfloat128_type_node;
813 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
814 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
815 return dfloat64_type_node;
816 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
817 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
818 return dfloat32_type_node;
821 /* Deal with fixed-point types. */
822 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
824 unsigned int unsignedp = 0, satp = 0;
825 machine_mode m1, m2;
826 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
828 m1 = TYPE_MODE (t1);
829 m2 = TYPE_MODE (t2);
831 /* If one input type is saturating, the result type is saturating. */
832 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
833 satp = 1;
835 /* If both fixed-point types are unsigned, the result type is unsigned.
836 When mixing fixed-point and integer types, follow the sign of the
837 fixed-point type.
838 Otherwise, the result type is signed. */
839 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
840 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
841 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
842 && TYPE_UNSIGNED (t1))
843 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
844 && TYPE_UNSIGNED (t2)))
845 unsignedp = 1;
847 /* The result type is signed. */
848 if (unsignedp == 0)
850 /* If the input type is unsigned, we need to convert to the
851 signed type. */
852 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
854 enum mode_class mclass = (enum mode_class) 0;
855 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
856 mclass = MODE_FRACT;
857 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
858 mclass = MODE_ACCUM;
859 else
860 gcc_unreachable ();
861 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
863 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
865 enum mode_class mclass = (enum mode_class) 0;
866 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
867 mclass = MODE_FRACT;
868 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
869 mclass = MODE_ACCUM;
870 else
871 gcc_unreachable ();
872 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
876 if (code1 == FIXED_POINT_TYPE)
878 fbit1 = GET_MODE_FBIT (m1);
879 ibit1 = GET_MODE_IBIT (m1);
881 else
883 fbit1 = 0;
884 /* Signed integers need to subtract one sign bit. */
885 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
888 if (code2 == FIXED_POINT_TYPE)
890 fbit2 = GET_MODE_FBIT (m2);
891 ibit2 = GET_MODE_IBIT (m2);
893 else
895 fbit2 = 0;
896 /* Signed integers need to subtract one sign bit. */
897 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
900 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
901 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
902 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
903 satp);
906 /* Both real or both integers; use the one with greater precision. */
908 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
909 return t1;
910 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
911 return t2;
913 /* Same precision. Prefer long longs to longs to ints when the
914 same precision, following the C99 rules on integer type rank
915 (which are equivalent to the C90 rules for C90 types). */
917 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
918 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
919 return long_long_unsigned_type_node;
921 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
924 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
925 return long_long_unsigned_type_node;
926 else
927 return long_long_integer_type_node;
930 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
931 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
932 return long_unsigned_type_node;
934 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
935 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
937 /* But preserve unsignedness from the other type,
938 since long cannot hold all the values of an unsigned int. */
939 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
940 return long_unsigned_type_node;
941 else
942 return long_integer_type_node;
945 /* Likewise, prefer long double to double even if same size. */
946 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
947 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
948 return long_double_type_node;
950 /* Likewise, prefer double to float even if same size.
951 We got a couple of embedded targets with 32 bit doubles, and the
952 pdp11 might have 64 bit floats. */
953 if (TYPE_MAIN_VARIANT (t1) == double_type_node
954 || TYPE_MAIN_VARIANT (t2) == double_type_node)
955 return double_type_node;
957 /* Otherwise prefer the unsigned one. */
959 if (TYPE_UNSIGNED (t1))
960 return t1;
961 else
962 return t2;
965 /* Wrapper around c_common_type that is used by c-common.c and other
966 front end optimizations that remove promotions. ENUMERAL_TYPEs
967 are allowed here and are converted to their compatible integer types.
968 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
969 preferably a non-Boolean type as the common type. */
970 tree
971 common_type (tree t1, tree t2)
973 if (TREE_CODE (t1) == ENUMERAL_TYPE)
974 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
975 if (TREE_CODE (t2) == ENUMERAL_TYPE)
976 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
978 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
979 if (TREE_CODE (t1) == BOOLEAN_TYPE
980 && TREE_CODE (t2) == BOOLEAN_TYPE)
981 return boolean_type_node;
983 /* If either type is BOOLEAN_TYPE, then return the other. */
984 if (TREE_CODE (t1) == BOOLEAN_TYPE)
985 return t2;
986 if (TREE_CODE (t2) == BOOLEAN_TYPE)
987 return t1;
989 return c_common_type (t1, t2);
992 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
993 or various other operations. Return 2 if they are compatible
994 but a warning may be needed if you use them together. */
997 comptypes (tree type1, tree type2)
999 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1000 int val;
1002 val = comptypes_internal (type1, type2, NULL, NULL);
1003 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1005 return val;
1008 /* Like comptypes, but if it returns non-zero because enum and int are
1009 compatible, it sets *ENUM_AND_INT_P to true. */
1011 static int
1012 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1014 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1015 int val;
1017 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1018 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1020 return val;
1023 /* Like comptypes, but if it returns nonzero for different types, it
1024 sets *DIFFERENT_TYPES_P to true. */
1027 comptypes_check_different_types (tree type1, tree type2,
1028 bool *different_types_p)
1030 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1031 int val;
1033 val = comptypes_internal (type1, type2, NULL, different_types_p);
1034 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1036 return val;
1039 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1040 or various other operations. Return 2 if they are compatible
1041 but a warning may be needed if you use them together. If
1042 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1043 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1044 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1045 NULL, and the types are compatible but different enough not to be
1046 permitted in C11 typedef redeclarations, then this sets
1047 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1048 false, but may or may not be set if the types are incompatible.
1049 This differs from comptypes, in that we don't free the seen
1050 types. */
1052 static int
1053 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1054 bool *different_types_p)
1056 const_tree t1 = type1;
1057 const_tree t2 = type2;
1058 int attrval, val;
1060 /* Suppress errors caused by previously reported errors. */
1062 if (t1 == t2 || !t1 || !t2
1063 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1064 return 1;
1066 /* Enumerated types are compatible with integer types, but this is
1067 not transitive: two enumerated types in the same translation unit
1068 are compatible with each other only if they are the same type. */
1070 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1072 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1073 if (TREE_CODE (t2) != VOID_TYPE)
1075 if (enum_and_int_p != NULL)
1076 *enum_and_int_p = true;
1077 if (different_types_p != NULL)
1078 *different_types_p = true;
1081 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1083 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1084 if (TREE_CODE (t1) != VOID_TYPE)
1086 if (enum_and_int_p != NULL)
1087 *enum_and_int_p = true;
1088 if (different_types_p != NULL)
1089 *different_types_p = true;
1093 if (t1 == t2)
1094 return 1;
1096 /* Different classes of types can't be compatible. */
1098 if (TREE_CODE (t1) != TREE_CODE (t2))
1099 return 0;
1101 /* Qualifiers must match. C99 6.7.3p9 */
1103 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1104 return 0;
1106 /* Allow for two different type nodes which have essentially the same
1107 definition. Note that we already checked for equality of the type
1108 qualifiers (just above). */
1110 if (TREE_CODE (t1) != ARRAY_TYPE
1111 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1112 return 1;
1114 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1115 if (!(attrval = comp_type_attributes (t1, t2)))
1116 return 0;
1118 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1119 val = 0;
1121 switch (TREE_CODE (t1))
1123 case POINTER_TYPE:
1124 /* Do not remove mode or aliasing information. */
1125 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1126 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1127 break;
1128 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1129 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1130 enum_and_int_p, different_types_p));
1131 break;
1133 case FUNCTION_TYPE:
1134 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1135 different_types_p);
1136 break;
1138 case ARRAY_TYPE:
1140 tree d1 = TYPE_DOMAIN (t1);
1141 tree d2 = TYPE_DOMAIN (t2);
1142 bool d1_variable, d2_variable;
1143 bool d1_zero, d2_zero;
1144 val = 1;
1146 /* Target types must match incl. qualifiers. */
1147 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1148 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1149 enum_and_int_p,
1150 different_types_p)))
1151 return 0;
1153 if (different_types_p != NULL
1154 && (d1 == 0) != (d2 == 0))
1155 *different_types_p = true;
1156 /* Sizes must match unless one is missing or variable. */
1157 if (d1 == 0 || d2 == 0 || d1 == d2)
1158 break;
1160 d1_zero = !TYPE_MAX_VALUE (d1);
1161 d2_zero = !TYPE_MAX_VALUE (d2);
1163 d1_variable = (!d1_zero
1164 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1165 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1166 d2_variable = (!d2_zero
1167 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1168 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1169 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1170 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1172 if (different_types_p != NULL
1173 && d1_variable != d2_variable)
1174 *different_types_p = true;
1175 if (d1_variable || d2_variable)
1176 break;
1177 if (d1_zero && d2_zero)
1178 break;
1179 if (d1_zero || d2_zero
1180 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1181 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1182 val = 0;
1184 break;
1187 case ENUMERAL_TYPE:
1188 case RECORD_TYPE:
1189 case UNION_TYPE:
1190 if (val != 1 && !same_translation_unit_p (t1, t2))
1192 tree a1 = TYPE_ATTRIBUTES (t1);
1193 tree a2 = TYPE_ATTRIBUTES (t2);
1195 if (! attribute_list_contained (a1, a2)
1196 && ! attribute_list_contained (a2, a1))
1197 break;
1199 if (attrval != 2)
1200 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1201 different_types_p);
1202 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1203 different_types_p);
1205 break;
1207 case VECTOR_TYPE:
1208 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1209 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1210 enum_and_int_p, different_types_p));
1211 break;
1213 default:
1214 break;
1216 return attrval == 2 && val == 1 ? 2 : val;
1219 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1220 their qualifiers, except for named address spaces. If the pointers point to
1221 different named addresses, then we must determine if one address space is a
1222 subset of the other. */
1224 static int
1225 comp_target_types (location_t location, tree ttl, tree ttr)
1227 int val;
1228 int val_ped;
1229 tree mvl = TREE_TYPE (ttl);
1230 tree mvr = TREE_TYPE (ttr);
1231 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1232 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1233 addr_space_t as_common;
1234 bool enum_and_int_p;
1236 /* Fail if pointers point to incompatible address spaces. */
1237 if (!addr_space_superset (asl, asr, &as_common))
1238 return 0;
1240 /* For pedantic record result of comptypes on arrays before losing
1241 qualifiers on the element type below. */
1242 val_ped = 1;
1244 if (TREE_CODE (mvl) == ARRAY_TYPE
1245 && TREE_CODE (mvr) == ARRAY_TYPE)
1246 val_ped = comptypes (mvl, mvr);
1248 /* Qualifiers on element types of array types that are
1249 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1251 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1252 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1253 : TYPE_MAIN_VARIANT (mvl));
1255 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1256 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1257 : TYPE_MAIN_VARIANT (mvr));
1259 enum_and_int_p = false;
1260 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1262 if (val == 1 && val_ped != 1)
1263 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1264 "are incompatible in ISO C");
1266 if (val == 2)
1267 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1269 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1270 warning_at (location, OPT_Wc___compat,
1271 "pointer target types incompatible in C++");
1273 return val;
1276 /* Subroutines of `comptypes'. */
1278 /* Determine whether two trees derive from the same translation unit.
1279 If the CONTEXT chain ends in a null, that tree's context is still
1280 being parsed, so if two trees have context chains ending in null,
1281 they're in the same translation unit. */
1283 same_translation_unit_p (const_tree t1, const_tree t2)
1285 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1286 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1288 case tcc_declaration:
1289 t1 = DECL_CONTEXT (t1); break;
1290 case tcc_type:
1291 t1 = TYPE_CONTEXT (t1); break;
1292 case tcc_exceptional:
1293 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1294 default: gcc_unreachable ();
1297 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1298 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1300 case tcc_declaration:
1301 t2 = DECL_CONTEXT (t2); break;
1302 case tcc_type:
1303 t2 = TYPE_CONTEXT (t2); break;
1304 case tcc_exceptional:
1305 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1306 default: gcc_unreachable ();
1309 return t1 == t2;
1312 /* Allocate the seen two types, assuming that they are compatible. */
1314 static struct tagged_tu_seen_cache *
1315 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1317 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1318 tu->next = tagged_tu_seen_base;
1319 tu->t1 = t1;
1320 tu->t2 = t2;
1322 tagged_tu_seen_base = tu;
1324 /* The C standard says that two structures in different translation
1325 units are compatible with each other only if the types of their
1326 fields are compatible (among other things). We assume that they
1327 are compatible until proven otherwise when building the cache.
1328 An example where this can occur is:
1329 struct a
1331 struct a *next;
1333 If we are comparing this against a similar struct in another TU,
1334 and did not assume they were compatible, we end up with an infinite
1335 loop. */
1336 tu->val = 1;
1337 return tu;
1340 /* Free the seen types until we get to TU_TIL. */
1342 static void
1343 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1345 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1346 while (tu != tu_til)
1348 const struct tagged_tu_seen_cache *const tu1
1349 = (const struct tagged_tu_seen_cache *) tu;
1350 tu = tu1->next;
1351 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1353 tagged_tu_seen_base = tu_til;
1356 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1357 compatible. If the two types are not the same (which has been
1358 checked earlier), this can only happen when multiple translation
1359 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1360 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1361 comptypes_internal. */
1363 static int
1364 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1365 bool *enum_and_int_p, bool *different_types_p)
1367 tree s1, s2;
1368 bool needs_warning = false;
1370 /* We have to verify that the tags of the types are the same. This
1371 is harder than it looks because this may be a typedef, so we have
1372 to go look at the original type. It may even be a typedef of a
1373 typedef...
1374 In the case of compiler-created builtin structs the TYPE_DECL
1375 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1376 while (TYPE_NAME (t1)
1377 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1378 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1379 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1381 while (TYPE_NAME (t2)
1382 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1383 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1384 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1386 /* C90 didn't have the requirement that the two tags be the same. */
1387 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1388 return 0;
1390 /* C90 didn't say what happened if one or both of the types were
1391 incomplete; we choose to follow C99 rules here, which is that they
1392 are compatible. */
1393 if (TYPE_SIZE (t1) == NULL
1394 || TYPE_SIZE (t2) == NULL)
1395 return 1;
1398 const struct tagged_tu_seen_cache * tts_i;
1399 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1400 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1401 return tts_i->val;
1404 switch (TREE_CODE (t1))
1406 case ENUMERAL_TYPE:
1408 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1409 /* Speed up the case where the type values are in the same order. */
1410 tree tv1 = TYPE_VALUES (t1);
1411 tree tv2 = TYPE_VALUES (t2);
1413 if (tv1 == tv2)
1415 return 1;
1418 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1420 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1421 break;
1422 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1424 tu->val = 0;
1425 return 0;
1429 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1431 return 1;
1433 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1435 tu->val = 0;
1436 return 0;
1439 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1441 tu->val = 0;
1442 return 0;
1445 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1447 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1448 if (s2 == NULL
1449 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1451 tu->val = 0;
1452 return 0;
1455 return 1;
1458 case UNION_TYPE:
1460 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1461 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1463 tu->val = 0;
1464 return 0;
1467 /* Speed up the common case where the fields are in the same order. */
1468 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1469 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1471 int result;
1473 if (DECL_NAME (s1) != DECL_NAME (s2))
1474 break;
1475 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1476 enum_and_int_p, different_types_p);
1478 if (result != 1 && !DECL_NAME (s1))
1479 break;
1480 if (result == 0)
1482 tu->val = 0;
1483 return 0;
1485 if (result == 2)
1486 needs_warning = true;
1488 if (TREE_CODE (s1) == FIELD_DECL
1489 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1490 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1492 tu->val = 0;
1493 return 0;
1496 if (!s1 && !s2)
1498 tu->val = needs_warning ? 2 : 1;
1499 return tu->val;
1502 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1504 bool ok = false;
1506 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1507 if (DECL_NAME (s1) == DECL_NAME (s2))
1509 int result;
1511 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1512 enum_and_int_p,
1513 different_types_p);
1515 if (result != 1 && !DECL_NAME (s1))
1516 continue;
1517 if (result == 0)
1519 tu->val = 0;
1520 return 0;
1522 if (result == 2)
1523 needs_warning = true;
1525 if (TREE_CODE (s1) == FIELD_DECL
1526 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1527 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1528 break;
1530 ok = true;
1531 break;
1533 if (!ok)
1535 tu->val = 0;
1536 return 0;
1539 tu->val = needs_warning ? 2 : 10;
1540 return tu->val;
1543 case RECORD_TYPE:
1545 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1547 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1548 s1 && s2;
1549 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1551 int result;
1552 if (TREE_CODE (s1) != TREE_CODE (s2)
1553 || DECL_NAME (s1) != DECL_NAME (s2))
1554 break;
1555 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1556 enum_and_int_p, different_types_p);
1557 if (result == 0)
1558 break;
1559 if (result == 2)
1560 needs_warning = true;
1562 if (TREE_CODE (s1) == FIELD_DECL
1563 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1564 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1565 break;
1567 if (s1 && s2)
1568 tu->val = 0;
1569 else
1570 tu->val = needs_warning ? 2 : 1;
1571 return tu->val;
1574 default:
1575 gcc_unreachable ();
1579 /* Return 1 if two function types F1 and F2 are compatible.
1580 If either type specifies no argument types,
1581 the other must specify a fixed number of self-promoting arg types.
1582 Otherwise, if one type specifies only the number of arguments,
1583 the other must specify that number of self-promoting arg types.
1584 Otherwise, the argument types must match.
1585 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1587 static int
1588 function_types_compatible_p (const_tree f1, const_tree f2,
1589 bool *enum_and_int_p, bool *different_types_p)
1591 tree args1, args2;
1592 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1593 int val = 1;
1594 int val1;
1595 tree ret1, ret2;
1597 ret1 = TREE_TYPE (f1);
1598 ret2 = TREE_TYPE (f2);
1600 /* 'volatile' qualifiers on a function's return type used to mean
1601 the function is noreturn. */
1602 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1603 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1604 if (TYPE_VOLATILE (ret1))
1605 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1606 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1607 if (TYPE_VOLATILE (ret2))
1608 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1609 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1610 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1611 if (val == 0)
1612 return 0;
1614 args1 = TYPE_ARG_TYPES (f1);
1615 args2 = TYPE_ARG_TYPES (f2);
1617 if (different_types_p != NULL
1618 && (args1 == 0) != (args2 == 0))
1619 *different_types_p = true;
1621 /* An unspecified parmlist matches any specified parmlist
1622 whose argument types don't need default promotions. */
1624 if (args1 == 0)
1626 if (!self_promoting_args_p (args2))
1627 return 0;
1628 /* If one of these types comes from a non-prototype fn definition,
1629 compare that with the other type's arglist.
1630 If they don't match, ask for a warning (but no error). */
1631 if (TYPE_ACTUAL_ARG_TYPES (f1)
1632 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1633 enum_and_int_p, different_types_p))
1634 val = 2;
1635 return val;
1637 if (args2 == 0)
1639 if (!self_promoting_args_p (args1))
1640 return 0;
1641 if (TYPE_ACTUAL_ARG_TYPES (f2)
1642 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1643 enum_and_int_p, different_types_p))
1644 val = 2;
1645 return val;
1648 /* Both types have argument lists: compare them and propagate results. */
1649 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1650 different_types_p);
1651 return val1 != 1 ? val1 : val;
1654 /* Check two lists of types for compatibility, returning 0 for
1655 incompatible, 1 for compatible, or 2 for compatible with
1656 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1657 comptypes_internal. */
1659 static int
1660 type_lists_compatible_p (const_tree args1, const_tree args2,
1661 bool *enum_and_int_p, bool *different_types_p)
1663 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1664 int val = 1;
1665 int newval = 0;
1667 while (1)
1669 tree a1, mv1, a2, mv2;
1670 if (args1 == 0 && args2 == 0)
1671 return val;
1672 /* If one list is shorter than the other,
1673 they fail to match. */
1674 if (args1 == 0 || args2 == 0)
1675 return 0;
1676 mv1 = a1 = TREE_VALUE (args1);
1677 mv2 = a2 = TREE_VALUE (args2);
1678 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1679 mv1 = (TYPE_ATOMIC (mv1)
1680 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1681 TYPE_QUAL_ATOMIC)
1682 : TYPE_MAIN_VARIANT (mv1));
1683 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1684 mv2 = (TYPE_ATOMIC (mv2)
1685 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1686 TYPE_QUAL_ATOMIC)
1687 : TYPE_MAIN_VARIANT (mv2));
1688 /* A null pointer instead of a type
1689 means there is supposed to be an argument
1690 but nothing is specified about what type it has.
1691 So match anything that self-promotes. */
1692 if (different_types_p != NULL
1693 && (a1 == 0) != (a2 == 0))
1694 *different_types_p = true;
1695 if (a1 == 0)
1697 if (c_type_promotes_to (a2) != a2)
1698 return 0;
1700 else if (a2 == 0)
1702 if (c_type_promotes_to (a1) != a1)
1703 return 0;
1705 /* If one of the lists has an error marker, ignore this arg. */
1706 else if (TREE_CODE (a1) == ERROR_MARK
1707 || TREE_CODE (a2) == ERROR_MARK)
1709 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1710 different_types_p)))
1712 if (different_types_p != NULL)
1713 *different_types_p = true;
1714 /* Allow wait (union {union wait *u; int *i} *)
1715 and wait (union wait *) to be compatible. */
1716 if (TREE_CODE (a1) == UNION_TYPE
1717 && (TYPE_NAME (a1) == 0
1718 || TYPE_TRANSPARENT_AGGR (a1))
1719 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1720 && tree_int_cst_equal (TYPE_SIZE (a1),
1721 TYPE_SIZE (a2)))
1723 tree memb;
1724 for (memb = TYPE_FIELDS (a1);
1725 memb; memb = DECL_CHAIN (memb))
1727 tree mv3 = TREE_TYPE (memb);
1728 if (mv3 && mv3 != error_mark_node
1729 && TREE_CODE (mv3) != ARRAY_TYPE)
1730 mv3 = (TYPE_ATOMIC (mv3)
1731 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1732 TYPE_QUAL_ATOMIC)
1733 : TYPE_MAIN_VARIANT (mv3));
1734 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1735 different_types_p))
1736 break;
1738 if (memb == 0)
1739 return 0;
1741 else if (TREE_CODE (a2) == UNION_TYPE
1742 && (TYPE_NAME (a2) == 0
1743 || TYPE_TRANSPARENT_AGGR (a2))
1744 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1745 && tree_int_cst_equal (TYPE_SIZE (a2),
1746 TYPE_SIZE (a1)))
1748 tree memb;
1749 for (memb = TYPE_FIELDS (a2);
1750 memb; memb = DECL_CHAIN (memb))
1752 tree mv3 = TREE_TYPE (memb);
1753 if (mv3 && mv3 != error_mark_node
1754 && TREE_CODE (mv3) != ARRAY_TYPE)
1755 mv3 = (TYPE_ATOMIC (mv3)
1756 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1757 TYPE_QUAL_ATOMIC)
1758 : TYPE_MAIN_VARIANT (mv3));
1759 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1760 different_types_p))
1761 break;
1763 if (memb == 0)
1764 return 0;
1766 else
1767 return 0;
1770 /* comptypes said ok, but record if it said to warn. */
1771 if (newval > val)
1772 val = newval;
1774 args1 = TREE_CHAIN (args1);
1775 args2 = TREE_CHAIN (args2);
1779 /* Compute the size to increment a pointer by. When a function type or void
1780 type or incomplete type is passed, size_one_node is returned.
1781 This function does not emit any diagnostics; the caller is responsible
1782 for that. */
1784 static tree
1785 c_size_in_bytes (const_tree type)
1787 enum tree_code code = TREE_CODE (type);
1789 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1790 || !COMPLETE_TYPE_P (type))
1791 return size_one_node;
1793 /* Convert in case a char is more than one unit. */
1794 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1795 size_int (TYPE_PRECISION (char_type_node)
1796 / BITS_PER_UNIT));
1799 /* Return either DECL or its known constant value (if it has one). */
1801 tree
1802 decl_constant_value (tree decl)
1804 if (/* Don't change a variable array bound or initial value to a constant
1805 in a place where a variable is invalid. Note that DECL_INITIAL
1806 isn't valid for a PARM_DECL. */
1807 current_function_decl != 0
1808 && TREE_CODE (decl) != PARM_DECL
1809 && !TREE_THIS_VOLATILE (decl)
1810 && TREE_READONLY (decl)
1811 && DECL_INITIAL (decl) != 0
1812 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1813 /* This is invalid if initial value is not constant.
1814 If it has either a function call, a memory reference,
1815 or a variable, then re-evaluating it could give different results. */
1816 && TREE_CONSTANT (DECL_INITIAL (decl))
1817 /* Check for cases where this is sub-optimal, even though valid. */
1818 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1819 return DECL_INITIAL (decl);
1820 return decl;
1823 /* Convert the array expression EXP to a pointer. */
1824 static tree
1825 array_to_pointer_conversion (location_t loc, tree exp)
1827 tree orig_exp = exp;
1828 tree type = TREE_TYPE (exp);
1829 tree adr;
1830 tree restype = TREE_TYPE (type);
1831 tree ptrtype;
1833 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1835 STRIP_TYPE_NOPS (exp);
1837 if (TREE_NO_WARNING (orig_exp))
1838 TREE_NO_WARNING (exp) = 1;
1840 ptrtype = build_pointer_type (restype);
1842 if (TREE_CODE (exp) == INDIRECT_REF)
1843 return convert (ptrtype, TREE_OPERAND (exp, 0));
1845 /* In C++ array compound literals are temporary objects unless they are
1846 const or appear in namespace scope, so they are destroyed too soon
1847 to use them for much of anything (c++/53220). */
1848 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1850 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1851 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1852 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1853 "converting an array compound literal to a pointer "
1854 "is ill-formed in C++");
1857 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1858 return convert (ptrtype, adr);
1861 /* Convert the function expression EXP to a pointer. */
1862 static tree
1863 function_to_pointer_conversion (location_t loc, tree exp)
1865 tree orig_exp = exp;
1867 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1869 STRIP_TYPE_NOPS (exp);
1871 if (TREE_NO_WARNING (orig_exp))
1872 TREE_NO_WARNING (exp) = 1;
1874 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1877 /* Mark EXP as read, not just set, for set but not used -Wunused
1878 warning purposes. */
1880 void
1881 mark_exp_read (tree exp)
1883 switch (TREE_CODE (exp))
1885 case VAR_DECL:
1886 case PARM_DECL:
1887 DECL_READ_P (exp) = 1;
1888 break;
1889 case ARRAY_REF:
1890 case COMPONENT_REF:
1891 case MODIFY_EXPR:
1892 case REALPART_EXPR:
1893 case IMAGPART_EXPR:
1894 CASE_CONVERT:
1895 case ADDR_EXPR:
1896 mark_exp_read (TREE_OPERAND (exp, 0));
1897 break;
1898 case COMPOUND_EXPR:
1899 case C_MAYBE_CONST_EXPR:
1900 mark_exp_read (TREE_OPERAND (exp, 1));
1901 break;
1902 default:
1903 break;
1907 /* Perform the default conversion of arrays and functions to pointers.
1908 Return the result of converting EXP. For any other expression, just
1909 return EXP.
1911 LOC is the location of the expression. */
1913 struct c_expr
1914 default_function_array_conversion (location_t loc, struct c_expr exp)
1916 tree orig_exp = exp.value;
1917 tree type = TREE_TYPE (exp.value);
1918 enum tree_code code = TREE_CODE (type);
1920 switch (code)
1922 case ARRAY_TYPE:
1924 bool not_lvalue = false;
1925 bool lvalue_array_p;
1927 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1928 || CONVERT_EXPR_P (exp.value))
1929 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1931 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1932 not_lvalue = true;
1933 exp.value = TREE_OPERAND (exp.value, 0);
1936 if (TREE_NO_WARNING (orig_exp))
1937 TREE_NO_WARNING (exp.value) = 1;
1939 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1940 if (!flag_isoc99 && !lvalue_array_p)
1942 /* Before C99, non-lvalue arrays do not decay to pointers.
1943 Normally, using such an array would be invalid; but it can
1944 be used correctly inside sizeof or as a statement expression.
1945 Thus, do not give an error here; an error will result later. */
1946 return exp;
1949 exp.value = array_to_pointer_conversion (loc, exp.value);
1951 break;
1952 case FUNCTION_TYPE:
1953 exp.value = function_to_pointer_conversion (loc, exp.value);
1954 break;
1955 default:
1956 break;
1959 return exp;
1962 struct c_expr
1963 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1965 mark_exp_read (exp.value);
1966 return default_function_array_conversion (loc, exp);
1969 /* Return whether EXPR should be treated as an atomic lvalue for the
1970 purposes of load and store handling. */
1972 static bool
1973 really_atomic_lvalue (tree expr)
1975 if (error_operand_p (expr))
1976 return false;
1977 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1978 return false;
1979 if (!lvalue_p (expr))
1980 return false;
1982 /* Ignore _Atomic on register variables, since their addresses can't
1983 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1984 sequences wouldn't work. Ignore _Atomic on structures containing
1985 bit-fields, since accessing elements of atomic structures or
1986 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1987 it's undefined at translation time or execution time, and the
1988 normal atomic sequences again wouldn't work. */
1989 while (handled_component_p (expr))
1991 if (TREE_CODE (expr) == COMPONENT_REF
1992 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1993 return false;
1994 expr = TREE_OPERAND (expr, 0);
1996 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1997 return false;
1998 return true;
2001 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2002 including converting functions and arrays to pointers if CONVERT_P.
2003 If READ_P, also mark the expression as having been read. */
2005 struct c_expr
2006 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2007 bool convert_p, bool read_p)
2009 if (read_p)
2010 mark_exp_read (exp.value);
2011 if (convert_p)
2012 exp = default_function_array_conversion (loc, exp);
2013 if (really_atomic_lvalue (exp.value))
2015 vec<tree, va_gc> *params;
2016 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2017 tree expr_type = TREE_TYPE (exp.value);
2018 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2019 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2021 gcc_assert (TYPE_ATOMIC (expr_type));
2023 /* Expansion of a generic atomic load may require an addition
2024 element, so allocate enough to prevent a resize. */
2025 vec_alloc (params, 4);
2027 /* Remove the qualifiers for the rest of the expressions and
2028 create the VAL temp variable to hold the RHS. */
2029 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2030 tmp = create_tmp_var (nonatomic_type);
2031 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2032 TREE_ADDRESSABLE (tmp) = 1;
2033 TREE_NO_WARNING (tmp) = 1;
2035 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2036 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2037 params->quick_push (expr_addr);
2038 params->quick_push (tmp_addr);
2039 params->quick_push (seq_cst);
2040 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2042 /* EXPR is always read. */
2043 mark_exp_read (exp.value);
2045 /* Return tmp which contains the value loaded. */
2046 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2048 return exp;
2051 /* EXP is an expression of integer type. Apply the integer promotions
2052 to it and return the promoted value. */
2054 tree
2055 perform_integral_promotions (tree exp)
2057 tree type = TREE_TYPE (exp);
2058 enum tree_code code = TREE_CODE (type);
2060 gcc_assert (INTEGRAL_TYPE_P (type));
2062 /* Normally convert enums to int,
2063 but convert wide enums to something wider. */
2064 if (code == ENUMERAL_TYPE)
2066 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2067 TYPE_PRECISION (integer_type_node)),
2068 ((TYPE_PRECISION (type)
2069 >= TYPE_PRECISION (integer_type_node))
2070 && TYPE_UNSIGNED (type)));
2072 return convert (type, exp);
2075 /* ??? This should no longer be needed now bit-fields have their
2076 proper types. */
2077 if (TREE_CODE (exp) == COMPONENT_REF
2078 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2079 /* If it's thinner than an int, promote it like a
2080 c_promoting_integer_type_p, otherwise leave it alone. */
2081 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2082 TYPE_PRECISION (integer_type_node)))
2083 return convert (integer_type_node, exp);
2085 if (c_promoting_integer_type_p (type))
2087 /* Preserve unsignedness if not really getting any wider. */
2088 if (TYPE_UNSIGNED (type)
2089 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2090 return convert (unsigned_type_node, exp);
2092 return convert (integer_type_node, exp);
2095 return exp;
2099 /* Perform default promotions for C data used in expressions.
2100 Enumeral types or short or char are converted to int.
2101 In addition, manifest constants symbols are replaced by their values. */
2103 tree
2104 default_conversion (tree exp)
2106 tree orig_exp;
2107 tree type = TREE_TYPE (exp);
2108 enum tree_code code = TREE_CODE (type);
2109 tree promoted_type;
2111 mark_exp_read (exp);
2113 /* Functions and arrays have been converted during parsing. */
2114 gcc_assert (code != FUNCTION_TYPE);
2115 if (code == ARRAY_TYPE)
2116 return exp;
2118 /* Constants can be used directly unless they're not loadable. */
2119 if (TREE_CODE (exp) == CONST_DECL)
2120 exp = DECL_INITIAL (exp);
2122 /* Strip no-op conversions. */
2123 orig_exp = exp;
2124 STRIP_TYPE_NOPS (exp);
2126 if (TREE_NO_WARNING (orig_exp))
2127 TREE_NO_WARNING (exp) = 1;
2129 if (code == VOID_TYPE)
2131 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2132 "void value not ignored as it ought to be");
2133 return error_mark_node;
2136 exp = require_complete_type (exp);
2137 if (exp == error_mark_node)
2138 return error_mark_node;
2140 promoted_type = targetm.promoted_type (type);
2141 if (promoted_type)
2142 return convert (promoted_type, exp);
2144 if (INTEGRAL_TYPE_P (type))
2145 return perform_integral_promotions (exp);
2147 return exp;
2150 /* Look up COMPONENT in a structure or union TYPE.
2152 If the component name is not found, returns NULL_TREE. Otherwise,
2153 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2154 stepping down the chain to the component, which is in the last
2155 TREE_VALUE of the list. Normally the list is of length one, but if
2156 the component is embedded within (nested) anonymous structures or
2157 unions, the list steps down the chain to the component. */
2159 static tree
2160 lookup_field (tree type, tree component)
2162 tree field;
2164 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2165 to the field elements. Use a binary search on this array to quickly
2166 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2167 will always be set for structures which have many elements. */
2169 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2171 int bot, top, half;
2172 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2174 field = TYPE_FIELDS (type);
2175 bot = 0;
2176 top = TYPE_LANG_SPECIFIC (type)->s->len;
2177 while (top - bot > 1)
2179 half = (top - bot + 1) >> 1;
2180 field = field_array[bot+half];
2182 if (DECL_NAME (field) == NULL_TREE)
2184 /* Step through all anon unions in linear fashion. */
2185 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2187 field = field_array[bot++];
2188 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2189 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2191 tree anon = lookup_field (TREE_TYPE (field), component);
2193 if (anon)
2194 return tree_cons (NULL_TREE, field, anon);
2196 /* The Plan 9 compiler permits referring
2197 directly to an anonymous struct/union field
2198 using a typedef name. */
2199 if (flag_plan9_extensions
2200 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2201 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2202 == TYPE_DECL)
2203 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2204 == component))
2205 break;
2209 /* Entire record is only anon unions. */
2210 if (bot > top)
2211 return NULL_TREE;
2213 /* Restart the binary search, with new lower bound. */
2214 continue;
2217 if (DECL_NAME (field) == component)
2218 break;
2219 if (DECL_NAME (field) < component)
2220 bot += half;
2221 else
2222 top = bot + half;
2225 if (DECL_NAME (field_array[bot]) == component)
2226 field = field_array[bot];
2227 else if (DECL_NAME (field) != component)
2228 return NULL_TREE;
2230 else
2232 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2234 if (DECL_NAME (field) == NULL_TREE
2235 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2236 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2238 tree anon = lookup_field (TREE_TYPE (field), component);
2240 if (anon)
2241 return tree_cons (NULL_TREE, field, anon);
2243 /* The Plan 9 compiler permits referring directly to an
2244 anonymous struct/union field using a typedef
2245 name. */
2246 if (flag_plan9_extensions
2247 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2248 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2249 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2250 == component))
2251 break;
2254 if (DECL_NAME (field) == component)
2255 break;
2258 if (field == NULL_TREE)
2259 return NULL_TREE;
2262 return tree_cons (NULL_TREE, field, NULL_TREE);
2265 /* Make an expression to refer to the COMPONENT field of structure or
2266 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2267 location of the COMPONENT_REF. */
2269 tree
2270 build_component_ref (location_t loc, tree datum, tree component)
2272 tree type = TREE_TYPE (datum);
2273 enum tree_code code = TREE_CODE (type);
2274 tree field = NULL;
2275 tree ref;
2276 bool datum_lvalue = lvalue_p (datum);
2278 if (!objc_is_public (datum, component))
2279 return error_mark_node;
2281 /* Detect Objective-C property syntax object.property. */
2282 if (c_dialect_objc ()
2283 && (ref = objc_maybe_build_component_ref (datum, component)))
2284 return ref;
2286 /* See if there is a field or component with name COMPONENT. */
2288 if (code == RECORD_TYPE || code == UNION_TYPE)
2290 if (!COMPLETE_TYPE_P (type))
2292 c_incomplete_type_error (NULL_TREE, type);
2293 return error_mark_node;
2296 field = lookup_field (type, component);
2298 if (!field)
2300 error_at (loc, "%qT has no member named %qE", type, component);
2301 return error_mark_node;
2304 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2305 This might be better solved in future the way the C++ front
2306 end does it - by giving the anonymous entities each a
2307 separate name and type, and then have build_component_ref
2308 recursively call itself. We can't do that here. */
2311 tree subdatum = TREE_VALUE (field);
2312 int quals;
2313 tree subtype;
2314 bool use_datum_quals;
2316 if (TREE_TYPE (subdatum) == error_mark_node)
2317 return error_mark_node;
2319 /* If this is an rvalue, it does not have qualifiers in C
2320 standard terms and we must avoid propagating such
2321 qualifiers down to a non-lvalue array that is then
2322 converted to a pointer. */
2323 use_datum_quals = (datum_lvalue
2324 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2326 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2327 if (use_datum_quals)
2328 quals |= TYPE_QUALS (TREE_TYPE (datum));
2329 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2331 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2332 NULL_TREE);
2333 SET_EXPR_LOCATION (ref, loc);
2334 if (TREE_READONLY (subdatum)
2335 || (use_datum_quals && TREE_READONLY (datum)))
2336 TREE_READONLY (ref) = 1;
2337 if (TREE_THIS_VOLATILE (subdatum)
2338 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2339 TREE_THIS_VOLATILE (ref) = 1;
2341 if (TREE_DEPRECATED (subdatum))
2342 warn_deprecated_use (subdatum, NULL_TREE);
2344 datum = ref;
2346 field = TREE_CHAIN (field);
2348 while (field);
2350 return ref;
2352 else if (code != ERROR_MARK)
2353 error_at (loc,
2354 "request for member %qE in something not a structure or union",
2355 component);
2357 return error_mark_node;
2360 /* Given an expression PTR for a pointer, return an expression
2361 for the value pointed to.
2362 ERRORSTRING is the name of the operator to appear in error messages.
2364 LOC is the location to use for the generated tree. */
2366 tree
2367 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2369 tree pointer = default_conversion (ptr);
2370 tree type = TREE_TYPE (pointer);
2371 tree ref;
2373 if (TREE_CODE (type) == POINTER_TYPE)
2375 if (CONVERT_EXPR_P (pointer)
2376 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2378 /* If a warning is issued, mark it to avoid duplicates from
2379 the backend. This only needs to be done at
2380 warn_strict_aliasing > 2. */
2381 if (warn_strict_aliasing > 2)
2382 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2383 type, TREE_OPERAND (pointer, 0)))
2384 TREE_NO_WARNING (pointer) = 1;
2387 if (TREE_CODE (pointer) == ADDR_EXPR
2388 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2389 == TREE_TYPE (type)))
2391 ref = TREE_OPERAND (pointer, 0);
2392 protected_set_expr_location (ref, loc);
2393 return ref;
2395 else
2397 tree t = TREE_TYPE (type);
2399 ref = build1 (INDIRECT_REF, t, pointer);
2401 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2403 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2405 error_at (loc, "dereferencing pointer to incomplete type "
2406 "%qT", t);
2407 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2409 return error_mark_node;
2411 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2412 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2414 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2415 so that we get the proper error message if the result is used
2416 to assign to. Also, &* is supposed to be a no-op.
2417 And ANSI C seems to specify that the type of the result
2418 should be the const type. */
2419 /* A de-reference of a pointer to const is not a const. It is valid
2420 to change it via some other pointer. */
2421 TREE_READONLY (ref) = TYPE_READONLY (t);
2422 TREE_SIDE_EFFECTS (ref)
2423 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2424 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2425 protected_set_expr_location (ref, loc);
2426 return ref;
2429 else if (TREE_CODE (pointer) != ERROR_MARK)
2430 invalid_indirection_error (loc, type, errstring);
2432 return error_mark_node;
2435 /* This handles expressions of the form "a[i]", which denotes
2436 an array reference.
2438 This is logically equivalent in C to *(a+i), but we may do it differently.
2439 If A is a variable or a member, we generate a primitive ARRAY_REF.
2440 This avoids forcing the array out of registers, and can work on
2441 arrays that are not lvalues (for example, members of structures returned
2442 by functions).
2444 For vector types, allow vector[i] but not i[vector], and create
2445 *(((type*)&vectortype) + i) for the expression.
2447 LOC is the location to use for the returned expression. */
2449 tree
2450 build_array_ref (location_t loc, tree array, tree index)
2452 tree ret;
2453 bool swapped = false;
2454 if (TREE_TYPE (array) == error_mark_node
2455 || TREE_TYPE (index) == error_mark_node)
2456 return error_mark_node;
2458 if (flag_cilkplus && contains_array_notation_expr (index))
2460 size_t rank = 0;
2461 if (!find_rank (loc, index, index, true, &rank))
2462 return error_mark_node;
2463 if (rank > 1)
2465 error_at (loc, "rank of the array's index is greater than 1");
2466 return error_mark_node;
2469 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2470 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2471 /* Allow vector[index] but not index[vector]. */
2472 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2474 tree temp;
2475 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2476 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2478 error_at (loc,
2479 "subscripted value is neither array nor pointer nor vector");
2481 return error_mark_node;
2483 temp = array;
2484 array = index;
2485 index = temp;
2486 swapped = true;
2489 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2491 error_at (loc, "array subscript is not an integer");
2492 return error_mark_node;
2495 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2497 error_at (loc, "subscripted value is pointer to function");
2498 return error_mark_node;
2501 /* ??? Existing practice has been to warn only when the char
2502 index is syntactically the index, not for char[array]. */
2503 if (!swapped)
2504 warn_array_subscript_with_type_char (index);
2506 /* Apply default promotions *after* noticing character types. */
2507 index = default_conversion (index);
2508 if (index == error_mark_node)
2509 return error_mark_node;
2511 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2513 bool non_lvalue
2514 = convert_vector_to_pointer_for_subscript (loc, &array, index);
2516 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2518 tree rval, type;
2520 /* An array that is indexed by a non-constant
2521 cannot be stored in a register; we must be able to do
2522 address arithmetic on its address.
2523 Likewise an array of elements of variable size. */
2524 if (TREE_CODE (index) != INTEGER_CST
2525 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2526 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2528 if (!c_mark_addressable (array))
2529 return error_mark_node;
2531 /* An array that is indexed by a constant value which is not within
2532 the array bounds cannot be stored in a register either; because we
2533 would get a crash in store_bit_field/extract_bit_field when trying
2534 to access a non-existent part of the register. */
2535 if (TREE_CODE (index) == INTEGER_CST
2536 && TYPE_DOMAIN (TREE_TYPE (array))
2537 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2539 if (!c_mark_addressable (array))
2540 return error_mark_node;
2543 if (pedantic || warn_c90_c99_compat)
2545 tree foo = array;
2546 while (TREE_CODE (foo) == COMPONENT_REF)
2547 foo = TREE_OPERAND (foo, 0);
2548 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2549 pedwarn (loc, OPT_Wpedantic,
2550 "ISO C forbids subscripting %<register%> array");
2551 else if (!lvalue_p (foo))
2552 pedwarn_c90 (loc, OPT_Wpedantic,
2553 "ISO C90 forbids subscripting non-lvalue "
2554 "array");
2557 type = TREE_TYPE (TREE_TYPE (array));
2558 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2559 /* Array ref is const/volatile if the array elements are
2560 or if the array is. */
2561 TREE_READONLY (rval)
2562 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2563 | TREE_READONLY (array));
2564 TREE_SIDE_EFFECTS (rval)
2565 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2566 | TREE_SIDE_EFFECTS (array));
2567 TREE_THIS_VOLATILE (rval)
2568 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2569 /* This was added by rms on 16 Nov 91.
2570 It fixes vol struct foo *a; a->elts[1]
2571 in an inline function.
2572 Hope it doesn't break something else. */
2573 | TREE_THIS_VOLATILE (array));
2574 ret = require_complete_type (rval);
2575 protected_set_expr_location (ret, loc);
2576 if (non_lvalue)
2577 ret = non_lvalue_loc (loc, ret);
2578 return ret;
2580 else
2582 tree ar = default_conversion (array);
2584 if (ar == error_mark_node)
2585 return ar;
2587 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2588 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2590 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2591 index, 0),
2592 RO_ARRAY_INDEXING);
2593 if (non_lvalue)
2594 ret = non_lvalue_loc (loc, ret);
2595 return ret;
2599 /* Build an external reference to identifier ID. FUN indicates
2600 whether this will be used for a function call. LOC is the source
2601 location of the identifier. This sets *TYPE to the type of the
2602 identifier, which is not the same as the type of the returned value
2603 for CONST_DECLs defined as enum constants. If the type of the
2604 identifier is not available, *TYPE is set to NULL. */
2605 tree
2606 build_external_ref (location_t loc, tree id, int fun, tree *type)
2608 tree ref;
2609 tree decl = lookup_name (id);
2611 /* In Objective-C, an instance variable (ivar) may be preferred to
2612 whatever lookup_name() found. */
2613 decl = objc_lookup_ivar (decl, id);
2615 *type = NULL;
2616 if (decl && decl != error_mark_node)
2618 ref = decl;
2619 *type = TREE_TYPE (ref);
2621 else if (fun)
2622 /* Implicit function declaration. */
2623 ref = implicitly_declare (loc, id);
2624 else if (decl == error_mark_node)
2625 /* Don't complain about something that's already been
2626 complained about. */
2627 return error_mark_node;
2628 else
2630 undeclared_variable (loc, id);
2631 return error_mark_node;
2634 if (TREE_TYPE (ref) == error_mark_node)
2635 return error_mark_node;
2637 if (TREE_DEPRECATED (ref))
2638 warn_deprecated_use (ref, NULL_TREE);
2640 /* Recursive call does not count as usage. */
2641 if (ref != current_function_decl)
2643 TREE_USED (ref) = 1;
2646 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2648 if (!in_sizeof && !in_typeof)
2649 C_DECL_USED (ref) = 1;
2650 else if (DECL_INITIAL (ref) == 0
2651 && DECL_EXTERNAL (ref)
2652 && !TREE_PUBLIC (ref))
2653 record_maybe_used_decl (ref);
2656 if (TREE_CODE (ref) == CONST_DECL)
2658 used_types_insert (TREE_TYPE (ref));
2660 if (warn_cxx_compat
2661 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2662 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2664 warning_at (loc, OPT_Wc___compat,
2665 ("enum constant defined in struct or union "
2666 "is not visible in C++"));
2667 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2670 ref = DECL_INITIAL (ref);
2671 TREE_CONSTANT (ref) = 1;
2673 else if (current_function_decl != 0
2674 && !DECL_FILE_SCOPE_P (current_function_decl)
2675 && (TREE_CODE (ref) == VAR_DECL
2676 || TREE_CODE (ref) == PARM_DECL
2677 || TREE_CODE (ref) == FUNCTION_DECL))
2679 tree context = decl_function_context (ref);
2681 if (context != 0 && context != current_function_decl)
2682 DECL_NONLOCAL (ref) = 1;
2684 /* C99 6.7.4p3: An inline definition of a function with external
2685 linkage ... shall not contain a reference to an identifier with
2686 internal linkage. */
2687 else if (current_function_decl != 0
2688 && DECL_DECLARED_INLINE_P (current_function_decl)
2689 && DECL_EXTERNAL (current_function_decl)
2690 && VAR_OR_FUNCTION_DECL_P (ref)
2691 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2692 && ! TREE_PUBLIC (ref)
2693 && DECL_CONTEXT (ref) != current_function_decl)
2694 record_inline_static (loc, current_function_decl, ref,
2695 csi_internal);
2697 return ref;
2700 /* Record details of decls possibly used inside sizeof or typeof. */
2701 struct maybe_used_decl
2703 /* The decl. */
2704 tree decl;
2705 /* The level seen at (in_sizeof + in_typeof). */
2706 int level;
2707 /* The next one at this level or above, or NULL. */
2708 struct maybe_used_decl *next;
2711 static struct maybe_used_decl *maybe_used_decls;
2713 /* Record that DECL, an undefined static function reference seen
2714 inside sizeof or typeof, might be used if the operand of sizeof is
2715 a VLA type or the operand of typeof is a variably modified
2716 type. */
2718 static void
2719 record_maybe_used_decl (tree decl)
2721 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2722 t->decl = decl;
2723 t->level = in_sizeof + in_typeof;
2724 t->next = maybe_used_decls;
2725 maybe_used_decls = t;
2728 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2729 USED is false, just discard them. If it is true, mark them used
2730 (if no longer inside sizeof or typeof) or move them to the next
2731 level up (if still inside sizeof or typeof). */
2733 void
2734 pop_maybe_used (bool used)
2736 struct maybe_used_decl *p = maybe_used_decls;
2737 int cur_level = in_sizeof + in_typeof;
2738 while (p && p->level > cur_level)
2740 if (used)
2742 if (cur_level == 0)
2743 C_DECL_USED (p->decl) = 1;
2744 else
2745 p->level = cur_level;
2747 p = p->next;
2749 if (!used || cur_level == 0)
2750 maybe_used_decls = p;
2753 /* Return the result of sizeof applied to EXPR. */
2755 struct c_expr
2756 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2758 struct c_expr ret;
2759 if (expr.value == error_mark_node)
2761 ret.value = error_mark_node;
2762 ret.original_code = ERROR_MARK;
2763 ret.original_type = NULL;
2764 pop_maybe_used (false);
2766 else
2768 bool expr_const_operands = true;
2770 if (TREE_CODE (expr.value) == PARM_DECL
2771 && C_ARRAY_PARAMETER (expr.value))
2773 if (warning_at (loc, OPT_Wsizeof_array_argument,
2774 "%<sizeof%> on array function parameter %qE will "
2775 "return size of %qT", expr.value,
2776 expr.original_type))
2777 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2779 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2780 &expr_const_operands);
2781 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2782 c_last_sizeof_arg = expr.value;
2783 ret.original_code = SIZEOF_EXPR;
2784 ret.original_type = NULL;
2785 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2787 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2788 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2789 folded_expr, ret.value);
2790 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2791 SET_EXPR_LOCATION (ret.value, loc);
2793 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2795 return ret;
2798 /* Return the result of sizeof applied to T, a structure for the type
2799 name passed to sizeof (rather than the type itself). LOC is the
2800 location of the original expression. */
2802 struct c_expr
2803 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2805 tree type;
2806 struct c_expr ret;
2807 tree type_expr = NULL_TREE;
2808 bool type_expr_const = true;
2809 type = groktypename (t, &type_expr, &type_expr_const);
2810 ret.value = c_sizeof (loc, type);
2811 c_last_sizeof_arg = type;
2812 ret.original_code = SIZEOF_EXPR;
2813 ret.original_type = NULL;
2814 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2815 && c_vla_type_p (type))
2817 /* If the type is a [*] array, it is a VLA but is represented as
2818 having a size of zero. In such a case we must ensure that
2819 the result of sizeof does not get folded to a constant by
2820 c_fully_fold, because if the size is evaluated the result is
2821 not constant and so constraints on zero or negative size
2822 arrays must not be applied when this sizeof call is inside
2823 another array declarator. */
2824 if (!type_expr)
2825 type_expr = integer_zero_node;
2826 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2827 type_expr, ret.value);
2828 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2830 pop_maybe_used (type != error_mark_node
2831 ? C_TYPE_VARIABLE_SIZE (type) : false);
2832 return ret;
2835 /* Build a function call to function FUNCTION with parameters PARAMS.
2836 The function call is at LOC.
2837 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2838 TREE_VALUE of each node is a parameter-expression.
2839 FUNCTION's data type may be a function type or a pointer-to-function. */
2841 tree
2842 build_function_call (location_t loc, tree function, tree params)
2844 vec<tree, va_gc> *v;
2845 tree ret;
2847 vec_alloc (v, list_length (params));
2848 for (; params; params = TREE_CHAIN (params))
2849 v->quick_push (TREE_VALUE (params));
2850 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2851 vec_free (v);
2852 return ret;
2855 /* Give a note about the location of the declaration of DECL. */
2857 static void inform_declaration (tree decl)
2859 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2860 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2863 /* Build a function call to function FUNCTION with parameters PARAMS.
2864 ORIGTYPES, if not NULL, is a vector of types; each element is
2865 either NULL or the original type of the corresponding element in
2866 PARAMS. The original type may differ from TREE_TYPE of the
2867 parameter for enums. FUNCTION's data type may be a function type
2868 or pointer-to-function. This function changes the elements of
2869 PARAMS. */
2871 tree
2872 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2873 tree function, vec<tree, va_gc> *params,
2874 vec<tree, va_gc> *origtypes)
2876 tree fntype, fundecl = 0;
2877 tree name = NULL_TREE, result;
2878 tree tem;
2879 int nargs;
2880 tree *argarray;
2883 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2884 STRIP_TYPE_NOPS (function);
2886 /* Convert anything with function type to a pointer-to-function. */
2887 if (TREE_CODE (function) == FUNCTION_DECL)
2889 name = DECL_NAME (function);
2891 if (flag_tm)
2892 tm_malloc_replacement (function);
2893 fundecl = function;
2894 /* Atomic functions have type checking/casting already done. They are
2895 often rewritten and don't match the original parameter list. */
2896 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2897 origtypes = NULL;
2899 if (flag_cilkplus
2900 && is_cilkplus_reduce_builtin (function))
2901 origtypes = NULL;
2903 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2904 function = function_to_pointer_conversion (loc, function);
2906 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2907 expressions, like those used for ObjC messenger dispatches. */
2908 if (params && !params->is_empty ())
2909 function = objc_rewrite_function_call (function, (*params)[0]);
2911 function = c_fully_fold (function, false, NULL);
2913 fntype = TREE_TYPE (function);
2915 if (TREE_CODE (fntype) == ERROR_MARK)
2916 return error_mark_node;
2918 if (!(TREE_CODE (fntype) == POINTER_TYPE
2919 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2921 if (!flag_diagnostics_show_caret)
2922 error_at (loc,
2923 "called object %qE is not a function or function pointer",
2924 function);
2925 else if (DECL_P (function))
2927 error_at (loc,
2928 "called object %qD is not a function or function pointer",
2929 function);
2930 inform_declaration (function);
2932 else
2933 error_at (loc,
2934 "called object is not a function or function pointer");
2935 return error_mark_node;
2938 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2939 current_function_returns_abnormally = 1;
2941 /* fntype now gets the type of function pointed to. */
2942 fntype = TREE_TYPE (fntype);
2944 /* Convert the parameters to the types declared in the
2945 function prototype, or apply default promotions. */
2947 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2948 origtypes, function, fundecl);
2949 if (nargs < 0)
2950 return error_mark_node;
2952 /* Check that the function is called through a compatible prototype.
2953 If it is not, warn. */
2954 if (CONVERT_EXPR_P (function)
2955 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2956 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2957 && !comptypes (fntype, TREE_TYPE (tem)))
2959 tree return_type = TREE_TYPE (fntype);
2961 /* This situation leads to run-time undefined behavior. We can't,
2962 therefore, simply error unless we can prove that all possible
2963 executions of the program must execute the code. */
2964 warning_at (loc, 0, "function called through a non-compatible type");
2966 if (VOID_TYPE_P (return_type)
2967 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2968 pedwarn (loc, 0,
2969 "function with qualified void return type called");
2972 argarray = vec_safe_address (params);
2974 /* Check that arguments to builtin functions match the expectations. */
2975 if (fundecl
2976 && DECL_BUILT_IN (fundecl)
2977 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2978 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2979 return error_mark_node;
2981 /* Check that the arguments to the function are valid. */
2982 check_function_arguments (fntype, nargs, argarray);
2984 if (name != NULL_TREE
2985 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2987 if (require_constant_value)
2988 result =
2989 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2990 function, nargs, argarray);
2991 else
2992 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2993 function, nargs, argarray);
2994 if (TREE_CODE (result) == NOP_EXPR
2995 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2996 STRIP_TYPE_NOPS (result);
2998 else
2999 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3000 function, nargs, argarray);
3002 if (VOID_TYPE_P (TREE_TYPE (result)))
3004 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3005 pedwarn (loc, 0,
3006 "function with qualified void return type called");
3007 return result;
3009 return require_complete_type (result);
3012 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3014 tree
3015 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3016 tree function, vec<tree, va_gc> *params,
3017 vec<tree, va_gc> *origtypes)
3019 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3020 STRIP_TYPE_NOPS (function);
3022 /* Convert anything with function type to a pointer-to-function. */
3023 if (TREE_CODE (function) == FUNCTION_DECL)
3025 /* Implement type-directed function overloading for builtins.
3026 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3027 handle all the type checking. The result is a complete expression
3028 that implements this function call. */
3029 tree tem = resolve_overloaded_builtin (loc, function, params);
3030 if (tem)
3031 return tem;
3033 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3036 /* Convert the argument expressions in the vector VALUES
3037 to the types in the list TYPELIST.
3039 If TYPELIST is exhausted, or when an element has NULL as its type,
3040 perform the default conversions.
3042 ORIGTYPES is the original types of the expressions in VALUES. This
3043 holds the type of enum values which have been converted to integral
3044 types. It may be NULL.
3046 FUNCTION is a tree for the called function. It is used only for
3047 error messages, where it is formatted with %qE.
3049 This is also where warnings about wrong number of args are generated.
3051 ARG_LOC are locations of function arguments (if any).
3053 Returns the actual number of arguments processed (which may be less
3054 than the length of VALUES in some error situations), or -1 on
3055 failure. */
3057 static int
3058 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3059 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3060 tree function, tree fundecl)
3062 tree typetail, val;
3063 unsigned int parmnum;
3064 bool error_args = false;
3065 const bool type_generic = fundecl
3066 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3067 bool type_generic_remove_excess_precision = false;
3068 tree selector;
3070 /* Change pointer to function to the function itself for
3071 diagnostics. */
3072 if (TREE_CODE (function) == ADDR_EXPR
3073 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3074 function = TREE_OPERAND (function, 0);
3076 /* Handle an ObjC selector specially for diagnostics. */
3077 selector = objc_message_selector ();
3079 /* For type-generic built-in functions, determine whether excess
3080 precision should be removed (classification) or not
3081 (comparison). */
3082 if (type_generic
3083 && DECL_BUILT_IN (fundecl)
3084 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3086 switch (DECL_FUNCTION_CODE (fundecl))
3088 case BUILT_IN_ISFINITE:
3089 case BUILT_IN_ISINF:
3090 case BUILT_IN_ISINF_SIGN:
3091 case BUILT_IN_ISNAN:
3092 case BUILT_IN_ISNORMAL:
3093 case BUILT_IN_FPCLASSIFY:
3094 type_generic_remove_excess_precision = true;
3095 break;
3097 default:
3098 type_generic_remove_excess_precision = false;
3099 break;
3102 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3103 return vec_safe_length (values);
3105 /* Scan the given expressions and types, producing individual
3106 converted arguments. */
3108 for (typetail = typelist, parmnum = 0;
3109 values && values->iterate (parmnum, &val);
3110 ++parmnum)
3112 tree type = typetail ? TREE_VALUE (typetail) : 0;
3113 tree valtype = TREE_TYPE (val);
3114 tree rname = function;
3115 int argnum = parmnum + 1;
3116 const char *invalid_func_diag;
3117 bool excess_precision = false;
3118 bool npc;
3119 tree parmval;
3120 /* Some __atomic_* builtins have additional hidden argument at
3121 position 0. */
3122 location_t ploc
3123 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3124 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3125 : input_location;
3127 if (type == void_type_node)
3129 if (selector)
3130 error_at (loc, "too many arguments to method %qE", selector);
3131 else
3132 error_at (loc, "too many arguments to function %qE", function);
3133 inform_declaration (fundecl);
3134 return parmnum;
3137 if (selector && argnum > 2)
3139 rname = selector;
3140 argnum -= 2;
3143 npc = null_pointer_constant_p (val);
3145 /* If there is excess precision and a prototype, convert once to
3146 the required type rather than converting via the semantic
3147 type. Likewise without a prototype a float value represented
3148 as long double should be converted once to double. But for
3149 type-generic classification functions excess precision must
3150 be removed here. */
3151 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3152 && (type || !type_generic || !type_generic_remove_excess_precision))
3154 val = TREE_OPERAND (val, 0);
3155 excess_precision = true;
3157 val = c_fully_fold (val, false, NULL);
3158 STRIP_TYPE_NOPS (val);
3160 val = require_complete_type (val);
3162 if (type != 0)
3164 /* Formal parm type is specified by a function prototype. */
3166 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3168 error_at (ploc, "type of formal parameter %d is incomplete",
3169 parmnum + 1);
3170 parmval = val;
3172 else
3174 tree origtype;
3176 /* Optionally warn about conversions that
3177 differ from the default conversions. */
3178 if (warn_traditional_conversion || warn_traditional)
3180 unsigned int formal_prec = TYPE_PRECISION (type);
3182 if (INTEGRAL_TYPE_P (type)
3183 && TREE_CODE (valtype) == REAL_TYPE)
3184 warning_at (ploc, OPT_Wtraditional_conversion,
3185 "passing argument %d of %qE as integer rather "
3186 "than floating due to prototype",
3187 argnum, rname);
3188 if (INTEGRAL_TYPE_P (type)
3189 && TREE_CODE (valtype) == COMPLEX_TYPE)
3190 warning_at (ploc, OPT_Wtraditional_conversion,
3191 "passing argument %d of %qE as integer rather "
3192 "than complex due to prototype",
3193 argnum, rname);
3194 else if (TREE_CODE (type) == COMPLEX_TYPE
3195 && TREE_CODE (valtype) == REAL_TYPE)
3196 warning_at (ploc, OPT_Wtraditional_conversion,
3197 "passing argument %d of %qE as complex rather "
3198 "than floating due to prototype",
3199 argnum, rname);
3200 else if (TREE_CODE (type) == REAL_TYPE
3201 && INTEGRAL_TYPE_P (valtype))
3202 warning_at (ploc, OPT_Wtraditional_conversion,
3203 "passing argument %d of %qE as floating rather "
3204 "than integer due to prototype",
3205 argnum, rname);
3206 else if (TREE_CODE (type) == COMPLEX_TYPE
3207 && INTEGRAL_TYPE_P (valtype))
3208 warning_at (ploc, OPT_Wtraditional_conversion,
3209 "passing argument %d of %qE as complex rather "
3210 "than integer due to prototype",
3211 argnum, rname);
3212 else if (TREE_CODE (type) == REAL_TYPE
3213 && TREE_CODE (valtype) == COMPLEX_TYPE)
3214 warning_at (ploc, OPT_Wtraditional_conversion,
3215 "passing argument %d of %qE as floating rather "
3216 "than complex due to prototype",
3217 argnum, rname);
3218 /* ??? At some point, messages should be written about
3219 conversions between complex types, but that's too messy
3220 to do now. */
3221 else if (TREE_CODE (type) == REAL_TYPE
3222 && TREE_CODE (valtype) == REAL_TYPE)
3224 /* Warn if any argument is passed as `float',
3225 since without a prototype it would be `double'. */
3226 if (formal_prec == TYPE_PRECISION (float_type_node)
3227 && type != dfloat32_type_node)
3228 warning_at (ploc, 0,
3229 "passing argument %d of %qE as %<float%> "
3230 "rather than %<double%> due to prototype",
3231 argnum, rname);
3233 /* Warn if mismatch between argument and prototype
3234 for decimal float types. Warn of conversions with
3235 binary float types and of precision narrowing due to
3236 prototype. */
3237 else if (type != valtype
3238 && (type == dfloat32_type_node
3239 || type == dfloat64_type_node
3240 || type == dfloat128_type_node
3241 || valtype == dfloat32_type_node
3242 || valtype == dfloat64_type_node
3243 || valtype == dfloat128_type_node)
3244 && (formal_prec
3245 <= TYPE_PRECISION (valtype)
3246 || (type == dfloat128_type_node
3247 && (valtype
3248 != dfloat64_type_node
3249 && (valtype
3250 != dfloat32_type_node)))
3251 || (type == dfloat64_type_node
3252 && (valtype
3253 != dfloat32_type_node))))
3254 warning_at (ploc, 0,
3255 "passing argument %d of %qE as %qT "
3256 "rather than %qT due to prototype",
3257 argnum, rname, type, valtype);
3260 /* Detect integer changing in width or signedness.
3261 These warnings are only activated with
3262 -Wtraditional-conversion, not with -Wtraditional. */
3263 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3264 && INTEGRAL_TYPE_P (valtype))
3266 tree would_have_been = default_conversion (val);
3267 tree type1 = TREE_TYPE (would_have_been);
3269 if (TREE_CODE (type) == ENUMERAL_TYPE
3270 && (TYPE_MAIN_VARIANT (type)
3271 == TYPE_MAIN_VARIANT (valtype)))
3272 /* No warning if function asks for enum
3273 and the actual arg is that enum type. */
3275 else if (formal_prec != TYPE_PRECISION (type1))
3276 warning_at (ploc, OPT_Wtraditional_conversion,
3277 "passing argument %d of %qE "
3278 "with different width due to prototype",
3279 argnum, rname);
3280 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3282 /* Don't complain if the formal parameter type
3283 is an enum, because we can't tell now whether
3284 the value was an enum--even the same enum. */
3285 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3287 else if (TREE_CODE (val) == INTEGER_CST
3288 && int_fits_type_p (val, type))
3289 /* Change in signedness doesn't matter
3290 if a constant value is unaffected. */
3292 /* If the value is extended from a narrower
3293 unsigned type, it doesn't matter whether we
3294 pass it as signed or unsigned; the value
3295 certainly is the same either way. */
3296 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3297 && TYPE_UNSIGNED (valtype))
3299 else if (TYPE_UNSIGNED (type))
3300 warning_at (ploc, OPT_Wtraditional_conversion,
3301 "passing argument %d of %qE "
3302 "as unsigned due to prototype",
3303 argnum, rname);
3304 else
3305 warning_at (ploc, OPT_Wtraditional_conversion,
3306 "passing argument %d of %qE "
3307 "as signed due to prototype",
3308 argnum, rname);
3312 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3313 sake of better warnings from convert_and_check. */
3314 if (excess_precision)
3315 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3316 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3317 parmval = convert_for_assignment (loc, ploc, type,
3318 val, origtype, ic_argpass,
3319 npc, fundecl, function,
3320 parmnum + 1);
3322 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3323 && INTEGRAL_TYPE_P (type)
3324 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3325 parmval = default_conversion (parmval);
3328 else if (TREE_CODE (valtype) == REAL_TYPE
3329 && (TYPE_PRECISION (valtype)
3330 <= TYPE_PRECISION (double_type_node))
3331 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3332 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3333 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3335 if (type_generic)
3336 parmval = val;
3337 else
3339 /* Convert `float' to `double'. */
3340 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3341 warning_at (ploc, OPT_Wdouble_promotion,
3342 "implicit conversion from %qT to %qT when passing "
3343 "argument to function",
3344 valtype, double_type_node);
3345 parmval = convert (double_type_node, val);
3348 else if (excess_precision && !type_generic)
3349 /* A "double" argument with excess precision being passed
3350 without a prototype or in variable arguments. */
3351 parmval = convert (valtype, val);
3352 else if ((invalid_func_diag =
3353 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3355 error (invalid_func_diag);
3356 return -1;
3358 else
3359 /* Convert `short' and `char' to full-size `int'. */
3360 parmval = default_conversion (val);
3362 (*values)[parmnum] = parmval;
3363 if (parmval == error_mark_node)
3364 error_args = true;
3366 if (typetail)
3367 typetail = TREE_CHAIN (typetail);
3370 gcc_assert (parmnum == vec_safe_length (values));
3372 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3374 error_at (loc, "too few arguments to function %qE", function);
3375 inform_declaration (fundecl);
3376 return -1;
3379 return error_args ? -1 : (int) parmnum;
3382 /* This is the entry point used by the parser to build unary operators
3383 in the input. CODE, a tree_code, specifies the unary operator, and
3384 ARG is the operand. For unary plus, the C parser currently uses
3385 CONVERT_EXPR for code.
3387 LOC is the location to use for the tree generated.
3390 struct c_expr
3391 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3393 struct c_expr result;
3395 result.value = build_unary_op (loc, code, arg.value, 0);
3396 result.original_code = code;
3397 result.original_type = NULL;
3399 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3400 overflow_warning (loc, result.value);
3402 return result;
3405 /* This is the entry point used by the parser to build binary operators
3406 in the input. CODE, a tree_code, specifies the binary operator, and
3407 ARG1 and ARG2 are the operands. In addition to constructing the
3408 expression, we check for operands that were written with other binary
3409 operators in a way that is likely to confuse the user.
3411 LOCATION is the location of the binary operator. */
3413 struct c_expr
3414 parser_build_binary_op (location_t location, enum tree_code code,
3415 struct c_expr arg1, struct c_expr arg2)
3417 struct c_expr result;
3419 enum tree_code code1 = arg1.original_code;
3420 enum tree_code code2 = arg2.original_code;
3421 tree type1 = (arg1.original_type
3422 ? arg1.original_type
3423 : TREE_TYPE (arg1.value));
3424 tree type2 = (arg2.original_type
3425 ? arg2.original_type
3426 : TREE_TYPE (arg2.value));
3428 result.value = build_binary_op (location, code,
3429 arg1.value, arg2.value, 1);
3430 result.original_code = code;
3431 result.original_type = NULL;
3433 if (TREE_CODE (result.value) == ERROR_MARK)
3434 return result;
3436 if (location != UNKNOWN_LOCATION)
3437 protected_set_expr_location (result.value, location);
3439 /* Check for cases such as x+y<<z which users are likely
3440 to misinterpret. */
3441 if (warn_parentheses)
3442 warn_about_parentheses (location, code, code1, arg1.value, code2,
3443 arg2.value);
3445 if (warn_logical_op)
3446 warn_logical_operator (location, code, TREE_TYPE (result.value),
3447 code1, arg1.value, code2, arg2.value);
3449 if (warn_logical_not_paren
3450 && code1 == TRUTH_NOT_EXPR
3451 && code2 != TRUTH_NOT_EXPR)
3452 warn_logical_not_parentheses (location, code, arg2.value);
3454 /* Warn about comparisons against string literals, with the exception
3455 of testing for equality or inequality of a string literal with NULL. */
3456 if (code == EQ_EXPR || code == NE_EXPR)
3458 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3459 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3460 warning_at (location, OPT_Waddress,
3461 "comparison with string literal results in unspecified behavior");
3463 else if (TREE_CODE_CLASS (code) == tcc_comparison
3464 && (code1 == STRING_CST || code2 == STRING_CST))
3465 warning_at (location, OPT_Waddress,
3466 "comparison with string literal results in unspecified behavior");
3468 if (TREE_OVERFLOW_P (result.value)
3469 && !TREE_OVERFLOW_P (arg1.value)
3470 && !TREE_OVERFLOW_P (arg2.value))
3471 overflow_warning (location, result.value);
3473 /* Warn about comparisons of different enum types. */
3474 if (warn_enum_compare
3475 && TREE_CODE_CLASS (code) == tcc_comparison
3476 && TREE_CODE (type1) == ENUMERAL_TYPE
3477 && TREE_CODE (type2) == ENUMERAL_TYPE
3478 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3479 warning_at (location, OPT_Wenum_compare,
3480 "comparison between %qT and %qT",
3481 type1, type2);
3483 return result;
3486 /* Return a tree for the difference of pointers OP0 and OP1.
3487 The resulting tree has type int. */
3489 static tree
3490 pointer_diff (location_t loc, tree op0, tree op1)
3492 tree restype = ptrdiff_type_node;
3493 tree result, inttype;
3495 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3496 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3497 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3498 tree orig_op1 = op1;
3500 /* If the operands point into different address spaces, we need to
3501 explicitly convert them to pointers into the common address space
3502 before we can subtract the numerical address values. */
3503 if (as0 != as1)
3505 addr_space_t as_common;
3506 tree common_type;
3508 /* Determine the common superset address space. This is guaranteed
3509 to exist because the caller verified that comp_target_types
3510 returned non-zero. */
3511 if (!addr_space_superset (as0, as1, &as_common))
3512 gcc_unreachable ();
3514 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3515 op0 = convert (common_type, op0);
3516 op1 = convert (common_type, op1);
3519 /* Determine integer type to perform computations in. This will usually
3520 be the same as the result type (ptrdiff_t), but may need to be a wider
3521 type if pointers for the address space are wider than ptrdiff_t. */
3522 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3523 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3524 else
3525 inttype = restype;
3527 if (TREE_CODE (target_type) == VOID_TYPE)
3528 pedwarn (loc, OPT_Wpointer_arith,
3529 "pointer of type %<void *%> used in subtraction");
3530 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3531 pedwarn (loc, OPT_Wpointer_arith,
3532 "pointer to a function used in subtraction");
3534 /* First do the subtraction as integers;
3535 then drop through to build the divide operator.
3536 Do not do default conversions on the minus operator
3537 in case restype is a short type. */
3539 op0 = build_binary_op (loc,
3540 MINUS_EXPR, convert (inttype, op0),
3541 convert (inttype, op1), 0);
3542 /* This generates an error if op1 is pointer to incomplete type. */
3543 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3544 error_at (loc, "arithmetic on pointer to an incomplete type");
3546 op1 = c_size_in_bytes (target_type);
3548 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3549 error_at (loc, "arithmetic on pointer to an empty aggregate");
3551 /* Divide by the size, in easiest possible way. */
3552 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3553 op0, convert (inttype, op1));
3555 /* Convert to final result type if necessary. */
3556 return convert (restype, result);
3559 /* Expand atomic compound assignments into an approriate sequence as
3560 specified by the C11 standard section 6.5.16.2.
3561 given
3562 _Atomic T1 E1
3563 T2 E2
3564 E1 op= E2
3566 This sequence is used for all types for which these operations are
3567 supported.
3569 In addition, built-in versions of the 'fe' prefixed routines may
3570 need to be invoked for floating point (real, complex or vector) when
3571 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3573 T1 newval;
3574 T1 old;
3575 T1 *addr
3576 T2 val
3577 fenv_t fenv
3579 addr = &E1;
3580 val = (E2);
3581 __atomic_load (addr, &old, SEQ_CST);
3582 feholdexcept (&fenv);
3583 loop:
3584 newval = old op val;
3585 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3586 SEQ_CST))
3587 goto done;
3588 feclearexcept (FE_ALL_EXCEPT);
3589 goto loop:
3590 done:
3591 feupdateenv (&fenv);
3593 Also note that the compiler is simply issuing the generic form of
3594 the atomic operations. This requires temp(s) and has their address
3595 taken. The atomic processing is smart enough to figure out when the
3596 size of an object can utilize a lock-free version, and convert the
3597 built-in call to the appropriate lock-free routine. The optimizers
3598 will then dispose of any temps that are no longer required, and
3599 lock-free implementations are utilized as long as there is target
3600 support for the required size.
3602 If the operator is NOP_EXPR, then this is a simple assignment, and
3603 an __atomic_store is issued to perform the assignment rather than
3604 the above loop.
3608 /* Build an atomic assignment at LOC, expanding into the proper
3609 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3610 the result of the operation, unless RETURN_OLD_P in which case
3611 return the old value of LHS (this is only for postincrement and
3612 postdecrement). */
3613 static tree
3614 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3615 tree rhs, bool return_old_p)
3617 tree fndecl, func_call;
3618 vec<tree, va_gc> *params;
3619 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3620 tree old, old_addr;
3621 tree compound_stmt;
3622 tree stmt, goto_stmt;
3623 tree loop_label, loop_decl, done_label, done_decl;
3625 tree lhs_type = TREE_TYPE (lhs);
3626 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3627 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3628 tree rhs_type = TREE_TYPE (rhs);
3630 gcc_assert (TYPE_ATOMIC (lhs_type));
3632 if (return_old_p)
3633 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3635 /* Allocate enough vector items for a compare_exchange. */
3636 vec_alloc (params, 6);
3638 /* Create a compound statement to hold the sequence of statements
3639 with a loop. */
3640 compound_stmt = c_begin_compound_stmt (false);
3642 /* Fold the RHS if it hasn't already been folded. */
3643 if (modifycode != NOP_EXPR)
3644 rhs = c_fully_fold (rhs, false, NULL);
3646 /* Remove the qualifiers for the rest of the expressions and create
3647 the VAL temp variable to hold the RHS. */
3648 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3649 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3650 val = create_tmp_var (nonatomic_rhs_type);
3651 TREE_ADDRESSABLE (val) = 1;
3652 TREE_NO_WARNING (val) = 1;
3653 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3654 SET_EXPR_LOCATION (rhs, loc);
3655 add_stmt (rhs);
3657 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3658 an atomic_store. */
3659 if (modifycode == NOP_EXPR)
3661 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3662 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3663 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3664 params->quick_push (lhs_addr);
3665 params->quick_push (rhs);
3666 params->quick_push (seq_cst);
3667 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3668 add_stmt (func_call);
3670 /* Finish the compound statement. */
3671 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3673 /* VAL is the value which was stored, return a COMPOUND_STMT of
3674 the statement and that value. */
3675 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3678 /* Create the variables and labels required for the op= form. */
3679 old = create_tmp_var (nonatomic_lhs_type);
3680 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3681 TREE_ADDRESSABLE (old) = 1;
3682 TREE_NO_WARNING (old) = 1;
3684 newval = create_tmp_var (nonatomic_lhs_type);
3685 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3686 TREE_ADDRESSABLE (newval) = 1;
3688 loop_decl = create_artificial_label (loc);
3689 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3691 done_decl = create_artificial_label (loc);
3692 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3694 /* __atomic_load (addr, &old, SEQ_CST). */
3695 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3696 params->quick_push (lhs_addr);
3697 params->quick_push (old_addr);
3698 params->quick_push (seq_cst);
3699 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3700 add_stmt (func_call);
3701 params->truncate (0);
3703 /* Create the expressions for floating-point environment
3704 manipulation, if required. */
3705 bool need_fenv = (flag_trapping_math
3706 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3707 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3708 if (need_fenv)
3709 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3711 if (hold_call)
3712 add_stmt (hold_call);
3714 /* loop: */
3715 add_stmt (loop_label);
3717 /* newval = old + val; */
3718 rhs = build_binary_op (loc, modifycode, old, val, 1);
3719 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3720 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3721 NULL_TREE, 0);
3722 if (rhs != error_mark_node)
3724 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3725 SET_EXPR_LOCATION (rhs, loc);
3726 add_stmt (rhs);
3729 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3730 goto done; */
3731 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3732 params->quick_push (lhs_addr);
3733 params->quick_push (old_addr);
3734 params->quick_push (newval_addr);
3735 params->quick_push (integer_zero_node);
3736 params->quick_push (seq_cst);
3737 params->quick_push (seq_cst);
3738 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3740 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3741 SET_EXPR_LOCATION (goto_stmt, loc);
3743 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3744 SET_EXPR_LOCATION (stmt, loc);
3745 add_stmt (stmt);
3747 if (clear_call)
3748 add_stmt (clear_call);
3750 /* goto loop; */
3751 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3752 SET_EXPR_LOCATION (goto_stmt, loc);
3753 add_stmt (goto_stmt);
3755 /* done: */
3756 add_stmt (done_label);
3758 if (update_call)
3759 add_stmt (update_call);
3761 /* Finish the compound statement. */
3762 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3764 /* NEWVAL is the value that was successfully stored, return a
3765 COMPOUND_EXPR of the statement and the appropriate value. */
3766 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3767 return_old_p ? old : newval);
3770 /* Construct and perhaps optimize a tree representation
3771 for a unary operation. CODE, a tree_code, specifies the operation
3772 and XARG is the operand.
3773 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3774 the default promotions (such as from short to int).
3775 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3776 allows non-lvalues; this is only used to handle conversion of non-lvalue
3777 arrays to pointers in C99.
3779 LOCATION is the location of the operator. */
3781 tree
3782 build_unary_op (location_t location,
3783 enum tree_code code, tree xarg, int flag)
3785 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3786 tree arg = xarg;
3787 tree argtype = 0;
3788 enum tree_code typecode;
3789 tree val;
3790 tree ret = error_mark_node;
3791 tree eptype = NULL_TREE;
3792 int noconvert = flag;
3793 const char *invalid_op_diag;
3794 bool int_operands;
3796 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3797 if (int_operands)
3798 arg = remove_c_maybe_const_expr (arg);
3800 if (code != ADDR_EXPR)
3801 arg = require_complete_type (arg);
3803 typecode = TREE_CODE (TREE_TYPE (arg));
3804 if (typecode == ERROR_MARK)
3805 return error_mark_node;
3806 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3807 typecode = INTEGER_TYPE;
3809 if ((invalid_op_diag
3810 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3812 error_at (location, invalid_op_diag);
3813 return error_mark_node;
3816 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3818 eptype = TREE_TYPE (arg);
3819 arg = TREE_OPERAND (arg, 0);
3822 switch (code)
3824 case CONVERT_EXPR:
3825 /* This is used for unary plus, because a CONVERT_EXPR
3826 is enough to prevent anybody from looking inside for
3827 associativity, but won't generate any code. */
3828 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3829 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3830 || typecode == VECTOR_TYPE))
3832 error_at (location, "wrong type argument to unary plus");
3833 return error_mark_node;
3835 else if (!noconvert)
3836 arg = default_conversion (arg);
3837 arg = non_lvalue_loc (location, arg);
3838 break;
3840 case NEGATE_EXPR:
3841 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3842 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3843 || typecode == VECTOR_TYPE))
3845 error_at (location, "wrong type argument to unary minus");
3846 return error_mark_node;
3848 else if (!noconvert)
3849 arg = default_conversion (arg);
3850 break;
3852 case BIT_NOT_EXPR:
3853 /* ~ works on integer types and non float vectors. */
3854 if (typecode == INTEGER_TYPE
3855 || (typecode == VECTOR_TYPE
3856 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3858 if (!noconvert)
3859 arg = default_conversion (arg);
3861 else if (typecode == COMPLEX_TYPE)
3863 code = CONJ_EXPR;
3864 pedwarn (location, OPT_Wpedantic,
3865 "ISO C does not support %<~%> for complex conjugation");
3866 if (!noconvert)
3867 arg = default_conversion (arg);
3869 else
3871 error_at (location, "wrong type argument to bit-complement");
3872 return error_mark_node;
3874 break;
3876 case ABS_EXPR:
3877 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3879 error_at (location, "wrong type argument to abs");
3880 return error_mark_node;
3882 else if (!noconvert)
3883 arg = default_conversion (arg);
3884 break;
3886 case CONJ_EXPR:
3887 /* Conjugating a real value is a no-op, but allow it anyway. */
3888 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3889 || typecode == COMPLEX_TYPE))
3891 error_at (location, "wrong type argument to conjugation");
3892 return error_mark_node;
3894 else if (!noconvert)
3895 arg = default_conversion (arg);
3896 break;
3898 case TRUTH_NOT_EXPR:
3899 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3900 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3901 && typecode != COMPLEX_TYPE)
3903 error_at (location,
3904 "wrong type argument to unary exclamation mark");
3905 return error_mark_node;
3907 if (int_operands)
3909 arg = c_objc_common_truthvalue_conversion (location, xarg);
3910 arg = remove_c_maybe_const_expr (arg);
3912 else
3913 arg = c_objc_common_truthvalue_conversion (location, arg);
3914 ret = invert_truthvalue_loc (location, arg);
3915 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3916 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3917 location = EXPR_LOCATION (ret);
3918 goto return_build_unary_op;
3920 case REALPART_EXPR:
3921 case IMAGPART_EXPR:
3922 ret = build_real_imag_expr (location, code, arg);
3923 if (ret == error_mark_node)
3924 return error_mark_node;
3925 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3926 eptype = TREE_TYPE (eptype);
3927 goto return_build_unary_op;
3929 case PREINCREMENT_EXPR:
3930 case POSTINCREMENT_EXPR:
3931 case PREDECREMENT_EXPR:
3932 case POSTDECREMENT_EXPR:
3934 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3936 tree inner = build_unary_op (location, code,
3937 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3938 if (inner == error_mark_node)
3939 return error_mark_node;
3940 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3941 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3942 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3943 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3944 goto return_build_unary_op;
3947 /* Complain about anything that is not a true lvalue. In
3948 Objective-C, skip this check for property_refs. */
3949 if (!objc_is_property_ref (arg)
3950 && !lvalue_or_else (location,
3951 arg, ((code == PREINCREMENT_EXPR
3952 || code == POSTINCREMENT_EXPR)
3953 ? lv_increment
3954 : lv_decrement)))
3955 return error_mark_node;
3957 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3959 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3960 warning_at (location, OPT_Wc___compat,
3961 "increment of enumeration value is invalid in C++");
3962 else
3963 warning_at (location, OPT_Wc___compat,
3964 "decrement of enumeration value is invalid in C++");
3967 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3968 arg = c_fully_fold (arg, false, NULL);
3970 bool atomic_op;
3971 atomic_op = really_atomic_lvalue (arg);
3973 /* Increment or decrement the real part of the value,
3974 and don't change the imaginary part. */
3975 if (typecode == COMPLEX_TYPE)
3977 tree real, imag;
3979 pedwarn (location, OPT_Wpedantic,
3980 "ISO C does not support %<++%> and %<--%> on complex types");
3982 if (!atomic_op)
3984 arg = stabilize_reference (arg);
3985 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3986 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3987 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3988 if (real == error_mark_node || imag == error_mark_node)
3989 return error_mark_node;
3990 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3991 real, imag);
3992 goto return_build_unary_op;
3996 /* Report invalid types. */
3998 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3999 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4000 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4002 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4003 error_at (location, "wrong type argument to increment");
4004 else
4005 error_at (location, "wrong type argument to decrement");
4007 return error_mark_node;
4011 tree inc;
4013 argtype = TREE_TYPE (arg);
4015 /* Compute the increment. */
4017 if (typecode == POINTER_TYPE)
4019 /* If pointer target is an incomplete type,
4020 we just cannot know how to do the arithmetic. */
4021 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4023 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4024 error_at (location,
4025 "increment of pointer to an incomplete type %qT",
4026 TREE_TYPE (argtype));
4027 else
4028 error_at (location,
4029 "decrement of pointer to an incomplete type %qT",
4030 TREE_TYPE (argtype));
4032 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4033 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4035 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4036 pedwarn (location, OPT_Wpointer_arith,
4037 "wrong type argument to increment");
4038 else
4039 pedwarn (location, OPT_Wpointer_arith,
4040 "wrong type argument to decrement");
4043 inc = c_size_in_bytes (TREE_TYPE (argtype));
4044 inc = convert_to_ptrofftype_loc (location, inc);
4046 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4048 /* For signed fract types, we invert ++ to -- or
4049 -- to ++, and change inc from 1 to -1, because
4050 it is not possible to represent 1 in signed fract constants.
4051 For unsigned fract types, the result always overflows and
4052 we get an undefined (original) or the maximum value. */
4053 if (code == PREINCREMENT_EXPR)
4054 code = PREDECREMENT_EXPR;
4055 else if (code == PREDECREMENT_EXPR)
4056 code = PREINCREMENT_EXPR;
4057 else if (code == POSTINCREMENT_EXPR)
4058 code = POSTDECREMENT_EXPR;
4059 else /* code == POSTDECREMENT_EXPR */
4060 code = POSTINCREMENT_EXPR;
4062 inc = integer_minus_one_node;
4063 inc = convert (argtype, inc);
4065 else
4067 inc = VECTOR_TYPE_P (argtype)
4068 ? build_one_cst (argtype)
4069 : integer_one_node;
4070 inc = convert (argtype, inc);
4073 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4074 need to ask Objective-C to build the increment or decrement
4075 expression for it. */
4076 if (objc_is_property_ref (arg))
4077 return objc_build_incr_expr_for_property_ref (location, code,
4078 arg, inc);
4080 /* Report a read-only lvalue. */
4081 if (TYPE_READONLY (argtype))
4083 readonly_error (location, arg,
4084 ((code == PREINCREMENT_EXPR
4085 || code == POSTINCREMENT_EXPR)
4086 ? lv_increment : lv_decrement));
4087 return error_mark_node;
4089 else if (TREE_READONLY (arg))
4090 readonly_warning (arg,
4091 ((code == PREINCREMENT_EXPR
4092 || code == POSTINCREMENT_EXPR)
4093 ? lv_increment : lv_decrement));
4095 /* If the argument is atomic, use the special code sequences for
4096 atomic compound assignment. */
4097 if (atomic_op)
4099 arg = stabilize_reference (arg);
4100 ret = build_atomic_assign (location, arg,
4101 ((code == PREINCREMENT_EXPR
4102 || code == POSTINCREMENT_EXPR)
4103 ? PLUS_EXPR
4104 : MINUS_EXPR),
4105 (FRACT_MODE_P (TYPE_MODE (argtype))
4106 ? inc
4107 : integer_one_node),
4108 (code == POSTINCREMENT_EXPR
4109 || code == POSTDECREMENT_EXPR));
4110 goto return_build_unary_op;
4113 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4114 val = boolean_increment (code, arg);
4115 else
4116 val = build2 (code, TREE_TYPE (arg), arg, inc);
4117 TREE_SIDE_EFFECTS (val) = 1;
4118 if (TREE_CODE (val) != code)
4119 TREE_NO_WARNING (val) = 1;
4120 ret = val;
4121 goto return_build_unary_op;
4124 case ADDR_EXPR:
4125 /* Note that this operation never does default_conversion. */
4127 /* The operand of unary '&' must be an lvalue (which excludes
4128 expressions of type void), or, in C99, the result of a [] or
4129 unary '*' operator. */
4130 if (VOID_TYPE_P (TREE_TYPE (arg))
4131 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4132 && (TREE_CODE (arg) != INDIRECT_REF
4133 || !flag_isoc99))
4134 pedwarn (location, 0, "taking address of expression of type %<void%>");
4136 /* Let &* cancel out to simplify resulting code. */
4137 if (TREE_CODE (arg) == INDIRECT_REF)
4139 /* Don't let this be an lvalue. */
4140 if (lvalue_p (TREE_OPERAND (arg, 0)))
4141 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4142 ret = TREE_OPERAND (arg, 0);
4143 goto return_build_unary_op;
4146 /* For &x[y], return x+y */
4147 if (TREE_CODE (arg) == ARRAY_REF)
4149 tree op0 = TREE_OPERAND (arg, 0);
4150 if (!c_mark_addressable (op0))
4151 return error_mark_node;
4154 /* Anything not already handled and not a true memory reference
4155 or a non-lvalue array is an error. */
4156 else if (typecode != FUNCTION_TYPE && !flag
4157 && !lvalue_or_else (location, arg, lv_addressof))
4158 return error_mark_node;
4160 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4161 folding later. */
4162 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4164 tree inner = build_unary_op (location, code,
4165 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4166 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4167 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4168 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4169 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4170 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4171 goto return_build_unary_op;
4174 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4175 argtype = TREE_TYPE (arg);
4177 /* If the lvalue is const or volatile, merge that into the type
4178 to which the address will point. This is only needed
4179 for function types. */
4180 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4181 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4182 && TREE_CODE (argtype) == FUNCTION_TYPE)
4184 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4185 int quals = orig_quals;
4187 if (TREE_READONLY (arg))
4188 quals |= TYPE_QUAL_CONST;
4189 if (TREE_THIS_VOLATILE (arg))
4190 quals |= TYPE_QUAL_VOLATILE;
4192 argtype = c_build_qualified_type (argtype, quals);
4195 if (!c_mark_addressable (arg))
4196 return error_mark_node;
4198 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4199 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4201 argtype = build_pointer_type (argtype);
4203 /* ??? Cope with user tricks that amount to offsetof. Delete this
4204 when we have proper support for integer constant expressions. */
4205 val = get_base_address (arg);
4206 if (val && TREE_CODE (val) == INDIRECT_REF
4207 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4209 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4210 goto return_build_unary_op;
4213 val = build1 (ADDR_EXPR, argtype, arg);
4215 ret = val;
4216 goto return_build_unary_op;
4218 default:
4219 gcc_unreachable ();
4222 if (argtype == 0)
4223 argtype = TREE_TYPE (arg);
4224 if (TREE_CODE (arg) == INTEGER_CST)
4225 ret = (require_constant_value
4226 ? fold_build1_initializer_loc (location, code, argtype, arg)
4227 : fold_build1_loc (location, code, argtype, arg));
4228 else
4229 ret = build1 (code, argtype, arg);
4230 return_build_unary_op:
4231 gcc_assert (ret != error_mark_node);
4232 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4233 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4234 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4235 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4236 ret = note_integer_operands (ret);
4237 if (eptype)
4238 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4239 protected_set_expr_location (ret, location);
4240 return ret;
4243 /* Return nonzero if REF is an lvalue valid for this language.
4244 Lvalues can be assigned, unless their type has TYPE_READONLY.
4245 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4247 bool
4248 lvalue_p (const_tree ref)
4250 const enum tree_code code = TREE_CODE (ref);
4252 switch (code)
4254 case REALPART_EXPR:
4255 case IMAGPART_EXPR:
4256 case COMPONENT_REF:
4257 return lvalue_p (TREE_OPERAND (ref, 0));
4259 case C_MAYBE_CONST_EXPR:
4260 return lvalue_p (TREE_OPERAND (ref, 1));
4262 case COMPOUND_LITERAL_EXPR:
4263 case STRING_CST:
4264 return 1;
4266 case INDIRECT_REF:
4267 case ARRAY_REF:
4268 case ARRAY_NOTATION_REF:
4269 case VAR_DECL:
4270 case PARM_DECL:
4271 case RESULT_DECL:
4272 case ERROR_MARK:
4273 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4274 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4276 case BIND_EXPR:
4277 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4279 default:
4280 return 0;
4284 /* Give a warning for storing in something that is read-only in GCC
4285 terms but not const in ISO C terms. */
4287 static void
4288 readonly_warning (tree arg, enum lvalue_use use)
4290 switch (use)
4292 case lv_assign:
4293 warning (0, "assignment of read-only location %qE", arg);
4294 break;
4295 case lv_increment:
4296 warning (0, "increment of read-only location %qE", arg);
4297 break;
4298 case lv_decrement:
4299 warning (0, "decrement of read-only location %qE", arg);
4300 break;
4301 default:
4302 gcc_unreachable ();
4304 return;
4308 /* Return nonzero if REF is an lvalue valid for this language;
4309 otherwise, print an error message and return zero. USE says
4310 how the lvalue is being used and so selects the error message.
4311 LOCATION is the location at which any error should be reported. */
4313 static int
4314 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4316 int win = lvalue_p (ref);
4318 if (!win)
4319 lvalue_error (loc, use);
4321 return win;
4324 /* Mark EXP saying that we need to be able to take the
4325 address of it; it should not be allocated in a register.
4326 Returns true if successful. */
4328 bool
4329 c_mark_addressable (tree exp)
4331 tree x = exp;
4333 while (1)
4334 switch (TREE_CODE (x))
4336 case COMPONENT_REF:
4337 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4339 error
4340 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4341 return false;
4344 /* ... fall through ... */
4346 case ADDR_EXPR:
4347 case ARRAY_REF:
4348 case REALPART_EXPR:
4349 case IMAGPART_EXPR:
4350 x = TREE_OPERAND (x, 0);
4351 break;
4353 case COMPOUND_LITERAL_EXPR:
4354 case CONSTRUCTOR:
4355 TREE_ADDRESSABLE (x) = 1;
4356 return true;
4358 case VAR_DECL:
4359 case CONST_DECL:
4360 case PARM_DECL:
4361 case RESULT_DECL:
4362 if (C_DECL_REGISTER (x)
4363 && DECL_NONLOCAL (x))
4365 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4367 error
4368 ("global register variable %qD used in nested function", x);
4369 return false;
4371 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4373 else if (C_DECL_REGISTER (x))
4375 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4376 error ("address of global register variable %qD requested", x);
4377 else
4378 error ("address of register variable %qD requested", x);
4379 return false;
4382 /* drops in */
4383 case FUNCTION_DECL:
4384 TREE_ADDRESSABLE (x) = 1;
4385 /* drops out */
4386 default:
4387 return true;
4391 /* Convert EXPR to TYPE, warning about conversion problems with
4392 constants. SEMANTIC_TYPE is the type this conversion would use
4393 without excess precision. If SEMANTIC_TYPE is NULL, this function
4394 is equivalent to convert_and_check. This function is a wrapper that
4395 handles conversions that may be different than
4396 the usual ones because of excess precision. */
4398 static tree
4399 ep_convert_and_check (location_t loc, tree type, tree expr,
4400 tree semantic_type)
4402 if (TREE_TYPE (expr) == type)
4403 return expr;
4405 if (!semantic_type)
4406 return convert_and_check (loc, type, expr);
4408 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4409 && TREE_TYPE (expr) != semantic_type)
4411 /* For integers, we need to check the real conversion, not
4412 the conversion to the excess precision type. */
4413 expr = convert_and_check (loc, semantic_type, expr);
4415 /* Result type is the excess precision type, which should be
4416 large enough, so do not check. */
4417 return convert (type, expr);
4420 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4421 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4422 if folded to an integer constant then the unselected half may
4423 contain arbitrary operations not normally permitted in constant
4424 expressions. Set the location of the expression to LOC. */
4426 tree
4427 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4428 tree op1, tree op1_original_type, tree op2,
4429 tree op2_original_type)
4431 tree type1;
4432 tree type2;
4433 enum tree_code code1;
4434 enum tree_code code2;
4435 tree result_type = NULL;
4436 tree semantic_result_type = NULL;
4437 tree orig_op1 = op1, orig_op2 = op2;
4438 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4439 bool ifexp_int_operands;
4440 tree ret;
4442 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4443 if (op1_int_operands)
4444 op1 = remove_c_maybe_const_expr (op1);
4445 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4446 if (op2_int_operands)
4447 op2 = remove_c_maybe_const_expr (op2);
4448 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4449 if (ifexp_int_operands)
4450 ifexp = remove_c_maybe_const_expr (ifexp);
4452 /* Promote both alternatives. */
4454 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4455 op1 = default_conversion (op1);
4456 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4457 op2 = default_conversion (op2);
4459 if (TREE_CODE (ifexp) == ERROR_MARK
4460 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4461 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4462 return error_mark_node;
4464 type1 = TREE_TYPE (op1);
4465 code1 = TREE_CODE (type1);
4466 type2 = TREE_TYPE (op2);
4467 code2 = TREE_CODE (type2);
4469 /* C90 does not permit non-lvalue arrays in conditional expressions.
4470 In C99 they will be pointers by now. */
4471 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4473 error_at (colon_loc, "non-lvalue array in conditional expression");
4474 return error_mark_node;
4477 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4478 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4479 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4480 || code1 == COMPLEX_TYPE)
4481 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4482 || code2 == COMPLEX_TYPE))
4484 semantic_result_type = c_common_type (type1, type2);
4485 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4487 op1 = TREE_OPERAND (op1, 0);
4488 type1 = TREE_TYPE (op1);
4489 gcc_assert (TREE_CODE (type1) == code1);
4491 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4493 op2 = TREE_OPERAND (op2, 0);
4494 type2 = TREE_TYPE (op2);
4495 gcc_assert (TREE_CODE (type2) == code2);
4499 if (warn_cxx_compat)
4501 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4502 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4504 if (TREE_CODE (t1) == ENUMERAL_TYPE
4505 && TREE_CODE (t2) == ENUMERAL_TYPE
4506 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4507 warning_at (colon_loc, OPT_Wc___compat,
4508 ("different enum types in conditional is "
4509 "invalid in C++: %qT vs %qT"),
4510 t1, t2);
4513 /* Quickly detect the usual case where op1 and op2 have the same type
4514 after promotion. */
4515 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4517 if (type1 == type2)
4518 result_type = type1;
4519 else
4520 result_type = TYPE_MAIN_VARIANT (type1);
4522 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4523 || code1 == COMPLEX_TYPE)
4524 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4525 || code2 == COMPLEX_TYPE))
4527 result_type = c_common_type (type1, type2);
4528 do_warn_double_promotion (result_type, type1, type2,
4529 "implicit conversion from %qT to %qT to "
4530 "match other result of conditional",
4531 colon_loc);
4533 /* If -Wsign-compare, warn here if type1 and type2 have
4534 different signedness. We'll promote the signed to unsigned
4535 and later code won't know it used to be different.
4536 Do this check on the original types, so that explicit casts
4537 will be considered, but default promotions won't. */
4538 if (c_inhibit_evaluation_warnings == 0)
4540 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4541 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4543 if (unsigned_op1 ^ unsigned_op2)
4545 bool ovf;
4547 /* Do not warn if the result type is signed, since the
4548 signed type will only be chosen if it can represent
4549 all the values of the unsigned type. */
4550 if (!TYPE_UNSIGNED (result_type))
4551 /* OK */;
4552 else
4554 bool op1_maybe_const = true;
4555 bool op2_maybe_const = true;
4557 /* Do not warn if the signed quantity is an
4558 unsuffixed integer literal (or some static
4559 constant expression involving such literals) and
4560 it is non-negative. This warning requires the
4561 operands to be folded for best results, so do
4562 that folding in this case even without
4563 warn_sign_compare to avoid warning options
4564 possibly affecting code generation. */
4565 c_inhibit_evaluation_warnings
4566 += (ifexp == truthvalue_false_node);
4567 op1 = c_fully_fold (op1, require_constant_value,
4568 &op1_maybe_const);
4569 c_inhibit_evaluation_warnings
4570 -= (ifexp == truthvalue_false_node);
4572 c_inhibit_evaluation_warnings
4573 += (ifexp == truthvalue_true_node);
4574 op2 = c_fully_fold (op2, require_constant_value,
4575 &op2_maybe_const);
4576 c_inhibit_evaluation_warnings
4577 -= (ifexp == truthvalue_true_node);
4579 if (warn_sign_compare)
4581 if ((unsigned_op2
4582 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4583 || (unsigned_op1
4584 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4585 /* OK */;
4586 else
4587 warning_at (colon_loc, OPT_Wsign_compare,
4588 ("signed and unsigned type in "
4589 "conditional expression"));
4591 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4592 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4593 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4594 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4599 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4601 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4602 pedwarn (colon_loc, OPT_Wpedantic,
4603 "ISO C forbids conditional expr with only one void side");
4604 result_type = void_type_node;
4606 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4608 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4609 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4610 addr_space_t as_common;
4612 if (comp_target_types (colon_loc, type1, type2))
4613 result_type = common_pointer_type (type1, type2);
4614 else if (null_pointer_constant_p (orig_op1))
4615 result_type = type2;
4616 else if (null_pointer_constant_p (orig_op2))
4617 result_type = type1;
4618 else if (!addr_space_superset (as1, as2, &as_common))
4620 error_at (colon_loc, "pointers to disjoint address spaces "
4621 "used in conditional expression");
4622 return error_mark_node;
4624 else if (VOID_TYPE_P (TREE_TYPE (type1))
4625 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4627 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4628 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4629 & ~TYPE_QUALS (TREE_TYPE (type1))))
4630 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4631 "pointer to array loses qualifier "
4632 "in conditional expression");
4634 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4635 pedwarn (colon_loc, OPT_Wpedantic,
4636 "ISO C forbids conditional expr between "
4637 "%<void *%> and function pointer");
4638 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4639 TREE_TYPE (type2)));
4641 else if (VOID_TYPE_P (TREE_TYPE (type2))
4642 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4644 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4645 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4646 & ~TYPE_QUALS (TREE_TYPE (type2))))
4647 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4648 "pointer to array loses qualifier "
4649 "in conditional expression");
4651 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4652 pedwarn (colon_loc, OPT_Wpedantic,
4653 "ISO C forbids conditional expr between "
4654 "%<void *%> and function pointer");
4655 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4656 TREE_TYPE (type1)));
4658 /* Objective-C pointer comparisons are a bit more lenient. */
4659 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4660 result_type = objc_common_type (type1, type2);
4661 else
4663 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4665 pedwarn (colon_loc, 0,
4666 "pointer type mismatch in conditional expression");
4667 result_type = build_pointer_type
4668 (build_qualified_type (void_type_node, qual));
4671 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4673 if (!null_pointer_constant_p (orig_op2))
4674 pedwarn (colon_loc, 0,
4675 "pointer/integer type mismatch in conditional expression");
4676 else
4678 op2 = null_pointer_node;
4680 result_type = type1;
4682 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4684 if (!null_pointer_constant_p (orig_op1))
4685 pedwarn (colon_loc, 0,
4686 "pointer/integer type mismatch in conditional expression");
4687 else
4689 op1 = null_pointer_node;
4691 result_type = type2;
4694 if (!result_type)
4696 if (flag_cond_mismatch)
4697 result_type = void_type_node;
4698 else
4700 error_at (colon_loc, "type mismatch in conditional expression");
4701 return error_mark_node;
4705 /* Merge const and volatile flags of the incoming types. */
4706 result_type
4707 = build_type_variant (result_type,
4708 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4709 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4711 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4712 semantic_result_type);
4713 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4714 semantic_result_type);
4716 if (ifexp_bcp && ifexp == truthvalue_true_node)
4718 op2_int_operands = true;
4719 op1 = c_fully_fold (op1, require_constant_value, NULL);
4721 if (ifexp_bcp && ifexp == truthvalue_false_node)
4723 op1_int_operands = true;
4724 op2 = c_fully_fold (op2, require_constant_value, NULL);
4726 int_const = int_operands = (ifexp_int_operands
4727 && op1_int_operands
4728 && op2_int_operands);
4729 if (int_operands)
4731 int_const = ((ifexp == truthvalue_true_node
4732 && TREE_CODE (orig_op1) == INTEGER_CST
4733 && !TREE_OVERFLOW (orig_op1))
4734 || (ifexp == truthvalue_false_node
4735 && TREE_CODE (orig_op2) == INTEGER_CST
4736 && !TREE_OVERFLOW (orig_op2)));
4738 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4739 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4740 else
4742 if (int_operands)
4744 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4745 nested inside of the expression. */
4746 op1 = c_fully_fold (op1, false, NULL);
4747 op2 = c_fully_fold (op2, false, NULL);
4749 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4750 if (int_operands)
4751 ret = note_integer_operands (ret);
4753 if (semantic_result_type)
4754 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4756 protected_set_expr_location (ret, colon_loc);
4757 return ret;
4760 /* Return a compound expression that performs two expressions and
4761 returns the value of the second of them.
4763 LOC is the location of the COMPOUND_EXPR. */
4765 tree
4766 build_compound_expr (location_t loc, tree expr1, tree expr2)
4768 bool expr1_int_operands, expr2_int_operands;
4769 tree eptype = NULL_TREE;
4770 tree ret;
4772 if (flag_cilkplus
4773 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4774 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4776 error_at (loc,
4777 "spawned function call cannot be part of a comma expression");
4778 return error_mark_node;
4780 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4781 if (expr1_int_operands)
4782 expr1 = remove_c_maybe_const_expr (expr1);
4783 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4784 if (expr2_int_operands)
4785 expr2 = remove_c_maybe_const_expr (expr2);
4787 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4788 expr1 = TREE_OPERAND (expr1, 0);
4789 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4791 eptype = TREE_TYPE (expr2);
4792 expr2 = TREE_OPERAND (expr2, 0);
4795 if (!TREE_SIDE_EFFECTS (expr1))
4797 /* The left-hand operand of a comma expression is like an expression
4798 statement: with -Wunused, we should warn if it doesn't have
4799 any side-effects, unless it was explicitly cast to (void). */
4800 if (warn_unused_value)
4802 if (VOID_TYPE_P (TREE_TYPE (expr1))
4803 && CONVERT_EXPR_P (expr1))
4804 ; /* (void) a, b */
4805 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4806 && TREE_CODE (expr1) == COMPOUND_EXPR
4807 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4808 ; /* (void) a, (void) b, c */
4809 else
4810 warning_at (loc, OPT_Wunused_value,
4811 "left-hand operand of comma expression has no effect");
4814 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4815 && warn_unused_value)
4817 tree r = expr1;
4818 location_t cloc = loc;
4819 while (TREE_CODE (r) == COMPOUND_EXPR)
4821 if (EXPR_HAS_LOCATION (r))
4822 cloc = EXPR_LOCATION (r);
4823 r = TREE_OPERAND (r, 1);
4825 if (!TREE_SIDE_EFFECTS (r)
4826 && !VOID_TYPE_P (TREE_TYPE (r))
4827 && !CONVERT_EXPR_P (r))
4828 warning_at (cloc, OPT_Wunused_value,
4829 "right-hand operand of comma expression has no effect");
4832 /* With -Wunused, we should also warn if the left-hand operand does have
4833 side-effects, but computes a value which is not used. For example, in
4834 `foo() + bar(), baz()' the result of the `+' operator is not used,
4835 so we should issue a warning. */
4836 else if (warn_unused_value)
4837 warn_if_unused_value (expr1, loc);
4839 if (expr2 == error_mark_node)
4840 return error_mark_node;
4842 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4844 if (flag_isoc99
4845 && expr1_int_operands
4846 && expr2_int_operands)
4847 ret = note_integer_operands (ret);
4849 if (eptype)
4850 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4852 protected_set_expr_location (ret, loc);
4853 return ret;
4856 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4857 which we are casting. OTYPE is the type of the expression being
4858 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4859 of the cast. -Wcast-qual appeared on the command line. Named
4860 address space qualifiers are not handled here, because they result
4861 in different warnings. */
4863 static void
4864 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4866 tree in_type = type;
4867 tree in_otype = otype;
4868 int added = 0;
4869 int discarded = 0;
4870 bool is_const;
4872 /* Check that the qualifiers on IN_TYPE are a superset of the
4873 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4874 nodes is uninteresting and we stop as soon as we hit a
4875 non-POINTER_TYPE node on either type. */
4878 in_otype = TREE_TYPE (in_otype);
4879 in_type = TREE_TYPE (in_type);
4881 /* GNU C allows cv-qualified function types. 'const' means the
4882 function is very pure, 'volatile' means it can't return. We
4883 need to warn when such qualifiers are added, not when they're
4884 taken away. */
4885 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4886 && TREE_CODE (in_type) == FUNCTION_TYPE)
4887 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4888 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4889 else
4890 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4891 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4893 while (TREE_CODE (in_type) == POINTER_TYPE
4894 && TREE_CODE (in_otype) == POINTER_TYPE);
4896 if (added)
4897 warning_at (loc, OPT_Wcast_qual,
4898 "cast adds %q#v qualifier to function type", added);
4900 if (discarded)
4901 /* There are qualifiers present in IN_OTYPE that are not present
4902 in IN_TYPE. */
4903 warning_at (loc, OPT_Wcast_qual,
4904 "cast discards %qv qualifier from pointer target type",
4905 discarded);
4907 if (added || discarded)
4908 return;
4910 /* A cast from **T to const **T is unsafe, because it can cause a
4911 const value to be changed with no additional warning. We only
4912 issue this warning if T is the same on both sides, and we only
4913 issue the warning if there are the same number of pointers on
4914 both sides, as otherwise the cast is clearly unsafe anyhow. A
4915 cast is unsafe when a qualifier is added at one level and const
4916 is not present at all outer levels.
4918 To issue this warning, we check at each level whether the cast
4919 adds new qualifiers not already seen. We don't need to special
4920 case function types, as they won't have the same
4921 TYPE_MAIN_VARIANT. */
4923 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4924 return;
4925 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4926 return;
4928 in_type = type;
4929 in_otype = otype;
4930 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4933 in_type = TREE_TYPE (in_type);
4934 in_otype = TREE_TYPE (in_otype);
4935 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4936 && !is_const)
4938 warning_at (loc, OPT_Wcast_qual,
4939 "to be safe all intermediate pointers in cast from "
4940 "%qT to %qT must be %<const%> qualified",
4941 otype, type);
4942 break;
4944 if (is_const)
4945 is_const = TYPE_READONLY (in_type);
4947 while (TREE_CODE (in_type) == POINTER_TYPE);
4950 /* Build an expression representing a cast to type TYPE of expression EXPR.
4951 LOC is the location of the cast-- typically the open paren of the cast. */
4953 tree
4954 build_c_cast (location_t loc, tree type, tree expr)
4956 tree value;
4958 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4959 expr = TREE_OPERAND (expr, 0);
4961 value = expr;
4963 if (type == error_mark_node || expr == error_mark_node)
4964 return error_mark_node;
4966 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4967 only in <protocol> qualifications. But when constructing cast expressions,
4968 the protocols do matter and must be kept around. */
4969 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4970 return build1 (NOP_EXPR, type, expr);
4972 type = TYPE_MAIN_VARIANT (type);
4974 if (TREE_CODE (type) == ARRAY_TYPE)
4976 error_at (loc, "cast specifies array type");
4977 return error_mark_node;
4980 if (TREE_CODE (type) == FUNCTION_TYPE)
4982 error_at (loc, "cast specifies function type");
4983 return error_mark_node;
4986 if (!VOID_TYPE_P (type))
4988 value = require_complete_type (value);
4989 if (value == error_mark_node)
4990 return error_mark_node;
4993 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4995 if (TREE_CODE (type) == RECORD_TYPE
4996 || TREE_CODE (type) == UNION_TYPE)
4997 pedwarn (loc, OPT_Wpedantic,
4998 "ISO C forbids casting nonscalar to the same type");
5000 /* Convert to remove any qualifiers from VALUE's type. */
5001 value = convert (type, value);
5003 else if (TREE_CODE (type) == UNION_TYPE)
5005 tree field;
5007 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5008 if (TREE_TYPE (field) != error_mark_node
5009 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5010 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5011 break;
5013 if (field)
5015 tree t;
5016 bool maybe_const = true;
5018 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5019 t = c_fully_fold (value, false, &maybe_const);
5020 t = build_constructor_single (type, field, t);
5021 if (!maybe_const)
5022 t = c_wrap_maybe_const (t, true);
5023 t = digest_init (loc, type, t,
5024 NULL_TREE, false, true, 0);
5025 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5026 return t;
5028 error_at (loc, "cast to union type from type not present in union");
5029 return error_mark_node;
5031 else
5033 tree otype, ovalue;
5035 if (type == void_type_node)
5037 tree t = build1 (CONVERT_EXPR, type, value);
5038 SET_EXPR_LOCATION (t, loc);
5039 return t;
5042 otype = TREE_TYPE (value);
5044 /* Optionally warn about potentially worrisome casts. */
5045 if (warn_cast_qual
5046 && TREE_CODE (type) == POINTER_TYPE
5047 && TREE_CODE (otype) == POINTER_TYPE)
5048 handle_warn_cast_qual (loc, type, otype);
5050 /* Warn about conversions between pointers to disjoint
5051 address spaces. */
5052 if (TREE_CODE (type) == POINTER_TYPE
5053 && TREE_CODE (otype) == POINTER_TYPE
5054 && !null_pointer_constant_p (value))
5056 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5057 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5058 addr_space_t as_common;
5060 if (!addr_space_superset (as_to, as_from, &as_common))
5062 if (ADDR_SPACE_GENERIC_P (as_from))
5063 warning_at (loc, 0, "cast to %s address space pointer "
5064 "from disjoint generic address space pointer",
5065 c_addr_space_name (as_to));
5067 else if (ADDR_SPACE_GENERIC_P (as_to))
5068 warning_at (loc, 0, "cast to generic address space pointer "
5069 "from disjoint %s address space pointer",
5070 c_addr_space_name (as_from));
5072 else
5073 warning_at (loc, 0, "cast to %s address space pointer "
5074 "from disjoint %s address space pointer",
5075 c_addr_space_name (as_to),
5076 c_addr_space_name (as_from));
5080 /* Warn about possible alignment problems. */
5081 if (STRICT_ALIGNMENT
5082 && TREE_CODE (type) == POINTER_TYPE
5083 && TREE_CODE (otype) == POINTER_TYPE
5084 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5085 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5086 /* Don't warn about opaque types, where the actual alignment
5087 restriction is unknown. */
5088 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5089 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5090 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5091 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5092 warning_at (loc, OPT_Wcast_align,
5093 "cast increases required alignment of target type");
5095 if (TREE_CODE (type) == INTEGER_TYPE
5096 && TREE_CODE (otype) == POINTER_TYPE
5097 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5098 /* Unlike conversion of integers to pointers, where the
5099 warning is disabled for converting constants because
5100 of cases such as SIG_*, warn about converting constant
5101 pointers to integers. In some cases it may cause unwanted
5102 sign extension, and a warning is appropriate. */
5103 warning_at (loc, OPT_Wpointer_to_int_cast,
5104 "cast from pointer to integer of different size");
5106 if (TREE_CODE (value) == CALL_EXPR
5107 && TREE_CODE (type) != TREE_CODE (otype))
5108 warning_at (loc, OPT_Wbad_function_cast,
5109 "cast from function call of type %qT "
5110 "to non-matching type %qT", otype, type);
5112 if (TREE_CODE (type) == POINTER_TYPE
5113 && TREE_CODE (otype) == INTEGER_TYPE
5114 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5115 /* Don't warn about converting any constant. */
5116 && !TREE_CONSTANT (value))
5117 warning_at (loc,
5118 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5119 "of different size");
5121 if (warn_strict_aliasing <= 2)
5122 strict_aliasing_warning (otype, type, expr);
5124 /* If pedantic, warn for conversions between function and object
5125 pointer types, except for converting a null pointer constant
5126 to function pointer type. */
5127 if (pedantic
5128 && TREE_CODE (type) == POINTER_TYPE
5129 && TREE_CODE (otype) == POINTER_TYPE
5130 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5131 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5132 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5133 "conversion of function pointer to object pointer type");
5135 if (pedantic
5136 && TREE_CODE (type) == POINTER_TYPE
5137 && TREE_CODE (otype) == POINTER_TYPE
5138 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5139 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5140 && !null_pointer_constant_p (value))
5141 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5142 "conversion of object pointer to function pointer type");
5144 ovalue = value;
5145 value = convert (type, value);
5147 /* Ignore any integer overflow caused by the cast. */
5148 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5150 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5152 if (!TREE_OVERFLOW (value))
5154 /* Avoid clobbering a shared constant. */
5155 value = copy_node (value);
5156 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5159 else if (TREE_OVERFLOW (value))
5160 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5161 value = wide_int_to_tree (TREE_TYPE (value), value);
5165 /* Don't let a cast be an lvalue. */
5166 if (value == expr)
5167 value = non_lvalue_loc (loc, value);
5169 /* Don't allow the results of casting to floating-point or complex
5170 types be confused with actual constants, or casts involving
5171 integer and pointer types other than direct integer-to-integer
5172 and integer-to-pointer be confused with integer constant
5173 expressions and null pointer constants. */
5174 if (TREE_CODE (value) == REAL_CST
5175 || TREE_CODE (value) == COMPLEX_CST
5176 || (TREE_CODE (value) == INTEGER_CST
5177 && !((TREE_CODE (expr) == INTEGER_CST
5178 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5179 || TREE_CODE (expr) == REAL_CST
5180 || TREE_CODE (expr) == COMPLEX_CST)))
5181 value = build1 (NOP_EXPR, type, value);
5183 if (CAN_HAVE_LOCATION_P (value))
5184 SET_EXPR_LOCATION (value, loc);
5185 return value;
5188 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5189 location of the open paren of the cast, or the position of the cast
5190 expr. */
5191 tree
5192 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5194 tree type;
5195 tree type_expr = NULL_TREE;
5196 bool type_expr_const = true;
5197 tree ret;
5198 int saved_wsp = warn_strict_prototypes;
5200 /* This avoids warnings about unprototyped casts on
5201 integers. E.g. "#define SIG_DFL (void(*)())0". */
5202 if (TREE_CODE (expr) == INTEGER_CST)
5203 warn_strict_prototypes = 0;
5204 type = groktypename (type_name, &type_expr, &type_expr_const);
5205 warn_strict_prototypes = saved_wsp;
5207 ret = build_c_cast (loc, type, expr);
5208 if (type_expr)
5210 bool inner_expr_const = true;
5211 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5212 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5213 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5214 && inner_expr_const);
5215 SET_EXPR_LOCATION (ret, loc);
5218 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5219 SET_EXPR_LOCATION (ret, loc);
5221 /* C++ does not permits types to be defined in a cast, but it
5222 allows references to incomplete types. */
5223 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5224 warning_at (loc, OPT_Wc___compat,
5225 "defining a type in a cast is invalid in C++");
5227 return ret;
5230 /* Build an assignment expression of lvalue LHS from value RHS.
5231 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5232 may differ from TREE_TYPE (LHS) for an enum bitfield.
5233 MODIFYCODE is the code for a binary operator that we use
5234 to combine the old value of LHS with RHS to get the new value.
5235 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5236 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5237 which may differ from TREE_TYPE (RHS) for an enum value.
5239 LOCATION is the location of the MODIFYCODE operator.
5240 RHS_LOC is the location of the RHS. */
5242 tree
5243 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5244 enum tree_code modifycode,
5245 location_t rhs_loc, tree rhs, tree rhs_origtype)
5247 tree result;
5248 tree newrhs;
5249 tree rhseval = NULL_TREE;
5250 tree rhs_semantic_type = NULL_TREE;
5251 tree lhstype = TREE_TYPE (lhs);
5252 tree olhstype = lhstype;
5253 bool npc;
5254 bool is_atomic_op;
5256 /* Types that aren't fully specified cannot be used in assignments. */
5257 lhs = require_complete_type (lhs);
5259 /* Avoid duplicate error messages from operands that had errors. */
5260 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5261 return error_mark_node;
5263 /* Ensure an error for assigning a non-lvalue array to an array in
5264 C90. */
5265 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5267 error_at (location, "assignment to expression with array type");
5268 return error_mark_node;
5271 /* For ObjC properties, defer this check. */
5272 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5273 return error_mark_node;
5275 is_atomic_op = really_atomic_lvalue (lhs);
5277 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5279 rhs_semantic_type = TREE_TYPE (rhs);
5280 rhs = TREE_OPERAND (rhs, 0);
5283 newrhs = rhs;
5285 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5287 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5288 lhs_origtype, modifycode, rhs_loc, rhs,
5289 rhs_origtype);
5290 if (inner == error_mark_node)
5291 return error_mark_node;
5292 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5293 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5294 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5295 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5296 protected_set_expr_location (result, location);
5297 return result;
5300 /* If a binary op has been requested, combine the old LHS value with the RHS
5301 producing the value we should actually store into the LHS. */
5303 if (modifycode != NOP_EXPR)
5305 lhs = c_fully_fold (lhs, false, NULL);
5306 lhs = stabilize_reference (lhs);
5308 /* Construct the RHS for any non-atomic compound assignemnt. */
5309 if (!is_atomic_op)
5311 /* If in LHS op= RHS the RHS has side-effects, ensure they
5312 are preevaluated before the rest of the assignment expression's
5313 side-effects, because RHS could contain e.g. function calls
5314 that modify LHS. */
5315 if (TREE_SIDE_EFFECTS (rhs))
5317 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5318 rhseval = newrhs;
5320 newrhs = build_binary_op (location,
5321 modifycode, lhs, newrhs, 1);
5323 /* The original type of the right hand side is no longer
5324 meaningful. */
5325 rhs_origtype = NULL_TREE;
5329 if (c_dialect_objc ())
5331 /* Check if we are modifying an Objective-C property reference;
5332 if so, we need to generate setter calls. */
5333 result = objc_maybe_build_modify_expr (lhs, newrhs);
5334 if (result)
5335 goto return_result;
5337 /* Else, do the check that we postponed for Objective-C. */
5338 if (!lvalue_or_else (location, lhs, lv_assign))
5339 return error_mark_node;
5342 /* Give an error for storing in something that is 'const'. */
5344 if (TYPE_READONLY (lhstype)
5345 || ((TREE_CODE (lhstype) == RECORD_TYPE
5346 || TREE_CODE (lhstype) == UNION_TYPE)
5347 && C_TYPE_FIELDS_READONLY (lhstype)))
5349 readonly_error (location, lhs, lv_assign);
5350 return error_mark_node;
5352 else if (TREE_READONLY (lhs))
5353 readonly_warning (lhs, lv_assign);
5355 /* If storing into a structure or union member,
5356 it has probably been given type `int'.
5357 Compute the type that would go with
5358 the actual amount of storage the member occupies. */
5360 if (TREE_CODE (lhs) == COMPONENT_REF
5361 && (TREE_CODE (lhstype) == INTEGER_TYPE
5362 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5363 || TREE_CODE (lhstype) == REAL_TYPE
5364 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5365 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5367 /* If storing in a field that is in actuality a short or narrower than one,
5368 we must store in the field in its actual type. */
5370 if (lhstype != TREE_TYPE (lhs))
5372 lhs = copy_node (lhs);
5373 TREE_TYPE (lhs) = lhstype;
5376 /* Issue -Wc++-compat warnings about an assignment to an enum type
5377 when LHS does not have its original type. This happens for,
5378 e.g., an enum bitfield in a struct. */
5379 if (warn_cxx_compat
5380 && lhs_origtype != NULL_TREE
5381 && lhs_origtype != lhstype
5382 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5384 tree checktype = (rhs_origtype != NULL_TREE
5385 ? rhs_origtype
5386 : TREE_TYPE (rhs));
5387 if (checktype != error_mark_node
5388 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5389 || (is_atomic_op && modifycode != NOP_EXPR)))
5390 warning_at (location, OPT_Wc___compat,
5391 "enum conversion in assignment is invalid in C++");
5394 /* If the lhs is atomic, remove that qualifier. */
5395 if (is_atomic_op)
5397 lhstype = build_qualified_type (lhstype,
5398 (TYPE_QUALS (lhstype)
5399 & ~TYPE_QUAL_ATOMIC));
5400 olhstype = build_qualified_type (olhstype,
5401 (TYPE_QUALS (lhstype)
5402 & ~TYPE_QUAL_ATOMIC));
5405 /* Convert new value to destination type. Fold it first, then
5406 restore any excess precision information, for the sake of
5407 conversion warnings. */
5409 if (!(is_atomic_op && modifycode != NOP_EXPR))
5411 npc = null_pointer_constant_p (newrhs);
5412 newrhs = c_fully_fold (newrhs, false, NULL);
5413 if (rhs_semantic_type)
5414 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5415 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5416 rhs_origtype, ic_assign, npc,
5417 NULL_TREE, NULL_TREE, 0);
5418 if (TREE_CODE (newrhs) == ERROR_MARK)
5419 return error_mark_node;
5422 /* Emit ObjC write barrier, if necessary. */
5423 if (c_dialect_objc () && flag_objc_gc)
5425 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5426 if (result)
5428 protected_set_expr_location (result, location);
5429 goto return_result;
5433 /* Scan operands. */
5435 if (is_atomic_op)
5436 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5437 else
5439 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5440 TREE_SIDE_EFFECTS (result) = 1;
5441 protected_set_expr_location (result, location);
5444 /* If we got the LHS in a different type for storing in,
5445 convert the result back to the nominal type of LHS
5446 so that the value we return always has the same type
5447 as the LHS argument. */
5449 if (olhstype == TREE_TYPE (result))
5450 goto return_result;
5452 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5453 rhs_origtype, ic_assign, false, NULL_TREE,
5454 NULL_TREE, 0);
5455 protected_set_expr_location (result, location);
5457 return_result:
5458 if (rhseval)
5459 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5460 return result;
5463 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5464 This is used to implement -fplan9-extensions. */
5466 static bool
5467 find_anonymous_field_with_type (tree struct_type, tree type)
5469 tree field;
5470 bool found;
5472 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5473 || TREE_CODE (struct_type) == UNION_TYPE);
5474 found = false;
5475 for (field = TYPE_FIELDS (struct_type);
5476 field != NULL_TREE;
5477 field = TREE_CHAIN (field))
5479 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5480 ? c_build_qualified_type (TREE_TYPE (field),
5481 TYPE_QUAL_ATOMIC)
5482 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5483 if (DECL_NAME (field) == NULL
5484 && comptypes (type, fieldtype))
5486 if (found)
5487 return false;
5488 found = true;
5490 else if (DECL_NAME (field) == NULL
5491 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5492 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5493 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5495 if (found)
5496 return false;
5497 found = true;
5500 return found;
5503 /* RHS is an expression whose type is pointer to struct. If there is
5504 an anonymous field in RHS with type TYPE, then return a pointer to
5505 that field in RHS. This is used with -fplan9-extensions. This
5506 returns NULL if no conversion could be found. */
5508 static tree
5509 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5511 tree rhs_struct_type, lhs_main_type;
5512 tree field, found_field;
5513 bool found_sub_field;
5514 tree ret;
5516 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5517 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5518 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5519 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5521 gcc_assert (POINTER_TYPE_P (type));
5522 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5523 ? c_build_qualified_type (TREE_TYPE (type),
5524 TYPE_QUAL_ATOMIC)
5525 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5527 found_field = NULL_TREE;
5528 found_sub_field = false;
5529 for (field = TYPE_FIELDS (rhs_struct_type);
5530 field != NULL_TREE;
5531 field = TREE_CHAIN (field))
5533 if (DECL_NAME (field) != NULL_TREE
5534 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5535 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5536 continue;
5537 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5538 ? c_build_qualified_type (TREE_TYPE (field),
5539 TYPE_QUAL_ATOMIC)
5540 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5541 if (comptypes (lhs_main_type, fieldtype))
5543 if (found_field != NULL_TREE)
5544 return NULL_TREE;
5545 found_field = field;
5547 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5548 lhs_main_type))
5550 if (found_field != NULL_TREE)
5551 return NULL_TREE;
5552 found_field = field;
5553 found_sub_field = true;
5557 if (found_field == NULL_TREE)
5558 return NULL_TREE;
5560 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5561 build_fold_indirect_ref (rhs), found_field,
5562 NULL_TREE);
5563 ret = build_fold_addr_expr_loc (location, ret);
5565 if (found_sub_field)
5567 ret = convert_to_anonymous_field (location, type, ret);
5568 gcc_assert (ret != NULL_TREE);
5571 return ret;
5574 /* Issue an error message for a bad initializer component.
5575 GMSGID identifies the message.
5576 The component name is taken from the spelling stack. */
5578 static void
5579 error_init (location_t loc, const char *gmsgid)
5581 char *ofwhat;
5583 /* The gmsgid may be a format string with %< and %>. */
5584 error_at (loc, gmsgid);
5585 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5586 if (*ofwhat)
5587 inform (loc, "(near initialization for %qs)", ofwhat);
5590 /* Issue a pedantic warning for a bad initializer component. OPT is
5591 the option OPT_* (from options.h) controlling this warning or 0 if
5592 it is unconditionally given. GMSGID identifies the message. The
5593 component name is taken from the spelling stack. */
5595 static void
5596 pedwarn_init (location_t location, int opt, const char *gmsgid)
5598 char *ofwhat;
5599 bool warned;
5601 /* The gmsgid may be a format string with %< and %>. */
5602 warned = pedwarn (location, opt, gmsgid);
5603 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5604 if (*ofwhat && warned)
5605 inform (location, "(near initialization for %qs)", ofwhat);
5608 /* Issue a warning for a bad initializer component.
5610 OPT is the OPT_W* value corresponding to the warning option that
5611 controls this warning. GMSGID identifies the message. The
5612 component name is taken from the spelling stack. */
5614 static void
5615 warning_init (location_t loc, int opt, const char *gmsgid)
5617 char *ofwhat;
5618 bool warned;
5620 /* The gmsgid may be a format string with %< and %>. */
5621 warned = warning_at (loc, opt, gmsgid);
5622 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5623 if (*ofwhat && warned)
5624 inform (loc, "(near initialization for %qs)", ofwhat);
5627 /* If TYPE is an array type and EXPR is a parenthesized string
5628 constant, warn if pedantic that EXPR is being used to initialize an
5629 object of type TYPE. */
5631 void
5632 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5634 if (pedantic
5635 && TREE_CODE (type) == ARRAY_TYPE
5636 && TREE_CODE (expr.value) == STRING_CST
5637 && expr.original_code != STRING_CST)
5638 pedwarn_init (loc, OPT_Wpedantic,
5639 "array initialized from parenthesized string constant");
5642 /* Convert value RHS to type TYPE as preparation for an assignment to
5643 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5644 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5645 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5646 constant before any folding.
5647 The real work of conversion is done by `convert'.
5648 The purpose of this function is to generate error messages
5649 for assignments that are not allowed in C.
5650 ERRTYPE says whether it is argument passing, assignment,
5651 initialization or return.
5653 LOCATION is the location of the assignment, EXPR_LOC is the location of
5654 the RHS or, for a function, location of an argument.
5655 FUNCTION is a tree for the function being called.
5656 PARMNUM is the number of the argument, for printing in error messages. */
5658 static tree
5659 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5660 tree rhs, tree origtype, enum impl_conv errtype,
5661 bool null_pointer_constant, tree fundecl,
5662 tree function, int parmnum)
5664 enum tree_code codel = TREE_CODE (type);
5665 tree orig_rhs = rhs;
5666 tree rhstype;
5667 enum tree_code coder;
5668 tree rname = NULL_TREE;
5669 bool objc_ok = false;
5671 if (errtype == ic_argpass)
5673 tree selector;
5674 /* Change pointer to function to the function itself for
5675 diagnostics. */
5676 if (TREE_CODE (function) == ADDR_EXPR
5677 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5678 function = TREE_OPERAND (function, 0);
5680 /* Handle an ObjC selector specially for diagnostics. */
5681 selector = objc_message_selector ();
5682 rname = function;
5683 if (selector && parmnum > 2)
5685 rname = selector;
5686 parmnum -= 2;
5690 /* This macro is used to emit diagnostics to ensure that all format
5691 strings are complete sentences, visible to gettext and checked at
5692 compile time. */
5693 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5694 do { \
5695 switch (errtype) \
5697 case ic_argpass: \
5698 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5699 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5700 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5701 "expected %qT but argument is of type %qT", \
5702 type, rhstype); \
5703 break; \
5704 case ic_assign: \
5705 pedwarn (LOCATION, OPT, AS); \
5706 break; \
5707 case ic_init: \
5708 pedwarn_init (LOCATION, OPT, IN); \
5709 break; \
5710 case ic_return: \
5711 pedwarn (LOCATION, OPT, RE); \
5712 break; \
5713 default: \
5714 gcc_unreachable (); \
5716 } while (0)
5718 /* This macro is used to emit diagnostics to ensure that all format
5719 strings are complete sentences, visible to gettext and checked at
5720 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
5721 extra parameter to enumerate qualifiers. */
5722 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5723 do { \
5724 switch (errtype) \
5726 case ic_argpass: \
5727 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5728 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5729 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5730 "expected %qT but argument is of type %qT", \
5731 type, rhstype); \
5732 break; \
5733 case ic_assign: \
5734 pedwarn (LOCATION, OPT, AS, QUALS); \
5735 break; \
5736 case ic_init: \
5737 pedwarn (LOCATION, OPT, IN, QUALS); \
5738 break; \
5739 case ic_return: \
5740 pedwarn (LOCATION, OPT, RE, QUALS); \
5741 break; \
5742 default: \
5743 gcc_unreachable (); \
5745 } while (0)
5747 /* This macro is used to emit diagnostics to ensure that all format
5748 strings are complete sentences, visible to gettext and checked at
5749 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5750 warning_at instead of pedwarn. */
5751 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5752 do { \
5753 switch (errtype) \
5755 case ic_argpass: \
5756 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5757 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5758 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5759 "expected %qT but argument is of type %qT", \
5760 type, rhstype); \
5761 break; \
5762 case ic_assign: \
5763 warning_at (LOCATION, OPT, AS, QUALS); \
5764 break; \
5765 case ic_init: \
5766 warning_at (LOCATION, OPT, IN, QUALS); \
5767 break; \
5768 case ic_return: \
5769 warning_at (LOCATION, OPT, RE, QUALS); \
5770 break; \
5771 default: \
5772 gcc_unreachable (); \
5774 } while (0)
5776 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5777 rhs = TREE_OPERAND (rhs, 0);
5779 rhstype = TREE_TYPE (rhs);
5780 coder = TREE_CODE (rhstype);
5782 if (coder == ERROR_MARK)
5783 return error_mark_node;
5785 if (c_dialect_objc ())
5787 int parmno;
5789 switch (errtype)
5791 case ic_return:
5792 parmno = 0;
5793 break;
5795 case ic_assign:
5796 parmno = -1;
5797 break;
5799 case ic_init:
5800 parmno = -2;
5801 break;
5803 default:
5804 parmno = parmnum;
5805 break;
5808 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5811 if (warn_cxx_compat)
5813 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5814 if (checktype != error_mark_node
5815 && TREE_CODE (type) == ENUMERAL_TYPE
5816 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5818 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5819 G_("enum conversion when passing argument "
5820 "%d of %qE is invalid in C++"),
5821 G_("enum conversion in assignment is "
5822 "invalid in C++"),
5823 G_("enum conversion in initialization is "
5824 "invalid in C++"),
5825 G_("enum conversion in return is "
5826 "invalid in C++"));
5830 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5831 return rhs;
5833 if (coder == VOID_TYPE)
5835 /* Except for passing an argument to an unprototyped function,
5836 this is a constraint violation. When passing an argument to
5837 an unprototyped function, it is compile-time undefined;
5838 making it a constraint in that case was rejected in
5839 DR#252. */
5840 error_at (location, "void value not ignored as it ought to be");
5841 return error_mark_node;
5843 rhs = require_complete_type (rhs);
5844 if (rhs == error_mark_node)
5845 return error_mark_node;
5846 /* A non-reference type can convert to a reference. This handles
5847 va_start, va_copy and possibly port built-ins. */
5848 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5850 if (!lvalue_p (rhs))
5852 error_at (location, "cannot pass rvalue to reference parameter");
5853 return error_mark_node;
5855 if (!c_mark_addressable (rhs))
5856 return error_mark_node;
5857 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5858 SET_EXPR_LOCATION (rhs, location);
5860 rhs = convert_for_assignment (location, expr_loc,
5861 build_pointer_type (TREE_TYPE (type)),
5862 rhs, origtype, errtype,
5863 null_pointer_constant, fundecl, function,
5864 parmnum);
5865 if (rhs == error_mark_node)
5866 return error_mark_node;
5868 rhs = build1 (NOP_EXPR, type, rhs);
5869 SET_EXPR_LOCATION (rhs, location);
5870 return rhs;
5872 /* Some types can interconvert without explicit casts. */
5873 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5874 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5875 return convert (type, rhs);
5876 /* Arithmetic types all interconvert, and enum is treated like int. */
5877 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5878 || codel == FIXED_POINT_TYPE
5879 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5880 || codel == BOOLEAN_TYPE)
5881 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5882 || coder == FIXED_POINT_TYPE
5883 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5884 || coder == BOOLEAN_TYPE))
5886 tree ret;
5887 bool save = in_late_binary_op;
5888 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5889 in_late_binary_op = true;
5890 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5891 ? expr_loc : location, type, orig_rhs);
5892 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5893 in_late_binary_op = save;
5894 return ret;
5897 /* Aggregates in different TUs might need conversion. */
5898 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5899 && codel == coder
5900 && comptypes (type, rhstype))
5901 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5902 ? expr_loc : location, type, rhs);
5904 /* Conversion to a transparent union or record from its member types.
5905 This applies only to function arguments. */
5906 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5907 && TYPE_TRANSPARENT_AGGR (type))
5908 && errtype == ic_argpass)
5910 tree memb, marginal_memb = NULL_TREE;
5912 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5914 tree memb_type = TREE_TYPE (memb);
5916 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5917 TYPE_MAIN_VARIANT (rhstype)))
5918 break;
5920 if (TREE_CODE (memb_type) != POINTER_TYPE)
5921 continue;
5923 if (coder == POINTER_TYPE)
5925 tree ttl = TREE_TYPE (memb_type);
5926 tree ttr = TREE_TYPE (rhstype);
5928 /* Any non-function converts to a [const][volatile] void *
5929 and vice versa; otherwise, targets must be the same.
5930 Meanwhile, the lhs target must have all the qualifiers of
5931 the rhs. */
5932 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5933 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5934 || comp_target_types (location, memb_type, rhstype))
5936 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5937 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5938 /* If this type won't generate any warnings, use it. */
5939 if (lquals == rquals
5940 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5941 && TREE_CODE (ttl) == FUNCTION_TYPE)
5942 ? ((lquals | rquals) == rquals)
5943 : ((lquals | rquals) == lquals)))
5944 break;
5946 /* Keep looking for a better type, but remember this one. */
5947 if (!marginal_memb)
5948 marginal_memb = memb;
5952 /* Can convert integer zero to any pointer type. */
5953 if (null_pointer_constant)
5955 rhs = null_pointer_node;
5956 break;
5960 if (memb || marginal_memb)
5962 if (!memb)
5964 /* We have only a marginally acceptable member type;
5965 it needs a warning. */
5966 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5967 tree ttr = TREE_TYPE (rhstype);
5969 /* Const and volatile mean something different for function
5970 types, so the usual warnings are not appropriate. */
5971 if (TREE_CODE (ttr) == FUNCTION_TYPE
5972 && TREE_CODE (ttl) == FUNCTION_TYPE)
5974 /* Because const and volatile on functions are
5975 restrictions that say the function will not do
5976 certain things, it is okay to use a const or volatile
5977 function where an ordinary one is wanted, but not
5978 vice-versa. */
5979 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5980 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5981 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
5982 OPT_Wdiscarded_qualifiers,
5983 G_("passing argument %d of %qE "
5984 "makes %q#v qualified function "
5985 "pointer from unqualified"),
5986 G_("assignment makes %q#v qualified "
5987 "function pointer from "
5988 "unqualified"),
5989 G_("initialization makes %q#v qualified "
5990 "function pointer from "
5991 "unqualified"),
5992 G_("return makes %q#v qualified function "
5993 "pointer from unqualified"),
5994 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5996 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5997 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5998 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
5999 OPT_Wdiscarded_qualifiers,
6000 G_("passing argument %d of %qE discards "
6001 "%qv qualifier from pointer target type"),
6002 G_("assignment discards %qv qualifier "
6003 "from pointer target type"),
6004 G_("initialization discards %qv qualifier "
6005 "from pointer target type"),
6006 G_("return discards %qv qualifier from "
6007 "pointer target type"),
6008 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6010 memb = marginal_memb;
6013 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6014 pedwarn (location, OPT_Wpedantic,
6015 "ISO C prohibits argument conversion to union type");
6017 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6018 return build_constructor_single (type, memb, rhs);
6022 /* Conversions among pointers */
6023 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6024 && (coder == codel))
6026 tree ttl = TREE_TYPE (type);
6027 tree ttr = TREE_TYPE (rhstype);
6028 tree mvl = ttl;
6029 tree mvr = ttr;
6030 bool is_opaque_pointer;
6031 int target_cmp = 0; /* Cache comp_target_types () result. */
6032 addr_space_t asl;
6033 addr_space_t asr;
6035 if (TREE_CODE (mvl) != ARRAY_TYPE)
6036 mvl = (TYPE_ATOMIC (mvl)
6037 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6038 TYPE_QUAL_ATOMIC)
6039 : TYPE_MAIN_VARIANT (mvl));
6040 if (TREE_CODE (mvr) != ARRAY_TYPE)
6041 mvr = (TYPE_ATOMIC (mvr)
6042 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6043 TYPE_QUAL_ATOMIC)
6044 : TYPE_MAIN_VARIANT (mvr));
6045 /* Opaque pointers are treated like void pointers. */
6046 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6048 /* The Plan 9 compiler permits a pointer to a struct to be
6049 automatically converted into a pointer to an anonymous field
6050 within the struct. */
6051 if (flag_plan9_extensions
6052 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6053 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6054 && mvl != mvr)
6056 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6057 if (new_rhs != NULL_TREE)
6059 rhs = new_rhs;
6060 rhstype = TREE_TYPE (rhs);
6061 coder = TREE_CODE (rhstype);
6062 ttr = TREE_TYPE (rhstype);
6063 mvr = TYPE_MAIN_VARIANT (ttr);
6067 /* C++ does not allow the implicit conversion void* -> T*. However,
6068 for the purpose of reducing the number of false positives, we
6069 tolerate the special case of
6071 int *p = NULL;
6073 where NULL is typically defined in C to be '(void *) 0'. */
6074 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6075 warning_at (errtype == ic_argpass ? expr_loc : location,
6076 OPT_Wc___compat,
6077 "request for implicit conversion "
6078 "from %qT to %qT not permitted in C++", rhstype, type);
6080 /* See if the pointers point to incompatible address spaces. */
6081 asl = TYPE_ADDR_SPACE (ttl);
6082 asr = TYPE_ADDR_SPACE (ttr);
6083 if (!null_pointer_constant_p (rhs)
6084 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6086 switch (errtype)
6088 case ic_argpass:
6089 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6090 "non-enclosed address space", parmnum, rname);
6091 break;
6092 case ic_assign:
6093 error_at (location, "assignment from pointer to "
6094 "non-enclosed address space");
6095 break;
6096 case ic_init:
6097 error_at (location, "initialization from pointer to "
6098 "non-enclosed address space");
6099 break;
6100 case ic_return:
6101 error_at (location, "return from pointer to "
6102 "non-enclosed address space");
6103 break;
6104 default:
6105 gcc_unreachable ();
6107 return error_mark_node;
6110 /* Check if the right-hand side has a format attribute but the
6111 left-hand side doesn't. */
6112 if (warn_suggest_attribute_format
6113 && check_missing_format_attribute (type, rhstype))
6115 switch (errtype)
6117 case ic_argpass:
6118 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6119 "argument %d of %qE might be "
6120 "a candidate for a format attribute",
6121 parmnum, rname);
6122 break;
6123 case ic_assign:
6124 warning_at (location, OPT_Wsuggest_attribute_format,
6125 "assignment left-hand side might be "
6126 "a candidate for a format attribute");
6127 break;
6128 case ic_init:
6129 warning_at (location, OPT_Wsuggest_attribute_format,
6130 "initialization left-hand side might be "
6131 "a candidate for a format attribute");
6132 break;
6133 case ic_return:
6134 warning_at (location, OPT_Wsuggest_attribute_format,
6135 "return type might be "
6136 "a candidate for a format attribute");
6137 break;
6138 default:
6139 gcc_unreachable ();
6143 /* Any non-function converts to a [const][volatile] void *
6144 and vice versa; otherwise, targets must be the same.
6145 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6146 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6147 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6148 || (target_cmp = comp_target_types (location, type, rhstype))
6149 || is_opaque_pointer
6150 || ((c_common_unsigned_type (mvl)
6151 == c_common_unsigned_type (mvr))
6152 && (c_common_signed_type (mvl)
6153 == c_common_signed_type (mvr))
6154 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6156 /* Warn about loss of qualifers from pointers to arrays with
6157 qualifiers on the element type. */
6158 if (TREE_CODE (ttr) == ARRAY_TYPE)
6160 ttr = strip_array_types (ttr);
6161 ttl = strip_array_types (ttl);
6163 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6164 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6165 WARNING_FOR_QUALIFIERS (location, expr_loc,
6166 OPT_Wdiscarded_array_qualifiers,
6167 G_("passing argument %d of %qE discards "
6168 "%qv qualifier from pointer target type"),
6169 G_("assignment discards %qv qualifier "
6170 "from pointer target type"),
6171 G_("initialization discards %qv qualifier "
6172 "from pointer target type"),
6173 G_("return discards %qv qualifier from "
6174 "pointer target type"),
6175 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6177 else if (pedantic
6178 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6180 (VOID_TYPE_P (ttr)
6181 && !null_pointer_constant
6182 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6183 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6184 G_("ISO C forbids passing argument %d of "
6185 "%qE between function pointer "
6186 "and %<void *%>"),
6187 G_("ISO C forbids assignment between "
6188 "function pointer and %<void *%>"),
6189 G_("ISO C forbids initialization between "
6190 "function pointer and %<void *%>"),
6191 G_("ISO C forbids return between function "
6192 "pointer and %<void *%>"));
6193 /* Const and volatile mean something different for function types,
6194 so the usual warnings are not appropriate. */
6195 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6196 && TREE_CODE (ttl) != FUNCTION_TYPE)
6198 /* Don't warn about loss of qualifier for conversions from
6199 qualified void* to pointers to arrays with corresponding
6200 qualifier on the element type. */
6201 if (!pedantic)
6202 ttl = strip_array_types (ttl);
6204 /* Assignments between atomic and non-atomic objects are OK. */
6205 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6206 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6208 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6209 OPT_Wdiscarded_qualifiers,
6210 G_("passing argument %d of %qE discards "
6211 "%qv qualifier from pointer target type"),
6212 G_("assignment discards %qv qualifier "
6213 "from pointer target type"),
6214 G_("initialization discards %qv qualifier "
6215 "from pointer target type"),
6216 G_("return discards %qv qualifier from "
6217 "pointer target type"),
6218 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6220 /* If this is not a case of ignoring a mismatch in signedness,
6221 no warning. */
6222 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6223 || target_cmp)
6225 /* If there is a mismatch, do warn. */
6226 else if (warn_pointer_sign)
6227 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6228 G_("pointer targets in passing argument "
6229 "%d of %qE differ in signedness"),
6230 G_("pointer targets in assignment "
6231 "differ in signedness"),
6232 G_("pointer targets in initialization "
6233 "differ in signedness"),
6234 G_("pointer targets in return differ "
6235 "in signedness"));
6237 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6238 && TREE_CODE (ttr) == FUNCTION_TYPE)
6240 /* Because const and volatile on functions are restrictions
6241 that say the function will not do certain things,
6242 it is okay to use a const or volatile function
6243 where an ordinary one is wanted, but not vice-versa. */
6244 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6245 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6246 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6247 OPT_Wdiscarded_qualifiers,
6248 G_("passing argument %d of %qE makes "
6249 "%q#v qualified function pointer "
6250 "from unqualified"),
6251 G_("assignment makes %q#v qualified function "
6252 "pointer from unqualified"),
6253 G_("initialization makes %q#v qualified "
6254 "function pointer from unqualified"),
6255 G_("return makes %q#v qualified function "
6256 "pointer from unqualified"),
6257 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6260 else
6261 /* Avoid warning about the volatile ObjC EH puts on decls. */
6262 if (!objc_ok)
6263 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6264 OPT_Wincompatible_pointer_types,
6265 G_("passing argument %d of %qE from "
6266 "incompatible pointer type"),
6267 G_("assignment from incompatible pointer type"),
6268 G_("initialization from incompatible "
6269 "pointer type"),
6270 G_("return from incompatible pointer type"));
6272 return convert (type, rhs);
6274 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6276 /* ??? This should not be an error when inlining calls to
6277 unprototyped functions. */
6278 error_at (location, "invalid use of non-lvalue array");
6279 return error_mark_node;
6281 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6283 /* An explicit constant 0 can convert to a pointer,
6284 or one that results from arithmetic, even including
6285 a cast to integer type. */
6286 if (!null_pointer_constant)
6287 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6288 OPT_Wint_conversion,
6289 G_("passing argument %d of %qE makes "
6290 "pointer from integer without a cast"),
6291 G_("assignment makes pointer from integer "
6292 "without a cast"),
6293 G_("initialization makes pointer from "
6294 "integer without a cast"),
6295 G_("return makes pointer from integer "
6296 "without a cast"));
6298 return convert (type, rhs);
6300 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6302 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6303 OPT_Wint_conversion,
6304 G_("passing argument %d of %qE makes integer "
6305 "from pointer without a cast"),
6306 G_("assignment makes integer from pointer "
6307 "without a cast"),
6308 G_("initialization makes integer from pointer "
6309 "without a cast"),
6310 G_("return makes integer from pointer "
6311 "without a cast"));
6312 return convert (type, rhs);
6314 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6316 tree ret;
6317 bool save = in_late_binary_op;
6318 in_late_binary_op = true;
6319 ret = convert (type, rhs);
6320 in_late_binary_op = save;
6321 return ret;
6324 switch (errtype)
6326 case ic_argpass:
6327 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6328 rname);
6329 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6330 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6331 "expected %qT but argument is of type %qT", type, rhstype);
6332 break;
6333 case ic_assign:
6334 error_at (location, "incompatible types when assigning to type %qT from "
6335 "type %qT", type, rhstype);
6336 break;
6337 case ic_init:
6338 error_at (location,
6339 "incompatible types when initializing type %qT using type %qT",
6340 type, rhstype);
6341 break;
6342 case ic_return:
6343 error_at (location,
6344 "incompatible types when returning type %qT but %qT was "
6345 "expected", rhstype, type);
6346 break;
6347 default:
6348 gcc_unreachable ();
6351 return error_mark_node;
6354 /* If VALUE is a compound expr all of whose expressions are constant, then
6355 return its value. Otherwise, return error_mark_node.
6357 This is for handling COMPOUND_EXPRs as initializer elements
6358 which is allowed with a warning when -pedantic is specified. */
6360 static tree
6361 valid_compound_expr_initializer (tree value, tree endtype)
6363 if (TREE_CODE (value) == COMPOUND_EXPR)
6365 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6366 == error_mark_node)
6367 return error_mark_node;
6368 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6369 endtype);
6371 else if (!initializer_constant_valid_p (value, endtype))
6372 return error_mark_node;
6373 else
6374 return value;
6377 /* Perform appropriate conversions on the initial value of a variable,
6378 store it in the declaration DECL,
6379 and print any error messages that are appropriate.
6380 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6381 If the init is invalid, store an ERROR_MARK.
6383 INIT_LOC is the location of the initial value. */
6385 void
6386 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6388 tree value, type;
6389 bool npc = false;
6391 /* If variable's type was invalidly declared, just ignore it. */
6393 type = TREE_TYPE (decl);
6394 if (TREE_CODE (type) == ERROR_MARK)
6395 return;
6397 /* Digest the specified initializer into an expression. */
6399 if (init)
6400 npc = null_pointer_constant_p (init);
6401 value = digest_init (init_loc, type, init, origtype, npc,
6402 true, TREE_STATIC (decl));
6404 /* Store the expression if valid; else report error. */
6406 if (!in_system_header_at (input_location)
6407 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6408 warning (OPT_Wtraditional, "traditional C rejects automatic "
6409 "aggregate initialization");
6411 DECL_INITIAL (decl) = value;
6413 /* ANSI wants warnings about out-of-range constant initializers. */
6414 STRIP_TYPE_NOPS (value);
6415 if (TREE_STATIC (decl))
6416 constant_expression_warning (value);
6418 /* Check if we need to set array size from compound literal size. */
6419 if (TREE_CODE (type) == ARRAY_TYPE
6420 && TYPE_DOMAIN (type) == 0
6421 && value != error_mark_node)
6423 tree inside_init = init;
6425 STRIP_TYPE_NOPS (inside_init);
6426 inside_init = fold (inside_init);
6428 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6430 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6432 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6434 /* For int foo[] = (int [3]){1}; we need to set array size
6435 now since later on array initializer will be just the
6436 brace enclosed list of the compound literal. */
6437 tree etype = strip_array_types (TREE_TYPE (decl));
6438 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6439 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6440 layout_type (type);
6441 layout_decl (cldecl, 0);
6442 TREE_TYPE (decl)
6443 = c_build_qualified_type (type, TYPE_QUALS (etype));
6449 /* Methods for storing and printing names for error messages. */
6451 /* Implement a spelling stack that allows components of a name to be pushed
6452 and popped. Each element on the stack is this structure. */
6454 struct spelling
6456 int kind;
6457 union
6459 unsigned HOST_WIDE_INT i;
6460 const char *s;
6461 } u;
6464 #define SPELLING_STRING 1
6465 #define SPELLING_MEMBER 2
6466 #define SPELLING_BOUNDS 3
6468 static struct spelling *spelling; /* Next stack element (unused). */
6469 static struct spelling *spelling_base; /* Spelling stack base. */
6470 static int spelling_size; /* Size of the spelling stack. */
6472 /* Macros to save and restore the spelling stack around push_... functions.
6473 Alternative to SAVE_SPELLING_STACK. */
6475 #define SPELLING_DEPTH() (spelling - spelling_base)
6476 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6478 /* Push an element on the spelling stack with type KIND and assign VALUE
6479 to MEMBER. */
6481 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6483 int depth = SPELLING_DEPTH (); \
6485 if (depth >= spelling_size) \
6487 spelling_size += 10; \
6488 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6489 spelling_size); \
6490 RESTORE_SPELLING_DEPTH (depth); \
6493 spelling->kind = (KIND); \
6494 spelling->MEMBER = (VALUE); \
6495 spelling++; \
6498 /* Push STRING on the stack. Printed literally. */
6500 static void
6501 push_string (const char *string)
6503 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6506 /* Push a member name on the stack. Printed as '.' STRING. */
6508 static void
6509 push_member_name (tree decl)
6511 const char *const string
6512 = (DECL_NAME (decl)
6513 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6514 : _("<anonymous>"));
6515 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6518 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6520 static void
6521 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6523 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6526 /* Compute the maximum size in bytes of the printed spelling. */
6528 static int
6529 spelling_length (void)
6531 int size = 0;
6532 struct spelling *p;
6534 for (p = spelling_base; p < spelling; p++)
6536 if (p->kind == SPELLING_BOUNDS)
6537 size += 25;
6538 else
6539 size += strlen (p->u.s) + 1;
6542 return size;
6545 /* Print the spelling to BUFFER and return it. */
6547 static char *
6548 print_spelling (char *buffer)
6550 char *d = buffer;
6551 struct spelling *p;
6553 for (p = spelling_base; p < spelling; p++)
6554 if (p->kind == SPELLING_BOUNDS)
6556 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6557 d += strlen (d);
6559 else
6561 const char *s;
6562 if (p->kind == SPELLING_MEMBER)
6563 *d++ = '.';
6564 for (s = p->u.s; (*d = *s++); d++)
6567 *d++ = '\0';
6568 return buffer;
6571 /* Digest the parser output INIT as an initializer for type TYPE.
6572 Return a C expression of type TYPE to represent the initial value.
6574 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6576 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6578 If INIT is a string constant, STRICT_STRING is true if it is
6579 unparenthesized or we should not warn here for it being parenthesized.
6580 For other types of INIT, STRICT_STRING is not used.
6582 INIT_LOC is the location of the INIT.
6584 REQUIRE_CONSTANT requests an error if non-constant initializers or
6585 elements are seen. */
6587 static tree
6588 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6589 bool null_pointer_constant, bool strict_string,
6590 int require_constant)
6592 enum tree_code code = TREE_CODE (type);
6593 tree inside_init = init;
6594 tree semantic_type = NULL_TREE;
6595 bool maybe_const = true;
6597 if (type == error_mark_node
6598 || !init
6599 || error_operand_p (init))
6600 return error_mark_node;
6602 STRIP_TYPE_NOPS (inside_init);
6604 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6606 semantic_type = TREE_TYPE (inside_init);
6607 inside_init = TREE_OPERAND (inside_init, 0);
6609 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6610 inside_init = decl_constant_value_for_optimization (inside_init);
6612 /* Initialization of an array of chars from a string constant
6613 optionally enclosed in braces. */
6615 if (code == ARRAY_TYPE && inside_init
6616 && TREE_CODE (inside_init) == STRING_CST)
6618 tree typ1
6619 = (TYPE_ATOMIC (TREE_TYPE (type))
6620 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6621 TYPE_QUAL_ATOMIC)
6622 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6623 /* Note that an array could be both an array of character type
6624 and an array of wchar_t if wchar_t is signed char or unsigned
6625 char. */
6626 bool char_array = (typ1 == char_type_node
6627 || typ1 == signed_char_type_node
6628 || typ1 == unsigned_char_type_node);
6629 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6630 bool char16_array = !!comptypes (typ1, char16_type_node);
6631 bool char32_array = !!comptypes (typ1, char32_type_node);
6633 if (char_array || wchar_array || char16_array || char32_array)
6635 struct c_expr expr;
6636 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6637 expr.value = inside_init;
6638 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6639 expr.original_type = NULL;
6640 maybe_warn_string_init (init_loc, type, expr);
6642 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6643 pedwarn_init (init_loc, OPT_Wpedantic,
6644 "initialization of a flexible array member");
6646 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6647 TYPE_MAIN_VARIANT (type)))
6648 return inside_init;
6650 if (char_array)
6652 if (typ2 != char_type_node)
6654 error_init (init_loc, "char-array initialized from wide "
6655 "string");
6656 return error_mark_node;
6659 else
6661 if (typ2 == char_type_node)
6663 error_init (init_loc, "wide character array initialized "
6664 "from non-wide string");
6665 return error_mark_node;
6667 else if (!comptypes(typ1, typ2))
6669 error_init (init_loc, "wide character array initialized "
6670 "from incompatible wide string");
6671 return error_mark_node;
6675 TREE_TYPE (inside_init) = type;
6676 if (TYPE_DOMAIN (type) != 0
6677 && TYPE_SIZE (type) != 0
6678 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6680 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6682 /* Subtract the size of a single (possibly wide) character
6683 because it's ok to ignore the terminating null char
6684 that is counted in the length of the constant. */
6685 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6686 (len
6687 - (TYPE_PRECISION (typ1)
6688 / BITS_PER_UNIT))))
6689 pedwarn_init (init_loc, 0,
6690 ("initializer-string for array of chars "
6691 "is too long"));
6692 else if (warn_cxx_compat
6693 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6694 warning_at (init_loc, OPT_Wc___compat,
6695 ("initializer-string for array chars "
6696 "is too long for C++"));
6699 return inside_init;
6701 else if (INTEGRAL_TYPE_P (typ1))
6703 error_init (init_loc, "array of inappropriate type initialized "
6704 "from string constant");
6705 return error_mark_node;
6709 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6710 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6711 below and handle as a constructor. */
6712 if (code == VECTOR_TYPE
6713 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6714 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6715 && TREE_CONSTANT (inside_init))
6717 if (TREE_CODE (inside_init) == VECTOR_CST
6718 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6719 TYPE_MAIN_VARIANT (type)))
6720 return inside_init;
6722 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6724 unsigned HOST_WIDE_INT ix;
6725 tree value;
6726 bool constant_p = true;
6728 /* Iterate through elements and check if all constructor
6729 elements are *_CSTs. */
6730 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6731 if (!CONSTANT_CLASS_P (value))
6733 constant_p = false;
6734 break;
6737 if (constant_p)
6738 return build_vector_from_ctor (type,
6739 CONSTRUCTOR_ELTS (inside_init));
6743 if (warn_sequence_point)
6744 verify_sequence_points (inside_init);
6746 /* Any type can be initialized
6747 from an expression of the same type, optionally with braces. */
6749 if (inside_init && TREE_TYPE (inside_init) != 0
6750 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6751 TYPE_MAIN_VARIANT (type))
6752 || (code == ARRAY_TYPE
6753 && comptypes (TREE_TYPE (inside_init), type))
6754 || (code == VECTOR_TYPE
6755 && comptypes (TREE_TYPE (inside_init), type))
6756 || (code == POINTER_TYPE
6757 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6758 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6759 TREE_TYPE (type)))))
6761 if (code == POINTER_TYPE)
6763 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6765 if (TREE_CODE (inside_init) == STRING_CST
6766 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6767 inside_init = array_to_pointer_conversion
6768 (init_loc, inside_init);
6769 else
6771 error_init (init_loc, "invalid use of non-lvalue array");
6772 return error_mark_node;
6777 if (code == VECTOR_TYPE)
6778 /* Although the types are compatible, we may require a
6779 conversion. */
6780 inside_init = convert (type, inside_init);
6782 if (require_constant
6783 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6785 /* As an extension, allow initializing objects with static storage
6786 duration with compound literals (which are then treated just as
6787 the brace enclosed list they contain). Also allow this for
6788 vectors, as we can only assign them with compound literals. */
6789 if (flag_isoc99 && code != VECTOR_TYPE)
6790 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6791 "is not constant");
6792 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6793 inside_init = DECL_INITIAL (decl);
6796 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6797 && TREE_CODE (inside_init) != CONSTRUCTOR)
6799 error_init (init_loc, "array initialized from non-constant array "
6800 "expression");
6801 return error_mark_node;
6804 /* Compound expressions can only occur here if -Wpedantic or
6805 -pedantic-errors is specified. In the later case, we always want
6806 an error. In the former case, we simply want a warning. */
6807 if (require_constant && pedantic
6808 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6810 inside_init
6811 = valid_compound_expr_initializer (inside_init,
6812 TREE_TYPE (inside_init));
6813 if (inside_init == error_mark_node)
6814 error_init (init_loc, "initializer element is not constant");
6815 else
6816 pedwarn_init (init_loc, OPT_Wpedantic,
6817 "initializer element is not constant");
6818 if (flag_pedantic_errors)
6819 inside_init = error_mark_node;
6821 else if (require_constant
6822 && !initializer_constant_valid_p (inside_init,
6823 TREE_TYPE (inside_init)))
6825 error_init (init_loc, "initializer element is not constant");
6826 inside_init = error_mark_node;
6828 else if (require_constant && !maybe_const)
6829 pedwarn_init (init_loc, 0,
6830 "initializer element is not a constant expression");
6832 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6833 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6834 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6835 type, inside_init, origtype,
6836 ic_init, null_pointer_constant,
6837 NULL_TREE, NULL_TREE, 0);
6838 return inside_init;
6841 /* Handle scalar types, including conversions. */
6843 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6844 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6845 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6847 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6848 && (TREE_CODE (init) == STRING_CST
6849 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6850 inside_init = init = array_to_pointer_conversion (init_loc, init);
6851 if (semantic_type)
6852 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6853 inside_init);
6854 inside_init
6855 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6856 inside_init, origtype, ic_init,
6857 null_pointer_constant, NULL_TREE, NULL_TREE,
6860 /* Check to see if we have already given an error message. */
6861 if (inside_init == error_mark_node)
6863 else if (require_constant && !TREE_CONSTANT (inside_init))
6865 error_init (init_loc, "initializer element is not constant");
6866 inside_init = error_mark_node;
6868 else if (require_constant
6869 && !initializer_constant_valid_p (inside_init,
6870 TREE_TYPE (inside_init)))
6872 error_init (init_loc, "initializer element is not computable at "
6873 "load time");
6874 inside_init = error_mark_node;
6876 else if (require_constant && !maybe_const)
6877 pedwarn_init (init_loc, 0,
6878 "initializer element is not a constant expression");
6880 return inside_init;
6883 /* Come here only for records and arrays. */
6885 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6887 error_init (init_loc, "variable-sized object may not be initialized");
6888 return error_mark_node;
6891 error_init (init_loc, "invalid initializer");
6892 return error_mark_node;
6895 /* Handle initializers that use braces. */
6897 /* Type of object we are accumulating a constructor for.
6898 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6899 static tree constructor_type;
6901 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6902 left to fill. */
6903 static tree constructor_fields;
6905 /* For an ARRAY_TYPE, this is the specified index
6906 at which to store the next element we get. */
6907 static tree constructor_index;
6909 /* For an ARRAY_TYPE, this is the maximum index. */
6910 static tree constructor_max_index;
6912 /* For a RECORD_TYPE, this is the first field not yet written out. */
6913 static tree constructor_unfilled_fields;
6915 /* For an ARRAY_TYPE, this is the index of the first element
6916 not yet written out. */
6917 static tree constructor_unfilled_index;
6919 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6920 This is so we can generate gaps between fields, when appropriate. */
6921 static tree constructor_bit_index;
6923 /* If we are saving up the elements rather than allocating them,
6924 this is the list of elements so far (in reverse order,
6925 most recent first). */
6926 static vec<constructor_elt, va_gc> *constructor_elements;
6928 /* 1 if constructor should be incrementally stored into a constructor chain,
6929 0 if all the elements should be kept in AVL tree. */
6930 static int constructor_incremental;
6932 /* 1 if so far this constructor's elements are all compile-time constants. */
6933 static int constructor_constant;
6935 /* 1 if so far this constructor's elements are all valid address constants. */
6936 static int constructor_simple;
6938 /* 1 if this constructor has an element that cannot be part of a
6939 constant expression. */
6940 static int constructor_nonconst;
6942 /* 1 if this constructor is erroneous so far. */
6943 static int constructor_erroneous;
6945 /* 1 if this constructor is the universal zero initializer { 0 }. */
6946 static int constructor_zeroinit;
6948 /* Structure for managing pending initializer elements, organized as an
6949 AVL tree. */
6951 struct init_node
6953 struct init_node *left, *right;
6954 struct init_node *parent;
6955 int balance;
6956 tree purpose;
6957 tree value;
6958 tree origtype;
6961 /* Tree of pending elements at this constructor level.
6962 These are elements encountered out of order
6963 which belong at places we haven't reached yet in actually
6964 writing the output.
6965 Will never hold tree nodes across GC runs. */
6966 static struct init_node *constructor_pending_elts;
6968 /* The SPELLING_DEPTH of this constructor. */
6969 static int constructor_depth;
6971 /* DECL node for which an initializer is being read.
6972 0 means we are reading a constructor expression
6973 such as (struct foo) {...}. */
6974 static tree constructor_decl;
6976 /* Nonzero if this is an initializer for a top-level decl. */
6977 static int constructor_top_level;
6979 /* Nonzero if there were any member designators in this initializer. */
6980 static int constructor_designated;
6982 /* Nesting depth of designator list. */
6983 static int designator_depth;
6985 /* Nonzero if there were diagnosed errors in this designator list. */
6986 static int designator_erroneous;
6989 /* This stack has a level for each implicit or explicit level of
6990 structuring in the initializer, including the outermost one. It
6991 saves the values of most of the variables above. */
6993 struct constructor_range_stack;
6995 struct constructor_stack
6997 struct constructor_stack *next;
6998 tree type;
6999 tree fields;
7000 tree index;
7001 tree max_index;
7002 tree unfilled_index;
7003 tree unfilled_fields;
7004 tree bit_index;
7005 vec<constructor_elt, va_gc> *elements;
7006 struct init_node *pending_elts;
7007 int offset;
7008 int depth;
7009 /* If value nonzero, this value should replace the entire
7010 constructor at this level. */
7011 struct c_expr replacement_value;
7012 struct constructor_range_stack *range_stack;
7013 char constant;
7014 char simple;
7015 char nonconst;
7016 char implicit;
7017 char erroneous;
7018 char outer;
7019 char incremental;
7020 char designated;
7021 int designator_depth;
7024 static struct constructor_stack *constructor_stack;
7026 /* This stack represents designators from some range designator up to
7027 the last designator in the list. */
7029 struct constructor_range_stack
7031 struct constructor_range_stack *next, *prev;
7032 struct constructor_stack *stack;
7033 tree range_start;
7034 tree index;
7035 tree range_end;
7036 tree fields;
7039 static struct constructor_range_stack *constructor_range_stack;
7041 /* This stack records separate initializers that are nested.
7042 Nested initializers can't happen in ANSI C, but GNU C allows them
7043 in cases like { ... (struct foo) { ... } ... }. */
7045 struct initializer_stack
7047 struct initializer_stack *next;
7048 tree decl;
7049 struct constructor_stack *constructor_stack;
7050 struct constructor_range_stack *constructor_range_stack;
7051 vec<constructor_elt, va_gc> *elements;
7052 struct spelling *spelling;
7053 struct spelling *spelling_base;
7054 int spelling_size;
7055 char top_level;
7056 char require_constant_value;
7057 char require_constant_elements;
7060 static struct initializer_stack *initializer_stack;
7062 /* Prepare to parse and output the initializer for variable DECL. */
7064 void
7065 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7067 const char *locus;
7068 struct initializer_stack *p = XNEW (struct initializer_stack);
7070 p->decl = constructor_decl;
7071 p->require_constant_value = require_constant_value;
7072 p->require_constant_elements = require_constant_elements;
7073 p->constructor_stack = constructor_stack;
7074 p->constructor_range_stack = constructor_range_stack;
7075 p->elements = constructor_elements;
7076 p->spelling = spelling;
7077 p->spelling_base = spelling_base;
7078 p->spelling_size = spelling_size;
7079 p->top_level = constructor_top_level;
7080 p->next = initializer_stack;
7081 initializer_stack = p;
7083 constructor_decl = decl;
7084 constructor_designated = 0;
7085 constructor_top_level = top_level;
7087 if (decl != 0 && decl != error_mark_node)
7089 require_constant_value = TREE_STATIC (decl);
7090 require_constant_elements
7091 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7092 /* For a scalar, you can always use any value to initialize,
7093 even within braces. */
7094 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7095 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7096 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7097 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
7098 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7100 else
7102 require_constant_value = 0;
7103 require_constant_elements = 0;
7104 locus = _("(anonymous)");
7107 constructor_stack = 0;
7108 constructor_range_stack = 0;
7110 found_missing_braces = 0;
7112 spelling_base = 0;
7113 spelling_size = 0;
7114 RESTORE_SPELLING_DEPTH (0);
7116 if (locus)
7117 push_string (locus);
7120 void
7121 finish_init (void)
7123 struct initializer_stack *p = initializer_stack;
7125 /* Free the whole constructor stack of this initializer. */
7126 while (constructor_stack)
7128 struct constructor_stack *q = constructor_stack;
7129 constructor_stack = q->next;
7130 free (q);
7133 gcc_assert (!constructor_range_stack);
7135 /* Pop back to the data of the outer initializer (if any). */
7136 free (spelling_base);
7138 constructor_decl = p->decl;
7139 require_constant_value = p->require_constant_value;
7140 require_constant_elements = p->require_constant_elements;
7141 constructor_stack = p->constructor_stack;
7142 constructor_range_stack = p->constructor_range_stack;
7143 constructor_elements = p->elements;
7144 spelling = p->spelling;
7145 spelling_base = p->spelling_base;
7146 spelling_size = p->spelling_size;
7147 constructor_top_level = p->top_level;
7148 initializer_stack = p->next;
7149 free (p);
7152 /* Call here when we see the initializer is surrounded by braces.
7153 This is instead of a call to push_init_level;
7154 it is matched by a call to pop_init_level.
7156 TYPE is the type to initialize, for a constructor expression.
7157 For an initializer for a decl, TYPE is zero. */
7159 void
7160 really_start_incremental_init (tree type)
7162 struct constructor_stack *p = XNEW (struct constructor_stack);
7164 if (type == 0)
7165 type = TREE_TYPE (constructor_decl);
7167 if (TREE_CODE (type) == VECTOR_TYPE
7168 && TYPE_VECTOR_OPAQUE (type))
7169 error ("opaque vector types cannot be initialized");
7171 p->type = constructor_type;
7172 p->fields = constructor_fields;
7173 p->index = constructor_index;
7174 p->max_index = constructor_max_index;
7175 p->unfilled_index = constructor_unfilled_index;
7176 p->unfilled_fields = constructor_unfilled_fields;
7177 p->bit_index = constructor_bit_index;
7178 p->elements = constructor_elements;
7179 p->constant = constructor_constant;
7180 p->simple = constructor_simple;
7181 p->nonconst = constructor_nonconst;
7182 p->erroneous = constructor_erroneous;
7183 p->pending_elts = constructor_pending_elts;
7184 p->depth = constructor_depth;
7185 p->replacement_value.value = 0;
7186 p->replacement_value.original_code = ERROR_MARK;
7187 p->replacement_value.original_type = NULL;
7188 p->implicit = 0;
7189 p->range_stack = 0;
7190 p->outer = 0;
7191 p->incremental = constructor_incremental;
7192 p->designated = constructor_designated;
7193 p->designator_depth = designator_depth;
7194 p->next = 0;
7195 constructor_stack = p;
7197 constructor_constant = 1;
7198 constructor_simple = 1;
7199 constructor_nonconst = 0;
7200 constructor_depth = SPELLING_DEPTH ();
7201 constructor_elements = NULL;
7202 constructor_pending_elts = 0;
7203 constructor_type = type;
7204 constructor_incremental = 1;
7205 constructor_designated = 0;
7206 constructor_zeroinit = 1;
7207 designator_depth = 0;
7208 designator_erroneous = 0;
7210 if (TREE_CODE (constructor_type) == RECORD_TYPE
7211 || TREE_CODE (constructor_type) == UNION_TYPE)
7213 constructor_fields = TYPE_FIELDS (constructor_type);
7214 /* Skip any nameless bit fields at the beginning. */
7215 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7216 && DECL_NAME (constructor_fields) == 0)
7217 constructor_fields = DECL_CHAIN (constructor_fields);
7219 constructor_unfilled_fields = constructor_fields;
7220 constructor_bit_index = bitsize_zero_node;
7222 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7224 if (TYPE_DOMAIN (constructor_type))
7226 constructor_max_index
7227 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7229 /* Detect non-empty initializations of zero-length arrays. */
7230 if (constructor_max_index == NULL_TREE
7231 && TYPE_SIZE (constructor_type))
7232 constructor_max_index = integer_minus_one_node;
7234 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7235 to initialize VLAs will cause a proper error; avoid tree
7236 checking errors as well by setting a safe value. */
7237 if (constructor_max_index
7238 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7239 constructor_max_index = integer_minus_one_node;
7241 constructor_index
7242 = convert (bitsizetype,
7243 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7245 else
7247 constructor_index = bitsize_zero_node;
7248 constructor_max_index = NULL_TREE;
7251 constructor_unfilled_index = constructor_index;
7253 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7255 /* Vectors are like simple fixed-size arrays. */
7256 constructor_max_index =
7257 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7258 constructor_index = bitsize_zero_node;
7259 constructor_unfilled_index = constructor_index;
7261 else
7263 /* Handle the case of int x = {5}; */
7264 constructor_fields = constructor_type;
7265 constructor_unfilled_fields = constructor_type;
7269 /* Push down into a subobject, for initialization.
7270 If this is for an explicit set of braces, IMPLICIT is 0.
7271 If it is because the next element belongs at a lower level,
7272 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7274 void
7275 push_init_level (location_t loc, int implicit,
7276 struct obstack *braced_init_obstack)
7278 struct constructor_stack *p;
7279 tree value = NULL_TREE;
7281 /* If we've exhausted any levels that didn't have braces,
7282 pop them now. If implicit == 1, this will have been done in
7283 process_init_element; do not repeat it here because in the case
7284 of excess initializers for an empty aggregate this leads to an
7285 infinite cycle of popping a level and immediately recreating
7286 it. */
7287 if (implicit != 1)
7289 while (constructor_stack->implicit)
7291 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7292 || TREE_CODE (constructor_type) == UNION_TYPE)
7293 && constructor_fields == 0)
7294 process_init_element (input_location,
7295 pop_init_level (loc, 1, braced_init_obstack),
7296 true, braced_init_obstack);
7297 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7298 && constructor_max_index
7299 && tree_int_cst_lt (constructor_max_index,
7300 constructor_index))
7301 process_init_element (input_location,
7302 pop_init_level (loc, 1, braced_init_obstack),
7303 true, braced_init_obstack);
7304 else
7305 break;
7309 /* Unless this is an explicit brace, we need to preserve previous
7310 content if any. */
7311 if (implicit)
7313 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7314 || TREE_CODE (constructor_type) == UNION_TYPE)
7315 && constructor_fields)
7316 value = find_init_member (constructor_fields, braced_init_obstack);
7317 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7318 value = find_init_member (constructor_index, braced_init_obstack);
7321 p = XNEW (struct constructor_stack);
7322 p->type = constructor_type;
7323 p->fields = constructor_fields;
7324 p->index = constructor_index;
7325 p->max_index = constructor_max_index;
7326 p->unfilled_index = constructor_unfilled_index;
7327 p->unfilled_fields = constructor_unfilled_fields;
7328 p->bit_index = constructor_bit_index;
7329 p->elements = constructor_elements;
7330 p->constant = constructor_constant;
7331 p->simple = constructor_simple;
7332 p->nonconst = constructor_nonconst;
7333 p->erroneous = constructor_erroneous;
7334 p->pending_elts = constructor_pending_elts;
7335 p->depth = constructor_depth;
7336 p->replacement_value.value = 0;
7337 p->replacement_value.original_code = ERROR_MARK;
7338 p->replacement_value.original_type = NULL;
7339 p->implicit = implicit;
7340 p->outer = 0;
7341 p->incremental = constructor_incremental;
7342 p->designated = constructor_designated;
7343 p->designator_depth = designator_depth;
7344 p->next = constructor_stack;
7345 p->range_stack = 0;
7346 constructor_stack = p;
7348 constructor_constant = 1;
7349 constructor_simple = 1;
7350 constructor_nonconst = 0;
7351 constructor_depth = SPELLING_DEPTH ();
7352 constructor_elements = NULL;
7353 constructor_incremental = 1;
7354 constructor_designated = 0;
7355 constructor_pending_elts = 0;
7356 if (!implicit)
7358 p->range_stack = constructor_range_stack;
7359 constructor_range_stack = 0;
7360 designator_depth = 0;
7361 designator_erroneous = 0;
7364 /* Don't die if an entire brace-pair level is superfluous
7365 in the containing level. */
7366 if (constructor_type == 0)
7368 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7369 || TREE_CODE (constructor_type) == UNION_TYPE)
7371 /* Don't die if there are extra init elts at the end. */
7372 if (constructor_fields == 0)
7373 constructor_type = 0;
7374 else
7376 constructor_type = TREE_TYPE (constructor_fields);
7377 push_member_name (constructor_fields);
7378 constructor_depth++;
7380 /* If upper initializer is designated, then mark this as
7381 designated too to prevent bogus warnings. */
7382 constructor_designated = p->designated;
7384 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7386 constructor_type = TREE_TYPE (constructor_type);
7387 push_array_bounds (tree_to_uhwi (constructor_index));
7388 constructor_depth++;
7391 if (constructor_type == 0)
7393 error_init (loc, "extra brace group at end of initializer");
7394 constructor_fields = 0;
7395 constructor_unfilled_fields = 0;
7396 return;
7399 if (value && TREE_CODE (value) == CONSTRUCTOR)
7401 constructor_constant = TREE_CONSTANT (value);
7402 constructor_simple = TREE_STATIC (value);
7403 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7404 constructor_elements = CONSTRUCTOR_ELTS (value);
7405 if (!vec_safe_is_empty (constructor_elements)
7406 && (TREE_CODE (constructor_type) == RECORD_TYPE
7407 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7408 set_nonincremental_init (braced_init_obstack);
7411 if (implicit == 1)
7412 found_missing_braces = 1;
7414 if (TREE_CODE (constructor_type) == RECORD_TYPE
7415 || TREE_CODE (constructor_type) == UNION_TYPE)
7417 constructor_fields = TYPE_FIELDS (constructor_type);
7418 /* Skip any nameless bit fields at the beginning. */
7419 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7420 && DECL_NAME (constructor_fields) == 0)
7421 constructor_fields = DECL_CHAIN (constructor_fields);
7423 constructor_unfilled_fields = constructor_fields;
7424 constructor_bit_index = bitsize_zero_node;
7426 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7428 /* Vectors are like simple fixed-size arrays. */
7429 constructor_max_index =
7430 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7431 constructor_index = bitsize_int (0);
7432 constructor_unfilled_index = constructor_index;
7434 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7436 if (TYPE_DOMAIN (constructor_type))
7438 constructor_max_index
7439 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7441 /* Detect non-empty initializations of zero-length arrays. */
7442 if (constructor_max_index == NULL_TREE
7443 && TYPE_SIZE (constructor_type))
7444 constructor_max_index = integer_minus_one_node;
7446 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7447 to initialize VLAs will cause a proper error; avoid tree
7448 checking errors as well by setting a safe value. */
7449 if (constructor_max_index
7450 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7451 constructor_max_index = integer_minus_one_node;
7453 constructor_index
7454 = convert (bitsizetype,
7455 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7457 else
7458 constructor_index = bitsize_zero_node;
7460 constructor_unfilled_index = constructor_index;
7461 if (value && TREE_CODE (value) == STRING_CST)
7463 /* We need to split the char/wchar array into individual
7464 characters, so that we don't have to special case it
7465 everywhere. */
7466 set_nonincremental_init_from_string (value, braced_init_obstack);
7469 else
7471 if (constructor_type != error_mark_node)
7472 warning_init (input_location, 0, "braces around scalar initializer");
7473 constructor_fields = constructor_type;
7474 constructor_unfilled_fields = constructor_type;
7478 /* At the end of an implicit or explicit brace level,
7479 finish up that level of constructor. If a single expression
7480 with redundant braces initialized that level, return the
7481 c_expr structure for that expression. Otherwise, the original_code
7482 element is set to ERROR_MARK.
7483 If we were outputting the elements as they are read, return 0 as the value
7484 from inner levels (process_init_element ignores that),
7485 but return error_mark_node as the value from the outermost level
7486 (that's what we want to put in DECL_INITIAL).
7487 Otherwise, return a CONSTRUCTOR expression as the value. */
7489 struct c_expr
7490 pop_init_level (location_t loc, int implicit,
7491 struct obstack *braced_init_obstack)
7493 struct constructor_stack *p;
7494 struct c_expr ret;
7495 ret.value = 0;
7496 ret.original_code = ERROR_MARK;
7497 ret.original_type = NULL;
7499 if (implicit == 0)
7501 /* When we come to an explicit close brace,
7502 pop any inner levels that didn't have explicit braces. */
7503 while (constructor_stack->implicit)
7504 process_init_element (input_location,
7505 pop_init_level (loc, 1, braced_init_obstack),
7506 true, braced_init_obstack);
7507 gcc_assert (!constructor_range_stack);
7510 /* Now output all pending elements. */
7511 constructor_incremental = 1;
7512 output_pending_init_elements (1, braced_init_obstack);
7514 p = constructor_stack;
7516 /* Error for initializing a flexible array member, or a zero-length
7517 array member in an inappropriate context. */
7518 if (constructor_type && constructor_fields
7519 && TREE_CODE (constructor_type) == ARRAY_TYPE
7520 && TYPE_DOMAIN (constructor_type)
7521 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7523 /* Silently discard empty initializations. The parser will
7524 already have pedwarned for empty brackets. */
7525 if (integer_zerop (constructor_unfilled_index))
7526 constructor_type = NULL_TREE;
7527 else
7529 gcc_assert (!TYPE_SIZE (constructor_type));
7531 if (constructor_depth > 2)
7532 error_init (loc, "initialization of flexible array member in a nested context");
7533 else
7534 pedwarn_init (loc, OPT_Wpedantic,
7535 "initialization of a flexible array member");
7537 /* We have already issued an error message for the existence
7538 of a flexible array member not at the end of the structure.
7539 Discard the initializer so that we do not die later. */
7540 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7541 constructor_type = NULL_TREE;
7545 /* Initialization with { } counts as zeroinit. */
7546 if (vec_safe_length (constructor_elements) == 0)
7547 constructor_zeroinit = 1;
7548 /* If the constructor has more than one element, it can't be { 0 }. */
7549 else if (vec_safe_length (constructor_elements) != 1)
7550 constructor_zeroinit = 0;
7552 /* Warn when some structs are initialized with direct aggregation. */
7553 if (!implicit && found_missing_braces && warn_missing_braces
7554 && !constructor_zeroinit)
7556 warning_init (loc, OPT_Wmissing_braces,
7557 "missing braces around initializer");
7560 /* Warn when some struct elements are implicitly initialized to zero. */
7561 if (warn_missing_field_initializers
7562 && constructor_type
7563 && TREE_CODE (constructor_type) == RECORD_TYPE
7564 && constructor_unfilled_fields)
7566 /* Do not warn for flexible array members or zero-length arrays. */
7567 while (constructor_unfilled_fields
7568 && (!DECL_SIZE (constructor_unfilled_fields)
7569 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7570 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7572 if (constructor_unfilled_fields
7573 /* Do not warn if this level of the initializer uses member
7574 designators; it is likely to be deliberate. */
7575 && !constructor_designated
7576 /* Do not warn about initializing with { 0 } or with { }. */
7577 && !constructor_zeroinit)
7579 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7580 "missing initializer for field %qD of %qT",
7581 constructor_unfilled_fields,
7582 constructor_type))
7583 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7584 "%qD declared here", constructor_unfilled_fields);
7588 /* Pad out the end of the structure. */
7589 if (p->replacement_value.value)
7590 /* If this closes a superfluous brace pair,
7591 just pass out the element between them. */
7592 ret = p->replacement_value;
7593 else if (constructor_type == 0)
7595 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7596 && TREE_CODE (constructor_type) != UNION_TYPE
7597 && TREE_CODE (constructor_type) != ARRAY_TYPE
7598 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7600 /* A nonincremental scalar initializer--just return
7601 the element, after verifying there is just one. */
7602 if (vec_safe_is_empty (constructor_elements))
7604 if (!constructor_erroneous)
7605 error_init (loc, "empty scalar initializer");
7606 ret.value = error_mark_node;
7608 else if (vec_safe_length (constructor_elements) != 1)
7610 error_init (loc, "extra elements in scalar initializer");
7611 ret.value = (*constructor_elements)[0].value;
7613 else
7614 ret.value = (*constructor_elements)[0].value;
7616 else
7618 if (constructor_erroneous)
7619 ret.value = error_mark_node;
7620 else
7622 ret.value = build_constructor (constructor_type,
7623 constructor_elements);
7624 if (constructor_constant)
7625 TREE_CONSTANT (ret.value) = 1;
7626 if (constructor_constant && constructor_simple)
7627 TREE_STATIC (ret.value) = 1;
7628 if (constructor_nonconst)
7629 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7633 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7635 if (constructor_nonconst)
7636 ret.original_code = C_MAYBE_CONST_EXPR;
7637 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7638 ret.original_code = ERROR_MARK;
7641 constructor_type = p->type;
7642 constructor_fields = p->fields;
7643 constructor_index = p->index;
7644 constructor_max_index = p->max_index;
7645 constructor_unfilled_index = p->unfilled_index;
7646 constructor_unfilled_fields = p->unfilled_fields;
7647 constructor_bit_index = p->bit_index;
7648 constructor_elements = p->elements;
7649 constructor_constant = p->constant;
7650 constructor_simple = p->simple;
7651 constructor_nonconst = p->nonconst;
7652 constructor_erroneous = p->erroneous;
7653 constructor_incremental = p->incremental;
7654 constructor_designated = p->designated;
7655 designator_depth = p->designator_depth;
7656 constructor_pending_elts = p->pending_elts;
7657 constructor_depth = p->depth;
7658 if (!p->implicit)
7659 constructor_range_stack = p->range_stack;
7660 RESTORE_SPELLING_DEPTH (constructor_depth);
7662 constructor_stack = p->next;
7663 free (p);
7665 if (ret.value == 0 && constructor_stack == 0)
7666 ret.value = error_mark_node;
7667 return ret;
7670 /* Common handling for both array range and field name designators.
7671 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7673 static int
7674 set_designator (location_t loc, int array,
7675 struct obstack *braced_init_obstack)
7677 tree subtype;
7678 enum tree_code subcode;
7680 /* Don't die if an entire brace-pair level is superfluous
7681 in the containing level. */
7682 if (constructor_type == 0)
7683 return 1;
7685 /* If there were errors in this designator list already, bail out
7686 silently. */
7687 if (designator_erroneous)
7688 return 1;
7690 if (!designator_depth)
7692 gcc_assert (!constructor_range_stack);
7694 /* Designator list starts at the level of closest explicit
7695 braces. */
7696 while (constructor_stack->implicit)
7697 process_init_element (input_location,
7698 pop_init_level (loc, 1, braced_init_obstack),
7699 true, braced_init_obstack);
7700 constructor_designated = 1;
7701 return 0;
7704 switch (TREE_CODE (constructor_type))
7706 case RECORD_TYPE:
7707 case UNION_TYPE:
7708 subtype = TREE_TYPE (constructor_fields);
7709 if (subtype != error_mark_node)
7710 subtype = TYPE_MAIN_VARIANT (subtype);
7711 break;
7712 case ARRAY_TYPE:
7713 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7714 break;
7715 default:
7716 gcc_unreachable ();
7719 subcode = TREE_CODE (subtype);
7720 if (array && subcode != ARRAY_TYPE)
7722 error_init (loc, "array index in non-array initializer");
7723 return 1;
7725 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7727 error_init (loc, "field name not in record or union initializer");
7728 return 1;
7731 constructor_designated = 1;
7732 push_init_level (loc, 2, braced_init_obstack);
7733 return 0;
7736 /* If there are range designators in designator list, push a new designator
7737 to constructor_range_stack. RANGE_END is end of such stack range or
7738 NULL_TREE if there is no range designator at this level. */
7740 static void
7741 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7743 struct constructor_range_stack *p;
7745 p = (struct constructor_range_stack *)
7746 obstack_alloc (braced_init_obstack,
7747 sizeof (struct constructor_range_stack));
7748 p->prev = constructor_range_stack;
7749 p->next = 0;
7750 p->fields = constructor_fields;
7751 p->range_start = constructor_index;
7752 p->index = constructor_index;
7753 p->stack = constructor_stack;
7754 p->range_end = range_end;
7755 if (constructor_range_stack)
7756 constructor_range_stack->next = p;
7757 constructor_range_stack = p;
7760 /* Within an array initializer, specify the next index to be initialized.
7761 FIRST is that index. If LAST is nonzero, then initialize a range
7762 of indices, running from FIRST through LAST. */
7764 void
7765 set_init_index (location_t loc, tree first, tree last,
7766 struct obstack *braced_init_obstack)
7768 if (set_designator (loc, 1, braced_init_obstack))
7769 return;
7771 designator_erroneous = 1;
7773 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7774 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7776 error_init (loc, "array index in initializer not of integer type");
7777 return;
7780 if (TREE_CODE (first) != INTEGER_CST)
7782 first = c_fully_fold (first, false, NULL);
7783 if (TREE_CODE (first) == INTEGER_CST)
7784 pedwarn_init (loc, OPT_Wpedantic,
7785 "array index in initializer is not "
7786 "an integer constant expression");
7789 if (last && TREE_CODE (last) != INTEGER_CST)
7791 last = c_fully_fold (last, false, NULL);
7792 if (TREE_CODE (last) == INTEGER_CST)
7793 pedwarn_init (loc, OPT_Wpedantic,
7794 "array index in initializer is not "
7795 "an integer constant expression");
7798 if (TREE_CODE (first) != INTEGER_CST)
7799 error_init (loc, "nonconstant array index in initializer");
7800 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7801 error_init (loc, "nonconstant array index in initializer");
7802 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7803 error_init (loc, "array index in non-array initializer");
7804 else if (tree_int_cst_sgn (first) == -1)
7805 error_init (loc, "array index in initializer exceeds array bounds");
7806 else if (constructor_max_index
7807 && tree_int_cst_lt (constructor_max_index, first))
7808 error_init (loc, "array index in initializer exceeds array bounds");
7809 else
7811 constant_expression_warning (first);
7812 if (last)
7813 constant_expression_warning (last);
7814 constructor_index = convert (bitsizetype, first);
7815 if (tree_int_cst_lt (constructor_index, first))
7817 constructor_index = copy_node (constructor_index);
7818 TREE_OVERFLOW (constructor_index) = 1;
7821 if (last)
7823 if (tree_int_cst_equal (first, last))
7824 last = 0;
7825 else if (tree_int_cst_lt (last, first))
7827 error_init (loc, "empty index range in initializer");
7828 last = 0;
7830 else
7832 last = convert (bitsizetype, last);
7833 if (constructor_max_index != 0
7834 && tree_int_cst_lt (constructor_max_index, last))
7836 error_init (loc, "array index range in initializer exceeds "
7837 "array bounds");
7838 last = 0;
7843 designator_depth++;
7844 designator_erroneous = 0;
7845 if (constructor_range_stack || last)
7846 push_range_stack (last, braced_init_obstack);
7850 /* Within a struct initializer, specify the next field to be initialized. */
7852 void
7853 set_init_label (location_t loc, tree fieldname,
7854 struct obstack *braced_init_obstack)
7856 tree field;
7858 if (set_designator (loc, 0, braced_init_obstack))
7859 return;
7861 designator_erroneous = 1;
7863 if (TREE_CODE (constructor_type) != RECORD_TYPE
7864 && TREE_CODE (constructor_type) != UNION_TYPE)
7866 error_init (loc, "field name not in record or union initializer");
7867 return;
7870 field = lookup_field (constructor_type, fieldname);
7872 if (field == 0)
7873 error ("unknown field %qE specified in initializer", fieldname);
7874 else
7877 constructor_fields = TREE_VALUE (field);
7878 designator_depth++;
7879 designator_erroneous = 0;
7880 if (constructor_range_stack)
7881 push_range_stack (NULL_TREE, braced_init_obstack);
7882 field = TREE_CHAIN (field);
7883 if (field)
7885 if (set_designator (loc, 0, braced_init_obstack))
7886 return;
7889 while (field != NULL_TREE);
7892 /* Add a new initializer to the tree of pending initializers. PURPOSE
7893 identifies the initializer, either array index or field in a structure.
7894 VALUE is the value of that index or field. If ORIGTYPE is not
7895 NULL_TREE, it is the original type of VALUE.
7897 IMPLICIT is true if value comes from pop_init_level (1),
7898 the new initializer has been merged with the existing one
7899 and thus no warnings should be emitted about overriding an
7900 existing initializer. */
7902 static void
7903 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7904 bool implicit, struct obstack *braced_init_obstack)
7906 struct init_node *p, **q, *r;
7908 q = &constructor_pending_elts;
7909 p = 0;
7911 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7913 while (*q != 0)
7915 p = *q;
7916 if (tree_int_cst_lt (purpose, p->purpose))
7917 q = &p->left;
7918 else if (tree_int_cst_lt (p->purpose, purpose))
7919 q = &p->right;
7920 else
7922 if (!implicit)
7924 if (TREE_SIDE_EFFECTS (p->value))
7925 warning_init (loc, 0,
7926 "initialized field with side-effects "
7927 "overwritten");
7928 else if (warn_override_init)
7929 warning_init (loc, OPT_Woverride_init,
7930 "initialized field overwritten");
7932 p->value = value;
7933 p->origtype = origtype;
7934 return;
7938 else
7940 tree bitpos;
7942 bitpos = bit_position (purpose);
7943 while (*q != NULL)
7945 p = *q;
7946 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7947 q = &p->left;
7948 else if (p->purpose != purpose)
7949 q = &p->right;
7950 else
7952 if (!implicit)
7954 if (TREE_SIDE_EFFECTS (p->value))
7955 warning_init (loc, 0,
7956 "initialized field with side-effects "
7957 "overwritten");
7958 else if (warn_override_init)
7959 warning_init (loc, OPT_Woverride_init,
7960 "initialized field overwritten");
7962 p->value = value;
7963 p->origtype = origtype;
7964 return;
7969 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7970 sizeof (struct init_node));
7971 r->purpose = purpose;
7972 r->value = value;
7973 r->origtype = origtype;
7975 *q = r;
7976 r->parent = p;
7977 r->left = 0;
7978 r->right = 0;
7979 r->balance = 0;
7981 while (p)
7983 struct init_node *s;
7985 if (r == p->left)
7987 if (p->balance == 0)
7988 p->balance = -1;
7989 else if (p->balance < 0)
7991 if (r->balance < 0)
7993 /* L rotation. */
7994 p->left = r->right;
7995 if (p->left)
7996 p->left->parent = p;
7997 r->right = p;
7999 p->balance = 0;
8000 r->balance = 0;
8002 s = p->parent;
8003 p->parent = r;
8004 r->parent = s;
8005 if (s)
8007 if (s->left == p)
8008 s->left = r;
8009 else
8010 s->right = r;
8012 else
8013 constructor_pending_elts = r;
8015 else
8017 /* LR rotation. */
8018 struct init_node *t = r->right;
8020 r->right = t->left;
8021 if (r->right)
8022 r->right->parent = r;
8023 t->left = r;
8025 p->left = t->right;
8026 if (p->left)
8027 p->left->parent = p;
8028 t->right = p;
8030 p->balance = t->balance < 0;
8031 r->balance = -(t->balance > 0);
8032 t->balance = 0;
8034 s = p->parent;
8035 p->parent = t;
8036 r->parent = t;
8037 t->parent = s;
8038 if (s)
8040 if (s->left == p)
8041 s->left = t;
8042 else
8043 s->right = t;
8045 else
8046 constructor_pending_elts = t;
8048 break;
8050 else
8052 /* p->balance == +1; growth of left side balances the node. */
8053 p->balance = 0;
8054 break;
8057 else /* r == p->right */
8059 if (p->balance == 0)
8060 /* Growth propagation from right side. */
8061 p->balance++;
8062 else if (p->balance > 0)
8064 if (r->balance > 0)
8066 /* R rotation. */
8067 p->right = r->left;
8068 if (p->right)
8069 p->right->parent = p;
8070 r->left = p;
8072 p->balance = 0;
8073 r->balance = 0;
8075 s = p->parent;
8076 p->parent = r;
8077 r->parent = s;
8078 if (s)
8080 if (s->left == p)
8081 s->left = r;
8082 else
8083 s->right = r;
8085 else
8086 constructor_pending_elts = r;
8088 else /* r->balance == -1 */
8090 /* RL rotation */
8091 struct init_node *t = r->left;
8093 r->left = t->right;
8094 if (r->left)
8095 r->left->parent = r;
8096 t->right = r;
8098 p->right = t->left;
8099 if (p->right)
8100 p->right->parent = p;
8101 t->left = p;
8103 r->balance = (t->balance < 0);
8104 p->balance = -(t->balance > 0);
8105 t->balance = 0;
8107 s = p->parent;
8108 p->parent = t;
8109 r->parent = t;
8110 t->parent = s;
8111 if (s)
8113 if (s->left == p)
8114 s->left = t;
8115 else
8116 s->right = t;
8118 else
8119 constructor_pending_elts = t;
8121 break;
8123 else
8125 /* p->balance == -1; growth of right side balances the node. */
8126 p->balance = 0;
8127 break;
8131 r = p;
8132 p = p->parent;
8136 /* Build AVL tree from a sorted chain. */
8138 static void
8139 set_nonincremental_init (struct obstack * braced_init_obstack)
8141 unsigned HOST_WIDE_INT ix;
8142 tree index, value;
8144 if (TREE_CODE (constructor_type) != RECORD_TYPE
8145 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8146 return;
8148 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8149 add_pending_init (input_location, index, value, NULL_TREE, true,
8150 braced_init_obstack);
8151 constructor_elements = NULL;
8152 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8154 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8155 /* Skip any nameless bit fields at the beginning. */
8156 while (constructor_unfilled_fields != 0
8157 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8158 && DECL_NAME (constructor_unfilled_fields) == 0)
8159 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8162 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8164 if (TYPE_DOMAIN (constructor_type))
8165 constructor_unfilled_index
8166 = convert (bitsizetype,
8167 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8168 else
8169 constructor_unfilled_index = bitsize_zero_node;
8171 constructor_incremental = 0;
8174 /* Build AVL tree from a string constant. */
8176 static void
8177 set_nonincremental_init_from_string (tree str,
8178 struct obstack * braced_init_obstack)
8180 tree value, purpose, type;
8181 HOST_WIDE_INT val[2];
8182 const char *p, *end;
8183 int byte, wchar_bytes, charwidth, bitpos;
8185 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8187 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8188 charwidth = TYPE_PRECISION (char_type_node);
8189 type = TREE_TYPE (constructor_type);
8190 p = TREE_STRING_POINTER (str);
8191 end = p + TREE_STRING_LENGTH (str);
8193 for (purpose = bitsize_zero_node;
8194 p < end
8195 && !(constructor_max_index
8196 && tree_int_cst_lt (constructor_max_index, purpose));
8197 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8199 if (wchar_bytes == 1)
8201 val[0] = (unsigned char) *p++;
8202 val[1] = 0;
8204 else
8206 val[1] = 0;
8207 val[0] = 0;
8208 for (byte = 0; byte < wchar_bytes; byte++)
8210 if (BYTES_BIG_ENDIAN)
8211 bitpos = (wchar_bytes - byte - 1) * charwidth;
8212 else
8213 bitpos = byte * charwidth;
8214 val[bitpos % HOST_BITS_PER_WIDE_INT]
8215 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8216 << (bitpos % HOST_BITS_PER_WIDE_INT);
8220 if (!TYPE_UNSIGNED (type))
8222 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8223 if (bitpos < HOST_BITS_PER_WIDE_INT)
8225 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8227 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8228 val[1] = -1;
8231 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8233 if (val[0] < 0)
8234 val[1] = -1;
8236 else if (val[1] & (((HOST_WIDE_INT) 1)
8237 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8238 val[1] |= ((HOST_WIDE_INT) -1)
8239 << (bitpos - HOST_BITS_PER_WIDE_INT);
8242 value = wide_int_to_tree (type,
8243 wide_int::from_array (val, 2,
8244 HOST_BITS_PER_WIDE_INT * 2));
8245 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8246 braced_init_obstack);
8249 constructor_incremental = 0;
8252 /* Return value of FIELD in pending initializer or zero if the field was
8253 not initialized yet. */
8255 static tree
8256 find_init_member (tree field, struct obstack * braced_init_obstack)
8258 struct init_node *p;
8260 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8262 if (constructor_incremental
8263 && tree_int_cst_lt (field, constructor_unfilled_index))
8264 set_nonincremental_init (braced_init_obstack);
8266 p = constructor_pending_elts;
8267 while (p)
8269 if (tree_int_cst_lt (field, p->purpose))
8270 p = p->left;
8271 else if (tree_int_cst_lt (p->purpose, field))
8272 p = p->right;
8273 else
8274 return p->value;
8277 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8279 tree bitpos = bit_position (field);
8281 if (constructor_incremental
8282 && (!constructor_unfilled_fields
8283 || tree_int_cst_lt (bitpos,
8284 bit_position (constructor_unfilled_fields))))
8285 set_nonincremental_init (braced_init_obstack);
8287 p = constructor_pending_elts;
8288 while (p)
8290 if (field == p->purpose)
8291 return p->value;
8292 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8293 p = p->left;
8294 else
8295 p = p->right;
8298 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8300 if (!vec_safe_is_empty (constructor_elements)
8301 && (constructor_elements->last ().index == field))
8302 return constructor_elements->last ().value;
8304 return 0;
8307 /* "Output" the next constructor element.
8308 At top level, really output it to assembler code now.
8309 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8310 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8311 TYPE is the data type that the containing data type wants here.
8312 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8313 If VALUE is a string constant, STRICT_STRING is true if it is
8314 unparenthesized or we should not warn here for it being parenthesized.
8315 For other types of VALUE, STRICT_STRING is not used.
8317 PENDING if non-nil means output pending elements that belong
8318 right after this element. (PENDING is normally 1;
8319 it is 0 while outputting pending elements, to avoid recursion.)
8321 IMPLICIT is true if value comes from pop_init_level (1),
8322 the new initializer has been merged with the existing one
8323 and thus no warnings should be emitted about overriding an
8324 existing initializer. */
8326 static void
8327 output_init_element (location_t loc, tree value, tree origtype,
8328 bool strict_string, tree type, tree field, int pending,
8329 bool implicit, struct obstack * braced_init_obstack)
8331 tree semantic_type = NULL_TREE;
8332 bool maybe_const = true;
8333 bool npc;
8335 if (type == error_mark_node || value == error_mark_node)
8337 constructor_erroneous = 1;
8338 return;
8340 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8341 && (TREE_CODE (value) == STRING_CST
8342 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8343 && !(TREE_CODE (value) == STRING_CST
8344 && TREE_CODE (type) == ARRAY_TYPE
8345 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8346 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8347 TYPE_MAIN_VARIANT (type)))
8348 value = array_to_pointer_conversion (input_location, value);
8350 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8351 && require_constant_value && pending)
8353 /* As an extension, allow initializing objects with static storage
8354 duration with compound literals (which are then treated just as
8355 the brace enclosed list they contain). */
8356 if (flag_isoc99)
8357 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8358 "constant");
8359 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8360 value = DECL_INITIAL (decl);
8363 npc = null_pointer_constant_p (value);
8364 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8366 semantic_type = TREE_TYPE (value);
8367 value = TREE_OPERAND (value, 0);
8369 value = c_fully_fold (value, require_constant_value, &maybe_const);
8371 if (value == error_mark_node)
8372 constructor_erroneous = 1;
8373 else if (!TREE_CONSTANT (value))
8374 constructor_constant = 0;
8375 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8376 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8377 || TREE_CODE (constructor_type) == UNION_TYPE)
8378 && DECL_C_BIT_FIELD (field)
8379 && TREE_CODE (value) != INTEGER_CST))
8380 constructor_simple = 0;
8381 if (!maybe_const)
8382 constructor_nonconst = 1;
8384 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8386 if (require_constant_value)
8388 error_init (loc, "initializer element is not constant");
8389 value = error_mark_node;
8391 else if (require_constant_elements)
8392 pedwarn (loc, OPT_Wpedantic,
8393 "initializer element is not computable at load time");
8395 else if (!maybe_const
8396 && (require_constant_value || require_constant_elements))
8397 pedwarn_init (loc, OPT_Wpedantic,
8398 "initializer element is not a constant expression");
8400 /* Issue -Wc++-compat warnings about initializing a bitfield with
8401 enum type. */
8402 if (warn_cxx_compat
8403 && field != NULL_TREE
8404 && TREE_CODE (field) == FIELD_DECL
8405 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8406 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8407 != TYPE_MAIN_VARIANT (type))
8408 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8410 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8411 if (checktype != error_mark_node
8412 && (TYPE_MAIN_VARIANT (checktype)
8413 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8414 warning_init (loc, OPT_Wc___compat,
8415 "enum conversion in initialization is invalid in C++");
8418 /* If this field is empty (and not at the end of structure),
8419 don't do anything other than checking the initializer. */
8420 if (field
8421 && (TREE_TYPE (field) == error_mark_node
8422 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8423 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8424 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8425 || DECL_CHAIN (field)))))
8426 return;
8428 if (semantic_type)
8429 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8430 value = digest_init (loc, type, value, origtype, npc, strict_string,
8431 require_constant_value);
8432 if (value == error_mark_node)
8434 constructor_erroneous = 1;
8435 return;
8437 if (require_constant_value || require_constant_elements)
8438 constant_expression_warning (value);
8440 /* If this element doesn't come next in sequence,
8441 put it on constructor_pending_elts. */
8442 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8443 && (!constructor_incremental
8444 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8446 if (constructor_incremental
8447 && tree_int_cst_lt (field, constructor_unfilled_index))
8448 set_nonincremental_init (braced_init_obstack);
8450 add_pending_init (loc, field, value, origtype, implicit,
8451 braced_init_obstack);
8452 return;
8454 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8455 && (!constructor_incremental
8456 || field != constructor_unfilled_fields))
8458 /* We do this for records but not for unions. In a union,
8459 no matter which field is specified, it can be initialized
8460 right away since it starts at the beginning of the union. */
8461 if (constructor_incremental)
8463 if (!constructor_unfilled_fields)
8464 set_nonincremental_init (braced_init_obstack);
8465 else
8467 tree bitpos, unfillpos;
8469 bitpos = bit_position (field);
8470 unfillpos = bit_position (constructor_unfilled_fields);
8472 if (tree_int_cst_lt (bitpos, unfillpos))
8473 set_nonincremental_init (braced_init_obstack);
8477 add_pending_init (loc, field, value, origtype, implicit,
8478 braced_init_obstack);
8479 return;
8481 else if (TREE_CODE (constructor_type) == UNION_TYPE
8482 && !vec_safe_is_empty (constructor_elements))
8484 if (!implicit)
8486 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8487 warning_init (loc, 0,
8488 "initialized field with side-effects overwritten");
8489 else if (warn_override_init)
8490 warning_init (loc, OPT_Woverride_init,
8491 "initialized field overwritten");
8494 /* We can have just one union field set. */
8495 constructor_elements = NULL;
8498 /* Otherwise, output this element either to
8499 constructor_elements or to the assembler file. */
8501 constructor_elt celt = {field, value};
8502 vec_safe_push (constructor_elements, celt);
8504 /* Advance the variable that indicates sequential elements output. */
8505 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8506 constructor_unfilled_index
8507 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8508 bitsize_one_node);
8509 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8511 constructor_unfilled_fields
8512 = DECL_CHAIN (constructor_unfilled_fields);
8514 /* Skip any nameless bit fields. */
8515 while (constructor_unfilled_fields != 0
8516 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8517 && DECL_NAME (constructor_unfilled_fields) == 0)
8518 constructor_unfilled_fields =
8519 DECL_CHAIN (constructor_unfilled_fields);
8521 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8522 constructor_unfilled_fields = 0;
8524 /* Now output any pending elements which have become next. */
8525 if (pending)
8526 output_pending_init_elements (0, braced_init_obstack);
8529 /* Output any pending elements which have become next.
8530 As we output elements, constructor_unfilled_{fields,index}
8531 advances, which may cause other elements to become next;
8532 if so, they too are output.
8534 If ALL is 0, we return when there are
8535 no more pending elements to output now.
8537 If ALL is 1, we output space as necessary so that
8538 we can output all the pending elements. */
8539 static void
8540 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8542 struct init_node *elt = constructor_pending_elts;
8543 tree next;
8545 retry:
8547 /* Look through the whole pending tree.
8548 If we find an element that should be output now,
8549 output it. Otherwise, set NEXT to the element
8550 that comes first among those still pending. */
8552 next = 0;
8553 while (elt)
8555 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8557 if (tree_int_cst_equal (elt->purpose,
8558 constructor_unfilled_index))
8559 output_init_element (input_location, elt->value, elt->origtype,
8560 true, TREE_TYPE (constructor_type),
8561 constructor_unfilled_index, 0, false,
8562 braced_init_obstack);
8563 else if (tree_int_cst_lt (constructor_unfilled_index,
8564 elt->purpose))
8566 /* Advance to the next smaller node. */
8567 if (elt->left)
8568 elt = elt->left;
8569 else
8571 /* We have reached the smallest node bigger than the
8572 current unfilled index. Fill the space first. */
8573 next = elt->purpose;
8574 break;
8577 else
8579 /* Advance to the next bigger node. */
8580 if (elt->right)
8581 elt = elt->right;
8582 else
8584 /* We have reached the biggest node in a subtree. Find
8585 the parent of it, which is the next bigger node. */
8586 while (elt->parent && elt->parent->right == elt)
8587 elt = elt->parent;
8588 elt = elt->parent;
8589 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8590 elt->purpose))
8592 next = elt->purpose;
8593 break;
8598 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8599 || TREE_CODE (constructor_type) == UNION_TYPE)
8601 tree ctor_unfilled_bitpos, elt_bitpos;
8603 /* If the current record is complete we are done. */
8604 if (constructor_unfilled_fields == 0)
8605 break;
8607 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8608 elt_bitpos = bit_position (elt->purpose);
8609 /* We can't compare fields here because there might be empty
8610 fields in between. */
8611 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8613 constructor_unfilled_fields = elt->purpose;
8614 output_init_element (input_location, elt->value, elt->origtype,
8615 true, TREE_TYPE (elt->purpose),
8616 elt->purpose, 0, false,
8617 braced_init_obstack);
8619 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8621 /* Advance to the next smaller node. */
8622 if (elt->left)
8623 elt = elt->left;
8624 else
8626 /* We have reached the smallest node bigger than the
8627 current unfilled field. Fill the space first. */
8628 next = elt->purpose;
8629 break;
8632 else
8634 /* Advance to the next bigger node. */
8635 if (elt->right)
8636 elt = elt->right;
8637 else
8639 /* We have reached the biggest node in a subtree. Find
8640 the parent of it, which is the next bigger node. */
8641 while (elt->parent && elt->parent->right == elt)
8642 elt = elt->parent;
8643 elt = elt->parent;
8644 if (elt
8645 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8646 bit_position (elt->purpose))))
8648 next = elt->purpose;
8649 break;
8656 /* Ordinarily return, but not if we want to output all
8657 and there are elements left. */
8658 if (!(all && next != 0))
8659 return;
8661 /* If it's not incremental, just skip over the gap, so that after
8662 jumping to retry we will output the next successive element. */
8663 if (TREE_CODE (constructor_type) == RECORD_TYPE
8664 || TREE_CODE (constructor_type) == UNION_TYPE)
8665 constructor_unfilled_fields = next;
8666 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8667 constructor_unfilled_index = next;
8669 /* ELT now points to the node in the pending tree with the next
8670 initializer to output. */
8671 goto retry;
8674 /* Add one non-braced element to the current constructor level.
8675 This adjusts the current position within the constructor's type.
8676 This may also start or terminate implicit levels
8677 to handle a partly-braced initializer.
8679 Once this has found the correct level for the new element,
8680 it calls output_init_element.
8682 IMPLICIT is true if value comes from pop_init_level (1),
8683 the new initializer has been merged with the existing one
8684 and thus no warnings should be emitted about overriding an
8685 existing initializer. */
8687 void
8688 process_init_element (location_t loc, struct c_expr value, bool implicit,
8689 struct obstack * braced_init_obstack)
8691 tree orig_value = value.value;
8692 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8693 bool strict_string = value.original_code == STRING_CST;
8694 bool was_designated = designator_depth != 0;
8696 designator_depth = 0;
8697 designator_erroneous = 0;
8699 if (!implicit && value.value && !integer_zerop (value.value))
8700 constructor_zeroinit = 0;
8702 /* Handle superfluous braces around string cst as in
8703 char x[] = {"foo"}; */
8704 if (string_flag
8705 && constructor_type
8706 && !was_designated
8707 && TREE_CODE (constructor_type) == ARRAY_TYPE
8708 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8709 && integer_zerop (constructor_unfilled_index))
8711 if (constructor_stack->replacement_value.value)
8712 error_init (loc, "excess elements in char array initializer");
8713 constructor_stack->replacement_value = value;
8714 return;
8717 if (constructor_stack->replacement_value.value != 0)
8719 error_init (loc, "excess elements in struct initializer");
8720 return;
8723 /* Ignore elements of a brace group if it is entirely superfluous
8724 and has already been diagnosed. */
8725 if (constructor_type == 0)
8726 return;
8728 if (!implicit && warn_designated_init && !was_designated
8729 && TREE_CODE (constructor_type) == RECORD_TYPE
8730 && lookup_attribute ("designated_init",
8731 TYPE_ATTRIBUTES (constructor_type)))
8732 warning_init (loc,
8733 OPT_Wdesignated_init,
8734 "positional initialization of field "
8735 "in %<struct%> declared with %<designated_init%> attribute");
8737 /* If we've exhausted any levels that didn't have braces,
8738 pop them now. */
8739 while (constructor_stack->implicit)
8741 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8742 || TREE_CODE (constructor_type) == UNION_TYPE)
8743 && constructor_fields == 0)
8744 process_init_element (loc,
8745 pop_init_level (loc, 1, braced_init_obstack),
8746 true, braced_init_obstack);
8747 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8748 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8749 && constructor_max_index
8750 && tree_int_cst_lt (constructor_max_index,
8751 constructor_index))
8752 process_init_element (loc,
8753 pop_init_level (loc, 1, braced_init_obstack),
8754 true, braced_init_obstack);
8755 else
8756 break;
8759 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8760 if (constructor_range_stack)
8762 /* If value is a compound literal and we'll be just using its
8763 content, don't put it into a SAVE_EXPR. */
8764 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8765 || !require_constant_value
8766 || flag_isoc99)
8768 tree semantic_type = NULL_TREE;
8769 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8771 semantic_type = TREE_TYPE (value.value);
8772 value.value = TREE_OPERAND (value.value, 0);
8774 value.value = c_save_expr (value.value);
8775 if (semantic_type)
8776 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8777 value.value);
8781 while (1)
8783 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8785 tree fieldtype;
8786 enum tree_code fieldcode;
8788 if (constructor_fields == 0)
8790 pedwarn_init (loc, 0, "excess elements in struct initializer");
8791 break;
8794 fieldtype = TREE_TYPE (constructor_fields);
8795 if (fieldtype != error_mark_node)
8796 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8797 fieldcode = TREE_CODE (fieldtype);
8799 /* Error for non-static initialization of a flexible array member. */
8800 if (fieldcode == ARRAY_TYPE
8801 && !require_constant_value
8802 && TYPE_SIZE (fieldtype) == NULL_TREE
8803 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8805 error_init (loc, "non-static initialization of a flexible "
8806 "array member");
8807 break;
8810 /* Accept a string constant to initialize a subarray. */
8811 if (value.value != 0
8812 && fieldcode == ARRAY_TYPE
8813 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8814 && string_flag)
8815 value.value = orig_value;
8816 /* Otherwise, if we have come to a subaggregate,
8817 and we don't have an element of its type, push into it. */
8818 else if (value.value != 0
8819 && value.value != error_mark_node
8820 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8821 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8822 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8824 push_init_level (loc, 1, braced_init_obstack);
8825 continue;
8828 if (value.value)
8830 push_member_name (constructor_fields);
8831 output_init_element (loc, value.value, value.original_type,
8832 strict_string, fieldtype,
8833 constructor_fields, 1, implicit,
8834 braced_init_obstack);
8835 RESTORE_SPELLING_DEPTH (constructor_depth);
8837 else
8838 /* Do the bookkeeping for an element that was
8839 directly output as a constructor. */
8841 /* For a record, keep track of end position of last field. */
8842 if (DECL_SIZE (constructor_fields))
8843 constructor_bit_index
8844 = size_binop_loc (input_location, PLUS_EXPR,
8845 bit_position (constructor_fields),
8846 DECL_SIZE (constructor_fields));
8848 /* If the current field was the first one not yet written out,
8849 it isn't now, so update. */
8850 if (constructor_unfilled_fields == constructor_fields)
8852 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8853 /* Skip any nameless bit fields. */
8854 while (constructor_unfilled_fields != 0
8855 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8856 && DECL_NAME (constructor_unfilled_fields) == 0)
8857 constructor_unfilled_fields =
8858 DECL_CHAIN (constructor_unfilled_fields);
8862 constructor_fields = DECL_CHAIN (constructor_fields);
8863 /* Skip any nameless bit fields at the beginning. */
8864 while (constructor_fields != 0
8865 && DECL_C_BIT_FIELD (constructor_fields)
8866 && DECL_NAME (constructor_fields) == 0)
8867 constructor_fields = DECL_CHAIN (constructor_fields);
8869 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8871 tree fieldtype;
8872 enum tree_code fieldcode;
8874 if (constructor_fields == 0)
8876 pedwarn_init (loc, 0,
8877 "excess elements in union initializer");
8878 break;
8881 fieldtype = TREE_TYPE (constructor_fields);
8882 if (fieldtype != error_mark_node)
8883 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8884 fieldcode = TREE_CODE (fieldtype);
8886 /* Warn that traditional C rejects initialization of unions.
8887 We skip the warning if the value is zero. This is done
8888 under the assumption that the zero initializer in user
8889 code appears conditioned on e.g. __STDC__ to avoid
8890 "missing initializer" warnings and relies on default
8891 initialization to zero in the traditional C case.
8892 We also skip the warning if the initializer is designated,
8893 again on the assumption that this must be conditional on
8894 __STDC__ anyway (and we've already complained about the
8895 member-designator already). */
8896 if (!in_system_header_at (input_location) && !constructor_designated
8897 && !(value.value && (integer_zerop (value.value)
8898 || real_zerop (value.value))))
8899 warning (OPT_Wtraditional, "traditional C rejects initialization "
8900 "of unions");
8902 /* Accept a string constant to initialize a subarray. */
8903 if (value.value != 0
8904 && fieldcode == ARRAY_TYPE
8905 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8906 && string_flag)
8907 value.value = orig_value;
8908 /* Otherwise, if we have come to a subaggregate,
8909 and we don't have an element of its type, push into it. */
8910 else if (value.value != 0
8911 && value.value != error_mark_node
8912 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8913 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8914 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8916 push_init_level (loc, 1, braced_init_obstack);
8917 continue;
8920 if (value.value)
8922 push_member_name (constructor_fields);
8923 output_init_element (loc, value.value, value.original_type,
8924 strict_string, fieldtype,
8925 constructor_fields, 1, implicit,
8926 braced_init_obstack);
8927 RESTORE_SPELLING_DEPTH (constructor_depth);
8929 else
8930 /* Do the bookkeeping for an element that was
8931 directly output as a constructor. */
8933 constructor_bit_index = DECL_SIZE (constructor_fields);
8934 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8937 constructor_fields = 0;
8939 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8941 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8942 enum tree_code eltcode = TREE_CODE (elttype);
8944 /* Accept a string constant to initialize a subarray. */
8945 if (value.value != 0
8946 && eltcode == ARRAY_TYPE
8947 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8948 && string_flag)
8949 value.value = orig_value;
8950 /* Otherwise, if we have come to a subaggregate,
8951 and we don't have an element of its type, push into it. */
8952 else if (value.value != 0
8953 && value.value != error_mark_node
8954 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8955 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8956 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8958 push_init_level (loc, 1, braced_init_obstack);
8959 continue;
8962 if (constructor_max_index != 0
8963 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8964 || integer_all_onesp (constructor_max_index)))
8966 pedwarn_init (loc, 0,
8967 "excess elements in array initializer");
8968 break;
8971 /* Now output the actual element. */
8972 if (value.value)
8974 push_array_bounds (tree_to_uhwi (constructor_index));
8975 output_init_element (loc, value.value, value.original_type,
8976 strict_string, elttype,
8977 constructor_index, 1, implicit,
8978 braced_init_obstack);
8979 RESTORE_SPELLING_DEPTH (constructor_depth);
8982 constructor_index
8983 = size_binop_loc (input_location, PLUS_EXPR,
8984 constructor_index, bitsize_one_node);
8986 if (!value.value)
8987 /* If we are doing the bookkeeping for an element that was
8988 directly output as a constructor, we must update
8989 constructor_unfilled_index. */
8990 constructor_unfilled_index = constructor_index;
8992 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8994 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8996 /* Do a basic check of initializer size. Note that vectors
8997 always have a fixed size derived from their type. */
8998 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9000 pedwarn_init (loc, 0,
9001 "excess elements in vector initializer");
9002 break;
9005 /* Now output the actual element. */
9006 if (value.value)
9008 if (TREE_CODE (value.value) == VECTOR_CST)
9009 elttype = TYPE_MAIN_VARIANT (constructor_type);
9010 output_init_element (loc, value.value, value.original_type,
9011 strict_string, elttype,
9012 constructor_index, 1, implicit,
9013 braced_init_obstack);
9016 constructor_index
9017 = size_binop_loc (input_location,
9018 PLUS_EXPR, constructor_index, bitsize_one_node);
9020 if (!value.value)
9021 /* If we are doing the bookkeeping for an element that was
9022 directly output as a constructor, we must update
9023 constructor_unfilled_index. */
9024 constructor_unfilled_index = constructor_index;
9027 /* Handle the sole element allowed in a braced initializer
9028 for a scalar variable. */
9029 else if (constructor_type != error_mark_node
9030 && constructor_fields == 0)
9032 pedwarn_init (loc, 0,
9033 "excess elements in scalar initializer");
9034 break;
9036 else
9038 if (value.value)
9039 output_init_element (loc, value.value, value.original_type,
9040 strict_string, constructor_type,
9041 NULL_TREE, 1, implicit,
9042 braced_init_obstack);
9043 constructor_fields = 0;
9046 /* Handle range initializers either at this level or anywhere higher
9047 in the designator stack. */
9048 if (constructor_range_stack)
9050 struct constructor_range_stack *p, *range_stack;
9051 int finish = 0;
9053 range_stack = constructor_range_stack;
9054 constructor_range_stack = 0;
9055 while (constructor_stack != range_stack->stack)
9057 gcc_assert (constructor_stack->implicit);
9058 process_init_element (loc,
9059 pop_init_level (loc, 1,
9060 braced_init_obstack),
9061 true, braced_init_obstack);
9063 for (p = range_stack;
9064 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9065 p = p->prev)
9067 gcc_assert (constructor_stack->implicit);
9068 process_init_element (loc,
9069 pop_init_level (loc, 1,
9070 braced_init_obstack),
9071 true, braced_init_obstack);
9074 p->index = size_binop_loc (input_location,
9075 PLUS_EXPR, p->index, bitsize_one_node);
9076 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9077 finish = 1;
9079 while (1)
9081 constructor_index = p->index;
9082 constructor_fields = p->fields;
9083 if (finish && p->range_end && p->index == p->range_start)
9085 finish = 0;
9086 p->prev = 0;
9088 p = p->next;
9089 if (!p)
9090 break;
9091 push_init_level (loc, 2, braced_init_obstack);
9092 p->stack = constructor_stack;
9093 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9094 p->index = p->range_start;
9097 if (!finish)
9098 constructor_range_stack = range_stack;
9099 continue;
9102 break;
9105 constructor_range_stack = 0;
9108 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9109 (guaranteed to be 'volatile' or null) and ARGS (represented using
9110 an ASM_EXPR node). */
9111 tree
9112 build_asm_stmt (tree cv_qualifier, tree args)
9114 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9115 ASM_VOLATILE_P (args) = 1;
9116 return add_stmt (args);
9119 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9120 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9121 SIMPLE indicates whether there was anything at all after the
9122 string in the asm expression -- asm("blah") and asm("blah" : )
9123 are subtly different. We use a ASM_EXPR node to represent this. */
9124 tree
9125 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9126 tree clobbers, tree labels, bool simple)
9128 tree tail;
9129 tree args;
9130 int i;
9131 const char *constraint;
9132 const char **oconstraints;
9133 bool allows_mem, allows_reg, is_inout;
9134 int ninputs, noutputs;
9136 ninputs = list_length (inputs);
9137 noutputs = list_length (outputs);
9138 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9140 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9142 /* Remove output conversions that change the type but not the mode. */
9143 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9145 tree output = TREE_VALUE (tail);
9147 output = c_fully_fold (output, false, NULL);
9149 /* ??? Really, this should not be here. Users should be using a
9150 proper lvalue, dammit. But there's a long history of using casts
9151 in the output operands. In cases like longlong.h, this becomes a
9152 primitive form of typechecking -- if the cast can be removed, then
9153 the output operand had a type of the proper width; otherwise we'll
9154 get an error. Gross, but ... */
9155 STRIP_NOPS (output);
9157 if (!lvalue_or_else (loc, output, lv_asm))
9158 output = error_mark_node;
9160 if (output != error_mark_node
9161 && (TREE_READONLY (output)
9162 || TYPE_READONLY (TREE_TYPE (output))
9163 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9164 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9165 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9166 readonly_error (loc, output, lv_asm);
9168 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9169 oconstraints[i] = constraint;
9171 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9172 &allows_mem, &allows_reg, &is_inout))
9174 /* If the operand is going to end up in memory,
9175 mark it addressable. */
9176 if (!allows_reg && !c_mark_addressable (output))
9177 output = error_mark_node;
9178 if (!(!allows_reg && allows_mem)
9179 && output != error_mark_node
9180 && VOID_TYPE_P (TREE_TYPE (output)))
9182 error_at (loc, "invalid use of void expression");
9183 output = error_mark_node;
9186 else
9187 output = error_mark_node;
9189 TREE_VALUE (tail) = output;
9192 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9194 tree input;
9196 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9197 input = TREE_VALUE (tail);
9199 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9200 oconstraints, &allows_mem, &allows_reg))
9202 /* If the operand is going to end up in memory,
9203 mark it addressable. */
9204 if (!allows_reg && allows_mem)
9206 input = c_fully_fold (input, false, NULL);
9208 /* Strip the nops as we allow this case. FIXME, this really
9209 should be rejected or made deprecated. */
9210 STRIP_NOPS (input);
9211 if (!c_mark_addressable (input))
9212 input = error_mark_node;
9214 else
9216 struct c_expr expr;
9217 memset (&expr, 0, sizeof (expr));
9218 expr.value = input;
9219 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9220 input = c_fully_fold (expr.value, false, NULL);
9222 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9224 error_at (loc, "invalid use of void expression");
9225 input = error_mark_node;
9229 else
9230 input = error_mark_node;
9232 TREE_VALUE (tail) = input;
9235 /* ASMs with labels cannot have outputs. This should have been
9236 enforced by the parser. */
9237 gcc_assert (outputs == NULL || labels == NULL);
9239 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9241 /* asm statements without outputs, including simple ones, are treated
9242 as volatile. */
9243 ASM_INPUT_P (args) = simple;
9244 ASM_VOLATILE_P (args) = (noutputs == 0);
9246 return args;
9249 /* Generate a goto statement to LABEL. LOC is the location of the
9250 GOTO. */
9252 tree
9253 c_finish_goto_label (location_t loc, tree label)
9255 tree decl = lookup_label_for_goto (loc, label);
9256 if (!decl)
9257 return NULL_TREE;
9258 TREE_USED (decl) = 1;
9260 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9261 SET_EXPR_LOCATION (t, loc);
9262 return add_stmt (t);
9266 /* Generate a computed goto statement to EXPR. LOC is the location of
9267 the GOTO. */
9269 tree
9270 c_finish_goto_ptr (location_t loc, tree expr)
9272 tree t;
9273 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9274 expr = c_fully_fold (expr, false, NULL);
9275 expr = convert (ptr_type_node, expr);
9276 t = build1 (GOTO_EXPR, void_type_node, expr);
9277 SET_EXPR_LOCATION (t, loc);
9278 return add_stmt (t);
9281 /* Generate a C `return' statement. RETVAL is the expression for what
9282 to return, or a null pointer for `return;' with no value. LOC is
9283 the location of the return statement, or the location of the expression,
9284 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9285 is the original type of RETVAL. */
9287 tree
9288 c_finish_return (location_t loc, tree retval, tree origtype)
9290 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9291 bool no_warning = false;
9292 bool npc = false;
9293 size_t rank = 0;
9295 if (TREE_THIS_VOLATILE (current_function_decl))
9296 warning_at (loc, 0,
9297 "function declared %<noreturn%> has a %<return%> statement");
9299 if (flag_cilkplus && contains_array_notation_expr (retval))
9301 /* Array notations are allowed in a return statement if it is inside a
9302 built-in array notation reduction function. */
9303 if (!find_rank (loc, retval, retval, false, &rank))
9304 return error_mark_node;
9305 if (rank >= 1)
9307 error_at (loc, "array notation expression cannot be used as a "
9308 "return value");
9309 return error_mark_node;
9312 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9314 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9315 "allowed");
9316 return error_mark_node;
9318 if (retval)
9320 tree semantic_type = NULL_TREE;
9321 npc = null_pointer_constant_p (retval);
9322 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9324 semantic_type = TREE_TYPE (retval);
9325 retval = TREE_OPERAND (retval, 0);
9327 retval = c_fully_fold (retval, false, NULL);
9328 if (semantic_type)
9329 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9332 if (!retval)
9334 current_function_returns_null = 1;
9335 if ((warn_return_type || flag_isoc99)
9336 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9338 if (flag_isoc99)
9339 pedwarn (loc, 0, "%<return%> with no value, in "
9340 "function returning non-void");
9341 else
9342 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9343 "in function returning non-void");
9344 no_warning = true;
9347 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9349 current_function_returns_null = 1;
9350 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9351 pedwarn (loc, 0,
9352 "%<return%> with a value, in function returning void");
9353 else
9354 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9355 "%<return%> with expression, in function returning void");
9357 else
9359 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9360 retval, origtype, ic_return,
9361 npc, NULL_TREE, NULL_TREE, 0);
9362 tree res = DECL_RESULT (current_function_decl);
9363 tree inner;
9364 bool save;
9366 current_function_returns_value = 1;
9367 if (t == error_mark_node)
9368 return NULL_TREE;
9370 save = in_late_binary_op;
9371 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9372 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9373 in_late_binary_op = true;
9374 inner = t = convert (TREE_TYPE (res), t);
9375 in_late_binary_op = save;
9377 /* Strip any conversions, additions, and subtractions, and see if
9378 we are returning the address of a local variable. Warn if so. */
9379 while (1)
9381 switch (TREE_CODE (inner))
9383 CASE_CONVERT:
9384 case NON_LVALUE_EXPR:
9385 case PLUS_EXPR:
9386 case POINTER_PLUS_EXPR:
9387 inner = TREE_OPERAND (inner, 0);
9388 continue;
9390 case MINUS_EXPR:
9391 /* If the second operand of the MINUS_EXPR has a pointer
9392 type (or is converted from it), this may be valid, so
9393 don't give a warning. */
9395 tree op1 = TREE_OPERAND (inner, 1);
9397 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9398 && (CONVERT_EXPR_P (op1)
9399 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9400 op1 = TREE_OPERAND (op1, 0);
9402 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9403 break;
9405 inner = TREE_OPERAND (inner, 0);
9406 continue;
9409 case ADDR_EXPR:
9410 inner = TREE_OPERAND (inner, 0);
9412 while (REFERENCE_CLASS_P (inner)
9413 && TREE_CODE (inner) != INDIRECT_REF)
9414 inner = TREE_OPERAND (inner, 0);
9416 if (DECL_P (inner)
9417 && !DECL_EXTERNAL (inner)
9418 && !TREE_STATIC (inner)
9419 && DECL_CONTEXT (inner) == current_function_decl)
9421 if (TREE_CODE (inner) == LABEL_DECL)
9422 warning_at (loc, OPT_Wreturn_local_addr,
9423 "function returns address of label");
9424 else
9426 warning_at (loc, OPT_Wreturn_local_addr,
9427 "function returns address of local variable");
9428 tree zero = build_zero_cst (TREE_TYPE (res));
9429 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9432 break;
9434 default:
9435 break;
9438 break;
9441 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9442 SET_EXPR_LOCATION (retval, loc);
9444 if (warn_sequence_point)
9445 verify_sequence_points (retval);
9448 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9449 TREE_NO_WARNING (ret_stmt) |= no_warning;
9450 return add_stmt (ret_stmt);
9453 struct c_switch {
9454 /* The SWITCH_EXPR being built. */
9455 tree switch_expr;
9457 /* The original type of the testing expression, i.e. before the
9458 default conversion is applied. */
9459 tree orig_type;
9461 /* A splay-tree mapping the low element of a case range to the high
9462 element, or NULL_TREE if there is no high element. Used to
9463 determine whether or not a new case label duplicates an old case
9464 label. We need a tree, rather than simply a hash table, because
9465 of the GNU case range extension. */
9466 splay_tree cases;
9468 /* The bindings at the point of the switch. This is used for
9469 warnings crossing decls when branching to a case label. */
9470 struct c_spot_bindings *bindings;
9472 /* The next node on the stack. */
9473 struct c_switch *next;
9476 /* A stack of the currently active switch statements. The innermost
9477 switch statement is on the top of the stack. There is no need to
9478 mark the stack for garbage collection because it is only active
9479 during the processing of the body of a function, and we never
9480 collect at that point. */
9482 struct c_switch *c_switch_stack;
9484 /* Start a C switch statement, testing expression EXP. Return the new
9485 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9486 SWITCH_COND_LOC is the location of the switch's condition.
9487 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9489 tree
9490 c_start_case (location_t switch_loc,
9491 location_t switch_cond_loc,
9492 tree exp, bool explicit_cast_p)
9494 tree orig_type = error_mark_node;
9495 struct c_switch *cs;
9497 if (exp != error_mark_node)
9499 orig_type = TREE_TYPE (exp);
9501 if (!INTEGRAL_TYPE_P (orig_type))
9503 if (orig_type != error_mark_node)
9505 error_at (switch_cond_loc, "switch quantity not an integer");
9506 orig_type = error_mark_node;
9508 exp = integer_zero_node;
9510 else
9512 tree type = TYPE_MAIN_VARIANT (orig_type);
9513 tree e = exp;
9515 /* Warn if the condition has boolean value. */
9516 while (TREE_CODE (e) == COMPOUND_EXPR)
9517 e = TREE_OPERAND (e, 1);
9519 if ((TREE_CODE (type) == BOOLEAN_TYPE
9520 || truth_value_p (TREE_CODE (e)))
9521 /* Explicit cast to int suppresses this warning. */
9522 && !(TREE_CODE (type) == INTEGER_TYPE
9523 && explicit_cast_p))
9524 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9525 "switch condition has boolean value");
9527 if (!in_system_header_at (input_location)
9528 && (type == long_integer_type_node
9529 || type == long_unsigned_type_node))
9530 warning_at (switch_cond_loc,
9531 OPT_Wtraditional, "%<long%> switch expression not "
9532 "converted to %<int%> in ISO C");
9534 exp = c_fully_fold (exp, false, NULL);
9535 exp = default_conversion (exp);
9537 if (warn_sequence_point)
9538 verify_sequence_points (exp);
9542 /* Add this new SWITCH_EXPR to the stack. */
9543 cs = XNEW (struct c_switch);
9544 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9545 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9546 cs->orig_type = orig_type;
9547 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9548 cs->bindings = c_get_switch_bindings ();
9549 cs->next = c_switch_stack;
9550 c_switch_stack = cs;
9552 return add_stmt (cs->switch_expr);
9555 /* Process a case label at location LOC. */
9557 tree
9558 do_case (location_t loc, tree low_value, tree high_value)
9560 tree label = NULL_TREE;
9562 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9564 low_value = c_fully_fold (low_value, false, NULL);
9565 if (TREE_CODE (low_value) == INTEGER_CST)
9566 pedwarn (loc, OPT_Wpedantic,
9567 "case label is not an integer constant expression");
9570 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9572 high_value = c_fully_fold (high_value, false, NULL);
9573 if (TREE_CODE (high_value) == INTEGER_CST)
9574 pedwarn (input_location, OPT_Wpedantic,
9575 "case label is not an integer constant expression");
9578 if (c_switch_stack == NULL)
9580 if (low_value)
9581 error_at (loc, "case label not within a switch statement");
9582 else
9583 error_at (loc, "%<default%> label not within a switch statement");
9584 return NULL_TREE;
9587 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9588 EXPR_LOCATION (c_switch_stack->switch_expr),
9589 loc))
9590 return NULL_TREE;
9592 label = c_add_case_label (loc, c_switch_stack->cases,
9593 SWITCH_COND (c_switch_stack->switch_expr),
9594 c_switch_stack->orig_type,
9595 low_value, high_value);
9596 if (label == error_mark_node)
9597 label = NULL_TREE;
9598 return label;
9601 /* Finish the switch statement. TYPE is the original type of the
9602 controlling expression of the switch, or NULL_TREE. */
9604 void
9605 c_finish_case (tree body, tree type)
9607 struct c_switch *cs = c_switch_stack;
9608 location_t switch_location;
9610 SWITCH_BODY (cs->switch_expr) = body;
9612 /* Emit warnings as needed. */
9613 switch_location = EXPR_LOCATION (cs->switch_expr);
9614 c_do_switch_warnings (cs->cases, switch_location,
9615 type ? type : TREE_TYPE (cs->switch_expr),
9616 SWITCH_COND (cs->switch_expr));
9618 /* Pop the stack. */
9619 c_switch_stack = cs->next;
9620 splay_tree_delete (cs->cases);
9621 c_release_switch_bindings (cs->bindings);
9622 XDELETE (cs);
9625 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9626 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9627 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9628 statement, and was not surrounded with parenthesis. */
9630 void
9631 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9632 tree else_block, bool nested_if)
9634 tree stmt;
9636 /* If the condition has array notations, then the rank of the then_block and
9637 else_block must be either 0 or be equal to the rank of the condition. If
9638 the condition does not have array notations then break them up as it is
9639 broken up in a normal expression. */
9640 if (flag_cilkplus && contains_array_notation_expr (cond))
9642 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9643 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9644 return;
9645 if (then_block
9646 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9647 return;
9648 if (else_block
9649 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9650 return;
9651 if (cond_rank != then_rank && then_rank != 0)
9653 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9654 " and the then-block");
9655 return;
9657 else if (cond_rank != else_rank && else_rank != 0)
9659 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9660 " and the else-block");
9661 return;
9664 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9665 if (warn_parentheses && nested_if && else_block == NULL)
9667 tree inner_if = then_block;
9669 /* We know from the grammar productions that there is an IF nested
9670 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9671 it might not be exactly THEN_BLOCK, but should be the last
9672 non-container statement within. */
9673 while (1)
9674 switch (TREE_CODE (inner_if))
9676 case COND_EXPR:
9677 goto found;
9678 case BIND_EXPR:
9679 inner_if = BIND_EXPR_BODY (inner_if);
9680 break;
9681 case STATEMENT_LIST:
9682 inner_if = expr_last (then_block);
9683 break;
9684 case TRY_FINALLY_EXPR:
9685 case TRY_CATCH_EXPR:
9686 inner_if = TREE_OPERAND (inner_if, 0);
9687 break;
9688 default:
9689 gcc_unreachable ();
9691 found:
9693 if (COND_EXPR_ELSE (inner_if))
9694 warning_at (if_locus, OPT_Wparentheses,
9695 "suggest explicit braces to avoid ambiguous %<else%>");
9698 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9699 SET_EXPR_LOCATION (stmt, if_locus);
9700 add_stmt (stmt);
9703 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9704 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9705 is false for DO loops. INCR is the FOR increment expression. BODY is
9706 the statement controlled by the loop. BLAB is the break label. CLAB is
9707 the continue label. Everything is allowed to be NULL. */
9709 void
9710 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9711 tree blab, tree clab, bool cond_is_first)
9713 tree entry = NULL, exit = NULL, t;
9715 /* In theory could forbid cilk spawn for loop increment expression,
9716 but it should work just fine. */
9718 /* If the condition is zero don't generate a loop construct. */
9719 if (cond && integer_zerop (cond))
9721 if (cond_is_first)
9723 t = build_and_jump (&blab);
9724 SET_EXPR_LOCATION (t, start_locus);
9725 add_stmt (t);
9728 else
9730 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9732 /* If we have an exit condition, then we build an IF with gotos either
9733 out of the loop, or to the top of it. If there's no exit condition,
9734 then we just build a jump back to the top. */
9735 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9737 if (cond && !integer_nonzerop (cond))
9739 /* Canonicalize the loop condition to the end. This means
9740 generating a branch to the loop condition. Reuse the
9741 continue label, if possible. */
9742 if (cond_is_first)
9744 if (incr || !clab)
9746 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9747 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9749 else
9750 t = build1 (GOTO_EXPR, void_type_node, clab);
9751 SET_EXPR_LOCATION (t, start_locus);
9752 add_stmt (t);
9755 t = build_and_jump (&blab);
9756 if (cond_is_first)
9757 exit = fold_build3_loc (start_locus,
9758 COND_EXPR, void_type_node, cond, exit, t);
9759 else
9760 exit = fold_build3_loc (input_location,
9761 COND_EXPR, void_type_node, cond, exit, t);
9764 add_stmt (top);
9767 if (body)
9768 add_stmt (body);
9769 if (clab)
9770 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9771 if (incr)
9772 add_stmt (incr);
9773 if (entry)
9774 add_stmt (entry);
9775 if (exit)
9776 add_stmt (exit);
9777 if (blab)
9778 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9781 tree
9782 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9784 bool skip;
9785 tree label = *label_p;
9787 /* In switch statements break is sometimes stylistically used after
9788 a return statement. This can lead to spurious warnings about
9789 control reaching the end of a non-void function when it is
9790 inlined. Note that we are calling block_may_fallthru with
9791 language specific tree nodes; this works because
9792 block_may_fallthru returns true when given something it does not
9793 understand. */
9794 skip = !block_may_fallthru (cur_stmt_list);
9796 if (!label)
9798 if (!skip)
9799 *label_p = label = create_artificial_label (loc);
9801 else if (TREE_CODE (label) == LABEL_DECL)
9803 else switch (TREE_INT_CST_LOW (label))
9805 case 0:
9806 if (is_break)
9807 error_at (loc, "break statement not within loop or switch");
9808 else
9809 error_at (loc, "continue statement not within a loop");
9810 return NULL_TREE;
9812 case 1:
9813 gcc_assert (is_break);
9814 error_at (loc, "break statement used with OpenMP for loop");
9815 return NULL_TREE;
9817 case 2:
9818 if (is_break)
9819 error ("break statement within %<#pragma simd%> loop body");
9820 else
9821 error ("continue statement within %<#pragma simd%> loop body");
9822 return NULL_TREE;
9824 default:
9825 gcc_unreachable ();
9828 if (skip)
9829 return NULL_TREE;
9831 if (!is_break)
9832 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9834 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9837 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9839 static void
9840 emit_side_effect_warnings (location_t loc, tree expr)
9842 if (expr == error_mark_node)
9844 else if (!TREE_SIDE_EFFECTS (expr))
9846 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9847 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9849 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9851 tree r = expr;
9852 location_t cloc = loc;
9853 while (TREE_CODE (r) == COMPOUND_EXPR)
9855 if (EXPR_HAS_LOCATION (r))
9856 cloc = EXPR_LOCATION (r);
9857 r = TREE_OPERAND (r, 1);
9859 if (!TREE_SIDE_EFFECTS (r)
9860 && !VOID_TYPE_P (TREE_TYPE (r))
9861 && !CONVERT_EXPR_P (r)
9862 && !TREE_NO_WARNING (r)
9863 && !TREE_NO_WARNING (expr))
9864 warning_at (cloc, OPT_Wunused_value,
9865 "right-hand operand of comma expression has no effect");
9867 else
9868 warn_if_unused_value (expr, loc);
9871 /* Process an expression as if it were a complete statement. Emit
9872 diagnostics, but do not call ADD_STMT. LOC is the location of the
9873 statement. */
9875 tree
9876 c_process_expr_stmt (location_t loc, tree expr)
9878 tree exprv;
9880 if (!expr)
9881 return NULL_TREE;
9883 expr = c_fully_fold (expr, false, NULL);
9885 if (warn_sequence_point)
9886 verify_sequence_points (expr);
9888 if (TREE_TYPE (expr) != error_mark_node
9889 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9890 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9891 error_at (loc, "expression statement has incomplete type");
9893 /* If we're not processing a statement expression, warn about unused values.
9894 Warnings for statement expressions will be emitted later, once we figure
9895 out which is the result. */
9896 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9897 && warn_unused_value)
9898 emit_side_effect_warnings (loc, expr);
9900 exprv = expr;
9901 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9902 exprv = TREE_OPERAND (exprv, 1);
9903 while (CONVERT_EXPR_P (exprv))
9904 exprv = TREE_OPERAND (exprv, 0);
9905 if (DECL_P (exprv)
9906 || handled_component_p (exprv)
9907 || TREE_CODE (exprv) == ADDR_EXPR)
9908 mark_exp_read (exprv);
9910 /* If the expression is not of a type to which we cannot assign a line
9911 number, wrap the thing in a no-op NOP_EXPR. */
9912 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9914 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9915 SET_EXPR_LOCATION (expr, loc);
9918 return expr;
9921 /* Emit an expression as a statement. LOC is the location of the
9922 expression. */
9924 tree
9925 c_finish_expr_stmt (location_t loc, tree expr)
9927 if (expr)
9928 return add_stmt (c_process_expr_stmt (loc, expr));
9929 else
9930 return NULL;
9933 /* Do the opposite and emit a statement as an expression. To begin,
9934 create a new binding level and return it. */
9936 tree
9937 c_begin_stmt_expr (void)
9939 tree ret;
9941 /* We must force a BLOCK for this level so that, if it is not expanded
9942 later, there is a way to turn off the entire subtree of blocks that
9943 are contained in it. */
9944 keep_next_level ();
9945 ret = c_begin_compound_stmt (true);
9947 c_bindings_start_stmt_expr (c_switch_stack == NULL
9948 ? NULL
9949 : c_switch_stack->bindings);
9951 /* Mark the current statement list as belonging to a statement list. */
9952 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9954 return ret;
9957 /* LOC is the location of the compound statement to which this body
9958 belongs. */
9960 tree
9961 c_finish_stmt_expr (location_t loc, tree body)
9963 tree last, type, tmp, val;
9964 tree *last_p;
9966 body = c_end_compound_stmt (loc, body, true);
9968 c_bindings_end_stmt_expr (c_switch_stack == NULL
9969 ? NULL
9970 : c_switch_stack->bindings);
9972 /* Locate the last statement in BODY. See c_end_compound_stmt
9973 about always returning a BIND_EXPR. */
9974 last_p = &BIND_EXPR_BODY (body);
9975 last = BIND_EXPR_BODY (body);
9977 continue_searching:
9978 if (TREE_CODE (last) == STATEMENT_LIST)
9980 tree_stmt_iterator i;
9982 /* This can happen with degenerate cases like ({ }). No value. */
9983 if (!TREE_SIDE_EFFECTS (last))
9984 return body;
9986 /* If we're supposed to generate side effects warnings, process
9987 all of the statements except the last. */
9988 if (warn_unused_value)
9990 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9992 location_t tloc;
9993 tree t = tsi_stmt (i);
9995 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9996 emit_side_effect_warnings (tloc, t);
9999 else
10000 i = tsi_last (last);
10001 last_p = tsi_stmt_ptr (i);
10002 last = *last_p;
10005 /* If the end of the list is exception related, then the list was split
10006 by a call to push_cleanup. Continue searching. */
10007 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10008 || TREE_CODE (last) == TRY_CATCH_EXPR)
10010 last_p = &TREE_OPERAND (last, 0);
10011 last = *last_p;
10012 goto continue_searching;
10015 if (last == error_mark_node)
10016 return last;
10018 /* In the case that the BIND_EXPR is not necessary, return the
10019 expression out from inside it. */
10020 if (last == BIND_EXPR_BODY (body)
10021 && BIND_EXPR_VARS (body) == NULL)
10023 /* Even if this looks constant, do not allow it in a constant
10024 expression. */
10025 last = c_wrap_maybe_const (last, true);
10026 /* Do not warn if the return value of a statement expression is
10027 unused. */
10028 TREE_NO_WARNING (last) = 1;
10029 return last;
10032 /* Extract the type of said expression. */
10033 type = TREE_TYPE (last);
10035 /* If we're not returning a value at all, then the BIND_EXPR that
10036 we already have is a fine expression to return. */
10037 if (!type || VOID_TYPE_P (type))
10038 return body;
10040 /* Now that we've located the expression containing the value, it seems
10041 silly to make voidify_wrapper_expr repeat the process. Create a
10042 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10043 tmp = create_tmp_var_raw (type);
10045 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10046 tree_expr_nonnegative_p giving up immediately. */
10047 val = last;
10048 if (TREE_CODE (val) == NOP_EXPR
10049 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10050 val = TREE_OPERAND (val, 0);
10052 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10053 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10056 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10057 SET_EXPR_LOCATION (t, loc);
10058 return t;
10062 /* Begin and end compound statements. This is as simple as pushing
10063 and popping new statement lists from the tree. */
10065 tree
10066 c_begin_compound_stmt (bool do_scope)
10068 tree stmt = push_stmt_list ();
10069 if (do_scope)
10070 push_scope ();
10071 return stmt;
10074 /* End a compound statement. STMT is the statement. LOC is the
10075 location of the compound statement-- this is usually the location
10076 of the opening brace. */
10078 tree
10079 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10081 tree block = NULL;
10083 if (do_scope)
10085 if (c_dialect_objc ())
10086 objc_clear_super_receiver ();
10087 block = pop_scope ();
10090 stmt = pop_stmt_list (stmt);
10091 stmt = c_build_bind_expr (loc, block, stmt);
10093 /* If this compound statement is nested immediately inside a statement
10094 expression, then force a BIND_EXPR to be created. Otherwise we'll
10095 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10096 STATEMENT_LISTs merge, and thus we can lose track of what statement
10097 was really last. */
10098 if (building_stmt_list_p ()
10099 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10100 && TREE_CODE (stmt) != BIND_EXPR)
10102 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10103 TREE_SIDE_EFFECTS (stmt) = 1;
10104 SET_EXPR_LOCATION (stmt, loc);
10107 return stmt;
10110 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10111 when the current scope is exited. EH_ONLY is true when this is not
10112 meant to apply to normal control flow transfer. */
10114 void
10115 push_cleanup (tree decl, tree cleanup, bool eh_only)
10117 enum tree_code code;
10118 tree stmt, list;
10119 bool stmt_expr;
10121 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10122 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10123 add_stmt (stmt);
10124 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10125 list = push_stmt_list ();
10126 TREE_OPERAND (stmt, 0) = list;
10127 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10130 /* Build a binary-operation expression without default conversions.
10131 CODE is the kind of expression to build.
10132 LOCATION is the operator's location.
10133 This function differs from `build' in several ways:
10134 the data type of the result is computed and recorded in it,
10135 warnings are generated if arg data types are invalid,
10136 special handling for addition and subtraction of pointers is known,
10137 and some optimization is done (operations on narrow ints
10138 are done in the narrower type when that gives the same result).
10139 Constant folding is also done before the result is returned.
10141 Note that the operands will never have enumeral types, or function
10142 or array types, because either they will have the default conversions
10143 performed or they have both just been converted to some other type in which
10144 the arithmetic is to be done. */
10146 tree
10147 build_binary_op (location_t location, enum tree_code code,
10148 tree orig_op0, tree orig_op1, int convert_p)
10150 tree type0, type1, orig_type0, orig_type1;
10151 tree eptype;
10152 enum tree_code code0, code1;
10153 tree op0, op1;
10154 tree ret = error_mark_node;
10155 const char *invalid_op_diag;
10156 bool op0_int_operands, op1_int_operands;
10157 bool int_const, int_const_or_overflow, int_operands;
10159 /* Expression code to give to the expression when it is built.
10160 Normally this is CODE, which is what the caller asked for,
10161 but in some special cases we change it. */
10162 enum tree_code resultcode = code;
10164 /* Data type in which the computation is to be performed.
10165 In the simplest cases this is the common type of the arguments. */
10166 tree result_type = NULL;
10168 /* When the computation is in excess precision, the type of the
10169 final EXCESS_PRECISION_EXPR. */
10170 tree semantic_result_type = NULL;
10172 /* Nonzero means operands have already been type-converted
10173 in whatever way is necessary.
10174 Zero means they need to be converted to RESULT_TYPE. */
10175 int converted = 0;
10177 /* Nonzero means create the expression with this type, rather than
10178 RESULT_TYPE. */
10179 tree build_type = 0;
10181 /* Nonzero means after finally constructing the expression
10182 convert it to this type. */
10183 tree final_type = 0;
10185 /* Nonzero if this is an operation like MIN or MAX which can
10186 safely be computed in short if both args are promoted shorts.
10187 Also implies COMMON.
10188 -1 indicates a bitwise operation; this makes a difference
10189 in the exact conditions for when it is safe to do the operation
10190 in a narrower mode. */
10191 int shorten = 0;
10193 /* Nonzero if this is a comparison operation;
10194 if both args are promoted shorts, compare the original shorts.
10195 Also implies COMMON. */
10196 int short_compare = 0;
10198 /* Nonzero if this is a right-shift operation, which can be computed on the
10199 original short and then promoted if the operand is a promoted short. */
10200 int short_shift = 0;
10202 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10203 int common = 0;
10205 /* True means types are compatible as far as ObjC is concerned. */
10206 bool objc_ok;
10208 /* True means this is an arithmetic operation that may need excess
10209 precision. */
10210 bool may_need_excess_precision;
10212 /* True means this is a boolean operation that converts both its
10213 operands to truth-values. */
10214 bool boolean_op = false;
10216 /* Remember whether we're doing / or %. */
10217 bool doing_div_or_mod = false;
10219 /* Remember whether we're doing << or >>. */
10220 bool doing_shift = false;
10222 /* Tree holding instrumentation expression. */
10223 tree instrument_expr = NULL;
10225 if (location == UNKNOWN_LOCATION)
10226 location = input_location;
10228 op0 = orig_op0;
10229 op1 = orig_op1;
10231 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10232 if (op0_int_operands)
10233 op0 = remove_c_maybe_const_expr (op0);
10234 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10235 if (op1_int_operands)
10236 op1 = remove_c_maybe_const_expr (op1);
10237 int_operands = (op0_int_operands && op1_int_operands);
10238 if (int_operands)
10240 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10241 && TREE_CODE (orig_op1) == INTEGER_CST);
10242 int_const = (int_const_or_overflow
10243 && !TREE_OVERFLOW (orig_op0)
10244 && !TREE_OVERFLOW (orig_op1));
10246 else
10247 int_const = int_const_or_overflow = false;
10249 /* Do not apply default conversion in mixed vector/scalar expression. */
10250 if (convert_p
10251 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10252 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10254 op0 = default_conversion (op0);
10255 op1 = default_conversion (op1);
10258 /* When Cilk Plus is enabled and there are array notations inside op0, then
10259 we check to see if there are builtin array notation functions. If
10260 so, then we take on the type of the array notation inside it. */
10261 if (flag_cilkplus && contains_array_notation_expr (op0))
10262 orig_type0 = type0 = find_correct_array_notation_type (op0);
10263 else
10264 orig_type0 = type0 = TREE_TYPE (op0);
10266 if (flag_cilkplus && contains_array_notation_expr (op1))
10267 orig_type1 = type1 = find_correct_array_notation_type (op1);
10268 else
10269 orig_type1 = type1 = TREE_TYPE (op1);
10271 /* The expression codes of the data types of the arguments tell us
10272 whether the arguments are integers, floating, pointers, etc. */
10273 code0 = TREE_CODE (type0);
10274 code1 = TREE_CODE (type1);
10276 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10277 STRIP_TYPE_NOPS (op0);
10278 STRIP_TYPE_NOPS (op1);
10280 /* If an error was already reported for one of the arguments,
10281 avoid reporting another error. */
10283 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10284 return error_mark_node;
10286 if ((invalid_op_diag
10287 = targetm.invalid_binary_op (code, type0, type1)))
10289 error_at (location, invalid_op_diag);
10290 return error_mark_node;
10293 switch (code)
10295 case PLUS_EXPR:
10296 case MINUS_EXPR:
10297 case MULT_EXPR:
10298 case TRUNC_DIV_EXPR:
10299 case CEIL_DIV_EXPR:
10300 case FLOOR_DIV_EXPR:
10301 case ROUND_DIV_EXPR:
10302 case EXACT_DIV_EXPR:
10303 may_need_excess_precision = true;
10304 break;
10305 default:
10306 may_need_excess_precision = false;
10307 break;
10309 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10311 op0 = TREE_OPERAND (op0, 0);
10312 type0 = TREE_TYPE (op0);
10314 else if (may_need_excess_precision
10315 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10317 type0 = eptype;
10318 op0 = convert (eptype, op0);
10320 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10322 op1 = TREE_OPERAND (op1, 0);
10323 type1 = TREE_TYPE (op1);
10325 else if (may_need_excess_precision
10326 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10328 type1 = eptype;
10329 op1 = convert (eptype, op1);
10332 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10334 /* In case when one of the operands of the binary operation is
10335 a vector and another is a scalar -- convert scalar to vector. */
10336 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10338 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10339 true);
10341 switch (convert_flag)
10343 case stv_error:
10344 return error_mark_node;
10345 case stv_firstarg:
10347 bool maybe_const = true;
10348 tree sc;
10349 sc = c_fully_fold (op0, false, &maybe_const);
10350 sc = save_expr (sc);
10351 sc = convert (TREE_TYPE (type1), sc);
10352 op0 = build_vector_from_val (type1, sc);
10353 if (!maybe_const)
10354 op0 = c_wrap_maybe_const (op0, true);
10355 orig_type0 = type0 = TREE_TYPE (op0);
10356 code0 = TREE_CODE (type0);
10357 converted = 1;
10358 break;
10360 case stv_secondarg:
10362 bool maybe_const = true;
10363 tree sc;
10364 sc = c_fully_fold (op1, false, &maybe_const);
10365 sc = save_expr (sc);
10366 sc = convert (TREE_TYPE (type0), sc);
10367 op1 = build_vector_from_val (type0, sc);
10368 if (!maybe_const)
10369 op1 = c_wrap_maybe_const (op1, true);
10370 orig_type1 = type1 = TREE_TYPE (op1);
10371 code1 = TREE_CODE (type1);
10372 converted = 1;
10373 break;
10375 default:
10376 break;
10380 switch (code)
10382 case PLUS_EXPR:
10383 /* Handle the pointer + int case. */
10384 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10386 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10387 goto return_build_binary_op;
10389 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10391 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10392 goto return_build_binary_op;
10394 else
10395 common = 1;
10396 break;
10398 case MINUS_EXPR:
10399 /* Subtraction of two similar pointers.
10400 We must subtract them as integers, then divide by object size. */
10401 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10402 && comp_target_types (location, type0, type1))
10404 ret = pointer_diff (location, op0, op1);
10405 goto return_build_binary_op;
10407 /* Handle pointer minus int. Just like pointer plus int. */
10408 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10410 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10411 goto return_build_binary_op;
10413 else
10414 common = 1;
10415 break;
10417 case MULT_EXPR:
10418 common = 1;
10419 break;
10421 case TRUNC_DIV_EXPR:
10422 case CEIL_DIV_EXPR:
10423 case FLOOR_DIV_EXPR:
10424 case ROUND_DIV_EXPR:
10425 case EXACT_DIV_EXPR:
10426 doing_div_or_mod = true;
10427 warn_for_div_by_zero (location, op1);
10429 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10430 || code0 == FIXED_POINT_TYPE
10431 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10432 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10433 || code1 == FIXED_POINT_TYPE
10434 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10436 enum tree_code tcode0 = code0, tcode1 = code1;
10438 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10439 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10440 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10441 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10443 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10444 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10445 resultcode = RDIV_EXPR;
10446 else
10447 /* Although it would be tempting to shorten always here, that
10448 loses on some targets, since the modulo instruction is
10449 undefined if the quotient can't be represented in the
10450 computation mode. We shorten only if unsigned or if
10451 dividing by something we know != -1. */
10452 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10453 || (TREE_CODE (op1) == INTEGER_CST
10454 && !integer_all_onesp (op1)));
10455 common = 1;
10457 break;
10459 case BIT_AND_EXPR:
10460 case BIT_IOR_EXPR:
10461 case BIT_XOR_EXPR:
10462 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10463 shorten = -1;
10464 /* Allow vector types which are not floating point types. */
10465 else if (code0 == VECTOR_TYPE
10466 && code1 == VECTOR_TYPE
10467 && !VECTOR_FLOAT_TYPE_P (type0)
10468 && !VECTOR_FLOAT_TYPE_P (type1))
10469 common = 1;
10470 break;
10472 case TRUNC_MOD_EXPR:
10473 case FLOOR_MOD_EXPR:
10474 doing_div_or_mod = true;
10475 warn_for_div_by_zero (location, op1);
10477 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10478 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10479 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10480 common = 1;
10481 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10483 /* Although it would be tempting to shorten always here, that loses
10484 on some targets, since the modulo instruction is undefined if the
10485 quotient can't be represented in the computation mode. We shorten
10486 only if unsigned or if dividing by something we know != -1. */
10487 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10488 || (TREE_CODE (op1) == INTEGER_CST
10489 && !integer_all_onesp (op1)));
10490 common = 1;
10492 break;
10494 case TRUTH_ANDIF_EXPR:
10495 case TRUTH_ORIF_EXPR:
10496 case TRUTH_AND_EXPR:
10497 case TRUTH_OR_EXPR:
10498 case TRUTH_XOR_EXPR:
10499 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10500 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10501 || code0 == FIXED_POINT_TYPE)
10502 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10503 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10504 || code1 == FIXED_POINT_TYPE))
10506 /* Result of these operations is always an int,
10507 but that does not mean the operands should be
10508 converted to ints! */
10509 result_type = integer_type_node;
10510 if (op0_int_operands)
10512 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10513 op0 = remove_c_maybe_const_expr (op0);
10515 else
10516 op0 = c_objc_common_truthvalue_conversion (location, op0);
10517 if (op1_int_operands)
10519 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10520 op1 = remove_c_maybe_const_expr (op1);
10522 else
10523 op1 = c_objc_common_truthvalue_conversion (location, op1);
10524 converted = 1;
10525 boolean_op = true;
10527 if (code == TRUTH_ANDIF_EXPR)
10529 int_const_or_overflow = (int_operands
10530 && TREE_CODE (orig_op0) == INTEGER_CST
10531 && (op0 == truthvalue_false_node
10532 || TREE_CODE (orig_op1) == INTEGER_CST));
10533 int_const = (int_const_or_overflow
10534 && !TREE_OVERFLOW (orig_op0)
10535 && (op0 == truthvalue_false_node
10536 || !TREE_OVERFLOW (orig_op1)));
10538 else if (code == TRUTH_ORIF_EXPR)
10540 int_const_or_overflow = (int_operands
10541 && TREE_CODE (orig_op0) == INTEGER_CST
10542 && (op0 == truthvalue_true_node
10543 || TREE_CODE (orig_op1) == INTEGER_CST));
10544 int_const = (int_const_or_overflow
10545 && !TREE_OVERFLOW (orig_op0)
10546 && (op0 == truthvalue_true_node
10547 || !TREE_OVERFLOW (orig_op1)));
10549 break;
10551 /* Shift operations: result has same type as first operand;
10552 always convert second operand to int.
10553 Also set SHORT_SHIFT if shifting rightward. */
10555 case RSHIFT_EXPR:
10556 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10557 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10559 result_type = type0;
10560 converted = 1;
10562 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10563 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10564 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10565 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10567 result_type = type0;
10568 converted = 1;
10570 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10571 && code1 == INTEGER_TYPE)
10573 doing_shift = true;
10574 if (TREE_CODE (op1) == INTEGER_CST)
10576 if (tree_int_cst_sgn (op1) < 0)
10578 int_const = false;
10579 if (c_inhibit_evaluation_warnings == 0)
10580 warning_at (location, OPT_Wshift_count_negative,
10581 "right shift count is negative");
10583 else
10585 if (!integer_zerop (op1))
10586 short_shift = 1;
10588 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10590 int_const = false;
10591 if (c_inhibit_evaluation_warnings == 0)
10592 warning_at (location, OPT_Wshift_count_overflow,
10593 "right shift count >= width of type");
10598 /* Use the type of the value to be shifted. */
10599 result_type = type0;
10600 /* Avoid converting op1 to result_type later. */
10601 converted = 1;
10603 break;
10605 case LSHIFT_EXPR:
10606 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10607 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10609 result_type = type0;
10610 converted = 1;
10612 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10613 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10614 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10615 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10617 result_type = type0;
10618 converted = 1;
10620 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10621 && code1 == INTEGER_TYPE)
10623 doing_shift = true;
10624 if (TREE_CODE (op1) == INTEGER_CST)
10626 if (tree_int_cst_sgn (op1) < 0)
10628 int_const = false;
10629 if (c_inhibit_evaluation_warnings == 0)
10630 warning_at (location, OPT_Wshift_count_negative,
10631 "left shift count is negative");
10634 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10636 int_const = false;
10637 if (c_inhibit_evaluation_warnings == 0)
10638 warning_at (location, OPT_Wshift_count_overflow,
10639 "left shift count >= width of type");
10643 /* Use the type of the value to be shifted. */
10644 result_type = type0;
10645 /* Avoid converting op1 to result_type later. */
10646 converted = 1;
10648 break;
10650 case EQ_EXPR:
10651 case NE_EXPR:
10652 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10654 tree intt;
10655 if (!vector_types_compatible_elements_p (type0, type1))
10657 error_at (location, "comparing vectors with different "
10658 "element types");
10659 return error_mark_node;
10662 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10664 error_at (location, "comparing vectors with different "
10665 "number of elements");
10666 return error_mark_node;
10669 /* Always construct signed integer vector type. */
10670 intt = c_common_type_for_size (GET_MODE_BITSIZE
10671 (TYPE_MODE (TREE_TYPE (type0))), 0);
10672 result_type = build_opaque_vector_type (intt,
10673 TYPE_VECTOR_SUBPARTS (type0));
10674 converted = 1;
10675 break;
10677 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10678 warning_at (location,
10679 OPT_Wfloat_equal,
10680 "comparing floating point with == or != is unsafe");
10681 /* Result of comparison is always int,
10682 but don't convert the args to int! */
10683 build_type = integer_type_node;
10684 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10685 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10686 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10687 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10688 short_compare = 1;
10689 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10691 if (TREE_CODE (op0) == ADDR_EXPR
10692 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10694 if (code == EQ_EXPR)
10695 warning_at (location,
10696 OPT_Waddress,
10697 "the comparison will always evaluate as %<false%> "
10698 "for the address of %qD will never be NULL",
10699 TREE_OPERAND (op0, 0));
10700 else
10701 warning_at (location,
10702 OPT_Waddress,
10703 "the comparison will always evaluate as %<true%> "
10704 "for the address of %qD will never be NULL",
10705 TREE_OPERAND (op0, 0));
10707 result_type = type0;
10709 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10711 if (TREE_CODE (op1) == ADDR_EXPR
10712 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10714 if (code == EQ_EXPR)
10715 warning_at (location,
10716 OPT_Waddress,
10717 "the comparison will always evaluate as %<false%> "
10718 "for the address of %qD will never be NULL",
10719 TREE_OPERAND (op1, 0));
10720 else
10721 warning_at (location,
10722 OPT_Waddress,
10723 "the comparison will always evaluate as %<true%> "
10724 "for the address of %qD will never be NULL",
10725 TREE_OPERAND (op1, 0));
10727 result_type = type1;
10729 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10731 tree tt0 = TREE_TYPE (type0);
10732 tree tt1 = TREE_TYPE (type1);
10733 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10734 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10735 addr_space_t as_common = ADDR_SPACE_GENERIC;
10737 /* Anything compares with void *. void * compares with anything.
10738 Otherwise, the targets must be compatible
10739 and both must be object or both incomplete. */
10740 if (comp_target_types (location, type0, type1))
10741 result_type = common_pointer_type (type0, type1);
10742 else if (!addr_space_superset (as0, as1, &as_common))
10744 error_at (location, "comparison of pointers to "
10745 "disjoint address spaces");
10746 return error_mark_node;
10748 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10750 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10751 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10752 "comparison of %<void *%> with function pointer");
10754 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10756 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10757 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10758 "comparison of %<void *%> with function pointer");
10760 else
10761 /* Avoid warning about the volatile ObjC EH puts on decls. */
10762 if (!objc_ok)
10763 pedwarn (location, 0,
10764 "comparison of distinct pointer types lacks a cast");
10766 if (result_type == NULL_TREE)
10768 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10769 result_type = build_pointer_type
10770 (build_qualified_type (void_type_node, qual));
10773 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10775 result_type = type0;
10776 pedwarn (location, 0, "comparison between pointer and integer");
10778 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10780 result_type = type1;
10781 pedwarn (location, 0, "comparison between pointer and integer");
10783 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10784 || truth_value_p (TREE_CODE (orig_op0)))
10785 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10786 || truth_value_p (TREE_CODE (orig_op1))))
10787 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10788 break;
10790 case LE_EXPR:
10791 case GE_EXPR:
10792 case LT_EXPR:
10793 case GT_EXPR:
10794 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10796 tree intt;
10797 if (!vector_types_compatible_elements_p (type0, type1))
10799 error_at (location, "comparing vectors with different "
10800 "element types");
10801 return error_mark_node;
10804 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10806 error_at (location, "comparing vectors with different "
10807 "number of elements");
10808 return error_mark_node;
10811 /* Always construct signed integer vector type. */
10812 intt = c_common_type_for_size (GET_MODE_BITSIZE
10813 (TYPE_MODE (TREE_TYPE (type0))), 0);
10814 result_type = build_opaque_vector_type (intt,
10815 TYPE_VECTOR_SUBPARTS (type0));
10816 converted = 1;
10817 break;
10819 build_type = integer_type_node;
10820 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10821 || code0 == FIXED_POINT_TYPE)
10822 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10823 || code1 == FIXED_POINT_TYPE))
10824 short_compare = 1;
10825 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10827 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10828 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10829 addr_space_t as_common;
10831 if (comp_target_types (location, type0, type1))
10833 result_type = common_pointer_type (type0, type1);
10834 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10835 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10836 pedwarn (location, 0,
10837 "comparison of complete and incomplete pointers");
10838 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10839 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10840 "ordered comparisons of pointers to functions");
10841 else if (null_pointer_constant_p (orig_op0)
10842 || null_pointer_constant_p (orig_op1))
10843 warning_at (location, OPT_Wextra,
10844 "ordered comparison of pointer with null pointer");
10847 else if (!addr_space_superset (as0, as1, &as_common))
10849 error_at (location, "comparison of pointers to "
10850 "disjoint address spaces");
10851 return error_mark_node;
10853 else
10855 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10856 result_type = build_pointer_type
10857 (build_qualified_type (void_type_node, qual));
10858 pedwarn (location, 0,
10859 "comparison of distinct pointer types lacks a cast");
10862 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10864 result_type = type0;
10865 if (pedantic)
10866 pedwarn (location, OPT_Wpedantic,
10867 "ordered comparison of pointer with integer zero");
10868 else if (extra_warnings)
10869 warning_at (location, OPT_Wextra,
10870 "ordered comparison of pointer with integer zero");
10872 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10874 result_type = type1;
10875 if (pedantic)
10876 pedwarn (location, OPT_Wpedantic,
10877 "ordered comparison of pointer with integer zero");
10878 else if (extra_warnings)
10879 warning_at (location, OPT_Wextra,
10880 "ordered comparison of pointer with integer zero");
10882 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10884 result_type = type0;
10885 pedwarn (location, 0, "comparison between pointer and integer");
10887 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10889 result_type = type1;
10890 pedwarn (location, 0, "comparison between pointer and integer");
10892 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10893 || truth_value_p (TREE_CODE (orig_op0)))
10894 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10895 || truth_value_p (TREE_CODE (orig_op1))))
10896 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10897 break;
10899 default:
10900 gcc_unreachable ();
10903 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10904 return error_mark_node;
10906 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10907 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10908 || !vector_types_compatible_elements_p (type0, type1)))
10910 binary_op_error (location, code, type0, type1);
10911 return error_mark_node;
10914 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10915 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10917 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10918 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10920 bool first_complex = (code0 == COMPLEX_TYPE);
10921 bool second_complex = (code1 == COMPLEX_TYPE);
10922 int none_complex = (!first_complex && !second_complex);
10924 if (shorten || common || short_compare)
10926 result_type = c_common_type (type0, type1);
10927 do_warn_double_promotion (result_type, type0, type1,
10928 "implicit conversion from %qT to %qT "
10929 "to match other operand of binary "
10930 "expression",
10931 location);
10932 if (result_type == error_mark_node)
10933 return error_mark_node;
10936 if (first_complex != second_complex
10937 && (code == PLUS_EXPR
10938 || code == MINUS_EXPR
10939 || code == MULT_EXPR
10940 || (code == TRUNC_DIV_EXPR && first_complex))
10941 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10942 && flag_signed_zeros)
10944 /* An operation on mixed real/complex operands must be
10945 handled specially, but the language-independent code can
10946 more easily optimize the plain complex arithmetic if
10947 -fno-signed-zeros. */
10948 tree real_type = TREE_TYPE (result_type);
10949 tree real, imag;
10950 if (type0 != orig_type0 || type1 != orig_type1)
10952 gcc_assert (may_need_excess_precision && common);
10953 semantic_result_type = c_common_type (orig_type0, orig_type1);
10955 if (first_complex)
10957 if (TREE_TYPE (op0) != result_type)
10958 op0 = convert_and_check (location, result_type, op0);
10959 if (TREE_TYPE (op1) != real_type)
10960 op1 = convert_and_check (location, real_type, op1);
10962 else
10964 if (TREE_TYPE (op0) != real_type)
10965 op0 = convert_and_check (location, real_type, op0);
10966 if (TREE_TYPE (op1) != result_type)
10967 op1 = convert_and_check (location, result_type, op1);
10969 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10970 return error_mark_node;
10971 if (first_complex)
10973 op0 = c_save_expr (op0);
10974 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10975 op0, 1);
10976 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10977 op0, 1);
10978 switch (code)
10980 case MULT_EXPR:
10981 case TRUNC_DIV_EXPR:
10982 op1 = c_save_expr (op1);
10983 imag = build2 (resultcode, real_type, imag, op1);
10984 /* Fall through. */
10985 case PLUS_EXPR:
10986 case MINUS_EXPR:
10987 real = build2 (resultcode, real_type, real, op1);
10988 break;
10989 default:
10990 gcc_unreachable();
10993 else
10995 op1 = c_save_expr (op1);
10996 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10997 op1, 1);
10998 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10999 op1, 1);
11000 switch (code)
11002 case MULT_EXPR:
11003 op0 = c_save_expr (op0);
11004 imag = build2 (resultcode, real_type, op0, imag);
11005 /* Fall through. */
11006 case PLUS_EXPR:
11007 real = build2 (resultcode, real_type, op0, real);
11008 break;
11009 case MINUS_EXPR:
11010 real = build2 (resultcode, real_type, op0, real);
11011 imag = build1 (NEGATE_EXPR, real_type, imag);
11012 break;
11013 default:
11014 gcc_unreachable();
11017 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11018 goto return_build_binary_op;
11021 /* For certain operations (which identify themselves by shorten != 0)
11022 if both args were extended from the same smaller type,
11023 do the arithmetic in that type and then extend.
11025 shorten !=0 and !=1 indicates a bitwise operation.
11026 For them, this optimization is safe only if
11027 both args are zero-extended or both are sign-extended.
11028 Otherwise, we might change the result.
11029 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11030 but calculated in (unsigned short) it would be (unsigned short)-1. */
11032 if (shorten && none_complex)
11034 final_type = result_type;
11035 result_type = shorten_binary_op (result_type, op0, op1,
11036 shorten == -1);
11039 /* Shifts can be shortened if shifting right. */
11041 if (short_shift)
11043 int unsigned_arg;
11044 tree arg0 = get_narrower (op0, &unsigned_arg);
11046 final_type = result_type;
11048 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11049 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11051 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11052 && tree_int_cst_sgn (op1) > 0
11053 /* We can shorten only if the shift count is less than the
11054 number of bits in the smaller type size. */
11055 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11056 /* We cannot drop an unsigned shift after sign-extension. */
11057 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11059 /* Do an unsigned shift if the operand was zero-extended. */
11060 result_type
11061 = c_common_signed_or_unsigned_type (unsigned_arg,
11062 TREE_TYPE (arg0));
11063 /* Convert value-to-be-shifted to that type. */
11064 if (TREE_TYPE (op0) != result_type)
11065 op0 = convert (result_type, op0);
11066 converted = 1;
11070 /* Comparison operations are shortened too but differently.
11071 They identify themselves by setting short_compare = 1. */
11073 if (short_compare)
11075 /* Don't write &op0, etc., because that would prevent op0
11076 from being kept in a register.
11077 Instead, make copies of the our local variables and
11078 pass the copies by reference, then copy them back afterward. */
11079 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11080 enum tree_code xresultcode = resultcode;
11081 tree val
11082 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11083 &xresultcode);
11085 if (val != 0)
11087 ret = val;
11088 goto return_build_binary_op;
11091 op0 = xop0, op1 = xop1;
11092 converted = 1;
11093 resultcode = xresultcode;
11095 if (c_inhibit_evaluation_warnings == 0)
11097 bool op0_maybe_const = true;
11098 bool op1_maybe_const = true;
11099 tree orig_op0_folded, orig_op1_folded;
11101 if (in_late_binary_op)
11103 orig_op0_folded = orig_op0;
11104 orig_op1_folded = orig_op1;
11106 else
11108 /* Fold for the sake of possible warnings, as in
11109 build_conditional_expr. This requires the
11110 "original" values to be folded, not just op0 and
11111 op1. */
11112 c_inhibit_evaluation_warnings++;
11113 op0 = c_fully_fold (op0, require_constant_value,
11114 &op0_maybe_const);
11115 op1 = c_fully_fold (op1, require_constant_value,
11116 &op1_maybe_const);
11117 c_inhibit_evaluation_warnings--;
11118 orig_op0_folded = c_fully_fold (orig_op0,
11119 require_constant_value,
11120 NULL);
11121 orig_op1_folded = c_fully_fold (orig_op1,
11122 require_constant_value,
11123 NULL);
11126 if (warn_sign_compare)
11127 warn_for_sign_compare (location, orig_op0_folded,
11128 orig_op1_folded, op0, op1,
11129 result_type, resultcode);
11130 if (!in_late_binary_op && !int_operands)
11132 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11133 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11134 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11135 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11141 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11142 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11143 Then the expression will be built.
11144 It will be given type FINAL_TYPE if that is nonzero;
11145 otherwise, it will be given type RESULT_TYPE. */
11147 if (!result_type)
11149 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11150 return error_mark_node;
11153 if (build_type == NULL_TREE)
11155 build_type = result_type;
11156 if ((type0 != orig_type0 || type1 != orig_type1)
11157 && !boolean_op)
11159 gcc_assert (may_need_excess_precision && common);
11160 semantic_result_type = c_common_type (orig_type0, orig_type1);
11164 if (!converted)
11166 op0 = ep_convert_and_check (location, result_type, op0,
11167 semantic_result_type);
11168 op1 = ep_convert_and_check (location, result_type, op1,
11169 semantic_result_type);
11171 /* This can happen if one operand has a vector type, and the other
11172 has a different type. */
11173 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11174 return error_mark_node;
11177 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11178 | SANITIZE_FLOAT_DIVIDE))
11179 && current_function_decl != 0
11180 && !lookup_attribute ("no_sanitize_undefined",
11181 DECL_ATTRIBUTES (current_function_decl))
11182 && (doing_div_or_mod || doing_shift))
11184 /* OP0 and/or OP1 might have side-effects. */
11185 op0 = c_save_expr (op0);
11186 op1 = c_save_expr (op1);
11187 op0 = c_fully_fold (op0, false, NULL);
11188 op1 = c_fully_fold (op1, false, NULL);
11189 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11190 | SANITIZE_FLOAT_DIVIDE)))
11191 instrument_expr = ubsan_instrument_division (location, op0, op1);
11192 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11193 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11196 /* Treat expressions in initializers specially as they can't trap. */
11197 if (int_const_or_overflow)
11198 ret = (require_constant_value
11199 ? fold_build2_initializer_loc (location, resultcode, build_type,
11200 op0, op1)
11201 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11202 else
11203 ret = build2 (resultcode, build_type, op0, op1);
11204 if (final_type != 0)
11205 ret = convert (final_type, ret);
11207 return_build_binary_op:
11208 gcc_assert (ret != error_mark_node);
11209 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11210 ret = (int_operands
11211 ? note_integer_operands (ret)
11212 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11213 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11214 && !in_late_binary_op)
11215 ret = note_integer_operands (ret);
11216 if (semantic_result_type)
11217 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11218 protected_set_expr_location (ret, location);
11220 if (instrument_expr != NULL)
11221 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11222 instrument_expr, ret);
11224 return ret;
11228 /* Convert EXPR to be a truth-value, validating its type for this
11229 purpose. LOCATION is the source location for the expression. */
11231 tree
11232 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11234 bool int_const, int_operands;
11236 switch (TREE_CODE (TREE_TYPE (expr)))
11238 case ARRAY_TYPE:
11239 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11240 return error_mark_node;
11242 case RECORD_TYPE:
11243 error_at (location, "used struct type value where scalar is required");
11244 return error_mark_node;
11246 case UNION_TYPE:
11247 error_at (location, "used union type value where scalar is required");
11248 return error_mark_node;
11250 case VOID_TYPE:
11251 error_at (location, "void value not ignored as it ought to be");
11252 return error_mark_node;
11254 case FUNCTION_TYPE:
11255 gcc_unreachable ();
11257 case VECTOR_TYPE:
11258 error_at (location, "used vector type where scalar is required");
11259 return error_mark_node;
11261 default:
11262 break;
11265 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11266 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11267 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11269 expr = remove_c_maybe_const_expr (expr);
11270 expr = build2 (NE_EXPR, integer_type_node, expr,
11271 convert (TREE_TYPE (expr), integer_zero_node));
11272 expr = note_integer_operands (expr);
11274 else
11275 /* ??? Should we also give an error for vectors rather than leaving
11276 those to give errors later? */
11277 expr = c_common_truthvalue_conversion (location, expr);
11279 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11281 if (TREE_OVERFLOW (expr))
11282 return expr;
11283 else
11284 return note_integer_operands (expr);
11286 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11287 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11288 return expr;
11292 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11293 required. */
11295 tree
11296 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11298 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11300 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11301 /* Executing a compound literal inside a function reinitializes
11302 it. */
11303 if (!TREE_STATIC (decl))
11304 *se = true;
11305 return decl;
11307 else
11308 return expr;
11311 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11313 tree
11314 c_begin_omp_parallel (void)
11316 tree block;
11318 keep_next_level ();
11319 block = c_begin_compound_stmt (true);
11321 return block;
11324 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11325 statement. LOC is the location of the OMP_PARALLEL. */
11327 tree
11328 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11330 tree stmt;
11332 block = c_end_compound_stmt (loc, block, true);
11334 stmt = make_node (OMP_PARALLEL);
11335 TREE_TYPE (stmt) = void_type_node;
11336 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11337 OMP_PARALLEL_BODY (stmt) = block;
11338 SET_EXPR_LOCATION (stmt, loc);
11340 return add_stmt (stmt);
11343 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11345 tree
11346 c_begin_omp_task (void)
11348 tree block;
11350 keep_next_level ();
11351 block = c_begin_compound_stmt (true);
11353 return block;
11356 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11357 statement. LOC is the location of the #pragma. */
11359 tree
11360 c_finish_omp_task (location_t loc, tree clauses, tree block)
11362 tree stmt;
11364 block = c_end_compound_stmt (loc, block, true);
11366 stmt = make_node (OMP_TASK);
11367 TREE_TYPE (stmt) = void_type_node;
11368 OMP_TASK_CLAUSES (stmt) = clauses;
11369 OMP_TASK_BODY (stmt) = block;
11370 SET_EXPR_LOCATION (stmt, loc);
11372 return add_stmt (stmt);
11375 /* Generate GOMP_cancel call for #pragma omp cancel. */
11377 void
11378 c_finish_omp_cancel (location_t loc, tree clauses)
11380 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11381 int mask = 0;
11382 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11383 mask = 1;
11384 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11385 mask = 2;
11386 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11387 mask = 4;
11388 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11389 mask = 8;
11390 else
11392 error_at (loc, "%<#pragma omp cancel must specify one of "
11393 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11394 "clauses");
11395 return;
11397 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11398 if (ifc != NULL_TREE)
11400 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11401 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11402 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11403 build_zero_cst (type));
11405 else
11406 ifc = boolean_true_node;
11407 tree stmt = build_call_expr_loc (loc, fn, 2,
11408 build_int_cst (integer_type_node, mask),
11409 ifc);
11410 add_stmt (stmt);
11413 /* Generate GOMP_cancellation_point call for
11414 #pragma omp cancellation point. */
11416 void
11417 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11419 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11420 int mask = 0;
11421 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11422 mask = 1;
11423 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11424 mask = 2;
11425 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11426 mask = 4;
11427 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11428 mask = 8;
11429 else
11431 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11432 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11433 "clauses");
11434 return;
11436 tree stmt = build_call_expr_loc (loc, fn, 1,
11437 build_int_cst (integer_type_node, mask));
11438 add_stmt (stmt);
11441 /* Helper function for handle_omp_array_sections. Called recursively
11442 to handle multiple array-section-subscripts. C is the clause,
11443 T current expression (initially OMP_CLAUSE_DECL), which is either
11444 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11445 expression if specified, TREE_VALUE length expression if specified,
11446 TREE_CHAIN is what it has been specified after, or some decl.
11447 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11448 set to true if any of the array-section-subscript could have length
11449 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11450 first array-section-subscript which is known not to have length
11451 of one. Given say:
11452 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11453 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11454 all are or may have length of 1, array-section-subscript [:2] is the
11455 first one knonwn not to have length 1. For array-section-subscript
11456 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11457 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11458 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11459 case though, as some lengths could be zero. */
11461 static tree
11462 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11463 bool &maybe_zero_len, unsigned int &first_non_one)
11465 tree ret, low_bound, length, type;
11466 if (TREE_CODE (t) != TREE_LIST)
11468 if (error_operand_p (t))
11469 return error_mark_node;
11470 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11472 if (DECL_P (t))
11473 error_at (OMP_CLAUSE_LOCATION (c),
11474 "%qD is not a variable in %qs clause", t,
11475 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11476 else
11477 error_at (OMP_CLAUSE_LOCATION (c),
11478 "%qE is not a variable in %qs clause", t,
11479 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11480 return error_mark_node;
11482 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11483 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11485 error_at (OMP_CLAUSE_LOCATION (c),
11486 "%qD is threadprivate variable in %qs clause", t,
11487 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11488 return error_mark_node;
11490 return t;
11493 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11494 maybe_zero_len, first_non_one);
11495 if (ret == error_mark_node || ret == NULL_TREE)
11496 return ret;
11498 type = TREE_TYPE (ret);
11499 low_bound = TREE_PURPOSE (t);
11500 length = TREE_VALUE (t);
11502 if (low_bound == error_mark_node || length == error_mark_node)
11503 return error_mark_node;
11505 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11507 error_at (OMP_CLAUSE_LOCATION (c),
11508 "low bound %qE of array section does not have integral type",
11509 low_bound);
11510 return error_mark_node;
11512 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11514 error_at (OMP_CLAUSE_LOCATION (c),
11515 "length %qE of array section does not have integral type",
11516 length);
11517 return error_mark_node;
11519 if (low_bound
11520 && TREE_CODE (low_bound) == INTEGER_CST
11521 && TYPE_PRECISION (TREE_TYPE (low_bound))
11522 > TYPE_PRECISION (sizetype))
11523 low_bound = fold_convert (sizetype, low_bound);
11524 if (length
11525 && TREE_CODE (length) == INTEGER_CST
11526 && TYPE_PRECISION (TREE_TYPE (length))
11527 > TYPE_PRECISION (sizetype))
11528 length = fold_convert (sizetype, length);
11529 if (low_bound == NULL_TREE)
11530 low_bound = integer_zero_node;
11532 if (length != NULL_TREE)
11534 if (!integer_nonzerop (length))
11535 maybe_zero_len = true;
11536 if (first_non_one == types.length ()
11537 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11538 first_non_one++;
11540 if (TREE_CODE (type) == ARRAY_TYPE)
11542 if (length == NULL_TREE
11543 && (TYPE_DOMAIN (type) == NULL_TREE
11544 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11546 error_at (OMP_CLAUSE_LOCATION (c),
11547 "for unknown bound array type length expression must "
11548 "be specified");
11549 return error_mark_node;
11551 if (TREE_CODE (low_bound) == INTEGER_CST
11552 && tree_int_cst_sgn (low_bound) == -1)
11554 error_at (OMP_CLAUSE_LOCATION (c),
11555 "negative low bound in array section in %qs clause",
11556 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11557 return error_mark_node;
11559 if (length != NULL_TREE
11560 && TREE_CODE (length) == INTEGER_CST
11561 && tree_int_cst_sgn (length) == -1)
11563 error_at (OMP_CLAUSE_LOCATION (c),
11564 "negative length in array section in %qs clause",
11565 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11566 return error_mark_node;
11568 if (TYPE_DOMAIN (type)
11569 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11570 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11571 == INTEGER_CST)
11573 tree size = size_binop (PLUS_EXPR,
11574 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11575 size_one_node);
11576 if (TREE_CODE (low_bound) == INTEGER_CST)
11578 if (tree_int_cst_lt (size, low_bound))
11580 error_at (OMP_CLAUSE_LOCATION (c),
11581 "low bound %qE above array section size "
11582 "in %qs clause", low_bound,
11583 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11584 return error_mark_node;
11586 if (tree_int_cst_equal (size, low_bound))
11587 maybe_zero_len = true;
11588 else if (length == NULL_TREE
11589 && first_non_one == types.length ()
11590 && tree_int_cst_equal
11591 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11592 low_bound))
11593 first_non_one++;
11595 else if (length == NULL_TREE)
11597 maybe_zero_len = true;
11598 if (first_non_one == types.length ())
11599 first_non_one++;
11601 if (length && TREE_CODE (length) == INTEGER_CST)
11603 if (tree_int_cst_lt (size, length))
11605 error_at (OMP_CLAUSE_LOCATION (c),
11606 "length %qE above array section size "
11607 "in %qs clause", length,
11608 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11609 return error_mark_node;
11611 if (TREE_CODE (low_bound) == INTEGER_CST)
11613 tree lbpluslen
11614 = size_binop (PLUS_EXPR,
11615 fold_convert (sizetype, low_bound),
11616 fold_convert (sizetype, length));
11617 if (TREE_CODE (lbpluslen) == INTEGER_CST
11618 && tree_int_cst_lt (size, lbpluslen))
11620 error_at (OMP_CLAUSE_LOCATION (c),
11621 "high bound %qE above array section size "
11622 "in %qs clause", lbpluslen,
11623 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11624 return error_mark_node;
11629 else if (length == NULL_TREE)
11631 maybe_zero_len = true;
11632 if (first_non_one == types.length ())
11633 first_non_one++;
11636 /* For [lb:] we will need to evaluate lb more than once. */
11637 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11639 tree lb = c_save_expr (low_bound);
11640 if (lb != low_bound)
11642 TREE_PURPOSE (t) = lb;
11643 low_bound = lb;
11647 else if (TREE_CODE (type) == POINTER_TYPE)
11649 if (length == NULL_TREE)
11651 error_at (OMP_CLAUSE_LOCATION (c),
11652 "for pointer type length expression must be specified");
11653 return error_mark_node;
11655 /* If there is a pointer type anywhere but in the very first
11656 array-section-subscript, the array section can't be contiguous. */
11657 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11658 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11660 error_at (OMP_CLAUSE_LOCATION (c),
11661 "array section is not contiguous in %qs clause",
11662 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11663 return error_mark_node;
11666 else
11668 error_at (OMP_CLAUSE_LOCATION (c),
11669 "%qE does not have pointer or array type", ret);
11670 return error_mark_node;
11672 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11673 types.safe_push (TREE_TYPE (ret));
11674 /* We will need to evaluate lb more than once. */
11675 tree lb = c_save_expr (low_bound);
11676 if (lb != low_bound)
11678 TREE_PURPOSE (t) = lb;
11679 low_bound = lb;
11681 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11682 return ret;
11685 /* Handle array sections for clause C. */
11687 static bool
11688 handle_omp_array_sections (tree c)
11690 bool maybe_zero_len = false;
11691 unsigned int first_non_one = 0;
11692 vec<tree> types = vNULL;
11693 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11694 maybe_zero_len, first_non_one);
11695 if (first == error_mark_node)
11697 types.release ();
11698 return true;
11700 if (first == NULL_TREE)
11702 types.release ();
11703 return false;
11705 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11707 tree t = OMP_CLAUSE_DECL (c);
11708 tree tem = NULL_TREE;
11709 types.release ();
11710 /* Need to evaluate side effects in the length expressions
11711 if any. */
11712 while (TREE_CODE (t) == TREE_LIST)
11714 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11716 if (tem == NULL_TREE)
11717 tem = TREE_VALUE (t);
11718 else
11719 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11720 TREE_VALUE (t), tem);
11722 t = TREE_CHAIN (t);
11724 if (tem)
11725 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11726 first = c_fully_fold (first, false, NULL);
11727 OMP_CLAUSE_DECL (c) = first;
11729 else
11731 unsigned int num = types.length (), i;
11732 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11733 tree condition = NULL_TREE;
11735 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11736 maybe_zero_len = true;
11738 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11739 t = TREE_CHAIN (t))
11741 tree low_bound = TREE_PURPOSE (t);
11742 tree length = TREE_VALUE (t);
11744 i--;
11745 if (low_bound
11746 && TREE_CODE (low_bound) == INTEGER_CST
11747 && TYPE_PRECISION (TREE_TYPE (low_bound))
11748 > TYPE_PRECISION (sizetype))
11749 low_bound = fold_convert (sizetype, low_bound);
11750 if (length
11751 && TREE_CODE (length) == INTEGER_CST
11752 && TYPE_PRECISION (TREE_TYPE (length))
11753 > TYPE_PRECISION (sizetype))
11754 length = fold_convert (sizetype, length);
11755 if (low_bound == NULL_TREE)
11756 low_bound = integer_zero_node;
11757 if (!maybe_zero_len && i > first_non_one)
11759 if (integer_nonzerop (low_bound))
11760 goto do_warn_noncontiguous;
11761 if (length != NULL_TREE
11762 && TREE_CODE (length) == INTEGER_CST
11763 && TYPE_DOMAIN (types[i])
11764 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11765 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11766 == INTEGER_CST)
11768 tree size;
11769 size = size_binop (PLUS_EXPR,
11770 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11771 size_one_node);
11772 if (!tree_int_cst_equal (length, size))
11774 do_warn_noncontiguous:
11775 error_at (OMP_CLAUSE_LOCATION (c),
11776 "array section is not contiguous in %qs "
11777 "clause",
11778 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11779 types.release ();
11780 return true;
11783 if (length != NULL_TREE
11784 && TREE_SIDE_EFFECTS (length))
11786 if (side_effects == NULL_TREE)
11787 side_effects = length;
11788 else
11789 side_effects = build2 (COMPOUND_EXPR,
11790 TREE_TYPE (side_effects),
11791 length, side_effects);
11794 else
11796 tree l;
11798 if (i > first_non_one && length && integer_nonzerop (length))
11799 continue;
11800 if (length)
11801 l = fold_convert (sizetype, length);
11802 else
11804 l = size_binop (PLUS_EXPR,
11805 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11806 size_one_node);
11807 l = size_binop (MINUS_EXPR, l,
11808 fold_convert (sizetype, low_bound));
11810 if (i > first_non_one)
11812 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11813 size_zero_node);
11814 if (condition == NULL_TREE)
11815 condition = l;
11816 else
11817 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11818 l, condition);
11820 else if (size == NULL_TREE)
11822 size = size_in_bytes (TREE_TYPE (types[i]));
11823 size = size_binop (MULT_EXPR, size, l);
11824 if (condition)
11825 size = fold_build3 (COND_EXPR, sizetype, condition,
11826 size, size_zero_node);
11828 else
11829 size = size_binop (MULT_EXPR, size, l);
11832 types.release ();
11833 if (side_effects)
11834 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11835 first = c_fully_fold (first, false, NULL);
11836 OMP_CLAUSE_DECL (c) = first;
11837 if (size)
11838 size = c_fully_fold (size, false, NULL);
11839 OMP_CLAUSE_SIZE (c) = size;
11840 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11841 return false;
11842 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11843 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11844 if (!c_mark_addressable (t))
11845 return false;
11846 OMP_CLAUSE_DECL (c2) = t;
11847 t = build_fold_addr_expr (first);
11848 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11849 tree ptr = OMP_CLAUSE_DECL (c2);
11850 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11851 ptr = build_fold_addr_expr (ptr);
11852 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11853 ptrdiff_type_node, t,
11854 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11855 ptrdiff_type_node, ptr));
11856 t = c_fully_fold (t, false, NULL);
11857 OMP_CLAUSE_SIZE (c2) = t;
11858 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11859 OMP_CLAUSE_CHAIN (c) = c2;
11861 return false;
11864 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11865 an inline call. But, remap
11866 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11867 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11869 static tree
11870 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11871 tree decl, tree placeholder)
11873 copy_body_data id;
11874 hash_map<tree, tree> decl_map;
11876 decl_map.put (omp_decl1, placeholder);
11877 decl_map.put (omp_decl2, decl);
11878 memset (&id, 0, sizeof (id));
11879 id.src_fn = DECL_CONTEXT (omp_decl1);
11880 id.dst_fn = current_function_decl;
11881 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11882 id.decl_map = &decl_map;
11884 id.copy_decl = copy_decl_no_change;
11885 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11886 id.transform_new_cfg = true;
11887 id.transform_return_to_modify = false;
11888 id.transform_lang_insert_block = NULL;
11889 id.eh_lp_nr = 0;
11890 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11891 return stmt;
11894 /* Helper function of c_finish_omp_clauses, called via walk_tree.
11895 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11897 static tree
11898 c_find_omp_placeholder_r (tree *tp, int *, void *data)
11900 if (*tp == (tree) data)
11901 return *tp;
11902 return NULL_TREE;
11905 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
11906 Remove any elements from the list that are invalid. */
11908 tree
11909 c_finish_omp_clauses (tree clauses)
11911 bitmap_head generic_head, firstprivate_head, lastprivate_head;
11912 bitmap_head aligned_head;
11913 tree c, t, *pc;
11914 bool branch_seen = false;
11915 bool copyprivate_seen = false;
11916 tree *nowait_clause = NULL;
11918 bitmap_obstack_initialize (NULL);
11919 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11920 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11921 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
11922 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
11924 for (pc = &clauses, c = clauses; c ; c = *pc)
11926 bool remove = false;
11927 bool need_complete = false;
11928 bool need_implicitly_determined = false;
11930 switch (OMP_CLAUSE_CODE (c))
11932 case OMP_CLAUSE_SHARED:
11933 need_implicitly_determined = true;
11934 goto check_dup_generic;
11936 case OMP_CLAUSE_PRIVATE:
11937 need_complete = true;
11938 need_implicitly_determined = true;
11939 goto check_dup_generic;
11941 case OMP_CLAUSE_REDUCTION:
11942 need_implicitly_determined = true;
11943 t = OMP_CLAUSE_DECL (c);
11944 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
11945 && (FLOAT_TYPE_P (TREE_TYPE (t))
11946 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
11948 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11949 const char *r_name = NULL;
11951 switch (r_code)
11953 case PLUS_EXPR:
11954 case MULT_EXPR:
11955 case MINUS_EXPR:
11956 break;
11957 case MIN_EXPR:
11958 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11959 r_name = "min";
11960 break;
11961 case MAX_EXPR:
11962 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11963 r_name = "max";
11964 break;
11965 case BIT_AND_EXPR:
11966 r_name = "&";
11967 break;
11968 case BIT_XOR_EXPR:
11969 r_name = "^";
11970 break;
11971 case BIT_IOR_EXPR:
11972 r_name = "|";
11973 break;
11974 case TRUTH_ANDIF_EXPR:
11975 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11976 r_name = "&&";
11977 break;
11978 case TRUTH_ORIF_EXPR:
11979 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11980 r_name = "||";
11981 break;
11982 default:
11983 gcc_unreachable ();
11985 if (r_name)
11987 error_at (OMP_CLAUSE_LOCATION (c),
11988 "%qE has invalid type for %<reduction(%s)%>",
11989 t, r_name);
11990 remove = true;
11991 break;
11994 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11996 error_at (OMP_CLAUSE_LOCATION (c),
11997 "user defined reduction not found for %qD", t);
11998 remove = true;
11999 break;
12001 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12003 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12004 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12005 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12006 VAR_DECL, NULL_TREE, type);
12007 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12008 DECL_ARTIFICIAL (placeholder) = 1;
12009 DECL_IGNORED_P (placeholder) = 1;
12010 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12011 c_mark_addressable (placeholder);
12012 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12013 c_mark_addressable (OMP_CLAUSE_DECL (c));
12014 OMP_CLAUSE_REDUCTION_MERGE (c)
12015 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12016 TREE_VEC_ELT (list, 0),
12017 TREE_VEC_ELT (list, 1),
12018 OMP_CLAUSE_DECL (c), placeholder);
12019 OMP_CLAUSE_REDUCTION_MERGE (c)
12020 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12021 void_type_node, NULL_TREE,
12022 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12023 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12024 if (TREE_VEC_LENGTH (list) == 6)
12026 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12027 c_mark_addressable (OMP_CLAUSE_DECL (c));
12028 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12029 c_mark_addressable (placeholder);
12030 tree init = TREE_VEC_ELT (list, 5);
12031 if (init == error_mark_node)
12032 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12033 OMP_CLAUSE_REDUCTION_INIT (c)
12034 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12035 TREE_VEC_ELT (list, 3),
12036 OMP_CLAUSE_DECL (c), placeholder);
12037 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12038 OMP_CLAUSE_REDUCTION_INIT (c)
12039 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12040 OMP_CLAUSE_REDUCTION_INIT (c));
12041 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12042 c_find_omp_placeholder_r,
12043 placeholder, NULL))
12044 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12046 else
12048 tree init;
12049 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12050 init = build_constructor (TREE_TYPE (t), NULL);
12051 else
12052 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12053 OMP_CLAUSE_REDUCTION_INIT (c)
12054 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12056 OMP_CLAUSE_REDUCTION_INIT (c)
12057 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12058 void_type_node, NULL_TREE,
12059 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12060 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12062 goto check_dup_generic;
12064 case OMP_CLAUSE_COPYPRIVATE:
12065 copyprivate_seen = true;
12066 if (nowait_clause)
12068 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12069 "%<nowait%> clause must not be used together "
12070 "with %<copyprivate%>");
12071 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12072 nowait_clause = NULL;
12074 goto check_dup_generic;
12076 case OMP_CLAUSE_COPYIN:
12077 t = OMP_CLAUSE_DECL (c);
12078 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12080 error_at (OMP_CLAUSE_LOCATION (c),
12081 "%qE must be %<threadprivate%> for %<copyin%>", t);
12082 remove = true;
12083 break;
12085 goto check_dup_generic;
12087 case OMP_CLAUSE_LINEAR:
12088 t = OMP_CLAUSE_DECL (c);
12089 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12090 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12092 error_at (OMP_CLAUSE_LOCATION (c),
12093 "linear clause applied to non-integral non-pointer "
12094 "variable with type %qT", TREE_TYPE (t));
12095 remove = true;
12096 break;
12098 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12100 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12101 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12102 OMP_CLAUSE_DECL (c), s);
12103 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12104 sizetype, s, OMP_CLAUSE_DECL (c));
12105 if (s == error_mark_node)
12106 s = size_one_node;
12107 OMP_CLAUSE_LINEAR_STEP (c) = s;
12109 else
12110 OMP_CLAUSE_LINEAR_STEP (c)
12111 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12112 goto check_dup_generic;
12114 check_dup_generic:
12115 t = OMP_CLAUSE_DECL (c);
12116 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12118 error_at (OMP_CLAUSE_LOCATION (c),
12119 "%qE is not a variable in clause %qs", t,
12120 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12121 remove = true;
12123 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12124 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12125 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12127 error_at (OMP_CLAUSE_LOCATION (c),
12128 "%qE appears more than once in data clauses", t);
12129 remove = true;
12131 else
12132 bitmap_set_bit (&generic_head, DECL_UID (t));
12133 break;
12135 case OMP_CLAUSE_FIRSTPRIVATE:
12136 t = OMP_CLAUSE_DECL (c);
12137 need_complete = true;
12138 need_implicitly_determined = true;
12139 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12141 error_at (OMP_CLAUSE_LOCATION (c),
12142 "%qE is not a variable in clause %<firstprivate%>", t);
12143 remove = true;
12145 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12146 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12148 error_at (OMP_CLAUSE_LOCATION (c),
12149 "%qE appears more than once in data clauses", t);
12150 remove = true;
12152 else
12153 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12154 break;
12156 case OMP_CLAUSE_LASTPRIVATE:
12157 t = OMP_CLAUSE_DECL (c);
12158 need_complete = true;
12159 need_implicitly_determined = true;
12160 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12162 error_at (OMP_CLAUSE_LOCATION (c),
12163 "%qE is not a variable in clause %<lastprivate%>", t);
12164 remove = true;
12166 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12167 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12169 error_at (OMP_CLAUSE_LOCATION (c),
12170 "%qE appears more than once in data clauses", t);
12171 remove = true;
12173 else
12174 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12175 break;
12177 case OMP_CLAUSE_ALIGNED:
12178 t = OMP_CLAUSE_DECL (c);
12179 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12181 error_at (OMP_CLAUSE_LOCATION (c),
12182 "%qE is not a variable in %<aligned%> clause", t);
12183 remove = true;
12185 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12186 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12188 error_at (OMP_CLAUSE_LOCATION (c),
12189 "%qE in %<aligned%> clause is neither a pointer nor "
12190 "an array", t);
12191 remove = true;
12193 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12195 error_at (OMP_CLAUSE_LOCATION (c),
12196 "%qE appears more than once in %<aligned%> clauses",
12198 remove = true;
12200 else
12201 bitmap_set_bit (&aligned_head, DECL_UID (t));
12202 break;
12204 case OMP_CLAUSE_DEPEND:
12205 t = OMP_CLAUSE_DECL (c);
12206 if (TREE_CODE (t) == TREE_LIST)
12208 if (handle_omp_array_sections (c))
12209 remove = true;
12210 break;
12212 if (t == error_mark_node)
12213 remove = true;
12214 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12216 error_at (OMP_CLAUSE_LOCATION (c),
12217 "%qE is not a variable in %<depend%> clause", t);
12218 remove = true;
12220 else if (!c_mark_addressable (t))
12221 remove = true;
12222 break;
12224 case OMP_CLAUSE_MAP:
12225 case OMP_CLAUSE_TO:
12226 case OMP_CLAUSE_FROM:
12227 t = OMP_CLAUSE_DECL (c);
12228 if (TREE_CODE (t) == TREE_LIST)
12230 if (handle_omp_array_sections (c))
12231 remove = true;
12232 else
12234 t = OMP_CLAUSE_DECL (c);
12235 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12237 error_at (OMP_CLAUSE_LOCATION (c),
12238 "array section does not have mappable type "
12239 "in %qs clause",
12240 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12241 remove = true;
12244 break;
12246 if (t == error_mark_node)
12247 remove = true;
12248 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12250 error_at (OMP_CLAUSE_LOCATION (c),
12251 "%qE is not a variable in %qs clause", t,
12252 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12253 remove = true;
12255 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12257 error_at (OMP_CLAUSE_LOCATION (c),
12258 "%qD is threadprivate variable in %qs clause", t,
12259 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12260 remove = true;
12262 else if (!c_mark_addressable (t))
12263 remove = true;
12264 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12265 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
12266 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12268 error_at (OMP_CLAUSE_LOCATION (c),
12269 "%qD does not have a mappable type in %qs clause", t,
12270 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12271 remove = true;
12273 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12275 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12276 error ("%qD appears more than once in motion clauses", t);
12277 else
12278 error ("%qD appears more than once in map clauses", t);
12279 remove = true;
12281 else
12282 bitmap_set_bit (&generic_head, DECL_UID (t));
12283 break;
12285 case OMP_CLAUSE_UNIFORM:
12286 t = OMP_CLAUSE_DECL (c);
12287 if (TREE_CODE (t) != PARM_DECL)
12289 if (DECL_P (t))
12290 error_at (OMP_CLAUSE_LOCATION (c),
12291 "%qD is not an argument in %<uniform%> clause", t);
12292 else
12293 error_at (OMP_CLAUSE_LOCATION (c),
12294 "%qE is not an argument in %<uniform%> clause", t);
12295 remove = true;
12296 break;
12298 goto check_dup_generic;
12300 case OMP_CLAUSE_NOWAIT:
12301 if (copyprivate_seen)
12303 error_at (OMP_CLAUSE_LOCATION (c),
12304 "%<nowait%> clause must not be used together "
12305 "with %<copyprivate%>");
12306 remove = true;
12307 break;
12309 nowait_clause = pc;
12310 pc = &OMP_CLAUSE_CHAIN (c);
12311 continue;
12313 case OMP_CLAUSE_IF:
12314 case OMP_CLAUSE_NUM_THREADS:
12315 case OMP_CLAUSE_NUM_TEAMS:
12316 case OMP_CLAUSE_THREAD_LIMIT:
12317 case OMP_CLAUSE_SCHEDULE:
12318 case OMP_CLAUSE_ORDERED:
12319 case OMP_CLAUSE_DEFAULT:
12320 case OMP_CLAUSE_UNTIED:
12321 case OMP_CLAUSE_COLLAPSE:
12322 case OMP_CLAUSE_FINAL:
12323 case OMP_CLAUSE_MERGEABLE:
12324 case OMP_CLAUSE_SAFELEN:
12325 case OMP_CLAUSE_SIMDLEN:
12326 case OMP_CLAUSE_DEVICE:
12327 case OMP_CLAUSE_DIST_SCHEDULE:
12328 case OMP_CLAUSE_PARALLEL:
12329 case OMP_CLAUSE_FOR:
12330 case OMP_CLAUSE_SECTIONS:
12331 case OMP_CLAUSE_TASKGROUP:
12332 case OMP_CLAUSE_PROC_BIND:
12333 case OMP_CLAUSE__CILK_FOR_COUNT_:
12334 pc = &OMP_CLAUSE_CHAIN (c);
12335 continue;
12337 case OMP_CLAUSE_INBRANCH:
12338 case OMP_CLAUSE_NOTINBRANCH:
12339 if (branch_seen)
12341 error_at (OMP_CLAUSE_LOCATION (c),
12342 "%<inbranch%> clause is incompatible with "
12343 "%<notinbranch%>");
12344 remove = true;
12345 break;
12347 branch_seen = true;
12348 pc = &OMP_CLAUSE_CHAIN (c);
12349 continue;
12351 default:
12352 gcc_unreachable ();
12355 if (!remove)
12357 t = OMP_CLAUSE_DECL (c);
12359 if (need_complete)
12361 t = require_complete_type (t);
12362 if (t == error_mark_node)
12363 remove = true;
12366 if (need_implicitly_determined)
12368 const char *share_name = NULL;
12370 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12371 share_name = "threadprivate";
12372 else switch (c_omp_predetermined_sharing (t))
12374 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12375 break;
12376 case OMP_CLAUSE_DEFAULT_SHARED:
12377 /* const vars may be specified in firstprivate clause. */
12378 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12379 && TREE_READONLY (t))
12380 break;
12381 share_name = "shared";
12382 break;
12383 case OMP_CLAUSE_DEFAULT_PRIVATE:
12384 share_name = "private";
12385 break;
12386 default:
12387 gcc_unreachable ();
12389 if (share_name)
12391 error_at (OMP_CLAUSE_LOCATION (c),
12392 "%qE is predetermined %qs for %qs",
12393 t, share_name,
12394 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12395 remove = true;
12400 if (remove)
12401 *pc = OMP_CLAUSE_CHAIN (c);
12402 else
12403 pc = &OMP_CLAUSE_CHAIN (c);
12406 bitmap_obstack_release (NULL);
12407 return clauses;
12410 /* Create a transaction node. */
12412 tree
12413 c_finish_transaction (location_t loc, tree block, int flags)
12415 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12416 if (flags & TM_STMT_ATTR_OUTER)
12417 TRANSACTION_EXPR_OUTER (stmt) = 1;
12418 if (flags & TM_STMT_ATTR_RELAXED)
12419 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12420 return add_stmt (stmt);
12423 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12424 down to the element type of an array. */
12426 tree
12427 c_build_qualified_type (tree type, int type_quals)
12429 if (type == error_mark_node)
12430 return type;
12432 if (TREE_CODE (type) == ARRAY_TYPE)
12434 tree t;
12435 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12436 type_quals);
12438 /* See if we already have an identically qualified type. */
12439 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12441 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12442 && TYPE_NAME (t) == TYPE_NAME (type)
12443 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12444 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12445 TYPE_ATTRIBUTES (type)))
12446 break;
12448 if (!t)
12450 tree domain = TYPE_DOMAIN (type);
12452 t = build_variant_type_copy (type);
12453 TREE_TYPE (t) = element_type;
12455 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12456 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12457 SET_TYPE_STRUCTURAL_EQUALITY (t);
12458 else if (TYPE_CANONICAL (element_type) != element_type
12459 || (domain && TYPE_CANONICAL (domain) != domain))
12461 tree unqualified_canon
12462 = build_array_type (TYPE_CANONICAL (element_type),
12463 domain? TYPE_CANONICAL (domain)
12464 : NULL_TREE);
12465 TYPE_CANONICAL (t)
12466 = c_build_qualified_type (unqualified_canon, type_quals);
12468 else
12469 TYPE_CANONICAL (t) = t;
12471 return t;
12474 /* A restrict-qualified pointer type must be a pointer to object or
12475 incomplete type. Note that the use of POINTER_TYPE_P also allows
12476 REFERENCE_TYPEs, which is appropriate for C++. */
12477 if ((type_quals & TYPE_QUAL_RESTRICT)
12478 && (!POINTER_TYPE_P (type)
12479 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12481 error ("invalid use of %<restrict%>");
12482 type_quals &= ~TYPE_QUAL_RESTRICT;
12485 return build_qualified_type (type, type_quals);
12488 /* Build a VA_ARG_EXPR for the C parser. */
12490 tree
12491 c_build_va_arg (location_t loc, tree expr, tree type)
12493 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12494 warning_at (loc, OPT_Wc___compat,
12495 "C++ requires promoted type, not enum type, in %<va_arg%>");
12496 return build_va_arg (loc, expr, type);
12499 /* Return truthvalue of whether T1 is the same tree structure as T2.
12500 Return 1 if they are the same. Return 0 if they are different. */
12502 bool
12503 c_tree_equal (tree t1, tree t2)
12505 enum tree_code code1, code2;
12507 if (t1 == t2)
12508 return true;
12509 if (!t1 || !t2)
12510 return false;
12512 for (code1 = TREE_CODE (t1);
12513 CONVERT_EXPR_CODE_P (code1)
12514 || code1 == NON_LVALUE_EXPR;
12515 code1 = TREE_CODE (t1))
12516 t1 = TREE_OPERAND (t1, 0);
12517 for (code2 = TREE_CODE (t2);
12518 CONVERT_EXPR_CODE_P (code2)
12519 || code2 == NON_LVALUE_EXPR;
12520 code2 = TREE_CODE (t2))
12521 t2 = TREE_OPERAND (t2, 0);
12523 /* They might have become equal now. */
12524 if (t1 == t2)
12525 return true;
12527 if (code1 != code2)
12528 return false;
12530 switch (code1)
12532 case INTEGER_CST:
12533 return wi::eq_p (t1, t2);
12535 case REAL_CST:
12536 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12538 case STRING_CST:
12539 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12540 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12541 TREE_STRING_LENGTH (t1));
12543 case FIXED_CST:
12544 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12545 TREE_FIXED_CST (t2));
12547 case COMPLEX_CST:
12548 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12549 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12551 case VECTOR_CST:
12552 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12554 case CONSTRUCTOR:
12555 /* We need to do this when determining whether or not two
12556 non-type pointer to member function template arguments
12557 are the same. */
12558 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12559 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12560 return false;
12562 tree field, value;
12563 unsigned int i;
12564 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12566 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12567 if (!c_tree_equal (field, elt2->index)
12568 || !c_tree_equal (value, elt2->value))
12569 return false;
12572 return true;
12574 case TREE_LIST:
12575 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12576 return false;
12577 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12578 return false;
12579 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12581 case SAVE_EXPR:
12582 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12584 case CALL_EXPR:
12586 tree arg1, arg2;
12587 call_expr_arg_iterator iter1, iter2;
12588 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12589 return false;
12590 for (arg1 = first_call_expr_arg (t1, &iter1),
12591 arg2 = first_call_expr_arg (t2, &iter2);
12592 arg1 && arg2;
12593 arg1 = next_call_expr_arg (&iter1),
12594 arg2 = next_call_expr_arg (&iter2))
12595 if (!c_tree_equal (arg1, arg2))
12596 return false;
12597 if (arg1 || arg2)
12598 return false;
12599 return true;
12602 case TARGET_EXPR:
12604 tree o1 = TREE_OPERAND (t1, 0);
12605 tree o2 = TREE_OPERAND (t2, 0);
12607 /* Special case: if either target is an unallocated VAR_DECL,
12608 it means that it's going to be unified with whatever the
12609 TARGET_EXPR is really supposed to initialize, so treat it
12610 as being equivalent to anything. */
12611 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12612 && !DECL_RTL_SET_P (o1))
12613 /*Nop*/;
12614 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12615 && !DECL_RTL_SET_P (o2))
12616 /*Nop*/;
12617 else if (!c_tree_equal (o1, o2))
12618 return false;
12620 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12623 case COMPONENT_REF:
12624 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12625 return false;
12626 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12628 case PARM_DECL:
12629 case VAR_DECL:
12630 case CONST_DECL:
12631 case FIELD_DECL:
12632 case FUNCTION_DECL:
12633 case IDENTIFIER_NODE:
12634 case SSA_NAME:
12635 return false;
12637 case TREE_VEC:
12639 unsigned ix;
12640 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12641 return false;
12642 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12643 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12644 TREE_VEC_ELT (t2, ix)))
12645 return false;
12646 return true;
12649 default:
12650 break;
12653 switch (TREE_CODE_CLASS (code1))
12655 case tcc_unary:
12656 case tcc_binary:
12657 case tcc_comparison:
12658 case tcc_expression:
12659 case tcc_vl_exp:
12660 case tcc_reference:
12661 case tcc_statement:
12663 int i, n = TREE_OPERAND_LENGTH (t1);
12665 switch (code1)
12667 case PREINCREMENT_EXPR:
12668 case PREDECREMENT_EXPR:
12669 case POSTINCREMENT_EXPR:
12670 case POSTDECREMENT_EXPR:
12671 n = 1;
12672 break;
12673 case ARRAY_REF:
12674 n = 2;
12675 break;
12676 default:
12677 break;
12680 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12681 && n != TREE_OPERAND_LENGTH (t2))
12682 return false;
12684 for (i = 0; i < n; ++i)
12685 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12686 return false;
12688 return true;
12691 case tcc_type:
12692 return comptypes (t1, t2);
12693 default:
12694 gcc_unreachable ();
12696 /* We can get here with --disable-checking. */
12697 return false;
12700 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12701 spawn-helper and BODY is the newly created body for FNDECL. */
12703 void
12704 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12706 tree list = alloc_stmt_list ();
12707 tree frame = make_cilk_frame (fndecl);
12708 tree dtor = create_cilk_function_exit (frame, false, true);
12709 add_local_decl (cfun, frame);
12711 DECL_SAVED_TREE (fndecl) = list;
12712 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12713 frame);
12714 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12715 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12717 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12718 append_to_statement_list (detach_expr, &body_list);
12720 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12721 body = fold_build_cleanup_point_expr (void_type_node, body);
12723 append_to_statement_list (body, &body_list);
12724 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12725 body_list, dtor), &list);