Daily bump.
[official-gcc.git] / gcc / c / c-typeck.c
blob0aac978c02e0609d7d2f40072750c88775ec8fc1
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2021 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 bad conversions. Used to select
57 diagnostic messages in convert_for_assignment. */
58 enum impl_conv {
59 ic_argpass,
60 ic_assign,
61 ic_init,
62 ic_return
65 /* The level of nesting inside "__alignof__". */
66 int in_alignof;
68 /* The level of nesting inside "sizeof". */
69 int in_sizeof;
71 /* The level of nesting inside "typeof". */
72 int in_typeof;
74 /* True when parsing OpenMP loop expressions. */
75 bool c_in_omp_for;
77 /* The argument of last parsed sizeof expression, only to be tested
78 if expr.original_code == SIZEOF_EXPR. */
79 tree c_last_sizeof_arg;
80 location_t c_last_sizeof_loc;
82 /* Nonzero if we might need to print a "missing braces around
83 initializer" message within this initializer. */
84 static int found_missing_braces;
86 static int require_constant_value;
87 static int require_constant_elements;
89 static bool null_pointer_constant_p (const_tree);
90 static tree qualify_type (tree, tree);
91 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
92 bool *);
93 static int comp_target_types (location_t, tree, tree);
94 static int function_types_compatible_p (const_tree, const_tree, bool *,
95 bool *);
96 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
97 static tree lookup_field (tree, tree);
98 static int convert_arguments (location_t, vec<location_t>, tree,
99 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
100 tree);
101 static tree pointer_diff (location_t, tree, tree, tree *);
102 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
103 enum impl_conv, bool, tree, tree, int,
104 int = 0);
105 static tree valid_compound_expr_initializer (tree, tree);
106 static void push_string (const char *);
107 static void push_member_name (tree);
108 static int spelling_length (void);
109 static char *print_spelling (char *);
110 static void warning_init (location_t, int, const char *);
111 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
112 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
113 bool, struct obstack *);
114 static void output_pending_init_elements (int, struct obstack *);
115 static bool set_designator (location_t, bool, struct obstack *);
116 static void push_range_stack (tree, struct obstack *);
117 static void add_pending_init (location_t, tree, tree, tree, bool,
118 struct obstack *);
119 static void set_nonincremental_init (struct obstack *);
120 static void set_nonincremental_init_from_string (tree, struct obstack *);
121 static tree find_init_member (tree, struct obstack *);
122 static void readonly_warning (tree, enum lvalue_use);
123 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
124 static void record_maybe_used_decl (tree);
125 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
127 /* Return true if EXP is a null pointer constant, false otherwise. */
129 static bool
130 null_pointer_constant_p (const_tree expr)
132 /* This should really operate on c_expr structures, but they aren't
133 yet available everywhere required. */
134 tree type = TREE_TYPE (expr);
135 return (TREE_CODE (expr) == INTEGER_CST
136 && !TREE_OVERFLOW (expr)
137 && integer_zerop (expr)
138 && (INTEGRAL_TYPE_P (type)
139 || (TREE_CODE (type) == POINTER_TYPE
140 && VOID_TYPE_P (TREE_TYPE (type))
141 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
144 /* EXPR may appear in an unevaluated part of an integer constant
145 expression, but not in an evaluated part. Wrap it in a
146 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
147 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
149 static tree
150 note_integer_operands (tree expr)
152 tree ret;
153 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
155 ret = copy_node (expr);
156 TREE_OVERFLOW (ret) = 1;
158 else
160 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
161 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
163 return ret;
166 /* Having checked whether EXPR may appear in an unevaluated part of an
167 integer constant expression and found that it may, remove any
168 C_MAYBE_CONST_EXPR noting this fact and return the resulting
169 expression. */
171 static inline tree
172 remove_c_maybe_const_expr (tree expr)
174 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
175 return C_MAYBE_CONST_EXPR_EXPR (expr);
176 else
177 return expr;
180 \f/* This is a cache to hold if two types are compatible or not. */
182 struct tagged_tu_seen_cache {
183 const struct tagged_tu_seen_cache * next;
184 const_tree t1;
185 const_tree t2;
186 /* The return value of tagged_types_tu_compatible_p if we had seen
187 these two types already. */
188 int val;
191 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
192 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
194 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
195 does not have an incomplete type. (That includes void types.)
196 LOC is the location of the use. */
198 tree
199 require_complete_type (location_t loc, tree value)
201 tree type = TREE_TYPE (value);
203 if (error_operand_p (value))
204 return error_mark_node;
206 /* First, detect a valid value with a complete type. */
207 if (COMPLETE_TYPE_P (type))
208 return value;
210 c_incomplete_type_error (loc, value, type);
211 return error_mark_node;
214 /* Print an error message for invalid use of an incomplete type.
215 VALUE is the expression that was used (or 0 if that isn't known)
216 and TYPE is the type that was invalid. LOC is the location for
217 the error. */
219 void
220 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
222 /* Avoid duplicate error message. */
223 if (TREE_CODE (type) == ERROR_MARK)
224 return;
226 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
227 error_at (loc, "%qD has an incomplete type %qT", value, type);
228 else
230 retry:
231 /* We must print an error message. Be clever about what it says. */
233 switch (TREE_CODE (type))
235 case RECORD_TYPE:
236 case UNION_TYPE:
237 case ENUMERAL_TYPE:
238 break;
240 case VOID_TYPE:
241 error_at (loc, "invalid use of void expression");
242 return;
244 case ARRAY_TYPE:
245 if (TYPE_DOMAIN (type))
247 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
249 error_at (loc, "invalid use of flexible array member");
250 return;
252 type = TREE_TYPE (type);
253 goto retry;
255 error_at (loc, "invalid use of array with unspecified bounds");
256 return;
258 default:
259 gcc_unreachable ();
262 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
263 error_at (loc, "invalid use of undefined type %qT", type);
264 else
265 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
266 error_at (loc, "invalid use of incomplete typedef %qT", type);
270 /* Given a type, apply default promotions wrt unnamed function
271 arguments and return the new type. */
273 tree
274 c_type_promotes_to (tree type)
276 tree ret = NULL_TREE;
278 if (TYPE_MAIN_VARIANT (type) == float_type_node)
279 ret = double_type_node;
280 else if (c_promoting_integer_type_p (type))
282 /* Preserve unsignedness if not really getting any wider. */
283 if (TYPE_UNSIGNED (type)
284 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
285 ret = unsigned_type_node;
286 else
287 ret = integer_type_node;
290 if (ret != NULL_TREE)
291 return (TYPE_ATOMIC (type)
292 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
293 : ret);
295 return type;
298 /* Return true if between two named address spaces, whether there is a superset
299 named address space that encompasses both address spaces. If there is a
300 superset, return which address space is the superset. */
302 static bool
303 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
305 if (as1 == as2)
307 *common = as1;
308 return true;
310 else if (targetm.addr_space.subset_p (as1, as2))
312 *common = as2;
313 return true;
315 else if (targetm.addr_space.subset_p (as2, as1))
317 *common = as1;
318 return true;
320 else
321 return false;
324 /* Return a variant of TYPE which has all the type qualifiers of LIKE
325 as well as those of TYPE. */
327 static tree
328 qualify_type (tree type, tree like)
330 addr_space_t as_type = TYPE_ADDR_SPACE (type);
331 addr_space_t as_like = TYPE_ADDR_SPACE (like);
332 addr_space_t as_common;
334 /* If the two named address spaces are different, determine the common
335 superset address space. If there isn't one, raise an error. */
336 if (!addr_space_superset (as_type, as_like, &as_common))
338 as_common = as_type;
339 error ("%qT and %qT are in disjoint named address spaces",
340 type, like);
343 return c_build_qualified_type (type,
344 TYPE_QUALS_NO_ADDR_SPACE (type)
345 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
346 | ENCODE_QUAL_ADDR_SPACE (as_common));
349 /* Return true iff the given tree T is a variable length array. */
351 bool
352 c_vla_type_p (const_tree t)
354 if (TREE_CODE (t) == ARRAY_TYPE
355 && C_TYPE_VARIABLE_SIZE (t))
356 return true;
357 return false;
360 /* If NTYPE is a type of a non-variadic function with a prototype
361 and OTYPE is a type of a function without a prototype and ATTRS
362 contains attribute format, diagnosess and removes it from ATTRS.
363 Returns the result of build_type_attribute_variant of NTYPE and
364 the (possibly) modified ATTRS. */
366 static tree
367 build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
369 if (!prototype_p (otype)
370 && prototype_p (ntype)
371 && lookup_attribute ("format", attrs))
373 warning_at (input_location, OPT_Wattributes,
374 "%qs attribute cannot be applied to a function that "
375 "does not take variable arguments", "format");
376 attrs = remove_attribute ("format", attrs);
378 return build_type_attribute_variant (ntype, attrs);
381 /* Return the composite type of two compatible types.
383 We assume that comptypes has already been done and returned
384 nonzero; if that isn't so, this may crash. In particular, we
385 assume that qualifiers match. */
387 tree
388 composite_type (tree t1, tree t2)
390 enum tree_code code1;
391 enum tree_code code2;
392 tree attributes;
394 /* Save time if the two types are the same. */
396 if (t1 == t2) return t1;
398 /* If one type is nonsense, use the other. */
399 if (t1 == error_mark_node)
400 return t2;
401 if (t2 == error_mark_node)
402 return t1;
404 code1 = TREE_CODE (t1);
405 code2 = TREE_CODE (t2);
407 /* Merge the attributes. */
408 attributes = targetm.merge_type_attributes (t1, t2);
410 /* If one is an enumerated type and the other is the compatible
411 integer type, the composite type might be either of the two
412 (DR#013 question 3). For consistency, use the enumerated type as
413 the composite type. */
415 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
416 return t1;
417 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
418 return t2;
420 gcc_assert (code1 == code2);
422 switch (code1)
424 case POINTER_TYPE:
425 /* For two pointers, do this recursively on the target type. */
427 tree pointed_to_1 = TREE_TYPE (t1);
428 tree pointed_to_2 = TREE_TYPE (t2);
429 tree target = composite_type (pointed_to_1, pointed_to_2);
430 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
431 t1 = build_type_attribute_variant (t1, attributes);
432 return qualify_type (t1, t2);
435 case ARRAY_TYPE:
437 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
438 int quals;
439 tree unqual_elt;
440 tree d1 = TYPE_DOMAIN (t1);
441 tree d2 = TYPE_DOMAIN (t2);
442 bool d1_variable, d2_variable;
443 bool d1_zero, d2_zero;
444 bool t1_complete, t2_complete;
446 /* We should not have any type quals on arrays at all. */
447 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
448 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
450 t1_complete = COMPLETE_TYPE_P (t1);
451 t2_complete = COMPLETE_TYPE_P (t2);
453 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
454 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
456 d1_variable = (!d1_zero
457 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
458 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
459 d2_variable = (!d2_zero
460 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
461 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
462 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
463 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
465 /* Save space: see if the result is identical to one of the args. */
466 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
467 && (d2_variable || d2_zero || !d1_variable))
468 return build_type_attribute_variant (t1, attributes);
469 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
470 && (d1_variable || d1_zero || !d2_variable))
471 return build_type_attribute_variant (t2, attributes);
473 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
474 return build_type_attribute_variant (t1, attributes);
475 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
476 return build_type_attribute_variant (t2, attributes);
478 /* Merge the element types, and have a size if either arg has
479 one. We may have qualifiers on the element types. To set
480 up TYPE_MAIN_VARIANT correctly, we need to form the
481 composite of the unqualified types and add the qualifiers
482 back at the end. */
483 quals = TYPE_QUALS (strip_array_types (elt));
484 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
485 t1 = build_array_type (unqual_elt,
486 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
487 && (d2_variable
488 || d2_zero
489 || !d1_variable))
490 ? t1
491 : t2));
492 /* Ensure a composite type involving a zero-length array type
493 is a zero-length type not an incomplete type. */
494 if (d1_zero && d2_zero
495 && (t1_complete || t2_complete)
496 && !COMPLETE_TYPE_P (t1))
498 TYPE_SIZE (t1) = bitsize_zero_node;
499 TYPE_SIZE_UNIT (t1) = size_zero_node;
501 t1 = c_build_qualified_type (t1, quals);
502 return build_type_attribute_variant (t1, attributes);
505 case ENUMERAL_TYPE:
506 case RECORD_TYPE:
507 case UNION_TYPE:
508 if (attributes != NULL)
510 /* Try harder not to create a new aggregate type. */
511 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
512 return t1;
513 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
514 return t2;
516 return build_type_attribute_variant (t1, attributes);
518 case FUNCTION_TYPE:
519 /* Function types: prefer the one that specified arg types.
520 If both do, merge the arg types. Also merge the return types. */
522 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
523 tree p1 = TYPE_ARG_TYPES (t1);
524 tree p2 = TYPE_ARG_TYPES (t2);
525 int len;
526 tree newargs, n;
527 int i;
529 /* Save space: see if the result is identical to one of the args. */
530 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
531 return build_functype_attribute_variant (t1, t2, attributes);
532 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
533 return build_functype_attribute_variant (t2, t1, attributes);
535 /* Simple way if one arg fails to specify argument types. */
536 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
538 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
539 t1 = build_type_attribute_variant (t1, attributes);
540 return qualify_type (t1, t2);
542 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
544 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
545 t1 = build_type_attribute_variant (t1, attributes);
546 return qualify_type (t1, t2);
549 /* If both args specify argument types, we must merge the two
550 lists, argument by argument. */
552 for (len = 0, newargs = p1;
553 newargs && newargs != void_list_node;
554 len++, newargs = TREE_CHAIN (newargs))
557 for (i = 0; i < len; i++)
558 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
560 n = newargs;
562 for (; p1 && p1 != void_list_node;
563 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
565 /* A null type means arg type is not specified.
566 Take whatever the other function type has. */
567 if (TREE_VALUE (p1) == NULL_TREE)
569 TREE_VALUE (n) = TREE_VALUE (p2);
570 goto parm_done;
572 if (TREE_VALUE (p2) == NULL_TREE)
574 TREE_VALUE (n) = TREE_VALUE (p1);
575 goto parm_done;
578 /* Given wait (union {union wait *u; int *i} *)
579 and wait (union wait *),
580 prefer union wait * as type of parm. */
581 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
582 && TREE_VALUE (p1) != TREE_VALUE (p2))
584 tree memb;
585 tree mv2 = TREE_VALUE (p2);
586 if (mv2 && mv2 != error_mark_node
587 && TREE_CODE (mv2) != ARRAY_TYPE)
588 mv2 = TYPE_MAIN_VARIANT (mv2);
589 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
590 memb; memb = DECL_CHAIN (memb))
592 tree mv3 = TREE_TYPE (memb);
593 if (mv3 && mv3 != error_mark_node
594 && TREE_CODE (mv3) != ARRAY_TYPE)
595 mv3 = TYPE_MAIN_VARIANT (mv3);
596 if (comptypes (mv3, mv2))
598 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
599 TREE_VALUE (p2));
600 pedwarn (input_location, OPT_Wpedantic,
601 "function types not truly compatible in ISO C");
602 goto parm_done;
606 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
607 && TREE_VALUE (p2) != TREE_VALUE (p1))
609 tree memb;
610 tree mv1 = TREE_VALUE (p1);
611 if (mv1 && mv1 != error_mark_node
612 && TREE_CODE (mv1) != ARRAY_TYPE)
613 mv1 = TYPE_MAIN_VARIANT (mv1);
614 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
615 memb; memb = DECL_CHAIN (memb))
617 tree mv3 = TREE_TYPE (memb);
618 if (mv3 && mv3 != error_mark_node
619 && TREE_CODE (mv3) != ARRAY_TYPE)
620 mv3 = TYPE_MAIN_VARIANT (mv3);
621 if (comptypes (mv3, mv1))
623 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
624 TREE_VALUE (p1));
625 pedwarn (input_location, OPT_Wpedantic,
626 "function types not truly compatible in ISO C");
627 goto parm_done;
631 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
632 parm_done: ;
635 t1 = build_function_type (valtype, newargs);
636 t1 = qualify_type (t1, t2);
638 /* FALLTHRU */
640 default:
641 return build_type_attribute_variant (t1, attributes);
646 /* Return the type of a conditional expression between pointers to
647 possibly differently qualified versions of compatible types.
649 We assume that comp_target_types has already been done and returned
650 nonzero; if that isn't so, this may crash. */
652 static tree
653 common_pointer_type (tree t1, tree t2)
655 tree attributes;
656 tree pointed_to_1, mv1;
657 tree pointed_to_2, mv2;
658 tree target;
659 unsigned target_quals;
660 addr_space_t as1, as2, as_common;
661 int quals1, quals2;
663 /* Save time if the two types are the same. */
665 if (t1 == t2) return t1;
667 /* If one type is nonsense, use the other. */
668 if (t1 == error_mark_node)
669 return t2;
670 if (t2 == error_mark_node)
671 return t1;
673 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
674 && TREE_CODE (t2) == POINTER_TYPE);
676 /* Merge the attributes. */
677 attributes = targetm.merge_type_attributes (t1, t2);
679 /* Find the composite type of the target types, and combine the
680 qualifiers of the two types' targets. Do not lose qualifiers on
681 array element types by taking the TYPE_MAIN_VARIANT. */
682 mv1 = pointed_to_1 = TREE_TYPE (t1);
683 mv2 = pointed_to_2 = TREE_TYPE (t2);
684 if (TREE_CODE (mv1) != ARRAY_TYPE)
685 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
686 if (TREE_CODE (mv2) != ARRAY_TYPE)
687 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
688 target = composite_type (mv1, mv2);
690 /* Strip array types to get correct qualifier for pointers to arrays */
691 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
692 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
694 /* For function types do not merge const qualifiers, but drop them
695 if used inconsistently. The middle-end uses these to mark const
696 and noreturn functions. */
697 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
698 target_quals = (quals1 & quals2);
699 else
700 target_quals = (quals1 | quals2);
702 /* If the two named address spaces are different, determine the common
703 superset address space. This is guaranteed to exist due to the
704 assumption that comp_target_type returned non-zero. */
705 as1 = TYPE_ADDR_SPACE (pointed_to_1);
706 as2 = TYPE_ADDR_SPACE (pointed_to_2);
707 if (!addr_space_superset (as1, as2, &as_common))
708 gcc_unreachable ();
710 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
712 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
713 return build_type_attribute_variant (t1, attributes);
716 /* Return the common type for two arithmetic types under the usual
717 arithmetic conversions. The default conversions have already been
718 applied, and enumerated types converted to their compatible integer
719 types. The resulting type is unqualified and has no attributes.
721 This is the type for the result of most arithmetic operations
722 if the operands have the given two types. */
724 static tree
725 c_common_type (tree t1, tree t2)
727 enum tree_code code1;
728 enum tree_code code2;
730 /* If one type is nonsense, use the other. */
731 if (t1 == error_mark_node)
732 return t2;
733 if (t2 == error_mark_node)
734 return t1;
736 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
737 t1 = TYPE_MAIN_VARIANT (t1);
739 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
740 t2 = TYPE_MAIN_VARIANT (t2);
742 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
744 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
745 t1 = build_type_attribute_variant (t1, attrs);
748 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
750 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
751 t2 = build_type_attribute_variant (t2, attrs);
754 /* Save time if the two types are the same. */
756 if (t1 == t2) return t1;
758 code1 = TREE_CODE (t1);
759 code2 = TREE_CODE (t2);
761 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
762 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
763 || code1 == INTEGER_TYPE);
764 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
765 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
766 || code2 == INTEGER_TYPE);
768 /* When one operand is a decimal float type, the other operand cannot be
769 a generic float type or a complex type. We also disallow vector types
770 here. */
771 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
772 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
774 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
776 error ("cannot mix operands of decimal floating and vector types");
777 return error_mark_node;
779 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
781 error ("cannot mix operands of decimal floating and complex types");
782 return error_mark_node;
784 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
786 error ("cannot mix operands of decimal floating "
787 "and other floating types");
788 return error_mark_node;
792 /* If one type is a vector type, return that type. (How the usual
793 arithmetic conversions apply to the vector types extension is not
794 precisely specified.) */
795 if (code1 == VECTOR_TYPE)
796 return t1;
798 if (code2 == VECTOR_TYPE)
799 return t2;
801 /* If one type is complex, form the common type of the non-complex
802 components, then make that complex. Use T1 or T2 if it is the
803 required type. */
804 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
806 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
807 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
808 tree subtype = c_common_type (subtype1, subtype2);
810 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
811 return t1;
812 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
813 return t2;
814 else
815 return build_complex_type (subtype);
818 /* If only one is real, use it as the result. */
820 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
821 return t1;
823 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
824 return t2;
826 /* If both are real and either are decimal floating point types, use
827 the decimal floating point type with the greater precision. */
829 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
831 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
832 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
833 return dfloat128_type_node;
834 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
835 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
836 return dfloat64_type_node;
837 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
838 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
839 return dfloat32_type_node;
842 /* Deal with fixed-point types. */
843 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
845 unsigned int unsignedp = 0, satp = 0;
846 scalar_mode m1, m2;
847 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
849 m1 = SCALAR_TYPE_MODE (t1);
850 m2 = SCALAR_TYPE_MODE (t2);
852 /* If one input type is saturating, the result type is saturating. */
853 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
854 satp = 1;
856 /* If both fixed-point types are unsigned, the result type is unsigned.
857 When mixing fixed-point and integer types, follow the sign of the
858 fixed-point type.
859 Otherwise, the result type is signed. */
860 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
861 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
862 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
863 && TYPE_UNSIGNED (t1))
864 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
865 && TYPE_UNSIGNED (t2)))
866 unsignedp = 1;
868 /* The result type is signed. */
869 if (unsignedp == 0)
871 /* If the input type is unsigned, we need to convert to the
872 signed type. */
873 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
875 enum mode_class mclass = (enum mode_class) 0;
876 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
877 mclass = MODE_FRACT;
878 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
879 mclass = MODE_ACCUM;
880 else
881 gcc_unreachable ();
882 m1 = as_a <scalar_mode>
883 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
885 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
887 enum mode_class mclass = (enum mode_class) 0;
888 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
889 mclass = MODE_FRACT;
890 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
891 mclass = MODE_ACCUM;
892 else
893 gcc_unreachable ();
894 m2 = as_a <scalar_mode>
895 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
899 if (code1 == FIXED_POINT_TYPE)
901 fbit1 = GET_MODE_FBIT (m1);
902 ibit1 = GET_MODE_IBIT (m1);
904 else
906 fbit1 = 0;
907 /* Signed integers need to subtract one sign bit. */
908 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
911 if (code2 == FIXED_POINT_TYPE)
913 fbit2 = GET_MODE_FBIT (m2);
914 ibit2 = GET_MODE_IBIT (m2);
916 else
918 fbit2 = 0;
919 /* Signed integers need to subtract one sign bit. */
920 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
923 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
924 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
925 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
926 satp);
929 /* Both real or both integers; use the one with greater precision. */
931 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
932 return t1;
933 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
934 return t2;
936 /* Same precision. Prefer long longs to longs to ints when the
937 same precision, following the C99 rules on integer type rank
938 (which are equivalent to the C90 rules for C90 types). */
940 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
941 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
942 return long_long_unsigned_type_node;
944 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
945 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
947 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
948 return long_long_unsigned_type_node;
949 else
950 return long_long_integer_type_node;
953 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
954 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
955 return long_unsigned_type_node;
957 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
958 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
960 /* But preserve unsignedness from the other type,
961 since long cannot hold all the values of an unsigned int. */
962 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
963 return long_unsigned_type_node;
964 else
965 return long_integer_type_node;
968 /* For floating types of the same TYPE_PRECISION (which we here
969 assume means either the same set of values, or sets of values
970 neither a subset of the other, with behavior being undefined in
971 the latter case), follow the rules from TS 18661-3: prefer
972 interchange types _FloatN, then standard types long double,
973 double, float, then extended types _FloatNx. For extended types,
974 check them starting with _Float128x as that seems most consistent
975 in spirit with preferring long double to double; for interchange
976 types, also check in that order for consistency although it's not
977 possible for more than one of them to have the same
978 precision. */
979 tree mv1 = TYPE_MAIN_VARIANT (t1);
980 tree mv2 = TYPE_MAIN_VARIANT (t2);
982 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
983 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
984 return FLOATN_TYPE_NODE (i);
986 /* Likewise, prefer long double to double even if same size. */
987 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
988 return long_double_type_node;
990 /* Likewise, prefer double to float even if same size.
991 We got a couple of embedded targets with 32 bit doubles, and the
992 pdp11 might have 64 bit floats. */
993 if (mv1 == double_type_node || mv2 == double_type_node)
994 return double_type_node;
996 if (mv1 == float_type_node || mv2 == float_type_node)
997 return float_type_node;
999 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1000 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1001 return FLOATNX_TYPE_NODE (i);
1003 /* Otherwise prefer the unsigned one. */
1005 if (TYPE_UNSIGNED (t1))
1006 return t1;
1007 else
1008 return t2;
1011 /* Wrapper around c_common_type that is used by c-common.c and other
1012 front end optimizations that remove promotions. ENUMERAL_TYPEs
1013 are allowed here and are converted to their compatible integer types.
1014 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1015 preferably a non-Boolean type as the common type. */
1016 tree
1017 common_type (tree t1, tree t2)
1019 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1020 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
1021 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1022 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
1024 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1025 if (TREE_CODE (t1) == BOOLEAN_TYPE
1026 && TREE_CODE (t2) == BOOLEAN_TYPE)
1027 return boolean_type_node;
1029 /* If either type is BOOLEAN_TYPE, then return the other. */
1030 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1031 return t2;
1032 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1033 return t1;
1035 return c_common_type (t1, t2);
1038 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1039 or various other operations. Return 2 if they are compatible
1040 but a warning may be needed if you use them together. */
1043 comptypes (tree type1, tree type2)
1045 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1046 int val;
1048 val = comptypes_internal (type1, type2, NULL, NULL);
1049 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1051 return val;
1054 /* Like comptypes, but if it returns non-zero because enum and int are
1055 compatible, it sets *ENUM_AND_INT_P to true. */
1057 static int
1058 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1060 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1061 int val;
1063 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1064 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1066 return val;
1069 /* Like comptypes, but if it returns nonzero for different types, it
1070 sets *DIFFERENT_TYPES_P to true. */
1073 comptypes_check_different_types (tree type1, tree type2,
1074 bool *different_types_p)
1076 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1077 int val;
1079 val = comptypes_internal (type1, type2, NULL, different_types_p);
1080 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1082 return val;
1085 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1086 or various other operations. Return 2 if they are compatible
1087 but a warning may be needed if you use them together. If
1088 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1089 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1090 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1091 NULL, and the types are compatible but different enough not to be
1092 permitted in C11 typedef redeclarations, then this sets
1093 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1094 false, but may or may not be set if the types are incompatible.
1095 This differs from comptypes, in that we don't free the seen
1096 types. */
1098 static int
1099 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1100 bool *different_types_p)
1102 const_tree t1 = type1;
1103 const_tree t2 = type2;
1104 int attrval, val;
1106 /* Suppress errors caused by previously reported errors. */
1108 if (t1 == t2 || !t1 || !t2
1109 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1110 return 1;
1112 /* Enumerated types are compatible with integer types, but this is
1113 not transitive: two enumerated types in the same translation unit
1114 are compatible with each other only if they are the same type. */
1116 if (TREE_CODE (t1) == ENUMERAL_TYPE
1117 && COMPLETE_TYPE_P (t1)
1118 && TREE_CODE (t2) != ENUMERAL_TYPE)
1120 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1121 if (TREE_CODE (t2) != VOID_TYPE)
1123 if (enum_and_int_p != NULL)
1124 *enum_and_int_p = true;
1125 if (different_types_p != NULL)
1126 *different_types_p = true;
1129 else if (TREE_CODE (t2) == ENUMERAL_TYPE
1130 && COMPLETE_TYPE_P (t2)
1131 && TREE_CODE (t1) != ENUMERAL_TYPE)
1133 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1134 if (TREE_CODE (t1) != VOID_TYPE)
1136 if (enum_and_int_p != NULL)
1137 *enum_and_int_p = true;
1138 if (different_types_p != NULL)
1139 *different_types_p = true;
1143 if (t1 == t2)
1144 return 1;
1146 /* Different classes of types can't be compatible. */
1148 if (TREE_CODE (t1) != TREE_CODE (t2))
1149 return 0;
1151 /* Qualifiers must match. C99 6.7.3p9 */
1153 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1154 return 0;
1156 /* Allow for two different type nodes which have essentially the same
1157 definition. Note that we already checked for equality of the type
1158 qualifiers (just above). */
1160 if (TREE_CODE (t1) != ARRAY_TYPE
1161 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1162 return 1;
1164 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1165 if (!(attrval = comp_type_attributes (t1, t2)))
1166 return 0;
1168 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1169 val = 0;
1171 switch (TREE_CODE (t1))
1173 case INTEGER_TYPE:
1174 case FIXED_POINT_TYPE:
1175 case REAL_TYPE:
1176 /* With these nodes, we can't determine type equivalence by
1177 looking at what is stored in the nodes themselves, because
1178 two nodes might have different TYPE_MAIN_VARIANTs but still
1179 represent the same type. For example, wchar_t and int could
1180 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1181 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1182 and are distinct types. On the other hand, int and the
1183 following typedef
1185 typedef int INT __attribute((may_alias));
1187 have identical properties, different TYPE_MAIN_VARIANTs, but
1188 represent the same type. The canonical type system keeps
1189 track of equivalence in this case, so we fall back on it. */
1190 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1192 case POINTER_TYPE:
1193 /* Do not remove mode information. */
1194 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1195 break;
1196 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1197 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1198 enum_and_int_p, different_types_p));
1199 break;
1201 case FUNCTION_TYPE:
1202 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1203 different_types_p);
1204 break;
1206 case ARRAY_TYPE:
1208 tree d1 = TYPE_DOMAIN (t1);
1209 tree d2 = TYPE_DOMAIN (t2);
1210 bool d1_variable, d2_variable;
1211 bool d1_zero, d2_zero;
1212 val = 1;
1214 /* Target types must match incl. qualifiers. */
1215 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1216 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1217 enum_and_int_p,
1218 different_types_p)) == 0)
1219 return 0;
1221 if (different_types_p != NULL
1222 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1223 *different_types_p = true;
1224 /* Sizes must match unless one is missing or variable. */
1225 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1226 break;
1228 d1_zero = !TYPE_MAX_VALUE (d1);
1229 d2_zero = !TYPE_MAX_VALUE (d2);
1231 d1_variable = (!d1_zero
1232 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1233 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1234 d2_variable = (!d2_zero
1235 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1236 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1237 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1238 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1240 if (different_types_p != NULL
1241 && d1_variable != d2_variable)
1242 *different_types_p = true;
1243 if (d1_variable || d2_variable)
1244 break;
1245 if (d1_zero && d2_zero)
1246 break;
1247 if (d1_zero || d2_zero
1248 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1249 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1250 val = 0;
1252 break;
1255 case ENUMERAL_TYPE:
1256 case RECORD_TYPE:
1257 case UNION_TYPE:
1258 if (val != 1 && !same_translation_unit_p (t1, t2))
1260 tree a1 = TYPE_ATTRIBUTES (t1);
1261 tree a2 = TYPE_ATTRIBUTES (t2);
1263 if (! attribute_list_contained (a1, a2)
1264 && ! attribute_list_contained (a2, a1))
1265 break;
1267 if (attrval != 2)
1268 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1269 different_types_p);
1270 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1271 different_types_p);
1273 break;
1275 case VECTOR_TYPE:
1276 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1277 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1278 enum_and_int_p, different_types_p));
1279 break;
1281 default:
1282 break;
1284 return attrval == 2 && val == 1 ? 2 : val;
1287 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1288 their qualifiers, except for named address spaces. If the pointers point to
1289 different named addresses, then we must determine if one address space is a
1290 subset of the other. */
1292 static int
1293 comp_target_types (location_t location, tree ttl, tree ttr)
1295 int val;
1296 int val_ped;
1297 tree mvl = TREE_TYPE (ttl);
1298 tree mvr = TREE_TYPE (ttr);
1299 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1300 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1301 addr_space_t as_common;
1302 bool enum_and_int_p;
1304 /* Fail if pointers point to incompatible address spaces. */
1305 if (!addr_space_superset (asl, asr, &as_common))
1306 return 0;
1308 /* For pedantic record result of comptypes on arrays before losing
1309 qualifiers on the element type below. */
1310 val_ped = 1;
1312 if (TREE_CODE (mvl) == ARRAY_TYPE
1313 && TREE_CODE (mvr) == ARRAY_TYPE)
1314 val_ped = comptypes (mvl, mvr);
1316 /* Qualifiers on element types of array types that are
1317 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1319 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1320 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1321 : TYPE_MAIN_VARIANT (mvl));
1323 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1324 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1325 : TYPE_MAIN_VARIANT (mvr));
1327 enum_and_int_p = false;
1328 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1330 if (val == 1 && val_ped != 1)
1331 pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1332 "in ISO C before C2X");
1334 if (val == 2)
1335 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1337 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1338 warning_at (location, OPT_Wc___compat,
1339 "pointer target types incompatible in C++");
1341 return val;
1344 /* Subroutines of `comptypes'. */
1346 /* Determine whether two trees derive from the same translation unit.
1347 If the CONTEXT chain ends in a null, that tree's context is still
1348 being parsed, so if two trees have context chains ending in null,
1349 they're in the same translation unit. */
1351 bool
1352 same_translation_unit_p (const_tree t1, const_tree t2)
1354 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1355 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1357 case tcc_declaration:
1358 t1 = DECL_CONTEXT (t1); break;
1359 case tcc_type:
1360 t1 = TYPE_CONTEXT (t1); break;
1361 case tcc_exceptional:
1362 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1363 default: gcc_unreachable ();
1366 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1367 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1369 case tcc_declaration:
1370 t2 = DECL_CONTEXT (t2); break;
1371 case tcc_type:
1372 t2 = TYPE_CONTEXT (t2); break;
1373 case tcc_exceptional:
1374 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1375 default: gcc_unreachable ();
1378 return t1 == t2;
1381 /* Allocate the seen two types, assuming that they are compatible. */
1383 static struct tagged_tu_seen_cache *
1384 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1386 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1387 tu->next = tagged_tu_seen_base;
1388 tu->t1 = t1;
1389 tu->t2 = t2;
1391 tagged_tu_seen_base = tu;
1393 /* The C standard says that two structures in different translation
1394 units are compatible with each other only if the types of their
1395 fields are compatible (among other things). We assume that they
1396 are compatible until proven otherwise when building the cache.
1397 An example where this can occur is:
1398 struct a
1400 struct a *next;
1402 If we are comparing this against a similar struct in another TU,
1403 and did not assume they were compatible, we end up with an infinite
1404 loop. */
1405 tu->val = 1;
1406 return tu;
1409 /* Free the seen types until we get to TU_TIL. */
1411 static void
1412 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1414 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1415 while (tu != tu_til)
1417 const struct tagged_tu_seen_cache *const tu1
1418 = (const struct tagged_tu_seen_cache *) tu;
1419 tu = tu1->next;
1420 XDELETE (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1422 tagged_tu_seen_base = tu_til;
1425 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1426 compatible. If the two types are not the same (which has been
1427 checked earlier), this can only happen when multiple translation
1428 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1429 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1430 comptypes_internal. */
1432 static int
1433 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1434 bool *enum_and_int_p, bool *different_types_p)
1436 tree s1, s2;
1437 bool needs_warning = false;
1439 /* We have to verify that the tags of the types are the same. This
1440 is harder than it looks because this may be a typedef, so we have
1441 to go look at the original type. It may even be a typedef of a
1442 typedef...
1443 In the case of compiler-created builtin structs the TYPE_DECL
1444 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1445 while (TYPE_NAME (t1)
1446 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1447 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1448 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1450 while (TYPE_NAME (t2)
1451 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1452 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1453 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1455 /* C90 didn't have the requirement that the two tags be the same. */
1456 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1457 return 0;
1459 /* C90 didn't say what happened if one or both of the types were
1460 incomplete; we choose to follow C99 rules here, which is that they
1461 are compatible. */
1462 if (TYPE_SIZE (t1) == NULL
1463 || TYPE_SIZE (t2) == NULL)
1464 return 1;
1467 const struct tagged_tu_seen_cache * tts_i;
1468 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1469 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1470 return tts_i->val;
1473 switch (TREE_CODE (t1))
1475 case ENUMERAL_TYPE:
1477 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1478 /* Speed up the case where the type values are in the same order. */
1479 tree tv1 = TYPE_VALUES (t1);
1480 tree tv2 = TYPE_VALUES (t2);
1482 if (tv1 == tv2)
1484 return 1;
1487 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1489 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1490 break;
1491 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1493 tu->val = 0;
1494 return 0;
1498 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1500 return 1;
1502 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1504 tu->val = 0;
1505 return 0;
1508 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1510 tu->val = 0;
1511 return 0;
1514 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1516 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1517 if (s2 == NULL
1518 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1520 tu->val = 0;
1521 return 0;
1524 return 1;
1527 case UNION_TYPE:
1529 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1530 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1532 tu->val = 0;
1533 return 0;
1536 /* Speed up the common case where the fields are in the same order. */
1537 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1538 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1540 int result;
1542 if (DECL_NAME (s1) != DECL_NAME (s2))
1543 break;
1544 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1545 enum_and_int_p, different_types_p);
1547 if (result != 1 && !DECL_NAME (s1))
1548 break;
1549 if (result == 0)
1551 tu->val = 0;
1552 return 0;
1554 if (result == 2)
1555 needs_warning = true;
1557 if (TREE_CODE (s1) == FIELD_DECL
1558 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1559 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1561 tu->val = 0;
1562 return 0;
1565 if (!s1 && !s2)
1567 tu->val = needs_warning ? 2 : 1;
1568 return tu->val;
1571 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1573 bool ok = false;
1575 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1576 if (DECL_NAME (s1) == DECL_NAME (s2))
1578 int result;
1580 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1581 enum_and_int_p,
1582 different_types_p);
1584 if (result != 1 && !DECL_NAME (s1))
1585 continue;
1586 if (result == 0)
1588 tu->val = 0;
1589 return 0;
1591 if (result == 2)
1592 needs_warning = true;
1594 if (TREE_CODE (s1) == FIELD_DECL
1595 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1596 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1597 break;
1599 ok = true;
1600 break;
1602 if (!ok)
1604 tu->val = 0;
1605 return 0;
1608 tu->val = needs_warning ? 2 : 10;
1609 return tu->val;
1612 case RECORD_TYPE:
1614 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1616 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1617 s1 && s2;
1618 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1620 int result;
1621 if (TREE_CODE (s1) != TREE_CODE (s2)
1622 || DECL_NAME (s1) != DECL_NAME (s2))
1623 break;
1624 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1625 enum_and_int_p, different_types_p);
1626 if (result == 0)
1627 break;
1628 if (result == 2)
1629 needs_warning = true;
1631 if (TREE_CODE (s1) == FIELD_DECL
1632 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1633 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1634 break;
1636 if (s1 && s2)
1637 tu->val = 0;
1638 else
1639 tu->val = needs_warning ? 2 : 1;
1640 return tu->val;
1643 default:
1644 gcc_unreachable ();
1648 /* Return 1 if two function types F1 and F2 are compatible.
1649 If either type specifies no argument types,
1650 the other must specify a fixed number of self-promoting arg types.
1651 Otherwise, if one type specifies only the number of arguments,
1652 the other must specify that number of self-promoting arg types.
1653 Otherwise, the argument types must match.
1654 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1656 static int
1657 function_types_compatible_p (const_tree f1, const_tree f2,
1658 bool *enum_and_int_p, bool *different_types_p)
1660 tree args1, args2;
1661 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1662 int val = 1;
1663 int val1;
1664 tree ret1, ret2;
1666 ret1 = TREE_TYPE (f1);
1667 ret2 = TREE_TYPE (f2);
1669 /* 'volatile' qualifiers on a function's return type used to mean
1670 the function is noreturn. */
1671 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1672 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1673 if (TYPE_VOLATILE (ret1))
1674 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1675 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1676 if (TYPE_VOLATILE (ret2))
1677 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1678 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1679 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1680 if (val == 0)
1681 return 0;
1683 args1 = TYPE_ARG_TYPES (f1);
1684 args2 = TYPE_ARG_TYPES (f2);
1686 if (different_types_p != NULL
1687 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1688 *different_types_p = true;
1690 /* An unspecified parmlist matches any specified parmlist
1691 whose argument types don't need default promotions. */
1693 if (args1 == NULL_TREE)
1695 if (flag_isoc2x ? stdarg_p (f2) : !self_promoting_args_p (args2))
1696 return 0;
1697 /* If one of these types comes from a non-prototype fn definition,
1698 compare that with the other type's arglist.
1699 If they don't match, ask for a warning (but no error). */
1700 if (TYPE_ACTUAL_ARG_TYPES (f1)
1701 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1702 enum_and_int_p, different_types_p) != 1)
1703 val = 2;
1704 return val;
1706 if (args2 == NULL_TREE)
1708 if (flag_isoc2x ? stdarg_p (f1) : !self_promoting_args_p (args1))
1709 return 0;
1710 if (TYPE_ACTUAL_ARG_TYPES (f2)
1711 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1712 enum_and_int_p, different_types_p) != 1)
1713 val = 2;
1714 return val;
1717 /* Both types have argument lists: compare them and propagate results. */
1718 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1719 different_types_p);
1720 return val1 != 1 ? val1 : val;
1723 /* Check two lists of types for compatibility, returning 0 for
1724 incompatible, 1 for compatible, or 2 for compatible with
1725 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1726 comptypes_internal. */
1728 static int
1729 type_lists_compatible_p (const_tree args1, const_tree args2,
1730 bool *enum_and_int_p, bool *different_types_p)
1732 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1733 int val = 1;
1734 int newval = 0;
1736 while (1)
1738 tree a1, mv1, a2, mv2;
1739 if (args1 == NULL_TREE && args2 == NULL_TREE)
1740 return val;
1741 /* If one list is shorter than the other,
1742 they fail to match. */
1743 if (args1 == NULL_TREE || args2 == NULL_TREE)
1744 return 0;
1745 mv1 = a1 = TREE_VALUE (args1);
1746 mv2 = a2 = TREE_VALUE (args2);
1747 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1748 mv1 = (TYPE_ATOMIC (mv1)
1749 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1750 TYPE_QUAL_ATOMIC)
1751 : TYPE_MAIN_VARIANT (mv1));
1752 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1753 mv2 = (TYPE_ATOMIC (mv2)
1754 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1755 TYPE_QUAL_ATOMIC)
1756 : TYPE_MAIN_VARIANT (mv2));
1757 /* A null pointer instead of a type
1758 means there is supposed to be an argument
1759 but nothing is specified about what type it has.
1760 So match anything that self-promotes. */
1761 if (different_types_p != NULL
1762 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1763 *different_types_p = true;
1764 if (a1 == NULL_TREE)
1766 if (c_type_promotes_to (a2) != a2)
1767 return 0;
1769 else if (a2 == NULL_TREE)
1771 if (c_type_promotes_to (a1) != a1)
1772 return 0;
1774 /* If one of the lists has an error marker, ignore this arg. */
1775 else if (TREE_CODE (a1) == ERROR_MARK
1776 || TREE_CODE (a2) == ERROR_MARK)
1778 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1779 different_types_p)))
1781 if (different_types_p != NULL)
1782 *different_types_p = true;
1783 /* Allow wait (union {union wait *u; int *i} *)
1784 and wait (union wait *) to be compatible. */
1785 if (TREE_CODE (a1) == UNION_TYPE
1786 && (TYPE_NAME (a1) == NULL_TREE
1787 || TYPE_TRANSPARENT_AGGR (a1))
1788 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1789 && tree_int_cst_equal (TYPE_SIZE (a1),
1790 TYPE_SIZE (a2)))
1792 tree memb;
1793 for (memb = TYPE_FIELDS (a1);
1794 memb; memb = DECL_CHAIN (memb))
1796 tree mv3 = TREE_TYPE (memb);
1797 if (mv3 && mv3 != error_mark_node
1798 && TREE_CODE (mv3) != ARRAY_TYPE)
1799 mv3 = (TYPE_ATOMIC (mv3)
1800 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1801 TYPE_QUAL_ATOMIC)
1802 : TYPE_MAIN_VARIANT (mv3));
1803 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1804 different_types_p))
1805 break;
1807 if (memb == NULL_TREE)
1808 return 0;
1810 else if (TREE_CODE (a2) == UNION_TYPE
1811 && (TYPE_NAME (a2) == NULL_TREE
1812 || TYPE_TRANSPARENT_AGGR (a2))
1813 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1814 && tree_int_cst_equal (TYPE_SIZE (a2),
1815 TYPE_SIZE (a1)))
1817 tree memb;
1818 for (memb = TYPE_FIELDS (a2);
1819 memb; memb = DECL_CHAIN (memb))
1821 tree mv3 = TREE_TYPE (memb);
1822 if (mv3 && mv3 != error_mark_node
1823 && TREE_CODE (mv3) != ARRAY_TYPE)
1824 mv3 = (TYPE_ATOMIC (mv3)
1825 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1826 TYPE_QUAL_ATOMIC)
1827 : TYPE_MAIN_VARIANT (mv3));
1828 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1829 different_types_p))
1830 break;
1832 if (memb == NULL_TREE)
1833 return 0;
1835 else
1836 return 0;
1839 /* comptypes said ok, but record if it said to warn. */
1840 if (newval > val)
1841 val = newval;
1843 args1 = TREE_CHAIN (args1);
1844 args2 = TREE_CHAIN (args2);
1848 /* Compute the size to increment a pointer by. When a function type or void
1849 type or incomplete type is passed, size_one_node is returned.
1850 This function does not emit any diagnostics; the caller is responsible
1851 for that. */
1853 static tree
1854 c_size_in_bytes (const_tree type)
1856 enum tree_code code = TREE_CODE (type);
1858 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1859 || !COMPLETE_TYPE_P (type))
1860 return size_one_node;
1862 /* Convert in case a char is more than one unit. */
1863 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1864 size_int (TYPE_PRECISION (char_type_node)
1865 / BITS_PER_UNIT));
1868 /* Return either DECL or its known constant value (if it has one). */
1870 tree
1871 decl_constant_value_1 (tree decl, bool in_init)
1873 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1874 TREE_CODE (decl) != PARM_DECL
1875 && !TREE_THIS_VOLATILE (decl)
1876 && TREE_READONLY (decl)
1877 && DECL_INITIAL (decl) != NULL_TREE
1878 && !error_operand_p (DECL_INITIAL (decl))
1879 /* This is invalid if initial value is not constant.
1880 If it has either a function call, a memory reference,
1881 or a variable, then re-evaluating it could give different results. */
1882 && TREE_CONSTANT (DECL_INITIAL (decl))
1883 /* Check for cases where this is sub-optimal, even though valid. */
1884 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1885 return DECL_INITIAL (decl);
1886 return decl;
1889 /* Return either DECL or its known constant value (if it has one).
1890 Like the above, but always return decl outside of functions. */
1892 tree
1893 decl_constant_value (tree decl)
1895 /* Don't change a variable array bound or initial value to a constant
1896 in a place where a variable is invalid. */
1897 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1900 /* Convert the array expression EXP to a pointer. */
1901 static tree
1902 array_to_pointer_conversion (location_t loc, tree exp)
1904 tree orig_exp = exp;
1905 tree type = TREE_TYPE (exp);
1906 tree adr;
1907 tree restype = TREE_TYPE (type);
1908 tree ptrtype;
1910 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1912 STRIP_TYPE_NOPS (exp);
1914 copy_warning (exp, orig_exp);
1916 ptrtype = build_pointer_type (restype);
1918 if (INDIRECT_REF_P (exp))
1919 return convert (ptrtype, TREE_OPERAND (exp, 0));
1921 /* In C++ array compound literals are temporary objects unless they are
1922 const or appear in namespace scope, so they are destroyed too soon
1923 to use them for much of anything (c++/53220). */
1924 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1926 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1927 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1928 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1929 "converting an array compound literal to a pointer "
1930 "is ill-formed in C++");
1933 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1934 return convert (ptrtype, adr);
1937 /* Convert the function expression EXP to a pointer. */
1938 static tree
1939 function_to_pointer_conversion (location_t loc, tree exp)
1941 tree orig_exp = exp;
1943 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1945 STRIP_TYPE_NOPS (exp);
1947 copy_warning (exp, orig_exp);
1949 return build_unary_op (loc, ADDR_EXPR, exp, false);
1952 /* Mark EXP as read, not just set, for set but not used -Wunused
1953 warning purposes. */
1955 void
1956 mark_exp_read (tree exp)
1958 switch (TREE_CODE (exp))
1960 case VAR_DECL:
1961 case PARM_DECL:
1962 DECL_READ_P (exp) = 1;
1963 break;
1964 case ARRAY_REF:
1965 case COMPONENT_REF:
1966 case MODIFY_EXPR:
1967 case REALPART_EXPR:
1968 case IMAGPART_EXPR:
1969 CASE_CONVERT:
1970 case ADDR_EXPR:
1971 case VIEW_CONVERT_EXPR:
1972 mark_exp_read (TREE_OPERAND (exp, 0));
1973 break;
1974 case COMPOUND_EXPR:
1975 /* Pattern match what build_atomic_assign produces with modifycode
1976 NOP_EXPR. */
1977 if (VAR_P (TREE_OPERAND (exp, 1))
1978 && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
1979 && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
1981 tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1982 tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
1983 if (TREE_CODE (t1) == TARGET_EXPR
1984 && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
1985 && TREE_CODE (t2) == CALL_EXPR)
1987 tree fndecl = get_callee_fndecl (t2);
1988 tree arg = NULL_TREE;
1989 if (fndecl
1990 && TREE_CODE (fndecl) == FUNCTION_DECL
1991 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1992 && call_expr_nargs (t2) >= 2)
1993 switch (DECL_FUNCTION_CODE (fndecl))
1995 case BUILT_IN_ATOMIC_STORE:
1996 arg = CALL_EXPR_ARG (t2, 1);
1997 break;
1998 case BUILT_IN_ATOMIC_STORE_1:
1999 case BUILT_IN_ATOMIC_STORE_2:
2000 case BUILT_IN_ATOMIC_STORE_4:
2001 case BUILT_IN_ATOMIC_STORE_8:
2002 case BUILT_IN_ATOMIC_STORE_16:
2003 arg = CALL_EXPR_ARG (t2, 0);
2004 break;
2005 default:
2006 break;
2008 if (arg)
2010 STRIP_NOPS (arg);
2011 if (TREE_CODE (arg) == ADDR_EXPR
2012 && DECL_P (TREE_OPERAND (arg, 0))
2013 && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2014 mark_exp_read (TREE_OPERAND (arg, 0));
2018 /* FALLTHRU */
2019 case C_MAYBE_CONST_EXPR:
2020 mark_exp_read (TREE_OPERAND (exp, 1));
2021 break;
2022 default:
2023 break;
2027 /* Perform the default conversion of arrays and functions to pointers.
2028 Return the result of converting EXP. For any other expression, just
2029 return EXP.
2031 LOC is the location of the expression. */
2033 struct c_expr
2034 default_function_array_conversion (location_t loc, struct c_expr exp)
2036 tree orig_exp = exp.value;
2037 tree type = TREE_TYPE (exp.value);
2038 enum tree_code code = TREE_CODE (type);
2040 switch (code)
2042 case ARRAY_TYPE:
2044 bool not_lvalue = false;
2045 bool lvalue_array_p;
2047 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2048 || CONVERT_EXPR_P (exp.value))
2049 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2051 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2052 not_lvalue = true;
2053 exp.value = TREE_OPERAND (exp.value, 0);
2056 copy_warning (exp.value, orig_exp);
2058 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2059 if (!flag_isoc99 && !lvalue_array_p)
2061 /* Before C99, non-lvalue arrays do not decay to pointers.
2062 Normally, using such an array would be invalid; but it can
2063 be used correctly inside sizeof or as a statement expression.
2064 Thus, do not give an error here; an error will result later. */
2065 return exp;
2068 exp.value = array_to_pointer_conversion (loc, exp.value);
2070 break;
2071 case FUNCTION_TYPE:
2072 exp.value = function_to_pointer_conversion (loc, exp.value);
2073 break;
2074 default:
2075 break;
2078 return exp;
2081 struct c_expr
2082 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2084 mark_exp_read (exp.value);
2085 return default_function_array_conversion (loc, exp);
2088 /* Return whether EXPR should be treated as an atomic lvalue for the
2089 purposes of load and store handling. */
2091 static bool
2092 really_atomic_lvalue (tree expr)
2094 if (error_operand_p (expr))
2095 return false;
2096 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2097 return false;
2098 if (!lvalue_p (expr))
2099 return false;
2101 /* Ignore _Atomic on register variables, since their addresses can't
2102 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2103 sequences wouldn't work. Ignore _Atomic on structures containing
2104 bit-fields, since accessing elements of atomic structures or
2105 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2106 it's undefined at translation time or execution time, and the
2107 normal atomic sequences again wouldn't work. */
2108 while (handled_component_p (expr))
2110 if (TREE_CODE (expr) == COMPONENT_REF
2111 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2112 return false;
2113 expr = TREE_OPERAND (expr, 0);
2115 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2116 return false;
2117 return true;
2120 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2121 including converting functions and arrays to pointers if CONVERT_P.
2122 If READ_P, also mark the expression as having been read. */
2124 struct c_expr
2125 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2126 bool convert_p, bool read_p)
2128 if (read_p)
2129 mark_exp_read (exp.value);
2130 if (convert_p)
2131 exp = default_function_array_conversion (loc, exp);
2132 if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2133 exp.value = require_complete_type (loc, exp.value);
2134 if (really_atomic_lvalue (exp.value))
2136 vec<tree, va_gc> *params;
2137 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2138 tree expr_type = TREE_TYPE (exp.value);
2139 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2140 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2142 gcc_assert (TYPE_ATOMIC (expr_type));
2144 /* Expansion of a generic atomic load may require an addition
2145 element, so allocate enough to prevent a resize. */
2146 vec_alloc (params, 4);
2148 /* Remove the qualifiers for the rest of the expressions and
2149 create the VAL temp variable to hold the RHS. */
2150 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2151 tmp = create_tmp_var_raw (nonatomic_type);
2152 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2153 TREE_ADDRESSABLE (tmp) = 1;
2154 /* Do not disable warnings for TMP even though it's artificial.
2155 -Winvalid-memory-model depends on it. */
2157 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2158 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2159 params->quick_push (expr_addr);
2160 params->quick_push (tmp_addr);
2161 params->quick_push (seq_cst);
2162 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2164 /* EXPR is always read. */
2165 mark_exp_read (exp.value);
2167 /* Return tmp which contains the value loaded. */
2168 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2169 NULL_TREE, NULL_TREE);
2171 if (convert_p && !error_operand_p (exp.value)
2172 && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2173 exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2174 return exp;
2177 /* EXP is an expression of integer type. Apply the integer promotions
2178 to it and return the promoted value. */
2180 tree
2181 perform_integral_promotions (tree exp)
2183 tree type = TREE_TYPE (exp);
2184 enum tree_code code = TREE_CODE (type);
2186 gcc_assert (INTEGRAL_TYPE_P (type));
2188 /* Normally convert enums to int,
2189 but convert wide enums to something wider. */
2190 if (code == ENUMERAL_TYPE)
2192 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2193 TYPE_PRECISION (integer_type_node)),
2194 ((TYPE_PRECISION (type)
2195 >= TYPE_PRECISION (integer_type_node))
2196 && TYPE_UNSIGNED (type)));
2198 return convert (type, exp);
2201 /* ??? This should no longer be needed now bit-fields have their
2202 proper types. */
2203 if (TREE_CODE (exp) == COMPONENT_REF
2204 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2205 /* If it's thinner than an int, promote it like a
2206 c_promoting_integer_type_p, otherwise leave it alone. */
2207 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2208 TYPE_PRECISION (integer_type_node)) < 0)
2209 return convert (integer_type_node, exp);
2211 if (c_promoting_integer_type_p (type))
2213 /* Preserve unsignedness if not really getting any wider. */
2214 if (TYPE_UNSIGNED (type)
2215 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2216 return convert (unsigned_type_node, exp);
2218 return convert (integer_type_node, exp);
2221 return exp;
2225 /* Perform default promotions for C data used in expressions.
2226 Enumeral types or short or char are converted to int.
2227 In addition, manifest constants symbols are replaced by their values. */
2229 tree
2230 default_conversion (tree exp)
2232 tree orig_exp;
2233 tree type = TREE_TYPE (exp);
2234 enum tree_code code = TREE_CODE (type);
2235 tree promoted_type;
2237 mark_exp_read (exp);
2239 /* Functions and arrays have been converted during parsing. */
2240 gcc_assert (code != FUNCTION_TYPE);
2241 if (code == ARRAY_TYPE)
2242 return exp;
2244 /* Constants can be used directly unless they're not loadable. */
2245 if (TREE_CODE (exp) == CONST_DECL)
2246 exp = DECL_INITIAL (exp);
2248 /* Strip no-op conversions. */
2249 orig_exp = exp;
2250 STRIP_TYPE_NOPS (exp);
2252 copy_warning (exp, orig_exp);
2254 if (code == VOID_TYPE)
2256 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2257 "void value not ignored as it ought to be");
2258 return error_mark_node;
2261 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2262 if (exp == error_mark_node)
2263 return error_mark_node;
2265 promoted_type = targetm.promoted_type (type);
2266 if (promoted_type)
2267 return convert (promoted_type, exp);
2269 if (INTEGRAL_TYPE_P (type))
2270 return perform_integral_promotions (exp);
2272 return exp;
2275 /* Look up COMPONENT in a structure or union TYPE.
2277 If the component name is not found, returns NULL_TREE. Otherwise,
2278 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2279 stepping down the chain to the component, which is in the last
2280 TREE_VALUE of the list. Normally the list is of length one, but if
2281 the component is embedded within (nested) anonymous structures or
2282 unions, the list steps down the chain to the component. */
2284 static tree
2285 lookup_field (tree type, tree component)
2287 tree field;
2289 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2290 to the field elements. Use a binary search on this array to quickly
2291 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2292 will always be set for structures which have many elements.
2294 Duplicate field checking replaces duplicates with NULL_TREE so
2295 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2296 case just iterate using DECL_CHAIN. */
2298 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2299 && !seen_error ())
2301 int bot, top, half;
2302 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2304 field = TYPE_FIELDS (type);
2305 bot = 0;
2306 top = TYPE_LANG_SPECIFIC (type)->s->len;
2307 while (top - bot > 1)
2309 half = (top - bot + 1) >> 1;
2310 field = field_array[bot+half];
2312 if (DECL_NAME (field) == NULL_TREE)
2314 /* Step through all anon unions in linear fashion. */
2315 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2317 field = field_array[bot++];
2318 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2320 tree anon = lookup_field (TREE_TYPE (field), component);
2322 if (anon)
2323 return tree_cons (NULL_TREE, field, anon);
2325 /* The Plan 9 compiler permits referring
2326 directly to an anonymous struct/union field
2327 using a typedef name. */
2328 if (flag_plan9_extensions
2329 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2330 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2331 == TYPE_DECL)
2332 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2333 == component))
2334 break;
2338 /* Entire record is only anon unions. */
2339 if (bot > top)
2340 return NULL_TREE;
2342 /* Restart the binary search, with new lower bound. */
2343 continue;
2346 if (DECL_NAME (field) == component)
2347 break;
2348 if (DECL_NAME (field) < component)
2349 bot += half;
2350 else
2351 top = bot + half;
2354 if (DECL_NAME (field_array[bot]) == component)
2355 field = field_array[bot];
2356 else if (DECL_NAME (field) != component)
2357 return NULL_TREE;
2359 else
2361 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2363 if (DECL_NAME (field) == NULL_TREE
2364 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2366 tree anon = lookup_field (TREE_TYPE (field), component);
2368 if (anon)
2369 return tree_cons (NULL_TREE, field, anon);
2371 /* The Plan 9 compiler permits referring directly to an
2372 anonymous struct/union field using a typedef
2373 name. */
2374 if (flag_plan9_extensions
2375 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2376 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2377 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2378 == component))
2379 break;
2382 if (DECL_NAME (field) == component)
2383 break;
2386 if (field == NULL_TREE)
2387 return NULL_TREE;
2390 return tree_cons (NULL_TREE, field, NULL_TREE);
2393 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2395 static void
2396 lookup_field_fuzzy_find_candidates (tree type, tree component,
2397 vec<tree> *candidates)
2399 tree field;
2400 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2402 if (DECL_NAME (field) == NULL_TREE
2403 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2404 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2405 candidates);
2407 if (DECL_NAME (field))
2408 candidates->safe_push (DECL_NAME (field));
2412 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2413 rather than returning a TREE_LIST for an exact match. */
2415 static tree
2416 lookup_field_fuzzy (tree type, tree component)
2418 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2420 /* First, gather a list of candidates. */
2421 auto_vec <tree> candidates;
2423 lookup_field_fuzzy_find_candidates (type, component,
2424 &candidates);
2426 return find_closest_identifier (component, &candidates);
2429 /* Support function for build_component_ref's error-handling.
2431 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2432 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2434 static bool
2435 should_suggest_deref_p (tree datum_type)
2437 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2438 allows "." for ptrs; we could be handling a failed attempt
2439 to access a property. */
2440 if (c_dialect_objc ())
2441 return false;
2443 /* Only suggest it for pointers... */
2444 if (TREE_CODE (datum_type) != POINTER_TYPE)
2445 return false;
2447 /* ...to structs/unions. */
2448 tree underlying_type = TREE_TYPE (datum_type);
2449 enum tree_code code = TREE_CODE (underlying_type);
2450 if (code == RECORD_TYPE || code == UNION_TYPE)
2451 return true;
2452 else
2453 return false;
2456 /* Make an expression to refer to the COMPONENT field of structure or
2457 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2458 location of the COMPONENT_REF. COMPONENT_LOC is the location
2459 of COMPONENT. */
2461 tree
2462 build_component_ref (location_t loc, tree datum, tree component,
2463 location_t component_loc)
2465 tree type = TREE_TYPE (datum);
2466 enum tree_code code = TREE_CODE (type);
2467 tree field = NULL;
2468 tree ref;
2469 bool datum_lvalue = lvalue_p (datum);
2471 if (!objc_is_public (datum, component))
2472 return error_mark_node;
2474 /* Detect Objective-C property syntax object.property. */
2475 if (c_dialect_objc ()
2476 && (ref = objc_maybe_build_component_ref (datum, component)))
2477 return ref;
2479 /* See if there is a field or component with name COMPONENT. */
2481 if (code == RECORD_TYPE || code == UNION_TYPE)
2483 if (!COMPLETE_TYPE_P (type))
2485 c_incomplete_type_error (loc, NULL_TREE, type);
2486 return error_mark_node;
2489 field = lookup_field (type, component);
2491 if (!field)
2493 tree guessed_id = lookup_field_fuzzy (type, component);
2494 if (guessed_id)
2496 /* Attempt to provide a fixit replacement hint, if
2497 we have a valid range for the component. */
2498 location_t reported_loc
2499 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2500 gcc_rich_location rich_loc (reported_loc);
2501 if (component_loc != UNKNOWN_LOCATION)
2502 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2503 error_at (&rich_loc,
2504 "%qT has no member named %qE; did you mean %qE?",
2505 type, component, guessed_id);
2507 else
2508 error_at (loc, "%qT has no member named %qE", type, component);
2509 return error_mark_node;
2512 /* Accessing elements of atomic structures or unions is undefined
2513 behavior (C11 6.5.2.3#5). */
2514 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2516 if (code == RECORD_TYPE)
2517 warning_at (loc, 0, "accessing a member %qE of an atomic "
2518 "structure %qE", component, datum);
2519 else
2520 warning_at (loc, 0, "accessing a member %qE of an atomic "
2521 "union %qE", component, datum);
2524 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2525 This might be better solved in future the way the C++ front
2526 end does it - by giving the anonymous entities each a
2527 separate name and type, and then have build_component_ref
2528 recursively call itself. We can't do that here. */
2531 tree subdatum = TREE_VALUE (field);
2532 int quals;
2533 tree subtype;
2534 bool use_datum_quals;
2536 if (TREE_TYPE (subdatum) == error_mark_node)
2537 return error_mark_node;
2539 /* If this is an rvalue, it does not have qualifiers in C
2540 standard terms and we must avoid propagating such
2541 qualifiers down to a non-lvalue array that is then
2542 converted to a pointer. */
2543 use_datum_quals = (datum_lvalue
2544 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2546 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2547 if (use_datum_quals)
2548 quals |= TYPE_QUALS (TREE_TYPE (datum));
2549 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2551 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2552 NULL_TREE);
2553 SET_EXPR_LOCATION (ref, loc);
2554 if (TREE_READONLY (subdatum)
2555 || (use_datum_quals && TREE_READONLY (datum)))
2556 TREE_READONLY (ref) = 1;
2557 if (TREE_THIS_VOLATILE (subdatum)
2558 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2559 TREE_THIS_VOLATILE (ref) = 1;
2561 if (TREE_UNAVAILABLE (subdatum))
2562 error_unavailable_use (subdatum, NULL_TREE);
2563 else if (TREE_DEPRECATED (subdatum))
2564 warn_deprecated_use (subdatum, NULL_TREE);
2566 datum = ref;
2568 field = TREE_CHAIN (field);
2570 while (field);
2572 return ref;
2574 else if (should_suggest_deref_p (type))
2576 /* Special-case the error message for "ptr.field" for the case
2577 where the user has confused "." vs "->". */
2578 rich_location richloc (line_table, loc);
2579 /* "loc" should be the "." token. */
2580 richloc.add_fixit_replace ("->");
2581 error_at (&richloc,
2582 "%qE is a pointer; did you mean to use %<->%>?",
2583 datum);
2584 return error_mark_node;
2586 else if (code != ERROR_MARK)
2587 error_at (loc,
2588 "request for member %qE in something not a structure or union",
2589 component);
2591 return error_mark_node;
2594 /* Given an expression PTR for a pointer, return an expression
2595 for the value pointed to.
2596 ERRORSTRING is the name of the operator to appear in error messages.
2598 LOC is the location to use for the generated tree. */
2600 tree
2601 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2603 tree pointer = default_conversion (ptr);
2604 tree type = TREE_TYPE (pointer);
2605 tree ref;
2607 if (TREE_CODE (type) == POINTER_TYPE)
2609 if (CONVERT_EXPR_P (pointer)
2610 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2612 /* If a warning is issued, mark it to avoid duplicates from
2613 the backend. This only needs to be done at
2614 warn_strict_aliasing > 2. */
2615 if (warn_strict_aliasing > 2)
2616 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2617 type, TREE_OPERAND (pointer, 0)))
2618 suppress_warning (pointer, OPT_Wstrict_aliasing_);
2621 if (TREE_CODE (pointer) == ADDR_EXPR
2622 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2623 == TREE_TYPE (type)))
2625 ref = TREE_OPERAND (pointer, 0);
2626 protected_set_expr_location (ref, loc);
2627 return ref;
2629 else
2631 tree t = TREE_TYPE (type);
2633 ref = build1 (INDIRECT_REF, t, pointer);
2635 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2636 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2638 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2639 so that we get the proper error message if the result is used
2640 to assign to. Also, &* is supposed to be a no-op.
2641 And ANSI C seems to specify that the type of the result
2642 should be the const type. */
2643 /* A de-reference of a pointer to const is not a const. It is valid
2644 to change it via some other pointer. */
2645 TREE_READONLY (ref) = TYPE_READONLY (t);
2646 TREE_SIDE_EFFECTS (ref)
2647 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2648 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2649 protected_set_expr_location (ref, loc);
2650 return ref;
2653 else if (TREE_CODE (pointer) != ERROR_MARK)
2654 invalid_indirection_error (loc, type, errstring);
2656 return error_mark_node;
2659 /* This handles expressions of the form "a[i]", which denotes
2660 an array reference.
2662 This is logically equivalent in C to *(a+i), but we may do it differently.
2663 If A is a variable or a member, we generate a primitive ARRAY_REF.
2664 This avoids forcing the array out of registers, and can work on
2665 arrays that are not lvalues (for example, members of structures returned
2666 by functions).
2668 For vector types, allow vector[i] but not i[vector], and create
2669 *(((type*)&vectortype) + i) for the expression.
2671 LOC is the location to use for the returned expression. */
2673 tree
2674 build_array_ref (location_t loc, tree array, tree index)
2676 tree ret;
2677 bool swapped = false;
2678 if (TREE_TYPE (array) == error_mark_node
2679 || TREE_TYPE (index) == error_mark_node)
2680 return error_mark_node;
2682 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2683 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2684 /* Allow vector[index] but not index[vector]. */
2685 && !gnu_vector_type_p (TREE_TYPE (array)))
2687 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2688 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2690 error_at (loc,
2691 "subscripted value is neither array nor pointer nor vector");
2693 return error_mark_node;
2695 std::swap (array, index);
2696 swapped = true;
2699 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2701 error_at (loc, "array subscript is not an integer");
2702 return error_mark_node;
2705 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2707 error_at (loc, "subscripted value is pointer to function");
2708 return error_mark_node;
2711 /* ??? Existing practice has been to warn only when the char
2712 index is syntactically the index, not for char[array]. */
2713 if (!swapped)
2714 warn_array_subscript_with_type_char (loc, index);
2716 /* Apply default promotions *after* noticing character types. */
2717 index = default_conversion (index);
2718 if (index == error_mark_node)
2719 return error_mark_node;
2721 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2723 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2724 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2726 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2728 tree rval, type;
2730 /* An array that is indexed by a non-constant
2731 cannot be stored in a register; we must be able to do
2732 address arithmetic on its address.
2733 Likewise an array of elements of variable size. */
2734 if (TREE_CODE (index) != INTEGER_CST
2735 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2736 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2738 if (!c_mark_addressable (array, true))
2739 return error_mark_node;
2741 /* An array that is indexed by a constant value which is not within
2742 the array bounds cannot be stored in a register either; because we
2743 would get a crash in store_bit_field/extract_bit_field when trying
2744 to access a non-existent part of the register. */
2745 if (TREE_CODE (index) == INTEGER_CST
2746 && TYPE_DOMAIN (TREE_TYPE (array))
2747 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2749 if (!c_mark_addressable (array))
2750 return error_mark_node;
2753 if ((pedantic || warn_c90_c99_compat)
2754 && ! was_vector)
2756 tree foo = array;
2757 while (TREE_CODE (foo) == COMPONENT_REF)
2758 foo = TREE_OPERAND (foo, 0);
2759 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2760 pedwarn (loc, OPT_Wpedantic,
2761 "ISO C forbids subscripting %<register%> array");
2762 else if (!lvalue_p (foo))
2763 pedwarn_c90 (loc, OPT_Wpedantic,
2764 "ISO C90 forbids subscripting non-lvalue "
2765 "array");
2768 type = TREE_TYPE (TREE_TYPE (array));
2769 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2770 /* Array ref is const/volatile if the array elements are
2771 or if the array is. */
2772 TREE_READONLY (rval)
2773 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2774 | TREE_READONLY (array));
2775 TREE_SIDE_EFFECTS (rval)
2776 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2777 | TREE_SIDE_EFFECTS (array));
2778 TREE_THIS_VOLATILE (rval)
2779 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2780 /* This was added by rms on 16 Nov 91.
2781 It fixes vol struct foo *a; a->elts[1]
2782 in an inline function.
2783 Hope it doesn't break something else. */
2784 | TREE_THIS_VOLATILE (array));
2785 ret = require_complete_type (loc, rval);
2786 protected_set_expr_location (ret, loc);
2787 if (non_lvalue)
2788 ret = non_lvalue_loc (loc, ret);
2789 return ret;
2791 else
2793 tree ar = default_conversion (array);
2795 if (ar == error_mark_node)
2796 return ar;
2798 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2799 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2801 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2802 index, false),
2803 RO_ARRAY_INDEXING);
2804 if (non_lvalue)
2805 ret = non_lvalue_loc (loc, ret);
2806 return ret;
2810 /* Build an external reference to identifier ID. FUN indicates
2811 whether this will be used for a function call. LOC is the source
2812 location of the identifier. This sets *TYPE to the type of the
2813 identifier, which is not the same as the type of the returned value
2814 for CONST_DECLs defined as enum constants. If the type of the
2815 identifier is not available, *TYPE is set to NULL. */
2816 tree
2817 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2819 tree ref;
2820 tree decl = lookup_name (id);
2822 /* In Objective-C, an instance variable (ivar) may be preferred to
2823 whatever lookup_name() found. */
2824 decl = objc_lookup_ivar (decl, id);
2826 *type = NULL;
2827 if (decl && decl != error_mark_node)
2829 ref = decl;
2830 *type = TREE_TYPE (ref);
2832 else if (fun)
2833 /* Implicit function declaration. */
2834 ref = implicitly_declare (loc, id);
2835 else if (decl == error_mark_node)
2836 /* Don't complain about something that's already been
2837 complained about. */
2838 return error_mark_node;
2839 else
2841 undeclared_variable (loc, id);
2842 return error_mark_node;
2845 if (TREE_TYPE (ref) == error_mark_node)
2846 return error_mark_node;
2848 if (TREE_UNAVAILABLE (ref))
2849 error_unavailable_use (ref, NULL_TREE);
2850 else if (TREE_DEPRECATED (ref))
2851 warn_deprecated_use (ref, NULL_TREE);
2853 /* Recursive call does not count as usage. */
2854 if (ref != current_function_decl)
2856 TREE_USED (ref) = 1;
2859 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2861 if (!in_sizeof && !in_typeof)
2862 C_DECL_USED (ref) = 1;
2863 else if (DECL_INITIAL (ref) == NULL_TREE
2864 && DECL_EXTERNAL (ref)
2865 && !TREE_PUBLIC (ref))
2866 record_maybe_used_decl (ref);
2869 if (TREE_CODE (ref) == CONST_DECL)
2871 used_types_insert (TREE_TYPE (ref));
2873 if (warn_cxx_compat
2874 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2875 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2877 warning_at (loc, OPT_Wc___compat,
2878 ("enum constant defined in struct or union "
2879 "is not visible in C++"));
2880 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2883 ref = DECL_INITIAL (ref);
2884 TREE_CONSTANT (ref) = 1;
2886 else if (current_function_decl != NULL_TREE
2887 && !DECL_FILE_SCOPE_P (current_function_decl)
2888 && (VAR_OR_FUNCTION_DECL_P (ref)
2889 || TREE_CODE (ref) == PARM_DECL))
2891 tree context = decl_function_context (ref);
2893 if (context != NULL_TREE && context != current_function_decl)
2894 DECL_NONLOCAL (ref) = 1;
2896 /* C99 6.7.4p3: An inline definition of a function with external
2897 linkage ... shall not contain a reference to an identifier with
2898 internal linkage. */
2899 else if (current_function_decl != NULL_TREE
2900 && DECL_DECLARED_INLINE_P (current_function_decl)
2901 && DECL_EXTERNAL (current_function_decl)
2902 && VAR_OR_FUNCTION_DECL_P (ref)
2903 && (!VAR_P (ref) || TREE_STATIC (ref))
2904 && ! TREE_PUBLIC (ref)
2905 && DECL_CONTEXT (ref) != current_function_decl)
2906 record_inline_static (loc, current_function_decl, ref,
2907 csi_internal);
2909 return ref;
2912 /* Record details of decls possibly used inside sizeof or typeof. */
2913 struct maybe_used_decl
2915 /* The decl. */
2916 tree decl;
2917 /* The level seen at (in_sizeof + in_typeof). */
2918 int level;
2919 /* The next one at this level or above, or NULL. */
2920 struct maybe_used_decl *next;
2923 static struct maybe_used_decl *maybe_used_decls;
2925 /* Record that DECL, an undefined static function reference seen
2926 inside sizeof or typeof, might be used if the operand of sizeof is
2927 a VLA type or the operand of typeof is a variably modified
2928 type. */
2930 static void
2931 record_maybe_used_decl (tree decl)
2933 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2934 t->decl = decl;
2935 t->level = in_sizeof + in_typeof;
2936 t->next = maybe_used_decls;
2937 maybe_used_decls = t;
2940 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2941 USED is false, just discard them. If it is true, mark them used
2942 (if no longer inside sizeof or typeof) or move them to the next
2943 level up (if still inside sizeof or typeof). */
2945 void
2946 pop_maybe_used (bool used)
2948 struct maybe_used_decl *p = maybe_used_decls;
2949 int cur_level = in_sizeof + in_typeof;
2950 while (p && p->level > cur_level)
2952 if (used)
2954 if (cur_level == 0)
2955 C_DECL_USED (p->decl) = 1;
2956 else
2957 p->level = cur_level;
2959 p = p->next;
2961 if (!used || cur_level == 0)
2962 maybe_used_decls = p;
2965 /* Return the result of sizeof applied to EXPR. */
2967 struct c_expr
2968 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2970 struct c_expr ret;
2971 if (expr.value == error_mark_node)
2973 ret.value = error_mark_node;
2974 ret.original_code = ERROR_MARK;
2975 ret.original_type = NULL;
2976 pop_maybe_used (false);
2978 else
2980 bool expr_const_operands = true;
2982 if (TREE_CODE (expr.value) == PARM_DECL
2983 && C_ARRAY_PARAMETER (expr.value))
2985 auto_diagnostic_group d;
2986 if (warning_at (loc, OPT_Wsizeof_array_argument,
2987 "%<sizeof%> on array function parameter %qE will "
2988 "return size of %qT", expr.value,
2989 TREE_TYPE (expr.value)))
2990 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2992 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2993 &expr_const_operands);
2994 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2995 c_last_sizeof_arg = expr.value;
2996 c_last_sizeof_loc = loc;
2997 ret.original_code = SIZEOF_EXPR;
2998 ret.original_type = NULL;
2999 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
3001 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
3002 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3003 folded_expr, ret.value);
3004 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3005 SET_EXPR_LOCATION (ret.value, loc);
3007 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3009 return ret;
3012 /* Return the result of sizeof applied to T, a structure for the type
3013 name passed to sizeof (rather than the type itself). LOC is the
3014 location of the original expression. */
3016 struct c_expr
3017 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3019 tree type;
3020 struct c_expr ret;
3021 tree type_expr = NULL_TREE;
3022 bool type_expr_const = true;
3023 type = groktypename (t, &type_expr, &type_expr_const);
3024 ret.value = c_sizeof (loc, type);
3025 c_last_sizeof_arg = type;
3026 c_last_sizeof_loc = loc;
3027 ret.original_code = SIZEOF_EXPR;
3028 ret.original_type = NULL;
3029 if (type == error_mark_node)
3031 ret.value = error_mark_node;
3032 ret.original_code = ERROR_MARK;
3034 else
3035 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3036 && C_TYPE_VARIABLE_SIZE (type))
3038 /* If the type is a [*] array, it is a VLA but is represented as
3039 having a size of zero. In such a case we must ensure that
3040 the result of sizeof does not get folded to a constant by
3041 c_fully_fold, because if the size is evaluated the result is
3042 not constant and so constraints on zero or negative size
3043 arrays must not be applied when this sizeof call is inside
3044 another array declarator. */
3045 if (!type_expr)
3046 type_expr = integer_zero_node;
3047 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3048 type_expr, ret.value);
3049 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3051 pop_maybe_used (type != error_mark_node
3052 ? C_TYPE_VARIABLE_SIZE (type) : false);
3053 return ret;
3056 /* Build a function call to function FUNCTION with parameters PARAMS.
3057 The function call is at LOC.
3058 PARAMS is a list--a chain of TREE_LIST nodes--in which the
3059 TREE_VALUE of each node is a parameter-expression.
3060 FUNCTION's data type may be a function type or a pointer-to-function. */
3062 tree
3063 build_function_call (location_t loc, tree function, tree params)
3065 vec<tree, va_gc> *v;
3066 tree ret;
3068 vec_alloc (v, list_length (params));
3069 for (; params; params = TREE_CHAIN (params))
3070 v->quick_push (TREE_VALUE (params));
3071 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3072 vec_free (v);
3073 return ret;
3076 /* Give a note about the location of the declaration of DECL. */
3078 static void
3079 inform_declaration (tree decl)
3081 if (decl && (TREE_CODE (decl) != FUNCTION_DECL
3082 || !DECL_IS_UNDECLARED_BUILTIN (decl)))
3083 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3086 /* Build a function call to function FUNCTION with parameters PARAMS.
3087 If FUNCTION is the result of resolving an overloaded target built-in,
3088 ORIG_FUNDECL is the original function decl, otherwise it is null.
3089 ORIGTYPES, if not NULL, is a vector of types; each element is
3090 either NULL or the original type of the corresponding element in
3091 PARAMS. The original type may differ from TREE_TYPE of the
3092 parameter for enums. FUNCTION's data type may be a function type
3093 or pointer-to-function. This function changes the elements of
3094 PARAMS. */
3096 tree
3097 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3098 tree function, vec<tree, va_gc> *params,
3099 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3101 tree fntype, fundecl = NULL_TREE;
3102 tree name = NULL_TREE, result;
3103 tree tem;
3104 int nargs;
3105 tree *argarray;
3108 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3109 STRIP_TYPE_NOPS (function);
3111 /* Convert anything with function type to a pointer-to-function. */
3112 if (TREE_CODE (function) == FUNCTION_DECL)
3114 name = DECL_NAME (function);
3116 if (flag_tm)
3117 tm_malloc_replacement (function);
3118 fundecl = function;
3119 if (!orig_fundecl)
3120 orig_fundecl = fundecl;
3121 /* Atomic functions have type checking/casting already done. They are
3122 often rewritten and don't match the original parameter list. */
3123 if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
3124 origtypes = NULL;
3126 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3127 function = function_to_pointer_conversion (loc, function);
3129 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3130 expressions, like those used for ObjC messenger dispatches. */
3131 if (params && !params->is_empty ())
3132 function = objc_rewrite_function_call (function, (*params)[0]);
3134 function = c_fully_fold (function, false, NULL);
3136 fntype = TREE_TYPE (function);
3138 if (TREE_CODE (fntype) == ERROR_MARK)
3139 return error_mark_node;
3141 if (!(TREE_CODE (fntype) == POINTER_TYPE
3142 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3144 if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
3145 error_at (loc,
3146 "called object %qE is not a function or function pointer",
3147 function);
3148 else if (DECL_P (function))
3150 error_at (loc,
3151 "called object %qD is not a function or function pointer",
3152 function);
3153 inform_declaration (function);
3155 else
3156 error_at (loc,
3157 "called object is not a function or function pointer");
3158 return error_mark_node;
3161 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3162 current_function_returns_abnormally = 1;
3164 /* fntype now gets the type of function pointed to. */
3165 fntype = TREE_TYPE (fntype);
3167 /* Convert the parameters to the types declared in the
3168 function prototype, or apply default promotions. */
3170 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3171 origtypes, function, fundecl);
3172 if (nargs < 0)
3173 return error_mark_node;
3175 /* Check that the function is called through a compatible prototype.
3176 If it is not, warn. */
3177 if (CONVERT_EXPR_P (function)
3178 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3179 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3180 && !comptypes (fntype, TREE_TYPE (tem)))
3182 tree return_type = TREE_TYPE (fntype);
3184 /* This situation leads to run-time undefined behavior. We can't,
3185 therefore, simply error unless we can prove that all possible
3186 executions of the program must execute the code. */
3187 warning_at (loc, 0, "function called through a non-compatible type");
3189 if (VOID_TYPE_P (return_type)
3190 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3191 pedwarn (loc, 0,
3192 "function with qualified void return type called");
3195 argarray = vec_safe_address (params);
3197 /* Check that arguments to builtin functions match the expectations. */
3198 if (fundecl
3199 && fndecl_built_in_p (fundecl)
3200 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3201 orig_fundecl, nargs, argarray))
3202 return error_mark_node;
3204 /* Check that the arguments to the function are valid. */
3205 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3206 nargs, argarray, &arg_loc);
3208 if (name != NULL_TREE
3209 && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
3211 if (require_constant_value)
3212 result
3213 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3214 function, nargs, argarray);
3215 else
3216 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3217 function, nargs, argarray);
3218 if (TREE_CODE (result) == NOP_EXPR
3219 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3220 STRIP_TYPE_NOPS (result);
3222 else
3223 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3224 function, nargs, argarray);
3225 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3226 later. */
3227 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3228 suppress_warning (result, OPT_Wnonnull);
3230 /* In this improbable scenario, a nested function returns a VM type.
3231 Create a TARGET_EXPR so that the call always has a LHS, much as
3232 what the C++ FE does for functions returning non-PODs. */
3233 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3235 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3236 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3237 NULL_TREE, NULL_TREE);
3240 if (VOID_TYPE_P (TREE_TYPE (result)))
3242 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3243 pedwarn (loc, 0,
3244 "function with qualified void return type called");
3245 return result;
3247 return require_complete_type (loc, result);
3250 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3252 tree
3253 c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
3254 tree function, vec<tree, va_gc> *params,
3255 vec<tree, va_gc> *origtypes)
3257 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3258 STRIP_TYPE_NOPS (function);
3260 /* Convert anything with function type to a pointer-to-function. */
3261 if (TREE_CODE (function) == FUNCTION_DECL)
3263 /* Implement type-directed function overloading for builtins.
3264 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3265 handle all the type checking. The result is a complete expression
3266 that implements this function call. */
3267 tree tem = resolve_overloaded_builtin (loc, function, params);
3268 if (tem)
3269 return tem;
3271 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3274 /* Helper for convert_arguments called to convert the VALue of argument
3275 number ARGNUM from ORIGTYPE to the corresponding parameter number
3276 PARMNUM and TYPE.
3277 PLOC is the location where the conversion is being performed.
3278 FUNCTION and FUNDECL are the same as in convert_arguments.
3279 VALTYPE is the original type of VAL before the conversion and,
3280 for EXCESS_PRECISION_EXPR, the operand of the expression.
3281 NPC is true if VAL represents the null pointer constant (VAL itself
3282 will have been folded to an integer constant).
3283 RNAME is the same as FUNCTION except in Objective C when it's
3284 the function selector.
3285 EXCESS_PRECISION is true when VAL was originally represented
3286 as EXCESS_PRECISION_EXPR.
3287 WARNOPT is the same as in convert_for_assignment. */
3289 static tree
3290 convert_argument (location_t ploc, tree function, tree fundecl,
3291 tree type, tree origtype, tree val, tree valtype,
3292 bool npc, tree rname, int parmnum, int argnum,
3293 bool excess_precision, int warnopt)
3295 /* Formal parm type is specified by a function prototype. */
3297 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3299 error_at (ploc, "type of formal parameter %d is incomplete",
3300 parmnum + 1);
3301 return val;
3304 /* Optionally warn about conversions that differ from the default
3305 conversions. */
3306 if (warn_traditional_conversion || warn_traditional)
3308 unsigned int formal_prec = TYPE_PRECISION (type);
3310 if (INTEGRAL_TYPE_P (type)
3311 && TREE_CODE (valtype) == REAL_TYPE)
3312 warning_at (ploc, OPT_Wtraditional_conversion,
3313 "passing argument %d of %qE as integer rather "
3314 "than floating due to prototype",
3315 argnum, rname);
3316 if (INTEGRAL_TYPE_P (type)
3317 && TREE_CODE (valtype) == COMPLEX_TYPE)
3318 warning_at (ploc, OPT_Wtraditional_conversion,
3319 "passing argument %d of %qE as integer rather "
3320 "than complex due to prototype",
3321 argnum, rname);
3322 else if (TREE_CODE (type) == COMPLEX_TYPE
3323 && TREE_CODE (valtype) == REAL_TYPE)
3324 warning_at (ploc, OPT_Wtraditional_conversion,
3325 "passing argument %d of %qE as complex rather "
3326 "than floating due to prototype",
3327 argnum, rname);
3328 else if (TREE_CODE (type) == REAL_TYPE
3329 && INTEGRAL_TYPE_P (valtype))
3330 warning_at (ploc, OPT_Wtraditional_conversion,
3331 "passing argument %d of %qE as floating rather "
3332 "than integer due to prototype",
3333 argnum, rname);
3334 else if (TREE_CODE (type) == COMPLEX_TYPE
3335 && INTEGRAL_TYPE_P (valtype))
3336 warning_at (ploc, OPT_Wtraditional_conversion,
3337 "passing argument %d of %qE as complex rather "
3338 "than integer due to prototype",
3339 argnum, rname);
3340 else if (TREE_CODE (type) == REAL_TYPE
3341 && TREE_CODE (valtype) == COMPLEX_TYPE)
3342 warning_at (ploc, OPT_Wtraditional_conversion,
3343 "passing argument %d of %qE as floating rather "
3344 "than complex due to prototype",
3345 argnum, rname);
3346 /* ??? At some point, messages should be written about
3347 conversions between complex types, but that's too messy
3348 to do now. */
3349 else if (TREE_CODE (type) == REAL_TYPE
3350 && TREE_CODE (valtype) == REAL_TYPE)
3352 /* Warn if any argument is passed as `float',
3353 since without a prototype it would be `double'. */
3354 if (formal_prec == TYPE_PRECISION (float_type_node)
3355 && type != dfloat32_type_node)
3356 warning_at (ploc, 0,
3357 "passing argument %d of %qE as %<float%> "
3358 "rather than %<double%> due to prototype",
3359 argnum, rname);
3361 /* Warn if mismatch between argument and prototype
3362 for decimal float types. Warn of conversions with
3363 binary float types and of precision narrowing due to
3364 prototype. */
3365 else if (type != valtype
3366 && (type == dfloat32_type_node
3367 || type == dfloat64_type_node
3368 || type == dfloat128_type_node
3369 || valtype == dfloat32_type_node
3370 || valtype == dfloat64_type_node
3371 || valtype == dfloat128_type_node)
3372 && (formal_prec
3373 <= TYPE_PRECISION (valtype)
3374 || (type == dfloat128_type_node
3375 && (valtype
3376 != dfloat64_type_node
3377 && (valtype
3378 != dfloat32_type_node)))
3379 || (type == dfloat64_type_node
3380 && (valtype
3381 != dfloat32_type_node))))
3382 warning_at (ploc, 0,
3383 "passing argument %d of %qE as %qT "
3384 "rather than %qT due to prototype",
3385 argnum, rname, type, valtype);
3388 /* Detect integer changing in width or signedness.
3389 These warnings are only activated with
3390 -Wtraditional-conversion, not with -Wtraditional. */
3391 else if (warn_traditional_conversion
3392 && INTEGRAL_TYPE_P (type)
3393 && INTEGRAL_TYPE_P (valtype))
3395 tree would_have_been = default_conversion (val);
3396 tree type1 = TREE_TYPE (would_have_been);
3398 if (val == error_mark_node)
3399 /* VAL could have been of incomplete type. */;
3400 else if (TREE_CODE (type) == ENUMERAL_TYPE
3401 && (TYPE_MAIN_VARIANT (type)
3402 == TYPE_MAIN_VARIANT (valtype)))
3403 /* No warning if function asks for enum
3404 and the actual arg is that enum type. */
3406 else if (formal_prec != TYPE_PRECISION (type1))
3407 warning_at (ploc, OPT_Wtraditional_conversion,
3408 "passing argument %d of %qE "
3409 "with different width due to prototype",
3410 argnum, rname);
3411 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3413 /* Don't complain if the formal parameter type
3414 is an enum, because we can't tell now whether
3415 the value was an enum--even the same enum. */
3416 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3418 else if (TREE_CODE (val) == INTEGER_CST
3419 && int_fits_type_p (val, type))
3420 /* Change in signedness doesn't matter
3421 if a constant value is unaffected. */
3423 /* If the value is extended from a narrower
3424 unsigned type, it doesn't matter whether we
3425 pass it as signed or unsigned; the value
3426 certainly is the same either way. */
3427 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3428 && TYPE_UNSIGNED (valtype))
3430 else if (TYPE_UNSIGNED (type))
3431 warning_at (ploc, OPT_Wtraditional_conversion,
3432 "passing argument %d of %qE "
3433 "as unsigned due to prototype",
3434 argnum, rname);
3435 else
3436 warning_at (ploc, OPT_Wtraditional_conversion,
3437 "passing argument %d of %qE "
3438 "as signed due to prototype",
3439 argnum, rname);
3443 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3444 sake of better warnings from convert_and_check. */
3445 if (excess_precision)
3446 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3448 tree parmval = convert_for_assignment (ploc, ploc, type,
3449 val, origtype, ic_argpass,
3450 npc, fundecl, function,
3451 parmnum + 1, warnopt);
3453 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3454 && INTEGRAL_TYPE_P (type)
3455 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3456 parmval = default_conversion (parmval);
3458 return parmval;
3461 /* Convert the argument expressions in the vector VALUES
3462 to the types in the list TYPELIST.
3464 If TYPELIST is exhausted, or when an element has NULL as its type,
3465 perform the default conversions.
3467 ORIGTYPES is the original types of the expressions in VALUES. This
3468 holds the type of enum values which have been converted to integral
3469 types. It may be NULL.
3471 FUNCTION is a tree for the called function. It is used only for
3472 error messages, where it is formatted with %qE.
3474 This is also where warnings about wrong number of args are generated.
3476 ARG_LOC are locations of function arguments (if any).
3478 Returns the actual number of arguments processed (which may be less
3479 than the length of VALUES in some error situations), or -1 on
3480 failure. */
3482 static int
3483 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3484 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3485 tree function, tree fundecl)
3487 unsigned int parmnum;
3488 bool error_args = false;
3489 const bool type_generic = fundecl
3490 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3491 bool type_generic_remove_excess_precision = false;
3492 bool type_generic_overflow_p = false;
3493 tree selector;
3495 /* Change pointer to function to the function itself for
3496 diagnostics. */
3497 if (TREE_CODE (function) == ADDR_EXPR
3498 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3499 function = TREE_OPERAND (function, 0);
3501 /* Handle an ObjC selector specially for diagnostics. */
3502 selector = objc_message_selector ();
3504 /* For a call to a built-in function declared without a prototype,
3505 set to the built-in function's argument list. */
3506 tree builtin_typelist = NULL_TREE;
3508 /* For type-generic built-in functions, determine whether excess
3509 precision should be removed (classification) or not
3510 (comparison). */
3511 if (fundecl
3512 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3514 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3515 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3517 /* For a call to a built-in function declared without a prototype
3518 use the types of the parameters of the internal built-in to
3519 match those of the arguments to. */
3520 if (tree bdecl = builtin_decl_explicit (code))
3521 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3524 /* For type-generic built-in functions, determine whether excess
3525 precision should be removed (classification) or not
3526 (comparison). */
3527 if (type_generic)
3528 switch (code)
3530 case BUILT_IN_ISFINITE:
3531 case BUILT_IN_ISINF:
3532 case BUILT_IN_ISINF_SIGN:
3533 case BUILT_IN_ISNAN:
3534 case BUILT_IN_ISNORMAL:
3535 case BUILT_IN_FPCLASSIFY:
3536 type_generic_remove_excess_precision = true;
3537 break;
3539 case BUILT_IN_ADD_OVERFLOW_P:
3540 case BUILT_IN_SUB_OVERFLOW_P:
3541 case BUILT_IN_MUL_OVERFLOW_P:
3542 /* The last argument of these type-generic builtins
3543 should not be promoted. */
3544 type_generic_overflow_p = true;
3545 break;
3547 default:
3548 break;
3552 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3553 individual converted arguments. */
3555 tree typetail, builtin_typetail, val;
3556 for (typetail = typelist,
3557 builtin_typetail = builtin_typelist,
3558 parmnum = 0;
3559 values && values->iterate (parmnum, &val);
3560 ++parmnum)
3562 /* The type of the function parameter (if it was declared with one). */
3563 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3564 /* The type of the built-in function parameter (if the function
3565 is a built-in). Used to detect type incompatibilities in
3566 calls to built-ins declared without a prototype. */
3567 tree builtin_type = (builtin_typetail
3568 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3569 /* The original type of the argument being passed to the function. */
3570 tree valtype = TREE_TYPE (val);
3571 /* The called function (or function selector in Objective C). */
3572 tree rname = function;
3573 int argnum = parmnum + 1;
3574 const char *invalid_func_diag;
3575 /* Set for EXCESS_PRECISION_EXPR arguments. */
3576 bool excess_precision = false;
3577 /* The value of the argument after conversion to the type
3578 of the function parameter it is passed to. */
3579 tree parmval;
3580 /* Some __atomic_* builtins have additional hidden argument at
3581 position 0. */
3582 location_t ploc
3583 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3584 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3585 : input_location;
3587 if (type == void_type_node)
3589 if (selector)
3590 error_at (loc, "too many arguments to method %qE", selector);
3591 else
3592 error_at (loc, "too many arguments to function %qE", function);
3593 inform_declaration (fundecl);
3594 return error_args ? -1 : (int) parmnum;
3597 if (builtin_type == void_type_node)
3599 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3600 "too many arguments to built-in function %qE "
3601 "expecting %d", function, parmnum))
3602 inform_declaration (fundecl);
3603 builtin_typetail = NULL_TREE;
3606 if (selector && argnum > 2)
3608 rname = selector;
3609 argnum -= 2;
3612 /* Determine if VAL is a null pointer constant before folding it. */
3613 bool npc = null_pointer_constant_p (val);
3615 /* If there is excess precision and a prototype, convert once to
3616 the required type rather than converting via the semantic
3617 type. Likewise without a prototype a float value represented
3618 as long double should be converted once to double. But for
3619 type-generic classification functions excess precision must
3620 be removed here. */
3621 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3622 && (type || !type_generic || !type_generic_remove_excess_precision))
3624 val = TREE_OPERAND (val, 0);
3625 excess_precision = true;
3627 val = c_fully_fold (val, false, NULL);
3628 STRIP_TYPE_NOPS (val);
3630 val = require_complete_type (ploc, val);
3632 /* Some floating-point arguments must be promoted to double when
3633 no type is specified by a prototype. This applies to
3634 arguments of type float, and to architecture-specific types
3635 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3636 bool promote_float_arg = false;
3637 if (type == NULL_TREE
3638 && TREE_CODE (valtype) == REAL_TYPE
3639 && (TYPE_PRECISION (valtype)
3640 <= TYPE_PRECISION (double_type_node))
3641 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3642 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3643 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3645 /* Promote this argument, unless it has a _FloatN or
3646 _FloatNx type. */
3647 promote_float_arg = true;
3648 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3649 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3651 promote_float_arg = false;
3652 break;
3656 if (type != NULL_TREE)
3658 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3659 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3660 val, valtype, npc, rname, parmnum, argnum,
3661 excess_precision, 0);
3663 else if (promote_float_arg)
3665 if (type_generic)
3666 parmval = val;
3667 else
3669 /* Convert `float' to `double'. */
3670 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3671 warning_at (ploc, OPT_Wdouble_promotion,
3672 "implicit conversion from %qT to %qT when passing "
3673 "argument to function",
3674 valtype, double_type_node);
3675 parmval = convert (double_type_node, val);
3678 else if ((excess_precision && !type_generic)
3679 || (type_generic_overflow_p && parmnum == 2))
3680 /* A "double" argument with excess precision being passed
3681 without a prototype or in variable arguments.
3682 The last argument of __builtin_*_overflow_p should not be
3683 promoted. */
3684 parmval = convert (valtype, val);
3685 else if ((invalid_func_diag =
3686 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3688 error (invalid_func_diag);
3689 return -1;
3691 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3693 return -1;
3695 else
3696 /* Convert `short' and `char' to full-size `int'. */
3697 parmval = default_conversion (val);
3699 (*values)[parmnum] = parmval;
3700 if (parmval == error_mark_node)
3701 error_args = true;
3703 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3705 /* For a call to a built-in function declared without a prototype,
3706 perform the conversions from the argument to the expected type
3707 but issue warnings rather than errors for any mismatches.
3708 Ignore the converted argument and use the PARMVAL obtained
3709 above by applying default conversions instead. */
3710 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3711 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3712 val, valtype, npc, rname, parmnum, argnum,
3713 excess_precision,
3714 OPT_Wbuiltin_declaration_mismatch);
3717 if (typetail)
3718 typetail = TREE_CHAIN (typetail);
3720 if (builtin_typetail)
3721 builtin_typetail = TREE_CHAIN (builtin_typetail);
3724 gcc_assert (parmnum == vec_safe_length (values));
3726 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3728 error_at (loc, "too few arguments to function %qE", function);
3729 inform_declaration (fundecl);
3730 return -1;
3733 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3735 unsigned nargs = parmnum;
3736 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3737 ++nargs;
3739 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3740 "too few arguments to built-in function %qE "
3741 "expecting %u", function, nargs - 1))
3742 inform_declaration (fundecl);
3745 return error_args ? -1 : (int) parmnum;
3748 /* This is the entry point used by the parser to build unary operators
3749 in the input. CODE, a tree_code, specifies the unary operator, and
3750 ARG is the operand. For unary plus, the C parser currently uses
3751 CONVERT_EXPR for code.
3753 LOC is the location to use for the tree generated.
3756 struct c_expr
3757 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3759 struct c_expr result;
3761 result.original_code = code;
3762 result.original_type = NULL;
3764 if (reject_gcc_builtin (arg.value))
3766 result.value = error_mark_node;
3768 else
3770 result.value = build_unary_op (loc, code, arg.value, false);
3772 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3773 overflow_warning (loc, result.value, arg.value);
3776 /* We are typically called when parsing a prefix token at LOC acting on
3777 ARG. Reflect this by updating the source range of the result to
3778 start at LOC and end at the end of ARG. */
3779 set_c_expr_source_range (&result,
3780 loc, arg.get_finish ());
3782 return result;
3785 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3787 bool
3788 char_type_p (tree type)
3790 return (type == char_type_node
3791 || type == unsigned_char_type_node
3792 || type == signed_char_type_node
3793 || type == char16_type_node
3794 || type == char32_type_node);
3797 /* This is the entry point used by the parser to build binary operators
3798 in the input. CODE, a tree_code, specifies the binary operator, and
3799 ARG1 and ARG2 are the operands. In addition to constructing the
3800 expression, we check for operands that were written with other binary
3801 operators in a way that is likely to confuse the user.
3803 LOCATION is the location of the binary operator. */
3805 struct c_expr
3806 parser_build_binary_op (location_t location, enum tree_code code,
3807 struct c_expr arg1, struct c_expr arg2)
3809 struct c_expr result;
3811 enum tree_code code1 = arg1.original_code;
3812 enum tree_code code2 = arg2.original_code;
3813 tree type1 = (arg1.original_type
3814 ? arg1.original_type
3815 : TREE_TYPE (arg1.value));
3816 tree type2 = (arg2.original_type
3817 ? arg2.original_type
3818 : TREE_TYPE (arg2.value));
3820 result.value = build_binary_op (location, code,
3821 arg1.value, arg2.value, true);
3822 result.original_code = code;
3823 result.original_type = NULL;
3825 if (TREE_CODE (result.value) == ERROR_MARK)
3827 set_c_expr_source_range (&result,
3828 arg1.get_start (),
3829 arg2.get_finish ());
3830 return result;
3833 if (location != UNKNOWN_LOCATION)
3834 protected_set_expr_location (result.value, location);
3836 set_c_expr_source_range (&result,
3837 arg1.get_start (),
3838 arg2.get_finish ());
3840 /* Check for cases such as x+y<<z which users are likely
3841 to misinterpret. */
3842 if (warn_parentheses)
3843 warn_about_parentheses (location, code, code1, arg1.value, code2,
3844 arg2.value);
3846 if (warn_logical_op)
3847 warn_logical_operator (location, code, TREE_TYPE (result.value),
3848 code1, arg1.value, code2, arg2.value);
3850 if (warn_tautological_compare)
3852 tree lhs = arg1.value;
3853 tree rhs = arg2.value;
3854 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3856 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3857 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3858 lhs = NULL_TREE;
3859 else
3860 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3862 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3864 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3865 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3866 rhs = NULL_TREE;
3867 else
3868 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3870 if (lhs != NULL_TREE && rhs != NULL_TREE)
3871 warn_tautological_cmp (location, code, lhs, rhs);
3874 if (warn_logical_not_paren
3875 && TREE_CODE_CLASS (code) == tcc_comparison
3876 && code1 == TRUTH_NOT_EXPR
3877 && code2 != TRUTH_NOT_EXPR
3878 /* Avoid warning for !!x == y. */
3879 && (TREE_CODE (arg1.value) != NE_EXPR
3880 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3882 /* Avoid warning for !b == y where b has _Bool type. */
3883 tree t = integer_zero_node;
3884 if (TREE_CODE (arg1.value) == EQ_EXPR
3885 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3886 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3888 t = TREE_OPERAND (arg1.value, 0);
3891 if (TREE_TYPE (t) != integer_type_node)
3892 break;
3893 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3894 t = C_MAYBE_CONST_EXPR_EXPR (t);
3895 else if (CONVERT_EXPR_P (t))
3896 t = TREE_OPERAND (t, 0);
3897 else
3898 break;
3900 while (1);
3902 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3903 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3906 /* Warn about comparisons against string literals, with the exception
3907 of testing for equality or inequality of a string literal with NULL. */
3908 if (code == EQ_EXPR || code == NE_EXPR)
3910 if ((code1 == STRING_CST
3911 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3912 || (code2 == STRING_CST
3913 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3914 warning_at (location, OPT_Waddress,
3915 "comparison with string literal results in unspecified behavior");
3916 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3917 if (POINTER_TYPE_P (type1)
3918 && null_pointer_constant_p (arg2.value)
3919 && char_type_p (type2))
3921 auto_diagnostic_group d;
3922 if (warning_at (location, OPT_Wpointer_compare,
3923 "comparison between pointer and zero character "
3924 "constant"))
3925 inform (arg1.get_start (),
3926 "did you mean to dereference the pointer?");
3928 else if (POINTER_TYPE_P (type2)
3929 && null_pointer_constant_p (arg1.value)
3930 && char_type_p (type1))
3932 auto_diagnostic_group d;
3933 if (warning_at (location, OPT_Wpointer_compare,
3934 "comparison between pointer and zero character "
3935 "constant"))
3936 inform (arg2.get_start (),
3937 "did you mean to dereference the pointer?");
3940 else if (TREE_CODE_CLASS (code) == tcc_comparison
3941 && (code1 == STRING_CST || code2 == STRING_CST))
3942 warning_at (location, OPT_Waddress,
3943 "comparison with string literal results in unspecified "
3944 "behavior");
3946 if (warn_array_compare
3947 && TREE_CODE_CLASS (code) == tcc_comparison
3948 && TREE_CODE (type1) == ARRAY_TYPE
3949 && TREE_CODE (type2) == ARRAY_TYPE)
3950 do_warn_array_compare (location, code, arg1.value, arg2.value);
3952 if (TREE_OVERFLOW_P (result.value)
3953 && !TREE_OVERFLOW_P (arg1.value)
3954 && !TREE_OVERFLOW_P (arg2.value))
3955 overflow_warning (location, result.value);
3957 /* Warn about comparisons of different enum types. */
3958 if (warn_enum_compare
3959 && TREE_CODE_CLASS (code) == tcc_comparison
3960 && TREE_CODE (type1) == ENUMERAL_TYPE
3961 && TREE_CODE (type2) == ENUMERAL_TYPE
3962 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3963 warning_at (location, OPT_Wenum_compare,
3964 "comparison between %qT and %qT",
3965 type1, type2);
3967 return result;
3970 /* Return a tree for the difference of pointers OP0 and OP1.
3971 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3972 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3974 static tree
3975 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3977 tree restype = ptrdiff_type_node;
3978 tree result, inttype;
3980 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3981 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3982 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3983 tree orig_op0 = op0;
3984 tree orig_op1 = op1;
3986 /* If the operands point into different address spaces, we need to
3987 explicitly convert them to pointers into the common address space
3988 before we can subtract the numerical address values. */
3989 if (as0 != as1)
3991 addr_space_t as_common;
3992 tree common_type;
3994 /* Determine the common superset address space. This is guaranteed
3995 to exist because the caller verified that comp_target_types
3996 returned non-zero. */
3997 if (!addr_space_superset (as0, as1, &as_common))
3998 gcc_unreachable ();
4000 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
4001 op0 = convert (common_type, op0);
4002 op1 = convert (common_type, op1);
4005 /* Determine integer type result of the subtraction. This will usually
4006 be the same as the result type (ptrdiff_t), but may need to be a wider
4007 type if pointers for the address space are wider than ptrdiff_t. */
4008 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
4009 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
4010 else
4011 inttype = restype;
4013 if (TREE_CODE (target_type) == VOID_TYPE)
4014 pedwarn (loc, OPT_Wpointer_arith,
4015 "pointer of type %<void *%> used in subtraction");
4016 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4017 pedwarn (loc, OPT_Wpointer_arith,
4018 "pointer to a function used in subtraction");
4020 if (current_function_decl != NULL_TREE
4021 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
4023 op0 = save_expr (op0);
4024 op1 = save_expr (op1);
4026 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
4027 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
4030 /* First do the subtraction, then build the divide operator
4031 and only convert at the very end.
4032 Do not do default conversions in case restype is a short type. */
4034 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4035 pointers. If some platform cannot provide that, or has a larger
4036 ptrdiff_type to support differences larger than half the address
4037 space, cast the pointers to some larger integer type and do the
4038 computations in that type. */
4039 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4040 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4041 convert (inttype, op1), false);
4042 else
4044 /* Cast away qualifiers. */
4045 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4046 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4047 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4050 /* This generates an error if op1 is pointer to incomplete type. */
4051 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4052 error_at (loc, "arithmetic on pointer to an incomplete type");
4053 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4054 TREE_TYPE (TREE_TYPE (orig_op0))))
4055 verify_type_context (loc, TCTX_POINTER_ARITH,
4056 TREE_TYPE (TREE_TYPE (orig_op1)));
4058 op1 = c_size_in_bytes (target_type);
4060 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4061 error_at (loc, "arithmetic on pointer to an empty aggregate");
4063 /* Divide by the size, in easiest possible way. */
4064 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4065 op0, convert (inttype, op1));
4067 /* Convert to final result type if necessary. */
4068 return convert (restype, result);
4071 /* Expand atomic compound assignments into an appropriate sequence as
4072 specified by the C11 standard section 6.5.16.2.
4074 _Atomic T1 E1
4075 T2 E2
4076 E1 op= E2
4078 This sequence is used for all types for which these operations are
4079 supported.
4081 In addition, built-in versions of the 'fe' prefixed routines may
4082 need to be invoked for floating point (real, complex or vector) when
4083 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4085 T1 newval;
4086 T1 old;
4087 T1 *addr
4088 T2 val
4089 fenv_t fenv
4091 addr = &E1;
4092 val = (E2);
4093 __atomic_load (addr, &old, SEQ_CST);
4094 feholdexcept (&fenv);
4095 loop:
4096 newval = old op val;
4097 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4098 SEQ_CST))
4099 goto done;
4100 feclearexcept (FE_ALL_EXCEPT);
4101 goto loop:
4102 done:
4103 feupdateenv (&fenv);
4105 The compiler will issue the __atomic_fetch_* built-in when possible,
4106 otherwise it will generate the generic form of the atomic operations.
4107 This requires temp(s) and has their address taken. The atomic processing
4108 is smart enough to figure out when the size of an object can utilize
4109 a lock-free version, and convert the built-in call to the appropriate
4110 lock-free routine. The optimizers will then dispose of any temps that
4111 are no longer required, and lock-free implementations are utilized as
4112 long as there is target support for the required size.
4114 If the operator is NOP_EXPR, then this is a simple assignment, and
4115 an __atomic_store is issued to perform the assignment rather than
4116 the above loop. */
4118 /* Build an atomic assignment at LOC, expanding into the proper
4119 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4120 the result of the operation, unless RETURN_OLD_P, in which case
4121 return the old value of LHS (this is only for postincrement and
4122 postdecrement). */
4124 static tree
4125 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4126 tree rhs, bool return_old_p)
4128 tree fndecl, func_call;
4129 vec<tree, va_gc> *params;
4130 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4131 tree old, old_addr;
4132 tree compound_stmt = NULL_TREE;
4133 tree stmt, goto_stmt;
4134 tree loop_label, loop_decl, done_label, done_decl;
4136 tree lhs_type = TREE_TYPE (lhs);
4137 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4138 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4139 tree rhs_semantic_type = TREE_TYPE (rhs);
4140 tree nonatomic_rhs_semantic_type;
4141 tree rhs_type;
4143 gcc_assert (TYPE_ATOMIC (lhs_type));
4145 if (return_old_p)
4146 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4148 /* Allocate enough vector items for a compare_exchange. */
4149 vec_alloc (params, 6);
4151 /* Create a compound statement to hold the sequence of statements
4152 with a loop. */
4153 if (modifycode != NOP_EXPR)
4155 compound_stmt = c_begin_compound_stmt (false);
4157 /* For consistency with build_modify_expr on non-_Atomic,
4158 mark the lhs as read. Also, it would be very hard to match
4159 such expressions in mark_exp_read. */
4160 mark_exp_read (lhs);
4163 /* Remove any excess precision (which is only present here in the
4164 case of compound assignments). */
4165 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4167 gcc_assert (modifycode != NOP_EXPR);
4168 rhs = TREE_OPERAND (rhs, 0);
4170 rhs_type = TREE_TYPE (rhs);
4172 /* Fold the RHS if it hasn't already been folded. */
4173 if (modifycode != NOP_EXPR)
4174 rhs = c_fully_fold (rhs, false, NULL);
4176 /* Remove the qualifiers for the rest of the expressions and create
4177 the VAL temp variable to hold the RHS. */
4178 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4179 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4180 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4181 TYPE_UNQUALIFIED);
4182 val = create_tmp_var_raw (nonatomic_rhs_type);
4183 TREE_ADDRESSABLE (val) = 1;
4184 suppress_warning (val);
4185 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4186 NULL_TREE);
4187 TREE_SIDE_EFFECTS (rhs) = 1;
4188 SET_EXPR_LOCATION (rhs, loc);
4189 if (modifycode != NOP_EXPR)
4190 add_stmt (rhs);
4192 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4193 an atomic_store. */
4194 if (modifycode == NOP_EXPR)
4196 compound_stmt = rhs;
4197 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4198 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4199 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4200 params->quick_push (lhs_addr);
4201 params->quick_push (rhs);
4202 params->quick_push (seq_cst);
4203 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4205 compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4206 compound_stmt, func_call);
4208 /* VAL is the value which was stored, return a COMPOUND_STMT of
4209 the statement and that value. */
4210 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4213 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4214 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4215 isn't applicable for such builtins. ??? Do we want to handle enums? */
4216 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4217 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4219 built_in_function fncode;
4220 switch (modifycode)
4222 case PLUS_EXPR:
4223 case POINTER_PLUS_EXPR:
4224 fncode = (return_old_p
4225 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4226 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4227 break;
4228 case MINUS_EXPR:
4229 fncode = (return_old_p
4230 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4231 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4232 break;
4233 case BIT_AND_EXPR:
4234 fncode = (return_old_p
4235 ? BUILT_IN_ATOMIC_FETCH_AND_N
4236 : BUILT_IN_ATOMIC_AND_FETCH_N);
4237 break;
4238 case BIT_IOR_EXPR:
4239 fncode = (return_old_p
4240 ? BUILT_IN_ATOMIC_FETCH_OR_N
4241 : BUILT_IN_ATOMIC_OR_FETCH_N);
4242 break;
4243 case BIT_XOR_EXPR:
4244 fncode = (return_old_p
4245 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4246 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4247 break;
4248 default:
4249 goto cas_loop;
4252 /* We can only use "_1" through "_16" variants of the atomic fetch
4253 built-ins. */
4254 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4255 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4256 goto cas_loop;
4258 /* If this is a pointer type, we need to multiply by the size of
4259 the pointer target type. */
4260 if (POINTER_TYPE_P (lhs_type))
4262 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4263 /* ??? This would introduce -Wdiscarded-qualifiers
4264 warning: __atomic_fetch_* expect volatile void *
4265 type as the first argument. (Assignments between
4266 atomic and non-atomic objects are OK.) */
4267 || TYPE_RESTRICT (lhs_type))
4268 goto cas_loop;
4269 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4270 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4271 convert (ptrdiff_type_node, rhs),
4272 convert (ptrdiff_type_node, sz));
4275 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4276 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4277 fndecl = builtin_decl_explicit (fncode);
4278 params->quick_push (lhs_addr);
4279 params->quick_push (rhs);
4280 params->quick_push (seq_cst);
4281 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4283 newval = create_tmp_var_raw (nonatomic_lhs_type);
4284 TREE_ADDRESSABLE (newval) = 1;
4285 suppress_warning (newval);
4286 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4287 NULL_TREE, NULL_TREE);
4288 SET_EXPR_LOCATION (rhs, loc);
4289 add_stmt (rhs);
4291 /* Finish the compound statement. */
4292 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4294 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4295 the statement and that value. */
4296 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4299 cas_loop:
4300 /* Create the variables and labels required for the op= form. */
4301 old = create_tmp_var_raw (nonatomic_lhs_type);
4302 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4303 TREE_ADDRESSABLE (old) = 1;
4304 suppress_warning (old);
4306 newval = create_tmp_var_raw (nonatomic_lhs_type);
4307 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4308 TREE_ADDRESSABLE (newval) = 1;
4309 suppress_warning (newval);
4311 loop_decl = create_artificial_label (loc);
4312 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4314 done_decl = create_artificial_label (loc);
4315 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4317 /* __atomic_load (addr, &old, SEQ_CST). */
4318 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4319 params->quick_push (lhs_addr);
4320 params->quick_push (old_addr);
4321 params->quick_push (seq_cst);
4322 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4323 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4324 NULL_TREE);
4325 add_stmt (old);
4326 params->truncate (0);
4328 /* Create the expressions for floating-point environment
4329 manipulation, if required. */
4330 bool need_fenv = (flag_trapping_math
4331 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4332 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4333 if (need_fenv)
4334 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4336 if (hold_call)
4337 add_stmt (hold_call);
4339 /* loop: */
4340 add_stmt (loop_label);
4342 /* newval = old + val; */
4343 if (rhs_type != rhs_semantic_type)
4344 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4345 rhs = build_binary_op (loc, modifycode, old, val, true);
4346 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4348 tree eptype = TREE_TYPE (rhs);
4349 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4350 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4352 else
4353 rhs = c_fully_fold (rhs, false, NULL);
4354 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4355 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4356 NULL_TREE, 0);
4357 if (rhs != error_mark_node)
4359 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4360 NULL_TREE);
4361 SET_EXPR_LOCATION (rhs, loc);
4362 add_stmt (rhs);
4365 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4366 goto done; */
4367 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4368 params->quick_push (lhs_addr);
4369 params->quick_push (old_addr);
4370 params->quick_push (newval_addr);
4371 params->quick_push (integer_zero_node);
4372 params->quick_push (seq_cst);
4373 params->quick_push (seq_cst);
4374 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4376 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4377 SET_EXPR_LOCATION (goto_stmt, loc);
4379 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4380 SET_EXPR_LOCATION (stmt, loc);
4381 add_stmt (stmt);
4383 if (clear_call)
4384 add_stmt (clear_call);
4386 /* goto loop; */
4387 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4388 SET_EXPR_LOCATION (goto_stmt, loc);
4389 add_stmt (goto_stmt);
4391 /* done: */
4392 add_stmt (done_label);
4394 if (update_call)
4395 add_stmt (update_call);
4397 /* Finish the compound statement. */
4398 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4400 /* NEWVAL is the value that was successfully stored, return a
4401 COMPOUND_EXPR of the statement and the appropriate value. */
4402 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4403 return_old_p ? old : newval);
4406 /* Construct and perhaps optimize a tree representation
4407 for a unary operation. CODE, a tree_code, specifies the operation
4408 and XARG is the operand.
4409 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4410 promotions (such as from short to int).
4411 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4412 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4413 to pointers in C99.
4415 LOCATION is the location of the operator. */
4417 tree
4418 build_unary_op (location_t location, enum tree_code code, tree xarg,
4419 bool noconvert)
4421 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4422 tree arg = xarg;
4423 tree argtype = NULL_TREE;
4424 enum tree_code typecode;
4425 tree val;
4426 tree ret = error_mark_node;
4427 tree eptype = NULL_TREE;
4428 const char *invalid_op_diag;
4429 bool int_operands;
4431 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4432 if (int_operands)
4433 arg = remove_c_maybe_const_expr (arg);
4435 if (code != ADDR_EXPR)
4436 arg = require_complete_type (location, arg);
4438 typecode = TREE_CODE (TREE_TYPE (arg));
4439 if (typecode == ERROR_MARK)
4440 return error_mark_node;
4441 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4442 typecode = INTEGER_TYPE;
4444 if ((invalid_op_diag
4445 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4447 error_at (location, invalid_op_diag);
4448 return error_mark_node;
4451 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4453 eptype = TREE_TYPE (arg);
4454 arg = TREE_OPERAND (arg, 0);
4457 switch (code)
4459 case CONVERT_EXPR:
4460 /* This is used for unary plus, because a CONVERT_EXPR
4461 is enough to prevent anybody from looking inside for
4462 associativity, but won't generate any code. */
4463 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4464 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4465 || gnu_vector_type_p (TREE_TYPE (arg))))
4467 error_at (location, "wrong type argument to unary plus");
4468 return error_mark_node;
4470 else if (!noconvert)
4471 arg = default_conversion (arg);
4472 arg = non_lvalue_loc (location, arg);
4473 break;
4475 case NEGATE_EXPR:
4476 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4477 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4478 || gnu_vector_type_p (TREE_TYPE (arg))))
4480 error_at (location, "wrong type argument to unary minus");
4481 return error_mark_node;
4483 else if (!noconvert)
4484 arg = default_conversion (arg);
4485 break;
4487 case BIT_NOT_EXPR:
4488 /* ~ works on integer types and non float vectors. */
4489 if (typecode == INTEGER_TYPE
4490 || (gnu_vector_type_p (TREE_TYPE (arg))
4491 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4493 tree e = arg;
4495 /* Warn if the expression has boolean value. */
4496 while (TREE_CODE (e) == COMPOUND_EXPR)
4497 e = TREE_OPERAND (e, 1);
4499 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4500 || truth_value_p (TREE_CODE (e))))
4502 auto_diagnostic_group d;
4503 if (warning_at (location, OPT_Wbool_operation,
4504 "%<~%> on a boolean expression"))
4506 gcc_rich_location richloc (location);
4507 richloc.add_fixit_insert_before (location, "!");
4508 inform (&richloc, "did you mean to use logical not?");
4511 if (!noconvert)
4512 arg = default_conversion (arg);
4514 else if (typecode == COMPLEX_TYPE)
4516 code = CONJ_EXPR;
4517 pedwarn (location, OPT_Wpedantic,
4518 "ISO C does not support %<~%> for complex conjugation");
4519 if (!noconvert)
4520 arg = default_conversion (arg);
4522 else
4524 error_at (location, "wrong type argument to bit-complement");
4525 return error_mark_node;
4527 break;
4529 case ABS_EXPR:
4530 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4532 error_at (location, "wrong type argument to abs");
4533 return error_mark_node;
4535 else if (!noconvert)
4536 arg = default_conversion (arg);
4537 break;
4539 case ABSU_EXPR:
4540 if (!(typecode == INTEGER_TYPE))
4542 error_at (location, "wrong type argument to absu");
4543 return error_mark_node;
4545 else if (!noconvert)
4546 arg = default_conversion (arg);
4547 break;
4549 case CONJ_EXPR:
4550 /* Conjugating a real value is a no-op, but allow it anyway. */
4551 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4552 || typecode == COMPLEX_TYPE))
4554 error_at (location, "wrong type argument to conjugation");
4555 return error_mark_node;
4557 else if (!noconvert)
4558 arg = default_conversion (arg);
4559 break;
4561 case TRUTH_NOT_EXPR:
4562 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4563 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4564 && typecode != COMPLEX_TYPE)
4566 error_at (location,
4567 "wrong type argument to unary exclamation mark");
4568 return error_mark_node;
4570 if (int_operands)
4572 arg = c_objc_common_truthvalue_conversion (location, xarg);
4573 arg = remove_c_maybe_const_expr (arg);
4575 else
4576 arg = c_objc_common_truthvalue_conversion (location, arg);
4577 ret = invert_truthvalue_loc (location, arg);
4578 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4579 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4580 location = EXPR_LOCATION (ret);
4581 goto return_build_unary_op;
4583 case REALPART_EXPR:
4584 case IMAGPART_EXPR:
4585 ret = build_real_imag_expr (location, code, arg);
4586 if (ret == error_mark_node)
4587 return error_mark_node;
4588 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4589 eptype = TREE_TYPE (eptype);
4590 goto return_build_unary_op;
4592 case PREINCREMENT_EXPR:
4593 case POSTINCREMENT_EXPR:
4594 case PREDECREMENT_EXPR:
4595 case POSTDECREMENT_EXPR:
4597 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4599 tree inner = build_unary_op (location, code,
4600 C_MAYBE_CONST_EXPR_EXPR (arg),
4601 noconvert);
4602 if (inner == error_mark_node)
4603 return error_mark_node;
4604 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4605 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4606 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4607 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4608 goto return_build_unary_op;
4611 /* Complain about anything that is not a true lvalue. In
4612 Objective-C, skip this check for property_refs. */
4613 if (!objc_is_property_ref (arg)
4614 && !lvalue_or_else (location,
4615 arg, ((code == PREINCREMENT_EXPR
4616 || code == POSTINCREMENT_EXPR)
4617 ? lv_increment
4618 : lv_decrement)))
4619 return error_mark_node;
4621 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4623 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4624 warning_at (location, OPT_Wc___compat,
4625 "increment of enumeration value is invalid in C++");
4626 else
4627 warning_at (location, OPT_Wc___compat,
4628 "decrement of enumeration value is invalid in C++");
4631 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4633 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4634 warning_at (location, OPT_Wbool_operation,
4635 "increment of a boolean expression");
4636 else
4637 warning_at (location, OPT_Wbool_operation,
4638 "decrement of a boolean expression");
4641 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4642 arg = c_fully_fold (arg, false, NULL, true);
4644 bool atomic_op;
4645 atomic_op = really_atomic_lvalue (arg);
4647 /* Increment or decrement the real part of the value,
4648 and don't change the imaginary part. */
4649 if (typecode == COMPLEX_TYPE)
4651 tree real, imag;
4653 pedwarn (location, OPT_Wpedantic,
4654 "ISO C does not support %<++%> and %<--%> on complex types");
4656 if (!atomic_op)
4658 arg = stabilize_reference (arg);
4659 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4660 true);
4661 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4662 true);
4663 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4664 if (real == error_mark_node || imag == error_mark_node)
4665 return error_mark_node;
4666 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4667 real, imag);
4668 goto return_build_unary_op;
4672 /* Report invalid types. */
4674 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4675 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4676 && typecode != COMPLEX_TYPE
4677 && !gnu_vector_type_p (TREE_TYPE (arg)))
4679 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4680 error_at (location, "wrong type argument to increment");
4681 else
4682 error_at (location, "wrong type argument to decrement");
4684 return error_mark_node;
4688 tree inc;
4690 argtype = TREE_TYPE (arg);
4692 /* Compute the increment. */
4694 if (typecode == POINTER_TYPE)
4696 /* If pointer target is an incomplete type,
4697 we just cannot know how to do the arithmetic. */
4698 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4700 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4701 error_at (location,
4702 "increment of pointer to an incomplete type %qT",
4703 TREE_TYPE (argtype));
4704 else
4705 error_at (location,
4706 "decrement of pointer to an incomplete type %qT",
4707 TREE_TYPE (argtype));
4709 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4710 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4712 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4713 pedwarn (location, OPT_Wpointer_arith,
4714 "wrong type argument to increment");
4715 else
4716 pedwarn (location, OPT_Wpointer_arith,
4717 "wrong type argument to decrement");
4719 else
4720 verify_type_context (location, TCTX_POINTER_ARITH,
4721 TREE_TYPE (argtype));
4723 inc = c_size_in_bytes (TREE_TYPE (argtype));
4724 inc = convert_to_ptrofftype_loc (location, inc);
4726 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4728 /* For signed fract types, we invert ++ to -- or
4729 -- to ++, and change inc from 1 to -1, because
4730 it is not possible to represent 1 in signed fract constants.
4731 For unsigned fract types, the result always overflows and
4732 we get an undefined (original) or the maximum value. */
4733 if (code == PREINCREMENT_EXPR)
4734 code = PREDECREMENT_EXPR;
4735 else if (code == PREDECREMENT_EXPR)
4736 code = PREINCREMENT_EXPR;
4737 else if (code == POSTINCREMENT_EXPR)
4738 code = POSTDECREMENT_EXPR;
4739 else /* code == POSTDECREMENT_EXPR */
4740 code = POSTINCREMENT_EXPR;
4742 inc = integer_minus_one_node;
4743 inc = convert (argtype, inc);
4745 else
4747 inc = VECTOR_TYPE_P (argtype)
4748 ? build_one_cst (argtype)
4749 : integer_one_node;
4750 inc = convert (argtype, inc);
4753 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4754 need to ask Objective-C to build the increment or decrement
4755 expression for it. */
4756 if (objc_is_property_ref (arg))
4757 return objc_build_incr_expr_for_property_ref (location, code,
4758 arg, inc);
4760 /* Report a read-only lvalue. */
4761 if (TYPE_READONLY (argtype))
4763 readonly_error (location, arg,
4764 ((code == PREINCREMENT_EXPR
4765 || code == POSTINCREMENT_EXPR)
4766 ? lv_increment : lv_decrement));
4767 return error_mark_node;
4769 else if (TREE_READONLY (arg))
4770 readonly_warning (arg,
4771 ((code == PREINCREMENT_EXPR
4772 || code == POSTINCREMENT_EXPR)
4773 ? lv_increment : lv_decrement));
4775 /* If the argument is atomic, use the special code sequences for
4776 atomic compound assignment. */
4777 if (atomic_op)
4779 arg = stabilize_reference (arg);
4780 ret = build_atomic_assign (location, arg,
4781 ((code == PREINCREMENT_EXPR
4782 || code == POSTINCREMENT_EXPR)
4783 ? PLUS_EXPR
4784 : MINUS_EXPR),
4785 (FRACT_MODE_P (TYPE_MODE (argtype))
4786 ? inc
4787 : integer_one_node),
4788 (code == POSTINCREMENT_EXPR
4789 || code == POSTDECREMENT_EXPR));
4790 goto return_build_unary_op;
4793 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4794 val = boolean_increment (code, arg);
4795 else
4796 val = build2 (code, TREE_TYPE (arg), arg, inc);
4797 TREE_SIDE_EFFECTS (val) = 1;
4798 ret = val;
4799 goto return_build_unary_op;
4802 case ADDR_EXPR:
4803 /* Note that this operation never does default_conversion. */
4805 /* The operand of unary '&' must be an lvalue (which excludes
4806 expressions of type void), or, in C99, the result of a [] or
4807 unary '*' operator. */
4808 if (VOID_TYPE_P (TREE_TYPE (arg))
4809 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4810 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4811 pedwarn (location, 0, "taking address of expression of type %<void%>");
4813 /* Let &* cancel out to simplify resulting code. */
4814 if (INDIRECT_REF_P (arg))
4816 /* Don't let this be an lvalue. */
4817 if (lvalue_p (TREE_OPERAND (arg, 0)))
4818 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4819 ret = TREE_OPERAND (arg, 0);
4820 goto return_build_unary_op;
4823 /* Anything not already handled and not a true memory reference
4824 or a non-lvalue array is an error. */
4825 if (typecode != FUNCTION_TYPE && !noconvert
4826 && !lvalue_or_else (location, arg, lv_addressof))
4827 return error_mark_node;
4829 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4830 folding later. */
4831 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4833 tree inner = build_unary_op (location, code,
4834 C_MAYBE_CONST_EXPR_EXPR (arg),
4835 noconvert);
4836 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4837 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4838 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4839 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4840 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4841 goto return_build_unary_op;
4844 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4845 argtype = TREE_TYPE (arg);
4847 /* If the lvalue is const or volatile, merge that into the type
4848 to which the address will point. This is only needed
4849 for function types. */
4850 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4851 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4852 && TREE_CODE (argtype) == FUNCTION_TYPE)
4854 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4855 int quals = orig_quals;
4857 if (TREE_READONLY (arg))
4858 quals |= TYPE_QUAL_CONST;
4859 if (TREE_THIS_VOLATILE (arg))
4860 quals |= TYPE_QUAL_VOLATILE;
4862 argtype = c_build_qualified_type (argtype, quals);
4865 switch (TREE_CODE (arg))
4867 case COMPONENT_REF:
4868 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4870 error_at (location, "cannot take address of bit-field %qD",
4871 TREE_OPERAND (arg, 1));
4872 return error_mark_node;
4875 /* fall through */
4877 case ARRAY_REF:
4878 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4880 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4881 && !POINTER_TYPE_P (TREE_TYPE (arg))
4882 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4884 error_at (location, "cannot take address of scalar with "
4885 "reverse storage order");
4886 return error_mark_node;
4889 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4890 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4891 warning_at (location, OPT_Wscalar_storage_order,
4892 "address of array with reverse scalar storage "
4893 "order requested");
4896 default:
4897 break;
4900 if (!c_mark_addressable (arg))
4901 return error_mark_node;
4903 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4904 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4906 argtype = build_pointer_type (argtype);
4908 /* ??? Cope with user tricks that amount to offsetof. Delete this
4909 when we have proper support for integer constant expressions. */
4910 val = get_base_address (arg);
4911 if (val && INDIRECT_REF_P (val)
4912 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4914 ret = fold_offsetof (arg, argtype);
4915 goto return_build_unary_op;
4918 val = build1 (ADDR_EXPR, argtype, arg);
4920 ret = val;
4921 goto return_build_unary_op;
4923 default:
4924 gcc_unreachable ();
4927 if (argtype == NULL_TREE)
4928 argtype = TREE_TYPE (arg);
4929 if (TREE_CODE (arg) == INTEGER_CST)
4930 ret = (require_constant_value
4931 ? fold_build1_initializer_loc (location, code, argtype, arg)
4932 : fold_build1_loc (location, code, argtype, arg));
4933 else
4934 ret = build1 (code, argtype, arg);
4935 return_build_unary_op:
4936 gcc_assert (ret != error_mark_node);
4937 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4938 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4939 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4940 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4941 ret = note_integer_operands (ret);
4942 if (eptype)
4943 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4944 protected_set_expr_location (ret, location);
4945 return ret;
4948 /* Return nonzero if REF is an lvalue valid for this language.
4949 Lvalues can be assigned, unless their type has TYPE_READONLY.
4950 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4952 bool
4953 lvalue_p (const_tree ref)
4955 const enum tree_code code = TREE_CODE (ref);
4957 switch (code)
4959 case REALPART_EXPR:
4960 case IMAGPART_EXPR:
4961 case COMPONENT_REF:
4962 return lvalue_p (TREE_OPERAND (ref, 0));
4964 case C_MAYBE_CONST_EXPR:
4965 return lvalue_p (TREE_OPERAND (ref, 1));
4967 case COMPOUND_LITERAL_EXPR:
4968 case STRING_CST:
4969 return true;
4971 case MEM_REF:
4972 case TARGET_MEM_REF:
4973 /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
4974 here as well. */
4975 case INDIRECT_REF:
4976 case ARRAY_REF:
4977 case VAR_DECL:
4978 case PARM_DECL:
4979 case RESULT_DECL:
4980 case ERROR_MARK:
4981 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4982 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4984 case BIND_EXPR:
4985 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4987 default:
4988 return false;
4992 /* Give a warning for storing in something that is read-only in GCC
4993 terms but not const in ISO C terms. */
4995 static void
4996 readonly_warning (tree arg, enum lvalue_use use)
4998 switch (use)
5000 case lv_assign:
5001 warning (0, "assignment of read-only location %qE", arg);
5002 break;
5003 case lv_increment:
5004 warning (0, "increment of read-only location %qE", arg);
5005 break;
5006 case lv_decrement:
5007 warning (0, "decrement of read-only location %qE", arg);
5008 break;
5009 default:
5010 gcc_unreachable ();
5012 return;
5016 /* Return nonzero if REF is an lvalue valid for this language;
5017 otherwise, print an error message and return zero. USE says
5018 how the lvalue is being used and so selects the error message.
5019 LOCATION is the location at which any error should be reported. */
5021 static int
5022 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
5024 int win = lvalue_p (ref);
5026 if (!win)
5027 lvalue_error (loc, use);
5029 return win;
5032 /* Mark EXP saying that we need to be able to take the
5033 address of it; it should not be allocated in a register.
5034 Returns true if successful. ARRAY_REF_P is true if this
5035 is for ARRAY_REF construction - in that case we don't want
5036 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5037 it is fine to use ARRAY_REFs for vector subscripts on vector
5038 register variables. */
5040 bool
5041 c_mark_addressable (tree exp, bool array_ref_p)
5043 tree x = exp;
5045 while (1)
5046 switch (TREE_CODE (x))
5048 case VIEW_CONVERT_EXPR:
5049 if (array_ref_p
5050 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5051 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5052 return true;
5053 x = TREE_OPERAND (x, 0);
5054 break;
5056 case COMPONENT_REF:
5057 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
5059 error ("cannot take address of bit-field %qD",
5060 TREE_OPERAND (x, 1));
5061 return false;
5063 /* FALLTHRU */
5064 case ADDR_EXPR:
5065 case ARRAY_REF:
5066 case REALPART_EXPR:
5067 case IMAGPART_EXPR:
5068 x = TREE_OPERAND (x, 0);
5069 break;
5071 case COMPOUND_LITERAL_EXPR:
5072 TREE_ADDRESSABLE (x) = 1;
5073 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5074 return true;
5076 case CONSTRUCTOR:
5077 TREE_ADDRESSABLE (x) = 1;
5078 return true;
5080 case VAR_DECL:
5081 case CONST_DECL:
5082 case PARM_DECL:
5083 case RESULT_DECL:
5084 if (C_DECL_REGISTER (x)
5085 && DECL_NONLOCAL (x))
5087 if (TREE_PUBLIC (x) || is_global_var (x))
5089 error
5090 ("global register variable %qD used in nested function", x);
5091 return false;
5093 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5095 else if (C_DECL_REGISTER (x))
5097 if (TREE_PUBLIC (x) || is_global_var (x))
5098 error ("address of global register variable %qD requested", x);
5099 else
5100 error ("address of register variable %qD requested", x);
5101 return false;
5104 /* FALLTHRU */
5105 case FUNCTION_DECL:
5106 TREE_ADDRESSABLE (x) = 1;
5107 /* FALLTHRU */
5108 default:
5109 return true;
5113 /* Convert EXPR to TYPE, warning about conversion problems with
5114 constants. SEMANTIC_TYPE is the type this conversion would use
5115 without excess precision. If SEMANTIC_TYPE is NULL, this function
5116 is equivalent to convert_and_check. This function is a wrapper that
5117 handles conversions that may be different than
5118 the usual ones because of excess precision. */
5120 static tree
5121 ep_convert_and_check (location_t loc, tree type, tree expr,
5122 tree semantic_type)
5124 if (TREE_TYPE (expr) == type)
5125 return expr;
5127 /* For C11, integer conversions may have results with excess
5128 precision. */
5129 if (flag_isoc11 || !semantic_type)
5130 return convert_and_check (loc, type, expr);
5132 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5133 && TREE_TYPE (expr) != semantic_type)
5135 /* For integers, we need to check the real conversion, not
5136 the conversion to the excess precision type. */
5137 expr = convert_and_check (loc, semantic_type, expr);
5139 /* Result type is the excess precision type, which should be
5140 large enough, so do not check. */
5141 return convert (type, expr);
5144 /* If EXPR refers to a built-in declared without a prototype returns
5145 the actual type of the built-in and, if non-null, set *BLTIN to
5146 a pointer to the built-in. Otherwise return the type of EXPR
5147 and clear *BLTIN if non-null. */
5149 static tree
5150 type_or_builtin_type (tree expr, tree *bltin = NULL)
5152 tree dummy;
5153 if (!bltin)
5154 bltin = &dummy;
5156 *bltin = NULL_TREE;
5158 tree type = TREE_TYPE (expr);
5159 if (TREE_CODE (expr) != ADDR_EXPR)
5160 return type;
5162 tree oper = TREE_OPERAND (expr, 0);
5163 if (!DECL_P (oper)
5164 || TREE_CODE (oper) != FUNCTION_DECL
5165 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5166 return type;
5168 built_in_function code = DECL_FUNCTION_CODE (oper);
5169 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5170 return type;
5172 if ((*bltin = builtin_decl_implicit (code)))
5173 type = build_pointer_type (TREE_TYPE (*bltin));
5175 return type;
5178 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5179 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5180 if folded to an integer constant then the unselected half may
5181 contain arbitrary operations not normally permitted in constant
5182 expressions. Set the location of the expression to LOC. */
5184 tree
5185 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5186 tree op1, tree op1_original_type, location_t op1_loc,
5187 tree op2, tree op2_original_type, location_t op2_loc)
5189 tree type1;
5190 tree type2;
5191 enum tree_code code1;
5192 enum tree_code code2;
5193 tree result_type = NULL;
5194 tree semantic_result_type = NULL;
5195 tree orig_op1 = op1, orig_op2 = op2;
5196 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5197 bool ifexp_int_operands;
5198 tree ret;
5200 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5201 if (op1_int_operands)
5202 op1 = remove_c_maybe_const_expr (op1);
5203 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5204 if (op2_int_operands)
5205 op2 = remove_c_maybe_const_expr (op2);
5206 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5207 if (ifexp_int_operands)
5208 ifexp = remove_c_maybe_const_expr (ifexp);
5210 /* Promote both alternatives. */
5212 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5213 op1 = default_conversion (op1);
5214 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5215 op2 = default_conversion (op2);
5217 if (TREE_CODE (ifexp) == ERROR_MARK
5218 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5219 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5220 return error_mark_node;
5222 tree bltin1 = NULL_TREE;
5223 tree bltin2 = NULL_TREE;
5224 type1 = type_or_builtin_type (op1, &bltin1);
5225 code1 = TREE_CODE (type1);
5226 type2 = type_or_builtin_type (op2, &bltin2);
5227 code2 = TREE_CODE (type2);
5229 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5230 return error_mark_node;
5232 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5233 return error_mark_node;
5235 /* C90 does not permit non-lvalue arrays in conditional expressions.
5236 In C99 they will be pointers by now. */
5237 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5239 error_at (colon_loc, "non-lvalue array in conditional expression");
5240 return error_mark_node;
5243 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5244 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5245 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5246 || code1 == COMPLEX_TYPE)
5247 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5248 || code2 == COMPLEX_TYPE))
5250 semantic_result_type = c_common_type (type1, type2);
5251 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5253 op1 = TREE_OPERAND (op1, 0);
5254 type1 = TREE_TYPE (op1);
5255 gcc_assert (TREE_CODE (type1) == code1);
5257 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5259 op2 = TREE_OPERAND (op2, 0);
5260 type2 = TREE_TYPE (op2);
5261 gcc_assert (TREE_CODE (type2) == code2);
5265 if (warn_cxx_compat)
5267 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5268 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5270 if (TREE_CODE (t1) == ENUMERAL_TYPE
5271 && TREE_CODE (t2) == ENUMERAL_TYPE
5272 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5273 warning_at (colon_loc, OPT_Wc___compat,
5274 ("different enum types in conditional is "
5275 "invalid in C++: %qT vs %qT"),
5276 t1, t2);
5279 /* Quickly detect the usual case where op1 and op2 have the same type
5280 after promotion. */
5281 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5283 if (type1 == type2)
5284 result_type = type1;
5285 else
5286 result_type = TYPE_MAIN_VARIANT (type1);
5288 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5289 || code1 == COMPLEX_TYPE)
5290 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5291 || code2 == COMPLEX_TYPE))
5293 /* In C11, a conditional expression between a floating-point
5294 type and an integer type should convert the integer type to
5295 the evaluation format of the floating-point type, with
5296 possible excess precision. */
5297 tree eptype1 = type1;
5298 tree eptype2 = type2;
5299 if (flag_isoc11)
5301 tree eptype;
5302 if (ANY_INTEGRAL_TYPE_P (type1)
5303 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5305 eptype2 = eptype;
5306 if (!semantic_result_type)
5307 semantic_result_type = c_common_type (type1, type2);
5309 else if (ANY_INTEGRAL_TYPE_P (type2)
5310 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5312 eptype1 = eptype;
5313 if (!semantic_result_type)
5314 semantic_result_type = c_common_type (type1, type2);
5317 result_type = c_common_type (eptype1, eptype2);
5318 if (result_type == error_mark_node)
5319 return error_mark_node;
5320 do_warn_double_promotion (result_type, type1, type2,
5321 "implicit conversion from %qT to %qT to "
5322 "match other result of conditional",
5323 colon_loc);
5325 /* If -Wsign-compare, warn here if type1 and type2 have
5326 different signedness. We'll promote the signed to unsigned
5327 and later code won't know it used to be different.
5328 Do this check on the original types, so that explicit casts
5329 will be considered, but default promotions won't. */
5330 if (c_inhibit_evaluation_warnings == 0)
5332 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5333 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5335 if (unsigned_op1 ^ unsigned_op2)
5337 bool ovf;
5339 /* Do not warn if the result type is signed, since the
5340 signed type will only be chosen if it can represent
5341 all the values of the unsigned type. */
5342 if (!TYPE_UNSIGNED (result_type))
5343 /* OK */;
5344 else
5346 bool op1_maybe_const = true;
5347 bool op2_maybe_const = true;
5349 /* Do not warn if the signed quantity is an
5350 unsuffixed integer literal (or some static
5351 constant expression involving such literals) and
5352 it is non-negative. This warning requires the
5353 operands to be folded for best results, so do
5354 that folding in this case even without
5355 warn_sign_compare to avoid warning options
5356 possibly affecting code generation. */
5357 c_inhibit_evaluation_warnings
5358 += (ifexp == truthvalue_false_node);
5359 op1 = c_fully_fold (op1, require_constant_value,
5360 &op1_maybe_const);
5361 c_inhibit_evaluation_warnings
5362 -= (ifexp == truthvalue_false_node);
5364 c_inhibit_evaluation_warnings
5365 += (ifexp == truthvalue_true_node);
5366 op2 = c_fully_fold (op2, require_constant_value,
5367 &op2_maybe_const);
5368 c_inhibit_evaluation_warnings
5369 -= (ifexp == truthvalue_true_node);
5371 if (warn_sign_compare)
5373 if ((unsigned_op2
5374 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5375 || (unsigned_op1
5376 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5377 /* OK */;
5378 else if (unsigned_op2)
5379 warning_at (op1_loc, OPT_Wsign_compare,
5380 "operand of %<?:%> changes signedness from "
5381 "%qT to %qT due to unsignedness of other "
5382 "operand", TREE_TYPE (orig_op1),
5383 TREE_TYPE (orig_op2));
5384 else
5385 warning_at (op2_loc, OPT_Wsign_compare,
5386 "operand of %<?:%> changes signedness from "
5387 "%qT to %qT due to unsignedness of other "
5388 "operand", TREE_TYPE (orig_op2),
5389 TREE_TYPE (orig_op1));
5391 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5392 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5393 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5394 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5399 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5401 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5402 pedwarn (colon_loc, OPT_Wpedantic,
5403 "ISO C forbids conditional expr with only one void side");
5404 result_type = void_type_node;
5406 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5408 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5409 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5410 addr_space_t as_common;
5412 if (comp_target_types (colon_loc, type1, type2))
5413 result_type = common_pointer_type (type1, type2);
5414 else if (null_pointer_constant_p (orig_op1))
5415 result_type = type2;
5416 else if (null_pointer_constant_p (orig_op2))
5417 result_type = type1;
5418 else if (!addr_space_superset (as1, as2, &as_common))
5420 error_at (colon_loc, "pointers to disjoint address spaces "
5421 "used in conditional expression");
5422 return error_mark_node;
5424 else if ((VOID_TYPE_P (TREE_TYPE (type1))
5425 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5426 || (VOID_TYPE_P (TREE_TYPE (type2))
5427 && !TYPE_ATOMIC (TREE_TYPE (type2))))
5429 tree t1 = TREE_TYPE (type1);
5430 tree t2 = TREE_TYPE (type2);
5431 if (!(VOID_TYPE_P (t1)
5432 && !TYPE_ATOMIC (t1)))
5434 /* roles are swapped */
5435 t1 = t2;
5436 t2 = TREE_TYPE (type1);
5438 tree t2_stripped = strip_array_types (t2);
5439 if ((TREE_CODE (t2) == ARRAY_TYPE)
5440 && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
5442 if (!flag_isoc2x)
5443 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5444 "pointer to array loses qualifier "
5445 "in conditional expression");
5446 else if (warn_c11_c2x_compat > 0)
5447 warning_at (colon_loc, OPT_Wc11_c2x_compat,
5448 "pointer to array loses qualifier "
5449 "in conditional expression in ISO C before C2X");
5451 if (TREE_CODE (t2) == FUNCTION_TYPE)
5452 pedwarn (colon_loc, OPT_Wpedantic,
5453 "ISO C forbids conditional expr between "
5454 "%<void *%> and function pointer");
5455 /* for array, use qualifiers of element type */
5456 if (flag_isoc2x)
5457 t2 = t2_stripped;
5458 result_type = build_pointer_type (qualify_type (t1, t2));
5460 /* Objective-C pointer comparisons are a bit more lenient. */
5461 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5462 result_type = objc_common_type (type1, type2);
5463 else
5465 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5466 if (bltin1 && bltin2)
5467 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5468 "pointer type mismatch between %qT and %qT "
5469 "of %qD and %qD in conditional expression",
5470 type1, type2, bltin1, bltin2);
5471 else
5472 pedwarn (colon_loc, 0,
5473 "pointer type mismatch in conditional expression");
5474 result_type = build_pointer_type
5475 (build_qualified_type (void_type_node, qual));
5478 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5480 if (!null_pointer_constant_p (orig_op2))
5481 pedwarn (colon_loc, 0,
5482 "pointer/integer type mismatch in conditional expression");
5483 else
5485 op2 = null_pointer_node;
5487 result_type = type1;
5489 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5491 if (!null_pointer_constant_p (orig_op1))
5492 pedwarn (colon_loc, 0,
5493 "pointer/integer type mismatch in conditional expression");
5494 else
5496 op1 = null_pointer_node;
5498 result_type = type2;
5501 if (!result_type)
5503 if (flag_cond_mismatch)
5504 result_type = void_type_node;
5505 else
5507 error_at (colon_loc, "type mismatch in conditional expression");
5508 return error_mark_node;
5512 /* Merge const and volatile flags of the incoming types. */
5513 result_type
5514 = build_type_variant (result_type,
5515 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5516 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5518 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5519 semantic_result_type);
5520 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5521 semantic_result_type);
5523 if (ifexp_bcp && ifexp == truthvalue_true_node)
5525 op2_int_operands = true;
5526 op1 = c_fully_fold (op1, require_constant_value, NULL);
5528 if (ifexp_bcp && ifexp == truthvalue_false_node)
5530 op1_int_operands = true;
5531 op2 = c_fully_fold (op2, require_constant_value, NULL);
5533 int_const = int_operands = (ifexp_int_operands
5534 && op1_int_operands
5535 && op2_int_operands);
5536 if (int_operands)
5538 int_const = ((ifexp == truthvalue_true_node
5539 && TREE_CODE (orig_op1) == INTEGER_CST
5540 && !TREE_OVERFLOW (orig_op1))
5541 || (ifexp == truthvalue_false_node
5542 && TREE_CODE (orig_op2) == INTEGER_CST
5543 && !TREE_OVERFLOW (orig_op2)));
5546 /* Need to convert condition operand into a vector mask. */
5547 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5549 tree vectype = TREE_TYPE (ifexp);
5550 tree elem_type = TREE_TYPE (vectype);
5551 tree zero = build_int_cst (elem_type, 0);
5552 tree zero_vec = build_vector_from_val (vectype, zero);
5553 tree cmp_type = truth_type_for (vectype);
5554 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5557 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5558 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5559 else
5561 if (int_operands)
5563 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5564 nested inside of the expression. */
5565 op1 = c_fully_fold (op1, false, NULL);
5566 op2 = c_fully_fold (op2, false, NULL);
5568 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5569 if (int_operands)
5570 ret = note_integer_operands (ret);
5572 if (semantic_result_type)
5573 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5575 protected_set_expr_location (ret, colon_loc);
5577 /* If the OP1 and OP2 are the same and don't have side-effects,
5578 warn here, because the COND_EXPR will be turned into OP1. */
5579 if (warn_duplicated_branches
5580 && TREE_CODE (ret) == COND_EXPR
5581 && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
5582 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5583 "this condition has identical branches");
5585 return ret;
5588 /* EXPR is an expression, location LOC, whose result is discarded.
5589 Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
5590 whose right-hand operand is such a call, possibly recursively). */
5592 static void
5593 maybe_warn_nodiscard (location_t loc, tree expr)
5595 if (VOID_TYPE_P (TREE_TYPE (expr)))
5596 return;
5597 while (TREE_CODE (expr) == COMPOUND_EXPR)
5599 expr = TREE_OPERAND (expr, 1);
5600 if (EXPR_HAS_LOCATION (expr))
5601 loc = EXPR_LOCATION (expr);
5603 if (TREE_CODE (expr) != CALL_EXPR)
5604 return;
5605 tree fn = CALL_EXPR_FN (expr);
5606 if (!fn)
5607 return;
5608 tree attr;
5609 if (TREE_CODE (fn) == ADDR_EXPR
5610 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5611 && (attr = lookup_attribute ("nodiscard",
5612 DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
5614 fn = TREE_OPERAND (fn, 0);
5615 tree args = TREE_VALUE (attr);
5616 if (args)
5617 args = TREE_VALUE (args);
5618 auto_diagnostic_group d;
5619 int warned;
5620 if (args)
5621 warned = warning_at (loc, OPT_Wunused_result,
5622 "ignoring return value of %qD, declared with "
5623 "attribute %<nodiscard%>: %E", fn, args);
5624 else
5625 warned = warning_at (loc, OPT_Wunused_result,
5626 "ignoring return value of %qD, declared with "
5627 "attribute %<nodiscard%>", fn);
5628 if (warned)
5629 inform (DECL_SOURCE_LOCATION (fn), "declared here");
5631 else
5633 tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
5634 attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
5635 if (!attr)
5636 return;
5637 tree args = TREE_VALUE (attr);
5638 if (args)
5639 args = TREE_VALUE (args);
5640 auto_diagnostic_group d;
5641 int warned;
5642 if (args)
5643 warned = warning_at (loc, OPT_Wunused_result,
5644 "ignoring return value of type %qT, declared "
5645 "with attribute %<nodiscard%>: %E",
5646 rettype, args);
5647 else
5648 warned = warning_at (loc, OPT_Wunused_result,
5649 "ignoring return value of type %qT, declared "
5650 "with attribute %<nodiscard%>", rettype);
5651 if (warned)
5653 if (TREE_CODE (fn) == ADDR_EXPR)
5655 fn = TREE_OPERAND (fn, 0);
5656 if (TREE_CODE (fn) == FUNCTION_DECL)
5657 inform (DECL_SOURCE_LOCATION (fn),
5658 "in call to %qD, declared here", fn);
5664 /* Return a compound expression that performs two expressions and
5665 returns the value of the second of them.
5667 LOC is the location of the COMPOUND_EXPR. */
5669 tree
5670 build_compound_expr (location_t loc, tree expr1, tree expr2)
5672 bool expr1_int_operands, expr2_int_operands;
5673 tree eptype = NULL_TREE;
5674 tree ret;
5676 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5677 if (expr1_int_operands)
5678 expr1 = remove_c_maybe_const_expr (expr1);
5679 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5680 if (expr2_int_operands)
5681 expr2 = remove_c_maybe_const_expr (expr2);
5683 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5684 expr1 = TREE_OPERAND (expr1, 0);
5685 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5687 eptype = TREE_TYPE (expr2);
5688 expr2 = TREE_OPERAND (expr2, 0);
5691 if (!TREE_SIDE_EFFECTS (expr1))
5693 /* The left-hand operand of a comma expression is like an expression
5694 statement: with -Wunused, we should warn if it doesn't have
5695 any side-effects, unless it was explicitly cast to (void). */
5696 if (warn_unused_value)
5698 if (VOID_TYPE_P (TREE_TYPE (expr1))
5699 && CONVERT_EXPR_P (expr1))
5700 ; /* (void) a, b */
5701 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5702 && TREE_CODE (expr1) == COMPOUND_EXPR
5703 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5704 ; /* (void) a, (void) b, c */
5705 else
5706 warning_at (loc, OPT_Wunused_value,
5707 "left-hand operand of comma expression has no effect");
5710 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5711 && warn_unused_value)
5713 tree r = expr1;
5714 location_t cloc = loc;
5715 while (TREE_CODE (r) == COMPOUND_EXPR)
5717 if (EXPR_HAS_LOCATION (r))
5718 cloc = EXPR_LOCATION (r);
5719 r = TREE_OPERAND (r, 1);
5721 if (!TREE_SIDE_EFFECTS (r)
5722 && !VOID_TYPE_P (TREE_TYPE (r))
5723 && !CONVERT_EXPR_P (r))
5724 warning_at (cloc, OPT_Wunused_value,
5725 "right-hand operand of comma expression has no effect");
5728 /* With -Wunused, we should also warn if the left-hand operand does have
5729 side-effects, but computes a value which is not used. For example, in
5730 `foo() + bar(), baz()' the result of the `+' operator is not used,
5731 so we should issue a warning. */
5732 else if (warn_unused_value)
5733 warn_if_unused_value (expr1, loc);
5735 maybe_warn_nodiscard (loc, expr1);
5737 if (expr2 == error_mark_node)
5738 return error_mark_node;
5740 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5742 if (flag_isoc99
5743 && expr1_int_operands
5744 && expr2_int_operands)
5745 ret = note_integer_operands (ret);
5747 if (eptype)
5748 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5750 protected_set_expr_location (ret, loc);
5751 return ret;
5754 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5755 which we are casting. OTYPE is the type of the expression being
5756 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5757 of the cast. -Wcast-qual appeared on the command line. Named
5758 address space qualifiers are not handled here, because they result
5759 in different warnings. */
5761 static void
5762 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5764 tree in_type = type;
5765 tree in_otype = otype;
5766 int added = 0;
5767 int discarded = 0;
5768 bool is_const;
5770 /* Check that the qualifiers on IN_TYPE are a superset of the
5771 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5772 nodes is uninteresting and we stop as soon as we hit a
5773 non-POINTER_TYPE node on either type. */
5776 in_otype = TREE_TYPE (in_otype);
5777 in_type = TREE_TYPE (in_type);
5779 /* GNU C allows cv-qualified function types. 'const' means the
5780 function is very pure, 'volatile' means it can't return. We
5781 need to warn when such qualifiers are added, not when they're
5782 taken away. */
5783 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5784 && TREE_CODE (in_type) == FUNCTION_TYPE)
5785 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5786 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5787 else
5788 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5789 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5791 while (TREE_CODE (in_type) == POINTER_TYPE
5792 && TREE_CODE (in_otype) == POINTER_TYPE);
5794 if (added)
5795 warning_at (loc, OPT_Wcast_qual,
5796 "cast adds %q#v qualifier to function type", added);
5798 if (discarded)
5799 /* There are qualifiers present in IN_OTYPE that are not present
5800 in IN_TYPE. */
5801 warning_at (loc, OPT_Wcast_qual,
5802 "cast discards %qv qualifier from pointer target type",
5803 discarded);
5805 if (added || discarded)
5806 return;
5808 /* A cast from **T to const **T is unsafe, because it can cause a
5809 const value to be changed with no additional warning. We only
5810 issue this warning if T is the same on both sides, and we only
5811 issue the warning if there are the same number of pointers on
5812 both sides, as otherwise the cast is clearly unsafe anyhow. A
5813 cast is unsafe when a qualifier is added at one level and const
5814 is not present at all outer levels.
5816 To issue this warning, we check at each level whether the cast
5817 adds new qualifiers not already seen. We don't need to special
5818 case function types, as they won't have the same
5819 TYPE_MAIN_VARIANT. */
5821 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5822 return;
5823 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5824 return;
5826 in_type = type;
5827 in_otype = otype;
5828 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5831 in_type = TREE_TYPE (in_type);
5832 in_otype = TREE_TYPE (in_otype);
5833 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5834 && !is_const)
5836 warning_at (loc, OPT_Wcast_qual,
5837 "to be safe all intermediate pointers in cast from "
5838 "%qT to %qT must be %<const%> qualified",
5839 otype, type);
5840 break;
5842 if (is_const)
5843 is_const = TYPE_READONLY (in_type);
5845 while (TREE_CODE (in_type) == POINTER_TYPE);
5848 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5850 static bool
5851 c_safe_arg_type_equiv_p (tree t1, tree t2)
5853 t1 = TYPE_MAIN_VARIANT (t1);
5854 t2 = TYPE_MAIN_VARIANT (t2);
5856 if (TREE_CODE (t1) == POINTER_TYPE
5857 && TREE_CODE (t2) == POINTER_TYPE)
5858 return true;
5860 /* The signedness of the parameter matters only when an integral
5861 type smaller than int is promoted to int, otherwise only the
5862 precision of the parameter matters.
5863 This check should make sure that the callee does not see
5864 undefined values in argument registers. */
5865 if (INTEGRAL_TYPE_P (t1)
5866 && INTEGRAL_TYPE_P (t2)
5867 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5868 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5869 || !targetm.calls.promote_prototypes (NULL_TREE)
5870 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5871 return true;
5873 return comptypes (t1, t2);
5876 /* Check if a type cast between two function types can be considered safe. */
5878 static bool
5879 c_safe_function_type_cast_p (tree t1, tree t2)
5881 if (TREE_TYPE (t1) == void_type_node &&
5882 TYPE_ARG_TYPES (t1) == void_list_node)
5883 return true;
5885 if (TREE_TYPE (t2) == void_type_node &&
5886 TYPE_ARG_TYPES (t2) == void_list_node)
5887 return true;
5889 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5890 return false;
5892 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5893 t1 && t2;
5894 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5895 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5896 return false;
5898 return true;
5901 /* Build an expression representing a cast to type TYPE of expression EXPR.
5902 LOC is the location of the cast-- typically the open paren of the cast. */
5904 tree
5905 build_c_cast (location_t loc, tree type, tree expr)
5907 tree value;
5909 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
5911 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5912 expr = TREE_OPERAND (expr, 0);
5914 value = expr;
5915 if (int_operands)
5916 value = remove_c_maybe_const_expr (value);
5918 if (type == error_mark_node || expr == error_mark_node)
5919 return error_mark_node;
5921 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5922 only in <protocol> qualifications. But when constructing cast expressions,
5923 the protocols do matter and must be kept around. */
5924 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5925 return build1 (NOP_EXPR, type, expr);
5927 type = TYPE_MAIN_VARIANT (type);
5929 if (TREE_CODE (type) == ARRAY_TYPE)
5931 error_at (loc, "cast specifies array type");
5932 return error_mark_node;
5935 if (TREE_CODE (type) == FUNCTION_TYPE)
5937 error_at (loc, "cast specifies function type");
5938 return error_mark_node;
5941 if (!VOID_TYPE_P (type))
5943 value = require_complete_type (loc, value);
5944 if (value == error_mark_node)
5945 return error_mark_node;
5948 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5950 if (RECORD_OR_UNION_TYPE_P (type))
5951 pedwarn (loc, OPT_Wpedantic,
5952 "ISO C forbids casting nonscalar to the same type");
5954 /* Convert to remove any qualifiers from VALUE's type. */
5955 value = convert (type, value);
5957 else if (TREE_CODE (type) == UNION_TYPE)
5959 tree field;
5961 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5962 if (TREE_TYPE (field) != error_mark_node
5963 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5964 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5965 break;
5967 if (field)
5969 tree t;
5970 bool maybe_const = true;
5972 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5973 t = c_fully_fold (value, false, &maybe_const);
5974 t = build_constructor_single (type, field, t);
5975 if (!maybe_const)
5976 t = c_wrap_maybe_const (t, true);
5977 t = digest_init (loc, type, t,
5978 NULL_TREE, false, true, 0);
5979 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5980 return t;
5982 error_at (loc, "cast to union type from type not present in union");
5983 return error_mark_node;
5985 else
5987 tree otype, ovalue;
5989 if (type == void_type_node)
5991 tree t = build1 (CONVERT_EXPR, type, value);
5992 SET_EXPR_LOCATION (t, loc);
5993 return t;
5996 otype = TREE_TYPE (value);
5998 /* Optionally warn about potentially worrisome casts. */
5999 if (warn_cast_qual
6000 && TREE_CODE (type) == POINTER_TYPE
6001 && TREE_CODE (otype) == POINTER_TYPE)
6002 handle_warn_cast_qual (loc, type, otype);
6004 /* Warn about conversions between pointers to disjoint
6005 address spaces. */
6006 if (TREE_CODE (type) == POINTER_TYPE
6007 && TREE_CODE (otype) == POINTER_TYPE
6008 && !null_pointer_constant_p (value))
6010 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
6011 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
6012 addr_space_t as_common;
6014 if (!addr_space_superset (as_to, as_from, &as_common))
6016 if (ADDR_SPACE_GENERIC_P (as_from))
6017 warning_at (loc, 0, "cast to %s address space pointer "
6018 "from disjoint generic address space pointer",
6019 c_addr_space_name (as_to));
6021 else if (ADDR_SPACE_GENERIC_P (as_to))
6022 warning_at (loc, 0, "cast to generic address space pointer "
6023 "from disjoint %s address space pointer",
6024 c_addr_space_name (as_from));
6026 else
6027 warning_at (loc, 0, "cast to %s address space pointer "
6028 "from disjoint %s address space pointer",
6029 c_addr_space_name (as_to),
6030 c_addr_space_name (as_from));
6034 /* Warn about possible alignment problems. */
6035 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
6036 && TREE_CODE (type) == POINTER_TYPE
6037 && TREE_CODE (otype) == POINTER_TYPE
6038 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
6039 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6040 /* Don't warn about opaque types, where the actual alignment
6041 restriction is unknown. */
6042 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
6043 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
6044 && min_align_of_type (TREE_TYPE (type))
6045 > min_align_of_type (TREE_TYPE (otype)))
6046 warning_at (loc, OPT_Wcast_align,
6047 "cast increases required alignment of target type");
6049 if (TREE_CODE (type) == INTEGER_TYPE
6050 && TREE_CODE (otype) == POINTER_TYPE
6051 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
6052 /* Unlike conversion of integers to pointers, where the
6053 warning is disabled for converting constants because
6054 of cases such as SIG_*, warn about converting constant
6055 pointers to integers. In some cases it may cause unwanted
6056 sign extension, and a warning is appropriate. */
6057 warning_at (loc, OPT_Wpointer_to_int_cast,
6058 "cast from pointer to integer of different size");
6060 if (TREE_CODE (value) == CALL_EXPR
6061 && TREE_CODE (type) != TREE_CODE (otype))
6062 warning_at (loc, OPT_Wbad_function_cast,
6063 "cast from function call of type %qT "
6064 "to non-matching type %qT", otype, type);
6066 if (TREE_CODE (type) == POINTER_TYPE
6067 && TREE_CODE (otype) == INTEGER_TYPE
6068 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
6069 /* Don't warn about converting any constant. */
6070 && !TREE_CONSTANT (value))
6071 warning_at (loc,
6072 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
6073 "of different size");
6075 if (warn_strict_aliasing <= 2)
6076 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
6078 /* If pedantic, warn for conversions between function and object
6079 pointer types, except for converting a null pointer constant
6080 to function pointer type. */
6081 if (pedantic
6082 && TREE_CODE (type) == POINTER_TYPE
6083 && TREE_CODE (otype) == POINTER_TYPE
6084 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6085 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
6086 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6087 "conversion of function pointer to object pointer type");
6089 if (pedantic
6090 && TREE_CODE (type) == POINTER_TYPE
6091 && TREE_CODE (otype) == POINTER_TYPE
6092 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6093 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6094 && !null_pointer_constant_p (value))
6095 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6096 "conversion of object pointer to function pointer type");
6098 if (TREE_CODE (type) == POINTER_TYPE
6099 && TREE_CODE (otype) == POINTER_TYPE
6100 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6101 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6102 && !c_safe_function_type_cast_p (TREE_TYPE (type),
6103 TREE_TYPE (otype)))
6104 warning_at (loc, OPT_Wcast_function_type,
6105 "cast between incompatible function types"
6106 " from %qT to %qT", otype, type);
6108 ovalue = value;
6109 value = convert (type, value);
6111 /* Ignore any integer overflow caused by the cast. */
6112 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
6114 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6116 if (!TREE_OVERFLOW (value))
6118 /* Avoid clobbering a shared constant. */
6119 value = copy_node (value);
6120 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
6123 else if (TREE_OVERFLOW (value))
6124 /* Reset VALUE's overflow flags, ensuring constant sharing. */
6125 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6129 /* Don't let a cast be an lvalue. */
6130 if (lvalue_p (value))
6131 value = non_lvalue_loc (loc, value);
6133 /* Don't allow the results of casting to floating-point or complex
6134 types be confused with actual constants, or casts involving
6135 integer and pointer types other than direct integer-to-integer
6136 and integer-to-pointer be confused with integer constant
6137 expressions and null pointer constants. */
6138 if (TREE_CODE (value) == REAL_CST
6139 || TREE_CODE (value) == COMPLEX_CST
6140 || (TREE_CODE (value) == INTEGER_CST
6141 && !((TREE_CODE (expr) == INTEGER_CST
6142 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6143 || TREE_CODE (expr) == REAL_CST
6144 || TREE_CODE (expr) == COMPLEX_CST)))
6145 value = build1 (NOP_EXPR, type, value);
6147 /* If the expression has integer operands and so can occur in an
6148 unevaluated part of an integer constant expression, ensure the
6149 return value reflects this. */
6150 if (int_operands
6151 && INTEGRAL_TYPE_P (type)
6152 && value != error_mark_node
6153 && !EXPR_INT_CONST_OPERANDS (value))
6154 value = note_integer_operands (value);
6156 protected_set_expr_location (value, loc);
6157 return value;
6160 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
6161 location of the open paren of the cast, or the position of the cast
6162 expr. */
6163 tree
6164 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6166 tree type;
6167 tree type_expr = NULL_TREE;
6168 bool type_expr_const = true;
6169 tree ret;
6170 int saved_wsp = warn_strict_prototypes;
6172 /* This avoids warnings about unprototyped casts on
6173 integers. E.g. "#define SIG_DFL (void(*)())0". */
6174 if (TREE_CODE (expr) == INTEGER_CST)
6175 warn_strict_prototypes = 0;
6176 type = groktypename (type_name, &type_expr, &type_expr_const);
6177 warn_strict_prototypes = saved_wsp;
6179 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6180 && reject_gcc_builtin (expr))
6181 return error_mark_node;
6183 ret = build_c_cast (loc, type, expr);
6184 if (type_expr)
6186 bool inner_expr_const = true;
6187 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6188 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6189 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6190 && inner_expr_const);
6191 SET_EXPR_LOCATION (ret, loc);
6194 if (!EXPR_HAS_LOCATION (ret))
6195 protected_set_expr_location (ret, loc);
6197 /* C++ does not permits types to be defined in a cast, but it
6198 allows references to incomplete types. */
6199 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6200 warning_at (loc, OPT_Wc___compat,
6201 "defining a type in a cast is invalid in C++");
6203 return ret;
6206 /* Build an assignment expression of lvalue LHS from value RHS.
6207 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6208 may differ from TREE_TYPE (LHS) for an enum bitfield.
6209 MODIFYCODE is the code for a binary operator that we use
6210 to combine the old value of LHS with RHS to get the new value.
6211 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6212 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6213 which may differ from TREE_TYPE (RHS) for an enum value.
6215 LOCATION is the location of the MODIFYCODE operator.
6216 RHS_LOC is the location of the RHS. */
6218 tree
6219 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6220 enum tree_code modifycode,
6221 location_t rhs_loc, tree rhs, tree rhs_origtype)
6223 tree result;
6224 tree newrhs;
6225 tree rhseval = NULL_TREE;
6226 tree lhstype = TREE_TYPE (lhs);
6227 tree olhstype = lhstype;
6228 bool npc;
6229 bool is_atomic_op;
6231 /* Types that aren't fully specified cannot be used in assignments. */
6232 lhs = require_complete_type (location, lhs);
6234 /* Avoid duplicate error messages from operands that had errors. */
6235 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6236 return error_mark_node;
6238 /* Ensure an error for assigning a non-lvalue array to an array in
6239 C90. */
6240 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6242 error_at (location, "assignment to expression with array type");
6243 return error_mark_node;
6246 /* For ObjC properties, defer this check. */
6247 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6248 return error_mark_node;
6250 is_atomic_op = really_atomic_lvalue (lhs);
6252 newrhs = rhs;
6254 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6256 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6257 lhs_origtype, modifycode, rhs_loc, rhs,
6258 rhs_origtype);
6259 if (inner == error_mark_node)
6260 return error_mark_node;
6261 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6262 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6263 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6264 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6265 protected_set_expr_location (result, location);
6266 return result;
6269 /* If a binary op has been requested, combine the old LHS value with the RHS
6270 producing the value we should actually store into the LHS. */
6272 if (modifycode != NOP_EXPR)
6274 lhs = c_fully_fold (lhs, false, NULL, true);
6275 lhs = stabilize_reference (lhs);
6277 /* Construct the RHS for any non-atomic compound assignemnt. */
6278 if (!is_atomic_op)
6280 /* If in LHS op= RHS the RHS has side-effects, ensure they
6281 are preevaluated before the rest of the assignment expression's
6282 side-effects, because RHS could contain e.g. function calls
6283 that modify LHS. */
6284 if (TREE_SIDE_EFFECTS (rhs))
6286 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6287 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6288 else
6289 newrhs = save_expr (rhs);
6290 rhseval = newrhs;
6291 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6292 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6293 newrhs);
6295 newrhs = build_binary_op (location,
6296 modifycode, lhs, newrhs, true);
6298 /* The original type of the right hand side is no longer
6299 meaningful. */
6300 rhs_origtype = NULL_TREE;
6304 if (c_dialect_objc ())
6306 /* Check if we are modifying an Objective-C property reference;
6307 if so, we need to generate setter calls. */
6308 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6309 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6310 else
6311 result = objc_maybe_build_modify_expr (lhs, newrhs);
6312 if (result)
6313 goto return_result;
6315 /* Else, do the check that we postponed for Objective-C. */
6316 if (!lvalue_or_else (location, lhs, lv_assign))
6317 return error_mark_node;
6320 /* Give an error for storing in something that is 'const'. */
6322 if (TYPE_READONLY (lhstype)
6323 || (RECORD_OR_UNION_TYPE_P (lhstype)
6324 && C_TYPE_FIELDS_READONLY (lhstype)))
6326 readonly_error (location, lhs, lv_assign);
6327 return error_mark_node;
6329 else if (TREE_READONLY (lhs))
6330 readonly_warning (lhs, lv_assign);
6332 /* If storing into a structure or union member,
6333 it has probably been given type `int'.
6334 Compute the type that would go with
6335 the actual amount of storage the member occupies. */
6337 if (TREE_CODE (lhs) == COMPONENT_REF
6338 && (TREE_CODE (lhstype) == INTEGER_TYPE
6339 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6340 || TREE_CODE (lhstype) == REAL_TYPE
6341 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6342 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6344 /* If storing in a field that is in actuality a short or narrower than one,
6345 we must store in the field in its actual type. */
6347 if (lhstype != TREE_TYPE (lhs))
6349 lhs = copy_node (lhs);
6350 TREE_TYPE (lhs) = lhstype;
6353 /* Issue -Wc++-compat warnings about an assignment to an enum type
6354 when LHS does not have its original type. This happens for,
6355 e.g., an enum bitfield in a struct. */
6356 if (warn_cxx_compat
6357 && lhs_origtype != NULL_TREE
6358 && lhs_origtype != lhstype
6359 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6361 tree checktype = (rhs_origtype != NULL_TREE
6362 ? rhs_origtype
6363 : TREE_TYPE (rhs));
6364 if (checktype != error_mark_node
6365 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6366 || (is_atomic_op && modifycode != NOP_EXPR)))
6367 warning_at (location, OPT_Wc___compat,
6368 "enum conversion in assignment is invalid in C++");
6371 /* Remove qualifiers. */
6372 lhstype = build_qualified_type (lhstype, TYPE_UNQUALIFIED);
6373 olhstype = build_qualified_type (olhstype, TYPE_UNQUALIFIED);
6375 /* Convert new value to destination type. Fold it first, then
6376 restore any excess precision information, for the sake of
6377 conversion warnings. */
6379 if (!(is_atomic_op && modifycode != NOP_EXPR))
6381 tree rhs_semantic_type = NULL_TREE;
6382 if (!c_in_omp_for)
6384 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6386 rhs_semantic_type = TREE_TYPE (newrhs);
6387 newrhs = TREE_OPERAND (newrhs, 0);
6389 npc = null_pointer_constant_p (newrhs);
6390 newrhs = c_fully_fold (newrhs, false, NULL);
6391 if (rhs_semantic_type)
6392 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6394 else
6395 npc = null_pointer_constant_p (newrhs);
6396 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6397 rhs_origtype, ic_assign, npc,
6398 NULL_TREE, NULL_TREE, 0);
6399 if (TREE_CODE (newrhs) == ERROR_MARK)
6400 return error_mark_node;
6403 /* Emit ObjC write barrier, if necessary. */
6404 if (c_dialect_objc () && flag_objc_gc)
6406 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6407 if (result)
6409 protected_set_expr_location (result, location);
6410 goto return_result;
6414 /* Scan operands. */
6416 if (is_atomic_op)
6417 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6418 else
6420 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6421 TREE_SIDE_EFFECTS (result) = 1;
6422 protected_set_expr_location (result, location);
6425 /* If we got the LHS in a different type for storing in,
6426 convert the result back to the nominal type of LHS
6427 so that the value we return always has the same type
6428 as the LHS argument. */
6430 if (olhstype == TREE_TYPE (result))
6431 goto return_result;
6433 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6434 rhs_origtype, ic_assign, false, NULL_TREE,
6435 NULL_TREE, 0);
6436 protected_set_expr_location (result, location);
6438 return_result:
6439 if (rhseval)
6440 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6441 return result;
6444 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6445 This is used to implement -fplan9-extensions. */
6447 static bool
6448 find_anonymous_field_with_type (tree struct_type, tree type)
6450 tree field;
6451 bool found;
6453 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6454 found = false;
6455 for (field = TYPE_FIELDS (struct_type);
6456 field != NULL_TREE;
6457 field = TREE_CHAIN (field))
6459 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6460 ? c_build_qualified_type (TREE_TYPE (field),
6461 TYPE_QUAL_ATOMIC)
6462 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6463 if (DECL_NAME (field) == NULL
6464 && comptypes (type, fieldtype))
6466 if (found)
6467 return false;
6468 found = true;
6470 else if (DECL_NAME (field) == NULL
6471 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6472 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6474 if (found)
6475 return false;
6476 found = true;
6479 return found;
6482 /* RHS is an expression whose type is pointer to struct. If there is
6483 an anonymous field in RHS with type TYPE, then return a pointer to
6484 that field in RHS. This is used with -fplan9-extensions. This
6485 returns NULL if no conversion could be found. */
6487 static tree
6488 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6490 tree rhs_struct_type, lhs_main_type;
6491 tree field, found_field;
6492 bool found_sub_field;
6493 tree ret;
6495 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6496 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6497 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6499 gcc_assert (POINTER_TYPE_P (type));
6500 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6501 ? c_build_qualified_type (TREE_TYPE (type),
6502 TYPE_QUAL_ATOMIC)
6503 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6505 found_field = NULL_TREE;
6506 found_sub_field = false;
6507 for (field = TYPE_FIELDS (rhs_struct_type);
6508 field != NULL_TREE;
6509 field = TREE_CHAIN (field))
6511 if (DECL_NAME (field) != NULL_TREE
6512 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6513 continue;
6514 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6515 ? c_build_qualified_type (TREE_TYPE (field),
6516 TYPE_QUAL_ATOMIC)
6517 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6518 if (comptypes (lhs_main_type, fieldtype))
6520 if (found_field != NULL_TREE)
6521 return NULL_TREE;
6522 found_field = field;
6524 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6525 lhs_main_type))
6527 if (found_field != NULL_TREE)
6528 return NULL_TREE;
6529 found_field = field;
6530 found_sub_field = true;
6534 if (found_field == NULL_TREE)
6535 return NULL_TREE;
6537 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6538 build_fold_indirect_ref (rhs), found_field,
6539 NULL_TREE);
6540 ret = build_fold_addr_expr_loc (location, ret);
6542 if (found_sub_field)
6544 ret = convert_to_anonymous_field (location, type, ret);
6545 gcc_assert (ret != NULL_TREE);
6548 return ret;
6551 /* Issue an error message for a bad initializer component.
6552 GMSGID identifies the message.
6553 The component name is taken from the spelling stack. */
6555 static void ATTRIBUTE_GCC_DIAG (2,0)
6556 error_init (location_t loc, const char *gmsgid, ...)
6558 char *ofwhat;
6560 auto_diagnostic_group d;
6562 /* The gmsgid may be a format string with %< and %>. */
6563 va_list ap;
6564 va_start (ap, gmsgid);
6565 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6566 va_end (ap);
6568 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6569 if (*ofwhat && warned)
6570 inform (loc, "(near initialization for %qs)", ofwhat);
6573 /* Issue a pedantic warning for a bad initializer component. OPT is
6574 the option OPT_* (from options.h) controlling this warning or 0 if
6575 it is unconditionally given. GMSGID identifies the message. The
6576 component name is taken from the spelling stack. */
6578 static void ATTRIBUTE_GCC_DIAG (3,0)
6579 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6581 /* Use the location where a macro was expanded rather than where
6582 it was defined to make sure macros defined in system headers
6583 but used incorrectly elsewhere are diagnosed. */
6584 location_t exploc = expansion_point_location_if_in_system_header (loc);
6585 auto_diagnostic_group d;
6586 va_list ap;
6587 va_start (ap, gmsgid);
6588 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6589 va_end (ap);
6590 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6591 if (*ofwhat && warned)
6592 inform (exploc, "(near initialization for %qs)", ofwhat);
6595 /* Issue a warning for a bad initializer component.
6597 OPT is the OPT_W* value corresponding to the warning option that
6598 controls this warning. GMSGID identifies the message. The
6599 component name is taken from the spelling stack. */
6601 static void
6602 warning_init (location_t loc, int opt, const char *gmsgid)
6604 char *ofwhat;
6605 bool warned;
6607 auto_diagnostic_group d;
6609 /* Use the location where a macro was expanded rather than where
6610 it was defined to make sure macros defined in system headers
6611 but used incorrectly elsewhere are diagnosed. */
6612 location_t exploc = expansion_point_location_if_in_system_header (loc);
6614 /* The gmsgid may be a format string with %< and %>. */
6615 warned = warning_at (exploc, opt, gmsgid);
6616 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6617 if (*ofwhat && warned)
6618 inform (exploc, "(near initialization for %qs)", ofwhat);
6621 /* If TYPE is an array type and EXPR is a parenthesized string
6622 constant, warn if pedantic that EXPR is being used to initialize an
6623 object of type TYPE. */
6625 void
6626 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6628 if (pedantic
6629 && TREE_CODE (type) == ARRAY_TYPE
6630 && TREE_CODE (expr.value) == STRING_CST
6631 && expr.original_code != STRING_CST)
6632 pedwarn_init (loc, OPT_Wpedantic,
6633 "array initialized from parenthesized string constant");
6636 /* Attempt to locate the parameter with the given index within FNDECL,
6637 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6639 static location_t
6640 get_fndecl_argument_location (tree fndecl, int argnum)
6642 int i;
6643 tree param;
6645 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6646 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6647 i < argnum && param;
6648 i++, param = TREE_CHAIN (param))
6651 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6652 return DECL_SOURCE_LOCATION (FNDECL). */
6653 if (param == NULL)
6654 return DECL_SOURCE_LOCATION (fndecl);
6656 return DECL_SOURCE_LOCATION (param);
6659 /* Issue a note about a mismatching argument for parameter PARMNUM
6660 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6661 Attempt to issue the note at the pertinent parameter of the decl;
6662 failing that issue it at the location of FUNDECL; failing that
6663 issue it at PLOC. */
6665 static void
6666 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6667 tree expected_type, tree actual_type)
6669 location_t loc;
6670 if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6671 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6672 else
6673 loc = ploc;
6675 inform (loc,
6676 "expected %qT but argument is of type %qT",
6677 expected_type, actual_type);
6680 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6681 function FUNDECL declared without prototype to parameter PARMNUM of
6682 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6684 static void
6685 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6686 tree parmtype, tree argtype)
6688 tree_code parmcode = TREE_CODE (parmtype);
6689 tree_code argcode = TREE_CODE (argtype);
6690 tree promoted = c_type_promotes_to (argtype);
6692 /* Avoid warning for enum arguments that promote to an integer type
6693 of the same size/mode. */
6694 if (parmcode == INTEGER_TYPE
6695 && argcode == ENUMERAL_TYPE
6696 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6697 return;
6699 if ((parmcode == argcode
6700 || (parmcode == INTEGER_TYPE
6701 && argcode == ENUMERAL_TYPE))
6702 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6703 return;
6705 /* This diagnoses even signed/unsigned mismatches. Those might be
6706 safe in many cases but GCC may emit suboptimal code for them so
6707 warning on those cases drives efficiency improvements. */
6708 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6709 TYPE_MAIN_VARIANT (promoted) == argtype
6710 ? G_("%qD argument %d type is %qT where %qT is expected "
6711 "in a call to built-in function declared without "
6712 "prototype")
6713 : G_("%qD argument %d promotes to %qT where %qT is expected "
6714 "in a call to built-in function declared without "
6715 "prototype"),
6716 fundecl, parmnum, promoted, parmtype))
6717 inform (DECL_SOURCE_LOCATION (fundecl),
6718 "built-in %qD declared here",
6719 fundecl);
6722 /* Convert value RHS to type TYPE as preparation for an assignment to
6723 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6724 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6725 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6726 constant before any folding.
6727 The real work of conversion is done by `convert'.
6728 The purpose of this function is to generate error messages
6729 for assignments that are not allowed in C.
6730 ERRTYPE says whether it is argument passing, assignment,
6731 initialization or return.
6733 In the following example, '~' denotes where EXPR_LOC and '^' where
6734 LOCATION point to:
6736 f (var); [ic_argpass]
6737 ^ ~~~
6738 x = var; [ic_assign]
6739 ^ ~~~;
6740 int x = var; [ic_init]
6742 return x; [ic_return]
6745 FUNCTION is a tree for the function being called.
6746 PARMNUM is the number of the argument, for printing in error messages.
6747 WARNOPT may be set to a warning option to issue the corresponding warning
6748 rather than an error for invalid conversions. Used for calls to built-in
6749 functions declared without a prototype. */
6751 static tree
6752 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6753 tree rhs, tree origtype, enum impl_conv errtype,
6754 bool null_pointer_constant, tree fundecl,
6755 tree function, int parmnum, int warnopt /* = 0 */)
6757 enum tree_code codel = TREE_CODE (type);
6758 tree orig_rhs = rhs;
6759 tree rhstype;
6760 enum tree_code coder;
6761 tree rname = NULL_TREE;
6762 bool objc_ok = false;
6764 /* Use the expansion point location to handle cases such as user's
6765 function returning a wrong-type macro defined in a system header. */
6766 location = expansion_point_location_if_in_system_header (location);
6768 if (errtype == ic_argpass)
6770 tree selector;
6771 /* Change pointer to function to the function itself for
6772 diagnostics. */
6773 if (TREE_CODE (function) == ADDR_EXPR
6774 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6775 function = TREE_OPERAND (function, 0);
6777 /* Handle an ObjC selector specially for diagnostics. */
6778 selector = objc_message_selector ();
6779 rname = function;
6780 if (selector && parmnum > 2)
6782 rname = selector;
6783 parmnum -= 2;
6787 /* This macro is used to emit diagnostics to ensure that all format
6788 strings are complete sentences, visible to gettext and checked at
6789 compile time. */
6790 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6791 do { \
6792 switch (errtype) \
6794 case ic_argpass: \
6796 auto_diagnostic_group d; \
6797 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6798 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6800 break; \
6801 case ic_assign: \
6802 pedwarn (LOCATION, OPT, AS); \
6803 break; \
6804 case ic_init: \
6805 pedwarn_init (LOCATION, OPT, IN); \
6806 break; \
6807 case ic_return: \
6808 pedwarn (LOCATION, OPT, RE); \
6809 break; \
6810 default: \
6811 gcc_unreachable (); \
6813 } while (0)
6815 /* This macro is used to emit diagnostics to ensure that all format
6816 strings are complete sentences, visible to gettext and checked at
6817 compile time. It can be called with 'pedwarn' or 'warning_at'. */
6818 #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6819 do { \
6820 switch (errtype) \
6822 case ic_argpass: \
6824 auto_diagnostic_group d; \
6825 if (PEDWARN) { \
6826 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6827 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6828 } else { \
6829 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6830 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6833 break; \
6834 case ic_assign: \
6835 if (PEDWARN) \
6836 pedwarn (LOCATION, OPT, AS, QUALS); \
6837 else \
6838 warning_at (LOCATION, OPT, AS, QUALS); \
6839 break; \
6840 case ic_init: \
6841 if (PEDWARN) \
6842 pedwarn (LOCATION, OPT, IN, QUALS); \
6843 else \
6844 warning_at (LOCATION, OPT, IN, QUALS); \
6845 break; \
6846 case ic_return: \
6847 if (PEDWARN) \
6848 pedwarn (LOCATION, OPT, RE, QUALS); \
6849 else \
6850 warning_at (LOCATION, OPT, RE, QUALS); \
6851 break; \
6852 default: \
6853 gcc_unreachable (); \
6855 } while (0)
6857 /* This macro is used to emit diagnostics to ensure that all format
6858 strings are complete sentences, visible to gettext and checked at
6859 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6860 extra parameter to enumerate qualifiers. */
6861 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6862 WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
6865 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6866 rhs = TREE_OPERAND (rhs, 0);
6868 rhstype = TREE_TYPE (rhs);
6869 coder = TREE_CODE (rhstype);
6871 if (coder == ERROR_MARK)
6872 return error_mark_node;
6874 if (c_dialect_objc ())
6876 int parmno;
6878 switch (errtype)
6880 case ic_return:
6881 parmno = 0;
6882 break;
6884 case ic_assign:
6885 parmno = -1;
6886 break;
6888 case ic_init:
6889 parmno = -2;
6890 break;
6892 default:
6893 parmno = parmnum;
6894 break;
6897 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6900 if (warn_cxx_compat)
6902 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6903 if (checktype != error_mark_node
6904 && TREE_CODE (type) == ENUMERAL_TYPE
6905 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6906 switch (errtype)
6908 case ic_argpass:
6909 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6910 "passing argument %d of %qE is invalid in C++",
6911 parmnum, rname))
6912 inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6913 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6914 "expected %qT but argument is of type %qT",
6915 type, rhstype);
6916 break;
6917 case ic_assign:
6918 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6919 "%qT in assignment is invalid in C++", rhstype, type);
6920 break;
6921 case ic_init:
6922 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6923 "%qT to %qT in initialization is invalid in C++",
6924 rhstype, type);
6925 break;
6926 case ic_return:
6927 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6928 "%qT in return is invalid in C++", rhstype, type);
6929 break;
6930 default:
6931 gcc_unreachable ();
6935 if (warn_enum_conversion)
6937 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6938 if (checktype != error_mark_node
6939 && TREE_CODE (checktype) == ENUMERAL_TYPE
6940 && TREE_CODE (type) == ENUMERAL_TYPE
6941 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6943 gcc_rich_location loc (location);
6944 warning_at (&loc, OPT_Wenum_conversion,
6945 "implicit conversion from %qT to %qT",
6946 checktype, type);
6950 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6952 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
6953 return rhs;
6956 if (coder == VOID_TYPE)
6958 /* Except for passing an argument to an unprototyped function,
6959 this is a constraint violation. When passing an argument to
6960 an unprototyped function, it is compile-time undefined;
6961 making it a constraint in that case was rejected in
6962 DR#252. */
6963 const char msg[] = "void value not ignored as it ought to be";
6964 if (warnopt)
6965 warning_at (location, warnopt, msg);
6966 else
6967 error_at (location, msg);
6968 return error_mark_node;
6970 rhs = require_complete_type (location, rhs);
6971 if (rhs == error_mark_node)
6972 return error_mark_node;
6974 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6975 return error_mark_node;
6977 /* A non-reference type can convert to a reference. This handles
6978 va_start, va_copy and possibly port built-ins. */
6979 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6981 if (!lvalue_p (rhs))
6983 const char msg[] = "cannot pass rvalue to reference parameter";
6984 if (warnopt)
6985 warning_at (location, warnopt, msg);
6986 else
6987 error_at (location, msg);
6988 return error_mark_node;
6990 if (!c_mark_addressable (rhs))
6991 return error_mark_node;
6992 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6993 SET_EXPR_LOCATION (rhs, location);
6995 rhs = convert_for_assignment (location, expr_loc,
6996 build_pointer_type (TREE_TYPE (type)),
6997 rhs, origtype, errtype,
6998 null_pointer_constant, fundecl, function,
6999 parmnum, warnopt);
7000 if (rhs == error_mark_node)
7001 return error_mark_node;
7003 rhs = build1 (NOP_EXPR, type, rhs);
7004 SET_EXPR_LOCATION (rhs, location);
7005 return rhs;
7007 /* Some types can interconvert without explicit casts. */
7008 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
7009 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
7010 return convert (type, rhs);
7011 /* Arithmetic types all interconvert, and enum is treated like int. */
7012 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
7013 || codel == FIXED_POINT_TYPE
7014 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
7015 || codel == BOOLEAN_TYPE)
7016 && (coder == INTEGER_TYPE || coder == REAL_TYPE
7017 || coder == FIXED_POINT_TYPE
7018 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
7019 || coder == BOOLEAN_TYPE))
7021 if (warnopt && errtype == ic_argpass)
7022 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
7023 rhstype);
7025 bool save = in_late_binary_op;
7026 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
7027 || (coder == REAL_TYPE
7028 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
7029 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
7030 in_late_binary_op = true;
7031 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
7032 ? expr_loc : location, type, orig_rhs);
7033 in_late_binary_op = save;
7034 return ret;
7037 /* Aggregates in different TUs might need conversion. */
7038 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
7039 && codel == coder
7040 && comptypes (type, rhstype))
7041 return convert_and_check (expr_loc != UNKNOWN_LOCATION
7042 ? expr_loc : location, type, rhs);
7044 /* Conversion to a transparent union or record from its member types.
7045 This applies only to function arguments. */
7046 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
7047 && TYPE_TRANSPARENT_AGGR (type))
7048 && errtype == ic_argpass)
7050 tree memb, marginal_memb = NULL_TREE;
7052 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
7054 tree memb_type = TREE_TYPE (memb);
7056 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
7057 TYPE_MAIN_VARIANT (rhstype)))
7058 break;
7060 if (TREE_CODE (memb_type) != POINTER_TYPE)
7061 continue;
7063 if (coder == POINTER_TYPE)
7065 tree ttl = TREE_TYPE (memb_type);
7066 tree ttr = TREE_TYPE (rhstype);
7068 /* Any non-function converts to a [const][volatile] void *
7069 and vice versa; otherwise, targets must be the same.
7070 Meanwhile, the lhs target must have all the qualifiers of
7071 the rhs. */
7072 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7073 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7074 || comp_target_types (location, memb_type, rhstype))
7076 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
7077 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
7078 /* If this type won't generate any warnings, use it. */
7079 if (lquals == rquals
7080 || ((TREE_CODE (ttr) == FUNCTION_TYPE
7081 && TREE_CODE (ttl) == FUNCTION_TYPE)
7082 ? ((lquals | rquals) == rquals)
7083 : ((lquals | rquals) == lquals)))
7084 break;
7086 /* Keep looking for a better type, but remember this one. */
7087 if (!marginal_memb)
7088 marginal_memb = memb;
7092 /* Can convert integer zero to any pointer type. */
7093 if (null_pointer_constant)
7095 rhs = null_pointer_node;
7096 break;
7100 if (memb || marginal_memb)
7102 if (!memb)
7104 /* We have only a marginally acceptable member type;
7105 it needs a warning. */
7106 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
7107 tree ttr = TREE_TYPE (rhstype);
7109 /* Const and volatile mean something different for function
7110 types, so the usual warnings are not appropriate. */
7111 if (TREE_CODE (ttr) == FUNCTION_TYPE
7112 && TREE_CODE (ttl) == FUNCTION_TYPE)
7114 /* Because const and volatile on functions are
7115 restrictions that say the function will not do
7116 certain things, it is okay to use a const or volatile
7117 function where an ordinary one is wanted, but not
7118 vice-versa. */
7119 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7120 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7121 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7122 OPT_Wdiscarded_qualifiers,
7123 G_("passing argument %d of %qE "
7124 "makes %q#v qualified function "
7125 "pointer from unqualified"),
7126 G_("assignment makes %q#v qualified "
7127 "function pointer from "
7128 "unqualified"),
7129 G_("initialization makes %q#v qualified "
7130 "function pointer from "
7131 "unqualified"),
7132 G_("return makes %q#v qualified function "
7133 "pointer from unqualified"),
7134 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7136 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7137 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7138 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7139 OPT_Wdiscarded_qualifiers,
7140 G_("passing argument %d of %qE discards "
7141 "%qv qualifier from pointer target type"),
7142 G_("assignment discards %qv qualifier "
7143 "from pointer target type"),
7144 G_("initialization discards %qv qualifier "
7145 "from pointer target type"),
7146 G_("return discards %qv qualifier from "
7147 "pointer target type"),
7148 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7150 memb = marginal_memb;
7153 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7154 pedwarn (location, OPT_Wpedantic,
7155 "ISO C prohibits argument conversion to union type");
7157 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7158 return build_constructor_single (type, memb, rhs);
7162 /* Conversions among pointers */
7163 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7164 && (coder == codel))
7166 /* If RHS refers to a built-in declared without a prototype
7167 BLTIN is the declaration of the built-in with a prototype
7168 and RHSTYPE is set to the actual type of the built-in. */
7169 tree bltin;
7170 rhstype = type_or_builtin_type (rhs, &bltin);
7172 tree ttl = TREE_TYPE (type);
7173 tree ttr = TREE_TYPE (rhstype);
7174 tree mvl = ttl;
7175 tree mvr = ttr;
7176 bool is_opaque_pointer;
7177 int target_cmp = 0; /* Cache comp_target_types () result. */
7178 addr_space_t asl;
7179 addr_space_t asr;
7181 if (TREE_CODE (mvl) != ARRAY_TYPE)
7182 mvl = (TYPE_ATOMIC (mvl)
7183 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7184 TYPE_QUAL_ATOMIC)
7185 : TYPE_MAIN_VARIANT (mvl));
7186 if (TREE_CODE (mvr) != ARRAY_TYPE)
7187 mvr = (TYPE_ATOMIC (mvr)
7188 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7189 TYPE_QUAL_ATOMIC)
7190 : TYPE_MAIN_VARIANT (mvr));
7191 /* Opaque pointers are treated like void pointers. */
7192 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7194 /* The Plan 9 compiler permits a pointer to a struct to be
7195 automatically converted into a pointer to an anonymous field
7196 within the struct. */
7197 if (flag_plan9_extensions
7198 && RECORD_OR_UNION_TYPE_P (mvl)
7199 && RECORD_OR_UNION_TYPE_P (mvr)
7200 && mvl != mvr)
7202 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7203 if (new_rhs != NULL_TREE)
7205 rhs = new_rhs;
7206 rhstype = TREE_TYPE (rhs);
7207 coder = TREE_CODE (rhstype);
7208 ttr = TREE_TYPE (rhstype);
7209 mvr = TYPE_MAIN_VARIANT (ttr);
7213 /* C++ does not allow the implicit conversion void* -> T*. However,
7214 for the purpose of reducing the number of false positives, we
7215 tolerate the special case of
7217 int *p = NULL;
7219 where NULL is typically defined in C to be '(void *) 0'. */
7220 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7221 warning_at (errtype == ic_argpass ? expr_loc : location,
7222 OPT_Wc___compat,
7223 "request for implicit conversion "
7224 "from %qT to %qT not permitted in C++", rhstype, type);
7226 /* See if the pointers point to incompatible address spaces. */
7227 asl = TYPE_ADDR_SPACE (ttl);
7228 asr = TYPE_ADDR_SPACE (ttr);
7229 if (!null_pointer_constant_p (rhs)
7230 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7232 switch (errtype)
7234 case ic_argpass:
7236 const char msg[] = G_("passing argument %d of %qE from "
7237 "pointer to non-enclosed address space");
7238 if (warnopt)
7239 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7240 else
7241 error_at (expr_loc, msg, parmnum, rname);
7242 break;
7244 case ic_assign:
7246 const char msg[] = G_("assignment from pointer to "
7247 "non-enclosed address space");
7248 if (warnopt)
7249 warning_at (location, warnopt, msg);
7250 else
7251 error_at (location, msg);
7252 break;
7254 case ic_init:
7256 const char msg[] = G_("initialization from pointer to "
7257 "non-enclosed address space");
7258 if (warnopt)
7259 warning_at (location, warnopt, msg);
7260 else
7261 error_at (location, msg);
7262 break;
7264 case ic_return:
7266 const char msg[] = G_("return from pointer to "
7267 "non-enclosed address space");
7268 if (warnopt)
7269 warning_at (location, warnopt, msg);
7270 else
7271 error_at (location, msg);
7272 break;
7274 default:
7275 gcc_unreachable ();
7277 return error_mark_node;
7280 /* Check if the right-hand side has a format attribute but the
7281 left-hand side doesn't. */
7282 if (warn_suggest_attribute_format
7283 && check_missing_format_attribute (type, rhstype))
7285 switch (errtype)
7287 case ic_argpass:
7288 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7289 "argument %d of %qE might be "
7290 "a candidate for a format attribute",
7291 parmnum, rname);
7292 break;
7293 case ic_assign:
7294 warning_at (location, OPT_Wsuggest_attribute_format,
7295 "assignment left-hand side might be "
7296 "a candidate for a format attribute");
7297 break;
7298 case ic_init:
7299 warning_at (location, OPT_Wsuggest_attribute_format,
7300 "initialization left-hand side might be "
7301 "a candidate for a format attribute");
7302 break;
7303 case ic_return:
7304 warning_at (location, OPT_Wsuggest_attribute_format,
7305 "return type might be "
7306 "a candidate for a format attribute");
7307 break;
7308 default:
7309 gcc_unreachable ();
7313 /* See if the pointers point to incompatible scalar storage orders. */
7314 if (warn_scalar_storage_order
7315 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7316 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7318 tree t;
7320 switch (errtype)
7322 case ic_argpass:
7323 /* Do not warn for built-in functions, for example memcpy, since we
7324 control how they behave and they can be useful in this area. */
7325 if (TREE_CODE (rname) != FUNCTION_DECL
7326 || !fndecl_built_in_p (rname))
7327 warning_at (location, OPT_Wscalar_storage_order,
7328 "passing argument %d of %qE from incompatible "
7329 "scalar storage order", parmnum, rname);
7330 break;
7331 case ic_assign:
7332 /* Do not warn if the RHS is a call to a function that returns a
7333 pointer that is not an alias. */
7334 if (TREE_CODE (rhs) != CALL_EXPR
7335 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7336 || !DECL_IS_MALLOC (t))
7337 warning_at (location, OPT_Wscalar_storage_order,
7338 "assignment to %qT from pointer type %qT with "
7339 "incompatible scalar storage order", type, rhstype);
7340 break;
7341 case ic_init:
7342 /* Likewise. */
7343 if (TREE_CODE (rhs) != CALL_EXPR
7344 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7345 || !DECL_IS_MALLOC (t))
7346 warning_at (location, OPT_Wscalar_storage_order,
7347 "initialization of %qT from pointer type %qT with "
7348 "incompatible scalar storage order", type, rhstype);
7349 break;
7350 case ic_return:
7351 warning_at (location, OPT_Wscalar_storage_order,
7352 "returning %qT from pointer type with incompatible "
7353 "scalar storage order %qT", rhstype, type);
7354 break;
7355 default:
7356 gcc_unreachable ();
7360 /* Any non-function converts to a [const][volatile] void *
7361 and vice versa; otherwise, targets must be the same.
7362 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7363 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7364 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7365 || (target_cmp = comp_target_types (location, type, rhstype))
7366 || is_opaque_pointer
7367 || ((c_common_unsigned_type (mvl)
7368 == c_common_unsigned_type (mvr))
7369 && (c_common_signed_type (mvl)
7370 == c_common_signed_type (mvr))
7371 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7373 /* Warn about loss of qualifers from pointers to arrays with
7374 qualifiers on the element type. */
7375 if (TREE_CODE (ttr) == ARRAY_TYPE)
7377 ttr = strip_array_types (ttr);
7378 ttl = strip_array_types (ttl);
7380 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7381 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7382 WARNING_FOR_QUALIFIERS (flag_isoc2x,
7383 location, expr_loc,
7384 OPT_Wdiscarded_array_qualifiers,
7385 G_("passing argument %d of %qE discards "
7386 "%qv qualifier from pointer target type"),
7387 G_("assignment discards %qv qualifier "
7388 "from pointer target type"),
7389 G_("initialization discards %qv qualifier "
7390 "from pointer target type"),
7391 G_("return discards %qv qualifier from "
7392 "pointer target type"),
7393 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7395 else if (pedantic
7396 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7398 (VOID_TYPE_P (ttr)
7399 && !null_pointer_constant
7400 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7401 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7402 G_("ISO C forbids passing argument %d of "
7403 "%qE between function pointer "
7404 "and %<void *%>"),
7405 G_("ISO C forbids assignment between "
7406 "function pointer and %<void *%>"),
7407 G_("ISO C forbids initialization between "
7408 "function pointer and %<void *%>"),
7409 G_("ISO C forbids return between function "
7410 "pointer and %<void *%>"));
7411 /* Const and volatile mean something different for function types,
7412 so the usual warnings are not appropriate. */
7413 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7414 && TREE_CODE (ttl) != FUNCTION_TYPE)
7416 /* Assignments between atomic and non-atomic objects are OK. */
7417 bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7418 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
7419 bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7420 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
7422 /* Don't warn about loss of qualifier for conversions from
7423 qualified void* to pointers to arrays with corresponding
7424 qualifier on the element type (except for pedantic before C23). */
7425 if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc2x))
7426 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7427 OPT_Wdiscarded_qualifiers,
7428 G_("passing argument %d of %qE discards "
7429 "%qv qualifier from pointer target type"),
7430 G_("assignment discards %qv qualifier "
7431 "from pointer target type"),
7432 G_("initialization discards %qv qualifier "
7433 "from pointer target type"),
7434 G_("return discards %qv qualifier from "
7435 "pointer target type"),
7436 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7437 else if (warn_quals_ped)
7438 pedwarn_c11 (location, OPT_Wc11_c2x_compat,
7439 "array with qualifier on the element is not qualified before C2X");
7441 /* If this is not a case of ignoring a mismatch in signedness,
7442 no warning. */
7443 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7444 || target_cmp)
7446 /* If there is a mismatch, do warn. */
7447 else if (warn_pointer_sign)
7448 switch (errtype)
7450 case ic_argpass:
7452 auto_diagnostic_group d;
7453 range_label_for_type_mismatch rhs_label (rhstype, type);
7454 gcc_rich_location richloc (expr_loc, &rhs_label);
7455 if (pedwarn (&richloc, OPT_Wpointer_sign,
7456 "pointer targets in passing argument %d of "
7457 "%qE differ in signedness", parmnum, rname))
7458 inform_for_arg (fundecl, expr_loc, parmnum, type,
7459 rhstype);
7461 break;
7462 case ic_assign:
7463 pedwarn (location, OPT_Wpointer_sign,
7464 "pointer targets in assignment from %qT to %qT "
7465 "differ in signedness", rhstype, type);
7466 break;
7467 case ic_init:
7468 pedwarn_init (location, OPT_Wpointer_sign,
7469 "pointer targets in initialization of %qT "
7470 "from %qT differ in signedness", type,
7471 rhstype);
7472 break;
7473 case ic_return:
7474 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7475 "returning %qT from a function with return type "
7476 "%qT differ in signedness", rhstype, type);
7477 break;
7478 default:
7479 gcc_unreachable ();
7482 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7483 && TREE_CODE (ttr) == FUNCTION_TYPE)
7485 /* Because const and volatile on functions are restrictions
7486 that say the function will not do certain things,
7487 it is okay to use a const or volatile function
7488 where an ordinary one is wanted, but not vice-versa. */
7489 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7490 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7491 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7492 OPT_Wdiscarded_qualifiers,
7493 G_("passing argument %d of %qE makes "
7494 "%q#v qualified function pointer "
7495 "from unqualified"),
7496 G_("assignment makes %q#v qualified function "
7497 "pointer from unqualified"),
7498 G_("initialization makes %q#v qualified "
7499 "function pointer from unqualified"),
7500 G_("return makes %q#v qualified function "
7501 "pointer from unqualified"),
7502 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7505 /* Avoid warning about the volatile ObjC EH puts on decls. */
7506 else if (!objc_ok)
7508 switch (errtype)
7510 case ic_argpass:
7512 auto_diagnostic_group d;
7513 range_label_for_type_mismatch rhs_label (rhstype, type);
7514 gcc_rich_location richloc (expr_loc, &rhs_label);
7515 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7516 "passing argument %d of %qE from incompatible "
7517 "pointer type", parmnum, rname))
7518 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7520 break;
7521 case ic_assign:
7522 if (bltin)
7523 pedwarn (location, OPT_Wincompatible_pointer_types,
7524 "assignment to %qT from pointer to "
7525 "%qD with incompatible type %qT",
7526 type, bltin, rhstype);
7527 else
7528 pedwarn (location, OPT_Wincompatible_pointer_types,
7529 "assignment to %qT from incompatible pointer type %qT",
7530 type, rhstype);
7531 break;
7532 case ic_init:
7533 if (bltin)
7534 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7535 "initialization of %qT from pointer to "
7536 "%qD with incompatible type %qT",
7537 type, bltin, rhstype);
7538 else
7539 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7540 "initialization of %qT from incompatible "
7541 "pointer type %qT",
7542 type, rhstype);
7543 break;
7544 case ic_return:
7545 if (bltin)
7546 pedwarn (location, OPT_Wincompatible_pointer_types,
7547 "returning pointer to %qD of type %qT from "
7548 "a function with incompatible type %qT",
7549 bltin, rhstype, type);
7550 else
7551 pedwarn (location, OPT_Wincompatible_pointer_types,
7552 "returning %qT from a function with incompatible "
7553 "return type %qT", rhstype, type);
7554 break;
7555 default:
7556 gcc_unreachable ();
7560 /* If RHS isn't an address, check pointer or array of packed
7561 struct or union. */
7562 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7564 return convert (type, rhs);
7566 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7568 /* ??? This should not be an error when inlining calls to
7569 unprototyped functions. */
7570 const char msg[] = "invalid use of non-lvalue array";
7571 if (warnopt)
7572 warning_at (location, warnopt, msg);
7573 else
7574 error_at (location, msg);
7575 return error_mark_node;
7577 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7579 /* An explicit constant 0 can convert to a pointer,
7580 or one that results from arithmetic, even including
7581 a cast to integer type. */
7582 if (!null_pointer_constant)
7583 switch (errtype)
7585 case ic_argpass:
7587 auto_diagnostic_group d;
7588 range_label_for_type_mismatch rhs_label (rhstype, type);
7589 gcc_rich_location richloc (expr_loc, &rhs_label);
7590 if (pedwarn (&richloc, OPT_Wint_conversion,
7591 "passing argument %d of %qE makes pointer from "
7592 "integer without a cast", parmnum, rname))
7593 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7595 break;
7596 case ic_assign:
7597 pedwarn (location, OPT_Wint_conversion,
7598 "assignment to %qT from %qT makes pointer from integer "
7599 "without a cast", type, rhstype);
7600 break;
7601 case ic_init:
7602 pedwarn_init (location, OPT_Wint_conversion,
7603 "initialization of %qT from %qT makes pointer from "
7604 "integer without a cast", type, rhstype);
7605 break;
7606 case ic_return:
7607 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7608 "function with return type %qT makes pointer from "
7609 "integer without a cast", rhstype, type);
7610 break;
7611 default:
7612 gcc_unreachable ();
7615 return convert (type, rhs);
7617 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7619 switch (errtype)
7621 case ic_argpass:
7623 auto_diagnostic_group d;
7624 range_label_for_type_mismatch rhs_label (rhstype, type);
7625 gcc_rich_location richloc (expr_loc, &rhs_label);
7626 if (pedwarn (&richloc, OPT_Wint_conversion,
7627 "passing argument %d of %qE makes integer from "
7628 "pointer without a cast", parmnum, rname))
7629 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7631 break;
7632 case ic_assign:
7633 pedwarn (location, OPT_Wint_conversion,
7634 "assignment to %qT from %qT makes integer from pointer "
7635 "without a cast", type, rhstype);
7636 break;
7637 case ic_init:
7638 pedwarn_init (location, OPT_Wint_conversion,
7639 "initialization of %qT from %qT makes integer from "
7640 "pointer without a cast", type, rhstype);
7641 break;
7642 case ic_return:
7643 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7644 "function with return type %qT makes integer from "
7645 "pointer without a cast", rhstype, type);
7646 break;
7647 default:
7648 gcc_unreachable ();
7651 return convert (type, rhs);
7653 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7655 tree ret;
7656 bool save = in_late_binary_op;
7657 in_late_binary_op = true;
7658 ret = convert (type, rhs);
7659 in_late_binary_op = save;
7660 return ret;
7663 switch (errtype)
7665 case ic_argpass:
7667 auto_diagnostic_group d;
7668 range_label_for_type_mismatch rhs_label (rhstype, type);
7669 gcc_rich_location richloc (expr_loc, &rhs_label);
7670 const char msg[] = G_("incompatible type for argument %d of %qE");
7671 if (warnopt)
7672 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7673 else
7674 error_at (&richloc, msg, parmnum, rname);
7675 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7677 break;
7678 case ic_assign:
7680 const char msg[]
7681 = G_("incompatible types when assigning to type %qT from type %qT");
7682 if (warnopt)
7683 warning_at (expr_loc, 0, msg, type, rhstype);
7684 else
7685 error_at (expr_loc, msg, type, rhstype);
7686 break;
7688 case ic_init:
7690 const char msg[]
7691 = G_("incompatible types when initializing type %qT using type %qT");
7692 if (warnopt)
7693 warning_at (location, 0, msg, type, rhstype);
7694 else
7695 error_at (location, msg, type, rhstype);
7696 break;
7698 case ic_return:
7700 const char msg[]
7701 = G_("incompatible types when returning type %qT but %qT was expected");
7702 if (warnopt)
7703 warning_at (location, 0, msg, rhstype, type);
7704 else
7705 error_at (location, msg, rhstype, type);
7706 break;
7708 default:
7709 gcc_unreachable ();
7712 return error_mark_node;
7715 /* If VALUE is a compound expr all of whose expressions are constant, then
7716 return its value. Otherwise, return error_mark_node.
7718 This is for handling COMPOUND_EXPRs as initializer elements
7719 which is allowed with a warning when -pedantic is specified. */
7721 static tree
7722 valid_compound_expr_initializer (tree value, tree endtype)
7724 if (TREE_CODE (value) == COMPOUND_EXPR)
7726 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7727 == error_mark_node)
7728 return error_mark_node;
7729 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7730 endtype);
7732 else if (!initializer_constant_valid_p (value, endtype))
7733 return error_mark_node;
7734 else
7735 return value;
7738 /* Perform appropriate conversions on the initial value of a variable,
7739 store it in the declaration DECL,
7740 and print any error messages that are appropriate.
7741 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7742 If the init is invalid, store an ERROR_MARK.
7744 INIT_LOC is the location of the initial value. */
7746 void
7747 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7749 tree value, type;
7750 bool npc = false;
7752 /* If variable's type was invalidly declared, just ignore it. */
7754 type = TREE_TYPE (decl);
7755 if (TREE_CODE (type) == ERROR_MARK)
7756 return;
7758 /* Digest the specified initializer into an expression. */
7760 if (init)
7761 npc = null_pointer_constant_p (init);
7762 value = digest_init (init_loc, type, init, origtype, npc,
7763 true, TREE_STATIC (decl));
7765 /* Store the expression if valid; else report error. */
7767 if (!in_system_header_at (input_location)
7768 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7769 warning (OPT_Wtraditional, "traditional C rejects automatic "
7770 "aggregate initialization");
7772 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7773 DECL_INITIAL (decl) = value;
7775 /* ANSI wants warnings about out-of-range constant initializers. */
7776 STRIP_TYPE_NOPS (value);
7777 if (TREE_STATIC (decl))
7778 constant_expression_warning (value);
7780 /* Check if we need to set array size from compound literal size. */
7781 if (TREE_CODE (type) == ARRAY_TYPE
7782 && TYPE_DOMAIN (type) == NULL_TREE
7783 && value != error_mark_node)
7785 tree inside_init = init;
7787 STRIP_TYPE_NOPS (inside_init);
7788 inside_init = fold (inside_init);
7790 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7792 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7794 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7796 /* For int foo[] = (int [3]){1}; we need to set array size
7797 now since later on array initializer will be just the
7798 brace enclosed list of the compound literal. */
7799 tree etype = strip_array_types (TREE_TYPE (decl));
7800 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7801 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7802 layout_type (type);
7803 layout_decl (cldecl, 0);
7804 TREE_TYPE (decl)
7805 = c_build_qualified_type (type, TYPE_QUALS (etype));
7811 /* Methods for storing and printing names for error messages. */
7813 /* Implement a spelling stack that allows components of a name to be pushed
7814 and popped. Each element on the stack is this structure. */
7816 struct spelling
7818 int kind;
7819 union
7821 unsigned HOST_WIDE_INT i;
7822 const char *s;
7823 } u;
7826 #define SPELLING_STRING 1
7827 #define SPELLING_MEMBER 2
7828 #define SPELLING_BOUNDS 3
7830 static struct spelling *spelling; /* Next stack element (unused). */
7831 static struct spelling *spelling_base; /* Spelling stack base. */
7832 static int spelling_size; /* Size of the spelling stack. */
7834 /* Macros to save and restore the spelling stack around push_... functions.
7835 Alternative to SAVE_SPELLING_STACK. */
7837 #define SPELLING_DEPTH() (spelling - spelling_base)
7838 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7840 /* Push an element on the spelling stack with type KIND and assign VALUE
7841 to MEMBER. */
7843 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7845 int depth = SPELLING_DEPTH (); \
7847 if (depth >= spelling_size) \
7849 spelling_size += 10; \
7850 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7851 spelling_size); \
7852 RESTORE_SPELLING_DEPTH (depth); \
7855 spelling->kind = (KIND); \
7856 spelling->MEMBER = (VALUE); \
7857 spelling++; \
7860 /* Push STRING on the stack. Printed literally. */
7862 static void
7863 push_string (const char *string)
7865 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7868 /* Push a member name on the stack. Printed as '.' STRING. */
7870 static void
7871 push_member_name (tree decl)
7873 const char *const string
7874 = (DECL_NAME (decl)
7875 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7876 : _("<anonymous>"));
7877 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7880 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7882 static void
7883 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7885 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7888 /* Compute the maximum size in bytes of the printed spelling. */
7890 static int
7891 spelling_length (void)
7893 int size = 0;
7894 struct spelling *p;
7896 for (p = spelling_base; p < spelling; p++)
7898 if (p->kind == SPELLING_BOUNDS)
7899 size += 25;
7900 else
7901 size += strlen (p->u.s) + 1;
7904 return size;
7907 /* Print the spelling to BUFFER and return it. */
7909 static char *
7910 print_spelling (char *buffer)
7912 char *d = buffer;
7913 struct spelling *p;
7915 for (p = spelling_base; p < spelling; p++)
7916 if (p->kind == SPELLING_BOUNDS)
7918 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7919 d += strlen (d);
7921 else
7923 const char *s;
7924 if (p->kind == SPELLING_MEMBER)
7925 *d++ = '.';
7926 for (s = p->u.s; (*d = *s++); d++)
7929 *d++ = '\0';
7930 return buffer;
7933 /* Digest the parser output INIT as an initializer for type TYPE.
7934 Return a C expression of type TYPE to represent the initial value.
7936 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7938 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7940 If INIT is a string constant, STRICT_STRING is true if it is
7941 unparenthesized or we should not warn here for it being parenthesized.
7942 For other types of INIT, STRICT_STRING is not used.
7944 INIT_LOC is the location of the INIT.
7946 REQUIRE_CONSTANT requests an error if non-constant initializers or
7947 elements are seen. */
7949 static tree
7950 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7951 bool null_pointer_constant, bool strict_string,
7952 int require_constant)
7954 enum tree_code code = TREE_CODE (type);
7955 tree inside_init = init;
7956 tree semantic_type = NULL_TREE;
7957 bool maybe_const = true;
7959 if (type == error_mark_node
7960 || !init
7961 || error_operand_p (init))
7962 return error_mark_node;
7964 STRIP_TYPE_NOPS (inside_init);
7966 if (!c_in_omp_for)
7968 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7970 semantic_type = TREE_TYPE (inside_init);
7971 inside_init = TREE_OPERAND (inside_init, 0);
7973 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7976 /* Initialization of an array of chars from a string constant
7977 optionally enclosed in braces. */
7979 if (code == ARRAY_TYPE && inside_init
7980 && TREE_CODE (inside_init) == STRING_CST)
7982 tree typ1
7983 = (TYPE_ATOMIC (TREE_TYPE (type))
7984 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7985 TYPE_QUAL_ATOMIC)
7986 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7987 /* Note that an array could be both an array of character type
7988 and an array of wchar_t if wchar_t is signed char or unsigned
7989 char. */
7990 bool char_array = (typ1 == char_type_node
7991 || typ1 == signed_char_type_node
7992 || typ1 == unsigned_char_type_node);
7993 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7994 bool char16_array = !!comptypes (typ1, char16_type_node);
7995 bool char32_array = !!comptypes (typ1, char32_type_node);
7997 if (char_array || wchar_array || char16_array || char32_array)
7999 struct c_expr expr;
8000 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
8001 bool incompat_string_cst = false;
8002 expr.value = inside_init;
8003 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
8004 expr.original_type = NULL;
8005 maybe_warn_string_init (init_loc, type, expr);
8007 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8008 pedwarn_init (init_loc, OPT_Wpedantic,
8009 "initialization of a flexible array member");
8011 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8012 TYPE_MAIN_VARIANT (type)))
8013 return inside_init;
8015 if (char_array)
8017 if (typ2 != char_type_node)
8018 incompat_string_cst = true;
8020 else if (!comptypes (typ1, typ2))
8021 incompat_string_cst = true;
8023 if (incompat_string_cst)
8025 error_init (init_loc, "cannot initialize array of %qT from "
8026 "a string literal with type array of %qT",
8027 typ1, typ2);
8028 return error_mark_node;
8031 if (TYPE_DOMAIN (type) != NULL_TREE
8032 && TYPE_SIZE (type) != NULL_TREE
8033 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8035 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8036 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
8038 /* Subtract the size of a single (possibly wide) character
8039 because it's ok to ignore the terminating null char
8040 that is counted in the length of the constant. */
8041 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
8042 pedwarn_init (init_loc, 0,
8043 ("initializer-string for array of %qT "
8044 "is too long"), typ1);
8045 else if (warn_cxx_compat
8046 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8047 warning_at (init_loc, OPT_Wc___compat,
8048 ("initializer-string for array of %qT "
8049 "is too long for C++"), typ1);
8050 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8052 unsigned HOST_WIDE_INT size
8053 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8054 const char *p = TREE_STRING_POINTER (inside_init);
8056 inside_init = build_string (size, p);
8060 TREE_TYPE (inside_init) = type;
8061 return inside_init;
8063 else if (INTEGRAL_TYPE_P (typ1))
8065 error_init (init_loc, "array of inappropriate type initialized "
8066 "from string constant");
8067 return error_mark_node;
8071 /* Build a VECTOR_CST from a *constant* vector constructor. If the
8072 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
8073 below and handle as a constructor. */
8074 if (code == VECTOR_TYPE
8075 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
8076 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
8077 && TREE_CONSTANT (inside_init))
8079 if (TREE_CODE (inside_init) == VECTOR_CST
8080 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8081 TYPE_MAIN_VARIANT (type)))
8082 return inside_init;
8084 if (TREE_CODE (inside_init) == CONSTRUCTOR)
8086 unsigned HOST_WIDE_INT ix;
8087 tree value;
8088 bool constant_p = true;
8090 /* Iterate through elements and check if all constructor
8091 elements are *_CSTs. */
8092 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
8093 if (!CONSTANT_CLASS_P (value))
8095 constant_p = false;
8096 break;
8099 if (constant_p)
8100 return build_vector_from_ctor (type,
8101 CONSTRUCTOR_ELTS (inside_init));
8105 if (warn_sequence_point)
8106 verify_sequence_points (inside_init);
8108 /* Any type can be initialized
8109 from an expression of the same type, optionally with braces. */
8111 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
8112 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8113 TYPE_MAIN_VARIANT (type))
8114 || (code == ARRAY_TYPE
8115 && comptypes (TREE_TYPE (inside_init), type))
8116 || (gnu_vector_type_p (type)
8117 && comptypes (TREE_TYPE (inside_init), type))
8118 || (code == POINTER_TYPE
8119 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
8120 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
8121 TREE_TYPE (type)))))
8123 if (code == POINTER_TYPE)
8125 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
8127 if (TREE_CODE (inside_init) == STRING_CST
8128 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8129 inside_init = array_to_pointer_conversion
8130 (init_loc, inside_init);
8131 else
8133 error_init (init_loc, "invalid use of non-lvalue array");
8134 return error_mark_node;
8139 if (code == VECTOR_TYPE)
8140 /* Although the types are compatible, we may require a
8141 conversion. */
8142 inside_init = convert (type, inside_init);
8144 if (require_constant
8145 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8147 /* As an extension, allow initializing objects with static storage
8148 duration with compound literals (which are then treated just as
8149 the brace enclosed list they contain). Also allow this for
8150 vectors, as we can only assign them with compound literals. */
8151 if (flag_isoc99 && code != VECTOR_TYPE)
8152 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
8153 "is not constant");
8154 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8155 inside_init = DECL_INITIAL (decl);
8158 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
8159 && TREE_CODE (inside_init) != CONSTRUCTOR)
8161 error_init (init_loc, "array initialized from non-constant array "
8162 "expression");
8163 return error_mark_node;
8166 /* Compound expressions can only occur here if -Wpedantic or
8167 -pedantic-errors is specified. In the later case, we always want
8168 an error. In the former case, we simply want a warning. */
8169 if (require_constant && pedantic
8170 && TREE_CODE (inside_init) == COMPOUND_EXPR)
8172 inside_init
8173 = valid_compound_expr_initializer (inside_init,
8174 TREE_TYPE (inside_init));
8175 if (inside_init == error_mark_node)
8176 error_init (init_loc, "initializer element is not constant");
8177 else
8178 pedwarn_init (init_loc, OPT_Wpedantic,
8179 "initializer element is not constant");
8180 if (flag_pedantic_errors)
8181 inside_init = error_mark_node;
8183 else if (require_constant
8184 && !initializer_constant_valid_p (inside_init,
8185 TREE_TYPE (inside_init)))
8187 error_init (init_loc, "initializer element is not constant");
8188 inside_init = error_mark_node;
8190 else if (require_constant && !maybe_const)
8191 pedwarn_init (init_loc, OPT_Wpedantic,
8192 "initializer element is not a constant expression");
8194 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8195 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8196 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8197 type, inside_init, origtype,
8198 ic_init, null_pointer_constant,
8199 NULL_TREE, NULL_TREE, 0);
8200 return inside_init;
8203 /* Handle scalar types, including conversions. */
8205 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8206 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8207 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
8209 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8210 && (TREE_CODE (init) == STRING_CST
8211 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8212 inside_init = init = array_to_pointer_conversion (init_loc, init);
8213 if (semantic_type)
8214 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8215 inside_init);
8216 inside_init
8217 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8218 inside_init, origtype, ic_init,
8219 null_pointer_constant, NULL_TREE, NULL_TREE,
8222 /* Check to see if we have already given an error message. */
8223 if (inside_init == error_mark_node)
8225 else if (require_constant && !TREE_CONSTANT (inside_init))
8227 error_init (init_loc, "initializer element is not constant");
8228 inside_init = error_mark_node;
8230 else if (require_constant
8231 && !initializer_constant_valid_p (inside_init,
8232 TREE_TYPE (inside_init)))
8234 error_init (init_loc, "initializer element is not computable at "
8235 "load time");
8236 inside_init = error_mark_node;
8238 else if (require_constant && !maybe_const)
8239 pedwarn_init (init_loc, OPT_Wpedantic,
8240 "initializer element is not a constant expression");
8242 return inside_init;
8245 /* Come here only for records and arrays. */
8247 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8249 error_init (init_loc, "variable-sized object may not be initialized");
8250 return error_mark_node;
8253 error_init (init_loc, "invalid initializer");
8254 return error_mark_node;
8257 /* Handle initializers that use braces. */
8259 /* Type of object we are accumulating a constructor for.
8260 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8261 static tree constructor_type;
8263 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8264 left to fill. */
8265 static tree constructor_fields;
8267 /* For an ARRAY_TYPE, this is the specified index
8268 at which to store the next element we get. */
8269 static tree constructor_index;
8271 /* For an ARRAY_TYPE, this is the maximum index. */
8272 static tree constructor_max_index;
8274 /* For a RECORD_TYPE, this is the first field not yet written out. */
8275 static tree constructor_unfilled_fields;
8277 /* For an ARRAY_TYPE, this is the index of the first element
8278 not yet written out. */
8279 static tree constructor_unfilled_index;
8281 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8282 This is so we can generate gaps between fields, when appropriate. */
8283 static tree constructor_bit_index;
8285 /* If we are saving up the elements rather than allocating them,
8286 this is the list of elements so far (in reverse order,
8287 most recent first). */
8288 static vec<constructor_elt, va_gc> *constructor_elements;
8290 /* 1 if constructor should be incrementally stored into a constructor chain,
8291 0 if all the elements should be kept in AVL tree. */
8292 static int constructor_incremental;
8294 /* 1 if so far this constructor's elements are all compile-time constants. */
8295 static int constructor_constant;
8297 /* 1 if so far this constructor's elements are all valid address constants. */
8298 static int constructor_simple;
8300 /* 1 if this constructor has an element that cannot be part of a
8301 constant expression. */
8302 static int constructor_nonconst;
8304 /* 1 if this constructor is erroneous so far. */
8305 static int constructor_erroneous;
8307 /* 1 if this constructor is the universal zero initializer { 0 }. */
8308 static int constructor_zeroinit;
8310 /* Structure for managing pending initializer elements, organized as an
8311 AVL tree. */
8313 struct init_node
8315 struct init_node *left, *right;
8316 struct init_node *parent;
8317 int balance;
8318 tree purpose;
8319 tree value;
8320 tree origtype;
8323 /* Tree of pending elements at this constructor level.
8324 These are elements encountered out of order
8325 which belong at places we haven't reached yet in actually
8326 writing the output.
8327 Will never hold tree nodes across GC runs. */
8328 static struct init_node *constructor_pending_elts;
8330 /* The SPELLING_DEPTH of this constructor. */
8331 static int constructor_depth;
8333 /* DECL node for which an initializer is being read.
8334 0 means we are reading a constructor expression
8335 such as (struct foo) {...}. */
8336 static tree constructor_decl;
8338 /* Nonzero if this is an initializer for a top-level decl. */
8339 static int constructor_top_level;
8341 /* Nonzero if there were any member designators in this initializer. */
8342 static int constructor_designated;
8344 /* Nesting depth of designator list. */
8345 static int designator_depth;
8347 /* Nonzero if there were diagnosed errors in this designator list. */
8348 static int designator_erroneous;
8351 /* This stack has a level for each implicit or explicit level of
8352 structuring in the initializer, including the outermost one. It
8353 saves the values of most of the variables above. */
8355 struct constructor_range_stack;
8357 struct constructor_stack
8359 struct constructor_stack *next;
8360 tree type;
8361 tree fields;
8362 tree index;
8363 tree max_index;
8364 tree unfilled_index;
8365 tree unfilled_fields;
8366 tree bit_index;
8367 vec<constructor_elt, va_gc> *elements;
8368 struct init_node *pending_elts;
8369 int offset;
8370 int depth;
8371 /* If value nonzero, this value should replace the entire
8372 constructor at this level. */
8373 struct c_expr replacement_value;
8374 struct constructor_range_stack *range_stack;
8375 char constant;
8376 char simple;
8377 char nonconst;
8378 char implicit;
8379 char erroneous;
8380 char outer;
8381 char incremental;
8382 char designated;
8383 int designator_depth;
8386 static struct constructor_stack *constructor_stack;
8388 /* This stack represents designators from some range designator up to
8389 the last designator in the list. */
8391 struct constructor_range_stack
8393 struct constructor_range_stack *next, *prev;
8394 struct constructor_stack *stack;
8395 tree range_start;
8396 tree index;
8397 tree range_end;
8398 tree fields;
8401 static struct constructor_range_stack *constructor_range_stack;
8403 /* This stack records separate initializers that are nested.
8404 Nested initializers can't happen in ANSI C, but GNU C allows them
8405 in cases like { ... (struct foo) { ... } ... }. */
8407 struct initializer_stack
8409 struct initializer_stack *next;
8410 tree decl;
8411 struct constructor_stack *constructor_stack;
8412 struct constructor_range_stack *constructor_range_stack;
8413 vec<constructor_elt, va_gc> *elements;
8414 struct spelling *spelling;
8415 struct spelling *spelling_base;
8416 int spelling_size;
8417 char top_level;
8418 char require_constant_value;
8419 char require_constant_elements;
8420 rich_location *missing_brace_richloc;
8423 static struct initializer_stack *initializer_stack;
8425 /* Prepare to parse and output the initializer for variable DECL. */
8427 void
8428 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8429 rich_location *richloc)
8431 const char *locus;
8432 struct initializer_stack *p = XNEW (struct initializer_stack);
8434 p->decl = constructor_decl;
8435 p->require_constant_value = require_constant_value;
8436 p->require_constant_elements = require_constant_elements;
8437 p->constructor_stack = constructor_stack;
8438 p->constructor_range_stack = constructor_range_stack;
8439 p->elements = constructor_elements;
8440 p->spelling = spelling;
8441 p->spelling_base = spelling_base;
8442 p->spelling_size = spelling_size;
8443 p->top_level = constructor_top_level;
8444 p->next = initializer_stack;
8445 p->missing_brace_richloc = richloc;
8446 initializer_stack = p;
8448 constructor_decl = decl;
8449 constructor_designated = 0;
8450 constructor_top_level = top_level;
8452 if (decl != NULL_TREE && decl != error_mark_node)
8454 require_constant_value = TREE_STATIC (decl);
8455 require_constant_elements
8456 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8457 /* For a scalar, you can always use any value to initialize,
8458 even within braces. */
8459 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8460 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8462 else
8464 require_constant_value = 0;
8465 require_constant_elements = 0;
8466 locus = _("(anonymous)");
8469 constructor_stack = 0;
8470 constructor_range_stack = 0;
8472 found_missing_braces = 0;
8474 spelling_base = 0;
8475 spelling_size = 0;
8476 RESTORE_SPELLING_DEPTH (0);
8478 if (locus)
8479 push_string (locus);
8482 void
8483 finish_init (void)
8485 struct initializer_stack *p = initializer_stack;
8487 /* Free the whole constructor stack of this initializer. */
8488 while (constructor_stack)
8490 struct constructor_stack *q = constructor_stack;
8491 constructor_stack = q->next;
8492 XDELETE (q);
8495 gcc_assert (!constructor_range_stack);
8497 /* Pop back to the data of the outer initializer (if any). */
8498 XDELETE (spelling_base);
8500 constructor_decl = p->decl;
8501 require_constant_value = p->require_constant_value;
8502 require_constant_elements = p->require_constant_elements;
8503 constructor_stack = p->constructor_stack;
8504 constructor_range_stack = p->constructor_range_stack;
8505 constructor_elements = p->elements;
8506 spelling = p->spelling;
8507 spelling_base = p->spelling_base;
8508 spelling_size = p->spelling_size;
8509 constructor_top_level = p->top_level;
8510 initializer_stack = p->next;
8511 XDELETE (p);
8514 /* Call here when we see the initializer is surrounded by braces.
8515 This is instead of a call to push_init_level;
8516 it is matched by a call to pop_init_level.
8518 TYPE is the type to initialize, for a constructor expression.
8519 For an initializer for a decl, TYPE is zero. */
8521 void
8522 really_start_incremental_init (tree type)
8524 struct constructor_stack *p = XNEW (struct constructor_stack);
8526 if (type == NULL_TREE)
8527 type = TREE_TYPE (constructor_decl);
8529 if (VECTOR_TYPE_P (type)
8530 && TYPE_VECTOR_OPAQUE (type))
8531 error ("opaque vector types cannot be initialized");
8533 p->type = constructor_type;
8534 p->fields = constructor_fields;
8535 p->index = constructor_index;
8536 p->max_index = constructor_max_index;
8537 p->unfilled_index = constructor_unfilled_index;
8538 p->unfilled_fields = constructor_unfilled_fields;
8539 p->bit_index = constructor_bit_index;
8540 p->elements = constructor_elements;
8541 p->constant = constructor_constant;
8542 p->simple = constructor_simple;
8543 p->nonconst = constructor_nonconst;
8544 p->erroneous = constructor_erroneous;
8545 p->pending_elts = constructor_pending_elts;
8546 p->depth = constructor_depth;
8547 p->replacement_value.value = 0;
8548 p->replacement_value.original_code = ERROR_MARK;
8549 p->replacement_value.original_type = NULL;
8550 p->implicit = 0;
8551 p->range_stack = 0;
8552 p->outer = 0;
8553 p->incremental = constructor_incremental;
8554 p->designated = constructor_designated;
8555 p->designator_depth = designator_depth;
8556 p->next = 0;
8557 constructor_stack = p;
8559 constructor_constant = 1;
8560 constructor_simple = 1;
8561 constructor_nonconst = 0;
8562 constructor_depth = SPELLING_DEPTH ();
8563 constructor_elements = NULL;
8564 constructor_pending_elts = 0;
8565 constructor_type = type;
8566 constructor_incremental = 1;
8567 constructor_designated = 0;
8568 constructor_zeroinit = 1;
8569 designator_depth = 0;
8570 designator_erroneous = 0;
8572 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8574 constructor_fields = TYPE_FIELDS (constructor_type);
8575 /* Skip any nameless bit fields at the beginning. */
8576 while (constructor_fields != NULL_TREE
8577 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8578 constructor_fields = DECL_CHAIN (constructor_fields);
8580 constructor_unfilled_fields = constructor_fields;
8581 constructor_bit_index = bitsize_zero_node;
8583 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8585 if (TYPE_DOMAIN (constructor_type))
8587 constructor_max_index
8588 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8590 /* Detect non-empty initializations of zero-length arrays. */
8591 if (constructor_max_index == NULL_TREE
8592 && TYPE_SIZE (constructor_type))
8593 constructor_max_index = integer_minus_one_node;
8595 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8596 to initialize VLAs will cause a proper error; avoid tree
8597 checking errors as well by setting a safe value. */
8598 if (constructor_max_index
8599 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8600 constructor_max_index = integer_minus_one_node;
8602 constructor_index
8603 = convert (bitsizetype,
8604 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8606 else
8608 constructor_index = bitsize_zero_node;
8609 constructor_max_index = NULL_TREE;
8612 constructor_unfilled_index = constructor_index;
8614 else if (gnu_vector_type_p (constructor_type))
8616 /* Vectors are like simple fixed-size arrays. */
8617 constructor_max_index =
8618 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8619 constructor_index = bitsize_zero_node;
8620 constructor_unfilled_index = constructor_index;
8622 else
8624 /* Handle the case of int x = {5}; */
8625 constructor_fields = constructor_type;
8626 constructor_unfilled_fields = constructor_type;
8630 extern location_t last_init_list_comma;
8632 /* Called when we see an open brace for a nested initializer. Finish
8633 off any pending levels with implicit braces. */
8634 void
8635 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8637 while (constructor_stack->implicit)
8639 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8640 && constructor_fields == NULL_TREE)
8641 process_init_element (input_location,
8642 pop_init_level (loc, 1, braced_init_obstack,
8643 last_init_list_comma),
8644 true, braced_init_obstack);
8645 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8646 && constructor_max_index
8647 && tree_int_cst_lt (constructor_max_index,
8648 constructor_index))
8649 process_init_element (input_location,
8650 pop_init_level (loc, 1, braced_init_obstack,
8651 last_init_list_comma),
8652 true, braced_init_obstack);
8653 else
8654 break;
8658 /* Push down into a subobject, for initialization.
8659 If this is for an explicit set of braces, IMPLICIT is 0.
8660 If it is because the next element belongs at a lower level,
8661 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8663 void
8664 push_init_level (location_t loc, int implicit,
8665 struct obstack *braced_init_obstack)
8667 struct constructor_stack *p;
8668 tree value = NULL_TREE;
8670 /* Unless this is an explicit brace, we need to preserve previous
8671 content if any. */
8672 if (implicit)
8674 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8675 value = find_init_member (constructor_fields, braced_init_obstack);
8676 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8677 value = find_init_member (constructor_index, braced_init_obstack);
8680 p = XNEW (struct constructor_stack);
8681 p->type = constructor_type;
8682 p->fields = constructor_fields;
8683 p->index = constructor_index;
8684 p->max_index = constructor_max_index;
8685 p->unfilled_index = constructor_unfilled_index;
8686 p->unfilled_fields = constructor_unfilled_fields;
8687 p->bit_index = constructor_bit_index;
8688 p->elements = constructor_elements;
8689 p->constant = constructor_constant;
8690 p->simple = constructor_simple;
8691 p->nonconst = constructor_nonconst;
8692 p->erroneous = constructor_erroneous;
8693 p->pending_elts = constructor_pending_elts;
8694 p->depth = constructor_depth;
8695 p->replacement_value.value = NULL_TREE;
8696 p->replacement_value.original_code = ERROR_MARK;
8697 p->replacement_value.original_type = NULL;
8698 p->implicit = implicit;
8699 p->outer = 0;
8700 p->incremental = constructor_incremental;
8701 p->designated = constructor_designated;
8702 p->designator_depth = designator_depth;
8703 p->next = constructor_stack;
8704 p->range_stack = 0;
8705 constructor_stack = p;
8707 constructor_constant = 1;
8708 constructor_simple = 1;
8709 constructor_nonconst = 0;
8710 constructor_depth = SPELLING_DEPTH ();
8711 constructor_elements = NULL;
8712 constructor_incremental = 1;
8713 constructor_designated = 0;
8714 constructor_pending_elts = 0;
8715 if (!implicit)
8717 p->range_stack = constructor_range_stack;
8718 constructor_range_stack = 0;
8719 designator_depth = 0;
8720 designator_erroneous = 0;
8723 /* Don't die if an entire brace-pair level is superfluous
8724 in the containing level. */
8725 if (constructor_type == NULL_TREE)
8727 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8729 /* Don't die if there are extra init elts at the end. */
8730 if (constructor_fields == NULL_TREE)
8731 constructor_type = NULL_TREE;
8732 else
8734 constructor_type = TREE_TYPE (constructor_fields);
8735 push_member_name (constructor_fields);
8736 constructor_depth++;
8738 /* If upper initializer is designated, then mark this as
8739 designated too to prevent bogus warnings. */
8740 constructor_designated = p->designated;
8742 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8744 constructor_type = TREE_TYPE (constructor_type);
8745 push_array_bounds (tree_to_uhwi (constructor_index));
8746 constructor_depth++;
8749 if (constructor_type == NULL_TREE)
8751 error_init (loc, "extra brace group at end of initializer");
8752 constructor_fields = NULL_TREE;
8753 constructor_unfilled_fields = NULL_TREE;
8754 return;
8757 if (value && TREE_CODE (value) == CONSTRUCTOR)
8759 constructor_constant = TREE_CONSTANT (value);
8760 constructor_simple = TREE_STATIC (value);
8761 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8762 constructor_elements = CONSTRUCTOR_ELTS (value);
8763 if (!vec_safe_is_empty (constructor_elements)
8764 && (TREE_CODE (constructor_type) == RECORD_TYPE
8765 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8766 set_nonincremental_init (braced_init_obstack);
8769 if (implicit == 1)
8771 found_missing_braces = 1;
8772 if (initializer_stack->missing_brace_richloc)
8773 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8774 (loc, "{");
8777 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8779 constructor_fields = TYPE_FIELDS (constructor_type);
8780 /* Skip any nameless bit fields at the beginning. */
8781 while (constructor_fields != NULL_TREE
8782 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8783 constructor_fields = DECL_CHAIN (constructor_fields);
8785 constructor_unfilled_fields = constructor_fields;
8786 constructor_bit_index = bitsize_zero_node;
8788 else if (gnu_vector_type_p (constructor_type))
8790 /* Vectors are like simple fixed-size arrays. */
8791 constructor_max_index =
8792 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8793 constructor_index = bitsize_int (0);
8794 constructor_unfilled_index = constructor_index;
8796 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8798 if (TYPE_DOMAIN (constructor_type))
8800 constructor_max_index
8801 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8803 /* Detect non-empty initializations of zero-length arrays. */
8804 if (constructor_max_index == NULL_TREE
8805 && TYPE_SIZE (constructor_type))
8806 constructor_max_index = integer_minus_one_node;
8808 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8809 to initialize VLAs will cause a proper error; avoid tree
8810 checking errors as well by setting a safe value. */
8811 if (constructor_max_index
8812 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8813 constructor_max_index = integer_minus_one_node;
8815 constructor_index
8816 = convert (bitsizetype,
8817 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8819 else
8820 constructor_index = bitsize_zero_node;
8822 constructor_unfilled_index = constructor_index;
8823 if (value && TREE_CODE (value) == STRING_CST)
8825 /* We need to split the char/wchar array into individual
8826 characters, so that we don't have to special case it
8827 everywhere. */
8828 set_nonincremental_init_from_string (value, braced_init_obstack);
8831 else
8833 if (constructor_type != error_mark_node)
8834 warning_init (input_location, 0, "braces around scalar initializer");
8835 constructor_fields = constructor_type;
8836 constructor_unfilled_fields = constructor_type;
8840 /* At the end of an implicit or explicit brace level,
8841 finish up that level of constructor. If a single expression
8842 with redundant braces initialized that level, return the
8843 c_expr structure for that expression. Otherwise, the original_code
8844 element is set to ERROR_MARK.
8845 If we were outputting the elements as they are read, return 0 as the value
8846 from inner levels (process_init_element ignores that),
8847 but return error_mark_node as the value from the outermost level
8848 (that's what we want to put in DECL_INITIAL).
8849 Otherwise, return a CONSTRUCTOR expression as the value. */
8851 struct c_expr
8852 pop_init_level (location_t loc, int implicit,
8853 struct obstack *braced_init_obstack,
8854 location_t insert_before)
8856 struct constructor_stack *p;
8857 struct c_expr ret;
8858 ret.value = NULL_TREE;
8859 ret.original_code = ERROR_MARK;
8860 ret.original_type = NULL;
8862 if (implicit == 0)
8864 /* When we come to an explicit close brace,
8865 pop any inner levels that didn't have explicit braces. */
8866 while (constructor_stack->implicit)
8867 process_init_element (input_location,
8868 pop_init_level (loc, 1, braced_init_obstack,
8869 insert_before),
8870 true, braced_init_obstack);
8871 gcc_assert (!constructor_range_stack);
8873 else
8874 if (initializer_stack->missing_brace_richloc)
8875 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8876 (insert_before, "}");
8878 /* Now output all pending elements. */
8879 constructor_incremental = 1;
8880 output_pending_init_elements (1, braced_init_obstack);
8882 p = constructor_stack;
8884 /* Error for initializing a flexible array member, or a zero-length
8885 array member in an inappropriate context. */
8886 if (constructor_type && constructor_fields
8887 && TREE_CODE (constructor_type) == ARRAY_TYPE
8888 && TYPE_DOMAIN (constructor_type)
8889 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8891 /* Silently discard empty initializations. The parser will
8892 already have pedwarned for empty brackets. */
8893 if (integer_zerop (constructor_unfilled_index))
8894 constructor_type = NULL_TREE;
8895 else
8897 gcc_assert (!TYPE_SIZE (constructor_type));
8899 if (constructor_depth > 2)
8900 error_init (loc, "initialization of flexible array member in a nested context");
8901 else
8902 pedwarn_init (loc, OPT_Wpedantic,
8903 "initialization of a flexible array member");
8905 /* We have already issued an error message for the existence
8906 of a flexible array member not at the end of the structure.
8907 Discard the initializer so that we do not die later. */
8908 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8909 constructor_type = NULL_TREE;
8913 switch (vec_safe_length (constructor_elements))
8915 case 0:
8916 /* Initialization with { } counts as zeroinit. */
8917 constructor_zeroinit = 1;
8918 break;
8919 case 1:
8920 /* This might be zeroinit as well. */
8921 if (integer_zerop ((*constructor_elements)[0].value))
8922 constructor_zeroinit = 1;
8923 break;
8924 default:
8925 /* If the constructor has more than one element, it can't be { 0 }. */
8926 constructor_zeroinit = 0;
8927 break;
8930 /* Warn when some structs are initialized with direct aggregation. */
8931 if (!implicit && found_missing_braces && warn_missing_braces
8932 && !constructor_zeroinit)
8934 gcc_assert (initializer_stack->missing_brace_richloc);
8935 warning_at (initializer_stack->missing_brace_richloc,
8936 OPT_Wmissing_braces,
8937 "missing braces around initializer");
8940 /* Warn when some struct elements are implicitly initialized to zero. */
8941 if (warn_missing_field_initializers
8942 && constructor_type
8943 && TREE_CODE (constructor_type) == RECORD_TYPE
8944 && constructor_unfilled_fields)
8946 /* Do not warn for flexible array members or zero-length arrays. */
8947 while (constructor_unfilled_fields
8948 && (!DECL_SIZE (constructor_unfilled_fields)
8949 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8950 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8952 if (constructor_unfilled_fields
8953 /* Do not warn if this level of the initializer uses member
8954 designators; it is likely to be deliberate. */
8955 && !constructor_designated
8956 /* Do not warn about initializing with { 0 } or with { }. */
8957 && !constructor_zeroinit)
8959 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8960 "missing initializer for field %qD of %qT",
8961 constructor_unfilled_fields,
8962 constructor_type))
8963 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8964 "%qD declared here", constructor_unfilled_fields);
8968 /* Pad out the end of the structure. */
8969 if (p->replacement_value.value)
8970 /* If this closes a superfluous brace pair,
8971 just pass out the element between them. */
8972 ret = p->replacement_value;
8973 else if (constructor_type == NULL_TREE)
8975 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8976 && TREE_CODE (constructor_type) != ARRAY_TYPE
8977 && !gnu_vector_type_p (constructor_type))
8979 /* A nonincremental scalar initializer--just return
8980 the element, after verifying there is just one. */
8981 if (vec_safe_is_empty (constructor_elements))
8983 if (!constructor_erroneous && constructor_type != error_mark_node)
8984 error_init (loc, "empty scalar initializer");
8985 ret.value = error_mark_node;
8987 else if (vec_safe_length (constructor_elements) != 1)
8989 error_init (loc, "extra elements in scalar initializer");
8990 ret.value = (*constructor_elements)[0].value;
8992 else
8993 ret.value = (*constructor_elements)[0].value;
8995 else
8997 if (constructor_erroneous)
8998 ret.value = error_mark_node;
8999 else
9001 ret.value = build_constructor (constructor_type,
9002 constructor_elements);
9003 if (constructor_constant)
9004 TREE_CONSTANT (ret.value) = 1;
9005 if (constructor_constant && constructor_simple)
9006 TREE_STATIC (ret.value) = 1;
9007 if (constructor_nonconst)
9008 CONSTRUCTOR_NON_CONST (ret.value) = 1;
9012 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
9014 if (constructor_nonconst)
9015 ret.original_code = C_MAYBE_CONST_EXPR;
9016 else if (ret.original_code == C_MAYBE_CONST_EXPR)
9017 ret.original_code = ERROR_MARK;
9020 constructor_type = p->type;
9021 constructor_fields = p->fields;
9022 constructor_index = p->index;
9023 constructor_max_index = p->max_index;
9024 constructor_unfilled_index = p->unfilled_index;
9025 constructor_unfilled_fields = p->unfilled_fields;
9026 constructor_bit_index = p->bit_index;
9027 constructor_elements = p->elements;
9028 constructor_constant = p->constant;
9029 constructor_simple = p->simple;
9030 constructor_nonconst = p->nonconst;
9031 constructor_erroneous = p->erroneous;
9032 constructor_incremental = p->incremental;
9033 constructor_designated = p->designated;
9034 designator_depth = p->designator_depth;
9035 constructor_pending_elts = p->pending_elts;
9036 constructor_depth = p->depth;
9037 if (!p->implicit)
9038 constructor_range_stack = p->range_stack;
9039 RESTORE_SPELLING_DEPTH (constructor_depth);
9041 constructor_stack = p->next;
9042 XDELETE (p);
9044 if (ret.value == NULL_TREE && constructor_stack == 0)
9045 ret.value = error_mark_node;
9046 return ret;
9049 /* Common handling for both array range and field name designators.
9050 ARRAY argument is nonzero for array ranges. Returns false for success. */
9052 static bool
9053 set_designator (location_t loc, bool array,
9054 struct obstack *braced_init_obstack)
9056 tree subtype;
9057 enum tree_code subcode;
9059 /* Don't die if an entire brace-pair level is superfluous
9060 in the containing level, or for an erroneous type. */
9061 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9062 return true;
9064 /* If there were errors in this designator list already, bail out
9065 silently. */
9066 if (designator_erroneous)
9067 return true;
9069 /* Likewise for an initializer for a variable-size type. Those are
9070 diagnosed in digest_init. */
9071 if (COMPLETE_TYPE_P (constructor_type)
9072 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9073 return true;
9075 if (!designator_depth)
9077 gcc_assert (!constructor_range_stack);
9079 /* Designator list starts at the level of closest explicit
9080 braces. */
9081 while (constructor_stack->implicit)
9082 process_init_element (input_location,
9083 pop_init_level (loc, 1, braced_init_obstack,
9084 last_init_list_comma),
9085 true, braced_init_obstack);
9086 constructor_designated = 1;
9087 return false;
9090 switch (TREE_CODE (constructor_type))
9092 case RECORD_TYPE:
9093 case UNION_TYPE:
9094 subtype = TREE_TYPE (constructor_fields);
9095 if (subtype != error_mark_node)
9096 subtype = TYPE_MAIN_VARIANT (subtype);
9097 break;
9098 case ARRAY_TYPE:
9099 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9100 break;
9101 default:
9102 gcc_unreachable ();
9105 subcode = TREE_CODE (subtype);
9106 if (array && subcode != ARRAY_TYPE)
9108 error_init (loc, "array index in non-array initializer");
9109 return true;
9111 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9113 error_init (loc, "field name not in record or union initializer");
9114 return true;
9117 constructor_designated = 1;
9118 finish_implicit_inits (loc, braced_init_obstack);
9119 push_init_level (loc, 2, braced_init_obstack);
9120 return false;
9123 /* If there are range designators in designator list, push a new designator
9124 to constructor_range_stack. RANGE_END is end of such stack range or
9125 NULL_TREE if there is no range designator at this level. */
9127 static void
9128 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9130 struct constructor_range_stack *p;
9132 p = (struct constructor_range_stack *)
9133 obstack_alloc (braced_init_obstack,
9134 sizeof (struct constructor_range_stack));
9135 p->prev = constructor_range_stack;
9136 p->next = 0;
9137 p->fields = constructor_fields;
9138 p->range_start = constructor_index;
9139 p->index = constructor_index;
9140 p->stack = constructor_stack;
9141 p->range_end = range_end;
9142 if (constructor_range_stack)
9143 constructor_range_stack->next = p;
9144 constructor_range_stack = p;
9147 /* Within an array initializer, specify the next index to be initialized.
9148 FIRST is that index. If LAST is nonzero, then initialize a range
9149 of indices, running from FIRST through LAST. */
9151 void
9152 set_init_index (location_t loc, tree first, tree last,
9153 struct obstack *braced_init_obstack)
9155 if (set_designator (loc, true, braced_init_obstack))
9156 return;
9158 designator_erroneous = 1;
9160 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9161 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9163 error_init (loc, "array index in initializer not of integer type");
9164 return;
9167 if (TREE_CODE (first) != INTEGER_CST)
9169 first = c_fully_fold (first, false, NULL);
9170 if (TREE_CODE (first) == INTEGER_CST)
9171 pedwarn_init (loc, OPT_Wpedantic,
9172 "array index in initializer is not "
9173 "an integer constant expression");
9176 if (last && TREE_CODE (last) != INTEGER_CST)
9178 last = c_fully_fold (last, false, NULL);
9179 if (TREE_CODE (last) == INTEGER_CST)
9180 pedwarn_init (loc, OPT_Wpedantic,
9181 "array index in initializer is not "
9182 "an integer constant expression");
9185 if (TREE_CODE (first) != INTEGER_CST)
9186 error_init (loc, "nonconstant array index in initializer");
9187 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9188 error_init (loc, "nonconstant array index in initializer");
9189 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9190 error_init (loc, "array index in non-array initializer");
9191 else if (tree_int_cst_sgn (first) == -1)
9192 error_init (loc, "array index in initializer exceeds array bounds");
9193 else if (constructor_max_index
9194 && tree_int_cst_lt (constructor_max_index, first))
9195 error_init (loc, "array index in initializer exceeds array bounds");
9196 else
9198 constant_expression_warning (first);
9199 if (last)
9200 constant_expression_warning (last);
9201 constructor_index = convert (bitsizetype, first);
9202 if (tree_int_cst_lt (constructor_index, first))
9204 constructor_index = copy_node (constructor_index);
9205 TREE_OVERFLOW (constructor_index) = 1;
9208 if (last)
9210 if (tree_int_cst_equal (first, last))
9211 last = NULL_TREE;
9212 else if (tree_int_cst_lt (last, first))
9214 error_init (loc, "empty index range in initializer");
9215 last = NULL_TREE;
9217 else
9219 last = convert (bitsizetype, last);
9220 if (constructor_max_index != NULL_TREE
9221 && tree_int_cst_lt (constructor_max_index, last))
9223 error_init (loc, "array index range in initializer exceeds "
9224 "array bounds");
9225 last = NULL_TREE;
9230 designator_depth++;
9231 designator_erroneous = 0;
9232 if (constructor_range_stack || last)
9233 push_range_stack (last, braced_init_obstack);
9237 /* Within a struct initializer, specify the next field to be initialized. */
9239 void
9240 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9241 struct obstack *braced_init_obstack)
9243 tree field;
9245 if (set_designator (loc, false, braced_init_obstack))
9246 return;
9248 designator_erroneous = 1;
9250 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9252 error_init (loc, "field name not in record or union initializer");
9253 return;
9256 field = lookup_field (constructor_type, fieldname);
9258 if (field == NULL_TREE)
9260 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9261 if (guessed_id)
9263 gcc_rich_location rich_loc (fieldname_loc);
9264 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9265 error_at (&rich_loc,
9266 "%qT has no member named %qE; did you mean %qE?",
9267 constructor_type, fieldname, guessed_id);
9269 else
9270 error_at (fieldname_loc, "%qT has no member named %qE",
9271 constructor_type, fieldname);
9273 else
9276 constructor_fields = TREE_VALUE (field);
9277 designator_depth++;
9278 designator_erroneous = 0;
9279 if (constructor_range_stack)
9280 push_range_stack (NULL_TREE, braced_init_obstack);
9281 field = TREE_CHAIN (field);
9282 if (field)
9284 if (set_designator (loc, false, braced_init_obstack))
9285 return;
9288 while (field != NULL_TREE);
9291 /* Add a new initializer to the tree of pending initializers. PURPOSE
9292 identifies the initializer, either array index or field in a structure.
9293 VALUE is the value of that index or field. If ORIGTYPE is not
9294 NULL_TREE, it is the original type of VALUE.
9296 IMPLICIT is true if value comes from pop_init_level (1),
9297 the new initializer has been merged with the existing one
9298 and thus no warnings should be emitted about overriding an
9299 existing initializer. */
9301 static void
9302 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9303 bool implicit, struct obstack *braced_init_obstack)
9305 struct init_node *p, **q, *r;
9307 q = &constructor_pending_elts;
9308 p = 0;
9310 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9312 while (*q != 0)
9314 p = *q;
9315 if (tree_int_cst_lt (purpose, p->purpose))
9316 q = &p->left;
9317 else if (tree_int_cst_lt (p->purpose, purpose))
9318 q = &p->right;
9319 else
9321 if (!implicit)
9323 if (TREE_SIDE_EFFECTS (p->value))
9324 warning_init (loc, OPT_Woverride_init_side_effects,
9325 "initialized field with side-effects "
9326 "overwritten");
9327 else if (warn_override_init)
9328 warning_init (loc, OPT_Woverride_init,
9329 "initialized field overwritten");
9331 p->value = value;
9332 p->origtype = origtype;
9333 return;
9337 else
9339 tree bitpos;
9341 bitpos = bit_position (purpose);
9342 while (*q != NULL)
9344 p = *q;
9345 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9346 q = &p->left;
9347 else if (p->purpose != purpose)
9348 q = &p->right;
9349 else
9351 if (!implicit)
9353 if (TREE_SIDE_EFFECTS (p->value))
9354 warning_init (loc, OPT_Woverride_init_side_effects,
9355 "initialized field with side-effects "
9356 "overwritten");
9357 else if (warn_override_init)
9358 warning_init (loc, OPT_Woverride_init,
9359 "initialized field overwritten");
9361 p->value = value;
9362 p->origtype = origtype;
9363 return;
9368 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9369 sizeof (struct init_node));
9370 r->purpose = purpose;
9371 r->value = value;
9372 r->origtype = origtype;
9374 *q = r;
9375 r->parent = p;
9376 r->left = 0;
9377 r->right = 0;
9378 r->balance = 0;
9380 while (p)
9382 struct init_node *s;
9384 if (r == p->left)
9386 if (p->balance == 0)
9387 p->balance = -1;
9388 else if (p->balance < 0)
9390 if (r->balance < 0)
9392 /* L rotation. */
9393 p->left = r->right;
9394 if (p->left)
9395 p->left->parent = p;
9396 r->right = p;
9398 p->balance = 0;
9399 r->balance = 0;
9401 s = p->parent;
9402 p->parent = r;
9403 r->parent = s;
9404 if (s)
9406 if (s->left == p)
9407 s->left = r;
9408 else
9409 s->right = r;
9411 else
9412 constructor_pending_elts = r;
9414 else
9416 /* LR rotation. */
9417 struct init_node *t = r->right;
9419 r->right = t->left;
9420 if (r->right)
9421 r->right->parent = r;
9422 t->left = r;
9424 p->left = t->right;
9425 if (p->left)
9426 p->left->parent = p;
9427 t->right = p;
9429 p->balance = t->balance < 0;
9430 r->balance = -(t->balance > 0);
9431 t->balance = 0;
9433 s = p->parent;
9434 p->parent = t;
9435 r->parent = t;
9436 t->parent = s;
9437 if (s)
9439 if (s->left == p)
9440 s->left = t;
9441 else
9442 s->right = t;
9444 else
9445 constructor_pending_elts = t;
9447 break;
9449 else
9451 /* p->balance == +1; growth of left side balances the node. */
9452 p->balance = 0;
9453 break;
9456 else /* r == p->right */
9458 if (p->balance == 0)
9459 /* Growth propagation from right side. */
9460 p->balance++;
9461 else if (p->balance > 0)
9463 if (r->balance > 0)
9465 /* R rotation. */
9466 p->right = r->left;
9467 if (p->right)
9468 p->right->parent = p;
9469 r->left = p;
9471 p->balance = 0;
9472 r->balance = 0;
9474 s = p->parent;
9475 p->parent = r;
9476 r->parent = s;
9477 if (s)
9479 if (s->left == p)
9480 s->left = r;
9481 else
9482 s->right = r;
9484 else
9485 constructor_pending_elts = r;
9487 else /* r->balance == -1 */
9489 /* RL rotation */
9490 struct init_node *t = r->left;
9492 r->left = t->right;
9493 if (r->left)
9494 r->left->parent = r;
9495 t->right = r;
9497 p->right = t->left;
9498 if (p->right)
9499 p->right->parent = p;
9500 t->left = p;
9502 r->balance = (t->balance < 0);
9503 p->balance = -(t->balance > 0);
9504 t->balance = 0;
9506 s = p->parent;
9507 p->parent = t;
9508 r->parent = t;
9509 t->parent = s;
9510 if (s)
9512 if (s->left == p)
9513 s->left = t;
9514 else
9515 s->right = t;
9517 else
9518 constructor_pending_elts = t;
9520 break;
9522 else
9524 /* p->balance == -1; growth of right side balances the node. */
9525 p->balance = 0;
9526 break;
9530 r = p;
9531 p = p->parent;
9535 /* Build AVL tree from a sorted chain. */
9537 static void
9538 set_nonincremental_init (struct obstack * braced_init_obstack)
9540 unsigned HOST_WIDE_INT ix;
9541 tree index, value;
9543 if (TREE_CODE (constructor_type) != RECORD_TYPE
9544 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9545 return;
9547 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9548 add_pending_init (input_location, index, value, NULL_TREE, true,
9549 braced_init_obstack);
9550 constructor_elements = NULL;
9551 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9553 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9554 /* Skip any nameless bit fields at the beginning. */
9555 while (constructor_unfilled_fields != NULL_TREE
9556 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9557 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9560 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9562 if (TYPE_DOMAIN (constructor_type))
9563 constructor_unfilled_index
9564 = convert (bitsizetype,
9565 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9566 else
9567 constructor_unfilled_index = bitsize_zero_node;
9569 constructor_incremental = 0;
9572 /* Build AVL tree from a string constant. */
9574 static void
9575 set_nonincremental_init_from_string (tree str,
9576 struct obstack * braced_init_obstack)
9578 tree value, purpose, type;
9579 HOST_WIDE_INT val[2];
9580 const char *p, *end;
9581 int byte, wchar_bytes, charwidth, bitpos;
9583 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9585 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9586 charwidth = TYPE_PRECISION (char_type_node);
9587 gcc_assert ((size_t) wchar_bytes * charwidth
9588 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9589 type = TREE_TYPE (constructor_type);
9590 p = TREE_STRING_POINTER (str);
9591 end = p + TREE_STRING_LENGTH (str);
9593 for (purpose = bitsize_zero_node;
9594 p < end
9595 && !(constructor_max_index
9596 && tree_int_cst_lt (constructor_max_index, purpose));
9597 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9599 if (wchar_bytes == 1)
9601 val[0] = (unsigned char) *p++;
9602 val[1] = 0;
9604 else
9606 val[1] = 0;
9607 val[0] = 0;
9608 for (byte = 0; byte < wchar_bytes; byte++)
9610 if (BYTES_BIG_ENDIAN)
9611 bitpos = (wchar_bytes - byte - 1) * charwidth;
9612 else
9613 bitpos = byte * charwidth;
9614 val[bitpos / HOST_BITS_PER_WIDE_INT]
9615 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9616 << (bitpos % HOST_BITS_PER_WIDE_INT);
9620 if (!TYPE_UNSIGNED (type))
9622 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9623 if (bitpos < HOST_BITS_PER_WIDE_INT)
9625 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9627 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9628 val[1] = -1;
9631 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9633 if (val[0] < 0)
9634 val[1] = -1;
9636 else if (val[1] & (HOST_WIDE_INT_1
9637 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9638 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9641 value = wide_int_to_tree (type,
9642 wide_int::from_array (val, 2,
9643 HOST_BITS_PER_WIDE_INT * 2));
9644 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9645 braced_init_obstack);
9648 constructor_incremental = 0;
9651 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9652 not initialized yet. */
9654 static tree
9655 find_init_member (tree field, struct obstack * braced_init_obstack)
9657 struct init_node *p;
9659 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9661 if (constructor_incremental
9662 && tree_int_cst_lt (field, constructor_unfilled_index))
9663 set_nonincremental_init (braced_init_obstack);
9665 p = constructor_pending_elts;
9666 while (p)
9668 if (tree_int_cst_lt (field, p->purpose))
9669 p = p->left;
9670 else if (tree_int_cst_lt (p->purpose, field))
9671 p = p->right;
9672 else
9673 return p->value;
9676 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9678 tree bitpos = bit_position (field);
9680 if (constructor_incremental
9681 && (!constructor_unfilled_fields
9682 || tree_int_cst_lt (bitpos,
9683 bit_position (constructor_unfilled_fields))))
9684 set_nonincremental_init (braced_init_obstack);
9686 p = constructor_pending_elts;
9687 while (p)
9689 if (field == p->purpose)
9690 return p->value;
9691 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9692 p = p->left;
9693 else
9694 p = p->right;
9697 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9699 if (!vec_safe_is_empty (constructor_elements)
9700 && (constructor_elements->last ().index == field))
9701 return constructor_elements->last ().value;
9703 return NULL_TREE;
9706 /* "Output" the next constructor element.
9707 At top level, really output it to assembler code now.
9708 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9709 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9710 TYPE is the data type that the containing data type wants here.
9711 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9712 If VALUE is a string constant, STRICT_STRING is true if it is
9713 unparenthesized or we should not warn here for it being parenthesized.
9714 For other types of VALUE, STRICT_STRING is not used.
9716 PENDING if true means output pending elements that belong
9717 right after this element. (PENDING is normally true;
9718 it is false while outputting pending elements, to avoid recursion.)
9720 IMPLICIT is true if value comes from pop_init_level (1),
9721 the new initializer has been merged with the existing one
9722 and thus no warnings should be emitted about overriding an
9723 existing initializer. */
9725 static void
9726 output_init_element (location_t loc, tree value, tree origtype,
9727 bool strict_string, tree type, tree field, bool pending,
9728 bool implicit, struct obstack * braced_init_obstack)
9730 tree semantic_type = NULL_TREE;
9731 bool maybe_const = true;
9732 bool npc;
9734 if (type == error_mark_node || value == error_mark_node)
9736 constructor_erroneous = 1;
9737 return;
9739 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9740 && (TREE_CODE (value) == STRING_CST
9741 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9742 && !(TREE_CODE (value) == STRING_CST
9743 && TREE_CODE (type) == ARRAY_TYPE
9744 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9745 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9746 TYPE_MAIN_VARIANT (type)))
9747 value = array_to_pointer_conversion (input_location, value);
9749 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9750 && require_constant_value && pending)
9752 /* As an extension, allow initializing objects with static storage
9753 duration with compound literals (which are then treated just as
9754 the brace enclosed list they contain). */
9755 if (flag_isoc99)
9756 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9757 "constant");
9758 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9759 value = DECL_INITIAL (decl);
9762 npc = null_pointer_constant_p (value);
9763 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9765 semantic_type = TREE_TYPE (value);
9766 value = TREE_OPERAND (value, 0);
9768 value = c_fully_fold (value, require_constant_value, &maybe_const);
9770 if (value == error_mark_node)
9771 constructor_erroneous = 1;
9772 else if (!TREE_CONSTANT (value))
9773 constructor_constant = 0;
9774 else if (!initializer_constant_valid_p (value,
9775 TREE_TYPE (value),
9776 AGGREGATE_TYPE_P (constructor_type)
9777 && TYPE_REVERSE_STORAGE_ORDER
9778 (constructor_type))
9779 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9780 && DECL_C_BIT_FIELD (field)
9781 && TREE_CODE (value) != INTEGER_CST))
9782 constructor_simple = 0;
9783 if (!maybe_const)
9784 constructor_nonconst = 1;
9786 /* Digest the initializer and issue any errors about incompatible
9787 types before issuing errors about non-constant initializers. */
9788 tree new_value = value;
9789 if (semantic_type)
9790 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9791 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9792 require_constant_value);
9793 if (new_value == error_mark_node)
9795 constructor_erroneous = 1;
9796 return;
9798 if (require_constant_value || require_constant_elements)
9799 constant_expression_warning (new_value);
9801 /* Proceed to check the constness of the original initializer. */
9802 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9804 if (require_constant_value)
9806 error_init (loc, "initializer element is not constant");
9807 value = error_mark_node;
9809 else if (require_constant_elements)
9810 pedwarn (loc, OPT_Wpedantic,
9811 "initializer element is not computable at load time");
9813 else if (!maybe_const
9814 && (require_constant_value || require_constant_elements))
9815 pedwarn_init (loc, OPT_Wpedantic,
9816 "initializer element is not a constant expression");
9818 /* Issue -Wc++-compat warnings about initializing a bitfield with
9819 enum type. */
9820 if (warn_cxx_compat
9821 && field != NULL_TREE
9822 && TREE_CODE (field) == FIELD_DECL
9823 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9824 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9825 != TYPE_MAIN_VARIANT (type))
9826 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9828 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9829 if (checktype != error_mark_node
9830 && (TYPE_MAIN_VARIANT (checktype)
9831 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9832 warning_init (loc, OPT_Wc___compat,
9833 "enum conversion in initialization is invalid in C++");
9836 /* If this field is empty and does not have side effects (and is not at
9837 the end of structure), don't do anything other than checking the
9838 initializer. */
9839 if (field
9840 && (TREE_TYPE (field) == error_mark_node
9841 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9842 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9843 && !TREE_SIDE_EFFECTS (new_value)
9844 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9845 || DECL_CHAIN (field)))))
9846 return;
9848 /* Finally, set VALUE to the initializer value digested above. */
9849 value = new_value;
9851 /* If this element doesn't come next in sequence,
9852 put it on constructor_pending_elts. */
9853 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9854 && (!constructor_incremental
9855 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9857 if (constructor_incremental
9858 && tree_int_cst_lt (field, constructor_unfilled_index))
9859 set_nonincremental_init (braced_init_obstack);
9861 add_pending_init (loc, field, value, origtype, implicit,
9862 braced_init_obstack);
9863 return;
9865 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9866 && (!constructor_incremental
9867 || field != constructor_unfilled_fields))
9869 /* We do this for records but not for unions. In a union,
9870 no matter which field is specified, it can be initialized
9871 right away since it starts at the beginning of the union. */
9872 if (constructor_incremental)
9874 if (!constructor_unfilled_fields)
9875 set_nonincremental_init (braced_init_obstack);
9876 else
9878 tree bitpos, unfillpos;
9880 bitpos = bit_position (field);
9881 unfillpos = bit_position (constructor_unfilled_fields);
9883 if (tree_int_cst_lt (bitpos, unfillpos))
9884 set_nonincremental_init (braced_init_obstack);
9888 add_pending_init (loc, field, value, origtype, implicit,
9889 braced_init_obstack);
9890 return;
9892 else if (TREE_CODE (constructor_type) == UNION_TYPE
9893 && !vec_safe_is_empty (constructor_elements))
9895 if (!implicit)
9897 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9898 warning_init (loc, OPT_Woverride_init_side_effects,
9899 "initialized field with side-effects overwritten");
9900 else if (warn_override_init)
9901 warning_init (loc, OPT_Woverride_init,
9902 "initialized field overwritten");
9905 /* We can have just one union field set. */
9906 constructor_elements = NULL;
9909 /* Otherwise, output this element either to
9910 constructor_elements or to the assembler file. */
9912 constructor_elt celt = {field, value};
9913 vec_safe_push (constructor_elements, celt);
9915 /* Advance the variable that indicates sequential elements output. */
9916 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9917 constructor_unfilled_index
9918 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9919 bitsize_one_node);
9920 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9922 constructor_unfilled_fields
9923 = DECL_CHAIN (constructor_unfilled_fields);
9925 /* Skip any nameless bit fields. */
9926 while (constructor_unfilled_fields != NULL_TREE
9927 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9928 constructor_unfilled_fields =
9929 DECL_CHAIN (constructor_unfilled_fields);
9931 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9932 constructor_unfilled_fields = NULL_TREE;
9934 /* Now output any pending elements which have become next. */
9935 if (pending)
9936 output_pending_init_elements (0, braced_init_obstack);
9939 /* For two FIELD_DECLs in the same chain, return -1 if field1
9940 comes before field2, 1 if field1 comes after field2 and
9941 0 if field1 == field2. */
9943 static int
9944 init_field_decl_cmp (tree field1, tree field2)
9946 if (field1 == field2)
9947 return 0;
9949 tree bitpos1 = bit_position (field1);
9950 tree bitpos2 = bit_position (field2);
9951 if (tree_int_cst_equal (bitpos1, bitpos2))
9953 /* If one of the fields has non-zero bitsize, then that
9954 field must be the last one in a sequence of zero
9955 sized fields, fields after it will have bigger
9956 bit_position. */
9957 if (TREE_TYPE (field1) != error_mark_node
9958 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9959 && integer_nonzerop (TREE_TYPE (field1)))
9960 return 1;
9961 if (TREE_TYPE (field2) != error_mark_node
9962 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9963 && integer_nonzerop (TREE_TYPE (field2)))
9964 return -1;
9965 /* Otherwise, fallback to DECL_CHAIN walk to find out
9966 which field comes earlier. Walk chains of both
9967 fields, so that if field1 and field2 are close to each
9968 other in either order, it is found soon even for large
9969 sequences of zero sized fields. */
9970 tree f1 = field1, f2 = field2;
9971 while (1)
9973 f1 = DECL_CHAIN (f1);
9974 f2 = DECL_CHAIN (f2);
9975 if (f1 == NULL_TREE)
9977 gcc_assert (f2);
9978 return 1;
9980 if (f2 == NULL_TREE)
9981 return -1;
9982 if (f1 == field2)
9983 return -1;
9984 if (f2 == field1)
9985 return 1;
9986 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9987 return 1;
9988 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9989 return -1;
9992 else if (tree_int_cst_lt (bitpos1, bitpos2))
9993 return -1;
9994 else
9995 return 1;
9998 /* Output any pending elements which have become next.
9999 As we output elements, constructor_unfilled_{fields,index}
10000 advances, which may cause other elements to become next;
10001 if so, they too are output.
10003 If ALL is 0, we return when there are
10004 no more pending elements to output now.
10006 If ALL is 1, we output space as necessary so that
10007 we can output all the pending elements. */
10008 static void
10009 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
10011 struct init_node *elt = constructor_pending_elts;
10012 tree next;
10014 retry:
10016 /* Look through the whole pending tree.
10017 If we find an element that should be output now,
10018 output it. Otherwise, set NEXT to the element
10019 that comes first among those still pending. */
10021 next = NULL_TREE;
10022 while (elt)
10024 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10026 if (tree_int_cst_equal (elt->purpose,
10027 constructor_unfilled_index))
10028 output_init_element (input_location, elt->value, elt->origtype,
10029 true, TREE_TYPE (constructor_type),
10030 constructor_unfilled_index, false, false,
10031 braced_init_obstack);
10032 else if (tree_int_cst_lt (constructor_unfilled_index,
10033 elt->purpose))
10035 /* Advance to the next smaller node. */
10036 if (elt->left)
10037 elt = elt->left;
10038 else
10040 /* We have reached the smallest node bigger than the
10041 current unfilled index. Fill the space first. */
10042 next = elt->purpose;
10043 break;
10046 else
10048 /* Advance to the next bigger node. */
10049 if (elt->right)
10050 elt = elt->right;
10051 else
10053 /* We have reached the biggest node in a subtree. Find
10054 the parent of it, which is the next bigger node. */
10055 while (elt->parent && elt->parent->right == elt)
10056 elt = elt->parent;
10057 elt = elt->parent;
10058 if (elt && tree_int_cst_lt (constructor_unfilled_index,
10059 elt->purpose))
10061 next = elt->purpose;
10062 break;
10067 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10069 /* If the current record is complete we are done. */
10070 if (constructor_unfilled_fields == NULL_TREE)
10071 break;
10073 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10074 elt->purpose);
10075 if (cmp == 0)
10076 output_init_element (input_location, elt->value, elt->origtype,
10077 true, TREE_TYPE (elt->purpose),
10078 elt->purpose, false, false,
10079 braced_init_obstack);
10080 else if (cmp < 0)
10082 /* Advance to the next smaller node. */
10083 if (elt->left)
10084 elt = elt->left;
10085 else
10087 /* We have reached the smallest node bigger than the
10088 current unfilled field. Fill the space first. */
10089 next = elt->purpose;
10090 break;
10093 else
10095 /* Advance to the next bigger node. */
10096 if (elt->right)
10097 elt = elt->right;
10098 else
10100 /* We have reached the biggest node in a subtree. Find
10101 the parent of it, which is the next bigger node. */
10102 while (elt->parent && elt->parent->right == elt)
10103 elt = elt->parent;
10104 elt = elt->parent;
10105 if (elt
10106 && init_field_decl_cmp (constructor_unfilled_fields,
10107 elt->purpose) < 0)
10109 next = elt->purpose;
10110 break;
10117 /* Ordinarily return, but not if we want to output all
10118 and there are elements left. */
10119 if (!(all && next != NULL_TREE))
10120 return;
10122 /* If it's not incremental, just skip over the gap, so that after
10123 jumping to retry we will output the next successive element. */
10124 if (RECORD_OR_UNION_TYPE_P (constructor_type))
10125 constructor_unfilled_fields = next;
10126 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10127 constructor_unfilled_index = next;
10129 /* ELT now points to the node in the pending tree with the next
10130 initializer to output. */
10131 goto retry;
10134 /* Expression VALUE coincides with the start of type TYPE in a braced
10135 initializer. Return true if we should treat VALUE as initializing
10136 the first element of TYPE, false if we should treat it as initializing
10137 TYPE as a whole.
10139 If the initializer is clearly invalid, the question becomes:
10140 which choice gives the best error message? */
10142 static bool
10143 initialize_elementwise_p (tree type, tree value)
10145 if (type == error_mark_node || value == error_mark_node)
10146 return false;
10148 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10150 tree value_type = TREE_TYPE (value);
10151 if (value_type == error_mark_node)
10152 return false;
10154 /* GNU vectors can be initialized elementwise. However, treat any
10155 kind of vector value as initializing the vector type as a whole,
10156 regardless of whether the value is a GNU vector. Such initializers
10157 are valid if and only if they would have been valid in a non-braced
10158 initializer like:
10160 TYPE foo = VALUE;
10162 so recursing into the vector type would be at best confusing or at
10163 worst wrong. For example, when -flax-vector-conversions is in effect,
10164 it's possible to initialize a V8HI from a V4SI, even though the vectors
10165 have different element types and different numbers of elements. */
10166 if (gnu_vector_type_p (type))
10167 return !VECTOR_TYPE_P (value_type);
10169 if (AGGREGATE_TYPE_P (type))
10170 return type != TYPE_MAIN_VARIANT (value_type);
10172 return false;
10175 /* Add one non-braced element to the current constructor level.
10176 This adjusts the current position within the constructor's type.
10177 This may also start or terminate implicit levels
10178 to handle a partly-braced initializer.
10180 Once this has found the correct level for the new element,
10181 it calls output_init_element.
10183 IMPLICIT is true if value comes from pop_init_level (1),
10184 the new initializer has been merged with the existing one
10185 and thus no warnings should be emitted about overriding an
10186 existing initializer. */
10188 void
10189 process_init_element (location_t loc, struct c_expr value, bool implicit,
10190 struct obstack * braced_init_obstack)
10192 tree orig_value = value.value;
10193 int string_flag
10194 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10195 bool strict_string = value.original_code == STRING_CST;
10196 bool was_designated = designator_depth != 0;
10198 designator_depth = 0;
10199 designator_erroneous = 0;
10201 if (!implicit && value.value && !integer_zerop (value.value))
10202 constructor_zeroinit = 0;
10204 /* Handle superfluous braces around string cst as in
10205 char x[] = {"foo"}; */
10206 if (string_flag
10207 && constructor_type
10208 && !was_designated
10209 && TREE_CODE (constructor_type) == ARRAY_TYPE
10210 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10211 && integer_zerop (constructor_unfilled_index))
10213 if (constructor_stack->replacement_value.value)
10214 error_init (loc, "excess elements in %<char%> array initializer");
10215 constructor_stack->replacement_value = value;
10216 return;
10219 if (constructor_stack->replacement_value.value != NULL_TREE)
10221 error_init (loc, "excess elements in struct initializer");
10222 return;
10225 /* Ignore elements of a brace group if it is entirely superfluous
10226 and has already been diagnosed, or if the type is erroneous. */
10227 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10228 return;
10230 /* Ignore elements of an initializer for a variable-size type.
10231 Those are diagnosed in digest_init. */
10232 if (COMPLETE_TYPE_P (constructor_type)
10233 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10234 return;
10236 if (!implicit && warn_designated_init && !was_designated
10237 && TREE_CODE (constructor_type) == RECORD_TYPE
10238 && lookup_attribute ("designated_init",
10239 TYPE_ATTRIBUTES (constructor_type)))
10240 warning_init (loc,
10241 OPT_Wdesignated_init,
10242 "positional initialization of field "
10243 "in %<struct%> declared with %<designated_init%> attribute");
10245 /* If we've exhausted any levels that didn't have braces,
10246 pop them now. */
10247 while (constructor_stack->implicit)
10249 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10250 && constructor_fields == NULL_TREE)
10251 process_init_element (loc,
10252 pop_init_level (loc, 1, braced_init_obstack,
10253 last_init_list_comma),
10254 true, braced_init_obstack);
10255 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10256 || gnu_vector_type_p (constructor_type))
10257 && constructor_max_index
10258 && tree_int_cst_lt (constructor_max_index,
10259 constructor_index))
10260 process_init_element (loc,
10261 pop_init_level (loc, 1, braced_init_obstack,
10262 last_init_list_comma),
10263 true, braced_init_obstack);
10264 else
10265 break;
10268 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10269 if (constructor_range_stack)
10271 /* If value is a compound literal and we'll be just using its
10272 content, don't put it into a SAVE_EXPR. */
10273 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10274 || !require_constant_value)
10276 tree semantic_type = NULL_TREE;
10277 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10279 semantic_type = TREE_TYPE (value.value);
10280 value.value = TREE_OPERAND (value.value, 0);
10282 value.value = save_expr (value.value);
10283 if (semantic_type)
10284 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10285 value.value);
10289 while (1)
10291 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10293 tree fieldtype;
10294 enum tree_code fieldcode;
10296 if (constructor_fields == NULL_TREE)
10298 pedwarn_init (loc, 0, "excess elements in struct initializer");
10299 break;
10302 fieldtype = TREE_TYPE (constructor_fields);
10303 if (fieldtype != error_mark_node)
10304 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10305 fieldcode = TREE_CODE (fieldtype);
10307 /* Error for non-static initialization of a flexible array member. */
10308 if (fieldcode == ARRAY_TYPE
10309 && !require_constant_value
10310 && TYPE_SIZE (fieldtype) == NULL_TREE
10311 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10313 error_init (loc, "non-static initialization of a flexible "
10314 "array member");
10315 break;
10318 /* Error for initialization of a flexible array member with
10319 a string constant if the structure is in an array. E.g.:
10320 struct S { int x; char y[]; };
10321 struct S s[] = { { 1, "foo" } };
10322 is invalid. */
10323 if (string_flag
10324 && fieldcode == ARRAY_TYPE
10325 && constructor_depth > 1
10326 && TYPE_SIZE (fieldtype) == NULL_TREE
10327 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10329 bool in_array_p = false;
10330 for (struct constructor_stack *p = constructor_stack;
10331 p && p->type; p = p->next)
10332 if (TREE_CODE (p->type) == ARRAY_TYPE)
10334 in_array_p = true;
10335 break;
10337 if (in_array_p)
10339 error_init (loc, "initialization of flexible array "
10340 "member in a nested context");
10341 break;
10345 /* Accept a string constant to initialize a subarray. */
10346 if (value.value != NULL_TREE
10347 && fieldcode == ARRAY_TYPE
10348 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10349 && string_flag)
10350 value.value = orig_value;
10351 /* Otherwise, if we have come to a subaggregate,
10352 and we don't have an element of its type, push into it. */
10353 else if (value.value != NULL_TREE
10354 && initialize_elementwise_p (fieldtype, value.value))
10356 push_init_level (loc, 1, braced_init_obstack);
10357 continue;
10360 if (value.value)
10362 push_member_name (constructor_fields);
10363 output_init_element (loc, value.value, value.original_type,
10364 strict_string, fieldtype,
10365 constructor_fields, true, implicit,
10366 braced_init_obstack);
10367 RESTORE_SPELLING_DEPTH (constructor_depth);
10369 else
10370 /* Do the bookkeeping for an element that was
10371 directly output as a constructor. */
10373 /* For a record, keep track of end position of last field. */
10374 if (DECL_SIZE (constructor_fields))
10375 constructor_bit_index
10376 = size_binop_loc (input_location, PLUS_EXPR,
10377 bit_position (constructor_fields),
10378 DECL_SIZE (constructor_fields));
10380 /* If the current field was the first one not yet written out,
10381 it isn't now, so update. */
10382 if (constructor_unfilled_fields == constructor_fields)
10384 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10385 /* Skip any nameless bit fields. */
10386 while (constructor_unfilled_fields != 0
10387 && (DECL_UNNAMED_BIT_FIELD
10388 (constructor_unfilled_fields)))
10389 constructor_unfilled_fields =
10390 DECL_CHAIN (constructor_unfilled_fields);
10394 constructor_fields = DECL_CHAIN (constructor_fields);
10395 /* Skip any nameless bit fields at the beginning. */
10396 while (constructor_fields != NULL_TREE
10397 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10398 constructor_fields = DECL_CHAIN (constructor_fields);
10400 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10402 tree fieldtype;
10403 enum tree_code fieldcode;
10405 if (constructor_fields == NULL_TREE)
10407 pedwarn_init (loc, 0,
10408 "excess elements in union initializer");
10409 break;
10412 fieldtype = TREE_TYPE (constructor_fields);
10413 if (fieldtype != error_mark_node)
10414 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10415 fieldcode = TREE_CODE (fieldtype);
10417 /* Warn that traditional C rejects initialization of unions.
10418 We skip the warning if the value is zero. This is done
10419 under the assumption that the zero initializer in user
10420 code appears conditioned on e.g. __STDC__ to avoid
10421 "missing initializer" warnings and relies on default
10422 initialization to zero in the traditional C case.
10423 We also skip the warning if the initializer is designated,
10424 again on the assumption that this must be conditional on
10425 __STDC__ anyway (and we've already complained about the
10426 member-designator already). */
10427 if (!in_system_header_at (input_location) && !constructor_designated
10428 && !(value.value && (integer_zerop (value.value)
10429 || real_zerop (value.value))))
10430 warning (OPT_Wtraditional, "traditional C rejects initialization "
10431 "of unions");
10433 /* Accept a string constant to initialize a subarray. */
10434 if (value.value != NULL_TREE
10435 && fieldcode == ARRAY_TYPE
10436 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10437 && string_flag)
10438 value.value = orig_value;
10439 /* Otherwise, if we have come to a subaggregate,
10440 and we don't have an element of its type, push into it. */
10441 else if (value.value != NULL_TREE
10442 && initialize_elementwise_p (fieldtype, value.value))
10444 push_init_level (loc, 1, braced_init_obstack);
10445 continue;
10448 if (value.value)
10450 push_member_name (constructor_fields);
10451 output_init_element (loc, value.value, value.original_type,
10452 strict_string, fieldtype,
10453 constructor_fields, true, implicit,
10454 braced_init_obstack);
10455 RESTORE_SPELLING_DEPTH (constructor_depth);
10457 else
10458 /* Do the bookkeeping for an element that was
10459 directly output as a constructor. */
10461 constructor_bit_index = DECL_SIZE (constructor_fields);
10462 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10465 constructor_fields = NULL_TREE;
10467 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10469 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10470 enum tree_code eltcode = TREE_CODE (elttype);
10472 /* Accept a string constant to initialize a subarray. */
10473 if (value.value != NULL_TREE
10474 && eltcode == ARRAY_TYPE
10475 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10476 && string_flag)
10477 value.value = orig_value;
10478 /* Otherwise, if we have come to a subaggregate,
10479 and we don't have an element of its type, push into it. */
10480 else if (value.value != NULL_TREE
10481 && initialize_elementwise_p (elttype, value.value))
10483 push_init_level (loc, 1, braced_init_obstack);
10484 continue;
10487 if (constructor_max_index != NULL_TREE
10488 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10489 || integer_all_onesp (constructor_max_index)))
10491 pedwarn_init (loc, 0,
10492 "excess elements in array initializer");
10493 break;
10496 /* Now output the actual element. */
10497 if (value.value)
10499 push_array_bounds (tree_to_uhwi (constructor_index));
10500 output_init_element (loc, value.value, value.original_type,
10501 strict_string, elttype,
10502 constructor_index, true, implicit,
10503 braced_init_obstack);
10504 RESTORE_SPELLING_DEPTH (constructor_depth);
10507 constructor_index
10508 = size_binop_loc (input_location, PLUS_EXPR,
10509 constructor_index, bitsize_one_node);
10511 if (!value.value)
10512 /* If we are doing the bookkeeping for an element that was
10513 directly output as a constructor, we must update
10514 constructor_unfilled_index. */
10515 constructor_unfilled_index = constructor_index;
10517 else if (gnu_vector_type_p (constructor_type))
10519 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10521 /* Do a basic check of initializer size. Note that vectors
10522 always have a fixed size derived from their type. */
10523 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10525 pedwarn_init (loc, 0,
10526 "excess elements in vector initializer");
10527 break;
10530 /* Now output the actual element. */
10531 if (value.value)
10533 if (TREE_CODE (value.value) == VECTOR_CST)
10534 elttype = TYPE_MAIN_VARIANT (constructor_type);
10535 output_init_element (loc, value.value, value.original_type,
10536 strict_string, elttype,
10537 constructor_index, true, implicit,
10538 braced_init_obstack);
10541 constructor_index
10542 = size_binop_loc (input_location,
10543 PLUS_EXPR, constructor_index, bitsize_one_node);
10545 if (!value.value)
10546 /* If we are doing the bookkeeping for an element that was
10547 directly output as a constructor, we must update
10548 constructor_unfilled_index. */
10549 constructor_unfilled_index = constructor_index;
10552 /* Handle the sole element allowed in a braced initializer
10553 for a scalar variable. */
10554 else if (constructor_type != error_mark_node
10555 && constructor_fields == NULL_TREE)
10557 pedwarn_init (loc, 0,
10558 "excess elements in scalar initializer");
10559 break;
10561 else
10563 if (value.value)
10564 output_init_element (loc, value.value, value.original_type,
10565 strict_string, constructor_type,
10566 NULL_TREE, true, implicit,
10567 braced_init_obstack);
10568 constructor_fields = NULL_TREE;
10571 /* Handle range initializers either at this level or anywhere higher
10572 in the designator stack. */
10573 if (constructor_range_stack)
10575 struct constructor_range_stack *p, *range_stack;
10576 int finish = 0;
10578 range_stack = constructor_range_stack;
10579 constructor_range_stack = 0;
10580 while (constructor_stack != range_stack->stack)
10582 gcc_assert (constructor_stack->implicit);
10583 process_init_element (loc,
10584 pop_init_level (loc, 1,
10585 braced_init_obstack,
10586 last_init_list_comma),
10587 true, braced_init_obstack);
10589 for (p = range_stack;
10590 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10591 p = p->prev)
10593 gcc_assert (constructor_stack->implicit);
10594 process_init_element (loc,
10595 pop_init_level (loc, 1,
10596 braced_init_obstack,
10597 last_init_list_comma),
10598 true, braced_init_obstack);
10601 p->index = size_binop_loc (input_location,
10602 PLUS_EXPR, p->index, bitsize_one_node);
10603 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10604 finish = 1;
10606 while (1)
10608 constructor_index = p->index;
10609 constructor_fields = p->fields;
10610 if (finish && p->range_end && p->index == p->range_start)
10612 finish = 0;
10613 p->prev = 0;
10615 p = p->next;
10616 if (!p)
10617 break;
10618 finish_implicit_inits (loc, braced_init_obstack);
10619 push_init_level (loc, 2, braced_init_obstack);
10620 p->stack = constructor_stack;
10621 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10622 p->index = p->range_start;
10625 if (!finish)
10626 constructor_range_stack = range_stack;
10627 continue;
10630 break;
10633 constructor_range_stack = 0;
10636 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10637 (guaranteed to be 'volatile' or null) and ARGS (represented using
10638 an ASM_EXPR node). */
10639 tree
10640 build_asm_stmt (bool is_volatile, tree args)
10642 if (is_volatile)
10643 ASM_VOLATILE_P (args) = 1;
10644 return add_stmt (args);
10647 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10648 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10649 SIMPLE indicates whether there was anything at all after the
10650 string in the asm expression -- asm("blah") and asm("blah" : )
10651 are subtly different. We use a ASM_EXPR node to represent this.
10652 LOC is the location of the asm, and IS_INLINE says whether this
10653 is asm inline. */
10654 tree
10655 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10656 tree clobbers, tree labels, bool simple, bool is_inline)
10658 tree tail;
10659 tree args;
10660 int i;
10661 const char *constraint;
10662 const char **oconstraints;
10663 bool allows_mem, allows_reg, is_inout;
10664 int ninputs, noutputs;
10666 ninputs = list_length (inputs);
10667 noutputs = list_length (outputs);
10668 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10670 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10672 /* Remove output conversions that change the type but not the mode. */
10673 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10675 tree output = TREE_VALUE (tail);
10677 output = c_fully_fold (output, false, NULL, true);
10679 /* ??? Really, this should not be here. Users should be using a
10680 proper lvalue, dammit. But there's a long history of using casts
10681 in the output operands. In cases like longlong.h, this becomes a
10682 primitive form of typechecking -- if the cast can be removed, then
10683 the output operand had a type of the proper width; otherwise we'll
10684 get an error. Gross, but ... */
10685 STRIP_NOPS (output);
10687 if (!lvalue_or_else (loc, output, lv_asm))
10688 output = error_mark_node;
10690 if (output != error_mark_node
10691 && (TREE_READONLY (output)
10692 || TYPE_READONLY (TREE_TYPE (output))
10693 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10694 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10695 readonly_error (loc, output, lv_asm);
10697 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10698 oconstraints[i] = constraint;
10700 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10701 &allows_mem, &allows_reg, &is_inout))
10703 /* If the operand is going to end up in memory,
10704 mark it addressable. */
10705 if (!allows_reg && !c_mark_addressable (output))
10706 output = error_mark_node;
10707 if (!(!allows_reg && allows_mem)
10708 && output != error_mark_node
10709 && VOID_TYPE_P (TREE_TYPE (output)))
10711 error_at (loc, "invalid use of void expression");
10712 output = error_mark_node;
10715 else
10716 output = error_mark_node;
10718 TREE_VALUE (tail) = output;
10721 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10723 tree input;
10725 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10726 input = TREE_VALUE (tail);
10728 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10729 oconstraints, &allows_mem, &allows_reg))
10731 /* If the operand is going to end up in memory,
10732 mark it addressable. */
10733 if (!allows_reg && allows_mem)
10735 input = c_fully_fold (input, false, NULL, true);
10737 /* Strip the nops as we allow this case. FIXME, this really
10738 should be rejected or made deprecated. */
10739 STRIP_NOPS (input);
10740 if (!c_mark_addressable (input))
10741 input = error_mark_node;
10743 else
10745 struct c_expr expr;
10746 memset (&expr, 0, sizeof (expr));
10747 expr.value = input;
10748 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10749 input = c_fully_fold (expr.value, false, NULL);
10751 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10753 error_at (loc, "invalid use of void expression");
10754 input = error_mark_node;
10758 else
10759 input = error_mark_node;
10761 TREE_VALUE (tail) = input;
10764 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10766 /* asm statements without outputs, including simple ones, are treated
10767 as volatile. */
10768 ASM_INPUT_P (args) = simple;
10769 ASM_VOLATILE_P (args) = (noutputs == 0);
10770 ASM_INLINE_P (args) = is_inline;
10772 return args;
10775 /* Generate a goto statement to LABEL. LOC is the location of the
10776 GOTO. */
10778 tree
10779 c_finish_goto_label (location_t loc, tree label)
10781 tree decl = lookup_label_for_goto (loc, label);
10782 if (!decl)
10783 return NULL_TREE;
10784 TREE_USED (decl) = 1;
10786 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10787 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10788 SET_EXPR_LOCATION (t, loc);
10789 return add_stmt (t);
10793 /* Generate a computed goto statement to EXPR. LOC is the location of
10794 the GOTO. */
10796 tree
10797 c_finish_goto_ptr (location_t loc, c_expr val)
10799 tree expr = val.value;
10800 tree t;
10801 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10802 if (expr != error_mark_node
10803 && !POINTER_TYPE_P (TREE_TYPE (expr))
10804 && !null_pointer_constant_p (expr))
10806 error_at (val.get_location (),
10807 "computed goto must be pointer type");
10808 expr = build_zero_cst (ptr_type_node);
10810 expr = c_fully_fold (expr, false, NULL);
10811 expr = convert (ptr_type_node, expr);
10812 t = build1 (GOTO_EXPR, void_type_node, expr);
10813 SET_EXPR_LOCATION (t, loc);
10814 return add_stmt (t);
10817 /* Generate a C `return' statement. RETVAL is the expression for what
10818 to return, or a null pointer for `return;' with no value. LOC is
10819 the location of the return statement, or the location of the expression,
10820 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10821 is the original type of RETVAL. */
10823 tree
10824 c_finish_return (location_t loc, tree retval, tree origtype)
10826 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10827 bool no_warning = false;
10828 bool npc = false;
10830 /* Use the expansion point to handle cases such as returning NULL
10831 in a function returning void. */
10832 location_t xloc = expansion_point_location_if_in_system_header (loc);
10834 if (TREE_THIS_VOLATILE (current_function_decl))
10835 warning_at (xloc, 0,
10836 "function declared %<noreturn%> has a %<return%> statement");
10838 if (retval)
10840 tree semantic_type = NULL_TREE;
10841 npc = null_pointer_constant_p (retval);
10842 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10844 semantic_type = TREE_TYPE (retval);
10845 retval = TREE_OPERAND (retval, 0);
10847 retval = c_fully_fold (retval, false, NULL);
10848 if (semantic_type
10849 && valtype != NULL_TREE
10850 && TREE_CODE (valtype) != VOID_TYPE)
10851 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10854 if (!retval)
10856 current_function_returns_null = 1;
10857 if ((warn_return_type >= 0 || flag_isoc99)
10858 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10860 bool warned_here;
10861 if (flag_isoc99)
10862 warned_here = pedwarn
10863 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10864 "%<return%> with no value, in function returning non-void");
10865 else
10866 warned_here = warning_at
10867 (loc, OPT_Wreturn_type,
10868 "%<return%> with no value, in function returning non-void");
10869 no_warning = true;
10870 if (warned_here)
10871 inform (DECL_SOURCE_LOCATION (current_function_decl),
10872 "declared here");
10875 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10877 current_function_returns_null = 1;
10878 bool warned_here;
10879 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10880 warned_here = pedwarn
10881 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10882 "%<return%> with a value, in function returning void");
10883 else
10884 warned_here = pedwarn
10885 (xloc, OPT_Wpedantic, "ISO C forbids "
10886 "%<return%> with expression, in function returning void");
10887 if (warned_here)
10888 inform (DECL_SOURCE_LOCATION (current_function_decl),
10889 "declared here");
10891 else
10893 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10894 retval, origtype, ic_return,
10895 npc, NULL_TREE, NULL_TREE, 0);
10896 tree res = DECL_RESULT (current_function_decl);
10897 tree inner;
10898 bool save;
10900 current_function_returns_value = 1;
10901 if (t == error_mark_node)
10902 return NULL_TREE;
10904 save = in_late_binary_op;
10905 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10906 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10907 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10908 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10909 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10910 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10911 in_late_binary_op = true;
10912 inner = t = convert (TREE_TYPE (res), t);
10913 in_late_binary_op = save;
10915 /* Strip any conversions, additions, and subtractions, and see if
10916 we are returning the address of a local variable. Warn if so. */
10917 while (1)
10919 switch (TREE_CODE (inner))
10921 CASE_CONVERT:
10922 case NON_LVALUE_EXPR:
10923 case PLUS_EXPR:
10924 case POINTER_PLUS_EXPR:
10925 inner = TREE_OPERAND (inner, 0);
10926 continue;
10928 case MINUS_EXPR:
10929 /* If the second operand of the MINUS_EXPR has a pointer
10930 type (or is converted from it), this may be valid, so
10931 don't give a warning. */
10933 tree op1 = TREE_OPERAND (inner, 1);
10935 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10936 && (CONVERT_EXPR_P (op1)
10937 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10938 op1 = TREE_OPERAND (op1, 0);
10940 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10941 break;
10943 inner = TREE_OPERAND (inner, 0);
10944 continue;
10947 case ADDR_EXPR:
10948 inner = TREE_OPERAND (inner, 0);
10950 while (REFERENCE_CLASS_P (inner)
10951 && !INDIRECT_REF_P (inner))
10952 inner = TREE_OPERAND (inner, 0);
10954 if (DECL_P (inner)
10955 && !DECL_EXTERNAL (inner)
10956 && !TREE_STATIC (inner)
10957 && DECL_CONTEXT (inner) == current_function_decl
10958 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
10960 if (TREE_CODE (inner) == LABEL_DECL)
10961 warning_at (loc, OPT_Wreturn_local_addr,
10962 "function returns address of label");
10963 else
10965 warning_at (loc, OPT_Wreturn_local_addr,
10966 "function returns address of local variable");
10967 tree zero = build_zero_cst (TREE_TYPE (res));
10968 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10971 break;
10973 default:
10974 break;
10977 break;
10980 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10981 SET_EXPR_LOCATION (retval, loc);
10983 if (warn_sequence_point)
10984 verify_sequence_points (retval);
10987 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10988 if (no_warning)
10989 suppress_warning (ret_stmt, OPT_Wreturn_type);
10990 return add_stmt (ret_stmt);
10993 struct c_switch {
10994 /* The SWITCH_STMT being built. */
10995 tree switch_stmt;
10997 /* The original type of the testing expression, i.e. before the
10998 default conversion is applied. */
10999 tree orig_type;
11001 /* A splay-tree mapping the low element of a case range to the high
11002 element, or NULL_TREE if there is no high element. Used to
11003 determine whether or not a new case label duplicates an old case
11004 label. We need a tree, rather than simply a hash table, because
11005 of the GNU case range extension. */
11006 splay_tree cases;
11008 /* The bindings at the point of the switch. This is used for
11009 warnings crossing decls when branching to a case label. */
11010 struct c_spot_bindings *bindings;
11012 /* Whether the switch includes any break statements. */
11013 bool break_stmt_seen_p;
11015 /* The next node on the stack. */
11016 struct c_switch *next;
11018 /* Remember whether the controlling expression had boolean type
11019 before integer promotions for the sake of -Wswitch-bool. */
11020 bool bool_cond_p;
11023 /* A stack of the currently active switch statements. The innermost
11024 switch statement is on the top of the stack. There is no need to
11025 mark the stack for garbage collection because it is only active
11026 during the processing of the body of a function, and we never
11027 collect at that point. */
11029 struct c_switch *c_switch_stack;
11031 /* Start a C switch statement, testing expression EXP. Return the new
11032 SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
11033 SWITCH_COND_LOC is the location of the switch's condition.
11034 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
11036 tree
11037 c_start_switch (location_t switch_loc,
11038 location_t switch_cond_loc,
11039 tree exp, bool explicit_cast_p)
11041 tree orig_type = error_mark_node;
11042 bool bool_cond_p = false;
11043 struct c_switch *cs;
11045 if (exp != error_mark_node)
11047 orig_type = TREE_TYPE (exp);
11049 if (!INTEGRAL_TYPE_P (orig_type))
11051 if (orig_type != error_mark_node)
11053 error_at (switch_cond_loc, "switch quantity not an integer");
11054 orig_type = error_mark_node;
11056 exp = integer_zero_node;
11058 else
11060 tree type = TYPE_MAIN_VARIANT (orig_type);
11061 tree e = exp;
11063 /* Warn if the condition has boolean value. */
11064 while (TREE_CODE (e) == COMPOUND_EXPR)
11065 e = TREE_OPERAND (e, 1);
11067 if ((TREE_CODE (type) == BOOLEAN_TYPE
11068 || truth_value_p (TREE_CODE (e)))
11069 /* Explicit cast to int suppresses this warning. */
11070 && !(TREE_CODE (type) == INTEGER_TYPE
11071 && explicit_cast_p))
11072 bool_cond_p = true;
11074 if (!in_system_header_at (input_location)
11075 && (type == long_integer_type_node
11076 || type == long_unsigned_type_node))
11077 warning_at (switch_cond_loc,
11078 OPT_Wtraditional, "%<long%> switch expression not "
11079 "converted to %<int%> in ISO C");
11081 exp = c_fully_fold (exp, false, NULL);
11082 exp = default_conversion (exp);
11084 if (warn_sequence_point)
11085 verify_sequence_points (exp);
11089 /* Add this new SWITCH_STMT to the stack. */
11090 cs = XNEW (struct c_switch);
11091 cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11092 NULL_TREE, orig_type, NULL_TREE);
11093 cs->orig_type = orig_type;
11094 cs->cases = splay_tree_new (case_compare, NULL, NULL);
11095 cs->bindings = c_get_switch_bindings ();
11096 cs->break_stmt_seen_p = false;
11097 cs->bool_cond_p = bool_cond_p;
11098 cs->next = c_switch_stack;
11099 c_switch_stack = cs;
11101 return add_stmt (cs->switch_stmt);
11104 /* Process a case label at location LOC. */
11106 tree
11107 do_case (location_t loc, tree low_value, tree high_value)
11109 tree label = NULL_TREE;
11111 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11113 low_value = c_fully_fold (low_value, false, NULL);
11114 if (TREE_CODE (low_value) == INTEGER_CST)
11115 pedwarn (loc, OPT_Wpedantic,
11116 "case label is not an integer constant expression");
11119 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11121 high_value = c_fully_fold (high_value, false, NULL);
11122 if (TREE_CODE (high_value) == INTEGER_CST)
11123 pedwarn (input_location, OPT_Wpedantic,
11124 "case label is not an integer constant expression");
11127 if (c_switch_stack == NULL)
11129 if (low_value)
11130 error_at (loc, "case label not within a switch statement");
11131 else
11132 error_at (loc, "%<default%> label not within a switch statement");
11133 return NULL_TREE;
11136 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11137 EXPR_LOCATION (c_switch_stack->switch_stmt),
11138 loc))
11139 return NULL_TREE;
11141 label = c_add_case_label (loc, c_switch_stack->cases,
11142 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11143 low_value, high_value);
11144 if (label == error_mark_node)
11145 label = NULL_TREE;
11146 return label;
11149 /* Finish the switch statement. TYPE is the original type of the
11150 controlling expression of the switch, or NULL_TREE. */
11152 void
11153 c_finish_switch (tree body, tree type)
11155 struct c_switch *cs = c_switch_stack;
11156 location_t switch_location;
11158 SWITCH_STMT_BODY (cs->switch_stmt) = body;
11160 /* Emit warnings as needed. */
11161 switch_location = EXPR_LOCATION (cs->switch_stmt);
11162 c_do_switch_warnings (cs->cases, switch_location,
11163 type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11164 SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11165 if (c_switch_covers_all_cases_p (cs->cases,
11166 SWITCH_STMT_TYPE (cs->switch_stmt)))
11167 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11168 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11170 /* Pop the stack. */
11171 c_switch_stack = cs->next;
11172 splay_tree_delete (cs->cases);
11173 c_release_switch_bindings (cs->bindings);
11174 XDELETE (cs);
11177 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
11178 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11179 may be null. */
11181 void
11182 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11183 tree else_block)
11185 tree stmt;
11187 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11188 SET_EXPR_LOCATION (stmt, if_locus);
11189 add_stmt (stmt);
11192 tree
11193 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11195 /* In switch statements break is sometimes stylistically used after
11196 a return statement. This can lead to spurious warnings about
11197 control reaching the end of a non-void function when it is
11198 inlined. Note that we are calling block_may_fallthru with
11199 language specific tree nodes; this works because
11200 block_may_fallthru returns true when given something it does not
11201 understand. */
11202 bool skip = !block_may_fallthru (cur_stmt_list);
11204 if (is_break)
11205 switch (in_statement)
11207 case 0:
11208 error_at (loc, "break statement not within loop or switch");
11209 return NULL_TREE;
11210 case IN_OMP_BLOCK:
11211 error_at (loc, "invalid exit from OpenMP structured block");
11212 return NULL_TREE;
11213 case IN_OMP_FOR:
11214 error_at (loc, "break statement used with OpenMP for loop");
11215 return NULL_TREE;
11216 case IN_ITERATION_STMT:
11217 case IN_OBJC_FOREACH:
11218 break;
11219 default:
11220 gcc_assert (in_statement & IN_SWITCH_STMT);
11221 c_switch_stack->break_stmt_seen_p = true;
11222 break;
11224 else
11225 switch (in_statement & ~IN_SWITCH_STMT)
11227 case 0:
11228 error_at (loc, "continue statement not within a loop");
11229 return NULL_TREE;
11230 case IN_OMP_BLOCK:
11231 error_at (loc, "invalid exit from OpenMP structured block");
11232 return NULL_TREE;
11233 case IN_ITERATION_STMT:
11234 case IN_OMP_FOR:
11235 case IN_OBJC_FOREACH:
11236 break;
11237 default:
11238 gcc_unreachable ();
11241 if (skip)
11242 return NULL_TREE;
11243 else if (in_statement & IN_OBJC_FOREACH)
11245 /* The foreach expander produces low-level code using gotos instead
11246 of a structured loop construct. */
11247 gcc_assert (label);
11248 return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11250 return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11253 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11255 static void
11256 emit_side_effect_warnings (location_t loc, tree expr)
11258 maybe_warn_nodiscard (loc, expr);
11259 if (!warn_unused_value)
11260 return;
11261 if (expr == error_mark_node)
11263 else if (!TREE_SIDE_EFFECTS (expr))
11265 if (!VOID_TYPE_P (TREE_TYPE (expr))
11266 && !warning_suppressed_p (expr, OPT_Wunused_value))
11267 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11269 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11271 tree r = expr;
11272 location_t cloc = loc;
11273 while (TREE_CODE (r) == COMPOUND_EXPR)
11275 if (EXPR_HAS_LOCATION (r))
11276 cloc = EXPR_LOCATION (r);
11277 r = TREE_OPERAND (r, 1);
11279 if (!TREE_SIDE_EFFECTS (r)
11280 && !VOID_TYPE_P (TREE_TYPE (r))
11281 && !CONVERT_EXPR_P (r)
11282 && !warning_suppressed_p (r, OPT_Wunused_value)
11283 && !warning_suppressed_p (expr, OPT_Wunused_value))
11284 warning_at (cloc, OPT_Wunused_value,
11285 "right-hand operand of comma expression has no effect");
11287 else
11288 warn_if_unused_value (expr, loc);
11291 /* Process an expression as if it were a complete statement. Emit
11292 diagnostics, but do not call ADD_STMT. LOC is the location of the
11293 statement. */
11295 tree
11296 c_process_expr_stmt (location_t loc, tree expr)
11298 tree exprv;
11300 if (!expr)
11301 return NULL_TREE;
11303 expr = c_fully_fold (expr, false, NULL);
11305 if (warn_sequence_point)
11306 verify_sequence_points (expr);
11308 if (TREE_TYPE (expr) != error_mark_node
11309 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11310 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11311 error_at (loc, "expression statement has incomplete type");
11313 /* If we're not processing a statement expression, warn about unused values.
11314 Warnings for statement expressions will be emitted later, once we figure
11315 out which is the result. */
11316 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11317 && (warn_unused_value || warn_unused_result))
11318 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11320 exprv = expr;
11321 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11322 exprv = TREE_OPERAND (exprv, 1);
11323 while (CONVERT_EXPR_P (exprv))
11324 exprv = TREE_OPERAND (exprv, 0);
11325 if (DECL_P (exprv)
11326 || handled_component_p (exprv)
11327 || TREE_CODE (exprv) == ADDR_EXPR)
11328 mark_exp_read (exprv);
11330 /* If the expression is not of a type to which we cannot assign a line
11331 number, wrap the thing in a no-op NOP_EXPR. */
11332 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11334 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11335 SET_EXPR_LOCATION (expr, loc);
11338 return expr;
11341 /* Emit an expression as a statement. LOC is the location of the
11342 expression. */
11344 tree
11345 c_finish_expr_stmt (location_t loc, tree expr)
11347 if (expr)
11348 return add_stmt (c_process_expr_stmt (loc, expr));
11349 else
11350 return NULL;
11353 /* Do the opposite and emit a statement as an expression. To begin,
11354 create a new binding level and return it. */
11356 tree
11357 c_begin_stmt_expr (void)
11359 tree ret;
11361 /* We must force a BLOCK for this level so that, if it is not expanded
11362 later, there is a way to turn off the entire subtree of blocks that
11363 are contained in it. */
11364 keep_next_level ();
11365 ret = c_begin_compound_stmt (true);
11367 c_bindings_start_stmt_expr (c_switch_stack == NULL
11368 ? NULL
11369 : c_switch_stack->bindings);
11371 /* Mark the current statement list as belonging to a statement list. */
11372 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11374 return ret;
11377 /* LOC is the location of the compound statement to which this body
11378 belongs. */
11380 tree
11381 c_finish_stmt_expr (location_t loc, tree body)
11383 tree last, type, tmp, val;
11384 tree *last_p;
11386 body = c_end_compound_stmt (loc, body, true);
11388 c_bindings_end_stmt_expr (c_switch_stack == NULL
11389 ? NULL
11390 : c_switch_stack->bindings);
11392 /* Locate the last statement in BODY. See c_end_compound_stmt
11393 about always returning a BIND_EXPR. */
11394 last_p = &BIND_EXPR_BODY (body);
11395 last = BIND_EXPR_BODY (body);
11397 continue_searching:
11398 if (TREE_CODE (last) == STATEMENT_LIST)
11400 tree_stmt_iterator l = tsi_last (last);
11402 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11403 tsi_prev (&l);
11405 /* This can happen with degenerate cases like ({ }). No value. */
11406 if (tsi_end_p (l))
11407 return body;
11409 /* If we're supposed to generate side effects warnings, process
11410 all of the statements except the last. */
11411 if (warn_unused_value || warn_unused_result)
11413 for (tree_stmt_iterator i = tsi_start (last);
11414 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11416 location_t tloc;
11417 tree t = tsi_stmt (i);
11419 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11420 emit_side_effect_warnings (tloc, t);
11423 last_p = tsi_stmt_ptr (l);
11424 last = *last_p;
11427 /* If the end of the list is exception related, then the list was split
11428 by a call to push_cleanup. Continue searching. */
11429 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11430 || TREE_CODE (last) == TRY_CATCH_EXPR)
11432 last_p = &TREE_OPERAND (last, 0);
11433 last = *last_p;
11434 goto continue_searching;
11437 if (last == error_mark_node)
11438 return last;
11440 /* In the case that the BIND_EXPR is not necessary, return the
11441 expression out from inside it. */
11442 if ((last == BIND_EXPR_BODY (body)
11443 /* Skip nested debug stmts. */
11444 || last == expr_first (BIND_EXPR_BODY (body)))
11445 && BIND_EXPR_VARS (body) == NULL)
11447 /* Even if this looks constant, do not allow it in a constant
11448 expression. */
11449 last = c_wrap_maybe_const (last, true);
11450 /* Do not warn if the return value of a statement expression is
11451 unused. */
11452 suppress_warning (last, OPT_Wunused);
11453 return last;
11456 /* Extract the type of said expression. */
11457 type = TREE_TYPE (last);
11459 /* If we're not returning a value at all, then the BIND_EXPR that
11460 we already have is a fine expression to return. */
11461 if (!type || VOID_TYPE_P (type))
11462 return body;
11464 /* Now that we've located the expression containing the value, it seems
11465 silly to make voidify_wrapper_expr repeat the process. Create a
11466 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11467 tmp = create_tmp_var_raw (type);
11469 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11470 tree_expr_nonnegative_p giving up immediately. */
11471 val = last;
11472 if (TREE_CODE (val) == NOP_EXPR
11473 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11474 val = TREE_OPERAND (val, 0);
11476 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11477 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11480 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11481 SET_EXPR_LOCATION (t, loc);
11482 return t;
11486 /* Begin and end compound statements. This is as simple as pushing
11487 and popping new statement lists from the tree. */
11489 tree
11490 c_begin_compound_stmt (bool do_scope)
11492 tree stmt = push_stmt_list ();
11493 if (do_scope)
11494 push_scope ();
11495 return stmt;
11498 /* End a compound statement. STMT is the statement. LOC is the
11499 location of the compound statement-- this is usually the location
11500 of the opening brace. */
11502 tree
11503 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11505 tree block = NULL;
11507 if (do_scope)
11509 if (c_dialect_objc ())
11510 objc_clear_super_receiver ();
11511 block = pop_scope ();
11514 stmt = pop_stmt_list (stmt);
11515 stmt = c_build_bind_expr (loc, block, stmt);
11517 /* If this compound statement is nested immediately inside a statement
11518 expression, then force a BIND_EXPR to be created. Otherwise we'll
11519 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11520 STATEMENT_LISTs merge, and thus we can lose track of what statement
11521 was really last. */
11522 if (building_stmt_list_p ()
11523 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11524 && TREE_CODE (stmt) != BIND_EXPR)
11526 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11527 TREE_SIDE_EFFECTS (stmt) = 1;
11528 SET_EXPR_LOCATION (stmt, loc);
11531 return stmt;
11534 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11535 when the current scope is exited. EH_ONLY is true when this is not
11536 meant to apply to normal control flow transfer. */
11538 void
11539 push_cleanup (tree decl, tree cleanup, bool eh_only)
11541 enum tree_code code;
11542 tree stmt, list;
11543 bool stmt_expr;
11545 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11546 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11547 add_stmt (stmt);
11548 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11549 list = push_stmt_list ();
11550 TREE_OPERAND (stmt, 0) = list;
11551 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11554 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11555 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11557 static tree
11558 build_vec_cmp (tree_code code, tree type,
11559 tree arg0, tree arg1)
11561 tree zero_vec = build_zero_cst (type);
11562 tree minus_one_vec = build_minus_one_cst (type);
11563 tree cmp_type = truth_type_for (type);
11564 tree cmp = build2 (code, cmp_type, arg0, arg1);
11565 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11568 /* Possibly warn about an address of OP never being NULL in a comparison
11569 operation CODE involving null. */
11571 static void
11572 maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
11574 if (!warn_address || warning_suppressed_p (op, OPT_Waddress))
11575 return;
11577 if (TREE_CODE (op) == NOP_EXPR)
11579 /* Allow casts to intptr_t to suppress the warning. */
11580 tree type = TREE_TYPE (op);
11581 if (TREE_CODE (type) == INTEGER_TYPE)
11582 return;
11583 op = TREE_OPERAND (op, 0);
11586 if (TREE_CODE (op) == POINTER_PLUS_EXPR)
11588 /* Allow a cast to void* to suppress the warning. */
11589 tree type = TREE_TYPE (TREE_TYPE (op));
11590 if (VOID_TYPE_P (type))
11591 return;
11593 /* Adding any value to a null pointer, including zero, is undefined
11594 in C. This includes the expression &p[0] where p is the null
11595 pointer, although &p[0] will have been folded to p by this point
11596 and so not diagnosed. */
11597 if (code == EQ_EXPR)
11598 warning_at (loc, OPT_Waddress,
11599 "the comparison will always evaluate as %<false%> "
11600 "for the pointer operand in %qE must not be NULL",
11601 op);
11602 else
11603 warning_at (loc, OPT_Waddress,
11604 "the comparison will always evaluate as %<true%> "
11605 "for the pointer operand in %qE must not be NULL",
11606 op);
11608 return;
11611 if (TREE_CODE (op) != ADDR_EXPR)
11612 return;
11614 op = TREE_OPERAND (op, 0);
11616 if (TREE_CODE (op) == IMAGPART_EXPR
11617 || TREE_CODE (op) == REALPART_EXPR)
11619 /* The address of either complex part may not be null. */
11620 if (code == EQ_EXPR)
11621 warning_at (loc, OPT_Waddress,
11622 "the comparison will always evaluate as %<false%> "
11623 "for the address of %qE will never be NULL",
11624 op);
11625 else
11626 warning_at (loc, OPT_Waddress,
11627 "the comparison will always evaluate as %<true%> "
11628 "for the address of %qE will never be NULL",
11629 op);
11630 return;
11633 /* Set to true in the loop below if OP dereferences is operand.
11634 In such a case the ultimate target need not be a decl for
11635 the null [in]equality test to be constant. */
11636 bool deref = false;
11638 /* Get the outermost array or object, or member. */
11639 while (handled_component_p (op))
11641 if (TREE_CODE (op) == COMPONENT_REF)
11643 /* Get the member (its address is never null). */
11644 op = TREE_OPERAND (op, 1);
11645 break;
11648 /* Get the outer array/object to refer to in the warning. */
11649 op = TREE_OPERAND (op, 0);
11650 deref = true;
11653 if ((!deref && !decl_with_nonnull_addr_p (op))
11654 || from_macro_expansion_at (loc))
11655 return;
11657 if (code == EQ_EXPR)
11658 warning_at (loc, OPT_Waddress,
11659 "the comparison will always evaluate as %<false%> "
11660 "for the address of %qE will never be NULL",
11661 op);
11662 else
11663 warning_at (loc, OPT_Waddress,
11664 "the comparison will always evaluate as %<true%> "
11665 "for the address of %qE will never be NULL",
11666 op);
11668 if (DECL_P (op))
11669 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
11672 /* Build a binary-operation expression without default conversions.
11673 CODE is the kind of expression to build.
11674 LOCATION is the operator's location.
11675 This function differs from `build' in several ways:
11676 the data type of the result is computed and recorded in it,
11677 warnings are generated if arg data types are invalid,
11678 special handling for addition and subtraction of pointers is known,
11679 and some optimization is done (operations on narrow ints
11680 are done in the narrower type when that gives the same result).
11681 Constant folding is also done before the result is returned.
11683 Note that the operands will never have enumeral types, or function
11684 or array types, because either they will have the default conversions
11685 performed or they have both just been converted to some other type in which
11686 the arithmetic is to be done. */
11688 tree
11689 build_binary_op (location_t location, enum tree_code code,
11690 tree orig_op0, tree orig_op1, bool convert_p)
11692 tree type0, type1, orig_type0, orig_type1;
11693 tree eptype;
11694 enum tree_code code0, code1;
11695 tree op0, op1;
11696 tree ret = error_mark_node;
11697 const char *invalid_op_diag;
11698 bool op0_int_operands, op1_int_operands;
11699 bool int_const, int_const_or_overflow, int_operands;
11701 /* Expression code to give to the expression when it is built.
11702 Normally this is CODE, which is what the caller asked for,
11703 but in some special cases we change it. */
11704 enum tree_code resultcode = code;
11706 /* Data type in which the computation is to be performed.
11707 In the simplest cases this is the common type of the arguments. */
11708 tree result_type = NULL;
11710 /* When the computation is in excess precision, the type of the
11711 final EXCESS_PRECISION_EXPR. */
11712 tree semantic_result_type = NULL;
11714 /* Nonzero means operands have already been type-converted
11715 in whatever way is necessary.
11716 Zero means they need to be converted to RESULT_TYPE. */
11717 int converted = 0;
11719 /* Nonzero means create the expression with this type, rather than
11720 RESULT_TYPE. */
11721 tree build_type = NULL_TREE;
11723 /* Nonzero means after finally constructing the expression
11724 convert it to this type. */
11725 tree final_type = NULL_TREE;
11727 /* Nonzero if this is an operation like MIN or MAX which can
11728 safely be computed in short if both args are promoted shorts.
11729 Also implies COMMON.
11730 -1 indicates a bitwise operation; this makes a difference
11731 in the exact conditions for when it is safe to do the operation
11732 in a narrower mode. */
11733 int shorten = 0;
11735 /* Nonzero if this is a comparison operation;
11736 if both args are promoted shorts, compare the original shorts.
11737 Also implies COMMON. */
11738 int short_compare = 0;
11740 /* Nonzero if this is a right-shift operation, which can be computed on the
11741 original short and then promoted if the operand is a promoted short. */
11742 int short_shift = 0;
11744 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11745 int common = 0;
11747 /* True means types are compatible as far as ObjC is concerned. */
11748 bool objc_ok;
11750 /* True means this is an arithmetic operation that may need excess
11751 precision. */
11752 bool may_need_excess_precision;
11754 /* True means this is a boolean operation that converts both its
11755 operands to truth-values. */
11756 bool boolean_op = false;
11758 /* Remember whether we're doing / or %. */
11759 bool doing_div_or_mod = false;
11761 /* Remember whether we're doing << or >>. */
11762 bool doing_shift = false;
11764 /* Tree holding instrumentation expression. */
11765 tree instrument_expr = NULL;
11767 if (location == UNKNOWN_LOCATION)
11768 location = input_location;
11770 op0 = orig_op0;
11771 op1 = orig_op1;
11773 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11774 if (op0_int_operands)
11775 op0 = remove_c_maybe_const_expr (op0);
11776 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11777 if (op1_int_operands)
11778 op1 = remove_c_maybe_const_expr (op1);
11779 int_operands = (op0_int_operands && op1_int_operands);
11780 if (int_operands)
11782 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11783 && TREE_CODE (orig_op1) == INTEGER_CST);
11784 int_const = (int_const_or_overflow
11785 && !TREE_OVERFLOW (orig_op0)
11786 && !TREE_OVERFLOW (orig_op1));
11788 else
11789 int_const = int_const_or_overflow = false;
11791 /* Do not apply default conversion in mixed vector/scalar expression. */
11792 if (convert_p
11793 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11795 op0 = default_conversion (op0);
11796 op1 = default_conversion (op1);
11799 orig_type0 = type0 = TREE_TYPE (op0);
11801 orig_type1 = type1 = TREE_TYPE (op1);
11803 /* The expression codes of the data types of the arguments tell us
11804 whether the arguments are integers, floating, pointers, etc. */
11805 code0 = TREE_CODE (type0);
11806 code1 = TREE_CODE (type1);
11808 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11809 STRIP_TYPE_NOPS (op0);
11810 STRIP_TYPE_NOPS (op1);
11812 /* If an error was already reported for one of the arguments,
11813 avoid reporting another error. */
11815 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11816 return error_mark_node;
11818 if (code0 == POINTER_TYPE
11819 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11820 return error_mark_node;
11822 if (code1 == POINTER_TYPE
11823 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11824 return error_mark_node;
11826 if ((invalid_op_diag
11827 = targetm.invalid_binary_op (code, type0, type1)))
11829 error_at (location, invalid_op_diag);
11830 return error_mark_node;
11833 switch (code)
11835 case PLUS_EXPR:
11836 case MINUS_EXPR:
11837 case MULT_EXPR:
11838 case TRUNC_DIV_EXPR:
11839 case CEIL_DIV_EXPR:
11840 case FLOOR_DIV_EXPR:
11841 case ROUND_DIV_EXPR:
11842 case EXACT_DIV_EXPR:
11843 may_need_excess_precision = true;
11844 break;
11846 case EQ_EXPR:
11847 case NE_EXPR:
11848 case LE_EXPR:
11849 case GE_EXPR:
11850 case LT_EXPR:
11851 case GT_EXPR:
11852 /* Excess precision for implicit conversions of integers to
11853 floating point in C11 and later. */
11854 may_need_excess_precision = (flag_isoc11
11855 && (ANY_INTEGRAL_TYPE_P (type0)
11856 || ANY_INTEGRAL_TYPE_P (type1)));
11857 break;
11859 default:
11860 may_need_excess_precision = false;
11861 break;
11863 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11865 op0 = TREE_OPERAND (op0, 0);
11866 type0 = TREE_TYPE (op0);
11868 else if (may_need_excess_precision
11869 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11871 type0 = eptype;
11872 op0 = convert (eptype, op0);
11874 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11876 op1 = TREE_OPERAND (op1, 0);
11877 type1 = TREE_TYPE (op1);
11879 else if (may_need_excess_precision
11880 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11882 type1 = eptype;
11883 op1 = convert (eptype, op1);
11886 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11888 /* In case when one of the operands of the binary operation is
11889 a vector and another is a scalar -- convert scalar to vector. */
11890 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
11891 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
11893 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11894 true);
11896 switch (convert_flag)
11898 case stv_error:
11899 return error_mark_node;
11900 case stv_firstarg:
11902 bool maybe_const = true;
11903 tree sc;
11904 sc = c_fully_fold (op0, false, &maybe_const);
11905 sc = save_expr (sc);
11906 sc = convert (TREE_TYPE (type1), sc);
11907 op0 = build_vector_from_val (type1, sc);
11908 if (!maybe_const)
11909 op0 = c_wrap_maybe_const (op0, true);
11910 orig_type0 = type0 = TREE_TYPE (op0);
11911 code0 = TREE_CODE (type0);
11912 converted = 1;
11913 break;
11915 case stv_secondarg:
11917 bool maybe_const = true;
11918 tree sc;
11919 sc = c_fully_fold (op1, false, &maybe_const);
11920 sc = save_expr (sc);
11921 sc = convert (TREE_TYPE (type0), sc);
11922 op1 = build_vector_from_val (type0, sc);
11923 if (!maybe_const)
11924 op1 = c_wrap_maybe_const (op1, true);
11925 orig_type1 = type1 = TREE_TYPE (op1);
11926 code1 = TREE_CODE (type1);
11927 converted = 1;
11928 break;
11930 default:
11931 break;
11935 switch (code)
11937 case PLUS_EXPR:
11938 /* Handle the pointer + int case. */
11939 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11941 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11942 goto return_build_binary_op;
11944 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11946 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11947 goto return_build_binary_op;
11949 else
11950 common = 1;
11951 break;
11953 case MINUS_EXPR:
11954 /* Subtraction of two similar pointers.
11955 We must subtract them as integers, then divide by object size. */
11956 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11957 && comp_target_types (location, type0, type1))
11959 ret = pointer_diff (location, op0, op1, &instrument_expr);
11960 goto return_build_binary_op;
11962 /* Handle pointer minus int. Just like pointer plus int. */
11963 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11965 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11966 goto return_build_binary_op;
11968 else
11969 common = 1;
11970 break;
11972 case MULT_EXPR:
11973 common = 1;
11974 break;
11976 case TRUNC_DIV_EXPR:
11977 case CEIL_DIV_EXPR:
11978 case FLOOR_DIV_EXPR:
11979 case ROUND_DIV_EXPR:
11980 case EXACT_DIV_EXPR:
11981 doing_div_or_mod = true;
11982 warn_for_div_by_zero (location, op1);
11984 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11985 || code0 == FIXED_POINT_TYPE
11986 || code0 == COMPLEX_TYPE
11987 || gnu_vector_type_p (type0))
11988 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11989 || code1 == FIXED_POINT_TYPE
11990 || code1 == COMPLEX_TYPE
11991 || gnu_vector_type_p (type1)))
11993 enum tree_code tcode0 = code0, tcode1 = code1;
11995 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11996 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11997 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11998 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
12000 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
12001 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
12002 resultcode = RDIV_EXPR;
12003 else
12004 /* Although it would be tempting to shorten always here, that
12005 loses on some targets, since the modulo instruction is
12006 undefined if the quotient can't be represented in the
12007 computation mode. We shorten only if unsigned or if
12008 dividing by something we know != -1. */
12009 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
12010 || (TREE_CODE (op1) == INTEGER_CST
12011 && !integer_all_onesp (op1)));
12012 common = 1;
12014 break;
12016 case BIT_AND_EXPR:
12017 case BIT_IOR_EXPR:
12018 case BIT_XOR_EXPR:
12019 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12020 shorten = -1;
12021 /* Allow vector types which are not floating point types. */
12022 else if (gnu_vector_type_p (type0)
12023 && gnu_vector_type_p (type1)
12024 && !VECTOR_FLOAT_TYPE_P (type0)
12025 && !VECTOR_FLOAT_TYPE_P (type1))
12026 common = 1;
12027 break;
12029 case TRUNC_MOD_EXPR:
12030 case FLOOR_MOD_EXPR:
12031 doing_div_or_mod = true;
12032 warn_for_div_by_zero (location, op1);
12034 if (gnu_vector_type_p (type0)
12035 && gnu_vector_type_p (type1)
12036 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12037 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12038 common = 1;
12039 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12041 /* Although it would be tempting to shorten always here, that loses
12042 on some targets, since the modulo instruction is undefined if the
12043 quotient can't be represented in the computation mode. We shorten
12044 only if unsigned or if dividing by something we know != -1. */
12045 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
12046 || (TREE_CODE (op1) == INTEGER_CST
12047 && !integer_all_onesp (op1)));
12048 common = 1;
12050 break;
12052 case TRUTH_ANDIF_EXPR:
12053 case TRUTH_ORIF_EXPR:
12054 case TRUTH_AND_EXPR:
12055 case TRUTH_OR_EXPR:
12056 case TRUTH_XOR_EXPR:
12057 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
12058 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12059 || code0 == FIXED_POINT_TYPE)
12060 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
12061 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12062 || code1 == FIXED_POINT_TYPE))
12064 /* Result of these operations is always an int,
12065 but that does not mean the operands should be
12066 converted to ints! */
12067 result_type = integer_type_node;
12068 if (op0_int_operands)
12070 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
12071 op0 = remove_c_maybe_const_expr (op0);
12073 else
12074 op0 = c_objc_common_truthvalue_conversion (location, op0);
12075 if (op1_int_operands)
12077 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
12078 op1 = remove_c_maybe_const_expr (op1);
12080 else
12081 op1 = c_objc_common_truthvalue_conversion (location, op1);
12082 converted = 1;
12083 boolean_op = true;
12085 if (code == TRUTH_ANDIF_EXPR)
12087 int_const_or_overflow = (int_operands
12088 && TREE_CODE (orig_op0) == INTEGER_CST
12089 && (op0 == truthvalue_false_node
12090 || TREE_CODE (orig_op1) == INTEGER_CST));
12091 int_const = (int_const_or_overflow
12092 && !TREE_OVERFLOW (orig_op0)
12093 && (op0 == truthvalue_false_node
12094 || !TREE_OVERFLOW (orig_op1)));
12096 else if (code == TRUTH_ORIF_EXPR)
12098 int_const_or_overflow = (int_operands
12099 && TREE_CODE (orig_op0) == INTEGER_CST
12100 && (op0 == truthvalue_true_node
12101 || TREE_CODE (orig_op1) == INTEGER_CST));
12102 int_const = (int_const_or_overflow
12103 && !TREE_OVERFLOW (orig_op0)
12104 && (op0 == truthvalue_true_node
12105 || !TREE_OVERFLOW (orig_op1)));
12107 break;
12109 /* Shift operations: result has same type as first operand;
12110 always convert second operand to int.
12111 Also set SHORT_SHIFT if shifting rightward. */
12113 case RSHIFT_EXPR:
12114 if (gnu_vector_type_p (type0)
12115 && gnu_vector_type_p (type1)
12116 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12117 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12118 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12119 TYPE_VECTOR_SUBPARTS (type1)))
12121 result_type = type0;
12122 converted = 1;
12124 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12125 || (gnu_vector_type_p (type0)
12126 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12127 && code1 == INTEGER_TYPE)
12129 doing_shift = true;
12130 if (TREE_CODE (op1) == INTEGER_CST)
12132 if (tree_int_cst_sgn (op1) < 0)
12134 int_const = false;
12135 if (c_inhibit_evaluation_warnings == 0)
12136 warning_at (location, OPT_Wshift_count_negative,
12137 "right shift count is negative");
12139 else if (code0 == VECTOR_TYPE)
12141 if (compare_tree_int (op1,
12142 TYPE_PRECISION (TREE_TYPE (type0)))
12143 >= 0)
12145 int_const = false;
12146 if (c_inhibit_evaluation_warnings == 0)
12147 warning_at (location, OPT_Wshift_count_overflow,
12148 "right shift count >= width of vector element");
12151 else
12153 if (!integer_zerop (op1))
12154 short_shift = 1;
12156 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12158 int_const = false;
12159 if (c_inhibit_evaluation_warnings == 0)
12160 warning_at (location, OPT_Wshift_count_overflow,
12161 "right shift count >= width of type");
12166 /* Use the type of the value to be shifted. */
12167 result_type = type0;
12168 /* Avoid converting op1 to result_type later. */
12169 converted = 1;
12171 break;
12173 case LSHIFT_EXPR:
12174 if (gnu_vector_type_p (type0)
12175 && gnu_vector_type_p (type1)
12176 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12177 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12178 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12179 TYPE_VECTOR_SUBPARTS (type1)))
12181 result_type = type0;
12182 converted = 1;
12184 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12185 || (gnu_vector_type_p (type0)
12186 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12187 && code1 == INTEGER_TYPE)
12189 doing_shift = true;
12190 if (TREE_CODE (op0) == INTEGER_CST
12191 && tree_int_cst_sgn (op0) < 0)
12193 /* Don't reject a left shift of a negative value in a context
12194 where a constant expression is needed in C90. */
12195 if (flag_isoc99)
12196 int_const = false;
12197 if (c_inhibit_evaluation_warnings == 0)
12198 warning_at (location, OPT_Wshift_negative_value,
12199 "left shift of negative value");
12201 if (TREE_CODE (op1) == INTEGER_CST)
12203 if (tree_int_cst_sgn (op1) < 0)
12205 int_const = false;
12206 if (c_inhibit_evaluation_warnings == 0)
12207 warning_at (location, OPT_Wshift_count_negative,
12208 "left shift count is negative");
12210 else if (code0 == VECTOR_TYPE)
12212 if (compare_tree_int (op1,
12213 TYPE_PRECISION (TREE_TYPE (type0)))
12214 >= 0)
12216 int_const = false;
12217 if (c_inhibit_evaluation_warnings == 0)
12218 warning_at (location, OPT_Wshift_count_overflow,
12219 "left shift count >= width of vector element");
12222 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12224 int_const = false;
12225 if (c_inhibit_evaluation_warnings == 0)
12226 warning_at (location, OPT_Wshift_count_overflow,
12227 "left shift count >= width of type");
12229 else if (TREE_CODE (op0) == INTEGER_CST
12230 && maybe_warn_shift_overflow (location, op0, op1)
12231 && flag_isoc99)
12232 int_const = false;
12235 /* Use the type of the value to be shifted. */
12236 result_type = type0;
12237 /* Avoid converting op1 to result_type later. */
12238 converted = 1;
12240 break;
12242 case EQ_EXPR:
12243 case NE_EXPR:
12244 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12246 tree intt;
12247 if (!vector_types_compatible_elements_p (type0, type1))
12249 error_at (location, "comparing vectors with different "
12250 "element types");
12251 return error_mark_node;
12254 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12255 TYPE_VECTOR_SUBPARTS (type1)))
12257 error_at (location, "comparing vectors with different "
12258 "number of elements");
12259 return error_mark_node;
12262 /* It's not precisely specified how the usual arithmetic
12263 conversions apply to the vector types. Here, we use
12264 the unsigned type if one of the operands is signed and
12265 the other one is unsigned. */
12266 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12268 if (!TYPE_UNSIGNED (type0))
12269 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12270 else
12271 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12272 warning_at (location, OPT_Wsign_compare, "comparison between "
12273 "types %qT and %qT", type0, type1);
12276 /* Always construct signed integer vector type. */
12277 intt = c_common_type_for_size (GET_MODE_BITSIZE
12278 (SCALAR_TYPE_MODE
12279 (TREE_TYPE (type0))), 0);
12280 if (!intt)
12282 error_at (location, "could not find an integer type "
12283 "of the same size as %qT",
12284 TREE_TYPE (type0));
12285 return error_mark_node;
12287 result_type = build_opaque_vector_type (intt,
12288 TYPE_VECTOR_SUBPARTS (type0));
12289 converted = 1;
12290 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12291 goto return_build_binary_op;
12293 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12294 warning_at (location,
12295 OPT_Wfloat_equal,
12296 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12297 /* Result of comparison is always int,
12298 but don't convert the args to int! */
12299 build_type = integer_type_node;
12300 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12301 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12302 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12303 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12304 short_compare = 1;
12305 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12307 maybe_warn_for_null_address (location, op0, code);
12308 result_type = type0;
12310 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12312 maybe_warn_for_null_address (location, op1, code);
12313 result_type = type1;
12315 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12317 tree tt0 = TREE_TYPE (type0);
12318 tree tt1 = TREE_TYPE (type1);
12319 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12320 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12321 addr_space_t as_common = ADDR_SPACE_GENERIC;
12323 /* Anything compares with void *. void * compares with anything.
12324 Otherwise, the targets must be compatible
12325 and both must be object or both incomplete. */
12326 if (comp_target_types (location, type0, type1))
12327 result_type = common_pointer_type (type0, type1);
12328 else if (!addr_space_superset (as0, as1, &as_common))
12330 error_at (location, "comparison of pointers to "
12331 "disjoint address spaces");
12332 return error_mark_node;
12334 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12336 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12337 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12338 "comparison of %<void *%> with function pointer");
12340 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12342 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12343 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12344 "comparison of %<void *%> with function pointer");
12346 else
12347 /* Avoid warning about the volatile ObjC EH puts on decls. */
12348 if (!objc_ok)
12349 pedwarn (location, 0,
12350 "comparison of distinct pointer types lacks a cast");
12352 if (result_type == NULL_TREE)
12354 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12355 result_type = build_pointer_type
12356 (build_qualified_type (void_type_node, qual));
12359 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12361 result_type = type0;
12362 pedwarn (location, 0, "comparison between pointer and integer");
12364 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12366 result_type = type1;
12367 pedwarn (location, 0, "comparison between pointer and integer");
12369 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12370 || truth_value_p (TREE_CODE (orig_op0)))
12371 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12372 || truth_value_p (TREE_CODE (orig_op1))))
12373 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12374 break;
12376 case LE_EXPR:
12377 case GE_EXPR:
12378 case LT_EXPR:
12379 case GT_EXPR:
12380 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12382 tree intt;
12383 if (!vector_types_compatible_elements_p (type0, type1))
12385 error_at (location, "comparing vectors with different "
12386 "element types");
12387 return error_mark_node;
12390 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12391 TYPE_VECTOR_SUBPARTS (type1)))
12393 error_at (location, "comparing vectors with different "
12394 "number of elements");
12395 return error_mark_node;
12398 /* It's not precisely specified how the usual arithmetic
12399 conversions apply to the vector types. Here, we use
12400 the unsigned type if one of the operands is signed and
12401 the other one is unsigned. */
12402 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12404 if (!TYPE_UNSIGNED (type0))
12405 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12406 else
12407 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12408 warning_at (location, OPT_Wsign_compare, "comparison between "
12409 "types %qT and %qT", type0, type1);
12412 /* Always construct signed integer vector type. */
12413 intt = c_common_type_for_size (GET_MODE_BITSIZE
12414 (SCALAR_TYPE_MODE
12415 (TREE_TYPE (type0))), 0);
12416 if (!intt)
12418 error_at (location, "could not find an integer type "
12419 "of the same size as %qT",
12420 TREE_TYPE (type0));
12421 return error_mark_node;
12423 result_type = build_opaque_vector_type (intt,
12424 TYPE_VECTOR_SUBPARTS (type0));
12425 converted = 1;
12426 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12427 goto return_build_binary_op;
12429 build_type = integer_type_node;
12430 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12431 || code0 == FIXED_POINT_TYPE)
12432 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12433 || code1 == FIXED_POINT_TYPE))
12434 short_compare = 1;
12435 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12437 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12438 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12439 addr_space_t as_common;
12441 if (comp_target_types (location, type0, type1))
12443 result_type = common_pointer_type (type0, type1);
12444 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12445 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12446 pedwarn_c99 (location, OPT_Wpedantic,
12447 "comparison of complete and incomplete pointers");
12448 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12449 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12450 "ordered comparisons of pointers to functions");
12451 else if (null_pointer_constant_p (orig_op0)
12452 || null_pointer_constant_p (orig_op1))
12453 warning_at (location, OPT_Wextra,
12454 "ordered comparison of pointer with null pointer");
12457 else if (!addr_space_superset (as0, as1, &as_common))
12459 error_at (location, "comparison of pointers to "
12460 "disjoint address spaces");
12461 return error_mark_node;
12463 else
12465 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12466 result_type = build_pointer_type
12467 (build_qualified_type (void_type_node, qual));
12468 pedwarn (location, 0,
12469 "comparison of distinct pointer types lacks a cast");
12472 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12474 result_type = type0;
12475 if (pedantic)
12476 pedwarn (location, OPT_Wpedantic,
12477 "ordered comparison of pointer with integer zero");
12478 else if (extra_warnings)
12479 warning_at (location, OPT_Wextra,
12480 "ordered comparison of pointer with integer zero");
12482 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12484 result_type = type1;
12485 if (pedantic)
12486 pedwarn (location, OPT_Wpedantic,
12487 "ordered comparison of pointer with integer zero");
12488 else if (extra_warnings)
12489 warning_at (location, OPT_Wextra,
12490 "ordered comparison of pointer with integer zero");
12492 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12494 result_type = type0;
12495 pedwarn (location, 0, "comparison between pointer and integer");
12497 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12499 result_type = type1;
12500 pedwarn (location, 0, "comparison between pointer and integer");
12503 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12504 && current_function_decl != NULL_TREE
12505 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12507 op0 = save_expr (op0);
12508 op1 = save_expr (op1);
12510 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12511 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12514 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12515 || truth_value_p (TREE_CODE (orig_op0)))
12516 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12517 || truth_value_p (TREE_CODE (orig_op1))))
12518 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12519 break;
12521 case MIN_EXPR:
12522 case MAX_EXPR:
12523 /* Used for OpenMP atomics. */
12524 gcc_assert (flag_openmp);
12525 common = 1;
12526 break;
12528 default:
12529 gcc_unreachable ();
12532 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12533 return error_mark_node;
12535 if (gnu_vector_type_p (type0)
12536 && gnu_vector_type_p (type1)
12537 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12538 || !vector_types_compatible_elements_p (type0, type1)))
12540 gcc_rich_location richloc (location);
12541 maybe_range_label_for_tree_type_mismatch
12542 label_for_op0 (orig_op0, orig_op1),
12543 label_for_op1 (orig_op1, orig_op0);
12544 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12545 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12546 binary_op_error (&richloc, code, type0, type1);
12547 return error_mark_node;
12550 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12551 || code0 == FIXED_POINT_TYPE
12552 || gnu_vector_type_p (type0))
12554 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12555 || code1 == FIXED_POINT_TYPE
12556 || gnu_vector_type_p (type1)))
12558 bool first_complex = (code0 == COMPLEX_TYPE);
12559 bool second_complex = (code1 == COMPLEX_TYPE);
12560 int none_complex = (!first_complex && !second_complex);
12562 if (shorten || common || short_compare)
12564 result_type = c_common_type (type0, type1);
12565 do_warn_double_promotion (result_type, type0, type1,
12566 "implicit conversion from %qT to %qT "
12567 "to match other operand of binary "
12568 "expression",
12569 location);
12570 if (result_type == error_mark_node)
12571 return error_mark_node;
12574 if (first_complex != second_complex
12575 && (code == PLUS_EXPR
12576 || code == MINUS_EXPR
12577 || code == MULT_EXPR
12578 || (code == TRUNC_DIV_EXPR && first_complex))
12579 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12580 && flag_signed_zeros)
12582 /* An operation on mixed real/complex operands must be
12583 handled specially, but the language-independent code can
12584 more easily optimize the plain complex arithmetic if
12585 -fno-signed-zeros. */
12586 tree real_type = TREE_TYPE (result_type);
12587 tree real, imag;
12588 if (type0 != orig_type0 || type1 != orig_type1)
12590 gcc_assert (may_need_excess_precision && common);
12591 semantic_result_type = c_common_type (orig_type0, orig_type1);
12593 if (first_complex)
12595 if (TREE_TYPE (op0) != result_type)
12596 op0 = convert_and_check (location, result_type, op0);
12597 if (TREE_TYPE (op1) != real_type)
12598 op1 = convert_and_check (location, real_type, op1);
12600 else
12602 if (TREE_TYPE (op0) != real_type)
12603 op0 = convert_and_check (location, real_type, op0);
12604 if (TREE_TYPE (op1) != result_type)
12605 op1 = convert_and_check (location, result_type, op1);
12607 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12608 return error_mark_node;
12609 if (first_complex)
12611 op0 = save_expr (op0);
12612 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12613 op0, true);
12614 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12615 op0, true);
12616 switch (code)
12618 case MULT_EXPR:
12619 case TRUNC_DIV_EXPR:
12620 op1 = save_expr (op1);
12621 imag = build2 (resultcode, real_type, imag, op1);
12622 /* Fall through. */
12623 case PLUS_EXPR:
12624 case MINUS_EXPR:
12625 real = build2 (resultcode, real_type, real, op1);
12626 break;
12627 default:
12628 gcc_unreachable();
12631 else
12633 op1 = save_expr (op1);
12634 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12635 op1, true);
12636 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12637 op1, true);
12638 switch (code)
12640 case MULT_EXPR:
12641 op0 = save_expr (op0);
12642 imag = build2 (resultcode, real_type, op0, imag);
12643 /* Fall through. */
12644 case PLUS_EXPR:
12645 real = build2 (resultcode, real_type, op0, real);
12646 break;
12647 case MINUS_EXPR:
12648 real = build2 (resultcode, real_type, op0, real);
12649 imag = build1 (NEGATE_EXPR, real_type, imag);
12650 break;
12651 default:
12652 gcc_unreachable();
12655 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12656 goto return_build_binary_op;
12659 /* For certain operations (which identify themselves by shorten != 0)
12660 if both args were extended from the same smaller type,
12661 do the arithmetic in that type and then extend.
12663 shorten !=0 and !=1 indicates a bitwise operation.
12664 For them, this optimization is safe only if
12665 both args are zero-extended or both are sign-extended.
12666 Otherwise, we might change the result.
12667 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12668 but calculated in (unsigned short) it would be (unsigned short)-1. */
12670 if (shorten && none_complex)
12672 final_type = result_type;
12673 result_type = shorten_binary_op (result_type, op0, op1,
12674 shorten == -1);
12677 /* Shifts can be shortened if shifting right. */
12679 if (short_shift)
12681 int unsigned_arg;
12682 tree arg0 = get_narrower (op0, &unsigned_arg);
12684 final_type = result_type;
12686 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12687 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12689 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12690 && tree_int_cst_sgn (op1) > 0
12691 /* We can shorten only if the shift count is less than the
12692 number of bits in the smaller type size. */
12693 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12694 /* We cannot drop an unsigned shift after sign-extension. */
12695 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12697 /* Do an unsigned shift if the operand was zero-extended. */
12698 result_type
12699 = c_common_signed_or_unsigned_type (unsigned_arg,
12700 TREE_TYPE (arg0));
12701 /* Convert value-to-be-shifted to that type. */
12702 if (TREE_TYPE (op0) != result_type)
12703 op0 = convert (result_type, op0);
12704 converted = 1;
12708 /* Comparison operations are shortened too but differently.
12709 They identify themselves by setting short_compare = 1. */
12711 if (short_compare)
12713 /* Don't write &op0, etc., because that would prevent op0
12714 from being kept in a register.
12715 Instead, make copies of the our local variables and
12716 pass the copies by reference, then copy them back afterward. */
12717 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12718 enum tree_code xresultcode = resultcode;
12719 tree val
12720 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12721 &xresultcode);
12723 if (val != NULL_TREE)
12725 ret = val;
12726 goto return_build_binary_op;
12729 op0 = xop0, op1 = xop1;
12730 converted = 1;
12731 resultcode = xresultcode;
12733 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
12735 bool op0_maybe_const = true;
12736 bool op1_maybe_const = true;
12737 tree orig_op0_folded, orig_op1_folded;
12739 if (in_late_binary_op)
12741 orig_op0_folded = orig_op0;
12742 orig_op1_folded = orig_op1;
12744 else
12746 /* Fold for the sake of possible warnings, as in
12747 build_conditional_expr. This requires the
12748 "original" values to be folded, not just op0 and
12749 op1. */
12750 c_inhibit_evaluation_warnings++;
12751 op0 = c_fully_fold (op0, require_constant_value,
12752 &op0_maybe_const);
12753 op1 = c_fully_fold (op1, require_constant_value,
12754 &op1_maybe_const);
12755 c_inhibit_evaluation_warnings--;
12756 orig_op0_folded = c_fully_fold (orig_op0,
12757 require_constant_value,
12758 NULL);
12759 orig_op1_folded = c_fully_fold (orig_op1,
12760 require_constant_value,
12761 NULL);
12764 if (warn_sign_compare)
12765 warn_for_sign_compare (location, orig_op0_folded,
12766 orig_op1_folded, op0, op1,
12767 result_type, resultcode);
12768 if (!in_late_binary_op && !int_operands)
12770 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12771 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12772 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12773 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12779 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12780 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12781 Then the expression will be built.
12782 It will be given type FINAL_TYPE if that is nonzero;
12783 otherwise, it will be given type RESULT_TYPE. */
12785 if (!result_type)
12787 /* Favor showing any expression locations that are available. */
12788 op_location_t oploc (location, UNKNOWN_LOCATION);
12789 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12790 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12791 return error_mark_node;
12794 if (build_type == NULL_TREE)
12796 build_type = result_type;
12797 if ((type0 != orig_type0 || type1 != orig_type1)
12798 && !boolean_op)
12800 gcc_assert (may_need_excess_precision && common);
12801 semantic_result_type = c_common_type (orig_type0, orig_type1);
12805 if (!converted)
12807 op0 = ep_convert_and_check (location, result_type, op0,
12808 semantic_result_type);
12809 op1 = ep_convert_and_check (location, result_type, op1,
12810 semantic_result_type);
12812 /* This can happen if one operand has a vector type, and the other
12813 has a different type. */
12814 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12815 return error_mark_node;
12818 if (sanitize_flags_p ((SANITIZE_SHIFT
12819 | SANITIZE_DIVIDE
12820 | SANITIZE_FLOAT_DIVIDE
12821 | SANITIZE_SI_OVERFLOW))
12822 && current_function_decl != NULL_TREE
12823 && (doing_div_or_mod || doing_shift)
12824 && !require_constant_value)
12826 /* OP0 and/or OP1 might have side-effects. */
12827 op0 = save_expr (op0);
12828 op1 = save_expr (op1);
12829 op0 = c_fully_fold (op0, false, NULL);
12830 op1 = c_fully_fold (op1, false, NULL);
12831 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12832 | SANITIZE_FLOAT_DIVIDE
12833 | SANITIZE_SI_OVERFLOW))))
12834 instrument_expr = ubsan_instrument_division (location, op0, op1);
12835 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12836 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12839 /* Treat expressions in initializers specially as they can't trap. */
12840 if (int_const_or_overflow)
12841 ret = (require_constant_value
12842 ? fold_build2_initializer_loc (location, resultcode, build_type,
12843 op0, op1)
12844 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12845 else
12846 ret = build2 (resultcode, build_type, op0, op1);
12847 if (final_type != NULL_TREE)
12848 ret = convert (final_type, ret);
12850 return_build_binary_op:
12851 gcc_assert (ret != error_mark_node);
12852 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12853 ret = (int_operands
12854 ? note_integer_operands (ret)
12855 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12856 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12857 && !in_late_binary_op)
12858 ret = note_integer_operands (ret);
12859 protected_set_expr_location (ret, location);
12861 if (instrument_expr != NULL)
12862 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12863 instrument_expr, ret);
12865 if (semantic_result_type)
12866 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12867 semantic_result_type, ret);
12869 return ret;
12873 /* Convert EXPR to be a truth-value, validating its type for this
12874 purpose. LOCATION is the source location for the expression. */
12876 tree
12877 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12879 bool int_const, int_operands;
12881 switch (TREE_CODE (TREE_TYPE (expr)))
12883 case ARRAY_TYPE:
12884 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12885 return error_mark_node;
12887 case RECORD_TYPE:
12888 error_at (location, "used struct type value where scalar is required");
12889 return error_mark_node;
12891 case UNION_TYPE:
12892 error_at (location, "used union type value where scalar is required");
12893 return error_mark_node;
12895 case VOID_TYPE:
12896 error_at (location, "void value not ignored as it ought to be");
12897 return error_mark_node;
12899 case POINTER_TYPE:
12900 if (reject_gcc_builtin (expr))
12901 return error_mark_node;
12902 break;
12904 case FUNCTION_TYPE:
12905 gcc_unreachable ();
12907 case VECTOR_TYPE:
12908 error_at (location, "used vector type where scalar is required");
12909 return error_mark_node;
12911 default:
12912 break;
12915 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12916 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12917 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12919 expr = remove_c_maybe_const_expr (expr);
12920 expr = build2 (NE_EXPR, integer_type_node, expr,
12921 convert (TREE_TYPE (expr), integer_zero_node));
12922 expr = note_integer_operands (expr);
12924 else
12925 /* ??? Should we also give an error for vectors rather than leaving
12926 those to give errors later? */
12927 expr = c_common_truthvalue_conversion (location, expr);
12929 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12931 if (TREE_OVERFLOW (expr))
12932 return expr;
12933 else
12934 return note_integer_operands (expr);
12936 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12937 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12938 return expr;
12942 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12943 required. */
12945 tree
12946 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12948 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12950 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12951 /* Executing a compound literal inside a function reinitializes
12952 it. */
12953 if (!TREE_STATIC (decl))
12954 *se = true;
12955 return decl;
12957 else
12958 return expr;
12961 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12962 statement. LOC is the location of the construct. */
12964 tree
12965 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12966 tree clauses)
12968 body = c_end_compound_stmt (loc, body, true);
12970 tree stmt = make_node (code);
12971 TREE_TYPE (stmt) = void_type_node;
12972 OMP_BODY (stmt) = body;
12973 OMP_CLAUSES (stmt) = clauses;
12974 SET_EXPR_LOCATION (stmt, loc);
12976 return add_stmt (stmt);
12979 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12980 statement. LOC is the location of the OACC_DATA. */
12982 tree
12983 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12985 tree stmt;
12987 block = c_end_compound_stmt (loc, block, true);
12989 stmt = make_node (OACC_DATA);
12990 TREE_TYPE (stmt) = void_type_node;
12991 OACC_DATA_CLAUSES (stmt) = clauses;
12992 OACC_DATA_BODY (stmt) = block;
12993 SET_EXPR_LOCATION (stmt, loc);
12995 return add_stmt (stmt);
12998 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12999 statement. LOC is the location of the OACC_HOST_DATA. */
13001 tree
13002 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
13004 tree stmt;
13006 block = c_end_compound_stmt (loc, block, true);
13008 stmt = make_node (OACC_HOST_DATA);
13009 TREE_TYPE (stmt) = void_type_node;
13010 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
13011 OACC_HOST_DATA_BODY (stmt) = block;
13012 SET_EXPR_LOCATION (stmt, loc);
13014 return add_stmt (stmt);
13017 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13019 tree
13020 c_begin_omp_parallel (void)
13022 tree block;
13024 keep_next_level ();
13025 block = c_begin_compound_stmt (true);
13027 return block;
13030 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
13031 statement. LOC is the location of the OMP_PARALLEL. */
13033 tree
13034 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
13036 tree stmt;
13038 block = c_end_compound_stmt (loc, block, true);
13040 stmt = make_node (OMP_PARALLEL);
13041 TREE_TYPE (stmt) = void_type_node;
13042 OMP_PARALLEL_CLAUSES (stmt) = clauses;
13043 OMP_PARALLEL_BODY (stmt) = block;
13044 SET_EXPR_LOCATION (stmt, loc);
13046 return add_stmt (stmt);
13049 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13051 tree
13052 c_begin_omp_task (void)
13054 tree block;
13056 keep_next_level ();
13057 block = c_begin_compound_stmt (true);
13059 return block;
13062 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
13063 statement. LOC is the location of the #pragma. */
13065 tree
13066 c_finish_omp_task (location_t loc, tree clauses, tree block)
13068 tree stmt;
13070 block = c_end_compound_stmt (loc, block, true);
13072 stmt = make_node (OMP_TASK);
13073 TREE_TYPE (stmt) = void_type_node;
13074 OMP_TASK_CLAUSES (stmt) = clauses;
13075 OMP_TASK_BODY (stmt) = block;
13076 SET_EXPR_LOCATION (stmt, loc);
13078 return add_stmt (stmt);
13081 /* Generate GOMP_cancel call for #pragma omp cancel. */
13083 void
13084 c_finish_omp_cancel (location_t loc, tree clauses)
13086 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
13087 int mask = 0;
13088 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13089 mask = 1;
13090 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13091 mask = 2;
13092 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13093 mask = 4;
13094 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13095 mask = 8;
13096 else
13098 error_at (loc, "%<#pragma omp cancel%> must specify one of "
13099 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13100 "clauses");
13101 return;
13103 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
13104 if (ifc != NULL_TREE)
13106 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
13107 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
13108 error_at (OMP_CLAUSE_LOCATION (ifc),
13109 "expected %<cancel%> %<if%> clause modifier");
13110 else
13112 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
13113 if (ifc2 != NULL_TREE)
13115 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
13116 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13117 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13118 error_at (OMP_CLAUSE_LOCATION (ifc2),
13119 "expected %<cancel%> %<if%> clause modifier");
13123 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13124 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13125 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13126 build_zero_cst (type));
13128 else
13129 ifc = boolean_true_node;
13130 tree stmt = build_call_expr_loc (loc, fn, 2,
13131 build_int_cst (integer_type_node, mask),
13132 ifc);
13133 add_stmt (stmt);
13136 /* Generate GOMP_cancellation_point call for
13137 #pragma omp cancellation point. */
13139 void
13140 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13142 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13143 int mask = 0;
13144 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13145 mask = 1;
13146 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13147 mask = 2;
13148 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13149 mask = 4;
13150 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13151 mask = 8;
13152 else
13154 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13155 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13156 "clauses");
13157 return;
13159 tree stmt = build_call_expr_loc (loc, fn, 1,
13160 build_int_cst (integer_type_node, mask));
13161 add_stmt (stmt);
13164 /* Helper function for handle_omp_array_sections. Called recursively
13165 to handle multiple array-section-subscripts. C is the clause,
13166 T current expression (initially OMP_CLAUSE_DECL), which is either
13167 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13168 expression if specified, TREE_VALUE length expression if specified,
13169 TREE_CHAIN is what it has been specified after, or some decl.
13170 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13171 set to true if any of the array-section-subscript could have length
13172 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13173 first array-section-subscript which is known not to have length
13174 of one. Given say:
13175 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13176 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13177 all are or may have length of 1, array-section-subscript [:2] is the
13178 first one known not to have length 1. For array-section-subscript
13179 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13180 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13181 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
13182 case though, as some lengths could be zero. */
13184 static tree
13185 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13186 bool &maybe_zero_len, unsigned int &first_non_one,
13187 enum c_omp_region_type ort)
13189 tree ret, low_bound, length, type;
13190 if (TREE_CODE (t) != TREE_LIST)
13192 if (error_operand_p (t))
13193 return error_mark_node;
13194 ret = t;
13195 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13196 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13197 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13199 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13200 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13201 return error_mark_node;
13203 if (TREE_CODE (t) == COMPONENT_REF
13204 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13205 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13206 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13208 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13210 error_at (OMP_CLAUSE_LOCATION (c),
13211 "bit-field %qE in %qs clause",
13212 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13213 return error_mark_node;
13215 while (TREE_CODE (t) == COMPONENT_REF)
13217 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13219 error_at (OMP_CLAUSE_LOCATION (c),
13220 "%qE is a member of a union", t);
13221 return error_mark_node;
13223 t = TREE_OPERAND (t, 0);
13224 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13226 if (maybe_ne (mem_ref_offset (t), 0))
13227 error_at (OMP_CLAUSE_LOCATION (c),
13228 "cannot dereference %qE in %qs clause", t,
13229 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13230 else
13231 t = TREE_OPERAND (t, 0);
13235 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13237 if (DECL_P (t))
13238 error_at (OMP_CLAUSE_LOCATION (c),
13239 "%qD is not a variable in %qs clause", t,
13240 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13241 else
13242 error_at (OMP_CLAUSE_LOCATION (c),
13243 "%qE is not a variable in %qs clause", t,
13244 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13245 return error_mark_node;
13247 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13248 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13249 && TYPE_ATOMIC (TREE_TYPE (t)))
13251 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13252 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13253 return error_mark_node;
13255 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13256 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13257 && VAR_P (t)
13258 && DECL_THREAD_LOCAL_P (t))
13260 error_at (OMP_CLAUSE_LOCATION (c),
13261 "%qD is threadprivate variable in %qs clause", t,
13262 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13263 return error_mark_node;
13265 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13266 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13267 && TYPE_ATOMIC (TREE_TYPE (t))
13268 && POINTER_TYPE_P (TREE_TYPE (t)))
13270 /* If the array section is pointer based and the pointer
13271 itself is _Atomic qualified, we need to atomically load
13272 the pointer. */
13273 c_expr expr;
13274 memset (&expr, 0, sizeof (expr));
13275 expr.value = ret;
13276 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13277 expr, false, false);
13278 ret = expr.value;
13280 return ret;
13283 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13284 maybe_zero_len, first_non_one, ort);
13285 if (ret == error_mark_node || ret == NULL_TREE)
13286 return ret;
13288 type = TREE_TYPE (ret);
13289 low_bound = TREE_PURPOSE (t);
13290 length = TREE_VALUE (t);
13292 if (low_bound == error_mark_node || length == error_mark_node)
13293 return error_mark_node;
13295 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13297 error_at (OMP_CLAUSE_LOCATION (c),
13298 "low bound %qE of array section does not have integral type",
13299 low_bound);
13300 return error_mark_node;
13302 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13304 error_at (OMP_CLAUSE_LOCATION (c),
13305 "length %qE of array section does not have integral type",
13306 length);
13307 return error_mark_node;
13309 if (low_bound
13310 && TREE_CODE (low_bound) == INTEGER_CST
13311 && TYPE_PRECISION (TREE_TYPE (low_bound))
13312 > TYPE_PRECISION (sizetype))
13313 low_bound = fold_convert (sizetype, low_bound);
13314 if (length
13315 && TREE_CODE (length) == INTEGER_CST
13316 && TYPE_PRECISION (TREE_TYPE (length))
13317 > TYPE_PRECISION (sizetype))
13318 length = fold_convert (sizetype, length);
13319 if (low_bound == NULL_TREE)
13320 low_bound = integer_zero_node;
13321 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13322 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13323 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13325 if (length != integer_one_node)
13327 error_at (OMP_CLAUSE_LOCATION (c),
13328 "expected single pointer in %qs clause",
13329 c_omp_map_clause_name (c, ort == C_ORT_ACC));
13330 return error_mark_node;
13333 if (length != NULL_TREE)
13335 if (!integer_nonzerop (length))
13337 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13338 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13339 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13340 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13341 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13343 if (integer_zerop (length))
13345 error_at (OMP_CLAUSE_LOCATION (c),
13346 "zero length array section in %qs clause",
13347 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13348 return error_mark_node;
13351 else
13352 maybe_zero_len = true;
13354 if (first_non_one == types.length ()
13355 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13356 first_non_one++;
13358 if (TREE_CODE (type) == ARRAY_TYPE)
13360 if (length == NULL_TREE
13361 && (TYPE_DOMAIN (type) == NULL_TREE
13362 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13364 error_at (OMP_CLAUSE_LOCATION (c),
13365 "for unknown bound array type length expression must "
13366 "be specified");
13367 return error_mark_node;
13369 if (TREE_CODE (low_bound) == INTEGER_CST
13370 && tree_int_cst_sgn (low_bound) == -1)
13372 error_at (OMP_CLAUSE_LOCATION (c),
13373 "negative low bound in array section in %qs clause",
13374 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13375 return error_mark_node;
13377 if (length != NULL_TREE
13378 && TREE_CODE (length) == INTEGER_CST
13379 && tree_int_cst_sgn (length) == -1)
13381 error_at (OMP_CLAUSE_LOCATION (c),
13382 "negative length in array section in %qs clause",
13383 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13384 return error_mark_node;
13386 if (TYPE_DOMAIN (type)
13387 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13388 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13389 == INTEGER_CST)
13391 tree size
13392 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13393 size = size_binop (PLUS_EXPR, size, size_one_node);
13394 if (TREE_CODE (low_bound) == INTEGER_CST)
13396 if (tree_int_cst_lt (size, low_bound))
13398 error_at (OMP_CLAUSE_LOCATION (c),
13399 "low bound %qE above array section size "
13400 "in %qs clause", low_bound,
13401 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13402 return error_mark_node;
13404 if (tree_int_cst_equal (size, low_bound))
13406 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13407 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13408 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13409 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13410 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13412 error_at (OMP_CLAUSE_LOCATION (c),
13413 "zero length array section in %qs clause",
13414 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13415 return error_mark_node;
13417 maybe_zero_len = true;
13419 else if (length == NULL_TREE
13420 && first_non_one == types.length ()
13421 && tree_int_cst_equal
13422 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13423 low_bound))
13424 first_non_one++;
13426 else if (length == NULL_TREE)
13428 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13429 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13430 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13431 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13432 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13433 maybe_zero_len = true;
13434 if (first_non_one == types.length ())
13435 first_non_one++;
13437 if (length && TREE_CODE (length) == INTEGER_CST)
13439 if (tree_int_cst_lt (size, length))
13441 error_at (OMP_CLAUSE_LOCATION (c),
13442 "length %qE above array section size "
13443 "in %qs clause", length,
13444 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13445 return error_mark_node;
13447 if (TREE_CODE (low_bound) == INTEGER_CST)
13449 tree lbpluslen
13450 = size_binop (PLUS_EXPR,
13451 fold_convert (sizetype, low_bound),
13452 fold_convert (sizetype, length));
13453 if (TREE_CODE (lbpluslen) == INTEGER_CST
13454 && tree_int_cst_lt (size, lbpluslen))
13456 error_at (OMP_CLAUSE_LOCATION (c),
13457 "high bound %qE above array section size "
13458 "in %qs clause", lbpluslen,
13459 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13460 return error_mark_node;
13465 else if (length == NULL_TREE)
13467 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13468 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13469 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13470 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13471 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13472 maybe_zero_len = true;
13473 if (first_non_one == types.length ())
13474 first_non_one++;
13477 /* For [lb:] we will need to evaluate lb more than once. */
13478 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13480 tree lb = save_expr (low_bound);
13481 if (lb != low_bound)
13483 TREE_PURPOSE (t) = lb;
13484 low_bound = lb;
13488 else if (TREE_CODE (type) == POINTER_TYPE)
13490 if (length == NULL_TREE)
13492 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
13493 error_at (OMP_CLAUSE_LOCATION (c),
13494 "for array function parameter length expression "
13495 "must be specified");
13496 else
13497 error_at (OMP_CLAUSE_LOCATION (c),
13498 "for pointer type length expression must be specified");
13499 return error_mark_node;
13501 if (length != NULL_TREE
13502 && TREE_CODE (length) == INTEGER_CST
13503 && tree_int_cst_sgn (length) == -1)
13505 error_at (OMP_CLAUSE_LOCATION (c),
13506 "negative length in array section in %qs clause",
13507 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13508 return error_mark_node;
13510 /* If there is a pointer type anywhere but in the very first
13511 array-section-subscript, the array section can't be contiguous. */
13512 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13513 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13514 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13516 error_at (OMP_CLAUSE_LOCATION (c),
13517 "array section is not contiguous in %qs clause",
13518 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13519 return error_mark_node;
13522 else
13524 error_at (OMP_CLAUSE_LOCATION (c),
13525 "%qE does not have pointer or array type", ret);
13526 return error_mark_node;
13528 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13529 types.safe_push (TREE_TYPE (ret));
13530 /* We will need to evaluate lb more than once. */
13531 tree lb = save_expr (low_bound);
13532 if (lb != low_bound)
13534 TREE_PURPOSE (t) = lb;
13535 low_bound = lb;
13537 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13538 return ret;
13541 /* Handle array sections for clause C. */
13543 static bool
13544 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13546 bool maybe_zero_len = false;
13547 unsigned int first_non_one = 0;
13548 auto_vec<tree, 10> types;
13549 tree *tp = &OMP_CLAUSE_DECL (c);
13550 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13551 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13552 && TREE_CODE (*tp) == TREE_LIST
13553 && TREE_PURPOSE (*tp)
13554 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13555 tp = &TREE_VALUE (*tp);
13556 tree first = handle_omp_array_sections_1 (c, *tp, types,
13557 maybe_zero_len, first_non_one,
13558 ort);
13559 if (first == error_mark_node)
13560 return true;
13561 if (first == NULL_TREE)
13562 return false;
13563 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13564 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13566 tree t = *tp;
13567 tree tem = NULL_TREE;
13568 /* Need to evaluate side effects in the length expressions
13569 if any. */
13570 while (TREE_CODE (t) == TREE_LIST)
13572 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13574 if (tem == NULL_TREE)
13575 tem = TREE_VALUE (t);
13576 else
13577 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13578 TREE_VALUE (t), tem);
13580 t = TREE_CHAIN (t);
13582 if (tem)
13583 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13584 first = c_fully_fold (first, false, NULL, true);
13585 *tp = first;
13587 else
13589 unsigned int num = types.length (), i;
13590 tree t, side_effects = NULL_TREE, size = NULL_TREE;
13591 tree condition = NULL_TREE;
13593 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13594 maybe_zero_len = true;
13596 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13597 t = TREE_CHAIN (t))
13599 tree low_bound = TREE_PURPOSE (t);
13600 tree length = TREE_VALUE (t);
13602 i--;
13603 if (low_bound
13604 && TREE_CODE (low_bound) == INTEGER_CST
13605 && TYPE_PRECISION (TREE_TYPE (low_bound))
13606 > TYPE_PRECISION (sizetype))
13607 low_bound = fold_convert (sizetype, low_bound);
13608 if (length
13609 && TREE_CODE (length) == INTEGER_CST
13610 && TYPE_PRECISION (TREE_TYPE (length))
13611 > TYPE_PRECISION (sizetype))
13612 length = fold_convert (sizetype, length);
13613 if (low_bound == NULL_TREE)
13614 low_bound = integer_zero_node;
13615 if (!maybe_zero_len && i > first_non_one)
13617 if (integer_nonzerop (low_bound))
13618 goto do_warn_noncontiguous;
13619 if (length != NULL_TREE
13620 && TREE_CODE (length) == INTEGER_CST
13621 && TYPE_DOMAIN (types[i])
13622 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13623 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13624 == INTEGER_CST)
13626 tree size;
13627 size = size_binop (PLUS_EXPR,
13628 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13629 size_one_node);
13630 if (!tree_int_cst_equal (length, size))
13632 do_warn_noncontiguous:
13633 error_at (OMP_CLAUSE_LOCATION (c),
13634 "array section is not contiguous in %qs "
13635 "clause",
13636 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13637 return true;
13640 if (length != NULL_TREE
13641 && TREE_SIDE_EFFECTS (length))
13643 if (side_effects == NULL_TREE)
13644 side_effects = length;
13645 else
13646 side_effects = build2 (COMPOUND_EXPR,
13647 TREE_TYPE (side_effects),
13648 length, side_effects);
13651 else
13653 tree l;
13655 if (i > first_non_one
13656 && ((length && integer_nonzerop (length))
13657 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13658 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13659 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13660 continue;
13661 if (length)
13662 l = fold_convert (sizetype, length);
13663 else
13665 l = size_binop (PLUS_EXPR,
13666 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13667 size_one_node);
13668 l = size_binop (MINUS_EXPR, l,
13669 fold_convert (sizetype, low_bound));
13671 if (i > first_non_one)
13673 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13674 size_zero_node);
13675 if (condition == NULL_TREE)
13676 condition = l;
13677 else
13678 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13679 l, condition);
13681 else if (size == NULL_TREE)
13683 size = size_in_bytes (TREE_TYPE (types[i]));
13684 tree eltype = TREE_TYPE (types[num - 1]);
13685 while (TREE_CODE (eltype) == ARRAY_TYPE)
13686 eltype = TREE_TYPE (eltype);
13687 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13688 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13689 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13691 if (integer_zerop (size)
13692 || integer_zerop (size_in_bytes (eltype)))
13694 error_at (OMP_CLAUSE_LOCATION (c),
13695 "zero length array section in %qs clause",
13696 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13697 return error_mark_node;
13699 size = size_binop (EXACT_DIV_EXPR, size,
13700 size_in_bytes (eltype));
13702 size = size_binop (MULT_EXPR, size, l);
13703 if (condition)
13704 size = fold_build3 (COND_EXPR, sizetype, condition,
13705 size, size_zero_node);
13707 else
13708 size = size_binop (MULT_EXPR, size, l);
13711 if (side_effects)
13712 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13713 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13714 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13715 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13717 size = size_binop (MINUS_EXPR, size, size_one_node);
13718 size = c_fully_fold (size, false, NULL);
13719 size = save_expr (size);
13720 tree index_type = build_index_type (size);
13721 tree eltype = TREE_TYPE (first);
13722 while (TREE_CODE (eltype) == ARRAY_TYPE)
13723 eltype = TREE_TYPE (eltype);
13724 tree type = build_array_type (eltype, index_type);
13725 tree ptype = build_pointer_type (eltype);
13726 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13727 t = build_fold_addr_expr (t);
13728 tree t2 = build_fold_addr_expr (first);
13729 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13730 ptrdiff_type_node, t2);
13731 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13732 ptrdiff_type_node, t2,
13733 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13734 ptrdiff_type_node, t));
13735 t2 = c_fully_fold (t2, false, NULL);
13736 if (tree_fits_shwi_p (t2))
13737 t = build2 (MEM_REF, type, t,
13738 build_int_cst (ptype, tree_to_shwi (t2)));
13739 else
13741 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13742 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13743 TREE_TYPE (t), t, t2);
13744 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13746 OMP_CLAUSE_DECL (c) = t;
13747 return false;
13749 first = c_fully_fold (first, false, NULL);
13750 OMP_CLAUSE_DECL (c) = first;
13751 if (size)
13752 size = c_fully_fold (size, false, NULL);
13753 OMP_CLAUSE_SIZE (c) = size;
13754 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13755 || (TREE_CODE (t) == COMPONENT_REF
13756 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13757 return false;
13758 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13759 switch (OMP_CLAUSE_MAP_KIND (c))
13761 case GOMP_MAP_ALLOC:
13762 case GOMP_MAP_IF_PRESENT:
13763 case GOMP_MAP_TO:
13764 case GOMP_MAP_FROM:
13765 case GOMP_MAP_TOFROM:
13766 case GOMP_MAP_ALWAYS_TO:
13767 case GOMP_MAP_ALWAYS_FROM:
13768 case GOMP_MAP_ALWAYS_TOFROM:
13769 case GOMP_MAP_RELEASE:
13770 case GOMP_MAP_DELETE:
13771 case GOMP_MAP_FORCE_TO:
13772 case GOMP_MAP_FORCE_FROM:
13773 case GOMP_MAP_FORCE_TOFROM:
13774 case GOMP_MAP_FORCE_PRESENT:
13775 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13776 break;
13777 default:
13778 break;
13780 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13781 if (TREE_CODE (t) == COMPONENT_REF)
13782 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
13783 else
13784 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13785 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
13786 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13787 && !c_mark_addressable (t))
13788 return false;
13789 OMP_CLAUSE_DECL (c2) = t;
13790 t = build_fold_addr_expr (first);
13791 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13792 tree ptr = OMP_CLAUSE_DECL (c2);
13793 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13794 ptr = build_fold_addr_expr (ptr);
13795 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13796 ptrdiff_type_node, t,
13797 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13798 ptrdiff_type_node, ptr));
13799 t = c_fully_fold (t, false, NULL);
13800 OMP_CLAUSE_SIZE (c2) = t;
13801 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13802 OMP_CLAUSE_CHAIN (c) = c2;
13804 return false;
13807 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13808 an inline call. But, remap
13809 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13810 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13812 static tree
13813 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13814 tree decl, tree placeholder)
13816 copy_body_data id;
13817 hash_map<tree, tree> decl_map;
13819 decl_map.put (omp_decl1, placeholder);
13820 decl_map.put (omp_decl2, decl);
13821 memset (&id, 0, sizeof (id));
13822 id.src_fn = DECL_CONTEXT (omp_decl1);
13823 id.dst_fn = current_function_decl;
13824 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13825 id.decl_map = &decl_map;
13827 id.copy_decl = copy_decl_no_change;
13828 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13829 id.transform_new_cfg = true;
13830 id.transform_return_to_modify = false;
13831 id.transform_lang_insert_block = NULL;
13832 id.eh_lp_nr = 0;
13833 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13834 return stmt;
13837 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13838 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13840 static tree
13841 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13843 if (*tp == (tree) data)
13844 return *tp;
13845 return NULL_TREE;
13848 /* Similarly, but also walk aggregate fields. */
13850 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13852 static tree
13853 c_find_omp_var_r (tree *tp, int *, void *data)
13855 if (*tp == ((struct c_find_omp_var_s *) data)->var)
13856 return *tp;
13857 if (RECORD_OR_UNION_TYPE_P (*tp))
13859 tree field;
13860 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13862 for (field = TYPE_FIELDS (*tp); field;
13863 field = DECL_CHAIN (field))
13864 if (TREE_CODE (field) == FIELD_DECL)
13866 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13867 c_find_omp_var_r, data, pset);
13868 if (ret)
13869 return ret;
13870 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13871 if (ret)
13872 return ret;
13873 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13874 pset);
13875 if (ret)
13876 return ret;
13877 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13878 if (ret)
13879 return ret;
13882 else if (INTEGRAL_TYPE_P (*tp))
13883 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13884 ((struct c_find_omp_var_s *) data)->pset);
13885 return NULL_TREE;
13888 /* Finish OpenMP iterators ITER. Return true if they are errorneous
13889 and clauses containing them should be removed. */
13891 static bool
13892 c_omp_finish_iterators (tree iter)
13894 bool ret = false;
13895 for (tree it = iter; it; it = TREE_CHAIN (it))
13897 tree var = TREE_VEC_ELT (it, 0);
13898 tree begin = TREE_VEC_ELT (it, 1);
13899 tree end = TREE_VEC_ELT (it, 2);
13900 tree step = TREE_VEC_ELT (it, 3);
13901 tree orig_step;
13902 tree type = TREE_TYPE (var);
13903 location_t loc = DECL_SOURCE_LOCATION (var);
13904 if (type == error_mark_node)
13906 ret = true;
13907 continue;
13909 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13911 error_at (loc, "iterator %qD has neither integral nor pointer type",
13912 var);
13913 ret = true;
13914 continue;
13916 else if (TYPE_ATOMIC (type))
13918 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
13919 ret = true;
13920 continue;
13922 else if (TYPE_READONLY (type))
13924 error_at (loc, "iterator %qD has const qualified type", var);
13925 ret = true;
13926 continue;
13928 else if (step == error_mark_node
13929 || TREE_TYPE (step) == error_mark_node)
13931 ret = true;
13932 continue;
13934 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13936 error_at (EXPR_LOC_OR_LOC (step, loc),
13937 "iterator step with non-integral type");
13938 ret = true;
13939 continue;
13941 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
13942 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
13943 orig_step = save_expr (c_fully_fold (step, false, NULL));
13944 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
13945 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
13946 if (POINTER_TYPE_P (type))
13948 begin = save_expr (begin);
13949 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
13950 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
13951 fold_convert (sizetype, step),
13952 fold_convert (sizetype, begin));
13953 step = fold_convert (ssizetype, step);
13955 if (integer_zerop (step))
13957 error_at (loc, "iterator %qD has zero step", var);
13958 ret = true;
13959 continue;
13962 if (begin == error_mark_node
13963 || end == error_mark_node
13964 || step == error_mark_node
13965 || orig_step == error_mark_node)
13967 ret = true;
13968 continue;
13970 hash_set<tree> pset;
13971 tree it2;
13972 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
13974 tree var2 = TREE_VEC_ELT (it2, 0);
13975 tree begin2 = TREE_VEC_ELT (it2, 1);
13976 tree end2 = TREE_VEC_ELT (it2, 2);
13977 tree step2 = TREE_VEC_ELT (it2, 3);
13978 tree type2 = TREE_TYPE (var2);
13979 location_t loc2 = DECL_SOURCE_LOCATION (var2);
13980 struct c_find_omp_var_s data = { var, &pset };
13981 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
13983 error_at (loc2,
13984 "type of iterator %qD refers to outer iterator %qD",
13985 var2, var);
13986 break;
13988 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
13990 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
13991 "begin expression refers to outer iterator %qD", var);
13992 break;
13994 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
13996 error_at (EXPR_LOC_OR_LOC (end2, loc2),
13997 "end expression refers to outer iterator %qD", var);
13998 break;
14000 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
14002 error_at (EXPR_LOC_OR_LOC (step2, loc2),
14003 "step expression refers to outer iterator %qD", var);
14004 break;
14007 if (it2)
14009 ret = true;
14010 continue;
14012 TREE_VEC_ELT (it, 1) = begin;
14013 TREE_VEC_ELT (it, 2) = end;
14014 TREE_VEC_ELT (it, 3) = step;
14015 TREE_VEC_ELT (it, 4) = orig_step;
14017 return ret;
14020 /* Ensure that pointers are used in OpenACC attach and detach clauses.
14021 Return true if an error has been detected. */
14023 static bool
14024 c_oacc_check_attachments (tree c)
14026 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14027 return false;
14029 /* OpenACC attach / detach clauses must be pointers. */
14030 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14031 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14033 tree t = OMP_CLAUSE_DECL (c);
14035 while (TREE_CODE (t) == TREE_LIST)
14036 t = TREE_CHAIN (t);
14038 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14040 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
14041 c_omp_map_clause_name (c, true));
14042 return true;
14046 return false;
14049 /* For all elements of CLAUSES, validate them against their constraints.
14050 Remove any elements from the list that are invalid. */
14052 tree
14053 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
14055 bitmap_head generic_head, firstprivate_head, lastprivate_head;
14056 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
14057 bitmap_head oacc_reduction_head;
14058 tree c, t, type, *pc;
14059 tree simdlen = NULL_TREE, safelen = NULL_TREE;
14060 bool branch_seen = false;
14061 bool copyprivate_seen = false;
14062 bool mergeable_seen = false;
14063 tree *detach_seen = NULL;
14064 bool linear_variable_step_check = false;
14065 tree *nowait_clause = NULL;
14066 tree ordered_clause = NULL_TREE;
14067 tree schedule_clause = NULL_TREE;
14068 bool oacc_async = false;
14069 tree last_iterators = NULL_TREE;
14070 bool last_iterators_remove = false;
14071 tree *nogroup_seen = NULL;
14072 tree *order_clause = NULL;
14073 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
14074 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
14075 int reduction_seen = 0;
14076 bool allocate_seen = false;
14077 bool implicit_moved = false;
14078 bool target_in_reduction_seen = false;
14080 bitmap_obstack_initialize (NULL);
14081 bitmap_initialize (&generic_head, &bitmap_default_obstack);
14082 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
14083 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
14084 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
14085 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
14086 bitmap_initialize (&map_head, &bitmap_default_obstack);
14087 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
14088 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
14089 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
14090 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
14091 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
14093 if (ort & C_ORT_ACC)
14094 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14095 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
14097 oacc_async = true;
14098 break;
14101 for (pc = &clauses, c = clauses; c ; c = *pc)
14103 bool remove = false;
14104 bool need_complete = false;
14105 bool need_implicitly_determined = false;
14107 switch (OMP_CLAUSE_CODE (c))
14109 case OMP_CLAUSE_SHARED:
14110 need_implicitly_determined = true;
14111 goto check_dup_generic;
14113 case OMP_CLAUSE_PRIVATE:
14114 need_complete = true;
14115 need_implicitly_determined = true;
14116 goto check_dup_generic;
14118 case OMP_CLAUSE_REDUCTION:
14119 if (reduction_seen == 0)
14120 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14121 else if (reduction_seen != -2
14122 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14123 ? -1 : 1))
14125 error_at (OMP_CLAUSE_LOCATION (c),
14126 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14127 "on the same construct");
14128 reduction_seen = -2;
14130 /* FALLTHRU */
14131 case OMP_CLAUSE_IN_REDUCTION:
14132 case OMP_CLAUSE_TASK_REDUCTION:
14133 need_implicitly_determined = true;
14134 t = OMP_CLAUSE_DECL (c);
14135 if (TREE_CODE (t) == TREE_LIST)
14137 if (handle_omp_array_sections (c, ort))
14139 remove = true;
14140 break;
14143 t = OMP_CLAUSE_DECL (c);
14144 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14145 && OMP_CLAUSE_REDUCTION_INSCAN (c))
14147 error_at (OMP_CLAUSE_LOCATION (c),
14148 "%<inscan%> %<reduction%> clause with array "
14149 "section");
14150 remove = true;
14151 break;
14154 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14155 if (t == error_mark_node)
14157 remove = true;
14158 break;
14160 if (oacc_async)
14161 c_mark_addressable (t);
14162 type = TREE_TYPE (t);
14163 if (TREE_CODE (t) == MEM_REF)
14164 type = TREE_TYPE (type);
14165 if (TREE_CODE (type) == ARRAY_TYPE)
14167 tree oatype = type;
14168 gcc_assert (TREE_CODE (t) != MEM_REF);
14169 while (TREE_CODE (type) == ARRAY_TYPE)
14170 type = TREE_TYPE (type);
14171 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14173 error_at (OMP_CLAUSE_LOCATION (c),
14174 "%qD in %<reduction%> clause is a zero size array",
14176 remove = true;
14177 break;
14179 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14180 TYPE_SIZE_UNIT (type));
14181 if (integer_zerop (size))
14183 error_at (OMP_CLAUSE_LOCATION (c),
14184 "%qD in %<reduction%> clause is a zero size array",
14186 remove = true;
14187 break;
14189 size = size_binop (MINUS_EXPR, size, size_one_node);
14190 size = save_expr (size);
14191 tree index_type = build_index_type (size);
14192 tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14193 index_type);
14194 atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14195 tree ptype = build_pointer_type (type);
14196 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14197 t = build_fold_addr_expr (t);
14198 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14199 OMP_CLAUSE_DECL (c) = t;
14201 if (TYPE_ATOMIC (type))
14203 error_at (OMP_CLAUSE_LOCATION (c),
14204 "%<_Atomic%> %qE in %<reduction%> clause", t);
14205 remove = true;
14206 break;
14208 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14209 || OMP_CLAUSE_REDUCTION_TASK (c))
14211 /* Disallow zero sized or potentially zero sized task
14212 reductions. */
14213 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14215 error_at (OMP_CLAUSE_LOCATION (c),
14216 "zero sized type %qT in %qs clause", type,
14217 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14218 remove = true;
14219 break;
14221 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14223 error_at (OMP_CLAUSE_LOCATION (c),
14224 "variable sized type %qT in %qs clause", type,
14225 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14226 remove = true;
14227 break;
14230 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14231 && (FLOAT_TYPE_P (type)
14232 || TREE_CODE (type) == COMPLEX_TYPE))
14234 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14235 const char *r_name = NULL;
14237 switch (r_code)
14239 case PLUS_EXPR:
14240 case MULT_EXPR:
14241 case MINUS_EXPR:
14242 case TRUTH_ANDIF_EXPR:
14243 case TRUTH_ORIF_EXPR:
14244 break;
14245 case MIN_EXPR:
14246 if (TREE_CODE (type) == COMPLEX_TYPE)
14247 r_name = "min";
14248 break;
14249 case MAX_EXPR:
14250 if (TREE_CODE (type) == COMPLEX_TYPE)
14251 r_name = "max";
14252 break;
14253 case BIT_AND_EXPR:
14254 r_name = "&";
14255 break;
14256 case BIT_XOR_EXPR:
14257 r_name = "^";
14258 break;
14259 case BIT_IOR_EXPR:
14260 r_name = "|";
14261 break;
14262 default:
14263 gcc_unreachable ();
14265 if (r_name)
14267 error_at (OMP_CLAUSE_LOCATION (c),
14268 "%qE has invalid type for %<reduction(%s)%>",
14269 t, r_name);
14270 remove = true;
14271 break;
14274 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14276 error_at (OMP_CLAUSE_LOCATION (c),
14277 "user defined reduction not found for %qE", t);
14278 remove = true;
14279 break;
14281 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14283 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14284 type = TYPE_MAIN_VARIANT (type);
14285 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14286 VAR_DECL, NULL_TREE, type);
14287 tree decl_placeholder = NULL_TREE;
14288 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14289 DECL_ARTIFICIAL (placeholder) = 1;
14290 DECL_IGNORED_P (placeholder) = 1;
14291 if (TREE_CODE (t) == MEM_REF)
14293 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14294 VAR_DECL, NULL_TREE, type);
14295 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14296 DECL_ARTIFICIAL (decl_placeholder) = 1;
14297 DECL_IGNORED_P (decl_placeholder) = 1;
14299 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14300 c_mark_addressable (placeholder);
14301 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14302 c_mark_addressable (decl_placeholder ? decl_placeholder
14303 : OMP_CLAUSE_DECL (c));
14304 OMP_CLAUSE_REDUCTION_MERGE (c)
14305 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14306 TREE_VEC_ELT (list, 0),
14307 TREE_VEC_ELT (list, 1),
14308 decl_placeholder ? decl_placeholder
14309 : OMP_CLAUSE_DECL (c), placeholder);
14310 OMP_CLAUSE_REDUCTION_MERGE (c)
14311 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14312 void_type_node, NULL_TREE,
14313 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14314 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14315 if (TREE_VEC_LENGTH (list) == 6)
14317 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14318 c_mark_addressable (decl_placeholder ? decl_placeholder
14319 : OMP_CLAUSE_DECL (c));
14320 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14321 c_mark_addressable (placeholder);
14322 tree init = TREE_VEC_ELT (list, 5);
14323 if (init == error_mark_node)
14324 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14325 OMP_CLAUSE_REDUCTION_INIT (c)
14326 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14327 TREE_VEC_ELT (list, 3),
14328 decl_placeholder ? decl_placeholder
14329 : OMP_CLAUSE_DECL (c), placeholder);
14330 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14332 tree v = decl_placeholder ? decl_placeholder : t;
14333 OMP_CLAUSE_REDUCTION_INIT (c)
14334 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14335 OMP_CLAUSE_REDUCTION_INIT (c));
14337 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14338 c_find_omp_placeholder_r,
14339 placeholder, NULL))
14340 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14342 else
14344 tree init;
14345 tree v = decl_placeholder ? decl_placeholder : t;
14346 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14347 init = build_constructor (TREE_TYPE (v), NULL);
14348 else
14349 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14350 OMP_CLAUSE_REDUCTION_INIT (c)
14351 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14353 OMP_CLAUSE_REDUCTION_INIT (c)
14354 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14355 void_type_node, NULL_TREE,
14356 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14357 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14359 if (TREE_CODE (t) == MEM_REF)
14361 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14362 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14363 != INTEGER_CST)
14365 sorry ("variable length element type in array "
14366 "%<reduction%> clause");
14367 remove = true;
14368 break;
14370 t = TREE_OPERAND (t, 0);
14371 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14372 t = TREE_OPERAND (t, 0);
14373 if (TREE_CODE (t) == ADDR_EXPR)
14374 t = TREE_OPERAND (t, 0);
14376 goto check_dup_generic_t;
14378 case OMP_CLAUSE_COPYPRIVATE:
14379 copyprivate_seen = true;
14380 if (nowait_clause)
14382 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14383 "%<nowait%> clause must not be used together "
14384 "with %<copyprivate%>");
14385 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14386 nowait_clause = NULL;
14388 goto check_dup_generic;
14390 case OMP_CLAUSE_COPYIN:
14391 t = OMP_CLAUSE_DECL (c);
14392 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14394 error_at (OMP_CLAUSE_LOCATION (c),
14395 "%qE must be %<threadprivate%> for %<copyin%>", t);
14396 remove = true;
14397 break;
14399 goto check_dup_generic;
14401 case OMP_CLAUSE_LINEAR:
14402 if (ort != C_ORT_OMP_DECLARE_SIMD)
14403 need_implicitly_determined = true;
14404 t = OMP_CLAUSE_DECL (c);
14405 if (ort != C_ORT_OMP_DECLARE_SIMD
14406 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
14408 error_at (OMP_CLAUSE_LOCATION (c),
14409 "modifier should not be specified in %<linear%> "
14410 "clause on %<simd%> or %<for%> constructs");
14411 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14413 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14414 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14416 error_at (OMP_CLAUSE_LOCATION (c),
14417 "linear clause applied to non-integral non-pointer "
14418 "variable with type %qT", TREE_TYPE (t));
14419 remove = true;
14420 break;
14422 if (TYPE_ATOMIC (TREE_TYPE (t)))
14424 error_at (OMP_CLAUSE_LOCATION (c),
14425 "%<_Atomic%> %qD in %<linear%> clause", t);
14426 remove = true;
14427 break;
14429 if (ort == C_ORT_OMP_DECLARE_SIMD)
14431 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14432 if (TREE_CODE (s) == PARM_DECL)
14434 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14435 /* map_head bitmap is used as uniform_head if
14436 declare_simd. */
14437 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14438 linear_variable_step_check = true;
14439 goto check_dup_generic;
14441 if (TREE_CODE (s) != INTEGER_CST)
14443 error_at (OMP_CLAUSE_LOCATION (c),
14444 "%<linear%> clause step %qE is neither constant "
14445 "nor a parameter", s);
14446 remove = true;
14447 break;
14450 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14452 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14453 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14454 OMP_CLAUSE_DECL (c), s);
14455 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14456 sizetype, fold_convert (sizetype, s),
14457 fold_convert
14458 (sizetype, OMP_CLAUSE_DECL (c)));
14459 if (s == error_mark_node)
14460 s = size_one_node;
14461 OMP_CLAUSE_LINEAR_STEP (c) = s;
14463 else
14464 OMP_CLAUSE_LINEAR_STEP (c)
14465 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14466 goto check_dup_generic;
14468 check_dup_generic:
14469 t = OMP_CLAUSE_DECL (c);
14470 check_dup_generic_t:
14471 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14473 error_at (OMP_CLAUSE_LOCATION (c),
14474 "%qE is not a variable in clause %qs", t,
14475 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14476 remove = true;
14478 else if ((ort == C_ORT_ACC
14479 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14480 || (ort == C_ORT_OMP
14481 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14482 || (OMP_CLAUSE_CODE (c)
14483 == OMP_CLAUSE_USE_DEVICE_ADDR)))
14484 || (ort == C_ORT_OMP_TARGET
14485 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
14487 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14488 && (bitmap_bit_p (&generic_head, DECL_UID (t))
14489 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
14491 error_at (OMP_CLAUSE_LOCATION (c),
14492 "%qD appears more than once in data-sharing "
14493 "clauses", t);
14494 remove = true;
14495 break;
14497 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
14498 target_in_reduction_seen = true;
14499 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14501 error_at (OMP_CLAUSE_LOCATION (c),
14502 ort == C_ORT_ACC
14503 ? "%qD appears more than once in reduction clauses"
14504 : "%qD appears more than once in data clauses",
14506 remove = true;
14508 else
14509 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14511 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14512 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14513 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
14514 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14516 error_at (OMP_CLAUSE_LOCATION (c),
14517 "%qE appears more than once in data clauses", t);
14518 remove = true;
14520 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14521 && bitmap_bit_p (&map_head, DECL_UID (t)))
14523 if (ort == C_ORT_ACC)
14524 error_at (OMP_CLAUSE_LOCATION (c),
14525 "%qD appears more than once in data clauses", t);
14526 else
14527 error_at (OMP_CLAUSE_LOCATION (c),
14528 "%qD appears both in data and map clauses", t);
14529 remove = true;
14531 else
14532 bitmap_set_bit (&generic_head, DECL_UID (t));
14533 break;
14535 case OMP_CLAUSE_FIRSTPRIVATE:
14536 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
14538 move_implicit:
14539 implicit_moved = true;
14540 /* Move firstprivate and map clauses with
14541 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
14542 clauses chain. */
14543 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
14544 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
14545 while (*pc1)
14546 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
14547 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
14549 *pc3 = *pc1;
14550 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
14551 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14553 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
14554 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
14556 *pc2 = *pc1;
14557 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
14558 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14560 else
14561 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
14562 *pc3 = NULL;
14563 *pc2 = cl2;
14564 *pc1 = cl1;
14565 continue;
14567 t = OMP_CLAUSE_DECL (c);
14568 need_complete = true;
14569 need_implicitly_determined = true;
14570 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14572 error_at (OMP_CLAUSE_LOCATION (c),
14573 "%qE is not a variable in clause %<firstprivate%>", t);
14574 remove = true;
14576 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14577 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
14578 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14579 remove = true;
14580 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14581 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14582 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14584 error_at (OMP_CLAUSE_LOCATION (c),
14585 "%qE appears more than once in data clauses", t);
14586 remove = true;
14588 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14590 if (ort == C_ORT_ACC)
14591 error_at (OMP_CLAUSE_LOCATION (c),
14592 "%qD appears more than once in data clauses", t);
14593 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14594 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
14595 /* Silently drop the clause. */;
14596 else
14597 error_at (OMP_CLAUSE_LOCATION (c),
14598 "%qD appears both in data and map clauses", t);
14599 remove = true;
14601 else
14602 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14603 break;
14605 case OMP_CLAUSE_LASTPRIVATE:
14606 t = OMP_CLAUSE_DECL (c);
14607 need_complete = true;
14608 need_implicitly_determined = true;
14609 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14611 error_at (OMP_CLAUSE_LOCATION (c),
14612 "%qE is not a variable in clause %<lastprivate%>", t);
14613 remove = true;
14615 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14616 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14618 error_at (OMP_CLAUSE_LOCATION (c),
14619 "%qE appears more than once in data clauses", t);
14620 remove = true;
14622 else
14623 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14624 break;
14626 case OMP_CLAUSE_ALIGNED:
14627 t = OMP_CLAUSE_DECL (c);
14628 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14630 error_at (OMP_CLAUSE_LOCATION (c),
14631 "%qE is not a variable in %<aligned%> clause", t);
14632 remove = true;
14634 else if (!POINTER_TYPE_P (TREE_TYPE (t))
14635 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14637 error_at (OMP_CLAUSE_LOCATION (c),
14638 "%qE in %<aligned%> clause is neither a pointer nor "
14639 "an array", t);
14640 remove = true;
14642 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14644 error_at (OMP_CLAUSE_LOCATION (c),
14645 "%<_Atomic%> %qD in %<aligned%> clause", t);
14646 remove = true;
14647 break;
14649 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14651 error_at (OMP_CLAUSE_LOCATION (c),
14652 "%qE appears more than once in %<aligned%> clauses",
14654 remove = true;
14656 else
14657 bitmap_set_bit (&aligned_head, DECL_UID (t));
14658 break;
14660 case OMP_CLAUSE_NONTEMPORAL:
14661 t = OMP_CLAUSE_DECL (c);
14662 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14664 error_at (OMP_CLAUSE_LOCATION (c),
14665 "%qE is not a variable in %<nontemporal%> clause", t);
14666 remove = true;
14668 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14670 error_at (OMP_CLAUSE_LOCATION (c),
14671 "%qE appears more than once in %<nontemporal%> "
14672 "clauses", t);
14673 remove = true;
14675 else
14676 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14677 break;
14679 case OMP_CLAUSE_ALLOCATE:
14680 t = OMP_CLAUSE_DECL (c);
14681 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14683 error_at (OMP_CLAUSE_LOCATION (c),
14684 "%qE is not a variable in %<allocate%> clause", t);
14685 remove = true;
14687 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14689 warning_at (OMP_CLAUSE_LOCATION (c), 0,
14690 "%qE appears more than once in %<allocate%> clauses",
14692 remove = true;
14694 else
14696 bitmap_set_bit (&aligned_head, DECL_UID (t));
14697 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
14698 allocate_seen = true;
14700 break;
14702 case OMP_CLAUSE_DEPEND:
14703 t = OMP_CLAUSE_DECL (c);
14704 if (t == NULL_TREE)
14706 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14707 == OMP_CLAUSE_DEPEND_SOURCE);
14708 break;
14710 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14712 gcc_assert (TREE_CODE (t) == TREE_LIST);
14713 for (; t; t = TREE_CHAIN (t))
14715 tree decl = TREE_VALUE (t);
14716 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14718 tree offset = TREE_PURPOSE (t);
14719 bool neg = wi::neg_p (wi::to_wide (offset));
14720 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14721 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14722 neg ? MINUS_EXPR : PLUS_EXPR,
14723 decl, offset);
14724 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14725 sizetype,
14726 fold_convert (sizetype, t2),
14727 fold_convert (sizetype, decl));
14728 if (t2 == error_mark_node)
14730 remove = true;
14731 break;
14733 TREE_PURPOSE (t) = t2;
14736 break;
14738 /* FALLTHRU */
14739 case OMP_CLAUSE_AFFINITY:
14740 t = OMP_CLAUSE_DECL (c);
14741 if (TREE_CODE (t) == TREE_LIST
14742 && TREE_PURPOSE (t)
14743 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14745 if (TREE_PURPOSE (t) != last_iterators)
14746 last_iterators_remove
14747 = c_omp_finish_iterators (TREE_PURPOSE (t));
14748 last_iterators = TREE_PURPOSE (t);
14749 t = TREE_VALUE (t);
14750 if (last_iterators_remove)
14751 t = error_mark_node;
14753 else
14754 last_iterators = NULL_TREE;
14755 if (TREE_CODE (t) == TREE_LIST)
14757 if (handle_omp_array_sections (c, ort))
14758 remove = true;
14759 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14760 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14762 error_at (OMP_CLAUSE_LOCATION (c),
14763 "%<depend%> clause with %<depobj%> dependence "
14764 "type on array section");
14765 remove = true;
14767 break;
14769 if (t == error_mark_node)
14770 remove = true;
14771 else if (!lvalue_p (t))
14773 error_at (OMP_CLAUSE_LOCATION (c),
14774 "%qE is not lvalue expression nor array section in "
14775 "%qs clause", t,
14776 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14777 remove = true;
14779 else if (TREE_CODE (t) == COMPONENT_REF
14780 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14782 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14783 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
14784 error_at (OMP_CLAUSE_LOCATION (c),
14785 "bit-field %qE in %qs clause", t,
14786 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14787 remove = true;
14789 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14790 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14792 if (!c_omp_depend_t_p (TREE_TYPE (t)))
14794 error_at (OMP_CLAUSE_LOCATION (c),
14795 "%qE does not have %<omp_depend_t%> type in "
14796 "%<depend%> clause with %<depobj%> dependence "
14797 "type", t);
14798 remove = true;
14801 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14802 && c_omp_depend_t_p (TREE_TYPE (t)))
14804 error_at (OMP_CLAUSE_LOCATION (c),
14805 "%qE should not have %<omp_depend_t%> type in "
14806 "%<depend%> clause with dependence type other than "
14807 "%<depobj%>", t);
14808 remove = true;
14810 if (!remove)
14812 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c), ADDR_EXPR,
14813 t, false);
14814 if (addr == error_mark_node)
14815 remove = true;
14816 else
14818 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14819 RO_UNARY_STAR);
14820 if (t == error_mark_node)
14821 remove = true;
14822 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14823 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14824 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14825 == TREE_VEC))
14826 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14827 else
14828 OMP_CLAUSE_DECL (c) = t;
14831 break;
14833 case OMP_CLAUSE_MAP:
14834 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
14835 goto move_implicit;
14836 /* FALLTHRU */
14837 case OMP_CLAUSE_TO:
14838 case OMP_CLAUSE_FROM:
14839 case OMP_CLAUSE__CACHE_:
14840 t = OMP_CLAUSE_DECL (c);
14841 if (TREE_CODE (t) == TREE_LIST)
14843 if (handle_omp_array_sections (c, ort))
14844 remove = true;
14845 else
14847 t = OMP_CLAUSE_DECL (c);
14848 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14850 error_at (OMP_CLAUSE_LOCATION (c),
14851 "array section does not have mappable type "
14852 "in %qs clause",
14853 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14854 remove = true;
14856 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14858 error_at (OMP_CLAUSE_LOCATION (c),
14859 "%<_Atomic%> %qE in %qs clause", t,
14860 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14861 remove = true;
14863 while (TREE_CODE (t) == ARRAY_REF)
14864 t = TREE_OPERAND (t, 0);
14865 if (TREE_CODE (t) == COMPONENT_REF
14866 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14868 while (TREE_CODE (t) == COMPONENT_REF)
14869 t = TREE_OPERAND (t, 0);
14870 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14871 && OMP_CLAUSE_MAP_IMPLICIT (c)
14872 && (bitmap_bit_p (&map_head, DECL_UID (t))
14873 || bitmap_bit_p (&map_field_head, DECL_UID (t))
14874 || bitmap_bit_p (&map_firstprivate_head,
14875 DECL_UID (t))))
14877 remove = true;
14878 break;
14880 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14881 break;
14882 if (bitmap_bit_p (&map_head, DECL_UID (t)))
14884 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14885 error_at (OMP_CLAUSE_LOCATION (c),
14886 "%qD appears more than once in motion "
14887 "clauses", t);
14888 else if (ort == C_ORT_ACC)
14889 error_at (OMP_CLAUSE_LOCATION (c),
14890 "%qD appears more than once in data "
14891 "clauses", t);
14892 else
14893 error_at (OMP_CLAUSE_LOCATION (c),
14894 "%qD appears more than once in map "
14895 "clauses", t);
14896 remove = true;
14898 else
14900 bitmap_set_bit (&map_head, DECL_UID (t));
14901 bitmap_set_bit (&map_field_head, DECL_UID (t));
14905 if (c_oacc_check_attachments (c))
14906 remove = true;
14907 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14908 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14909 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
14910 /* In this case, we have a single array element which is a
14911 pointer, and we already set OMP_CLAUSE_SIZE in
14912 handle_omp_array_sections above. For attach/detach clauses,
14913 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
14914 here. */
14915 OMP_CLAUSE_SIZE (c) = size_zero_node;
14916 break;
14918 if (t == error_mark_node)
14920 remove = true;
14921 break;
14923 /* OpenACC attach / detach clauses must be pointers. */
14924 if (c_oacc_check_attachments (c))
14926 remove = true;
14927 break;
14929 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14930 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14931 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
14932 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
14933 bias) to zero here, so it is not set erroneously to the pointer
14934 size later on in gimplify.c. */
14935 OMP_CLAUSE_SIZE (c) = size_zero_node;
14936 if (TREE_CODE (t) == COMPONENT_REF
14937 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
14939 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
14941 error_at (OMP_CLAUSE_LOCATION (c),
14942 "bit-field %qE in %qs clause",
14943 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14944 remove = true;
14946 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14948 error_at (OMP_CLAUSE_LOCATION (c),
14949 "%qE does not have a mappable type in %qs clause",
14950 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14951 remove = true;
14953 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14955 error_at (OMP_CLAUSE_LOCATION (c),
14956 "%<_Atomic%> %qE in %qs clause", t,
14957 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14958 remove = true;
14960 while (TREE_CODE (t) == COMPONENT_REF)
14962 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
14963 == UNION_TYPE)
14965 error_at (OMP_CLAUSE_LOCATION (c),
14966 "%qE is a member of a union", t);
14967 remove = true;
14968 break;
14970 t = TREE_OPERAND (t, 0);
14971 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
14973 if (maybe_ne (mem_ref_offset (t), 0))
14974 error_at (OMP_CLAUSE_LOCATION (c),
14975 "cannot dereference %qE in %qs clause", t,
14976 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14977 else
14978 t = TREE_OPERAND (t, 0);
14981 if (remove)
14982 break;
14983 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14985 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
14986 || (ort != C_ORT_ACC
14987 && bitmap_bit_p (&map_head, DECL_UID (t))))
14988 break;
14991 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14993 error_at (OMP_CLAUSE_LOCATION (c),
14994 "%qE is not a variable in %qs clause", t,
14995 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14996 remove = true;
14998 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15000 error_at (OMP_CLAUSE_LOCATION (c),
15001 "%qD is threadprivate variable in %qs clause", t,
15002 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15003 remove = true;
15005 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15006 || (OMP_CLAUSE_MAP_KIND (c)
15007 != GOMP_MAP_FIRSTPRIVATE_POINTER))
15008 && !c_mark_addressable (t))
15009 remove = true;
15010 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15011 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
15012 || (OMP_CLAUSE_MAP_KIND (c)
15013 == GOMP_MAP_FIRSTPRIVATE_POINTER)
15014 || (OMP_CLAUSE_MAP_KIND (c)
15015 == GOMP_MAP_FORCE_DEVICEPTR)))
15016 && t == OMP_CLAUSE_DECL (c)
15017 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
15019 error_at (OMP_CLAUSE_LOCATION (c),
15020 "%qD does not have a mappable type in %qs clause", t,
15021 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15022 remove = true;
15024 else if (TREE_TYPE (t) == error_mark_node)
15025 remove = true;
15026 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15028 error_at (OMP_CLAUSE_LOCATION (c),
15029 "%<_Atomic%> %qE in %qs clause", t,
15030 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15031 remove = true;
15033 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15034 && OMP_CLAUSE_MAP_IMPLICIT (c)
15035 && (bitmap_bit_p (&map_head, DECL_UID (t))
15036 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15037 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))))
15038 remove = true;
15039 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15040 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
15042 if (bitmap_bit_p (&generic_head, DECL_UID (t))
15043 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15044 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15046 error_at (OMP_CLAUSE_LOCATION (c),
15047 "%qD appears more than once in data clauses", t);
15048 remove = true;
15050 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
15052 if (ort == C_ORT_ACC)
15053 error_at (OMP_CLAUSE_LOCATION (c),
15054 "%qD appears more than once in data clauses", t);
15055 else
15056 error_at (OMP_CLAUSE_LOCATION (c),
15057 "%qD appears both in data and map clauses", t);
15058 remove = true;
15060 else
15061 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
15063 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15064 && (ort == C_ORT_ACC
15065 || !bitmap_bit_p (&map_field_head, DECL_UID (t))))
15067 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15068 error_at (OMP_CLAUSE_LOCATION (c),
15069 "%qD appears more than once in motion clauses", t);
15070 else if (ort == C_ORT_ACC)
15071 error_at (OMP_CLAUSE_LOCATION (c),
15072 "%qD appears more than once in data clauses", t);
15073 else
15074 error_at (OMP_CLAUSE_LOCATION (c),
15075 "%qD appears more than once in map clauses", t);
15076 remove = true;
15078 else if (ort == C_ORT_ACC
15079 && bitmap_bit_p (&generic_head, DECL_UID (t)))
15081 error_at (OMP_CLAUSE_LOCATION (c),
15082 "%qD appears more than once in data clauses", t);
15083 remove = true;
15085 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
15087 if (ort == C_ORT_ACC)
15088 error_at (OMP_CLAUSE_LOCATION (c),
15089 "%qD appears more than once in data clauses", t);
15090 else
15091 error_at (OMP_CLAUSE_LOCATION (c),
15092 "%qD appears both in data and map clauses", t);
15093 remove = true;
15095 else
15097 bitmap_set_bit (&map_head, DECL_UID (t));
15098 if (t != OMP_CLAUSE_DECL (c)
15099 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
15100 bitmap_set_bit (&map_field_head, DECL_UID (t));
15102 break;
15104 case OMP_CLAUSE_TO_DECLARE:
15105 case OMP_CLAUSE_LINK:
15106 t = OMP_CLAUSE_DECL (c);
15107 if (TREE_CODE (t) == FUNCTION_DECL
15108 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
15110 else if (!VAR_P (t))
15112 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
15113 error_at (OMP_CLAUSE_LOCATION (c),
15114 "%qE is neither a variable nor a function name in "
15115 "clause %qs", t,
15116 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15117 else
15118 error_at (OMP_CLAUSE_LOCATION (c),
15119 "%qE is not a variable in clause %qs", t,
15120 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15121 remove = true;
15123 else if (DECL_THREAD_LOCAL_P (t))
15125 error_at (OMP_CLAUSE_LOCATION (c),
15126 "%qD is threadprivate variable in %qs clause", t,
15127 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15128 remove = true;
15130 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
15132 error_at (OMP_CLAUSE_LOCATION (c),
15133 "%qD does not have a mappable type in %qs clause", t,
15134 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15135 remove = true;
15137 if (remove)
15138 break;
15139 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15141 error_at (OMP_CLAUSE_LOCATION (c),
15142 "%qE appears more than once on the same "
15143 "%<declare target%> directive", t);
15144 remove = true;
15146 else
15147 bitmap_set_bit (&generic_head, DECL_UID (t));
15148 break;
15150 case OMP_CLAUSE_UNIFORM:
15151 t = OMP_CLAUSE_DECL (c);
15152 if (TREE_CODE (t) != PARM_DECL)
15154 if (DECL_P (t))
15155 error_at (OMP_CLAUSE_LOCATION (c),
15156 "%qD is not an argument in %<uniform%> clause", t);
15157 else
15158 error_at (OMP_CLAUSE_LOCATION (c),
15159 "%qE is not an argument in %<uniform%> clause", t);
15160 remove = true;
15161 break;
15163 /* map_head bitmap is used as uniform_head if declare_simd. */
15164 bitmap_set_bit (&map_head, DECL_UID (t));
15165 goto check_dup_generic;
15167 case OMP_CLAUSE_IS_DEVICE_PTR:
15168 case OMP_CLAUSE_USE_DEVICE_PTR:
15169 t = OMP_CLAUSE_DECL (c);
15170 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15172 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15173 && ort != C_ORT_ACC)
15175 error_at (OMP_CLAUSE_LOCATION (c),
15176 "%qs variable is not a pointer",
15177 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15178 remove = true;
15180 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15182 error_at (OMP_CLAUSE_LOCATION (c),
15183 "%qs variable is neither a pointer nor an array",
15184 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15185 remove = true;
15188 goto check_dup_generic;
15190 case OMP_CLAUSE_USE_DEVICE_ADDR:
15191 t = OMP_CLAUSE_DECL (c);
15192 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15193 c_mark_addressable (t);
15194 goto check_dup_generic;
15196 case OMP_CLAUSE_NOWAIT:
15197 if (copyprivate_seen)
15199 error_at (OMP_CLAUSE_LOCATION (c),
15200 "%<nowait%> clause must not be used together "
15201 "with %<copyprivate%>");
15202 remove = true;
15203 break;
15205 nowait_clause = pc;
15206 pc = &OMP_CLAUSE_CHAIN (c);
15207 continue;
15209 case OMP_CLAUSE_ORDER:
15210 if (ordered_clause)
15212 error_at (OMP_CLAUSE_LOCATION (c),
15213 "%<order%> clause must not be used together "
15214 "with %<ordered%>");
15215 remove = true;
15216 break;
15218 else if (order_clause)
15220 /* Silently remove duplicates. */
15221 remove = true;
15222 break;
15224 order_clause = pc;
15225 pc = &OMP_CLAUSE_CHAIN (c);
15226 continue;
15228 case OMP_CLAUSE_DETACH:
15229 t = OMP_CLAUSE_DECL (c);
15230 if (detach_seen)
15232 error_at (OMP_CLAUSE_LOCATION (c),
15233 "too many %qs clauses on a task construct",
15234 "detach");
15235 remove = true;
15236 break;
15238 detach_seen = pc;
15239 pc = &OMP_CLAUSE_CHAIN (c);
15240 c_mark_addressable (t);
15241 continue;
15243 case OMP_CLAUSE_IF:
15244 case OMP_CLAUSE_NUM_THREADS:
15245 case OMP_CLAUSE_NUM_TEAMS:
15246 case OMP_CLAUSE_THREAD_LIMIT:
15247 case OMP_CLAUSE_DEFAULT:
15248 case OMP_CLAUSE_UNTIED:
15249 case OMP_CLAUSE_COLLAPSE:
15250 case OMP_CLAUSE_FINAL:
15251 case OMP_CLAUSE_DEVICE:
15252 case OMP_CLAUSE_DIST_SCHEDULE:
15253 case OMP_CLAUSE_PARALLEL:
15254 case OMP_CLAUSE_FOR:
15255 case OMP_CLAUSE_SECTIONS:
15256 case OMP_CLAUSE_TASKGROUP:
15257 case OMP_CLAUSE_PROC_BIND:
15258 case OMP_CLAUSE_DEVICE_TYPE:
15259 case OMP_CLAUSE_PRIORITY:
15260 case OMP_CLAUSE_GRAINSIZE:
15261 case OMP_CLAUSE_NUM_TASKS:
15262 case OMP_CLAUSE_THREADS:
15263 case OMP_CLAUSE_SIMD:
15264 case OMP_CLAUSE_HINT:
15265 case OMP_CLAUSE_FILTER:
15266 case OMP_CLAUSE_DEFAULTMAP:
15267 case OMP_CLAUSE_BIND:
15268 case OMP_CLAUSE_NUM_GANGS:
15269 case OMP_CLAUSE_NUM_WORKERS:
15270 case OMP_CLAUSE_VECTOR_LENGTH:
15271 case OMP_CLAUSE_ASYNC:
15272 case OMP_CLAUSE_WAIT:
15273 case OMP_CLAUSE_AUTO:
15274 case OMP_CLAUSE_INDEPENDENT:
15275 case OMP_CLAUSE_SEQ:
15276 case OMP_CLAUSE_GANG:
15277 case OMP_CLAUSE_WORKER:
15278 case OMP_CLAUSE_VECTOR:
15279 case OMP_CLAUSE_TILE:
15280 case OMP_CLAUSE_IF_PRESENT:
15281 case OMP_CLAUSE_FINALIZE:
15282 case OMP_CLAUSE_NOHOST:
15283 pc = &OMP_CLAUSE_CHAIN (c);
15284 continue;
15286 case OMP_CLAUSE_MERGEABLE:
15287 mergeable_seen = true;
15288 pc = &OMP_CLAUSE_CHAIN (c);
15289 continue;
15291 case OMP_CLAUSE_NOGROUP:
15292 nogroup_seen = pc;
15293 pc = &OMP_CLAUSE_CHAIN (c);
15294 continue;
15296 case OMP_CLAUSE_SCHEDULE:
15297 schedule_clause = c;
15298 pc = &OMP_CLAUSE_CHAIN (c);
15299 continue;
15301 case OMP_CLAUSE_ORDERED:
15302 ordered_clause = c;
15303 if (order_clause)
15305 error_at (OMP_CLAUSE_LOCATION (*order_clause),
15306 "%<order%> clause must not be used together "
15307 "with %<ordered%>");
15308 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
15309 order_clause = NULL;
15311 pc = &OMP_CLAUSE_CHAIN (c);
15312 continue;
15314 case OMP_CLAUSE_SAFELEN:
15315 safelen = c;
15316 pc = &OMP_CLAUSE_CHAIN (c);
15317 continue;
15318 case OMP_CLAUSE_SIMDLEN:
15319 simdlen = c;
15320 pc = &OMP_CLAUSE_CHAIN (c);
15321 continue;
15323 case OMP_CLAUSE_INBRANCH:
15324 case OMP_CLAUSE_NOTINBRANCH:
15325 if (branch_seen)
15327 error_at (OMP_CLAUSE_LOCATION (c),
15328 "%<inbranch%> clause is incompatible with "
15329 "%<notinbranch%>");
15330 remove = true;
15331 break;
15333 branch_seen = true;
15334 pc = &OMP_CLAUSE_CHAIN (c);
15335 continue;
15337 case OMP_CLAUSE_INCLUSIVE:
15338 case OMP_CLAUSE_EXCLUSIVE:
15339 need_complete = true;
15340 need_implicitly_determined = true;
15341 t = OMP_CLAUSE_DECL (c);
15342 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15344 error_at (OMP_CLAUSE_LOCATION (c),
15345 "%qE is not a variable in clause %qs", t,
15346 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15347 remove = true;
15349 break;
15351 default:
15352 gcc_unreachable ();
15355 if (!remove)
15357 t = OMP_CLAUSE_DECL (c);
15359 if (need_complete)
15361 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15362 if (t == error_mark_node)
15363 remove = true;
15366 if (need_implicitly_determined)
15368 const char *share_name = NULL;
15370 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15371 share_name = "threadprivate";
15372 else switch (c_omp_predetermined_sharing (t))
15374 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15375 break;
15376 case OMP_CLAUSE_DEFAULT_SHARED:
15377 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15378 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15379 && c_omp_predefined_variable (t))
15380 /* The __func__ variable and similar function-local
15381 predefined variables may be listed in a shared or
15382 firstprivate clause. */
15383 break;
15384 share_name = "shared";
15385 break;
15386 case OMP_CLAUSE_DEFAULT_PRIVATE:
15387 share_name = "private";
15388 break;
15389 default:
15390 gcc_unreachable ();
15392 if (share_name)
15394 error_at (OMP_CLAUSE_LOCATION (c),
15395 "%qE is predetermined %qs for %qs",
15396 t, share_name,
15397 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15398 remove = true;
15400 else if (TREE_READONLY (t)
15401 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15402 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15404 error_at (OMP_CLAUSE_LOCATION (c),
15405 "%<const%> qualified %qE may appear only in "
15406 "%<shared%> or %<firstprivate%> clauses", t);
15407 remove = true;
15412 if (remove)
15413 *pc = OMP_CLAUSE_CHAIN (c);
15414 else
15415 pc = &OMP_CLAUSE_CHAIN (c);
15418 if (simdlen
15419 && safelen
15420 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
15421 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
15423 error_at (OMP_CLAUSE_LOCATION (simdlen),
15424 "%<simdlen%> clause value is bigger than "
15425 "%<safelen%> clause value");
15426 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
15427 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
15430 if (ordered_clause
15431 && schedule_clause
15432 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15433 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
15435 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15436 "%<nonmonotonic%> schedule modifier specified together "
15437 "with %<ordered%> clause");
15438 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15439 = (enum omp_clause_schedule_kind)
15440 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15441 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
15444 if (reduction_seen < 0 && ordered_clause)
15446 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
15447 "%qs clause specified together with %<inscan%> "
15448 "%<reduction%> clause", "ordered");
15449 reduction_seen = -2;
15452 if (reduction_seen < 0 && schedule_clause)
15454 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15455 "%qs clause specified together with %<inscan%> "
15456 "%<reduction%> clause", "schedule");
15457 reduction_seen = -2;
15460 if (linear_variable_step_check
15461 || reduction_seen == -2
15462 || allocate_seen
15463 || target_in_reduction_seen)
15464 for (pc = &clauses, c = clauses; c ; c = *pc)
15466 bool remove = false;
15467 if (allocate_seen)
15468 switch (OMP_CLAUSE_CODE (c))
15470 case OMP_CLAUSE_REDUCTION:
15471 case OMP_CLAUSE_IN_REDUCTION:
15472 case OMP_CLAUSE_TASK_REDUCTION:
15473 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
15475 t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
15476 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15477 t = TREE_OPERAND (t, 0);
15478 if (TREE_CODE (t) == ADDR_EXPR
15479 || TREE_CODE (t) == INDIRECT_REF)
15480 t = TREE_OPERAND (t, 0);
15481 if (DECL_P (t))
15482 bitmap_clear_bit (&aligned_head, DECL_UID (t));
15483 break;
15485 /* FALLTHRU */
15486 case OMP_CLAUSE_PRIVATE:
15487 case OMP_CLAUSE_FIRSTPRIVATE:
15488 case OMP_CLAUSE_LASTPRIVATE:
15489 case OMP_CLAUSE_LINEAR:
15490 if (DECL_P (OMP_CLAUSE_DECL (c)))
15491 bitmap_clear_bit (&aligned_head,
15492 DECL_UID (OMP_CLAUSE_DECL (c)));
15493 break;
15494 default:
15495 break;
15497 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15498 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
15499 && !bitmap_bit_p (&map_head,
15500 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
15502 error_at (OMP_CLAUSE_LOCATION (c),
15503 "%<linear%> clause step is a parameter %qD not "
15504 "specified in %<uniform%> clause",
15505 OMP_CLAUSE_LINEAR_STEP (c));
15506 remove = true;
15508 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15509 && reduction_seen == -2)
15510 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
15511 if (target_in_reduction_seen
15512 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
15514 tree t = OMP_CLAUSE_DECL (c);
15515 while (handled_component_p (t)
15516 || TREE_CODE (t) == INDIRECT_REF
15517 || TREE_CODE (t) == ADDR_EXPR
15518 || TREE_CODE (t) == MEM_REF
15519 || TREE_CODE (t) == NON_LVALUE_EXPR)
15520 t = TREE_OPERAND (t, 0);
15521 if (DECL_P (t)
15522 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15523 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
15526 if (remove)
15527 *pc = OMP_CLAUSE_CHAIN (c);
15528 else
15529 pc = &OMP_CLAUSE_CHAIN (c);
15532 if (allocate_seen)
15533 for (pc = &clauses, c = clauses; c ; c = *pc)
15535 bool remove = false;
15536 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
15537 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
15538 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
15540 error_at (OMP_CLAUSE_LOCATION (c),
15541 "%qD specified in %<allocate%> clause but not in "
15542 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
15543 remove = true;
15545 if (remove)
15546 *pc = OMP_CLAUSE_CHAIN (c);
15547 else
15548 pc = &OMP_CLAUSE_CHAIN (c);
15551 if (nogroup_seen && reduction_seen)
15553 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
15554 "%<nogroup%> clause must not be used together with "
15555 "%<reduction%> clause");
15556 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
15559 if (detach_seen)
15561 if (mergeable_seen)
15563 error_at (OMP_CLAUSE_LOCATION (*detach_seen),
15564 "%<detach%> clause must not be used together with "
15565 "%<mergeable%> clause");
15566 *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
15568 else
15570 tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
15572 for (pc = &clauses, c = clauses; c ; c = *pc)
15574 bool remove = false;
15575 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15576 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15577 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
15578 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
15579 && OMP_CLAUSE_DECL (c) == detach_decl)
15581 error_at (OMP_CLAUSE_LOCATION (c),
15582 "the event handle of a %<detach%> clause "
15583 "should not be in a data-sharing clause");
15584 remove = true;
15586 if (remove)
15587 *pc = OMP_CLAUSE_CHAIN (c);
15588 else
15589 pc = &OMP_CLAUSE_CHAIN (c);
15594 bitmap_obstack_release (NULL);
15595 return clauses;
15598 /* Return code to initialize DST with a copy constructor from SRC.
15599 C doesn't have copy constructors nor assignment operators, only for
15600 _Atomic vars we need to perform __atomic_load from src into a temporary
15601 followed by __atomic_store of the temporary to dst. */
15603 tree
15604 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
15606 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
15607 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
15609 location_t loc = OMP_CLAUSE_LOCATION (clause);
15610 tree type = TREE_TYPE (dst);
15611 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
15612 tree tmp = create_tmp_var (nonatomic_type);
15613 tree tmp_addr = build_fold_addr_expr (tmp);
15614 TREE_ADDRESSABLE (tmp) = 1;
15615 suppress_warning (tmp);
15616 tree src_addr = build_fold_addr_expr (src);
15617 tree dst_addr = build_fold_addr_expr (dst);
15618 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
15619 vec<tree, va_gc> *params;
15620 /* Expansion of a generic atomic load may require an addition
15621 element, so allocate enough to prevent a resize. */
15622 vec_alloc (params, 4);
15624 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
15625 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
15626 params->quick_push (src_addr);
15627 params->quick_push (tmp_addr);
15628 params->quick_push (seq_cst);
15629 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15631 vec_alloc (params, 4);
15633 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
15634 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
15635 params->quick_push (dst_addr);
15636 params->quick_push (tmp_addr);
15637 params->quick_push (seq_cst);
15638 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15639 return build2 (COMPOUND_EXPR, void_type_node, load, store);
15642 /* Create a transaction node. */
15644 tree
15645 c_finish_transaction (location_t loc, tree block, int flags)
15647 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
15648 if (flags & TM_STMT_ATTR_OUTER)
15649 TRANSACTION_EXPR_OUTER (stmt) = 1;
15650 if (flags & TM_STMT_ATTR_RELAXED)
15651 TRANSACTION_EXPR_RELAXED (stmt) = 1;
15652 return add_stmt (stmt);
15655 /* Make a variant type in the proper way for C/C++, propagating qualifiers
15656 down to the element type of an array. If ORIG_QUAL_TYPE is not
15657 NULL, then it should be used as the qualified type
15658 ORIG_QUAL_INDIRECT levels down in array type derivation (to
15659 preserve information about the typedef name from which an array
15660 type was derived). */
15662 tree
15663 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
15664 size_t orig_qual_indirect)
15666 if (type == error_mark_node)
15667 return type;
15669 if (TREE_CODE (type) == ARRAY_TYPE)
15671 tree t;
15672 tree element_type = c_build_qualified_type (TREE_TYPE (type),
15673 type_quals, orig_qual_type,
15674 orig_qual_indirect - 1);
15676 /* See if we already have an identically qualified type. */
15677 if (orig_qual_type && orig_qual_indirect == 0)
15678 t = orig_qual_type;
15679 else
15680 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
15682 if (TYPE_QUALS (strip_array_types (t)) == type_quals
15683 && TYPE_NAME (t) == TYPE_NAME (type)
15684 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
15685 && attribute_list_equal (TYPE_ATTRIBUTES (t),
15686 TYPE_ATTRIBUTES (type)))
15687 break;
15689 if (!t)
15691 tree domain = TYPE_DOMAIN (type);
15693 t = build_variant_type_copy (type);
15694 TREE_TYPE (t) = element_type;
15696 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
15697 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
15698 SET_TYPE_STRUCTURAL_EQUALITY (t);
15699 else if (TYPE_CANONICAL (element_type) != element_type
15700 || (domain && TYPE_CANONICAL (domain) != domain))
15702 tree unqualified_canon
15703 = build_array_type (TYPE_CANONICAL (element_type),
15704 domain? TYPE_CANONICAL (domain)
15705 : NULL_TREE);
15706 if (TYPE_REVERSE_STORAGE_ORDER (type))
15708 unqualified_canon
15709 = build_distinct_type_copy (unqualified_canon);
15710 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
15712 TYPE_CANONICAL (t)
15713 = c_build_qualified_type (unqualified_canon, type_quals);
15715 else
15716 TYPE_CANONICAL (t) = t;
15718 return t;
15721 /* A restrict-qualified pointer type must be a pointer to object or
15722 incomplete type. Note that the use of POINTER_TYPE_P also allows
15723 REFERENCE_TYPEs, which is appropriate for C++. */
15724 if ((type_quals & TYPE_QUAL_RESTRICT)
15725 && (!POINTER_TYPE_P (type)
15726 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
15728 error ("invalid use of %<restrict%>");
15729 type_quals &= ~TYPE_QUAL_RESTRICT;
15732 tree var_type = (orig_qual_type && orig_qual_indirect == 0
15733 ? orig_qual_type
15734 : build_qualified_type (type, type_quals));
15735 /* A variant type does not inherit the list of incomplete vars from the
15736 type main variant. */
15737 if ((RECORD_OR_UNION_TYPE_P (var_type)
15738 || TREE_CODE (var_type) == ENUMERAL_TYPE)
15739 && TYPE_MAIN_VARIANT (var_type) != var_type)
15740 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
15741 return var_type;
15744 /* Build a VA_ARG_EXPR for the C parser. */
15746 tree
15747 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
15749 if (error_operand_p (type))
15750 return error_mark_node;
15751 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15752 order because it takes the address of the expression. */
15753 else if (handled_component_p (expr)
15754 && reverse_storage_order_for_component_p (expr))
15756 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
15757 return error_mark_node;
15759 else if (!COMPLETE_TYPE_P (type))
15761 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
15762 "type %qT", type);
15763 return error_mark_node;
15765 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
15766 warning_at (loc2, OPT_Wc___compat,
15767 "C++ requires promoted type, not enum type, in %<va_arg%>");
15768 return build_va_arg (loc2, expr, type);
15771 /* Return truthvalue of whether T1 is the same tree structure as T2.
15772 Return 1 if they are the same. Return false if they are different. */
15774 bool
15775 c_tree_equal (tree t1, tree t2)
15777 enum tree_code code1, code2;
15779 if (t1 == t2)
15780 return true;
15781 if (!t1 || !t2)
15782 return false;
15784 for (code1 = TREE_CODE (t1);
15785 CONVERT_EXPR_CODE_P (code1)
15786 || code1 == NON_LVALUE_EXPR;
15787 code1 = TREE_CODE (t1))
15788 t1 = TREE_OPERAND (t1, 0);
15789 for (code2 = TREE_CODE (t2);
15790 CONVERT_EXPR_CODE_P (code2)
15791 || code2 == NON_LVALUE_EXPR;
15792 code2 = TREE_CODE (t2))
15793 t2 = TREE_OPERAND (t2, 0);
15795 /* They might have become equal now. */
15796 if (t1 == t2)
15797 return true;
15799 if (code1 != code2)
15800 return false;
15802 switch (code1)
15804 case INTEGER_CST:
15805 return wi::to_wide (t1) == wi::to_wide (t2);
15807 case REAL_CST:
15808 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
15810 case STRING_CST:
15811 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
15812 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
15813 TREE_STRING_LENGTH (t1));
15815 case FIXED_CST:
15816 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
15817 TREE_FIXED_CST (t2));
15819 case COMPLEX_CST:
15820 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
15821 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
15823 case VECTOR_CST:
15824 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
15826 case CONSTRUCTOR:
15827 /* We need to do this when determining whether or not two
15828 non-type pointer to member function template arguments
15829 are the same. */
15830 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
15831 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
15832 return false;
15834 tree field, value;
15835 unsigned int i;
15836 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
15838 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
15839 if (!c_tree_equal (field, elt2->index)
15840 || !c_tree_equal (value, elt2->value))
15841 return false;
15844 return true;
15846 case TREE_LIST:
15847 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
15848 return false;
15849 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
15850 return false;
15851 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
15853 case SAVE_EXPR:
15854 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15856 case CALL_EXPR:
15858 tree arg1, arg2;
15859 call_expr_arg_iterator iter1, iter2;
15860 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
15861 return false;
15862 for (arg1 = first_call_expr_arg (t1, &iter1),
15863 arg2 = first_call_expr_arg (t2, &iter2);
15864 arg1 && arg2;
15865 arg1 = next_call_expr_arg (&iter1),
15866 arg2 = next_call_expr_arg (&iter2))
15867 if (!c_tree_equal (arg1, arg2))
15868 return false;
15869 if (arg1 || arg2)
15870 return false;
15871 return true;
15874 case TARGET_EXPR:
15876 tree o1 = TREE_OPERAND (t1, 0);
15877 tree o2 = TREE_OPERAND (t2, 0);
15879 /* Special case: if either target is an unallocated VAR_DECL,
15880 it means that it's going to be unified with whatever the
15881 TARGET_EXPR is really supposed to initialize, so treat it
15882 as being equivalent to anything. */
15883 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
15884 && !DECL_RTL_SET_P (o1))
15885 /*Nop*/;
15886 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
15887 && !DECL_RTL_SET_P (o2))
15888 /*Nop*/;
15889 else if (!c_tree_equal (o1, o2))
15890 return false;
15892 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
15895 case COMPONENT_REF:
15896 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
15897 return false;
15898 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15900 case PARM_DECL:
15901 case VAR_DECL:
15902 case CONST_DECL:
15903 case FIELD_DECL:
15904 case FUNCTION_DECL:
15905 case IDENTIFIER_NODE:
15906 case SSA_NAME:
15907 return false;
15909 case TREE_VEC:
15911 unsigned ix;
15912 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
15913 return false;
15914 for (ix = TREE_VEC_LENGTH (t1); ix--;)
15915 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
15916 TREE_VEC_ELT (t2, ix)))
15917 return false;
15918 return true;
15921 default:
15922 break;
15925 switch (TREE_CODE_CLASS (code1))
15927 case tcc_unary:
15928 case tcc_binary:
15929 case tcc_comparison:
15930 case tcc_expression:
15931 case tcc_vl_exp:
15932 case tcc_reference:
15933 case tcc_statement:
15935 int i, n = TREE_OPERAND_LENGTH (t1);
15937 switch (code1)
15939 case PREINCREMENT_EXPR:
15940 case PREDECREMENT_EXPR:
15941 case POSTINCREMENT_EXPR:
15942 case POSTDECREMENT_EXPR:
15943 n = 1;
15944 break;
15945 case ARRAY_REF:
15946 n = 2;
15947 break;
15948 default:
15949 break;
15952 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
15953 && n != TREE_OPERAND_LENGTH (t2))
15954 return false;
15956 for (i = 0; i < n; ++i)
15957 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
15958 return false;
15960 return true;
15963 case tcc_type:
15964 return comptypes (t1, t2);
15965 default:
15966 gcc_unreachable ();
15968 /* We can get here with --disable-checking. */
15969 return false;
15972 /* Returns true when the function declaration FNDECL is implicit,
15973 introduced as a result of a call to an otherwise undeclared
15974 function, and false otherwise. */
15976 bool
15977 c_decl_implicit (const_tree fndecl)
15979 return C_DECL_IMPLICIT (fndecl);