gcc/ChangeLog
[official-gcc.git] / gcc / c / c-typeck.c
blobc622a9027e6f9bb0b38162f5aac0864cd5e459ff
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "alias.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
34 #include "trans-mem.h"
35 #include "varasm.h"
36 #include "stmt.h"
37 #include "langhooks.h"
38 #include "c-tree.h"
39 #include "c-lang.h"
40 #include "flags.h"
41 #include "intl.h"
42 #include "target.h"
43 #include "tree-iterator.h"
44 #include "bitmap.h"
45 #include "predict.h"
46 #include "hard-reg-set.h"
47 #include "function.h"
48 #include "gimple-expr.h"
49 #include "gimplify.h"
50 #include "tree-inline.h"
51 #include "omp-low.h"
52 #include "c-family/c-objc.h"
53 #include "c-family/c-common.h"
54 #include "c-family/c-ubsan.h"
55 #include "cilk.h"
56 #include "gomp-constants.h"
58 /* Possible cases of implicit bad conversions. Used to select
59 diagnostic messages in convert_for_assignment. */
60 enum impl_conv {
61 ic_argpass,
62 ic_assign,
63 ic_init,
64 ic_return
67 /* The level of nesting inside "__alignof__". */
68 int in_alignof;
70 /* The level of nesting inside "sizeof". */
71 int in_sizeof;
73 /* The level of nesting inside "typeof". */
74 int in_typeof;
76 /* The argument of last parsed sizeof expression, only to be tested
77 if expr.original_code == SIZEOF_EXPR. */
78 tree c_last_sizeof_arg;
80 /* Nonzero if we might need to print a "missing braces around
81 initializer" message within this initializer. */
82 static int found_missing_braces;
84 static int require_constant_value;
85 static int require_constant_elements;
87 static bool null_pointer_constant_p (const_tree);
88 static tree qualify_type (tree, tree);
89 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
90 bool *);
91 static int comp_target_types (location_t, tree, tree);
92 static int function_types_compatible_p (const_tree, const_tree, bool *,
93 bool *);
94 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
95 static tree lookup_field (tree, tree);
96 static int convert_arguments (location_t, vec<location_t>, tree,
97 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
98 tree);
99 static tree pointer_diff (location_t, tree, tree);
100 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
101 enum impl_conv, bool, tree, tree, int);
102 static tree valid_compound_expr_initializer (tree, tree);
103 static void push_string (const char *);
104 static void push_member_name (tree);
105 static int spelling_length (void);
106 static char *print_spelling (char *);
107 static void warning_init (location_t, int, const char *);
108 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
109 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
110 bool, struct obstack *);
111 static void output_pending_init_elements (int, struct obstack *);
112 static int set_designator (location_t, int, struct obstack *);
113 static void push_range_stack (tree, struct obstack *);
114 static void add_pending_init (location_t, tree, tree, tree, bool,
115 struct obstack *);
116 static void set_nonincremental_init (struct obstack *);
117 static void set_nonincremental_init_from_string (tree, struct obstack *);
118 static tree find_init_member (tree, struct obstack *);
119 static void readonly_warning (tree, enum lvalue_use);
120 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
121 static void record_maybe_used_decl (tree);
122 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
124 /* Return true if EXP is a null pointer constant, false otherwise. */
126 static bool
127 null_pointer_constant_p (const_tree expr)
129 /* This should really operate on c_expr structures, but they aren't
130 yet available everywhere required. */
131 tree type = TREE_TYPE (expr);
132 return (TREE_CODE (expr) == INTEGER_CST
133 && !TREE_OVERFLOW (expr)
134 && integer_zerop (expr)
135 && (INTEGRAL_TYPE_P (type)
136 || (TREE_CODE (type) == POINTER_TYPE
137 && VOID_TYPE_P (TREE_TYPE (type))
138 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
141 /* EXPR may appear in an unevaluated part of an integer constant
142 expression, but not in an evaluated part. Wrap it in a
143 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
144 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
146 static tree
147 note_integer_operands (tree expr)
149 tree ret;
150 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
152 ret = copy_node (expr);
153 TREE_OVERFLOW (ret) = 1;
155 else
157 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
158 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
160 return ret;
163 /* Having checked whether EXPR may appear in an unevaluated part of an
164 integer constant expression and found that it may, remove any
165 C_MAYBE_CONST_EXPR noting this fact and return the resulting
166 expression. */
168 static inline tree
169 remove_c_maybe_const_expr (tree expr)
171 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
172 return C_MAYBE_CONST_EXPR_EXPR (expr);
173 else
174 return expr;
177 \f/* This is a cache to hold if two types are compatible or not. */
179 struct tagged_tu_seen_cache {
180 const struct tagged_tu_seen_cache * next;
181 const_tree t1;
182 const_tree t2;
183 /* The return value of tagged_types_tu_compatible_p if we had seen
184 these two types already. */
185 int val;
188 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
189 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
191 /* Do `exp = require_complete_type (exp);' to make sure exp
192 does not have an incomplete type. (That includes void types.) */
194 tree
195 require_complete_type (tree value)
197 tree type = TREE_TYPE (value);
199 if (error_operand_p (value))
200 return error_mark_node;
202 /* First, detect a valid value with a complete type. */
203 if (COMPLETE_TYPE_P (type))
204 return value;
206 c_incomplete_type_error (value, type);
207 return error_mark_node;
210 /* Print an error message for invalid use of an incomplete type.
211 VALUE is the expression that was used (or 0 if that isn't known)
212 and TYPE is the type that was invalid. */
214 void
215 c_incomplete_type_error (const_tree value, const_tree type)
217 /* Avoid duplicate error message. */
218 if (TREE_CODE (type) == ERROR_MARK)
219 return;
221 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
222 error ("%qD has an incomplete type %qT", value, type);
223 else
225 retry:
226 /* We must print an error message. Be clever about what it says. */
228 switch (TREE_CODE (type))
230 case RECORD_TYPE:
231 case UNION_TYPE:
232 case ENUMERAL_TYPE:
233 break;
235 case VOID_TYPE:
236 error ("invalid use of void expression");
237 return;
239 case ARRAY_TYPE:
240 if (TYPE_DOMAIN (type))
242 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
244 error ("invalid use of flexible array member");
245 return;
247 type = TREE_TYPE (type);
248 goto retry;
250 error ("invalid use of array with unspecified bounds");
251 return;
253 default:
254 gcc_unreachable ();
257 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
258 error ("invalid use of undefined type %qT", type);
259 else
260 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
261 error ("invalid use of incomplete typedef %qT", type);
265 /* Given a type, apply default promotions wrt unnamed function
266 arguments and return the new type. */
268 tree
269 c_type_promotes_to (tree type)
271 tree ret = NULL_TREE;
273 if (TYPE_MAIN_VARIANT (type) == float_type_node)
274 ret = double_type_node;
275 else if (c_promoting_integer_type_p (type))
277 /* Preserve unsignedness if not really getting any wider. */
278 if (TYPE_UNSIGNED (type)
279 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
280 ret = unsigned_type_node;
281 else
282 ret = integer_type_node;
285 if (ret != NULL_TREE)
286 return (TYPE_ATOMIC (type)
287 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
288 : ret);
290 return type;
293 /* Return true if between two named address spaces, whether there is a superset
294 named address space that encompasses both address spaces. If there is a
295 superset, return which address space is the superset. */
297 static bool
298 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
300 if (as1 == as2)
302 *common = as1;
303 return true;
305 else if (targetm.addr_space.subset_p (as1, as2))
307 *common = as2;
308 return true;
310 else if (targetm.addr_space.subset_p (as2, as1))
312 *common = as1;
313 return true;
315 else
316 return false;
319 /* Return a variant of TYPE which has all the type qualifiers of LIKE
320 as well as those of TYPE. */
322 static tree
323 qualify_type (tree type, tree like)
325 addr_space_t as_type = TYPE_ADDR_SPACE (type);
326 addr_space_t as_like = TYPE_ADDR_SPACE (like);
327 addr_space_t as_common;
329 /* If the two named address spaces are different, determine the common
330 superset address space. If there isn't one, raise an error. */
331 if (!addr_space_superset (as_type, as_like, &as_common))
333 as_common = as_type;
334 error ("%qT and %qT are in disjoint named address spaces",
335 type, like);
338 return c_build_qualified_type (type,
339 TYPE_QUALS_NO_ADDR_SPACE (type)
340 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
341 | ENCODE_QUAL_ADDR_SPACE (as_common));
344 /* Return true iff the given tree T is a variable length array. */
346 bool
347 c_vla_type_p (const_tree t)
349 if (TREE_CODE (t) == ARRAY_TYPE
350 && C_TYPE_VARIABLE_SIZE (t))
351 return true;
352 return false;
355 /* Return the composite type of two compatible types.
357 We assume that comptypes has already been done and returned
358 nonzero; if that isn't so, this may crash. In particular, we
359 assume that qualifiers match. */
361 tree
362 composite_type (tree t1, tree t2)
364 enum tree_code code1;
365 enum tree_code code2;
366 tree attributes;
368 /* Save time if the two types are the same. */
370 if (t1 == t2) return t1;
372 /* If one type is nonsense, use the other. */
373 if (t1 == error_mark_node)
374 return t2;
375 if (t2 == error_mark_node)
376 return t1;
378 code1 = TREE_CODE (t1);
379 code2 = TREE_CODE (t2);
381 /* Merge the attributes. */
382 attributes = targetm.merge_type_attributes (t1, t2);
384 /* If one is an enumerated type and the other is the compatible
385 integer type, the composite type might be either of the two
386 (DR#013 question 3). For consistency, use the enumerated type as
387 the composite type. */
389 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
390 return t1;
391 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
392 return t2;
394 gcc_assert (code1 == code2);
396 switch (code1)
398 case POINTER_TYPE:
399 /* For two pointers, do this recursively on the target type. */
401 tree pointed_to_1 = TREE_TYPE (t1);
402 tree pointed_to_2 = TREE_TYPE (t2);
403 tree target = composite_type (pointed_to_1, pointed_to_2);
404 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
405 t1 = build_type_attribute_variant (t1, attributes);
406 return qualify_type (t1, t2);
409 case ARRAY_TYPE:
411 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
412 int quals;
413 tree unqual_elt;
414 tree d1 = TYPE_DOMAIN (t1);
415 tree d2 = TYPE_DOMAIN (t2);
416 bool d1_variable, d2_variable;
417 bool d1_zero, d2_zero;
418 bool t1_complete, t2_complete;
420 /* We should not have any type quals on arrays at all. */
421 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
422 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
424 t1_complete = COMPLETE_TYPE_P (t1);
425 t2_complete = COMPLETE_TYPE_P (t2);
427 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
428 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
430 d1_variable = (!d1_zero
431 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
432 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
433 d2_variable = (!d2_zero
434 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
435 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
436 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
437 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
439 /* Save space: see if the result is identical to one of the args. */
440 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
441 && (d2_variable || d2_zero || !d1_variable))
442 return build_type_attribute_variant (t1, attributes);
443 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
444 && (d1_variable || d1_zero || !d2_variable))
445 return build_type_attribute_variant (t2, attributes);
447 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
448 return build_type_attribute_variant (t1, attributes);
449 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
450 return build_type_attribute_variant (t2, attributes);
452 /* Merge the element types, and have a size if either arg has
453 one. We may have qualifiers on the element types. To set
454 up TYPE_MAIN_VARIANT correctly, we need to form the
455 composite of the unqualified types and add the qualifiers
456 back at the end. */
457 quals = TYPE_QUALS (strip_array_types (elt));
458 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
459 t1 = build_array_type (unqual_elt,
460 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
461 && (d2_variable
462 || d2_zero
463 || !d1_variable))
464 ? t1
465 : t2));
466 /* Ensure a composite type involving a zero-length array type
467 is a zero-length type not an incomplete type. */
468 if (d1_zero && d2_zero
469 && (t1_complete || t2_complete)
470 && !COMPLETE_TYPE_P (t1))
472 TYPE_SIZE (t1) = bitsize_zero_node;
473 TYPE_SIZE_UNIT (t1) = size_zero_node;
475 t1 = c_build_qualified_type (t1, quals);
476 return build_type_attribute_variant (t1, attributes);
479 case ENUMERAL_TYPE:
480 case RECORD_TYPE:
481 case UNION_TYPE:
482 if (attributes != NULL)
484 /* Try harder not to create a new aggregate type. */
485 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
486 return t1;
487 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
488 return t2;
490 return build_type_attribute_variant (t1, attributes);
492 case FUNCTION_TYPE:
493 /* Function types: prefer the one that specified arg types.
494 If both do, merge the arg types. Also merge the return types. */
496 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
497 tree p1 = TYPE_ARG_TYPES (t1);
498 tree p2 = TYPE_ARG_TYPES (t2);
499 int len;
500 tree newargs, n;
501 int i;
503 /* Save space: see if the result is identical to one of the args. */
504 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
505 return build_type_attribute_variant (t1, attributes);
506 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
507 return build_type_attribute_variant (t2, attributes);
509 /* Simple way if one arg fails to specify argument types. */
510 if (TYPE_ARG_TYPES (t1) == 0)
512 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
513 t1 = build_type_attribute_variant (t1, attributes);
514 return qualify_type (t1, t2);
516 if (TYPE_ARG_TYPES (t2) == 0)
518 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
519 t1 = build_type_attribute_variant (t1, attributes);
520 return qualify_type (t1, t2);
523 /* If both args specify argument types, we must merge the two
524 lists, argument by argument. */
526 len = list_length (p1);
527 newargs = 0;
529 for (i = 0; i < len; i++)
530 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
532 n = newargs;
534 for (; p1;
535 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
537 /* A null type means arg type is not specified.
538 Take whatever the other function type has. */
539 if (TREE_VALUE (p1) == 0)
541 TREE_VALUE (n) = TREE_VALUE (p2);
542 goto parm_done;
544 if (TREE_VALUE (p2) == 0)
546 TREE_VALUE (n) = TREE_VALUE (p1);
547 goto parm_done;
550 /* Given wait (union {union wait *u; int *i} *)
551 and wait (union wait *),
552 prefer union wait * as type of parm. */
553 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
554 && TREE_VALUE (p1) != TREE_VALUE (p2))
556 tree memb;
557 tree mv2 = TREE_VALUE (p2);
558 if (mv2 && mv2 != error_mark_node
559 && TREE_CODE (mv2) != ARRAY_TYPE)
560 mv2 = TYPE_MAIN_VARIANT (mv2);
561 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
562 memb; memb = DECL_CHAIN (memb))
564 tree mv3 = TREE_TYPE (memb);
565 if (mv3 && mv3 != error_mark_node
566 && TREE_CODE (mv3) != ARRAY_TYPE)
567 mv3 = TYPE_MAIN_VARIANT (mv3);
568 if (comptypes (mv3, mv2))
570 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
571 TREE_VALUE (p2));
572 pedwarn (input_location, OPT_Wpedantic,
573 "function types not truly compatible in ISO C");
574 goto parm_done;
578 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
579 && TREE_VALUE (p2) != TREE_VALUE (p1))
581 tree memb;
582 tree mv1 = TREE_VALUE (p1);
583 if (mv1 && mv1 != error_mark_node
584 && TREE_CODE (mv1) != ARRAY_TYPE)
585 mv1 = TYPE_MAIN_VARIANT (mv1);
586 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
587 memb; memb = DECL_CHAIN (memb))
589 tree mv3 = TREE_TYPE (memb);
590 if (mv3 && mv3 != error_mark_node
591 && TREE_CODE (mv3) != ARRAY_TYPE)
592 mv3 = TYPE_MAIN_VARIANT (mv3);
593 if (comptypes (mv3, mv1))
595 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
596 TREE_VALUE (p1));
597 pedwarn (input_location, OPT_Wpedantic,
598 "function types not truly compatible in ISO C");
599 goto parm_done;
603 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
604 parm_done: ;
607 t1 = build_function_type (valtype, newargs);
608 t1 = qualify_type (t1, t2);
609 /* ... falls through ... */
612 default:
613 return build_type_attribute_variant (t1, attributes);
618 /* Return the type of a conditional expression between pointers to
619 possibly differently qualified versions of compatible types.
621 We assume that comp_target_types has already been done and returned
622 nonzero; if that isn't so, this may crash. */
624 static tree
625 common_pointer_type (tree t1, tree t2)
627 tree attributes;
628 tree pointed_to_1, mv1;
629 tree pointed_to_2, mv2;
630 tree target;
631 unsigned target_quals;
632 addr_space_t as1, as2, as_common;
633 int quals1, quals2;
635 /* Save time if the two types are the same. */
637 if (t1 == t2) return t1;
639 /* If one type is nonsense, use the other. */
640 if (t1 == error_mark_node)
641 return t2;
642 if (t2 == error_mark_node)
643 return t1;
645 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
646 && TREE_CODE (t2) == POINTER_TYPE);
648 /* Merge the attributes. */
649 attributes = targetm.merge_type_attributes (t1, t2);
651 /* Find the composite type of the target types, and combine the
652 qualifiers of the two types' targets. Do not lose qualifiers on
653 array element types by taking the TYPE_MAIN_VARIANT. */
654 mv1 = pointed_to_1 = TREE_TYPE (t1);
655 mv2 = pointed_to_2 = TREE_TYPE (t2);
656 if (TREE_CODE (mv1) != ARRAY_TYPE)
657 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
658 if (TREE_CODE (mv2) != ARRAY_TYPE)
659 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
660 target = composite_type (mv1, mv2);
662 /* Strip array types to get correct qualifier for pointers to arrays */
663 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
664 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
666 /* For function types do not merge const qualifiers, but drop them
667 if used inconsistently. The middle-end uses these to mark const
668 and noreturn functions. */
669 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
670 target_quals = (quals1 & quals2);
671 else
672 target_quals = (quals1 | quals2);
674 /* If the two named address spaces are different, determine the common
675 superset address space. This is guaranteed to exist due to the
676 assumption that comp_target_type returned non-zero. */
677 as1 = TYPE_ADDR_SPACE (pointed_to_1);
678 as2 = TYPE_ADDR_SPACE (pointed_to_2);
679 if (!addr_space_superset (as1, as2, &as_common))
680 gcc_unreachable ();
682 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
684 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
685 return build_type_attribute_variant (t1, attributes);
688 /* Return the common type for two arithmetic types under the usual
689 arithmetic conversions. The default conversions have already been
690 applied, and enumerated types converted to their compatible integer
691 types. The resulting type is unqualified and has no attributes.
693 This is the type for the result of most arithmetic operations
694 if the operands have the given two types. */
696 static tree
697 c_common_type (tree t1, tree t2)
699 enum tree_code code1;
700 enum tree_code code2;
702 /* If one type is nonsense, use the other. */
703 if (t1 == error_mark_node)
704 return t2;
705 if (t2 == error_mark_node)
706 return t1;
708 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
709 t1 = TYPE_MAIN_VARIANT (t1);
711 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
712 t2 = TYPE_MAIN_VARIANT (t2);
714 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
715 t1 = build_type_attribute_variant (t1, NULL_TREE);
717 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
718 t2 = build_type_attribute_variant (t2, NULL_TREE);
720 /* Save time if the two types are the same. */
722 if (t1 == t2) return t1;
724 code1 = TREE_CODE (t1);
725 code2 = TREE_CODE (t2);
727 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
728 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
729 || code1 == INTEGER_TYPE);
730 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
731 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
732 || code2 == INTEGER_TYPE);
734 /* When one operand is a decimal float type, the other operand cannot be
735 a generic float type or a complex type. We also disallow vector types
736 here. */
737 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
738 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
740 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
742 error ("can%'t mix operands of decimal float and vector types");
743 return error_mark_node;
745 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
747 error ("can%'t mix operands of decimal float and complex types");
748 return error_mark_node;
750 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
752 error ("can%'t mix operands of decimal float and other float types");
753 return error_mark_node;
757 /* If one type is a vector type, return that type. (How the usual
758 arithmetic conversions apply to the vector types extension is not
759 precisely specified.) */
760 if (code1 == VECTOR_TYPE)
761 return t1;
763 if (code2 == VECTOR_TYPE)
764 return t2;
766 /* If one type is complex, form the common type of the non-complex
767 components, then make that complex. Use T1 or T2 if it is the
768 required type. */
769 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
771 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
772 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
773 tree subtype = c_common_type (subtype1, subtype2);
775 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
776 return t1;
777 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
778 return t2;
779 else
780 return build_complex_type (subtype);
783 /* If only one is real, use it as the result. */
785 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
786 return t1;
788 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
789 return t2;
791 /* If both are real and either are decimal floating point types, use
792 the decimal floating point type with the greater precision. */
794 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
796 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
797 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
798 return dfloat128_type_node;
799 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
800 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
801 return dfloat64_type_node;
802 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
803 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
804 return dfloat32_type_node;
807 /* Deal with fixed-point types. */
808 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
810 unsigned int unsignedp = 0, satp = 0;
811 machine_mode m1, m2;
812 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
814 m1 = TYPE_MODE (t1);
815 m2 = TYPE_MODE (t2);
817 /* If one input type is saturating, the result type is saturating. */
818 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
819 satp = 1;
821 /* If both fixed-point types are unsigned, the result type is unsigned.
822 When mixing fixed-point and integer types, follow the sign of the
823 fixed-point type.
824 Otherwise, the result type is signed. */
825 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
826 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
827 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
828 && TYPE_UNSIGNED (t1))
829 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
830 && TYPE_UNSIGNED (t2)))
831 unsignedp = 1;
833 /* The result type is signed. */
834 if (unsignedp == 0)
836 /* If the input type is unsigned, we need to convert to the
837 signed type. */
838 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
840 enum mode_class mclass = (enum mode_class) 0;
841 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
842 mclass = MODE_FRACT;
843 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
844 mclass = MODE_ACCUM;
845 else
846 gcc_unreachable ();
847 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
849 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
851 enum mode_class mclass = (enum mode_class) 0;
852 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
853 mclass = MODE_FRACT;
854 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
855 mclass = MODE_ACCUM;
856 else
857 gcc_unreachable ();
858 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
862 if (code1 == FIXED_POINT_TYPE)
864 fbit1 = GET_MODE_FBIT (m1);
865 ibit1 = GET_MODE_IBIT (m1);
867 else
869 fbit1 = 0;
870 /* Signed integers need to subtract one sign bit. */
871 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
874 if (code2 == FIXED_POINT_TYPE)
876 fbit2 = GET_MODE_FBIT (m2);
877 ibit2 = GET_MODE_IBIT (m2);
879 else
881 fbit2 = 0;
882 /* Signed integers need to subtract one sign bit. */
883 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
886 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
887 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
888 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
889 satp);
892 /* Both real or both integers; use the one with greater precision. */
894 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
895 return t1;
896 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
897 return t2;
899 /* Same precision. Prefer long longs to longs to ints when the
900 same precision, following the C99 rules on integer type rank
901 (which are equivalent to the C90 rules for C90 types). */
903 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
904 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
905 return long_long_unsigned_type_node;
907 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
908 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
910 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
911 return long_long_unsigned_type_node;
912 else
913 return long_long_integer_type_node;
916 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
917 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
918 return long_unsigned_type_node;
920 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
921 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
923 /* But preserve unsignedness from the other type,
924 since long cannot hold all the values of an unsigned int. */
925 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
926 return long_unsigned_type_node;
927 else
928 return long_integer_type_node;
931 /* Likewise, prefer long double to double even if same size. */
932 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
933 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
934 return long_double_type_node;
936 /* Likewise, prefer double to float even if same size.
937 We got a couple of embedded targets with 32 bit doubles, and the
938 pdp11 might have 64 bit floats. */
939 if (TYPE_MAIN_VARIANT (t1) == double_type_node
940 || TYPE_MAIN_VARIANT (t2) == double_type_node)
941 return double_type_node;
943 /* Otherwise prefer the unsigned one. */
945 if (TYPE_UNSIGNED (t1))
946 return t1;
947 else
948 return t2;
951 /* Wrapper around c_common_type that is used by c-common.c and other
952 front end optimizations that remove promotions. ENUMERAL_TYPEs
953 are allowed here and are converted to their compatible integer types.
954 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
955 preferably a non-Boolean type as the common type. */
956 tree
957 common_type (tree t1, tree t2)
959 if (TREE_CODE (t1) == ENUMERAL_TYPE)
960 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
961 if (TREE_CODE (t2) == ENUMERAL_TYPE)
962 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
964 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
965 if (TREE_CODE (t1) == BOOLEAN_TYPE
966 && TREE_CODE (t2) == BOOLEAN_TYPE)
967 return boolean_type_node;
969 /* If either type is BOOLEAN_TYPE, then return the other. */
970 if (TREE_CODE (t1) == BOOLEAN_TYPE)
971 return t2;
972 if (TREE_CODE (t2) == BOOLEAN_TYPE)
973 return t1;
975 return c_common_type (t1, t2);
978 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
979 or various other operations. Return 2 if they are compatible
980 but a warning may be needed if you use them together. */
983 comptypes (tree type1, tree type2)
985 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
986 int val;
988 val = comptypes_internal (type1, type2, NULL, NULL);
989 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
991 return val;
994 /* Like comptypes, but if it returns non-zero because enum and int are
995 compatible, it sets *ENUM_AND_INT_P to true. */
997 static int
998 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1000 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1001 int val;
1003 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1004 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1006 return val;
1009 /* Like comptypes, but if it returns nonzero for different types, it
1010 sets *DIFFERENT_TYPES_P to true. */
1013 comptypes_check_different_types (tree type1, tree type2,
1014 bool *different_types_p)
1016 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1017 int val;
1019 val = comptypes_internal (type1, type2, NULL, different_types_p);
1020 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1022 return val;
1025 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1026 or various other operations. Return 2 if they are compatible
1027 but a warning may be needed if you use them together. If
1028 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1029 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1030 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1031 NULL, and the types are compatible but different enough not to be
1032 permitted in C11 typedef redeclarations, then this sets
1033 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1034 false, but may or may not be set if the types are incompatible.
1035 This differs from comptypes, in that we don't free the seen
1036 types. */
1038 static int
1039 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1040 bool *different_types_p)
1042 const_tree t1 = type1;
1043 const_tree t2 = type2;
1044 int attrval, val;
1046 /* Suppress errors caused by previously reported errors. */
1048 if (t1 == t2 || !t1 || !t2
1049 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1050 return 1;
1052 /* Enumerated types are compatible with integer types, but this is
1053 not transitive: two enumerated types in the same translation unit
1054 are compatible with each other only if they are the same type. */
1056 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1058 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1059 if (TREE_CODE (t2) != VOID_TYPE)
1061 if (enum_and_int_p != NULL)
1062 *enum_and_int_p = true;
1063 if (different_types_p != NULL)
1064 *different_types_p = true;
1067 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1069 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1070 if (TREE_CODE (t1) != VOID_TYPE)
1072 if (enum_and_int_p != NULL)
1073 *enum_and_int_p = true;
1074 if (different_types_p != NULL)
1075 *different_types_p = true;
1079 if (t1 == t2)
1080 return 1;
1082 /* Different classes of types can't be compatible. */
1084 if (TREE_CODE (t1) != TREE_CODE (t2))
1085 return 0;
1087 /* Qualifiers must match. C99 6.7.3p9 */
1089 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1090 return 0;
1092 /* Allow for two different type nodes which have essentially the same
1093 definition. Note that we already checked for equality of the type
1094 qualifiers (just above). */
1096 if (TREE_CODE (t1) != ARRAY_TYPE
1097 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1098 return 1;
1100 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1101 if (!(attrval = comp_type_attributes (t1, t2)))
1102 return 0;
1104 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1105 val = 0;
1107 switch (TREE_CODE (t1))
1109 case POINTER_TYPE:
1110 /* Do not remove mode or aliasing information. */
1111 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1112 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1113 break;
1114 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1115 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1116 enum_and_int_p, different_types_p));
1117 break;
1119 case FUNCTION_TYPE:
1120 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1121 different_types_p);
1122 break;
1124 case ARRAY_TYPE:
1126 tree d1 = TYPE_DOMAIN (t1);
1127 tree d2 = TYPE_DOMAIN (t2);
1128 bool d1_variable, d2_variable;
1129 bool d1_zero, d2_zero;
1130 val = 1;
1132 /* Target types must match incl. qualifiers. */
1133 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1134 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1135 enum_and_int_p,
1136 different_types_p)))
1137 return 0;
1139 if (different_types_p != NULL
1140 && (d1 == 0) != (d2 == 0))
1141 *different_types_p = true;
1142 /* Sizes must match unless one is missing or variable. */
1143 if (d1 == 0 || d2 == 0 || d1 == d2)
1144 break;
1146 d1_zero = !TYPE_MAX_VALUE (d1);
1147 d2_zero = !TYPE_MAX_VALUE (d2);
1149 d1_variable = (!d1_zero
1150 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1151 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1152 d2_variable = (!d2_zero
1153 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1154 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1155 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1156 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1158 if (different_types_p != NULL
1159 && d1_variable != d2_variable)
1160 *different_types_p = true;
1161 if (d1_variable || d2_variable)
1162 break;
1163 if (d1_zero && d2_zero)
1164 break;
1165 if (d1_zero || d2_zero
1166 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1167 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1168 val = 0;
1170 break;
1173 case ENUMERAL_TYPE:
1174 case RECORD_TYPE:
1175 case UNION_TYPE:
1176 if (val != 1 && !same_translation_unit_p (t1, t2))
1178 tree a1 = TYPE_ATTRIBUTES (t1);
1179 tree a2 = TYPE_ATTRIBUTES (t2);
1181 if (! attribute_list_contained (a1, a2)
1182 && ! attribute_list_contained (a2, a1))
1183 break;
1185 if (attrval != 2)
1186 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1187 different_types_p);
1188 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1189 different_types_p);
1191 break;
1193 case VECTOR_TYPE:
1194 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1195 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1196 enum_and_int_p, different_types_p));
1197 break;
1199 default:
1200 break;
1202 return attrval == 2 && val == 1 ? 2 : val;
1205 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1206 their qualifiers, except for named address spaces. If the pointers point to
1207 different named addresses, then we must determine if one address space is a
1208 subset of the other. */
1210 static int
1211 comp_target_types (location_t location, tree ttl, tree ttr)
1213 int val;
1214 int val_ped;
1215 tree mvl = TREE_TYPE (ttl);
1216 tree mvr = TREE_TYPE (ttr);
1217 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1218 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1219 addr_space_t as_common;
1220 bool enum_and_int_p;
1222 /* Fail if pointers point to incompatible address spaces. */
1223 if (!addr_space_superset (asl, asr, &as_common))
1224 return 0;
1226 /* For pedantic record result of comptypes on arrays before losing
1227 qualifiers on the element type below. */
1228 val_ped = 1;
1230 if (TREE_CODE (mvl) == ARRAY_TYPE
1231 && TREE_CODE (mvr) == ARRAY_TYPE)
1232 val_ped = comptypes (mvl, mvr);
1234 /* Qualifiers on element types of array types that are
1235 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1237 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1238 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1239 : TYPE_MAIN_VARIANT (mvl));
1241 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1242 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1243 : TYPE_MAIN_VARIANT (mvr));
1245 enum_and_int_p = false;
1246 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1248 if (val == 1 && val_ped != 1)
1249 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1250 "are incompatible in ISO C");
1252 if (val == 2)
1253 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1255 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1256 warning_at (location, OPT_Wc___compat,
1257 "pointer target types incompatible in C++");
1259 return val;
1262 /* Subroutines of `comptypes'. */
1264 /* Determine whether two trees derive from the same translation unit.
1265 If the CONTEXT chain ends in a null, that tree's context is still
1266 being parsed, so if two trees have context chains ending in null,
1267 they're in the same translation unit. */
1269 same_translation_unit_p (const_tree t1, const_tree t2)
1271 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1272 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1274 case tcc_declaration:
1275 t1 = DECL_CONTEXT (t1); break;
1276 case tcc_type:
1277 t1 = TYPE_CONTEXT (t1); break;
1278 case tcc_exceptional:
1279 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1280 default: gcc_unreachable ();
1283 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1284 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1286 case tcc_declaration:
1287 t2 = DECL_CONTEXT (t2); break;
1288 case tcc_type:
1289 t2 = TYPE_CONTEXT (t2); break;
1290 case tcc_exceptional:
1291 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1292 default: gcc_unreachable ();
1295 return t1 == t2;
1298 /* Allocate the seen two types, assuming that they are compatible. */
1300 static struct tagged_tu_seen_cache *
1301 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1303 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1304 tu->next = tagged_tu_seen_base;
1305 tu->t1 = t1;
1306 tu->t2 = t2;
1308 tagged_tu_seen_base = tu;
1310 /* The C standard says that two structures in different translation
1311 units are compatible with each other only if the types of their
1312 fields are compatible (among other things). We assume that they
1313 are compatible until proven otherwise when building the cache.
1314 An example where this can occur is:
1315 struct a
1317 struct a *next;
1319 If we are comparing this against a similar struct in another TU,
1320 and did not assume they were compatible, we end up with an infinite
1321 loop. */
1322 tu->val = 1;
1323 return tu;
1326 /* Free the seen types until we get to TU_TIL. */
1328 static void
1329 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1331 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1332 while (tu != tu_til)
1334 const struct tagged_tu_seen_cache *const tu1
1335 = (const struct tagged_tu_seen_cache *) tu;
1336 tu = tu1->next;
1337 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1339 tagged_tu_seen_base = tu_til;
1342 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1343 compatible. If the two types are not the same (which has been
1344 checked earlier), this can only happen when multiple translation
1345 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1346 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1347 comptypes_internal. */
1349 static int
1350 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1351 bool *enum_and_int_p, bool *different_types_p)
1353 tree s1, s2;
1354 bool needs_warning = false;
1356 /* We have to verify that the tags of the types are the same. This
1357 is harder than it looks because this may be a typedef, so we have
1358 to go look at the original type. It may even be a typedef of a
1359 typedef...
1360 In the case of compiler-created builtin structs the TYPE_DECL
1361 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1362 while (TYPE_NAME (t1)
1363 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1364 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1365 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1367 while (TYPE_NAME (t2)
1368 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1369 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1370 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1372 /* C90 didn't have the requirement that the two tags be the same. */
1373 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1374 return 0;
1376 /* C90 didn't say what happened if one or both of the types were
1377 incomplete; we choose to follow C99 rules here, which is that they
1378 are compatible. */
1379 if (TYPE_SIZE (t1) == NULL
1380 || TYPE_SIZE (t2) == NULL)
1381 return 1;
1384 const struct tagged_tu_seen_cache * tts_i;
1385 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1386 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1387 return tts_i->val;
1390 switch (TREE_CODE (t1))
1392 case ENUMERAL_TYPE:
1394 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1395 /* Speed up the case where the type values are in the same order. */
1396 tree tv1 = TYPE_VALUES (t1);
1397 tree tv2 = TYPE_VALUES (t2);
1399 if (tv1 == tv2)
1401 return 1;
1404 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1406 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1407 break;
1408 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1410 tu->val = 0;
1411 return 0;
1415 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1417 return 1;
1419 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1421 tu->val = 0;
1422 return 0;
1425 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1427 tu->val = 0;
1428 return 0;
1431 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1433 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1434 if (s2 == NULL
1435 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1437 tu->val = 0;
1438 return 0;
1441 return 1;
1444 case UNION_TYPE:
1446 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1447 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1449 tu->val = 0;
1450 return 0;
1453 /* Speed up the common case where the fields are in the same order. */
1454 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1455 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1457 int result;
1459 if (DECL_NAME (s1) != DECL_NAME (s2))
1460 break;
1461 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1462 enum_and_int_p, different_types_p);
1464 if (result != 1 && !DECL_NAME (s1))
1465 break;
1466 if (result == 0)
1468 tu->val = 0;
1469 return 0;
1471 if (result == 2)
1472 needs_warning = true;
1474 if (TREE_CODE (s1) == FIELD_DECL
1475 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1476 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1478 tu->val = 0;
1479 return 0;
1482 if (!s1 && !s2)
1484 tu->val = needs_warning ? 2 : 1;
1485 return tu->val;
1488 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1490 bool ok = false;
1492 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1493 if (DECL_NAME (s1) == DECL_NAME (s2))
1495 int result;
1497 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1498 enum_and_int_p,
1499 different_types_p);
1501 if (result != 1 && !DECL_NAME (s1))
1502 continue;
1503 if (result == 0)
1505 tu->val = 0;
1506 return 0;
1508 if (result == 2)
1509 needs_warning = true;
1511 if (TREE_CODE (s1) == FIELD_DECL
1512 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1513 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1514 break;
1516 ok = true;
1517 break;
1519 if (!ok)
1521 tu->val = 0;
1522 return 0;
1525 tu->val = needs_warning ? 2 : 10;
1526 return tu->val;
1529 case RECORD_TYPE:
1531 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1533 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1534 s1 && s2;
1535 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1537 int result;
1538 if (TREE_CODE (s1) != TREE_CODE (s2)
1539 || DECL_NAME (s1) != DECL_NAME (s2))
1540 break;
1541 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1542 enum_and_int_p, different_types_p);
1543 if (result == 0)
1544 break;
1545 if (result == 2)
1546 needs_warning = true;
1548 if (TREE_CODE (s1) == FIELD_DECL
1549 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1550 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1551 break;
1553 if (s1 && s2)
1554 tu->val = 0;
1555 else
1556 tu->val = needs_warning ? 2 : 1;
1557 return tu->val;
1560 default:
1561 gcc_unreachable ();
1565 /* Return 1 if two function types F1 and F2 are compatible.
1566 If either type specifies no argument types,
1567 the other must specify a fixed number of self-promoting arg types.
1568 Otherwise, if one type specifies only the number of arguments,
1569 the other must specify that number of self-promoting arg types.
1570 Otherwise, the argument types must match.
1571 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1573 static int
1574 function_types_compatible_p (const_tree f1, const_tree f2,
1575 bool *enum_and_int_p, bool *different_types_p)
1577 tree args1, args2;
1578 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1579 int val = 1;
1580 int val1;
1581 tree ret1, ret2;
1583 ret1 = TREE_TYPE (f1);
1584 ret2 = TREE_TYPE (f2);
1586 /* 'volatile' qualifiers on a function's return type used to mean
1587 the function is noreturn. */
1588 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1589 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1590 if (TYPE_VOLATILE (ret1))
1591 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1592 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1593 if (TYPE_VOLATILE (ret2))
1594 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1595 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1596 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1597 if (val == 0)
1598 return 0;
1600 args1 = TYPE_ARG_TYPES (f1);
1601 args2 = TYPE_ARG_TYPES (f2);
1603 if (different_types_p != NULL
1604 && (args1 == 0) != (args2 == 0))
1605 *different_types_p = true;
1607 /* An unspecified parmlist matches any specified parmlist
1608 whose argument types don't need default promotions. */
1610 if (args1 == 0)
1612 if (!self_promoting_args_p (args2))
1613 return 0;
1614 /* If one of these types comes from a non-prototype fn definition,
1615 compare that with the other type's arglist.
1616 If they don't match, ask for a warning (but no error). */
1617 if (TYPE_ACTUAL_ARG_TYPES (f1)
1618 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1619 enum_and_int_p, different_types_p))
1620 val = 2;
1621 return val;
1623 if (args2 == 0)
1625 if (!self_promoting_args_p (args1))
1626 return 0;
1627 if (TYPE_ACTUAL_ARG_TYPES (f2)
1628 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1629 enum_and_int_p, different_types_p))
1630 val = 2;
1631 return val;
1634 /* Both types have argument lists: compare them and propagate results. */
1635 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1636 different_types_p);
1637 return val1 != 1 ? val1 : val;
1640 /* Check two lists of types for compatibility, returning 0 for
1641 incompatible, 1 for compatible, or 2 for compatible with
1642 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1643 comptypes_internal. */
1645 static int
1646 type_lists_compatible_p (const_tree args1, const_tree args2,
1647 bool *enum_and_int_p, bool *different_types_p)
1649 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1650 int val = 1;
1651 int newval = 0;
1653 while (1)
1655 tree a1, mv1, a2, mv2;
1656 if (args1 == 0 && args2 == 0)
1657 return val;
1658 /* If one list is shorter than the other,
1659 they fail to match. */
1660 if (args1 == 0 || args2 == 0)
1661 return 0;
1662 mv1 = a1 = TREE_VALUE (args1);
1663 mv2 = a2 = TREE_VALUE (args2);
1664 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1665 mv1 = (TYPE_ATOMIC (mv1)
1666 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1667 TYPE_QUAL_ATOMIC)
1668 : TYPE_MAIN_VARIANT (mv1));
1669 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1670 mv2 = (TYPE_ATOMIC (mv2)
1671 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1672 TYPE_QUAL_ATOMIC)
1673 : TYPE_MAIN_VARIANT (mv2));
1674 /* A null pointer instead of a type
1675 means there is supposed to be an argument
1676 but nothing is specified about what type it has.
1677 So match anything that self-promotes. */
1678 if (different_types_p != NULL
1679 && (a1 == 0) != (a2 == 0))
1680 *different_types_p = true;
1681 if (a1 == 0)
1683 if (c_type_promotes_to (a2) != a2)
1684 return 0;
1686 else if (a2 == 0)
1688 if (c_type_promotes_to (a1) != a1)
1689 return 0;
1691 /* If one of the lists has an error marker, ignore this arg. */
1692 else if (TREE_CODE (a1) == ERROR_MARK
1693 || TREE_CODE (a2) == ERROR_MARK)
1695 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1696 different_types_p)))
1698 if (different_types_p != NULL)
1699 *different_types_p = true;
1700 /* Allow wait (union {union wait *u; int *i} *)
1701 and wait (union wait *) to be compatible. */
1702 if (TREE_CODE (a1) == UNION_TYPE
1703 && (TYPE_NAME (a1) == 0
1704 || TYPE_TRANSPARENT_AGGR (a1))
1705 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1706 && tree_int_cst_equal (TYPE_SIZE (a1),
1707 TYPE_SIZE (a2)))
1709 tree memb;
1710 for (memb = TYPE_FIELDS (a1);
1711 memb; memb = DECL_CHAIN (memb))
1713 tree mv3 = TREE_TYPE (memb);
1714 if (mv3 && mv3 != error_mark_node
1715 && TREE_CODE (mv3) != ARRAY_TYPE)
1716 mv3 = (TYPE_ATOMIC (mv3)
1717 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1718 TYPE_QUAL_ATOMIC)
1719 : TYPE_MAIN_VARIANT (mv3));
1720 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1721 different_types_p))
1722 break;
1724 if (memb == 0)
1725 return 0;
1727 else if (TREE_CODE (a2) == UNION_TYPE
1728 && (TYPE_NAME (a2) == 0
1729 || TYPE_TRANSPARENT_AGGR (a2))
1730 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1731 && tree_int_cst_equal (TYPE_SIZE (a2),
1732 TYPE_SIZE (a1)))
1734 tree memb;
1735 for (memb = TYPE_FIELDS (a2);
1736 memb; memb = DECL_CHAIN (memb))
1738 tree mv3 = TREE_TYPE (memb);
1739 if (mv3 && mv3 != error_mark_node
1740 && TREE_CODE (mv3) != ARRAY_TYPE)
1741 mv3 = (TYPE_ATOMIC (mv3)
1742 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1743 TYPE_QUAL_ATOMIC)
1744 : TYPE_MAIN_VARIANT (mv3));
1745 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1746 different_types_p))
1747 break;
1749 if (memb == 0)
1750 return 0;
1752 else
1753 return 0;
1756 /* comptypes said ok, but record if it said to warn. */
1757 if (newval > val)
1758 val = newval;
1760 args1 = TREE_CHAIN (args1);
1761 args2 = TREE_CHAIN (args2);
1765 /* Compute the size to increment a pointer by. When a function type or void
1766 type or incomplete type is passed, size_one_node is returned.
1767 This function does not emit any diagnostics; the caller is responsible
1768 for that. */
1770 static tree
1771 c_size_in_bytes (const_tree type)
1773 enum tree_code code = TREE_CODE (type);
1775 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1776 || !COMPLETE_TYPE_P (type))
1777 return size_one_node;
1779 /* Convert in case a char is more than one unit. */
1780 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1781 size_int (TYPE_PRECISION (char_type_node)
1782 / BITS_PER_UNIT));
1785 /* Return either DECL or its known constant value (if it has one). */
1787 tree
1788 decl_constant_value (tree decl)
1790 if (/* Don't change a variable array bound or initial value to a constant
1791 in a place where a variable is invalid. Note that DECL_INITIAL
1792 isn't valid for a PARM_DECL. */
1793 current_function_decl != 0
1794 && TREE_CODE (decl) != PARM_DECL
1795 && !TREE_THIS_VOLATILE (decl)
1796 && TREE_READONLY (decl)
1797 && DECL_INITIAL (decl) != 0
1798 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1799 /* This is invalid if initial value is not constant.
1800 If it has either a function call, a memory reference,
1801 or a variable, then re-evaluating it could give different results. */
1802 && TREE_CONSTANT (DECL_INITIAL (decl))
1803 /* Check for cases where this is sub-optimal, even though valid. */
1804 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1805 return DECL_INITIAL (decl);
1806 return decl;
1809 /* Convert the array expression EXP to a pointer. */
1810 static tree
1811 array_to_pointer_conversion (location_t loc, tree exp)
1813 tree orig_exp = exp;
1814 tree type = TREE_TYPE (exp);
1815 tree adr;
1816 tree restype = TREE_TYPE (type);
1817 tree ptrtype;
1819 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1821 STRIP_TYPE_NOPS (exp);
1823 if (TREE_NO_WARNING (orig_exp))
1824 TREE_NO_WARNING (exp) = 1;
1826 ptrtype = build_pointer_type (restype);
1828 if (INDIRECT_REF_P (exp))
1829 return convert (ptrtype, TREE_OPERAND (exp, 0));
1831 /* In C++ array compound literals are temporary objects unless they are
1832 const or appear in namespace scope, so they are destroyed too soon
1833 to use them for much of anything (c++/53220). */
1834 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1836 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1837 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1838 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1839 "converting an array compound literal to a pointer "
1840 "is ill-formed in C++");
1843 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1844 return convert (ptrtype, adr);
1847 /* Convert the function expression EXP to a pointer. */
1848 static tree
1849 function_to_pointer_conversion (location_t loc, tree exp)
1851 tree orig_exp = exp;
1853 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1855 STRIP_TYPE_NOPS (exp);
1857 if (TREE_NO_WARNING (orig_exp))
1858 TREE_NO_WARNING (exp) = 1;
1860 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1863 /* Mark EXP as read, not just set, for set but not used -Wunused
1864 warning purposes. */
1866 void
1867 mark_exp_read (tree exp)
1869 switch (TREE_CODE (exp))
1871 case VAR_DECL:
1872 case PARM_DECL:
1873 DECL_READ_P (exp) = 1;
1874 break;
1875 case ARRAY_REF:
1876 case COMPONENT_REF:
1877 case MODIFY_EXPR:
1878 case REALPART_EXPR:
1879 case IMAGPART_EXPR:
1880 CASE_CONVERT:
1881 case ADDR_EXPR:
1882 mark_exp_read (TREE_OPERAND (exp, 0));
1883 break;
1884 case COMPOUND_EXPR:
1885 case C_MAYBE_CONST_EXPR:
1886 mark_exp_read (TREE_OPERAND (exp, 1));
1887 break;
1888 default:
1889 break;
1893 /* Perform the default conversion of arrays and functions to pointers.
1894 Return the result of converting EXP. For any other expression, just
1895 return EXP.
1897 LOC is the location of the expression. */
1899 struct c_expr
1900 default_function_array_conversion (location_t loc, struct c_expr exp)
1902 tree orig_exp = exp.value;
1903 tree type = TREE_TYPE (exp.value);
1904 enum tree_code code = TREE_CODE (type);
1906 switch (code)
1908 case ARRAY_TYPE:
1910 bool not_lvalue = false;
1911 bool lvalue_array_p;
1913 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1914 || CONVERT_EXPR_P (exp.value))
1915 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1917 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1918 not_lvalue = true;
1919 exp.value = TREE_OPERAND (exp.value, 0);
1922 if (TREE_NO_WARNING (orig_exp))
1923 TREE_NO_WARNING (exp.value) = 1;
1925 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1926 if (!flag_isoc99 && !lvalue_array_p)
1928 /* Before C99, non-lvalue arrays do not decay to pointers.
1929 Normally, using such an array would be invalid; but it can
1930 be used correctly inside sizeof or as a statement expression.
1931 Thus, do not give an error here; an error will result later. */
1932 return exp;
1935 exp.value = array_to_pointer_conversion (loc, exp.value);
1937 break;
1938 case FUNCTION_TYPE:
1939 exp.value = function_to_pointer_conversion (loc, exp.value);
1940 break;
1941 default:
1942 break;
1945 return exp;
1948 struct c_expr
1949 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1951 mark_exp_read (exp.value);
1952 return default_function_array_conversion (loc, exp);
1955 /* Return whether EXPR should be treated as an atomic lvalue for the
1956 purposes of load and store handling. */
1958 static bool
1959 really_atomic_lvalue (tree expr)
1961 if (error_operand_p (expr))
1962 return false;
1963 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1964 return false;
1965 if (!lvalue_p (expr))
1966 return false;
1968 /* Ignore _Atomic on register variables, since their addresses can't
1969 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1970 sequences wouldn't work. Ignore _Atomic on structures containing
1971 bit-fields, since accessing elements of atomic structures or
1972 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1973 it's undefined at translation time or execution time, and the
1974 normal atomic sequences again wouldn't work. */
1975 while (handled_component_p (expr))
1977 if (TREE_CODE (expr) == COMPONENT_REF
1978 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1979 return false;
1980 expr = TREE_OPERAND (expr, 0);
1982 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1983 return false;
1984 return true;
1987 /* Convert expression EXP (location LOC) from lvalue to rvalue,
1988 including converting functions and arrays to pointers if CONVERT_P.
1989 If READ_P, also mark the expression as having been read. */
1991 struct c_expr
1992 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1993 bool convert_p, bool read_p)
1995 if (read_p)
1996 mark_exp_read (exp.value);
1997 if (convert_p)
1998 exp = default_function_array_conversion (loc, exp);
1999 if (really_atomic_lvalue (exp.value))
2001 vec<tree, va_gc> *params;
2002 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2003 tree expr_type = TREE_TYPE (exp.value);
2004 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2005 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2007 gcc_assert (TYPE_ATOMIC (expr_type));
2009 /* Expansion of a generic atomic load may require an addition
2010 element, so allocate enough to prevent a resize. */
2011 vec_alloc (params, 4);
2013 /* Remove the qualifiers for the rest of the expressions and
2014 create the VAL temp variable to hold the RHS. */
2015 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2016 tmp = create_tmp_var_raw (nonatomic_type);
2017 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2018 TREE_ADDRESSABLE (tmp) = 1;
2019 TREE_NO_WARNING (tmp) = 1;
2021 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2022 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2023 params->quick_push (expr_addr);
2024 params->quick_push (tmp_addr);
2025 params->quick_push (seq_cst);
2026 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2028 /* EXPR is always read. */
2029 mark_exp_read (exp.value);
2031 /* Return tmp which contains the value loaded. */
2032 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2033 NULL_TREE, NULL_TREE);
2035 return exp;
2038 /* EXP is an expression of integer type. Apply the integer promotions
2039 to it and return the promoted value. */
2041 tree
2042 perform_integral_promotions (tree exp)
2044 tree type = TREE_TYPE (exp);
2045 enum tree_code code = TREE_CODE (type);
2047 gcc_assert (INTEGRAL_TYPE_P (type));
2049 /* Normally convert enums to int,
2050 but convert wide enums to something wider. */
2051 if (code == ENUMERAL_TYPE)
2053 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2054 TYPE_PRECISION (integer_type_node)),
2055 ((TYPE_PRECISION (type)
2056 >= TYPE_PRECISION (integer_type_node))
2057 && TYPE_UNSIGNED (type)));
2059 return convert (type, exp);
2062 /* ??? This should no longer be needed now bit-fields have their
2063 proper types. */
2064 if (TREE_CODE (exp) == COMPONENT_REF
2065 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2066 /* If it's thinner than an int, promote it like a
2067 c_promoting_integer_type_p, otherwise leave it alone. */
2068 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2069 TYPE_PRECISION (integer_type_node)))
2070 return convert (integer_type_node, exp);
2072 if (c_promoting_integer_type_p (type))
2074 /* Preserve unsignedness if not really getting any wider. */
2075 if (TYPE_UNSIGNED (type)
2076 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2077 return convert (unsigned_type_node, exp);
2079 return convert (integer_type_node, exp);
2082 return exp;
2086 /* Perform default promotions for C data used in expressions.
2087 Enumeral types or short or char are converted to int.
2088 In addition, manifest constants symbols are replaced by their values. */
2090 tree
2091 default_conversion (tree exp)
2093 tree orig_exp;
2094 tree type = TREE_TYPE (exp);
2095 enum tree_code code = TREE_CODE (type);
2096 tree promoted_type;
2098 mark_exp_read (exp);
2100 /* Functions and arrays have been converted during parsing. */
2101 gcc_assert (code != FUNCTION_TYPE);
2102 if (code == ARRAY_TYPE)
2103 return exp;
2105 /* Constants can be used directly unless they're not loadable. */
2106 if (TREE_CODE (exp) == CONST_DECL)
2107 exp = DECL_INITIAL (exp);
2109 /* Strip no-op conversions. */
2110 orig_exp = exp;
2111 STRIP_TYPE_NOPS (exp);
2113 if (TREE_NO_WARNING (orig_exp))
2114 TREE_NO_WARNING (exp) = 1;
2116 if (code == VOID_TYPE)
2118 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2119 "void value not ignored as it ought to be");
2120 return error_mark_node;
2123 exp = require_complete_type (exp);
2124 if (exp == error_mark_node)
2125 return error_mark_node;
2127 promoted_type = targetm.promoted_type (type);
2128 if (promoted_type)
2129 return convert (promoted_type, exp);
2131 if (INTEGRAL_TYPE_P (type))
2132 return perform_integral_promotions (exp);
2134 return exp;
2137 /* Look up COMPONENT in a structure or union TYPE.
2139 If the component name is not found, returns NULL_TREE. Otherwise,
2140 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2141 stepping down the chain to the component, which is in the last
2142 TREE_VALUE of the list. Normally the list is of length one, but if
2143 the component is embedded within (nested) anonymous structures or
2144 unions, the list steps down the chain to the component. */
2146 static tree
2147 lookup_field (tree type, tree component)
2149 tree field;
2151 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2152 to the field elements. Use a binary search on this array to quickly
2153 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2154 will always be set for structures which have many elements. */
2156 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2158 int bot, top, half;
2159 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2161 field = TYPE_FIELDS (type);
2162 bot = 0;
2163 top = TYPE_LANG_SPECIFIC (type)->s->len;
2164 while (top - bot > 1)
2166 half = (top - bot + 1) >> 1;
2167 field = field_array[bot+half];
2169 if (DECL_NAME (field) == NULL_TREE)
2171 /* Step through all anon unions in linear fashion. */
2172 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2174 field = field_array[bot++];
2175 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2176 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2178 tree anon = lookup_field (TREE_TYPE (field), component);
2180 if (anon)
2181 return tree_cons (NULL_TREE, field, anon);
2183 /* The Plan 9 compiler permits referring
2184 directly to an anonymous struct/union field
2185 using a typedef name. */
2186 if (flag_plan9_extensions
2187 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2188 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2189 == TYPE_DECL)
2190 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2191 == component))
2192 break;
2196 /* Entire record is only anon unions. */
2197 if (bot > top)
2198 return NULL_TREE;
2200 /* Restart the binary search, with new lower bound. */
2201 continue;
2204 if (DECL_NAME (field) == component)
2205 break;
2206 if (DECL_NAME (field) < component)
2207 bot += half;
2208 else
2209 top = bot + half;
2212 if (DECL_NAME (field_array[bot]) == component)
2213 field = field_array[bot];
2214 else if (DECL_NAME (field) != component)
2215 return NULL_TREE;
2217 else
2219 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2221 if (DECL_NAME (field) == NULL_TREE
2222 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2223 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2225 tree anon = lookup_field (TREE_TYPE (field), component);
2227 if (anon)
2228 return tree_cons (NULL_TREE, field, anon);
2230 /* The Plan 9 compiler permits referring directly to an
2231 anonymous struct/union field using a typedef
2232 name. */
2233 if (flag_plan9_extensions
2234 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2235 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2236 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2237 == component))
2238 break;
2241 if (DECL_NAME (field) == component)
2242 break;
2245 if (field == NULL_TREE)
2246 return NULL_TREE;
2249 return tree_cons (NULL_TREE, field, NULL_TREE);
2252 /* Make an expression to refer to the COMPONENT field of structure or
2253 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2254 location of the COMPONENT_REF. */
2256 tree
2257 build_component_ref (location_t loc, tree datum, tree component)
2259 tree type = TREE_TYPE (datum);
2260 enum tree_code code = TREE_CODE (type);
2261 tree field = NULL;
2262 tree ref;
2263 bool datum_lvalue = lvalue_p (datum);
2265 if (!objc_is_public (datum, component))
2266 return error_mark_node;
2268 /* Detect Objective-C property syntax object.property. */
2269 if (c_dialect_objc ()
2270 && (ref = objc_maybe_build_component_ref (datum, component)))
2271 return ref;
2273 /* See if there is a field or component with name COMPONENT. */
2275 if (code == RECORD_TYPE || code == UNION_TYPE)
2277 if (!COMPLETE_TYPE_P (type))
2279 c_incomplete_type_error (NULL_TREE, type);
2280 return error_mark_node;
2283 field = lookup_field (type, component);
2285 if (!field)
2287 error_at (loc, "%qT has no member named %qE", type, component);
2288 return error_mark_node;
2291 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2292 This might be better solved in future the way the C++ front
2293 end does it - by giving the anonymous entities each a
2294 separate name and type, and then have build_component_ref
2295 recursively call itself. We can't do that here. */
2298 tree subdatum = TREE_VALUE (field);
2299 int quals;
2300 tree subtype;
2301 bool use_datum_quals;
2303 if (TREE_TYPE (subdatum) == error_mark_node)
2304 return error_mark_node;
2306 /* If this is an rvalue, it does not have qualifiers in C
2307 standard terms and we must avoid propagating such
2308 qualifiers down to a non-lvalue array that is then
2309 converted to a pointer. */
2310 use_datum_quals = (datum_lvalue
2311 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2313 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2314 if (use_datum_quals)
2315 quals |= TYPE_QUALS (TREE_TYPE (datum));
2316 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2318 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2319 NULL_TREE);
2320 SET_EXPR_LOCATION (ref, loc);
2321 if (TREE_READONLY (subdatum)
2322 || (use_datum_quals && TREE_READONLY (datum)))
2323 TREE_READONLY (ref) = 1;
2324 if (TREE_THIS_VOLATILE (subdatum)
2325 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2326 TREE_THIS_VOLATILE (ref) = 1;
2328 if (TREE_DEPRECATED (subdatum))
2329 warn_deprecated_use (subdatum, NULL_TREE);
2331 datum = ref;
2333 field = TREE_CHAIN (field);
2335 while (field);
2337 return ref;
2339 else if (code != ERROR_MARK)
2340 error_at (loc,
2341 "request for member %qE in something not a structure or union",
2342 component);
2344 return error_mark_node;
2347 /* Given an expression PTR for a pointer, return an expression
2348 for the value pointed to.
2349 ERRORSTRING is the name of the operator to appear in error messages.
2351 LOC is the location to use for the generated tree. */
2353 tree
2354 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2356 tree pointer = default_conversion (ptr);
2357 tree type = TREE_TYPE (pointer);
2358 tree ref;
2360 if (TREE_CODE (type) == POINTER_TYPE)
2362 if (CONVERT_EXPR_P (pointer)
2363 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2365 /* If a warning is issued, mark it to avoid duplicates from
2366 the backend. This only needs to be done at
2367 warn_strict_aliasing > 2. */
2368 if (warn_strict_aliasing > 2)
2369 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2370 type, TREE_OPERAND (pointer, 0)))
2371 TREE_NO_WARNING (pointer) = 1;
2374 if (TREE_CODE (pointer) == ADDR_EXPR
2375 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2376 == TREE_TYPE (type)))
2378 ref = TREE_OPERAND (pointer, 0);
2379 protected_set_expr_location (ref, loc);
2380 return ref;
2382 else
2384 tree t = TREE_TYPE (type);
2386 ref = build1 (INDIRECT_REF, t, pointer);
2388 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2390 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2392 error_at (loc, "dereferencing pointer to incomplete type "
2393 "%qT", t);
2394 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2396 return error_mark_node;
2398 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2399 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2401 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2402 so that we get the proper error message if the result is used
2403 to assign to. Also, &* is supposed to be a no-op.
2404 And ANSI C seems to specify that the type of the result
2405 should be the const type. */
2406 /* A de-reference of a pointer to const is not a const. It is valid
2407 to change it via some other pointer. */
2408 TREE_READONLY (ref) = TYPE_READONLY (t);
2409 TREE_SIDE_EFFECTS (ref)
2410 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2411 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2412 protected_set_expr_location (ref, loc);
2413 return ref;
2416 else if (TREE_CODE (pointer) != ERROR_MARK)
2417 invalid_indirection_error (loc, type, errstring);
2419 return error_mark_node;
2422 /* This handles expressions of the form "a[i]", which denotes
2423 an array reference.
2425 This is logically equivalent in C to *(a+i), but we may do it differently.
2426 If A is a variable or a member, we generate a primitive ARRAY_REF.
2427 This avoids forcing the array out of registers, and can work on
2428 arrays that are not lvalues (for example, members of structures returned
2429 by functions).
2431 For vector types, allow vector[i] but not i[vector], and create
2432 *(((type*)&vectortype) + i) for the expression.
2434 LOC is the location to use for the returned expression. */
2436 tree
2437 build_array_ref (location_t loc, tree array, tree index)
2439 tree ret;
2440 bool swapped = false;
2441 if (TREE_TYPE (array) == error_mark_node
2442 || TREE_TYPE (index) == error_mark_node)
2443 return error_mark_node;
2445 if (flag_cilkplus && contains_array_notation_expr (index))
2447 size_t rank = 0;
2448 if (!find_rank (loc, index, index, true, &rank))
2449 return error_mark_node;
2450 if (rank > 1)
2452 error_at (loc, "rank of the array's index is greater than 1");
2453 return error_mark_node;
2456 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2457 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2458 /* Allow vector[index] but not index[vector]. */
2459 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2461 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2462 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2464 error_at (loc,
2465 "subscripted value is neither array nor pointer nor vector");
2467 return error_mark_node;
2469 std::swap (array, index);
2470 swapped = true;
2473 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2475 error_at (loc, "array subscript is not an integer");
2476 return error_mark_node;
2479 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2481 error_at (loc, "subscripted value is pointer to function");
2482 return error_mark_node;
2485 /* ??? Existing practice has been to warn only when the char
2486 index is syntactically the index, not for char[array]. */
2487 if (!swapped)
2488 warn_array_subscript_with_type_char (loc, index);
2490 /* Apply default promotions *after* noticing character types. */
2491 index = default_conversion (index);
2492 if (index == error_mark_node)
2493 return error_mark_node;
2495 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2497 bool non_lvalue
2498 = convert_vector_to_pointer_for_subscript (loc, &array, index);
2500 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2502 tree rval, type;
2504 /* An array that is indexed by a non-constant
2505 cannot be stored in a register; we must be able to do
2506 address arithmetic on its address.
2507 Likewise an array of elements of variable size. */
2508 if (TREE_CODE (index) != INTEGER_CST
2509 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2510 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2512 if (!c_mark_addressable (array))
2513 return error_mark_node;
2515 /* An array that is indexed by a constant value which is not within
2516 the array bounds cannot be stored in a register either; because we
2517 would get a crash in store_bit_field/extract_bit_field when trying
2518 to access a non-existent part of the register. */
2519 if (TREE_CODE (index) == INTEGER_CST
2520 && TYPE_DOMAIN (TREE_TYPE (array))
2521 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2523 if (!c_mark_addressable (array))
2524 return error_mark_node;
2527 if (pedantic || warn_c90_c99_compat)
2529 tree foo = array;
2530 while (TREE_CODE (foo) == COMPONENT_REF)
2531 foo = TREE_OPERAND (foo, 0);
2532 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2533 pedwarn (loc, OPT_Wpedantic,
2534 "ISO C forbids subscripting %<register%> array");
2535 else if (!lvalue_p (foo))
2536 pedwarn_c90 (loc, OPT_Wpedantic,
2537 "ISO C90 forbids subscripting non-lvalue "
2538 "array");
2541 type = TREE_TYPE (TREE_TYPE (array));
2542 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2543 /* Array ref is const/volatile if the array elements are
2544 or if the array is. */
2545 TREE_READONLY (rval)
2546 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2547 | TREE_READONLY (array));
2548 TREE_SIDE_EFFECTS (rval)
2549 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2550 | TREE_SIDE_EFFECTS (array));
2551 TREE_THIS_VOLATILE (rval)
2552 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2553 /* This was added by rms on 16 Nov 91.
2554 It fixes vol struct foo *a; a->elts[1]
2555 in an inline function.
2556 Hope it doesn't break something else. */
2557 | TREE_THIS_VOLATILE (array));
2558 ret = require_complete_type (rval);
2559 protected_set_expr_location (ret, loc);
2560 if (non_lvalue)
2561 ret = non_lvalue_loc (loc, ret);
2562 return ret;
2564 else
2566 tree ar = default_conversion (array);
2568 if (ar == error_mark_node)
2569 return ar;
2571 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2572 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2574 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2575 index, 0),
2576 RO_ARRAY_INDEXING);
2577 if (non_lvalue)
2578 ret = non_lvalue_loc (loc, ret);
2579 return ret;
2583 /* Build an external reference to identifier ID. FUN indicates
2584 whether this will be used for a function call. LOC is the source
2585 location of the identifier. This sets *TYPE to the type of the
2586 identifier, which is not the same as the type of the returned value
2587 for CONST_DECLs defined as enum constants. If the type of the
2588 identifier is not available, *TYPE is set to NULL. */
2589 tree
2590 build_external_ref (location_t loc, tree id, int fun, tree *type)
2592 tree ref;
2593 tree decl = lookup_name (id);
2595 /* In Objective-C, an instance variable (ivar) may be preferred to
2596 whatever lookup_name() found. */
2597 decl = objc_lookup_ivar (decl, id);
2599 *type = NULL;
2600 if (decl && decl != error_mark_node)
2602 ref = decl;
2603 *type = TREE_TYPE (ref);
2605 else if (fun)
2606 /* Implicit function declaration. */
2607 ref = implicitly_declare (loc, id);
2608 else if (decl == error_mark_node)
2609 /* Don't complain about something that's already been
2610 complained about. */
2611 return error_mark_node;
2612 else
2614 undeclared_variable (loc, id);
2615 return error_mark_node;
2618 if (TREE_TYPE (ref) == error_mark_node)
2619 return error_mark_node;
2621 if (TREE_DEPRECATED (ref))
2622 warn_deprecated_use (ref, NULL_TREE);
2624 /* Recursive call does not count as usage. */
2625 if (ref != current_function_decl)
2627 TREE_USED (ref) = 1;
2630 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2632 if (!in_sizeof && !in_typeof)
2633 C_DECL_USED (ref) = 1;
2634 else if (DECL_INITIAL (ref) == 0
2635 && DECL_EXTERNAL (ref)
2636 && !TREE_PUBLIC (ref))
2637 record_maybe_used_decl (ref);
2640 if (TREE_CODE (ref) == CONST_DECL)
2642 used_types_insert (TREE_TYPE (ref));
2644 if (warn_cxx_compat
2645 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2646 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2648 warning_at (loc, OPT_Wc___compat,
2649 ("enum constant defined in struct or union "
2650 "is not visible in C++"));
2651 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2654 ref = DECL_INITIAL (ref);
2655 TREE_CONSTANT (ref) = 1;
2657 else if (current_function_decl != 0
2658 && !DECL_FILE_SCOPE_P (current_function_decl)
2659 && (VAR_OR_FUNCTION_DECL_P (ref)
2660 || TREE_CODE (ref) == PARM_DECL))
2662 tree context = decl_function_context (ref);
2664 if (context != 0 && context != current_function_decl)
2665 DECL_NONLOCAL (ref) = 1;
2667 /* C99 6.7.4p3: An inline definition of a function with external
2668 linkage ... shall not contain a reference to an identifier with
2669 internal linkage. */
2670 else if (current_function_decl != 0
2671 && DECL_DECLARED_INLINE_P (current_function_decl)
2672 && DECL_EXTERNAL (current_function_decl)
2673 && VAR_OR_FUNCTION_DECL_P (ref)
2674 && (!VAR_P (ref) || TREE_STATIC (ref))
2675 && ! TREE_PUBLIC (ref)
2676 && DECL_CONTEXT (ref) != current_function_decl)
2677 record_inline_static (loc, current_function_decl, ref,
2678 csi_internal);
2680 return ref;
2683 /* Record details of decls possibly used inside sizeof or typeof. */
2684 struct maybe_used_decl
2686 /* The decl. */
2687 tree decl;
2688 /* The level seen at (in_sizeof + in_typeof). */
2689 int level;
2690 /* The next one at this level or above, or NULL. */
2691 struct maybe_used_decl *next;
2694 static struct maybe_used_decl *maybe_used_decls;
2696 /* Record that DECL, an undefined static function reference seen
2697 inside sizeof or typeof, might be used if the operand of sizeof is
2698 a VLA type or the operand of typeof is a variably modified
2699 type. */
2701 static void
2702 record_maybe_used_decl (tree decl)
2704 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2705 t->decl = decl;
2706 t->level = in_sizeof + in_typeof;
2707 t->next = maybe_used_decls;
2708 maybe_used_decls = t;
2711 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2712 USED is false, just discard them. If it is true, mark them used
2713 (if no longer inside sizeof or typeof) or move them to the next
2714 level up (if still inside sizeof or typeof). */
2716 void
2717 pop_maybe_used (bool used)
2719 struct maybe_used_decl *p = maybe_used_decls;
2720 int cur_level = in_sizeof + in_typeof;
2721 while (p && p->level > cur_level)
2723 if (used)
2725 if (cur_level == 0)
2726 C_DECL_USED (p->decl) = 1;
2727 else
2728 p->level = cur_level;
2730 p = p->next;
2732 if (!used || cur_level == 0)
2733 maybe_used_decls = p;
2736 /* Return the result of sizeof applied to EXPR. */
2738 struct c_expr
2739 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2741 struct c_expr ret;
2742 if (expr.value == error_mark_node)
2744 ret.value = error_mark_node;
2745 ret.original_code = ERROR_MARK;
2746 ret.original_type = NULL;
2747 pop_maybe_used (false);
2749 else
2751 bool expr_const_operands = true;
2753 if (TREE_CODE (expr.value) == PARM_DECL
2754 && C_ARRAY_PARAMETER (expr.value))
2756 if (warning_at (loc, OPT_Wsizeof_array_argument,
2757 "%<sizeof%> on array function parameter %qE will "
2758 "return size of %qT", expr.value,
2759 expr.original_type))
2760 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2762 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2763 &expr_const_operands);
2764 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2765 c_last_sizeof_arg = expr.value;
2766 ret.original_code = SIZEOF_EXPR;
2767 ret.original_type = NULL;
2768 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2770 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2771 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2772 folded_expr, ret.value);
2773 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2774 SET_EXPR_LOCATION (ret.value, loc);
2776 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2778 return ret;
2781 /* Return the result of sizeof applied to T, a structure for the type
2782 name passed to sizeof (rather than the type itself). LOC is the
2783 location of the original expression. */
2785 struct c_expr
2786 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2788 tree type;
2789 struct c_expr ret;
2790 tree type_expr = NULL_TREE;
2791 bool type_expr_const = true;
2792 type = groktypename (t, &type_expr, &type_expr_const);
2793 ret.value = c_sizeof (loc, type);
2794 c_last_sizeof_arg = type;
2795 ret.original_code = SIZEOF_EXPR;
2796 ret.original_type = NULL;
2797 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2798 && c_vla_type_p (type))
2800 /* If the type is a [*] array, it is a VLA but is represented as
2801 having a size of zero. In such a case we must ensure that
2802 the result of sizeof does not get folded to a constant by
2803 c_fully_fold, because if the size is evaluated the result is
2804 not constant and so constraints on zero or negative size
2805 arrays must not be applied when this sizeof call is inside
2806 another array declarator. */
2807 if (!type_expr)
2808 type_expr = integer_zero_node;
2809 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2810 type_expr, ret.value);
2811 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2813 pop_maybe_used (type != error_mark_node
2814 ? C_TYPE_VARIABLE_SIZE (type) : false);
2815 return ret;
2818 /* Build a function call to function FUNCTION with parameters PARAMS.
2819 The function call is at LOC.
2820 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2821 TREE_VALUE of each node is a parameter-expression.
2822 FUNCTION's data type may be a function type or a pointer-to-function. */
2824 tree
2825 build_function_call (location_t loc, tree function, tree params)
2827 vec<tree, va_gc> *v;
2828 tree ret;
2830 vec_alloc (v, list_length (params));
2831 for (; params; params = TREE_CHAIN (params))
2832 v->quick_push (TREE_VALUE (params));
2833 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2834 vec_free (v);
2835 return ret;
2838 /* Give a note about the location of the declaration of DECL. */
2840 static void
2841 inform_declaration (tree decl)
2843 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2844 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2847 /* Build a function call to function FUNCTION with parameters PARAMS.
2848 ORIGTYPES, if not NULL, is a vector of types; each element is
2849 either NULL or the original type of the corresponding element in
2850 PARAMS. The original type may differ from TREE_TYPE of the
2851 parameter for enums. FUNCTION's data type may be a function type
2852 or pointer-to-function. This function changes the elements of
2853 PARAMS. */
2855 tree
2856 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2857 tree function, vec<tree, va_gc> *params,
2858 vec<tree, va_gc> *origtypes)
2860 tree fntype, fundecl = 0;
2861 tree name = NULL_TREE, result;
2862 tree tem;
2863 int nargs;
2864 tree *argarray;
2867 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2868 STRIP_TYPE_NOPS (function);
2870 /* Convert anything with function type to a pointer-to-function. */
2871 if (TREE_CODE (function) == FUNCTION_DECL)
2873 name = DECL_NAME (function);
2875 if (flag_tm)
2876 tm_malloc_replacement (function);
2877 fundecl = function;
2878 /* Atomic functions have type checking/casting already done. They are
2879 often rewritten and don't match the original parameter list. */
2880 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2881 origtypes = NULL;
2883 if (flag_cilkplus
2884 && is_cilkplus_reduce_builtin (function))
2885 origtypes = NULL;
2887 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2888 function = function_to_pointer_conversion (loc, function);
2890 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2891 expressions, like those used for ObjC messenger dispatches. */
2892 if (params && !params->is_empty ())
2893 function = objc_rewrite_function_call (function, (*params)[0]);
2895 function = c_fully_fold (function, false, NULL);
2897 fntype = TREE_TYPE (function);
2899 if (TREE_CODE (fntype) == ERROR_MARK)
2900 return error_mark_node;
2902 if (!(TREE_CODE (fntype) == POINTER_TYPE
2903 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2905 if (!flag_diagnostics_show_caret)
2906 error_at (loc,
2907 "called object %qE is not a function or function pointer",
2908 function);
2909 else if (DECL_P (function))
2911 error_at (loc,
2912 "called object %qD is not a function or function pointer",
2913 function);
2914 inform_declaration (function);
2916 else
2917 error_at (loc,
2918 "called object is not a function or function pointer");
2919 return error_mark_node;
2922 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2923 current_function_returns_abnormally = 1;
2925 /* fntype now gets the type of function pointed to. */
2926 fntype = TREE_TYPE (fntype);
2928 /* Convert the parameters to the types declared in the
2929 function prototype, or apply default promotions. */
2931 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2932 origtypes, function, fundecl);
2933 if (nargs < 0)
2934 return error_mark_node;
2936 /* Check that the function is called through a compatible prototype.
2937 If it is not, warn. */
2938 if (CONVERT_EXPR_P (function)
2939 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2940 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2941 && !comptypes (fntype, TREE_TYPE (tem)))
2943 tree return_type = TREE_TYPE (fntype);
2945 /* This situation leads to run-time undefined behavior. We can't,
2946 therefore, simply error unless we can prove that all possible
2947 executions of the program must execute the code. */
2948 warning_at (loc, 0, "function called through a non-compatible type");
2950 if (VOID_TYPE_P (return_type)
2951 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2952 pedwarn (loc, 0,
2953 "function with qualified void return type called");
2956 argarray = vec_safe_address (params);
2958 /* Check that arguments to builtin functions match the expectations. */
2959 if (fundecl
2960 && DECL_BUILT_IN (fundecl)
2961 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2962 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2963 return error_mark_node;
2965 /* Check that the arguments to the function are valid. */
2966 check_function_arguments (fntype, nargs, argarray);
2968 if (name != NULL_TREE
2969 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2971 if (require_constant_value)
2972 result =
2973 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2974 function, nargs, argarray);
2975 else
2976 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2977 function, nargs, argarray);
2978 if (TREE_CODE (result) == NOP_EXPR
2979 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2980 STRIP_TYPE_NOPS (result);
2982 else
2983 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2984 function, nargs, argarray);
2986 if (VOID_TYPE_P (TREE_TYPE (result)))
2988 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2989 pedwarn (loc, 0,
2990 "function with qualified void return type called");
2991 return result;
2993 return require_complete_type (result);
2996 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2998 tree
2999 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3000 tree function, vec<tree, va_gc> *params,
3001 vec<tree, va_gc> *origtypes)
3003 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3004 STRIP_TYPE_NOPS (function);
3006 /* Convert anything with function type to a pointer-to-function. */
3007 if (TREE_CODE (function) == FUNCTION_DECL)
3009 /* Implement type-directed function overloading for builtins.
3010 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3011 handle all the type checking. The result is a complete expression
3012 that implements this function call. */
3013 tree tem = resolve_overloaded_builtin (loc, function, params);
3014 if (tem)
3015 return tem;
3017 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3020 /* Convert the argument expressions in the vector VALUES
3021 to the types in the list TYPELIST.
3023 If TYPELIST is exhausted, or when an element has NULL as its type,
3024 perform the default conversions.
3026 ORIGTYPES is the original types of the expressions in VALUES. This
3027 holds the type of enum values which have been converted to integral
3028 types. It may be NULL.
3030 FUNCTION is a tree for the called function. It is used only for
3031 error messages, where it is formatted with %qE.
3033 This is also where warnings about wrong number of args are generated.
3035 ARG_LOC are locations of function arguments (if any).
3037 Returns the actual number of arguments processed (which may be less
3038 than the length of VALUES in some error situations), or -1 on
3039 failure. */
3041 static int
3042 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3043 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3044 tree function, tree fundecl)
3046 tree typetail, val;
3047 unsigned int parmnum;
3048 bool error_args = false;
3049 const bool type_generic = fundecl
3050 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3051 bool type_generic_remove_excess_precision = false;
3052 tree selector;
3054 /* Change pointer to function to the function itself for
3055 diagnostics. */
3056 if (TREE_CODE (function) == ADDR_EXPR
3057 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3058 function = TREE_OPERAND (function, 0);
3060 /* Handle an ObjC selector specially for diagnostics. */
3061 selector = objc_message_selector ();
3063 /* For type-generic built-in functions, determine whether excess
3064 precision should be removed (classification) or not
3065 (comparison). */
3066 if (type_generic
3067 && DECL_BUILT_IN (fundecl)
3068 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3070 switch (DECL_FUNCTION_CODE (fundecl))
3072 case BUILT_IN_ISFINITE:
3073 case BUILT_IN_ISINF:
3074 case BUILT_IN_ISINF_SIGN:
3075 case BUILT_IN_ISNAN:
3076 case BUILT_IN_ISNORMAL:
3077 case BUILT_IN_FPCLASSIFY:
3078 type_generic_remove_excess_precision = true;
3079 break;
3081 default:
3082 type_generic_remove_excess_precision = false;
3083 break;
3086 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3087 return vec_safe_length (values);
3089 /* Scan the given expressions and types, producing individual
3090 converted arguments. */
3092 for (typetail = typelist, parmnum = 0;
3093 values && values->iterate (parmnum, &val);
3094 ++parmnum)
3096 tree type = typetail ? TREE_VALUE (typetail) : 0;
3097 tree valtype = TREE_TYPE (val);
3098 tree rname = function;
3099 int argnum = parmnum + 1;
3100 const char *invalid_func_diag;
3101 bool excess_precision = false;
3102 bool npc;
3103 tree parmval;
3104 /* Some __atomic_* builtins have additional hidden argument at
3105 position 0. */
3106 location_t ploc
3107 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3108 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3109 : input_location;
3111 if (type == void_type_node)
3113 if (selector)
3114 error_at (loc, "too many arguments to method %qE", selector);
3115 else
3116 error_at (loc, "too many arguments to function %qE", function);
3117 inform_declaration (fundecl);
3118 return error_args ? -1 : (int) parmnum;
3121 if (selector && argnum > 2)
3123 rname = selector;
3124 argnum -= 2;
3127 npc = null_pointer_constant_p (val);
3129 /* If there is excess precision and a prototype, convert once to
3130 the required type rather than converting via the semantic
3131 type. Likewise without a prototype a float value represented
3132 as long double should be converted once to double. But for
3133 type-generic classification functions excess precision must
3134 be removed here. */
3135 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3136 && (type || !type_generic || !type_generic_remove_excess_precision))
3138 val = TREE_OPERAND (val, 0);
3139 excess_precision = true;
3141 val = c_fully_fold (val, false, NULL);
3142 STRIP_TYPE_NOPS (val);
3144 val = require_complete_type (val);
3146 if (type != 0)
3148 /* Formal parm type is specified by a function prototype. */
3150 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3152 error_at (ploc, "type of formal parameter %d is incomplete",
3153 parmnum + 1);
3154 parmval = val;
3156 else
3158 tree origtype;
3160 /* Optionally warn about conversions that
3161 differ from the default conversions. */
3162 if (warn_traditional_conversion || warn_traditional)
3164 unsigned int formal_prec = TYPE_PRECISION (type);
3166 if (INTEGRAL_TYPE_P (type)
3167 && TREE_CODE (valtype) == REAL_TYPE)
3168 warning_at (ploc, OPT_Wtraditional_conversion,
3169 "passing argument %d of %qE as integer rather "
3170 "than floating due to prototype",
3171 argnum, rname);
3172 if (INTEGRAL_TYPE_P (type)
3173 && TREE_CODE (valtype) == COMPLEX_TYPE)
3174 warning_at (ploc, OPT_Wtraditional_conversion,
3175 "passing argument %d of %qE as integer rather "
3176 "than complex due to prototype",
3177 argnum, rname);
3178 else if (TREE_CODE (type) == COMPLEX_TYPE
3179 && TREE_CODE (valtype) == REAL_TYPE)
3180 warning_at (ploc, OPT_Wtraditional_conversion,
3181 "passing argument %d of %qE as complex rather "
3182 "than floating due to prototype",
3183 argnum, rname);
3184 else if (TREE_CODE (type) == REAL_TYPE
3185 && INTEGRAL_TYPE_P (valtype))
3186 warning_at (ploc, OPT_Wtraditional_conversion,
3187 "passing argument %d of %qE as floating rather "
3188 "than integer due to prototype",
3189 argnum, rname);
3190 else if (TREE_CODE (type) == COMPLEX_TYPE
3191 && INTEGRAL_TYPE_P (valtype))
3192 warning_at (ploc, OPT_Wtraditional_conversion,
3193 "passing argument %d of %qE as complex rather "
3194 "than integer due to prototype",
3195 argnum, rname);
3196 else if (TREE_CODE (type) == REAL_TYPE
3197 && TREE_CODE (valtype) == COMPLEX_TYPE)
3198 warning_at (ploc, OPT_Wtraditional_conversion,
3199 "passing argument %d of %qE as floating rather "
3200 "than complex due to prototype",
3201 argnum, rname);
3202 /* ??? At some point, messages should be written about
3203 conversions between complex types, but that's too messy
3204 to do now. */
3205 else if (TREE_CODE (type) == REAL_TYPE
3206 && TREE_CODE (valtype) == REAL_TYPE)
3208 /* Warn if any argument is passed as `float',
3209 since without a prototype it would be `double'. */
3210 if (formal_prec == TYPE_PRECISION (float_type_node)
3211 && type != dfloat32_type_node)
3212 warning_at (ploc, 0,
3213 "passing argument %d of %qE as %<float%> "
3214 "rather than %<double%> due to prototype",
3215 argnum, rname);
3217 /* Warn if mismatch between argument and prototype
3218 for decimal float types. Warn of conversions with
3219 binary float types and of precision narrowing due to
3220 prototype. */
3221 else if (type != valtype
3222 && (type == dfloat32_type_node
3223 || type == dfloat64_type_node
3224 || type == dfloat128_type_node
3225 || valtype == dfloat32_type_node
3226 || valtype == dfloat64_type_node
3227 || valtype == dfloat128_type_node)
3228 && (formal_prec
3229 <= TYPE_PRECISION (valtype)
3230 || (type == dfloat128_type_node
3231 && (valtype
3232 != dfloat64_type_node
3233 && (valtype
3234 != dfloat32_type_node)))
3235 || (type == dfloat64_type_node
3236 && (valtype
3237 != dfloat32_type_node))))
3238 warning_at (ploc, 0,
3239 "passing argument %d of %qE as %qT "
3240 "rather than %qT due to prototype",
3241 argnum, rname, type, valtype);
3244 /* Detect integer changing in width or signedness.
3245 These warnings are only activated with
3246 -Wtraditional-conversion, not with -Wtraditional. */
3247 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3248 && INTEGRAL_TYPE_P (valtype))
3250 tree would_have_been = default_conversion (val);
3251 tree type1 = TREE_TYPE (would_have_been);
3253 if (TREE_CODE (type) == ENUMERAL_TYPE
3254 && (TYPE_MAIN_VARIANT (type)
3255 == TYPE_MAIN_VARIANT (valtype)))
3256 /* No warning if function asks for enum
3257 and the actual arg is that enum type. */
3259 else if (formal_prec != TYPE_PRECISION (type1))
3260 warning_at (ploc, OPT_Wtraditional_conversion,
3261 "passing argument %d of %qE "
3262 "with different width due to prototype",
3263 argnum, rname);
3264 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3266 /* Don't complain if the formal parameter type
3267 is an enum, because we can't tell now whether
3268 the value was an enum--even the same enum. */
3269 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3271 else if (TREE_CODE (val) == INTEGER_CST
3272 && int_fits_type_p (val, type))
3273 /* Change in signedness doesn't matter
3274 if a constant value is unaffected. */
3276 /* If the value is extended from a narrower
3277 unsigned type, it doesn't matter whether we
3278 pass it as signed or unsigned; the value
3279 certainly is the same either way. */
3280 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3281 && TYPE_UNSIGNED (valtype))
3283 else if (TYPE_UNSIGNED (type))
3284 warning_at (ploc, OPT_Wtraditional_conversion,
3285 "passing argument %d of %qE "
3286 "as unsigned due to prototype",
3287 argnum, rname);
3288 else
3289 warning_at (ploc, OPT_Wtraditional_conversion,
3290 "passing argument %d of %qE "
3291 "as signed due to prototype",
3292 argnum, rname);
3296 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3297 sake of better warnings from convert_and_check. */
3298 if (excess_precision)
3299 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3300 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3301 parmval = convert_for_assignment (loc, ploc, type,
3302 val, origtype, ic_argpass,
3303 npc, fundecl, function,
3304 parmnum + 1);
3306 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3307 && INTEGRAL_TYPE_P (type)
3308 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3309 parmval = default_conversion (parmval);
3312 else if (TREE_CODE (valtype) == REAL_TYPE
3313 && (TYPE_PRECISION (valtype)
3314 <= TYPE_PRECISION (double_type_node))
3315 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3316 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3317 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3319 if (type_generic)
3320 parmval = val;
3321 else
3323 /* Convert `float' to `double'. */
3324 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3325 warning_at (ploc, OPT_Wdouble_promotion,
3326 "implicit conversion from %qT to %qT when passing "
3327 "argument to function",
3328 valtype, double_type_node);
3329 parmval = convert (double_type_node, val);
3332 else if (excess_precision && !type_generic)
3333 /* A "double" argument with excess precision being passed
3334 without a prototype or in variable arguments. */
3335 parmval = convert (valtype, val);
3336 else if ((invalid_func_diag =
3337 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3339 error (invalid_func_diag);
3340 return -1;
3342 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3344 return -1;
3346 else
3347 /* Convert `short' and `char' to full-size `int'. */
3348 parmval = default_conversion (val);
3350 (*values)[parmnum] = parmval;
3351 if (parmval == error_mark_node)
3352 error_args = true;
3354 if (typetail)
3355 typetail = TREE_CHAIN (typetail);
3358 gcc_assert (parmnum == vec_safe_length (values));
3360 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3362 error_at (loc, "too few arguments to function %qE", function);
3363 inform_declaration (fundecl);
3364 return -1;
3367 return error_args ? -1 : (int) parmnum;
3370 /* This is the entry point used by the parser to build unary operators
3371 in the input. CODE, a tree_code, specifies the unary operator, and
3372 ARG is the operand. For unary plus, the C parser currently uses
3373 CONVERT_EXPR for code.
3375 LOC is the location to use for the tree generated.
3378 struct c_expr
3379 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3381 struct c_expr result;
3383 result.original_code = code;
3384 result.original_type = NULL;
3386 if (reject_gcc_builtin (arg.value))
3388 result.value = error_mark_node;
3390 else
3392 result.value = build_unary_op (loc, code, arg.value, 0);
3394 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3395 overflow_warning (loc, result.value);
3398 return result;
3401 /* This is the entry point used by the parser to build binary operators
3402 in the input. CODE, a tree_code, specifies the binary operator, and
3403 ARG1 and ARG2 are the operands. In addition to constructing the
3404 expression, we check for operands that were written with other binary
3405 operators in a way that is likely to confuse the user.
3407 LOCATION is the location of the binary operator. */
3409 struct c_expr
3410 parser_build_binary_op (location_t location, enum tree_code code,
3411 struct c_expr arg1, struct c_expr arg2)
3413 struct c_expr result;
3415 enum tree_code code1 = arg1.original_code;
3416 enum tree_code code2 = arg2.original_code;
3417 tree type1 = (arg1.original_type
3418 ? arg1.original_type
3419 : TREE_TYPE (arg1.value));
3420 tree type2 = (arg2.original_type
3421 ? arg2.original_type
3422 : TREE_TYPE (arg2.value));
3424 result.value = build_binary_op (location, code,
3425 arg1.value, arg2.value, 1);
3426 result.original_code = code;
3427 result.original_type = NULL;
3429 if (TREE_CODE (result.value) == ERROR_MARK)
3430 return result;
3432 if (location != UNKNOWN_LOCATION)
3433 protected_set_expr_location (result.value, location);
3435 /* Check for cases such as x+y<<z which users are likely
3436 to misinterpret. */
3437 if (warn_parentheses)
3438 warn_about_parentheses (location, code, code1, arg1.value, code2,
3439 arg2.value);
3441 if (warn_logical_op)
3442 warn_logical_operator (location, code, TREE_TYPE (result.value),
3443 code1, arg1.value, code2, arg2.value);
3445 if (warn_tautological_compare)
3446 warn_tautological_cmp (location, code, arg1.value, arg2.value);
3448 if (warn_logical_not_paren
3449 && TREE_CODE_CLASS (code) == tcc_comparison
3450 && code1 == TRUTH_NOT_EXPR
3451 && code2 != TRUTH_NOT_EXPR
3452 /* Avoid warning for !!x == y. */
3453 && (TREE_CODE (arg1.value) != NE_EXPR
3454 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3456 /* Avoid warning for !b == y where b has _Bool type. */
3457 tree t = integer_zero_node;
3458 if (TREE_CODE (arg1.value) == EQ_EXPR
3459 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3460 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3462 t = TREE_OPERAND (arg1.value, 0);
3465 if (TREE_TYPE (t) != integer_type_node)
3466 break;
3467 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3468 t = C_MAYBE_CONST_EXPR_EXPR (t);
3469 else if (CONVERT_EXPR_P (t))
3470 t = TREE_OPERAND (t, 0);
3471 else
3472 break;
3474 while (1);
3476 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3477 warn_logical_not_parentheses (location, code, arg2.value);
3480 /* Warn about comparisons against string literals, with the exception
3481 of testing for equality or inequality of a string literal with NULL. */
3482 if (code == EQ_EXPR || code == NE_EXPR)
3484 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3485 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3486 warning_at (location, OPT_Waddress,
3487 "comparison with string literal results in unspecified behavior");
3489 else if (TREE_CODE_CLASS (code) == tcc_comparison
3490 && (code1 == STRING_CST || code2 == STRING_CST))
3491 warning_at (location, OPT_Waddress,
3492 "comparison with string literal results in unspecified behavior");
3494 if (TREE_OVERFLOW_P (result.value)
3495 && !TREE_OVERFLOW_P (arg1.value)
3496 && !TREE_OVERFLOW_P (arg2.value))
3497 overflow_warning (location, result.value);
3499 /* Warn about comparisons of different enum types. */
3500 if (warn_enum_compare
3501 && TREE_CODE_CLASS (code) == tcc_comparison
3502 && TREE_CODE (type1) == ENUMERAL_TYPE
3503 && TREE_CODE (type2) == ENUMERAL_TYPE
3504 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3505 warning_at (location, OPT_Wenum_compare,
3506 "comparison between %qT and %qT",
3507 type1, type2);
3509 return result;
3512 /* Return a tree for the difference of pointers OP0 and OP1.
3513 The resulting tree has type int. */
3515 static tree
3516 pointer_diff (location_t loc, tree op0, tree op1)
3518 tree restype = ptrdiff_type_node;
3519 tree result, inttype;
3521 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3522 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3523 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3524 tree orig_op1 = op1;
3526 /* If the operands point into different address spaces, we need to
3527 explicitly convert them to pointers into the common address space
3528 before we can subtract the numerical address values. */
3529 if (as0 != as1)
3531 addr_space_t as_common;
3532 tree common_type;
3534 /* Determine the common superset address space. This is guaranteed
3535 to exist because the caller verified that comp_target_types
3536 returned non-zero. */
3537 if (!addr_space_superset (as0, as1, &as_common))
3538 gcc_unreachable ();
3540 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3541 op0 = convert (common_type, op0);
3542 op1 = convert (common_type, op1);
3545 /* Determine integer type to perform computations in. This will usually
3546 be the same as the result type (ptrdiff_t), but may need to be a wider
3547 type if pointers for the address space are wider than ptrdiff_t. */
3548 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3549 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3550 else
3551 inttype = restype;
3553 if (TREE_CODE (target_type) == VOID_TYPE)
3554 pedwarn (loc, OPT_Wpointer_arith,
3555 "pointer of type %<void *%> used in subtraction");
3556 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3557 pedwarn (loc, OPT_Wpointer_arith,
3558 "pointer to a function used in subtraction");
3560 /* First do the subtraction as integers;
3561 then drop through to build the divide operator.
3562 Do not do default conversions on the minus operator
3563 in case restype is a short type. */
3565 op0 = build_binary_op (loc,
3566 MINUS_EXPR, convert (inttype, op0),
3567 convert (inttype, op1), 0);
3568 /* This generates an error if op1 is pointer to incomplete type. */
3569 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3570 error_at (loc, "arithmetic on pointer to an incomplete type");
3572 op1 = c_size_in_bytes (target_type);
3574 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3575 error_at (loc, "arithmetic on pointer to an empty aggregate");
3577 /* Divide by the size, in easiest possible way. */
3578 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3579 op0, convert (inttype, op1));
3581 /* Convert to final result type if necessary. */
3582 return convert (restype, result);
3585 /* Expand atomic compound assignments into an approriate sequence as
3586 specified by the C11 standard section 6.5.16.2.
3587 given
3588 _Atomic T1 E1
3589 T2 E2
3590 E1 op= E2
3592 This sequence is used for all types for which these operations are
3593 supported.
3595 In addition, built-in versions of the 'fe' prefixed routines may
3596 need to be invoked for floating point (real, complex or vector) when
3597 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3599 T1 newval;
3600 T1 old;
3601 T1 *addr
3602 T2 val
3603 fenv_t fenv
3605 addr = &E1;
3606 val = (E2);
3607 __atomic_load (addr, &old, SEQ_CST);
3608 feholdexcept (&fenv);
3609 loop:
3610 newval = old op val;
3611 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3612 SEQ_CST))
3613 goto done;
3614 feclearexcept (FE_ALL_EXCEPT);
3615 goto loop:
3616 done:
3617 feupdateenv (&fenv);
3619 Also note that the compiler is simply issuing the generic form of
3620 the atomic operations. This requires temp(s) and has their address
3621 taken. The atomic processing is smart enough to figure out when the
3622 size of an object can utilize a lock-free version, and convert the
3623 built-in call to the appropriate lock-free routine. The optimizers
3624 will then dispose of any temps that are no longer required, and
3625 lock-free implementations are utilized as long as there is target
3626 support for the required size.
3628 If the operator is NOP_EXPR, then this is a simple assignment, and
3629 an __atomic_store is issued to perform the assignment rather than
3630 the above loop.
3634 /* Build an atomic assignment at LOC, expanding into the proper
3635 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3636 the result of the operation, unless RETURN_OLD_P in which case
3637 return the old value of LHS (this is only for postincrement and
3638 postdecrement). */
3639 static tree
3640 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3641 tree rhs, bool return_old_p)
3643 tree fndecl, func_call;
3644 vec<tree, va_gc> *params;
3645 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3646 tree old, old_addr;
3647 tree compound_stmt;
3648 tree stmt, goto_stmt;
3649 tree loop_label, loop_decl, done_label, done_decl;
3651 tree lhs_type = TREE_TYPE (lhs);
3652 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3653 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3654 tree rhs_type = TREE_TYPE (rhs);
3656 gcc_assert (TYPE_ATOMIC (lhs_type));
3658 if (return_old_p)
3659 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3661 /* Allocate enough vector items for a compare_exchange. */
3662 vec_alloc (params, 6);
3664 /* Create a compound statement to hold the sequence of statements
3665 with a loop. */
3666 compound_stmt = c_begin_compound_stmt (false);
3668 /* Fold the RHS if it hasn't already been folded. */
3669 if (modifycode != NOP_EXPR)
3670 rhs = c_fully_fold (rhs, false, NULL);
3672 /* Remove the qualifiers for the rest of the expressions and create
3673 the VAL temp variable to hold the RHS. */
3674 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3675 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3676 val = create_tmp_var_raw (nonatomic_rhs_type);
3677 TREE_ADDRESSABLE (val) = 1;
3678 TREE_NO_WARNING (val) = 1;
3679 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3680 NULL_TREE);
3681 SET_EXPR_LOCATION (rhs, loc);
3682 add_stmt (rhs);
3684 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3685 an atomic_store. */
3686 if (modifycode == NOP_EXPR)
3688 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3689 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3690 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3691 params->quick_push (lhs_addr);
3692 params->quick_push (rhs);
3693 params->quick_push (seq_cst);
3694 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3695 add_stmt (func_call);
3697 /* Finish the compound statement. */
3698 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3700 /* VAL is the value which was stored, return a COMPOUND_STMT of
3701 the statement and that value. */
3702 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3705 /* Create the variables and labels required for the op= form. */
3706 old = create_tmp_var_raw (nonatomic_lhs_type);
3707 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3708 TREE_ADDRESSABLE (old) = 1;
3709 TREE_NO_WARNING (old) = 1;
3711 newval = create_tmp_var_raw (nonatomic_lhs_type);
3712 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3713 TREE_ADDRESSABLE (newval) = 1;
3715 loop_decl = create_artificial_label (loc);
3716 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3718 done_decl = create_artificial_label (loc);
3719 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3721 /* __atomic_load (addr, &old, SEQ_CST). */
3722 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3723 params->quick_push (lhs_addr);
3724 params->quick_push (old_addr);
3725 params->quick_push (seq_cst);
3726 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3727 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
3728 NULL_TREE);
3729 add_stmt (old);
3730 params->truncate (0);
3732 /* Create the expressions for floating-point environment
3733 manipulation, if required. */
3734 bool need_fenv = (flag_trapping_math
3735 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3736 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3737 if (need_fenv)
3738 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3740 if (hold_call)
3741 add_stmt (hold_call);
3743 /* loop: */
3744 add_stmt (loop_label);
3746 /* newval = old + val; */
3747 rhs = build_binary_op (loc, modifycode, old, val, 1);
3748 rhs = c_fully_fold (rhs, false, NULL);
3749 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3750 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3751 NULL_TREE, 0);
3752 if (rhs != error_mark_node)
3754 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
3755 NULL_TREE);
3756 SET_EXPR_LOCATION (rhs, loc);
3757 add_stmt (rhs);
3760 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3761 goto done; */
3762 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3763 params->quick_push (lhs_addr);
3764 params->quick_push (old_addr);
3765 params->quick_push (newval_addr);
3766 params->quick_push (integer_zero_node);
3767 params->quick_push (seq_cst);
3768 params->quick_push (seq_cst);
3769 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3771 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3772 SET_EXPR_LOCATION (goto_stmt, loc);
3774 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3775 SET_EXPR_LOCATION (stmt, loc);
3776 add_stmt (stmt);
3778 if (clear_call)
3779 add_stmt (clear_call);
3781 /* goto loop; */
3782 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3783 SET_EXPR_LOCATION (goto_stmt, loc);
3784 add_stmt (goto_stmt);
3786 /* done: */
3787 add_stmt (done_label);
3789 if (update_call)
3790 add_stmt (update_call);
3792 /* Finish the compound statement. */
3793 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3795 /* NEWVAL is the value that was successfully stored, return a
3796 COMPOUND_EXPR of the statement and the appropriate value. */
3797 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3798 return_old_p ? old : newval);
3801 /* Construct and perhaps optimize a tree representation
3802 for a unary operation. CODE, a tree_code, specifies the operation
3803 and XARG is the operand.
3804 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3805 the default promotions (such as from short to int).
3806 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3807 allows non-lvalues; this is only used to handle conversion of non-lvalue
3808 arrays to pointers in C99.
3810 LOCATION is the location of the operator. */
3812 tree
3813 build_unary_op (location_t location,
3814 enum tree_code code, tree xarg, int flag)
3816 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3817 tree arg = xarg;
3818 tree argtype = 0;
3819 enum tree_code typecode;
3820 tree val;
3821 tree ret = error_mark_node;
3822 tree eptype = NULL_TREE;
3823 int noconvert = flag;
3824 const char *invalid_op_diag;
3825 bool int_operands;
3827 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3828 if (int_operands)
3829 arg = remove_c_maybe_const_expr (arg);
3831 if (code != ADDR_EXPR)
3832 arg = require_complete_type (arg);
3834 typecode = TREE_CODE (TREE_TYPE (arg));
3835 if (typecode == ERROR_MARK)
3836 return error_mark_node;
3837 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3838 typecode = INTEGER_TYPE;
3840 if ((invalid_op_diag
3841 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3843 error_at (location, invalid_op_diag);
3844 return error_mark_node;
3847 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3849 eptype = TREE_TYPE (arg);
3850 arg = TREE_OPERAND (arg, 0);
3853 switch (code)
3855 case CONVERT_EXPR:
3856 /* This is used for unary plus, because a CONVERT_EXPR
3857 is enough to prevent anybody from looking inside for
3858 associativity, but won't generate any code. */
3859 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3860 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3861 || typecode == VECTOR_TYPE))
3863 error_at (location, "wrong type argument to unary plus");
3864 return error_mark_node;
3866 else if (!noconvert)
3867 arg = default_conversion (arg);
3868 arg = non_lvalue_loc (location, arg);
3869 break;
3871 case NEGATE_EXPR:
3872 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3873 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3874 || typecode == VECTOR_TYPE))
3876 error_at (location, "wrong type argument to unary minus");
3877 return error_mark_node;
3879 else if (!noconvert)
3880 arg = default_conversion (arg);
3881 break;
3883 case BIT_NOT_EXPR:
3884 /* ~ works on integer types and non float vectors. */
3885 if (typecode == INTEGER_TYPE
3886 || (typecode == VECTOR_TYPE
3887 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3889 if (!noconvert)
3890 arg = default_conversion (arg);
3892 else if (typecode == COMPLEX_TYPE)
3894 code = CONJ_EXPR;
3895 pedwarn (location, OPT_Wpedantic,
3896 "ISO C does not support %<~%> for complex conjugation");
3897 if (!noconvert)
3898 arg = default_conversion (arg);
3900 else
3902 error_at (location, "wrong type argument to bit-complement");
3903 return error_mark_node;
3905 break;
3907 case ABS_EXPR:
3908 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3910 error_at (location, "wrong type argument to abs");
3911 return error_mark_node;
3913 else if (!noconvert)
3914 arg = default_conversion (arg);
3915 break;
3917 case CONJ_EXPR:
3918 /* Conjugating a real value is a no-op, but allow it anyway. */
3919 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3920 || typecode == COMPLEX_TYPE))
3922 error_at (location, "wrong type argument to conjugation");
3923 return error_mark_node;
3925 else if (!noconvert)
3926 arg = default_conversion (arg);
3927 break;
3929 case TRUTH_NOT_EXPR:
3930 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3931 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3932 && typecode != COMPLEX_TYPE)
3934 error_at (location,
3935 "wrong type argument to unary exclamation mark");
3936 return error_mark_node;
3938 if (int_operands)
3940 arg = c_objc_common_truthvalue_conversion (location, xarg);
3941 arg = remove_c_maybe_const_expr (arg);
3943 else
3944 arg = c_objc_common_truthvalue_conversion (location, arg);
3945 ret = invert_truthvalue_loc (location, arg);
3946 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3947 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3948 location = EXPR_LOCATION (ret);
3949 goto return_build_unary_op;
3951 case REALPART_EXPR:
3952 case IMAGPART_EXPR:
3953 ret = build_real_imag_expr (location, code, arg);
3954 if (ret == error_mark_node)
3955 return error_mark_node;
3956 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3957 eptype = TREE_TYPE (eptype);
3958 goto return_build_unary_op;
3960 case PREINCREMENT_EXPR:
3961 case POSTINCREMENT_EXPR:
3962 case PREDECREMENT_EXPR:
3963 case POSTDECREMENT_EXPR:
3965 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3967 tree inner = build_unary_op (location, code,
3968 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3969 if (inner == error_mark_node)
3970 return error_mark_node;
3971 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3972 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3973 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3974 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3975 goto return_build_unary_op;
3978 /* Complain about anything that is not a true lvalue. In
3979 Objective-C, skip this check for property_refs. */
3980 if (!objc_is_property_ref (arg)
3981 && !lvalue_or_else (location,
3982 arg, ((code == PREINCREMENT_EXPR
3983 || code == POSTINCREMENT_EXPR)
3984 ? lv_increment
3985 : lv_decrement)))
3986 return error_mark_node;
3988 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3990 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3991 warning_at (location, OPT_Wc___compat,
3992 "increment of enumeration value is invalid in C++");
3993 else
3994 warning_at (location, OPT_Wc___compat,
3995 "decrement of enumeration value is invalid in C++");
3998 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3999 arg = c_fully_fold (arg, false, NULL);
4001 bool atomic_op;
4002 atomic_op = really_atomic_lvalue (arg);
4004 /* Increment or decrement the real part of the value,
4005 and don't change the imaginary part. */
4006 if (typecode == COMPLEX_TYPE)
4008 tree real, imag;
4010 pedwarn (location, OPT_Wpedantic,
4011 "ISO C does not support %<++%> and %<--%> on complex types");
4013 if (!atomic_op)
4015 arg = stabilize_reference (arg);
4016 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4017 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4018 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4019 if (real == error_mark_node || imag == error_mark_node)
4020 return error_mark_node;
4021 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4022 real, imag);
4023 goto return_build_unary_op;
4027 /* Report invalid types. */
4029 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4030 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4031 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4033 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4034 error_at (location, "wrong type argument to increment");
4035 else
4036 error_at (location, "wrong type argument to decrement");
4038 return error_mark_node;
4042 tree inc;
4044 argtype = TREE_TYPE (arg);
4046 /* Compute the increment. */
4048 if (typecode == POINTER_TYPE)
4050 /* If pointer target is an incomplete type,
4051 we just cannot know how to do the arithmetic. */
4052 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4054 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4055 error_at (location,
4056 "increment of pointer to an incomplete type %qT",
4057 TREE_TYPE (argtype));
4058 else
4059 error_at (location,
4060 "decrement of pointer to an incomplete type %qT",
4061 TREE_TYPE (argtype));
4063 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4064 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4066 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4067 pedwarn (location, OPT_Wpointer_arith,
4068 "wrong type argument to increment");
4069 else
4070 pedwarn (location, OPT_Wpointer_arith,
4071 "wrong type argument to decrement");
4074 inc = c_size_in_bytes (TREE_TYPE (argtype));
4075 inc = convert_to_ptrofftype_loc (location, inc);
4077 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4079 /* For signed fract types, we invert ++ to -- or
4080 -- to ++, and change inc from 1 to -1, because
4081 it is not possible to represent 1 in signed fract constants.
4082 For unsigned fract types, the result always overflows and
4083 we get an undefined (original) or the maximum value. */
4084 if (code == PREINCREMENT_EXPR)
4085 code = PREDECREMENT_EXPR;
4086 else if (code == PREDECREMENT_EXPR)
4087 code = PREINCREMENT_EXPR;
4088 else if (code == POSTINCREMENT_EXPR)
4089 code = POSTDECREMENT_EXPR;
4090 else /* code == POSTDECREMENT_EXPR */
4091 code = POSTINCREMENT_EXPR;
4093 inc = integer_minus_one_node;
4094 inc = convert (argtype, inc);
4096 else
4098 inc = VECTOR_TYPE_P (argtype)
4099 ? build_one_cst (argtype)
4100 : integer_one_node;
4101 inc = convert (argtype, inc);
4104 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4105 need to ask Objective-C to build the increment or decrement
4106 expression for it. */
4107 if (objc_is_property_ref (arg))
4108 return objc_build_incr_expr_for_property_ref (location, code,
4109 arg, inc);
4111 /* Report a read-only lvalue. */
4112 if (TYPE_READONLY (argtype))
4114 readonly_error (location, arg,
4115 ((code == PREINCREMENT_EXPR
4116 || code == POSTINCREMENT_EXPR)
4117 ? lv_increment : lv_decrement));
4118 return error_mark_node;
4120 else if (TREE_READONLY (arg))
4121 readonly_warning (arg,
4122 ((code == PREINCREMENT_EXPR
4123 || code == POSTINCREMENT_EXPR)
4124 ? lv_increment : lv_decrement));
4126 /* If the argument is atomic, use the special code sequences for
4127 atomic compound assignment. */
4128 if (atomic_op)
4130 arg = stabilize_reference (arg);
4131 ret = build_atomic_assign (location, arg,
4132 ((code == PREINCREMENT_EXPR
4133 || code == POSTINCREMENT_EXPR)
4134 ? PLUS_EXPR
4135 : MINUS_EXPR),
4136 (FRACT_MODE_P (TYPE_MODE (argtype))
4137 ? inc
4138 : integer_one_node),
4139 (code == POSTINCREMENT_EXPR
4140 || code == POSTDECREMENT_EXPR));
4141 goto return_build_unary_op;
4144 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4145 val = boolean_increment (code, arg);
4146 else
4147 val = build2 (code, TREE_TYPE (arg), arg, inc);
4148 TREE_SIDE_EFFECTS (val) = 1;
4149 if (TREE_CODE (val) != code)
4150 TREE_NO_WARNING (val) = 1;
4151 ret = val;
4152 goto return_build_unary_op;
4155 case ADDR_EXPR:
4156 /* Note that this operation never does default_conversion. */
4158 /* The operand of unary '&' must be an lvalue (which excludes
4159 expressions of type void), or, in C99, the result of a [] or
4160 unary '*' operator. */
4161 if (VOID_TYPE_P (TREE_TYPE (arg))
4162 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4163 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4164 pedwarn (location, 0, "taking address of expression of type %<void%>");
4166 /* Let &* cancel out to simplify resulting code. */
4167 if (INDIRECT_REF_P (arg))
4169 /* Don't let this be an lvalue. */
4170 if (lvalue_p (TREE_OPERAND (arg, 0)))
4171 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4172 ret = TREE_OPERAND (arg, 0);
4173 goto return_build_unary_op;
4176 /* For &x[y], return x+y */
4177 if (TREE_CODE (arg) == ARRAY_REF)
4179 tree op0 = TREE_OPERAND (arg, 0);
4180 if (!c_mark_addressable (op0))
4181 return error_mark_node;
4184 /* Anything not already handled and not a true memory reference
4185 or a non-lvalue array is an error. */
4186 else if (typecode != FUNCTION_TYPE && !flag
4187 && !lvalue_or_else (location, arg, lv_addressof))
4188 return error_mark_node;
4190 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4191 folding later. */
4192 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4194 tree inner = build_unary_op (location, code,
4195 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4196 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4197 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4198 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4199 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4200 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4201 goto return_build_unary_op;
4204 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4205 argtype = TREE_TYPE (arg);
4207 /* If the lvalue is const or volatile, merge that into the type
4208 to which the address will point. This is only needed
4209 for function types. */
4210 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4211 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4212 && TREE_CODE (argtype) == FUNCTION_TYPE)
4214 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4215 int quals = orig_quals;
4217 if (TREE_READONLY (arg))
4218 quals |= TYPE_QUAL_CONST;
4219 if (TREE_THIS_VOLATILE (arg))
4220 quals |= TYPE_QUAL_VOLATILE;
4222 argtype = c_build_qualified_type (argtype, quals);
4225 if (!c_mark_addressable (arg))
4226 return error_mark_node;
4228 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4229 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4231 argtype = build_pointer_type (argtype);
4233 /* ??? Cope with user tricks that amount to offsetof. Delete this
4234 when we have proper support for integer constant expressions. */
4235 val = get_base_address (arg);
4236 if (val && INDIRECT_REF_P (val)
4237 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4239 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4240 goto return_build_unary_op;
4243 val = build1 (ADDR_EXPR, argtype, arg);
4245 ret = val;
4246 goto return_build_unary_op;
4248 default:
4249 gcc_unreachable ();
4252 if (argtype == 0)
4253 argtype = TREE_TYPE (arg);
4254 if (TREE_CODE (arg) == INTEGER_CST)
4255 ret = (require_constant_value
4256 ? fold_build1_initializer_loc (location, code, argtype, arg)
4257 : fold_build1_loc (location, code, argtype, arg));
4258 else
4259 ret = build1 (code, argtype, arg);
4260 return_build_unary_op:
4261 gcc_assert (ret != error_mark_node);
4262 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4263 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4264 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4265 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4266 ret = note_integer_operands (ret);
4267 if (eptype)
4268 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4269 protected_set_expr_location (ret, location);
4270 return ret;
4273 /* Return nonzero if REF is an lvalue valid for this language.
4274 Lvalues can be assigned, unless their type has TYPE_READONLY.
4275 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4277 bool
4278 lvalue_p (const_tree ref)
4280 const enum tree_code code = TREE_CODE (ref);
4282 switch (code)
4284 case REALPART_EXPR:
4285 case IMAGPART_EXPR:
4286 case COMPONENT_REF:
4287 return lvalue_p (TREE_OPERAND (ref, 0));
4289 case C_MAYBE_CONST_EXPR:
4290 return lvalue_p (TREE_OPERAND (ref, 1));
4292 case COMPOUND_LITERAL_EXPR:
4293 case STRING_CST:
4294 return 1;
4296 case INDIRECT_REF:
4297 case ARRAY_REF:
4298 case ARRAY_NOTATION_REF:
4299 case VAR_DECL:
4300 case PARM_DECL:
4301 case RESULT_DECL:
4302 case ERROR_MARK:
4303 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4304 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4306 case BIND_EXPR:
4307 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4309 default:
4310 return 0;
4314 /* Give a warning for storing in something that is read-only in GCC
4315 terms but not const in ISO C terms. */
4317 static void
4318 readonly_warning (tree arg, enum lvalue_use use)
4320 switch (use)
4322 case lv_assign:
4323 warning (0, "assignment of read-only location %qE", arg);
4324 break;
4325 case lv_increment:
4326 warning (0, "increment of read-only location %qE", arg);
4327 break;
4328 case lv_decrement:
4329 warning (0, "decrement of read-only location %qE", arg);
4330 break;
4331 default:
4332 gcc_unreachable ();
4334 return;
4338 /* Return nonzero if REF is an lvalue valid for this language;
4339 otherwise, print an error message and return zero. USE says
4340 how the lvalue is being used and so selects the error message.
4341 LOCATION is the location at which any error should be reported. */
4343 static int
4344 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4346 int win = lvalue_p (ref);
4348 if (!win)
4349 lvalue_error (loc, use);
4351 return win;
4354 /* Mark EXP saying that we need to be able to take the
4355 address of it; it should not be allocated in a register.
4356 Returns true if successful. */
4358 bool
4359 c_mark_addressable (tree exp)
4361 tree x = exp;
4363 while (1)
4364 switch (TREE_CODE (x))
4366 case COMPONENT_REF:
4367 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4369 error
4370 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4371 return false;
4374 /* ... fall through ... */
4376 case ADDR_EXPR:
4377 case ARRAY_REF:
4378 case REALPART_EXPR:
4379 case IMAGPART_EXPR:
4380 x = TREE_OPERAND (x, 0);
4381 break;
4383 case COMPOUND_LITERAL_EXPR:
4384 case CONSTRUCTOR:
4385 TREE_ADDRESSABLE (x) = 1;
4386 return true;
4388 case VAR_DECL:
4389 case CONST_DECL:
4390 case PARM_DECL:
4391 case RESULT_DECL:
4392 if (C_DECL_REGISTER (x)
4393 && DECL_NONLOCAL (x))
4395 if (TREE_PUBLIC (x) || is_global_var (x))
4397 error
4398 ("global register variable %qD used in nested function", x);
4399 return false;
4401 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4403 else if (C_DECL_REGISTER (x))
4405 if (TREE_PUBLIC (x) || is_global_var (x))
4406 error ("address of global register variable %qD requested", x);
4407 else
4408 error ("address of register variable %qD requested", x);
4409 return false;
4412 /* drops in */
4413 case FUNCTION_DECL:
4414 TREE_ADDRESSABLE (x) = 1;
4415 /* drops out */
4416 default:
4417 return true;
4421 /* Convert EXPR to TYPE, warning about conversion problems with
4422 constants. SEMANTIC_TYPE is the type this conversion would use
4423 without excess precision. If SEMANTIC_TYPE is NULL, this function
4424 is equivalent to convert_and_check. This function is a wrapper that
4425 handles conversions that may be different than
4426 the usual ones because of excess precision. */
4428 static tree
4429 ep_convert_and_check (location_t loc, tree type, tree expr,
4430 tree semantic_type)
4432 if (TREE_TYPE (expr) == type)
4433 return expr;
4435 if (!semantic_type)
4436 return convert_and_check (loc, type, expr);
4438 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4439 && TREE_TYPE (expr) != semantic_type)
4441 /* For integers, we need to check the real conversion, not
4442 the conversion to the excess precision type. */
4443 expr = convert_and_check (loc, semantic_type, expr);
4445 /* Result type is the excess precision type, which should be
4446 large enough, so do not check. */
4447 return convert (type, expr);
4450 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4451 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4452 if folded to an integer constant then the unselected half may
4453 contain arbitrary operations not normally permitted in constant
4454 expressions. Set the location of the expression to LOC. */
4456 tree
4457 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4458 tree op1, tree op1_original_type, tree op2,
4459 tree op2_original_type)
4461 tree type1;
4462 tree type2;
4463 enum tree_code code1;
4464 enum tree_code code2;
4465 tree result_type = NULL;
4466 tree semantic_result_type = NULL;
4467 tree orig_op1 = op1, orig_op2 = op2;
4468 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4469 bool ifexp_int_operands;
4470 tree ret;
4472 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4473 if (op1_int_operands)
4474 op1 = remove_c_maybe_const_expr (op1);
4475 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4476 if (op2_int_operands)
4477 op2 = remove_c_maybe_const_expr (op2);
4478 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4479 if (ifexp_int_operands)
4480 ifexp = remove_c_maybe_const_expr (ifexp);
4482 /* Promote both alternatives. */
4484 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4485 op1 = default_conversion (op1);
4486 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4487 op2 = default_conversion (op2);
4489 if (TREE_CODE (ifexp) == ERROR_MARK
4490 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4491 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4492 return error_mark_node;
4494 type1 = TREE_TYPE (op1);
4495 code1 = TREE_CODE (type1);
4496 type2 = TREE_TYPE (op2);
4497 code2 = TREE_CODE (type2);
4499 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4500 return error_mark_node;
4502 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4503 return error_mark_node;
4505 /* C90 does not permit non-lvalue arrays in conditional expressions.
4506 In C99 they will be pointers by now. */
4507 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4509 error_at (colon_loc, "non-lvalue array in conditional expression");
4510 return error_mark_node;
4513 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4514 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4515 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4516 || code1 == COMPLEX_TYPE)
4517 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4518 || code2 == COMPLEX_TYPE))
4520 semantic_result_type = c_common_type (type1, type2);
4521 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4523 op1 = TREE_OPERAND (op1, 0);
4524 type1 = TREE_TYPE (op1);
4525 gcc_assert (TREE_CODE (type1) == code1);
4527 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4529 op2 = TREE_OPERAND (op2, 0);
4530 type2 = TREE_TYPE (op2);
4531 gcc_assert (TREE_CODE (type2) == code2);
4535 if (warn_cxx_compat)
4537 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4538 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4540 if (TREE_CODE (t1) == ENUMERAL_TYPE
4541 && TREE_CODE (t2) == ENUMERAL_TYPE
4542 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4543 warning_at (colon_loc, OPT_Wc___compat,
4544 ("different enum types in conditional is "
4545 "invalid in C++: %qT vs %qT"),
4546 t1, t2);
4549 /* Quickly detect the usual case where op1 and op2 have the same type
4550 after promotion. */
4551 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4553 if (type1 == type2)
4554 result_type = type1;
4555 else
4556 result_type = TYPE_MAIN_VARIANT (type1);
4558 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4559 || code1 == COMPLEX_TYPE)
4560 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4561 || code2 == COMPLEX_TYPE))
4563 result_type = c_common_type (type1, type2);
4564 do_warn_double_promotion (result_type, type1, type2,
4565 "implicit conversion from %qT to %qT to "
4566 "match other result of conditional",
4567 colon_loc);
4569 /* If -Wsign-compare, warn here if type1 and type2 have
4570 different signedness. We'll promote the signed to unsigned
4571 and later code won't know it used to be different.
4572 Do this check on the original types, so that explicit casts
4573 will be considered, but default promotions won't. */
4574 if (c_inhibit_evaluation_warnings == 0)
4576 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4577 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4579 if (unsigned_op1 ^ unsigned_op2)
4581 bool ovf;
4583 /* Do not warn if the result type is signed, since the
4584 signed type will only be chosen if it can represent
4585 all the values of the unsigned type. */
4586 if (!TYPE_UNSIGNED (result_type))
4587 /* OK */;
4588 else
4590 bool op1_maybe_const = true;
4591 bool op2_maybe_const = true;
4593 /* Do not warn if the signed quantity is an
4594 unsuffixed integer literal (or some static
4595 constant expression involving such literals) and
4596 it is non-negative. This warning requires the
4597 operands to be folded for best results, so do
4598 that folding in this case even without
4599 warn_sign_compare to avoid warning options
4600 possibly affecting code generation. */
4601 c_inhibit_evaluation_warnings
4602 += (ifexp == truthvalue_false_node);
4603 op1 = c_fully_fold (op1, require_constant_value,
4604 &op1_maybe_const);
4605 c_inhibit_evaluation_warnings
4606 -= (ifexp == truthvalue_false_node);
4608 c_inhibit_evaluation_warnings
4609 += (ifexp == truthvalue_true_node);
4610 op2 = c_fully_fold (op2, require_constant_value,
4611 &op2_maybe_const);
4612 c_inhibit_evaluation_warnings
4613 -= (ifexp == truthvalue_true_node);
4615 if (warn_sign_compare)
4617 if ((unsigned_op2
4618 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4619 || (unsigned_op1
4620 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4621 /* OK */;
4622 else
4623 warning_at (colon_loc, OPT_Wsign_compare,
4624 ("signed and unsigned type in "
4625 "conditional expression"));
4627 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4628 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4629 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4630 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4635 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4637 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4638 pedwarn (colon_loc, OPT_Wpedantic,
4639 "ISO C forbids conditional expr with only one void side");
4640 result_type = void_type_node;
4642 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4644 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4645 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4646 addr_space_t as_common;
4648 if (comp_target_types (colon_loc, type1, type2))
4649 result_type = common_pointer_type (type1, type2);
4650 else if (null_pointer_constant_p (orig_op1))
4651 result_type = type2;
4652 else if (null_pointer_constant_p (orig_op2))
4653 result_type = type1;
4654 else if (!addr_space_superset (as1, as2, &as_common))
4656 error_at (colon_loc, "pointers to disjoint address spaces "
4657 "used in conditional expression");
4658 return error_mark_node;
4660 else if (VOID_TYPE_P (TREE_TYPE (type1))
4661 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4663 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4664 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4665 & ~TYPE_QUALS (TREE_TYPE (type1))))
4666 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4667 "pointer to array loses qualifier "
4668 "in conditional expression");
4670 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4671 pedwarn (colon_loc, OPT_Wpedantic,
4672 "ISO C forbids conditional expr between "
4673 "%<void *%> and function pointer");
4674 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4675 TREE_TYPE (type2)));
4677 else if (VOID_TYPE_P (TREE_TYPE (type2))
4678 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4680 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4681 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4682 & ~TYPE_QUALS (TREE_TYPE (type2))))
4683 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4684 "pointer to array loses qualifier "
4685 "in conditional expression");
4687 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4688 pedwarn (colon_loc, OPT_Wpedantic,
4689 "ISO C forbids conditional expr between "
4690 "%<void *%> and function pointer");
4691 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4692 TREE_TYPE (type1)));
4694 /* Objective-C pointer comparisons are a bit more lenient. */
4695 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4696 result_type = objc_common_type (type1, type2);
4697 else
4699 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4701 pedwarn (colon_loc, 0,
4702 "pointer type mismatch in conditional expression");
4703 result_type = build_pointer_type
4704 (build_qualified_type (void_type_node, qual));
4707 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4709 if (!null_pointer_constant_p (orig_op2))
4710 pedwarn (colon_loc, 0,
4711 "pointer/integer type mismatch in conditional expression");
4712 else
4714 op2 = null_pointer_node;
4716 result_type = type1;
4718 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4720 if (!null_pointer_constant_p (orig_op1))
4721 pedwarn (colon_loc, 0,
4722 "pointer/integer type mismatch in conditional expression");
4723 else
4725 op1 = null_pointer_node;
4727 result_type = type2;
4730 if (!result_type)
4732 if (flag_cond_mismatch)
4733 result_type = void_type_node;
4734 else
4736 error_at (colon_loc, "type mismatch in conditional expression");
4737 return error_mark_node;
4741 /* Merge const and volatile flags of the incoming types. */
4742 result_type
4743 = build_type_variant (result_type,
4744 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4745 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4747 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4748 semantic_result_type);
4749 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4750 semantic_result_type);
4752 if (ifexp_bcp && ifexp == truthvalue_true_node)
4754 op2_int_operands = true;
4755 op1 = c_fully_fold (op1, require_constant_value, NULL);
4757 if (ifexp_bcp && ifexp == truthvalue_false_node)
4759 op1_int_operands = true;
4760 op2 = c_fully_fold (op2, require_constant_value, NULL);
4762 int_const = int_operands = (ifexp_int_operands
4763 && op1_int_operands
4764 && op2_int_operands);
4765 if (int_operands)
4767 int_const = ((ifexp == truthvalue_true_node
4768 && TREE_CODE (orig_op1) == INTEGER_CST
4769 && !TREE_OVERFLOW (orig_op1))
4770 || (ifexp == truthvalue_false_node
4771 && TREE_CODE (orig_op2) == INTEGER_CST
4772 && !TREE_OVERFLOW (orig_op2)));
4774 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4775 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4776 else
4778 if (int_operands)
4780 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4781 nested inside of the expression. */
4782 op1 = c_fully_fold (op1, false, NULL);
4783 op2 = c_fully_fold (op2, false, NULL);
4785 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4786 if (int_operands)
4787 ret = note_integer_operands (ret);
4789 if (semantic_result_type)
4790 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4792 protected_set_expr_location (ret, colon_loc);
4793 return ret;
4796 /* Return a compound expression that performs two expressions and
4797 returns the value of the second of them.
4799 LOC is the location of the COMPOUND_EXPR. */
4801 tree
4802 build_compound_expr (location_t loc, tree expr1, tree expr2)
4804 bool expr1_int_operands, expr2_int_operands;
4805 tree eptype = NULL_TREE;
4806 tree ret;
4808 if (flag_cilkplus
4809 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4810 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4812 error_at (loc,
4813 "spawned function call cannot be part of a comma expression");
4814 return error_mark_node;
4816 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4817 if (expr1_int_operands)
4818 expr1 = remove_c_maybe_const_expr (expr1);
4819 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4820 if (expr2_int_operands)
4821 expr2 = remove_c_maybe_const_expr (expr2);
4823 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4824 expr1 = TREE_OPERAND (expr1, 0);
4825 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4827 eptype = TREE_TYPE (expr2);
4828 expr2 = TREE_OPERAND (expr2, 0);
4831 if (!TREE_SIDE_EFFECTS (expr1))
4833 /* The left-hand operand of a comma expression is like an expression
4834 statement: with -Wunused, we should warn if it doesn't have
4835 any side-effects, unless it was explicitly cast to (void). */
4836 if (warn_unused_value)
4838 if (VOID_TYPE_P (TREE_TYPE (expr1))
4839 && CONVERT_EXPR_P (expr1))
4840 ; /* (void) a, b */
4841 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4842 && TREE_CODE (expr1) == COMPOUND_EXPR
4843 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4844 ; /* (void) a, (void) b, c */
4845 else
4846 warning_at (loc, OPT_Wunused_value,
4847 "left-hand operand of comma expression has no effect");
4850 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4851 && warn_unused_value)
4853 tree r = expr1;
4854 location_t cloc = loc;
4855 while (TREE_CODE (r) == COMPOUND_EXPR)
4857 if (EXPR_HAS_LOCATION (r))
4858 cloc = EXPR_LOCATION (r);
4859 r = TREE_OPERAND (r, 1);
4861 if (!TREE_SIDE_EFFECTS (r)
4862 && !VOID_TYPE_P (TREE_TYPE (r))
4863 && !CONVERT_EXPR_P (r))
4864 warning_at (cloc, OPT_Wunused_value,
4865 "right-hand operand of comma expression has no effect");
4868 /* With -Wunused, we should also warn if the left-hand operand does have
4869 side-effects, but computes a value which is not used. For example, in
4870 `foo() + bar(), baz()' the result of the `+' operator is not used,
4871 so we should issue a warning. */
4872 else if (warn_unused_value)
4873 warn_if_unused_value (expr1, loc);
4875 if (expr2 == error_mark_node)
4876 return error_mark_node;
4878 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4880 if (flag_isoc99
4881 && expr1_int_operands
4882 && expr2_int_operands)
4883 ret = note_integer_operands (ret);
4885 if (eptype)
4886 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4888 protected_set_expr_location (ret, loc);
4889 return ret;
4892 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4893 which we are casting. OTYPE is the type of the expression being
4894 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4895 of the cast. -Wcast-qual appeared on the command line. Named
4896 address space qualifiers are not handled here, because they result
4897 in different warnings. */
4899 static void
4900 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4902 tree in_type = type;
4903 tree in_otype = otype;
4904 int added = 0;
4905 int discarded = 0;
4906 bool is_const;
4908 /* Check that the qualifiers on IN_TYPE are a superset of the
4909 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4910 nodes is uninteresting and we stop as soon as we hit a
4911 non-POINTER_TYPE node on either type. */
4914 in_otype = TREE_TYPE (in_otype);
4915 in_type = TREE_TYPE (in_type);
4917 /* GNU C allows cv-qualified function types. 'const' means the
4918 function is very pure, 'volatile' means it can't return. We
4919 need to warn when such qualifiers are added, not when they're
4920 taken away. */
4921 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4922 && TREE_CODE (in_type) == FUNCTION_TYPE)
4923 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4924 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4925 else
4926 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4927 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4929 while (TREE_CODE (in_type) == POINTER_TYPE
4930 && TREE_CODE (in_otype) == POINTER_TYPE);
4932 if (added)
4933 warning_at (loc, OPT_Wcast_qual,
4934 "cast adds %q#v qualifier to function type", added);
4936 if (discarded)
4937 /* There are qualifiers present in IN_OTYPE that are not present
4938 in IN_TYPE. */
4939 warning_at (loc, OPT_Wcast_qual,
4940 "cast discards %qv qualifier from pointer target type",
4941 discarded);
4943 if (added || discarded)
4944 return;
4946 /* A cast from **T to const **T is unsafe, because it can cause a
4947 const value to be changed with no additional warning. We only
4948 issue this warning if T is the same on both sides, and we only
4949 issue the warning if there are the same number of pointers on
4950 both sides, as otherwise the cast is clearly unsafe anyhow. A
4951 cast is unsafe when a qualifier is added at one level and const
4952 is not present at all outer levels.
4954 To issue this warning, we check at each level whether the cast
4955 adds new qualifiers not already seen. We don't need to special
4956 case function types, as they won't have the same
4957 TYPE_MAIN_VARIANT. */
4959 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4960 return;
4961 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4962 return;
4964 in_type = type;
4965 in_otype = otype;
4966 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4969 in_type = TREE_TYPE (in_type);
4970 in_otype = TREE_TYPE (in_otype);
4971 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4972 && !is_const)
4974 warning_at (loc, OPT_Wcast_qual,
4975 "to be safe all intermediate pointers in cast from "
4976 "%qT to %qT must be %<const%> qualified",
4977 otype, type);
4978 break;
4980 if (is_const)
4981 is_const = TYPE_READONLY (in_type);
4983 while (TREE_CODE (in_type) == POINTER_TYPE);
4986 /* Build an expression representing a cast to type TYPE of expression EXPR.
4987 LOC is the location of the cast-- typically the open paren of the cast. */
4989 tree
4990 build_c_cast (location_t loc, tree type, tree expr)
4992 tree value;
4994 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4995 expr = TREE_OPERAND (expr, 0);
4997 value = expr;
4999 if (type == error_mark_node || expr == error_mark_node)
5000 return error_mark_node;
5002 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5003 only in <protocol> qualifications. But when constructing cast expressions,
5004 the protocols do matter and must be kept around. */
5005 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5006 return build1 (NOP_EXPR, type, expr);
5008 type = TYPE_MAIN_VARIANT (type);
5010 if (TREE_CODE (type) == ARRAY_TYPE)
5012 error_at (loc, "cast specifies array type");
5013 return error_mark_node;
5016 if (TREE_CODE (type) == FUNCTION_TYPE)
5018 error_at (loc, "cast specifies function type");
5019 return error_mark_node;
5022 if (!VOID_TYPE_P (type))
5024 value = require_complete_type (value);
5025 if (value == error_mark_node)
5026 return error_mark_node;
5029 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5031 if (TREE_CODE (type) == RECORD_TYPE
5032 || TREE_CODE (type) == UNION_TYPE)
5033 pedwarn (loc, OPT_Wpedantic,
5034 "ISO C forbids casting nonscalar to the same type");
5036 /* Convert to remove any qualifiers from VALUE's type. */
5037 value = convert (type, value);
5039 else if (TREE_CODE (type) == UNION_TYPE)
5041 tree field;
5043 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5044 if (TREE_TYPE (field) != error_mark_node
5045 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5046 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5047 break;
5049 if (field)
5051 tree t;
5052 bool maybe_const = true;
5054 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5055 t = c_fully_fold (value, false, &maybe_const);
5056 t = build_constructor_single (type, field, t);
5057 if (!maybe_const)
5058 t = c_wrap_maybe_const (t, true);
5059 t = digest_init (loc, type, t,
5060 NULL_TREE, false, true, 0);
5061 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5062 return t;
5064 error_at (loc, "cast to union type from type not present in union");
5065 return error_mark_node;
5067 else
5069 tree otype, ovalue;
5071 if (type == void_type_node)
5073 tree t = build1 (CONVERT_EXPR, type, value);
5074 SET_EXPR_LOCATION (t, loc);
5075 return t;
5078 otype = TREE_TYPE (value);
5080 /* Optionally warn about potentially worrisome casts. */
5081 if (warn_cast_qual
5082 && TREE_CODE (type) == POINTER_TYPE
5083 && TREE_CODE (otype) == POINTER_TYPE)
5084 handle_warn_cast_qual (loc, type, otype);
5086 /* Warn about conversions between pointers to disjoint
5087 address spaces. */
5088 if (TREE_CODE (type) == POINTER_TYPE
5089 && TREE_CODE (otype) == POINTER_TYPE
5090 && !null_pointer_constant_p (value))
5092 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5093 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5094 addr_space_t as_common;
5096 if (!addr_space_superset (as_to, as_from, &as_common))
5098 if (ADDR_SPACE_GENERIC_P (as_from))
5099 warning_at (loc, 0, "cast to %s address space pointer "
5100 "from disjoint generic address space pointer",
5101 c_addr_space_name (as_to));
5103 else if (ADDR_SPACE_GENERIC_P (as_to))
5104 warning_at (loc, 0, "cast to generic address space pointer "
5105 "from disjoint %s address space pointer",
5106 c_addr_space_name (as_from));
5108 else
5109 warning_at (loc, 0, "cast to %s address space pointer "
5110 "from disjoint %s address space pointer",
5111 c_addr_space_name (as_to),
5112 c_addr_space_name (as_from));
5116 /* Warn about possible alignment problems. */
5117 if (STRICT_ALIGNMENT
5118 && TREE_CODE (type) == POINTER_TYPE
5119 && TREE_CODE (otype) == POINTER_TYPE
5120 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5121 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5122 /* Don't warn about opaque types, where the actual alignment
5123 restriction is unknown. */
5124 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5125 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5126 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5127 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5128 warning_at (loc, OPT_Wcast_align,
5129 "cast increases required alignment of target type");
5131 if (TREE_CODE (type) == INTEGER_TYPE
5132 && TREE_CODE (otype) == POINTER_TYPE
5133 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5134 /* Unlike conversion of integers to pointers, where the
5135 warning is disabled for converting constants because
5136 of cases such as SIG_*, warn about converting constant
5137 pointers to integers. In some cases it may cause unwanted
5138 sign extension, and a warning is appropriate. */
5139 warning_at (loc, OPT_Wpointer_to_int_cast,
5140 "cast from pointer to integer of different size");
5142 if (TREE_CODE (value) == CALL_EXPR
5143 && TREE_CODE (type) != TREE_CODE (otype))
5144 warning_at (loc, OPT_Wbad_function_cast,
5145 "cast from function call of type %qT "
5146 "to non-matching type %qT", otype, type);
5148 if (TREE_CODE (type) == POINTER_TYPE
5149 && TREE_CODE (otype) == INTEGER_TYPE
5150 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5151 /* Don't warn about converting any constant. */
5152 && !TREE_CONSTANT (value))
5153 warning_at (loc,
5154 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5155 "of different size");
5157 if (warn_strict_aliasing <= 2)
5158 strict_aliasing_warning (otype, type, expr);
5160 /* If pedantic, warn for conversions between function and object
5161 pointer types, except for converting a null pointer constant
5162 to function pointer type. */
5163 if (pedantic
5164 && TREE_CODE (type) == POINTER_TYPE
5165 && TREE_CODE (otype) == POINTER_TYPE
5166 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5167 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5168 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5169 "conversion of function pointer to object pointer type");
5171 if (pedantic
5172 && TREE_CODE (type) == POINTER_TYPE
5173 && TREE_CODE (otype) == POINTER_TYPE
5174 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5175 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5176 && !null_pointer_constant_p (value))
5177 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5178 "conversion of object pointer to function pointer type");
5180 ovalue = value;
5181 value = convert (type, value);
5183 /* Ignore any integer overflow caused by the cast. */
5184 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5186 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5188 if (!TREE_OVERFLOW (value))
5190 /* Avoid clobbering a shared constant. */
5191 value = copy_node (value);
5192 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5195 else if (TREE_OVERFLOW (value))
5196 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5197 value = wide_int_to_tree (TREE_TYPE (value), value);
5201 /* Don't let a cast be an lvalue. */
5202 if (lvalue_p (value))
5203 value = non_lvalue_loc (loc, value);
5205 /* Don't allow the results of casting to floating-point or complex
5206 types be confused with actual constants, or casts involving
5207 integer and pointer types other than direct integer-to-integer
5208 and integer-to-pointer be confused with integer constant
5209 expressions and null pointer constants. */
5210 if (TREE_CODE (value) == REAL_CST
5211 || TREE_CODE (value) == COMPLEX_CST
5212 || (TREE_CODE (value) == INTEGER_CST
5213 && !((TREE_CODE (expr) == INTEGER_CST
5214 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5215 || TREE_CODE (expr) == REAL_CST
5216 || TREE_CODE (expr) == COMPLEX_CST)))
5217 value = build1 (NOP_EXPR, type, value);
5219 if (CAN_HAVE_LOCATION_P (value))
5220 SET_EXPR_LOCATION (value, loc);
5221 return value;
5224 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5225 location of the open paren of the cast, or the position of the cast
5226 expr. */
5227 tree
5228 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5230 tree type;
5231 tree type_expr = NULL_TREE;
5232 bool type_expr_const = true;
5233 tree ret;
5234 int saved_wsp = warn_strict_prototypes;
5236 /* This avoids warnings about unprototyped casts on
5237 integers. E.g. "#define SIG_DFL (void(*)())0". */
5238 if (TREE_CODE (expr) == INTEGER_CST)
5239 warn_strict_prototypes = 0;
5240 type = groktypename (type_name, &type_expr, &type_expr_const);
5241 warn_strict_prototypes = saved_wsp;
5243 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5244 && reject_gcc_builtin (expr))
5245 return error_mark_node;
5247 ret = build_c_cast (loc, type, expr);
5248 if (type_expr)
5250 bool inner_expr_const = true;
5251 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5252 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5253 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5254 && inner_expr_const);
5255 SET_EXPR_LOCATION (ret, loc);
5258 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5259 SET_EXPR_LOCATION (ret, loc);
5261 /* C++ does not permits types to be defined in a cast, but it
5262 allows references to incomplete types. */
5263 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5264 warning_at (loc, OPT_Wc___compat,
5265 "defining a type in a cast is invalid in C++");
5267 return ret;
5270 /* Build an assignment expression of lvalue LHS from value RHS.
5271 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5272 may differ from TREE_TYPE (LHS) for an enum bitfield.
5273 MODIFYCODE is the code for a binary operator that we use
5274 to combine the old value of LHS with RHS to get the new value.
5275 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5276 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5277 which may differ from TREE_TYPE (RHS) for an enum value.
5279 LOCATION is the location of the MODIFYCODE operator.
5280 RHS_LOC is the location of the RHS. */
5282 tree
5283 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5284 enum tree_code modifycode,
5285 location_t rhs_loc, tree rhs, tree rhs_origtype)
5287 tree result;
5288 tree newrhs;
5289 tree rhseval = NULL_TREE;
5290 tree rhs_semantic_type = NULL_TREE;
5291 tree lhstype = TREE_TYPE (lhs);
5292 tree olhstype = lhstype;
5293 bool npc;
5294 bool is_atomic_op;
5296 /* Types that aren't fully specified cannot be used in assignments. */
5297 lhs = require_complete_type (lhs);
5299 /* Avoid duplicate error messages from operands that had errors. */
5300 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5301 return error_mark_node;
5303 /* Ensure an error for assigning a non-lvalue array to an array in
5304 C90. */
5305 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5307 error_at (location, "assignment to expression with array type");
5308 return error_mark_node;
5311 /* For ObjC properties, defer this check. */
5312 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5313 return error_mark_node;
5315 is_atomic_op = really_atomic_lvalue (lhs);
5317 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5319 rhs_semantic_type = TREE_TYPE (rhs);
5320 rhs = TREE_OPERAND (rhs, 0);
5323 newrhs = rhs;
5325 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5327 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5328 lhs_origtype, modifycode, rhs_loc, rhs,
5329 rhs_origtype);
5330 if (inner == error_mark_node)
5331 return error_mark_node;
5332 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5333 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5334 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5335 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5336 protected_set_expr_location (result, location);
5337 return result;
5340 /* If a binary op has been requested, combine the old LHS value with the RHS
5341 producing the value we should actually store into the LHS. */
5343 if (modifycode != NOP_EXPR)
5345 lhs = c_fully_fold (lhs, false, NULL);
5346 lhs = stabilize_reference (lhs);
5348 /* Construct the RHS for any non-atomic compound assignemnt. */
5349 if (!is_atomic_op)
5351 /* If in LHS op= RHS the RHS has side-effects, ensure they
5352 are preevaluated before the rest of the assignment expression's
5353 side-effects, because RHS could contain e.g. function calls
5354 that modify LHS. */
5355 if (TREE_SIDE_EFFECTS (rhs))
5357 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5358 rhseval = newrhs;
5360 newrhs = build_binary_op (location,
5361 modifycode, lhs, newrhs, 1);
5363 /* The original type of the right hand side is no longer
5364 meaningful. */
5365 rhs_origtype = NULL_TREE;
5369 if (c_dialect_objc ())
5371 /* Check if we are modifying an Objective-C property reference;
5372 if so, we need to generate setter calls. */
5373 result = objc_maybe_build_modify_expr (lhs, newrhs);
5374 if (result)
5375 goto return_result;
5377 /* Else, do the check that we postponed for Objective-C. */
5378 if (!lvalue_or_else (location, lhs, lv_assign))
5379 return error_mark_node;
5382 /* Give an error for storing in something that is 'const'. */
5384 if (TYPE_READONLY (lhstype)
5385 || ((TREE_CODE (lhstype) == RECORD_TYPE
5386 || TREE_CODE (lhstype) == UNION_TYPE)
5387 && C_TYPE_FIELDS_READONLY (lhstype)))
5389 readonly_error (location, lhs, lv_assign);
5390 return error_mark_node;
5392 else if (TREE_READONLY (lhs))
5393 readonly_warning (lhs, lv_assign);
5395 /* If storing into a structure or union member,
5396 it has probably been given type `int'.
5397 Compute the type that would go with
5398 the actual amount of storage the member occupies. */
5400 if (TREE_CODE (lhs) == COMPONENT_REF
5401 && (TREE_CODE (lhstype) == INTEGER_TYPE
5402 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5403 || TREE_CODE (lhstype) == REAL_TYPE
5404 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5405 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5407 /* If storing in a field that is in actuality a short or narrower than one,
5408 we must store in the field in its actual type. */
5410 if (lhstype != TREE_TYPE (lhs))
5412 lhs = copy_node (lhs);
5413 TREE_TYPE (lhs) = lhstype;
5416 /* Issue -Wc++-compat warnings about an assignment to an enum type
5417 when LHS does not have its original type. This happens for,
5418 e.g., an enum bitfield in a struct. */
5419 if (warn_cxx_compat
5420 && lhs_origtype != NULL_TREE
5421 && lhs_origtype != lhstype
5422 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5424 tree checktype = (rhs_origtype != NULL_TREE
5425 ? rhs_origtype
5426 : TREE_TYPE (rhs));
5427 if (checktype != error_mark_node
5428 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5429 || (is_atomic_op && modifycode != NOP_EXPR)))
5430 warning_at (location, OPT_Wc___compat,
5431 "enum conversion in assignment is invalid in C++");
5434 /* If the lhs is atomic, remove that qualifier. */
5435 if (is_atomic_op)
5437 lhstype = build_qualified_type (lhstype,
5438 (TYPE_QUALS (lhstype)
5439 & ~TYPE_QUAL_ATOMIC));
5440 olhstype = build_qualified_type (olhstype,
5441 (TYPE_QUALS (lhstype)
5442 & ~TYPE_QUAL_ATOMIC));
5445 /* Convert new value to destination type. Fold it first, then
5446 restore any excess precision information, for the sake of
5447 conversion warnings. */
5449 if (!(is_atomic_op && modifycode != NOP_EXPR))
5451 npc = null_pointer_constant_p (newrhs);
5452 newrhs = c_fully_fold (newrhs, false, NULL);
5453 if (rhs_semantic_type)
5454 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5455 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5456 rhs_origtype, ic_assign, npc,
5457 NULL_TREE, NULL_TREE, 0);
5458 if (TREE_CODE (newrhs) == ERROR_MARK)
5459 return error_mark_node;
5462 /* Emit ObjC write barrier, if necessary. */
5463 if (c_dialect_objc () && flag_objc_gc)
5465 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5466 if (result)
5468 protected_set_expr_location (result, location);
5469 goto return_result;
5473 /* Scan operands. */
5475 if (is_atomic_op)
5476 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5477 else
5479 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5480 TREE_SIDE_EFFECTS (result) = 1;
5481 protected_set_expr_location (result, location);
5484 /* If we got the LHS in a different type for storing in,
5485 convert the result back to the nominal type of LHS
5486 so that the value we return always has the same type
5487 as the LHS argument. */
5489 if (olhstype == TREE_TYPE (result))
5490 goto return_result;
5492 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5493 rhs_origtype, ic_assign, false, NULL_TREE,
5494 NULL_TREE, 0);
5495 protected_set_expr_location (result, location);
5497 return_result:
5498 if (rhseval)
5499 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5500 return result;
5503 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5504 This is used to implement -fplan9-extensions. */
5506 static bool
5507 find_anonymous_field_with_type (tree struct_type, tree type)
5509 tree field;
5510 bool found;
5512 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5513 || TREE_CODE (struct_type) == UNION_TYPE);
5514 found = false;
5515 for (field = TYPE_FIELDS (struct_type);
5516 field != NULL_TREE;
5517 field = TREE_CHAIN (field))
5519 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5520 ? c_build_qualified_type (TREE_TYPE (field),
5521 TYPE_QUAL_ATOMIC)
5522 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5523 if (DECL_NAME (field) == NULL
5524 && comptypes (type, fieldtype))
5526 if (found)
5527 return false;
5528 found = true;
5530 else if (DECL_NAME (field) == NULL
5531 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5532 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5533 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5535 if (found)
5536 return false;
5537 found = true;
5540 return found;
5543 /* RHS is an expression whose type is pointer to struct. If there is
5544 an anonymous field in RHS with type TYPE, then return a pointer to
5545 that field in RHS. This is used with -fplan9-extensions. This
5546 returns NULL if no conversion could be found. */
5548 static tree
5549 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5551 tree rhs_struct_type, lhs_main_type;
5552 tree field, found_field;
5553 bool found_sub_field;
5554 tree ret;
5556 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5557 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5558 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5559 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5561 gcc_assert (POINTER_TYPE_P (type));
5562 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5563 ? c_build_qualified_type (TREE_TYPE (type),
5564 TYPE_QUAL_ATOMIC)
5565 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5567 found_field = NULL_TREE;
5568 found_sub_field = false;
5569 for (field = TYPE_FIELDS (rhs_struct_type);
5570 field != NULL_TREE;
5571 field = TREE_CHAIN (field))
5573 if (DECL_NAME (field) != NULL_TREE
5574 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5575 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5576 continue;
5577 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5578 ? c_build_qualified_type (TREE_TYPE (field),
5579 TYPE_QUAL_ATOMIC)
5580 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5581 if (comptypes (lhs_main_type, fieldtype))
5583 if (found_field != NULL_TREE)
5584 return NULL_TREE;
5585 found_field = field;
5587 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5588 lhs_main_type))
5590 if (found_field != NULL_TREE)
5591 return NULL_TREE;
5592 found_field = field;
5593 found_sub_field = true;
5597 if (found_field == NULL_TREE)
5598 return NULL_TREE;
5600 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5601 build_fold_indirect_ref (rhs), found_field,
5602 NULL_TREE);
5603 ret = build_fold_addr_expr_loc (location, ret);
5605 if (found_sub_field)
5607 ret = convert_to_anonymous_field (location, type, ret);
5608 gcc_assert (ret != NULL_TREE);
5611 return ret;
5614 /* Issue an error message for a bad initializer component.
5615 GMSGID identifies the message.
5616 The component name is taken from the spelling stack. */
5618 static void
5619 error_init (location_t loc, const char *gmsgid)
5621 char *ofwhat;
5623 /* The gmsgid may be a format string with %< and %>. */
5624 error_at (loc, gmsgid);
5625 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5626 if (*ofwhat)
5627 inform (loc, "(near initialization for %qs)", ofwhat);
5630 /* Issue a pedantic warning for a bad initializer component. OPT is
5631 the option OPT_* (from options.h) controlling this warning or 0 if
5632 it is unconditionally given. GMSGID identifies the message. The
5633 component name is taken from the spelling stack. */
5635 static void
5636 pedwarn_init (location_t location, int opt, const char *gmsgid)
5638 char *ofwhat;
5639 bool warned;
5641 /* The gmsgid may be a format string with %< and %>. */
5642 warned = pedwarn (location, opt, gmsgid);
5643 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5644 if (*ofwhat && warned)
5645 inform (location, "(near initialization for %qs)", ofwhat);
5648 /* Issue a warning for a bad initializer component.
5650 OPT is the OPT_W* value corresponding to the warning option that
5651 controls this warning. GMSGID identifies the message. The
5652 component name is taken from the spelling stack. */
5654 static void
5655 warning_init (location_t loc, int opt, const char *gmsgid)
5657 char *ofwhat;
5658 bool warned;
5660 /* The gmsgid may be a format string with %< and %>. */
5661 warned = warning_at (loc, opt, gmsgid);
5662 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5663 if (*ofwhat && warned)
5664 inform (loc, "(near initialization for %qs)", ofwhat);
5667 /* If TYPE is an array type and EXPR is a parenthesized string
5668 constant, warn if pedantic that EXPR is being used to initialize an
5669 object of type TYPE. */
5671 void
5672 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5674 if (pedantic
5675 && TREE_CODE (type) == ARRAY_TYPE
5676 && TREE_CODE (expr.value) == STRING_CST
5677 && expr.original_code != STRING_CST)
5678 pedwarn_init (loc, OPT_Wpedantic,
5679 "array initialized from parenthesized string constant");
5682 /* Convert value RHS to type TYPE as preparation for an assignment to
5683 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5684 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5685 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5686 constant before any folding.
5687 The real work of conversion is done by `convert'.
5688 The purpose of this function is to generate error messages
5689 for assignments that are not allowed in C.
5690 ERRTYPE says whether it is argument passing, assignment,
5691 initialization or return.
5693 LOCATION is the location of the assignment, EXPR_LOC is the location of
5694 the RHS or, for a function, location of an argument.
5695 FUNCTION is a tree for the function being called.
5696 PARMNUM is the number of the argument, for printing in error messages. */
5698 static tree
5699 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5700 tree rhs, tree origtype, enum impl_conv errtype,
5701 bool null_pointer_constant, tree fundecl,
5702 tree function, int parmnum)
5704 enum tree_code codel = TREE_CODE (type);
5705 tree orig_rhs = rhs;
5706 tree rhstype;
5707 enum tree_code coder;
5708 tree rname = NULL_TREE;
5709 bool objc_ok = false;
5711 if (errtype == ic_argpass)
5713 tree selector;
5714 /* Change pointer to function to the function itself for
5715 diagnostics. */
5716 if (TREE_CODE (function) == ADDR_EXPR
5717 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5718 function = TREE_OPERAND (function, 0);
5720 /* Handle an ObjC selector specially for diagnostics. */
5721 selector = objc_message_selector ();
5722 rname = function;
5723 if (selector && parmnum > 2)
5725 rname = selector;
5726 parmnum -= 2;
5730 /* This macro is used to emit diagnostics to ensure that all format
5731 strings are complete sentences, visible to gettext and checked at
5732 compile time. */
5733 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5734 do { \
5735 switch (errtype) \
5737 case ic_argpass: \
5738 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5739 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5740 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5741 "expected %qT but argument is of type %qT", \
5742 type, rhstype); \
5743 break; \
5744 case ic_assign: \
5745 pedwarn (LOCATION, OPT, AS); \
5746 break; \
5747 case ic_init: \
5748 pedwarn_init (LOCATION, OPT, IN); \
5749 break; \
5750 case ic_return: \
5751 pedwarn (LOCATION, OPT, RE); \
5752 break; \
5753 default: \
5754 gcc_unreachable (); \
5756 } while (0)
5758 /* This macro is used to emit diagnostics to ensure that all format
5759 strings are complete sentences, visible to gettext and checked at
5760 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
5761 extra parameter to enumerate qualifiers. */
5762 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5763 do { \
5764 switch (errtype) \
5766 case ic_argpass: \
5767 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5768 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5769 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5770 "expected %qT but argument is of type %qT", \
5771 type, rhstype); \
5772 break; \
5773 case ic_assign: \
5774 pedwarn (LOCATION, OPT, AS, QUALS); \
5775 break; \
5776 case ic_init: \
5777 pedwarn (LOCATION, OPT, IN, QUALS); \
5778 break; \
5779 case ic_return: \
5780 pedwarn (LOCATION, OPT, RE, QUALS); \
5781 break; \
5782 default: \
5783 gcc_unreachable (); \
5785 } while (0)
5787 /* This macro is used to emit diagnostics to ensure that all format
5788 strings are complete sentences, visible to gettext and checked at
5789 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5790 warning_at instead of pedwarn. */
5791 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5792 do { \
5793 switch (errtype) \
5795 case ic_argpass: \
5796 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5797 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5798 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5799 "expected %qT but argument is of type %qT", \
5800 type, rhstype); \
5801 break; \
5802 case ic_assign: \
5803 warning_at (LOCATION, OPT, AS, QUALS); \
5804 break; \
5805 case ic_init: \
5806 warning_at (LOCATION, OPT, IN, QUALS); \
5807 break; \
5808 case ic_return: \
5809 warning_at (LOCATION, OPT, RE, QUALS); \
5810 break; \
5811 default: \
5812 gcc_unreachable (); \
5814 } while (0)
5816 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5817 rhs = TREE_OPERAND (rhs, 0);
5819 rhstype = TREE_TYPE (rhs);
5820 coder = TREE_CODE (rhstype);
5822 if (coder == ERROR_MARK)
5823 return error_mark_node;
5825 if (c_dialect_objc ())
5827 int parmno;
5829 switch (errtype)
5831 case ic_return:
5832 parmno = 0;
5833 break;
5835 case ic_assign:
5836 parmno = -1;
5837 break;
5839 case ic_init:
5840 parmno = -2;
5841 break;
5843 default:
5844 parmno = parmnum;
5845 break;
5848 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5851 if (warn_cxx_compat)
5853 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5854 if (checktype != error_mark_node
5855 && TREE_CODE (type) == ENUMERAL_TYPE
5856 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5858 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5859 G_("enum conversion when passing argument "
5860 "%d of %qE is invalid in C++"),
5861 G_("enum conversion in assignment is "
5862 "invalid in C++"),
5863 G_("enum conversion in initialization is "
5864 "invalid in C++"),
5865 G_("enum conversion in return is "
5866 "invalid in C++"));
5870 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5871 return rhs;
5873 if (coder == VOID_TYPE)
5875 /* Except for passing an argument to an unprototyped function,
5876 this is a constraint violation. When passing an argument to
5877 an unprototyped function, it is compile-time undefined;
5878 making it a constraint in that case was rejected in
5879 DR#252. */
5880 error_at (location, "void value not ignored as it ought to be");
5881 return error_mark_node;
5883 rhs = require_complete_type (rhs);
5884 if (rhs == error_mark_node)
5885 return error_mark_node;
5887 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
5888 return error_mark_node;
5890 /* A non-reference type can convert to a reference. This handles
5891 va_start, va_copy and possibly port built-ins. */
5892 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5894 if (!lvalue_p (rhs))
5896 error_at (location, "cannot pass rvalue to reference parameter");
5897 return error_mark_node;
5899 if (!c_mark_addressable (rhs))
5900 return error_mark_node;
5901 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5902 SET_EXPR_LOCATION (rhs, location);
5904 rhs = convert_for_assignment (location, expr_loc,
5905 build_pointer_type (TREE_TYPE (type)),
5906 rhs, origtype, errtype,
5907 null_pointer_constant, fundecl, function,
5908 parmnum);
5909 if (rhs == error_mark_node)
5910 return error_mark_node;
5912 rhs = build1 (NOP_EXPR, type, rhs);
5913 SET_EXPR_LOCATION (rhs, location);
5914 return rhs;
5916 /* Some types can interconvert without explicit casts. */
5917 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5918 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5919 return convert (type, rhs);
5920 /* Arithmetic types all interconvert, and enum is treated like int. */
5921 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5922 || codel == FIXED_POINT_TYPE
5923 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5924 || codel == BOOLEAN_TYPE)
5925 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5926 || coder == FIXED_POINT_TYPE
5927 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5928 || coder == BOOLEAN_TYPE))
5930 tree ret;
5931 bool save = in_late_binary_op;
5932 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
5933 || (coder == REAL_TYPE
5934 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
5935 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
5936 in_late_binary_op = true;
5937 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5938 ? expr_loc : location, type, orig_rhs);
5939 in_late_binary_op = save;
5940 return ret;
5943 /* Aggregates in different TUs might need conversion. */
5944 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5945 && codel == coder
5946 && comptypes (type, rhstype))
5947 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5948 ? expr_loc : location, type, rhs);
5950 /* Conversion to a transparent union or record from its member types.
5951 This applies only to function arguments. */
5952 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5953 && TYPE_TRANSPARENT_AGGR (type))
5954 && errtype == ic_argpass)
5956 tree memb, marginal_memb = NULL_TREE;
5958 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5960 tree memb_type = TREE_TYPE (memb);
5962 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5963 TYPE_MAIN_VARIANT (rhstype)))
5964 break;
5966 if (TREE_CODE (memb_type) != POINTER_TYPE)
5967 continue;
5969 if (coder == POINTER_TYPE)
5971 tree ttl = TREE_TYPE (memb_type);
5972 tree ttr = TREE_TYPE (rhstype);
5974 /* Any non-function converts to a [const][volatile] void *
5975 and vice versa; otherwise, targets must be the same.
5976 Meanwhile, the lhs target must have all the qualifiers of
5977 the rhs. */
5978 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5979 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5980 || comp_target_types (location, memb_type, rhstype))
5982 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5983 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5984 /* If this type won't generate any warnings, use it. */
5985 if (lquals == rquals
5986 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5987 && TREE_CODE (ttl) == FUNCTION_TYPE)
5988 ? ((lquals | rquals) == rquals)
5989 : ((lquals | rquals) == lquals)))
5990 break;
5992 /* Keep looking for a better type, but remember this one. */
5993 if (!marginal_memb)
5994 marginal_memb = memb;
5998 /* Can convert integer zero to any pointer type. */
5999 if (null_pointer_constant)
6001 rhs = null_pointer_node;
6002 break;
6006 if (memb || marginal_memb)
6008 if (!memb)
6010 /* We have only a marginally acceptable member type;
6011 it needs a warning. */
6012 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6013 tree ttr = TREE_TYPE (rhstype);
6015 /* Const and volatile mean something different for function
6016 types, so the usual warnings are not appropriate. */
6017 if (TREE_CODE (ttr) == FUNCTION_TYPE
6018 && TREE_CODE (ttl) == FUNCTION_TYPE)
6020 /* Because const and volatile on functions are
6021 restrictions that say the function will not do
6022 certain things, it is okay to use a const or volatile
6023 function where an ordinary one is wanted, but not
6024 vice-versa. */
6025 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6026 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6027 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6028 OPT_Wdiscarded_qualifiers,
6029 G_("passing argument %d of %qE "
6030 "makes %q#v qualified function "
6031 "pointer from unqualified"),
6032 G_("assignment makes %q#v qualified "
6033 "function pointer from "
6034 "unqualified"),
6035 G_("initialization makes %q#v qualified "
6036 "function pointer from "
6037 "unqualified"),
6038 G_("return makes %q#v qualified function "
6039 "pointer from unqualified"),
6040 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6042 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6043 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6044 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6045 OPT_Wdiscarded_qualifiers,
6046 G_("passing argument %d of %qE discards "
6047 "%qv qualifier from pointer target type"),
6048 G_("assignment discards %qv qualifier "
6049 "from pointer target type"),
6050 G_("initialization discards %qv qualifier "
6051 "from pointer target type"),
6052 G_("return discards %qv qualifier from "
6053 "pointer target type"),
6054 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6056 memb = marginal_memb;
6059 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6060 pedwarn (location, OPT_Wpedantic,
6061 "ISO C prohibits argument conversion to union type");
6063 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6064 return build_constructor_single (type, memb, rhs);
6068 /* Conversions among pointers */
6069 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6070 && (coder == codel))
6072 tree ttl = TREE_TYPE (type);
6073 tree ttr = TREE_TYPE (rhstype);
6074 tree mvl = ttl;
6075 tree mvr = ttr;
6076 bool is_opaque_pointer;
6077 int target_cmp = 0; /* Cache comp_target_types () result. */
6078 addr_space_t asl;
6079 addr_space_t asr;
6081 if (TREE_CODE (mvl) != ARRAY_TYPE)
6082 mvl = (TYPE_ATOMIC (mvl)
6083 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6084 TYPE_QUAL_ATOMIC)
6085 : TYPE_MAIN_VARIANT (mvl));
6086 if (TREE_CODE (mvr) != ARRAY_TYPE)
6087 mvr = (TYPE_ATOMIC (mvr)
6088 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6089 TYPE_QUAL_ATOMIC)
6090 : TYPE_MAIN_VARIANT (mvr));
6091 /* Opaque pointers are treated like void pointers. */
6092 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6094 /* The Plan 9 compiler permits a pointer to a struct to be
6095 automatically converted into a pointer to an anonymous field
6096 within the struct. */
6097 if (flag_plan9_extensions
6098 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6099 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6100 && mvl != mvr)
6102 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6103 if (new_rhs != NULL_TREE)
6105 rhs = new_rhs;
6106 rhstype = TREE_TYPE (rhs);
6107 coder = TREE_CODE (rhstype);
6108 ttr = TREE_TYPE (rhstype);
6109 mvr = TYPE_MAIN_VARIANT (ttr);
6113 /* C++ does not allow the implicit conversion void* -> T*. However,
6114 for the purpose of reducing the number of false positives, we
6115 tolerate the special case of
6117 int *p = NULL;
6119 where NULL is typically defined in C to be '(void *) 0'. */
6120 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6121 warning_at (errtype == ic_argpass ? expr_loc : location,
6122 OPT_Wc___compat,
6123 "request for implicit conversion "
6124 "from %qT to %qT not permitted in C++", rhstype, type);
6126 /* See if the pointers point to incompatible address spaces. */
6127 asl = TYPE_ADDR_SPACE (ttl);
6128 asr = TYPE_ADDR_SPACE (ttr);
6129 if (!null_pointer_constant_p (rhs)
6130 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6132 switch (errtype)
6134 case ic_argpass:
6135 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6136 "non-enclosed address space", parmnum, rname);
6137 break;
6138 case ic_assign:
6139 error_at (location, "assignment from pointer to "
6140 "non-enclosed address space");
6141 break;
6142 case ic_init:
6143 error_at (location, "initialization from pointer to "
6144 "non-enclosed address space");
6145 break;
6146 case ic_return:
6147 error_at (location, "return from pointer to "
6148 "non-enclosed address space");
6149 break;
6150 default:
6151 gcc_unreachable ();
6153 return error_mark_node;
6156 /* Check if the right-hand side has a format attribute but the
6157 left-hand side doesn't. */
6158 if (warn_suggest_attribute_format
6159 && check_missing_format_attribute (type, rhstype))
6161 switch (errtype)
6163 case ic_argpass:
6164 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6165 "argument %d of %qE might be "
6166 "a candidate for a format attribute",
6167 parmnum, rname);
6168 break;
6169 case ic_assign:
6170 warning_at (location, OPT_Wsuggest_attribute_format,
6171 "assignment left-hand side might be "
6172 "a candidate for a format attribute");
6173 break;
6174 case ic_init:
6175 warning_at (location, OPT_Wsuggest_attribute_format,
6176 "initialization left-hand side might be "
6177 "a candidate for a format attribute");
6178 break;
6179 case ic_return:
6180 warning_at (location, OPT_Wsuggest_attribute_format,
6181 "return type might be "
6182 "a candidate for a format attribute");
6183 break;
6184 default:
6185 gcc_unreachable ();
6189 /* Any non-function converts to a [const][volatile] void *
6190 and vice versa; otherwise, targets must be the same.
6191 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6192 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6193 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6194 || (target_cmp = comp_target_types (location, type, rhstype))
6195 || is_opaque_pointer
6196 || ((c_common_unsigned_type (mvl)
6197 == c_common_unsigned_type (mvr))
6198 && (c_common_signed_type (mvl)
6199 == c_common_signed_type (mvr))
6200 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6202 /* Warn about loss of qualifers from pointers to arrays with
6203 qualifiers on the element type. */
6204 if (TREE_CODE (ttr) == ARRAY_TYPE)
6206 ttr = strip_array_types (ttr);
6207 ttl = strip_array_types (ttl);
6209 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6210 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6211 WARNING_FOR_QUALIFIERS (location, expr_loc,
6212 OPT_Wdiscarded_array_qualifiers,
6213 G_("passing argument %d of %qE discards "
6214 "%qv qualifier from pointer target type"),
6215 G_("assignment discards %qv qualifier "
6216 "from pointer target type"),
6217 G_("initialization discards %qv qualifier "
6218 "from pointer target type"),
6219 G_("return discards %qv qualifier from "
6220 "pointer target type"),
6221 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6223 else if (pedantic
6224 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6226 (VOID_TYPE_P (ttr)
6227 && !null_pointer_constant
6228 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6229 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6230 G_("ISO C forbids passing argument %d of "
6231 "%qE between function pointer "
6232 "and %<void *%>"),
6233 G_("ISO C forbids assignment between "
6234 "function pointer and %<void *%>"),
6235 G_("ISO C forbids initialization between "
6236 "function pointer and %<void *%>"),
6237 G_("ISO C forbids return between function "
6238 "pointer and %<void *%>"));
6239 /* Const and volatile mean something different for function types,
6240 so the usual warnings are not appropriate. */
6241 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6242 && TREE_CODE (ttl) != FUNCTION_TYPE)
6244 /* Don't warn about loss of qualifier for conversions from
6245 qualified void* to pointers to arrays with corresponding
6246 qualifier on the element type. */
6247 if (!pedantic)
6248 ttl = strip_array_types (ttl);
6250 /* Assignments between atomic and non-atomic objects are OK. */
6251 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6252 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6254 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6255 OPT_Wdiscarded_qualifiers,
6256 G_("passing argument %d of %qE discards "
6257 "%qv qualifier from pointer target type"),
6258 G_("assignment discards %qv qualifier "
6259 "from pointer target type"),
6260 G_("initialization discards %qv qualifier "
6261 "from pointer target type"),
6262 G_("return discards %qv qualifier from "
6263 "pointer target type"),
6264 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6266 /* If this is not a case of ignoring a mismatch in signedness,
6267 no warning. */
6268 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6269 || target_cmp)
6271 /* If there is a mismatch, do warn. */
6272 else if (warn_pointer_sign)
6273 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6274 G_("pointer targets in passing argument "
6275 "%d of %qE differ in signedness"),
6276 G_("pointer targets in assignment "
6277 "differ in signedness"),
6278 G_("pointer targets in initialization "
6279 "differ in signedness"),
6280 G_("pointer targets in return differ "
6281 "in signedness"));
6283 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6284 && TREE_CODE (ttr) == FUNCTION_TYPE)
6286 /* Because const and volatile on functions are restrictions
6287 that say the function will not do certain things,
6288 it is okay to use a const or volatile function
6289 where an ordinary one is wanted, but not vice-versa. */
6290 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6291 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6292 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6293 OPT_Wdiscarded_qualifiers,
6294 G_("passing argument %d of %qE makes "
6295 "%q#v qualified function pointer "
6296 "from unqualified"),
6297 G_("assignment makes %q#v qualified function "
6298 "pointer from unqualified"),
6299 G_("initialization makes %q#v qualified "
6300 "function pointer from unqualified"),
6301 G_("return makes %q#v qualified function "
6302 "pointer from unqualified"),
6303 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6306 else
6307 /* Avoid warning about the volatile ObjC EH puts on decls. */
6308 if (!objc_ok)
6309 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6310 OPT_Wincompatible_pointer_types,
6311 G_("passing argument %d of %qE from "
6312 "incompatible pointer type"),
6313 G_("assignment from incompatible pointer type"),
6314 G_("initialization from incompatible "
6315 "pointer type"),
6316 G_("return from incompatible pointer type"));
6318 return convert (type, rhs);
6320 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6322 /* ??? This should not be an error when inlining calls to
6323 unprototyped functions. */
6324 error_at (location, "invalid use of non-lvalue array");
6325 return error_mark_node;
6327 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6329 /* An explicit constant 0 can convert to a pointer,
6330 or one that results from arithmetic, even including
6331 a cast to integer type. */
6332 if (!null_pointer_constant)
6333 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6334 OPT_Wint_conversion,
6335 G_("passing argument %d of %qE makes "
6336 "pointer from integer without a cast"),
6337 G_("assignment makes pointer from integer "
6338 "without a cast"),
6339 G_("initialization makes pointer from "
6340 "integer without a cast"),
6341 G_("return makes pointer from integer "
6342 "without a cast"));
6344 return convert (type, rhs);
6346 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6348 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6349 OPT_Wint_conversion,
6350 G_("passing argument %d of %qE makes integer "
6351 "from pointer without a cast"),
6352 G_("assignment makes integer from pointer "
6353 "without a cast"),
6354 G_("initialization makes integer from pointer "
6355 "without a cast"),
6356 G_("return makes integer from pointer "
6357 "without a cast"));
6358 return convert (type, rhs);
6360 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6362 tree ret;
6363 bool save = in_late_binary_op;
6364 in_late_binary_op = true;
6365 ret = convert (type, rhs);
6366 in_late_binary_op = save;
6367 return ret;
6370 switch (errtype)
6372 case ic_argpass:
6373 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6374 rname);
6375 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6376 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6377 "expected %qT but argument is of type %qT", type, rhstype);
6378 break;
6379 case ic_assign:
6380 error_at (location, "incompatible types when assigning to type %qT from "
6381 "type %qT", type, rhstype);
6382 break;
6383 case ic_init:
6384 error_at (location,
6385 "incompatible types when initializing type %qT using type %qT",
6386 type, rhstype);
6387 break;
6388 case ic_return:
6389 error_at (location,
6390 "incompatible types when returning type %qT but %qT was "
6391 "expected", rhstype, type);
6392 break;
6393 default:
6394 gcc_unreachable ();
6397 return error_mark_node;
6400 /* If VALUE is a compound expr all of whose expressions are constant, then
6401 return its value. Otherwise, return error_mark_node.
6403 This is for handling COMPOUND_EXPRs as initializer elements
6404 which is allowed with a warning when -pedantic is specified. */
6406 static tree
6407 valid_compound_expr_initializer (tree value, tree endtype)
6409 if (TREE_CODE (value) == COMPOUND_EXPR)
6411 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6412 == error_mark_node)
6413 return error_mark_node;
6414 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6415 endtype);
6417 else if (!initializer_constant_valid_p (value, endtype))
6418 return error_mark_node;
6419 else
6420 return value;
6423 /* Perform appropriate conversions on the initial value of a variable,
6424 store it in the declaration DECL,
6425 and print any error messages that are appropriate.
6426 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6427 If the init is invalid, store an ERROR_MARK.
6429 INIT_LOC is the location of the initial value. */
6431 void
6432 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6434 tree value, type;
6435 bool npc = false;
6437 /* If variable's type was invalidly declared, just ignore it. */
6439 type = TREE_TYPE (decl);
6440 if (TREE_CODE (type) == ERROR_MARK)
6441 return;
6443 /* Digest the specified initializer into an expression. */
6445 if (init)
6446 npc = null_pointer_constant_p (init);
6447 value = digest_init (init_loc, type, init, origtype, npc,
6448 true, TREE_STATIC (decl));
6450 /* Store the expression if valid; else report error. */
6452 if (!in_system_header_at (input_location)
6453 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6454 warning (OPT_Wtraditional, "traditional C rejects automatic "
6455 "aggregate initialization");
6457 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6458 DECL_INITIAL (decl) = value;
6460 /* ANSI wants warnings about out-of-range constant initializers. */
6461 STRIP_TYPE_NOPS (value);
6462 if (TREE_STATIC (decl))
6463 constant_expression_warning (value);
6465 /* Check if we need to set array size from compound literal size. */
6466 if (TREE_CODE (type) == ARRAY_TYPE
6467 && TYPE_DOMAIN (type) == 0
6468 && value != error_mark_node)
6470 tree inside_init = init;
6472 STRIP_TYPE_NOPS (inside_init);
6473 inside_init = fold (inside_init);
6475 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6477 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6479 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6481 /* For int foo[] = (int [3]){1}; we need to set array size
6482 now since later on array initializer will be just the
6483 brace enclosed list of the compound literal. */
6484 tree etype = strip_array_types (TREE_TYPE (decl));
6485 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6486 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6487 layout_type (type);
6488 layout_decl (cldecl, 0);
6489 TREE_TYPE (decl)
6490 = c_build_qualified_type (type, TYPE_QUALS (etype));
6496 /* Methods for storing and printing names for error messages. */
6498 /* Implement a spelling stack that allows components of a name to be pushed
6499 and popped. Each element on the stack is this structure. */
6501 struct spelling
6503 int kind;
6504 union
6506 unsigned HOST_WIDE_INT i;
6507 const char *s;
6508 } u;
6511 #define SPELLING_STRING 1
6512 #define SPELLING_MEMBER 2
6513 #define SPELLING_BOUNDS 3
6515 static struct spelling *spelling; /* Next stack element (unused). */
6516 static struct spelling *spelling_base; /* Spelling stack base. */
6517 static int spelling_size; /* Size of the spelling stack. */
6519 /* Macros to save and restore the spelling stack around push_... functions.
6520 Alternative to SAVE_SPELLING_STACK. */
6522 #define SPELLING_DEPTH() (spelling - spelling_base)
6523 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6525 /* Push an element on the spelling stack with type KIND and assign VALUE
6526 to MEMBER. */
6528 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6530 int depth = SPELLING_DEPTH (); \
6532 if (depth >= spelling_size) \
6534 spelling_size += 10; \
6535 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6536 spelling_size); \
6537 RESTORE_SPELLING_DEPTH (depth); \
6540 spelling->kind = (KIND); \
6541 spelling->MEMBER = (VALUE); \
6542 spelling++; \
6545 /* Push STRING on the stack. Printed literally. */
6547 static void
6548 push_string (const char *string)
6550 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6553 /* Push a member name on the stack. Printed as '.' STRING. */
6555 static void
6556 push_member_name (tree decl)
6558 const char *const string
6559 = (DECL_NAME (decl)
6560 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6561 : _("<anonymous>"));
6562 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6565 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6567 static void
6568 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6570 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6573 /* Compute the maximum size in bytes of the printed spelling. */
6575 static int
6576 spelling_length (void)
6578 int size = 0;
6579 struct spelling *p;
6581 for (p = spelling_base; p < spelling; p++)
6583 if (p->kind == SPELLING_BOUNDS)
6584 size += 25;
6585 else
6586 size += strlen (p->u.s) + 1;
6589 return size;
6592 /* Print the spelling to BUFFER and return it. */
6594 static char *
6595 print_spelling (char *buffer)
6597 char *d = buffer;
6598 struct spelling *p;
6600 for (p = spelling_base; p < spelling; p++)
6601 if (p->kind == SPELLING_BOUNDS)
6603 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6604 d += strlen (d);
6606 else
6608 const char *s;
6609 if (p->kind == SPELLING_MEMBER)
6610 *d++ = '.';
6611 for (s = p->u.s; (*d = *s++); d++)
6614 *d++ = '\0';
6615 return buffer;
6618 /* Digest the parser output INIT as an initializer for type TYPE.
6619 Return a C expression of type TYPE to represent the initial value.
6621 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6623 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6625 If INIT is a string constant, STRICT_STRING is true if it is
6626 unparenthesized or we should not warn here for it being parenthesized.
6627 For other types of INIT, STRICT_STRING is not used.
6629 INIT_LOC is the location of the INIT.
6631 REQUIRE_CONSTANT requests an error if non-constant initializers or
6632 elements are seen. */
6634 static tree
6635 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6636 bool null_pointer_constant, bool strict_string,
6637 int require_constant)
6639 enum tree_code code = TREE_CODE (type);
6640 tree inside_init = init;
6641 tree semantic_type = NULL_TREE;
6642 bool maybe_const = true;
6644 if (type == error_mark_node
6645 || !init
6646 || error_operand_p (init))
6647 return error_mark_node;
6649 STRIP_TYPE_NOPS (inside_init);
6651 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6653 semantic_type = TREE_TYPE (inside_init);
6654 inside_init = TREE_OPERAND (inside_init, 0);
6656 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6657 inside_init = decl_constant_value_for_optimization (inside_init);
6659 /* Initialization of an array of chars from a string constant
6660 optionally enclosed in braces. */
6662 if (code == ARRAY_TYPE && inside_init
6663 && TREE_CODE (inside_init) == STRING_CST)
6665 tree typ1
6666 = (TYPE_ATOMIC (TREE_TYPE (type))
6667 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6668 TYPE_QUAL_ATOMIC)
6669 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6670 /* Note that an array could be both an array of character type
6671 and an array of wchar_t if wchar_t is signed char or unsigned
6672 char. */
6673 bool char_array = (typ1 == char_type_node
6674 || typ1 == signed_char_type_node
6675 || typ1 == unsigned_char_type_node);
6676 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6677 bool char16_array = !!comptypes (typ1, char16_type_node);
6678 bool char32_array = !!comptypes (typ1, char32_type_node);
6680 if (char_array || wchar_array || char16_array || char32_array)
6682 struct c_expr expr;
6683 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6684 expr.value = inside_init;
6685 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6686 expr.original_type = NULL;
6687 maybe_warn_string_init (init_loc, type, expr);
6689 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6690 pedwarn_init (init_loc, OPT_Wpedantic,
6691 "initialization of a flexible array member");
6693 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6694 TYPE_MAIN_VARIANT (type)))
6695 return inside_init;
6697 if (char_array)
6699 if (typ2 != char_type_node)
6701 error_init (init_loc, "char-array initialized from wide "
6702 "string");
6703 return error_mark_node;
6706 else
6708 if (typ2 == char_type_node)
6710 error_init (init_loc, "wide character array initialized "
6711 "from non-wide string");
6712 return error_mark_node;
6714 else if (!comptypes(typ1, typ2))
6716 error_init (init_loc, "wide character array initialized "
6717 "from incompatible wide string");
6718 return error_mark_node;
6722 TREE_TYPE (inside_init) = type;
6723 if (TYPE_DOMAIN (type) != 0
6724 && TYPE_SIZE (type) != 0
6725 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6727 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6729 /* Subtract the size of a single (possibly wide) character
6730 because it's ok to ignore the terminating null char
6731 that is counted in the length of the constant. */
6732 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6733 (len
6734 - (TYPE_PRECISION (typ1)
6735 / BITS_PER_UNIT))))
6736 pedwarn_init (init_loc, 0,
6737 ("initializer-string for array of chars "
6738 "is too long"));
6739 else if (warn_cxx_compat
6740 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6741 warning_at (init_loc, OPT_Wc___compat,
6742 ("initializer-string for array chars "
6743 "is too long for C++"));
6746 return inside_init;
6748 else if (INTEGRAL_TYPE_P (typ1))
6750 error_init (init_loc, "array of inappropriate type initialized "
6751 "from string constant");
6752 return error_mark_node;
6756 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6757 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6758 below and handle as a constructor. */
6759 if (code == VECTOR_TYPE
6760 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
6761 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6762 && TREE_CONSTANT (inside_init))
6764 if (TREE_CODE (inside_init) == VECTOR_CST
6765 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6766 TYPE_MAIN_VARIANT (type)))
6767 return inside_init;
6769 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6771 unsigned HOST_WIDE_INT ix;
6772 tree value;
6773 bool constant_p = true;
6775 /* Iterate through elements and check if all constructor
6776 elements are *_CSTs. */
6777 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6778 if (!CONSTANT_CLASS_P (value))
6780 constant_p = false;
6781 break;
6784 if (constant_p)
6785 return build_vector_from_ctor (type,
6786 CONSTRUCTOR_ELTS (inside_init));
6790 if (warn_sequence_point)
6791 verify_sequence_points (inside_init);
6793 /* Any type can be initialized
6794 from an expression of the same type, optionally with braces. */
6796 if (inside_init && TREE_TYPE (inside_init) != 0
6797 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6798 TYPE_MAIN_VARIANT (type))
6799 || (code == ARRAY_TYPE
6800 && comptypes (TREE_TYPE (inside_init), type))
6801 || (code == VECTOR_TYPE
6802 && comptypes (TREE_TYPE (inside_init), type))
6803 || (code == POINTER_TYPE
6804 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6805 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6806 TREE_TYPE (type)))))
6808 if (code == POINTER_TYPE)
6810 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6812 if (TREE_CODE (inside_init) == STRING_CST
6813 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6814 inside_init = array_to_pointer_conversion
6815 (init_loc, inside_init);
6816 else
6818 error_init (init_loc, "invalid use of non-lvalue array");
6819 return error_mark_node;
6824 if (code == VECTOR_TYPE)
6825 /* Although the types are compatible, we may require a
6826 conversion. */
6827 inside_init = convert (type, inside_init);
6829 if (require_constant
6830 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6832 /* As an extension, allow initializing objects with static storage
6833 duration with compound literals (which are then treated just as
6834 the brace enclosed list they contain). Also allow this for
6835 vectors, as we can only assign them with compound literals. */
6836 if (flag_isoc99 && code != VECTOR_TYPE)
6837 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6838 "is not constant");
6839 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6840 inside_init = DECL_INITIAL (decl);
6843 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6844 && TREE_CODE (inside_init) != CONSTRUCTOR)
6846 error_init (init_loc, "array initialized from non-constant array "
6847 "expression");
6848 return error_mark_node;
6851 /* Compound expressions can only occur here if -Wpedantic or
6852 -pedantic-errors is specified. In the later case, we always want
6853 an error. In the former case, we simply want a warning. */
6854 if (require_constant && pedantic
6855 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6857 inside_init
6858 = valid_compound_expr_initializer (inside_init,
6859 TREE_TYPE (inside_init));
6860 if (inside_init == error_mark_node)
6861 error_init (init_loc, "initializer element is not constant");
6862 else
6863 pedwarn_init (init_loc, OPT_Wpedantic,
6864 "initializer element is not constant");
6865 if (flag_pedantic_errors)
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 constant");
6873 inside_init = error_mark_node;
6875 else if (require_constant && !maybe_const)
6876 pedwarn_init (init_loc, OPT_Wpedantic,
6877 "initializer element is not a constant expression");
6879 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6880 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6881 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6882 type, inside_init, origtype,
6883 ic_init, null_pointer_constant,
6884 NULL_TREE, NULL_TREE, 0);
6885 return inside_init;
6888 /* Handle scalar types, including conversions. */
6890 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6891 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6892 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6894 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6895 && (TREE_CODE (init) == STRING_CST
6896 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6897 inside_init = init = array_to_pointer_conversion (init_loc, init);
6898 if (semantic_type)
6899 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6900 inside_init);
6901 inside_init
6902 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6903 inside_init, origtype, ic_init,
6904 null_pointer_constant, NULL_TREE, NULL_TREE,
6907 /* Check to see if we have already given an error message. */
6908 if (inside_init == error_mark_node)
6910 else if (require_constant && !TREE_CONSTANT (inside_init))
6912 error_init (init_loc, "initializer element is not constant");
6913 inside_init = error_mark_node;
6915 else if (require_constant
6916 && !initializer_constant_valid_p (inside_init,
6917 TREE_TYPE (inside_init)))
6919 error_init (init_loc, "initializer element is not computable at "
6920 "load time");
6921 inside_init = error_mark_node;
6923 else if (require_constant && !maybe_const)
6924 pedwarn_init (init_loc, OPT_Wpedantic,
6925 "initializer element is not a constant expression");
6927 return inside_init;
6930 /* Come here only for records and arrays. */
6932 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6934 error_init (init_loc, "variable-sized object may not be initialized");
6935 return error_mark_node;
6938 error_init (init_loc, "invalid initializer");
6939 return error_mark_node;
6942 /* Handle initializers that use braces. */
6944 /* Type of object we are accumulating a constructor for.
6945 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6946 static tree constructor_type;
6948 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6949 left to fill. */
6950 static tree constructor_fields;
6952 /* For an ARRAY_TYPE, this is the specified index
6953 at which to store the next element we get. */
6954 static tree constructor_index;
6956 /* For an ARRAY_TYPE, this is the maximum index. */
6957 static tree constructor_max_index;
6959 /* For a RECORD_TYPE, this is the first field not yet written out. */
6960 static tree constructor_unfilled_fields;
6962 /* For an ARRAY_TYPE, this is the index of the first element
6963 not yet written out. */
6964 static tree constructor_unfilled_index;
6966 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6967 This is so we can generate gaps between fields, when appropriate. */
6968 static tree constructor_bit_index;
6970 /* If we are saving up the elements rather than allocating them,
6971 this is the list of elements so far (in reverse order,
6972 most recent first). */
6973 static vec<constructor_elt, va_gc> *constructor_elements;
6975 /* 1 if constructor should be incrementally stored into a constructor chain,
6976 0 if all the elements should be kept in AVL tree. */
6977 static int constructor_incremental;
6979 /* 1 if so far this constructor's elements are all compile-time constants. */
6980 static int constructor_constant;
6982 /* 1 if so far this constructor's elements are all valid address constants. */
6983 static int constructor_simple;
6985 /* 1 if this constructor has an element that cannot be part of a
6986 constant expression. */
6987 static int constructor_nonconst;
6989 /* 1 if this constructor is erroneous so far. */
6990 static int constructor_erroneous;
6992 /* 1 if this constructor is the universal zero initializer { 0 }. */
6993 static int constructor_zeroinit;
6995 /* Structure for managing pending initializer elements, organized as an
6996 AVL tree. */
6998 struct init_node
7000 struct init_node *left, *right;
7001 struct init_node *parent;
7002 int balance;
7003 tree purpose;
7004 tree value;
7005 tree origtype;
7008 /* Tree of pending elements at this constructor level.
7009 These are elements encountered out of order
7010 which belong at places we haven't reached yet in actually
7011 writing the output.
7012 Will never hold tree nodes across GC runs. */
7013 static struct init_node *constructor_pending_elts;
7015 /* The SPELLING_DEPTH of this constructor. */
7016 static int constructor_depth;
7018 /* DECL node for which an initializer is being read.
7019 0 means we are reading a constructor expression
7020 such as (struct foo) {...}. */
7021 static tree constructor_decl;
7023 /* Nonzero if this is an initializer for a top-level decl. */
7024 static int constructor_top_level;
7026 /* Nonzero if there were any member designators in this initializer. */
7027 static int constructor_designated;
7029 /* Nesting depth of designator list. */
7030 static int designator_depth;
7032 /* Nonzero if there were diagnosed errors in this designator list. */
7033 static int designator_erroneous;
7036 /* This stack has a level for each implicit or explicit level of
7037 structuring in the initializer, including the outermost one. It
7038 saves the values of most of the variables above. */
7040 struct constructor_range_stack;
7042 struct constructor_stack
7044 struct constructor_stack *next;
7045 tree type;
7046 tree fields;
7047 tree index;
7048 tree max_index;
7049 tree unfilled_index;
7050 tree unfilled_fields;
7051 tree bit_index;
7052 vec<constructor_elt, va_gc> *elements;
7053 struct init_node *pending_elts;
7054 int offset;
7055 int depth;
7056 /* If value nonzero, this value should replace the entire
7057 constructor at this level. */
7058 struct c_expr replacement_value;
7059 struct constructor_range_stack *range_stack;
7060 char constant;
7061 char simple;
7062 char nonconst;
7063 char implicit;
7064 char erroneous;
7065 char outer;
7066 char incremental;
7067 char designated;
7068 int designator_depth;
7071 static struct constructor_stack *constructor_stack;
7073 /* This stack represents designators from some range designator up to
7074 the last designator in the list. */
7076 struct constructor_range_stack
7078 struct constructor_range_stack *next, *prev;
7079 struct constructor_stack *stack;
7080 tree range_start;
7081 tree index;
7082 tree range_end;
7083 tree fields;
7086 static struct constructor_range_stack *constructor_range_stack;
7088 /* This stack records separate initializers that are nested.
7089 Nested initializers can't happen in ANSI C, but GNU C allows them
7090 in cases like { ... (struct foo) { ... } ... }. */
7092 struct initializer_stack
7094 struct initializer_stack *next;
7095 tree decl;
7096 struct constructor_stack *constructor_stack;
7097 struct constructor_range_stack *constructor_range_stack;
7098 vec<constructor_elt, va_gc> *elements;
7099 struct spelling *spelling;
7100 struct spelling *spelling_base;
7101 int spelling_size;
7102 char top_level;
7103 char require_constant_value;
7104 char require_constant_elements;
7107 static struct initializer_stack *initializer_stack;
7109 /* Prepare to parse and output the initializer for variable DECL. */
7111 void
7112 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7114 const char *locus;
7115 struct initializer_stack *p = XNEW (struct initializer_stack);
7117 p->decl = constructor_decl;
7118 p->require_constant_value = require_constant_value;
7119 p->require_constant_elements = require_constant_elements;
7120 p->constructor_stack = constructor_stack;
7121 p->constructor_range_stack = constructor_range_stack;
7122 p->elements = constructor_elements;
7123 p->spelling = spelling;
7124 p->spelling_base = spelling_base;
7125 p->spelling_size = spelling_size;
7126 p->top_level = constructor_top_level;
7127 p->next = initializer_stack;
7128 initializer_stack = p;
7130 constructor_decl = decl;
7131 constructor_designated = 0;
7132 constructor_top_level = top_level;
7134 if (decl != 0 && decl != error_mark_node)
7136 require_constant_value = TREE_STATIC (decl);
7137 require_constant_elements
7138 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7139 /* For a scalar, you can always use any value to initialize,
7140 even within braces. */
7141 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7142 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7144 else
7146 require_constant_value = 0;
7147 require_constant_elements = 0;
7148 locus = _("(anonymous)");
7151 constructor_stack = 0;
7152 constructor_range_stack = 0;
7154 found_missing_braces = 0;
7156 spelling_base = 0;
7157 spelling_size = 0;
7158 RESTORE_SPELLING_DEPTH (0);
7160 if (locus)
7161 push_string (locus);
7164 void
7165 finish_init (void)
7167 struct initializer_stack *p = initializer_stack;
7169 /* Free the whole constructor stack of this initializer. */
7170 while (constructor_stack)
7172 struct constructor_stack *q = constructor_stack;
7173 constructor_stack = q->next;
7174 free (q);
7177 gcc_assert (!constructor_range_stack);
7179 /* Pop back to the data of the outer initializer (if any). */
7180 free (spelling_base);
7182 constructor_decl = p->decl;
7183 require_constant_value = p->require_constant_value;
7184 require_constant_elements = p->require_constant_elements;
7185 constructor_stack = p->constructor_stack;
7186 constructor_range_stack = p->constructor_range_stack;
7187 constructor_elements = p->elements;
7188 spelling = p->spelling;
7189 spelling_base = p->spelling_base;
7190 spelling_size = p->spelling_size;
7191 constructor_top_level = p->top_level;
7192 initializer_stack = p->next;
7193 free (p);
7196 /* Call here when we see the initializer is surrounded by braces.
7197 This is instead of a call to push_init_level;
7198 it is matched by a call to pop_init_level.
7200 TYPE is the type to initialize, for a constructor expression.
7201 For an initializer for a decl, TYPE is zero. */
7203 void
7204 really_start_incremental_init (tree type)
7206 struct constructor_stack *p = XNEW (struct constructor_stack);
7208 if (type == 0)
7209 type = TREE_TYPE (constructor_decl);
7211 if (VECTOR_TYPE_P (type)
7212 && TYPE_VECTOR_OPAQUE (type))
7213 error ("opaque vector types cannot be initialized");
7215 p->type = constructor_type;
7216 p->fields = constructor_fields;
7217 p->index = constructor_index;
7218 p->max_index = constructor_max_index;
7219 p->unfilled_index = constructor_unfilled_index;
7220 p->unfilled_fields = constructor_unfilled_fields;
7221 p->bit_index = constructor_bit_index;
7222 p->elements = constructor_elements;
7223 p->constant = constructor_constant;
7224 p->simple = constructor_simple;
7225 p->nonconst = constructor_nonconst;
7226 p->erroneous = constructor_erroneous;
7227 p->pending_elts = constructor_pending_elts;
7228 p->depth = constructor_depth;
7229 p->replacement_value.value = 0;
7230 p->replacement_value.original_code = ERROR_MARK;
7231 p->replacement_value.original_type = NULL;
7232 p->implicit = 0;
7233 p->range_stack = 0;
7234 p->outer = 0;
7235 p->incremental = constructor_incremental;
7236 p->designated = constructor_designated;
7237 p->designator_depth = designator_depth;
7238 p->next = 0;
7239 constructor_stack = p;
7241 constructor_constant = 1;
7242 constructor_simple = 1;
7243 constructor_nonconst = 0;
7244 constructor_depth = SPELLING_DEPTH ();
7245 constructor_elements = NULL;
7246 constructor_pending_elts = 0;
7247 constructor_type = type;
7248 constructor_incremental = 1;
7249 constructor_designated = 0;
7250 constructor_zeroinit = 1;
7251 designator_depth = 0;
7252 designator_erroneous = 0;
7254 if (TREE_CODE (constructor_type) == RECORD_TYPE
7255 || TREE_CODE (constructor_type) == UNION_TYPE)
7257 constructor_fields = TYPE_FIELDS (constructor_type);
7258 /* Skip any nameless bit fields at the beginning. */
7259 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7260 && DECL_NAME (constructor_fields) == 0)
7261 constructor_fields = DECL_CHAIN (constructor_fields);
7263 constructor_unfilled_fields = constructor_fields;
7264 constructor_bit_index = bitsize_zero_node;
7266 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7268 if (TYPE_DOMAIN (constructor_type))
7270 constructor_max_index
7271 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7273 /* Detect non-empty initializations of zero-length arrays. */
7274 if (constructor_max_index == NULL_TREE
7275 && TYPE_SIZE (constructor_type))
7276 constructor_max_index = integer_minus_one_node;
7278 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7279 to initialize VLAs will cause a proper error; avoid tree
7280 checking errors as well by setting a safe value. */
7281 if (constructor_max_index
7282 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7283 constructor_max_index = integer_minus_one_node;
7285 constructor_index
7286 = convert (bitsizetype,
7287 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7289 else
7291 constructor_index = bitsize_zero_node;
7292 constructor_max_index = NULL_TREE;
7295 constructor_unfilled_index = constructor_index;
7297 else if (VECTOR_TYPE_P (constructor_type))
7299 /* Vectors are like simple fixed-size arrays. */
7300 constructor_max_index =
7301 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7302 constructor_index = bitsize_zero_node;
7303 constructor_unfilled_index = constructor_index;
7305 else
7307 /* Handle the case of int x = {5}; */
7308 constructor_fields = constructor_type;
7309 constructor_unfilled_fields = constructor_type;
7313 /* Push down into a subobject, for initialization.
7314 If this is for an explicit set of braces, IMPLICIT is 0.
7315 If it is because the next element belongs at a lower level,
7316 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7318 void
7319 push_init_level (location_t loc, int implicit,
7320 struct obstack *braced_init_obstack)
7322 struct constructor_stack *p;
7323 tree value = NULL_TREE;
7325 /* If we've exhausted any levels that didn't have braces,
7326 pop them now. If implicit == 1, this will have been done in
7327 process_init_element; do not repeat it here because in the case
7328 of excess initializers for an empty aggregate this leads to an
7329 infinite cycle of popping a level and immediately recreating
7330 it. */
7331 if (implicit != 1)
7333 while (constructor_stack->implicit)
7335 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7336 || TREE_CODE (constructor_type) == UNION_TYPE)
7337 && constructor_fields == 0)
7338 process_init_element (input_location,
7339 pop_init_level (loc, 1, braced_init_obstack),
7340 true, braced_init_obstack);
7341 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7342 && constructor_max_index
7343 && tree_int_cst_lt (constructor_max_index,
7344 constructor_index))
7345 process_init_element (input_location,
7346 pop_init_level (loc, 1, braced_init_obstack),
7347 true, braced_init_obstack);
7348 else
7349 break;
7353 /* Unless this is an explicit brace, we need to preserve previous
7354 content if any. */
7355 if (implicit)
7357 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7358 || TREE_CODE (constructor_type) == UNION_TYPE)
7359 && constructor_fields)
7360 value = find_init_member (constructor_fields, braced_init_obstack);
7361 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7362 value = find_init_member (constructor_index, braced_init_obstack);
7365 p = XNEW (struct constructor_stack);
7366 p->type = constructor_type;
7367 p->fields = constructor_fields;
7368 p->index = constructor_index;
7369 p->max_index = constructor_max_index;
7370 p->unfilled_index = constructor_unfilled_index;
7371 p->unfilled_fields = constructor_unfilled_fields;
7372 p->bit_index = constructor_bit_index;
7373 p->elements = constructor_elements;
7374 p->constant = constructor_constant;
7375 p->simple = constructor_simple;
7376 p->nonconst = constructor_nonconst;
7377 p->erroneous = constructor_erroneous;
7378 p->pending_elts = constructor_pending_elts;
7379 p->depth = constructor_depth;
7380 p->replacement_value.value = 0;
7381 p->replacement_value.original_code = ERROR_MARK;
7382 p->replacement_value.original_type = NULL;
7383 p->implicit = implicit;
7384 p->outer = 0;
7385 p->incremental = constructor_incremental;
7386 p->designated = constructor_designated;
7387 p->designator_depth = designator_depth;
7388 p->next = constructor_stack;
7389 p->range_stack = 0;
7390 constructor_stack = p;
7392 constructor_constant = 1;
7393 constructor_simple = 1;
7394 constructor_nonconst = 0;
7395 constructor_depth = SPELLING_DEPTH ();
7396 constructor_elements = NULL;
7397 constructor_incremental = 1;
7398 constructor_designated = 0;
7399 constructor_pending_elts = 0;
7400 if (!implicit)
7402 p->range_stack = constructor_range_stack;
7403 constructor_range_stack = 0;
7404 designator_depth = 0;
7405 designator_erroneous = 0;
7408 /* Don't die if an entire brace-pair level is superfluous
7409 in the containing level. */
7410 if (constructor_type == 0)
7412 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7413 || TREE_CODE (constructor_type) == UNION_TYPE)
7415 /* Don't die if there are extra init elts at the end. */
7416 if (constructor_fields == 0)
7417 constructor_type = 0;
7418 else
7420 constructor_type = TREE_TYPE (constructor_fields);
7421 push_member_name (constructor_fields);
7422 constructor_depth++;
7424 /* If upper initializer is designated, then mark this as
7425 designated too to prevent bogus warnings. */
7426 constructor_designated = p->designated;
7428 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7430 constructor_type = TREE_TYPE (constructor_type);
7431 push_array_bounds (tree_to_uhwi (constructor_index));
7432 constructor_depth++;
7435 if (constructor_type == 0)
7437 error_init (loc, "extra brace group at end of initializer");
7438 constructor_fields = 0;
7439 constructor_unfilled_fields = 0;
7440 return;
7443 if (value && TREE_CODE (value) == CONSTRUCTOR)
7445 constructor_constant = TREE_CONSTANT (value);
7446 constructor_simple = TREE_STATIC (value);
7447 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7448 constructor_elements = CONSTRUCTOR_ELTS (value);
7449 if (!vec_safe_is_empty (constructor_elements)
7450 && (TREE_CODE (constructor_type) == RECORD_TYPE
7451 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7452 set_nonincremental_init (braced_init_obstack);
7455 if (implicit == 1)
7456 found_missing_braces = 1;
7458 if (TREE_CODE (constructor_type) == RECORD_TYPE
7459 || TREE_CODE (constructor_type) == UNION_TYPE)
7461 constructor_fields = TYPE_FIELDS (constructor_type);
7462 /* Skip any nameless bit fields at the beginning. */
7463 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7464 && DECL_NAME (constructor_fields) == 0)
7465 constructor_fields = DECL_CHAIN (constructor_fields);
7467 constructor_unfilled_fields = constructor_fields;
7468 constructor_bit_index = bitsize_zero_node;
7470 else if (VECTOR_TYPE_P (constructor_type))
7472 /* Vectors are like simple fixed-size arrays. */
7473 constructor_max_index =
7474 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7475 constructor_index = bitsize_int (0);
7476 constructor_unfilled_index = constructor_index;
7478 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7480 if (TYPE_DOMAIN (constructor_type))
7482 constructor_max_index
7483 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7485 /* Detect non-empty initializations of zero-length arrays. */
7486 if (constructor_max_index == NULL_TREE
7487 && TYPE_SIZE (constructor_type))
7488 constructor_max_index = integer_minus_one_node;
7490 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7491 to initialize VLAs will cause a proper error; avoid tree
7492 checking errors as well by setting a safe value. */
7493 if (constructor_max_index
7494 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7495 constructor_max_index = integer_minus_one_node;
7497 constructor_index
7498 = convert (bitsizetype,
7499 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7501 else
7502 constructor_index = bitsize_zero_node;
7504 constructor_unfilled_index = constructor_index;
7505 if (value && TREE_CODE (value) == STRING_CST)
7507 /* We need to split the char/wchar array into individual
7508 characters, so that we don't have to special case it
7509 everywhere. */
7510 set_nonincremental_init_from_string (value, braced_init_obstack);
7513 else
7515 if (constructor_type != error_mark_node)
7516 warning_init (input_location, 0, "braces around scalar initializer");
7517 constructor_fields = constructor_type;
7518 constructor_unfilled_fields = constructor_type;
7522 /* At the end of an implicit or explicit brace level,
7523 finish up that level of constructor. If a single expression
7524 with redundant braces initialized that level, return the
7525 c_expr structure for that expression. Otherwise, the original_code
7526 element is set to ERROR_MARK.
7527 If we were outputting the elements as they are read, return 0 as the value
7528 from inner levels (process_init_element ignores that),
7529 but return error_mark_node as the value from the outermost level
7530 (that's what we want to put in DECL_INITIAL).
7531 Otherwise, return a CONSTRUCTOR expression as the value. */
7533 struct c_expr
7534 pop_init_level (location_t loc, int implicit,
7535 struct obstack *braced_init_obstack)
7537 struct constructor_stack *p;
7538 struct c_expr ret;
7539 ret.value = 0;
7540 ret.original_code = ERROR_MARK;
7541 ret.original_type = NULL;
7543 if (implicit == 0)
7545 /* When we come to an explicit close brace,
7546 pop any inner levels that didn't have explicit braces. */
7547 while (constructor_stack->implicit)
7548 process_init_element (input_location,
7549 pop_init_level (loc, 1, braced_init_obstack),
7550 true, braced_init_obstack);
7551 gcc_assert (!constructor_range_stack);
7554 /* Now output all pending elements. */
7555 constructor_incremental = 1;
7556 output_pending_init_elements (1, braced_init_obstack);
7558 p = constructor_stack;
7560 /* Error for initializing a flexible array member, or a zero-length
7561 array member in an inappropriate context. */
7562 if (constructor_type && constructor_fields
7563 && TREE_CODE (constructor_type) == ARRAY_TYPE
7564 && TYPE_DOMAIN (constructor_type)
7565 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7567 /* Silently discard empty initializations. The parser will
7568 already have pedwarned for empty brackets. */
7569 if (integer_zerop (constructor_unfilled_index))
7570 constructor_type = NULL_TREE;
7571 else
7573 gcc_assert (!TYPE_SIZE (constructor_type));
7575 if (constructor_depth > 2)
7576 error_init (loc, "initialization of flexible array member in a nested context");
7577 else
7578 pedwarn_init (loc, OPT_Wpedantic,
7579 "initialization of a flexible array member");
7581 /* We have already issued an error message for the existence
7582 of a flexible array member not at the end of the structure.
7583 Discard the initializer so that we do not die later. */
7584 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7585 constructor_type = NULL_TREE;
7589 switch (vec_safe_length (constructor_elements))
7591 case 0:
7592 /* Initialization with { } counts as zeroinit. */
7593 constructor_zeroinit = 1;
7594 break;
7595 case 1:
7596 /* This might be zeroinit as well. */
7597 if (integer_zerop ((*constructor_elements)[0].value))
7598 constructor_zeroinit = 1;
7599 break;
7600 default:
7601 /* If the constructor has more than one element, it can't be { 0 }. */
7602 constructor_zeroinit = 0;
7603 break;
7606 /* Warn when some structs are initialized with direct aggregation. */
7607 if (!implicit && found_missing_braces && warn_missing_braces
7608 && !constructor_zeroinit)
7609 warning_init (loc, OPT_Wmissing_braces,
7610 "missing braces around initializer");
7612 /* Warn when some struct elements are implicitly initialized to zero. */
7613 if (warn_missing_field_initializers
7614 && constructor_type
7615 && TREE_CODE (constructor_type) == RECORD_TYPE
7616 && constructor_unfilled_fields)
7618 /* Do not warn for flexible array members or zero-length arrays. */
7619 while (constructor_unfilled_fields
7620 && (!DECL_SIZE (constructor_unfilled_fields)
7621 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7622 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7624 if (constructor_unfilled_fields
7625 /* Do not warn if this level of the initializer uses member
7626 designators; it is likely to be deliberate. */
7627 && !constructor_designated
7628 /* Do not warn about initializing with { 0 } or with { }. */
7629 && !constructor_zeroinit)
7631 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7632 "missing initializer for field %qD of %qT",
7633 constructor_unfilled_fields,
7634 constructor_type))
7635 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7636 "%qD declared here", constructor_unfilled_fields);
7640 /* Pad out the end of the structure. */
7641 if (p->replacement_value.value)
7642 /* If this closes a superfluous brace pair,
7643 just pass out the element between them. */
7644 ret = p->replacement_value;
7645 else if (constructor_type == 0)
7647 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7648 && TREE_CODE (constructor_type) != UNION_TYPE
7649 && TREE_CODE (constructor_type) != ARRAY_TYPE
7650 && !VECTOR_TYPE_P (constructor_type))
7652 /* A nonincremental scalar initializer--just return
7653 the element, after verifying there is just one. */
7654 if (vec_safe_is_empty (constructor_elements))
7656 if (!constructor_erroneous)
7657 error_init (loc, "empty scalar initializer");
7658 ret.value = error_mark_node;
7660 else if (vec_safe_length (constructor_elements) != 1)
7662 error_init (loc, "extra elements in scalar initializer");
7663 ret.value = (*constructor_elements)[0].value;
7665 else
7666 ret.value = (*constructor_elements)[0].value;
7668 else
7670 if (constructor_erroneous)
7671 ret.value = error_mark_node;
7672 else
7674 ret.value = build_constructor (constructor_type,
7675 constructor_elements);
7676 if (constructor_constant)
7677 TREE_CONSTANT (ret.value) = 1;
7678 if (constructor_constant && constructor_simple)
7679 TREE_STATIC (ret.value) = 1;
7680 if (constructor_nonconst)
7681 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7685 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7687 if (constructor_nonconst)
7688 ret.original_code = C_MAYBE_CONST_EXPR;
7689 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7690 ret.original_code = ERROR_MARK;
7693 constructor_type = p->type;
7694 constructor_fields = p->fields;
7695 constructor_index = p->index;
7696 constructor_max_index = p->max_index;
7697 constructor_unfilled_index = p->unfilled_index;
7698 constructor_unfilled_fields = p->unfilled_fields;
7699 constructor_bit_index = p->bit_index;
7700 constructor_elements = p->elements;
7701 constructor_constant = p->constant;
7702 constructor_simple = p->simple;
7703 constructor_nonconst = p->nonconst;
7704 constructor_erroneous = p->erroneous;
7705 constructor_incremental = p->incremental;
7706 constructor_designated = p->designated;
7707 designator_depth = p->designator_depth;
7708 constructor_pending_elts = p->pending_elts;
7709 constructor_depth = p->depth;
7710 if (!p->implicit)
7711 constructor_range_stack = p->range_stack;
7712 RESTORE_SPELLING_DEPTH (constructor_depth);
7714 constructor_stack = p->next;
7715 free (p);
7717 if (ret.value == 0 && constructor_stack == 0)
7718 ret.value = error_mark_node;
7719 return ret;
7722 /* Common handling for both array range and field name designators.
7723 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7725 static int
7726 set_designator (location_t loc, int array,
7727 struct obstack *braced_init_obstack)
7729 tree subtype;
7730 enum tree_code subcode;
7732 /* Don't die if an entire brace-pair level is superfluous
7733 in the containing level. */
7734 if (constructor_type == 0)
7735 return 1;
7737 /* If there were errors in this designator list already, bail out
7738 silently. */
7739 if (designator_erroneous)
7740 return 1;
7742 if (!designator_depth)
7744 gcc_assert (!constructor_range_stack);
7746 /* Designator list starts at the level of closest explicit
7747 braces. */
7748 while (constructor_stack->implicit)
7749 process_init_element (input_location,
7750 pop_init_level (loc, 1, braced_init_obstack),
7751 true, braced_init_obstack);
7752 constructor_designated = 1;
7753 return 0;
7756 switch (TREE_CODE (constructor_type))
7758 case RECORD_TYPE:
7759 case UNION_TYPE:
7760 subtype = TREE_TYPE (constructor_fields);
7761 if (subtype != error_mark_node)
7762 subtype = TYPE_MAIN_VARIANT (subtype);
7763 break;
7764 case ARRAY_TYPE:
7765 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7766 break;
7767 default:
7768 gcc_unreachable ();
7771 subcode = TREE_CODE (subtype);
7772 if (array && subcode != ARRAY_TYPE)
7774 error_init (loc, "array index in non-array initializer");
7775 return 1;
7777 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7779 error_init (loc, "field name not in record or union initializer");
7780 return 1;
7783 constructor_designated = 1;
7784 push_init_level (loc, 2, braced_init_obstack);
7785 return 0;
7788 /* If there are range designators in designator list, push a new designator
7789 to constructor_range_stack. RANGE_END is end of such stack range or
7790 NULL_TREE if there is no range designator at this level. */
7792 static void
7793 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7795 struct constructor_range_stack *p;
7797 p = (struct constructor_range_stack *)
7798 obstack_alloc (braced_init_obstack,
7799 sizeof (struct constructor_range_stack));
7800 p->prev = constructor_range_stack;
7801 p->next = 0;
7802 p->fields = constructor_fields;
7803 p->range_start = constructor_index;
7804 p->index = constructor_index;
7805 p->stack = constructor_stack;
7806 p->range_end = range_end;
7807 if (constructor_range_stack)
7808 constructor_range_stack->next = p;
7809 constructor_range_stack = p;
7812 /* Within an array initializer, specify the next index to be initialized.
7813 FIRST is that index. If LAST is nonzero, then initialize a range
7814 of indices, running from FIRST through LAST. */
7816 void
7817 set_init_index (location_t loc, tree first, tree last,
7818 struct obstack *braced_init_obstack)
7820 if (set_designator (loc, 1, braced_init_obstack))
7821 return;
7823 designator_erroneous = 1;
7825 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7826 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7828 error_init (loc, "array index in initializer not of integer type");
7829 return;
7832 if (TREE_CODE (first) != INTEGER_CST)
7834 first = c_fully_fold (first, false, NULL);
7835 if (TREE_CODE (first) == INTEGER_CST)
7836 pedwarn_init (loc, OPT_Wpedantic,
7837 "array index in initializer is not "
7838 "an integer constant expression");
7841 if (last && TREE_CODE (last) != INTEGER_CST)
7843 last = c_fully_fold (last, false, NULL);
7844 if (TREE_CODE (last) == INTEGER_CST)
7845 pedwarn_init (loc, OPT_Wpedantic,
7846 "array index in initializer is not "
7847 "an integer constant expression");
7850 if (TREE_CODE (first) != INTEGER_CST)
7851 error_init (loc, "nonconstant array index in initializer");
7852 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7853 error_init (loc, "nonconstant array index in initializer");
7854 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7855 error_init (loc, "array index in non-array initializer");
7856 else if (tree_int_cst_sgn (first) == -1)
7857 error_init (loc, "array index in initializer exceeds array bounds");
7858 else if (constructor_max_index
7859 && tree_int_cst_lt (constructor_max_index, first))
7860 error_init (loc, "array index in initializer exceeds array bounds");
7861 else
7863 constant_expression_warning (first);
7864 if (last)
7865 constant_expression_warning (last);
7866 constructor_index = convert (bitsizetype, first);
7867 if (tree_int_cst_lt (constructor_index, first))
7869 constructor_index = copy_node (constructor_index);
7870 TREE_OVERFLOW (constructor_index) = 1;
7873 if (last)
7875 if (tree_int_cst_equal (first, last))
7876 last = 0;
7877 else if (tree_int_cst_lt (last, first))
7879 error_init (loc, "empty index range in initializer");
7880 last = 0;
7882 else
7884 last = convert (bitsizetype, last);
7885 if (constructor_max_index != 0
7886 && tree_int_cst_lt (constructor_max_index, last))
7888 error_init (loc, "array index range in initializer exceeds "
7889 "array bounds");
7890 last = 0;
7895 designator_depth++;
7896 designator_erroneous = 0;
7897 if (constructor_range_stack || last)
7898 push_range_stack (last, braced_init_obstack);
7902 /* Within a struct initializer, specify the next field to be initialized. */
7904 void
7905 set_init_label (location_t loc, tree fieldname,
7906 struct obstack *braced_init_obstack)
7908 tree field;
7910 if (set_designator (loc, 0, braced_init_obstack))
7911 return;
7913 designator_erroneous = 1;
7915 if (TREE_CODE (constructor_type) != RECORD_TYPE
7916 && TREE_CODE (constructor_type) != UNION_TYPE)
7918 error_init (loc, "field name not in record or union initializer");
7919 return;
7922 field = lookup_field (constructor_type, fieldname);
7924 if (field == 0)
7925 error_at (loc, "unknown field %qE specified in initializer", fieldname);
7926 else
7929 constructor_fields = TREE_VALUE (field);
7930 designator_depth++;
7931 designator_erroneous = 0;
7932 if (constructor_range_stack)
7933 push_range_stack (NULL_TREE, braced_init_obstack);
7934 field = TREE_CHAIN (field);
7935 if (field)
7937 if (set_designator (loc, 0, braced_init_obstack))
7938 return;
7941 while (field != NULL_TREE);
7944 /* Add a new initializer to the tree of pending initializers. PURPOSE
7945 identifies the initializer, either array index or field in a structure.
7946 VALUE is the value of that index or field. If ORIGTYPE is not
7947 NULL_TREE, it is the original type of VALUE.
7949 IMPLICIT is true if value comes from pop_init_level (1),
7950 the new initializer has been merged with the existing one
7951 and thus no warnings should be emitted about overriding an
7952 existing initializer. */
7954 static void
7955 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7956 bool implicit, struct obstack *braced_init_obstack)
7958 struct init_node *p, **q, *r;
7960 q = &constructor_pending_elts;
7961 p = 0;
7963 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7965 while (*q != 0)
7967 p = *q;
7968 if (tree_int_cst_lt (purpose, p->purpose))
7969 q = &p->left;
7970 else if (tree_int_cst_lt (p->purpose, purpose))
7971 q = &p->right;
7972 else
7974 if (!implicit)
7976 if (TREE_SIDE_EFFECTS (p->value))
7977 warning_init (loc, OPT_Woverride_init_side_effects,
7978 "initialized field with side-effects "
7979 "overwritten");
7980 else if (warn_override_init)
7981 warning_init (loc, OPT_Woverride_init,
7982 "initialized field overwritten");
7984 p->value = value;
7985 p->origtype = origtype;
7986 return;
7990 else
7992 tree bitpos;
7994 bitpos = bit_position (purpose);
7995 while (*q != NULL)
7997 p = *q;
7998 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7999 q = &p->left;
8000 else if (p->purpose != purpose)
8001 q = &p->right;
8002 else
8004 if (!implicit)
8006 if (TREE_SIDE_EFFECTS (p->value))
8007 warning_init (loc, OPT_Woverride_init_side_effects,
8008 "initialized field with side-effects "
8009 "overwritten");
8010 else if (warn_override_init)
8011 warning_init (loc, OPT_Woverride_init,
8012 "initialized field overwritten");
8014 p->value = value;
8015 p->origtype = origtype;
8016 return;
8021 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8022 sizeof (struct init_node));
8023 r->purpose = purpose;
8024 r->value = value;
8025 r->origtype = origtype;
8027 *q = r;
8028 r->parent = p;
8029 r->left = 0;
8030 r->right = 0;
8031 r->balance = 0;
8033 while (p)
8035 struct init_node *s;
8037 if (r == p->left)
8039 if (p->balance == 0)
8040 p->balance = -1;
8041 else if (p->balance < 0)
8043 if (r->balance < 0)
8045 /* L rotation. */
8046 p->left = r->right;
8047 if (p->left)
8048 p->left->parent = p;
8049 r->right = p;
8051 p->balance = 0;
8052 r->balance = 0;
8054 s = p->parent;
8055 p->parent = r;
8056 r->parent = s;
8057 if (s)
8059 if (s->left == p)
8060 s->left = r;
8061 else
8062 s->right = r;
8064 else
8065 constructor_pending_elts = r;
8067 else
8069 /* LR rotation. */
8070 struct init_node *t = r->right;
8072 r->right = t->left;
8073 if (r->right)
8074 r->right->parent = r;
8075 t->left = r;
8077 p->left = t->right;
8078 if (p->left)
8079 p->left->parent = p;
8080 t->right = p;
8082 p->balance = t->balance < 0;
8083 r->balance = -(t->balance > 0);
8084 t->balance = 0;
8086 s = p->parent;
8087 p->parent = t;
8088 r->parent = t;
8089 t->parent = s;
8090 if (s)
8092 if (s->left == p)
8093 s->left = t;
8094 else
8095 s->right = t;
8097 else
8098 constructor_pending_elts = t;
8100 break;
8102 else
8104 /* p->balance == +1; growth of left side balances the node. */
8105 p->balance = 0;
8106 break;
8109 else /* r == p->right */
8111 if (p->balance == 0)
8112 /* Growth propagation from right side. */
8113 p->balance++;
8114 else if (p->balance > 0)
8116 if (r->balance > 0)
8118 /* R rotation. */
8119 p->right = r->left;
8120 if (p->right)
8121 p->right->parent = p;
8122 r->left = p;
8124 p->balance = 0;
8125 r->balance = 0;
8127 s = p->parent;
8128 p->parent = r;
8129 r->parent = s;
8130 if (s)
8132 if (s->left == p)
8133 s->left = r;
8134 else
8135 s->right = r;
8137 else
8138 constructor_pending_elts = r;
8140 else /* r->balance == -1 */
8142 /* RL rotation */
8143 struct init_node *t = r->left;
8145 r->left = t->right;
8146 if (r->left)
8147 r->left->parent = r;
8148 t->right = r;
8150 p->right = t->left;
8151 if (p->right)
8152 p->right->parent = p;
8153 t->left = p;
8155 r->balance = (t->balance < 0);
8156 p->balance = -(t->balance > 0);
8157 t->balance = 0;
8159 s = p->parent;
8160 p->parent = t;
8161 r->parent = t;
8162 t->parent = s;
8163 if (s)
8165 if (s->left == p)
8166 s->left = t;
8167 else
8168 s->right = t;
8170 else
8171 constructor_pending_elts = t;
8173 break;
8175 else
8177 /* p->balance == -1; growth of right side balances the node. */
8178 p->balance = 0;
8179 break;
8183 r = p;
8184 p = p->parent;
8188 /* Build AVL tree from a sorted chain. */
8190 static void
8191 set_nonincremental_init (struct obstack * braced_init_obstack)
8193 unsigned HOST_WIDE_INT ix;
8194 tree index, value;
8196 if (TREE_CODE (constructor_type) != RECORD_TYPE
8197 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8198 return;
8200 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8201 add_pending_init (input_location, index, value, NULL_TREE, true,
8202 braced_init_obstack);
8203 constructor_elements = NULL;
8204 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8206 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8207 /* Skip any nameless bit fields at the beginning. */
8208 while (constructor_unfilled_fields != 0
8209 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8210 && DECL_NAME (constructor_unfilled_fields) == 0)
8211 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8214 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8216 if (TYPE_DOMAIN (constructor_type))
8217 constructor_unfilled_index
8218 = convert (bitsizetype,
8219 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8220 else
8221 constructor_unfilled_index = bitsize_zero_node;
8223 constructor_incremental = 0;
8226 /* Build AVL tree from a string constant. */
8228 static void
8229 set_nonincremental_init_from_string (tree str,
8230 struct obstack * braced_init_obstack)
8232 tree value, purpose, type;
8233 HOST_WIDE_INT val[2];
8234 const char *p, *end;
8235 int byte, wchar_bytes, charwidth, bitpos;
8237 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8239 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8240 charwidth = TYPE_PRECISION (char_type_node);
8241 type = TREE_TYPE (constructor_type);
8242 p = TREE_STRING_POINTER (str);
8243 end = p + TREE_STRING_LENGTH (str);
8245 for (purpose = bitsize_zero_node;
8246 p < end
8247 && !(constructor_max_index
8248 && tree_int_cst_lt (constructor_max_index, purpose));
8249 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8251 if (wchar_bytes == 1)
8253 val[0] = (unsigned char) *p++;
8254 val[1] = 0;
8256 else
8258 val[1] = 0;
8259 val[0] = 0;
8260 for (byte = 0; byte < wchar_bytes; byte++)
8262 if (BYTES_BIG_ENDIAN)
8263 bitpos = (wchar_bytes - byte - 1) * charwidth;
8264 else
8265 bitpos = byte * charwidth;
8266 val[bitpos % HOST_BITS_PER_WIDE_INT]
8267 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8268 << (bitpos % HOST_BITS_PER_WIDE_INT);
8272 if (!TYPE_UNSIGNED (type))
8274 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8275 if (bitpos < HOST_BITS_PER_WIDE_INT)
8277 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8279 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8280 val[1] = -1;
8283 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8285 if (val[0] < 0)
8286 val[1] = -1;
8288 else if (val[1] & (((HOST_WIDE_INT) 1)
8289 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8290 val[1] |= ((HOST_WIDE_INT) -1)
8291 << (bitpos - HOST_BITS_PER_WIDE_INT);
8294 value = wide_int_to_tree (type,
8295 wide_int::from_array (val, 2,
8296 HOST_BITS_PER_WIDE_INT * 2));
8297 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8298 braced_init_obstack);
8301 constructor_incremental = 0;
8304 /* Return value of FIELD in pending initializer or zero if the field was
8305 not initialized yet. */
8307 static tree
8308 find_init_member (tree field, struct obstack * braced_init_obstack)
8310 struct init_node *p;
8312 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8314 if (constructor_incremental
8315 && tree_int_cst_lt (field, constructor_unfilled_index))
8316 set_nonincremental_init (braced_init_obstack);
8318 p = constructor_pending_elts;
8319 while (p)
8321 if (tree_int_cst_lt (field, p->purpose))
8322 p = p->left;
8323 else if (tree_int_cst_lt (p->purpose, field))
8324 p = p->right;
8325 else
8326 return p->value;
8329 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8331 tree bitpos = bit_position (field);
8333 if (constructor_incremental
8334 && (!constructor_unfilled_fields
8335 || tree_int_cst_lt (bitpos,
8336 bit_position (constructor_unfilled_fields))))
8337 set_nonincremental_init (braced_init_obstack);
8339 p = constructor_pending_elts;
8340 while (p)
8342 if (field == p->purpose)
8343 return p->value;
8344 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8345 p = p->left;
8346 else
8347 p = p->right;
8350 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8352 if (!vec_safe_is_empty (constructor_elements)
8353 && (constructor_elements->last ().index == field))
8354 return constructor_elements->last ().value;
8356 return 0;
8359 /* "Output" the next constructor element.
8360 At top level, really output it to assembler code now.
8361 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8362 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8363 TYPE is the data type that the containing data type wants here.
8364 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8365 If VALUE is a string constant, STRICT_STRING is true if it is
8366 unparenthesized or we should not warn here for it being parenthesized.
8367 For other types of VALUE, STRICT_STRING is not used.
8369 PENDING if non-nil means output pending elements that belong
8370 right after this element. (PENDING is normally 1;
8371 it is 0 while outputting pending elements, to avoid recursion.)
8373 IMPLICIT is true if value comes from pop_init_level (1),
8374 the new initializer has been merged with the existing one
8375 and thus no warnings should be emitted about overriding an
8376 existing initializer. */
8378 static void
8379 output_init_element (location_t loc, tree value, tree origtype,
8380 bool strict_string, tree type, tree field, int pending,
8381 bool implicit, struct obstack * braced_init_obstack)
8383 tree semantic_type = NULL_TREE;
8384 bool maybe_const = true;
8385 bool npc;
8387 if (type == error_mark_node || value == error_mark_node)
8389 constructor_erroneous = 1;
8390 return;
8392 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8393 && (TREE_CODE (value) == STRING_CST
8394 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8395 && !(TREE_CODE (value) == STRING_CST
8396 && TREE_CODE (type) == ARRAY_TYPE
8397 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8398 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8399 TYPE_MAIN_VARIANT (type)))
8400 value = array_to_pointer_conversion (input_location, value);
8402 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8403 && require_constant_value && pending)
8405 /* As an extension, allow initializing objects with static storage
8406 duration with compound literals (which are then treated just as
8407 the brace enclosed list they contain). */
8408 if (flag_isoc99)
8409 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8410 "constant");
8411 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8412 value = DECL_INITIAL (decl);
8415 npc = null_pointer_constant_p (value);
8416 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8418 semantic_type = TREE_TYPE (value);
8419 value = TREE_OPERAND (value, 0);
8421 value = c_fully_fold (value, require_constant_value, &maybe_const);
8423 if (value == error_mark_node)
8424 constructor_erroneous = 1;
8425 else if (!TREE_CONSTANT (value))
8426 constructor_constant = 0;
8427 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8428 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8429 || TREE_CODE (constructor_type) == UNION_TYPE)
8430 && DECL_C_BIT_FIELD (field)
8431 && TREE_CODE (value) != INTEGER_CST))
8432 constructor_simple = 0;
8433 if (!maybe_const)
8434 constructor_nonconst = 1;
8436 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8438 if (require_constant_value)
8440 error_init (loc, "initializer element is not constant");
8441 value = error_mark_node;
8443 else if (require_constant_elements)
8444 pedwarn (loc, OPT_Wpedantic,
8445 "initializer element is not computable at load time");
8447 else if (!maybe_const
8448 && (require_constant_value || require_constant_elements))
8449 pedwarn_init (loc, OPT_Wpedantic,
8450 "initializer element is not a constant expression");
8452 /* Issue -Wc++-compat warnings about initializing a bitfield with
8453 enum type. */
8454 if (warn_cxx_compat
8455 && field != NULL_TREE
8456 && TREE_CODE (field) == FIELD_DECL
8457 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8458 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8459 != TYPE_MAIN_VARIANT (type))
8460 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8462 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8463 if (checktype != error_mark_node
8464 && (TYPE_MAIN_VARIANT (checktype)
8465 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8466 warning_init (loc, OPT_Wc___compat,
8467 "enum conversion in initialization is invalid in C++");
8470 /* If this field is empty (and not at the end of structure),
8471 don't do anything other than checking the initializer. */
8472 if (field
8473 && (TREE_TYPE (field) == error_mark_node
8474 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8475 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8476 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8477 || DECL_CHAIN (field)))))
8478 return;
8480 if (semantic_type)
8481 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8482 value = digest_init (loc, type, value, origtype, npc, strict_string,
8483 require_constant_value);
8484 if (value == error_mark_node)
8486 constructor_erroneous = 1;
8487 return;
8489 if (require_constant_value || require_constant_elements)
8490 constant_expression_warning (value);
8492 /* If this element doesn't come next in sequence,
8493 put it on constructor_pending_elts. */
8494 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8495 && (!constructor_incremental
8496 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8498 if (constructor_incremental
8499 && tree_int_cst_lt (field, constructor_unfilled_index))
8500 set_nonincremental_init (braced_init_obstack);
8502 add_pending_init (loc, field, value, origtype, implicit,
8503 braced_init_obstack);
8504 return;
8506 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8507 && (!constructor_incremental
8508 || field != constructor_unfilled_fields))
8510 /* We do this for records but not for unions. In a union,
8511 no matter which field is specified, it can be initialized
8512 right away since it starts at the beginning of the union. */
8513 if (constructor_incremental)
8515 if (!constructor_unfilled_fields)
8516 set_nonincremental_init (braced_init_obstack);
8517 else
8519 tree bitpos, unfillpos;
8521 bitpos = bit_position (field);
8522 unfillpos = bit_position (constructor_unfilled_fields);
8524 if (tree_int_cst_lt (bitpos, unfillpos))
8525 set_nonincremental_init (braced_init_obstack);
8529 add_pending_init (loc, field, value, origtype, implicit,
8530 braced_init_obstack);
8531 return;
8533 else if (TREE_CODE (constructor_type) == UNION_TYPE
8534 && !vec_safe_is_empty (constructor_elements))
8536 if (!implicit)
8538 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8539 warning_init (loc, OPT_Woverride_init_side_effects,
8540 "initialized field with side-effects overwritten");
8541 else if (warn_override_init)
8542 warning_init (loc, OPT_Woverride_init,
8543 "initialized field overwritten");
8546 /* We can have just one union field set. */
8547 constructor_elements = NULL;
8550 /* Otherwise, output this element either to
8551 constructor_elements or to the assembler file. */
8553 constructor_elt celt = {field, value};
8554 vec_safe_push (constructor_elements, celt);
8556 /* Advance the variable that indicates sequential elements output. */
8557 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8558 constructor_unfilled_index
8559 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8560 bitsize_one_node);
8561 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8563 constructor_unfilled_fields
8564 = DECL_CHAIN (constructor_unfilled_fields);
8566 /* Skip any nameless bit fields. */
8567 while (constructor_unfilled_fields != 0
8568 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8569 && DECL_NAME (constructor_unfilled_fields) == 0)
8570 constructor_unfilled_fields =
8571 DECL_CHAIN (constructor_unfilled_fields);
8573 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8574 constructor_unfilled_fields = 0;
8576 /* Now output any pending elements which have become next. */
8577 if (pending)
8578 output_pending_init_elements (0, braced_init_obstack);
8581 /* Output any pending elements which have become next.
8582 As we output elements, constructor_unfilled_{fields,index}
8583 advances, which may cause other elements to become next;
8584 if so, they too are output.
8586 If ALL is 0, we return when there are
8587 no more pending elements to output now.
8589 If ALL is 1, we output space as necessary so that
8590 we can output all the pending elements. */
8591 static void
8592 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8594 struct init_node *elt = constructor_pending_elts;
8595 tree next;
8597 retry:
8599 /* Look through the whole pending tree.
8600 If we find an element that should be output now,
8601 output it. Otherwise, set NEXT to the element
8602 that comes first among those still pending. */
8604 next = 0;
8605 while (elt)
8607 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8609 if (tree_int_cst_equal (elt->purpose,
8610 constructor_unfilled_index))
8611 output_init_element (input_location, elt->value, elt->origtype,
8612 true, TREE_TYPE (constructor_type),
8613 constructor_unfilled_index, 0, false,
8614 braced_init_obstack);
8615 else if (tree_int_cst_lt (constructor_unfilled_index,
8616 elt->purpose))
8618 /* Advance to the next smaller node. */
8619 if (elt->left)
8620 elt = elt->left;
8621 else
8623 /* We have reached the smallest node bigger than the
8624 current unfilled index. Fill the space first. */
8625 next = elt->purpose;
8626 break;
8629 else
8631 /* Advance to the next bigger node. */
8632 if (elt->right)
8633 elt = elt->right;
8634 else
8636 /* We have reached the biggest node in a subtree. Find
8637 the parent of it, which is the next bigger node. */
8638 while (elt->parent && elt->parent->right == elt)
8639 elt = elt->parent;
8640 elt = elt->parent;
8641 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8642 elt->purpose))
8644 next = elt->purpose;
8645 break;
8650 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8651 || TREE_CODE (constructor_type) == UNION_TYPE)
8653 tree ctor_unfilled_bitpos, elt_bitpos;
8655 /* If the current record is complete we are done. */
8656 if (constructor_unfilled_fields == 0)
8657 break;
8659 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8660 elt_bitpos = bit_position (elt->purpose);
8661 /* We can't compare fields here because there might be empty
8662 fields in between. */
8663 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8665 constructor_unfilled_fields = elt->purpose;
8666 output_init_element (input_location, elt->value, elt->origtype,
8667 true, TREE_TYPE (elt->purpose),
8668 elt->purpose, 0, false,
8669 braced_init_obstack);
8671 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8673 /* Advance to the next smaller node. */
8674 if (elt->left)
8675 elt = elt->left;
8676 else
8678 /* We have reached the smallest node bigger than the
8679 current unfilled field. Fill the space first. */
8680 next = elt->purpose;
8681 break;
8684 else
8686 /* Advance to the next bigger node. */
8687 if (elt->right)
8688 elt = elt->right;
8689 else
8691 /* We have reached the biggest node in a subtree. Find
8692 the parent of it, which is the next bigger node. */
8693 while (elt->parent && elt->parent->right == elt)
8694 elt = elt->parent;
8695 elt = elt->parent;
8696 if (elt
8697 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8698 bit_position (elt->purpose))))
8700 next = elt->purpose;
8701 break;
8708 /* Ordinarily return, but not if we want to output all
8709 and there are elements left. */
8710 if (!(all && next != 0))
8711 return;
8713 /* If it's not incremental, just skip over the gap, so that after
8714 jumping to retry we will output the next successive element. */
8715 if (TREE_CODE (constructor_type) == RECORD_TYPE
8716 || TREE_CODE (constructor_type) == UNION_TYPE)
8717 constructor_unfilled_fields = next;
8718 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8719 constructor_unfilled_index = next;
8721 /* ELT now points to the node in the pending tree with the next
8722 initializer to output. */
8723 goto retry;
8726 /* Add one non-braced element to the current constructor level.
8727 This adjusts the current position within the constructor's type.
8728 This may also start or terminate implicit levels
8729 to handle a partly-braced initializer.
8731 Once this has found the correct level for the new element,
8732 it calls output_init_element.
8734 IMPLICIT is true if value comes from pop_init_level (1),
8735 the new initializer has been merged with the existing one
8736 and thus no warnings should be emitted about overriding an
8737 existing initializer. */
8739 void
8740 process_init_element (location_t loc, struct c_expr value, bool implicit,
8741 struct obstack * braced_init_obstack)
8743 tree orig_value = value.value;
8744 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8745 bool strict_string = value.original_code == STRING_CST;
8746 bool was_designated = designator_depth != 0;
8748 designator_depth = 0;
8749 designator_erroneous = 0;
8751 if (!implicit && value.value && !integer_zerop (value.value))
8752 constructor_zeroinit = 0;
8754 /* Handle superfluous braces around string cst as in
8755 char x[] = {"foo"}; */
8756 if (string_flag
8757 && constructor_type
8758 && !was_designated
8759 && TREE_CODE (constructor_type) == ARRAY_TYPE
8760 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8761 && integer_zerop (constructor_unfilled_index))
8763 if (constructor_stack->replacement_value.value)
8764 error_init (loc, "excess elements in char array initializer");
8765 constructor_stack->replacement_value = value;
8766 return;
8769 if (constructor_stack->replacement_value.value != 0)
8771 error_init (loc, "excess elements in struct initializer");
8772 return;
8775 /* Ignore elements of a brace group if it is entirely superfluous
8776 and has already been diagnosed. */
8777 if (constructor_type == 0)
8778 return;
8780 if (!implicit && warn_designated_init && !was_designated
8781 && TREE_CODE (constructor_type) == RECORD_TYPE
8782 && lookup_attribute ("designated_init",
8783 TYPE_ATTRIBUTES (constructor_type)))
8784 warning_init (loc,
8785 OPT_Wdesignated_init,
8786 "positional initialization of field "
8787 "in %<struct%> declared with %<designated_init%> attribute");
8789 /* If we've exhausted any levels that didn't have braces,
8790 pop them now. */
8791 while (constructor_stack->implicit)
8793 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8794 || TREE_CODE (constructor_type) == UNION_TYPE)
8795 && constructor_fields == 0)
8796 process_init_element (loc,
8797 pop_init_level (loc, 1, braced_init_obstack),
8798 true, braced_init_obstack);
8799 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8800 || VECTOR_TYPE_P (constructor_type))
8801 && constructor_max_index
8802 && tree_int_cst_lt (constructor_max_index,
8803 constructor_index))
8804 process_init_element (loc,
8805 pop_init_level (loc, 1, braced_init_obstack),
8806 true, braced_init_obstack);
8807 else
8808 break;
8811 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8812 if (constructor_range_stack)
8814 /* If value is a compound literal and we'll be just using its
8815 content, don't put it into a SAVE_EXPR. */
8816 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8817 || !require_constant_value)
8819 tree semantic_type = NULL_TREE;
8820 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8822 semantic_type = TREE_TYPE (value.value);
8823 value.value = TREE_OPERAND (value.value, 0);
8825 value.value = c_save_expr (value.value);
8826 if (semantic_type)
8827 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8828 value.value);
8832 while (1)
8834 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8836 tree fieldtype;
8837 enum tree_code fieldcode;
8839 if (constructor_fields == 0)
8841 pedwarn_init (loc, 0, "excess elements in struct initializer");
8842 break;
8845 fieldtype = TREE_TYPE (constructor_fields);
8846 if (fieldtype != error_mark_node)
8847 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8848 fieldcode = TREE_CODE (fieldtype);
8850 /* Error for non-static initialization of a flexible array member. */
8851 if (fieldcode == ARRAY_TYPE
8852 && !require_constant_value
8853 && TYPE_SIZE (fieldtype) == NULL_TREE
8854 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8856 error_init (loc, "non-static initialization of a flexible "
8857 "array member");
8858 break;
8861 /* Error for initialization of a flexible array member with
8862 a string constant if the structure is in an array. E.g.:
8863 struct S { int x; char y[]; };
8864 struct S s[] = { { 1, "foo" } };
8865 is invalid. */
8866 if (string_flag
8867 && fieldcode == ARRAY_TYPE
8868 && constructor_depth > 1
8869 && TYPE_SIZE (fieldtype) == NULL_TREE
8870 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8872 bool in_array_p = false;
8873 for (struct constructor_stack *p = constructor_stack;
8874 p && p->type; p = p->next)
8875 if (TREE_CODE (p->type) == ARRAY_TYPE)
8877 in_array_p = true;
8878 break;
8880 if (in_array_p)
8882 error_init (loc, "initialization of flexible array "
8883 "member in a nested context");
8884 break;
8888 /* Accept a string constant to initialize a subarray. */
8889 if (value.value != 0
8890 && fieldcode == ARRAY_TYPE
8891 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8892 && string_flag)
8893 value.value = orig_value;
8894 /* Otherwise, if we have come to a subaggregate,
8895 and we don't have an element of its type, push into it. */
8896 else if (value.value != 0
8897 && value.value != error_mark_node
8898 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8899 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8900 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8902 push_init_level (loc, 1, braced_init_obstack);
8903 continue;
8906 if (value.value)
8908 push_member_name (constructor_fields);
8909 output_init_element (loc, value.value, value.original_type,
8910 strict_string, fieldtype,
8911 constructor_fields, 1, implicit,
8912 braced_init_obstack);
8913 RESTORE_SPELLING_DEPTH (constructor_depth);
8915 else
8916 /* Do the bookkeeping for an element that was
8917 directly output as a constructor. */
8919 /* For a record, keep track of end position of last field. */
8920 if (DECL_SIZE (constructor_fields))
8921 constructor_bit_index
8922 = size_binop_loc (input_location, PLUS_EXPR,
8923 bit_position (constructor_fields),
8924 DECL_SIZE (constructor_fields));
8926 /* If the current field was the first one not yet written out,
8927 it isn't now, so update. */
8928 if (constructor_unfilled_fields == constructor_fields)
8930 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8931 /* Skip any nameless bit fields. */
8932 while (constructor_unfilled_fields != 0
8933 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8934 && DECL_NAME (constructor_unfilled_fields) == 0)
8935 constructor_unfilled_fields =
8936 DECL_CHAIN (constructor_unfilled_fields);
8940 constructor_fields = DECL_CHAIN (constructor_fields);
8941 /* Skip any nameless bit fields at the beginning. */
8942 while (constructor_fields != 0
8943 && DECL_C_BIT_FIELD (constructor_fields)
8944 && DECL_NAME (constructor_fields) == 0)
8945 constructor_fields = DECL_CHAIN (constructor_fields);
8947 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8949 tree fieldtype;
8950 enum tree_code fieldcode;
8952 if (constructor_fields == 0)
8954 pedwarn_init (loc, 0,
8955 "excess elements in union initializer");
8956 break;
8959 fieldtype = TREE_TYPE (constructor_fields);
8960 if (fieldtype != error_mark_node)
8961 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8962 fieldcode = TREE_CODE (fieldtype);
8964 /* Warn that traditional C rejects initialization of unions.
8965 We skip the warning if the value is zero. This is done
8966 under the assumption that the zero initializer in user
8967 code appears conditioned on e.g. __STDC__ to avoid
8968 "missing initializer" warnings and relies on default
8969 initialization to zero in the traditional C case.
8970 We also skip the warning if the initializer is designated,
8971 again on the assumption that this must be conditional on
8972 __STDC__ anyway (and we've already complained about the
8973 member-designator already). */
8974 if (!in_system_header_at (input_location) && !constructor_designated
8975 && !(value.value && (integer_zerop (value.value)
8976 || real_zerop (value.value))))
8977 warning (OPT_Wtraditional, "traditional C rejects initialization "
8978 "of unions");
8980 /* Accept a string constant to initialize a subarray. */
8981 if (value.value != 0
8982 && fieldcode == ARRAY_TYPE
8983 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8984 && string_flag)
8985 value.value = orig_value;
8986 /* Otherwise, if we have come to a subaggregate,
8987 and we don't have an element of its type, push into it. */
8988 else if (value.value != 0
8989 && value.value != error_mark_node
8990 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8991 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8992 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8994 push_init_level (loc, 1, braced_init_obstack);
8995 continue;
8998 if (value.value)
9000 push_member_name (constructor_fields);
9001 output_init_element (loc, value.value, value.original_type,
9002 strict_string, fieldtype,
9003 constructor_fields, 1, implicit,
9004 braced_init_obstack);
9005 RESTORE_SPELLING_DEPTH (constructor_depth);
9007 else
9008 /* Do the bookkeeping for an element that was
9009 directly output as a constructor. */
9011 constructor_bit_index = DECL_SIZE (constructor_fields);
9012 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9015 constructor_fields = 0;
9017 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9019 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9020 enum tree_code eltcode = TREE_CODE (elttype);
9022 /* Accept a string constant to initialize a subarray. */
9023 if (value.value != 0
9024 && eltcode == ARRAY_TYPE
9025 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9026 && string_flag)
9027 value.value = orig_value;
9028 /* Otherwise, if we have come to a subaggregate,
9029 and we don't have an element of its type, push into it. */
9030 else if (value.value != 0
9031 && value.value != error_mark_node
9032 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9033 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9034 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9036 push_init_level (loc, 1, braced_init_obstack);
9037 continue;
9040 if (constructor_max_index != 0
9041 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9042 || integer_all_onesp (constructor_max_index)))
9044 pedwarn_init (loc, 0,
9045 "excess elements in array initializer");
9046 break;
9049 /* Now output the actual element. */
9050 if (value.value)
9052 push_array_bounds (tree_to_uhwi (constructor_index));
9053 output_init_element (loc, value.value, value.original_type,
9054 strict_string, elttype,
9055 constructor_index, 1, implicit,
9056 braced_init_obstack);
9057 RESTORE_SPELLING_DEPTH (constructor_depth);
9060 constructor_index
9061 = size_binop_loc (input_location, PLUS_EXPR,
9062 constructor_index, bitsize_one_node);
9064 if (!value.value)
9065 /* If we are doing the bookkeeping for an element that was
9066 directly output as a constructor, we must update
9067 constructor_unfilled_index. */
9068 constructor_unfilled_index = constructor_index;
9070 else if (VECTOR_TYPE_P (constructor_type))
9072 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9074 /* Do a basic check of initializer size. Note that vectors
9075 always have a fixed size derived from their type. */
9076 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9078 pedwarn_init (loc, 0,
9079 "excess elements in vector initializer");
9080 break;
9083 /* Now output the actual element. */
9084 if (value.value)
9086 if (TREE_CODE (value.value) == VECTOR_CST)
9087 elttype = TYPE_MAIN_VARIANT (constructor_type);
9088 output_init_element (loc, value.value, value.original_type,
9089 strict_string, elttype,
9090 constructor_index, 1, implicit,
9091 braced_init_obstack);
9094 constructor_index
9095 = size_binop_loc (input_location,
9096 PLUS_EXPR, constructor_index, bitsize_one_node);
9098 if (!value.value)
9099 /* If we are doing the bookkeeping for an element that was
9100 directly output as a constructor, we must update
9101 constructor_unfilled_index. */
9102 constructor_unfilled_index = constructor_index;
9105 /* Handle the sole element allowed in a braced initializer
9106 for a scalar variable. */
9107 else if (constructor_type != error_mark_node
9108 && constructor_fields == 0)
9110 pedwarn_init (loc, 0,
9111 "excess elements in scalar initializer");
9112 break;
9114 else
9116 if (value.value)
9117 output_init_element (loc, value.value, value.original_type,
9118 strict_string, constructor_type,
9119 NULL_TREE, 1, implicit,
9120 braced_init_obstack);
9121 constructor_fields = 0;
9124 /* Handle range initializers either at this level or anywhere higher
9125 in the designator stack. */
9126 if (constructor_range_stack)
9128 struct constructor_range_stack *p, *range_stack;
9129 int finish = 0;
9131 range_stack = constructor_range_stack;
9132 constructor_range_stack = 0;
9133 while (constructor_stack != range_stack->stack)
9135 gcc_assert (constructor_stack->implicit);
9136 process_init_element (loc,
9137 pop_init_level (loc, 1,
9138 braced_init_obstack),
9139 true, braced_init_obstack);
9141 for (p = range_stack;
9142 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9143 p = p->prev)
9145 gcc_assert (constructor_stack->implicit);
9146 process_init_element (loc,
9147 pop_init_level (loc, 1,
9148 braced_init_obstack),
9149 true, braced_init_obstack);
9152 p->index = size_binop_loc (input_location,
9153 PLUS_EXPR, p->index, bitsize_one_node);
9154 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9155 finish = 1;
9157 while (1)
9159 constructor_index = p->index;
9160 constructor_fields = p->fields;
9161 if (finish && p->range_end && p->index == p->range_start)
9163 finish = 0;
9164 p->prev = 0;
9166 p = p->next;
9167 if (!p)
9168 break;
9169 push_init_level (loc, 2, braced_init_obstack);
9170 p->stack = constructor_stack;
9171 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9172 p->index = p->range_start;
9175 if (!finish)
9176 constructor_range_stack = range_stack;
9177 continue;
9180 break;
9183 constructor_range_stack = 0;
9186 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9187 (guaranteed to be 'volatile' or null) and ARGS (represented using
9188 an ASM_EXPR node). */
9189 tree
9190 build_asm_stmt (tree cv_qualifier, tree args)
9192 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9193 ASM_VOLATILE_P (args) = 1;
9194 return add_stmt (args);
9197 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9198 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9199 SIMPLE indicates whether there was anything at all after the
9200 string in the asm expression -- asm("blah") and asm("blah" : )
9201 are subtly different. We use a ASM_EXPR node to represent this. */
9202 tree
9203 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9204 tree clobbers, tree labels, bool simple)
9206 tree tail;
9207 tree args;
9208 int i;
9209 const char *constraint;
9210 const char **oconstraints;
9211 bool allows_mem, allows_reg, is_inout;
9212 int ninputs, noutputs;
9214 ninputs = list_length (inputs);
9215 noutputs = list_length (outputs);
9216 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9218 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9220 /* Remove output conversions that change the type but not the mode. */
9221 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9223 tree output = TREE_VALUE (tail);
9225 output = c_fully_fold (output, false, NULL);
9227 /* ??? Really, this should not be here. Users should be using a
9228 proper lvalue, dammit. But there's a long history of using casts
9229 in the output operands. In cases like longlong.h, this becomes a
9230 primitive form of typechecking -- if the cast can be removed, then
9231 the output operand had a type of the proper width; otherwise we'll
9232 get an error. Gross, but ... */
9233 STRIP_NOPS (output);
9235 if (!lvalue_or_else (loc, output, lv_asm))
9236 output = error_mark_node;
9238 if (output != error_mark_node
9239 && (TREE_READONLY (output)
9240 || TYPE_READONLY (TREE_TYPE (output))
9241 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9242 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9243 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9244 readonly_error (loc, output, lv_asm);
9246 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9247 oconstraints[i] = constraint;
9249 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9250 &allows_mem, &allows_reg, &is_inout))
9252 /* If the operand is going to end up in memory,
9253 mark it addressable. */
9254 if (!allows_reg && !c_mark_addressable (output))
9255 output = error_mark_node;
9256 if (!(!allows_reg && allows_mem)
9257 && output != error_mark_node
9258 && VOID_TYPE_P (TREE_TYPE (output)))
9260 error_at (loc, "invalid use of void expression");
9261 output = error_mark_node;
9264 else
9265 output = error_mark_node;
9267 TREE_VALUE (tail) = output;
9270 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9272 tree input;
9274 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9275 input = TREE_VALUE (tail);
9277 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9278 oconstraints, &allows_mem, &allows_reg))
9280 /* If the operand is going to end up in memory,
9281 mark it addressable. */
9282 if (!allows_reg && allows_mem)
9284 input = c_fully_fold (input, false, NULL);
9286 /* Strip the nops as we allow this case. FIXME, this really
9287 should be rejected or made deprecated. */
9288 STRIP_NOPS (input);
9289 if (!c_mark_addressable (input))
9290 input = error_mark_node;
9292 else
9294 struct c_expr expr;
9295 memset (&expr, 0, sizeof (expr));
9296 expr.value = input;
9297 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9298 input = c_fully_fold (expr.value, false, NULL);
9300 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9302 error_at (loc, "invalid use of void expression");
9303 input = error_mark_node;
9307 else
9308 input = error_mark_node;
9310 TREE_VALUE (tail) = input;
9313 /* ASMs with labels cannot have outputs. This should have been
9314 enforced by the parser. */
9315 gcc_assert (outputs == NULL || labels == NULL);
9317 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9319 /* asm statements without outputs, including simple ones, are treated
9320 as volatile. */
9321 ASM_INPUT_P (args) = simple;
9322 ASM_VOLATILE_P (args) = (noutputs == 0);
9324 return args;
9327 /* Generate a goto statement to LABEL. LOC is the location of the
9328 GOTO. */
9330 tree
9331 c_finish_goto_label (location_t loc, tree label)
9333 tree decl = lookup_label_for_goto (loc, label);
9334 if (!decl)
9335 return NULL_TREE;
9336 TREE_USED (decl) = 1;
9338 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9339 SET_EXPR_LOCATION (t, loc);
9340 return add_stmt (t);
9344 /* Generate a computed goto statement to EXPR. LOC is the location of
9345 the GOTO. */
9347 tree
9348 c_finish_goto_ptr (location_t loc, tree expr)
9350 tree t;
9351 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9352 expr = c_fully_fold (expr, false, NULL);
9353 expr = convert (ptr_type_node, expr);
9354 t = build1 (GOTO_EXPR, void_type_node, expr);
9355 SET_EXPR_LOCATION (t, loc);
9356 return add_stmt (t);
9359 /* Generate a C `return' statement. RETVAL is the expression for what
9360 to return, or a null pointer for `return;' with no value. LOC is
9361 the location of the return statement, or the location of the expression,
9362 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9363 is the original type of RETVAL. */
9365 tree
9366 c_finish_return (location_t loc, tree retval, tree origtype)
9368 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9369 bool no_warning = false;
9370 bool npc = false;
9371 size_t rank = 0;
9373 if (TREE_THIS_VOLATILE (current_function_decl))
9374 warning_at (loc, 0,
9375 "function declared %<noreturn%> has a %<return%> statement");
9377 if (flag_cilkplus && contains_array_notation_expr (retval))
9379 /* Array notations are allowed in a return statement if it is inside a
9380 built-in array notation reduction function. */
9381 if (!find_rank (loc, retval, retval, false, &rank))
9382 return error_mark_node;
9383 if (rank >= 1)
9385 error_at (loc, "array notation expression cannot be used as a "
9386 "return value");
9387 return error_mark_node;
9390 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9392 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9393 "allowed");
9394 return error_mark_node;
9396 if (retval)
9398 tree semantic_type = NULL_TREE;
9399 npc = null_pointer_constant_p (retval);
9400 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9402 semantic_type = TREE_TYPE (retval);
9403 retval = TREE_OPERAND (retval, 0);
9405 retval = c_fully_fold (retval, false, NULL);
9406 if (semantic_type)
9407 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9410 if (!retval)
9412 current_function_returns_null = 1;
9413 if ((warn_return_type || flag_isoc99)
9414 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9416 if (flag_isoc99)
9417 pedwarn (loc, 0, "%<return%> with no value, in "
9418 "function returning non-void");
9419 else
9420 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9421 "in function returning non-void");
9422 no_warning = true;
9425 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9427 current_function_returns_null = 1;
9428 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9429 pedwarn (loc, 0,
9430 "%<return%> with a value, in function returning void");
9431 else
9432 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9433 "%<return%> with expression, in function returning void");
9435 else
9437 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9438 retval, origtype, ic_return,
9439 npc, NULL_TREE, NULL_TREE, 0);
9440 tree res = DECL_RESULT (current_function_decl);
9441 tree inner;
9442 bool save;
9444 current_function_returns_value = 1;
9445 if (t == error_mark_node)
9446 return NULL_TREE;
9448 save = in_late_binary_op;
9449 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9450 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9451 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9452 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9453 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9454 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9455 in_late_binary_op = true;
9456 inner = t = convert (TREE_TYPE (res), t);
9457 in_late_binary_op = save;
9459 /* Strip any conversions, additions, and subtractions, and see if
9460 we are returning the address of a local variable. Warn if so. */
9461 while (1)
9463 switch (TREE_CODE (inner))
9465 CASE_CONVERT:
9466 case NON_LVALUE_EXPR:
9467 case PLUS_EXPR:
9468 case POINTER_PLUS_EXPR:
9469 inner = TREE_OPERAND (inner, 0);
9470 continue;
9472 case MINUS_EXPR:
9473 /* If the second operand of the MINUS_EXPR has a pointer
9474 type (or is converted from it), this may be valid, so
9475 don't give a warning. */
9477 tree op1 = TREE_OPERAND (inner, 1);
9479 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9480 && (CONVERT_EXPR_P (op1)
9481 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9482 op1 = TREE_OPERAND (op1, 0);
9484 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9485 break;
9487 inner = TREE_OPERAND (inner, 0);
9488 continue;
9491 case ADDR_EXPR:
9492 inner = TREE_OPERAND (inner, 0);
9494 while (REFERENCE_CLASS_P (inner)
9495 && !INDIRECT_REF_P (inner))
9496 inner = TREE_OPERAND (inner, 0);
9498 if (DECL_P (inner)
9499 && !DECL_EXTERNAL (inner)
9500 && !TREE_STATIC (inner)
9501 && DECL_CONTEXT (inner) == current_function_decl)
9503 if (TREE_CODE (inner) == LABEL_DECL)
9504 warning_at (loc, OPT_Wreturn_local_addr,
9505 "function returns address of label");
9506 else
9508 warning_at (loc, OPT_Wreturn_local_addr,
9509 "function returns address of local variable");
9510 tree zero = build_zero_cst (TREE_TYPE (res));
9511 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9514 break;
9516 default:
9517 break;
9520 break;
9523 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9524 SET_EXPR_LOCATION (retval, loc);
9526 if (warn_sequence_point)
9527 verify_sequence_points (retval);
9530 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9531 TREE_NO_WARNING (ret_stmt) |= no_warning;
9532 return add_stmt (ret_stmt);
9535 struct c_switch {
9536 /* The SWITCH_EXPR being built. */
9537 tree switch_expr;
9539 /* The original type of the testing expression, i.e. before the
9540 default conversion is applied. */
9541 tree orig_type;
9543 /* A splay-tree mapping the low element of a case range to the high
9544 element, or NULL_TREE if there is no high element. Used to
9545 determine whether or not a new case label duplicates an old case
9546 label. We need a tree, rather than simply a hash table, because
9547 of the GNU case range extension. */
9548 splay_tree cases;
9550 /* The bindings at the point of the switch. This is used for
9551 warnings crossing decls when branching to a case label. */
9552 struct c_spot_bindings *bindings;
9554 /* The next node on the stack. */
9555 struct c_switch *next;
9557 /* Remember whether the controlling expression had boolean type
9558 before integer promotions for the sake of -Wswitch-bool. */
9559 bool bool_cond_p;
9561 /* Remember whether there was a case value that is outside the
9562 range of the ORIG_TYPE. */
9563 bool outside_range_p;
9566 /* A stack of the currently active switch statements. The innermost
9567 switch statement is on the top of the stack. There is no need to
9568 mark the stack for garbage collection because it is only active
9569 during the processing of the body of a function, and we never
9570 collect at that point. */
9572 struct c_switch *c_switch_stack;
9574 /* Start a C switch statement, testing expression EXP. Return the new
9575 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9576 SWITCH_COND_LOC is the location of the switch's condition.
9577 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
9579 tree
9580 c_start_case (location_t switch_loc,
9581 location_t switch_cond_loc,
9582 tree exp, bool explicit_cast_p)
9584 tree orig_type = error_mark_node;
9585 bool bool_cond_p = false;
9586 struct c_switch *cs;
9588 if (exp != error_mark_node)
9590 orig_type = TREE_TYPE (exp);
9592 if (!INTEGRAL_TYPE_P (orig_type))
9594 if (orig_type != error_mark_node)
9596 error_at (switch_cond_loc, "switch quantity not an integer");
9597 orig_type = error_mark_node;
9599 exp = integer_zero_node;
9601 else
9603 tree type = TYPE_MAIN_VARIANT (orig_type);
9604 tree e = exp;
9606 /* Warn if the condition has boolean value. */
9607 while (TREE_CODE (e) == COMPOUND_EXPR)
9608 e = TREE_OPERAND (e, 1);
9610 if ((TREE_CODE (type) == BOOLEAN_TYPE
9611 || truth_value_p (TREE_CODE (e)))
9612 /* Explicit cast to int suppresses this warning. */
9613 && !(TREE_CODE (type) == INTEGER_TYPE
9614 && explicit_cast_p))
9615 bool_cond_p = true;
9617 if (!in_system_header_at (input_location)
9618 && (type == long_integer_type_node
9619 || type == long_unsigned_type_node))
9620 warning_at (switch_cond_loc,
9621 OPT_Wtraditional, "%<long%> switch expression not "
9622 "converted to %<int%> in ISO C");
9624 exp = c_fully_fold (exp, false, NULL);
9625 exp = default_conversion (exp);
9627 if (warn_sequence_point)
9628 verify_sequence_points (exp);
9632 /* Add this new SWITCH_EXPR to the stack. */
9633 cs = XNEW (struct c_switch);
9634 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9635 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9636 cs->orig_type = orig_type;
9637 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9638 cs->bindings = c_get_switch_bindings ();
9639 cs->bool_cond_p = bool_cond_p;
9640 cs->outside_range_p = false;
9641 cs->next = c_switch_stack;
9642 c_switch_stack = cs;
9644 return add_stmt (cs->switch_expr);
9647 /* Process a case label at location LOC. */
9649 tree
9650 do_case (location_t loc, tree low_value, tree high_value)
9652 tree label = NULL_TREE;
9654 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9656 low_value = c_fully_fold (low_value, false, NULL);
9657 if (TREE_CODE (low_value) == INTEGER_CST)
9658 pedwarn (loc, OPT_Wpedantic,
9659 "case label is not an integer constant expression");
9662 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9664 high_value = c_fully_fold (high_value, false, NULL);
9665 if (TREE_CODE (high_value) == INTEGER_CST)
9666 pedwarn (input_location, OPT_Wpedantic,
9667 "case label is not an integer constant expression");
9670 if (c_switch_stack == NULL)
9672 if (low_value)
9673 error_at (loc, "case label not within a switch statement");
9674 else
9675 error_at (loc, "%<default%> label not within a switch statement");
9676 return NULL_TREE;
9679 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9680 EXPR_LOCATION (c_switch_stack->switch_expr),
9681 loc))
9682 return NULL_TREE;
9684 label = c_add_case_label (loc, c_switch_stack->cases,
9685 SWITCH_COND (c_switch_stack->switch_expr),
9686 c_switch_stack->orig_type,
9687 low_value, high_value,
9688 &c_switch_stack->outside_range_p);
9689 if (label == error_mark_node)
9690 label = NULL_TREE;
9691 return label;
9694 /* Finish the switch statement. TYPE is the original type of the
9695 controlling expression of the switch, or NULL_TREE. */
9697 void
9698 c_finish_case (tree body, tree type)
9700 struct c_switch *cs = c_switch_stack;
9701 location_t switch_location;
9703 SWITCH_BODY (cs->switch_expr) = body;
9705 /* Emit warnings as needed. */
9706 switch_location = EXPR_LOCATION (cs->switch_expr);
9707 c_do_switch_warnings (cs->cases, switch_location,
9708 type ? type : TREE_TYPE (cs->switch_expr),
9709 SWITCH_COND (cs->switch_expr),
9710 cs->bool_cond_p, cs->outside_range_p);
9712 /* Pop the stack. */
9713 c_switch_stack = cs->next;
9714 splay_tree_delete (cs->cases);
9715 c_release_switch_bindings (cs->bindings);
9716 XDELETE (cs);
9719 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9720 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9721 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9722 statement, and was not surrounded with parenthesis. */
9724 void
9725 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9726 tree else_block, bool nested_if)
9728 tree stmt;
9730 /* If the condition has array notations, then the rank of the then_block and
9731 else_block must be either 0 or be equal to the rank of the condition. If
9732 the condition does not have array notations then break them up as it is
9733 broken up in a normal expression. */
9734 if (flag_cilkplus && contains_array_notation_expr (cond))
9736 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9737 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9738 return;
9739 if (then_block
9740 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9741 return;
9742 if (else_block
9743 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9744 return;
9745 if (cond_rank != then_rank && then_rank != 0)
9747 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9748 " and the then-block");
9749 return;
9751 else if (cond_rank != else_rank && else_rank != 0)
9753 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9754 " and the else-block");
9755 return;
9758 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9759 if (warn_parentheses && nested_if && else_block == NULL)
9761 tree inner_if = then_block;
9763 /* We know from the grammar productions that there is an IF nested
9764 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9765 it might not be exactly THEN_BLOCK, but should be the last
9766 non-container statement within. */
9767 while (1)
9768 switch (TREE_CODE (inner_if))
9770 case COND_EXPR:
9771 goto found;
9772 case BIND_EXPR:
9773 inner_if = BIND_EXPR_BODY (inner_if);
9774 break;
9775 case STATEMENT_LIST:
9776 inner_if = expr_last (then_block);
9777 break;
9778 case TRY_FINALLY_EXPR:
9779 case TRY_CATCH_EXPR:
9780 inner_if = TREE_OPERAND (inner_if, 0);
9781 break;
9782 default:
9783 gcc_unreachable ();
9785 found:
9787 if (COND_EXPR_ELSE (inner_if))
9788 warning_at (if_locus, OPT_Wparentheses,
9789 "suggest explicit braces to avoid ambiguous %<else%>");
9792 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9793 SET_EXPR_LOCATION (stmt, if_locus);
9794 add_stmt (stmt);
9797 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9798 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9799 is false for DO loops. INCR is the FOR increment expression. BODY is
9800 the statement controlled by the loop. BLAB is the break label. CLAB is
9801 the continue label. Everything is allowed to be NULL. */
9803 void
9804 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9805 tree blab, tree clab, bool cond_is_first)
9807 tree entry = NULL, exit = NULL, t;
9809 /* In theory could forbid cilk spawn for loop increment expression,
9810 but it should work just fine. */
9812 /* If the condition is zero don't generate a loop construct. */
9813 if (cond && integer_zerop (cond))
9815 if (cond_is_first)
9817 t = build_and_jump (&blab);
9818 SET_EXPR_LOCATION (t, start_locus);
9819 add_stmt (t);
9822 else
9824 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9826 /* If we have an exit condition, then we build an IF with gotos either
9827 out of the loop, or to the top of it. If there's no exit condition,
9828 then we just build a jump back to the top. */
9829 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9831 if (cond && !integer_nonzerop (cond))
9833 /* Canonicalize the loop condition to the end. This means
9834 generating a branch to the loop condition. Reuse the
9835 continue label, if possible. */
9836 if (cond_is_first)
9838 if (incr || !clab)
9840 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9841 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9843 else
9844 t = build1 (GOTO_EXPR, void_type_node, clab);
9845 SET_EXPR_LOCATION (t, start_locus);
9846 add_stmt (t);
9849 t = build_and_jump (&blab);
9850 if (cond_is_first)
9851 exit = fold_build3_loc (start_locus,
9852 COND_EXPR, void_type_node, cond, exit, t);
9853 else
9854 exit = fold_build3_loc (input_location,
9855 COND_EXPR, void_type_node, cond, exit, t);
9858 add_stmt (top);
9861 if (body)
9862 add_stmt (body);
9863 if (clab)
9864 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9865 if (incr)
9866 add_stmt (incr);
9867 if (entry)
9868 add_stmt (entry);
9869 if (exit)
9870 add_stmt (exit);
9871 if (blab)
9872 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9875 tree
9876 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9878 bool skip;
9879 tree label = *label_p;
9881 /* In switch statements break is sometimes stylistically used after
9882 a return statement. This can lead to spurious warnings about
9883 control reaching the end of a non-void function when it is
9884 inlined. Note that we are calling block_may_fallthru with
9885 language specific tree nodes; this works because
9886 block_may_fallthru returns true when given something it does not
9887 understand. */
9888 skip = !block_may_fallthru (cur_stmt_list);
9890 if (!label)
9892 if (!skip)
9893 *label_p = label = create_artificial_label (loc);
9895 else if (TREE_CODE (label) == LABEL_DECL)
9897 else switch (TREE_INT_CST_LOW (label))
9899 case 0:
9900 if (is_break)
9901 error_at (loc, "break statement not within loop or switch");
9902 else
9903 error_at (loc, "continue statement not within a loop");
9904 return NULL_TREE;
9906 case 1:
9907 gcc_assert (is_break);
9908 error_at (loc, "break statement used with OpenMP for loop");
9909 return NULL_TREE;
9911 case 2:
9912 if (is_break)
9913 error ("break statement within %<#pragma simd%> loop body");
9914 else
9915 error ("continue statement within %<#pragma simd%> loop body");
9916 return NULL_TREE;
9918 default:
9919 gcc_unreachable ();
9922 if (skip)
9923 return NULL_TREE;
9925 if (!is_break)
9926 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9928 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9931 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9933 static void
9934 emit_side_effect_warnings (location_t loc, tree expr)
9936 if (expr == error_mark_node)
9938 else if (!TREE_SIDE_EFFECTS (expr))
9940 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9941 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9943 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9945 tree r = expr;
9946 location_t cloc = loc;
9947 while (TREE_CODE (r) == COMPOUND_EXPR)
9949 if (EXPR_HAS_LOCATION (r))
9950 cloc = EXPR_LOCATION (r);
9951 r = TREE_OPERAND (r, 1);
9953 if (!TREE_SIDE_EFFECTS (r)
9954 && !VOID_TYPE_P (TREE_TYPE (r))
9955 && !CONVERT_EXPR_P (r)
9956 && !TREE_NO_WARNING (r)
9957 && !TREE_NO_WARNING (expr))
9958 warning_at (cloc, OPT_Wunused_value,
9959 "right-hand operand of comma expression has no effect");
9961 else
9962 warn_if_unused_value (expr, loc);
9965 /* Process an expression as if it were a complete statement. Emit
9966 diagnostics, but do not call ADD_STMT. LOC is the location of the
9967 statement. */
9969 tree
9970 c_process_expr_stmt (location_t loc, tree expr)
9972 tree exprv;
9974 if (!expr)
9975 return NULL_TREE;
9977 expr = c_fully_fold (expr, false, NULL);
9979 if (warn_sequence_point)
9980 verify_sequence_points (expr);
9982 if (TREE_TYPE (expr) != error_mark_node
9983 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9984 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9985 error_at (loc, "expression statement has incomplete type");
9987 /* If we're not processing a statement expression, warn about unused values.
9988 Warnings for statement expressions will be emitted later, once we figure
9989 out which is the result. */
9990 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9991 && warn_unused_value)
9992 emit_side_effect_warnings (loc, expr);
9994 exprv = expr;
9995 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9996 exprv = TREE_OPERAND (exprv, 1);
9997 while (CONVERT_EXPR_P (exprv))
9998 exprv = TREE_OPERAND (exprv, 0);
9999 if (DECL_P (exprv)
10000 || handled_component_p (exprv)
10001 || TREE_CODE (exprv) == ADDR_EXPR)
10002 mark_exp_read (exprv);
10004 /* If the expression is not of a type to which we cannot assign a line
10005 number, wrap the thing in a no-op NOP_EXPR. */
10006 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10008 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10009 SET_EXPR_LOCATION (expr, loc);
10012 return expr;
10015 /* Emit an expression as a statement. LOC is the location of the
10016 expression. */
10018 tree
10019 c_finish_expr_stmt (location_t loc, tree expr)
10021 if (expr)
10022 return add_stmt (c_process_expr_stmt (loc, expr));
10023 else
10024 return NULL;
10027 /* Do the opposite and emit a statement as an expression. To begin,
10028 create a new binding level and return it. */
10030 tree
10031 c_begin_stmt_expr (void)
10033 tree ret;
10035 /* We must force a BLOCK for this level so that, if it is not expanded
10036 later, there is a way to turn off the entire subtree of blocks that
10037 are contained in it. */
10038 keep_next_level ();
10039 ret = c_begin_compound_stmt (true);
10041 c_bindings_start_stmt_expr (c_switch_stack == NULL
10042 ? NULL
10043 : c_switch_stack->bindings);
10045 /* Mark the current statement list as belonging to a statement list. */
10046 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10048 return ret;
10051 /* LOC is the location of the compound statement to which this body
10052 belongs. */
10054 tree
10055 c_finish_stmt_expr (location_t loc, tree body)
10057 tree last, type, tmp, val;
10058 tree *last_p;
10060 body = c_end_compound_stmt (loc, body, true);
10062 c_bindings_end_stmt_expr (c_switch_stack == NULL
10063 ? NULL
10064 : c_switch_stack->bindings);
10066 /* Locate the last statement in BODY. See c_end_compound_stmt
10067 about always returning a BIND_EXPR. */
10068 last_p = &BIND_EXPR_BODY (body);
10069 last = BIND_EXPR_BODY (body);
10071 continue_searching:
10072 if (TREE_CODE (last) == STATEMENT_LIST)
10074 tree_stmt_iterator i;
10076 /* This can happen with degenerate cases like ({ }). No value. */
10077 if (!TREE_SIDE_EFFECTS (last))
10078 return body;
10080 /* If we're supposed to generate side effects warnings, process
10081 all of the statements except the last. */
10082 if (warn_unused_value)
10084 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10086 location_t tloc;
10087 tree t = tsi_stmt (i);
10089 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10090 emit_side_effect_warnings (tloc, t);
10093 else
10094 i = tsi_last (last);
10095 last_p = tsi_stmt_ptr (i);
10096 last = *last_p;
10099 /* If the end of the list is exception related, then the list was split
10100 by a call to push_cleanup. Continue searching. */
10101 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10102 || TREE_CODE (last) == TRY_CATCH_EXPR)
10104 last_p = &TREE_OPERAND (last, 0);
10105 last = *last_p;
10106 goto continue_searching;
10109 if (last == error_mark_node)
10110 return last;
10112 /* In the case that the BIND_EXPR is not necessary, return the
10113 expression out from inside it. */
10114 if (last == BIND_EXPR_BODY (body)
10115 && BIND_EXPR_VARS (body) == NULL)
10117 /* Even if this looks constant, do not allow it in a constant
10118 expression. */
10119 last = c_wrap_maybe_const (last, true);
10120 /* Do not warn if the return value of a statement expression is
10121 unused. */
10122 TREE_NO_WARNING (last) = 1;
10123 return last;
10126 /* Extract the type of said expression. */
10127 type = TREE_TYPE (last);
10129 /* If we're not returning a value at all, then the BIND_EXPR that
10130 we already have is a fine expression to return. */
10131 if (!type || VOID_TYPE_P (type))
10132 return body;
10134 /* Now that we've located the expression containing the value, it seems
10135 silly to make voidify_wrapper_expr repeat the process. Create a
10136 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10137 tmp = create_tmp_var_raw (type);
10139 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10140 tree_expr_nonnegative_p giving up immediately. */
10141 val = last;
10142 if (TREE_CODE (val) == NOP_EXPR
10143 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10144 val = TREE_OPERAND (val, 0);
10146 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10147 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10150 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10151 SET_EXPR_LOCATION (t, loc);
10152 return t;
10156 /* Begin and end compound statements. This is as simple as pushing
10157 and popping new statement lists from the tree. */
10159 tree
10160 c_begin_compound_stmt (bool do_scope)
10162 tree stmt = push_stmt_list ();
10163 if (do_scope)
10164 push_scope ();
10165 return stmt;
10168 /* End a compound statement. STMT is the statement. LOC is the
10169 location of the compound statement-- this is usually the location
10170 of the opening brace. */
10172 tree
10173 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10175 tree block = NULL;
10177 if (do_scope)
10179 if (c_dialect_objc ())
10180 objc_clear_super_receiver ();
10181 block = pop_scope ();
10184 stmt = pop_stmt_list (stmt);
10185 stmt = c_build_bind_expr (loc, block, stmt);
10187 /* If this compound statement is nested immediately inside a statement
10188 expression, then force a BIND_EXPR to be created. Otherwise we'll
10189 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10190 STATEMENT_LISTs merge, and thus we can lose track of what statement
10191 was really last. */
10192 if (building_stmt_list_p ()
10193 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10194 && TREE_CODE (stmt) != BIND_EXPR)
10196 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10197 TREE_SIDE_EFFECTS (stmt) = 1;
10198 SET_EXPR_LOCATION (stmt, loc);
10201 return stmt;
10204 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10205 when the current scope is exited. EH_ONLY is true when this is not
10206 meant to apply to normal control flow transfer. */
10208 void
10209 push_cleanup (tree decl, tree cleanup, bool eh_only)
10211 enum tree_code code;
10212 tree stmt, list;
10213 bool stmt_expr;
10215 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10216 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10217 add_stmt (stmt);
10218 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10219 list = push_stmt_list ();
10220 TREE_OPERAND (stmt, 0) = list;
10221 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10224 /* Build a binary-operation expression without default conversions.
10225 CODE is the kind of expression to build.
10226 LOCATION is the operator's location.
10227 This function differs from `build' in several ways:
10228 the data type of the result is computed and recorded in it,
10229 warnings are generated if arg data types are invalid,
10230 special handling for addition and subtraction of pointers is known,
10231 and some optimization is done (operations on narrow ints
10232 are done in the narrower type when that gives the same result).
10233 Constant folding is also done before the result is returned.
10235 Note that the operands will never have enumeral types, or function
10236 or array types, because either they will have the default conversions
10237 performed or they have both just been converted to some other type in which
10238 the arithmetic is to be done. */
10240 tree
10241 build_binary_op (location_t location, enum tree_code code,
10242 tree orig_op0, tree orig_op1, int convert_p)
10244 tree type0, type1, orig_type0, orig_type1;
10245 tree eptype;
10246 enum tree_code code0, code1;
10247 tree op0, op1;
10248 tree ret = error_mark_node;
10249 const char *invalid_op_diag;
10250 bool op0_int_operands, op1_int_operands;
10251 bool int_const, int_const_or_overflow, int_operands;
10253 /* Expression code to give to the expression when it is built.
10254 Normally this is CODE, which is what the caller asked for,
10255 but in some special cases we change it. */
10256 enum tree_code resultcode = code;
10258 /* Data type in which the computation is to be performed.
10259 In the simplest cases this is the common type of the arguments. */
10260 tree result_type = NULL;
10262 /* When the computation is in excess precision, the type of the
10263 final EXCESS_PRECISION_EXPR. */
10264 tree semantic_result_type = NULL;
10266 /* Nonzero means operands have already been type-converted
10267 in whatever way is necessary.
10268 Zero means they need to be converted to RESULT_TYPE. */
10269 int converted = 0;
10271 /* Nonzero means create the expression with this type, rather than
10272 RESULT_TYPE. */
10273 tree build_type = 0;
10275 /* Nonzero means after finally constructing the expression
10276 convert it to this type. */
10277 tree final_type = 0;
10279 /* Nonzero if this is an operation like MIN or MAX which can
10280 safely be computed in short if both args are promoted shorts.
10281 Also implies COMMON.
10282 -1 indicates a bitwise operation; this makes a difference
10283 in the exact conditions for when it is safe to do the operation
10284 in a narrower mode. */
10285 int shorten = 0;
10287 /* Nonzero if this is a comparison operation;
10288 if both args are promoted shorts, compare the original shorts.
10289 Also implies COMMON. */
10290 int short_compare = 0;
10292 /* Nonzero if this is a right-shift operation, which can be computed on the
10293 original short and then promoted if the operand is a promoted short. */
10294 int short_shift = 0;
10296 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10297 int common = 0;
10299 /* True means types are compatible as far as ObjC is concerned. */
10300 bool objc_ok;
10302 /* True means this is an arithmetic operation that may need excess
10303 precision. */
10304 bool may_need_excess_precision;
10306 /* True means this is a boolean operation that converts both its
10307 operands to truth-values. */
10308 bool boolean_op = false;
10310 /* Remember whether we're doing / or %. */
10311 bool doing_div_or_mod = false;
10313 /* Remember whether we're doing << or >>. */
10314 bool doing_shift = false;
10316 /* Tree holding instrumentation expression. */
10317 tree instrument_expr = NULL;
10319 if (location == UNKNOWN_LOCATION)
10320 location = input_location;
10322 op0 = orig_op0;
10323 op1 = orig_op1;
10325 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10326 if (op0_int_operands)
10327 op0 = remove_c_maybe_const_expr (op0);
10328 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10329 if (op1_int_operands)
10330 op1 = remove_c_maybe_const_expr (op1);
10331 int_operands = (op0_int_operands && op1_int_operands);
10332 if (int_operands)
10334 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10335 && TREE_CODE (orig_op1) == INTEGER_CST);
10336 int_const = (int_const_or_overflow
10337 && !TREE_OVERFLOW (orig_op0)
10338 && !TREE_OVERFLOW (orig_op1));
10340 else
10341 int_const = int_const_or_overflow = false;
10343 /* Do not apply default conversion in mixed vector/scalar expression. */
10344 if (convert_p
10345 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10347 op0 = default_conversion (op0);
10348 op1 = default_conversion (op1);
10351 /* When Cilk Plus is enabled and there are array notations inside op0, then
10352 we check to see if there are builtin array notation functions. If
10353 so, then we take on the type of the array notation inside it. */
10354 if (flag_cilkplus && contains_array_notation_expr (op0))
10355 orig_type0 = type0 = find_correct_array_notation_type (op0);
10356 else
10357 orig_type0 = type0 = TREE_TYPE (op0);
10359 if (flag_cilkplus && contains_array_notation_expr (op1))
10360 orig_type1 = type1 = find_correct_array_notation_type (op1);
10361 else
10362 orig_type1 = type1 = TREE_TYPE (op1);
10364 /* The expression codes of the data types of the arguments tell us
10365 whether the arguments are integers, floating, pointers, etc. */
10366 code0 = TREE_CODE (type0);
10367 code1 = TREE_CODE (type1);
10369 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10370 STRIP_TYPE_NOPS (op0);
10371 STRIP_TYPE_NOPS (op1);
10373 /* If an error was already reported for one of the arguments,
10374 avoid reporting another error. */
10376 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10377 return error_mark_node;
10379 if (code0 == POINTER_TYPE
10380 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10381 return error_mark_node;
10383 if (code1 == POINTER_TYPE
10384 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10385 return error_mark_node;
10387 if ((invalid_op_diag
10388 = targetm.invalid_binary_op (code, type0, type1)))
10390 error_at (location, invalid_op_diag);
10391 return error_mark_node;
10394 switch (code)
10396 case PLUS_EXPR:
10397 case MINUS_EXPR:
10398 case MULT_EXPR:
10399 case TRUNC_DIV_EXPR:
10400 case CEIL_DIV_EXPR:
10401 case FLOOR_DIV_EXPR:
10402 case ROUND_DIV_EXPR:
10403 case EXACT_DIV_EXPR:
10404 may_need_excess_precision = true;
10405 break;
10406 default:
10407 may_need_excess_precision = false;
10408 break;
10410 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10412 op0 = TREE_OPERAND (op0, 0);
10413 type0 = TREE_TYPE (op0);
10415 else if (may_need_excess_precision
10416 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10418 type0 = eptype;
10419 op0 = convert (eptype, op0);
10421 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10423 op1 = TREE_OPERAND (op1, 0);
10424 type1 = TREE_TYPE (op1);
10426 else if (may_need_excess_precision
10427 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10429 type1 = eptype;
10430 op1 = convert (eptype, op1);
10433 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10435 /* In case when one of the operands of the binary operation is
10436 a vector and another is a scalar -- convert scalar to vector. */
10437 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10439 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10440 true);
10442 switch (convert_flag)
10444 case stv_error:
10445 return error_mark_node;
10446 case stv_firstarg:
10448 bool maybe_const = true;
10449 tree sc;
10450 sc = c_fully_fold (op0, false, &maybe_const);
10451 sc = save_expr (sc);
10452 sc = convert (TREE_TYPE (type1), sc);
10453 op0 = build_vector_from_val (type1, sc);
10454 if (!maybe_const)
10455 op0 = c_wrap_maybe_const (op0, true);
10456 orig_type0 = type0 = TREE_TYPE (op0);
10457 code0 = TREE_CODE (type0);
10458 converted = 1;
10459 break;
10461 case stv_secondarg:
10463 bool maybe_const = true;
10464 tree sc;
10465 sc = c_fully_fold (op1, false, &maybe_const);
10466 sc = save_expr (sc);
10467 sc = convert (TREE_TYPE (type0), sc);
10468 op1 = build_vector_from_val (type0, sc);
10469 if (!maybe_const)
10470 op1 = c_wrap_maybe_const (op1, true);
10471 orig_type1 = type1 = TREE_TYPE (op1);
10472 code1 = TREE_CODE (type1);
10473 converted = 1;
10474 break;
10476 default:
10477 break;
10481 switch (code)
10483 case PLUS_EXPR:
10484 /* Handle the pointer + int case. */
10485 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10487 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10488 goto return_build_binary_op;
10490 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10492 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10493 goto return_build_binary_op;
10495 else
10496 common = 1;
10497 break;
10499 case MINUS_EXPR:
10500 /* Subtraction of two similar pointers.
10501 We must subtract them as integers, then divide by object size. */
10502 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10503 && comp_target_types (location, type0, type1))
10505 ret = pointer_diff (location, op0, op1);
10506 goto return_build_binary_op;
10508 /* Handle pointer minus int. Just like pointer plus int. */
10509 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10511 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10512 goto return_build_binary_op;
10514 else
10515 common = 1;
10516 break;
10518 case MULT_EXPR:
10519 common = 1;
10520 break;
10522 case TRUNC_DIV_EXPR:
10523 case CEIL_DIV_EXPR:
10524 case FLOOR_DIV_EXPR:
10525 case ROUND_DIV_EXPR:
10526 case EXACT_DIV_EXPR:
10527 doing_div_or_mod = true;
10528 warn_for_div_by_zero (location, op1);
10530 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10531 || code0 == FIXED_POINT_TYPE
10532 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10533 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10534 || code1 == FIXED_POINT_TYPE
10535 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10537 enum tree_code tcode0 = code0, tcode1 = code1;
10539 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10540 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10541 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10542 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10544 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10545 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10546 resultcode = RDIV_EXPR;
10547 else
10548 /* Although it would be tempting to shorten always here, that
10549 loses on some targets, since the modulo instruction is
10550 undefined if the quotient can't be represented in the
10551 computation mode. We shorten only if unsigned or if
10552 dividing by something we know != -1. */
10553 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10554 || (TREE_CODE (op1) == INTEGER_CST
10555 && !integer_all_onesp (op1)));
10556 common = 1;
10558 break;
10560 case BIT_AND_EXPR:
10561 case BIT_IOR_EXPR:
10562 case BIT_XOR_EXPR:
10563 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10564 shorten = -1;
10565 /* Allow vector types which are not floating point types. */
10566 else if (code0 == VECTOR_TYPE
10567 && code1 == VECTOR_TYPE
10568 && !VECTOR_FLOAT_TYPE_P (type0)
10569 && !VECTOR_FLOAT_TYPE_P (type1))
10570 common = 1;
10571 break;
10573 case TRUNC_MOD_EXPR:
10574 case FLOOR_MOD_EXPR:
10575 doing_div_or_mod = true;
10576 warn_for_div_by_zero (location, op1);
10578 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10579 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10580 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10581 common = 1;
10582 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10584 /* Although it would be tempting to shorten always here, that loses
10585 on some targets, since the modulo instruction is undefined if the
10586 quotient can't be represented in the computation mode. We shorten
10587 only if unsigned or if dividing by something we know != -1. */
10588 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10589 || (TREE_CODE (op1) == INTEGER_CST
10590 && !integer_all_onesp (op1)));
10591 common = 1;
10593 break;
10595 case TRUTH_ANDIF_EXPR:
10596 case TRUTH_ORIF_EXPR:
10597 case TRUTH_AND_EXPR:
10598 case TRUTH_OR_EXPR:
10599 case TRUTH_XOR_EXPR:
10600 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10601 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10602 || code0 == FIXED_POINT_TYPE)
10603 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10604 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10605 || code1 == FIXED_POINT_TYPE))
10607 /* Result of these operations is always an int,
10608 but that does not mean the operands should be
10609 converted to ints! */
10610 result_type = integer_type_node;
10611 if (op0_int_operands)
10613 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10614 op0 = remove_c_maybe_const_expr (op0);
10616 else
10617 op0 = c_objc_common_truthvalue_conversion (location, op0);
10618 if (op1_int_operands)
10620 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10621 op1 = remove_c_maybe_const_expr (op1);
10623 else
10624 op1 = c_objc_common_truthvalue_conversion (location, op1);
10625 converted = 1;
10626 boolean_op = true;
10628 if (code == TRUTH_ANDIF_EXPR)
10630 int_const_or_overflow = (int_operands
10631 && TREE_CODE (orig_op0) == INTEGER_CST
10632 && (op0 == truthvalue_false_node
10633 || TREE_CODE (orig_op1) == INTEGER_CST));
10634 int_const = (int_const_or_overflow
10635 && !TREE_OVERFLOW (orig_op0)
10636 && (op0 == truthvalue_false_node
10637 || !TREE_OVERFLOW (orig_op1)));
10639 else if (code == TRUTH_ORIF_EXPR)
10641 int_const_or_overflow = (int_operands
10642 && TREE_CODE (orig_op0) == INTEGER_CST
10643 && (op0 == truthvalue_true_node
10644 || TREE_CODE (orig_op1) == INTEGER_CST));
10645 int_const = (int_const_or_overflow
10646 && !TREE_OVERFLOW (orig_op0)
10647 && (op0 == truthvalue_true_node
10648 || !TREE_OVERFLOW (orig_op1)));
10650 break;
10652 /* Shift operations: result has same type as first operand;
10653 always convert second operand to int.
10654 Also set SHORT_SHIFT if shifting rightward. */
10656 case RSHIFT_EXPR:
10657 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10658 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10660 result_type = type0;
10661 converted = 1;
10663 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10664 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10665 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10666 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10668 result_type = type0;
10669 converted = 1;
10671 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10672 && code1 == INTEGER_TYPE)
10674 doing_shift = true;
10675 if (TREE_CODE (op1) == INTEGER_CST)
10677 if (tree_int_cst_sgn (op1) < 0)
10679 int_const = false;
10680 if (c_inhibit_evaluation_warnings == 0)
10681 warning_at (location, OPT_Wshift_count_negative,
10682 "right shift count is negative");
10684 else
10686 if (!integer_zerop (op1))
10687 short_shift = 1;
10689 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10691 int_const = false;
10692 if (c_inhibit_evaluation_warnings == 0)
10693 warning_at (location, OPT_Wshift_count_overflow,
10694 "right shift count >= width of type");
10699 /* Use the type of the value to be shifted. */
10700 result_type = type0;
10701 /* Avoid converting op1 to result_type later. */
10702 converted = 1;
10704 break;
10706 case LSHIFT_EXPR:
10707 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10708 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10710 result_type = type0;
10711 converted = 1;
10713 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10714 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10715 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10716 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10718 result_type = type0;
10719 converted = 1;
10721 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10722 && code1 == INTEGER_TYPE)
10724 doing_shift = true;
10725 if (TREE_CODE (op0) == INTEGER_CST
10726 && tree_int_cst_sgn (op0) < 0)
10728 /* Don't reject a left shift of a negative value in a context
10729 where a constant expression is needed in C90. */
10730 if (flag_isoc99)
10731 int_const = false;
10732 if (c_inhibit_evaluation_warnings == 0)
10733 warning_at (location, OPT_Wshift_negative_value,
10734 "left shift of negative value");
10736 if (TREE_CODE (op1) == INTEGER_CST)
10738 if (tree_int_cst_sgn (op1) < 0)
10740 int_const = false;
10741 if (c_inhibit_evaluation_warnings == 0)
10742 warning_at (location, OPT_Wshift_count_negative,
10743 "left shift count is negative");
10745 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10747 int_const = false;
10748 if (c_inhibit_evaluation_warnings == 0)
10749 warning_at (location, OPT_Wshift_count_overflow,
10750 "left shift count >= width of type");
10752 else if (TREE_CODE (op0) == INTEGER_CST
10753 && maybe_warn_shift_overflow (location, op0, op1)
10754 && flag_isoc99)
10755 int_const = false;
10758 /* Use the type of the value to be shifted. */
10759 result_type = type0;
10760 /* Avoid converting op1 to result_type later. */
10761 converted = 1;
10763 break;
10765 case EQ_EXPR:
10766 case NE_EXPR:
10767 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10769 tree intt;
10770 if (!vector_types_compatible_elements_p (type0, type1))
10772 error_at (location, "comparing vectors with different "
10773 "element types");
10774 return error_mark_node;
10777 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10779 error_at (location, "comparing vectors with different "
10780 "number of elements");
10781 return error_mark_node;
10784 /* Always construct signed integer vector type. */
10785 intt = c_common_type_for_size (GET_MODE_BITSIZE
10786 (TYPE_MODE (TREE_TYPE (type0))), 0);
10787 result_type = build_opaque_vector_type (intt,
10788 TYPE_VECTOR_SUBPARTS (type0));
10789 converted = 1;
10790 break;
10792 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10793 warning_at (location,
10794 OPT_Wfloat_equal,
10795 "comparing floating point with == or != is unsafe");
10796 /* Result of comparison is always int,
10797 but don't convert the args to int! */
10798 build_type = integer_type_node;
10799 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10800 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10801 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10802 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10803 short_compare = 1;
10804 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10806 if (TREE_CODE (op0) == ADDR_EXPR
10807 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10809 if (code == EQ_EXPR)
10810 warning_at (location,
10811 OPT_Waddress,
10812 "the comparison will always evaluate as %<false%> "
10813 "for the address of %qD will never be NULL",
10814 TREE_OPERAND (op0, 0));
10815 else
10816 warning_at (location,
10817 OPT_Waddress,
10818 "the comparison will always evaluate as %<true%> "
10819 "for the address of %qD will never be NULL",
10820 TREE_OPERAND (op0, 0));
10822 result_type = type0;
10824 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10826 if (TREE_CODE (op1) == ADDR_EXPR
10827 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10829 if (code == EQ_EXPR)
10830 warning_at (location,
10831 OPT_Waddress,
10832 "the comparison will always evaluate as %<false%> "
10833 "for the address of %qD will never be NULL",
10834 TREE_OPERAND (op1, 0));
10835 else
10836 warning_at (location,
10837 OPT_Waddress,
10838 "the comparison will always evaluate as %<true%> "
10839 "for the address of %qD will never be NULL",
10840 TREE_OPERAND (op1, 0));
10842 result_type = type1;
10844 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10846 tree tt0 = TREE_TYPE (type0);
10847 tree tt1 = TREE_TYPE (type1);
10848 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10849 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10850 addr_space_t as_common = ADDR_SPACE_GENERIC;
10852 /* Anything compares with void *. void * compares with anything.
10853 Otherwise, the targets must be compatible
10854 and both must be object or both incomplete. */
10855 if (comp_target_types (location, type0, type1))
10856 result_type = common_pointer_type (type0, type1);
10857 else if (!addr_space_superset (as0, as1, &as_common))
10859 error_at (location, "comparison of pointers to "
10860 "disjoint address spaces");
10861 return error_mark_node;
10863 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10865 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10866 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10867 "comparison of %<void *%> with function pointer");
10869 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10871 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10872 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10873 "comparison of %<void *%> with function pointer");
10875 else
10876 /* Avoid warning about the volatile ObjC EH puts on decls. */
10877 if (!objc_ok)
10878 pedwarn (location, 0,
10879 "comparison of distinct pointer types lacks a cast");
10881 if (result_type == NULL_TREE)
10883 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10884 result_type = build_pointer_type
10885 (build_qualified_type (void_type_node, qual));
10888 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10890 result_type = type0;
10891 pedwarn (location, 0, "comparison between pointer and integer");
10893 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10895 result_type = type1;
10896 pedwarn (location, 0, "comparison between pointer and integer");
10898 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10899 || truth_value_p (TREE_CODE (orig_op0)))
10900 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10901 || truth_value_p (TREE_CODE (orig_op1))))
10902 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10903 break;
10905 case LE_EXPR:
10906 case GE_EXPR:
10907 case LT_EXPR:
10908 case GT_EXPR:
10909 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10911 tree intt;
10912 if (!vector_types_compatible_elements_p (type0, type1))
10914 error_at (location, "comparing vectors with different "
10915 "element types");
10916 return error_mark_node;
10919 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10921 error_at (location, "comparing vectors with different "
10922 "number of elements");
10923 return error_mark_node;
10926 /* Always construct signed integer vector type. */
10927 intt = c_common_type_for_size (GET_MODE_BITSIZE
10928 (TYPE_MODE (TREE_TYPE (type0))), 0);
10929 result_type = build_opaque_vector_type (intt,
10930 TYPE_VECTOR_SUBPARTS (type0));
10931 converted = 1;
10932 break;
10934 build_type = integer_type_node;
10935 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10936 || code0 == FIXED_POINT_TYPE)
10937 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10938 || code1 == FIXED_POINT_TYPE))
10939 short_compare = 1;
10940 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10942 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10943 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10944 addr_space_t as_common;
10946 if (comp_target_types (location, type0, type1))
10948 result_type = common_pointer_type (type0, type1);
10949 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10950 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10951 pedwarn (location, 0,
10952 "comparison of complete and incomplete pointers");
10953 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10954 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10955 "ordered comparisons of pointers to functions");
10956 else if (null_pointer_constant_p (orig_op0)
10957 || null_pointer_constant_p (orig_op1))
10958 warning_at (location, OPT_Wextra,
10959 "ordered comparison of pointer with null pointer");
10962 else if (!addr_space_superset (as0, as1, &as_common))
10964 error_at (location, "comparison of pointers to "
10965 "disjoint address spaces");
10966 return error_mark_node;
10968 else
10970 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10971 result_type = build_pointer_type
10972 (build_qualified_type (void_type_node, qual));
10973 pedwarn (location, 0,
10974 "comparison of distinct pointer types lacks a cast");
10977 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10979 result_type = type0;
10980 if (pedantic)
10981 pedwarn (location, OPT_Wpedantic,
10982 "ordered comparison of pointer with integer zero");
10983 else if (extra_warnings)
10984 warning_at (location, OPT_Wextra,
10985 "ordered comparison of pointer with integer zero");
10987 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10989 result_type = type1;
10990 if (pedantic)
10991 pedwarn (location, OPT_Wpedantic,
10992 "ordered comparison of pointer with integer zero");
10993 else if (extra_warnings)
10994 warning_at (location, OPT_Wextra,
10995 "ordered comparison of pointer with integer zero");
10997 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10999 result_type = type0;
11000 pedwarn (location, 0, "comparison between pointer and integer");
11002 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11004 result_type = type1;
11005 pedwarn (location, 0, "comparison between pointer and integer");
11007 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11008 || truth_value_p (TREE_CODE (orig_op0)))
11009 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11010 || truth_value_p (TREE_CODE (orig_op1))))
11011 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11012 break;
11014 default:
11015 gcc_unreachable ();
11018 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11019 return error_mark_node;
11021 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11022 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11023 || !vector_types_compatible_elements_p (type0, type1)))
11025 binary_op_error (location, code, type0, type1);
11026 return error_mark_node;
11029 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11030 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11032 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11033 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11035 bool first_complex = (code0 == COMPLEX_TYPE);
11036 bool second_complex = (code1 == COMPLEX_TYPE);
11037 int none_complex = (!first_complex && !second_complex);
11039 if (shorten || common || short_compare)
11041 result_type = c_common_type (type0, type1);
11042 do_warn_double_promotion (result_type, type0, type1,
11043 "implicit conversion from %qT to %qT "
11044 "to match other operand of binary "
11045 "expression",
11046 location);
11047 if (result_type == error_mark_node)
11048 return error_mark_node;
11051 if (first_complex != second_complex
11052 && (code == PLUS_EXPR
11053 || code == MINUS_EXPR
11054 || code == MULT_EXPR
11055 || (code == TRUNC_DIV_EXPR && first_complex))
11056 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11057 && flag_signed_zeros)
11059 /* An operation on mixed real/complex operands must be
11060 handled specially, but the language-independent code can
11061 more easily optimize the plain complex arithmetic if
11062 -fno-signed-zeros. */
11063 tree real_type = TREE_TYPE (result_type);
11064 tree real, imag;
11065 if (type0 != orig_type0 || type1 != orig_type1)
11067 gcc_assert (may_need_excess_precision && common);
11068 semantic_result_type = c_common_type (orig_type0, orig_type1);
11070 if (first_complex)
11072 if (TREE_TYPE (op0) != result_type)
11073 op0 = convert_and_check (location, result_type, op0);
11074 if (TREE_TYPE (op1) != real_type)
11075 op1 = convert_and_check (location, real_type, op1);
11077 else
11079 if (TREE_TYPE (op0) != real_type)
11080 op0 = convert_and_check (location, real_type, op0);
11081 if (TREE_TYPE (op1) != result_type)
11082 op1 = convert_and_check (location, result_type, op1);
11084 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11085 return error_mark_node;
11086 if (first_complex)
11088 op0 = c_save_expr (op0);
11089 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11090 op0, 1);
11091 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11092 op0, 1);
11093 switch (code)
11095 case MULT_EXPR:
11096 case TRUNC_DIV_EXPR:
11097 op1 = c_save_expr (op1);
11098 imag = build2 (resultcode, real_type, imag, op1);
11099 /* Fall through. */
11100 case PLUS_EXPR:
11101 case MINUS_EXPR:
11102 real = build2 (resultcode, real_type, real, op1);
11103 break;
11104 default:
11105 gcc_unreachable();
11108 else
11110 op1 = c_save_expr (op1);
11111 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11112 op1, 1);
11113 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11114 op1, 1);
11115 switch (code)
11117 case MULT_EXPR:
11118 op0 = c_save_expr (op0);
11119 imag = build2 (resultcode, real_type, op0, imag);
11120 /* Fall through. */
11121 case PLUS_EXPR:
11122 real = build2 (resultcode, real_type, op0, real);
11123 break;
11124 case MINUS_EXPR:
11125 real = build2 (resultcode, real_type, op0, real);
11126 imag = build1 (NEGATE_EXPR, real_type, imag);
11127 break;
11128 default:
11129 gcc_unreachable();
11132 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11133 goto return_build_binary_op;
11136 /* For certain operations (which identify themselves by shorten != 0)
11137 if both args were extended from the same smaller type,
11138 do the arithmetic in that type and then extend.
11140 shorten !=0 and !=1 indicates a bitwise operation.
11141 For them, this optimization is safe only if
11142 both args are zero-extended or both are sign-extended.
11143 Otherwise, we might change the result.
11144 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11145 but calculated in (unsigned short) it would be (unsigned short)-1. */
11147 if (shorten && none_complex)
11149 final_type = result_type;
11150 result_type = shorten_binary_op (result_type, op0, op1,
11151 shorten == -1);
11154 /* Shifts can be shortened if shifting right. */
11156 if (short_shift)
11158 int unsigned_arg;
11159 tree arg0 = get_narrower (op0, &unsigned_arg);
11161 final_type = result_type;
11163 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11164 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11166 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11167 && tree_int_cst_sgn (op1) > 0
11168 /* We can shorten only if the shift count is less than the
11169 number of bits in the smaller type size. */
11170 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11171 /* We cannot drop an unsigned shift after sign-extension. */
11172 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11174 /* Do an unsigned shift if the operand was zero-extended. */
11175 result_type
11176 = c_common_signed_or_unsigned_type (unsigned_arg,
11177 TREE_TYPE (arg0));
11178 /* Convert value-to-be-shifted to that type. */
11179 if (TREE_TYPE (op0) != result_type)
11180 op0 = convert (result_type, op0);
11181 converted = 1;
11185 /* Comparison operations are shortened too but differently.
11186 They identify themselves by setting short_compare = 1. */
11188 if (short_compare)
11190 /* Don't write &op0, etc., because that would prevent op0
11191 from being kept in a register.
11192 Instead, make copies of the our local variables and
11193 pass the copies by reference, then copy them back afterward. */
11194 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11195 enum tree_code xresultcode = resultcode;
11196 tree val
11197 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11198 &xresultcode);
11200 if (val != 0)
11202 ret = val;
11203 goto return_build_binary_op;
11206 op0 = xop0, op1 = xop1;
11207 converted = 1;
11208 resultcode = xresultcode;
11210 if (c_inhibit_evaluation_warnings == 0)
11212 bool op0_maybe_const = true;
11213 bool op1_maybe_const = true;
11214 tree orig_op0_folded, orig_op1_folded;
11216 if (in_late_binary_op)
11218 orig_op0_folded = orig_op0;
11219 orig_op1_folded = orig_op1;
11221 else
11223 /* Fold for the sake of possible warnings, as in
11224 build_conditional_expr. This requires the
11225 "original" values to be folded, not just op0 and
11226 op1. */
11227 c_inhibit_evaluation_warnings++;
11228 op0 = c_fully_fold (op0, require_constant_value,
11229 &op0_maybe_const);
11230 op1 = c_fully_fold (op1, require_constant_value,
11231 &op1_maybe_const);
11232 c_inhibit_evaluation_warnings--;
11233 orig_op0_folded = c_fully_fold (orig_op0,
11234 require_constant_value,
11235 NULL);
11236 orig_op1_folded = c_fully_fold (orig_op1,
11237 require_constant_value,
11238 NULL);
11241 if (warn_sign_compare)
11242 warn_for_sign_compare (location, orig_op0_folded,
11243 orig_op1_folded, op0, op1,
11244 result_type, resultcode);
11245 if (!in_late_binary_op && !int_operands)
11247 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11248 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11249 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11250 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11256 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11257 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11258 Then the expression will be built.
11259 It will be given type FINAL_TYPE if that is nonzero;
11260 otherwise, it will be given type RESULT_TYPE. */
11262 if (!result_type)
11264 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11265 return error_mark_node;
11268 if (build_type == NULL_TREE)
11270 build_type = result_type;
11271 if ((type0 != orig_type0 || type1 != orig_type1)
11272 && !boolean_op)
11274 gcc_assert (may_need_excess_precision && common);
11275 semantic_result_type = c_common_type (orig_type0, orig_type1);
11279 if (!converted)
11281 op0 = ep_convert_and_check (location, result_type, op0,
11282 semantic_result_type);
11283 op1 = ep_convert_and_check (location, result_type, op1,
11284 semantic_result_type);
11286 /* This can happen if one operand has a vector type, and the other
11287 has a different type. */
11288 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11289 return error_mark_node;
11292 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11293 | SANITIZE_FLOAT_DIVIDE))
11294 && do_ubsan_in_current_function ()
11295 && (doing_div_or_mod || doing_shift))
11297 /* OP0 and/or OP1 might have side-effects. */
11298 op0 = c_save_expr (op0);
11299 op1 = c_save_expr (op1);
11300 op0 = c_fully_fold (op0, false, NULL);
11301 op1 = c_fully_fold (op1, false, NULL);
11302 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11303 | SANITIZE_FLOAT_DIVIDE)))
11304 instrument_expr = ubsan_instrument_division (location, op0, op1);
11305 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11306 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11309 /* Treat expressions in initializers specially as they can't trap. */
11310 if (int_const_or_overflow)
11311 ret = (require_constant_value
11312 ? fold_build2_initializer_loc (location, resultcode, build_type,
11313 op0, op1)
11314 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11315 else
11316 ret = build2 (resultcode, build_type, op0, op1);
11317 if (final_type != 0)
11318 ret = convert (final_type, ret);
11320 return_build_binary_op:
11321 gcc_assert (ret != error_mark_node);
11322 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11323 ret = (int_operands
11324 ? note_integer_operands (ret)
11325 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11326 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11327 && !in_late_binary_op)
11328 ret = note_integer_operands (ret);
11329 if (semantic_result_type)
11330 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11331 protected_set_expr_location (ret, location);
11333 if (instrument_expr != NULL)
11334 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11335 instrument_expr, ret);
11337 return ret;
11341 /* Convert EXPR to be a truth-value, validating its type for this
11342 purpose. LOCATION is the source location for the expression. */
11344 tree
11345 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11347 bool int_const, int_operands;
11349 switch (TREE_CODE (TREE_TYPE (expr)))
11351 case ARRAY_TYPE:
11352 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11353 return error_mark_node;
11355 case RECORD_TYPE:
11356 error_at (location, "used struct type value where scalar is required");
11357 return error_mark_node;
11359 case UNION_TYPE:
11360 error_at (location, "used union type value where scalar is required");
11361 return error_mark_node;
11363 case VOID_TYPE:
11364 error_at (location, "void value not ignored as it ought to be");
11365 return error_mark_node;
11367 case POINTER_TYPE:
11368 if (reject_gcc_builtin (expr))
11369 return error_mark_node;
11370 break;
11372 case FUNCTION_TYPE:
11373 gcc_unreachable ();
11375 case VECTOR_TYPE:
11376 error_at (location, "used vector type where scalar is required");
11377 return error_mark_node;
11379 default:
11380 break;
11383 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11384 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11385 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11387 expr = remove_c_maybe_const_expr (expr);
11388 expr = build2 (NE_EXPR, integer_type_node, expr,
11389 convert (TREE_TYPE (expr), integer_zero_node));
11390 expr = note_integer_operands (expr);
11392 else
11393 /* ??? Should we also give an error for vectors rather than leaving
11394 those to give errors later? */
11395 expr = c_common_truthvalue_conversion (location, expr);
11397 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11399 if (TREE_OVERFLOW (expr))
11400 return expr;
11401 else
11402 return note_integer_operands (expr);
11404 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11405 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11406 return expr;
11410 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11411 required. */
11413 tree
11414 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11416 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11418 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11419 /* Executing a compound literal inside a function reinitializes
11420 it. */
11421 if (!TREE_STATIC (decl))
11422 *se = true;
11423 return decl;
11425 else
11426 return expr;
11429 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11430 statement. LOC is the location of the OACC_PARALLEL. */
11432 tree
11433 c_finish_oacc_parallel (location_t loc, tree clauses, tree block)
11435 tree stmt;
11437 block = c_end_compound_stmt (loc, block, true);
11439 stmt = make_node (OACC_PARALLEL);
11440 TREE_TYPE (stmt) = void_type_node;
11441 OACC_PARALLEL_CLAUSES (stmt) = clauses;
11442 OACC_PARALLEL_BODY (stmt) = block;
11443 SET_EXPR_LOCATION (stmt, loc);
11445 return add_stmt (stmt);
11448 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11449 statement. LOC is the location of the OACC_KERNELS. */
11451 tree
11452 c_finish_oacc_kernels (location_t loc, tree clauses, tree block)
11454 tree stmt;
11456 block = c_end_compound_stmt (loc, block, true);
11458 stmt = make_node (OACC_KERNELS);
11459 TREE_TYPE (stmt) = void_type_node;
11460 OACC_KERNELS_CLAUSES (stmt) = clauses;
11461 OACC_KERNELS_BODY (stmt) = block;
11462 SET_EXPR_LOCATION (stmt, loc);
11464 return add_stmt (stmt);
11467 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11468 statement. LOC is the location of the OACC_DATA. */
11470 tree
11471 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11473 tree stmt;
11475 block = c_end_compound_stmt (loc, block, true);
11477 stmt = make_node (OACC_DATA);
11478 TREE_TYPE (stmt) = void_type_node;
11479 OACC_DATA_CLAUSES (stmt) = clauses;
11480 OACC_DATA_BODY (stmt) = block;
11481 SET_EXPR_LOCATION (stmt, loc);
11483 return add_stmt (stmt);
11486 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11488 tree
11489 c_begin_omp_parallel (void)
11491 tree block;
11493 keep_next_level ();
11494 block = c_begin_compound_stmt (true);
11496 return block;
11499 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11500 statement. LOC is the location of the OMP_PARALLEL. */
11502 tree
11503 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11505 tree stmt;
11507 block = c_end_compound_stmt (loc, block, true);
11509 stmt = make_node (OMP_PARALLEL);
11510 TREE_TYPE (stmt) = void_type_node;
11511 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11512 OMP_PARALLEL_BODY (stmt) = block;
11513 SET_EXPR_LOCATION (stmt, loc);
11515 return add_stmt (stmt);
11518 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11520 tree
11521 c_begin_omp_task (void)
11523 tree block;
11525 keep_next_level ();
11526 block = c_begin_compound_stmt (true);
11528 return block;
11531 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11532 statement. LOC is the location of the #pragma. */
11534 tree
11535 c_finish_omp_task (location_t loc, tree clauses, tree block)
11537 tree stmt;
11539 block = c_end_compound_stmt (loc, block, true);
11541 stmt = make_node (OMP_TASK);
11542 TREE_TYPE (stmt) = void_type_node;
11543 OMP_TASK_CLAUSES (stmt) = clauses;
11544 OMP_TASK_BODY (stmt) = block;
11545 SET_EXPR_LOCATION (stmt, loc);
11547 return add_stmt (stmt);
11550 /* Generate GOMP_cancel call for #pragma omp cancel. */
11552 void
11553 c_finish_omp_cancel (location_t loc, tree clauses)
11555 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11556 int mask = 0;
11557 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11558 mask = 1;
11559 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11560 mask = 2;
11561 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11562 mask = 4;
11563 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11564 mask = 8;
11565 else
11567 error_at (loc, "%<#pragma omp cancel must specify one of "
11568 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11569 "clauses");
11570 return;
11572 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11573 if (ifc != NULL_TREE)
11575 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11576 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11577 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11578 build_zero_cst (type));
11580 else
11581 ifc = boolean_true_node;
11582 tree stmt = build_call_expr_loc (loc, fn, 2,
11583 build_int_cst (integer_type_node, mask),
11584 ifc);
11585 add_stmt (stmt);
11588 /* Generate GOMP_cancellation_point call for
11589 #pragma omp cancellation point. */
11591 void
11592 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11594 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11595 int mask = 0;
11596 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11597 mask = 1;
11598 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11599 mask = 2;
11600 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11601 mask = 4;
11602 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11603 mask = 8;
11604 else
11606 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11607 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11608 "clauses");
11609 return;
11611 tree stmt = build_call_expr_loc (loc, fn, 1,
11612 build_int_cst (integer_type_node, mask));
11613 add_stmt (stmt);
11616 /* Helper function for handle_omp_array_sections. Called recursively
11617 to handle multiple array-section-subscripts. C is the clause,
11618 T current expression (initially OMP_CLAUSE_DECL), which is either
11619 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11620 expression if specified, TREE_VALUE length expression if specified,
11621 TREE_CHAIN is what it has been specified after, or some decl.
11622 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11623 set to true if any of the array-section-subscript could have length
11624 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11625 first array-section-subscript which is known not to have length
11626 of one. Given say:
11627 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11628 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11629 all are or may have length of 1, array-section-subscript [:2] is the
11630 first one knonwn not to have length 1. For array-section-subscript
11631 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11632 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11633 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11634 case though, as some lengths could be zero. */
11636 static tree
11637 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11638 bool &maybe_zero_len, unsigned int &first_non_one)
11640 tree ret, low_bound, length, type;
11641 if (TREE_CODE (t) != TREE_LIST)
11643 if (error_operand_p (t))
11644 return error_mark_node;
11645 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
11647 if (DECL_P (t))
11648 error_at (OMP_CLAUSE_LOCATION (c),
11649 "%qD is not a variable in %qs clause", t,
11650 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11651 else
11652 error_at (OMP_CLAUSE_LOCATION (c),
11653 "%qE is not a variable in %qs clause", t,
11654 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11655 return error_mark_node;
11657 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11658 && VAR_P (t) && DECL_THREAD_LOCAL_P (t))
11660 error_at (OMP_CLAUSE_LOCATION (c),
11661 "%qD is threadprivate variable in %qs clause", t,
11662 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11663 return error_mark_node;
11665 return t;
11668 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11669 maybe_zero_len, first_non_one);
11670 if (ret == error_mark_node || ret == NULL_TREE)
11671 return ret;
11673 type = TREE_TYPE (ret);
11674 low_bound = TREE_PURPOSE (t);
11675 length = TREE_VALUE (t);
11677 if (low_bound == error_mark_node || length == error_mark_node)
11678 return error_mark_node;
11680 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11682 error_at (OMP_CLAUSE_LOCATION (c),
11683 "low bound %qE of array section does not have integral type",
11684 low_bound);
11685 return error_mark_node;
11687 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11689 error_at (OMP_CLAUSE_LOCATION (c),
11690 "length %qE of array section does not have integral type",
11691 length);
11692 return error_mark_node;
11694 if (low_bound
11695 && TREE_CODE (low_bound) == INTEGER_CST
11696 && TYPE_PRECISION (TREE_TYPE (low_bound))
11697 > TYPE_PRECISION (sizetype))
11698 low_bound = fold_convert (sizetype, low_bound);
11699 if (length
11700 && TREE_CODE (length) == INTEGER_CST
11701 && TYPE_PRECISION (TREE_TYPE (length))
11702 > TYPE_PRECISION (sizetype))
11703 length = fold_convert (sizetype, length);
11704 if (low_bound == NULL_TREE)
11705 low_bound = integer_zero_node;
11707 if (length != NULL_TREE)
11709 if (!integer_nonzerop (length))
11710 maybe_zero_len = true;
11711 if (first_non_one == types.length ()
11712 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11713 first_non_one++;
11715 if (TREE_CODE (type) == ARRAY_TYPE)
11717 if (length == NULL_TREE
11718 && (TYPE_DOMAIN (type) == NULL_TREE
11719 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11721 error_at (OMP_CLAUSE_LOCATION (c),
11722 "for unknown bound array type length expression must "
11723 "be specified");
11724 return error_mark_node;
11726 if (TREE_CODE (low_bound) == INTEGER_CST
11727 && tree_int_cst_sgn (low_bound) == -1)
11729 error_at (OMP_CLAUSE_LOCATION (c),
11730 "negative low bound in array section in %qs clause",
11731 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11732 return error_mark_node;
11734 if (length != NULL_TREE
11735 && TREE_CODE (length) == INTEGER_CST
11736 && tree_int_cst_sgn (length) == -1)
11738 error_at (OMP_CLAUSE_LOCATION (c),
11739 "negative length in array section in %qs clause",
11740 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11741 return error_mark_node;
11743 if (TYPE_DOMAIN (type)
11744 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11745 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11746 == INTEGER_CST)
11748 tree size = size_binop (PLUS_EXPR,
11749 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11750 size_one_node);
11751 if (TREE_CODE (low_bound) == INTEGER_CST)
11753 if (tree_int_cst_lt (size, low_bound))
11755 error_at (OMP_CLAUSE_LOCATION (c),
11756 "low bound %qE above array section size "
11757 "in %qs clause", low_bound,
11758 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11759 return error_mark_node;
11761 if (tree_int_cst_equal (size, low_bound))
11762 maybe_zero_len = true;
11763 else if (length == NULL_TREE
11764 && first_non_one == types.length ()
11765 && tree_int_cst_equal
11766 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11767 low_bound))
11768 first_non_one++;
11770 else if (length == NULL_TREE)
11772 maybe_zero_len = true;
11773 if (first_non_one == types.length ())
11774 first_non_one++;
11776 if (length && TREE_CODE (length) == INTEGER_CST)
11778 if (tree_int_cst_lt (size, length))
11780 error_at (OMP_CLAUSE_LOCATION (c),
11781 "length %qE above array section size "
11782 "in %qs clause", length,
11783 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11784 return error_mark_node;
11786 if (TREE_CODE (low_bound) == INTEGER_CST)
11788 tree lbpluslen
11789 = size_binop (PLUS_EXPR,
11790 fold_convert (sizetype, low_bound),
11791 fold_convert (sizetype, length));
11792 if (TREE_CODE (lbpluslen) == INTEGER_CST
11793 && tree_int_cst_lt (size, lbpluslen))
11795 error_at (OMP_CLAUSE_LOCATION (c),
11796 "high bound %qE above array section size "
11797 "in %qs clause", lbpluslen,
11798 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11799 return error_mark_node;
11804 else if (length == NULL_TREE)
11806 maybe_zero_len = true;
11807 if (first_non_one == types.length ())
11808 first_non_one++;
11811 /* For [lb:] we will need to evaluate lb more than once. */
11812 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11814 tree lb = c_save_expr (low_bound);
11815 if (lb != low_bound)
11817 TREE_PURPOSE (t) = lb;
11818 low_bound = lb;
11822 else if (TREE_CODE (type) == POINTER_TYPE)
11824 if (length == NULL_TREE)
11826 error_at (OMP_CLAUSE_LOCATION (c),
11827 "for pointer type length expression must be specified");
11828 return error_mark_node;
11830 /* If there is a pointer type anywhere but in the very first
11831 array-section-subscript, the array section can't be contiguous. */
11832 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11833 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11835 error_at (OMP_CLAUSE_LOCATION (c),
11836 "array section is not contiguous in %qs clause",
11837 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11838 return error_mark_node;
11841 else
11843 error_at (OMP_CLAUSE_LOCATION (c),
11844 "%qE does not have pointer or array type", ret);
11845 return error_mark_node;
11847 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11848 types.safe_push (TREE_TYPE (ret));
11849 /* We will need to evaluate lb more than once. */
11850 tree lb = c_save_expr (low_bound);
11851 if (lb != low_bound)
11853 TREE_PURPOSE (t) = lb;
11854 low_bound = lb;
11856 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11857 return ret;
11860 /* Handle array sections for clause C. */
11862 static bool
11863 handle_omp_array_sections (tree c)
11865 bool maybe_zero_len = false;
11866 unsigned int first_non_one = 0;
11867 vec<tree> types = vNULL;
11868 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11869 maybe_zero_len, first_non_one);
11870 if (first == error_mark_node)
11872 types.release ();
11873 return true;
11875 if (first == NULL_TREE)
11877 types.release ();
11878 return false;
11880 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11882 tree t = OMP_CLAUSE_DECL (c);
11883 tree tem = NULL_TREE;
11884 types.release ();
11885 /* Need to evaluate side effects in the length expressions
11886 if any. */
11887 while (TREE_CODE (t) == TREE_LIST)
11889 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11891 if (tem == NULL_TREE)
11892 tem = TREE_VALUE (t);
11893 else
11894 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11895 TREE_VALUE (t), tem);
11897 t = TREE_CHAIN (t);
11899 if (tem)
11900 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11901 first = c_fully_fold (first, false, NULL);
11902 OMP_CLAUSE_DECL (c) = first;
11904 else
11906 unsigned int num = types.length (), i;
11907 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11908 tree condition = NULL_TREE;
11910 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11911 maybe_zero_len = true;
11913 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11914 t = TREE_CHAIN (t))
11916 tree low_bound = TREE_PURPOSE (t);
11917 tree length = TREE_VALUE (t);
11919 i--;
11920 if (low_bound
11921 && TREE_CODE (low_bound) == INTEGER_CST
11922 && TYPE_PRECISION (TREE_TYPE (low_bound))
11923 > TYPE_PRECISION (sizetype))
11924 low_bound = fold_convert (sizetype, low_bound);
11925 if (length
11926 && TREE_CODE (length) == INTEGER_CST
11927 && TYPE_PRECISION (TREE_TYPE (length))
11928 > TYPE_PRECISION (sizetype))
11929 length = fold_convert (sizetype, length);
11930 if (low_bound == NULL_TREE)
11931 low_bound = integer_zero_node;
11932 if (!maybe_zero_len && i > first_non_one)
11934 if (integer_nonzerop (low_bound))
11935 goto do_warn_noncontiguous;
11936 if (length != NULL_TREE
11937 && TREE_CODE (length) == INTEGER_CST
11938 && TYPE_DOMAIN (types[i])
11939 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11940 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11941 == INTEGER_CST)
11943 tree size;
11944 size = size_binop (PLUS_EXPR,
11945 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11946 size_one_node);
11947 if (!tree_int_cst_equal (length, size))
11949 do_warn_noncontiguous:
11950 error_at (OMP_CLAUSE_LOCATION (c),
11951 "array section is not contiguous in %qs "
11952 "clause",
11953 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11954 types.release ();
11955 return true;
11958 if (length != NULL_TREE
11959 && TREE_SIDE_EFFECTS (length))
11961 if (side_effects == NULL_TREE)
11962 side_effects = length;
11963 else
11964 side_effects = build2 (COMPOUND_EXPR,
11965 TREE_TYPE (side_effects),
11966 length, side_effects);
11969 else
11971 tree l;
11973 if (i > first_non_one && length && integer_nonzerop (length))
11974 continue;
11975 if (length)
11976 l = fold_convert (sizetype, length);
11977 else
11979 l = size_binop (PLUS_EXPR,
11980 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11981 size_one_node);
11982 l = size_binop (MINUS_EXPR, l,
11983 fold_convert (sizetype, low_bound));
11985 if (i > first_non_one)
11987 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11988 size_zero_node);
11989 if (condition == NULL_TREE)
11990 condition = l;
11991 else
11992 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11993 l, condition);
11995 else if (size == NULL_TREE)
11997 size = size_in_bytes (TREE_TYPE (types[i]));
11998 size = size_binop (MULT_EXPR, size, l);
11999 if (condition)
12000 size = fold_build3 (COND_EXPR, sizetype, condition,
12001 size, size_zero_node);
12003 else
12004 size = size_binop (MULT_EXPR, size, l);
12007 types.release ();
12008 if (side_effects)
12009 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12010 first = c_fully_fold (first, false, NULL);
12011 OMP_CLAUSE_DECL (c) = first;
12012 if (size)
12013 size = c_fully_fold (size, false, NULL);
12014 OMP_CLAUSE_SIZE (c) = size;
12015 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12016 return false;
12017 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12018 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12019 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12020 if (!c_mark_addressable (t))
12021 return false;
12022 OMP_CLAUSE_DECL (c2) = t;
12023 t = build_fold_addr_expr (first);
12024 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12025 tree ptr = OMP_CLAUSE_DECL (c2);
12026 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12027 ptr = build_fold_addr_expr (ptr);
12028 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12029 ptrdiff_type_node, t,
12030 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12031 ptrdiff_type_node, ptr));
12032 t = c_fully_fold (t, false, NULL);
12033 OMP_CLAUSE_SIZE (c2) = t;
12034 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12035 OMP_CLAUSE_CHAIN (c) = c2;
12037 return false;
12040 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12041 an inline call. But, remap
12042 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12043 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12045 static tree
12046 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12047 tree decl, tree placeholder)
12049 copy_body_data id;
12050 hash_map<tree, tree> decl_map;
12052 decl_map.put (omp_decl1, placeholder);
12053 decl_map.put (omp_decl2, decl);
12054 memset (&id, 0, sizeof (id));
12055 id.src_fn = DECL_CONTEXT (omp_decl1);
12056 id.dst_fn = current_function_decl;
12057 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12058 id.decl_map = &decl_map;
12060 id.copy_decl = copy_decl_no_change;
12061 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12062 id.transform_new_cfg = true;
12063 id.transform_return_to_modify = false;
12064 id.transform_lang_insert_block = NULL;
12065 id.eh_lp_nr = 0;
12066 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12067 return stmt;
12070 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12071 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12073 static tree
12074 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12076 if (*tp == (tree) data)
12077 return *tp;
12078 return NULL_TREE;
12081 /* For all elements of CLAUSES, validate them against their constraints.
12082 Remove any elements from the list that are invalid. */
12084 tree
12085 c_finish_omp_clauses (tree clauses)
12087 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12088 bitmap_head aligned_head;
12089 tree c, t, *pc;
12090 bool branch_seen = false;
12091 bool copyprivate_seen = false;
12092 tree *nowait_clause = NULL;
12094 bitmap_obstack_initialize (NULL);
12095 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12096 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12097 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12098 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12100 for (pc = &clauses, c = clauses; c ; c = *pc)
12102 bool remove = false;
12103 bool need_complete = false;
12104 bool need_implicitly_determined = false;
12106 switch (OMP_CLAUSE_CODE (c))
12108 case OMP_CLAUSE_SHARED:
12109 need_implicitly_determined = true;
12110 goto check_dup_generic;
12112 case OMP_CLAUSE_PRIVATE:
12113 need_complete = true;
12114 need_implicitly_determined = true;
12115 goto check_dup_generic;
12117 case OMP_CLAUSE_REDUCTION:
12118 need_implicitly_determined = true;
12119 t = OMP_CLAUSE_DECL (c);
12120 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12121 && (FLOAT_TYPE_P (TREE_TYPE (t))
12122 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
12124 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12125 const char *r_name = NULL;
12127 switch (r_code)
12129 case PLUS_EXPR:
12130 case MULT_EXPR:
12131 case MINUS_EXPR:
12132 break;
12133 case MIN_EXPR:
12134 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12135 r_name = "min";
12136 break;
12137 case MAX_EXPR:
12138 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12139 r_name = "max";
12140 break;
12141 case BIT_AND_EXPR:
12142 r_name = "&";
12143 break;
12144 case BIT_XOR_EXPR:
12145 r_name = "^";
12146 break;
12147 case BIT_IOR_EXPR:
12148 r_name = "|";
12149 break;
12150 case TRUTH_ANDIF_EXPR:
12151 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12152 r_name = "&&";
12153 break;
12154 case TRUTH_ORIF_EXPR:
12155 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12156 r_name = "||";
12157 break;
12158 default:
12159 gcc_unreachable ();
12161 if (r_name)
12163 error_at (OMP_CLAUSE_LOCATION (c),
12164 "%qE has invalid type for %<reduction(%s)%>",
12165 t, r_name);
12166 remove = true;
12167 break;
12170 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12172 error_at (OMP_CLAUSE_LOCATION (c),
12173 "user defined reduction not found for %qD", t);
12174 remove = true;
12175 break;
12177 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12179 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12180 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12181 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12182 VAR_DECL, NULL_TREE, type);
12183 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12184 DECL_ARTIFICIAL (placeholder) = 1;
12185 DECL_IGNORED_P (placeholder) = 1;
12186 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12187 c_mark_addressable (placeholder);
12188 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12189 c_mark_addressable (OMP_CLAUSE_DECL (c));
12190 OMP_CLAUSE_REDUCTION_MERGE (c)
12191 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12192 TREE_VEC_ELT (list, 0),
12193 TREE_VEC_ELT (list, 1),
12194 OMP_CLAUSE_DECL (c), placeholder);
12195 OMP_CLAUSE_REDUCTION_MERGE (c)
12196 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12197 void_type_node, NULL_TREE,
12198 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12199 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12200 if (TREE_VEC_LENGTH (list) == 6)
12202 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12203 c_mark_addressable (OMP_CLAUSE_DECL (c));
12204 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12205 c_mark_addressable (placeholder);
12206 tree init = TREE_VEC_ELT (list, 5);
12207 if (init == error_mark_node)
12208 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12209 OMP_CLAUSE_REDUCTION_INIT (c)
12210 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12211 TREE_VEC_ELT (list, 3),
12212 OMP_CLAUSE_DECL (c), placeholder);
12213 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12214 OMP_CLAUSE_REDUCTION_INIT (c)
12215 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12216 OMP_CLAUSE_REDUCTION_INIT (c));
12217 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12218 c_find_omp_placeholder_r,
12219 placeholder, NULL))
12220 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12222 else
12224 tree init;
12225 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12226 init = build_constructor (TREE_TYPE (t), NULL);
12227 else
12228 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12229 OMP_CLAUSE_REDUCTION_INIT (c)
12230 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12232 OMP_CLAUSE_REDUCTION_INIT (c)
12233 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12234 void_type_node, NULL_TREE,
12235 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12236 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12238 goto check_dup_generic;
12240 case OMP_CLAUSE_COPYPRIVATE:
12241 copyprivate_seen = true;
12242 if (nowait_clause)
12244 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12245 "%<nowait%> clause must not be used together "
12246 "with %<copyprivate%>");
12247 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12248 nowait_clause = NULL;
12250 goto check_dup_generic;
12252 case OMP_CLAUSE_COPYIN:
12253 t = OMP_CLAUSE_DECL (c);
12254 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
12256 error_at (OMP_CLAUSE_LOCATION (c),
12257 "%qE must be %<threadprivate%> for %<copyin%>", t);
12258 remove = true;
12259 break;
12261 goto check_dup_generic;
12263 case OMP_CLAUSE_LINEAR:
12264 t = OMP_CLAUSE_DECL (c);
12265 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12266 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12268 error_at (OMP_CLAUSE_LOCATION (c),
12269 "linear clause applied to non-integral non-pointer "
12270 "variable with type %qT", TREE_TYPE (t));
12271 remove = true;
12272 break;
12274 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12276 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12277 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12278 OMP_CLAUSE_DECL (c), s);
12279 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12280 sizetype, s, OMP_CLAUSE_DECL (c));
12281 if (s == error_mark_node)
12282 s = size_one_node;
12283 OMP_CLAUSE_LINEAR_STEP (c) = s;
12285 else
12286 OMP_CLAUSE_LINEAR_STEP (c)
12287 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12288 goto check_dup_generic;
12290 check_dup_generic:
12291 t = OMP_CLAUSE_DECL (c);
12292 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12294 error_at (OMP_CLAUSE_LOCATION (c),
12295 "%qE is not a variable in clause %qs", t,
12296 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12297 remove = true;
12299 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12300 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12301 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12303 error_at (OMP_CLAUSE_LOCATION (c),
12304 "%qE appears more than once in data clauses", t);
12305 remove = true;
12307 else
12308 bitmap_set_bit (&generic_head, DECL_UID (t));
12309 break;
12311 case OMP_CLAUSE_FIRSTPRIVATE:
12312 t = OMP_CLAUSE_DECL (c);
12313 need_complete = true;
12314 need_implicitly_determined = true;
12315 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12317 error_at (OMP_CLAUSE_LOCATION (c),
12318 "%qE is not a variable in clause %<firstprivate%>", t);
12319 remove = true;
12321 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12322 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12324 error_at (OMP_CLAUSE_LOCATION (c),
12325 "%qE appears more than once in data clauses", t);
12326 remove = true;
12328 else
12329 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12330 break;
12332 case OMP_CLAUSE_LASTPRIVATE:
12333 t = OMP_CLAUSE_DECL (c);
12334 need_complete = true;
12335 need_implicitly_determined = true;
12336 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12338 error_at (OMP_CLAUSE_LOCATION (c),
12339 "%qE is not a variable in clause %<lastprivate%>", t);
12340 remove = true;
12342 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12343 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12345 error_at (OMP_CLAUSE_LOCATION (c),
12346 "%qE appears more than once in data clauses", t);
12347 remove = true;
12349 else
12350 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12351 break;
12353 case OMP_CLAUSE_ALIGNED:
12354 t = OMP_CLAUSE_DECL (c);
12355 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12357 error_at (OMP_CLAUSE_LOCATION (c),
12358 "%qE is not a variable in %<aligned%> clause", t);
12359 remove = true;
12361 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12362 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12364 error_at (OMP_CLAUSE_LOCATION (c),
12365 "%qE in %<aligned%> clause is neither a pointer nor "
12366 "an array", t);
12367 remove = true;
12369 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12371 error_at (OMP_CLAUSE_LOCATION (c),
12372 "%qE appears more than once in %<aligned%> clauses",
12374 remove = true;
12376 else
12377 bitmap_set_bit (&aligned_head, DECL_UID (t));
12378 break;
12380 case OMP_CLAUSE_DEPEND:
12381 t = OMP_CLAUSE_DECL (c);
12382 if (TREE_CODE (t) == TREE_LIST)
12384 if (handle_omp_array_sections (c))
12385 remove = true;
12386 break;
12388 if (t == error_mark_node)
12389 remove = true;
12390 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12392 error_at (OMP_CLAUSE_LOCATION (c),
12393 "%qE is not a variable in %<depend%> clause", t);
12394 remove = true;
12396 else if (!c_mark_addressable (t))
12397 remove = true;
12398 break;
12400 case OMP_CLAUSE_MAP:
12401 case OMP_CLAUSE_TO:
12402 case OMP_CLAUSE_FROM:
12403 case OMP_CLAUSE__CACHE_:
12404 t = OMP_CLAUSE_DECL (c);
12405 if (TREE_CODE (t) == TREE_LIST)
12407 if (handle_omp_array_sections (c))
12408 remove = true;
12409 else
12411 t = OMP_CLAUSE_DECL (c);
12412 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12414 error_at (OMP_CLAUSE_LOCATION (c),
12415 "array section does not have mappable type "
12416 "in %qs clause",
12417 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12418 remove = true;
12421 break;
12423 if (t == error_mark_node)
12424 remove = true;
12425 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12427 error_at (OMP_CLAUSE_LOCATION (c),
12428 "%qE is not a variable in %qs clause", t,
12429 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12430 remove = true;
12432 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
12434 error_at (OMP_CLAUSE_LOCATION (c),
12435 "%qD is threadprivate variable in %qs clause", t,
12436 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12437 remove = true;
12439 else if (!c_mark_addressable (t))
12440 remove = true;
12441 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12442 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12443 || (OMP_CLAUSE_MAP_KIND (c)
12444 == GOMP_MAP_FORCE_DEVICEPTR)))
12445 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12447 error_at (OMP_CLAUSE_LOCATION (c),
12448 "%qD does not have a mappable type in %qs clause", t,
12449 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12450 remove = true;
12452 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12454 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12455 error ("%qD appears more than once in motion clauses", t);
12456 else
12457 error ("%qD appears more than once in map clauses", t);
12458 remove = true;
12460 else
12461 bitmap_set_bit (&generic_head, DECL_UID (t));
12462 break;
12464 case OMP_CLAUSE_UNIFORM:
12465 t = OMP_CLAUSE_DECL (c);
12466 if (TREE_CODE (t) != PARM_DECL)
12468 if (DECL_P (t))
12469 error_at (OMP_CLAUSE_LOCATION (c),
12470 "%qD is not an argument in %<uniform%> clause", t);
12471 else
12472 error_at (OMP_CLAUSE_LOCATION (c),
12473 "%qE is not an argument in %<uniform%> clause", t);
12474 remove = true;
12475 break;
12477 goto check_dup_generic;
12479 case OMP_CLAUSE_NOWAIT:
12480 if (copyprivate_seen)
12482 error_at (OMP_CLAUSE_LOCATION (c),
12483 "%<nowait%> clause must not be used together "
12484 "with %<copyprivate%>");
12485 remove = true;
12486 break;
12488 nowait_clause = pc;
12489 pc = &OMP_CLAUSE_CHAIN (c);
12490 continue;
12492 case OMP_CLAUSE_IF:
12493 case OMP_CLAUSE_NUM_THREADS:
12494 case OMP_CLAUSE_NUM_TEAMS:
12495 case OMP_CLAUSE_THREAD_LIMIT:
12496 case OMP_CLAUSE_SCHEDULE:
12497 case OMP_CLAUSE_ORDERED:
12498 case OMP_CLAUSE_DEFAULT:
12499 case OMP_CLAUSE_UNTIED:
12500 case OMP_CLAUSE_COLLAPSE:
12501 case OMP_CLAUSE_FINAL:
12502 case OMP_CLAUSE_MERGEABLE:
12503 case OMP_CLAUSE_SAFELEN:
12504 case OMP_CLAUSE_SIMDLEN:
12505 case OMP_CLAUSE_DEVICE:
12506 case OMP_CLAUSE_DIST_SCHEDULE:
12507 case OMP_CLAUSE_PARALLEL:
12508 case OMP_CLAUSE_FOR:
12509 case OMP_CLAUSE_SECTIONS:
12510 case OMP_CLAUSE_TASKGROUP:
12511 case OMP_CLAUSE_PROC_BIND:
12512 case OMP_CLAUSE__CILK_FOR_COUNT_:
12513 case OMP_CLAUSE_NUM_GANGS:
12514 case OMP_CLAUSE_NUM_WORKERS:
12515 case OMP_CLAUSE_VECTOR_LENGTH:
12516 case OMP_CLAUSE_ASYNC:
12517 case OMP_CLAUSE_WAIT:
12518 case OMP_CLAUSE_AUTO:
12519 case OMP_CLAUSE_SEQ:
12520 case OMP_CLAUSE_GANG:
12521 case OMP_CLAUSE_WORKER:
12522 case OMP_CLAUSE_VECTOR:
12523 pc = &OMP_CLAUSE_CHAIN (c);
12524 continue;
12526 case OMP_CLAUSE_INBRANCH:
12527 case OMP_CLAUSE_NOTINBRANCH:
12528 if (branch_seen)
12530 error_at (OMP_CLAUSE_LOCATION (c),
12531 "%<inbranch%> clause is incompatible with "
12532 "%<notinbranch%>");
12533 remove = true;
12534 break;
12536 branch_seen = true;
12537 pc = &OMP_CLAUSE_CHAIN (c);
12538 continue;
12540 default:
12541 gcc_unreachable ();
12544 if (!remove)
12546 t = OMP_CLAUSE_DECL (c);
12548 if (need_complete)
12550 t = require_complete_type (t);
12551 if (t == error_mark_node)
12552 remove = true;
12555 if (need_implicitly_determined)
12557 const char *share_name = NULL;
12559 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
12560 share_name = "threadprivate";
12561 else switch (c_omp_predetermined_sharing (t))
12563 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12564 break;
12565 case OMP_CLAUSE_DEFAULT_SHARED:
12566 /* const vars may be specified in firstprivate clause. */
12567 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12568 && TREE_READONLY (t))
12569 break;
12570 share_name = "shared";
12571 break;
12572 case OMP_CLAUSE_DEFAULT_PRIVATE:
12573 share_name = "private";
12574 break;
12575 default:
12576 gcc_unreachable ();
12578 if (share_name)
12580 error_at (OMP_CLAUSE_LOCATION (c),
12581 "%qE is predetermined %qs for %qs",
12582 t, share_name,
12583 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12584 remove = true;
12589 if (remove)
12590 *pc = OMP_CLAUSE_CHAIN (c);
12591 else
12592 pc = &OMP_CLAUSE_CHAIN (c);
12595 bitmap_obstack_release (NULL);
12596 return clauses;
12599 /* Create a transaction node. */
12601 tree
12602 c_finish_transaction (location_t loc, tree block, int flags)
12604 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12605 if (flags & TM_STMT_ATTR_OUTER)
12606 TRANSACTION_EXPR_OUTER (stmt) = 1;
12607 if (flags & TM_STMT_ATTR_RELAXED)
12608 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12609 return add_stmt (stmt);
12612 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12613 down to the element type of an array. */
12615 tree
12616 c_build_qualified_type (tree type, int type_quals)
12618 if (type == error_mark_node)
12619 return type;
12621 if (TREE_CODE (type) == ARRAY_TYPE)
12623 tree t;
12624 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12625 type_quals);
12627 /* See if we already have an identically qualified type. */
12628 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12630 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12631 && TYPE_NAME (t) == TYPE_NAME (type)
12632 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12633 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12634 TYPE_ATTRIBUTES (type)))
12635 break;
12637 if (!t)
12639 tree domain = TYPE_DOMAIN (type);
12641 t = build_variant_type_copy (type);
12642 TREE_TYPE (t) = element_type;
12644 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12645 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12646 SET_TYPE_STRUCTURAL_EQUALITY (t);
12647 else if (TYPE_CANONICAL (element_type) != element_type
12648 || (domain && TYPE_CANONICAL (domain) != domain))
12650 tree unqualified_canon
12651 = build_array_type (TYPE_CANONICAL (element_type),
12652 domain? TYPE_CANONICAL (domain)
12653 : NULL_TREE);
12654 TYPE_CANONICAL (t)
12655 = c_build_qualified_type (unqualified_canon, type_quals);
12657 else
12658 TYPE_CANONICAL (t) = t;
12660 return t;
12663 /* A restrict-qualified pointer type must be a pointer to object or
12664 incomplete type. Note that the use of POINTER_TYPE_P also allows
12665 REFERENCE_TYPEs, which is appropriate for C++. */
12666 if ((type_quals & TYPE_QUAL_RESTRICT)
12667 && (!POINTER_TYPE_P (type)
12668 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12670 error ("invalid use of %<restrict%>");
12671 type_quals &= ~TYPE_QUAL_RESTRICT;
12674 return build_qualified_type (type, type_quals);
12677 /* Build a VA_ARG_EXPR for the C parser. */
12679 tree
12680 c_build_va_arg (location_t loc, tree expr, tree type)
12682 if (error_operand_p (type))
12683 return error_mark_node;
12684 else if (!COMPLETE_TYPE_P (type))
12686 error_at (loc, "second argument to %<va_arg%> is of incomplete "
12687 "type %qT", type);
12688 return error_mark_node;
12690 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12691 warning_at (loc, OPT_Wc___compat,
12692 "C++ requires promoted type, not enum type, in %<va_arg%>");
12693 return build_va_arg (loc, expr, type);
12696 /* Return truthvalue of whether T1 is the same tree structure as T2.
12697 Return 1 if they are the same. Return 0 if they are different. */
12699 bool
12700 c_tree_equal (tree t1, tree t2)
12702 enum tree_code code1, code2;
12704 if (t1 == t2)
12705 return true;
12706 if (!t1 || !t2)
12707 return false;
12709 for (code1 = TREE_CODE (t1);
12710 CONVERT_EXPR_CODE_P (code1)
12711 || code1 == NON_LVALUE_EXPR;
12712 code1 = TREE_CODE (t1))
12713 t1 = TREE_OPERAND (t1, 0);
12714 for (code2 = TREE_CODE (t2);
12715 CONVERT_EXPR_CODE_P (code2)
12716 || code2 == NON_LVALUE_EXPR;
12717 code2 = TREE_CODE (t2))
12718 t2 = TREE_OPERAND (t2, 0);
12720 /* They might have become equal now. */
12721 if (t1 == t2)
12722 return true;
12724 if (code1 != code2)
12725 return false;
12727 switch (code1)
12729 case INTEGER_CST:
12730 return wi::eq_p (t1, t2);
12732 case REAL_CST:
12733 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12735 case STRING_CST:
12736 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12737 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12738 TREE_STRING_LENGTH (t1));
12740 case FIXED_CST:
12741 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12742 TREE_FIXED_CST (t2));
12744 case COMPLEX_CST:
12745 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12746 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12748 case VECTOR_CST:
12749 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12751 case CONSTRUCTOR:
12752 /* We need to do this when determining whether or not two
12753 non-type pointer to member function template arguments
12754 are the same. */
12755 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12756 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12757 return false;
12759 tree field, value;
12760 unsigned int i;
12761 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12763 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12764 if (!c_tree_equal (field, elt2->index)
12765 || !c_tree_equal (value, elt2->value))
12766 return false;
12769 return true;
12771 case TREE_LIST:
12772 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12773 return false;
12774 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12775 return false;
12776 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12778 case SAVE_EXPR:
12779 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12781 case CALL_EXPR:
12783 tree arg1, arg2;
12784 call_expr_arg_iterator iter1, iter2;
12785 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12786 return false;
12787 for (arg1 = first_call_expr_arg (t1, &iter1),
12788 arg2 = first_call_expr_arg (t2, &iter2);
12789 arg1 && arg2;
12790 arg1 = next_call_expr_arg (&iter1),
12791 arg2 = next_call_expr_arg (&iter2))
12792 if (!c_tree_equal (arg1, arg2))
12793 return false;
12794 if (arg1 || arg2)
12795 return false;
12796 return true;
12799 case TARGET_EXPR:
12801 tree o1 = TREE_OPERAND (t1, 0);
12802 tree o2 = TREE_OPERAND (t2, 0);
12804 /* Special case: if either target is an unallocated VAR_DECL,
12805 it means that it's going to be unified with whatever the
12806 TARGET_EXPR is really supposed to initialize, so treat it
12807 as being equivalent to anything. */
12808 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
12809 && !DECL_RTL_SET_P (o1))
12810 /*Nop*/;
12811 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
12812 && !DECL_RTL_SET_P (o2))
12813 /*Nop*/;
12814 else if (!c_tree_equal (o1, o2))
12815 return false;
12817 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12820 case COMPONENT_REF:
12821 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12822 return false;
12823 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12825 case PARM_DECL:
12826 case VAR_DECL:
12827 case CONST_DECL:
12828 case FIELD_DECL:
12829 case FUNCTION_DECL:
12830 case IDENTIFIER_NODE:
12831 case SSA_NAME:
12832 return false;
12834 case TREE_VEC:
12836 unsigned ix;
12837 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12838 return false;
12839 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12840 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12841 TREE_VEC_ELT (t2, ix)))
12842 return false;
12843 return true;
12846 default:
12847 break;
12850 switch (TREE_CODE_CLASS (code1))
12852 case tcc_unary:
12853 case tcc_binary:
12854 case tcc_comparison:
12855 case tcc_expression:
12856 case tcc_vl_exp:
12857 case tcc_reference:
12858 case tcc_statement:
12860 int i, n = TREE_OPERAND_LENGTH (t1);
12862 switch (code1)
12864 case PREINCREMENT_EXPR:
12865 case PREDECREMENT_EXPR:
12866 case POSTINCREMENT_EXPR:
12867 case POSTDECREMENT_EXPR:
12868 n = 1;
12869 break;
12870 case ARRAY_REF:
12871 n = 2;
12872 break;
12873 default:
12874 break;
12877 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12878 && n != TREE_OPERAND_LENGTH (t2))
12879 return false;
12881 for (i = 0; i < n; ++i)
12882 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12883 return false;
12885 return true;
12888 case tcc_type:
12889 return comptypes (t1, t2);
12890 default:
12891 gcc_unreachable ();
12893 /* We can get here with --disable-checking. */
12894 return false;
12897 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12898 spawn-helper and BODY is the newly created body for FNDECL. */
12900 void
12901 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12903 tree list = alloc_stmt_list ();
12904 tree frame = make_cilk_frame (fndecl);
12905 tree dtor = create_cilk_function_exit (frame, false, true);
12906 add_local_decl (cfun, frame);
12908 DECL_SAVED_TREE (fndecl) = list;
12909 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12910 frame);
12911 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12912 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12914 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12915 append_to_statement_list (detach_expr, &body_list);
12917 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12918 body = fold_build_cleanup_point_expr (void_type_node, body);
12920 append_to_statement_list (body, &body_list);
12921 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12922 body_list, dtor), &list);
12925 /* Returns true when the function declaration FNDECL is implicit,
12926 introduced as a result of a call to an otherwise undeclared
12927 function, and false otherwise. */
12929 bool
12930 c_decl_implicit (const_tree fndecl)
12932 return C_DECL_IMPLICIT (fndecl);