Fix for ICE with -g on testcase with incomplete types.
[official-gcc.git] / gcc / c / c-typeck.c
blob1b0b9e2a6d700dd9907a58eb4c6a48c882c85e1e
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)));
4775 /* Need to convert condition operand into a vector mask. */
4776 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
4778 tree vectype = TREE_TYPE (ifexp);
4779 tree elem_type = TREE_TYPE (vectype);
4780 tree zero = build_int_cst (elem_type, 0);
4781 tree zero_vec = build_vector_from_val (vectype, zero);
4782 tree cmp_type = build_same_sized_truth_vector_type (vectype);
4783 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
4786 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4787 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4788 else
4790 if (int_operands)
4792 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4793 nested inside of the expression. */
4794 op1 = c_fully_fold (op1, false, NULL);
4795 op2 = c_fully_fold (op2, false, NULL);
4797 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4798 if (int_operands)
4799 ret = note_integer_operands (ret);
4801 if (semantic_result_type)
4802 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4804 protected_set_expr_location (ret, colon_loc);
4805 return ret;
4808 /* Return a compound expression that performs two expressions and
4809 returns the value of the second of them.
4811 LOC is the location of the COMPOUND_EXPR. */
4813 tree
4814 build_compound_expr (location_t loc, tree expr1, tree expr2)
4816 bool expr1_int_operands, expr2_int_operands;
4817 tree eptype = NULL_TREE;
4818 tree ret;
4820 if (flag_cilkplus
4821 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4822 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4824 error_at (loc,
4825 "spawned function call cannot be part of a comma expression");
4826 return error_mark_node;
4828 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4829 if (expr1_int_operands)
4830 expr1 = remove_c_maybe_const_expr (expr1);
4831 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4832 if (expr2_int_operands)
4833 expr2 = remove_c_maybe_const_expr (expr2);
4835 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4836 expr1 = TREE_OPERAND (expr1, 0);
4837 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4839 eptype = TREE_TYPE (expr2);
4840 expr2 = TREE_OPERAND (expr2, 0);
4843 if (!TREE_SIDE_EFFECTS (expr1))
4845 /* The left-hand operand of a comma expression is like an expression
4846 statement: with -Wunused, we should warn if it doesn't have
4847 any side-effects, unless it was explicitly cast to (void). */
4848 if (warn_unused_value)
4850 if (VOID_TYPE_P (TREE_TYPE (expr1))
4851 && CONVERT_EXPR_P (expr1))
4852 ; /* (void) a, b */
4853 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4854 && TREE_CODE (expr1) == COMPOUND_EXPR
4855 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4856 ; /* (void) a, (void) b, c */
4857 else
4858 warning_at (loc, OPT_Wunused_value,
4859 "left-hand operand of comma expression has no effect");
4862 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4863 && warn_unused_value)
4865 tree r = expr1;
4866 location_t cloc = loc;
4867 while (TREE_CODE (r) == COMPOUND_EXPR)
4869 if (EXPR_HAS_LOCATION (r))
4870 cloc = EXPR_LOCATION (r);
4871 r = TREE_OPERAND (r, 1);
4873 if (!TREE_SIDE_EFFECTS (r)
4874 && !VOID_TYPE_P (TREE_TYPE (r))
4875 && !CONVERT_EXPR_P (r))
4876 warning_at (cloc, OPT_Wunused_value,
4877 "right-hand operand of comma expression has no effect");
4880 /* With -Wunused, we should also warn if the left-hand operand does have
4881 side-effects, but computes a value which is not used. For example, in
4882 `foo() + bar(), baz()' the result of the `+' operator is not used,
4883 so we should issue a warning. */
4884 else if (warn_unused_value)
4885 warn_if_unused_value (expr1, loc);
4887 if (expr2 == error_mark_node)
4888 return error_mark_node;
4890 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4892 if (flag_isoc99
4893 && expr1_int_operands
4894 && expr2_int_operands)
4895 ret = note_integer_operands (ret);
4897 if (eptype)
4898 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4900 protected_set_expr_location (ret, loc);
4901 return ret;
4904 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4905 which we are casting. OTYPE is the type of the expression being
4906 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4907 of the cast. -Wcast-qual appeared on the command line. Named
4908 address space qualifiers are not handled here, because they result
4909 in different warnings. */
4911 static void
4912 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4914 tree in_type = type;
4915 tree in_otype = otype;
4916 int added = 0;
4917 int discarded = 0;
4918 bool is_const;
4920 /* Check that the qualifiers on IN_TYPE are a superset of the
4921 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4922 nodes is uninteresting and we stop as soon as we hit a
4923 non-POINTER_TYPE node on either type. */
4926 in_otype = TREE_TYPE (in_otype);
4927 in_type = TREE_TYPE (in_type);
4929 /* GNU C allows cv-qualified function types. 'const' means the
4930 function is very pure, 'volatile' means it can't return. We
4931 need to warn when such qualifiers are added, not when they're
4932 taken away. */
4933 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4934 && TREE_CODE (in_type) == FUNCTION_TYPE)
4935 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4936 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4937 else
4938 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4939 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4941 while (TREE_CODE (in_type) == POINTER_TYPE
4942 && TREE_CODE (in_otype) == POINTER_TYPE);
4944 if (added)
4945 warning_at (loc, OPT_Wcast_qual,
4946 "cast adds %q#v qualifier to function type", added);
4948 if (discarded)
4949 /* There are qualifiers present in IN_OTYPE that are not present
4950 in IN_TYPE. */
4951 warning_at (loc, OPT_Wcast_qual,
4952 "cast discards %qv qualifier from pointer target type",
4953 discarded);
4955 if (added || discarded)
4956 return;
4958 /* A cast from **T to const **T is unsafe, because it can cause a
4959 const value to be changed with no additional warning. We only
4960 issue this warning if T is the same on both sides, and we only
4961 issue the warning if there are the same number of pointers on
4962 both sides, as otherwise the cast is clearly unsafe anyhow. A
4963 cast is unsafe when a qualifier is added at one level and const
4964 is not present at all outer levels.
4966 To issue this warning, we check at each level whether the cast
4967 adds new qualifiers not already seen. We don't need to special
4968 case function types, as they won't have the same
4969 TYPE_MAIN_VARIANT. */
4971 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4972 return;
4973 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4974 return;
4976 in_type = type;
4977 in_otype = otype;
4978 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4981 in_type = TREE_TYPE (in_type);
4982 in_otype = TREE_TYPE (in_otype);
4983 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4984 && !is_const)
4986 warning_at (loc, OPT_Wcast_qual,
4987 "to be safe all intermediate pointers in cast from "
4988 "%qT to %qT must be %<const%> qualified",
4989 otype, type);
4990 break;
4992 if (is_const)
4993 is_const = TYPE_READONLY (in_type);
4995 while (TREE_CODE (in_type) == POINTER_TYPE);
4998 /* Build an expression representing a cast to type TYPE of expression EXPR.
4999 LOC is the location of the cast-- typically the open paren of the cast. */
5001 tree
5002 build_c_cast (location_t loc, tree type, tree expr)
5004 tree value;
5006 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5007 expr = TREE_OPERAND (expr, 0);
5009 value = expr;
5011 if (type == error_mark_node || expr == error_mark_node)
5012 return error_mark_node;
5014 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5015 only in <protocol> qualifications. But when constructing cast expressions,
5016 the protocols do matter and must be kept around. */
5017 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5018 return build1 (NOP_EXPR, type, expr);
5020 type = TYPE_MAIN_VARIANT (type);
5022 if (TREE_CODE (type) == ARRAY_TYPE)
5024 error_at (loc, "cast specifies array type");
5025 return error_mark_node;
5028 if (TREE_CODE (type) == FUNCTION_TYPE)
5030 error_at (loc, "cast specifies function type");
5031 return error_mark_node;
5034 if (!VOID_TYPE_P (type))
5036 value = require_complete_type (value);
5037 if (value == error_mark_node)
5038 return error_mark_node;
5041 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5043 if (TREE_CODE (type) == RECORD_TYPE
5044 || TREE_CODE (type) == UNION_TYPE)
5045 pedwarn (loc, OPT_Wpedantic,
5046 "ISO C forbids casting nonscalar to the same type");
5048 /* Convert to remove any qualifiers from VALUE's type. */
5049 value = convert (type, value);
5051 else if (TREE_CODE (type) == UNION_TYPE)
5053 tree field;
5055 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5056 if (TREE_TYPE (field) != error_mark_node
5057 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5058 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5059 break;
5061 if (field)
5063 tree t;
5064 bool maybe_const = true;
5066 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5067 t = c_fully_fold (value, false, &maybe_const);
5068 t = build_constructor_single (type, field, t);
5069 if (!maybe_const)
5070 t = c_wrap_maybe_const (t, true);
5071 t = digest_init (loc, type, t,
5072 NULL_TREE, false, true, 0);
5073 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5074 return t;
5076 error_at (loc, "cast to union type from type not present in union");
5077 return error_mark_node;
5079 else
5081 tree otype, ovalue;
5083 if (type == void_type_node)
5085 tree t = build1 (CONVERT_EXPR, type, value);
5086 SET_EXPR_LOCATION (t, loc);
5087 return t;
5090 otype = TREE_TYPE (value);
5092 /* Optionally warn about potentially worrisome casts. */
5093 if (warn_cast_qual
5094 && TREE_CODE (type) == POINTER_TYPE
5095 && TREE_CODE (otype) == POINTER_TYPE)
5096 handle_warn_cast_qual (loc, type, otype);
5098 /* Warn about conversions between pointers to disjoint
5099 address spaces. */
5100 if (TREE_CODE (type) == POINTER_TYPE
5101 && TREE_CODE (otype) == POINTER_TYPE
5102 && !null_pointer_constant_p (value))
5104 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5105 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5106 addr_space_t as_common;
5108 if (!addr_space_superset (as_to, as_from, &as_common))
5110 if (ADDR_SPACE_GENERIC_P (as_from))
5111 warning_at (loc, 0, "cast to %s address space pointer "
5112 "from disjoint generic address space pointer",
5113 c_addr_space_name (as_to));
5115 else if (ADDR_SPACE_GENERIC_P (as_to))
5116 warning_at (loc, 0, "cast to generic address space pointer "
5117 "from disjoint %s address space pointer",
5118 c_addr_space_name (as_from));
5120 else
5121 warning_at (loc, 0, "cast to %s address space pointer "
5122 "from disjoint %s address space pointer",
5123 c_addr_space_name (as_to),
5124 c_addr_space_name (as_from));
5128 /* Warn about possible alignment problems. */
5129 if (STRICT_ALIGNMENT
5130 && TREE_CODE (type) == POINTER_TYPE
5131 && TREE_CODE (otype) == POINTER_TYPE
5132 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5133 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5134 /* Don't warn about opaque types, where the actual alignment
5135 restriction is unknown. */
5136 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5137 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5138 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5139 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5140 warning_at (loc, OPT_Wcast_align,
5141 "cast increases required alignment of target type");
5143 if (TREE_CODE (type) == INTEGER_TYPE
5144 && TREE_CODE (otype) == POINTER_TYPE
5145 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5146 /* Unlike conversion of integers to pointers, where the
5147 warning is disabled for converting constants because
5148 of cases such as SIG_*, warn about converting constant
5149 pointers to integers. In some cases it may cause unwanted
5150 sign extension, and a warning is appropriate. */
5151 warning_at (loc, OPT_Wpointer_to_int_cast,
5152 "cast from pointer to integer of different size");
5154 if (TREE_CODE (value) == CALL_EXPR
5155 && TREE_CODE (type) != TREE_CODE (otype))
5156 warning_at (loc, OPT_Wbad_function_cast,
5157 "cast from function call of type %qT "
5158 "to non-matching type %qT", otype, type);
5160 if (TREE_CODE (type) == POINTER_TYPE
5161 && TREE_CODE (otype) == INTEGER_TYPE
5162 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5163 /* Don't warn about converting any constant. */
5164 && !TREE_CONSTANT (value))
5165 warning_at (loc,
5166 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5167 "of different size");
5169 if (warn_strict_aliasing <= 2)
5170 strict_aliasing_warning (otype, type, expr);
5172 /* If pedantic, warn for conversions between function and object
5173 pointer types, except for converting a null pointer constant
5174 to function pointer type. */
5175 if (pedantic
5176 && TREE_CODE (type) == POINTER_TYPE
5177 && TREE_CODE (otype) == POINTER_TYPE
5178 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5179 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5180 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5181 "conversion of function pointer to object pointer type");
5183 if (pedantic
5184 && TREE_CODE (type) == POINTER_TYPE
5185 && TREE_CODE (otype) == POINTER_TYPE
5186 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5187 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5188 && !null_pointer_constant_p (value))
5189 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5190 "conversion of object pointer to function pointer type");
5192 ovalue = value;
5193 value = convert (type, value);
5195 /* Ignore any integer overflow caused by the cast. */
5196 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5198 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5200 if (!TREE_OVERFLOW (value))
5202 /* Avoid clobbering a shared constant. */
5203 value = copy_node (value);
5204 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5207 else if (TREE_OVERFLOW (value))
5208 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5209 value = wide_int_to_tree (TREE_TYPE (value), value);
5213 /* Don't let a cast be an lvalue. */
5214 if (lvalue_p (value))
5215 value = non_lvalue_loc (loc, value);
5217 /* Don't allow the results of casting to floating-point or complex
5218 types be confused with actual constants, or casts involving
5219 integer and pointer types other than direct integer-to-integer
5220 and integer-to-pointer be confused with integer constant
5221 expressions and null pointer constants. */
5222 if (TREE_CODE (value) == REAL_CST
5223 || TREE_CODE (value) == COMPLEX_CST
5224 || (TREE_CODE (value) == INTEGER_CST
5225 && !((TREE_CODE (expr) == INTEGER_CST
5226 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5227 || TREE_CODE (expr) == REAL_CST
5228 || TREE_CODE (expr) == COMPLEX_CST)))
5229 value = build1 (NOP_EXPR, type, value);
5231 protected_set_expr_location (value, loc);
5232 return value;
5235 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5236 location of the open paren of the cast, or the position of the cast
5237 expr. */
5238 tree
5239 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5241 tree type;
5242 tree type_expr = NULL_TREE;
5243 bool type_expr_const = true;
5244 tree ret;
5245 int saved_wsp = warn_strict_prototypes;
5247 /* This avoids warnings about unprototyped casts on
5248 integers. E.g. "#define SIG_DFL (void(*)())0". */
5249 if (TREE_CODE (expr) == INTEGER_CST)
5250 warn_strict_prototypes = 0;
5251 type = groktypename (type_name, &type_expr, &type_expr_const);
5252 warn_strict_prototypes = saved_wsp;
5254 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5255 && reject_gcc_builtin (expr))
5256 return error_mark_node;
5258 ret = build_c_cast (loc, type, expr);
5259 if (type_expr)
5261 bool inner_expr_const = true;
5262 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5263 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5264 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5265 && inner_expr_const);
5266 SET_EXPR_LOCATION (ret, loc);
5269 if (!EXPR_HAS_LOCATION (ret))
5270 protected_set_expr_location (ret, loc);
5272 /* C++ does not permits types to be defined in a cast, but it
5273 allows references to incomplete types. */
5274 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5275 warning_at (loc, OPT_Wc___compat,
5276 "defining a type in a cast is invalid in C++");
5278 return ret;
5281 /* Build an assignment expression of lvalue LHS from value RHS.
5282 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5283 may differ from TREE_TYPE (LHS) for an enum bitfield.
5284 MODIFYCODE is the code for a binary operator that we use
5285 to combine the old value of LHS with RHS to get the new value.
5286 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5287 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5288 which may differ from TREE_TYPE (RHS) for an enum value.
5290 LOCATION is the location of the MODIFYCODE operator.
5291 RHS_LOC is the location of the RHS. */
5293 tree
5294 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5295 enum tree_code modifycode,
5296 location_t rhs_loc, tree rhs, tree rhs_origtype)
5298 tree result;
5299 tree newrhs;
5300 tree rhseval = NULL_TREE;
5301 tree rhs_semantic_type = NULL_TREE;
5302 tree lhstype = TREE_TYPE (lhs);
5303 tree olhstype = lhstype;
5304 bool npc;
5305 bool is_atomic_op;
5307 /* Types that aren't fully specified cannot be used in assignments. */
5308 lhs = require_complete_type (lhs);
5310 /* Avoid duplicate error messages from operands that had errors. */
5311 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5312 return error_mark_node;
5314 /* Ensure an error for assigning a non-lvalue array to an array in
5315 C90. */
5316 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5318 error_at (location, "assignment to expression with array type");
5319 return error_mark_node;
5322 /* For ObjC properties, defer this check. */
5323 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5324 return error_mark_node;
5326 is_atomic_op = really_atomic_lvalue (lhs);
5328 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5330 rhs_semantic_type = TREE_TYPE (rhs);
5331 rhs = TREE_OPERAND (rhs, 0);
5334 newrhs = rhs;
5336 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5338 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5339 lhs_origtype, modifycode, rhs_loc, rhs,
5340 rhs_origtype);
5341 if (inner == error_mark_node)
5342 return error_mark_node;
5343 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5344 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5345 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5346 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5347 protected_set_expr_location (result, location);
5348 return result;
5351 /* If a binary op has been requested, combine the old LHS value with the RHS
5352 producing the value we should actually store into the LHS. */
5354 if (modifycode != NOP_EXPR)
5356 lhs = c_fully_fold (lhs, false, NULL);
5357 lhs = stabilize_reference (lhs);
5359 /* Construct the RHS for any non-atomic compound assignemnt. */
5360 if (!is_atomic_op)
5362 /* If in LHS op= RHS the RHS has side-effects, ensure they
5363 are preevaluated before the rest of the assignment expression's
5364 side-effects, because RHS could contain e.g. function calls
5365 that modify LHS. */
5366 if (TREE_SIDE_EFFECTS (rhs))
5368 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5369 rhseval = newrhs;
5371 newrhs = build_binary_op (location,
5372 modifycode, lhs, newrhs, 1);
5374 /* The original type of the right hand side is no longer
5375 meaningful. */
5376 rhs_origtype = NULL_TREE;
5380 if (c_dialect_objc ())
5382 /* Check if we are modifying an Objective-C property reference;
5383 if so, we need to generate setter calls. */
5384 result = objc_maybe_build_modify_expr (lhs, newrhs);
5385 if (result)
5386 goto return_result;
5388 /* Else, do the check that we postponed for Objective-C. */
5389 if (!lvalue_or_else (location, lhs, lv_assign))
5390 return error_mark_node;
5393 /* Give an error for storing in something that is 'const'. */
5395 if (TYPE_READONLY (lhstype)
5396 || ((TREE_CODE (lhstype) == RECORD_TYPE
5397 || TREE_CODE (lhstype) == UNION_TYPE)
5398 && C_TYPE_FIELDS_READONLY (lhstype)))
5400 readonly_error (location, lhs, lv_assign);
5401 return error_mark_node;
5403 else if (TREE_READONLY (lhs))
5404 readonly_warning (lhs, lv_assign);
5406 /* If storing into a structure or union member,
5407 it has probably been given type `int'.
5408 Compute the type that would go with
5409 the actual amount of storage the member occupies. */
5411 if (TREE_CODE (lhs) == COMPONENT_REF
5412 && (TREE_CODE (lhstype) == INTEGER_TYPE
5413 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5414 || TREE_CODE (lhstype) == REAL_TYPE
5415 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5416 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5418 /* If storing in a field that is in actuality a short or narrower than one,
5419 we must store in the field in its actual type. */
5421 if (lhstype != TREE_TYPE (lhs))
5423 lhs = copy_node (lhs);
5424 TREE_TYPE (lhs) = lhstype;
5427 /* Issue -Wc++-compat warnings about an assignment to an enum type
5428 when LHS does not have its original type. This happens for,
5429 e.g., an enum bitfield in a struct. */
5430 if (warn_cxx_compat
5431 && lhs_origtype != NULL_TREE
5432 && lhs_origtype != lhstype
5433 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5435 tree checktype = (rhs_origtype != NULL_TREE
5436 ? rhs_origtype
5437 : TREE_TYPE (rhs));
5438 if (checktype != error_mark_node
5439 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5440 || (is_atomic_op && modifycode != NOP_EXPR)))
5441 warning_at (location, OPT_Wc___compat,
5442 "enum conversion in assignment is invalid in C++");
5445 /* If the lhs is atomic, remove that qualifier. */
5446 if (is_atomic_op)
5448 lhstype = build_qualified_type (lhstype,
5449 (TYPE_QUALS (lhstype)
5450 & ~TYPE_QUAL_ATOMIC));
5451 olhstype = build_qualified_type (olhstype,
5452 (TYPE_QUALS (lhstype)
5453 & ~TYPE_QUAL_ATOMIC));
5456 /* Convert new value to destination type. Fold it first, then
5457 restore any excess precision information, for the sake of
5458 conversion warnings. */
5460 if (!(is_atomic_op && modifycode != NOP_EXPR))
5462 npc = null_pointer_constant_p (newrhs);
5463 newrhs = c_fully_fold (newrhs, false, NULL);
5464 if (rhs_semantic_type)
5465 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5466 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5467 rhs_origtype, ic_assign, npc,
5468 NULL_TREE, NULL_TREE, 0);
5469 if (TREE_CODE (newrhs) == ERROR_MARK)
5470 return error_mark_node;
5473 /* Emit ObjC write barrier, if necessary. */
5474 if (c_dialect_objc () && flag_objc_gc)
5476 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5477 if (result)
5479 protected_set_expr_location (result, location);
5480 goto return_result;
5484 /* Scan operands. */
5486 if (is_atomic_op)
5487 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5488 else
5490 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5491 TREE_SIDE_EFFECTS (result) = 1;
5492 protected_set_expr_location (result, location);
5495 /* If we got the LHS in a different type for storing in,
5496 convert the result back to the nominal type of LHS
5497 so that the value we return always has the same type
5498 as the LHS argument. */
5500 if (olhstype == TREE_TYPE (result))
5501 goto return_result;
5503 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5504 rhs_origtype, ic_assign, false, NULL_TREE,
5505 NULL_TREE, 0);
5506 protected_set_expr_location (result, location);
5508 return_result:
5509 if (rhseval)
5510 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5511 return result;
5514 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5515 This is used to implement -fplan9-extensions. */
5517 static bool
5518 find_anonymous_field_with_type (tree struct_type, tree type)
5520 tree field;
5521 bool found;
5523 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5524 || TREE_CODE (struct_type) == UNION_TYPE);
5525 found = false;
5526 for (field = TYPE_FIELDS (struct_type);
5527 field != NULL_TREE;
5528 field = TREE_CHAIN (field))
5530 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5531 ? c_build_qualified_type (TREE_TYPE (field),
5532 TYPE_QUAL_ATOMIC)
5533 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5534 if (DECL_NAME (field) == NULL
5535 && comptypes (type, fieldtype))
5537 if (found)
5538 return false;
5539 found = true;
5541 else if (DECL_NAME (field) == NULL
5542 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5543 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5544 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5546 if (found)
5547 return false;
5548 found = true;
5551 return found;
5554 /* RHS is an expression whose type is pointer to struct. If there is
5555 an anonymous field in RHS with type TYPE, then return a pointer to
5556 that field in RHS. This is used with -fplan9-extensions. This
5557 returns NULL if no conversion could be found. */
5559 static tree
5560 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5562 tree rhs_struct_type, lhs_main_type;
5563 tree field, found_field;
5564 bool found_sub_field;
5565 tree ret;
5567 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5568 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5569 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5570 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5572 gcc_assert (POINTER_TYPE_P (type));
5573 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5574 ? c_build_qualified_type (TREE_TYPE (type),
5575 TYPE_QUAL_ATOMIC)
5576 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5578 found_field = NULL_TREE;
5579 found_sub_field = false;
5580 for (field = TYPE_FIELDS (rhs_struct_type);
5581 field != NULL_TREE;
5582 field = TREE_CHAIN (field))
5584 if (DECL_NAME (field) != NULL_TREE
5585 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5586 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5587 continue;
5588 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5589 ? c_build_qualified_type (TREE_TYPE (field),
5590 TYPE_QUAL_ATOMIC)
5591 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5592 if (comptypes (lhs_main_type, fieldtype))
5594 if (found_field != NULL_TREE)
5595 return NULL_TREE;
5596 found_field = field;
5598 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5599 lhs_main_type))
5601 if (found_field != NULL_TREE)
5602 return NULL_TREE;
5603 found_field = field;
5604 found_sub_field = true;
5608 if (found_field == NULL_TREE)
5609 return NULL_TREE;
5611 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5612 build_fold_indirect_ref (rhs), found_field,
5613 NULL_TREE);
5614 ret = build_fold_addr_expr_loc (location, ret);
5616 if (found_sub_field)
5618 ret = convert_to_anonymous_field (location, type, ret);
5619 gcc_assert (ret != NULL_TREE);
5622 return ret;
5625 /* Issue an error message for a bad initializer component.
5626 GMSGID identifies the message.
5627 The component name is taken from the spelling stack. */
5629 static void
5630 error_init (location_t loc, const char *gmsgid)
5632 char *ofwhat;
5634 /* The gmsgid may be a format string with %< and %>. */
5635 error_at (loc, gmsgid);
5636 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5637 if (*ofwhat)
5638 inform (loc, "(near initialization for %qs)", ofwhat);
5641 /* Issue a pedantic warning for a bad initializer component. OPT is
5642 the option OPT_* (from options.h) controlling this warning or 0 if
5643 it is unconditionally given. GMSGID identifies the message. The
5644 component name is taken from the spelling stack. */
5646 static void
5647 pedwarn_init (location_t location, int opt, const char *gmsgid)
5649 char *ofwhat;
5650 bool warned;
5652 /* The gmsgid may be a format string with %< and %>. */
5653 warned = pedwarn (location, opt, gmsgid);
5654 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5655 if (*ofwhat && warned)
5656 inform (location, "(near initialization for %qs)", ofwhat);
5659 /* Issue a warning for a bad initializer component.
5661 OPT is the OPT_W* value corresponding to the warning option that
5662 controls this warning. GMSGID identifies the message. The
5663 component name is taken from the spelling stack. */
5665 static void
5666 warning_init (location_t loc, int opt, const char *gmsgid)
5668 char *ofwhat;
5669 bool warned;
5671 /* The gmsgid may be a format string with %< and %>. */
5672 warned = warning_at (loc, opt, gmsgid);
5673 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5674 if (*ofwhat && warned)
5675 inform (loc, "(near initialization for %qs)", ofwhat);
5678 /* If TYPE is an array type and EXPR is a parenthesized string
5679 constant, warn if pedantic that EXPR is being used to initialize an
5680 object of type TYPE. */
5682 void
5683 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5685 if (pedantic
5686 && TREE_CODE (type) == ARRAY_TYPE
5687 && TREE_CODE (expr.value) == STRING_CST
5688 && expr.original_code != STRING_CST)
5689 pedwarn_init (loc, OPT_Wpedantic,
5690 "array initialized from parenthesized string constant");
5693 /* Convert value RHS to type TYPE as preparation for an assignment to
5694 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5695 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5696 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5697 constant before any folding.
5698 The real work of conversion is done by `convert'.
5699 The purpose of this function is to generate error messages
5700 for assignments that are not allowed in C.
5701 ERRTYPE says whether it is argument passing, assignment,
5702 initialization or return.
5704 In the following example, '~' denotes where EXPR_LOC and '^' where
5705 LOCATION point to:
5707 f (var); [ic_argpass]
5708 ^ ~~~
5709 x = var; [ic_assign]
5710 ^ ~~~;
5711 int x = var; [ic_init]
5713 return x; [ic_return]
5716 FUNCTION is a tree for the function being called.
5717 PARMNUM is the number of the argument, for printing in error messages. */
5719 static tree
5720 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5721 tree rhs, tree origtype, enum impl_conv errtype,
5722 bool null_pointer_constant, tree fundecl,
5723 tree function, int parmnum)
5725 enum tree_code codel = TREE_CODE (type);
5726 tree orig_rhs = rhs;
5727 tree rhstype;
5728 enum tree_code coder;
5729 tree rname = NULL_TREE;
5730 bool objc_ok = false;
5732 /* Use the expansion point location to handle cases such as user's
5733 function returning a wrong-type macro defined in a system header. */
5734 location = expansion_point_location_if_in_system_header (location);
5736 if (errtype == ic_argpass)
5738 tree selector;
5739 /* Change pointer to function to the function itself for
5740 diagnostics. */
5741 if (TREE_CODE (function) == ADDR_EXPR
5742 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5743 function = TREE_OPERAND (function, 0);
5745 /* Handle an ObjC selector specially for diagnostics. */
5746 selector = objc_message_selector ();
5747 rname = function;
5748 if (selector && parmnum > 2)
5750 rname = selector;
5751 parmnum -= 2;
5755 /* This macro is used to emit diagnostics to ensure that all format
5756 strings are complete sentences, visible to gettext and checked at
5757 compile time. */
5758 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5759 do { \
5760 switch (errtype) \
5762 case ic_argpass: \
5763 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5764 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5765 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5766 "expected %qT but argument is of type %qT", \
5767 type, rhstype); \
5768 break; \
5769 case ic_assign: \
5770 pedwarn (LOCATION, OPT, AS); \
5771 break; \
5772 case ic_init: \
5773 pedwarn_init (LOCATION, OPT, IN); \
5774 break; \
5775 case ic_return: \
5776 pedwarn (LOCATION, OPT, RE); \
5777 break; \
5778 default: \
5779 gcc_unreachable (); \
5781 } while (0)
5783 /* This macro is used to emit diagnostics to ensure that all format
5784 strings are complete sentences, visible to gettext and checked at
5785 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
5786 extra parameter to enumerate qualifiers. */
5787 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5788 do { \
5789 switch (errtype) \
5791 case ic_argpass: \
5792 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5793 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5794 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5795 "expected %qT but argument is of type %qT", \
5796 type, rhstype); \
5797 break; \
5798 case ic_assign: \
5799 pedwarn (LOCATION, OPT, AS, QUALS); \
5800 break; \
5801 case ic_init: \
5802 pedwarn (LOCATION, OPT, IN, QUALS); \
5803 break; \
5804 case ic_return: \
5805 pedwarn (LOCATION, OPT, RE, QUALS); \
5806 break; \
5807 default: \
5808 gcc_unreachable (); \
5810 } while (0)
5812 /* This macro is used to emit diagnostics to ensure that all format
5813 strings are complete sentences, visible to gettext and checked at
5814 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5815 warning_at instead of pedwarn. */
5816 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5817 do { \
5818 switch (errtype) \
5820 case ic_argpass: \
5821 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5822 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5823 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5824 "expected %qT but argument is of type %qT", \
5825 type, rhstype); \
5826 break; \
5827 case ic_assign: \
5828 warning_at (LOCATION, OPT, AS, QUALS); \
5829 break; \
5830 case ic_init: \
5831 warning_at (LOCATION, OPT, IN, QUALS); \
5832 break; \
5833 case ic_return: \
5834 warning_at (LOCATION, OPT, RE, QUALS); \
5835 break; \
5836 default: \
5837 gcc_unreachable (); \
5839 } while (0)
5841 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5842 rhs = TREE_OPERAND (rhs, 0);
5844 rhstype = TREE_TYPE (rhs);
5845 coder = TREE_CODE (rhstype);
5847 if (coder == ERROR_MARK)
5848 return error_mark_node;
5850 if (c_dialect_objc ())
5852 int parmno;
5854 switch (errtype)
5856 case ic_return:
5857 parmno = 0;
5858 break;
5860 case ic_assign:
5861 parmno = -1;
5862 break;
5864 case ic_init:
5865 parmno = -2;
5866 break;
5868 default:
5869 parmno = parmnum;
5870 break;
5873 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5876 if (warn_cxx_compat)
5878 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5879 if (checktype != error_mark_node
5880 && TREE_CODE (type) == ENUMERAL_TYPE
5881 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5883 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5884 G_("enum conversion when passing argument "
5885 "%d of %qE is invalid in C++"),
5886 G_("enum conversion in assignment is "
5887 "invalid in C++"),
5888 G_("enum conversion in initialization is "
5889 "invalid in C++"),
5890 G_("enum conversion in return is "
5891 "invalid in C++"));
5895 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5896 return rhs;
5898 if (coder == VOID_TYPE)
5900 /* Except for passing an argument to an unprototyped function,
5901 this is a constraint violation. When passing an argument to
5902 an unprototyped function, it is compile-time undefined;
5903 making it a constraint in that case was rejected in
5904 DR#252. */
5905 error_at (location, "void value not ignored as it ought to be");
5906 return error_mark_node;
5908 rhs = require_complete_type (rhs);
5909 if (rhs == error_mark_node)
5910 return error_mark_node;
5912 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
5913 return error_mark_node;
5915 /* A non-reference type can convert to a reference. This handles
5916 va_start, va_copy and possibly port built-ins. */
5917 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5919 if (!lvalue_p (rhs))
5921 error_at (location, "cannot pass rvalue to reference parameter");
5922 return error_mark_node;
5924 if (!c_mark_addressable (rhs))
5925 return error_mark_node;
5926 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5927 SET_EXPR_LOCATION (rhs, location);
5929 rhs = convert_for_assignment (location, expr_loc,
5930 build_pointer_type (TREE_TYPE (type)),
5931 rhs, origtype, errtype,
5932 null_pointer_constant, fundecl, function,
5933 parmnum);
5934 if (rhs == error_mark_node)
5935 return error_mark_node;
5937 rhs = build1 (NOP_EXPR, type, rhs);
5938 SET_EXPR_LOCATION (rhs, location);
5939 return rhs;
5941 /* Some types can interconvert without explicit casts. */
5942 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5943 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5944 return convert (type, rhs);
5945 /* Arithmetic types all interconvert, and enum is treated like int. */
5946 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5947 || codel == FIXED_POINT_TYPE
5948 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5949 || codel == BOOLEAN_TYPE)
5950 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5951 || coder == FIXED_POINT_TYPE
5952 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5953 || coder == BOOLEAN_TYPE))
5955 tree ret;
5956 bool save = in_late_binary_op;
5957 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
5958 || (coder == REAL_TYPE
5959 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
5960 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
5961 in_late_binary_op = true;
5962 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5963 ? expr_loc : location, type, orig_rhs);
5964 in_late_binary_op = save;
5965 return ret;
5968 /* Aggregates in different TUs might need conversion. */
5969 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5970 && codel == coder
5971 && comptypes (type, rhstype))
5972 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5973 ? expr_loc : location, type, rhs);
5975 /* Conversion to a transparent union or record from its member types.
5976 This applies only to function arguments. */
5977 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5978 && TYPE_TRANSPARENT_AGGR (type))
5979 && errtype == ic_argpass)
5981 tree memb, marginal_memb = NULL_TREE;
5983 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5985 tree memb_type = TREE_TYPE (memb);
5987 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5988 TYPE_MAIN_VARIANT (rhstype)))
5989 break;
5991 if (TREE_CODE (memb_type) != POINTER_TYPE)
5992 continue;
5994 if (coder == POINTER_TYPE)
5996 tree ttl = TREE_TYPE (memb_type);
5997 tree ttr = TREE_TYPE (rhstype);
5999 /* Any non-function converts to a [const][volatile] void *
6000 and vice versa; otherwise, targets must be the same.
6001 Meanwhile, the lhs target must have all the qualifiers of
6002 the rhs. */
6003 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6004 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6005 || comp_target_types (location, memb_type, rhstype))
6007 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6008 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6009 /* If this type won't generate any warnings, use it. */
6010 if (lquals == rquals
6011 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6012 && TREE_CODE (ttl) == FUNCTION_TYPE)
6013 ? ((lquals | rquals) == rquals)
6014 : ((lquals | rquals) == lquals)))
6015 break;
6017 /* Keep looking for a better type, but remember this one. */
6018 if (!marginal_memb)
6019 marginal_memb = memb;
6023 /* Can convert integer zero to any pointer type. */
6024 if (null_pointer_constant)
6026 rhs = null_pointer_node;
6027 break;
6031 if (memb || marginal_memb)
6033 if (!memb)
6035 /* We have only a marginally acceptable member type;
6036 it needs a warning. */
6037 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6038 tree ttr = TREE_TYPE (rhstype);
6040 /* Const and volatile mean something different for function
6041 types, so the usual warnings are not appropriate. */
6042 if (TREE_CODE (ttr) == FUNCTION_TYPE
6043 && TREE_CODE (ttl) == FUNCTION_TYPE)
6045 /* Because const and volatile on functions are
6046 restrictions that say the function will not do
6047 certain things, it is okay to use a const or volatile
6048 function where an ordinary one is wanted, but not
6049 vice-versa. */
6050 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6051 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6052 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6053 OPT_Wdiscarded_qualifiers,
6054 G_("passing argument %d of %qE "
6055 "makes %q#v qualified function "
6056 "pointer from unqualified"),
6057 G_("assignment makes %q#v qualified "
6058 "function pointer from "
6059 "unqualified"),
6060 G_("initialization makes %q#v qualified "
6061 "function pointer from "
6062 "unqualified"),
6063 G_("return makes %q#v qualified function "
6064 "pointer from unqualified"),
6065 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6067 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6068 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6069 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6070 OPT_Wdiscarded_qualifiers,
6071 G_("passing argument %d of %qE discards "
6072 "%qv qualifier from pointer target type"),
6073 G_("assignment discards %qv qualifier "
6074 "from pointer target type"),
6075 G_("initialization discards %qv qualifier "
6076 "from pointer target type"),
6077 G_("return discards %qv qualifier from "
6078 "pointer target type"),
6079 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6081 memb = marginal_memb;
6084 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6085 pedwarn (location, OPT_Wpedantic,
6086 "ISO C prohibits argument conversion to union type");
6088 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6089 return build_constructor_single (type, memb, rhs);
6093 /* Conversions among pointers */
6094 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6095 && (coder == codel))
6097 tree ttl = TREE_TYPE (type);
6098 tree ttr = TREE_TYPE (rhstype);
6099 tree mvl = ttl;
6100 tree mvr = ttr;
6101 bool is_opaque_pointer;
6102 int target_cmp = 0; /* Cache comp_target_types () result. */
6103 addr_space_t asl;
6104 addr_space_t asr;
6106 if (TREE_CODE (mvl) != ARRAY_TYPE)
6107 mvl = (TYPE_ATOMIC (mvl)
6108 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6109 TYPE_QUAL_ATOMIC)
6110 : TYPE_MAIN_VARIANT (mvl));
6111 if (TREE_CODE (mvr) != ARRAY_TYPE)
6112 mvr = (TYPE_ATOMIC (mvr)
6113 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6114 TYPE_QUAL_ATOMIC)
6115 : TYPE_MAIN_VARIANT (mvr));
6116 /* Opaque pointers are treated like void pointers. */
6117 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6119 /* The Plan 9 compiler permits a pointer to a struct to be
6120 automatically converted into a pointer to an anonymous field
6121 within the struct. */
6122 if (flag_plan9_extensions
6123 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6124 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6125 && mvl != mvr)
6127 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6128 if (new_rhs != NULL_TREE)
6130 rhs = new_rhs;
6131 rhstype = TREE_TYPE (rhs);
6132 coder = TREE_CODE (rhstype);
6133 ttr = TREE_TYPE (rhstype);
6134 mvr = TYPE_MAIN_VARIANT (ttr);
6138 /* C++ does not allow the implicit conversion void* -> T*. However,
6139 for the purpose of reducing the number of false positives, we
6140 tolerate the special case of
6142 int *p = NULL;
6144 where NULL is typically defined in C to be '(void *) 0'. */
6145 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6146 warning_at (errtype == ic_argpass ? expr_loc : location,
6147 OPT_Wc___compat,
6148 "request for implicit conversion "
6149 "from %qT to %qT not permitted in C++", rhstype, type);
6151 /* See if the pointers point to incompatible address spaces. */
6152 asl = TYPE_ADDR_SPACE (ttl);
6153 asr = TYPE_ADDR_SPACE (ttr);
6154 if (!null_pointer_constant_p (rhs)
6155 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6157 switch (errtype)
6159 case ic_argpass:
6160 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6161 "non-enclosed address space", parmnum, rname);
6162 break;
6163 case ic_assign:
6164 error_at (location, "assignment from pointer to "
6165 "non-enclosed address space");
6166 break;
6167 case ic_init:
6168 error_at (location, "initialization from pointer to "
6169 "non-enclosed address space");
6170 break;
6171 case ic_return:
6172 error_at (location, "return from pointer to "
6173 "non-enclosed address space");
6174 break;
6175 default:
6176 gcc_unreachable ();
6178 return error_mark_node;
6181 /* Check if the right-hand side has a format attribute but the
6182 left-hand side doesn't. */
6183 if (warn_suggest_attribute_format
6184 && check_missing_format_attribute (type, rhstype))
6186 switch (errtype)
6188 case ic_argpass:
6189 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6190 "argument %d of %qE might be "
6191 "a candidate for a format attribute",
6192 parmnum, rname);
6193 break;
6194 case ic_assign:
6195 warning_at (location, OPT_Wsuggest_attribute_format,
6196 "assignment left-hand side might be "
6197 "a candidate for a format attribute");
6198 break;
6199 case ic_init:
6200 warning_at (location, OPT_Wsuggest_attribute_format,
6201 "initialization left-hand side might be "
6202 "a candidate for a format attribute");
6203 break;
6204 case ic_return:
6205 warning_at (location, OPT_Wsuggest_attribute_format,
6206 "return type might be "
6207 "a candidate for a format attribute");
6208 break;
6209 default:
6210 gcc_unreachable ();
6214 /* Any non-function converts to a [const][volatile] void *
6215 and vice versa; otherwise, targets must be the same.
6216 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6217 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6218 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6219 || (target_cmp = comp_target_types (location, type, rhstype))
6220 || is_opaque_pointer
6221 || ((c_common_unsigned_type (mvl)
6222 == c_common_unsigned_type (mvr))
6223 && (c_common_signed_type (mvl)
6224 == c_common_signed_type (mvr))
6225 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6227 /* Warn about loss of qualifers from pointers to arrays with
6228 qualifiers on the element type. */
6229 if (TREE_CODE (ttr) == ARRAY_TYPE)
6231 ttr = strip_array_types (ttr);
6232 ttl = strip_array_types (ttl);
6234 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6235 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6236 WARNING_FOR_QUALIFIERS (location, expr_loc,
6237 OPT_Wdiscarded_array_qualifiers,
6238 G_("passing argument %d of %qE discards "
6239 "%qv qualifier from pointer target type"),
6240 G_("assignment discards %qv qualifier "
6241 "from pointer target type"),
6242 G_("initialization discards %qv qualifier "
6243 "from pointer target type"),
6244 G_("return discards %qv qualifier from "
6245 "pointer target type"),
6246 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6248 else if (pedantic
6249 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6251 (VOID_TYPE_P (ttr)
6252 && !null_pointer_constant
6253 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6254 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6255 G_("ISO C forbids passing argument %d of "
6256 "%qE between function pointer "
6257 "and %<void *%>"),
6258 G_("ISO C forbids assignment between "
6259 "function pointer and %<void *%>"),
6260 G_("ISO C forbids initialization between "
6261 "function pointer and %<void *%>"),
6262 G_("ISO C forbids return between function "
6263 "pointer and %<void *%>"));
6264 /* Const and volatile mean something different for function types,
6265 so the usual warnings are not appropriate. */
6266 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6267 && TREE_CODE (ttl) != FUNCTION_TYPE)
6269 /* Don't warn about loss of qualifier for conversions from
6270 qualified void* to pointers to arrays with corresponding
6271 qualifier on the element type. */
6272 if (!pedantic)
6273 ttl = strip_array_types (ttl);
6275 /* Assignments between atomic and non-atomic objects are OK. */
6276 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6277 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6279 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6280 OPT_Wdiscarded_qualifiers,
6281 G_("passing argument %d of %qE discards "
6282 "%qv qualifier from pointer target type"),
6283 G_("assignment discards %qv qualifier "
6284 "from pointer target type"),
6285 G_("initialization discards %qv qualifier "
6286 "from pointer target type"),
6287 G_("return discards %qv qualifier from "
6288 "pointer target type"),
6289 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6291 /* If this is not a case of ignoring a mismatch in signedness,
6292 no warning. */
6293 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6294 || target_cmp)
6296 /* If there is a mismatch, do warn. */
6297 else if (warn_pointer_sign)
6298 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6299 G_("pointer targets in passing argument "
6300 "%d of %qE differ in signedness"),
6301 G_("pointer targets in assignment "
6302 "differ in signedness"),
6303 G_("pointer targets in initialization "
6304 "differ in signedness"),
6305 G_("pointer targets in return differ "
6306 "in signedness"));
6308 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6309 && TREE_CODE (ttr) == FUNCTION_TYPE)
6311 /* Because const and volatile on functions are restrictions
6312 that say the function will not do certain things,
6313 it is okay to use a const or volatile function
6314 where an ordinary one is wanted, but not vice-versa. */
6315 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6316 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6317 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6318 OPT_Wdiscarded_qualifiers,
6319 G_("passing argument %d of %qE makes "
6320 "%q#v qualified function pointer "
6321 "from unqualified"),
6322 G_("assignment makes %q#v qualified function "
6323 "pointer from unqualified"),
6324 G_("initialization makes %q#v qualified "
6325 "function pointer from unqualified"),
6326 G_("return makes %q#v qualified function "
6327 "pointer from unqualified"),
6328 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6331 else
6332 /* Avoid warning about the volatile ObjC EH puts on decls. */
6333 if (!objc_ok)
6334 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6335 OPT_Wincompatible_pointer_types,
6336 G_("passing argument %d of %qE from "
6337 "incompatible pointer type"),
6338 G_("assignment from incompatible pointer type"),
6339 G_("initialization from incompatible "
6340 "pointer type"),
6341 G_("return from incompatible pointer type"));
6343 return convert (type, rhs);
6345 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6347 /* ??? This should not be an error when inlining calls to
6348 unprototyped functions. */
6349 error_at (location, "invalid use of non-lvalue array");
6350 return error_mark_node;
6352 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6354 /* An explicit constant 0 can convert to a pointer,
6355 or one that results from arithmetic, even including
6356 a cast to integer type. */
6357 if (!null_pointer_constant)
6358 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6359 OPT_Wint_conversion,
6360 G_("passing argument %d of %qE makes "
6361 "pointer from integer without a cast"),
6362 G_("assignment makes pointer from integer "
6363 "without a cast"),
6364 G_("initialization makes pointer from "
6365 "integer without a cast"),
6366 G_("return makes pointer from integer "
6367 "without a cast"));
6369 return convert (type, rhs);
6371 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6373 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6374 OPT_Wint_conversion,
6375 G_("passing argument %d of %qE makes integer "
6376 "from pointer without a cast"),
6377 G_("assignment makes integer from pointer "
6378 "without a cast"),
6379 G_("initialization makes integer from pointer "
6380 "without a cast"),
6381 G_("return makes integer from pointer "
6382 "without a cast"));
6383 return convert (type, rhs);
6385 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6387 tree ret;
6388 bool save = in_late_binary_op;
6389 in_late_binary_op = true;
6390 ret = convert (type, rhs);
6391 in_late_binary_op = save;
6392 return ret;
6395 switch (errtype)
6397 case ic_argpass:
6398 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6399 rname);
6400 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6401 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6402 "expected %qT but argument is of type %qT", type, rhstype);
6403 break;
6404 case ic_assign:
6405 error_at (location, "incompatible types when assigning to type %qT from "
6406 "type %qT", type, rhstype);
6407 break;
6408 case ic_init:
6409 error_at (location,
6410 "incompatible types when initializing type %qT using type %qT",
6411 type, rhstype);
6412 break;
6413 case ic_return:
6414 error_at (location,
6415 "incompatible types when returning type %qT but %qT was "
6416 "expected", rhstype, type);
6417 break;
6418 default:
6419 gcc_unreachable ();
6422 return error_mark_node;
6425 /* If VALUE is a compound expr all of whose expressions are constant, then
6426 return its value. Otherwise, return error_mark_node.
6428 This is for handling COMPOUND_EXPRs as initializer elements
6429 which is allowed with a warning when -pedantic is specified. */
6431 static tree
6432 valid_compound_expr_initializer (tree value, tree endtype)
6434 if (TREE_CODE (value) == COMPOUND_EXPR)
6436 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6437 == error_mark_node)
6438 return error_mark_node;
6439 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6440 endtype);
6442 else if (!initializer_constant_valid_p (value, endtype))
6443 return error_mark_node;
6444 else
6445 return value;
6448 /* Perform appropriate conversions on the initial value of a variable,
6449 store it in the declaration DECL,
6450 and print any error messages that are appropriate.
6451 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6452 If the init is invalid, store an ERROR_MARK.
6454 INIT_LOC is the location of the initial value. */
6456 void
6457 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6459 tree value, type;
6460 bool npc = false;
6462 /* If variable's type was invalidly declared, just ignore it. */
6464 type = TREE_TYPE (decl);
6465 if (TREE_CODE (type) == ERROR_MARK)
6466 return;
6468 /* Digest the specified initializer into an expression. */
6470 if (init)
6471 npc = null_pointer_constant_p (init);
6472 value = digest_init (init_loc, type, init, origtype, npc,
6473 true, TREE_STATIC (decl));
6475 /* Store the expression if valid; else report error. */
6477 if (!in_system_header_at (input_location)
6478 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6479 warning (OPT_Wtraditional, "traditional C rejects automatic "
6480 "aggregate initialization");
6482 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6483 DECL_INITIAL (decl) = value;
6485 /* ANSI wants warnings about out-of-range constant initializers. */
6486 STRIP_TYPE_NOPS (value);
6487 if (TREE_STATIC (decl))
6488 constant_expression_warning (value);
6490 /* Check if we need to set array size from compound literal size. */
6491 if (TREE_CODE (type) == ARRAY_TYPE
6492 && TYPE_DOMAIN (type) == 0
6493 && value != error_mark_node)
6495 tree inside_init = init;
6497 STRIP_TYPE_NOPS (inside_init);
6498 inside_init = fold (inside_init);
6500 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6502 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6504 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6506 /* For int foo[] = (int [3]){1}; we need to set array size
6507 now since later on array initializer will be just the
6508 brace enclosed list of the compound literal. */
6509 tree etype = strip_array_types (TREE_TYPE (decl));
6510 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6511 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6512 layout_type (type);
6513 layout_decl (cldecl, 0);
6514 TREE_TYPE (decl)
6515 = c_build_qualified_type (type, TYPE_QUALS (etype));
6521 /* Methods for storing and printing names for error messages. */
6523 /* Implement a spelling stack that allows components of a name to be pushed
6524 and popped. Each element on the stack is this structure. */
6526 struct spelling
6528 int kind;
6529 union
6531 unsigned HOST_WIDE_INT i;
6532 const char *s;
6533 } u;
6536 #define SPELLING_STRING 1
6537 #define SPELLING_MEMBER 2
6538 #define SPELLING_BOUNDS 3
6540 static struct spelling *spelling; /* Next stack element (unused). */
6541 static struct spelling *spelling_base; /* Spelling stack base. */
6542 static int spelling_size; /* Size of the spelling stack. */
6544 /* Macros to save and restore the spelling stack around push_... functions.
6545 Alternative to SAVE_SPELLING_STACK. */
6547 #define SPELLING_DEPTH() (spelling - spelling_base)
6548 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6550 /* Push an element on the spelling stack with type KIND and assign VALUE
6551 to MEMBER. */
6553 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6555 int depth = SPELLING_DEPTH (); \
6557 if (depth >= spelling_size) \
6559 spelling_size += 10; \
6560 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6561 spelling_size); \
6562 RESTORE_SPELLING_DEPTH (depth); \
6565 spelling->kind = (KIND); \
6566 spelling->MEMBER = (VALUE); \
6567 spelling++; \
6570 /* Push STRING on the stack. Printed literally. */
6572 static void
6573 push_string (const char *string)
6575 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6578 /* Push a member name on the stack. Printed as '.' STRING. */
6580 static void
6581 push_member_name (tree decl)
6583 const char *const string
6584 = (DECL_NAME (decl)
6585 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6586 : _("<anonymous>"));
6587 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6590 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6592 static void
6593 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6595 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6598 /* Compute the maximum size in bytes of the printed spelling. */
6600 static int
6601 spelling_length (void)
6603 int size = 0;
6604 struct spelling *p;
6606 for (p = spelling_base; p < spelling; p++)
6608 if (p->kind == SPELLING_BOUNDS)
6609 size += 25;
6610 else
6611 size += strlen (p->u.s) + 1;
6614 return size;
6617 /* Print the spelling to BUFFER and return it. */
6619 static char *
6620 print_spelling (char *buffer)
6622 char *d = buffer;
6623 struct spelling *p;
6625 for (p = spelling_base; p < spelling; p++)
6626 if (p->kind == SPELLING_BOUNDS)
6628 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6629 d += strlen (d);
6631 else
6633 const char *s;
6634 if (p->kind == SPELLING_MEMBER)
6635 *d++ = '.';
6636 for (s = p->u.s; (*d = *s++); d++)
6639 *d++ = '\0';
6640 return buffer;
6643 /* Digest the parser output INIT as an initializer for type TYPE.
6644 Return a C expression of type TYPE to represent the initial value.
6646 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6648 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6650 If INIT is a string constant, STRICT_STRING is true if it is
6651 unparenthesized or we should not warn here for it being parenthesized.
6652 For other types of INIT, STRICT_STRING is not used.
6654 INIT_LOC is the location of the INIT.
6656 REQUIRE_CONSTANT requests an error if non-constant initializers or
6657 elements are seen. */
6659 static tree
6660 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6661 bool null_pointer_constant, bool strict_string,
6662 int require_constant)
6664 enum tree_code code = TREE_CODE (type);
6665 tree inside_init = init;
6666 tree semantic_type = NULL_TREE;
6667 bool maybe_const = true;
6669 if (type == error_mark_node
6670 || !init
6671 || error_operand_p (init))
6672 return error_mark_node;
6674 STRIP_TYPE_NOPS (inside_init);
6676 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6678 semantic_type = TREE_TYPE (inside_init);
6679 inside_init = TREE_OPERAND (inside_init, 0);
6681 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6682 inside_init = decl_constant_value_for_optimization (inside_init);
6684 /* Initialization of an array of chars from a string constant
6685 optionally enclosed in braces. */
6687 if (code == ARRAY_TYPE && inside_init
6688 && TREE_CODE (inside_init) == STRING_CST)
6690 tree typ1
6691 = (TYPE_ATOMIC (TREE_TYPE (type))
6692 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6693 TYPE_QUAL_ATOMIC)
6694 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6695 /* Note that an array could be both an array of character type
6696 and an array of wchar_t if wchar_t is signed char or unsigned
6697 char. */
6698 bool char_array = (typ1 == char_type_node
6699 || typ1 == signed_char_type_node
6700 || typ1 == unsigned_char_type_node);
6701 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6702 bool char16_array = !!comptypes (typ1, char16_type_node);
6703 bool char32_array = !!comptypes (typ1, char32_type_node);
6705 if (char_array || wchar_array || char16_array || char32_array)
6707 struct c_expr expr;
6708 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6709 expr.value = inside_init;
6710 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6711 expr.original_type = NULL;
6712 maybe_warn_string_init (init_loc, type, expr);
6714 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6715 pedwarn_init (init_loc, OPT_Wpedantic,
6716 "initialization of a flexible array member");
6718 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6719 TYPE_MAIN_VARIANT (type)))
6720 return inside_init;
6722 if (char_array)
6724 if (typ2 != char_type_node)
6726 error_init (init_loc, "char-array initialized from wide "
6727 "string");
6728 return error_mark_node;
6731 else
6733 if (typ2 == char_type_node)
6735 error_init (init_loc, "wide character array initialized "
6736 "from non-wide string");
6737 return error_mark_node;
6739 else if (!comptypes(typ1, typ2))
6741 error_init (init_loc, "wide character array initialized "
6742 "from incompatible wide string");
6743 return error_mark_node;
6747 TREE_TYPE (inside_init) = type;
6748 if (TYPE_DOMAIN (type) != 0
6749 && TYPE_SIZE (type) != 0
6750 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6752 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6754 /* Subtract the size of a single (possibly wide) character
6755 because it's ok to ignore the terminating null char
6756 that is counted in the length of the constant. */
6757 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6758 (len
6759 - (TYPE_PRECISION (typ1)
6760 / BITS_PER_UNIT))))
6761 pedwarn_init (init_loc, 0,
6762 ("initializer-string for array of chars "
6763 "is too long"));
6764 else if (warn_cxx_compat
6765 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6766 warning_at (init_loc, OPT_Wc___compat,
6767 ("initializer-string for array chars "
6768 "is too long for C++"));
6771 return inside_init;
6773 else if (INTEGRAL_TYPE_P (typ1))
6775 error_init (init_loc, "array of inappropriate type initialized "
6776 "from string constant");
6777 return error_mark_node;
6781 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6782 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6783 below and handle as a constructor. */
6784 if (code == VECTOR_TYPE
6785 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
6786 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6787 && TREE_CONSTANT (inside_init))
6789 if (TREE_CODE (inside_init) == VECTOR_CST
6790 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6791 TYPE_MAIN_VARIANT (type)))
6792 return inside_init;
6794 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6796 unsigned HOST_WIDE_INT ix;
6797 tree value;
6798 bool constant_p = true;
6800 /* Iterate through elements and check if all constructor
6801 elements are *_CSTs. */
6802 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6803 if (!CONSTANT_CLASS_P (value))
6805 constant_p = false;
6806 break;
6809 if (constant_p)
6810 return build_vector_from_ctor (type,
6811 CONSTRUCTOR_ELTS (inside_init));
6815 if (warn_sequence_point)
6816 verify_sequence_points (inside_init);
6818 /* Any type can be initialized
6819 from an expression of the same type, optionally with braces. */
6821 if (inside_init && TREE_TYPE (inside_init) != 0
6822 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6823 TYPE_MAIN_VARIANT (type))
6824 || (code == ARRAY_TYPE
6825 && comptypes (TREE_TYPE (inside_init), type))
6826 || (code == VECTOR_TYPE
6827 && comptypes (TREE_TYPE (inside_init), type))
6828 || (code == POINTER_TYPE
6829 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6830 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6831 TREE_TYPE (type)))))
6833 if (code == POINTER_TYPE)
6835 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6837 if (TREE_CODE (inside_init) == STRING_CST
6838 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6839 inside_init = array_to_pointer_conversion
6840 (init_loc, inside_init);
6841 else
6843 error_init (init_loc, "invalid use of non-lvalue array");
6844 return error_mark_node;
6849 if (code == VECTOR_TYPE)
6850 /* Although the types are compatible, we may require a
6851 conversion. */
6852 inside_init = convert (type, inside_init);
6854 if (require_constant
6855 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6857 /* As an extension, allow initializing objects with static storage
6858 duration with compound literals (which are then treated just as
6859 the brace enclosed list they contain). Also allow this for
6860 vectors, as we can only assign them with compound literals. */
6861 if (flag_isoc99 && code != VECTOR_TYPE)
6862 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6863 "is not constant");
6864 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6865 inside_init = DECL_INITIAL (decl);
6868 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6869 && TREE_CODE (inside_init) != CONSTRUCTOR)
6871 error_init (init_loc, "array initialized from non-constant array "
6872 "expression");
6873 return error_mark_node;
6876 /* Compound expressions can only occur here if -Wpedantic or
6877 -pedantic-errors is specified. In the later case, we always want
6878 an error. In the former case, we simply want a warning. */
6879 if (require_constant && pedantic
6880 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6882 inside_init
6883 = valid_compound_expr_initializer (inside_init,
6884 TREE_TYPE (inside_init));
6885 if (inside_init == error_mark_node)
6886 error_init (init_loc, "initializer element is not constant");
6887 else
6888 pedwarn_init (init_loc, OPT_Wpedantic,
6889 "initializer element is not constant");
6890 if (flag_pedantic_errors)
6891 inside_init = error_mark_node;
6893 else if (require_constant
6894 && !initializer_constant_valid_p (inside_init,
6895 TREE_TYPE (inside_init)))
6897 error_init (init_loc, "initializer element is not constant");
6898 inside_init = error_mark_node;
6900 else if (require_constant && !maybe_const)
6901 pedwarn_init (init_loc, OPT_Wpedantic,
6902 "initializer element is not a constant expression");
6904 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6905 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6906 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6907 type, inside_init, origtype,
6908 ic_init, null_pointer_constant,
6909 NULL_TREE, NULL_TREE, 0);
6910 return inside_init;
6913 /* Handle scalar types, including conversions. */
6915 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6916 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6917 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6919 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6920 && (TREE_CODE (init) == STRING_CST
6921 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6922 inside_init = init = array_to_pointer_conversion (init_loc, init);
6923 if (semantic_type)
6924 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6925 inside_init);
6926 inside_init
6927 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6928 inside_init, origtype, ic_init,
6929 null_pointer_constant, NULL_TREE, NULL_TREE,
6932 /* Check to see if we have already given an error message. */
6933 if (inside_init == error_mark_node)
6935 else if (require_constant && !TREE_CONSTANT (inside_init))
6937 error_init (init_loc, "initializer element is not constant");
6938 inside_init = error_mark_node;
6940 else if (require_constant
6941 && !initializer_constant_valid_p (inside_init,
6942 TREE_TYPE (inside_init)))
6944 error_init (init_loc, "initializer element is not computable at "
6945 "load time");
6946 inside_init = error_mark_node;
6948 else if (require_constant && !maybe_const)
6949 pedwarn_init (init_loc, OPT_Wpedantic,
6950 "initializer element is not a constant expression");
6952 return inside_init;
6955 /* Come here only for records and arrays. */
6957 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6959 error_init (init_loc, "variable-sized object may not be initialized");
6960 return error_mark_node;
6963 error_init (init_loc, "invalid initializer");
6964 return error_mark_node;
6967 /* Handle initializers that use braces. */
6969 /* Type of object we are accumulating a constructor for.
6970 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6971 static tree constructor_type;
6973 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6974 left to fill. */
6975 static tree constructor_fields;
6977 /* For an ARRAY_TYPE, this is the specified index
6978 at which to store the next element we get. */
6979 static tree constructor_index;
6981 /* For an ARRAY_TYPE, this is the maximum index. */
6982 static tree constructor_max_index;
6984 /* For a RECORD_TYPE, this is the first field not yet written out. */
6985 static tree constructor_unfilled_fields;
6987 /* For an ARRAY_TYPE, this is the index of the first element
6988 not yet written out. */
6989 static tree constructor_unfilled_index;
6991 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6992 This is so we can generate gaps between fields, when appropriate. */
6993 static tree constructor_bit_index;
6995 /* If we are saving up the elements rather than allocating them,
6996 this is the list of elements so far (in reverse order,
6997 most recent first). */
6998 static vec<constructor_elt, va_gc> *constructor_elements;
7000 /* 1 if constructor should be incrementally stored into a constructor chain,
7001 0 if all the elements should be kept in AVL tree. */
7002 static int constructor_incremental;
7004 /* 1 if so far this constructor's elements are all compile-time constants. */
7005 static int constructor_constant;
7007 /* 1 if so far this constructor's elements are all valid address constants. */
7008 static int constructor_simple;
7010 /* 1 if this constructor has an element that cannot be part of a
7011 constant expression. */
7012 static int constructor_nonconst;
7014 /* 1 if this constructor is erroneous so far. */
7015 static int constructor_erroneous;
7017 /* 1 if this constructor is the universal zero initializer { 0 }. */
7018 static int constructor_zeroinit;
7020 /* Structure for managing pending initializer elements, organized as an
7021 AVL tree. */
7023 struct init_node
7025 struct init_node *left, *right;
7026 struct init_node *parent;
7027 int balance;
7028 tree purpose;
7029 tree value;
7030 tree origtype;
7033 /* Tree of pending elements at this constructor level.
7034 These are elements encountered out of order
7035 which belong at places we haven't reached yet in actually
7036 writing the output.
7037 Will never hold tree nodes across GC runs. */
7038 static struct init_node *constructor_pending_elts;
7040 /* The SPELLING_DEPTH of this constructor. */
7041 static int constructor_depth;
7043 /* DECL node for which an initializer is being read.
7044 0 means we are reading a constructor expression
7045 such as (struct foo) {...}. */
7046 static tree constructor_decl;
7048 /* Nonzero if this is an initializer for a top-level decl. */
7049 static int constructor_top_level;
7051 /* Nonzero if there were any member designators in this initializer. */
7052 static int constructor_designated;
7054 /* Nesting depth of designator list. */
7055 static int designator_depth;
7057 /* Nonzero if there were diagnosed errors in this designator list. */
7058 static int designator_erroneous;
7061 /* This stack has a level for each implicit or explicit level of
7062 structuring in the initializer, including the outermost one. It
7063 saves the values of most of the variables above. */
7065 struct constructor_range_stack;
7067 struct constructor_stack
7069 struct constructor_stack *next;
7070 tree type;
7071 tree fields;
7072 tree index;
7073 tree max_index;
7074 tree unfilled_index;
7075 tree unfilled_fields;
7076 tree bit_index;
7077 vec<constructor_elt, va_gc> *elements;
7078 struct init_node *pending_elts;
7079 int offset;
7080 int depth;
7081 /* If value nonzero, this value should replace the entire
7082 constructor at this level. */
7083 struct c_expr replacement_value;
7084 struct constructor_range_stack *range_stack;
7085 char constant;
7086 char simple;
7087 char nonconst;
7088 char implicit;
7089 char erroneous;
7090 char outer;
7091 char incremental;
7092 char designated;
7093 int designator_depth;
7096 static struct constructor_stack *constructor_stack;
7098 /* This stack represents designators from some range designator up to
7099 the last designator in the list. */
7101 struct constructor_range_stack
7103 struct constructor_range_stack *next, *prev;
7104 struct constructor_stack *stack;
7105 tree range_start;
7106 tree index;
7107 tree range_end;
7108 tree fields;
7111 static struct constructor_range_stack *constructor_range_stack;
7113 /* This stack records separate initializers that are nested.
7114 Nested initializers can't happen in ANSI C, but GNU C allows them
7115 in cases like { ... (struct foo) { ... } ... }. */
7117 struct initializer_stack
7119 struct initializer_stack *next;
7120 tree decl;
7121 struct constructor_stack *constructor_stack;
7122 struct constructor_range_stack *constructor_range_stack;
7123 vec<constructor_elt, va_gc> *elements;
7124 struct spelling *spelling;
7125 struct spelling *spelling_base;
7126 int spelling_size;
7127 char top_level;
7128 char require_constant_value;
7129 char require_constant_elements;
7132 static struct initializer_stack *initializer_stack;
7134 /* Prepare to parse and output the initializer for variable DECL. */
7136 void
7137 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7139 const char *locus;
7140 struct initializer_stack *p = XNEW (struct initializer_stack);
7142 p->decl = constructor_decl;
7143 p->require_constant_value = require_constant_value;
7144 p->require_constant_elements = require_constant_elements;
7145 p->constructor_stack = constructor_stack;
7146 p->constructor_range_stack = constructor_range_stack;
7147 p->elements = constructor_elements;
7148 p->spelling = spelling;
7149 p->spelling_base = spelling_base;
7150 p->spelling_size = spelling_size;
7151 p->top_level = constructor_top_level;
7152 p->next = initializer_stack;
7153 initializer_stack = p;
7155 constructor_decl = decl;
7156 constructor_designated = 0;
7157 constructor_top_level = top_level;
7159 if (decl != 0 && decl != error_mark_node)
7161 require_constant_value = TREE_STATIC (decl);
7162 require_constant_elements
7163 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7164 /* For a scalar, you can always use any value to initialize,
7165 even within braces. */
7166 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7167 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7169 else
7171 require_constant_value = 0;
7172 require_constant_elements = 0;
7173 locus = _("(anonymous)");
7176 constructor_stack = 0;
7177 constructor_range_stack = 0;
7179 found_missing_braces = 0;
7181 spelling_base = 0;
7182 spelling_size = 0;
7183 RESTORE_SPELLING_DEPTH (0);
7185 if (locus)
7186 push_string (locus);
7189 void
7190 finish_init (void)
7192 struct initializer_stack *p = initializer_stack;
7194 /* Free the whole constructor stack of this initializer. */
7195 while (constructor_stack)
7197 struct constructor_stack *q = constructor_stack;
7198 constructor_stack = q->next;
7199 free (q);
7202 gcc_assert (!constructor_range_stack);
7204 /* Pop back to the data of the outer initializer (if any). */
7205 free (spelling_base);
7207 constructor_decl = p->decl;
7208 require_constant_value = p->require_constant_value;
7209 require_constant_elements = p->require_constant_elements;
7210 constructor_stack = p->constructor_stack;
7211 constructor_range_stack = p->constructor_range_stack;
7212 constructor_elements = p->elements;
7213 spelling = p->spelling;
7214 spelling_base = p->spelling_base;
7215 spelling_size = p->spelling_size;
7216 constructor_top_level = p->top_level;
7217 initializer_stack = p->next;
7218 free (p);
7221 /* Call here when we see the initializer is surrounded by braces.
7222 This is instead of a call to push_init_level;
7223 it is matched by a call to pop_init_level.
7225 TYPE is the type to initialize, for a constructor expression.
7226 For an initializer for a decl, TYPE is zero. */
7228 void
7229 really_start_incremental_init (tree type)
7231 struct constructor_stack *p = XNEW (struct constructor_stack);
7233 if (type == 0)
7234 type = TREE_TYPE (constructor_decl);
7236 if (VECTOR_TYPE_P (type)
7237 && TYPE_VECTOR_OPAQUE (type))
7238 error ("opaque vector types cannot be initialized");
7240 p->type = constructor_type;
7241 p->fields = constructor_fields;
7242 p->index = constructor_index;
7243 p->max_index = constructor_max_index;
7244 p->unfilled_index = constructor_unfilled_index;
7245 p->unfilled_fields = constructor_unfilled_fields;
7246 p->bit_index = constructor_bit_index;
7247 p->elements = constructor_elements;
7248 p->constant = constructor_constant;
7249 p->simple = constructor_simple;
7250 p->nonconst = constructor_nonconst;
7251 p->erroneous = constructor_erroneous;
7252 p->pending_elts = constructor_pending_elts;
7253 p->depth = constructor_depth;
7254 p->replacement_value.value = 0;
7255 p->replacement_value.original_code = ERROR_MARK;
7256 p->replacement_value.original_type = NULL;
7257 p->implicit = 0;
7258 p->range_stack = 0;
7259 p->outer = 0;
7260 p->incremental = constructor_incremental;
7261 p->designated = constructor_designated;
7262 p->designator_depth = designator_depth;
7263 p->next = 0;
7264 constructor_stack = p;
7266 constructor_constant = 1;
7267 constructor_simple = 1;
7268 constructor_nonconst = 0;
7269 constructor_depth = SPELLING_DEPTH ();
7270 constructor_elements = NULL;
7271 constructor_pending_elts = 0;
7272 constructor_type = type;
7273 constructor_incremental = 1;
7274 constructor_designated = 0;
7275 constructor_zeroinit = 1;
7276 designator_depth = 0;
7277 designator_erroneous = 0;
7279 if (TREE_CODE (constructor_type) == RECORD_TYPE
7280 || TREE_CODE (constructor_type) == UNION_TYPE)
7282 constructor_fields = TYPE_FIELDS (constructor_type);
7283 /* Skip any nameless bit fields at the beginning. */
7284 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7285 && DECL_NAME (constructor_fields) == 0)
7286 constructor_fields = DECL_CHAIN (constructor_fields);
7288 constructor_unfilled_fields = constructor_fields;
7289 constructor_bit_index = bitsize_zero_node;
7291 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7293 if (TYPE_DOMAIN (constructor_type))
7295 constructor_max_index
7296 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7298 /* Detect non-empty initializations of zero-length arrays. */
7299 if (constructor_max_index == NULL_TREE
7300 && TYPE_SIZE (constructor_type))
7301 constructor_max_index = integer_minus_one_node;
7303 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7304 to initialize VLAs will cause a proper error; avoid tree
7305 checking errors as well by setting a safe value. */
7306 if (constructor_max_index
7307 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7308 constructor_max_index = integer_minus_one_node;
7310 constructor_index
7311 = convert (bitsizetype,
7312 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7314 else
7316 constructor_index = bitsize_zero_node;
7317 constructor_max_index = NULL_TREE;
7320 constructor_unfilled_index = constructor_index;
7322 else if (VECTOR_TYPE_P (constructor_type))
7324 /* Vectors are like simple fixed-size arrays. */
7325 constructor_max_index =
7326 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7327 constructor_index = bitsize_zero_node;
7328 constructor_unfilled_index = constructor_index;
7330 else
7332 /* Handle the case of int x = {5}; */
7333 constructor_fields = constructor_type;
7334 constructor_unfilled_fields = constructor_type;
7338 /* Push down into a subobject, for initialization.
7339 If this is for an explicit set of braces, IMPLICIT is 0.
7340 If it is because the next element belongs at a lower level,
7341 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7343 void
7344 push_init_level (location_t loc, int implicit,
7345 struct obstack *braced_init_obstack)
7347 struct constructor_stack *p;
7348 tree value = NULL_TREE;
7350 /* If we've exhausted any levels that didn't have braces,
7351 pop them now. If implicit == 1, this will have been done in
7352 process_init_element; do not repeat it here because in the case
7353 of excess initializers for an empty aggregate this leads to an
7354 infinite cycle of popping a level and immediately recreating
7355 it. */
7356 if (implicit != 1)
7358 while (constructor_stack->implicit)
7360 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7361 || TREE_CODE (constructor_type) == UNION_TYPE)
7362 && constructor_fields == 0)
7363 process_init_element (input_location,
7364 pop_init_level (loc, 1, braced_init_obstack),
7365 true, braced_init_obstack);
7366 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7367 && constructor_max_index
7368 && tree_int_cst_lt (constructor_max_index,
7369 constructor_index))
7370 process_init_element (input_location,
7371 pop_init_level (loc, 1, braced_init_obstack),
7372 true, braced_init_obstack);
7373 else
7374 break;
7378 /* Unless this is an explicit brace, we need to preserve previous
7379 content if any. */
7380 if (implicit)
7382 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7383 || TREE_CODE (constructor_type) == UNION_TYPE)
7384 && constructor_fields)
7385 value = find_init_member (constructor_fields, braced_init_obstack);
7386 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7387 value = find_init_member (constructor_index, braced_init_obstack);
7390 p = XNEW (struct constructor_stack);
7391 p->type = constructor_type;
7392 p->fields = constructor_fields;
7393 p->index = constructor_index;
7394 p->max_index = constructor_max_index;
7395 p->unfilled_index = constructor_unfilled_index;
7396 p->unfilled_fields = constructor_unfilled_fields;
7397 p->bit_index = constructor_bit_index;
7398 p->elements = constructor_elements;
7399 p->constant = constructor_constant;
7400 p->simple = constructor_simple;
7401 p->nonconst = constructor_nonconst;
7402 p->erroneous = constructor_erroneous;
7403 p->pending_elts = constructor_pending_elts;
7404 p->depth = constructor_depth;
7405 p->replacement_value.value = 0;
7406 p->replacement_value.original_code = ERROR_MARK;
7407 p->replacement_value.original_type = NULL;
7408 p->implicit = implicit;
7409 p->outer = 0;
7410 p->incremental = constructor_incremental;
7411 p->designated = constructor_designated;
7412 p->designator_depth = designator_depth;
7413 p->next = constructor_stack;
7414 p->range_stack = 0;
7415 constructor_stack = p;
7417 constructor_constant = 1;
7418 constructor_simple = 1;
7419 constructor_nonconst = 0;
7420 constructor_depth = SPELLING_DEPTH ();
7421 constructor_elements = NULL;
7422 constructor_incremental = 1;
7423 constructor_designated = 0;
7424 constructor_pending_elts = 0;
7425 if (!implicit)
7427 p->range_stack = constructor_range_stack;
7428 constructor_range_stack = 0;
7429 designator_depth = 0;
7430 designator_erroneous = 0;
7433 /* Don't die if an entire brace-pair level is superfluous
7434 in the containing level. */
7435 if (constructor_type == 0)
7437 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7438 || TREE_CODE (constructor_type) == UNION_TYPE)
7440 /* Don't die if there are extra init elts at the end. */
7441 if (constructor_fields == 0)
7442 constructor_type = 0;
7443 else
7445 constructor_type = TREE_TYPE (constructor_fields);
7446 push_member_name (constructor_fields);
7447 constructor_depth++;
7449 /* If upper initializer is designated, then mark this as
7450 designated too to prevent bogus warnings. */
7451 constructor_designated = p->designated;
7453 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7455 constructor_type = TREE_TYPE (constructor_type);
7456 push_array_bounds (tree_to_uhwi (constructor_index));
7457 constructor_depth++;
7460 if (constructor_type == 0)
7462 error_init (loc, "extra brace group at end of initializer");
7463 constructor_fields = 0;
7464 constructor_unfilled_fields = 0;
7465 return;
7468 if (value && TREE_CODE (value) == CONSTRUCTOR)
7470 constructor_constant = TREE_CONSTANT (value);
7471 constructor_simple = TREE_STATIC (value);
7472 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7473 constructor_elements = CONSTRUCTOR_ELTS (value);
7474 if (!vec_safe_is_empty (constructor_elements)
7475 && (TREE_CODE (constructor_type) == RECORD_TYPE
7476 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7477 set_nonincremental_init (braced_init_obstack);
7480 if (implicit == 1)
7481 found_missing_braces = 1;
7483 if (TREE_CODE (constructor_type) == RECORD_TYPE
7484 || TREE_CODE (constructor_type) == UNION_TYPE)
7486 constructor_fields = TYPE_FIELDS (constructor_type);
7487 /* Skip any nameless bit fields at the beginning. */
7488 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7489 && DECL_NAME (constructor_fields) == 0)
7490 constructor_fields = DECL_CHAIN (constructor_fields);
7492 constructor_unfilled_fields = constructor_fields;
7493 constructor_bit_index = bitsize_zero_node;
7495 else if (VECTOR_TYPE_P (constructor_type))
7497 /* Vectors are like simple fixed-size arrays. */
7498 constructor_max_index =
7499 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7500 constructor_index = bitsize_int (0);
7501 constructor_unfilled_index = constructor_index;
7503 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7505 if (TYPE_DOMAIN (constructor_type))
7507 constructor_max_index
7508 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7510 /* Detect non-empty initializations of zero-length arrays. */
7511 if (constructor_max_index == NULL_TREE
7512 && TYPE_SIZE (constructor_type))
7513 constructor_max_index = integer_minus_one_node;
7515 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7516 to initialize VLAs will cause a proper error; avoid tree
7517 checking errors as well by setting a safe value. */
7518 if (constructor_max_index
7519 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7520 constructor_max_index = integer_minus_one_node;
7522 constructor_index
7523 = convert (bitsizetype,
7524 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7526 else
7527 constructor_index = bitsize_zero_node;
7529 constructor_unfilled_index = constructor_index;
7530 if (value && TREE_CODE (value) == STRING_CST)
7532 /* We need to split the char/wchar array into individual
7533 characters, so that we don't have to special case it
7534 everywhere. */
7535 set_nonincremental_init_from_string (value, braced_init_obstack);
7538 else
7540 if (constructor_type != error_mark_node)
7541 warning_init (input_location, 0, "braces around scalar initializer");
7542 constructor_fields = constructor_type;
7543 constructor_unfilled_fields = constructor_type;
7547 /* At the end of an implicit or explicit brace level,
7548 finish up that level of constructor. If a single expression
7549 with redundant braces initialized that level, return the
7550 c_expr structure for that expression. Otherwise, the original_code
7551 element is set to ERROR_MARK.
7552 If we were outputting the elements as they are read, return 0 as the value
7553 from inner levels (process_init_element ignores that),
7554 but return error_mark_node as the value from the outermost level
7555 (that's what we want to put in DECL_INITIAL).
7556 Otherwise, return a CONSTRUCTOR expression as the value. */
7558 struct c_expr
7559 pop_init_level (location_t loc, int implicit,
7560 struct obstack *braced_init_obstack)
7562 struct constructor_stack *p;
7563 struct c_expr ret;
7564 ret.value = 0;
7565 ret.original_code = ERROR_MARK;
7566 ret.original_type = NULL;
7568 if (implicit == 0)
7570 /* When we come to an explicit close brace,
7571 pop any inner levels that didn't have explicit braces. */
7572 while (constructor_stack->implicit)
7573 process_init_element (input_location,
7574 pop_init_level (loc, 1, braced_init_obstack),
7575 true, braced_init_obstack);
7576 gcc_assert (!constructor_range_stack);
7579 /* Now output all pending elements. */
7580 constructor_incremental = 1;
7581 output_pending_init_elements (1, braced_init_obstack);
7583 p = constructor_stack;
7585 /* Error for initializing a flexible array member, or a zero-length
7586 array member in an inappropriate context. */
7587 if (constructor_type && constructor_fields
7588 && TREE_CODE (constructor_type) == ARRAY_TYPE
7589 && TYPE_DOMAIN (constructor_type)
7590 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7592 /* Silently discard empty initializations. The parser will
7593 already have pedwarned for empty brackets. */
7594 if (integer_zerop (constructor_unfilled_index))
7595 constructor_type = NULL_TREE;
7596 else
7598 gcc_assert (!TYPE_SIZE (constructor_type));
7600 if (constructor_depth > 2)
7601 error_init (loc, "initialization of flexible array member in a nested context");
7602 else
7603 pedwarn_init (loc, OPT_Wpedantic,
7604 "initialization of a flexible array member");
7606 /* We have already issued an error message for the existence
7607 of a flexible array member not at the end of the structure.
7608 Discard the initializer so that we do not die later. */
7609 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7610 constructor_type = NULL_TREE;
7614 switch (vec_safe_length (constructor_elements))
7616 case 0:
7617 /* Initialization with { } counts as zeroinit. */
7618 constructor_zeroinit = 1;
7619 break;
7620 case 1:
7621 /* This might be zeroinit as well. */
7622 if (integer_zerop ((*constructor_elements)[0].value))
7623 constructor_zeroinit = 1;
7624 break;
7625 default:
7626 /* If the constructor has more than one element, it can't be { 0 }. */
7627 constructor_zeroinit = 0;
7628 break;
7631 /* Warn when some structs are initialized with direct aggregation. */
7632 if (!implicit && found_missing_braces && warn_missing_braces
7633 && !constructor_zeroinit)
7634 warning_init (loc, OPT_Wmissing_braces,
7635 "missing braces around initializer");
7637 /* Warn when some struct elements are implicitly initialized to zero. */
7638 if (warn_missing_field_initializers
7639 && constructor_type
7640 && TREE_CODE (constructor_type) == RECORD_TYPE
7641 && constructor_unfilled_fields)
7643 /* Do not warn for flexible array members or zero-length arrays. */
7644 while (constructor_unfilled_fields
7645 && (!DECL_SIZE (constructor_unfilled_fields)
7646 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7647 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7649 if (constructor_unfilled_fields
7650 /* Do not warn if this level of the initializer uses member
7651 designators; it is likely to be deliberate. */
7652 && !constructor_designated
7653 /* Do not warn about initializing with { 0 } or with { }. */
7654 && !constructor_zeroinit)
7656 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7657 "missing initializer for field %qD of %qT",
7658 constructor_unfilled_fields,
7659 constructor_type))
7660 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7661 "%qD declared here", constructor_unfilled_fields);
7665 /* Pad out the end of the structure. */
7666 if (p->replacement_value.value)
7667 /* If this closes a superfluous brace pair,
7668 just pass out the element between them. */
7669 ret = p->replacement_value;
7670 else if (constructor_type == 0)
7672 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7673 && TREE_CODE (constructor_type) != UNION_TYPE
7674 && TREE_CODE (constructor_type) != ARRAY_TYPE
7675 && !VECTOR_TYPE_P (constructor_type))
7677 /* A nonincremental scalar initializer--just return
7678 the element, after verifying there is just one. */
7679 if (vec_safe_is_empty (constructor_elements))
7681 if (!constructor_erroneous)
7682 error_init (loc, "empty scalar initializer");
7683 ret.value = error_mark_node;
7685 else if (vec_safe_length (constructor_elements) != 1)
7687 error_init (loc, "extra elements in scalar initializer");
7688 ret.value = (*constructor_elements)[0].value;
7690 else
7691 ret.value = (*constructor_elements)[0].value;
7693 else
7695 if (constructor_erroneous)
7696 ret.value = error_mark_node;
7697 else
7699 ret.value = build_constructor (constructor_type,
7700 constructor_elements);
7701 if (constructor_constant)
7702 TREE_CONSTANT (ret.value) = 1;
7703 if (constructor_constant && constructor_simple)
7704 TREE_STATIC (ret.value) = 1;
7705 if (constructor_nonconst)
7706 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7710 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7712 if (constructor_nonconst)
7713 ret.original_code = C_MAYBE_CONST_EXPR;
7714 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7715 ret.original_code = ERROR_MARK;
7718 constructor_type = p->type;
7719 constructor_fields = p->fields;
7720 constructor_index = p->index;
7721 constructor_max_index = p->max_index;
7722 constructor_unfilled_index = p->unfilled_index;
7723 constructor_unfilled_fields = p->unfilled_fields;
7724 constructor_bit_index = p->bit_index;
7725 constructor_elements = p->elements;
7726 constructor_constant = p->constant;
7727 constructor_simple = p->simple;
7728 constructor_nonconst = p->nonconst;
7729 constructor_erroneous = p->erroneous;
7730 constructor_incremental = p->incremental;
7731 constructor_designated = p->designated;
7732 designator_depth = p->designator_depth;
7733 constructor_pending_elts = p->pending_elts;
7734 constructor_depth = p->depth;
7735 if (!p->implicit)
7736 constructor_range_stack = p->range_stack;
7737 RESTORE_SPELLING_DEPTH (constructor_depth);
7739 constructor_stack = p->next;
7740 free (p);
7742 if (ret.value == 0 && constructor_stack == 0)
7743 ret.value = error_mark_node;
7744 return ret;
7747 /* Common handling for both array range and field name designators.
7748 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7750 static int
7751 set_designator (location_t loc, int array,
7752 struct obstack *braced_init_obstack)
7754 tree subtype;
7755 enum tree_code subcode;
7757 /* Don't die if an entire brace-pair level is superfluous
7758 in the containing level. */
7759 if (constructor_type == 0)
7760 return 1;
7762 /* If there were errors in this designator list already, bail out
7763 silently. */
7764 if (designator_erroneous)
7765 return 1;
7767 if (!designator_depth)
7769 gcc_assert (!constructor_range_stack);
7771 /* Designator list starts at the level of closest explicit
7772 braces. */
7773 while (constructor_stack->implicit)
7774 process_init_element (input_location,
7775 pop_init_level (loc, 1, braced_init_obstack),
7776 true, braced_init_obstack);
7777 constructor_designated = 1;
7778 return 0;
7781 switch (TREE_CODE (constructor_type))
7783 case RECORD_TYPE:
7784 case UNION_TYPE:
7785 subtype = TREE_TYPE (constructor_fields);
7786 if (subtype != error_mark_node)
7787 subtype = TYPE_MAIN_VARIANT (subtype);
7788 break;
7789 case ARRAY_TYPE:
7790 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7791 break;
7792 default:
7793 gcc_unreachable ();
7796 subcode = TREE_CODE (subtype);
7797 if (array && subcode != ARRAY_TYPE)
7799 error_init (loc, "array index in non-array initializer");
7800 return 1;
7802 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7804 error_init (loc, "field name not in record or union initializer");
7805 return 1;
7808 constructor_designated = 1;
7809 push_init_level (loc, 2, braced_init_obstack);
7810 return 0;
7813 /* If there are range designators in designator list, push a new designator
7814 to constructor_range_stack. RANGE_END is end of such stack range or
7815 NULL_TREE if there is no range designator at this level. */
7817 static void
7818 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7820 struct constructor_range_stack *p;
7822 p = (struct constructor_range_stack *)
7823 obstack_alloc (braced_init_obstack,
7824 sizeof (struct constructor_range_stack));
7825 p->prev = constructor_range_stack;
7826 p->next = 0;
7827 p->fields = constructor_fields;
7828 p->range_start = constructor_index;
7829 p->index = constructor_index;
7830 p->stack = constructor_stack;
7831 p->range_end = range_end;
7832 if (constructor_range_stack)
7833 constructor_range_stack->next = p;
7834 constructor_range_stack = p;
7837 /* Within an array initializer, specify the next index to be initialized.
7838 FIRST is that index. If LAST is nonzero, then initialize a range
7839 of indices, running from FIRST through LAST. */
7841 void
7842 set_init_index (location_t loc, tree first, tree last,
7843 struct obstack *braced_init_obstack)
7845 if (set_designator (loc, 1, braced_init_obstack))
7846 return;
7848 designator_erroneous = 1;
7850 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7851 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7853 error_init (loc, "array index in initializer not of integer type");
7854 return;
7857 if (TREE_CODE (first) != INTEGER_CST)
7859 first = c_fully_fold (first, false, NULL);
7860 if (TREE_CODE (first) == INTEGER_CST)
7861 pedwarn_init (loc, OPT_Wpedantic,
7862 "array index in initializer is not "
7863 "an integer constant expression");
7866 if (last && TREE_CODE (last) != INTEGER_CST)
7868 last = c_fully_fold (last, false, NULL);
7869 if (TREE_CODE (last) == INTEGER_CST)
7870 pedwarn_init (loc, OPT_Wpedantic,
7871 "array index in initializer is not "
7872 "an integer constant expression");
7875 if (TREE_CODE (first) != INTEGER_CST)
7876 error_init (loc, "nonconstant array index in initializer");
7877 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7878 error_init (loc, "nonconstant array index in initializer");
7879 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7880 error_init (loc, "array index in non-array initializer");
7881 else if (tree_int_cst_sgn (first) == -1)
7882 error_init (loc, "array index in initializer exceeds array bounds");
7883 else if (constructor_max_index
7884 && tree_int_cst_lt (constructor_max_index, first))
7885 error_init (loc, "array index in initializer exceeds array bounds");
7886 else
7888 constant_expression_warning (first);
7889 if (last)
7890 constant_expression_warning (last);
7891 constructor_index = convert (bitsizetype, first);
7892 if (tree_int_cst_lt (constructor_index, first))
7894 constructor_index = copy_node (constructor_index);
7895 TREE_OVERFLOW (constructor_index) = 1;
7898 if (last)
7900 if (tree_int_cst_equal (first, last))
7901 last = 0;
7902 else if (tree_int_cst_lt (last, first))
7904 error_init (loc, "empty index range in initializer");
7905 last = 0;
7907 else
7909 last = convert (bitsizetype, last);
7910 if (constructor_max_index != 0
7911 && tree_int_cst_lt (constructor_max_index, last))
7913 error_init (loc, "array index range in initializer exceeds "
7914 "array bounds");
7915 last = 0;
7920 designator_depth++;
7921 designator_erroneous = 0;
7922 if (constructor_range_stack || last)
7923 push_range_stack (last, braced_init_obstack);
7927 /* Within a struct initializer, specify the next field to be initialized. */
7929 void
7930 set_init_label (location_t loc, tree fieldname,
7931 struct obstack *braced_init_obstack)
7933 tree field;
7935 if (set_designator (loc, 0, braced_init_obstack))
7936 return;
7938 designator_erroneous = 1;
7940 if (TREE_CODE (constructor_type) != RECORD_TYPE
7941 && TREE_CODE (constructor_type) != UNION_TYPE)
7943 error_init (loc, "field name not in record or union initializer");
7944 return;
7947 field = lookup_field (constructor_type, fieldname);
7949 if (field == 0)
7950 error_at (loc, "unknown field %qE specified in initializer", fieldname);
7951 else
7954 constructor_fields = TREE_VALUE (field);
7955 designator_depth++;
7956 designator_erroneous = 0;
7957 if (constructor_range_stack)
7958 push_range_stack (NULL_TREE, braced_init_obstack);
7959 field = TREE_CHAIN (field);
7960 if (field)
7962 if (set_designator (loc, 0, braced_init_obstack))
7963 return;
7966 while (field != NULL_TREE);
7969 /* Add a new initializer to the tree of pending initializers. PURPOSE
7970 identifies the initializer, either array index or field in a structure.
7971 VALUE is the value of that index or field. If ORIGTYPE is not
7972 NULL_TREE, it is the original type of VALUE.
7974 IMPLICIT is true if value comes from pop_init_level (1),
7975 the new initializer has been merged with the existing one
7976 and thus no warnings should be emitted about overriding an
7977 existing initializer. */
7979 static void
7980 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7981 bool implicit, struct obstack *braced_init_obstack)
7983 struct init_node *p, **q, *r;
7985 q = &constructor_pending_elts;
7986 p = 0;
7988 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7990 while (*q != 0)
7992 p = *q;
7993 if (tree_int_cst_lt (purpose, p->purpose))
7994 q = &p->left;
7995 else if (tree_int_cst_lt (p->purpose, purpose))
7996 q = &p->right;
7997 else
7999 if (!implicit)
8001 if (TREE_SIDE_EFFECTS (p->value))
8002 warning_init (loc, OPT_Woverride_init_side_effects,
8003 "initialized field with side-effects "
8004 "overwritten");
8005 else if (warn_override_init)
8006 warning_init (loc, OPT_Woverride_init,
8007 "initialized field overwritten");
8009 p->value = value;
8010 p->origtype = origtype;
8011 return;
8015 else
8017 tree bitpos;
8019 bitpos = bit_position (purpose);
8020 while (*q != NULL)
8022 p = *q;
8023 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8024 q = &p->left;
8025 else if (p->purpose != purpose)
8026 q = &p->right;
8027 else
8029 if (!implicit)
8031 if (TREE_SIDE_EFFECTS (p->value))
8032 warning_init (loc, OPT_Woverride_init_side_effects,
8033 "initialized field with side-effects "
8034 "overwritten");
8035 else if (warn_override_init)
8036 warning_init (loc, OPT_Woverride_init,
8037 "initialized field overwritten");
8039 p->value = value;
8040 p->origtype = origtype;
8041 return;
8046 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8047 sizeof (struct init_node));
8048 r->purpose = purpose;
8049 r->value = value;
8050 r->origtype = origtype;
8052 *q = r;
8053 r->parent = p;
8054 r->left = 0;
8055 r->right = 0;
8056 r->balance = 0;
8058 while (p)
8060 struct init_node *s;
8062 if (r == p->left)
8064 if (p->balance == 0)
8065 p->balance = -1;
8066 else if (p->balance < 0)
8068 if (r->balance < 0)
8070 /* L rotation. */
8071 p->left = r->right;
8072 if (p->left)
8073 p->left->parent = p;
8074 r->right = p;
8076 p->balance = 0;
8077 r->balance = 0;
8079 s = p->parent;
8080 p->parent = r;
8081 r->parent = s;
8082 if (s)
8084 if (s->left == p)
8085 s->left = r;
8086 else
8087 s->right = r;
8089 else
8090 constructor_pending_elts = r;
8092 else
8094 /* LR rotation. */
8095 struct init_node *t = r->right;
8097 r->right = t->left;
8098 if (r->right)
8099 r->right->parent = r;
8100 t->left = r;
8102 p->left = t->right;
8103 if (p->left)
8104 p->left->parent = p;
8105 t->right = p;
8107 p->balance = t->balance < 0;
8108 r->balance = -(t->balance > 0);
8109 t->balance = 0;
8111 s = p->parent;
8112 p->parent = t;
8113 r->parent = t;
8114 t->parent = s;
8115 if (s)
8117 if (s->left == p)
8118 s->left = t;
8119 else
8120 s->right = t;
8122 else
8123 constructor_pending_elts = t;
8125 break;
8127 else
8129 /* p->balance == +1; growth of left side balances the node. */
8130 p->balance = 0;
8131 break;
8134 else /* r == p->right */
8136 if (p->balance == 0)
8137 /* Growth propagation from right side. */
8138 p->balance++;
8139 else if (p->balance > 0)
8141 if (r->balance > 0)
8143 /* R rotation. */
8144 p->right = r->left;
8145 if (p->right)
8146 p->right->parent = p;
8147 r->left = p;
8149 p->balance = 0;
8150 r->balance = 0;
8152 s = p->parent;
8153 p->parent = r;
8154 r->parent = s;
8155 if (s)
8157 if (s->left == p)
8158 s->left = r;
8159 else
8160 s->right = r;
8162 else
8163 constructor_pending_elts = r;
8165 else /* r->balance == -1 */
8167 /* RL rotation */
8168 struct init_node *t = r->left;
8170 r->left = t->right;
8171 if (r->left)
8172 r->left->parent = r;
8173 t->right = r;
8175 p->right = t->left;
8176 if (p->right)
8177 p->right->parent = p;
8178 t->left = p;
8180 r->balance = (t->balance < 0);
8181 p->balance = -(t->balance > 0);
8182 t->balance = 0;
8184 s = p->parent;
8185 p->parent = t;
8186 r->parent = t;
8187 t->parent = s;
8188 if (s)
8190 if (s->left == p)
8191 s->left = t;
8192 else
8193 s->right = t;
8195 else
8196 constructor_pending_elts = t;
8198 break;
8200 else
8202 /* p->balance == -1; growth of right side balances the node. */
8203 p->balance = 0;
8204 break;
8208 r = p;
8209 p = p->parent;
8213 /* Build AVL tree from a sorted chain. */
8215 static void
8216 set_nonincremental_init (struct obstack * braced_init_obstack)
8218 unsigned HOST_WIDE_INT ix;
8219 tree index, value;
8221 if (TREE_CODE (constructor_type) != RECORD_TYPE
8222 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8223 return;
8225 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8226 add_pending_init (input_location, index, value, NULL_TREE, true,
8227 braced_init_obstack);
8228 constructor_elements = NULL;
8229 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8231 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8232 /* Skip any nameless bit fields at the beginning. */
8233 while (constructor_unfilled_fields != 0
8234 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8235 && DECL_NAME (constructor_unfilled_fields) == 0)
8236 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8239 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8241 if (TYPE_DOMAIN (constructor_type))
8242 constructor_unfilled_index
8243 = convert (bitsizetype,
8244 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8245 else
8246 constructor_unfilled_index = bitsize_zero_node;
8248 constructor_incremental = 0;
8251 /* Build AVL tree from a string constant. */
8253 static void
8254 set_nonincremental_init_from_string (tree str,
8255 struct obstack * braced_init_obstack)
8257 tree value, purpose, type;
8258 HOST_WIDE_INT val[2];
8259 const char *p, *end;
8260 int byte, wchar_bytes, charwidth, bitpos;
8262 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8264 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8265 charwidth = TYPE_PRECISION (char_type_node);
8266 type = TREE_TYPE (constructor_type);
8267 p = TREE_STRING_POINTER (str);
8268 end = p + TREE_STRING_LENGTH (str);
8270 for (purpose = bitsize_zero_node;
8271 p < end
8272 && !(constructor_max_index
8273 && tree_int_cst_lt (constructor_max_index, purpose));
8274 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8276 if (wchar_bytes == 1)
8278 val[0] = (unsigned char) *p++;
8279 val[1] = 0;
8281 else
8283 val[1] = 0;
8284 val[0] = 0;
8285 for (byte = 0; byte < wchar_bytes; byte++)
8287 if (BYTES_BIG_ENDIAN)
8288 bitpos = (wchar_bytes - byte - 1) * charwidth;
8289 else
8290 bitpos = byte * charwidth;
8291 val[bitpos % HOST_BITS_PER_WIDE_INT]
8292 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8293 << (bitpos % HOST_BITS_PER_WIDE_INT);
8297 if (!TYPE_UNSIGNED (type))
8299 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8300 if (bitpos < HOST_BITS_PER_WIDE_INT)
8302 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8304 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8305 val[1] = -1;
8308 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8310 if (val[0] < 0)
8311 val[1] = -1;
8313 else if (val[1] & (((HOST_WIDE_INT) 1)
8314 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8315 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8318 value = wide_int_to_tree (type,
8319 wide_int::from_array (val, 2,
8320 HOST_BITS_PER_WIDE_INT * 2));
8321 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8322 braced_init_obstack);
8325 constructor_incremental = 0;
8328 /* Return value of FIELD in pending initializer or zero if the field was
8329 not initialized yet. */
8331 static tree
8332 find_init_member (tree field, struct obstack * braced_init_obstack)
8334 struct init_node *p;
8336 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8338 if (constructor_incremental
8339 && tree_int_cst_lt (field, constructor_unfilled_index))
8340 set_nonincremental_init (braced_init_obstack);
8342 p = constructor_pending_elts;
8343 while (p)
8345 if (tree_int_cst_lt (field, p->purpose))
8346 p = p->left;
8347 else if (tree_int_cst_lt (p->purpose, field))
8348 p = p->right;
8349 else
8350 return p->value;
8353 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8355 tree bitpos = bit_position (field);
8357 if (constructor_incremental
8358 && (!constructor_unfilled_fields
8359 || tree_int_cst_lt (bitpos,
8360 bit_position (constructor_unfilled_fields))))
8361 set_nonincremental_init (braced_init_obstack);
8363 p = constructor_pending_elts;
8364 while (p)
8366 if (field == p->purpose)
8367 return p->value;
8368 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8369 p = p->left;
8370 else
8371 p = p->right;
8374 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8376 if (!vec_safe_is_empty (constructor_elements)
8377 && (constructor_elements->last ().index == field))
8378 return constructor_elements->last ().value;
8380 return 0;
8383 /* "Output" the next constructor element.
8384 At top level, really output it to assembler code now.
8385 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8386 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8387 TYPE is the data type that the containing data type wants here.
8388 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8389 If VALUE is a string constant, STRICT_STRING is true if it is
8390 unparenthesized or we should not warn here for it being parenthesized.
8391 For other types of VALUE, STRICT_STRING is not used.
8393 PENDING if non-nil means output pending elements that belong
8394 right after this element. (PENDING is normally 1;
8395 it is 0 while outputting pending elements, to avoid recursion.)
8397 IMPLICIT is true if value comes from pop_init_level (1),
8398 the new initializer has been merged with the existing one
8399 and thus no warnings should be emitted about overriding an
8400 existing initializer. */
8402 static void
8403 output_init_element (location_t loc, tree value, tree origtype,
8404 bool strict_string, tree type, tree field, int pending,
8405 bool implicit, struct obstack * braced_init_obstack)
8407 tree semantic_type = NULL_TREE;
8408 bool maybe_const = true;
8409 bool npc;
8411 if (type == error_mark_node || value == error_mark_node)
8413 constructor_erroneous = 1;
8414 return;
8416 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8417 && (TREE_CODE (value) == STRING_CST
8418 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8419 && !(TREE_CODE (value) == STRING_CST
8420 && TREE_CODE (type) == ARRAY_TYPE
8421 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8422 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8423 TYPE_MAIN_VARIANT (type)))
8424 value = array_to_pointer_conversion (input_location, value);
8426 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8427 && require_constant_value && pending)
8429 /* As an extension, allow initializing objects with static storage
8430 duration with compound literals (which are then treated just as
8431 the brace enclosed list they contain). */
8432 if (flag_isoc99)
8433 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8434 "constant");
8435 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8436 value = DECL_INITIAL (decl);
8439 npc = null_pointer_constant_p (value);
8440 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8442 semantic_type = TREE_TYPE (value);
8443 value = TREE_OPERAND (value, 0);
8445 value = c_fully_fold (value, require_constant_value, &maybe_const);
8447 if (value == error_mark_node)
8448 constructor_erroneous = 1;
8449 else if (!TREE_CONSTANT (value))
8450 constructor_constant = 0;
8451 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8452 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8453 || TREE_CODE (constructor_type) == UNION_TYPE)
8454 && DECL_C_BIT_FIELD (field)
8455 && TREE_CODE (value) != INTEGER_CST))
8456 constructor_simple = 0;
8457 if (!maybe_const)
8458 constructor_nonconst = 1;
8460 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8462 if (require_constant_value)
8464 error_init (loc, "initializer element is not constant");
8465 value = error_mark_node;
8467 else if (require_constant_elements)
8468 pedwarn (loc, OPT_Wpedantic,
8469 "initializer element is not computable at load time");
8471 else if (!maybe_const
8472 && (require_constant_value || require_constant_elements))
8473 pedwarn_init (loc, OPT_Wpedantic,
8474 "initializer element is not a constant expression");
8476 /* Issue -Wc++-compat warnings about initializing a bitfield with
8477 enum type. */
8478 if (warn_cxx_compat
8479 && field != NULL_TREE
8480 && TREE_CODE (field) == FIELD_DECL
8481 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8482 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8483 != TYPE_MAIN_VARIANT (type))
8484 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8486 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8487 if (checktype != error_mark_node
8488 && (TYPE_MAIN_VARIANT (checktype)
8489 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8490 warning_init (loc, OPT_Wc___compat,
8491 "enum conversion in initialization is invalid in C++");
8494 /* If this field is empty (and not at the end of structure),
8495 don't do anything other than checking the initializer. */
8496 if (field
8497 && (TREE_TYPE (field) == error_mark_node
8498 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8499 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8500 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8501 || DECL_CHAIN (field)))))
8502 return;
8504 if (semantic_type)
8505 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8506 value = digest_init (loc, type, value, origtype, npc, strict_string,
8507 require_constant_value);
8508 if (value == error_mark_node)
8510 constructor_erroneous = 1;
8511 return;
8513 if (require_constant_value || require_constant_elements)
8514 constant_expression_warning (value);
8516 /* If this element doesn't come next in sequence,
8517 put it on constructor_pending_elts. */
8518 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8519 && (!constructor_incremental
8520 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8522 if (constructor_incremental
8523 && tree_int_cst_lt (field, constructor_unfilled_index))
8524 set_nonincremental_init (braced_init_obstack);
8526 add_pending_init (loc, field, value, origtype, implicit,
8527 braced_init_obstack);
8528 return;
8530 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8531 && (!constructor_incremental
8532 || field != constructor_unfilled_fields))
8534 /* We do this for records but not for unions. In a union,
8535 no matter which field is specified, it can be initialized
8536 right away since it starts at the beginning of the union. */
8537 if (constructor_incremental)
8539 if (!constructor_unfilled_fields)
8540 set_nonincremental_init (braced_init_obstack);
8541 else
8543 tree bitpos, unfillpos;
8545 bitpos = bit_position (field);
8546 unfillpos = bit_position (constructor_unfilled_fields);
8548 if (tree_int_cst_lt (bitpos, unfillpos))
8549 set_nonincremental_init (braced_init_obstack);
8553 add_pending_init (loc, field, value, origtype, implicit,
8554 braced_init_obstack);
8555 return;
8557 else if (TREE_CODE (constructor_type) == UNION_TYPE
8558 && !vec_safe_is_empty (constructor_elements))
8560 if (!implicit)
8562 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8563 warning_init (loc, OPT_Woverride_init_side_effects,
8564 "initialized field with side-effects overwritten");
8565 else if (warn_override_init)
8566 warning_init (loc, OPT_Woverride_init,
8567 "initialized field overwritten");
8570 /* We can have just one union field set. */
8571 constructor_elements = NULL;
8574 /* Otherwise, output this element either to
8575 constructor_elements or to the assembler file. */
8577 constructor_elt celt = {field, value};
8578 vec_safe_push (constructor_elements, celt);
8580 /* Advance the variable that indicates sequential elements output. */
8581 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8582 constructor_unfilled_index
8583 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8584 bitsize_one_node);
8585 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8587 constructor_unfilled_fields
8588 = DECL_CHAIN (constructor_unfilled_fields);
8590 /* Skip any nameless bit fields. */
8591 while (constructor_unfilled_fields != 0
8592 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8593 && DECL_NAME (constructor_unfilled_fields) == 0)
8594 constructor_unfilled_fields =
8595 DECL_CHAIN (constructor_unfilled_fields);
8597 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8598 constructor_unfilled_fields = 0;
8600 /* Now output any pending elements which have become next. */
8601 if (pending)
8602 output_pending_init_elements (0, braced_init_obstack);
8605 /* Output any pending elements which have become next.
8606 As we output elements, constructor_unfilled_{fields,index}
8607 advances, which may cause other elements to become next;
8608 if so, they too are output.
8610 If ALL is 0, we return when there are
8611 no more pending elements to output now.
8613 If ALL is 1, we output space as necessary so that
8614 we can output all the pending elements. */
8615 static void
8616 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8618 struct init_node *elt = constructor_pending_elts;
8619 tree next;
8621 retry:
8623 /* Look through the whole pending tree.
8624 If we find an element that should be output now,
8625 output it. Otherwise, set NEXT to the element
8626 that comes first among those still pending. */
8628 next = 0;
8629 while (elt)
8631 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8633 if (tree_int_cst_equal (elt->purpose,
8634 constructor_unfilled_index))
8635 output_init_element (input_location, elt->value, elt->origtype,
8636 true, TREE_TYPE (constructor_type),
8637 constructor_unfilled_index, 0, false,
8638 braced_init_obstack);
8639 else if (tree_int_cst_lt (constructor_unfilled_index,
8640 elt->purpose))
8642 /* Advance to the next smaller node. */
8643 if (elt->left)
8644 elt = elt->left;
8645 else
8647 /* We have reached the smallest node bigger than the
8648 current unfilled index. Fill the space first. */
8649 next = elt->purpose;
8650 break;
8653 else
8655 /* Advance to the next bigger node. */
8656 if (elt->right)
8657 elt = elt->right;
8658 else
8660 /* We have reached the biggest node in a subtree. Find
8661 the parent of it, which is the next bigger node. */
8662 while (elt->parent && elt->parent->right == elt)
8663 elt = elt->parent;
8664 elt = elt->parent;
8665 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8666 elt->purpose))
8668 next = elt->purpose;
8669 break;
8674 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8675 || TREE_CODE (constructor_type) == UNION_TYPE)
8677 tree ctor_unfilled_bitpos, elt_bitpos;
8679 /* If the current record is complete we are done. */
8680 if (constructor_unfilled_fields == 0)
8681 break;
8683 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8684 elt_bitpos = bit_position (elt->purpose);
8685 /* We can't compare fields here because there might be empty
8686 fields in between. */
8687 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8689 constructor_unfilled_fields = elt->purpose;
8690 output_init_element (input_location, elt->value, elt->origtype,
8691 true, TREE_TYPE (elt->purpose),
8692 elt->purpose, 0, false,
8693 braced_init_obstack);
8695 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8697 /* Advance to the next smaller node. */
8698 if (elt->left)
8699 elt = elt->left;
8700 else
8702 /* We have reached the smallest node bigger than the
8703 current unfilled field. Fill the space first. */
8704 next = elt->purpose;
8705 break;
8708 else
8710 /* Advance to the next bigger node. */
8711 if (elt->right)
8712 elt = elt->right;
8713 else
8715 /* We have reached the biggest node in a subtree. Find
8716 the parent of it, which is the next bigger node. */
8717 while (elt->parent && elt->parent->right == elt)
8718 elt = elt->parent;
8719 elt = elt->parent;
8720 if (elt
8721 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8722 bit_position (elt->purpose))))
8724 next = elt->purpose;
8725 break;
8732 /* Ordinarily return, but not if we want to output all
8733 and there are elements left. */
8734 if (!(all && next != 0))
8735 return;
8737 /* If it's not incremental, just skip over the gap, so that after
8738 jumping to retry we will output the next successive element. */
8739 if (TREE_CODE (constructor_type) == RECORD_TYPE
8740 || TREE_CODE (constructor_type) == UNION_TYPE)
8741 constructor_unfilled_fields = next;
8742 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8743 constructor_unfilled_index = next;
8745 /* ELT now points to the node in the pending tree with the next
8746 initializer to output. */
8747 goto retry;
8750 /* Add one non-braced element to the current constructor level.
8751 This adjusts the current position within the constructor's type.
8752 This may also start or terminate implicit levels
8753 to handle a partly-braced initializer.
8755 Once this has found the correct level for the new element,
8756 it calls output_init_element.
8758 IMPLICIT is true if value comes from pop_init_level (1),
8759 the new initializer has been merged with the existing one
8760 and thus no warnings should be emitted about overriding an
8761 existing initializer. */
8763 void
8764 process_init_element (location_t loc, struct c_expr value, bool implicit,
8765 struct obstack * braced_init_obstack)
8767 tree orig_value = value.value;
8768 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8769 bool strict_string = value.original_code == STRING_CST;
8770 bool was_designated = designator_depth != 0;
8772 designator_depth = 0;
8773 designator_erroneous = 0;
8775 if (!implicit && value.value && !integer_zerop (value.value))
8776 constructor_zeroinit = 0;
8778 /* Handle superfluous braces around string cst as in
8779 char x[] = {"foo"}; */
8780 if (string_flag
8781 && constructor_type
8782 && !was_designated
8783 && TREE_CODE (constructor_type) == ARRAY_TYPE
8784 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8785 && integer_zerop (constructor_unfilled_index))
8787 if (constructor_stack->replacement_value.value)
8788 error_init (loc, "excess elements in char array initializer");
8789 constructor_stack->replacement_value = value;
8790 return;
8793 if (constructor_stack->replacement_value.value != 0)
8795 error_init (loc, "excess elements in struct initializer");
8796 return;
8799 /* Ignore elements of a brace group if it is entirely superfluous
8800 and has already been diagnosed. */
8801 if (constructor_type == 0)
8802 return;
8804 if (!implicit && warn_designated_init && !was_designated
8805 && TREE_CODE (constructor_type) == RECORD_TYPE
8806 && lookup_attribute ("designated_init",
8807 TYPE_ATTRIBUTES (constructor_type)))
8808 warning_init (loc,
8809 OPT_Wdesignated_init,
8810 "positional initialization of field "
8811 "in %<struct%> declared with %<designated_init%> attribute");
8813 /* If we've exhausted any levels that didn't have braces,
8814 pop them now. */
8815 while (constructor_stack->implicit)
8817 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8818 || TREE_CODE (constructor_type) == UNION_TYPE)
8819 && constructor_fields == 0)
8820 process_init_element (loc,
8821 pop_init_level (loc, 1, braced_init_obstack),
8822 true, braced_init_obstack);
8823 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8824 || VECTOR_TYPE_P (constructor_type))
8825 && constructor_max_index
8826 && tree_int_cst_lt (constructor_max_index,
8827 constructor_index))
8828 process_init_element (loc,
8829 pop_init_level (loc, 1, braced_init_obstack),
8830 true, braced_init_obstack);
8831 else
8832 break;
8835 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8836 if (constructor_range_stack)
8838 /* If value is a compound literal and we'll be just using its
8839 content, don't put it into a SAVE_EXPR. */
8840 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8841 || !require_constant_value)
8843 tree semantic_type = NULL_TREE;
8844 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8846 semantic_type = TREE_TYPE (value.value);
8847 value.value = TREE_OPERAND (value.value, 0);
8849 value.value = c_save_expr (value.value);
8850 if (semantic_type)
8851 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8852 value.value);
8856 while (1)
8858 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8860 tree fieldtype;
8861 enum tree_code fieldcode;
8863 if (constructor_fields == 0)
8865 pedwarn_init (loc, 0, "excess elements in struct initializer");
8866 break;
8869 fieldtype = TREE_TYPE (constructor_fields);
8870 if (fieldtype != error_mark_node)
8871 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8872 fieldcode = TREE_CODE (fieldtype);
8874 /* Error for non-static initialization of a flexible array member. */
8875 if (fieldcode == ARRAY_TYPE
8876 && !require_constant_value
8877 && TYPE_SIZE (fieldtype) == NULL_TREE
8878 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8880 error_init (loc, "non-static initialization of a flexible "
8881 "array member");
8882 break;
8885 /* Error for initialization of a flexible array member with
8886 a string constant if the structure is in an array. E.g.:
8887 struct S { int x; char y[]; };
8888 struct S s[] = { { 1, "foo" } };
8889 is invalid. */
8890 if (string_flag
8891 && fieldcode == ARRAY_TYPE
8892 && constructor_depth > 1
8893 && TYPE_SIZE (fieldtype) == NULL_TREE
8894 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8896 bool in_array_p = false;
8897 for (struct constructor_stack *p = constructor_stack;
8898 p && p->type; p = p->next)
8899 if (TREE_CODE (p->type) == ARRAY_TYPE)
8901 in_array_p = true;
8902 break;
8904 if (in_array_p)
8906 error_init (loc, "initialization of flexible array "
8907 "member in a nested context");
8908 break;
8912 /* Accept a string constant to initialize a subarray. */
8913 if (value.value != 0
8914 && fieldcode == ARRAY_TYPE
8915 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8916 && string_flag)
8917 value.value = orig_value;
8918 /* Otherwise, if we have come to a subaggregate,
8919 and we don't have an element of its type, push into it. */
8920 else if (value.value != 0
8921 && value.value != error_mark_node
8922 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8923 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8924 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8926 push_init_level (loc, 1, braced_init_obstack);
8927 continue;
8930 if (value.value)
8932 push_member_name (constructor_fields);
8933 output_init_element (loc, value.value, value.original_type,
8934 strict_string, fieldtype,
8935 constructor_fields, 1, implicit,
8936 braced_init_obstack);
8937 RESTORE_SPELLING_DEPTH (constructor_depth);
8939 else
8940 /* Do the bookkeeping for an element that was
8941 directly output as a constructor. */
8943 /* For a record, keep track of end position of last field. */
8944 if (DECL_SIZE (constructor_fields))
8945 constructor_bit_index
8946 = size_binop_loc (input_location, PLUS_EXPR,
8947 bit_position (constructor_fields),
8948 DECL_SIZE (constructor_fields));
8950 /* If the current field was the first one not yet written out,
8951 it isn't now, so update. */
8952 if (constructor_unfilled_fields == constructor_fields)
8954 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8955 /* Skip any nameless bit fields. */
8956 while (constructor_unfilled_fields != 0
8957 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8958 && DECL_NAME (constructor_unfilled_fields) == 0)
8959 constructor_unfilled_fields =
8960 DECL_CHAIN (constructor_unfilled_fields);
8964 constructor_fields = DECL_CHAIN (constructor_fields);
8965 /* Skip any nameless bit fields at the beginning. */
8966 while (constructor_fields != 0
8967 && DECL_C_BIT_FIELD (constructor_fields)
8968 && DECL_NAME (constructor_fields) == 0)
8969 constructor_fields = DECL_CHAIN (constructor_fields);
8971 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8973 tree fieldtype;
8974 enum tree_code fieldcode;
8976 if (constructor_fields == 0)
8978 pedwarn_init (loc, 0,
8979 "excess elements in union initializer");
8980 break;
8983 fieldtype = TREE_TYPE (constructor_fields);
8984 if (fieldtype != error_mark_node)
8985 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8986 fieldcode = TREE_CODE (fieldtype);
8988 /* Warn that traditional C rejects initialization of unions.
8989 We skip the warning if the value is zero. This is done
8990 under the assumption that the zero initializer in user
8991 code appears conditioned on e.g. __STDC__ to avoid
8992 "missing initializer" warnings and relies on default
8993 initialization to zero in the traditional C case.
8994 We also skip the warning if the initializer is designated,
8995 again on the assumption that this must be conditional on
8996 __STDC__ anyway (and we've already complained about the
8997 member-designator already). */
8998 if (!in_system_header_at (input_location) && !constructor_designated
8999 && !(value.value && (integer_zerop (value.value)
9000 || real_zerop (value.value))))
9001 warning (OPT_Wtraditional, "traditional C rejects initialization "
9002 "of unions");
9004 /* Accept a string constant to initialize a subarray. */
9005 if (value.value != 0
9006 && fieldcode == ARRAY_TYPE
9007 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9008 && string_flag)
9009 value.value = orig_value;
9010 /* Otherwise, if we have come to a subaggregate,
9011 and we don't have an element of its type, push into it. */
9012 else if (value.value != 0
9013 && value.value != error_mark_node
9014 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9015 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9016 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9018 push_init_level (loc, 1, braced_init_obstack);
9019 continue;
9022 if (value.value)
9024 push_member_name (constructor_fields);
9025 output_init_element (loc, value.value, value.original_type,
9026 strict_string, fieldtype,
9027 constructor_fields, 1, implicit,
9028 braced_init_obstack);
9029 RESTORE_SPELLING_DEPTH (constructor_depth);
9031 else
9032 /* Do the bookkeeping for an element that was
9033 directly output as a constructor. */
9035 constructor_bit_index = DECL_SIZE (constructor_fields);
9036 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9039 constructor_fields = 0;
9041 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9043 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9044 enum tree_code eltcode = TREE_CODE (elttype);
9046 /* Accept a string constant to initialize a subarray. */
9047 if (value.value != 0
9048 && eltcode == ARRAY_TYPE
9049 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9050 && string_flag)
9051 value.value = orig_value;
9052 /* Otherwise, if we have come to a subaggregate,
9053 and we don't have an element of its type, push into it. */
9054 else if (value.value != 0
9055 && value.value != error_mark_node
9056 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9057 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9058 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9060 push_init_level (loc, 1, braced_init_obstack);
9061 continue;
9064 if (constructor_max_index != 0
9065 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9066 || integer_all_onesp (constructor_max_index)))
9068 pedwarn_init (loc, 0,
9069 "excess elements in array initializer");
9070 break;
9073 /* Now output the actual element. */
9074 if (value.value)
9076 push_array_bounds (tree_to_uhwi (constructor_index));
9077 output_init_element (loc, value.value, value.original_type,
9078 strict_string, elttype,
9079 constructor_index, 1, implicit,
9080 braced_init_obstack);
9081 RESTORE_SPELLING_DEPTH (constructor_depth);
9084 constructor_index
9085 = size_binop_loc (input_location, PLUS_EXPR,
9086 constructor_index, bitsize_one_node);
9088 if (!value.value)
9089 /* If we are doing the bookkeeping for an element that was
9090 directly output as a constructor, we must update
9091 constructor_unfilled_index. */
9092 constructor_unfilled_index = constructor_index;
9094 else if (VECTOR_TYPE_P (constructor_type))
9096 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9098 /* Do a basic check of initializer size. Note that vectors
9099 always have a fixed size derived from their type. */
9100 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9102 pedwarn_init (loc, 0,
9103 "excess elements in vector initializer");
9104 break;
9107 /* Now output the actual element. */
9108 if (value.value)
9110 if (TREE_CODE (value.value) == VECTOR_CST)
9111 elttype = TYPE_MAIN_VARIANT (constructor_type);
9112 output_init_element (loc, value.value, value.original_type,
9113 strict_string, elttype,
9114 constructor_index, 1, implicit,
9115 braced_init_obstack);
9118 constructor_index
9119 = size_binop_loc (input_location,
9120 PLUS_EXPR, constructor_index, bitsize_one_node);
9122 if (!value.value)
9123 /* If we are doing the bookkeeping for an element that was
9124 directly output as a constructor, we must update
9125 constructor_unfilled_index. */
9126 constructor_unfilled_index = constructor_index;
9129 /* Handle the sole element allowed in a braced initializer
9130 for a scalar variable. */
9131 else if (constructor_type != error_mark_node
9132 && constructor_fields == 0)
9134 pedwarn_init (loc, 0,
9135 "excess elements in scalar initializer");
9136 break;
9138 else
9140 if (value.value)
9141 output_init_element (loc, value.value, value.original_type,
9142 strict_string, constructor_type,
9143 NULL_TREE, 1, implicit,
9144 braced_init_obstack);
9145 constructor_fields = 0;
9148 /* Handle range initializers either at this level or anywhere higher
9149 in the designator stack. */
9150 if (constructor_range_stack)
9152 struct constructor_range_stack *p, *range_stack;
9153 int finish = 0;
9155 range_stack = constructor_range_stack;
9156 constructor_range_stack = 0;
9157 while (constructor_stack != range_stack->stack)
9159 gcc_assert (constructor_stack->implicit);
9160 process_init_element (loc,
9161 pop_init_level (loc, 1,
9162 braced_init_obstack),
9163 true, braced_init_obstack);
9165 for (p = range_stack;
9166 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9167 p = p->prev)
9169 gcc_assert (constructor_stack->implicit);
9170 process_init_element (loc,
9171 pop_init_level (loc, 1,
9172 braced_init_obstack),
9173 true, braced_init_obstack);
9176 p->index = size_binop_loc (input_location,
9177 PLUS_EXPR, p->index, bitsize_one_node);
9178 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9179 finish = 1;
9181 while (1)
9183 constructor_index = p->index;
9184 constructor_fields = p->fields;
9185 if (finish && p->range_end && p->index == p->range_start)
9187 finish = 0;
9188 p->prev = 0;
9190 p = p->next;
9191 if (!p)
9192 break;
9193 push_init_level (loc, 2, braced_init_obstack);
9194 p->stack = constructor_stack;
9195 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9196 p->index = p->range_start;
9199 if (!finish)
9200 constructor_range_stack = range_stack;
9201 continue;
9204 break;
9207 constructor_range_stack = 0;
9210 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9211 (guaranteed to be 'volatile' or null) and ARGS (represented using
9212 an ASM_EXPR node). */
9213 tree
9214 build_asm_stmt (tree cv_qualifier, tree args)
9216 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9217 ASM_VOLATILE_P (args) = 1;
9218 return add_stmt (args);
9221 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9222 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9223 SIMPLE indicates whether there was anything at all after the
9224 string in the asm expression -- asm("blah") and asm("blah" : )
9225 are subtly different. We use a ASM_EXPR node to represent this. */
9226 tree
9227 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9228 tree clobbers, tree labels, bool simple)
9230 tree tail;
9231 tree args;
9232 int i;
9233 const char *constraint;
9234 const char **oconstraints;
9235 bool allows_mem, allows_reg, is_inout;
9236 int ninputs, noutputs;
9238 ninputs = list_length (inputs);
9239 noutputs = list_length (outputs);
9240 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9242 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9244 /* Remove output conversions that change the type but not the mode. */
9245 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9247 tree output = TREE_VALUE (tail);
9249 output = c_fully_fold (output, false, NULL);
9251 /* ??? Really, this should not be here. Users should be using a
9252 proper lvalue, dammit. But there's a long history of using casts
9253 in the output operands. In cases like longlong.h, this becomes a
9254 primitive form of typechecking -- if the cast can be removed, then
9255 the output operand had a type of the proper width; otherwise we'll
9256 get an error. Gross, but ... */
9257 STRIP_NOPS (output);
9259 if (!lvalue_or_else (loc, output, lv_asm))
9260 output = error_mark_node;
9262 if (output != error_mark_node
9263 && (TREE_READONLY (output)
9264 || TYPE_READONLY (TREE_TYPE (output))
9265 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9266 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9267 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9268 readonly_error (loc, output, lv_asm);
9270 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9271 oconstraints[i] = constraint;
9273 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9274 &allows_mem, &allows_reg, &is_inout))
9276 /* If the operand is going to end up in memory,
9277 mark it addressable. */
9278 if (!allows_reg && !c_mark_addressable (output))
9279 output = error_mark_node;
9280 if (!(!allows_reg && allows_mem)
9281 && output != error_mark_node
9282 && VOID_TYPE_P (TREE_TYPE (output)))
9284 error_at (loc, "invalid use of void expression");
9285 output = error_mark_node;
9288 else
9289 output = error_mark_node;
9291 TREE_VALUE (tail) = output;
9294 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9296 tree input;
9298 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9299 input = TREE_VALUE (tail);
9301 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9302 oconstraints, &allows_mem, &allows_reg))
9304 /* If the operand is going to end up in memory,
9305 mark it addressable. */
9306 if (!allows_reg && allows_mem)
9308 input = c_fully_fold (input, false, NULL);
9310 /* Strip the nops as we allow this case. FIXME, this really
9311 should be rejected or made deprecated. */
9312 STRIP_NOPS (input);
9313 if (!c_mark_addressable (input))
9314 input = error_mark_node;
9316 else
9318 struct c_expr expr;
9319 memset (&expr, 0, sizeof (expr));
9320 expr.value = input;
9321 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9322 input = c_fully_fold (expr.value, false, NULL);
9324 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9326 error_at (loc, "invalid use of void expression");
9327 input = error_mark_node;
9331 else
9332 input = error_mark_node;
9334 TREE_VALUE (tail) = input;
9337 /* ASMs with labels cannot have outputs. This should have been
9338 enforced by the parser. */
9339 gcc_assert (outputs == NULL || labels == NULL);
9341 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9343 /* asm statements without outputs, including simple ones, are treated
9344 as volatile. */
9345 ASM_INPUT_P (args) = simple;
9346 ASM_VOLATILE_P (args) = (noutputs == 0);
9348 return args;
9351 /* Generate a goto statement to LABEL. LOC is the location of the
9352 GOTO. */
9354 tree
9355 c_finish_goto_label (location_t loc, tree label)
9357 tree decl = lookup_label_for_goto (loc, label);
9358 if (!decl)
9359 return NULL_TREE;
9360 TREE_USED (decl) = 1;
9362 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9363 SET_EXPR_LOCATION (t, loc);
9364 return add_stmt (t);
9368 /* Generate a computed goto statement to EXPR. LOC is the location of
9369 the GOTO. */
9371 tree
9372 c_finish_goto_ptr (location_t loc, tree expr)
9374 tree t;
9375 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9376 expr = c_fully_fold (expr, false, NULL);
9377 expr = convert (ptr_type_node, expr);
9378 t = build1 (GOTO_EXPR, void_type_node, expr);
9379 SET_EXPR_LOCATION (t, loc);
9380 return add_stmt (t);
9383 /* Generate a C `return' statement. RETVAL is the expression for what
9384 to return, or a null pointer for `return;' with no value. LOC is
9385 the location of the return statement, or the location of the expression,
9386 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9387 is the original type of RETVAL. */
9389 tree
9390 c_finish_return (location_t loc, tree retval, tree origtype)
9392 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9393 bool no_warning = false;
9394 bool npc = false;
9395 size_t rank = 0;
9397 /* Use the expansion point to handle cases such as returning NULL
9398 in a function returning void. */
9399 source_location xloc = expansion_point_location_if_in_system_header (loc);
9401 if (TREE_THIS_VOLATILE (current_function_decl))
9402 warning_at (xloc, 0,
9403 "function declared %<noreturn%> has a %<return%> statement");
9405 if (flag_cilkplus && contains_array_notation_expr (retval))
9407 /* Array notations are allowed in a return statement if it is inside a
9408 built-in array notation reduction function. */
9409 if (!find_rank (loc, retval, retval, false, &rank))
9410 return error_mark_node;
9411 if (rank >= 1)
9413 error_at (loc, "array notation expression cannot be used as a "
9414 "return value");
9415 return error_mark_node;
9418 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9420 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9421 "allowed");
9422 return error_mark_node;
9424 if (retval)
9426 tree semantic_type = NULL_TREE;
9427 npc = null_pointer_constant_p (retval);
9428 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9430 semantic_type = TREE_TYPE (retval);
9431 retval = TREE_OPERAND (retval, 0);
9433 retval = c_fully_fold (retval, false, NULL);
9434 if (semantic_type)
9435 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9438 if (!retval)
9440 current_function_returns_null = 1;
9441 if ((warn_return_type || flag_isoc99)
9442 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9444 if (flag_isoc99)
9445 pedwarn (loc, 0, "%<return%> with no value, in "
9446 "function returning non-void");
9447 else
9448 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9449 "in function returning non-void");
9450 no_warning = true;
9453 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9455 current_function_returns_null = 1;
9456 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9457 pedwarn (xloc, 0,
9458 "%<return%> with a value, in function returning void");
9459 else
9460 pedwarn (xloc, OPT_Wpedantic, "ISO C forbids "
9461 "%<return%> with expression, in function returning void");
9463 else
9465 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9466 retval, origtype, ic_return,
9467 npc, NULL_TREE, NULL_TREE, 0);
9468 tree res = DECL_RESULT (current_function_decl);
9469 tree inner;
9470 bool save;
9472 current_function_returns_value = 1;
9473 if (t == error_mark_node)
9474 return NULL_TREE;
9476 save = in_late_binary_op;
9477 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9478 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9479 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9480 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9481 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9482 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9483 in_late_binary_op = true;
9484 inner = t = convert (TREE_TYPE (res), t);
9485 in_late_binary_op = save;
9487 /* Strip any conversions, additions, and subtractions, and see if
9488 we are returning the address of a local variable. Warn if so. */
9489 while (1)
9491 switch (TREE_CODE (inner))
9493 CASE_CONVERT:
9494 case NON_LVALUE_EXPR:
9495 case PLUS_EXPR:
9496 case POINTER_PLUS_EXPR:
9497 inner = TREE_OPERAND (inner, 0);
9498 continue;
9500 case MINUS_EXPR:
9501 /* If the second operand of the MINUS_EXPR has a pointer
9502 type (or is converted from it), this may be valid, so
9503 don't give a warning. */
9505 tree op1 = TREE_OPERAND (inner, 1);
9507 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9508 && (CONVERT_EXPR_P (op1)
9509 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9510 op1 = TREE_OPERAND (op1, 0);
9512 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9513 break;
9515 inner = TREE_OPERAND (inner, 0);
9516 continue;
9519 case ADDR_EXPR:
9520 inner = TREE_OPERAND (inner, 0);
9522 while (REFERENCE_CLASS_P (inner)
9523 && !INDIRECT_REF_P (inner))
9524 inner = TREE_OPERAND (inner, 0);
9526 if (DECL_P (inner)
9527 && !DECL_EXTERNAL (inner)
9528 && !TREE_STATIC (inner)
9529 && DECL_CONTEXT (inner) == current_function_decl)
9531 if (TREE_CODE (inner) == LABEL_DECL)
9532 warning_at (loc, OPT_Wreturn_local_addr,
9533 "function returns address of label");
9534 else
9536 warning_at (loc, OPT_Wreturn_local_addr,
9537 "function returns address of local variable");
9538 tree zero = build_zero_cst (TREE_TYPE (res));
9539 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9542 break;
9544 default:
9545 break;
9548 break;
9551 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9552 SET_EXPR_LOCATION (retval, loc);
9554 if (warn_sequence_point)
9555 verify_sequence_points (retval);
9558 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9559 TREE_NO_WARNING (ret_stmt) |= no_warning;
9560 return add_stmt (ret_stmt);
9563 struct c_switch {
9564 /* The SWITCH_EXPR being built. */
9565 tree switch_expr;
9567 /* The original type of the testing expression, i.e. before the
9568 default conversion is applied. */
9569 tree orig_type;
9571 /* A splay-tree mapping the low element of a case range to the high
9572 element, or NULL_TREE if there is no high element. Used to
9573 determine whether or not a new case label duplicates an old case
9574 label. We need a tree, rather than simply a hash table, because
9575 of the GNU case range extension. */
9576 splay_tree cases;
9578 /* The bindings at the point of the switch. This is used for
9579 warnings crossing decls when branching to a case label. */
9580 struct c_spot_bindings *bindings;
9582 /* The next node on the stack. */
9583 struct c_switch *next;
9585 /* Remember whether the controlling expression had boolean type
9586 before integer promotions for the sake of -Wswitch-bool. */
9587 bool bool_cond_p;
9589 /* Remember whether there was a case value that is outside the
9590 range of the ORIG_TYPE. */
9591 bool outside_range_p;
9594 /* A stack of the currently active switch statements. The innermost
9595 switch statement is on the top of the stack. There is no need to
9596 mark the stack for garbage collection because it is only active
9597 during the processing of the body of a function, and we never
9598 collect at that point. */
9600 struct c_switch *c_switch_stack;
9602 /* Start a C switch statement, testing expression EXP. Return the new
9603 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9604 SWITCH_COND_LOC is the location of the switch's condition.
9605 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
9607 tree
9608 c_start_case (location_t switch_loc,
9609 location_t switch_cond_loc,
9610 tree exp, bool explicit_cast_p)
9612 tree orig_type = error_mark_node;
9613 bool bool_cond_p = false;
9614 struct c_switch *cs;
9616 if (exp != error_mark_node)
9618 orig_type = TREE_TYPE (exp);
9620 if (!INTEGRAL_TYPE_P (orig_type))
9622 if (orig_type != error_mark_node)
9624 error_at (switch_cond_loc, "switch quantity not an integer");
9625 orig_type = error_mark_node;
9627 exp = integer_zero_node;
9629 else
9631 tree type = TYPE_MAIN_VARIANT (orig_type);
9632 tree e = exp;
9634 /* Warn if the condition has boolean value. */
9635 while (TREE_CODE (e) == COMPOUND_EXPR)
9636 e = TREE_OPERAND (e, 1);
9638 if ((TREE_CODE (type) == BOOLEAN_TYPE
9639 || truth_value_p (TREE_CODE (e)))
9640 /* Explicit cast to int suppresses this warning. */
9641 && !(TREE_CODE (type) == INTEGER_TYPE
9642 && explicit_cast_p))
9643 bool_cond_p = true;
9645 if (!in_system_header_at (input_location)
9646 && (type == long_integer_type_node
9647 || type == long_unsigned_type_node))
9648 warning_at (switch_cond_loc,
9649 OPT_Wtraditional, "%<long%> switch expression not "
9650 "converted to %<int%> in ISO C");
9652 exp = c_fully_fold (exp, false, NULL);
9653 exp = default_conversion (exp);
9655 if (warn_sequence_point)
9656 verify_sequence_points (exp);
9660 /* Add this new SWITCH_EXPR to the stack. */
9661 cs = XNEW (struct c_switch);
9662 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9663 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9664 cs->orig_type = orig_type;
9665 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9666 cs->bindings = c_get_switch_bindings ();
9667 cs->bool_cond_p = bool_cond_p;
9668 cs->outside_range_p = false;
9669 cs->next = c_switch_stack;
9670 c_switch_stack = cs;
9672 return add_stmt (cs->switch_expr);
9675 /* Process a case label at location LOC. */
9677 tree
9678 do_case (location_t loc, tree low_value, tree high_value)
9680 tree label = NULL_TREE;
9682 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9684 low_value = c_fully_fold (low_value, false, NULL);
9685 if (TREE_CODE (low_value) == INTEGER_CST)
9686 pedwarn (loc, OPT_Wpedantic,
9687 "case label is not an integer constant expression");
9690 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9692 high_value = c_fully_fold (high_value, false, NULL);
9693 if (TREE_CODE (high_value) == INTEGER_CST)
9694 pedwarn (input_location, OPT_Wpedantic,
9695 "case label is not an integer constant expression");
9698 if (c_switch_stack == NULL)
9700 if (low_value)
9701 error_at (loc, "case label not within a switch statement");
9702 else
9703 error_at (loc, "%<default%> label not within a switch statement");
9704 return NULL_TREE;
9707 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9708 EXPR_LOCATION (c_switch_stack->switch_expr),
9709 loc))
9710 return NULL_TREE;
9712 label = c_add_case_label (loc, c_switch_stack->cases,
9713 SWITCH_COND (c_switch_stack->switch_expr),
9714 c_switch_stack->orig_type,
9715 low_value, high_value,
9716 &c_switch_stack->outside_range_p);
9717 if (label == error_mark_node)
9718 label = NULL_TREE;
9719 return label;
9722 /* Finish the switch statement. TYPE is the original type of the
9723 controlling expression of the switch, or NULL_TREE. */
9725 void
9726 c_finish_case (tree body, tree type)
9728 struct c_switch *cs = c_switch_stack;
9729 location_t switch_location;
9731 SWITCH_BODY (cs->switch_expr) = body;
9733 /* Emit warnings as needed. */
9734 switch_location = EXPR_LOCATION (cs->switch_expr);
9735 c_do_switch_warnings (cs->cases, switch_location,
9736 type ? type : TREE_TYPE (cs->switch_expr),
9737 SWITCH_COND (cs->switch_expr),
9738 cs->bool_cond_p, cs->outside_range_p);
9740 /* Pop the stack. */
9741 c_switch_stack = cs->next;
9742 splay_tree_delete (cs->cases);
9743 c_release_switch_bindings (cs->bindings);
9744 XDELETE (cs);
9747 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9748 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9749 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9750 statement, and was not surrounded with parenthesis. */
9752 void
9753 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9754 tree else_block, bool nested_if)
9756 tree stmt;
9758 /* If the condition has array notations, then the rank of the then_block and
9759 else_block must be either 0 or be equal to the rank of the condition. If
9760 the condition does not have array notations then break them up as it is
9761 broken up in a normal expression. */
9762 if (flag_cilkplus && contains_array_notation_expr (cond))
9764 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9765 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9766 return;
9767 if (then_block
9768 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9769 return;
9770 if (else_block
9771 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9772 return;
9773 if (cond_rank != then_rank && then_rank != 0)
9775 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9776 " and the then-block");
9777 return;
9779 else if (cond_rank != else_rank && else_rank != 0)
9781 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9782 " and the else-block");
9783 return;
9786 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9787 if (warn_parentheses && nested_if && else_block == NULL)
9789 tree inner_if = then_block;
9791 /* We know from the grammar productions that there is an IF nested
9792 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9793 it might not be exactly THEN_BLOCK, but should be the last
9794 non-container statement within. */
9795 while (1)
9796 switch (TREE_CODE (inner_if))
9798 case COND_EXPR:
9799 goto found;
9800 case BIND_EXPR:
9801 inner_if = BIND_EXPR_BODY (inner_if);
9802 break;
9803 case STATEMENT_LIST:
9804 inner_if = expr_last (then_block);
9805 break;
9806 case TRY_FINALLY_EXPR:
9807 case TRY_CATCH_EXPR:
9808 inner_if = TREE_OPERAND (inner_if, 0);
9809 break;
9810 default:
9811 gcc_unreachable ();
9813 found:
9815 if (COND_EXPR_ELSE (inner_if))
9816 warning_at (if_locus, OPT_Wparentheses,
9817 "suggest explicit braces to avoid ambiguous %<else%>");
9820 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9821 SET_EXPR_LOCATION (stmt, if_locus);
9822 add_stmt (stmt);
9825 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9826 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9827 is false for DO loops. INCR is the FOR increment expression. BODY is
9828 the statement controlled by the loop. BLAB is the break label. CLAB is
9829 the continue label. Everything is allowed to be NULL. */
9831 void
9832 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9833 tree blab, tree clab, bool cond_is_first)
9835 tree entry = NULL, exit = NULL, t;
9837 /* In theory could forbid cilk spawn for loop increment expression,
9838 but it should work just fine. */
9840 /* If the condition is zero don't generate a loop construct. */
9841 if (cond && integer_zerop (cond))
9843 if (cond_is_first)
9845 t = build_and_jump (&blab);
9846 SET_EXPR_LOCATION (t, start_locus);
9847 add_stmt (t);
9850 else
9852 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9854 /* If we have an exit condition, then we build an IF with gotos either
9855 out of the loop, or to the top of it. If there's no exit condition,
9856 then we just build a jump back to the top. */
9857 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9859 if (cond && !integer_nonzerop (cond))
9861 /* Canonicalize the loop condition to the end. This means
9862 generating a branch to the loop condition. Reuse the
9863 continue label, if possible. */
9864 if (cond_is_first)
9866 if (incr || !clab)
9868 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9869 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9871 else
9872 t = build1 (GOTO_EXPR, void_type_node, clab);
9873 SET_EXPR_LOCATION (t, start_locus);
9874 add_stmt (t);
9877 t = build_and_jump (&blab);
9878 if (cond_is_first)
9879 exit = fold_build3_loc (start_locus,
9880 COND_EXPR, void_type_node, cond, exit, t);
9881 else
9882 exit = fold_build3_loc (input_location,
9883 COND_EXPR, void_type_node, cond, exit, t);
9886 add_stmt (top);
9889 if (body)
9890 add_stmt (body);
9891 if (clab)
9892 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9893 if (incr)
9894 add_stmt (incr);
9895 if (entry)
9896 add_stmt (entry);
9897 if (exit)
9898 add_stmt (exit);
9899 if (blab)
9900 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9903 tree
9904 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9906 bool skip;
9907 tree label = *label_p;
9909 /* In switch statements break is sometimes stylistically used after
9910 a return statement. This can lead to spurious warnings about
9911 control reaching the end of a non-void function when it is
9912 inlined. Note that we are calling block_may_fallthru with
9913 language specific tree nodes; this works because
9914 block_may_fallthru returns true when given something it does not
9915 understand. */
9916 skip = !block_may_fallthru (cur_stmt_list);
9918 if (!label)
9920 if (!skip)
9921 *label_p = label = create_artificial_label (loc);
9923 else if (TREE_CODE (label) == LABEL_DECL)
9925 else switch (TREE_INT_CST_LOW (label))
9927 case 0:
9928 if (is_break)
9929 error_at (loc, "break statement not within loop or switch");
9930 else
9931 error_at (loc, "continue statement not within a loop");
9932 return NULL_TREE;
9934 case 1:
9935 gcc_assert (is_break);
9936 error_at (loc, "break statement used with OpenMP for loop");
9937 return NULL_TREE;
9939 case 2:
9940 if (is_break)
9941 error ("break statement within %<#pragma simd%> loop body");
9942 else
9943 error ("continue statement within %<#pragma simd%> loop body");
9944 return NULL_TREE;
9946 default:
9947 gcc_unreachable ();
9950 if (skip)
9951 return NULL_TREE;
9953 if (!is_break)
9954 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9956 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9959 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9961 static void
9962 emit_side_effect_warnings (location_t loc, tree expr)
9964 if (expr == error_mark_node)
9966 else if (!TREE_SIDE_EFFECTS (expr))
9968 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9969 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9971 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9973 tree r = expr;
9974 location_t cloc = loc;
9975 while (TREE_CODE (r) == COMPOUND_EXPR)
9977 if (EXPR_HAS_LOCATION (r))
9978 cloc = EXPR_LOCATION (r);
9979 r = TREE_OPERAND (r, 1);
9981 if (!TREE_SIDE_EFFECTS (r)
9982 && !VOID_TYPE_P (TREE_TYPE (r))
9983 && !CONVERT_EXPR_P (r)
9984 && !TREE_NO_WARNING (r)
9985 && !TREE_NO_WARNING (expr))
9986 warning_at (cloc, OPT_Wunused_value,
9987 "right-hand operand of comma expression has no effect");
9989 else
9990 warn_if_unused_value (expr, loc);
9993 /* Process an expression as if it were a complete statement. Emit
9994 diagnostics, but do not call ADD_STMT. LOC is the location of the
9995 statement. */
9997 tree
9998 c_process_expr_stmt (location_t loc, tree expr)
10000 tree exprv;
10002 if (!expr)
10003 return NULL_TREE;
10005 expr = c_fully_fold (expr, false, NULL);
10007 if (warn_sequence_point)
10008 verify_sequence_points (expr);
10010 if (TREE_TYPE (expr) != error_mark_node
10011 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10012 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10013 error_at (loc, "expression statement has incomplete type");
10015 /* If we're not processing a statement expression, warn about unused values.
10016 Warnings for statement expressions will be emitted later, once we figure
10017 out which is the result. */
10018 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10019 && warn_unused_value)
10020 emit_side_effect_warnings (loc, expr);
10022 exprv = expr;
10023 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10024 exprv = TREE_OPERAND (exprv, 1);
10025 while (CONVERT_EXPR_P (exprv))
10026 exprv = TREE_OPERAND (exprv, 0);
10027 if (DECL_P (exprv)
10028 || handled_component_p (exprv)
10029 || TREE_CODE (exprv) == ADDR_EXPR)
10030 mark_exp_read (exprv);
10032 /* If the expression is not of a type to which we cannot assign a line
10033 number, wrap the thing in a no-op NOP_EXPR. */
10034 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10036 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10037 SET_EXPR_LOCATION (expr, loc);
10040 return expr;
10043 /* Emit an expression as a statement. LOC is the location of the
10044 expression. */
10046 tree
10047 c_finish_expr_stmt (location_t loc, tree expr)
10049 if (expr)
10050 return add_stmt (c_process_expr_stmt (loc, expr));
10051 else
10052 return NULL;
10055 /* Do the opposite and emit a statement as an expression. To begin,
10056 create a new binding level and return it. */
10058 tree
10059 c_begin_stmt_expr (void)
10061 tree ret;
10063 /* We must force a BLOCK for this level so that, if it is not expanded
10064 later, there is a way to turn off the entire subtree of blocks that
10065 are contained in it. */
10066 keep_next_level ();
10067 ret = c_begin_compound_stmt (true);
10069 c_bindings_start_stmt_expr (c_switch_stack == NULL
10070 ? NULL
10071 : c_switch_stack->bindings);
10073 /* Mark the current statement list as belonging to a statement list. */
10074 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10076 return ret;
10079 /* LOC is the location of the compound statement to which this body
10080 belongs. */
10082 tree
10083 c_finish_stmt_expr (location_t loc, tree body)
10085 tree last, type, tmp, val;
10086 tree *last_p;
10088 body = c_end_compound_stmt (loc, body, true);
10090 c_bindings_end_stmt_expr (c_switch_stack == NULL
10091 ? NULL
10092 : c_switch_stack->bindings);
10094 /* Locate the last statement in BODY. See c_end_compound_stmt
10095 about always returning a BIND_EXPR. */
10096 last_p = &BIND_EXPR_BODY (body);
10097 last = BIND_EXPR_BODY (body);
10099 continue_searching:
10100 if (TREE_CODE (last) == STATEMENT_LIST)
10102 tree_stmt_iterator i;
10104 /* This can happen with degenerate cases like ({ }). No value. */
10105 if (!TREE_SIDE_EFFECTS (last))
10106 return body;
10108 /* If we're supposed to generate side effects warnings, process
10109 all of the statements except the last. */
10110 if (warn_unused_value)
10112 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10114 location_t tloc;
10115 tree t = tsi_stmt (i);
10117 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10118 emit_side_effect_warnings (tloc, t);
10121 else
10122 i = tsi_last (last);
10123 last_p = tsi_stmt_ptr (i);
10124 last = *last_p;
10127 /* If the end of the list is exception related, then the list was split
10128 by a call to push_cleanup. Continue searching. */
10129 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10130 || TREE_CODE (last) == TRY_CATCH_EXPR)
10132 last_p = &TREE_OPERAND (last, 0);
10133 last = *last_p;
10134 goto continue_searching;
10137 if (last == error_mark_node)
10138 return last;
10140 /* In the case that the BIND_EXPR is not necessary, return the
10141 expression out from inside it. */
10142 if (last == BIND_EXPR_BODY (body)
10143 && BIND_EXPR_VARS (body) == NULL)
10145 /* Even if this looks constant, do not allow it in a constant
10146 expression. */
10147 last = c_wrap_maybe_const (last, true);
10148 /* Do not warn if the return value of a statement expression is
10149 unused. */
10150 TREE_NO_WARNING (last) = 1;
10151 return last;
10154 /* Extract the type of said expression. */
10155 type = TREE_TYPE (last);
10157 /* If we're not returning a value at all, then the BIND_EXPR that
10158 we already have is a fine expression to return. */
10159 if (!type || VOID_TYPE_P (type))
10160 return body;
10162 /* Now that we've located the expression containing the value, it seems
10163 silly to make voidify_wrapper_expr repeat the process. Create a
10164 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10165 tmp = create_tmp_var_raw (type);
10167 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10168 tree_expr_nonnegative_p giving up immediately. */
10169 val = last;
10170 if (TREE_CODE (val) == NOP_EXPR
10171 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10172 val = TREE_OPERAND (val, 0);
10174 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10175 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10178 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10179 SET_EXPR_LOCATION (t, loc);
10180 return t;
10184 /* Begin and end compound statements. This is as simple as pushing
10185 and popping new statement lists from the tree. */
10187 tree
10188 c_begin_compound_stmt (bool do_scope)
10190 tree stmt = push_stmt_list ();
10191 if (do_scope)
10192 push_scope ();
10193 return stmt;
10196 /* End a compound statement. STMT is the statement. LOC is the
10197 location of the compound statement-- this is usually the location
10198 of the opening brace. */
10200 tree
10201 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10203 tree block = NULL;
10205 if (do_scope)
10207 if (c_dialect_objc ())
10208 objc_clear_super_receiver ();
10209 block = pop_scope ();
10212 stmt = pop_stmt_list (stmt);
10213 stmt = c_build_bind_expr (loc, block, stmt);
10215 /* If this compound statement is nested immediately inside a statement
10216 expression, then force a BIND_EXPR to be created. Otherwise we'll
10217 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10218 STATEMENT_LISTs merge, and thus we can lose track of what statement
10219 was really last. */
10220 if (building_stmt_list_p ()
10221 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10222 && TREE_CODE (stmt) != BIND_EXPR)
10224 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10225 TREE_SIDE_EFFECTS (stmt) = 1;
10226 SET_EXPR_LOCATION (stmt, loc);
10229 return stmt;
10232 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10233 when the current scope is exited. EH_ONLY is true when this is not
10234 meant to apply to normal control flow transfer. */
10236 void
10237 push_cleanup (tree decl, tree cleanup, bool eh_only)
10239 enum tree_code code;
10240 tree stmt, list;
10241 bool stmt_expr;
10243 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10244 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10245 add_stmt (stmt);
10246 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10247 list = push_stmt_list ();
10248 TREE_OPERAND (stmt, 0) = list;
10249 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10252 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10253 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10255 static tree
10256 build_vec_cmp (tree_code code, tree type,
10257 tree arg0, tree arg1)
10259 tree zero_vec = build_zero_cst (type);
10260 tree minus_one_vec = build_minus_one_cst (type);
10261 tree cmp_type = build_same_sized_truth_vector_type (type);
10262 tree cmp = build2 (code, cmp_type, arg0, arg1);
10263 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10266 /* Build a binary-operation expression without default conversions.
10267 CODE is the kind of expression to build.
10268 LOCATION is the operator's location.
10269 This function differs from `build' in several ways:
10270 the data type of the result is computed and recorded in it,
10271 warnings are generated if arg data types are invalid,
10272 special handling for addition and subtraction of pointers is known,
10273 and some optimization is done (operations on narrow ints
10274 are done in the narrower type when that gives the same result).
10275 Constant folding is also done before the result is returned.
10277 Note that the operands will never have enumeral types, or function
10278 or array types, because either they will have the default conversions
10279 performed or they have both just been converted to some other type in which
10280 the arithmetic is to be done. */
10282 tree
10283 build_binary_op (location_t location, enum tree_code code,
10284 tree orig_op0, tree orig_op1, int convert_p)
10286 tree type0, type1, orig_type0, orig_type1;
10287 tree eptype;
10288 enum tree_code code0, code1;
10289 tree op0, op1;
10290 tree ret = error_mark_node;
10291 const char *invalid_op_diag;
10292 bool op0_int_operands, op1_int_operands;
10293 bool int_const, int_const_or_overflow, int_operands;
10295 /* Expression code to give to the expression when it is built.
10296 Normally this is CODE, which is what the caller asked for,
10297 but in some special cases we change it. */
10298 enum tree_code resultcode = code;
10300 /* Data type in which the computation is to be performed.
10301 In the simplest cases this is the common type of the arguments. */
10302 tree result_type = NULL;
10304 /* When the computation is in excess precision, the type of the
10305 final EXCESS_PRECISION_EXPR. */
10306 tree semantic_result_type = NULL;
10308 /* Nonzero means operands have already been type-converted
10309 in whatever way is necessary.
10310 Zero means they need to be converted to RESULT_TYPE. */
10311 int converted = 0;
10313 /* Nonzero means create the expression with this type, rather than
10314 RESULT_TYPE. */
10315 tree build_type = 0;
10317 /* Nonzero means after finally constructing the expression
10318 convert it to this type. */
10319 tree final_type = 0;
10321 /* Nonzero if this is an operation like MIN or MAX which can
10322 safely be computed in short if both args are promoted shorts.
10323 Also implies COMMON.
10324 -1 indicates a bitwise operation; this makes a difference
10325 in the exact conditions for when it is safe to do the operation
10326 in a narrower mode. */
10327 int shorten = 0;
10329 /* Nonzero if this is a comparison operation;
10330 if both args are promoted shorts, compare the original shorts.
10331 Also implies COMMON. */
10332 int short_compare = 0;
10334 /* Nonzero if this is a right-shift operation, which can be computed on the
10335 original short and then promoted if the operand is a promoted short. */
10336 int short_shift = 0;
10338 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10339 int common = 0;
10341 /* True means types are compatible as far as ObjC is concerned. */
10342 bool objc_ok;
10344 /* True means this is an arithmetic operation that may need excess
10345 precision. */
10346 bool may_need_excess_precision;
10348 /* True means this is a boolean operation that converts both its
10349 operands to truth-values. */
10350 bool boolean_op = false;
10352 /* Remember whether we're doing / or %. */
10353 bool doing_div_or_mod = false;
10355 /* Remember whether we're doing << or >>. */
10356 bool doing_shift = false;
10358 /* Tree holding instrumentation expression. */
10359 tree instrument_expr = NULL;
10361 if (location == UNKNOWN_LOCATION)
10362 location = input_location;
10364 op0 = orig_op0;
10365 op1 = orig_op1;
10367 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10368 if (op0_int_operands)
10369 op0 = remove_c_maybe_const_expr (op0);
10370 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10371 if (op1_int_operands)
10372 op1 = remove_c_maybe_const_expr (op1);
10373 int_operands = (op0_int_operands && op1_int_operands);
10374 if (int_operands)
10376 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10377 && TREE_CODE (orig_op1) == INTEGER_CST);
10378 int_const = (int_const_or_overflow
10379 && !TREE_OVERFLOW (orig_op0)
10380 && !TREE_OVERFLOW (orig_op1));
10382 else
10383 int_const = int_const_or_overflow = false;
10385 /* Do not apply default conversion in mixed vector/scalar expression. */
10386 if (convert_p
10387 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10389 op0 = default_conversion (op0);
10390 op1 = default_conversion (op1);
10393 /* When Cilk Plus is enabled and there are array notations inside op0, then
10394 we check to see if there are builtin array notation functions. If
10395 so, then we take on the type of the array notation inside it. */
10396 if (flag_cilkplus && contains_array_notation_expr (op0))
10397 orig_type0 = type0 = find_correct_array_notation_type (op0);
10398 else
10399 orig_type0 = type0 = TREE_TYPE (op0);
10401 if (flag_cilkplus && contains_array_notation_expr (op1))
10402 orig_type1 = type1 = find_correct_array_notation_type (op1);
10403 else
10404 orig_type1 = type1 = TREE_TYPE (op1);
10406 /* The expression codes of the data types of the arguments tell us
10407 whether the arguments are integers, floating, pointers, etc. */
10408 code0 = TREE_CODE (type0);
10409 code1 = TREE_CODE (type1);
10411 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10412 STRIP_TYPE_NOPS (op0);
10413 STRIP_TYPE_NOPS (op1);
10415 /* If an error was already reported for one of the arguments,
10416 avoid reporting another error. */
10418 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10419 return error_mark_node;
10421 if (code0 == POINTER_TYPE
10422 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10423 return error_mark_node;
10425 if (code1 == POINTER_TYPE
10426 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10427 return error_mark_node;
10429 if ((invalid_op_diag
10430 = targetm.invalid_binary_op (code, type0, type1)))
10432 error_at (location, invalid_op_diag);
10433 return error_mark_node;
10436 switch (code)
10438 case PLUS_EXPR:
10439 case MINUS_EXPR:
10440 case MULT_EXPR:
10441 case TRUNC_DIV_EXPR:
10442 case CEIL_DIV_EXPR:
10443 case FLOOR_DIV_EXPR:
10444 case ROUND_DIV_EXPR:
10445 case EXACT_DIV_EXPR:
10446 may_need_excess_precision = true;
10447 break;
10448 default:
10449 may_need_excess_precision = false;
10450 break;
10452 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10454 op0 = TREE_OPERAND (op0, 0);
10455 type0 = TREE_TYPE (op0);
10457 else if (may_need_excess_precision
10458 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10460 type0 = eptype;
10461 op0 = convert (eptype, op0);
10463 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10465 op1 = TREE_OPERAND (op1, 0);
10466 type1 = TREE_TYPE (op1);
10468 else if (may_need_excess_precision
10469 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10471 type1 = eptype;
10472 op1 = convert (eptype, op1);
10475 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10477 /* In case when one of the operands of the binary operation is
10478 a vector and another is a scalar -- convert scalar to vector. */
10479 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10481 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10482 true);
10484 switch (convert_flag)
10486 case stv_error:
10487 return error_mark_node;
10488 case stv_firstarg:
10490 bool maybe_const = true;
10491 tree sc;
10492 sc = c_fully_fold (op0, false, &maybe_const);
10493 sc = save_expr (sc);
10494 sc = convert (TREE_TYPE (type1), sc);
10495 op0 = build_vector_from_val (type1, sc);
10496 if (!maybe_const)
10497 op0 = c_wrap_maybe_const (op0, true);
10498 orig_type0 = type0 = TREE_TYPE (op0);
10499 code0 = TREE_CODE (type0);
10500 converted = 1;
10501 break;
10503 case stv_secondarg:
10505 bool maybe_const = true;
10506 tree sc;
10507 sc = c_fully_fold (op1, false, &maybe_const);
10508 sc = save_expr (sc);
10509 sc = convert (TREE_TYPE (type0), sc);
10510 op1 = build_vector_from_val (type0, sc);
10511 if (!maybe_const)
10512 op1 = c_wrap_maybe_const (op1, true);
10513 orig_type1 = type1 = TREE_TYPE (op1);
10514 code1 = TREE_CODE (type1);
10515 converted = 1;
10516 break;
10518 default:
10519 break;
10523 switch (code)
10525 case PLUS_EXPR:
10526 /* Handle the pointer + int case. */
10527 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10529 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10530 goto return_build_binary_op;
10532 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10534 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10535 goto return_build_binary_op;
10537 else
10538 common = 1;
10539 break;
10541 case MINUS_EXPR:
10542 /* Subtraction of two similar pointers.
10543 We must subtract them as integers, then divide by object size. */
10544 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10545 && comp_target_types (location, type0, type1))
10547 ret = pointer_diff (location, op0, op1);
10548 goto return_build_binary_op;
10550 /* Handle pointer minus int. Just like pointer plus int. */
10551 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10553 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10554 goto return_build_binary_op;
10556 else
10557 common = 1;
10558 break;
10560 case MULT_EXPR:
10561 common = 1;
10562 break;
10564 case TRUNC_DIV_EXPR:
10565 case CEIL_DIV_EXPR:
10566 case FLOOR_DIV_EXPR:
10567 case ROUND_DIV_EXPR:
10568 case EXACT_DIV_EXPR:
10569 doing_div_or_mod = true;
10570 warn_for_div_by_zero (location, op1);
10572 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10573 || code0 == FIXED_POINT_TYPE
10574 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10575 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10576 || code1 == FIXED_POINT_TYPE
10577 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10579 enum tree_code tcode0 = code0, tcode1 = code1;
10581 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10582 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10583 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10584 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10586 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10587 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10588 resultcode = RDIV_EXPR;
10589 else
10590 /* Although it would be tempting to shorten always here, that
10591 loses on some targets, since the modulo instruction is
10592 undefined if the quotient can't be represented in the
10593 computation mode. We shorten only if unsigned or if
10594 dividing by something we know != -1. */
10595 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10596 || (TREE_CODE (op1) == INTEGER_CST
10597 && !integer_all_onesp (op1)));
10598 common = 1;
10600 break;
10602 case BIT_AND_EXPR:
10603 case BIT_IOR_EXPR:
10604 case BIT_XOR_EXPR:
10605 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10606 shorten = -1;
10607 /* Allow vector types which are not floating point types. */
10608 else if (code0 == VECTOR_TYPE
10609 && code1 == VECTOR_TYPE
10610 && !VECTOR_FLOAT_TYPE_P (type0)
10611 && !VECTOR_FLOAT_TYPE_P (type1))
10612 common = 1;
10613 break;
10615 case TRUNC_MOD_EXPR:
10616 case FLOOR_MOD_EXPR:
10617 doing_div_or_mod = true;
10618 warn_for_div_by_zero (location, op1);
10620 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10621 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10622 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10623 common = 1;
10624 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10626 /* Although it would be tempting to shorten always here, that loses
10627 on some targets, since the modulo instruction is undefined if the
10628 quotient can't be represented in the computation mode. We shorten
10629 only if unsigned or if dividing by something we know != -1. */
10630 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10631 || (TREE_CODE (op1) == INTEGER_CST
10632 && !integer_all_onesp (op1)));
10633 common = 1;
10635 break;
10637 case TRUTH_ANDIF_EXPR:
10638 case TRUTH_ORIF_EXPR:
10639 case TRUTH_AND_EXPR:
10640 case TRUTH_OR_EXPR:
10641 case TRUTH_XOR_EXPR:
10642 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10643 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10644 || code0 == FIXED_POINT_TYPE)
10645 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10646 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10647 || code1 == FIXED_POINT_TYPE))
10649 /* Result of these operations is always an int,
10650 but that does not mean the operands should be
10651 converted to ints! */
10652 result_type = integer_type_node;
10653 if (op0_int_operands)
10655 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10656 op0 = remove_c_maybe_const_expr (op0);
10658 else
10659 op0 = c_objc_common_truthvalue_conversion (location, op0);
10660 if (op1_int_operands)
10662 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10663 op1 = remove_c_maybe_const_expr (op1);
10665 else
10666 op1 = c_objc_common_truthvalue_conversion (location, op1);
10667 converted = 1;
10668 boolean_op = true;
10670 if (code == TRUTH_ANDIF_EXPR)
10672 int_const_or_overflow = (int_operands
10673 && TREE_CODE (orig_op0) == INTEGER_CST
10674 && (op0 == truthvalue_false_node
10675 || TREE_CODE (orig_op1) == INTEGER_CST));
10676 int_const = (int_const_or_overflow
10677 && !TREE_OVERFLOW (orig_op0)
10678 && (op0 == truthvalue_false_node
10679 || !TREE_OVERFLOW (orig_op1)));
10681 else if (code == TRUTH_ORIF_EXPR)
10683 int_const_or_overflow = (int_operands
10684 && TREE_CODE (orig_op0) == INTEGER_CST
10685 && (op0 == truthvalue_true_node
10686 || TREE_CODE (orig_op1) == INTEGER_CST));
10687 int_const = (int_const_or_overflow
10688 && !TREE_OVERFLOW (orig_op0)
10689 && (op0 == truthvalue_true_node
10690 || !TREE_OVERFLOW (orig_op1)));
10692 break;
10694 /* Shift operations: result has same type as first operand;
10695 always convert second operand to int.
10696 Also set SHORT_SHIFT if shifting rightward. */
10698 case RSHIFT_EXPR:
10699 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10700 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10702 result_type = type0;
10703 converted = 1;
10705 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10706 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10707 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10708 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10710 result_type = type0;
10711 converted = 1;
10713 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10714 && code1 == INTEGER_TYPE)
10716 doing_shift = true;
10717 if (TREE_CODE (op1) == INTEGER_CST)
10719 if (tree_int_cst_sgn (op1) < 0)
10721 int_const = false;
10722 if (c_inhibit_evaluation_warnings == 0)
10723 warning_at (location, OPT_Wshift_count_negative,
10724 "right shift count is negative");
10726 else
10728 if (!integer_zerop (op1))
10729 short_shift = 1;
10731 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10733 int_const = false;
10734 if (c_inhibit_evaluation_warnings == 0)
10735 warning_at (location, OPT_Wshift_count_overflow,
10736 "right shift count >= width of type");
10741 /* Use the type of the value to be shifted. */
10742 result_type = type0;
10743 /* Avoid converting op1 to result_type later. */
10744 converted = 1;
10746 break;
10748 case LSHIFT_EXPR:
10749 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10750 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10752 result_type = type0;
10753 converted = 1;
10755 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10756 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10757 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10758 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10760 result_type = type0;
10761 converted = 1;
10763 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10764 && code1 == INTEGER_TYPE)
10766 doing_shift = true;
10767 if (TREE_CODE (op0) == INTEGER_CST
10768 && tree_int_cst_sgn (op0) < 0)
10770 /* Don't reject a left shift of a negative value in a context
10771 where a constant expression is needed in C90. */
10772 if (flag_isoc99)
10773 int_const = false;
10774 if (c_inhibit_evaluation_warnings == 0)
10775 warning_at (location, OPT_Wshift_negative_value,
10776 "left shift of negative value");
10778 if (TREE_CODE (op1) == INTEGER_CST)
10780 if (tree_int_cst_sgn (op1) < 0)
10782 int_const = false;
10783 if (c_inhibit_evaluation_warnings == 0)
10784 warning_at (location, OPT_Wshift_count_negative,
10785 "left shift count is negative");
10787 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10789 int_const = false;
10790 if (c_inhibit_evaluation_warnings == 0)
10791 warning_at (location, OPT_Wshift_count_overflow,
10792 "left shift count >= width of type");
10794 else if (TREE_CODE (op0) == INTEGER_CST
10795 && maybe_warn_shift_overflow (location, op0, op1)
10796 && flag_isoc99)
10797 int_const = false;
10800 /* Use the type of the value to be shifted. */
10801 result_type = type0;
10802 /* Avoid converting op1 to result_type later. */
10803 converted = 1;
10805 break;
10807 case EQ_EXPR:
10808 case NE_EXPR:
10809 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10811 tree intt;
10812 if (!vector_types_compatible_elements_p (type0, type1))
10814 error_at (location, "comparing vectors with different "
10815 "element types");
10816 return error_mark_node;
10819 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10821 error_at (location, "comparing vectors with different "
10822 "number of elements");
10823 return error_mark_node;
10826 /* Always construct signed integer vector type. */
10827 intt = c_common_type_for_size (GET_MODE_BITSIZE
10828 (TYPE_MODE (TREE_TYPE (type0))), 0);
10829 result_type = build_opaque_vector_type (intt,
10830 TYPE_VECTOR_SUBPARTS (type0));
10831 converted = 1;
10832 ret = build_vec_cmp (resultcode, result_type, op0, op1);
10833 goto return_build_binary_op;
10835 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10836 warning_at (location,
10837 OPT_Wfloat_equal,
10838 "comparing floating point with == or != is unsafe");
10839 /* Result of comparison is always int,
10840 but don't convert the args to int! */
10841 build_type = integer_type_node;
10842 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10843 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10844 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10845 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10846 short_compare = 1;
10847 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10849 if (warn_nonnull
10850 && TREE_CODE (op0) == PARM_DECL && nonnull_arg_p (op0))
10851 warning_at (location, OPT_Wnonnull,
10852 "nonnull argument %qD compared to NULL", op0);
10854 if (TREE_CODE (op0) == ADDR_EXPR
10855 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10857 if (code == EQ_EXPR)
10858 warning_at (location,
10859 OPT_Waddress,
10860 "the comparison will always evaluate as %<false%> "
10861 "for the address of %qD will never be NULL",
10862 TREE_OPERAND (op0, 0));
10863 else
10864 warning_at (location,
10865 OPT_Waddress,
10866 "the comparison will always evaluate as %<true%> "
10867 "for the address of %qD will never be NULL",
10868 TREE_OPERAND (op0, 0));
10870 result_type = type0;
10872 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10874 if (warn_nonnull
10875 && TREE_CODE (op1) == PARM_DECL && nonnull_arg_p (op1))
10876 warning_at (location, OPT_Wnonnull,
10877 "nonnull argument %qD compared to NULL", op1);
10879 if (TREE_CODE (op1) == ADDR_EXPR
10880 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10882 if (code == EQ_EXPR)
10883 warning_at (location,
10884 OPT_Waddress,
10885 "the comparison will always evaluate as %<false%> "
10886 "for the address of %qD will never be NULL",
10887 TREE_OPERAND (op1, 0));
10888 else
10889 warning_at (location,
10890 OPT_Waddress,
10891 "the comparison will always evaluate as %<true%> "
10892 "for the address of %qD will never be NULL",
10893 TREE_OPERAND (op1, 0));
10895 result_type = type1;
10897 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10899 tree tt0 = TREE_TYPE (type0);
10900 tree tt1 = TREE_TYPE (type1);
10901 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10902 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10903 addr_space_t as_common = ADDR_SPACE_GENERIC;
10905 /* Anything compares with void *. void * compares with anything.
10906 Otherwise, the targets must be compatible
10907 and both must be object or both incomplete. */
10908 if (comp_target_types (location, type0, type1))
10909 result_type = common_pointer_type (type0, type1);
10910 else if (!addr_space_superset (as0, as1, &as_common))
10912 error_at (location, "comparison of pointers to "
10913 "disjoint address spaces");
10914 return error_mark_node;
10916 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10918 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10919 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10920 "comparison of %<void *%> with function pointer");
10922 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10924 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10925 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10926 "comparison of %<void *%> with function pointer");
10928 else
10929 /* Avoid warning about the volatile ObjC EH puts on decls. */
10930 if (!objc_ok)
10931 pedwarn (location, 0,
10932 "comparison of distinct pointer types lacks a cast");
10934 if (result_type == NULL_TREE)
10936 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10937 result_type = build_pointer_type
10938 (build_qualified_type (void_type_node, qual));
10941 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10943 result_type = type0;
10944 pedwarn (location, 0, "comparison between pointer and integer");
10946 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10948 result_type = type1;
10949 pedwarn (location, 0, "comparison between pointer and integer");
10951 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10952 || truth_value_p (TREE_CODE (orig_op0)))
10953 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10954 || truth_value_p (TREE_CODE (orig_op1))))
10955 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10956 break;
10958 case LE_EXPR:
10959 case GE_EXPR:
10960 case LT_EXPR:
10961 case GT_EXPR:
10962 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10964 tree intt;
10965 if (!vector_types_compatible_elements_p (type0, type1))
10967 error_at (location, "comparing vectors with different "
10968 "element types");
10969 return error_mark_node;
10972 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10974 error_at (location, "comparing vectors with different "
10975 "number of elements");
10976 return error_mark_node;
10979 /* Always construct signed integer vector type. */
10980 intt = c_common_type_for_size (GET_MODE_BITSIZE
10981 (TYPE_MODE (TREE_TYPE (type0))), 0);
10982 result_type = build_opaque_vector_type (intt,
10983 TYPE_VECTOR_SUBPARTS (type0));
10984 converted = 1;
10985 ret = build_vec_cmp (resultcode, result_type, op0, op1);
10986 goto return_build_binary_op;
10988 build_type = integer_type_node;
10989 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10990 || code0 == FIXED_POINT_TYPE)
10991 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10992 || code1 == FIXED_POINT_TYPE))
10993 short_compare = 1;
10994 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10996 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10997 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10998 addr_space_t as_common;
11000 if (comp_target_types (location, type0, type1))
11002 result_type = common_pointer_type (type0, type1);
11003 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11004 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11005 pedwarn (location, 0,
11006 "comparison of complete and incomplete pointers");
11007 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11008 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11009 "ordered comparisons of pointers to functions");
11010 else if (null_pointer_constant_p (orig_op0)
11011 || null_pointer_constant_p (orig_op1))
11012 warning_at (location, OPT_Wextra,
11013 "ordered comparison of pointer with null pointer");
11016 else if (!addr_space_superset (as0, as1, &as_common))
11018 error_at (location, "comparison of pointers to "
11019 "disjoint address spaces");
11020 return error_mark_node;
11022 else
11024 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11025 result_type = build_pointer_type
11026 (build_qualified_type (void_type_node, qual));
11027 pedwarn (location, 0,
11028 "comparison of distinct pointer types lacks a cast");
11031 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11033 result_type = type0;
11034 if (pedantic)
11035 pedwarn (location, OPT_Wpedantic,
11036 "ordered comparison of pointer with integer zero");
11037 else if (extra_warnings)
11038 warning_at (location, OPT_Wextra,
11039 "ordered comparison of pointer with integer zero");
11041 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11043 result_type = type1;
11044 if (pedantic)
11045 pedwarn (location, OPT_Wpedantic,
11046 "ordered comparison of pointer with integer zero");
11047 else if (extra_warnings)
11048 warning_at (location, OPT_Wextra,
11049 "ordered comparison of pointer with integer zero");
11051 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11053 result_type = type0;
11054 pedwarn (location, 0, "comparison between pointer and integer");
11056 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11058 result_type = type1;
11059 pedwarn (location, 0, "comparison between pointer and integer");
11061 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11062 || truth_value_p (TREE_CODE (orig_op0)))
11063 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11064 || truth_value_p (TREE_CODE (orig_op1))))
11065 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11066 break;
11068 default:
11069 gcc_unreachable ();
11072 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11073 return error_mark_node;
11075 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11076 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11077 || !vector_types_compatible_elements_p (type0, type1)))
11079 binary_op_error (location, code, type0, type1);
11080 return error_mark_node;
11083 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11084 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11086 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11087 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11089 bool first_complex = (code0 == COMPLEX_TYPE);
11090 bool second_complex = (code1 == COMPLEX_TYPE);
11091 int none_complex = (!first_complex && !second_complex);
11093 if (shorten || common || short_compare)
11095 result_type = c_common_type (type0, type1);
11096 do_warn_double_promotion (result_type, type0, type1,
11097 "implicit conversion from %qT to %qT "
11098 "to match other operand of binary "
11099 "expression",
11100 location);
11101 if (result_type == error_mark_node)
11102 return error_mark_node;
11105 if (first_complex != second_complex
11106 && (code == PLUS_EXPR
11107 || code == MINUS_EXPR
11108 || code == MULT_EXPR
11109 || (code == TRUNC_DIV_EXPR && first_complex))
11110 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11111 && flag_signed_zeros)
11113 /* An operation on mixed real/complex operands must be
11114 handled specially, but the language-independent code can
11115 more easily optimize the plain complex arithmetic if
11116 -fno-signed-zeros. */
11117 tree real_type = TREE_TYPE (result_type);
11118 tree real, imag;
11119 if (type0 != orig_type0 || type1 != orig_type1)
11121 gcc_assert (may_need_excess_precision && common);
11122 semantic_result_type = c_common_type (orig_type0, orig_type1);
11124 if (first_complex)
11126 if (TREE_TYPE (op0) != result_type)
11127 op0 = convert_and_check (location, result_type, op0);
11128 if (TREE_TYPE (op1) != real_type)
11129 op1 = convert_and_check (location, real_type, op1);
11131 else
11133 if (TREE_TYPE (op0) != real_type)
11134 op0 = convert_and_check (location, real_type, op0);
11135 if (TREE_TYPE (op1) != result_type)
11136 op1 = convert_and_check (location, result_type, op1);
11138 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11139 return error_mark_node;
11140 if (first_complex)
11142 op0 = c_save_expr (op0);
11143 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11144 op0, 1);
11145 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11146 op0, 1);
11147 switch (code)
11149 case MULT_EXPR:
11150 case TRUNC_DIV_EXPR:
11151 op1 = c_save_expr (op1);
11152 imag = build2 (resultcode, real_type, imag, op1);
11153 /* Fall through. */
11154 case PLUS_EXPR:
11155 case MINUS_EXPR:
11156 real = build2 (resultcode, real_type, real, op1);
11157 break;
11158 default:
11159 gcc_unreachable();
11162 else
11164 op1 = c_save_expr (op1);
11165 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11166 op1, 1);
11167 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11168 op1, 1);
11169 switch (code)
11171 case MULT_EXPR:
11172 op0 = c_save_expr (op0);
11173 imag = build2 (resultcode, real_type, op0, imag);
11174 /* Fall through. */
11175 case PLUS_EXPR:
11176 real = build2 (resultcode, real_type, op0, real);
11177 break;
11178 case MINUS_EXPR:
11179 real = build2 (resultcode, real_type, op0, real);
11180 imag = build1 (NEGATE_EXPR, real_type, imag);
11181 break;
11182 default:
11183 gcc_unreachable();
11186 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11187 goto return_build_binary_op;
11190 /* For certain operations (which identify themselves by shorten != 0)
11191 if both args were extended from the same smaller type,
11192 do the arithmetic in that type and then extend.
11194 shorten !=0 and !=1 indicates a bitwise operation.
11195 For them, this optimization is safe only if
11196 both args are zero-extended or both are sign-extended.
11197 Otherwise, we might change the result.
11198 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11199 but calculated in (unsigned short) it would be (unsigned short)-1. */
11201 if (shorten && none_complex)
11203 final_type = result_type;
11204 result_type = shorten_binary_op (result_type, op0, op1,
11205 shorten == -1);
11208 /* Shifts can be shortened if shifting right. */
11210 if (short_shift)
11212 int unsigned_arg;
11213 tree arg0 = get_narrower (op0, &unsigned_arg);
11215 final_type = result_type;
11217 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11218 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11220 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11221 && tree_int_cst_sgn (op1) > 0
11222 /* We can shorten only if the shift count is less than the
11223 number of bits in the smaller type size. */
11224 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11225 /* We cannot drop an unsigned shift after sign-extension. */
11226 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11228 /* Do an unsigned shift if the operand was zero-extended. */
11229 result_type
11230 = c_common_signed_or_unsigned_type (unsigned_arg,
11231 TREE_TYPE (arg0));
11232 /* Convert value-to-be-shifted to that type. */
11233 if (TREE_TYPE (op0) != result_type)
11234 op0 = convert (result_type, op0);
11235 converted = 1;
11239 /* Comparison operations are shortened too but differently.
11240 They identify themselves by setting short_compare = 1. */
11242 if (short_compare)
11244 /* Don't write &op0, etc., because that would prevent op0
11245 from being kept in a register.
11246 Instead, make copies of the our local variables and
11247 pass the copies by reference, then copy them back afterward. */
11248 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11249 enum tree_code xresultcode = resultcode;
11250 tree val
11251 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11252 &xresultcode);
11254 if (val != 0)
11256 ret = val;
11257 goto return_build_binary_op;
11260 op0 = xop0, op1 = xop1;
11261 converted = 1;
11262 resultcode = xresultcode;
11264 if (c_inhibit_evaluation_warnings == 0)
11266 bool op0_maybe_const = true;
11267 bool op1_maybe_const = true;
11268 tree orig_op0_folded, orig_op1_folded;
11270 if (in_late_binary_op)
11272 orig_op0_folded = orig_op0;
11273 orig_op1_folded = orig_op1;
11275 else
11277 /* Fold for the sake of possible warnings, as in
11278 build_conditional_expr. This requires the
11279 "original" values to be folded, not just op0 and
11280 op1. */
11281 c_inhibit_evaluation_warnings++;
11282 op0 = c_fully_fold (op0, require_constant_value,
11283 &op0_maybe_const);
11284 op1 = c_fully_fold (op1, require_constant_value,
11285 &op1_maybe_const);
11286 c_inhibit_evaluation_warnings--;
11287 orig_op0_folded = c_fully_fold (orig_op0,
11288 require_constant_value,
11289 NULL);
11290 orig_op1_folded = c_fully_fold (orig_op1,
11291 require_constant_value,
11292 NULL);
11295 if (warn_sign_compare)
11296 warn_for_sign_compare (location, orig_op0_folded,
11297 orig_op1_folded, op0, op1,
11298 result_type, resultcode);
11299 if (!in_late_binary_op && !int_operands)
11301 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11302 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11303 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11304 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11310 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11311 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11312 Then the expression will be built.
11313 It will be given type FINAL_TYPE if that is nonzero;
11314 otherwise, it will be given type RESULT_TYPE. */
11316 if (!result_type)
11318 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11319 return error_mark_node;
11322 if (build_type == NULL_TREE)
11324 build_type = result_type;
11325 if ((type0 != orig_type0 || type1 != orig_type1)
11326 && !boolean_op)
11328 gcc_assert (may_need_excess_precision && common);
11329 semantic_result_type = c_common_type (orig_type0, orig_type1);
11333 if (!converted)
11335 op0 = ep_convert_and_check (location, result_type, op0,
11336 semantic_result_type);
11337 op1 = ep_convert_and_check (location, result_type, op1,
11338 semantic_result_type);
11340 /* This can happen if one operand has a vector type, and the other
11341 has a different type. */
11342 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11343 return error_mark_node;
11346 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11347 | SANITIZE_FLOAT_DIVIDE))
11348 && do_ubsan_in_current_function ()
11349 && (doing_div_or_mod || doing_shift)
11350 && !require_constant_value)
11352 /* OP0 and/or OP1 might have side-effects. */
11353 op0 = c_save_expr (op0);
11354 op1 = c_save_expr (op1);
11355 op0 = c_fully_fold (op0, false, NULL);
11356 op1 = c_fully_fold (op1, false, NULL);
11357 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11358 | SANITIZE_FLOAT_DIVIDE)))
11359 instrument_expr = ubsan_instrument_division (location, op0, op1);
11360 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11361 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11364 /* Treat expressions in initializers specially as they can't trap. */
11365 if (int_const_or_overflow)
11366 ret = (require_constant_value
11367 ? fold_build2_initializer_loc (location, resultcode, build_type,
11368 op0, op1)
11369 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11370 else
11371 ret = build2 (resultcode, build_type, op0, op1);
11372 if (final_type != 0)
11373 ret = convert (final_type, ret);
11375 return_build_binary_op:
11376 gcc_assert (ret != error_mark_node);
11377 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11378 ret = (int_operands
11379 ? note_integer_operands (ret)
11380 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11381 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11382 && !in_late_binary_op)
11383 ret = note_integer_operands (ret);
11384 if (semantic_result_type)
11385 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11386 protected_set_expr_location (ret, location);
11388 if (instrument_expr != NULL)
11389 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11390 instrument_expr, ret);
11392 return ret;
11396 /* Convert EXPR to be a truth-value, validating its type for this
11397 purpose. LOCATION is the source location for the expression. */
11399 tree
11400 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11402 bool int_const, int_operands;
11404 switch (TREE_CODE (TREE_TYPE (expr)))
11406 case ARRAY_TYPE:
11407 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11408 return error_mark_node;
11410 case RECORD_TYPE:
11411 error_at (location, "used struct type value where scalar is required");
11412 return error_mark_node;
11414 case UNION_TYPE:
11415 error_at (location, "used union type value where scalar is required");
11416 return error_mark_node;
11418 case VOID_TYPE:
11419 error_at (location, "void value not ignored as it ought to be");
11420 return error_mark_node;
11422 case POINTER_TYPE:
11423 if (reject_gcc_builtin (expr))
11424 return error_mark_node;
11425 break;
11427 case FUNCTION_TYPE:
11428 gcc_unreachable ();
11430 case VECTOR_TYPE:
11431 error_at (location, "used vector type where scalar is required");
11432 return error_mark_node;
11434 default:
11435 break;
11438 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11439 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11440 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11442 expr = remove_c_maybe_const_expr (expr);
11443 expr = build2 (NE_EXPR, integer_type_node, expr,
11444 convert (TREE_TYPE (expr), integer_zero_node));
11445 expr = note_integer_operands (expr);
11447 else
11448 /* ??? Should we also give an error for vectors rather than leaving
11449 those to give errors later? */
11450 expr = c_common_truthvalue_conversion (location, expr);
11452 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11454 if (TREE_OVERFLOW (expr))
11455 return expr;
11456 else
11457 return note_integer_operands (expr);
11459 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11460 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11461 return expr;
11465 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11466 required. */
11468 tree
11469 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11471 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11473 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11474 /* Executing a compound literal inside a function reinitializes
11475 it. */
11476 if (!TREE_STATIC (decl))
11477 *se = true;
11478 return decl;
11480 else
11481 return expr;
11484 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11485 statement. LOC is the location of the construct. */
11487 tree
11488 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11489 tree clauses)
11491 body = c_end_compound_stmt (loc, body, true);
11493 tree stmt = make_node (code);
11494 TREE_TYPE (stmt) = void_type_node;
11495 OMP_BODY (stmt) = body;
11496 OMP_CLAUSES (stmt) = clauses;
11497 SET_EXPR_LOCATION (stmt, loc);
11499 return add_stmt (stmt);
11502 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11503 statement. LOC is the location of the OACC_DATA. */
11505 tree
11506 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11508 tree stmt;
11510 block = c_end_compound_stmt (loc, block, true);
11512 stmt = make_node (OACC_DATA);
11513 TREE_TYPE (stmt) = void_type_node;
11514 OACC_DATA_CLAUSES (stmt) = clauses;
11515 OACC_DATA_BODY (stmt) = block;
11516 SET_EXPR_LOCATION (stmt, loc);
11518 return add_stmt (stmt);
11521 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11523 tree
11524 c_begin_omp_parallel (void)
11526 tree block;
11528 keep_next_level ();
11529 block = c_begin_compound_stmt (true);
11531 return block;
11534 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11535 statement. LOC is the location of the OMP_PARALLEL. */
11537 tree
11538 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11540 tree stmt;
11542 block = c_end_compound_stmt (loc, block, true);
11544 stmt = make_node (OMP_PARALLEL);
11545 TREE_TYPE (stmt) = void_type_node;
11546 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11547 OMP_PARALLEL_BODY (stmt) = block;
11548 SET_EXPR_LOCATION (stmt, loc);
11550 return add_stmt (stmt);
11553 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11555 tree
11556 c_begin_omp_task (void)
11558 tree block;
11560 keep_next_level ();
11561 block = c_begin_compound_stmt (true);
11563 return block;
11566 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11567 statement. LOC is the location of the #pragma. */
11569 tree
11570 c_finish_omp_task (location_t loc, tree clauses, tree block)
11572 tree stmt;
11574 block = c_end_compound_stmt (loc, block, true);
11576 stmt = make_node (OMP_TASK);
11577 TREE_TYPE (stmt) = void_type_node;
11578 OMP_TASK_CLAUSES (stmt) = clauses;
11579 OMP_TASK_BODY (stmt) = block;
11580 SET_EXPR_LOCATION (stmt, loc);
11582 return add_stmt (stmt);
11585 /* Generate GOMP_cancel call for #pragma omp cancel. */
11587 void
11588 c_finish_omp_cancel (location_t loc, tree clauses)
11590 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11591 int mask = 0;
11592 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11593 mask = 1;
11594 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11595 mask = 2;
11596 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11597 mask = 4;
11598 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11599 mask = 8;
11600 else
11602 error_at (loc, "%<#pragma omp cancel must specify one of "
11603 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11604 "clauses");
11605 return;
11607 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11608 if (ifc != NULL_TREE)
11610 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11611 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11612 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11613 build_zero_cst (type));
11615 else
11616 ifc = boolean_true_node;
11617 tree stmt = build_call_expr_loc (loc, fn, 2,
11618 build_int_cst (integer_type_node, mask),
11619 ifc);
11620 add_stmt (stmt);
11623 /* Generate GOMP_cancellation_point call for
11624 #pragma omp cancellation point. */
11626 void
11627 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11629 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11630 int mask = 0;
11631 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11632 mask = 1;
11633 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11634 mask = 2;
11635 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11636 mask = 4;
11637 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11638 mask = 8;
11639 else
11641 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11642 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11643 "clauses");
11644 return;
11646 tree stmt = build_call_expr_loc (loc, fn, 1,
11647 build_int_cst (integer_type_node, mask));
11648 add_stmt (stmt);
11651 /* Helper function for handle_omp_array_sections. Called recursively
11652 to handle multiple array-section-subscripts. C is the clause,
11653 T current expression (initially OMP_CLAUSE_DECL), which is either
11654 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11655 expression if specified, TREE_VALUE length expression if specified,
11656 TREE_CHAIN is what it has been specified after, or some decl.
11657 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11658 set to true if any of the array-section-subscript could have length
11659 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11660 first array-section-subscript which is known not to have length
11661 of one. Given say:
11662 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11663 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11664 all are or may have length of 1, array-section-subscript [:2] is the
11665 first one known not to have length 1. For array-section-subscript
11666 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11667 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11668 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11669 case though, as some lengths could be zero. */
11671 static tree
11672 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11673 bool &maybe_zero_len, unsigned int &first_non_one,
11674 bool is_omp)
11676 tree ret, low_bound, length, type;
11677 if (TREE_CODE (t) != TREE_LIST)
11679 if (error_operand_p (t))
11680 return error_mark_node;
11681 ret = t;
11682 if (TREE_CODE (t) == COMPONENT_REF
11683 && is_omp
11684 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
11685 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
11686 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
11688 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
11690 error_at (OMP_CLAUSE_LOCATION (c),
11691 "bit-field %qE in %qs clause",
11692 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11693 return error_mark_node;
11695 while (TREE_CODE (t) == COMPONENT_REF)
11697 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
11699 error_at (OMP_CLAUSE_LOCATION (c),
11700 "%qE is a member of a union", t);
11701 return error_mark_node;
11703 t = TREE_OPERAND (t, 0);
11706 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
11708 if (DECL_P (t))
11709 error_at (OMP_CLAUSE_LOCATION (c),
11710 "%qD is not a variable in %qs clause", t,
11711 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11712 else
11713 error_at (OMP_CLAUSE_LOCATION (c),
11714 "%qE is not a variable in %qs clause", t,
11715 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11716 return error_mark_node;
11718 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11719 && VAR_P (t) && DECL_THREAD_LOCAL_P (t))
11721 error_at (OMP_CLAUSE_LOCATION (c),
11722 "%qD is threadprivate variable in %qs clause", t,
11723 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11724 return error_mark_node;
11726 return ret;
11729 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11730 maybe_zero_len, first_non_one, is_omp);
11731 if (ret == error_mark_node || ret == NULL_TREE)
11732 return ret;
11734 type = TREE_TYPE (ret);
11735 low_bound = TREE_PURPOSE (t);
11736 length = TREE_VALUE (t);
11738 if (low_bound == error_mark_node || length == error_mark_node)
11739 return error_mark_node;
11741 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11743 error_at (OMP_CLAUSE_LOCATION (c),
11744 "low bound %qE of array section does not have integral type",
11745 low_bound);
11746 return error_mark_node;
11748 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11750 error_at (OMP_CLAUSE_LOCATION (c),
11751 "length %qE of array section does not have integral type",
11752 length);
11753 return error_mark_node;
11755 if (low_bound
11756 && TREE_CODE (low_bound) == INTEGER_CST
11757 && TYPE_PRECISION (TREE_TYPE (low_bound))
11758 > TYPE_PRECISION (sizetype))
11759 low_bound = fold_convert (sizetype, low_bound);
11760 if (length
11761 && TREE_CODE (length) == INTEGER_CST
11762 && TYPE_PRECISION (TREE_TYPE (length))
11763 > TYPE_PRECISION (sizetype))
11764 length = fold_convert (sizetype, length);
11765 if (low_bound == NULL_TREE)
11766 low_bound = integer_zero_node;
11768 if (length != NULL_TREE)
11770 if (!integer_nonzerop (length))
11772 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11773 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
11775 if (integer_zerop (length))
11777 error_at (OMP_CLAUSE_LOCATION (c),
11778 "zero length array section in %qs clause",
11779 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11780 return error_mark_node;
11783 else
11784 maybe_zero_len = true;
11786 if (first_non_one == types.length ()
11787 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11788 first_non_one++;
11790 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
11791 && !integer_zerop (low_bound))
11793 error_at (OMP_CLAUSE_LOCATION (c),
11794 "%<reduction%> array section has to be zero-based");
11795 return error_mark_node;
11797 if (TREE_CODE (type) == ARRAY_TYPE)
11799 if (length == NULL_TREE
11800 && (TYPE_DOMAIN (type) == NULL_TREE
11801 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11803 error_at (OMP_CLAUSE_LOCATION (c),
11804 "for unknown bound array type length expression must "
11805 "be specified");
11806 return error_mark_node;
11808 if (TREE_CODE (low_bound) == INTEGER_CST
11809 && tree_int_cst_sgn (low_bound) == -1)
11811 error_at (OMP_CLAUSE_LOCATION (c),
11812 "negative low bound in array section in %qs clause",
11813 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11814 return error_mark_node;
11816 if (length != NULL_TREE
11817 && TREE_CODE (length) == INTEGER_CST
11818 && tree_int_cst_sgn (length) == -1)
11820 error_at (OMP_CLAUSE_LOCATION (c),
11821 "negative length in array section in %qs clause",
11822 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11823 return error_mark_node;
11825 if (TYPE_DOMAIN (type)
11826 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11827 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11828 == INTEGER_CST)
11830 tree size = size_binop (PLUS_EXPR,
11831 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11832 size_one_node);
11833 if (TREE_CODE (low_bound) == INTEGER_CST)
11835 if (tree_int_cst_lt (size, low_bound))
11837 error_at (OMP_CLAUSE_LOCATION (c),
11838 "low bound %qE above array section size "
11839 "in %qs clause", low_bound,
11840 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11841 return error_mark_node;
11843 if (tree_int_cst_equal (size, low_bound))
11845 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11846 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
11848 error_at (OMP_CLAUSE_LOCATION (c),
11849 "zero length array section in %qs clause",
11850 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11851 return error_mark_node;
11853 maybe_zero_len = true;
11855 else if (length == NULL_TREE
11856 && first_non_one == types.length ()
11857 && tree_int_cst_equal
11858 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11859 low_bound))
11860 first_non_one++;
11862 else if (length == NULL_TREE)
11864 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11865 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
11866 maybe_zero_len = true;
11867 if (first_non_one == types.length ())
11868 first_non_one++;
11870 if (length && TREE_CODE (length) == INTEGER_CST)
11872 if (tree_int_cst_lt (size, length))
11874 error_at (OMP_CLAUSE_LOCATION (c),
11875 "length %qE above array section size "
11876 "in %qs clause", length,
11877 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11878 return error_mark_node;
11880 if (TREE_CODE (low_bound) == INTEGER_CST)
11882 tree lbpluslen
11883 = size_binop (PLUS_EXPR,
11884 fold_convert (sizetype, low_bound),
11885 fold_convert (sizetype, length));
11886 if (TREE_CODE (lbpluslen) == INTEGER_CST
11887 && tree_int_cst_lt (size, lbpluslen))
11889 error_at (OMP_CLAUSE_LOCATION (c),
11890 "high bound %qE above array section size "
11891 "in %qs clause", lbpluslen,
11892 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11893 return error_mark_node;
11898 else if (length == NULL_TREE)
11900 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11901 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
11902 maybe_zero_len = true;
11903 if (first_non_one == types.length ())
11904 first_non_one++;
11907 /* For [lb:] we will need to evaluate lb more than once. */
11908 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11910 tree lb = c_save_expr (low_bound);
11911 if (lb != low_bound)
11913 TREE_PURPOSE (t) = lb;
11914 low_bound = lb;
11918 else if (TREE_CODE (type) == POINTER_TYPE)
11920 if (length == NULL_TREE)
11922 error_at (OMP_CLAUSE_LOCATION (c),
11923 "for pointer type length expression must be specified");
11924 return error_mark_node;
11926 if (length != NULL_TREE
11927 && TREE_CODE (length) == INTEGER_CST
11928 && tree_int_cst_sgn (length) == -1)
11930 error_at (OMP_CLAUSE_LOCATION (c),
11931 "negative length in array section in %qs clause",
11932 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11933 return error_mark_node;
11935 /* If there is a pointer type anywhere but in the very first
11936 array-section-subscript, the array section can't be contiguous. */
11937 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11938 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11940 error_at (OMP_CLAUSE_LOCATION (c),
11941 "array section is not contiguous in %qs clause",
11942 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11943 return error_mark_node;
11946 else
11948 error_at (OMP_CLAUSE_LOCATION (c),
11949 "%qE does not have pointer or array type", ret);
11950 return error_mark_node;
11952 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11953 types.safe_push (TREE_TYPE (ret));
11954 /* We will need to evaluate lb more than once. */
11955 tree lb = c_save_expr (low_bound);
11956 if (lb != low_bound)
11958 TREE_PURPOSE (t) = lb;
11959 low_bound = lb;
11961 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11962 return ret;
11965 /* Handle array sections for clause C. */
11967 static bool
11968 handle_omp_array_sections (tree c, bool is_omp)
11970 bool maybe_zero_len = false;
11971 unsigned int first_non_one = 0;
11972 auto_vec<tree, 10> types;
11973 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11974 maybe_zero_len, first_non_one,
11975 is_omp);
11976 if (first == error_mark_node)
11977 return true;
11978 if (first == NULL_TREE)
11979 return false;
11980 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11982 tree t = OMP_CLAUSE_DECL (c);
11983 tree tem = NULL_TREE;
11984 /* Need to evaluate side effects in the length expressions
11985 if any. */
11986 while (TREE_CODE (t) == TREE_LIST)
11988 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11990 if (tem == NULL_TREE)
11991 tem = TREE_VALUE (t);
11992 else
11993 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11994 TREE_VALUE (t), tem);
11996 t = TREE_CHAIN (t);
11998 if (tem)
11999 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12000 first = c_fully_fold (first, false, NULL);
12001 OMP_CLAUSE_DECL (c) = first;
12003 else
12005 unsigned int num = types.length (), i;
12006 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12007 tree condition = NULL_TREE;
12009 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12010 maybe_zero_len = true;
12012 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12013 t = TREE_CHAIN (t))
12015 tree low_bound = TREE_PURPOSE (t);
12016 tree length = TREE_VALUE (t);
12018 i--;
12019 if (low_bound
12020 && TREE_CODE (low_bound) == INTEGER_CST
12021 && TYPE_PRECISION (TREE_TYPE (low_bound))
12022 > TYPE_PRECISION (sizetype))
12023 low_bound = fold_convert (sizetype, low_bound);
12024 if (length
12025 && TREE_CODE (length) == INTEGER_CST
12026 && TYPE_PRECISION (TREE_TYPE (length))
12027 > TYPE_PRECISION (sizetype))
12028 length = fold_convert (sizetype, length);
12029 if (low_bound == NULL_TREE)
12030 low_bound = integer_zero_node;
12031 if (!maybe_zero_len && i > first_non_one)
12033 if (integer_nonzerop (low_bound))
12034 goto do_warn_noncontiguous;
12035 if (length != NULL_TREE
12036 && TREE_CODE (length) == INTEGER_CST
12037 && TYPE_DOMAIN (types[i])
12038 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12039 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12040 == INTEGER_CST)
12042 tree size;
12043 size = size_binop (PLUS_EXPR,
12044 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12045 size_one_node);
12046 if (!tree_int_cst_equal (length, size))
12048 do_warn_noncontiguous:
12049 error_at (OMP_CLAUSE_LOCATION (c),
12050 "array section is not contiguous in %qs "
12051 "clause",
12052 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12053 return true;
12056 if (length != NULL_TREE
12057 && TREE_SIDE_EFFECTS (length))
12059 if (side_effects == NULL_TREE)
12060 side_effects = length;
12061 else
12062 side_effects = build2 (COMPOUND_EXPR,
12063 TREE_TYPE (side_effects),
12064 length, side_effects);
12067 else
12069 tree l;
12071 if (i > first_non_one
12072 && ((length && integer_nonzerop (length))
12073 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12074 continue;
12075 if (length)
12076 l = fold_convert (sizetype, length);
12077 else
12079 l = size_binop (PLUS_EXPR,
12080 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12081 size_one_node);
12082 l = size_binop (MINUS_EXPR, l,
12083 fold_convert (sizetype, low_bound));
12085 if (i > first_non_one)
12087 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12088 size_zero_node);
12089 if (condition == NULL_TREE)
12090 condition = l;
12091 else
12092 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12093 l, condition);
12095 else if (size == NULL_TREE)
12097 size = size_in_bytes (TREE_TYPE (types[i]));
12098 tree eltype = TREE_TYPE (types[num - 1]);
12099 while (TREE_CODE (eltype) == ARRAY_TYPE)
12100 eltype = TREE_TYPE (eltype);
12101 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12103 if (integer_zerop (size)
12104 || integer_zerop (size_in_bytes (eltype)))
12106 error_at (OMP_CLAUSE_LOCATION (c),
12107 "zero length array section in %qs clause",
12108 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12109 return error_mark_node;
12111 size = size_binop (EXACT_DIV_EXPR, size,
12112 size_in_bytes (eltype));
12114 size = size_binop (MULT_EXPR, size, l);
12115 if (condition)
12116 size = fold_build3 (COND_EXPR, sizetype, condition,
12117 size, size_zero_node);
12119 else
12120 size = size_binop (MULT_EXPR, size, l);
12123 if (side_effects)
12124 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12125 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12127 size = size_binop (MINUS_EXPR, size, size_one_node);
12128 size = c_fully_fold (size, false, NULL);
12129 tree index_type = build_index_type (size);
12130 tree eltype = TREE_TYPE (first);
12131 while (TREE_CODE (eltype) == ARRAY_TYPE)
12132 eltype = TREE_TYPE (eltype);
12133 tree type = build_array_type (eltype, index_type);
12134 tree ptype = build_pointer_type (eltype);
12135 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12136 t = build_fold_addr_expr (t);
12137 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12138 OMP_CLAUSE_DECL (c) = t;
12139 return false;
12141 first = c_fully_fold (first, false, NULL);
12142 OMP_CLAUSE_DECL (c) = first;
12143 if (size)
12144 size = c_fully_fold (size, false, NULL);
12145 OMP_CLAUSE_SIZE (c) = size;
12146 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12147 || (TREE_CODE (t) == COMPONENT_REF
12148 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12149 return false;
12150 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12151 if (is_omp)
12152 switch (OMP_CLAUSE_MAP_KIND (c))
12154 case GOMP_MAP_ALLOC:
12155 case GOMP_MAP_TO:
12156 case GOMP_MAP_FROM:
12157 case GOMP_MAP_TOFROM:
12158 case GOMP_MAP_ALWAYS_TO:
12159 case GOMP_MAP_ALWAYS_FROM:
12160 case GOMP_MAP_ALWAYS_TOFROM:
12161 case GOMP_MAP_RELEASE:
12162 case GOMP_MAP_DELETE:
12163 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12164 break;
12165 default:
12166 break;
12168 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12169 OMP_CLAUSE_SET_MAP_KIND (c2, is_omp
12170 ? GOMP_MAP_FIRSTPRIVATE_POINTER
12171 : GOMP_MAP_POINTER);
12172 if (!is_omp && !c_mark_addressable (t))
12173 return false;
12174 OMP_CLAUSE_DECL (c2) = t;
12175 t = build_fold_addr_expr (first);
12176 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12177 tree ptr = OMP_CLAUSE_DECL (c2);
12178 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12179 ptr = build_fold_addr_expr (ptr);
12180 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12181 ptrdiff_type_node, t,
12182 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12183 ptrdiff_type_node, ptr));
12184 t = c_fully_fold (t, false, NULL);
12185 OMP_CLAUSE_SIZE (c2) = t;
12186 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12187 OMP_CLAUSE_CHAIN (c) = c2;
12189 return false;
12192 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12193 an inline call. But, remap
12194 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12195 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12197 static tree
12198 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12199 tree decl, tree placeholder)
12201 copy_body_data id;
12202 hash_map<tree, tree> decl_map;
12204 decl_map.put (omp_decl1, placeholder);
12205 decl_map.put (omp_decl2, decl);
12206 memset (&id, 0, sizeof (id));
12207 id.src_fn = DECL_CONTEXT (omp_decl1);
12208 id.dst_fn = current_function_decl;
12209 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12210 id.decl_map = &decl_map;
12212 id.copy_decl = copy_decl_no_change;
12213 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12214 id.transform_new_cfg = true;
12215 id.transform_return_to_modify = false;
12216 id.transform_lang_insert_block = NULL;
12217 id.eh_lp_nr = 0;
12218 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12219 return stmt;
12222 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12223 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12225 static tree
12226 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12228 if (*tp == (tree) data)
12229 return *tp;
12230 return NULL_TREE;
12233 /* For all elements of CLAUSES, validate them against their constraints.
12234 Remove any elements from the list that are invalid. */
12236 tree
12237 c_finish_omp_clauses (tree clauses, bool is_omp, bool declare_simd)
12239 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12240 bitmap_head aligned_head, map_head, map_field_head, generic_field_head;
12241 tree c, t, type, *pc;
12242 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12243 bool branch_seen = false;
12244 bool copyprivate_seen = false;
12245 tree *nowait_clause = NULL;
12247 bitmap_obstack_initialize (NULL);
12248 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12249 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12250 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12251 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12252 bitmap_initialize (&map_head, &bitmap_default_obstack);
12253 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12254 bitmap_initialize (&generic_field_head, &bitmap_default_obstack);
12256 for (pc = &clauses, c = clauses; c ; c = *pc)
12258 bool remove = false;
12259 bool need_complete = false;
12260 bool need_implicitly_determined = false;
12262 switch (OMP_CLAUSE_CODE (c))
12264 case OMP_CLAUSE_SHARED:
12265 need_implicitly_determined = true;
12266 goto check_dup_generic;
12268 case OMP_CLAUSE_PRIVATE:
12269 need_complete = true;
12270 need_implicitly_determined = true;
12271 goto check_dup_generic;
12273 case OMP_CLAUSE_REDUCTION:
12274 need_implicitly_determined = true;
12275 t = OMP_CLAUSE_DECL (c);
12276 if (TREE_CODE (t) == TREE_LIST)
12278 if (handle_omp_array_sections (c, is_omp))
12280 remove = true;
12281 break;
12284 t = OMP_CLAUSE_DECL (c);
12286 t = require_complete_type (t);
12287 if (t == error_mark_node)
12289 remove = true;
12290 break;
12292 type = TREE_TYPE (t);
12293 if (TREE_CODE (t) == MEM_REF)
12294 type = TREE_TYPE (type);
12295 if (TREE_CODE (type) == ARRAY_TYPE)
12297 tree oatype = type;
12298 gcc_assert (TREE_CODE (t) != MEM_REF);
12299 while (TREE_CODE (type) == ARRAY_TYPE)
12300 type = TREE_TYPE (type);
12301 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12303 error_at (OMP_CLAUSE_LOCATION (c),
12304 "%qD in %<reduction%> clause is a zero size array",
12306 remove = true;
12307 break;
12309 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12310 TYPE_SIZE_UNIT (type));
12311 if (integer_zerop (size))
12313 error_at (OMP_CLAUSE_LOCATION (c),
12314 "%qD in %<reduction%> clause is a zero size array",
12316 remove = true;
12317 break;
12319 size = size_binop (MINUS_EXPR, size, size_one_node);
12320 tree index_type = build_index_type (size);
12321 tree atype = build_array_type (type, index_type);
12322 tree ptype = build_pointer_type (type);
12323 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12324 t = build_fold_addr_expr (t);
12325 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12326 OMP_CLAUSE_DECL (c) = t;
12328 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12329 && (FLOAT_TYPE_P (type)
12330 || TREE_CODE (type) == COMPLEX_TYPE))
12332 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12333 const char *r_name = NULL;
12335 switch (r_code)
12337 case PLUS_EXPR:
12338 case MULT_EXPR:
12339 case MINUS_EXPR:
12340 break;
12341 case MIN_EXPR:
12342 if (TREE_CODE (type) == COMPLEX_TYPE)
12343 r_name = "min";
12344 break;
12345 case MAX_EXPR:
12346 if (TREE_CODE (type) == COMPLEX_TYPE)
12347 r_name = "max";
12348 break;
12349 case BIT_AND_EXPR:
12350 r_name = "&";
12351 break;
12352 case BIT_XOR_EXPR:
12353 r_name = "^";
12354 break;
12355 case BIT_IOR_EXPR:
12356 r_name = "|";
12357 break;
12358 case TRUTH_ANDIF_EXPR:
12359 if (FLOAT_TYPE_P (type))
12360 r_name = "&&";
12361 break;
12362 case TRUTH_ORIF_EXPR:
12363 if (FLOAT_TYPE_P (type))
12364 r_name = "||";
12365 break;
12366 default:
12367 gcc_unreachable ();
12369 if (r_name)
12371 error_at (OMP_CLAUSE_LOCATION (c),
12372 "%qE has invalid type for %<reduction(%s)%>",
12373 t, r_name);
12374 remove = true;
12375 break;
12378 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12380 error_at (OMP_CLAUSE_LOCATION (c),
12381 "user defined reduction not found for %qE", t);
12382 remove = true;
12383 break;
12385 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12387 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12388 type = TYPE_MAIN_VARIANT (type);
12389 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12390 VAR_DECL, NULL_TREE, type);
12391 tree decl_placeholder = NULL_TREE;
12392 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12393 DECL_ARTIFICIAL (placeholder) = 1;
12394 DECL_IGNORED_P (placeholder) = 1;
12395 if (TREE_CODE (t) == MEM_REF)
12397 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12398 VAR_DECL, NULL_TREE, type);
12399 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12400 DECL_ARTIFICIAL (decl_placeholder) = 1;
12401 DECL_IGNORED_P (decl_placeholder) = 1;
12403 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12404 c_mark_addressable (placeholder);
12405 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12406 c_mark_addressable (decl_placeholder ? decl_placeholder
12407 : OMP_CLAUSE_DECL (c));
12408 OMP_CLAUSE_REDUCTION_MERGE (c)
12409 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12410 TREE_VEC_ELT (list, 0),
12411 TREE_VEC_ELT (list, 1),
12412 decl_placeholder ? decl_placeholder
12413 : OMP_CLAUSE_DECL (c), placeholder);
12414 OMP_CLAUSE_REDUCTION_MERGE (c)
12415 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12416 void_type_node, NULL_TREE,
12417 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12418 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12419 if (TREE_VEC_LENGTH (list) == 6)
12421 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12422 c_mark_addressable (decl_placeholder ? decl_placeholder
12423 : OMP_CLAUSE_DECL (c));
12424 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12425 c_mark_addressable (placeholder);
12426 tree init = TREE_VEC_ELT (list, 5);
12427 if (init == error_mark_node)
12428 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12429 OMP_CLAUSE_REDUCTION_INIT (c)
12430 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12431 TREE_VEC_ELT (list, 3),
12432 decl_placeholder ? decl_placeholder
12433 : OMP_CLAUSE_DECL (c), placeholder);
12434 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12436 tree v = decl_placeholder ? decl_placeholder : t;
12437 OMP_CLAUSE_REDUCTION_INIT (c)
12438 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12439 OMP_CLAUSE_REDUCTION_INIT (c));
12441 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12442 c_find_omp_placeholder_r,
12443 placeholder, NULL))
12444 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12446 else
12448 tree init;
12449 tree v = decl_placeholder ? decl_placeholder : t;
12450 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
12451 init = build_constructor (TREE_TYPE (v), NULL);
12452 else
12453 init = fold_convert (TREE_TYPE (v), integer_zero_node);
12454 OMP_CLAUSE_REDUCTION_INIT (c)
12455 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
12457 OMP_CLAUSE_REDUCTION_INIT (c)
12458 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12459 void_type_node, NULL_TREE,
12460 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12461 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12463 if (TREE_CODE (t) == MEM_REF)
12465 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
12466 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
12467 != INTEGER_CST)
12469 sorry ("variable length element type in array "
12470 "%<reduction%> clause");
12471 remove = true;
12472 break;
12474 t = TREE_OPERAND (t, 0);
12475 if (TREE_CODE (t) == ADDR_EXPR)
12476 t = TREE_OPERAND (t, 0);
12478 goto check_dup_generic_t;
12480 case OMP_CLAUSE_COPYPRIVATE:
12481 copyprivate_seen = true;
12482 if (nowait_clause)
12484 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12485 "%<nowait%> clause must not be used together "
12486 "with %<copyprivate%>");
12487 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12488 nowait_clause = NULL;
12490 goto check_dup_generic;
12492 case OMP_CLAUSE_COPYIN:
12493 t = OMP_CLAUSE_DECL (c);
12494 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
12496 error_at (OMP_CLAUSE_LOCATION (c),
12497 "%qE must be %<threadprivate%> for %<copyin%>", t);
12498 remove = true;
12499 break;
12501 goto check_dup_generic;
12503 case OMP_CLAUSE_LINEAR:
12504 if (!declare_simd)
12505 need_implicitly_determined = true;
12506 t = OMP_CLAUSE_DECL (c);
12507 if (!declare_simd
12508 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
12510 error_at (OMP_CLAUSE_LOCATION (c),
12511 "modifier should not be specified in %<linear%> "
12512 "clause on %<simd%> or %<for%> constructs");
12513 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
12515 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12516 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12518 error_at (OMP_CLAUSE_LOCATION (c),
12519 "linear clause applied to non-integral non-pointer "
12520 "variable with type %qT", TREE_TYPE (t));
12521 remove = true;
12522 break;
12524 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12526 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12527 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12528 OMP_CLAUSE_DECL (c), s);
12529 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12530 sizetype, fold_convert (sizetype, s),
12531 fold_convert
12532 (sizetype, OMP_CLAUSE_DECL (c)));
12533 if (s == error_mark_node)
12534 s = size_one_node;
12535 OMP_CLAUSE_LINEAR_STEP (c) = s;
12537 else
12538 OMP_CLAUSE_LINEAR_STEP (c)
12539 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12540 goto check_dup_generic;
12542 check_dup_generic:
12543 t = OMP_CLAUSE_DECL (c);
12544 check_dup_generic_t:
12545 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12547 error_at (OMP_CLAUSE_LOCATION (c),
12548 "%qE is not a variable in clause %qs", t,
12549 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12550 remove = true;
12552 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12553 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12554 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12556 error_at (OMP_CLAUSE_LOCATION (c),
12557 "%qE appears more than once in data clauses", t);
12558 remove = true;
12560 else
12561 bitmap_set_bit (&generic_head, DECL_UID (t));
12562 break;
12564 case OMP_CLAUSE_FIRSTPRIVATE:
12565 t = OMP_CLAUSE_DECL (c);
12566 need_complete = true;
12567 need_implicitly_determined = true;
12568 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12570 error_at (OMP_CLAUSE_LOCATION (c),
12571 "%qE is not a variable in clause %<firstprivate%>", t);
12572 remove = true;
12574 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12575 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12577 error_at (OMP_CLAUSE_LOCATION (c),
12578 "%qE appears more than once in data clauses", t);
12579 remove = true;
12581 else
12582 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12583 break;
12585 case OMP_CLAUSE_LASTPRIVATE:
12586 t = OMP_CLAUSE_DECL (c);
12587 need_complete = true;
12588 need_implicitly_determined = true;
12589 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12591 error_at (OMP_CLAUSE_LOCATION (c),
12592 "%qE is not a variable in clause %<lastprivate%>", t);
12593 remove = true;
12595 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12596 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12598 error_at (OMP_CLAUSE_LOCATION (c),
12599 "%qE appears more than once in data clauses", t);
12600 remove = true;
12602 else
12603 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12604 break;
12606 case OMP_CLAUSE_ALIGNED:
12607 t = OMP_CLAUSE_DECL (c);
12608 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12610 error_at (OMP_CLAUSE_LOCATION (c),
12611 "%qE is not a variable in %<aligned%> clause", t);
12612 remove = true;
12614 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12615 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12617 error_at (OMP_CLAUSE_LOCATION (c),
12618 "%qE in %<aligned%> clause is neither a pointer nor "
12619 "an array", t);
12620 remove = true;
12622 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12624 error_at (OMP_CLAUSE_LOCATION (c),
12625 "%qE appears more than once in %<aligned%> clauses",
12627 remove = true;
12629 else
12630 bitmap_set_bit (&aligned_head, DECL_UID (t));
12631 break;
12633 case OMP_CLAUSE_DEPEND:
12634 t = OMP_CLAUSE_DECL (c);
12635 if (t == NULL_TREE)
12637 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
12638 == OMP_CLAUSE_DEPEND_SOURCE);
12639 break;
12641 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
12643 gcc_assert (TREE_CODE (t) == TREE_LIST);
12644 for (; t; t = TREE_CHAIN (t))
12646 tree decl = TREE_VALUE (t);
12647 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
12649 tree offset = TREE_PURPOSE (t);
12650 bool neg = wi::neg_p ((wide_int) offset);
12651 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
12652 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
12653 neg ? MINUS_EXPR : PLUS_EXPR,
12654 decl, offset);
12655 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12656 sizetype,
12657 fold_convert (sizetype, t2),
12658 fold_convert (sizetype, decl));
12659 if (t2 == error_mark_node)
12661 remove = true;
12662 break;
12664 TREE_PURPOSE (t) = t2;
12667 break;
12669 if (TREE_CODE (t) == TREE_LIST)
12671 if (handle_omp_array_sections (c, is_omp))
12672 remove = true;
12673 break;
12675 if (t == error_mark_node)
12676 remove = true;
12677 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12679 error_at (OMP_CLAUSE_LOCATION (c),
12680 "%qE is not a variable in %<depend%> clause", t);
12681 remove = true;
12683 else if (!c_mark_addressable (t))
12684 remove = true;
12685 break;
12687 case OMP_CLAUSE_MAP:
12688 case OMP_CLAUSE_TO:
12689 case OMP_CLAUSE_FROM:
12690 case OMP_CLAUSE__CACHE_:
12691 t = OMP_CLAUSE_DECL (c);
12692 if (TREE_CODE (t) == TREE_LIST)
12694 if (handle_omp_array_sections (c, is_omp))
12695 remove = true;
12696 else
12698 t = OMP_CLAUSE_DECL (c);
12699 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12701 error_at (OMP_CLAUSE_LOCATION (c),
12702 "array section does not have mappable type "
12703 "in %qs clause",
12704 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12705 remove = true;
12707 while (TREE_CODE (t) == ARRAY_REF)
12708 t = TREE_OPERAND (t, 0);
12709 if (TREE_CODE (t) == COMPONENT_REF
12710 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12712 while (TREE_CODE (t) == COMPONENT_REF)
12713 t = TREE_OPERAND (t, 0);
12714 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
12715 break;
12716 if (bitmap_bit_p (&map_head, DECL_UID (t)))
12718 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12719 error ("%qD appears more than once in motion"
12720 " clauses", t);
12721 else
12722 error ("%qD appears more than once in map"
12723 " clauses", t);
12724 remove = true;
12726 else
12728 bitmap_set_bit (&map_head, DECL_UID (t));
12729 bitmap_set_bit (&map_field_head, DECL_UID (t));
12733 break;
12735 if (t == error_mark_node)
12737 remove = true;
12738 break;
12740 if (TREE_CODE (t) == COMPONENT_REF
12741 && is_omp
12742 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
12744 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12746 error_at (OMP_CLAUSE_LOCATION (c),
12747 "bit-field %qE in %qs clause",
12748 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12749 remove = true;
12751 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12753 error_at (OMP_CLAUSE_LOCATION (c),
12754 "%qE does not have a mappable type in %qs clause",
12755 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12756 remove = true;
12758 while (TREE_CODE (t) == COMPONENT_REF)
12760 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
12761 == UNION_TYPE)
12763 error_at (OMP_CLAUSE_LOCATION (c),
12764 "%qE is a member of a union", t);
12765 remove = true;
12766 break;
12768 t = TREE_OPERAND (t, 0);
12770 if (remove)
12771 break;
12772 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
12774 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12775 && (OMP_CLAUSE_MAP_KIND (c)
12776 == GOMP_MAP_FIRSTPRIVATE_POINTER))
12778 if (bitmap_bit_p (&generic_field_head, DECL_UID (t)))
12779 break;
12781 else if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
12782 break;
12785 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12787 error_at (OMP_CLAUSE_LOCATION (c),
12788 "%qE is not a variable in %qs clause", t,
12789 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12790 remove = true;
12792 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
12794 error_at (OMP_CLAUSE_LOCATION (c),
12795 "%qD is threadprivate variable in %qs clause", t,
12796 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12797 remove = true;
12799 else if (!c_mark_addressable (t))
12800 remove = true;
12801 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12802 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12803 || (OMP_CLAUSE_MAP_KIND (c)
12804 == GOMP_MAP_FIRSTPRIVATE_POINTER)
12805 || (OMP_CLAUSE_MAP_KIND (c)
12806 == GOMP_MAP_FORCE_DEVICEPTR)))
12807 && t == OMP_CLAUSE_DECL (c)
12808 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12810 error_at (OMP_CLAUSE_LOCATION (c),
12811 "%qD does not have a mappable type in %qs clause", t,
12812 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12813 remove = true;
12815 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12816 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
12818 if (bitmap_bit_p (&generic_head, DECL_UID (t))
12819 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12821 error ("%qD appears more than once in data clauses", t);
12822 remove = true;
12824 else
12826 bitmap_set_bit (&generic_head, DECL_UID (t));
12827 if (t != OMP_CLAUSE_DECL (c)
12828 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
12829 bitmap_set_bit (&generic_field_head, DECL_UID (t));
12832 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
12834 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12835 error ("%qD appears more than once in motion clauses", t);
12836 else
12837 error ("%qD appears more than once in map clauses", t);
12838 remove = true;
12840 else
12842 bitmap_set_bit (&map_head, DECL_UID (t));
12843 if (t != OMP_CLAUSE_DECL (c)
12844 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
12845 bitmap_set_bit (&map_field_head, DECL_UID (t));
12847 break;
12849 case OMP_CLAUSE_TO_DECLARE:
12850 t = OMP_CLAUSE_DECL (c);
12851 if (TREE_CODE (t) == FUNCTION_DECL)
12852 break;
12853 /* FALLTHRU */
12854 case OMP_CLAUSE_LINK:
12855 t = OMP_CLAUSE_DECL (c);
12856 if (!VAR_P (t))
12858 error_at (OMP_CLAUSE_LOCATION (c),
12859 "%qE is not a variable in clause %qs", t,
12860 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12861 remove = true;
12863 else if (DECL_THREAD_LOCAL_P (t))
12865 error_at (OMP_CLAUSE_LOCATION (c),
12866 "%qD is threadprivate variable in %qs clause", t,
12867 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12868 remove = true;
12870 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12872 error_at (OMP_CLAUSE_LOCATION (c),
12873 "%qD does not have a mappable type in %qs clause", t,
12874 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12875 remove = true;
12877 break;
12879 case OMP_CLAUSE_UNIFORM:
12880 t = OMP_CLAUSE_DECL (c);
12881 if (TREE_CODE (t) != PARM_DECL)
12883 if (DECL_P (t))
12884 error_at (OMP_CLAUSE_LOCATION (c),
12885 "%qD is not an argument in %<uniform%> clause", t);
12886 else
12887 error_at (OMP_CLAUSE_LOCATION (c),
12888 "%qE is not an argument in %<uniform%> clause", t);
12889 remove = true;
12890 break;
12892 goto check_dup_generic;
12894 case OMP_CLAUSE_IS_DEVICE_PTR:
12895 case OMP_CLAUSE_USE_DEVICE_PTR:
12896 t = OMP_CLAUSE_DECL (c);
12897 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
12898 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12900 error_at (OMP_CLAUSE_LOCATION (c),
12901 "%qs variable is neither a pointer nor an array",
12902 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12903 remove = true;
12905 goto check_dup_generic;
12907 case OMP_CLAUSE_NOWAIT:
12908 if (copyprivate_seen)
12910 error_at (OMP_CLAUSE_LOCATION (c),
12911 "%<nowait%> clause must not be used together "
12912 "with %<copyprivate%>");
12913 remove = true;
12914 break;
12916 nowait_clause = pc;
12917 pc = &OMP_CLAUSE_CHAIN (c);
12918 continue;
12920 case OMP_CLAUSE_IF:
12921 case OMP_CLAUSE_NUM_THREADS:
12922 case OMP_CLAUSE_NUM_TEAMS:
12923 case OMP_CLAUSE_THREAD_LIMIT:
12924 case OMP_CLAUSE_SCHEDULE:
12925 case OMP_CLAUSE_ORDERED:
12926 case OMP_CLAUSE_DEFAULT:
12927 case OMP_CLAUSE_UNTIED:
12928 case OMP_CLAUSE_COLLAPSE:
12929 case OMP_CLAUSE_FINAL:
12930 case OMP_CLAUSE_MERGEABLE:
12931 case OMP_CLAUSE_DEVICE:
12932 case OMP_CLAUSE_DIST_SCHEDULE:
12933 case OMP_CLAUSE_PARALLEL:
12934 case OMP_CLAUSE_FOR:
12935 case OMP_CLAUSE_SECTIONS:
12936 case OMP_CLAUSE_TASKGROUP:
12937 case OMP_CLAUSE_PROC_BIND:
12938 case OMP_CLAUSE_PRIORITY:
12939 case OMP_CLAUSE_GRAINSIZE:
12940 case OMP_CLAUSE_NUM_TASKS:
12941 case OMP_CLAUSE_NOGROUP:
12942 case OMP_CLAUSE_THREADS:
12943 case OMP_CLAUSE_SIMD:
12944 case OMP_CLAUSE_HINT:
12945 case OMP_CLAUSE_DEFAULTMAP:
12946 case OMP_CLAUSE__CILK_FOR_COUNT_:
12947 case OMP_CLAUSE_NUM_GANGS:
12948 case OMP_CLAUSE_NUM_WORKERS:
12949 case OMP_CLAUSE_VECTOR_LENGTH:
12950 case OMP_CLAUSE_ASYNC:
12951 case OMP_CLAUSE_WAIT:
12952 case OMP_CLAUSE_AUTO:
12953 case OMP_CLAUSE_SEQ:
12954 case OMP_CLAUSE_GANG:
12955 case OMP_CLAUSE_WORKER:
12956 case OMP_CLAUSE_VECTOR:
12957 pc = &OMP_CLAUSE_CHAIN (c);
12958 continue;
12960 case OMP_CLAUSE_SAFELEN:
12961 safelen = c;
12962 pc = &OMP_CLAUSE_CHAIN (c);
12963 continue;
12964 case OMP_CLAUSE_SIMDLEN:
12965 simdlen = c;
12966 pc = &OMP_CLAUSE_CHAIN (c);
12967 continue;
12969 case OMP_CLAUSE_INBRANCH:
12970 case OMP_CLAUSE_NOTINBRANCH:
12971 if (branch_seen)
12973 error_at (OMP_CLAUSE_LOCATION (c),
12974 "%<inbranch%> clause is incompatible with "
12975 "%<notinbranch%>");
12976 remove = true;
12977 break;
12979 branch_seen = true;
12980 pc = &OMP_CLAUSE_CHAIN (c);
12981 continue;
12983 default:
12984 gcc_unreachable ();
12987 if (!remove)
12989 t = OMP_CLAUSE_DECL (c);
12991 if (need_complete)
12993 t = require_complete_type (t);
12994 if (t == error_mark_node)
12995 remove = true;
12998 if (need_implicitly_determined)
13000 const char *share_name = NULL;
13002 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13003 share_name = "threadprivate";
13004 else switch (c_omp_predetermined_sharing (t))
13006 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13007 break;
13008 case OMP_CLAUSE_DEFAULT_SHARED:
13009 /* const vars may be specified in firstprivate clause. */
13010 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13011 && TREE_READONLY (t))
13012 break;
13013 share_name = "shared";
13014 break;
13015 case OMP_CLAUSE_DEFAULT_PRIVATE:
13016 share_name = "private";
13017 break;
13018 default:
13019 gcc_unreachable ();
13021 if (share_name)
13023 error_at (OMP_CLAUSE_LOCATION (c),
13024 "%qE is predetermined %qs for %qs",
13025 t, share_name,
13026 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13027 remove = true;
13032 if (remove)
13033 *pc = OMP_CLAUSE_CHAIN (c);
13034 else
13035 pc = &OMP_CLAUSE_CHAIN (c);
13038 if (simdlen
13039 && safelen
13040 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13041 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13043 error_at (OMP_CLAUSE_LOCATION (simdlen),
13044 "%<simdlen%> clause value is bigger than "
13045 "%<safelen%> clause value");
13046 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13047 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13050 bitmap_obstack_release (NULL);
13051 return clauses;
13054 /* Create a transaction node. */
13056 tree
13057 c_finish_transaction (location_t loc, tree block, int flags)
13059 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13060 if (flags & TM_STMT_ATTR_OUTER)
13061 TRANSACTION_EXPR_OUTER (stmt) = 1;
13062 if (flags & TM_STMT_ATTR_RELAXED)
13063 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13064 return add_stmt (stmt);
13067 /* Make a variant type in the proper way for C/C++, propagating qualifiers
13068 down to the element type of an array. */
13070 tree
13071 c_build_qualified_type (tree type, int type_quals)
13073 if (type == error_mark_node)
13074 return type;
13076 if (TREE_CODE (type) == ARRAY_TYPE)
13078 tree t;
13079 tree element_type = c_build_qualified_type (TREE_TYPE (type),
13080 type_quals);
13082 /* See if we already have an identically qualified type. */
13083 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13085 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13086 && TYPE_NAME (t) == TYPE_NAME (type)
13087 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13088 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13089 TYPE_ATTRIBUTES (type)))
13090 break;
13092 if (!t)
13094 tree domain = TYPE_DOMAIN (type);
13096 t = build_variant_type_copy (type);
13097 TREE_TYPE (t) = element_type;
13099 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13100 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13101 SET_TYPE_STRUCTURAL_EQUALITY (t);
13102 else if (TYPE_CANONICAL (element_type) != element_type
13103 || (domain && TYPE_CANONICAL (domain) != domain))
13105 tree unqualified_canon
13106 = build_array_type (TYPE_CANONICAL (element_type),
13107 domain? TYPE_CANONICAL (domain)
13108 : NULL_TREE);
13109 TYPE_CANONICAL (t)
13110 = c_build_qualified_type (unqualified_canon, type_quals);
13112 else
13113 TYPE_CANONICAL (t) = t;
13115 return t;
13118 /* A restrict-qualified pointer type must be a pointer to object or
13119 incomplete type. Note that the use of POINTER_TYPE_P also allows
13120 REFERENCE_TYPEs, which is appropriate for C++. */
13121 if ((type_quals & TYPE_QUAL_RESTRICT)
13122 && (!POINTER_TYPE_P (type)
13123 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13125 error ("invalid use of %<restrict%>");
13126 type_quals &= ~TYPE_QUAL_RESTRICT;
13129 tree var_type = build_qualified_type (type, type_quals);
13130 /* A variant type does not inherit the list of incomplete vars from the
13131 type main variant. */
13132 if (TREE_CODE (var_type) == RECORD_TYPE
13133 || TREE_CODE (var_type) == UNION_TYPE)
13134 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13135 return var_type;
13138 /* Build a VA_ARG_EXPR for the C parser. */
13140 tree
13141 c_build_va_arg (location_t loc, tree expr, tree type)
13143 if (error_operand_p (type))
13144 return error_mark_node;
13145 else if (!COMPLETE_TYPE_P (type))
13147 error_at (loc, "second argument to %<va_arg%> is of incomplete "
13148 "type %qT", type);
13149 return error_mark_node;
13151 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
13152 warning_at (loc, OPT_Wc___compat,
13153 "C++ requires promoted type, not enum type, in %<va_arg%>");
13154 return build_va_arg (loc, expr, type);
13157 /* Return truthvalue of whether T1 is the same tree structure as T2.
13158 Return 1 if they are the same. Return 0 if they are different. */
13160 bool
13161 c_tree_equal (tree t1, tree t2)
13163 enum tree_code code1, code2;
13165 if (t1 == t2)
13166 return true;
13167 if (!t1 || !t2)
13168 return false;
13170 for (code1 = TREE_CODE (t1);
13171 CONVERT_EXPR_CODE_P (code1)
13172 || code1 == NON_LVALUE_EXPR;
13173 code1 = TREE_CODE (t1))
13174 t1 = TREE_OPERAND (t1, 0);
13175 for (code2 = TREE_CODE (t2);
13176 CONVERT_EXPR_CODE_P (code2)
13177 || code2 == NON_LVALUE_EXPR;
13178 code2 = TREE_CODE (t2))
13179 t2 = TREE_OPERAND (t2, 0);
13181 /* They might have become equal now. */
13182 if (t1 == t2)
13183 return true;
13185 if (code1 != code2)
13186 return false;
13188 switch (code1)
13190 case INTEGER_CST:
13191 return wi::eq_p (t1, t2);
13193 case REAL_CST:
13194 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
13196 case STRING_CST:
13197 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13198 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13199 TREE_STRING_LENGTH (t1));
13201 case FIXED_CST:
13202 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13203 TREE_FIXED_CST (t2));
13205 case COMPLEX_CST:
13206 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13207 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13209 case VECTOR_CST:
13210 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13212 case CONSTRUCTOR:
13213 /* We need to do this when determining whether or not two
13214 non-type pointer to member function template arguments
13215 are the same. */
13216 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13217 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13218 return false;
13220 tree field, value;
13221 unsigned int i;
13222 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13224 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13225 if (!c_tree_equal (field, elt2->index)
13226 || !c_tree_equal (value, elt2->value))
13227 return false;
13230 return true;
13232 case TREE_LIST:
13233 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
13234 return false;
13235 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
13236 return false;
13237 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
13239 case SAVE_EXPR:
13240 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13242 case CALL_EXPR:
13244 tree arg1, arg2;
13245 call_expr_arg_iterator iter1, iter2;
13246 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
13247 return false;
13248 for (arg1 = first_call_expr_arg (t1, &iter1),
13249 arg2 = first_call_expr_arg (t2, &iter2);
13250 arg1 && arg2;
13251 arg1 = next_call_expr_arg (&iter1),
13252 arg2 = next_call_expr_arg (&iter2))
13253 if (!c_tree_equal (arg1, arg2))
13254 return false;
13255 if (arg1 || arg2)
13256 return false;
13257 return true;
13260 case TARGET_EXPR:
13262 tree o1 = TREE_OPERAND (t1, 0);
13263 tree o2 = TREE_OPERAND (t2, 0);
13265 /* Special case: if either target is an unallocated VAR_DECL,
13266 it means that it's going to be unified with whatever the
13267 TARGET_EXPR is really supposed to initialize, so treat it
13268 as being equivalent to anything. */
13269 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
13270 && !DECL_RTL_SET_P (o1))
13271 /*Nop*/;
13272 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
13273 && !DECL_RTL_SET_P (o2))
13274 /*Nop*/;
13275 else if (!c_tree_equal (o1, o2))
13276 return false;
13278 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
13281 case COMPONENT_REF:
13282 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
13283 return false;
13284 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13286 case PARM_DECL:
13287 case VAR_DECL:
13288 case CONST_DECL:
13289 case FIELD_DECL:
13290 case FUNCTION_DECL:
13291 case IDENTIFIER_NODE:
13292 case SSA_NAME:
13293 return false;
13295 case TREE_VEC:
13297 unsigned ix;
13298 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
13299 return false;
13300 for (ix = TREE_VEC_LENGTH (t1); ix--;)
13301 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
13302 TREE_VEC_ELT (t2, ix)))
13303 return false;
13304 return true;
13307 default:
13308 break;
13311 switch (TREE_CODE_CLASS (code1))
13313 case tcc_unary:
13314 case tcc_binary:
13315 case tcc_comparison:
13316 case tcc_expression:
13317 case tcc_vl_exp:
13318 case tcc_reference:
13319 case tcc_statement:
13321 int i, n = TREE_OPERAND_LENGTH (t1);
13323 switch (code1)
13325 case PREINCREMENT_EXPR:
13326 case PREDECREMENT_EXPR:
13327 case POSTINCREMENT_EXPR:
13328 case POSTDECREMENT_EXPR:
13329 n = 1;
13330 break;
13331 case ARRAY_REF:
13332 n = 2;
13333 break;
13334 default:
13335 break;
13338 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
13339 && n != TREE_OPERAND_LENGTH (t2))
13340 return false;
13342 for (i = 0; i < n; ++i)
13343 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
13344 return false;
13346 return true;
13349 case tcc_type:
13350 return comptypes (t1, t2);
13351 default:
13352 gcc_unreachable ();
13354 /* We can get here with --disable-checking. */
13355 return false;
13358 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
13359 spawn-helper and BODY is the newly created body for FNDECL. */
13361 void
13362 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
13364 tree list = alloc_stmt_list ();
13365 tree frame = make_cilk_frame (fndecl);
13366 tree dtor = create_cilk_function_exit (frame, false, true);
13367 add_local_decl (cfun, frame);
13369 DECL_SAVED_TREE (fndecl) = list;
13370 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
13371 frame);
13372 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
13373 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
13375 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
13376 append_to_statement_list (detach_expr, &body_list);
13378 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
13379 body = fold_build_cleanup_point_expr (void_type_node, body);
13381 append_to_statement_list (body, &body_list);
13382 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
13383 body_list, dtor), &list);
13386 /* Returns true when the function declaration FNDECL is implicit,
13387 introduced as a result of a call to an otherwise undeclared
13388 function, and false otherwise. */
13390 bool
13391 c_decl_implicit (const_tree fndecl)
13393 return C_DECL_IMPLICIT (fndecl);