2016-12-24 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / c / c-typeck.c
bloba269682d4ed98da960139d8e0c47d698ae7720b2
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2016 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 "memmodel.h"
30 #include "target.h"
31 #include "function.h"
32 #include "bitmap.h"
33 #include "c-tree.h"
34 #include "gimple-expr.h"
35 #include "predict.h"
36 #include "stor-layout.h"
37 #include "trans-mem.h"
38 #include "varasm.h"
39 #include "stmt.h"
40 #include "langhooks.h"
41 #include "c-lang.h"
42 #include "intl.h"
43 #include "tree-iterator.h"
44 #include "gimplify.h"
45 #include "tree-inline.h"
46 #include "omp-general.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
49 #include "cilk.h"
50 #include "gomp-constants.h"
51 #include "spellcheck-tree.h"
52 #include "gcc-rich-location.h"
54 /* Possible cases of implicit bad conversions. Used to select
55 diagnostic messages in convert_for_assignment. */
56 enum impl_conv {
57 ic_argpass,
58 ic_assign,
59 ic_init,
60 ic_return
63 /* The level of nesting inside "__alignof__". */
64 int in_alignof;
66 /* The level of nesting inside "sizeof". */
67 int in_sizeof;
69 /* The level of nesting inside "typeof". */
70 int in_typeof;
72 /* The argument of last parsed sizeof expression, only to be tested
73 if expr.original_code == SIZEOF_EXPR. */
74 tree c_last_sizeof_arg;
76 /* Nonzero if we might need to print a "missing braces around
77 initializer" message within this initializer. */
78 static int found_missing_braces;
80 static int require_constant_value;
81 static int require_constant_elements;
83 static bool null_pointer_constant_p (const_tree);
84 static tree qualify_type (tree, tree);
85 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
86 bool *);
87 static int comp_target_types (location_t, tree, tree);
88 static int function_types_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
91 static tree lookup_field (tree, tree);
92 static int convert_arguments (location_t, vec<location_t>, tree,
93 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
94 tree);
95 static tree pointer_diff (location_t, tree, tree);
96 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
97 enum impl_conv, bool, tree, tree, int);
98 static tree valid_compound_expr_initializer (tree, tree);
99 static void push_string (const char *);
100 static void push_member_name (tree);
101 static int spelling_length (void);
102 static char *print_spelling (char *);
103 static void warning_init (location_t, int, const char *);
104 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
105 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
106 bool, struct obstack *);
107 static void output_pending_init_elements (int, struct obstack *);
108 static int set_designator (location_t, int, struct obstack *);
109 static void push_range_stack (tree, struct obstack *);
110 static void add_pending_init (location_t, tree, tree, tree, bool,
111 struct obstack *);
112 static void set_nonincremental_init (struct obstack *);
113 static void set_nonincremental_init_from_string (tree, struct obstack *);
114 static tree find_init_member (tree, struct obstack *);
115 static void readonly_warning (tree, enum lvalue_use);
116 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
117 static void record_maybe_used_decl (tree);
118 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
120 /* Return true if EXP is a null pointer constant, false otherwise. */
122 static bool
123 null_pointer_constant_p (const_tree expr)
125 /* This should really operate on c_expr structures, but they aren't
126 yet available everywhere required. */
127 tree type = TREE_TYPE (expr);
128 return (TREE_CODE (expr) == INTEGER_CST
129 && !TREE_OVERFLOW (expr)
130 && integer_zerop (expr)
131 && (INTEGRAL_TYPE_P (type)
132 || (TREE_CODE (type) == POINTER_TYPE
133 && VOID_TYPE_P (TREE_TYPE (type))
134 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
137 /* EXPR may appear in an unevaluated part of an integer constant
138 expression, but not in an evaluated part. Wrap it in a
139 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
140 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
142 static tree
143 note_integer_operands (tree expr)
145 tree ret;
146 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
148 ret = copy_node (expr);
149 TREE_OVERFLOW (ret) = 1;
151 else
153 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
154 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
156 return ret;
159 /* Having checked whether EXPR may appear in an unevaluated part of an
160 integer constant expression and found that it may, remove any
161 C_MAYBE_CONST_EXPR noting this fact and return the resulting
162 expression. */
164 static inline tree
165 remove_c_maybe_const_expr (tree expr)
167 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
168 return C_MAYBE_CONST_EXPR_EXPR (expr);
169 else
170 return expr;
173 \f/* This is a cache to hold if two types are compatible or not. */
175 struct tagged_tu_seen_cache {
176 const struct tagged_tu_seen_cache * next;
177 const_tree t1;
178 const_tree t2;
179 /* The return value of tagged_types_tu_compatible_p if we had seen
180 these two types already. */
181 int val;
184 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
185 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
187 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
188 does not have an incomplete type. (That includes void types.)
189 LOC is the location of the use. */
191 tree
192 require_complete_type (location_t loc, tree value)
194 tree type = TREE_TYPE (value);
196 if (error_operand_p (value))
197 return error_mark_node;
199 /* First, detect a valid value with a complete type. */
200 if (COMPLETE_TYPE_P (type))
201 return value;
203 c_incomplete_type_error (loc, value, type);
204 return error_mark_node;
207 /* Print an error message for invalid use of an incomplete type.
208 VALUE is the expression that was used (or 0 if that isn't known)
209 and TYPE is the type that was invalid. LOC is the location for
210 the error. */
212 void
213 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
215 /* Avoid duplicate error message. */
216 if (TREE_CODE (type) == ERROR_MARK)
217 return;
219 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
220 error_at (loc, "%qD has an incomplete type %qT", value, type);
221 else
223 retry:
224 /* We must print an error message. Be clever about what it says. */
226 switch (TREE_CODE (type))
228 case RECORD_TYPE:
229 case UNION_TYPE:
230 case ENUMERAL_TYPE:
231 break;
233 case VOID_TYPE:
234 error_at (loc, "invalid use of void expression");
235 return;
237 case ARRAY_TYPE:
238 if (TYPE_DOMAIN (type))
240 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
242 error_at (loc, "invalid use of flexible array member");
243 return;
245 type = TREE_TYPE (type);
246 goto retry;
248 error_at (loc, "invalid use of array with unspecified bounds");
249 return;
251 default:
252 gcc_unreachable ();
255 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
256 error_at (loc, "invalid use of undefined type %qT", type);
257 else
258 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
259 error_at (loc, "invalid use of incomplete typedef %qT", type);
263 /* Given a type, apply default promotions wrt unnamed function
264 arguments and return the new type. */
266 tree
267 c_type_promotes_to (tree type)
269 tree ret = NULL_TREE;
271 if (TYPE_MAIN_VARIANT (type) == float_type_node)
272 ret = double_type_node;
273 else if (c_promoting_integer_type_p (type))
275 /* Preserve unsignedness if not really getting any wider. */
276 if (TYPE_UNSIGNED (type)
277 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
278 ret = unsigned_type_node;
279 else
280 ret = integer_type_node;
283 if (ret != NULL_TREE)
284 return (TYPE_ATOMIC (type)
285 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
286 : ret);
288 return type;
291 /* Return true if between two named address spaces, whether there is a superset
292 named address space that encompasses both address spaces. If there is a
293 superset, return which address space is the superset. */
295 static bool
296 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
298 if (as1 == as2)
300 *common = as1;
301 return true;
303 else if (targetm.addr_space.subset_p (as1, as2))
305 *common = as2;
306 return true;
308 else if (targetm.addr_space.subset_p (as2, as1))
310 *common = as1;
311 return true;
313 else
314 return false;
317 /* Return a variant of TYPE which has all the type qualifiers of LIKE
318 as well as those of TYPE. */
320 static tree
321 qualify_type (tree type, tree like)
323 addr_space_t as_type = TYPE_ADDR_SPACE (type);
324 addr_space_t as_like = TYPE_ADDR_SPACE (like);
325 addr_space_t as_common;
327 /* If the two named address spaces are different, determine the common
328 superset address space. If there isn't one, raise an error. */
329 if (!addr_space_superset (as_type, as_like, &as_common))
331 as_common = as_type;
332 error ("%qT and %qT are in disjoint named address spaces",
333 type, like);
336 return c_build_qualified_type (type,
337 TYPE_QUALS_NO_ADDR_SPACE (type)
338 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
339 | ENCODE_QUAL_ADDR_SPACE (as_common));
342 /* Return true iff the given tree T is a variable length array. */
344 bool
345 c_vla_type_p (const_tree t)
347 if (TREE_CODE (t) == ARRAY_TYPE
348 && C_TYPE_VARIABLE_SIZE (t))
349 return true;
350 return false;
353 /* Return the composite type of two compatible types.
355 We assume that comptypes has already been done and returned
356 nonzero; if that isn't so, this may crash. In particular, we
357 assume that qualifiers match. */
359 tree
360 composite_type (tree t1, tree t2)
362 enum tree_code code1;
363 enum tree_code code2;
364 tree attributes;
366 /* Save time if the two types are the same. */
368 if (t1 == t2) return t1;
370 /* If one type is nonsense, use the other. */
371 if (t1 == error_mark_node)
372 return t2;
373 if (t2 == error_mark_node)
374 return t1;
376 code1 = TREE_CODE (t1);
377 code2 = TREE_CODE (t2);
379 /* Merge the attributes. */
380 attributes = targetm.merge_type_attributes (t1, t2);
382 /* If one is an enumerated type and the other is the compatible
383 integer type, the composite type might be either of the two
384 (DR#013 question 3). For consistency, use the enumerated type as
385 the composite type. */
387 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
388 return t1;
389 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
390 return t2;
392 gcc_assert (code1 == code2);
394 switch (code1)
396 case POINTER_TYPE:
397 /* For two pointers, do this recursively on the target type. */
399 tree pointed_to_1 = TREE_TYPE (t1);
400 tree pointed_to_2 = TREE_TYPE (t2);
401 tree target = composite_type (pointed_to_1, pointed_to_2);
402 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
403 t1 = build_type_attribute_variant (t1, attributes);
404 return qualify_type (t1, t2);
407 case ARRAY_TYPE:
409 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
410 int quals;
411 tree unqual_elt;
412 tree d1 = TYPE_DOMAIN (t1);
413 tree d2 = TYPE_DOMAIN (t2);
414 bool d1_variable, d2_variable;
415 bool d1_zero, d2_zero;
416 bool t1_complete, t2_complete;
418 /* We should not have any type quals on arrays at all. */
419 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
420 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
422 t1_complete = COMPLETE_TYPE_P (t1);
423 t2_complete = COMPLETE_TYPE_P (t2);
425 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
426 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
428 d1_variable = (!d1_zero
429 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
430 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
431 d2_variable = (!d2_zero
432 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
433 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
434 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
435 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
437 /* Save space: see if the result is identical to one of the args. */
438 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
439 && (d2_variable || d2_zero || !d1_variable))
440 return build_type_attribute_variant (t1, attributes);
441 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
442 && (d1_variable || d1_zero || !d2_variable))
443 return build_type_attribute_variant (t2, attributes);
445 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
446 return build_type_attribute_variant (t1, attributes);
447 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
448 return build_type_attribute_variant (t2, attributes);
450 /* Merge the element types, and have a size if either arg has
451 one. We may have qualifiers on the element types. To set
452 up TYPE_MAIN_VARIANT correctly, we need to form the
453 composite of the unqualified types and add the qualifiers
454 back at the end. */
455 quals = TYPE_QUALS (strip_array_types (elt));
456 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
457 t1 = build_array_type (unqual_elt,
458 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
459 && (d2_variable
460 || d2_zero
461 || !d1_variable))
462 ? t1
463 : t2));
464 /* Ensure a composite type involving a zero-length array type
465 is a zero-length type not an incomplete type. */
466 if (d1_zero && d2_zero
467 && (t1_complete || t2_complete)
468 && !COMPLETE_TYPE_P (t1))
470 TYPE_SIZE (t1) = bitsize_zero_node;
471 TYPE_SIZE_UNIT (t1) = size_zero_node;
473 t1 = c_build_qualified_type (t1, quals);
474 return build_type_attribute_variant (t1, attributes);
477 case ENUMERAL_TYPE:
478 case RECORD_TYPE:
479 case UNION_TYPE:
480 if (attributes != NULL)
482 /* Try harder not to create a new aggregate type. */
483 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
484 return t1;
485 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
486 return t2;
488 return build_type_attribute_variant (t1, attributes);
490 case FUNCTION_TYPE:
491 /* Function types: prefer the one that specified arg types.
492 If both do, merge the arg types. Also merge the return types. */
494 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
495 tree p1 = TYPE_ARG_TYPES (t1);
496 tree p2 = TYPE_ARG_TYPES (t2);
497 int len;
498 tree newargs, n;
499 int i;
501 /* Save space: see if the result is identical to one of the args. */
502 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
503 return build_type_attribute_variant (t1, attributes);
504 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
505 return build_type_attribute_variant (t2, attributes);
507 /* Simple way if one arg fails to specify argument types. */
508 if (TYPE_ARG_TYPES (t1) == 0)
510 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
511 t1 = build_type_attribute_variant (t1, attributes);
512 return qualify_type (t1, t2);
514 if (TYPE_ARG_TYPES (t2) == 0)
516 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
517 t1 = build_type_attribute_variant (t1, attributes);
518 return qualify_type (t1, t2);
521 /* If both args specify argument types, we must merge the two
522 lists, argument by argument. */
524 for (len = 0, newargs = p1;
525 newargs && newargs != void_list_node;
526 len++, newargs = TREE_CHAIN (newargs))
529 for (i = 0; i < len; i++)
530 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
532 n = newargs;
534 for (; p1 && p1 != void_list_node;
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);
610 /* FALLTHRU */
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 /* For floating types of the same TYPE_PRECISION (which we here
932 assume means either the same set of values, or sets of values
933 neither a subset of the other, with behavior being undefined in
934 the latter case), follow the rules from TS 18661-3: prefer
935 interchange types _FloatN, then standard types long double,
936 double, float, then extended types _FloatNx. For extended types,
937 check them starting with _Float128x as that seems most consistent
938 in spirit with preferring long double to double; for interchange
939 types, also check in that order for consistency although it's not
940 possible for more than one of them to have the same
941 precision. */
942 tree mv1 = TYPE_MAIN_VARIANT (t1);
943 tree mv2 = TYPE_MAIN_VARIANT (t2);
945 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
946 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
947 return FLOATN_TYPE_NODE (i);
949 /* Likewise, prefer long double to double even if same size. */
950 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
951 return long_double_type_node;
953 /* Likewise, prefer double to float even if same size.
954 We got a couple of embedded targets with 32 bit doubles, and the
955 pdp11 might have 64 bit floats. */
956 if (mv1 == double_type_node || mv2 == double_type_node)
957 return double_type_node;
959 if (mv1 == float_type_node || mv2 == float_type_node)
960 return float_type_node;
962 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
963 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
964 return FLOATNX_TYPE_NODE (i);
966 /* Otherwise prefer the unsigned one. */
968 if (TYPE_UNSIGNED (t1))
969 return t1;
970 else
971 return t2;
974 /* Wrapper around c_common_type that is used by c-common.c and other
975 front end optimizations that remove promotions. ENUMERAL_TYPEs
976 are allowed here and are converted to their compatible integer types.
977 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
978 preferably a non-Boolean type as the common type. */
979 tree
980 common_type (tree t1, tree t2)
982 if (TREE_CODE (t1) == ENUMERAL_TYPE)
983 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
984 if (TREE_CODE (t2) == ENUMERAL_TYPE)
985 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
987 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
988 if (TREE_CODE (t1) == BOOLEAN_TYPE
989 && TREE_CODE (t2) == BOOLEAN_TYPE)
990 return boolean_type_node;
992 /* If either type is BOOLEAN_TYPE, then return the other. */
993 if (TREE_CODE (t1) == BOOLEAN_TYPE)
994 return t2;
995 if (TREE_CODE (t2) == BOOLEAN_TYPE)
996 return t1;
998 return c_common_type (t1, t2);
1001 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1002 or various other operations. Return 2 if they are compatible
1003 but a warning may be needed if you use them together. */
1006 comptypes (tree type1, tree type2)
1008 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1009 int val;
1011 val = comptypes_internal (type1, type2, NULL, NULL);
1012 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1014 return val;
1017 /* Like comptypes, but if it returns non-zero because enum and int are
1018 compatible, it sets *ENUM_AND_INT_P to true. */
1020 static int
1021 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1023 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1024 int val;
1026 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1027 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1029 return val;
1032 /* Like comptypes, but if it returns nonzero for different types, it
1033 sets *DIFFERENT_TYPES_P to true. */
1036 comptypes_check_different_types (tree type1, tree type2,
1037 bool *different_types_p)
1039 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1040 int val;
1042 val = comptypes_internal (type1, type2, NULL, different_types_p);
1043 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1045 return val;
1048 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1049 or various other operations. Return 2 if they are compatible
1050 but a warning may be needed if you use them together. If
1051 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1052 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1053 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1054 NULL, and the types are compatible but different enough not to be
1055 permitted in C11 typedef redeclarations, then this sets
1056 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1057 false, but may or may not be set if the types are incompatible.
1058 This differs from comptypes, in that we don't free the seen
1059 types. */
1061 static int
1062 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1063 bool *different_types_p)
1065 const_tree t1 = type1;
1066 const_tree t2 = type2;
1067 int attrval, val;
1069 /* Suppress errors caused by previously reported errors. */
1071 if (t1 == t2 || !t1 || !t2
1072 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1073 return 1;
1075 /* Enumerated types are compatible with integer types, but this is
1076 not transitive: two enumerated types in the same translation unit
1077 are compatible with each other only if they are the same type. */
1079 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1081 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1082 if (TREE_CODE (t2) != VOID_TYPE)
1084 if (enum_and_int_p != NULL)
1085 *enum_and_int_p = true;
1086 if (different_types_p != NULL)
1087 *different_types_p = true;
1090 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1092 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1093 if (TREE_CODE (t1) != VOID_TYPE)
1095 if (enum_and_int_p != NULL)
1096 *enum_and_int_p = true;
1097 if (different_types_p != NULL)
1098 *different_types_p = true;
1102 if (t1 == t2)
1103 return 1;
1105 /* Different classes of types can't be compatible. */
1107 if (TREE_CODE (t1) != TREE_CODE (t2))
1108 return 0;
1110 /* Qualifiers must match. C99 6.7.3p9 */
1112 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1113 return 0;
1115 /* Allow for two different type nodes which have essentially the same
1116 definition. Note that we already checked for equality of the type
1117 qualifiers (just above). */
1119 if (TREE_CODE (t1) != ARRAY_TYPE
1120 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1121 return 1;
1123 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1124 if (!(attrval = comp_type_attributes (t1, t2)))
1125 return 0;
1127 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1128 val = 0;
1130 switch (TREE_CODE (t1))
1132 case INTEGER_TYPE:
1133 case FIXED_POINT_TYPE:
1134 case REAL_TYPE:
1135 /* With these nodes, we can't determine type equivalence by
1136 looking at what is stored in the nodes themselves, because
1137 two nodes might have different TYPE_MAIN_VARIANTs but still
1138 represent the same type. For example, wchar_t and int could
1139 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1140 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1141 and are distinct types. On the other hand, int and the
1142 following typedef
1144 typedef int INT __attribute((may_alias));
1146 have identical properties, different TYPE_MAIN_VARIANTs, but
1147 represent the same type. The canonical type system keeps
1148 track of equivalence in this case, so we fall back on it. */
1149 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1151 case POINTER_TYPE:
1152 /* Do not remove mode information. */
1153 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1154 break;
1155 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1156 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1157 enum_and_int_p, different_types_p));
1158 break;
1160 case FUNCTION_TYPE:
1161 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1162 different_types_p);
1163 break;
1165 case ARRAY_TYPE:
1167 tree d1 = TYPE_DOMAIN (t1);
1168 tree d2 = TYPE_DOMAIN (t2);
1169 bool d1_variable, d2_variable;
1170 bool d1_zero, d2_zero;
1171 val = 1;
1173 /* Target types must match incl. qualifiers. */
1174 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1175 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1176 enum_and_int_p,
1177 different_types_p)))
1178 return 0;
1180 if (different_types_p != NULL
1181 && (d1 == 0) != (d2 == 0))
1182 *different_types_p = true;
1183 /* Sizes must match unless one is missing or variable. */
1184 if (d1 == 0 || d2 == 0 || d1 == d2)
1185 break;
1187 d1_zero = !TYPE_MAX_VALUE (d1);
1188 d2_zero = !TYPE_MAX_VALUE (d2);
1190 d1_variable = (!d1_zero
1191 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1192 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1193 d2_variable = (!d2_zero
1194 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1195 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1196 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1197 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1199 if (different_types_p != NULL
1200 && d1_variable != d2_variable)
1201 *different_types_p = true;
1202 if (d1_variable || d2_variable)
1203 break;
1204 if (d1_zero && d2_zero)
1205 break;
1206 if (d1_zero || d2_zero
1207 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1208 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1209 val = 0;
1211 break;
1214 case ENUMERAL_TYPE:
1215 case RECORD_TYPE:
1216 case UNION_TYPE:
1217 if (val != 1 && !same_translation_unit_p (t1, t2))
1219 tree a1 = TYPE_ATTRIBUTES (t1);
1220 tree a2 = TYPE_ATTRIBUTES (t2);
1222 if (! attribute_list_contained (a1, a2)
1223 && ! attribute_list_contained (a2, a1))
1224 break;
1226 if (attrval != 2)
1227 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1228 different_types_p);
1229 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1230 different_types_p);
1232 break;
1234 case VECTOR_TYPE:
1235 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1236 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1237 enum_and_int_p, different_types_p));
1238 break;
1240 default:
1241 break;
1243 return attrval == 2 && val == 1 ? 2 : val;
1246 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1247 their qualifiers, except for named address spaces. If the pointers point to
1248 different named addresses, then we must determine if one address space is a
1249 subset of the other. */
1251 static int
1252 comp_target_types (location_t location, tree ttl, tree ttr)
1254 int val;
1255 int val_ped;
1256 tree mvl = TREE_TYPE (ttl);
1257 tree mvr = TREE_TYPE (ttr);
1258 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1259 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1260 addr_space_t as_common;
1261 bool enum_and_int_p;
1263 /* Fail if pointers point to incompatible address spaces. */
1264 if (!addr_space_superset (asl, asr, &as_common))
1265 return 0;
1267 /* For pedantic record result of comptypes on arrays before losing
1268 qualifiers on the element type below. */
1269 val_ped = 1;
1271 if (TREE_CODE (mvl) == ARRAY_TYPE
1272 && TREE_CODE (mvr) == ARRAY_TYPE)
1273 val_ped = comptypes (mvl, mvr);
1275 /* Qualifiers on element types of array types that are
1276 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1278 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1279 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1280 : TYPE_MAIN_VARIANT (mvl));
1282 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1283 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1284 : TYPE_MAIN_VARIANT (mvr));
1286 enum_and_int_p = false;
1287 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1289 if (val == 1 && val_ped != 1)
1290 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1291 "are incompatible in ISO C");
1293 if (val == 2)
1294 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1296 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1297 warning_at (location, OPT_Wc___compat,
1298 "pointer target types incompatible in C++");
1300 return val;
1303 /* Subroutines of `comptypes'. */
1305 /* Determine whether two trees derive from the same translation unit.
1306 If the CONTEXT chain ends in a null, that tree's context is still
1307 being parsed, so if two trees have context chains ending in null,
1308 they're in the same translation unit. */
1310 same_translation_unit_p (const_tree t1, const_tree t2)
1312 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1313 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1315 case tcc_declaration:
1316 t1 = DECL_CONTEXT (t1); break;
1317 case tcc_type:
1318 t1 = TYPE_CONTEXT (t1); break;
1319 case tcc_exceptional:
1320 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1321 default: gcc_unreachable ();
1324 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1325 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1327 case tcc_declaration:
1328 t2 = DECL_CONTEXT (t2); break;
1329 case tcc_type:
1330 t2 = TYPE_CONTEXT (t2); break;
1331 case tcc_exceptional:
1332 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1333 default: gcc_unreachable ();
1336 return t1 == t2;
1339 /* Allocate the seen two types, assuming that they are compatible. */
1341 static struct tagged_tu_seen_cache *
1342 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1344 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1345 tu->next = tagged_tu_seen_base;
1346 tu->t1 = t1;
1347 tu->t2 = t2;
1349 tagged_tu_seen_base = tu;
1351 /* The C standard says that two structures in different translation
1352 units are compatible with each other only if the types of their
1353 fields are compatible (among other things). We assume that they
1354 are compatible until proven otherwise when building the cache.
1355 An example where this can occur is:
1356 struct a
1358 struct a *next;
1360 If we are comparing this against a similar struct in another TU,
1361 and did not assume they were compatible, we end up with an infinite
1362 loop. */
1363 tu->val = 1;
1364 return tu;
1367 /* Free the seen types until we get to TU_TIL. */
1369 static void
1370 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1372 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1373 while (tu != tu_til)
1375 const struct tagged_tu_seen_cache *const tu1
1376 = (const struct tagged_tu_seen_cache *) tu;
1377 tu = tu1->next;
1378 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1380 tagged_tu_seen_base = tu_til;
1383 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1384 compatible. If the two types are not the same (which has been
1385 checked earlier), this can only happen when multiple translation
1386 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1387 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1388 comptypes_internal. */
1390 static int
1391 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1392 bool *enum_and_int_p, bool *different_types_p)
1394 tree s1, s2;
1395 bool needs_warning = false;
1397 /* We have to verify that the tags of the types are the same. This
1398 is harder than it looks because this may be a typedef, so we have
1399 to go look at the original type. It may even be a typedef of a
1400 typedef...
1401 In the case of compiler-created builtin structs the TYPE_DECL
1402 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1403 while (TYPE_NAME (t1)
1404 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1405 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1406 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1408 while (TYPE_NAME (t2)
1409 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1410 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1411 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1413 /* C90 didn't have the requirement that the two tags be the same. */
1414 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1415 return 0;
1417 /* C90 didn't say what happened if one or both of the types were
1418 incomplete; we choose to follow C99 rules here, which is that they
1419 are compatible. */
1420 if (TYPE_SIZE (t1) == NULL
1421 || TYPE_SIZE (t2) == NULL)
1422 return 1;
1425 const struct tagged_tu_seen_cache * tts_i;
1426 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1427 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1428 return tts_i->val;
1431 switch (TREE_CODE (t1))
1433 case ENUMERAL_TYPE:
1435 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1436 /* Speed up the case where the type values are in the same order. */
1437 tree tv1 = TYPE_VALUES (t1);
1438 tree tv2 = TYPE_VALUES (t2);
1440 if (tv1 == tv2)
1442 return 1;
1445 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1447 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1448 break;
1449 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1451 tu->val = 0;
1452 return 0;
1456 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1458 return 1;
1460 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1462 tu->val = 0;
1463 return 0;
1466 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1468 tu->val = 0;
1469 return 0;
1472 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1474 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1475 if (s2 == NULL
1476 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1478 tu->val = 0;
1479 return 0;
1482 return 1;
1485 case UNION_TYPE:
1487 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1488 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1490 tu->val = 0;
1491 return 0;
1494 /* Speed up the common case where the fields are in the same order. */
1495 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1496 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1498 int result;
1500 if (DECL_NAME (s1) != DECL_NAME (s2))
1501 break;
1502 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1503 enum_and_int_p, different_types_p);
1505 if (result != 1 && !DECL_NAME (s1))
1506 break;
1507 if (result == 0)
1509 tu->val = 0;
1510 return 0;
1512 if (result == 2)
1513 needs_warning = true;
1515 if (TREE_CODE (s1) == FIELD_DECL
1516 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1517 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1519 tu->val = 0;
1520 return 0;
1523 if (!s1 && !s2)
1525 tu->val = needs_warning ? 2 : 1;
1526 return tu->val;
1529 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1531 bool ok = false;
1533 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1534 if (DECL_NAME (s1) == DECL_NAME (s2))
1536 int result;
1538 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1539 enum_and_int_p,
1540 different_types_p);
1542 if (result != 1 && !DECL_NAME (s1))
1543 continue;
1544 if (result == 0)
1546 tu->val = 0;
1547 return 0;
1549 if (result == 2)
1550 needs_warning = true;
1552 if (TREE_CODE (s1) == FIELD_DECL
1553 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1554 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1555 break;
1557 ok = true;
1558 break;
1560 if (!ok)
1562 tu->val = 0;
1563 return 0;
1566 tu->val = needs_warning ? 2 : 10;
1567 return tu->val;
1570 case RECORD_TYPE:
1572 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1574 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1575 s1 && s2;
1576 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1578 int result;
1579 if (TREE_CODE (s1) != TREE_CODE (s2)
1580 || DECL_NAME (s1) != DECL_NAME (s2))
1581 break;
1582 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1583 enum_and_int_p, different_types_p);
1584 if (result == 0)
1585 break;
1586 if (result == 2)
1587 needs_warning = true;
1589 if (TREE_CODE (s1) == FIELD_DECL
1590 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1591 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1592 break;
1594 if (s1 && s2)
1595 tu->val = 0;
1596 else
1597 tu->val = needs_warning ? 2 : 1;
1598 return tu->val;
1601 default:
1602 gcc_unreachable ();
1606 /* Return 1 if two function types F1 and F2 are compatible.
1607 If either type specifies no argument types,
1608 the other must specify a fixed number of self-promoting arg types.
1609 Otherwise, if one type specifies only the number of arguments,
1610 the other must specify that number of self-promoting arg types.
1611 Otherwise, the argument types must match.
1612 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1614 static int
1615 function_types_compatible_p (const_tree f1, const_tree f2,
1616 bool *enum_and_int_p, bool *different_types_p)
1618 tree args1, args2;
1619 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1620 int val = 1;
1621 int val1;
1622 tree ret1, ret2;
1624 ret1 = TREE_TYPE (f1);
1625 ret2 = TREE_TYPE (f2);
1627 /* 'volatile' qualifiers on a function's return type used to mean
1628 the function is noreturn. */
1629 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1630 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1631 if (TYPE_VOLATILE (ret1))
1632 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1633 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1634 if (TYPE_VOLATILE (ret2))
1635 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1636 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1637 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1638 if (val == 0)
1639 return 0;
1641 args1 = TYPE_ARG_TYPES (f1);
1642 args2 = TYPE_ARG_TYPES (f2);
1644 if (different_types_p != NULL
1645 && (args1 == 0) != (args2 == 0))
1646 *different_types_p = true;
1648 /* An unspecified parmlist matches any specified parmlist
1649 whose argument types don't need default promotions. */
1651 if (args1 == 0)
1653 if (!self_promoting_args_p (args2))
1654 return 0;
1655 /* If one of these types comes from a non-prototype fn definition,
1656 compare that with the other type's arglist.
1657 If they don't match, ask for a warning (but no error). */
1658 if (TYPE_ACTUAL_ARG_TYPES (f1)
1659 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1660 enum_and_int_p, different_types_p))
1661 val = 2;
1662 return val;
1664 if (args2 == 0)
1666 if (!self_promoting_args_p (args1))
1667 return 0;
1668 if (TYPE_ACTUAL_ARG_TYPES (f2)
1669 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1670 enum_and_int_p, different_types_p))
1671 val = 2;
1672 return val;
1675 /* Both types have argument lists: compare them and propagate results. */
1676 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1677 different_types_p);
1678 return val1 != 1 ? val1 : val;
1681 /* Check two lists of types for compatibility, returning 0 for
1682 incompatible, 1 for compatible, or 2 for compatible with
1683 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1684 comptypes_internal. */
1686 static int
1687 type_lists_compatible_p (const_tree args1, const_tree args2,
1688 bool *enum_and_int_p, bool *different_types_p)
1690 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1691 int val = 1;
1692 int newval = 0;
1694 while (1)
1696 tree a1, mv1, a2, mv2;
1697 if (args1 == 0 && args2 == 0)
1698 return val;
1699 /* If one list is shorter than the other,
1700 they fail to match. */
1701 if (args1 == 0 || args2 == 0)
1702 return 0;
1703 mv1 = a1 = TREE_VALUE (args1);
1704 mv2 = a2 = TREE_VALUE (args2);
1705 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1706 mv1 = (TYPE_ATOMIC (mv1)
1707 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1708 TYPE_QUAL_ATOMIC)
1709 : TYPE_MAIN_VARIANT (mv1));
1710 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1711 mv2 = (TYPE_ATOMIC (mv2)
1712 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1713 TYPE_QUAL_ATOMIC)
1714 : TYPE_MAIN_VARIANT (mv2));
1715 /* A null pointer instead of a type
1716 means there is supposed to be an argument
1717 but nothing is specified about what type it has.
1718 So match anything that self-promotes. */
1719 if (different_types_p != NULL
1720 && (a1 == 0) != (a2 == 0))
1721 *different_types_p = true;
1722 if (a1 == 0)
1724 if (c_type_promotes_to (a2) != a2)
1725 return 0;
1727 else if (a2 == 0)
1729 if (c_type_promotes_to (a1) != a1)
1730 return 0;
1732 /* If one of the lists has an error marker, ignore this arg. */
1733 else if (TREE_CODE (a1) == ERROR_MARK
1734 || TREE_CODE (a2) == ERROR_MARK)
1736 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1737 different_types_p)))
1739 if (different_types_p != NULL)
1740 *different_types_p = true;
1741 /* Allow wait (union {union wait *u; int *i} *)
1742 and wait (union wait *) to be compatible. */
1743 if (TREE_CODE (a1) == UNION_TYPE
1744 && (TYPE_NAME (a1) == 0
1745 || TYPE_TRANSPARENT_AGGR (a1))
1746 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1747 && tree_int_cst_equal (TYPE_SIZE (a1),
1748 TYPE_SIZE (a2)))
1750 tree memb;
1751 for (memb = TYPE_FIELDS (a1);
1752 memb; memb = DECL_CHAIN (memb))
1754 tree mv3 = TREE_TYPE (memb);
1755 if (mv3 && mv3 != error_mark_node
1756 && TREE_CODE (mv3) != ARRAY_TYPE)
1757 mv3 = (TYPE_ATOMIC (mv3)
1758 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1759 TYPE_QUAL_ATOMIC)
1760 : TYPE_MAIN_VARIANT (mv3));
1761 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1762 different_types_p))
1763 break;
1765 if (memb == 0)
1766 return 0;
1768 else if (TREE_CODE (a2) == UNION_TYPE
1769 && (TYPE_NAME (a2) == 0
1770 || TYPE_TRANSPARENT_AGGR (a2))
1771 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1772 && tree_int_cst_equal (TYPE_SIZE (a2),
1773 TYPE_SIZE (a1)))
1775 tree memb;
1776 for (memb = TYPE_FIELDS (a2);
1777 memb; memb = DECL_CHAIN (memb))
1779 tree mv3 = TREE_TYPE (memb);
1780 if (mv3 && mv3 != error_mark_node
1781 && TREE_CODE (mv3) != ARRAY_TYPE)
1782 mv3 = (TYPE_ATOMIC (mv3)
1783 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1784 TYPE_QUAL_ATOMIC)
1785 : TYPE_MAIN_VARIANT (mv3));
1786 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1787 different_types_p))
1788 break;
1790 if (memb == 0)
1791 return 0;
1793 else
1794 return 0;
1797 /* comptypes said ok, but record if it said to warn. */
1798 if (newval > val)
1799 val = newval;
1801 args1 = TREE_CHAIN (args1);
1802 args2 = TREE_CHAIN (args2);
1806 /* Compute the size to increment a pointer by. When a function type or void
1807 type or incomplete type is passed, size_one_node is returned.
1808 This function does not emit any diagnostics; the caller is responsible
1809 for that. */
1811 static tree
1812 c_size_in_bytes (const_tree type)
1814 enum tree_code code = TREE_CODE (type);
1816 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1817 || !COMPLETE_TYPE_P (type))
1818 return size_one_node;
1820 /* Convert in case a char is more than one unit. */
1821 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1822 size_int (TYPE_PRECISION (char_type_node)
1823 / BITS_PER_UNIT));
1826 /* Return either DECL or its known constant value (if it has one). */
1828 tree
1829 decl_constant_value (tree decl)
1831 if (/* Don't change a variable array bound or initial value to a constant
1832 in a place where a variable is invalid. Note that DECL_INITIAL
1833 isn't valid for a PARM_DECL. */
1834 current_function_decl != 0
1835 && TREE_CODE (decl) != PARM_DECL
1836 && !TREE_THIS_VOLATILE (decl)
1837 && TREE_READONLY (decl)
1838 && DECL_INITIAL (decl) != 0
1839 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1840 /* This is invalid if initial value is not constant.
1841 If it has either a function call, a memory reference,
1842 or a variable, then re-evaluating it could give different results. */
1843 && TREE_CONSTANT (DECL_INITIAL (decl))
1844 /* Check for cases where this is sub-optimal, even though valid. */
1845 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1846 return DECL_INITIAL (decl);
1847 return decl;
1850 /* Convert the array expression EXP to a pointer. */
1851 static tree
1852 array_to_pointer_conversion (location_t loc, tree exp)
1854 tree orig_exp = exp;
1855 tree type = TREE_TYPE (exp);
1856 tree adr;
1857 tree restype = TREE_TYPE (type);
1858 tree ptrtype;
1860 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1862 STRIP_TYPE_NOPS (exp);
1864 if (TREE_NO_WARNING (orig_exp))
1865 TREE_NO_WARNING (exp) = 1;
1867 ptrtype = build_pointer_type (restype);
1869 if (INDIRECT_REF_P (exp))
1870 return convert (ptrtype, TREE_OPERAND (exp, 0));
1872 /* In C++ array compound literals are temporary objects unless they are
1873 const or appear in namespace scope, so they are destroyed too soon
1874 to use them for much of anything (c++/53220). */
1875 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1877 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1878 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1879 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1880 "converting an array compound literal to a pointer "
1881 "is ill-formed in C++");
1884 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1885 return convert (ptrtype, adr);
1888 /* Convert the function expression EXP to a pointer. */
1889 static tree
1890 function_to_pointer_conversion (location_t loc, tree exp)
1892 tree orig_exp = exp;
1894 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1896 STRIP_TYPE_NOPS (exp);
1898 if (TREE_NO_WARNING (orig_exp))
1899 TREE_NO_WARNING (exp) = 1;
1901 return build_unary_op (loc, ADDR_EXPR, exp, false);
1904 /* Mark EXP as read, not just set, for set but not used -Wunused
1905 warning purposes. */
1907 void
1908 mark_exp_read (tree exp)
1910 switch (TREE_CODE (exp))
1912 case VAR_DECL:
1913 case PARM_DECL:
1914 DECL_READ_P (exp) = 1;
1915 break;
1916 case ARRAY_REF:
1917 case COMPONENT_REF:
1918 case MODIFY_EXPR:
1919 case REALPART_EXPR:
1920 case IMAGPART_EXPR:
1921 CASE_CONVERT:
1922 case ADDR_EXPR:
1923 case VIEW_CONVERT_EXPR:
1924 mark_exp_read (TREE_OPERAND (exp, 0));
1925 break;
1926 case COMPOUND_EXPR:
1927 case C_MAYBE_CONST_EXPR:
1928 mark_exp_read (TREE_OPERAND (exp, 1));
1929 break;
1930 default:
1931 break;
1935 /* Perform the default conversion of arrays and functions to pointers.
1936 Return the result of converting EXP. For any other expression, just
1937 return EXP.
1939 LOC is the location of the expression. */
1941 struct c_expr
1942 default_function_array_conversion (location_t loc, struct c_expr exp)
1944 tree orig_exp = exp.value;
1945 tree type = TREE_TYPE (exp.value);
1946 enum tree_code code = TREE_CODE (type);
1948 switch (code)
1950 case ARRAY_TYPE:
1952 bool not_lvalue = false;
1953 bool lvalue_array_p;
1955 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1956 || CONVERT_EXPR_P (exp.value))
1957 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1959 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1960 not_lvalue = true;
1961 exp.value = TREE_OPERAND (exp.value, 0);
1964 if (TREE_NO_WARNING (orig_exp))
1965 TREE_NO_WARNING (exp.value) = 1;
1967 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1968 if (!flag_isoc99 && !lvalue_array_p)
1970 /* Before C99, non-lvalue arrays do not decay to pointers.
1971 Normally, using such an array would be invalid; but it can
1972 be used correctly inside sizeof or as a statement expression.
1973 Thus, do not give an error here; an error will result later. */
1974 return exp;
1977 exp.value = array_to_pointer_conversion (loc, exp.value);
1979 break;
1980 case FUNCTION_TYPE:
1981 exp.value = function_to_pointer_conversion (loc, exp.value);
1982 break;
1983 default:
1984 break;
1987 return exp;
1990 struct c_expr
1991 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1993 mark_exp_read (exp.value);
1994 return default_function_array_conversion (loc, exp);
1997 /* Return whether EXPR should be treated as an atomic lvalue for the
1998 purposes of load and store handling. */
2000 static bool
2001 really_atomic_lvalue (tree expr)
2003 if (error_operand_p (expr))
2004 return false;
2005 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2006 return false;
2007 if (!lvalue_p (expr))
2008 return false;
2010 /* Ignore _Atomic on register variables, since their addresses can't
2011 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2012 sequences wouldn't work. Ignore _Atomic on structures containing
2013 bit-fields, since accessing elements of atomic structures or
2014 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2015 it's undefined at translation time or execution time, and the
2016 normal atomic sequences again wouldn't work. */
2017 while (handled_component_p (expr))
2019 if (TREE_CODE (expr) == COMPONENT_REF
2020 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2021 return false;
2022 expr = TREE_OPERAND (expr, 0);
2024 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2025 return false;
2026 return true;
2029 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2030 including converting functions and arrays to pointers if CONVERT_P.
2031 If READ_P, also mark the expression as having been read. */
2033 struct c_expr
2034 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2035 bool convert_p, bool read_p)
2037 if (read_p)
2038 mark_exp_read (exp.value);
2039 if (convert_p)
2040 exp = default_function_array_conversion (loc, exp);
2041 if (really_atomic_lvalue (exp.value))
2043 vec<tree, va_gc> *params;
2044 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2045 tree expr_type = TREE_TYPE (exp.value);
2046 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2047 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2049 gcc_assert (TYPE_ATOMIC (expr_type));
2051 /* Expansion of a generic atomic load may require an addition
2052 element, so allocate enough to prevent a resize. */
2053 vec_alloc (params, 4);
2055 /* Remove the qualifiers for the rest of the expressions and
2056 create the VAL temp variable to hold the RHS. */
2057 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2058 tmp = create_tmp_var_raw (nonatomic_type);
2059 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2060 TREE_ADDRESSABLE (tmp) = 1;
2061 TREE_NO_WARNING (tmp) = 1;
2063 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2064 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2065 params->quick_push (expr_addr);
2066 params->quick_push (tmp_addr);
2067 params->quick_push (seq_cst);
2068 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2070 /* EXPR is always read. */
2071 mark_exp_read (exp.value);
2073 /* Return tmp which contains the value loaded. */
2074 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2075 NULL_TREE, NULL_TREE);
2077 return exp;
2080 /* EXP is an expression of integer type. Apply the integer promotions
2081 to it and return the promoted value. */
2083 tree
2084 perform_integral_promotions (tree exp)
2086 tree type = TREE_TYPE (exp);
2087 enum tree_code code = TREE_CODE (type);
2089 gcc_assert (INTEGRAL_TYPE_P (type));
2091 /* Normally convert enums to int,
2092 but convert wide enums to something wider. */
2093 if (code == ENUMERAL_TYPE)
2095 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2096 TYPE_PRECISION (integer_type_node)),
2097 ((TYPE_PRECISION (type)
2098 >= TYPE_PRECISION (integer_type_node))
2099 && TYPE_UNSIGNED (type)));
2101 return convert (type, exp);
2104 /* ??? This should no longer be needed now bit-fields have their
2105 proper types. */
2106 if (TREE_CODE (exp) == COMPONENT_REF
2107 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2108 /* If it's thinner than an int, promote it like a
2109 c_promoting_integer_type_p, otherwise leave it alone. */
2110 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2111 TYPE_PRECISION (integer_type_node)))
2112 return convert (integer_type_node, exp);
2114 if (c_promoting_integer_type_p (type))
2116 /* Preserve unsignedness if not really getting any wider. */
2117 if (TYPE_UNSIGNED (type)
2118 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2119 return convert (unsigned_type_node, exp);
2121 return convert (integer_type_node, exp);
2124 return exp;
2128 /* Perform default promotions for C data used in expressions.
2129 Enumeral types or short or char are converted to int.
2130 In addition, manifest constants symbols are replaced by their values. */
2132 tree
2133 default_conversion (tree exp)
2135 tree orig_exp;
2136 tree type = TREE_TYPE (exp);
2137 enum tree_code code = TREE_CODE (type);
2138 tree promoted_type;
2140 mark_exp_read (exp);
2142 /* Functions and arrays have been converted during parsing. */
2143 gcc_assert (code != FUNCTION_TYPE);
2144 if (code == ARRAY_TYPE)
2145 return exp;
2147 /* Constants can be used directly unless they're not loadable. */
2148 if (TREE_CODE (exp) == CONST_DECL)
2149 exp = DECL_INITIAL (exp);
2151 /* Strip no-op conversions. */
2152 orig_exp = exp;
2153 STRIP_TYPE_NOPS (exp);
2155 if (TREE_NO_WARNING (orig_exp))
2156 TREE_NO_WARNING (exp) = 1;
2158 if (code == VOID_TYPE)
2160 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2161 "void value not ignored as it ought to be");
2162 return error_mark_node;
2165 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2166 if (exp == error_mark_node)
2167 return error_mark_node;
2169 promoted_type = targetm.promoted_type (type);
2170 if (promoted_type)
2171 return convert (promoted_type, exp);
2173 if (INTEGRAL_TYPE_P (type))
2174 return perform_integral_promotions (exp);
2176 return exp;
2179 /* Look up COMPONENT in a structure or union TYPE.
2181 If the component name is not found, returns NULL_TREE. Otherwise,
2182 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2183 stepping down the chain to the component, which is in the last
2184 TREE_VALUE of the list. Normally the list is of length one, but if
2185 the component is embedded within (nested) anonymous structures or
2186 unions, the list steps down the chain to the component. */
2188 static tree
2189 lookup_field (tree type, tree component)
2191 tree field;
2193 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2194 to the field elements. Use a binary search on this array to quickly
2195 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2196 will always be set for structures which have many elements. */
2198 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2200 int bot, top, half;
2201 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2203 field = TYPE_FIELDS (type);
2204 bot = 0;
2205 top = TYPE_LANG_SPECIFIC (type)->s->len;
2206 while (top - bot > 1)
2208 half = (top - bot + 1) >> 1;
2209 field = field_array[bot+half];
2211 if (DECL_NAME (field) == NULL_TREE)
2213 /* Step through all anon unions in linear fashion. */
2214 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2216 field = field_array[bot++];
2217 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2219 tree anon = lookup_field (TREE_TYPE (field), component);
2221 if (anon)
2222 return tree_cons (NULL_TREE, field, anon);
2224 /* The Plan 9 compiler permits referring
2225 directly to an anonymous struct/union field
2226 using a typedef name. */
2227 if (flag_plan9_extensions
2228 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2229 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2230 == TYPE_DECL)
2231 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2232 == component))
2233 break;
2237 /* Entire record is only anon unions. */
2238 if (bot > top)
2239 return NULL_TREE;
2241 /* Restart the binary search, with new lower bound. */
2242 continue;
2245 if (DECL_NAME (field) == component)
2246 break;
2247 if (DECL_NAME (field) < component)
2248 bot += half;
2249 else
2250 top = bot + half;
2253 if (DECL_NAME (field_array[bot]) == component)
2254 field = field_array[bot];
2255 else if (DECL_NAME (field) != component)
2256 return NULL_TREE;
2258 else
2260 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2262 if (DECL_NAME (field) == NULL_TREE
2263 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2265 tree anon = lookup_field (TREE_TYPE (field), component);
2267 if (anon)
2268 return tree_cons (NULL_TREE, field, anon);
2270 /* The Plan 9 compiler permits referring directly to an
2271 anonymous struct/union field using a typedef
2272 name. */
2273 if (flag_plan9_extensions
2274 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2275 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2276 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2277 == component))
2278 break;
2281 if (DECL_NAME (field) == component)
2282 break;
2285 if (field == NULL_TREE)
2286 return NULL_TREE;
2289 return tree_cons (NULL_TREE, field, NULL_TREE);
2292 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2294 static void
2295 lookup_field_fuzzy_find_candidates (tree type, tree component,
2296 vec<tree> *candidates)
2298 tree field;
2299 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2301 if (DECL_NAME (field) == NULL_TREE
2302 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2303 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2304 candidates);
2306 if (DECL_NAME (field))
2307 candidates->safe_push (DECL_NAME (field));
2311 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2312 rather than returning a TREE_LIST for an exact match. */
2314 static tree
2315 lookup_field_fuzzy (tree type, tree component)
2317 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2319 /* First, gather a list of candidates. */
2320 auto_vec <tree> candidates;
2322 lookup_field_fuzzy_find_candidates (type, component,
2323 &candidates);
2325 return find_closest_identifier (component, &candidates);
2328 /* Support function for build_component_ref's error-handling.
2330 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2331 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2333 static bool
2334 should_suggest_deref_p (tree datum_type)
2336 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2337 allows "." for ptrs; we could be handling a failed attempt
2338 to access a property. */
2339 if (c_dialect_objc ())
2340 return false;
2342 /* Only suggest it for pointers... */
2343 if (TREE_CODE (datum_type) != POINTER_TYPE)
2344 return false;
2346 /* ...to structs/unions. */
2347 tree underlying_type = TREE_TYPE (datum_type);
2348 enum tree_code code = TREE_CODE (underlying_type);
2349 if (code == RECORD_TYPE || code == UNION_TYPE)
2350 return true;
2351 else
2352 return false;
2355 /* Make an expression to refer to the COMPONENT field of structure or
2356 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2357 location of the COMPONENT_REF. COMPONENT_LOC is the location
2358 of COMPONENT. */
2360 tree
2361 build_component_ref (location_t loc, tree datum, tree component,
2362 location_t component_loc)
2364 tree type = TREE_TYPE (datum);
2365 enum tree_code code = TREE_CODE (type);
2366 tree field = NULL;
2367 tree ref;
2368 bool datum_lvalue = lvalue_p (datum);
2370 if (!objc_is_public (datum, component))
2371 return error_mark_node;
2373 /* Detect Objective-C property syntax object.property. */
2374 if (c_dialect_objc ()
2375 && (ref = objc_maybe_build_component_ref (datum, component)))
2376 return ref;
2378 /* See if there is a field or component with name COMPONENT. */
2380 if (code == RECORD_TYPE || code == UNION_TYPE)
2382 if (!COMPLETE_TYPE_P (type))
2384 c_incomplete_type_error (loc, NULL_TREE, type);
2385 return error_mark_node;
2388 field = lookup_field (type, component);
2390 if (!field)
2392 tree guessed_id = lookup_field_fuzzy (type, component);
2393 if (guessed_id)
2395 /* Attempt to provide a fixit replacement hint, if
2396 we have a valid range for the component. */
2397 location_t reported_loc
2398 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2399 gcc_rich_location rich_loc (reported_loc);
2400 if (component_loc != UNKNOWN_LOCATION)
2401 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2402 error_at_rich_loc
2403 (&rich_loc,
2404 "%qT has no member named %qE; did you mean %qE?",
2405 type, component, guessed_id);
2407 else
2408 error_at (loc, "%qT has no member named %qE", type, component);
2409 return error_mark_node;
2412 /* Accessing elements of atomic structures or unions is undefined
2413 behavior (C11 6.5.2.3#5). */
2414 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2416 if (code == RECORD_TYPE)
2417 warning_at (loc, 0, "accessing a member %qE of an atomic "
2418 "structure %qE", component, datum);
2419 else
2420 warning_at (loc, 0, "accessing a member %qE of an atomic "
2421 "union %qE", component, datum);
2424 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2425 This might be better solved in future the way the C++ front
2426 end does it - by giving the anonymous entities each a
2427 separate name and type, and then have build_component_ref
2428 recursively call itself. We can't do that here. */
2431 tree subdatum = TREE_VALUE (field);
2432 int quals;
2433 tree subtype;
2434 bool use_datum_quals;
2436 if (TREE_TYPE (subdatum) == error_mark_node)
2437 return error_mark_node;
2439 /* If this is an rvalue, it does not have qualifiers in C
2440 standard terms and we must avoid propagating such
2441 qualifiers down to a non-lvalue array that is then
2442 converted to a pointer. */
2443 use_datum_quals = (datum_lvalue
2444 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2446 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2447 if (use_datum_quals)
2448 quals |= TYPE_QUALS (TREE_TYPE (datum));
2449 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2451 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2452 NULL_TREE);
2453 SET_EXPR_LOCATION (ref, loc);
2454 if (TREE_READONLY (subdatum)
2455 || (use_datum_quals && TREE_READONLY (datum)))
2456 TREE_READONLY (ref) = 1;
2457 if (TREE_THIS_VOLATILE (subdatum)
2458 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2459 TREE_THIS_VOLATILE (ref) = 1;
2461 if (TREE_DEPRECATED (subdatum))
2462 warn_deprecated_use (subdatum, NULL_TREE);
2464 datum = ref;
2466 field = TREE_CHAIN (field);
2468 while (field);
2470 return ref;
2472 else if (should_suggest_deref_p (type))
2474 /* Special-case the error message for "ptr.field" for the case
2475 where the user has confused "." vs "->". */
2476 rich_location richloc (line_table, loc);
2477 /* "loc" should be the "." token. */
2478 richloc.add_fixit_replace ("->");
2479 error_at_rich_loc (&richloc,
2480 "%qE is a pointer; did you mean to use %<->%>?",
2481 datum);
2482 return error_mark_node;
2484 else if (code != ERROR_MARK)
2485 error_at (loc,
2486 "request for member %qE in something not a structure or union",
2487 component);
2489 return error_mark_node;
2492 /* Given an expression PTR for a pointer, return an expression
2493 for the value pointed to.
2494 ERRORSTRING is the name of the operator to appear in error messages.
2496 LOC is the location to use for the generated tree. */
2498 tree
2499 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2501 tree pointer = default_conversion (ptr);
2502 tree type = TREE_TYPE (pointer);
2503 tree ref;
2505 if (TREE_CODE (type) == POINTER_TYPE)
2507 if (CONVERT_EXPR_P (pointer)
2508 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2510 /* If a warning is issued, mark it to avoid duplicates from
2511 the backend. This only needs to be done at
2512 warn_strict_aliasing > 2. */
2513 if (warn_strict_aliasing > 2)
2514 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2515 type, TREE_OPERAND (pointer, 0)))
2516 TREE_NO_WARNING (pointer) = 1;
2519 if (TREE_CODE (pointer) == ADDR_EXPR
2520 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2521 == TREE_TYPE (type)))
2523 ref = TREE_OPERAND (pointer, 0);
2524 protected_set_expr_location (ref, loc);
2525 return ref;
2527 else
2529 tree t = TREE_TYPE (type);
2531 ref = build1 (INDIRECT_REF, t, pointer);
2533 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2535 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2537 error_at (loc, "dereferencing pointer to incomplete type "
2538 "%qT", t);
2539 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2541 return error_mark_node;
2543 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2544 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2546 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2547 so that we get the proper error message if the result is used
2548 to assign to. Also, &* is supposed to be a no-op.
2549 And ANSI C seems to specify that the type of the result
2550 should be the const type. */
2551 /* A de-reference of a pointer to const is not a const. It is valid
2552 to change it via some other pointer. */
2553 TREE_READONLY (ref) = TYPE_READONLY (t);
2554 TREE_SIDE_EFFECTS (ref)
2555 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2556 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2557 protected_set_expr_location (ref, loc);
2558 return ref;
2561 else if (TREE_CODE (pointer) != ERROR_MARK)
2562 invalid_indirection_error (loc, type, errstring);
2564 return error_mark_node;
2567 /* This handles expressions of the form "a[i]", which denotes
2568 an array reference.
2570 This is logically equivalent in C to *(a+i), but we may do it differently.
2571 If A is a variable or a member, we generate a primitive ARRAY_REF.
2572 This avoids forcing the array out of registers, and can work on
2573 arrays that are not lvalues (for example, members of structures returned
2574 by functions).
2576 For vector types, allow vector[i] but not i[vector], and create
2577 *(((type*)&vectortype) + i) for the expression.
2579 LOC is the location to use for the returned expression. */
2581 tree
2582 build_array_ref (location_t loc, tree array, tree index)
2584 tree ret;
2585 bool swapped = false;
2586 if (TREE_TYPE (array) == error_mark_node
2587 || TREE_TYPE (index) == error_mark_node)
2588 return error_mark_node;
2590 if (flag_cilkplus && contains_array_notation_expr (index))
2592 size_t rank = 0;
2593 if (!find_rank (loc, index, index, true, &rank))
2594 return error_mark_node;
2595 if (rank > 1)
2597 error_at (loc, "rank of the array's index is greater than 1");
2598 return error_mark_node;
2601 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2602 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2603 /* Allow vector[index] but not index[vector]. */
2604 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2606 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2607 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2609 error_at (loc,
2610 "subscripted value is neither array nor pointer nor vector");
2612 return error_mark_node;
2614 std::swap (array, index);
2615 swapped = true;
2618 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2620 error_at (loc, "array subscript is not an integer");
2621 return error_mark_node;
2624 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2626 error_at (loc, "subscripted value is pointer to function");
2627 return error_mark_node;
2630 /* ??? Existing practice has been to warn only when the char
2631 index is syntactically the index, not for char[array]. */
2632 if (!swapped)
2633 warn_array_subscript_with_type_char (loc, index);
2635 /* Apply default promotions *after* noticing character types. */
2636 index = default_conversion (index);
2637 if (index == error_mark_node)
2638 return error_mark_node;
2640 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2642 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2643 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2645 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2647 tree rval, type;
2649 /* An array that is indexed by a non-constant
2650 cannot be stored in a register; we must be able to do
2651 address arithmetic on its address.
2652 Likewise an array of elements of variable size. */
2653 if (TREE_CODE (index) != INTEGER_CST
2654 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2655 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2657 if (!c_mark_addressable (array))
2658 return error_mark_node;
2660 /* An array that is indexed by a constant value which is not within
2661 the array bounds cannot be stored in a register either; because we
2662 would get a crash in store_bit_field/extract_bit_field when trying
2663 to access a non-existent part of the register. */
2664 if (TREE_CODE (index) == INTEGER_CST
2665 && TYPE_DOMAIN (TREE_TYPE (array))
2666 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2668 if (!c_mark_addressable (array))
2669 return error_mark_node;
2672 if ((pedantic || warn_c90_c99_compat)
2673 && ! was_vector)
2675 tree foo = array;
2676 while (TREE_CODE (foo) == COMPONENT_REF)
2677 foo = TREE_OPERAND (foo, 0);
2678 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2679 pedwarn (loc, OPT_Wpedantic,
2680 "ISO C forbids subscripting %<register%> array");
2681 else if (!lvalue_p (foo))
2682 pedwarn_c90 (loc, OPT_Wpedantic,
2683 "ISO C90 forbids subscripting non-lvalue "
2684 "array");
2687 type = TREE_TYPE (TREE_TYPE (array));
2688 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2689 /* Array ref is const/volatile if the array elements are
2690 or if the array is. */
2691 TREE_READONLY (rval)
2692 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2693 | TREE_READONLY (array));
2694 TREE_SIDE_EFFECTS (rval)
2695 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2696 | TREE_SIDE_EFFECTS (array));
2697 TREE_THIS_VOLATILE (rval)
2698 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2699 /* This was added by rms on 16 Nov 91.
2700 It fixes vol struct foo *a; a->elts[1]
2701 in an inline function.
2702 Hope it doesn't break something else. */
2703 | TREE_THIS_VOLATILE (array));
2704 ret = require_complete_type (loc, rval);
2705 protected_set_expr_location (ret, loc);
2706 if (non_lvalue)
2707 ret = non_lvalue_loc (loc, ret);
2708 return ret;
2710 else
2712 tree ar = default_conversion (array);
2714 if (ar == error_mark_node)
2715 return ar;
2717 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2718 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2720 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2721 index, 0),
2722 RO_ARRAY_INDEXING);
2723 if (non_lvalue)
2724 ret = non_lvalue_loc (loc, ret);
2725 return ret;
2729 /* Build an external reference to identifier ID. FUN indicates
2730 whether this will be used for a function call. LOC is the source
2731 location of the identifier. This sets *TYPE to the type of the
2732 identifier, which is not the same as the type of the returned value
2733 for CONST_DECLs defined as enum constants. If the type of the
2734 identifier is not available, *TYPE is set to NULL. */
2735 tree
2736 build_external_ref (location_t loc, tree id, int fun, tree *type)
2738 tree ref;
2739 tree decl = lookup_name (id);
2741 /* In Objective-C, an instance variable (ivar) may be preferred to
2742 whatever lookup_name() found. */
2743 decl = objc_lookup_ivar (decl, id);
2745 *type = NULL;
2746 if (decl && decl != error_mark_node)
2748 ref = decl;
2749 *type = TREE_TYPE (ref);
2751 else if (fun)
2752 /* Implicit function declaration. */
2753 ref = implicitly_declare (loc, id);
2754 else if (decl == error_mark_node)
2755 /* Don't complain about something that's already been
2756 complained about. */
2757 return error_mark_node;
2758 else
2760 undeclared_variable (loc, id);
2761 return error_mark_node;
2764 if (TREE_TYPE (ref) == error_mark_node)
2765 return error_mark_node;
2767 if (TREE_DEPRECATED (ref))
2768 warn_deprecated_use (ref, NULL_TREE);
2770 /* Recursive call does not count as usage. */
2771 if (ref != current_function_decl)
2773 TREE_USED (ref) = 1;
2776 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2778 if (!in_sizeof && !in_typeof)
2779 C_DECL_USED (ref) = 1;
2780 else if (DECL_INITIAL (ref) == 0
2781 && DECL_EXTERNAL (ref)
2782 && !TREE_PUBLIC (ref))
2783 record_maybe_used_decl (ref);
2786 if (TREE_CODE (ref) == CONST_DECL)
2788 used_types_insert (TREE_TYPE (ref));
2790 if (warn_cxx_compat
2791 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2792 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2794 warning_at (loc, OPT_Wc___compat,
2795 ("enum constant defined in struct or union "
2796 "is not visible in C++"));
2797 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2800 ref = DECL_INITIAL (ref);
2801 TREE_CONSTANT (ref) = 1;
2803 else if (current_function_decl != 0
2804 && !DECL_FILE_SCOPE_P (current_function_decl)
2805 && (VAR_OR_FUNCTION_DECL_P (ref)
2806 || TREE_CODE (ref) == PARM_DECL))
2808 tree context = decl_function_context (ref);
2810 if (context != 0 && context != current_function_decl)
2811 DECL_NONLOCAL (ref) = 1;
2813 /* C99 6.7.4p3: An inline definition of a function with external
2814 linkage ... shall not contain a reference to an identifier with
2815 internal linkage. */
2816 else if (current_function_decl != 0
2817 && DECL_DECLARED_INLINE_P (current_function_decl)
2818 && DECL_EXTERNAL (current_function_decl)
2819 && VAR_OR_FUNCTION_DECL_P (ref)
2820 && (!VAR_P (ref) || TREE_STATIC (ref))
2821 && ! TREE_PUBLIC (ref)
2822 && DECL_CONTEXT (ref) != current_function_decl)
2823 record_inline_static (loc, current_function_decl, ref,
2824 csi_internal);
2826 return ref;
2829 /* Record details of decls possibly used inside sizeof or typeof. */
2830 struct maybe_used_decl
2832 /* The decl. */
2833 tree decl;
2834 /* The level seen at (in_sizeof + in_typeof). */
2835 int level;
2836 /* The next one at this level or above, or NULL. */
2837 struct maybe_used_decl *next;
2840 static struct maybe_used_decl *maybe_used_decls;
2842 /* Record that DECL, an undefined static function reference seen
2843 inside sizeof or typeof, might be used if the operand of sizeof is
2844 a VLA type or the operand of typeof is a variably modified
2845 type. */
2847 static void
2848 record_maybe_used_decl (tree decl)
2850 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2851 t->decl = decl;
2852 t->level = in_sizeof + in_typeof;
2853 t->next = maybe_used_decls;
2854 maybe_used_decls = t;
2857 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2858 USED is false, just discard them. If it is true, mark them used
2859 (if no longer inside sizeof or typeof) or move them to the next
2860 level up (if still inside sizeof or typeof). */
2862 void
2863 pop_maybe_used (bool used)
2865 struct maybe_used_decl *p = maybe_used_decls;
2866 int cur_level = in_sizeof + in_typeof;
2867 while (p && p->level > cur_level)
2869 if (used)
2871 if (cur_level == 0)
2872 C_DECL_USED (p->decl) = 1;
2873 else
2874 p->level = cur_level;
2876 p = p->next;
2878 if (!used || cur_level == 0)
2879 maybe_used_decls = p;
2882 /* Return the result of sizeof applied to EXPR. */
2884 struct c_expr
2885 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2887 struct c_expr ret;
2888 if (expr.value == error_mark_node)
2890 ret.value = error_mark_node;
2891 ret.original_code = ERROR_MARK;
2892 ret.original_type = NULL;
2893 pop_maybe_used (false);
2895 else
2897 bool expr_const_operands = true;
2899 if (TREE_CODE (expr.value) == PARM_DECL
2900 && C_ARRAY_PARAMETER (expr.value))
2902 if (warning_at (loc, OPT_Wsizeof_array_argument,
2903 "%<sizeof%> on array function parameter %qE will "
2904 "return size of %qT", expr.value,
2905 expr.original_type))
2906 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2908 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2909 &expr_const_operands);
2910 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2911 c_last_sizeof_arg = expr.value;
2912 ret.original_code = SIZEOF_EXPR;
2913 ret.original_type = NULL;
2914 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2916 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2917 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2918 folded_expr, ret.value);
2919 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2920 SET_EXPR_LOCATION (ret.value, loc);
2922 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2924 return ret;
2927 /* Return the result of sizeof applied to T, a structure for the type
2928 name passed to sizeof (rather than the type itself). LOC is the
2929 location of the original expression. */
2931 struct c_expr
2932 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2934 tree type;
2935 struct c_expr ret;
2936 tree type_expr = NULL_TREE;
2937 bool type_expr_const = true;
2938 type = groktypename (t, &type_expr, &type_expr_const);
2939 ret.value = c_sizeof (loc, type);
2940 c_last_sizeof_arg = type;
2941 ret.original_code = SIZEOF_EXPR;
2942 ret.original_type = NULL;
2943 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2944 && c_vla_type_p (type))
2946 /* If the type is a [*] array, it is a VLA but is represented as
2947 having a size of zero. In such a case we must ensure that
2948 the result of sizeof does not get folded to a constant by
2949 c_fully_fold, because if the size is evaluated the result is
2950 not constant and so constraints on zero or negative size
2951 arrays must not be applied when this sizeof call is inside
2952 another array declarator. */
2953 if (!type_expr)
2954 type_expr = integer_zero_node;
2955 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2956 type_expr, ret.value);
2957 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2959 pop_maybe_used (type != error_mark_node
2960 ? C_TYPE_VARIABLE_SIZE (type) : false);
2961 return ret;
2964 /* Build a function call to function FUNCTION with parameters PARAMS.
2965 The function call is at LOC.
2966 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2967 TREE_VALUE of each node is a parameter-expression.
2968 FUNCTION's data type may be a function type or a pointer-to-function. */
2970 tree
2971 build_function_call (location_t loc, tree function, tree params)
2973 vec<tree, va_gc> *v;
2974 tree ret;
2976 vec_alloc (v, list_length (params));
2977 for (; params; params = TREE_CHAIN (params))
2978 v->quick_push (TREE_VALUE (params));
2979 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2980 vec_free (v);
2981 return ret;
2984 /* Give a note about the location of the declaration of DECL. */
2986 static void
2987 inform_declaration (tree decl)
2989 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2990 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2993 /* Build a function call to function FUNCTION with parameters PARAMS.
2994 ORIGTYPES, if not NULL, is a vector of types; each element is
2995 either NULL or the original type of the corresponding element in
2996 PARAMS. The original type may differ from TREE_TYPE of the
2997 parameter for enums. FUNCTION's data type may be a function type
2998 or pointer-to-function. This function changes the elements of
2999 PARAMS. */
3001 tree
3002 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3003 tree function, vec<tree, va_gc> *params,
3004 vec<tree, va_gc> *origtypes)
3006 tree fntype, fundecl = 0;
3007 tree name = NULL_TREE, result;
3008 tree tem;
3009 int nargs;
3010 tree *argarray;
3013 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3014 STRIP_TYPE_NOPS (function);
3016 /* Convert anything with function type to a pointer-to-function. */
3017 if (TREE_CODE (function) == FUNCTION_DECL)
3019 name = DECL_NAME (function);
3021 if (flag_tm)
3022 tm_malloc_replacement (function);
3023 fundecl = function;
3024 /* Atomic functions have type checking/casting already done. They are
3025 often rewritten and don't match the original parameter list. */
3026 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3027 origtypes = NULL;
3029 if (flag_cilkplus
3030 && is_cilkplus_reduce_builtin (function))
3031 origtypes = NULL;
3033 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3034 function = function_to_pointer_conversion (loc, function);
3036 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3037 expressions, like those used for ObjC messenger dispatches. */
3038 if (params && !params->is_empty ())
3039 function = objc_rewrite_function_call (function, (*params)[0]);
3041 function = c_fully_fold (function, false, NULL);
3043 fntype = TREE_TYPE (function);
3045 if (TREE_CODE (fntype) == ERROR_MARK)
3046 return error_mark_node;
3048 if (!(TREE_CODE (fntype) == POINTER_TYPE
3049 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3051 if (!flag_diagnostics_show_caret)
3052 error_at (loc,
3053 "called object %qE is not a function or function pointer",
3054 function);
3055 else if (DECL_P (function))
3057 error_at (loc,
3058 "called object %qD is not a function or function pointer",
3059 function);
3060 inform_declaration (function);
3062 else
3063 error_at (loc,
3064 "called object is not a function or function pointer");
3065 return error_mark_node;
3068 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3069 current_function_returns_abnormally = 1;
3071 /* fntype now gets the type of function pointed to. */
3072 fntype = TREE_TYPE (fntype);
3074 /* Convert the parameters to the types declared in the
3075 function prototype, or apply default promotions. */
3077 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3078 origtypes, function, fundecl);
3079 if (nargs < 0)
3080 return error_mark_node;
3082 /* Check that the function is called through a compatible prototype.
3083 If it is not, warn. */
3084 if (CONVERT_EXPR_P (function)
3085 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3086 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3087 && !comptypes (fntype, TREE_TYPE (tem)))
3089 tree return_type = TREE_TYPE (fntype);
3091 /* This situation leads to run-time undefined behavior. We can't,
3092 therefore, simply error unless we can prove that all possible
3093 executions of the program must execute the code. */
3094 warning_at (loc, 0, "function called through a non-compatible type");
3096 if (VOID_TYPE_P (return_type)
3097 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3098 pedwarn (loc, 0,
3099 "function with qualified void return type called");
3102 argarray = vec_safe_address (params);
3104 /* Check that arguments to builtin functions match the expectations. */
3105 if (fundecl
3106 && DECL_BUILT_IN (fundecl)
3107 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3108 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3109 argarray))
3110 return error_mark_node;
3112 /* Check that the arguments to the function are valid. */
3113 bool warned_p = check_function_arguments (loc, fntype, nargs, argarray);
3115 if (name != NULL_TREE
3116 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3118 if (require_constant_value)
3119 result
3120 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3121 function, nargs, argarray);
3122 else
3123 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3124 function, nargs, argarray);
3125 if (TREE_CODE (result) == NOP_EXPR
3126 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3127 STRIP_TYPE_NOPS (result);
3129 else
3130 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3131 function, nargs, argarray);
3132 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3133 later. */
3134 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3135 TREE_NO_WARNING (result) = 1;
3137 /* In this improbable scenario, a nested function returns a VM type.
3138 Create a TARGET_EXPR so that the call always has a LHS, much as
3139 what the C++ FE does for functions returning non-PODs. */
3140 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3142 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3143 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3144 NULL_TREE, NULL_TREE);
3147 if (VOID_TYPE_P (TREE_TYPE (result)))
3149 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3150 pedwarn (loc, 0,
3151 "function with qualified void return type called");
3152 return result;
3154 return require_complete_type (loc, result);
3157 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3159 tree
3160 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3161 tree function, vec<tree, va_gc> *params,
3162 vec<tree, va_gc> *origtypes)
3164 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3165 STRIP_TYPE_NOPS (function);
3167 /* Convert anything with function type to a pointer-to-function. */
3168 if (TREE_CODE (function) == FUNCTION_DECL)
3170 /* Implement type-directed function overloading for builtins.
3171 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3172 handle all the type checking. The result is a complete expression
3173 that implements this function call. */
3174 tree tem = resolve_overloaded_builtin (loc, function, params);
3175 if (tem)
3176 return tem;
3178 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3181 /* Convert the argument expressions in the vector VALUES
3182 to the types in the list TYPELIST.
3184 If TYPELIST is exhausted, or when an element has NULL as its type,
3185 perform the default conversions.
3187 ORIGTYPES is the original types of the expressions in VALUES. This
3188 holds the type of enum values which have been converted to integral
3189 types. It may be NULL.
3191 FUNCTION is a tree for the called function. It is used only for
3192 error messages, where it is formatted with %qE.
3194 This is also where warnings about wrong number of args are generated.
3196 ARG_LOC are locations of function arguments (if any).
3198 Returns the actual number of arguments processed (which may be less
3199 than the length of VALUES in some error situations), or -1 on
3200 failure. */
3202 static int
3203 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3204 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3205 tree function, tree fundecl)
3207 tree typetail, val;
3208 unsigned int parmnum;
3209 bool error_args = false;
3210 const bool type_generic = fundecl
3211 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3212 bool type_generic_remove_excess_precision = false;
3213 bool type_generic_overflow_p = false;
3214 tree selector;
3216 /* Change pointer to function to the function itself for
3217 diagnostics. */
3218 if (TREE_CODE (function) == ADDR_EXPR
3219 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3220 function = TREE_OPERAND (function, 0);
3222 /* Handle an ObjC selector specially for diagnostics. */
3223 selector = objc_message_selector ();
3225 /* For type-generic built-in functions, determine whether excess
3226 precision should be removed (classification) or not
3227 (comparison). */
3228 if (type_generic
3229 && DECL_BUILT_IN (fundecl)
3230 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3232 switch (DECL_FUNCTION_CODE (fundecl))
3234 case BUILT_IN_ISFINITE:
3235 case BUILT_IN_ISINF:
3236 case BUILT_IN_ISINF_SIGN:
3237 case BUILT_IN_ISNAN:
3238 case BUILT_IN_ISNORMAL:
3239 case BUILT_IN_FPCLASSIFY:
3240 type_generic_remove_excess_precision = true;
3241 break;
3243 case BUILT_IN_ADD_OVERFLOW_P:
3244 case BUILT_IN_SUB_OVERFLOW_P:
3245 case BUILT_IN_MUL_OVERFLOW_P:
3246 /* The last argument of these type-generic builtins
3247 should not be promoted. */
3248 type_generic_overflow_p = true;
3249 break;
3251 default:
3252 break;
3255 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3256 return vec_safe_length (values);
3258 /* Scan the given expressions and types, producing individual
3259 converted arguments. */
3261 for (typetail = typelist, parmnum = 0;
3262 values && values->iterate (parmnum, &val);
3263 ++parmnum)
3265 tree type = typetail ? TREE_VALUE (typetail) : 0;
3266 tree valtype = TREE_TYPE (val);
3267 tree rname = function;
3268 int argnum = parmnum + 1;
3269 const char *invalid_func_diag;
3270 bool excess_precision = false;
3271 bool npc;
3272 tree parmval;
3273 /* Some __atomic_* builtins have additional hidden argument at
3274 position 0. */
3275 location_t ploc
3276 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3277 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3278 : input_location;
3280 if (type == void_type_node)
3282 if (selector)
3283 error_at (loc, "too many arguments to method %qE", selector);
3284 else
3285 error_at (loc, "too many arguments to function %qE", function);
3286 inform_declaration (fundecl);
3287 return error_args ? -1 : (int) parmnum;
3290 if (selector && argnum > 2)
3292 rname = selector;
3293 argnum -= 2;
3296 npc = null_pointer_constant_p (val);
3298 /* If there is excess precision and a prototype, convert once to
3299 the required type rather than converting via the semantic
3300 type. Likewise without a prototype a float value represented
3301 as long double should be converted once to double. But for
3302 type-generic classification functions excess precision must
3303 be removed here. */
3304 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3305 && (type || !type_generic || !type_generic_remove_excess_precision))
3307 val = TREE_OPERAND (val, 0);
3308 excess_precision = true;
3310 val = c_fully_fold (val, false, NULL);
3311 STRIP_TYPE_NOPS (val);
3313 val = require_complete_type (ploc, val);
3315 /* Some floating-point arguments must be promoted to double when
3316 no type is specified by a prototype. This applies to
3317 arguments of type float, and to architecture-specific types
3318 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3319 bool promote_float_arg = false;
3320 if (type == NULL_TREE
3321 && TREE_CODE (valtype) == REAL_TYPE
3322 && (TYPE_PRECISION (valtype)
3323 <= TYPE_PRECISION (double_type_node))
3324 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3325 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3326 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3328 /* Promote this argument, unless it has a _FloatN or
3329 _FloatNx type. */
3330 promote_float_arg = true;
3331 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3332 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3334 promote_float_arg = false;
3335 break;
3339 if (type != 0)
3341 /* Formal parm type is specified by a function prototype. */
3343 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3345 error_at (ploc, "type of formal parameter %d is incomplete",
3346 parmnum + 1);
3347 parmval = val;
3349 else
3351 tree origtype;
3353 /* Optionally warn about conversions that
3354 differ from the default conversions. */
3355 if (warn_traditional_conversion || warn_traditional)
3357 unsigned int formal_prec = TYPE_PRECISION (type);
3359 if (INTEGRAL_TYPE_P (type)
3360 && TREE_CODE (valtype) == REAL_TYPE)
3361 warning_at (ploc, OPT_Wtraditional_conversion,
3362 "passing argument %d of %qE as integer rather "
3363 "than floating due to prototype",
3364 argnum, rname);
3365 if (INTEGRAL_TYPE_P (type)
3366 && TREE_CODE (valtype) == COMPLEX_TYPE)
3367 warning_at (ploc, OPT_Wtraditional_conversion,
3368 "passing argument %d of %qE as integer rather "
3369 "than complex due to prototype",
3370 argnum, rname);
3371 else if (TREE_CODE (type) == COMPLEX_TYPE
3372 && TREE_CODE (valtype) == REAL_TYPE)
3373 warning_at (ploc, OPT_Wtraditional_conversion,
3374 "passing argument %d of %qE as complex rather "
3375 "than floating due to prototype",
3376 argnum, rname);
3377 else if (TREE_CODE (type) == REAL_TYPE
3378 && INTEGRAL_TYPE_P (valtype))
3379 warning_at (ploc, OPT_Wtraditional_conversion,
3380 "passing argument %d of %qE as floating rather "
3381 "than integer due to prototype",
3382 argnum, rname);
3383 else if (TREE_CODE (type) == COMPLEX_TYPE
3384 && INTEGRAL_TYPE_P (valtype))
3385 warning_at (ploc, OPT_Wtraditional_conversion,
3386 "passing argument %d of %qE as complex rather "
3387 "than integer due to prototype",
3388 argnum, rname);
3389 else if (TREE_CODE (type) == REAL_TYPE
3390 && TREE_CODE (valtype) == COMPLEX_TYPE)
3391 warning_at (ploc, OPT_Wtraditional_conversion,
3392 "passing argument %d of %qE as floating rather "
3393 "than complex due to prototype",
3394 argnum, rname);
3395 /* ??? At some point, messages should be written about
3396 conversions between complex types, but that's too messy
3397 to do now. */
3398 else if (TREE_CODE (type) == REAL_TYPE
3399 && TREE_CODE (valtype) == REAL_TYPE)
3401 /* Warn if any argument is passed as `float',
3402 since without a prototype it would be `double'. */
3403 if (formal_prec == TYPE_PRECISION (float_type_node)
3404 && type != dfloat32_type_node)
3405 warning_at (ploc, 0,
3406 "passing argument %d of %qE as %<float%> "
3407 "rather than %<double%> due to prototype",
3408 argnum, rname);
3410 /* Warn if mismatch between argument and prototype
3411 for decimal float types. Warn of conversions with
3412 binary float types and of precision narrowing due to
3413 prototype. */
3414 else if (type != valtype
3415 && (type == dfloat32_type_node
3416 || type == dfloat64_type_node
3417 || type == dfloat128_type_node
3418 || valtype == dfloat32_type_node
3419 || valtype == dfloat64_type_node
3420 || valtype == dfloat128_type_node)
3421 && (formal_prec
3422 <= TYPE_PRECISION (valtype)
3423 || (type == dfloat128_type_node
3424 && (valtype
3425 != dfloat64_type_node
3426 && (valtype
3427 != dfloat32_type_node)))
3428 || (type == dfloat64_type_node
3429 && (valtype
3430 != dfloat32_type_node))))
3431 warning_at (ploc, 0,
3432 "passing argument %d of %qE as %qT "
3433 "rather than %qT due to prototype",
3434 argnum, rname, type, valtype);
3437 /* Detect integer changing in width or signedness.
3438 These warnings are only activated with
3439 -Wtraditional-conversion, not with -Wtraditional. */
3440 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3441 && INTEGRAL_TYPE_P (valtype))
3443 tree would_have_been = default_conversion (val);
3444 tree type1 = TREE_TYPE (would_have_been);
3446 if (TREE_CODE (type) == ENUMERAL_TYPE
3447 && (TYPE_MAIN_VARIANT (type)
3448 == TYPE_MAIN_VARIANT (valtype)))
3449 /* No warning if function asks for enum
3450 and the actual arg is that enum type. */
3452 else if (formal_prec != TYPE_PRECISION (type1))
3453 warning_at (ploc, OPT_Wtraditional_conversion,
3454 "passing argument %d of %qE "
3455 "with different width due to prototype",
3456 argnum, rname);
3457 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3459 /* Don't complain if the formal parameter type
3460 is an enum, because we can't tell now whether
3461 the value was an enum--even the same enum. */
3462 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3464 else if (TREE_CODE (val) == INTEGER_CST
3465 && int_fits_type_p (val, type))
3466 /* Change in signedness doesn't matter
3467 if a constant value is unaffected. */
3469 /* If the value is extended from a narrower
3470 unsigned type, it doesn't matter whether we
3471 pass it as signed or unsigned; the value
3472 certainly is the same either way. */
3473 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3474 && TYPE_UNSIGNED (valtype))
3476 else if (TYPE_UNSIGNED (type))
3477 warning_at (ploc, OPT_Wtraditional_conversion,
3478 "passing argument %d of %qE "
3479 "as unsigned due to prototype",
3480 argnum, rname);
3481 else
3482 warning_at (ploc, OPT_Wtraditional_conversion,
3483 "passing argument %d of %qE "
3484 "as signed due to prototype",
3485 argnum, rname);
3489 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3490 sake of better warnings from convert_and_check. */
3491 if (excess_precision)
3492 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3493 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3494 parmval = convert_for_assignment (loc, ploc, type,
3495 val, origtype, ic_argpass,
3496 npc, fundecl, function,
3497 parmnum + 1);
3499 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3500 && INTEGRAL_TYPE_P (type)
3501 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3502 parmval = default_conversion (parmval);
3505 else if (promote_float_arg)
3507 if (type_generic)
3508 parmval = val;
3509 else
3511 /* Convert `float' to `double'. */
3512 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3513 warning_at (ploc, OPT_Wdouble_promotion,
3514 "implicit conversion from %qT to %qT when passing "
3515 "argument to function",
3516 valtype, double_type_node);
3517 parmval = convert (double_type_node, val);
3520 else if ((excess_precision && !type_generic)
3521 || (type_generic_overflow_p && parmnum == 2))
3522 /* A "double" argument with excess precision being passed
3523 without a prototype or in variable arguments.
3524 The last argument of __builtin_*_overflow_p should not be
3525 promoted. */
3526 parmval = convert (valtype, val);
3527 else if ((invalid_func_diag =
3528 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3530 error (invalid_func_diag);
3531 return -1;
3533 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3535 return -1;
3537 else
3538 /* Convert `short' and `char' to full-size `int'. */
3539 parmval = default_conversion (val);
3541 (*values)[parmnum] = parmval;
3542 if (parmval == error_mark_node)
3543 error_args = true;
3545 if (typetail)
3546 typetail = TREE_CHAIN (typetail);
3549 gcc_assert (parmnum == vec_safe_length (values));
3551 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3553 error_at (loc, "too few arguments to function %qE", function);
3554 inform_declaration (fundecl);
3555 return -1;
3558 return error_args ? -1 : (int) parmnum;
3561 /* This is the entry point used by the parser to build unary operators
3562 in the input. CODE, a tree_code, specifies the unary operator, and
3563 ARG is the operand. For unary plus, the C parser currently uses
3564 CONVERT_EXPR for code.
3566 LOC is the location to use for the tree generated.
3569 struct c_expr
3570 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3572 struct c_expr result;
3574 result.original_code = code;
3575 result.original_type = NULL;
3577 if (reject_gcc_builtin (arg.value))
3579 result.value = error_mark_node;
3581 else
3583 result.value = build_unary_op (loc, code, arg.value, false);
3585 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3586 overflow_warning (loc, result.value);
3589 /* We are typically called when parsing a prefix token at LOC acting on
3590 ARG. Reflect this by updating the source range of the result to
3591 start at LOC and end at the end of ARG. */
3592 set_c_expr_source_range (&result,
3593 loc, arg.get_finish ());
3595 return result;
3598 /* This is the entry point used by the parser to build binary operators
3599 in the input. CODE, a tree_code, specifies the binary operator, and
3600 ARG1 and ARG2 are the operands. In addition to constructing the
3601 expression, we check for operands that were written with other binary
3602 operators in a way that is likely to confuse the user.
3604 LOCATION is the location of the binary operator. */
3606 struct c_expr
3607 parser_build_binary_op (location_t location, enum tree_code code,
3608 struct c_expr arg1, struct c_expr arg2)
3610 struct c_expr result;
3612 enum tree_code code1 = arg1.original_code;
3613 enum tree_code code2 = arg2.original_code;
3614 tree type1 = (arg1.original_type
3615 ? arg1.original_type
3616 : TREE_TYPE (arg1.value));
3617 tree type2 = (arg2.original_type
3618 ? arg2.original_type
3619 : TREE_TYPE (arg2.value));
3621 result.value = build_binary_op (location, code,
3622 arg1.value, arg2.value, 1);
3623 result.original_code = code;
3624 result.original_type = NULL;
3626 if (TREE_CODE (result.value) == ERROR_MARK)
3628 set_c_expr_source_range (&result,
3629 arg1.get_start (),
3630 arg2.get_finish ());
3631 return result;
3634 if (location != UNKNOWN_LOCATION)
3635 protected_set_expr_location (result.value, location);
3637 set_c_expr_source_range (&result,
3638 arg1.get_start (),
3639 arg2.get_finish ());
3641 /* Check for cases such as x+y<<z which users are likely
3642 to misinterpret. */
3643 if (warn_parentheses)
3644 warn_about_parentheses (location, code, code1, arg1.value, code2,
3645 arg2.value);
3647 if (warn_logical_op)
3648 warn_logical_operator (location, code, TREE_TYPE (result.value),
3649 code1, arg1.value, code2, arg2.value);
3651 if (warn_tautological_compare)
3653 tree lhs = arg1.value;
3654 tree rhs = arg2.value;
3655 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3657 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3658 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3659 lhs = NULL_TREE;
3660 else
3661 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3663 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3665 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3666 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3667 rhs = NULL_TREE;
3668 else
3669 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3671 if (lhs != NULL_TREE && rhs != NULL_TREE)
3672 warn_tautological_cmp (location, code, lhs, rhs);
3675 if (warn_logical_not_paren
3676 && TREE_CODE_CLASS (code) == tcc_comparison
3677 && code1 == TRUTH_NOT_EXPR
3678 && code2 != TRUTH_NOT_EXPR
3679 /* Avoid warning for !!x == y. */
3680 && (TREE_CODE (arg1.value) != NE_EXPR
3681 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3683 /* Avoid warning for !b == y where b has _Bool type. */
3684 tree t = integer_zero_node;
3685 if (TREE_CODE (arg1.value) == EQ_EXPR
3686 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3687 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3689 t = TREE_OPERAND (arg1.value, 0);
3692 if (TREE_TYPE (t) != integer_type_node)
3693 break;
3694 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3695 t = C_MAYBE_CONST_EXPR_EXPR (t);
3696 else if (CONVERT_EXPR_P (t))
3697 t = TREE_OPERAND (t, 0);
3698 else
3699 break;
3701 while (1);
3703 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3704 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3707 /* Warn about comparisons against string literals, with the exception
3708 of testing for equality or inequality of a string literal with NULL. */
3709 if (code == EQ_EXPR || code == NE_EXPR)
3711 if ((code1 == STRING_CST
3712 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3713 || (code2 == STRING_CST
3714 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3715 warning_at (location, OPT_Waddress,
3716 "comparison with string literal results in unspecified behavior");
3718 else if (TREE_CODE_CLASS (code) == tcc_comparison
3719 && (code1 == STRING_CST || code2 == STRING_CST))
3720 warning_at (location, OPT_Waddress,
3721 "comparison with string literal results in unspecified behavior");
3723 if (TREE_OVERFLOW_P (result.value)
3724 && !TREE_OVERFLOW_P (arg1.value)
3725 && !TREE_OVERFLOW_P (arg2.value))
3726 overflow_warning (location, result.value);
3728 /* Warn about comparisons of different enum types. */
3729 if (warn_enum_compare
3730 && TREE_CODE_CLASS (code) == tcc_comparison
3731 && TREE_CODE (type1) == ENUMERAL_TYPE
3732 && TREE_CODE (type2) == ENUMERAL_TYPE
3733 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3734 warning_at (location, OPT_Wenum_compare,
3735 "comparison between %qT and %qT",
3736 type1, type2);
3738 return result;
3741 /* Return a tree for the difference of pointers OP0 and OP1.
3742 The resulting tree has type int. */
3744 static tree
3745 pointer_diff (location_t loc, tree op0, tree op1)
3747 tree restype = ptrdiff_type_node;
3748 tree result, inttype;
3750 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3751 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3752 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3753 tree orig_op1 = op1;
3755 /* If the operands point into different address spaces, we need to
3756 explicitly convert them to pointers into the common address space
3757 before we can subtract the numerical address values. */
3758 if (as0 != as1)
3760 addr_space_t as_common;
3761 tree common_type;
3763 /* Determine the common superset address space. This is guaranteed
3764 to exist because the caller verified that comp_target_types
3765 returned non-zero. */
3766 if (!addr_space_superset (as0, as1, &as_common))
3767 gcc_unreachable ();
3769 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3770 op0 = convert (common_type, op0);
3771 op1 = convert (common_type, op1);
3774 /* Determine integer type to perform computations in. This will usually
3775 be the same as the result type (ptrdiff_t), but may need to be a wider
3776 type if pointers for the address space are wider than ptrdiff_t. */
3777 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3778 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3779 else
3780 inttype = restype;
3782 if (TREE_CODE (target_type) == VOID_TYPE)
3783 pedwarn (loc, OPT_Wpointer_arith,
3784 "pointer of type %<void *%> used in subtraction");
3785 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3786 pedwarn (loc, OPT_Wpointer_arith,
3787 "pointer to a function used in subtraction");
3789 /* First do the subtraction as integers;
3790 then drop through to build the divide operator.
3791 Do not do default conversions on the minus operator
3792 in case restype is a short type. */
3794 op0 = build_binary_op (loc,
3795 MINUS_EXPR, convert (inttype, op0),
3796 convert (inttype, op1), 0);
3797 /* This generates an error if op1 is pointer to incomplete type. */
3798 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3799 error_at (loc, "arithmetic on pointer to an incomplete type");
3801 op1 = c_size_in_bytes (target_type);
3803 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3804 error_at (loc, "arithmetic on pointer to an empty aggregate");
3806 /* Divide by the size, in easiest possible way. */
3807 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3808 op0, convert (inttype, op1));
3810 /* Convert to final result type if necessary. */
3811 return convert (restype, result);
3814 /* Expand atomic compound assignments into an appropriate sequence as
3815 specified by the C11 standard section 6.5.16.2.
3817 _Atomic T1 E1
3818 T2 E2
3819 E1 op= E2
3821 This sequence is used for all types for which these operations are
3822 supported.
3824 In addition, built-in versions of the 'fe' prefixed routines may
3825 need to be invoked for floating point (real, complex or vector) when
3826 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3828 T1 newval;
3829 T1 old;
3830 T1 *addr
3831 T2 val
3832 fenv_t fenv
3834 addr = &E1;
3835 val = (E2);
3836 __atomic_load (addr, &old, SEQ_CST);
3837 feholdexcept (&fenv);
3838 loop:
3839 newval = old op val;
3840 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3841 SEQ_CST))
3842 goto done;
3843 feclearexcept (FE_ALL_EXCEPT);
3844 goto loop:
3845 done:
3846 feupdateenv (&fenv);
3848 The compiler will issue the __atomic_fetch_* built-in when possible,
3849 otherwise it will generate the generic form of the atomic operations.
3850 This requires temp(s) and has their address taken. The atomic processing
3851 is smart enough to figure out when the size of an object can utilize
3852 a lock-free version, and convert the built-in call to the appropriate
3853 lock-free routine. The optimizers will then dispose of any temps that
3854 are no longer required, and lock-free implementations are utilized as
3855 long as there is target support for the required size.
3857 If the operator is NOP_EXPR, then this is a simple assignment, and
3858 an __atomic_store is issued to perform the assignment rather than
3859 the above loop. */
3861 /* Build an atomic assignment at LOC, expanding into the proper
3862 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3863 the result of the operation, unless RETURN_OLD_P, in which case
3864 return the old value of LHS (this is only for postincrement and
3865 postdecrement). */
3867 static tree
3868 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3869 tree rhs, bool return_old_p)
3871 tree fndecl, func_call;
3872 vec<tree, va_gc> *params;
3873 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3874 tree old, old_addr;
3875 tree compound_stmt;
3876 tree stmt, goto_stmt;
3877 tree loop_label, loop_decl, done_label, done_decl;
3879 tree lhs_type = TREE_TYPE (lhs);
3880 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3881 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3882 tree rhs_type = TREE_TYPE (rhs);
3884 gcc_assert (TYPE_ATOMIC (lhs_type));
3886 if (return_old_p)
3887 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3889 /* Allocate enough vector items for a compare_exchange. */
3890 vec_alloc (params, 6);
3892 /* Create a compound statement to hold the sequence of statements
3893 with a loop. */
3894 compound_stmt = c_begin_compound_stmt (false);
3896 /* Fold the RHS if it hasn't already been folded. */
3897 if (modifycode != NOP_EXPR)
3898 rhs = c_fully_fold (rhs, false, NULL);
3900 /* Remove the qualifiers for the rest of the expressions and create
3901 the VAL temp variable to hold the RHS. */
3902 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3903 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3904 val = create_tmp_var_raw (nonatomic_rhs_type);
3905 TREE_ADDRESSABLE (val) = 1;
3906 TREE_NO_WARNING (val) = 1;
3907 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3908 NULL_TREE);
3909 SET_EXPR_LOCATION (rhs, loc);
3910 add_stmt (rhs);
3912 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3913 an atomic_store. */
3914 if (modifycode == NOP_EXPR)
3916 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3917 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3918 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3919 params->quick_push (lhs_addr);
3920 params->quick_push (rhs);
3921 params->quick_push (seq_cst);
3922 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3923 add_stmt (func_call);
3925 /* Finish the compound statement. */
3926 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3928 /* VAL is the value which was stored, return a COMPOUND_STMT of
3929 the statement and that value. */
3930 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3933 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3934 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3935 isn't applicable for such builtins. ??? Do we want to handle enums? */
3936 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3937 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3939 built_in_function fncode;
3940 switch (modifycode)
3942 case PLUS_EXPR:
3943 case POINTER_PLUS_EXPR:
3944 fncode = (return_old_p
3945 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3946 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3947 break;
3948 case MINUS_EXPR:
3949 fncode = (return_old_p
3950 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3951 : BUILT_IN_ATOMIC_SUB_FETCH_N);
3952 break;
3953 case BIT_AND_EXPR:
3954 fncode = (return_old_p
3955 ? BUILT_IN_ATOMIC_FETCH_AND_N
3956 : BUILT_IN_ATOMIC_AND_FETCH_N);
3957 break;
3958 case BIT_IOR_EXPR:
3959 fncode = (return_old_p
3960 ? BUILT_IN_ATOMIC_FETCH_OR_N
3961 : BUILT_IN_ATOMIC_OR_FETCH_N);
3962 break;
3963 case BIT_XOR_EXPR:
3964 fncode = (return_old_p
3965 ? BUILT_IN_ATOMIC_FETCH_XOR_N
3966 : BUILT_IN_ATOMIC_XOR_FETCH_N);
3967 break;
3968 default:
3969 goto cas_loop;
3972 /* We can only use "_1" through "_16" variants of the atomic fetch
3973 built-ins. */
3974 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
3975 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
3976 goto cas_loop;
3978 /* If this is a pointer type, we need to multiply by the size of
3979 the pointer target type. */
3980 if (POINTER_TYPE_P (lhs_type))
3982 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
3983 /* ??? This would introduce -Wdiscarded-qualifiers
3984 warning: __atomic_fetch_* expect volatile void *
3985 type as the first argument. (Assignments between
3986 atomic and non-atomic objects are OK.) */
3987 || TYPE_RESTRICT (lhs_type))
3988 goto cas_loop;
3989 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
3990 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
3991 convert (ptrdiff_type_node, rhs),
3992 convert (ptrdiff_type_node, sz));
3995 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
3996 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
3997 fndecl = builtin_decl_explicit (fncode);
3998 params->quick_push (lhs_addr);
3999 params->quick_push (rhs);
4000 params->quick_push (seq_cst);
4001 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4003 newval = create_tmp_var_raw (nonatomic_lhs_type);
4004 TREE_ADDRESSABLE (newval) = 1;
4005 TREE_NO_WARNING (newval) = 1;
4006 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4007 NULL_TREE, NULL_TREE);
4008 SET_EXPR_LOCATION (rhs, loc);
4009 add_stmt (rhs);
4011 /* Finish the compound statement. */
4012 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4014 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4015 the statement and that value. */
4016 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4019 cas_loop:
4020 /* Create the variables and labels required for the op= form. */
4021 old = create_tmp_var_raw (nonatomic_lhs_type);
4022 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4023 TREE_ADDRESSABLE (old) = 1;
4024 TREE_NO_WARNING (old) = 1;
4026 newval = create_tmp_var_raw (nonatomic_lhs_type);
4027 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4028 TREE_ADDRESSABLE (newval) = 1;
4029 TREE_NO_WARNING (newval) = 1;
4031 loop_decl = create_artificial_label (loc);
4032 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4034 done_decl = create_artificial_label (loc);
4035 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4037 /* __atomic_load (addr, &old, SEQ_CST). */
4038 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4039 params->quick_push (lhs_addr);
4040 params->quick_push (old_addr);
4041 params->quick_push (seq_cst);
4042 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4043 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4044 NULL_TREE);
4045 add_stmt (old);
4046 params->truncate (0);
4048 /* Create the expressions for floating-point environment
4049 manipulation, if required. */
4050 bool need_fenv = (flag_trapping_math
4051 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4052 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4053 if (need_fenv)
4054 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4056 if (hold_call)
4057 add_stmt (hold_call);
4059 /* loop: */
4060 add_stmt (loop_label);
4062 /* newval = old + val; */
4063 rhs = build_binary_op (loc, modifycode, old, val, 1);
4064 rhs = c_fully_fold (rhs, false, NULL);
4065 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4066 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4067 NULL_TREE, 0);
4068 if (rhs != error_mark_node)
4070 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4071 NULL_TREE);
4072 SET_EXPR_LOCATION (rhs, loc);
4073 add_stmt (rhs);
4076 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4077 goto done; */
4078 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4079 params->quick_push (lhs_addr);
4080 params->quick_push (old_addr);
4081 params->quick_push (newval_addr);
4082 params->quick_push (integer_zero_node);
4083 params->quick_push (seq_cst);
4084 params->quick_push (seq_cst);
4085 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4087 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4088 SET_EXPR_LOCATION (goto_stmt, loc);
4090 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4091 SET_EXPR_LOCATION (stmt, loc);
4092 add_stmt (stmt);
4094 if (clear_call)
4095 add_stmt (clear_call);
4097 /* goto loop; */
4098 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4099 SET_EXPR_LOCATION (goto_stmt, loc);
4100 add_stmt (goto_stmt);
4102 /* done: */
4103 add_stmt (done_label);
4105 if (update_call)
4106 add_stmt (update_call);
4108 /* Finish the compound statement. */
4109 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4111 /* NEWVAL is the value that was successfully stored, return a
4112 COMPOUND_EXPR of the statement and the appropriate value. */
4113 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4114 return_old_p ? old : newval);
4117 /* Construct and perhaps optimize a tree representation
4118 for a unary operation. CODE, a tree_code, specifies the operation
4119 and XARG is the operand.
4120 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4121 promotions (such as from short to int).
4122 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4123 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4124 to pointers in C99.
4126 LOCATION is the location of the operator. */
4128 tree
4129 build_unary_op (location_t location, enum tree_code code, tree xarg,
4130 bool noconvert)
4132 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4133 tree arg = xarg;
4134 tree argtype = 0;
4135 enum tree_code typecode;
4136 tree val;
4137 tree ret = error_mark_node;
4138 tree eptype = NULL_TREE;
4139 const char *invalid_op_diag;
4140 bool int_operands;
4142 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4143 if (int_operands)
4144 arg = remove_c_maybe_const_expr (arg);
4146 if (code != ADDR_EXPR)
4147 arg = require_complete_type (location, arg);
4149 typecode = TREE_CODE (TREE_TYPE (arg));
4150 if (typecode == ERROR_MARK)
4151 return error_mark_node;
4152 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4153 typecode = INTEGER_TYPE;
4155 if ((invalid_op_diag
4156 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4158 error_at (location, invalid_op_diag);
4159 return error_mark_node;
4162 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4164 eptype = TREE_TYPE (arg);
4165 arg = TREE_OPERAND (arg, 0);
4168 switch (code)
4170 case CONVERT_EXPR:
4171 /* This is used for unary plus, because a CONVERT_EXPR
4172 is enough to prevent anybody from looking inside for
4173 associativity, but won't generate any code. */
4174 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4175 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4176 || typecode == VECTOR_TYPE))
4178 error_at (location, "wrong type argument to unary plus");
4179 return error_mark_node;
4181 else if (!noconvert)
4182 arg = default_conversion (arg);
4183 arg = non_lvalue_loc (location, arg);
4184 break;
4186 case NEGATE_EXPR:
4187 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4188 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4189 || typecode == VECTOR_TYPE))
4191 error_at (location, "wrong type argument to unary minus");
4192 return error_mark_node;
4194 else if (!noconvert)
4195 arg = default_conversion (arg);
4196 break;
4198 case BIT_NOT_EXPR:
4199 /* ~ works on integer types and non float vectors. */
4200 if (typecode == INTEGER_TYPE
4201 || (typecode == VECTOR_TYPE
4202 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4204 tree e = arg;
4206 /* Warn if the expression has boolean value. */
4207 while (TREE_CODE (e) == COMPOUND_EXPR)
4208 e = TREE_OPERAND (e, 1);
4210 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4211 || truth_value_p (TREE_CODE (e)))
4212 && warning_at (location, OPT_Wbool_operation,
4213 "%<~%> on a boolean expression"))
4215 gcc_rich_location richloc (location);
4216 richloc.add_fixit_insert_before (location, "!");
4217 inform_at_rich_loc (&richloc, "did you mean to use logical "
4218 "not?");
4220 if (!noconvert)
4221 arg = default_conversion (arg);
4223 else if (typecode == COMPLEX_TYPE)
4225 code = CONJ_EXPR;
4226 pedwarn (location, OPT_Wpedantic,
4227 "ISO C does not support %<~%> for complex conjugation");
4228 if (!noconvert)
4229 arg = default_conversion (arg);
4231 else
4233 error_at (location, "wrong type argument to bit-complement");
4234 return error_mark_node;
4236 break;
4238 case ABS_EXPR:
4239 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4241 error_at (location, "wrong type argument to abs");
4242 return error_mark_node;
4244 else if (!noconvert)
4245 arg = default_conversion (arg);
4246 break;
4248 case CONJ_EXPR:
4249 /* Conjugating a real value is a no-op, but allow it anyway. */
4250 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4251 || typecode == COMPLEX_TYPE))
4253 error_at (location, "wrong type argument to conjugation");
4254 return error_mark_node;
4256 else if (!noconvert)
4257 arg = default_conversion (arg);
4258 break;
4260 case TRUTH_NOT_EXPR:
4261 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4262 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4263 && typecode != COMPLEX_TYPE)
4265 error_at (location,
4266 "wrong type argument to unary exclamation mark");
4267 return error_mark_node;
4269 if (int_operands)
4271 arg = c_objc_common_truthvalue_conversion (location, xarg);
4272 arg = remove_c_maybe_const_expr (arg);
4274 else
4275 arg = c_objc_common_truthvalue_conversion (location, arg);
4276 ret = invert_truthvalue_loc (location, arg);
4277 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4278 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4279 location = EXPR_LOCATION (ret);
4280 goto return_build_unary_op;
4282 case REALPART_EXPR:
4283 case IMAGPART_EXPR:
4284 ret = build_real_imag_expr (location, code, arg);
4285 if (ret == error_mark_node)
4286 return error_mark_node;
4287 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4288 eptype = TREE_TYPE (eptype);
4289 goto return_build_unary_op;
4291 case PREINCREMENT_EXPR:
4292 case POSTINCREMENT_EXPR:
4293 case PREDECREMENT_EXPR:
4294 case POSTDECREMENT_EXPR:
4296 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4298 tree inner = build_unary_op (location, code,
4299 C_MAYBE_CONST_EXPR_EXPR (arg),
4300 noconvert);
4301 if (inner == error_mark_node)
4302 return error_mark_node;
4303 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4304 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4305 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4306 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4307 goto return_build_unary_op;
4310 /* Complain about anything that is not a true lvalue. In
4311 Objective-C, skip this check for property_refs. */
4312 if (!objc_is_property_ref (arg)
4313 && !lvalue_or_else (location,
4314 arg, ((code == PREINCREMENT_EXPR
4315 || code == POSTINCREMENT_EXPR)
4316 ? lv_increment
4317 : lv_decrement)))
4318 return error_mark_node;
4320 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4322 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4323 warning_at (location, OPT_Wc___compat,
4324 "increment of enumeration value is invalid in C++");
4325 else
4326 warning_at (location, OPT_Wc___compat,
4327 "decrement of enumeration value is invalid in C++");
4330 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4332 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4333 warning_at (location, OPT_Wbool_operation,
4334 "increment of a boolean expression");
4335 else
4336 warning_at (location, OPT_Wbool_operation,
4337 "decrement of a boolean expression");
4340 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4341 arg = c_fully_fold (arg, false, NULL);
4343 bool atomic_op;
4344 atomic_op = really_atomic_lvalue (arg);
4346 /* Increment or decrement the real part of the value,
4347 and don't change the imaginary part. */
4348 if (typecode == COMPLEX_TYPE)
4350 tree real, imag;
4352 pedwarn (location, OPT_Wpedantic,
4353 "ISO C does not support %<++%> and %<--%> on complex types");
4355 if (!atomic_op)
4357 arg = stabilize_reference (arg);
4358 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4359 true);
4360 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4361 true);
4362 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4363 if (real == error_mark_node || imag == error_mark_node)
4364 return error_mark_node;
4365 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4366 real, imag);
4367 goto return_build_unary_op;
4371 /* Report invalid types. */
4373 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4374 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4375 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4377 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4378 error_at (location, "wrong type argument to increment");
4379 else
4380 error_at (location, "wrong type argument to decrement");
4382 return error_mark_node;
4386 tree inc;
4388 argtype = TREE_TYPE (arg);
4390 /* Compute the increment. */
4392 if (typecode == POINTER_TYPE)
4394 /* If pointer target is an incomplete type,
4395 we just cannot know how to do the arithmetic. */
4396 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4398 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4399 error_at (location,
4400 "increment of pointer to an incomplete type %qT",
4401 TREE_TYPE (argtype));
4402 else
4403 error_at (location,
4404 "decrement of pointer to an incomplete type %qT",
4405 TREE_TYPE (argtype));
4407 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4408 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4410 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4411 pedwarn (location, OPT_Wpointer_arith,
4412 "wrong type argument to increment");
4413 else
4414 pedwarn (location, OPT_Wpointer_arith,
4415 "wrong type argument to decrement");
4418 inc = c_size_in_bytes (TREE_TYPE (argtype));
4419 inc = convert_to_ptrofftype_loc (location, inc);
4421 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4423 /* For signed fract types, we invert ++ to -- or
4424 -- to ++, and change inc from 1 to -1, because
4425 it is not possible to represent 1 in signed fract constants.
4426 For unsigned fract types, the result always overflows and
4427 we get an undefined (original) or the maximum value. */
4428 if (code == PREINCREMENT_EXPR)
4429 code = PREDECREMENT_EXPR;
4430 else if (code == PREDECREMENT_EXPR)
4431 code = PREINCREMENT_EXPR;
4432 else if (code == POSTINCREMENT_EXPR)
4433 code = POSTDECREMENT_EXPR;
4434 else /* code == POSTDECREMENT_EXPR */
4435 code = POSTINCREMENT_EXPR;
4437 inc = integer_minus_one_node;
4438 inc = convert (argtype, inc);
4440 else
4442 inc = VECTOR_TYPE_P (argtype)
4443 ? build_one_cst (argtype)
4444 : integer_one_node;
4445 inc = convert (argtype, inc);
4448 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4449 need to ask Objective-C to build the increment or decrement
4450 expression for it. */
4451 if (objc_is_property_ref (arg))
4452 return objc_build_incr_expr_for_property_ref (location, code,
4453 arg, inc);
4455 /* Report a read-only lvalue. */
4456 if (TYPE_READONLY (argtype))
4458 readonly_error (location, arg,
4459 ((code == PREINCREMENT_EXPR
4460 || code == POSTINCREMENT_EXPR)
4461 ? lv_increment : lv_decrement));
4462 return error_mark_node;
4464 else if (TREE_READONLY (arg))
4465 readonly_warning (arg,
4466 ((code == PREINCREMENT_EXPR
4467 || code == POSTINCREMENT_EXPR)
4468 ? lv_increment : lv_decrement));
4470 /* If the argument is atomic, use the special code sequences for
4471 atomic compound assignment. */
4472 if (atomic_op)
4474 arg = stabilize_reference (arg);
4475 ret = build_atomic_assign (location, arg,
4476 ((code == PREINCREMENT_EXPR
4477 || code == POSTINCREMENT_EXPR)
4478 ? PLUS_EXPR
4479 : MINUS_EXPR),
4480 (FRACT_MODE_P (TYPE_MODE (argtype))
4481 ? inc
4482 : integer_one_node),
4483 (code == POSTINCREMENT_EXPR
4484 || code == POSTDECREMENT_EXPR));
4485 goto return_build_unary_op;
4488 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4489 val = boolean_increment (code, arg);
4490 else
4491 val = build2 (code, TREE_TYPE (arg), arg, inc);
4492 TREE_SIDE_EFFECTS (val) = 1;
4493 if (TREE_CODE (val) != code)
4494 TREE_NO_WARNING (val) = 1;
4495 ret = val;
4496 goto return_build_unary_op;
4499 case ADDR_EXPR:
4500 /* Note that this operation never does default_conversion. */
4502 /* The operand of unary '&' must be an lvalue (which excludes
4503 expressions of type void), or, in C99, the result of a [] or
4504 unary '*' operator. */
4505 if (VOID_TYPE_P (TREE_TYPE (arg))
4506 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4507 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4508 pedwarn (location, 0, "taking address of expression of type %<void%>");
4510 /* Let &* cancel out to simplify resulting code. */
4511 if (INDIRECT_REF_P (arg))
4513 /* Don't let this be an lvalue. */
4514 if (lvalue_p (TREE_OPERAND (arg, 0)))
4515 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4516 ret = TREE_OPERAND (arg, 0);
4517 goto return_build_unary_op;
4520 /* Anything not already handled and not a true memory reference
4521 or a non-lvalue array is an error. */
4522 if (typecode != FUNCTION_TYPE && !noconvert
4523 && !lvalue_or_else (location, arg, lv_addressof))
4524 return error_mark_node;
4526 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4527 folding later. */
4528 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4530 tree inner = build_unary_op (location, code,
4531 C_MAYBE_CONST_EXPR_EXPR (arg),
4532 noconvert);
4533 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4534 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4535 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4536 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4537 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4538 goto return_build_unary_op;
4541 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4542 argtype = TREE_TYPE (arg);
4544 /* If the lvalue is const or volatile, merge that into the type
4545 to which the address will point. This is only needed
4546 for function types. */
4547 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4548 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4549 && TREE_CODE (argtype) == FUNCTION_TYPE)
4551 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4552 int quals = orig_quals;
4554 if (TREE_READONLY (arg))
4555 quals |= TYPE_QUAL_CONST;
4556 if (TREE_THIS_VOLATILE (arg))
4557 quals |= TYPE_QUAL_VOLATILE;
4559 argtype = c_build_qualified_type (argtype, quals);
4562 switch (TREE_CODE (arg))
4564 case COMPONENT_REF:
4565 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4567 error_at (location, "cannot take address of bit-field %qD",
4568 TREE_OPERAND (arg, 1));
4569 return error_mark_node;
4572 /* fall through */
4574 case ARRAY_REF:
4575 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4577 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4578 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4580 error_at (location, "cannot take address of scalar with "
4581 "reverse storage order");
4582 return error_mark_node;
4585 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4586 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4587 warning_at (location, OPT_Wscalar_storage_order,
4588 "address of array with reverse scalar storage "
4589 "order requested");
4592 default:
4593 break;
4596 if (!c_mark_addressable (arg))
4597 return error_mark_node;
4599 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4600 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4602 argtype = build_pointer_type (argtype);
4604 /* ??? Cope with user tricks that amount to offsetof. Delete this
4605 when we have proper support for integer constant expressions. */
4606 val = get_base_address (arg);
4607 if (val && INDIRECT_REF_P (val)
4608 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4610 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4611 goto return_build_unary_op;
4614 val = build1 (ADDR_EXPR, argtype, arg);
4616 ret = val;
4617 goto return_build_unary_op;
4619 default:
4620 gcc_unreachable ();
4623 if (argtype == 0)
4624 argtype = TREE_TYPE (arg);
4625 if (TREE_CODE (arg) == INTEGER_CST)
4626 ret = (require_constant_value
4627 ? fold_build1_initializer_loc (location, code, argtype, arg)
4628 : fold_build1_loc (location, code, argtype, arg));
4629 else
4630 ret = build1 (code, argtype, arg);
4631 return_build_unary_op:
4632 gcc_assert (ret != error_mark_node);
4633 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4634 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4635 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4636 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4637 ret = note_integer_operands (ret);
4638 if (eptype)
4639 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4640 protected_set_expr_location (ret, location);
4641 return ret;
4644 /* Return nonzero if REF is an lvalue valid for this language.
4645 Lvalues can be assigned, unless their type has TYPE_READONLY.
4646 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4648 bool
4649 lvalue_p (const_tree ref)
4651 const enum tree_code code = TREE_CODE (ref);
4653 switch (code)
4655 case REALPART_EXPR:
4656 case IMAGPART_EXPR:
4657 case COMPONENT_REF:
4658 return lvalue_p (TREE_OPERAND (ref, 0));
4660 case C_MAYBE_CONST_EXPR:
4661 return lvalue_p (TREE_OPERAND (ref, 1));
4663 case COMPOUND_LITERAL_EXPR:
4664 case STRING_CST:
4665 return true;
4667 case INDIRECT_REF:
4668 case ARRAY_REF:
4669 case ARRAY_NOTATION_REF:
4670 case VAR_DECL:
4671 case PARM_DECL:
4672 case RESULT_DECL:
4673 case ERROR_MARK:
4674 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4675 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4677 case BIND_EXPR:
4678 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4680 default:
4681 return false;
4685 /* Give a warning for storing in something that is read-only in GCC
4686 terms but not const in ISO C terms. */
4688 static void
4689 readonly_warning (tree arg, enum lvalue_use use)
4691 switch (use)
4693 case lv_assign:
4694 warning (0, "assignment of read-only location %qE", arg);
4695 break;
4696 case lv_increment:
4697 warning (0, "increment of read-only location %qE", arg);
4698 break;
4699 case lv_decrement:
4700 warning (0, "decrement of read-only location %qE", arg);
4701 break;
4702 default:
4703 gcc_unreachable ();
4705 return;
4709 /* Return nonzero if REF is an lvalue valid for this language;
4710 otherwise, print an error message and return zero. USE says
4711 how the lvalue is being used and so selects the error message.
4712 LOCATION is the location at which any error should be reported. */
4714 static int
4715 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4717 int win = lvalue_p (ref);
4719 if (!win)
4720 lvalue_error (loc, use);
4722 return win;
4725 /* Mark EXP saying that we need to be able to take the
4726 address of it; it should not be allocated in a register.
4727 Returns true if successful. */
4729 bool
4730 c_mark_addressable (tree exp)
4732 tree x = exp;
4734 while (1)
4735 switch (TREE_CODE (x))
4737 case COMPONENT_REF:
4738 case ADDR_EXPR:
4739 case ARRAY_REF:
4740 case REALPART_EXPR:
4741 case IMAGPART_EXPR:
4742 x = TREE_OPERAND (x, 0);
4743 break;
4745 case COMPOUND_LITERAL_EXPR:
4746 case CONSTRUCTOR:
4747 TREE_ADDRESSABLE (x) = 1;
4748 return true;
4750 case VAR_DECL:
4751 case CONST_DECL:
4752 case PARM_DECL:
4753 case RESULT_DECL:
4754 if (C_DECL_REGISTER (x)
4755 && DECL_NONLOCAL (x))
4757 if (TREE_PUBLIC (x) || is_global_var (x))
4759 error
4760 ("global register variable %qD used in nested function", x);
4761 return false;
4763 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4765 else if (C_DECL_REGISTER (x))
4767 if (TREE_PUBLIC (x) || is_global_var (x))
4768 error ("address of global register variable %qD requested", x);
4769 else
4770 error ("address of register variable %qD requested", x);
4771 return false;
4774 /* FALLTHRU */
4775 case FUNCTION_DECL:
4776 TREE_ADDRESSABLE (x) = 1;
4777 /* FALLTHRU */
4778 default:
4779 return true;
4783 /* Convert EXPR to TYPE, warning about conversion problems with
4784 constants. SEMANTIC_TYPE is the type this conversion would use
4785 without excess precision. If SEMANTIC_TYPE is NULL, this function
4786 is equivalent to convert_and_check. This function is a wrapper that
4787 handles conversions that may be different than
4788 the usual ones because of excess precision. */
4790 static tree
4791 ep_convert_and_check (location_t loc, tree type, tree expr,
4792 tree semantic_type)
4794 if (TREE_TYPE (expr) == type)
4795 return expr;
4797 if (!semantic_type)
4798 return convert_and_check (loc, type, expr);
4800 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4801 && TREE_TYPE (expr) != semantic_type)
4803 /* For integers, we need to check the real conversion, not
4804 the conversion to the excess precision type. */
4805 expr = convert_and_check (loc, semantic_type, expr);
4807 /* Result type is the excess precision type, which should be
4808 large enough, so do not check. */
4809 return convert (type, expr);
4812 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4813 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4814 if folded to an integer constant then the unselected half may
4815 contain arbitrary operations not normally permitted in constant
4816 expressions. Set the location of the expression to LOC. */
4818 tree
4819 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4820 tree op1, tree op1_original_type, tree op2,
4821 tree op2_original_type)
4823 tree type1;
4824 tree type2;
4825 enum tree_code code1;
4826 enum tree_code code2;
4827 tree result_type = NULL;
4828 tree semantic_result_type = NULL;
4829 tree orig_op1 = op1, orig_op2 = op2;
4830 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4831 bool ifexp_int_operands;
4832 tree ret;
4834 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4835 if (op1_int_operands)
4836 op1 = remove_c_maybe_const_expr (op1);
4837 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4838 if (op2_int_operands)
4839 op2 = remove_c_maybe_const_expr (op2);
4840 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4841 if (ifexp_int_operands)
4842 ifexp = remove_c_maybe_const_expr (ifexp);
4844 /* Promote both alternatives. */
4846 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4847 op1 = default_conversion (op1);
4848 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4849 op2 = default_conversion (op2);
4851 if (TREE_CODE (ifexp) == ERROR_MARK
4852 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4853 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4854 return error_mark_node;
4856 type1 = TREE_TYPE (op1);
4857 code1 = TREE_CODE (type1);
4858 type2 = TREE_TYPE (op2);
4859 code2 = TREE_CODE (type2);
4861 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4862 return error_mark_node;
4864 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4865 return error_mark_node;
4867 /* C90 does not permit non-lvalue arrays in conditional expressions.
4868 In C99 they will be pointers by now. */
4869 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4871 error_at (colon_loc, "non-lvalue array in conditional expression");
4872 return error_mark_node;
4875 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4876 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4877 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4878 || code1 == COMPLEX_TYPE)
4879 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4880 || code2 == COMPLEX_TYPE))
4882 semantic_result_type = c_common_type (type1, type2);
4883 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4885 op1 = TREE_OPERAND (op1, 0);
4886 type1 = TREE_TYPE (op1);
4887 gcc_assert (TREE_CODE (type1) == code1);
4889 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4891 op2 = TREE_OPERAND (op2, 0);
4892 type2 = TREE_TYPE (op2);
4893 gcc_assert (TREE_CODE (type2) == code2);
4897 if (warn_cxx_compat)
4899 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4900 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4902 if (TREE_CODE (t1) == ENUMERAL_TYPE
4903 && TREE_CODE (t2) == ENUMERAL_TYPE
4904 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4905 warning_at (colon_loc, OPT_Wc___compat,
4906 ("different enum types in conditional is "
4907 "invalid in C++: %qT vs %qT"),
4908 t1, t2);
4911 /* Quickly detect the usual case where op1 and op2 have the same type
4912 after promotion. */
4913 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4915 if (type1 == type2)
4916 result_type = type1;
4917 else
4918 result_type = TYPE_MAIN_VARIANT (type1);
4920 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4921 || code1 == COMPLEX_TYPE)
4922 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4923 || code2 == COMPLEX_TYPE))
4925 result_type = c_common_type (type1, type2);
4926 if (result_type == error_mark_node)
4927 return error_mark_node;
4928 do_warn_double_promotion (result_type, type1, type2,
4929 "implicit conversion from %qT to %qT to "
4930 "match other result of conditional",
4931 colon_loc);
4933 /* If -Wsign-compare, warn here if type1 and type2 have
4934 different signedness. We'll promote the signed to unsigned
4935 and later code won't know it used to be different.
4936 Do this check on the original types, so that explicit casts
4937 will be considered, but default promotions won't. */
4938 if (c_inhibit_evaluation_warnings == 0)
4940 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4941 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4943 if (unsigned_op1 ^ unsigned_op2)
4945 bool ovf;
4947 /* Do not warn if the result type is signed, since the
4948 signed type will only be chosen if it can represent
4949 all the values of the unsigned type. */
4950 if (!TYPE_UNSIGNED (result_type))
4951 /* OK */;
4952 else
4954 bool op1_maybe_const = true;
4955 bool op2_maybe_const = true;
4957 /* Do not warn if the signed quantity is an
4958 unsuffixed integer literal (or some static
4959 constant expression involving such literals) and
4960 it is non-negative. This warning requires the
4961 operands to be folded for best results, so do
4962 that folding in this case even without
4963 warn_sign_compare to avoid warning options
4964 possibly affecting code generation. */
4965 c_inhibit_evaluation_warnings
4966 += (ifexp == truthvalue_false_node);
4967 op1 = c_fully_fold (op1, require_constant_value,
4968 &op1_maybe_const);
4969 c_inhibit_evaluation_warnings
4970 -= (ifexp == truthvalue_false_node);
4972 c_inhibit_evaluation_warnings
4973 += (ifexp == truthvalue_true_node);
4974 op2 = c_fully_fold (op2, require_constant_value,
4975 &op2_maybe_const);
4976 c_inhibit_evaluation_warnings
4977 -= (ifexp == truthvalue_true_node);
4979 if (warn_sign_compare)
4981 if ((unsigned_op2
4982 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4983 || (unsigned_op1
4984 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4985 /* OK */;
4986 else
4987 warning_at (colon_loc, OPT_Wsign_compare,
4988 ("signed and unsigned type in "
4989 "conditional expression"));
4991 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4992 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4993 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4994 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4999 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5001 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5002 pedwarn (colon_loc, OPT_Wpedantic,
5003 "ISO C forbids conditional expr with only one void side");
5004 result_type = void_type_node;
5006 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5008 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5009 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5010 addr_space_t as_common;
5012 if (comp_target_types (colon_loc, type1, type2))
5013 result_type = common_pointer_type (type1, type2);
5014 else if (null_pointer_constant_p (orig_op1))
5015 result_type = type2;
5016 else if (null_pointer_constant_p (orig_op2))
5017 result_type = type1;
5018 else if (!addr_space_superset (as1, as2, &as_common))
5020 error_at (colon_loc, "pointers to disjoint address spaces "
5021 "used in conditional expression");
5022 return error_mark_node;
5024 else if (VOID_TYPE_P (TREE_TYPE (type1))
5025 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5027 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5028 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5029 & ~TYPE_QUALS (TREE_TYPE (type1))))
5030 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5031 "pointer to array loses qualifier "
5032 "in conditional expression");
5034 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5035 pedwarn (colon_loc, OPT_Wpedantic,
5036 "ISO C forbids conditional expr between "
5037 "%<void *%> and function pointer");
5038 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5039 TREE_TYPE (type2)));
5041 else if (VOID_TYPE_P (TREE_TYPE (type2))
5042 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5044 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5045 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5046 & ~TYPE_QUALS (TREE_TYPE (type2))))
5047 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5048 "pointer to array loses qualifier "
5049 "in conditional expression");
5051 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5052 pedwarn (colon_loc, OPT_Wpedantic,
5053 "ISO C forbids conditional expr between "
5054 "%<void *%> and function pointer");
5055 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5056 TREE_TYPE (type1)));
5058 /* Objective-C pointer comparisons are a bit more lenient. */
5059 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5060 result_type = objc_common_type (type1, type2);
5061 else
5063 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5065 pedwarn (colon_loc, 0,
5066 "pointer type mismatch in conditional expression");
5067 result_type = build_pointer_type
5068 (build_qualified_type (void_type_node, qual));
5071 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5073 if (!null_pointer_constant_p (orig_op2))
5074 pedwarn (colon_loc, 0,
5075 "pointer/integer type mismatch in conditional expression");
5076 else
5078 op2 = null_pointer_node;
5080 result_type = type1;
5082 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5084 if (!null_pointer_constant_p (orig_op1))
5085 pedwarn (colon_loc, 0,
5086 "pointer/integer type mismatch in conditional expression");
5087 else
5089 op1 = null_pointer_node;
5091 result_type = type2;
5094 if (!result_type)
5096 if (flag_cond_mismatch)
5097 result_type = void_type_node;
5098 else
5100 error_at (colon_loc, "type mismatch in conditional expression");
5101 return error_mark_node;
5105 /* Merge const and volatile flags of the incoming types. */
5106 result_type
5107 = build_type_variant (result_type,
5108 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5109 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5111 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5112 semantic_result_type);
5113 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5114 semantic_result_type);
5116 if (ifexp_bcp && ifexp == truthvalue_true_node)
5118 op2_int_operands = true;
5119 op1 = c_fully_fold (op1, require_constant_value, NULL);
5121 if (ifexp_bcp && ifexp == truthvalue_false_node)
5123 op1_int_operands = true;
5124 op2 = c_fully_fold (op2, require_constant_value, NULL);
5126 int_const = int_operands = (ifexp_int_operands
5127 && op1_int_operands
5128 && op2_int_operands);
5129 if (int_operands)
5131 int_const = ((ifexp == truthvalue_true_node
5132 && TREE_CODE (orig_op1) == INTEGER_CST
5133 && !TREE_OVERFLOW (orig_op1))
5134 || (ifexp == truthvalue_false_node
5135 && TREE_CODE (orig_op2) == INTEGER_CST
5136 && !TREE_OVERFLOW (orig_op2)));
5139 /* Need to convert condition operand into a vector mask. */
5140 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5142 tree vectype = TREE_TYPE (ifexp);
5143 tree elem_type = TREE_TYPE (vectype);
5144 tree zero = build_int_cst (elem_type, 0);
5145 tree zero_vec = build_vector_from_val (vectype, zero);
5146 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5147 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5150 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5151 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5152 else
5154 if (int_operands)
5156 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5157 nested inside of the expression. */
5158 op1 = c_fully_fold (op1, false, NULL);
5159 op2 = c_fully_fold (op2, false, NULL);
5161 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5162 if (int_operands)
5163 ret = note_integer_operands (ret);
5165 if (semantic_result_type)
5166 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5168 protected_set_expr_location (ret, colon_loc);
5169 return ret;
5172 /* Return a compound expression that performs two expressions and
5173 returns the value of the second of them.
5175 LOC is the location of the COMPOUND_EXPR. */
5177 tree
5178 build_compound_expr (location_t loc, tree expr1, tree expr2)
5180 bool expr1_int_operands, expr2_int_operands;
5181 tree eptype = NULL_TREE;
5182 tree ret;
5184 if (flag_cilkplus
5185 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5186 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5188 error_at (loc,
5189 "spawned function call cannot be part of a comma expression");
5190 return error_mark_node;
5192 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5193 if (expr1_int_operands)
5194 expr1 = remove_c_maybe_const_expr (expr1);
5195 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5196 if (expr2_int_operands)
5197 expr2 = remove_c_maybe_const_expr (expr2);
5199 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5200 expr1 = TREE_OPERAND (expr1, 0);
5201 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5203 eptype = TREE_TYPE (expr2);
5204 expr2 = TREE_OPERAND (expr2, 0);
5207 if (!TREE_SIDE_EFFECTS (expr1))
5209 /* The left-hand operand of a comma expression is like an expression
5210 statement: with -Wunused, we should warn if it doesn't have
5211 any side-effects, unless it was explicitly cast to (void). */
5212 if (warn_unused_value)
5214 if (VOID_TYPE_P (TREE_TYPE (expr1))
5215 && CONVERT_EXPR_P (expr1))
5216 ; /* (void) a, b */
5217 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5218 && TREE_CODE (expr1) == COMPOUND_EXPR
5219 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5220 ; /* (void) a, (void) b, c */
5221 else
5222 warning_at (loc, OPT_Wunused_value,
5223 "left-hand operand of comma expression has no effect");
5226 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5227 && warn_unused_value)
5229 tree r = expr1;
5230 location_t cloc = loc;
5231 while (TREE_CODE (r) == COMPOUND_EXPR)
5233 if (EXPR_HAS_LOCATION (r))
5234 cloc = EXPR_LOCATION (r);
5235 r = TREE_OPERAND (r, 1);
5237 if (!TREE_SIDE_EFFECTS (r)
5238 && !VOID_TYPE_P (TREE_TYPE (r))
5239 && !CONVERT_EXPR_P (r))
5240 warning_at (cloc, OPT_Wunused_value,
5241 "right-hand operand of comma expression has no effect");
5244 /* With -Wunused, we should also warn if the left-hand operand does have
5245 side-effects, but computes a value which is not used. For example, in
5246 `foo() + bar(), baz()' the result of the `+' operator is not used,
5247 so we should issue a warning. */
5248 else if (warn_unused_value)
5249 warn_if_unused_value (expr1, loc);
5251 if (expr2 == error_mark_node)
5252 return error_mark_node;
5254 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5256 if (flag_isoc99
5257 && expr1_int_operands
5258 && expr2_int_operands)
5259 ret = note_integer_operands (ret);
5261 if (eptype)
5262 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5264 protected_set_expr_location (ret, loc);
5265 return ret;
5268 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5269 which we are casting. OTYPE is the type of the expression being
5270 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5271 of the cast. -Wcast-qual appeared on the command line. Named
5272 address space qualifiers are not handled here, because they result
5273 in different warnings. */
5275 static void
5276 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5278 tree in_type = type;
5279 tree in_otype = otype;
5280 int added = 0;
5281 int discarded = 0;
5282 bool is_const;
5284 /* Check that the qualifiers on IN_TYPE are a superset of the
5285 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5286 nodes is uninteresting and we stop as soon as we hit a
5287 non-POINTER_TYPE node on either type. */
5290 in_otype = TREE_TYPE (in_otype);
5291 in_type = TREE_TYPE (in_type);
5293 /* GNU C allows cv-qualified function types. 'const' means the
5294 function is very pure, 'volatile' means it can't return. We
5295 need to warn when such qualifiers are added, not when they're
5296 taken away. */
5297 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5298 && TREE_CODE (in_type) == FUNCTION_TYPE)
5299 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5300 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5301 else
5302 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5303 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5305 while (TREE_CODE (in_type) == POINTER_TYPE
5306 && TREE_CODE (in_otype) == POINTER_TYPE);
5308 if (added)
5309 warning_at (loc, OPT_Wcast_qual,
5310 "cast adds %q#v qualifier to function type", added);
5312 if (discarded)
5313 /* There are qualifiers present in IN_OTYPE that are not present
5314 in IN_TYPE. */
5315 warning_at (loc, OPT_Wcast_qual,
5316 "cast discards %qv qualifier from pointer target type",
5317 discarded);
5319 if (added || discarded)
5320 return;
5322 /* A cast from **T to const **T is unsafe, because it can cause a
5323 const value to be changed with no additional warning. We only
5324 issue this warning if T is the same on both sides, and we only
5325 issue the warning if there are the same number of pointers on
5326 both sides, as otherwise the cast is clearly unsafe anyhow. A
5327 cast is unsafe when a qualifier is added at one level and const
5328 is not present at all outer levels.
5330 To issue this warning, we check at each level whether the cast
5331 adds new qualifiers not already seen. We don't need to special
5332 case function types, as they won't have the same
5333 TYPE_MAIN_VARIANT. */
5335 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5336 return;
5337 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5338 return;
5340 in_type = type;
5341 in_otype = otype;
5342 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5345 in_type = TREE_TYPE (in_type);
5346 in_otype = TREE_TYPE (in_otype);
5347 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5348 && !is_const)
5350 warning_at (loc, OPT_Wcast_qual,
5351 "to be safe all intermediate pointers in cast from "
5352 "%qT to %qT must be %<const%> qualified",
5353 otype, type);
5354 break;
5356 if (is_const)
5357 is_const = TYPE_READONLY (in_type);
5359 while (TREE_CODE (in_type) == POINTER_TYPE);
5362 /* Build an expression representing a cast to type TYPE of expression EXPR.
5363 LOC is the location of the cast-- typically the open paren of the cast. */
5365 tree
5366 build_c_cast (location_t loc, tree type, tree expr)
5368 tree value;
5370 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5371 expr = TREE_OPERAND (expr, 0);
5373 value = expr;
5375 if (type == error_mark_node || expr == error_mark_node)
5376 return error_mark_node;
5378 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5379 only in <protocol> qualifications. But when constructing cast expressions,
5380 the protocols do matter and must be kept around. */
5381 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5382 return build1 (NOP_EXPR, type, expr);
5384 type = TYPE_MAIN_VARIANT (type);
5386 if (TREE_CODE (type) == ARRAY_TYPE)
5388 error_at (loc, "cast specifies array type");
5389 return error_mark_node;
5392 if (TREE_CODE (type) == FUNCTION_TYPE)
5394 error_at (loc, "cast specifies function type");
5395 return error_mark_node;
5398 if (!VOID_TYPE_P (type))
5400 value = require_complete_type (loc, value);
5401 if (value == error_mark_node)
5402 return error_mark_node;
5405 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5407 if (RECORD_OR_UNION_TYPE_P (type))
5408 pedwarn (loc, OPT_Wpedantic,
5409 "ISO C forbids casting nonscalar to the same type");
5411 /* Convert to remove any qualifiers from VALUE's type. */
5412 value = convert (type, value);
5414 else if (TREE_CODE (type) == UNION_TYPE)
5416 tree field;
5418 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5419 if (TREE_TYPE (field) != error_mark_node
5420 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5421 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5422 break;
5424 if (field)
5426 tree t;
5427 bool maybe_const = true;
5429 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5430 t = c_fully_fold (value, false, &maybe_const);
5431 t = build_constructor_single (type, field, t);
5432 if (!maybe_const)
5433 t = c_wrap_maybe_const (t, true);
5434 t = digest_init (loc, type, t,
5435 NULL_TREE, false, true, 0);
5436 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5437 return t;
5439 error_at (loc, "cast to union type from type not present in union");
5440 return error_mark_node;
5442 else
5444 tree otype, ovalue;
5446 if (type == void_type_node)
5448 tree t = build1 (CONVERT_EXPR, type, value);
5449 SET_EXPR_LOCATION (t, loc);
5450 return t;
5453 otype = TREE_TYPE (value);
5455 /* Optionally warn about potentially worrisome casts. */
5456 if (warn_cast_qual
5457 && TREE_CODE (type) == POINTER_TYPE
5458 && TREE_CODE (otype) == POINTER_TYPE)
5459 handle_warn_cast_qual (loc, type, otype);
5461 /* Warn about conversions between pointers to disjoint
5462 address spaces. */
5463 if (TREE_CODE (type) == POINTER_TYPE
5464 && TREE_CODE (otype) == POINTER_TYPE
5465 && !null_pointer_constant_p (value))
5467 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5468 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5469 addr_space_t as_common;
5471 if (!addr_space_superset (as_to, as_from, &as_common))
5473 if (ADDR_SPACE_GENERIC_P (as_from))
5474 warning_at (loc, 0, "cast to %s address space pointer "
5475 "from disjoint generic address space pointer",
5476 c_addr_space_name (as_to));
5478 else if (ADDR_SPACE_GENERIC_P (as_to))
5479 warning_at (loc, 0, "cast to generic address space pointer "
5480 "from disjoint %s address space pointer",
5481 c_addr_space_name (as_from));
5483 else
5484 warning_at (loc, 0, "cast to %s address space pointer "
5485 "from disjoint %s address space pointer",
5486 c_addr_space_name (as_to),
5487 c_addr_space_name (as_from));
5491 /* Warn about possible alignment problems. */
5492 if (STRICT_ALIGNMENT
5493 && TREE_CODE (type) == POINTER_TYPE
5494 && TREE_CODE (otype) == POINTER_TYPE
5495 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5496 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5497 /* Don't warn about opaque types, where the actual alignment
5498 restriction is unknown. */
5499 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5500 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5501 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5502 warning_at (loc, OPT_Wcast_align,
5503 "cast increases required alignment of target type");
5505 if (TREE_CODE (type) == INTEGER_TYPE
5506 && TREE_CODE (otype) == POINTER_TYPE
5507 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5508 /* Unlike conversion of integers to pointers, where the
5509 warning is disabled for converting constants because
5510 of cases such as SIG_*, warn about converting constant
5511 pointers to integers. In some cases it may cause unwanted
5512 sign extension, and a warning is appropriate. */
5513 warning_at (loc, OPT_Wpointer_to_int_cast,
5514 "cast from pointer to integer of different size");
5516 if (TREE_CODE (value) == CALL_EXPR
5517 && TREE_CODE (type) != TREE_CODE (otype))
5518 warning_at (loc, OPT_Wbad_function_cast,
5519 "cast from function call of type %qT "
5520 "to non-matching type %qT", otype, type);
5522 if (TREE_CODE (type) == POINTER_TYPE
5523 && TREE_CODE (otype) == INTEGER_TYPE
5524 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5525 /* Don't warn about converting any constant. */
5526 && !TREE_CONSTANT (value))
5527 warning_at (loc,
5528 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5529 "of different size");
5531 if (warn_strict_aliasing <= 2)
5532 strict_aliasing_warning (otype, type, expr);
5534 /* If pedantic, warn for conversions between function and object
5535 pointer types, except for converting a null pointer constant
5536 to function pointer type. */
5537 if (pedantic
5538 && TREE_CODE (type) == POINTER_TYPE
5539 && TREE_CODE (otype) == POINTER_TYPE
5540 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5541 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5542 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5543 "conversion of function pointer to object pointer type");
5545 if (pedantic
5546 && TREE_CODE (type) == POINTER_TYPE
5547 && TREE_CODE (otype) == POINTER_TYPE
5548 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5549 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5550 && !null_pointer_constant_p (value))
5551 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5552 "conversion of object pointer to function pointer type");
5554 ovalue = value;
5555 value = convert (type, value);
5557 /* Ignore any integer overflow caused by the cast. */
5558 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5560 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5562 if (!TREE_OVERFLOW (value))
5564 /* Avoid clobbering a shared constant. */
5565 value = copy_node (value);
5566 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5569 else if (TREE_OVERFLOW (value))
5570 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5571 value = wide_int_to_tree (TREE_TYPE (value), value);
5575 /* Don't let a cast be an lvalue. */
5576 if (lvalue_p (value))
5577 value = non_lvalue_loc (loc, value);
5579 /* Don't allow the results of casting to floating-point or complex
5580 types be confused with actual constants, or casts involving
5581 integer and pointer types other than direct integer-to-integer
5582 and integer-to-pointer be confused with integer constant
5583 expressions and null pointer constants. */
5584 if (TREE_CODE (value) == REAL_CST
5585 || TREE_CODE (value) == COMPLEX_CST
5586 || (TREE_CODE (value) == INTEGER_CST
5587 && !((TREE_CODE (expr) == INTEGER_CST
5588 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5589 || TREE_CODE (expr) == REAL_CST
5590 || TREE_CODE (expr) == COMPLEX_CST)))
5591 value = build1 (NOP_EXPR, type, value);
5593 protected_set_expr_location (value, loc);
5594 return value;
5597 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5598 location of the open paren of the cast, or the position of the cast
5599 expr. */
5600 tree
5601 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5603 tree type;
5604 tree type_expr = NULL_TREE;
5605 bool type_expr_const = true;
5606 tree ret;
5607 int saved_wsp = warn_strict_prototypes;
5609 /* This avoids warnings about unprototyped casts on
5610 integers. E.g. "#define SIG_DFL (void(*)())0". */
5611 if (TREE_CODE (expr) == INTEGER_CST)
5612 warn_strict_prototypes = 0;
5613 type = groktypename (type_name, &type_expr, &type_expr_const);
5614 warn_strict_prototypes = saved_wsp;
5616 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5617 && reject_gcc_builtin (expr))
5618 return error_mark_node;
5620 ret = build_c_cast (loc, type, expr);
5621 if (type_expr)
5623 bool inner_expr_const = true;
5624 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5625 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5626 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5627 && inner_expr_const);
5628 SET_EXPR_LOCATION (ret, loc);
5631 if (!EXPR_HAS_LOCATION (ret))
5632 protected_set_expr_location (ret, loc);
5634 /* C++ does not permits types to be defined in a cast, but it
5635 allows references to incomplete types. */
5636 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5637 warning_at (loc, OPT_Wc___compat,
5638 "defining a type in a cast is invalid in C++");
5640 return ret;
5643 /* Build an assignment expression of lvalue LHS from value RHS.
5644 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5645 may differ from TREE_TYPE (LHS) for an enum bitfield.
5646 MODIFYCODE is the code for a binary operator that we use
5647 to combine the old value of LHS with RHS to get the new value.
5648 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5649 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5650 which may differ from TREE_TYPE (RHS) for an enum value.
5652 LOCATION is the location of the MODIFYCODE operator.
5653 RHS_LOC is the location of the RHS. */
5655 tree
5656 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5657 enum tree_code modifycode,
5658 location_t rhs_loc, tree rhs, tree rhs_origtype)
5660 tree result;
5661 tree newrhs;
5662 tree rhseval = NULL_TREE;
5663 tree rhs_semantic_type = NULL_TREE;
5664 tree lhstype = TREE_TYPE (lhs);
5665 tree olhstype = lhstype;
5666 bool npc;
5667 bool is_atomic_op;
5669 /* Types that aren't fully specified cannot be used in assignments. */
5670 lhs = require_complete_type (location, lhs);
5672 /* Avoid duplicate error messages from operands that had errors. */
5673 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5674 return error_mark_node;
5676 /* Ensure an error for assigning a non-lvalue array to an array in
5677 C90. */
5678 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5680 error_at (location, "assignment to expression with array type");
5681 return error_mark_node;
5684 /* For ObjC properties, defer this check. */
5685 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5686 return error_mark_node;
5688 is_atomic_op = really_atomic_lvalue (lhs);
5690 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5692 rhs_semantic_type = TREE_TYPE (rhs);
5693 rhs = TREE_OPERAND (rhs, 0);
5696 newrhs = rhs;
5698 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5700 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5701 lhs_origtype, modifycode, rhs_loc, rhs,
5702 rhs_origtype);
5703 if (inner == error_mark_node)
5704 return error_mark_node;
5705 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5706 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5707 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5708 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5709 protected_set_expr_location (result, location);
5710 return result;
5713 /* If a binary op has been requested, combine the old LHS value with the RHS
5714 producing the value we should actually store into the LHS. */
5716 if (modifycode != NOP_EXPR)
5718 lhs = c_fully_fold (lhs, false, NULL);
5719 lhs = stabilize_reference (lhs);
5721 /* Construct the RHS for any non-atomic compound assignemnt. */
5722 if (!is_atomic_op)
5724 /* If in LHS op= RHS the RHS has side-effects, ensure they
5725 are preevaluated before the rest of the assignment expression's
5726 side-effects, because RHS could contain e.g. function calls
5727 that modify LHS. */
5728 if (TREE_SIDE_EFFECTS (rhs))
5730 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5731 rhseval = newrhs;
5733 newrhs = build_binary_op (location,
5734 modifycode, lhs, newrhs, 1);
5736 /* The original type of the right hand side is no longer
5737 meaningful. */
5738 rhs_origtype = NULL_TREE;
5742 if (c_dialect_objc ())
5744 /* Check if we are modifying an Objective-C property reference;
5745 if so, we need to generate setter calls. */
5746 result = objc_maybe_build_modify_expr (lhs, newrhs);
5747 if (result)
5748 goto return_result;
5750 /* Else, do the check that we postponed for Objective-C. */
5751 if (!lvalue_or_else (location, lhs, lv_assign))
5752 return error_mark_node;
5755 /* Give an error for storing in something that is 'const'. */
5757 if (TYPE_READONLY (lhstype)
5758 || (RECORD_OR_UNION_TYPE_P (lhstype)
5759 && C_TYPE_FIELDS_READONLY (lhstype)))
5761 readonly_error (location, lhs, lv_assign);
5762 return error_mark_node;
5764 else if (TREE_READONLY (lhs))
5765 readonly_warning (lhs, lv_assign);
5767 /* If storing into a structure or union member,
5768 it has probably been given type `int'.
5769 Compute the type that would go with
5770 the actual amount of storage the member occupies. */
5772 if (TREE_CODE (lhs) == COMPONENT_REF
5773 && (TREE_CODE (lhstype) == INTEGER_TYPE
5774 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5775 || TREE_CODE (lhstype) == REAL_TYPE
5776 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5777 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5779 /* If storing in a field that is in actuality a short or narrower than one,
5780 we must store in the field in its actual type. */
5782 if (lhstype != TREE_TYPE (lhs))
5784 lhs = copy_node (lhs);
5785 TREE_TYPE (lhs) = lhstype;
5788 /* Issue -Wc++-compat warnings about an assignment to an enum type
5789 when LHS does not have its original type. This happens for,
5790 e.g., an enum bitfield in a struct. */
5791 if (warn_cxx_compat
5792 && lhs_origtype != NULL_TREE
5793 && lhs_origtype != lhstype
5794 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5796 tree checktype = (rhs_origtype != NULL_TREE
5797 ? rhs_origtype
5798 : TREE_TYPE (rhs));
5799 if (checktype != error_mark_node
5800 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5801 || (is_atomic_op && modifycode != NOP_EXPR)))
5802 warning_at (location, OPT_Wc___compat,
5803 "enum conversion in assignment is invalid in C++");
5806 /* If the lhs is atomic, remove that qualifier. */
5807 if (is_atomic_op)
5809 lhstype = build_qualified_type (lhstype,
5810 (TYPE_QUALS (lhstype)
5811 & ~TYPE_QUAL_ATOMIC));
5812 olhstype = build_qualified_type (olhstype,
5813 (TYPE_QUALS (lhstype)
5814 & ~TYPE_QUAL_ATOMIC));
5817 /* Convert new value to destination type. Fold it first, then
5818 restore any excess precision information, for the sake of
5819 conversion warnings. */
5821 if (!(is_atomic_op && modifycode != NOP_EXPR))
5823 npc = null_pointer_constant_p (newrhs);
5824 newrhs = c_fully_fold (newrhs, false, NULL);
5825 if (rhs_semantic_type)
5826 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5827 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5828 rhs_origtype, ic_assign, npc,
5829 NULL_TREE, NULL_TREE, 0);
5830 if (TREE_CODE (newrhs) == ERROR_MARK)
5831 return error_mark_node;
5834 /* Emit ObjC write barrier, if necessary. */
5835 if (c_dialect_objc () && flag_objc_gc)
5837 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5838 if (result)
5840 protected_set_expr_location (result, location);
5841 goto return_result;
5845 /* Scan operands. */
5847 if (is_atomic_op)
5848 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5849 else
5851 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5852 TREE_SIDE_EFFECTS (result) = 1;
5853 protected_set_expr_location (result, location);
5856 /* If we got the LHS in a different type for storing in,
5857 convert the result back to the nominal type of LHS
5858 so that the value we return always has the same type
5859 as the LHS argument. */
5861 if (olhstype == TREE_TYPE (result))
5862 goto return_result;
5864 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5865 rhs_origtype, ic_assign, false, NULL_TREE,
5866 NULL_TREE, 0);
5867 protected_set_expr_location (result, location);
5869 return_result:
5870 if (rhseval)
5871 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5872 return result;
5875 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5876 This is used to implement -fplan9-extensions. */
5878 static bool
5879 find_anonymous_field_with_type (tree struct_type, tree type)
5881 tree field;
5882 bool found;
5884 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
5885 found = false;
5886 for (field = TYPE_FIELDS (struct_type);
5887 field != NULL_TREE;
5888 field = TREE_CHAIN (field))
5890 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5891 ? c_build_qualified_type (TREE_TYPE (field),
5892 TYPE_QUAL_ATOMIC)
5893 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5894 if (DECL_NAME (field) == NULL
5895 && comptypes (type, fieldtype))
5897 if (found)
5898 return false;
5899 found = true;
5901 else if (DECL_NAME (field) == NULL
5902 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
5903 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5905 if (found)
5906 return false;
5907 found = true;
5910 return found;
5913 /* RHS is an expression whose type is pointer to struct. If there is
5914 an anonymous field in RHS with type TYPE, then return a pointer to
5915 that field in RHS. This is used with -fplan9-extensions. This
5916 returns NULL if no conversion could be found. */
5918 static tree
5919 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5921 tree rhs_struct_type, lhs_main_type;
5922 tree field, found_field;
5923 bool found_sub_field;
5924 tree ret;
5926 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5927 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5928 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
5930 gcc_assert (POINTER_TYPE_P (type));
5931 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5932 ? c_build_qualified_type (TREE_TYPE (type),
5933 TYPE_QUAL_ATOMIC)
5934 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5936 found_field = NULL_TREE;
5937 found_sub_field = false;
5938 for (field = TYPE_FIELDS (rhs_struct_type);
5939 field != NULL_TREE;
5940 field = TREE_CHAIN (field))
5942 if (DECL_NAME (field) != NULL_TREE
5943 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
5944 continue;
5945 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5946 ? c_build_qualified_type (TREE_TYPE (field),
5947 TYPE_QUAL_ATOMIC)
5948 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5949 if (comptypes (lhs_main_type, fieldtype))
5951 if (found_field != NULL_TREE)
5952 return NULL_TREE;
5953 found_field = field;
5955 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5956 lhs_main_type))
5958 if (found_field != NULL_TREE)
5959 return NULL_TREE;
5960 found_field = field;
5961 found_sub_field = true;
5965 if (found_field == NULL_TREE)
5966 return NULL_TREE;
5968 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5969 build_fold_indirect_ref (rhs), found_field,
5970 NULL_TREE);
5971 ret = build_fold_addr_expr_loc (location, ret);
5973 if (found_sub_field)
5975 ret = convert_to_anonymous_field (location, type, ret);
5976 gcc_assert (ret != NULL_TREE);
5979 return ret;
5982 /* Issue an error message for a bad initializer component.
5983 GMSGID identifies the message.
5984 The component name is taken from the spelling stack. */
5986 static void
5987 error_init (location_t loc, const char *gmsgid)
5989 char *ofwhat;
5991 /* The gmsgid may be a format string with %< and %>. */
5992 error_at (loc, gmsgid);
5993 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5994 if (*ofwhat)
5995 inform (loc, "(near initialization for %qs)", ofwhat);
5998 /* Issue a pedantic warning for a bad initializer component. OPT is
5999 the option OPT_* (from options.h) controlling this warning or 0 if
6000 it is unconditionally given. GMSGID identifies the message. The
6001 component name is taken from the spelling stack. */
6003 static void
6004 pedwarn_init (location_t loc, int opt, const char *gmsgid)
6006 char *ofwhat;
6007 bool warned;
6009 /* Use the location where a macro was expanded rather than where
6010 it was defined to make sure macros defined in system headers
6011 but used incorrectly elsewhere are diagnosed. */
6012 source_location exploc = expansion_point_location_if_in_system_header (loc);
6014 /* The gmsgid may be a format string with %< and %>. */
6015 warned = pedwarn (exploc, opt, gmsgid);
6016 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6017 if (*ofwhat && warned)
6018 inform (exploc, "(near initialization for %qs)", ofwhat);
6021 /* Issue a warning for a bad initializer component.
6023 OPT is the OPT_W* value corresponding to the warning option that
6024 controls this warning. GMSGID identifies the message. The
6025 component name is taken from the spelling stack. */
6027 static void
6028 warning_init (location_t loc, int opt, const char *gmsgid)
6030 char *ofwhat;
6031 bool warned;
6033 /* Use the location where a macro was expanded rather than where
6034 it was defined to make sure macros defined in system headers
6035 but used incorrectly elsewhere are diagnosed. */
6036 source_location exploc = expansion_point_location_if_in_system_header (loc);
6038 /* The gmsgid may be a format string with %< and %>. */
6039 warned = warning_at (exploc, opt, gmsgid);
6040 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6041 if (*ofwhat && warned)
6042 inform (exploc, "(near initialization for %qs)", ofwhat);
6045 /* If TYPE is an array type and EXPR is a parenthesized string
6046 constant, warn if pedantic that EXPR is being used to initialize an
6047 object of type TYPE. */
6049 void
6050 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6052 if (pedantic
6053 && TREE_CODE (type) == ARRAY_TYPE
6054 && TREE_CODE (expr.value) == STRING_CST
6055 && expr.original_code != STRING_CST)
6056 pedwarn_init (loc, OPT_Wpedantic,
6057 "array initialized from parenthesized string constant");
6060 /* Convert value RHS to type TYPE as preparation for an assignment to
6061 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6062 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6063 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6064 constant before any folding.
6065 The real work of conversion is done by `convert'.
6066 The purpose of this function is to generate error messages
6067 for assignments that are not allowed in C.
6068 ERRTYPE says whether it is argument passing, assignment,
6069 initialization or return.
6071 In the following example, '~' denotes where EXPR_LOC and '^' where
6072 LOCATION point to:
6074 f (var); [ic_argpass]
6075 ^ ~~~
6076 x = var; [ic_assign]
6077 ^ ~~~;
6078 int x = var; [ic_init]
6080 return x; [ic_return]
6083 FUNCTION is a tree for the function being called.
6084 PARMNUM is the number of the argument, for printing in error messages. */
6086 static tree
6087 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6088 tree rhs, tree origtype, enum impl_conv errtype,
6089 bool null_pointer_constant, tree fundecl,
6090 tree function, int parmnum)
6092 enum tree_code codel = TREE_CODE (type);
6093 tree orig_rhs = rhs;
6094 tree rhstype;
6095 enum tree_code coder;
6096 tree rname = NULL_TREE;
6097 bool objc_ok = false;
6099 /* Use the expansion point location to handle cases such as user's
6100 function returning a wrong-type macro defined in a system header. */
6101 location = expansion_point_location_if_in_system_header (location);
6103 if (errtype == ic_argpass)
6105 tree selector;
6106 /* Change pointer to function to the function itself for
6107 diagnostics. */
6108 if (TREE_CODE (function) == ADDR_EXPR
6109 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6110 function = TREE_OPERAND (function, 0);
6112 /* Handle an ObjC selector specially for diagnostics. */
6113 selector = objc_message_selector ();
6114 rname = function;
6115 if (selector && parmnum > 2)
6117 rname = selector;
6118 parmnum -= 2;
6122 /* This macro is used to emit diagnostics to ensure that all format
6123 strings are complete sentences, visible to gettext and checked at
6124 compile time. */
6125 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6126 do { \
6127 switch (errtype) \
6129 case ic_argpass: \
6130 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6131 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6132 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6133 "expected %qT but argument is of type %qT", \
6134 type, rhstype); \
6135 break; \
6136 case ic_assign: \
6137 pedwarn (LOCATION, OPT, AS); \
6138 break; \
6139 case ic_init: \
6140 pedwarn_init (LOCATION, OPT, IN); \
6141 break; \
6142 case ic_return: \
6143 pedwarn (LOCATION, OPT, RE); \
6144 break; \
6145 default: \
6146 gcc_unreachable (); \
6148 } while (0)
6150 /* This macro is used to emit diagnostics to ensure that all format
6151 strings are complete sentences, visible to gettext and checked at
6152 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6153 extra parameter to enumerate qualifiers. */
6154 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6155 do { \
6156 switch (errtype) \
6158 case ic_argpass: \
6159 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6160 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6161 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6162 "expected %qT but argument is of type %qT", \
6163 type, rhstype); \
6164 break; \
6165 case ic_assign: \
6166 pedwarn (LOCATION, OPT, AS, QUALS); \
6167 break; \
6168 case ic_init: \
6169 pedwarn (LOCATION, OPT, IN, QUALS); \
6170 break; \
6171 case ic_return: \
6172 pedwarn (LOCATION, OPT, RE, QUALS); \
6173 break; \
6174 default: \
6175 gcc_unreachable (); \
6177 } while (0)
6179 /* This macro is used to emit diagnostics to ensure that all format
6180 strings are complete sentences, visible to gettext and checked at
6181 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6182 warning_at instead of pedwarn. */
6183 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6184 do { \
6185 switch (errtype) \
6187 case ic_argpass: \
6188 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6189 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6190 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6191 "expected %qT but argument is of type %qT", \
6192 type, rhstype); \
6193 break; \
6194 case ic_assign: \
6195 warning_at (LOCATION, OPT, AS, QUALS); \
6196 break; \
6197 case ic_init: \
6198 warning_at (LOCATION, OPT, IN, QUALS); \
6199 break; \
6200 case ic_return: \
6201 warning_at (LOCATION, OPT, RE, QUALS); \
6202 break; \
6203 default: \
6204 gcc_unreachable (); \
6206 } while (0)
6208 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6209 rhs = TREE_OPERAND (rhs, 0);
6211 rhstype = TREE_TYPE (rhs);
6212 coder = TREE_CODE (rhstype);
6214 if (coder == ERROR_MARK)
6215 return error_mark_node;
6217 if (c_dialect_objc ())
6219 int parmno;
6221 switch (errtype)
6223 case ic_return:
6224 parmno = 0;
6225 break;
6227 case ic_assign:
6228 parmno = -1;
6229 break;
6231 case ic_init:
6232 parmno = -2;
6233 break;
6235 default:
6236 parmno = parmnum;
6237 break;
6240 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6243 if (warn_cxx_compat)
6245 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6246 if (checktype != error_mark_node
6247 && TREE_CODE (type) == ENUMERAL_TYPE
6248 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6250 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6251 G_("enum conversion when passing argument "
6252 "%d of %qE is invalid in C++"),
6253 G_("enum conversion in assignment is "
6254 "invalid in C++"),
6255 G_("enum conversion in initialization is "
6256 "invalid in C++"),
6257 G_("enum conversion in return is "
6258 "invalid in C++"));
6262 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6263 return rhs;
6265 if (coder == VOID_TYPE)
6267 /* Except for passing an argument to an unprototyped function,
6268 this is a constraint violation. When passing an argument to
6269 an unprototyped function, it is compile-time undefined;
6270 making it a constraint in that case was rejected in
6271 DR#252. */
6272 error_at (location, "void value not ignored as it ought to be");
6273 return error_mark_node;
6275 rhs = require_complete_type (location, rhs);
6276 if (rhs == error_mark_node)
6277 return error_mark_node;
6279 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6280 return error_mark_node;
6282 /* A non-reference type can convert to a reference. This handles
6283 va_start, va_copy and possibly port built-ins. */
6284 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6286 if (!lvalue_p (rhs))
6288 error_at (location, "cannot pass rvalue to reference parameter");
6289 return error_mark_node;
6291 if (!c_mark_addressable (rhs))
6292 return error_mark_node;
6293 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6294 SET_EXPR_LOCATION (rhs, location);
6296 rhs = convert_for_assignment (location, expr_loc,
6297 build_pointer_type (TREE_TYPE (type)),
6298 rhs, origtype, errtype,
6299 null_pointer_constant, fundecl, function,
6300 parmnum);
6301 if (rhs == error_mark_node)
6302 return error_mark_node;
6304 rhs = build1 (NOP_EXPR, type, rhs);
6305 SET_EXPR_LOCATION (rhs, location);
6306 return rhs;
6308 /* Some types can interconvert without explicit casts. */
6309 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6310 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6311 return convert (type, rhs);
6312 /* Arithmetic types all interconvert, and enum is treated like int. */
6313 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6314 || codel == FIXED_POINT_TYPE
6315 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6316 || codel == BOOLEAN_TYPE)
6317 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6318 || coder == FIXED_POINT_TYPE
6319 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6320 || coder == BOOLEAN_TYPE))
6322 tree ret;
6323 bool save = in_late_binary_op;
6324 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6325 || (coder == REAL_TYPE
6326 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6327 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
6328 in_late_binary_op = true;
6329 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6330 ? expr_loc : location, type, orig_rhs);
6331 in_late_binary_op = save;
6332 return ret;
6335 /* Aggregates in different TUs might need conversion. */
6336 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6337 && codel == coder
6338 && comptypes (type, rhstype))
6339 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6340 ? expr_loc : location, type, rhs);
6342 /* Conversion to a transparent union or record from its member types.
6343 This applies only to function arguments. */
6344 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6345 && TYPE_TRANSPARENT_AGGR (type))
6346 && errtype == ic_argpass)
6348 tree memb, marginal_memb = NULL_TREE;
6350 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6352 tree memb_type = TREE_TYPE (memb);
6354 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6355 TYPE_MAIN_VARIANT (rhstype)))
6356 break;
6358 if (TREE_CODE (memb_type) != POINTER_TYPE)
6359 continue;
6361 if (coder == POINTER_TYPE)
6363 tree ttl = TREE_TYPE (memb_type);
6364 tree ttr = TREE_TYPE (rhstype);
6366 /* Any non-function converts to a [const][volatile] void *
6367 and vice versa; otherwise, targets must be the same.
6368 Meanwhile, the lhs target must have all the qualifiers of
6369 the rhs. */
6370 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6371 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6372 || comp_target_types (location, memb_type, rhstype))
6374 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6375 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6376 /* If this type won't generate any warnings, use it. */
6377 if (lquals == rquals
6378 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6379 && TREE_CODE (ttl) == FUNCTION_TYPE)
6380 ? ((lquals | rquals) == rquals)
6381 : ((lquals | rquals) == lquals)))
6382 break;
6384 /* Keep looking for a better type, but remember this one. */
6385 if (!marginal_memb)
6386 marginal_memb = memb;
6390 /* Can convert integer zero to any pointer type. */
6391 if (null_pointer_constant)
6393 rhs = null_pointer_node;
6394 break;
6398 if (memb || marginal_memb)
6400 if (!memb)
6402 /* We have only a marginally acceptable member type;
6403 it needs a warning. */
6404 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6405 tree ttr = TREE_TYPE (rhstype);
6407 /* Const and volatile mean something different for function
6408 types, so the usual warnings are not appropriate. */
6409 if (TREE_CODE (ttr) == FUNCTION_TYPE
6410 && TREE_CODE (ttl) == FUNCTION_TYPE)
6412 /* Because const and volatile on functions are
6413 restrictions that say the function will not do
6414 certain things, it is okay to use a const or volatile
6415 function where an ordinary one is wanted, but not
6416 vice-versa. */
6417 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6418 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6419 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6420 OPT_Wdiscarded_qualifiers,
6421 G_("passing argument %d of %qE "
6422 "makes %q#v qualified function "
6423 "pointer from unqualified"),
6424 G_("assignment makes %q#v qualified "
6425 "function pointer from "
6426 "unqualified"),
6427 G_("initialization makes %q#v qualified "
6428 "function pointer from "
6429 "unqualified"),
6430 G_("return makes %q#v qualified function "
6431 "pointer from unqualified"),
6432 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6434 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6435 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6436 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6437 OPT_Wdiscarded_qualifiers,
6438 G_("passing argument %d of %qE discards "
6439 "%qv qualifier from pointer target type"),
6440 G_("assignment discards %qv qualifier "
6441 "from pointer target type"),
6442 G_("initialization discards %qv qualifier "
6443 "from pointer target type"),
6444 G_("return discards %qv qualifier from "
6445 "pointer target type"),
6446 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6448 memb = marginal_memb;
6451 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6452 pedwarn (location, OPT_Wpedantic,
6453 "ISO C prohibits argument conversion to union type");
6455 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6456 return build_constructor_single (type, memb, rhs);
6460 /* Conversions among pointers */
6461 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6462 && (coder == codel))
6464 tree ttl = TREE_TYPE (type);
6465 tree ttr = TREE_TYPE (rhstype);
6466 tree mvl = ttl;
6467 tree mvr = ttr;
6468 bool is_opaque_pointer;
6469 int target_cmp = 0; /* Cache comp_target_types () result. */
6470 addr_space_t asl;
6471 addr_space_t asr;
6473 if (TREE_CODE (mvl) != ARRAY_TYPE)
6474 mvl = (TYPE_ATOMIC (mvl)
6475 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6476 TYPE_QUAL_ATOMIC)
6477 : TYPE_MAIN_VARIANT (mvl));
6478 if (TREE_CODE (mvr) != ARRAY_TYPE)
6479 mvr = (TYPE_ATOMIC (mvr)
6480 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6481 TYPE_QUAL_ATOMIC)
6482 : TYPE_MAIN_VARIANT (mvr));
6483 /* Opaque pointers are treated like void pointers. */
6484 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6486 /* The Plan 9 compiler permits a pointer to a struct to be
6487 automatically converted into a pointer to an anonymous field
6488 within the struct. */
6489 if (flag_plan9_extensions
6490 && RECORD_OR_UNION_TYPE_P (mvl)
6491 && RECORD_OR_UNION_TYPE_P (mvr)
6492 && mvl != mvr)
6494 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6495 if (new_rhs != NULL_TREE)
6497 rhs = new_rhs;
6498 rhstype = TREE_TYPE (rhs);
6499 coder = TREE_CODE (rhstype);
6500 ttr = TREE_TYPE (rhstype);
6501 mvr = TYPE_MAIN_VARIANT (ttr);
6505 /* C++ does not allow the implicit conversion void* -> T*. However,
6506 for the purpose of reducing the number of false positives, we
6507 tolerate the special case of
6509 int *p = NULL;
6511 where NULL is typically defined in C to be '(void *) 0'. */
6512 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6513 warning_at (errtype == ic_argpass ? expr_loc : location,
6514 OPT_Wc___compat,
6515 "request for implicit conversion "
6516 "from %qT to %qT not permitted in C++", rhstype, type);
6518 /* See if the pointers point to incompatible address spaces. */
6519 asl = TYPE_ADDR_SPACE (ttl);
6520 asr = TYPE_ADDR_SPACE (ttr);
6521 if (!null_pointer_constant_p (rhs)
6522 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6524 switch (errtype)
6526 case ic_argpass:
6527 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6528 "non-enclosed address space", parmnum, rname);
6529 break;
6530 case ic_assign:
6531 error_at (location, "assignment from pointer to "
6532 "non-enclosed address space");
6533 break;
6534 case ic_init:
6535 error_at (location, "initialization from pointer to "
6536 "non-enclosed address space");
6537 break;
6538 case ic_return:
6539 error_at (location, "return from pointer to "
6540 "non-enclosed address space");
6541 break;
6542 default:
6543 gcc_unreachable ();
6545 return error_mark_node;
6548 /* Check if the right-hand side has a format attribute but the
6549 left-hand side doesn't. */
6550 if (warn_suggest_attribute_format
6551 && check_missing_format_attribute (type, rhstype))
6553 switch (errtype)
6555 case ic_argpass:
6556 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6557 "argument %d of %qE might be "
6558 "a candidate for a format attribute",
6559 parmnum, rname);
6560 break;
6561 case ic_assign:
6562 warning_at (location, OPT_Wsuggest_attribute_format,
6563 "assignment left-hand side might be "
6564 "a candidate for a format attribute");
6565 break;
6566 case ic_init:
6567 warning_at (location, OPT_Wsuggest_attribute_format,
6568 "initialization left-hand side might be "
6569 "a candidate for a format attribute");
6570 break;
6571 case ic_return:
6572 warning_at (location, OPT_Wsuggest_attribute_format,
6573 "return type might be "
6574 "a candidate for a format attribute");
6575 break;
6576 default:
6577 gcc_unreachable ();
6581 /* Any non-function converts to a [const][volatile] void *
6582 and vice versa; otherwise, targets must be the same.
6583 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6584 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6585 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6586 || (target_cmp = comp_target_types (location, type, rhstype))
6587 || is_opaque_pointer
6588 || ((c_common_unsigned_type (mvl)
6589 == c_common_unsigned_type (mvr))
6590 && (c_common_signed_type (mvl)
6591 == c_common_signed_type (mvr))
6592 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6594 /* Warn about loss of qualifers from pointers to arrays with
6595 qualifiers on the element type. */
6596 if (TREE_CODE (ttr) == ARRAY_TYPE)
6598 ttr = strip_array_types (ttr);
6599 ttl = strip_array_types (ttl);
6601 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6602 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6603 WARNING_FOR_QUALIFIERS (location, expr_loc,
6604 OPT_Wdiscarded_array_qualifiers,
6605 G_("passing argument %d of %qE discards "
6606 "%qv qualifier from pointer target type"),
6607 G_("assignment discards %qv qualifier "
6608 "from pointer target type"),
6609 G_("initialization discards %qv qualifier "
6610 "from pointer target type"),
6611 G_("return discards %qv qualifier from "
6612 "pointer target type"),
6613 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6615 else if (pedantic
6616 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6618 (VOID_TYPE_P (ttr)
6619 && !null_pointer_constant
6620 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6621 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6622 G_("ISO C forbids passing argument %d of "
6623 "%qE between function pointer "
6624 "and %<void *%>"),
6625 G_("ISO C forbids assignment between "
6626 "function pointer and %<void *%>"),
6627 G_("ISO C forbids initialization between "
6628 "function pointer and %<void *%>"),
6629 G_("ISO C forbids return between function "
6630 "pointer and %<void *%>"));
6631 /* Const and volatile mean something different for function types,
6632 so the usual warnings are not appropriate. */
6633 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6634 && TREE_CODE (ttl) != FUNCTION_TYPE)
6636 /* Don't warn about loss of qualifier for conversions from
6637 qualified void* to pointers to arrays with corresponding
6638 qualifier on the element type. */
6639 if (!pedantic)
6640 ttl = strip_array_types (ttl);
6642 /* Assignments between atomic and non-atomic objects are OK. */
6643 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6644 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6646 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6647 OPT_Wdiscarded_qualifiers,
6648 G_("passing argument %d of %qE discards "
6649 "%qv qualifier from pointer target type"),
6650 G_("assignment discards %qv qualifier "
6651 "from pointer target type"),
6652 G_("initialization discards %qv qualifier "
6653 "from pointer target type"),
6654 G_("return discards %qv qualifier from "
6655 "pointer target type"),
6656 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6658 /* If this is not a case of ignoring a mismatch in signedness,
6659 no warning. */
6660 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6661 || target_cmp)
6663 /* If there is a mismatch, do warn. */
6664 else if (warn_pointer_sign)
6665 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6666 G_("pointer targets in passing argument "
6667 "%d of %qE differ in signedness"),
6668 G_("pointer targets in assignment "
6669 "differ in signedness"),
6670 G_("pointer targets in initialization "
6671 "differ in signedness"),
6672 G_("pointer targets in return differ "
6673 "in signedness"));
6675 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6676 && TREE_CODE (ttr) == FUNCTION_TYPE)
6678 /* Because const and volatile on functions are restrictions
6679 that say the function will not do certain things,
6680 it is okay to use a const or volatile function
6681 where an ordinary one is wanted, but not vice-versa. */
6682 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6683 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6684 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6685 OPT_Wdiscarded_qualifiers,
6686 G_("passing argument %d of %qE makes "
6687 "%q#v qualified function pointer "
6688 "from unqualified"),
6689 G_("assignment makes %q#v qualified function "
6690 "pointer from unqualified"),
6691 G_("initialization makes %q#v qualified "
6692 "function pointer from unqualified"),
6693 G_("return makes %q#v qualified function "
6694 "pointer from unqualified"),
6695 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6698 else
6699 /* Avoid warning about the volatile ObjC EH puts on decls. */
6700 if (!objc_ok)
6701 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6702 OPT_Wincompatible_pointer_types,
6703 G_("passing argument %d of %qE from "
6704 "incompatible pointer type"),
6705 G_("assignment from incompatible pointer type"),
6706 G_("initialization from incompatible "
6707 "pointer type"),
6708 G_("return from incompatible pointer type"));
6710 return convert (type, rhs);
6712 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6714 /* ??? This should not be an error when inlining calls to
6715 unprototyped functions. */
6716 error_at (location, "invalid use of non-lvalue array");
6717 return error_mark_node;
6719 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6721 /* An explicit constant 0 can convert to a pointer,
6722 or one that results from arithmetic, even including
6723 a cast to integer type. */
6724 if (!null_pointer_constant)
6725 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6726 OPT_Wint_conversion,
6727 G_("passing argument %d of %qE makes "
6728 "pointer from integer without a cast"),
6729 G_("assignment makes pointer from integer "
6730 "without a cast"),
6731 G_("initialization makes pointer from "
6732 "integer without a cast"),
6733 G_("return makes pointer from integer "
6734 "without a cast"));
6736 return convert (type, rhs);
6738 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6740 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6741 OPT_Wint_conversion,
6742 G_("passing argument %d of %qE makes integer "
6743 "from pointer without a cast"),
6744 G_("assignment makes integer from pointer "
6745 "without a cast"),
6746 G_("initialization makes integer from pointer "
6747 "without a cast"),
6748 G_("return makes integer from pointer "
6749 "without a cast"));
6750 return convert (type, rhs);
6752 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6754 tree ret;
6755 bool save = in_late_binary_op;
6756 in_late_binary_op = true;
6757 ret = convert (type, rhs);
6758 in_late_binary_op = save;
6759 return ret;
6762 switch (errtype)
6764 case ic_argpass:
6765 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6766 rname);
6767 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6768 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6769 "expected %qT but argument is of type %qT", type, rhstype);
6770 break;
6771 case ic_assign:
6772 error_at (location, "incompatible types when assigning to type %qT from "
6773 "type %qT", type, rhstype);
6774 break;
6775 case ic_init:
6776 error_at (location,
6777 "incompatible types when initializing type %qT using type %qT",
6778 type, rhstype);
6779 break;
6780 case ic_return:
6781 error_at (location,
6782 "incompatible types when returning type %qT but %qT was "
6783 "expected", rhstype, type);
6784 break;
6785 default:
6786 gcc_unreachable ();
6789 return error_mark_node;
6792 /* If VALUE is a compound expr all of whose expressions are constant, then
6793 return its value. Otherwise, return error_mark_node.
6795 This is for handling COMPOUND_EXPRs as initializer elements
6796 which is allowed with a warning when -pedantic is specified. */
6798 static tree
6799 valid_compound_expr_initializer (tree value, tree endtype)
6801 if (TREE_CODE (value) == COMPOUND_EXPR)
6803 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6804 == error_mark_node)
6805 return error_mark_node;
6806 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6807 endtype);
6809 else if (!initializer_constant_valid_p (value, endtype))
6810 return error_mark_node;
6811 else
6812 return value;
6815 /* Perform appropriate conversions on the initial value of a variable,
6816 store it in the declaration DECL,
6817 and print any error messages that are appropriate.
6818 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6819 If the init is invalid, store an ERROR_MARK.
6821 INIT_LOC is the location of the initial value. */
6823 void
6824 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6826 tree value, type;
6827 bool npc = false;
6829 /* If variable's type was invalidly declared, just ignore it. */
6831 type = TREE_TYPE (decl);
6832 if (TREE_CODE (type) == ERROR_MARK)
6833 return;
6835 /* Digest the specified initializer into an expression. */
6837 if (init)
6838 npc = null_pointer_constant_p (init);
6839 value = digest_init (init_loc, type, init, origtype, npc,
6840 true, TREE_STATIC (decl));
6842 /* Store the expression if valid; else report error. */
6844 if (!in_system_header_at (input_location)
6845 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6846 warning (OPT_Wtraditional, "traditional C rejects automatic "
6847 "aggregate initialization");
6849 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6850 DECL_INITIAL (decl) = value;
6852 /* ANSI wants warnings about out-of-range constant initializers. */
6853 STRIP_TYPE_NOPS (value);
6854 if (TREE_STATIC (decl))
6855 constant_expression_warning (value);
6857 /* Check if we need to set array size from compound literal size. */
6858 if (TREE_CODE (type) == ARRAY_TYPE
6859 && TYPE_DOMAIN (type) == 0
6860 && value != error_mark_node)
6862 tree inside_init = init;
6864 STRIP_TYPE_NOPS (inside_init);
6865 inside_init = fold (inside_init);
6867 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6869 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6871 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6873 /* For int foo[] = (int [3]){1}; we need to set array size
6874 now since later on array initializer will be just the
6875 brace enclosed list of the compound literal. */
6876 tree etype = strip_array_types (TREE_TYPE (decl));
6877 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6878 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6879 layout_type (type);
6880 layout_decl (cldecl, 0);
6881 TREE_TYPE (decl)
6882 = c_build_qualified_type (type, TYPE_QUALS (etype));
6888 /* Methods for storing and printing names for error messages. */
6890 /* Implement a spelling stack that allows components of a name to be pushed
6891 and popped. Each element on the stack is this structure. */
6893 struct spelling
6895 int kind;
6896 union
6898 unsigned HOST_WIDE_INT i;
6899 const char *s;
6900 } u;
6903 #define SPELLING_STRING 1
6904 #define SPELLING_MEMBER 2
6905 #define SPELLING_BOUNDS 3
6907 static struct spelling *spelling; /* Next stack element (unused). */
6908 static struct spelling *spelling_base; /* Spelling stack base. */
6909 static int spelling_size; /* Size of the spelling stack. */
6911 /* Macros to save and restore the spelling stack around push_... functions.
6912 Alternative to SAVE_SPELLING_STACK. */
6914 #define SPELLING_DEPTH() (spelling - spelling_base)
6915 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6917 /* Push an element on the spelling stack with type KIND and assign VALUE
6918 to MEMBER. */
6920 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6922 int depth = SPELLING_DEPTH (); \
6924 if (depth >= spelling_size) \
6926 spelling_size += 10; \
6927 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6928 spelling_size); \
6929 RESTORE_SPELLING_DEPTH (depth); \
6932 spelling->kind = (KIND); \
6933 spelling->MEMBER = (VALUE); \
6934 spelling++; \
6937 /* Push STRING on the stack. Printed literally. */
6939 static void
6940 push_string (const char *string)
6942 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6945 /* Push a member name on the stack. Printed as '.' STRING. */
6947 static void
6948 push_member_name (tree decl)
6950 const char *const string
6951 = (DECL_NAME (decl)
6952 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6953 : _("<anonymous>"));
6954 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6957 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6959 static void
6960 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6962 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6965 /* Compute the maximum size in bytes of the printed spelling. */
6967 static int
6968 spelling_length (void)
6970 int size = 0;
6971 struct spelling *p;
6973 for (p = spelling_base; p < spelling; p++)
6975 if (p->kind == SPELLING_BOUNDS)
6976 size += 25;
6977 else
6978 size += strlen (p->u.s) + 1;
6981 return size;
6984 /* Print the spelling to BUFFER and return it. */
6986 static char *
6987 print_spelling (char *buffer)
6989 char *d = buffer;
6990 struct spelling *p;
6992 for (p = spelling_base; p < spelling; p++)
6993 if (p->kind == SPELLING_BOUNDS)
6995 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6996 d += strlen (d);
6998 else
7000 const char *s;
7001 if (p->kind == SPELLING_MEMBER)
7002 *d++ = '.';
7003 for (s = p->u.s; (*d = *s++); d++)
7006 *d++ = '\0';
7007 return buffer;
7010 /* Digest the parser output INIT as an initializer for type TYPE.
7011 Return a C expression of type TYPE to represent the initial value.
7013 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7015 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7017 If INIT is a string constant, STRICT_STRING is true if it is
7018 unparenthesized or we should not warn here for it being parenthesized.
7019 For other types of INIT, STRICT_STRING is not used.
7021 INIT_LOC is the location of the INIT.
7023 REQUIRE_CONSTANT requests an error if non-constant initializers or
7024 elements are seen. */
7026 static tree
7027 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7028 bool null_pointer_constant, bool strict_string,
7029 int require_constant)
7031 enum tree_code code = TREE_CODE (type);
7032 tree inside_init = init;
7033 tree semantic_type = NULL_TREE;
7034 bool maybe_const = true;
7036 if (type == error_mark_node
7037 || !init
7038 || error_operand_p (init))
7039 return error_mark_node;
7041 STRIP_TYPE_NOPS (inside_init);
7043 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7045 semantic_type = TREE_TYPE (inside_init);
7046 inside_init = TREE_OPERAND (inside_init, 0);
7048 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7049 inside_init = decl_constant_value_for_optimization (inside_init);
7051 /* Initialization of an array of chars from a string constant
7052 optionally enclosed in braces. */
7054 if (code == ARRAY_TYPE && inside_init
7055 && TREE_CODE (inside_init) == STRING_CST)
7057 tree typ1
7058 = (TYPE_ATOMIC (TREE_TYPE (type))
7059 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7060 TYPE_QUAL_ATOMIC)
7061 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7062 /* Note that an array could be both an array of character type
7063 and an array of wchar_t if wchar_t is signed char or unsigned
7064 char. */
7065 bool char_array = (typ1 == char_type_node
7066 || typ1 == signed_char_type_node
7067 || typ1 == unsigned_char_type_node);
7068 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7069 bool char16_array = !!comptypes (typ1, char16_type_node);
7070 bool char32_array = !!comptypes (typ1, char32_type_node);
7072 if (char_array || wchar_array || char16_array || char32_array)
7074 struct c_expr expr;
7075 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7076 expr.value = inside_init;
7077 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7078 expr.original_type = NULL;
7079 maybe_warn_string_init (init_loc, type, expr);
7081 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7082 pedwarn_init (init_loc, OPT_Wpedantic,
7083 "initialization of a flexible array member");
7085 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7086 TYPE_MAIN_VARIANT (type)))
7087 return inside_init;
7089 if (char_array)
7091 if (typ2 != char_type_node)
7093 error_init (init_loc, "char-array initialized from wide "
7094 "string");
7095 return error_mark_node;
7098 else
7100 if (typ2 == char_type_node)
7102 error_init (init_loc, "wide character array initialized "
7103 "from non-wide string");
7104 return error_mark_node;
7106 else if (!comptypes(typ1, typ2))
7108 error_init (init_loc, "wide character array initialized "
7109 "from incompatible wide string");
7110 return error_mark_node;
7114 TREE_TYPE (inside_init) = type;
7115 if (TYPE_DOMAIN (type) != 0
7116 && TYPE_SIZE (type) != 0
7117 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7119 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7121 /* Subtract the size of a single (possibly wide) character
7122 because it's ok to ignore the terminating null char
7123 that is counted in the length of the constant. */
7124 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7125 (len
7126 - (TYPE_PRECISION (typ1)
7127 / BITS_PER_UNIT))))
7128 pedwarn_init (init_loc, 0,
7129 ("initializer-string for array of chars "
7130 "is too long"));
7131 else if (warn_cxx_compat
7132 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7133 warning_at (init_loc, OPT_Wc___compat,
7134 ("initializer-string for array chars "
7135 "is too long for C++"));
7138 return inside_init;
7140 else if (INTEGRAL_TYPE_P (typ1))
7142 error_init (init_loc, "array of inappropriate type initialized "
7143 "from string constant");
7144 return error_mark_node;
7148 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7149 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7150 below and handle as a constructor. */
7151 if (code == VECTOR_TYPE
7152 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7153 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7154 && TREE_CONSTANT (inside_init))
7156 if (TREE_CODE (inside_init) == VECTOR_CST
7157 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7158 TYPE_MAIN_VARIANT (type)))
7159 return inside_init;
7161 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7163 unsigned HOST_WIDE_INT ix;
7164 tree value;
7165 bool constant_p = true;
7167 /* Iterate through elements and check if all constructor
7168 elements are *_CSTs. */
7169 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7170 if (!CONSTANT_CLASS_P (value))
7172 constant_p = false;
7173 break;
7176 if (constant_p)
7177 return build_vector_from_ctor (type,
7178 CONSTRUCTOR_ELTS (inside_init));
7182 if (warn_sequence_point)
7183 verify_sequence_points (inside_init);
7185 /* Any type can be initialized
7186 from an expression of the same type, optionally with braces. */
7188 if (inside_init && TREE_TYPE (inside_init) != 0
7189 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7190 TYPE_MAIN_VARIANT (type))
7191 || (code == ARRAY_TYPE
7192 && comptypes (TREE_TYPE (inside_init), type))
7193 || (code == VECTOR_TYPE
7194 && comptypes (TREE_TYPE (inside_init), type))
7195 || (code == POINTER_TYPE
7196 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7197 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7198 TREE_TYPE (type)))))
7200 if (code == POINTER_TYPE)
7202 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7204 if (TREE_CODE (inside_init) == STRING_CST
7205 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7206 inside_init = array_to_pointer_conversion
7207 (init_loc, inside_init);
7208 else
7210 error_init (init_loc, "invalid use of non-lvalue array");
7211 return error_mark_node;
7216 if (code == VECTOR_TYPE)
7217 /* Although the types are compatible, we may require a
7218 conversion. */
7219 inside_init = convert (type, inside_init);
7221 if (require_constant
7222 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7224 /* As an extension, allow initializing objects with static storage
7225 duration with compound literals (which are then treated just as
7226 the brace enclosed list they contain). Also allow this for
7227 vectors, as we can only assign them with compound literals. */
7228 if (flag_isoc99 && code != VECTOR_TYPE)
7229 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7230 "is not constant");
7231 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7232 inside_init = DECL_INITIAL (decl);
7235 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7236 && TREE_CODE (inside_init) != CONSTRUCTOR)
7238 error_init (init_loc, "array initialized from non-constant array "
7239 "expression");
7240 return error_mark_node;
7243 /* Compound expressions can only occur here if -Wpedantic or
7244 -pedantic-errors is specified. In the later case, we always want
7245 an error. In the former case, we simply want a warning. */
7246 if (require_constant && pedantic
7247 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7249 inside_init
7250 = valid_compound_expr_initializer (inside_init,
7251 TREE_TYPE (inside_init));
7252 if (inside_init == error_mark_node)
7253 error_init (init_loc, "initializer element is not constant");
7254 else
7255 pedwarn_init (init_loc, OPT_Wpedantic,
7256 "initializer element is not constant");
7257 if (flag_pedantic_errors)
7258 inside_init = error_mark_node;
7260 else if (require_constant
7261 && !initializer_constant_valid_p (inside_init,
7262 TREE_TYPE (inside_init)))
7264 error_init (init_loc, "initializer element is not constant");
7265 inside_init = error_mark_node;
7267 else if (require_constant && !maybe_const)
7268 pedwarn_init (init_loc, OPT_Wpedantic,
7269 "initializer element is not a constant expression");
7271 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7272 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7273 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7274 type, inside_init, origtype,
7275 ic_init, null_pointer_constant,
7276 NULL_TREE, NULL_TREE, 0);
7277 return inside_init;
7280 /* Handle scalar types, including conversions. */
7282 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7283 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7284 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7286 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7287 && (TREE_CODE (init) == STRING_CST
7288 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7289 inside_init = init = array_to_pointer_conversion (init_loc, init);
7290 if (semantic_type)
7291 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7292 inside_init);
7293 inside_init
7294 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7295 inside_init, origtype, ic_init,
7296 null_pointer_constant, NULL_TREE, NULL_TREE,
7299 /* Check to see if we have already given an error message. */
7300 if (inside_init == error_mark_node)
7302 else if (require_constant && !TREE_CONSTANT (inside_init))
7304 error_init (init_loc, "initializer element is not constant");
7305 inside_init = error_mark_node;
7307 else if (require_constant
7308 && !initializer_constant_valid_p (inside_init,
7309 TREE_TYPE (inside_init)))
7311 error_init (init_loc, "initializer element is not computable at "
7312 "load time");
7313 inside_init = error_mark_node;
7315 else if (require_constant && !maybe_const)
7316 pedwarn_init (init_loc, OPT_Wpedantic,
7317 "initializer element is not a constant expression");
7319 return inside_init;
7322 /* Come here only for records and arrays. */
7324 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7326 error_init (init_loc, "variable-sized object may not be initialized");
7327 return error_mark_node;
7330 error_init (init_loc, "invalid initializer");
7331 return error_mark_node;
7334 /* Handle initializers that use braces. */
7336 /* Type of object we are accumulating a constructor for.
7337 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7338 static tree constructor_type;
7340 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7341 left to fill. */
7342 static tree constructor_fields;
7344 /* For an ARRAY_TYPE, this is the specified index
7345 at which to store the next element we get. */
7346 static tree constructor_index;
7348 /* For an ARRAY_TYPE, this is the maximum index. */
7349 static tree constructor_max_index;
7351 /* For a RECORD_TYPE, this is the first field not yet written out. */
7352 static tree constructor_unfilled_fields;
7354 /* For an ARRAY_TYPE, this is the index of the first element
7355 not yet written out. */
7356 static tree constructor_unfilled_index;
7358 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7359 This is so we can generate gaps between fields, when appropriate. */
7360 static tree constructor_bit_index;
7362 /* If we are saving up the elements rather than allocating them,
7363 this is the list of elements so far (in reverse order,
7364 most recent first). */
7365 static vec<constructor_elt, va_gc> *constructor_elements;
7367 /* 1 if constructor should be incrementally stored into a constructor chain,
7368 0 if all the elements should be kept in AVL tree. */
7369 static int constructor_incremental;
7371 /* 1 if so far this constructor's elements are all compile-time constants. */
7372 static int constructor_constant;
7374 /* 1 if so far this constructor's elements are all valid address constants. */
7375 static int constructor_simple;
7377 /* 1 if this constructor has an element that cannot be part of a
7378 constant expression. */
7379 static int constructor_nonconst;
7381 /* 1 if this constructor is erroneous so far. */
7382 static int constructor_erroneous;
7384 /* 1 if this constructor is the universal zero initializer { 0 }. */
7385 static int constructor_zeroinit;
7387 /* Structure for managing pending initializer elements, organized as an
7388 AVL tree. */
7390 struct init_node
7392 struct init_node *left, *right;
7393 struct init_node *parent;
7394 int balance;
7395 tree purpose;
7396 tree value;
7397 tree origtype;
7400 /* Tree of pending elements at this constructor level.
7401 These are elements encountered out of order
7402 which belong at places we haven't reached yet in actually
7403 writing the output.
7404 Will never hold tree nodes across GC runs. */
7405 static struct init_node *constructor_pending_elts;
7407 /* The SPELLING_DEPTH of this constructor. */
7408 static int constructor_depth;
7410 /* DECL node for which an initializer is being read.
7411 0 means we are reading a constructor expression
7412 such as (struct foo) {...}. */
7413 static tree constructor_decl;
7415 /* Nonzero if this is an initializer for a top-level decl. */
7416 static int constructor_top_level;
7418 /* Nonzero if there were any member designators in this initializer. */
7419 static int constructor_designated;
7421 /* Nesting depth of designator list. */
7422 static int designator_depth;
7424 /* Nonzero if there were diagnosed errors in this designator list. */
7425 static int designator_erroneous;
7428 /* This stack has a level for each implicit or explicit level of
7429 structuring in the initializer, including the outermost one. It
7430 saves the values of most of the variables above. */
7432 struct constructor_range_stack;
7434 struct constructor_stack
7436 struct constructor_stack *next;
7437 tree type;
7438 tree fields;
7439 tree index;
7440 tree max_index;
7441 tree unfilled_index;
7442 tree unfilled_fields;
7443 tree bit_index;
7444 vec<constructor_elt, va_gc> *elements;
7445 struct init_node *pending_elts;
7446 int offset;
7447 int depth;
7448 /* If value nonzero, this value should replace the entire
7449 constructor at this level. */
7450 struct c_expr replacement_value;
7451 struct constructor_range_stack *range_stack;
7452 char constant;
7453 char simple;
7454 char nonconst;
7455 char implicit;
7456 char erroneous;
7457 char outer;
7458 char incremental;
7459 char designated;
7460 int designator_depth;
7463 static struct constructor_stack *constructor_stack;
7465 /* This stack represents designators from some range designator up to
7466 the last designator in the list. */
7468 struct constructor_range_stack
7470 struct constructor_range_stack *next, *prev;
7471 struct constructor_stack *stack;
7472 tree range_start;
7473 tree index;
7474 tree range_end;
7475 tree fields;
7478 static struct constructor_range_stack *constructor_range_stack;
7480 /* This stack records separate initializers that are nested.
7481 Nested initializers can't happen in ANSI C, but GNU C allows them
7482 in cases like { ... (struct foo) { ... } ... }. */
7484 struct initializer_stack
7486 struct initializer_stack *next;
7487 tree decl;
7488 struct constructor_stack *constructor_stack;
7489 struct constructor_range_stack *constructor_range_stack;
7490 vec<constructor_elt, va_gc> *elements;
7491 struct spelling *spelling;
7492 struct spelling *spelling_base;
7493 int spelling_size;
7494 char top_level;
7495 char require_constant_value;
7496 char require_constant_elements;
7499 static struct initializer_stack *initializer_stack;
7501 /* Prepare to parse and output the initializer for variable DECL. */
7503 void
7504 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7506 const char *locus;
7507 struct initializer_stack *p = XNEW (struct initializer_stack);
7509 p->decl = constructor_decl;
7510 p->require_constant_value = require_constant_value;
7511 p->require_constant_elements = require_constant_elements;
7512 p->constructor_stack = constructor_stack;
7513 p->constructor_range_stack = constructor_range_stack;
7514 p->elements = constructor_elements;
7515 p->spelling = spelling;
7516 p->spelling_base = spelling_base;
7517 p->spelling_size = spelling_size;
7518 p->top_level = constructor_top_level;
7519 p->next = initializer_stack;
7520 initializer_stack = p;
7522 constructor_decl = decl;
7523 constructor_designated = 0;
7524 constructor_top_level = top_level;
7526 if (decl != 0 && decl != error_mark_node)
7528 require_constant_value = TREE_STATIC (decl);
7529 require_constant_elements
7530 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7531 /* For a scalar, you can always use any value to initialize,
7532 even within braces. */
7533 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7534 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7536 else
7538 require_constant_value = 0;
7539 require_constant_elements = 0;
7540 locus = _("(anonymous)");
7543 constructor_stack = 0;
7544 constructor_range_stack = 0;
7546 found_missing_braces = 0;
7548 spelling_base = 0;
7549 spelling_size = 0;
7550 RESTORE_SPELLING_DEPTH (0);
7552 if (locus)
7553 push_string (locus);
7556 void
7557 finish_init (void)
7559 struct initializer_stack *p = initializer_stack;
7561 /* Free the whole constructor stack of this initializer. */
7562 while (constructor_stack)
7564 struct constructor_stack *q = constructor_stack;
7565 constructor_stack = q->next;
7566 free (q);
7569 gcc_assert (!constructor_range_stack);
7571 /* Pop back to the data of the outer initializer (if any). */
7572 free (spelling_base);
7574 constructor_decl = p->decl;
7575 require_constant_value = p->require_constant_value;
7576 require_constant_elements = p->require_constant_elements;
7577 constructor_stack = p->constructor_stack;
7578 constructor_range_stack = p->constructor_range_stack;
7579 constructor_elements = p->elements;
7580 spelling = p->spelling;
7581 spelling_base = p->spelling_base;
7582 spelling_size = p->spelling_size;
7583 constructor_top_level = p->top_level;
7584 initializer_stack = p->next;
7585 free (p);
7588 /* Call here when we see the initializer is surrounded by braces.
7589 This is instead of a call to push_init_level;
7590 it is matched by a call to pop_init_level.
7592 TYPE is the type to initialize, for a constructor expression.
7593 For an initializer for a decl, TYPE is zero. */
7595 void
7596 really_start_incremental_init (tree type)
7598 struct constructor_stack *p = XNEW (struct constructor_stack);
7600 if (type == 0)
7601 type = TREE_TYPE (constructor_decl);
7603 if (VECTOR_TYPE_P (type)
7604 && TYPE_VECTOR_OPAQUE (type))
7605 error ("opaque vector types cannot be initialized");
7607 p->type = constructor_type;
7608 p->fields = constructor_fields;
7609 p->index = constructor_index;
7610 p->max_index = constructor_max_index;
7611 p->unfilled_index = constructor_unfilled_index;
7612 p->unfilled_fields = constructor_unfilled_fields;
7613 p->bit_index = constructor_bit_index;
7614 p->elements = constructor_elements;
7615 p->constant = constructor_constant;
7616 p->simple = constructor_simple;
7617 p->nonconst = constructor_nonconst;
7618 p->erroneous = constructor_erroneous;
7619 p->pending_elts = constructor_pending_elts;
7620 p->depth = constructor_depth;
7621 p->replacement_value.value = 0;
7622 p->replacement_value.original_code = ERROR_MARK;
7623 p->replacement_value.original_type = NULL;
7624 p->implicit = 0;
7625 p->range_stack = 0;
7626 p->outer = 0;
7627 p->incremental = constructor_incremental;
7628 p->designated = constructor_designated;
7629 p->designator_depth = designator_depth;
7630 p->next = 0;
7631 constructor_stack = p;
7633 constructor_constant = 1;
7634 constructor_simple = 1;
7635 constructor_nonconst = 0;
7636 constructor_depth = SPELLING_DEPTH ();
7637 constructor_elements = NULL;
7638 constructor_pending_elts = 0;
7639 constructor_type = type;
7640 constructor_incremental = 1;
7641 constructor_designated = 0;
7642 constructor_zeroinit = 1;
7643 designator_depth = 0;
7644 designator_erroneous = 0;
7646 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7648 constructor_fields = TYPE_FIELDS (constructor_type);
7649 /* Skip any nameless bit fields at the beginning. */
7650 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7651 && DECL_NAME (constructor_fields) == 0)
7652 constructor_fields = DECL_CHAIN (constructor_fields);
7654 constructor_unfilled_fields = constructor_fields;
7655 constructor_bit_index = bitsize_zero_node;
7657 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7659 if (TYPE_DOMAIN (constructor_type))
7661 constructor_max_index
7662 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7664 /* Detect non-empty initializations of zero-length arrays. */
7665 if (constructor_max_index == NULL_TREE
7666 && TYPE_SIZE (constructor_type))
7667 constructor_max_index = integer_minus_one_node;
7669 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7670 to initialize VLAs will cause a proper error; avoid tree
7671 checking errors as well by setting a safe value. */
7672 if (constructor_max_index
7673 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7674 constructor_max_index = integer_minus_one_node;
7676 constructor_index
7677 = convert (bitsizetype,
7678 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7680 else
7682 constructor_index = bitsize_zero_node;
7683 constructor_max_index = NULL_TREE;
7686 constructor_unfilled_index = constructor_index;
7688 else if (VECTOR_TYPE_P (constructor_type))
7690 /* Vectors are like simple fixed-size arrays. */
7691 constructor_max_index =
7692 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7693 constructor_index = bitsize_zero_node;
7694 constructor_unfilled_index = constructor_index;
7696 else
7698 /* Handle the case of int x = {5}; */
7699 constructor_fields = constructor_type;
7700 constructor_unfilled_fields = constructor_type;
7704 /* Called when we see an open brace for a nested initializer. Finish
7705 off any pending levels with implicit braces. */
7706 void
7707 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7709 while (constructor_stack->implicit)
7711 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7712 && constructor_fields == 0)
7713 process_init_element (input_location,
7714 pop_init_level (loc, 1, braced_init_obstack),
7715 true, braced_init_obstack);
7716 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7717 && constructor_max_index
7718 && tree_int_cst_lt (constructor_max_index,
7719 constructor_index))
7720 process_init_element (input_location,
7721 pop_init_level (loc, 1, braced_init_obstack),
7722 true, braced_init_obstack);
7723 else
7724 break;
7728 /* Push down into a subobject, for initialization.
7729 If this is for an explicit set of braces, IMPLICIT is 0.
7730 If it is because the next element belongs at a lower level,
7731 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7733 void
7734 push_init_level (location_t loc, int implicit,
7735 struct obstack *braced_init_obstack)
7737 struct constructor_stack *p;
7738 tree value = NULL_TREE;
7740 /* Unless this is an explicit brace, we need to preserve previous
7741 content if any. */
7742 if (implicit)
7744 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7745 value = find_init_member (constructor_fields, braced_init_obstack);
7746 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7747 value = find_init_member (constructor_index, braced_init_obstack);
7750 p = XNEW (struct constructor_stack);
7751 p->type = constructor_type;
7752 p->fields = constructor_fields;
7753 p->index = constructor_index;
7754 p->max_index = constructor_max_index;
7755 p->unfilled_index = constructor_unfilled_index;
7756 p->unfilled_fields = constructor_unfilled_fields;
7757 p->bit_index = constructor_bit_index;
7758 p->elements = constructor_elements;
7759 p->constant = constructor_constant;
7760 p->simple = constructor_simple;
7761 p->nonconst = constructor_nonconst;
7762 p->erroneous = constructor_erroneous;
7763 p->pending_elts = constructor_pending_elts;
7764 p->depth = constructor_depth;
7765 p->replacement_value.value = 0;
7766 p->replacement_value.original_code = ERROR_MARK;
7767 p->replacement_value.original_type = NULL;
7768 p->implicit = implicit;
7769 p->outer = 0;
7770 p->incremental = constructor_incremental;
7771 p->designated = constructor_designated;
7772 p->designator_depth = designator_depth;
7773 p->next = constructor_stack;
7774 p->range_stack = 0;
7775 constructor_stack = p;
7777 constructor_constant = 1;
7778 constructor_simple = 1;
7779 constructor_nonconst = 0;
7780 constructor_depth = SPELLING_DEPTH ();
7781 constructor_elements = NULL;
7782 constructor_incremental = 1;
7783 constructor_designated = 0;
7784 constructor_pending_elts = 0;
7785 if (!implicit)
7787 p->range_stack = constructor_range_stack;
7788 constructor_range_stack = 0;
7789 designator_depth = 0;
7790 designator_erroneous = 0;
7793 /* Don't die if an entire brace-pair level is superfluous
7794 in the containing level. */
7795 if (constructor_type == 0)
7797 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
7799 /* Don't die if there are extra init elts at the end. */
7800 if (constructor_fields == 0)
7801 constructor_type = 0;
7802 else
7804 constructor_type = TREE_TYPE (constructor_fields);
7805 push_member_name (constructor_fields);
7806 constructor_depth++;
7808 /* If upper initializer is designated, then mark this as
7809 designated too to prevent bogus warnings. */
7810 constructor_designated = p->designated;
7812 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7814 constructor_type = TREE_TYPE (constructor_type);
7815 push_array_bounds (tree_to_uhwi (constructor_index));
7816 constructor_depth++;
7819 if (constructor_type == 0)
7821 error_init (loc, "extra brace group at end of initializer");
7822 constructor_fields = 0;
7823 constructor_unfilled_fields = 0;
7824 return;
7827 if (value && TREE_CODE (value) == CONSTRUCTOR)
7829 constructor_constant = TREE_CONSTANT (value);
7830 constructor_simple = TREE_STATIC (value);
7831 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7832 constructor_elements = CONSTRUCTOR_ELTS (value);
7833 if (!vec_safe_is_empty (constructor_elements)
7834 && (TREE_CODE (constructor_type) == RECORD_TYPE
7835 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7836 set_nonincremental_init (braced_init_obstack);
7839 if (implicit == 1)
7840 found_missing_braces = 1;
7842 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7844 constructor_fields = TYPE_FIELDS (constructor_type);
7845 /* Skip any nameless bit fields at the beginning. */
7846 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7847 && DECL_NAME (constructor_fields) == 0)
7848 constructor_fields = DECL_CHAIN (constructor_fields);
7850 constructor_unfilled_fields = constructor_fields;
7851 constructor_bit_index = bitsize_zero_node;
7853 else if (VECTOR_TYPE_P (constructor_type))
7855 /* Vectors are like simple fixed-size arrays. */
7856 constructor_max_index =
7857 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7858 constructor_index = bitsize_int (0);
7859 constructor_unfilled_index = constructor_index;
7861 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7863 if (TYPE_DOMAIN (constructor_type))
7865 constructor_max_index
7866 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7868 /* Detect non-empty initializations of zero-length arrays. */
7869 if (constructor_max_index == NULL_TREE
7870 && TYPE_SIZE (constructor_type))
7871 constructor_max_index = integer_minus_one_node;
7873 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7874 to initialize VLAs will cause a proper error; avoid tree
7875 checking errors as well by setting a safe value. */
7876 if (constructor_max_index
7877 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7878 constructor_max_index = integer_minus_one_node;
7880 constructor_index
7881 = convert (bitsizetype,
7882 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7884 else
7885 constructor_index = bitsize_zero_node;
7887 constructor_unfilled_index = constructor_index;
7888 if (value && TREE_CODE (value) == STRING_CST)
7890 /* We need to split the char/wchar array into individual
7891 characters, so that we don't have to special case it
7892 everywhere. */
7893 set_nonincremental_init_from_string (value, braced_init_obstack);
7896 else
7898 if (constructor_type != error_mark_node)
7899 warning_init (input_location, 0, "braces around scalar initializer");
7900 constructor_fields = constructor_type;
7901 constructor_unfilled_fields = constructor_type;
7905 /* At the end of an implicit or explicit brace level,
7906 finish up that level of constructor. If a single expression
7907 with redundant braces initialized that level, return the
7908 c_expr structure for that expression. Otherwise, the original_code
7909 element is set to ERROR_MARK.
7910 If we were outputting the elements as they are read, return 0 as the value
7911 from inner levels (process_init_element ignores that),
7912 but return error_mark_node as the value from the outermost level
7913 (that's what we want to put in DECL_INITIAL).
7914 Otherwise, return a CONSTRUCTOR expression as the value. */
7916 struct c_expr
7917 pop_init_level (location_t loc, int implicit,
7918 struct obstack *braced_init_obstack)
7920 struct constructor_stack *p;
7921 struct c_expr ret;
7922 ret.value = 0;
7923 ret.original_code = ERROR_MARK;
7924 ret.original_type = NULL;
7926 if (implicit == 0)
7928 /* When we come to an explicit close brace,
7929 pop any inner levels that didn't have explicit braces. */
7930 while (constructor_stack->implicit)
7931 process_init_element (input_location,
7932 pop_init_level (loc, 1, braced_init_obstack),
7933 true, braced_init_obstack);
7934 gcc_assert (!constructor_range_stack);
7937 /* Now output all pending elements. */
7938 constructor_incremental = 1;
7939 output_pending_init_elements (1, braced_init_obstack);
7941 p = constructor_stack;
7943 /* Error for initializing a flexible array member, or a zero-length
7944 array member in an inappropriate context. */
7945 if (constructor_type && constructor_fields
7946 && TREE_CODE (constructor_type) == ARRAY_TYPE
7947 && TYPE_DOMAIN (constructor_type)
7948 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7950 /* Silently discard empty initializations. The parser will
7951 already have pedwarned for empty brackets. */
7952 if (integer_zerop (constructor_unfilled_index))
7953 constructor_type = NULL_TREE;
7954 else
7956 gcc_assert (!TYPE_SIZE (constructor_type));
7958 if (constructor_depth > 2)
7959 error_init (loc, "initialization of flexible array member in a nested context");
7960 else
7961 pedwarn_init (loc, OPT_Wpedantic,
7962 "initialization of a flexible array member");
7964 /* We have already issued an error message for the existence
7965 of a flexible array member not at the end of the structure.
7966 Discard the initializer so that we do not die later. */
7967 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7968 constructor_type = NULL_TREE;
7972 switch (vec_safe_length (constructor_elements))
7974 case 0:
7975 /* Initialization with { } counts as zeroinit. */
7976 constructor_zeroinit = 1;
7977 break;
7978 case 1:
7979 /* This might be zeroinit as well. */
7980 if (integer_zerop ((*constructor_elements)[0].value))
7981 constructor_zeroinit = 1;
7982 break;
7983 default:
7984 /* If the constructor has more than one element, it can't be { 0 }. */
7985 constructor_zeroinit = 0;
7986 break;
7989 /* Warn when some structs are initialized with direct aggregation. */
7990 if (!implicit && found_missing_braces && warn_missing_braces
7991 && !constructor_zeroinit)
7992 warning_init (loc, OPT_Wmissing_braces,
7993 "missing braces around initializer");
7995 /* Warn when some struct elements are implicitly initialized to zero. */
7996 if (warn_missing_field_initializers
7997 && constructor_type
7998 && TREE_CODE (constructor_type) == RECORD_TYPE
7999 && constructor_unfilled_fields)
8001 /* Do not warn for flexible array members or zero-length arrays. */
8002 while (constructor_unfilled_fields
8003 && (!DECL_SIZE (constructor_unfilled_fields)
8004 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8005 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8007 if (constructor_unfilled_fields
8008 /* Do not warn if this level of the initializer uses member
8009 designators; it is likely to be deliberate. */
8010 && !constructor_designated
8011 /* Do not warn about initializing with { 0 } or with { }. */
8012 && !constructor_zeroinit)
8014 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8015 "missing initializer for field %qD of %qT",
8016 constructor_unfilled_fields,
8017 constructor_type))
8018 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8019 "%qD declared here", constructor_unfilled_fields);
8023 /* Pad out the end of the structure. */
8024 if (p->replacement_value.value)
8025 /* If this closes a superfluous brace pair,
8026 just pass out the element between them. */
8027 ret = p->replacement_value;
8028 else if (constructor_type == 0)
8030 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8031 && TREE_CODE (constructor_type) != ARRAY_TYPE
8032 && !VECTOR_TYPE_P (constructor_type))
8034 /* A nonincremental scalar initializer--just return
8035 the element, after verifying there is just one. */
8036 if (vec_safe_is_empty (constructor_elements))
8038 if (!constructor_erroneous)
8039 error_init (loc, "empty scalar initializer");
8040 ret.value = error_mark_node;
8042 else if (vec_safe_length (constructor_elements) != 1)
8044 error_init (loc, "extra elements in scalar initializer");
8045 ret.value = (*constructor_elements)[0].value;
8047 else
8048 ret.value = (*constructor_elements)[0].value;
8050 else
8052 if (constructor_erroneous)
8053 ret.value = error_mark_node;
8054 else
8056 ret.value = build_constructor (constructor_type,
8057 constructor_elements);
8058 if (constructor_constant)
8059 TREE_CONSTANT (ret.value) = 1;
8060 if (constructor_constant && constructor_simple)
8061 TREE_STATIC (ret.value) = 1;
8062 if (constructor_nonconst)
8063 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8067 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8069 if (constructor_nonconst)
8070 ret.original_code = C_MAYBE_CONST_EXPR;
8071 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8072 ret.original_code = ERROR_MARK;
8075 constructor_type = p->type;
8076 constructor_fields = p->fields;
8077 constructor_index = p->index;
8078 constructor_max_index = p->max_index;
8079 constructor_unfilled_index = p->unfilled_index;
8080 constructor_unfilled_fields = p->unfilled_fields;
8081 constructor_bit_index = p->bit_index;
8082 constructor_elements = p->elements;
8083 constructor_constant = p->constant;
8084 constructor_simple = p->simple;
8085 constructor_nonconst = p->nonconst;
8086 constructor_erroneous = p->erroneous;
8087 constructor_incremental = p->incremental;
8088 constructor_designated = p->designated;
8089 designator_depth = p->designator_depth;
8090 constructor_pending_elts = p->pending_elts;
8091 constructor_depth = p->depth;
8092 if (!p->implicit)
8093 constructor_range_stack = p->range_stack;
8094 RESTORE_SPELLING_DEPTH (constructor_depth);
8096 constructor_stack = p->next;
8097 free (p);
8099 if (ret.value == 0 && constructor_stack == 0)
8100 ret.value = error_mark_node;
8101 return ret;
8104 /* Common handling for both array range and field name designators.
8105 ARRAY argument is nonzero for array ranges. Returns zero for success. */
8107 static int
8108 set_designator (location_t loc, int array,
8109 struct obstack *braced_init_obstack)
8111 tree subtype;
8112 enum tree_code subcode;
8114 /* Don't die if an entire brace-pair level is superfluous
8115 in the containing level. */
8116 if (constructor_type == 0)
8117 return 1;
8119 /* If there were errors in this designator list already, bail out
8120 silently. */
8121 if (designator_erroneous)
8122 return 1;
8124 if (!designator_depth)
8126 gcc_assert (!constructor_range_stack);
8128 /* Designator list starts at the level of closest explicit
8129 braces. */
8130 while (constructor_stack->implicit)
8131 process_init_element (input_location,
8132 pop_init_level (loc, 1, braced_init_obstack),
8133 true, braced_init_obstack);
8134 constructor_designated = 1;
8135 return 0;
8138 switch (TREE_CODE (constructor_type))
8140 case RECORD_TYPE:
8141 case UNION_TYPE:
8142 subtype = TREE_TYPE (constructor_fields);
8143 if (subtype != error_mark_node)
8144 subtype = TYPE_MAIN_VARIANT (subtype);
8145 break;
8146 case ARRAY_TYPE:
8147 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8148 break;
8149 default:
8150 gcc_unreachable ();
8153 subcode = TREE_CODE (subtype);
8154 if (array && subcode != ARRAY_TYPE)
8156 error_init (loc, "array index in non-array initializer");
8157 return 1;
8159 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8161 error_init (loc, "field name not in record or union initializer");
8162 return 1;
8165 constructor_designated = 1;
8166 finish_implicit_inits (loc, braced_init_obstack);
8167 push_init_level (loc, 2, braced_init_obstack);
8168 return 0;
8171 /* If there are range designators in designator list, push a new designator
8172 to constructor_range_stack. RANGE_END is end of such stack range or
8173 NULL_TREE if there is no range designator at this level. */
8175 static void
8176 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8178 struct constructor_range_stack *p;
8180 p = (struct constructor_range_stack *)
8181 obstack_alloc (braced_init_obstack,
8182 sizeof (struct constructor_range_stack));
8183 p->prev = constructor_range_stack;
8184 p->next = 0;
8185 p->fields = constructor_fields;
8186 p->range_start = constructor_index;
8187 p->index = constructor_index;
8188 p->stack = constructor_stack;
8189 p->range_end = range_end;
8190 if (constructor_range_stack)
8191 constructor_range_stack->next = p;
8192 constructor_range_stack = p;
8195 /* Within an array initializer, specify the next index to be initialized.
8196 FIRST is that index. If LAST is nonzero, then initialize a range
8197 of indices, running from FIRST through LAST. */
8199 void
8200 set_init_index (location_t loc, tree first, tree last,
8201 struct obstack *braced_init_obstack)
8203 if (set_designator (loc, 1, braced_init_obstack))
8204 return;
8206 designator_erroneous = 1;
8208 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8209 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8211 error_init (loc, "array index in initializer not of integer type");
8212 return;
8215 if (TREE_CODE (first) != INTEGER_CST)
8217 first = c_fully_fold (first, false, NULL);
8218 if (TREE_CODE (first) == INTEGER_CST)
8219 pedwarn_init (loc, OPT_Wpedantic,
8220 "array index in initializer is not "
8221 "an integer constant expression");
8224 if (last && TREE_CODE (last) != INTEGER_CST)
8226 last = c_fully_fold (last, false, NULL);
8227 if (TREE_CODE (last) == INTEGER_CST)
8228 pedwarn_init (loc, OPT_Wpedantic,
8229 "array index in initializer is not "
8230 "an integer constant expression");
8233 if (TREE_CODE (first) != INTEGER_CST)
8234 error_init (loc, "nonconstant array index in initializer");
8235 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
8236 error_init (loc, "nonconstant array index in initializer");
8237 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8238 error_init (loc, "array index in non-array initializer");
8239 else if (tree_int_cst_sgn (first) == -1)
8240 error_init (loc, "array index in initializer exceeds array bounds");
8241 else if (constructor_max_index
8242 && tree_int_cst_lt (constructor_max_index, first))
8243 error_init (loc, "array index in initializer exceeds array bounds");
8244 else
8246 constant_expression_warning (first);
8247 if (last)
8248 constant_expression_warning (last);
8249 constructor_index = convert (bitsizetype, first);
8250 if (tree_int_cst_lt (constructor_index, first))
8252 constructor_index = copy_node (constructor_index);
8253 TREE_OVERFLOW (constructor_index) = 1;
8256 if (last)
8258 if (tree_int_cst_equal (first, last))
8259 last = 0;
8260 else if (tree_int_cst_lt (last, first))
8262 error_init (loc, "empty index range in initializer");
8263 last = 0;
8265 else
8267 last = convert (bitsizetype, last);
8268 if (constructor_max_index != 0
8269 && tree_int_cst_lt (constructor_max_index, last))
8271 error_init (loc, "array index range in initializer exceeds "
8272 "array bounds");
8273 last = 0;
8278 designator_depth++;
8279 designator_erroneous = 0;
8280 if (constructor_range_stack || last)
8281 push_range_stack (last, braced_init_obstack);
8285 /* Within a struct initializer, specify the next field to be initialized. */
8287 void
8288 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8289 struct obstack *braced_init_obstack)
8291 tree field;
8293 if (set_designator (loc, 0, braced_init_obstack))
8294 return;
8296 designator_erroneous = 1;
8298 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8300 error_init (loc, "field name not in record or union initializer");
8301 return;
8304 field = lookup_field (constructor_type, fieldname);
8306 if (field == 0)
8308 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8309 if (guessed_id)
8311 gcc_rich_location rich_loc (fieldname_loc);
8312 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8313 error_at_rich_loc
8314 (&rich_loc,
8315 "%qT has no member named %qE; did you mean %qE?",
8316 constructor_type, fieldname, guessed_id);
8318 else
8319 error_at (fieldname_loc, "%qT has no member named %qE",
8320 constructor_type, fieldname);
8322 else
8325 constructor_fields = TREE_VALUE (field);
8326 designator_depth++;
8327 designator_erroneous = 0;
8328 if (constructor_range_stack)
8329 push_range_stack (NULL_TREE, braced_init_obstack);
8330 field = TREE_CHAIN (field);
8331 if (field)
8333 if (set_designator (loc, 0, braced_init_obstack))
8334 return;
8337 while (field != NULL_TREE);
8340 /* Add a new initializer to the tree of pending initializers. PURPOSE
8341 identifies the initializer, either array index or field in a structure.
8342 VALUE is the value of that index or field. If ORIGTYPE is not
8343 NULL_TREE, it is the original type of VALUE.
8345 IMPLICIT is true if value comes from pop_init_level (1),
8346 the new initializer has been merged with the existing one
8347 and thus no warnings should be emitted about overriding an
8348 existing initializer. */
8350 static void
8351 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8352 bool implicit, struct obstack *braced_init_obstack)
8354 struct init_node *p, **q, *r;
8356 q = &constructor_pending_elts;
8357 p = 0;
8359 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8361 while (*q != 0)
8363 p = *q;
8364 if (tree_int_cst_lt (purpose, p->purpose))
8365 q = &p->left;
8366 else if (tree_int_cst_lt (p->purpose, purpose))
8367 q = &p->right;
8368 else
8370 if (!implicit)
8372 if (TREE_SIDE_EFFECTS (p->value))
8373 warning_init (loc, OPT_Woverride_init_side_effects,
8374 "initialized field with side-effects "
8375 "overwritten");
8376 else if (warn_override_init)
8377 warning_init (loc, OPT_Woverride_init,
8378 "initialized field overwritten");
8380 p->value = value;
8381 p->origtype = origtype;
8382 return;
8386 else
8388 tree bitpos;
8390 bitpos = bit_position (purpose);
8391 while (*q != NULL)
8393 p = *q;
8394 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8395 q = &p->left;
8396 else if (p->purpose != purpose)
8397 q = &p->right;
8398 else
8400 if (!implicit)
8402 if (TREE_SIDE_EFFECTS (p->value))
8403 warning_init (loc, OPT_Woverride_init_side_effects,
8404 "initialized field with side-effects "
8405 "overwritten");
8406 else if (warn_override_init)
8407 warning_init (loc, OPT_Woverride_init,
8408 "initialized field overwritten");
8410 p->value = value;
8411 p->origtype = origtype;
8412 return;
8417 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8418 sizeof (struct init_node));
8419 r->purpose = purpose;
8420 r->value = value;
8421 r->origtype = origtype;
8423 *q = r;
8424 r->parent = p;
8425 r->left = 0;
8426 r->right = 0;
8427 r->balance = 0;
8429 while (p)
8431 struct init_node *s;
8433 if (r == p->left)
8435 if (p->balance == 0)
8436 p->balance = -1;
8437 else if (p->balance < 0)
8439 if (r->balance < 0)
8441 /* L rotation. */
8442 p->left = r->right;
8443 if (p->left)
8444 p->left->parent = p;
8445 r->right = p;
8447 p->balance = 0;
8448 r->balance = 0;
8450 s = p->parent;
8451 p->parent = r;
8452 r->parent = s;
8453 if (s)
8455 if (s->left == p)
8456 s->left = r;
8457 else
8458 s->right = r;
8460 else
8461 constructor_pending_elts = r;
8463 else
8465 /* LR rotation. */
8466 struct init_node *t = r->right;
8468 r->right = t->left;
8469 if (r->right)
8470 r->right->parent = r;
8471 t->left = r;
8473 p->left = t->right;
8474 if (p->left)
8475 p->left->parent = p;
8476 t->right = p;
8478 p->balance = t->balance < 0;
8479 r->balance = -(t->balance > 0);
8480 t->balance = 0;
8482 s = p->parent;
8483 p->parent = t;
8484 r->parent = t;
8485 t->parent = s;
8486 if (s)
8488 if (s->left == p)
8489 s->left = t;
8490 else
8491 s->right = t;
8493 else
8494 constructor_pending_elts = t;
8496 break;
8498 else
8500 /* p->balance == +1; growth of left side balances the node. */
8501 p->balance = 0;
8502 break;
8505 else /* r == p->right */
8507 if (p->balance == 0)
8508 /* Growth propagation from right side. */
8509 p->balance++;
8510 else if (p->balance > 0)
8512 if (r->balance > 0)
8514 /* R rotation. */
8515 p->right = r->left;
8516 if (p->right)
8517 p->right->parent = p;
8518 r->left = p;
8520 p->balance = 0;
8521 r->balance = 0;
8523 s = p->parent;
8524 p->parent = r;
8525 r->parent = s;
8526 if (s)
8528 if (s->left == p)
8529 s->left = r;
8530 else
8531 s->right = r;
8533 else
8534 constructor_pending_elts = r;
8536 else /* r->balance == -1 */
8538 /* RL rotation */
8539 struct init_node *t = r->left;
8541 r->left = t->right;
8542 if (r->left)
8543 r->left->parent = r;
8544 t->right = r;
8546 p->right = t->left;
8547 if (p->right)
8548 p->right->parent = p;
8549 t->left = p;
8551 r->balance = (t->balance < 0);
8552 p->balance = -(t->balance > 0);
8553 t->balance = 0;
8555 s = p->parent;
8556 p->parent = t;
8557 r->parent = t;
8558 t->parent = s;
8559 if (s)
8561 if (s->left == p)
8562 s->left = t;
8563 else
8564 s->right = t;
8566 else
8567 constructor_pending_elts = t;
8569 break;
8571 else
8573 /* p->balance == -1; growth of right side balances the node. */
8574 p->balance = 0;
8575 break;
8579 r = p;
8580 p = p->parent;
8584 /* Build AVL tree from a sorted chain. */
8586 static void
8587 set_nonincremental_init (struct obstack * braced_init_obstack)
8589 unsigned HOST_WIDE_INT ix;
8590 tree index, value;
8592 if (TREE_CODE (constructor_type) != RECORD_TYPE
8593 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8594 return;
8596 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8597 add_pending_init (input_location, index, value, NULL_TREE, true,
8598 braced_init_obstack);
8599 constructor_elements = NULL;
8600 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8602 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8603 /* Skip any nameless bit fields at the beginning. */
8604 while (constructor_unfilled_fields != 0
8605 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8606 && DECL_NAME (constructor_unfilled_fields) == 0)
8607 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8610 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8612 if (TYPE_DOMAIN (constructor_type))
8613 constructor_unfilled_index
8614 = convert (bitsizetype,
8615 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8616 else
8617 constructor_unfilled_index = bitsize_zero_node;
8619 constructor_incremental = 0;
8622 /* Build AVL tree from a string constant. */
8624 static void
8625 set_nonincremental_init_from_string (tree str,
8626 struct obstack * braced_init_obstack)
8628 tree value, purpose, type;
8629 HOST_WIDE_INT val[2];
8630 const char *p, *end;
8631 int byte, wchar_bytes, charwidth, bitpos;
8633 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8635 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8636 charwidth = TYPE_PRECISION (char_type_node);
8637 gcc_assert ((size_t) wchar_bytes * charwidth
8638 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
8639 type = TREE_TYPE (constructor_type);
8640 p = TREE_STRING_POINTER (str);
8641 end = p + TREE_STRING_LENGTH (str);
8643 for (purpose = bitsize_zero_node;
8644 p < end
8645 && !(constructor_max_index
8646 && tree_int_cst_lt (constructor_max_index, purpose));
8647 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8649 if (wchar_bytes == 1)
8651 val[0] = (unsigned char) *p++;
8652 val[1] = 0;
8654 else
8656 val[1] = 0;
8657 val[0] = 0;
8658 for (byte = 0; byte < wchar_bytes; byte++)
8660 if (BYTES_BIG_ENDIAN)
8661 bitpos = (wchar_bytes - byte - 1) * charwidth;
8662 else
8663 bitpos = byte * charwidth;
8664 val[bitpos / HOST_BITS_PER_WIDE_INT]
8665 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8666 << (bitpos % HOST_BITS_PER_WIDE_INT);
8670 if (!TYPE_UNSIGNED (type))
8672 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8673 if (bitpos < HOST_BITS_PER_WIDE_INT)
8675 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
8677 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8678 val[1] = -1;
8681 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8683 if (val[0] < 0)
8684 val[1] = -1;
8686 else if (val[1] & (HOST_WIDE_INT_1
8687 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8688 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8691 value = wide_int_to_tree (type,
8692 wide_int::from_array (val, 2,
8693 HOST_BITS_PER_WIDE_INT * 2));
8694 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8695 braced_init_obstack);
8698 constructor_incremental = 0;
8701 /* Return value of FIELD in pending initializer or zero if the field was
8702 not initialized yet. */
8704 static tree
8705 find_init_member (tree field, struct obstack * braced_init_obstack)
8707 struct init_node *p;
8709 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8711 if (constructor_incremental
8712 && tree_int_cst_lt (field, constructor_unfilled_index))
8713 set_nonincremental_init (braced_init_obstack);
8715 p = constructor_pending_elts;
8716 while (p)
8718 if (tree_int_cst_lt (field, p->purpose))
8719 p = p->left;
8720 else if (tree_int_cst_lt (p->purpose, field))
8721 p = p->right;
8722 else
8723 return p->value;
8726 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8728 tree bitpos = bit_position (field);
8730 if (constructor_incremental
8731 && (!constructor_unfilled_fields
8732 || tree_int_cst_lt (bitpos,
8733 bit_position (constructor_unfilled_fields))))
8734 set_nonincremental_init (braced_init_obstack);
8736 p = constructor_pending_elts;
8737 while (p)
8739 if (field == p->purpose)
8740 return p->value;
8741 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8742 p = p->left;
8743 else
8744 p = p->right;
8747 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8749 if (!vec_safe_is_empty (constructor_elements)
8750 && (constructor_elements->last ().index == field))
8751 return constructor_elements->last ().value;
8753 return 0;
8756 /* "Output" the next constructor element.
8757 At top level, really output it to assembler code now.
8758 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8759 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8760 TYPE is the data type that the containing data type wants here.
8761 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8762 If VALUE is a string constant, STRICT_STRING is true if it is
8763 unparenthesized or we should not warn here for it being parenthesized.
8764 For other types of VALUE, STRICT_STRING is not used.
8766 PENDING if non-nil means output pending elements that belong
8767 right after this element. (PENDING is normally 1;
8768 it is 0 while outputting pending elements, to avoid recursion.)
8770 IMPLICIT is true if value comes from pop_init_level (1),
8771 the new initializer has been merged with the existing one
8772 and thus no warnings should be emitted about overriding an
8773 existing initializer. */
8775 static void
8776 output_init_element (location_t loc, tree value, tree origtype,
8777 bool strict_string, tree type, tree field, int pending,
8778 bool implicit, struct obstack * braced_init_obstack)
8780 tree semantic_type = NULL_TREE;
8781 bool maybe_const = true;
8782 bool npc;
8784 if (type == error_mark_node || value == error_mark_node)
8786 constructor_erroneous = 1;
8787 return;
8789 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8790 && (TREE_CODE (value) == STRING_CST
8791 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8792 && !(TREE_CODE (value) == STRING_CST
8793 && TREE_CODE (type) == ARRAY_TYPE
8794 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8795 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8796 TYPE_MAIN_VARIANT (type)))
8797 value = array_to_pointer_conversion (input_location, value);
8799 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8800 && require_constant_value && pending)
8802 /* As an extension, allow initializing objects with static storage
8803 duration with compound literals (which are then treated just as
8804 the brace enclosed list they contain). */
8805 if (flag_isoc99)
8806 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8807 "constant");
8808 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8809 value = DECL_INITIAL (decl);
8812 npc = null_pointer_constant_p (value);
8813 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8815 semantic_type = TREE_TYPE (value);
8816 value = TREE_OPERAND (value, 0);
8818 value = c_fully_fold (value, require_constant_value, &maybe_const);
8820 if (value == error_mark_node)
8821 constructor_erroneous = 1;
8822 else if (!TREE_CONSTANT (value))
8823 constructor_constant = 0;
8824 else if (!initializer_constant_valid_p (value,
8825 TREE_TYPE (value),
8826 AGGREGATE_TYPE_P (constructor_type)
8827 && TYPE_REVERSE_STORAGE_ORDER
8828 (constructor_type))
8829 || (RECORD_OR_UNION_TYPE_P (constructor_type)
8830 && DECL_C_BIT_FIELD (field)
8831 && TREE_CODE (value) != INTEGER_CST))
8832 constructor_simple = 0;
8833 if (!maybe_const)
8834 constructor_nonconst = 1;
8836 /* Digest the initializer and issue any errors about incompatible
8837 types before issuing errors about non-constant initializers. */
8838 tree new_value = value;
8839 if (semantic_type)
8840 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8841 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
8842 require_constant_value);
8843 if (new_value == error_mark_node)
8845 constructor_erroneous = 1;
8846 return;
8848 if (require_constant_value || require_constant_elements)
8849 constant_expression_warning (new_value);
8851 /* Proceed to check the constness of the original initializer. */
8852 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8854 if (require_constant_value)
8856 error_init (loc, "initializer element is not constant");
8857 value = error_mark_node;
8859 else if (require_constant_elements)
8860 pedwarn (loc, OPT_Wpedantic,
8861 "initializer element is not computable at load time");
8863 else if (!maybe_const
8864 && (require_constant_value || require_constant_elements))
8865 pedwarn_init (loc, OPT_Wpedantic,
8866 "initializer element is not a constant expression");
8868 /* Issue -Wc++-compat warnings about initializing a bitfield with
8869 enum type. */
8870 if (warn_cxx_compat
8871 && field != NULL_TREE
8872 && TREE_CODE (field) == FIELD_DECL
8873 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8874 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8875 != TYPE_MAIN_VARIANT (type))
8876 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8878 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8879 if (checktype != error_mark_node
8880 && (TYPE_MAIN_VARIANT (checktype)
8881 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8882 warning_init (loc, OPT_Wc___compat,
8883 "enum conversion in initialization is invalid in C++");
8886 /* If this field is empty (and not at the end of structure),
8887 don't do anything other than checking the initializer. */
8888 if (field
8889 && (TREE_TYPE (field) == error_mark_node
8890 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8891 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8892 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8893 || DECL_CHAIN (field)))))
8894 return;
8896 /* Finally, set VALUE to the initializer value digested above. */
8897 value = new_value;
8899 /* If this element doesn't come next in sequence,
8900 put it on constructor_pending_elts. */
8901 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8902 && (!constructor_incremental
8903 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8905 if (constructor_incremental
8906 && tree_int_cst_lt (field, constructor_unfilled_index))
8907 set_nonincremental_init (braced_init_obstack);
8909 add_pending_init (loc, field, value, origtype, implicit,
8910 braced_init_obstack);
8911 return;
8913 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8914 && (!constructor_incremental
8915 || field != constructor_unfilled_fields))
8917 /* We do this for records but not for unions. In a union,
8918 no matter which field is specified, it can be initialized
8919 right away since it starts at the beginning of the union. */
8920 if (constructor_incremental)
8922 if (!constructor_unfilled_fields)
8923 set_nonincremental_init (braced_init_obstack);
8924 else
8926 tree bitpos, unfillpos;
8928 bitpos = bit_position (field);
8929 unfillpos = bit_position (constructor_unfilled_fields);
8931 if (tree_int_cst_lt (bitpos, unfillpos))
8932 set_nonincremental_init (braced_init_obstack);
8936 add_pending_init (loc, field, value, origtype, implicit,
8937 braced_init_obstack);
8938 return;
8940 else if (TREE_CODE (constructor_type) == UNION_TYPE
8941 && !vec_safe_is_empty (constructor_elements))
8943 if (!implicit)
8945 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8946 warning_init (loc, OPT_Woverride_init_side_effects,
8947 "initialized field with side-effects overwritten");
8948 else if (warn_override_init)
8949 warning_init (loc, OPT_Woverride_init,
8950 "initialized field overwritten");
8953 /* We can have just one union field set. */
8954 constructor_elements = NULL;
8957 /* Otherwise, output this element either to
8958 constructor_elements or to the assembler file. */
8960 constructor_elt celt = {field, value};
8961 vec_safe_push (constructor_elements, celt);
8963 /* Advance the variable that indicates sequential elements output. */
8964 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8965 constructor_unfilled_index
8966 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8967 bitsize_one_node);
8968 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8970 constructor_unfilled_fields
8971 = DECL_CHAIN (constructor_unfilled_fields);
8973 /* Skip any nameless bit fields. */
8974 while (constructor_unfilled_fields != 0
8975 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8976 && DECL_NAME (constructor_unfilled_fields) == 0)
8977 constructor_unfilled_fields =
8978 DECL_CHAIN (constructor_unfilled_fields);
8980 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8981 constructor_unfilled_fields = 0;
8983 /* Now output any pending elements which have become next. */
8984 if (pending)
8985 output_pending_init_elements (0, braced_init_obstack);
8988 /* Output any pending elements which have become next.
8989 As we output elements, constructor_unfilled_{fields,index}
8990 advances, which may cause other elements to become next;
8991 if so, they too are output.
8993 If ALL is 0, we return when there are
8994 no more pending elements to output now.
8996 If ALL is 1, we output space as necessary so that
8997 we can output all the pending elements. */
8998 static void
8999 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9001 struct init_node *elt = constructor_pending_elts;
9002 tree next;
9004 retry:
9006 /* Look through the whole pending tree.
9007 If we find an element that should be output now,
9008 output it. Otherwise, set NEXT to the element
9009 that comes first among those still pending. */
9011 next = 0;
9012 while (elt)
9014 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9016 if (tree_int_cst_equal (elt->purpose,
9017 constructor_unfilled_index))
9018 output_init_element (input_location, elt->value, elt->origtype,
9019 true, TREE_TYPE (constructor_type),
9020 constructor_unfilled_index, 0, false,
9021 braced_init_obstack);
9022 else if (tree_int_cst_lt (constructor_unfilled_index,
9023 elt->purpose))
9025 /* Advance to the next smaller node. */
9026 if (elt->left)
9027 elt = elt->left;
9028 else
9030 /* We have reached the smallest node bigger than the
9031 current unfilled index. Fill the space first. */
9032 next = elt->purpose;
9033 break;
9036 else
9038 /* Advance to the next bigger node. */
9039 if (elt->right)
9040 elt = elt->right;
9041 else
9043 /* We have reached the biggest node in a subtree. Find
9044 the parent of it, which is the next bigger node. */
9045 while (elt->parent && elt->parent->right == elt)
9046 elt = elt->parent;
9047 elt = elt->parent;
9048 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9049 elt->purpose))
9051 next = elt->purpose;
9052 break;
9057 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9059 tree ctor_unfilled_bitpos, elt_bitpos;
9061 /* If the current record is complete we are done. */
9062 if (constructor_unfilled_fields == 0)
9063 break;
9065 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9066 elt_bitpos = bit_position (elt->purpose);
9067 /* We can't compare fields here because there might be empty
9068 fields in between. */
9069 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9071 constructor_unfilled_fields = elt->purpose;
9072 output_init_element (input_location, elt->value, elt->origtype,
9073 true, TREE_TYPE (elt->purpose),
9074 elt->purpose, 0, false,
9075 braced_init_obstack);
9077 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9079 /* Advance to the next smaller node. */
9080 if (elt->left)
9081 elt = elt->left;
9082 else
9084 /* We have reached the smallest node bigger than the
9085 current unfilled field. Fill the space first. */
9086 next = elt->purpose;
9087 break;
9090 else
9092 /* Advance to the next bigger node. */
9093 if (elt->right)
9094 elt = elt->right;
9095 else
9097 /* We have reached the biggest node in a subtree. Find
9098 the parent of it, which is the next bigger node. */
9099 while (elt->parent && elt->parent->right == elt)
9100 elt = elt->parent;
9101 elt = elt->parent;
9102 if (elt
9103 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9104 bit_position (elt->purpose))))
9106 next = elt->purpose;
9107 break;
9114 /* Ordinarily return, but not if we want to output all
9115 and there are elements left. */
9116 if (!(all && next != 0))
9117 return;
9119 /* If it's not incremental, just skip over the gap, so that after
9120 jumping to retry we will output the next successive element. */
9121 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9122 constructor_unfilled_fields = next;
9123 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9124 constructor_unfilled_index = next;
9126 /* ELT now points to the node in the pending tree with the next
9127 initializer to output. */
9128 goto retry;
9131 /* Add one non-braced element to the current constructor level.
9132 This adjusts the current position within the constructor's type.
9133 This may also start or terminate implicit levels
9134 to handle a partly-braced initializer.
9136 Once this has found the correct level for the new element,
9137 it calls output_init_element.
9139 IMPLICIT is true if value comes from pop_init_level (1),
9140 the new initializer has been merged with the existing one
9141 and thus no warnings should be emitted about overriding an
9142 existing initializer. */
9144 void
9145 process_init_element (location_t loc, struct c_expr value, bool implicit,
9146 struct obstack * braced_init_obstack)
9148 tree orig_value = value.value;
9149 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
9150 bool strict_string = value.original_code == STRING_CST;
9151 bool was_designated = designator_depth != 0;
9153 designator_depth = 0;
9154 designator_erroneous = 0;
9156 if (!implicit && value.value && !integer_zerop (value.value))
9157 constructor_zeroinit = 0;
9159 /* Handle superfluous braces around string cst as in
9160 char x[] = {"foo"}; */
9161 if (string_flag
9162 && constructor_type
9163 && !was_designated
9164 && TREE_CODE (constructor_type) == ARRAY_TYPE
9165 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9166 && integer_zerop (constructor_unfilled_index))
9168 if (constructor_stack->replacement_value.value)
9169 error_init (loc, "excess elements in char array initializer");
9170 constructor_stack->replacement_value = value;
9171 return;
9174 if (constructor_stack->replacement_value.value != 0)
9176 error_init (loc, "excess elements in struct initializer");
9177 return;
9180 /* Ignore elements of a brace group if it is entirely superfluous
9181 and has already been diagnosed. */
9182 if (constructor_type == 0)
9183 return;
9185 if (!implicit && warn_designated_init && !was_designated
9186 && TREE_CODE (constructor_type) == RECORD_TYPE
9187 && lookup_attribute ("designated_init",
9188 TYPE_ATTRIBUTES (constructor_type)))
9189 warning_init (loc,
9190 OPT_Wdesignated_init,
9191 "positional initialization of field "
9192 "in %<struct%> declared with %<designated_init%> attribute");
9194 /* If we've exhausted any levels that didn't have braces,
9195 pop them now. */
9196 while (constructor_stack->implicit)
9198 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9199 && constructor_fields == 0)
9200 process_init_element (loc,
9201 pop_init_level (loc, 1, braced_init_obstack),
9202 true, braced_init_obstack);
9203 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9204 || VECTOR_TYPE_P (constructor_type))
9205 && constructor_max_index
9206 && tree_int_cst_lt (constructor_max_index,
9207 constructor_index))
9208 process_init_element (loc,
9209 pop_init_level (loc, 1, braced_init_obstack),
9210 true, braced_init_obstack);
9211 else
9212 break;
9215 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9216 if (constructor_range_stack)
9218 /* If value is a compound literal and we'll be just using its
9219 content, don't put it into a SAVE_EXPR. */
9220 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9221 || !require_constant_value)
9223 tree semantic_type = NULL_TREE;
9224 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9226 semantic_type = TREE_TYPE (value.value);
9227 value.value = TREE_OPERAND (value.value, 0);
9229 value.value = c_save_expr (value.value);
9230 if (semantic_type)
9231 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9232 value.value);
9236 while (1)
9238 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9240 tree fieldtype;
9241 enum tree_code fieldcode;
9243 if (constructor_fields == 0)
9245 pedwarn_init (loc, 0, "excess elements in struct initializer");
9246 break;
9249 fieldtype = TREE_TYPE (constructor_fields);
9250 if (fieldtype != error_mark_node)
9251 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9252 fieldcode = TREE_CODE (fieldtype);
9254 /* Error for non-static initialization of a flexible array member. */
9255 if (fieldcode == ARRAY_TYPE
9256 && !require_constant_value
9257 && TYPE_SIZE (fieldtype) == NULL_TREE
9258 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9260 error_init (loc, "non-static initialization of a flexible "
9261 "array member");
9262 break;
9265 /* Error for initialization of a flexible array member with
9266 a string constant if the structure is in an array. E.g.:
9267 struct S { int x; char y[]; };
9268 struct S s[] = { { 1, "foo" } };
9269 is invalid. */
9270 if (string_flag
9271 && fieldcode == ARRAY_TYPE
9272 && constructor_depth > 1
9273 && TYPE_SIZE (fieldtype) == NULL_TREE
9274 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9276 bool in_array_p = false;
9277 for (struct constructor_stack *p = constructor_stack;
9278 p && p->type; p = p->next)
9279 if (TREE_CODE (p->type) == ARRAY_TYPE)
9281 in_array_p = true;
9282 break;
9284 if (in_array_p)
9286 error_init (loc, "initialization of flexible array "
9287 "member in a nested context");
9288 break;
9292 /* Accept a string constant to initialize a subarray. */
9293 if (value.value != 0
9294 && fieldcode == ARRAY_TYPE
9295 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9296 && string_flag)
9297 value.value = orig_value;
9298 /* Otherwise, if we have come to a subaggregate,
9299 and we don't have an element of its type, push into it. */
9300 else if (value.value != 0
9301 && value.value != error_mark_node
9302 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9303 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9304 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9306 push_init_level (loc, 1, braced_init_obstack);
9307 continue;
9310 if (value.value)
9312 push_member_name (constructor_fields);
9313 output_init_element (loc, value.value, value.original_type,
9314 strict_string, fieldtype,
9315 constructor_fields, 1, implicit,
9316 braced_init_obstack);
9317 RESTORE_SPELLING_DEPTH (constructor_depth);
9319 else
9320 /* Do the bookkeeping for an element that was
9321 directly output as a constructor. */
9323 /* For a record, keep track of end position of last field. */
9324 if (DECL_SIZE (constructor_fields))
9325 constructor_bit_index
9326 = size_binop_loc (input_location, PLUS_EXPR,
9327 bit_position (constructor_fields),
9328 DECL_SIZE (constructor_fields));
9330 /* If the current field was the first one not yet written out,
9331 it isn't now, so update. */
9332 if (constructor_unfilled_fields == constructor_fields)
9334 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9335 /* Skip any nameless bit fields. */
9336 while (constructor_unfilled_fields != 0
9337 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9338 && DECL_NAME (constructor_unfilled_fields) == 0)
9339 constructor_unfilled_fields =
9340 DECL_CHAIN (constructor_unfilled_fields);
9344 constructor_fields = DECL_CHAIN (constructor_fields);
9345 /* Skip any nameless bit fields at the beginning. */
9346 while (constructor_fields != 0
9347 && DECL_C_BIT_FIELD (constructor_fields)
9348 && DECL_NAME (constructor_fields) == 0)
9349 constructor_fields = DECL_CHAIN (constructor_fields);
9351 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9353 tree fieldtype;
9354 enum tree_code fieldcode;
9356 if (constructor_fields == 0)
9358 pedwarn_init (loc, 0,
9359 "excess elements in union initializer");
9360 break;
9363 fieldtype = TREE_TYPE (constructor_fields);
9364 if (fieldtype != error_mark_node)
9365 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9366 fieldcode = TREE_CODE (fieldtype);
9368 /* Warn that traditional C rejects initialization of unions.
9369 We skip the warning if the value is zero. This is done
9370 under the assumption that the zero initializer in user
9371 code appears conditioned on e.g. __STDC__ to avoid
9372 "missing initializer" warnings and relies on default
9373 initialization to zero in the traditional C case.
9374 We also skip the warning if the initializer is designated,
9375 again on the assumption that this must be conditional on
9376 __STDC__ anyway (and we've already complained about the
9377 member-designator already). */
9378 if (!in_system_header_at (input_location) && !constructor_designated
9379 && !(value.value && (integer_zerop (value.value)
9380 || real_zerop (value.value))))
9381 warning (OPT_Wtraditional, "traditional C rejects initialization "
9382 "of unions");
9384 /* Accept a string constant to initialize a subarray. */
9385 if (value.value != 0
9386 && fieldcode == ARRAY_TYPE
9387 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9388 && string_flag)
9389 value.value = orig_value;
9390 /* Otherwise, if we have come to a subaggregate,
9391 and we don't have an element of its type, push into it. */
9392 else if (value.value != 0
9393 && value.value != error_mark_node
9394 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9395 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9396 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9398 push_init_level (loc, 1, braced_init_obstack);
9399 continue;
9402 if (value.value)
9404 push_member_name (constructor_fields);
9405 output_init_element (loc, value.value, value.original_type,
9406 strict_string, fieldtype,
9407 constructor_fields, 1, implicit,
9408 braced_init_obstack);
9409 RESTORE_SPELLING_DEPTH (constructor_depth);
9411 else
9412 /* Do the bookkeeping for an element that was
9413 directly output as a constructor. */
9415 constructor_bit_index = DECL_SIZE (constructor_fields);
9416 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9419 constructor_fields = 0;
9421 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9423 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9424 enum tree_code eltcode = TREE_CODE (elttype);
9426 /* Accept a string constant to initialize a subarray. */
9427 if (value.value != 0
9428 && eltcode == ARRAY_TYPE
9429 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9430 && string_flag)
9431 value.value = orig_value;
9432 /* Otherwise, if we have come to a subaggregate,
9433 and we don't have an element of its type, push into it. */
9434 else if (value.value != 0
9435 && value.value != error_mark_node
9436 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9437 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9438 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9440 push_init_level (loc, 1, braced_init_obstack);
9441 continue;
9444 if (constructor_max_index != 0
9445 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9446 || integer_all_onesp (constructor_max_index)))
9448 pedwarn_init (loc, 0,
9449 "excess elements in array initializer");
9450 break;
9453 /* Now output the actual element. */
9454 if (value.value)
9456 push_array_bounds (tree_to_uhwi (constructor_index));
9457 output_init_element (loc, value.value, value.original_type,
9458 strict_string, elttype,
9459 constructor_index, 1, implicit,
9460 braced_init_obstack);
9461 RESTORE_SPELLING_DEPTH (constructor_depth);
9464 constructor_index
9465 = size_binop_loc (input_location, PLUS_EXPR,
9466 constructor_index, bitsize_one_node);
9468 if (!value.value)
9469 /* If we are doing the bookkeeping for an element that was
9470 directly output as a constructor, we must update
9471 constructor_unfilled_index. */
9472 constructor_unfilled_index = constructor_index;
9474 else if (VECTOR_TYPE_P (constructor_type))
9476 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9478 /* Do a basic check of initializer size. Note that vectors
9479 always have a fixed size derived from their type. */
9480 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9482 pedwarn_init (loc, 0,
9483 "excess elements in vector initializer");
9484 break;
9487 /* Now output the actual element. */
9488 if (value.value)
9490 if (TREE_CODE (value.value) == VECTOR_CST)
9491 elttype = TYPE_MAIN_VARIANT (constructor_type);
9492 output_init_element (loc, value.value, value.original_type,
9493 strict_string, elttype,
9494 constructor_index, 1, implicit,
9495 braced_init_obstack);
9498 constructor_index
9499 = size_binop_loc (input_location,
9500 PLUS_EXPR, constructor_index, bitsize_one_node);
9502 if (!value.value)
9503 /* If we are doing the bookkeeping for an element that was
9504 directly output as a constructor, we must update
9505 constructor_unfilled_index. */
9506 constructor_unfilled_index = constructor_index;
9509 /* Handle the sole element allowed in a braced initializer
9510 for a scalar variable. */
9511 else if (constructor_type != error_mark_node
9512 && constructor_fields == 0)
9514 pedwarn_init (loc, 0,
9515 "excess elements in scalar initializer");
9516 break;
9518 else
9520 if (value.value)
9521 output_init_element (loc, value.value, value.original_type,
9522 strict_string, constructor_type,
9523 NULL_TREE, 1, implicit,
9524 braced_init_obstack);
9525 constructor_fields = 0;
9528 /* Handle range initializers either at this level or anywhere higher
9529 in the designator stack. */
9530 if (constructor_range_stack)
9532 struct constructor_range_stack *p, *range_stack;
9533 int finish = 0;
9535 range_stack = constructor_range_stack;
9536 constructor_range_stack = 0;
9537 while (constructor_stack != range_stack->stack)
9539 gcc_assert (constructor_stack->implicit);
9540 process_init_element (loc,
9541 pop_init_level (loc, 1,
9542 braced_init_obstack),
9543 true, braced_init_obstack);
9545 for (p = range_stack;
9546 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9547 p = p->prev)
9549 gcc_assert (constructor_stack->implicit);
9550 process_init_element (loc,
9551 pop_init_level (loc, 1,
9552 braced_init_obstack),
9553 true, braced_init_obstack);
9556 p->index = size_binop_loc (input_location,
9557 PLUS_EXPR, p->index, bitsize_one_node);
9558 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9559 finish = 1;
9561 while (1)
9563 constructor_index = p->index;
9564 constructor_fields = p->fields;
9565 if (finish && p->range_end && p->index == p->range_start)
9567 finish = 0;
9568 p->prev = 0;
9570 p = p->next;
9571 if (!p)
9572 break;
9573 finish_implicit_inits (loc, braced_init_obstack);
9574 push_init_level (loc, 2, braced_init_obstack);
9575 p->stack = constructor_stack;
9576 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9577 p->index = p->range_start;
9580 if (!finish)
9581 constructor_range_stack = range_stack;
9582 continue;
9585 break;
9588 constructor_range_stack = 0;
9591 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9592 (guaranteed to be 'volatile' or null) and ARGS (represented using
9593 an ASM_EXPR node). */
9594 tree
9595 build_asm_stmt (tree cv_qualifier, tree args)
9597 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9598 ASM_VOLATILE_P (args) = 1;
9599 return add_stmt (args);
9602 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9603 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9604 SIMPLE indicates whether there was anything at all after the
9605 string in the asm expression -- asm("blah") and asm("blah" : )
9606 are subtly different. We use a ASM_EXPR node to represent this. */
9607 tree
9608 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9609 tree clobbers, tree labels, bool simple)
9611 tree tail;
9612 tree args;
9613 int i;
9614 const char *constraint;
9615 const char **oconstraints;
9616 bool allows_mem, allows_reg, is_inout;
9617 int ninputs, noutputs;
9619 ninputs = list_length (inputs);
9620 noutputs = list_length (outputs);
9621 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9623 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9625 /* Remove output conversions that change the type but not the mode. */
9626 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9628 tree output = TREE_VALUE (tail);
9630 output = c_fully_fold (output, false, NULL);
9632 /* ??? Really, this should not be here. Users should be using a
9633 proper lvalue, dammit. But there's a long history of using casts
9634 in the output operands. In cases like longlong.h, this becomes a
9635 primitive form of typechecking -- if the cast can be removed, then
9636 the output operand had a type of the proper width; otherwise we'll
9637 get an error. Gross, but ... */
9638 STRIP_NOPS (output);
9640 if (!lvalue_or_else (loc, output, lv_asm))
9641 output = error_mark_node;
9643 if (output != error_mark_node
9644 && (TREE_READONLY (output)
9645 || TYPE_READONLY (TREE_TYPE (output))
9646 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9647 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9648 readonly_error (loc, output, lv_asm);
9650 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9651 oconstraints[i] = constraint;
9653 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9654 &allows_mem, &allows_reg, &is_inout))
9656 /* If the operand is going to end up in memory,
9657 mark it addressable. */
9658 if (!allows_reg && !c_mark_addressable (output))
9659 output = error_mark_node;
9660 if (!(!allows_reg && allows_mem)
9661 && output != error_mark_node
9662 && VOID_TYPE_P (TREE_TYPE (output)))
9664 error_at (loc, "invalid use of void expression");
9665 output = error_mark_node;
9668 else
9669 output = error_mark_node;
9671 TREE_VALUE (tail) = output;
9674 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9676 tree input;
9678 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9679 input = TREE_VALUE (tail);
9681 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9682 oconstraints, &allows_mem, &allows_reg))
9684 /* If the operand is going to end up in memory,
9685 mark it addressable. */
9686 if (!allows_reg && allows_mem)
9688 input = c_fully_fold (input, false, NULL);
9690 /* Strip the nops as we allow this case. FIXME, this really
9691 should be rejected or made deprecated. */
9692 STRIP_NOPS (input);
9693 if (!c_mark_addressable (input))
9694 input = error_mark_node;
9696 else
9698 struct c_expr expr;
9699 memset (&expr, 0, sizeof (expr));
9700 expr.value = input;
9701 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9702 input = c_fully_fold (expr.value, false, NULL);
9704 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9706 error_at (loc, "invalid use of void expression");
9707 input = error_mark_node;
9711 else
9712 input = error_mark_node;
9714 TREE_VALUE (tail) = input;
9717 /* ASMs with labels cannot have outputs. This should have been
9718 enforced by the parser. */
9719 gcc_assert (outputs == NULL || labels == NULL);
9721 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9723 /* asm statements without outputs, including simple ones, are treated
9724 as volatile. */
9725 ASM_INPUT_P (args) = simple;
9726 ASM_VOLATILE_P (args) = (noutputs == 0);
9728 return args;
9731 /* Generate a goto statement to LABEL. LOC is the location of the
9732 GOTO. */
9734 tree
9735 c_finish_goto_label (location_t loc, tree label)
9737 tree decl = lookup_label_for_goto (loc, label);
9738 if (!decl)
9739 return NULL_TREE;
9740 TREE_USED (decl) = 1;
9742 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9743 SET_EXPR_LOCATION (t, loc);
9744 return add_stmt (t);
9748 /* Generate a computed goto statement to EXPR. LOC is the location of
9749 the GOTO. */
9751 tree
9752 c_finish_goto_ptr (location_t loc, tree expr)
9754 tree t;
9755 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9756 expr = c_fully_fold (expr, false, NULL);
9757 expr = convert (ptr_type_node, expr);
9758 t = build1 (GOTO_EXPR, void_type_node, expr);
9759 SET_EXPR_LOCATION (t, loc);
9760 return add_stmt (t);
9763 /* Generate a C `return' statement. RETVAL is the expression for what
9764 to return, or a null pointer for `return;' with no value. LOC is
9765 the location of the return statement, or the location of the expression,
9766 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9767 is the original type of RETVAL. */
9769 tree
9770 c_finish_return (location_t loc, tree retval, tree origtype)
9772 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9773 bool no_warning = false;
9774 bool npc = false;
9775 size_t rank = 0;
9777 /* Use the expansion point to handle cases such as returning NULL
9778 in a function returning void. */
9779 source_location xloc = expansion_point_location_if_in_system_header (loc);
9781 if (TREE_THIS_VOLATILE (current_function_decl))
9782 warning_at (xloc, 0,
9783 "function declared %<noreturn%> has a %<return%> statement");
9785 if (flag_cilkplus && contains_array_notation_expr (retval))
9787 /* Array notations are allowed in a return statement if it is inside a
9788 built-in array notation reduction function. */
9789 if (!find_rank (loc, retval, retval, false, &rank))
9790 return error_mark_node;
9791 if (rank >= 1)
9793 error_at (loc, "array notation expression cannot be used as a "
9794 "return value");
9795 return error_mark_node;
9798 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9800 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9801 "allowed");
9802 return error_mark_node;
9804 if (retval)
9806 tree semantic_type = NULL_TREE;
9807 npc = null_pointer_constant_p (retval);
9808 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9810 semantic_type = TREE_TYPE (retval);
9811 retval = TREE_OPERAND (retval, 0);
9813 retval = c_fully_fold (retval, false, NULL);
9814 if (semantic_type)
9815 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9818 if (!retval)
9820 current_function_returns_null = 1;
9821 if ((warn_return_type || flag_isoc99)
9822 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9824 bool warned_here;
9825 if (flag_isoc99)
9826 warned_here = pedwarn
9827 (loc, 0,
9828 "%<return%> with no value, in function returning non-void");
9829 else
9830 warned_here = warning_at
9831 (loc, OPT_Wreturn_type,
9832 "%<return%> with no value, in function returning non-void");
9833 no_warning = true;
9834 if (warned_here)
9835 inform (DECL_SOURCE_LOCATION (current_function_decl),
9836 "declared here");
9839 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9841 current_function_returns_null = 1;
9842 bool warned_here;
9843 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9844 warned_here = pedwarn
9845 (xloc, 0,
9846 "%<return%> with a value, in function returning void");
9847 else
9848 warned_here = pedwarn
9849 (xloc, OPT_Wpedantic, "ISO C forbids "
9850 "%<return%> with expression, in function returning void");
9851 if (warned_here)
9852 inform (DECL_SOURCE_LOCATION (current_function_decl),
9853 "declared here");
9855 else
9857 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9858 retval, origtype, ic_return,
9859 npc, NULL_TREE, NULL_TREE, 0);
9860 tree res = DECL_RESULT (current_function_decl);
9861 tree inner;
9862 bool save;
9864 current_function_returns_value = 1;
9865 if (t == error_mark_node)
9866 return NULL_TREE;
9868 save = in_late_binary_op;
9869 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9870 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9871 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9872 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9873 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9874 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9875 in_late_binary_op = true;
9876 inner = t = convert (TREE_TYPE (res), t);
9877 in_late_binary_op = save;
9879 /* Strip any conversions, additions, and subtractions, and see if
9880 we are returning the address of a local variable. Warn if so. */
9881 while (1)
9883 switch (TREE_CODE (inner))
9885 CASE_CONVERT:
9886 case NON_LVALUE_EXPR:
9887 case PLUS_EXPR:
9888 case POINTER_PLUS_EXPR:
9889 inner = TREE_OPERAND (inner, 0);
9890 continue;
9892 case MINUS_EXPR:
9893 /* If the second operand of the MINUS_EXPR has a pointer
9894 type (or is converted from it), this may be valid, so
9895 don't give a warning. */
9897 tree op1 = TREE_OPERAND (inner, 1);
9899 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9900 && (CONVERT_EXPR_P (op1)
9901 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9902 op1 = TREE_OPERAND (op1, 0);
9904 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9905 break;
9907 inner = TREE_OPERAND (inner, 0);
9908 continue;
9911 case ADDR_EXPR:
9912 inner = TREE_OPERAND (inner, 0);
9914 while (REFERENCE_CLASS_P (inner)
9915 && !INDIRECT_REF_P (inner))
9916 inner = TREE_OPERAND (inner, 0);
9918 if (DECL_P (inner)
9919 && !DECL_EXTERNAL (inner)
9920 && !TREE_STATIC (inner)
9921 && DECL_CONTEXT (inner) == current_function_decl)
9923 if (TREE_CODE (inner) == LABEL_DECL)
9924 warning_at (loc, OPT_Wreturn_local_addr,
9925 "function returns address of label");
9926 else
9928 warning_at (loc, OPT_Wreturn_local_addr,
9929 "function returns address of local variable");
9930 tree zero = build_zero_cst (TREE_TYPE (res));
9931 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9934 break;
9936 default:
9937 break;
9940 break;
9943 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9944 SET_EXPR_LOCATION (retval, loc);
9946 if (warn_sequence_point)
9947 verify_sequence_points (retval);
9950 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9951 TREE_NO_WARNING (ret_stmt) |= no_warning;
9952 return add_stmt (ret_stmt);
9955 struct c_switch {
9956 /* The SWITCH_EXPR being built. */
9957 tree switch_expr;
9959 /* The original type of the testing expression, i.e. before the
9960 default conversion is applied. */
9961 tree orig_type;
9963 /* A splay-tree mapping the low element of a case range to the high
9964 element, or NULL_TREE if there is no high element. Used to
9965 determine whether or not a new case label duplicates an old case
9966 label. We need a tree, rather than simply a hash table, because
9967 of the GNU case range extension. */
9968 splay_tree cases;
9970 /* The bindings at the point of the switch. This is used for
9971 warnings crossing decls when branching to a case label. */
9972 struct c_spot_bindings *bindings;
9974 /* The next node on the stack. */
9975 struct c_switch *next;
9977 /* Remember whether the controlling expression had boolean type
9978 before integer promotions for the sake of -Wswitch-bool. */
9979 bool bool_cond_p;
9981 /* Remember whether there was a case value that is outside the
9982 range of the ORIG_TYPE. */
9983 bool outside_range_p;
9986 /* A stack of the currently active switch statements. The innermost
9987 switch statement is on the top of the stack. There is no need to
9988 mark the stack for garbage collection because it is only active
9989 during the processing of the body of a function, and we never
9990 collect at that point. */
9992 struct c_switch *c_switch_stack;
9994 /* Start a C switch statement, testing expression EXP. Return the new
9995 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9996 SWITCH_COND_LOC is the location of the switch's condition.
9997 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
9999 tree
10000 c_start_case (location_t switch_loc,
10001 location_t switch_cond_loc,
10002 tree exp, bool explicit_cast_p)
10004 tree orig_type = error_mark_node;
10005 bool bool_cond_p = false;
10006 struct c_switch *cs;
10008 if (exp != error_mark_node)
10010 orig_type = TREE_TYPE (exp);
10012 if (!INTEGRAL_TYPE_P (orig_type))
10014 if (orig_type != error_mark_node)
10016 error_at (switch_cond_loc, "switch quantity not an integer");
10017 orig_type = error_mark_node;
10019 exp = integer_zero_node;
10021 else
10023 tree type = TYPE_MAIN_VARIANT (orig_type);
10024 tree e = exp;
10026 /* Warn if the condition has boolean value. */
10027 while (TREE_CODE (e) == COMPOUND_EXPR)
10028 e = TREE_OPERAND (e, 1);
10030 if ((TREE_CODE (type) == BOOLEAN_TYPE
10031 || truth_value_p (TREE_CODE (e)))
10032 /* Explicit cast to int suppresses this warning. */
10033 && !(TREE_CODE (type) == INTEGER_TYPE
10034 && explicit_cast_p))
10035 bool_cond_p = true;
10037 if (!in_system_header_at (input_location)
10038 && (type == long_integer_type_node
10039 || type == long_unsigned_type_node))
10040 warning_at (switch_cond_loc,
10041 OPT_Wtraditional, "%<long%> switch expression not "
10042 "converted to %<int%> in ISO C");
10044 exp = c_fully_fold (exp, false, NULL);
10045 exp = default_conversion (exp);
10047 if (warn_sequence_point)
10048 verify_sequence_points (exp);
10052 /* Add this new SWITCH_EXPR to the stack. */
10053 cs = XNEW (struct c_switch);
10054 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
10055 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10056 cs->orig_type = orig_type;
10057 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10058 cs->bindings = c_get_switch_bindings ();
10059 cs->bool_cond_p = bool_cond_p;
10060 cs->outside_range_p = false;
10061 cs->next = c_switch_stack;
10062 c_switch_stack = cs;
10064 return add_stmt (cs->switch_expr);
10067 /* Process a case label at location LOC. */
10069 tree
10070 do_case (location_t loc, tree low_value, tree high_value)
10072 tree label = NULL_TREE;
10074 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10076 low_value = c_fully_fold (low_value, false, NULL);
10077 if (TREE_CODE (low_value) == INTEGER_CST)
10078 pedwarn (loc, OPT_Wpedantic,
10079 "case label is not an integer constant expression");
10082 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10084 high_value = c_fully_fold (high_value, false, NULL);
10085 if (TREE_CODE (high_value) == INTEGER_CST)
10086 pedwarn (input_location, OPT_Wpedantic,
10087 "case label is not an integer constant expression");
10090 if (c_switch_stack == NULL)
10092 if (low_value)
10093 error_at (loc, "case label not within a switch statement");
10094 else
10095 error_at (loc, "%<default%> label not within a switch statement");
10096 return NULL_TREE;
10099 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10100 EXPR_LOCATION (c_switch_stack->switch_expr),
10101 loc))
10102 return NULL_TREE;
10104 label = c_add_case_label (loc, c_switch_stack->cases,
10105 SWITCH_COND (c_switch_stack->switch_expr),
10106 c_switch_stack->orig_type,
10107 low_value, high_value,
10108 &c_switch_stack->outside_range_p);
10109 if (label == error_mark_node)
10110 label = NULL_TREE;
10111 return label;
10114 /* Finish the switch statement. TYPE is the original type of the
10115 controlling expression of the switch, or NULL_TREE. */
10117 void
10118 c_finish_case (tree body, tree type)
10120 struct c_switch *cs = c_switch_stack;
10121 location_t switch_location;
10123 SWITCH_BODY (cs->switch_expr) = body;
10125 /* Emit warnings as needed. */
10126 switch_location = EXPR_LOCATION (cs->switch_expr);
10127 c_do_switch_warnings (cs->cases, switch_location,
10128 type ? type : TREE_TYPE (cs->switch_expr),
10129 SWITCH_COND (cs->switch_expr),
10130 cs->bool_cond_p, cs->outside_range_p);
10132 /* Pop the stack. */
10133 c_switch_stack = cs->next;
10134 splay_tree_delete (cs->cases);
10135 c_release_switch_bindings (cs->bindings);
10136 XDELETE (cs);
10139 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10140 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10141 may be null. */
10143 void
10144 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10145 tree else_block)
10147 tree stmt;
10149 /* If the condition has array notations, then the rank of the then_block and
10150 else_block must be either 0 or be equal to the rank of the condition. If
10151 the condition does not have array notations then break them up as it is
10152 broken up in a normal expression. */
10153 if (flag_cilkplus && contains_array_notation_expr (cond))
10155 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10156 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10157 return;
10158 if (then_block
10159 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10160 return;
10161 if (else_block
10162 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10163 return;
10164 if (cond_rank != then_rank && then_rank != 0)
10166 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10167 " and the then-block");
10168 return;
10170 else if (cond_rank != else_rank && else_rank != 0)
10172 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10173 " and the else-block");
10174 return;
10178 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10179 SET_EXPR_LOCATION (stmt, if_locus);
10180 add_stmt (stmt);
10183 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10184 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10185 is false for DO loops. INCR is the FOR increment expression. BODY is
10186 the statement controlled by the loop. BLAB is the break label. CLAB is
10187 the continue label. Everything is allowed to be NULL. */
10189 void
10190 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10191 tree blab, tree clab, bool cond_is_first)
10193 tree entry = NULL, exit = NULL, t;
10195 /* In theory could forbid cilk spawn for loop increment expression,
10196 but it should work just fine. */
10198 /* If the condition is zero don't generate a loop construct. */
10199 if (cond && integer_zerop (cond))
10201 if (cond_is_first)
10203 t = build_and_jump (&blab);
10204 SET_EXPR_LOCATION (t, start_locus);
10205 add_stmt (t);
10208 else
10210 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10212 /* If we have an exit condition, then we build an IF with gotos either
10213 out of the loop, or to the top of it. If there's no exit condition,
10214 then we just build a jump back to the top. */
10215 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10217 if (cond && !integer_nonzerop (cond))
10219 /* Canonicalize the loop condition to the end. This means
10220 generating a branch to the loop condition. Reuse the
10221 continue label, if possible. */
10222 if (cond_is_first)
10224 if (incr || !clab)
10226 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10227 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10229 else
10230 t = build1 (GOTO_EXPR, void_type_node, clab);
10231 SET_EXPR_LOCATION (t, start_locus);
10232 add_stmt (t);
10235 t = build_and_jump (&blab);
10236 if (cond_is_first)
10237 exit = fold_build3_loc (start_locus,
10238 COND_EXPR, void_type_node, cond, exit, t);
10239 else
10240 exit = fold_build3_loc (input_location,
10241 COND_EXPR, void_type_node, cond, exit, t);
10243 else
10245 /* For the backward-goto's location of an unconditional loop
10246 use the beginning of the body, or, if there is none, the
10247 top of the loop. */
10248 location_t loc = EXPR_LOCATION (expr_first (body));
10249 if (loc == UNKNOWN_LOCATION)
10250 loc = start_locus;
10251 SET_EXPR_LOCATION (exit, loc);
10254 add_stmt (top);
10257 if (body)
10258 add_stmt (body);
10259 if (clab)
10260 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10261 if (incr)
10262 add_stmt (incr);
10263 if (entry)
10264 add_stmt (entry);
10265 if (exit)
10266 add_stmt (exit);
10267 if (blab)
10268 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10271 tree
10272 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10274 bool skip;
10275 tree label = *label_p;
10277 /* In switch statements break is sometimes stylistically used after
10278 a return statement. This can lead to spurious warnings about
10279 control reaching the end of a non-void function when it is
10280 inlined. Note that we are calling block_may_fallthru with
10281 language specific tree nodes; this works because
10282 block_may_fallthru returns true when given something it does not
10283 understand. */
10284 skip = !block_may_fallthru (cur_stmt_list);
10286 if (!label)
10288 if (!skip)
10289 *label_p = label = create_artificial_label (loc);
10291 else if (TREE_CODE (label) == LABEL_DECL)
10293 else switch (TREE_INT_CST_LOW (label))
10295 case 0:
10296 if (is_break)
10297 error_at (loc, "break statement not within loop or switch");
10298 else
10299 error_at (loc, "continue statement not within a loop");
10300 return NULL_TREE;
10302 case 1:
10303 gcc_assert (is_break);
10304 error_at (loc, "break statement used with OpenMP for loop");
10305 return NULL_TREE;
10307 case 2:
10308 if (is_break)
10309 error ("break statement within %<#pragma simd%> loop body");
10310 else
10311 error ("continue statement within %<#pragma simd%> loop body");
10312 return NULL_TREE;
10314 default:
10315 gcc_unreachable ();
10318 if (skip)
10319 return NULL_TREE;
10321 if (!is_break)
10322 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10324 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10327 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10329 static void
10330 emit_side_effect_warnings (location_t loc, tree expr)
10332 if (expr == error_mark_node)
10334 else if (!TREE_SIDE_EFFECTS (expr))
10336 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10337 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10339 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10341 tree r = expr;
10342 location_t cloc = loc;
10343 while (TREE_CODE (r) == COMPOUND_EXPR)
10345 if (EXPR_HAS_LOCATION (r))
10346 cloc = EXPR_LOCATION (r);
10347 r = TREE_OPERAND (r, 1);
10349 if (!TREE_SIDE_EFFECTS (r)
10350 && !VOID_TYPE_P (TREE_TYPE (r))
10351 && !CONVERT_EXPR_P (r)
10352 && !TREE_NO_WARNING (r)
10353 && !TREE_NO_WARNING (expr))
10354 warning_at (cloc, OPT_Wunused_value,
10355 "right-hand operand of comma expression has no effect");
10357 else
10358 warn_if_unused_value (expr, loc);
10361 /* Process an expression as if it were a complete statement. Emit
10362 diagnostics, but do not call ADD_STMT. LOC is the location of the
10363 statement. */
10365 tree
10366 c_process_expr_stmt (location_t loc, tree expr)
10368 tree exprv;
10370 if (!expr)
10371 return NULL_TREE;
10373 expr = c_fully_fold (expr, false, NULL);
10375 if (warn_sequence_point)
10376 verify_sequence_points (expr);
10378 if (TREE_TYPE (expr) != error_mark_node
10379 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10380 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10381 error_at (loc, "expression statement has incomplete type");
10383 /* If we're not processing a statement expression, warn about unused values.
10384 Warnings for statement expressions will be emitted later, once we figure
10385 out which is the result. */
10386 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10387 && warn_unused_value)
10388 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10390 exprv = expr;
10391 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10392 exprv = TREE_OPERAND (exprv, 1);
10393 while (CONVERT_EXPR_P (exprv))
10394 exprv = TREE_OPERAND (exprv, 0);
10395 if (DECL_P (exprv)
10396 || handled_component_p (exprv)
10397 || TREE_CODE (exprv) == ADDR_EXPR)
10398 mark_exp_read (exprv);
10400 /* If the expression is not of a type to which we cannot assign a line
10401 number, wrap the thing in a no-op NOP_EXPR. */
10402 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10404 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10405 SET_EXPR_LOCATION (expr, loc);
10408 return expr;
10411 /* Emit an expression as a statement. LOC is the location of the
10412 expression. */
10414 tree
10415 c_finish_expr_stmt (location_t loc, tree expr)
10417 if (expr)
10418 return add_stmt (c_process_expr_stmt (loc, expr));
10419 else
10420 return NULL;
10423 /* Do the opposite and emit a statement as an expression. To begin,
10424 create a new binding level and return it. */
10426 tree
10427 c_begin_stmt_expr (void)
10429 tree ret;
10431 /* We must force a BLOCK for this level so that, if it is not expanded
10432 later, there is a way to turn off the entire subtree of blocks that
10433 are contained in it. */
10434 keep_next_level ();
10435 ret = c_begin_compound_stmt (true);
10437 c_bindings_start_stmt_expr (c_switch_stack == NULL
10438 ? NULL
10439 : c_switch_stack->bindings);
10441 /* Mark the current statement list as belonging to a statement list. */
10442 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10444 return ret;
10447 /* LOC is the location of the compound statement to which this body
10448 belongs. */
10450 tree
10451 c_finish_stmt_expr (location_t loc, tree body)
10453 tree last, type, tmp, val;
10454 tree *last_p;
10456 body = c_end_compound_stmt (loc, body, true);
10458 c_bindings_end_stmt_expr (c_switch_stack == NULL
10459 ? NULL
10460 : c_switch_stack->bindings);
10462 /* Locate the last statement in BODY. See c_end_compound_stmt
10463 about always returning a BIND_EXPR. */
10464 last_p = &BIND_EXPR_BODY (body);
10465 last = BIND_EXPR_BODY (body);
10467 continue_searching:
10468 if (TREE_CODE (last) == STATEMENT_LIST)
10470 tree_stmt_iterator i;
10472 /* This can happen with degenerate cases like ({ }). No value. */
10473 if (!TREE_SIDE_EFFECTS (last))
10474 return body;
10476 /* If we're supposed to generate side effects warnings, process
10477 all of the statements except the last. */
10478 if (warn_unused_value)
10480 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10482 location_t tloc;
10483 tree t = tsi_stmt (i);
10485 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10486 emit_side_effect_warnings (tloc, t);
10489 else
10490 i = tsi_last (last);
10491 last_p = tsi_stmt_ptr (i);
10492 last = *last_p;
10495 /* If the end of the list is exception related, then the list was split
10496 by a call to push_cleanup. Continue searching. */
10497 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10498 || TREE_CODE (last) == TRY_CATCH_EXPR)
10500 last_p = &TREE_OPERAND (last, 0);
10501 last = *last_p;
10502 goto continue_searching;
10505 if (last == error_mark_node)
10506 return last;
10508 /* In the case that the BIND_EXPR is not necessary, return the
10509 expression out from inside it. */
10510 if (last == BIND_EXPR_BODY (body)
10511 && BIND_EXPR_VARS (body) == NULL)
10513 /* Even if this looks constant, do not allow it in a constant
10514 expression. */
10515 last = c_wrap_maybe_const (last, true);
10516 /* Do not warn if the return value of a statement expression is
10517 unused. */
10518 TREE_NO_WARNING (last) = 1;
10519 return last;
10522 /* Extract the type of said expression. */
10523 type = TREE_TYPE (last);
10525 /* If we're not returning a value at all, then the BIND_EXPR that
10526 we already have is a fine expression to return. */
10527 if (!type || VOID_TYPE_P (type))
10528 return body;
10530 /* Now that we've located the expression containing the value, it seems
10531 silly to make voidify_wrapper_expr repeat the process. Create a
10532 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10533 tmp = create_tmp_var_raw (type);
10535 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10536 tree_expr_nonnegative_p giving up immediately. */
10537 val = last;
10538 if (TREE_CODE (val) == NOP_EXPR
10539 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10540 val = TREE_OPERAND (val, 0);
10542 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10543 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10546 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10547 SET_EXPR_LOCATION (t, loc);
10548 return t;
10552 /* Begin and end compound statements. This is as simple as pushing
10553 and popping new statement lists from the tree. */
10555 tree
10556 c_begin_compound_stmt (bool do_scope)
10558 tree stmt = push_stmt_list ();
10559 if (do_scope)
10560 push_scope ();
10561 return stmt;
10564 /* End a compound statement. STMT is the statement. LOC is the
10565 location of the compound statement-- this is usually the location
10566 of the opening brace. */
10568 tree
10569 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10571 tree block = NULL;
10573 if (do_scope)
10575 if (c_dialect_objc ())
10576 objc_clear_super_receiver ();
10577 block = pop_scope ();
10580 stmt = pop_stmt_list (stmt);
10581 stmt = c_build_bind_expr (loc, block, stmt);
10583 /* If this compound statement is nested immediately inside a statement
10584 expression, then force a BIND_EXPR to be created. Otherwise we'll
10585 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10586 STATEMENT_LISTs merge, and thus we can lose track of what statement
10587 was really last. */
10588 if (building_stmt_list_p ()
10589 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10590 && TREE_CODE (stmt) != BIND_EXPR)
10592 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10593 TREE_SIDE_EFFECTS (stmt) = 1;
10594 SET_EXPR_LOCATION (stmt, loc);
10597 return stmt;
10600 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10601 when the current scope is exited. EH_ONLY is true when this is not
10602 meant to apply to normal control flow transfer. */
10604 void
10605 push_cleanup (tree decl, tree cleanup, bool eh_only)
10607 enum tree_code code;
10608 tree stmt, list;
10609 bool stmt_expr;
10611 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10612 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10613 add_stmt (stmt);
10614 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10615 list = push_stmt_list ();
10616 TREE_OPERAND (stmt, 0) = list;
10617 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10620 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10621 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10623 static tree
10624 build_vec_cmp (tree_code code, tree type,
10625 tree arg0, tree arg1)
10627 tree zero_vec = build_zero_cst (type);
10628 tree minus_one_vec = build_minus_one_cst (type);
10629 tree cmp_type = build_same_sized_truth_vector_type (type);
10630 tree cmp = build2 (code, cmp_type, arg0, arg1);
10631 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10634 /* Build a binary-operation expression without default conversions.
10635 CODE is the kind of expression to build.
10636 LOCATION is the operator's location.
10637 This function differs from `build' in several ways:
10638 the data type of the result is computed and recorded in it,
10639 warnings are generated if arg data types are invalid,
10640 special handling for addition and subtraction of pointers is known,
10641 and some optimization is done (operations on narrow ints
10642 are done in the narrower type when that gives the same result).
10643 Constant folding is also done before the result is returned.
10645 Note that the operands will never have enumeral types, or function
10646 or array types, because either they will have the default conversions
10647 performed or they have both just been converted to some other type in which
10648 the arithmetic is to be done. */
10650 tree
10651 build_binary_op (location_t location, enum tree_code code,
10652 tree orig_op0, tree orig_op1, int convert_p)
10654 tree type0, type1, orig_type0, orig_type1;
10655 tree eptype;
10656 enum tree_code code0, code1;
10657 tree op0, op1;
10658 tree ret = error_mark_node;
10659 const char *invalid_op_diag;
10660 bool op0_int_operands, op1_int_operands;
10661 bool int_const, int_const_or_overflow, int_operands;
10663 /* Expression code to give to the expression when it is built.
10664 Normally this is CODE, which is what the caller asked for,
10665 but in some special cases we change it. */
10666 enum tree_code resultcode = code;
10668 /* Data type in which the computation is to be performed.
10669 In the simplest cases this is the common type of the arguments. */
10670 tree result_type = NULL;
10672 /* When the computation is in excess precision, the type of the
10673 final EXCESS_PRECISION_EXPR. */
10674 tree semantic_result_type = NULL;
10676 /* Nonzero means operands have already been type-converted
10677 in whatever way is necessary.
10678 Zero means they need to be converted to RESULT_TYPE. */
10679 int converted = 0;
10681 /* Nonzero means create the expression with this type, rather than
10682 RESULT_TYPE. */
10683 tree build_type = 0;
10685 /* Nonzero means after finally constructing the expression
10686 convert it to this type. */
10687 tree final_type = 0;
10689 /* Nonzero if this is an operation like MIN or MAX which can
10690 safely be computed in short if both args are promoted shorts.
10691 Also implies COMMON.
10692 -1 indicates a bitwise operation; this makes a difference
10693 in the exact conditions for when it is safe to do the operation
10694 in a narrower mode. */
10695 int shorten = 0;
10697 /* Nonzero if this is a comparison operation;
10698 if both args are promoted shorts, compare the original shorts.
10699 Also implies COMMON. */
10700 int short_compare = 0;
10702 /* Nonzero if this is a right-shift operation, which can be computed on the
10703 original short and then promoted if the operand is a promoted short. */
10704 int short_shift = 0;
10706 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10707 int common = 0;
10709 /* True means types are compatible as far as ObjC is concerned. */
10710 bool objc_ok;
10712 /* True means this is an arithmetic operation that may need excess
10713 precision. */
10714 bool may_need_excess_precision;
10716 /* True means this is a boolean operation that converts both its
10717 operands to truth-values. */
10718 bool boolean_op = false;
10720 /* Remember whether we're doing / or %. */
10721 bool doing_div_or_mod = false;
10723 /* Remember whether we're doing << or >>. */
10724 bool doing_shift = false;
10726 /* Tree holding instrumentation expression. */
10727 tree instrument_expr = NULL;
10729 if (location == UNKNOWN_LOCATION)
10730 location = input_location;
10732 op0 = orig_op0;
10733 op1 = orig_op1;
10735 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10736 if (op0_int_operands)
10737 op0 = remove_c_maybe_const_expr (op0);
10738 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10739 if (op1_int_operands)
10740 op1 = remove_c_maybe_const_expr (op1);
10741 int_operands = (op0_int_operands && op1_int_operands);
10742 if (int_operands)
10744 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10745 && TREE_CODE (orig_op1) == INTEGER_CST);
10746 int_const = (int_const_or_overflow
10747 && !TREE_OVERFLOW (orig_op0)
10748 && !TREE_OVERFLOW (orig_op1));
10750 else
10751 int_const = int_const_or_overflow = false;
10753 /* Do not apply default conversion in mixed vector/scalar expression. */
10754 if (convert_p
10755 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10757 op0 = default_conversion (op0);
10758 op1 = default_conversion (op1);
10761 /* When Cilk Plus is enabled and there are array notations inside op0, then
10762 we check to see if there are builtin array notation functions. If
10763 so, then we take on the type of the array notation inside it. */
10764 if (flag_cilkplus && contains_array_notation_expr (op0))
10765 orig_type0 = type0 = find_correct_array_notation_type (op0);
10766 else
10767 orig_type0 = type0 = TREE_TYPE (op0);
10769 if (flag_cilkplus && contains_array_notation_expr (op1))
10770 orig_type1 = type1 = find_correct_array_notation_type (op1);
10771 else
10772 orig_type1 = type1 = TREE_TYPE (op1);
10774 /* The expression codes of the data types of the arguments tell us
10775 whether the arguments are integers, floating, pointers, etc. */
10776 code0 = TREE_CODE (type0);
10777 code1 = TREE_CODE (type1);
10779 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10780 STRIP_TYPE_NOPS (op0);
10781 STRIP_TYPE_NOPS (op1);
10783 /* If an error was already reported for one of the arguments,
10784 avoid reporting another error. */
10786 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10787 return error_mark_node;
10789 if (code0 == POINTER_TYPE
10790 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10791 return error_mark_node;
10793 if (code1 == POINTER_TYPE
10794 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10795 return error_mark_node;
10797 if ((invalid_op_diag
10798 = targetm.invalid_binary_op (code, type0, type1)))
10800 error_at (location, invalid_op_diag);
10801 return error_mark_node;
10804 switch (code)
10806 case PLUS_EXPR:
10807 case MINUS_EXPR:
10808 case MULT_EXPR:
10809 case TRUNC_DIV_EXPR:
10810 case CEIL_DIV_EXPR:
10811 case FLOOR_DIV_EXPR:
10812 case ROUND_DIV_EXPR:
10813 case EXACT_DIV_EXPR:
10814 may_need_excess_precision = true;
10815 break;
10816 default:
10817 may_need_excess_precision = false;
10818 break;
10820 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10822 op0 = TREE_OPERAND (op0, 0);
10823 type0 = TREE_TYPE (op0);
10825 else if (may_need_excess_precision
10826 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10828 type0 = eptype;
10829 op0 = convert (eptype, op0);
10831 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10833 op1 = TREE_OPERAND (op1, 0);
10834 type1 = TREE_TYPE (op1);
10836 else if (may_need_excess_precision
10837 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10839 type1 = eptype;
10840 op1 = convert (eptype, op1);
10843 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10845 /* In case when one of the operands of the binary operation is
10846 a vector and another is a scalar -- convert scalar to vector. */
10847 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10849 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10850 true);
10852 switch (convert_flag)
10854 case stv_error:
10855 return error_mark_node;
10856 case stv_firstarg:
10858 bool maybe_const = true;
10859 tree sc;
10860 sc = c_fully_fold (op0, false, &maybe_const);
10861 sc = save_expr (sc);
10862 sc = convert (TREE_TYPE (type1), sc);
10863 op0 = build_vector_from_val (type1, sc);
10864 if (!maybe_const)
10865 op0 = c_wrap_maybe_const (op0, true);
10866 orig_type0 = type0 = TREE_TYPE (op0);
10867 code0 = TREE_CODE (type0);
10868 converted = 1;
10869 break;
10871 case stv_secondarg:
10873 bool maybe_const = true;
10874 tree sc;
10875 sc = c_fully_fold (op1, false, &maybe_const);
10876 sc = save_expr (sc);
10877 sc = convert (TREE_TYPE (type0), sc);
10878 op1 = build_vector_from_val (type0, sc);
10879 if (!maybe_const)
10880 op1 = c_wrap_maybe_const (op1, true);
10881 orig_type1 = type1 = TREE_TYPE (op1);
10882 code1 = TREE_CODE (type1);
10883 converted = 1;
10884 break;
10886 default:
10887 break;
10891 switch (code)
10893 case PLUS_EXPR:
10894 /* Handle the pointer + int case. */
10895 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10897 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10898 goto return_build_binary_op;
10900 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10902 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10903 goto return_build_binary_op;
10905 else
10906 common = 1;
10907 break;
10909 case MINUS_EXPR:
10910 /* Subtraction of two similar pointers.
10911 We must subtract them as integers, then divide by object size. */
10912 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10913 && comp_target_types (location, type0, type1))
10915 ret = pointer_diff (location, op0, op1);
10916 goto return_build_binary_op;
10918 /* Handle pointer minus int. Just like pointer plus int. */
10919 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10921 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10922 goto return_build_binary_op;
10924 else
10925 common = 1;
10926 break;
10928 case MULT_EXPR:
10929 common = 1;
10930 break;
10932 case TRUNC_DIV_EXPR:
10933 case CEIL_DIV_EXPR:
10934 case FLOOR_DIV_EXPR:
10935 case ROUND_DIV_EXPR:
10936 case EXACT_DIV_EXPR:
10937 doing_div_or_mod = true;
10938 warn_for_div_by_zero (location, op1);
10940 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10941 || code0 == FIXED_POINT_TYPE
10942 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10943 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10944 || code1 == FIXED_POINT_TYPE
10945 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10947 enum tree_code tcode0 = code0, tcode1 = code1;
10949 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10950 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10951 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10952 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10954 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10955 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10956 resultcode = RDIV_EXPR;
10957 else
10958 /* Although it would be tempting to shorten always here, that
10959 loses on some targets, since the modulo instruction is
10960 undefined if the quotient can't be represented in the
10961 computation mode. We shorten only if unsigned or if
10962 dividing by something we know != -1. */
10963 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10964 || (TREE_CODE (op1) == INTEGER_CST
10965 && !integer_all_onesp (op1)));
10966 common = 1;
10968 break;
10970 case BIT_AND_EXPR:
10971 case BIT_IOR_EXPR:
10972 case BIT_XOR_EXPR:
10973 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10974 shorten = -1;
10975 /* Allow vector types which are not floating point types. */
10976 else if (code0 == VECTOR_TYPE
10977 && code1 == VECTOR_TYPE
10978 && !VECTOR_FLOAT_TYPE_P (type0)
10979 && !VECTOR_FLOAT_TYPE_P (type1))
10980 common = 1;
10981 break;
10983 case TRUNC_MOD_EXPR:
10984 case FLOOR_MOD_EXPR:
10985 doing_div_or_mod = true;
10986 warn_for_div_by_zero (location, op1);
10988 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10989 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10990 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10991 common = 1;
10992 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10994 /* Although it would be tempting to shorten always here, that loses
10995 on some targets, since the modulo instruction is undefined if the
10996 quotient can't be represented in the computation mode. We shorten
10997 only if unsigned or if dividing by something we know != -1. */
10998 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10999 || (TREE_CODE (op1) == INTEGER_CST
11000 && !integer_all_onesp (op1)));
11001 common = 1;
11003 break;
11005 case TRUTH_ANDIF_EXPR:
11006 case TRUTH_ORIF_EXPR:
11007 case TRUTH_AND_EXPR:
11008 case TRUTH_OR_EXPR:
11009 case TRUTH_XOR_EXPR:
11010 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11011 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11012 || code0 == FIXED_POINT_TYPE)
11013 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11014 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11015 || code1 == FIXED_POINT_TYPE))
11017 /* Result of these operations is always an int,
11018 but that does not mean the operands should be
11019 converted to ints! */
11020 result_type = integer_type_node;
11021 if (op0_int_operands)
11023 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11024 op0 = remove_c_maybe_const_expr (op0);
11026 else
11027 op0 = c_objc_common_truthvalue_conversion (location, op0);
11028 if (op1_int_operands)
11030 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11031 op1 = remove_c_maybe_const_expr (op1);
11033 else
11034 op1 = c_objc_common_truthvalue_conversion (location, op1);
11035 converted = 1;
11036 boolean_op = true;
11038 if (code == TRUTH_ANDIF_EXPR)
11040 int_const_or_overflow = (int_operands
11041 && TREE_CODE (orig_op0) == INTEGER_CST
11042 && (op0 == truthvalue_false_node
11043 || TREE_CODE (orig_op1) == INTEGER_CST));
11044 int_const = (int_const_or_overflow
11045 && !TREE_OVERFLOW (orig_op0)
11046 && (op0 == truthvalue_false_node
11047 || !TREE_OVERFLOW (orig_op1)));
11049 else if (code == TRUTH_ORIF_EXPR)
11051 int_const_or_overflow = (int_operands
11052 && TREE_CODE (orig_op0) == INTEGER_CST
11053 && (op0 == truthvalue_true_node
11054 || TREE_CODE (orig_op1) == INTEGER_CST));
11055 int_const = (int_const_or_overflow
11056 && !TREE_OVERFLOW (orig_op0)
11057 && (op0 == truthvalue_true_node
11058 || !TREE_OVERFLOW (orig_op1)));
11060 break;
11062 /* Shift operations: result has same type as first operand;
11063 always convert second operand to int.
11064 Also set SHORT_SHIFT if shifting rightward. */
11066 case RSHIFT_EXPR:
11067 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11068 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11069 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11070 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11072 result_type = type0;
11073 converted = 1;
11075 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11076 || code0 == VECTOR_TYPE)
11077 && code1 == INTEGER_TYPE)
11079 doing_shift = true;
11080 if (TREE_CODE (op1) == INTEGER_CST)
11082 if (tree_int_cst_sgn (op1) < 0)
11084 int_const = false;
11085 if (c_inhibit_evaluation_warnings == 0)
11086 warning_at (location, OPT_Wshift_count_negative,
11087 "right shift count is negative");
11089 else if (code0 == VECTOR_TYPE)
11091 if (compare_tree_int (op1,
11092 TYPE_PRECISION (TREE_TYPE (type0)))
11093 >= 0)
11095 int_const = false;
11096 if (c_inhibit_evaluation_warnings == 0)
11097 warning_at (location, OPT_Wshift_count_overflow,
11098 "right shift count >= width of vector element");
11101 else
11103 if (!integer_zerop (op1))
11104 short_shift = 1;
11106 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11108 int_const = false;
11109 if (c_inhibit_evaluation_warnings == 0)
11110 warning_at (location, OPT_Wshift_count_overflow,
11111 "right shift count >= width of type");
11116 /* Use the type of the value to be shifted. */
11117 result_type = type0;
11118 /* Avoid converting op1 to result_type later. */
11119 converted = 1;
11121 break;
11123 case LSHIFT_EXPR:
11124 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11125 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11126 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11127 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11129 result_type = type0;
11130 converted = 1;
11132 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11133 || code0 == VECTOR_TYPE)
11134 && code1 == INTEGER_TYPE)
11136 doing_shift = true;
11137 if (TREE_CODE (op0) == INTEGER_CST
11138 && tree_int_cst_sgn (op0) < 0)
11140 /* Don't reject a left shift of a negative value in a context
11141 where a constant expression is needed in C90. */
11142 if (flag_isoc99)
11143 int_const = false;
11144 if (c_inhibit_evaluation_warnings == 0)
11145 warning_at (location, OPT_Wshift_negative_value,
11146 "left shift of negative value");
11148 if (TREE_CODE (op1) == INTEGER_CST)
11150 if (tree_int_cst_sgn (op1) < 0)
11152 int_const = false;
11153 if (c_inhibit_evaluation_warnings == 0)
11154 warning_at (location, OPT_Wshift_count_negative,
11155 "left shift count is negative");
11157 else if (code0 == VECTOR_TYPE)
11159 if (compare_tree_int (op1,
11160 TYPE_PRECISION (TREE_TYPE (type0)))
11161 >= 0)
11163 int_const = false;
11164 if (c_inhibit_evaluation_warnings == 0)
11165 warning_at (location, OPT_Wshift_count_overflow,
11166 "left shift count >= width of vector element");
11169 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11171 int_const = false;
11172 if (c_inhibit_evaluation_warnings == 0)
11173 warning_at (location, OPT_Wshift_count_overflow,
11174 "left shift count >= width of type");
11176 else if (TREE_CODE (op0) == INTEGER_CST
11177 && maybe_warn_shift_overflow (location, op0, op1)
11178 && flag_isoc99)
11179 int_const = false;
11182 /* Use the type of the value to be shifted. */
11183 result_type = type0;
11184 /* Avoid converting op1 to result_type later. */
11185 converted = 1;
11187 break;
11189 case EQ_EXPR:
11190 case NE_EXPR:
11191 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11193 tree intt;
11194 if (!vector_types_compatible_elements_p (type0, type1))
11196 error_at (location, "comparing vectors with different "
11197 "element types");
11198 return error_mark_node;
11201 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11203 error_at (location, "comparing vectors with different "
11204 "number of elements");
11205 return error_mark_node;
11208 /* It's not precisely specified how the usual arithmetic
11209 conversions apply to the vector types. Here, we use
11210 the unsigned type if one of the operands is signed and
11211 the other one is unsigned. */
11212 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11214 if (!TYPE_UNSIGNED (type0))
11215 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11216 else
11217 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11218 warning_at (location, OPT_Wsign_compare, "comparison between "
11219 "types %qT and %qT", type0, type1);
11222 /* Always construct signed integer vector type. */
11223 intt = c_common_type_for_size (GET_MODE_BITSIZE
11224 (TYPE_MODE (TREE_TYPE (type0))), 0);
11225 result_type = build_opaque_vector_type (intt,
11226 TYPE_VECTOR_SUBPARTS (type0));
11227 converted = 1;
11228 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11229 goto return_build_binary_op;
11231 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11232 warning_at (location,
11233 OPT_Wfloat_equal,
11234 "comparing floating point with == or != is unsafe");
11235 /* Result of comparison is always int,
11236 but don't convert the args to int! */
11237 build_type = integer_type_node;
11238 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11239 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11240 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11241 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11242 short_compare = 1;
11243 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11245 if (TREE_CODE (op0) == ADDR_EXPR
11246 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11247 && !from_macro_expansion_at (location))
11249 if (code == EQ_EXPR)
11250 warning_at (location,
11251 OPT_Waddress,
11252 "the comparison will always evaluate as %<false%> "
11253 "for the address of %qD will never be NULL",
11254 TREE_OPERAND (op0, 0));
11255 else
11256 warning_at (location,
11257 OPT_Waddress,
11258 "the comparison will always evaluate as %<true%> "
11259 "for the address of %qD will never be NULL",
11260 TREE_OPERAND (op0, 0));
11262 result_type = type0;
11264 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11266 if (TREE_CODE (op1) == ADDR_EXPR
11267 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11268 && !from_macro_expansion_at (location))
11270 if (code == EQ_EXPR)
11271 warning_at (location,
11272 OPT_Waddress,
11273 "the comparison will always evaluate as %<false%> "
11274 "for the address of %qD will never be NULL",
11275 TREE_OPERAND (op1, 0));
11276 else
11277 warning_at (location,
11278 OPT_Waddress,
11279 "the comparison will always evaluate as %<true%> "
11280 "for the address of %qD will never be NULL",
11281 TREE_OPERAND (op1, 0));
11283 result_type = type1;
11285 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11287 tree tt0 = TREE_TYPE (type0);
11288 tree tt1 = TREE_TYPE (type1);
11289 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11290 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11291 addr_space_t as_common = ADDR_SPACE_GENERIC;
11293 /* Anything compares with void *. void * compares with anything.
11294 Otherwise, the targets must be compatible
11295 and both must be object or both incomplete. */
11296 if (comp_target_types (location, type0, type1))
11297 result_type = common_pointer_type (type0, type1);
11298 else if (!addr_space_superset (as0, as1, &as_common))
11300 error_at (location, "comparison of pointers to "
11301 "disjoint address spaces");
11302 return error_mark_node;
11304 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11306 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11307 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11308 "comparison of %<void *%> with function pointer");
11310 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11312 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11313 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11314 "comparison of %<void *%> with function pointer");
11316 else
11317 /* Avoid warning about the volatile ObjC EH puts on decls. */
11318 if (!objc_ok)
11319 pedwarn (location, 0,
11320 "comparison of distinct pointer types lacks a cast");
11322 if (result_type == NULL_TREE)
11324 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11325 result_type = build_pointer_type
11326 (build_qualified_type (void_type_node, qual));
11329 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11331 result_type = type0;
11332 pedwarn (location, 0, "comparison between pointer and integer");
11334 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11336 result_type = type1;
11337 pedwarn (location, 0, "comparison between pointer and integer");
11339 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11340 || truth_value_p (TREE_CODE (orig_op0)))
11341 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11342 || truth_value_p (TREE_CODE (orig_op1))))
11343 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11344 break;
11346 case LE_EXPR:
11347 case GE_EXPR:
11348 case LT_EXPR:
11349 case GT_EXPR:
11350 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11352 tree intt;
11353 if (!vector_types_compatible_elements_p (type0, type1))
11355 error_at (location, "comparing vectors with different "
11356 "element types");
11357 return error_mark_node;
11360 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11362 error_at (location, "comparing vectors with different "
11363 "number of elements");
11364 return error_mark_node;
11367 /* It's not precisely specified how the usual arithmetic
11368 conversions apply to the vector types. Here, we use
11369 the unsigned type if one of the operands is signed and
11370 the other one is unsigned. */
11371 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11373 if (!TYPE_UNSIGNED (type0))
11374 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11375 else
11376 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11377 warning_at (location, OPT_Wsign_compare, "comparison between "
11378 "types %qT and %qT", type0, type1);
11381 /* Always construct signed integer vector type. */
11382 intt = c_common_type_for_size (GET_MODE_BITSIZE
11383 (TYPE_MODE (TREE_TYPE (type0))), 0);
11384 result_type = build_opaque_vector_type (intt,
11385 TYPE_VECTOR_SUBPARTS (type0));
11386 converted = 1;
11387 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11388 goto return_build_binary_op;
11390 build_type = integer_type_node;
11391 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11392 || code0 == FIXED_POINT_TYPE)
11393 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11394 || code1 == FIXED_POINT_TYPE))
11395 short_compare = 1;
11396 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11398 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11399 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11400 addr_space_t as_common;
11402 if (comp_target_types (location, type0, type1))
11404 result_type = common_pointer_type (type0, type1);
11405 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11406 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11407 pedwarn (location, 0,
11408 "comparison of complete and incomplete pointers");
11409 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11410 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11411 "ordered comparisons of pointers to functions");
11412 else if (null_pointer_constant_p (orig_op0)
11413 || null_pointer_constant_p (orig_op1))
11414 warning_at (location, OPT_Wextra,
11415 "ordered comparison of pointer with null pointer");
11418 else if (!addr_space_superset (as0, as1, &as_common))
11420 error_at (location, "comparison of pointers to "
11421 "disjoint address spaces");
11422 return error_mark_node;
11424 else
11426 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11427 result_type = build_pointer_type
11428 (build_qualified_type (void_type_node, qual));
11429 pedwarn (location, 0,
11430 "comparison of distinct pointer types lacks a cast");
11433 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11435 result_type = type0;
11436 if (pedantic)
11437 pedwarn (location, OPT_Wpedantic,
11438 "ordered comparison of pointer with integer zero");
11439 else if (extra_warnings)
11440 warning_at (location, OPT_Wextra,
11441 "ordered comparison of pointer with integer zero");
11443 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11445 result_type = type1;
11446 if (pedantic)
11447 pedwarn (location, OPT_Wpedantic,
11448 "ordered comparison of pointer with integer zero");
11449 else if (extra_warnings)
11450 warning_at (location, OPT_Wextra,
11451 "ordered comparison of pointer with integer zero");
11453 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11455 result_type = type0;
11456 pedwarn (location, 0, "comparison between pointer and integer");
11458 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11460 result_type = type1;
11461 pedwarn (location, 0, "comparison between pointer and integer");
11463 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11464 || truth_value_p (TREE_CODE (orig_op0)))
11465 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11466 || truth_value_p (TREE_CODE (orig_op1))))
11467 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11468 break;
11470 default:
11471 gcc_unreachable ();
11474 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11475 return error_mark_node;
11477 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11478 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11479 || !vector_types_compatible_elements_p (type0, type1)))
11481 gcc_rich_location richloc (location);
11482 richloc.maybe_add_expr (orig_op0);
11483 richloc.maybe_add_expr (orig_op1);
11484 binary_op_error (&richloc, code, type0, type1);
11485 return error_mark_node;
11488 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11489 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11491 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11492 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11494 bool first_complex = (code0 == COMPLEX_TYPE);
11495 bool second_complex = (code1 == COMPLEX_TYPE);
11496 int none_complex = (!first_complex && !second_complex);
11498 if (shorten || common || short_compare)
11500 result_type = c_common_type (type0, type1);
11501 do_warn_double_promotion (result_type, type0, type1,
11502 "implicit conversion from %qT to %qT "
11503 "to match other operand of binary "
11504 "expression",
11505 location);
11506 if (result_type == error_mark_node)
11507 return error_mark_node;
11510 if (first_complex != second_complex
11511 && (code == PLUS_EXPR
11512 || code == MINUS_EXPR
11513 || code == MULT_EXPR
11514 || (code == TRUNC_DIV_EXPR && first_complex))
11515 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11516 && flag_signed_zeros)
11518 /* An operation on mixed real/complex operands must be
11519 handled specially, but the language-independent code can
11520 more easily optimize the plain complex arithmetic if
11521 -fno-signed-zeros. */
11522 tree real_type = TREE_TYPE (result_type);
11523 tree real, imag;
11524 if (type0 != orig_type0 || type1 != orig_type1)
11526 gcc_assert (may_need_excess_precision && common);
11527 semantic_result_type = c_common_type (orig_type0, orig_type1);
11529 if (first_complex)
11531 if (TREE_TYPE (op0) != result_type)
11532 op0 = convert_and_check (location, result_type, op0);
11533 if (TREE_TYPE (op1) != real_type)
11534 op1 = convert_and_check (location, real_type, op1);
11536 else
11538 if (TREE_TYPE (op0) != real_type)
11539 op0 = convert_and_check (location, real_type, op0);
11540 if (TREE_TYPE (op1) != result_type)
11541 op1 = convert_and_check (location, result_type, op1);
11543 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11544 return error_mark_node;
11545 if (first_complex)
11547 op0 = c_save_expr (op0);
11548 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11549 op0, true);
11550 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11551 op0, true);
11552 switch (code)
11554 case MULT_EXPR:
11555 case TRUNC_DIV_EXPR:
11556 op1 = c_save_expr (op1);
11557 imag = build2 (resultcode, real_type, imag, op1);
11558 /* Fall through. */
11559 case PLUS_EXPR:
11560 case MINUS_EXPR:
11561 real = build2 (resultcode, real_type, real, op1);
11562 break;
11563 default:
11564 gcc_unreachable();
11567 else
11569 op1 = c_save_expr (op1);
11570 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11571 op1, true);
11572 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11573 op1, true);
11574 switch (code)
11576 case MULT_EXPR:
11577 op0 = c_save_expr (op0);
11578 imag = build2 (resultcode, real_type, op0, imag);
11579 /* Fall through. */
11580 case PLUS_EXPR:
11581 real = build2 (resultcode, real_type, op0, real);
11582 break;
11583 case MINUS_EXPR:
11584 real = build2 (resultcode, real_type, op0, real);
11585 imag = build1 (NEGATE_EXPR, real_type, imag);
11586 break;
11587 default:
11588 gcc_unreachable();
11591 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11592 goto return_build_binary_op;
11595 /* For certain operations (which identify themselves by shorten != 0)
11596 if both args were extended from the same smaller type,
11597 do the arithmetic in that type and then extend.
11599 shorten !=0 and !=1 indicates a bitwise operation.
11600 For them, this optimization is safe only if
11601 both args are zero-extended or both are sign-extended.
11602 Otherwise, we might change the result.
11603 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11604 but calculated in (unsigned short) it would be (unsigned short)-1. */
11606 if (shorten && none_complex)
11608 final_type = result_type;
11609 result_type = shorten_binary_op (result_type, op0, op1,
11610 shorten == -1);
11613 /* Shifts can be shortened if shifting right. */
11615 if (short_shift)
11617 int unsigned_arg;
11618 tree arg0 = get_narrower (op0, &unsigned_arg);
11620 final_type = result_type;
11622 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11623 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11625 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11626 && tree_int_cst_sgn (op1) > 0
11627 /* We can shorten only if the shift count is less than the
11628 number of bits in the smaller type size. */
11629 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11630 /* We cannot drop an unsigned shift after sign-extension. */
11631 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11633 /* Do an unsigned shift if the operand was zero-extended. */
11634 result_type
11635 = c_common_signed_or_unsigned_type (unsigned_arg,
11636 TREE_TYPE (arg0));
11637 /* Convert value-to-be-shifted to that type. */
11638 if (TREE_TYPE (op0) != result_type)
11639 op0 = convert (result_type, op0);
11640 converted = 1;
11644 /* Comparison operations are shortened too but differently.
11645 They identify themselves by setting short_compare = 1. */
11647 if (short_compare)
11649 /* Don't write &op0, etc., because that would prevent op0
11650 from being kept in a register.
11651 Instead, make copies of the our local variables and
11652 pass the copies by reference, then copy them back afterward. */
11653 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11654 enum tree_code xresultcode = resultcode;
11655 tree val
11656 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11657 &xresultcode);
11659 if (val != 0)
11661 ret = val;
11662 goto return_build_binary_op;
11665 op0 = xop0, op1 = xop1;
11666 converted = 1;
11667 resultcode = xresultcode;
11669 if (c_inhibit_evaluation_warnings == 0)
11671 bool op0_maybe_const = true;
11672 bool op1_maybe_const = true;
11673 tree orig_op0_folded, orig_op1_folded;
11675 if (in_late_binary_op)
11677 orig_op0_folded = orig_op0;
11678 orig_op1_folded = orig_op1;
11680 else
11682 /* Fold for the sake of possible warnings, as in
11683 build_conditional_expr. This requires the
11684 "original" values to be folded, not just op0 and
11685 op1. */
11686 c_inhibit_evaluation_warnings++;
11687 op0 = c_fully_fold (op0, require_constant_value,
11688 &op0_maybe_const);
11689 op1 = c_fully_fold (op1, require_constant_value,
11690 &op1_maybe_const);
11691 c_inhibit_evaluation_warnings--;
11692 orig_op0_folded = c_fully_fold (orig_op0,
11693 require_constant_value,
11694 NULL);
11695 orig_op1_folded = c_fully_fold (orig_op1,
11696 require_constant_value,
11697 NULL);
11700 if (warn_sign_compare)
11701 warn_for_sign_compare (location, orig_op0_folded,
11702 orig_op1_folded, op0, op1,
11703 result_type, resultcode);
11704 if (!in_late_binary_op && !int_operands)
11706 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11707 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11708 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11709 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11715 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11716 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11717 Then the expression will be built.
11718 It will be given type FINAL_TYPE if that is nonzero;
11719 otherwise, it will be given type RESULT_TYPE. */
11721 if (!result_type)
11723 gcc_rich_location richloc (location);
11724 richloc.maybe_add_expr (orig_op0);
11725 richloc.maybe_add_expr (orig_op1);
11726 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
11727 return error_mark_node;
11730 if (build_type == NULL_TREE)
11732 build_type = result_type;
11733 if ((type0 != orig_type0 || type1 != orig_type1)
11734 && !boolean_op)
11736 gcc_assert (may_need_excess_precision && common);
11737 semantic_result_type = c_common_type (orig_type0, orig_type1);
11741 if (!converted)
11743 op0 = ep_convert_and_check (location, result_type, op0,
11744 semantic_result_type);
11745 op1 = ep_convert_and_check (location, result_type, op1,
11746 semantic_result_type);
11748 /* This can happen if one operand has a vector type, and the other
11749 has a different type. */
11750 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11751 return error_mark_node;
11754 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11755 | SANITIZE_FLOAT_DIVIDE))
11756 && do_ubsan_in_current_function ()
11757 && (doing_div_or_mod || doing_shift)
11758 && !require_constant_value)
11760 /* OP0 and/or OP1 might have side-effects. */
11761 op0 = c_save_expr (op0);
11762 op1 = c_save_expr (op1);
11763 op0 = c_fully_fold (op0, false, NULL);
11764 op1 = c_fully_fold (op1, false, NULL);
11765 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11766 | SANITIZE_FLOAT_DIVIDE)))
11767 instrument_expr = ubsan_instrument_division (location, op0, op1);
11768 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11769 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11772 /* Treat expressions in initializers specially as they can't trap. */
11773 if (int_const_or_overflow)
11774 ret = (require_constant_value
11775 ? fold_build2_initializer_loc (location, resultcode, build_type,
11776 op0, op1)
11777 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11778 else
11779 ret = build2 (resultcode, build_type, op0, op1);
11780 if (final_type != 0)
11781 ret = convert (final_type, ret);
11783 return_build_binary_op:
11784 gcc_assert (ret != error_mark_node);
11785 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11786 ret = (int_operands
11787 ? note_integer_operands (ret)
11788 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11789 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11790 && !in_late_binary_op)
11791 ret = note_integer_operands (ret);
11792 if (semantic_result_type)
11793 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11794 protected_set_expr_location (ret, location);
11796 if (instrument_expr != NULL)
11797 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11798 instrument_expr, ret);
11800 return ret;
11804 /* Convert EXPR to be a truth-value, validating its type for this
11805 purpose. LOCATION is the source location for the expression. */
11807 tree
11808 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11810 bool int_const, int_operands;
11812 switch (TREE_CODE (TREE_TYPE (expr)))
11814 case ARRAY_TYPE:
11815 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11816 return error_mark_node;
11818 case RECORD_TYPE:
11819 error_at (location, "used struct type value where scalar is required");
11820 return error_mark_node;
11822 case UNION_TYPE:
11823 error_at (location, "used union type value where scalar is required");
11824 return error_mark_node;
11826 case VOID_TYPE:
11827 error_at (location, "void value not ignored as it ought to be");
11828 return error_mark_node;
11830 case POINTER_TYPE:
11831 if (reject_gcc_builtin (expr))
11832 return error_mark_node;
11833 break;
11835 case FUNCTION_TYPE:
11836 gcc_unreachable ();
11838 case VECTOR_TYPE:
11839 error_at (location, "used vector type where scalar is required");
11840 return error_mark_node;
11842 default:
11843 break;
11846 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11847 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11848 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11850 expr = remove_c_maybe_const_expr (expr);
11851 expr = build2 (NE_EXPR, integer_type_node, expr,
11852 convert (TREE_TYPE (expr), integer_zero_node));
11853 expr = note_integer_operands (expr);
11855 else
11856 /* ??? Should we also give an error for vectors rather than leaving
11857 those to give errors later? */
11858 expr = c_common_truthvalue_conversion (location, expr);
11860 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11862 if (TREE_OVERFLOW (expr))
11863 return expr;
11864 else
11865 return note_integer_operands (expr);
11867 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11868 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11869 return expr;
11873 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11874 required. */
11876 tree
11877 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11879 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11881 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11882 /* Executing a compound literal inside a function reinitializes
11883 it. */
11884 if (!TREE_STATIC (decl))
11885 *se = true;
11886 return decl;
11888 else
11889 return expr;
11892 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11893 statement. LOC is the location of the construct. */
11895 tree
11896 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11897 tree clauses)
11899 body = c_end_compound_stmt (loc, body, true);
11901 tree stmt = make_node (code);
11902 TREE_TYPE (stmt) = void_type_node;
11903 OMP_BODY (stmt) = body;
11904 OMP_CLAUSES (stmt) = clauses;
11905 SET_EXPR_LOCATION (stmt, loc);
11907 return add_stmt (stmt);
11910 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11911 statement. LOC is the location of the OACC_DATA. */
11913 tree
11914 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11916 tree stmt;
11918 block = c_end_compound_stmt (loc, block, true);
11920 stmt = make_node (OACC_DATA);
11921 TREE_TYPE (stmt) = void_type_node;
11922 OACC_DATA_CLAUSES (stmt) = clauses;
11923 OACC_DATA_BODY (stmt) = block;
11924 SET_EXPR_LOCATION (stmt, loc);
11926 return add_stmt (stmt);
11929 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
11930 statement. LOC is the location of the OACC_HOST_DATA. */
11932 tree
11933 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
11935 tree stmt;
11937 block = c_end_compound_stmt (loc, block, true);
11939 stmt = make_node (OACC_HOST_DATA);
11940 TREE_TYPE (stmt) = void_type_node;
11941 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
11942 OACC_HOST_DATA_BODY (stmt) = block;
11943 SET_EXPR_LOCATION (stmt, loc);
11945 return add_stmt (stmt);
11948 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11950 tree
11951 c_begin_omp_parallel (void)
11953 tree block;
11955 keep_next_level ();
11956 block = c_begin_compound_stmt (true);
11958 return block;
11961 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11962 statement. LOC is the location of the OMP_PARALLEL. */
11964 tree
11965 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11967 tree stmt;
11969 block = c_end_compound_stmt (loc, block, true);
11971 stmt = make_node (OMP_PARALLEL);
11972 TREE_TYPE (stmt) = void_type_node;
11973 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11974 OMP_PARALLEL_BODY (stmt) = block;
11975 SET_EXPR_LOCATION (stmt, loc);
11977 return add_stmt (stmt);
11980 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11982 tree
11983 c_begin_omp_task (void)
11985 tree block;
11987 keep_next_level ();
11988 block = c_begin_compound_stmt (true);
11990 return block;
11993 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11994 statement. LOC is the location of the #pragma. */
11996 tree
11997 c_finish_omp_task (location_t loc, tree clauses, tree block)
11999 tree stmt;
12001 block = c_end_compound_stmt (loc, block, true);
12003 stmt = make_node (OMP_TASK);
12004 TREE_TYPE (stmt) = void_type_node;
12005 OMP_TASK_CLAUSES (stmt) = clauses;
12006 OMP_TASK_BODY (stmt) = block;
12007 SET_EXPR_LOCATION (stmt, loc);
12009 return add_stmt (stmt);
12012 /* Generate GOMP_cancel call for #pragma omp cancel. */
12014 void
12015 c_finish_omp_cancel (location_t loc, tree clauses)
12017 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12018 int mask = 0;
12019 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12020 mask = 1;
12021 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12022 mask = 2;
12023 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12024 mask = 4;
12025 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12026 mask = 8;
12027 else
12029 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12030 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12031 "clauses");
12032 return;
12034 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12035 if (ifc != NULL_TREE)
12037 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12038 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12039 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12040 build_zero_cst (type));
12042 else
12043 ifc = boolean_true_node;
12044 tree stmt = build_call_expr_loc (loc, fn, 2,
12045 build_int_cst (integer_type_node, mask),
12046 ifc);
12047 add_stmt (stmt);
12050 /* Generate GOMP_cancellation_point call for
12051 #pragma omp cancellation point. */
12053 void
12054 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12056 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12057 int mask = 0;
12058 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12059 mask = 1;
12060 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12061 mask = 2;
12062 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12063 mask = 4;
12064 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12065 mask = 8;
12066 else
12068 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12069 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12070 "clauses");
12071 return;
12073 tree stmt = build_call_expr_loc (loc, fn, 1,
12074 build_int_cst (integer_type_node, mask));
12075 add_stmt (stmt);
12078 /* Helper function for handle_omp_array_sections. Called recursively
12079 to handle multiple array-section-subscripts. C is the clause,
12080 T current expression (initially OMP_CLAUSE_DECL), which is either
12081 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12082 expression if specified, TREE_VALUE length expression if specified,
12083 TREE_CHAIN is what it has been specified after, or some decl.
12084 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12085 set to true if any of the array-section-subscript could have length
12086 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12087 first array-section-subscript which is known not to have length
12088 of one. Given say:
12089 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12090 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12091 all are or may have length of 1, array-section-subscript [:2] is the
12092 first one known not to have length 1. For array-section-subscript
12093 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12094 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12095 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12096 case though, as some lengths could be zero. */
12098 static tree
12099 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12100 bool &maybe_zero_len, unsigned int &first_non_one,
12101 enum c_omp_region_type ort)
12103 tree ret, low_bound, length, type;
12104 if (TREE_CODE (t) != TREE_LIST)
12106 if (error_operand_p (t))
12107 return error_mark_node;
12108 ret = t;
12109 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12110 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12112 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12113 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12114 return error_mark_node;
12116 if (TREE_CODE (t) == COMPONENT_REF
12117 && ort == C_ORT_OMP
12118 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12119 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12120 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12122 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12124 error_at (OMP_CLAUSE_LOCATION (c),
12125 "bit-field %qE in %qs clause",
12126 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12127 return error_mark_node;
12129 while (TREE_CODE (t) == COMPONENT_REF)
12131 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12133 error_at (OMP_CLAUSE_LOCATION (c),
12134 "%qE is a member of a union", t);
12135 return error_mark_node;
12137 t = TREE_OPERAND (t, 0);
12140 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12142 if (DECL_P (t))
12143 error_at (OMP_CLAUSE_LOCATION (c),
12144 "%qD is not a variable in %qs clause", t,
12145 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12146 else
12147 error_at (OMP_CLAUSE_LOCATION (c),
12148 "%qE is not a variable in %qs clause", t,
12149 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12150 return error_mark_node;
12152 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12153 && TYPE_ATOMIC (TREE_TYPE (t)))
12155 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12156 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12157 return error_mark_node;
12159 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12160 && VAR_P (t)
12161 && DECL_THREAD_LOCAL_P (t))
12163 error_at (OMP_CLAUSE_LOCATION (c),
12164 "%qD is threadprivate variable in %qs clause", t,
12165 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12166 return error_mark_node;
12168 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12169 && TYPE_ATOMIC (TREE_TYPE (t))
12170 && POINTER_TYPE_P (TREE_TYPE (t)))
12172 /* If the array section is pointer based and the pointer
12173 itself is _Atomic qualified, we need to atomically load
12174 the pointer. */
12175 c_expr expr;
12176 memset (&expr, 0, sizeof (expr));
12177 expr.value = ret;
12178 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12179 expr, false, false);
12180 ret = expr.value;
12182 return ret;
12185 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12186 maybe_zero_len, first_non_one, ort);
12187 if (ret == error_mark_node || ret == NULL_TREE)
12188 return ret;
12190 type = TREE_TYPE (ret);
12191 low_bound = TREE_PURPOSE (t);
12192 length = TREE_VALUE (t);
12194 if (low_bound == error_mark_node || length == error_mark_node)
12195 return error_mark_node;
12197 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12199 error_at (OMP_CLAUSE_LOCATION (c),
12200 "low bound %qE of array section does not have integral type",
12201 low_bound);
12202 return error_mark_node;
12204 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12206 error_at (OMP_CLAUSE_LOCATION (c),
12207 "length %qE of array section does not have integral type",
12208 length);
12209 return error_mark_node;
12211 if (low_bound
12212 && TREE_CODE (low_bound) == INTEGER_CST
12213 && TYPE_PRECISION (TREE_TYPE (low_bound))
12214 > TYPE_PRECISION (sizetype))
12215 low_bound = fold_convert (sizetype, low_bound);
12216 if (length
12217 && TREE_CODE (length) == INTEGER_CST
12218 && TYPE_PRECISION (TREE_TYPE (length))
12219 > TYPE_PRECISION (sizetype))
12220 length = fold_convert (sizetype, length);
12221 if (low_bound == NULL_TREE)
12222 low_bound = integer_zero_node;
12224 if (length != NULL_TREE)
12226 if (!integer_nonzerop (length))
12228 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12229 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12231 if (integer_zerop (length))
12233 error_at (OMP_CLAUSE_LOCATION (c),
12234 "zero length array section in %qs clause",
12235 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12236 return error_mark_node;
12239 else
12240 maybe_zero_len = true;
12242 if (first_non_one == types.length ()
12243 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12244 first_non_one++;
12246 if (TREE_CODE (type) == ARRAY_TYPE)
12248 if (length == NULL_TREE
12249 && (TYPE_DOMAIN (type) == NULL_TREE
12250 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12252 error_at (OMP_CLAUSE_LOCATION (c),
12253 "for unknown bound array type length expression must "
12254 "be specified");
12255 return error_mark_node;
12257 if (TREE_CODE (low_bound) == INTEGER_CST
12258 && tree_int_cst_sgn (low_bound) == -1)
12260 error_at (OMP_CLAUSE_LOCATION (c),
12261 "negative low bound in array section in %qs clause",
12262 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12263 return error_mark_node;
12265 if (length != NULL_TREE
12266 && TREE_CODE (length) == INTEGER_CST
12267 && tree_int_cst_sgn (length) == -1)
12269 error_at (OMP_CLAUSE_LOCATION (c),
12270 "negative length in array section in %qs clause",
12271 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12272 return error_mark_node;
12274 if (TYPE_DOMAIN (type)
12275 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12276 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12277 == INTEGER_CST)
12279 tree size = size_binop (PLUS_EXPR,
12280 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12281 size_one_node);
12282 if (TREE_CODE (low_bound) == INTEGER_CST)
12284 if (tree_int_cst_lt (size, low_bound))
12286 error_at (OMP_CLAUSE_LOCATION (c),
12287 "low bound %qE above array section size "
12288 "in %qs clause", low_bound,
12289 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12290 return error_mark_node;
12292 if (tree_int_cst_equal (size, low_bound))
12294 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12295 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12297 error_at (OMP_CLAUSE_LOCATION (c),
12298 "zero length array section in %qs clause",
12299 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12300 return error_mark_node;
12302 maybe_zero_len = true;
12304 else if (length == NULL_TREE
12305 && first_non_one == types.length ()
12306 && tree_int_cst_equal
12307 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12308 low_bound))
12309 first_non_one++;
12311 else if (length == NULL_TREE)
12313 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12314 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12315 maybe_zero_len = true;
12316 if (first_non_one == types.length ())
12317 first_non_one++;
12319 if (length && TREE_CODE (length) == INTEGER_CST)
12321 if (tree_int_cst_lt (size, length))
12323 error_at (OMP_CLAUSE_LOCATION (c),
12324 "length %qE above array section size "
12325 "in %qs clause", length,
12326 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12327 return error_mark_node;
12329 if (TREE_CODE (low_bound) == INTEGER_CST)
12331 tree lbpluslen
12332 = size_binop (PLUS_EXPR,
12333 fold_convert (sizetype, low_bound),
12334 fold_convert (sizetype, length));
12335 if (TREE_CODE (lbpluslen) == INTEGER_CST
12336 && tree_int_cst_lt (size, lbpluslen))
12338 error_at (OMP_CLAUSE_LOCATION (c),
12339 "high bound %qE above array section size "
12340 "in %qs clause", lbpluslen,
12341 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12342 return error_mark_node;
12347 else if (length == NULL_TREE)
12349 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12350 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12351 maybe_zero_len = true;
12352 if (first_non_one == types.length ())
12353 first_non_one++;
12356 /* For [lb:] we will need to evaluate lb more than once. */
12357 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12359 tree lb = c_save_expr (low_bound);
12360 if (lb != low_bound)
12362 TREE_PURPOSE (t) = lb;
12363 low_bound = lb;
12367 else if (TREE_CODE (type) == POINTER_TYPE)
12369 if (length == NULL_TREE)
12371 error_at (OMP_CLAUSE_LOCATION (c),
12372 "for pointer type length expression must be specified");
12373 return error_mark_node;
12375 if (length != NULL_TREE
12376 && TREE_CODE (length) == INTEGER_CST
12377 && tree_int_cst_sgn (length) == -1)
12379 error_at (OMP_CLAUSE_LOCATION (c),
12380 "negative length in array section in %qs clause",
12381 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12382 return error_mark_node;
12384 /* If there is a pointer type anywhere but in the very first
12385 array-section-subscript, the array section can't be contiguous. */
12386 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12387 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12389 error_at (OMP_CLAUSE_LOCATION (c),
12390 "array section is not contiguous in %qs clause",
12391 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12392 return error_mark_node;
12395 else
12397 error_at (OMP_CLAUSE_LOCATION (c),
12398 "%qE does not have pointer or array type", ret);
12399 return error_mark_node;
12401 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12402 types.safe_push (TREE_TYPE (ret));
12403 /* We will need to evaluate lb more than once. */
12404 tree lb = c_save_expr (low_bound);
12405 if (lb != low_bound)
12407 TREE_PURPOSE (t) = lb;
12408 low_bound = lb;
12410 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12411 return ret;
12414 /* Handle array sections for clause C. */
12416 static bool
12417 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12419 bool maybe_zero_len = false;
12420 unsigned int first_non_one = 0;
12421 auto_vec<tree, 10> types;
12422 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12423 maybe_zero_len, first_non_one,
12424 ort);
12425 if (first == error_mark_node)
12426 return true;
12427 if (first == NULL_TREE)
12428 return false;
12429 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12431 tree t = OMP_CLAUSE_DECL (c);
12432 tree tem = NULL_TREE;
12433 /* Need to evaluate side effects in the length expressions
12434 if any. */
12435 while (TREE_CODE (t) == TREE_LIST)
12437 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12439 if (tem == NULL_TREE)
12440 tem = TREE_VALUE (t);
12441 else
12442 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12443 TREE_VALUE (t), tem);
12445 t = TREE_CHAIN (t);
12447 if (tem)
12448 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12449 first = c_fully_fold (first, false, NULL);
12450 OMP_CLAUSE_DECL (c) = first;
12452 else
12454 unsigned int num = types.length (), i;
12455 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12456 tree condition = NULL_TREE;
12458 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12459 maybe_zero_len = true;
12461 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12462 t = TREE_CHAIN (t))
12464 tree low_bound = TREE_PURPOSE (t);
12465 tree length = TREE_VALUE (t);
12467 i--;
12468 if (low_bound
12469 && TREE_CODE (low_bound) == INTEGER_CST
12470 && TYPE_PRECISION (TREE_TYPE (low_bound))
12471 > TYPE_PRECISION (sizetype))
12472 low_bound = fold_convert (sizetype, low_bound);
12473 if (length
12474 && TREE_CODE (length) == INTEGER_CST
12475 && TYPE_PRECISION (TREE_TYPE (length))
12476 > TYPE_PRECISION (sizetype))
12477 length = fold_convert (sizetype, length);
12478 if (low_bound == NULL_TREE)
12479 low_bound = integer_zero_node;
12480 if (!maybe_zero_len && i > first_non_one)
12482 if (integer_nonzerop (low_bound))
12483 goto do_warn_noncontiguous;
12484 if (length != NULL_TREE
12485 && TREE_CODE (length) == INTEGER_CST
12486 && TYPE_DOMAIN (types[i])
12487 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12488 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12489 == INTEGER_CST)
12491 tree size;
12492 size = size_binop (PLUS_EXPR,
12493 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12494 size_one_node);
12495 if (!tree_int_cst_equal (length, size))
12497 do_warn_noncontiguous:
12498 error_at (OMP_CLAUSE_LOCATION (c),
12499 "array section is not contiguous in %qs "
12500 "clause",
12501 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12502 return true;
12505 if (length != NULL_TREE
12506 && TREE_SIDE_EFFECTS (length))
12508 if (side_effects == NULL_TREE)
12509 side_effects = length;
12510 else
12511 side_effects = build2 (COMPOUND_EXPR,
12512 TREE_TYPE (side_effects),
12513 length, side_effects);
12516 else
12518 tree l;
12520 if (i > first_non_one
12521 && ((length && integer_nonzerop (length))
12522 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12523 continue;
12524 if (length)
12525 l = fold_convert (sizetype, length);
12526 else
12528 l = size_binop (PLUS_EXPR,
12529 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12530 size_one_node);
12531 l = size_binop (MINUS_EXPR, l,
12532 fold_convert (sizetype, low_bound));
12534 if (i > first_non_one)
12536 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12537 size_zero_node);
12538 if (condition == NULL_TREE)
12539 condition = l;
12540 else
12541 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12542 l, condition);
12544 else if (size == NULL_TREE)
12546 size = size_in_bytes (TREE_TYPE (types[i]));
12547 tree eltype = TREE_TYPE (types[num - 1]);
12548 while (TREE_CODE (eltype) == ARRAY_TYPE)
12549 eltype = TREE_TYPE (eltype);
12550 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12552 if (integer_zerop (size)
12553 || integer_zerop (size_in_bytes (eltype)))
12555 error_at (OMP_CLAUSE_LOCATION (c),
12556 "zero length array section in %qs clause",
12557 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12558 return error_mark_node;
12560 size = size_binop (EXACT_DIV_EXPR, size,
12561 size_in_bytes (eltype));
12563 size = size_binop (MULT_EXPR, size, l);
12564 if (condition)
12565 size = fold_build3 (COND_EXPR, sizetype, condition,
12566 size, size_zero_node);
12568 else
12569 size = size_binop (MULT_EXPR, size, l);
12572 if (side_effects)
12573 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12574 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12576 size = size_binop (MINUS_EXPR, size, size_one_node);
12577 size = c_fully_fold (size, false, NULL);
12578 tree index_type = build_index_type (size);
12579 tree eltype = TREE_TYPE (first);
12580 while (TREE_CODE (eltype) == ARRAY_TYPE)
12581 eltype = TREE_TYPE (eltype);
12582 tree type = build_array_type (eltype, index_type);
12583 tree ptype = build_pointer_type (eltype);
12584 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12585 t = build_fold_addr_expr (t);
12586 tree t2 = build_fold_addr_expr (first);
12587 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12588 ptrdiff_type_node, t2);
12589 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12590 ptrdiff_type_node, t2,
12591 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12592 ptrdiff_type_node, t));
12593 t2 = c_fully_fold (t2, false, NULL);
12594 if (tree_fits_shwi_p (t2))
12595 t = build2 (MEM_REF, type, t,
12596 build_int_cst (ptype, tree_to_shwi (t2)));
12597 else
12599 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12600 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12601 TREE_TYPE (t), t, t2);
12602 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12604 OMP_CLAUSE_DECL (c) = t;
12605 return false;
12607 first = c_fully_fold (first, false, NULL);
12608 OMP_CLAUSE_DECL (c) = first;
12609 if (size)
12610 size = c_fully_fold (size, false, NULL);
12611 OMP_CLAUSE_SIZE (c) = size;
12612 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12613 || (TREE_CODE (t) == COMPONENT_REF
12614 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12615 return false;
12616 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12617 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
12618 switch (OMP_CLAUSE_MAP_KIND (c))
12620 case GOMP_MAP_ALLOC:
12621 case GOMP_MAP_TO:
12622 case GOMP_MAP_FROM:
12623 case GOMP_MAP_TOFROM:
12624 case GOMP_MAP_ALWAYS_TO:
12625 case GOMP_MAP_ALWAYS_FROM:
12626 case GOMP_MAP_ALWAYS_TOFROM:
12627 case GOMP_MAP_RELEASE:
12628 case GOMP_MAP_DELETE:
12629 case GOMP_MAP_FORCE_TO:
12630 case GOMP_MAP_FORCE_FROM:
12631 case GOMP_MAP_FORCE_TOFROM:
12632 case GOMP_MAP_FORCE_PRESENT:
12633 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12634 break;
12635 default:
12636 break;
12638 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12639 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
12640 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12641 else if (TREE_CODE (t) == COMPONENT_REF)
12642 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12643 else
12644 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12645 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12646 && !c_mark_addressable (t))
12647 return false;
12648 OMP_CLAUSE_DECL (c2) = t;
12649 t = build_fold_addr_expr (first);
12650 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12651 tree ptr = OMP_CLAUSE_DECL (c2);
12652 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12653 ptr = build_fold_addr_expr (ptr);
12654 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12655 ptrdiff_type_node, t,
12656 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12657 ptrdiff_type_node, ptr));
12658 t = c_fully_fold (t, false, NULL);
12659 OMP_CLAUSE_SIZE (c2) = t;
12660 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12661 OMP_CLAUSE_CHAIN (c) = c2;
12663 return false;
12666 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12667 an inline call. But, remap
12668 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12669 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12671 static tree
12672 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12673 tree decl, tree placeholder)
12675 copy_body_data id;
12676 hash_map<tree, tree> decl_map;
12678 decl_map.put (omp_decl1, placeholder);
12679 decl_map.put (omp_decl2, decl);
12680 memset (&id, 0, sizeof (id));
12681 id.src_fn = DECL_CONTEXT (omp_decl1);
12682 id.dst_fn = current_function_decl;
12683 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12684 id.decl_map = &decl_map;
12686 id.copy_decl = copy_decl_no_change;
12687 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12688 id.transform_new_cfg = true;
12689 id.transform_return_to_modify = false;
12690 id.transform_lang_insert_block = NULL;
12691 id.eh_lp_nr = 0;
12692 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12693 return stmt;
12696 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12697 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12699 static tree
12700 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12702 if (*tp == (tree) data)
12703 return *tp;
12704 return NULL_TREE;
12707 /* For all elements of CLAUSES, validate them against their constraints.
12708 Remove any elements from the list that are invalid. */
12710 tree
12711 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
12713 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12714 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
12715 tree c, t, type, *pc;
12716 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12717 bool branch_seen = false;
12718 bool copyprivate_seen = false;
12719 bool linear_variable_step_check = false;
12720 tree *nowait_clause = NULL;
12721 bool ordered_seen = false;
12722 tree schedule_clause = NULL_TREE;
12723 bool oacc_async = false;
12725 bitmap_obstack_initialize (NULL);
12726 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12727 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12728 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12729 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12730 bitmap_initialize (&map_head, &bitmap_default_obstack);
12731 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12732 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
12734 if (ort & C_ORT_ACC)
12735 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12736 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12738 oacc_async = true;
12739 break;
12742 for (pc = &clauses, c = clauses; c ; c = *pc)
12744 bool remove = false;
12745 bool need_complete = false;
12746 bool need_implicitly_determined = false;
12748 switch (OMP_CLAUSE_CODE (c))
12750 case OMP_CLAUSE_SHARED:
12751 need_implicitly_determined = true;
12752 goto check_dup_generic;
12754 case OMP_CLAUSE_PRIVATE:
12755 need_complete = true;
12756 need_implicitly_determined = true;
12757 goto check_dup_generic;
12759 case OMP_CLAUSE_REDUCTION:
12760 need_implicitly_determined = true;
12761 t = OMP_CLAUSE_DECL (c);
12762 if (TREE_CODE (t) == TREE_LIST)
12764 if (handle_omp_array_sections (c, ort))
12766 remove = true;
12767 break;
12770 t = OMP_CLAUSE_DECL (c);
12772 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
12773 if (t == error_mark_node)
12775 remove = true;
12776 break;
12778 if (oacc_async)
12779 c_mark_addressable (t);
12780 type = TREE_TYPE (t);
12781 if (TREE_CODE (t) == MEM_REF)
12782 type = TREE_TYPE (type);
12783 if (TREE_CODE (type) == ARRAY_TYPE)
12785 tree oatype = type;
12786 gcc_assert (TREE_CODE (t) != MEM_REF);
12787 while (TREE_CODE (type) == ARRAY_TYPE)
12788 type = TREE_TYPE (type);
12789 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12791 error_at (OMP_CLAUSE_LOCATION (c),
12792 "%qD in %<reduction%> clause is a zero size array",
12794 remove = true;
12795 break;
12797 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12798 TYPE_SIZE_UNIT (type));
12799 if (integer_zerop (size))
12801 error_at (OMP_CLAUSE_LOCATION (c),
12802 "%qD in %<reduction%> clause is a zero size array",
12804 remove = true;
12805 break;
12807 size = size_binop (MINUS_EXPR, size, size_one_node);
12808 tree index_type = build_index_type (size);
12809 tree atype = build_array_type (type, index_type);
12810 tree ptype = build_pointer_type (type);
12811 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12812 t = build_fold_addr_expr (t);
12813 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12814 OMP_CLAUSE_DECL (c) = t;
12816 if (TYPE_ATOMIC (type))
12818 error_at (OMP_CLAUSE_LOCATION (c),
12819 "%<_Atomic%> %qE in %<reduction%> clause", t);
12820 remove = true;
12821 break;
12823 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12824 && (FLOAT_TYPE_P (type)
12825 || TREE_CODE (type) == COMPLEX_TYPE))
12827 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12828 const char *r_name = NULL;
12830 switch (r_code)
12832 case PLUS_EXPR:
12833 case MULT_EXPR:
12834 case MINUS_EXPR:
12835 break;
12836 case MIN_EXPR:
12837 if (TREE_CODE (type) == COMPLEX_TYPE)
12838 r_name = "min";
12839 break;
12840 case MAX_EXPR:
12841 if (TREE_CODE (type) == COMPLEX_TYPE)
12842 r_name = "max";
12843 break;
12844 case BIT_AND_EXPR:
12845 r_name = "&";
12846 break;
12847 case BIT_XOR_EXPR:
12848 r_name = "^";
12849 break;
12850 case BIT_IOR_EXPR:
12851 r_name = "|";
12852 break;
12853 case TRUTH_ANDIF_EXPR:
12854 if (FLOAT_TYPE_P (type))
12855 r_name = "&&";
12856 break;
12857 case TRUTH_ORIF_EXPR:
12858 if (FLOAT_TYPE_P (type))
12859 r_name = "||";
12860 break;
12861 default:
12862 gcc_unreachable ();
12864 if (r_name)
12866 error_at (OMP_CLAUSE_LOCATION (c),
12867 "%qE has invalid type for %<reduction(%s)%>",
12868 t, r_name);
12869 remove = true;
12870 break;
12873 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12875 error_at (OMP_CLAUSE_LOCATION (c),
12876 "user defined reduction not found for %qE", t);
12877 remove = true;
12878 break;
12880 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12882 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12883 type = TYPE_MAIN_VARIANT (type);
12884 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12885 VAR_DECL, NULL_TREE, type);
12886 tree decl_placeholder = NULL_TREE;
12887 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12888 DECL_ARTIFICIAL (placeholder) = 1;
12889 DECL_IGNORED_P (placeholder) = 1;
12890 if (TREE_CODE (t) == MEM_REF)
12892 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12893 VAR_DECL, NULL_TREE, type);
12894 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12895 DECL_ARTIFICIAL (decl_placeholder) = 1;
12896 DECL_IGNORED_P (decl_placeholder) = 1;
12898 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12899 c_mark_addressable (placeholder);
12900 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12901 c_mark_addressable (decl_placeholder ? decl_placeholder
12902 : OMP_CLAUSE_DECL (c));
12903 OMP_CLAUSE_REDUCTION_MERGE (c)
12904 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12905 TREE_VEC_ELT (list, 0),
12906 TREE_VEC_ELT (list, 1),
12907 decl_placeholder ? decl_placeholder
12908 : OMP_CLAUSE_DECL (c), placeholder);
12909 OMP_CLAUSE_REDUCTION_MERGE (c)
12910 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12911 void_type_node, NULL_TREE,
12912 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12913 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12914 if (TREE_VEC_LENGTH (list) == 6)
12916 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12917 c_mark_addressable (decl_placeholder ? decl_placeholder
12918 : OMP_CLAUSE_DECL (c));
12919 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12920 c_mark_addressable (placeholder);
12921 tree init = TREE_VEC_ELT (list, 5);
12922 if (init == error_mark_node)
12923 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12924 OMP_CLAUSE_REDUCTION_INIT (c)
12925 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12926 TREE_VEC_ELT (list, 3),
12927 decl_placeholder ? decl_placeholder
12928 : OMP_CLAUSE_DECL (c), placeholder);
12929 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12931 tree v = decl_placeholder ? decl_placeholder : t;
12932 OMP_CLAUSE_REDUCTION_INIT (c)
12933 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12934 OMP_CLAUSE_REDUCTION_INIT (c));
12936 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12937 c_find_omp_placeholder_r,
12938 placeholder, NULL))
12939 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12941 else
12943 tree init;
12944 tree v = decl_placeholder ? decl_placeholder : t;
12945 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
12946 init = build_constructor (TREE_TYPE (v), NULL);
12947 else
12948 init = fold_convert (TREE_TYPE (v), integer_zero_node);
12949 OMP_CLAUSE_REDUCTION_INIT (c)
12950 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
12952 OMP_CLAUSE_REDUCTION_INIT (c)
12953 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12954 void_type_node, NULL_TREE,
12955 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12956 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12958 if (TREE_CODE (t) == MEM_REF)
12960 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
12961 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
12962 != INTEGER_CST)
12964 sorry ("variable length element type in array "
12965 "%<reduction%> clause");
12966 remove = true;
12967 break;
12969 t = TREE_OPERAND (t, 0);
12970 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
12971 t = TREE_OPERAND (t, 0);
12972 if (TREE_CODE (t) == ADDR_EXPR)
12973 t = TREE_OPERAND (t, 0);
12975 goto check_dup_generic_t;
12977 case OMP_CLAUSE_COPYPRIVATE:
12978 copyprivate_seen = true;
12979 if (nowait_clause)
12981 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12982 "%<nowait%> clause must not be used together "
12983 "with %<copyprivate%>");
12984 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12985 nowait_clause = NULL;
12987 goto check_dup_generic;
12989 case OMP_CLAUSE_COPYIN:
12990 t = OMP_CLAUSE_DECL (c);
12991 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
12993 error_at (OMP_CLAUSE_LOCATION (c),
12994 "%qE must be %<threadprivate%> for %<copyin%>", t);
12995 remove = true;
12996 break;
12998 goto check_dup_generic;
13000 case OMP_CLAUSE_LINEAR:
13001 if (ort != C_ORT_OMP_DECLARE_SIMD)
13002 need_implicitly_determined = true;
13003 t = OMP_CLAUSE_DECL (c);
13004 if (ort != C_ORT_OMP_DECLARE_SIMD
13005 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13007 error_at (OMP_CLAUSE_LOCATION (c),
13008 "modifier should not be specified in %<linear%> "
13009 "clause on %<simd%> or %<for%> constructs");
13010 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13012 if (ort & C_ORT_CILK)
13014 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13015 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13016 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13018 error_at (OMP_CLAUSE_LOCATION (c),
13019 "linear clause applied to non-integral, "
13020 "non-floating, non-pointer variable with type %qT",
13021 TREE_TYPE (t));
13022 remove = true;
13023 break;
13026 else
13028 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13029 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13031 error_at (OMP_CLAUSE_LOCATION (c),
13032 "linear clause applied to non-integral non-pointer "
13033 "variable with type %qT", TREE_TYPE (t));
13034 remove = true;
13035 break;
13037 if (TYPE_ATOMIC (TREE_TYPE (t)))
13039 error_at (OMP_CLAUSE_LOCATION (c),
13040 "%<_Atomic%> %qD in %<linear%> clause", t);
13041 remove = true;
13042 break;
13045 if (ort == C_ORT_OMP_DECLARE_SIMD)
13047 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13048 if (TREE_CODE (s) == PARM_DECL)
13050 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13051 /* map_head bitmap is used as uniform_head if
13052 declare_simd. */
13053 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13054 linear_variable_step_check = true;
13055 goto check_dup_generic;
13057 if (TREE_CODE (s) != INTEGER_CST)
13059 error_at (OMP_CLAUSE_LOCATION (c),
13060 "%<linear%> clause step %qE is neither constant "
13061 "nor a parameter", s);
13062 remove = true;
13063 break;
13066 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13068 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13069 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13070 OMP_CLAUSE_DECL (c), s);
13071 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13072 sizetype, fold_convert (sizetype, s),
13073 fold_convert
13074 (sizetype, OMP_CLAUSE_DECL (c)));
13075 if (s == error_mark_node)
13076 s = size_one_node;
13077 OMP_CLAUSE_LINEAR_STEP (c) = s;
13079 else
13080 OMP_CLAUSE_LINEAR_STEP (c)
13081 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13082 goto check_dup_generic;
13084 check_dup_generic:
13085 t = OMP_CLAUSE_DECL (c);
13086 check_dup_generic_t:
13087 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13089 error_at (OMP_CLAUSE_LOCATION (c),
13090 "%qE is not a variable in clause %qs", t,
13091 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13092 remove = true;
13094 else if (ort == C_ORT_ACC
13095 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13097 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13099 error ("%qD appears more than once in reduction clauses", t);
13100 remove = true;
13102 else
13103 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13105 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13106 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13107 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13109 error_at (OMP_CLAUSE_LOCATION (c),
13110 "%qE appears more than once in data clauses", t);
13111 remove = true;
13113 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13114 && bitmap_bit_p (&map_head, DECL_UID (t)))
13116 if (ort == C_ORT_ACC)
13117 error ("%qD appears more than once in data clauses", t);
13118 else
13119 error ("%qD appears both in data and map clauses", t);
13120 remove = true;
13122 else
13123 bitmap_set_bit (&generic_head, DECL_UID (t));
13124 break;
13126 case OMP_CLAUSE_FIRSTPRIVATE:
13127 t = OMP_CLAUSE_DECL (c);
13128 need_complete = true;
13129 need_implicitly_determined = true;
13130 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13132 error_at (OMP_CLAUSE_LOCATION (c),
13133 "%qE is not a variable in clause %<firstprivate%>", t);
13134 remove = true;
13136 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13137 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13139 error_at (OMP_CLAUSE_LOCATION (c),
13140 "%qE appears more than once in data clauses", t);
13141 remove = true;
13143 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13145 if (ort == C_ORT_ACC)
13146 error ("%qD appears more than once in data clauses", t);
13147 else
13148 error ("%qD appears both in data and map clauses", t);
13149 remove = true;
13151 else
13152 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13153 break;
13155 case OMP_CLAUSE_LASTPRIVATE:
13156 t = OMP_CLAUSE_DECL (c);
13157 need_complete = true;
13158 need_implicitly_determined = true;
13159 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13161 error_at (OMP_CLAUSE_LOCATION (c),
13162 "%qE is not a variable in clause %<lastprivate%>", t);
13163 remove = true;
13165 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13166 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13168 error_at (OMP_CLAUSE_LOCATION (c),
13169 "%qE appears more than once in data clauses", t);
13170 remove = true;
13172 else
13173 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13174 break;
13176 case OMP_CLAUSE_ALIGNED:
13177 t = OMP_CLAUSE_DECL (c);
13178 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13180 error_at (OMP_CLAUSE_LOCATION (c),
13181 "%qE is not a variable in %<aligned%> clause", t);
13182 remove = true;
13184 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13185 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13187 error_at (OMP_CLAUSE_LOCATION (c),
13188 "%qE in %<aligned%> clause is neither a pointer nor "
13189 "an array", t);
13190 remove = true;
13192 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13194 error_at (OMP_CLAUSE_LOCATION (c),
13195 "%<_Atomic%> %qD in %<aligned%> clause", t);
13196 remove = true;
13197 break;
13199 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13201 error_at (OMP_CLAUSE_LOCATION (c),
13202 "%qE appears more than once in %<aligned%> clauses",
13204 remove = true;
13206 else
13207 bitmap_set_bit (&aligned_head, DECL_UID (t));
13208 break;
13210 case OMP_CLAUSE_DEPEND:
13211 t = OMP_CLAUSE_DECL (c);
13212 if (t == NULL_TREE)
13214 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13215 == OMP_CLAUSE_DEPEND_SOURCE);
13216 break;
13218 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13220 gcc_assert (TREE_CODE (t) == TREE_LIST);
13221 for (; t; t = TREE_CHAIN (t))
13223 tree decl = TREE_VALUE (t);
13224 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13226 tree offset = TREE_PURPOSE (t);
13227 bool neg = wi::neg_p ((wide_int) offset);
13228 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13229 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13230 neg ? MINUS_EXPR : PLUS_EXPR,
13231 decl, offset);
13232 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13233 sizetype,
13234 fold_convert (sizetype, t2),
13235 fold_convert (sizetype, decl));
13236 if (t2 == error_mark_node)
13238 remove = true;
13239 break;
13241 TREE_PURPOSE (t) = t2;
13244 break;
13246 if (TREE_CODE (t) == TREE_LIST)
13248 if (handle_omp_array_sections (c, ort))
13249 remove = true;
13250 break;
13252 if (t == error_mark_node)
13253 remove = true;
13254 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13256 error_at (OMP_CLAUSE_LOCATION (c),
13257 "%qE is not a variable in %<depend%> clause", t);
13258 remove = true;
13260 else if (!c_mark_addressable (t))
13261 remove = true;
13262 break;
13264 case OMP_CLAUSE_MAP:
13265 case OMP_CLAUSE_TO:
13266 case OMP_CLAUSE_FROM:
13267 case OMP_CLAUSE__CACHE_:
13268 t = OMP_CLAUSE_DECL (c);
13269 if (TREE_CODE (t) == TREE_LIST)
13271 if (handle_omp_array_sections (c, ort))
13272 remove = true;
13273 else
13275 t = OMP_CLAUSE_DECL (c);
13276 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13278 error_at (OMP_CLAUSE_LOCATION (c),
13279 "array section does not have mappable type "
13280 "in %qs clause",
13281 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13282 remove = true;
13284 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13286 error_at (OMP_CLAUSE_LOCATION (c),
13287 "%<_Atomic%> %qE in %qs clause", t,
13288 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13289 remove = true;
13291 while (TREE_CODE (t) == ARRAY_REF)
13292 t = TREE_OPERAND (t, 0);
13293 if (TREE_CODE (t) == COMPONENT_REF
13294 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13296 while (TREE_CODE (t) == COMPONENT_REF)
13297 t = TREE_OPERAND (t, 0);
13298 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13299 break;
13300 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13302 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13303 error ("%qD appears more than once in motion"
13304 " clauses", t);
13305 else if (ort == C_ORT_ACC)
13306 error ("%qD appears more than once in data"
13307 " clauses", t);
13308 else
13309 error ("%qD appears more than once in map"
13310 " clauses", t);
13311 remove = true;
13313 else
13315 bitmap_set_bit (&map_head, DECL_UID (t));
13316 bitmap_set_bit (&map_field_head, DECL_UID (t));
13320 break;
13322 if (t == error_mark_node)
13324 remove = true;
13325 break;
13327 if (TREE_CODE (t) == COMPONENT_REF
13328 && (ort & C_ORT_OMP)
13329 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13331 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13333 error_at (OMP_CLAUSE_LOCATION (c),
13334 "bit-field %qE in %qs clause",
13335 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13336 remove = true;
13338 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13340 error_at (OMP_CLAUSE_LOCATION (c),
13341 "%qE does not have a mappable type in %qs clause",
13342 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13343 remove = true;
13345 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13347 error_at (OMP_CLAUSE_LOCATION (c),
13348 "%<_Atomic%> %qE in %qs clause", t,
13349 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13350 remove = true;
13352 while (TREE_CODE (t) == COMPONENT_REF)
13354 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13355 == UNION_TYPE)
13357 error_at (OMP_CLAUSE_LOCATION (c),
13358 "%qE is a member of a union", t);
13359 remove = true;
13360 break;
13362 t = TREE_OPERAND (t, 0);
13364 if (remove)
13365 break;
13366 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13368 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13369 break;
13372 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13374 error_at (OMP_CLAUSE_LOCATION (c),
13375 "%qE is not a variable in %qs clause", t,
13376 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13377 remove = true;
13379 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13381 error_at (OMP_CLAUSE_LOCATION (c),
13382 "%qD is threadprivate variable in %qs clause", t,
13383 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13384 remove = true;
13386 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13387 || (OMP_CLAUSE_MAP_KIND (c)
13388 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13389 && !c_mark_addressable (t))
13390 remove = true;
13391 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13392 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13393 || (OMP_CLAUSE_MAP_KIND (c)
13394 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13395 || (OMP_CLAUSE_MAP_KIND (c)
13396 == GOMP_MAP_FORCE_DEVICEPTR)))
13397 && t == OMP_CLAUSE_DECL (c)
13398 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13400 error_at (OMP_CLAUSE_LOCATION (c),
13401 "%qD does not have a mappable type in %qs clause", t,
13402 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13403 remove = true;
13405 else if (TREE_TYPE (t) == error_mark_node)
13406 remove = true;
13407 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13409 error_at (OMP_CLAUSE_LOCATION (c),
13410 "%<_Atomic%> %qE in %qs clause", t,
13411 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13412 remove = true;
13414 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13415 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13417 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13418 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13420 error ("%qD appears more than once in data clauses", t);
13421 remove = true;
13423 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13425 if (ort == C_ORT_ACC)
13426 error ("%qD appears more than once in data clauses", t);
13427 else
13428 error ("%qD appears both in data and map clauses", t);
13429 remove = true;
13431 else
13432 bitmap_set_bit (&generic_head, DECL_UID (t));
13434 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13436 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13437 error ("%qD appears more than once in motion clauses", t);
13438 else if (ort == C_ORT_ACC)
13439 error ("%qD appears more than once in data clauses", t);
13440 else
13441 error ("%qD appears more than once in map clauses", t);
13442 remove = true;
13444 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13445 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13447 if (ort == C_ORT_ACC)
13448 error ("%qD appears more than once in data clauses", t);
13449 else
13450 error ("%qD appears both in data and map clauses", t);
13451 remove = true;
13453 else
13455 bitmap_set_bit (&map_head, DECL_UID (t));
13456 if (t != OMP_CLAUSE_DECL (c)
13457 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13458 bitmap_set_bit (&map_field_head, DECL_UID (t));
13460 break;
13462 case OMP_CLAUSE_TO_DECLARE:
13463 case OMP_CLAUSE_LINK:
13464 t = OMP_CLAUSE_DECL (c);
13465 if (TREE_CODE (t) == FUNCTION_DECL
13466 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13468 else if (!VAR_P (t))
13470 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13471 error_at (OMP_CLAUSE_LOCATION (c),
13472 "%qE is neither a variable nor a function name in "
13473 "clause %qs", t,
13474 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13475 else
13476 error_at (OMP_CLAUSE_LOCATION (c),
13477 "%qE is not a variable in clause %qs", t,
13478 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13479 remove = true;
13481 else if (DECL_THREAD_LOCAL_P (t))
13483 error_at (OMP_CLAUSE_LOCATION (c),
13484 "%qD is threadprivate variable in %qs clause", t,
13485 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13486 remove = true;
13488 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13490 error_at (OMP_CLAUSE_LOCATION (c),
13491 "%qD does not have a mappable type in %qs clause", t,
13492 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13493 remove = true;
13495 if (remove)
13496 break;
13497 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13499 error_at (OMP_CLAUSE_LOCATION (c),
13500 "%qE appears more than once on the same "
13501 "%<declare target%> directive", t);
13502 remove = true;
13504 else
13505 bitmap_set_bit (&generic_head, DECL_UID (t));
13506 break;
13508 case OMP_CLAUSE_UNIFORM:
13509 t = OMP_CLAUSE_DECL (c);
13510 if (TREE_CODE (t) != PARM_DECL)
13512 if (DECL_P (t))
13513 error_at (OMP_CLAUSE_LOCATION (c),
13514 "%qD is not an argument in %<uniform%> clause", t);
13515 else
13516 error_at (OMP_CLAUSE_LOCATION (c),
13517 "%qE is not an argument in %<uniform%> clause", t);
13518 remove = true;
13519 break;
13521 /* map_head bitmap is used as uniform_head if declare_simd. */
13522 bitmap_set_bit (&map_head, DECL_UID (t));
13523 goto check_dup_generic;
13525 case OMP_CLAUSE_IS_DEVICE_PTR:
13526 case OMP_CLAUSE_USE_DEVICE_PTR:
13527 t = OMP_CLAUSE_DECL (c);
13528 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13529 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13531 error_at (OMP_CLAUSE_LOCATION (c),
13532 "%qs variable is neither a pointer nor an array",
13533 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13534 remove = true;
13536 goto check_dup_generic;
13538 case OMP_CLAUSE_NOWAIT:
13539 if (copyprivate_seen)
13541 error_at (OMP_CLAUSE_LOCATION (c),
13542 "%<nowait%> clause must not be used together "
13543 "with %<copyprivate%>");
13544 remove = true;
13545 break;
13547 nowait_clause = pc;
13548 pc = &OMP_CLAUSE_CHAIN (c);
13549 continue;
13551 case OMP_CLAUSE_IF:
13552 case OMP_CLAUSE_NUM_THREADS:
13553 case OMP_CLAUSE_NUM_TEAMS:
13554 case OMP_CLAUSE_THREAD_LIMIT:
13555 case OMP_CLAUSE_DEFAULT:
13556 case OMP_CLAUSE_UNTIED:
13557 case OMP_CLAUSE_COLLAPSE:
13558 case OMP_CLAUSE_FINAL:
13559 case OMP_CLAUSE_MERGEABLE:
13560 case OMP_CLAUSE_DEVICE:
13561 case OMP_CLAUSE_DIST_SCHEDULE:
13562 case OMP_CLAUSE_PARALLEL:
13563 case OMP_CLAUSE_FOR:
13564 case OMP_CLAUSE_SECTIONS:
13565 case OMP_CLAUSE_TASKGROUP:
13566 case OMP_CLAUSE_PROC_BIND:
13567 case OMP_CLAUSE_PRIORITY:
13568 case OMP_CLAUSE_GRAINSIZE:
13569 case OMP_CLAUSE_NUM_TASKS:
13570 case OMP_CLAUSE_NOGROUP:
13571 case OMP_CLAUSE_THREADS:
13572 case OMP_CLAUSE_SIMD:
13573 case OMP_CLAUSE_HINT:
13574 case OMP_CLAUSE_DEFAULTMAP:
13575 case OMP_CLAUSE__CILK_FOR_COUNT_:
13576 case OMP_CLAUSE_NUM_GANGS:
13577 case OMP_CLAUSE_NUM_WORKERS:
13578 case OMP_CLAUSE_VECTOR_LENGTH:
13579 case OMP_CLAUSE_ASYNC:
13580 case OMP_CLAUSE_WAIT:
13581 case OMP_CLAUSE_AUTO:
13582 case OMP_CLAUSE_INDEPENDENT:
13583 case OMP_CLAUSE_SEQ:
13584 case OMP_CLAUSE_GANG:
13585 case OMP_CLAUSE_WORKER:
13586 case OMP_CLAUSE_VECTOR:
13587 case OMP_CLAUSE_TILE:
13588 pc = &OMP_CLAUSE_CHAIN (c);
13589 continue;
13591 case OMP_CLAUSE_SCHEDULE:
13592 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13594 const char *p = NULL;
13595 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13597 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13598 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13599 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13600 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13601 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13602 default: gcc_unreachable ();
13604 if (p)
13606 error_at (OMP_CLAUSE_LOCATION (c),
13607 "%<nonmonotonic%> modifier specified for %qs "
13608 "schedule kind", p);
13609 OMP_CLAUSE_SCHEDULE_KIND (c)
13610 = (enum omp_clause_schedule_kind)
13611 (OMP_CLAUSE_SCHEDULE_KIND (c)
13612 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13615 schedule_clause = c;
13616 pc = &OMP_CLAUSE_CHAIN (c);
13617 continue;
13619 case OMP_CLAUSE_ORDERED:
13620 ordered_seen = true;
13621 pc = &OMP_CLAUSE_CHAIN (c);
13622 continue;
13624 case OMP_CLAUSE_SAFELEN:
13625 safelen = c;
13626 pc = &OMP_CLAUSE_CHAIN (c);
13627 continue;
13628 case OMP_CLAUSE_SIMDLEN:
13629 simdlen = c;
13630 pc = &OMP_CLAUSE_CHAIN (c);
13631 continue;
13633 case OMP_CLAUSE_INBRANCH:
13634 case OMP_CLAUSE_NOTINBRANCH:
13635 if (branch_seen)
13637 error_at (OMP_CLAUSE_LOCATION (c),
13638 "%<inbranch%> clause is incompatible with "
13639 "%<notinbranch%>");
13640 remove = true;
13641 break;
13643 branch_seen = true;
13644 pc = &OMP_CLAUSE_CHAIN (c);
13645 continue;
13647 default:
13648 gcc_unreachable ();
13651 if (!remove)
13653 t = OMP_CLAUSE_DECL (c);
13655 if (need_complete)
13657 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13658 if (t == error_mark_node)
13659 remove = true;
13662 if (need_implicitly_determined)
13664 const char *share_name = NULL;
13666 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13667 share_name = "threadprivate";
13668 else switch (c_omp_predetermined_sharing (t))
13670 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13671 break;
13672 case OMP_CLAUSE_DEFAULT_SHARED:
13673 /* const vars may be specified in firstprivate clause. */
13674 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13675 && TREE_READONLY (t))
13676 break;
13677 share_name = "shared";
13678 break;
13679 case OMP_CLAUSE_DEFAULT_PRIVATE:
13680 share_name = "private";
13681 break;
13682 default:
13683 gcc_unreachable ();
13685 if (share_name)
13687 error_at (OMP_CLAUSE_LOCATION (c),
13688 "%qE is predetermined %qs for %qs",
13689 t, share_name,
13690 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13691 remove = true;
13696 if (remove)
13697 *pc = OMP_CLAUSE_CHAIN (c);
13698 else
13699 pc = &OMP_CLAUSE_CHAIN (c);
13702 if (simdlen
13703 && safelen
13704 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13705 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13707 error_at (OMP_CLAUSE_LOCATION (simdlen),
13708 "%<simdlen%> clause value is bigger than "
13709 "%<safelen%> clause value");
13710 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13711 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13714 if (ordered_seen
13715 && schedule_clause
13716 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13717 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13719 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13720 "%<nonmonotonic%> schedule modifier specified together "
13721 "with %<ordered%> clause");
13722 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13723 = (enum omp_clause_schedule_kind)
13724 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13725 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13728 if (linear_variable_step_check)
13729 for (pc = &clauses, c = clauses; c ; c = *pc)
13731 bool remove = false;
13732 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13733 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13734 && !bitmap_bit_p (&map_head,
13735 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13737 error_at (OMP_CLAUSE_LOCATION (c),
13738 "%<linear%> clause step is a parameter %qD not "
13739 "specified in %<uniform%> clause",
13740 OMP_CLAUSE_LINEAR_STEP (c));
13741 remove = true;
13744 if (remove)
13745 *pc = OMP_CLAUSE_CHAIN (c);
13746 else
13747 pc = &OMP_CLAUSE_CHAIN (c);
13750 bitmap_obstack_release (NULL);
13751 return clauses;
13754 /* Return code to initialize DST with a copy constructor from SRC.
13755 C doesn't have copy constructors nor assignment operators, only for
13756 _Atomic vars we need to perform __atomic_load from src into a temporary
13757 followed by __atomic_store of the temporary to dst. */
13759 tree
13760 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
13762 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
13763 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13765 location_t loc = OMP_CLAUSE_LOCATION (clause);
13766 tree type = TREE_TYPE (dst);
13767 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
13768 tree tmp = create_tmp_var (nonatomic_type);
13769 tree tmp_addr = build_fold_addr_expr (tmp);
13770 TREE_ADDRESSABLE (tmp) = 1;
13771 TREE_NO_WARNING (tmp) = 1;
13772 tree src_addr = build_fold_addr_expr (src);
13773 tree dst_addr = build_fold_addr_expr (dst);
13774 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
13775 vec<tree, va_gc> *params;
13776 /* Expansion of a generic atomic load may require an addition
13777 element, so allocate enough to prevent a resize. */
13778 vec_alloc (params, 4);
13780 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13781 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
13782 params->quick_push (src_addr);
13783 params->quick_push (tmp_addr);
13784 params->quick_push (seq_cst);
13785 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13787 vec_alloc (params, 4);
13789 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
13790 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
13791 params->quick_push (dst_addr);
13792 params->quick_push (tmp_addr);
13793 params->quick_push (seq_cst);
13794 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13795 return build2 (COMPOUND_EXPR, void_type_node, load, store);
13798 /* Create a transaction node. */
13800 tree
13801 c_finish_transaction (location_t loc, tree block, int flags)
13803 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13804 if (flags & TM_STMT_ATTR_OUTER)
13805 TRANSACTION_EXPR_OUTER (stmt) = 1;
13806 if (flags & TM_STMT_ATTR_RELAXED)
13807 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13808 return add_stmt (stmt);
13811 /* Make a variant type in the proper way for C/C++, propagating qualifiers
13812 down to the element type of an array. If ORIG_QUAL_TYPE is not
13813 NULL, then it should be used as the qualified type
13814 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13815 preserve information about the typedef name from which an array
13816 type was derived). */
13818 tree
13819 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13820 size_t orig_qual_indirect)
13822 if (type == error_mark_node)
13823 return type;
13825 if (TREE_CODE (type) == ARRAY_TYPE)
13827 tree t;
13828 tree element_type = c_build_qualified_type (TREE_TYPE (type),
13829 type_quals, orig_qual_type,
13830 orig_qual_indirect - 1);
13832 /* See if we already have an identically qualified type. */
13833 if (orig_qual_type && orig_qual_indirect == 0)
13834 t = orig_qual_type;
13835 else
13836 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13838 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13839 && TYPE_NAME (t) == TYPE_NAME (type)
13840 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13841 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13842 TYPE_ATTRIBUTES (type)))
13843 break;
13845 if (!t)
13847 tree domain = TYPE_DOMAIN (type);
13849 t = build_variant_type_copy (type);
13850 TREE_TYPE (t) = element_type;
13852 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13853 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13854 SET_TYPE_STRUCTURAL_EQUALITY (t);
13855 else if (TYPE_CANONICAL (element_type) != element_type
13856 || (domain && TYPE_CANONICAL (domain) != domain))
13858 tree unqualified_canon
13859 = build_array_type (TYPE_CANONICAL (element_type),
13860 domain? TYPE_CANONICAL (domain)
13861 : NULL_TREE);
13862 if (TYPE_REVERSE_STORAGE_ORDER (type))
13864 unqualified_canon
13865 = build_distinct_type_copy (unqualified_canon);
13866 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13868 TYPE_CANONICAL (t)
13869 = c_build_qualified_type (unqualified_canon, type_quals);
13871 else
13872 TYPE_CANONICAL (t) = t;
13874 return t;
13877 /* A restrict-qualified pointer type must be a pointer to object or
13878 incomplete type. Note that the use of POINTER_TYPE_P also allows
13879 REFERENCE_TYPEs, which is appropriate for C++. */
13880 if ((type_quals & TYPE_QUAL_RESTRICT)
13881 && (!POINTER_TYPE_P (type)
13882 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13884 error ("invalid use of %<restrict%>");
13885 type_quals &= ~TYPE_QUAL_RESTRICT;
13888 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13889 ? orig_qual_type
13890 : build_qualified_type (type, type_quals));
13891 /* A variant type does not inherit the list of incomplete vars from the
13892 type main variant. */
13893 if (RECORD_OR_UNION_TYPE_P (var_type)
13894 && TYPE_MAIN_VARIANT (var_type) != var_type)
13895 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13896 return var_type;
13899 /* Build a VA_ARG_EXPR for the C parser. */
13901 tree
13902 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
13904 if (error_operand_p (type))
13905 return error_mark_node;
13906 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13907 order because it takes the address of the expression. */
13908 else if (handled_component_p (expr)
13909 && reverse_storage_order_for_component_p (expr))
13911 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
13912 return error_mark_node;
13914 else if (!COMPLETE_TYPE_P (type))
13916 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
13917 "type %qT", type);
13918 return error_mark_node;
13920 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
13921 warning_at (loc2, OPT_Wc___compat,
13922 "C++ requires promoted type, not enum type, in %<va_arg%>");
13923 return build_va_arg (loc2, expr, type);
13926 /* Return truthvalue of whether T1 is the same tree structure as T2.
13927 Return 1 if they are the same. Return 0 if they are different. */
13929 bool
13930 c_tree_equal (tree t1, tree t2)
13932 enum tree_code code1, code2;
13934 if (t1 == t2)
13935 return true;
13936 if (!t1 || !t2)
13937 return false;
13939 for (code1 = TREE_CODE (t1);
13940 CONVERT_EXPR_CODE_P (code1)
13941 || code1 == NON_LVALUE_EXPR;
13942 code1 = TREE_CODE (t1))
13943 t1 = TREE_OPERAND (t1, 0);
13944 for (code2 = TREE_CODE (t2);
13945 CONVERT_EXPR_CODE_P (code2)
13946 || code2 == NON_LVALUE_EXPR;
13947 code2 = TREE_CODE (t2))
13948 t2 = TREE_OPERAND (t2, 0);
13950 /* They might have become equal now. */
13951 if (t1 == t2)
13952 return true;
13954 if (code1 != code2)
13955 return false;
13957 switch (code1)
13959 case INTEGER_CST:
13960 return wi::eq_p (t1, t2);
13962 case REAL_CST:
13963 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
13965 case STRING_CST:
13966 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13967 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13968 TREE_STRING_LENGTH (t1));
13970 case FIXED_CST:
13971 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13972 TREE_FIXED_CST (t2));
13974 case COMPLEX_CST:
13975 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13976 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13978 case VECTOR_CST:
13979 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13981 case CONSTRUCTOR:
13982 /* We need to do this when determining whether or not two
13983 non-type pointer to member function template arguments
13984 are the same. */
13985 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13986 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13987 return false;
13989 tree field, value;
13990 unsigned int i;
13991 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13993 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13994 if (!c_tree_equal (field, elt2->index)
13995 || !c_tree_equal (value, elt2->value))
13996 return false;
13999 return true;
14001 case TREE_LIST:
14002 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14003 return false;
14004 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14005 return false;
14006 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14008 case SAVE_EXPR:
14009 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14011 case CALL_EXPR:
14013 tree arg1, arg2;
14014 call_expr_arg_iterator iter1, iter2;
14015 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14016 return false;
14017 for (arg1 = first_call_expr_arg (t1, &iter1),
14018 arg2 = first_call_expr_arg (t2, &iter2);
14019 arg1 && arg2;
14020 arg1 = next_call_expr_arg (&iter1),
14021 arg2 = next_call_expr_arg (&iter2))
14022 if (!c_tree_equal (arg1, arg2))
14023 return false;
14024 if (arg1 || arg2)
14025 return false;
14026 return true;
14029 case TARGET_EXPR:
14031 tree o1 = TREE_OPERAND (t1, 0);
14032 tree o2 = TREE_OPERAND (t2, 0);
14034 /* Special case: if either target is an unallocated VAR_DECL,
14035 it means that it's going to be unified with whatever the
14036 TARGET_EXPR is really supposed to initialize, so treat it
14037 as being equivalent to anything. */
14038 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
14039 && !DECL_RTL_SET_P (o1))
14040 /*Nop*/;
14041 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
14042 && !DECL_RTL_SET_P (o2))
14043 /*Nop*/;
14044 else if (!c_tree_equal (o1, o2))
14045 return false;
14047 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14050 case COMPONENT_REF:
14051 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14052 return false;
14053 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14055 case PARM_DECL:
14056 case VAR_DECL:
14057 case CONST_DECL:
14058 case FIELD_DECL:
14059 case FUNCTION_DECL:
14060 case IDENTIFIER_NODE:
14061 case SSA_NAME:
14062 return false;
14064 case TREE_VEC:
14066 unsigned ix;
14067 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14068 return false;
14069 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14070 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14071 TREE_VEC_ELT (t2, ix)))
14072 return false;
14073 return true;
14076 default:
14077 break;
14080 switch (TREE_CODE_CLASS (code1))
14082 case tcc_unary:
14083 case tcc_binary:
14084 case tcc_comparison:
14085 case tcc_expression:
14086 case tcc_vl_exp:
14087 case tcc_reference:
14088 case tcc_statement:
14090 int i, n = TREE_OPERAND_LENGTH (t1);
14092 switch (code1)
14094 case PREINCREMENT_EXPR:
14095 case PREDECREMENT_EXPR:
14096 case POSTINCREMENT_EXPR:
14097 case POSTDECREMENT_EXPR:
14098 n = 1;
14099 break;
14100 case ARRAY_REF:
14101 n = 2;
14102 break;
14103 default:
14104 break;
14107 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14108 && n != TREE_OPERAND_LENGTH (t2))
14109 return false;
14111 for (i = 0; i < n; ++i)
14112 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14113 return false;
14115 return true;
14118 case tcc_type:
14119 return comptypes (t1, t2);
14120 default:
14121 gcc_unreachable ();
14123 /* We can get here with --disable-checking. */
14124 return false;
14127 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14128 spawn-helper and BODY is the newly created body for FNDECL. */
14130 void
14131 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
14133 tree list = alloc_stmt_list ();
14134 tree frame = make_cilk_frame (fndecl);
14135 tree dtor = create_cilk_function_exit (frame, false, true);
14136 add_local_decl (cfun, frame);
14138 DECL_SAVED_TREE (fndecl) = list;
14139 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
14140 frame);
14141 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
14142 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
14144 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
14145 append_to_statement_list (detach_expr, &body_list);
14147 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
14148 body = fold_build_cleanup_point_expr (void_type_node, body);
14150 append_to_statement_list (body, &body_list);
14151 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
14152 body_list, dtor), &list);
14155 /* Returns true when the function declaration FNDECL is implicit,
14156 introduced as a result of a call to an otherwise undeclared
14157 function, and false otherwise. */
14159 bool
14160 c_decl_implicit (const_tree fndecl)
14162 return C_DECL_IMPLICIT (fndecl);