c: Stray inform note with -Waddress [PR106947]
[official-gcc.git] / gcc / c / c-typeck.cc
blob33d1e8439db0d0937d394df2c2ad0a61e3f5e04b
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2022 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 "gomp-constants.h"
50 #include "spellcheck-tree.h"
51 #include "gcc-rich-location.h"
52 #include "stringpool.h"
53 #include "attribs.h"
54 #include "asan.h"
56 /* Possible cases of implicit conversions. Used to select diagnostic messages
57 and control folding initializers in convert_for_assignment. */
58 enum impl_conv {
59 ic_argpass,
60 ic_assign,
61 ic_init,
62 ic_init_const,
63 ic_return
66 /* The level of nesting inside "__alignof__". */
67 int in_alignof;
69 /* The level of nesting inside "sizeof". */
70 int in_sizeof;
72 /* The level of nesting inside "typeof". */
73 int in_typeof;
75 /* True when parsing OpenMP loop expressions. */
76 bool c_in_omp_for;
78 /* The argument of last parsed sizeof expression, only to be tested
79 if expr.original_code == SIZEOF_EXPR. */
80 tree c_last_sizeof_arg;
81 location_t c_last_sizeof_loc;
83 /* Nonzero if we might need to print a "missing braces around
84 initializer" message within this initializer. */
85 static int found_missing_braces;
87 static int require_constant_value;
88 static int require_constant_elements;
90 static bool null_pointer_constant_p (const_tree);
91 static tree qualify_type (tree, tree);
92 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
93 bool *);
94 static int comp_target_types (location_t, tree, tree);
95 static int function_types_compatible_p (const_tree, const_tree, bool *,
96 bool *);
97 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
98 static tree lookup_field (tree, tree);
99 static int convert_arguments (location_t, vec<location_t>, tree,
100 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
101 tree);
102 static tree pointer_diff (location_t, tree, tree, tree *);
103 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
104 enum impl_conv, bool, tree, tree, int,
105 int = 0);
106 static tree valid_compound_expr_initializer (tree, tree);
107 static void push_string (const char *);
108 static void push_member_name (tree);
109 static int spelling_length (void);
110 static char *print_spelling (char *);
111 static void warning_init (location_t, int, const char *);
112 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
113 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
114 bool, struct obstack *);
115 static void output_pending_init_elements (int, struct obstack *);
116 static bool set_designator (location_t, bool, struct obstack *);
117 static void push_range_stack (tree, struct obstack *);
118 static void add_pending_init (location_t, tree, tree, tree, bool,
119 struct obstack *);
120 static void set_nonincremental_init (struct obstack *);
121 static void set_nonincremental_init_from_string (tree, struct obstack *);
122 static tree find_init_member (tree, struct obstack *);
123 static void readonly_warning (tree, enum lvalue_use);
124 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
125 static void record_maybe_used_decl (tree);
126 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
128 /* Return true if EXP is a null pointer constant, false otherwise. */
130 static bool
131 null_pointer_constant_p (const_tree expr)
133 /* This should really operate on c_expr structures, but they aren't
134 yet available everywhere required. */
135 tree type = TREE_TYPE (expr);
137 /* An integer constant expression with the value 0, such an expression
138 cast to type void*, or the predefined constant nullptr, are a null
139 pointer constant. */
140 if (expr == nullptr_node)
141 return true;
143 return (TREE_CODE (expr) == INTEGER_CST
144 && !TREE_OVERFLOW (expr)
145 && integer_zerop (expr)
146 && (INTEGRAL_TYPE_P (type)
147 || (TREE_CODE (type) == POINTER_TYPE
148 && VOID_TYPE_P (TREE_TYPE (type))
149 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
152 /* EXPR may appear in an unevaluated part of an integer constant
153 expression, but not in an evaluated part. Wrap it in a
154 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
155 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
157 static tree
158 note_integer_operands (tree expr)
160 tree ret;
161 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
163 ret = copy_node (expr);
164 TREE_OVERFLOW (ret) = 1;
166 else
168 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
169 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
171 return ret;
174 /* Having checked whether EXPR may appear in an unevaluated part of an
175 integer constant expression and found that it may, remove any
176 C_MAYBE_CONST_EXPR noting this fact and return the resulting
177 expression. */
179 static inline tree
180 remove_c_maybe_const_expr (tree expr)
182 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
183 return C_MAYBE_CONST_EXPR_EXPR (expr);
184 else
185 return expr;
188 \f/* This is a cache to hold if two types are compatible or not. */
190 struct tagged_tu_seen_cache {
191 const struct tagged_tu_seen_cache * next;
192 const_tree t1;
193 const_tree t2;
194 /* The return value of tagged_types_tu_compatible_p if we had seen
195 these two types already. */
196 int val;
199 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
200 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
202 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
203 does not have an incomplete type. (That includes void types.)
204 LOC is the location of the use. */
206 tree
207 require_complete_type (location_t loc, tree value)
209 tree type = TREE_TYPE (value);
211 if (error_operand_p (value))
212 return error_mark_node;
214 /* First, detect a valid value with a complete type. */
215 if (COMPLETE_TYPE_P (type))
216 return value;
218 c_incomplete_type_error (loc, value, type);
219 return error_mark_node;
222 /* Print an error message for invalid use of an incomplete type.
223 VALUE is the expression that was used (or 0 if that isn't known)
224 and TYPE is the type that was invalid. LOC is the location for
225 the error. */
227 void
228 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
230 /* Avoid duplicate error message. */
231 if (TREE_CODE (type) == ERROR_MARK)
232 return;
234 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
235 error_at (loc, "%qD has an incomplete type %qT", value, type);
236 else
238 retry:
239 /* We must print an error message. Be clever about what it says. */
241 switch (TREE_CODE (type))
243 case RECORD_TYPE:
244 case UNION_TYPE:
245 case ENUMERAL_TYPE:
246 break;
248 case VOID_TYPE:
249 error_at (loc, "invalid use of void expression");
250 return;
252 case ARRAY_TYPE:
253 if (TYPE_DOMAIN (type))
255 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
257 error_at (loc, "invalid use of flexible array member");
258 return;
260 type = TREE_TYPE (type);
261 goto retry;
263 error_at (loc, "invalid use of array with unspecified bounds");
264 return;
266 default:
267 gcc_unreachable ();
270 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
271 error_at (loc, "invalid use of undefined type %qT", type);
272 else
273 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
274 error_at (loc, "invalid use of incomplete typedef %qT", type);
278 /* Given a type, apply default promotions wrt unnamed function
279 arguments and return the new type. */
281 tree
282 c_type_promotes_to (tree type)
284 tree ret = NULL_TREE;
286 if (TYPE_MAIN_VARIANT (type) == float_type_node)
287 ret = double_type_node;
288 else if (c_promoting_integer_type_p (type))
290 /* Preserve unsignedness if not really getting any wider. */
291 if (TYPE_UNSIGNED (type)
292 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
293 ret = unsigned_type_node;
294 else
295 ret = integer_type_node;
298 if (ret != NULL_TREE)
299 return (TYPE_ATOMIC (type)
300 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
301 : ret);
303 return type;
306 /* Return true if between two named address spaces, whether there is a superset
307 named address space that encompasses both address spaces. If there is a
308 superset, return which address space is the superset. */
310 static bool
311 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
313 if (as1 == as2)
315 *common = as1;
316 return true;
318 else if (targetm.addr_space.subset_p (as1, as2))
320 *common = as2;
321 return true;
323 else if (targetm.addr_space.subset_p (as2, as1))
325 *common = as1;
326 return true;
328 else
329 return false;
332 /* Return a variant of TYPE which has all the type qualifiers of LIKE
333 as well as those of TYPE. */
335 static tree
336 qualify_type (tree type, tree like)
338 addr_space_t as_type = TYPE_ADDR_SPACE (type);
339 addr_space_t as_like = TYPE_ADDR_SPACE (like);
340 addr_space_t as_common;
342 /* If the two named address spaces are different, determine the common
343 superset address space. If there isn't one, raise an error. */
344 if (!addr_space_superset (as_type, as_like, &as_common))
346 as_common = as_type;
347 error ("%qT and %qT are in disjoint named address spaces",
348 type, like);
351 return c_build_qualified_type (type,
352 TYPE_QUALS_NO_ADDR_SPACE (type)
353 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
354 | ENCODE_QUAL_ADDR_SPACE (as_common));
357 /* Return true iff the given tree T is a variable length array. */
359 bool
360 c_vla_type_p (const_tree t)
362 if (TREE_CODE (t) == ARRAY_TYPE
363 && C_TYPE_VARIABLE_SIZE (t))
364 return true;
365 return false;
368 /* If NTYPE is a type of a non-variadic function with a prototype
369 and OTYPE is a type of a function without a prototype and ATTRS
370 contains attribute format, diagnosess and removes it from ATTRS.
371 Returns the result of build_type_attribute_variant of NTYPE and
372 the (possibly) modified ATTRS. */
374 static tree
375 build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
377 if (!prototype_p (otype)
378 && prototype_p (ntype)
379 && lookup_attribute ("format", attrs))
381 warning_at (input_location, OPT_Wattributes,
382 "%qs attribute cannot be applied to a function that "
383 "does not take variable arguments", "format");
384 attrs = remove_attribute ("format", attrs);
386 return build_type_attribute_variant (ntype, attrs);
389 /* Return the composite type of two compatible types.
391 We assume that comptypes has already been done and returned
392 nonzero; if that isn't so, this may crash. In particular, we
393 assume that qualifiers match. */
395 tree
396 composite_type (tree t1, tree t2)
398 enum tree_code code1;
399 enum tree_code code2;
400 tree attributes;
402 /* Save time if the two types are the same. */
404 if (t1 == t2) return t1;
406 /* If one type is nonsense, use the other. */
407 if (t1 == error_mark_node)
408 return t2;
409 if (t2 == error_mark_node)
410 return t1;
412 code1 = TREE_CODE (t1);
413 code2 = TREE_CODE (t2);
415 /* Merge the attributes. */
416 attributes = targetm.merge_type_attributes (t1, t2);
418 /* If one is an enumerated type and the other is the compatible
419 integer type, the composite type might be either of the two
420 (DR#013 question 3). For consistency, use the enumerated type as
421 the composite type. */
423 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
424 return t1;
425 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
426 return t2;
428 gcc_assert (code1 == code2);
430 switch (code1)
432 case POINTER_TYPE:
433 /* For two pointers, do this recursively on the target type. */
435 tree pointed_to_1 = TREE_TYPE (t1);
436 tree pointed_to_2 = TREE_TYPE (t2);
437 tree target = composite_type (pointed_to_1, pointed_to_2);
438 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
439 t1 = build_type_attribute_variant (t1, attributes);
440 return qualify_type (t1, t2);
443 case ARRAY_TYPE:
445 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
446 int quals;
447 tree unqual_elt;
448 tree d1 = TYPE_DOMAIN (t1);
449 tree d2 = TYPE_DOMAIN (t2);
450 bool d1_variable, d2_variable;
451 bool d1_zero, d2_zero;
452 bool t1_complete, t2_complete;
454 /* We should not have any type quals on arrays at all. */
455 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
456 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
458 t1_complete = COMPLETE_TYPE_P (t1);
459 t2_complete = COMPLETE_TYPE_P (t2);
461 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
462 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
464 d1_variable = (!d1_zero
465 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
466 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
467 d2_variable = (!d2_zero
468 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
469 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
470 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
471 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
473 /* Save space: see if the result is identical to one of the args. */
474 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
475 && (d2_variable || d2_zero || !d1_variable))
476 return build_type_attribute_variant (t1, attributes);
477 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
478 && (d1_variable || d1_zero || !d2_variable))
479 return build_type_attribute_variant (t2, attributes);
481 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
482 return build_type_attribute_variant (t1, attributes);
483 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
484 return build_type_attribute_variant (t2, attributes);
486 /* Merge the element types, and have a size if either arg has
487 one. We may have qualifiers on the element types. To set
488 up TYPE_MAIN_VARIANT correctly, we need to form the
489 composite of the unqualified types and add the qualifiers
490 back at the end. */
491 quals = TYPE_QUALS (strip_array_types (elt));
492 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
493 t1 = build_array_type (unqual_elt,
494 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
495 && (d2_variable
496 || d2_zero
497 || !d1_variable))
498 ? t1
499 : t2));
500 /* Ensure a composite type involving a zero-length array type
501 is a zero-length type not an incomplete type. */
502 if (d1_zero && d2_zero
503 && (t1_complete || t2_complete)
504 && !COMPLETE_TYPE_P (t1))
506 TYPE_SIZE (t1) = bitsize_zero_node;
507 TYPE_SIZE_UNIT (t1) = size_zero_node;
509 t1 = c_build_qualified_type (t1, quals);
510 return build_type_attribute_variant (t1, attributes);
513 case ENUMERAL_TYPE:
514 case RECORD_TYPE:
515 case UNION_TYPE:
516 if (attributes != NULL)
518 /* Try harder not to create a new aggregate type. */
519 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
520 return t1;
521 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
522 return t2;
524 return build_type_attribute_variant (t1, attributes);
526 case FUNCTION_TYPE:
527 /* Function types: prefer the one that specified arg types.
528 If both do, merge the arg types. Also merge the return types. */
530 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
531 tree p1 = TYPE_ARG_TYPES (t1);
532 tree p2 = TYPE_ARG_TYPES (t2);
533 int len;
534 tree newargs, n;
535 int i;
537 /* Save space: see if the result is identical to one of the args. */
538 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
539 return build_functype_attribute_variant (t1, t2, attributes);
540 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
541 return build_functype_attribute_variant (t2, t1, attributes);
543 /* Simple way if one arg fails to specify argument types. */
544 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
546 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
547 t1 = build_type_attribute_variant (t1, attributes);
548 return qualify_type (t1, t2);
550 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
552 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
553 t1 = build_type_attribute_variant (t1, attributes);
554 return qualify_type (t1, t2);
557 /* If both args specify argument types, we must merge the two
558 lists, argument by argument. */
560 for (len = 0, newargs = p1;
561 newargs && newargs != void_list_node;
562 len++, newargs = TREE_CHAIN (newargs))
565 for (i = 0; i < len; i++)
566 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
568 n = newargs;
570 for (; p1 && p1 != void_list_node;
571 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
573 /* A null type means arg type is not specified.
574 Take whatever the other function type has. */
575 if (TREE_VALUE (p1) == NULL_TREE)
577 TREE_VALUE (n) = TREE_VALUE (p2);
578 goto parm_done;
580 if (TREE_VALUE (p2) == NULL_TREE)
582 TREE_VALUE (n) = TREE_VALUE (p1);
583 goto parm_done;
586 /* Given wait (union {union wait *u; int *i} *)
587 and wait (union wait *),
588 prefer union wait * as type of parm. */
589 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
590 && TREE_VALUE (p1) != TREE_VALUE (p2))
592 tree memb;
593 tree mv2 = TREE_VALUE (p2);
594 if (mv2 && mv2 != error_mark_node
595 && TREE_CODE (mv2) != ARRAY_TYPE)
596 mv2 = TYPE_MAIN_VARIANT (mv2);
597 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
598 memb; memb = DECL_CHAIN (memb))
600 tree mv3 = TREE_TYPE (memb);
601 if (mv3 && mv3 != error_mark_node
602 && TREE_CODE (mv3) != ARRAY_TYPE)
603 mv3 = TYPE_MAIN_VARIANT (mv3);
604 if (comptypes (mv3, mv2))
606 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
607 TREE_VALUE (p2));
608 pedwarn (input_location, OPT_Wpedantic,
609 "function types not truly compatible in ISO C");
610 goto parm_done;
614 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
615 && TREE_VALUE (p2) != TREE_VALUE (p1))
617 tree memb;
618 tree mv1 = TREE_VALUE (p1);
619 if (mv1 && mv1 != error_mark_node
620 && TREE_CODE (mv1) != ARRAY_TYPE)
621 mv1 = TYPE_MAIN_VARIANT (mv1);
622 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
623 memb; memb = DECL_CHAIN (memb))
625 tree mv3 = TREE_TYPE (memb);
626 if (mv3 && mv3 != error_mark_node
627 && TREE_CODE (mv3) != ARRAY_TYPE)
628 mv3 = TYPE_MAIN_VARIANT (mv3);
629 if (comptypes (mv3, mv1))
631 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
632 TREE_VALUE (p1));
633 pedwarn (input_location, OPT_Wpedantic,
634 "function types not truly compatible in ISO C");
635 goto parm_done;
639 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
640 parm_done: ;
643 t1 = build_function_type (valtype, newargs);
644 t1 = qualify_type (t1, t2);
646 /* FALLTHRU */
648 default:
649 return build_type_attribute_variant (t1, attributes);
654 /* Return the type of a conditional expression between pointers to
655 possibly differently qualified versions of compatible types.
657 We assume that comp_target_types has already been done and returned
658 nonzero; if that isn't so, this may crash. */
660 static tree
661 common_pointer_type (tree t1, tree t2)
663 tree attributes;
664 tree pointed_to_1, mv1;
665 tree pointed_to_2, mv2;
666 tree target;
667 unsigned target_quals;
668 addr_space_t as1, as2, as_common;
669 int quals1, quals2;
671 /* Save time if the two types are the same. */
673 if (t1 == t2) return t1;
675 /* If one type is nonsense, use the other. */
676 if (t1 == error_mark_node)
677 return t2;
678 if (t2 == error_mark_node)
679 return t1;
681 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
682 && TREE_CODE (t2) == POINTER_TYPE);
684 /* Merge the attributes. */
685 attributes = targetm.merge_type_attributes (t1, t2);
687 /* Find the composite type of the target types, and combine the
688 qualifiers of the two types' targets. Do not lose qualifiers on
689 array element types by taking the TYPE_MAIN_VARIANT. */
690 mv1 = pointed_to_1 = TREE_TYPE (t1);
691 mv2 = pointed_to_2 = TREE_TYPE (t2);
692 if (TREE_CODE (mv1) != ARRAY_TYPE)
693 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
694 if (TREE_CODE (mv2) != ARRAY_TYPE)
695 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
696 target = composite_type (mv1, mv2);
698 /* Strip array types to get correct qualifier for pointers to arrays */
699 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
700 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
702 /* For function types do not merge const qualifiers, but drop them
703 if used inconsistently. The middle-end uses these to mark const
704 and noreturn functions. */
705 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
706 target_quals = (quals1 & quals2);
707 else
708 target_quals = (quals1 | quals2);
710 /* If the two named address spaces are different, determine the common
711 superset address space. This is guaranteed to exist due to the
712 assumption that comp_target_type returned non-zero. */
713 as1 = TYPE_ADDR_SPACE (pointed_to_1);
714 as2 = TYPE_ADDR_SPACE (pointed_to_2);
715 if (!addr_space_superset (as1, as2, &as_common))
716 gcc_unreachable ();
718 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
720 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
721 return build_type_attribute_variant (t1, attributes);
724 /* Return the common type for two arithmetic types under the usual
725 arithmetic conversions. The default conversions have already been
726 applied, and enumerated types converted to their compatible integer
727 types. The resulting type is unqualified and has no attributes.
729 This is the type for the result of most arithmetic operations
730 if the operands have the given two types. */
732 static tree
733 c_common_type (tree t1, tree t2)
735 enum tree_code code1;
736 enum tree_code code2;
738 /* If one type is nonsense, use the other. */
739 if (t1 == error_mark_node)
740 return t2;
741 if (t2 == error_mark_node)
742 return t1;
744 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
745 t1 = TYPE_MAIN_VARIANT (t1);
747 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
748 t2 = TYPE_MAIN_VARIANT (t2);
750 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
752 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
753 t1 = build_type_attribute_variant (t1, attrs);
756 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
758 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
759 t2 = build_type_attribute_variant (t2, attrs);
762 /* Save time if the two types are the same. */
764 if (t1 == t2) return t1;
766 code1 = TREE_CODE (t1);
767 code2 = TREE_CODE (t2);
769 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
770 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
771 || code1 == INTEGER_TYPE);
772 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
773 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
774 || code2 == INTEGER_TYPE);
776 /* When one operand is a decimal float type, the other operand cannot be
777 a generic float type or a complex type. We also disallow vector types
778 here. */
779 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
780 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
782 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
784 error ("cannot mix operands of decimal floating and vector types");
785 return error_mark_node;
787 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
789 error ("cannot mix operands of decimal floating and complex types");
790 return error_mark_node;
792 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
794 error ("cannot mix operands of decimal floating "
795 "and other floating types");
796 return error_mark_node;
800 /* If one type is a vector type, return that type. (How the usual
801 arithmetic conversions apply to the vector types extension is not
802 precisely specified.) */
803 if (code1 == VECTOR_TYPE)
804 return t1;
806 if (code2 == VECTOR_TYPE)
807 return t2;
809 /* If one type is complex, form the common type of the non-complex
810 components, then make that complex. Use T1 or T2 if it is the
811 required type. */
812 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
814 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
815 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
816 tree subtype = c_common_type (subtype1, subtype2);
818 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
819 return t1;
820 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
821 return t2;
822 else
823 return build_complex_type (subtype);
826 /* If only one is real, use it as the result. */
828 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
829 return t1;
831 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
832 return t2;
834 /* If both are real and either are decimal floating point types, use
835 the decimal floating point type with the greater precision. */
837 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
839 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
840 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
841 return dfloat128_type_node;
842 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
843 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
844 return dfloat64_type_node;
845 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
846 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
847 return dfloat32_type_node;
850 /* Deal with fixed-point types. */
851 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
853 unsigned int unsignedp = 0, satp = 0;
854 scalar_mode m1, m2;
855 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
857 m1 = SCALAR_TYPE_MODE (t1);
858 m2 = SCALAR_TYPE_MODE (t2);
860 /* If one input type is saturating, the result type is saturating. */
861 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
862 satp = 1;
864 /* If both fixed-point types are unsigned, the result type is unsigned.
865 When mixing fixed-point and integer types, follow the sign of the
866 fixed-point type.
867 Otherwise, the result type is signed. */
868 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
869 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
870 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
871 && TYPE_UNSIGNED (t1))
872 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
873 && TYPE_UNSIGNED (t2)))
874 unsignedp = 1;
876 /* The result type is signed. */
877 if (unsignedp == 0)
879 /* If the input type is unsigned, we need to convert to the
880 signed type. */
881 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
883 enum mode_class mclass = (enum mode_class) 0;
884 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
885 mclass = MODE_FRACT;
886 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
887 mclass = MODE_ACCUM;
888 else
889 gcc_unreachable ();
890 m1 = as_a <scalar_mode>
891 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
893 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
895 enum mode_class mclass = (enum mode_class) 0;
896 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
897 mclass = MODE_FRACT;
898 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
899 mclass = MODE_ACCUM;
900 else
901 gcc_unreachable ();
902 m2 = as_a <scalar_mode>
903 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
907 if (code1 == FIXED_POINT_TYPE)
909 fbit1 = GET_MODE_FBIT (m1);
910 ibit1 = GET_MODE_IBIT (m1);
912 else
914 fbit1 = 0;
915 /* Signed integers need to subtract one sign bit. */
916 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
919 if (code2 == FIXED_POINT_TYPE)
921 fbit2 = GET_MODE_FBIT (m2);
922 ibit2 = GET_MODE_IBIT (m2);
924 else
926 fbit2 = 0;
927 /* Signed integers need to subtract one sign bit. */
928 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
931 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
932 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
933 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
934 satp);
937 /* Both real or both integers; use the one with greater precision. */
939 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
940 return t1;
941 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
942 return t2;
944 /* Same precision. Prefer long longs to longs to ints when the
945 same precision, following the C99 rules on integer type rank
946 (which are equivalent to the C90 rules for C90 types). */
948 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
949 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
950 return long_long_unsigned_type_node;
952 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
953 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
955 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
956 return long_long_unsigned_type_node;
957 else
958 return long_long_integer_type_node;
961 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
962 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
963 return long_unsigned_type_node;
965 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
966 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
968 /* But preserve unsignedness from the other type,
969 since long cannot hold all the values of an unsigned int. */
970 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
971 return long_unsigned_type_node;
972 else
973 return long_integer_type_node;
976 /* For floating types of the same TYPE_PRECISION (which we here
977 assume means either the same set of values, or sets of values
978 neither a subset of the other, with behavior being undefined in
979 the latter case), follow the rules from TS 18661-3: prefer
980 interchange types _FloatN, then standard types long double,
981 double, float, then extended types _FloatNx. For extended types,
982 check them starting with _Float128x as that seems most consistent
983 in spirit with preferring long double to double; for interchange
984 types, also check in that order for consistency although it's not
985 possible for more than one of them to have the same
986 precision. */
987 tree mv1 = TYPE_MAIN_VARIANT (t1);
988 tree mv2 = TYPE_MAIN_VARIANT (t2);
990 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
991 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
992 return FLOATN_TYPE_NODE (i);
994 /* Likewise, prefer long double to double even if same size. */
995 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
996 return long_double_type_node;
998 /* Likewise, prefer double to float even if same size.
999 We got a couple of embedded targets with 32 bit doubles, and the
1000 pdp11 might have 64 bit floats. */
1001 if (mv1 == double_type_node || mv2 == double_type_node)
1002 return double_type_node;
1004 if (mv1 == float_type_node || mv2 == float_type_node)
1005 return float_type_node;
1007 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1008 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1009 return FLOATNX_TYPE_NODE (i);
1011 /* Otherwise prefer the unsigned one. */
1013 if (TYPE_UNSIGNED (t1))
1014 return t1;
1015 else
1016 return t2;
1019 /* Wrapper around c_common_type that is used by c-common.cc and other
1020 front end optimizations that remove promotions. ENUMERAL_TYPEs
1021 are allowed here and are converted to their compatible integer types.
1022 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1023 preferably a non-Boolean type as the common type. */
1024 tree
1025 common_type (tree t1, tree t2)
1027 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1028 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
1029 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1030 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
1032 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1033 if (TREE_CODE (t1) == BOOLEAN_TYPE
1034 && TREE_CODE (t2) == BOOLEAN_TYPE)
1035 return boolean_type_node;
1037 /* If either type is BOOLEAN_TYPE, then return the other. */
1038 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1039 return t2;
1040 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1041 return t1;
1043 return c_common_type (t1, t2);
1046 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1047 or various other operations. Return 2 if they are compatible
1048 but a warning may be needed if you use them together. */
1051 comptypes (tree type1, tree type2)
1053 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1054 int val;
1056 val = comptypes_internal (type1, type2, NULL, NULL);
1057 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1059 return val;
1062 /* Like comptypes, but if it returns non-zero because enum and int are
1063 compatible, it sets *ENUM_AND_INT_P to true. */
1066 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1068 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1069 int val;
1071 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1072 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1074 return val;
1077 /* Like comptypes, but if it returns nonzero for different types, it
1078 sets *DIFFERENT_TYPES_P to true. */
1081 comptypes_check_different_types (tree type1, tree type2,
1082 bool *different_types_p)
1084 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1085 int val;
1087 val = comptypes_internal (type1, type2, NULL, different_types_p);
1088 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1090 return val;
1093 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1094 or various other operations. Return 2 if they are compatible
1095 but a warning may be needed if you use them together. If
1096 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1097 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1098 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1099 NULL, and the types are compatible but different enough not to be
1100 permitted in C11 typedef redeclarations, then this sets
1101 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1102 false, but may or may not be set if the types are incompatible.
1103 This differs from comptypes, in that we don't free the seen
1104 types. */
1106 static int
1107 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1108 bool *different_types_p)
1110 const_tree t1 = type1;
1111 const_tree t2 = type2;
1112 int attrval, val;
1114 /* Suppress errors caused by previously reported errors. */
1116 if (t1 == t2 || !t1 || !t2
1117 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1118 return 1;
1120 /* Enumerated types are compatible with integer types, but this is
1121 not transitive: two enumerated types in the same translation unit
1122 are compatible with each other only if they are the same type. */
1124 if (TREE_CODE (t1) == ENUMERAL_TYPE
1125 && COMPLETE_TYPE_P (t1)
1126 && TREE_CODE (t2) != ENUMERAL_TYPE)
1128 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1129 if (TREE_CODE (t2) != VOID_TYPE)
1131 if (enum_and_int_p != NULL)
1132 *enum_and_int_p = true;
1133 if (different_types_p != NULL)
1134 *different_types_p = true;
1137 else if (TREE_CODE (t2) == ENUMERAL_TYPE
1138 && COMPLETE_TYPE_P (t2)
1139 && TREE_CODE (t1) != ENUMERAL_TYPE)
1141 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1142 if (TREE_CODE (t1) != VOID_TYPE)
1144 if (enum_and_int_p != NULL)
1145 *enum_and_int_p = true;
1146 if (different_types_p != NULL)
1147 *different_types_p = true;
1151 if (t1 == t2)
1152 return 1;
1154 /* Different classes of types can't be compatible. */
1156 if (TREE_CODE (t1) != TREE_CODE (t2))
1157 return 0;
1159 /* Qualifiers must match. C99 6.7.3p9 */
1161 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1162 return 0;
1164 /* Allow for two different type nodes which have essentially the same
1165 definition. Note that we already checked for equality of the type
1166 qualifiers (just above). */
1168 if (TREE_CODE (t1) != ARRAY_TYPE
1169 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1170 return 1;
1172 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1173 if (!(attrval = comp_type_attributes (t1, t2)))
1174 return 0;
1176 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1177 val = 0;
1179 switch (TREE_CODE (t1))
1181 case INTEGER_TYPE:
1182 case FIXED_POINT_TYPE:
1183 case REAL_TYPE:
1184 /* With these nodes, we can't determine type equivalence by
1185 looking at what is stored in the nodes themselves, because
1186 two nodes might have different TYPE_MAIN_VARIANTs but still
1187 represent the same type. For example, wchar_t and int could
1188 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1189 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1190 and are distinct types. On the other hand, int and the
1191 following typedef
1193 typedef int INT __attribute((may_alias));
1195 have identical properties, different TYPE_MAIN_VARIANTs, but
1196 represent the same type. The canonical type system keeps
1197 track of equivalence in this case, so we fall back on it. */
1198 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1200 case POINTER_TYPE:
1201 /* Do not remove mode information. */
1202 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1203 break;
1204 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1205 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1206 enum_and_int_p, different_types_p));
1207 break;
1209 case FUNCTION_TYPE:
1210 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1211 different_types_p);
1212 break;
1214 case ARRAY_TYPE:
1216 tree d1 = TYPE_DOMAIN (t1);
1217 tree d2 = TYPE_DOMAIN (t2);
1218 bool d1_variable, d2_variable;
1219 bool d1_zero, d2_zero;
1220 val = 1;
1222 /* Target types must match incl. qualifiers. */
1223 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1224 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1225 enum_and_int_p,
1226 different_types_p)) == 0)
1227 return 0;
1229 if (different_types_p != NULL
1230 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1231 *different_types_p = true;
1232 /* Sizes must match unless one is missing or variable. */
1233 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1234 break;
1236 d1_zero = !TYPE_MAX_VALUE (d1);
1237 d2_zero = !TYPE_MAX_VALUE (d2);
1239 d1_variable = (!d1_zero
1240 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1241 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1242 d2_variable = (!d2_zero
1243 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1244 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1245 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1246 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1248 if (different_types_p != NULL
1249 && d1_variable != d2_variable)
1250 *different_types_p = true;
1251 if (d1_variable || d2_variable)
1252 break;
1253 if (d1_zero && d2_zero)
1254 break;
1255 if (d1_zero || d2_zero
1256 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1257 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1258 val = 0;
1260 break;
1263 case ENUMERAL_TYPE:
1264 case RECORD_TYPE:
1265 case UNION_TYPE:
1266 if (val != 1 && !same_translation_unit_p (t1, t2))
1268 tree a1 = TYPE_ATTRIBUTES (t1);
1269 tree a2 = TYPE_ATTRIBUTES (t2);
1271 if (! attribute_list_contained (a1, a2)
1272 && ! attribute_list_contained (a2, a1))
1273 break;
1275 if (attrval != 2)
1276 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1277 different_types_p);
1278 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1279 different_types_p);
1281 break;
1283 case VECTOR_TYPE:
1284 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1285 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1286 enum_and_int_p, different_types_p));
1287 break;
1289 default:
1290 break;
1292 return attrval == 2 && val == 1 ? 2 : val;
1295 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1296 their qualifiers, except for named address spaces. If the pointers point to
1297 different named addresses, then we must determine if one address space is a
1298 subset of the other. */
1300 static int
1301 comp_target_types (location_t location, tree ttl, tree ttr)
1303 int val;
1304 int val_ped;
1305 tree mvl = TREE_TYPE (ttl);
1306 tree mvr = TREE_TYPE (ttr);
1307 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1308 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1309 addr_space_t as_common;
1310 bool enum_and_int_p;
1312 /* Fail if pointers point to incompatible address spaces. */
1313 if (!addr_space_superset (asl, asr, &as_common))
1314 return 0;
1316 /* For pedantic record result of comptypes on arrays before losing
1317 qualifiers on the element type below. */
1318 val_ped = 1;
1320 if (TREE_CODE (mvl) == ARRAY_TYPE
1321 && TREE_CODE (mvr) == ARRAY_TYPE)
1322 val_ped = comptypes (mvl, mvr);
1324 /* Qualifiers on element types of array types that are
1325 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1327 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1328 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1329 : TYPE_MAIN_VARIANT (mvl));
1331 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1332 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1333 : TYPE_MAIN_VARIANT (mvr));
1335 enum_and_int_p = false;
1336 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1338 if (val == 1 && val_ped != 1)
1339 pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1340 "in ISO C before C2X");
1342 if (val == 2)
1343 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1345 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1346 warning_at (location, OPT_Wc___compat,
1347 "pointer target types incompatible in C++");
1349 return val;
1352 /* Subroutines of `comptypes'. */
1354 /* Determine whether two trees derive from the same translation unit.
1355 If the CONTEXT chain ends in a null, that tree's context is still
1356 being parsed, so if two trees have context chains ending in null,
1357 they're in the same translation unit. */
1359 bool
1360 same_translation_unit_p (const_tree t1, const_tree t2)
1362 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1363 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1365 case tcc_declaration:
1366 t1 = DECL_CONTEXT (t1); break;
1367 case tcc_type:
1368 t1 = TYPE_CONTEXT (t1); break;
1369 case tcc_exceptional:
1370 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1371 default: gcc_unreachable ();
1374 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1375 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1377 case tcc_declaration:
1378 t2 = DECL_CONTEXT (t2); break;
1379 case tcc_type:
1380 t2 = TYPE_CONTEXT (t2); break;
1381 case tcc_exceptional:
1382 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1383 default: gcc_unreachable ();
1386 return t1 == t2;
1389 /* Allocate the seen two types, assuming that they are compatible. */
1391 static struct tagged_tu_seen_cache *
1392 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1394 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1395 tu->next = tagged_tu_seen_base;
1396 tu->t1 = t1;
1397 tu->t2 = t2;
1399 tagged_tu_seen_base = tu;
1401 /* The C standard says that two structures in different translation
1402 units are compatible with each other only if the types of their
1403 fields are compatible (among other things). We assume that they
1404 are compatible until proven otherwise when building the cache.
1405 An example where this can occur is:
1406 struct a
1408 struct a *next;
1410 If we are comparing this against a similar struct in another TU,
1411 and did not assume they were compatible, we end up with an infinite
1412 loop. */
1413 tu->val = 1;
1414 return tu;
1417 /* Free the seen types until we get to TU_TIL. */
1419 static void
1420 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1422 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1423 while (tu != tu_til)
1425 const struct tagged_tu_seen_cache *const tu1
1426 = (const struct tagged_tu_seen_cache *) tu;
1427 tu = tu1->next;
1428 XDELETE (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1430 tagged_tu_seen_base = tu_til;
1433 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1434 compatible. If the two types are not the same (which has been
1435 checked earlier), this can only happen when multiple translation
1436 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1437 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1438 comptypes_internal. */
1440 static int
1441 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1442 bool *enum_and_int_p, bool *different_types_p)
1444 tree s1, s2;
1445 bool needs_warning = false;
1447 /* We have to verify that the tags of the types are the same. This
1448 is harder than it looks because this may be a typedef, so we have
1449 to go look at the original type. It may even be a typedef of a
1450 typedef...
1451 In the case of compiler-created builtin structs the TYPE_DECL
1452 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1453 while (TYPE_NAME (t1)
1454 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1455 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1456 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1458 while (TYPE_NAME (t2)
1459 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1460 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1461 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1463 /* C90 didn't have the requirement that the two tags be the same. */
1464 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1465 return 0;
1467 /* C90 didn't say what happened if one or both of the types were
1468 incomplete; we choose to follow C99 rules here, which is that they
1469 are compatible. */
1470 if (TYPE_SIZE (t1) == NULL
1471 || TYPE_SIZE (t2) == NULL)
1472 return 1;
1475 const struct tagged_tu_seen_cache * tts_i;
1476 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1477 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1478 return tts_i->val;
1481 switch (TREE_CODE (t1))
1483 case ENUMERAL_TYPE:
1485 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1486 /* Speed up the case where the type values are in the same order. */
1487 tree tv1 = TYPE_VALUES (t1);
1488 tree tv2 = TYPE_VALUES (t2);
1490 if (tv1 == tv2)
1492 return 1;
1495 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1497 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1498 break;
1499 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1501 tu->val = 0;
1502 return 0;
1506 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1508 return 1;
1510 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1512 tu->val = 0;
1513 return 0;
1516 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1518 tu->val = 0;
1519 return 0;
1522 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1524 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1525 if (s2 == NULL
1526 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1528 tu->val = 0;
1529 return 0;
1532 return 1;
1535 case UNION_TYPE:
1537 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1538 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1540 tu->val = 0;
1541 return 0;
1544 /* Speed up the common case where the fields are in the same order. */
1545 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1546 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1548 int result;
1550 if (DECL_NAME (s1) != DECL_NAME (s2))
1551 break;
1552 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1553 enum_and_int_p, different_types_p);
1555 if (result != 1 && !DECL_NAME (s1))
1556 break;
1557 if (result == 0)
1559 tu->val = 0;
1560 return 0;
1562 if (result == 2)
1563 needs_warning = true;
1565 if (TREE_CODE (s1) == FIELD_DECL
1566 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1567 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1569 tu->val = 0;
1570 return 0;
1573 if (!s1 && !s2)
1575 tu->val = needs_warning ? 2 : 1;
1576 return tu->val;
1579 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1581 bool ok = false;
1583 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1584 if (DECL_NAME (s1) == DECL_NAME (s2))
1586 int result;
1588 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1589 enum_and_int_p,
1590 different_types_p);
1592 if (result != 1 && !DECL_NAME (s1))
1593 continue;
1594 if (result == 0)
1596 tu->val = 0;
1597 return 0;
1599 if (result == 2)
1600 needs_warning = true;
1602 if (TREE_CODE (s1) == FIELD_DECL
1603 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1604 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1605 break;
1607 ok = true;
1608 break;
1610 if (!ok)
1612 tu->val = 0;
1613 return 0;
1616 tu->val = needs_warning ? 2 : 10;
1617 return tu->val;
1620 case RECORD_TYPE:
1622 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1624 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1625 s1 && s2;
1626 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1628 int result;
1629 if (TREE_CODE (s1) != TREE_CODE (s2)
1630 || DECL_NAME (s1) != DECL_NAME (s2))
1631 break;
1632 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1633 enum_and_int_p, different_types_p);
1634 if (result == 0)
1635 break;
1636 if (result == 2)
1637 needs_warning = true;
1639 if (TREE_CODE (s1) == FIELD_DECL
1640 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1641 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1642 break;
1644 if (s1 && s2)
1645 tu->val = 0;
1646 else
1647 tu->val = needs_warning ? 2 : 1;
1648 return tu->val;
1651 default:
1652 gcc_unreachable ();
1656 /* Return 1 if two function types F1 and F2 are compatible.
1657 If either type specifies no argument types,
1658 the other must specify a fixed number of self-promoting arg types.
1659 Otherwise, if one type specifies only the number of arguments,
1660 the other must specify that number of self-promoting arg types.
1661 Otherwise, the argument types must match.
1662 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1664 static int
1665 function_types_compatible_p (const_tree f1, const_tree f2,
1666 bool *enum_and_int_p, bool *different_types_p)
1668 tree args1, args2;
1669 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1670 int val = 1;
1671 int val1;
1672 tree ret1, ret2;
1674 ret1 = TREE_TYPE (f1);
1675 ret2 = TREE_TYPE (f2);
1677 /* 'volatile' qualifiers on a function's return type used to mean
1678 the function is noreturn. */
1679 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1680 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1681 if (TYPE_VOLATILE (ret1))
1682 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1683 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1684 if (TYPE_VOLATILE (ret2))
1685 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1686 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1687 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1688 if (val == 0)
1689 return 0;
1691 args1 = TYPE_ARG_TYPES (f1);
1692 args2 = TYPE_ARG_TYPES (f2);
1694 if (different_types_p != NULL
1695 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1696 *different_types_p = true;
1698 /* An unspecified parmlist matches any specified parmlist
1699 whose argument types don't need default promotions. */
1701 if (args1 == NULL_TREE)
1703 if (!self_promoting_args_p (args2))
1704 return 0;
1705 /* If one of these types comes from a non-prototype fn definition,
1706 compare that with the other type's arglist.
1707 If they don't match, ask for a warning (but no error). */
1708 if (TYPE_ACTUAL_ARG_TYPES (f1)
1709 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1710 enum_and_int_p, different_types_p) != 1)
1711 val = 2;
1712 return val;
1714 if (args2 == NULL_TREE)
1716 if (!self_promoting_args_p (args1))
1717 return 0;
1718 if (TYPE_ACTUAL_ARG_TYPES (f2)
1719 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1720 enum_and_int_p, different_types_p) != 1)
1721 val = 2;
1722 return val;
1725 /* Both types have argument lists: compare them and propagate results. */
1726 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1727 different_types_p);
1728 return val1 != 1 ? val1 : val;
1731 /* Check two lists of types for compatibility, returning 0 for
1732 incompatible, 1 for compatible, or 2 for compatible with
1733 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1734 comptypes_internal. */
1736 static int
1737 type_lists_compatible_p (const_tree args1, const_tree args2,
1738 bool *enum_and_int_p, bool *different_types_p)
1740 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1741 int val = 1;
1742 int newval = 0;
1744 while (1)
1746 tree a1, mv1, a2, mv2;
1747 if (args1 == NULL_TREE && args2 == NULL_TREE)
1748 return val;
1749 /* If one list is shorter than the other,
1750 they fail to match. */
1751 if (args1 == NULL_TREE || args2 == NULL_TREE)
1752 return 0;
1753 mv1 = a1 = TREE_VALUE (args1);
1754 mv2 = a2 = TREE_VALUE (args2);
1755 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1756 mv1 = (TYPE_ATOMIC (mv1)
1757 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1758 TYPE_QUAL_ATOMIC)
1759 : TYPE_MAIN_VARIANT (mv1));
1760 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1761 mv2 = (TYPE_ATOMIC (mv2)
1762 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1763 TYPE_QUAL_ATOMIC)
1764 : TYPE_MAIN_VARIANT (mv2));
1765 /* A null pointer instead of a type
1766 means there is supposed to be an argument
1767 but nothing is specified about what type it has.
1768 So match anything that self-promotes. */
1769 if (different_types_p != NULL
1770 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1771 *different_types_p = true;
1772 if (a1 == NULL_TREE)
1774 if (c_type_promotes_to (a2) != a2)
1775 return 0;
1777 else if (a2 == NULL_TREE)
1779 if (c_type_promotes_to (a1) != a1)
1780 return 0;
1782 /* If one of the lists has an error marker, ignore this arg. */
1783 else if (TREE_CODE (a1) == ERROR_MARK
1784 || TREE_CODE (a2) == ERROR_MARK)
1786 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1787 different_types_p)))
1789 if (different_types_p != NULL)
1790 *different_types_p = true;
1791 /* Allow wait (union {union wait *u; int *i} *)
1792 and wait (union wait *) to be compatible. */
1793 if (TREE_CODE (a1) == UNION_TYPE
1794 && (TYPE_NAME (a1) == NULL_TREE
1795 || TYPE_TRANSPARENT_AGGR (a1))
1796 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1797 && tree_int_cst_equal (TYPE_SIZE (a1),
1798 TYPE_SIZE (a2)))
1800 tree memb;
1801 for (memb = TYPE_FIELDS (a1);
1802 memb; memb = DECL_CHAIN (memb))
1804 tree mv3 = TREE_TYPE (memb);
1805 if (mv3 && mv3 != error_mark_node
1806 && TREE_CODE (mv3) != ARRAY_TYPE)
1807 mv3 = (TYPE_ATOMIC (mv3)
1808 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1809 TYPE_QUAL_ATOMIC)
1810 : TYPE_MAIN_VARIANT (mv3));
1811 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1812 different_types_p))
1813 break;
1815 if (memb == NULL_TREE)
1816 return 0;
1818 else if (TREE_CODE (a2) == UNION_TYPE
1819 && (TYPE_NAME (a2) == NULL_TREE
1820 || TYPE_TRANSPARENT_AGGR (a2))
1821 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1822 && tree_int_cst_equal (TYPE_SIZE (a2),
1823 TYPE_SIZE (a1)))
1825 tree memb;
1826 for (memb = TYPE_FIELDS (a2);
1827 memb; memb = DECL_CHAIN (memb))
1829 tree mv3 = TREE_TYPE (memb);
1830 if (mv3 && mv3 != error_mark_node
1831 && TREE_CODE (mv3) != ARRAY_TYPE)
1832 mv3 = (TYPE_ATOMIC (mv3)
1833 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1834 TYPE_QUAL_ATOMIC)
1835 : TYPE_MAIN_VARIANT (mv3));
1836 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1837 different_types_p))
1838 break;
1840 if (memb == NULL_TREE)
1841 return 0;
1843 else
1844 return 0;
1847 /* comptypes said ok, but record if it said to warn. */
1848 if (newval > val)
1849 val = newval;
1851 args1 = TREE_CHAIN (args1);
1852 args2 = TREE_CHAIN (args2);
1856 /* Compute the size to increment a pointer by. When a function type or void
1857 type or incomplete type is passed, size_one_node is returned.
1858 This function does not emit any diagnostics; the caller is responsible
1859 for that. */
1861 static tree
1862 c_size_in_bytes (const_tree type)
1864 enum tree_code code = TREE_CODE (type);
1866 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1867 || !COMPLETE_TYPE_P (type))
1868 return size_one_node;
1870 /* Convert in case a char is more than one unit. */
1871 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1872 size_int (TYPE_PRECISION (char_type_node)
1873 / BITS_PER_UNIT));
1876 /* Return either DECL or its known constant value (if it has one). */
1878 tree
1879 decl_constant_value_1 (tree decl, bool in_init)
1881 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1882 TREE_CODE (decl) != PARM_DECL
1883 && !TREE_THIS_VOLATILE (decl)
1884 && TREE_READONLY (decl)
1885 && DECL_INITIAL (decl) != NULL_TREE
1886 && !error_operand_p (DECL_INITIAL (decl))
1887 /* This is invalid if initial value is not constant.
1888 If it has either a function call, a memory reference,
1889 or a variable, then re-evaluating it could give different results. */
1890 && TREE_CONSTANT (DECL_INITIAL (decl))
1891 /* Check for cases where this is sub-optimal, even though valid. */
1892 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1893 return DECL_INITIAL (decl);
1894 return decl;
1897 /* Return either DECL or its known constant value (if it has one).
1898 Like the above, but always return decl outside of functions. */
1900 tree
1901 decl_constant_value (tree decl)
1903 /* Don't change a variable array bound or initial value to a constant
1904 in a place where a variable is invalid. */
1905 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1908 /* Convert the array expression EXP to a pointer. */
1909 static tree
1910 array_to_pointer_conversion (location_t loc, tree exp)
1912 tree orig_exp = exp;
1913 tree type = TREE_TYPE (exp);
1914 tree adr;
1915 tree restype = TREE_TYPE (type);
1916 tree ptrtype;
1918 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1920 STRIP_TYPE_NOPS (exp);
1922 copy_warning (exp, orig_exp);
1924 ptrtype = build_pointer_type (restype);
1926 if (INDIRECT_REF_P (exp))
1927 return convert (ptrtype, TREE_OPERAND (exp, 0));
1929 /* In C++ array compound literals are temporary objects unless they are
1930 const or appear in namespace scope, so they are destroyed too soon
1931 to use them for much of anything (c++/53220). */
1932 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1934 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1935 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1936 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1937 "converting an array compound literal to a pointer "
1938 "is ill-formed in C++");
1941 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1942 return convert (ptrtype, adr);
1945 /* Convert the function expression EXP to a pointer. */
1946 static tree
1947 function_to_pointer_conversion (location_t loc, tree exp)
1949 tree orig_exp = exp;
1951 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1953 STRIP_TYPE_NOPS (exp);
1955 copy_warning (exp, orig_exp);
1957 return build_unary_op (loc, ADDR_EXPR, exp, false);
1960 /* Mark EXP as read, not just set, for set but not used -Wunused
1961 warning purposes. */
1963 void
1964 mark_exp_read (tree exp)
1966 switch (TREE_CODE (exp))
1968 case VAR_DECL:
1969 case PARM_DECL:
1970 DECL_READ_P (exp) = 1;
1971 break;
1972 case ARRAY_REF:
1973 case COMPONENT_REF:
1974 case MODIFY_EXPR:
1975 case REALPART_EXPR:
1976 case IMAGPART_EXPR:
1977 CASE_CONVERT:
1978 case ADDR_EXPR:
1979 case VIEW_CONVERT_EXPR:
1980 mark_exp_read (TREE_OPERAND (exp, 0));
1981 break;
1982 case COMPOUND_EXPR:
1983 /* Pattern match what build_atomic_assign produces with modifycode
1984 NOP_EXPR. */
1985 if (VAR_P (TREE_OPERAND (exp, 1))
1986 && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
1987 && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
1989 tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1990 tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
1991 if (TREE_CODE (t1) == TARGET_EXPR
1992 && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
1993 && TREE_CODE (t2) == CALL_EXPR)
1995 tree fndecl = get_callee_fndecl (t2);
1996 tree arg = NULL_TREE;
1997 if (fndecl
1998 && TREE_CODE (fndecl) == FUNCTION_DECL
1999 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
2000 && call_expr_nargs (t2) >= 2)
2001 switch (DECL_FUNCTION_CODE (fndecl))
2003 case BUILT_IN_ATOMIC_STORE:
2004 arg = CALL_EXPR_ARG (t2, 1);
2005 break;
2006 case BUILT_IN_ATOMIC_STORE_1:
2007 case BUILT_IN_ATOMIC_STORE_2:
2008 case BUILT_IN_ATOMIC_STORE_4:
2009 case BUILT_IN_ATOMIC_STORE_8:
2010 case BUILT_IN_ATOMIC_STORE_16:
2011 arg = CALL_EXPR_ARG (t2, 0);
2012 break;
2013 default:
2014 break;
2016 if (arg)
2018 STRIP_NOPS (arg);
2019 if (TREE_CODE (arg) == ADDR_EXPR
2020 && DECL_P (TREE_OPERAND (arg, 0))
2021 && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2022 mark_exp_read (TREE_OPERAND (arg, 0));
2026 /* FALLTHRU */
2027 case C_MAYBE_CONST_EXPR:
2028 mark_exp_read (TREE_OPERAND (exp, 1));
2029 break;
2030 default:
2031 break;
2035 /* Perform the default conversion of arrays and functions to pointers.
2036 Return the result of converting EXP. For any other expression, just
2037 return EXP.
2039 LOC is the location of the expression. */
2041 struct c_expr
2042 default_function_array_conversion (location_t loc, struct c_expr exp)
2044 tree orig_exp = exp.value;
2045 tree type = TREE_TYPE (exp.value);
2046 enum tree_code code = TREE_CODE (type);
2048 switch (code)
2050 case ARRAY_TYPE:
2052 bool not_lvalue = false;
2053 bool lvalue_array_p;
2055 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2056 || CONVERT_EXPR_P (exp.value))
2057 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2059 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2060 not_lvalue = true;
2061 exp.value = TREE_OPERAND (exp.value, 0);
2064 copy_warning (exp.value, orig_exp);
2066 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2067 if (!flag_isoc99 && !lvalue_array_p)
2069 /* Before C99, non-lvalue arrays do not decay to pointers.
2070 Normally, using such an array would be invalid; but it can
2071 be used correctly inside sizeof or as a statement expression.
2072 Thus, do not give an error here; an error will result later. */
2073 return exp;
2076 exp.value = array_to_pointer_conversion (loc, exp.value);
2078 break;
2079 case FUNCTION_TYPE:
2080 exp.value = function_to_pointer_conversion (loc, exp.value);
2081 break;
2082 default:
2083 break;
2086 return exp;
2089 struct c_expr
2090 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2092 mark_exp_read (exp.value);
2093 return default_function_array_conversion (loc, exp);
2096 /* Return whether EXPR should be treated as an atomic lvalue for the
2097 purposes of load and store handling. */
2099 static bool
2100 really_atomic_lvalue (tree expr)
2102 if (error_operand_p (expr))
2103 return false;
2104 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2105 return false;
2106 if (!lvalue_p (expr))
2107 return false;
2109 /* Ignore _Atomic on register variables, since their addresses can't
2110 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2111 sequences wouldn't work. Ignore _Atomic on structures containing
2112 bit-fields, since accessing elements of atomic structures or
2113 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2114 it's undefined at translation time or execution time, and the
2115 normal atomic sequences again wouldn't work. */
2116 while (handled_component_p (expr))
2118 if (TREE_CODE (expr) == COMPONENT_REF
2119 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2120 return false;
2121 expr = TREE_OPERAND (expr, 0);
2123 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2124 return false;
2125 return true;
2128 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2129 including converting functions and arrays to pointers if CONVERT_P.
2130 If READ_P, also mark the expression as having been read. */
2132 struct c_expr
2133 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2134 bool convert_p, bool read_p)
2136 if (read_p)
2137 mark_exp_read (exp.value);
2138 if (convert_p)
2139 exp = default_function_array_conversion (loc, exp);
2140 if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2141 exp.value = require_complete_type (loc, exp.value);
2142 if (really_atomic_lvalue (exp.value))
2144 vec<tree, va_gc> *params;
2145 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2146 tree expr_type = TREE_TYPE (exp.value);
2147 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2148 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2150 gcc_assert (TYPE_ATOMIC (expr_type));
2152 /* Expansion of a generic atomic load may require an addition
2153 element, so allocate enough to prevent a resize. */
2154 vec_alloc (params, 4);
2156 /* Remove the qualifiers for the rest of the expressions and
2157 create the VAL temp variable to hold the RHS. */
2158 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2159 tmp = create_tmp_var_raw (nonatomic_type);
2160 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2161 TREE_ADDRESSABLE (tmp) = 1;
2162 /* Do not disable warnings for TMP even though it's artificial.
2163 -Winvalid-memory-model depends on it. */
2165 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2166 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2167 params->quick_push (expr_addr);
2168 params->quick_push (tmp_addr);
2169 params->quick_push (seq_cst);
2170 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2172 /* EXPR is always read. */
2173 mark_exp_read (exp.value);
2175 /* Return tmp which contains the value loaded. */
2176 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2177 NULL_TREE, NULL_TREE);
2179 if (convert_p && !error_operand_p (exp.value)
2180 && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2181 exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2182 return exp;
2185 /* EXP is an expression of integer type. Apply the integer promotions
2186 to it and return the promoted value. */
2188 tree
2189 perform_integral_promotions (tree exp)
2191 tree type = TREE_TYPE (exp);
2192 enum tree_code code = TREE_CODE (type);
2194 gcc_assert (INTEGRAL_TYPE_P (type));
2196 /* Normally convert enums to int,
2197 but convert wide enums to something wider. */
2198 if (code == ENUMERAL_TYPE)
2200 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2201 TYPE_PRECISION (integer_type_node)),
2202 ((TYPE_PRECISION (type)
2203 >= TYPE_PRECISION (integer_type_node))
2204 && TYPE_UNSIGNED (type)));
2206 return convert (type, exp);
2209 /* ??? This should no longer be needed now bit-fields have their
2210 proper types. */
2211 if (TREE_CODE (exp) == COMPONENT_REF
2212 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2213 /* If it's thinner than an int, promote it like a
2214 c_promoting_integer_type_p, otherwise leave it alone. */
2215 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2216 TYPE_PRECISION (integer_type_node)) < 0)
2217 return convert (integer_type_node, exp);
2219 if (c_promoting_integer_type_p (type))
2221 /* Preserve unsignedness if not really getting any wider. */
2222 if (TYPE_UNSIGNED (type)
2223 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2224 return convert (unsigned_type_node, exp);
2226 return convert (integer_type_node, exp);
2229 return exp;
2233 /* Perform default promotions for C data used in expressions.
2234 Enumeral types or short or char are converted to int.
2235 In addition, manifest constants symbols are replaced by their values. */
2237 tree
2238 default_conversion (tree exp)
2240 tree orig_exp;
2241 tree type = TREE_TYPE (exp);
2242 enum tree_code code = TREE_CODE (type);
2243 tree promoted_type;
2245 mark_exp_read (exp);
2247 /* Functions and arrays have been converted during parsing. */
2248 gcc_assert (code != FUNCTION_TYPE);
2249 if (code == ARRAY_TYPE)
2250 return exp;
2252 /* Constants can be used directly unless they're not loadable. */
2253 if (TREE_CODE (exp) == CONST_DECL)
2254 exp = DECL_INITIAL (exp);
2256 /* Strip no-op conversions. */
2257 orig_exp = exp;
2258 STRIP_TYPE_NOPS (exp);
2260 copy_warning (exp, orig_exp);
2262 if (code == VOID_TYPE)
2264 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2265 "void value not ignored as it ought to be");
2266 return error_mark_node;
2269 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2270 if (exp == error_mark_node)
2271 return error_mark_node;
2273 promoted_type = targetm.promoted_type (type);
2274 if (promoted_type)
2275 return convert (promoted_type, exp);
2277 if (INTEGRAL_TYPE_P (type))
2278 return perform_integral_promotions (exp);
2280 return exp;
2283 /* Look up COMPONENT in a structure or union TYPE.
2285 If the component name is not found, returns NULL_TREE. Otherwise,
2286 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2287 stepping down the chain to the component, which is in the last
2288 TREE_VALUE of the list. Normally the list is of length one, but if
2289 the component is embedded within (nested) anonymous structures or
2290 unions, the list steps down the chain to the component. */
2292 static tree
2293 lookup_field (tree type, tree component)
2295 tree field;
2297 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2298 to the field elements. Use a binary search on this array to quickly
2299 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2300 will always be set for structures which have many elements.
2302 Duplicate field checking replaces duplicates with NULL_TREE so
2303 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2304 case just iterate using DECL_CHAIN. */
2306 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2307 && !seen_error ())
2309 int bot, top, half;
2310 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2312 field = TYPE_FIELDS (type);
2313 bot = 0;
2314 top = TYPE_LANG_SPECIFIC (type)->s->len;
2315 while (top - bot > 1)
2317 half = (top - bot + 1) >> 1;
2318 field = field_array[bot+half];
2320 if (DECL_NAME (field) == NULL_TREE)
2322 /* Step through all anon unions in linear fashion. */
2323 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2325 field = field_array[bot++];
2326 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2328 tree anon = lookup_field (TREE_TYPE (field), component);
2330 if (anon)
2331 return tree_cons (NULL_TREE, field, anon);
2333 /* The Plan 9 compiler permits referring
2334 directly to an anonymous struct/union field
2335 using a typedef name. */
2336 if (flag_plan9_extensions
2337 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2338 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2339 == TYPE_DECL)
2340 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2341 == component))
2342 break;
2346 /* Entire record is only anon unions. */
2347 if (bot > top)
2348 return NULL_TREE;
2350 /* Restart the binary search, with new lower bound. */
2351 continue;
2354 if (DECL_NAME (field) == component)
2355 break;
2356 if (DECL_NAME (field) < component)
2357 bot += half;
2358 else
2359 top = bot + half;
2362 if (DECL_NAME (field_array[bot]) == component)
2363 field = field_array[bot];
2364 else if (DECL_NAME (field) != component)
2365 return NULL_TREE;
2367 else
2369 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2371 if (DECL_NAME (field) == NULL_TREE
2372 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2374 tree anon = lookup_field (TREE_TYPE (field), component);
2376 if (anon)
2377 return tree_cons (NULL_TREE, field, anon);
2379 /* The Plan 9 compiler permits referring directly to an
2380 anonymous struct/union field using a typedef
2381 name. */
2382 if (flag_plan9_extensions
2383 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2384 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2385 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2386 == component))
2387 break;
2390 if (DECL_NAME (field) == component)
2391 break;
2394 if (field == NULL_TREE)
2395 return NULL_TREE;
2398 return tree_cons (NULL_TREE, field, NULL_TREE);
2401 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2403 static void
2404 lookup_field_fuzzy_find_candidates (tree type, tree component,
2405 vec<tree> *candidates)
2407 tree field;
2408 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2410 if (DECL_NAME (field) == NULL_TREE
2411 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2412 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2413 candidates);
2415 if (DECL_NAME (field))
2416 candidates->safe_push (DECL_NAME (field));
2420 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2421 rather than returning a TREE_LIST for an exact match. */
2423 static tree
2424 lookup_field_fuzzy (tree type, tree component)
2426 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2428 /* First, gather a list of candidates. */
2429 auto_vec <tree> candidates;
2431 lookup_field_fuzzy_find_candidates (type, component,
2432 &candidates);
2434 return find_closest_identifier (component, &candidates);
2437 /* Support function for build_component_ref's error-handling.
2439 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2440 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2442 static bool
2443 should_suggest_deref_p (tree datum_type)
2445 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2446 allows "." for ptrs; we could be handling a failed attempt
2447 to access a property. */
2448 if (c_dialect_objc ())
2449 return false;
2451 /* Only suggest it for pointers... */
2452 if (TREE_CODE (datum_type) != POINTER_TYPE)
2453 return false;
2455 /* ...to structs/unions. */
2456 tree underlying_type = TREE_TYPE (datum_type);
2457 enum tree_code code = TREE_CODE (underlying_type);
2458 if (code == RECORD_TYPE || code == UNION_TYPE)
2459 return true;
2460 else
2461 return false;
2464 /* Make an expression to refer to the COMPONENT field of structure or
2465 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2466 location of the COMPONENT_REF. COMPONENT_LOC is the location
2467 of COMPONENT. ARROW_LOC is the location of the first -> operand if
2468 it is from -> operator. */
2470 tree
2471 build_component_ref (location_t loc, tree datum, tree component,
2472 location_t component_loc, location_t arrow_loc)
2474 tree type = TREE_TYPE (datum);
2475 enum tree_code code = TREE_CODE (type);
2476 tree field = NULL;
2477 tree ref;
2478 bool datum_lvalue = lvalue_p (datum);
2480 if (!objc_is_public (datum, component))
2481 return error_mark_node;
2483 /* Detect Objective-C property syntax object.property. */
2484 if (c_dialect_objc ()
2485 && (ref = objc_maybe_build_component_ref (datum, component)))
2486 return ref;
2488 /* See if there is a field or component with name COMPONENT. */
2490 if (code == RECORD_TYPE || code == UNION_TYPE)
2492 if (!COMPLETE_TYPE_P (type))
2494 c_incomplete_type_error (loc, NULL_TREE, type);
2495 return error_mark_node;
2498 field = lookup_field (type, component);
2500 if (!field)
2502 tree guessed_id = lookup_field_fuzzy (type, component);
2503 if (guessed_id)
2505 /* Attempt to provide a fixit replacement hint, if
2506 we have a valid range for the component. */
2507 location_t reported_loc
2508 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2509 gcc_rich_location rich_loc (reported_loc);
2510 if (component_loc != UNKNOWN_LOCATION)
2511 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2512 error_at (&rich_loc,
2513 "%qT has no member named %qE; did you mean %qE?",
2514 type, component, guessed_id);
2516 else
2517 error_at (loc, "%qT has no member named %qE", type, component);
2518 return error_mark_node;
2521 /* Accessing elements of atomic structures or unions is undefined
2522 behavior (C11 6.5.2.3#5). */
2523 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2525 if (code == RECORD_TYPE)
2526 warning_at (loc, 0, "accessing a member %qE of an atomic "
2527 "structure %qE", component, datum);
2528 else
2529 warning_at (loc, 0, "accessing a member %qE of an atomic "
2530 "union %qE", component, datum);
2533 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2534 This might be better solved in future the way the C++ front
2535 end does it - by giving the anonymous entities each a
2536 separate name and type, and then have build_component_ref
2537 recursively call itself. We can't do that here. */
2540 tree subdatum = TREE_VALUE (field);
2541 int quals;
2542 tree subtype;
2543 bool use_datum_quals;
2545 if (TREE_TYPE (subdatum) == error_mark_node)
2546 return error_mark_node;
2548 /* If this is an rvalue, it does not have qualifiers in C
2549 standard terms and we must avoid propagating such
2550 qualifiers down to a non-lvalue array that is then
2551 converted to a pointer. */
2552 use_datum_quals = (datum_lvalue
2553 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2555 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2556 if (use_datum_quals)
2557 quals |= TYPE_QUALS (TREE_TYPE (datum));
2558 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2560 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2561 NULL_TREE);
2562 SET_EXPR_LOCATION (ref, loc);
2563 if (TREE_READONLY (subdatum)
2564 || (use_datum_quals && TREE_READONLY (datum)))
2565 TREE_READONLY (ref) = 1;
2566 if (TREE_THIS_VOLATILE (subdatum)
2567 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2568 TREE_THIS_VOLATILE (ref) = 1;
2570 if (TREE_UNAVAILABLE (subdatum))
2571 error_unavailable_use (subdatum, NULL_TREE);
2572 else if (TREE_DEPRECATED (subdatum))
2573 warn_deprecated_use (subdatum, NULL_TREE);
2575 datum = ref;
2577 field = TREE_CHAIN (field);
2579 while (field);
2581 return ref;
2583 else if (should_suggest_deref_p (type))
2585 /* Special-case the error message for "ptr.field" for the case
2586 where the user has confused "." vs "->". */
2587 rich_location richloc (line_table, loc);
2588 if (TREE_CODE (datum) == INDIRECT_REF && arrow_loc != UNKNOWN_LOCATION)
2590 richloc.add_fixit_insert_before (arrow_loc, "(*");
2591 richloc.add_fixit_insert_after (arrow_loc, ")");
2592 error_at (&richloc,
2593 "%qE is a pointer to pointer; did you mean to dereference "
2594 "it before applying %<->%> to it?",
2595 TREE_OPERAND (datum, 0));
2597 else
2599 /* "loc" should be the "." token. */
2600 richloc.add_fixit_replace ("->");
2601 error_at (&richloc,
2602 "%qE is a pointer; did you mean to use %<->%>?",
2603 datum);
2605 return error_mark_node;
2607 else if (code != ERROR_MARK)
2608 error_at (loc,
2609 "request for member %qE in something not a structure or union",
2610 component);
2612 return error_mark_node;
2615 /* Given an expression PTR for a pointer, return an expression
2616 for the value pointed to.
2617 ERRORSTRING is the name of the operator to appear in error messages.
2619 LOC is the location to use for the generated tree. */
2621 tree
2622 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2624 tree pointer = default_conversion (ptr);
2625 tree type = TREE_TYPE (pointer);
2626 tree ref;
2628 if (TREE_CODE (type) == POINTER_TYPE)
2630 if (CONVERT_EXPR_P (pointer)
2631 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2633 /* If a warning is issued, mark it to avoid duplicates from
2634 the backend. This only needs to be done at
2635 warn_strict_aliasing > 2. */
2636 if (warn_strict_aliasing > 2)
2637 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2638 type, TREE_OPERAND (pointer, 0)))
2639 suppress_warning (pointer, OPT_Wstrict_aliasing_);
2642 if (TREE_CODE (pointer) == ADDR_EXPR
2643 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2644 == TREE_TYPE (type)))
2646 ref = TREE_OPERAND (pointer, 0);
2647 protected_set_expr_location (ref, loc);
2648 return ref;
2650 else
2652 tree t = TREE_TYPE (type);
2654 ref = build1 (INDIRECT_REF, t, pointer);
2656 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2657 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2659 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2660 so that we get the proper error message if the result is used
2661 to assign to. Also, &* is supposed to be a no-op.
2662 And ANSI C seems to specify that the type of the result
2663 should be the const type. */
2664 /* A de-reference of a pointer to const is not a const. It is valid
2665 to change it via some other pointer. */
2666 TREE_READONLY (ref) = TYPE_READONLY (t);
2667 TREE_SIDE_EFFECTS (ref)
2668 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2669 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2670 protected_set_expr_location (ref, loc);
2671 return ref;
2674 else if (TREE_CODE (pointer) != ERROR_MARK)
2675 invalid_indirection_error (loc, type, errstring);
2677 return error_mark_node;
2680 /* This handles expressions of the form "a[i]", which denotes
2681 an array reference.
2683 This is logically equivalent in C to *(a+i), but we may do it differently.
2684 If A is a variable or a member, we generate a primitive ARRAY_REF.
2685 This avoids forcing the array out of registers, and can work on
2686 arrays that are not lvalues (for example, members of structures returned
2687 by functions).
2689 For vector types, allow vector[i] but not i[vector], and create
2690 *(((type*)&vectortype) + i) for the expression.
2692 LOC is the location to use for the returned expression. */
2694 tree
2695 build_array_ref (location_t loc, tree array, tree index)
2697 tree ret;
2698 bool swapped = false;
2699 if (TREE_TYPE (array) == error_mark_node
2700 || TREE_TYPE (index) == error_mark_node)
2701 return error_mark_node;
2703 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2704 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2705 /* Allow vector[index] but not index[vector]. */
2706 && !gnu_vector_type_p (TREE_TYPE (array)))
2708 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2709 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2711 error_at (loc,
2712 "subscripted value is neither array nor pointer nor vector");
2714 return error_mark_node;
2716 std::swap (array, index);
2717 swapped = true;
2720 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2722 error_at (loc, "array subscript is not an integer");
2723 return error_mark_node;
2726 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2728 error_at (loc, "subscripted value is pointer to function");
2729 return error_mark_node;
2732 /* ??? Existing practice has been to warn only when the char
2733 index is syntactically the index, not for char[array]. */
2734 if (!swapped)
2735 warn_array_subscript_with_type_char (loc, index);
2737 /* Apply default promotions *after* noticing character types. */
2738 index = default_conversion (index);
2739 if (index == error_mark_node)
2740 return error_mark_node;
2742 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2744 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2745 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2747 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2749 tree rval, type;
2751 /* An array that is indexed by a non-constant
2752 cannot be stored in a register; we must be able to do
2753 address arithmetic on its address.
2754 Likewise an array of elements of variable size. */
2755 if (TREE_CODE (index) != INTEGER_CST
2756 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2757 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2759 if (!c_mark_addressable (array, true))
2760 return error_mark_node;
2762 /* An array that is indexed by a constant value which is not within
2763 the array bounds cannot be stored in a register either; because we
2764 would get a crash in store_bit_field/extract_bit_field when trying
2765 to access a non-existent part of the register. */
2766 if (TREE_CODE (index) == INTEGER_CST
2767 && TYPE_DOMAIN (TREE_TYPE (array))
2768 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2770 if (!c_mark_addressable (array))
2771 return error_mark_node;
2774 if ((pedantic || warn_c90_c99_compat)
2775 && ! was_vector)
2777 tree foo = array;
2778 while (TREE_CODE (foo) == COMPONENT_REF)
2779 foo = TREE_OPERAND (foo, 0);
2780 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2781 pedwarn (loc, OPT_Wpedantic,
2782 "ISO C forbids subscripting %<register%> array");
2783 else if (!lvalue_p (foo))
2784 pedwarn_c90 (loc, OPT_Wpedantic,
2785 "ISO C90 forbids subscripting non-lvalue "
2786 "array");
2789 type = TREE_TYPE (TREE_TYPE (array));
2790 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2791 /* Array ref is const/volatile if the array elements are
2792 or if the array is. */
2793 TREE_READONLY (rval)
2794 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2795 | TREE_READONLY (array));
2796 TREE_SIDE_EFFECTS (rval)
2797 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2798 | TREE_SIDE_EFFECTS (array));
2799 TREE_THIS_VOLATILE (rval)
2800 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2801 /* This was added by rms on 16 Nov 91.
2802 It fixes vol struct foo *a; a->elts[1]
2803 in an inline function.
2804 Hope it doesn't break something else. */
2805 | TREE_THIS_VOLATILE (array));
2806 ret = require_complete_type (loc, rval);
2807 protected_set_expr_location (ret, loc);
2808 if (non_lvalue)
2809 ret = non_lvalue_loc (loc, ret);
2810 return ret;
2812 else
2814 tree ar = default_conversion (array);
2816 if (ar == error_mark_node)
2817 return ar;
2819 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2820 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2822 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2823 index, false),
2824 RO_ARRAY_INDEXING);
2825 if (non_lvalue)
2826 ret = non_lvalue_loc (loc, ret);
2827 return ret;
2831 /* Build an external reference to identifier ID. FUN indicates
2832 whether this will be used for a function call. LOC is the source
2833 location of the identifier. This sets *TYPE to the type of the
2834 identifier, which is not the same as the type of the returned value
2835 for CONST_DECLs defined as enum constants. If the type of the
2836 identifier is not available, *TYPE is set to NULL. */
2837 tree
2838 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2840 tree ref;
2841 tree decl = lookup_name (id);
2843 /* In Objective-C, an instance variable (ivar) may be preferred to
2844 whatever lookup_name() found. */
2845 decl = objc_lookup_ivar (decl, id);
2847 *type = NULL;
2848 if (decl && decl != error_mark_node)
2850 ref = decl;
2851 *type = TREE_TYPE (ref);
2853 else if (fun)
2854 /* Implicit function declaration. */
2855 ref = implicitly_declare (loc, id);
2856 else if (decl == error_mark_node)
2857 /* Don't complain about something that's already been
2858 complained about. */
2859 return error_mark_node;
2860 else
2862 undeclared_variable (loc, id);
2863 return error_mark_node;
2866 if (TREE_TYPE (ref) == error_mark_node)
2867 return error_mark_node;
2869 if (TREE_UNAVAILABLE (ref))
2870 error_unavailable_use (ref, NULL_TREE);
2871 else if (TREE_DEPRECATED (ref))
2872 warn_deprecated_use (ref, NULL_TREE);
2874 /* Recursive call does not count as usage. */
2875 if (ref != current_function_decl)
2877 TREE_USED (ref) = 1;
2880 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2882 if (!in_sizeof && !in_typeof)
2883 C_DECL_USED (ref) = 1;
2884 else if (DECL_INITIAL (ref) == NULL_TREE
2885 && DECL_EXTERNAL (ref)
2886 && !TREE_PUBLIC (ref))
2887 record_maybe_used_decl (ref);
2890 if (TREE_CODE (ref) == CONST_DECL)
2892 used_types_insert (TREE_TYPE (ref));
2894 if (warn_cxx_compat
2895 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2896 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2898 warning_at (loc, OPT_Wc___compat,
2899 ("enum constant defined in struct or union "
2900 "is not visible in C++"));
2901 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2904 ref = DECL_INITIAL (ref);
2905 TREE_CONSTANT (ref) = 1;
2907 else if (current_function_decl != NULL_TREE
2908 && !DECL_FILE_SCOPE_P (current_function_decl)
2909 && (VAR_OR_FUNCTION_DECL_P (ref)
2910 || TREE_CODE (ref) == PARM_DECL))
2912 tree context = decl_function_context (ref);
2914 if (context != NULL_TREE && context != current_function_decl)
2915 DECL_NONLOCAL (ref) = 1;
2917 /* C99 6.7.4p3: An inline definition of a function with external
2918 linkage ... shall not contain a reference to an identifier with
2919 internal linkage. */
2920 else if (current_function_decl != NULL_TREE
2921 && DECL_DECLARED_INLINE_P (current_function_decl)
2922 && DECL_EXTERNAL (current_function_decl)
2923 && VAR_OR_FUNCTION_DECL_P (ref)
2924 && (!VAR_P (ref) || TREE_STATIC (ref))
2925 && ! TREE_PUBLIC (ref)
2926 && DECL_CONTEXT (ref) != current_function_decl)
2927 record_inline_static (loc, current_function_decl, ref,
2928 csi_internal);
2930 return ref;
2933 /* Record details of decls possibly used inside sizeof or typeof. */
2934 struct maybe_used_decl
2936 /* The decl. */
2937 tree decl;
2938 /* The level seen at (in_sizeof + in_typeof). */
2939 int level;
2940 /* The next one at this level or above, or NULL. */
2941 struct maybe_used_decl *next;
2944 static struct maybe_used_decl *maybe_used_decls;
2946 /* Record that DECL, an undefined static function reference seen
2947 inside sizeof or typeof, might be used if the operand of sizeof is
2948 a VLA type or the operand of typeof is a variably modified
2949 type. */
2951 static void
2952 record_maybe_used_decl (tree decl)
2954 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2955 t->decl = decl;
2956 t->level = in_sizeof + in_typeof;
2957 t->next = maybe_used_decls;
2958 maybe_used_decls = t;
2961 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2962 USED is false, just discard them. If it is true, mark them used
2963 (if no longer inside sizeof or typeof) or move them to the next
2964 level up (if still inside sizeof or typeof). */
2966 void
2967 pop_maybe_used (bool used)
2969 struct maybe_used_decl *p = maybe_used_decls;
2970 int cur_level = in_sizeof + in_typeof;
2971 while (p && p->level > cur_level)
2973 if (used)
2975 if (cur_level == 0)
2976 C_DECL_USED (p->decl) = 1;
2977 else
2978 p->level = cur_level;
2980 p = p->next;
2982 if (!used || cur_level == 0)
2983 maybe_used_decls = p;
2986 /* Return the result of sizeof applied to EXPR. */
2988 struct c_expr
2989 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2991 struct c_expr ret;
2992 if (expr.value == error_mark_node)
2994 ret.value = error_mark_node;
2995 ret.original_code = ERROR_MARK;
2996 ret.original_type = NULL;
2997 pop_maybe_used (false);
2999 else
3001 bool expr_const_operands = true;
3003 if (TREE_CODE (expr.value) == PARM_DECL
3004 && C_ARRAY_PARAMETER (expr.value))
3006 auto_diagnostic_group d;
3007 if (warning_at (loc, OPT_Wsizeof_array_argument,
3008 "%<sizeof%> on array function parameter %qE will "
3009 "return size of %qT", expr.value,
3010 TREE_TYPE (expr.value)))
3011 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
3013 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
3014 &expr_const_operands);
3015 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
3016 c_last_sizeof_arg = expr.value;
3017 c_last_sizeof_loc = loc;
3018 ret.original_code = SIZEOF_EXPR;
3019 ret.original_type = NULL;
3020 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
3022 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
3023 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3024 folded_expr, ret.value);
3025 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3026 SET_EXPR_LOCATION (ret.value, loc);
3028 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3030 return ret;
3033 /* Return the result of sizeof applied to T, a structure for the type
3034 name passed to sizeof (rather than the type itself). LOC is the
3035 location of the original expression. */
3037 struct c_expr
3038 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3040 tree type;
3041 struct c_expr ret;
3042 tree type_expr = NULL_TREE;
3043 bool type_expr_const = true;
3044 type = groktypename (t, &type_expr, &type_expr_const);
3045 ret.value = c_sizeof (loc, type);
3046 c_last_sizeof_arg = type;
3047 c_last_sizeof_loc = loc;
3048 ret.original_code = SIZEOF_EXPR;
3049 ret.original_type = NULL;
3050 if (type == error_mark_node)
3052 ret.value = error_mark_node;
3053 ret.original_code = ERROR_MARK;
3055 else
3056 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3057 && C_TYPE_VARIABLE_SIZE (type))
3059 /* If the type is a [*] array, it is a VLA but is represented as
3060 having a size of zero. In such a case we must ensure that
3061 the result of sizeof does not get folded to a constant by
3062 c_fully_fold, because if the size is evaluated the result is
3063 not constant and so constraints on zero or negative size
3064 arrays must not be applied when this sizeof call is inside
3065 another array declarator. */
3066 if (!type_expr)
3067 type_expr = integer_zero_node;
3068 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3069 type_expr, ret.value);
3070 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3072 pop_maybe_used (type != error_mark_node
3073 ? C_TYPE_VARIABLE_SIZE (type) : false);
3074 return ret;
3077 /* Build a function call to function FUNCTION with parameters PARAMS.
3078 The function call is at LOC.
3079 PARAMS is a list--a chain of TREE_LIST nodes--in which the
3080 TREE_VALUE of each node is a parameter-expression.
3081 FUNCTION's data type may be a function type or a pointer-to-function. */
3083 tree
3084 build_function_call (location_t loc, tree function, tree params)
3086 vec<tree, va_gc> *v;
3087 tree ret;
3089 vec_alloc (v, list_length (params));
3090 for (; params; params = TREE_CHAIN (params))
3091 v->quick_push (TREE_VALUE (params));
3092 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3093 vec_free (v);
3094 return ret;
3097 /* Give a note about the location of the declaration of DECL. */
3099 static void
3100 inform_declaration (tree decl)
3102 if (decl && (TREE_CODE (decl) != FUNCTION_DECL
3103 || !DECL_IS_UNDECLARED_BUILTIN (decl)))
3104 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3107 /* Build a function call to function FUNCTION with parameters PARAMS.
3108 If FUNCTION is the result of resolving an overloaded target built-in,
3109 ORIG_FUNDECL is the original function decl, otherwise it is null.
3110 ORIGTYPES, if not NULL, is a vector of types; each element is
3111 either NULL or the original type of the corresponding element in
3112 PARAMS. The original type may differ from TREE_TYPE of the
3113 parameter for enums. FUNCTION's data type may be a function type
3114 or pointer-to-function. This function changes the elements of
3115 PARAMS. */
3117 tree
3118 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3119 tree function, vec<tree, va_gc> *params,
3120 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3122 tree fntype, fundecl = NULL_TREE;
3123 tree name = NULL_TREE, result;
3124 tree tem;
3125 int nargs;
3126 tree *argarray;
3129 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3130 STRIP_TYPE_NOPS (function);
3132 /* Convert anything with function type to a pointer-to-function. */
3133 if (TREE_CODE (function) == FUNCTION_DECL)
3135 name = DECL_NAME (function);
3137 if (flag_tm)
3138 tm_malloc_replacement (function);
3139 fundecl = function;
3140 if (!orig_fundecl)
3141 orig_fundecl = fundecl;
3142 /* Atomic functions have type checking/casting already done. They are
3143 often rewritten and don't match the original parameter list. */
3144 if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
3145 origtypes = NULL;
3147 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3148 function = function_to_pointer_conversion (loc, function);
3150 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3151 expressions, like those used for ObjC messenger dispatches. */
3152 if (params && !params->is_empty ())
3153 function = objc_rewrite_function_call (function, (*params)[0]);
3155 function = c_fully_fold (function, false, NULL);
3157 fntype = TREE_TYPE (function);
3159 if (TREE_CODE (fntype) == ERROR_MARK)
3160 return error_mark_node;
3162 if (!(TREE_CODE (fntype) == POINTER_TYPE
3163 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3165 if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
3166 error_at (loc,
3167 "called object %qE is not a function or function pointer",
3168 function);
3169 else if (DECL_P (function))
3171 error_at (loc,
3172 "called object %qD is not a function or function pointer",
3173 function);
3174 inform_declaration (function);
3176 else
3177 error_at (loc,
3178 "called object is not a function or function pointer");
3179 return error_mark_node;
3182 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3183 current_function_returns_abnormally = 1;
3185 /* fntype now gets the type of function pointed to. */
3186 fntype = TREE_TYPE (fntype);
3188 /* Convert the parameters to the types declared in the
3189 function prototype, or apply default promotions. */
3191 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3192 origtypes, function, fundecl);
3193 if (nargs < 0)
3194 return error_mark_node;
3196 /* Check that the function is called through a compatible prototype.
3197 If it is not, warn. */
3198 if (CONVERT_EXPR_P (function)
3199 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3200 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3201 && !comptypes (fntype, TREE_TYPE (tem)))
3203 tree return_type = TREE_TYPE (fntype);
3205 /* This situation leads to run-time undefined behavior. We can't,
3206 therefore, simply error unless we can prove that all possible
3207 executions of the program must execute the code. */
3208 warning_at (loc, 0, "function called through a non-compatible type");
3210 if (VOID_TYPE_P (return_type)
3211 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3212 pedwarn (loc, 0,
3213 "function with qualified void return type called");
3216 argarray = vec_safe_address (params);
3218 /* Check that arguments to builtin functions match the expectations. */
3219 if (fundecl
3220 && fndecl_built_in_p (fundecl)
3221 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3222 orig_fundecl, nargs, argarray))
3223 return error_mark_node;
3225 /* Check that the arguments to the function are valid. */
3226 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3227 nargs, argarray, &arg_loc);
3229 if (name != NULL_TREE
3230 && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
3232 if (require_constant_value)
3233 result
3234 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3235 function, nargs, argarray);
3236 else
3237 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3238 function, nargs, argarray);
3239 if (TREE_CODE (result) == NOP_EXPR
3240 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3241 STRIP_TYPE_NOPS (result);
3243 else
3244 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3245 function, nargs, argarray);
3246 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3247 later. */
3248 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3249 suppress_warning (result, OPT_Wnonnull);
3251 /* In this improbable scenario, a nested function returns a VM type.
3252 Create a TARGET_EXPR so that the call always has a LHS, much as
3253 what the C++ FE does for functions returning non-PODs. */
3254 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3256 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3257 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3258 NULL_TREE, NULL_TREE);
3261 if (VOID_TYPE_P (TREE_TYPE (result)))
3263 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3264 pedwarn (loc, 0,
3265 "function with qualified void return type called");
3266 return result;
3268 return require_complete_type (loc, result);
3271 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3273 tree
3274 c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
3275 tree function, vec<tree, va_gc> *params,
3276 vec<tree, va_gc> *origtypes)
3278 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3279 STRIP_TYPE_NOPS (function);
3281 /* Convert anything with function type to a pointer-to-function. */
3282 if (TREE_CODE (function) == FUNCTION_DECL)
3284 /* Implement type-directed function overloading for builtins.
3285 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3286 handle all the type checking. The result is a complete expression
3287 that implements this function call. */
3288 tree tem = resolve_overloaded_builtin (loc, function, params);
3289 if (tem)
3290 return tem;
3292 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3295 /* Helper for convert_arguments called to convert the VALue of argument
3296 number ARGNUM from ORIGTYPE to the corresponding parameter number
3297 PARMNUM and TYPE.
3298 PLOC is the location where the conversion is being performed.
3299 FUNCTION and FUNDECL are the same as in convert_arguments.
3300 VALTYPE is the original type of VAL before the conversion and,
3301 for EXCESS_PRECISION_EXPR, the operand of the expression.
3302 NPC is true if VAL represents the null pointer constant (VAL itself
3303 will have been folded to an integer constant).
3304 RNAME is the same as FUNCTION except in Objective C when it's
3305 the function selector.
3306 EXCESS_PRECISION is true when VAL was originally represented
3307 as EXCESS_PRECISION_EXPR.
3308 WARNOPT is the same as in convert_for_assignment. */
3310 static tree
3311 convert_argument (location_t ploc, tree function, tree fundecl,
3312 tree type, tree origtype, tree val, tree valtype,
3313 bool npc, tree rname, int parmnum, int argnum,
3314 bool excess_precision, int warnopt)
3316 /* Formal parm type is specified by a function prototype. */
3318 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3320 error_at (ploc, "type of formal parameter %d is incomplete",
3321 parmnum + 1);
3322 return val;
3325 /* Optionally warn about conversions that differ from the default
3326 conversions. */
3327 if (warn_traditional_conversion || warn_traditional)
3329 unsigned int formal_prec = TYPE_PRECISION (type);
3331 if (INTEGRAL_TYPE_P (type)
3332 && TREE_CODE (valtype) == REAL_TYPE)
3333 warning_at (ploc, OPT_Wtraditional_conversion,
3334 "passing argument %d of %qE as integer rather "
3335 "than floating due to prototype",
3336 argnum, rname);
3337 if (INTEGRAL_TYPE_P (type)
3338 && TREE_CODE (valtype) == COMPLEX_TYPE)
3339 warning_at (ploc, OPT_Wtraditional_conversion,
3340 "passing argument %d of %qE as integer rather "
3341 "than complex due to prototype",
3342 argnum, rname);
3343 else if (TREE_CODE (type) == COMPLEX_TYPE
3344 && TREE_CODE (valtype) == REAL_TYPE)
3345 warning_at (ploc, OPT_Wtraditional_conversion,
3346 "passing argument %d of %qE as complex rather "
3347 "than floating due to prototype",
3348 argnum, rname);
3349 else if (TREE_CODE (type) == REAL_TYPE
3350 && INTEGRAL_TYPE_P (valtype))
3351 warning_at (ploc, OPT_Wtraditional_conversion,
3352 "passing argument %d of %qE as floating rather "
3353 "than integer due to prototype",
3354 argnum, rname);
3355 else if (TREE_CODE (type) == COMPLEX_TYPE
3356 && INTEGRAL_TYPE_P (valtype))
3357 warning_at (ploc, OPT_Wtraditional_conversion,
3358 "passing argument %d of %qE as complex rather "
3359 "than integer due to prototype",
3360 argnum, rname);
3361 else if (TREE_CODE (type) == REAL_TYPE
3362 && TREE_CODE (valtype) == COMPLEX_TYPE)
3363 warning_at (ploc, OPT_Wtraditional_conversion,
3364 "passing argument %d of %qE as floating rather "
3365 "than complex due to prototype",
3366 argnum, rname);
3367 /* ??? At some point, messages should be written about
3368 conversions between complex types, but that's too messy
3369 to do now. */
3370 else if (TREE_CODE (type) == REAL_TYPE
3371 && TREE_CODE (valtype) == REAL_TYPE)
3373 /* Warn if any argument is passed as `float',
3374 since without a prototype it would be `double'. */
3375 if (formal_prec == TYPE_PRECISION (float_type_node)
3376 && type != dfloat32_type_node)
3377 warning_at (ploc, 0,
3378 "passing argument %d of %qE as %<float%> "
3379 "rather than %<double%> due to prototype",
3380 argnum, rname);
3382 /* Warn if mismatch between argument and prototype
3383 for decimal float types. Warn of conversions with
3384 binary float types and of precision narrowing due to
3385 prototype. */
3386 else if (type != valtype
3387 && (type == dfloat32_type_node
3388 || type == dfloat64_type_node
3389 || type == dfloat128_type_node
3390 || valtype == dfloat32_type_node
3391 || valtype == dfloat64_type_node
3392 || valtype == dfloat128_type_node)
3393 && (formal_prec
3394 <= TYPE_PRECISION (valtype)
3395 || (type == dfloat128_type_node
3396 && (valtype
3397 != dfloat64_type_node
3398 && (valtype
3399 != dfloat32_type_node)))
3400 || (type == dfloat64_type_node
3401 && (valtype
3402 != dfloat32_type_node))))
3403 warning_at (ploc, 0,
3404 "passing argument %d of %qE as %qT "
3405 "rather than %qT due to prototype",
3406 argnum, rname, type, valtype);
3409 /* Detect integer changing in width or signedness.
3410 These warnings are only activated with
3411 -Wtraditional-conversion, not with -Wtraditional. */
3412 else if (warn_traditional_conversion
3413 && INTEGRAL_TYPE_P (type)
3414 && INTEGRAL_TYPE_P (valtype))
3416 tree would_have_been = default_conversion (val);
3417 tree type1 = TREE_TYPE (would_have_been);
3419 if (val == error_mark_node)
3420 /* VAL could have been of incomplete type. */;
3421 else if (TREE_CODE (type) == ENUMERAL_TYPE
3422 && (TYPE_MAIN_VARIANT (type)
3423 == TYPE_MAIN_VARIANT (valtype)))
3424 /* No warning if function asks for enum
3425 and the actual arg is that enum type. */
3427 else if (formal_prec != TYPE_PRECISION (type1))
3428 warning_at (ploc, OPT_Wtraditional_conversion,
3429 "passing argument %d of %qE "
3430 "with different width due to prototype",
3431 argnum, rname);
3432 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3434 /* Don't complain if the formal parameter type
3435 is an enum, because we can't tell now whether
3436 the value was an enum--even the same enum. */
3437 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3439 else if (TREE_CODE (val) == INTEGER_CST
3440 && int_fits_type_p (val, type))
3441 /* Change in signedness doesn't matter
3442 if a constant value is unaffected. */
3444 /* If the value is extended from a narrower
3445 unsigned type, it doesn't matter whether we
3446 pass it as signed or unsigned; the value
3447 certainly is the same either way. */
3448 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3449 && TYPE_UNSIGNED (valtype))
3451 else if (TYPE_UNSIGNED (type))
3452 warning_at (ploc, OPT_Wtraditional_conversion,
3453 "passing argument %d of %qE "
3454 "as unsigned due to prototype",
3455 argnum, rname);
3456 else
3457 warning_at (ploc, OPT_Wtraditional_conversion,
3458 "passing argument %d of %qE "
3459 "as signed due to prototype",
3460 argnum, rname);
3464 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3465 sake of better warnings from convert_and_check. */
3466 if (excess_precision)
3467 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3469 tree parmval = convert_for_assignment (ploc, ploc, type,
3470 val, origtype, ic_argpass,
3471 npc, fundecl, function,
3472 parmnum + 1, warnopt);
3474 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3475 && INTEGRAL_TYPE_P (type)
3476 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3477 parmval = default_conversion (parmval);
3479 return parmval;
3482 /* Convert the argument expressions in the vector VALUES
3483 to the types in the list TYPELIST.
3485 If TYPELIST is exhausted, or when an element has NULL as its type,
3486 perform the default conversions.
3488 ORIGTYPES is the original types of the expressions in VALUES. This
3489 holds the type of enum values which have been converted to integral
3490 types. It may be NULL.
3492 FUNCTION is a tree for the called function. It is used only for
3493 error messages, where it is formatted with %qE.
3495 This is also where warnings about wrong number of args are generated.
3497 ARG_LOC are locations of function arguments (if any).
3499 Returns the actual number of arguments processed (which may be less
3500 than the length of VALUES in some error situations), or -1 on
3501 failure. */
3503 static int
3504 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3505 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3506 tree function, tree fundecl)
3508 unsigned int parmnum;
3509 bool error_args = false;
3510 const bool type_generic = fundecl
3511 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3512 bool type_generic_remove_excess_precision = false;
3513 bool type_generic_overflow_p = false;
3514 tree selector;
3516 /* Change pointer to function to the function itself for
3517 diagnostics. */
3518 if (TREE_CODE (function) == ADDR_EXPR
3519 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3520 function = TREE_OPERAND (function, 0);
3522 /* Handle an ObjC selector specially for diagnostics. */
3523 selector = objc_message_selector ();
3525 /* For a call to a built-in function declared without a prototype,
3526 set to the built-in function's argument list. */
3527 tree builtin_typelist = NULL_TREE;
3529 /* For type-generic built-in functions, determine whether excess
3530 precision should be removed (classification) or not
3531 (comparison). */
3532 if (fundecl
3533 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3535 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3536 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3538 /* For a call to a built-in function declared without a prototype
3539 use the types of the parameters of the internal built-in to
3540 match those of the arguments to. */
3541 if (tree bdecl = builtin_decl_explicit (code))
3542 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3545 /* For type-generic built-in functions, determine whether excess
3546 precision should be removed (classification) or not
3547 (comparison). */
3548 if (type_generic)
3549 switch (code)
3551 case BUILT_IN_ISFINITE:
3552 case BUILT_IN_ISINF:
3553 case BUILT_IN_ISINF_SIGN:
3554 case BUILT_IN_ISNAN:
3555 case BUILT_IN_ISNORMAL:
3556 case BUILT_IN_ISSIGNALING:
3557 case BUILT_IN_FPCLASSIFY:
3558 type_generic_remove_excess_precision = true;
3559 break;
3561 case BUILT_IN_ADD_OVERFLOW_P:
3562 case BUILT_IN_SUB_OVERFLOW_P:
3563 case BUILT_IN_MUL_OVERFLOW_P:
3564 /* The last argument of these type-generic builtins
3565 should not be promoted. */
3566 type_generic_overflow_p = true;
3567 break;
3569 default:
3570 break;
3574 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3575 individual converted arguments. */
3577 tree typetail, builtin_typetail, val;
3578 for (typetail = typelist,
3579 builtin_typetail = builtin_typelist,
3580 parmnum = 0;
3581 values && values->iterate (parmnum, &val);
3582 ++parmnum)
3584 /* The type of the function parameter (if it was declared with one). */
3585 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3586 /* The type of the built-in function parameter (if the function
3587 is a built-in). Used to detect type incompatibilities in
3588 calls to built-ins declared without a prototype. */
3589 tree builtin_type = (builtin_typetail
3590 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3591 /* The original type of the argument being passed to the function. */
3592 tree valtype = TREE_TYPE (val);
3593 /* The called function (or function selector in Objective C). */
3594 tree rname = function;
3595 int argnum = parmnum + 1;
3596 const char *invalid_func_diag;
3597 /* Set for EXCESS_PRECISION_EXPR arguments. */
3598 bool excess_precision = false;
3599 /* The value of the argument after conversion to the type
3600 of the function parameter it is passed to. */
3601 tree parmval;
3602 /* Some __atomic_* builtins have additional hidden argument at
3603 position 0. */
3604 location_t ploc
3605 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3606 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3607 : input_location;
3609 if (type == void_type_node)
3611 if (selector)
3612 error_at (loc, "too many arguments to method %qE", selector);
3613 else
3614 error_at (loc, "too many arguments to function %qE", function);
3615 inform_declaration (fundecl);
3616 return error_args ? -1 : (int) parmnum;
3619 if (builtin_type == void_type_node)
3621 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3622 "too many arguments to built-in function %qE "
3623 "expecting %d", function, parmnum))
3624 inform_declaration (fundecl);
3625 builtin_typetail = NULL_TREE;
3628 if (selector && argnum > 2)
3630 rname = selector;
3631 argnum -= 2;
3634 /* Determine if VAL is a null pointer constant before folding it. */
3635 bool npc = null_pointer_constant_p (val);
3637 /* If there is excess precision and a prototype, convert once to
3638 the required type rather than converting via the semantic
3639 type. Likewise without a prototype a float value represented
3640 as long double should be converted once to double. But for
3641 type-generic classification functions excess precision must
3642 be removed here. */
3643 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3644 && (type || !type_generic || !type_generic_remove_excess_precision))
3646 val = TREE_OPERAND (val, 0);
3647 excess_precision = true;
3649 val = c_fully_fold (val, false, NULL);
3650 STRIP_TYPE_NOPS (val);
3652 val = require_complete_type (ploc, val);
3654 /* Some floating-point arguments must be promoted to double when
3655 no type is specified by a prototype. This applies to
3656 arguments of type float, and to architecture-specific types
3657 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3658 bool promote_float_arg = false;
3659 if (type == NULL_TREE
3660 && TREE_CODE (valtype) == REAL_TYPE
3661 && (TYPE_PRECISION (valtype)
3662 <= TYPE_PRECISION (double_type_node))
3663 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3664 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3665 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3667 /* Promote this argument, unless it has a _FloatN or
3668 _FloatNx type. */
3669 promote_float_arg = true;
3670 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3671 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3673 promote_float_arg = false;
3674 break;
3678 if (type != NULL_TREE)
3680 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3681 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3682 val, valtype, npc, rname, parmnum, argnum,
3683 excess_precision, 0);
3685 else if (promote_float_arg)
3687 if (type_generic)
3688 parmval = val;
3689 else
3691 /* Convert `float' to `double'. */
3692 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3693 warning_at (ploc, OPT_Wdouble_promotion,
3694 "implicit conversion from %qT to %qT when passing "
3695 "argument to function",
3696 valtype, double_type_node);
3697 parmval = convert (double_type_node, val);
3700 else if ((excess_precision && !type_generic)
3701 || (type_generic_overflow_p && parmnum == 2))
3702 /* A "double" argument with excess precision being passed
3703 without a prototype or in variable arguments.
3704 The last argument of __builtin_*_overflow_p should not be
3705 promoted. */
3706 parmval = convert (valtype, val);
3707 else if ((invalid_func_diag =
3708 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3710 error (invalid_func_diag);
3711 return -1;
3713 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3715 return -1;
3717 else
3718 /* Convert `short' and `char' to full-size `int'. */
3719 parmval = default_conversion (val);
3721 (*values)[parmnum] = parmval;
3722 if (parmval == error_mark_node)
3723 error_args = true;
3725 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3727 /* For a call to a built-in function declared without a prototype,
3728 perform the conversions from the argument to the expected type
3729 but issue warnings rather than errors for any mismatches.
3730 Ignore the converted argument and use the PARMVAL obtained
3731 above by applying default conversions instead. */
3732 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3733 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3734 val, valtype, npc, rname, parmnum, argnum,
3735 excess_precision,
3736 OPT_Wbuiltin_declaration_mismatch);
3739 if (typetail)
3740 typetail = TREE_CHAIN (typetail);
3742 if (builtin_typetail)
3743 builtin_typetail = TREE_CHAIN (builtin_typetail);
3746 gcc_assert (parmnum == vec_safe_length (values));
3748 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3750 error_at (loc, "too few arguments to function %qE", function);
3751 inform_declaration (fundecl);
3752 return -1;
3755 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3757 unsigned nargs = parmnum;
3758 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3759 ++nargs;
3761 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3762 "too few arguments to built-in function %qE "
3763 "expecting %u", function, nargs - 1))
3764 inform_declaration (fundecl);
3767 return error_args ? -1 : (int) parmnum;
3770 /* This is the entry point used by the parser to build unary operators
3771 in the input. CODE, a tree_code, specifies the unary operator, and
3772 ARG is the operand. For unary plus, the C parser currently uses
3773 CONVERT_EXPR for code.
3775 LOC is the location to use for the tree generated.
3778 struct c_expr
3779 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3781 struct c_expr result;
3783 result.original_code = code;
3784 result.original_type = NULL;
3786 if (reject_gcc_builtin (arg.value))
3788 result.value = error_mark_node;
3790 else
3792 result.value = build_unary_op (loc, code, arg.value, false);
3794 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3795 overflow_warning (loc, result.value, arg.value);
3798 /* We are typically called when parsing a prefix token at LOC acting on
3799 ARG. Reflect this by updating the source range of the result to
3800 start at LOC and end at the end of ARG. */
3801 set_c_expr_source_range (&result,
3802 loc, arg.get_finish ());
3804 return result;
3807 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3809 bool
3810 char_type_p (tree type)
3812 return (type == char_type_node
3813 || type == unsigned_char_type_node
3814 || type == signed_char_type_node
3815 || type == char16_type_node
3816 || type == char32_type_node);
3819 /* This is the entry point used by the parser to build binary operators
3820 in the input. CODE, a tree_code, specifies the binary operator, and
3821 ARG1 and ARG2 are the operands. In addition to constructing the
3822 expression, we check for operands that were written with other binary
3823 operators in a way that is likely to confuse the user.
3825 LOCATION is the location of the binary operator. */
3827 struct c_expr
3828 parser_build_binary_op (location_t location, enum tree_code code,
3829 struct c_expr arg1, struct c_expr arg2)
3831 struct c_expr result;
3832 result.m_decimal = 0;
3834 enum tree_code code1 = arg1.original_code;
3835 enum tree_code code2 = arg2.original_code;
3836 tree type1 = (arg1.original_type
3837 ? arg1.original_type
3838 : TREE_TYPE (arg1.value));
3839 tree type2 = (arg2.original_type
3840 ? arg2.original_type
3841 : TREE_TYPE (arg2.value));
3843 result.value = build_binary_op (location, code,
3844 arg1.value, arg2.value, true);
3845 result.original_code = code;
3846 result.original_type = NULL;
3848 if (TREE_CODE (result.value) == ERROR_MARK)
3850 set_c_expr_source_range (&result,
3851 arg1.get_start (),
3852 arg2.get_finish ());
3853 return result;
3856 if (location != UNKNOWN_LOCATION)
3857 protected_set_expr_location (result.value, location);
3859 set_c_expr_source_range (&result,
3860 arg1.get_start (),
3861 arg2.get_finish ());
3863 /* Check for cases such as x+y<<z which users are likely
3864 to misinterpret. */
3865 if (warn_parentheses)
3866 warn_about_parentheses (location, code, code1, arg1.value, code2,
3867 arg2.value);
3869 if (warn_logical_op)
3870 warn_logical_operator (location, code, TREE_TYPE (result.value),
3871 code1, arg1.value, code2, arg2.value);
3873 if (warn_tautological_compare)
3875 tree lhs = arg1.value;
3876 tree rhs = arg2.value;
3877 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3879 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3880 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3881 lhs = NULL_TREE;
3882 else
3883 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3885 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3887 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3888 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3889 rhs = NULL_TREE;
3890 else
3891 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3893 if (lhs != NULL_TREE && rhs != NULL_TREE)
3894 warn_tautological_cmp (location, code, lhs, rhs);
3897 if (warn_logical_not_paren
3898 && TREE_CODE_CLASS (code) == tcc_comparison
3899 && code1 == TRUTH_NOT_EXPR
3900 && code2 != TRUTH_NOT_EXPR
3901 /* Avoid warning for !!x == y. */
3902 && (TREE_CODE (arg1.value) != NE_EXPR
3903 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3905 /* Avoid warning for !b == y where b has _Bool type. */
3906 tree t = integer_zero_node;
3907 if (TREE_CODE (arg1.value) == EQ_EXPR
3908 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3909 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3911 t = TREE_OPERAND (arg1.value, 0);
3914 if (TREE_TYPE (t) != integer_type_node)
3915 break;
3916 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3917 t = C_MAYBE_CONST_EXPR_EXPR (t);
3918 else if (CONVERT_EXPR_P (t))
3919 t = TREE_OPERAND (t, 0);
3920 else
3921 break;
3923 while (1);
3925 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3926 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3929 /* Warn about comparisons against string literals, with the exception
3930 of testing for equality or inequality of a string literal with NULL. */
3931 if (code == EQ_EXPR || code == NE_EXPR)
3933 if ((code1 == STRING_CST
3934 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3935 || (code2 == STRING_CST
3936 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3937 warning_at (location, OPT_Waddress,
3938 "comparison with string literal results in unspecified behavior");
3939 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3940 if (POINTER_TYPE_P (type1)
3941 && null_pointer_constant_p (arg2.value)
3942 && char_type_p (type2))
3944 auto_diagnostic_group d;
3945 if (warning_at (location, OPT_Wpointer_compare,
3946 "comparison between pointer and zero character "
3947 "constant"))
3948 inform (arg1.get_start (),
3949 "did you mean to dereference the pointer?");
3951 else if (POINTER_TYPE_P (type2)
3952 && null_pointer_constant_p (arg1.value)
3953 && char_type_p (type1))
3955 auto_diagnostic_group d;
3956 if (warning_at (location, OPT_Wpointer_compare,
3957 "comparison between pointer and zero character "
3958 "constant"))
3959 inform (arg2.get_start (),
3960 "did you mean to dereference the pointer?");
3963 else if (TREE_CODE_CLASS (code) == tcc_comparison
3964 && (code1 == STRING_CST || code2 == STRING_CST))
3965 warning_at (location, OPT_Waddress,
3966 "comparison with string literal results in unspecified "
3967 "behavior");
3969 if (warn_array_compare
3970 && TREE_CODE_CLASS (code) == tcc_comparison
3971 && TREE_CODE (type1) == ARRAY_TYPE
3972 && TREE_CODE (type2) == ARRAY_TYPE)
3973 do_warn_array_compare (location, code, arg1.value, arg2.value);
3975 if (TREE_OVERFLOW_P (result.value)
3976 && !TREE_OVERFLOW_P (arg1.value)
3977 && !TREE_OVERFLOW_P (arg2.value))
3978 overflow_warning (location, result.value);
3980 /* Warn about comparisons of different enum types. */
3981 if (warn_enum_compare
3982 && TREE_CODE_CLASS (code) == tcc_comparison
3983 && TREE_CODE (type1) == ENUMERAL_TYPE
3984 && TREE_CODE (type2) == ENUMERAL_TYPE
3985 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3986 warning_at (location, OPT_Wenum_compare,
3987 "comparison between %qT and %qT",
3988 type1, type2);
3990 if (warn_xor_used_as_pow
3991 && code == BIT_XOR_EXPR
3992 && arg1.m_decimal
3993 && arg2.m_decimal)
3994 check_for_xor_used_as_pow (arg1.get_location (), arg1.value,
3995 location,
3996 arg2.value);
3998 return result;
4001 /* Return a tree for the difference of pointers OP0 and OP1.
4002 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
4003 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
4005 static tree
4006 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
4008 tree restype = ptrdiff_type_node;
4009 tree result, inttype;
4011 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
4012 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
4013 tree target_type = TREE_TYPE (TREE_TYPE (op0));
4014 tree orig_op0 = op0;
4015 tree orig_op1 = op1;
4017 /* If the operands point into different address spaces, we need to
4018 explicitly convert them to pointers into the common address space
4019 before we can subtract the numerical address values. */
4020 if (as0 != as1)
4022 addr_space_t as_common;
4023 tree common_type;
4025 /* Determine the common superset address space. This is guaranteed
4026 to exist because the caller verified that comp_target_types
4027 returned non-zero. */
4028 if (!addr_space_superset (as0, as1, &as_common))
4029 gcc_unreachable ();
4031 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
4032 op0 = convert (common_type, op0);
4033 op1 = convert (common_type, op1);
4036 /* Determine integer type result of the subtraction. This will usually
4037 be the same as the result type (ptrdiff_t), but may need to be a wider
4038 type if pointers for the address space are wider than ptrdiff_t. */
4039 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
4040 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
4041 else
4042 inttype = restype;
4044 if (TREE_CODE (target_type) == VOID_TYPE)
4045 pedwarn (loc, OPT_Wpointer_arith,
4046 "pointer of type %<void *%> used in subtraction");
4047 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4048 pedwarn (loc, OPT_Wpointer_arith,
4049 "pointer to a function used in subtraction");
4051 if (current_function_decl != NULL_TREE
4052 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
4054 op0 = save_expr (op0);
4055 op1 = save_expr (op1);
4057 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
4058 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
4061 /* First do the subtraction, then build the divide operator
4062 and only convert at the very end.
4063 Do not do default conversions in case restype is a short type. */
4065 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4066 pointers. If some platform cannot provide that, or has a larger
4067 ptrdiff_type to support differences larger than half the address
4068 space, cast the pointers to some larger integer type and do the
4069 computations in that type. */
4070 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4071 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4072 convert (inttype, op1), false);
4073 else
4075 /* Cast away qualifiers. */
4076 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4077 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4078 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4081 /* This generates an error if op1 is pointer to incomplete type. */
4082 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4083 error_at (loc, "arithmetic on pointer to an incomplete type");
4084 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4085 TREE_TYPE (TREE_TYPE (orig_op0))))
4086 verify_type_context (loc, TCTX_POINTER_ARITH,
4087 TREE_TYPE (TREE_TYPE (orig_op1)));
4089 op1 = c_size_in_bytes (target_type);
4091 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4092 error_at (loc, "arithmetic on pointer to an empty aggregate");
4094 /* Divide by the size, in easiest possible way. */
4095 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4096 op0, convert (inttype, op1));
4098 /* Convert to final result type if necessary. */
4099 return convert (restype, result);
4102 /* Expand atomic compound assignments into an appropriate sequence as
4103 specified by the C11 standard section 6.5.16.2.
4105 _Atomic T1 E1
4106 T2 E2
4107 E1 op= E2
4109 This sequence is used for all types for which these operations are
4110 supported.
4112 In addition, built-in versions of the 'fe' prefixed routines may
4113 need to be invoked for floating point (real, complex or vector) when
4114 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4116 T1 newval;
4117 T1 old;
4118 T1 *addr
4119 T2 val
4120 fenv_t fenv
4122 addr = &E1;
4123 val = (E2);
4124 __atomic_load (addr, &old, SEQ_CST);
4125 feholdexcept (&fenv);
4126 loop:
4127 newval = old op val;
4128 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4129 SEQ_CST))
4130 goto done;
4131 feclearexcept (FE_ALL_EXCEPT);
4132 goto loop:
4133 done:
4134 feupdateenv (&fenv);
4136 The compiler will issue the __atomic_fetch_* built-in when possible,
4137 otherwise it will generate the generic form of the atomic operations.
4138 This requires temp(s) and has their address taken. The atomic processing
4139 is smart enough to figure out when the size of an object can utilize
4140 a lock-free version, and convert the built-in call to the appropriate
4141 lock-free routine. The optimizers will then dispose of any temps that
4142 are no longer required, and lock-free implementations are utilized as
4143 long as there is target support for the required size.
4145 If the operator is NOP_EXPR, then this is a simple assignment, and
4146 an __atomic_store is issued to perform the assignment rather than
4147 the above loop. */
4149 /* Build an atomic assignment at LOC, expanding into the proper
4150 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4151 the result of the operation, unless RETURN_OLD_P, in which case
4152 return the old value of LHS (this is only for postincrement and
4153 postdecrement). */
4155 static tree
4156 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4157 tree rhs, bool return_old_p)
4159 tree fndecl, func_call;
4160 vec<tree, va_gc> *params;
4161 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4162 tree old, old_addr;
4163 tree compound_stmt = NULL_TREE;
4164 tree stmt, goto_stmt;
4165 tree loop_label, loop_decl, done_label, done_decl;
4167 tree lhs_type = TREE_TYPE (lhs);
4168 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4169 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4170 tree rhs_semantic_type = TREE_TYPE (rhs);
4171 tree nonatomic_rhs_semantic_type;
4172 tree rhs_type;
4174 gcc_assert (TYPE_ATOMIC (lhs_type));
4176 if (return_old_p)
4177 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4179 /* Allocate enough vector items for a compare_exchange. */
4180 vec_alloc (params, 6);
4182 /* Create a compound statement to hold the sequence of statements
4183 with a loop. */
4184 if (modifycode != NOP_EXPR)
4186 compound_stmt = c_begin_compound_stmt (false);
4188 /* For consistency with build_modify_expr on non-_Atomic,
4189 mark the lhs as read. Also, it would be very hard to match
4190 such expressions in mark_exp_read. */
4191 mark_exp_read (lhs);
4194 /* Remove any excess precision (which is only present here in the
4195 case of compound assignments). */
4196 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4198 gcc_assert (modifycode != NOP_EXPR);
4199 rhs = TREE_OPERAND (rhs, 0);
4201 rhs_type = TREE_TYPE (rhs);
4203 /* Fold the RHS if it hasn't already been folded. */
4204 if (modifycode != NOP_EXPR)
4205 rhs = c_fully_fold (rhs, false, NULL);
4207 /* Remove the qualifiers for the rest of the expressions and create
4208 the VAL temp variable to hold the RHS. */
4209 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4210 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4211 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4212 TYPE_UNQUALIFIED);
4213 val = create_tmp_var_raw (nonatomic_rhs_type);
4214 TREE_ADDRESSABLE (val) = 1;
4215 suppress_warning (val);
4216 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4217 NULL_TREE);
4218 TREE_SIDE_EFFECTS (rhs) = 1;
4219 SET_EXPR_LOCATION (rhs, loc);
4220 if (modifycode != NOP_EXPR)
4221 add_stmt (rhs);
4223 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4224 an atomic_store. */
4225 if (modifycode == NOP_EXPR)
4227 compound_stmt = rhs;
4228 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4229 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4230 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4231 params->quick_push (lhs_addr);
4232 params->quick_push (rhs);
4233 params->quick_push (seq_cst);
4234 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4236 compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4237 compound_stmt, func_call);
4239 /* VAL is the value which was stored, return a COMPOUND_STMT of
4240 the statement and that value. */
4241 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4244 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4245 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4246 isn't applicable for such builtins. ??? Do we want to handle enums? */
4247 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4248 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4250 built_in_function fncode;
4251 switch (modifycode)
4253 case PLUS_EXPR:
4254 case POINTER_PLUS_EXPR:
4255 fncode = (return_old_p
4256 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4257 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4258 break;
4259 case MINUS_EXPR:
4260 fncode = (return_old_p
4261 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4262 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4263 break;
4264 case BIT_AND_EXPR:
4265 fncode = (return_old_p
4266 ? BUILT_IN_ATOMIC_FETCH_AND_N
4267 : BUILT_IN_ATOMIC_AND_FETCH_N);
4268 break;
4269 case BIT_IOR_EXPR:
4270 fncode = (return_old_p
4271 ? BUILT_IN_ATOMIC_FETCH_OR_N
4272 : BUILT_IN_ATOMIC_OR_FETCH_N);
4273 break;
4274 case BIT_XOR_EXPR:
4275 fncode = (return_old_p
4276 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4277 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4278 break;
4279 default:
4280 goto cas_loop;
4283 /* We can only use "_1" through "_16" variants of the atomic fetch
4284 built-ins. */
4285 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4286 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4287 goto cas_loop;
4289 /* If this is a pointer type, we need to multiply by the size of
4290 the pointer target type. */
4291 if (POINTER_TYPE_P (lhs_type))
4293 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4294 /* ??? This would introduce -Wdiscarded-qualifiers
4295 warning: __atomic_fetch_* expect volatile void *
4296 type as the first argument. (Assignments between
4297 atomic and non-atomic objects are OK.) */
4298 || TYPE_RESTRICT (lhs_type))
4299 goto cas_loop;
4300 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4301 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4302 convert (ptrdiff_type_node, rhs),
4303 convert (ptrdiff_type_node, sz));
4306 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4307 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4308 fndecl = builtin_decl_explicit (fncode);
4309 params->quick_push (lhs_addr);
4310 params->quick_push (rhs);
4311 params->quick_push (seq_cst);
4312 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4314 newval = create_tmp_var_raw (nonatomic_lhs_type);
4315 TREE_ADDRESSABLE (newval) = 1;
4316 suppress_warning (newval);
4317 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4318 NULL_TREE, NULL_TREE);
4319 SET_EXPR_LOCATION (rhs, loc);
4320 add_stmt (rhs);
4322 /* Finish the compound statement. */
4323 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4325 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4326 the statement and that value. */
4327 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4330 cas_loop:
4331 /* Create the variables and labels required for the op= form. */
4332 old = create_tmp_var_raw (nonatomic_lhs_type);
4333 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4334 TREE_ADDRESSABLE (old) = 1;
4335 suppress_warning (old);
4337 newval = create_tmp_var_raw (nonatomic_lhs_type);
4338 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4339 TREE_ADDRESSABLE (newval) = 1;
4340 suppress_warning (newval);
4342 loop_decl = create_artificial_label (loc);
4343 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4345 done_decl = create_artificial_label (loc);
4346 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4348 /* __atomic_load (addr, &old, SEQ_CST). */
4349 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4350 params->quick_push (lhs_addr);
4351 params->quick_push (old_addr);
4352 params->quick_push (seq_cst);
4353 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4354 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4355 NULL_TREE);
4356 add_stmt (old);
4357 params->truncate (0);
4359 /* Create the expressions for floating-point environment
4360 manipulation, if required. */
4361 bool need_fenv = (flag_trapping_math
4362 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4363 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4364 if (need_fenv)
4365 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4367 if (hold_call)
4368 add_stmt (hold_call);
4370 /* loop: */
4371 add_stmt (loop_label);
4373 /* newval = old + val; */
4374 if (rhs_type != rhs_semantic_type)
4375 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4376 rhs = build_binary_op (loc, modifycode, old, val, true);
4377 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4379 tree eptype = TREE_TYPE (rhs);
4380 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4381 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4383 else
4384 rhs = c_fully_fold (rhs, false, NULL);
4385 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4386 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4387 NULL_TREE, 0);
4388 if (rhs != error_mark_node)
4390 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4391 NULL_TREE);
4392 SET_EXPR_LOCATION (rhs, loc);
4393 add_stmt (rhs);
4396 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4397 goto done; */
4398 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4399 params->quick_push (lhs_addr);
4400 params->quick_push (old_addr);
4401 params->quick_push (newval_addr);
4402 params->quick_push (integer_zero_node);
4403 params->quick_push (seq_cst);
4404 params->quick_push (seq_cst);
4405 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4407 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4408 SET_EXPR_LOCATION (goto_stmt, loc);
4410 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4411 SET_EXPR_LOCATION (stmt, loc);
4412 add_stmt (stmt);
4414 if (clear_call)
4415 add_stmt (clear_call);
4417 /* goto loop; */
4418 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4419 SET_EXPR_LOCATION (goto_stmt, loc);
4420 add_stmt (goto_stmt);
4422 /* done: */
4423 add_stmt (done_label);
4425 if (update_call)
4426 add_stmt (update_call);
4428 /* Finish the compound statement. */
4429 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4431 /* NEWVAL is the value that was successfully stored, return a
4432 COMPOUND_EXPR of the statement and the appropriate value. */
4433 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4434 return_old_p ? old : newval);
4437 /* Construct and perhaps optimize a tree representation
4438 for a unary operation. CODE, a tree_code, specifies the operation
4439 and XARG is the operand.
4440 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4441 promotions (such as from short to int).
4442 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4443 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4444 to pointers in C99.
4446 LOCATION is the location of the operator. */
4448 tree
4449 build_unary_op (location_t location, enum tree_code code, tree xarg,
4450 bool noconvert)
4452 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4453 tree arg = xarg;
4454 tree argtype = NULL_TREE;
4455 enum tree_code typecode;
4456 tree val;
4457 tree ret = error_mark_node;
4458 tree eptype = NULL_TREE;
4459 const char *invalid_op_diag;
4460 bool int_operands;
4462 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4463 if (int_operands)
4464 arg = remove_c_maybe_const_expr (arg);
4466 if (code != ADDR_EXPR)
4467 arg = require_complete_type (location, arg);
4469 typecode = TREE_CODE (TREE_TYPE (arg));
4470 if (typecode == ERROR_MARK)
4471 return error_mark_node;
4472 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4473 typecode = INTEGER_TYPE;
4475 if ((invalid_op_diag
4476 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4478 error_at (location, invalid_op_diag);
4479 return error_mark_node;
4482 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4484 eptype = TREE_TYPE (arg);
4485 arg = TREE_OPERAND (arg, 0);
4488 switch (code)
4490 case CONVERT_EXPR:
4491 /* This is used for unary plus, because a CONVERT_EXPR
4492 is enough to prevent anybody from looking inside for
4493 associativity, but won't generate any code. */
4494 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4495 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4496 || gnu_vector_type_p (TREE_TYPE (arg))))
4498 error_at (location, "wrong type argument to unary plus");
4499 return error_mark_node;
4501 else if (!noconvert)
4502 arg = default_conversion (arg);
4503 arg = non_lvalue_loc (location, arg);
4504 break;
4506 case NEGATE_EXPR:
4507 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4508 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4509 || gnu_vector_type_p (TREE_TYPE (arg))))
4511 error_at (location, "wrong type argument to unary minus");
4512 return error_mark_node;
4514 else if (!noconvert)
4515 arg = default_conversion (arg);
4516 break;
4518 case BIT_NOT_EXPR:
4519 /* ~ works on integer types and non float vectors. */
4520 if (typecode == INTEGER_TYPE
4521 || (gnu_vector_type_p (TREE_TYPE (arg))
4522 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4524 tree e = arg;
4526 /* Warn if the expression has boolean value. */
4527 while (TREE_CODE (e) == COMPOUND_EXPR)
4528 e = TREE_OPERAND (e, 1);
4530 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4531 || truth_value_p (TREE_CODE (e))))
4533 auto_diagnostic_group d;
4534 if (warning_at (location, OPT_Wbool_operation,
4535 "%<~%> on a boolean expression"))
4537 gcc_rich_location richloc (location);
4538 richloc.add_fixit_insert_before (location, "!");
4539 inform (&richloc, "did you mean to use logical not?");
4542 if (!noconvert)
4543 arg = default_conversion (arg);
4545 else if (typecode == COMPLEX_TYPE)
4547 code = CONJ_EXPR;
4548 pedwarn (location, OPT_Wpedantic,
4549 "ISO C does not support %<~%> for complex conjugation");
4550 if (!noconvert)
4551 arg = default_conversion (arg);
4553 else
4555 error_at (location, "wrong type argument to bit-complement");
4556 return error_mark_node;
4558 break;
4560 case ABS_EXPR:
4561 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4563 error_at (location, "wrong type argument to abs");
4564 return error_mark_node;
4566 else if (!noconvert)
4567 arg = default_conversion (arg);
4568 break;
4570 case ABSU_EXPR:
4571 if (!(typecode == INTEGER_TYPE))
4573 error_at (location, "wrong type argument to absu");
4574 return error_mark_node;
4576 else if (!noconvert)
4577 arg = default_conversion (arg);
4578 break;
4580 case CONJ_EXPR:
4581 /* Conjugating a real value is a no-op, but allow it anyway. */
4582 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4583 || typecode == COMPLEX_TYPE))
4585 error_at (location, "wrong type argument to conjugation");
4586 return error_mark_node;
4588 else if (!noconvert)
4589 arg = default_conversion (arg);
4590 break;
4592 case TRUTH_NOT_EXPR:
4593 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4594 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4595 && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE)
4597 error_at (location,
4598 "wrong type argument to unary exclamation mark");
4599 return error_mark_node;
4601 if (int_operands)
4603 arg = c_objc_common_truthvalue_conversion (location, xarg);
4604 arg = remove_c_maybe_const_expr (arg);
4606 else
4607 arg = c_objc_common_truthvalue_conversion (location, arg);
4608 ret = invert_truthvalue_loc (location, arg);
4609 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4610 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4611 location = EXPR_LOCATION (ret);
4612 goto return_build_unary_op;
4614 case REALPART_EXPR:
4615 case IMAGPART_EXPR:
4616 ret = build_real_imag_expr (location, code, arg);
4617 if (ret == error_mark_node)
4618 return error_mark_node;
4619 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4620 eptype = TREE_TYPE (eptype);
4621 goto return_build_unary_op;
4623 case PREINCREMENT_EXPR:
4624 case POSTINCREMENT_EXPR:
4625 case PREDECREMENT_EXPR:
4626 case POSTDECREMENT_EXPR:
4628 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4630 tree inner = build_unary_op (location, code,
4631 C_MAYBE_CONST_EXPR_EXPR (arg),
4632 noconvert);
4633 if (inner == error_mark_node)
4634 return error_mark_node;
4635 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4636 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4637 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4638 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4639 goto return_build_unary_op;
4642 /* Complain about anything that is not a true lvalue. In
4643 Objective-C, skip this check for property_refs. */
4644 if (!objc_is_property_ref (arg)
4645 && !lvalue_or_else (location,
4646 arg, ((code == PREINCREMENT_EXPR
4647 || code == POSTINCREMENT_EXPR)
4648 ? lv_increment
4649 : lv_decrement)))
4650 return error_mark_node;
4652 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4654 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4655 warning_at (location, OPT_Wc___compat,
4656 "increment of enumeration value is invalid in C++");
4657 else
4658 warning_at (location, OPT_Wc___compat,
4659 "decrement of enumeration value is invalid in C++");
4662 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4664 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4665 warning_at (location, OPT_Wbool_operation,
4666 "increment of a boolean expression");
4667 else
4668 warning_at (location, OPT_Wbool_operation,
4669 "decrement of a boolean expression");
4672 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4673 arg = c_fully_fold (arg, false, NULL, true);
4675 bool atomic_op;
4676 atomic_op = really_atomic_lvalue (arg);
4678 /* Increment or decrement the real part of the value,
4679 and don't change the imaginary part. */
4680 if (typecode == COMPLEX_TYPE)
4682 tree real, imag;
4684 pedwarn (location, OPT_Wpedantic,
4685 "ISO C does not support %<++%> and %<--%> on complex types");
4687 if (!atomic_op)
4689 arg = stabilize_reference (arg);
4690 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4691 true);
4692 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4693 true);
4694 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4695 if (real == error_mark_node || imag == error_mark_node)
4696 return error_mark_node;
4697 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4698 real, imag);
4699 goto return_build_unary_op;
4703 /* Report invalid types. */
4705 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4706 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4707 && typecode != COMPLEX_TYPE
4708 && !gnu_vector_type_p (TREE_TYPE (arg)))
4710 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4711 error_at (location, "wrong type argument to increment");
4712 else
4713 error_at (location, "wrong type argument to decrement");
4715 return error_mark_node;
4719 tree inc;
4721 argtype = TREE_TYPE (arg);
4723 /* Compute the increment. */
4725 if (typecode == POINTER_TYPE)
4727 /* If pointer target is an incomplete type,
4728 we just cannot know how to do the arithmetic. */
4729 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4731 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4732 error_at (location,
4733 "increment of pointer to an incomplete type %qT",
4734 TREE_TYPE (argtype));
4735 else
4736 error_at (location,
4737 "decrement of pointer to an incomplete type %qT",
4738 TREE_TYPE (argtype));
4740 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4741 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4743 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4744 pedwarn (location, OPT_Wpointer_arith,
4745 "wrong type argument to increment");
4746 else
4747 pedwarn (location, OPT_Wpointer_arith,
4748 "wrong type argument to decrement");
4750 else
4751 verify_type_context (location, TCTX_POINTER_ARITH,
4752 TREE_TYPE (argtype));
4754 inc = c_size_in_bytes (TREE_TYPE (argtype));
4755 inc = convert_to_ptrofftype_loc (location, inc);
4757 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4759 /* For signed fract types, we invert ++ to -- or
4760 -- to ++, and change inc from 1 to -1, because
4761 it is not possible to represent 1 in signed fract constants.
4762 For unsigned fract types, the result always overflows and
4763 we get an undefined (original) or the maximum value. */
4764 if (code == PREINCREMENT_EXPR)
4765 code = PREDECREMENT_EXPR;
4766 else if (code == PREDECREMENT_EXPR)
4767 code = PREINCREMENT_EXPR;
4768 else if (code == POSTINCREMENT_EXPR)
4769 code = POSTDECREMENT_EXPR;
4770 else /* code == POSTDECREMENT_EXPR */
4771 code = POSTINCREMENT_EXPR;
4773 inc = integer_minus_one_node;
4774 inc = convert (argtype, inc);
4776 else
4778 inc = VECTOR_TYPE_P (argtype)
4779 ? build_one_cst (argtype)
4780 : integer_one_node;
4781 inc = convert (argtype, inc);
4784 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4785 need to ask Objective-C to build the increment or decrement
4786 expression for it. */
4787 if (objc_is_property_ref (arg))
4788 return objc_build_incr_expr_for_property_ref (location, code,
4789 arg, inc);
4791 /* Report a read-only lvalue. */
4792 if (TYPE_READONLY (argtype))
4794 readonly_error (location, arg,
4795 ((code == PREINCREMENT_EXPR
4796 || code == POSTINCREMENT_EXPR)
4797 ? lv_increment : lv_decrement));
4798 return error_mark_node;
4800 else if (TREE_READONLY (arg))
4801 readonly_warning (arg,
4802 ((code == PREINCREMENT_EXPR
4803 || code == POSTINCREMENT_EXPR)
4804 ? lv_increment : lv_decrement));
4806 /* If the argument is atomic, use the special code sequences for
4807 atomic compound assignment. */
4808 if (atomic_op)
4810 arg = stabilize_reference (arg);
4811 ret = build_atomic_assign (location, arg,
4812 ((code == PREINCREMENT_EXPR
4813 || code == POSTINCREMENT_EXPR)
4814 ? PLUS_EXPR
4815 : MINUS_EXPR),
4816 (FRACT_MODE_P (TYPE_MODE (argtype))
4817 ? inc
4818 : integer_one_node),
4819 (code == POSTINCREMENT_EXPR
4820 || code == POSTDECREMENT_EXPR));
4821 goto return_build_unary_op;
4824 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4825 val = boolean_increment (code, arg);
4826 else
4827 val = build2 (code, TREE_TYPE (arg), arg, inc);
4828 TREE_SIDE_EFFECTS (val) = 1;
4829 ret = val;
4830 goto return_build_unary_op;
4833 case ADDR_EXPR:
4834 /* Note that this operation never does default_conversion. */
4836 /* The operand of unary '&' must be an lvalue (which excludes
4837 expressions of type void), or, in C99, the result of a [] or
4838 unary '*' operator. */
4839 if (VOID_TYPE_P (TREE_TYPE (arg))
4840 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4841 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4842 pedwarn (location, 0, "taking address of expression of type %<void%>");
4844 /* Let &* cancel out to simplify resulting code. */
4845 if (INDIRECT_REF_P (arg))
4847 /* Don't let this be an lvalue. */
4848 if (lvalue_p (TREE_OPERAND (arg, 0)))
4849 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4850 ret = TREE_OPERAND (arg, 0);
4851 goto return_build_unary_op;
4854 /* Anything not already handled and not a true memory reference
4855 or a non-lvalue array is an error. */
4856 if (typecode != FUNCTION_TYPE && !noconvert
4857 && !lvalue_or_else (location, arg, lv_addressof))
4858 return error_mark_node;
4860 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4861 folding later. */
4862 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4864 tree inner = build_unary_op (location, code,
4865 C_MAYBE_CONST_EXPR_EXPR (arg),
4866 noconvert);
4867 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4868 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4869 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4870 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4871 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4872 goto return_build_unary_op;
4875 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4876 argtype = TREE_TYPE (arg);
4878 /* If the lvalue is const or volatile, merge that into the type
4879 to which the address will point. This is only needed
4880 for function types. */
4881 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4882 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4883 && TREE_CODE (argtype) == FUNCTION_TYPE)
4885 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4886 int quals = orig_quals;
4888 if (TREE_READONLY (arg))
4889 quals |= TYPE_QUAL_CONST;
4890 if (TREE_THIS_VOLATILE (arg))
4891 quals |= TYPE_QUAL_VOLATILE;
4893 argtype = c_build_qualified_type (argtype, quals);
4896 switch (TREE_CODE (arg))
4898 case COMPONENT_REF:
4899 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4901 error_at (location, "cannot take address of bit-field %qD",
4902 TREE_OPERAND (arg, 1));
4903 return error_mark_node;
4906 /* fall through */
4908 case ARRAY_REF:
4909 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4911 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4912 && !POINTER_TYPE_P (TREE_TYPE (arg))
4913 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4915 error_at (location, "cannot take address of scalar with "
4916 "reverse storage order");
4917 return error_mark_node;
4920 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4921 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4922 warning_at (location, OPT_Wscalar_storage_order,
4923 "address of array with reverse scalar storage "
4924 "order requested");
4927 default:
4928 break;
4931 if (!c_mark_addressable (arg))
4932 return error_mark_node;
4934 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4935 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4937 argtype = build_pointer_type (argtype);
4939 /* ??? Cope with user tricks that amount to offsetof. Delete this
4940 when we have proper support for integer constant expressions. */
4941 val = get_base_address (arg);
4942 if (val && INDIRECT_REF_P (val)
4943 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4945 ret = fold_offsetof (arg, argtype);
4946 goto return_build_unary_op;
4949 val = build1 (ADDR_EXPR, argtype, arg);
4951 ret = val;
4952 goto return_build_unary_op;
4954 case PAREN_EXPR:
4955 ret = build1 (code, TREE_TYPE (arg), arg);
4956 goto return_build_unary_op;
4958 default:
4959 gcc_unreachable ();
4962 if (argtype == NULL_TREE)
4963 argtype = TREE_TYPE (arg);
4964 if (TREE_CODE (arg) == INTEGER_CST)
4965 ret = (require_constant_value
4966 ? fold_build1_initializer_loc (location, code, argtype, arg)
4967 : fold_build1_loc (location, code, argtype, arg));
4968 else
4969 ret = build1 (code, argtype, arg);
4970 return_build_unary_op:
4971 gcc_assert (ret != error_mark_node);
4972 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4973 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4974 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4975 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4976 ret = note_integer_operands (ret);
4977 if (eptype)
4978 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4979 protected_set_expr_location (ret, location);
4980 return ret;
4983 /* Return nonzero if REF is an lvalue valid for this language.
4984 Lvalues can be assigned, unless their type has TYPE_READONLY.
4985 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4987 bool
4988 lvalue_p (const_tree ref)
4990 const enum tree_code code = TREE_CODE (ref);
4992 switch (code)
4994 case REALPART_EXPR:
4995 case IMAGPART_EXPR:
4996 case COMPONENT_REF:
4997 return lvalue_p (TREE_OPERAND (ref, 0));
4999 case C_MAYBE_CONST_EXPR:
5000 return lvalue_p (TREE_OPERAND (ref, 1));
5002 case COMPOUND_LITERAL_EXPR:
5003 case STRING_CST:
5004 return true;
5006 case MEM_REF:
5007 case TARGET_MEM_REF:
5008 /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
5009 here as well. */
5010 case INDIRECT_REF:
5011 case ARRAY_REF:
5012 case VAR_DECL:
5013 case PARM_DECL:
5014 case RESULT_DECL:
5015 case ERROR_MARK:
5016 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
5017 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
5019 case BIND_EXPR:
5020 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
5022 default:
5023 return false;
5027 /* Give a warning for storing in something that is read-only in GCC
5028 terms but not const in ISO C terms. */
5030 static void
5031 readonly_warning (tree arg, enum lvalue_use use)
5033 switch (use)
5035 case lv_assign:
5036 warning (0, "assignment of read-only location %qE", arg);
5037 break;
5038 case lv_increment:
5039 warning (0, "increment of read-only location %qE", arg);
5040 break;
5041 case lv_decrement:
5042 warning (0, "decrement of read-only location %qE", arg);
5043 break;
5044 default:
5045 gcc_unreachable ();
5047 return;
5051 /* Return nonzero if REF is an lvalue valid for this language;
5052 otherwise, print an error message and return zero. USE says
5053 how the lvalue is being used and so selects the error message.
5054 LOCATION is the location at which any error should be reported. */
5056 static int
5057 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
5059 int win = lvalue_p (ref);
5061 if (!win)
5062 lvalue_error (loc, use);
5064 return win;
5067 /* Mark EXP saying that we need to be able to take the
5068 address of it; it should not be allocated in a register.
5069 Returns true if successful. ARRAY_REF_P is true if this
5070 is for ARRAY_REF construction - in that case we don't want
5071 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5072 it is fine to use ARRAY_REFs for vector subscripts on vector
5073 register variables. */
5075 bool
5076 c_mark_addressable (tree exp, bool array_ref_p)
5078 tree x = exp;
5080 while (1)
5081 switch (TREE_CODE (x))
5083 case VIEW_CONVERT_EXPR:
5084 if (array_ref_p
5085 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5086 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5087 return true;
5088 x = TREE_OPERAND (x, 0);
5089 break;
5091 case COMPONENT_REF:
5092 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
5094 error ("cannot take address of bit-field %qD",
5095 TREE_OPERAND (x, 1));
5096 return false;
5098 /* FALLTHRU */
5099 case ADDR_EXPR:
5100 case ARRAY_REF:
5101 case REALPART_EXPR:
5102 case IMAGPART_EXPR:
5103 x = TREE_OPERAND (x, 0);
5104 break;
5106 case COMPOUND_LITERAL_EXPR:
5107 TREE_ADDRESSABLE (x) = 1;
5108 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5109 return true;
5111 case CONSTRUCTOR:
5112 TREE_ADDRESSABLE (x) = 1;
5113 return true;
5115 case VAR_DECL:
5116 case CONST_DECL:
5117 case PARM_DECL:
5118 case RESULT_DECL:
5119 if (C_DECL_REGISTER (x)
5120 && DECL_NONLOCAL (x))
5122 if (TREE_PUBLIC (x) || is_global_var (x))
5124 error
5125 ("global register variable %qD used in nested function", x);
5126 return false;
5128 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5130 else if (C_DECL_REGISTER (x))
5132 if (TREE_PUBLIC (x) || is_global_var (x))
5133 error ("address of global register variable %qD requested", x);
5134 else
5135 error ("address of register variable %qD requested", x);
5136 return false;
5139 /* FALLTHRU */
5140 case FUNCTION_DECL:
5141 TREE_ADDRESSABLE (x) = 1;
5142 /* FALLTHRU */
5143 default:
5144 return true;
5148 /* Convert EXPR to TYPE, warning about conversion problems with
5149 constants. SEMANTIC_TYPE is the type this conversion would use
5150 without excess precision. If SEMANTIC_TYPE is NULL, this function
5151 is equivalent to convert_and_check. This function is a wrapper that
5152 handles conversions that may be different than
5153 the usual ones because of excess precision. */
5155 static tree
5156 ep_convert_and_check (location_t loc, tree type, tree expr,
5157 tree semantic_type)
5159 if (TREE_TYPE (expr) == type)
5160 return expr;
5162 /* For C11, integer conversions may have results with excess
5163 precision. */
5164 if (flag_isoc11 || !semantic_type)
5165 return convert_and_check (loc, type, expr);
5167 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5168 && TREE_TYPE (expr) != semantic_type)
5170 /* For integers, we need to check the real conversion, not
5171 the conversion to the excess precision type. */
5172 expr = convert_and_check (loc, semantic_type, expr);
5174 /* Result type is the excess precision type, which should be
5175 large enough, so do not check. */
5176 return convert (type, expr);
5179 /* If EXPR refers to a built-in declared without a prototype returns
5180 the actual type of the built-in and, if non-null, set *BLTIN to
5181 a pointer to the built-in. Otherwise return the type of EXPR
5182 and clear *BLTIN if non-null. */
5184 static tree
5185 type_or_builtin_type (tree expr, tree *bltin = NULL)
5187 tree dummy;
5188 if (!bltin)
5189 bltin = &dummy;
5191 *bltin = NULL_TREE;
5193 tree type = TREE_TYPE (expr);
5194 if (TREE_CODE (expr) != ADDR_EXPR)
5195 return type;
5197 tree oper = TREE_OPERAND (expr, 0);
5198 if (!DECL_P (oper)
5199 || TREE_CODE (oper) != FUNCTION_DECL
5200 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5201 return type;
5203 built_in_function code = DECL_FUNCTION_CODE (oper);
5204 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5205 return type;
5207 if ((*bltin = builtin_decl_implicit (code)))
5208 type = build_pointer_type (TREE_TYPE (*bltin));
5210 return type;
5213 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5214 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5215 if folded to an integer constant then the unselected half may
5216 contain arbitrary operations not normally permitted in constant
5217 expressions. Set the location of the expression to LOC. */
5219 tree
5220 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5221 tree op1, tree op1_original_type, location_t op1_loc,
5222 tree op2, tree op2_original_type, location_t op2_loc)
5224 tree type1;
5225 tree type2;
5226 enum tree_code code1;
5227 enum tree_code code2;
5228 tree result_type = NULL;
5229 tree semantic_result_type = NULL;
5230 tree orig_op1 = op1, orig_op2 = op2;
5231 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5232 bool ifexp_int_operands;
5233 tree ret;
5235 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5236 if (op1_int_operands)
5237 op1 = remove_c_maybe_const_expr (op1);
5238 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5239 if (op2_int_operands)
5240 op2 = remove_c_maybe_const_expr (op2);
5241 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5242 if (ifexp_int_operands)
5243 ifexp = remove_c_maybe_const_expr (ifexp);
5245 /* Promote both alternatives. */
5247 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5248 op1 = default_conversion (op1);
5249 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5250 op2 = default_conversion (op2);
5252 if (TREE_CODE (ifexp) == ERROR_MARK
5253 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5254 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5255 return error_mark_node;
5257 tree bltin1 = NULL_TREE;
5258 tree bltin2 = NULL_TREE;
5259 type1 = type_or_builtin_type (op1, &bltin1);
5260 code1 = TREE_CODE (type1);
5261 type2 = type_or_builtin_type (op2, &bltin2);
5262 code2 = TREE_CODE (type2);
5264 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5265 return error_mark_node;
5267 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5268 return error_mark_node;
5270 /* C90 does not permit non-lvalue arrays in conditional expressions.
5271 In C99 they will be pointers by now. */
5272 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5274 error_at (colon_loc, "non-lvalue array in conditional expression");
5275 return error_mark_node;
5278 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5279 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5280 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5281 || code1 == COMPLEX_TYPE)
5282 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5283 || code2 == COMPLEX_TYPE))
5285 semantic_result_type = c_common_type (type1, type2);
5286 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5288 op1 = TREE_OPERAND (op1, 0);
5289 type1 = TREE_TYPE (op1);
5290 gcc_assert (TREE_CODE (type1) == code1);
5292 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5294 op2 = TREE_OPERAND (op2, 0);
5295 type2 = TREE_TYPE (op2);
5296 gcc_assert (TREE_CODE (type2) == code2);
5300 if (warn_cxx_compat)
5302 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5303 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5305 if (TREE_CODE (t1) == ENUMERAL_TYPE
5306 && TREE_CODE (t2) == ENUMERAL_TYPE
5307 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5308 warning_at (colon_loc, OPT_Wc___compat,
5309 ("different enum types in conditional is "
5310 "invalid in C++: %qT vs %qT"),
5311 t1, t2);
5314 /* Quickly detect the usual case where op1 and op2 have the same type
5315 after promotion. */
5316 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5318 if (type1 == type2)
5319 result_type = type1;
5320 else
5321 result_type = TYPE_MAIN_VARIANT (type1);
5323 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5324 || code1 == COMPLEX_TYPE)
5325 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5326 || code2 == COMPLEX_TYPE))
5328 /* In C11, a conditional expression between a floating-point
5329 type and an integer type should convert the integer type to
5330 the evaluation format of the floating-point type, with
5331 possible excess precision. */
5332 tree eptype1 = type1;
5333 tree eptype2 = type2;
5334 if (flag_isoc11)
5336 tree eptype;
5337 if (ANY_INTEGRAL_TYPE_P (type1)
5338 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5340 eptype2 = eptype;
5341 if (!semantic_result_type)
5342 semantic_result_type = c_common_type (type1, type2);
5344 else if (ANY_INTEGRAL_TYPE_P (type2)
5345 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5347 eptype1 = eptype;
5348 if (!semantic_result_type)
5349 semantic_result_type = c_common_type (type1, type2);
5352 result_type = c_common_type (eptype1, eptype2);
5353 if (result_type == error_mark_node)
5354 return error_mark_node;
5355 do_warn_double_promotion (result_type, type1, type2,
5356 "implicit conversion from %qT to %qT to "
5357 "match other result of conditional",
5358 colon_loc);
5360 /* If -Wsign-compare, warn here if type1 and type2 have
5361 different signedness. We'll promote the signed to unsigned
5362 and later code won't know it used to be different.
5363 Do this check on the original types, so that explicit casts
5364 will be considered, but default promotions won't. */
5365 if (c_inhibit_evaluation_warnings == 0)
5367 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5368 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5370 if (unsigned_op1 ^ unsigned_op2)
5372 bool ovf;
5374 /* Do not warn if the result type is signed, since the
5375 signed type will only be chosen if it can represent
5376 all the values of the unsigned type. */
5377 if (!TYPE_UNSIGNED (result_type))
5378 /* OK */;
5379 else
5381 bool op1_maybe_const = true;
5382 bool op2_maybe_const = true;
5384 /* Do not warn if the signed quantity is an
5385 unsuffixed integer literal (or some static
5386 constant expression involving such literals) and
5387 it is non-negative. This warning requires the
5388 operands to be folded for best results, so do
5389 that folding in this case even without
5390 warn_sign_compare to avoid warning options
5391 possibly affecting code generation. */
5392 c_inhibit_evaluation_warnings
5393 += (ifexp == truthvalue_false_node);
5394 op1 = c_fully_fold (op1, require_constant_value,
5395 &op1_maybe_const);
5396 c_inhibit_evaluation_warnings
5397 -= (ifexp == truthvalue_false_node);
5399 c_inhibit_evaluation_warnings
5400 += (ifexp == truthvalue_true_node);
5401 op2 = c_fully_fold (op2, require_constant_value,
5402 &op2_maybe_const);
5403 c_inhibit_evaluation_warnings
5404 -= (ifexp == truthvalue_true_node);
5406 if (warn_sign_compare)
5408 if ((unsigned_op2
5409 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5410 || (unsigned_op1
5411 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5412 /* OK */;
5413 else if (unsigned_op2)
5414 warning_at (op1_loc, OPT_Wsign_compare,
5415 "operand of %<?:%> changes signedness from "
5416 "%qT to %qT due to unsignedness of other "
5417 "operand", TREE_TYPE (orig_op1),
5418 TREE_TYPE (orig_op2));
5419 else
5420 warning_at (op2_loc, OPT_Wsign_compare,
5421 "operand of %<?:%> changes signedness from "
5422 "%qT to %qT due to unsignedness of other "
5423 "operand", TREE_TYPE (orig_op2),
5424 TREE_TYPE (orig_op1));
5426 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5427 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5428 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5429 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5434 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5436 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5437 pedwarn (colon_loc, OPT_Wpedantic,
5438 "ISO C forbids conditional expr with only one void side");
5439 result_type = void_type_node;
5441 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5443 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5444 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5445 addr_space_t as_common;
5447 if (comp_target_types (colon_loc, type1, type2))
5448 result_type = common_pointer_type (type1, type2);
5449 else if (null_pointer_constant_p (orig_op1))
5450 result_type = type2;
5451 else if (null_pointer_constant_p (orig_op2))
5452 result_type = type1;
5453 else if (!addr_space_superset (as1, as2, &as_common))
5455 error_at (colon_loc, "pointers to disjoint address spaces "
5456 "used in conditional expression");
5457 return error_mark_node;
5459 else if ((VOID_TYPE_P (TREE_TYPE (type1))
5460 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5461 || (VOID_TYPE_P (TREE_TYPE (type2))
5462 && !TYPE_ATOMIC (TREE_TYPE (type2))))
5464 tree t1 = TREE_TYPE (type1);
5465 tree t2 = TREE_TYPE (type2);
5466 if (!(VOID_TYPE_P (t1)
5467 && !TYPE_ATOMIC (t1)))
5469 /* roles are swapped */
5470 t1 = t2;
5471 t2 = TREE_TYPE (type1);
5473 tree t2_stripped = strip_array_types (t2);
5474 if ((TREE_CODE (t2) == ARRAY_TYPE)
5475 && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
5477 if (!flag_isoc2x)
5478 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5479 "pointer to array loses qualifier "
5480 "in conditional expression");
5481 else if (warn_c11_c2x_compat > 0)
5482 warning_at (colon_loc, OPT_Wc11_c2x_compat,
5483 "pointer to array loses qualifier "
5484 "in conditional expression in ISO C before C2X");
5486 if (TREE_CODE (t2) == FUNCTION_TYPE)
5487 pedwarn (colon_loc, OPT_Wpedantic,
5488 "ISO C forbids conditional expr between "
5489 "%<void *%> and function pointer");
5490 /* for array, use qualifiers of element type */
5491 if (flag_isoc2x)
5492 t2 = t2_stripped;
5493 result_type = build_pointer_type (qualify_type (t1, t2));
5495 /* Objective-C pointer comparisons are a bit more lenient. */
5496 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5497 result_type = objc_common_type (type1, type2);
5498 else
5500 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5501 if (bltin1 && bltin2)
5502 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5503 "pointer type mismatch between %qT and %qT "
5504 "of %qD and %qD in conditional expression",
5505 type1, type2, bltin1, bltin2);
5506 else
5507 pedwarn (colon_loc, 0,
5508 "pointer type mismatch in conditional expression");
5509 result_type = build_pointer_type
5510 (build_qualified_type (void_type_node, qual));
5513 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5515 if (!null_pointer_constant_p (orig_op2))
5516 pedwarn (colon_loc, 0,
5517 "pointer/integer type mismatch in conditional expression");
5518 else
5520 op2 = null_pointer_node;
5522 result_type = type1;
5524 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5526 if (!null_pointer_constant_p (orig_op1))
5527 pedwarn (colon_loc, 0,
5528 "pointer/integer type mismatch in conditional expression");
5529 else
5531 op1 = null_pointer_node;
5533 result_type = type2;
5535 /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
5536 type nullptr_t and the other is a pointer, the result type is the pointer
5537 type." */
5538 else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE)
5539 result_type = type2;
5540 else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE)
5541 result_type = type1;
5543 if (!result_type)
5545 if (flag_cond_mismatch)
5546 result_type = void_type_node;
5547 else
5549 error_at (colon_loc, "type mismatch in conditional expression");
5550 return error_mark_node;
5554 /* Merge const and volatile flags of the incoming types. */
5555 result_type
5556 = build_type_variant (result_type,
5557 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5558 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5560 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5561 semantic_result_type);
5562 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5563 semantic_result_type);
5565 if (ifexp_bcp && ifexp == truthvalue_true_node)
5567 op2_int_operands = true;
5568 op1 = c_fully_fold (op1, require_constant_value, NULL);
5570 if (ifexp_bcp && ifexp == truthvalue_false_node)
5572 op1_int_operands = true;
5573 op2 = c_fully_fold (op2, require_constant_value, NULL);
5575 int_const = int_operands = (ifexp_int_operands
5576 && op1_int_operands
5577 && op2_int_operands);
5578 if (int_operands)
5580 int_const = ((ifexp == truthvalue_true_node
5581 && TREE_CODE (orig_op1) == INTEGER_CST
5582 && !TREE_OVERFLOW (orig_op1))
5583 || (ifexp == truthvalue_false_node
5584 && TREE_CODE (orig_op2) == INTEGER_CST
5585 && !TREE_OVERFLOW (orig_op2)));
5588 /* Need to convert condition operand into a vector mask. */
5589 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5591 tree vectype = TREE_TYPE (ifexp);
5592 tree elem_type = TREE_TYPE (vectype);
5593 tree zero = build_int_cst (elem_type, 0);
5594 tree zero_vec = build_vector_from_val (vectype, zero);
5595 tree cmp_type = truth_type_for (vectype);
5596 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5599 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5600 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5601 else
5603 if (int_operands)
5605 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5606 nested inside of the expression. */
5607 op1 = c_fully_fold (op1, false, NULL);
5608 op2 = c_fully_fold (op2, false, NULL);
5610 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5611 if (int_operands)
5612 ret = note_integer_operands (ret);
5614 if (semantic_result_type)
5615 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5617 protected_set_expr_location (ret, colon_loc);
5619 /* If the OP1 and OP2 are the same and don't have side-effects,
5620 warn here, because the COND_EXPR will be turned into OP1. */
5621 if (warn_duplicated_branches
5622 && TREE_CODE (ret) == COND_EXPR
5623 && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
5624 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5625 "this condition has identical branches");
5627 return ret;
5630 /* EXPR is an expression, location LOC, whose result is discarded.
5631 Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
5632 whose right-hand operand is such a call, possibly recursively). */
5634 static void
5635 maybe_warn_nodiscard (location_t loc, tree expr)
5637 if (VOID_TYPE_P (TREE_TYPE (expr)))
5638 return;
5639 while (TREE_CODE (expr) == COMPOUND_EXPR)
5641 expr = TREE_OPERAND (expr, 1);
5642 if (EXPR_HAS_LOCATION (expr))
5643 loc = EXPR_LOCATION (expr);
5645 if (TREE_CODE (expr) != CALL_EXPR)
5646 return;
5647 tree fn = CALL_EXPR_FN (expr);
5648 if (!fn)
5649 return;
5650 tree attr;
5651 if (TREE_CODE (fn) == ADDR_EXPR
5652 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5653 && (attr = lookup_attribute ("nodiscard",
5654 DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
5656 fn = TREE_OPERAND (fn, 0);
5657 tree args = TREE_VALUE (attr);
5658 if (args)
5659 args = TREE_VALUE (args);
5660 auto_diagnostic_group d;
5661 int warned;
5662 if (args)
5663 warned = warning_at (loc, OPT_Wunused_result,
5664 "ignoring return value of %qD, declared with "
5665 "attribute %<nodiscard%>: %E", fn, args);
5666 else
5667 warned = warning_at (loc, OPT_Wunused_result,
5668 "ignoring return value of %qD, declared with "
5669 "attribute %<nodiscard%>", fn);
5670 if (warned)
5671 inform (DECL_SOURCE_LOCATION (fn), "declared here");
5673 else
5675 tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
5676 attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
5677 if (!attr)
5678 return;
5679 tree args = TREE_VALUE (attr);
5680 if (args)
5681 args = TREE_VALUE (args);
5682 auto_diagnostic_group d;
5683 int warned;
5684 if (args)
5685 warned = warning_at (loc, OPT_Wunused_result,
5686 "ignoring return value of type %qT, declared "
5687 "with attribute %<nodiscard%>: %E",
5688 rettype, args);
5689 else
5690 warned = warning_at (loc, OPT_Wunused_result,
5691 "ignoring return value of type %qT, declared "
5692 "with attribute %<nodiscard%>", rettype);
5693 if (warned)
5695 if (TREE_CODE (fn) == ADDR_EXPR)
5697 fn = TREE_OPERAND (fn, 0);
5698 if (TREE_CODE (fn) == FUNCTION_DECL)
5699 inform (DECL_SOURCE_LOCATION (fn),
5700 "in call to %qD, declared here", fn);
5706 /* Return a compound expression that performs two expressions and
5707 returns the value of the second of them.
5709 LOC is the location of the COMPOUND_EXPR. */
5711 tree
5712 build_compound_expr (location_t loc, tree expr1, tree expr2)
5714 bool expr1_int_operands, expr2_int_operands;
5715 tree eptype = NULL_TREE;
5716 tree ret;
5718 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5719 if (expr1_int_operands)
5720 expr1 = remove_c_maybe_const_expr (expr1);
5721 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5722 if (expr2_int_operands)
5723 expr2 = remove_c_maybe_const_expr (expr2);
5725 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5726 expr1 = TREE_OPERAND (expr1, 0);
5727 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5729 eptype = TREE_TYPE (expr2);
5730 expr2 = TREE_OPERAND (expr2, 0);
5733 if (!TREE_SIDE_EFFECTS (expr1))
5735 /* The left-hand operand of a comma expression is like an expression
5736 statement: with -Wunused, we should warn if it doesn't have
5737 any side-effects, unless it was explicitly cast to (void). */
5738 if (warn_unused_value)
5740 if (VOID_TYPE_P (TREE_TYPE (expr1))
5741 && CONVERT_EXPR_P (expr1))
5742 ; /* (void) a, b */
5743 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5744 && TREE_CODE (expr1) == COMPOUND_EXPR
5745 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5746 ; /* (void) a, (void) b, c */
5747 else
5748 warning_at (loc, OPT_Wunused_value,
5749 "left-hand operand of comma expression has no effect");
5752 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5753 && warn_unused_value)
5755 tree r = expr1;
5756 location_t cloc = loc;
5757 while (TREE_CODE (r) == COMPOUND_EXPR)
5759 if (EXPR_HAS_LOCATION (r))
5760 cloc = EXPR_LOCATION (r);
5761 r = TREE_OPERAND (r, 1);
5763 if (!TREE_SIDE_EFFECTS (r)
5764 && !VOID_TYPE_P (TREE_TYPE (r))
5765 && !CONVERT_EXPR_P (r))
5766 warning_at (cloc, OPT_Wunused_value,
5767 "right-hand operand of comma expression has no effect");
5770 /* With -Wunused, we should also warn if the left-hand operand does have
5771 side-effects, but computes a value which is not used. For example, in
5772 `foo() + bar(), baz()' the result of the `+' operator is not used,
5773 so we should issue a warning. */
5774 else if (warn_unused_value)
5775 warn_if_unused_value (expr1, loc);
5777 maybe_warn_nodiscard (loc, expr1);
5779 if (expr2 == error_mark_node)
5780 return error_mark_node;
5782 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5784 if (flag_isoc99
5785 && expr1_int_operands
5786 && expr2_int_operands)
5787 ret = note_integer_operands (ret);
5789 if (eptype)
5790 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5792 protected_set_expr_location (ret, loc);
5793 return ret;
5796 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5797 which we are casting. OTYPE is the type of the expression being
5798 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5799 of the cast. -Wcast-qual appeared on the command line. Named
5800 address space qualifiers are not handled here, because they result
5801 in different warnings. */
5803 static void
5804 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5806 tree in_type = type;
5807 tree in_otype = otype;
5808 int added = 0;
5809 int discarded = 0;
5810 bool is_const;
5812 /* Check that the qualifiers on IN_TYPE are a superset of the
5813 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5814 nodes is uninteresting and we stop as soon as we hit a
5815 non-POINTER_TYPE node on either type. */
5818 in_otype = TREE_TYPE (in_otype);
5819 in_type = TREE_TYPE (in_type);
5821 /* GNU C allows cv-qualified function types. 'const' means the
5822 function is very pure, 'volatile' means it can't return. We
5823 need to warn when such qualifiers are added, not when they're
5824 taken away. */
5825 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5826 && TREE_CODE (in_type) == FUNCTION_TYPE)
5827 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5828 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5829 else
5830 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5831 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5833 while (TREE_CODE (in_type) == POINTER_TYPE
5834 && TREE_CODE (in_otype) == POINTER_TYPE);
5836 if (added)
5837 warning_at (loc, OPT_Wcast_qual,
5838 "cast adds %q#v qualifier to function type", added);
5840 if (discarded)
5841 /* There are qualifiers present in IN_OTYPE that are not present
5842 in IN_TYPE. */
5843 warning_at (loc, OPT_Wcast_qual,
5844 "cast discards %qv qualifier from pointer target type",
5845 discarded);
5847 if (added || discarded)
5848 return;
5850 /* A cast from **T to const **T is unsafe, because it can cause a
5851 const value to be changed with no additional warning. We only
5852 issue this warning if T is the same on both sides, and we only
5853 issue the warning if there are the same number of pointers on
5854 both sides, as otherwise the cast is clearly unsafe anyhow. A
5855 cast is unsafe when a qualifier is added at one level and const
5856 is not present at all outer levels.
5858 To issue this warning, we check at each level whether the cast
5859 adds new qualifiers not already seen. We don't need to special
5860 case function types, as they won't have the same
5861 TYPE_MAIN_VARIANT. */
5863 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5864 return;
5865 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5866 return;
5868 in_type = type;
5869 in_otype = otype;
5870 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5873 in_type = TREE_TYPE (in_type);
5874 in_otype = TREE_TYPE (in_otype);
5875 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5876 && !is_const)
5878 warning_at (loc, OPT_Wcast_qual,
5879 "to be safe all intermediate pointers in cast from "
5880 "%qT to %qT must be %<const%> qualified",
5881 otype, type);
5882 break;
5884 if (is_const)
5885 is_const = TYPE_READONLY (in_type);
5887 while (TREE_CODE (in_type) == POINTER_TYPE);
5890 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5892 static bool
5893 c_safe_arg_type_equiv_p (tree t1, tree t2)
5895 t1 = TYPE_MAIN_VARIANT (t1);
5896 t2 = TYPE_MAIN_VARIANT (t2);
5898 if (TREE_CODE (t1) == POINTER_TYPE
5899 && TREE_CODE (t2) == POINTER_TYPE)
5900 return true;
5902 /* The signedness of the parameter matters only when an integral
5903 type smaller than int is promoted to int, otherwise only the
5904 precision of the parameter matters.
5905 This check should make sure that the callee does not see
5906 undefined values in argument registers. */
5907 if (INTEGRAL_TYPE_P (t1)
5908 && INTEGRAL_TYPE_P (t2)
5909 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5910 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5911 || !targetm.calls.promote_prototypes (NULL_TREE)
5912 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5913 return true;
5915 return comptypes (t1, t2);
5918 /* Check if a type cast between two function types can be considered safe. */
5920 static bool
5921 c_safe_function_type_cast_p (tree t1, tree t2)
5923 if (TREE_TYPE (t1) == void_type_node &&
5924 TYPE_ARG_TYPES (t1) == void_list_node)
5925 return true;
5927 if (TREE_TYPE (t2) == void_type_node &&
5928 TYPE_ARG_TYPES (t2) == void_list_node)
5929 return true;
5931 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5932 return false;
5934 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5935 t1 && t2;
5936 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5937 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5938 return false;
5940 return true;
5943 /* Build an expression representing a cast to type TYPE of expression EXPR.
5944 LOC is the location of the cast-- typically the open paren of the cast. */
5946 tree
5947 build_c_cast (location_t loc, tree type, tree expr)
5949 tree value;
5951 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
5953 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5954 expr = TREE_OPERAND (expr, 0);
5956 value = expr;
5957 if (int_operands)
5958 value = remove_c_maybe_const_expr (value);
5960 if (type == error_mark_node || expr == error_mark_node)
5961 return error_mark_node;
5963 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5964 only in <protocol> qualifications. But when constructing cast expressions,
5965 the protocols do matter and must be kept around. */
5966 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5967 return build1 (NOP_EXPR, type, expr);
5969 type = TYPE_MAIN_VARIANT (type);
5971 if (TREE_CODE (type) == ARRAY_TYPE)
5973 error_at (loc, "cast specifies array type");
5974 return error_mark_node;
5977 if (TREE_CODE (type) == FUNCTION_TYPE)
5979 error_at (loc, "cast specifies function type");
5980 return error_mark_node;
5983 if (!VOID_TYPE_P (type))
5985 value = require_complete_type (loc, value);
5986 if (value == error_mark_node)
5987 return error_mark_node;
5990 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5992 if (RECORD_OR_UNION_TYPE_P (type))
5993 pedwarn (loc, OPT_Wpedantic,
5994 "ISO C forbids casting nonscalar to the same type");
5996 /* Convert to remove any qualifiers from VALUE's type. */
5997 value = convert (type, value);
5999 else if (TREE_CODE (type) == UNION_TYPE)
6001 tree field;
6003 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6004 if (TREE_TYPE (field) != error_mark_node
6005 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
6006 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
6007 break;
6009 if (field)
6011 tree t;
6012 bool maybe_const = true;
6014 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
6015 t = c_fully_fold (value, false, &maybe_const);
6016 t = build_constructor_single (type, field, t);
6017 if (!maybe_const)
6018 t = c_wrap_maybe_const (t, true);
6019 t = digest_init (loc, type, t,
6020 NULL_TREE, false, true, 0);
6021 TREE_CONSTANT (t) = TREE_CONSTANT (value);
6022 return t;
6024 error_at (loc, "cast to union type from type not present in union");
6025 return error_mark_node;
6027 else
6029 tree otype, ovalue;
6031 if (type == void_type_node)
6033 tree t = build1 (CONVERT_EXPR, type, value);
6034 SET_EXPR_LOCATION (t, loc);
6035 return t;
6038 otype = TREE_TYPE (value);
6040 /* Optionally warn about potentially worrisome casts. */
6041 if (warn_cast_qual
6042 && TREE_CODE (type) == POINTER_TYPE
6043 && TREE_CODE (otype) == POINTER_TYPE)
6044 handle_warn_cast_qual (loc, type, otype);
6046 /* Warn about conversions between pointers to disjoint
6047 address spaces. */
6048 if (TREE_CODE (type) == POINTER_TYPE
6049 && TREE_CODE (otype) == POINTER_TYPE
6050 && !null_pointer_constant_p (value))
6052 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
6053 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
6054 addr_space_t as_common;
6056 if (!addr_space_superset (as_to, as_from, &as_common))
6058 if (ADDR_SPACE_GENERIC_P (as_from))
6059 warning_at (loc, 0, "cast to %qs address space pointer "
6060 "from disjoint generic address space pointer",
6061 c_addr_space_name (as_to));
6063 else if (ADDR_SPACE_GENERIC_P (as_to))
6064 warning_at (loc, 0, "cast to generic address space pointer "
6065 "from disjoint %qs address space pointer",
6066 c_addr_space_name (as_from));
6068 else
6069 warning_at (loc, 0, "cast to %qs address space pointer "
6070 "from disjoint %qs address space pointer",
6071 c_addr_space_name (as_to),
6072 c_addr_space_name (as_from));
6076 /* Warn about possible alignment problems. */
6077 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
6078 && TREE_CODE (type) == POINTER_TYPE
6079 && TREE_CODE (otype) == POINTER_TYPE
6080 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
6081 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6082 /* Don't warn about opaque types, where the actual alignment
6083 restriction is unknown. */
6084 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
6085 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
6086 && min_align_of_type (TREE_TYPE (type))
6087 > min_align_of_type (TREE_TYPE (otype)))
6088 warning_at (loc, OPT_Wcast_align,
6089 "cast increases required alignment of target type");
6091 if (TREE_CODE (type) == INTEGER_TYPE
6092 && TREE_CODE (otype) == POINTER_TYPE
6093 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
6094 /* Unlike conversion of integers to pointers, where the
6095 warning is disabled for converting constants because
6096 of cases such as SIG_*, warn about converting constant
6097 pointers to integers. In some cases it may cause unwanted
6098 sign extension, and a warning is appropriate. */
6099 warning_at (loc, OPT_Wpointer_to_int_cast,
6100 "cast from pointer to integer of different size");
6102 if (TREE_CODE (value) == CALL_EXPR
6103 && TREE_CODE (type) != TREE_CODE (otype))
6104 warning_at (loc, OPT_Wbad_function_cast,
6105 "cast from function call of type %qT "
6106 "to non-matching type %qT", otype, type);
6108 if (TREE_CODE (type) == POINTER_TYPE
6109 && TREE_CODE (otype) == INTEGER_TYPE
6110 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
6111 /* Don't warn about converting any constant. */
6112 && !TREE_CONSTANT (value))
6113 warning_at (loc,
6114 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
6115 "of different size");
6117 if (warn_strict_aliasing <= 2)
6118 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
6120 /* If pedantic, warn for conversions between function and object
6121 pointer types, except for converting a null pointer constant
6122 to function pointer type. */
6123 if (pedantic
6124 && TREE_CODE (type) == POINTER_TYPE
6125 && TREE_CODE (otype) == POINTER_TYPE
6126 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6127 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
6128 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6129 "conversion of function pointer to object pointer type");
6131 if (pedantic
6132 && TREE_CODE (type) == POINTER_TYPE
6133 && TREE_CODE (otype) == POINTER_TYPE
6134 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6135 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6136 && !null_pointer_constant_p (value))
6137 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6138 "conversion of object pointer to function pointer type");
6140 if (TREE_CODE (type) == POINTER_TYPE
6141 && TREE_CODE (otype) == POINTER_TYPE
6142 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6143 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6144 && !c_safe_function_type_cast_p (TREE_TYPE (type),
6145 TREE_TYPE (otype)))
6146 warning_at (loc, OPT_Wcast_function_type,
6147 "cast between incompatible function types"
6148 " from %qT to %qT", otype, type);
6150 ovalue = value;
6151 value = convert (type, value);
6153 /* Ignore any integer overflow caused by the cast. */
6154 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
6156 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6158 if (!TREE_OVERFLOW (value))
6160 /* Avoid clobbering a shared constant. */
6161 value = copy_node (value);
6162 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
6165 else if (TREE_OVERFLOW (value))
6166 /* Reset VALUE's overflow flags, ensuring constant sharing. */
6167 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6171 /* Don't let a cast be an lvalue. */
6172 if (lvalue_p (value))
6173 value = non_lvalue_loc (loc, value);
6175 /* Don't allow the results of casting to floating-point or complex
6176 types be confused with actual constants, or casts involving
6177 integer and pointer types other than direct integer-to-integer
6178 and integer-to-pointer be confused with integer constant
6179 expressions and null pointer constants. */
6180 if (TREE_CODE (value) == REAL_CST
6181 || TREE_CODE (value) == COMPLEX_CST
6182 || (TREE_CODE (value) == INTEGER_CST
6183 && !((TREE_CODE (expr) == INTEGER_CST
6184 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6185 || TREE_CODE (expr) == REAL_CST
6186 || TREE_CODE (expr) == COMPLEX_CST)))
6187 value = build1 (NOP_EXPR, type, value);
6189 /* If the expression has integer operands and so can occur in an
6190 unevaluated part of an integer constant expression, ensure the
6191 return value reflects this. */
6192 if (int_operands
6193 && INTEGRAL_TYPE_P (type)
6194 && value != error_mark_node
6195 && !EXPR_INT_CONST_OPERANDS (value))
6196 value = note_integer_operands (value);
6198 protected_set_expr_location (value, loc);
6199 return value;
6202 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
6203 location of the open paren of the cast, or the position of the cast
6204 expr. */
6205 tree
6206 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6208 tree type;
6209 tree type_expr = NULL_TREE;
6210 bool type_expr_const = true;
6211 tree ret;
6212 int saved_wsp = warn_strict_prototypes;
6214 /* This avoids warnings about unprototyped casts on
6215 integers. E.g. "#define SIG_DFL (void(*)())0". */
6216 if (TREE_CODE (expr) == INTEGER_CST)
6217 warn_strict_prototypes = 0;
6218 type = groktypename (type_name, &type_expr, &type_expr_const);
6219 warn_strict_prototypes = saved_wsp;
6221 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6222 && reject_gcc_builtin (expr))
6223 return error_mark_node;
6225 ret = build_c_cast (loc, type, expr);
6226 if (type_expr)
6228 bool inner_expr_const = true;
6229 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6230 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6231 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6232 && inner_expr_const);
6233 SET_EXPR_LOCATION (ret, loc);
6236 if (!EXPR_HAS_LOCATION (ret))
6237 protected_set_expr_location (ret, loc);
6239 /* C++ does not permits types to be defined in a cast, but it
6240 allows references to incomplete types. */
6241 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6242 warning_at (loc, OPT_Wc___compat,
6243 "defining a type in a cast is invalid in C++");
6245 return ret;
6248 /* Build an assignment expression of lvalue LHS from value RHS.
6249 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6250 may differ from TREE_TYPE (LHS) for an enum bitfield.
6251 MODIFYCODE is the code for a binary operator that we use
6252 to combine the old value of LHS with RHS to get the new value.
6253 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6254 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6255 which may differ from TREE_TYPE (RHS) for an enum value.
6257 LOCATION is the location of the MODIFYCODE operator.
6258 RHS_LOC is the location of the RHS. */
6260 tree
6261 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6262 enum tree_code modifycode,
6263 location_t rhs_loc, tree rhs, tree rhs_origtype)
6265 tree result;
6266 tree newrhs;
6267 tree rhseval = NULL_TREE;
6268 tree lhstype = TREE_TYPE (lhs);
6269 tree olhstype = lhstype;
6270 bool npc;
6271 bool is_atomic_op;
6273 /* Types that aren't fully specified cannot be used in assignments. */
6274 lhs = require_complete_type (location, lhs);
6276 /* Avoid duplicate error messages from operands that had errors. */
6277 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6278 return error_mark_node;
6280 /* Ensure an error for assigning a non-lvalue array to an array in
6281 C90. */
6282 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6284 error_at (location, "assignment to expression with array type");
6285 return error_mark_node;
6288 /* For ObjC properties, defer this check. */
6289 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6290 return error_mark_node;
6292 is_atomic_op = really_atomic_lvalue (lhs);
6294 newrhs = rhs;
6296 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6298 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6299 lhs_origtype, modifycode, rhs_loc, rhs,
6300 rhs_origtype);
6301 if (inner == error_mark_node)
6302 return error_mark_node;
6303 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6304 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6305 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6306 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6307 protected_set_expr_location (result, location);
6308 return result;
6311 /* If a binary op has been requested, combine the old LHS value with the RHS
6312 producing the value we should actually store into the LHS. */
6314 if (modifycode != NOP_EXPR)
6316 lhs = c_fully_fold (lhs, false, NULL, true);
6317 lhs = stabilize_reference (lhs);
6319 /* Construct the RHS for any non-atomic compound assignemnt. */
6320 if (!is_atomic_op)
6322 /* If in LHS op= RHS the RHS has side-effects, ensure they
6323 are preevaluated before the rest of the assignment expression's
6324 side-effects, because RHS could contain e.g. function calls
6325 that modify LHS. */
6326 if (TREE_SIDE_EFFECTS (rhs))
6328 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6329 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6330 else
6331 newrhs = save_expr (rhs);
6332 rhseval = newrhs;
6333 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6334 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6335 newrhs);
6337 newrhs = build_binary_op (location,
6338 modifycode, lhs, newrhs, true);
6340 /* The original type of the right hand side is no longer
6341 meaningful. */
6342 rhs_origtype = NULL_TREE;
6346 if (c_dialect_objc ())
6348 /* Check if we are modifying an Objective-C property reference;
6349 if so, we need to generate setter calls. */
6350 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6351 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6352 else
6353 result = objc_maybe_build_modify_expr (lhs, newrhs);
6354 if (result)
6355 goto return_result;
6357 /* Else, do the check that we postponed for Objective-C. */
6358 if (!lvalue_or_else (location, lhs, lv_assign))
6359 return error_mark_node;
6362 /* Give an error for storing in something that is 'const'. */
6364 if (TYPE_READONLY (lhstype)
6365 || (RECORD_OR_UNION_TYPE_P (lhstype)
6366 && C_TYPE_FIELDS_READONLY (lhstype)))
6368 readonly_error (location, lhs, lv_assign);
6369 return error_mark_node;
6371 else if (TREE_READONLY (lhs))
6372 readonly_warning (lhs, lv_assign);
6374 /* If storing into a structure or union member,
6375 it has probably been given type `int'.
6376 Compute the type that would go with
6377 the actual amount of storage the member occupies. */
6379 if (TREE_CODE (lhs) == COMPONENT_REF
6380 && (TREE_CODE (lhstype) == INTEGER_TYPE
6381 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6382 || TREE_CODE (lhstype) == REAL_TYPE
6383 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6384 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6386 /* If storing in a field that is in actuality a short or narrower than one,
6387 we must store in the field in its actual type. */
6389 if (lhstype != TREE_TYPE (lhs))
6391 lhs = copy_node (lhs);
6392 TREE_TYPE (lhs) = lhstype;
6395 /* Issue -Wc++-compat warnings about an assignment to an enum type
6396 when LHS does not have its original type. This happens for,
6397 e.g., an enum bitfield in a struct. */
6398 if (warn_cxx_compat
6399 && lhs_origtype != NULL_TREE
6400 && lhs_origtype != lhstype
6401 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6403 tree checktype = (rhs_origtype != NULL_TREE
6404 ? rhs_origtype
6405 : TREE_TYPE (rhs));
6406 if (checktype != error_mark_node
6407 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6408 || (is_atomic_op && modifycode != NOP_EXPR)))
6409 warning_at (location, OPT_Wc___compat,
6410 "enum conversion in assignment is invalid in C++");
6413 /* Remove qualifiers. */
6414 lhstype = build_qualified_type (lhstype, TYPE_UNQUALIFIED);
6415 olhstype = build_qualified_type (olhstype, TYPE_UNQUALIFIED);
6417 /* Convert new value to destination type. Fold it first, then
6418 restore any excess precision information, for the sake of
6419 conversion warnings. */
6421 if (!(is_atomic_op && modifycode != NOP_EXPR))
6423 tree rhs_semantic_type = NULL_TREE;
6424 if (!c_in_omp_for)
6426 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6428 rhs_semantic_type = TREE_TYPE (newrhs);
6429 newrhs = TREE_OPERAND (newrhs, 0);
6431 npc = null_pointer_constant_p (newrhs);
6432 newrhs = c_fully_fold (newrhs, false, NULL);
6433 if (rhs_semantic_type)
6434 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6436 else
6437 npc = null_pointer_constant_p (newrhs);
6438 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6439 rhs_origtype, ic_assign, npc,
6440 NULL_TREE, NULL_TREE, 0);
6441 if (TREE_CODE (newrhs) == ERROR_MARK)
6442 return error_mark_node;
6445 /* Emit ObjC write barrier, if necessary. */
6446 if (c_dialect_objc () && flag_objc_gc)
6448 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6449 if (result)
6451 protected_set_expr_location (result, location);
6452 goto return_result;
6456 /* Scan operands. */
6458 if (is_atomic_op)
6459 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6460 else
6462 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6463 TREE_SIDE_EFFECTS (result) = 1;
6464 protected_set_expr_location (result, location);
6467 /* If we got the LHS in a different type for storing in,
6468 convert the result back to the nominal type of LHS
6469 so that the value we return always has the same type
6470 as the LHS argument. */
6472 if (olhstype == TREE_TYPE (result))
6473 goto return_result;
6475 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6476 rhs_origtype, ic_assign, false, NULL_TREE,
6477 NULL_TREE, 0);
6478 protected_set_expr_location (result, location);
6480 return_result:
6481 if (rhseval)
6482 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6483 return result;
6486 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6487 This is used to implement -fplan9-extensions. */
6489 static bool
6490 find_anonymous_field_with_type (tree struct_type, tree type)
6492 tree field;
6493 bool found;
6495 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6496 found = false;
6497 for (field = TYPE_FIELDS (struct_type);
6498 field != NULL_TREE;
6499 field = TREE_CHAIN (field))
6501 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6502 ? c_build_qualified_type (TREE_TYPE (field),
6503 TYPE_QUAL_ATOMIC)
6504 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6505 if (DECL_NAME (field) == NULL
6506 && comptypes (type, fieldtype))
6508 if (found)
6509 return false;
6510 found = true;
6512 else if (DECL_NAME (field) == NULL
6513 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6514 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6516 if (found)
6517 return false;
6518 found = true;
6521 return found;
6524 /* RHS is an expression whose type is pointer to struct. If there is
6525 an anonymous field in RHS with type TYPE, then return a pointer to
6526 that field in RHS. This is used with -fplan9-extensions. This
6527 returns NULL if no conversion could be found. */
6529 static tree
6530 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6532 tree rhs_struct_type, lhs_main_type;
6533 tree field, found_field;
6534 bool found_sub_field;
6535 tree ret;
6537 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6538 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6539 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6541 gcc_assert (POINTER_TYPE_P (type));
6542 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6543 ? c_build_qualified_type (TREE_TYPE (type),
6544 TYPE_QUAL_ATOMIC)
6545 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6547 found_field = NULL_TREE;
6548 found_sub_field = false;
6549 for (field = TYPE_FIELDS (rhs_struct_type);
6550 field != NULL_TREE;
6551 field = TREE_CHAIN (field))
6553 if (DECL_NAME (field) != NULL_TREE
6554 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6555 continue;
6556 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6557 ? c_build_qualified_type (TREE_TYPE (field),
6558 TYPE_QUAL_ATOMIC)
6559 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6560 if (comptypes (lhs_main_type, fieldtype))
6562 if (found_field != NULL_TREE)
6563 return NULL_TREE;
6564 found_field = field;
6566 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6567 lhs_main_type))
6569 if (found_field != NULL_TREE)
6570 return NULL_TREE;
6571 found_field = field;
6572 found_sub_field = true;
6576 if (found_field == NULL_TREE)
6577 return NULL_TREE;
6579 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6580 build_fold_indirect_ref (rhs), found_field,
6581 NULL_TREE);
6582 ret = build_fold_addr_expr_loc (location, ret);
6584 if (found_sub_field)
6586 ret = convert_to_anonymous_field (location, type, ret);
6587 gcc_assert (ret != NULL_TREE);
6590 return ret;
6593 /* Issue an error message for a bad initializer component.
6594 GMSGID identifies the message.
6595 The component name is taken from the spelling stack. */
6597 static void ATTRIBUTE_GCC_DIAG (2,0)
6598 error_init (location_t loc, const char *gmsgid, ...)
6600 char *ofwhat;
6602 auto_diagnostic_group d;
6604 /* The gmsgid may be a format string with %< and %>. */
6605 va_list ap;
6606 va_start (ap, gmsgid);
6607 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6608 va_end (ap);
6610 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6611 if (*ofwhat && warned)
6612 inform (loc, "(near initialization for %qs)", ofwhat);
6615 /* Issue a pedantic warning for a bad initializer component. OPT is
6616 the option OPT_* (from options.h) controlling this warning or 0 if
6617 it is unconditionally given. GMSGID identifies the message. The
6618 component name is taken from the spelling stack. */
6620 static void ATTRIBUTE_GCC_DIAG (3,0)
6621 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6623 /* Use the location where a macro was expanded rather than where
6624 it was defined to make sure macros defined in system headers
6625 but used incorrectly elsewhere are diagnosed. */
6626 location_t exploc = expansion_point_location_if_in_system_header (loc);
6627 auto_diagnostic_group d;
6628 va_list ap;
6629 va_start (ap, gmsgid);
6630 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6631 va_end (ap);
6632 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6633 if (*ofwhat && warned)
6634 inform (exploc, "(near initialization for %qs)", ofwhat);
6637 /* Issue a warning for a bad initializer component.
6639 OPT is the OPT_W* value corresponding to the warning option that
6640 controls this warning. GMSGID identifies the message. The
6641 component name is taken from the spelling stack. */
6643 static void
6644 warning_init (location_t loc, int opt, const char *gmsgid)
6646 char *ofwhat;
6647 bool warned;
6649 auto_diagnostic_group d;
6651 /* Use the location where a macro was expanded rather than where
6652 it was defined to make sure macros defined in system headers
6653 but used incorrectly elsewhere are diagnosed. */
6654 location_t exploc = expansion_point_location_if_in_system_header (loc);
6656 /* The gmsgid may be a format string with %< and %>. */
6657 warned = warning_at (exploc, opt, gmsgid);
6658 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6659 if (*ofwhat && warned)
6660 inform (exploc, "(near initialization for %qs)", ofwhat);
6663 /* If TYPE is an array type and EXPR is a parenthesized string
6664 constant, warn if pedantic that EXPR is being used to initialize an
6665 object of type TYPE. */
6667 void
6668 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6670 if (pedantic
6671 && TREE_CODE (type) == ARRAY_TYPE
6672 && TREE_CODE (expr.value) == STRING_CST
6673 && expr.original_code != STRING_CST)
6674 pedwarn_init (loc, OPT_Wpedantic,
6675 "array initialized from parenthesized string constant");
6678 /* Attempt to locate the parameter with the given index within FNDECL,
6679 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6681 static location_t
6682 get_fndecl_argument_location (tree fndecl, int argnum)
6684 int i;
6685 tree param;
6687 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6688 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6689 i < argnum && param;
6690 i++, param = TREE_CHAIN (param))
6693 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6694 return DECL_SOURCE_LOCATION (FNDECL). */
6695 if (param == NULL)
6696 return DECL_SOURCE_LOCATION (fndecl);
6698 return DECL_SOURCE_LOCATION (param);
6701 /* Issue a note about a mismatching argument for parameter PARMNUM
6702 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6703 Attempt to issue the note at the pertinent parameter of the decl;
6704 failing that issue it at the location of FUNDECL; failing that
6705 issue it at PLOC. */
6707 static void
6708 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6709 tree expected_type, tree actual_type)
6711 location_t loc;
6712 if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6713 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6714 else
6715 loc = ploc;
6717 inform (loc,
6718 "expected %qT but argument is of type %qT",
6719 expected_type, actual_type);
6722 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6723 function FUNDECL declared without prototype to parameter PARMNUM of
6724 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6726 static void
6727 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6728 tree parmtype, tree argtype)
6730 tree_code parmcode = TREE_CODE (parmtype);
6731 tree_code argcode = TREE_CODE (argtype);
6732 tree promoted = c_type_promotes_to (argtype);
6734 /* Avoid warning for enum arguments that promote to an integer type
6735 of the same size/mode. */
6736 if (parmcode == INTEGER_TYPE
6737 && argcode == ENUMERAL_TYPE
6738 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6739 return;
6741 if ((parmcode == argcode
6742 || (parmcode == INTEGER_TYPE
6743 && argcode == ENUMERAL_TYPE))
6744 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6745 return;
6747 /* This diagnoses even signed/unsigned mismatches. Those might be
6748 safe in many cases but GCC may emit suboptimal code for them so
6749 warning on those cases drives efficiency improvements. */
6750 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6751 TYPE_MAIN_VARIANT (promoted) == argtype
6752 ? G_("%qD argument %d type is %qT where %qT is expected "
6753 "in a call to built-in function declared without "
6754 "prototype")
6755 : G_("%qD argument %d promotes to %qT where %qT is expected "
6756 "in a call to built-in function declared without "
6757 "prototype"),
6758 fundecl, parmnum, promoted, parmtype))
6759 inform (DECL_SOURCE_LOCATION (fundecl),
6760 "built-in %qD declared here",
6761 fundecl);
6764 /* Convert value RHS to type TYPE as preparation for an assignment to
6765 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6766 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6767 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6768 constant before any folding.
6769 The real work of conversion is done by `convert'.
6770 The purpose of this function is to generate error messages
6771 for assignments that are not allowed in C.
6772 ERRTYPE says whether it is argument passing, assignment,
6773 initialization or return.
6775 In the following example, '~' denotes where EXPR_LOC and '^' where
6776 LOCATION point to:
6778 f (var); [ic_argpass]
6779 ^ ~~~
6780 x = var; [ic_assign]
6781 ^ ~~~;
6782 int x = var; [ic_init]
6784 return x; [ic_return]
6787 FUNCTION is a tree for the function being called.
6788 PARMNUM is the number of the argument, for printing in error messages.
6789 WARNOPT may be set to a warning option to issue the corresponding warning
6790 rather than an error for invalid conversions. Used for calls to built-in
6791 functions declared without a prototype. */
6793 static tree
6794 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6795 tree rhs, tree origtype, enum impl_conv errtype,
6796 bool null_pointer_constant, tree fundecl,
6797 tree function, int parmnum, int warnopt /* = 0 */)
6799 enum tree_code codel = TREE_CODE (type);
6800 tree orig_rhs = rhs;
6801 tree rhstype;
6802 enum tree_code coder;
6803 tree rname = NULL_TREE;
6804 bool objc_ok = false;
6806 /* Use the expansion point location to handle cases such as user's
6807 function returning a wrong-type macro defined in a system header. */
6808 location = expansion_point_location_if_in_system_header (location);
6810 if (errtype == ic_argpass)
6812 tree selector;
6813 /* Change pointer to function to the function itself for
6814 diagnostics. */
6815 if (TREE_CODE (function) == ADDR_EXPR
6816 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6817 function = TREE_OPERAND (function, 0);
6819 /* Handle an ObjC selector specially for diagnostics. */
6820 selector = objc_message_selector ();
6821 rname = function;
6822 if (selector && parmnum > 2)
6824 rname = selector;
6825 parmnum -= 2;
6829 /* This macro is used to emit diagnostics to ensure that all format
6830 strings are complete sentences, visible to gettext and checked at
6831 compile time. */
6832 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6833 do { \
6834 switch (errtype) \
6836 case ic_argpass: \
6838 auto_diagnostic_group d; \
6839 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6840 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6842 break; \
6843 case ic_assign: \
6844 pedwarn (LOCATION, OPT, AS); \
6845 break; \
6846 case ic_init: \
6847 case ic_init_const: \
6848 pedwarn_init (LOCATION, OPT, IN); \
6849 break; \
6850 case ic_return: \
6851 pedwarn (LOCATION, OPT, RE); \
6852 break; \
6853 default: \
6854 gcc_unreachable (); \
6856 } while (0)
6858 /* This macro is used to emit diagnostics to ensure that all format
6859 strings are complete sentences, visible to gettext and checked at
6860 compile time. It can be called with 'pedwarn' or 'warning_at'. */
6861 #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6862 do { \
6863 switch (errtype) \
6865 case ic_argpass: \
6867 auto_diagnostic_group d; \
6868 if (PEDWARN) { \
6869 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6870 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6871 } else { \
6872 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6873 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6876 break; \
6877 case ic_assign: \
6878 if (PEDWARN) \
6879 pedwarn (LOCATION, OPT, AS, QUALS); \
6880 else \
6881 warning_at (LOCATION, OPT, AS, QUALS); \
6882 break; \
6883 case ic_init: \
6884 case ic_init_const: \
6885 if (PEDWARN) \
6886 pedwarn (LOCATION, OPT, IN, QUALS); \
6887 else \
6888 warning_at (LOCATION, OPT, IN, QUALS); \
6889 break; \
6890 case ic_return: \
6891 if (PEDWARN) \
6892 pedwarn (LOCATION, OPT, RE, QUALS); \
6893 else \
6894 warning_at (LOCATION, OPT, RE, QUALS); \
6895 break; \
6896 default: \
6897 gcc_unreachable (); \
6899 } while (0)
6901 /* This macro is used to emit diagnostics to ensure that all format
6902 strings are complete sentences, visible to gettext and checked at
6903 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6904 extra parameter to enumerate qualifiers. */
6905 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6906 WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
6909 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6910 rhs = TREE_OPERAND (rhs, 0);
6912 rhstype = TREE_TYPE (rhs);
6913 coder = TREE_CODE (rhstype);
6915 if (coder == ERROR_MARK)
6916 return error_mark_node;
6918 if (c_dialect_objc ())
6920 int parmno;
6922 switch (errtype)
6924 case ic_return:
6925 parmno = 0;
6926 break;
6928 case ic_assign:
6929 parmno = -1;
6930 break;
6932 case ic_init:
6933 case ic_init_const:
6934 parmno = -2;
6935 break;
6937 default:
6938 parmno = parmnum;
6939 break;
6942 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6945 if (warn_cxx_compat)
6947 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6948 if (checktype != error_mark_node
6949 && TREE_CODE (type) == ENUMERAL_TYPE
6950 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6951 switch (errtype)
6953 case ic_argpass:
6954 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6955 "passing argument %d of %qE is invalid in C++",
6956 parmnum, rname))
6957 inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6958 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6959 "expected %qT but argument is of type %qT",
6960 type, rhstype);
6961 break;
6962 case ic_assign:
6963 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6964 "%qT in assignment is invalid in C++", rhstype, type);
6965 break;
6966 case ic_init:
6967 case ic_init_const:
6968 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6969 "%qT to %qT in initialization is invalid in C++",
6970 rhstype, type);
6971 break;
6972 case ic_return:
6973 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6974 "%qT in return is invalid in C++", rhstype, type);
6975 break;
6976 default:
6977 gcc_unreachable ();
6981 if (warn_enum_conversion)
6983 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6984 if (checktype != error_mark_node
6985 && TREE_CODE (checktype) == ENUMERAL_TYPE
6986 && TREE_CODE (type) == ENUMERAL_TYPE
6987 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6989 gcc_rich_location loc (location);
6990 warning_at (&loc, OPT_Wenum_conversion,
6991 "implicit conversion from %qT to %qT",
6992 checktype, type);
6996 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6998 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
6999 return rhs;
7002 if (coder == VOID_TYPE)
7004 /* Except for passing an argument to an unprototyped function,
7005 this is a constraint violation. When passing an argument to
7006 an unprototyped function, it is compile-time undefined;
7007 making it a constraint in that case was rejected in
7008 DR#252. */
7009 const char msg[] = "void value not ignored as it ought to be";
7010 if (warnopt)
7011 warning_at (location, warnopt, msg);
7012 else
7013 error_at (location, msg);
7014 return error_mark_node;
7016 rhs = require_complete_type (location, rhs);
7017 if (rhs == error_mark_node)
7018 return error_mark_node;
7020 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
7021 return error_mark_node;
7023 /* A non-reference type can convert to a reference. This handles
7024 va_start, va_copy and possibly port built-ins. */
7025 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
7027 if (!lvalue_p (rhs))
7029 const char msg[] = "cannot pass rvalue to reference parameter";
7030 if (warnopt)
7031 warning_at (location, warnopt, msg);
7032 else
7033 error_at (location, msg);
7034 return error_mark_node;
7036 if (!c_mark_addressable (rhs))
7037 return error_mark_node;
7038 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
7039 SET_EXPR_LOCATION (rhs, location);
7041 rhs = convert_for_assignment (location, expr_loc,
7042 build_pointer_type (TREE_TYPE (type)),
7043 rhs, origtype, errtype,
7044 null_pointer_constant, fundecl, function,
7045 parmnum, warnopt);
7046 if (rhs == error_mark_node)
7047 return error_mark_node;
7049 rhs = build1 (NOP_EXPR, type, rhs);
7050 SET_EXPR_LOCATION (rhs, location);
7051 return rhs;
7053 /* Some types can interconvert without explicit casts. */
7054 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
7055 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
7056 return convert (type, rhs);
7057 /* Arithmetic types all interconvert, and enum is treated like int. */
7058 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
7059 || codel == FIXED_POINT_TYPE
7060 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
7061 || codel == BOOLEAN_TYPE)
7062 && (coder == INTEGER_TYPE || coder == REAL_TYPE
7063 || coder == FIXED_POINT_TYPE
7064 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
7065 || coder == BOOLEAN_TYPE))
7067 if (warnopt && errtype == ic_argpass)
7068 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
7069 rhstype);
7071 bool save = in_late_binary_op;
7072 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
7073 || (coder == REAL_TYPE
7074 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
7075 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
7076 in_late_binary_op = true;
7077 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
7078 ? expr_loc : location, type, orig_rhs,
7079 errtype == ic_init_const);
7080 in_late_binary_op = save;
7081 return ret;
7084 /* Aggregates in different TUs might need conversion. */
7085 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
7086 && codel == coder
7087 && comptypes (type, rhstype))
7088 return convert_and_check (expr_loc != UNKNOWN_LOCATION
7089 ? expr_loc : location, type, rhs);
7091 /* Conversion to a transparent union or record from its member types.
7092 This applies only to function arguments. */
7093 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
7094 && TYPE_TRANSPARENT_AGGR (type))
7095 && errtype == ic_argpass)
7097 tree memb, marginal_memb = NULL_TREE;
7099 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
7101 tree memb_type = TREE_TYPE (memb);
7103 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
7104 TYPE_MAIN_VARIANT (rhstype)))
7105 break;
7107 if (TREE_CODE (memb_type) != POINTER_TYPE)
7108 continue;
7110 if (coder == POINTER_TYPE)
7112 tree ttl = TREE_TYPE (memb_type);
7113 tree ttr = TREE_TYPE (rhstype);
7115 /* Any non-function converts to a [const][volatile] void *
7116 and vice versa; otherwise, targets must be the same.
7117 Meanwhile, the lhs target must have all the qualifiers of
7118 the rhs. */
7119 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7120 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7121 || comp_target_types (location, memb_type, rhstype))
7123 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
7124 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
7125 /* If this type won't generate any warnings, use it. */
7126 if (lquals == rquals
7127 || ((TREE_CODE (ttr) == FUNCTION_TYPE
7128 && TREE_CODE (ttl) == FUNCTION_TYPE)
7129 ? ((lquals | rquals) == rquals)
7130 : ((lquals | rquals) == lquals)))
7131 break;
7133 /* Keep looking for a better type, but remember this one. */
7134 if (!marginal_memb)
7135 marginal_memb = memb;
7139 /* Can convert integer zero to any pointer type. */
7140 if (null_pointer_constant)
7142 rhs = null_pointer_node;
7143 break;
7147 if (memb || marginal_memb)
7149 if (!memb)
7151 /* We have only a marginally acceptable member type;
7152 it needs a warning. */
7153 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
7154 tree ttr = TREE_TYPE (rhstype);
7156 /* Const and volatile mean something different for function
7157 types, so the usual warnings are not appropriate. */
7158 if (TREE_CODE (ttr) == FUNCTION_TYPE
7159 && TREE_CODE (ttl) == FUNCTION_TYPE)
7161 /* Because const and volatile on functions are
7162 restrictions that say the function will not do
7163 certain things, it is okay to use a const or volatile
7164 function where an ordinary one is wanted, but not
7165 vice-versa. */
7166 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7167 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7168 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7169 OPT_Wdiscarded_qualifiers,
7170 G_("passing argument %d of %qE "
7171 "makes %q#v qualified function "
7172 "pointer from unqualified"),
7173 G_("assignment makes %q#v qualified "
7174 "function pointer from "
7175 "unqualified"),
7176 G_("initialization makes %q#v qualified "
7177 "function pointer from "
7178 "unqualified"),
7179 G_("return makes %q#v qualified function "
7180 "pointer from unqualified"),
7181 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7183 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7184 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7185 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7186 OPT_Wdiscarded_qualifiers,
7187 G_("passing argument %d of %qE discards "
7188 "%qv qualifier from pointer target type"),
7189 G_("assignment discards %qv qualifier "
7190 "from pointer target type"),
7191 G_("initialization discards %qv qualifier "
7192 "from pointer target type"),
7193 G_("return discards %qv qualifier from "
7194 "pointer target type"),
7195 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7197 memb = marginal_memb;
7200 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7201 pedwarn (location, OPT_Wpedantic,
7202 "ISO C prohibits argument conversion to union type");
7204 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7205 return build_constructor_single (type, memb, rhs);
7209 /* Conversions among pointers */
7210 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7211 && (coder == codel))
7213 /* If RHS refers to a built-in declared without a prototype
7214 BLTIN is the declaration of the built-in with a prototype
7215 and RHSTYPE is set to the actual type of the built-in. */
7216 tree bltin;
7217 rhstype = type_or_builtin_type (rhs, &bltin);
7219 tree ttl = TREE_TYPE (type);
7220 tree ttr = TREE_TYPE (rhstype);
7221 tree mvl = ttl;
7222 tree mvr = ttr;
7223 bool is_opaque_pointer;
7224 int target_cmp = 0; /* Cache comp_target_types () result. */
7225 addr_space_t asl;
7226 addr_space_t asr;
7228 if (TREE_CODE (mvl) != ARRAY_TYPE)
7229 mvl = (TYPE_ATOMIC (mvl)
7230 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7231 TYPE_QUAL_ATOMIC)
7232 : TYPE_MAIN_VARIANT (mvl));
7233 if (TREE_CODE (mvr) != ARRAY_TYPE)
7234 mvr = (TYPE_ATOMIC (mvr)
7235 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7236 TYPE_QUAL_ATOMIC)
7237 : TYPE_MAIN_VARIANT (mvr));
7238 /* Opaque pointers are treated like void pointers. */
7239 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7241 /* The Plan 9 compiler permits a pointer to a struct to be
7242 automatically converted into a pointer to an anonymous field
7243 within the struct. */
7244 if (flag_plan9_extensions
7245 && RECORD_OR_UNION_TYPE_P (mvl)
7246 && RECORD_OR_UNION_TYPE_P (mvr)
7247 && mvl != mvr)
7249 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7250 if (new_rhs != NULL_TREE)
7252 rhs = new_rhs;
7253 rhstype = TREE_TYPE (rhs);
7254 coder = TREE_CODE (rhstype);
7255 ttr = TREE_TYPE (rhstype);
7256 mvr = TYPE_MAIN_VARIANT (ttr);
7260 /* C++ does not allow the implicit conversion void* -> T*. However,
7261 for the purpose of reducing the number of false positives, we
7262 tolerate the special case of
7264 int *p = NULL;
7266 where NULL is typically defined in C to be '(void *) 0'. */
7267 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7268 warning_at (errtype == ic_argpass ? expr_loc : location,
7269 OPT_Wc___compat,
7270 "request for implicit conversion "
7271 "from %qT to %qT not permitted in C++", rhstype, type);
7273 /* See if the pointers point to incompatible address spaces. */
7274 asl = TYPE_ADDR_SPACE (ttl);
7275 asr = TYPE_ADDR_SPACE (ttr);
7276 if (!null_pointer_constant_p (rhs)
7277 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7279 auto_diagnostic_group d;
7280 bool diagnosed = true;
7281 switch (errtype)
7283 case ic_argpass:
7285 const char msg[] = G_("passing argument %d of %qE from "
7286 "pointer to non-enclosed address space");
7287 if (warnopt)
7288 diagnosed
7289 = warning_at (expr_loc, warnopt, msg, parmnum, rname);
7290 else
7291 error_at (expr_loc, msg, parmnum, rname);
7292 break;
7294 case ic_assign:
7296 const char msg[] = G_("assignment from pointer to "
7297 "non-enclosed address space");
7298 if (warnopt)
7299 diagnosed = warning_at (location, warnopt, msg);
7300 else
7301 error_at (location, msg);
7302 break;
7304 case ic_init:
7305 case ic_init_const:
7307 const char msg[] = G_("initialization from pointer to "
7308 "non-enclosed address space");
7309 if (warnopt)
7310 diagnosed = warning_at (location, warnopt, msg);
7311 else
7312 error_at (location, msg);
7313 break;
7315 case ic_return:
7317 const char msg[] = G_("return from pointer to "
7318 "non-enclosed address space");
7319 if (warnopt)
7320 diagnosed = warning_at (location, warnopt, msg);
7321 else
7322 error_at (location, msg);
7323 break;
7325 default:
7326 gcc_unreachable ();
7328 if (diagnosed)
7330 if (errtype == ic_argpass)
7331 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7332 else
7333 inform (location, "expected %qT but pointer is of type %qT",
7334 type, rhstype);
7336 return error_mark_node;
7339 /* Check if the right-hand side has a format attribute but the
7340 left-hand side doesn't. */
7341 if (warn_suggest_attribute_format
7342 && check_missing_format_attribute (type, rhstype))
7344 switch (errtype)
7346 case ic_argpass:
7347 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7348 "argument %d of %qE might be "
7349 "a candidate for a format attribute",
7350 parmnum, rname);
7351 break;
7352 case ic_assign:
7353 warning_at (location, OPT_Wsuggest_attribute_format,
7354 "assignment left-hand side might be "
7355 "a candidate for a format attribute");
7356 break;
7357 case ic_init:
7358 case ic_init_const:
7359 warning_at (location, OPT_Wsuggest_attribute_format,
7360 "initialization left-hand side might be "
7361 "a candidate for a format attribute");
7362 break;
7363 case ic_return:
7364 warning_at (location, OPT_Wsuggest_attribute_format,
7365 "return type might be "
7366 "a candidate for a format attribute");
7367 break;
7368 default:
7369 gcc_unreachable ();
7373 /* See if the pointers point to incompatible scalar storage orders. */
7374 if (warn_scalar_storage_order
7375 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7376 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7378 tree t;
7380 switch (errtype)
7382 case ic_argpass:
7383 /* Do not warn for built-in functions, for example memcpy, since we
7384 control how they behave and they can be useful in this area. */
7385 if (TREE_CODE (rname) != FUNCTION_DECL
7386 || !fndecl_built_in_p (rname))
7387 warning_at (location, OPT_Wscalar_storage_order,
7388 "passing argument %d of %qE from incompatible "
7389 "scalar storage order", parmnum, rname);
7390 break;
7391 case ic_assign:
7392 /* Do not warn if the RHS is a call to a function that returns a
7393 pointer that is not an alias. */
7394 if (TREE_CODE (rhs) != CALL_EXPR
7395 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7396 || !DECL_IS_MALLOC (t))
7397 warning_at (location, OPT_Wscalar_storage_order,
7398 "assignment to %qT from pointer type %qT with "
7399 "incompatible scalar storage order", type, rhstype);
7400 break;
7401 case ic_init:
7402 case ic_init_const:
7403 /* Likewise. */
7404 if (TREE_CODE (rhs) != CALL_EXPR
7405 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7406 || !DECL_IS_MALLOC (t))
7407 warning_at (location, OPT_Wscalar_storage_order,
7408 "initialization of %qT from pointer type %qT with "
7409 "incompatible scalar storage order", type, rhstype);
7410 break;
7411 case ic_return:
7412 warning_at (location, OPT_Wscalar_storage_order,
7413 "returning %qT from pointer type with incompatible "
7414 "scalar storage order %qT", rhstype, type);
7415 break;
7416 default:
7417 gcc_unreachable ();
7421 /* Any non-function converts to a [const][volatile] void *
7422 and vice versa; otherwise, targets must be the same.
7423 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7424 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7425 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7426 || (target_cmp = comp_target_types (location, type, rhstype))
7427 || is_opaque_pointer
7428 || ((c_common_unsigned_type (mvl)
7429 == c_common_unsigned_type (mvr))
7430 && (c_common_signed_type (mvl)
7431 == c_common_signed_type (mvr))
7432 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7434 /* Warn about loss of qualifers from pointers to arrays with
7435 qualifiers on the element type. */
7436 if (TREE_CODE (ttr) == ARRAY_TYPE)
7438 ttr = strip_array_types (ttr);
7439 ttl = strip_array_types (ttl);
7441 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7442 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7443 WARNING_FOR_QUALIFIERS (flag_isoc2x,
7444 location, expr_loc,
7445 OPT_Wdiscarded_array_qualifiers,
7446 G_("passing argument %d of %qE discards "
7447 "%qv qualifier from pointer target type"),
7448 G_("assignment discards %qv qualifier "
7449 "from pointer target type"),
7450 G_("initialization discards %qv qualifier "
7451 "from pointer target type"),
7452 G_("return discards %qv qualifier from "
7453 "pointer target type"),
7454 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7456 else if (pedantic
7457 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7459 (VOID_TYPE_P (ttr)
7460 && !null_pointer_constant
7461 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7462 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7463 G_("ISO C forbids passing argument %d of "
7464 "%qE between function pointer "
7465 "and %<void *%>"),
7466 G_("ISO C forbids assignment between "
7467 "function pointer and %<void *%>"),
7468 G_("ISO C forbids initialization between "
7469 "function pointer and %<void *%>"),
7470 G_("ISO C forbids return between function "
7471 "pointer and %<void *%>"));
7472 /* Const and volatile mean something different for function types,
7473 so the usual warnings are not appropriate. */
7474 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7475 && TREE_CODE (ttl) != FUNCTION_TYPE)
7477 /* Assignments between atomic and non-atomic objects are OK. */
7478 bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7479 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
7480 bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7481 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
7483 /* Don't warn about loss of qualifier for conversions from
7484 qualified void* to pointers to arrays with corresponding
7485 qualifier on the element type (except for pedantic before C23). */
7486 if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc2x))
7487 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7488 OPT_Wdiscarded_qualifiers,
7489 G_("passing argument %d of %qE discards "
7490 "%qv qualifier from pointer target type"),
7491 G_("assignment discards %qv qualifier "
7492 "from pointer target type"),
7493 G_("initialization discards %qv qualifier "
7494 "from pointer target type"),
7495 G_("return discards %qv qualifier from "
7496 "pointer target type"),
7497 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7498 else if (warn_quals_ped)
7499 pedwarn_c11 (location, OPT_Wc11_c2x_compat,
7500 "array with qualifier on the element is not qualified before C2X");
7502 /* If this is not a case of ignoring a mismatch in signedness,
7503 no warning. */
7504 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7505 || target_cmp)
7507 /* If there is a mismatch, do warn. */
7508 else if (warn_pointer_sign)
7509 switch (errtype)
7511 case ic_argpass:
7513 auto_diagnostic_group d;
7514 range_label_for_type_mismatch rhs_label (rhstype, type);
7515 gcc_rich_location richloc (expr_loc, &rhs_label);
7516 if (pedwarn (&richloc, OPT_Wpointer_sign,
7517 "pointer targets in passing argument %d of "
7518 "%qE differ in signedness", parmnum, rname))
7519 inform_for_arg (fundecl, expr_loc, parmnum, type,
7520 rhstype);
7522 break;
7523 case ic_assign:
7524 pedwarn (location, OPT_Wpointer_sign,
7525 "pointer targets in assignment from %qT to %qT "
7526 "differ in signedness", rhstype, type);
7527 break;
7528 case ic_init:
7529 case ic_init_const:
7530 pedwarn_init (location, OPT_Wpointer_sign,
7531 "pointer targets in initialization of %qT "
7532 "from %qT differ in signedness", type,
7533 rhstype);
7534 break;
7535 case ic_return:
7536 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7537 "returning %qT from a function with return type "
7538 "%qT differ in signedness", rhstype, type);
7539 break;
7540 default:
7541 gcc_unreachable ();
7544 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7545 && TREE_CODE (ttr) == FUNCTION_TYPE)
7547 /* Because const and volatile on functions are restrictions
7548 that say the function will not do certain things,
7549 it is okay to use a const or volatile function
7550 where an ordinary one is wanted, but not vice-versa. */
7551 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7552 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7553 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7554 OPT_Wdiscarded_qualifiers,
7555 G_("passing argument %d of %qE makes "
7556 "%q#v qualified function pointer "
7557 "from unqualified"),
7558 G_("assignment makes %q#v qualified function "
7559 "pointer from unqualified"),
7560 G_("initialization makes %q#v qualified "
7561 "function pointer from unqualified"),
7562 G_("return makes %q#v qualified function "
7563 "pointer from unqualified"),
7564 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7567 /* Avoid warning about the volatile ObjC EH puts on decls. */
7568 else if (!objc_ok)
7570 switch (errtype)
7572 case ic_argpass:
7574 auto_diagnostic_group d;
7575 range_label_for_type_mismatch rhs_label (rhstype, type);
7576 gcc_rich_location richloc (expr_loc, &rhs_label);
7577 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7578 "passing argument %d of %qE from incompatible "
7579 "pointer type", parmnum, rname))
7580 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7582 break;
7583 case ic_assign:
7584 if (bltin)
7585 pedwarn (location, OPT_Wincompatible_pointer_types,
7586 "assignment to %qT from pointer to "
7587 "%qD with incompatible type %qT",
7588 type, bltin, rhstype);
7589 else
7590 pedwarn (location, OPT_Wincompatible_pointer_types,
7591 "assignment to %qT from incompatible pointer type %qT",
7592 type, rhstype);
7593 break;
7594 case ic_init:
7595 case ic_init_const:
7596 if (bltin)
7597 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7598 "initialization of %qT from pointer to "
7599 "%qD with incompatible type %qT",
7600 type, bltin, rhstype);
7601 else
7602 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7603 "initialization of %qT from incompatible "
7604 "pointer type %qT",
7605 type, rhstype);
7606 break;
7607 case ic_return:
7608 if (bltin)
7609 pedwarn (location, OPT_Wincompatible_pointer_types,
7610 "returning pointer to %qD of type %qT from "
7611 "a function with incompatible type %qT",
7612 bltin, rhstype, type);
7613 else
7614 pedwarn (location, OPT_Wincompatible_pointer_types,
7615 "returning %qT from a function with incompatible "
7616 "return type %qT", rhstype, type);
7617 break;
7618 default:
7619 gcc_unreachable ();
7623 /* If RHS isn't an address, check pointer or array of packed
7624 struct or union. */
7625 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7627 return convert (type, rhs);
7629 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7631 /* ??? This should not be an error when inlining calls to
7632 unprototyped functions. */
7633 const char msg[] = "invalid use of non-lvalue array";
7634 if (warnopt)
7635 warning_at (location, warnopt, msg);
7636 else
7637 error_at (location, msg);
7638 return error_mark_node;
7640 else if (codel == POINTER_TYPE
7641 && (coder == INTEGER_TYPE || coder == NULLPTR_TYPE))
7643 /* An explicit constant 0 or type nullptr_t can convert to a pointer,
7644 or one that results from arithmetic, even including a cast to
7645 integer type. */
7646 if (!null_pointer_constant && coder != NULLPTR_TYPE)
7647 switch (errtype)
7649 case ic_argpass:
7651 auto_diagnostic_group d;
7652 range_label_for_type_mismatch rhs_label (rhstype, type);
7653 gcc_rich_location richloc (expr_loc, &rhs_label);
7654 if (pedwarn (&richloc, OPT_Wint_conversion,
7655 "passing argument %d of %qE makes pointer from "
7656 "integer without a cast", parmnum, rname))
7657 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7659 break;
7660 case ic_assign:
7661 pedwarn (location, OPT_Wint_conversion,
7662 "assignment to %qT from %qT makes pointer from integer "
7663 "without a cast", type, rhstype);
7664 break;
7665 case ic_init:
7666 case ic_init_const:
7667 pedwarn_init (location, OPT_Wint_conversion,
7668 "initialization of %qT from %qT makes pointer from "
7669 "integer without a cast", type, rhstype);
7670 break;
7671 case ic_return:
7672 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7673 "function with return type %qT makes pointer from "
7674 "integer without a cast", rhstype, type);
7675 break;
7676 default:
7677 gcc_unreachable ();
7680 return convert (type, rhs);
7682 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7684 switch (errtype)
7686 case ic_argpass:
7688 auto_diagnostic_group d;
7689 range_label_for_type_mismatch rhs_label (rhstype, type);
7690 gcc_rich_location richloc (expr_loc, &rhs_label);
7691 if (pedwarn (&richloc, OPT_Wint_conversion,
7692 "passing argument %d of %qE makes integer from "
7693 "pointer without a cast", parmnum, rname))
7694 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7696 break;
7697 case ic_assign:
7698 pedwarn (location, OPT_Wint_conversion,
7699 "assignment to %qT from %qT makes integer from pointer "
7700 "without a cast", type, rhstype);
7701 break;
7702 case ic_init:
7703 case ic_init_const:
7704 pedwarn_init (location, OPT_Wint_conversion,
7705 "initialization of %qT from %qT makes integer from "
7706 "pointer without a cast", type, rhstype);
7707 break;
7708 case ic_return:
7709 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7710 "function with return type %qT makes integer from "
7711 "pointer without a cast", rhstype, type);
7712 break;
7713 default:
7714 gcc_unreachable ();
7717 return convert (type, rhs);
7719 else if (codel == BOOLEAN_TYPE
7720 /* The type nullptr_t may be converted to bool. The
7721 result is false. */
7722 && (coder == POINTER_TYPE || coder == NULLPTR_TYPE))
7724 tree ret;
7725 bool save = in_late_binary_op;
7726 in_late_binary_op = true;
7727 ret = convert (type, rhs);
7728 in_late_binary_op = save;
7729 return ret;
7732 switch (errtype)
7734 case ic_argpass:
7736 auto_diagnostic_group d;
7737 range_label_for_type_mismatch rhs_label (rhstype, type);
7738 gcc_rich_location richloc (expr_loc, &rhs_label);
7739 const char msg[] = G_("incompatible type for argument %d of %qE");
7740 if (warnopt)
7741 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7742 else
7743 error_at (&richloc, msg, parmnum, rname);
7744 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7746 break;
7747 case ic_assign:
7749 const char msg[]
7750 = G_("incompatible types when assigning to type %qT from type %qT");
7751 if (warnopt)
7752 warning_at (expr_loc, 0, msg, type, rhstype);
7753 else
7754 error_at (expr_loc, msg, type, rhstype);
7755 break;
7757 case ic_init:
7758 case ic_init_const:
7760 const char msg[]
7761 = G_("incompatible types when initializing type %qT using type %qT");
7762 if (warnopt)
7763 warning_at (location, 0, msg, type, rhstype);
7764 else
7765 error_at (location, msg, type, rhstype);
7766 break;
7768 case ic_return:
7770 const char msg[]
7771 = G_("incompatible types when returning type %qT but %qT was expected");
7772 if (warnopt)
7773 warning_at (location, 0, msg, rhstype, type);
7774 else
7775 error_at (location, msg, rhstype, type);
7776 break;
7778 default:
7779 gcc_unreachable ();
7782 return error_mark_node;
7785 /* If VALUE is a compound expr all of whose expressions are constant, then
7786 return its value. Otherwise, return error_mark_node.
7788 This is for handling COMPOUND_EXPRs as initializer elements
7789 which is allowed with a warning when -pedantic is specified. */
7791 static tree
7792 valid_compound_expr_initializer (tree value, tree endtype)
7794 if (TREE_CODE (value) == COMPOUND_EXPR)
7796 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7797 == error_mark_node)
7798 return error_mark_node;
7799 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7800 endtype);
7802 else if (!initializer_constant_valid_p (value, endtype))
7803 return error_mark_node;
7804 else
7805 return value;
7808 /* Perform appropriate conversions on the initial value of a variable,
7809 store it in the declaration DECL,
7810 and print any error messages that are appropriate.
7811 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7812 If the init is invalid, store an ERROR_MARK.
7814 INIT_LOC is the location of the initial value. */
7816 void
7817 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7819 tree value, type;
7820 bool npc = false;
7822 /* If variable's type was invalidly declared, just ignore it. */
7824 type = TREE_TYPE (decl);
7825 if (TREE_CODE (type) == ERROR_MARK)
7826 return;
7828 /* Digest the specified initializer into an expression. */
7830 if (init)
7831 npc = null_pointer_constant_p (init);
7832 value = digest_init (init_loc, type, init, origtype, npc,
7833 true, TREE_STATIC (decl));
7835 /* Store the expression if valid; else report error. */
7837 if (!in_system_header_at (input_location)
7838 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7839 warning (OPT_Wtraditional, "traditional C rejects automatic "
7840 "aggregate initialization");
7842 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7843 DECL_INITIAL (decl) = value;
7845 /* ANSI wants warnings about out-of-range constant initializers. */
7846 STRIP_TYPE_NOPS (value);
7847 if (TREE_STATIC (decl))
7848 constant_expression_warning (value);
7850 /* Check if we need to set array size from compound literal size. */
7851 if (TREE_CODE (type) == ARRAY_TYPE
7852 && TYPE_DOMAIN (type) == NULL_TREE
7853 && value != error_mark_node)
7855 tree inside_init = init;
7857 STRIP_TYPE_NOPS (inside_init);
7858 inside_init = fold (inside_init);
7860 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7862 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7864 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7866 /* For int foo[] = (int [3]){1}; we need to set array size
7867 now since later on array initializer will be just the
7868 brace enclosed list of the compound literal. */
7869 tree etype = strip_array_types (TREE_TYPE (decl));
7870 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7871 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7872 layout_type (type);
7873 layout_decl (cldecl, 0);
7874 TREE_TYPE (decl)
7875 = c_build_qualified_type (type, TYPE_QUALS (etype));
7881 /* Methods for storing and printing names for error messages. */
7883 /* Implement a spelling stack that allows components of a name to be pushed
7884 and popped. Each element on the stack is this structure. */
7886 struct spelling
7888 int kind;
7889 union
7891 unsigned HOST_WIDE_INT i;
7892 const char *s;
7893 } u;
7896 #define SPELLING_STRING 1
7897 #define SPELLING_MEMBER 2
7898 #define SPELLING_BOUNDS 3
7900 static struct spelling *spelling; /* Next stack element (unused). */
7901 static struct spelling *spelling_base; /* Spelling stack base. */
7902 static int spelling_size; /* Size of the spelling stack. */
7904 /* Macros to save and restore the spelling stack around push_... functions.
7905 Alternative to SAVE_SPELLING_STACK. */
7907 #define SPELLING_DEPTH() (spelling - spelling_base)
7908 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7910 /* Push an element on the spelling stack with type KIND and assign VALUE
7911 to MEMBER. */
7913 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7915 int depth = SPELLING_DEPTH (); \
7917 if (depth >= spelling_size) \
7919 spelling_size += 10; \
7920 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7921 spelling_size); \
7922 RESTORE_SPELLING_DEPTH (depth); \
7925 spelling->kind = (KIND); \
7926 spelling->MEMBER = (VALUE); \
7927 spelling++; \
7930 /* Push STRING on the stack. Printed literally. */
7932 static void
7933 push_string (const char *string)
7935 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7938 /* Push a member name on the stack. Printed as '.' STRING. */
7940 static void
7941 push_member_name (tree decl)
7943 const char *const string
7944 = (DECL_NAME (decl)
7945 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7946 : _("<anonymous>"));
7947 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7950 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7952 static void
7953 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7955 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7958 /* Compute the maximum size in bytes of the printed spelling. */
7960 static int
7961 spelling_length (void)
7963 int size = 0;
7964 struct spelling *p;
7966 for (p = spelling_base; p < spelling; p++)
7968 if (p->kind == SPELLING_BOUNDS)
7969 size += 25;
7970 else
7971 size += strlen (p->u.s) + 1;
7974 return size;
7977 /* Print the spelling to BUFFER and return it. */
7979 static char *
7980 print_spelling (char *buffer)
7982 char *d = buffer;
7983 struct spelling *p;
7985 for (p = spelling_base; p < spelling; p++)
7986 if (p->kind == SPELLING_BOUNDS)
7988 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7989 d += strlen (d);
7991 else
7993 const char *s;
7994 if (p->kind == SPELLING_MEMBER)
7995 *d++ = '.';
7996 for (s = p->u.s; (*d = *s++); d++)
7999 *d++ = '\0';
8000 return buffer;
8003 /* Digest the parser output INIT as an initializer for type TYPE.
8004 Return a C expression of type TYPE to represent the initial value.
8006 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
8008 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
8010 If INIT is a string constant, STRICT_STRING is true if it is
8011 unparenthesized or we should not warn here for it being parenthesized.
8012 For other types of INIT, STRICT_STRING is not used.
8014 INIT_LOC is the location of the INIT.
8016 REQUIRE_CONSTANT requests an error if non-constant initializers or
8017 elements are seen. */
8019 static tree
8020 digest_init (location_t init_loc, tree type, tree init, tree origtype,
8021 bool null_pointer_constant, bool strict_string,
8022 int require_constant)
8024 enum tree_code code = TREE_CODE (type);
8025 tree inside_init = init;
8026 tree semantic_type = NULL_TREE;
8027 bool maybe_const = true;
8029 if (type == error_mark_node
8030 || !init
8031 || error_operand_p (init))
8032 return error_mark_node;
8034 STRIP_TYPE_NOPS (inside_init);
8036 if (!c_in_omp_for)
8038 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
8040 semantic_type = TREE_TYPE (inside_init);
8041 inside_init = TREE_OPERAND (inside_init, 0);
8043 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
8046 /* Initialization of an array of chars from a string constant
8047 optionally enclosed in braces. */
8049 if (code == ARRAY_TYPE && inside_init
8050 && TREE_CODE (inside_init) == STRING_CST)
8052 tree typ1
8053 = (TYPE_ATOMIC (TREE_TYPE (type))
8054 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
8055 TYPE_QUAL_ATOMIC)
8056 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
8057 /* Note that an array could be both an array of character type
8058 and an array of wchar_t if wchar_t is signed char or unsigned
8059 char. */
8060 bool char_array = (typ1 == char_type_node
8061 || typ1 == signed_char_type_node
8062 || typ1 == unsigned_char_type_node);
8063 bool wchar_array = !!comptypes (typ1, wchar_type_node);
8064 bool char16_array = !!comptypes (typ1, char16_type_node);
8065 bool char32_array = !!comptypes (typ1, char32_type_node);
8067 if (char_array || wchar_array || char16_array || char32_array)
8069 struct c_expr expr;
8070 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
8071 bool incompat_string_cst = false;
8072 expr.value = inside_init;
8073 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
8074 expr.original_type = NULL;
8075 maybe_warn_string_init (init_loc, type, expr);
8077 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8078 pedwarn_init (init_loc, OPT_Wpedantic,
8079 "initialization of a flexible array member");
8081 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8082 TYPE_MAIN_VARIANT (type)))
8083 return inside_init;
8085 if (char_array)
8087 if (typ2 != char_type_node && typ2 != char8_type_node)
8088 incompat_string_cst = true;
8090 else if (!comptypes (typ1, typ2))
8091 incompat_string_cst = true;
8093 if (incompat_string_cst)
8095 error_init (init_loc, "cannot initialize array of %qT from "
8096 "a string literal with type array of %qT",
8097 typ1, typ2);
8098 return error_mark_node;
8101 if (TYPE_DOMAIN (type) != NULL_TREE
8102 && TYPE_SIZE (type) != NULL_TREE
8103 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8105 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8106 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
8108 /* Subtract the size of a single (possibly wide) character
8109 because it's ok to ignore the terminating null char
8110 that is counted in the length of the constant. */
8111 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
8112 pedwarn_init (init_loc, 0,
8113 ("initializer-string for array of %qT "
8114 "is too long"), typ1);
8115 else if (warn_cxx_compat
8116 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8117 warning_at (init_loc, OPT_Wc___compat,
8118 ("initializer-string for array of %qT "
8119 "is too long for C++"), typ1);
8120 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8122 unsigned HOST_WIDE_INT size
8123 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8124 const char *p = TREE_STRING_POINTER (inside_init);
8126 inside_init = build_string (size, p);
8130 TREE_TYPE (inside_init) = type;
8131 return inside_init;
8133 else if (INTEGRAL_TYPE_P (typ1))
8135 error_init (init_loc, "array of inappropriate type initialized "
8136 "from string constant");
8137 return error_mark_node;
8141 /* Build a VECTOR_CST from a *constant* vector constructor. If the
8142 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
8143 below and handle as a constructor. */
8144 if (code == VECTOR_TYPE
8145 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
8146 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
8147 && TREE_CONSTANT (inside_init))
8149 if (TREE_CODE (inside_init) == VECTOR_CST
8150 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8151 TYPE_MAIN_VARIANT (type)))
8152 return inside_init;
8154 if (TREE_CODE (inside_init) == CONSTRUCTOR)
8156 unsigned HOST_WIDE_INT ix;
8157 tree value;
8158 bool constant_p = true;
8160 /* Iterate through elements and check if all constructor
8161 elements are *_CSTs. */
8162 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
8163 if (!CONSTANT_CLASS_P (value))
8165 constant_p = false;
8166 break;
8169 if (constant_p)
8170 return build_vector_from_ctor (type,
8171 CONSTRUCTOR_ELTS (inside_init));
8175 if (warn_sequence_point)
8176 verify_sequence_points (inside_init);
8178 /* Any type can be initialized
8179 from an expression of the same type, optionally with braces. */
8181 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
8182 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8183 TYPE_MAIN_VARIANT (type))
8184 || (code == ARRAY_TYPE
8185 && comptypes (TREE_TYPE (inside_init), type))
8186 || (gnu_vector_type_p (type)
8187 && comptypes (TREE_TYPE (inside_init), type))
8188 || (code == POINTER_TYPE
8189 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
8190 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
8191 TREE_TYPE (type)))))
8193 if (code == POINTER_TYPE)
8195 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
8197 if (TREE_CODE (inside_init) == STRING_CST
8198 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8199 inside_init = array_to_pointer_conversion
8200 (init_loc, inside_init);
8201 else
8203 error_init (init_loc, "invalid use of non-lvalue array");
8204 return error_mark_node;
8209 if (code == VECTOR_TYPE)
8210 /* Although the types are compatible, we may require a
8211 conversion. */
8212 inside_init = convert (type, inside_init);
8214 if (require_constant
8215 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8217 /* As an extension, allow initializing objects with static storage
8218 duration with compound literals (which are then treated just as
8219 the brace enclosed list they contain). Also allow this for
8220 vectors, as we can only assign them with compound literals. */
8221 if (flag_isoc99 && code != VECTOR_TYPE)
8222 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
8223 "is not constant");
8224 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8225 inside_init = DECL_INITIAL (decl);
8228 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
8229 && TREE_CODE (inside_init) != CONSTRUCTOR)
8231 error_init (init_loc, "array initialized from non-constant array "
8232 "expression");
8233 return error_mark_node;
8236 /* Compound expressions can only occur here if -Wpedantic or
8237 -pedantic-errors is specified. In the later case, we always want
8238 an error. In the former case, we simply want a warning. */
8239 if (require_constant && pedantic
8240 && TREE_CODE (inside_init) == COMPOUND_EXPR)
8242 inside_init
8243 = valid_compound_expr_initializer (inside_init,
8244 TREE_TYPE (inside_init));
8245 if (inside_init == error_mark_node)
8246 error_init (init_loc, "initializer element is not constant");
8247 else
8248 pedwarn_init (init_loc, OPT_Wpedantic,
8249 "initializer element is not constant");
8250 if (flag_pedantic_errors)
8251 inside_init = error_mark_node;
8253 else if (require_constant
8254 && !initializer_constant_valid_p (inside_init,
8255 TREE_TYPE (inside_init)))
8257 error_init (init_loc, "initializer element is not constant");
8258 inside_init = error_mark_node;
8260 else if (require_constant && !maybe_const)
8261 pedwarn_init (init_loc, OPT_Wpedantic,
8262 "initializer element is not a constant expression");
8264 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8265 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8266 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8267 type, inside_init, origtype,
8268 (require_constant
8269 ? ic_init_const
8270 : ic_init), null_pointer_constant,
8271 NULL_TREE, NULL_TREE, 0);
8272 return inside_init;
8275 /* Handle scalar types, including conversions. */
8277 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8278 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8279 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
8281 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8282 && (TREE_CODE (init) == STRING_CST
8283 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8284 inside_init = init = array_to_pointer_conversion (init_loc, init);
8285 if (semantic_type)
8286 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8287 inside_init);
8288 inside_init
8289 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8290 inside_init, origtype,
8291 require_constant ? ic_init_const : ic_init,
8292 null_pointer_constant, NULL_TREE, NULL_TREE,
8295 /* Check to see if we have already given an error message. */
8296 if (inside_init == error_mark_node)
8298 else if (require_constant && !TREE_CONSTANT (inside_init))
8300 error_init (init_loc, "initializer element is not constant");
8301 inside_init = error_mark_node;
8303 else if (require_constant
8304 && !initializer_constant_valid_p (inside_init,
8305 TREE_TYPE (inside_init)))
8307 error_init (init_loc, "initializer element is not computable at "
8308 "load time");
8309 inside_init = error_mark_node;
8311 else if (require_constant && !maybe_const)
8312 pedwarn_init (init_loc, OPT_Wpedantic,
8313 "initializer element is not a constant expression");
8315 return inside_init;
8318 /* Come here only for records and arrays. */
8320 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8322 error_init (init_loc,
8323 "variable-sized object may not be initialized except "
8324 "with an empty initializer");
8325 return error_mark_node;
8328 error_init (init_loc, "invalid initializer");
8329 return error_mark_node;
8332 /* Handle initializers that use braces. */
8334 /* Type of object we are accumulating a constructor for.
8335 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8336 static tree constructor_type;
8338 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8339 left to fill. */
8340 static tree constructor_fields;
8342 /* For an ARRAY_TYPE, this is the specified index
8343 at which to store the next element we get. */
8344 static tree constructor_index;
8346 /* For an ARRAY_TYPE, this is the maximum index. */
8347 static tree constructor_max_index;
8349 /* For a RECORD_TYPE, this is the first field not yet written out. */
8350 static tree constructor_unfilled_fields;
8352 /* For an ARRAY_TYPE, this is the index of the first element
8353 not yet written out. */
8354 static tree constructor_unfilled_index;
8356 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8357 This is so we can generate gaps between fields, when appropriate. */
8358 static tree constructor_bit_index;
8360 /* If we are saving up the elements rather than allocating them,
8361 this is the list of elements so far (in reverse order,
8362 most recent first). */
8363 static vec<constructor_elt, va_gc> *constructor_elements;
8365 /* 1 if constructor should be incrementally stored into a constructor chain,
8366 0 if all the elements should be kept in AVL tree. */
8367 static int constructor_incremental;
8369 /* 1 if so far this constructor's elements are all compile-time constants. */
8370 static int constructor_constant;
8372 /* 1 if so far this constructor's elements are all valid address constants. */
8373 static int constructor_simple;
8375 /* 1 if this constructor has an element that cannot be part of a
8376 constant expression. */
8377 static int constructor_nonconst;
8379 /* 1 if this constructor is erroneous so far. */
8380 static int constructor_erroneous;
8382 /* 1 if this constructor is the universal zero initializer { 0 }. */
8383 static int constructor_zeroinit;
8385 /* Structure for managing pending initializer elements, organized as an
8386 AVL tree. */
8388 struct init_node
8390 struct init_node *left, *right;
8391 struct init_node *parent;
8392 int balance;
8393 tree purpose;
8394 tree value;
8395 tree origtype;
8398 /* Tree of pending elements at this constructor level.
8399 These are elements encountered out of order
8400 which belong at places we haven't reached yet in actually
8401 writing the output.
8402 Will never hold tree nodes across GC runs. */
8403 static struct init_node *constructor_pending_elts;
8405 /* The SPELLING_DEPTH of this constructor. */
8406 static int constructor_depth;
8408 /* DECL node for which an initializer is being read.
8409 0 means we are reading a constructor expression
8410 such as (struct foo) {...}. */
8411 static tree constructor_decl;
8413 /* Nonzero if this is an initializer for a top-level decl. */
8414 static int constructor_top_level;
8416 /* Nonzero if there were any member designators in this initializer. */
8417 static int constructor_designated;
8419 /* Nesting depth of designator list. */
8420 static int designator_depth;
8422 /* Nonzero if there were diagnosed errors in this designator list. */
8423 static int designator_erroneous;
8426 /* This stack has a level for each implicit or explicit level of
8427 structuring in the initializer, including the outermost one. It
8428 saves the values of most of the variables above. */
8430 struct constructor_range_stack;
8432 struct constructor_stack
8434 struct constructor_stack *next;
8435 tree type;
8436 tree fields;
8437 tree index;
8438 tree max_index;
8439 tree unfilled_index;
8440 tree unfilled_fields;
8441 tree bit_index;
8442 vec<constructor_elt, va_gc> *elements;
8443 struct init_node *pending_elts;
8444 int offset;
8445 int depth;
8446 /* If value nonzero, this value should replace the entire
8447 constructor at this level. */
8448 struct c_expr replacement_value;
8449 struct constructor_range_stack *range_stack;
8450 char constant;
8451 char simple;
8452 char nonconst;
8453 char implicit;
8454 char erroneous;
8455 char outer;
8456 char incremental;
8457 char designated;
8458 int designator_depth;
8461 static struct constructor_stack *constructor_stack;
8463 /* This stack represents designators from some range designator up to
8464 the last designator in the list. */
8466 struct constructor_range_stack
8468 struct constructor_range_stack *next, *prev;
8469 struct constructor_stack *stack;
8470 tree range_start;
8471 tree index;
8472 tree range_end;
8473 tree fields;
8476 static struct constructor_range_stack *constructor_range_stack;
8478 /* This stack records separate initializers that are nested.
8479 Nested initializers can't happen in ANSI C, but GNU C allows them
8480 in cases like { ... (struct foo) { ... } ... }. */
8482 struct initializer_stack
8484 struct initializer_stack *next;
8485 tree decl;
8486 struct constructor_stack *constructor_stack;
8487 struct constructor_range_stack *constructor_range_stack;
8488 vec<constructor_elt, va_gc> *elements;
8489 struct spelling *spelling;
8490 struct spelling *spelling_base;
8491 int spelling_size;
8492 char top_level;
8493 char require_constant_value;
8494 char require_constant_elements;
8495 char designated;
8496 rich_location *missing_brace_richloc;
8499 static struct initializer_stack *initializer_stack;
8501 /* Prepare to parse and output the initializer for variable DECL. */
8503 void
8504 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8505 rich_location *richloc)
8507 const char *locus;
8508 struct initializer_stack *p = XNEW (struct initializer_stack);
8510 p->decl = constructor_decl;
8511 p->require_constant_value = require_constant_value;
8512 p->require_constant_elements = require_constant_elements;
8513 p->constructor_stack = constructor_stack;
8514 p->constructor_range_stack = constructor_range_stack;
8515 p->elements = constructor_elements;
8516 p->spelling = spelling;
8517 p->spelling_base = spelling_base;
8518 p->spelling_size = spelling_size;
8519 p->top_level = constructor_top_level;
8520 p->next = initializer_stack;
8521 p->missing_brace_richloc = richloc;
8522 p->designated = constructor_designated;
8523 initializer_stack = p;
8525 constructor_decl = decl;
8526 constructor_designated = 0;
8527 constructor_top_level = top_level;
8529 if (decl != NULL_TREE && decl != error_mark_node)
8531 require_constant_value = TREE_STATIC (decl);
8532 require_constant_elements
8533 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8534 /* For a scalar, you can always use any value to initialize,
8535 even within braces. */
8536 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8537 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8539 else
8541 require_constant_value = 0;
8542 require_constant_elements = 0;
8543 locus = _("(anonymous)");
8546 constructor_stack = 0;
8547 constructor_range_stack = 0;
8549 found_missing_braces = 0;
8551 spelling_base = 0;
8552 spelling_size = 0;
8553 RESTORE_SPELLING_DEPTH (0);
8555 if (locus)
8556 push_string (locus);
8559 void
8560 finish_init (void)
8562 struct initializer_stack *p = initializer_stack;
8564 /* Free the whole constructor stack of this initializer. */
8565 while (constructor_stack)
8567 struct constructor_stack *q = constructor_stack;
8568 constructor_stack = q->next;
8569 XDELETE (q);
8572 gcc_assert (!constructor_range_stack);
8574 /* Pop back to the data of the outer initializer (if any). */
8575 XDELETE (spelling_base);
8577 constructor_decl = p->decl;
8578 require_constant_value = p->require_constant_value;
8579 require_constant_elements = p->require_constant_elements;
8580 constructor_stack = p->constructor_stack;
8581 constructor_designated = p->designated;
8582 constructor_range_stack = p->constructor_range_stack;
8583 constructor_elements = p->elements;
8584 spelling = p->spelling;
8585 spelling_base = p->spelling_base;
8586 spelling_size = p->spelling_size;
8587 constructor_top_level = p->top_level;
8588 initializer_stack = p->next;
8589 XDELETE (p);
8592 /* Call here when we see the initializer is surrounded by braces.
8593 This is instead of a call to push_init_level;
8594 it is matched by a call to pop_init_level.
8596 TYPE is the type to initialize, for a constructor expression.
8597 For an initializer for a decl, TYPE is zero. */
8599 void
8600 really_start_incremental_init (tree type)
8602 struct constructor_stack *p = XNEW (struct constructor_stack);
8604 if (type == NULL_TREE)
8605 type = TREE_TYPE (constructor_decl);
8607 if (VECTOR_TYPE_P (type)
8608 && TYPE_VECTOR_OPAQUE (type))
8609 error ("opaque vector types cannot be initialized");
8611 p->type = constructor_type;
8612 p->fields = constructor_fields;
8613 p->index = constructor_index;
8614 p->max_index = constructor_max_index;
8615 p->unfilled_index = constructor_unfilled_index;
8616 p->unfilled_fields = constructor_unfilled_fields;
8617 p->bit_index = constructor_bit_index;
8618 p->elements = constructor_elements;
8619 p->constant = constructor_constant;
8620 p->simple = constructor_simple;
8621 p->nonconst = constructor_nonconst;
8622 p->erroneous = constructor_erroneous;
8623 p->pending_elts = constructor_pending_elts;
8624 p->depth = constructor_depth;
8625 p->replacement_value.value = 0;
8626 p->replacement_value.original_code = ERROR_MARK;
8627 p->replacement_value.original_type = NULL;
8628 p->implicit = 0;
8629 p->range_stack = 0;
8630 p->outer = 0;
8631 p->incremental = constructor_incremental;
8632 p->designated = constructor_designated;
8633 p->designator_depth = designator_depth;
8634 p->next = 0;
8635 constructor_stack = p;
8637 constructor_constant = 1;
8638 constructor_simple = 1;
8639 constructor_nonconst = 0;
8640 constructor_depth = SPELLING_DEPTH ();
8641 constructor_elements = NULL;
8642 constructor_pending_elts = 0;
8643 constructor_type = type;
8644 constructor_incremental = 1;
8645 constructor_designated = 0;
8646 constructor_zeroinit = 1;
8647 designator_depth = 0;
8648 designator_erroneous = 0;
8650 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8652 constructor_fields = TYPE_FIELDS (constructor_type);
8653 /* Skip any nameless bit fields at the beginning. */
8654 while (constructor_fields != NULL_TREE
8655 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8656 constructor_fields = DECL_CHAIN (constructor_fields);
8658 constructor_unfilled_fields = constructor_fields;
8659 constructor_bit_index = bitsize_zero_node;
8661 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8663 if (TYPE_DOMAIN (constructor_type))
8665 constructor_max_index
8666 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8668 /* Detect non-empty initializations of zero-length arrays. */
8669 if (constructor_max_index == NULL_TREE
8670 && TYPE_SIZE (constructor_type))
8671 constructor_max_index = integer_minus_one_node;
8673 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8674 to initialize VLAs with a nonempty initializer will cause a
8675 proper error; avoid tree checking errors as well by setting a
8676 safe value. */
8677 if (constructor_max_index
8678 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8679 constructor_max_index = integer_minus_one_node;
8681 constructor_index
8682 = convert (bitsizetype,
8683 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8685 else
8687 constructor_index = bitsize_zero_node;
8688 constructor_max_index = NULL_TREE;
8691 constructor_unfilled_index = constructor_index;
8693 else if (gnu_vector_type_p (constructor_type))
8695 /* Vectors are like simple fixed-size arrays. */
8696 constructor_max_index =
8697 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8698 constructor_index = bitsize_zero_node;
8699 constructor_unfilled_index = constructor_index;
8701 else
8703 /* Handle the case of int x = {5}; */
8704 constructor_fields = constructor_type;
8705 constructor_unfilled_fields = constructor_type;
8709 extern location_t last_init_list_comma;
8711 /* Called when we see an open brace for a nested initializer. Finish
8712 off any pending levels with implicit braces. */
8713 void
8714 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8716 while (constructor_stack->implicit)
8718 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8719 && constructor_fields == NULL_TREE)
8720 process_init_element (input_location,
8721 pop_init_level (loc, 1, braced_init_obstack,
8722 last_init_list_comma),
8723 true, braced_init_obstack);
8724 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8725 && constructor_max_index
8726 && tree_int_cst_lt (constructor_max_index,
8727 constructor_index))
8728 process_init_element (input_location,
8729 pop_init_level (loc, 1, braced_init_obstack,
8730 last_init_list_comma),
8731 true, braced_init_obstack);
8732 else
8733 break;
8737 /* Push down into a subobject, for initialization.
8738 If this is for an explicit set of braces, IMPLICIT is 0.
8739 If it is because the next element belongs at a lower level,
8740 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8742 void
8743 push_init_level (location_t loc, int implicit,
8744 struct obstack *braced_init_obstack)
8746 struct constructor_stack *p;
8747 tree value = NULL_TREE;
8749 /* Unless this is an explicit brace, we need to preserve previous
8750 content if any. */
8751 if (implicit)
8753 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8754 value = find_init_member (constructor_fields, braced_init_obstack);
8755 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8756 value = find_init_member (constructor_index, braced_init_obstack);
8759 p = XNEW (struct constructor_stack);
8760 p->type = constructor_type;
8761 p->fields = constructor_fields;
8762 p->index = constructor_index;
8763 p->max_index = constructor_max_index;
8764 p->unfilled_index = constructor_unfilled_index;
8765 p->unfilled_fields = constructor_unfilled_fields;
8766 p->bit_index = constructor_bit_index;
8767 p->elements = constructor_elements;
8768 p->constant = constructor_constant;
8769 p->simple = constructor_simple;
8770 p->nonconst = constructor_nonconst;
8771 p->erroneous = constructor_erroneous;
8772 p->pending_elts = constructor_pending_elts;
8773 p->depth = constructor_depth;
8774 p->replacement_value.value = NULL_TREE;
8775 p->replacement_value.original_code = ERROR_MARK;
8776 p->replacement_value.original_type = NULL;
8777 p->implicit = implicit;
8778 p->outer = 0;
8779 p->incremental = constructor_incremental;
8780 p->designated = constructor_designated;
8781 p->designator_depth = designator_depth;
8782 p->next = constructor_stack;
8783 p->range_stack = 0;
8784 constructor_stack = p;
8786 constructor_constant = 1;
8787 constructor_simple = 1;
8788 constructor_nonconst = 0;
8789 constructor_depth = SPELLING_DEPTH ();
8790 constructor_elements = NULL;
8791 constructor_incremental = 1;
8792 /* If the upper initializer is designated, then mark this as
8793 designated too to prevent bogus warnings. */
8794 constructor_designated = p->designated;
8795 constructor_pending_elts = 0;
8796 if (!implicit)
8798 p->range_stack = constructor_range_stack;
8799 constructor_range_stack = 0;
8800 designator_depth = 0;
8801 designator_erroneous = 0;
8804 /* Don't die if an entire brace-pair level is superfluous
8805 in the containing level. */
8806 if (constructor_type == NULL_TREE)
8808 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8810 /* Don't die if there are extra init elts at the end. */
8811 if (constructor_fields == NULL_TREE)
8812 constructor_type = NULL_TREE;
8813 else
8815 constructor_type = TREE_TYPE (constructor_fields);
8816 push_member_name (constructor_fields);
8817 constructor_depth++;
8820 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8822 constructor_type = TREE_TYPE (constructor_type);
8823 push_array_bounds (tree_to_uhwi (constructor_index));
8824 constructor_depth++;
8827 if (constructor_type == NULL_TREE)
8829 error_init (loc, "extra brace group at end of initializer");
8830 constructor_fields = NULL_TREE;
8831 constructor_unfilled_fields = NULL_TREE;
8832 return;
8835 if (value && TREE_CODE (value) == CONSTRUCTOR)
8837 constructor_constant = TREE_CONSTANT (value);
8838 constructor_simple = TREE_STATIC (value);
8839 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8840 constructor_elements = CONSTRUCTOR_ELTS (value);
8841 if (!vec_safe_is_empty (constructor_elements)
8842 && (TREE_CODE (constructor_type) == RECORD_TYPE
8843 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8844 set_nonincremental_init (braced_init_obstack);
8847 if (implicit == 1)
8849 found_missing_braces = 1;
8850 if (initializer_stack->missing_brace_richloc)
8851 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8852 (loc, "{");
8855 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8857 constructor_fields = TYPE_FIELDS (constructor_type);
8858 /* Skip any nameless bit fields at the beginning. */
8859 while (constructor_fields != NULL_TREE
8860 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8861 constructor_fields = DECL_CHAIN (constructor_fields);
8863 constructor_unfilled_fields = constructor_fields;
8864 constructor_bit_index = bitsize_zero_node;
8866 else if (gnu_vector_type_p (constructor_type))
8868 /* Vectors are like simple fixed-size arrays. */
8869 constructor_max_index =
8870 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8871 constructor_index = bitsize_int (0);
8872 constructor_unfilled_index = constructor_index;
8874 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8876 if (TYPE_DOMAIN (constructor_type))
8878 constructor_max_index
8879 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8881 /* Detect non-empty initializations of zero-length arrays. */
8882 if (constructor_max_index == NULL_TREE
8883 && TYPE_SIZE (constructor_type))
8884 constructor_max_index = integer_minus_one_node;
8886 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8887 to initialize VLAs will cause a proper error; avoid tree
8888 checking errors as well by setting a safe value. */
8889 if (constructor_max_index
8890 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8891 constructor_max_index = integer_minus_one_node;
8893 constructor_index
8894 = convert (bitsizetype,
8895 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8897 else
8898 constructor_index = bitsize_zero_node;
8900 constructor_unfilled_index = constructor_index;
8901 if (value && TREE_CODE (value) == STRING_CST)
8903 /* We need to split the char/wchar array into individual
8904 characters, so that we don't have to special case it
8905 everywhere. */
8906 set_nonincremental_init_from_string (value, braced_init_obstack);
8909 else
8911 if (constructor_type != error_mark_node)
8912 warning_init (input_location, 0, "braces around scalar initializer");
8913 constructor_fields = constructor_type;
8914 constructor_unfilled_fields = constructor_type;
8918 /* At the end of an implicit or explicit brace level,
8919 finish up that level of constructor. If a single expression
8920 with redundant braces initialized that level, return the
8921 c_expr structure for that expression. Otherwise, the original_code
8922 element is set to ERROR_MARK.
8923 If we were outputting the elements as they are read, return 0 as the value
8924 from inner levels (process_init_element ignores that),
8925 but return error_mark_node as the value from the outermost level
8926 (that's what we want to put in DECL_INITIAL).
8927 Otherwise, return a CONSTRUCTOR expression as the value. */
8929 struct c_expr
8930 pop_init_level (location_t loc, int implicit,
8931 struct obstack *braced_init_obstack,
8932 location_t insert_before)
8934 struct constructor_stack *p;
8935 struct c_expr ret;
8936 ret.value = NULL_TREE;
8937 ret.original_code = ERROR_MARK;
8938 ret.original_type = NULL;
8940 if (implicit == 0)
8942 /* When we come to an explicit close brace,
8943 pop any inner levels that didn't have explicit braces. */
8944 while (constructor_stack->implicit)
8945 process_init_element (input_location,
8946 pop_init_level (loc, 1, braced_init_obstack,
8947 insert_before),
8948 true, braced_init_obstack);
8949 gcc_assert (!constructor_range_stack);
8951 else
8952 if (initializer_stack->missing_brace_richloc)
8953 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8954 (insert_before, "}");
8956 /* Now output all pending elements. */
8957 constructor_incremental = 1;
8958 output_pending_init_elements (1, braced_init_obstack);
8960 p = constructor_stack;
8962 /* Error for initializing a flexible array member, or a zero-length
8963 array member in an inappropriate context. */
8964 if (constructor_type && constructor_fields
8965 && TREE_CODE (constructor_type) == ARRAY_TYPE
8966 && TYPE_DOMAIN (constructor_type)
8967 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8969 /* Silently discard empty initializations. The parser will
8970 already have pedwarned for empty brackets. */
8971 if (integer_zerop (constructor_unfilled_index))
8972 constructor_type = NULL_TREE;
8973 else
8975 gcc_assert (!TYPE_SIZE (constructor_type));
8977 if (constructor_depth > 2)
8978 error_init (loc, "initialization of flexible array member in a nested context");
8979 else
8980 pedwarn_init (loc, OPT_Wpedantic,
8981 "initialization of a flexible array member");
8983 /* We have already issued an error message for the existence
8984 of a flexible array member not at the end of the structure.
8985 Discard the initializer so that we do not die later. */
8986 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8987 constructor_type = NULL_TREE;
8991 switch (vec_safe_length (constructor_elements))
8993 case 0:
8994 /* Initialization with { } counts as zeroinit. */
8995 constructor_zeroinit = 1;
8996 break;
8997 case 1:
8998 /* This might be zeroinit as well. */
8999 if (integer_zerop ((*constructor_elements)[0].value))
9000 constructor_zeroinit = 1;
9001 break;
9002 default:
9003 /* If the constructor has more than one element, it can't be { 0 }. */
9004 constructor_zeroinit = 0;
9005 break;
9008 /* Warn when some structs are initialized with direct aggregation. */
9009 if (!implicit && found_missing_braces && warn_missing_braces
9010 && !constructor_zeroinit)
9012 gcc_assert (initializer_stack->missing_brace_richloc);
9013 warning_at (initializer_stack->missing_brace_richloc,
9014 OPT_Wmissing_braces,
9015 "missing braces around initializer");
9018 /* Warn when some struct elements are implicitly initialized to zero. */
9019 if (warn_missing_field_initializers
9020 && constructor_type
9021 && TREE_CODE (constructor_type) == RECORD_TYPE
9022 && constructor_unfilled_fields)
9024 /* Do not warn for flexible array members or zero-length arrays. */
9025 while (constructor_unfilled_fields
9026 && (!DECL_SIZE (constructor_unfilled_fields)
9027 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
9028 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
9030 if (constructor_unfilled_fields
9031 /* Do not warn if this level of the initializer uses member
9032 designators; it is likely to be deliberate. */
9033 && !constructor_designated
9034 /* Do not warn about initializing with { 0 } or with { }. */
9035 && !constructor_zeroinit)
9037 if (warning_at (input_location, OPT_Wmissing_field_initializers,
9038 "missing initializer for field %qD of %qT",
9039 constructor_unfilled_fields,
9040 constructor_type))
9041 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
9042 "%qD declared here", constructor_unfilled_fields);
9046 /* Pad out the end of the structure. */
9047 if (p->replacement_value.value)
9048 /* If this closes a superfluous brace pair,
9049 just pass out the element between them. */
9050 ret = p->replacement_value;
9051 else if (constructor_type == NULL_TREE)
9053 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
9054 && TREE_CODE (constructor_type) != ARRAY_TYPE
9055 && !gnu_vector_type_p (constructor_type))
9057 /* A nonincremental scalar initializer--just return
9058 the element, after verifying there is just one.
9059 Empty scalar initializers are supported in C2X. */
9060 if (vec_safe_is_empty (constructor_elements))
9062 if (constructor_erroneous || constructor_type == error_mark_node)
9063 ret.value = error_mark_node;
9064 else
9065 ret.value = build_zero_cst (constructor_type);
9067 else if (vec_safe_length (constructor_elements) != 1)
9069 error_init (loc, "extra elements in scalar initializer");
9070 ret.value = (*constructor_elements)[0].value;
9072 else
9073 ret.value = (*constructor_elements)[0].value;
9075 else
9077 if (constructor_erroneous)
9078 ret.value = error_mark_node;
9079 else
9081 ret.value = build_constructor (constructor_type,
9082 constructor_elements);
9083 if (constructor_constant)
9084 TREE_CONSTANT (ret.value) = 1;
9085 if (constructor_constant && constructor_simple)
9086 TREE_STATIC (ret.value) = 1;
9087 if (constructor_nonconst)
9088 CONSTRUCTOR_NON_CONST (ret.value) = 1;
9092 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
9094 if (constructor_nonconst)
9095 ret.original_code = C_MAYBE_CONST_EXPR;
9096 else if (ret.original_code == C_MAYBE_CONST_EXPR)
9097 ret.original_code = ERROR_MARK;
9100 constructor_type = p->type;
9101 constructor_fields = p->fields;
9102 constructor_index = p->index;
9103 constructor_max_index = p->max_index;
9104 constructor_unfilled_index = p->unfilled_index;
9105 constructor_unfilled_fields = p->unfilled_fields;
9106 constructor_bit_index = p->bit_index;
9107 constructor_elements = p->elements;
9108 constructor_constant = p->constant;
9109 constructor_simple = p->simple;
9110 constructor_nonconst = p->nonconst;
9111 constructor_erroneous = p->erroneous;
9112 constructor_incremental = p->incremental;
9113 constructor_designated = p->designated;
9114 designator_depth = p->designator_depth;
9115 constructor_pending_elts = p->pending_elts;
9116 constructor_depth = p->depth;
9117 if (!p->implicit)
9118 constructor_range_stack = p->range_stack;
9119 RESTORE_SPELLING_DEPTH (constructor_depth);
9121 constructor_stack = p->next;
9122 XDELETE (p);
9124 if (ret.value == NULL_TREE && constructor_stack == 0)
9125 ret.value = error_mark_node;
9126 return ret;
9129 /* Common handling for both array range and field name designators.
9130 ARRAY argument is nonzero for array ranges. Returns false for success. */
9132 static bool
9133 set_designator (location_t loc, bool array,
9134 struct obstack *braced_init_obstack)
9136 tree subtype;
9137 enum tree_code subcode;
9139 /* Don't die if an entire brace-pair level is superfluous
9140 in the containing level, or for an erroneous type. */
9141 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9142 return true;
9144 /* If there were errors in this designator list already, bail out
9145 silently. */
9146 if (designator_erroneous)
9147 return true;
9149 /* Likewise for an initializer for a variable-size type. Those are
9150 diagnosed in the parser, except for empty initializer braces. */
9151 if (COMPLETE_TYPE_P (constructor_type)
9152 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9153 return true;
9155 if (!designator_depth)
9157 gcc_assert (!constructor_range_stack);
9159 /* Designator list starts at the level of closest explicit
9160 braces. */
9161 while (constructor_stack->implicit)
9162 process_init_element (input_location,
9163 pop_init_level (loc, 1, braced_init_obstack,
9164 last_init_list_comma),
9165 true, braced_init_obstack);
9166 constructor_designated = 1;
9167 return false;
9170 switch (TREE_CODE (constructor_type))
9172 case RECORD_TYPE:
9173 case UNION_TYPE:
9174 subtype = TREE_TYPE (constructor_fields);
9175 if (subtype != error_mark_node)
9176 subtype = TYPE_MAIN_VARIANT (subtype);
9177 break;
9178 case ARRAY_TYPE:
9179 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9180 break;
9181 default:
9182 gcc_unreachable ();
9185 subcode = TREE_CODE (subtype);
9186 if (array && subcode != ARRAY_TYPE)
9188 error_init (loc, "array index in non-array initializer");
9189 return true;
9191 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9193 error_init (loc, "field name not in record or union initializer");
9194 return true;
9197 constructor_designated = 1;
9198 finish_implicit_inits (loc, braced_init_obstack);
9199 push_init_level (loc, 2, braced_init_obstack);
9200 return false;
9203 /* If there are range designators in designator list, push a new designator
9204 to constructor_range_stack. RANGE_END is end of such stack range or
9205 NULL_TREE if there is no range designator at this level. */
9207 static void
9208 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9210 struct constructor_range_stack *p;
9212 p = (struct constructor_range_stack *)
9213 obstack_alloc (braced_init_obstack,
9214 sizeof (struct constructor_range_stack));
9215 p->prev = constructor_range_stack;
9216 p->next = 0;
9217 p->fields = constructor_fields;
9218 p->range_start = constructor_index;
9219 p->index = constructor_index;
9220 p->stack = constructor_stack;
9221 p->range_end = range_end;
9222 if (constructor_range_stack)
9223 constructor_range_stack->next = p;
9224 constructor_range_stack = p;
9227 /* Within an array initializer, specify the next index to be initialized.
9228 FIRST is that index. If LAST is nonzero, then initialize a range
9229 of indices, running from FIRST through LAST. */
9231 void
9232 set_init_index (location_t loc, tree first, tree last,
9233 struct obstack *braced_init_obstack)
9235 if (set_designator (loc, true, braced_init_obstack))
9236 return;
9238 designator_erroneous = 1;
9240 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9241 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9243 error_init (loc, "array index in initializer not of integer type");
9244 return;
9247 if (TREE_CODE (first) != INTEGER_CST)
9249 first = c_fully_fold (first, false, NULL);
9250 if (TREE_CODE (first) == INTEGER_CST)
9251 pedwarn_init (loc, OPT_Wpedantic,
9252 "array index in initializer is not "
9253 "an integer constant expression");
9256 if (last && TREE_CODE (last) != INTEGER_CST)
9258 last = c_fully_fold (last, false, NULL);
9259 if (TREE_CODE (last) == INTEGER_CST)
9260 pedwarn_init (loc, OPT_Wpedantic,
9261 "array index in initializer is not "
9262 "an integer constant expression");
9265 if (TREE_CODE (first) != INTEGER_CST)
9266 error_init (loc, "nonconstant array index in initializer");
9267 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9268 error_init (loc, "nonconstant array index in initializer");
9269 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9270 error_init (loc, "array index in non-array initializer");
9271 else if (tree_int_cst_sgn (first) == -1)
9272 error_init (loc, "array index in initializer exceeds array bounds");
9273 else if (constructor_max_index
9274 && tree_int_cst_lt (constructor_max_index, first))
9275 error_init (loc, "array index in initializer exceeds array bounds");
9276 else
9278 constant_expression_warning (first);
9279 if (last)
9280 constant_expression_warning (last);
9281 constructor_index = convert (bitsizetype, first);
9282 if (tree_int_cst_lt (constructor_index, first))
9284 constructor_index = copy_node (constructor_index);
9285 TREE_OVERFLOW (constructor_index) = 1;
9288 if (last)
9290 if (tree_int_cst_equal (first, last))
9291 last = NULL_TREE;
9292 else if (tree_int_cst_lt (last, first))
9294 error_init (loc, "empty index range in initializer");
9295 last = NULL_TREE;
9297 else
9299 last = convert (bitsizetype, last);
9300 if (constructor_max_index != NULL_TREE
9301 && tree_int_cst_lt (constructor_max_index, last))
9303 error_init (loc, "array index range in initializer exceeds "
9304 "array bounds");
9305 last = NULL_TREE;
9310 designator_depth++;
9311 designator_erroneous = 0;
9312 if (constructor_range_stack || last)
9313 push_range_stack (last, braced_init_obstack);
9317 /* Within a struct initializer, specify the next field to be initialized. */
9319 void
9320 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9321 struct obstack *braced_init_obstack)
9323 tree field;
9325 if (set_designator (loc, false, braced_init_obstack))
9326 return;
9328 designator_erroneous = 1;
9330 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9332 error_init (loc, "field name not in record or union initializer");
9333 return;
9336 field = lookup_field (constructor_type, fieldname);
9338 if (field == NULL_TREE)
9340 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9341 if (guessed_id)
9343 gcc_rich_location rich_loc (fieldname_loc);
9344 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9345 error_at (&rich_loc,
9346 "%qT has no member named %qE; did you mean %qE?",
9347 constructor_type, fieldname, guessed_id);
9349 else
9350 error_at (fieldname_loc, "%qT has no member named %qE",
9351 constructor_type, fieldname);
9353 else
9356 constructor_fields = TREE_VALUE (field);
9357 designator_depth++;
9358 designator_erroneous = 0;
9359 if (constructor_range_stack)
9360 push_range_stack (NULL_TREE, braced_init_obstack);
9361 field = TREE_CHAIN (field);
9362 if (field)
9364 if (set_designator (loc, false, braced_init_obstack))
9365 return;
9368 while (field != NULL_TREE);
9371 /* Add a new initializer to the tree of pending initializers. PURPOSE
9372 identifies the initializer, either array index or field in a structure.
9373 VALUE is the value of that index or field. If ORIGTYPE is not
9374 NULL_TREE, it is the original type of VALUE.
9376 IMPLICIT is true if value comes from pop_init_level (1),
9377 the new initializer has been merged with the existing one
9378 and thus no warnings should be emitted about overriding an
9379 existing initializer. */
9381 static void
9382 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9383 bool implicit, struct obstack *braced_init_obstack)
9385 struct init_node *p, **q, *r;
9387 q = &constructor_pending_elts;
9388 p = 0;
9390 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9392 while (*q != 0)
9394 p = *q;
9395 if (tree_int_cst_lt (purpose, p->purpose))
9396 q = &p->left;
9397 else if (tree_int_cst_lt (p->purpose, purpose))
9398 q = &p->right;
9399 else
9401 if (!implicit)
9403 if (TREE_SIDE_EFFECTS (p->value))
9404 warning_init (loc, OPT_Woverride_init_side_effects,
9405 "initialized field with side-effects "
9406 "overwritten");
9407 else if (warn_override_init)
9408 warning_init (loc, OPT_Woverride_init,
9409 "initialized field overwritten");
9411 p->value = value;
9412 p->origtype = origtype;
9413 return;
9417 else
9419 tree bitpos;
9421 bitpos = bit_position (purpose);
9422 while (*q != NULL)
9424 p = *q;
9425 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9426 q = &p->left;
9427 else if (p->purpose != purpose)
9428 q = &p->right;
9429 else
9431 if (!implicit)
9433 if (TREE_SIDE_EFFECTS (p->value))
9434 warning_init (loc, OPT_Woverride_init_side_effects,
9435 "initialized field with side-effects "
9436 "overwritten");
9437 else if (warn_override_init)
9438 warning_init (loc, OPT_Woverride_init,
9439 "initialized field overwritten");
9441 p->value = value;
9442 p->origtype = origtype;
9443 return;
9448 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9449 sizeof (struct init_node));
9450 r->purpose = purpose;
9451 r->value = value;
9452 r->origtype = origtype;
9454 *q = r;
9455 r->parent = p;
9456 r->left = 0;
9457 r->right = 0;
9458 r->balance = 0;
9460 while (p)
9462 struct init_node *s;
9464 if (r == p->left)
9466 if (p->balance == 0)
9467 p->balance = -1;
9468 else if (p->balance < 0)
9470 if (r->balance < 0)
9472 /* L rotation. */
9473 p->left = r->right;
9474 if (p->left)
9475 p->left->parent = p;
9476 r->right = p;
9478 p->balance = 0;
9479 r->balance = 0;
9481 s = p->parent;
9482 p->parent = r;
9483 r->parent = s;
9484 if (s)
9486 if (s->left == p)
9487 s->left = r;
9488 else
9489 s->right = r;
9491 else
9492 constructor_pending_elts = r;
9494 else
9496 /* LR rotation. */
9497 struct init_node *t = r->right;
9499 r->right = t->left;
9500 if (r->right)
9501 r->right->parent = r;
9502 t->left = r;
9504 p->left = t->right;
9505 if (p->left)
9506 p->left->parent = p;
9507 t->right = p;
9509 p->balance = t->balance < 0;
9510 r->balance = -(t->balance > 0);
9511 t->balance = 0;
9513 s = p->parent;
9514 p->parent = t;
9515 r->parent = t;
9516 t->parent = s;
9517 if (s)
9519 if (s->left == p)
9520 s->left = t;
9521 else
9522 s->right = t;
9524 else
9525 constructor_pending_elts = t;
9527 break;
9529 else
9531 /* p->balance == +1; growth of left side balances the node. */
9532 p->balance = 0;
9533 break;
9536 else /* r == p->right */
9538 if (p->balance == 0)
9539 /* Growth propagation from right side. */
9540 p->balance++;
9541 else if (p->balance > 0)
9543 if (r->balance > 0)
9545 /* R rotation. */
9546 p->right = r->left;
9547 if (p->right)
9548 p->right->parent = p;
9549 r->left = p;
9551 p->balance = 0;
9552 r->balance = 0;
9554 s = p->parent;
9555 p->parent = r;
9556 r->parent = s;
9557 if (s)
9559 if (s->left == p)
9560 s->left = r;
9561 else
9562 s->right = r;
9564 else
9565 constructor_pending_elts = r;
9567 else /* r->balance == -1 */
9569 /* RL rotation */
9570 struct init_node *t = r->left;
9572 r->left = t->right;
9573 if (r->left)
9574 r->left->parent = r;
9575 t->right = r;
9577 p->right = t->left;
9578 if (p->right)
9579 p->right->parent = p;
9580 t->left = p;
9582 r->balance = (t->balance < 0);
9583 p->balance = -(t->balance > 0);
9584 t->balance = 0;
9586 s = p->parent;
9587 p->parent = t;
9588 r->parent = t;
9589 t->parent = s;
9590 if (s)
9592 if (s->left == p)
9593 s->left = t;
9594 else
9595 s->right = t;
9597 else
9598 constructor_pending_elts = t;
9600 break;
9602 else
9604 /* p->balance == -1; growth of right side balances the node. */
9605 p->balance = 0;
9606 break;
9610 r = p;
9611 p = p->parent;
9615 /* Build AVL tree from a sorted chain. */
9617 static void
9618 set_nonincremental_init (struct obstack * braced_init_obstack)
9620 unsigned HOST_WIDE_INT ix;
9621 tree index, value;
9623 if (TREE_CODE (constructor_type) != RECORD_TYPE
9624 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9625 return;
9627 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9628 add_pending_init (input_location, index, value, NULL_TREE, true,
9629 braced_init_obstack);
9630 constructor_elements = NULL;
9631 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9633 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9634 /* Skip any nameless bit fields at the beginning. */
9635 while (constructor_unfilled_fields != NULL_TREE
9636 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9637 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9640 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9642 if (TYPE_DOMAIN (constructor_type))
9643 constructor_unfilled_index
9644 = convert (bitsizetype,
9645 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9646 else
9647 constructor_unfilled_index = bitsize_zero_node;
9649 constructor_incremental = 0;
9652 /* Build AVL tree from a string constant. */
9654 static void
9655 set_nonincremental_init_from_string (tree str,
9656 struct obstack * braced_init_obstack)
9658 tree value, purpose, type;
9659 HOST_WIDE_INT val[2];
9660 const char *p, *end;
9661 int byte, wchar_bytes, charwidth, bitpos;
9663 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9665 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9666 charwidth = TYPE_PRECISION (char_type_node);
9667 gcc_assert ((size_t) wchar_bytes * charwidth
9668 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9669 type = TREE_TYPE (constructor_type);
9670 p = TREE_STRING_POINTER (str);
9671 end = p + TREE_STRING_LENGTH (str);
9673 for (purpose = bitsize_zero_node;
9674 p < end
9675 && !(constructor_max_index
9676 && tree_int_cst_lt (constructor_max_index, purpose));
9677 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9679 if (wchar_bytes == 1)
9681 val[0] = (unsigned char) *p++;
9682 val[1] = 0;
9684 else
9686 val[1] = 0;
9687 val[0] = 0;
9688 for (byte = 0; byte < wchar_bytes; byte++)
9690 if (BYTES_BIG_ENDIAN)
9691 bitpos = (wchar_bytes - byte - 1) * charwidth;
9692 else
9693 bitpos = byte * charwidth;
9694 val[bitpos / HOST_BITS_PER_WIDE_INT]
9695 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9696 << (bitpos % HOST_BITS_PER_WIDE_INT);
9700 if (!TYPE_UNSIGNED (type))
9702 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9703 if (bitpos < HOST_BITS_PER_WIDE_INT)
9705 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9707 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9708 val[1] = -1;
9711 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9713 if (val[0] < 0)
9714 val[1] = -1;
9716 else if (val[1] & (HOST_WIDE_INT_1
9717 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9718 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9721 value = wide_int_to_tree (type,
9722 wide_int::from_array (val, 2,
9723 HOST_BITS_PER_WIDE_INT * 2));
9724 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9725 braced_init_obstack);
9728 constructor_incremental = 0;
9731 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9732 not initialized yet. */
9734 static tree
9735 find_init_member (tree field, struct obstack * braced_init_obstack)
9737 struct init_node *p;
9739 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9741 if (constructor_incremental
9742 && tree_int_cst_lt (field, constructor_unfilled_index))
9743 set_nonincremental_init (braced_init_obstack);
9745 p = constructor_pending_elts;
9746 while (p)
9748 if (tree_int_cst_lt (field, p->purpose))
9749 p = p->left;
9750 else if (tree_int_cst_lt (p->purpose, field))
9751 p = p->right;
9752 else
9753 return p->value;
9756 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9758 tree bitpos = bit_position (field);
9760 if (constructor_incremental
9761 && (!constructor_unfilled_fields
9762 || tree_int_cst_lt (bitpos,
9763 bit_position (constructor_unfilled_fields))))
9764 set_nonincremental_init (braced_init_obstack);
9766 p = constructor_pending_elts;
9767 while (p)
9769 if (field == p->purpose)
9770 return p->value;
9771 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9772 p = p->left;
9773 else
9774 p = p->right;
9777 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9779 if (!vec_safe_is_empty (constructor_elements)
9780 && (constructor_elements->last ().index == field))
9781 return constructor_elements->last ().value;
9783 return NULL_TREE;
9786 /* "Output" the next constructor element.
9787 At top level, really output it to assembler code now.
9788 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9789 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9790 TYPE is the data type that the containing data type wants here.
9791 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9792 If VALUE is a string constant, STRICT_STRING is true if it is
9793 unparenthesized or we should not warn here for it being parenthesized.
9794 For other types of VALUE, STRICT_STRING is not used.
9796 PENDING if true means output pending elements that belong
9797 right after this element. (PENDING is normally true;
9798 it is false while outputting pending elements, to avoid recursion.)
9800 IMPLICIT is true if value comes from pop_init_level (1),
9801 the new initializer has been merged with the existing one
9802 and thus no warnings should be emitted about overriding an
9803 existing initializer. */
9805 static void
9806 output_init_element (location_t loc, tree value, tree origtype,
9807 bool strict_string, tree type, tree field, bool pending,
9808 bool implicit, struct obstack * braced_init_obstack)
9810 tree semantic_type = NULL_TREE;
9811 bool maybe_const = true;
9812 bool npc;
9814 if (type == error_mark_node || value == error_mark_node)
9816 constructor_erroneous = 1;
9817 return;
9819 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9820 && (TREE_CODE (value) == STRING_CST
9821 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9822 && !(TREE_CODE (value) == STRING_CST
9823 && TREE_CODE (type) == ARRAY_TYPE
9824 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9825 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9826 TYPE_MAIN_VARIANT (type)))
9827 value = array_to_pointer_conversion (input_location, value);
9829 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9830 && require_constant_value && pending)
9832 /* As an extension, allow initializing objects with static storage
9833 duration with compound literals (which are then treated just as
9834 the brace enclosed list they contain). */
9835 if (flag_isoc99)
9836 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9837 "constant");
9838 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9839 value = DECL_INITIAL (decl);
9842 npc = null_pointer_constant_p (value);
9843 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9845 semantic_type = TREE_TYPE (value);
9846 value = TREE_OPERAND (value, 0);
9848 value = c_fully_fold (value, require_constant_value, &maybe_const);
9850 if (value == error_mark_node)
9851 constructor_erroneous = 1;
9852 else if (!TREE_CONSTANT (value))
9853 constructor_constant = 0;
9854 else if (!initializer_constant_valid_p (value,
9855 TREE_TYPE (value),
9856 AGGREGATE_TYPE_P (constructor_type)
9857 && TYPE_REVERSE_STORAGE_ORDER
9858 (constructor_type))
9859 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9860 && DECL_C_BIT_FIELD (field)
9861 && TREE_CODE (value) != INTEGER_CST))
9862 constructor_simple = 0;
9863 if (!maybe_const)
9864 constructor_nonconst = 1;
9866 /* Digest the initializer and issue any errors about incompatible
9867 types before issuing errors about non-constant initializers. */
9868 tree new_value = value;
9869 if (semantic_type)
9870 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9871 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9872 require_constant_value);
9873 if (new_value == error_mark_node)
9875 constructor_erroneous = 1;
9876 return;
9878 if (require_constant_value || require_constant_elements)
9879 constant_expression_warning (new_value);
9881 /* Proceed to check the constness of the original initializer. */
9882 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9884 if (require_constant_value)
9886 error_init (loc, "initializer element is not constant");
9887 value = error_mark_node;
9889 else if (require_constant_elements)
9890 pedwarn (loc, OPT_Wpedantic,
9891 "initializer element is not computable at load time");
9893 else if (!maybe_const
9894 && (require_constant_value || require_constant_elements))
9895 pedwarn_init (loc, OPT_Wpedantic,
9896 "initializer element is not a constant expression");
9898 /* Issue -Wc++-compat warnings about initializing a bitfield with
9899 enum type. */
9900 if (warn_cxx_compat
9901 && field != NULL_TREE
9902 && TREE_CODE (field) == FIELD_DECL
9903 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9904 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9905 != TYPE_MAIN_VARIANT (type))
9906 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9908 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9909 if (checktype != error_mark_node
9910 && (TYPE_MAIN_VARIANT (checktype)
9911 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9912 warning_init (loc, OPT_Wc___compat,
9913 "enum conversion in initialization is invalid in C++");
9916 /* If this field is empty and does not have side effects (and is not at
9917 the end of structure), don't do anything other than checking the
9918 initializer. */
9919 if (field
9920 && (TREE_TYPE (field) == error_mark_node
9921 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9922 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9923 && !TREE_SIDE_EFFECTS (new_value)
9924 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9925 || DECL_CHAIN (field)))))
9926 return;
9928 /* Finally, set VALUE to the initializer value digested above. */
9929 value = new_value;
9931 /* If this element doesn't come next in sequence,
9932 put it on constructor_pending_elts. */
9933 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9934 && (!constructor_incremental
9935 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9937 if (constructor_incremental
9938 && tree_int_cst_lt (field, constructor_unfilled_index))
9939 set_nonincremental_init (braced_init_obstack);
9941 add_pending_init (loc, field, value, origtype, implicit,
9942 braced_init_obstack);
9943 return;
9945 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9946 && (!constructor_incremental
9947 || field != constructor_unfilled_fields))
9949 /* We do this for records but not for unions. In a union,
9950 no matter which field is specified, it can be initialized
9951 right away since it starts at the beginning of the union. */
9952 if (constructor_incremental)
9954 if (!constructor_unfilled_fields)
9955 set_nonincremental_init (braced_init_obstack);
9956 else
9958 tree bitpos, unfillpos;
9960 bitpos = bit_position (field);
9961 unfillpos = bit_position (constructor_unfilled_fields);
9963 if (tree_int_cst_lt (bitpos, unfillpos))
9964 set_nonincremental_init (braced_init_obstack);
9968 add_pending_init (loc, field, value, origtype, implicit,
9969 braced_init_obstack);
9970 return;
9972 else if (TREE_CODE (constructor_type) == UNION_TYPE
9973 && !vec_safe_is_empty (constructor_elements))
9975 if (!implicit)
9977 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9978 warning_init (loc, OPT_Woverride_init_side_effects,
9979 "initialized field with side-effects overwritten");
9980 else if (warn_override_init)
9981 warning_init (loc, OPT_Woverride_init,
9982 "initialized field overwritten");
9985 /* We can have just one union field set. */
9986 constructor_elements = NULL;
9989 /* Otherwise, output this element either to
9990 constructor_elements or to the assembler file. */
9992 constructor_elt celt = {field, value};
9993 vec_safe_push (constructor_elements, celt);
9995 /* Advance the variable that indicates sequential elements output. */
9996 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9997 constructor_unfilled_index
9998 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9999 bitsize_one_node);
10000 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
10002 constructor_unfilled_fields
10003 = DECL_CHAIN (constructor_unfilled_fields);
10005 /* Skip any nameless bit fields. */
10006 while (constructor_unfilled_fields != NULL_TREE
10007 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
10008 constructor_unfilled_fields =
10009 DECL_CHAIN (constructor_unfilled_fields);
10011 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10012 constructor_unfilled_fields = NULL_TREE;
10014 /* Now output any pending elements which have become next. */
10015 if (pending)
10016 output_pending_init_elements (0, braced_init_obstack);
10019 /* For two FIELD_DECLs in the same chain, return -1 if field1
10020 comes before field2, 1 if field1 comes after field2 and
10021 0 if field1 == field2. */
10023 static int
10024 init_field_decl_cmp (tree field1, tree field2)
10026 if (field1 == field2)
10027 return 0;
10029 tree bitpos1 = bit_position (field1);
10030 tree bitpos2 = bit_position (field2);
10031 if (tree_int_cst_equal (bitpos1, bitpos2))
10033 /* If one of the fields has non-zero bitsize, then that
10034 field must be the last one in a sequence of zero
10035 sized fields, fields after it will have bigger
10036 bit_position. */
10037 if (TREE_TYPE (field1) != error_mark_node
10038 && COMPLETE_TYPE_P (TREE_TYPE (field1))
10039 && integer_nonzerop (TREE_TYPE (field1)))
10040 return 1;
10041 if (TREE_TYPE (field2) != error_mark_node
10042 && COMPLETE_TYPE_P (TREE_TYPE (field2))
10043 && integer_nonzerop (TREE_TYPE (field2)))
10044 return -1;
10045 /* Otherwise, fallback to DECL_CHAIN walk to find out
10046 which field comes earlier. Walk chains of both
10047 fields, so that if field1 and field2 are close to each
10048 other in either order, it is found soon even for large
10049 sequences of zero sized fields. */
10050 tree f1 = field1, f2 = field2;
10051 while (1)
10053 f1 = DECL_CHAIN (f1);
10054 f2 = DECL_CHAIN (f2);
10055 if (f1 == NULL_TREE)
10057 gcc_assert (f2);
10058 return 1;
10060 if (f2 == NULL_TREE)
10061 return -1;
10062 if (f1 == field2)
10063 return -1;
10064 if (f2 == field1)
10065 return 1;
10066 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
10067 return 1;
10068 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
10069 return -1;
10072 else if (tree_int_cst_lt (bitpos1, bitpos2))
10073 return -1;
10074 else
10075 return 1;
10078 /* Output any pending elements which have become next.
10079 As we output elements, constructor_unfilled_{fields,index}
10080 advances, which may cause other elements to become next;
10081 if so, they too are output.
10083 If ALL is 0, we return when there are
10084 no more pending elements to output now.
10086 If ALL is 1, we output space as necessary so that
10087 we can output all the pending elements. */
10088 static void
10089 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
10091 struct init_node *elt = constructor_pending_elts;
10092 tree next;
10094 retry:
10096 /* Look through the whole pending tree.
10097 If we find an element that should be output now,
10098 output it. Otherwise, set NEXT to the element
10099 that comes first among those still pending. */
10101 next = NULL_TREE;
10102 while (elt)
10104 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10106 if (tree_int_cst_equal (elt->purpose,
10107 constructor_unfilled_index))
10108 output_init_element (input_location, elt->value, elt->origtype,
10109 true, TREE_TYPE (constructor_type),
10110 constructor_unfilled_index, false, false,
10111 braced_init_obstack);
10112 else if (tree_int_cst_lt (constructor_unfilled_index,
10113 elt->purpose))
10115 /* Advance to the next smaller node. */
10116 if (elt->left)
10117 elt = elt->left;
10118 else
10120 /* We have reached the smallest node bigger than the
10121 current unfilled index. Fill the space first. */
10122 next = elt->purpose;
10123 break;
10126 else
10128 /* Advance to the next bigger node. */
10129 if (elt->right)
10130 elt = elt->right;
10131 else
10133 /* We have reached the biggest node in a subtree. Find
10134 the parent of it, which is the next bigger node. */
10135 while (elt->parent && elt->parent->right == elt)
10136 elt = elt->parent;
10137 elt = elt->parent;
10138 if (elt && tree_int_cst_lt (constructor_unfilled_index,
10139 elt->purpose))
10141 next = elt->purpose;
10142 break;
10147 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10149 /* If the current record is complete we are done. */
10150 if (constructor_unfilled_fields == NULL_TREE)
10151 break;
10153 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10154 elt->purpose);
10155 if (cmp == 0)
10156 output_init_element (input_location, elt->value, elt->origtype,
10157 true, TREE_TYPE (elt->purpose),
10158 elt->purpose, false, false,
10159 braced_init_obstack);
10160 else if (cmp < 0)
10162 /* Advance to the next smaller node. */
10163 if (elt->left)
10164 elt = elt->left;
10165 else
10167 /* We have reached the smallest node bigger than the
10168 current unfilled field. Fill the space first. */
10169 next = elt->purpose;
10170 break;
10173 else
10175 /* Advance to the next bigger node. */
10176 if (elt->right)
10177 elt = elt->right;
10178 else
10180 /* We have reached the biggest node in a subtree. Find
10181 the parent of it, which is the next bigger node. */
10182 while (elt->parent && elt->parent->right == elt)
10183 elt = elt->parent;
10184 elt = elt->parent;
10185 if (elt
10186 && init_field_decl_cmp (constructor_unfilled_fields,
10187 elt->purpose) < 0)
10189 next = elt->purpose;
10190 break;
10197 /* Ordinarily return, but not if we want to output all
10198 and there are elements left. */
10199 if (!(all && next != NULL_TREE))
10200 return;
10202 /* If it's not incremental, just skip over the gap, so that after
10203 jumping to retry we will output the next successive element. */
10204 if (RECORD_OR_UNION_TYPE_P (constructor_type))
10205 constructor_unfilled_fields = next;
10206 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10207 constructor_unfilled_index = next;
10209 /* ELT now points to the node in the pending tree with the next
10210 initializer to output. */
10211 goto retry;
10214 /* Expression VALUE coincides with the start of type TYPE in a braced
10215 initializer. Return true if we should treat VALUE as initializing
10216 the first element of TYPE, false if we should treat it as initializing
10217 TYPE as a whole.
10219 If the initializer is clearly invalid, the question becomes:
10220 which choice gives the best error message? */
10222 static bool
10223 initialize_elementwise_p (tree type, tree value)
10225 if (type == error_mark_node || value == error_mark_node)
10226 return false;
10228 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10230 tree value_type = TREE_TYPE (value);
10231 if (value_type == error_mark_node)
10232 return false;
10234 /* GNU vectors can be initialized elementwise. However, treat any
10235 kind of vector value as initializing the vector type as a whole,
10236 regardless of whether the value is a GNU vector. Such initializers
10237 are valid if and only if they would have been valid in a non-braced
10238 initializer like:
10240 TYPE foo = VALUE;
10242 so recursing into the vector type would be at best confusing or at
10243 worst wrong. For example, when -flax-vector-conversions is in effect,
10244 it's possible to initialize a V8HI from a V4SI, even though the vectors
10245 have different element types and different numbers of elements. */
10246 if (gnu_vector_type_p (type))
10247 return !VECTOR_TYPE_P (value_type);
10249 if (AGGREGATE_TYPE_P (type))
10250 return type != TYPE_MAIN_VARIANT (value_type);
10252 return false;
10255 /* Add one non-braced element to the current constructor level.
10256 This adjusts the current position within the constructor's type.
10257 This may also start or terminate implicit levels
10258 to handle a partly-braced initializer.
10260 Once this has found the correct level for the new element,
10261 it calls output_init_element.
10263 IMPLICIT is true if value comes from pop_init_level (1),
10264 the new initializer has been merged with the existing one
10265 and thus no warnings should be emitted about overriding an
10266 existing initializer. */
10268 void
10269 process_init_element (location_t loc, struct c_expr value, bool implicit,
10270 struct obstack * braced_init_obstack)
10272 tree orig_value = value.value;
10273 int string_flag
10274 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10275 bool strict_string = value.original_code == STRING_CST;
10276 bool was_designated = designator_depth != 0;
10278 designator_depth = 0;
10279 designator_erroneous = 0;
10281 if (!implicit && value.value && !integer_zerop (value.value))
10282 constructor_zeroinit = 0;
10284 /* Handle superfluous braces around string cst as in
10285 char x[] = {"foo"}; */
10286 if (string_flag
10287 && constructor_type
10288 && !was_designated
10289 && TREE_CODE (constructor_type) == ARRAY_TYPE
10290 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10291 && integer_zerop (constructor_unfilled_index))
10293 if (constructor_stack->replacement_value.value)
10294 error_init (loc, "excess elements in %<char%> array initializer");
10295 constructor_stack->replacement_value = value;
10296 return;
10299 if (constructor_stack->replacement_value.value != NULL_TREE)
10301 error_init (loc, "excess elements in struct initializer");
10302 return;
10305 /* Ignore elements of a brace group if it is entirely superfluous
10306 and has already been diagnosed, or if the type is erroneous. */
10307 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10308 return;
10310 /* Ignore elements of an initializer for a variable-size type.
10311 Those are diagnosed in the parser (empty initializer braces are OK). */
10312 if (COMPLETE_TYPE_P (constructor_type)
10313 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10314 return;
10316 if (!implicit && warn_designated_init && !was_designated
10317 && TREE_CODE (constructor_type) == RECORD_TYPE
10318 && lookup_attribute ("designated_init",
10319 TYPE_ATTRIBUTES (constructor_type)))
10320 warning_init (loc,
10321 OPT_Wdesignated_init,
10322 "positional initialization of field "
10323 "in %<struct%> declared with %<designated_init%> attribute");
10325 /* If we've exhausted any levels that didn't have braces,
10326 pop them now. */
10327 while (constructor_stack->implicit)
10329 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10330 && constructor_fields == NULL_TREE)
10331 process_init_element (loc,
10332 pop_init_level (loc, 1, braced_init_obstack,
10333 last_init_list_comma),
10334 true, braced_init_obstack);
10335 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10336 || gnu_vector_type_p (constructor_type))
10337 && constructor_max_index
10338 && tree_int_cst_lt (constructor_max_index,
10339 constructor_index))
10340 process_init_element (loc,
10341 pop_init_level (loc, 1, braced_init_obstack,
10342 last_init_list_comma),
10343 true, braced_init_obstack);
10344 else
10345 break;
10348 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10349 if (constructor_range_stack)
10351 /* If value is a compound literal and we'll be just using its
10352 content, don't put it into a SAVE_EXPR. */
10353 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10354 || !require_constant_value)
10356 tree semantic_type = NULL_TREE;
10357 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10359 semantic_type = TREE_TYPE (value.value);
10360 value.value = TREE_OPERAND (value.value, 0);
10362 value.value = save_expr (value.value);
10363 if (semantic_type)
10364 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10365 value.value);
10369 while (1)
10371 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10373 tree fieldtype;
10374 enum tree_code fieldcode;
10376 if (constructor_fields == NULL_TREE)
10378 pedwarn_init (loc, 0, "excess elements in struct initializer");
10379 break;
10382 fieldtype = TREE_TYPE (constructor_fields);
10383 if (fieldtype != error_mark_node)
10384 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10385 fieldcode = TREE_CODE (fieldtype);
10387 /* Error for non-static initialization of a flexible array member. */
10388 if (fieldcode == ARRAY_TYPE
10389 && !require_constant_value
10390 && TYPE_SIZE (fieldtype) == NULL_TREE
10391 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10393 error_init (loc, "non-static initialization of a flexible "
10394 "array member");
10395 break;
10398 /* Error for initialization of a flexible array member with
10399 a string constant if the structure is in an array. E.g.:
10400 struct S { int x; char y[]; };
10401 struct S s[] = { { 1, "foo" } };
10402 is invalid. */
10403 if (string_flag
10404 && fieldcode == ARRAY_TYPE
10405 && constructor_depth > 1
10406 && TYPE_SIZE (fieldtype) == NULL_TREE
10407 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10409 bool in_array_p = false;
10410 for (struct constructor_stack *p = constructor_stack;
10411 p && p->type; p = p->next)
10412 if (TREE_CODE (p->type) == ARRAY_TYPE)
10414 in_array_p = true;
10415 break;
10417 if (in_array_p)
10419 error_init (loc, "initialization of flexible array "
10420 "member in a nested context");
10421 break;
10425 /* Accept a string constant to initialize a subarray. */
10426 if (value.value != NULL_TREE
10427 && fieldcode == ARRAY_TYPE
10428 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10429 && string_flag)
10430 value.value = orig_value;
10431 /* Otherwise, if we have come to a subaggregate,
10432 and we don't have an element of its type, push into it. */
10433 else if (value.value != NULL_TREE
10434 && initialize_elementwise_p (fieldtype, value.value))
10436 push_init_level (loc, 1, braced_init_obstack);
10437 continue;
10440 if (value.value)
10442 push_member_name (constructor_fields);
10443 output_init_element (loc, value.value, value.original_type,
10444 strict_string, fieldtype,
10445 constructor_fields, true, implicit,
10446 braced_init_obstack);
10447 RESTORE_SPELLING_DEPTH (constructor_depth);
10449 else
10450 /* Do the bookkeeping for an element that was
10451 directly output as a constructor. */
10453 /* For a record, keep track of end position of last field. */
10454 if (DECL_SIZE (constructor_fields))
10455 constructor_bit_index
10456 = size_binop_loc (input_location, PLUS_EXPR,
10457 bit_position (constructor_fields),
10458 DECL_SIZE (constructor_fields));
10460 /* If the current field was the first one not yet written out,
10461 it isn't now, so update. */
10462 if (constructor_unfilled_fields == constructor_fields)
10464 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10465 /* Skip any nameless bit fields. */
10466 while (constructor_unfilled_fields != 0
10467 && (DECL_UNNAMED_BIT_FIELD
10468 (constructor_unfilled_fields)))
10469 constructor_unfilled_fields =
10470 DECL_CHAIN (constructor_unfilled_fields);
10474 constructor_fields = DECL_CHAIN (constructor_fields);
10475 /* Skip any nameless bit fields at the beginning. */
10476 while (constructor_fields != NULL_TREE
10477 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10478 constructor_fields = DECL_CHAIN (constructor_fields);
10480 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10482 tree fieldtype;
10483 enum tree_code fieldcode;
10485 if (constructor_fields == NULL_TREE)
10487 pedwarn_init (loc, 0,
10488 "excess elements in union initializer");
10489 break;
10492 fieldtype = TREE_TYPE (constructor_fields);
10493 if (fieldtype != error_mark_node)
10494 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10495 fieldcode = TREE_CODE (fieldtype);
10497 /* Warn that traditional C rejects initialization of unions.
10498 We skip the warning if the value is zero. This is done
10499 under the assumption that the zero initializer in user
10500 code appears conditioned on e.g. __STDC__ to avoid
10501 "missing initializer" warnings and relies on default
10502 initialization to zero in the traditional C case.
10503 We also skip the warning if the initializer is designated,
10504 again on the assumption that this must be conditional on
10505 __STDC__ anyway (and we've already complained about the
10506 member-designator already). */
10507 if (!in_system_header_at (input_location) && !constructor_designated
10508 && !(value.value && (integer_zerop (value.value)
10509 || real_zerop (value.value))))
10510 warning (OPT_Wtraditional, "traditional C rejects initialization "
10511 "of unions");
10513 /* Accept a string constant to initialize a subarray. */
10514 if (value.value != NULL_TREE
10515 && fieldcode == ARRAY_TYPE
10516 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10517 && string_flag)
10518 value.value = orig_value;
10519 /* Otherwise, if we have come to a subaggregate,
10520 and we don't have an element of its type, push into it. */
10521 else if (value.value != NULL_TREE
10522 && initialize_elementwise_p (fieldtype, value.value))
10524 push_init_level (loc, 1, braced_init_obstack);
10525 continue;
10528 if (value.value)
10530 push_member_name (constructor_fields);
10531 output_init_element (loc, value.value, value.original_type,
10532 strict_string, fieldtype,
10533 constructor_fields, true, implicit,
10534 braced_init_obstack);
10535 RESTORE_SPELLING_DEPTH (constructor_depth);
10537 else
10538 /* Do the bookkeeping for an element that was
10539 directly output as a constructor. */
10541 constructor_bit_index = DECL_SIZE (constructor_fields);
10542 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10545 constructor_fields = NULL_TREE;
10547 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10549 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10550 enum tree_code eltcode = TREE_CODE (elttype);
10552 /* Accept a string constant to initialize a subarray. */
10553 if (value.value != NULL_TREE
10554 && eltcode == ARRAY_TYPE
10555 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10556 && string_flag)
10557 value.value = orig_value;
10558 /* Otherwise, if we have come to a subaggregate,
10559 and we don't have an element of its type, push into it. */
10560 else if (value.value != NULL_TREE
10561 && initialize_elementwise_p (elttype, value.value))
10563 push_init_level (loc, 1, braced_init_obstack);
10564 continue;
10567 if (constructor_max_index != NULL_TREE
10568 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10569 || integer_all_onesp (constructor_max_index)))
10571 pedwarn_init (loc, 0,
10572 "excess elements in array initializer");
10573 break;
10576 /* Now output the actual element. */
10577 if (value.value)
10579 push_array_bounds (tree_to_uhwi (constructor_index));
10580 output_init_element (loc, value.value, value.original_type,
10581 strict_string, elttype,
10582 constructor_index, true, implicit,
10583 braced_init_obstack);
10584 RESTORE_SPELLING_DEPTH (constructor_depth);
10587 constructor_index
10588 = size_binop_loc (input_location, PLUS_EXPR,
10589 constructor_index, bitsize_one_node);
10591 if (!value.value)
10592 /* If we are doing the bookkeeping for an element that was
10593 directly output as a constructor, we must update
10594 constructor_unfilled_index. */
10595 constructor_unfilled_index = constructor_index;
10597 else if (gnu_vector_type_p (constructor_type))
10599 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10601 /* Do a basic check of initializer size. Note that vectors
10602 always have a fixed size derived from their type. */
10603 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10605 pedwarn_init (loc, 0,
10606 "excess elements in vector initializer");
10607 break;
10610 /* Now output the actual element. */
10611 if (value.value)
10613 if (TREE_CODE (value.value) == VECTOR_CST)
10614 elttype = TYPE_MAIN_VARIANT (constructor_type);
10615 output_init_element (loc, value.value, value.original_type,
10616 strict_string, elttype,
10617 constructor_index, true, implicit,
10618 braced_init_obstack);
10621 constructor_index
10622 = size_binop_loc (input_location,
10623 PLUS_EXPR, constructor_index, bitsize_one_node);
10625 if (!value.value)
10626 /* If we are doing the bookkeeping for an element that was
10627 directly output as a constructor, we must update
10628 constructor_unfilled_index. */
10629 constructor_unfilled_index = constructor_index;
10632 /* Handle the sole element allowed in a braced initializer
10633 for a scalar variable. */
10634 else if (constructor_type != error_mark_node
10635 && constructor_fields == NULL_TREE)
10637 pedwarn_init (loc, 0,
10638 "excess elements in scalar initializer");
10639 break;
10641 else
10643 if (value.value)
10644 output_init_element (loc, value.value, value.original_type,
10645 strict_string, constructor_type,
10646 NULL_TREE, true, implicit,
10647 braced_init_obstack);
10648 constructor_fields = NULL_TREE;
10651 /* Handle range initializers either at this level or anywhere higher
10652 in the designator stack. */
10653 if (constructor_range_stack)
10655 struct constructor_range_stack *p, *range_stack;
10656 int finish = 0;
10658 range_stack = constructor_range_stack;
10659 constructor_range_stack = 0;
10660 while (constructor_stack != range_stack->stack)
10662 gcc_assert (constructor_stack->implicit);
10663 process_init_element (loc,
10664 pop_init_level (loc, 1,
10665 braced_init_obstack,
10666 last_init_list_comma),
10667 true, braced_init_obstack);
10669 for (p = range_stack;
10670 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10671 p = p->prev)
10673 gcc_assert (constructor_stack->implicit);
10674 process_init_element (loc,
10675 pop_init_level (loc, 1,
10676 braced_init_obstack,
10677 last_init_list_comma),
10678 true, braced_init_obstack);
10681 p->index = size_binop_loc (input_location,
10682 PLUS_EXPR, p->index, bitsize_one_node);
10683 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10684 finish = 1;
10686 while (1)
10688 constructor_index = p->index;
10689 constructor_fields = p->fields;
10690 if (finish && p->range_end && p->index == p->range_start)
10692 finish = 0;
10693 p->prev = 0;
10695 p = p->next;
10696 if (!p)
10697 break;
10698 finish_implicit_inits (loc, braced_init_obstack);
10699 push_init_level (loc, 2, braced_init_obstack);
10700 p->stack = constructor_stack;
10701 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10702 p->index = p->range_start;
10705 if (!finish)
10706 constructor_range_stack = range_stack;
10707 continue;
10710 break;
10713 constructor_range_stack = 0;
10716 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10717 (guaranteed to be 'volatile' or null) and ARGS (represented using
10718 an ASM_EXPR node). */
10719 tree
10720 build_asm_stmt (bool is_volatile, tree args)
10722 if (is_volatile)
10723 ASM_VOLATILE_P (args) = 1;
10724 return add_stmt (args);
10727 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10728 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10729 SIMPLE indicates whether there was anything at all after the
10730 string in the asm expression -- asm("blah") and asm("blah" : )
10731 are subtly different. We use a ASM_EXPR node to represent this.
10732 LOC is the location of the asm, and IS_INLINE says whether this
10733 is asm inline. */
10734 tree
10735 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10736 tree clobbers, tree labels, bool simple, bool is_inline)
10738 tree tail;
10739 tree args;
10740 int i;
10741 const char *constraint;
10742 const char **oconstraints;
10743 bool allows_mem, allows_reg, is_inout;
10744 int ninputs, noutputs;
10746 ninputs = list_length (inputs);
10747 noutputs = list_length (outputs);
10748 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10750 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10752 /* Remove output conversions that change the type but not the mode. */
10753 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10755 tree output = TREE_VALUE (tail);
10757 output = c_fully_fold (output, false, NULL, true);
10759 /* ??? Really, this should not be here. Users should be using a
10760 proper lvalue, dammit. But there's a long history of using casts
10761 in the output operands. In cases like longlong.h, this becomes a
10762 primitive form of typechecking -- if the cast can be removed, then
10763 the output operand had a type of the proper width; otherwise we'll
10764 get an error. Gross, but ... */
10765 STRIP_NOPS (output);
10767 if (!lvalue_or_else (loc, output, lv_asm))
10768 output = error_mark_node;
10770 if (output != error_mark_node
10771 && (TREE_READONLY (output)
10772 || TYPE_READONLY (TREE_TYPE (output))
10773 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10774 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10775 readonly_error (loc, output, lv_asm);
10777 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10778 oconstraints[i] = constraint;
10780 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10781 &allows_mem, &allows_reg, &is_inout))
10783 /* If the operand is going to end up in memory,
10784 mark it addressable. */
10785 if (!allows_reg && !c_mark_addressable (output))
10786 output = error_mark_node;
10787 if (!(!allows_reg && allows_mem)
10788 && output != error_mark_node
10789 && VOID_TYPE_P (TREE_TYPE (output)))
10791 error_at (loc, "invalid use of void expression");
10792 output = error_mark_node;
10795 else
10796 output = error_mark_node;
10798 TREE_VALUE (tail) = output;
10801 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10803 tree input;
10805 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10806 input = TREE_VALUE (tail);
10808 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10809 oconstraints, &allows_mem, &allows_reg))
10811 /* If the operand is going to end up in memory,
10812 mark it addressable. */
10813 if (!allows_reg && allows_mem)
10815 input = c_fully_fold (input, false, NULL, true);
10817 /* Strip the nops as we allow this case. FIXME, this really
10818 should be rejected or made deprecated. */
10819 STRIP_NOPS (input);
10820 if (!c_mark_addressable (input))
10821 input = error_mark_node;
10823 else
10825 struct c_expr expr;
10826 memset (&expr, 0, sizeof (expr));
10827 expr.value = input;
10828 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10829 input = c_fully_fold (expr.value, false, NULL);
10831 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10833 error_at (loc, "invalid use of void expression");
10834 input = error_mark_node;
10838 else
10839 input = error_mark_node;
10841 TREE_VALUE (tail) = input;
10844 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10846 /* asm statements without outputs, including simple ones, are treated
10847 as volatile. */
10848 ASM_INPUT_P (args) = simple;
10849 ASM_VOLATILE_P (args) = (noutputs == 0);
10850 ASM_INLINE_P (args) = is_inline;
10852 return args;
10855 /* Generate a goto statement to LABEL. LOC is the location of the
10856 GOTO. */
10858 tree
10859 c_finish_goto_label (location_t loc, tree label)
10861 tree decl = lookup_label_for_goto (loc, label);
10862 if (!decl)
10863 return NULL_TREE;
10864 TREE_USED (decl) = 1;
10866 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10867 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10868 SET_EXPR_LOCATION (t, loc);
10869 return add_stmt (t);
10873 /* Generate a computed goto statement to EXPR. LOC is the location of
10874 the GOTO. */
10876 tree
10877 c_finish_goto_ptr (location_t loc, c_expr val)
10879 tree expr = val.value;
10880 tree t;
10881 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10882 if (expr != error_mark_node
10883 && !POINTER_TYPE_P (TREE_TYPE (expr))
10884 && !null_pointer_constant_p (expr))
10886 error_at (val.get_location (),
10887 "computed goto must be pointer type");
10888 expr = build_zero_cst (ptr_type_node);
10890 expr = c_fully_fold (expr, false, NULL);
10891 expr = convert (ptr_type_node, expr);
10892 t = build1 (GOTO_EXPR, void_type_node, expr);
10893 SET_EXPR_LOCATION (t, loc);
10894 return add_stmt (t);
10897 /* Generate a C `return' statement. RETVAL is the expression for what
10898 to return, or a null pointer for `return;' with no value. LOC is
10899 the location of the return statement, or the location of the expression,
10900 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10901 is the original type of RETVAL. */
10903 tree
10904 c_finish_return (location_t loc, tree retval, tree origtype)
10906 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10907 bool no_warning = false;
10908 bool npc = false;
10910 /* Use the expansion point to handle cases such as returning NULL
10911 in a function returning void. */
10912 location_t xloc = expansion_point_location_if_in_system_header (loc);
10914 if (TREE_THIS_VOLATILE (current_function_decl))
10915 warning_at (xloc, 0,
10916 "function declared %<noreturn%> has a %<return%> statement");
10918 if (retval)
10920 tree semantic_type = NULL_TREE;
10921 npc = null_pointer_constant_p (retval);
10922 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10924 semantic_type = TREE_TYPE (retval);
10925 retval = TREE_OPERAND (retval, 0);
10927 retval = c_fully_fold (retval, false, NULL);
10928 if (semantic_type
10929 && valtype != NULL_TREE
10930 && TREE_CODE (valtype) != VOID_TYPE)
10931 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10934 if (!retval)
10936 current_function_returns_null = 1;
10937 if ((warn_return_type >= 0 || flag_isoc99)
10938 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10940 bool warned_here;
10941 if (flag_isoc99)
10942 warned_here = pedwarn
10943 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10944 "%<return%> with no value, in function returning non-void");
10945 else
10946 warned_here = warning_at
10947 (loc, OPT_Wreturn_type,
10948 "%<return%> with no value, in function returning non-void");
10949 no_warning = true;
10950 if (warned_here)
10951 inform (DECL_SOURCE_LOCATION (current_function_decl),
10952 "declared here");
10955 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10957 current_function_returns_null = 1;
10958 bool warned_here;
10959 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10960 warned_here = pedwarn
10961 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10962 "%<return%> with a value, in function returning void");
10963 else
10964 warned_here = pedwarn
10965 (xloc, OPT_Wpedantic, "ISO C forbids "
10966 "%<return%> with expression, in function returning void");
10967 if (warned_here)
10968 inform (DECL_SOURCE_LOCATION (current_function_decl),
10969 "declared here");
10971 else
10973 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10974 retval, origtype, ic_return,
10975 npc, NULL_TREE, NULL_TREE, 0);
10976 tree res = DECL_RESULT (current_function_decl);
10977 tree inner;
10978 bool save;
10980 current_function_returns_value = 1;
10981 if (t == error_mark_node)
10982 return NULL_TREE;
10984 save = in_late_binary_op;
10985 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10986 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10987 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10988 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10989 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10990 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10991 in_late_binary_op = true;
10992 inner = t = convert (TREE_TYPE (res), t);
10993 in_late_binary_op = save;
10995 /* Strip any conversions, additions, and subtractions, and see if
10996 we are returning the address of a local variable. Warn if so. */
10997 while (1)
10999 switch (TREE_CODE (inner))
11001 CASE_CONVERT:
11002 case NON_LVALUE_EXPR:
11003 case PLUS_EXPR:
11004 case POINTER_PLUS_EXPR:
11005 inner = TREE_OPERAND (inner, 0);
11006 continue;
11008 case MINUS_EXPR:
11009 /* If the second operand of the MINUS_EXPR has a pointer
11010 type (or is converted from it), this may be valid, so
11011 don't give a warning. */
11013 tree op1 = TREE_OPERAND (inner, 1);
11015 while (!POINTER_TYPE_P (TREE_TYPE (op1))
11016 && (CONVERT_EXPR_P (op1)
11017 || TREE_CODE (op1) == NON_LVALUE_EXPR))
11018 op1 = TREE_OPERAND (op1, 0);
11020 if (POINTER_TYPE_P (TREE_TYPE (op1)))
11021 break;
11023 inner = TREE_OPERAND (inner, 0);
11024 continue;
11027 case ADDR_EXPR:
11028 inner = TREE_OPERAND (inner, 0);
11030 while (REFERENCE_CLASS_P (inner)
11031 && !INDIRECT_REF_P (inner))
11032 inner = TREE_OPERAND (inner, 0);
11034 if (DECL_P (inner)
11035 && !DECL_EXTERNAL (inner)
11036 && !TREE_STATIC (inner)
11037 && DECL_CONTEXT (inner) == current_function_decl
11038 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
11040 if (TREE_CODE (inner) == LABEL_DECL)
11041 warning_at (loc, OPT_Wreturn_local_addr,
11042 "function returns address of label");
11043 else
11045 warning_at (loc, OPT_Wreturn_local_addr,
11046 "function returns address of local variable");
11047 tree zero = build_zero_cst (TREE_TYPE (res));
11048 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
11051 break;
11053 default:
11054 break;
11057 break;
11060 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
11061 SET_EXPR_LOCATION (retval, loc);
11063 if (warn_sequence_point)
11064 verify_sequence_points (retval);
11067 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
11068 if (no_warning)
11069 suppress_warning (ret_stmt, OPT_Wreturn_type);
11070 return add_stmt (ret_stmt);
11073 struct c_switch {
11074 /* The SWITCH_STMT being built. */
11075 tree switch_stmt;
11077 /* The original type of the testing expression, i.e. before the
11078 default conversion is applied. */
11079 tree orig_type;
11081 /* A splay-tree mapping the low element of a case range to the high
11082 element, or NULL_TREE if there is no high element. Used to
11083 determine whether or not a new case label duplicates an old case
11084 label. We need a tree, rather than simply a hash table, because
11085 of the GNU case range extension. */
11086 splay_tree cases;
11088 /* The bindings at the point of the switch. This is used for
11089 warnings crossing decls when branching to a case label. */
11090 struct c_spot_bindings *bindings;
11092 /* Whether the switch includes any break statements. */
11093 bool break_stmt_seen_p;
11095 /* The next node on the stack. */
11096 struct c_switch *next;
11098 /* Remember whether the controlling expression had boolean type
11099 before integer promotions for the sake of -Wswitch-bool. */
11100 bool bool_cond_p;
11103 /* A stack of the currently active switch statements. The innermost
11104 switch statement is on the top of the stack. There is no need to
11105 mark the stack for garbage collection because it is only active
11106 during the processing of the body of a function, and we never
11107 collect at that point. */
11109 struct c_switch *c_switch_stack;
11111 /* Start a C switch statement, testing expression EXP. Return the new
11112 SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
11113 SWITCH_COND_LOC is the location of the switch's condition.
11114 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
11116 tree
11117 c_start_switch (location_t switch_loc,
11118 location_t switch_cond_loc,
11119 tree exp, bool explicit_cast_p)
11121 tree orig_type = error_mark_node;
11122 bool bool_cond_p = false;
11123 struct c_switch *cs;
11125 if (exp != error_mark_node)
11127 orig_type = TREE_TYPE (exp);
11129 if (!INTEGRAL_TYPE_P (orig_type))
11131 if (orig_type != error_mark_node)
11133 error_at (switch_cond_loc, "switch quantity not an integer");
11134 orig_type = error_mark_node;
11136 exp = integer_zero_node;
11138 else
11140 tree type = TYPE_MAIN_VARIANT (orig_type);
11141 tree e = exp;
11143 /* Warn if the condition has boolean value. */
11144 while (TREE_CODE (e) == COMPOUND_EXPR)
11145 e = TREE_OPERAND (e, 1);
11147 if ((TREE_CODE (type) == BOOLEAN_TYPE
11148 || truth_value_p (TREE_CODE (e)))
11149 /* Explicit cast to int suppresses this warning. */
11150 && !(TREE_CODE (type) == INTEGER_TYPE
11151 && explicit_cast_p))
11152 bool_cond_p = true;
11154 if (!in_system_header_at (input_location)
11155 && (type == long_integer_type_node
11156 || type == long_unsigned_type_node))
11157 warning_at (switch_cond_loc,
11158 OPT_Wtraditional, "%<long%> switch expression not "
11159 "converted to %<int%> in ISO C");
11161 exp = c_fully_fold (exp, false, NULL);
11162 exp = default_conversion (exp);
11164 if (warn_sequence_point)
11165 verify_sequence_points (exp);
11169 /* Add this new SWITCH_STMT to the stack. */
11170 cs = XNEW (struct c_switch);
11171 cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11172 NULL_TREE, orig_type, NULL_TREE);
11173 cs->orig_type = orig_type;
11174 cs->cases = splay_tree_new (case_compare, NULL, NULL);
11175 cs->bindings = c_get_switch_bindings ();
11176 cs->break_stmt_seen_p = false;
11177 cs->bool_cond_p = bool_cond_p;
11178 cs->next = c_switch_stack;
11179 c_switch_stack = cs;
11181 return add_stmt (cs->switch_stmt);
11184 /* Process a case label at location LOC, with attributes ATTRS. */
11186 tree
11187 do_case (location_t loc, tree low_value, tree high_value, tree attrs)
11189 tree label = NULL_TREE;
11191 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11193 low_value = c_fully_fold (low_value, false, NULL);
11194 if (TREE_CODE (low_value) == INTEGER_CST)
11195 pedwarn (loc, OPT_Wpedantic,
11196 "case label is not an integer constant expression");
11199 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11201 high_value = c_fully_fold (high_value, false, NULL);
11202 if (TREE_CODE (high_value) == INTEGER_CST)
11203 pedwarn (input_location, OPT_Wpedantic,
11204 "case label is not an integer constant expression");
11207 if (c_switch_stack == NULL)
11209 if (low_value)
11210 error_at (loc, "case label not within a switch statement");
11211 else
11212 error_at (loc, "%<default%> label not within a switch statement");
11213 return NULL_TREE;
11216 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11217 EXPR_LOCATION (c_switch_stack->switch_stmt),
11218 loc))
11219 return NULL_TREE;
11221 label = c_add_case_label (loc, c_switch_stack->cases,
11222 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11223 low_value, high_value, attrs);
11224 if (label == error_mark_node)
11225 label = NULL_TREE;
11226 return label;
11229 /* Finish the switch statement. TYPE is the original type of the
11230 controlling expression of the switch, or NULL_TREE. */
11232 void
11233 c_finish_switch (tree body, tree type)
11235 struct c_switch *cs = c_switch_stack;
11236 location_t switch_location;
11238 SWITCH_STMT_BODY (cs->switch_stmt) = body;
11240 /* Emit warnings as needed. */
11241 switch_location = EXPR_LOCATION (cs->switch_stmt);
11242 c_do_switch_warnings (cs->cases, switch_location,
11243 type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11244 SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11245 if (c_switch_covers_all_cases_p (cs->cases,
11246 SWITCH_STMT_TYPE (cs->switch_stmt)))
11247 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11248 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11250 /* Pop the stack. */
11251 c_switch_stack = cs->next;
11252 splay_tree_delete (cs->cases);
11253 c_release_switch_bindings (cs->bindings);
11254 XDELETE (cs);
11257 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
11258 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11259 may be null. */
11261 void
11262 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11263 tree else_block)
11265 tree stmt;
11267 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11268 SET_EXPR_LOCATION (stmt, if_locus);
11269 add_stmt (stmt);
11272 tree
11273 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11275 /* In switch statements break is sometimes stylistically used after
11276 a return statement. This can lead to spurious warnings about
11277 control reaching the end of a non-void function when it is
11278 inlined. Note that we are calling block_may_fallthru with
11279 language specific tree nodes; this works because
11280 block_may_fallthru returns true when given something it does not
11281 understand. */
11282 bool skip = !block_may_fallthru (cur_stmt_list);
11284 if (is_break)
11285 switch (in_statement)
11287 case 0:
11288 error_at (loc, "break statement not within loop or switch");
11289 return NULL_TREE;
11290 case IN_OMP_BLOCK:
11291 error_at (loc, "invalid exit from OpenMP structured block");
11292 return NULL_TREE;
11293 case IN_OMP_FOR:
11294 error_at (loc, "break statement used with OpenMP for loop");
11295 return NULL_TREE;
11296 case IN_ITERATION_STMT:
11297 case IN_OBJC_FOREACH:
11298 break;
11299 default:
11300 gcc_assert (in_statement & IN_SWITCH_STMT);
11301 c_switch_stack->break_stmt_seen_p = true;
11302 break;
11304 else
11305 switch (in_statement & ~IN_SWITCH_STMT)
11307 case 0:
11308 error_at (loc, "continue statement not within a loop");
11309 return NULL_TREE;
11310 case IN_OMP_BLOCK:
11311 error_at (loc, "invalid exit from OpenMP structured block");
11312 return NULL_TREE;
11313 case IN_ITERATION_STMT:
11314 case IN_OMP_FOR:
11315 case IN_OBJC_FOREACH:
11316 break;
11317 default:
11318 gcc_unreachable ();
11321 if (skip)
11322 return NULL_TREE;
11323 else if ((in_statement & IN_OBJC_FOREACH)
11324 && !(is_break && (in_statement & IN_SWITCH_STMT)))
11326 /* The foreach expander produces low-level code using gotos instead
11327 of a structured loop construct. */
11328 gcc_assert (label);
11329 return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11331 return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11334 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11336 static void
11337 emit_side_effect_warnings (location_t loc, tree expr)
11339 maybe_warn_nodiscard (loc, expr);
11340 if (!warn_unused_value)
11341 return;
11342 if (expr == error_mark_node)
11344 else if (!TREE_SIDE_EFFECTS (expr))
11346 if (!VOID_TYPE_P (TREE_TYPE (expr))
11347 && !warning_suppressed_p (expr, OPT_Wunused_value))
11348 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11350 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11352 tree r = expr;
11353 location_t cloc = loc;
11354 while (TREE_CODE (r) == COMPOUND_EXPR)
11356 if (EXPR_HAS_LOCATION (r))
11357 cloc = EXPR_LOCATION (r);
11358 r = TREE_OPERAND (r, 1);
11360 if (!TREE_SIDE_EFFECTS (r)
11361 && !VOID_TYPE_P (TREE_TYPE (r))
11362 && !CONVERT_EXPR_P (r)
11363 && !warning_suppressed_p (r, OPT_Wunused_value)
11364 && !warning_suppressed_p (expr, OPT_Wunused_value))
11365 warning_at (cloc, OPT_Wunused_value,
11366 "right-hand operand of comma expression has no effect");
11368 else
11369 warn_if_unused_value (expr, loc);
11372 /* Process an expression as if it were a complete statement. Emit
11373 diagnostics, but do not call ADD_STMT. LOC is the location of the
11374 statement. */
11376 tree
11377 c_process_expr_stmt (location_t loc, tree expr)
11379 tree exprv;
11381 if (!expr)
11382 return NULL_TREE;
11384 expr = c_fully_fold (expr, false, NULL);
11386 if (warn_sequence_point)
11387 verify_sequence_points (expr);
11389 if (TREE_TYPE (expr) != error_mark_node
11390 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11391 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11392 error_at (loc, "expression statement has incomplete type");
11394 /* If we're not processing a statement expression, warn about unused values.
11395 Warnings for statement expressions will be emitted later, once we figure
11396 out which is the result. */
11397 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11398 && (warn_unused_value || warn_unused_result))
11399 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11401 exprv = expr;
11402 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11403 exprv = TREE_OPERAND (exprv, 1);
11404 while (CONVERT_EXPR_P (exprv))
11405 exprv = TREE_OPERAND (exprv, 0);
11406 if (DECL_P (exprv)
11407 || handled_component_p (exprv)
11408 || TREE_CODE (exprv) == ADDR_EXPR)
11409 mark_exp_read (exprv);
11411 /* If the expression is not of a type to which we cannot assign a line
11412 number, wrap the thing in a no-op NOP_EXPR. */
11413 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11415 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11416 SET_EXPR_LOCATION (expr, loc);
11419 return expr;
11422 /* Emit an expression as a statement. LOC is the location of the
11423 expression. */
11425 tree
11426 c_finish_expr_stmt (location_t loc, tree expr)
11428 if (expr)
11429 return add_stmt (c_process_expr_stmt (loc, expr));
11430 else
11431 return NULL;
11434 /* Do the opposite and emit a statement as an expression. To begin,
11435 create a new binding level and return it. */
11437 tree
11438 c_begin_stmt_expr (void)
11440 tree ret;
11442 /* We must force a BLOCK for this level so that, if it is not expanded
11443 later, there is a way to turn off the entire subtree of blocks that
11444 are contained in it. */
11445 keep_next_level ();
11446 ret = c_begin_compound_stmt (true);
11448 c_bindings_start_stmt_expr (c_switch_stack == NULL
11449 ? NULL
11450 : c_switch_stack->bindings);
11452 /* Mark the current statement list as belonging to a statement list. */
11453 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11455 return ret;
11458 /* LOC is the location of the compound statement to which this body
11459 belongs. */
11461 tree
11462 c_finish_stmt_expr (location_t loc, tree body)
11464 tree last, type, tmp, val;
11465 tree *last_p;
11467 body = c_end_compound_stmt (loc, body, true);
11469 c_bindings_end_stmt_expr (c_switch_stack == NULL
11470 ? NULL
11471 : c_switch_stack->bindings);
11473 /* Locate the last statement in BODY. See c_end_compound_stmt
11474 about always returning a BIND_EXPR. */
11475 last_p = &BIND_EXPR_BODY (body);
11476 last = BIND_EXPR_BODY (body);
11478 continue_searching:
11479 if (TREE_CODE (last) == STATEMENT_LIST)
11481 tree_stmt_iterator l = tsi_last (last);
11483 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11484 tsi_prev (&l);
11486 /* This can happen with degenerate cases like ({ }). No value. */
11487 if (tsi_end_p (l))
11488 return body;
11490 /* If we're supposed to generate side effects warnings, process
11491 all of the statements except the last. */
11492 if (warn_unused_value || warn_unused_result)
11494 for (tree_stmt_iterator i = tsi_start (last);
11495 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11497 location_t tloc;
11498 tree t = tsi_stmt (i);
11500 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11501 emit_side_effect_warnings (tloc, t);
11504 last_p = tsi_stmt_ptr (l);
11505 last = *last_p;
11508 /* If the end of the list is exception related, then the list was split
11509 by a call to push_cleanup. Continue searching. */
11510 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11511 || TREE_CODE (last) == TRY_CATCH_EXPR)
11513 last_p = &TREE_OPERAND (last, 0);
11514 last = *last_p;
11515 goto continue_searching;
11518 if (last == error_mark_node)
11519 return last;
11521 /* In the case that the BIND_EXPR is not necessary, return the
11522 expression out from inside it. */
11523 if ((last == BIND_EXPR_BODY (body)
11524 /* Skip nested debug stmts. */
11525 || last == expr_first (BIND_EXPR_BODY (body)))
11526 && BIND_EXPR_VARS (body) == NULL)
11528 /* Even if this looks constant, do not allow it in a constant
11529 expression. */
11530 last = c_wrap_maybe_const (last, true);
11531 /* Do not warn if the return value of a statement expression is
11532 unused. */
11533 suppress_warning (last, OPT_Wunused);
11534 return last;
11537 /* Extract the type of said expression. */
11538 type = TREE_TYPE (last);
11540 /* If we're not returning a value at all, then the BIND_EXPR that
11541 we already have is a fine expression to return. */
11542 if (!type || VOID_TYPE_P (type))
11543 return body;
11545 /* Now that we've located the expression containing the value, it seems
11546 silly to make voidify_wrapper_expr repeat the process. Create a
11547 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11548 tmp = create_tmp_var_raw (type);
11550 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11551 tree_expr_nonnegative_p giving up immediately. */
11552 val = last;
11553 if (TREE_CODE (val) == NOP_EXPR
11554 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11555 val = TREE_OPERAND (val, 0);
11557 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11558 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11561 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11562 SET_EXPR_LOCATION (t, loc);
11563 return t;
11567 /* Begin and end compound statements. This is as simple as pushing
11568 and popping new statement lists from the tree. */
11570 tree
11571 c_begin_compound_stmt (bool do_scope)
11573 tree stmt = push_stmt_list ();
11574 if (do_scope)
11575 push_scope ();
11576 return stmt;
11579 /* End a compound statement. STMT is the statement. LOC is the
11580 location of the compound statement-- this is usually the location
11581 of the opening brace. */
11583 tree
11584 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11586 tree block = NULL;
11588 if (do_scope)
11590 if (c_dialect_objc ())
11591 objc_clear_super_receiver ();
11592 block = pop_scope ();
11595 stmt = pop_stmt_list (stmt);
11596 stmt = c_build_bind_expr (loc, block, stmt);
11598 /* If this compound statement is nested immediately inside a statement
11599 expression, then force a BIND_EXPR to be created. Otherwise we'll
11600 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11601 STATEMENT_LISTs merge, and thus we can lose track of what statement
11602 was really last. */
11603 if (building_stmt_list_p ()
11604 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11605 && TREE_CODE (stmt) != BIND_EXPR)
11607 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11608 TREE_SIDE_EFFECTS (stmt) = 1;
11609 SET_EXPR_LOCATION (stmt, loc);
11612 return stmt;
11615 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11616 when the current scope is exited. EH_ONLY is true when this is not
11617 meant to apply to normal control flow transfer. */
11619 void
11620 push_cleanup (tree decl, tree cleanup, bool eh_only)
11622 enum tree_code code;
11623 tree stmt, list;
11624 bool stmt_expr;
11626 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11627 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11628 add_stmt (stmt);
11629 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11630 list = push_stmt_list ();
11631 TREE_OPERAND (stmt, 0) = list;
11632 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11635 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11636 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11638 static tree
11639 build_vec_cmp (tree_code code, tree type,
11640 tree arg0, tree arg1)
11642 tree zero_vec = build_zero_cst (type);
11643 tree minus_one_vec = build_minus_one_cst (type);
11644 tree cmp_type = truth_type_for (type);
11645 tree cmp = build2 (code, cmp_type, arg0, arg1);
11646 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11649 /* Possibly warn about an address of OP never being NULL in a comparison
11650 operation CODE involving null. */
11652 static void
11653 maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
11655 /* Prevent warnings issued for macro expansion. */
11656 if (!warn_address
11657 || warning_suppressed_p (op, OPT_Waddress)
11658 || from_macro_expansion_at (loc))
11659 return;
11661 if (TREE_CODE (op) == NOP_EXPR)
11663 /* Allow casts to intptr_t to suppress the warning. */
11664 tree type = TREE_TYPE (op);
11665 if (TREE_CODE (type) == INTEGER_TYPE)
11666 return;
11667 op = TREE_OPERAND (op, 0);
11670 if (TREE_CODE (op) == POINTER_PLUS_EXPR)
11672 /* Allow a cast to void* to suppress the warning. */
11673 tree type = TREE_TYPE (TREE_TYPE (op));
11674 if (VOID_TYPE_P (type))
11675 return;
11677 /* Adding any value to a null pointer, including zero, is undefined
11678 in C. This includes the expression &p[0] where p is the null
11679 pointer, although &p[0] will have been folded to p by this point
11680 and so not diagnosed. */
11681 if (code == EQ_EXPR)
11682 warning_at (loc, OPT_Waddress,
11683 "the comparison will always evaluate as %<false%> "
11684 "for the pointer operand in %qE must not be NULL",
11685 op);
11686 else
11687 warning_at (loc, OPT_Waddress,
11688 "the comparison will always evaluate as %<true%> "
11689 "for the pointer operand in %qE must not be NULL",
11690 op);
11692 return;
11695 if (TREE_CODE (op) != ADDR_EXPR)
11696 return;
11698 op = TREE_OPERAND (op, 0);
11700 if (TREE_CODE (op) == IMAGPART_EXPR
11701 || TREE_CODE (op) == REALPART_EXPR)
11703 /* The address of either complex part may not be null. */
11704 if (code == EQ_EXPR)
11705 warning_at (loc, OPT_Waddress,
11706 "the comparison will always evaluate as %<false%> "
11707 "for the address of %qE will never be NULL",
11708 op);
11709 else
11710 warning_at (loc, OPT_Waddress,
11711 "the comparison will always evaluate as %<true%> "
11712 "for the address of %qE will never be NULL",
11713 op);
11714 return;
11717 /* Set to true in the loop below if OP dereferences is operand.
11718 In such a case the ultimate target need not be a decl for
11719 the null [in]equality test to be constant. */
11720 bool deref = false;
11722 /* Get the outermost array or object, or member. */
11723 while (handled_component_p (op))
11725 if (TREE_CODE (op) == COMPONENT_REF)
11727 /* Get the member (its address is never null). */
11728 op = TREE_OPERAND (op, 1);
11729 break;
11732 /* Get the outer array/object to refer to in the warning. */
11733 op = TREE_OPERAND (op, 0);
11734 deref = true;
11737 if ((!deref && !decl_with_nonnull_addr_p (op))
11738 || from_macro_expansion_at (loc))
11739 return;
11741 bool w;
11742 if (code == EQ_EXPR)
11743 w = warning_at (loc, OPT_Waddress,
11744 "the comparison will always evaluate as %<false%> "
11745 "for the address of %qE will never be NULL",
11746 op);
11747 else
11748 w = warning_at (loc, OPT_Waddress,
11749 "the comparison will always evaluate as %<true%> "
11750 "for the address of %qE will never be NULL",
11751 op);
11753 if (w && DECL_P (op))
11754 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
11757 /* Build a binary-operation expression without default conversions.
11758 CODE is the kind of expression to build.
11759 LOCATION is the operator's location.
11760 This function differs from `build' in several ways:
11761 the data type of the result is computed and recorded in it,
11762 warnings are generated if arg data types are invalid,
11763 special handling for addition and subtraction of pointers is known,
11764 and some optimization is done (operations on narrow ints
11765 are done in the narrower type when that gives the same result).
11766 Constant folding is also done before the result is returned.
11768 Note that the operands will never have enumeral types, or function
11769 or array types, because either they will have the default conversions
11770 performed or they have both just been converted to some other type in which
11771 the arithmetic is to be done. */
11773 tree
11774 build_binary_op (location_t location, enum tree_code code,
11775 tree orig_op0, tree orig_op1, bool convert_p)
11777 tree type0, type1, orig_type0, orig_type1;
11778 tree eptype;
11779 enum tree_code code0, code1;
11780 tree op0, op1;
11781 tree ret = error_mark_node;
11782 const char *invalid_op_diag;
11783 bool op0_int_operands, op1_int_operands;
11784 bool int_const, int_const_or_overflow, int_operands;
11786 /* Expression code to give to the expression when it is built.
11787 Normally this is CODE, which is what the caller asked for,
11788 but in some special cases we change it. */
11789 enum tree_code resultcode = code;
11791 /* Data type in which the computation is to be performed.
11792 In the simplest cases this is the common type of the arguments. */
11793 tree result_type = NULL;
11795 /* When the computation is in excess precision, the type of the
11796 final EXCESS_PRECISION_EXPR. */
11797 tree semantic_result_type = NULL;
11799 /* Nonzero means operands have already been type-converted
11800 in whatever way is necessary.
11801 Zero means they need to be converted to RESULT_TYPE. */
11802 int converted = 0;
11804 /* Nonzero means create the expression with this type, rather than
11805 RESULT_TYPE. */
11806 tree build_type = NULL_TREE;
11808 /* Nonzero means after finally constructing the expression
11809 convert it to this type. */
11810 tree final_type = NULL_TREE;
11812 /* Nonzero if this is an operation like MIN or MAX which can
11813 safely be computed in short if both args are promoted shorts.
11814 Also implies COMMON.
11815 -1 indicates a bitwise operation; this makes a difference
11816 in the exact conditions for when it is safe to do the operation
11817 in a narrower mode. */
11818 int shorten = 0;
11820 /* Nonzero if this is a comparison operation;
11821 if both args are promoted shorts, compare the original shorts.
11822 Also implies COMMON. */
11823 int short_compare = 0;
11825 /* Nonzero if this is a right-shift operation, which can be computed on the
11826 original short and then promoted if the operand is a promoted short. */
11827 int short_shift = 0;
11829 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11830 int common = 0;
11832 /* True means types are compatible as far as ObjC is concerned. */
11833 bool objc_ok;
11835 /* True means this is an arithmetic operation that may need excess
11836 precision. */
11837 bool may_need_excess_precision;
11839 /* True means this is a boolean operation that converts both its
11840 operands to truth-values. */
11841 bool boolean_op = false;
11843 /* Remember whether we're doing / or %. */
11844 bool doing_div_or_mod = false;
11846 /* Remember whether we're doing << or >>. */
11847 bool doing_shift = false;
11849 /* Tree holding instrumentation expression. */
11850 tree instrument_expr = NULL;
11852 if (location == UNKNOWN_LOCATION)
11853 location = input_location;
11855 op0 = orig_op0;
11856 op1 = orig_op1;
11858 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11859 if (op0_int_operands)
11860 op0 = remove_c_maybe_const_expr (op0);
11861 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11862 if (op1_int_operands)
11863 op1 = remove_c_maybe_const_expr (op1);
11864 int_operands = (op0_int_operands && op1_int_operands);
11865 if (int_operands)
11867 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11868 && TREE_CODE (orig_op1) == INTEGER_CST);
11869 int_const = (int_const_or_overflow
11870 && !TREE_OVERFLOW (orig_op0)
11871 && !TREE_OVERFLOW (orig_op1));
11873 else
11874 int_const = int_const_or_overflow = false;
11876 /* Do not apply default conversion in mixed vector/scalar expression. */
11877 if (convert_p
11878 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11880 op0 = default_conversion (op0);
11881 op1 = default_conversion (op1);
11884 orig_type0 = type0 = TREE_TYPE (op0);
11886 orig_type1 = type1 = TREE_TYPE (op1);
11888 /* The expression codes of the data types of the arguments tell us
11889 whether the arguments are integers, floating, pointers, etc. */
11890 code0 = TREE_CODE (type0);
11891 code1 = TREE_CODE (type1);
11893 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11894 STRIP_TYPE_NOPS (op0);
11895 STRIP_TYPE_NOPS (op1);
11897 /* If an error was already reported for one of the arguments,
11898 avoid reporting another error. */
11900 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11901 return error_mark_node;
11903 if (code0 == POINTER_TYPE
11904 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11905 return error_mark_node;
11907 if (code1 == POINTER_TYPE
11908 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11909 return error_mark_node;
11911 if ((invalid_op_diag
11912 = targetm.invalid_binary_op (code, type0, type1)))
11914 error_at (location, invalid_op_diag);
11915 return error_mark_node;
11918 switch (code)
11920 case PLUS_EXPR:
11921 case MINUS_EXPR:
11922 case MULT_EXPR:
11923 case TRUNC_DIV_EXPR:
11924 case CEIL_DIV_EXPR:
11925 case FLOOR_DIV_EXPR:
11926 case ROUND_DIV_EXPR:
11927 case EXACT_DIV_EXPR:
11928 may_need_excess_precision = true;
11929 break;
11931 case EQ_EXPR:
11932 case NE_EXPR:
11933 case LE_EXPR:
11934 case GE_EXPR:
11935 case LT_EXPR:
11936 case GT_EXPR:
11937 /* Excess precision for implicit conversions of integers to
11938 floating point in C11 and later. */
11939 may_need_excess_precision = (flag_isoc11
11940 && (ANY_INTEGRAL_TYPE_P (type0)
11941 || ANY_INTEGRAL_TYPE_P (type1)));
11942 break;
11944 default:
11945 may_need_excess_precision = false;
11946 break;
11948 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11950 op0 = TREE_OPERAND (op0, 0);
11951 type0 = TREE_TYPE (op0);
11953 else if (may_need_excess_precision
11954 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11956 type0 = eptype;
11957 op0 = convert (eptype, op0);
11959 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11961 op1 = TREE_OPERAND (op1, 0);
11962 type1 = TREE_TYPE (op1);
11964 else if (may_need_excess_precision
11965 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11967 type1 = eptype;
11968 op1 = convert (eptype, op1);
11971 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11973 /* In case when one of the operands of the binary operation is
11974 a vector and another is a scalar -- convert scalar to vector. */
11975 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
11976 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
11978 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11979 true);
11981 switch (convert_flag)
11983 case stv_error:
11984 return error_mark_node;
11985 case stv_firstarg:
11987 bool maybe_const = true;
11988 tree sc;
11989 sc = c_fully_fold (op0, false, &maybe_const);
11990 sc = save_expr (sc);
11991 sc = convert (TREE_TYPE (type1), sc);
11992 op0 = build_vector_from_val (type1, sc);
11993 if (!maybe_const)
11994 op0 = c_wrap_maybe_const (op0, true);
11995 orig_type0 = type0 = TREE_TYPE (op0);
11996 code0 = TREE_CODE (type0);
11997 converted = 1;
11998 break;
12000 case stv_secondarg:
12002 bool maybe_const = true;
12003 tree sc;
12004 sc = c_fully_fold (op1, false, &maybe_const);
12005 sc = save_expr (sc);
12006 sc = convert (TREE_TYPE (type0), sc);
12007 op1 = build_vector_from_val (type0, sc);
12008 if (!maybe_const)
12009 op1 = c_wrap_maybe_const (op1, true);
12010 orig_type1 = type1 = TREE_TYPE (op1);
12011 code1 = TREE_CODE (type1);
12012 converted = 1;
12013 break;
12015 default:
12016 break;
12020 switch (code)
12022 case PLUS_EXPR:
12023 /* Handle the pointer + int case. */
12024 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12026 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
12027 goto return_build_binary_op;
12029 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
12031 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
12032 goto return_build_binary_op;
12034 else
12035 common = 1;
12036 break;
12038 case MINUS_EXPR:
12039 /* Subtraction of two similar pointers.
12040 We must subtract them as integers, then divide by object size. */
12041 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
12042 && comp_target_types (location, type0, type1))
12044 ret = pointer_diff (location, op0, op1, &instrument_expr);
12045 goto return_build_binary_op;
12047 /* Handle pointer minus int. Just like pointer plus int. */
12048 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12050 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
12051 goto return_build_binary_op;
12053 else
12054 common = 1;
12055 break;
12057 case MULT_EXPR:
12058 common = 1;
12059 break;
12061 case TRUNC_DIV_EXPR:
12062 case CEIL_DIV_EXPR:
12063 case FLOOR_DIV_EXPR:
12064 case ROUND_DIV_EXPR:
12065 case EXACT_DIV_EXPR:
12066 doing_div_or_mod = true;
12067 warn_for_div_by_zero (location, op1);
12069 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12070 || code0 == FIXED_POINT_TYPE
12071 || code0 == COMPLEX_TYPE
12072 || gnu_vector_type_p (type0))
12073 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12074 || code1 == FIXED_POINT_TYPE
12075 || code1 == COMPLEX_TYPE
12076 || gnu_vector_type_p (type1)))
12078 enum tree_code tcode0 = code0, tcode1 = code1;
12080 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
12081 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
12082 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
12083 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
12085 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
12086 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
12087 resultcode = RDIV_EXPR;
12088 else
12089 /* Although it would be tempting to shorten always here, that
12090 loses on some targets, since the modulo instruction is
12091 undefined if the quotient can't be represented in the
12092 computation mode. We shorten only if unsigned or if
12093 dividing by something we know != -1. */
12094 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
12095 || (TREE_CODE (op1) == INTEGER_CST
12096 && !integer_all_onesp (op1)));
12097 common = 1;
12099 break;
12101 case BIT_AND_EXPR:
12102 case BIT_IOR_EXPR:
12103 case BIT_XOR_EXPR:
12104 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12105 shorten = -1;
12106 /* Allow vector types which are not floating point types. */
12107 else if (gnu_vector_type_p (type0)
12108 && gnu_vector_type_p (type1)
12109 && !VECTOR_FLOAT_TYPE_P (type0)
12110 && !VECTOR_FLOAT_TYPE_P (type1))
12111 common = 1;
12112 break;
12114 case TRUNC_MOD_EXPR:
12115 case FLOOR_MOD_EXPR:
12116 doing_div_or_mod = true;
12117 warn_for_div_by_zero (location, op1);
12119 if (gnu_vector_type_p (type0)
12120 && gnu_vector_type_p (type1)
12121 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12122 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12123 common = 1;
12124 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12126 /* Although it would be tempting to shorten always here, that loses
12127 on some targets, since the modulo instruction is undefined if the
12128 quotient can't be represented in the computation mode. We shorten
12129 only if unsigned or if dividing by something we know != -1. */
12130 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
12131 || (TREE_CODE (op1) == INTEGER_CST
12132 && !integer_all_onesp (op1)));
12133 common = 1;
12135 break;
12137 case TRUTH_ANDIF_EXPR:
12138 case TRUTH_ORIF_EXPR:
12139 case TRUTH_AND_EXPR:
12140 case TRUTH_OR_EXPR:
12141 case TRUTH_XOR_EXPR:
12142 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
12143 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12144 || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE)
12145 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
12146 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12147 || code1 == FIXED_POINT_TYPE || code1 == NULLPTR_TYPE))
12149 /* Result of these operations is always an int,
12150 but that does not mean the operands should be
12151 converted to ints! */
12152 result_type = integer_type_node;
12153 if (op0_int_operands)
12155 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
12156 op0 = remove_c_maybe_const_expr (op0);
12158 else
12159 op0 = c_objc_common_truthvalue_conversion (location, op0);
12160 if (op1_int_operands)
12162 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
12163 op1 = remove_c_maybe_const_expr (op1);
12165 else
12166 op1 = c_objc_common_truthvalue_conversion (location, op1);
12167 converted = 1;
12168 boolean_op = true;
12170 if (code == TRUTH_ANDIF_EXPR)
12172 int_const_or_overflow = (int_operands
12173 && TREE_CODE (orig_op0) == INTEGER_CST
12174 && (op0 == truthvalue_false_node
12175 || TREE_CODE (orig_op1) == INTEGER_CST));
12176 int_const = (int_const_or_overflow
12177 && !TREE_OVERFLOW (orig_op0)
12178 && (op0 == truthvalue_false_node
12179 || !TREE_OVERFLOW (orig_op1)));
12181 else if (code == TRUTH_ORIF_EXPR)
12183 int_const_or_overflow = (int_operands
12184 && TREE_CODE (orig_op0) == INTEGER_CST
12185 && (op0 == truthvalue_true_node
12186 || TREE_CODE (orig_op1) == INTEGER_CST));
12187 int_const = (int_const_or_overflow
12188 && !TREE_OVERFLOW (orig_op0)
12189 && (op0 == truthvalue_true_node
12190 || !TREE_OVERFLOW (orig_op1)));
12192 break;
12194 /* Shift operations: result has same type as first operand;
12195 always convert second operand to int.
12196 Also set SHORT_SHIFT if shifting rightward. */
12198 case RSHIFT_EXPR:
12199 if (gnu_vector_type_p (type0)
12200 && gnu_vector_type_p (type1)
12201 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12202 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12203 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12204 TYPE_VECTOR_SUBPARTS (type1)))
12206 result_type = type0;
12207 converted = 1;
12209 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12210 || (gnu_vector_type_p (type0)
12211 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12212 && code1 == INTEGER_TYPE)
12214 doing_shift = true;
12215 if (TREE_CODE (op1) == INTEGER_CST)
12217 if (tree_int_cst_sgn (op1) < 0)
12219 int_const = false;
12220 if (c_inhibit_evaluation_warnings == 0)
12221 warning_at (location, OPT_Wshift_count_negative,
12222 "right shift count is negative");
12224 else if (code0 == VECTOR_TYPE)
12226 if (compare_tree_int (op1,
12227 TYPE_PRECISION (TREE_TYPE (type0)))
12228 >= 0)
12230 int_const = false;
12231 if (c_inhibit_evaluation_warnings == 0)
12232 warning_at (location, OPT_Wshift_count_overflow,
12233 "right shift count >= width of vector element");
12236 else
12238 if (!integer_zerop (op1))
12239 short_shift = 1;
12241 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12243 int_const = false;
12244 if (c_inhibit_evaluation_warnings == 0)
12245 warning_at (location, OPT_Wshift_count_overflow,
12246 "right shift count >= width of type");
12251 /* Use the type of the value to be shifted. */
12252 result_type = type0;
12253 /* Avoid converting op1 to result_type later. */
12254 converted = 1;
12256 break;
12258 case LSHIFT_EXPR:
12259 if (gnu_vector_type_p (type0)
12260 && gnu_vector_type_p (type1)
12261 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12262 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12263 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12264 TYPE_VECTOR_SUBPARTS (type1)))
12266 result_type = type0;
12267 converted = 1;
12269 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12270 || (gnu_vector_type_p (type0)
12271 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12272 && code1 == INTEGER_TYPE)
12274 doing_shift = true;
12275 if (TREE_CODE (op0) == INTEGER_CST
12276 && tree_int_cst_sgn (op0) < 0
12277 && !TYPE_OVERFLOW_WRAPS (type0))
12279 /* Don't reject a left shift of a negative value in a context
12280 where a constant expression is needed in C90. */
12281 if (flag_isoc99)
12282 int_const = false;
12283 if (c_inhibit_evaluation_warnings == 0)
12284 warning_at (location, OPT_Wshift_negative_value,
12285 "left shift of negative value");
12287 if (TREE_CODE (op1) == INTEGER_CST)
12289 if (tree_int_cst_sgn (op1) < 0)
12291 int_const = false;
12292 if (c_inhibit_evaluation_warnings == 0)
12293 warning_at (location, OPT_Wshift_count_negative,
12294 "left shift count is negative");
12296 else if (code0 == VECTOR_TYPE)
12298 if (compare_tree_int (op1,
12299 TYPE_PRECISION (TREE_TYPE (type0)))
12300 >= 0)
12302 int_const = false;
12303 if (c_inhibit_evaluation_warnings == 0)
12304 warning_at (location, OPT_Wshift_count_overflow,
12305 "left shift count >= width of vector element");
12308 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12310 int_const = false;
12311 if (c_inhibit_evaluation_warnings == 0)
12312 warning_at (location, OPT_Wshift_count_overflow,
12313 "left shift count >= width of type");
12315 else if (TREE_CODE (op0) == INTEGER_CST
12316 && maybe_warn_shift_overflow (location, op0, op1)
12317 && flag_isoc99)
12318 int_const = false;
12321 /* Use the type of the value to be shifted. */
12322 result_type = type0;
12323 /* Avoid converting op1 to result_type later. */
12324 converted = 1;
12326 break;
12328 case EQ_EXPR:
12329 case NE_EXPR:
12330 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12332 tree intt;
12333 if (!vector_types_compatible_elements_p (type0, type1))
12335 error_at (location, "comparing vectors with different "
12336 "element types");
12337 return error_mark_node;
12340 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12341 TYPE_VECTOR_SUBPARTS (type1)))
12343 error_at (location, "comparing vectors with different "
12344 "number of elements");
12345 return error_mark_node;
12348 /* It's not precisely specified how the usual arithmetic
12349 conversions apply to the vector types. Here, we use
12350 the unsigned type if one of the operands is signed and
12351 the other one is unsigned. */
12352 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12354 if (!TYPE_UNSIGNED (type0))
12355 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12356 else
12357 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12358 warning_at (location, OPT_Wsign_compare, "comparison between "
12359 "types %qT and %qT", type0, type1);
12362 /* Always construct signed integer vector type. */
12363 intt = c_common_type_for_size (GET_MODE_BITSIZE
12364 (SCALAR_TYPE_MODE
12365 (TREE_TYPE (type0))), 0);
12366 if (!intt)
12368 error_at (location, "could not find an integer type "
12369 "of the same size as %qT",
12370 TREE_TYPE (type0));
12371 return error_mark_node;
12373 result_type = build_opaque_vector_type (intt,
12374 TYPE_VECTOR_SUBPARTS (type0));
12375 converted = 1;
12376 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12377 goto return_build_binary_op;
12379 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12380 warning_at (location,
12381 OPT_Wfloat_equal,
12382 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12383 /* Result of comparison is always int,
12384 but don't convert the args to int! */
12385 build_type = integer_type_node;
12386 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12387 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12388 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12389 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12390 short_compare = 1;
12391 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12393 maybe_warn_for_null_address (location, op0, code);
12394 result_type = type0;
12396 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12398 maybe_warn_for_null_address (location, op1, code);
12399 result_type = type1;
12401 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12403 tree tt0 = TREE_TYPE (type0);
12404 tree tt1 = TREE_TYPE (type1);
12405 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12406 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12407 addr_space_t as_common = ADDR_SPACE_GENERIC;
12409 /* Anything compares with void *. void * compares with anything.
12410 Otherwise, the targets must be compatible
12411 and both must be object or both incomplete. */
12412 if (comp_target_types (location, type0, type1))
12413 result_type = common_pointer_type (type0, type1);
12414 else if (!addr_space_superset (as0, as1, &as_common))
12416 error_at (location, "comparison of pointers to "
12417 "disjoint address spaces");
12418 return error_mark_node;
12420 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12422 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12423 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12424 "comparison of %<void *%> with function pointer");
12426 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12428 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12429 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12430 "comparison of %<void *%> with function pointer");
12432 else
12433 /* Avoid warning about the volatile ObjC EH puts on decls. */
12434 if (!objc_ok)
12435 pedwarn (location, 0,
12436 "comparison of distinct pointer types lacks a cast");
12438 if (result_type == NULL_TREE)
12440 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12441 result_type = build_pointer_type
12442 (build_qualified_type (void_type_node, qual));
12445 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12447 result_type = type0;
12448 pedwarn (location, 0, "comparison between pointer and integer");
12450 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12452 result_type = type1;
12453 pedwarn (location, 0, "comparison between pointer and integer");
12455 /* 6.5.9: One of the following shall hold:
12456 -- both operands have type nullptr_t; */
12457 else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE)
12459 result_type = nullptr_type_node;
12460 /* No need to convert the operands to result_type later. */
12461 converted = 1;
12463 /* -- one operand has type nullptr_t and the other is a null pointer
12464 constant. We will have to convert the former to the type of the
12465 latter, because during gimplification we can't have mismatching
12466 comparison operand type. We convert from nullptr_t to the other
12467 type, since only nullptr_t can be converted to nullptr_t. Also,
12468 even a constant 0 is a null pointer constant, so we may have to
12469 create a pointer type from its type. */
12470 else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1))
12471 result_type = (INTEGRAL_TYPE_P (type1)
12472 ? build_pointer_type (type1) : type1);
12473 else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0))
12474 result_type = (INTEGRAL_TYPE_P (type0)
12475 ? build_pointer_type (type0) : type0);
12476 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12477 || truth_value_p (TREE_CODE (orig_op0)))
12478 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12479 || truth_value_p (TREE_CODE (orig_op1))))
12480 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12481 break;
12483 case LE_EXPR:
12484 case GE_EXPR:
12485 case LT_EXPR:
12486 case GT_EXPR:
12487 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12489 tree intt;
12490 if (!vector_types_compatible_elements_p (type0, type1))
12492 error_at (location, "comparing vectors with different "
12493 "element types");
12494 return error_mark_node;
12497 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12498 TYPE_VECTOR_SUBPARTS (type1)))
12500 error_at (location, "comparing vectors with different "
12501 "number of elements");
12502 return error_mark_node;
12505 /* It's not precisely specified how the usual arithmetic
12506 conversions apply to the vector types. Here, we use
12507 the unsigned type if one of the operands is signed and
12508 the other one is unsigned. */
12509 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12511 if (!TYPE_UNSIGNED (type0))
12512 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12513 else
12514 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12515 warning_at (location, OPT_Wsign_compare, "comparison between "
12516 "types %qT and %qT", type0, type1);
12519 /* Always construct signed integer vector type. */
12520 intt = c_common_type_for_size (GET_MODE_BITSIZE
12521 (SCALAR_TYPE_MODE
12522 (TREE_TYPE (type0))), 0);
12523 if (!intt)
12525 error_at (location, "could not find an integer type "
12526 "of the same size as %qT",
12527 TREE_TYPE (type0));
12528 return error_mark_node;
12530 result_type = build_opaque_vector_type (intt,
12531 TYPE_VECTOR_SUBPARTS (type0));
12532 converted = 1;
12533 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12534 goto return_build_binary_op;
12536 build_type = integer_type_node;
12537 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12538 || code0 == FIXED_POINT_TYPE)
12539 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12540 || code1 == FIXED_POINT_TYPE))
12541 short_compare = 1;
12542 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12544 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12545 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12546 addr_space_t as_common;
12548 if (comp_target_types (location, type0, type1))
12550 result_type = common_pointer_type (type0, type1);
12551 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12552 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12553 pedwarn_c99 (location, OPT_Wpedantic,
12554 "comparison of complete and incomplete pointers");
12555 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12556 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12557 "ordered comparisons of pointers to functions");
12558 else if (null_pointer_constant_p (orig_op0)
12559 || null_pointer_constant_p (orig_op1))
12560 warning_at (location, OPT_Wextra,
12561 "ordered comparison of pointer with null pointer");
12564 else if (!addr_space_superset (as0, as1, &as_common))
12566 error_at (location, "comparison of pointers to "
12567 "disjoint address spaces");
12568 return error_mark_node;
12570 else
12572 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12573 result_type = build_pointer_type
12574 (build_qualified_type (void_type_node, qual));
12575 pedwarn (location, 0,
12576 "comparison of distinct pointer types lacks a cast");
12579 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12581 result_type = type0;
12582 if (pedantic)
12583 pedwarn (location, OPT_Wpedantic,
12584 "ordered comparison of pointer with integer zero");
12585 else if (extra_warnings)
12586 warning_at (location, OPT_Wextra,
12587 "ordered comparison of pointer with integer zero");
12589 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12591 result_type = type1;
12592 if (pedantic)
12593 pedwarn (location, OPT_Wpedantic,
12594 "ordered comparison of pointer with integer zero");
12595 else if (extra_warnings)
12596 warning_at (location, OPT_Wextra,
12597 "ordered comparison of pointer with integer zero");
12599 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12601 result_type = type0;
12602 pedwarn (location, 0, "comparison between pointer and integer");
12604 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12606 result_type = type1;
12607 pedwarn (location, 0, "comparison between pointer and integer");
12610 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12611 && current_function_decl != NULL_TREE
12612 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12614 op0 = save_expr (op0);
12615 op1 = save_expr (op1);
12617 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12618 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12621 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12622 || truth_value_p (TREE_CODE (orig_op0)))
12623 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12624 || truth_value_p (TREE_CODE (orig_op1))))
12625 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12626 break;
12628 case MIN_EXPR:
12629 case MAX_EXPR:
12630 /* Used for OpenMP atomics. */
12631 gcc_assert (flag_openmp);
12632 common = 1;
12633 break;
12635 default:
12636 gcc_unreachable ();
12639 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12640 return error_mark_node;
12642 if (gnu_vector_type_p (type0)
12643 && gnu_vector_type_p (type1)
12644 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12645 || !vector_types_compatible_elements_p (type0, type1)))
12647 gcc_rich_location richloc (location);
12648 maybe_range_label_for_tree_type_mismatch
12649 label_for_op0 (orig_op0, orig_op1),
12650 label_for_op1 (orig_op1, orig_op0);
12651 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12652 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12653 binary_op_error (&richloc, code, type0, type1);
12654 return error_mark_node;
12657 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12658 || code0 == FIXED_POINT_TYPE
12659 || gnu_vector_type_p (type0))
12661 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12662 || code1 == FIXED_POINT_TYPE
12663 || gnu_vector_type_p (type1)))
12665 bool first_complex = (code0 == COMPLEX_TYPE);
12666 bool second_complex = (code1 == COMPLEX_TYPE);
12667 int none_complex = (!first_complex && !second_complex);
12669 if (shorten || common || short_compare)
12671 result_type = c_common_type (type0, type1);
12672 do_warn_double_promotion (result_type, type0, type1,
12673 "implicit conversion from %qT to %qT "
12674 "to match other operand of binary "
12675 "expression",
12676 location);
12677 if (result_type == error_mark_node)
12678 return error_mark_node;
12681 if (first_complex != second_complex
12682 && (code == PLUS_EXPR
12683 || code == MINUS_EXPR
12684 || code == MULT_EXPR
12685 || (code == TRUNC_DIV_EXPR && first_complex))
12686 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12687 && flag_signed_zeros)
12689 /* An operation on mixed real/complex operands must be
12690 handled specially, but the language-independent code can
12691 more easily optimize the plain complex arithmetic if
12692 -fno-signed-zeros. */
12693 tree real_type = TREE_TYPE (result_type);
12694 tree real, imag;
12695 if (type0 != orig_type0 || type1 != orig_type1)
12697 gcc_assert (may_need_excess_precision && common);
12698 semantic_result_type = c_common_type (orig_type0, orig_type1);
12700 if (first_complex)
12702 if (TREE_TYPE (op0) != result_type)
12703 op0 = convert_and_check (location, result_type, op0);
12704 if (TREE_TYPE (op1) != real_type)
12705 op1 = convert_and_check (location, real_type, op1);
12707 else
12709 if (TREE_TYPE (op0) != real_type)
12710 op0 = convert_and_check (location, real_type, op0);
12711 if (TREE_TYPE (op1) != result_type)
12712 op1 = convert_and_check (location, result_type, op1);
12714 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12715 return error_mark_node;
12716 if (first_complex)
12718 op0 = save_expr (op0);
12719 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12720 op0, true);
12721 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12722 op0, true);
12723 switch (code)
12725 case MULT_EXPR:
12726 case TRUNC_DIV_EXPR:
12727 op1 = save_expr (op1);
12728 imag = build2 (resultcode, real_type, imag, op1);
12729 /* Fall through. */
12730 case PLUS_EXPR:
12731 case MINUS_EXPR:
12732 real = build2 (resultcode, real_type, real, op1);
12733 break;
12734 default:
12735 gcc_unreachable();
12738 else
12740 op1 = save_expr (op1);
12741 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12742 op1, true);
12743 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12744 op1, true);
12745 switch (code)
12747 case MULT_EXPR:
12748 op0 = save_expr (op0);
12749 imag = build2 (resultcode, real_type, op0, imag);
12750 /* Fall through. */
12751 case PLUS_EXPR:
12752 real = build2 (resultcode, real_type, op0, real);
12753 break;
12754 case MINUS_EXPR:
12755 real = build2 (resultcode, real_type, op0, real);
12756 imag = build1 (NEGATE_EXPR, real_type, imag);
12757 break;
12758 default:
12759 gcc_unreachable();
12762 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12763 goto return_build_binary_op;
12766 /* For certain operations (which identify themselves by shorten != 0)
12767 if both args were extended from the same smaller type,
12768 do the arithmetic in that type and then extend.
12770 shorten !=0 and !=1 indicates a bitwise operation.
12771 For them, this optimization is safe only if
12772 both args are zero-extended or both are sign-extended.
12773 Otherwise, we might change the result.
12774 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12775 but calculated in (unsigned short) it would be (unsigned short)-1. */
12777 if (shorten && none_complex)
12779 final_type = result_type;
12780 result_type = shorten_binary_op (result_type, op0, op1,
12781 shorten == -1);
12784 /* Shifts can be shortened if shifting right. */
12786 if (short_shift)
12788 int unsigned_arg;
12789 tree arg0 = get_narrower (op0, &unsigned_arg);
12791 final_type = result_type;
12793 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12794 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12796 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12797 && tree_int_cst_sgn (op1) > 0
12798 /* We can shorten only if the shift count is less than the
12799 number of bits in the smaller type size. */
12800 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12801 /* We cannot drop an unsigned shift after sign-extension. */
12802 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12804 /* Do an unsigned shift if the operand was zero-extended. */
12805 result_type
12806 = c_common_signed_or_unsigned_type (unsigned_arg,
12807 TREE_TYPE (arg0));
12808 /* Convert value-to-be-shifted to that type. */
12809 if (TREE_TYPE (op0) != result_type)
12810 op0 = convert (result_type, op0);
12811 converted = 1;
12815 /* Comparison operations are shortened too but differently.
12816 They identify themselves by setting short_compare = 1. */
12818 if (short_compare)
12820 /* Don't write &op0, etc., because that would prevent op0
12821 from being kept in a register.
12822 Instead, make copies of the our local variables and
12823 pass the copies by reference, then copy them back afterward. */
12824 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12825 enum tree_code xresultcode = resultcode;
12826 tree val
12827 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12828 &xresultcode);
12830 if (val != NULL_TREE)
12832 ret = val;
12833 goto return_build_binary_op;
12836 op0 = xop0, op1 = xop1;
12837 converted = 1;
12838 resultcode = xresultcode;
12840 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
12842 bool op0_maybe_const = true;
12843 bool op1_maybe_const = true;
12844 tree orig_op0_folded, orig_op1_folded;
12846 if (in_late_binary_op)
12848 orig_op0_folded = orig_op0;
12849 orig_op1_folded = orig_op1;
12851 else
12853 /* Fold for the sake of possible warnings, as in
12854 build_conditional_expr. This requires the
12855 "original" values to be folded, not just op0 and
12856 op1. */
12857 c_inhibit_evaluation_warnings++;
12858 op0 = c_fully_fold (op0, require_constant_value,
12859 &op0_maybe_const);
12860 op1 = c_fully_fold (op1, require_constant_value,
12861 &op1_maybe_const);
12862 c_inhibit_evaluation_warnings--;
12863 orig_op0_folded = c_fully_fold (orig_op0,
12864 require_constant_value,
12865 NULL);
12866 orig_op1_folded = c_fully_fold (orig_op1,
12867 require_constant_value,
12868 NULL);
12871 if (warn_sign_compare)
12872 warn_for_sign_compare (location, orig_op0_folded,
12873 orig_op1_folded, op0, op1,
12874 result_type, resultcode);
12875 if (!in_late_binary_op && !int_operands)
12877 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12878 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12879 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12880 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12886 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12887 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12888 Then the expression will be built.
12889 It will be given type FINAL_TYPE if that is nonzero;
12890 otherwise, it will be given type RESULT_TYPE. */
12892 if (!result_type)
12894 /* Favor showing any expression locations that are available. */
12895 op_location_t oploc (location, UNKNOWN_LOCATION);
12896 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12897 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12898 return error_mark_node;
12901 if (build_type == NULL_TREE)
12903 build_type = result_type;
12904 if ((type0 != orig_type0 || type1 != orig_type1)
12905 && !boolean_op)
12907 gcc_assert (may_need_excess_precision && common);
12908 semantic_result_type = c_common_type (orig_type0, orig_type1);
12912 if (!converted)
12914 op0 = ep_convert_and_check (location, result_type, op0,
12915 semantic_result_type);
12916 op1 = ep_convert_and_check (location, result_type, op1,
12917 semantic_result_type);
12919 /* This can happen if one operand has a vector type, and the other
12920 has a different type. */
12921 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12922 return error_mark_node;
12925 if (sanitize_flags_p ((SANITIZE_SHIFT
12926 | SANITIZE_DIVIDE
12927 | SANITIZE_FLOAT_DIVIDE
12928 | SANITIZE_SI_OVERFLOW))
12929 && current_function_decl != NULL_TREE
12930 && (doing_div_or_mod || doing_shift)
12931 && !require_constant_value)
12933 /* OP0 and/or OP1 might have side-effects. */
12934 op0 = save_expr (op0);
12935 op1 = save_expr (op1);
12936 op0 = c_fully_fold (op0, false, NULL);
12937 op1 = c_fully_fold (op1, false, NULL);
12938 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12939 | SANITIZE_FLOAT_DIVIDE
12940 | SANITIZE_SI_OVERFLOW))))
12941 instrument_expr = ubsan_instrument_division (location, op0, op1);
12942 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12943 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12946 /* Treat expressions in initializers specially as they can't trap. */
12947 if (int_const_or_overflow)
12948 ret = (require_constant_value
12949 ? fold_build2_initializer_loc (location, resultcode, build_type,
12950 op0, op1)
12951 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12952 else
12953 ret = build2 (resultcode, build_type, op0, op1);
12954 if (final_type != NULL_TREE)
12955 ret = convert (final_type, ret);
12957 return_build_binary_op:
12958 gcc_assert (ret != error_mark_node);
12959 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12960 ret = (int_operands
12961 ? note_integer_operands (ret)
12962 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12963 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12964 && !in_late_binary_op)
12965 ret = note_integer_operands (ret);
12966 protected_set_expr_location (ret, location);
12968 if (instrument_expr != NULL)
12969 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12970 instrument_expr, ret);
12972 if (semantic_result_type)
12973 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12974 semantic_result_type, ret);
12976 return ret;
12980 /* Convert EXPR to be a truth-value, validating its type for this
12981 purpose. LOCATION is the source location for the expression. */
12983 tree
12984 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12986 bool int_const, int_operands;
12988 switch (TREE_CODE (TREE_TYPE (expr)))
12990 case ARRAY_TYPE:
12991 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12992 return error_mark_node;
12994 case RECORD_TYPE:
12995 error_at (location, "used struct type value where scalar is required");
12996 return error_mark_node;
12998 case UNION_TYPE:
12999 error_at (location, "used union type value where scalar is required");
13000 return error_mark_node;
13002 case VOID_TYPE:
13003 error_at (location, "void value not ignored as it ought to be");
13004 return error_mark_node;
13006 case POINTER_TYPE:
13007 if (reject_gcc_builtin (expr))
13008 return error_mark_node;
13009 break;
13011 case FUNCTION_TYPE:
13012 gcc_unreachable ();
13014 case VECTOR_TYPE:
13015 error_at (location, "used vector type where scalar is required");
13016 return error_mark_node;
13018 default:
13019 break;
13022 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
13023 int_operands = EXPR_INT_CONST_OPERANDS (expr);
13024 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
13026 expr = remove_c_maybe_const_expr (expr);
13027 expr = build2 (NE_EXPR, integer_type_node, expr,
13028 convert (TREE_TYPE (expr), integer_zero_node));
13029 expr = note_integer_operands (expr);
13031 else
13032 /* ??? Should we also give an error for vectors rather than leaving
13033 those to give errors later? */
13034 expr = c_common_truthvalue_conversion (location, expr);
13036 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
13038 if (TREE_OVERFLOW (expr))
13039 return expr;
13040 else
13041 return note_integer_operands (expr);
13043 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
13044 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
13045 return expr;
13049 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
13050 required. */
13052 tree
13053 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
13055 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
13057 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
13058 /* Executing a compound literal inside a function reinitializes
13059 it. */
13060 if (!TREE_STATIC (decl))
13061 *se = true;
13062 return decl;
13064 else
13065 return expr;
13068 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
13069 statement. LOC is the location of the construct. */
13071 tree
13072 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
13073 tree clauses)
13075 body = c_end_compound_stmt (loc, body, true);
13077 tree stmt = make_node (code);
13078 TREE_TYPE (stmt) = void_type_node;
13079 OMP_BODY (stmt) = body;
13080 OMP_CLAUSES (stmt) = clauses;
13081 SET_EXPR_LOCATION (stmt, loc);
13083 return add_stmt (stmt);
13086 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
13087 statement. LOC is the location of the OACC_DATA. */
13089 tree
13090 c_finish_oacc_data (location_t loc, tree clauses, tree block)
13092 tree stmt;
13094 block = c_end_compound_stmt (loc, block, true);
13096 stmt = make_node (OACC_DATA);
13097 TREE_TYPE (stmt) = void_type_node;
13098 OACC_DATA_CLAUSES (stmt) = clauses;
13099 OACC_DATA_BODY (stmt) = block;
13100 SET_EXPR_LOCATION (stmt, loc);
13102 return add_stmt (stmt);
13105 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
13106 statement. LOC is the location of the OACC_HOST_DATA. */
13108 tree
13109 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
13111 tree stmt;
13113 block = c_end_compound_stmt (loc, block, true);
13115 stmt = make_node (OACC_HOST_DATA);
13116 TREE_TYPE (stmt) = void_type_node;
13117 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
13118 OACC_HOST_DATA_BODY (stmt) = block;
13119 SET_EXPR_LOCATION (stmt, loc);
13121 return add_stmt (stmt);
13124 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13126 tree
13127 c_begin_omp_parallel (void)
13129 tree block;
13131 keep_next_level ();
13132 block = c_begin_compound_stmt (true);
13134 return block;
13137 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
13138 statement. LOC is the location of the OMP_PARALLEL. */
13140 tree
13141 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
13143 tree stmt;
13145 block = c_end_compound_stmt (loc, block, true);
13147 stmt = make_node (OMP_PARALLEL);
13148 TREE_TYPE (stmt) = void_type_node;
13149 OMP_PARALLEL_CLAUSES (stmt) = clauses;
13150 OMP_PARALLEL_BODY (stmt) = block;
13151 SET_EXPR_LOCATION (stmt, loc);
13153 return add_stmt (stmt);
13156 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13158 tree
13159 c_begin_omp_task (void)
13161 tree block;
13163 keep_next_level ();
13164 block = c_begin_compound_stmt (true);
13166 return block;
13169 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
13170 statement. LOC is the location of the #pragma. */
13172 tree
13173 c_finish_omp_task (location_t loc, tree clauses, tree block)
13175 tree stmt;
13177 block = c_end_compound_stmt (loc, block, true);
13179 stmt = make_node (OMP_TASK);
13180 TREE_TYPE (stmt) = void_type_node;
13181 OMP_TASK_CLAUSES (stmt) = clauses;
13182 OMP_TASK_BODY (stmt) = block;
13183 SET_EXPR_LOCATION (stmt, loc);
13185 return add_stmt (stmt);
13188 /* Generate GOMP_cancel call for #pragma omp cancel. */
13190 void
13191 c_finish_omp_cancel (location_t loc, tree clauses)
13193 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
13194 int mask = 0;
13195 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13196 mask = 1;
13197 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13198 mask = 2;
13199 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13200 mask = 4;
13201 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13202 mask = 8;
13203 else
13205 error_at (loc, "%<#pragma omp cancel%> must specify one of "
13206 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13207 "clauses");
13208 return;
13210 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
13211 if (ifc != NULL_TREE)
13213 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
13214 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
13215 error_at (OMP_CLAUSE_LOCATION (ifc),
13216 "expected %<cancel%> %<if%> clause modifier");
13217 else
13219 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
13220 if (ifc2 != NULL_TREE)
13222 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
13223 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13224 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13225 error_at (OMP_CLAUSE_LOCATION (ifc2),
13226 "expected %<cancel%> %<if%> clause modifier");
13230 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13231 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13232 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13233 build_zero_cst (type));
13235 else
13236 ifc = boolean_true_node;
13237 tree stmt = build_call_expr_loc (loc, fn, 2,
13238 build_int_cst (integer_type_node, mask),
13239 ifc);
13240 add_stmt (stmt);
13243 /* Generate GOMP_cancellation_point call for
13244 #pragma omp cancellation point. */
13246 void
13247 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13249 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13250 int mask = 0;
13251 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13252 mask = 1;
13253 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13254 mask = 2;
13255 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13256 mask = 4;
13257 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13258 mask = 8;
13259 else
13261 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13262 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13263 "clauses");
13264 return;
13266 tree stmt = build_call_expr_loc (loc, fn, 1,
13267 build_int_cst (integer_type_node, mask));
13268 add_stmt (stmt);
13271 /* Helper function for handle_omp_array_sections. Called recursively
13272 to handle multiple array-section-subscripts. C is the clause,
13273 T current expression (initially OMP_CLAUSE_DECL), which is either
13274 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13275 expression if specified, TREE_VALUE length expression if specified,
13276 TREE_CHAIN is what it has been specified after, or some decl.
13277 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13278 set to true if any of the array-section-subscript could have length
13279 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13280 first array-section-subscript which is known not to have length
13281 of one. Given say:
13282 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13283 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13284 all are or may have length of 1, array-section-subscript [:2] is the
13285 first one known not to have length 1. For array-section-subscript
13286 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13287 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13288 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
13289 case though, as some lengths could be zero. */
13291 static tree
13292 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13293 bool &maybe_zero_len, unsigned int &first_non_one,
13294 enum c_omp_region_type ort)
13296 tree ret, low_bound, length, type;
13297 if (TREE_CODE (t) != TREE_LIST)
13299 if (error_operand_p (t))
13300 return error_mark_node;
13301 ret = t;
13302 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13303 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13304 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13306 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13307 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13308 return error_mark_node;
13310 while (TREE_CODE (t) == INDIRECT_REF)
13312 t = TREE_OPERAND (t, 0);
13313 STRIP_NOPS (t);
13314 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13315 t = TREE_OPERAND (t, 0);
13317 while (TREE_CODE (t) == COMPOUND_EXPR)
13319 t = TREE_OPERAND (t, 1);
13320 STRIP_NOPS (t);
13322 if (TREE_CODE (t) == COMPONENT_REF
13323 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13324 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13325 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13327 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13329 error_at (OMP_CLAUSE_LOCATION (c),
13330 "bit-field %qE in %qs clause",
13331 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13332 return error_mark_node;
13334 while (TREE_CODE (t) == COMPONENT_REF)
13336 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13338 error_at (OMP_CLAUSE_LOCATION (c),
13339 "%qE is a member of a union", t);
13340 return error_mark_node;
13342 t = TREE_OPERAND (t, 0);
13343 while (TREE_CODE (t) == MEM_REF
13344 || TREE_CODE (t) == INDIRECT_REF
13345 || TREE_CODE (t) == ARRAY_REF)
13347 t = TREE_OPERAND (t, 0);
13348 STRIP_NOPS (t);
13349 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13350 t = TREE_OPERAND (t, 0);
13352 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13354 if (maybe_ne (mem_ref_offset (t), 0))
13355 error_at (OMP_CLAUSE_LOCATION (c),
13356 "cannot dereference %qE in %qs clause", t,
13357 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13358 else
13359 t = TREE_OPERAND (t, 0);
13363 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13365 if (DECL_P (t))
13366 error_at (OMP_CLAUSE_LOCATION (c),
13367 "%qD is not a variable in %qs clause", t,
13368 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13369 else
13370 error_at (OMP_CLAUSE_LOCATION (c),
13371 "%qE is not a variable in %qs clause", t,
13372 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13373 return error_mark_node;
13375 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13376 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13377 && TYPE_ATOMIC (TREE_TYPE (t)))
13379 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13380 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13381 return error_mark_node;
13383 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13384 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13385 && VAR_P (t)
13386 && DECL_THREAD_LOCAL_P (t))
13388 error_at (OMP_CLAUSE_LOCATION (c),
13389 "%qD is threadprivate variable in %qs clause", t,
13390 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13391 return error_mark_node;
13393 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13394 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13395 && TYPE_ATOMIC (TREE_TYPE (t))
13396 && POINTER_TYPE_P (TREE_TYPE (t)))
13398 /* If the array section is pointer based and the pointer
13399 itself is _Atomic qualified, we need to atomically load
13400 the pointer. */
13401 c_expr expr;
13402 memset (&expr, 0, sizeof (expr));
13403 expr.value = ret;
13404 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13405 expr, false, false);
13406 ret = expr.value;
13408 return ret;
13411 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13412 maybe_zero_len, first_non_one, ort);
13413 if (ret == error_mark_node || ret == NULL_TREE)
13414 return ret;
13416 type = TREE_TYPE (ret);
13417 low_bound = TREE_PURPOSE (t);
13418 length = TREE_VALUE (t);
13420 if (low_bound == error_mark_node || length == error_mark_node)
13421 return error_mark_node;
13423 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13425 error_at (OMP_CLAUSE_LOCATION (c),
13426 "low bound %qE of array section does not have integral type",
13427 low_bound);
13428 return error_mark_node;
13430 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13432 error_at (OMP_CLAUSE_LOCATION (c),
13433 "length %qE of array section does not have integral type",
13434 length);
13435 return error_mark_node;
13437 if (low_bound
13438 && TREE_CODE (low_bound) == INTEGER_CST
13439 && TYPE_PRECISION (TREE_TYPE (low_bound))
13440 > TYPE_PRECISION (sizetype))
13441 low_bound = fold_convert (sizetype, low_bound);
13442 if (length
13443 && TREE_CODE (length) == INTEGER_CST
13444 && TYPE_PRECISION (TREE_TYPE (length))
13445 > TYPE_PRECISION (sizetype))
13446 length = fold_convert (sizetype, length);
13447 if (low_bound == NULL_TREE)
13448 low_bound = integer_zero_node;
13449 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13450 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13451 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13453 if (length != integer_one_node)
13455 error_at (OMP_CLAUSE_LOCATION (c),
13456 "expected single pointer in %qs clause",
13457 user_omp_clause_code_name (c, ort == C_ORT_ACC));
13458 return error_mark_node;
13461 if (length != NULL_TREE)
13463 if (!integer_nonzerop (length))
13465 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13466 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13467 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13468 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13469 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13471 if (integer_zerop (length))
13473 error_at (OMP_CLAUSE_LOCATION (c),
13474 "zero length array section in %qs clause",
13475 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13476 return error_mark_node;
13479 else
13480 maybe_zero_len = true;
13482 if (first_non_one == types.length ()
13483 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13484 first_non_one++;
13486 if (TREE_CODE (type) == ARRAY_TYPE)
13488 if (length == NULL_TREE
13489 && (TYPE_DOMAIN (type) == NULL_TREE
13490 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13492 error_at (OMP_CLAUSE_LOCATION (c),
13493 "for unknown bound array type length expression must "
13494 "be specified");
13495 return error_mark_node;
13497 if (TREE_CODE (low_bound) == INTEGER_CST
13498 && tree_int_cst_sgn (low_bound) == -1)
13500 error_at (OMP_CLAUSE_LOCATION (c),
13501 "negative low bound in array section in %qs clause",
13502 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13503 return error_mark_node;
13505 if (length != NULL_TREE
13506 && TREE_CODE (length) == INTEGER_CST
13507 && tree_int_cst_sgn (length) == -1)
13509 error_at (OMP_CLAUSE_LOCATION (c),
13510 "negative length in array section in %qs clause",
13511 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13512 return error_mark_node;
13514 if (TYPE_DOMAIN (type)
13515 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13516 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13517 == INTEGER_CST)
13519 tree size
13520 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13521 size = size_binop (PLUS_EXPR, size, size_one_node);
13522 if (TREE_CODE (low_bound) == INTEGER_CST)
13524 if (tree_int_cst_lt (size, low_bound))
13526 error_at (OMP_CLAUSE_LOCATION (c),
13527 "low bound %qE above array section size "
13528 "in %qs clause", low_bound,
13529 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13530 return error_mark_node;
13532 if (tree_int_cst_equal (size, low_bound))
13534 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13535 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13536 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13537 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13538 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13540 error_at (OMP_CLAUSE_LOCATION (c),
13541 "zero length array section in %qs clause",
13542 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13543 return error_mark_node;
13545 maybe_zero_len = true;
13547 else if (length == NULL_TREE
13548 && first_non_one == types.length ()
13549 && tree_int_cst_equal
13550 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13551 low_bound))
13552 first_non_one++;
13554 else if (length == NULL_TREE)
13556 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13557 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13558 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13559 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13560 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13561 maybe_zero_len = true;
13562 if (first_non_one == types.length ())
13563 first_non_one++;
13565 if (length && TREE_CODE (length) == INTEGER_CST)
13567 if (tree_int_cst_lt (size, length))
13569 error_at (OMP_CLAUSE_LOCATION (c),
13570 "length %qE above array section size "
13571 "in %qs clause", length,
13572 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13573 return error_mark_node;
13575 if (TREE_CODE (low_bound) == INTEGER_CST)
13577 tree lbpluslen
13578 = size_binop (PLUS_EXPR,
13579 fold_convert (sizetype, low_bound),
13580 fold_convert (sizetype, length));
13581 if (TREE_CODE (lbpluslen) == INTEGER_CST
13582 && tree_int_cst_lt (size, lbpluslen))
13584 error_at (OMP_CLAUSE_LOCATION (c),
13585 "high bound %qE above array section size "
13586 "in %qs clause", lbpluslen,
13587 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13588 return error_mark_node;
13593 else if (length == NULL_TREE)
13595 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13596 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13597 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13598 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13599 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13600 maybe_zero_len = true;
13601 if (first_non_one == types.length ())
13602 first_non_one++;
13605 /* For [lb:] we will need to evaluate lb more than once. */
13606 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13608 tree lb = save_expr (low_bound);
13609 if (lb != low_bound)
13611 TREE_PURPOSE (t) = lb;
13612 low_bound = lb;
13616 else if (TREE_CODE (type) == POINTER_TYPE)
13618 if (length == NULL_TREE)
13620 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
13621 error_at (OMP_CLAUSE_LOCATION (c),
13622 "for array function parameter length expression "
13623 "must be specified");
13624 else
13625 error_at (OMP_CLAUSE_LOCATION (c),
13626 "for pointer type length expression must be specified");
13627 return error_mark_node;
13629 if (length != NULL_TREE
13630 && TREE_CODE (length) == INTEGER_CST
13631 && tree_int_cst_sgn (length) == -1)
13633 error_at (OMP_CLAUSE_LOCATION (c),
13634 "negative length in array section in %qs clause",
13635 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13636 return error_mark_node;
13638 /* If there is a pointer type anywhere but in the very first
13639 array-section-subscript, the array section could be non-contiguous. */
13640 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13641 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13642 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13644 /* If any prior dimension has a non-one length, then deem this
13645 array section as non-contiguous. */
13646 for (tree d = TREE_CHAIN (t); TREE_CODE (d) == TREE_LIST;
13647 d = TREE_CHAIN (d))
13649 tree d_length = TREE_VALUE (d);
13650 if (d_length == NULL_TREE || !integer_onep (d_length))
13652 error_at (OMP_CLAUSE_LOCATION (c),
13653 "array section is not contiguous in %qs clause",
13654 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13655 return error_mark_node;
13660 else
13662 error_at (OMP_CLAUSE_LOCATION (c),
13663 "%qE does not have pointer or array type", ret);
13664 return error_mark_node;
13666 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13667 types.safe_push (TREE_TYPE (ret));
13668 /* We will need to evaluate lb more than once. */
13669 tree lb = save_expr (low_bound);
13670 if (lb != low_bound)
13672 TREE_PURPOSE (t) = lb;
13673 low_bound = lb;
13675 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13676 return ret;
13679 /* Handle array sections for clause C. */
13681 static bool
13682 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13684 bool maybe_zero_len = false;
13685 unsigned int first_non_one = 0;
13686 auto_vec<tree, 10> types;
13687 tree *tp = &OMP_CLAUSE_DECL (c);
13688 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13689 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13690 && TREE_CODE (*tp) == TREE_LIST
13691 && TREE_PURPOSE (*tp)
13692 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13693 tp = &TREE_VALUE (*tp);
13694 tree first = handle_omp_array_sections_1 (c, *tp, types,
13695 maybe_zero_len, first_non_one,
13696 ort);
13697 if (first == error_mark_node)
13698 return true;
13699 if (first == NULL_TREE)
13700 return false;
13701 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13702 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13704 tree t = *tp;
13705 tree tem = NULL_TREE;
13706 /* Need to evaluate side effects in the length expressions
13707 if any. */
13708 while (TREE_CODE (t) == TREE_LIST)
13710 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13712 if (tem == NULL_TREE)
13713 tem = TREE_VALUE (t);
13714 else
13715 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13716 TREE_VALUE (t), tem);
13718 t = TREE_CHAIN (t);
13720 if (tem)
13721 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13722 first = c_fully_fold (first, false, NULL, true);
13723 *tp = first;
13725 else
13727 unsigned int num = types.length (), i;
13728 tree t, side_effects = NULL_TREE, size = NULL_TREE;
13729 tree condition = NULL_TREE;
13731 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13732 maybe_zero_len = true;
13734 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13735 t = TREE_CHAIN (t))
13737 tree low_bound = TREE_PURPOSE (t);
13738 tree length = TREE_VALUE (t);
13740 i--;
13741 if (low_bound
13742 && TREE_CODE (low_bound) == INTEGER_CST
13743 && TYPE_PRECISION (TREE_TYPE (low_bound))
13744 > TYPE_PRECISION (sizetype))
13745 low_bound = fold_convert (sizetype, low_bound);
13746 if (length
13747 && TREE_CODE (length) == INTEGER_CST
13748 && TYPE_PRECISION (TREE_TYPE (length))
13749 > TYPE_PRECISION (sizetype))
13750 length = fold_convert (sizetype, length);
13751 if (low_bound == NULL_TREE)
13752 low_bound = integer_zero_node;
13753 if (!maybe_zero_len && i > first_non_one)
13755 if (integer_nonzerop (low_bound))
13756 goto do_warn_noncontiguous;
13757 if (length != NULL_TREE
13758 && TREE_CODE (length) == INTEGER_CST
13759 && TYPE_DOMAIN (types[i])
13760 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13761 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13762 == INTEGER_CST)
13764 tree size;
13765 size = size_binop (PLUS_EXPR,
13766 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13767 size_one_node);
13768 if (!tree_int_cst_equal (length, size))
13770 do_warn_noncontiguous:
13771 error_at (OMP_CLAUSE_LOCATION (c),
13772 "array section is not contiguous in %qs "
13773 "clause",
13774 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13775 return true;
13778 if (length != NULL_TREE
13779 && TREE_SIDE_EFFECTS (length))
13781 if (side_effects == NULL_TREE)
13782 side_effects = length;
13783 else
13784 side_effects = build2 (COMPOUND_EXPR,
13785 TREE_TYPE (side_effects),
13786 length, side_effects);
13789 else
13791 tree l;
13793 if (i > first_non_one
13794 && ((length && integer_nonzerop (length))
13795 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13796 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13797 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13798 continue;
13799 if (length)
13800 l = fold_convert (sizetype, length);
13801 else
13803 l = size_binop (PLUS_EXPR,
13804 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13805 size_one_node);
13806 l = size_binop (MINUS_EXPR, l,
13807 fold_convert (sizetype, low_bound));
13809 if (i > first_non_one)
13811 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13812 size_zero_node);
13813 if (condition == NULL_TREE)
13814 condition = l;
13815 else
13816 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13817 l, condition);
13819 else if (size == NULL_TREE)
13821 size = size_in_bytes (TREE_TYPE (types[i]));
13822 tree eltype = TREE_TYPE (types[num - 1]);
13823 while (TREE_CODE (eltype) == ARRAY_TYPE)
13824 eltype = TREE_TYPE (eltype);
13825 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13826 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13827 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13829 if (integer_zerop (size)
13830 || integer_zerop (size_in_bytes (eltype)))
13832 error_at (OMP_CLAUSE_LOCATION (c),
13833 "zero length array section in %qs clause",
13834 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13835 return error_mark_node;
13837 size = size_binop (EXACT_DIV_EXPR, size,
13838 size_in_bytes (eltype));
13840 size = size_binop (MULT_EXPR, size, l);
13841 if (condition)
13842 size = fold_build3 (COND_EXPR, sizetype, condition,
13843 size, size_zero_node);
13845 else
13846 size = size_binop (MULT_EXPR, size, l);
13849 if (side_effects)
13850 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13851 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13852 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13853 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13855 size = size_binop (MINUS_EXPR, size, size_one_node);
13856 size = c_fully_fold (size, false, NULL);
13857 size = save_expr (size);
13858 tree index_type = build_index_type (size);
13859 tree eltype = TREE_TYPE (first);
13860 while (TREE_CODE (eltype) == ARRAY_TYPE)
13861 eltype = TREE_TYPE (eltype);
13862 tree type = build_array_type (eltype, index_type);
13863 tree ptype = build_pointer_type (eltype);
13864 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13865 t = build_fold_addr_expr (t);
13866 tree t2 = build_fold_addr_expr (first);
13867 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13868 ptrdiff_type_node, t2);
13869 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13870 ptrdiff_type_node, t2,
13871 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13872 ptrdiff_type_node, t));
13873 t2 = c_fully_fold (t2, false, NULL);
13874 if (tree_fits_shwi_p (t2))
13875 t = build2 (MEM_REF, type, t,
13876 build_int_cst (ptype, tree_to_shwi (t2)));
13877 else
13879 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13880 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13881 TREE_TYPE (t), t, t2);
13882 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13884 OMP_CLAUSE_DECL (c) = t;
13885 return false;
13887 first = c_fully_fold (first, false, NULL);
13888 OMP_CLAUSE_DECL (c) = first;
13889 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
13890 return false;
13891 if (size)
13892 size = c_fully_fold (size, false, NULL);
13893 OMP_CLAUSE_SIZE (c) = size;
13894 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13895 || (TREE_CODE (t) == COMPONENT_REF
13896 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13897 return false;
13898 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13899 switch (OMP_CLAUSE_MAP_KIND (c))
13901 case GOMP_MAP_ALLOC:
13902 case GOMP_MAP_IF_PRESENT:
13903 case GOMP_MAP_TO:
13904 case GOMP_MAP_FROM:
13905 case GOMP_MAP_TOFROM:
13906 case GOMP_MAP_ALWAYS_TO:
13907 case GOMP_MAP_ALWAYS_FROM:
13908 case GOMP_MAP_ALWAYS_TOFROM:
13909 case GOMP_MAP_RELEASE:
13910 case GOMP_MAP_DELETE:
13911 case GOMP_MAP_FORCE_TO:
13912 case GOMP_MAP_FORCE_FROM:
13913 case GOMP_MAP_FORCE_TOFROM:
13914 case GOMP_MAP_FORCE_PRESENT:
13915 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13916 break;
13917 default:
13918 break;
13920 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13921 if (TREE_CODE (t) == COMPONENT_REF)
13922 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
13923 else
13924 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13925 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
13926 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13927 && !c_mark_addressable (t))
13928 return false;
13929 OMP_CLAUSE_DECL (c2) = t;
13930 t = build_fold_addr_expr (first);
13931 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13932 tree ptr = OMP_CLAUSE_DECL (c2);
13933 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13934 ptr = build_fold_addr_expr (ptr);
13935 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13936 ptrdiff_type_node, t,
13937 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13938 ptrdiff_type_node, ptr));
13939 t = c_fully_fold (t, false, NULL);
13940 OMP_CLAUSE_SIZE (c2) = t;
13941 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13942 OMP_CLAUSE_CHAIN (c) = c2;
13944 return false;
13947 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13948 an inline call. But, remap
13949 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13950 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13952 static tree
13953 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13954 tree decl, tree placeholder)
13956 copy_body_data id;
13957 hash_map<tree, tree> decl_map;
13959 decl_map.put (omp_decl1, placeholder);
13960 decl_map.put (omp_decl2, decl);
13961 memset (&id, 0, sizeof (id));
13962 id.src_fn = DECL_CONTEXT (omp_decl1);
13963 id.dst_fn = current_function_decl;
13964 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13965 id.decl_map = &decl_map;
13967 id.copy_decl = copy_decl_no_change;
13968 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13969 id.transform_new_cfg = true;
13970 id.transform_return_to_modify = false;
13971 id.eh_lp_nr = 0;
13972 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13973 return stmt;
13976 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13977 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13979 static tree
13980 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13982 if (*tp == (tree) data)
13983 return *tp;
13984 return NULL_TREE;
13987 /* Similarly, but also walk aggregate fields. */
13989 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13991 static tree
13992 c_find_omp_var_r (tree *tp, int *, void *data)
13994 if (*tp == ((struct c_find_omp_var_s *) data)->var)
13995 return *tp;
13996 if (RECORD_OR_UNION_TYPE_P (*tp))
13998 tree field;
13999 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
14001 for (field = TYPE_FIELDS (*tp); field;
14002 field = DECL_CHAIN (field))
14003 if (TREE_CODE (field) == FIELD_DECL)
14005 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
14006 c_find_omp_var_r, data, pset);
14007 if (ret)
14008 return ret;
14009 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
14010 if (ret)
14011 return ret;
14012 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
14013 pset);
14014 if (ret)
14015 return ret;
14016 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
14017 if (ret)
14018 return ret;
14021 else if (INTEGRAL_TYPE_P (*tp))
14022 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
14023 ((struct c_find_omp_var_s *) data)->pset);
14024 return NULL_TREE;
14027 /* Finish OpenMP iterators ITER. Return true if they are errorneous
14028 and clauses containing them should be removed. */
14030 static bool
14031 c_omp_finish_iterators (tree iter)
14033 bool ret = false;
14034 for (tree it = iter; it; it = TREE_CHAIN (it))
14036 tree var = TREE_VEC_ELT (it, 0);
14037 tree begin = TREE_VEC_ELT (it, 1);
14038 tree end = TREE_VEC_ELT (it, 2);
14039 tree step = TREE_VEC_ELT (it, 3);
14040 tree orig_step;
14041 tree type = TREE_TYPE (var);
14042 location_t loc = DECL_SOURCE_LOCATION (var);
14043 if (type == error_mark_node)
14045 ret = true;
14046 continue;
14048 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14050 error_at (loc, "iterator %qD has neither integral nor pointer type",
14051 var);
14052 ret = true;
14053 continue;
14055 else if (TYPE_ATOMIC (type))
14057 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
14058 ret = true;
14059 continue;
14061 else if (TYPE_READONLY (type))
14063 error_at (loc, "iterator %qD has const qualified type", var);
14064 ret = true;
14065 continue;
14067 else if (step == error_mark_node
14068 || TREE_TYPE (step) == error_mark_node)
14070 ret = true;
14071 continue;
14073 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
14075 error_at (EXPR_LOC_OR_LOC (step, loc),
14076 "iterator step with non-integral type");
14077 ret = true;
14078 continue;
14080 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
14081 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
14082 orig_step = save_expr (c_fully_fold (step, false, NULL));
14083 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
14084 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
14085 if (POINTER_TYPE_P (type))
14087 begin = save_expr (begin);
14088 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
14089 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
14090 fold_convert (sizetype, step),
14091 fold_convert (sizetype, begin));
14092 step = fold_convert (ssizetype, step);
14094 if (integer_zerop (step))
14096 error_at (loc, "iterator %qD has zero step", var);
14097 ret = true;
14098 continue;
14101 if (begin == error_mark_node
14102 || end == error_mark_node
14103 || step == error_mark_node
14104 || orig_step == error_mark_node)
14106 ret = true;
14107 continue;
14109 hash_set<tree> pset;
14110 tree it2;
14111 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
14113 tree var2 = TREE_VEC_ELT (it2, 0);
14114 tree begin2 = TREE_VEC_ELT (it2, 1);
14115 tree end2 = TREE_VEC_ELT (it2, 2);
14116 tree step2 = TREE_VEC_ELT (it2, 3);
14117 tree type2 = TREE_TYPE (var2);
14118 location_t loc2 = DECL_SOURCE_LOCATION (var2);
14119 struct c_find_omp_var_s data = { var, &pset };
14120 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
14122 error_at (loc2,
14123 "type of iterator %qD refers to outer iterator %qD",
14124 var2, var);
14125 break;
14127 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
14129 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
14130 "begin expression refers to outer iterator %qD", var);
14131 break;
14133 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
14135 error_at (EXPR_LOC_OR_LOC (end2, loc2),
14136 "end expression refers to outer iterator %qD", var);
14137 break;
14139 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
14141 error_at (EXPR_LOC_OR_LOC (step2, loc2),
14142 "step expression refers to outer iterator %qD", var);
14143 break;
14146 if (it2)
14148 ret = true;
14149 continue;
14151 TREE_VEC_ELT (it, 1) = begin;
14152 TREE_VEC_ELT (it, 2) = end;
14153 TREE_VEC_ELT (it, 3) = step;
14154 TREE_VEC_ELT (it, 4) = orig_step;
14156 return ret;
14159 /* Ensure that pointers are used in OpenACC attach and detach clauses.
14160 Return true if an error has been detected. */
14162 static bool
14163 c_oacc_check_attachments (tree c)
14165 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14166 return false;
14168 /* OpenACC attach / detach clauses must be pointers. */
14169 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14170 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14172 tree t = OMP_CLAUSE_DECL (c);
14174 while (TREE_CODE (t) == TREE_LIST)
14175 t = TREE_CHAIN (t);
14177 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14179 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
14180 user_omp_clause_code_name (c, true));
14181 return true;
14185 return false;
14188 /* For all elements of CLAUSES, validate them against their constraints.
14189 Remove any elements from the list that are invalid. */
14191 tree
14192 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
14194 bitmap_head generic_head, firstprivate_head, lastprivate_head;
14195 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
14196 bitmap_head oacc_reduction_head, is_on_device_head;
14197 tree c, t, type, *pc;
14198 tree simdlen = NULL_TREE, safelen = NULL_TREE;
14199 bool branch_seen = false;
14200 bool copyprivate_seen = false;
14201 bool mergeable_seen = false;
14202 tree *detach_seen = NULL;
14203 bool linear_variable_step_check = false;
14204 tree *nowait_clause = NULL;
14205 tree ordered_clause = NULL_TREE;
14206 tree schedule_clause = NULL_TREE;
14207 bool oacc_async = false;
14208 bool indir_component_ref_p = false;
14209 tree last_iterators = NULL_TREE;
14210 bool last_iterators_remove = false;
14211 tree *nogroup_seen = NULL;
14212 tree *order_clause = NULL;
14213 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
14214 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
14215 int reduction_seen = 0;
14216 bool allocate_seen = false;
14217 bool implicit_moved = false;
14218 bool target_in_reduction_seen = false;
14220 bitmap_obstack_initialize (NULL);
14221 bitmap_initialize (&generic_head, &bitmap_default_obstack);
14222 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
14223 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
14224 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
14225 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
14226 bitmap_initialize (&map_head, &bitmap_default_obstack);
14227 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
14228 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
14229 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
14230 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
14231 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
14232 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
14234 if (ort & C_ORT_ACC)
14235 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14236 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
14238 oacc_async = true;
14239 break;
14242 tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
14244 for (pc = &clauses, c = clauses; c ; c = *pc)
14246 bool remove = false;
14247 bool need_complete = false;
14248 bool need_implicitly_determined = false;
14250 /* We've reached the end of a list of expanded nodes. Reset the group
14251 start pointer. */
14252 if (c == grp_sentinel)
14253 grp_start_p = NULL;
14255 switch (OMP_CLAUSE_CODE (c))
14257 case OMP_CLAUSE_SHARED:
14258 need_implicitly_determined = true;
14259 goto check_dup_generic;
14261 case OMP_CLAUSE_PRIVATE:
14262 need_complete = true;
14263 need_implicitly_determined = true;
14264 goto check_dup_generic;
14266 case OMP_CLAUSE_REDUCTION:
14267 if (reduction_seen == 0)
14268 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14269 else if (reduction_seen != -2
14270 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14271 ? -1 : 1))
14273 error_at (OMP_CLAUSE_LOCATION (c),
14274 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14275 "on the same construct");
14276 reduction_seen = -2;
14278 /* FALLTHRU */
14279 case OMP_CLAUSE_IN_REDUCTION:
14280 case OMP_CLAUSE_TASK_REDUCTION:
14281 need_implicitly_determined = true;
14282 t = OMP_CLAUSE_DECL (c);
14283 if (TREE_CODE (t) == TREE_LIST)
14285 if (handle_omp_array_sections (c, ort))
14287 remove = true;
14288 break;
14291 t = OMP_CLAUSE_DECL (c);
14292 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14293 && OMP_CLAUSE_REDUCTION_INSCAN (c))
14295 error_at (OMP_CLAUSE_LOCATION (c),
14296 "%<inscan%> %<reduction%> clause with array "
14297 "section");
14298 remove = true;
14299 break;
14302 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14303 if (t == error_mark_node)
14305 remove = true;
14306 break;
14308 if (oacc_async)
14309 c_mark_addressable (t);
14310 type = TREE_TYPE (t);
14311 if (TREE_CODE (t) == MEM_REF)
14312 type = TREE_TYPE (type);
14313 if (TREE_CODE (type) == ARRAY_TYPE)
14315 tree oatype = type;
14316 gcc_assert (TREE_CODE (t) != MEM_REF);
14317 while (TREE_CODE (type) == ARRAY_TYPE)
14318 type = TREE_TYPE (type);
14319 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14321 error_at (OMP_CLAUSE_LOCATION (c),
14322 "%qD in %<reduction%> clause is a zero size array",
14324 remove = true;
14325 break;
14327 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14328 TYPE_SIZE_UNIT (type));
14329 if (integer_zerop (size))
14331 error_at (OMP_CLAUSE_LOCATION (c),
14332 "%qD in %<reduction%> clause is a zero size array",
14334 remove = true;
14335 break;
14337 size = size_binop (MINUS_EXPR, size, size_one_node);
14338 size = save_expr (size);
14339 tree index_type = build_index_type (size);
14340 tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14341 index_type);
14342 atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14343 tree ptype = build_pointer_type (type);
14344 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14345 t = build_fold_addr_expr (t);
14346 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14347 OMP_CLAUSE_DECL (c) = t;
14349 if (TYPE_ATOMIC (type))
14351 error_at (OMP_CLAUSE_LOCATION (c),
14352 "%<_Atomic%> %qE in %<reduction%> clause", t);
14353 remove = true;
14354 break;
14356 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14357 || OMP_CLAUSE_REDUCTION_TASK (c))
14359 /* Disallow zero sized or potentially zero sized task
14360 reductions. */
14361 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14363 error_at (OMP_CLAUSE_LOCATION (c),
14364 "zero sized type %qT in %qs clause", type,
14365 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14366 remove = true;
14367 break;
14369 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14371 error_at (OMP_CLAUSE_LOCATION (c),
14372 "variable sized type %qT in %qs clause", type,
14373 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14374 remove = true;
14375 break;
14378 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14379 && (FLOAT_TYPE_P (type)
14380 || TREE_CODE (type) == COMPLEX_TYPE))
14382 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14383 const char *r_name = NULL;
14385 switch (r_code)
14387 case PLUS_EXPR:
14388 case MULT_EXPR:
14389 case MINUS_EXPR:
14390 case TRUTH_ANDIF_EXPR:
14391 case TRUTH_ORIF_EXPR:
14392 break;
14393 case MIN_EXPR:
14394 if (TREE_CODE (type) == COMPLEX_TYPE)
14395 r_name = "min";
14396 break;
14397 case MAX_EXPR:
14398 if (TREE_CODE (type) == COMPLEX_TYPE)
14399 r_name = "max";
14400 break;
14401 case BIT_AND_EXPR:
14402 r_name = "&";
14403 break;
14404 case BIT_XOR_EXPR:
14405 r_name = "^";
14406 break;
14407 case BIT_IOR_EXPR:
14408 r_name = "|";
14409 break;
14410 default:
14411 gcc_unreachable ();
14413 if (r_name)
14415 error_at (OMP_CLAUSE_LOCATION (c),
14416 "%qE has invalid type for %<reduction(%s)%>",
14417 t, r_name);
14418 remove = true;
14419 break;
14422 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14424 error_at (OMP_CLAUSE_LOCATION (c),
14425 "user defined reduction not found for %qE", t);
14426 remove = true;
14427 break;
14429 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14431 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14432 type = TYPE_MAIN_VARIANT (type);
14433 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14434 VAR_DECL, NULL_TREE, type);
14435 tree decl_placeholder = NULL_TREE;
14436 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14437 DECL_ARTIFICIAL (placeholder) = 1;
14438 DECL_IGNORED_P (placeholder) = 1;
14439 if (TREE_CODE (t) == MEM_REF)
14441 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14442 VAR_DECL, NULL_TREE, type);
14443 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14444 DECL_ARTIFICIAL (decl_placeholder) = 1;
14445 DECL_IGNORED_P (decl_placeholder) = 1;
14447 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14448 c_mark_addressable (placeholder);
14449 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14450 c_mark_addressable (decl_placeholder ? decl_placeholder
14451 : OMP_CLAUSE_DECL (c));
14452 OMP_CLAUSE_REDUCTION_MERGE (c)
14453 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14454 TREE_VEC_ELT (list, 0),
14455 TREE_VEC_ELT (list, 1),
14456 decl_placeholder ? decl_placeholder
14457 : OMP_CLAUSE_DECL (c), placeholder);
14458 OMP_CLAUSE_REDUCTION_MERGE (c)
14459 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14460 void_type_node, NULL_TREE,
14461 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14462 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14463 if (TREE_VEC_LENGTH (list) == 6)
14465 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14466 c_mark_addressable (decl_placeholder ? decl_placeholder
14467 : OMP_CLAUSE_DECL (c));
14468 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14469 c_mark_addressable (placeholder);
14470 tree init = TREE_VEC_ELT (list, 5);
14471 if (init == error_mark_node)
14472 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14473 OMP_CLAUSE_REDUCTION_INIT (c)
14474 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14475 TREE_VEC_ELT (list, 3),
14476 decl_placeholder ? decl_placeholder
14477 : OMP_CLAUSE_DECL (c), placeholder);
14478 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14480 tree v = decl_placeholder ? decl_placeholder : t;
14481 OMP_CLAUSE_REDUCTION_INIT (c)
14482 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14483 OMP_CLAUSE_REDUCTION_INIT (c));
14485 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14486 c_find_omp_placeholder_r,
14487 placeholder, NULL))
14488 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14490 else
14492 tree init;
14493 tree v = decl_placeholder ? decl_placeholder : t;
14494 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14495 init = build_constructor (TREE_TYPE (v), NULL);
14496 else
14497 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14498 OMP_CLAUSE_REDUCTION_INIT (c)
14499 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14501 OMP_CLAUSE_REDUCTION_INIT (c)
14502 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14503 void_type_node, NULL_TREE,
14504 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14505 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14507 if (TREE_CODE (t) == MEM_REF)
14509 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14510 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14511 != INTEGER_CST)
14513 sorry ("variable length element type in array "
14514 "%<reduction%> clause");
14515 remove = true;
14516 break;
14518 t = TREE_OPERAND (t, 0);
14519 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14520 t = TREE_OPERAND (t, 0);
14521 if (TREE_CODE (t) == ADDR_EXPR)
14522 t = TREE_OPERAND (t, 0);
14524 goto check_dup_generic_t;
14526 case OMP_CLAUSE_COPYPRIVATE:
14527 copyprivate_seen = true;
14528 if (nowait_clause)
14530 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14531 "%<nowait%> clause must not be used together "
14532 "with %<copyprivate%>");
14533 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14534 nowait_clause = NULL;
14536 goto check_dup_generic;
14538 case OMP_CLAUSE_COPYIN:
14539 t = OMP_CLAUSE_DECL (c);
14540 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14542 error_at (OMP_CLAUSE_LOCATION (c),
14543 "%qE must be %<threadprivate%> for %<copyin%>", t);
14544 remove = true;
14545 break;
14547 goto check_dup_generic;
14549 case OMP_CLAUSE_LINEAR:
14550 if (ort != C_ORT_OMP_DECLARE_SIMD)
14551 need_implicitly_determined = true;
14552 t = OMP_CLAUSE_DECL (c);
14553 if (ort != C_ORT_OMP_DECLARE_SIMD
14554 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
14555 && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
14557 error_at (OMP_CLAUSE_LOCATION (c),
14558 "modifier should not be specified in %<linear%> "
14559 "clause on %<simd%> or %<for%> constructs when not "
14560 "using OpenMP 5.2 modifiers");
14561 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14563 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14564 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14566 error_at (OMP_CLAUSE_LOCATION (c),
14567 "linear clause applied to non-integral non-pointer "
14568 "variable with type %qT", TREE_TYPE (t));
14569 remove = true;
14570 break;
14572 if (TYPE_ATOMIC (TREE_TYPE (t)))
14574 error_at (OMP_CLAUSE_LOCATION (c),
14575 "%<_Atomic%> %qD in %<linear%> clause", t);
14576 remove = true;
14577 break;
14579 if (ort == C_ORT_OMP_DECLARE_SIMD)
14581 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14582 if (TREE_CODE (s) == PARM_DECL)
14584 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14585 /* map_head bitmap is used as uniform_head if
14586 declare_simd. */
14587 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14588 linear_variable_step_check = true;
14589 goto check_dup_generic;
14591 if (TREE_CODE (s) != INTEGER_CST)
14593 error_at (OMP_CLAUSE_LOCATION (c),
14594 "%<linear%> clause step %qE is neither constant "
14595 "nor a parameter", s);
14596 remove = true;
14597 break;
14600 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14602 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14603 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14604 OMP_CLAUSE_DECL (c), s);
14605 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14606 sizetype, fold_convert (sizetype, s),
14607 fold_convert
14608 (sizetype, OMP_CLAUSE_DECL (c)));
14609 if (s == error_mark_node)
14610 s = size_one_node;
14611 OMP_CLAUSE_LINEAR_STEP (c) = s;
14613 else
14614 OMP_CLAUSE_LINEAR_STEP (c)
14615 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14616 goto check_dup_generic;
14618 check_dup_generic:
14619 t = OMP_CLAUSE_DECL (c);
14620 check_dup_generic_t:
14621 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14623 error_at (OMP_CLAUSE_LOCATION (c),
14624 "%qE is not a variable in clause %qs", t,
14625 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14626 remove = true;
14628 else if ((ort == C_ORT_ACC
14629 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14630 || (ort == C_ORT_OMP
14631 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14632 || (OMP_CLAUSE_CODE (c)
14633 == OMP_CLAUSE_USE_DEVICE_ADDR)))
14634 || (ort == C_ORT_OMP_TARGET
14635 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
14637 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14638 && (bitmap_bit_p (&generic_head, DECL_UID (t))
14639 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
14641 error_at (OMP_CLAUSE_LOCATION (c),
14642 "%qD appears more than once in data-sharing "
14643 "clauses", t);
14644 remove = true;
14645 break;
14647 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
14648 target_in_reduction_seen = true;
14649 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14651 error_at (OMP_CLAUSE_LOCATION (c),
14652 ort == C_ORT_ACC
14653 ? "%qD appears more than once in reduction clauses"
14654 : "%qD appears more than once in data clauses",
14656 remove = true;
14658 else
14659 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14661 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14662 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14663 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
14664 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14666 error_at (OMP_CLAUSE_LOCATION (c),
14667 "%qE appears more than once in data clauses", t);
14668 remove = true;
14670 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14671 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
14672 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
14673 && bitmap_bit_p (&map_head, DECL_UID (t)))
14675 if (ort == C_ORT_ACC)
14676 error_at (OMP_CLAUSE_LOCATION (c),
14677 "%qD appears more than once in data clauses", t);
14678 else
14679 error_at (OMP_CLAUSE_LOCATION (c),
14680 "%qD appears both in data and map clauses", t);
14681 remove = true;
14683 else
14684 bitmap_set_bit (&generic_head, DECL_UID (t));
14685 break;
14687 case OMP_CLAUSE_FIRSTPRIVATE:
14688 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
14690 move_implicit:
14691 implicit_moved = true;
14692 /* Move firstprivate and map clauses with
14693 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
14694 clauses chain. */
14695 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
14696 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
14697 while (*pc1)
14698 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
14699 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
14701 *pc3 = *pc1;
14702 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
14703 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14705 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
14706 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
14708 *pc2 = *pc1;
14709 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
14710 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14712 else
14713 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
14714 *pc3 = NULL;
14715 *pc2 = cl2;
14716 *pc1 = cl1;
14717 continue;
14719 t = OMP_CLAUSE_DECL (c);
14720 need_complete = true;
14721 need_implicitly_determined = true;
14722 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14724 error_at (OMP_CLAUSE_LOCATION (c),
14725 "%qE is not a variable in clause %<firstprivate%>", t);
14726 remove = true;
14728 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14729 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
14730 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14731 remove = true;
14732 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14733 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14734 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14736 error_at (OMP_CLAUSE_LOCATION (c),
14737 "%qE appears more than once in data clauses", t);
14738 remove = true;
14740 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14742 if (ort == C_ORT_ACC)
14743 error_at (OMP_CLAUSE_LOCATION (c),
14744 "%qD appears more than once in data clauses", t);
14745 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14746 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
14747 /* Silently drop the clause. */;
14748 else
14749 error_at (OMP_CLAUSE_LOCATION (c),
14750 "%qD appears both in data and map clauses", t);
14751 remove = true;
14753 else
14754 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14755 break;
14757 case OMP_CLAUSE_LASTPRIVATE:
14758 t = OMP_CLAUSE_DECL (c);
14759 need_complete = true;
14760 need_implicitly_determined = true;
14761 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14763 error_at (OMP_CLAUSE_LOCATION (c),
14764 "%qE is not a variable in clause %<lastprivate%>", t);
14765 remove = true;
14767 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14768 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14770 error_at (OMP_CLAUSE_LOCATION (c),
14771 "%qE appears more than once in data clauses", t);
14772 remove = true;
14774 else
14775 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14776 break;
14778 case OMP_CLAUSE_ALIGNED:
14779 t = OMP_CLAUSE_DECL (c);
14780 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14782 error_at (OMP_CLAUSE_LOCATION (c),
14783 "%qE is not a variable in %<aligned%> clause", t);
14784 remove = true;
14786 else if (!POINTER_TYPE_P (TREE_TYPE (t))
14787 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14789 error_at (OMP_CLAUSE_LOCATION (c),
14790 "%qE in %<aligned%> clause is neither a pointer nor "
14791 "an array", t);
14792 remove = true;
14794 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14796 error_at (OMP_CLAUSE_LOCATION (c),
14797 "%<_Atomic%> %qD in %<aligned%> clause", t);
14798 remove = true;
14799 break;
14801 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14803 error_at (OMP_CLAUSE_LOCATION (c),
14804 "%qE appears more than once in %<aligned%> clauses",
14806 remove = true;
14808 else
14809 bitmap_set_bit (&aligned_head, DECL_UID (t));
14810 break;
14812 case OMP_CLAUSE_NONTEMPORAL:
14813 t = OMP_CLAUSE_DECL (c);
14814 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14816 error_at (OMP_CLAUSE_LOCATION (c),
14817 "%qE is not a variable in %<nontemporal%> clause", t);
14818 remove = true;
14820 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14822 error_at (OMP_CLAUSE_LOCATION (c),
14823 "%qE appears more than once in %<nontemporal%> "
14824 "clauses", t);
14825 remove = true;
14827 else
14828 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14829 break;
14831 case OMP_CLAUSE_ALLOCATE:
14832 t = OMP_CLAUSE_DECL (c);
14833 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14835 error_at (OMP_CLAUSE_LOCATION (c),
14836 "%qE is not a variable in %<allocate%> clause", t);
14837 remove = true;
14839 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14841 warning_at (OMP_CLAUSE_LOCATION (c), 0,
14842 "%qE appears more than once in %<allocate%> clauses",
14844 remove = true;
14846 else
14848 bitmap_set_bit (&aligned_head, DECL_UID (t));
14849 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
14850 allocate_seen = true;
14852 break;
14854 case OMP_CLAUSE_DOACROSS:
14855 t = OMP_CLAUSE_DECL (c);
14856 if (t == NULL_TREE)
14857 break;
14858 if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
14860 gcc_assert (TREE_CODE (t) == TREE_LIST);
14861 for (; t; t = TREE_CHAIN (t))
14863 tree decl = TREE_VALUE (t);
14864 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14866 tree offset = TREE_PURPOSE (t);
14867 bool neg = wi::neg_p (wi::to_wide (offset));
14868 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14869 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14870 neg ? MINUS_EXPR : PLUS_EXPR,
14871 decl, offset);
14872 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14873 sizetype,
14874 fold_convert (sizetype, t2),
14875 fold_convert (sizetype, decl));
14876 if (t2 == error_mark_node)
14878 remove = true;
14879 break;
14881 TREE_PURPOSE (t) = t2;
14884 break;
14886 gcc_unreachable ();
14887 case OMP_CLAUSE_DEPEND:
14888 case OMP_CLAUSE_AFFINITY:
14889 t = OMP_CLAUSE_DECL (c);
14890 if (TREE_CODE (t) == TREE_LIST
14891 && TREE_PURPOSE (t)
14892 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14894 if (TREE_PURPOSE (t) != last_iterators)
14895 last_iterators_remove
14896 = c_omp_finish_iterators (TREE_PURPOSE (t));
14897 last_iterators = TREE_PURPOSE (t);
14898 t = TREE_VALUE (t);
14899 if (last_iterators_remove)
14900 t = error_mark_node;
14902 else
14903 last_iterators = NULL_TREE;
14904 if (TREE_CODE (t) == TREE_LIST)
14906 if (handle_omp_array_sections (c, ort))
14907 remove = true;
14908 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14909 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14911 error_at (OMP_CLAUSE_LOCATION (c),
14912 "%<depend%> clause with %<depobj%> dependence "
14913 "type on array section");
14914 remove = true;
14916 break;
14918 if (t == error_mark_node)
14919 remove = true;
14920 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14921 && t == ridpointers[RID_OMP_ALL_MEMORY])
14923 if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
14924 && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
14926 error_at (OMP_CLAUSE_LOCATION (c),
14927 "%<omp_all_memory%> used with %<depend%> kind "
14928 "other than %<out%> or %<inout%>");
14929 remove = true;
14932 else if (!lvalue_p (t))
14934 error_at (OMP_CLAUSE_LOCATION (c),
14935 "%qE is not lvalue expression nor array section in "
14936 "%qs clause", t,
14937 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14938 remove = true;
14940 else if (TREE_CODE (t) == COMPONENT_REF
14941 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14943 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14944 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
14945 error_at (OMP_CLAUSE_LOCATION (c),
14946 "bit-field %qE in %qs clause", t,
14947 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14948 remove = true;
14950 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14951 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14953 if (!c_omp_depend_t_p (TREE_TYPE (t)))
14955 error_at (OMP_CLAUSE_LOCATION (c),
14956 "%qE does not have %<omp_depend_t%> type in "
14957 "%<depend%> clause with %<depobj%> dependence "
14958 "type", t);
14959 remove = true;
14962 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14963 && c_omp_depend_t_p (TREE_TYPE (t)))
14965 error_at (OMP_CLAUSE_LOCATION (c),
14966 "%qE should not have %<omp_depend_t%> type in "
14967 "%<depend%> clause with dependence type other than "
14968 "%<depobj%>", t);
14969 remove = true;
14971 if (!remove)
14973 if (t == ridpointers[RID_OMP_ALL_MEMORY])
14974 t = null_pointer_node;
14975 else
14977 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
14978 ADDR_EXPR, t, false);
14979 if (addr == error_mark_node)
14981 remove = true;
14982 break;
14984 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14985 RO_UNARY_STAR);
14986 if (t == error_mark_node)
14988 remove = true;
14989 break;
14992 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14993 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14994 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14995 == TREE_VEC))
14996 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14997 else
14998 OMP_CLAUSE_DECL (c) = t;
15000 break;
15002 case OMP_CLAUSE_MAP:
15003 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
15004 goto move_implicit;
15005 /* FALLTHRU */
15006 case OMP_CLAUSE_TO:
15007 case OMP_CLAUSE_FROM:
15008 case OMP_CLAUSE__CACHE_:
15009 t = OMP_CLAUSE_DECL (c);
15010 if (TREE_CODE (t) == TREE_LIST)
15012 grp_start_p = pc;
15013 grp_sentinel = OMP_CLAUSE_CHAIN (c);
15015 if (handle_omp_array_sections (c, ort))
15016 remove = true;
15017 else
15019 t = OMP_CLAUSE_DECL (c);
15020 if (!omp_mappable_type (TREE_TYPE (t)))
15022 error_at (OMP_CLAUSE_LOCATION (c),
15023 "array section does not have mappable type "
15024 "in %qs clause",
15025 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15026 remove = true;
15028 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15030 error_at (OMP_CLAUSE_LOCATION (c),
15031 "%<_Atomic%> %qE in %qs clause", t,
15032 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15033 remove = true;
15035 while (TREE_CODE (t) == ARRAY_REF)
15036 t = TREE_OPERAND (t, 0);
15037 if (TREE_CODE (t) == COMPONENT_REF
15038 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
15042 t = TREE_OPERAND (t, 0);
15043 if (TREE_CODE (t) == MEM_REF
15044 || TREE_CODE (t) == INDIRECT_REF)
15046 t = TREE_OPERAND (t, 0);
15047 STRIP_NOPS (t);
15048 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15049 t = TREE_OPERAND (t, 0);
15052 while (TREE_CODE (t) == COMPONENT_REF
15053 || TREE_CODE (t) == ARRAY_REF);
15055 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15056 && OMP_CLAUSE_MAP_IMPLICIT (c)
15057 && (bitmap_bit_p (&map_head, DECL_UID (t))
15058 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15059 || bitmap_bit_p (&map_firstprivate_head,
15060 DECL_UID (t))))
15062 remove = true;
15063 break;
15065 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
15066 break;
15067 if (bitmap_bit_p (&map_head, DECL_UID (t)))
15069 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15070 error_at (OMP_CLAUSE_LOCATION (c),
15071 "%qD appears more than once in motion "
15072 "clauses", t);
15073 else if (ort == C_ORT_ACC)
15074 error_at (OMP_CLAUSE_LOCATION (c),
15075 "%qD appears more than once in data "
15076 "clauses", t);
15077 else
15078 error_at (OMP_CLAUSE_LOCATION (c),
15079 "%qD appears more than once in map "
15080 "clauses", t);
15081 remove = true;
15083 else
15085 bitmap_set_bit (&map_head, DECL_UID (t));
15086 bitmap_set_bit (&map_field_head, DECL_UID (t));
15090 if (c_oacc_check_attachments (c))
15091 remove = true;
15092 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15093 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15094 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15095 /* In this case, we have a single array element which is a
15096 pointer, and we already set OMP_CLAUSE_SIZE in
15097 handle_omp_array_sections above. For attach/detach clauses,
15098 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
15099 here. */
15100 OMP_CLAUSE_SIZE (c) = size_zero_node;
15101 break;
15103 if (t == error_mark_node)
15105 remove = true;
15106 break;
15108 /* OpenACC attach / detach clauses must be pointers. */
15109 if (c_oacc_check_attachments (c))
15111 remove = true;
15112 break;
15114 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15115 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15116 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15117 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
15118 bias) to zero here, so it is not set erroneously to the pointer
15119 size later on in gimplify.cc. */
15120 OMP_CLAUSE_SIZE (c) = size_zero_node;
15121 while (TREE_CODE (t) == INDIRECT_REF
15122 || TREE_CODE (t) == ARRAY_REF)
15124 t = TREE_OPERAND (t, 0);
15125 STRIP_NOPS (t);
15126 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15127 t = TREE_OPERAND (t, 0);
15129 while (TREE_CODE (t) == COMPOUND_EXPR)
15131 t = TREE_OPERAND (t, 1);
15132 STRIP_NOPS (t);
15134 indir_component_ref_p = false;
15135 if (TREE_CODE (t) == COMPONENT_REF
15136 && (TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
15137 || TREE_CODE (TREE_OPERAND (t, 0)) == INDIRECT_REF
15138 || TREE_CODE (TREE_OPERAND (t, 0)) == ARRAY_REF))
15140 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
15141 indir_component_ref_p = true;
15142 STRIP_NOPS (t);
15143 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15144 t = TREE_OPERAND (t, 0);
15147 if (TREE_CODE (t) == COMPONENT_REF
15148 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
15150 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
15152 error_at (OMP_CLAUSE_LOCATION (c),
15153 "bit-field %qE in %qs clause",
15154 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15155 remove = true;
15157 else if (!omp_mappable_type (TREE_TYPE (t)))
15159 error_at (OMP_CLAUSE_LOCATION (c),
15160 "%qE does not have a mappable type in %qs clause",
15161 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15162 remove = true;
15164 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15166 error_at (OMP_CLAUSE_LOCATION (c),
15167 "%<_Atomic%> %qE in %qs clause", t,
15168 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15169 remove = true;
15171 while (TREE_CODE (t) == COMPONENT_REF)
15173 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
15174 == UNION_TYPE)
15176 error_at (OMP_CLAUSE_LOCATION (c),
15177 "%qE is a member of a union", t);
15178 remove = true;
15179 break;
15181 t = TREE_OPERAND (t, 0);
15182 if (TREE_CODE (t) == MEM_REF)
15184 if (maybe_ne (mem_ref_offset (t), 0))
15185 error_at (OMP_CLAUSE_LOCATION (c),
15186 "cannot dereference %qE in %qs clause", t,
15187 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15188 else
15189 t = TREE_OPERAND (t, 0);
15191 while (TREE_CODE (t) == MEM_REF
15192 || TREE_CODE (t) == INDIRECT_REF
15193 || TREE_CODE (t) == ARRAY_REF)
15195 t = TREE_OPERAND (t, 0);
15196 STRIP_NOPS (t);
15197 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15198 t = TREE_OPERAND (t, 0);
15201 if (remove)
15202 break;
15203 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15205 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
15206 || (ort != C_ORT_ACC
15207 && bitmap_bit_p (&map_head, DECL_UID (t))))
15208 break;
15211 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15213 error_at (OMP_CLAUSE_LOCATION (c),
15214 "%qE is not a variable in %qs clause", t,
15215 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15216 remove = true;
15218 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15220 error_at (OMP_CLAUSE_LOCATION (c),
15221 "%qD is threadprivate variable in %qs clause", t,
15222 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15223 remove = true;
15225 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15226 || (OMP_CLAUSE_MAP_KIND (c)
15227 != GOMP_MAP_FIRSTPRIVATE_POINTER))
15228 && !indir_component_ref_p
15229 && !c_mark_addressable (t))
15230 remove = true;
15231 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15232 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
15233 || (OMP_CLAUSE_MAP_KIND (c)
15234 == GOMP_MAP_FIRSTPRIVATE_POINTER)
15235 || (OMP_CLAUSE_MAP_KIND (c)
15236 == GOMP_MAP_FORCE_DEVICEPTR)))
15237 && t == OMP_CLAUSE_DECL (c)
15238 && !omp_mappable_type (TREE_TYPE (t)))
15240 error_at (OMP_CLAUSE_LOCATION (c),
15241 "%qD does not have a mappable type in %qs clause", t,
15242 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15243 remove = true;
15245 else if (TREE_TYPE (t) == error_mark_node)
15246 remove = true;
15247 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15249 error_at (OMP_CLAUSE_LOCATION (c),
15250 "%<_Atomic%> %qE in %qs clause", t,
15251 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15252 remove = true;
15254 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15255 && OMP_CLAUSE_MAP_IMPLICIT (c)
15256 && (bitmap_bit_p (&map_head, DECL_UID (t))
15257 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15258 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))))
15259 remove = true;
15260 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15261 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
15263 if (bitmap_bit_p (&generic_head, DECL_UID (t))
15264 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15265 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15267 error_at (OMP_CLAUSE_LOCATION (c),
15268 "%qD appears more than once in data clauses", t);
15269 remove = true;
15271 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15272 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15274 if (ort == C_ORT_ACC)
15275 error_at (OMP_CLAUSE_LOCATION (c),
15276 "%qD appears more than once in data clauses", t);
15277 else
15278 error_at (OMP_CLAUSE_LOCATION (c),
15279 "%qD appears both in data and map clauses", t);
15280 remove = true;
15282 else
15283 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
15285 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15286 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15288 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15289 error_at (OMP_CLAUSE_LOCATION (c),
15290 "%qD appears more than once in motion clauses", t);
15291 else if (ort == C_ORT_ACC)
15292 error_at (OMP_CLAUSE_LOCATION (c),
15293 "%qD appears more than once in data clauses", t);
15294 else
15295 error_at (OMP_CLAUSE_LOCATION (c),
15296 "%qD appears more than once in map clauses", t);
15297 remove = true;
15299 else if (ort == C_ORT_ACC
15300 && bitmap_bit_p (&generic_head, DECL_UID (t)))
15302 error_at (OMP_CLAUSE_LOCATION (c),
15303 "%qD appears more than once in data clauses", t);
15304 remove = true;
15306 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15307 || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
15309 if (ort == C_ORT_ACC)
15310 error_at (OMP_CLAUSE_LOCATION (c),
15311 "%qD appears more than once in data clauses", t);
15312 else
15313 error_at (OMP_CLAUSE_LOCATION (c),
15314 "%qD appears both in data and map clauses", t);
15315 remove = true;
15317 else
15319 bitmap_set_bit (&map_head, DECL_UID (t));
15320 if (t != OMP_CLAUSE_DECL (c)
15321 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
15322 bitmap_set_bit (&map_field_head, DECL_UID (t));
15324 break;
15326 case OMP_CLAUSE_ENTER:
15327 case OMP_CLAUSE_LINK:
15328 t = OMP_CLAUSE_DECL (c);
15329 const char *cname;
15330 cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
15331 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
15332 && OMP_CLAUSE_ENTER_TO (c))
15333 cname = "to";
15334 if (TREE_CODE (t) == FUNCTION_DECL
15335 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15337 else if (!VAR_P (t))
15339 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15340 error_at (OMP_CLAUSE_LOCATION (c),
15341 "%qE is neither a variable nor a function name in "
15342 "clause %qs", t, cname);
15343 else
15344 error_at (OMP_CLAUSE_LOCATION (c),
15345 "%qE is not a variable in clause %qs", t, cname);
15346 remove = true;
15348 else if (DECL_THREAD_LOCAL_P (t))
15350 error_at (OMP_CLAUSE_LOCATION (c),
15351 "%qD is threadprivate variable in %qs clause", t,
15352 cname);
15353 remove = true;
15355 else if (!omp_mappable_type (TREE_TYPE (t)))
15357 error_at (OMP_CLAUSE_LOCATION (c),
15358 "%qD does not have a mappable type in %qs clause", t,
15359 cname);
15360 remove = true;
15362 if (remove)
15363 break;
15364 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15366 error_at (OMP_CLAUSE_LOCATION (c),
15367 "%qE appears more than once on the same "
15368 "%<declare target%> directive", t);
15369 remove = true;
15371 else
15372 bitmap_set_bit (&generic_head, DECL_UID (t));
15373 break;
15375 case OMP_CLAUSE_UNIFORM:
15376 t = OMP_CLAUSE_DECL (c);
15377 if (TREE_CODE (t) != PARM_DECL)
15379 if (DECL_P (t))
15380 error_at (OMP_CLAUSE_LOCATION (c),
15381 "%qD is not an argument in %<uniform%> clause", t);
15382 else
15383 error_at (OMP_CLAUSE_LOCATION (c),
15384 "%qE is not an argument in %<uniform%> clause", t);
15385 remove = true;
15386 break;
15388 /* map_head bitmap is used as uniform_head if declare_simd. */
15389 bitmap_set_bit (&map_head, DECL_UID (t));
15390 goto check_dup_generic;
15392 case OMP_CLAUSE_IS_DEVICE_PTR:
15393 case OMP_CLAUSE_USE_DEVICE_PTR:
15394 t = OMP_CLAUSE_DECL (c);
15395 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15396 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15397 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15399 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15400 && ort != C_ORT_ACC)
15402 error_at (OMP_CLAUSE_LOCATION (c),
15403 "%qs variable is not a pointer",
15404 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15405 remove = true;
15407 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15409 error_at (OMP_CLAUSE_LOCATION (c),
15410 "%qs variable is neither a pointer nor an array",
15411 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15412 remove = true;
15415 goto check_dup_generic;
15417 case OMP_CLAUSE_HAS_DEVICE_ADDR:
15418 t = OMP_CLAUSE_DECL (c);
15419 if (TREE_CODE (t) == TREE_LIST)
15421 if (handle_omp_array_sections (c, ort))
15422 remove = true;
15423 else
15425 t = OMP_CLAUSE_DECL (c);
15426 while (TREE_CODE (t) == ARRAY_REF)
15427 t = TREE_OPERAND (t, 0);
15430 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15431 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15432 c_mark_addressable (t);
15433 goto check_dup_generic_t;
15435 case OMP_CLAUSE_USE_DEVICE_ADDR:
15436 t = OMP_CLAUSE_DECL (c);
15437 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15438 c_mark_addressable (t);
15439 goto check_dup_generic;
15441 case OMP_CLAUSE_NOWAIT:
15442 if (copyprivate_seen)
15444 error_at (OMP_CLAUSE_LOCATION (c),
15445 "%<nowait%> clause must not be used together "
15446 "with %<copyprivate%>");
15447 remove = true;
15448 break;
15450 nowait_clause = pc;
15451 pc = &OMP_CLAUSE_CHAIN (c);
15452 continue;
15454 case OMP_CLAUSE_ORDER:
15455 if (ordered_clause)
15457 error_at (OMP_CLAUSE_LOCATION (c),
15458 "%<order%> clause must not be used together "
15459 "with %<ordered%>");
15460 remove = true;
15461 break;
15463 else if (order_clause)
15465 /* Silently remove duplicates. */
15466 remove = true;
15467 break;
15469 order_clause = pc;
15470 pc = &OMP_CLAUSE_CHAIN (c);
15471 continue;
15473 case OMP_CLAUSE_DETACH:
15474 t = OMP_CLAUSE_DECL (c);
15475 if (detach_seen)
15477 error_at (OMP_CLAUSE_LOCATION (c),
15478 "too many %qs clauses on a task construct",
15479 "detach");
15480 remove = true;
15481 break;
15483 detach_seen = pc;
15484 pc = &OMP_CLAUSE_CHAIN (c);
15485 c_mark_addressable (t);
15486 continue;
15488 case OMP_CLAUSE_IF:
15489 case OMP_CLAUSE_NUM_THREADS:
15490 case OMP_CLAUSE_NUM_TEAMS:
15491 case OMP_CLAUSE_THREAD_LIMIT:
15492 case OMP_CLAUSE_DEFAULT:
15493 case OMP_CLAUSE_UNTIED:
15494 case OMP_CLAUSE_COLLAPSE:
15495 case OMP_CLAUSE_FINAL:
15496 case OMP_CLAUSE_DEVICE:
15497 case OMP_CLAUSE_DIST_SCHEDULE:
15498 case OMP_CLAUSE_PARALLEL:
15499 case OMP_CLAUSE_FOR:
15500 case OMP_CLAUSE_SECTIONS:
15501 case OMP_CLAUSE_TASKGROUP:
15502 case OMP_CLAUSE_PROC_BIND:
15503 case OMP_CLAUSE_DEVICE_TYPE:
15504 case OMP_CLAUSE_PRIORITY:
15505 case OMP_CLAUSE_GRAINSIZE:
15506 case OMP_CLAUSE_NUM_TASKS:
15507 case OMP_CLAUSE_THREADS:
15508 case OMP_CLAUSE_SIMD:
15509 case OMP_CLAUSE_HINT:
15510 case OMP_CLAUSE_FILTER:
15511 case OMP_CLAUSE_DEFAULTMAP:
15512 case OMP_CLAUSE_BIND:
15513 case OMP_CLAUSE_NUM_GANGS:
15514 case OMP_CLAUSE_NUM_WORKERS:
15515 case OMP_CLAUSE_VECTOR_LENGTH:
15516 case OMP_CLAUSE_ASYNC:
15517 case OMP_CLAUSE_WAIT:
15518 case OMP_CLAUSE_AUTO:
15519 case OMP_CLAUSE_INDEPENDENT:
15520 case OMP_CLAUSE_SEQ:
15521 case OMP_CLAUSE_GANG:
15522 case OMP_CLAUSE_WORKER:
15523 case OMP_CLAUSE_VECTOR:
15524 case OMP_CLAUSE_TILE:
15525 case OMP_CLAUSE_IF_PRESENT:
15526 case OMP_CLAUSE_FINALIZE:
15527 case OMP_CLAUSE_NOHOST:
15528 pc = &OMP_CLAUSE_CHAIN (c);
15529 continue;
15531 case OMP_CLAUSE_MERGEABLE:
15532 mergeable_seen = true;
15533 pc = &OMP_CLAUSE_CHAIN (c);
15534 continue;
15536 case OMP_CLAUSE_NOGROUP:
15537 nogroup_seen = pc;
15538 pc = &OMP_CLAUSE_CHAIN (c);
15539 continue;
15541 case OMP_CLAUSE_SCHEDULE:
15542 schedule_clause = c;
15543 pc = &OMP_CLAUSE_CHAIN (c);
15544 continue;
15546 case OMP_CLAUSE_ORDERED:
15547 ordered_clause = c;
15548 if (order_clause)
15550 error_at (OMP_CLAUSE_LOCATION (*order_clause),
15551 "%<order%> clause must not be used together "
15552 "with %<ordered%>");
15553 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
15554 order_clause = NULL;
15556 pc = &OMP_CLAUSE_CHAIN (c);
15557 continue;
15559 case OMP_CLAUSE_SAFELEN:
15560 safelen = c;
15561 pc = &OMP_CLAUSE_CHAIN (c);
15562 continue;
15563 case OMP_CLAUSE_SIMDLEN:
15564 simdlen = c;
15565 pc = &OMP_CLAUSE_CHAIN (c);
15566 continue;
15568 case OMP_CLAUSE_INBRANCH:
15569 case OMP_CLAUSE_NOTINBRANCH:
15570 if (branch_seen)
15572 error_at (OMP_CLAUSE_LOCATION (c),
15573 "%<inbranch%> clause is incompatible with "
15574 "%<notinbranch%>");
15575 remove = true;
15576 break;
15578 branch_seen = true;
15579 pc = &OMP_CLAUSE_CHAIN (c);
15580 continue;
15582 case OMP_CLAUSE_INCLUSIVE:
15583 case OMP_CLAUSE_EXCLUSIVE:
15584 need_complete = true;
15585 need_implicitly_determined = true;
15586 t = OMP_CLAUSE_DECL (c);
15587 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15589 error_at (OMP_CLAUSE_LOCATION (c),
15590 "%qE is not a variable in clause %qs", t,
15591 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15592 remove = true;
15594 break;
15596 default:
15597 gcc_unreachable ();
15600 if (!remove)
15602 t = OMP_CLAUSE_DECL (c);
15604 if (need_complete)
15606 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15607 if (t == error_mark_node)
15608 remove = true;
15611 if (need_implicitly_determined)
15613 const char *share_name = NULL;
15615 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15616 share_name = "threadprivate";
15617 else switch (c_omp_predetermined_sharing (t))
15619 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15620 break;
15621 case OMP_CLAUSE_DEFAULT_SHARED:
15622 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15623 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15624 && c_omp_predefined_variable (t))
15625 /* The __func__ variable and similar function-local
15626 predefined variables may be listed in a shared or
15627 firstprivate clause. */
15628 break;
15629 share_name = "shared";
15630 break;
15631 case OMP_CLAUSE_DEFAULT_PRIVATE:
15632 share_name = "private";
15633 break;
15634 default:
15635 gcc_unreachable ();
15637 if (share_name)
15639 error_at (OMP_CLAUSE_LOCATION (c),
15640 "%qE is predetermined %qs for %qs",
15641 t, share_name,
15642 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15643 remove = true;
15645 else if (TREE_READONLY (t)
15646 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15647 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15649 error_at (OMP_CLAUSE_LOCATION (c),
15650 "%<const%> qualified %qE may appear only in "
15651 "%<shared%> or %<firstprivate%> clauses", t);
15652 remove = true;
15657 if (remove)
15659 if (grp_start_p)
15661 /* If we found a clause to remove, we want to remove the whole
15662 expanded group, otherwise gimplify
15663 (omp_resolve_clause_dependencies) can get confused. */
15664 *grp_start_p = grp_sentinel;
15665 pc = grp_start_p;
15666 grp_start_p = NULL;
15668 else
15669 *pc = OMP_CLAUSE_CHAIN (c);
15671 else
15672 pc = &OMP_CLAUSE_CHAIN (c);
15675 if (simdlen
15676 && safelen
15677 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
15678 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
15680 error_at (OMP_CLAUSE_LOCATION (simdlen),
15681 "%<simdlen%> clause value is bigger than "
15682 "%<safelen%> clause value");
15683 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
15684 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
15687 if (ordered_clause
15688 && schedule_clause
15689 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15690 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
15692 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15693 "%<nonmonotonic%> schedule modifier specified together "
15694 "with %<ordered%> clause");
15695 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15696 = (enum omp_clause_schedule_kind)
15697 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15698 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
15701 if (reduction_seen < 0 && ordered_clause)
15703 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
15704 "%qs clause specified together with %<inscan%> "
15705 "%<reduction%> clause", "ordered");
15706 reduction_seen = -2;
15709 if (reduction_seen < 0 && schedule_clause)
15711 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15712 "%qs clause specified together with %<inscan%> "
15713 "%<reduction%> clause", "schedule");
15714 reduction_seen = -2;
15717 if (linear_variable_step_check
15718 || reduction_seen == -2
15719 || allocate_seen
15720 || target_in_reduction_seen)
15721 for (pc = &clauses, c = clauses; c ; c = *pc)
15723 bool remove = false;
15724 if (allocate_seen)
15725 switch (OMP_CLAUSE_CODE (c))
15727 case OMP_CLAUSE_REDUCTION:
15728 case OMP_CLAUSE_IN_REDUCTION:
15729 case OMP_CLAUSE_TASK_REDUCTION:
15730 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
15732 t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
15733 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15734 t = TREE_OPERAND (t, 0);
15735 if (TREE_CODE (t) == ADDR_EXPR
15736 || TREE_CODE (t) == INDIRECT_REF)
15737 t = TREE_OPERAND (t, 0);
15738 if (DECL_P (t))
15739 bitmap_clear_bit (&aligned_head, DECL_UID (t));
15740 break;
15742 /* FALLTHRU */
15743 case OMP_CLAUSE_PRIVATE:
15744 case OMP_CLAUSE_FIRSTPRIVATE:
15745 case OMP_CLAUSE_LASTPRIVATE:
15746 case OMP_CLAUSE_LINEAR:
15747 if (DECL_P (OMP_CLAUSE_DECL (c)))
15748 bitmap_clear_bit (&aligned_head,
15749 DECL_UID (OMP_CLAUSE_DECL (c)));
15750 break;
15751 default:
15752 break;
15754 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15755 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
15756 && !bitmap_bit_p (&map_head,
15757 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
15759 error_at (OMP_CLAUSE_LOCATION (c),
15760 "%<linear%> clause step is a parameter %qD not "
15761 "specified in %<uniform%> clause",
15762 OMP_CLAUSE_LINEAR_STEP (c));
15763 remove = true;
15765 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15766 && reduction_seen == -2)
15767 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
15768 if (target_in_reduction_seen
15769 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
15771 tree t = OMP_CLAUSE_DECL (c);
15772 while (handled_component_p (t)
15773 || TREE_CODE (t) == INDIRECT_REF
15774 || TREE_CODE (t) == ADDR_EXPR
15775 || TREE_CODE (t) == MEM_REF
15776 || TREE_CODE (t) == NON_LVALUE_EXPR)
15777 t = TREE_OPERAND (t, 0);
15778 if (DECL_P (t)
15779 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15780 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
15783 if (remove)
15784 *pc = OMP_CLAUSE_CHAIN (c);
15785 else
15786 pc = &OMP_CLAUSE_CHAIN (c);
15789 if (allocate_seen)
15790 for (pc = &clauses, c = clauses; c ; c = *pc)
15792 bool remove = false;
15793 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
15794 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
15795 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
15797 error_at (OMP_CLAUSE_LOCATION (c),
15798 "%qD specified in %<allocate%> clause but not in "
15799 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
15800 remove = true;
15802 if (remove)
15803 *pc = OMP_CLAUSE_CHAIN (c);
15804 else
15805 pc = &OMP_CLAUSE_CHAIN (c);
15808 if (nogroup_seen && reduction_seen)
15810 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
15811 "%<nogroup%> clause must not be used together with "
15812 "%<reduction%> clause");
15813 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
15816 if (detach_seen)
15818 if (mergeable_seen)
15820 error_at (OMP_CLAUSE_LOCATION (*detach_seen),
15821 "%<detach%> clause must not be used together with "
15822 "%<mergeable%> clause");
15823 *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
15825 else
15827 tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
15829 for (pc = &clauses, c = clauses; c ; c = *pc)
15831 bool remove = false;
15832 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15833 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15834 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
15835 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
15836 && OMP_CLAUSE_DECL (c) == detach_decl)
15838 error_at (OMP_CLAUSE_LOCATION (c),
15839 "the event handle of a %<detach%> clause "
15840 "should not be in a data-sharing clause");
15841 remove = true;
15843 if (remove)
15844 *pc = OMP_CLAUSE_CHAIN (c);
15845 else
15846 pc = &OMP_CLAUSE_CHAIN (c);
15851 bitmap_obstack_release (NULL);
15852 return clauses;
15855 /* Return code to initialize DST with a copy constructor from SRC.
15856 C doesn't have copy constructors nor assignment operators, only for
15857 _Atomic vars we need to perform __atomic_load from src into a temporary
15858 followed by __atomic_store of the temporary to dst. */
15860 tree
15861 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
15863 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
15864 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
15866 location_t loc = OMP_CLAUSE_LOCATION (clause);
15867 tree type = TREE_TYPE (dst);
15868 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
15869 tree tmp = create_tmp_var (nonatomic_type);
15870 tree tmp_addr = build_fold_addr_expr (tmp);
15871 TREE_ADDRESSABLE (tmp) = 1;
15872 suppress_warning (tmp);
15873 tree src_addr = build_fold_addr_expr (src);
15874 tree dst_addr = build_fold_addr_expr (dst);
15875 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
15876 vec<tree, va_gc> *params;
15877 /* Expansion of a generic atomic load may require an addition
15878 element, so allocate enough to prevent a resize. */
15879 vec_alloc (params, 4);
15881 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
15882 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
15883 params->quick_push (src_addr);
15884 params->quick_push (tmp_addr);
15885 params->quick_push (seq_cst);
15886 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15888 vec_alloc (params, 4);
15890 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
15891 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
15892 params->quick_push (dst_addr);
15893 params->quick_push (tmp_addr);
15894 params->quick_push (seq_cst);
15895 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15896 return build2 (COMPOUND_EXPR, void_type_node, load, store);
15899 /* Create a transaction node. */
15901 tree
15902 c_finish_transaction (location_t loc, tree block, int flags)
15904 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
15905 if (flags & TM_STMT_ATTR_OUTER)
15906 TRANSACTION_EXPR_OUTER (stmt) = 1;
15907 if (flags & TM_STMT_ATTR_RELAXED)
15908 TRANSACTION_EXPR_RELAXED (stmt) = 1;
15909 return add_stmt (stmt);
15912 /* Make a variant type in the proper way for C/C++, propagating qualifiers
15913 down to the element type of an array. If ORIG_QUAL_TYPE is not
15914 NULL, then it should be used as the qualified type
15915 ORIG_QUAL_INDIRECT levels down in array type derivation (to
15916 preserve information about the typedef name from which an array
15917 type was derived). */
15919 tree
15920 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
15921 size_t orig_qual_indirect)
15923 if (type == error_mark_node)
15924 return type;
15926 if (TREE_CODE (type) == ARRAY_TYPE)
15928 tree t;
15929 tree element_type = c_build_qualified_type (TREE_TYPE (type),
15930 type_quals, orig_qual_type,
15931 orig_qual_indirect - 1);
15933 /* See if we already have an identically qualified type. */
15934 if (orig_qual_type && orig_qual_indirect == 0)
15935 t = orig_qual_type;
15936 else
15937 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
15939 if (TYPE_QUALS (strip_array_types (t)) == type_quals
15940 && TYPE_NAME (t) == TYPE_NAME (type)
15941 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
15942 && attribute_list_equal (TYPE_ATTRIBUTES (t),
15943 TYPE_ATTRIBUTES (type)))
15944 break;
15946 if (!t)
15948 tree domain = TYPE_DOMAIN (type);
15950 t = build_variant_type_copy (type);
15951 TREE_TYPE (t) = element_type;
15953 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
15954 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
15955 SET_TYPE_STRUCTURAL_EQUALITY (t);
15956 else if (TYPE_CANONICAL (element_type) != element_type
15957 || (domain && TYPE_CANONICAL (domain) != domain))
15959 tree unqualified_canon
15960 = build_array_type (TYPE_CANONICAL (element_type),
15961 domain? TYPE_CANONICAL (domain)
15962 : NULL_TREE);
15963 if (TYPE_REVERSE_STORAGE_ORDER (type))
15965 unqualified_canon
15966 = build_distinct_type_copy (unqualified_canon);
15967 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
15969 TYPE_CANONICAL (t)
15970 = c_build_qualified_type (unqualified_canon, type_quals);
15972 else
15973 TYPE_CANONICAL (t) = t;
15975 return t;
15978 /* A restrict-qualified pointer type must be a pointer to object or
15979 incomplete type. Note that the use of POINTER_TYPE_P also allows
15980 REFERENCE_TYPEs, which is appropriate for C++. */
15981 if ((type_quals & TYPE_QUAL_RESTRICT)
15982 && (!POINTER_TYPE_P (type)
15983 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
15985 error ("invalid use of %<restrict%>");
15986 type_quals &= ~TYPE_QUAL_RESTRICT;
15989 tree var_type = (orig_qual_type && orig_qual_indirect == 0
15990 ? orig_qual_type
15991 : build_qualified_type (type, type_quals));
15992 /* A variant type does not inherit the list of incomplete vars from the
15993 type main variant. */
15994 if ((RECORD_OR_UNION_TYPE_P (var_type)
15995 || TREE_CODE (var_type) == ENUMERAL_TYPE)
15996 && TYPE_MAIN_VARIANT (var_type) != var_type)
15997 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
15998 return var_type;
16001 /* Build a VA_ARG_EXPR for the C parser. */
16003 tree
16004 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
16006 if (error_operand_p (type))
16007 return error_mark_node;
16008 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
16009 order because it takes the address of the expression. */
16010 else if (handled_component_p (expr)
16011 && reverse_storage_order_for_component_p (expr))
16013 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
16014 return error_mark_node;
16016 else if (!COMPLETE_TYPE_P (type))
16018 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
16019 "type %qT", type);
16020 return error_mark_node;
16022 else if (TREE_CODE (type) == FUNCTION_TYPE)
16024 error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
16025 type);
16026 return error_mark_node;
16028 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
16029 warning_at (loc2, OPT_Wc___compat,
16030 "C++ requires promoted type, not enum type, in %<va_arg%>");
16031 return build_va_arg (loc2, expr, type);
16034 /* Return truthvalue of whether T1 is the same tree structure as T2.
16035 Return 1 if they are the same. Return false if they are different. */
16037 bool
16038 c_tree_equal (tree t1, tree t2)
16040 enum tree_code code1, code2;
16042 if (t1 == t2)
16043 return true;
16044 if (!t1 || !t2)
16045 return false;
16047 for (code1 = TREE_CODE (t1);
16048 CONVERT_EXPR_CODE_P (code1)
16049 || code1 == NON_LVALUE_EXPR;
16050 code1 = TREE_CODE (t1))
16051 t1 = TREE_OPERAND (t1, 0);
16052 for (code2 = TREE_CODE (t2);
16053 CONVERT_EXPR_CODE_P (code2)
16054 || code2 == NON_LVALUE_EXPR;
16055 code2 = TREE_CODE (t2))
16056 t2 = TREE_OPERAND (t2, 0);
16058 /* They might have become equal now. */
16059 if (t1 == t2)
16060 return true;
16062 if (code1 != code2)
16063 return false;
16065 switch (code1)
16067 case INTEGER_CST:
16068 return wi::to_wide (t1) == wi::to_wide (t2);
16070 case REAL_CST:
16071 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
16073 case STRING_CST:
16074 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
16075 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
16076 TREE_STRING_LENGTH (t1));
16078 case FIXED_CST:
16079 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
16080 TREE_FIXED_CST (t2));
16082 case COMPLEX_CST:
16083 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
16084 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
16086 case VECTOR_CST:
16087 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
16089 case CONSTRUCTOR:
16090 /* We need to do this when determining whether or not two
16091 non-type pointer to member function template arguments
16092 are the same. */
16093 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
16094 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
16095 return false;
16097 tree field, value;
16098 unsigned int i;
16099 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
16101 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
16102 if (!c_tree_equal (field, elt2->index)
16103 || !c_tree_equal (value, elt2->value))
16104 return false;
16107 return true;
16109 case TREE_LIST:
16110 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
16111 return false;
16112 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
16113 return false;
16114 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
16116 case SAVE_EXPR:
16117 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16119 case CALL_EXPR:
16121 tree arg1, arg2;
16122 call_expr_arg_iterator iter1, iter2;
16123 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
16124 return false;
16125 for (arg1 = first_call_expr_arg (t1, &iter1),
16126 arg2 = first_call_expr_arg (t2, &iter2);
16127 arg1 && arg2;
16128 arg1 = next_call_expr_arg (&iter1),
16129 arg2 = next_call_expr_arg (&iter2))
16130 if (!c_tree_equal (arg1, arg2))
16131 return false;
16132 if (arg1 || arg2)
16133 return false;
16134 return true;
16137 case TARGET_EXPR:
16139 tree o1 = TREE_OPERAND (t1, 0);
16140 tree o2 = TREE_OPERAND (t2, 0);
16142 /* Special case: if either target is an unallocated VAR_DECL,
16143 it means that it's going to be unified with whatever the
16144 TARGET_EXPR is really supposed to initialize, so treat it
16145 as being equivalent to anything. */
16146 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
16147 && !DECL_RTL_SET_P (o1))
16148 /*Nop*/;
16149 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
16150 && !DECL_RTL_SET_P (o2))
16151 /*Nop*/;
16152 else if (!c_tree_equal (o1, o2))
16153 return false;
16155 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
16158 case COMPONENT_REF:
16159 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
16160 return false;
16161 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16163 case PARM_DECL:
16164 case VAR_DECL:
16165 case CONST_DECL:
16166 case FIELD_DECL:
16167 case FUNCTION_DECL:
16168 case IDENTIFIER_NODE:
16169 case SSA_NAME:
16170 return false;
16172 case TREE_VEC:
16174 unsigned ix;
16175 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
16176 return false;
16177 for (ix = TREE_VEC_LENGTH (t1); ix--;)
16178 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
16179 TREE_VEC_ELT (t2, ix)))
16180 return false;
16181 return true;
16184 default:
16185 break;
16188 switch (TREE_CODE_CLASS (code1))
16190 case tcc_unary:
16191 case tcc_binary:
16192 case tcc_comparison:
16193 case tcc_expression:
16194 case tcc_vl_exp:
16195 case tcc_reference:
16196 case tcc_statement:
16198 int i, n = TREE_OPERAND_LENGTH (t1);
16200 switch (code1)
16202 case PREINCREMENT_EXPR:
16203 case PREDECREMENT_EXPR:
16204 case POSTINCREMENT_EXPR:
16205 case POSTDECREMENT_EXPR:
16206 n = 1;
16207 break;
16208 case ARRAY_REF:
16209 n = 2;
16210 break;
16211 default:
16212 break;
16215 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
16216 && n != TREE_OPERAND_LENGTH (t2))
16217 return false;
16219 for (i = 0; i < n; ++i)
16220 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
16221 return false;
16223 return true;
16226 case tcc_type:
16227 return comptypes (t1, t2);
16228 default:
16229 gcc_unreachable ();
16233 /* Returns true when the function declaration FNDECL is implicit,
16234 introduced as a result of a call to an otherwise undeclared
16235 function, and false otherwise. */
16237 bool
16238 c_decl_implicit (const_tree fndecl)
16240 return C_DECL_IMPLICIT (fndecl);