Daily bump.
[official-gcc.git] / gcc / c / c-typeck.c
blobb472e448011ef11878763b0294cafd1d7f6e6da8
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 behavior");
3945 if (TREE_OVERFLOW_P (result.value)
3946 && !TREE_OVERFLOW_P (arg1.value)
3947 && !TREE_OVERFLOW_P (arg2.value))
3948 overflow_warning (location, result.value);
3950 /* Warn about comparisons of different enum types. */
3951 if (warn_enum_compare
3952 && TREE_CODE_CLASS (code) == tcc_comparison
3953 && TREE_CODE (type1) == ENUMERAL_TYPE
3954 && TREE_CODE (type2) == ENUMERAL_TYPE
3955 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3956 warning_at (location, OPT_Wenum_compare,
3957 "comparison between %qT and %qT",
3958 type1, type2);
3960 return result;
3963 /* Return a tree for the difference of pointers OP0 and OP1.
3964 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3965 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3967 static tree
3968 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3970 tree restype = ptrdiff_type_node;
3971 tree result, inttype;
3973 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3974 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3975 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3976 tree orig_op0 = op0;
3977 tree orig_op1 = op1;
3979 /* If the operands point into different address spaces, we need to
3980 explicitly convert them to pointers into the common address space
3981 before we can subtract the numerical address values. */
3982 if (as0 != as1)
3984 addr_space_t as_common;
3985 tree common_type;
3987 /* Determine the common superset address space. This is guaranteed
3988 to exist because the caller verified that comp_target_types
3989 returned non-zero. */
3990 if (!addr_space_superset (as0, as1, &as_common))
3991 gcc_unreachable ();
3993 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3994 op0 = convert (common_type, op0);
3995 op1 = convert (common_type, op1);
3998 /* Determine integer type result of the subtraction. This will usually
3999 be the same as the result type (ptrdiff_t), but may need to be a wider
4000 type if pointers for the address space are wider than ptrdiff_t. */
4001 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
4002 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
4003 else
4004 inttype = restype;
4006 if (TREE_CODE (target_type) == VOID_TYPE)
4007 pedwarn (loc, OPT_Wpointer_arith,
4008 "pointer of type %<void *%> used in subtraction");
4009 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4010 pedwarn (loc, OPT_Wpointer_arith,
4011 "pointer to a function used in subtraction");
4013 if (current_function_decl != NULL_TREE
4014 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
4016 op0 = save_expr (op0);
4017 op1 = save_expr (op1);
4019 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
4020 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
4023 /* First do the subtraction, then build the divide operator
4024 and only convert at the very end.
4025 Do not do default conversions in case restype is a short type. */
4027 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4028 pointers. If some platform cannot provide that, or has a larger
4029 ptrdiff_type to support differences larger than half the address
4030 space, cast the pointers to some larger integer type and do the
4031 computations in that type. */
4032 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4033 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4034 convert (inttype, op1), false);
4035 else
4037 /* Cast away qualifiers. */
4038 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4039 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4040 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4043 /* This generates an error if op1 is pointer to incomplete type. */
4044 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4045 error_at (loc, "arithmetic on pointer to an incomplete type");
4046 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4047 TREE_TYPE (TREE_TYPE (orig_op0))))
4048 verify_type_context (loc, TCTX_POINTER_ARITH,
4049 TREE_TYPE (TREE_TYPE (orig_op1)));
4051 op1 = c_size_in_bytes (target_type);
4053 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4054 error_at (loc, "arithmetic on pointer to an empty aggregate");
4056 /* Divide by the size, in easiest possible way. */
4057 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4058 op0, convert (inttype, op1));
4060 /* Convert to final result type if necessary. */
4061 return convert (restype, result);
4064 /* Expand atomic compound assignments into an appropriate sequence as
4065 specified by the C11 standard section 6.5.16.2.
4067 _Atomic T1 E1
4068 T2 E2
4069 E1 op= E2
4071 This sequence is used for all types for which these operations are
4072 supported.
4074 In addition, built-in versions of the 'fe' prefixed routines may
4075 need to be invoked for floating point (real, complex or vector) when
4076 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4078 T1 newval;
4079 T1 old;
4080 T1 *addr
4081 T2 val
4082 fenv_t fenv
4084 addr = &E1;
4085 val = (E2);
4086 __atomic_load (addr, &old, SEQ_CST);
4087 feholdexcept (&fenv);
4088 loop:
4089 newval = old op val;
4090 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4091 SEQ_CST))
4092 goto done;
4093 feclearexcept (FE_ALL_EXCEPT);
4094 goto loop:
4095 done:
4096 feupdateenv (&fenv);
4098 The compiler will issue the __atomic_fetch_* built-in when possible,
4099 otherwise it will generate the generic form of the atomic operations.
4100 This requires temp(s) and has their address taken. The atomic processing
4101 is smart enough to figure out when the size of an object can utilize
4102 a lock-free version, and convert the built-in call to the appropriate
4103 lock-free routine. The optimizers will then dispose of any temps that
4104 are no longer required, and lock-free implementations are utilized as
4105 long as there is target support for the required size.
4107 If the operator is NOP_EXPR, then this is a simple assignment, and
4108 an __atomic_store is issued to perform the assignment rather than
4109 the above loop. */
4111 /* Build an atomic assignment at LOC, expanding into the proper
4112 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4113 the result of the operation, unless RETURN_OLD_P, in which case
4114 return the old value of LHS (this is only for postincrement and
4115 postdecrement). */
4117 static tree
4118 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4119 tree rhs, bool return_old_p)
4121 tree fndecl, func_call;
4122 vec<tree, va_gc> *params;
4123 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4124 tree old, old_addr;
4125 tree compound_stmt = NULL_TREE;
4126 tree stmt, goto_stmt;
4127 tree loop_label, loop_decl, done_label, done_decl;
4129 tree lhs_type = TREE_TYPE (lhs);
4130 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4131 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4132 tree rhs_semantic_type = TREE_TYPE (rhs);
4133 tree nonatomic_rhs_semantic_type;
4134 tree rhs_type;
4136 gcc_assert (TYPE_ATOMIC (lhs_type));
4138 if (return_old_p)
4139 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4141 /* Allocate enough vector items for a compare_exchange. */
4142 vec_alloc (params, 6);
4144 /* Create a compound statement to hold the sequence of statements
4145 with a loop. */
4146 if (modifycode != NOP_EXPR)
4148 compound_stmt = c_begin_compound_stmt (false);
4150 /* For consistency with build_modify_expr on non-_Atomic,
4151 mark the lhs as read. Also, it would be very hard to match
4152 such expressions in mark_exp_read. */
4153 mark_exp_read (lhs);
4156 /* Remove any excess precision (which is only present here in the
4157 case of compound assignments). */
4158 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4160 gcc_assert (modifycode != NOP_EXPR);
4161 rhs = TREE_OPERAND (rhs, 0);
4163 rhs_type = TREE_TYPE (rhs);
4165 /* Fold the RHS if it hasn't already been folded. */
4166 if (modifycode != NOP_EXPR)
4167 rhs = c_fully_fold (rhs, false, NULL);
4169 /* Remove the qualifiers for the rest of the expressions and create
4170 the VAL temp variable to hold the RHS. */
4171 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4172 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4173 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4174 TYPE_UNQUALIFIED);
4175 val = create_tmp_var_raw (nonatomic_rhs_type);
4176 TREE_ADDRESSABLE (val) = 1;
4177 suppress_warning (val);
4178 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4179 NULL_TREE);
4180 TREE_SIDE_EFFECTS (rhs) = 1;
4181 SET_EXPR_LOCATION (rhs, loc);
4182 if (modifycode != NOP_EXPR)
4183 add_stmt (rhs);
4185 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4186 an atomic_store. */
4187 if (modifycode == NOP_EXPR)
4189 compound_stmt = rhs;
4190 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4191 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4192 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4193 params->quick_push (lhs_addr);
4194 params->quick_push (rhs);
4195 params->quick_push (seq_cst);
4196 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4198 compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4199 compound_stmt, func_call);
4201 /* VAL is the value which was stored, return a COMPOUND_STMT of
4202 the statement and that value. */
4203 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4206 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4207 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4208 isn't applicable for such builtins. ??? Do we want to handle enums? */
4209 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4210 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4212 built_in_function fncode;
4213 switch (modifycode)
4215 case PLUS_EXPR:
4216 case POINTER_PLUS_EXPR:
4217 fncode = (return_old_p
4218 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4219 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4220 break;
4221 case MINUS_EXPR:
4222 fncode = (return_old_p
4223 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4224 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4225 break;
4226 case BIT_AND_EXPR:
4227 fncode = (return_old_p
4228 ? BUILT_IN_ATOMIC_FETCH_AND_N
4229 : BUILT_IN_ATOMIC_AND_FETCH_N);
4230 break;
4231 case BIT_IOR_EXPR:
4232 fncode = (return_old_p
4233 ? BUILT_IN_ATOMIC_FETCH_OR_N
4234 : BUILT_IN_ATOMIC_OR_FETCH_N);
4235 break;
4236 case BIT_XOR_EXPR:
4237 fncode = (return_old_p
4238 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4239 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4240 break;
4241 default:
4242 goto cas_loop;
4245 /* We can only use "_1" through "_16" variants of the atomic fetch
4246 built-ins. */
4247 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4248 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4249 goto cas_loop;
4251 /* If this is a pointer type, we need to multiply by the size of
4252 the pointer target type. */
4253 if (POINTER_TYPE_P (lhs_type))
4255 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4256 /* ??? This would introduce -Wdiscarded-qualifiers
4257 warning: __atomic_fetch_* expect volatile void *
4258 type as the first argument. (Assignments between
4259 atomic and non-atomic objects are OK.) */
4260 || TYPE_RESTRICT (lhs_type))
4261 goto cas_loop;
4262 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4263 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4264 convert (ptrdiff_type_node, rhs),
4265 convert (ptrdiff_type_node, sz));
4268 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4269 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4270 fndecl = builtin_decl_explicit (fncode);
4271 params->quick_push (lhs_addr);
4272 params->quick_push (rhs);
4273 params->quick_push (seq_cst);
4274 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4276 newval = create_tmp_var_raw (nonatomic_lhs_type);
4277 TREE_ADDRESSABLE (newval) = 1;
4278 suppress_warning (newval);
4279 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4280 NULL_TREE, NULL_TREE);
4281 SET_EXPR_LOCATION (rhs, loc);
4282 add_stmt (rhs);
4284 /* Finish the compound statement. */
4285 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4287 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4288 the statement and that value. */
4289 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4292 cas_loop:
4293 /* Create the variables and labels required for the op= form. */
4294 old = create_tmp_var_raw (nonatomic_lhs_type);
4295 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4296 TREE_ADDRESSABLE (old) = 1;
4297 suppress_warning (old);
4299 newval = create_tmp_var_raw (nonatomic_lhs_type);
4300 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4301 TREE_ADDRESSABLE (newval) = 1;
4302 suppress_warning (newval);
4304 loop_decl = create_artificial_label (loc);
4305 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4307 done_decl = create_artificial_label (loc);
4308 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4310 /* __atomic_load (addr, &old, SEQ_CST). */
4311 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4312 params->quick_push (lhs_addr);
4313 params->quick_push (old_addr);
4314 params->quick_push (seq_cst);
4315 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4316 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4317 NULL_TREE);
4318 add_stmt (old);
4319 params->truncate (0);
4321 /* Create the expressions for floating-point environment
4322 manipulation, if required. */
4323 bool need_fenv = (flag_trapping_math
4324 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4325 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4326 if (need_fenv)
4327 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4329 if (hold_call)
4330 add_stmt (hold_call);
4332 /* loop: */
4333 add_stmt (loop_label);
4335 /* newval = old + val; */
4336 if (rhs_type != rhs_semantic_type)
4337 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4338 rhs = build_binary_op (loc, modifycode, old, val, true);
4339 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4341 tree eptype = TREE_TYPE (rhs);
4342 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4343 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4345 else
4346 rhs = c_fully_fold (rhs, false, NULL);
4347 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4348 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4349 NULL_TREE, 0);
4350 if (rhs != error_mark_node)
4352 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4353 NULL_TREE);
4354 SET_EXPR_LOCATION (rhs, loc);
4355 add_stmt (rhs);
4358 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4359 goto done; */
4360 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4361 params->quick_push (lhs_addr);
4362 params->quick_push (old_addr);
4363 params->quick_push (newval_addr);
4364 params->quick_push (integer_zero_node);
4365 params->quick_push (seq_cst);
4366 params->quick_push (seq_cst);
4367 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4369 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4370 SET_EXPR_LOCATION (goto_stmt, loc);
4372 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4373 SET_EXPR_LOCATION (stmt, loc);
4374 add_stmt (stmt);
4376 if (clear_call)
4377 add_stmt (clear_call);
4379 /* goto loop; */
4380 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4381 SET_EXPR_LOCATION (goto_stmt, loc);
4382 add_stmt (goto_stmt);
4384 /* done: */
4385 add_stmt (done_label);
4387 if (update_call)
4388 add_stmt (update_call);
4390 /* Finish the compound statement. */
4391 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4393 /* NEWVAL is the value that was successfully stored, return a
4394 COMPOUND_EXPR of the statement and the appropriate value. */
4395 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4396 return_old_p ? old : newval);
4399 /* Construct and perhaps optimize a tree representation
4400 for a unary operation. CODE, a tree_code, specifies the operation
4401 and XARG is the operand.
4402 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4403 promotions (such as from short to int).
4404 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4405 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4406 to pointers in C99.
4408 LOCATION is the location of the operator. */
4410 tree
4411 build_unary_op (location_t location, enum tree_code code, tree xarg,
4412 bool noconvert)
4414 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4415 tree arg = xarg;
4416 tree argtype = NULL_TREE;
4417 enum tree_code typecode;
4418 tree val;
4419 tree ret = error_mark_node;
4420 tree eptype = NULL_TREE;
4421 const char *invalid_op_diag;
4422 bool int_operands;
4424 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4425 if (int_operands)
4426 arg = remove_c_maybe_const_expr (arg);
4428 if (code != ADDR_EXPR)
4429 arg = require_complete_type (location, arg);
4431 typecode = TREE_CODE (TREE_TYPE (arg));
4432 if (typecode == ERROR_MARK)
4433 return error_mark_node;
4434 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4435 typecode = INTEGER_TYPE;
4437 if ((invalid_op_diag
4438 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4440 error_at (location, invalid_op_diag);
4441 return error_mark_node;
4444 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4446 eptype = TREE_TYPE (arg);
4447 arg = TREE_OPERAND (arg, 0);
4450 switch (code)
4452 case CONVERT_EXPR:
4453 /* This is used for unary plus, because a CONVERT_EXPR
4454 is enough to prevent anybody from looking inside for
4455 associativity, but won't generate any code. */
4456 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4457 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4458 || gnu_vector_type_p (TREE_TYPE (arg))))
4460 error_at (location, "wrong type argument to unary plus");
4461 return error_mark_node;
4463 else if (!noconvert)
4464 arg = default_conversion (arg);
4465 arg = non_lvalue_loc (location, arg);
4466 break;
4468 case NEGATE_EXPR:
4469 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4470 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4471 || gnu_vector_type_p (TREE_TYPE (arg))))
4473 error_at (location, "wrong type argument to unary minus");
4474 return error_mark_node;
4476 else if (!noconvert)
4477 arg = default_conversion (arg);
4478 break;
4480 case BIT_NOT_EXPR:
4481 /* ~ works on integer types and non float vectors. */
4482 if (typecode == INTEGER_TYPE
4483 || (gnu_vector_type_p (TREE_TYPE (arg))
4484 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4486 tree e = arg;
4488 /* Warn if the expression has boolean value. */
4489 while (TREE_CODE (e) == COMPOUND_EXPR)
4490 e = TREE_OPERAND (e, 1);
4492 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4493 || truth_value_p (TREE_CODE (e))))
4495 auto_diagnostic_group d;
4496 if (warning_at (location, OPT_Wbool_operation,
4497 "%<~%> on a boolean expression"))
4499 gcc_rich_location richloc (location);
4500 richloc.add_fixit_insert_before (location, "!");
4501 inform (&richloc, "did you mean to use logical not?");
4504 if (!noconvert)
4505 arg = default_conversion (arg);
4507 else if (typecode == COMPLEX_TYPE)
4509 code = CONJ_EXPR;
4510 pedwarn (location, OPT_Wpedantic,
4511 "ISO C does not support %<~%> for complex conjugation");
4512 if (!noconvert)
4513 arg = default_conversion (arg);
4515 else
4517 error_at (location, "wrong type argument to bit-complement");
4518 return error_mark_node;
4520 break;
4522 case ABS_EXPR:
4523 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4525 error_at (location, "wrong type argument to abs");
4526 return error_mark_node;
4528 else if (!noconvert)
4529 arg = default_conversion (arg);
4530 break;
4532 case ABSU_EXPR:
4533 if (!(typecode == INTEGER_TYPE))
4535 error_at (location, "wrong type argument to absu");
4536 return error_mark_node;
4538 else if (!noconvert)
4539 arg = default_conversion (arg);
4540 break;
4542 case CONJ_EXPR:
4543 /* Conjugating a real value is a no-op, but allow it anyway. */
4544 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4545 || typecode == COMPLEX_TYPE))
4547 error_at (location, "wrong type argument to conjugation");
4548 return error_mark_node;
4550 else if (!noconvert)
4551 arg = default_conversion (arg);
4552 break;
4554 case TRUTH_NOT_EXPR:
4555 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4556 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4557 && typecode != COMPLEX_TYPE)
4559 error_at (location,
4560 "wrong type argument to unary exclamation mark");
4561 return error_mark_node;
4563 if (int_operands)
4565 arg = c_objc_common_truthvalue_conversion (location, xarg);
4566 arg = remove_c_maybe_const_expr (arg);
4568 else
4569 arg = c_objc_common_truthvalue_conversion (location, arg);
4570 ret = invert_truthvalue_loc (location, arg);
4571 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4572 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4573 location = EXPR_LOCATION (ret);
4574 goto return_build_unary_op;
4576 case REALPART_EXPR:
4577 case IMAGPART_EXPR:
4578 ret = build_real_imag_expr (location, code, arg);
4579 if (ret == error_mark_node)
4580 return error_mark_node;
4581 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4582 eptype = TREE_TYPE (eptype);
4583 goto return_build_unary_op;
4585 case PREINCREMENT_EXPR:
4586 case POSTINCREMENT_EXPR:
4587 case PREDECREMENT_EXPR:
4588 case POSTDECREMENT_EXPR:
4590 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4592 tree inner = build_unary_op (location, code,
4593 C_MAYBE_CONST_EXPR_EXPR (arg),
4594 noconvert);
4595 if (inner == error_mark_node)
4596 return error_mark_node;
4597 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4598 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4599 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4600 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4601 goto return_build_unary_op;
4604 /* Complain about anything that is not a true lvalue. In
4605 Objective-C, skip this check for property_refs. */
4606 if (!objc_is_property_ref (arg)
4607 && !lvalue_or_else (location,
4608 arg, ((code == PREINCREMENT_EXPR
4609 || code == POSTINCREMENT_EXPR)
4610 ? lv_increment
4611 : lv_decrement)))
4612 return error_mark_node;
4614 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4616 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4617 warning_at (location, OPT_Wc___compat,
4618 "increment of enumeration value is invalid in C++");
4619 else
4620 warning_at (location, OPT_Wc___compat,
4621 "decrement of enumeration value is invalid in C++");
4624 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4626 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4627 warning_at (location, OPT_Wbool_operation,
4628 "increment of a boolean expression");
4629 else
4630 warning_at (location, OPT_Wbool_operation,
4631 "decrement of a boolean expression");
4634 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4635 arg = c_fully_fold (arg, false, NULL, true);
4637 bool atomic_op;
4638 atomic_op = really_atomic_lvalue (arg);
4640 /* Increment or decrement the real part of the value,
4641 and don't change the imaginary part. */
4642 if (typecode == COMPLEX_TYPE)
4644 tree real, imag;
4646 pedwarn (location, OPT_Wpedantic,
4647 "ISO C does not support %<++%> and %<--%> on complex types");
4649 if (!atomic_op)
4651 arg = stabilize_reference (arg);
4652 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4653 true);
4654 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4655 true);
4656 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4657 if (real == error_mark_node || imag == error_mark_node)
4658 return error_mark_node;
4659 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4660 real, imag);
4661 goto return_build_unary_op;
4665 /* Report invalid types. */
4667 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4668 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4669 && typecode != COMPLEX_TYPE
4670 && !gnu_vector_type_p (TREE_TYPE (arg)))
4672 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4673 error_at (location, "wrong type argument to increment");
4674 else
4675 error_at (location, "wrong type argument to decrement");
4677 return error_mark_node;
4681 tree inc;
4683 argtype = TREE_TYPE (arg);
4685 /* Compute the increment. */
4687 if (typecode == POINTER_TYPE)
4689 /* If pointer target is an incomplete type,
4690 we just cannot know how to do the arithmetic. */
4691 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4693 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4694 error_at (location,
4695 "increment of pointer to an incomplete type %qT",
4696 TREE_TYPE (argtype));
4697 else
4698 error_at (location,
4699 "decrement of pointer to an incomplete type %qT",
4700 TREE_TYPE (argtype));
4702 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4703 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4705 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4706 pedwarn (location, OPT_Wpointer_arith,
4707 "wrong type argument to increment");
4708 else
4709 pedwarn (location, OPT_Wpointer_arith,
4710 "wrong type argument to decrement");
4712 else
4713 verify_type_context (location, TCTX_POINTER_ARITH,
4714 TREE_TYPE (argtype));
4716 inc = c_size_in_bytes (TREE_TYPE (argtype));
4717 inc = convert_to_ptrofftype_loc (location, inc);
4719 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4721 /* For signed fract types, we invert ++ to -- or
4722 -- to ++, and change inc from 1 to -1, because
4723 it is not possible to represent 1 in signed fract constants.
4724 For unsigned fract types, the result always overflows and
4725 we get an undefined (original) or the maximum value. */
4726 if (code == PREINCREMENT_EXPR)
4727 code = PREDECREMENT_EXPR;
4728 else if (code == PREDECREMENT_EXPR)
4729 code = PREINCREMENT_EXPR;
4730 else if (code == POSTINCREMENT_EXPR)
4731 code = POSTDECREMENT_EXPR;
4732 else /* code == POSTDECREMENT_EXPR */
4733 code = POSTINCREMENT_EXPR;
4735 inc = integer_minus_one_node;
4736 inc = convert (argtype, inc);
4738 else
4740 inc = VECTOR_TYPE_P (argtype)
4741 ? build_one_cst (argtype)
4742 : integer_one_node;
4743 inc = convert (argtype, inc);
4746 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4747 need to ask Objective-C to build the increment or decrement
4748 expression for it. */
4749 if (objc_is_property_ref (arg))
4750 return objc_build_incr_expr_for_property_ref (location, code,
4751 arg, inc);
4753 /* Report a read-only lvalue. */
4754 if (TYPE_READONLY (argtype))
4756 readonly_error (location, arg,
4757 ((code == PREINCREMENT_EXPR
4758 || code == POSTINCREMENT_EXPR)
4759 ? lv_increment : lv_decrement));
4760 return error_mark_node;
4762 else if (TREE_READONLY (arg))
4763 readonly_warning (arg,
4764 ((code == PREINCREMENT_EXPR
4765 || code == POSTINCREMENT_EXPR)
4766 ? lv_increment : lv_decrement));
4768 /* If the argument is atomic, use the special code sequences for
4769 atomic compound assignment. */
4770 if (atomic_op)
4772 arg = stabilize_reference (arg);
4773 ret = build_atomic_assign (location, arg,
4774 ((code == PREINCREMENT_EXPR
4775 || code == POSTINCREMENT_EXPR)
4776 ? PLUS_EXPR
4777 : MINUS_EXPR),
4778 (FRACT_MODE_P (TYPE_MODE (argtype))
4779 ? inc
4780 : integer_one_node),
4781 (code == POSTINCREMENT_EXPR
4782 || code == POSTDECREMENT_EXPR));
4783 goto return_build_unary_op;
4786 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4787 val = boolean_increment (code, arg);
4788 else
4789 val = build2 (code, TREE_TYPE (arg), arg, inc);
4790 TREE_SIDE_EFFECTS (val) = 1;
4791 ret = val;
4792 goto return_build_unary_op;
4795 case ADDR_EXPR:
4796 /* Note that this operation never does default_conversion. */
4798 /* The operand of unary '&' must be an lvalue (which excludes
4799 expressions of type void), or, in C99, the result of a [] or
4800 unary '*' operator. */
4801 if (VOID_TYPE_P (TREE_TYPE (arg))
4802 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4803 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4804 pedwarn (location, 0, "taking address of expression of type %<void%>");
4806 /* Let &* cancel out to simplify resulting code. */
4807 if (INDIRECT_REF_P (arg))
4809 /* Don't let this be an lvalue. */
4810 if (lvalue_p (TREE_OPERAND (arg, 0)))
4811 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4812 ret = TREE_OPERAND (arg, 0);
4813 goto return_build_unary_op;
4816 /* Anything not already handled and not a true memory reference
4817 or a non-lvalue array is an error. */
4818 if (typecode != FUNCTION_TYPE && !noconvert
4819 && !lvalue_or_else (location, arg, lv_addressof))
4820 return error_mark_node;
4822 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4823 folding later. */
4824 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4826 tree inner = build_unary_op (location, code,
4827 C_MAYBE_CONST_EXPR_EXPR (arg),
4828 noconvert);
4829 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4830 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4831 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4832 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4833 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4834 goto return_build_unary_op;
4837 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4838 argtype = TREE_TYPE (arg);
4840 /* If the lvalue is const or volatile, merge that into the type
4841 to which the address will point. This is only needed
4842 for function types. */
4843 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4844 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4845 && TREE_CODE (argtype) == FUNCTION_TYPE)
4847 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4848 int quals = orig_quals;
4850 if (TREE_READONLY (arg))
4851 quals |= TYPE_QUAL_CONST;
4852 if (TREE_THIS_VOLATILE (arg))
4853 quals |= TYPE_QUAL_VOLATILE;
4855 argtype = c_build_qualified_type (argtype, quals);
4858 switch (TREE_CODE (arg))
4860 case COMPONENT_REF:
4861 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4863 error_at (location, "cannot take address of bit-field %qD",
4864 TREE_OPERAND (arg, 1));
4865 return error_mark_node;
4868 /* fall through */
4870 case ARRAY_REF:
4871 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4873 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4874 && !POINTER_TYPE_P (TREE_TYPE (arg))
4875 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4877 error_at (location, "cannot take address of scalar with "
4878 "reverse storage order");
4879 return error_mark_node;
4882 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4883 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4884 warning_at (location, OPT_Wscalar_storage_order,
4885 "address of array with reverse scalar storage "
4886 "order requested");
4889 default:
4890 break;
4893 if (!c_mark_addressable (arg))
4894 return error_mark_node;
4896 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4897 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4899 argtype = build_pointer_type (argtype);
4901 /* ??? Cope with user tricks that amount to offsetof. Delete this
4902 when we have proper support for integer constant expressions. */
4903 val = get_base_address (arg);
4904 if (val && INDIRECT_REF_P (val)
4905 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4907 ret = fold_offsetof (arg, argtype);
4908 goto return_build_unary_op;
4911 val = build1 (ADDR_EXPR, argtype, arg);
4913 ret = val;
4914 goto return_build_unary_op;
4916 default:
4917 gcc_unreachable ();
4920 if (argtype == NULL_TREE)
4921 argtype = TREE_TYPE (arg);
4922 if (TREE_CODE (arg) == INTEGER_CST)
4923 ret = (require_constant_value
4924 ? fold_build1_initializer_loc (location, code, argtype, arg)
4925 : fold_build1_loc (location, code, argtype, arg));
4926 else
4927 ret = build1 (code, argtype, arg);
4928 return_build_unary_op:
4929 gcc_assert (ret != error_mark_node);
4930 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4931 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4932 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4933 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4934 ret = note_integer_operands (ret);
4935 if (eptype)
4936 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4937 protected_set_expr_location (ret, location);
4938 return ret;
4941 /* Return nonzero if REF is an lvalue valid for this language.
4942 Lvalues can be assigned, unless their type has TYPE_READONLY.
4943 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4945 bool
4946 lvalue_p (const_tree ref)
4948 const enum tree_code code = TREE_CODE (ref);
4950 switch (code)
4952 case REALPART_EXPR:
4953 case IMAGPART_EXPR:
4954 case COMPONENT_REF:
4955 return lvalue_p (TREE_OPERAND (ref, 0));
4957 case C_MAYBE_CONST_EXPR:
4958 return lvalue_p (TREE_OPERAND (ref, 1));
4960 case COMPOUND_LITERAL_EXPR:
4961 case STRING_CST:
4962 return true;
4964 case INDIRECT_REF:
4965 case ARRAY_REF:
4966 case VAR_DECL:
4967 case PARM_DECL:
4968 case RESULT_DECL:
4969 case ERROR_MARK:
4970 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4971 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4973 case BIND_EXPR:
4974 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4976 default:
4977 return false;
4981 /* Give a warning for storing in something that is read-only in GCC
4982 terms but not const in ISO C terms. */
4984 static void
4985 readonly_warning (tree arg, enum lvalue_use use)
4987 switch (use)
4989 case lv_assign:
4990 warning (0, "assignment of read-only location %qE", arg);
4991 break;
4992 case lv_increment:
4993 warning (0, "increment of read-only location %qE", arg);
4994 break;
4995 case lv_decrement:
4996 warning (0, "decrement of read-only location %qE", arg);
4997 break;
4998 default:
4999 gcc_unreachable ();
5001 return;
5005 /* Return nonzero if REF is an lvalue valid for this language;
5006 otherwise, print an error message and return zero. USE says
5007 how the lvalue is being used and so selects the error message.
5008 LOCATION is the location at which any error should be reported. */
5010 static int
5011 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
5013 int win = lvalue_p (ref);
5015 if (!win)
5016 lvalue_error (loc, use);
5018 return win;
5021 /* Mark EXP saying that we need to be able to take the
5022 address of it; it should not be allocated in a register.
5023 Returns true if successful. ARRAY_REF_P is true if this
5024 is for ARRAY_REF construction - in that case we don't want
5025 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5026 it is fine to use ARRAY_REFs for vector subscripts on vector
5027 register variables. */
5029 bool
5030 c_mark_addressable (tree exp, bool array_ref_p)
5032 tree x = exp;
5034 while (1)
5035 switch (TREE_CODE (x))
5037 case VIEW_CONVERT_EXPR:
5038 if (array_ref_p
5039 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5040 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5041 return true;
5042 x = TREE_OPERAND (x, 0);
5043 break;
5045 case COMPONENT_REF:
5046 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
5048 error ("cannot take address of bit-field %qD",
5049 TREE_OPERAND (x, 1));
5050 return false;
5052 /* FALLTHRU */
5053 case ADDR_EXPR:
5054 case ARRAY_REF:
5055 case REALPART_EXPR:
5056 case IMAGPART_EXPR:
5057 x = TREE_OPERAND (x, 0);
5058 break;
5060 case COMPOUND_LITERAL_EXPR:
5061 TREE_ADDRESSABLE (x) = 1;
5062 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5063 return true;
5065 case CONSTRUCTOR:
5066 TREE_ADDRESSABLE (x) = 1;
5067 return true;
5069 case VAR_DECL:
5070 case CONST_DECL:
5071 case PARM_DECL:
5072 case RESULT_DECL:
5073 if (C_DECL_REGISTER (x)
5074 && DECL_NONLOCAL (x))
5076 if (TREE_PUBLIC (x) || is_global_var (x))
5078 error
5079 ("global register variable %qD used in nested function", x);
5080 return false;
5082 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5084 else if (C_DECL_REGISTER (x))
5086 if (TREE_PUBLIC (x) || is_global_var (x))
5087 error ("address of global register variable %qD requested", x);
5088 else
5089 error ("address of register variable %qD requested", x);
5090 return false;
5093 /* FALLTHRU */
5094 case FUNCTION_DECL:
5095 TREE_ADDRESSABLE (x) = 1;
5096 /* FALLTHRU */
5097 default:
5098 return true;
5102 /* Convert EXPR to TYPE, warning about conversion problems with
5103 constants. SEMANTIC_TYPE is the type this conversion would use
5104 without excess precision. If SEMANTIC_TYPE is NULL, this function
5105 is equivalent to convert_and_check. This function is a wrapper that
5106 handles conversions that may be different than
5107 the usual ones because of excess precision. */
5109 static tree
5110 ep_convert_and_check (location_t loc, tree type, tree expr,
5111 tree semantic_type)
5113 if (TREE_TYPE (expr) == type)
5114 return expr;
5116 /* For C11, integer conversions may have results with excess
5117 precision. */
5118 if (flag_isoc11 || !semantic_type)
5119 return convert_and_check (loc, type, expr);
5121 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5122 && TREE_TYPE (expr) != semantic_type)
5124 /* For integers, we need to check the real conversion, not
5125 the conversion to the excess precision type. */
5126 expr = convert_and_check (loc, semantic_type, expr);
5128 /* Result type is the excess precision type, which should be
5129 large enough, so do not check. */
5130 return convert (type, expr);
5133 /* If EXPR refers to a built-in declared without a prototype returns
5134 the actual type of the built-in and, if non-null, set *BLTIN to
5135 a pointer to the built-in. Otherwise return the type of EXPR
5136 and clear *BLTIN if non-null. */
5138 static tree
5139 type_or_builtin_type (tree expr, tree *bltin = NULL)
5141 tree dummy;
5142 if (!bltin)
5143 bltin = &dummy;
5145 *bltin = NULL_TREE;
5147 tree type = TREE_TYPE (expr);
5148 if (TREE_CODE (expr) != ADDR_EXPR)
5149 return type;
5151 tree oper = TREE_OPERAND (expr, 0);
5152 if (!DECL_P (oper)
5153 || TREE_CODE (oper) != FUNCTION_DECL
5154 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5155 return type;
5157 built_in_function code = DECL_FUNCTION_CODE (oper);
5158 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5159 return type;
5161 if ((*bltin = builtin_decl_implicit (code)))
5162 type = build_pointer_type (TREE_TYPE (*bltin));
5164 return type;
5167 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5168 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5169 if folded to an integer constant then the unselected half may
5170 contain arbitrary operations not normally permitted in constant
5171 expressions. Set the location of the expression to LOC. */
5173 tree
5174 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5175 tree op1, tree op1_original_type, location_t op1_loc,
5176 tree op2, tree op2_original_type, location_t op2_loc)
5178 tree type1;
5179 tree type2;
5180 enum tree_code code1;
5181 enum tree_code code2;
5182 tree result_type = NULL;
5183 tree semantic_result_type = NULL;
5184 tree orig_op1 = op1, orig_op2 = op2;
5185 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5186 bool ifexp_int_operands;
5187 tree ret;
5189 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5190 if (op1_int_operands)
5191 op1 = remove_c_maybe_const_expr (op1);
5192 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5193 if (op2_int_operands)
5194 op2 = remove_c_maybe_const_expr (op2);
5195 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5196 if (ifexp_int_operands)
5197 ifexp = remove_c_maybe_const_expr (ifexp);
5199 /* Promote both alternatives. */
5201 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5202 op1 = default_conversion (op1);
5203 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5204 op2 = default_conversion (op2);
5206 if (TREE_CODE (ifexp) == ERROR_MARK
5207 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5208 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5209 return error_mark_node;
5211 tree bltin1 = NULL_TREE;
5212 tree bltin2 = NULL_TREE;
5213 type1 = type_or_builtin_type (op1, &bltin1);
5214 code1 = TREE_CODE (type1);
5215 type2 = type_or_builtin_type (op2, &bltin2);
5216 code2 = TREE_CODE (type2);
5218 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5219 return error_mark_node;
5221 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5222 return error_mark_node;
5224 /* C90 does not permit non-lvalue arrays in conditional expressions.
5225 In C99 they will be pointers by now. */
5226 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5228 error_at (colon_loc, "non-lvalue array in conditional expression");
5229 return error_mark_node;
5232 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5233 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5234 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5235 || code1 == COMPLEX_TYPE)
5236 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5237 || code2 == COMPLEX_TYPE))
5239 semantic_result_type = c_common_type (type1, type2);
5240 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5242 op1 = TREE_OPERAND (op1, 0);
5243 type1 = TREE_TYPE (op1);
5244 gcc_assert (TREE_CODE (type1) == code1);
5246 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5248 op2 = TREE_OPERAND (op2, 0);
5249 type2 = TREE_TYPE (op2);
5250 gcc_assert (TREE_CODE (type2) == code2);
5254 if (warn_cxx_compat)
5256 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5257 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5259 if (TREE_CODE (t1) == ENUMERAL_TYPE
5260 && TREE_CODE (t2) == ENUMERAL_TYPE
5261 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5262 warning_at (colon_loc, OPT_Wc___compat,
5263 ("different enum types in conditional is "
5264 "invalid in C++: %qT vs %qT"),
5265 t1, t2);
5268 /* Quickly detect the usual case where op1 and op2 have the same type
5269 after promotion. */
5270 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5272 if (type1 == type2)
5273 result_type = type1;
5274 else
5275 result_type = TYPE_MAIN_VARIANT (type1);
5277 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5278 || code1 == COMPLEX_TYPE)
5279 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5280 || code2 == COMPLEX_TYPE))
5282 /* In C11, a conditional expression between a floating-point
5283 type and an integer type should convert the integer type to
5284 the evaluation format of the floating-point type, with
5285 possible excess precision. */
5286 tree eptype1 = type1;
5287 tree eptype2 = type2;
5288 if (flag_isoc11)
5290 tree eptype;
5291 if (ANY_INTEGRAL_TYPE_P (type1)
5292 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5294 eptype2 = eptype;
5295 if (!semantic_result_type)
5296 semantic_result_type = c_common_type (type1, type2);
5298 else if (ANY_INTEGRAL_TYPE_P (type2)
5299 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5301 eptype1 = eptype;
5302 if (!semantic_result_type)
5303 semantic_result_type = c_common_type (type1, type2);
5306 result_type = c_common_type (eptype1, eptype2);
5307 if (result_type == error_mark_node)
5308 return error_mark_node;
5309 do_warn_double_promotion (result_type, type1, type2,
5310 "implicit conversion from %qT to %qT to "
5311 "match other result of conditional",
5312 colon_loc);
5314 /* If -Wsign-compare, warn here if type1 and type2 have
5315 different signedness. We'll promote the signed to unsigned
5316 and later code won't know it used to be different.
5317 Do this check on the original types, so that explicit casts
5318 will be considered, but default promotions won't. */
5319 if (c_inhibit_evaluation_warnings == 0)
5321 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5322 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5324 if (unsigned_op1 ^ unsigned_op2)
5326 bool ovf;
5328 /* Do not warn if the result type is signed, since the
5329 signed type will only be chosen if it can represent
5330 all the values of the unsigned type. */
5331 if (!TYPE_UNSIGNED (result_type))
5332 /* OK */;
5333 else
5335 bool op1_maybe_const = true;
5336 bool op2_maybe_const = true;
5338 /* Do not warn if the signed quantity is an
5339 unsuffixed integer literal (or some static
5340 constant expression involving such literals) and
5341 it is non-negative. This warning requires the
5342 operands to be folded for best results, so do
5343 that folding in this case even without
5344 warn_sign_compare to avoid warning options
5345 possibly affecting code generation. */
5346 c_inhibit_evaluation_warnings
5347 += (ifexp == truthvalue_false_node);
5348 op1 = c_fully_fold (op1, require_constant_value,
5349 &op1_maybe_const);
5350 c_inhibit_evaluation_warnings
5351 -= (ifexp == truthvalue_false_node);
5353 c_inhibit_evaluation_warnings
5354 += (ifexp == truthvalue_true_node);
5355 op2 = c_fully_fold (op2, require_constant_value,
5356 &op2_maybe_const);
5357 c_inhibit_evaluation_warnings
5358 -= (ifexp == truthvalue_true_node);
5360 if (warn_sign_compare)
5362 if ((unsigned_op2
5363 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5364 || (unsigned_op1
5365 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5366 /* OK */;
5367 else if (unsigned_op2)
5368 warning_at (op1_loc, OPT_Wsign_compare,
5369 "operand of %<?:%> changes signedness from "
5370 "%qT to %qT due to unsignedness of other "
5371 "operand", TREE_TYPE (orig_op1),
5372 TREE_TYPE (orig_op2));
5373 else
5374 warning_at (op2_loc, OPT_Wsign_compare,
5375 "operand of %<?:%> changes signedness from "
5376 "%qT to %qT due to unsignedness of other "
5377 "operand", TREE_TYPE (orig_op2),
5378 TREE_TYPE (orig_op1));
5380 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5381 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5382 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5383 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5388 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5390 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5391 pedwarn (colon_loc, OPT_Wpedantic,
5392 "ISO C forbids conditional expr with only one void side");
5393 result_type = void_type_node;
5395 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5397 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5398 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5399 addr_space_t as_common;
5401 if (comp_target_types (colon_loc, type1, type2))
5402 result_type = common_pointer_type (type1, type2);
5403 else if (null_pointer_constant_p (orig_op1))
5404 result_type = type2;
5405 else if (null_pointer_constant_p (orig_op2))
5406 result_type = type1;
5407 else if (!addr_space_superset (as1, as2, &as_common))
5409 error_at (colon_loc, "pointers to disjoint address spaces "
5410 "used in conditional expression");
5411 return error_mark_node;
5413 else if ((VOID_TYPE_P (TREE_TYPE (type1))
5414 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5415 || (VOID_TYPE_P (TREE_TYPE (type2))
5416 && !TYPE_ATOMIC (TREE_TYPE (type2))))
5418 tree t1 = TREE_TYPE (type1);
5419 tree t2 = TREE_TYPE (type2);
5420 if (!(VOID_TYPE_P (t1)
5421 && !TYPE_ATOMIC (t1)))
5423 /* roles are swapped */
5424 t1 = t2;
5425 t2 = TREE_TYPE (type1);
5427 tree t2_stripped = strip_array_types (t2);
5428 if ((TREE_CODE (t2) == ARRAY_TYPE)
5429 && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
5431 if (!flag_isoc2x)
5432 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5433 "pointer to array loses qualifier "
5434 "in conditional expression");
5435 else if (warn_c11_c2x_compat > 0)
5436 warning_at (colon_loc, OPT_Wc11_c2x_compat,
5437 "pointer to array loses qualifier "
5438 "in conditional expression in ISO C before C2X");
5440 if (TREE_CODE (t2) == FUNCTION_TYPE)
5441 pedwarn (colon_loc, OPT_Wpedantic,
5442 "ISO C forbids conditional expr between "
5443 "%<void *%> and function pointer");
5444 /* for array, use qualifiers of element type */
5445 if (flag_isoc2x)
5446 t2 = t2_stripped;
5447 result_type = build_pointer_type (qualify_type (t1, t2));
5449 /* Objective-C pointer comparisons are a bit more lenient. */
5450 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5451 result_type = objc_common_type (type1, type2);
5452 else
5454 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5455 if (bltin1 && bltin2)
5456 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5457 "pointer type mismatch between %qT and %qT "
5458 "of %qD and %qD in conditional expression",
5459 type1, type2, bltin1, bltin2);
5460 else
5461 pedwarn (colon_loc, 0,
5462 "pointer type mismatch in conditional expression");
5463 result_type = build_pointer_type
5464 (build_qualified_type (void_type_node, qual));
5467 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5469 if (!null_pointer_constant_p (orig_op2))
5470 pedwarn (colon_loc, 0,
5471 "pointer/integer type mismatch in conditional expression");
5472 else
5474 op2 = null_pointer_node;
5476 result_type = type1;
5478 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5480 if (!null_pointer_constant_p (orig_op1))
5481 pedwarn (colon_loc, 0,
5482 "pointer/integer type mismatch in conditional expression");
5483 else
5485 op1 = null_pointer_node;
5487 result_type = type2;
5490 if (!result_type)
5492 if (flag_cond_mismatch)
5493 result_type = void_type_node;
5494 else
5496 error_at (colon_loc, "type mismatch in conditional expression");
5497 return error_mark_node;
5501 /* Merge const and volatile flags of the incoming types. */
5502 result_type
5503 = build_type_variant (result_type,
5504 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5505 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5507 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5508 semantic_result_type);
5509 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5510 semantic_result_type);
5512 if (ifexp_bcp && ifexp == truthvalue_true_node)
5514 op2_int_operands = true;
5515 op1 = c_fully_fold (op1, require_constant_value, NULL);
5517 if (ifexp_bcp && ifexp == truthvalue_false_node)
5519 op1_int_operands = true;
5520 op2 = c_fully_fold (op2, require_constant_value, NULL);
5522 int_const = int_operands = (ifexp_int_operands
5523 && op1_int_operands
5524 && op2_int_operands);
5525 if (int_operands)
5527 int_const = ((ifexp == truthvalue_true_node
5528 && TREE_CODE (orig_op1) == INTEGER_CST
5529 && !TREE_OVERFLOW (orig_op1))
5530 || (ifexp == truthvalue_false_node
5531 && TREE_CODE (orig_op2) == INTEGER_CST
5532 && !TREE_OVERFLOW (orig_op2)));
5535 /* Need to convert condition operand into a vector mask. */
5536 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5538 tree vectype = TREE_TYPE (ifexp);
5539 tree elem_type = TREE_TYPE (vectype);
5540 tree zero = build_int_cst (elem_type, 0);
5541 tree zero_vec = build_vector_from_val (vectype, zero);
5542 tree cmp_type = truth_type_for (vectype);
5543 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5546 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5547 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5548 else
5550 if (int_operands)
5552 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5553 nested inside of the expression. */
5554 op1 = c_fully_fold (op1, false, NULL);
5555 op2 = c_fully_fold (op2, false, NULL);
5557 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5558 if (int_operands)
5559 ret = note_integer_operands (ret);
5561 if (semantic_result_type)
5562 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5564 protected_set_expr_location (ret, colon_loc);
5566 /* If the OP1 and OP2 are the same and don't have side-effects,
5567 warn here, because the COND_EXPR will be turned into OP1. */
5568 if (warn_duplicated_branches
5569 && TREE_CODE (ret) == COND_EXPR
5570 && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
5571 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5572 "this condition has identical branches");
5574 return ret;
5577 /* EXPR is an expression, location LOC, whose result is discarded.
5578 Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
5579 whose right-hand operand is such a call, possibly recursively). */
5581 static void
5582 maybe_warn_nodiscard (location_t loc, tree expr)
5584 if (VOID_TYPE_P (TREE_TYPE (expr)))
5585 return;
5586 while (TREE_CODE (expr) == COMPOUND_EXPR)
5588 expr = TREE_OPERAND (expr, 1);
5589 if (EXPR_HAS_LOCATION (expr))
5590 loc = EXPR_LOCATION (expr);
5592 if (TREE_CODE (expr) != CALL_EXPR)
5593 return;
5594 tree fn = CALL_EXPR_FN (expr);
5595 if (!fn)
5596 return;
5597 tree attr;
5598 if (TREE_CODE (fn) == ADDR_EXPR
5599 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5600 && (attr = lookup_attribute ("nodiscard",
5601 DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
5603 fn = TREE_OPERAND (fn, 0);
5604 tree args = TREE_VALUE (attr);
5605 if (args)
5606 args = TREE_VALUE (args);
5607 auto_diagnostic_group d;
5608 int warned;
5609 if (args)
5610 warned = warning_at (loc, OPT_Wunused_result,
5611 "ignoring return value of %qD, declared with "
5612 "attribute %<nodiscard%>: %E", fn, args);
5613 else
5614 warned = warning_at (loc, OPT_Wunused_result,
5615 "ignoring return value of %qD, declared with "
5616 "attribute %<nodiscard%>", fn);
5617 if (warned)
5618 inform (DECL_SOURCE_LOCATION (fn), "declared here");
5620 else
5622 tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
5623 attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
5624 if (!attr)
5625 return;
5626 tree args = TREE_VALUE (attr);
5627 if (args)
5628 args = TREE_VALUE (args);
5629 auto_diagnostic_group d;
5630 int warned;
5631 if (args)
5632 warned = warning_at (loc, OPT_Wunused_result,
5633 "ignoring return value of type %qT, declared "
5634 "with attribute %<nodiscard%>: %E",
5635 rettype, args);
5636 else
5637 warned = warning_at (loc, OPT_Wunused_result,
5638 "ignoring return value of type %qT, declared "
5639 "with attribute %<nodiscard%>", rettype);
5640 if (warned)
5642 if (TREE_CODE (fn) == ADDR_EXPR)
5644 fn = TREE_OPERAND (fn, 0);
5645 if (TREE_CODE (fn) == FUNCTION_DECL)
5646 inform (DECL_SOURCE_LOCATION (fn),
5647 "in call to %qD, declared here", fn);
5653 /* Return a compound expression that performs two expressions and
5654 returns the value of the second of them.
5656 LOC is the location of the COMPOUND_EXPR. */
5658 tree
5659 build_compound_expr (location_t loc, tree expr1, tree expr2)
5661 bool expr1_int_operands, expr2_int_operands;
5662 tree eptype = NULL_TREE;
5663 tree ret;
5665 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5666 if (expr1_int_operands)
5667 expr1 = remove_c_maybe_const_expr (expr1);
5668 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5669 if (expr2_int_operands)
5670 expr2 = remove_c_maybe_const_expr (expr2);
5672 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5673 expr1 = TREE_OPERAND (expr1, 0);
5674 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5676 eptype = TREE_TYPE (expr2);
5677 expr2 = TREE_OPERAND (expr2, 0);
5680 if (!TREE_SIDE_EFFECTS (expr1))
5682 /* The left-hand operand of a comma expression is like an expression
5683 statement: with -Wunused, we should warn if it doesn't have
5684 any side-effects, unless it was explicitly cast to (void). */
5685 if (warn_unused_value)
5687 if (VOID_TYPE_P (TREE_TYPE (expr1))
5688 && CONVERT_EXPR_P (expr1))
5689 ; /* (void) a, b */
5690 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5691 && TREE_CODE (expr1) == COMPOUND_EXPR
5692 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5693 ; /* (void) a, (void) b, c */
5694 else
5695 warning_at (loc, OPT_Wunused_value,
5696 "left-hand operand of comma expression has no effect");
5699 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5700 && warn_unused_value)
5702 tree r = expr1;
5703 location_t cloc = loc;
5704 while (TREE_CODE (r) == COMPOUND_EXPR)
5706 if (EXPR_HAS_LOCATION (r))
5707 cloc = EXPR_LOCATION (r);
5708 r = TREE_OPERAND (r, 1);
5710 if (!TREE_SIDE_EFFECTS (r)
5711 && !VOID_TYPE_P (TREE_TYPE (r))
5712 && !CONVERT_EXPR_P (r))
5713 warning_at (cloc, OPT_Wunused_value,
5714 "right-hand operand of comma expression has no effect");
5717 /* With -Wunused, we should also warn if the left-hand operand does have
5718 side-effects, but computes a value which is not used. For example, in
5719 `foo() + bar(), baz()' the result of the `+' operator is not used,
5720 so we should issue a warning. */
5721 else if (warn_unused_value)
5722 warn_if_unused_value (expr1, loc);
5724 maybe_warn_nodiscard (loc, expr1);
5726 if (expr2 == error_mark_node)
5727 return error_mark_node;
5729 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5731 if (flag_isoc99
5732 && expr1_int_operands
5733 && expr2_int_operands)
5734 ret = note_integer_operands (ret);
5736 if (eptype)
5737 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5739 protected_set_expr_location (ret, loc);
5740 return ret;
5743 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5744 which we are casting. OTYPE is the type of the expression being
5745 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5746 of the cast. -Wcast-qual appeared on the command line. Named
5747 address space qualifiers are not handled here, because they result
5748 in different warnings. */
5750 static void
5751 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5753 tree in_type = type;
5754 tree in_otype = otype;
5755 int added = 0;
5756 int discarded = 0;
5757 bool is_const;
5759 /* Check that the qualifiers on IN_TYPE are a superset of the
5760 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5761 nodes is uninteresting and we stop as soon as we hit a
5762 non-POINTER_TYPE node on either type. */
5765 in_otype = TREE_TYPE (in_otype);
5766 in_type = TREE_TYPE (in_type);
5768 /* GNU C allows cv-qualified function types. 'const' means the
5769 function is very pure, 'volatile' means it can't return. We
5770 need to warn when such qualifiers are added, not when they're
5771 taken away. */
5772 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5773 && TREE_CODE (in_type) == FUNCTION_TYPE)
5774 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5775 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5776 else
5777 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5778 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5780 while (TREE_CODE (in_type) == POINTER_TYPE
5781 && TREE_CODE (in_otype) == POINTER_TYPE);
5783 if (added)
5784 warning_at (loc, OPT_Wcast_qual,
5785 "cast adds %q#v qualifier to function type", added);
5787 if (discarded)
5788 /* There are qualifiers present in IN_OTYPE that are not present
5789 in IN_TYPE. */
5790 warning_at (loc, OPT_Wcast_qual,
5791 "cast discards %qv qualifier from pointer target type",
5792 discarded);
5794 if (added || discarded)
5795 return;
5797 /* A cast from **T to const **T is unsafe, because it can cause a
5798 const value to be changed with no additional warning. We only
5799 issue this warning if T is the same on both sides, and we only
5800 issue the warning if there are the same number of pointers on
5801 both sides, as otherwise the cast is clearly unsafe anyhow. A
5802 cast is unsafe when a qualifier is added at one level and const
5803 is not present at all outer levels.
5805 To issue this warning, we check at each level whether the cast
5806 adds new qualifiers not already seen. We don't need to special
5807 case function types, as they won't have the same
5808 TYPE_MAIN_VARIANT. */
5810 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5811 return;
5812 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5813 return;
5815 in_type = type;
5816 in_otype = otype;
5817 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5820 in_type = TREE_TYPE (in_type);
5821 in_otype = TREE_TYPE (in_otype);
5822 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5823 && !is_const)
5825 warning_at (loc, OPT_Wcast_qual,
5826 "to be safe all intermediate pointers in cast from "
5827 "%qT to %qT must be %<const%> qualified",
5828 otype, type);
5829 break;
5831 if (is_const)
5832 is_const = TYPE_READONLY (in_type);
5834 while (TREE_CODE (in_type) == POINTER_TYPE);
5837 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5839 static bool
5840 c_safe_arg_type_equiv_p (tree t1, tree t2)
5842 t1 = TYPE_MAIN_VARIANT (t1);
5843 t2 = TYPE_MAIN_VARIANT (t2);
5845 if (TREE_CODE (t1) == POINTER_TYPE
5846 && TREE_CODE (t2) == POINTER_TYPE)
5847 return true;
5849 /* The signedness of the parameter matters only when an integral
5850 type smaller than int is promoted to int, otherwise only the
5851 precision of the parameter matters.
5852 This check should make sure that the callee does not see
5853 undefined values in argument registers. */
5854 if (INTEGRAL_TYPE_P (t1)
5855 && INTEGRAL_TYPE_P (t2)
5856 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5857 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5858 || !targetm.calls.promote_prototypes (NULL_TREE)
5859 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5860 return true;
5862 return comptypes (t1, t2);
5865 /* Check if a type cast between two function types can be considered safe. */
5867 static bool
5868 c_safe_function_type_cast_p (tree t1, tree t2)
5870 if (TREE_TYPE (t1) == void_type_node &&
5871 TYPE_ARG_TYPES (t1) == void_list_node)
5872 return true;
5874 if (TREE_TYPE (t2) == void_type_node &&
5875 TYPE_ARG_TYPES (t2) == void_list_node)
5876 return true;
5878 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5879 return false;
5881 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5882 t1 && t2;
5883 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5884 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5885 return false;
5887 return true;
5890 /* Build an expression representing a cast to type TYPE of expression EXPR.
5891 LOC is the location of the cast-- typically the open paren of the cast. */
5893 tree
5894 build_c_cast (location_t loc, tree type, tree expr)
5896 tree value;
5898 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
5900 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5901 expr = TREE_OPERAND (expr, 0);
5903 value = expr;
5904 if (int_operands)
5905 value = remove_c_maybe_const_expr (value);
5907 if (type == error_mark_node || expr == error_mark_node)
5908 return error_mark_node;
5910 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5911 only in <protocol> qualifications. But when constructing cast expressions,
5912 the protocols do matter and must be kept around. */
5913 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5914 return build1 (NOP_EXPR, type, expr);
5916 type = TYPE_MAIN_VARIANT (type);
5918 if (TREE_CODE (type) == ARRAY_TYPE)
5920 error_at (loc, "cast specifies array type");
5921 return error_mark_node;
5924 if (TREE_CODE (type) == FUNCTION_TYPE)
5926 error_at (loc, "cast specifies function type");
5927 return error_mark_node;
5930 if (!VOID_TYPE_P (type))
5932 value = require_complete_type (loc, value);
5933 if (value == error_mark_node)
5934 return error_mark_node;
5937 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5939 if (RECORD_OR_UNION_TYPE_P (type))
5940 pedwarn (loc, OPT_Wpedantic,
5941 "ISO C forbids casting nonscalar to the same type");
5943 /* Convert to remove any qualifiers from VALUE's type. */
5944 value = convert (type, value);
5946 else if (TREE_CODE (type) == UNION_TYPE)
5948 tree field;
5950 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5951 if (TREE_TYPE (field) != error_mark_node
5952 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5953 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5954 break;
5956 if (field)
5958 tree t;
5959 bool maybe_const = true;
5961 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5962 t = c_fully_fold (value, false, &maybe_const);
5963 t = build_constructor_single (type, field, t);
5964 if (!maybe_const)
5965 t = c_wrap_maybe_const (t, true);
5966 t = digest_init (loc, type, t,
5967 NULL_TREE, false, true, 0);
5968 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5969 return t;
5971 error_at (loc, "cast to union type from type not present in union");
5972 return error_mark_node;
5974 else
5976 tree otype, ovalue;
5978 if (type == void_type_node)
5980 tree t = build1 (CONVERT_EXPR, type, value);
5981 SET_EXPR_LOCATION (t, loc);
5982 return t;
5985 otype = TREE_TYPE (value);
5987 /* Optionally warn about potentially worrisome casts. */
5988 if (warn_cast_qual
5989 && TREE_CODE (type) == POINTER_TYPE
5990 && TREE_CODE (otype) == POINTER_TYPE)
5991 handle_warn_cast_qual (loc, type, otype);
5993 /* Warn about conversions between pointers to disjoint
5994 address spaces. */
5995 if (TREE_CODE (type) == POINTER_TYPE
5996 && TREE_CODE (otype) == POINTER_TYPE
5997 && !null_pointer_constant_p (value))
5999 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
6000 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
6001 addr_space_t as_common;
6003 if (!addr_space_superset (as_to, as_from, &as_common))
6005 if (ADDR_SPACE_GENERIC_P (as_from))
6006 warning_at (loc, 0, "cast to %s address space pointer "
6007 "from disjoint generic address space pointer",
6008 c_addr_space_name (as_to));
6010 else if (ADDR_SPACE_GENERIC_P (as_to))
6011 warning_at (loc, 0, "cast to generic address space pointer "
6012 "from disjoint %s address space pointer",
6013 c_addr_space_name (as_from));
6015 else
6016 warning_at (loc, 0, "cast to %s address space pointer "
6017 "from disjoint %s address space pointer",
6018 c_addr_space_name (as_to),
6019 c_addr_space_name (as_from));
6023 /* Warn about possible alignment problems. */
6024 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
6025 && TREE_CODE (type) == POINTER_TYPE
6026 && TREE_CODE (otype) == POINTER_TYPE
6027 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
6028 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6029 /* Don't warn about opaque types, where the actual alignment
6030 restriction is unknown. */
6031 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
6032 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
6033 && min_align_of_type (TREE_TYPE (type))
6034 > min_align_of_type (TREE_TYPE (otype)))
6035 warning_at (loc, OPT_Wcast_align,
6036 "cast increases required alignment of target type");
6038 if (TREE_CODE (type) == INTEGER_TYPE
6039 && TREE_CODE (otype) == POINTER_TYPE
6040 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
6041 /* Unlike conversion of integers to pointers, where the
6042 warning is disabled for converting constants because
6043 of cases such as SIG_*, warn about converting constant
6044 pointers to integers. In some cases it may cause unwanted
6045 sign extension, and a warning is appropriate. */
6046 warning_at (loc, OPT_Wpointer_to_int_cast,
6047 "cast from pointer to integer of different size");
6049 if (TREE_CODE (value) == CALL_EXPR
6050 && TREE_CODE (type) != TREE_CODE (otype))
6051 warning_at (loc, OPT_Wbad_function_cast,
6052 "cast from function call of type %qT "
6053 "to non-matching type %qT", otype, type);
6055 if (TREE_CODE (type) == POINTER_TYPE
6056 && TREE_CODE (otype) == INTEGER_TYPE
6057 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
6058 /* Don't warn about converting any constant. */
6059 && !TREE_CONSTANT (value))
6060 warning_at (loc,
6061 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
6062 "of different size");
6064 if (warn_strict_aliasing <= 2)
6065 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
6067 /* If pedantic, warn for conversions between function and object
6068 pointer types, except for converting a null pointer constant
6069 to function pointer type. */
6070 if (pedantic
6071 && TREE_CODE (type) == POINTER_TYPE
6072 && TREE_CODE (otype) == POINTER_TYPE
6073 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6074 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
6075 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6076 "conversion of function pointer to object pointer type");
6078 if (pedantic
6079 && TREE_CODE (type) == POINTER_TYPE
6080 && TREE_CODE (otype) == POINTER_TYPE
6081 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6082 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6083 && !null_pointer_constant_p (value))
6084 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6085 "conversion of object pointer to function pointer type");
6087 if (TREE_CODE (type) == POINTER_TYPE
6088 && TREE_CODE (otype) == POINTER_TYPE
6089 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6090 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6091 && !c_safe_function_type_cast_p (TREE_TYPE (type),
6092 TREE_TYPE (otype)))
6093 warning_at (loc, OPT_Wcast_function_type,
6094 "cast between incompatible function types"
6095 " from %qT to %qT", otype, type);
6097 ovalue = value;
6098 value = convert (type, value);
6100 /* Ignore any integer overflow caused by the cast. */
6101 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
6103 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6105 if (!TREE_OVERFLOW (value))
6107 /* Avoid clobbering a shared constant. */
6108 value = copy_node (value);
6109 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
6112 else if (TREE_OVERFLOW (value))
6113 /* Reset VALUE's overflow flags, ensuring constant sharing. */
6114 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6118 /* Don't let a cast be an lvalue. */
6119 if (lvalue_p (value))
6120 value = non_lvalue_loc (loc, value);
6122 /* Don't allow the results of casting to floating-point or complex
6123 types be confused with actual constants, or casts involving
6124 integer and pointer types other than direct integer-to-integer
6125 and integer-to-pointer be confused with integer constant
6126 expressions and null pointer constants. */
6127 if (TREE_CODE (value) == REAL_CST
6128 || TREE_CODE (value) == COMPLEX_CST
6129 || (TREE_CODE (value) == INTEGER_CST
6130 && !((TREE_CODE (expr) == INTEGER_CST
6131 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6132 || TREE_CODE (expr) == REAL_CST
6133 || TREE_CODE (expr) == COMPLEX_CST)))
6134 value = build1 (NOP_EXPR, type, value);
6136 /* If the expression has integer operands and so can occur in an
6137 unevaluated part of an integer constant expression, ensure the
6138 return value reflects this. */
6139 if (int_operands
6140 && INTEGRAL_TYPE_P (type)
6141 && value != error_mark_node
6142 && !EXPR_INT_CONST_OPERANDS (value))
6143 value = note_integer_operands (value);
6145 protected_set_expr_location (value, loc);
6146 return value;
6149 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
6150 location of the open paren of the cast, or the position of the cast
6151 expr. */
6152 tree
6153 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6155 tree type;
6156 tree type_expr = NULL_TREE;
6157 bool type_expr_const = true;
6158 tree ret;
6159 int saved_wsp = warn_strict_prototypes;
6161 /* This avoids warnings about unprototyped casts on
6162 integers. E.g. "#define SIG_DFL (void(*)())0". */
6163 if (TREE_CODE (expr) == INTEGER_CST)
6164 warn_strict_prototypes = 0;
6165 type = groktypename (type_name, &type_expr, &type_expr_const);
6166 warn_strict_prototypes = saved_wsp;
6168 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6169 && reject_gcc_builtin (expr))
6170 return error_mark_node;
6172 ret = build_c_cast (loc, type, expr);
6173 if (type_expr)
6175 bool inner_expr_const = true;
6176 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6177 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6178 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6179 && inner_expr_const);
6180 SET_EXPR_LOCATION (ret, loc);
6183 if (!EXPR_HAS_LOCATION (ret))
6184 protected_set_expr_location (ret, loc);
6186 /* C++ does not permits types to be defined in a cast, but it
6187 allows references to incomplete types. */
6188 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6189 warning_at (loc, OPT_Wc___compat,
6190 "defining a type in a cast is invalid in C++");
6192 return ret;
6195 /* Build an assignment expression of lvalue LHS from value RHS.
6196 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6197 may differ from TREE_TYPE (LHS) for an enum bitfield.
6198 MODIFYCODE is the code for a binary operator that we use
6199 to combine the old value of LHS with RHS to get the new value.
6200 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6201 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6202 which may differ from TREE_TYPE (RHS) for an enum value.
6204 LOCATION is the location of the MODIFYCODE operator.
6205 RHS_LOC is the location of the RHS. */
6207 tree
6208 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6209 enum tree_code modifycode,
6210 location_t rhs_loc, tree rhs, tree rhs_origtype)
6212 tree result;
6213 tree newrhs;
6214 tree rhseval = NULL_TREE;
6215 tree lhstype = TREE_TYPE (lhs);
6216 tree olhstype = lhstype;
6217 bool npc;
6218 bool is_atomic_op;
6220 /* Types that aren't fully specified cannot be used in assignments. */
6221 lhs = require_complete_type (location, lhs);
6223 /* Avoid duplicate error messages from operands that had errors. */
6224 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6225 return error_mark_node;
6227 /* Ensure an error for assigning a non-lvalue array to an array in
6228 C90. */
6229 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6231 error_at (location, "assignment to expression with array type");
6232 return error_mark_node;
6235 /* For ObjC properties, defer this check. */
6236 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6237 return error_mark_node;
6239 is_atomic_op = really_atomic_lvalue (lhs);
6241 newrhs = rhs;
6243 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6245 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6246 lhs_origtype, modifycode, rhs_loc, rhs,
6247 rhs_origtype);
6248 if (inner == error_mark_node)
6249 return error_mark_node;
6250 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6251 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6252 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6253 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6254 protected_set_expr_location (result, location);
6255 return result;
6258 /* If a binary op has been requested, combine the old LHS value with the RHS
6259 producing the value we should actually store into the LHS. */
6261 if (modifycode != NOP_EXPR)
6263 lhs = c_fully_fold (lhs, false, NULL, true);
6264 lhs = stabilize_reference (lhs);
6266 /* Construct the RHS for any non-atomic compound assignemnt. */
6267 if (!is_atomic_op)
6269 /* If in LHS op= RHS the RHS has side-effects, ensure they
6270 are preevaluated before the rest of the assignment expression's
6271 side-effects, because RHS could contain e.g. function calls
6272 that modify LHS. */
6273 if (TREE_SIDE_EFFECTS (rhs))
6275 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6276 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6277 else
6278 newrhs = save_expr (rhs);
6279 rhseval = newrhs;
6280 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6281 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6282 newrhs);
6284 newrhs = build_binary_op (location,
6285 modifycode, lhs, newrhs, true);
6287 /* The original type of the right hand side is no longer
6288 meaningful. */
6289 rhs_origtype = NULL_TREE;
6293 if (c_dialect_objc ())
6295 /* Check if we are modifying an Objective-C property reference;
6296 if so, we need to generate setter calls. */
6297 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6298 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6299 else
6300 result = objc_maybe_build_modify_expr (lhs, newrhs);
6301 if (result)
6302 goto return_result;
6304 /* Else, do the check that we postponed for Objective-C. */
6305 if (!lvalue_or_else (location, lhs, lv_assign))
6306 return error_mark_node;
6309 /* Give an error for storing in something that is 'const'. */
6311 if (TYPE_READONLY (lhstype)
6312 || (RECORD_OR_UNION_TYPE_P (lhstype)
6313 && C_TYPE_FIELDS_READONLY (lhstype)))
6315 readonly_error (location, lhs, lv_assign);
6316 return error_mark_node;
6318 else if (TREE_READONLY (lhs))
6319 readonly_warning (lhs, lv_assign);
6321 /* If storing into a structure or union member,
6322 it has probably been given type `int'.
6323 Compute the type that would go with
6324 the actual amount of storage the member occupies. */
6326 if (TREE_CODE (lhs) == COMPONENT_REF
6327 && (TREE_CODE (lhstype) == INTEGER_TYPE
6328 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6329 || TREE_CODE (lhstype) == REAL_TYPE
6330 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6331 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6333 /* If storing in a field that is in actuality a short or narrower than one,
6334 we must store in the field in its actual type. */
6336 if (lhstype != TREE_TYPE (lhs))
6338 lhs = copy_node (lhs);
6339 TREE_TYPE (lhs) = lhstype;
6342 /* Issue -Wc++-compat warnings about an assignment to an enum type
6343 when LHS does not have its original type. This happens for,
6344 e.g., an enum bitfield in a struct. */
6345 if (warn_cxx_compat
6346 && lhs_origtype != NULL_TREE
6347 && lhs_origtype != lhstype
6348 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6350 tree checktype = (rhs_origtype != NULL_TREE
6351 ? rhs_origtype
6352 : TREE_TYPE (rhs));
6353 if (checktype != error_mark_node
6354 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6355 || (is_atomic_op && modifycode != NOP_EXPR)))
6356 warning_at (location, OPT_Wc___compat,
6357 "enum conversion in assignment is invalid in C++");
6360 /* Remove qualifiers. */
6361 lhstype = build_qualified_type (lhstype, TYPE_UNQUALIFIED);
6362 olhstype = build_qualified_type (olhstype, TYPE_UNQUALIFIED);
6364 /* Convert new value to destination type. Fold it first, then
6365 restore any excess precision information, for the sake of
6366 conversion warnings. */
6368 if (!(is_atomic_op && modifycode != NOP_EXPR))
6370 tree rhs_semantic_type = NULL_TREE;
6371 if (!c_in_omp_for)
6373 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6375 rhs_semantic_type = TREE_TYPE (newrhs);
6376 newrhs = TREE_OPERAND (newrhs, 0);
6378 npc = null_pointer_constant_p (newrhs);
6379 newrhs = c_fully_fold (newrhs, false, NULL);
6380 if (rhs_semantic_type)
6381 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6383 else
6384 npc = null_pointer_constant_p (newrhs);
6385 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6386 rhs_origtype, ic_assign, npc,
6387 NULL_TREE, NULL_TREE, 0);
6388 if (TREE_CODE (newrhs) == ERROR_MARK)
6389 return error_mark_node;
6392 /* Emit ObjC write barrier, if necessary. */
6393 if (c_dialect_objc () && flag_objc_gc)
6395 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6396 if (result)
6398 protected_set_expr_location (result, location);
6399 goto return_result;
6403 /* Scan operands. */
6405 if (is_atomic_op)
6406 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6407 else
6409 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6410 TREE_SIDE_EFFECTS (result) = 1;
6411 protected_set_expr_location (result, location);
6414 /* If we got the LHS in a different type for storing in,
6415 convert the result back to the nominal type of LHS
6416 so that the value we return always has the same type
6417 as the LHS argument. */
6419 if (olhstype == TREE_TYPE (result))
6420 goto return_result;
6422 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6423 rhs_origtype, ic_assign, false, NULL_TREE,
6424 NULL_TREE, 0);
6425 protected_set_expr_location (result, location);
6427 return_result:
6428 if (rhseval)
6429 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6430 return result;
6433 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6434 This is used to implement -fplan9-extensions. */
6436 static bool
6437 find_anonymous_field_with_type (tree struct_type, tree type)
6439 tree field;
6440 bool found;
6442 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6443 found = false;
6444 for (field = TYPE_FIELDS (struct_type);
6445 field != NULL_TREE;
6446 field = TREE_CHAIN (field))
6448 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6449 ? c_build_qualified_type (TREE_TYPE (field),
6450 TYPE_QUAL_ATOMIC)
6451 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6452 if (DECL_NAME (field) == NULL
6453 && comptypes (type, fieldtype))
6455 if (found)
6456 return false;
6457 found = true;
6459 else if (DECL_NAME (field) == NULL
6460 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6461 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6463 if (found)
6464 return false;
6465 found = true;
6468 return found;
6471 /* RHS is an expression whose type is pointer to struct. If there is
6472 an anonymous field in RHS with type TYPE, then return a pointer to
6473 that field in RHS. This is used with -fplan9-extensions. This
6474 returns NULL if no conversion could be found. */
6476 static tree
6477 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6479 tree rhs_struct_type, lhs_main_type;
6480 tree field, found_field;
6481 bool found_sub_field;
6482 tree ret;
6484 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6485 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6486 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6488 gcc_assert (POINTER_TYPE_P (type));
6489 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6490 ? c_build_qualified_type (TREE_TYPE (type),
6491 TYPE_QUAL_ATOMIC)
6492 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6494 found_field = NULL_TREE;
6495 found_sub_field = false;
6496 for (field = TYPE_FIELDS (rhs_struct_type);
6497 field != NULL_TREE;
6498 field = TREE_CHAIN (field))
6500 if (DECL_NAME (field) != NULL_TREE
6501 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6502 continue;
6503 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6504 ? c_build_qualified_type (TREE_TYPE (field),
6505 TYPE_QUAL_ATOMIC)
6506 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6507 if (comptypes (lhs_main_type, fieldtype))
6509 if (found_field != NULL_TREE)
6510 return NULL_TREE;
6511 found_field = field;
6513 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6514 lhs_main_type))
6516 if (found_field != NULL_TREE)
6517 return NULL_TREE;
6518 found_field = field;
6519 found_sub_field = true;
6523 if (found_field == NULL_TREE)
6524 return NULL_TREE;
6526 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6527 build_fold_indirect_ref (rhs), found_field,
6528 NULL_TREE);
6529 ret = build_fold_addr_expr_loc (location, ret);
6531 if (found_sub_field)
6533 ret = convert_to_anonymous_field (location, type, ret);
6534 gcc_assert (ret != NULL_TREE);
6537 return ret;
6540 /* Issue an error message for a bad initializer component.
6541 GMSGID identifies the message.
6542 The component name is taken from the spelling stack. */
6544 static void ATTRIBUTE_GCC_DIAG (2,0)
6545 error_init (location_t loc, const char *gmsgid, ...)
6547 char *ofwhat;
6549 auto_diagnostic_group d;
6551 /* The gmsgid may be a format string with %< and %>. */
6552 va_list ap;
6553 va_start (ap, gmsgid);
6554 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6555 va_end (ap);
6557 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6558 if (*ofwhat && warned)
6559 inform (loc, "(near initialization for %qs)", ofwhat);
6562 /* Issue a pedantic warning for a bad initializer component. OPT is
6563 the option OPT_* (from options.h) controlling this warning or 0 if
6564 it is unconditionally given. GMSGID identifies the message. The
6565 component name is taken from the spelling stack. */
6567 static void ATTRIBUTE_GCC_DIAG (3,0)
6568 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6570 /* Use the location where a macro was expanded rather than where
6571 it was defined to make sure macros defined in system headers
6572 but used incorrectly elsewhere are diagnosed. */
6573 location_t exploc = expansion_point_location_if_in_system_header (loc);
6574 auto_diagnostic_group d;
6575 va_list ap;
6576 va_start (ap, gmsgid);
6577 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6578 va_end (ap);
6579 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6580 if (*ofwhat && warned)
6581 inform (exploc, "(near initialization for %qs)", ofwhat);
6584 /* Issue a warning for a bad initializer component.
6586 OPT is the OPT_W* value corresponding to the warning option that
6587 controls this warning. GMSGID identifies the message. The
6588 component name is taken from the spelling stack. */
6590 static void
6591 warning_init (location_t loc, int opt, const char *gmsgid)
6593 char *ofwhat;
6594 bool warned;
6596 auto_diagnostic_group d;
6598 /* Use the location where a macro was expanded rather than where
6599 it was defined to make sure macros defined in system headers
6600 but used incorrectly elsewhere are diagnosed. */
6601 location_t exploc = expansion_point_location_if_in_system_header (loc);
6603 /* The gmsgid may be a format string with %< and %>. */
6604 warned = warning_at (exploc, opt, gmsgid);
6605 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6606 if (*ofwhat && warned)
6607 inform (exploc, "(near initialization for %qs)", ofwhat);
6610 /* If TYPE is an array type and EXPR is a parenthesized string
6611 constant, warn if pedantic that EXPR is being used to initialize an
6612 object of type TYPE. */
6614 void
6615 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6617 if (pedantic
6618 && TREE_CODE (type) == ARRAY_TYPE
6619 && TREE_CODE (expr.value) == STRING_CST
6620 && expr.original_code != STRING_CST)
6621 pedwarn_init (loc, OPT_Wpedantic,
6622 "array initialized from parenthesized string constant");
6625 /* Attempt to locate the parameter with the given index within FNDECL,
6626 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6628 static location_t
6629 get_fndecl_argument_location (tree fndecl, int argnum)
6631 int i;
6632 tree param;
6634 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6635 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6636 i < argnum && param;
6637 i++, param = TREE_CHAIN (param))
6640 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6641 return DECL_SOURCE_LOCATION (FNDECL). */
6642 if (param == NULL)
6643 return DECL_SOURCE_LOCATION (fndecl);
6645 return DECL_SOURCE_LOCATION (param);
6648 /* Issue a note about a mismatching argument for parameter PARMNUM
6649 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6650 Attempt to issue the note at the pertinent parameter of the decl;
6651 failing that issue it at the location of FUNDECL; failing that
6652 issue it at PLOC. */
6654 static void
6655 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6656 tree expected_type, tree actual_type)
6658 location_t loc;
6659 if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6660 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6661 else
6662 loc = ploc;
6664 inform (loc,
6665 "expected %qT but argument is of type %qT",
6666 expected_type, actual_type);
6669 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6670 function FUNDECL declared without prototype to parameter PARMNUM of
6671 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6673 static void
6674 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6675 tree parmtype, tree argtype)
6677 tree_code parmcode = TREE_CODE (parmtype);
6678 tree_code argcode = TREE_CODE (argtype);
6679 tree promoted = c_type_promotes_to (argtype);
6681 /* Avoid warning for enum arguments that promote to an integer type
6682 of the same size/mode. */
6683 if (parmcode == INTEGER_TYPE
6684 && argcode == ENUMERAL_TYPE
6685 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6686 return;
6688 if ((parmcode == argcode
6689 || (parmcode == INTEGER_TYPE
6690 && argcode == ENUMERAL_TYPE))
6691 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6692 return;
6694 /* This diagnoses even signed/unsigned mismatches. Those might be
6695 safe in many cases but GCC may emit suboptimal code for them so
6696 warning on those cases drives efficiency improvements. */
6697 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6698 TYPE_MAIN_VARIANT (promoted) == argtype
6699 ? G_("%qD argument %d type is %qT where %qT is expected "
6700 "in a call to built-in function declared without "
6701 "prototype")
6702 : G_("%qD argument %d promotes to %qT where %qT is expected "
6703 "in a call to built-in function declared without "
6704 "prototype"),
6705 fundecl, parmnum, promoted, parmtype))
6706 inform (DECL_SOURCE_LOCATION (fundecl),
6707 "built-in %qD declared here",
6708 fundecl);
6711 /* Convert value RHS to type TYPE as preparation for an assignment to
6712 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6713 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6714 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6715 constant before any folding.
6716 The real work of conversion is done by `convert'.
6717 The purpose of this function is to generate error messages
6718 for assignments that are not allowed in C.
6719 ERRTYPE says whether it is argument passing, assignment,
6720 initialization or return.
6722 In the following example, '~' denotes where EXPR_LOC and '^' where
6723 LOCATION point to:
6725 f (var); [ic_argpass]
6726 ^ ~~~
6727 x = var; [ic_assign]
6728 ^ ~~~;
6729 int x = var; [ic_init]
6731 return x; [ic_return]
6734 FUNCTION is a tree for the function being called.
6735 PARMNUM is the number of the argument, for printing in error messages.
6736 WARNOPT may be set to a warning option to issue the corresponding warning
6737 rather than an error for invalid conversions. Used for calls to built-in
6738 functions declared without a prototype. */
6740 static tree
6741 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6742 tree rhs, tree origtype, enum impl_conv errtype,
6743 bool null_pointer_constant, tree fundecl,
6744 tree function, int parmnum, int warnopt /* = 0 */)
6746 enum tree_code codel = TREE_CODE (type);
6747 tree orig_rhs = rhs;
6748 tree rhstype;
6749 enum tree_code coder;
6750 tree rname = NULL_TREE;
6751 bool objc_ok = false;
6753 /* Use the expansion point location to handle cases such as user's
6754 function returning a wrong-type macro defined in a system header. */
6755 location = expansion_point_location_if_in_system_header (location);
6757 if (errtype == ic_argpass)
6759 tree selector;
6760 /* Change pointer to function to the function itself for
6761 diagnostics. */
6762 if (TREE_CODE (function) == ADDR_EXPR
6763 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6764 function = TREE_OPERAND (function, 0);
6766 /* Handle an ObjC selector specially for diagnostics. */
6767 selector = objc_message_selector ();
6768 rname = function;
6769 if (selector && parmnum > 2)
6771 rname = selector;
6772 parmnum -= 2;
6776 /* This macro is used to emit diagnostics to ensure that all format
6777 strings are complete sentences, visible to gettext and checked at
6778 compile time. */
6779 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6780 do { \
6781 switch (errtype) \
6783 case ic_argpass: \
6785 auto_diagnostic_group d; \
6786 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6787 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6789 break; \
6790 case ic_assign: \
6791 pedwarn (LOCATION, OPT, AS); \
6792 break; \
6793 case ic_init: \
6794 pedwarn_init (LOCATION, OPT, IN); \
6795 break; \
6796 case ic_return: \
6797 pedwarn (LOCATION, OPT, RE); \
6798 break; \
6799 default: \
6800 gcc_unreachable (); \
6802 } while (0)
6804 /* This macro is used to emit diagnostics to ensure that all format
6805 strings are complete sentences, visible to gettext and checked at
6806 compile time. It can be called with 'pedwarn' or 'warning_at'. */
6807 #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6808 do { \
6809 switch (errtype) \
6811 case ic_argpass: \
6813 auto_diagnostic_group d; \
6814 if (PEDWARN) { \
6815 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6816 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6817 } else { \
6818 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6819 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6822 break; \
6823 case ic_assign: \
6824 if (PEDWARN) \
6825 pedwarn (LOCATION, OPT, AS, QUALS); \
6826 else \
6827 warning_at (LOCATION, OPT, AS, QUALS); \
6828 break; \
6829 case ic_init: \
6830 if (PEDWARN) \
6831 pedwarn (LOCATION, OPT, IN, QUALS); \
6832 else \
6833 warning_at (LOCATION, OPT, IN, QUALS); \
6834 break; \
6835 case ic_return: \
6836 if (PEDWARN) \
6837 pedwarn (LOCATION, OPT, RE, QUALS); \
6838 else \
6839 warning_at (LOCATION, OPT, RE, QUALS); \
6840 break; \
6841 default: \
6842 gcc_unreachable (); \
6844 } while (0)
6846 /* This macro is used to emit diagnostics to ensure that all format
6847 strings are complete sentences, visible to gettext and checked at
6848 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6849 extra parameter to enumerate qualifiers. */
6850 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6851 WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
6854 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6855 rhs = TREE_OPERAND (rhs, 0);
6857 rhstype = TREE_TYPE (rhs);
6858 coder = TREE_CODE (rhstype);
6860 if (coder == ERROR_MARK)
6861 return error_mark_node;
6863 if (c_dialect_objc ())
6865 int parmno;
6867 switch (errtype)
6869 case ic_return:
6870 parmno = 0;
6871 break;
6873 case ic_assign:
6874 parmno = -1;
6875 break;
6877 case ic_init:
6878 parmno = -2;
6879 break;
6881 default:
6882 parmno = parmnum;
6883 break;
6886 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6889 if (warn_cxx_compat)
6891 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6892 if (checktype != error_mark_node
6893 && TREE_CODE (type) == ENUMERAL_TYPE
6894 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6895 switch (errtype)
6897 case ic_argpass:
6898 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6899 "passing argument %d of %qE is invalid in C++",
6900 parmnum, rname))
6901 inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6902 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6903 "expected %qT but argument is of type %qT",
6904 type, rhstype);
6905 break;
6906 case ic_assign:
6907 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6908 "%qT in assignment is invalid in C++", rhstype, type);
6909 break;
6910 case ic_init:
6911 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6912 "%qT to %qT in initialization is invalid in C++",
6913 rhstype, type);
6914 break;
6915 case ic_return:
6916 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6917 "%qT in return is invalid in C++", rhstype, type);
6918 break;
6919 default:
6920 gcc_unreachable ();
6924 if (warn_enum_conversion)
6926 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6927 if (checktype != error_mark_node
6928 && TREE_CODE (checktype) == ENUMERAL_TYPE
6929 && TREE_CODE (type) == ENUMERAL_TYPE
6930 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6932 gcc_rich_location loc (location);
6933 warning_at (&loc, OPT_Wenum_conversion,
6934 "implicit conversion from %qT to %qT",
6935 checktype, type);
6939 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6941 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
6942 return rhs;
6945 if (coder == VOID_TYPE)
6947 /* Except for passing an argument to an unprototyped function,
6948 this is a constraint violation. When passing an argument to
6949 an unprototyped function, it is compile-time undefined;
6950 making it a constraint in that case was rejected in
6951 DR#252. */
6952 const char msg[] = "void value not ignored as it ought to be";
6953 if (warnopt)
6954 warning_at (location, warnopt, msg);
6955 else
6956 error_at (location, msg);
6957 return error_mark_node;
6959 rhs = require_complete_type (location, rhs);
6960 if (rhs == error_mark_node)
6961 return error_mark_node;
6963 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6964 return error_mark_node;
6966 /* A non-reference type can convert to a reference. This handles
6967 va_start, va_copy and possibly port built-ins. */
6968 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6970 if (!lvalue_p (rhs))
6972 const char msg[] = "cannot pass rvalue to reference parameter";
6973 if (warnopt)
6974 warning_at (location, warnopt, msg);
6975 else
6976 error_at (location, msg);
6977 return error_mark_node;
6979 if (!c_mark_addressable (rhs))
6980 return error_mark_node;
6981 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6982 SET_EXPR_LOCATION (rhs, location);
6984 rhs = convert_for_assignment (location, expr_loc,
6985 build_pointer_type (TREE_TYPE (type)),
6986 rhs, origtype, errtype,
6987 null_pointer_constant, fundecl, function,
6988 parmnum, warnopt);
6989 if (rhs == error_mark_node)
6990 return error_mark_node;
6992 rhs = build1 (NOP_EXPR, type, rhs);
6993 SET_EXPR_LOCATION (rhs, location);
6994 return rhs;
6996 /* Some types can interconvert without explicit casts. */
6997 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6998 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6999 return convert (type, rhs);
7000 /* Arithmetic types all interconvert, and enum is treated like int. */
7001 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
7002 || codel == FIXED_POINT_TYPE
7003 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
7004 || codel == BOOLEAN_TYPE)
7005 && (coder == INTEGER_TYPE || coder == REAL_TYPE
7006 || coder == FIXED_POINT_TYPE
7007 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
7008 || coder == BOOLEAN_TYPE))
7010 if (warnopt && errtype == ic_argpass)
7011 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
7012 rhstype);
7014 bool save = in_late_binary_op;
7015 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
7016 || (coder == REAL_TYPE
7017 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
7018 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
7019 in_late_binary_op = true;
7020 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
7021 ? expr_loc : location, type, orig_rhs);
7022 in_late_binary_op = save;
7023 return ret;
7026 /* Aggregates in different TUs might need conversion. */
7027 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
7028 && codel == coder
7029 && comptypes (type, rhstype))
7030 return convert_and_check (expr_loc != UNKNOWN_LOCATION
7031 ? expr_loc : location, type, rhs);
7033 /* Conversion to a transparent union or record from its member types.
7034 This applies only to function arguments. */
7035 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
7036 && TYPE_TRANSPARENT_AGGR (type))
7037 && errtype == ic_argpass)
7039 tree memb, marginal_memb = NULL_TREE;
7041 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
7043 tree memb_type = TREE_TYPE (memb);
7045 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
7046 TYPE_MAIN_VARIANT (rhstype)))
7047 break;
7049 if (TREE_CODE (memb_type) != POINTER_TYPE)
7050 continue;
7052 if (coder == POINTER_TYPE)
7054 tree ttl = TREE_TYPE (memb_type);
7055 tree ttr = TREE_TYPE (rhstype);
7057 /* Any non-function converts to a [const][volatile] void *
7058 and vice versa; otherwise, targets must be the same.
7059 Meanwhile, the lhs target must have all the qualifiers of
7060 the rhs. */
7061 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7062 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7063 || comp_target_types (location, memb_type, rhstype))
7065 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
7066 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
7067 /* If this type won't generate any warnings, use it. */
7068 if (lquals == rquals
7069 || ((TREE_CODE (ttr) == FUNCTION_TYPE
7070 && TREE_CODE (ttl) == FUNCTION_TYPE)
7071 ? ((lquals | rquals) == rquals)
7072 : ((lquals | rquals) == lquals)))
7073 break;
7075 /* Keep looking for a better type, but remember this one. */
7076 if (!marginal_memb)
7077 marginal_memb = memb;
7081 /* Can convert integer zero to any pointer type. */
7082 if (null_pointer_constant)
7084 rhs = null_pointer_node;
7085 break;
7089 if (memb || marginal_memb)
7091 if (!memb)
7093 /* We have only a marginally acceptable member type;
7094 it needs a warning. */
7095 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
7096 tree ttr = TREE_TYPE (rhstype);
7098 /* Const and volatile mean something different for function
7099 types, so the usual warnings are not appropriate. */
7100 if (TREE_CODE (ttr) == FUNCTION_TYPE
7101 && TREE_CODE (ttl) == FUNCTION_TYPE)
7103 /* Because const and volatile on functions are
7104 restrictions that say the function will not do
7105 certain things, it is okay to use a const or volatile
7106 function where an ordinary one is wanted, but not
7107 vice-versa. */
7108 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7109 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7110 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7111 OPT_Wdiscarded_qualifiers,
7112 G_("passing argument %d of %qE "
7113 "makes %q#v qualified function "
7114 "pointer from unqualified"),
7115 G_("assignment makes %q#v qualified "
7116 "function pointer from "
7117 "unqualified"),
7118 G_("initialization makes %q#v qualified "
7119 "function pointer from "
7120 "unqualified"),
7121 G_("return makes %q#v qualified function "
7122 "pointer from unqualified"),
7123 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7125 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7126 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7127 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7128 OPT_Wdiscarded_qualifiers,
7129 G_("passing argument %d of %qE discards "
7130 "%qv qualifier from pointer target type"),
7131 G_("assignment discards %qv qualifier "
7132 "from pointer target type"),
7133 G_("initialization discards %qv qualifier "
7134 "from pointer target type"),
7135 G_("return discards %qv qualifier from "
7136 "pointer target type"),
7137 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7139 memb = marginal_memb;
7142 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7143 pedwarn (location, OPT_Wpedantic,
7144 "ISO C prohibits argument conversion to union type");
7146 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7147 return build_constructor_single (type, memb, rhs);
7151 /* Conversions among pointers */
7152 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7153 && (coder == codel))
7155 /* If RHS refers to a built-in declared without a prototype
7156 BLTIN is the declaration of the built-in with a prototype
7157 and RHSTYPE is set to the actual type of the built-in. */
7158 tree bltin;
7159 rhstype = type_or_builtin_type (rhs, &bltin);
7161 tree ttl = TREE_TYPE (type);
7162 tree ttr = TREE_TYPE (rhstype);
7163 tree mvl = ttl;
7164 tree mvr = ttr;
7165 bool is_opaque_pointer;
7166 int target_cmp = 0; /* Cache comp_target_types () result. */
7167 addr_space_t asl;
7168 addr_space_t asr;
7170 if (TREE_CODE (mvl) != ARRAY_TYPE)
7171 mvl = (TYPE_ATOMIC (mvl)
7172 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7173 TYPE_QUAL_ATOMIC)
7174 : TYPE_MAIN_VARIANT (mvl));
7175 if (TREE_CODE (mvr) != ARRAY_TYPE)
7176 mvr = (TYPE_ATOMIC (mvr)
7177 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7178 TYPE_QUAL_ATOMIC)
7179 : TYPE_MAIN_VARIANT (mvr));
7180 /* Opaque pointers are treated like void pointers. */
7181 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7183 /* The Plan 9 compiler permits a pointer to a struct to be
7184 automatically converted into a pointer to an anonymous field
7185 within the struct. */
7186 if (flag_plan9_extensions
7187 && RECORD_OR_UNION_TYPE_P (mvl)
7188 && RECORD_OR_UNION_TYPE_P (mvr)
7189 && mvl != mvr)
7191 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7192 if (new_rhs != NULL_TREE)
7194 rhs = new_rhs;
7195 rhstype = TREE_TYPE (rhs);
7196 coder = TREE_CODE (rhstype);
7197 ttr = TREE_TYPE (rhstype);
7198 mvr = TYPE_MAIN_VARIANT (ttr);
7202 /* C++ does not allow the implicit conversion void* -> T*. However,
7203 for the purpose of reducing the number of false positives, we
7204 tolerate the special case of
7206 int *p = NULL;
7208 where NULL is typically defined in C to be '(void *) 0'. */
7209 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7210 warning_at (errtype == ic_argpass ? expr_loc : location,
7211 OPT_Wc___compat,
7212 "request for implicit conversion "
7213 "from %qT to %qT not permitted in C++", rhstype, type);
7215 /* See if the pointers point to incompatible address spaces. */
7216 asl = TYPE_ADDR_SPACE (ttl);
7217 asr = TYPE_ADDR_SPACE (ttr);
7218 if (!null_pointer_constant_p (rhs)
7219 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7221 switch (errtype)
7223 case ic_argpass:
7225 const char msg[] = G_("passing argument %d of %qE from "
7226 "pointer to non-enclosed address space");
7227 if (warnopt)
7228 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7229 else
7230 error_at (expr_loc, msg, parmnum, rname);
7231 break;
7233 case ic_assign:
7235 const char msg[] = G_("assignment from pointer to "
7236 "non-enclosed address space");
7237 if (warnopt)
7238 warning_at (location, warnopt, msg);
7239 else
7240 error_at (location, msg);
7241 break;
7243 case ic_init:
7245 const char msg[] = G_("initialization from pointer to "
7246 "non-enclosed address space");
7247 if (warnopt)
7248 warning_at (location, warnopt, msg);
7249 else
7250 error_at (location, msg);
7251 break;
7253 case ic_return:
7255 const char msg[] = G_("return from pointer to "
7256 "non-enclosed address space");
7257 if (warnopt)
7258 warning_at (location, warnopt, msg);
7259 else
7260 error_at (location, msg);
7261 break;
7263 default:
7264 gcc_unreachable ();
7266 return error_mark_node;
7269 /* Check if the right-hand side has a format attribute but the
7270 left-hand side doesn't. */
7271 if (warn_suggest_attribute_format
7272 && check_missing_format_attribute (type, rhstype))
7274 switch (errtype)
7276 case ic_argpass:
7277 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7278 "argument %d of %qE might be "
7279 "a candidate for a format attribute",
7280 parmnum, rname);
7281 break;
7282 case ic_assign:
7283 warning_at (location, OPT_Wsuggest_attribute_format,
7284 "assignment left-hand side might be "
7285 "a candidate for a format attribute");
7286 break;
7287 case ic_init:
7288 warning_at (location, OPT_Wsuggest_attribute_format,
7289 "initialization left-hand side might be "
7290 "a candidate for a format attribute");
7291 break;
7292 case ic_return:
7293 warning_at (location, OPT_Wsuggest_attribute_format,
7294 "return type might be "
7295 "a candidate for a format attribute");
7296 break;
7297 default:
7298 gcc_unreachable ();
7302 /* See if the pointers point to incompatible scalar storage orders. */
7303 if (warn_scalar_storage_order
7304 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7305 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7307 tree t;
7309 switch (errtype)
7311 case ic_argpass:
7312 /* Do not warn for built-in functions, for example memcpy, since we
7313 control how they behave and they can be useful in this area. */
7314 if (TREE_CODE (rname) != FUNCTION_DECL
7315 || !fndecl_built_in_p (rname))
7316 warning_at (location, OPT_Wscalar_storage_order,
7317 "passing argument %d of %qE from incompatible "
7318 "scalar storage order", parmnum, rname);
7319 break;
7320 case ic_assign:
7321 /* Do not warn if the RHS is a call to a function that returns a
7322 pointer that is not an alias. */
7323 if (TREE_CODE (rhs) != CALL_EXPR
7324 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7325 || !DECL_IS_MALLOC (t))
7326 warning_at (location, OPT_Wscalar_storage_order,
7327 "assignment to %qT from pointer type %qT with "
7328 "incompatible scalar storage order", type, rhstype);
7329 break;
7330 case ic_init:
7331 /* Likewise. */
7332 if (TREE_CODE (rhs) != CALL_EXPR
7333 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7334 || !DECL_IS_MALLOC (t))
7335 warning_at (location, OPT_Wscalar_storage_order,
7336 "initialization of %qT from pointer type %qT with "
7337 "incompatible scalar storage order", type, rhstype);
7338 break;
7339 case ic_return:
7340 warning_at (location, OPT_Wscalar_storage_order,
7341 "returning %qT from pointer type with incompatible "
7342 "scalar storage order %qT", rhstype, type);
7343 break;
7344 default:
7345 gcc_unreachable ();
7349 /* Any non-function converts to a [const][volatile] void *
7350 and vice versa; otherwise, targets must be the same.
7351 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7352 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7353 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7354 || (target_cmp = comp_target_types (location, type, rhstype))
7355 || is_opaque_pointer
7356 || ((c_common_unsigned_type (mvl)
7357 == c_common_unsigned_type (mvr))
7358 && (c_common_signed_type (mvl)
7359 == c_common_signed_type (mvr))
7360 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7362 /* Warn about loss of qualifers from pointers to arrays with
7363 qualifiers on the element type. */
7364 if (TREE_CODE (ttr) == ARRAY_TYPE)
7366 ttr = strip_array_types (ttr);
7367 ttl = strip_array_types (ttl);
7369 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7370 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7371 WARNING_FOR_QUALIFIERS (flag_isoc2x,
7372 location, expr_loc,
7373 OPT_Wdiscarded_array_qualifiers,
7374 G_("passing argument %d of %qE discards "
7375 "%qv qualifier from pointer target type"),
7376 G_("assignment discards %qv qualifier "
7377 "from pointer target type"),
7378 G_("initialization discards %qv qualifier "
7379 "from pointer target type"),
7380 G_("return discards %qv qualifier from "
7381 "pointer target type"),
7382 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7384 else if (pedantic
7385 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7387 (VOID_TYPE_P (ttr)
7388 && !null_pointer_constant
7389 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7390 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7391 G_("ISO C forbids passing argument %d of "
7392 "%qE between function pointer "
7393 "and %<void *%>"),
7394 G_("ISO C forbids assignment between "
7395 "function pointer and %<void *%>"),
7396 G_("ISO C forbids initialization between "
7397 "function pointer and %<void *%>"),
7398 G_("ISO C forbids return between function "
7399 "pointer and %<void *%>"));
7400 /* Const and volatile mean something different for function types,
7401 so the usual warnings are not appropriate. */
7402 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7403 && TREE_CODE (ttl) != FUNCTION_TYPE)
7405 /* Assignments between atomic and non-atomic objects are OK. */
7406 bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7407 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
7408 bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7409 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
7411 /* Don't warn about loss of qualifier for conversions from
7412 qualified void* to pointers to arrays with corresponding
7413 qualifier on the element type (except for pedantic before C23). */
7414 if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc2x))
7415 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7416 OPT_Wdiscarded_qualifiers,
7417 G_("passing argument %d of %qE discards "
7418 "%qv qualifier from pointer target type"),
7419 G_("assignment discards %qv qualifier "
7420 "from pointer target type"),
7421 G_("initialization discards %qv qualifier "
7422 "from pointer target type"),
7423 G_("return discards %qv qualifier from "
7424 "pointer target type"),
7425 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7426 else if (warn_quals_ped)
7427 pedwarn_c11 (location, OPT_Wc11_c2x_compat,
7428 "array with qualifier on the element is not qualified before C2X");
7430 /* If this is not a case of ignoring a mismatch in signedness,
7431 no warning. */
7432 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7433 || target_cmp)
7435 /* If there is a mismatch, do warn. */
7436 else if (warn_pointer_sign)
7437 switch (errtype)
7439 case ic_argpass:
7441 auto_diagnostic_group d;
7442 range_label_for_type_mismatch rhs_label (rhstype, type);
7443 gcc_rich_location richloc (expr_loc, &rhs_label);
7444 if (pedwarn (&richloc, OPT_Wpointer_sign,
7445 "pointer targets in passing argument %d of "
7446 "%qE differ in signedness", parmnum, rname))
7447 inform_for_arg (fundecl, expr_loc, parmnum, type,
7448 rhstype);
7450 break;
7451 case ic_assign:
7452 pedwarn (location, OPT_Wpointer_sign,
7453 "pointer targets in assignment from %qT to %qT "
7454 "differ in signedness", rhstype, type);
7455 break;
7456 case ic_init:
7457 pedwarn_init (location, OPT_Wpointer_sign,
7458 "pointer targets in initialization of %qT "
7459 "from %qT differ in signedness", type,
7460 rhstype);
7461 break;
7462 case ic_return:
7463 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7464 "returning %qT from a function with return type "
7465 "%qT differ in signedness", rhstype, type);
7466 break;
7467 default:
7468 gcc_unreachable ();
7471 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7472 && TREE_CODE (ttr) == FUNCTION_TYPE)
7474 /* Because const and volatile on functions are restrictions
7475 that say the function will not do certain things,
7476 it is okay to use a const or volatile function
7477 where an ordinary one is wanted, but not vice-versa. */
7478 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7479 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7480 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7481 OPT_Wdiscarded_qualifiers,
7482 G_("passing argument %d of %qE makes "
7483 "%q#v qualified function pointer "
7484 "from unqualified"),
7485 G_("assignment makes %q#v qualified function "
7486 "pointer from unqualified"),
7487 G_("initialization makes %q#v qualified "
7488 "function pointer from unqualified"),
7489 G_("return makes %q#v qualified function "
7490 "pointer from unqualified"),
7491 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7494 /* Avoid warning about the volatile ObjC EH puts on decls. */
7495 else if (!objc_ok)
7497 switch (errtype)
7499 case ic_argpass:
7501 auto_diagnostic_group d;
7502 range_label_for_type_mismatch rhs_label (rhstype, type);
7503 gcc_rich_location richloc (expr_loc, &rhs_label);
7504 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7505 "passing argument %d of %qE from incompatible "
7506 "pointer type", parmnum, rname))
7507 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7509 break;
7510 case ic_assign:
7511 if (bltin)
7512 pedwarn (location, OPT_Wincompatible_pointer_types,
7513 "assignment to %qT from pointer to "
7514 "%qD with incompatible type %qT",
7515 type, bltin, rhstype);
7516 else
7517 pedwarn (location, OPT_Wincompatible_pointer_types,
7518 "assignment to %qT from incompatible pointer type %qT",
7519 type, rhstype);
7520 break;
7521 case ic_init:
7522 if (bltin)
7523 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7524 "initialization of %qT from pointer to "
7525 "%qD with incompatible type %qT",
7526 type, bltin, rhstype);
7527 else
7528 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7529 "initialization of %qT from incompatible "
7530 "pointer type %qT",
7531 type, rhstype);
7532 break;
7533 case ic_return:
7534 if (bltin)
7535 pedwarn (location, OPT_Wincompatible_pointer_types,
7536 "returning pointer to %qD of type %qT from "
7537 "a function with incompatible type %qT",
7538 bltin, rhstype, type);
7539 else
7540 pedwarn (location, OPT_Wincompatible_pointer_types,
7541 "returning %qT from a function with incompatible "
7542 "return type %qT", rhstype, type);
7543 break;
7544 default:
7545 gcc_unreachable ();
7549 /* If RHS isn't an address, check pointer or array of packed
7550 struct or union. */
7551 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7553 return convert (type, rhs);
7555 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7557 /* ??? This should not be an error when inlining calls to
7558 unprototyped functions. */
7559 const char msg[] = "invalid use of non-lvalue array";
7560 if (warnopt)
7561 warning_at (location, warnopt, msg);
7562 else
7563 error_at (location, msg);
7564 return error_mark_node;
7566 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7568 /* An explicit constant 0 can convert to a pointer,
7569 or one that results from arithmetic, even including
7570 a cast to integer type. */
7571 if (!null_pointer_constant)
7572 switch (errtype)
7574 case ic_argpass:
7576 auto_diagnostic_group d;
7577 range_label_for_type_mismatch rhs_label (rhstype, type);
7578 gcc_rich_location richloc (expr_loc, &rhs_label);
7579 if (pedwarn (&richloc, OPT_Wint_conversion,
7580 "passing argument %d of %qE makes pointer from "
7581 "integer without a cast", parmnum, rname))
7582 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7584 break;
7585 case ic_assign:
7586 pedwarn (location, OPT_Wint_conversion,
7587 "assignment to %qT from %qT makes pointer from integer "
7588 "without a cast", type, rhstype);
7589 break;
7590 case ic_init:
7591 pedwarn_init (location, OPT_Wint_conversion,
7592 "initialization of %qT from %qT makes pointer from "
7593 "integer without a cast", type, rhstype);
7594 break;
7595 case ic_return:
7596 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7597 "function with return type %qT makes pointer from "
7598 "integer without a cast", rhstype, type);
7599 break;
7600 default:
7601 gcc_unreachable ();
7604 return convert (type, rhs);
7606 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7608 switch (errtype)
7610 case ic_argpass:
7612 auto_diagnostic_group d;
7613 range_label_for_type_mismatch rhs_label (rhstype, type);
7614 gcc_rich_location richloc (expr_loc, &rhs_label);
7615 if (pedwarn (&richloc, OPT_Wint_conversion,
7616 "passing argument %d of %qE makes integer from "
7617 "pointer without a cast", parmnum, rname))
7618 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7620 break;
7621 case ic_assign:
7622 pedwarn (location, OPT_Wint_conversion,
7623 "assignment to %qT from %qT makes integer from pointer "
7624 "without a cast", type, rhstype);
7625 break;
7626 case ic_init:
7627 pedwarn_init (location, OPT_Wint_conversion,
7628 "initialization of %qT from %qT makes integer from "
7629 "pointer without a cast", type, rhstype);
7630 break;
7631 case ic_return:
7632 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7633 "function with return type %qT makes integer from "
7634 "pointer without a cast", rhstype, type);
7635 break;
7636 default:
7637 gcc_unreachable ();
7640 return convert (type, rhs);
7642 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7644 tree ret;
7645 bool save = in_late_binary_op;
7646 in_late_binary_op = true;
7647 ret = convert (type, rhs);
7648 in_late_binary_op = save;
7649 return ret;
7652 switch (errtype)
7654 case ic_argpass:
7656 auto_diagnostic_group d;
7657 range_label_for_type_mismatch rhs_label (rhstype, type);
7658 gcc_rich_location richloc (expr_loc, &rhs_label);
7659 const char msg[] = G_("incompatible type for argument %d of %qE");
7660 if (warnopt)
7661 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7662 else
7663 error_at (&richloc, msg, parmnum, rname);
7664 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7666 break;
7667 case ic_assign:
7669 const char msg[]
7670 = G_("incompatible types when assigning to type %qT from type %qT");
7671 if (warnopt)
7672 warning_at (expr_loc, 0, msg, type, rhstype);
7673 else
7674 error_at (expr_loc, msg, type, rhstype);
7675 break;
7677 case ic_init:
7679 const char msg[]
7680 = G_("incompatible types when initializing type %qT using type %qT");
7681 if (warnopt)
7682 warning_at (location, 0, msg, type, rhstype);
7683 else
7684 error_at (location, msg, type, rhstype);
7685 break;
7687 case ic_return:
7689 const char msg[]
7690 = G_("incompatible types when returning type %qT but %qT was expected");
7691 if (warnopt)
7692 warning_at (location, 0, msg, rhstype, type);
7693 else
7694 error_at (location, msg, rhstype, type);
7695 break;
7697 default:
7698 gcc_unreachable ();
7701 return error_mark_node;
7704 /* If VALUE is a compound expr all of whose expressions are constant, then
7705 return its value. Otherwise, return error_mark_node.
7707 This is for handling COMPOUND_EXPRs as initializer elements
7708 which is allowed with a warning when -pedantic is specified. */
7710 static tree
7711 valid_compound_expr_initializer (tree value, tree endtype)
7713 if (TREE_CODE (value) == COMPOUND_EXPR)
7715 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7716 == error_mark_node)
7717 return error_mark_node;
7718 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7719 endtype);
7721 else if (!initializer_constant_valid_p (value, endtype))
7722 return error_mark_node;
7723 else
7724 return value;
7727 /* Perform appropriate conversions on the initial value of a variable,
7728 store it in the declaration DECL,
7729 and print any error messages that are appropriate.
7730 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7731 If the init is invalid, store an ERROR_MARK.
7733 INIT_LOC is the location of the initial value. */
7735 void
7736 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7738 tree value, type;
7739 bool npc = false;
7741 /* If variable's type was invalidly declared, just ignore it. */
7743 type = TREE_TYPE (decl);
7744 if (TREE_CODE (type) == ERROR_MARK)
7745 return;
7747 /* Digest the specified initializer into an expression. */
7749 if (init)
7750 npc = null_pointer_constant_p (init);
7751 value = digest_init (init_loc, type, init, origtype, npc,
7752 true, TREE_STATIC (decl));
7754 /* Store the expression if valid; else report error. */
7756 if (!in_system_header_at (input_location)
7757 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7758 warning (OPT_Wtraditional, "traditional C rejects automatic "
7759 "aggregate initialization");
7761 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7762 DECL_INITIAL (decl) = value;
7764 /* ANSI wants warnings about out-of-range constant initializers. */
7765 STRIP_TYPE_NOPS (value);
7766 if (TREE_STATIC (decl))
7767 constant_expression_warning (value);
7769 /* Check if we need to set array size from compound literal size. */
7770 if (TREE_CODE (type) == ARRAY_TYPE
7771 && TYPE_DOMAIN (type) == NULL_TREE
7772 && value != error_mark_node)
7774 tree inside_init = init;
7776 STRIP_TYPE_NOPS (inside_init);
7777 inside_init = fold (inside_init);
7779 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7781 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7783 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7785 /* For int foo[] = (int [3]){1}; we need to set array size
7786 now since later on array initializer will be just the
7787 brace enclosed list of the compound literal. */
7788 tree etype = strip_array_types (TREE_TYPE (decl));
7789 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7790 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7791 layout_type (type);
7792 layout_decl (cldecl, 0);
7793 TREE_TYPE (decl)
7794 = c_build_qualified_type (type, TYPE_QUALS (etype));
7800 /* Methods for storing and printing names for error messages. */
7802 /* Implement a spelling stack that allows components of a name to be pushed
7803 and popped. Each element on the stack is this structure. */
7805 struct spelling
7807 int kind;
7808 union
7810 unsigned HOST_WIDE_INT i;
7811 const char *s;
7812 } u;
7815 #define SPELLING_STRING 1
7816 #define SPELLING_MEMBER 2
7817 #define SPELLING_BOUNDS 3
7819 static struct spelling *spelling; /* Next stack element (unused). */
7820 static struct spelling *spelling_base; /* Spelling stack base. */
7821 static int spelling_size; /* Size of the spelling stack. */
7823 /* Macros to save and restore the spelling stack around push_... functions.
7824 Alternative to SAVE_SPELLING_STACK. */
7826 #define SPELLING_DEPTH() (spelling - spelling_base)
7827 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7829 /* Push an element on the spelling stack with type KIND and assign VALUE
7830 to MEMBER. */
7832 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7834 int depth = SPELLING_DEPTH (); \
7836 if (depth >= spelling_size) \
7838 spelling_size += 10; \
7839 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7840 spelling_size); \
7841 RESTORE_SPELLING_DEPTH (depth); \
7844 spelling->kind = (KIND); \
7845 spelling->MEMBER = (VALUE); \
7846 spelling++; \
7849 /* Push STRING on the stack. Printed literally. */
7851 static void
7852 push_string (const char *string)
7854 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7857 /* Push a member name on the stack. Printed as '.' STRING. */
7859 static void
7860 push_member_name (tree decl)
7862 const char *const string
7863 = (DECL_NAME (decl)
7864 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7865 : _("<anonymous>"));
7866 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7869 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7871 static void
7872 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7874 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7877 /* Compute the maximum size in bytes of the printed spelling. */
7879 static int
7880 spelling_length (void)
7882 int size = 0;
7883 struct spelling *p;
7885 for (p = spelling_base; p < spelling; p++)
7887 if (p->kind == SPELLING_BOUNDS)
7888 size += 25;
7889 else
7890 size += strlen (p->u.s) + 1;
7893 return size;
7896 /* Print the spelling to BUFFER and return it. */
7898 static char *
7899 print_spelling (char *buffer)
7901 char *d = buffer;
7902 struct spelling *p;
7904 for (p = spelling_base; p < spelling; p++)
7905 if (p->kind == SPELLING_BOUNDS)
7907 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7908 d += strlen (d);
7910 else
7912 const char *s;
7913 if (p->kind == SPELLING_MEMBER)
7914 *d++ = '.';
7915 for (s = p->u.s; (*d = *s++); d++)
7918 *d++ = '\0';
7919 return buffer;
7922 /* Digest the parser output INIT as an initializer for type TYPE.
7923 Return a C expression of type TYPE to represent the initial value.
7925 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7927 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7929 If INIT is a string constant, STRICT_STRING is true if it is
7930 unparenthesized or we should not warn here for it being parenthesized.
7931 For other types of INIT, STRICT_STRING is not used.
7933 INIT_LOC is the location of the INIT.
7935 REQUIRE_CONSTANT requests an error if non-constant initializers or
7936 elements are seen. */
7938 static tree
7939 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7940 bool null_pointer_constant, bool strict_string,
7941 int require_constant)
7943 enum tree_code code = TREE_CODE (type);
7944 tree inside_init = init;
7945 tree semantic_type = NULL_TREE;
7946 bool maybe_const = true;
7948 if (type == error_mark_node
7949 || !init
7950 || error_operand_p (init))
7951 return error_mark_node;
7953 STRIP_TYPE_NOPS (inside_init);
7955 if (!c_in_omp_for)
7957 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7959 semantic_type = TREE_TYPE (inside_init);
7960 inside_init = TREE_OPERAND (inside_init, 0);
7962 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7965 /* Initialization of an array of chars from a string constant
7966 optionally enclosed in braces. */
7968 if (code == ARRAY_TYPE && inside_init
7969 && TREE_CODE (inside_init) == STRING_CST)
7971 tree typ1
7972 = (TYPE_ATOMIC (TREE_TYPE (type))
7973 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7974 TYPE_QUAL_ATOMIC)
7975 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7976 /* Note that an array could be both an array of character type
7977 and an array of wchar_t if wchar_t is signed char or unsigned
7978 char. */
7979 bool char_array = (typ1 == char_type_node
7980 || typ1 == signed_char_type_node
7981 || typ1 == unsigned_char_type_node);
7982 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7983 bool char16_array = !!comptypes (typ1, char16_type_node);
7984 bool char32_array = !!comptypes (typ1, char32_type_node);
7986 if (char_array || wchar_array || char16_array || char32_array)
7988 struct c_expr expr;
7989 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7990 bool incompat_string_cst = false;
7991 expr.value = inside_init;
7992 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7993 expr.original_type = NULL;
7994 maybe_warn_string_init (init_loc, type, expr);
7996 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7997 pedwarn_init (init_loc, OPT_Wpedantic,
7998 "initialization of a flexible array member");
8000 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8001 TYPE_MAIN_VARIANT (type)))
8002 return inside_init;
8004 if (char_array)
8006 if (typ2 != char_type_node)
8007 incompat_string_cst = true;
8009 else if (!comptypes (typ1, typ2))
8010 incompat_string_cst = true;
8012 if (incompat_string_cst)
8014 error_init (init_loc, "cannot initialize array of %qT from "
8015 "a string literal with type array of %qT",
8016 typ1, typ2);
8017 return error_mark_node;
8020 if (TYPE_DOMAIN (type) != NULL_TREE
8021 && TYPE_SIZE (type) != NULL_TREE
8022 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8024 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8025 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
8027 /* Subtract the size of a single (possibly wide) character
8028 because it's ok to ignore the terminating null char
8029 that is counted in the length of the constant. */
8030 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
8031 pedwarn_init (init_loc, 0,
8032 ("initializer-string for array of %qT "
8033 "is too long"), typ1);
8034 else if (warn_cxx_compat
8035 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8036 warning_at (init_loc, OPT_Wc___compat,
8037 ("initializer-string for array of %qT "
8038 "is too long for C++"), typ1);
8039 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8041 unsigned HOST_WIDE_INT size
8042 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8043 const char *p = TREE_STRING_POINTER (inside_init);
8045 inside_init = build_string (size, p);
8049 TREE_TYPE (inside_init) = type;
8050 return inside_init;
8052 else if (INTEGRAL_TYPE_P (typ1))
8054 error_init (init_loc, "array of inappropriate type initialized "
8055 "from string constant");
8056 return error_mark_node;
8060 /* Build a VECTOR_CST from a *constant* vector constructor. If the
8061 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
8062 below and handle as a constructor. */
8063 if (code == VECTOR_TYPE
8064 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
8065 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
8066 && TREE_CONSTANT (inside_init))
8068 if (TREE_CODE (inside_init) == VECTOR_CST
8069 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8070 TYPE_MAIN_VARIANT (type)))
8071 return inside_init;
8073 if (TREE_CODE (inside_init) == CONSTRUCTOR)
8075 unsigned HOST_WIDE_INT ix;
8076 tree value;
8077 bool constant_p = true;
8079 /* Iterate through elements and check if all constructor
8080 elements are *_CSTs. */
8081 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
8082 if (!CONSTANT_CLASS_P (value))
8084 constant_p = false;
8085 break;
8088 if (constant_p)
8089 return build_vector_from_ctor (type,
8090 CONSTRUCTOR_ELTS (inside_init));
8094 if (warn_sequence_point)
8095 verify_sequence_points (inside_init);
8097 /* Any type can be initialized
8098 from an expression of the same type, optionally with braces. */
8100 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
8101 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8102 TYPE_MAIN_VARIANT (type))
8103 || (code == ARRAY_TYPE
8104 && comptypes (TREE_TYPE (inside_init), type))
8105 || (gnu_vector_type_p (type)
8106 && comptypes (TREE_TYPE (inside_init), type))
8107 || (code == POINTER_TYPE
8108 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
8109 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
8110 TREE_TYPE (type)))))
8112 if (code == POINTER_TYPE)
8114 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
8116 if (TREE_CODE (inside_init) == STRING_CST
8117 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8118 inside_init = array_to_pointer_conversion
8119 (init_loc, inside_init);
8120 else
8122 error_init (init_loc, "invalid use of non-lvalue array");
8123 return error_mark_node;
8128 if (code == VECTOR_TYPE)
8129 /* Although the types are compatible, we may require a
8130 conversion. */
8131 inside_init = convert (type, inside_init);
8133 if (require_constant
8134 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8136 /* As an extension, allow initializing objects with static storage
8137 duration with compound literals (which are then treated just as
8138 the brace enclosed list they contain). Also allow this for
8139 vectors, as we can only assign them with compound literals. */
8140 if (flag_isoc99 && code != VECTOR_TYPE)
8141 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
8142 "is not constant");
8143 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8144 inside_init = DECL_INITIAL (decl);
8147 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
8148 && TREE_CODE (inside_init) != CONSTRUCTOR)
8150 error_init (init_loc, "array initialized from non-constant array "
8151 "expression");
8152 return error_mark_node;
8155 /* Compound expressions can only occur here if -Wpedantic or
8156 -pedantic-errors is specified. In the later case, we always want
8157 an error. In the former case, we simply want a warning. */
8158 if (require_constant && pedantic
8159 && TREE_CODE (inside_init) == COMPOUND_EXPR)
8161 inside_init
8162 = valid_compound_expr_initializer (inside_init,
8163 TREE_TYPE (inside_init));
8164 if (inside_init == error_mark_node)
8165 error_init (init_loc, "initializer element is not constant");
8166 else
8167 pedwarn_init (init_loc, OPT_Wpedantic,
8168 "initializer element is not constant");
8169 if (flag_pedantic_errors)
8170 inside_init = error_mark_node;
8172 else if (require_constant
8173 && !initializer_constant_valid_p (inside_init,
8174 TREE_TYPE (inside_init)))
8176 error_init (init_loc, "initializer element is not constant");
8177 inside_init = error_mark_node;
8179 else if (require_constant && !maybe_const)
8180 pedwarn_init (init_loc, OPT_Wpedantic,
8181 "initializer element is not a constant expression");
8183 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8184 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8185 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8186 type, inside_init, origtype,
8187 ic_init, null_pointer_constant,
8188 NULL_TREE, NULL_TREE, 0);
8189 return inside_init;
8192 /* Handle scalar types, including conversions. */
8194 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8195 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8196 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
8198 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8199 && (TREE_CODE (init) == STRING_CST
8200 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8201 inside_init = init = array_to_pointer_conversion (init_loc, init);
8202 if (semantic_type)
8203 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8204 inside_init);
8205 inside_init
8206 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8207 inside_init, origtype, ic_init,
8208 null_pointer_constant, NULL_TREE, NULL_TREE,
8211 /* Check to see if we have already given an error message. */
8212 if (inside_init == error_mark_node)
8214 else if (require_constant && !TREE_CONSTANT (inside_init))
8216 error_init (init_loc, "initializer element is not constant");
8217 inside_init = error_mark_node;
8219 else if (require_constant
8220 && !initializer_constant_valid_p (inside_init,
8221 TREE_TYPE (inside_init)))
8223 error_init (init_loc, "initializer element is not computable at "
8224 "load time");
8225 inside_init = error_mark_node;
8227 else if (require_constant && !maybe_const)
8228 pedwarn_init (init_loc, OPT_Wpedantic,
8229 "initializer element is not a constant expression");
8231 return inside_init;
8234 /* Come here only for records and arrays. */
8236 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8238 error_init (init_loc, "variable-sized object may not be initialized");
8239 return error_mark_node;
8242 error_init (init_loc, "invalid initializer");
8243 return error_mark_node;
8246 /* Handle initializers that use braces. */
8248 /* Type of object we are accumulating a constructor for.
8249 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8250 static tree constructor_type;
8252 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8253 left to fill. */
8254 static tree constructor_fields;
8256 /* For an ARRAY_TYPE, this is the specified index
8257 at which to store the next element we get. */
8258 static tree constructor_index;
8260 /* For an ARRAY_TYPE, this is the maximum index. */
8261 static tree constructor_max_index;
8263 /* For a RECORD_TYPE, this is the first field not yet written out. */
8264 static tree constructor_unfilled_fields;
8266 /* For an ARRAY_TYPE, this is the index of the first element
8267 not yet written out. */
8268 static tree constructor_unfilled_index;
8270 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8271 This is so we can generate gaps between fields, when appropriate. */
8272 static tree constructor_bit_index;
8274 /* If we are saving up the elements rather than allocating them,
8275 this is the list of elements so far (in reverse order,
8276 most recent first). */
8277 static vec<constructor_elt, va_gc> *constructor_elements;
8279 /* 1 if constructor should be incrementally stored into a constructor chain,
8280 0 if all the elements should be kept in AVL tree. */
8281 static int constructor_incremental;
8283 /* 1 if so far this constructor's elements are all compile-time constants. */
8284 static int constructor_constant;
8286 /* 1 if so far this constructor's elements are all valid address constants. */
8287 static int constructor_simple;
8289 /* 1 if this constructor has an element that cannot be part of a
8290 constant expression. */
8291 static int constructor_nonconst;
8293 /* 1 if this constructor is erroneous so far. */
8294 static int constructor_erroneous;
8296 /* 1 if this constructor is the universal zero initializer { 0 }. */
8297 static int constructor_zeroinit;
8299 /* Structure for managing pending initializer elements, organized as an
8300 AVL tree. */
8302 struct init_node
8304 struct init_node *left, *right;
8305 struct init_node *parent;
8306 int balance;
8307 tree purpose;
8308 tree value;
8309 tree origtype;
8312 /* Tree of pending elements at this constructor level.
8313 These are elements encountered out of order
8314 which belong at places we haven't reached yet in actually
8315 writing the output.
8316 Will never hold tree nodes across GC runs. */
8317 static struct init_node *constructor_pending_elts;
8319 /* The SPELLING_DEPTH of this constructor. */
8320 static int constructor_depth;
8322 /* DECL node for which an initializer is being read.
8323 0 means we are reading a constructor expression
8324 such as (struct foo) {...}. */
8325 static tree constructor_decl;
8327 /* Nonzero if this is an initializer for a top-level decl. */
8328 static int constructor_top_level;
8330 /* Nonzero if there were any member designators in this initializer. */
8331 static int constructor_designated;
8333 /* Nesting depth of designator list. */
8334 static int designator_depth;
8336 /* Nonzero if there were diagnosed errors in this designator list. */
8337 static int designator_erroneous;
8340 /* This stack has a level for each implicit or explicit level of
8341 structuring in the initializer, including the outermost one. It
8342 saves the values of most of the variables above. */
8344 struct constructor_range_stack;
8346 struct constructor_stack
8348 struct constructor_stack *next;
8349 tree type;
8350 tree fields;
8351 tree index;
8352 tree max_index;
8353 tree unfilled_index;
8354 tree unfilled_fields;
8355 tree bit_index;
8356 vec<constructor_elt, va_gc> *elements;
8357 struct init_node *pending_elts;
8358 int offset;
8359 int depth;
8360 /* If value nonzero, this value should replace the entire
8361 constructor at this level. */
8362 struct c_expr replacement_value;
8363 struct constructor_range_stack *range_stack;
8364 char constant;
8365 char simple;
8366 char nonconst;
8367 char implicit;
8368 char erroneous;
8369 char outer;
8370 char incremental;
8371 char designated;
8372 int designator_depth;
8375 static struct constructor_stack *constructor_stack;
8377 /* This stack represents designators from some range designator up to
8378 the last designator in the list. */
8380 struct constructor_range_stack
8382 struct constructor_range_stack *next, *prev;
8383 struct constructor_stack *stack;
8384 tree range_start;
8385 tree index;
8386 tree range_end;
8387 tree fields;
8390 static struct constructor_range_stack *constructor_range_stack;
8392 /* This stack records separate initializers that are nested.
8393 Nested initializers can't happen in ANSI C, but GNU C allows them
8394 in cases like { ... (struct foo) { ... } ... }. */
8396 struct initializer_stack
8398 struct initializer_stack *next;
8399 tree decl;
8400 struct constructor_stack *constructor_stack;
8401 struct constructor_range_stack *constructor_range_stack;
8402 vec<constructor_elt, va_gc> *elements;
8403 struct spelling *spelling;
8404 struct spelling *spelling_base;
8405 int spelling_size;
8406 char top_level;
8407 char require_constant_value;
8408 char require_constant_elements;
8409 rich_location *missing_brace_richloc;
8412 static struct initializer_stack *initializer_stack;
8414 /* Prepare to parse and output the initializer for variable DECL. */
8416 void
8417 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8418 rich_location *richloc)
8420 const char *locus;
8421 struct initializer_stack *p = XNEW (struct initializer_stack);
8423 p->decl = constructor_decl;
8424 p->require_constant_value = require_constant_value;
8425 p->require_constant_elements = require_constant_elements;
8426 p->constructor_stack = constructor_stack;
8427 p->constructor_range_stack = constructor_range_stack;
8428 p->elements = constructor_elements;
8429 p->spelling = spelling;
8430 p->spelling_base = spelling_base;
8431 p->spelling_size = spelling_size;
8432 p->top_level = constructor_top_level;
8433 p->next = initializer_stack;
8434 p->missing_brace_richloc = richloc;
8435 initializer_stack = p;
8437 constructor_decl = decl;
8438 constructor_designated = 0;
8439 constructor_top_level = top_level;
8441 if (decl != NULL_TREE && decl != error_mark_node)
8443 require_constant_value = TREE_STATIC (decl);
8444 require_constant_elements
8445 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8446 /* For a scalar, you can always use any value to initialize,
8447 even within braces. */
8448 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8449 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8451 else
8453 require_constant_value = 0;
8454 require_constant_elements = 0;
8455 locus = _("(anonymous)");
8458 constructor_stack = 0;
8459 constructor_range_stack = 0;
8461 found_missing_braces = 0;
8463 spelling_base = 0;
8464 spelling_size = 0;
8465 RESTORE_SPELLING_DEPTH (0);
8467 if (locus)
8468 push_string (locus);
8471 void
8472 finish_init (void)
8474 struct initializer_stack *p = initializer_stack;
8476 /* Free the whole constructor stack of this initializer. */
8477 while (constructor_stack)
8479 struct constructor_stack *q = constructor_stack;
8480 constructor_stack = q->next;
8481 XDELETE (q);
8484 gcc_assert (!constructor_range_stack);
8486 /* Pop back to the data of the outer initializer (if any). */
8487 XDELETE (spelling_base);
8489 constructor_decl = p->decl;
8490 require_constant_value = p->require_constant_value;
8491 require_constant_elements = p->require_constant_elements;
8492 constructor_stack = p->constructor_stack;
8493 constructor_range_stack = p->constructor_range_stack;
8494 constructor_elements = p->elements;
8495 spelling = p->spelling;
8496 spelling_base = p->spelling_base;
8497 spelling_size = p->spelling_size;
8498 constructor_top_level = p->top_level;
8499 initializer_stack = p->next;
8500 XDELETE (p);
8503 /* Call here when we see the initializer is surrounded by braces.
8504 This is instead of a call to push_init_level;
8505 it is matched by a call to pop_init_level.
8507 TYPE is the type to initialize, for a constructor expression.
8508 For an initializer for a decl, TYPE is zero. */
8510 void
8511 really_start_incremental_init (tree type)
8513 struct constructor_stack *p = XNEW (struct constructor_stack);
8515 if (type == NULL_TREE)
8516 type = TREE_TYPE (constructor_decl);
8518 if (VECTOR_TYPE_P (type)
8519 && TYPE_VECTOR_OPAQUE (type))
8520 error ("opaque vector types cannot be initialized");
8522 p->type = constructor_type;
8523 p->fields = constructor_fields;
8524 p->index = constructor_index;
8525 p->max_index = constructor_max_index;
8526 p->unfilled_index = constructor_unfilled_index;
8527 p->unfilled_fields = constructor_unfilled_fields;
8528 p->bit_index = constructor_bit_index;
8529 p->elements = constructor_elements;
8530 p->constant = constructor_constant;
8531 p->simple = constructor_simple;
8532 p->nonconst = constructor_nonconst;
8533 p->erroneous = constructor_erroneous;
8534 p->pending_elts = constructor_pending_elts;
8535 p->depth = constructor_depth;
8536 p->replacement_value.value = 0;
8537 p->replacement_value.original_code = ERROR_MARK;
8538 p->replacement_value.original_type = NULL;
8539 p->implicit = 0;
8540 p->range_stack = 0;
8541 p->outer = 0;
8542 p->incremental = constructor_incremental;
8543 p->designated = constructor_designated;
8544 p->designator_depth = designator_depth;
8545 p->next = 0;
8546 constructor_stack = p;
8548 constructor_constant = 1;
8549 constructor_simple = 1;
8550 constructor_nonconst = 0;
8551 constructor_depth = SPELLING_DEPTH ();
8552 constructor_elements = NULL;
8553 constructor_pending_elts = 0;
8554 constructor_type = type;
8555 constructor_incremental = 1;
8556 constructor_designated = 0;
8557 constructor_zeroinit = 1;
8558 designator_depth = 0;
8559 designator_erroneous = 0;
8561 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8563 constructor_fields = TYPE_FIELDS (constructor_type);
8564 /* Skip any nameless bit fields at the beginning. */
8565 while (constructor_fields != NULL_TREE
8566 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8567 constructor_fields = DECL_CHAIN (constructor_fields);
8569 constructor_unfilled_fields = constructor_fields;
8570 constructor_bit_index = bitsize_zero_node;
8572 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8574 if (TYPE_DOMAIN (constructor_type))
8576 constructor_max_index
8577 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8579 /* Detect non-empty initializations of zero-length arrays. */
8580 if (constructor_max_index == NULL_TREE
8581 && TYPE_SIZE (constructor_type))
8582 constructor_max_index = integer_minus_one_node;
8584 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8585 to initialize VLAs will cause a proper error; avoid tree
8586 checking errors as well by setting a safe value. */
8587 if (constructor_max_index
8588 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8589 constructor_max_index = integer_minus_one_node;
8591 constructor_index
8592 = convert (bitsizetype,
8593 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8595 else
8597 constructor_index = bitsize_zero_node;
8598 constructor_max_index = NULL_TREE;
8601 constructor_unfilled_index = constructor_index;
8603 else if (gnu_vector_type_p (constructor_type))
8605 /* Vectors are like simple fixed-size arrays. */
8606 constructor_max_index =
8607 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8608 constructor_index = bitsize_zero_node;
8609 constructor_unfilled_index = constructor_index;
8611 else
8613 /* Handle the case of int x = {5}; */
8614 constructor_fields = constructor_type;
8615 constructor_unfilled_fields = constructor_type;
8619 extern location_t last_init_list_comma;
8621 /* Called when we see an open brace for a nested initializer. Finish
8622 off any pending levels with implicit braces. */
8623 void
8624 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8626 while (constructor_stack->implicit)
8628 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8629 && constructor_fields == NULL_TREE)
8630 process_init_element (input_location,
8631 pop_init_level (loc, 1, braced_init_obstack,
8632 last_init_list_comma),
8633 true, braced_init_obstack);
8634 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8635 && constructor_max_index
8636 && tree_int_cst_lt (constructor_max_index,
8637 constructor_index))
8638 process_init_element (input_location,
8639 pop_init_level (loc, 1, braced_init_obstack,
8640 last_init_list_comma),
8641 true, braced_init_obstack);
8642 else
8643 break;
8647 /* Push down into a subobject, for initialization.
8648 If this is for an explicit set of braces, IMPLICIT is 0.
8649 If it is because the next element belongs at a lower level,
8650 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8652 void
8653 push_init_level (location_t loc, int implicit,
8654 struct obstack *braced_init_obstack)
8656 struct constructor_stack *p;
8657 tree value = NULL_TREE;
8659 /* Unless this is an explicit brace, we need to preserve previous
8660 content if any. */
8661 if (implicit)
8663 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8664 value = find_init_member (constructor_fields, braced_init_obstack);
8665 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8666 value = find_init_member (constructor_index, braced_init_obstack);
8669 p = XNEW (struct constructor_stack);
8670 p->type = constructor_type;
8671 p->fields = constructor_fields;
8672 p->index = constructor_index;
8673 p->max_index = constructor_max_index;
8674 p->unfilled_index = constructor_unfilled_index;
8675 p->unfilled_fields = constructor_unfilled_fields;
8676 p->bit_index = constructor_bit_index;
8677 p->elements = constructor_elements;
8678 p->constant = constructor_constant;
8679 p->simple = constructor_simple;
8680 p->nonconst = constructor_nonconst;
8681 p->erroneous = constructor_erroneous;
8682 p->pending_elts = constructor_pending_elts;
8683 p->depth = constructor_depth;
8684 p->replacement_value.value = NULL_TREE;
8685 p->replacement_value.original_code = ERROR_MARK;
8686 p->replacement_value.original_type = NULL;
8687 p->implicit = implicit;
8688 p->outer = 0;
8689 p->incremental = constructor_incremental;
8690 p->designated = constructor_designated;
8691 p->designator_depth = designator_depth;
8692 p->next = constructor_stack;
8693 p->range_stack = 0;
8694 constructor_stack = p;
8696 constructor_constant = 1;
8697 constructor_simple = 1;
8698 constructor_nonconst = 0;
8699 constructor_depth = SPELLING_DEPTH ();
8700 constructor_elements = NULL;
8701 constructor_incremental = 1;
8702 constructor_designated = 0;
8703 constructor_pending_elts = 0;
8704 if (!implicit)
8706 p->range_stack = constructor_range_stack;
8707 constructor_range_stack = 0;
8708 designator_depth = 0;
8709 designator_erroneous = 0;
8712 /* Don't die if an entire brace-pair level is superfluous
8713 in the containing level. */
8714 if (constructor_type == NULL_TREE)
8716 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8718 /* Don't die if there are extra init elts at the end. */
8719 if (constructor_fields == NULL_TREE)
8720 constructor_type = NULL_TREE;
8721 else
8723 constructor_type = TREE_TYPE (constructor_fields);
8724 push_member_name (constructor_fields);
8725 constructor_depth++;
8727 /* If upper initializer is designated, then mark this as
8728 designated too to prevent bogus warnings. */
8729 constructor_designated = p->designated;
8731 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8733 constructor_type = TREE_TYPE (constructor_type);
8734 push_array_bounds (tree_to_uhwi (constructor_index));
8735 constructor_depth++;
8738 if (constructor_type == NULL_TREE)
8740 error_init (loc, "extra brace group at end of initializer");
8741 constructor_fields = NULL_TREE;
8742 constructor_unfilled_fields = NULL_TREE;
8743 return;
8746 if (value && TREE_CODE (value) == CONSTRUCTOR)
8748 constructor_constant = TREE_CONSTANT (value);
8749 constructor_simple = TREE_STATIC (value);
8750 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8751 constructor_elements = CONSTRUCTOR_ELTS (value);
8752 if (!vec_safe_is_empty (constructor_elements)
8753 && (TREE_CODE (constructor_type) == RECORD_TYPE
8754 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8755 set_nonincremental_init (braced_init_obstack);
8758 if (implicit == 1)
8760 found_missing_braces = 1;
8761 if (initializer_stack->missing_brace_richloc)
8762 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8763 (loc, "{");
8766 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8768 constructor_fields = TYPE_FIELDS (constructor_type);
8769 /* Skip any nameless bit fields at the beginning. */
8770 while (constructor_fields != NULL_TREE
8771 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8772 constructor_fields = DECL_CHAIN (constructor_fields);
8774 constructor_unfilled_fields = constructor_fields;
8775 constructor_bit_index = bitsize_zero_node;
8777 else if (gnu_vector_type_p (constructor_type))
8779 /* Vectors are like simple fixed-size arrays. */
8780 constructor_max_index =
8781 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8782 constructor_index = bitsize_int (0);
8783 constructor_unfilled_index = constructor_index;
8785 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8787 if (TYPE_DOMAIN (constructor_type))
8789 constructor_max_index
8790 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8792 /* Detect non-empty initializations of zero-length arrays. */
8793 if (constructor_max_index == NULL_TREE
8794 && TYPE_SIZE (constructor_type))
8795 constructor_max_index = integer_minus_one_node;
8797 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8798 to initialize VLAs will cause a proper error; avoid tree
8799 checking errors as well by setting a safe value. */
8800 if (constructor_max_index
8801 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8802 constructor_max_index = integer_minus_one_node;
8804 constructor_index
8805 = convert (bitsizetype,
8806 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8808 else
8809 constructor_index = bitsize_zero_node;
8811 constructor_unfilled_index = constructor_index;
8812 if (value && TREE_CODE (value) == STRING_CST)
8814 /* We need to split the char/wchar array into individual
8815 characters, so that we don't have to special case it
8816 everywhere. */
8817 set_nonincremental_init_from_string (value, braced_init_obstack);
8820 else
8822 if (constructor_type != error_mark_node)
8823 warning_init (input_location, 0, "braces around scalar initializer");
8824 constructor_fields = constructor_type;
8825 constructor_unfilled_fields = constructor_type;
8829 /* At the end of an implicit or explicit brace level,
8830 finish up that level of constructor. If a single expression
8831 with redundant braces initialized that level, return the
8832 c_expr structure for that expression. Otherwise, the original_code
8833 element is set to ERROR_MARK.
8834 If we were outputting the elements as they are read, return 0 as the value
8835 from inner levels (process_init_element ignores that),
8836 but return error_mark_node as the value from the outermost level
8837 (that's what we want to put in DECL_INITIAL).
8838 Otherwise, return a CONSTRUCTOR expression as the value. */
8840 struct c_expr
8841 pop_init_level (location_t loc, int implicit,
8842 struct obstack *braced_init_obstack,
8843 location_t insert_before)
8845 struct constructor_stack *p;
8846 struct c_expr ret;
8847 ret.value = NULL_TREE;
8848 ret.original_code = ERROR_MARK;
8849 ret.original_type = NULL;
8851 if (implicit == 0)
8853 /* When we come to an explicit close brace,
8854 pop any inner levels that didn't have explicit braces. */
8855 while (constructor_stack->implicit)
8856 process_init_element (input_location,
8857 pop_init_level (loc, 1, braced_init_obstack,
8858 insert_before),
8859 true, braced_init_obstack);
8860 gcc_assert (!constructor_range_stack);
8862 else
8863 if (initializer_stack->missing_brace_richloc)
8864 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8865 (insert_before, "}");
8867 /* Now output all pending elements. */
8868 constructor_incremental = 1;
8869 output_pending_init_elements (1, braced_init_obstack);
8871 p = constructor_stack;
8873 /* Error for initializing a flexible array member, or a zero-length
8874 array member in an inappropriate context. */
8875 if (constructor_type && constructor_fields
8876 && TREE_CODE (constructor_type) == ARRAY_TYPE
8877 && TYPE_DOMAIN (constructor_type)
8878 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8880 /* Silently discard empty initializations. The parser will
8881 already have pedwarned for empty brackets. */
8882 if (integer_zerop (constructor_unfilled_index))
8883 constructor_type = NULL_TREE;
8884 else
8886 gcc_assert (!TYPE_SIZE (constructor_type));
8888 if (constructor_depth > 2)
8889 error_init (loc, "initialization of flexible array member in a nested context");
8890 else
8891 pedwarn_init (loc, OPT_Wpedantic,
8892 "initialization of a flexible array member");
8894 /* We have already issued an error message for the existence
8895 of a flexible array member not at the end of the structure.
8896 Discard the initializer so that we do not die later. */
8897 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8898 constructor_type = NULL_TREE;
8902 switch (vec_safe_length (constructor_elements))
8904 case 0:
8905 /* Initialization with { } counts as zeroinit. */
8906 constructor_zeroinit = 1;
8907 break;
8908 case 1:
8909 /* This might be zeroinit as well. */
8910 if (integer_zerop ((*constructor_elements)[0].value))
8911 constructor_zeroinit = 1;
8912 break;
8913 default:
8914 /* If the constructor has more than one element, it can't be { 0 }. */
8915 constructor_zeroinit = 0;
8916 break;
8919 /* Warn when some structs are initialized with direct aggregation. */
8920 if (!implicit && found_missing_braces && warn_missing_braces
8921 && !constructor_zeroinit)
8923 gcc_assert (initializer_stack->missing_brace_richloc);
8924 warning_at (initializer_stack->missing_brace_richloc,
8925 OPT_Wmissing_braces,
8926 "missing braces around initializer");
8929 /* Warn when some struct elements are implicitly initialized to zero. */
8930 if (warn_missing_field_initializers
8931 && constructor_type
8932 && TREE_CODE (constructor_type) == RECORD_TYPE
8933 && constructor_unfilled_fields)
8935 /* Do not warn for flexible array members or zero-length arrays. */
8936 while (constructor_unfilled_fields
8937 && (!DECL_SIZE (constructor_unfilled_fields)
8938 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8939 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8941 if (constructor_unfilled_fields
8942 /* Do not warn if this level of the initializer uses member
8943 designators; it is likely to be deliberate. */
8944 && !constructor_designated
8945 /* Do not warn about initializing with { 0 } or with { }. */
8946 && !constructor_zeroinit)
8948 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8949 "missing initializer for field %qD of %qT",
8950 constructor_unfilled_fields,
8951 constructor_type))
8952 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8953 "%qD declared here", constructor_unfilled_fields);
8957 /* Pad out the end of the structure. */
8958 if (p->replacement_value.value)
8959 /* If this closes a superfluous brace pair,
8960 just pass out the element between them. */
8961 ret = p->replacement_value;
8962 else if (constructor_type == NULL_TREE)
8964 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8965 && TREE_CODE (constructor_type) != ARRAY_TYPE
8966 && !gnu_vector_type_p (constructor_type))
8968 /* A nonincremental scalar initializer--just return
8969 the element, after verifying there is just one. */
8970 if (vec_safe_is_empty (constructor_elements))
8972 if (!constructor_erroneous && constructor_type != error_mark_node)
8973 error_init (loc, "empty scalar initializer");
8974 ret.value = error_mark_node;
8976 else if (vec_safe_length (constructor_elements) != 1)
8978 error_init (loc, "extra elements in scalar initializer");
8979 ret.value = (*constructor_elements)[0].value;
8981 else
8982 ret.value = (*constructor_elements)[0].value;
8984 else
8986 if (constructor_erroneous)
8987 ret.value = error_mark_node;
8988 else
8990 ret.value = build_constructor (constructor_type,
8991 constructor_elements);
8992 if (constructor_constant)
8993 TREE_CONSTANT (ret.value) = 1;
8994 if (constructor_constant && constructor_simple)
8995 TREE_STATIC (ret.value) = 1;
8996 if (constructor_nonconst)
8997 CONSTRUCTOR_NON_CONST (ret.value) = 1;
9001 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
9003 if (constructor_nonconst)
9004 ret.original_code = C_MAYBE_CONST_EXPR;
9005 else if (ret.original_code == C_MAYBE_CONST_EXPR)
9006 ret.original_code = ERROR_MARK;
9009 constructor_type = p->type;
9010 constructor_fields = p->fields;
9011 constructor_index = p->index;
9012 constructor_max_index = p->max_index;
9013 constructor_unfilled_index = p->unfilled_index;
9014 constructor_unfilled_fields = p->unfilled_fields;
9015 constructor_bit_index = p->bit_index;
9016 constructor_elements = p->elements;
9017 constructor_constant = p->constant;
9018 constructor_simple = p->simple;
9019 constructor_nonconst = p->nonconst;
9020 constructor_erroneous = p->erroneous;
9021 constructor_incremental = p->incremental;
9022 constructor_designated = p->designated;
9023 designator_depth = p->designator_depth;
9024 constructor_pending_elts = p->pending_elts;
9025 constructor_depth = p->depth;
9026 if (!p->implicit)
9027 constructor_range_stack = p->range_stack;
9028 RESTORE_SPELLING_DEPTH (constructor_depth);
9030 constructor_stack = p->next;
9031 XDELETE (p);
9033 if (ret.value == NULL_TREE && constructor_stack == 0)
9034 ret.value = error_mark_node;
9035 return ret;
9038 /* Common handling for both array range and field name designators.
9039 ARRAY argument is nonzero for array ranges. Returns false for success. */
9041 static bool
9042 set_designator (location_t loc, bool array,
9043 struct obstack *braced_init_obstack)
9045 tree subtype;
9046 enum tree_code subcode;
9048 /* Don't die if an entire brace-pair level is superfluous
9049 in the containing level, or for an erroneous type. */
9050 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9051 return true;
9053 /* If there were errors in this designator list already, bail out
9054 silently. */
9055 if (designator_erroneous)
9056 return true;
9058 /* Likewise for an initializer for a variable-size type. Those are
9059 diagnosed in digest_init. */
9060 if (COMPLETE_TYPE_P (constructor_type)
9061 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9062 return true;
9064 if (!designator_depth)
9066 gcc_assert (!constructor_range_stack);
9068 /* Designator list starts at the level of closest explicit
9069 braces. */
9070 while (constructor_stack->implicit)
9071 process_init_element (input_location,
9072 pop_init_level (loc, 1, braced_init_obstack,
9073 last_init_list_comma),
9074 true, braced_init_obstack);
9075 constructor_designated = 1;
9076 return false;
9079 switch (TREE_CODE (constructor_type))
9081 case RECORD_TYPE:
9082 case UNION_TYPE:
9083 subtype = TREE_TYPE (constructor_fields);
9084 if (subtype != error_mark_node)
9085 subtype = TYPE_MAIN_VARIANT (subtype);
9086 break;
9087 case ARRAY_TYPE:
9088 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9089 break;
9090 default:
9091 gcc_unreachable ();
9094 subcode = TREE_CODE (subtype);
9095 if (array && subcode != ARRAY_TYPE)
9097 error_init (loc, "array index in non-array initializer");
9098 return true;
9100 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9102 error_init (loc, "field name not in record or union initializer");
9103 return true;
9106 constructor_designated = 1;
9107 finish_implicit_inits (loc, braced_init_obstack);
9108 push_init_level (loc, 2, braced_init_obstack);
9109 return false;
9112 /* If there are range designators in designator list, push a new designator
9113 to constructor_range_stack. RANGE_END is end of such stack range or
9114 NULL_TREE if there is no range designator at this level. */
9116 static void
9117 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9119 struct constructor_range_stack *p;
9121 p = (struct constructor_range_stack *)
9122 obstack_alloc (braced_init_obstack,
9123 sizeof (struct constructor_range_stack));
9124 p->prev = constructor_range_stack;
9125 p->next = 0;
9126 p->fields = constructor_fields;
9127 p->range_start = constructor_index;
9128 p->index = constructor_index;
9129 p->stack = constructor_stack;
9130 p->range_end = range_end;
9131 if (constructor_range_stack)
9132 constructor_range_stack->next = p;
9133 constructor_range_stack = p;
9136 /* Within an array initializer, specify the next index to be initialized.
9137 FIRST is that index. If LAST is nonzero, then initialize a range
9138 of indices, running from FIRST through LAST. */
9140 void
9141 set_init_index (location_t loc, tree first, tree last,
9142 struct obstack *braced_init_obstack)
9144 if (set_designator (loc, true, braced_init_obstack))
9145 return;
9147 designator_erroneous = 1;
9149 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9150 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9152 error_init (loc, "array index in initializer not of integer type");
9153 return;
9156 if (TREE_CODE (first) != INTEGER_CST)
9158 first = c_fully_fold (first, false, NULL);
9159 if (TREE_CODE (first) == INTEGER_CST)
9160 pedwarn_init (loc, OPT_Wpedantic,
9161 "array index in initializer is not "
9162 "an integer constant expression");
9165 if (last && TREE_CODE (last) != INTEGER_CST)
9167 last = c_fully_fold (last, false, NULL);
9168 if (TREE_CODE (last) == INTEGER_CST)
9169 pedwarn_init (loc, OPT_Wpedantic,
9170 "array index in initializer is not "
9171 "an integer constant expression");
9174 if (TREE_CODE (first) != INTEGER_CST)
9175 error_init (loc, "nonconstant array index in initializer");
9176 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9177 error_init (loc, "nonconstant array index in initializer");
9178 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9179 error_init (loc, "array index in non-array initializer");
9180 else if (tree_int_cst_sgn (first) == -1)
9181 error_init (loc, "array index in initializer exceeds array bounds");
9182 else if (constructor_max_index
9183 && tree_int_cst_lt (constructor_max_index, first))
9184 error_init (loc, "array index in initializer exceeds array bounds");
9185 else
9187 constant_expression_warning (first);
9188 if (last)
9189 constant_expression_warning (last);
9190 constructor_index = convert (bitsizetype, first);
9191 if (tree_int_cst_lt (constructor_index, first))
9193 constructor_index = copy_node (constructor_index);
9194 TREE_OVERFLOW (constructor_index) = 1;
9197 if (last)
9199 if (tree_int_cst_equal (first, last))
9200 last = NULL_TREE;
9201 else if (tree_int_cst_lt (last, first))
9203 error_init (loc, "empty index range in initializer");
9204 last = NULL_TREE;
9206 else
9208 last = convert (bitsizetype, last);
9209 if (constructor_max_index != NULL_TREE
9210 && tree_int_cst_lt (constructor_max_index, last))
9212 error_init (loc, "array index range in initializer exceeds "
9213 "array bounds");
9214 last = NULL_TREE;
9219 designator_depth++;
9220 designator_erroneous = 0;
9221 if (constructor_range_stack || last)
9222 push_range_stack (last, braced_init_obstack);
9226 /* Within a struct initializer, specify the next field to be initialized. */
9228 void
9229 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9230 struct obstack *braced_init_obstack)
9232 tree field;
9234 if (set_designator (loc, false, braced_init_obstack))
9235 return;
9237 designator_erroneous = 1;
9239 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9241 error_init (loc, "field name not in record or union initializer");
9242 return;
9245 field = lookup_field (constructor_type, fieldname);
9247 if (field == NULL_TREE)
9249 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9250 if (guessed_id)
9252 gcc_rich_location rich_loc (fieldname_loc);
9253 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9254 error_at (&rich_loc,
9255 "%qT has no member named %qE; did you mean %qE?",
9256 constructor_type, fieldname, guessed_id);
9258 else
9259 error_at (fieldname_loc, "%qT has no member named %qE",
9260 constructor_type, fieldname);
9262 else
9265 constructor_fields = TREE_VALUE (field);
9266 designator_depth++;
9267 designator_erroneous = 0;
9268 if (constructor_range_stack)
9269 push_range_stack (NULL_TREE, braced_init_obstack);
9270 field = TREE_CHAIN (field);
9271 if (field)
9273 if (set_designator (loc, false, braced_init_obstack))
9274 return;
9277 while (field != NULL_TREE);
9280 /* Add a new initializer to the tree of pending initializers. PURPOSE
9281 identifies the initializer, either array index or field in a structure.
9282 VALUE is the value of that index or field. If ORIGTYPE is not
9283 NULL_TREE, it is the original type of VALUE.
9285 IMPLICIT is true if value comes from pop_init_level (1),
9286 the new initializer has been merged with the existing one
9287 and thus no warnings should be emitted about overriding an
9288 existing initializer. */
9290 static void
9291 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9292 bool implicit, struct obstack *braced_init_obstack)
9294 struct init_node *p, **q, *r;
9296 q = &constructor_pending_elts;
9297 p = 0;
9299 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9301 while (*q != 0)
9303 p = *q;
9304 if (tree_int_cst_lt (purpose, p->purpose))
9305 q = &p->left;
9306 else if (tree_int_cst_lt (p->purpose, purpose))
9307 q = &p->right;
9308 else
9310 if (!implicit)
9312 if (TREE_SIDE_EFFECTS (p->value))
9313 warning_init (loc, OPT_Woverride_init_side_effects,
9314 "initialized field with side-effects "
9315 "overwritten");
9316 else if (warn_override_init)
9317 warning_init (loc, OPT_Woverride_init,
9318 "initialized field overwritten");
9320 p->value = value;
9321 p->origtype = origtype;
9322 return;
9326 else
9328 tree bitpos;
9330 bitpos = bit_position (purpose);
9331 while (*q != NULL)
9333 p = *q;
9334 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9335 q = &p->left;
9336 else if (p->purpose != purpose)
9337 q = &p->right;
9338 else
9340 if (!implicit)
9342 if (TREE_SIDE_EFFECTS (p->value))
9343 warning_init (loc, OPT_Woverride_init_side_effects,
9344 "initialized field with side-effects "
9345 "overwritten");
9346 else if (warn_override_init)
9347 warning_init (loc, OPT_Woverride_init,
9348 "initialized field overwritten");
9350 p->value = value;
9351 p->origtype = origtype;
9352 return;
9357 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9358 sizeof (struct init_node));
9359 r->purpose = purpose;
9360 r->value = value;
9361 r->origtype = origtype;
9363 *q = r;
9364 r->parent = p;
9365 r->left = 0;
9366 r->right = 0;
9367 r->balance = 0;
9369 while (p)
9371 struct init_node *s;
9373 if (r == p->left)
9375 if (p->balance == 0)
9376 p->balance = -1;
9377 else if (p->balance < 0)
9379 if (r->balance < 0)
9381 /* L rotation. */
9382 p->left = r->right;
9383 if (p->left)
9384 p->left->parent = p;
9385 r->right = p;
9387 p->balance = 0;
9388 r->balance = 0;
9390 s = p->parent;
9391 p->parent = r;
9392 r->parent = s;
9393 if (s)
9395 if (s->left == p)
9396 s->left = r;
9397 else
9398 s->right = r;
9400 else
9401 constructor_pending_elts = r;
9403 else
9405 /* LR rotation. */
9406 struct init_node *t = r->right;
9408 r->right = t->left;
9409 if (r->right)
9410 r->right->parent = r;
9411 t->left = r;
9413 p->left = t->right;
9414 if (p->left)
9415 p->left->parent = p;
9416 t->right = p;
9418 p->balance = t->balance < 0;
9419 r->balance = -(t->balance > 0);
9420 t->balance = 0;
9422 s = p->parent;
9423 p->parent = t;
9424 r->parent = t;
9425 t->parent = s;
9426 if (s)
9428 if (s->left == p)
9429 s->left = t;
9430 else
9431 s->right = t;
9433 else
9434 constructor_pending_elts = t;
9436 break;
9438 else
9440 /* p->balance == +1; growth of left side balances the node. */
9441 p->balance = 0;
9442 break;
9445 else /* r == p->right */
9447 if (p->balance == 0)
9448 /* Growth propagation from right side. */
9449 p->balance++;
9450 else if (p->balance > 0)
9452 if (r->balance > 0)
9454 /* R rotation. */
9455 p->right = r->left;
9456 if (p->right)
9457 p->right->parent = p;
9458 r->left = p;
9460 p->balance = 0;
9461 r->balance = 0;
9463 s = p->parent;
9464 p->parent = r;
9465 r->parent = s;
9466 if (s)
9468 if (s->left == p)
9469 s->left = r;
9470 else
9471 s->right = r;
9473 else
9474 constructor_pending_elts = r;
9476 else /* r->balance == -1 */
9478 /* RL rotation */
9479 struct init_node *t = r->left;
9481 r->left = t->right;
9482 if (r->left)
9483 r->left->parent = r;
9484 t->right = r;
9486 p->right = t->left;
9487 if (p->right)
9488 p->right->parent = p;
9489 t->left = p;
9491 r->balance = (t->balance < 0);
9492 p->balance = -(t->balance > 0);
9493 t->balance = 0;
9495 s = p->parent;
9496 p->parent = t;
9497 r->parent = t;
9498 t->parent = s;
9499 if (s)
9501 if (s->left == p)
9502 s->left = t;
9503 else
9504 s->right = t;
9506 else
9507 constructor_pending_elts = t;
9509 break;
9511 else
9513 /* p->balance == -1; growth of right side balances the node. */
9514 p->balance = 0;
9515 break;
9519 r = p;
9520 p = p->parent;
9524 /* Build AVL tree from a sorted chain. */
9526 static void
9527 set_nonincremental_init (struct obstack * braced_init_obstack)
9529 unsigned HOST_WIDE_INT ix;
9530 tree index, value;
9532 if (TREE_CODE (constructor_type) != RECORD_TYPE
9533 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9534 return;
9536 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9537 add_pending_init (input_location, index, value, NULL_TREE, true,
9538 braced_init_obstack);
9539 constructor_elements = NULL;
9540 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9542 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9543 /* Skip any nameless bit fields at the beginning. */
9544 while (constructor_unfilled_fields != NULL_TREE
9545 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9546 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9549 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9551 if (TYPE_DOMAIN (constructor_type))
9552 constructor_unfilled_index
9553 = convert (bitsizetype,
9554 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9555 else
9556 constructor_unfilled_index = bitsize_zero_node;
9558 constructor_incremental = 0;
9561 /* Build AVL tree from a string constant. */
9563 static void
9564 set_nonincremental_init_from_string (tree str,
9565 struct obstack * braced_init_obstack)
9567 tree value, purpose, type;
9568 HOST_WIDE_INT val[2];
9569 const char *p, *end;
9570 int byte, wchar_bytes, charwidth, bitpos;
9572 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9574 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9575 charwidth = TYPE_PRECISION (char_type_node);
9576 gcc_assert ((size_t) wchar_bytes * charwidth
9577 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9578 type = TREE_TYPE (constructor_type);
9579 p = TREE_STRING_POINTER (str);
9580 end = p + TREE_STRING_LENGTH (str);
9582 for (purpose = bitsize_zero_node;
9583 p < end
9584 && !(constructor_max_index
9585 && tree_int_cst_lt (constructor_max_index, purpose));
9586 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9588 if (wchar_bytes == 1)
9590 val[0] = (unsigned char) *p++;
9591 val[1] = 0;
9593 else
9595 val[1] = 0;
9596 val[0] = 0;
9597 for (byte = 0; byte < wchar_bytes; byte++)
9599 if (BYTES_BIG_ENDIAN)
9600 bitpos = (wchar_bytes - byte - 1) * charwidth;
9601 else
9602 bitpos = byte * charwidth;
9603 val[bitpos / HOST_BITS_PER_WIDE_INT]
9604 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9605 << (bitpos % HOST_BITS_PER_WIDE_INT);
9609 if (!TYPE_UNSIGNED (type))
9611 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9612 if (bitpos < HOST_BITS_PER_WIDE_INT)
9614 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9616 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9617 val[1] = -1;
9620 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9622 if (val[0] < 0)
9623 val[1] = -1;
9625 else if (val[1] & (HOST_WIDE_INT_1
9626 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9627 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9630 value = wide_int_to_tree (type,
9631 wide_int::from_array (val, 2,
9632 HOST_BITS_PER_WIDE_INT * 2));
9633 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9634 braced_init_obstack);
9637 constructor_incremental = 0;
9640 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9641 not initialized yet. */
9643 static tree
9644 find_init_member (tree field, struct obstack * braced_init_obstack)
9646 struct init_node *p;
9648 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9650 if (constructor_incremental
9651 && tree_int_cst_lt (field, constructor_unfilled_index))
9652 set_nonincremental_init (braced_init_obstack);
9654 p = constructor_pending_elts;
9655 while (p)
9657 if (tree_int_cst_lt (field, p->purpose))
9658 p = p->left;
9659 else if (tree_int_cst_lt (p->purpose, field))
9660 p = p->right;
9661 else
9662 return p->value;
9665 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9667 tree bitpos = bit_position (field);
9669 if (constructor_incremental
9670 && (!constructor_unfilled_fields
9671 || tree_int_cst_lt (bitpos,
9672 bit_position (constructor_unfilled_fields))))
9673 set_nonincremental_init (braced_init_obstack);
9675 p = constructor_pending_elts;
9676 while (p)
9678 if (field == p->purpose)
9679 return p->value;
9680 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9681 p = p->left;
9682 else
9683 p = p->right;
9686 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9688 if (!vec_safe_is_empty (constructor_elements)
9689 && (constructor_elements->last ().index == field))
9690 return constructor_elements->last ().value;
9692 return NULL_TREE;
9695 /* "Output" the next constructor element.
9696 At top level, really output it to assembler code now.
9697 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9698 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9699 TYPE is the data type that the containing data type wants here.
9700 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9701 If VALUE is a string constant, STRICT_STRING is true if it is
9702 unparenthesized or we should not warn here for it being parenthesized.
9703 For other types of VALUE, STRICT_STRING is not used.
9705 PENDING if true means output pending elements that belong
9706 right after this element. (PENDING is normally true;
9707 it is false while outputting pending elements, to avoid recursion.)
9709 IMPLICIT is true if value comes from pop_init_level (1),
9710 the new initializer has been merged with the existing one
9711 and thus no warnings should be emitted about overriding an
9712 existing initializer. */
9714 static void
9715 output_init_element (location_t loc, tree value, tree origtype,
9716 bool strict_string, tree type, tree field, bool pending,
9717 bool implicit, struct obstack * braced_init_obstack)
9719 tree semantic_type = NULL_TREE;
9720 bool maybe_const = true;
9721 bool npc;
9723 if (type == error_mark_node || value == error_mark_node)
9725 constructor_erroneous = 1;
9726 return;
9728 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9729 && (TREE_CODE (value) == STRING_CST
9730 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9731 && !(TREE_CODE (value) == STRING_CST
9732 && TREE_CODE (type) == ARRAY_TYPE
9733 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9734 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9735 TYPE_MAIN_VARIANT (type)))
9736 value = array_to_pointer_conversion (input_location, value);
9738 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9739 && require_constant_value && pending)
9741 /* As an extension, allow initializing objects with static storage
9742 duration with compound literals (which are then treated just as
9743 the brace enclosed list they contain). */
9744 if (flag_isoc99)
9745 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9746 "constant");
9747 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9748 value = DECL_INITIAL (decl);
9751 npc = null_pointer_constant_p (value);
9752 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9754 semantic_type = TREE_TYPE (value);
9755 value = TREE_OPERAND (value, 0);
9757 value = c_fully_fold (value, require_constant_value, &maybe_const);
9759 if (value == error_mark_node)
9760 constructor_erroneous = 1;
9761 else if (!TREE_CONSTANT (value))
9762 constructor_constant = 0;
9763 else if (!initializer_constant_valid_p (value,
9764 TREE_TYPE (value),
9765 AGGREGATE_TYPE_P (constructor_type)
9766 && TYPE_REVERSE_STORAGE_ORDER
9767 (constructor_type))
9768 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9769 && DECL_C_BIT_FIELD (field)
9770 && TREE_CODE (value) != INTEGER_CST))
9771 constructor_simple = 0;
9772 if (!maybe_const)
9773 constructor_nonconst = 1;
9775 /* Digest the initializer and issue any errors about incompatible
9776 types before issuing errors about non-constant initializers. */
9777 tree new_value = value;
9778 if (semantic_type)
9779 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9780 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9781 require_constant_value);
9782 if (new_value == error_mark_node)
9784 constructor_erroneous = 1;
9785 return;
9787 if (require_constant_value || require_constant_elements)
9788 constant_expression_warning (new_value);
9790 /* Proceed to check the constness of the original initializer. */
9791 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9793 if (require_constant_value)
9795 error_init (loc, "initializer element is not constant");
9796 value = error_mark_node;
9798 else if (require_constant_elements)
9799 pedwarn (loc, OPT_Wpedantic,
9800 "initializer element is not computable at load time");
9802 else if (!maybe_const
9803 && (require_constant_value || require_constant_elements))
9804 pedwarn_init (loc, OPT_Wpedantic,
9805 "initializer element is not a constant expression");
9807 /* Issue -Wc++-compat warnings about initializing a bitfield with
9808 enum type. */
9809 if (warn_cxx_compat
9810 && field != NULL_TREE
9811 && TREE_CODE (field) == FIELD_DECL
9812 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9813 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9814 != TYPE_MAIN_VARIANT (type))
9815 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9817 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9818 if (checktype != error_mark_node
9819 && (TYPE_MAIN_VARIANT (checktype)
9820 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9821 warning_init (loc, OPT_Wc___compat,
9822 "enum conversion in initialization is invalid in C++");
9825 /* If this field is empty and does not have side effects (and is not at
9826 the end of structure), don't do anything other than checking the
9827 initializer. */
9828 if (field
9829 && (TREE_TYPE (field) == error_mark_node
9830 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9831 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9832 && !TREE_SIDE_EFFECTS (new_value)
9833 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9834 || DECL_CHAIN (field)))))
9835 return;
9837 /* Finally, set VALUE to the initializer value digested above. */
9838 value = new_value;
9840 /* If this element doesn't come next in sequence,
9841 put it on constructor_pending_elts. */
9842 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9843 && (!constructor_incremental
9844 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9846 if (constructor_incremental
9847 && tree_int_cst_lt (field, constructor_unfilled_index))
9848 set_nonincremental_init (braced_init_obstack);
9850 add_pending_init (loc, field, value, origtype, implicit,
9851 braced_init_obstack);
9852 return;
9854 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9855 && (!constructor_incremental
9856 || field != constructor_unfilled_fields))
9858 /* We do this for records but not for unions. In a union,
9859 no matter which field is specified, it can be initialized
9860 right away since it starts at the beginning of the union. */
9861 if (constructor_incremental)
9863 if (!constructor_unfilled_fields)
9864 set_nonincremental_init (braced_init_obstack);
9865 else
9867 tree bitpos, unfillpos;
9869 bitpos = bit_position (field);
9870 unfillpos = bit_position (constructor_unfilled_fields);
9872 if (tree_int_cst_lt (bitpos, unfillpos))
9873 set_nonincremental_init (braced_init_obstack);
9877 add_pending_init (loc, field, value, origtype, implicit,
9878 braced_init_obstack);
9879 return;
9881 else if (TREE_CODE (constructor_type) == UNION_TYPE
9882 && !vec_safe_is_empty (constructor_elements))
9884 if (!implicit)
9886 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9887 warning_init (loc, OPT_Woverride_init_side_effects,
9888 "initialized field with side-effects overwritten");
9889 else if (warn_override_init)
9890 warning_init (loc, OPT_Woverride_init,
9891 "initialized field overwritten");
9894 /* We can have just one union field set. */
9895 constructor_elements = NULL;
9898 /* Otherwise, output this element either to
9899 constructor_elements or to the assembler file. */
9901 constructor_elt celt = {field, value};
9902 vec_safe_push (constructor_elements, celt);
9904 /* Advance the variable that indicates sequential elements output. */
9905 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9906 constructor_unfilled_index
9907 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9908 bitsize_one_node);
9909 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9911 constructor_unfilled_fields
9912 = DECL_CHAIN (constructor_unfilled_fields);
9914 /* Skip any nameless bit fields. */
9915 while (constructor_unfilled_fields != NULL_TREE
9916 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9917 constructor_unfilled_fields =
9918 DECL_CHAIN (constructor_unfilled_fields);
9920 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9921 constructor_unfilled_fields = NULL_TREE;
9923 /* Now output any pending elements which have become next. */
9924 if (pending)
9925 output_pending_init_elements (0, braced_init_obstack);
9928 /* For two FIELD_DECLs in the same chain, return -1 if field1
9929 comes before field2, 1 if field1 comes after field2 and
9930 0 if field1 == field2. */
9932 static int
9933 init_field_decl_cmp (tree field1, tree field2)
9935 if (field1 == field2)
9936 return 0;
9938 tree bitpos1 = bit_position (field1);
9939 tree bitpos2 = bit_position (field2);
9940 if (tree_int_cst_equal (bitpos1, bitpos2))
9942 /* If one of the fields has non-zero bitsize, then that
9943 field must be the last one in a sequence of zero
9944 sized fields, fields after it will have bigger
9945 bit_position. */
9946 if (TREE_TYPE (field1) != error_mark_node
9947 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9948 && integer_nonzerop (TREE_TYPE (field1)))
9949 return 1;
9950 if (TREE_TYPE (field2) != error_mark_node
9951 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9952 && integer_nonzerop (TREE_TYPE (field2)))
9953 return -1;
9954 /* Otherwise, fallback to DECL_CHAIN walk to find out
9955 which field comes earlier. Walk chains of both
9956 fields, so that if field1 and field2 are close to each
9957 other in either order, it is found soon even for large
9958 sequences of zero sized fields. */
9959 tree f1 = field1, f2 = field2;
9960 while (1)
9962 f1 = DECL_CHAIN (f1);
9963 f2 = DECL_CHAIN (f2);
9964 if (f1 == NULL_TREE)
9966 gcc_assert (f2);
9967 return 1;
9969 if (f2 == NULL_TREE)
9970 return -1;
9971 if (f1 == field2)
9972 return -1;
9973 if (f2 == field1)
9974 return 1;
9975 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9976 return 1;
9977 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9978 return -1;
9981 else if (tree_int_cst_lt (bitpos1, bitpos2))
9982 return -1;
9983 else
9984 return 1;
9987 /* Output any pending elements which have become next.
9988 As we output elements, constructor_unfilled_{fields,index}
9989 advances, which may cause other elements to become next;
9990 if so, they too are output.
9992 If ALL is 0, we return when there are
9993 no more pending elements to output now.
9995 If ALL is 1, we output space as necessary so that
9996 we can output all the pending elements. */
9997 static void
9998 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
10000 struct init_node *elt = constructor_pending_elts;
10001 tree next;
10003 retry:
10005 /* Look through the whole pending tree.
10006 If we find an element that should be output now,
10007 output it. Otherwise, set NEXT to the element
10008 that comes first among those still pending. */
10010 next = NULL_TREE;
10011 while (elt)
10013 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10015 if (tree_int_cst_equal (elt->purpose,
10016 constructor_unfilled_index))
10017 output_init_element (input_location, elt->value, elt->origtype,
10018 true, TREE_TYPE (constructor_type),
10019 constructor_unfilled_index, false, false,
10020 braced_init_obstack);
10021 else if (tree_int_cst_lt (constructor_unfilled_index,
10022 elt->purpose))
10024 /* Advance to the next smaller node. */
10025 if (elt->left)
10026 elt = elt->left;
10027 else
10029 /* We have reached the smallest node bigger than the
10030 current unfilled index. Fill the space first. */
10031 next = elt->purpose;
10032 break;
10035 else
10037 /* Advance to the next bigger node. */
10038 if (elt->right)
10039 elt = elt->right;
10040 else
10042 /* We have reached the biggest node in a subtree. Find
10043 the parent of it, which is the next bigger node. */
10044 while (elt->parent && elt->parent->right == elt)
10045 elt = elt->parent;
10046 elt = elt->parent;
10047 if (elt && tree_int_cst_lt (constructor_unfilled_index,
10048 elt->purpose))
10050 next = elt->purpose;
10051 break;
10056 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10058 /* If the current record is complete we are done. */
10059 if (constructor_unfilled_fields == NULL_TREE)
10060 break;
10062 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10063 elt->purpose);
10064 if (cmp == 0)
10065 output_init_element (input_location, elt->value, elt->origtype,
10066 true, TREE_TYPE (elt->purpose),
10067 elt->purpose, false, false,
10068 braced_init_obstack);
10069 else if (cmp < 0)
10071 /* Advance to the next smaller node. */
10072 if (elt->left)
10073 elt = elt->left;
10074 else
10076 /* We have reached the smallest node bigger than the
10077 current unfilled field. Fill the space first. */
10078 next = elt->purpose;
10079 break;
10082 else
10084 /* Advance to the next bigger node. */
10085 if (elt->right)
10086 elt = elt->right;
10087 else
10089 /* We have reached the biggest node in a subtree. Find
10090 the parent of it, which is the next bigger node. */
10091 while (elt->parent && elt->parent->right == elt)
10092 elt = elt->parent;
10093 elt = elt->parent;
10094 if (elt
10095 && init_field_decl_cmp (constructor_unfilled_fields,
10096 elt->purpose) < 0)
10098 next = elt->purpose;
10099 break;
10106 /* Ordinarily return, but not if we want to output all
10107 and there are elements left. */
10108 if (!(all && next != NULL_TREE))
10109 return;
10111 /* If it's not incremental, just skip over the gap, so that after
10112 jumping to retry we will output the next successive element. */
10113 if (RECORD_OR_UNION_TYPE_P (constructor_type))
10114 constructor_unfilled_fields = next;
10115 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10116 constructor_unfilled_index = next;
10118 /* ELT now points to the node in the pending tree with the next
10119 initializer to output. */
10120 goto retry;
10123 /* Expression VALUE coincides with the start of type TYPE in a braced
10124 initializer. Return true if we should treat VALUE as initializing
10125 the first element of TYPE, false if we should treat it as initializing
10126 TYPE as a whole.
10128 If the initializer is clearly invalid, the question becomes:
10129 which choice gives the best error message? */
10131 static bool
10132 initialize_elementwise_p (tree type, tree value)
10134 if (type == error_mark_node || value == error_mark_node)
10135 return false;
10137 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10139 tree value_type = TREE_TYPE (value);
10140 if (value_type == error_mark_node)
10141 return false;
10143 /* GNU vectors can be initialized elementwise. However, treat any
10144 kind of vector value as initializing the vector type as a whole,
10145 regardless of whether the value is a GNU vector. Such initializers
10146 are valid if and only if they would have been valid in a non-braced
10147 initializer like:
10149 TYPE foo = VALUE;
10151 so recursing into the vector type would be at best confusing or at
10152 worst wrong. For example, when -flax-vector-conversions is in effect,
10153 it's possible to initialize a V8HI from a V4SI, even though the vectors
10154 have different element types and different numbers of elements. */
10155 if (gnu_vector_type_p (type))
10156 return !VECTOR_TYPE_P (value_type);
10158 if (AGGREGATE_TYPE_P (type))
10159 return type != TYPE_MAIN_VARIANT (value_type);
10161 return false;
10164 /* Add one non-braced element to the current constructor level.
10165 This adjusts the current position within the constructor's type.
10166 This may also start or terminate implicit levels
10167 to handle a partly-braced initializer.
10169 Once this has found the correct level for the new element,
10170 it calls output_init_element.
10172 IMPLICIT is true if value comes from pop_init_level (1),
10173 the new initializer has been merged with the existing one
10174 and thus no warnings should be emitted about overriding an
10175 existing initializer. */
10177 void
10178 process_init_element (location_t loc, struct c_expr value, bool implicit,
10179 struct obstack * braced_init_obstack)
10181 tree orig_value = value.value;
10182 int string_flag
10183 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10184 bool strict_string = value.original_code == STRING_CST;
10185 bool was_designated = designator_depth != 0;
10187 designator_depth = 0;
10188 designator_erroneous = 0;
10190 if (!implicit && value.value && !integer_zerop (value.value))
10191 constructor_zeroinit = 0;
10193 /* Handle superfluous braces around string cst as in
10194 char x[] = {"foo"}; */
10195 if (string_flag
10196 && constructor_type
10197 && !was_designated
10198 && TREE_CODE (constructor_type) == ARRAY_TYPE
10199 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10200 && integer_zerop (constructor_unfilled_index))
10202 if (constructor_stack->replacement_value.value)
10203 error_init (loc, "excess elements in %<char%> array initializer");
10204 constructor_stack->replacement_value = value;
10205 return;
10208 if (constructor_stack->replacement_value.value != NULL_TREE)
10210 error_init (loc, "excess elements in struct initializer");
10211 return;
10214 /* Ignore elements of a brace group if it is entirely superfluous
10215 and has already been diagnosed, or if the type is erroneous. */
10216 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10217 return;
10219 /* Ignore elements of an initializer for a variable-size type.
10220 Those are diagnosed in digest_init. */
10221 if (COMPLETE_TYPE_P (constructor_type)
10222 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10223 return;
10225 if (!implicit && warn_designated_init && !was_designated
10226 && TREE_CODE (constructor_type) == RECORD_TYPE
10227 && lookup_attribute ("designated_init",
10228 TYPE_ATTRIBUTES (constructor_type)))
10229 warning_init (loc,
10230 OPT_Wdesignated_init,
10231 "positional initialization of field "
10232 "in %<struct%> declared with %<designated_init%> attribute");
10234 /* If we've exhausted any levels that didn't have braces,
10235 pop them now. */
10236 while (constructor_stack->implicit)
10238 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10239 && constructor_fields == NULL_TREE)
10240 process_init_element (loc,
10241 pop_init_level (loc, 1, braced_init_obstack,
10242 last_init_list_comma),
10243 true, braced_init_obstack);
10244 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10245 || gnu_vector_type_p (constructor_type))
10246 && constructor_max_index
10247 && tree_int_cst_lt (constructor_max_index,
10248 constructor_index))
10249 process_init_element (loc,
10250 pop_init_level (loc, 1, braced_init_obstack,
10251 last_init_list_comma),
10252 true, braced_init_obstack);
10253 else
10254 break;
10257 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10258 if (constructor_range_stack)
10260 /* If value is a compound literal and we'll be just using its
10261 content, don't put it into a SAVE_EXPR. */
10262 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10263 || !require_constant_value)
10265 tree semantic_type = NULL_TREE;
10266 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10268 semantic_type = TREE_TYPE (value.value);
10269 value.value = TREE_OPERAND (value.value, 0);
10271 value.value = save_expr (value.value);
10272 if (semantic_type)
10273 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10274 value.value);
10278 while (1)
10280 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10282 tree fieldtype;
10283 enum tree_code fieldcode;
10285 if (constructor_fields == NULL_TREE)
10287 pedwarn_init (loc, 0, "excess elements in struct initializer");
10288 break;
10291 fieldtype = TREE_TYPE (constructor_fields);
10292 if (fieldtype != error_mark_node)
10293 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10294 fieldcode = TREE_CODE (fieldtype);
10296 /* Error for non-static initialization of a flexible array member. */
10297 if (fieldcode == ARRAY_TYPE
10298 && !require_constant_value
10299 && TYPE_SIZE (fieldtype) == NULL_TREE
10300 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10302 error_init (loc, "non-static initialization of a flexible "
10303 "array member");
10304 break;
10307 /* Error for initialization of a flexible array member with
10308 a string constant if the structure is in an array. E.g.:
10309 struct S { int x; char y[]; };
10310 struct S s[] = { { 1, "foo" } };
10311 is invalid. */
10312 if (string_flag
10313 && fieldcode == ARRAY_TYPE
10314 && constructor_depth > 1
10315 && TYPE_SIZE (fieldtype) == NULL_TREE
10316 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10318 bool in_array_p = false;
10319 for (struct constructor_stack *p = constructor_stack;
10320 p && p->type; p = p->next)
10321 if (TREE_CODE (p->type) == ARRAY_TYPE)
10323 in_array_p = true;
10324 break;
10326 if (in_array_p)
10328 error_init (loc, "initialization of flexible array "
10329 "member in a nested context");
10330 break;
10334 /* Accept a string constant to initialize a subarray. */
10335 if (value.value != NULL_TREE
10336 && fieldcode == ARRAY_TYPE
10337 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10338 && string_flag)
10339 value.value = orig_value;
10340 /* Otherwise, if we have come to a subaggregate,
10341 and we don't have an element of its type, push into it. */
10342 else if (value.value != NULL_TREE
10343 && initialize_elementwise_p (fieldtype, value.value))
10345 push_init_level (loc, 1, braced_init_obstack);
10346 continue;
10349 if (value.value)
10351 push_member_name (constructor_fields);
10352 output_init_element (loc, value.value, value.original_type,
10353 strict_string, fieldtype,
10354 constructor_fields, true, implicit,
10355 braced_init_obstack);
10356 RESTORE_SPELLING_DEPTH (constructor_depth);
10358 else
10359 /* Do the bookkeeping for an element that was
10360 directly output as a constructor. */
10362 /* For a record, keep track of end position of last field. */
10363 if (DECL_SIZE (constructor_fields))
10364 constructor_bit_index
10365 = size_binop_loc (input_location, PLUS_EXPR,
10366 bit_position (constructor_fields),
10367 DECL_SIZE (constructor_fields));
10369 /* If the current field was the first one not yet written out,
10370 it isn't now, so update. */
10371 if (constructor_unfilled_fields == constructor_fields)
10373 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10374 /* Skip any nameless bit fields. */
10375 while (constructor_unfilled_fields != 0
10376 && (DECL_UNNAMED_BIT_FIELD
10377 (constructor_unfilled_fields)))
10378 constructor_unfilled_fields =
10379 DECL_CHAIN (constructor_unfilled_fields);
10383 constructor_fields = DECL_CHAIN (constructor_fields);
10384 /* Skip any nameless bit fields at the beginning. */
10385 while (constructor_fields != NULL_TREE
10386 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10387 constructor_fields = DECL_CHAIN (constructor_fields);
10389 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10391 tree fieldtype;
10392 enum tree_code fieldcode;
10394 if (constructor_fields == NULL_TREE)
10396 pedwarn_init (loc, 0,
10397 "excess elements in union initializer");
10398 break;
10401 fieldtype = TREE_TYPE (constructor_fields);
10402 if (fieldtype != error_mark_node)
10403 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10404 fieldcode = TREE_CODE (fieldtype);
10406 /* Warn that traditional C rejects initialization of unions.
10407 We skip the warning if the value is zero. This is done
10408 under the assumption that the zero initializer in user
10409 code appears conditioned on e.g. __STDC__ to avoid
10410 "missing initializer" warnings and relies on default
10411 initialization to zero in the traditional C case.
10412 We also skip the warning if the initializer is designated,
10413 again on the assumption that this must be conditional on
10414 __STDC__ anyway (and we've already complained about the
10415 member-designator already). */
10416 if (!in_system_header_at (input_location) && !constructor_designated
10417 && !(value.value && (integer_zerop (value.value)
10418 || real_zerop (value.value))))
10419 warning (OPT_Wtraditional, "traditional C rejects initialization "
10420 "of unions");
10422 /* Accept a string constant to initialize a subarray. */
10423 if (value.value != NULL_TREE
10424 && fieldcode == ARRAY_TYPE
10425 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10426 && string_flag)
10427 value.value = orig_value;
10428 /* Otherwise, if we have come to a subaggregate,
10429 and we don't have an element of its type, push into it. */
10430 else if (value.value != NULL_TREE
10431 && initialize_elementwise_p (fieldtype, value.value))
10433 push_init_level (loc, 1, braced_init_obstack);
10434 continue;
10437 if (value.value)
10439 push_member_name (constructor_fields);
10440 output_init_element (loc, value.value, value.original_type,
10441 strict_string, fieldtype,
10442 constructor_fields, true, implicit,
10443 braced_init_obstack);
10444 RESTORE_SPELLING_DEPTH (constructor_depth);
10446 else
10447 /* Do the bookkeeping for an element that was
10448 directly output as a constructor. */
10450 constructor_bit_index = DECL_SIZE (constructor_fields);
10451 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10454 constructor_fields = NULL_TREE;
10456 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10458 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10459 enum tree_code eltcode = TREE_CODE (elttype);
10461 /* Accept a string constant to initialize a subarray. */
10462 if (value.value != NULL_TREE
10463 && eltcode == ARRAY_TYPE
10464 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10465 && string_flag)
10466 value.value = orig_value;
10467 /* Otherwise, if we have come to a subaggregate,
10468 and we don't have an element of its type, push into it. */
10469 else if (value.value != NULL_TREE
10470 && initialize_elementwise_p (elttype, value.value))
10472 push_init_level (loc, 1, braced_init_obstack);
10473 continue;
10476 if (constructor_max_index != NULL_TREE
10477 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10478 || integer_all_onesp (constructor_max_index)))
10480 pedwarn_init (loc, 0,
10481 "excess elements in array initializer");
10482 break;
10485 /* Now output the actual element. */
10486 if (value.value)
10488 push_array_bounds (tree_to_uhwi (constructor_index));
10489 output_init_element (loc, value.value, value.original_type,
10490 strict_string, elttype,
10491 constructor_index, true, implicit,
10492 braced_init_obstack);
10493 RESTORE_SPELLING_DEPTH (constructor_depth);
10496 constructor_index
10497 = size_binop_loc (input_location, PLUS_EXPR,
10498 constructor_index, bitsize_one_node);
10500 if (!value.value)
10501 /* If we are doing the bookkeeping for an element that was
10502 directly output as a constructor, we must update
10503 constructor_unfilled_index. */
10504 constructor_unfilled_index = constructor_index;
10506 else if (gnu_vector_type_p (constructor_type))
10508 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10510 /* Do a basic check of initializer size. Note that vectors
10511 always have a fixed size derived from their type. */
10512 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10514 pedwarn_init (loc, 0,
10515 "excess elements in vector initializer");
10516 break;
10519 /* Now output the actual element. */
10520 if (value.value)
10522 if (TREE_CODE (value.value) == VECTOR_CST)
10523 elttype = TYPE_MAIN_VARIANT (constructor_type);
10524 output_init_element (loc, value.value, value.original_type,
10525 strict_string, elttype,
10526 constructor_index, true, implicit,
10527 braced_init_obstack);
10530 constructor_index
10531 = size_binop_loc (input_location,
10532 PLUS_EXPR, constructor_index, bitsize_one_node);
10534 if (!value.value)
10535 /* If we are doing the bookkeeping for an element that was
10536 directly output as a constructor, we must update
10537 constructor_unfilled_index. */
10538 constructor_unfilled_index = constructor_index;
10541 /* Handle the sole element allowed in a braced initializer
10542 for a scalar variable. */
10543 else if (constructor_type != error_mark_node
10544 && constructor_fields == NULL_TREE)
10546 pedwarn_init (loc, 0,
10547 "excess elements in scalar initializer");
10548 break;
10550 else
10552 if (value.value)
10553 output_init_element (loc, value.value, value.original_type,
10554 strict_string, constructor_type,
10555 NULL_TREE, true, implicit,
10556 braced_init_obstack);
10557 constructor_fields = NULL_TREE;
10560 /* Handle range initializers either at this level or anywhere higher
10561 in the designator stack. */
10562 if (constructor_range_stack)
10564 struct constructor_range_stack *p, *range_stack;
10565 int finish = 0;
10567 range_stack = constructor_range_stack;
10568 constructor_range_stack = 0;
10569 while (constructor_stack != range_stack->stack)
10571 gcc_assert (constructor_stack->implicit);
10572 process_init_element (loc,
10573 pop_init_level (loc, 1,
10574 braced_init_obstack,
10575 last_init_list_comma),
10576 true, braced_init_obstack);
10578 for (p = range_stack;
10579 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10580 p = p->prev)
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);
10590 p->index = size_binop_loc (input_location,
10591 PLUS_EXPR, p->index, bitsize_one_node);
10592 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10593 finish = 1;
10595 while (1)
10597 constructor_index = p->index;
10598 constructor_fields = p->fields;
10599 if (finish && p->range_end && p->index == p->range_start)
10601 finish = 0;
10602 p->prev = 0;
10604 p = p->next;
10605 if (!p)
10606 break;
10607 finish_implicit_inits (loc, braced_init_obstack);
10608 push_init_level (loc, 2, braced_init_obstack);
10609 p->stack = constructor_stack;
10610 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10611 p->index = p->range_start;
10614 if (!finish)
10615 constructor_range_stack = range_stack;
10616 continue;
10619 break;
10622 constructor_range_stack = 0;
10625 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10626 (guaranteed to be 'volatile' or null) and ARGS (represented using
10627 an ASM_EXPR node). */
10628 tree
10629 build_asm_stmt (bool is_volatile, tree args)
10631 if (is_volatile)
10632 ASM_VOLATILE_P (args) = 1;
10633 return add_stmt (args);
10636 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10637 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10638 SIMPLE indicates whether there was anything at all after the
10639 string in the asm expression -- asm("blah") and asm("blah" : )
10640 are subtly different. We use a ASM_EXPR node to represent this.
10641 LOC is the location of the asm, and IS_INLINE says whether this
10642 is asm inline. */
10643 tree
10644 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10645 tree clobbers, tree labels, bool simple, bool is_inline)
10647 tree tail;
10648 tree args;
10649 int i;
10650 const char *constraint;
10651 const char **oconstraints;
10652 bool allows_mem, allows_reg, is_inout;
10653 int ninputs, noutputs;
10655 ninputs = list_length (inputs);
10656 noutputs = list_length (outputs);
10657 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10659 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10661 /* Remove output conversions that change the type but not the mode. */
10662 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10664 tree output = TREE_VALUE (tail);
10666 output = c_fully_fold (output, false, NULL, true);
10668 /* ??? Really, this should not be here. Users should be using a
10669 proper lvalue, dammit. But there's a long history of using casts
10670 in the output operands. In cases like longlong.h, this becomes a
10671 primitive form of typechecking -- if the cast can be removed, then
10672 the output operand had a type of the proper width; otherwise we'll
10673 get an error. Gross, but ... */
10674 STRIP_NOPS (output);
10676 if (!lvalue_or_else (loc, output, lv_asm))
10677 output = error_mark_node;
10679 if (output != error_mark_node
10680 && (TREE_READONLY (output)
10681 || TYPE_READONLY (TREE_TYPE (output))
10682 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10683 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10684 readonly_error (loc, output, lv_asm);
10686 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10687 oconstraints[i] = constraint;
10689 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10690 &allows_mem, &allows_reg, &is_inout))
10692 /* If the operand is going to end up in memory,
10693 mark it addressable. */
10694 if (!allows_reg && !c_mark_addressable (output))
10695 output = error_mark_node;
10696 if (!(!allows_reg && allows_mem)
10697 && output != error_mark_node
10698 && VOID_TYPE_P (TREE_TYPE (output)))
10700 error_at (loc, "invalid use of void expression");
10701 output = error_mark_node;
10704 else
10705 output = error_mark_node;
10707 TREE_VALUE (tail) = output;
10710 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10712 tree input;
10714 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10715 input = TREE_VALUE (tail);
10717 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10718 oconstraints, &allows_mem, &allows_reg))
10720 /* If the operand is going to end up in memory,
10721 mark it addressable. */
10722 if (!allows_reg && allows_mem)
10724 input = c_fully_fold (input, false, NULL, true);
10726 /* Strip the nops as we allow this case. FIXME, this really
10727 should be rejected or made deprecated. */
10728 STRIP_NOPS (input);
10729 if (!c_mark_addressable (input))
10730 input = error_mark_node;
10732 else
10734 struct c_expr expr;
10735 memset (&expr, 0, sizeof (expr));
10736 expr.value = input;
10737 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10738 input = c_fully_fold (expr.value, false, NULL);
10740 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10742 error_at (loc, "invalid use of void expression");
10743 input = error_mark_node;
10747 else
10748 input = error_mark_node;
10750 TREE_VALUE (tail) = input;
10753 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10755 /* asm statements without outputs, including simple ones, are treated
10756 as volatile. */
10757 ASM_INPUT_P (args) = simple;
10758 ASM_VOLATILE_P (args) = (noutputs == 0);
10759 ASM_INLINE_P (args) = is_inline;
10761 return args;
10764 /* Generate a goto statement to LABEL. LOC is the location of the
10765 GOTO. */
10767 tree
10768 c_finish_goto_label (location_t loc, tree label)
10770 tree decl = lookup_label_for_goto (loc, label);
10771 if (!decl)
10772 return NULL_TREE;
10773 TREE_USED (decl) = 1;
10775 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10776 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10777 SET_EXPR_LOCATION (t, loc);
10778 return add_stmt (t);
10782 /* Generate a computed goto statement to EXPR. LOC is the location of
10783 the GOTO. */
10785 tree
10786 c_finish_goto_ptr (location_t loc, c_expr val)
10788 tree expr = val.value;
10789 tree t;
10790 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10791 if (expr != error_mark_node
10792 && !POINTER_TYPE_P (TREE_TYPE (expr))
10793 && !null_pointer_constant_p (expr))
10795 error_at (val.get_location (),
10796 "computed goto must be pointer type");
10797 expr = build_zero_cst (ptr_type_node);
10799 expr = c_fully_fold (expr, false, NULL);
10800 expr = convert (ptr_type_node, expr);
10801 t = build1 (GOTO_EXPR, void_type_node, expr);
10802 SET_EXPR_LOCATION (t, loc);
10803 return add_stmt (t);
10806 /* Generate a C `return' statement. RETVAL is the expression for what
10807 to return, or a null pointer for `return;' with no value. LOC is
10808 the location of the return statement, or the location of the expression,
10809 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10810 is the original type of RETVAL. */
10812 tree
10813 c_finish_return (location_t loc, tree retval, tree origtype)
10815 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10816 bool no_warning = false;
10817 bool npc = false;
10819 /* Use the expansion point to handle cases such as returning NULL
10820 in a function returning void. */
10821 location_t xloc = expansion_point_location_if_in_system_header (loc);
10823 if (TREE_THIS_VOLATILE (current_function_decl))
10824 warning_at (xloc, 0,
10825 "function declared %<noreturn%> has a %<return%> statement");
10827 if (retval)
10829 tree semantic_type = NULL_TREE;
10830 npc = null_pointer_constant_p (retval);
10831 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10833 semantic_type = TREE_TYPE (retval);
10834 retval = TREE_OPERAND (retval, 0);
10836 retval = c_fully_fold (retval, false, NULL);
10837 if (semantic_type
10838 && valtype != NULL_TREE
10839 && TREE_CODE (valtype) != VOID_TYPE)
10840 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10843 if (!retval)
10845 current_function_returns_null = 1;
10846 if ((warn_return_type >= 0 || flag_isoc99)
10847 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10849 bool warned_here;
10850 if (flag_isoc99)
10851 warned_here = pedwarn
10852 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10853 "%<return%> with no value, in function returning non-void");
10854 else
10855 warned_here = warning_at
10856 (loc, OPT_Wreturn_type,
10857 "%<return%> with no value, in function returning non-void");
10858 no_warning = true;
10859 if (warned_here)
10860 inform (DECL_SOURCE_LOCATION (current_function_decl),
10861 "declared here");
10864 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10866 current_function_returns_null = 1;
10867 bool warned_here;
10868 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10869 warned_here = pedwarn
10870 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10871 "%<return%> with a value, in function returning void");
10872 else
10873 warned_here = pedwarn
10874 (xloc, OPT_Wpedantic, "ISO C forbids "
10875 "%<return%> with expression, in function returning void");
10876 if (warned_here)
10877 inform (DECL_SOURCE_LOCATION (current_function_decl),
10878 "declared here");
10880 else
10882 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10883 retval, origtype, ic_return,
10884 npc, NULL_TREE, NULL_TREE, 0);
10885 tree res = DECL_RESULT (current_function_decl);
10886 tree inner;
10887 bool save;
10889 current_function_returns_value = 1;
10890 if (t == error_mark_node)
10891 return NULL_TREE;
10893 save = in_late_binary_op;
10894 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10895 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10896 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10897 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10898 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10899 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10900 in_late_binary_op = true;
10901 inner = t = convert (TREE_TYPE (res), t);
10902 in_late_binary_op = save;
10904 /* Strip any conversions, additions, and subtractions, and see if
10905 we are returning the address of a local variable. Warn if so. */
10906 while (1)
10908 switch (TREE_CODE (inner))
10910 CASE_CONVERT:
10911 case NON_LVALUE_EXPR:
10912 case PLUS_EXPR:
10913 case POINTER_PLUS_EXPR:
10914 inner = TREE_OPERAND (inner, 0);
10915 continue;
10917 case MINUS_EXPR:
10918 /* If the second operand of the MINUS_EXPR has a pointer
10919 type (or is converted from it), this may be valid, so
10920 don't give a warning. */
10922 tree op1 = TREE_OPERAND (inner, 1);
10924 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10925 && (CONVERT_EXPR_P (op1)
10926 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10927 op1 = TREE_OPERAND (op1, 0);
10929 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10930 break;
10932 inner = TREE_OPERAND (inner, 0);
10933 continue;
10936 case ADDR_EXPR:
10937 inner = TREE_OPERAND (inner, 0);
10939 while (REFERENCE_CLASS_P (inner)
10940 && !INDIRECT_REF_P (inner))
10941 inner = TREE_OPERAND (inner, 0);
10943 if (DECL_P (inner)
10944 && !DECL_EXTERNAL (inner)
10945 && !TREE_STATIC (inner)
10946 && DECL_CONTEXT (inner) == current_function_decl
10947 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
10949 if (TREE_CODE (inner) == LABEL_DECL)
10950 warning_at (loc, OPT_Wreturn_local_addr,
10951 "function returns address of label");
10952 else
10954 warning_at (loc, OPT_Wreturn_local_addr,
10955 "function returns address of local variable");
10956 tree zero = build_zero_cst (TREE_TYPE (res));
10957 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10960 break;
10962 default:
10963 break;
10966 break;
10969 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10970 SET_EXPR_LOCATION (retval, loc);
10972 if (warn_sequence_point)
10973 verify_sequence_points (retval);
10976 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10977 if (no_warning)
10978 suppress_warning (ret_stmt, OPT_Wreturn_type);
10979 return add_stmt (ret_stmt);
10982 struct c_switch {
10983 /* The SWITCH_STMT being built. */
10984 tree switch_stmt;
10986 /* The original type of the testing expression, i.e. before the
10987 default conversion is applied. */
10988 tree orig_type;
10990 /* A splay-tree mapping the low element of a case range to the high
10991 element, or NULL_TREE if there is no high element. Used to
10992 determine whether or not a new case label duplicates an old case
10993 label. We need a tree, rather than simply a hash table, because
10994 of the GNU case range extension. */
10995 splay_tree cases;
10997 /* The bindings at the point of the switch. This is used for
10998 warnings crossing decls when branching to a case label. */
10999 struct c_spot_bindings *bindings;
11001 /* Whether the switch includes any break statements. */
11002 bool break_stmt_seen_p;
11004 /* The next node on the stack. */
11005 struct c_switch *next;
11007 /* Remember whether the controlling expression had boolean type
11008 before integer promotions for the sake of -Wswitch-bool. */
11009 bool bool_cond_p;
11012 /* A stack of the currently active switch statements. The innermost
11013 switch statement is on the top of the stack. There is no need to
11014 mark the stack for garbage collection because it is only active
11015 during the processing of the body of a function, and we never
11016 collect at that point. */
11018 struct c_switch *c_switch_stack;
11020 /* Start a C switch statement, testing expression EXP. Return the new
11021 SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
11022 SWITCH_COND_LOC is the location of the switch's condition.
11023 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
11025 tree
11026 c_start_switch (location_t switch_loc,
11027 location_t switch_cond_loc,
11028 tree exp, bool explicit_cast_p)
11030 tree orig_type = error_mark_node;
11031 bool bool_cond_p = false;
11032 struct c_switch *cs;
11034 if (exp != error_mark_node)
11036 orig_type = TREE_TYPE (exp);
11038 if (!INTEGRAL_TYPE_P (orig_type))
11040 if (orig_type != error_mark_node)
11042 error_at (switch_cond_loc, "switch quantity not an integer");
11043 orig_type = error_mark_node;
11045 exp = integer_zero_node;
11047 else
11049 tree type = TYPE_MAIN_VARIANT (orig_type);
11050 tree e = exp;
11052 /* Warn if the condition has boolean value. */
11053 while (TREE_CODE (e) == COMPOUND_EXPR)
11054 e = TREE_OPERAND (e, 1);
11056 if ((TREE_CODE (type) == BOOLEAN_TYPE
11057 || truth_value_p (TREE_CODE (e)))
11058 /* Explicit cast to int suppresses this warning. */
11059 && !(TREE_CODE (type) == INTEGER_TYPE
11060 && explicit_cast_p))
11061 bool_cond_p = true;
11063 if (!in_system_header_at (input_location)
11064 && (type == long_integer_type_node
11065 || type == long_unsigned_type_node))
11066 warning_at (switch_cond_loc,
11067 OPT_Wtraditional, "%<long%> switch expression not "
11068 "converted to %<int%> in ISO C");
11070 exp = c_fully_fold (exp, false, NULL);
11071 exp = default_conversion (exp);
11073 if (warn_sequence_point)
11074 verify_sequence_points (exp);
11078 /* Add this new SWITCH_STMT to the stack. */
11079 cs = XNEW (struct c_switch);
11080 cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11081 NULL_TREE, orig_type, NULL_TREE);
11082 cs->orig_type = orig_type;
11083 cs->cases = splay_tree_new (case_compare, NULL, NULL);
11084 cs->bindings = c_get_switch_bindings ();
11085 cs->break_stmt_seen_p = false;
11086 cs->bool_cond_p = bool_cond_p;
11087 cs->next = c_switch_stack;
11088 c_switch_stack = cs;
11090 return add_stmt (cs->switch_stmt);
11093 /* Process a case label at location LOC. */
11095 tree
11096 do_case (location_t loc, tree low_value, tree high_value)
11098 tree label = NULL_TREE;
11100 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11102 low_value = c_fully_fold (low_value, false, NULL);
11103 if (TREE_CODE (low_value) == INTEGER_CST)
11104 pedwarn (loc, OPT_Wpedantic,
11105 "case label is not an integer constant expression");
11108 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11110 high_value = c_fully_fold (high_value, false, NULL);
11111 if (TREE_CODE (high_value) == INTEGER_CST)
11112 pedwarn (input_location, OPT_Wpedantic,
11113 "case label is not an integer constant expression");
11116 if (c_switch_stack == NULL)
11118 if (low_value)
11119 error_at (loc, "case label not within a switch statement");
11120 else
11121 error_at (loc, "%<default%> label not within a switch statement");
11122 return NULL_TREE;
11125 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11126 EXPR_LOCATION (c_switch_stack->switch_stmt),
11127 loc))
11128 return NULL_TREE;
11130 label = c_add_case_label (loc, c_switch_stack->cases,
11131 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11132 low_value, high_value);
11133 if (label == error_mark_node)
11134 label = NULL_TREE;
11135 return label;
11138 /* Finish the switch statement. TYPE is the original type of the
11139 controlling expression of the switch, or NULL_TREE. */
11141 void
11142 c_finish_switch (tree body, tree type)
11144 struct c_switch *cs = c_switch_stack;
11145 location_t switch_location;
11147 SWITCH_STMT_BODY (cs->switch_stmt) = body;
11149 /* Emit warnings as needed. */
11150 switch_location = EXPR_LOCATION (cs->switch_stmt);
11151 c_do_switch_warnings (cs->cases, switch_location,
11152 type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11153 SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11154 if (c_switch_covers_all_cases_p (cs->cases,
11155 SWITCH_STMT_TYPE (cs->switch_stmt)))
11156 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11157 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11159 /* Pop the stack. */
11160 c_switch_stack = cs->next;
11161 splay_tree_delete (cs->cases);
11162 c_release_switch_bindings (cs->bindings);
11163 XDELETE (cs);
11166 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
11167 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11168 may be null. */
11170 void
11171 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11172 tree else_block)
11174 tree stmt;
11176 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11177 SET_EXPR_LOCATION (stmt, if_locus);
11178 add_stmt (stmt);
11181 tree
11182 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11184 /* In switch statements break is sometimes stylistically used after
11185 a return statement. This can lead to spurious warnings about
11186 control reaching the end of a non-void function when it is
11187 inlined. Note that we are calling block_may_fallthru with
11188 language specific tree nodes; this works because
11189 block_may_fallthru returns true when given something it does not
11190 understand. */
11191 bool skip = !block_may_fallthru (cur_stmt_list);
11193 if (is_break)
11194 switch (in_statement)
11196 case 0:
11197 error_at (loc, "break statement not within loop or switch");
11198 return NULL_TREE;
11199 case IN_OMP_BLOCK:
11200 error_at (loc, "invalid exit from OpenMP structured block");
11201 return NULL_TREE;
11202 case IN_OMP_FOR:
11203 error_at (loc, "break statement used with OpenMP for loop");
11204 return NULL_TREE;
11205 case IN_ITERATION_STMT:
11206 case IN_OBJC_FOREACH:
11207 break;
11208 default:
11209 gcc_assert (in_statement & IN_SWITCH_STMT);
11210 c_switch_stack->break_stmt_seen_p = true;
11211 break;
11213 else
11214 switch (in_statement & ~IN_SWITCH_STMT)
11216 case 0:
11217 error_at (loc, "continue statement not within a loop");
11218 return NULL_TREE;
11219 case IN_OMP_BLOCK:
11220 error_at (loc, "invalid exit from OpenMP structured block");
11221 return NULL_TREE;
11222 case IN_ITERATION_STMT:
11223 case IN_OMP_FOR:
11224 case IN_OBJC_FOREACH:
11225 break;
11226 default:
11227 gcc_unreachable ();
11230 if (skip)
11231 return NULL_TREE;
11232 else if (in_statement & IN_OBJC_FOREACH)
11234 /* The foreach expander produces low-level code using gotos instead
11235 of a structured loop construct. */
11236 gcc_assert (label);
11237 return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11239 return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11242 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11244 static void
11245 emit_side_effect_warnings (location_t loc, tree expr)
11247 maybe_warn_nodiscard (loc, expr);
11248 if (!warn_unused_value)
11249 return;
11250 if (expr == error_mark_node)
11252 else if (!TREE_SIDE_EFFECTS (expr))
11254 if (!VOID_TYPE_P (TREE_TYPE (expr))
11255 && !warning_suppressed_p (expr, OPT_Wunused_value))
11256 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11258 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11260 tree r = expr;
11261 location_t cloc = loc;
11262 while (TREE_CODE (r) == COMPOUND_EXPR)
11264 if (EXPR_HAS_LOCATION (r))
11265 cloc = EXPR_LOCATION (r);
11266 r = TREE_OPERAND (r, 1);
11268 if (!TREE_SIDE_EFFECTS (r)
11269 && !VOID_TYPE_P (TREE_TYPE (r))
11270 && !CONVERT_EXPR_P (r)
11271 && !warning_suppressed_p (r, OPT_Wunused_value)
11272 && !warning_suppressed_p (expr, OPT_Wunused_value))
11273 warning_at (cloc, OPT_Wunused_value,
11274 "right-hand operand of comma expression has no effect");
11276 else
11277 warn_if_unused_value (expr, loc);
11280 /* Process an expression as if it were a complete statement. Emit
11281 diagnostics, but do not call ADD_STMT. LOC is the location of the
11282 statement. */
11284 tree
11285 c_process_expr_stmt (location_t loc, tree expr)
11287 tree exprv;
11289 if (!expr)
11290 return NULL_TREE;
11292 expr = c_fully_fold (expr, false, NULL);
11294 if (warn_sequence_point)
11295 verify_sequence_points (expr);
11297 if (TREE_TYPE (expr) != error_mark_node
11298 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11299 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11300 error_at (loc, "expression statement has incomplete type");
11302 /* If we're not processing a statement expression, warn about unused values.
11303 Warnings for statement expressions will be emitted later, once we figure
11304 out which is the result. */
11305 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11306 && (warn_unused_value || warn_unused_result))
11307 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11309 exprv = expr;
11310 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11311 exprv = TREE_OPERAND (exprv, 1);
11312 while (CONVERT_EXPR_P (exprv))
11313 exprv = TREE_OPERAND (exprv, 0);
11314 if (DECL_P (exprv)
11315 || handled_component_p (exprv)
11316 || TREE_CODE (exprv) == ADDR_EXPR)
11317 mark_exp_read (exprv);
11319 /* If the expression is not of a type to which we cannot assign a line
11320 number, wrap the thing in a no-op NOP_EXPR. */
11321 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11323 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11324 SET_EXPR_LOCATION (expr, loc);
11327 return expr;
11330 /* Emit an expression as a statement. LOC is the location of the
11331 expression. */
11333 tree
11334 c_finish_expr_stmt (location_t loc, tree expr)
11336 if (expr)
11337 return add_stmt (c_process_expr_stmt (loc, expr));
11338 else
11339 return NULL;
11342 /* Do the opposite and emit a statement as an expression. To begin,
11343 create a new binding level and return it. */
11345 tree
11346 c_begin_stmt_expr (void)
11348 tree ret;
11350 /* We must force a BLOCK for this level so that, if it is not expanded
11351 later, there is a way to turn off the entire subtree of blocks that
11352 are contained in it. */
11353 keep_next_level ();
11354 ret = c_begin_compound_stmt (true);
11356 c_bindings_start_stmt_expr (c_switch_stack == NULL
11357 ? NULL
11358 : c_switch_stack->bindings);
11360 /* Mark the current statement list as belonging to a statement list. */
11361 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11363 return ret;
11366 /* LOC is the location of the compound statement to which this body
11367 belongs. */
11369 tree
11370 c_finish_stmt_expr (location_t loc, tree body)
11372 tree last, type, tmp, val;
11373 tree *last_p;
11375 body = c_end_compound_stmt (loc, body, true);
11377 c_bindings_end_stmt_expr (c_switch_stack == NULL
11378 ? NULL
11379 : c_switch_stack->bindings);
11381 /* Locate the last statement in BODY. See c_end_compound_stmt
11382 about always returning a BIND_EXPR. */
11383 last_p = &BIND_EXPR_BODY (body);
11384 last = BIND_EXPR_BODY (body);
11386 continue_searching:
11387 if (TREE_CODE (last) == STATEMENT_LIST)
11389 tree_stmt_iterator l = tsi_last (last);
11391 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11392 tsi_prev (&l);
11394 /* This can happen with degenerate cases like ({ }). No value. */
11395 if (tsi_end_p (l))
11396 return body;
11398 /* If we're supposed to generate side effects warnings, process
11399 all of the statements except the last. */
11400 if (warn_unused_value || warn_unused_result)
11402 for (tree_stmt_iterator i = tsi_start (last);
11403 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11405 location_t tloc;
11406 tree t = tsi_stmt (i);
11408 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11409 emit_side_effect_warnings (tloc, t);
11412 last_p = tsi_stmt_ptr (l);
11413 last = *last_p;
11416 /* If the end of the list is exception related, then the list was split
11417 by a call to push_cleanup. Continue searching. */
11418 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11419 || TREE_CODE (last) == TRY_CATCH_EXPR)
11421 last_p = &TREE_OPERAND (last, 0);
11422 last = *last_p;
11423 goto continue_searching;
11426 if (last == error_mark_node)
11427 return last;
11429 /* In the case that the BIND_EXPR is not necessary, return the
11430 expression out from inside it. */
11431 if ((last == BIND_EXPR_BODY (body)
11432 /* Skip nested debug stmts. */
11433 || last == expr_first (BIND_EXPR_BODY (body)))
11434 && BIND_EXPR_VARS (body) == NULL)
11436 /* Even if this looks constant, do not allow it in a constant
11437 expression. */
11438 last = c_wrap_maybe_const (last, true);
11439 /* Do not warn if the return value of a statement expression is
11440 unused. */
11441 suppress_warning (last, OPT_Wunused);
11442 return last;
11445 /* Extract the type of said expression. */
11446 type = TREE_TYPE (last);
11448 /* If we're not returning a value at all, then the BIND_EXPR that
11449 we already have is a fine expression to return. */
11450 if (!type || VOID_TYPE_P (type))
11451 return body;
11453 /* Now that we've located the expression containing the value, it seems
11454 silly to make voidify_wrapper_expr repeat the process. Create a
11455 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11456 tmp = create_tmp_var_raw (type);
11458 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11459 tree_expr_nonnegative_p giving up immediately. */
11460 val = last;
11461 if (TREE_CODE (val) == NOP_EXPR
11462 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11463 val = TREE_OPERAND (val, 0);
11465 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11466 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11469 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11470 SET_EXPR_LOCATION (t, loc);
11471 return t;
11475 /* Begin and end compound statements. This is as simple as pushing
11476 and popping new statement lists from the tree. */
11478 tree
11479 c_begin_compound_stmt (bool do_scope)
11481 tree stmt = push_stmt_list ();
11482 if (do_scope)
11483 push_scope ();
11484 return stmt;
11487 /* End a compound statement. STMT is the statement. LOC is the
11488 location of the compound statement-- this is usually the location
11489 of the opening brace. */
11491 tree
11492 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11494 tree block = NULL;
11496 if (do_scope)
11498 if (c_dialect_objc ())
11499 objc_clear_super_receiver ();
11500 block = pop_scope ();
11503 stmt = pop_stmt_list (stmt);
11504 stmt = c_build_bind_expr (loc, block, stmt);
11506 /* If this compound statement is nested immediately inside a statement
11507 expression, then force a BIND_EXPR to be created. Otherwise we'll
11508 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11509 STATEMENT_LISTs merge, and thus we can lose track of what statement
11510 was really last. */
11511 if (building_stmt_list_p ()
11512 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11513 && TREE_CODE (stmt) != BIND_EXPR)
11515 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11516 TREE_SIDE_EFFECTS (stmt) = 1;
11517 SET_EXPR_LOCATION (stmt, loc);
11520 return stmt;
11523 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11524 when the current scope is exited. EH_ONLY is true when this is not
11525 meant to apply to normal control flow transfer. */
11527 void
11528 push_cleanup (tree decl, tree cleanup, bool eh_only)
11530 enum tree_code code;
11531 tree stmt, list;
11532 bool stmt_expr;
11534 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11535 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11536 add_stmt (stmt);
11537 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11538 list = push_stmt_list ();
11539 TREE_OPERAND (stmt, 0) = list;
11540 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11543 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11544 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11546 static tree
11547 build_vec_cmp (tree_code code, tree type,
11548 tree arg0, tree arg1)
11550 tree zero_vec = build_zero_cst (type);
11551 tree minus_one_vec = build_minus_one_cst (type);
11552 tree cmp_type = truth_type_for (type);
11553 tree cmp = build2 (code, cmp_type, arg0, arg1);
11554 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11557 /* Build a binary-operation expression without default conversions.
11558 CODE is the kind of expression to build.
11559 LOCATION is the operator's location.
11560 This function differs from `build' in several ways:
11561 the data type of the result is computed and recorded in it,
11562 warnings are generated if arg data types are invalid,
11563 special handling for addition and subtraction of pointers is known,
11564 and some optimization is done (operations on narrow ints
11565 are done in the narrower type when that gives the same result).
11566 Constant folding is also done before the result is returned.
11568 Note that the operands will never have enumeral types, or function
11569 or array types, because either they will have the default conversions
11570 performed or they have both just been converted to some other type in which
11571 the arithmetic is to be done. */
11573 tree
11574 build_binary_op (location_t location, enum tree_code code,
11575 tree orig_op0, tree orig_op1, bool convert_p)
11577 tree type0, type1, orig_type0, orig_type1;
11578 tree eptype;
11579 enum tree_code code0, code1;
11580 tree op0, op1;
11581 tree ret = error_mark_node;
11582 const char *invalid_op_diag;
11583 bool op0_int_operands, op1_int_operands;
11584 bool int_const, int_const_or_overflow, int_operands;
11586 /* Expression code to give to the expression when it is built.
11587 Normally this is CODE, which is what the caller asked for,
11588 but in some special cases we change it. */
11589 enum tree_code resultcode = code;
11591 /* Data type in which the computation is to be performed.
11592 In the simplest cases this is the common type of the arguments. */
11593 tree result_type = NULL;
11595 /* When the computation is in excess precision, the type of the
11596 final EXCESS_PRECISION_EXPR. */
11597 tree semantic_result_type = NULL;
11599 /* Nonzero means operands have already been type-converted
11600 in whatever way is necessary.
11601 Zero means they need to be converted to RESULT_TYPE. */
11602 int converted = 0;
11604 /* Nonzero means create the expression with this type, rather than
11605 RESULT_TYPE. */
11606 tree build_type = NULL_TREE;
11608 /* Nonzero means after finally constructing the expression
11609 convert it to this type. */
11610 tree final_type = NULL_TREE;
11612 /* Nonzero if this is an operation like MIN or MAX which can
11613 safely be computed in short if both args are promoted shorts.
11614 Also implies COMMON.
11615 -1 indicates a bitwise operation; this makes a difference
11616 in the exact conditions for when it is safe to do the operation
11617 in a narrower mode. */
11618 int shorten = 0;
11620 /* Nonzero if this is a comparison operation;
11621 if both args are promoted shorts, compare the original shorts.
11622 Also implies COMMON. */
11623 int short_compare = 0;
11625 /* Nonzero if this is a right-shift operation, which can be computed on the
11626 original short and then promoted if the operand is a promoted short. */
11627 int short_shift = 0;
11629 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11630 int common = 0;
11632 /* True means types are compatible as far as ObjC is concerned. */
11633 bool objc_ok;
11635 /* True means this is an arithmetic operation that may need excess
11636 precision. */
11637 bool may_need_excess_precision;
11639 /* True means this is a boolean operation that converts both its
11640 operands to truth-values. */
11641 bool boolean_op = false;
11643 /* Remember whether we're doing / or %. */
11644 bool doing_div_or_mod = false;
11646 /* Remember whether we're doing << or >>. */
11647 bool doing_shift = false;
11649 /* Tree holding instrumentation expression. */
11650 tree instrument_expr = NULL;
11652 if (location == UNKNOWN_LOCATION)
11653 location = input_location;
11655 op0 = orig_op0;
11656 op1 = orig_op1;
11658 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11659 if (op0_int_operands)
11660 op0 = remove_c_maybe_const_expr (op0);
11661 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11662 if (op1_int_operands)
11663 op1 = remove_c_maybe_const_expr (op1);
11664 int_operands = (op0_int_operands && op1_int_operands);
11665 if (int_operands)
11667 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11668 && TREE_CODE (orig_op1) == INTEGER_CST);
11669 int_const = (int_const_or_overflow
11670 && !TREE_OVERFLOW (orig_op0)
11671 && !TREE_OVERFLOW (orig_op1));
11673 else
11674 int_const = int_const_or_overflow = false;
11676 /* Do not apply default conversion in mixed vector/scalar expression. */
11677 if (convert_p
11678 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11680 op0 = default_conversion (op0);
11681 op1 = default_conversion (op1);
11684 orig_type0 = type0 = TREE_TYPE (op0);
11686 orig_type1 = type1 = TREE_TYPE (op1);
11688 /* The expression codes of the data types of the arguments tell us
11689 whether the arguments are integers, floating, pointers, etc. */
11690 code0 = TREE_CODE (type0);
11691 code1 = TREE_CODE (type1);
11693 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11694 STRIP_TYPE_NOPS (op0);
11695 STRIP_TYPE_NOPS (op1);
11697 /* If an error was already reported for one of the arguments,
11698 avoid reporting another error. */
11700 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11701 return error_mark_node;
11703 if (code0 == POINTER_TYPE
11704 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11705 return error_mark_node;
11707 if (code1 == POINTER_TYPE
11708 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11709 return error_mark_node;
11711 if ((invalid_op_diag
11712 = targetm.invalid_binary_op (code, type0, type1)))
11714 error_at (location, invalid_op_diag);
11715 return error_mark_node;
11718 switch (code)
11720 case PLUS_EXPR:
11721 case MINUS_EXPR:
11722 case MULT_EXPR:
11723 case TRUNC_DIV_EXPR:
11724 case CEIL_DIV_EXPR:
11725 case FLOOR_DIV_EXPR:
11726 case ROUND_DIV_EXPR:
11727 case EXACT_DIV_EXPR:
11728 may_need_excess_precision = true;
11729 break;
11731 case EQ_EXPR:
11732 case NE_EXPR:
11733 case LE_EXPR:
11734 case GE_EXPR:
11735 case LT_EXPR:
11736 case GT_EXPR:
11737 /* Excess precision for implicit conversions of integers to
11738 floating point in C11 and later. */
11739 may_need_excess_precision = (flag_isoc11
11740 && (ANY_INTEGRAL_TYPE_P (type0)
11741 || ANY_INTEGRAL_TYPE_P (type1)));
11742 break;
11744 default:
11745 may_need_excess_precision = false;
11746 break;
11748 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11750 op0 = TREE_OPERAND (op0, 0);
11751 type0 = TREE_TYPE (op0);
11753 else if (may_need_excess_precision
11754 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11756 type0 = eptype;
11757 op0 = convert (eptype, op0);
11759 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11761 op1 = TREE_OPERAND (op1, 0);
11762 type1 = TREE_TYPE (op1);
11764 else if (may_need_excess_precision
11765 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11767 type1 = eptype;
11768 op1 = convert (eptype, op1);
11771 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11773 /* In case when one of the operands of the binary operation is
11774 a vector and another is a scalar -- convert scalar to vector. */
11775 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
11776 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
11778 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11779 true);
11781 switch (convert_flag)
11783 case stv_error:
11784 return error_mark_node;
11785 case stv_firstarg:
11787 bool maybe_const = true;
11788 tree sc;
11789 sc = c_fully_fold (op0, false, &maybe_const);
11790 sc = save_expr (sc);
11791 sc = convert (TREE_TYPE (type1), sc);
11792 op0 = build_vector_from_val (type1, sc);
11793 if (!maybe_const)
11794 op0 = c_wrap_maybe_const (op0, true);
11795 orig_type0 = type0 = TREE_TYPE (op0);
11796 code0 = TREE_CODE (type0);
11797 converted = 1;
11798 break;
11800 case stv_secondarg:
11802 bool maybe_const = true;
11803 tree sc;
11804 sc = c_fully_fold (op1, false, &maybe_const);
11805 sc = save_expr (sc);
11806 sc = convert (TREE_TYPE (type0), sc);
11807 op1 = build_vector_from_val (type0, sc);
11808 if (!maybe_const)
11809 op1 = c_wrap_maybe_const (op1, true);
11810 orig_type1 = type1 = TREE_TYPE (op1);
11811 code1 = TREE_CODE (type1);
11812 converted = 1;
11813 break;
11815 default:
11816 break;
11820 switch (code)
11822 case PLUS_EXPR:
11823 /* Handle the pointer + int case. */
11824 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11826 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11827 goto return_build_binary_op;
11829 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11831 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11832 goto return_build_binary_op;
11834 else
11835 common = 1;
11836 break;
11838 case MINUS_EXPR:
11839 /* Subtraction of two similar pointers.
11840 We must subtract them as integers, then divide by object size. */
11841 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11842 && comp_target_types (location, type0, type1))
11844 ret = pointer_diff (location, op0, op1, &instrument_expr);
11845 goto return_build_binary_op;
11847 /* Handle pointer minus int. Just like pointer plus int. */
11848 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11850 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11851 goto return_build_binary_op;
11853 else
11854 common = 1;
11855 break;
11857 case MULT_EXPR:
11858 common = 1;
11859 break;
11861 case TRUNC_DIV_EXPR:
11862 case CEIL_DIV_EXPR:
11863 case FLOOR_DIV_EXPR:
11864 case ROUND_DIV_EXPR:
11865 case EXACT_DIV_EXPR:
11866 doing_div_or_mod = true;
11867 warn_for_div_by_zero (location, op1);
11869 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11870 || code0 == FIXED_POINT_TYPE
11871 || code0 == COMPLEX_TYPE
11872 || gnu_vector_type_p (type0))
11873 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11874 || code1 == FIXED_POINT_TYPE
11875 || code1 == COMPLEX_TYPE
11876 || gnu_vector_type_p (type1)))
11878 enum tree_code tcode0 = code0, tcode1 = code1;
11880 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11881 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11882 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11883 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11885 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11886 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11887 resultcode = RDIV_EXPR;
11888 else
11889 /* Although it would be tempting to shorten always here, that
11890 loses on some targets, since the modulo instruction is
11891 undefined if the quotient can't be represented in the
11892 computation mode. We shorten only if unsigned or if
11893 dividing by something we know != -1. */
11894 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11895 || (TREE_CODE (op1) == INTEGER_CST
11896 && !integer_all_onesp (op1)));
11897 common = 1;
11899 break;
11901 case BIT_AND_EXPR:
11902 case BIT_IOR_EXPR:
11903 case BIT_XOR_EXPR:
11904 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11905 shorten = -1;
11906 /* Allow vector types which are not floating point types. */
11907 else if (gnu_vector_type_p (type0)
11908 && gnu_vector_type_p (type1)
11909 && !VECTOR_FLOAT_TYPE_P (type0)
11910 && !VECTOR_FLOAT_TYPE_P (type1))
11911 common = 1;
11912 break;
11914 case TRUNC_MOD_EXPR:
11915 case FLOOR_MOD_EXPR:
11916 doing_div_or_mod = true;
11917 warn_for_div_by_zero (location, op1);
11919 if (gnu_vector_type_p (type0)
11920 && gnu_vector_type_p (type1)
11921 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11922 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11923 common = 1;
11924 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11926 /* Although it would be tempting to shorten always here, that loses
11927 on some targets, since the modulo instruction is undefined if the
11928 quotient can't be represented in the computation mode. We shorten
11929 only if unsigned or if dividing by something we know != -1. */
11930 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11931 || (TREE_CODE (op1) == INTEGER_CST
11932 && !integer_all_onesp (op1)));
11933 common = 1;
11935 break;
11937 case TRUTH_ANDIF_EXPR:
11938 case TRUTH_ORIF_EXPR:
11939 case TRUTH_AND_EXPR:
11940 case TRUTH_OR_EXPR:
11941 case TRUTH_XOR_EXPR:
11942 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11943 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11944 || code0 == FIXED_POINT_TYPE)
11945 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11946 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11947 || code1 == FIXED_POINT_TYPE))
11949 /* Result of these operations is always an int,
11950 but that does not mean the operands should be
11951 converted to ints! */
11952 result_type = integer_type_node;
11953 if (op0_int_operands)
11955 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11956 op0 = remove_c_maybe_const_expr (op0);
11958 else
11959 op0 = c_objc_common_truthvalue_conversion (location, op0);
11960 if (op1_int_operands)
11962 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11963 op1 = remove_c_maybe_const_expr (op1);
11965 else
11966 op1 = c_objc_common_truthvalue_conversion (location, op1);
11967 converted = 1;
11968 boolean_op = true;
11970 if (code == TRUTH_ANDIF_EXPR)
11972 int_const_or_overflow = (int_operands
11973 && TREE_CODE (orig_op0) == INTEGER_CST
11974 && (op0 == truthvalue_false_node
11975 || TREE_CODE (orig_op1) == INTEGER_CST));
11976 int_const = (int_const_or_overflow
11977 && !TREE_OVERFLOW (orig_op0)
11978 && (op0 == truthvalue_false_node
11979 || !TREE_OVERFLOW (orig_op1)));
11981 else if (code == TRUTH_ORIF_EXPR)
11983 int_const_or_overflow = (int_operands
11984 && TREE_CODE (orig_op0) == INTEGER_CST
11985 && (op0 == truthvalue_true_node
11986 || TREE_CODE (orig_op1) == INTEGER_CST));
11987 int_const = (int_const_or_overflow
11988 && !TREE_OVERFLOW (orig_op0)
11989 && (op0 == truthvalue_true_node
11990 || !TREE_OVERFLOW (orig_op1)));
11992 break;
11994 /* Shift operations: result has same type as first operand;
11995 always convert second operand to int.
11996 Also set SHORT_SHIFT if shifting rightward. */
11998 case RSHIFT_EXPR:
11999 if (gnu_vector_type_p (type0)
12000 && gnu_vector_type_p (type1)
12001 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12002 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12003 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12004 TYPE_VECTOR_SUBPARTS (type1)))
12006 result_type = type0;
12007 converted = 1;
12009 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12010 || (gnu_vector_type_p (type0)
12011 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12012 && code1 == INTEGER_TYPE)
12014 doing_shift = true;
12015 if (TREE_CODE (op1) == INTEGER_CST)
12017 if (tree_int_cst_sgn (op1) < 0)
12019 int_const = false;
12020 if (c_inhibit_evaluation_warnings == 0)
12021 warning_at (location, OPT_Wshift_count_negative,
12022 "right shift count is negative");
12024 else if (code0 == VECTOR_TYPE)
12026 if (compare_tree_int (op1,
12027 TYPE_PRECISION (TREE_TYPE (type0)))
12028 >= 0)
12030 int_const = false;
12031 if (c_inhibit_evaluation_warnings == 0)
12032 warning_at (location, OPT_Wshift_count_overflow,
12033 "right shift count >= width of vector element");
12036 else
12038 if (!integer_zerop (op1))
12039 short_shift = 1;
12041 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12043 int_const = false;
12044 if (c_inhibit_evaluation_warnings == 0)
12045 warning_at (location, OPT_Wshift_count_overflow,
12046 "right shift count >= width of type");
12051 /* Use the type of the value to be shifted. */
12052 result_type = type0;
12053 /* Avoid converting op1 to result_type later. */
12054 converted = 1;
12056 break;
12058 case LSHIFT_EXPR:
12059 if (gnu_vector_type_p (type0)
12060 && gnu_vector_type_p (type1)
12061 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12062 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12063 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12064 TYPE_VECTOR_SUBPARTS (type1)))
12066 result_type = type0;
12067 converted = 1;
12069 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12070 || (gnu_vector_type_p (type0)
12071 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12072 && code1 == INTEGER_TYPE)
12074 doing_shift = true;
12075 if (TREE_CODE (op0) == INTEGER_CST
12076 && tree_int_cst_sgn (op0) < 0)
12078 /* Don't reject a left shift of a negative value in a context
12079 where a constant expression is needed in C90. */
12080 if (flag_isoc99)
12081 int_const = false;
12082 if (c_inhibit_evaluation_warnings == 0)
12083 warning_at (location, OPT_Wshift_negative_value,
12084 "left shift of negative value");
12086 if (TREE_CODE (op1) == INTEGER_CST)
12088 if (tree_int_cst_sgn (op1) < 0)
12090 int_const = false;
12091 if (c_inhibit_evaluation_warnings == 0)
12092 warning_at (location, OPT_Wshift_count_negative,
12093 "left shift count is negative");
12095 else if (code0 == VECTOR_TYPE)
12097 if (compare_tree_int (op1,
12098 TYPE_PRECISION (TREE_TYPE (type0)))
12099 >= 0)
12101 int_const = false;
12102 if (c_inhibit_evaluation_warnings == 0)
12103 warning_at (location, OPT_Wshift_count_overflow,
12104 "left shift count >= width of vector element");
12107 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12109 int_const = false;
12110 if (c_inhibit_evaluation_warnings == 0)
12111 warning_at (location, OPT_Wshift_count_overflow,
12112 "left shift count >= width of type");
12114 else if (TREE_CODE (op0) == INTEGER_CST
12115 && maybe_warn_shift_overflow (location, op0, op1)
12116 && flag_isoc99)
12117 int_const = false;
12120 /* Use the type of the value to be shifted. */
12121 result_type = type0;
12122 /* Avoid converting op1 to result_type later. */
12123 converted = 1;
12125 break;
12127 case EQ_EXPR:
12128 case NE_EXPR:
12129 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12131 tree intt;
12132 if (!vector_types_compatible_elements_p (type0, type1))
12134 error_at (location, "comparing vectors with different "
12135 "element types");
12136 return error_mark_node;
12139 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12140 TYPE_VECTOR_SUBPARTS (type1)))
12142 error_at (location, "comparing vectors with different "
12143 "number of elements");
12144 return error_mark_node;
12147 /* It's not precisely specified how the usual arithmetic
12148 conversions apply to the vector types. Here, we use
12149 the unsigned type if one of the operands is signed and
12150 the other one is unsigned. */
12151 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12153 if (!TYPE_UNSIGNED (type0))
12154 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12155 else
12156 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12157 warning_at (location, OPT_Wsign_compare, "comparison between "
12158 "types %qT and %qT", type0, type1);
12161 /* Always construct signed integer vector type. */
12162 intt = c_common_type_for_size (GET_MODE_BITSIZE
12163 (SCALAR_TYPE_MODE
12164 (TREE_TYPE (type0))), 0);
12165 if (!intt)
12167 error_at (location, "could not find an integer type "
12168 "of the same size as %qT",
12169 TREE_TYPE (type0));
12170 return error_mark_node;
12172 result_type = build_opaque_vector_type (intt,
12173 TYPE_VECTOR_SUBPARTS (type0));
12174 converted = 1;
12175 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12176 goto return_build_binary_op;
12178 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12179 warning_at (location,
12180 OPT_Wfloat_equal,
12181 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12182 /* Result of comparison is always int,
12183 but don't convert the args to int! */
12184 build_type = integer_type_node;
12185 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12186 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12187 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12188 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12189 short_compare = 1;
12190 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12192 if (TREE_CODE (op0) == ADDR_EXPR
12193 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
12194 && !from_macro_expansion_at (location))
12196 if (code == EQ_EXPR)
12197 warning_at (location,
12198 OPT_Waddress,
12199 "the comparison will always evaluate as %<false%> "
12200 "for the address of %qD will never be NULL",
12201 TREE_OPERAND (op0, 0));
12202 else
12203 warning_at (location,
12204 OPT_Waddress,
12205 "the comparison will always evaluate as %<true%> "
12206 "for the address of %qD will never be NULL",
12207 TREE_OPERAND (op0, 0));
12209 result_type = type0;
12211 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12213 if (TREE_CODE (op1) == ADDR_EXPR
12214 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
12215 && !from_macro_expansion_at (location))
12217 if (code == EQ_EXPR)
12218 warning_at (location,
12219 OPT_Waddress,
12220 "the comparison will always evaluate as %<false%> "
12221 "for the address of %qD will never be NULL",
12222 TREE_OPERAND (op1, 0));
12223 else
12224 warning_at (location,
12225 OPT_Waddress,
12226 "the comparison will always evaluate as %<true%> "
12227 "for the address of %qD will never be NULL",
12228 TREE_OPERAND (op1, 0));
12230 result_type = type1;
12232 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12234 tree tt0 = TREE_TYPE (type0);
12235 tree tt1 = TREE_TYPE (type1);
12236 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12237 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12238 addr_space_t as_common = ADDR_SPACE_GENERIC;
12240 /* Anything compares with void *. void * compares with anything.
12241 Otherwise, the targets must be compatible
12242 and both must be object or both incomplete. */
12243 if (comp_target_types (location, type0, type1))
12244 result_type = common_pointer_type (type0, type1);
12245 else if (!addr_space_superset (as0, as1, &as_common))
12247 error_at (location, "comparison of pointers to "
12248 "disjoint address spaces");
12249 return error_mark_node;
12251 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12253 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12254 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12255 "comparison of %<void *%> with function pointer");
12257 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12259 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12260 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12261 "comparison of %<void *%> with function pointer");
12263 else
12264 /* Avoid warning about the volatile ObjC EH puts on decls. */
12265 if (!objc_ok)
12266 pedwarn (location, 0,
12267 "comparison of distinct pointer types lacks a cast");
12269 if (result_type == NULL_TREE)
12271 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12272 result_type = build_pointer_type
12273 (build_qualified_type (void_type_node, qual));
12276 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12278 result_type = type0;
12279 pedwarn (location, 0, "comparison between pointer and integer");
12281 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12283 result_type = type1;
12284 pedwarn (location, 0, "comparison between pointer and integer");
12286 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12287 || truth_value_p (TREE_CODE (orig_op0)))
12288 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12289 || truth_value_p (TREE_CODE (orig_op1))))
12290 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12291 break;
12293 case LE_EXPR:
12294 case GE_EXPR:
12295 case LT_EXPR:
12296 case GT_EXPR:
12297 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12299 tree intt;
12300 if (!vector_types_compatible_elements_p (type0, type1))
12302 error_at (location, "comparing vectors with different "
12303 "element types");
12304 return error_mark_node;
12307 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12308 TYPE_VECTOR_SUBPARTS (type1)))
12310 error_at (location, "comparing vectors with different "
12311 "number of elements");
12312 return error_mark_node;
12315 /* It's not precisely specified how the usual arithmetic
12316 conversions apply to the vector types. Here, we use
12317 the unsigned type if one of the operands is signed and
12318 the other one is unsigned. */
12319 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12321 if (!TYPE_UNSIGNED (type0))
12322 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12323 else
12324 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12325 warning_at (location, OPT_Wsign_compare, "comparison between "
12326 "types %qT and %qT", type0, type1);
12329 /* Always construct signed integer vector type. */
12330 intt = c_common_type_for_size (GET_MODE_BITSIZE
12331 (SCALAR_TYPE_MODE
12332 (TREE_TYPE (type0))), 0);
12333 if (!intt)
12335 error_at (location, "could not find an integer type "
12336 "of the same size as %qT",
12337 TREE_TYPE (type0));
12338 return error_mark_node;
12340 result_type = build_opaque_vector_type (intt,
12341 TYPE_VECTOR_SUBPARTS (type0));
12342 converted = 1;
12343 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12344 goto return_build_binary_op;
12346 build_type = integer_type_node;
12347 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12348 || code0 == FIXED_POINT_TYPE)
12349 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12350 || code1 == FIXED_POINT_TYPE))
12351 short_compare = 1;
12352 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12354 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12355 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12356 addr_space_t as_common;
12358 if (comp_target_types (location, type0, type1))
12360 result_type = common_pointer_type (type0, type1);
12361 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12362 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12363 pedwarn_c99 (location, OPT_Wpedantic,
12364 "comparison of complete and incomplete pointers");
12365 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12366 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12367 "ordered comparisons of pointers to functions");
12368 else if (null_pointer_constant_p (orig_op0)
12369 || null_pointer_constant_p (orig_op1))
12370 warning_at (location, OPT_Wextra,
12371 "ordered comparison of pointer with null pointer");
12374 else if (!addr_space_superset (as0, as1, &as_common))
12376 error_at (location, "comparison of pointers to "
12377 "disjoint address spaces");
12378 return error_mark_node;
12380 else
12382 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12383 result_type = build_pointer_type
12384 (build_qualified_type (void_type_node, qual));
12385 pedwarn (location, 0,
12386 "comparison of distinct pointer types lacks a cast");
12389 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12391 result_type = type0;
12392 if (pedantic)
12393 pedwarn (location, OPT_Wpedantic,
12394 "ordered comparison of pointer with integer zero");
12395 else if (extra_warnings)
12396 warning_at (location, OPT_Wextra,
12397 "ordered comparison of pointer with integer zero");
12399 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12401 result_type = type1;
12402 if (pedantic)
12403 pedwarn (location, OPT_Wpedantic,
12404 "ordered comparison of pointer with integer zero");
12405 else if (extra_warnings)
12406 warning_at (location, OPT_Wextra,
12407 "ordered comparison of pointer with integer zero");
12409 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12411 result_type = type0;
12412 pedwarn (location, 0, "comparison between pointer and integer");
12414 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12416 result_type = type1;
12417 pedwarn (location, 0, "comparison between pointer and integer");
12420 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12421 && current_function_decl != NULL_TREE
12422 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12424 op0 = save_expr (op0);
12425 op1 = save_expr (op1);
12427 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12428 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12431 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12432 || truth_value_p (TREE_CODE (orig_op0)))
12433 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12434 || truth_value_p (TREE_CODE (orig_op1))))
12435 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12436 break;
12438 case MIN_EXPR:
12439 case MAX_EXPR:
12440 /* Used for OpenMP atomics. */
12441 gcc_assert (flag_openmp);
12442 common = 1;
12443 break;
12445 default:
12446 gcc_unreachable ();
12449 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12450 return error_mark_node;
12452 if (gnu_vector_type_p (type0)
12453 && gnu_vector_type_p (type1)
12454 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12455 || !vector_types_compatible_elements_p (type0, type1)))
12457 gcc_rich_location richloc (location);
12458 maybe_range_label_for_tree_type_mismatch
12459 label_for_op0 (orig_op0, orig_op1),
12460 label_for_op1 (orig_op1, orig_op0);
12461 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12462 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12463 binary_op_error (&richloc, code, type0, type1);
12464 return error_mark_node;
12467 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12468 || code0 == FIXED_POINT_TYPE
12469 || gnu_vector_type_p (type0))
12471 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12472 || code1 == FIXED_POINT_TYPE
12473 || gnu_vector_type_p (type1)))
12475 bool first_complex = (code0 == COMPLEX_TYPE);
12476 bool second_complex = (code1 == COMPLEX_TYPE);
12477 int none_complex = (!first_complex && !second_complex);
12479 if (shorten || common || short_compare)
12481 result_type = c_common_type (type0, type1);
12482 do_warn_double_promotion (result_type, type0, type1,
12483 "implicit conversion from %qT to %qT "
12484 "to match other operand of binary "
12485 "expression",
12486 location);
12487 if (result_type == error_mark_node)
12488 return error_mark_node;
12491 if (first_complex != second_complex
12492 && (code == PLUS_EXPR
12493 || code == MINUS_EXPR
12494 || code == MULT_EXPR
12495 || (code == TRUNC_DIV_EXPR && first_complex))
12496 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12497 && flag_signed_zeros)
12499 /* An operation on mixed real/complex operands must be
12500 handled specially, but the language-independent code can
12501 more easily optimize the plain complex arithmetic if
12502 -fno-signed-zeros. */
12503 tree real_type = TREE_TYPE (result_type);
12504 tree real, imag;
12505 if (type0 != orig_type0 || type1 != orig_type1)
12507 gcc_assert (may_need_excess_precision && common);
12508 semantic_result_type = c_common_type (orig_type0, orig_type1);
12510 if (first_complex)
12512 if (TREE_TYPE (op0) != result_type)
12513 op0 = convert_and_check (location, result_type, op0);
12514 if (TREE_TYPE (op1) != real_type)
12515 op1 = convert_and_check (location, real_type, op1);
12517 else
12519 if (TREE_TYPE (op0) != real_type)
12520 op0 = convert_and_check (location, real_type, op0);
12521 if (TREE_TYPE (op1) != result_type)
12522 op1 = convert_and_check (location, result_type, op1);
12524 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12525 return error_mark_node;
12526 if (first_complex)
12528 op0 = save_expr (op0);
12529 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12530 op0, true);
12531 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12532 op0, true);
12533 switch (code)
12535 case MULT_EXPR:
12536 case TRUNC_DIV_EXPR:
12537 op1 = save_expr (op1);
12538 imag = build2 (resultcode, real_type, imag, op1);
12539 /* Fall through. */
12540 case PLUS_EXPR:
12541 case MINUS_EXPR:
12542 real = build2 (resultcode, real_type, real, op1);
12543 break;
12544 default:
12545 gcc_unreachable();
12548 else
12550 op1 = save_expr (op1);
12551 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12552 op1, true);
12553 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12554 op1, true);
12555 switch (code)
12557 case MULT_EXPR:
12558 op0 = save_expr (op0);
12559 imag = build2 (resultcode, real_type, op0, imag);
12560 /* Fall through. */
12561 case PLUS_EXPR:
12562 real = build2 (resultcode, real_type, op0, real);
12563 break;
12564 case MINUS_EXPR:
12565 real = build2 (resultcode, real_type, op0, real);
12566 imag = build1 (NEGATE_EXPR, real_type, imag);
12567 break;
12568 default:
12569 gcc_unreachable();
12572 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12573 goto return_build_binary_op;
12576 /* For certain operations (which identify themselves by shorten != 0)
12577 if both args were extended from the same smaller type,
12578 do the arithmetic in that type and then extend.
12580 shorten !=0 and !=1 indicates a bitwise operation.
12581 For them, this optimization is safe only if
12582 both args are zero-extended or both are sign-extended.
12583 Otherwise, we might change the result.
12584 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12585 but calculated in (unsigned short) it would be (unsigned short)-1. */
12587 if (shorten && none_complex)
12589 final_type = result_type;
12590 result_type = shorten_binary_op (result_type, op0, op1,
12591 shorten == -1);
12594 /* Shifts can be shortened if shifting right. */
12596 if (short_shift)
12598 int unsigned_arg;
12599 tree arg0 = get_narrower (op0, &unsigned_arg);
12601 final_type = result_type;
12603 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12604 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12606 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12607 && tree_int_cst_sgn (op1) > 0
12608 /* We can shorten only if the shift count is less than the
12609 number of bits in the smaller type size. */
12610 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12611 /* We cannot drop an unsigned shift after sign-extension. */
12612 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12614 /* Do an unsigned shift if the operand was zero-extended. */
12615 result_type
12616 = c_common_signed_or_unsigned_type (unsigned_arg,
12617 TREE_TYPE (arg0));
12618 /* Convert value-to-be-shifted to that type. */
12619 if (TREE_TYPE (op0) != result_type)
12620 op0 = convert (result_type, op0);
12621 converted = 1;
12625 /* Comparison operations are shortened too but differently.
12626 They identify themselves by setting short_compare = 1. */
12628 if (short_compare)
12630 /* Don't write &op0, etc., because that would prevent op0
12631 from being kept in a register.
12632 Instead, make copies of the our local variables and
12633 pass the copies by reference, then copy them back afterward. */
12634 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12635 enum tree_code xresultcode = resultcode;
12636 tree val
12637 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12638 &xresultcode);
12640 if (val != NULL_TREE)
12642 ret = val;
12643 goto return_build_binary_op;
12646 op0 = xop0, op1 = xop1;
12647 converted = 1;
12648 resultcode = xresultcode;
12650 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
12652 bool op0_maybe_const = true;
12653 bool op1_maybe_const = true;
12654 tree orig_op0_folded, orig_op1_folded;
12656 if (in_late_binary_op)
12658 orig_op0_folded = orig_op0;
12659 orig_op1_folded = orig_op1;
12661 else
12663 /* Fold for the sake of possible warnings, as in
12664 build_conditional_expr. This requires the
12665 "original" values to be folded, not just op0 and
12666 op1. */
12667 c_inhibit_evaluation_warnings++;
12668 op0 = c_fully_fold (op0, require_constant_value,
12669 &op0_maybe_const);
12670 op1 = c_fully_fold (op1, require_constant_value,
12671 &op1_maybe_const);
12672 c_inhibit_evaluation_warnings--;
12673 orig_op0_folded = c_fully_fold (orig_op0,
12674 require_constant_value,
12675 NULL);
12676 orig_op1_folded = c_fully_fold (orig_op1,
12677 require_constant_value,
12678 NULL);
12681 if (warn_sign_compare)
12682 warn_for_sign_compare (location, orig_op0_folded,
12683 orig_op1_folded, op0, op1,
12684 result_type, resultcode);
12685 if (!in_late_binary_op && !int_operands)
12687 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12688 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12689 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12690 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12696 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12697 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12698 Then the expression will be built.
12699 It will be given type FINAL_TYPE if that is nonzero;
12700 otherwise, it will be given type RESULT_TYPE. */
12702 if (!result_type)
12704 /* Favor showing any expression locations that are available. */
12705 op_location_t oploc (location, UNKNOWN_LOCATION);
12706 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12707 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12708 return error_mark_node;
12711 if (build_type == NULL_TREE)
12713 build_type = result_type;
12714 if ((type0 != orig_type0 || type1 != orig_type1)
12715 && !boolean_op)
12717 gcc_assert (may_need_excess_precision && common);
12718 semantic_result_type = c_common_type (orig_type0, orig_type1);
12722 if (!converted)
12724 op0 = ep_convert_and_check (location, result_type, op0,
12725 semantic_result_type);
12726 op1 = ep_convert_and_check (location, result_type, op1,
12727 semantic_result_type);
12729 /* This can happen if one operand has a vector type, and the other
12730 has a different type. */
12731 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12732 return error_mark_node;
12735 if (sanitize_flags_p ((SANITIZE_SHIFT
12736 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
12737 && current_function_decl != NULL_TREE
12738 && (doing_div_or_mod || doing_shift)
12739 && !require_constant_value)
12741 /* OP0 and/or OP1 might have side-effects. */
12742 op0 = save_expr (op0);
12743 op1 = save_expr (op1);
12744 op0 = c_fully_fold (op0, false, NULL);
12745 op1 = c_fully_fold (op1, false, NULL);
12746 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12747 | SANITIZE_FLOAT_DIVIDE))))
12748 instrument_expr = ubsan_instrument_division (location, op0, op1);
12749 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12750 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12753 /* Treat expressions in initializers specially as they can't trap. */
12754 if (int_const_or_overflow)
12755 ret = (require_constant_value
12756 ? fold_build2_initializer_loc (location, resultcode, build_type,
12757 op0, op1)
12758 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12759 else
12760 ret = build2 (resultcode, build_type, op0, op1);
12761 if (final_type != NULL_TREE)
12762 ret = convert (final_type, ret);
12764 return_build_binary_op:
12765 gcc_assert (ret != error_mark_node);
12766 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12767 ret = (int_operands
12768 ? note_integer_operands (ret)
12769 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12770 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12771 && !in_late_binary_op)
12772 ret = note_integer_operands (ret);
12773 protected_set_expr_location (ret, location);
12775 if (instrument_expr != NULL)
12776 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12777 instrument_expr, ret);
12779 if (semantic_result_type)
12780 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12781 semantic_result_type, ret);
12783 return ret;
12787 /* Convert EXPR to be a truth-value, validating its type for this
12788 purpose. LOCATION is the source location for the expression. */
12790 tree
12791 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12793 bool int_const, int_operands;
12795 switch (TREE_CODE (TREE_TYPE (expr)))
12797 case ARRAY_TYPE:
12798 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12799 return error_mark_node;
12801 case RECORD_TYPE:
12802 error_at (location, "used struct type value where scalar is required");
12803 return error_mark_node;
12805 case UNION_TYPE:
12806 error_at (location, "used union type value where scalar is required");
12807 return error_mark_node;
12809 case VOID_TYPE:
12810 error_at (location, "void value not ignored as it ought to be");
12811 return error_mark_node;
12813 case POINTER_TYPE:
12814 if (reject_gcc_builtin (expr))
12815 return error_mark_node;
12816 break;
12818 case FUNCTION_TYPE:
12819 gcc_unreachable ();
12821 case VECTOR_TYPE:
12822 error_at (location, "used vector type where scalar is required");
12823 return error_mark_node;
12825 default:
12826 break;
12829 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12830 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12831 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12833 expr = remove_c_maybe_const_expr (expr);
12834 expr = build2 (NE_EXPR, integer_type_node, expr,
12835 convert (TREE_TYPE (expr), integer_zero_node));
12836 expr = note_integer_operands (expr);
12838 else
12839 /* ??? Should we also give an error for vectors rather than leaving
12840 those to give errors later? */
12841 expr = c_common_truthvalue_conversion (location, expr);
12843 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12845 if (TREE_OVERFLOW (expr))
12846 return expr;
12847 else
12848 return note_integer_operands (expr);
12850 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12851 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12852 return expr;
12856 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12857 required. */
12859 tree
12860 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12862 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12864 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12865 /* Executing a compound literal inside a function reinitializes
12866 it. */
12867 if (!TREE_STATIC (decl))
12868 *se = true;
12869 return decl;
12871 else
12872 return expr;
12875 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12876 statement. LOC is the location of the construct. */
12878 tree
12879 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12880 tree clauses)
12882 body = c_end_compound_stmt (loc, body, true);
12884 tree stmt = make_node (code);
12885 TREE_TYPE (stmt) = void_type_node;
12886 OMP_BODY (stmt) = body;
12887 OMP_CLAUSES (stmt) = clauses;
12888 SET_EXPR_LOCATION (stmt, loc);
12890 return add_stmt (stmt);
12893 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12894 statement. LOC is the location of the OACC_DATA. */
12896 tree
12897 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12899 tree stmt;
12901 block = c_end_compound_stmt (loc, block, true);
12903 stmt = make_node (OACC_DATA);
12904 TREE_TYPE (stmt) = void_type_node;
12905 OACC_DATA_CLAUSES (stmt) = clauses;
12906 OACC_DATA_BODY (stmt) = block;
12907 SET_EXPR_LOCATION (stmt, loc);
12909 return add_stmt (stmt);
12912 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12913 statement. LOC is the location of the OACC_HOST_DATA. */
12915 tree
12916 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12918 tree stmt;
12920 block = c_end_compound_stmt (loc, block, true);
12922 stmt = make_node (OACC_HOST_DATA);
12923 TREE_TYPE (stmt) = void_type_node;
12924 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12925 OACC_HOST_DATA_BODY (stmt) = block;
12926 SET_EXPR_LOCATION (stmt, loc);
12928 return add_stmt (stmt);
12931 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12933 tree
12934 c_begin_omp_parallel (void)
12936 tree block;
12938 keep_next_level ();
12939 block = c_begin_compound_stmt (true);
12941 return block;
12944 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12945 statement. LOC is the location of the OMP_PARALLEL. */
12947 tree
12948 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12950 tree stmt;
12952 block = c_end_compound_stmt (loc, block, true);
12954 stmt = make_node (OMP_PARALLEL);
12955 TREE_TYPE (stmt) = void_type_node;
12956 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12957 OMP_PARALLEL_BODY (stmt) = block;
12958 SET_EXPR_LOCATION (stmt, loc);
12960 return add_stmt (stmt);
12963 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12965 tree
12966 c_begin_omp_task (void)
12968 tree block;
12970 keep_next_level ();
12971 block = c_begin_compound_stmt (true);
12973 return block;
12976 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12977 statement. LOC is the location of the #pragma. */
12979 tree
12980 c_finish_omp_task (location_t loc, tree clauses, tree block)
12982 tree stmt;
12984 block = c_end_compound_stmt (loc, block, true);
12986 stmt = make_node (OMP_TASK);
12987 TREE_TYPE (stmt) = void_type_node;
12988 OMP_TASK_CLAUSES (stmt) = clauses;
12989 OMP_TASK_BODY (stmt) = block;
12990 SET_EXPR_LOCATION (stmt, loc);
12992 return add_stmt (stmt);
12995 /* Generate GOMP_cancel call for #pragma omp cancel. */
12997 void
12998 c_finish_omp_cancel (location_t loc, tree clauses)
13000 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
13001 int mask = 0;
13002 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13003 mask = 1;
13004 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13005 mask = 2;
13006 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13007 mask = 4;
13008 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13009 mask = 8;
13010 else
13012 error_at (loc, "%<#pragma omp cancel%> must specify one of "
13013 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13014 "clauses");
13015 return;
13017 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
13018 if (ifc != NULL_TREE)
13020 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
13021 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
13022 error_at (OMP_CLAUSE_LOCATION (ifc),
13023 "expected %<cancel%> %<if%> clause modifier");
13024 else
13026 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
13027 if (ifc2 != NULL_TREE)
13029 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
13030 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13031 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13032 error_at (OMP_CLAUSE_LOCATION (ifc2),
13033 "expected %<cancel%> %<if%> clause modifier");
13037 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13038 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13039 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13040 build_zero_cst (type));
13042 else
13043 ifc = boolean_true_node;
13044 tree stmt = build_call_expr_loc (loc, fn, 2,
13045 build_int_cst (integer_type_node, mask),
13046 ifc);
13047 add_stmt (stmt);
13050 /* Generate GOMP_cancellation_point call for
13051 #pragma omp cancellation point. */
13053 void
13054 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13056 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13057 int mask = 0;
13058 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13059 mask = 1;
13060 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13061 mask = 2;
13062 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13063 mask = 4;
13064 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13065 mask = 8;
13066 else
13068 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13069 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13070 "clauses");
13071 return;
13073 tree stmt = build_call_expr_loc (loc, fn, 1,
13074 build_int_cst (integer_type_node, mask));
13075 add_stmt (stmt);
13078 /* Helper function for handle_omp_array_sections. Called recursively
13079 to handle multiple array-section-subscripts. C is the clause,
13080 T current expression (initially OMP_CLAUSE_DECL), which is either
13081 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13082 expression if specified, TREE_VALUE length expression if specified,
13083 TREE_CHAIN is what it has been specified after, or some decl.
13084 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13085 set to true if any of the array-section-subscript could have length
13086 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13087 first array-section-subscript which is known not to have length
13088 of one. Given say:
13089 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13090 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13091 all are or may have length of 1, array-section-subscript [:2] is the
13092 first one known not to have length 1. For array-section-subscript
13093 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13094 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13095 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
13096 case though, as some lengths could be zero. */
13098 static tree
13099 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13100 bool &maybe_zero_len, unsigned int &first_non_one,
13101 enum c_omp_region_type ort)
13103 tree ret, low_bound, length, type;
13104 if (TREE_CODE (t) != TREE_LIST)
13106 if (error_operand_p (t))
13107 return error_mark_node;
13108 ret = t;
13109 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13110 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13111 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13113 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13114 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13115 return error_mark_node;
13117 if (TREE_CODE (t) == COMPONENT_REF
13118 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13119 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13120 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13122 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13124 error_at (OMP_CLAUSE_LOCATION (c),
13125 "bit-field %qE in %qs clause",
13126 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13127 return error_mark_node;
13129 while (TREE_CODE (t) == COMPONENT_REF)
13131 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13133 error_at (OMP_CLAUSE_LOCATION (c),
13134 "%qE is a member of a union", t);
13135 return error_mark_node;
13137 t = TREE_OPERAND (t, 0);
13138 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13140 if (maybe_ne (mem_ref_offset (t), 0))
13141 error_at (OMP_CLAUSE_LOCATION (c),
13142 "cannot dereference %qE in %qs clause", t,
13143 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13144 else
13145 t = TREE_OPERAND (t, 0);
13149 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13151 if (DECL_P (t))
13152 error_at (OMP_CLAUSE_LOCATION (c),
13153 "%qD is not a variable in %qs clause", t,
13154 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13155 else
13156 error_at (OMP_CLAUSE_LOCATION (c),
13157 "%qE is not a variable in %qs clause", t,
13158 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13159 return error_mark_node;
13161 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13162 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13163 && TYPE_ATOMIC (TREE_TYPE (t)))
13165 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13166 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13167 return error_mark_node;
13169 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13170 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13171 && VAR_P (t)
13172 && DECL_THREAD_LOCAL_P (t))
13174 error_at (OMP_CLAUSE_LOCATION (c),
13175 "%qD is threadprivate variable in %qs clause", t,
13176 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13177 return error_mark_node;
13179 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13180 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13181 && TYPE_ATOMIC (TREE_TYPE (t))
13182 && POINTER_TYPE_P (TREE_TYPE (t)))
13184 /* If the array section is pointer based and the pointer
13185 itself is _Atomic qualified, we need to atomically load
13186 the pointer. */
13187 c_expr expr;
13188 memset (&expr, 0, sizeof (expr));
13189 expr.value = ret;
13190 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13191 expr, false, false);
13192 ret = expr.value;
13194 return ret;
13197 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13198 maybe_zero_len, first_non_one, ort);
13199 if (ret == error_mark_node || ret == NULL_TREE)
13200 return ret;
13202 type = TREE_TYPE (ret);
13203 low_bound = TREE_PURPOSE (t);
13204 length = TREE_VALUE (t);
13206 if (low_bound == error_mark_node || length == error_mark_node)
13207 return error_mark_node;
13209 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13211 error_at (OMP_CLAUSE_LOCATION (c),
13212 "low bound %qE of array section does not have integral type",
13213 low_bound);
13214 return error_mark_node;
13216 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13218 error_at (OMP_CLAUSE_LOCATION (c),
13219 "length %qE of array section does not have integral type",
13220 length);
13221 return error_mark_node;
13223 if (low_bound
13224 && TREE_CODE (low_bound) == INTEGER_CST
13225 && TYPE_PRECISION (TREE_TYPE (low_bound))
13226 > TYPE_PRECISION (sizetype))
13227 low_bound = fold_convert (sizetype, low_bound);
13228 if (length
13229 && TREE_CODE (length) == INTEGER_CST
13230 && TYPE_PRECISION (TREE_TYPE (length))
13231 > TYPE_PRECISION (sizetype))
13232 length = fold_convert (sizetype, length);
13233 if (low_bound == NULL_TREE)
13234 low_bound = integer_zero_node;
13235 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13236 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13237 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13239 if (length != integer_one_node)
13241 error_at (OMP_CLAUSE_LOCATION (c),
13242 "expected single pointer in %qs clause",
13243 c_omp_map_clause_name (c, ort == C_ORT_ACC));
13244 return error_mark_node;
13247 if (length != NULL_TREE)
13249 if (!integer_nonzerop (length))
13251 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13252 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13253 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13254 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13255 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13257 if (integer_zerop (length))
13259 error_at (OMP_CLAUSE_LOCATION (c),
13260 "zero length array section in %qs clause",
13261 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13262 return error_mark_node;
13265 else
13266 maybe_zero_len = true;
13268 if (first_non_one == types.length ()
13269 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13270 first_non_one++;
13272 if (TREE_CODE (type) == ARRAY_TYPE)
13274 if (length == NULL_TREE
13275 && (TYPE_DOMAIN (type) == NULL_TREE
13276 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13278 error_at (OMP_CLAUSE_LOCATION (c),
13279 "for unknown bound array type length expression must "
13280 "be specified");
13281 return error_mark_node;
13283 if (TREE_CODE (low_bound) == INTEGER_CST
13284 && tree_int_cst_sgn (low_bound) == -1)
13286 error_at (OMP_CLAUSE_LOCATION (c),
13287 "negative low bound in array section in %qs clause",
13288 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13289 return error_mark_node;
13291 if (length != NULL_TREE
13292 && TREE_CODE (length) == INTEGER_CST
13293 && tree_int_cst_sgn (length) == -1)
13295 error_at (OMP_CLAUSE_LOCATION (c),
13296 "negative length in array section in %qs clause",
13297 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13298 return error_mark_node;
13300 if (TYPE_DOMAIN (type)
13301 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13302 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13303 == INTEGER_CST)
13305 tree size
13306 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13307 size = size_binop (PLUS_EXPR, size, size_one_node);
13308 if (TREE_CODE (low_bound) == INTEGER_CST)
13310 if (tree_int_cst_lt (size, low_bound))
13312 error_at (OMP_CLAUSE_LOCATION (c),
13313 "low bound %qE above array section size "
13314 "in %qs clause", low_bound,
13315 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13316 return error_mark_node;
13318 if (tree_int_cst_equal (size, low_bound))
13320 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13321 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13322 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13323 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13324 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13326 error_at (OMP_CLAUSE_LOCATION (c),
13327 "zero length array section in %qs clause",
13328 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13329 return error_mark_node;
13331 maybe_zero_len = true;
13333 else if (length == NULL_TREE
13334 && first_non_one == types.length ()
13335 && tree_int_cst_equal
13336 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13337 low_bound))
13338 first_non_one++;
13340 else if (length == NULL_TREE)
13342 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13343 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13344 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13345 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13346 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13347 maybe_zero_len = true;
13348 if (first_non_one == types.length ())
13349 first_non_one++;
13351 if (length && TREE_CODE (length) == INTEGER_CST)
13353 if (tree_int_cst_lt (size, length))
13355 error_at (OMP_CLAUSE_LOCATION (c),
13356 "length %qE above array section size "
13357 "in %qs clause", length,
13358 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13359 return error_mark_node;
13361 if (TREE_CODE (low_bound) == INTEGER_CST)
13363 tree lbpluslen
13364 = size_binop (PLUS_EXPR,
13365 fold_convert (sizetype, low_bound),
13366 fold_convert (sizetype, length));
13367 if (TREE_CODE (lbpluslen) == INTEGER_CST
13368 && tree_int_cst_lt (size, lbpluslen))
13370 error_at (OMP_CLAUSE_LOCATION (c),
13371 "high bound %qE above array section size "
13372 "in %qs clause", lbpluslen,
13373 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13374 return error_mark_node;
13379 else if (length == NULL_TREE)
13381 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13382 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13383 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13384 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13385 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13386 maybe_zero_len = true;
13387 if (first_non_one == types.length ())
13388 first_non_one++;
13391 /* For [lb:] we will need to evaluate lb more than once. */
13392 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13394 tree lb = save_expr (low_bound);
13395 if (lb != low_bound)
13397 TREE_PURPOSE (t) = lb;
13398 low_bound = lb;
13402 else if (TREE_CODE (type) == POINTER_TYPE)
13404 if (length == NULL_TREE)
13406 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
13407 error_at (OMP_CLAUSE_LOCATION (c),
13408 "for array function parameter length expression "
13409 "must be specified");
13410 else
13411 error_at (OMP_CLAUSE_LOCATION (c),
13412 "for pointer type length expression must be specified");
13413 return error_mark_node;
13415 if (length != NULL_TREE
13416 && TREE_CODE (length) == INTEGER_CST
13417 && tree_int_cst_sgn (length) == -1)
13419 error_at (OMP_CLAUSE_LOCATION (c),
13420 "negative length in array section in %qs clause",
13421 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13422 return error_mark_node;
13424 /* If there is a pointer type anywhere but in the very first
13425 array-section-subscript, the array section can't be contiguous. */
13426 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13427 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13428 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13430 error_at (OMP_CLAUSE_LOCATION (c),
13431 "array section is not contiguous in %qs clause",
13432 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13433 return error_mark_node;
13436 else
13438 error_at (OMP_CLAUSE_LOCATION (c),
13439 "%qE does not have pointer or array type", ret);
13440 return error_mark_node;
13442 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13443 types.safe_push (TREE_TYPE (ret));
13444 /* We will need to evaluate lb more than once. */
13445 tree lb = save_expr (low_bound);
13446 if (lb != low_bound)
13448 TREE_PURPOSE (t) = lb;
13449 low_bound = lb;
13451 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13452 return ret;
13455 /* Handle array sections for clause C. */
13457 static bool
13458 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13460 bool maybe_zero_len = false;
13461 unsigned int first_non_one = 0;
13462 auto_vec<tree, 10> types;
13463 tree *tp = &OMP_CLAUSE_DECL (c);
13464 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13465 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13466 && TREE_CODE (*tp) == TREE_LIST
13467 && TREE_PURPOSE (*tp)
13468 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13469 tp = &TREE_VALUE (*tp);
13470 tree first = handle_omp_array_sections_1 (c, *tp, types,
13471 maybe_zero_len, first_non_one,
13472 ort);
13473 if (first == error_mark_node)
13474 return true;
13475 if (first == NULL_TREE)
13476 return false;
13477 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13478 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13480 tree t = *tp;
13481 tree tem = NULL_TREE;
13482 /* Need to evaluate side effects in the length expressions
13483 if any. */
13484 while (TREE_CODE (t) == TREE_LIST)
13486 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13488 if (tem == NULL_TREE)
13489 tem = TREE_VALUE (t);
13490 else
13491 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13492 TREE_VALUE (t), tem);
13494 t = TREE_CHAIN (t);
13496 if (tem)
13497 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13498 first = c_fully_fold (first, false, NULL, true);
13499 *tp = first;
13501 else
13503 unsigned int num = types.length (), i;
13504 tree t, side_effects = NULL_TREE, size = NULL_TREE;
13505 tree condition = NULL_TREE;
13507 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13508 maybe_zero_len = true;
13510 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13511 t = TREE_CHAIN (t))
13513 tree low_bound = TREE_PURPOSE (t);
13514 tree length = TREE_VALUE (t);
13516 i--;
13517 if (low_bound
13518 && TREE_CODE (low_bound) == INTEGER_CST
13519 && TYPE_PRECISION (TREE_TYPE (low_bound))
13520 > TYPE_PRECISION (sizetype))
13521 low_bound = fold_convert (sizetype, low_bound);
13522 if (length
13523 && TREE_CODE (length) == INTEGER_CST
13524 && TYPE_PRECISION (TREE_TYPE (length))
13525 > TYPE_PRECISION (sizetype))
13526 length = fold_convert (sizetype, length);
13527 if (low_bound == NULL_TREE)
13528 low_bound = integer_zero_node;
13529 if (!maybe_zero_len && i > first_non_one)
13531 if (integer_nonzerop (low_bound))
13532 goto do_warn_noncontiguous;
13533 if (length != NULL_TREE
13534 && TREE_CODE (length) == INTEGER_CST
13535 && TYPE_DOMAIN (types[i])
13536 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13537 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13538 == INTEGER_CST)
13540 tree size;
13541 size = size_binop (PLUS_EXPR,
13542 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13543 size_one_node);
13544 if (!tree_int_cst_equal (length, size))
13546 do_warn_noncontiguous:
13547 error_at (OMP_CLAUSE_LOCATION (c),
13548 "array section is not contiguous in %qs "
13549 "clause",
13550 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13551 return true;
13554 if (length != NULL_TREE
13555 && TREE_SIDE_EFFECTS (length))
13557 if (side_effects == NULL_TREE)
13558 side_effects = length;
13559 else
13560 side_effects = build2 (COMPOUND_EXPR,
13561 TREE_TYPE (side_effects),
13562 length, side_effects);
13565 else
13567 tree l;
13569 if (i > first_non_one
13570 && ((length && integer_nonzerop (length))
13571 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13572 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13573 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13574 continue;
13575 if (length)
13576 l = fold_convert (sizetype, length);
13577 else
13579 l = size_binop (PLUS_EXPR,
13580 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13581 size_one_node);
13582 l = size_binop (MINUS_EXPR, l,
13583 fold_convert (sizetype, low_bound));
13585 if (i > first_non_one)
13587 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13588 size_zero_node);
13589 if (condition == NULL_TREE)
13590 condition = l;
13591 else
13592 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13593 l, condition);
13595 else if (size == NULL_TREE)
13597 size = size_in_bytes (TREE_TYPE (types[i]));
13598 tree eltype = TREE_TYPE (types[num - 1]);
13599 while (TREE_CODE (eltype) == ARRAY_TYPE)
13600 eltype = TREE_TYPE (eltype);
13601 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13602 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13603 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13605 if (integer_zerop (size)
13606 || integer_zerop (size_in_bytes (eltype)))
13608 error_at (OMP_CLAUSE_LOCATION (c),
13609 "zero length array section in %qs clause",
13610 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13611 return error_mark_node;
13613 size = size_binop (EXACT_DIV_EXPR, size,
13614 size_in_bytes (eltype));
13616 size = size_binop (MULT_EXPR, size, l);
13617 if (condition)
13618 size = fold_build3 (COND_EXPR, sizetype, condition,
13619 size, size_zero_node);
13621 else
13622 size = size_binop (MULT_EXPR, size, l);
13625 if (side_effects)
13626 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13627 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13628 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13629 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13631 size = size_binop (MINUS_EXPR, size, size_one_node);
13632 size = c_fully_fold (size, false, NULL);
13633 size = save_expr (size);
13634 tree index_type = build_index_type (size);
13635 tree eltype = TREE_TYPE (first);
13636 while (TREE_CODE (eltype) == ARRAY_TYPE)
13637 eltype = TREE_TYPE (eltype);
13638 tree type = build_array_type (eltype, index_type);
13639 tree ptype = build_pointer_type (eltype);
13640 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13641 t = build_fold_addr_expr (t);
13642 tree t2 = build_fold_addr_expr (first);
13643 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13644 ptrdiff_type_node, t2);
13645 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13646 ptrdiff_type_node, t2,
13647 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13648 ptrdiff_type_node, t));
13649 t2 = c_fully_fold (t2, false, NULL);
13650 if (tree_fits_shwi_p (t2))
13651 t = build2 (MEM_REF, type, t,
13652 build_int_cst (ptype, tree_to_shwi (t2)));
13653 else
13655 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13656 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13657 TREE_TYPE (t), t, t2);
13658 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13660 OMP_CLAUSE_DECL (c) = t;
13661 return false;
13663 first = c_fully_fold (first, false, NULL);
13664 OMP_CLAUSE_DECL (c) = first;
13665 if (size)
13666 size = c_fully_fold (size, false, NULL);
13667 OMP_CLAUSE_SIZE (c) = size;
13668 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13669 || (TREE_CODE (t) == COMPONENT_REF
13670 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13671 return false;
13672 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13673 switch (OMP_CLAUSE_MAP_KIND (c))
13675 case GOMP_MAP_ALLOC:
13676 case GOMP_MAP_IF_PRESENT:
13677 case GOMP_MAP_TO:
13678 case GOMP_MAP_FROM:
13679 case GOMP_MAP_TOFROM:
13680 case GOMP_MAP_ALWAYS_TO:
13681 case GOMP_MAP_ALWAYS_FROM:
13682 case GOMP_MAP_ALWAYS_TOFROM:
13683 case GOMP_MAP_RELEASE:
13684 case GOMP_MAP_DELETE:
13685 case GOMP_MAP_FORCE_TO:
13686 case GOMP_MAP_FORCE_FROM:
13687 case GOMP_MAP_FORCE_TOFROM:
13688 case GOMP_MAP_FORCE_PRESENT:
13689 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13690 break;
13691 default:
13692 break;
13694 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13695 if (TREE_CODE (t) == COMPONENT_REF)
13696 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
13697 else
13698 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13699 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
13700 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13701 && !c_mark_addressable (t))
13702 return false;
13703 OMP_CLAUSE_DECL (c2) = t;
13704 t = build_fold_addr_expr (first);
13705 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13706 tree ptr = OMP_CLAUSE_DECL (c2);
13707 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13708 ptr = build_fold_addr_expr (ptr);
13709 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13710 ptrdiff_type_node, t,
13711 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13712 ptrdiff_type_node, ptr));
13713 t = c_fully_fold (t, false, NULL);
13714 OMP_CLAUSE_SIZE (c2) = t;
13715 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13716 OMP_CLAUSE_CHAIN (c) = c2;
13718 return false;
13721 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13722 an inline call. But, remap
13723 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13724 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13726 static tree
13727 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13728 tree decl, tree placeholder)
13730 copy_body_data id;
13731 hash_map<tree, tree> decl_map;
13733 decl_map.put (omp_decl1, placeholder);
13734 decl_map.put (omp_decl2, decl);
13735 memset (&id, 0, sizeof (id));
13736 id.src_fn = DECL_CONTEXT (omp_decl1);
13737 id.dst_fn = current_function_decl;
13738 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13739 id.decl_map = &decl_map;
13741 id.copy_decl = copy_decl_no_change;
13742 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13743 id.transform_new_cfg = true;
13744 id.transform_return_to_modify = false;
13745 id.transform_lang_insert_block = NULL;
13746 id.eh_lp_nr = 0;
13747 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13748 return stmt;
13751 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13752 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13754 static tree
13755 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13757 if (*tp == (tree) data)
13758 return *tp;
13759 return NULL_TREE;
13762 /* Similarly, but also walk aggregate fields. */
13764 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13766 static tree
13767 c_find_omp_var_r (tree *tp, int *, void *data)
13769 if (*tp == ((struct c_find_omp_var_s *) data)->var)
13770 return *tp;
13771 if (RECORD_OR_UNION_TYPE_P (*tp))
13773 tree field;
13774 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13776 for (field = TYPE_FIELDS (*tp); field;
13777 field = DECL_CHAIN (field))
13778 if (TREE_CODE (field) == FIELD_DECL)
13780 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13781 c_find_omp_var_r, data, pset);
13782 if (ret)
13783 return ret;
13784 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13785 if (ret)
13786 return ret;
13787 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13788 pset);
13789 if (ret)
13790 return ret;
13791 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13792 if (ret)
13793 return ret;
13796 else if (INTEGRAL_TYPE_P (*tp))
13797 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13798 ((struct c_find_omp_var_s *) data)->pset);
13799 return NULL_TREE;
13802 /* Finish OpenMP iterators ITER. Return true if they are errorneous
13803 and clauses containing them should be removed. */
13805 static bool
13806 c_omp_finish_iterators (tree iter)
13808 bool ret = false;
13809 for (tree it = iter; it; it = TREE_CHAIN (it))
13811 tree var = TREE_VEC_ELT (it, 0);
13812 tree begin = TREE_VEC_ELT (it, 1);
13813 tree end = TREE_VEC_ELT (it, 2);
13814 tree step = TREE_VEC_ELT (it, 3);
13815 tree orig_step;
13816 tree type = TREE_TYPE (var);
13817 location_t loc = DECL_SOURCE_LOCATION (var);
13818 if (type == error_mark_node)
13820 ret = true;
13821 continue;
13823 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13825 error_at (loc, "iterator %qD has neither integral nor pointer type",
13826 var);
13827 ret = true;
13828 continue;
13830 else if (TYPE_ATOMIC (type))
13832 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
13833 ret = true;
13834 continue;
13836 else if (TYPE_READONLY (type))
13838 error_at (loc, "iterator %qD has const qualified type", var);
13839 ret = true;
13840 continue;
13842 else if (step == error_mark_node
13843 || TREE_TYPE (step) == error_mark_node)
13845 ret = true;
13846 continue;
13848 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13850 error_at (EXPR_LOC_OR_LOC (step, loc),
13851 "iterator step with non-integral type");
13852 ret = true;
13853 continue;
13855 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
13856 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
13857 orig_step = save_expr (c_fully_fold (step, false, NULL));
13858 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
13859 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
13860 if (POINTER_TYPE_P (type))
13862 begin = save_expr (begin);
13863 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
13864 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
13865 fold_convert (sizetype, step),
13866 fold_convert (sizetype, begin));
13867 step = fold_convert (ssizetype, step);
13869 if (integer_zerop (step))
13871 error_at (loc, "iterator %qD has zero step", var);
13872 ret = true;
13873 continue;
13876 if (begin == error_mark_node
13877 || end == error_mark_node
13878 || step == error_mark_node
13879 || orig_step == error_mark_node)
13881 ret = true;
13882 continue;
13884 hash_set<tree> pset;
13885 tree it2;
13886 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
13888 tree var2 = TREE_VEC_ELT (it2, 0);
13889 tree begin2 = TREE_VEC_ELT (it2, 1);
13890 tree end2 = TREE_VEC_ELT (it2, 2);
13891 tree step2 = TREE_VEC_ELT (it2, 3);
13892 tree type2 = TREE_TYPE (var2);
13893 location_t loc2 = DECL_SOURCE_LOCATION (var2);
13894 struct c_find_omp_var_s data = { var, &pset };
13895 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
13897 error_at (loc2,
13898 "type of iterator %qD refers to outer iterator %qD",
13899 var2, var);
13900 break;
13902 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
13904 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
13905 "begin expression refers to outer iterator %qD", var);
13906 break;
13908 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
13910 error_at (EXPR_LOC_OR_LOC (end2, loc2),
13911 "end expression refers to outer iterator %qD", var);
13912 break;
13914 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
13916 error_at (EXPR_LOC_OR_LOC (step2, loc2),
13917 "step expression refers to outer iterator %qD", var);
13918 break;
13921 if (it2)
13923 ret = true;
13924 continue;
13926 TREE_VEC_ELT (it, 1) = begin;
13927 TREE_VEC_ELT (it, 2) = end;
13928 TREE_VEC_ELT (it, 3) = step;
13929 TREE_VEC_ELT (it, 4) = orig_step;
13931 return ret;
13934 /* Ensure that pointers are used in OpenACC attach and detach clauses.
13935 Return true if an error has been detected. */
13937 static bool
13938 c_oacc_check_attachments (tree c)
13940 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13941 return false;
13943 /* OpenACC attach / detach clauses must be pointers. */
13944 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13945 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
13947 tree t = OMP_CLAUSE_DECL (c);
13949 while (TREE_CODE (t) == TREE_LIST)
13950 t = TREE_CHAIN (t);
13952 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13954 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
13955 c_omp_map_clause_name (c, true));
13956 return true;
13960 return false;
13963 /* For all elements of CLAUSES, validate them against their constraints.
13964 Remove any elements from the list that are invalid. */
13966 tree
13967 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
13969 bitmap_head generic_head, firstprivate_head, lastprivate_head;
13970 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
13971 bitmap_head oacc_reduction_head;
13972 tree c, t, type, *pc;
13973 tree simdlen = NULL_TREE, safelen = NULL_TREE;
13974 bool branch_seen = false;
13975 bool copyprivate_seen = false;
13976 bool mergeable_seen = false;
13977 tree *detach_seen = NULL;
13978 bool linear_variable_step_check = false;
13979 tree *nowait_clause = NULL;
13980 tree ordered_clause = NULL_TREE;
13981 tree schedule_clause = NULL_TREE;
13982 bool oacc_async = false;
13983 tree last_iterators = NULL_TREE;
13984 bool last_iterators_remove = false;
13985 tree *nogroup_seen = NULL;
13986 tree *order_clause = NULL;
13987 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
13988 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
13989 int reduction_seen = 0;
13990 bool allocate_seen = false;
13991 bool implicit_moved = false;
13992 bool target_in_reduction_seen = false;
13994 bitmap_obstack_initialize (NULL);
13995 bitmap_initialize (&generic_head, &bitmap_default_obstack);
13996 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13997 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
13998 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
13999 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
14000 bitmap_initialize (&map_head, &bitmap_default_obstack);
14001 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
14002 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
14003 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
14004 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
14005 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
14007 if (ort & C_ORT_ACC)
14008 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14009 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
14011 oacc_async = true;
14012 break;
14015 for (pc = &clauses, c = clauses; c ; c = *pc)
14017 bool remove = false;
14018 bool need_complete = false;
14019 bool need_implicitly_determined = false;
14021 switch (OMP_CLAUSE_CODE (c))
14023 case OMP_CLAUSE_SHARED:
14024 need_implicitly_determined = true;
14025 goto check_dup_generic;
14027 case OMP_CLAUSE_PRIVATE:
14028 need_complete = true;
14029 need_implicitly_determined = true;
14030 goto check_dup_generic;
14032 case OMP_CLAUSE_REDUCTION:
14033 if (reduction_seen == 0)
14034 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14035 else if (reduction_seen != -2
14036 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14037 ? -1 : 1))
14039 error_at (OMP_CLAUSE_LOCATION (c),
14040 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14041 "on the same construct");
14042 reduction_seen = -2;
14044 /* FALLTHRU */
14045 case OMP_CLAUSE_IN_REDUCTION:
14046 case OMP_CLAUSE_TASK_REDUCTION:
14047 need_implicitly_determined = true;
14048 t = OMP_CLAUSE_DECL (c);
14049 if (TREE_CODE (t) == TREE_LIST)
14051 if (handle_omp_array_sections (c, ort))
14053 remove = true;
14054 break;
14057 t = OMP_CLAUSE_DECL (c);
14058 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14059 && OMP_CLAUSE_REDUCTION_INSCAN (c))
14061 error_at (OMP_CLAUSE_LOCATION (c),
14062 "%<inscan%> %<reduction%> clause with array "
14063 "section");
14064 remove = true;
14065 break;
14068 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14069 if (t == error_mark_node)
14071 remove = true;
14072 break;
14074 if (oacc_async)
14075 c_mark_addressable (t);
14076 type = TREE_TYPE (t);
14077 if (TREE_CODE (t) == MEM_REF)
14078 type = TREE_TYPE (type);
14079 if (TREE_CODE (type) == ARRAY_TYPE)
14081 tree oatype = type;
14082 gcc_assert (TREE_CODE (t) != MEM_REF);
14083 while (TREE_CODE (type) == ARRAY_TYPE)
14084 type = TREE_TYPE (type);
14085 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14087 error_at (OMP_CLAUSE_LOCATION (c),
14088 "%qD in %<reduction%> clause is a zero size array",
14090 remove = true;
14091 break;
14093 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14094 TYPE_SIZE_UNIT (type));
14095 if (integer_zerop (size))
14097 error_at (OMP_CLAUSE_LOCATION (c),
14098 "%qD in %<reduction%> clause is a zero size array",
14100 remove = true;
14101 break;
14103 size = size_binop (MINUS_EXPR, size, size_one_node);
14104 size = save_expr (size);
14105 tree index_type = build_index_type (size);
14106 tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14107 index_type);
14108 atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14109 tree ptype = build_pointer_type (type);
14110 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14111 t = build_fold_addr_expr (t);
14112 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14113 OMP_CLAUSE_DECL (c) = t;
14115 if (TYPE_ATOMIC (type))
14117 error_at (OMP_CLAUSE_LOCATION (c),
14118 "%<_Atomic%> %qE in %<reduction%> clause", t);
14119 remove = true;
14120 break;
14122 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14123 || OMP_CLAUSE_REDUCTION_TASK (c))
14125 /* Disallow zero sized or potentially zero sized task
14126 reductions. */
14127 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14129 error_at (OMP_CLAUSE_LOCATION (c),
14130 "zero sized type %qT in %qs clause", type,
14131 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14132 remove = true;
14133 break;
14135 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14137 error_at (OMP_CLAUSE_LOCATION (c),
14138 "variable sized type %qT in %qs clause", type,
14139 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14140 remove = true;
14141 break;
14144 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14145 && (FLOAT_TYPE_P (type)
14146 || TREE_CODE (type) == COMPLEX_TYPE))
14148 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14149 const char *r_name = NULL;
14151 switch (r_code)
14153 case PLUS_EXPR:
14154 case MULT_EXPR:
14155 case MINUS_EXPR:
14156 case TRUTH_ANDIF_EXPR:
14157 case TRUTH_ORIF_EXPR:
14158 break;
14159 case MIN_EXPR:
14160 if (TREE_CODE (type) == COMPLEX_TYPE)
14161 r_name = "min";
14162 break;
14163 case MAX_EXPR:
14164 if (TREE_CODE (type) == COMPLEX_TYPE)
14165 r_name = "max";
14166 break;
14167 case BIT_AND_EXPR:
14168 r_name = "&";
14169 break;
14170 case BIT_XOR_EXPR:
14171 r_name = "^";
14172 break;
14173 case BIT_IOR_EXPR:
14174 r_name = "|";
14175 break;
14176 default:
14177 gcc_unreachable ();
14179 if (r_name)
14181 error_at (OMP_CLAUSE_LOCATION (c),
14182 "%qE has invalid type for %<reduction(%s)%>",
14183 t, r_name);
14184 remove = true;
14185 break;
14188 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14190 error_at (OMP_CLAUSE_LOCATION (c),
14191 "user defined reduction not found for %qE", t);
14192 remove = true;
14193 break;
14195 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14197 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14198 type = TYPE_MAIN_VARIANT (type);
14199 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14200 VAR_DECL, NULL_TREE, type);
14201 tree decl_placeholder = NULL_TREE;
14202 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14203 DECL_ARTIFICIAL (placeholder) = 1;
14204 DECL_IGNORED_P (placeholder) = 1;
14205 if (TREE_CODE (t) == MEM_REF)
14207 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14208 VAR_DECL, NULL_TREE, type);
14209 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14210 DECL_ARTIFICIAL (decl_placeholder) = 1;
14211 DECL_IGNORED_P (decl_placeholder) = 1;
14213 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14214 c_mark_addressable (placeholder);
14215 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14216 c_mark_addressable (decl_placeholder ? decl_placeholder
14217 : OMP_CLAUSE_DECL (c));
14218 OMP_CLAUSE_REDUCTION_MERGE (c)
14219 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14220 TREE_VEC_ELT (list, 0),
14221 TREE_VEC_ELT (list, 1),
14222 decl_placeholder ? decl_placeholder
14223 : OMP_CLAUSE_DECL (c), placeholder);
14224 OMP_CLAUSE_REDUCTION_MERGE (c)
14225 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14226 void_type_node, NULL_TREE,
14227 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14228 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14229 if (TREE_VEC_LENGTH (list) == 6)
14231 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14232 c_mark_addressable (decl_placeholder ? decl_placeholder
14233 : OMP_CLAUSE_DECL (c));
14234 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14235 c_mark_addressable (placeholder);
14236 tree init = TREE_VEC_ELT (list, 5);
14237 if (init == error_mark_node)
14238 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14239 OMP_CLAUSE_REDUCTION_INIT (c)
14240 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14241 TREE_VEC_ELT (list, 3),
14242 decl_placeholder ? decl_placeholder
14243 : OMP_CLAUSE_DECL (c), placeholder);
14244 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14246 tree v = decl_placeholder ? decl_placeholder : t;
14247 OMP_CLAUSE_REDUCTION_INIT (c)
14248 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14249 OMP_CLAUSE_REDUCTION_INIT (c));
14251 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14252 c_find_omp_placeholder_r,
14253 placeholder, NULL))
14254 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14256 else
14258 tree init;
14259 tree v = decl_placeholder ? decl_placeholder : t;
14260 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14261 init = build_constructor (TREE_TYPE (v), NULL);
14262 else
14263 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14264 OMP_CLAUSE_REDUCTION_INIT (c)
14265 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14267 OMP_CLAUSE_REDUCTION_INIT (c)
14268 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14269 void_type_node, NULL_TREE,
14270 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14271 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14273 if (TREE_CODE (t) == MEM_REF)
14275 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14276 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14277 != INTEGER_CST)
14279 sorry ("variable length element type in array "
14280 "%<reduction%> clause");
14281 remove = true;
14282 break;
14284 t = TREE_OPERAND (t, 0);
14285 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14286 t = TREE_OPERAND (t, 0);
14287 if (TREE_CODE (t) == ADDR_EXPR)
14288 t = TREE_OPERAND (t, 0);
14290 goto check_dup_generic_t;
14292 case OMP_CLAUSE_COPYPRIVATE:
14293 copyprivate_seen = true;
14294 if (nowait_clause)
14296 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14297 "%<nowait%> clause must not be used together "
14298 "with %<copyprivate%>");
14299 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14300 nowait_clause = NULL;
14302 goto check_dup_generic;
14304 case OMP_CLAUSE_COPYIN:
14305 t = OMP_CLAUSE_DECL (c);
14306 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14308 error_at (OMP_CLAUSE_LOCATION (c),
14309 "%qE must be %<threadprivate%> for %<copyin%>", t);
14310 remove = true;
14311 break;
14313 goto check_dup_generic;
14315 case OMP_CLAUSE_LINEAR:
14316 if (ort != C_ORT_OMP_DECLARE_SIMD)
14317 need_implicitly_determined = true;
14318 t = OMP_CLAUSE_DECL (c);
14319 if (ort != C_ORT_OMP_DECLARE_SIMD
14320 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
14322 error_at (OMP_CLAUSE_LOCATION (c),
14323 "modifier should not be specified in %<linear%> "
14324 "clause on %<simd%> or %<for%> constructs");
14325 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14327 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14328 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14330 error_at (OMP_CLAUSE_LOCATION (c),
14331 "linear clause applied to non-integral non-pointer "
14332 "variable with type %qT", TREE_TYPE (t));
14333 remove = true;
14334 break;
14336 if (TYPE_ATOMIC (TREE_TYPE (t)))
14338 error_at (OMP_CLAUSE_LOCATION (c),
14339 "%<_Atomic%> %qD in %<linear%> clause", t);
14340 remove = true;
14341 break;
14343 if (ort == C_ORT_OMP_DECLARE_SIMD)
14345 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14346 if (TREE_CODE (s) == PARM_DECL)
14348 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14349 /* map_head bitmap is used as uniform_head if
14350 declare_simd. */
14351 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14352 linear_variable_step_check = true;
14353 goto check_dup_generic;
14355 if (TREE_CODE (s) != INTEGER_CST)
14357 error_at (OMP_CLAUSE_LOCATION (c),
14358 "%<linear%> clause step %qE is neither constant "
14359 "nor a parameter", s);
14360 remove = true;
14361 break;
14364 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14366 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14367 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14368 OMP_CLAUSE_DECL (c), s);
14369 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14370 sizetype, fold_convert (sizetype, s),
14371 fold_convert
14372 (sizetype, OMP_CLAUSE_DECL (c)));
14373 if (s == error_mark_node)
14374 s = size_one_node;
14375 OMP_CLAUSE_LINEAR_STEP (c) = s;
14377 else
14378 OMP_CLAUSE_LINEAR_STEP (c)
14379 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14380 goto check_dup_generic;
14382 check_dup_generic:
14383 t = OMP_CLAUSE_DECL (c);
14384 check_dup_generic_t:
14385 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14387 error_at (OMP_CLAUSE_LOCATION (c),
14388 "%qE is not a variable in clause %qs", t,
14389 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14390 remove = true;
14392 else if ((ort == C_ORT_ACC
14393 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14394 || (ort == C_ORT_OMP
14395 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14396 || (OMP_CLAUSE_CODE (c)
14397 == OMP_CLAUSE_USE_DEVICE_ADDR)))
14398 || (ort == C_ORT_OMP_TARGET
14399 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
14401 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14402 && (bitmap_bit_p (&generic_head, DECL_UID (t))
14403 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
14405 error_at (OMP_CLAUSE_LOCATION (c),
14406 "%qD appears more than once in data-sharing "
14407 "clauses", t);
14408 remove = true;
14409 break;
14411 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
14412 target_in_reduction_seen = true;
14413 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14415 error_at (OMP_CLAUSE_LOCATION (c),
14416 ort == C_ORT_ACC
14417 ? "%qD appears more than once in reduction clauses"
14418 : "%qD appears more than once in data clauses",
14420 remove = true;
14422 else
14423 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14425 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14426 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14427 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
14428 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14430 error_at (OMP_CLAUSE_LOCATION (c),
14431 "%qE appears more than once in data clauses", t);
14432 remove = true;
14434 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14435 && bitmap_bit_p (&map_head, DECL_UID (t)))
14437 if (ort == C_ORT_ACC)
14438 error_at (OMP_CLAUSE_LOCATION (c),
14439 "%qD appears more than once in data clauses", t);
14440 else
14441 error_at (OMP_CLAUSE_LOCATION (c),
14442 "%qD appears both in data and map clauses", t);
14443 remove = true;
14445 else
14446 bitmap_set_bit (&generic_head, DECL_UID (t));
14447 break;
14449 case OMP_CLAUSE_FIRSTPRIVATE:
14450 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
14452 move_implicit:
14453 implicit_moved = true;
14454 /* Move firstprivate and map clauses with
14455 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
14456 clauses chain. */
14457 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
14458 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
14459 while (*pc1)
14460 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
14461 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
14463 *pc3 = *pc1;
14464 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
14465 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14467 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
14468 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
14470 *pc2 = *pc1;
14471 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
14472 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14474 else
14475 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
14476 *pc3 = NULL;
14477 *pc2 = cl2;
14478 *pc1 = cl1;
14479 continue;
14481 t = OMP_CLAUSE_DECL (c);
14482 need_complete = true;
14483 need_implicitly_determined = true;
14484 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14486 error_at (OMP_CLAUSE_LOCATION (c),
14487 "%qE is not a variable in clause %<firstprivate%>", t);
14488 remove = true;
14490 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14491 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
14492 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14493 remove = true;
14494 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14495 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14496 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14498 error_at (OMP_CLAUSE_LOCATION (c),
14499 "%qE appears more than once in data clauses", t);
14500 remove = true;
14502 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14504 if (ort == C_ORT_ACC)
14505 error_at (OMP_CLAUSE_LOCATION (c),
14506 "%qD appears more than once in data clauses", t);
14507 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14508 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
14509 /* Silently drop the clause. */;
14510 else
14511 error_at (OMP_CLAUSE_LOCATION (c),
14512 "%qD appears both in data and map clauses", t);
14513 remove = true;
14515 else
14516 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14517 break;
14519 case OMP_CLAUSE_LASTPRIVATE:
14520 t = OMP_CLAUSE_DECL (c);
14521 need_complete = true;
14522 need_implicitly_determined = true;
14523 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14525 error_at (OMP_CLAUSE_LOCATION (c),
14526 "%qE is not a variable in clause %<lastprivate%>", t);
14527 remove = true;
14529 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14530 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14532 error_at (OMP_CLAUSE_LOCATION (c),
14533 "%qE appears more than once in data clauses", t);
14534 remove = true;
14536 else
14537 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14538 break;
14540 case OMP_CLAUSE_ALIGNED:
14541 t = OMP_CLAUSE_DECL (c);
14542 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14544 error_at (OMP_CLAUSE_LOCATION (c),
14545 "%qE is not a variable in %<aligned%> clause", t);
14546 remove = true;
14548 else if (!POINTER_TYPE_P (TREE_TYPE (t))
14549 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14551 error_at (OMP_CLAUSE_LOCATION (c),
14552 "%qE in %<aligned%> clause is neither a pointer nor "
14553 "an array", t);
14554 remove = true;
14556 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14558 error_at (OMP_CLAUSE_LOCATION (c),
14559 "%<_Atomic%> %qD in %<aligned%> clause", t);
14560 remove = true;
14561 break;
14563 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14565 error_at (OMP_CLAUSE_LOCATION (c),
14566 "%qE appears more than once in %<aligned%> clauses",
14568 remove = true;
14570 else
14571 bitmap_set_bit (&aligned_head, DECL_UID (t));
14572 break;
14574 case OMP_CLAUSE_NONTEMPORAL:
14575 t = OMP_CLAUSE_DECL (c);
14576 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14578 error_at (OMP_CLAUSE_LOCATION (c),
14579 "%qE is not a variable in %<nontemporal%> clause", t);
14580 remove = true;
14582 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14584 error_at (OMP_CLAUSE_LOCATION (c),
14585 "%qE appears more than once in %<nontemporal%> "
14586 "clauses", t);
14587 remove = true;
14589 else
14590 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14591 break;
14593 case OMP_CLAUSE_ALLOCATE:
14594 t = OMP_CLAUSE_DECL (c);
14595 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14597 error_at (OMP_CLAUSE_LOCATION (c),
14598 "%qE is not a variable in %<allocate%> clause", t);
14599 remove = true;
14601 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14603 warning_at (OMP_CLAUSE_LOCATION (c), 0,
14604 "%qE appears more than once in %<allocate%> clauses",
14606 remove = true;
14608 else
14610 bitmap_set_bit (&aligned_head, DECL_UID (t));
14611 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
14612 allocate_seen = true;
14614 break;
14616 case OMP_CLAUSE_DEPEND:
14617 t = OMP_CLAUSE_DECL (c);
14618 if (t == NULL_TREE)
14620 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14621 == OMP_CLAUSE_DEPEND_SOURCE);
14622 break;
14624 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14626 gcc_assert (TREE_CODE (t) == TREE_LIST);
14627 for (; t; t = TREE_CHAIN (t))
14629 tree decl = TREE_VALUE (t);
14630 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14632 tree offset = TREE_PURPOSE (t);
14633 bool neg = wi::neg_p (wi::to_wide (offset));
14634 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14635 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14636 neg ? MINUS_EXPR : PLUS_EXPR,
14637 decl, offset);
14638 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14639 sizetype,
14640 fold_convert (sizetype, t2),
14641 fold_convert (sizetype, decl));
14642 if (t2 == error_mark_node)
14644 remove = true;
14645 break;
14647 TREE_PURPOSE (t) = t2;
14650 break;
14652 /* FALLTHRU */
14653 case OMP_CLAUSE_AFFINITY:
14654 t = OMP_CLAUSE_DECL (c);
14655 if (TREE_CODE (t) == TREE_LIST
14656 && TREE_PURPOSE (t)
14657 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14659 if (TREE_PURPOSE (t) != last_iterators)
14660 last_iterators_remove
14661 = c_omp_finish_iterators (TREE_PURPOSE (t));
14662 last_iterators = TREE_PURPOSE (t);
14663 t = TREE_VALUE (t);
14664 if (last_iterators_remove)
14665 t = error_mark_node;
14667 else
14668 last_iterators = NULL_TREE;
14669 if (TREE_CODE (t) == TREE_LIST)
14671 if (handle_omp_array_sections (c, ort))
14672 remove = true;
14673 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14674 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14676 error_at (OMP_CLAUSE_LOCATION (c),
14677 "%<depend%> clause with %<depobj%> dependence "
14678 "type on array section");
14679 remove = true;
14681 break;
14683 if (t == error_mark_node)
14684 remove = true;
14685 else if (!lvalue_p (t))
14687 error_at (OMP_CLAUSE_LOCATION (c),
14688 "%qE is not lvalue expression nor array section in "
14689 "%qs clause", t,
14690 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14691 remove = true;
14693 else if (TREE_CODE (t) == COMPONENT_REF
14694 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14696 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14697 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
14698 error_at (OMP_CLAUSE_LOCATION (c),
14699 "bit-field %qE in %qs clause", t,
14700 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14701 remove = true;
14703 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14704 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14706 if (!c_omp_depend_t_p (TREE_TYPE (t)))
14708 error_at (OMP_CLAUSE_LOCATION (c),
14709 "%qE does not have %<omp_depend_t%> type in "
14710 "%<depend%> clause with %<depobj%> dependence "
14711 "type", t);
14712 remove = true;
14715 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14716 && c_omp_depend_t_p (TREE_TYPE (t)))
14718 error_at (OMP_CLAUSE_LOCATION (c),
14719 "%qE should not have %<omp_depend_t%> type in "
14720 "%<depend%> clause with dependence type other than "
14721 "%<depobj%>", t);
14722 remove = true;
14724 if (!remove)
14726 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c), ADDR_EXPR,
14727 t, false);
14728 if (addr == error_mark_node)
14729 remove = true;
14730 else
14732 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14733 RO_UNARY_STAR);
14734 if (t == error_mark_node)
14735 remove = true;
14736 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14737 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14738 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14739 == TREE_VEC))
14740 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14741 else
14742 OMP_CLAUSE_DECL (c) = t;
14745 break;
14747 case OMP_CLAUSE_MAP:
14748 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
14749 goto move_implicit;
14750 /* FALLTHRU */
14751 case OMP_CLAUSE_TO:
14752 case OMP_CLAUSE_FROM:
14753 case OMP_CLAUSE__CACHE_:
14754 t = OMP_CLAUSE_DECL (c);
14755 if (TREE_CODE (t) == TREE_LIST)
14757 if (handle_omp_array_sections (c, ort))
14758 remove = true;
14759 else
14761 t = OMP_CLAUSE_DECL (c);
14762 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14764 error_at (OMP_CLAUSE_LOCATION (c),
14765 "array section does not have mappable type "
14766 "in %qs clause",
14767 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14768 remove = true;
14770 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14772 error_at (OMP_CLAUSE_LOCATION (c),
14773 "%<_Atomic%> %qE in %qs clause", t,
14774 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14775 remove = true;
14777 while (TREE_CODE (t) == ARRAY_REF)
14778 t = TREE_OPERAND (t, 0);
14779 if (TREE_CODE (t) == COMPONENT_REF
14780 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14782 while (TREE_CODE (t) == COMPONENT_REF)
14783 t = TREE_OPERAND (t, 0);
14784 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14785 && OMP_CLAUSE_MAP_IMPLICIT (c)
14786 && (bitmap_bit_p (&map_head, DECL_UID (t))
14787 || bitmap_bit_p (&map_field_head, DECL_UID (t))
14788 || bitmap_bit_p (&map_firstprivate_head,
14789 DECL_UID (t))))
14791 remove = true;
14792 break;
14794 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14795 break;
14796 if (bitmap_bit_p (&map_head, DECL_UID (t)))
14798 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14799 error_at (OMP_CLAUSE_LOCATION (c),
14800 "%qD appears more than once in motion "
14801 "clauses", t);
14802 else if (ort == C_ORT_ACC)
14803 error_at (OMP_CLAUSE_LOCATION (c),
14804 "%qD appears more than once in data "
14805 "clauses", t);
14806 else
14807 error_at (OMP_CLAUSE_LOCATION (c),
14808 "%qD appears more than once in map "
14809 "clauses", t);
14810 remove = true;
14812 else
14814 bitmap_set_bit (&map_head, DECL_UID (t));
14815 bitmap_set_bit (&map_field_head, DECL_UID (t));
14819 if (c_oacc_check_attachments (c))
14820 remove = true;
14821 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14822 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14823 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
14824 /* In this case, we have a single array element which is a
14825 pointer, and we already set OMP_CLAUSE_SIZE in
14826 handle_omp_array_sections above. For attach/detach clauses,
14827 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
14828 here. */
14829 OMP_CLAUSE_SIZE (c) = size_zero_node;
14830 break;
14832 if (t == error_mark_node)
14834 remove = true;
14835 break;
14837 /* OpenACC attach / detach clauses must be pointers. */
14838 if (c_oacc_check_attachments (c))
14840 remove = true;
14841 break;
14843 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14844 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14845 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
14846 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
14847 bias) to zero here, so it is not set erroneously to the pointer
14848 size later on in gimplify.c. */
14849 OMP_CLAUSE_SIZE (c) = size_zero_node;
14850 if (TREE_CODE (t) == COMPONENT_REF
14851 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
14853 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
14855 error_at (OMP_CLAUSE_LOCATION (c),
14856 "bit-field %qE in %qs clause",
14857 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14858 remove = true;
14860 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14862 error_at (OMP_CLAUSE_LOCATION (c),
14863 "%qE does not have a mappable type in %qs clause",
14864 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14865 remove = true;
14867 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14869 error_at (OMP_CLAUSE_LOCATION (c),
14870 "%<_Atomic%> %qE in %qs clause", t,
14871 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14872 remove = true;
14874 while (TREE_CODE (t) == COMPONENT_REF)
14876 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
14877 == UNION_TYPE)
14879 error_at (OMP_CLAUSE_LOCATION (c),
14880 "%qE is a member of a union", t);
14881 remove = true;
14882 break;
14884 t = TREE_OPERAND (t, 0);
14885 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
14887 if (maybe_ne (mem_ref_offset (t), 0))
14888 error_at (OMP_CLAUSE_LOCATION (c),
14889 "cannot dereference %qE in %qs clause", t,
14890 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14891 else
14892 t = TREE_OPERAND (t, 0);
14895 if (remove)
14896 break;
14897 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14899 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
14900 || (ort != C_ORT_ACC
14901 && bitmap_bit_p (&map_head, DECL_UID (t))))
14902 break;
14905 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14907 error_at (OMP_CLAUSE_LOCATION (c),
14908 "%qE is not a variable in %qs clause", t,
14909 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14910 remove = true;
14912 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14914 error_at (OMP_CLAUSE_LOCATION (c),
14915 "%qD is threadprivate variable in %qs clause", t,
14916 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14917 remove = true;
14919 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14920 || (OMP_CLAUSE_MAP_KIND (c)
14921 != GOMP_MAP_FIRSTPRIVATE_POINTER))
14922 && !c_mark_addressable (t))
14923 remove = true;
14924 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14925 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
14926 || (OMP_CLAUSE_MAP_KIND (c)
14927 == GOMP_MAP_FIRSTPRIVATE_POINTER)
14928 || (OMP_CLAUSE_MAP_KIND (c)
14929 == GOMP_MAP_FORCE_DEVICEPTR)))
14930 && t == OMP_CLAUSE_DECL (c)
14931 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14933 error_at (OMP_CLAUSE_LOCATION (c),
14934 "%qD does not have a mappable type in %qs clause", t,
14935 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14936 remove = true;
14938 else if (TREE_TYPE (t) == error_mark_node)
14939 remove = true;
14940 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
14942 error_at (OMP_CLAUSE_LOCATION (c),
14943 "%<_Atomic%> %qE in %qs clause", t,
14944 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14945 remove = true;
14947 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14948 && OMP_CLAUSE_MAP_IMPLICIT (c)
14949 && (bitmap_bit_p (&map_head, DECL_UID (t))
14950 || bitmap_bit_p (&map_field_head, DECL_UID (t))
14951 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))))
14952 remove = true;
14953 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14954 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
14956 if (bitmap_bit_p (&generic_head, DECL_UID (t))
14957 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14958 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14960 error_at (OMP_CLAUSE_LOCATION (c),
14961 "%qD appears more than once in data clauses", t);
14962 remove = true;
14964 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14966 if (ort == C_ORT_ACC)
14967 error_at (OMP_CLAUSE_LOCATION (c),
14968 "%qD appears more than once in data clauses", t);
14969 else
14970 error_at (OMP_CLAUSE_LOCATION (c),
14971 "%qD appears both in data and map clauses", t);
14972 remove = true;
14974 else
14975 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
14977 else if (bitmap_bit_p (&map_head, DECL_UID (t))
14978 && (ort == C_ORT_ACC
14979 || !bitmap_bit_p (&map_field_head, DECL_UID (t))))
14981 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14982 error_at (OMP_CLAUSE_LOCATION (c),
14983 "%qD appears more than once in motion clauses", t);
14984 else if (ort == C_ORT_ACC)
14985 error_at (OMP_CLAUSE_LOCATION (c),
14986 "%qD appears more than once in data clauses", t);
14987 else
14988 error_at (OMP_CLAUSE_LOCATION (c),
14989 "%qD appears more than once in map clauses", t);
14990 remove = true;
14992 else if (ort == C_ORT_ACC
14993 && bitmap_bit_p (&generic_head, DECL_UID (t)))
14995 error_at (OMP_CLAUSE_LOCATION (c),
14996 "%qD appears more than once in data clauses", t);
14997 remove = true;
14999 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
15001 if (ort == C_ORT_ACC)
15002 error_at (OMP_CLAUSE_LOCATION (c),
15003 "%qD appears more than once in data clauses", t);
15004 else
15005 error_at (OMP_CLAUSE_LOCATION (c),
15006 "%qD appears both in data and map clauses", t);
15007 remove = true;
15009 else
15011 bitmap_set_bit (&map_head, DECL_UID (t));
15012 if (t != OMP_CLAUSE_DECL (c)
15013 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
15014 bitmap_set_bit (&map_field_head, DECL_UID (t));
15016 break;
15018 case OMP_CLAUSE_TO_DECLARE:
15019 case OMP_CLAUSE_LINK:
15020 t = OMP_CLAUSE_DECL (c);
15021 if (TREE_CODE (t) == FUNCTION_DECL
15022 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
15024 else if (!VAR_P (t))
15026 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
15027 error_at (OMP_CLAUSE_LOCATION (c),
15028 "%qE is neither a variable nor a function name in "
15029 "clause %qs", t,
15030 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15031 else
15032 error_at (OMP_CLAUSE_LOCATION (c),
15033 "%qE is not a variable in clause %qs", t,
15034 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15035 remove = true;
15037 else if (DECL_THREAD_LOCAL_P (t))
15039 error_at (OMP_CLAUSE_LOCATION (c),
15040 "%qD is threadprivate variable in %qs clause", t,
15041 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15042 remove = true;
15044 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
15046 error_at (OMP_CLAUSE_LOCATION (c),
15047 "%qD does not have a mappable type in %qs clause", t,
15048 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15049 remove = true;
15051 if (remove)
15052 break;
15053 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15055 error_at (OMP_CLAUSE_LOCATION (c),
15056 "%qE appears more than once on the same "
15057 "%<declare target%> directive", t);
15058 remove = true;
15060 else
15061 bitmap_set_bit (&generic_head, DECL_UID (t));
15062 break;
15064 case OMP_CLAUSE_UNIFORM:
15065 t = OMP_CLAUSE_DECL (c);
15066 if (TREE_CODE (t) != PARM_DECL)
15068 if (DECL_P (t))
15069 error_at (OMP_CLAUSE_LOCATION (c),
15070 "%qD is not an argument in %<uniform%> clause", t);
15071 else
15072 error_at (OMP_CLAUSE_LOCATION (c),
15073 "%qE is not an argument in %<uniform%> clause", t);
15074 remove = true;
15075 break;
15077 /* map_head bitmap is used as uniform_head if declare_simd. */
15078 bitmap_set_bit (&map_head, DECL_UID (t));
15079 goto check_dup_generic;
15081 case OMP_CLAUSE_IS_DEVICE_PTR:
15082 case OMP_CLAUSE_USE_DEVICE_PTR:
15083 t = OMP_CLAUSE_DECL (c);
15084 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15086 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15087 && ort != C_ORT_ACC)
15089 error_at (OMP_CLAUSE_LOCATION (c),
15090 "%qs variable is not a pointer",
15091 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15092 remove = true;
15094 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15096 error_at (OMP_CLAUSE_LOCATION (c),
15097 "%qs variable is neither a pointer nor an array",
15098 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15099 remove = true;
15102 goto check_dup_generic;
15104 case OMP_CLAUSE_USE_DEVICE_ADDR:
15105 t = OMP_CLAUSE_DECL (c);
15106 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15107 c_mark_addressable (t);
15108 goto check_dup_generic;
15110 case OMP_CLAUSE_NOWAIT:
15111 if (copyprivate_seen)
15113 error_at (OMP_CLAUSE_LOCATION (c),
15114 "%<nowait%> clause must not be used together "
15115 "with %<copyprivate%>");
15116 remove = true;
15117 break;
15119 nowait_clause = pc;
15120 pc = &OMP_CLAUSE_CHAIN (c);
15121 continue;
15123 case OMP_CLAUSE_ORDER:
15124 if (ordered_clause)
15126 error_at (OMP_CLAUSE_LOCATION (c),
15127 "%<order%> clause must not be used together "
15128 "with %<ordered%>");
15129 remove = true;
15130 break;
15132 else if (order_clause)
15134 /* Silently remove duplicates. */
15135 remove = true;
15136 break;
15138 order_clause = pc;
15139 pc = &OMP_CLAUSE_CHAIN (c);
15140 continue;
15142 case OMP_CLAUSE_DETACH:
15143 t = OMP_CLAUSE_DECL (c);
15144 if (detach_seen)
15146 error_at (OMP_CLAUSE_LOCATION (c),
15147 "too many %qs clauses on a task construct",
15148 "detach");
15149 remove = true;
15150 break;
15152 detach_seen = pc;
15153 pc = &OMP_CLAUSE_CHAIN (c);
15154 c_mark_addressable (t);
15155 continue;
15157 case OMP_CLAUSE_IF:
15158 case OMP_CLAUSE_NUM_THREADS:
15159 case OMP_CLAUSE_NUM_TEAMS:
15160 case OMP_CLAUSE_THREAD_LIMIT:
15161 case OMP_CLAUSE_DEFAULT:
15162 case OMP_CLAUSE_UNTIED:
15163 case OMP_CLAUSE_COLLAPSE:
15164 case OMP_CLAUSE_FINAL:
15165 case OMP_CLAUSE_DEVICE:
15166 case OMP_CLAUSE_DIST_SCHEDULE:
15167 case OMP_CLAUSE_PARALLEL:
15168 case OMP_CLAUSE_FOR:
15169 case OMP_CLAUSE_SECTIONS:
15170 case OMP_CLAUSE_TASKGROUP:
15171 case OMP_CLAUSE_PROC_BIND:
15172 case OMP_CLAUSE_DEVICE_TYPE:
15173 case OMP_CLAUSE_PRIORITY:
15174 case OMP_CLAUSE_GRAINSIZE:
15175 case OMP_CLAUSE_NUM_TASKS:
15176 case OMP_CLAUSE_THREADS:
15177 case OMP_CLAUSE_SIMD:
15178 case OMP_CLAUSE_HINT:
15179 case OMP_CLAUSE_FILTER:
15180 case OMP_CLAUSE_DEFAULTMAP:
15181 case OMP_CLAUSE_BIND:
15182 case OMP_CLAUSE_NUM_GANGS:
15183 case OMP_CLAUSE_NUM_WORKERS:
15184 case OMP_CLAUSE_VECTOR_LENGTH:
15185 case OMP_CLAUSE_ASYNC:
15186 case OMP_CLAUSE_WAIT:
15187 case OMP_CLAUSE_AUTO:
15188 case OMP_CLAUSE_INDEPENDENT:
15189 case OMP_CLAUSE_SEQ:
15190 case OMP_CLAUSE_GANG:
15191 case OMP_CLAUSE_WORKER:
15192 case OMP_CLAUSE_VECTOR:
15193 case OMP_CLAUSE_TILE:
15194 case OMP_CLAUSE_IF_PRESENT:
15195 case OMP_CLAUSE_FINALIZE:
15196 case OMP_CLAUSE_NOHOST:
15197 pc = &OMP_CLAUSE_CHAIN (c);
15198 continue;
15200 case OMP_CLAUSE_MERGEABLE:
15201 mergeable_seen = true;
15202 pc = &OMP_CLAUSE_CHAIN (c);
15203 continue;
15205 case OMP_CLAUSE_NOGROUP:
15206 nogroup_seen = pc;
15207 pc = &OMP_CLAUSE_CHAIN (c);
15208 continue;
15210 case OMP_CLAUSE_SCHEDULE:
15211 schedule_clause = c;
15212 pc = &OMP_CLAUSE_CHAIN (c);
15213 continue;
15215 case OMP_CLAUSE_ORDERED:
15216 ordered_clause = c;
15217 if (order_clause)
15219 error_at (OMP_CLAUSE_LOCATION (*order_clause),
15220 "%<order%> clause must not be used together "
15221 "with %<ordered%>");
15222 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
15223 order_clause = NULL;
15225 pc = &OMP_CLAUSE_CHAIN (c);
15226 continue;
15228 case OMP_CLAUSE_SAFELEN:
15229 safelen = c;
15230 pc = &OMP_CLAUSE_CHAIN (c);
15231 continue;
15232 case OMP_CLAUSE_SIMDLEN:
15233 simdlen = c;
15234 pc = &OMP_CLAUSE_CHAIN (c);
15235 continue;
15237 case OMP_CLAUSE_INBRANCH:
15238 case OMP_CLAUSE_NOTINBRANCH:
15239 if (branch_seen)
15241 error_at (OMP_CLAUSE_LOCATION (c),
15242 "%<inbranch%> clause is incompatible with "
15243 "%<notinbranch%>");
15244 remove = true;
15245 break;
15247 branch_seen = true;
15248 pc = &OMP_CLAUSE_CHAIN (c);
15249 continue;
15251 case OMP_CLAUSE_INCLUSIVE:
15252 case OMP_CLAUSE_EXCLUSIVE:
15253 need_complete = true;
15254 need_implicitly_determined = true;
15255 t = OMP_CLAUSE_DECL (c);
15256 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15258 error_at (OMP_CLAUSE_LOCATION (c),
15259 "%qE is not a variable in clause %qs", t,
15260 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15261 remove = true;
15263 break;
15265 default:
15266 gcc_unreachable ();
15269 if (!remove)
15271 t = OMP_CLAUSE_DECL (c);
15273 if (need_complete)
15275 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15276 if (t == error_mark_node)
15277 remove = true;
15280 if (need_implicitly_determined)
15282 const char *share_name = NULL;
15284 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15285 share_name = "threadprivate";
15286 else switch (c_omp_predetermined_sharing (t))
15288 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15289 break;
15290 case OMP_CLAUSE_DEFAULT_SHARED:
15291 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15292 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15293 && c_omp_predefined_variable (t))
15294 /* The __func__ variable and similar function-local
15295 predefined variables may be listed in a shared or
15296 firstprivate clause. */
15297 break;
15298 share_name = "shared";
15299 break;
15300 case OMP_CLAUSE_DEFAULT_PRIVATE:
15301 share_name = "private";
15302 break;
15303 default:
15304 gcc_unreachable ();
15306 if (share_name)
15308 error_at (OMP_CLAUSE_LOCATION (c),
15309 "%qE is predetermined %qs for %qs",
15310 t, share_name,
15311 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15312 remove = true;
15314 else if (TREE_READONLY (t)
15315 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15316 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15318 error_at (OMP_CLAUSE_LOCATION (c),
15319 "%<const%> qualified %qE may appear only in "
15320 "%<shared%> or %<firstprivate%> clauses", t);
15321 remove = true;
15326 if (remove)
15327 *pc = OMP_CLAUSE_CHAIN (c);
15328 else
15329 pc = &OMP_CLAUSE_CHAIN (c);
15332 if (simdlen
15333 && safelen
15334 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
15335 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
15337 error_at (OMP_CLAUSE_LOCATION (simdlen),
15338 "%<simdlen%> clause value is bigger than "
15339 "%<safelen%> clause value");
15340 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
15341 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
15344 if (ordered_clause
15345 && schedule_clause
15346 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15347 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
15349 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15350 "%<nonmonotonic%> schedule modifier specified together "
15351 "with %<ordered%> clause");
15352 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15353 = (enum omp_clause_schedule_kind)
15354 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15355 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
15358 if (reduction_seen < 0 && ordered_clause)
15360 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
15361 "%qs clause specified together with %<inscan%> "
15362 "%<reduction%> clause", "ordered");
15363 reduction_seen = -2;
15366 if (reduction_seen < 0 && schedule_clause)
15368 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15369 "%qs clause specified together with %<inscan%> "
15370 "%<reduction%> clause", "schedule");
15371 reduction_seen = -2;
15374 if (linear_variable_step_check
15375 || reduction_seen == -2
15376 || allocate_seen
15377 || target_in_reduction_seen)
15378 for (pc = &clauses, c = clauses; c ; c = *pc)
15380 bool remove = false;
15381 if (allocate_seen)
15382 switch (OMP_CLAUSE_CODE (c))
15384 case OMP_CLAUSE_REDUCTION:
15385 case OMP_CLAUSE_IN_REDUCTION:
15386 case OMP_CLAUSE_TASK_REDUCTION:
15387 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
15389 t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
15390 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15391 t = TREE_OPERAND (t, 0);
15392 if (TREE_CODE (t) == ADDR_EXPR
15393 || TREE_CODE (t) == INDIRECT_REF)
15394 t = TREE_OPERAND (t, 0);
15395 if (DECL_P (t))
15396 bitmap_clear_bit (&aligned_head, DECL_UID (t));
15397 break;
15399 /* FALLTHRU */
15400 case OMP_CLAUSE_PRIVATE:
15401 case OMP_CLAUSE_FIRSTPRIVATE:
15402 case OMP_CLAUSE_LASTPRIVATE:
15403 case OMP_CLAUSE_LINEAR:
15404 if (DECL_P (OMP_CLAUSE_DECL (c)))
15405 bitmap_clear_bit (&aligned_head,
15406 DECL_UID (OMP_CLAUSE_DECL (c)));
15407 break;
15408 default:
15409 break;
15411 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15412 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
15413 && !bitmap_bit_p (&map_head,
15414 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
15416 error_at (OMP_CLAUSE_LOCATION (c),
15417 "%<linear%> clause step is a parameter %qD not "
15418 "specified in %<uniform%> clause",
15419 OMP_CLAUSE_LINEAR_STEP (c));
15420 remove = true;
15422 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15423 && reduction_seen == -2)
15424 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
15425 if (target_in_reduction_seen
15426 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
15428 tree t = OMP_CLAUSE_DECL (c);
15429 while (handled_component_p (t)
15430 || TREE_CODE (t) == INDIRECT_REF
15431 || TREE_CODE (t) == ADDR_EXPR
15432 || TREE_CODE (t) == MEM_REF
15433 || TREE_CODE (t) == NON_LVALUE_EXPR)
15434 t = TREE_OPERAND (t, 0);
15435 if (DECL_P (t)
15436 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15437 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
15440 if (remove)
15441 *pc = OMP_CLAUSE_CHAIN (c);
15442 else
15443 pc = &OMP_CLAUSE_CHAIN (c);
15446 if (allocate_seen)
15447 for (pc = &clauses, c = clauses; c ; c = *pc)
15449 bool remove = false;
15450 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
15451 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
15452 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
15454 error_at (OMP_CLAUSE_LOCATION (c),
15455 "%qD specified in %<allocate%> clause but not in "
15456 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
15457 remove = true;
15459 if (remove)
15460 *pc = OMP_CLAUSE_CHAIN (c);
15461 else
15462 pc = &OMP_CLAUSE_CHAIN (c);
15465 if (nogroup_seen && reduction_seen)
15467 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
15468 "%<nogroup%> clause must not be used together with "
15469 "%<reduction%> clause");
15470 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
15473 if (detach_seen)
15475 if (mergeable_seen)
15477 error_at (OMP_CLAUSE_LOCATION (*detach_seen),
15478 "%<detach%> clause must not be used together with "
15479 "%<mergeable%> clause");
15480 *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
15482 else
15484 tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
15486 for (pc = &clauses, c = clauses; c ; c = *pc)
15488 bool remove = false;
15489 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15490 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15491 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
15492 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
15493 && OMP_CLAUSE_DECL (c) == detach_decl)
15495 error_at (OMP_CLAUSE_LOCATION (c),
15496 "the event handle of a %<detach%> clause "
15497 "should not be in a data-sharing clause");
15498 remove = true;
15500 if (remove)
15501 *pc = OMP_CLAUSE_CHAIN (c);
15502 else
15503 pc = &OMP_CLAUSE_CHAIN (c);
15508 bitmap_obstack_release (NULL);
15509 return clauses;
15512 /* Return code to initialize DST with a copy constructor from SRC.
15513 C doesn't have copy constructors nor assignment operators, only for
15514 _Atomic vars we need to perform __atomic_load from src into a temporary
15515 followed by __atomic_store of the temporary to dst. */
15517 tree
15518 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
15520 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
15521 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
15523 location_t loc = OMP_CLAUSE_LOCATION (clause);
15524 tree type = TREE_TYPE (dst);
15525 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
15526 tree tmp = create_tmp_var (nonatomic_type);
15527 tree tmp_addr = build_fold_addr_expr (tmp);
15528 TREE_ADDRESSABLE (tmp) = 1;
15529 suppress_warning (tmp);
15530 tree src_addr = build_fold_addr_expr (src);
15531 tree dst_addr = build_fold_addr_expr (dst);
15532 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
15533 vec<tree, va_gc> *params;
15534 /* Expansion of a generic atomic load may require an addition
15535 element, so allocate enough to prevent a resize. */
15536 vec_alloc (params, 4);
15538 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
15539 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
15540 params->quick_push (src_addr);
15541 params->quick_push (tmp_addr);
15542 params->quick_push (seq_cst);
15543 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15545 vec_alloc (params, 4);
15547 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
15548 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
15549 params->quick_push (dst_addr);
15550 params->quick_push (tmp_addr);
15551 params->quick_push (seq_cst);
15552 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15553 return build2 (COMPOUND_EXPR, void_type_node, load, store);
15556 /* Create a transaction node. */
15558 tree
15559 c_finish_transaction (location_t loc, tree block, int flags)
15561 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
15562 if (flags & TM_STMT_ATTR_OUTER)
15563 TRANSACTION_EXPR_OUTER (stmt) = 1;
15564 if (flags & TM_STMT_ATTR_RELAXED)
15565 TRANSACTION_EXPR_RELAXED (stmt) = 1;
15566 return add_stmt (stmt);
15569 /* Make a variant type in the proper way for C/C++, propagating qualifiers
15570 down to the element type of an array. If ORIG_QUAL_TYPE is not
15571 NULL, then it should be used as the qualified type
15572 ORIG_QUAL_INDIRECT levels down in array type derivation (to
15573 preserve information about the typedef name from which an array
15574 type was derived). */
15576 tree
15577 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
15578 size_t orig_qual_indirect)
15580 if (type == error_mark_node)
15581 return type;
15583 if (TREE_CODE (type) == ARRAY_TYPE)
15585 tree t;
15586 tree element_type = c_build_qualified_type (TREE_TYPE (type),
15587 type_quals, orig_qual_type,
15588 orig_qual_indirect - 1);
15590 /* See if we already have an identically qualified type. */
15591 if (orig_qual_type && orig_qual_indirect == 0)
15592 t = orig_qual_type;
15593 else
15594 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
15596 if (TYPE_QUALS (strip_array_types (t)) == type_quals
15597 && TYPE_NAME (t) == TYPE_NAME (type)
15598 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
15599 && attribute_list_equal (TYPE_ATTRIBUTES (t),
15600 TYPE_ATTRIBUTES (type)))
15601 break;
15603 if (!t)
15605 tree domain = TYPE_DOMAIN (type);
15607 t = build_variant_type_copy (type);
15608 TREE_TYPE (t) = element_type;
15610 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
15611 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
15612 SET_TYPE_STRUCTURAL_EQUALITY (t);
15613 else if (TYPE_CANONICAL (element_type) != element_type
15614 || (domain && TYPE_CANONICAL (domain) != domain))
15616 tree unqualified_canon
15617 = build_array_type (TYPE_CANONICAL (element_type),
15618 domain? TYPE_CANONICAL (domain)
15619 : NULL_TREE);
15620 if (TYPE_REVERSE_STORAGE_ORDER (type))
15622 unqualified_canon
15623 = build_distinct_type_copy (unqualified_canon);
15624 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
15626 TYPE_CANONICAL (t)
15627 = c_build_qualified_type (unqualified_canon, type_quals);
15629 else
15630 TYPE_CANONICAL (t) = t;
15632 return t;
15635 /* A restrict-qualified pointer type must be a pointer to object or
15636 incomplete type. Note that the use of POINTER_TYPE_P also allows
15637 REFERENCE_TYPEs, which is appropriate for C++. */
15638 if ((type_quals & TYPE_QUAL_RESTRICT)
15639 && (!POINTER_TYPE_P (type)
15640 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
15642 error ("invalid use of %<restrict%>");
15643 type_quals &= ~TYPE_QUAL_RESTRICT;
15646 tree var_type = (orig_qual_type && orig_qual_indirect == 0
15647 ? orig_qual_type
15648 : build_qualified_type (type, type_quals));
15649 /* A variant type does not inherit the list of incomplete vars from the
15650 type main variant. */
15651 if ((RECORD_OR_UNION_TYPE_P (var_type)
15652 || TREE_CODE (var_type) == ENUMERAL_TYPE)
15653 && TYPE_MAIN_VARIANT (var_type) != var_type)
15654 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
15655 return var_type;
15658 /* Build a VA_ARG_EXPR for the C parser. */
15660 tree
15661 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
15663 if (error_operand_p (type))
15664 return error_mark_node;
15665 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15666 order because it takes the address of the expression. */
15667 else if (handled_component_p (expr)
15668 && reverse_storage_order_for_component_p (expr))
15670 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
15671 return error_mark_node;
15673 else if (!COMPLETE_TYPE_P (type))
15675 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
15676 "type %qT", type);
15677 return error_mark_node;
15679 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
15680 warning_at (loc2, OPT_Wc___compat,
15681 "C++ requires promoted type, not enum type, in %<va_arg%>");
15682 return build_va_arg (loc2, expr, type);
15685 /* Return truthvalue of whether T1 is the same tree structure as T2.
15686 Return 1 if they are the same. Return false if they are different. */
15688 bool
15689 c_tree_equal (tree t1, tree t2)
15691 enum tree_code code1, code2;
15693 if (t1 == t2)
15694 return true;
15695 if (!t1 || !t2)
15696 return false;
15698 for (code1 = TREE_CODE (t1);
15699 CONVERT_EXPR_CODE_P (code1)
15700 || code1 == NON_LVALUE_EXPR;
15701 code1 = TREE_CODE (t1))
15702 t1 = TREE_OPERAND (t1, 0);
15703 for (code2 = TREE_CODE (t2);
15704 CONVERT_EXPR_CODE_P (code2)
15705 || code2 == NON_LVALUE_EXPR;
15706 code2 = TREE_CODE (t2))
15707 t2 = TREE_OPERAND (t2, 0);
15709 /* They might have become equal now. */
15710 if (t1 == t2)
15711 return true;
15713 if (code1 != code2)
15714 return false;
15716 switch (code1)
15718 case INTEGER_CST:
15719 return wi::to_wide (t1) == wi::to_wide (t2);
15721 case REAL_CST:
15722 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
15724 case STRING_CST:
15725 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
15726 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
15727 TREE_STRING_LENGTH (t1));
15729 case FIXED_CST:
15730 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
15731 TREE_FIXED_CST (t2));
15733 case COMPLEX_CST:
15734 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
15735 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
15737 case VECTOR_CST:
15738 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
15740 case CONSTRUCTOR:
15741 /* We need to do this when determining whether or not two
15742 non-type pointer to member function template arguments
15743 are the same. */
15744 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
15745 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
15746 return false;
15748 tree field, value;
15749 unsigned int i;
15750 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
15752 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
15753 if (!c_tree_equal (field, elt2->index)
15754 || !c_tree_equal (value, elt2->value))
15755 return false;
15758 return true;
15760 case TREE_LIST:
15761 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
15762 return false;
15763 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
15764 return false;
15765 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
15767 case SAVE_EXPR:
15768 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15770 case CALL_EXPR:
15772 tree arg1, arg2;
15773 call_expr_arg_iterator iter1, iter2;
15774 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
15775 return false;
15776 for (arg1 = first_call_expr_arg (t1, &iter1),
15777 arg2 = first_call_expr_arg (t2, &iter2);
15778 arg1 && arg2;
15779 arg1 = next_call_expr_arg (&iter1),
15780 arg2 = next_call_expr_arg (&iter2))
15781 if (!c_tree_equal (arg1, arg2))
15782 return false;
15783 if (arg1 || arg2)
15784 return false;
15785 return true;
15788 case TARGET_EXPR:
15790 tree o1 = TREE_OPERAND (t1, 0);
15791 tree o2 = TREE_OPERAND (t2, 0);
15793 /* Special case: if either target is an unallocated VAR_DECL,
15794 it means that it's going to be unified with whatever the
15795 TARGET_EXPR is really supposed to initialize, so treat it
15796 as being equivalent to anything. */
15797 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
15798 && !DECL_RTL_SET_P (o1))
15799 /*Nop*/;
15800 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
15801 && !DECL_RTL_SET_P (o2))
15802 /*Nop*/;
15803 else if (!c_tree_equal (o1, o2))
15804 return false;
15806 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
15809 case COMPONENT_REF:
15810 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
15811 return false;
15812 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15814 case PARM_DECL:
15815 case VAR_DECL:
15816 case CONST_DECL:
15817 case FIELD_DECL:
15818 case FUNCTION_DECL:
15819 case IDENTIFIER_NODE:
15820 case SSA_NAME:
15821 return false;
15823 case TREE_VEC:
15825 unsigned ix;
15826 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
15827 return false;
15828 for (ix = TREE_VEC_LENGTH (t1); ix--;)
15829 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
15830 TREE_VEC_ELT (t2, ix)))
15831 return false;
15832 return true;
15835 default:
15836 break;
15839 switch (TREE_CODE_CLASS (code1))
15841 case tcc_unary:
15842 case tcc_binary:
15843 case tcc_comparison:
15844 case tcc_expression:
15845 case tcc_vl_exp:
15846 case tcc_reference:
15847 case tcc_statement:
15849 int i, n = TREE_OPERAND_LENGTH (t1);
15851 switch (code1)
15853 case PREINCREMENT_EXPR:
15854 case PREDECREMENT_EXPR:
15855 case POSTINCREMENT_EXPR:
15856 case POSTDECREMENT_EXPR:
15857 n = 1;
15858 break;
15859 case ARRAY_REF:
15860 n = 2;
15861 break;
15862 default:
15863 break;
15866 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
15867 && n != TREE_OPERAND_LENGTH (t2))
15868 return false;
15870 for (i = 0; i < n; ++i)
15871 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
15872 return false;
15874 return true;
15877 case tcc_type:
15878 return comptypes (t1, t2);
15879 default:
15880 gcc_unreachable ();
15882 /* We can get here with --disable-checking. */
15883 return false;
15886 /* Returns true when the function declaration FNDECL is implicit,
15887 introduced as a result of a call to an otherwise undeclared
15888 function, and false otherwise. */
15890 bool
15891 c_decl_implicit (const_tree fndecl)
15893 return C_DECL_IMPLICIT (fndecl);