compiler: add missing Slice_info_expression::do_traverse
[official-gcc.git] / gcc / c / c-typeck.cc
blob45bacc06c474f75144386c387cbd59d70fbaf37d
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2023 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"
55 #include "realmpfr.h"
57 /* Possible cases of implicit conversions. Used to select diagnostic messages
58 and control folding initializers in convert_for_assignment. */
59 enum impl_conv {
60 ic_argpass,
61 ic_assign,
62 ic_init,
63 ic_init_const,
64 ic_return
67 /* The level of nesting inside "__alignof__". */
68 int in_alignof;
70 /* The level of nesting inside "sizeof". */
71 int in_sizeof;
73 /* The level of nesting inside "typeof". */
74 int in_typeof;
76 /* True when parsing OpenMP loop expressions. */
77 bool c_in_omp_for;
79 /* The argument of last parsed sizeof expression, only to be tested
80 if expr.original_code == SIZEOF_EXPR. */
81 tree c_last_sizeof_arg;
82 location_t c_last_sizeof_loc;
84 /* Nonzero if we might need to print a "missing braces around
85 initializer" message within this initializer. */
86 static int found_missing_braces;
88 static bool require_constant_value;
89 static bool require_constant_elements;
90 static bool require_constexpr_value;
92 static tree qualify_type (tree, tree);
93 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
94 bool *);
95 static int comp_target_types (location_t, tree, tree);
96 static int function_types_compatible_p (const_tree, const_tree, bool *,
97 bool *);
98 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
99 static tree lookup_field (tree, tree);
100 static int convert_arguments (location_t, vec<location_t>, tree,
101 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
102 tree);
103 static tree pointer_diff (location_t, tree, tree, tree *);
104 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
105 enum impl_conv, bool, tree, tree, int,
106 int = 0);
107 static tree valid_compound_expr_initializer (tree, tree);
108 static void push_string (const char *);
109 static void push_member_name (tree);
110 static int spelling_length (void);
111 static char *print_spelling (char *);
112 static void warning_init (location_t, int, const char *);
113 static tree digest_init (location_t, tree, tree, tree, bool, bool, bool, bool,
114 bool, bool);
115 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
116 bool, struct obstack *);
117 static void output_pending_init_elements (int, struct obstack *);
118 static bool set_designator (location_t, bool, struct obstack *);
119 static void push_range_stack (tree, struct obstack *);
120 static void add_pending_init (location_t, tree, tree, tree, bool,
121 struct obstack *);
122 static void set_nonincremental_init (struct obstack *);
123 static void set_nonincremental_init_from_string (tree, struct obstack *);
124 static tree find_init_member (tree, struct obstack *);
125 static void readonly_warning (tree, enum lvalue_use);
126 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
127 static void record_maybe_used_decl (tree);
128 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
130 /* Return true if EXP is a null pointer constant, false otherwise. */
132 bool
133 null_pointer_constant_p (const_tree expr)
135 /* This should really operate on c_expr structures, but they aren't
136 yet available everywhere required. */
137 tree type = TREE_TYPE (expr);
139 /* An integer constant expression with the value 0, such an expression
140 cast to type void*, or the predefined constant nullptr, are a null
141 pointer constant. */
142 if (expr == nullptr_node)
143 return true;
145 return (TREE_CODE (expr) == INTEGER_CST
146 && !TREE_OVERFLOW (expr)
147 && integer_zerop (expr)
148 && (INTEGRAL_TYPE_P (type)
149 || (TREE_CODE (type) == POINTER_TYPE
150 && VOID_TYPE_P (TREE_TYPE (type))
151 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
154 /* EXPR may appear in an unevaluated part of an integer constant
155 expression, but not in an evaluated part. Wrap it in a
156 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
157 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
159 static tree
160 note_integer_operands (tree expr)
162 tree ret;
163 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
165 ret = copy_node (expr);
166 TREE_OVERFLOW (ret) = 1;
168 else
170 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
171 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
173 return ret;
176 /* Having checked whether EXPR may appear in an unevaluated part of an
177 integer constant expression and found that it may, remove any
178 C_MAYBE_CONST_EXPR noting this fact and return the resulting
179 expression. */
181 static inline tree
182 remove_c_maybe_const_expr (tree expr)
184 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
185 return C_MAYBE_CONST_EXPR_EXPR (expr);
186 else
187 return expr;
190 \f/* This is a cache to hold if two types are compatible or not. */
192 struct tagged_tu_seen_cache {
193 const struct tagged_tu_seen_cache * next;
194 const_tree t1;
195 const_tree t2;
196 /* The return value of tagged_types_tu_compatible_p if we had seen
197 these two types already. */
198 int val;
201 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
202 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
204 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
205 does not have an incomplete type. (That includes void types.)
206 LOC is the location of the use. */
208 tree
209 require_complete_type (location_t loc, tree value)
211 tree type = TREE_TYPE (value);
213 if (error_operand_p (value))
214 return error_mark_node;
216 /* First, detect a valid value with a complete type. */
217 if (COMPLETE_TYPE_P (type))
218 return value;
220 c_incomplete_type_error (loc, value, type);
221 return error_mark_node;
224 /* Print an error message for invalid use of an incomplete type.
225 VALUE is the expression that was used (or 0 if that isn't known)
226 and TYPE is the type that was invalid. LOC is the location for
227 the error. */
229 void
230 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
232 /* Avoid duplicate error message. */
233 if (TREE_CODE (type) == ERROR_MARK)
234 return;
236 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
237 error_at (loc, "%qD has an incomplete type %qT", value, type);
238 else
240 retry:
241 /* We must print an error message. Be clever about what it says. */
243 switch (TREE_CODE (type))
245 case RECORD_TYPE:
246 case UNION_TYPE:
247 case ENUMERAL_TYPE:
248 break;
250 case VOID_TYPE:
251 error_at (loc, "invalid use of void expression");
252 return;
254 case ARRAY_TYPE:
255 if (TYPE_DOMAIN (type))
257 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
259 error_at (loc, "invalid use of flexible array member");
260 return;
262 type = TREE_TYPE (type);
263 goto retry;
265 error_at (loc, "invalid use of array with unspecified bounds");
266 return;
268 default:
269 gcc_unreachable ();
272 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
273 error_at (loc, "invalid use of undefined type %qT", type);
274 else
275 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
276 error_at (loc, "invalid use of incomplete typedef %qT", type);
280 /* Given a type, apply default promotions wrt unnamed function
281 arguments and return the new type. */
283 tree
284 c_type_promotes_to (tree type)
286 tree ret = NULL_TREE;
288 if (TYPE_MAIN_VARIANT (type) == float_type_node)
289 ret = double_type_node;
290 else if (c_promoting_integer_type_p (type))
292 /* Preserve unsignedness if not really getting any wider. */
293 if (TYPE_UNSIGNED (type)
294 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
295 ret = unsigned_type_node;
296 else
297 ret = integer_type_node;
300 if (ret != NULL_TREE)
301 return (TYPE_ATOMIC (type)
302 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
303 : ret);
305 return type;
308 /* Return true if between two named address spaces, whether there is a superset
309 named address space that encompasses both address spaces. If there is a
310 superset, return which address space is the superset. */
312 static bool
313 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
315 if (as1 == as2)
317 *common = as1;
318 return true;
320 else if (targetm.addr_space.subset_p (as1, as2))
322 *common = as2;
323 return true;
325 else if (targetm.addr_space.subset_p (as2, as1))
327 *common = as1;
328 return true;
330 else
331 return false;
334 /* Return a variant of TYPE which has all the type qualifiers of LIKE
335 as well as those of TYPE. */
337 static tree
338 qualify_type (tree type, tree like)
340 addr_space_t as_type = TYPE_ADDR_SPACE (type);
341 addr_space_t as_like = TYPE_ADDR_SPACE (like);
342 addr_space_t as_common;
344 /* If the two named address spaces are different, determine the common
345 superset address space. If there isn't one, raise an error. */
346 if (!addr_space_superset (as_type, as_like, &as_common))
348 as_common = as_type;
349 error ("%qT and %qT are in disjoint named address spaces",
350 type, like);
353 return c_build_qualified_type (type,
354 TYPE_QUALS_NO_ADDR_SPACE (type)
355 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
356 | ENCODE_QUAL_ADDR_SPACE (as_common));
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
416 && (code2 == INTEGER_TYPE || code2 == BOOLEAN_TYPE))
417 return t1;
418 if (code2 == ENUMERAL_TYPE
419 && (code1 == INTEGER_TYPE || code1 == BOOLEAN_TYPE))
420 return t2;
422 gcc_assert (code1 == code2);
424 switch (code1)
426 case POINTER_TYPE:
427 /* For two pointers, do this recursively on the target type. */
429 tree pointed_to_1 = TREE_TYPE (t1);
430 tree pointed_to_2 = TREE_TYPE (t2);
431 tree target = composite_type (pointed_to_1, pointed_to_2);
432 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
433 t1 = build_type_attribute_variant (t1, attributes);
434 return qualify_type (t1, t2);
437 case ARRAY_TYPE:
439 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
440 int quals;
441 tree unqual_elt;
442 tree d1 = TYPE_DOMAIN (t1);
443 tree d2 = TYPE_DOMAIN (t2);
444 bool d1_variable, d2_variable;
445 bool d1_zero, d2_zero;
446 bool t1_complete, t2_complete;
448 /* We should not have any type quals on arrays at all. */
449 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
450 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
452 t1_complete = COMPLETE_TYPE_P (t1);
453 t2_complete = COMPLETE_TYPE_P (t2);
455 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
456 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
458 d1_variable = (!d1_zero
459 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
460 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
461 d2_variable = (!d2_zero
462 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
463 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
464 d1_variable = d1_variable || (d1_zero && C_TYPE_VARIABLE_SIZE (t1));
465 d2_variable = d2_variable || (d2_zero && C_TYPE_VARIABLE_SIZE (t2));
467 /* Save space: see if the result is identical to one of the args. */
468 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
469 && (d2_variable || d2_zero || !d1_variable))
470 return build_type_attribute_variant (t1, attributes);
471 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
472 && (d1_variable || d1_zero || !d2_variable))
473 return build_type_attribute_variant (t2, attributes);
475 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
476 return build_type_attribute_variant (t1, attributes);
477 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
478 return build_type_attribute_variant (t2, attributes);
480 /* Merge the element types, and have a size if either arg has
481 one. We may have qualifiers on the element types. To set
482 up TYPE_MAIN_VARIANT correctly, we need to form the
483 composite of the unqualified types and add the qualifiers
484 back at the end. */
485 quals = TYPE_QUALS (strip_array_types (elt));
486 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
487 t1 = build_array_type (unqual_elt,
488 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
489 && (d2_variable
490 || d2_zero
491 || !d1_variable))
492 ? t1
493 : t2));
494 /* Ensure a composite type involving a zero-length array type
495 is a zero-length type not an incomplete type. */
496 if (d1_zero && d2_zero
497 && (t1_complete || t2_complete)
498 && !COMPLETE_TYPE_P (t1))
500 TYPE_SIZE (t1) = bitsize_zero_node;
501 TYPE_SIZE_UNIT (t1) = size_zero_node;
503 t1 = c_build_qualified_type (t1, quals);
504 return build_type_attribute_variant (t1, attributes);
507 case ENUMERAL_TYPE:
508 case RECORD_TYPE:
509 case UNION_TYPE:
510 if (attributes != NULL)
512 /* Try harder not to create a new aggregate type. */
513 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
514 return t1;
515 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
516 return t2;
518 return build_type_attribute_variant (t1, attributes);
520 case FUNCTION_TYPE:
521 /* Function types: prefer the one that specified arg types.
522 If both do, merge the arg types. Also merge the return types. */
524 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
525 tree p1 = TYPE_ARG_TYPES (t1);
526 tree p2 = TYPE_ARG_TYPES (t2);
527 int len;
528 tree newargs, n;
529 int i;
531 /* Save space: see if the result is identical to one of the args. */
532 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
533 return build_functype_attribute_variant (t1, t2, attributes);
534 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
535 return build_functype_attribute_variant (t2, t1, attributes);
537 /* Simple way if one arg fails to specify argument types. */
538 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
540 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2),
541 TYPE_NO_NAMED_ARGS_STDARG_P (t2));
542 t1 = build_type_attribute_variant (t1, attributes);
543 return qualify_type (t1, t2);
545 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
547 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1),
548 TYPE_NO_NAMED_ARGS_STDARG_P (t1));
549 t1 = build_type_attribute_variant (t1, attributes);
550 return qualify_type (t1, t2);
553 /* If both args specify argument types, we must merge the two
554 lists, argument by argument. */
556 for (len = 0, newargs = p1;
557 newargs && newargs != void_list_node;
558 len++, newargs = TREE_CHAIN (newargs))
561 for (i = 0; i < len; i++)
562 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
564 n = newargs;
566 for (; p1 && p1 != void_list_node;
567 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
569 /* A null type means arg type is not specified.
570 Take whatever the other function type has. */
571 if (TREE_VALUE (p1) == NULL_TREE)
573 TREE_VALUE (n) = TREE_VALUE (p2);
574 goto parm_done;
576 if (TREE_VALUE (p2) == NULL_TREE)
578 TREE_VALUE (n) = TREE_VALUE (p1);
579 goto parm_done;
582 /* Given wait (union {union wait *u; int *i} *)
583 and wait (union wait *),
584 prefer union wait * as type of parm. */
585 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
586 && TREE_VALUE (p1) != TREE_VALUE (p2))
588 tree memb;
589 tree mv2 = TREE_VALUE (p2);
590 if (mv2 && mv2 != error_mark_node
591 && TREE_CODE (mv2) != ARRAY_TYPE)
592 mv2 = TYPE_MAIN_VARIANT (mv2);
593 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
594 memb; memb = DECL_CHAIN (memb))
596 tree mv3 = TREE_TYPE (memb);
597 if (mv3 && mv3 != error_mark_node
598 && TREE_CODE (mv3) != ARRAY_TYPE)
599 mv3 = TYPE_MAIN_VARIANT (mv3);
600 if (comptypes (mv3, mv2))
602 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
603 TREE_VALUE (p2));
604 pedwarn (input_location, OPT_Wpedantic,
605 "function types not truly compatible in ISO C");
606 goto parm_done;
610 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
611 && TREE_VALUE (p2) != TREE_VALUE (p1))
613 tree memb;
614 tree mv1 = TREE_VALUE (p1);
615 if (mv1 && mv1 != error_mark_node
616 && TREE_CODE (mv1) != ARRAY_TYPE)
617 mv1 = TYPE_MAIN_VARIANT (mv1);
618 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
619 memb; memb = DECL_CHAIN (memb))
621 tree mv3 = TREE_TYPE (memb);
622 if (mv3 && mv3 != error_mark_node
623 && TREE_CODE (mv3) != ARRAY_TYPE)
624 mv3 = TYPE_MAIN_VARIANT (mv3);
625 if (comptypes (mv3, mv1))
627 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
628 TREE_VALUE (p1));
629 pedwarn (input_location, OPT_Wpedantic,
630 "function types not truly compatible in ISO C");
631 goto parm_done;
635 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
636 parm_done: ;
639 t1 = build_function_type (valtype, newargs);
640 t1 = qualify_type (t1, t2);
642 /* FALLTHRU */
644 default:
645 return build_type_attribute_variant (t1, attributes);
650 /* Return the type of a conditional expression between pointers to
651 possibly differently qualified versions of compatible types.
653 We assume that comp_target_types has already been done and returned
654 nonzero; if that isn't so, this may crash. */
656 static tree
657 common_pointer_type (tree t1, tree t2)
659 tree attributes;
660 tree pointed_to_1, mv1;
661 tree pointed_to_2, mv2;
662 tree target;
663 unsigned target_quals;
664 addr_space_t as1, as2, as_common;
665 int quals1, quals2;
667 /* Save time if the two types are the same. */
669 if (t1 == t2) return t1;
671 /* If one type is nonsense, use the other. */
672 if (t1 == error_mark_node)
673 return t2;
674 if (t2 == error_mark_node)
675 return t1;
677 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
678 && TREE_CODE (t2) == POINTER_TYPE);
680 /* Merge the attributes. */
681 attributes = targetm.merge_type_attributes (t1, t2);
683 /* Find the composite type of the target types, and combine the
684 qualifiers of the two types' targets. Do not lose qualifiers on
685 array element types by taking the TYPE_MAIN_VARIANT. */
686 mv1 = pointed_to_1 = TREE_TYPE (t1);
687 mv2 = pointed_to_2 = TREE_TYPE (t2);
688 if (TREE_CODE (mv1) != ARRAY_TYPE)
689 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
690 if (TREE_CODE (mv2) != ARRAY_TYPE)
691 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
692 target = composite_type (mv1, mv2);
694 /* Strip array types to get correct qualifier for pointers to arrays */
695 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
696 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
698 /* For function types do not merge const qualifiers, but drop them
699 if used inconsistently. The middle-end uses these to mark const
700 and noreturn functions. */
701 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
702 target_quals = (quals1 & quals2);
703 else
704 target_quals = (quals1 | quals2);
706 /* If the two named address spaces are different, determine the common
707 superset address space. This is guaranteed to exist due to the
708 assumption that comp_target_type returned non-zero. */
709 as1 = TYPE_ADDR_SPACE (pointed_to_1);
710 as2 = TYPE_ADDR_SPACE (pointed_to_2);
711 if (!addr_space_superset (as1, as2, &as_common))
712 gcc_unreachable ();
714 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
716 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
717 return build_type_attribute_variant (t1, attributes);
720 /* Return the common type for two arithmetic types under the usual
721 arithmetic conversions. The default conversions have already been
722 applied, and enumerated types converted to their compatible integer
723 types. The resulting type is unqualified and has no attributes.
725 This is the type for the result of most arithmetic operations
726 if the operands have the given two types. */
728 static tree
729 c_common_type (tree t1, tree t2)
731 enum tree_code code1;
732 enum tree_code code2;
734 /* If one type is nonsense, use the other. */
735 if (t1 == error_mark_node)
736 return t2;
737 if (t2 == error_mark_node)
738 return t1;
740 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
741 t1 = TYPE_MAIN_VARIANT (t1);
743 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
744 t2 = TYPE_MAIN_VARIANT (t2);
746 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
748 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
749 t1 = build_type_attribute_variant (t1, attrs);
752 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
754 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
755 t2 = build_type_attribute_variant (t2, attrs);
758 /* Save time if the two types are the same. */
760 if (t1 == t2) return t1;
762 code1 = TREE_CODE (t1);
763 code2 = TREE_CODE (t2);
765 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
766 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
767 || code1 == INTEGER_TYPE);
768 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
769 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
770 || code2 == INTEGER_TYPE);
772 /* When one operand is a decimal float type, the other operand cannot be
773 a generic float type or a complex type. We also disallow vector types
774 here. */
775 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
776 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
778 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
780 error ("cannot mix operands of decimal floating and vector types");
781 return error_mark_node;
783 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
785 error ("cannot mix operands of decimal floating and complex types");
786 return error_mark_node;
788 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
790 error ("cannot mix operands of decimal floating "
791 "and other floating types");
792 return error_mark_node;
796 /* If one type is a vector type, return that type. (How the usual
797 arithmetic conversions apply to the vector types extension is not
798 precisely specified.) */
799 if (code1 == VECTOR_TYPE)
800 return t1;
802 if (code2 == VECTOR_TYPE)
803 return t2;
805 /* If one type is complex, form the common type of the non-complex
806 components, then make that complex. Use T1 or T2 if it is the
807 required type. */
808 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
810 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
811 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
812 tree subtype = c_common_type (subtype1, subtype2);
814 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
815 return t1;
816 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
817 return t2;
818 else
819 return build_complex_type (subtype);
822 /* If only one is real, use it as the result. */
824 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
825 return t1;
827 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
828 return t2;
830 /* If both are real and either are decimal floating point types, use
831 the decimal floating point type with the greater precision. */
833 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
835 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
836 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
837 return dfloat128_type_node;
838 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
839 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
840 return dfloat64_type_node;
841 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
842 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
843 return dfloat32_type_node;
846 /* Deal with fixed-point types. */
847 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
849 unsigned int unsignedp = 0, satp = 0;
850 scalar_mode m1, m2;
851 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
853 m1 = SCALAR_TYPE_MODE (t1);
854 m2 = SCALAR_TYPE_MODE (t2);
856 /* If one input type is saturating, the result type is saturating. */
857 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
858 satp = 1;
860 /* If both fixed-point types are unsigned, the result type is unsigned.
861 When mixing fixed-point and integer types, follow the sign of the
862 fixed-point type.
863 Otherwise, the result type is signed. */
864 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
865 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
866 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
867 && TYPE_UNSIGNED (t1))
868 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
869 && TYPE_UNSIGNED (t2)))
870 unsignedp = 1;
872 /* The result type is signed. */
873 if (unsignedp == 0)
875 /* If the input type is unsigned, we need to convert to the
876 signed type. */
877 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
879 enum mode_class mclass = (enum mode_class) 0;
880 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
881 mclass = MODE_FRACT;
882 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
883 mclass = MODE_ACCUM;
884 else
885 gcc_unreachable ();
886 m1 = as_a <scalar_mode>
887 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
889 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
891 enum mode_class mclass = (enum mode_class) 0;
892 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
893 mclass = MODE_FRACT;
894 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
895 mclass = MODE_ACCUM;
896 else
897 gcc_unreachable ();
898 m2 = as_a <scalar_mode>
899 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
903 if (code1 == FIXED_POINT_TYPE)
905 fbit1 = GET_MODE_FBIT (m1);
906 ibit1 = GET_MODE_IBIT (m1);
908 else
910 fbit1 = 0;
911 /* Signed integers need to subtract one sign bit. */
912 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
915 if (code2 == FIXED_POINT_TYPE)
917 fbit2 = GET_MODE_FBIT (m2);
918 ibit2 = GET_MODE_IBIT (m2);
920 else
922 fbit2 = 0;
923 /* Signed integers need to subtract one sign bit. */
924 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
927 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
928 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
929 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
930 satp);
933 /* Both real or both integers; use the one with greater precision. */
935 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
936 return t1;
937 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
938 return t2;
940 /* Same precision. Prefer long longs to longs to ints when the
941 same precision, following the C99 rules on integer type rank
942 (which are equivalent to the C90 rules for C90 types). */
944 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
945 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
946 return long_long_unsigned_type_node;
948 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
949 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
951 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
952 return long_long_unsigned_type_node;
953 else
954 return long_long_integer_type_node;
957 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
958 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
959 return long_unsigned_type_node;
961 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
962 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
964 /* But preserve unsignedness from the other type,
965 since long cannot hold all the values of an unsigned int. */
966 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
967 return long_unsigned_type_node;
968 else
969 return long_integer_type_node;
972 /* For floating types of the same TYPE_PRECISION (which we here
973 assume means either the same set of values, or sets of values
974 neither a subset of the other, with behavior being undefined in
975 the latter case), follow the rules from TS 18661-3: prefer
976 interchange types _FloatN, then standard types long double,
977 double, float, then extended types _FloatNx. For extended types,
978 check them starting with _Float128x as that seems most consistent
979 in spirit with preferring long double to double; for interchange
980 types, also check in that order for consistency although it's not
981 possible for more than one of them to have the same
982 precision. */
983 tree mv1 = TYPE_MAIN_VARIANT (t1);
984 tree mv2 = TYPE_MAIN_VARIANT (t2);
986 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
987 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
988 return FLOATN_TYPE_NODE (i);
990 /* Likewise, prefer long double to double even if same size. */
991 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
992 return long_double_type_node;
994 /* Likewise, prefer double to float even if same size.
995 We got a couple of embedded targets with 32 bit doubles, and the
996 pdp11 might have 64 bit floats. */
997 if (mv1 == double_type_node || mv2 == double_type_node)
998 return double_type_node;
1000 if (mv1 == float_type_node || mv2 == float_type_node)
1001 return float_type_node;
1003 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1004 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1005 return FLOATNX_TYPE_NODE (i);
1007 /* Otherwise prefer the unsigned one. */
1009 if (TYPE_UNSIGNED (t1))
1010 return t1;
1011 else
1012 return t2;
1015 /* Wrapper around c_common_type that is used by c-common.cc and other
1016 front end optimizations that remove promotions. ENUMERAL_TYPEs
1017 are allowed here and are converted to their compatible integer types.
1018 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1019 preferably a non-Boolean type as the common type. */
1020 tree
1021 common_type (tree t1, tree t2)
1023 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1024 t1 = ENUM_UNDERLYING_TYPE (t1);
1025 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1026 t2 = ENUM_UNDERLYING_TYPE (t2);
1028 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1029 if (TREE_CODE (t1) == BOOLEAN_TYPE
1030 && TREE_CODE (t2) == BOOLEAN_TYPE)
1031 return boolean_type_node;
1033 /* If either type is BOOLEAN_TYPE, then return the other. */
1034 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1035 return t2;
1036 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1037 return t1;
1039 return c_common_type (t1, t2);
1042 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1043 or various other operations. Return 2 if they are compatible
1044 but a warning may be needed if you use them together. */
1047 comptypes (tree type1, tree type2)
1049 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1050 int val;
1052 val = comptypes_internal (type1, type2, NULL, NULL);
1053 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1055 return val;
1058 /* Like comptypes, but if it returns non-zero because enum and int are
1059 compatible, it sets *ENUM_AND_INT_P to true. */
1062 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1064 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1065 int val;
1067 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1068 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1070 return val;
1073 /* Like comptypes, but if it returns nonzero for different types, it
1074 sets *DIFFERENT_TYPES_P to true. */
1077 comptypes_check_different_types (tree type1, tree type2,
1078 bool *different_types_p)
1080 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1081 int val;
1083 val = comptypes_internal (type1, type2, NULL, different_types_p);
1084 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1086 return val;
1089 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1090 or various other operations. Return 2 if they are compatible
1091 but a warning may be needed if you use them together. If
1092 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1093 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1094 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1095 NULL, and the types are compatible but different enough not to be
1096 permitted in C11 typedef redeclarations, then this sets
1097 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1098 false, but may or may not be set if the types are incompatible.
1099 This differs from comptypes, in that we don't free the seen
1100 types. */
1102 static int
1103 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1104 bool *different_types_p)
1106 const_tree t1 = type1;
1107 const_tree t2 = type2;
1108 int attrval, val;
1110 /* Suppress errors caused by previously reported errors. */
1112 if (t1 == t2 || !t1 || !t2
1113 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1114 return 1;
1116 /* Enumerated types are compatible with integer types, but this is
1117 not transitive: two enumerated types in the same translation unit
1118 are compatible with each other only if they are the same type. */
1120 if (TREE_CODE (t1) == ENUMERAL_TYPE
1121 && COMPLETE_TYPE_P (t1)
1122 && TREE_CODE (t2) != ENUMERAL_TYPE)
1124 t1 = ENUM_UNDERLYING_TYPE (t1);
1125 if (TREE_CODE (t2) != VOID_TYPE)
1127 if (enum_and_int_p != NULL)
1128 *enum_and_int_p = true;
1129 if (different_types_p != NULL)
1130 *different_types_p = true;
1133 else if (TREE_CODE (t2) == ENUMERAL_TYPE
1134 && COMPLETE_TYPE_P (t2)
1135 && TREE_CODE (t1) != ENUMERAL_TYPE)
1137 t2 = ENUM_UNDERLYING_TYPE (t2);
1138 if (TREE_CODE (t1) != VOID_TYPE)
1140 if (enum_and_int_p != NULL)
1141 *enum_and_int_p = true;
1142 if (different_types_p != NULL)
1143 *different_types_p = true;
1147 if (t1 == t2)
1148 return 1;
1150 /* Different classes of types can't be compatible. */
1152 if (TREE_CODE (t1) != TREE_CODE (t2))
1153 return 0;
1155 /* Qualifiers must match. C99 6.7.3p9 */
1157 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1158 return 0;
1160 /* Allow for two different type nodes which have essentially the same
1161 definition. Note that we already checked for equality of the type
1162 qualifiers (just above). */
1164 if (TREE_CODE (t1) != ARRAY_TYPE
1165 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1166 return 1;
1168 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1169 if (!(attrval = comp_type_attributes (t1, t2)))
1170 return 0;
1172 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1173 val = 0;
1175 switch (TREE_CODE (t1))
1177 case INTEGER_TYPE:
1178 case FIXED_POINT_TYPE:
1179 case REAL_TYPE:
1180 /* With these nodes, we can't determine type equivalence by
1181 looking at what is stored in the nodes themselves, because
1182 two nodes might have different TYPE_MAIN_VARIANTs but still
1183 represent the same type. For example, wchar_t and int could
1184 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1185 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1186 and are distinct types. On the other hand, int and the
1187 following typedef
1189 typedef int INT __attribute((may_alias));
1191 have identical properties, different TYPE_MAIN_VARIANTs, but
1192 represent the same type. The canonical type system keeps
1193 track of equivalence in this case, so we fall back on it. */
1194 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1196 case POINTER_TYPE:
1197 /* Do not remove mode information. */
1198 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1199 break;
1200 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1201 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1202 enum_and_int_p, different_types_p));
1203 break;
1205 case FUNCTION_TYPE:
1206 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1207 different_types_p);
1208 break;
1210 case ARRAY_TYPE:
1212 tree d1 = TYPE_DOMAIN (t1);
1213 tree d2 = TYPE_DOMAIN (t2);
1214 bool d1_variable, d2_variable;
1215 bool d1_zero, d2_zero;
1216 val = 1;
1218 /* Target types must match incl. qualifiers. */
1219 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1220 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1221 enum_and_int_p,
1222 different_types_p)) == 0)
1223 return 0;
1225 if (different_types_p != NULL
1226 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1227 *different_types_p = true;
1228 /* Sizes must match unless one is missing or variable. */
1229 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1230 break;
1232 d1_zero = !TYPE_MAX_VALUE (d1);
1233 d2_zero = !TYPE_MAX_VALUE (d2);
1235 d1_variable = (!d1_zero
1236 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1237 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1238 d2_variable = (!d2_zero
1239 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1240 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1241 d1_variable = d1_variable || (d1_zero && C_TYPE_VARIABLE_SIZE (t1));
1242 d2_variable = d2_variable || (d2_zero && C_TYPE_VARIABLE_SIZE (t2));
1244 if (different_types_p != NULL
1245 && d1_variable != d2_variable)
1246 *different_types_p = true;
1247 if (d1_variable || d2_variable)
1248 break;
1249 if (d1_zero && d2_zero)
1250 break;
1251 if (d1_zero || d2_zero
1252 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1253 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1254 val = 0;
1256 break;
1259 case ENUMERAL_TYPE:
1260 case RECORD_TYPE:
1261 case UNION_TYPE:
1262 if (val != 1 && !same_translation_unit_p (t1, t2))
1264 tree a1 = TYPE_ATTRIBUTES (t1);
1265 tree a2 = TYPE_ATTRIBUTES (t2);
1267 if (! attribute_list_contained (a1, a2)
1268 && ! attribute_list_contained (a2, a1))
1269 break;
1271 if (attrval != 2)
1272 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1273 different_types_p);
1274 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1275 different_types_p);
1277 break;
1279 case VECTOR_TYPE:
1280 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1281 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1282 enum_and_int_p, different_types_p));
1283 break;
1285 default:
1286 break;
1288 return attrval == 2 && val == 1 ? 2 : val;
1291 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1292 their qualifiers, except for named address spaces. If the pointers point to
1293 different named addresses, then we must determine if one address space is a
1294 subset of the other. */
1296 static int
1297 comp_target_types (location_t location, tree ttl, tree ttr)
1299 int val;
1300 int val_ped;
1301 tree mvl = TREE_TYPE (ttl);
1302 tree mvr = TREE_TYPE (ttr);
1303 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1304 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1305 addr_space_t as_common;
1306 bool enum_and_int_p;
1308 /* Fail if pointers point to incompatible address spaces. */
1309 if (!addr_space_superset (asl, asr, &as_common))
1310 return 0;
1312 /* For pedantic record result of comptypes on arrays before losing
1313 qualifiers on the element type below. */
1314 val_ped = 1;
1316 if (TREE_CODE (mvl) == ARRAY_TYPE
1317 && TREE_CODE (mvr) == ARRAY_TYPE)
1318 val_ped = comptypes (mvl, mvr);
1320 /* Qualifiers on element types of array types that are
1321 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1323 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1324 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1325 : TYPE_MAIN_VARIANT (mvl));
1327 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1328 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1329 : TYPE_MAIN_VARIANT (mvr));
1331 enum_and_int_p = false;
1332 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1334 if (val == 1 && val_ped != 1)
1335 pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1336 "in ISO C before C2X");
1338 if (val == 2)
1339 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1341 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1342 warning_at (location, OPT_Wc___compat,
1343 "pointer target types incompatible in C++");
1345 return val;
1348 /* Subroutines of `comptypes'. */
1350 /* Determine whether two trees derive from the same translation unit.
1351 If the CONTEXT chain ends in a null, that tree's context is still
1352 being parsed, so if two trees have context chains ending in null,
1353 they're in the same translation unit. */
1355 bool
1356 same_translation_unit_p (const_tree t1, const_tree t2)
1358 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1359 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1361 case tcc_declaration:
1362 t1 = DECL_CONTEXT (t1); break;
1363 case tcc_type:
1364 t1 = TYPE_CONTEXT (t1); break;
1365 case tcc_exceptional:
1366 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1367 default: gcc_unreachable ();
1370 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1371 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1373 case tcc_declaration:
1374 t2 = DECL_CONTEXT (t2); break;
1375 case tcc_type:
1376 t2 = TYPE_CONTEXT (t2); break;
1377 case tcc_exceptional:
1378 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1379 default: gcc_unreachable ();
1382 return t1 == t2;
1385 /* Allocate the seen two types, assuming that they are compatible. */
1387 static struct tagged_tu_seen_cache *
1388 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1390 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1391 tu->next = tagged_tu_seen_base;
1392 tu->t1 = t1;
1393 tu->t2 = t2;
1395 tagged_tu_seen_base = tu;
1397 /* The C standard says that two structures in different translation
1398 units are compatible with each other only if the types of their
1399 fields are compatible (among other things). We assume that they
1400 are compatible until proven otherwise when building the cache.
1401 An example where this can occur is:
1402 struct a
1404 struct a *next;
1406 If we are comparing this against a similar struct in another TU,
1407 and did not assume they were compatible, we end up with an infinite
1408 loop. */
1409 tu->val = 1;
1410 return tu;
1413 /* Free the seen types until we get to TU_TIL. */
1415 static void
1416 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1418 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1419 while (tu != tu_til)
1421 const struct tagged_tu_seen_cache *const tu1
1422 = (const struct tagged_tu_seen_cache *) tu;
1423 tu = tu1->next;
1424 XDELETE (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1426 tagged_tu_seen_base = tu_til;
1429 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1430 compatible. If the two types are not the same (which has been
1431 checked earlier), this can only happen when multiple translation
1432 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1433 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1434 comptypes_internal. */
1436 static int
1437 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1438 bool *enum_and_int_p, bool *different_types_p)
1440 tree s1, s2;
1441 bool needs_warning = false;
1443 /* We have to verify that the tags of the types are the same. This
1444 is harder than it looks because this may be a typedef, so we have
1445 to go look at the original type. It may even be a typedef of a
1446 typedef...
1447 In the case of compiler-created builtin structs the TYPE_DECL
1448 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1449 while (TYPE_NAME (t1)
1450 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1451 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1452 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1454 while (TYPE_NAME (t2)
1455 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1456 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1457 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1459 /* C90 didn't have the requirement that the two tags be the same. */
1460 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1461 return 0;
1463 /* C90 didn't say what happened if one or both of the types were
1464 incomplete; we choose to follow C99 rules here, which is that they
1465 are compatible. */
1466 if (TYPE_SIZE (t1) == NULL
1467 || TYPE_SIZE (t2) == NULL)
1468 return 1;
1471 const struct tagged_tu_seen_cache * tts_i;
1472 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1473 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1474 return tts_i->val;
1477 switch (TREE_CODE (t1))
1479 case ENUMERAL_TYPE:
1481 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1482 /* Speed up the case where the type values are in the same order. */
1483 tree tv1 = TYPE_VALUES (t1);
1484 tree tv2 = TYPE_VALUES (t2);
1486 if (tv1 == tv2)
1488 return 1;
1491 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1493 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1494 break;
1495 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1497 tu->val = 0;
1498 return 0;
1502 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1504 return 1;
1506 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1508 tu->val = 0;
1509 return 0;
1512 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1514 tu->val = 0;
1515 return 0;
1518 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1520 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1521 if (s2 == NULL
1522 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1524 tu->val = 0;
1525 return 0;
1528 return 1;
1531 case UNION_TYPE:
1533 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1534 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1536 tu->val = 0;
1537 return 0;
1540 /* Speed up the common case where the fields are in the same order. */
1541 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1542 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1544 int result;
1546 if (DECL_NAME (s1) != DECL_NAME (s2))
1547 break;
1548 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1549 enum_and_int_p, different_types_p);
1551 if (result != 1 && !DECL_NAME (s1))
1552 break;
1553 if (result == 0)
1555 tu->val = 0;
1556 return 0;
1558 if (result == 2)
1559 needs_warning = true;
1561 if (TREE_CODE (s1) == FIELD_DECL
1562 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1563 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1565 tu->val = 0;
1566 return 0;
1569 if (!s1 && !s2)
1571 tu->val = needs_warning ? 2 : 1;
1572 return tu->val;
1575 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1577 bool ok = false;
1579 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1580 if (DECL_NAME (s1) == DECL_NAME (s2))
1582 int result;
1584 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1585 enum_and_int_p,
1586 different_types_p);
1588 if (result != 1 && !DECL_NAME (s1))
1589 continue;
1590 if (result == 0)
1592 tu->val = 0;
1593 return 0;
1595 if (result == 2)
1596 needs_warning = true;
1598 if (TREE_CODE (s1) == FIELD_DECL
1599 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1600 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1601 break;
1603 ok = true;
1604 break;
1606 if (!ok)
1608 tu->val = 0;
1609 return 0;
1612 tu->val = needs_warning ? 2 : 10;
1613 return tu->val;
1616 case RECORD_TYPE:
1618 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1620 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1621 s1 && s2;
1622 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1624 int result;
1625 if (TREE_CODE (s1) != TREE_CODE (s2)
1626 || DECL_NAME (s1) != DECL_NAME (s2))
1627 break;
1628 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1629 enum_and_int_p, different_types_p);
1630 if (result == 0)
1631 break;
1632 if (result == 2)
1633 needs_warning = true;
1635 if (TREE_CODE (s1) == FIELD_DECL
1636 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1637 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1638 break;
1640 if (s1 && s2)
1641 tu->val = 0;
1642 else
1643 tu->val = needs_warning ? 2 : 1;
1644 return tu->val;
1647 default:
1648 gcc_unreachable ();
1652 /* Return 1 if two function types F1 and F2 are compatible.
1653 If either type specifies no argument types,
1654 the other must specify a fixed number of self-promoting arg types.
1655 Otherwise, if one type specifies only the number of arguments,
1656 the other must specify that number of self-promoting arg types.
1657 Otherwise, the argument types must match.
1658 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1660 static int
1661 function_types_compatible_p (const_tree f1, const_tree f2,
1662 bool *enum_and_int_p, bool *different_types_p)
1664 tree args1, args2;
1665 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1666 int val = 1;
1667 int val1;
1668 tree ret1, ret2;
1670 ret1 = TREE_TYPE (f1);
1671 ret2 = TREE_TYPE (f2);
1673 /* 'volatile' qualifiers on a function's return type used to mean
1674 the function is noreturn. */
1675 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1676 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1677 if (TYPE_VOLATILE (ret1))
1678 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1679 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1680 if (TYPE_VOLATILE (ret2))
1681 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1682 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1683 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1684 if (val == 0)
1685 return 0;
1687 args1 = TYPE_ARG_TYPES (f1);
1688 args2 = TYPE_ARG_TYPES (f2);
1690 if (different_types_p != NULL
1691 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1692 *different_types_p = true;
1694 /* An unspecified parmlist matches any specified parmlist
1695 whose argument types don't need default promotions. */
1697 if (args1 == NULL_TREE)
1699 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
1700 return 0;
1701 if (!self_promoting_args_p (args2))
1702 return 0;
1703 /* If one of these types comes from a non-prototype fn definition,
1704 compare that with the other type's arglist.
1705 If they don't match, ask for a warning (but no error). */
1706 if (TYPE_ACTUAL_ARG_TYPES (f1)
1707 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1708 enum_and_int_p, different_types_p) != 1)
1709 val = 2;
1710 return val;
1712 if (args2 == NULL_TREE)
1714 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
1715 return 0;
1716 if (!self_promoting_args_p (args1))
1717 return 0;
1718 if (TYPE_ACTUAL_ARG_TYPES (f2)
1719 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1720 enum_and_int_p, different_types_p) != 1)
1721 val = 2;
1722 return val;
1725 /* Both types have argument lists: compare them and propagate results. */
1726 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1727 different_types_p);
1728 return val1 != 1 ? val1 : val;
1731 /* Check two lists of types for compatibility, returning 0 for
1732 incompatible, 1 for compatible, or 2 for compatible with
1733 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1734 comptypes_internal. */
1736 static int
1737 type_lists_compatible_p (const_tree args1, const_tree args2,
1738 bool *enum_and_int_p, bool *different_types_p)
1740 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1741 int val = 1;
1742 int newval = 0;
1744 while (1)
1746 tree a1, mv1, a2, mv2;
1747 if (args1 == NULL_TREE && args2 == NULL_TREE)
1748 return val;
1749 /* If one list is shorter than the other,
1750 they fail to match. */
1751 if (args1 == NULL_TREE || args2 == NULL_TREE)
1752 return 0;
1753 mv1 = a1 = TREE_VALUE (args1);
1754 mv2 = a2 = TREE_VALUE (args2);
1755 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1756 mv1 = (TYPE_ATOMIC (mv1)
1757 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1758 TYPE_QUAL_ATOMIC)
1759 : TYPE_MAIN_VARIANT (mv1));
1760 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1761 mv2 = (TYPE_ATOMIC (mv2)
1762 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1763 TYPE_QUAL_ATOMIC)
1764 : TYPE_MAIN_VARIANT (mv2));
1765 /* A null pointer instead of a type
1766 means there is supposed to be an argument
1767 but nothing is specified about what type it has.
1768 So match anything that self-promotes. */
1769 if (different_types_p != NULL
1770 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1771 *different_types_p = true;
1772 if (a1 == NULL_TREE)
1774 if (c_type_promotes_to (a2) != a2)
1775 return 0;
1777 else if (a2 == NULL_TREE)
1779 if (c_type_promotes_to (a1) != a1)
1780 return 0;
1782 /* If one of the lists has an error marker, ignore this arg. */
1783 else if (TREE_CODE (a1) == ERROR_MARK
1784 || TREE_CODE (a2) == ERROR_MARK)
1786 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1787 different_types_p)))
1789 if (different_types_p != NULL)
1790 *different_types_p = true;
1791 /* Allow wait (union {union wait *u; int *i} *)
1792 and wait (union wait *) to be compatible. */
1793 if (TREE_CODE (a1) == UNION_TYPE
1794 && (TYPE_NAME (a1) == NULL_TREE
1795 || TYPE_TRANSPARENT_AGGR (a1))
1796 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1797 && tree_int_cst_equal (TYPE_SIZE (a1),
1798 TYPE_SIZE (a2)))
1800 tree memb;
1801 for (memb = TYPE_FIELDS (a1);
1802 memb; memb = DECL_CHAIN (memb))
1804 tree mv3 = TREE_TYPE (memb);
1805 if (mv3 && mv3 != error_mark_node
1806 && TREE_CODE (mv3) != ARRAY_TYPE)
1807 mv3 = (TYPE_ATOMIC (mv3)
1808 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1809 TYPE_QUAL_ATOMIC)
1810 : TYPE_MAIN_VARIANT (mv3));
1811 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1812 different_types_p))
1813 break;
1815 if (memb == NULL_TREE)
1816 return 0;
1818 else if (TREE_CODE (a2) == UNION_TYPE
1819 && (TYPE_NAME (a2) == NULL_TREE
1820 || TYPE_TRANSPARENT_AGGR (a2))
1821 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1822 && tree_int_cst_equal (TYPE_SIZE (a2),
1823 TYPE_SIZE (a1)))
1825 tree memb;
1826 for (memb = TYPE_FIELDS (a2);
1827 memb; memb = DECL_CHAIN (memb))
1829 tree mv3 = TREE_TYPE (memb);
1830 if (mv3 && mv3 != error_mark_node
1831 && TREE_CODE (mv3) != ARRAY_TYPE)
1832 mv3 = (TYPE_ATOMIC (mv3)
1833 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1834 TYPE_QUAL_ATOMIC)
1835 : TYPE_MAIN_VARIANT (mv3));
1836 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1837 different_types_p))
1838 break;
1840 if (memb == NULL_TREE)
1841 return 0;
1843 else
1844 return 0;
1847 /* comptypes said ok, but record if it said to warn. */
1848 if (newval > val)
1849 val = newval;
1851 args1 = TREE_CHAIN (args1);
1852 args2 = TREE_CHAIN (args2);
1856 /* Compute the size to increment a pointer by. When a function type or void
1857 type or incomplete type is passed, size_one_node is returned.
1858 This function does not emit any diagnostics; the caller is responsible
1859 for that. */
1861 static tree
1862 c_size_in_bytes (const_tree type)
1864 enum tree_code code = TREE_CODE (type);
1866 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1867 || !COMPLETE_TYPE_P (type))
1868 return size_one_node;
1870 /* Convert in case a char is more than one unit. */
1871 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1872 size_int (TYPE_PRECISION (char_type_node)
1873 / BITS_PER_UNIT));
1876 /* Return either DECL or its known constant value (if it has one). */
1878 tree
1879 decl_constant_value_1 (tree decl, bool in_init)
1881 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1882 TREE_CODE (decl) != PARM_DECL
1883 && !TREE_THIS_VOLATILE (decl)
1884 && TREE_READONLY (decl)
1885 && DECL_INITIAL (decl) != NULL_TREE
1886 && !error_operand_p (DECL_INITIAL (decl))
1887 /* This is invalid if initial value is not constant.
1888 If it has either a function call, a memory reference,
1889 or a variable, then re-evaluating it could give different results. */
1890 && TREE_CONSTANT (DECL_INITIAL (decl))
1891 /* Check for cases where this is sub-optimal, even though valid. */
1892 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1893 return DECL_INITIAL (decl);
1894 return decl;
1897 /* Return either DECL or its known constant value (if it has one).
1898 Like the above, but always return decl outside of functions. */
1900 tree
1901 decl_constant_value (tree decl)
1903 /* Don't change a variable array bound or initial value to a constant
1904 in a place where a variable is invalid. */
1905 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1908 /* Convert the array expression EXP to a pointer. */
1909 static tree
1910 array_to_pointer_conversion (location_t loc, tree exp)
1912 tree orig_exp = exp;
1913 tree type = TREE_TYPE (exp);
1914 tree adr;
1915 tree restype = TREE_TYPE (type);
1916 tree ptrtype;
1918 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1920 STRIP_TYPE_NOPS (exp);
1922 copy_warning (exp, orig_exp);
1924 ptrtype = build_pointer_type (restype);
1926 if (INDIRECT_REF_P (exp))
1927 return convert (ptrtype, TREE_OPERAND (exp, 0));
1929 /* In C++ array compound literals are temporary objects unless they are
1930 const or appear in namespace scope, so they are destroyed too soon
1931 to use them for much of anything (c++/53220). */
1932 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1934 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1935 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1936 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1937 "converting an array compound literal to a pointer "
1938 "is ill-formed in C++");
1941 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1942 return convert (ptrtype, adr);
1945 /* Convert the function expression EXP to a pointer. */
1946 static tree
1947 function_to_pointer_conversion (location_t loc, tree exp)
1949 tree orig_exp = exp;
1951 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1953 STRIP_TYPE_NOPS (exp);
1955 copy_warning (exp, orig_exp);
1957 return build_unary_op (loc, ADDR_EXPR, exp, false);
1960 /* Mark EXP as read, not just set, for set but not used -Wunused
1961 warning purposes. */
1963 void
1964 mark_exp_read (tree exp)
1966 switch (TREE_CODE (exp))
1968 case VAR_DECL:
1969 case PARM_DECL:
1970 DECL_READ_P (exp) = 1;
1971 break;
1972 case ARRAY_REF:
1973 case COMPONENT_REF:
1974 case MODIFY_EXPR:
1975 case REALPART_EXPR:
1976 case IMAGPART_EXPR:
1977 CASE_CONVERT:
1978 case ADDR_EXPR:
1979 case VIEW_CONVERT_EXPR:
1980 mark_exp_read (TREE_OPERAND (exp, 0));
1981 break;
1982 case COMPOUND_EXPR:
1983 /* Pattern match what build_atomic_assign produces with modifycode
1984 NOP_EXPR. */
1985 if (VAR_P (TREE_OPERAND (exp, 1))
1986 && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
1987 && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
1989 tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1990 tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
1991 if (TREE_CODE (t1) == TARGET_EXPR
1992 && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
1993 && TREE_CODE (t2) == CALL_EXPR)
1995 tree fndecl = get_callee_fndecl (t2);
1996 tree arg = NULL_TREE;
1997 if (fndecl
1998 && TREE_CODE (fndecl) == FUNCTION_DECL
1999 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
2000 && call_expr_nargs (t2) >= 2)
2001 switch (DECL_FUNCTION_CODE (fndecl))
2003 case BUILT_IN_ATOMIC_STORE:
2004 arg = CALL_EXPR_ARG (t2, 1);
2005 break;
2006 case BUILT_IN_ATOMIC_STORE_1:
2007 case BUILT_IN_ATOMIC_STORE_2:
2008 case BUILT_IN_ATOMIC_STORE_4:
2009 case BUILT_IN_ATOMIC_STORE_8:
2010 case BUILT_IN_ATOMIC_STORE_16:
2011 arg = CALL_EXPR_ARG (t2, 0);
2012 break;
2013 default:
2014 break;
2016 if (arg)
2018 STRIP_NOPS (arg);
2019 if (TREE_CODE (arg) == ADDR_EXPR
2020 && DECL_P (TREE_OPERAND (arg, 0))
2021 && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2022 mark_exp_read (TREE_OPERAND (arg, 0));
2026 /* FALLTHRU */
2027 case C_MAYBE_CONST_EXPR:
2028 mark_exp_read (TREE_OPERAND (exp, 1));
2029 break;
2030 default:
2031 break;
2035 /* Perform the default conversion of arrays and functions to pointers.
2036 Return the result of converting EXP. For any other expression, just
2037 return EXP.
2039 LOC is the location of the expression. */
2041 struct c_expr
2042 default_function_array_conversion (location_t loc, struct c_expr exp)
2044 tree orig_exp = exp.value;
2045 tree type = TREE_TYPE (exp.value);
2046 enum tree_code code = TREE_CODE (type);
2048 switch (code)
2050 case ARRAY_TYPE:
2052 bool not_lvalue = false;
2053 bool lvalue_array_p;
2055 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2056 || CONVERT_EXPR_P (exp.value))
2057 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2059 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2060 not_lvalue = true;
2061 exp.value = TREE_OPERAND (exp.value, 0);
2064 copy_warning (exp.value, orig_exp);
2066 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2067 if (!flag_isoc99 && !lvalue_array_p)
2069 /* Before C99, non-lvalue arrays do not decay to pointers.
2070 Normally, using such an array would be invalid; but it can
2071 be used correctly inside sizeof or as a statement expression.
2072 Thus, do not give an error here; an error will result later. */
2073 return exp;
2076 exp.value = array_to_pointer_conversion (loc, exp.value);
2078 break;
2079 case FUNCTION_TYPE:
2080 exp.value = function_to_pointer_conversion (loc, exp.value);
2081 break;
2082 default:
2083 break;
2086 return exp;
2089 struct c_expr
2090 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2092 mark_exp_read (exp.value);
2093 return default_function_array_conversion (loc, exp);
2096 /* Return whether EXPR should be treated as an atomic lvalue for the
2097 purposes of load and store handling. */
2099 static bool
2100 really_atomic_lvalue (tree expr)
2102 if (error_operand_p (expr))
2103 return false;
2104 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2105 return false;
2106 if (!lvalue_p (expr))
2107 return false;
2109 /* Ignore _Atomic on register variables, since their addresses can't
2110 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2111 sequences wouldn't work. Ignore _Atomic on structures containing
2112 bit-fields, since accessing elements of atomic structures or
2113 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2114 it's undefined at translation time or execution time, and the
2115 normal atomic sequences again wouldn't work. */
2116 while (handled_component_p (expr))
2118 if (TREE_CODE (expr) == COMPONENT_REF
2119 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2120 return false;
2121 expr = TREE_OPERAND (expr, 0);
2123 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2124 return false;
2125 return true;
2128 /* If EXPR is a named constant (C2x) derived from a constexpr variable
2129 - that is, a reference to such a variable, or a member extracted by
2130 a sequence of structure and union (but not array) member accesses
2131 (where union member accesses must access the same member as
2132 initialized) - then return the corresponding initializer;
2133 otherwise, return NULL_TREE. */
2135 static tree
2136 maybe_get_constexpr_init (tree expr)
2138 tree decl = NULL_TREE;
2139 if (TREE_CODE (expr) == VAR_DECL)
2140 decl = expr;
2141 else if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2142 decl = COMPOUND_LITERAL_EXPR_DECL (expr);
2143 if (decl
2144 && C_DECL_DECLARED_CONSTEXPR (decl)
2145 && DECL_INITIAL (decl) != NULL_TREE
2146 && !error_operand_p (DECL_INITIAL (decl)))
2147 return DECL_INITIAL (decl);
2148 if (TREE_CODE (expr) != COMPONENT_REF)
2149 return NULL_TREE;
2150 tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0));
2151 if (inner == NULL_TREE)
2152 return NULL_TREE;
2153 while ((CONVERT_EXPR_P (inner) || TREE_CODE (inner) == NON_LVALUE_EXPR)
2154 && !error_operand_p (inner)
2155 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
2156 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (inner, 0)))))
2157 inner = TREE_OPERAND (inner, 0);
2158 if (TREE_CODE (inner) != CONSTRUCTOR)
2159 return NULL_TREE;
2160 tree field = TREE_OPERAND (expr, 1);
2161 unsigned HOST_WIDE_INT cidx;
2162 tree cfield, cvalue;
2163 bool have_other_init = false;
2164 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner), cidx, cfield, cvalue)
2166 if (cfield == field)
2167 return cvalue;
2168 have_other_init = true;
2170 if (TREE_CODE (TREE_TYPE (inner)) == UNION_TYPE
2171 && (have_other_init || field != TYPE_FIELDS (TREE_TYPE (inner))))
2172 return NULL_TREE;
2173 /* Return a default initializer. */
2174 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (expr)))
2175 return build_constructor (TREE_TYPE (expr), NULL);
2176 return build_zero_cst (TREE_TYPE (expr));
2179 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2180 including converting functions and arrays to pointers if CONVERT_P.
2181 If READ_P, also mark the expression as having been read. If
2182 FOR_INIT, constexpr expressions of structure and union type should
2183 be replaced by the corresponding CONSTRUCTOR; otherwise, only
2184 constexpr scalars (including elements of structures and unions) are
2185 replaced by their initializers. */
2187 struct c_expr
2188 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2189 bool convert_p, bool read_p, bool for_init)
2191 bool force_non_npc = false;
2192 if (read_p)
2193 mark_exp_read (exp.value);
2194 if (convert_p)
2195 exp = default_function_array_conversion (loc, exp);
2196 if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2197 exp.value = require_complete_type (loc, exp.value);
2198 if (for_init || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp.value)))
2200 tree init = maybe_get_constexpr_init (exp.value);
2201 if (init != NULL_TREE)
2203 /* A named constant of pointer type or type nullptr_t is not
2204 a null pointer constant even if the initializer is
2205 one. */
2206 if (TREE_CODE (init) == INTEGER_CST
2207 && !INTEGRAL_TYPE_P (TREE_TYPE (init))
2208 && integer_zerop (init))
2209 force_non_npc = true;
2210 exp.value = init;
2213 if (really_atomic_lvalue (exp.value))
2215 vec<tree, va_gc> *params;
2216 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2217 tree expr_type = TREE_TYPE (exp.value);
2218 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2219 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2221 gcc_assert (TYPE_ATOMIC (expr_type));
2223 /* Expansion of a generic atomic load may require an addition
2224 element, so allocate enough to prevent a resize. */
2225 vec_alloc (params, 4);
2227 /* Remove the qualifiers for the rest of the expressions and
2228 create the VAL temp variable to hold the RHS. */
2229 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2230 tmp = create_tmp_var_raw (nonatomic_type);
2231 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2232 TREE_ADDRESSABLE (tmp) = 1;
2233 /* Do not disable warnings for TMP even though it's artificial.
2234 -Winvalid-memory-model depends on it. */
2236 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2237 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2238 params->quick_push (expr_addr);
2239 params->quick_push (tmp_addr);
2240 params->quick_push (seq_cst);
2241 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2243 /* EXPR is always read. */
2244 mark_exp_read (exp.value);
2246 /* Return tmp which contains the value loaded. */
2247 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2248 NULL_TREE, NULL_TREE);
2250 if (convert_p && !error_operand_p (exp.value)
2251 && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2252 exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2253 if (force_non_npc)
2254 exp.value = build1 (NOP_EXPR, TREE_TYPE (exp.value), exp.value);
2255 return exp;
2258 /* EXP is an expression of integer type. Apply the integer promotions
2259 to it and return the promoted value. */
2261 tree
2262 perform_integral_promotions (tree exp)
2264 tree type = TREE_TYPE (exp);
2265 enum tree_code code = TREE_CODE (type);
2267 gcc_assert (INTEGRAL_TYPE_P (type));
2269 /* Convert enums to the result of applying the integer promotions to
2270 their underlying type. */
2271 if (code == ENUMERAL_TYPE)
2273 type = ENUM_UNDERLYING_TYPE (type);
2274 if (c_promoting_integer_type_p (type))
2276 if (TYPE_UNSIGNED (type)
2277 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2278 type = unsigned_type_node;
2279 else
2280 type = integer_type_node;
2283 return convert (type, exp);
2286 /* ??? This should no longer be needed now bit-fields have their
2287 proper types. */
2288 if (TREE_CODE (exp) == COMPONENT_REF
2289 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2290 /* If it's thinner than an int, promote it like a
2291 c_promoting_integer_type_p, otherwise leave it alone. */
2292 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2293 TYPE_PRECISION (integer_type_node)) < 0)
2294 return convert (integer_type_node, exp);
2296 if (c_promoting_integer_type_p (type))
2298 /* Preserve unsignedness if not really getting any wider. */
2299 if (TYPE_UNSIGNED (type)
2300 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2301 return convert (unsigned_type_node, exp);
2303 return convert (integer_type_node, exp);
2306 return exp;
2310 /* Perform default promotions for C data used in expressions.
2311 Enumeral types or short or char are converted to int.
2312 In addition, manifest constants symbols are replaced by their values. */
2314 tree
2315 default_conversion (tree exp)
2317 tree orig_exp;
2318 tree type = TREE_TYPE (exp);
2319 enum tree_code code = TREE_CODE (type);
2320 tree promoted_type;
2322 mark_exp_read (exp);
2324 /* Functions and arrays have been converted during parsing. */
2325 gcc_assert (code != FUNCTION_TYPE);
2326 if (code == ARRAY_TYPE)
2327 return exp;
2329 /* Constants can be used directly unless they're not loadable. */
2330 if (TREE_CODE (exp) == CONST_DECL)
2331 exp = DECL_INITIAL (exp);
2333 /* Strip no-op conversions. */
2334 orig_exp = exp;
2335 STRIP_TYPE_NOPS (exp);
2337 copy_warning (exp, orig_exp);
2339 if (code == VOID_TYPE)
2341 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2342 "void value not ignored as it ought to be");
2343 return error_mark_node;
2346 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2347 if (exp == error_mark_node)
2348 return error_mark_node;
2350 promoted_type = targetm.promoted_type (type);
2351 if (promoted_type)
2352 return convert (promoted_type, exp);
2354 if (INTEGRAL_TYPE_P (type))
2355 return perform_integral_promotions (exp);
2357 return exp;
2360 /* Look up COMPONENT in a structure or union TYPE.
2362 If the component name is not found, returns NULL_TREE. Otherwise,
2363 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2364 stepping down the chain to the component, which is in the last
2365 TREE_VALUE of the list. Normally the list is of length one, but if
2366 the component is embedded within (nested) anonymous structures or
2367 unions, the list steps down the chain to the component. */
2369 static tree
2370 lookup_field (tree type, tree component)
2372 tree field;
2374 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2375 to the field elements. Use a binary search on this array to quickly
2376 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2377 will always be set for structures which have many elements.
2379 Duplicate field checking replaces duplicates with NULL_TREE so
2380 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2381 case just iterate using DECL_CHAIN. */
2383 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2384 && !seen_error ())
2386 int bot, top, half;
2387 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2389 field = TYPE_FIELDS (type);
2390 bot = 0;
2391 top = TYPE_LANG_SPECIFIC (type)->s->len;
2392 while (top - bot > 1)
2394 half = (top - bot + 1) >> 1;
2395 field = field_array[bot+half];
2397 if (DECL_NAME (field) == NULL_TREE)
2399 /* Step through all anon unions in linear fashion. */
2400 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2402 field = field_array[bot++];
2403 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2405 tree anon = lookup_field (TREE_TYPE (field), component);
2407 if (anon)
2408 return tree_cons (NULL_TREE, field, anon);
2410 /* The Plan 9 compiler permits referring
2411 directly to an anonymous struct/union field
2412 using a typedef name. */
2413 if (flag_plan9_extensions
2414 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2415 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2416 == TYPE_DECL)
2417 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2418 == component))
2419 break;
2423 /* Entire record is only anon unions. */
2424 if (bot > top)
2425 return NULL_TREE;
2427 /* Restart the binary search, with new lower bound. */
2428 continue;
2431 if (DECL_NAME (field) == component)
2432 break;
2433 if (DECL_NAME (field) < component)
2434 bot += half;
2435 else
2436 top = bot + half;
2439 if (DECL_NAME (field_array[bot]) == component)
2440 field = field_array[bot];
2441 else if (DECL_NAME (field) != component)
2442 return NULL_TREE;
2444 else
2446 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2448 if (DECL_NAME (field) == NULL_TREE
2449 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2451 tree anon = lookup_field (TREE_TYPE (field), component);
2453 if (anon)
2454 return tree_cons (NULL_TREE, field, anon);
2456 /* The Plan 9 compiler permits referring directly to an
2457 anonymous struct/union field using a typedef
2458 name. */
2459 if (flag_plan9_extensions
2460 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2461 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2462 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2463 == component))
2464 break;
2467 if (DECL_NAME (field) == component)
2468 break;
2471 if (field == NULL_TREE)
2472 return NULL_TREE;
2475 return tree_cons (NULL_TREE, field, NULL_TREE);
2478 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2480 static void
2481 lookup_field_fuzzy_find_candidates (tree type, tree component,
2482 vec<tree> *candidates)
2484 tree field;
2485 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2487 if (DECL_NAME (field) == NULL_TREE
2488 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2489 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2490 candidates);
2492 if (DECL_NAME (field))
2493 candidates->safe_push (DECL_NAME (field));
2497 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2498 rather than returning a TREE_LIST for an exact match. */
2500 static tree
2501 lookup_field_fuzzy (tree type, tree component)
2503 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2505 /* First, gather a list of candidates. */
2506 auto_vec <tree> candidates;
2508 lookup_field_fuzzy_find_candidates (type, component,
2509 &candidates);
2511 return find_closest_identifier (component, &candidates);
2514 /* Support function for build_component_ref's error-handling.
2516 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2517 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2519 static bool
2520 should_suggest_deref_p (tree datum_type)
2522 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2523 allows "." for ptrs; we could be handling a failed attempt
2524 to access a property. */
2525 if (c_dialect_objc ())
2526 return false;
2528 /* Only suggest it for pointers... */
2529 if (TREE_CODE (datum_type) != POINTER_TYPE)
2530 return false;
2532 /* ...to structs/unions. */
2533 tree underlying_type = TREE_TYPE (datum_type);
2534 enum tree_code code = TREE_CODE (underlying_type);
2535 if (code == RECORD_TYPE || code == UNION_TYPE)
2536 return true;
2537 else
2538 return false;
2541 /* Make an expression to refer to the COMPONENT field of structure or
2542 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2543 location of the COMPONENT_REF. COMPONENT_LOC is the location
2544 of COMPONENT. ARROW_LOC is the location of the first -> operand if
2545 it is from -> operator. */
2547 tree
2548 build_component_ref (location_t loc, tree datum, tree component,
2549 location_t component_loc, location_t arrow_loc)
2551 tree type = TREE_TYPE (datum);
2552 enum tree_code code = TREE_CODE (type);
2553 tree field = NULL;
2554 tree ref;
2555 bool datum_lvalue = lvalue_p (datum);
2557 if (!objc_is_public (datum, component))
2558 return error_mark_node;
2560 /* Detect Objective-C property syntax object.property. */
2561 if (c_dialect_objc ()
2562 && (ref = objc_maybe_build_component_ref (datum, component)))
2563 return ref;
2565 /* See if there is a field or component with name COMPONENT. */
2567 if (code == RECORD_TYPE || code == UNION_TYPE)
2569 if (!COMPLETE_TYPE_P (type))
2571 c_incomplete_type_error (loc, NULL_TREE, type);
2572 return error_mark_node;
2575 field = lookup_field (type, component);
2577 if (!field)
2579 tree guessed_id = lookup_field_fuzzy (type, component);
2580 if (guessed_id)
2582 /* Attempt to provide a fixit replacement hint, if
2583 we have a valid range for the component. */
2584 location_t reported_loc
2585 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2586 gcc_rich_location rich_loc (reported_loc);
2587 if (component_loc != UNKNOWN_LOCATION)
2588 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2589 error_at (&rich_loc,
2590 "%qT has no member named %qE; did you mean %qE?",
2591 type, component, guessed_id);
2593 else
2594 error_at (loc, "%qT has no member named %qE", type, component);
2595 return error_mark_node;
2598 /* Accessing elements of atomic structures or unions is undefined
2599 behavior (C11 6.5.2.3#5). */
2600 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2602 if (code == RECORD_TYPE)
2603 warning_at (loc, 0, "accessing a member %qE of an atomic "
2604 "structure %qE", component, datum);
2605 else
2606 warning_at (loc, 0, "accessing a member %qE of an atomic "
2607 "union %qE", component, datum);
2610 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2611 This might be better solved in future the way the C++ front
2612 end does it - by giving the anonymous entities each a
2613 separate name and type, and then have build_component_ref
2614 recursively call itself. We can't do that here. */
2617 tree subdatum = TREE_VALUE (field);
2618 int quals;
2619 tree subtype;
2620 bool use_datum_quals;
2622 if (TREE_TYPE (subdatum) == error_mark_node)
2623 return error_mark_node;
2625 /* If this is an rvalue, it does not have qualifiers in C
2626 standard terms and we must avoid propagating such
2627 qualifiers down to a non-lvalue array that is then
2628 converted to a pointer. */
2629 use_datum_quals = (datum_lvalue
2630 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2632 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2633 if (use_datum_quals)
2634 quals |= TYPE_QUALS (TREE_TYPE (datum));
2635 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2637 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2638 NULL_TREE);
2639 SET_EXPR_LOCATION (ref, loc);
2640 if (TREE_READONLY (subdatum)
2641 || (use_datum_quals && TREE_READONLY (datum)))
2642 TREE_READONLY (ref) = 1;
2643 if (TREE_THIS_VOLATILE (subdatum)
2644 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2645 TREE_THIS_VOLATILE (ref) = 1;
2647 if (TREE_UNAVAILABLE (subdatum))
2648 error_unavailable_use (subdatum, NULL_TREE);
2649 else if (TREE_DEPRECATED (subdatum))
2650 warn_deprecated_use (subdatum, NULL_TREE);
2652 datum = ref;
2654 field = TREE_CHAIN (field);
2656 while (field);
2658 return ref;
2660 else if (should_suggest_deref_p (type))
2662 /* Special-case the error message for "ptr.field" for the case
2663 where the user has confused "." vs "->". */
2664 rich_location richloc (line_table, loc);
2665 if (TREE_CODE (datum) == INDIRECT_REF && arrow_loc != UNKNOWN_LOCATION)
2667 richloc.add_fixit_insert_before (arrow_loc, "(*");
2668 richloc.add_fixit_insert_after (arrow_loc, ")");
2669 error_at (&richloc,
2670 "%qE is a pointer to pointer; did you mean to dereference "
2671 "it before applying %<->%> to it?",
2672 TREE_OPERAND (datum, 0));
2674 else
2676 /* "loc" should be the "." token. */
2677 richloc.add_fixit_replace ("->");
2678 error_at (&richloc,
2679 "%qE is a pointer; did you mean to use %<->%>?",
2680 datum);
2682 return error_mark_node;
2684 else if (code != ERROR_MARK)
2685 error_at (loc,
2686 "request for member %qE in something not a structure or union",
2687 component);
2689 return error_mark_node;
2692 /* Given an expression PTR for a pointer, return an expression
2693 for the value pointed to.
2694 ERRORSTRING is the name of the operator to appear in error messages.
2696 LOC is the location to use for the generated tree. */
2698 tree
2699 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2701 tree pointer = default_conversion (ptr);
2702 tree type = TREE_TYPE (pointer);
2703 tree ref;
2705 if (TREE_CODE (type) == POINTER_TYPE)
2707 if (CONVERT_EXPR_P (pointer)
2708 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2710 /* If a warning is issued, mark it to avoid duplicates from
2711 the backend. This only needs to be done at
2712 warn_strict_aliasing > 2. */
2713 if (warn_strict_aliasing > 2)
2714 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2715 type, TREE_OPERAND (pointer, 0)))
2716 suppress_warning (pointer, OPT_Wstrict_aliasing_);
2719 if (TREE_CODE (pointer) == ADDR_EXPR
2720 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2721 == TREE_TYPE (type)))
2723 ref = TREE_OPERAND (pointer, 0);
2724 protected_set_expr_location (ref, loc);
2725 return ref;
2727 else
2729 tree t = TREE_TYPE (type);
2731 ref = build1 (INDIRECT_REF, t, pointer);
2733 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2734 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2736 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2737 so that we get the proper error message if the result is used
2738 to assign to. Also, &* is supposed to be a no-op.
2739 And ANSI C seems to specify that the type of the result
2740 should be the const type. */
2741 /* A de-reference of a pointer to const is not a const. It is valid
2742 to change it via some other pointer. */
2743 TREE_READONLY (ref) = TYPE_READONLY (t);
2744 TREE_SIDE_EFFECTS (ref)
2745 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2746 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2747 protected_set_expr_location (ref, loc);
2748 return ref;
2751 else if (TREE_CODE (pointer) != ERROR_MARK)
2752 invalid_indirection_error (loc, type, errstring);
2754 return error_mark_node;
2757 /* This handles expressions of the form "a[i]", which denotes
2758 an array reference.
2760 This is logically equivalent in C to *(a+i), but we may do it differently.
2761 If A is a variable or a member, we generate a primitive ARRAY_REF.
2762 This avoids forcing the array out of registers, and can work on
2763 arrays that are not lvalues (for example, members of structures returned
2764 by functions).
2766 For vector types, allow vector[i] but not i[vector], and create
2767 *(((type*)&vectortype) + i) for the expression.
2769 LOC is the location to use for the returned expression. */
2771 tree
2772 build_array_ref (location_t loc, tree array, tree index)
2774 tree ret;
2775 bool swapped = false;
2776 if (TREE_TYPE (array) == error_mark_node
2777 || TREE_TYPE (index) == error_mark_node)
2778 return error_mark_node;
2780 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2781 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2782 /* Allow vector[index] but not index[vector]. */
2783 && !gnu_vector_type_p (TREE_TYPE (array)))
2785 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2786 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2788 error_at (loc,
2789 "subscripted value is neither array nor pointer nor vector");
2791 return error_mark_node;
2793 std::swap (array, index);
2794 swapped = true;
2797 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2799 error_at (loc, "array subscript is not an integer");
2800 return error_mark_node;
2803 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2805 error_at (loc, "subscripted value is pointer to function");
2806 return error_mark_node;
2809 /* ??? Existing practice has been to warn only when the char
2810 index is syntactically the index, not for char[array]. */
2811 if (!swapped)
2812 warn_array_subscript_with_type_char (loc, index);
2814 /* Apply default promotions *after* noticing character types. */
2815 index = default_conversion (index);
2816 if (index == error_mark_node)
2817 return error_mark_node;
2819 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2821 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2822 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2824 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2826 tree rval, type;
2828 /* An array that is indexed by a non-constant
2829 cannot be stored in a register; we must be able to do
2830 address arithmetic on its address.
2831 Likewise an array of elements of variable size. */
2832 if (TREE_CODE (index) != INTEGER_CST
2833 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2834 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2836 if (!c_mark_addressable (array, true))
2837 return error_mark_node;
2839 /* An array that is indexed by a constant value which is not within
2840 the array bounds cannot be stored in a register either; because we
2841 would get a crash in store_bit_field/extract_bit_field when trying
2842 to access a non-existent part of the register. */
2843 if (TREE_CODE (index) == INTEGER_CST
2844 && TYPE_DOMAIN (TREE_TYPE (array))
2845 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2847 if (!c_mark_addressable (array))
2848 return error_mark_node;
2851 if ((pedantic || warn_c90_c99_compat)
2852 && ! was_vector)
2854 tree foo = array;
2855 while (TREE_CODE (foo) == COMPONENT_REF)
2856 foo = TREE_OPERAND (foo, 0);
2857 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2858 pedwarn (loc, OPT_Wpedantic,
2859 "ISO C forbids subscripting %<register%> array");
2860 else if (!lvalue_p (foo))
2861 pedwarn_c90 (loc, OPT_Wpedantic,
2862 "ISO C90 forbids subscripting non-lvalue "
2863 "array");
2866 type = TREE_TYPE (TREE_TYPE (array));
2867 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2868 /* Array ref is const/volatile if the array elements are
2869 or if the array is. */
2870 TREE_READONLY (rval)
2871 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2872 | TREE_READONLY (array));
2873 TREE_SIDE_EFFECTS (rval)
2874 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2875 | TREE_SIDE_EFFECTS (array));
2876 TREE_THIS_VOLATILE (rval)
2877 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2878 /* This was added by rms on 16 Nov 91.
2879 It fixes vol struct foo *a; a->elts[1]
2880 in an inline function.
2881 Hope it doesn't break something else. */
2882 | TREE_THIS_VOLATILE (array));
2883 ret = require_complete_type (loc, rval);
2884 protected_set_expr_location (ret, loc);
2885 if (non_lvalue)
2886 ret = non_lvalue_loc (loc, ret);
2887 return ret;
2889 else
2891 tree ar = default_conversion (array);
2893 if (ar == error_mark_node)
2894 return ar;
2896 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2897 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2899 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2900 index, false),
2901 RO_ARRAY_INDEXING);
2902 if (non_lvalue)
2903 ret = non_lvalue_loc (loc, ret);
2904 return ret;
2908 /* Build an external reference to identifier ID. FUN indicates
2909 whether this will be used for a function call. LOC is the source
2910 location of the identifier. This sets *TYPE to the type of the
2911 identifier, which is not the same as the type of the returned value
2912 for CONST_DECLs defined as enum constants. If the type of the
2913 identifier is not available, *TYPE is set to NULL. */
2914 tree
2915 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2917 tree ref;
2918 tree decl = lookup_name (id);
2920 /* In Objective-C, an instance variable (ivar) may be preferred to
2921 whatever lookup_name() found. */
2922 decl = objc_lookup_ivar (decl, id);
2924 *type = NULL;
2925 if (decl && decl != error_mark_node)
2927 ref = decl;
2928 *type = TREE_TYPE (ref);
2929 if (DECL_P (decl) && C_DECL_UNDERSPECIFIED (decl))
2930 error_at (loc, "underspecified %qD referenced in its initializer",
2931 decl);
2933 else if (fun)
2934 /* Implicit function declaration. */
2935 ref = implicitly_declare (loc, id);
2936 else if (decl == error_mark_node)
2937 /* Don't complain about something that's already been
2938 complained about. */
2939 return error_mark_node;
2940 else
2942 undeclared_variable (loc, id);
2943 return error_mark_node;
2946 if (TREE_TYPE (ref) == error_mark_node)
2947 return error_mark_node;
2949 if (TREE_UNAVAILABLE (ref))
2950 error_unavailable_use (ref, NULL_TREE);
2951 else if (TREE_DEPRECATED (ref))
2952 warn_deprecated_use (ref, NULL_TREE);
2954 /* Recursive call does not count as usage. */
2955 if (ref != current_function_decl)
2957 TREE_USED (ref) = 1;
2960 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2962 if (!in_sizeof && !in_typeof)
2963 C_DECL_USED (ref) = 1;
2964 else if (DECL_INITIAL (ref) == NULL_TREE
2965 && DECL_EXTERNAL (ref)
2966 && !TREE_PUBLIC (ref))
2967 record_maybe_used_decl (ref);
2970 if (TREE_CODE (ref) == CONST_DECL)
2972 used_types_insert (TREE_TYPE (ref));
2974 if (warn_cxx_compat
2975 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2976 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2978 warning_at (loc, OPT_Wc___compat,
2979 ("enum constant defined in struct or union "
2980 "is not visible in C++"));
2981 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2984 ref = DECL_INITIAL (ref);
2985 TREE_CONSTANT (ref) = 1;
2987 else if (current_function_decl != NULL_TREE
2988 && !DECL_FILE_SCOPE_P (current_function_decl)
2989 && (VAR_OR_FUNCTION_DECL_P (ref)
2990 || TREE_CODE (ref) == PARM_DECL))
2992 tree context = decl_function_context (ref);
2994 if (context != NULL_TREE && context != current_function_decl)
2995 DECL_NONLOCAL (ref) = 1;
2997 /* C99 6.7.4p3: An inline definition of a function with external
2998 linkage ... shall not contain a reference to an identifier with
2999 internal linkage. */
3000 else if (current_function_decl != NULL_TREE
3001 && DECL_DECLARED_INLINE_P (current_function_decl)
3002 && DECL_EXTERNAL (current_function_decl)
3003 && VAR_OR_FUNCTION_DECL_P (ref)
3004 && (!VAR_P (ref) || TREE_STATIC (ref))
3005 && ! TREE_PUBLIC (ref)
3006 && DECL_CONTEXT (ref) != current_function_decl)
3007 record_inline_static (loc, current_function_decl, ref,
3008 csi_internal);
3010 return ref;
3013 /* Record details of decls possibly used inside sizeof or typeof. */
3014 struct maybe_used_decl
3016 /* The decl. */
3017 tree decl;
3018 /* The level seen at (in_sizeof + in_typeof). */
3019 int level;
3020 /* The next one at this level or above, or NULL. */
3021 struct maybe_used_decl *next;
3024 static struct maybe_used_decl *maybe_used_decls;
3026 /* Record that DECL, an undefined static function reference seen
3027 inside sizeof or typeof, might be used if the operand of sizeof is
3028 a VLA type or the operand of typeof is a variably modified
3029 type. */
3031 static void
3032 record_maybe_used_decl (tree decl)
3034 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
3035 t->decl = decl;
3036 t->level = in_sizeof + in_typeof;
3037 t->next = maybe_used_decls;
3038 maybe_used_decls = t;
3041 /* Pop the stack of decls possibly used inside sizeof or typeof. If
3042 USED is false, just discard them. If it is true, mark them used
3043 (if no longer inside sizeof or typeof) or move them to the next
3044 level up (if still inside sizeof or typeof). */
3046 void
3047 pop_maybe_used (bool used)
3049 struct maybe_used_decl *p = maybe_used_decls;
3050 int cur_level = in_sizeof + in_typeof;
3051 while (p && p->level > cur_level)
3053 if (used)
3055 if (cur_level == 0)
3056 C_DECL_USED (p->decl) = 1;
3057 else
3058 p->level = cur_level;
3060 p = p->next;
3062 if (!used || cur_level == 0)
3063 maybe_used_decls = p;
3066 /* Return the result of sizeof applied to EXPR. */
3068 struct c_expr
3069 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
3071 struct c_expr ret;
3072 if (expr.value == error_mark_node)
3074 ret.value = error_mark_node;
3075 ret.original_code = ERROR_MARK;
3076 ret.original_type = NULL;
3077 ret.m_decimal = 0;
3078 pop_maybe_used (false);
3080 else
3082 bool expr_const_operands = true;
3084 if (TREE_CODE (expr.value) == PARM_DECL
3085 && C_ARRAY_PARAMETER (expr.value))
3087 auto_diagnostic_group d;
3088 if (warning_at (loc, OPT_Wsizeof_array_argument,
3089 "%<sizeof%> on array function parameter %qE will "
3090 "return size of %qT", expr.value,
3091 TREE_TYPE (expr.value)))
3092 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
3094 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
3095 &expr_const_operands);
3096 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
3097 c_last_sizeof_arg = expr.value;
3098 c_last_sizeof_loc = loc;
3099 ret.original_code = SIZEOF_EXPR;
3100 ret.original_type = NULL;
3101 ret.m_decimal = 0;
3102 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
3104 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
3105 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3106 folded_expr, ret.value);
3107 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3108 SET_EXPR_LOCATION (ret.value, loc);
3110 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3112 return ret;
3115 /* Return the result of sizeof applied to T, a structure for the type
3116 name passed to sizeof (rather than the type itself). LOC is the
3117 location of the original expression. */
3119 struct c_expr
3120 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3122 tree type;
3123 struct c_expr ret;
3124 tree type_expr = NULL_TREE;
3125 bool type_expr_const = true;
3126 type = groktypename (t, &type_expr, &type_expr_const);
3127 ret.value = c_sizeof (loc, type);
3128 c_last_sizeof_arg = type;
3129 c_last_sizeof_loc = loc;
3130 ret.original_code = SIZEOF_EXPR;
3131 ret.original_type = NULL;
3132 ret.m_decimal = 0;
3133 if (type == error_mark_node)
3135 ret.value = error_mark_node;
3136 ret.original_code = ERROR_MARK;
3138 else
3139 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3140 && C_TYPE_VARIABLE_SIZE (type))
3142 /* If the type is a [*] array, it is a VLA but is represented as
3143 having a size of zero. In such a case we must ensure that
3144 the result of sizeof does not get folded to a constant by
3145 c_fully_fold, because if the size is evaluated the result is
3146 not constant and so constraints on zero or negative size
3147 arrays must not be applied when this sizeof call is inside
3148 another array declarator. */
3149 if (!type_expr)
3150 type_expr = integer_zero_node;
3151 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3152 type_expr, ret.value);
3153 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3155 pop_maybe_used (type != error_mark_node
3156 ? C_TYPE_VARIABLE_SIZE (type) : false);
3157 return ret;
3160 /* Build a function call to function FUNCTION with parameters PARAMS.
3161 The function call is at LOC.
3162 PARAMS is a list--a chain of TREE_LIST nodes--in which the
3163 TREE_VALUE of each node is a parameter-expression.
3164 FUNCTION's data type may be a function type or a pointer-to-function. */
3166 tree
3167 build_function_call (location_t loc, tree function, tree params)
3169 vec<tree, va_gc> *v;
3170 tree ret;
3172 vec_alloc (v, list_length (params));
3173 for (; params; params = TREE_CHAIN (params))
3174 v->quick_push (TREE_VALUE (params));
3175 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3176 vec_free (v);
3177 return ret;
3180 /* Give a note about the location of the declaration of DECL. */
3182 static void
3183 inform_declaration (tree decl)
3185 if (decl && (TREE_CODE (decl) != FUNCTION_DECL
3186 || !DECL_IS_UNDECLARED_BUILTIN (decl)))
3187 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3190 /* Build a function call to function FUNCTION with parameters PARAMS.
3191 If FUNCTION is the result of resolving an overloaded target built-in,
3192 ORIG_FUNDECL is the original function decl, otherwise it is null.
3193 ORIGTYPES, if not NULL, is a vector of types; each element is
3194 either NULL or the original type of the corresponding element in
3195 PARAMS. The original type may differ from TREE_TYPE of the
3196 parameter for enums. FUNCTION's data type may be a function type
3197 or pointer-to-function. This function changes the elements of
3198 PARAMS. */
3200 tree
3201 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3202 tree function, vec<tree, va_gc> *params,
3203 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3205 tree fntype, fundecl = NULL_TREE;
3206 tree name = NULL_TREE, result;
3207 tree tem;
3208 int nargs;
3209 tree *argarray;
3212 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3213 STRIP_TYPE_NOPS (function);
3215 /* Convert anything with function type to a pointer-to-function. */
3216 if (TREE_CODE (function) == FUNCTION_DECL)
3218 name = DECL_NAME (function);
3220 if (flag_tm)
3221 tm_malloc_replacement (function);
3222 fundecl = function;
3223 if (!orig_fundecl)
3224 orig_fundecl = fundecl;
3225 /* Atomic functions have type checking/casting already done. They are
3226 often rewritten and don't match the original parameter list. */
3227 if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
3228 origtypes = NULL;
3230 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3231 function = function_to_pointer_conversion (loc, function);
3233 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3234 expressions, like those used for ObjC messenger dispatches. */
3235 if (params && !params->is_empty ())
3236 function = objc_rewrite_function_call (function, (*params)[0]);
3238 function = c_fully_fold (function, false, NULL);
3240 fntype = TREE_TYPE (function);
3242 if (TREE_CODE (fntype) == ERROR_MARK)
3243 return error_mark_node;
3245 if (!(TREE_CODE (fntype) == POINTER_TYPE
3246 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3248 if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
3249 error_at (loc,
3250 "called object %qE is not a function or function pointer",
3251 function);
3252 else if (DECL_P (function))
3254 error_at (loc,
3255 "called object %qD is not a function or function pointer",
3256 function);
3257 inform_declaration (function);
3259 else
3260 error_at (loc,
3261 "called object is not a function or function pointer");
3262 return error_mark_node;
3265 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3266 current_function_returns_abnormally = 1;
3268 /* fntype now gets the type of function pointed to. */
3269 fntype = TREE_TYPE (fntype);
3270 tree return_type = TREE_TYPE (fntype);
3272 /* Convert the parameters to the types declared in the
3273 function prototype, or apply default promotions. */
3275 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3276 origtypes, function, fundecl);
3277 if (nargs < 0)
3278 return error_mark_node;
3280 /* Check that the function is called through a compatible prototype.
3281 If it is not, warn. */
3282 if (CONVERT_EXPR_P (function)
3283 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3284 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3285 && !comptypes (fntype, TREE_TYPE (tem)))
3287 /* This situation leads to run-time undefined behavior. We can't,
3288 therefore, simply error unless we can prove that all possible
3289 executions of the program must execute the code. */
3290 warning_at (loc, 0, "function called through a non-compatible type");
3292 if (VOID_TYPE_P (return_type)
3293 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3294 pedwarn (loc, 0,
3295 "function with qualified void return type called");
3298 argarray = vec_safe_address (params);
3300 /* Check that arguments to builtin functions match the expectations. */
3301 if (fundecl
3302 && fndecl_built_in_p (fundecl)
3303 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3304 orig_fundecl, nargs, argarray))
3305 return error_mark_node;
3307 /* Check that the arguments to the function are valid. */
3308 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3309 nargs, argarray, &arg_loc);
3311 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
3312 && !VOID_TYPE_P (return_type))
3313 return_type = c_build_qualified_type (return_type, TYPE_UNQUALIFIED);
3314 if (name != NULL_TREE
3315 && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
3317 if (require_constant_value)
3318 result
3319 = fold_build_call_array_initializer_loc (loc, return_type,
3320 function, nargs, argarray);
3321 else
3322 result = fold_build_call_array_loc (loc, return_type,
3323 function, nargs, argarray);
3324 if (TREE_CODE (result) == NOP_EXPR
3325 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3326 STRIP_TYPE_NOPS (result);
3328 else
3329 result = build_call_array_loc (loc, return_type,
3330 function, nargs, argarray);
3331 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3332 later. */
3333 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3334 suppress_warning (result, OPT_Wnonnull);
3336 /* In this improbable scenario, a nested function returns a VM type.
3337 Create a TARGET_EXPR so that the call always has a LHS, much as
3338 what the C++ FE does for functions returning non-PODs. */
3339 if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (fntype)))
3341 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3342 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3343 NULL_TREE, NULL_TREE);
3346 if (VOID_TYPE_P (TREE_TYPE (result)))
3348 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3349 pedwarn (loc, 0,
3350 "function with qualified void return type called");
3351 return result;
3353 return require_complete_type (loc, result);
3356 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3358 tree
3359 c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
3360 tree function, vec<tree, va_gc> *params,
3361 vec<tree, va_gc> *origtypes)
3363 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3364 STRIP_TYPE_NOPS (function);
3366 /* Convert anything with function type to a pointer-to-function. */
3367 if (TREE_CODE (function) == FUNCTION_DECL)
3369 /* Implement type-directed function overloading for builtins.
3370 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3371 handle all the type checking. The result is a complete expression
3372 that implements this function call. */
3373 tree tem = resolve_overloaded_builtin (loc, function, params);
3374 if (tem)
3375 return tem;
3377 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3380 /* Helper for convert_arguments called to convert the VALue of argument
3381 number ARGNUM from ORIGTYPE to the corresponding parameter number
3382 PARMNUM and TYPE.
3383 PLOC is the location where the conversion is being performed.
3384 FUNCTION and FUNDECL are the same as in convert_arguments.
3385 VALTYPE is the original type of VAL before the conversion and,
3386 for EXCESS_PRECISION_EXPR, the operand of the expression.
3387 NPC is true if VAL represents the null pointer constant (VAL itself
3388 will have been folded to an integer constant).
3389 RNAME is the same as FUNCTION except in Objective C when it's
3390 the function selector.
3391 EXCESS_PRECISION is true when VAL was originally represented
3392 as EXCESS_PRECISION_EXPR.
3393 WARNOPT is the same as in convert_for_assignment. */
3395 static tree
3396 convert_argument (location_t ploc, tree function, tree fundecl,
3397 tree type, tree origtype, tree val, tree valtype,
3398 bool npc, tree rname, int parmnum, int argnum,
3399 bool excess_precision, int warnopt)
3401 /* Formal parm type is specified by a function prototype. */
3403 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3405 error_at (ploc, "type of formal parameter %d is incomplete",
3406 parmnum + 1);
3407 return val;
3410 /* Optionally warn about conversions that differ from the default
3411 conversions. */
3412 if (warn_traditional_conversion || warn_traditional)
3414 unsigned int formal_prec = TYPE_PRECISION (type);
3416 if (INTEGRAL_TYPE_P (type)
3417 && TREE_CODE (valtype) == REAL_TYPE)
3418 warning_at (ploc, OPT_Wtraditional_conversion,
3419 "passing argument %d of %qE as integer rather "
3420 "than floating due to prototype",
3421 argnum, rname);
3422 if (INTEGRAL_TYPE_P (type)
3423 && TREE_CODE (valtype) == COMPLEX_TYPE)
3424 warning_at (ploc, OPT_Wtraditional_conversion,
3425 "passing argument %d of %qE as integer rather "
3426 "than complex due to prototype",
3427 argnum, rname);
3428 else if (TREE_CODE (type) == COMPLEX_TYPE
3429 && TREE_CODE (valtype) == REAL_TYPE)
3430 warning_at (ploc, OPT_Wtraditional_conversion,
3431 "passing argument %d of %qE as complex rather "
3432 "than floating due to prototype",
3433 argnum, rname);
3434 else if (TREE_CODE (type) == REAL_TYPE
3435 && INTEGRAL_TYPE_P (valtype))
3436 warning_at (ploc, OPT_Wtraditional_conversion,
3437 "passing argument %d of %qE as floating rather "
3438 "than integer due to prototype",
3439 argnum, rname);
3440 else if (TREE_CODE (type) == COMPLEX_TYPE
3441 && INTEGRAL_TYPE_P (valtype))
3442 warning_at (ploc, OPT_Wtraditional_conversion,
3443 "passing argument %d of %qE as complex rather "
3444 "than integer due to prototype",
3445 argnum, rname);
3446 else if (TREE_CODE (type) == REAL_TYPE
3447 && TREE_CODE (valtype) == COMPLEX_TYPE)
3448 warning_at (ploc, OPT_Wtraditional_conversion,
3449 "passing argument %d of %qE as floating rather "
3450 "than complex due to prototype",
3451 argnum, rname);
3452 /* ??? At some point, messages should be written about
3453 conversions between complex types, but that's too messy
3454 to do now. */
3455 else if (TREE_CODE (type) == REAL_TYPE
3456 && TREE_CODE (valtype) == REAL_TYPE)
3458 /* Warn if any argument is passed as `float',
3459 since without a prototype it would be `double'. */
3460 if (formal_prec == TYPE_PRECISION (float_type_node)
3461 && type != dfloat32_type_node)
3462 warning_at (ploc, 0,
3463 "passing argument %d of %qE as %<float%> "
3464 "rather than %<double%> due to prototype",
3465 argnum, rname);
3467 /* Warn if mismatch between argument and prototype
3468 for decimal float types. Warn of conversions with
3469 binary float types and of precision narrowing due to
3470 prototype. */
3471 else if (type != valtype
3472 && (type == dfloat32_type_node
3473 || type == dfloat64_type_node
3474 || type == dfloat128_type_node
3475 || valtype == dfloat32_type_node
3476 || valtype == dfloat64_type_node
3477 || valtype == dfloat128_type_node)
3478 && (formal_prec
3479 <= TYPE_PRECISION (valtype)
3480 || (type == dfloat128_type_node
3481 && (valtype
3482 != dfloat64_type_node
3483 && (valtype
3484 != dfloat32_type_node)))
3485 || (type == dfloat64_type_node
3486 && (valtype
3487 != dfloat32_type_node))))
3488 warning_at (ploc, 0,
3489 "passing argument %d of %qE as %qT "
3490 "rather than %qT due to prototype",
3491 argnum, rname, type, valtype);
3494 /* Detect integer changing in width or signedness.
3495 These warnings are only activated with
3496 -Wtraditional-conversion, not with -Wtraditional. */
3497 else if (warn_traditional_conversion
3498 && INTEGRAL_TYPE_P (type)
3499 && INTEGRAL_TYPE_P (valtype))
3501 tree would_have_been = default_conversion (val);
3502 tree type1 = TREE_TYPE (would_have_been);
3504 if (val == error_mark_node)
3505 /* VAL could have been of incomplete type. */;
3506 else if (TREE_CODE (type) == ENUMERAL_TYPE
3507 && (TYPE_MAIN_VARIANT (type)
3508 == TYPE_MAIN_VARIANT (valtype)))
3509 /* No warning if function asks for enum
3510 and the actual arg is that enum type. */
3512 else if (formal_prec != TYPE_PRECISION (type1))
3513 warning_at (ploc, OPT_Wtraditional_conversion,
3514 "passing argument %d of %qE "
3515 "with different width due to prototype",
3516 argnum, rname);
3517 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3519 /* Don't complain if the formal parameter type
3520 is an enum, because we can't tell now whether
3521 the value was an enum--even the same enum. */
3522 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3524 else if (TREE_CODE (val) == INTEGER_CST
3525 && int_fits_type_p (val, type))
3526 /* Change in signedness doesn't matter
3527 if a constant value is unaffected. */
3529 /* If the value is extended from a narrower
3530 unsigned type, it doesn't matter whether we
3531 pass it as signed or unsigned; the value
3532 certainly is the same either way. */
3533 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3534 && TYPE_UNSIGNED (valtype))
3536 else if (TYPE_UNSIGNED (type))
3537 warning_at (ploc, OPT_Wtraditional_conversion,
3538 "passing argument %d of %qE "
3539 "as unsigned due to prototype",
3540 argnum, rname);
3541 else
3542 warning_at (ploc, OPT_Wtraditional_conversion,
3543 "passing argument %d of %qE "
3544 "as signed due to prototype",
3545 argnum, rname);
3549 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3550 sake of better warnings from convert_and_check. */
3551 if (excess_precision)
3552 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3554 tree parmval = convert_for_assignment (ploc, ploc, type,
3555 val, origtype, ic_argpass,
3556 npc, fundecl, function,
3557 parmnum + 1, warnopt);
3559 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3560 && INTEGRAL_TYPE_P (type)
3561 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3562 parmval = default_conversion (parmval);
3564 return parmval;
3567 /* Convert the argument expressions in the vector VALUES
3568 to the types in the list TYPELIST.
3570 If TYPELIST is exhausted, or when an element has NULL as its type,
3571 perform the default conversions.
3573 ORIGTYPES is the original types of the expressions in VALUES. This
3574 holds the type of enum values which have been converted to integral
3575 types. It may be NULL.
3577 FUNCTION is a tree for the called function. It is used only for
3578 error messages, where it is formatted with %qE.
3580 This is also where warnings about wrong number of args are generated.
3582 ARG_LOC are locations of function arguments (if any).
3584 Returns the actual number of arguments processed (which may be less
3585 than the length of VALUES in some error situations), or -1 on
3586 failure. */
3588 static int
3589 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3590 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3591 tree function, tree fundecl)
3593 unsigned int parmnum;
3594 bool error_args = false;
3595 const bool type_generic = fundecl
3596 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3597 bool type_generic_remove_excess_precision = false;
3598 bool type_generic_overflow_p = false;
3599 tree selector;
3601 /* Change pointer to function to the function itself for
3602 diagnostics. */
3603 if (TREE_CODE (function) == ADDR_EXPR
3604 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3605 function = TREE_OPERAND (function, 0);
3607 /* Handle an ObjC selector specially for diagnostics. */
3608 selector = objc_message_selector ();
3610 /* For a call to a built-in function declared without a prototype,
3611 set to the built-in function's argument list. */
3612 tree builtin_typelist = NULL_TREE;
3614 /* For type-generic built-in functions, determine whether excess
3615 precision should be removed (classification) or not
3616 (comparison). */
3617 if (fundecl
3618 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3620 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3621 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3623 /* For a call to a built-in function declared without a prototype
3624 use the types of the parameters of the internal built-in to
3625 match those of the arguments to. */
3626 if (tree bdecl = builtin_decl_explicit (code))
3627 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3630 /* For type-generic built-in functions, determine whether excess
3631 precision should be removed (classification) or not
3632 (comparison). */
3633 if (type_generic)
3634 switch (code)
3636 case BUILT_IN_ISFINITE:
3637 case BUILT_IN_ISINF:
3638 case BUILT_IN_ISINF_SIGN:
3639 case BUILT_IN_ISNAN:
3640 case BUILT_IN_ISNORMAL:
3641 case BUILT_IN_ISSIGNALING:
3642 case BUILT_IN_FPCLASSIFY:
3643 type_generic_remove_excess_precision = true;
3644 break;
3646 case BUILT_IN_ADD_OVERFLOW_P:
3647 case BUILT_IN_SUB_OVERFLOW_P:
3648 case BUILT_IN_MUL_OVERFLOW_P:
3649 /* The last argument of these type-generic builtins
3650 should not be promoted. */
3651 type_generic_overflow_p = true;
3652 break;
3654 default:
3655 break;
3659 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3660 individual converted arguments. */
3662 tree typetail, builtin_typetail, val;
3663 for (typetail = typelist,
3664 builtin_typetail = builtin_typelist,
3665 parmnum = 0;
3666 values && values->iterate (parmnum, &val);
3667 ++parmnum)
3669 /* The type of the function parameter (if it was declared with one). */
3670 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3671 /* The type of the built-in function parameter (if the function
3672 is a built-in). Used to detect type incompatibilities in
3673 calls to built-ins declared without a prototype. */
3674 tree builtin_type = (builtin_typetail
3675 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3676 /* The original type of the argument being passed to the function. */
3677 tree valtype = TREE_TYPE (val);
3678 /* The called function (or function selector in Objective C). */
3679 tree rname = function;
3680 int argnum = parmnum + 1;
3681 const char *invalid_func_diag;
3682 /* Set for EXCESS_PRECISION_EXPR arguments. */
3683 bool excess_precision = false;
3684 /* The value of the argument after conversion to the type
3685 of the function parameter it is passed to. */
3686 tree parmval;
3687 /* Some __atomic_* builtins have additional hidden argument at
3688 position 0. */
3689 location_t ploc
3690 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3691 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3692 : input_location;
3694 if (type == void_type_node)
3696 if (selector)
3697 error_at (loc, "too many arguments to method %qE", selector);
3698 else
3699 error_at (loc, "too many arguments to function %qE", function);
3700 inform_declaration (fundecl);
3701 return error_args ? -1 : (int) parmnum;
3704 if (builtin_type == void_type_node)
3706 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3707 "too many arguments to built-in function %qE "
3708 "expecting %d", function, parmnum))
3709 inform_declaration (fundecl);
3710 builtin_typetail = NULL_TREE;
3713 if (selector && argnum > 2)
3715 rname = selector;
3716 argnum -= 2;
3719 /* Determine if VAL is a null pointer constant before folding it. */
3720 bool npc = null_pointer_constant_p (val);
3722 /* If there is excess precision and a prototype, convert once to
3723 the required type rather than converting via the semantic
3724 type. Likewise without a prototype a float value represented
3725 as long double should be converted once to double. But for
3726 type-generic classification functions excess precision must
3727 be removed here. */
3728 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3729 && (type || !type_generic || !type_generic_remove_excess_precision))
3731 val = TREE_OPERAND (val, 0);
3732 excess_precision = true;
3734 val = c_fully_fold (val, false, NULL);
3735 STRIP_TYPE_NOPS (val);
3737 val = require_complete_type (ploc, val);
3739 /* Some floating-point arguments must be promoted to double when
3740 no type is specified by a prototype. This applies to
3741 arguments of type float, and to architecture-specific types
3742 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3743 bool promote_float_arg = false;
3744 if (type == NULL_TREE
3745 && TREE_CODE (valtype) == REAL_TYPE
3746 && (TYPE_PRECISION (valtype)
3747 <= TYPE_PRECISION (double_type_node))
3748 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3749 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3750 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3752 /* Promote this argument, unless it has a _FloatN or
3753 _FloatNx type. */
3754 promote_float_arg = true;
3755 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3756 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3758 promote_float_arg = false;
3759 break;
3761 /* Don't promote __bf16 either. */
3762 if (TYPE_MAIN_VARIANT (valtype) == bfloat16_type_node)
3763 promote_float_arg = false;
3766 if (type != NULL_TREE)
3768 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3769 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3770 val, valtype, npc, rname, parmnum, argnum,
3771 excess_precision, 0);
3773 else if (promote_float_arg)
3775 if (type_generic)
3776 parmval = val;
3777 else
3779 /* Convert `float' to `double'. */
3780 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3781 warning_at (ploc, OPT_Wdouble_promotion,
3782 "implicit conversion from %qT to %qT when passing "
3783 "argument to function",
3784 valtype, double_type_node);
3785 parmval = convert (double_type_node, val);
3788 else if ((excess_precision && !type_generic)
3789 || (type_generic_overflow_p && parmnum == 2))
3790 /* A "double" argument with excess precision being passed
3791 without a prototype or in variable arguments.
3792 The last argument of __builtin_*_overflow_p should not be
3793 promoted. */
3794 parmval = convert (valtype, val);
3795 else if ((invalid_func_diag =
3796 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3798 error (invalid_func_diag);
3799 return -1;
3801 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3803 return -1;
3805 else
3806 /* Convert `short' and `char' to full-size `int'. */
3807 parmval = default_conversion (val);
3809 (*values)[parmnum] = parmval;
3810 if (parmval == error_mark_node)
3811 error_args = true;
3813 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3815 /* For a call to a built-in function declared without a prototype,
3816 perform the conversions from the argument to the expected type
3817 but issue warnings rather than errors for any mismatches.
3818 Ignore the converted argument and use the PARMVAL obtained
3819 above by applying default conversions instead. */
3820 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3821 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3822 val, valtype, npc, rname, parmnum, argnum,
3823 excess_precision,
3824 OPT_Wbuiltin_declaration_mismatch);
3827 if (typetail)
3828 typetail = TREE_CHAIN (typetail);
3830 if (builtin_typetail)
3831 builtin_typetail = TREE_CHAIN (builtin_typetail);
3834 gcc_assert (parmnum == vec_safe_length (values));
3836 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3838 error_at (loc, "too few arguments to function %qE", function);
3839 inform_declaration (fundecl);
3840 return -1;
3843 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3845 unsigned nargs = parmnum;
3846 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3847 ++nargs;
3849 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3850 "too few arguments to built-in function %qE "
3851 "expecting %u", function, nargs - 1))
3852 inform_declaration (fundecl);
3855 return error_args ? -1 : (int) parmnum;
3858 /* This is the entry point used by the parser to build unary operators
3859 in the input. CODE, a tree_code, specifies the unary operator, and
3860 ARG is the operand. For unary plus, the C parser currently uses
3861 CONVERT_EXPR for code.
3863 LOC is the location to use for the tree generated.
3866 struct c_expr
3867 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3869 struct c_expr result;
3871 result.original_code = code;
3872 result.original_type = NULL;
3873 result.m_decimal = 0;
3875 if (reject_gcc_builtin (arg.value))
3877 result.value = error_mark_node;
3879 else
3881 result.value = build_unary_op (loc, code, arg.value, false);
3883 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3884 overflow_warning (loc, result.value, arg.value);
3887 /* We are typically called when parsing a prefix token at LOC acting on
3888 ARG. Reflect this by updating the source range of the result to
3889 start at LOC and end at the end of ARG. */
3890 set_c_expr_source_range (&result,
3891 loc, arg.get_finish ());
3893 return result;
3896 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3898 bool
3899 char_type_p (tree type)
3901 return (type == char_type_node
3902 || type == unsigned_char_type_node
3903 || type == signed_char_type_node
3904 || type == char16_type_node
3905 || type == char32_type_node);
3908 /* This is the entry point used by the parser to build binary operators
3909 in the input. CODE, a tree_code, specifies the binary operator, and
3910 ARG1 and ARG2 are the operands. In addition to constructing the
3911 expression, we check for operands that were written with other binary
3912 operators in a way that is likely to confuse the user.
3914 LOCATION is the location of the binary operator. */
3916 struct c_expr
3917 parser_build_binary_op (location_t location, enum tree_code code,
3918 struct c_expr arg1, struct c_expr arg2)
3920 struct c_expr result;
3921 result.m_decimal = 0;
3923 enum tree_code code1 = arg1.original_code;
3924 enum tree_code code2 = arg2.original_code;
3925 tree type1 = (arg1.original_type
3926 ? arg1.original_type
3927 : TREE_TYPE (arg1.value));
3928 tree type2 = (arg2.original_type
3929 ? arg2.original_type
3930 : TREE_TYPE (arg2.value));
3932 result.value = build_binary_op (location, code,
3933 arg1.value, arg2.value, true);
3934 result.original_code = code;
3935 result.original_type = NULL;
3936 result.m_decimal = 0;
3938 if (TREE_CODE (result.value) == ERROR_MARK)
3940 set_c_expr_source_range (&result,
3941 arg1.get_start (),
3942 arg2.get_finish ());
3943 return result;
3946 if (location != UNKNOWN_LOCATION)
3947 protected_set_expr_location (result.value, location);
3949 set_c_expr_source_range (&result,
3950 arg1.get_start (),
3951 arg2.get_finish ());
3953 /* Check for cases such as x+y<<z which users are likely
3954 to misinterpret. */
3955 if (warn_parentheses)
3956 warn_about_parentheses (location, code, code1, arg1.value, code2,
3957 arg2.value);
3959 if (warn_logical_op)
3960 warn_logical_operator (location, code, TREE_TYPE (result.value),
3961 code1, arg1.value, code2, arg2.value);
3963 if (warn_tautological_compare)
3965 tree lhs = arg1.value;
3966 tree rhs = arg2.value;
3967 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3969 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3970 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3971 lhs = NULL_TREE;
3972 else
3973 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3975 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3977 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3978 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3979 rhs = NULL_TREE;
3980 else
3981 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3983 if (lhs != NULL_TREE && rhs != NULL_TREE)
3984 warn_tautological_cmp (location, code, lhs, rhs);
3987 if (warn_logical_not_paren
3988 && TREE_CODE_CLASS (code) == tcc_comparison
3989 && code1 == TRUTH_NOT_EXPR
3990 && code2 != TRUTH_NOT_EXPR
3991 /* Avoid warning for !!x == y. */
3992 && (TREE_CODE (arg1.value) != NE_EXPR
3993 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3995 /* Avoid warning for !b == y where b has _Bool type. */
3996 tree t = integer_zero_node;
3997 if (TREE_CODE (arg1.value) == EQ_EXPR
3998 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3999 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
4001 t = TREE_OPERAND (arg1.value, 0);
4004 if (TREE_TYPE (t) != integer_type_node)
4005 break;
4006 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
4007 t = C_MAYBE_CONST_EXPR_EXPR (t);
4008 else if (CONVERT_EXPR_P (t))
4009 t = TREE_OPERAND (t, 0);
4010 else
4011 break;
4013 while (1);
4015 if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t)))
4016 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
4019 /* Warn about comparisons against string literals, with the exception
4020 of testing for equality or inequality of a string literal with NULL. */
4021 if (code == EQ_EXPR || code == NE_EXPR)
4023 if ((code1 == STRING_CST
4024 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
4025 || (code2 == STRING_CST
4026 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
4027 warning_at (location, OPT_Waddress,
4028 "comparison with string literal results in unspecified behavior");
4029 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
4030 if (POINTER_TYPE_P (type1)
4031 && null_pointer_constant_p (arg2.value)
4032 && char_type_p (type2))
4034 auto_diagnostic_group d;
4035 if (warning_at (location, OPT_Wpointer_compare,
4036 "comparison between pointer and zero character "
4037 "constant"))
4038 inform (arg1.get_start (),
4039 "did you mean to dereference the pointer?");
4041 else if (POINTER_TYPE_P (type2)
4042 && null_pointer_constant_p (arg1.value)
4043 && char_type_p (type1))
4045 auto_diagnostic_group d;
4046 if (warning_at (location, OPT_Wpointer_compare,
4047 "comparison between pointer and zero character "
4048 "constant"))
4049 inform (arg2.get_start (),
4050 "did you mean to dereference the pointer?");
4053 else if (TREE_CODE_CLASS (code) == tcc_comparison
4054 && (code1 == STRING_CST || code2 == STRING_CST))
4055 warning_at (location, OPT_Waddress,
4056 "comparison with string literal results in unspecified "
4057 "behavior");
4059 if (warn_array_compare
4060 && TREE_CODE_CLASS (code) == tcc_comparison
4061 && TREE_CODE (type1) == ARRAY_TYPE
4062 && TREE_CODE (type2) == ARRAY_TYPE)
4063 do_warn_array_compare (location, code, arg1.value, arg2.value);
4065 if (TREE_OVERFLOW_P (result.value)
4066 && !TREE_OVERFLOW_P (arg1.value)
4067 && !TREE_OVERFLOW_P (arg2.value))
4068 overflow_warning (location, result.value);
4070 /* Warn about comparisons of different enum types. */
4071 if (warn_enum_compare
4072 && TREE_CODE_CLASS (code) == tcc_comparison
4073 && TREE_CODE (type1) == ENUMERAL_TYPE
4074 && TREE_CODE (type2) == ENUMERAL_TYPE
4075 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
4076 warning_at (location, OPT_Wenum_compare,
4077 "comparison between %qT and %qT",
4078 type1, type2);
4080 if (warn_xor_used_as_pow
4081 && code == BIT_XOR_EXPR
4082 && arg1.m_decimal
4083 && arg2.m_decimal)
4084 check_for_xor_used_as_pow (arg1.get_location (), arg1.value,
4085 location,
4086 arg2.value);
4088 return result;
4091 /* Return a tree for the difference of pointers OP0 and OP1.
4092 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
4093 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
4095 static tree
4096 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
4098 tree restype = ptrdiff_type_node;
4099 tree result, inttype;
4101 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
4102 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
4103 tree target_type = TREE_TYPE (TREE_TYPE (op0));
4104 tree orig_op0 = op0;
4105 tree orig_op1 = op1;
4107 /* If the operands point into different address spaces, we need to
4108 explicitly convert them to pointers into the common address space
4109 before we can subtract the numerical address values. */
4110 if (as0 != as1)
4112 addr_space_t as_common;
4113 tree common_type;
4115 /* Determine the common superset address space. This is guaranteed
4116 to exist because the caller verified that comp_target_types
4117 returned non-zero. */
4118 if (!addr_space_superset (as0, as1, &as_common))
4119 gcc_unreachable ();
4121 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
4122 op0 = convert (common_type, op0);
4123 op1 = convert (common_type, op1);
4126 /* Determine integer type result of the subtraction. This will usually
4127 be the same as the result type (ptrdiff_t), but may need to be a wider
4128 type if pointers for the address space are wider than ptrdiff_t. */
4129 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
4130 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
4131 else
4132 inttype = restype;
4134 if (TREE_CODE (target_type) == VOID_TYPE)
4135 pedwarn (loc, OPT_Wpointer_arith,
4136 "pointer of type %<void *%> used in subtraction");
4137 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4138 pedwarn (loc, OPT_Wpointer_arith,
4139 "pointer to a function used in subtraction");
4141 if (current_function_decl != NULL_TREE
4142 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
4144 op0 = save_expr (op0);
4145 op1 = save_expr (op1);
4147 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
4148 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
4151 /* First do the subtraction, then build the divide operator
4152 and only convert at the very end.
4153 Do not do default conversions in case restype is a short type. */
4155 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4156 pointers. If some platform cannot provide that, or has a larger
4157 ptrdiff_type to support differences larger than half the address
4158 space, cast the pointers to some larger integer type and do the
4159 computations in that type. */
4160 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4161 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4162 convert (inttype, op1), false);
4163 else
4165 /* Cast away qualifiers. */
4166 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4167 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4168 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4171 /* This generates an error if op1 is pointer to incomplete type. */
4172 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4173 error_at (loc, "arithmetic on pointer to an incomplete type");
4174 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4175 TREE_TYPE (TREE_TYPE (orig_op0))))
4176 verify_type_context (loc, TCTX_POINTER_ARITH,
4177 TREE_TYPE (TREE_TYPE (orig_op1)));
4179 op1 = c_size_in_bytes (target_type);
4181 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4182 error_at (loc, "arithmetic on pointer to an empty aggregate");
4184 /* Divide by the size, in easiest possible way. */
4185 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4186 op0, convert (inttype, op1));
4188 /* Convert to final result type if necessary. */
4189 return convert (restype, result);
4192 /* Expand atomic compound assignments into an appropriate sequence as
4193 specified by the C11 standard section 6.5.16.2.
4195 _Atomic T1 E1
4196 T2 E2
4197 E1 op= E2
4199 This sequence is used for all types for which these operations are
4200 supported.
4202 In addition, built-in versions of the 'fe' prefixed routines may
4203 need to be invoked for floating point (real, complex or vector) when
4204 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4206 T1 newval;
4207 T1 old;
4208 T1 *addr
4209 T2 val
4210 fenv_t fenv
4212 addr = &E1;
4213 val = (E2);
4214 __atomic_load (addr, &old, SEQ_CST);
4215 feholdexcept (&fenv);
4216 loop:
4217 newval = old op val;
4218 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4219 SEQ_CST))
4220 goto done;
4221 feclearexcept (FE_ALL_EXCEPT);
4222 goto loop:
4223 done:
4224 feupdateenv (&fenv);
4226 The compiler will issue the __atomic_fetch_* built-in when possible,
4227 otherwise it will generate the generic form of the atomic operations.
4228 This requires temp(s) and has their address taken. The atomic processing
4229 is smart enough to figure out when the size of an object can utilize
4230 a lock-free version, and convert the built-in call to the appropriate
4231 lock-free routine. The optimizers will then dispose of any temps that
4232 are no longer required, and lock-free implementations are utilized as
4233 long as there is target support for the required size.
4235 If the operator is NOP_EXPR, then this is a simple assignment, and
4236 an __atomic_store is issued to perform the assignment rather than
4237 the above loop. */
4239 /* Build an atomic assignment at LOC, expanding into the proper
4240 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4241 the result of the operation, unless RETURN_OLD_P, in which case
4242 return the old value of LHS (this is only for postincrement and
4243 postdecrement). */
4245 static tree
4246 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4247 tree rhs, bool return_old_p)
4249 tree fndecl, func_call;
4250 vec<tree, va_gc> *params;
4251 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4252 tree old, old_addr;
4253 tree compound_stmt = NULL_TREE;
4254 tree stmt, goto_stmt;
4255 tree loop_label, loop_decl, done_label, done_decl;
4257 tree lhs_type = TREE_TYPE (lhs);
4258 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4259 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4260 tree rhs_semantic_type = TREE_TYPE (rhs);
4261 tree nonatomic_rhs_semantic_type;
4262 tree rhs_type;
4264 gcc_assert (TYPE_ATOMIC (lhs_type));
4266 if (return_old_p)
4267 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4269 /* Allocate enough vector items for a compare_exchange. */
4270 vec_alloc (params, 6);
4272 /* Create a compound statement to hold the sequence of statements
4273 with a loop. */
4274 if (modifycode != NOP_EXPR)
4276 compound_stmt = c_begin_compound_stmt (false);
4278 /* For consistency with build_modify_expr on non-_Atomic,
4279 mark the lhs as read. Also, it would be very hard to match
4280 such expressions in mark_exp_read. */
4281 mark_exp_read (lhs);
4284 /* Remove any excess precision (which is only present here in the
4285 case of compound assignments). */
4286 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4288 gcc_assert (modifycode != NOP_EXPR);
4289 rhs = TREE_OPERAND (rhs, 0);
4291 rhs_type = TREE_TYPE (rhs);
4293 /* Fold the RHS if it hasn't already been folded. */
4294 if (modifycode != NOP_EXPR)
4295 rhs = c_fully_fold (rhs, false, NULL);
4297 /* Remove the qualifiers for the rest of the expressions and create
4298 the VAL temp variable to hold the RHS. */
4299 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4300 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4301 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4302 TYPE_UNQUALIFIED);
4303 val = create_tmp_var_raw (nonatomic_rhs_type);
4304 TREE_ADDRESSABLE (val) = 1;
4305 suppress_warning (val);
4306 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4307 NULL_TREE);
4308 TREE_SIDE_EFFECTS (rhs) = 1;
4309 SET_EXPR_LOCATION (rhs, loc);
4310 if (modifycode != NOP_EXPR)
4311 add_stmt (rhs);
4313 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4314 an atomic_store. */
4315 if (modifycode == NOP_EXPR)
4317 compound_stmt = rhs;
4318 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4319 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4320 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4321 params->quick_push (lhs_addr);
4322 params->quick_push (rhs);
4323 params->quick_push (seq_cst);
4324 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4326 compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4327 compound_stmt, func_call);
4329 /* VAL is the value which was stored, return a COMPOUND_STMT of
4330 the statement and that value. */
4331 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4334 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4335 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4336 isn't applicable for such builtins. ??? Do we want to handle enums? */
4337 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4338 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4340 built_in_function fncode;
4341 switch (modifycode)
4343 case PLUS_EXPR:
4344 case POINTER_PLUS_EXPR:
4345 fncode = (return_old_p
4346 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4347 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4348 break;
4349 case MINUS_EXPR:
4350 fncode = (return_old_p
4351 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4352 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4353 break;
4354 case BIT_AND_EXPR:
4355 fncode = (return_old_p
4356 ? BUILT_IN_ATOMIC_FETCH_AND_N
4357 : BUILT_IN_ATOMIC_AND_FETCH_N);
4358 break;
4359 case BIT_IOR_EXPR:
4360 fncode = (return_old_p
4361 ? BUILT_IN_ATOMIC_FETCH_OR_N
4362 : BUILT_IN_ATOMIC_OR_FETCH_N);
4363 break;
4364 case BIT_XOR_EXPR:
4365 fncode = (return_old_p
4366 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4367 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4368 break;
4369 default:
4370 goto cas_loop;
4373 /* We can only use "_1" through "_16" variants of the atomic fetch
4374 built-ins. */
4375 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4376 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4377 goto cas_loop;
4379 /* If this is a pointer type, we need to multiply by the size of
4380 the pointer target type. */
4381 if (POINTER_TYPE_P (lhs_type))
4383 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4384 /* ??? This would introduce -Wdiscarded-qualifiers
4385 warning: __atomic_fetch_* expect volatile void *
4386 type as the first argument. (Assignments between
4387 atomic and non-atomic objects are OK.) */
4388 || TYPE_RESTRICT (lhs_type))
4389 goto cas_loop;
4390 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4391 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4392 convert (ptrdiff_type_node, rhs),
4393 convert (ptrdiff_type_node, sz));
4396 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4397 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4398 fndecl = builtin_decl_explicit (fncode);
4399 params->quick_push (lhs_addr);
4400 params->quick_push (rhs);
4401 params->quick_push (seq_cst);
4402 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4404 newval = create_tmp_var_raw (nonatomic_lhs_type);
4405 TREE_ADDRESSABLE (newval) = 1;
4406 suppress_warning (newval);
4407 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4408 NULL_TREE, NULL_TREE);
4409 SET_EXPR_LOCATION (rhs, loc);
4410 add_stmt (rhs);
4412 /* Finish the compound statement. */
4413 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4415 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4416 the statement and that value. */
4417 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4420 cas_loop:
4421 /* Create the variables and labels required for the op= form. */
4422 old = create_tmp_var_raw (nonatomic_lhs_type);
4423 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4424 TREE_ADDRESSABLE (old) = 1;
4425 suppress_warning (old);
4427 newval = create_tmp_var_raw (nonatomic_lhs_type);
4428 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4429 TREE_ADDRESSABLE (newval) = 1;
4430 suppress_warning (newval);
4432 loop_decl = create_artificial_label (loc);
4433 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4435 done_decl = create_artificial_label (loc);
4436 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4438 /* __atomic_load (addr, &old, SEQ_CST). */
4439 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4440 params->quick_push (lhs_addr);
4441 params->quick_push (old_addr);
4442 params->quick_push (seq_cst);
4443 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4444 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4445 NULL_TREE);
4446 add_stmt (old);
4447 params->truncate (0);
4449 /* Create the expressions for floating-point environment
4450 manipulation, if required. */
4451 bool need_fenv = (flag_trapping_math
4452 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4453 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4454 if (need_fenv)
4455 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4457 if (hold_call)
4458 add_stmt (hold_call);
4460 /* loop: */
4461 add_stmt (loop_label);
4463 /* newval = old + val; */
4464 if (rhs_type != rhs_semantic_type)
4465 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4466 rhs = build_binary_op (loc, modifycode, old, val, true);
4467 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4469 tree eptype = TREE_TYPE (rhs);
4470 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4471 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4473 else
4474 rhs = c_fully_fold (rhs, false, NULL);
4475 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4476 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4477 NULL_TREE, 0);
4478 if (rhs != error_mark_node)
4480 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4481 NULL_TREE);
4482 SET_EXPR_LOCATION (rhs, loc);
4483 add_stmt (rhs);
4486 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4487 goto done; */
4488 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4489 params->quick_push (lhs_addr);
4490 params->quick_push (old_addr);
4491 params->quick_push (newval_addr);
4492 params->quick_push (integer_zero_node);
4493 params->quick_push (seq_cst);
4494 params->quick_push (seq_cst);
4495 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4497 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4498 SET_EXPR_LOCATION (goto_stmt, loc);
4500 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4501 SET_EXPR_LOCATION (stmt, loc);
4502 add_stmt (stmt);
4504 if (clear_call)
4505 add_stmt (clear_call);
4507 /* goto loop; */
4508 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4509 SET_EXPR_LOCATION (goto_stmt, loc);
4510 add_stmt (goto_stmt);
4512 /* done: */
4513 add_stmt (done_label);
4515 if (update_call)
4516 add_stmt (update_call);
4518 /* Finish the compound statement. */
4519 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4521 /* NEWVAL is the value that was successfully stored, return a
4522 COMPOUND_EXPR of the statement and the appropriate value. */
4523 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4524 return_old_p ? old : newval);
4527 /* Construct and perhaps optimize a tree representation
4528 for a unary operation. CODE, a tree_code, specifies the operation
4529 and XARG is the operand.
4530 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4531 promotions (such as from short to int).
4532 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4533 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4534 to pointers in C99.
4536 LOCATION is the location of the operator. */
4538 tree
4539 build_unary_op (location_t location, enum tree_code code, tree xarg,
4540 bool noconvert)
4542 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4543 tree arg = xarg;
4544 tree argtype = NULL_TREE;
4545 enum tree_code typecode;
4546 tree val;
4547 tree ret = error_mark_node;
4548 tree eptype = NULL_TREE;
4549 const char *invalid_op_diag;
4550 bool int_operands;
4552 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4553 if (int_operands)
4554 arg = remove_c_maybe_const_expr (arg);
4556 if (code != ADDR_EXPR)
4557 arg = require_complete_type (location, arg);
4559 typecode = TREE_CODE (TREE_TYPE (arg));
4560 if (typecode == ERROR_MARK)
4561 return error_mark_node;
4562 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4563 typecode = INTEGER_TYPE;
4565 if ((invalid_op_diag
4566 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4568 error_at (location, invalid_op_diag);
4569 return error_mark_node;
4572 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4574 eptype = TREE_TYPE (arg);
4575 arg = TREE_OPERAND (arg, 0);
4578 switch (code)
4580 case CONVERT_EXPR:
4581 /* This is used for unary plus, because a CONVERT_EXPR
4582 is enough to prevent anybody from looking inside for
4583 associativity, but won't generate any code. */
4584 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4585 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4586 || gnu_vector_type_p (TREE_TYPE (arg))))
4588 error_at (location, "wrong type argument to unary plus");
4589 return error_mark_node;
4591 else if (!noconvert)
4592 arg = default_conversion (arg);
4593 arg = non_lvalue_loc (location, arg);
4594 break;
4596 case NEGATE_EXPR:
4597 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4598 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4599 || gnu_vector_type_p (TREE_TYPE (arg))))
4601 error_at (location, "wrong type argument to unary minus");
4602 return error_mark_node;
4604 else if (!noconvert)
4605 arg = default_conversion (arg);
4606 break;
4608 case BIT_NOT_EXPR:
4609 /* ~ works on integer types and non float vectors. */
4610 if (typecode == INTEGER_TYPE
4611 || (gnu_vector_type_p (TREE_TYPE (arg))
4612 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4614 tree e = arg;
4616 /* Warn if the expression has boolean value. */
4617 while (TREE_CODE (e) == COMPOUND_EXPR)
4618 e = TREE_OPERAND (e, 1);
4620 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg))
4621 || truth_value_p (TREE_CODE (e))))
4623 auto_diagnostic_group d;
4624 if (warning_at (location, OPT_Wbool_operation,
4625 "%<~%> on a boolean expression"))
4627 gcc_rich_location richloc (location);
4628 richloc.add_fixit_insert_before (location, "!");
4629 inform (&richloc, "did you mean to use logical not?");
4632 if (!noconvert)
4633 arg = default_conversion (arg);
4635 else if (typecode == COMPLEX_TYPE)
4637 code = CONJ_EXPR;
4638 pedwarn (location, OPT_Wpedantic,
4639 "ISO C does not support %<~%> for complex conjugation");
4640 if (!noconvert)
4641 arg = default_conversion (arg);
4643 else
4645 error_at (location, "wrong type argument to bit-complement");
4646 return error_mark_node;
4648 break;
4650 case ABS_EXPR:
4651 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4653 error_at (location, "wrong type argument to abs");
4654 return error_mark_node;
4656 else if (!noconvert)
4657 arg = default_conversion (arg);
4658 break;
4660 case ABSU_EXPR:
4661 if (!(typecode == INTEGER_TYPE))
4663 error_at (location, "wrong type argument to absu");
4664 return error_mark_node;
4666 else if (!noconvert)
4667 arg = default_conversion (arg);
4668 break;
4670 case CONJ_EXPR:
4671 /* Conjugating a real value is a no-op, but allow it anyway. */
4672 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4673 || typecode == COMPLEX_TYPE))
4675 error_at (location, "wrong type argument to conjugation");
4676 return error_mark_node;
4678 else if (!noconvert)
4679 arg = default_conversion (arg);
4680 break;
4682 case TRUTH_NOT_EXPR:
4683 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4684 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4685 && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE)
4687 error_at (location,
4688 "wrong type argument to unary exclamation mark");
4689 return error_mark_node;
4691 if (int_operands)
4693 arg = c_objc_common_truthvalue_conversion (location, xarg);
4694 arg = remove_c_maybe_const_expr (arg);
4696 else
4697 arg = c_objc_common_truthvalue_conversion (location, arg);
4698 ret = invert_truthvalue_loc (location, arg);
4699 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4700 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4701 location = EXPR_LOCATION (ret);
4702 goto return_build_unary_op;
4704 case REALPART_EXPR:
4705 case IMAGPART_EXPR:
4706 ret = build_real_imag_expr (location, code, arg);
4707 if (ret == error_mark_node)
4708 return error_mark_node;
4709 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4710 eptype = TREE_TYPE (eptype);
4711 goto return_build_unary_op;
4713 case PREINCREMENT_EXPR:
4714 case POSTINCREMENT_EXPR:
4715 case PREDECREMENT_EXPR:
4716 case POSTDECREMENT_EXPR:
4718 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4720 tree inner = build_unary_op (location, code,
4721 C_MAYBE_CONST_EXPR_EXPR (arg),
4722 noconvert);
4723 if (inner == error_mark_node)
4724 return error_mark_node;
4725 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4726 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4727 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4728 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4729 goto return_build_unary_op;
4732 /* Complain about anything that is not a true lvalue. In
4733 Objective-C, skip this check for property_refs. */
4734 if (!objc_is_property_ref (arg)
4735 && !lvalue_or_else (location,
4736 arg, ((code == PREINCREMENT_EXPR
4737 || code == POSTINCREMENT_EXPR)
4738 ? lv_increment
4739 : lv_decrement)))
4740 return error_mark_node;
4742 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4744 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4745 warning_at (location, OPT_Wc___compat,
4746 "increment of enumeration value is invalid in C++");
4747 else
4748 warning_at (location, OPT_Wc___compat,
4749 "decrement of enumeration value is invalid in C++");
4752 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
4754 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4755 warning_at (location, OPT_Wbool_operation,
4756 "increment of a boolean expression");
4757 else
4758 warning_at (location, OPT_Wbool_operation,
4759 "decrement of a boolean expression");
4762 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4763 arg = c_fully_fold (arg, false, NULL, true);
4765 bool atomic_op;
4766 atomic_op = really_atomic_lvalue (arg);
4768 /* Increment or decrement the real part of the value,
4769 and don't change the imaginary part. */
4770 if (typecode == COMPLEX_TYPE)
4772 tree real, imag;
4774 pedwarn (location, OPT_Wpedantic,
4775 "ISO C does not support %<++%> and %<--%> on complex types");
4777 if (!atomic_op)
4779 arg = stabilize_reference (arg);
4780 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4781 true);
4782 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4783 true);
4784 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4785 if (real == error_mark_node || imag == error_mark_node)
4786 return error_mark_node;
4787 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4788 real, imag);
4789 goto return_build_unary_op;
4793 /* Report invalid types. */
4795 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4796 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4797 && typecode != COMPLEX_TYPE
4798 && !gnu_vector_type_p (TREE_TYPE (arg)))
4800 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4801 error_at (location, "wrong type argument to increment");
4802 else
4803 error_at (location, "wrong type argument to decrement");
4805 return error_mark_node;
4809 tree inc;
4811 argtype = TREE_TYPE (arg);
4813 /* Compute the increment. */
4815 if (typecode == POINTER_TYPE)
4817 /* If pointer target is an incomplete type,
4818 we just cannot know how to do the arithmetic. */
4819 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4821 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4822 error_at (location,
4823 "increment of pointer to an incomplete type %qT",
4824 TREE_TYPE (argtype));
4825 else
4826 error_at (location,
4827 "decrement of pointer to an incomplete type %qT",
4828 TREE_TYPE (argtype));
4830 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4831 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4833 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4834 pedwarn (location, OPT_Wpointer_arith,
4835 "wrong type argument to increment");
4836 else
4837 pedwarn (location, OPT_Wpointer_arith,
4838 "wrong type argument to decrement");
4840 else
4841 verify_type_context (location, TCTX_POINTER_ARITH,
4842 TREE_TYPE (argtype));
4844 inc = c_size_in_bytes (TREE_TYPE (argtype));
4845 inc = convert_to_ptrofftype_loc (location, inc);
4847 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4849 /* For signed fract types, we invert ++ to -- or
4850 -- to ++, and change inc from 1 to -1, because
4851 it is not possible to represent 1 in signed fract constants.
4852 For unsigned fract types, the result always overflows and
4853 we get an undefined (original) or the maximum value. */
4854 if (code == PREINCREMENT_EXPR)
4855 code = PREDECREMENT_EXPR;
4856 else if (code == PREDECREMENT_EXPR)
4857 code = PREINCREMENT_EXPR;
4858 else if (code == POSTINCREMENT_EXPR)
4859 code = POSTDECREMENT_EXPR;
4860 else /* code == POSTDECREMENT_EXPR */
4861 code = POSTINCREMENT_EXPR;
4863 inc = integer_minus_one_node;
4864 inc = convert (argtype, inc);
4866 else
4868 inc = VECTOR_TYPE_P (argtype)
4869 ? build_one_cst (argtype)
4870 : integer_one_node;
4871 inc = convert (argtype, inc);
4874 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4875 need to ask Objective-C to build the increment or decrement
4876 expression for it. */
4877 if (objc_is_property_ref (arg))
4878 return objc_build_incr_expr_for_property_ref (location, code,
4879 arg, inc);
4881 /* Report a read-only lvalue. */
4882 if (TYPE_READONLY (argtype))
4884 readonly_error (location, arg,
4885 ((code == PREINCREMENT_EXPR
4886 || code == POSTINCREMENT_EXPR)
4887 ? lv_increment : lv_decrement));
4888 return error_mark_node;
4890 else if (TREE_READONLY (arg))
4891 readonly_warning (arg,
4892 ((code == PREINCREMENT_EXPR
4893 || code == POSTINCREMENT_EXPR)
4894 ? lv_increment : lv_decrement));
4896 /* If the argument is atomic, use the special code sequences for
4897 atomic compound assignment. */
4898 if (atomic_op)
4900 arg = stabilize_reference (arg);
4901 ret = build_atomic_assign (location, arg,
4902 ((code == PREINCREMENT_EXPR
4903 || code == POSTINCREMENT_EXPR)
4904 ? PLUS_EXPR
4905 : MINUS_EXPR),
4906 (FRACT_MODE_P (TYPE_MODE (argtype))
4907 ? inc
4908 : integer_one_node),
4909 (code == POSTINCREMENT_EXPR
4910 || code == POSTDECREMENT_EXPR));
4911 goto return_build_unary_op;
4914 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
4915 val = boolean_increment (code, arg);
4916 else
4917 val = build2 (code, TREE_TYPE (arg), arg, inc);
4918 TREE_SIDE_EFFECTS (val) = 1;
4919 if (TYPE_QUALS (TREE_TYPE (val)) != TYPE_UNQUALIFIED)
4920 TREE_TYPE (val) = c_build_qualified_type (TREE_TYPE (val),
4921 TYPE_UNQUALIFIED);
4922 ret = val;
4923 goto return_build_unary_op;
4926 case ADDR_EXPR:
4927 /* Note that this operation never does default_conversion. */
4929 /* The operand of unary '&' must be an lvalue (which excludes
4930 expressions of type void), or, in C99, the result of a [] or
4931 unary '*' operator. */
4932 if (VOID_TYPE_P (TREE_TYPE (arg))
4933 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4934 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4935 pedwarn (location, 0, "taking address of expression of type %<void%>");
4937 /* Let &* cancel out to simplify resulting code. */
4938 if (INDIRECT_REF_P (arg))
4940 /* Don't let this be an lvalue. */
4941 if (lvalue_p (TREE_OPERAND (arg, 0)))
4942 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4943 ret = TREE_OPERAND (arg, 0);
4944 goto return_build_unary_op;
4947 /* Anything not already handled and not a true memory reference
4948 or a non-lvalue array is an error. */
4949 if (typecode != FUNCTION_TYPE && !noconvert
4950 && !lvalue_or_else (location, arg, lv_addressof))
4951 return error_mark_node;
4953 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4954 folding later. */
4955 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4957 tree inner = build_unary_op (location, code,
4958 C_MAYBE_CONST_EXPR_EXPR (arg),
4959 noconvert);
4960 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4961 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4962 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4963 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4964 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4965 goto return_build_unary_op;
4968 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4969 argtype = TREE_TYPE (arg);
4971 /* If the lvalue is const or volatile, merge that into the type
4972 to which the address will point. This is only needed
4973 for function types. */
4974 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4975 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4976 && TREE_CODE (argtype) == FUNCTION_TYPE)
4978 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4979 int quals = orig_quals;
4981 if (TREE_READONLY (arg))
4982 quals |= TYPE_QUAL_CONST;
4983 if (TREE_THIS_VOLATILE (arg))
4984 quals |= TYPE_QUAL_VOLATILE;
4986 argtype = c_build_qualified_type (argtype, quals);
4989 switch (TREE_CODE (arg))
4991 case COMPONENT_REF:
4992 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4994 error_at (location, "cannot take address of bit-field %qD",
4995 TREE_OPERAND (arg, 1));
4996 return error_mark_node;
4999 /* fall through */
5001 case ARRAY_REF:
5002 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
5004 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
5005 && !POINTER_TYPE_P (TREE_TYPE (arg))
5006 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
5008 error_at (location, "cannot take address of scalar with "
5009 "reverse storage order");
5010 return error_mark_node;
5013 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
5014 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
5015 warning_at (location, OPT_Wscalar_storage_order,
5016 "address of array with reverse scalar storage "
5017 "order requested");
5020 default:
5021 break;
5024 if (!c_mark_addressable (arg))
5025 return error_mark_node;
5027 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
5028 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
5030 argtype = build_pointer_type (argtype);
5032 /* ??? Cope with user tricks that amount to offsetof. Delete this
5033 when we have proper support for integer constant expressions. */
5034 val = get_base_address (arg);
5035 if (val && INDIRECT_REF_P (val)
5036 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5038 ret = fold_offsetof (arg, argtype);
5039 goto return_build_unary_op;
5042 val = build1 (ADDR_EXPR, argtype, arg);
5044 ret = val;
5045 goto return_build_unary_op;
5047 case PAREN_EXPR:
5048 ret = build1 (code, TREE_TYPE (arg), arg);
5049 goto return_build_unary_op;
5051 default:
5052 gcc_unreachable ();
5055 if (argtype == NULL_TREE)
5056 argtype = TREE_TYPE (arg);
5057 if (TREE_CODE (arg) == INTEGER_CST)
5058 ret = (require_constant_value
5059 ? fold_build1_initializer_loc (location, code, argtype, arg)
5060 : fold_build1_loc (location, code, argtype, arg));
5061 else
5062 ret = build1 (code, argtype, arg);
5063 return_build_unary_op:
5064 gcc_assert (ret != error_mark_node);
5065 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
5066 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
5067 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
5068 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
5069 ret = note_integer_operands (ret);
5070 if (eptype)
5071 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5072 protected_set_expr_location (ret, location);
5073 return ret;
5076 /* Return nonzero if REF is an lvalue valid for this language.
5077 Lvalues can be assigned, unless their type has TYPE_READONLY.
5078 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
5080 bool
5081 lvalue_p (const_tree ref)
5083 const enum tree_code code = TREE_CODE (ref);
5085 switch (code)
5087 case REALPART_EXPR:
5088 case IMAGPART_EXPR:
5089 case COMPONENT_REF:
5090 return lvalue_p (TREE_OPERAND (ref, 0));
5092 case C_MAYBE_CONST_EXPR:
5093 return lvalue_p (TREE_OPERAND (ref, 1));
5095 case COMPOUND_LITERAL_EXPR:
5096 case STRING_CST:
5097 return true;
5099 case MEM_REF:
5100 case TARGET_MEM_REF:
5101 /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
5102 here as well. */
5103 case INDIRECT_REF:
5104 case ARRAY_REF:
5105 case VAR_DECL:
5106 case PARM_DECL:
5107 case RESULT_DECL:
5108 case ERROR_MARK:
5109 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
5110 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
5112 case BIND_EXPR:
5113 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
5115 default:
5116 return false;
5120 /* Give a warning for storing in something that is read-only in GCC
5121 terms but not const in ISO C terms. */
5123 static void
5124 readonly_warning (tree arg, enum lvalue_use use)
5126 switch (use)
5128 case lv_assign:
5129 warning (0, "assignment of read-only location %qE", arg);
5130 break;
5131 case lv_increment:
5132 warning (0, "increment of read-only location %qE", arg);
5133 break;
5134 case lv_decrement:
5135 warning (0, "decrement of read-only location %qE", arg);
5136 break;
5137 default:
5138 gcc_unreachable ();
5140 return;
5144 /* Return nonzero if REF is an lvalue valid for this language;
5145 otherwise, print an error message and return zero. USE says
5146 how the lvalue is being used and so selects the error message.
5147 LOCATION is the location at which any error should be reported. */
5149 static int
5150 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
5152 int win = lvalue_p (ref);
5154 if (!win)
5155 lvalue_error (loc, use);
5157 return win;
5160 /* Mark EXP saying that we need to be able to take the
5161 address of it; it should not be allocated in a register.
5162 Returns true if successful. ARRAY_REF_P is true if this
5163 is for ARRAY_REF construction - in that case we don't want
5164 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5165 it is fine to use ARRAY_REFs for vector subscripts on vector
5166 register variables. */
5168 bool
5169 c_mark_addressable (tree exp, bool array_ref_p)
5171 tree x = exp;
5173 while (1)
5174 switch (TREE_CODE (x))
5176 case VIEW_CONVERT_EXPR:
5177 if (array_ref_p
5178 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5179 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5180 return true;
5181 x = TREE_OPERAND (x, 0);
5182 break;
5184 case COMPONENT_REF:
5185 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
5187 error ("cannot take address of bit-field %qD",
5188 TREE_OPERAND (x, 1));
5189 return false;
5191 /* FALLTHRU */
5192 case ADDR_EXPR:
5193 case ARRAY_REF:
5194 case REALPART_EXPR:
5195 case IMAGPART_EXPR:
5196 x = TREE_OPERAND (x, 0);
5197 break;
5199 case COMPOUND_LITERAL_EXPR:
5200 if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)))
5202 error ("address of register compound literal requested");
5203 return false;
5205 TREE_ADDRESSABLE (x) = 1;
5206 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5207 return true;
5209 case CONSTRUCTOR:
5210 TREE_ADDRESSABLE (x) = 1;
5211 return true;
5213 case VAR_DECL:
5214 case CONST_DECL:
5215 case PARM_DECL:
5216 case RESULT_DECL:
5217 if (C_DECL_REGISTER (x)
5218 && DECL_NONLOCAL (x))
5220 if (TREE_PUBLIC (x) || is_global_var (x))
5222 error
5223 ("global register variable %qD used in nested function", x);
5224 return false;
5226 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5228 else if (C_DECL_REGISTER (x))
5230 if (TREE_PUBLIC (x) || is_global_var (x))
5231 error ("address of global register variable %qD requested", x);
5232 else
5233 error ("address of register variable %qD requested", x);
5234 return false;
5237 /* FALLTHRU */
5238 case FUNCTION_DECL:
5239 TREE_ADDRESSABLE (x) = 1;
5240 /* FALLTHRU */
5241 default:
5242 return true;
5246 /* Convert EXPR to TYPE, warning about conversion problems with
5247 constants. SEMANTIC_TYPE is the type this conversion would use
5248 without excess precision. If SEMANTIC_TYPE is NULL, this function
5249 is equivalent to convert_and_check. This function is a wrapper that
5250 handles conversions that may be different than
5251 the usual ones because of excess precision. */
5253 static tree
5254 ep_convert_and_check (location_t loc, tree type, tree expr,
5255 tree semantic_type)
5257 if (TREE_TYPE (expr) == type)
5258 return expr;
5260 /* For C11, integer conversions may have results with excess
5261 precision. */
5262 if (flag_isoc11 || !semantic_type)
5263 return convert_and_check (loc, type, expr);
5265 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5266 && TREE_TYPE (expr) != semantic_type)
5268 /* For integers, we need to check the real conversion, not
5269 the conversion to the excess precision type. */
5270 expr = convert_and_check (loc, semantic_type, expr);
5272 /* Result type is the excess precision type, which should be
5273 large enough, so do not check. */
5274 return convert (type, expr);
5277 /* If EXPR refers to a built-in declared without a prototype returns
5278 the actual type of the built-in and, if non-null, set *BLTIN to
5279 a pointer to the built-in. Otherwise return the type of EXPR
5280 and clear *BLTIN if non-null. */
5282 static tree
5283 type_or_builtin_type (tree expr, tree *bltin = NULL)
5285 tree dummy;
5286 if (!bltin)
5287 bltin = &dummy;
5289 *bltin = NULL_TREE;
5291 tree type = TREE_TYPE (expr);
5292 if (TREE_CODE (expr) != ADDR_EXPR)
5293 return type;
5295 tree oper = TREE_OPERAND (expr, 0);
5296 if (!DECL_P (oper)
5297 || TREE_CODE (oper) != FUNCTION_DECL
5298 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5299 return type;
5301 built_in_function code = DECL_FUNCTION_CODE (oper);
5302 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5303 return type;
5305 if ((*bltin = builtin_decl_implicit (code)))
5306 type = build_pointer_type (TREE_TYPE (*bltin));
5308 return type;
5311 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5312 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5313 if folded to an integer constant then the unselected half may
5314 contain arbitrary operations not normally permitted in constant
5315 expressions. Set the location of the expression to LOC. */
5317 tree
5318 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5319 tree op1, tree op1_original_type, location_t op1_loc,
5320 tree op2, tree op2_original_type, location_t op2_loc)
5322 tree type1;
5323 tree type2;
5324 enum tree_code code1;
5325 enum tree_code code2;
5326 tree result_type = NULL;
5327 tree semantic_result_type = NULL;
5328 tree orig_op1 = op1, orig_op2 = op2;
5329 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5330 bool ifexp_int_operands;
5331 tree ret;
5333 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5334 if (op1_int_operands)
5335 op1 = remove_c_maybe_const_expr (op1);
5336 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5337 if (op2_int_operands)
5338 op2 = remove_c_maybe_const_expr (op2);
5339 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5340 if (ifexp_int_operands)
5341 ifexp = remove_c_maybe_const_expr (ifexp);
5343 /* Promote both alternatives. */
5345 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5346 op1 = default_conversion (op1);
5347 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5348 op2 = default_conversion (op2);
5350 if (TREE_CODE (ifexp) == ERROR_MARK
5351 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5352 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5353 return error_mark_node;
5355 tree bltin1 = NULL_TREE;
5356 tree bltin2 = NULL_TREE;
5357 type1 = type_or_builtin_type (op1, &bltin1);
5358 code1 = TREE_CODE (type1);
5359 type2 = type_or_builtin_type (op2, &bltin2);
5360 code2 = TREE_CODE (type2);
5362 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5363 return error_mark_node;
5365 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5366 return error_mark_node;
5368 /* C90 does not permit non-lvalue arrays in conditional expressions.
5369 In C99 they will be pointers by now. */
5370 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5372 error_at (colon_loc, "non-lvalue array in conditional expression");
5373 return error_mark_node;
5376 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5377 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5378 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5379 || code1 == COMPLEX_TYPE)
5380 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5381 || code2 == COMPLEX_TYPE))
5383 semantic_result_type = c_common_type (type1, type2);
5384 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5386 op1 = TREE_OPERAND (op1, 0);
5387 type1 = TREE_TYPE (op1);
5388 gcc_assert (TREE_CODE (type1) == code1);
5390 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5392 op2 = TREE_OPERAND (op2, 0);
5393 type2 = TREE_TYPE (op2);
5394 gcc_assert (TREE_CODE (type2) == code2);
5398 if (warn_cxx_compat)
5400 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5401 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5403 if (TREE_CODE (t1) == ENUMERAL_TYPE
5404 && TREE_CODE (t2) == ENUMERAL_TYPE
5405 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5406 warning_at (colon_loc, OPT_Wc___compat,
5407 ("different enum types in conditional is "
5408 "invalid in C++: %qT vs %qT"),
5409 t1, t2);
5412 /* Quickly detect the usual case where op1 and op2 have the same type
5413 after promotion. */
5414 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5416 if (type1 == type2)
5417 result_type = type1;
5418 else
5419 result_type = TYPE_MAIN_VARIANT (type1);
5421 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5422 || code1 == COMPLEX_TYPE)
5423 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5424 || code2 == COMPLEX_TYPE))
5426 /* In C11, a conditional expression between a floating-point
5427 type and an integer type should convert the integer type to
5428 the evaluation format of the floating-point type, with
5429 possible excess precision. */
5430 tree eptype1 = type1;
5431 tree eptype2 = type2;
5432 if (flag_isoc11)
5434 tree eptype;
5435 if (ANY_INTEGRAL_TYPE_P (type1)
5436 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5438 eptype2 = eptype;
5439 if (!semantic_result_type)
5440 semantic_result_type = c_common_type (type1, type2);
5442 else if (ANY_INTEGRAL_TYPE_P (type2)
5443 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5445 eptype1 = eptype;
5446 if (!semantic_result_type)
5447 semantic_result_type = c_common_type (type1, type2);
5450 result_type = c_common_type (eptype1, eptype2);
5451 if (result_type == error_mark_node)
5452 return error_mark_node;
5453 do_warn_double_promotion (result_type, type1, type2,
5454 "implicit conversion from %qT to %qT to "
5455 "match other result of conditional",
5456 colon_loc);
5458 /* If -Wsign-compare, warn here if type1 and type2 have
5459 different signedness. We'll promote the signed to unsigned
5460 and later code won't know it used to be different.
5461 Do this check on the original types, so that explicit casts
5462 will be considered, but default promotions won't. */
5463 if (c_inhibit_evaluation_warnings == 0)
5465 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5466 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5468 if (unsigned_op1 ^ unsigned_op2)
5470 bool ovf;
5472 /* Do not warn if the result type is signed, since the
5473 signed type will only be chosen if it can represent
5474 all the values of the unsigned type. */
5475 if (!TYPE_UNSIGNED (result_type))
5476 /* OK */;
5477 else
5479 bool op1_maybe_const = true;
5480 bool op2_maybe_const = true;
5482 /* Do not warn if the signed quantity is an
5483 unsuffixed integer literal (or some static
5484 constant expression involving such literals) and
5485 it is non-negative. This warning requires the
5486 operands to be folded for best results, so do
5487 that folding in this case even without
5488 warn_sign_compare to avoid warning options
5489 possibly affecting code generation. */
5490 c_inhibit_evaluation_warnings
5491 += (ifexp == truthvalue_false_node);
5492 op1 = c_fully_fold (op1, require_constant_value,
5493 &op1_maybe_const);
5494 c_inhibit_evaluation_warnings
5495 -= (ifexp == truthvalue_false_node);
5497 c_inhibit_evaluation_warnings
5498 += (ifexp == truthvalue_true_node);
5499 op2 = c_fully_fold (op2, require_constant_value,
5500 &op2_maybe_const);
5501 c_inhibit_evaluation_warnings
5502 -= (ifexp == truthvalue_true_node);
5504 if (warn_sign_compare)
5506 if ((unsigned_op2
5507 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5508 || (unsigned_op1
5509 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5510 /* OK */;
5511 else if (unsigned_op2)
5512 warning_at (op1_loc, OPT_Wsign_compare,
5513 "operand of %<?:%> changes signedness from "
5514 "%qT to %qT due to unsignedness of other "
5515 "operand", TREE_TYPE (orig_op1),
5516 TREE_TYPE (orig_op2));
5517 else
5518 warning_at (op2_loc, OPT_Wsign_compare,
5519 "operand of %<?:%> changes signedness from "
5520 "%qT to %qT due to unsignedness of other "
5521 "operand", TREE_TYPE (orig_op2),
5522 TREE_TYPE (orig_op1));
5524 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5525 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5526 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5527 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5532 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5534 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5535 pedwarn (colon_loc, OPT_Wpedantic,
5536 "ISO C forbids conditional expr with only one void side");
5537 result_type = void_type_node;
5539 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5541 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5542 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5543 addr_space_t as_common;
5545 if (comp_target_types (colon_loc, type1, type2))
5546 result_type = common_pointer_type (type1, type2);
5547 else if (null_pointer_constant_p (orig_op1))
5548 result_type = type2;
5549 else if (null_pointer_constant_p (orig_op2))
5550 result_type = type1;
5551 else if (!addr_space_superset (as1, as2, &as_common))
5553 error_at (colon_loc, "pointers to disjoint address spaces "
5554 "used in conditional expression");
5555 return error_mark_node;
5557 else if ((VOID_TYPE_P (TREE_TYPE (type1))
5558 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5559 || (VOID_TYPE_P (TREE_TYPE (type2))
5560 && !TYPE_ATOMIC (TREE_TYPE (type2))))
5562 tree t1 = TREE_TYPE (type1);
5563 tree t2 = TREE_TYPE (type2);
5564 if (!(VOID_TYPE_P (t1)
5565 && !TYPE_ATOMIC (t1)))
5567 /* roles are swapped */
5568 t1 = t2;
5569 t2 = TREE_TYPE (type1);
5571 tree t2_stripped = strip_array_types (t2);
5572 if ((TREE_CODE (t2) == ARRAY_TYPE)
5573 && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
5575 if (!flag_isoc2x)
5576 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5577 "pointer to array loses qualifier "
5578 "in conditional expression");
5579 else if (warn_c11_c2x_compat > 0)
5580 warning_at (colon_loc, OPT_Wc11_c2x_compat,
5581 "pointer to array loses qualifier "
5582 "in conditional expression in ISO C before C2X");
5584 if (TREE_CODE (t2) == FUNCTION_TYPE)
5585 pedwarn (colon_loc, OPT_Wpedantic,
5586 "ISO C forbids conditional expr between "
5587 "%<void *%> and function pointer");
5588 /* for array, use qualifiers of element type */
5589 if (flag_isoc2x)
5590 t2 = t2_stripped;
5591 result_type = build_pointer_type (qualify_type (t1, t2));
5593 /* Objective-C pointer comparisons are a bit more lenient. */
5594 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5595 result_type = objc_common_type (type1, type2);
5596 else
5598 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5599 if (bltin1 && bltin2)
5600 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5601 "pointer type mismatch between %qT and %qT "
5602 "of %qD and %qD in conditional expression",
5603 type1, type2, bltin1, bltin2);
5604 else
5605 pedwarn (colon_loc, 0,
5606 "pointer type mismatch in conditional expression");
5607 result_type = build_pointer_type
5608 (build_qualified_type (void_type_node, qual));
5611 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5613 if (!null_pointer_constant_p (orig_op2))
5614 pedwarn (colon_loc, 0,
5615 "pointer/integer type mismatch in conditional expression");
5616 else
5618 op2 = null_pointer_node;
5620 result_type = type1;
5622 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5624 if (!null_pointer_constant_p (orig_op1))
5625 pedwarn (colon_loc, 0,
5626 "pointer/integer type mismatch in conditional expression");
5627 else
5629 op1 = null_pointer_node;
5631 result_type = type2;
5633 /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
5634 type nullptr_t and the other is a pointer, the result type is the pointer
5635 type." */
5636 else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE)
5637 result_type = type2;
5638 else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE)
5639 result_type = type1;
5641 if (!result_type)
5643 if (flag_cond_mismatch)
5644 result_type = void_type_node;
5645 else
5647 error_at (colon_loc, "type mismatch in conditional expression");
5648 return error_mark_node;
5652 /* Merge const and volatile flags of the incoming types. */
5653 result_type
5654 = build_type_variant (result_type,
5655 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5656 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5658 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5659 semantic_result_type);
5660 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5661 semantic_result_type);
5663 if (ifexp_bcp && ifexp == truthvalue_true_node)
5665 op2_int_operands = true;
5666 op1 = c_fully_fold (op1, require_constant_value, NULL);
5668 if (ifexp_bcp && ifexp == truthvalue_false_node)
5670 op1_int_operands = true;
5671 op2 = c_fully_fold (op2, require_constant_value, NULL);
5673 int_const = int_operands = (ifexp_int_operands
5674 && op1_int_operands
5675 && op2_int_operands);
5676 if (int_operands)
5678 int_const = ((ifexp == truthvalue_true_node
5679 && TREE_CODE (orig_op1) == INTEGER_CST
5680 && !TREE_OVERFLOW (orig_op1))
5681 || (ifexp == truthvalue_false_node
5682 && TREE_CODE (orig_op2) == INTEGER_CST
5683 && !TREE_OVERFLOW (orig_op2)));
5686 /* Need to convert condition operand into a vector mask. */
5687 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5689 tree vectype = TREE_TYPE (ifexp);
5690 tree elem_type = TREE_TYPE (vectype);
5691 tree zero = build_int_cst (elem_type, 0);
5692 tree zero_vec = build_vector_from_val (vectype, zero);
5693 tree cmp_type = truth_type_for (vectype);
5694 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5697 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5698 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5699 else
5701 if (int_operands)
5703 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5704 nested inside of the expression. */
5705 op1 = c_fully_fold (op1, false, NULL);
5706 op2 = c_fully_fold (op2, false, NULL);
5708 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5709 if (int_operands)
5710 ret = note_integer_operands (ret);
5712 if (semantic_result_type)
5713 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5715 protected_set_expr_location (ret, colon_loc);
5717 /* If the OP1 and OP2 are the same and don't have side-effects,
5718 warn here, because the COND_EXPR will be turned into OP1. */
5719 if (warn_duplicated_branches
5720 && TREE_CODE (ret) == COND_EXPR
5721 && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
5722 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5723 "this condition has identical branches");
5725 return ret;
5728 /* EXPR is an expression, location LOC, whose result is discarded.
5729 Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
5730 whose right-hand operand is such a call, possibly recursively). */
5732 static void
5733 maybe_warn_nodiscard (location_t loc, tree expr)
5735 if (VOID_TYPE_P (TREE_TYPE (expr)))
5736 return;
5737 while (TREE_CODE (expr) == COMPOUND_EXPR)
5739 expr = TREE_OPERAND (expr, 1);
5740 if (EXPR_HAS_LOCATION (expr))
5741 loc = EXPR_LOCATION (expr);
5743 if (TREE_CODE (expr) != CALL_EXPR)
5744 return;
5745 tree fn = CALL_EXPR_FN (expr);
5746 if (!fn)
5747 return;
5748 tree attr;
5749 if (TREE_CODE (fn) == ADDR_EXPR
5750 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5751 && (attr = lookup_attribute ("nodiscard",
5752 DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
5754 fn = TREE_OPERAND (fn, 0);
5755 tree args = TREE_VALUE (attr);
5756 if (args)
5757 args = TREE_VALUE (args);
5758 auto_diagnostic_group d;
5759 int warned;
5760 if (args)
5761 warned = warning_at (loc, OPT_Wunused_result,
5762 "ignoring return value of %qD, declared with "
5763 "attribute %<nodiscard%>: %E", fn, args);
5764 else
5765 warned = warning_at (loc, OPT_Wunused_result,
5766 "ignoring return value of %qD, declared with "
5767 "attribute %<nodiscard%>", fn);
5768 if (warned)
5769 inform (DECL_SOURCE_LOCATION (fn), "declared here");
5771 else
5773 tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
5774 attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
5775 if (!attr)
5776 return;
5777 tree args = TREE_VALUE (attr);
5778 if (args)
5779 args = TREE_VALUE (args);
5780 auto_diagnostic_group d;
5781 int warned;
5782 if (args)
5783 warned = warning_at (loc, OPT_Wunused_result,
5784 "ignoring return value of type %qT, declared "
5785 "with attribute %<nodiscard%>: %E",
5786 rettype, args);
5787 else
5788 warned = warning_at (loc, OPT_Wunused_result,
5789 "ignoring return value of type %qT, declared "
5790 "with attribute %<nodiscard%>", rettype);
5791 if (warned)
5793 if (TREE_CODE (fn) == ADDR_EXPR)
5795 fn = TREE_OPERAND (fn, 0);
5796 if (TREE_CODE (fn) == FUNCTION_DECL)
5797 inform (DECL_SOURCE_LOCATION (fn),
5798 "in call to %qD, declared here", fn);
5804 /* Return a compound expression that performs two expressions and
5805 returns the value of the second of them.
5807 LOC is the location of the COMPOUND_EXPR. */
5809 tree
5810 build_compound_expr (location_t loc, tree expr1, tree expr2)
5812 bool expr1_int_operands, expr2_int_operands;
5813 tree eptype = NULL_TREE;
5814 tree ret;
5816 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5817 if (expr1_int_operands)
5818 expr1 = remove_c_maybe_const_expr (expr1);
5819 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5820 if (expr2_int_operands)
5821 expr2 = remove_c_maybe_const_expr (expr2);
5823 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5824 expr1 = TREE_OPERAND (expr1, 0);
5825 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5827 eptype = TREE_TYPE (expr2);
5828 expr2 = TREE_OPERAND (expr2, 0);
5831 if (!TREE_SIDE_EFFECTS (expr1))
5833 /* The left-hand operand of a comma expression is like an expression
5834 statement: with -Wunused, we should warn if it doesn't have
5835 any side-effects, unless it was explicitly cast to (void). */
5836 if (warn_unused_value)
5838 if (VOID_TYPE_P (TREE_TYPE (expr1))
5839 && CONVERT_EXPR_P (expr1))
5840 ; /* (void) a, b */
5841 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5842 && TREE_CODE (expr1) == COMPOUND_EXPR
5843 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5844 ; /* (void) a, (void) b, c */
5845 else
5846 warning_at (loc, OPT_Wunused_value,
5847 "left-hand operand of comma expression has no effect");
5850 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5851 && warn_unused_value)
5853 tree r = expr1;
5854 location_t cloc = loc;
5855 while (TREE_CODE (r) == COMPOUND_EXPR)
5857 if (EXPR_HAS_LOCATION (r))
5858 cloc = EXPR_LOCATION (r);
5859 r = TREE_OPERAND (r, 1);
5861 if (!TREE_SIDE_EFFECTS (r)
5862 && !VOID_TYPE_P (TREE_TYPE (r))
5863 && !CONVERT_EXPR_P (r))
5864 warning_at (cloc, OPT_Wunused_value,
5865 "right-hand operand of comma expression has no effect");
5868 /* With -Wunused, we should also warn if the left-hand operand does have
5869 side-effects, but computes a value which is not used. For example, in
5870 `foo() + bar(), baz()' the result of the `+' operator is not used,
5871 so we should issue a warning. */
5872 else if (warn_unused_value)
5873 warn_if_unused_value (expr1, loc);
5875 maybe_warn_nodiscard (loc, expr1);
5877 if (expr2 == error_mark_node)
5878 return error_mark_node;
5880 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5882 if (flag_isoc99
5883 && expr1_int_operands
5884 && expr2_int_operands)
5885 ret = note_integer_operands (ret);
5887 if (eptype)
5888 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5890 protected_set_expr_location (ret, loc);
5891 return ret;
5894 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5895 which we are casting. OTYPE is the type of the expression being
5896 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5897 of the cast. -Wcast-qual appeared on the command line. Named
5898 address space qualifiers are not handled here, because they result
5899 in different warnings. */
5901 static void
5902 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5904 tree in_type = type;
5905 tree in_otype = otype;
5906 int added = 0;
5907 int discarded = 0;
5908 bool is_const;
5910 /* Check that the qualifiers on IN_TYPE are a superset of the
5911 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5912 nodes is uninteresting and we stop as soon as we hit a
5913 non-POINTER_TYPE node on either type. */
5916 in_otype = TREE_TYPE (in_otype);
5917 in_type = TREE_TYPE (in_type);
5919 /* GNU C allows cv-qualified function types. 'const' means the
5920 function is very pure, 'volatile' means it can't return. We
5921 need to warn when such qualifiers are added, not when they're
5922 taken away. */
5923 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5924 && TREE_CODE (in_type) == FUNCTION_TYPE)
5925 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5926 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5927 else
5928 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5929 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5931 while (TREE_CODE (in_type) == POINTER_TYPE
5932 && TREE_CODE (in_otype) == POINTER_TYPE);
5934 if (added)
5935 warning_at (loc, OPT_Wcast_qual,
5936 "cast adds %q#v qualifier to function type", added);
5938 if (discarded)
5939 /* There are qualifiers present in IN_OTYPE that are not present
5940 in IN_TYPE. */
5941 warning_at (loc, OPT_Wcast_qual,
5942 "cast discards %qv qualifier from pointer target type",
5943 discarded);
5945 if (added || discarded)
5946 return;
5948 /* A cast from **T to const **T is unsafe, because it can cause a
5949 const value to be changed with no additional warning. We only
5950 issue this warning if T is the same on both sides, and we only
5951 issue the warning if there are the same number of pointers on
5952 both sides, as otherwise the cast is clearly unsafe anyhow. A
5953 cast is unsafe when a qualifier is added at one level and const
5954 is not present at all outer levels.
5956 To issue this warning, we check at each level whether the cast
5957 adds new qualifiers not already seen. We don't need to special
5958 case function types, as they won't have the same
5959 TYPE_MAIN_VARIANT. */
5961 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5962 return;
5963 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5964 return;
5966 in_type = type;
5967 in_otype = otype;
5968 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5971 in_type = TREE_TYPE (in_type);
5972 in_otype = TREE_TYPE (in_otype);
5973 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5974 && !is_const)
5976 warning_at (loc, OPT_Wcast_qual,
5977 "to be safe all intermediate pointers in cast from "
5978 "%qT to %qT must be %<const%> qualified",
5979 otype, type);
5980 break;
5982 if (is_const)
5983 is_const = TYPE_READONLY (in_type);
5985 while (TREE_CODE (in_type) == POINTER_TYPE);
5988 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5990 static bool
5991 c_safe_arg_type_equiv_p (tree t1, tree t2)
5993 t1 = TYPE_MAIN_VARIANT (t1);
5994 t2 = TYPE_MAIN_VARIANT (t2);
5996 if (TREE_CODE (t1) == POINTER_TYPE
5997 && TREE_CODE (t2) == POINTER_TYPE)
5998 return true;
6000 /* The signedness of the parameter matters only when an integral
6001 type smaller than int is promoted to int, otherwise only the
6002 precision of the parameter matters.
6003 This check should make sure that the callee does not see
6004 undefined values in argument registers. */
6005 if (INTEGRAL_TYPE_P (t1)
6006 && INTEGRAL_TYPE_P (t2)
6007 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
6008 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
6009 || !targetm.calls.promote_prototypes (NULL_TREE)
6010 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
6011 return true;
6013 return comptypes (t1, t2);
6016 /* Check if a type cast between two function types can be considered safe. */
6018 static bool
6019 c_safe_function_type_cast_p (tree t1, tree t2)
6021 if (TREE_TYPE (t1) == void_type_node &&
6022 TYPE_ARG_TYPES (t1) == void_list_node)
6023 return true;
6025 if (TREE_TYPE (t2) == void_type_node &&
6026 TYPE_ARG_TYPES (t2) == void_list_node)
6027 return true;
6029 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
6030 return false;
6032 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
6033 t1 && t2;
6034 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6035 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
6036 return false;
6038 return true;
6041 /* Build an expression representing a cast to type TYPE of expression EXPR.
6042 LOC is the location of the cast-- typically the open paren of the cast. */
6044 tree
6045 build_c_cast (location_t loc, tree type, tree expr)
6047 tree value;
6049 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
6051 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
6052 expr = TREE_OPERAND (expr, 0);
6054 value = expr;
6055 if (int_operands)
6056 value = remove_c_maybe_const_expr (value);
6058 if (type == error_mark_node || expr == error_mark_node)
6059 return error_mark_node;
6061 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
6062 only in <protocol> qualifications. But when constructing cast expressions,
6063 the protocols do matter and must be kept around. */
6064 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
6065 return build1 (NOP_EXPR, type, expr);
6067 type = TYPE_MAIN_VARIANT (type);
6069 if (TREE_CODE (type) == ARRAY_TYPE)
6071 error_at (loc, "cast specifies array type");
6072 return error_mark_node;
6075 if (TREE_CODE (type) == FUNCTION_TYPE)
6077 error_at (loc, "cast specifies function type");
6078 return error_mark_node;
6081 if (!VOID_TYPE_P (type))
6083 value = require_complete_type (loc, value);
6084 if (value == error_mark_node)
6085 return error_mark_node;
6088 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
6090 if (RECORD_OR_UNION_TYPE_P (type))
6091 pedwarn (loc, OPT_Wpedantic,
6092 "ISO C forbids casting nonscalar to the same type");
6094 /* Convert to remove any qualifiers from VALUE's type. */
6095 value = convert (type, value);
6097 else if (TREE_CODE (type) == UNION_TYPE)
6099 tree field;
6101 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6102 if (TREE_TYPE (field) != error_mark_node
6103 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
6104 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
6105 break;
6107 if (field)
6109 tree t;
6110 bool maybe_const = true;
6112 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
6113 t = c_fully_fold (value, false, &maybe_const);
6114 t = build_constructor_single (type, field, t);
6115 if (!maybe_const)
6116 t = c_wrap_maybe_const (t, true);
6117 t = digest_init (loc, type, t,
6118 NULL_TREE, false, false, false, true, false, false);
6119 TREE_CONSTANT (t) = TREE_CONSTANT (value);
6120 return t;
6122 error_at (loc, "cast to union type from type not present in union");
6123 return error_mark_node;
6125 else
6127 tree otype, ovalue;
6129 if (type == void_type_node)
6131 tree t = build1 (CONVERT_EXPR, type, value);
6132 SET_EXPR_LOCATION (t, loc);
6133 return t;
6136 otype = TREE_TYPE (value);
6138 /* Optionally warn about potentially worrisome casts. */
6139 if (warn_cast_qual
6140 && TREE_CODE (type) == POINTER_TYPE
6141 && TREE_CODE (otype) == POINTER_TYPE)
6142 handle_warn_cast_qual (loc, type, otype);
6144 /* Warn about conversions between pointers to disjoint
6145 address spaces. */
6146 if (TREE_CODE (type) == POINTER_TYPE
6147 && TREE_CODE (otype) == POINTER_TYPE
6148 && !null_pointer_constant_p (value))
6150 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
6151 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
6152 addr_space_t as_common;
6154 if (!addr_space_superset (as_to, as_from, &as_common))
6156 if (ADDR_SPACE_GENERIC_P (as_from))
6157 warning_at (loc, 0, "cast to %qs address space pointer "
6158 "from disjoint generic address space pointer",
6159 c_addr_space_name (as_to));
6161 else if (ADDR_SPACE_GENERIC_P (as_to))
6162 warning_at (loc, 0, "cast to generic address space pointer "
6163 "from disjoint %qs address space pointer",
6164 c_addr_space_name (as_from));
6166 else
6167 warning_at (loc, 0, "cast to %qs address space pointer "
6168 "from disjoint %qs address space pointer",
6169 c_addr_space_name (as_to),
6170 c_addr_space_name (as_from));
6174 /* Warn about possible alignment problems. */
6175 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
6176 && TREE_CODE (type) == POINTER_TYPE
6177 && TREE_CODE (otype) == POINTER_TYPE
6178 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
6179 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6180 /* Don't warn about opaque types, where the actual alignment
6181 restriction is unknown. */
6182 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
6183 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
6184 && min_align_of_type (TREE_TYPE (type))
6185 > min_align_of_type (TREE_TYPE (otype)))
6186 warning_at (loc, OPT_Wcast_align,
6187 "cast increases required alignment of target type");
6189 if (TREE_CODE (type) == INTEGER_TYPE
6190 && TREE_CODE (otype) == POINTER_TYPE
6191 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
6192 /* Unlike conversion of integers to pointers, where the
6193 warning is disabled for converting constants because
6194 of cases such as SIG_*, warn about converting constant
6195 pointers to integers. In some cases it may cause unwanted
6196 sign extension, and a warning is appropriate. */
6197 warning_at (loc, OPT_Wpointer_to_int_cast,
6198 "cast from pointer to integer of different size");
6200 if (TREE_CODE (value) == CALL_EXPR
6201 && TREE_CODE (type) != TREE_CODE (otype))
6202 warning_at (loc, OPT_Wbad_function_cast,
6203 "cast from function call of type %qT "
6204 "to non-matching type %qT", otype, type);
6206 if (TREE_CODE (type) == POINTER_TYPE
6207 && TREE_CODE (otype) == INTEGER_TYPE
6208 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
6209 /* Don't warn about converting any constant. */
6210 && !TREE_CONSTANT (value))
6211 warning_at (loc,
6212 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
6213 "of different size");
6215 if (warn_strict_aliasing <= 2)
6216 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
6218 /* If pedantic, warn for conversions between function and object
6219 pointer types, except for converting a null pointer constant
6220 to function pointer type. */
6221 if (pedantic
6222 && TREE_CODE (type) == POINTER_TYPE
6223 && TREE_CODE (otype) == POINTER_TYPE
6224 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6225 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
6226 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6227 "conversion of function pointer to object pointer type");
6229 if (pedantic
6230 && TREE_CODE (type) == POINTER_TYPE
6231 && TREE_CODE (otype) == POINTER_TYPE
6232 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6233 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6234 && !null_pointer_constant_p (value))
6235 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6236 "conversion of object pointer to function pointer type");
6238 if (TREE_CODE (type) == POINTER_TYPE
6239 && TREE_CODE (otype) == POINTER_TYPE
6240 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6241 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6242 && !c_safe_function_type_cast_p (TREE_TYPE (type),
6243 TREE_TYPE (otype)))
6244 warning_at (loc, OPT_Wcast_function_type,
6245 "cast between incompatible function types"
6246 " from %qT to %qT", otype, type);
6248 ovalue = value;
6249 value = convert (type, value);
6251 /* Ignore any integer overflow caused by the cast. */
6252 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
6254 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6256 if (!TREE_OVERFLOW (value))
6258 /* Avoid clobbering a shared constant. */
6259 value = copy_node (value);
6260 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
6263 else if (TREE_OVERFLOW (value))
6264 /* Reset VALUE's overflow flags, ensuring constant sharing. */
6265 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6269 /* Don't let a cast be an lvalue. */
6270 if (lvalue_p (value))
6271 value = non_lvalue_loc (loc, value);
6273 /* Don't allow the results of casting to floating-point or complex
6274 types be confused with actual constants, or casts involving
6275 integer and pointer types other than direct integer-to-integer
6276 and integer-to-pointer be confused with integer constant
6277 expressions and null pointer constants. */
6278 if (TREE_CODE (value) == REAL_CST
6279 || TREE_CODE (value) == COMPLEX_CST
6280 || (TREE_CODE (value) == INTEGER_CST
6281 && !((TREE_CODE (expr) == INTEGER_CST
6282 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6283 || TREE_CODE (expr) == REAL_CST
6284 || TREE_CODE (expr) == COMPLEX_CST)))
6285 value = build1 (NOP_EXPR, type, value);
6287 /* If the expression has integer operands and so can occur in an
6288 unevaluated part of an integer constant expression, ensure the
6289 return value reflects this. */
6290 if (int_operands
6291 && INTEGRAL_TYPE_P (type)
6292 && value != error_mark_node
6293 && !EXPR_INT_CONST_OPERANDS (value))
6294 value = note_integer_operands (value);
6296 protected_set_expr_location (value, loc);
6297 return value;
6300 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
6301 location of the open paren of the cast, or the position of the cast
6302 expr. */
6303 tree
6304 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6306 tree type;
6307 tree type_expr = NULL_TREE;
6308 bool type_expr_const = true;
6309 tree ret;
6310 int saved_wsp = warn_strict_prototypes;
6312 /* This avoids warnings about unprototyped casts on
6313 integers. E.g. "#define SIG_DFL (void(*)())0". */
6314 if (TREE_CODE (expr) == INTEGER_CST)
6315 warn_strict_prototypes = 0;
6316 type = groktypename (type_name, &type_expr, &type_expr_const);
6317 warn_strict_prototypes = saved_wsp;
6319 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6320 && reject_gcc_builtin (expr))
6321 return error_mark_node;
6323 ret = build_c_cast (loc, type, expr);
6324 if (type_expr)
6326 bool inner_expr_const = true;
6327 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6328 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6329 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6330 && inner_expr_const);
6331 SET_EXPR_LOCATION (ret, loc);
6334 if (!EXPR_HAS_LOCATION (ret))
6335 protected_set_expr_location (ret, loc);
6337 /* C++ does not permits types to be defined in a cast, but it
6338 allows references to incomplete types. */
6339 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6340 warning_at (loc, OPT_Wc___compat,
6341 "defining a type in a cast is invalid in C++");
6343 return ret;
6346 /* Build an assignment expression of lvalue LHS from value RHS.
6347 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6348 may differ from TREE_TYPE (LHS) for an enum bitfield.
6349 MODIFYCODE is the code for a binary operator that we use
6350 to combine the old value of LHS with RHS to get the new value.
6351 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6352 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6353 which may differ from TREE_TYPE (RHS) for an enum value.
6355 LOCATION is the location of the MODIFYCODE operator.
6356 RHS_LOC is the location of the RHS. */
6358 tree
6359 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6360 enum tree_code modifycode,
6361 location_t rhs_loc, tree rhs, tree rhs_origtype)
6363 tree result;
6364 tree newrhs;
6365 tree rhseval = NULL_TREE;
6366 tree lhstype = TREE_TYPE (lhs);
6367 tree olhstype = lhstype;
6368 bool npc;
6369 bool is_atomic_op;
6371 /* Types that aren't fully specified cannot be used in assignments. */
6372 lhs = require_complete_type (location, lhs);
6374 /* Avoid duplicate error messages from operands that had errors. */
6375 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6376 return error_mark_node;
6378 /* Ensure an error for assigning a non-lvalue array to an array in
6379 C90. */
6380 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6382 error_at (location, "assignment to expression with array type");
6383 return error_mark_node;
6386 /* For ObjC properties, defer this check. */
6387 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6388 return error_mark_node;
6390 is_atomic_op = really_atomic_lvalue (lhs);
6392 newrhs = rhs;
6394 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6396 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6397 lhs_origtype, modifycode, rhs_loc, rhs,
6398 rhs_origtype);
6399 if (inner == error_mark_node)
6400 return error_mark_node;
6401 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6402 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6403 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6404 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6405 protected_set_expr_location (result, location);
6406 return result;
6409 /* If a binary op has been requested, combine the old LHS value with the RHS
6410 producing the value we should actually store into the LHS. */
6412 if (modifycode != NOP_EXPR)
6414 lhs = c_fully_fold (lhs, false, NULL, true);
6415 lhs = stabilize_reference (lhs);
6417 /* Construct the RHS for any non-atomic compound assignemnt. */
6418 if (!is_atomic_op)
6420 /* If in LHS op= RHS the RHS has side-effects, ensure they
6421 are preevaluated before the rest of the assignment expression's
6422 side-effects, because RHS could contain e.g. function calls
6423 that modify LHS. */
6424 if (TREE_SIDE_EFFECTS (rhs))
6426 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6427 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6428 else
6429 newrhs = save_expr (rhs);
6430 rhseval = newrhs;
6431 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6432 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6433 newrhs);
6435 newrhs = build_binary_op (location,
6436 modifycode, lhs, newrhs, true);
6438 /* The original type of the right hand side is no longer
6439 meaningful. */
6440 rhs_origtype = NULL_TREE;
6444 if (c_dialect_objc ())
6446 /* Check if we are modifying an Objective-C property reference;
6447 if so, we need to generate setter calls. */
6448 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6449 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6450 else
6451 result = objc_maybe_build_modify_expr (lhs, newrhs);
6452 if (result)
6453 goto return_result;
6455 /* Else, do the check that we postponed for Objective-C. */
6456 if (!lvalue_or_else (location, lhs, lv_assign))
6457 return error_mark_node;
6460 /* Give an error for storing in something that is 'const'. */
6462 if (TYPE_READONLY (lhstype)
6463 || (RECORD_OR_UNION_TYPE_P (lhstype)
6464 && C_TYPE_FIELDS_READONLY (lhstype)))
6466 readonly_error (location, lhs, lv_assign);
6467 return error_mark_node;
6469 else if (TREE_READONLY (lhs))
6470 readonly_warning (lhs, lv_assign);
6472 /* If storing into a structure or union member,
6473 it has probably been given type `int'.
6474 Compute the type that would go with
6475 the actual amount of storage the member occupies. */
6477 if (TREE_CODE (lhs) == COMPONENT_REF
6478 && (TREE_CODE (lhstype) == INTEGER_TYPE
6479 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6480 || TREE_CODE (lhstype) == REAL_TYPE
6481 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6482 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6484 /* If storing in a field that is in actuality a short or narrower than one,
6485 we must store in the field in its actual type. */
6487 if (lhstype != TREE_TYPE (lhs))
6489 lhs = copy_node (lhs);
6490 TREE_TYPE (lhs) = lhstype;
6493 /* Issue -Wc++-compat warnings about an assignment to an enum type
6494 when LHS does not have its original type. This happens for,
6495 e.g., an enum bitfield in a struct. */
6496 if (warn_cxx_compat
6497 && lhs_origtype != NULL_TREE
6498 && lhs_origtype != lhstype
6499 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6501 tree checktype = (rhs_origtype != NULL_TREE
6502 ? rhs_origtype
6503 : TREE_TYPE (rhs));
6504 if (checktype != error_mark_node
6505 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6506 || (is_atomic_op && modifycode != NOP_EXPR)))
6507 warning_at (location, OPT_Wc___compat,
6508 "enum conversion in assignment is invalid in C++");
6511 /* Remove qualifiers. */
6512 lhstype = build_qualified_type (lhstype, TYPE_UNQUALIFIED);
6513 olhstype = build_qualified_type (olhstype, TYPE_UNQUALIFIED);
6515 /* Convert new value to destination type. Fold it first, then
6516 restore any excess precision information, for the sake of
6517 conversion warnings. */
6519 if (!(is_atomic_op && modifycode != NOP_EXPR))
6521 tree rhs_semantic_type = NULL_TREE;
6522 if (!c_in_omp_for)
6524 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6526 rhs_semantic_type = TREE_TYPE (newrhs);
6527 newrhs = TREE_OPERAND (newrhs, 0);
6529 npc = null_pointer_constant_p (newrhs);
6530 newrhs = c_fully_fold (newrhs, false, NULL);
6531 if (rhs_semantic_type)
6532 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6534 else
6535 npc = null_pointer_constant_p (newrhs);
6536 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6537 rhs_origtype, ic_assign, npc,
6538 NULL_TREE, NULL_TREE, 0);
6539 if (TREE_CODE (newrhs) == ERROR_MARK)
6540 return error_mark_node;
6543 /* Emit ObjC write barrier, if necessary. */
6544 if (c_dialect_objc () && flag_objc_gc)
6546 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6547 if (result)
6549 protected_set_expr_location (result, location);
6550 goto return_result;
6554 /* Scan operands. */
6556 if (is_atomic_op)
6557 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6558 else
6560 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6561 TREE_SIDE_EFFECTS (result) = 1;
6562 protected_set_expr_location (result, location);
6565 /* If we got the LHS in a different type for storing in,
6566 convert the result back to the nominal type of LHS
6567 so that the value we return always has the same type
6568 as the LHS argument. */
6570 if (olhstype == TREE_TYPE (result))
6571 goto return_result;
6573 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6574 rhs_origtype, ic_assign, false, NULL_TREE,
6575 NULL_TREE, 0);
6576 protected_set_expr_location (result, location);
6578 return_result:
6579 if (rhseval)
6580 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6581 return result;
6584 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6585 This is used to implement -fplan9-extensions. */
6587 static bool
6588 find_anonymous_field_with_type (tree struct_type, tree type)
6590 tree field;
6591 bool found;
6593 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6594 found = false;
6595 for (field = TYPE_FIELDS (struct_type);
6596 field != NULL_TREE;
6597 field = TREE_CHAIN (field))
6599 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6600 ? c_build_qualified_type (TREE_TYPE (field),
6601 TYPE_QUAL_ATOMIC)
6602 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6603 if (DECL_NAME (field) == NULL
6604 && comptypes (type, fieldtype))
6606 if (found)
6607 return false;
6608 found = true;
6610 else if (DECL_NAME (field) == NULL
6611 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6612 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6614 if (found)
6615 return false;
6616 found = true;
6619 return found;
6622 /* RHS is an expression whose type is pointer to struct. If there is
6623 an anonymous field in RHS with type TYPE, then return a pointer to
6624 that field in RHS. This is used with -fplan9-extensions. This
6625 returns NULL if no conversion could be found. */
6627 static tree
6628 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6630 tree rhs_struct_type, lhs_main_type;
6631 tree field, found_field;
6632 bool found_sub_field;
6633 tree ret;
6635 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6636 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6637 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6639 gcc_assert (POINTER_TYPE_P (type));
6640 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6641 ? c_build_qualified_type (TREE_TYPE (type),
6642 TYPE_QUAL_ATOMIC)
6643 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6645 found_field = NULL_TREE;
6646 found_sub_field = false;
6647 for (field = TYPE_FIELDS (rhs_struct_type);
6648 field != NULL_TREE;
6649 field = TREE_CHAIN (field))
6651 if (DECL_NAME (field) != NULL_TREE
6652 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6653 continue;
6654 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6655 ? c_build_qualified_type (TREE_TYPE (field),
6656 TYPE_QUAL_ATOMIC)
6657 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6658 if (comptypes (lhs_main_type, fieldtype))
6660 if (found_field != NULL_TREE)
6661 return NULL_TREE;
6662 found_field = field;
6664 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6665 lhs_main_type))
6667 if (found_field != NULL_TREE)
6668 return NULL_TREE;
6669 found_field = field;
6670 found_sub_field = true;
6674 if (found_field == NULL_TREE)
6675 return NULL_TREE;
6677 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6678 build_fold_indirect_ref (rhs), found_field,
6679 NULL_TREE);
6680 ret = build_fold_addr_expr_loc (location, ret);
6682 if (found_sub_field)
6684 ret = convert_to_anonymous_field (location, type, ret);
6685 gcc_assert (ret != NULL_TREE);
6688 return ret;
6691 /* Issue an error message for a bad initializer component.
6692 GMSGID identifies the message.
6693 The component name is taken from the spelling stack. */
6695 static void ATTRIBUTE_GCC_DIAG (2,0)
6696 error_init (location_t loc, const char *gmsgid, ...)
6698 char *ofwhat;
6700 auto_diagnostic_group d;
6702 /* The gmsgid may be a format string with %< and %>. */
6703 va_list ap;
6704 va_start (ap, gmsgid);
6705 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6706 va_end (ap);
6708 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6709 if (*ofwhat && warned)
6710 inform (loc, "(near initialization for %qs)", ofwhat);
6713 /* Issue a pedantic warning for a bad initializer component. OPT is
6714 the option OPT_* (from options.h) controlling this warning or 0 if
6715 it is unconditionally given. GMSGID identifies the message. The
6716 component name is taken from the spelling stack. */
6718 static void ATTRIBUTE_GCC_DIAG (3,0)
6719 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6721 /* Use the location where a macro was expanded rather than where
6722 it was defined to make sure macros defined in system headers
6723 but used incorrectly elsewhere are diagnosed. */
6724 location_t exploc = expansion_point_location_if_in_system_header (loc);
6725 auto_diagnostic_group d;
6726 va_list ap;
6727 va_start (ap, gmsgid);
6728 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6729 va_end (ap);
6730 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6731 if (*ofwhat && warned)
6732 inform (exploc, "(near initialization for %qs)", ofwhat);
6735 /* Issue a warning for a bad initializer component.
6737 OPT is the OPT_W* value corresponding to the warning option that
6738 controls this warning. GMSGID identifies the message. The
6739 component name is taken from the spelling stack. */
6741 static void
6742 warning_init (location_t loc, int opt, const char *gmsgid)
6744 char *ofwhat;
6745 bool warned;
6747 auto_diagnostic_group d;
6749 /* Use the location where a macro was expanded rather than where
6750 it was defined to make sure macros defined in system headers
6751 but used incorrectly elsewhere are diagnosed. */
6752 location_t exploc = expansion_point_location_if_in_system_header (loc);
6754 /* The gmsgid may be a format string with %< and %>. */
6755 warned = warning_at (exploc, opt, gmsgid);
6756 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6757 if (*ofwhat && warned)
6758 inform (exploc, "(near initialization for %qs)", ofwhat);
6761 /* If TYPE is an array type and EXPR is a parenthesized string
6762 constant, warn if pedantic that EXPR is being used to initialize an
6763 object of type TYPE. */
6765 void
6766 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6768 if (pedantic
6769 && TREE_CODE (type) == ARRAY_TYPE
6770 && TREE_CODE (expr.value) == STRING_CST
6771 && expr.original_code != STRING_CST)
6772 pedwarn_init (loc, OPT_Wpedantic,
6773 "array initialized from parenthesized string constant");
6776 /* Attempt to locate the parameter with the given index within FNDECL,
6777 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6779 static location_t
6780 get_fndecl_argument_location (tree fndecl, int argnum)
6782 int i;
6783 tree param;
6785 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6786 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6787 i < argnum && param;
6788 i++, param = TREE_CHAIN (param))
6791 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6792 return DECL_SOURCE_LOCATION (FNDECL). */
6793 if (param == NULL)
6794 return DECL_SOURCE_LOCATION (fndecl);
6796 return DECL_SOURCE_LOCATION (param);
6799 /* Issue a note about a mismatching argument for parameter PARMNUM
6800 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6801 Attempt to issue the note at the pertinent parameter of the decl;
6802 failing that issue it at the location of FUNDECL; failing that
6803 issue it at PLOC. */
6805 static void
6806 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6807 tree expected_type, tree actual_type)
6809 location_t loc;
6810 if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6811 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6812 else
6813 loc = ploc;
6815 inform (loc,
6816 "expected %qT but argument is of type %qT",
6817 expected_type, actual_type);
6820 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6821 function FUNDECL declared without prototype to parameter PARMNUM of
6822 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6824 static void
6825 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6826 tree parmtype, tree argtype)
6828 tree_code parmcode = TREE_CODE (parmtype);
6829 tree_code argcode = TREE_CODE (argtype);
6830 tree promoted = c_type_promotes_to (argtype);
6832 /* Avoid warning for enum arguments that promote to an integer type
6833 of the same size/mode. */
6834 if (parmcode == INTEGER_TYPE
6835 && argcode == ENUMERAL_TYPE
6836 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6837 return;
6839 if ((parmcode == argcode
6840 || (parmcode == INTEGER_TYPE
6841 && argcode == ENUMERAL_TYPE))
6842 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6843 return;
6845 /* This diagnoses even signed/unsigned mismatches. Those might be
6846 safe in many cases but GCC may emit suboptimal code for them so
6847 warning on those cases drives efficiency improvements. */
6848 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6849 TYPE_MAIN_VARIANT (promoted) == argtype
6850 ? G_("%qD argument %d type is %qT where %qT is expected "
6851 "in a call to built-in function declared without "
6852 "prototype")
6853 : G_("%qD argument %d promotes to %qT where %qT is expected "
6854 "in a call to built-in function declared without "
6855 "prototype"),
6856 fundecl, parmnum, promoted, parmtype))
6857 inform (DECL_SOURCE_LOCATION (fundecl),
6858 "built-in %qD declared here",
6859 fundecl);
6862 /* Convert value RHS to type TYPE as preparation for an assignment to
6863 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6864 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6865 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6866 constant before any folding.
6867 The real work of conversion is done by `convert'.
6868 The purpose of this function is to generate error messages
6869 for assignments that are not allowed in C.
6870 ERRTYPE says whether it is argument passing, assignment,
6871 initialization or return.
6873 In the following example, '~' denotes where EXPR_LOC and '^' where
6874 LOCATION point to:
6876 f (var); [ic_argpass]
6877 ^ ~~~
6878 x = var; [ic_assign]
6879 ^ ~~~;
6880 int x = var; [ic_init]
6882 return x; [ic_return]
6885 FUNCTION is a tree for the function being called.
6886 PARMNUM is the number of the argument, for printing in error messages.
6887 WARNOPT may be set to a warning option to issue the corresponding warning
6888 rather than an error for invalid conversions. Used for calls to built-in
6889 functions declared without a prototype. */
6891 static tree
6892 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6893 tree rhs, tree origtype, enum impl_conv errtype,
6894 bool null_pointer_constant, tree fundecl,
6895 tree function, int parmnum, int warnopt /* = 0 */)
6897 enum tree_code codel = TREE_CODE (type);
6898 tree orig_rhs = rhs;
6899 tree rhstype;
6900 enum tree_code coder;
6901 tree rname = NULL_TREE;
6902 bool objc_ok = false;
6904 /* Use the expansion point location to handle cases such as user's
6905 function returning a wrong-type macro defined in a system header. */
6906 location = expansion_point_location_if_in_system_header (location);
6908 if (errtype == ic_argpass)
6910 tree selector;
6911 /* Change pointer to function to the function itself for
6912 diagnostics. */
6913 if (TREE_CODE (function) == ADDR_EXPR
6914 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6915 function = TREE_OPERAND (function, 0);
6917 /* Handle an ObjC selector specially for diagnostics. */
6918 selector = objc_message_selector ();
6919 rname = function;
6920 if (selector && parmnum > 2)
6922 rname = selector;
6923 parmnum -= 2;
6927 /* This macro is used to emit diagnostics to ensure that all format
6928 strings are complete sentences, visible to gettext and checked at
6929 compile time. */
6930 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6931 do { \
6932 switch (errtype) \
6934 case ic_argpass: \
6936 auto_diagnostic_group d; \
6937 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6938 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6940 break; \
6941 case ic_assign: \
6942 pedwarn (LOCATION, OPT, AS); \
6943 break; \
6944 case ic_init: \
6945 case ic_init_const: \
6946 pedwarn_init (LOCATION, OPT, IN); \
6947 break; \
6948 case ic_return: \
6949 pedwarn (LOCATION, OPT, RE); \
6950 break; \
6951 default: \
6952 gcc_unreachable (); \
6954 } while (0)
6956 /* This macro is used to emit diagnostics to ensure that all format
6957 strings are complete sentences, visible to gettext and checked at
6958 compile time. It can be called with 'pedwarn' or 'warning_at'. */
6959 #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6960 do { \
6961 switch (errtype) \
6963 case ic_argpass: \
6965 auto_diagnostic_group d; \
6966 if (PEDWARN) { \
6967 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6968 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6969 } else { \
6970 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6971 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6974 break; \
6975 case ic_assign: \
6976 if (PEDWARN) \
6977 pedwarn (LOCATION, OPT, AS, QUALS); \
6978 else \
6979 warning_at (LOCATION, OPT, AS, QUALS); \
6980 break; \
6981 case ic_init: \
6982 case ic_init_const: \
6983 if (PEDWARN) \
6984 pedwarn (LOCATION, OPT, IN, QUALS); \
6985 else \
6986 warning_at (LOCATION, OPT, IN, QUALS); \
6987 break; \
6988 case ic_return: \
6989 if (PEDWARN) \
6990 pedwarn (LOCATION, OPT, RE, QUALS); \
6991 else \
6992 warning_at (LOCATION, OPT, RE, QUALS); \
6993 break; \
6994 default: \
6995 gcc_unreachable (); \
6997 } while (0)
6999 /* This macro is used to emit diagnostics to ensure that all format
7000 strings are complete sentences, visible to gettext and checked at
7001 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
7002 extra parameter to enumerate qualifiers. */
7003 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
7004 WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
7007 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7008 rhs = TREE_OPERAND (rhs, 0);
7010 rhstype = TREE_TYPE (rhs);
7011 coder = TREE_CODE (rhstype);
7013 if (coder == ERROR_MARK)
7014 return error_mark_node;
7016 if (c_dialect_objc ())
7018 int parmno;
7020 switch (errtype)
7022 case ic_return:
7023 parmno = 0;
7024 break;
7026 case ic_assign:
7027 parmno = -1;
7028 break;
7030 case ic_init:
7031 case ic_init_const:
7032 parmno = -2;
7033 break;
7035 default:
7036 parmno = parmnum;
7037 break;
7040 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
7043 if (warn_cxx_compat)
7045 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
7046 if (checktype != error_mark_node
7047 && TREE_CODE (type) == ENUMERAL_TYPE
7048 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
7049 switch (errtype)
7051 case ic_argpass:
7052 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
7053 "passing argument %d of %qE is invalid in C++",
7054 parmnum, rname))
7055 inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
7056 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
7057 "expected %qT but argument is of type %qT",
7058 type, rhstype);
7059 break;
7060 case ic_assign:
7061 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
7062 "%qT in assignment is invalid in C++", rhstype, type);
7063 break;
7064 case ic_init:
7065 case ic_init_const:
7066 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
7067 "%qT to %qT in initialization is invalid in C++",
7068 rhstype, type);
7069 break;
7070 case ic_return:
7071 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
7072 "%qT in return is invalid in C++", rhstype, type);
7073 break;
7074 default:
7075 gcc_unreachable ();
7079 if (warn_enum_conversion)
7081 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
7082 if (checktype != error_mark_node
7083 && TREE_CODE (checktype) == ENUMERAL_TYPE
7084 && TREE_CODE (type) == ENUMERAL_TYPE
7085 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
7087 gcc_rich_location loc (location);
7088 warning_at (&loc, OPT_Wenum_conversion,
7089 "implicit conversion from %qT to %qT",
7090 checktype, type);
7094 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
7096 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7097 return rhs;
7100 if (coder == VOID_TYPE)
7102 /* Except for passing an argument to an unprototyped function,
7103 this is a constraint violation. When passing an argument to
7104 an unprototyped function, it is compile-time undefined;
7105 making it a constraint in that case was rejected in
7106 DR#252. */
7107 const char msg[] = "void value not ignored as it ought to be";
7108 if (warnopt)
7109 warning_at (location, warnopt, msg);
7110 else
7111 error_at (location, msg);
7112 return error_mark_node;
7114 rhs = require_complete_type (location, rhs);
7115 if (rhs == error_mark_node)
7116 return error_mark_node;
7118 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
7119 return error_mark_node;
7121 /* A non-reference type can convert to a reference. This handles
7122 va_start, va_copy and possibly port built-ins. */
7123 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
7125 if (!lvalue_p (rhs))
7127 const char msg[] = "cannot pass rvalue to reference parameter";
7128 if (warnopt)
7129 warning_at (location, warnopt, msg);
7130 else
7131 error_at (location, msg);
7132 return error_mark_node;
7134 if (!c_mark_addressable (rhs))
7135 return error_mark_node;
7136 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
7137 SET_EXPR_LOCATION (rhs, location);
7139 rhs = convert_for_assignment (location, expr_loc,
7140 build_pointer_type (TREE_TYPE (type)),
7141 rhs, origtype, errtype,
7142 null_pointer_constant, fundecl, function,
7143 parmnum, warnopt);
7144 if (rhs == error_mark_node)
7145 return error_mark_node;
7147 rhs = build1 (NOP_EXPR, type, rhs);
7148 SET_EXPR_LOCATION (rhs, location);
7149 return rhs;
7151 /* Some types can interconvert without explicit casts. */
7152 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
7153 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
7154 return convert (type, rhs);
7155 /* Arithmetic types all interconvert, and enum is treated like int. */
7156 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
7157 || codel == FIXED_POINT_TYPE
7158 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
7159 || codel == BOOLEAN_TYPE)
7160 && (coder == INTEGER_TYPE || coder == REAL_TYPE
7161 || coder == FIXED_POINT_TYPE
7162 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
7163 || coder == BOOLEAN_TYPE))
7165 if (warnopt && errtype == ic_argpass)
7166 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
7167 rhstype);
7169 bool save = in_late_binary_op;
7170 if (C_BOOLEAN_TYPE_P (type) || codel == COMPLEX_TYPE
7171 || (coder == REAL_TYPE
7172 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
7173 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
7174 in_late_binary_op = true;
7175 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
7176 ? expr_loc : location, type, orig_rhs,
7177 errtype == ic_init_const);
7178 in_late_binary_op = save;
7179 return ret;
7182 /* Aggregates in different TUs might need conversion. */
7183 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
7184 && codel == coder
7185 && comptypes (type, rhstype))
7186 return convert_and_check (expr_loc != UNKNOWN_LOCATION
7187 ? expr_loc : location, type, rhs);
7189 /* Conversion to a transparent union or record from its member types.
7190 This applies only to function arguments. */
7191 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
7192 && TYPE_TRANSPARENT_AGGR (type))
7193 && errtype == ic_argpass)
7195 tree memb, marginal_memb = NULL_TREE;
7197 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
7199 tree memb_type = TREE_TYPE (memb);
7201 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
7202 TYPE_MAIN_VARIANT (rhstype)))
7203 break;
7205 if (TREE_CODE (memb_type) != POINTER_TYPE)
7206 continue;
7208 if (coder == POINTER_TYPE)
7210 tree ttl = TREE_TYPE (memb_type);
7211 tree ttr = TREE_TYPE (rhstype);
7213 /* Any non-function converts to a [const][volatile] void *
7214 and vice versa; otherwise, targets must be the same.
7215 Meanwhile, the lhs target must have all the qualifiers of
7216 the rhs. */
7217 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7218 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7219 || comp_target_types (location, memb_type, rhstype))
7221 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
7222 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
7223 /* If this type won't generate any warnings, use it. */
7224 if (lquals == rquals
7225 || ((TREE_CODE (ttr) == FUNCTION_TYPE
7226 && TREE_CODE (ttl) == FUNCTION_TYPE)
7227 ? ((lquals | rquals) == rquals)
7228 : ((lquals | rquals) == lquals)))
7229 break;
7231 /* Keep looking for a better type, but remember this one. */
7232 if (!marginal_memb)
7233 marginal_memb = memb;
7237 /* Can convert integer zero to any pointer type. */
7238 if (null_pointer_constant)
7240 rhs = null_pointer_node;
7241 break;
7245 if (memb || marginal_memb)
7247 if (!memb)
7249 /* We have only a marginally acceptable member type;
7250 it needs a warning. */
7251 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
7252 tree ttr = TREE_TYPE (rhstype);
7254 /* Const and volatile mean something different for function
7255 types, so the usual warnings are not appropriate. */
7256 if (TREE_CODE (ttr) == FUNCTION_TYPE
7257 && TREE_CODE (ttl) == FUNCTION_TYPE)
7259 /* Because const and volatile on functions are
7260 restrictions that say the function will not do
7261 certain things, it is okay to use a const or volatile
7262 function where an ordinary one is wanted, but not
7263 vice-versa. */
7264 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7265 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7266 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7267 OPT_Wdiscarded_qualifiers,
7268 G_("passing argument %d of %qE "
7269 "makes %q#v qualified function "
7270 "pointer from unqualified"),
7271 G_("assignment makes %q#v qualified "
7272 "function pointer from "
7273 "unqualified"),
7274 G_("initialization makes %q#v qualified "
7275 "function pointer from "
7276 "unqualified"),
7277 G_("return makes %q#v qualified function "
7278 "pointer from unqualified"),
7279 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7281 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7282 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7283 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7284 OPT_Wdiscarded_qualifiers,
7285 G_("passing argument %d of %qE discards "
7286 "%qv qualifier from pointer target type"),
7287 G_("assignment discards %qv qualifier "
7288 "from pointer target type"),
7289 G_("initialization discards %qv qualifier "
7290 "from pointer target type"),
7291 G_("return discards %qv qualifier from "
7292 "pointer target type"),
7293 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7295 memb = marginal_memb;
7298 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7299 pedwarn (location, OPT_Wpedantic,
7300 "ISO C prohibits argument conversion to union type");
7302 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7303 return build_constructor_single (type, memb, rhs);
7307 /* Conversions among pointers */
7308 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7309 && (coder == codel))
7311 /* If RHS refers to a built-in declared without a prototype
7312 BLTIN is the declaration of the built-in with a prototype
7313 and RHSTYPE is set to the actual type of the built-in. */
7314 tree bltin;
7315 rhstype = type_or_builtin_type (rhs, &bltin);
7317 tree ttl = TREE_TYPE (type);
7318 tree ttr = TREE_TYPE (rhstype);
7319 tree mvl = ttl;
7320 tree mvr = ttr;
7321 bool is_opaque_pointer;
7322 int target_cmp = 0; /* Cache comp_target_types () result. */
7323 addr_space_t asl;
7324 addr_space_t asr;
7326 if (TREE_CODE (mvl) != ARRAY_TYPE)
7327 mvl = (TYPE_ATOMIC (mvl)
7328 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7329 TYPE_QUAL_ATOMIC)
7330 : TYPE_MAIN_VARIANT (mvl));
7331 if (TREE_CODE (mvr) != ARRAY_TYPE)
7332 mvr = (TYPE_ATOMIC (mvr)
7333 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7334 TYPE_QUAL_ATOMIC)
7335 : TYPE_MAIN_VARIANT (mvr));
7336 /* Opaque pointers are treated like void pointers. */
7337 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7339 /* The Plan 9 compiler permits a pointer to a struct to be
7340 automatically converted into a pointer to an anonymous field
7341 within the struct. */
7342 if (flag_plan9_extensions
7343 && RECORD_OR_UNION_TYPE_P (mvl)
7344 && RECORD_OR_UNION_TYPE_P (mvr)
7345 && mvl != mvr)
7347 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7348 if (new_rhs != NULL_TREE)
7350 rhs = new_rhs;
7351 rhstype = TREE_TYPE (rhs);
7352 coder = TREE_CODE (rhstype);
7353 ttr = TREE_TYPE (rhstype);
7354 mvr = TYPE_MAIN_VARIANT (ttr);
7358 /* C++ does not allow the implicit conversion void* -> T*. However,
7359 for the purpose of reducing the number of false positives, we
7360 tolerate the special case of
7362 int *p = NULL;
7364 where NULL is typically defined in C to be '(void *) 0'. */
7365 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7366 warning_at (errtype == ic_argpass ? expr_loc : location,
7367 OPT_Wc___compat,
7368 "request for implicit conversion "
7369 "from %qT to %qT not permitted in C++", rhstype, type);
7371 /* See if the pointers point to incompatible address spaces. */
7372 asl = TYPE_ADDR_SPACE (ttl);
7373 asr = TYPE_ADDR_SPACE (ttr);
7374 if (!null_pointer_constant_p (rhs)
7375 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7377 auto_diagnostic_group d;
7378 bool diagnosed = true;
7379 switch (errtype)
7381 case ic_argpass:
7383 const char msg[] = G_("passing argument %d of %qE from "
7384 "pointer to non-enclosed address space");
7385 if (warnopt)
7386 diagnosed
7387 = warning_at (expr_loc, warnopt, msg, parmnum, rname);
7388 else
7389 error_at (expr_loc, msg, parmnum, rname);
7390 break;
7392 case ic_assign:
7394 const char msg[] = G_("assignment from pointer to "
7395 "non-enclosed address space");
7396 if (warnopt)
7397 diagnosed = warning_at (location, warnopt, msg);
7398 else
7399 error_at (location, msg);
7400 break;
7402 case ic_init:
7403 case ic_init_const:
7405 const char msg[] = G_("initialization from pointer to "
7406 "non-enclosed address space");
7407 if (warnopt)
7408 diagnosed = warning_at (location, warnopt, msg);
7409 else
7410 error_at (location, msg);
7411 break;
7413 case ic_return:
7415 const char msg[] = G_("return from pointer to "
7416 "non-enclosed address space");
7417 if (warnopt)
7418 diagnosed = warning_at (location, warnopt, msg);
7419 else
7420 error_at (location, msg);
7421 break;
7423 default:
7424 gcc_unreachable ();
7426 if (diagnosed)
7428 if (errtype == ic_argpass)
7429 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7430 else
7431 inform (location, "expected %qT but pointer is of type %qT",
7432 type, rhstype);
7434 return error_mark_node;
7437 /* Check if the right-hand side has a format attribute but the
7438 left-hand side doesn't. */
7439 if (warn_suggest_attribute_format
7440 && check_missing_format_attribute (type, rhstype))
7442 switch (errtype)
7444 case ic_argpass:
7445 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7446 "argument %d of %qE might be "
7447 "a candidate for a format attribute",
7448 parmnum, rname);
7449 break;
7450 case ic_assign:
7451 warning_at (location, OPT_Wsuggest_attribute_format,
7452 "assignment left-hand side might be "
7453 "a candidate for a format attribute");
7454 break;
7455 case ic_init:
7456 case ic_init_const:
7457 warning_at (location, OPT_Wsuggest_attribute_format,
7458 "initialization left-hand side might be "
7459 "a candidate for a format attribute");
7460 break;
7461 case ic_return:
7462 warning_at (location, OPT_Wsuggest_attribute_format,
7463 "return type might be "
7464 "a candidate for a format attribute");
7465 break;
7466 default:
7467 gcc_unreachable ();
7471 /* See if the pointers point to incompatible scalar storage orders. */
7472 if (warn_scalar_storage_order
7473 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7474 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7476 tree t;
7478 switch (errtype)
7480 case ic_argpass:
7481 /* Do not warn for built-in functions, for example memcpy, since we
7482 control how they behave and they can be useful in this area. */
7483 if (TREE_CODE (rname) != FUNCTION_DECL
7484 || !fndecl_built_in_p (rname))
7485 warning_at (location, OPT_Wscalar_storage_order,
7486 "passing argument %d of %qE from incompatible "
7487 "scalar storage order", parmnum, rname);
7488 break;
7489 case ic_assign:
7490 /* Do not warn if the RHS is a call to a function that returns a
7491 pointer that is not an alias. */
7492 if (TREE_CODE (rhs) != CALL_EXPR
7493 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7494 || !DECL_IS_MALLOC (t))
7495 warning_at (location, OPT_Wscalar_storage_order,
7496 "assignment to %qT from pointer type %qT with "
7497 "incompatible scalar storage order", type, rhstype);
7498 break;
7499 case ic_init:
7500 case ic_init_const:
7501 /* Likewise. */
7502 if (TREE_CODE (rhs) != CALL_EXPR
7503 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7504 || !DECL_IS_MALLOC (t))
7505 warning_at (location, OPT_Wscalar_storage_order,
7506 "initialization of %qT from pointer type %qT with "
7507 "incompatible scalar storage order", type, rhstype);
7508 break;
7509 case ic_return:
7510 warning_at (location, OPT_Wscalar_storage_order,
7511 "returning %qT from pointer type with incompatible "
7512 "scalar storage order %qT", rhstype, type);
7513 break;
7514 default:
7515 gcc_unreachable ();
7519 /* Any non-function converts to a [const][volatile] void *
7520 and vice versa; otherwise, targets must be the same.
7521 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7522 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7523 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7524 || (target_cmp = comp_target_types (location, type, rhstype))
7525 || is_opaque_pointer
7526 || ((c_common_unsigned_type (mvl)
7527 == c_common_unsigned_type (mvr))
7528 && (c_common_signed_type (mvl)
7529 == c_common_signed_type (mvr))
7530 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7532 /* Warn about loss of qualifers from pointers to arrays with
7533 qualifiers on the element type. */
7534 if (TREE_CODE (ttr) == ARRAY_TYPE)
7536 ttr = strip_array_types (ttr);
7537 ttl = strip_array_types (ttl);
7539 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7540 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7541 WARNING_FOR_QUALIFIERS (flag_isoc2x,
7542 location, expr_loc,
7543 OPT_Wdiscarded_array_qualifiers,
7544 G_("passing argument %d of %qE discards "
7545 "%qv qualifier from pointer target type"),
7546 G_("assignment discards %qv qualifier "
7547 "from pointer target type"),
7548 G_("initialization discards %qv qualifier "
7549 "from pointer target type"),
7550 G_("return discards %qv qualifier from "
7551 "pointer target type"),
7552 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7554 else if (pedantic
7555 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7557 (VOID_TYPE_P (ttr)
7558 && !null_pointer_constant
7559 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7560 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7561 G_("ISO C forbids passing argument %d of "
7562 "%qE between function pointer "
7563 "and %<void *%>"),
7564 G_("ISO C forbids assignment between "
7565 "function pointer and %<void *%>"),
7566 G_("ISO C forbids initialization between "
7567 "function pointer and %<void *%>"),
7568 G_("ISO C forbids return between function "
7569 "pointer and %<void *%>"));
7570 /* Const and volatile mean something different for function types,
7571 so the usual warnings are not appropriate. */
7572 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7573 && TREE_CODE (ttl) != FUNCTION_TYPE)
7575 /* Assignments between atomic and non-atomic objects are OK. */
7576 bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7577 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
7578 bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7579 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
7581 /* Don't warn about loss of qualifier for conversions from
7582 qualified void* to pointers to arrays with corresponding
7583 qualifier on the element type (except for pedantic before C23). */
7584 if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc2x))
7585 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7586 OPT_Wdiscarded_qualifiers,
7587 G_("passing argument %d of %qE discards "
7588 "%qv qualifier from pointer target type"),
7589 G_("assignment discards %qv qualifier "
7590 "from pointer target type"),
7591 G_("initialization discards %qv qualifier "
7592 "from pointer target type"),
7593 G_("return discards %qv qualifier from "
7594 "pointer target type"),
7595 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7596 else if (warn_quals_ped)
7597 pedwarn_c11 (location, OPT_Wc11_c2x_compat,
7598 "array with qualifier on the element is not qualified before C2X");
7600 /* If this is not a case of ignoring a mismatch in signedness,
7601 no warning. */
7602 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7603 || target_cmp)
7605 /* If there is a mismatch, do warn. */
7606 else if (warn_pointer_sign)
7607 switch (errtype)
7609 case ic_argpass:
7611 auto_diagnostic_group d;
7612 range_label_for_type_mismatch rhs_label (rhstype, type);
7613 gcc_rich_location richloc (expr_loc, &rhs_label);
7614 if (pedwarn (&richloc, OPT_Wpointer_sign,
7615 "pointer targets in passing argument %d of "
7616 "%qE differ in signedness", parmnum, rname))
7617 inform_for_arg (fundecl, expr_loc, parmnum, type,
7618 rhstype);
7620 break;
7621 case ic_assign:
7622 pedwarn (location, OPT_Wpointer_sign,
7623 "pointer targets in assignment from %qT to %qT "
7624 "differ in signedness", rhstype, type);
7625 break;
7626 case ic_init:
7627 case ic_init_const:
7628 pedwarn_init (location, OPT_Wpointer_sign,
7629 "pointer targets in initialization of %qT "
7630 "from %qT differ in signedness", type,
7631 rhstype);
7632 break;
7633 case ic_return:
7634 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7635 "returning %qT from a function with return type "
7636 "%qT differ in signedness", rhstype, type);
7637 break;
7638 default:
7639 gcc_unreachable ();
7642 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7643 && TREE_CODE (ttr) == FUNCTION_TYPE)
7645 /* Because const and volatile on functions are restrictions
7646 that say the function will not do certain things,
7647 it is okay to use a const or volatile function
7648 where an ordinary one is wanted, but not vice-versa. */
7649 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7650 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7651 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7652 OPT_Wdiscarded_qualifiers,
7653 G_("passing argument %d of %qE makes "
7654 "%q#v qualified function pointer "
7655 "from unqualified"),
7656 G_("assignment makes %q#v qualified function "
7657 "pointer from unqualified"),
7658 G_("initialization makes %q#v qualified "
7659 "function pointer from unqualified"),
7660 G_("return makes %q#v qualified function "
7661 "pointer from unqualified"),
7662 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7665 /* Avoid warning about the volatile ObjC EH puts on decls. */
7666 else if (!objc_ok)
7668 switch (errtype)
7670 case ic_argpass:
7672 auto_diagnostic_group d;
7673 range_label_for_type_mismatch rhs_label (rhstype, type);
7674 gcc_rich_location richloc (expr_loc, &rhs_label);
7675 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7676 "passing argument %d of %qE from incompatible "
7677 "pointer type", parmnum, rname))
7678 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7680 break;
7681 case ic_assign:
7682 if (bltin)
7683 pedwarn (location, OPT_Wincompatible_pointer_types,
7684 "assignment to %qT from pointer to "
7685 "%qD with incompatible type %qT",
7686 type, bltin, rhstype);
7687 else
7688 pedwarn (location, OPT_Wincompatible_pointer_types,
7689 "assignment to %qT from incompatible pointer type %qT",
7690 type, rhstype);
7691 break;
7692 case ic_init:
7693 case ic_init_const:
7694 if (bltin)
7695 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7696 "initialization of %qT from pointer to "
7697 "%qD with incompatible type %qT",
7698 type, bltin, rhstype);
7699 else
7700 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7701 "initialization of %qT from incompatible "
7702 "pointer type %qT",
7703 type, rhstype);
7704 break;
7705 case ic_return:
7706 if (bltin)
7707 pedwarn (location, OPT_Wincompatible_pointer_types,
7708 "returning pointer to %qD of type %qT from "
7709 "a function with incompatible type %qT",
7710 bltin, rhstype, type);
7711 else
7712 pedwarn (location, OPT_Wincompatible_pointer_types,
7713 "returning %qT from a function with incompatible "
7714 "return type %qT", rhstype, type);
7715 break;
7716 default:
7717 gcc_unreachable ();
7721 /* If RHS isn't an address, check pointer or array of packed
7722 struct or union. */
7723 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7725 return convert (type, rhs);
7727 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7729 /* ??? This should not be an error when inlining calls to
7730 unprototyped functions. */
7731 const char msg[] = "invalid use of non-lvalue array";
7732 if (warnopt)
7733 warning_at (location, warnopt, msg);
7734 else
7735 error_at (location, msg);
7736 return error_mark_node;
7738 else if (codel == POINTER_TYPE
7739 && (coder == INTEGER_TYPE || coder == NULLPTR_TYPE))
7741 /* An explicit constant 0 or type nullptr_t can convert to a pointer,
7742 or one that results from arithmetic, even including a cast to
7743 integer type. */
7744 if (!null_pointer_constant && coder != NULLPTR_TYPE)
7745 switch (errtype)
7747 case ic_argpass:
7749 auto_diagnostic_group d;
7750 range_label_for_type_mismatch rhs_label (rhstype, type);
7751 gcc_rich_location richloc (expr_loc, &rhs_label);
7752 if (pedwarn (&richloc, OPT_Wint_conversion,
7753 "passing argument %d of %qE makes pointer from "
7754 "integer without a cast", parmnum, rname))
7755 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7757 break;
7758 case ic_assign:
7759 pedwarn (location, OPT_Wint_conversion,
7760 "assignment to %qT from %qT makes pointer from integer "
7761 "without a cast", type, rhstype);
7762 break;
7763 case ic_init:
7764 case ic_init_const:
7765 pedwarn_init (location, OPT_Wint_conversion,
7766 "initialization of %qT from %qT makes pointer from "
7767 "integer without a cast", type, rhstype);
7768 break;
7769 case ic_return:
7770 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7771 "function with return type %qT makes pointer from "
7772 "integer without a cast", rhstype, type);
7773 break;
7774 default:
7775 gcc_unreachable ();
7778 return convert (type, rhs);
7780 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7782 switch (errtype)
7784 case ic_argpass:
7786 auto_diagnostic_group d;
7787 range_label_for_type_mismatch rhs_label (rhstype, type);
7788 gcc_rich_location richloc (expr_loc, &rhs_label);
7789 if (pedwarn (&richloc, OPT_Wint_conversion,
7790 "passing argument %d of %qE makes integer from "
7791 "pointer without a cast", parmnum, rname))
7792 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7794 break;
7795 case ic_assign:
7796 pedwarn (location, OPT_Wint_conversion,
7797 "assignment to %qT from %qT makes integer from pointer "
7798 "without a cast", type, rhstype);
7799 break;
7800 case ic_init:
7801 case ic_init_const:
7802 pedwarn_init (location, OPT_Wint_conversion,
7803 "initialization of %qT from %qT makes integer from "
7804 "pointer without a cast", type, rhstype);
7805 break;
7806 case ic_return:
7807 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7808 "function with return type %qT makes integer from "
7809 "pointer without a cast", rhstype, type);
7810 break;
7811 default:
7812 gcc_unreachable ();
7815 return convert (type, rhs);
7817 else if (C_BOOLEAN_TYPE_P (type)
7818 /* The type nullptr_t may be converted to bool. The
7819 result is false. */
7820 && (coder == POINTER_TYPE || coder == NULLPTR_TYPE))
7822 tree ret;
7823 bool save = in_late_binary_op;
7824 in_late_binary_op = true;
7825 ret = convert (type, rhs);
7826 in_late_binary_op = save;
7827 return ret;
7829 else if (codel == NULLPTR_TYPE && null_pointer_constant)
7830 return convert (type, rhs);
7832 switch (errtype)
7834 case ic_argpass:
7836 auto_diagnostic_group d;
7837 range_label_for_type_mismatch rhs_label (rhstype, type);
7838 gcc_rich_location richloc (expr_loc, &rhs_label);
7839 const char msg[] = G_("incompatible type for argument %d of %qE");
7840 if (warnopt)
7841 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7842 else
7843 error_at (&richloc, msg, parmnum, rname);
7844 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7846 break;
7847 case ic_assign:
7849 const char msg[]
7850 = G_("incompatible types when assigning to type %qT from type %qT");
7851 if (warnopt)
7852 warning_at (expr_loc, 0, msg, type, rhstype);
7853 else
7854 error_at (expr_loc, msg, type, rhstype);
7855 break;
7857 case ic_init:
7858 case ic_init_const:
7860 const char msg[]
7861 = G_("incompatible types when initializing type %qT using type %qT");
7862 if (warnopt)
7863 warning_at (location, 0, msg, type, rhstype);
7864 else
7865 error_at (location, msg, type, rhstype);
7866 break;
7868 case ic_return:
7870 const char msg[]
7871 = G_("incompatible types when returning type %qT but %qT was expected");
7872 if (warnopt)
7873 warning_at (location, 0, msg, rhstype, type);
7874 else
7875 error_at (location, msg, rhstype, type);
7876 break;
7878 default:
7879 gcc_unreachable ();
7882 return error_mark_node;
7885 /* If VALUE is a compound expr all of whose expressions are constant, then
7886 return its value. Otherwise, return error_mark_node.
7888 This is for handling COMPOUND_EXPRs as initializer elements
7889 which is allowed with a warning when -pedantic is specified. */
7891 static tree
7892 valid_compound_expr_initializer (tree value, tree endtype)
7894 if (TREE_CODE (value) == COMPOUND_EXPR)
7896 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7897 == error_mark_node)
7898 return error_mark_node;
7899 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7900 endtype);
7902 else if (!initializer_constant_valid_p (value, endtype))
7903 return error_mark_node;
7904 else
7905 return value;
7908 /* Perform appropriate conversions on the initial value of a variable,
7909 store it in the declaration DECL,
7910 and print any error messages that are appropriate.
7911 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7912 If the init is invalid, store an ERROR_MARK.
7914 INIT_LOC is the location of the initial value. */
7916 void
7917 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7919 tree value, type;
7920 bool npc = false;
7921 bool int_const_expr = false;
7922 bool arith_const_expr = false;
7924 /* If variable's type was invalidly declared, just ignore it. */
7926 type = TREE_TYPE (decl);
7927 if (TREE_CODE (type) == ERROR_MARK)
7928 return;
7930 /* Digest the specified initializer into an expression. */
7932 if (init)
7934 npc = null_pointer_constant_p (init);
7935 int_const_expr = (TREE_CODE (init) == INTEGER_CST
7936 && !TREE_OVERFLOW (init)
7937 && INTEGRAL_TYPE_P (TREE_TYPE (init)));
7938 /* Not fully determined before folding. */
7939 arith_const_expr = true;
7941 bool constexpr_p = (TREE_CODE (decl) == VAR_DECL
7942 && C_DECL_DECLARED_CONSTEXPR (decl));
7943 value = digest_init (init_loc, type, init, origtype, npc, int_const_expr,
7944 arith_const_expr, true,
7945 TREE_STATIC (decl) || constexpr_p, constexpr_p);
7947 /* Store the expression if valid; else report error. */
7949 if (!in_system_header_at (input_location)
7950 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7951 warning (OPT_Wtraditional, "traditional C rejects automatic "
7952 "aggregate initialization");
7954 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7955 DECL_INITIAL (decl) = value;
7957 /* ANSI wants warnings about out-of-range constant initializers. */
7958 STRIP_TYPE_NOPS (value);
7959 if (TREE_STATIC (decl))
7960 constant_expression_warning (value);
7962 /* Check if we need to set array size from compound literal size. */
7963 if (TREE_CODE (type) == ARRAY_TYPE
7964 && TYPE_DOMAIN (type) == NULL_TREE
7965 && value != error_mark_node)
7967 tree inside_init = init;
7969 STRIP_TYPE_NOPS (inside_init);
7970 inside_init = fold (inside_init);
7972 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7974 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7976 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7978 /* For int foo[] = (int [3]){1}; we need to set array size
7979 now since later on array initializer will be just the
7980 brace enclosed list of the compound literal. */
7981 tree etype = strip_array_types (TREE_TYPE (decl));
7982 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7983 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7984 layout_type (type);
7985 layout_decl (cldecl, 0);
7986 TREE_TYPE (decl)
7987 = c_build_qualified_type (type, TYPE_QUALS (etype));
7993 /* Methods for storing and printing names for error messages. */
7995 /* Implement a spelling stack that allows components of a name to be pushed
7996 and popped. Each element on the stack is this structure. */
7998 struct spelling
8000 int kind;
8001 union
8003 unsigned HOST_WIDE_INT i;
8004 const char *s;
8005 } u;
8008 #define SPELLING_STRING 1
8009 #define SPELLING_MEMBER 2
8010 #define SPELLING_BOUNDS 3
8012 static struct spelling *spelling; /* Next stack element (unused). */
8013 static struct spelling *spelling_base; /* Spelling stack base. */
8014 static int spelling_size; /* Size of the spelling stack. */
8016 /* Macros to save and restore the spelling stack around push_... functions.
8017 Alternative to SAVE_SPELLING_STACK. */
8019 #define SPELLING_DEPTH() (spelling - spelling_base)
8020 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
8022 /* Push an element on the spelling stack with type KIND and assign VALUE
8023 to MEMBER. */
8025 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
8027 int depth = SPELLING_DEPTH (); \
8029 if (depth >= spelling_size) \
8031 spelling_size += 10; \
8032 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
8033 spelling_size); \
8034 RESTORE_SPELLING_DEPTH (depth); \
8037 spelling->kind = (KIND); \
8038 spelling->MEMBER = (VALUE); \
8039 spelling++; \
8042 /* Push STRING on the stack. Printed literally. */
8044 static void
8045 push_string (const char *string)
8047 PUSH_SPELLING (SPELLING_STRING, string, u.s);
8050 /* Push a member name on the stack. Printed as '.' STRING. */
8052 static void
8053 push_member_name (tree decl)
8055 const char *const string
8056 = (DECL_NAME (decl)
8057 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
8058 : _("<anonymous>"));
8059 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
8062 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
8064 static void
8065 push_array_bounds (unsigned HOST_WIDE_INT bounds)
8067 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
8070 /* Compute the maximum size in bytes of the printed spelling. */
8072 static int
8073 spelling_length (void)
8075 int size = 0;
8076 struct spelling *p;
8078 for (p = spelling_base; p < spelling; p++)
8080 if (p->kind == SPELLING_BOUNDS)
8081 size += 25;
8082 else
8083 size += strlen (p->u.s) + 1;
8086 return size;
8089 /* Print the spelling to BUFFER and return it. */
8091 static char *
8092 print_spelling (char *buffer)
8094 char *d = buffer;
8095 struct spelling *p;
8097 for (p = spelling_base; p < spelling; p++)
8098 if (p->kind == SPELLING_BOUNDS)
8100 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
8101 d += strlen (d);
8103 else
8105 const char *s;
8106 if (p->kind == SPELLING_MEMBER)
8107 *d++ = '.';
8108 for (s = p->u.s; (*d = *s++); d++)
8111 *d++ = '\0';
8112 return buffer;
8115 /* Check whether INIT, a floating or integer constant, is
8116 representable in TYPE, a real floating type with the same radix or
8117 a decimal floating type initialized with a binary floating
8118 constant. Return true if OK, false if not. */
8119 static bool
8120 constexpr_init_fits_real_type (tree type, tree init)
8122 gcc_assert (TREE_CODE (type) == REAL_TYPE);
8123 gcc_assert (TREE_CODE (init) == INTEGER_CST || TREE_CODE (init) == REAL_CST);
8124 if (TREE_CODE (init) == REAL_CST
8125 && TYPE_MODE (TREE_TYPE (init)) == TYPE_MODE (type))
8127 /* Same mode, no conversion required except for the case of
8128 signaling NaNs if the types are incompatible (e.g. double and
8129 long double with the same mode). */
8130 if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init))
8131 && !comptypes (TYPE_MAIN_VARIANT (type),
8132 TYPE_MAIN_VARIANT (TREE_TYPE (init))))
8133 return false;
8134 return true;
8136 if (TREE_CODE (init) == INTEGER_CST)
8138 tree converted = build_real_from_int_cst (type, init);
8139 bool fail = false;
8140 wide_int w = real_to_integer (&TREE_REAL_CST (converted), &fail,
8141 TYPE_PRECISION (TREE_TYPE (init)));
8142 return !fail && wi::eq_p (w, wi::to_wide (init));
8144 if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init)))
8145 return false;
8146 if ((REAL_VALUE_ISINF (TREE_REAL_CST (init))
8147 && MODE_HAS_INFINITIES (TYPE_MODE (type)))
8148 || (REAL_VALUE_ISNAN (TREE_REAL_CST (init))
8149 && MODE_HAS_NANS (TYPE_MODE (type))))
8150 return true;
8151 if (DECIMAL_FLOAT_TYPE_P (type)
8152 && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init)))
8154 /* This is valid if the real number represented by the
8155 initializer can be exactly represented in the decimal
8156 type. Compare the values using MPFR. */
8157 REAL_VALUE_TYPE t;
8158 real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
8159 mpfr_t bin_val, dec_val;
8160 mpfr_init2 (bin_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
8161 mpfr_init2 (dec_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
8162 mpfr_from_real (bin_val, &TREE_REAL_CST (init), MPFR_RNDN);
8163 char string[256];
8164 real_to_decimal (string, &t, sizeof string, 0, 1);
8165 bool res = (mpfr_strtofr (dec_val, string, NULL, 10, MPFR_RNDN) == 0
8166 && mpfr_equal_p (bin_val, dec_val));
8167 mpfr_clear (bin_val);
8168 mpfr_clear (dec_val);
8169 return res;
8171 /* exact_real_truncate is not quite right here, since it doesn't
8172 allow even an exact conversion to subnormal values. */
8173 REAL_VALUE_TYPE t;
8174 real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
8175 return real_identical (&t, &TREE_REAL_CST (init));
8178 /* Check whether INIT (location LOC) is valid as a 'constexpr'
8179 initializer for type TYPE, and give an error if not. INIT has
8180 already been folded and verified to be constant. INT_CONST_EXPR
8181 and ARITH_CONST_EXPR say whether it is an integer constant
8182 expression or arithmetic constant expression, respectively. If
8183 TYPE is not a scalar type, this function does nothing. */
8185 static void
8186 check_constexpr_init (location_t loc, tree type, tree init,
8187 bool int_const_expr, bool arith_const_expr)
8189 if (POINTER_TYPE_P (type))
8191 /* The initializer must be null. */
8192 if (TREE_CODE (init) != INTEGER_CST || !integer_zerop (init))
8193 error_at (loc, "%<constexpr%> pointer initializer is not null");
8194 return;
8196 if (INTEGRAL_TYPE_P (type))
8198 /* The initializer must be an integer constant expression,
8199 representable in the target type. */
8200 if (!int_const_expr)
8201 error_at (loc, "%<constexpr%> integer initializer is not an "
8202 "integer constant expression");
8203 if (!int_fits_type_p (init, type))
8204 error_at (loc, "%<constexpr%> initializer not representable in "
8205 "type of object");
8206 return;
8208 /* We don't apply any extra checks to extension types such as vector
8209 or fixed-point types. */
8210 if (TREE_CODE (type) != REAL_TYPE && TREE_CODE (type) != COMPLEX_TYPE)
8211 return;
8212 if (!arith_const_expr)
8214 error_at (loc, "%<constexpr%> initializer is not an arithmetic "
8215 "constant expression");
8216 return;
8218 /* We don't apply any extra checks to complex integers. */
8219 if (TREE_CODE (type) == COMPLEX_TYPE
8220 && TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8221 return;
8222 /* Following N3082, a real type cannot be initialized from a complex
8223 type and a binary type cannot be initialized from a decimal type
8224 (but initializing a decimal type from a binary type is OK).
8225 Signaling NaN initializers are OK only if the types are
8226 compatible (not just the same mode); all quiet NaN and infinity
8227 initializations are considered to preserve the value. */
8228 if (TREE_CODE (TREE_TYPE (init)) == COMPLEX_TYPE
8229 && TREE_CODE (type) == REAL_TYPE)
8231 error_at (loc, "%<constexpr%> initializer for a real type is of "
8232 "complex type");
8233 return;
8235 if (TREE_CODE (type) == REAL_TYPE
8236 && TREE_CODE (TREE_TYPE (init)) == REAL_TYPE
8237 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init))
8238 && !DECIMAL_FLOAT_TYPE_P (type))
8240 error_at (loc, "%<constexpr%> initializer for a binary "
8241 "floating-point type is of decimal type");
8242 return;
8244 bool fits;
8245 if (TREE_CODE (type) == COMPLEX_TYPE)
8247 switch (TREE_CODE (init))
8249 case INTEGER_CST:
8250 case REAL_CST:
8251 fits = constexpr_init_fits_real_type (TREE_TYPE (type), init);
8252 break;
8253 case COMPLEX_CST:
8254 fits = (constexpr_init_fits_real_type (TREE_TYPE (type),
8255 TREE_REALPART (init))
8256 && constexpr_init_fits_real_type (TREE_TYPE (type),
8257 TREE_IMAGPART (init)));
8258 break;
8259 default:
8260 gcc_unreachable ();
8263 else
8264 fits = constexpr_init_fits_real_type (type, init);
8265 if (!fits)
8266 error_at (loc, "%<constexpr%> initializer not representable in "
8267 "type of object");
8270 /* Digest the parser output INIT as an initializer for type TYPE.
8271 Return a C expression of type TYPE to represent the initial value.
8273 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
8275 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant,
8276 INT_CONST_EXPR is true if INIT is an integer constant expression,
8277 and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic
8278 constant expression, false if it has already been determined in the
8279 caller that it is not (but folding may have made the value passed here
8280 indistinguishable from an arithmetic constant expression).
8282 If INIT is a string constant, STRICT_STRING is true if it is
8283 unparenthesized or we should not warn here for it being parenthesized.
8284 For other types of INIT, STRICT_STRING is not used.
8286 INIT_LOC is the location of the INIT.
8288 REQUIRE_CONSTANT requests an error if non-constant initializers or
8289 elements are seen. REQUIRE_CONSTEXPR means the stricter requirements
8290 on initializers for 'constexpr' objects apply. */
8292 static tree
8293 digest_init (location_t init_loc, tree type, tree init, tree origtype,
8294 bool null_pointer_constant, bool int_const_expr,
8295 bool arith_const_expr, bool strict_string,
8296 bool require_constant, bool require_constexpr)
8298 enum tree_code code = TREE_CODE (type);
8299 tree inside_init = init;
8300 tree semantic_type = NULL_TREE;
8301 bool maybe_const = true;
8303 if (type == error_mark_node
8304 || !init
8305 || error_operand_p (init))
8306 return error_mark_node;
8308 STRIP_TYPE_NOPS (inside_init);
8310 if (!c_in_omp_for)
8312 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
8314 semantic_type = TREE_TYPE (inside_init);
8315 inside_init = TREE_OPERAND (inside_init, 0);
8317 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
8319 /* TODO: this may not detect all cases of expressions folding to
8320 constants that are not arithmetic constant expressions. */
8321 if (!maybe_const)
8322 arith_const_expr = false;
8323 else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init))
8324 && TREE_CODE (TREE_TYPE (inside_init)) != REAL_TYPE
8325 && TREE_CODE (TREE_TYPE (inside_init)) != COMPLEX_TYPE)
8326 arith_const_expr = false;
8327 else if (TREE_CODE (inside_init) != INTEGER_CST
8328 && TREE_CODE (inside_init) != REAL_CST
8329 && TREE_CODE (inside_init) != COMPLEX_CST)
8330 arith_const_expr = false;
8331 else if (TREE_OVERFLOW (inside_init))
8332 arith_const_expr = false;
8334 /* Initialization of an array of chars from a string constant
8335 optionally enclosed in braces. */
8337 if (code == ARRAY_TYPE && inside_init
8338 && TREE_CODE (inside_init) == STRING_CST)
8340 tree typ1
8341 = (TYPE_ATOMIC (TREE_TYPE (type))
8342 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
8343 TYPE_QUAL_ATOMIC)
8344 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
8345 /* Note that an array could be both an array of character type
8346 and an array of wchar_t if wchar_t is signed char or unsigned
8347 char. */
8348 bool char_array = (typ1 == char_type_node
8349 || typ1 == signed_char_type_node
8350 || typ1 == unsigned_char_type_node);
8351 bool wchar_array = !!comptypes (typ1, wchar_type_node);
8352 bool char16_array = !!comptypes (typ1, char16_type_node);
8353 bool char32_array = !!comptypes (typ1, char32_type_node);
8355 if (char_array || wchar_array || char16_array || char32_array)
8357 struct c_expr expr;
8358 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
8359 bool incompat_string_cst = false;
8360 expr.value = inside_init;
8361 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
8362 expr.original_type = NULL;
8363 expr.m_decimal = 0;
8364 maybe_warn_string_init (init_loc, type, expr);
8366 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8367 pedwarn_init (init_loc, OPT_Wpedantic,
8368 "initialization of a flexible array member");
8370 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8371 TYPE_MAIN_VARIANT (type)))
8372 return inside_init;
8374 if (char_array)
8376 if (typ2 != char_type_node && typ2 != char8_type_node)
8377 incompat_string_cst = true;
8379 else if (!comptypes (typ1, typ2))
8380 incompat_string_cst = true;
8382 if (incompat_string_cst)
8384 error_init (init_loc, "cannot initialize array of %qT from "
8385 "a string literal with type array of %qT",
8386 typ1, typ2);
8387 return error_mark_node;
8390 if (require_constexpr
8391 && TYPE_UNSIGNED (typ1) != TYPE_UNSIGNED (typ2))
8393 /* Check if all characters of the string can be
8394 represented in the type of the constexpr object being
8395 initialized. */
8396 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8397 const unsigned char *p =
8398 (const unsigned char *) TREE_STRING_POINTER (inside_init);
8399 gcc_assert (CHAR_TYPE_SIZE == 8 && CHAR_BIT == 8);
8400 for (unsigned i = 0; i < len; i++)
8401 if (p[i] > 127)
8403 error_init (init_loc, "%<constexpr%> initializer not "
8404 "representable in type of object");
8405 break;
8409 if (TYPE_DOMAIN (type) != NULL_TREE
8410 && TYPE_SIZE (type) != NULL_TREE
8411 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8413 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8414 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
8416 /* Subtract the size of a single (possibly wide) character
8417 because it's ok to ignore the terminating null char
8418 that is counted in the length of the constant. */
8419 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
8420 pedwarn_init (init_loc, 0,
8421 ("initializer-string for array of %qT "
8422 "is too long"), typ1);
8423 else if (warn_cxx_compat
8424 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8425 warning_at (init_loc, OPT_Wc___compat,
8426 ("initializer-string for array of %qT "
8427 "is too long for C++"), typ1);
8428 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8430 unsigned HOST_WIDE_INT size
8431 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8432 const char *p = TREE_STRING_POINTER (inside_init);
8434 inside_init = build_string (size, p);
8438 TREE_TYPE (inside_init) = type;
8439 return inside_init;
8441 else if (INTEGRAL_TYPE_P (typ1))
8443 error_init (init_loc, "array of inappropriate type initialized "
8444 "from string constant");
8445 return error_mark_node;
8449 /* Build a VECTOR_CST from a *constant* vector constructor. If the
8450 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
8451 below and handle as a constructor. */
8452 if (code == VECTOR_TYPE
8453 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
8454 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
8455 && TREE_CONSTANT (inside_init))
8457 if (TREE_CODE (inside_init) == VECTOR_CST
8458 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8459 TYPE_MAIN_VARIANT (type)))
8460 return inside_init;
8462 if (TREE_CODE (inside_init) == CONSTRUCTOR)
8464 unsigned HOST_WIDE_INT ix;
8465 tree value;
8466 bool constant_p = true;
8468 /* Iterate through elements and check if all constructor
8469 elements are *_CSTs. */
8470 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
8471 if (!CONSTANT_CLASS_P (value))
8473 constant_p = false;
8474 break;
8477 if (constant_p)
8478 return build_vector_from_ctor (type,
8479 CONSTRUCTOR_ELTS (inside_init));
8483 if (warn_sequence_point)
8484 verify_sequence_points (inside_init);
8486 /* Any type can be initialized
8487 from an expression of the same type, optionally with braces. */
8489 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
8490 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8491 TYPE_MAIN_VARIANT (type))
8492 || (code == ARRAY_TYPE
8493 && comptypes (TREE_TYPE (inside_init), type))
8494 || (gnu_vector_type_p (type)
8495 && comptypes (TREE_TYPE (inside_init), type))
8496 || (code == POINTER_TYPE
8497 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
8498 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
8499 TREE_TYPE (type)))))
8501 if (code == POINTER_TYPE)
8503 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
8505 if (TREE_CODE (inside_init) == STRING_CST
8506 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8507 inside_init = array_to_pointer_conversion
8508 (init_loc, inside_init);
8509 else
8511 error_init (init_loc, "invalid use of non-lvalue array");
8512 return error_mark_node;
8517 if (code == VECTOR_TYPE)
8518 /* Although the types are compatible, we may require a
8519 conversion. */
8520 inside_init = convert (type, inside_init);
8522 if (require_constant
8523 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8525 /* As an extension, allow initializing objects with static storage
8526 duration with compound literals (which are then treated just as
8527 the brace enclosed list they contain). Also allow this for
8528 vectors, as we can only assign them with compound literals. */
8529 if (flag_isoc99 && code != VECTOR_TYPE)
8530 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
8531 "is not constant");
8532 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8533 inside_init = DECL_INITIAL (decl);
8536 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
8537 && TREE_CODE (inside_init) != CONSTRUCTOR)
8539 error_init (init_loc, "array initialized from non-constant array "
8540 "expression");
8541 return error_mark_node;
8544 /* Compound expressions can only occur here if -Wpedantic or
8545 -pedantic-errors is specified. In the later case, we always want
8546 an error. In the former case, we simply want a warning. */
8547 if (require_constant && pedantic
8548 && TREE_CODE (inside_init) == COMPOUND_EXPR)
8550 inside_init
8551 = valid_compound_expr_initializer (inside_init,
8552 TREE_TYPE (inside_init));
8553 if (inside_init == error_mark_node)
8554 error_init (init_loc, "initializer element is not constant");
8555 else
8556 pedwarn_init (init_loc, OPT_Wpedantic,
8557 "initializer element is not constant");
8558 if (flag_pedantic_errors)
8559 inside_init = error_mark_node;
8561 else if (require_constant
8562 && !initializer_constant_valid_p (inside_init,
8563 TREE_TYPE (inside_init)))
8565 error_init (init_loc, "initializer element is not constant");
8566 inside_init = error_mark_node;
8568 else if (require_constant && !maybe_const)
8569 pedwarn_init (init_loc, OPT_Wpedantic,
8570 "initializer element is not a constant expression");
8571 else if (require_constexpr)
8572 check_constexpr_init (init_loc, type, inside_init,
8573 int_const_expr, arith_const_expr);
8575 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8576 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8577 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8578 type, inside_init, origtype,
8579 (require_constant
8580 ? ic_init_const
8581 : ic_init), null_pointer_constant,
8582 NULL_TREE, NULL_TREE, 0);
8583 return inside_init;
8586 /* Handle scalar types, including conversions. */
8588 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8589 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8590 || code == COMPLEX_TYPE || code == VECTOR_TYPE || code == NULLPTR_TYPE)
8592 tree unconverted_init = inside_init;
8593 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8594 && (TREE_CODE (init) == STRING_CST
8595 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8596 inside_init = init = array_to_pointer_conversion (init_loc, init);
8597 if (semantic_type)
8598 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8599 inside_init);
8600 inside_init
8601 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8602 inside_init, origtype,
8603 require_constant ? ic_init_const : ic_init,
8604 null_pointer_constant, NULL_TREE, NULL_TREE,
8607 /* Check to see if we have already given an error message. */
8608 if (inside_init == error_mark_node)
8610 else if (require_constant && !TREE_CONSTANT (inside_init))
8612 error_init (init_loc, "initializer element is not constant");
8613 inside_init = error_mark_node;
8615 else if (require_constant
8616 && !initializer_constant_valid_p (inside_init,
8617 TREE_TYPE (inside_init)))
8619 error_init (init_loc, "initializer element is not computable at "
8620 "load time");
8621 inside_init = error_mark_node;
8623 else if (require_constant && !maybe_const)
8624 pedwarn_init (init_loc, OPT_Wpedantic,
8625 "initializer element is not a constant expression");
8626 else if (require_constexpr)
8627 check_constexpr_init (init_loc, type, unconverted_init,
8628 int_const_expr, arith_const_expr);
8630 return inside_init;
8633 /* Come here only for records and arrays. */
8635 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8637 error_init (init_loc,
8638 "variable-sized object may not be initialized except "
8639 "with an empty initializer");
8640 return error_mark_node;
8643 error_init (init_loc, "invalid initializer");
8644 return error_mark_node;
8647 /* Handle initializers that use braces. */
8649 /* Type of object we are accumulating a constructor for.
8650 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8651 static tree constructor_type;
8653 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8654 left to fill. */
8655 static tree constructor_fields;
8657 /* For an ARRAY_TYPE, this is the specified index
8658 at which to store the next element we get. */
8659 static tree constructor_index;
8661 /* For an ARRAY_TYPE, this is the maximum index. */
8662 static tree constructor_max_index;
8664 /* For a RECORD_TYPE, this is the first field not yet written out. */
8665 static tree constructor_unfilled_fields;
8667 /* For an ARRAY_TYPE, this is the index of the first element
8668 not yet written out. */
8669 static tree constructor_unfilled_index;
8671 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8672 This is so we can generate gaps between fields, when appropriate. */
8673 static tree constructor_bit_index;
8675 /* If we are saving up the elements rather than allocating them,
8676 this is the list of elements so far (in reverse order,
8677 most recent first). */
8678 static vec<constructor_elt, va_gc> *constructor_elements;
8680 /* 1 if constructor should be incrementally stored into a constructor chain,
8681 0 if all the elements should be kept in AVL tree. */
8682 static int constructor_incremental;
8684 /* 1 if so far this constructor's elements are all compile-time constants. */
8685 static int constructor_constant;
8687 /* 1 if so far this constructor's elements are all valid address constants. */
8688 static int constructor_simple;
8690 /* 1 if this constructor has an element that cannot be part of a
8691 constant expression. */
8692 static int constructor_nonconst;
8694 /* 1 if this constructor is erroneous so far. */
8695 static int constructor_erroneous;
8697 /* 1 if this constructor is the universal zero initializer { 0 }. */
8698 static int constructor_zeroinit;
8700 /* Structure for managing pending initializer elements, organized as an
8701 AVL tree. */
8703 struct init_node
8705 struct init_node *left, *right;
8706 struct init_node *parent;
8707 int balance;
8708 tree purpose;
8709 tree value;
8710 tree origtype;
8713 /* Tree of pending elements at this constructor level.
8714 These are elements encountered out of order
8715 which belong at places we haven't reached yet in actually
8716 writing the output.
8717 Will never hold tree nodes across GC runs. */
8718 static struct init_node *constructor_pending_elts;
8720 /* The SPELLING_DEPTH of this constructor. */
8721 static int constructor_depth;
8723 /* DECL node for which an initializer is being read.
8724 0 means we are reading a constructor expression
8725 such as (struct foo) {...}. */
8726 static tree constructor_decl;
8728 /* Nonzero if there were any member designators in this initializer. */
8729 static int constructor_designated;
8731 /* Nesting depth of designator list. */
8732 static int designator_depth;
8734 /* Nonzero if there were diagnosed errors in this designator list. */
8735 static int designator_erroneous;
8738 /* This stack has a level for each implicit or explicit level of
8739 structuring in the initializer, including the outermost one. It
8740 saves the values of most of the variables above. */
8742 struct constructor_range_stack;
8744 struct constructor_stack
8746 struct constructor_stack *next;
8747 tree type;
8748 tree fields;
8749 tree index;
8750 tree max_index;
8751 tree unfilled_index;
8752 tree unfilled_fields;
8753 tree bit_index;
8754 vec<constructor_elt, va_gc> *elements;
8755 struct init_node *pending_elts;
8756 int offset;
8757 int depth;
8758 /* If value nonzero, this value should replace the entire
8759 constructor at this level. */
8760 struct c_expr replacement_value;
8761 struct constructor_range_stack *range_stack;
8762 char constant;
8763 char simple;
8764 char nonconst;
8765 char implicit;
8766 char erroneous;
8767 char outer;
8768 char incremental;
8769 char designated;
8770 int designator_depth;
8773 static struct constructor_stack *constructor_stack;
8775 /* This stack represents designators from some range designator up to
8776 the last designator in the list. */
8778 struct constructor_range_stack
8780 struct constructor_range_stack *next, *prev;
8781 struct constructor_stack *stack;
8782 tree range_start;
8783 tree index;
8784 tree range_end;
8785 tree fields;
8788 static struct constructor_range_stack *constructor_range_stack;
8790 /* This stack records separate initializers that are nested.
8791 Nested initializers can't happen in ANSI C, but GNU C allows them
8792 in cases like { ... (struct foo) { ... } ... }. */
8794 struct initializer_stack
8796 struct initializer_stack *next;
8797 tree decl;
8798 struct constructor_stack *constructor_stack;
8799 struct constructor_range_stack *constructor_range_stack;
8800 vec<constructor_elt, va_gc> *elements;
8801 struct spelling *spelling;
8802 struct spelling *spelling_base;
8803 int spelling_size;
8804 char require_constant_value;
8805 char require_constant_elements;
8806 char require_constexpr_value;
8807 char designated;
8808 rich_location *missing_brace_richloc;
8811 static struct initializer_stack *initializer_stack;
8813 /* Prepare to parse and output the initializer for variable DECL. */
8815 void
8816 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED,
8817 bool init_require_constant, bool init_require_constexpr,
8818 rich_location *richloc)
8820 const char *locus;
8821 struct initializer_stack *p = XNEW (struct initializer_stack);
8823 p->decl = constructor_decl;
8824 p->require_constant_value = require_constant_value;
8825 p->require_constant_elements = require_constant_elements;
8826 p->require_constexpr_value = require_constexpr_value;
8827 p->constructor_stack = constructor_stack;
8828 p->constructor_range_stack = constructor_range_stack;
8829 p->elements = constructor_elements;
8830 p->spelling = spelling;
8831 p->spelling_base = spelling_base;
8832 p->spelling_size = spelling_size;
8833 p->next = initializer_stack;
8834 p->missing_brace_richloc = richloc;
8835 p->designated = constructor_designated;
8836 initializer_stack = p;
8838 constructor_decl = decl;
8839 constructor_designated = 0;
8841 require_constant_value = init_require_constant;
8842 require_constexpr_value = init_require_constexpr;
8843 if (decl != NULL_TREE && decl != error_mark_node)
8845 require_constant_elements
8846 = ((init_require_constant || (pedantic && !flag_isoc99))
8847 /* For a scalar, you can always use any value to initialize,
8848 even within braces. */
8849 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8850 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8852 else
8854 require_constant_elements = false;
8855 locus = _("(anonymous)");
8858 constructor_stack = 0;
8859 constructor_range_stack = 0;
8861 found_missing_braces = 0;
8863 spelling_base = 0;
8864 spelling_size = 0;
8865 RESTORE_SPELLING_DEPTH (0);
8867 if (locus)
8868 push_string (locus);
8871 void
8872 finish_init (void)
8874 struct initializer_stack *p = initializer_stack;
8876 /* Free the whole constructor stack of this initializer. */
8877 while (constructor_stack)
8879 struct constructor_stack *q = constructor_stack;
8880 constructor_stack = q->next;
8881 XDELETE (q);
8884 gcc_assert (!constructor_range_stack);
8886 /* Pop back to the data of the outer initializer (if any). */
8887 XDELETE (spelling_base);
8889 constructor_decl = p->decl;
8890 require_constant_value = p->require_constant_value;
8891 require_constant_elements = p->require_constant_elements;
8892 require_constexpr_value = p->require_constexpr_value;
8893 constructor_stack = p->constructor_stack;
8894 constructor_designated = p->designated;
8895 constructor_range_stack = p->constructor_range_stack;
8896 constructor_elements = p->elements;
8897 spelling = p->spelling;
8898 spelling_base = p->spelling_base;
8899 spelling_size = p->spelling_size;
8900 initializer_stack = p->next;
8901 XDELETE (p);
8904 /* Call here when we see the initializer is surrounded by braces.
8905 This is instead of a call to push_init_level;
8906 it is matched by a call to pop_init_level.
8908 TYPE is the type to initialize, for a constructor expression.
8909 For an initializer for a decl, TYPE is zero. */
8911 void
8912 really_start_incremental_init (tree type)
8914 struct constructor_stack *p = XNEW (struct constructor_stack);
8916 if (type == NULL_TREE)
8917 type = TREE_TYPE (constructor_decl);
8919 if (VECTOR_TYPE_P (type)
8920 && TYPE_VECTOR_OPAQUE (type))
8921 error ("opaque vector types cannot be initialized");
8923 p->type = constructor_type;
8924 p->fields = constructor_fields;
8925 p->index = constructor_index;
8926 p->max_index = constructor_max_index;
8927 p->unfilled_index = constructor_unfilled_index;
8928 p->unfilled_fields = constructor_unfilled_fields;
8929 p->bit_index = constructor_bit_index;
8930 p->elements = constructor_elements;
8931 p->constant = constructor_constant;
8932 p->simple = constructor_simple;
8933 p->nonconst = constructor_nonconst;
8934 p->erroneous = constructor_erroneous;
8935 p->pending_elts = constructor_pending_elts;
8936 p->depth = constructor_depth;
8937 p->replacement_value.value = 0;
8938 p->replacement_value.original_code = ERROR_MARK;
8939 p->replacement_value.original_type = NULL;
8940 p->implicit = 0;
8941 p->range_stack = 0;
8942 p->outer = 0;
8943 p->incremental = constructor_incremental;
8944 p->designated = constructor_designated;
8945 p->designator_depth = designator_depth;
8946 p->next = 0;
8947 constructor_stack = p;
8949 constructor_constant = 1;
8950 constructor_simple = 1;
8951 constructor_nonconst = 0;
8952 constructor_depth = SPELLING_DEPTH ();
8953 constructor_elements = NULL;
8954 constructor_pending_elts = 0;
8955 constructor_type = type;
8956 constructor_incremental = 1;
8957 constructor_designated = 0;
8958 constructor_zeroinit = 1;
8959 designator_depth = 0;
8960 designator_erroneous = 0;
8962 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8964 constructor_fields = TYPE_FIELDS (constructor_type);
8965 /* Skip any nameless bit fields at the beginning. */
8966 while (constructor_fields != NULL_TREE
8967 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8968 constructor_fields = DECL_CHAIN (constructor_fields);
8970 constructor_unfilled_fields = constructor_fields;
8971 constructor_bit_index = bitsize_zero_node;
8973 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8975 if (TYPE_DOMAIN (constructor_type))
8977 constructor_max_index
8978 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8980 /* Detect non-empty initializations of zero-length arrays. */
8981 if (constructor_max_index == NULL_TREE
8982 && TYPE_SIZE (constructor_type))
8983 constructor_max_index = integer_minus_one_node;
8985 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8986 to initialize VLAs with a nonempty initializer will cause a
8987 proper error; avoid tree checking errors as well by setting a
8988 safe value. */
8989 if (constructor_max_index
8990 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8991 constructor_max_index = integer_minus_one_node;
8993 constructor_index
8994 = convert (bitsizetype,
8995 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8997 else
8999 constructor_index = bitsize_zero_node;
9000 constructor_max_index = NULL_TREE;
9003 constructor_unfilled_index = constructor_index;
9005 else if (gnu_vector_type_p (constructor_type))
9007 /* Vectors are like simple fixed-size arrays. */
9008 constructor_max_index =
9009 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
9010 constructor_index = bitsize_zero_node;
9011 constructor_unfilled_index = constructor_index;
9013 else
9015 /* Handle the case of int x = {5}; */
9016 constructor_fields = constructor_type;
9017 constructor_unfilled_fields = constructor_type;
9021 extern location_t last_init_list_comma;
9023 /* Called when we see an open brace for a nested initializer. Finish
9024 off any pending levels with implicit braces. */
9025 void
9026 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
9028 while (constructor_stack->implicit)
9030 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9031 && constructor_fields == NULL_TREE)
9032 process_init_element (input_location,
9033 pop_init_level (loc, 1, braced_init_obstack,
9034 last_init_list_comma),
9035 true, braced_init_obstack);
9036 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
9037 && constructor_max_index
9038 && tree_int_cst_lt (constructor_max_index,
9039 constructor_index))
9040 process_init_element (input_location,
9041 pop_init_level (loc, 1, braced_init_obstack,
9042 last_init_list_comma),
9043 true, braced_init_obstack);
9044 else
9045 break;
9049 /* Push down into a subobject, for initialization.
9050 If this is for an explicit set of braces, IMPLICIT is 0.
9051 If it is because the next element belongs at a lower level,
9052 IMPLICIT is 1 (or 2 if the push is because of designator list). */
9054 void
9055 push_init_level (location_t loc, int implicit,
9056 struct obstack *braced_init_obstack)
9058 struct constructor_stack *p;
9059 tree value = NULL_TREE;
9061 /* Unless this is an explicit brace, we need to preserve previous
9062 content if any. */
9063 if (implicit)
9065 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
9066 value = find_init_member (constructor_fields, braced_init_obstack);
9067 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9068 value = find_init_member (constructor_index, braced_init_obstack);
9071 p = XNEW (struct constructor_stack);
9072 p->type = constructor_type;
9073 p->fields = constructor_fields;
9074 p->index = constructor_index;
9075 p->max_index = constructor_max_index;
9076 p->unfilled_index = constructor_unfilled_index;
9077 p->unfilled_fields = constructor_unfilled_fields;
9078 p->bit_index = constructor_bit_index;
9079 p->elements = constructor_elements;
9080 p->constant = constructor_constant;
9081 p->simple = constructor_simple;
9082 p->nonconst = constructor_nonconst;
9083 p->erroneous = constructor_erroneous;
9084 p->pending_elts = constructor_pending_elts;
9085 p->depth = constructor_depth;
9086 p->replacement_value.value = NULL_TREE;
9087 p->replacement_value.original_code = ERROR_MARK;
9088 p->replacement_value.original_type = NULL;
9089 p->implicit = implicit;
9090 p->outer = 0;
9091 p->incremental = constructor_incremental;
9092 p->designated = constructor_designated;
9093 p->designator_depth = designator_depth;
9094 p->next = constructor_stack;
9095 p->range_stack = 0;
9096 constructor_stack = p;
9098 constructor_constant = 1;
9099 constructor_simple = 1;
9100 constructor_nonconst = 0;
9101 constructor_depth = SPELLING_DEPTH ();
9102 constructor_elements = NULL;
9103 constructor_incremental = 1;
9104 /* If the upper initializer is designated, then mark this as
9105 designated too to prevent bogus warnings. */
9106 constructor_designated = p->designated;
9107 constructor_pending_elts = 0;
9108 if (!implicit)
9110 p->range_stack = constructor_range_stack;
9111 constructor_range_stack = 0;
9112 designator_depth = 0;
9113 designator_erroneous = 0;
9116 /* Don't die if an entire brace-pair level is superfluous
9117 in the containing level. */
9118 if (constructor_type == NULL_TREE)
9120 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9122 /* Don't die if there are extra init elts at the end. */
9123 if (constructor_fields == NULL_TREE)
9124 constructor_type = NULL_TREE;
9125 else
9127 constructor_type = TREE_TYPE (constructor_fields);
9128 push_member_name (constructor_fields);
9129 constructor_depth++;
9132 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9134 constructor_type = TREE_TYPE (constructor_type);
9135 push_array_bounds (tree_to_uhwi (constructor_index));
9136 constructor_depth++;
9139 if (constructor_type == NULL_TREE)
9141 error_init (loc, "extra brace group at end of initializer");
9142 constructor_fields = NULL_TREE;
9143 constructor_unfilled_fields = NULL_TREE;
9144 return;
9147 if (value && TREE_CODE (value) == CONSTRUCTOR)
9149 constructor_constant = TREE_CONSTANT (value);
9150 constructor_simple = TREE_STATIC (value);
9151 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
9152 constructor_elements = CONSTRUCTOR_ELTS (value);
9153 if (!vec_safe_is_empty (constructor_elements)
9154 && (TREE_CODE (constructor_type) == RECORD_TYPE
9155 || TREE_CODE (constructor_type) == ARRAY_TYPE))
9156 set_nonincremental_init (braced_init_obstack);
9159 if (implicit == 1)
9161 found_missing_braces = 1;
9162 if (initializer_stack->missing_brace_richloc)
9163 initializer_stack->missing_brace_richloc->add_fixit_insert_before
9164 (loc, "{");
9167 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9169 constructor_fields = TYPE_FIELDS (constructor_type);
9170 /* Skip any nameless bit fields at the beginning. */
9171 while (constructor_fields != NULL_TREE
9172 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
9173 constructor_fields = DECL_CHAIN (constructor_fields);
9175 constructor_unfilled_fields = constructor_fields;
9176 constructor_bit_index = bitsize_zero_node;
9178 else if (gnu_vector_type_p (constructor_type))
9180 /* Vectors are like simple fixed-size arrays. */
9181 constructor_max_index =
9182 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
9183 constructor_index = bitsize_int (0);
9184 constructor_unfilled_index = constructor_index;
9186 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9188 if (TYPE_DOMAIN (constructor_type))
9190 constructor_max_index
9191 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
9193 /* Detect non-empty initializations of zero-length arrays. */
9194 if (constructor_max_index == NULL_TREE
9195 && TYPE_SIZE (constructor_type))
9196 constructor_max_index = integer_minus_one_node;
9198 /* constructor_max_index needs to be an INTEGER_CST. Attempts
9199 to initialize VLAs will cause a proper error; avoid tree
9200 checking errors as well by setting a safe value. */
9201 if (constructor_max_index
9202 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9203 constructor_max_index = integer_minus_one_node;
9205 constructor_index
9206 = convert (bitsizetype,
9207 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9209 else
9210 constructor_index = bitsize_zero_node;
9212 constructor_unfilled_index = constructor_index;
9213 if (value && TREE_CODE (value) == STRING_CST)
9215 /* We need to split the char/wchar array into individual
9216 characters, so that we don't have to special case it
9217 everywhere. */
9218 set_nonincremental_init_from_string (value, braced_init_obstack);
9221 else
9223 if (constructor_type != error_mark_node)
9224 warning_init (input_location, 0, "braces around scalar initializer");
9225 constructor_fields = constructor_type;
9226 constructor_unfilled_fields = constructor_type;
9230 /* At the end of an implicit or explicit brace level,
9231 finish up that level of constructor. If a single expression
9232 with redundant braces initialized that level, return the
9233 c_expr structure for that expression. Otherwise, the original_code
9234 element is set to ERROR_MARK.
9235 If we were outputting the elements as they are read, return 0 as the value
9236 from inner levels (process_init_element ignores that),
9237 but return error_mark_node as the value from the outermost level
9238 (that's what we want to put in DECL_INITIAL).
9239 Otherwise, return a CONSTRUCTOR expression as the value. */
9241 struct c_expr
9242 pop_init_level (location_t loc, int implicit,
9243 struct obstack *braced_init_obstack,
9244 location_t insert_before)
9246 struct constructor_stack *p;
9247 struct c_expr ret;
9248 ret.value = NULL_TREE;
9249 ret.original_code = ERROR_MARK;
9250 ret.original_type = NULL;
9251 ret.m_decimal = 0;
9253 if (implicit == 0)
9255 /* When we come to an explicit close brace,
9256 pop any inner levels that didn't have explicit braces. */
9257 while (constructor_stack->implicit)
9258 process_init_element (input_location,
9259 pop_init_level (loc, 1, braced_init_obstack,
9260 insert_before),
9261 true, braced_init_obstack);
9262 gcc_assert (!constructor_range_stack);
9264 else
9265 if (initializer_stack->missing_brace_richloc)
9266 initializer_stack->missing_brace_richloc->add_fixit_insert_before
9267 (insert_before, "}");
9269 /* Now output all pending elements. */
9270 constructor_incremental = 1;
9271 output_pending_init_elements (1, braced_init_obstack);
9273 p = constructor_stack;
9275 /* Error for initializing a flexible array member, or a zero-length
9276 array member in an inappropriate context. */
9277 if (constructor_type && constructor_fields
9278 && TREE_CODE (constructor_type) == ARRAY_TYPE
9279 && TYPE_DOMAIN (constructor_type)
9280 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
9282 /* Silently discard empty initializations. The parser will
9283 already have pedwarned for empty brackets. */
9284 if (integer_zerop (constructor_unfilled_index))
9285 constructor_type = NULL_TREE;
9286 else
9288 gcc_assert (!TYPE_SIZE (constructor_type));
9290 if (constructor_depth > 2)
9291 error_init (loc, "initialization of flexible array member in a nested context");
9292 else
9293 pedwarn_init (loc, OPT_Wpedantic,
9294 "initialization of a flexible array member");
9296 /* We have already issued an error message for the existence
9297 of a flexible array member not at the end of the structure.
9298 Discard the initializer so that we do not die later. */
9299 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
9300 constructor_type = NULL_TREE;
9304 switch (vec_safe_length (constructor_elements))
9306 case 0:
9307 /* Initialization with { } counts as zeroinit. */
9308 constructor_zeroinit = 1;
9309 break;
9310 case 1:
9311 /* This might be zeroinit as well. */
9312 if (integer_zerop ((*constructor_elements)[0].value))
9313 constructor_zeroinit = 1;
9314 break;
9315 default:
9316 /* If the constructor has more than one element, it can't be { 0 }. */
9317 constructor_zeroinit = 0;
9318 break;
9321 /* Warn when some structs are initialized with direct aggregation. */
9322 if (!implicit && found_missing_braces && warn_missing_braces
9323 && !constructor_zeroinit)
9325 gcc_assert (initializer_stack->missing_brace_richloc);
9326 warning_at (initializer_stack->missing_brace_richloc,
9327 OPT_Wmissing_braces,
9328 "missing braces around initializer");
9331 /* Warn when some struct elements are implicitly initialized to zero. */
9332 if (warn_missing_field_initializers
9333 && constructor_type
9334 && TREE_CODE (constructor_type) == RECORD_TYPE
9335 && constructor_unfilled_fields)
9337 /* Do not warn for flexible array members or zero-length arrays. */
9338 while (constructor_unfilled_fields
9339 && (!DECL_SIZE (constructor_unfilled_fields)
9340 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
9341 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
9343 if (constructor_unfilled_fields
9344 /* Do not warn if this level of the initializer uses member
9345 designators; it is likely to be deliberate. */
9346 && !constructor_designated
9347 /* Do not warn about initializing with { 0 } or with { }. */
9348 && !constructor_zeroinit)
9350 if (warning_at (input_location, OPT_Wmissing_field_initializers,
9351 "missing initializer for field %qD of %qT",
9352 constructor_unfilled_fields,
9353 constructor_type))
9354 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
9355 "%qD declared here", constructor_unfilled_fields);
9359 /* Pad out the end of the structure. */
9360 if (p->replacement_value.value)
9361 /* If this closes a superfluous brace pair,
9362 just pass out the element between them. */
9363 ret = p->replacement_value;
9364 else if (constructor_type == NULL_TREE)
9366 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
9367 && TREE_CODE (constructor_type) != ARRAY_TYPE
9368 && !gnu_vector_type_p (constructor_type))
9370 /* A nonincremental scalar initializer--just return
9371 the element, after verifying there is just one.
9372 Empty scalar initializers are supported in C2X. */
9373 if (vec_safe_is_empty (constructor_elements))
9375 if (constructor_erroneous || constructor_type == error_mark_node)
9376 ret.value = error_mark_node;
9377 else if (TREE_CODE (constructor_type) == POINTER_TYPE)
9378 /* Ensure this is a null pointer constant in the case of a
9379 'constexpr' object initialized with {}. */
9380 ret.value = build_zero_cst (ptr_type_node);
9381 else
9382 ret.value = build_zero_cst (constructor_type);
9384 else if (vec_safe_length (constructor_elements) != 1)
9386 error_init (loc, "extra elements in scalar initializer");
9387 ret.value = (*constructor_elements)[0].value;
9389 else
9390 ret.value = (*constructor_elements)[0].value;
9392 else
9394 if (constructor_erroneous)
9395 ret.value = error_mark_node;
9396 else
9398 ret.value = build_constructor (constructor_type,
9399 constructor_elements);
9400 if (constructor_constant)
9401 TREE_CONSTANT (ret.value) = 1;
9402 if (constructor_constant && constructor_simple)
9403 TREE_STATIC (ret.value) = 1;
9404 if (constructor_nonconst)
9405 CONSTRUCTOR_NON_CONST (ret.value) = 1;
9409 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
9411 if (constructor_nonconst)
9412 ret.original_code = C_MAYBE_CONST_EXPR;
9413 else if (ret.original_code == C_MAYBE_CONST_EXPR)
9414 ret.original_code = ERROR_MARK;
9417 constructor_type = p->type;
9418 constructor_fields = p->fields;
9419 constructor_index = p->index;
9420 constructor_max_index = p->max_index;
9421 constructor_unfilled_index = p->unfilled_index;
9422 constructor_unfilled_fields = p->unfilled_fields;
9423 constructor_bit_index = p->bit_index;
9424 constructor_elements = p->elements;
9425 constructor_constant = p->constant;
9426 constructor_simple = p->simple;
9427 constructor_nonconst = p->nonconst;
9428 constructor_erroneous = p->erroneous;
9429 constructor_incremental = p->incremental;
9430 constructor_designated = p->designated;
9431 designator_depth = p->designator_depth;
9432 constructor_pending_elts = p->pending_elts;
9433 constructor_depth = p->depth;
9434 if (!p->implicit)
9435 constructor_range_stack = p->range_stack;
9436 RESTORE_SPELLING_DEPTH (constructor_depth);
9438 constructor_stack = p->next;
9439 XDELETE (p);
9441 if (ret.value == NULL_TREE && constructor_stack == 0)
9442 ret.value = error_mark_node;
9443 return ret;
9446 /* Common handling for both array range and field name designators.
9447 ARRAY argument is nonzero for array ranges. Returns false for success. */
9449 static bool
9450 set_designator (location_t loc, bool array,
9451 struct obstack *braced_init_obstack)
9453 tree subtype;
9454 enum tree_code subcode;
9456 /* Don't die if an entire brace-pair level is superfluous
9457 in the containing level, or for an erroneous type. */
9458 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9459 return true;
9461 /* If there were errors in this designator list already, bail out
9462 silently. */
9463 if (designator_erroneous)
9464 return true;
9466 /* Likewise for an initializer for a variable-size type. Those are
9467 diagnosed in the parser, except for empty initializer braces. */
9468 if (COMPLETE_TYPE_P (constructor_type)
9469 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9470 return true;
9472 if (!designator_depth)
9474 gcc_assert (!constructor_range_stack);
9476 /* Designator list starts at the level of closest explicit
9477 braces. */
9478 while (constructor_stack->implicit)
9479 process_init_element (input_location,
9480 pop_init_level (loc, 1, braced_init_obstack,
9481 last_init_list_comma),
9482 true, braced_init_obstack);
9483 constructor_designated = 1;
9484 return false;
9487 switch (TREE_CODE (constructor_type))
9489 case RECORD_TYPE:
9490 case UNION_TYPE:
9491 subtype = TREE_TYPE (constructor_fields);
9492 if (subtype != error_mark_node)
9493 subtype = TYPE_MAIN_VARIANT (subtype);
9494 break;
9495 case ARRAY_TYPE:
9496 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9497 break;
9498 default:
9499 gcc_unreachable ();
9502 subcode = TREE_CODE (subtype);
9503 if (array && subcode != ARRAY_TYPE)
9505 error_init (loc, "array index in non-array initializer");
9506 return true;
9508 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9510 error_init (loc, "field name not in record or union initializer");
9511 return true;
9514 constructor_designated = 1;
9515 finish_implicit_inits (loc, braced_init_obstack);
9516 push_init_level (loc, 2, braced_init_obstack);
9517 return false;
9520 /* If there are range designators in designator list, push a new designator
9521 to constructor_range_stack. RANGE_END is end of such stack range or
9522 NULL_TREE if there is no range designator at this level. */
9524 static void
9525 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9527 struct constructor_range_stack *p;
9529 p = (struct constructor_range_stack *)
9530 obstack_alloc (braced_init_obstack,
9531 sizeof (struct constructor_range_stack));
9532 p->prev = constructor_range_stack;
9533 p->next = 0;
9534 p->fields = constructor_fields;
9535 p->range_start = constructor_index;
9536 p->index = constructor_index;
9537 p->stack = constructor_stack;
9538 p->range_end = range_end;
9539 if (constructor_range_stack)
9540 constructor_range_stack->next = p;
9541 constructor_range_stack = p;
9544 /* Within an array initializer, specify the next index to be initialized.
9545 FIRST is that index. If LAST is nonzero, then initialize a range
9546 of indices, running from FIRST through LAST. */
9548 void
9549 set_init_index (location_t loc, tree first, tree last,
9550 struct obstack *braced_init_obstack)
9552 if (set_designator (loc, true, braced_init_obstack))
9553 return;
9555 designator_erroneous = 1;
9557 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9558 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9560 error_init (loc, "array index in initializer not of integer type");
9561 return;
9564 if (TREE_CODE (first) != INTEGER_CST)
9566 first = c_fully_fold (first, false, NULL);
9567 if (TREE_CODE (first) == INTEGER_CST)
9568 pedwarn_init (loc, OPT_Wpedantic,
9569 "array index in initializer is not "
9570 "an integer constant expression");
9573 if (last && TREE_CODE (last) != INTEGER_CST)
9575 last = c_fully_fold (last, false, NULL);
9576 if (TREE_CODE (last) == INTEGER_CST)
9577 pedwarn_init (loc, OPT_Wpedantic,
9578 "array index in initializer is not "
9579 "an integer constant expression");
9582 if (TREE_CODE (first) != INTEGER_CST)
9583 error_init (loc, "nonconstant array index in initializer");
9584 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9585 error_init (loc, "nonconstant array index in initializer");
9586 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9587 error_init (loc, "array index in non-array initializer");
9588 else if (tree_int_cst_sgn (first) == -1)
9589 error_init (loc, "array index in initializer exceeds array bounds");
9590 else if (constructor_max_index
9591 && tree_int_cst_lt (constructor_max_index, first))
9592 error_init (loc, "array index in initializer exceeds array bounds");
9593 else
9595 constant_expression_warning (first);
9596 if (last)
9597 constant_expression_warning (last);
9598 constructor_index = convert (bitsizetype, first);
9599 if (tree_int_cst_lt (constructor_index, first))
9601 constructor_index = copy_node (constructor_index);
9602 TREE_OVERFLOW (constructor_index) = 1;
9605 if (last)
9607 if (tree_int_cst_equal (first, last))
9608 last = NULL_TREE;
9609 else if (tree_int_cst_lt (last, first))
9611 error_init (loc, "empty index range in initializer");
9612 last = NULL_TREE;
9614 else
9616 last = convert (bitsizetype, last);
9617 if (constructor_max_index != NULL_TREE
9618 && tree_int_cst_lt (constructor_max_index, last))
9620 error_init (loc, "array index range in initializer exceeds "
9621 "array bounds");
9622 last = NULL_TREE;
9627 designator_depth++;
9628 designator_erroneous = 0;
9629 if (constructor_range_stack || last)
9630 push_range_stack (last, braced_init_obstack);
9634 /* Within a struct initializer, specify the next field to be initialized. */
9636 void
9637 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9638 struct obstack *braced_init_obstack)
9640 tree field;
9642 if (set_designator (loc, false, braced_init_obstack))
9643 return;
9645 designator_erroneous = 1;
9647 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9649 error_init (loc, "field name not in record or union initializer");
9650 return;
9653 field = lookup_field (constructor_type, fieldname);
9655 if (field == NULL_TREE)
9657 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9658 if (guessed_id)
9660 gcc_rich_location rich_loc (fieldname_loc);
9661 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9662 error_at (&rich_loc,
9663 "%qT has no member named %qE; did you mean %qE?",
9664 constructor_type, fieldname, guessed_id);
9666 else
9667 error_at (fieldname_loc, "%qT has no member named %qE",
9668 constructor_type, fieldname);
9670 else
9673 constructor_fields = TREE_VALUE (field);
9674 designator_depth++;
9675 designator_erroneous = 0;
9676 if (constructor_range_stack)
9677 push_range_stack (NULL_TREE, braced_init_obstack);
9678 field = TREE_CHAIN (field);
9679 if (field)
9681 if (set_designator (loc, false, braced_init_obstack))
9682 return;
9685 while (field != NULL_TREE);
9688 /* Add a new initializer to the tree of pending initializers. PURPOSE
9689 identifies the initializer, either array index or field in a structure.
9690 VALUE is the value of that index or field. If ORIGTYPE is not
9691 NULL_TREE, it is the original type of VALUE.
9693 IMPLICIT is true if value comes from pop_init_level (1),
9694 the new initializer has been merged with the existing one
9695 and thus no warnings should be emitted about overriding an
9696 existing initializer. */
9698 static void
9699 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9700 bool implicit, struct obstack *braced_init_obstack)
9702 struct init_node *p, **q, *r;
9704 q = &constructor_pending_elts;
9705 p = 0;
9707 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9709 while (*q != 0)
9711 p = *q;
9712 if (tree_int_cst_lt (purpose, p->purpose))
9713 q = &p->left;
9714 else if (tree_int_cst_lt (p->purpose, purpose))
9715 q = &p->right;
9716 else
9718 if (!implicit)
9720 if (TREE_SIDE_EFFECTS (p->value))
9721 warning_init (loc, OPT_Woverride_init_side_effects,
9722 "initialized field with side-effects "
9723 "overwritten");
9724 else if (warn_override_init)
9725 warning_init (loc, OPT_Woverride_init,
9726 "initialized field overwritten");
9728 p->value = value;
9729 p->origtype = origtype;
9730 return;
9734 else
9736 tree bitpos;
9738 bitpos = bit_position (purpose);
9739 while (*q != NULL)
9741 p = *q;
9742 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9743 q = &p->left;
9744 else if (p->purpose != purpose)
9745 q = &p->right;
9746 else
9748 if (!implicit)
9750 if (TREE_SIDE_EFFECTS (p->value))
9751 warning_init (loc, OPT_Woverride_init_side_effects,
9752 "initialized field with side-effects "
9753 "overwritten");
9754 else if (warn_override_init)
9755 warning_init (loc, OPT_Woverride_init,
9756 "initialized field overwritten");
9758 p->value = value;
9759 p->origtype = origtype;
9760 return;
9765 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9766 sizeof (struct init_node));
9767 r->purpose = purpose;
9768 r->value = value;
9769 r->origtype = origtype;
9771 *q = r;
9772 r->parent = p;
9773 r->left = 0;
9774 r->right = 0;
9775 r->balance = 0;
9777 while (p)
9779 struct init_node *s;
9781 if (r == p->left)
9783 if (p->balance == 0)
9784 p->balance = -1;
9785 else if (p->balance < 0)
9787 if (r->balance < 0)
9789 /* L rotation. */
9790 p->left = r->right;
9791 if (p->left)
9792 p->left->parent = p;
9793 r->right = p;
9795 p->balance = 0;
9796 r->balance = 0;
9798 s = p->parent;
9799 p->parent = r;
9800 r->parent = s;
9801 if (s)
9803 if (s->left == p)
9804 s->left = r;
9805 else
9806 s->right = r;
9808 else
9809 constructor_pending_elts = r;
9811 else
9813 /* LR rotation. */
9814 struct init_node *t = r->right;
9816 r->right = t->left;
9817 if (r->right)
9818 r->right->parent = r;
9819 t->left = r;
9821 p->left = t->right;
9822 if (p->left)
9823 p->left->parent = p;
9824 t->right = p;
9826 p->balance = t->balance < 0;
9827 r->balance = -(t->balance > 0);
9828 t->balance = 0;
9830 s = p->parent;
9831 p->parent = t;
9832 r->parent = t;
9833 t->parent = s;
9834 if (s)
9836 if (s->left == p)
9837 s->left = t;
9838 else
9839 s->right = t;
9841 else
9842 constructor_pending_elts = t;
9844 break;
9846 else
9848 /* p->balance == +1; growth of left side balances the node. */
9849 p->balance = 0;
9850 break;
9853 else /* r == p->right */
9855 if (p->balance == 0)
9856 /* Growth propagation from right side. */
9857 p->balance++;
9858 else if (p->balance > 0)
9860 if (r->balance > 0)
9862 /* R rotation. */
9863 p->right = r->left;
9864 if (p->right)
9865 p->right->parent = p;
9866 r->left = p;
9868 p->balance = 0;
9869 r->balance = 0;
9871 s = p->parent;
9872 p->parent = r;
9873 r->parent = s;
9874 if (s)
9876 if (s->left == p)
9877 s->left = r;
9878 else
9879 s->right = r;
9881 else
9882 constructor_pending_elts = r;
9884 else /* r->balance == -1 */
9886 /* RL rotation */
9887 struct init_node *t = r->left;
9889 r->left = t->right;
9890 if (r->left)
9891 r->left->parent = r;
9892 t->right = r;
9894 p->right = t->left;
9895 if (p->right)
9896 p->right->parent = p;
9897 t->left = p;
9899 r->balance = (t->balance < 0);
9900 p->balance = -(t->balance > 0);
9901 t->balance = 0;
9903 s = p->parent;
9904 p->parent = t;
9905 r->parent = t;
9906 t->parent = s;
9907 if (s)
9909 if (s->left == p)
9910 s->left = t;
9911 else
9912 s->right = t;
9914 else
9915 constructor_pending_elts = t;
9917 break;
9919 else
9921 /* p->balance == -1; growth of right side balances the node. */
9922 p->balance = 0;
9923 break;
9927 r = p;
9928 p = p->parent;
9932 /* Build AVL tree from a sorted chain. */
9934 static void
9935 set_nonincremental_init (struct obstack * braced_init_obstack)
9937 unsigned HOST_WIDE_INT ix;
9938 tree index, value;
9940 if (TREE_CODE (constructor_type) != RECORD_TYPE
9941 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9942 return;
9944 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9945 add_pending_init (input_location, index, value, NULL_TREE, true,
9946 braced_init_obstack);
9947 constructor_elements = NULL;
9948 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9950 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9951 /* Skip any nameless bit fields at the beginning. */
9952 while (constructor_unfilled_fields != NULL_TREE
9953 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9954 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9957 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9959 if (TYPE_DOMAIN (constructor_type))
9960 constructor_unfilled_index
9961 = convert (bitsizetype,
9962 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9963 else
9964 constructor_unfilled_index = bitsize_zero_node;
9966 constructor_incremental = 0;
9969 /* Build AVL tree from a string constant. */
9971 static void
9972 set_nonincremental_init_from_string (tree str,
9973 struct obstack * braced_init_obstack)
9975 tree value, purpose, type;
9976 HOST_WIDE_INT val[2];
9977 const char *p, *end;
9978 int byte, wchar_bytes, charwidth, bitpos;
9980 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9982 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9983 charwidth = TYPE_PRECISION (char_type_node);
9984 gcc_assert ((size_t) wchar_bytes * charwidth
9985 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9986 type = TREE_TYPE (constructor_type);
9987 p = TREE_STRING_POINTER (str);
9988 end = p + TREE_STRING_LENGTH (str);
9990 for (purpose = bitsize_zero_node;
9991 p < end
9992 && !(constructor_max_index
9993 && tree_int_cst_lt (constructor_max_index, purpose));
9994 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9996 if (wchar_bytes == 1)
9998 val[0] = (unsigned char) *p++;
9999 val[1] = 0;
10001 else
10003 val[1] = 0;
10004 val[0] = 0;
10005 for (byte = 0; byte < wchar_bytes; byte++)
10007 if (BYTES_BIG_ENDIAN)
10008 bitpos = (wchar_bytes - byte - 1) * charwidth;
10009 else
10010 bitpos = byte * charwidth;
10011 val[bitpos / HOST_BITS_PER_WIDE_INT]
10012 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
10013 << (bitpos % HOST_BITS_PER_WIDE_INT);
10017 if (!TYPE_UNSIGNED (type))
10019 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
10020 if (bitpos < HOST_BITS_PER_WIDE_INT)
10022 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
10024 val[0] |= HOST_WIDE_INT_M1U << bitpos;
10025 val[1] = -1;
10028 else if (bitpos == HOST_BITS_PER_WIDE_INT)
10030 if (val[0] < 0)
10031 val[1] = -1;
10033 else if (val[1] & (HOST_WIDE_INT_1
10034 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
10035 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
10038 value = wide_int_to_tree (type,
10039 wide_int::from_array (val, 2,
10040 HOST_BITS_PER_WIDE_INT * 2));
10041 add_pending_init (input_location, purpose, value, NULL_TREE, true,
10042 braced_init_obstack);
10045 constructor_incremental = 0;
10048 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
10049 not initialized yet. */
10051 static tree
10052 find_init_member (tree field, struct obstack * braced_init_obstack)
10054 struct init_node *p;
10056 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10058 if (constructor_incremental
10059 && tree_int_cst_lt (field, constructor_unfilled_index))
10060 set_nonincremental_init (braced_init_obstack);
10062 p = constructor_pending_elts;
10063 while (p)
10065 if (tree_int_cst_lt (field, p->purpose))
10066 p = p->left;
10067 else if (tree_int_cst_lt (p->purpose, field))
10068 p = p->right;
10069 else
10070 return p->value;
10073 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
10075 tree bitpos = bit_position (field);
10077 if (constructor_incremental
10078 && (!constructor_unfilled_fields
10079 || tree_int_cst_lt (bitpos,
10080 bit_position (constructor_unfilled_fields))))
10081 set_nonincremental_init (braced_init_obstack);
10083 p = constructor_pending_elts;
10084 while (p)
10086 if (field == p->purpose)
10087 return p->value;
10088 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
10089 p = p->left;
10090 else
10091 p = p->right;
10094 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10096 if (!vec_safe_is_empty (constructor_elements)
10097 && (constructor_elements->last ().index == field))
10098 return constructor_elements->last ().value;
10100 return NULL_TREE;
10103 /* "Output" the next constructor element.
10104 At top level, really output it to assembler code now.
10105 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
10106 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
10107 TYPE is the data type that the containing data type wants here.
10108 FIELD is the field (a FIELD_DECL) or the index that this element fills.
10109 If VALUE is a string constant, STRICT_STRING is true if it is
10110 unparenthesized or we should not warn here for it being parenthesized.
10111 For other types of VALUE, STRICT_STRING is not used.
10113 PENDING if true means output pending elements that belong
10114 right after this element. (PENDING is normally true;
10115 it is false while outputting pending elements, to avoid recursion.)
10117 IMPLICIT is true if value comes from pop_init_level (1),
10118 the new initializer has been merged with the existing one
10119 and thus no warnings should be emitted about overriding an
10120 existing initializer. */
10122 static void
10123 output_init_element (location_t loc, tree value, tree origtype,
10124 bool strict_string, tree type, tree field, bool pending,
10125 bool implicit, struct obstack * braced_init_obstack)
10127 tree semantic_type = NULL_TREE;
10128 bool maybe_const = true;
10129 bool npc, int_const_expr, arith_const_expr;
10131 if (type == error_mark_node || value == error_mark_node)
10133 constructor_erroneous = 1;
10134 return;
10136 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
10137 && (TREE_CODE (value) == STRING_CST
10138 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
10139 && !(TREE_CODE (value) == STRING_CST
10140 && TREE_CODE (type) == ARRAY_TYPE
10141 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
10142 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
10143 TYPE_MAIN_VARIANT (type)))
10144 value = array_to_pointer_conversion (input_location, value);
10146 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
10147 && require_constant_value && pending)
10149 /* As an extension, allow initializing objects with static storage
10150 duration with compound literals (which are then treated just as
10151 the brace enclosed list they contain). */
10152 if (flag_isoc99)
10153 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
10154 "constant");
10155 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
10156 value = DECL_INITIAL (decl);
10159 npc = null_pointer_constant_p (value);
10160 int_const_expr = (TREE_CODE (value) == INTEGER_CST
10161 && !TREE_OVERFLOW (value)
10162 && INTEGRAL_TYPE_P (TREE_TYPE (value)));
10163 /* Not fully determined before folding. */
10164 arith_const_expr = true;
10165 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
10167 semantic_type = TREE_TYPE (value);
10168 value = TREE_OPERAND (value, 0);
10170 value = c_fully_fold (value, require_constant_value, &maybe_const);
10171 /* TODO: this may not detect all cases of expressions folding to
10172 constants that are not arithmetic constant expressions. */
10173 if (!maybe_const)
10174 arith_const_expr = false;
10175 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
10176 && TREE_CODE (TREE_TYPE (value)) != REAL_TYPE
10177 && TREE_CODE (TREE_TYPE (value)) != COMPLEX_TYPE)
10178 arith_const_expr = false;
10179 else if (TREE_CODE (value) != INTEGER_CST
10180 && TREE_CODE (value) != REAL_CST
10181 && TREE_CODE (value) != COMPLEX_CST)
10182 arith_const_expr = false;
10183 else if (TREE_OVERFLOW (value))
10184 arith_const_expr = false;
10186 if (value == error_mark_node)
10187 constructor_erroneous = 1;
10188 else if (!TREE_CONSTANT (value))
10189 constructor_constant = 0;
10190 else if (!initializer_constant_valid_p (value,
10191 TREE_TYPE (value),
10192 AGGREGATE_TYPE_P (constructor_type)
10193 && TYPE_REVERSE_STORAGE_ORDER
10194 (constructor_type))
10195 || (RECORD_OR_UNION_TYPE_P (constructor_type)
10196 && DECL_C_BIT_FIELD (field)
10197 && TREE_CODE (value) != INTEGER_CST))
10198 constructor_simple = 0;
10199 if (!maybe_const)
10200 constructor_nonconst = 1;
10202 /* Digest the initializer and issue any errors about incompatible
10203 types before issuing errors about non-constant initializers. */
10204 tree new_value = value;
10205 if (semantic_type)
10206 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
10207 /* In the case of braces around a scalar initializer, the result of
10208 this initializer processing goes through digest_init again at the
10209 outer level. In the case of a constexpr initializer for a
10210 pointer, avoid converting a null pointer constant to something
10211 that is not a null pointer constant to avoid a spurious error
10212 from that second processing. */
10213 if (!require_constexpr_value
10214 || !npc
10215 || TREE_CODE (constructor_type) != POINTER_TYPE)
10216 new_value = digest_init (loc, type, new_value, origtype, npc,
10217 int_const_expr, arith_const_expr, strict_string,
10218 require_constant_value, require_constexpr_value);
10219 if (new_value == error_mark_node)
10221 constructor_erroneous = 1;
10222 return;
10224 if (require_constant_value || require_constant_elements)
10225 constant_expression_warning (new_value);
10227 /* Proceed to check the constness of the original initializer. */
10228 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
10230 if (require_constant_value)
10232 error_init (loc, "initializer element is not constant");
10233 value = error_mark_node;
10235 else if (require_constant_elements)
10236 pedwarn (loc, OPT_Wpedantic,
10237 "initializer element is not computable at load time");
10239 else if (!maybe_const
10240 && (require_constant_value || require_constant_elements))
10241 pedwarn_init (loc, OPT_Wpedantic,
10242 "initializer element is not a constant expression");
10243 /* digest_init has already carried out the additional checks
10244 required for 'constexpr' initializers (using the information
10245 passed to it about whether the original initializer was certain
10246 kinds of constant expression), so that check does not need to be
10247 repeated here. */
10249 /* Issue -Wc++-compat warnings about initializing a bitfield with
10250 enum type. */
10251 if (warn_cxx_compat
10252 && field != NULL_TREE
10253 && TREE_CODE (field) == FIELD_DECL
10254 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
10255 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
10256 != TYPE_MAIN_VARIANT (type))
10257 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
10259 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
10260 if (checktype != error_mark_node
10261 && (TYPE_MAIN_VARIANT (checktype)
10262 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
10263 warning_init (loc, OPT_Wc___compat,
10264 "enum conversion in initialization is invalid in C++");
10267 /* If this field is empty and does not have side effects (and is not at
10268 the end of structure), don't do anything other than checking the
10269 initializer. */
10270 if (field
10271 && (TREE_TYPE (field) == error_mark_node
10272 || (COMPLETE_TYPE_P (TREE_TYPE (field))
10273 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
10274 && !TREE_SIDE_EFFECTS (new_value)
10275 && (TREE_CODE (constructor_type) == ARRAY_TYPE
10276 || DECL_CHAIN (field)))))
10277 return;
10279 /* Finally, set VALUE to the initializer value digested above. */
10280 value = new_value;
10282 /* If this element doesn't come next in sequence,
10283 put it on constructor_pending_elts. */
10284 if (TREE_CODE (constructor_type) == ARRAY_TYPE
10285 && (!constructor_incremental
10286 || !tree_int_cst_equal (field, constructor_unfilled_index)))
10288 if (constructor_incremental
10289 && tree_int_cst_lt (field, constructor_unfilled_index))
10290 set_nonincremental_init (braced_init_obstack);
10292 add_pending_init (loc, field, value, origtype, implicit,
10293 braced_init_obstack);
10294 return;
10296 else if (TREE_CODE (constructor_type) == RECORD_TYPE
10297 && (!constructor_incremental
10298 || field != constructor_unfilled_fields))
10300 /* We do this for records but not for unions. In a union,
10301 no matter which field is specified, it can be initialized
10302 right away since it starts at the beginning of the union. */
10303 if (constructor_incremental)
10305 if (!constructor_unfilled_fields)
10306 set_nonincremental_init (braced_init_obstack);
10307 else
10309 tree bitpos, unfillpos;
10311 bitpos = bit_position (field);
10312 unfillpos = bit_position (constructor_unfilled_fields);
10314 if (tree_int_cst_lt (bitpos, unfillpos))
10315 set_nonincremental_init (braced_init_obstack);
10319 add_pending_init (loc, field, value, origtype, implicit,
10320 braced_init_obstack);
10321 return;
10323 else if (TREE_CODE (constructor_type) == UNION_TYPE
10324 && !vec_safe_is_empty (constructor_elements))
10326 if (!implicit)
10328 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
10329 warning_init (loc, OPT_Woverride_init_side_effects,
10330 "initialized field with side-effects overwritten");
10331 else if (warn_override_init)
10332 warning_init (loc, OPT_Woverride_init,
10333 "initialized field overwritten");
10336 /* We can have just one union field set. */
10337 constructor_elements = NULL;
10340 /* Otherwise, output this element either to
10341 constructor_elements or to the assembler file. */
10343 constructor_elt celt = {field, value};
10344 vec_safe_push (constructor_elements, celt);
10346 /* Advance the variable that indicates sequential elements output. */
10347 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10348 constructor_unfilled_index
10349 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
10350 bitsize_one_node);
10351 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
10353 constructor_unfilled_fields
10354 = DECL_CHAIN (constructor_unfilled_fields);
10356 /* Skip any nameless bit fields. */
10357 while (constructor_unfilled_fields != NULL_TREE
10358 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
10359 constructor_unfilled_fields =
10360 DECL_CHAIN (constructor_unfilled_fields);
10362 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10363 constructor_unfilled_fields = NULL_TREE;
10365 /* Now output any pending elements which have become next. */
10366 if (pending)
10367 output_pending_init_elements (0, braced_init_obstack);
10370 /* For two FIELD_DECLs in the same chain, return -1 if field1
10371 comes before field2, 1 if field1 comes after field2 and
10372 0 if field1 == field2. */
10374 static int
10375 init_field_decl_cmp (tree field1, tree field2)
10377 if (field1 == field2)
10378 return 0;
10380 tree bitpos1 = bit_position (field1);
10381 tree bitpos2 = bit_position (field2);
10382 if (tree_int_cst_equal (bitpos1, bitpos2))
10384 /* If one of the fields has non-zero bitsize, then that
10385 field must be the last one in a sequence of zero
10386 sized fields, fields after it will have bigger
10387 bit_position. */
10388 if (TREE_TYPE (field1) != error_mark_node
10389 && COMPLETE_TYPE_P (TREE_TYPE (field1))
10390 && integer_nonzerop (TREE_TYPE (field1)))
10391 return 1;
10392 if (TREE_TYPE (field2) != error_mark_node
10393 && COMPLETE_TYPE_P (TREE_TYPE (field2))
10394 && integer_nonzerop (TREE_TYPE (field2)))
10395 return -1;
10396 /* Otherwise, fallback to DECL_CHAIN walk to find out
10397 which field comes earlier. Walk chains of both
10398 fields, so that if field1 and field2 are close to each
10399 other in either order, it is found soon even for large
10400 sequences of zero sized fields. */
10401 tree f1 = field1, f2 = field2;
10402 while (1)
10404 f1 = DECL_CHAIN (f1);
10405 f2 = DECL_CHAIN (f2);
10406 if (f1 == NULL_TREE)
10408 gcc_assert (f2);
10409 return 1;
10411 if (f2 == NULL_TREE)
10412 return -1;
10413 if (f1 == field2)
10414 return -1;
10415 if (f2 == field1)
10416 return 1;
10417 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
10418 return 1;
10419 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
10420 return -1;
10423 else if (tree_int_cst_lt (bitpos1, bitpos2))
10424 return -1;
10425 else
10426 return 1;
10429 /* Output any pending elements which have become next.
10430 As we output elements, constructor_unfilled_{fields,index}
10431 advances, which may cause other elements to become next;
10432 if so, they too are output.
10434 If ALL is 0, we return when there are
10435 no more pending elements to output now.
10437 If ALL is 1, we output space as necessary so that
10438 we can output all the pending elements. */
10439 static void
10440 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
10442 struct init_node *elt = constructor_pending_elts;
10443 tree next;
10445 retry:
10447 /* Look through the whole pending tree.
10448 If we find an element that should be output now,
10449 output it. Otherwise, set NEXT to the element
10450 that comes first among those still pending. */
10452 next = NULL_TREE;
10453 while (elt)
10455 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10457 if (tree_int_cst_equal (elt->purpose,
10458 constructor_unfilled_index))
10459 output_init_element (input_location, elt->value, elt->origtype,
10460 true, TREE_TYPE (constructor_type),
10461 constructor_unfilled_index, false, false,
10462 braced_init_obstack);
10463 else if (tree_int_cst_lt (constructor_unfilled_index,
10464 elt->purpose))
10466 /* Advance to the next smaller node. */
10467 if (elt->left)
10468 elt = elt->left;
10469 else
10471 /* We have reached the smallest node bigger than the
10472 current unfilled index. Fill the space first. */
10473 next = elt->purpose;
10474 break;
10477 else
10479 /* Advance to the next bigger node. */
10480 if (elt->right)
10481 elt = elt->right;
10482 else
10484 /* We have reached the biggest node in a subtree. Find
10485 the parent of it, which is the next bigger node. */
10486 while (elt->parent && elt->parent->right == elt)
10487 elt = elt->parent;
10488 elt = elt->parent;
10489 if (elt && tree_int_cst_lt (constructor_unfilled_index,
10490 elt->purpose))
10492 next = elt->purpose;
10493 break;
10498 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10500 /* If the current record is complete we are done. */
10501 if (constructor_unfilled_fields == NULL_TREE)
10502 break;
10504 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10505 elt->purpose);
10506 if (cmp == 0)
10507 output_init_element (input_location, elt->value, elt->origtype,
10508 true, TREE_TYPE (elt->purpose),
10509 elt->purpose, false, false,
10510 braced_init_obstack);
10511 else if (cmp < 0)
10513 /* Advance to the next smaller node. */
10514 if (elt->left)
10515 elt = elt->left;
10516 else
10518 /* We have reached the smallest node bigger than the
10519 current unfilled field. Fill the space first. */
10520 next = elt->purpose;
10521 break;
10524 else
10526 /* Advance to the next bigger node. */
10527 if (elt->right)
10528 elt = elt->right;
10529 else
10531 /* We have reached the biggest node in a subtree. Find
10532 the parent of it, which is the next bigger node. */
10533 while (elt->parent && elt->parent->right == elt)
10534 elt = elt->parent;
10535 elt = elt->parent;
10536 if (elt
10537 && init_field_decl_cmp (constructor_unfilled_fields,
10538 elt->purpose) < 0)
10540 next = elt->purpose;
10541 break;
10548 /* Ordinarily return, but not if we want to output all
10549 and there are elements left. */
10550 if (!(all && next != NULL_TREE))
10551 return;
10553 /* If it's not incremental, just skip over the gap, so that after
10554 jumping to retry we will output the next successive element. */
10555 if (RECORD_OR_UNION_TYPE_P (constructor_type))
10556 constructor_unfilled_fields = next;
10557 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10558 constructor_unfilled_index = next;
10560 /* ELT now points to the node in the pending tree with the next
10561 initializer to output. */
10562 goto retry;
10565 /* Expression VALUE coincides with the start of type TYPE in a braced
10566 initializer. Return true if we should treat VALUE as initializing
10567 the first element of TYPE, false if we should treat it as initializing
10568 TYPE as a whole.
10570 If the initializer is clearly invalid, the question becomes:
10571 which choice gives the best error message? */
10573 static bool
10574 initialize_elementwise_p (tree type, tree value)
10576 if (type == error_mark_node || value == error_mark_node)
10577 return false;
10579 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10581 tree value_type = TREE_TYPE (value);
10582 if (value_type == error_mark_node)
10583 return false;
10585 /* GNU vectors can be initialized elementwise. However, treat any
10586 kind of vector value as initializing the vector type as a whole,
10587 regardless of whether the value is a GNU vector. Such initializers
10588 are valid if and only if they would have been valid in a non-braced
10589 initializer like:
10591 TYPE foo = VALUE;
10593 so recursing into the vector type would be at best confusing or at
10594 worst wrong. For example, when -flax-vector-conversions is in effect,
10595 it's possible to initialize a V8HI from a V4SI, even though the vectors
10596 have different element types and different numbers of elements. */
10597 if (gnu_vector_type_p (type))
10598 return !VECTOR_TYPE_P (value_type);
10600 if (AGGREGATE_TYPE_P (type))
10601 return type != TYPE_MAIN_VARIANT (value_type);
10603 return false;
10606 /* Add one non-braced element to the current constructor level.
10607 This adjusts the current position within the constructor's type.
10608 This may also start or terminate implicit levels
10609 to handle a partly-braced initializer.
10611 Once this has found the correct level for the new element,
10612 it calls output_init_element.
10614 IMPLICIT is true if value comes from pop_init_level (1),
10615 the new initializer has been merged with the existing one
10616 and thus no warnings should be emitted about overriding an
10617 existing initializer. */
10619 void
10620 process_init_element (location_t loc, struct c_expr value, bool implicit,
10621 struct obstack * braced_init_obstack)
10623 tree orig_value = value.value;
10624 int string_flag
10625 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10626 bool strict_string = value.original_code == STRING_CST;
10627 bool was_designated = designator_depth != 0;
10629 designator_depth = 0;
10630 designator_erroneous = 0;
10632 if (!implicit && value.value && !integer_zerop (value.value))
10633 constructor_zeroinit = 0;
10635 /* Handle superfluous braces around string cst as in
10636 char x[] = {"foo"}; */
10637 if (string_flag
10638 && constructor_type
10639 && !was_designated
10640 && TREE_CODE (constructor_type) == ARRAY_TYPE
10641 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10642 && integer_zerop (constructor_unfilled_index))
10644 if (constructor_stack->replacement_value.value)
10645 error_init (loc, "excess elements in %<char%> array initializer");
10646 constructor_stack->replacement_value = value;
10647 return;
10650 if (constructor_stack->replacement_value.value != NULL_TREE)
10652 error_init (loc, "excess elements in struct initializer");
10653 return;
10656 /* Ignore elements of a brace group if it is entirely superfluous
10657 and has already been diagnosed, or if the type is erroneous. */
10658 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10659 return;
10661 /* Ignore elements of an initializer for a variable-size type.
10662 Those are diagnosed in the parser (empty initializer braces are OK). */
10663 if (COMPLETE_TYPE_P (constructor_type)
10664 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10665 return;
10667 if (!implicit && warn_designated_init && !was_designated
10668 && TREE_CODE (constructor_type) == RECORD_TYPE
10669 && lookup_attribute ("designated_init",
10670 TYPE_ATTRIBUTES (constructor_type)))
10671 warning_init (loc,
10672 OPT_Wdesignated_init,
10673 "positional initialization of field "
10674 "in %<struct%> declared with %<designated_init%> attribute");
10676 /* If we've exhausted any levels that didn't have braces,
10677 pop them now. */
10678 while (constructor_stack->implicit)
10680 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10681 && constructor_fields == NULL_TREE)
10682 process_init_element (loc,
10683 pop_init_level (loc, 1, braced_init_obstack,
10684 last_init_list_comma),
10685 true, braced_init_obstack);
10686 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10687 || gnu_vector_type_p (constructor_type))
10688 && constructor_max_index
10689 && tree_int_cst_lt (constructor_max_index,
10690 constructor_index))
10691 process_init_element (loc,
10692 pop_init_level (loc, 1, braced_init_obstack,
10693 last_init_list_comma),
10694 true, braced_init_obstack);
10695 else
10696 break;
10699 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10700 if (constructor_range_stack)
10702 /* If value is a compound literal and we'll be just using its
10703 content, don't put it into a SAVE_EXPR. */
10704 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10705 || !require_constant_value)
10707 tree semantic_type = NULL_TREE;
10708 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10710 semantic_type = TREE_TYPE (value.value);
10711 value.value = TREE_OPERAND (value.value, 0);
10713 value.value = save_expr (value.value);
10714 if (semantic_type)
10715 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10716 value.value);
10720 while (1)
10722 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10724 tree fieldtype;
10725 enum tree_code fieldcode;
10727 if (constructor_fields == NULL_TREE)
10729 pedwarn_init (loc, 0, "excess elements in struct initializer");
10730 break;
10733 fieldtype = TREE_TYPE (constructor_fields);
10734 if (fieldtype != error_mark_node)
10735 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10736 fieldcode = TREE_CODE (fieldtype);
10738 /* Error for non-static initialization of a flexible array member. */
10739 if (fieldcode == ARRAY_TYPE
10740 && !require_constant_value
10741 && TYPE_SIZE (fieldtype) == NULL_TREE
10742 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10744 error_init (loc, "non-static initialization of a flexible "
10745 "array member");
10746 break;
10749 /* Error for initialization of a flexible array member with
10750 a string constant if the structure is in an array. E.g.:
10751 struct S { int x; char y[]; };
10752 struct S s[] = { { 1, "foo" } };
10753 is invalid. */
10754 if (string_flag
10755 && fieldcode == ARRAY_TYPE
10756 && constructor_depth > 1
10757 && TYPE_SIZE (fieldtype) == NULL_TREE
10758 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10760 bool in_array_p = false;
10761 for (struct constructor_stack *p = constructor_stack;
10762 p && p->type; p = p->next)
10763 if (TREE_CODE (p->type) == ARRAY_TYPE)
10765 in_array_p = true;
10766 break;
10768 if (in_array_p)
10770 error_init (loc, "initialization of flexible array "
10771 "member in a nested context");
10772 break;
10776 /* Accept a string constant to initialize a subarray. */
10777 if (value.value != NULL_TREE
10778 && fieldcode == ARRAY_TYPE
10779 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10780 && string_flag)
10781 value.value = orig_value;
10782 /* Otherwise, if we have come to a subaggregate,
10783 and we don't have an element of its type, push into it. */
10784 else if (value.value != NULL_TREE
10785 && initialize_elementwise_p (fieldtype, value.value))
10787 push_init_level (loc, 1, braced_init_obstack);
10788 continue;
10791 if (value.value)
10793 push_member_name (constructor_fields);
10794 output_init_element (loc, value.value, value.original_type,
10795 strict_string, fieldtype,
10796 constructor_fields, true, implicit,
10797 braced_init_obstack);
10798 RESTORE_SPELLING_DEPTH (constructor_depth);
10800 else
10801 /* Do the bookkeeping for an element that was
10802 directly output as a constructor. */
10804 /* For a record, keep track of end position of last field. */
10805 if (DECL_SIZE (constructor_fields))
10806 constructor_bit_index
10807 = size_binop_loc (input_location, PLUS_EXPR,
10808 bit_position (constructor_fields),
10809 DECL_SIZE (constructor_fields));
10811 /* If the current field was the first one not yet written out,
10812 it isn't now, so update. */
10813 if (constructor_unfilled_fields == constructor_fields)
10815 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10816 /* Skip any nameless bit fields. */
10817 while (constructor_unfilled_fields != 0
10818 && (DECL_UNNAMED_BIT_FIELD
10819 (constructor_unfilled_fields)))
10820 constructor_unfilled_fields =
10821 DECL_CHAIN (constructor_unfilled_fields);
10825 constructor_fields = DECL_CHAIN (constructor_fields);
10826 /* Skip any nameless bit fields at the beginning. */
10827 while (constructor_fields != NULL_TREE
10828 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10829 constructor_fields = DECL_CHAIN (constructor_fields);
10831 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10833 tree fieldtype;
10834 enum tree_code fieldcode;
10836 if (constructor_fields == NULL_TREE)
10838 pedwarn_init (loc, 0,
10839 "excess elements in union initializer");
10840 break;
10843 fieldtype = TREE_TYPE (constructor_fields);
10844 if (fieldtype != error_mark_node)
10845 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10846 fieldcode = TREE_CODE (fieldtype);
10848 /* Warn that traditional C rejects initialization of unions.
10849 We skip the warning if the value is zero. This is done
10850 under the assumption that the zero initializer in user
10851 code appears conditioned on e.g. __STDC__ to avoid
10852 "missing initializer" warnings and relies on default
10853 initialization to zero in the traditional C case.
10854 We also skip the warning if the initializer is designated,
10855 again on the assumption that this must be conditional on
10856 __STDC__ anyway (and we've already complained about the
10857 member-designator already). */
10858 if (!in_system_header_at (input_location) && !constructor_designated
10859 && !(value.value && (integer_zerop (value.value)
10860 || real_zerop (value.value))))
10861 warning (OPT_Wtraditional, "traditional C rejects initialization "
10862 "of unions");
10864 /* Accept a string constant to initialize a subarray. */
10865 if (value.value != NULL_TREE
10866 && fieldcode == ARRAY_TYPE
10867 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10868 && string_flag)
10869 value.value = orig_value;
10870 /* Otherwise, if we have come to a subaggregate,
10871 and we don't have an element of its type, push into it. */
10872 else if (value.value != NULL_TREE
10873 && initialize_elementwise_p (fieldtype, value.value))
10875 push_init_level (loc, 1, braced_init_obstack);
10876 continue;
10879 if (value.value)
10881 push_member_name (constructor_fields);
10882 output_init_element (loc, value.value, value.original_type,
10883 strict_string, fieldtype,
10884 constructor_fields, true, implicit,
10885 braced_init_obstack);
10886 RESTORE_SPELLING_DEPTH (constructor_depth);
10888 else
10889 /* Do the bookkeeping for an element that was
10890 directly output as a constructor. */
10892 constructor_bit_index = DECL_SIZE (constructor_fields);
10893 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10896 constructor_fields = NULL_TREE;
10898 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10900 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10901 enum tree_code eltcode = TREE_CODE (elttype);
10903 /* Accept a string constant to initialize a subarray. */
10904 if (value.value != NULL_TREE
10905 && eltcode == ARRAY_TYPE
10906 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10907 && string_flag)
10908 value.value = orig_value;
10909 /* Otherwise, if we have come to a subaggregate,
10910 and we don't have an element of its type, push into it. */
10911 else if (value.value != NULL_TREE
10912 && initialize_elementwise_p (elttype, value.value))
10914 push_init_level (loc, 1, braced_init_obstack);
10915 continue;
10918 if (constructor_max_index != NULL_TREE
10919 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10920 || integer_all_onesp (constructor_max_index)))
10922 pedwarn_init (loc, 0,
10923 "excess elements in array initializer");
10924 break;
10927 /* Now output the actual element. */
10928 if (value.value)
10930 push_array_bounds (tree_to_uhwi (constructor_index));
10931 output_init_element (loc, value.value, value.original_type,
10932 strict_string, elttype,
10933 constructor_index, true, implicit,
10934 braced_init_obstack);
10935 RESTORE_SPELLING_DEPTH (constructor_depth);
10938 constructor_index
10939 = size_binop_loc (input_location, PLUS_EXPR,
10940 constructor_index, bitsize_one_node);
10942 if (!value.value)
10943 /* If we are doing the bookkeeping for an element that was
10944 directly output as a constructor, we must update
10945 constructor_unfilled_index. */
10946 constructor_unfilled_index = constructor_index;
10948 else if (gnu_vector_type_p (constructor_type))
10950 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10952 /* Do a basic check of initializer size. Note that vectors
10953 always have a fixed size derived from their type. */
10954 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10956 pedwarn_init (loc, 0,
10957 "excess elements in vector initializer");
10958 break;
10961 /* Now output the actual element. */
10962 if (value.value)
10964 if (TREE_CODE (value.value) == VECTOR_CST)
10965 elttype = TYPE_MAIN_VARIANT (constructor_type);
10966 output_init_element (loc, value.value, value.original_type,
10967 strict_string, elttype,
10968 constructor_index, true, implicit,
10969 braced_init_obstack);
10972 constructor_index
10973 = size_binop_loc (input_location,
10974 PLUS_EXPR, constructor_index, bitsize_one_node);
10976 if (!value.value)
10977 /* If we are doing the bookkeeping for an element that was
10978 directly output as a constructor, we must update
10979 constructor_unfilled_index. */
10980 constructor_unfilled_index = constructor_index;
10983 /* Handle the sole element allowed in a braced initializer
10984 for a scalar variable. */
10985 else if (constructor_type != error_mark_node
10986 && constructor_fields == NULL_TREE)
10988 pedwarn_init (loc, 0,
10989 "excess elements in scalar initializer");
10990 break;
10992 else
10994 if (value.value)
10995 output_init_element (loc, value.value, value.original_type,
10996 strict_string, constructor_type,
10997 NULL_TREE, true, implicit,
10998 braced_init_obstack);
10999 constructor_fields = NULL_TREE;
11002 /* Handle range initializers either at this level or anywhere higher
11003 in the designator stack. */
11004 if (constructor_range_stack)
11006 struct constructor_range_stack *p, *range_stack;
11007 int finish = 0;
11009 range_stack = constructor_range_stack;
11010 constructor_range_stack = 0;
11011 while (constructor_stack != range_stack->stack)
11013 gcc_assert (constructor_stack->implicit);
11014 process_init_element (loc,
11015 pop_init_level (loc, 1,
11016 braced_init_obstack,
11017 last_init_list_comma),
11018 true, braced_init_obstack);
11020 for (p = range_stack;
11021 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
11022 p = p->prev)
11024 gcc_assert (constructor_stack->implicit);
11025 process_init_element (loc,
11026 pop_init_level (loc, 1,
11027 braced_init_obstack,
11028 last_init_list_comma),
11029 true, braced_init_obstack);
11032 p->index = size_binop_loc (input_location,
11033 PLUS_EXPR, p->index, bitsize_one_node);
11034 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
11035 finish = 1;
11037 while (1)
11039 constructor_index = p->index;
11040 constructor_fields = p->fields;
11041 if (finish && p->range_end && p->index == p->range_start)
11043 finish = 0;
11044 p->prev = 0;
11046 p = p->next;
11047 if (!p)
11048 break;
11049 finish_implicit_inits (loc, braced_init_obstack);
11050 push_init_level (loc, 2, braced_init_obstack);
11051 p->stack = constructor_stack;
11052 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
11053 p->index = p->range_start;
11056 if (!finish)
11057 constructor_range_stack = range_stack;
11058 continue;
11061 break;
11064 constructor_range_stack = 0;
11067 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
11068 (guaranteed to be 'volatile' or null) and ARGS (represented using
11069 an ASM_EXPR node). */
11070 tree
11071 build_asm_stmt (bool is_volatile, tree args)
11073 if (is_volatile)
11074 ASM_VOLATILE_P (args) = 1;
11075 return add_stmt (args);
11078 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
11079 some INPUTS, and some CLOBBERS. The latter three may be NULL.
11080 SIMPLE indicates whether there was anything at all after the
11081 string in the asm expression -- asm("blah") and asm("blah" : )
11082 are subtly different. We use a ASM_EXPR node to represent this.
11083 LOC is the location of the asm, and IS_INLINE says whether this
11084 is asm inline. */
11085 tree
11086 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
11087 tree clobbers, tree labels, bool simple, bool is_inline)
11089 tree tail;
11090 tree args;
11091 int i;
11092 const char *constraint;
11093 const char **oconstraints;
11094 bool allows_mem, allows_reg, is_inout;
11095 int ninputs, noutputs;
11097 ninputs = list_length (inputs);
11098 noutputs = list_length (outputs);
11099 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
11101 string = resolve_asm_operand_names (string, outputs, inputs, labels);
11103 /* Remove output conversions that change the type but not the mode. */
11104 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
11106 tree output = TREE_VALUE (tail);
11108 output = c_fully_fold (output, false, NULL, true);
11110 /* ??? Really, this should not be here. Users should be using a
11111 proper lvalue, dammit. But there's a long history of using casts
11112 in the output operands. In cases like longlong.h, this becomes a
11113 primitive form of typechecking -- if the cast can be removed, then
11114 the output operand had a type of the proper width; otherwise we'll
11115 get an error. Gross, but ... */
11116 STRIP_NOPS (output);
11118 if (!lvalue_or_else (loc, output, lv_asm))
11119 output = error_mark_node;
11121 if (output != error_mark_node
11122 && (TREE_READONLY (output)
11123 || TYPE_READONLY (TREE_TYPE (output))
11124 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
11125 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
11126 readonly_error (loc, output, lv_asm);
11128 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
11129 oconstraints[i] = constraint;
11131 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
11132 &allows_mem, &allows_reg, &is_inout))
11134 /* If the operand is going to end up in memory,
11135 mark it addressable. */
11136 if (!allows_reg && !c_mark_addressable (output))
11137 output = error_mark_node;
11138 if (!(!allows_reg && allows_mem)
11139 && output != error_mark_node
11140 && VOID_TYPE_P (TREE_TYPE (output)))
11142 error_at (loc, "invalid use of void expression");
11143 output = error_mark_node;
11146 else
11147 output = error_mark_node;
11149 TREE_VALUE (tail) = output;
11152 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
11154 tree input;
11156 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
11157 input = TREE_VALUE (tail);
11159 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
11160 oconstraints, &allows_mem, &allows_reg))
11162 /* If the operand is going to end up in memory,
11163 mark it addressable. */
11164 if (!allows_reg && allows_mem)
11166 input = c_fully_fold (input, false, NULL, true);
11168 /* Strip the nops as we allow this case. FIXME, this really
11169 should be rejected or made deprecated. */
11170 STRIP_NOPS (input);
11171 if (!c_mark_addressable (input))
11172 input = error_mark_node;
11174 else
11176 struct c_expr expr;
11177 memset (&expr, 0, sizeof (expr));
11178 expr.value = input;
11179 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
11180 input = c_fully_fold (expr.value, false, NULL);
11182 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
11184 error_at (loc, "invalid use of void expression");
11185 input = error_mark_node;
11189 else
11190 input = error_mark_node;
11192 TREE_VALUE (tail) = input;
11195 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
11197 /* asm statements without outputs, including simple ones, are treated
11198 as volatile. */
11199 ASM_INPUT_P (args) = simple;
11200 ASM_VOLATILE_P (args) = (noutputs == 0);
11201 ASM_INLINE_P (args) = is_inline;
11203 return args;
11206 /* Generate a goto statement to LABEL. LOC is the location of the
11207 GOTO. */
11209 tree
11210 c_finish_goto_label (location_t loc, tree label)
11212 tree decl = lookup_label_for_goto (loc, label);
11213 if (!decl)
11214 return NULL_TREE;
11215 TREE_USED (decl) = 1;
11217 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
11218 tree t = build1 (GOTO_EXPR, void_type_node, decl);
11219 SET_EXPR_LOCATION (t, loc);
11220 return add_stmt (t);
11224 /* Generate a computed goto statement to EXPR. LOC is the location of
11225 the GOTO. */
11227 tree
11228 c_finish_goto_ptr (location_t loc, c_expr val)
11230 tree expr = val.value;
11231 tree t;
11232 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
11233 if (expr != error_mark_node
11234 && !POINTER_TYPE_P (TREE_TYPE (expr))
11235 && !null_pointer_constant_p (expr))
11237 error_at (val.get_location (),
11238 "computed goto must be pointer type");
11239 expr = build_zero_cst (ptr_type_node);
11241 expr = c_fully_fold (expr, false, NULL);
11242 expr = convert (ptr_type_node, expr);
11243 t = build1 (GOTO_EXPR, void_type_node, expr);
11244 SET_EXPR_LOCATION (t, loc);
11245 return add_stmt (t);
11248 /* Generate a C `return' statement. RETVAL is the expression for what
11249 to return, or a null pointer for `return;' with no value. LOC is
11250 the location of the return statement, or the location of the expression,
11251 if the statement has any. If ORIGTYPE is not NULL_TREE, it
11252 is the original type of RETVAL. */
11254 tree
11255 c_finish_return (location_t loc, tree retval, tree origtype)
11257 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
11258 bool no_warning = false;
11259 bool npc = false;
11261 /* Use the expansion point to handle cases such as returning NULL
11262 in a function returning void. */
11263 location_t xloc = expansion_point_location_if_in_system_header (loc);
11265 if (TREE_THIS_VOLATILE (current_function_decl))
11266 warning_at (xloc, 0,
11267 "function declared %<noreturn%> has a %<return%> statement");
11269 if (retval)
11271 tree semantic_type = NULL_TREE;
11272 npc = null_pointer_constant_p (retval);
11273 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
11275 semantic_type = TREE_TYPE (retval);
11276 retval = TREE_OPERAND (retval, 0);
11278 retval = c_fully_fold (retval, false, NULL);
11279 if (semantic_type
11280 && valtype != NULL_TREE
11281 && TREE_CODE (valtype) != VOID_TYPE)
11282 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
11285 if (!retval)
11287 current_function_returns_null = 1;
11288 if ((warn_return_type >= 0 || flag_isoc99)
11289 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
11291 bool warned_here;
11292 if (flag_isoc99)
11293 warned_here = pedwarn
11294 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
11295 "%<return%> with no value, in function returning non-void");
11296 else
11297 warned_here = warning_at
11298 (loc, OPT_Wreturn_type,
11299 "%<return%> with no value, in function returning non-void");
11300 no_warning = true;
11301 if (warned_here)
11302 inform (DECL_SOURCE_LOCATION (current_function_decl),
11303 "declared here");
11306 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
11308 current_function_returns_null = 1;
11309 bool warned_here;
11310 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
11311 warned_here = pedwarn
11312 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
11313 "%<return%> with a value, in function returning void");
11314 else
11315 warned_here = pedwarn
11316 (xloc, OPT_Wpedantic, "ISO C forbids "
11317 "%<return%> with expression, in function returning void");
11318 if (warned_here)
11319 inform (DECL_SOURCE_LOCATION (current_function_decl),
11320 "declared here");
11322 else
11324 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
11325 retval, origtype, ic_return,
11326 npc, NULL_TREE, NULL_TREE, 0);
11327 tree res = DECL_RESULT (current_function_decl);
11328 tree inner;
11329 bool save;
11331 current_function_returns_value = 1;
11332 if (t == error_mark_node)
11333 return NULL_TREE;
11335 save = in_late_binary_op;
11336 if (C_BOOLEAN_TYPE_P (TREE_TYPE (res))
11337 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
11338 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
11339 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
11340 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
11341 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
11342 in_late_binary_op = true;
11343 inner = t = convert (TREE_TYPE (res), t);
11344 in_late_binary_op = save;
11346 /* Strip any conversions, additions, and subtractions, and see if
11347 we are returning the address of a local variable. Warn if so. */
11348 while (1)
11350 switch (TREE_CODE (inner))
11352 CASE_CONVERT:
11353 case NON_LVALUE_EXPR:
11354 case PLUS_EXPR:
11355 case POINTER_PLUS_EXPR:
11356 inner = TREE_OPERAND (inner, 0);
11357 continue;
11359 case MINUS_EXPR:
11360 /* If the second operand of the MINUS_EXPR has a pointer
11361 type (or is converted from it), this may be valid, so
11362 don't give a warning. */
11364 tree op1 = TREE_OPERAND (inner, 1);
11366 while (!POINTER_TYPE_P (TREE_TYPE (op1))
11367 && (CONVERT_EXPR_P (op1)
11368 || TREE_CODE (op1) == NON_LVALUE_EXPR))
11369 op1 = TREE_OPERAND (op1, 0);
11371 if (POINTER_TYPE_P (TREE_TYPE (op1)))
11372 break;
11374 inner = TREE_OPERAND (inner, 0);
11375 continue;
11378 case ADDR_EXPR:
11379 inner = TREE_OPERAND (inner, 0);
11381 while (REFERENCE_CLASS_P (inner)
11382 && !INDIRECT_REF_P (inner))
11383 inner = TREE_OPERAND (inner, 0);
11385 if (DECL_P (inner)
11386 && !DECL_EXTERNAL (inner)
11387 && !TREE_STATIC (inner)
11388 && DECL_CONTEXT (inner) == current_function_decl
11389 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
11391 if (TREE_CODE (inner) == LABEL_DECL)
11392 warning_at (loc, OPT_Wreturn_local_addr,
11393 "function returns address of label");
11394 else
11396 warning_at (loc, OPT_Wreturn_local_addr,
11397 "function returns address of local variable");
11398 tree zero = build_zero_cst (TREE_TYPE (res));
11399 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
11402 break;
11404 default:
11405 break;
11408 break;
11411 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
11412 SET_EXPR_LOCATION (retval, loc);
11414 if (warn_sequence_point)
11415 verify_sequence_points (retval);
11418 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
11419 if (no_warning)
11420 suppress_warning (ret_stmt, OPT_Wreturn_type);
11421 return add_stmt (ret_stmt);
11424 struct c_switch {
11425 /* The SWITCH_STMT being built. */
11426 tree switch_stmt;
11428 /* The original type of the testing expression, i.e. before the
11429 default conversion is applied. */
11430 tree orig_type;
11432 /* A splay-tree mapping the low element of a case range to the high
11433 element, or NULL_TREE if there is no high element. Used to
11434 determine whether or not a new case label duplicates an old case
11435 label. We need a tree, rather than simply a hash table, because
11436 of the GNU case range extension. */
11437 splay_tree cases;
11439 /* The bindings at the point of the switch. This is used for
11440 warnings crossing decls when branching to a case label. */
11441 struct c_spot_bindings *bindings;
11443 /* Whether the switch includes any break statements. */
11444 bool break_stmt_seen_p;
11446 /* The next node on the stack. */
11447 struct c_switch *next;
11449 /* Remember whether the controlling expression had boolean type
11450 before integer promotions for the sake of -Wswitch-bool. */
11451 bool bool_cond_p;
11454 /* A stack of the currently active switch statements. The innermost
11455 switch statement is on the top of the stack. There is no need to
11456 mark the stack for garbage collection because it is only active
11457 during the processing of the body of a function, and we never
11458 collect at that point. */
11460 struct c_switch *c_switch_stack;
11462 /* Start a C switch statement, testing expression EXP. Return the new
11463 SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
11464 SWITCH_COND_LOC is the location of the switch's condition.
11465 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
11467 tree
11468 c_start_switch (location_t switch_loc,
11469 location_t switch_cond_loc,
11470 tree exp, bool explicit_cast_p)
11472 tree orig_type = error_mark_node;
11473 bool bool_cond_p = false;
11474 struct c_switch *cs;
11476 if (exp != error_mark_node)
11478 orig_type = TREE_TYPE (exp);
11480 if (!INTEGRAL_TYPE_P (orig_type))
11482 if (orig_type != error_mark_node)
11484 error_at (switch_cond_loc, "switch quantity not an integer");
11485 orig_type = error_mark_node;
11487 exp = integer_zero_node;
11489 else
11491 tree type = TYPE_MAIN_VARIANT (orig_type);
11492 tree e = exp;
11494 /* Warn if the condition has boolean value. */
11495 while (TREE_CODE (e) == COMPOUND_EXPR)
11496 e = TREE_OPERAND (e, 1);
11498 if ((C_BOOLEAN_TYPE_P (type)
11499 || truth_value_p (TREE_CODE (e)))
11500 /* Explicit cast to int suppresses this warning. */
11501 && !(TREE_CODE (type) == INTEGER_TYPE
11502 && explicit_cast_p))
11503 bool_cond_p = true;
11505 if (!in_system_header_at (input_location)
11506 && (type == long_integer_type_node
11507 || type == long_unsigned_type_node))
11508 warning_at (switch_cond_loc,
11509 OPT_Wtraditional, "%<long%> switch expression not "
11510 "converted to %<int%> in ISO C");
11512 exp = c_fully_fold (exp, false, NULL);
11513 exp = default_conversion (exp);
11515 if (warn_sequence_point)
11516 verify_sequence_points (exp);
11520 /* Add this new SWITCH_STMT to the stack. */
11521 cs = XNEW (struct c_switch);
11522 cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11523 NULL_TREE, orig_type, NULL_TREE);
11524 cs->orig_type = orig_type;
11525 cs->cases = splay_tree_new (case_compare, NULL, NULL);
11526 cs->bindings = c_get_switch_bindings ();
11527 cs->break_stmt_seen_p = false;
11528 cs->bool_cond_p = bool_cond_p;
11529 cs->next = c_switch_stack;
11530 c_switch_stack = cs;
11532 return add_stmt (cs->switch_stmt);
11535 /* Process a case label at location LOC, with attributes ATTRS. */
11537 tree
11538 do_case (location_t loc, tree low_value, tree high_value, tree attrs)
11540 tree label = NULL_TREE;
11542 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11544 low_value = c_fully_fold (low_value, false, NULL);
11545 if (TREE_CODE (low_value) == INTEGER_CST)
11546 pedwarn (loc, OPT_Wpedantic,
11547 "case label is not an integer constant expression");
11550 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11552 high_value = c_fully_fold (high_value, false, NULL);
11553 if (TREE_CODE (high_value) == INTEGER_CST)
11554 pedwarn (input_location, OPT_Wpedantic,
11555 "case label is not an integer constant expression");
11558 if (c_switch_stack == NULL)
11560 if (low_value)
11561 error_at (loc, "case label not within a switch statement");
11562 else
11563 error_at (loc, "%<default%> label not within a switch statement");
11564 return NULL_TREE;
11567 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11568 EXPR_LOCATION (c_switch_stack->switch_stmt),
11569 loc))
11570 return NULL_TREE;
11572 label = c_add_case_label (loc, c_switch_stack->cases,
11573 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11574 low_value, high_value, attrs);
11575 if (label == error_mark_node)
11576 label = NULL_TREE;
11577 return label;
11580 /* Finish the switch statement. TYPE is the original type of the
11581 controlling expression of the switch, or NULL_TREE. */
11583 void
11584 c_finish_switch (tree body, tree type)
11586 struct c_switch *cs = c_switch_stack;
11587 location_t switch_location;
11589 SWITCH_STMT_BODY (cs->switch_stmt) = body;
11591 /* Emit warnings as needed. */
11592 switch_location = EXPR_LOCATION (cs->switch_stmt);
11593 c_do_switch_warnings (cs->cases, switch_location,
11594 type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11595 SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11596 if (c_switch_covers_all_cases_p (cs->cases,
11597 SWITCH_STMT_TYPE (cs->switch_stmt)))
11598 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11599 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11601 /* Pop the stack. */
11602 c_switch_stack = cs->next;
11603 splay_tree_delete (cs->cases);
11604 c_release_switch_bindings (cs->bindings);
11605 XDELETE (cs);
11608 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
11609 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11610 may be null. */
11612 void
11613 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11614 tree else_block)
11616 tree stmt;
11618 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11619 SET_EXPR_LOCATION (stmt, if_locus);
11620 add_stmt (stmt);
11623 tree
11624 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11626 /* In switch statements break is sometimes stylistically used after
11627 a return statement. This can lead to spurious warnings about
11628 control reaching the end of a non-void function when it is
11629 inlined. Note that we are calling block_may_fallthru with
11630 language specific tree nodes; this works because
11631 block_may_fallthru returns true when given something it does not
11632 understand. */
11633 bool skip = !block_may_fallthru (cur_stmt_list);
11635 if (is_break)
11636 switch (in_statement)
11638 case 0:
11639 error_at (loc, "break statement not within loop or switch");
11640 return NULL_TREE;
11641 case IN_OMP_BLOCK:
11642 error_at (loc, "invalid exit from OpenMP structured block");
11643 return NULL_TREE;
11644 case IN_OMP_FOR:
11645 error_at (loc, "break statement used with OpenMP for loop");
11646 return NULL_TREE;
11647 case IN_ITERATION_STMT:
11648 case IN_OBJC_FOREACH:
11649 break;
11650 default:
11651 gcc_assert (in_statement & IN_SWITCH_STMT);
11652 c_switch_stack->break_stmt_seen_p = true;
11653 break;
11655 else
11656 switch (in_statement & ~IN_SWITCH_STMT)
11658 case 0:
11659 error_at (loc, "continue statement not within a loop");
11660 return NULL_TREE;
11661 case IN_OMP_BLOCK:
11662 error_at (loc, "invalid exit from OpenMP structured block");
11663 return NULL_TREE;
11664 case IN_ITERATION_STMT:
11665 case IN_OMP_FOR:
11666 case IN_OBJC_FOREACH:
11667 break;
11668 default:
11669 gcc_unreachable ();
11672 if (skip)
11673 return NULL_TREE;
11674 else if ((in_statement & IN_OBJC_FOREACH)
11675 && !(is_break && (in_statement & IN_SWITCH_STMT)))
11677 /* The foreach expander produces low-level code using gotos instead
11678 of a structured loop construct. */
11679 gcc_assert (label);
11680 return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11682 return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11685 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11687 static void
11688 emit_side_effect_warnings (location_t loc, tree expr)
11690 maybe_warn_nodiscard (loc, expr);
11691 if (!warn_unused_value)
11692 return;
11693 if (expr == error_mark_node)
11695 else if (!TREE_SIDE_EFFECTS (expr))
11697 if (!VOID_TYPE_P (TREE_TYPE (expr))
11698 && !warning_suppressed_p (expr, OPT_Wunused_value))
11699 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11701 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11703 tree r = expr;
11704 location_t cloc = loc;
11705 while (TREE_CODE (r) == COMPOUND_EXPR)
11707 if (EXPR_HAS_LOCATION (r))
11708 cloc = EXPR_LOCATION (r);
11709 r = TREE_OPERAND (r, 1);
11711 if (!TREE_SIDE_EFFECTS (r)
11712 && !VOID_TYPE_P (TREE_TYPE (r))
11713 && !CONVERT_EXPR_P (r)
11714 && !warning_suppressed_p (r, OPT_Wunused_value)
11715 && !warning_suppressed_p (expr, OPT_Wunused_value))
11716 warning_at (cloc, OPT_Wunused_value,
11717 "right-hand operand of comma expression has no effect");
11719 else
11720 warn_if_unused_value (expr, loc);
11723 /* Process an expression as if it were a complete statement. Emit
11724 diagnostics, but do not call ADD_STMT. LOC is the location of the
11725 statement. */
11727 tree
11728 c_process_expr_stmt (location_t loc, tree expr)
11730 tree exprv;
11732 if (!expr)
11733 return NULL_TREE;
11735 expr = c_fully_fold (expr, false, NULL);
11737 if (warn_sequence_point)
11738 verify_sequence_points (expr);
11740 if (TREE_TYPE (expr) != error_mark_node
11741 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11742 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11743 error_at (loc, "expression statement has incomplete type");
11745 /* If we're not processing a statement expression, warn about unused values.
11746 Warnings for statement expressions will be emitted later, once we figure
11747 out which is the result. */
11748 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11749 && (warn_unused_value || warn_unused_result))
11750 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11752 exprv = expr;
11753 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11754 exprv = TREE_OPERAND (exprv, 1);
11755 while (CONVERT_EXPR_P (exprv))
11756 exprv = TREE_OPERAND (exprv, 0);
11757 if (DECL_P (exprv)
11758 || handled_component_p (exprv)
11759 || TREE_CODE (exprv) == ADDR_EXPR)
11760 mark_exp_read (exprv);
11762 /* If the expression is not of a type to which we cannot assign a line
11763 number, wrap the thing in a no-op NOP_EXPR. */
11764 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11766 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11767 SET_EXPR_LOCATION (expr, loc);
11770 return expr;
11773 /* Emit an expression as a statement. LOC is the location of the
11774 expression. */
11776 tree
11777 c_finish_expr_stmt (location_t loc, tree expr)
11779 if (expr)
11780 return add_stmt (c_process_expr_stmt (loc, expr));
11781 else
11782 return NULL;
11785 /* Do the opposite and emit a statement as an expression. To begin,
11786 create a new binding level and return it. */
11788 tree
11789 c_begin_stmt_expr (void)
11791 tree ret;
11793 /* We must force a BLOCK for this level so that, if it is not expanded
11794 later, there is a way to turn off the entire subtree of blocks that
11795 are contained in it. */
11796 keep_next_level ();
11797 ret = c_begin_compound_stmt (true);
11799 c_bindings_start_stmt_expr (c_switch_stack == NULL
11800 ? NULL
11801 : c_switch_stack->bindings);
11803 /* Mark the current statement list as belonging to a statement list. */
11804 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11806 return ret;
11809 /* LOC is the location of the compound statement to which this body
11810 belongs. */
11812 tree
11813 c_finish_stmt_expr (location_t loc, tree body)
11815 tree last, type, tmp, val;
11816 tree *last_p;
11818 body = c_end_compound_stmt (loc, body, true);
11820 c_bindings_end_stmt_expr (c_switch_stack == NULL
11821 ? NULL
11822 : c_switch_stack->bindings);
11824 /* Locate the last statement in BODY. See c_end_compound_stmt
11825 about always returning a BIND_EXPR. */
11826 last_p = &BIND_EXPR_BODY (body);
11827 last = BIND_EXPR_BODY (body);
11829 continue_searching:
11830 if (TREE_CODE (last) == STATEMENT_LIST)
11832 tree_stmt_iterator l = tsi_last (last);
11834 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11835 tsi_prev (&l);
11837 /* This can happen with degenerate cases like ({ }). No value. */
11838 if (tsi_end_p (l))
11839 return body;
11841 /* If we're supposed to generate side effects warnings, process
11842 all of the statements except the last. */
11843 if (warn_unused_value || warn_unused_result)
11845 for (tree_stmt_iterator i = tsi_start (last);
11846 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11848 location_t tloc;
11849 tree t = tsi_stmt (i);
11851 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11852 emit_side_effect_warnings (tloc, t);
11855 last_p = tsi_stmt_ptr (l);
11856 last = *last_p;
11859 /* If the end of the list is exception related, then the list was split
11860 by a call to push_cleanup. Continue searching. */
11861 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11862 || TREE_CODE (last) == TRY_CATCH_EXPR)
11864 last_p = &TREE_OPERAND (last, 0);
11865 last = *last_p;
11866 goto continue_searching;
11869 if (last == error_mark_node)
11870 return last;
11872 /* In the case that the BIND_EXPR is not necessary, return the
11873 expression out from inside it. */
11874 if ((last == BIND_EXPR_BODY (body)
11875 /* Skip nested debug stmts. */
11876 || last == expr_first (BIND_EXPR_BODY (body)))
11877 && BIND_EXPR_VARS (body) == NULL)
11879 /* Even if this looks constant, do not allow it in a constant
11880 expression. */
11881 last = c_wrap_maybe_const (last, true);
11882 /* Do not warn if the return value of a statement expression is
11883 unused. */
11884 suppress_warning (last, OPT_Wunused);
11885 return last;
11888 /* Extract the type of said expression. */
11889 type = TREE_TYPE (last);
11891 /* If we're not returning a value at all, then the BIND_EXPR that
11892 we already have is a fine expression to return. */
11893 if (!type || VOID_TYPE_P (type))
11894 return body;
11896 /* Now that we've located the expression containing the value, it seems
11897 silly to make voidify_wrapper_expr repeat the process. Create a
11898 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11899 tmp = create_tmp_var_raw (type);
11901 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11902 tree_expr_nonnegative_p giving up immediately. */
11903 val = last;
11904 if (TREE_CODE (val) == NOP_EXPR
11905 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11906 val = TREE_OPERAND (val, 0);
11908 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11909 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11912 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11913 SET_EXPR_LOCATION (t, loc);
11914 return t;
11918 /* Begin and end compound statements. This is as simple as pushing
11919 and popping new statement lists from the tree. */
11921 tree
11922 c_begin_compound_stmt (bool do_scope)
11924 tree stmt = push_stmt_list ();
11925 if (do_scope)
11926 push_scope ();
11927 return stmt;
11930 /* End a compound statement. STMT is the statement. LOC is the
11931 location of the compound statement-- this is usually the location
11932 of the opening brace. */
11934 tree
11935 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11937 tree block = NULL;
11939 if (do_scope)
11941 if (c_dialect_objc ())
11942 objc_clear_super_receiver ();
11943 block = pop_scope ();
11946 stmt = pop_stmt_list (stmt);
11947 stmt = c_build_bind_expr (loc, block, stmt);
11949 /* If this compound statement is nested immediately inside a statement
11950 expression, then force a BIND_EXPR to be created. Otherwise we'll
11951 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11952 STATEMENT_LISTs merge, and thus we can lose track of what statement
11953 was really last. */
11954 if (building_stmt_list_p ()
11955 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11956 && TREE_CODE (stmt) != BIND_EXPR)
11958 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11959 TREE_SIDE_EFFECTS (stmt) = 1;
11960 SET_EXPR_LOCATION (stmt, loc);
11963 return stmt;
11966 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11967 when the current scope is exited. EH_ONLY is true when this is not
11968 meant to apply to normal control flow transfer. */
11970 void
11971 push_cleanup (tree decl, tree cleanup, bool eh_only)
11973 enum tree_code code;
11974 tree stmt, list;
11975 bool stmt_expr;
11977 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11978 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11979 add_stmt (stmt);
11980 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11981 list = push_stmt_list ();
11982 TREE_OPERAND (stmt, 0) = list;
11983 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11986 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11987 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11989 static tree
11990 build_vec_cmp (tree_code code, tree type,
11991 tree arg0, tree arg1)
11993 tree zero_vec = build_zero_cst (type);
11994 tree minus_one_vec = build_minus_one_cst (type);
11995 tree cmp_type = truth_type_for (type);
11996 tree cmp = build2 (code, cmp_type, arg0, arg1);
11997 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
12000 /* Possibly warn about an address of OP never being NULL in a comparison
12001 operation CODE involving null. */
12003 static void
12004 maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
12006 /* Prevent warnings issued for macro expansion. */
12007 if (!warn_address
12008 || warning_suppressed_p (op, OPT_Waddress)
12009 || from_macro_expansion_at (loc))
12010 return;
12012 if (TREE_CODE (op) == NOP_EXPR)
12014 /* Allow casts to intptr_t to suppress the warning. */
12015 tree type = TREE_TYPE (op);
12016 if (TREE_CODE (type) == INTEGER_TYPE)
12017 return;
12018 op = TREE_OPERAND (op, 0);
12021 if (TREE_CODE (op) == POINTER_PLUS_EXPR)
12023 /* Allow a cast to void* to suppress the warning. */
12024 tree type = TREE_TYPE (TREE_TYPE (op));
12025 if (VOID_TYPE_P (type))
12026 return;
12028 /* Adding any value to a null pointer, including zero, is undefined
12029 in C. This includes the expression &p[0] where p is the null
12030 pointer, although &p[0] will have been folded to p by this point
12031 and so not diagnosed. */
12032 if (code == EQ_EXPR)
12033 warning_at (loc, OPT_Waddress,
12034 "the comparison will always evaluate as %<false%> "
12035 "for the pointer operand in %qE must not be NULL",
12036 op);
12037 else
12038 warning_at (loc, OPT_Waddress,
12039 "the comparison will always evaluate as %<true%> "
12040 "for the pointer operand in %qE must not be NULL",
12041 op);
12043 return;
12046 if (TREE_CODE (op) != ADDR_EXPR)
12047 return;
12049 op = TREE_OPERAND (op, 0);
12051 if (TREE_CODE (op) == IMAGPART_EXPR
12052 || TREE_CODE (op) == REALPART_EXPR)
12054 /* The address of either complex part may not be null. */
12055 if (code == EQ_EXPR)
12056 warning_at (loc, OPT_Waddress,
12057 "the comparison will always evaluate as %<false%> "
12058 "for the address of %qE will never be NULL",
12059 op);
12060 else
12061 warning_at (loc, OPT_Waddress,
12062 "the comparison will always evaluate as %<true%> "
12063 "for the address of %qE will never be NULL",
12064 op);
12065 return;
12068 /* Set to true in the loop below if OP dereferences is operand.
12069 In such a case the ultimate target need not be a decl for
12070 the null [in]equality test to be constant. */
12071 bool deref = false;
12073 /* Get the outermost array or object, or member. */
12074 while (handled_component_p (op))
12076 if (TREE_CODE (op) == COMPONENT_REF)
12078 /* Get the member (its address is never null). */
12079 op = TREE_OPERAND (op, 1);
12080 break;
12083 /* Get the outer array/object to refer to in the warning. */
12084 op = TREE_OPERAND (op, 0);
12085 deref = true;
12088 if ((!deref && !decl_with_nonnull_addr_p (op))
12089 || from_macro_expansion_at (loc))
12090 return;
12092 bool w;
12093 if (code == EQ_EXPR)
12094 w = warning_at (loc, OPT_Waddress,
12095 "the comparison will always evaluate as %<false%> "
12096 "for the address of %qE will never be NULL",
12097 op);
12098 else
12099 w = warning_at (loc, OPT_Waddress,
12100 "the comparison will always evaluate as %<true%> "
12101 "for the address of %qE will never be NULL",
12102 op);
12104 if (w && DECL_P (op))
12105 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
12108 /* Build a binary-operation expression without default conversions.
12109 CODE is the kind of expression to build.
12110 LOCATION is the operator's location.
12111 This function differs from `build' in several ways:
12112 the data type of the result is computed and recorded in it,
12113 warnings are generated if arg data types are invalid,
12114 special handling for addition and subtraction of pointers is known,
12115 and some optimization is done (operations on narrow ints
12116 are done in the narrower type when that gives the same result).
12117 Constant folding is also done before the result is returned.
12119 Note that the operands will never have enumeral types, or function
12120 or array types, because either they will have the default conversions
12121 performed or they have both just been converted to some other type in which
12122 the arithmetic is to be done. */
12124 tree
12125 build_binary_op (location_t location, enum tree_code code,
12126 tree orig_op0, tree orig_op1, bool convert_p)
12128 tree type0, type1, orig_type0, orig_type1;
12129 tree eptype;
12130 enum tree_code code0, code1;
12131 tree op0, op1;
12132 tree ret = error_mark_node;
12133 const char *invalid_op_diag;
12134 bool op0_int_operands, op1_int_operands;
12135 bool int_const, int_const_or_overflow, int_operands;
12137 /* Expression code to give to the expression when it is built.
12138 Normally this is CODE, which is what the caller asked for,
12139 but in some special cases we change it. */
12140 enum tree_code resultcode = code;
12142 /* Data type in which the computation is to be performed.
12143 In the simplest cases this is the common type of the arguments. */
12144 tree result_type = NULL;
12146 /* When the computation is in excess precision, the type of the
12147 final EXCESS_PRECISION_EXPR. */
12148 tree semantic_result_type = NULL;
12150 /* Nonzero means operands have already been type-converted
12151 in whatever way is necessary.
12152 Zero means they need to be converted to RESULT_TYPE. */
12153 int converted = 0;
12155 /* Nonzero means create the expression with this type, rather than
12156 RESULT_TYPE. */
12157 tree build_type = NULL_TREE;
12159 /* Nonzero means after finally constructing the expression
12160 convert it to this type. */
12161 tree final_type = NULL_TREE;
12163 /* Nonzero if this is an operation like MIN or MAX which can
12164 safely be computed in short if both args are promoted shorts.
12165 Also implies COMMON.
12166 -1 indicates a bitwise operation; this makes a difference
12167 in the exact conditions for when it is safe to do the operation
12168 in a narrower mode. */
12169 int shorten = 0;
12171 /* Nonzero if this is a comparison operation;
12172 if both args are promoted shorts, compare the original shorts.
12173 Also implies COMMON. */
12174 int short_compare = 0;
12176 /* Nonzero if this is a right-shift operation, which can be computed on the
12177 original short and then promoted if the operand is a promoted short. */
12178 int short_shift = 0;
12180 /* Nonzero means set RESULT_TYPE to the common type of the args. */
12181 int common = 0;
12183 /* True means types are compatible as far as ObjC is concerned. */
12184 bool objc_ok;
12186 /* True means this is an arithmetic operation that may need excess
12187 precision. */
12188 bool may_need_excess_precision;
12190 /* True means this is a boolean operation that converts both its
12191 operands to truth-values. */
12192 bool boolean_op = false;
12194 /* Remember whether we're doing / or %. */
12195 bool doing_div_or_mod = false;
12197 /* Remember whether we're doing << or >>. */
12198 bool doing_shift = false;
12200 /* Tree holding instrumentation expression. */
12201 tree instrument_expr = NULL;
12203 if (location == UNKNOWN_LOCATION)
12204 location = input_location;
12206 op0 = orig_op0;
12207 op1 = orig_op1;
12209 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
12210 if (op0_int_operands)
12211 op0 = remove_c_maybe_const_expr (op0);
12212 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
12213 if (op1_int_operands)
12214 op1 = remove_c_maybe_const_expr (op1);
12215 int_operands = (op0_int_operands && op1_int_operands);
12216 if (int_operands)
12218 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
12219 && TREE_CODE (orig_op1) == INTEGER_CST);
12220 int_const = (int_const_or_overflow
12221 && !TREE_OVERFLOW (orig_op0)
12222 && !TREE_OVERFLOW (orig_op1));
12224 else
12225 int_const = int_const_or_overflow = false;
12227 /* Do not apply default conversion in mixed vector/scalar expression. */
12228 if (convert_p
12229 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
12231 op0 = default_conversion (op0);
12232 op1 = default_conversion (op1);
12235 orig_type0 = type0 = TREE_TYPE (op0);
12237 orig_type1 = type1 = TREE_TYPE (op1);
12239 /* The expression codes of the data types of the arguments tell us
12240 whether the arguments are integers, floating, pointers, etc. */
12241 code0 = TREE_CODE (type0);
12242 code1 = TREE_CODE (type1);
12244 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
12245 STRIP_TYPE_NOPS (op0);
12246 STRIP_TYPE_NOPS (op1);
12248 /* If an error was already reported for one of the arguments,
12249 avoid reporting another error. */
12251 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12252 return error_mark_node;
12254 if (code0 == POINTER_TYPE
12255 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
12256 return error_mark_node;
12258 if (code1 == POINTER_TYPE
12259 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
12260 return error_mark_node;
12262 if ((invalid_op_diag
12263 = targetm.invalid_binary_op (code, type0, type1)))
12265 error_at (location, invalid_op_diag);
12266 return error_mark_node;
12269 switch (code)
12271 case PLUS_EXPR:
12272 case MINUS_EXPR:
12273 case MULT_EXPR:
12274 case TRUNC_DIV_EXPR:
12275 case CEIL_DIV_EXPR:
12276 case FLOOR_DIV_EXPR:
12277 case ROUND_DIV_EXPR:
12278 case EXACT_DIV_EXPR:
12279 may_need_excess_precision = true;
12280 break;
12282 case EQ_EXPR:
12283 case NE_EXPR:
12284 case LE_EXPR:
12285 case GE_EXPR:
12286 case LT_EXPR:
12287 case GT_EXPR:
12288 /* Excess precision for implicit conversions of integers to
12289 floating point in C11 and later. */
12290 may_need_excess_precision = (flag_isoc11
12291 && (ANY_INTEGRAL_TYPE_P (type0)
12292 || ANY_INTEGRAL_TYPE_P (type1)));
12293 break;
12295 default:
12296 may_need_excess_precision = false;
12297 break;
12299 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
12301 op0 = TREE_OPERAND (op0, 0);
12302 type0 = TREE_TYPE (op0);
12304 else if (may_need_excess_precision
12305 && (eptype = excess_precision_type (type0)) != NULL_TREE)
12307 type0 = eptype;
12308 op0 = convert (eptype, op0);
12310 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
12312 op1 = TREE_OPERAND (op1, 0);
12313 type1 = TREE_TYPE (op1);
12315 else if (may_need_excess_precision
12316 && (eptype = excess_precision_type (type1)) != NULL_TREE)
12318 type1 = eptype;
12319 op1 = convert (eptype, op1);
12322 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
12324 /* In case when one of the operands of the binary operation is
12325 a vector and another is a scalar -- convert scalar to vector. */
12326 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
12327 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
12329 enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
12330 orig_op1, true);
12332 switch (convert_flag)
12334 case stv_error:
12335 return error_mark_node;
12336 case stv_firstarg:
12338 bool maybe_const = true;
12339 tree sc;
12340 sc = c_fully_fold (op0, false, &maybe_const);
12341 sc = save_expr (sc);
12342 sc = convert (TREE_TYPE (type1), sc);
12343 op0 = build_vector_from_val (type1, sc);
12344 if (!maybe_const)
12345 op0 = c_wrap_maybe_const (op0, true);
12346 orig_type0 = type0 = TREE_TYPE (op0);
12347 code0 = TREE_CODE (type0);
12348 converted = 1;
12349 break;
12351 case stv_secondarg:
12353 bool maybe_const = true;
12354 tree sc;
12355 sc = c_fully_fold (op1, false, &maybe_const);
12356 sc = save_expr (sc);
12357 sc = convert (TREE_TYPE (type0), sc);
12358 op1 = build_vector_from_val (type0, sc);
12359 if (!maybe_const)
12360 op1 = c_wrap_maybe_const (op1, true);
12361 orig_type1 = type1 = TREE_TYPE (op1);
12362 code1 = TREE_CODE (type1);
12363 converted = 1;
12364 break;
12366 default:
12367 break;
12371 switch (code)
12373 case PLUS_EXPR:
12374 /* Handle the pointer + int case. */
12375 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12377 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
12378 goto return_build_binary_op;
12380 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
12382 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
12383 goto return_build_binary_op;
12385 else
12386 common = 1;
12387 break;
12389 case MINUS_EXPR:
12390 /* Subtraction of two similar pointers.
12391 We must subtract them as integers, then divide by object size. */
12392 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
12393 && comp_target_types (location, type0, type1))
12395 ret = pointer_diff (location, op0, op1, &instrument_expr);
12396 goto return_build_binary_op;
12398 /* Handle pointer minus int. Just like pointer plus int. */
12399 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12401 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
12402 goto return_build_binary_op;
12404 else
12405 common = 1;
12406 break;
12408 case MULT_EXPR:
12409 common = 1;
12410 break;
12412 case TRUNC_DIV_EXPR:
12413 case CEIL_DIV_EXPR:
12414 case FLOOR_DIV_EXPR:
12415 case ROUND_DIV_EXPR:
12416 case EXACT_DIV_EXPR:
12417 doing_div_or_mod = true;
12418 warn_for_div_by_zero (location, op1);
12420 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12421 || code0 == FIXED_POINT_TYPE
12422 || code0 == COMPLEX_TYPE
12423 || gnu_vector_type_p (type0))
12424 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12425 || code1 == FIXED_POINT_TYPE
12426 || code1 == COMPLEX_TYPE
12427 || gnu_vector_type_p (type1)))
12429 enum tree_code tcode0 = code0, tcode1 = code1;
12431 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
12432 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
12433 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
12434 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
12436 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
12437 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
12438 resultcode = RDIV_EXPR;
12439 else
12440 /* Although it would be tempting to shorten always here, that
12441 loses on some targets, since the modulo instruction is
12442 undefined if the quotient can't be represented in the
12443 computation mode. We shorten only if unsigned or if
12444 dividing by something we know != -1. */
12445 shorten = may_shorten_divmod (op0, op1);
12446 common = 1;
12448 break;
12450 case BIT_AND_EXPR:
12451 case BIT_IOR_EXPR:
12452 case BIT_XOR_EXPR:
12453 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12454 shorten = -1;
12455 /* Allow vector types which are not floating point types. */
12456 else if (gnu_vector_type_p (type0)
12457 && gnu_vector_type_p (type1)
12458 && !VECTOR_FLOAT_TYPE_P (type0)
12459 && !VECTOR_FLOAT_TYPE_P (type1))
12460 common = 1;
12461 break;
12463 case TRUNC_MOD_EXPR:
12464 case FLOOR_MOD_EXPR:
12465 doing_div_or_mod = true;
12466 warn_for_div_by_zero (location, op1);
12468 if (gnu_vector_type_p (type0)
12469 && gnu_vector_type_p (type1)
12470 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12471 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12472 common = 1;
12473 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12475 /* Although it would be tempting to shorten always here, that loses
12476 on some targets, since the modulo instruction is undefined if the
12477 quotient can't be represented in the computation mode. We shorten
12478 only if unsigned or if dividing by something we know != -1. */
12479 shorten = may_shorten_divmod (op0, op1);
12480 common = 1;
12482 break;
12484 case TRUTH_ANDIF_EXPR:
12485 case TRUTH_ORIF_EXPR:
12486 case TRUTH_AND_EXPR:
12487 case TRUTH_OR_EXPR:
12488 case TRUTH_XOR_EXPR:
12489 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
12490 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12491 || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE)
12492 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
12493 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12494 || code1 == FIXED_POINT_TYPE || code1 == NULLPTR_TYPE))
12496 /* Result of these operations is always an int,
12497 but that does not mean the operands should be
12498 converted to ints! */
12499 result_type = integer_type_node;
12500 if (op0_int_operands)
12502 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
12503 op0 = remove_c_maybe_const_expr (op0);
12505 else
12506 op0 = c_objc_common_truthvalue_conversion (location, op0);
12507 if (op1_int_operands)
12509 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
12510 op1 = remove_c_maybe_const_expr (op1);
12512 else
12513 op1 = c_objc_common_truthvalue_conversion (location, op1);
12514 converted = 1;
12515 boolean_op = true;
12517 if (code == TRUTH_ANDIF_EXPR)
12519 int_const_or_overflow = (int_operands
12520 && TREE_CODE (orig_op0) == INTEGER_CST
12521 && (op0 == truthvalue_false_node
12522 || TREE_CODE (orig_op1) == INTEGER_CST));
12523 int_const = (int_const_or_overflow
12524 && !TREE_OVERFLOW (orig_op0)
12525 && (op0 == truthvalue_false_node
12526 || !TREE_OVERFLOW (orig_op1)));
12528 else if (code == TRUTH_ORIF_EXPR)
12530 int_const_or_overflow = (int_operands
12531 && TREE_CODE (orig_op0) == INTEGER_CST
12532 && (op0 == truthvalue_true_node
12533 || TREE_CODE (orig_op1) == INTEGER_CST));
12534 int_const = (int_const_or_overflow
12535 && !TREE_OVERFLOW (orig_op0)
12536 && (op0 == truthvalue_true_node
12537 || !TREE_OVERFLOW (orig_op1)));
12539 break;
12541 /* Shift operations: result has same type as first operand;
12542 always convert second operand to int.
12543 Also set SHORT_SHIFT if shifting rightward. */
12545 case RSHIFT_EXPR:
12546 if (gnu_vector_type_p (type0)
12547 && gnu_vector_type_p (type1)
12548 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12549 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12550 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12551 TYPE_VECTOR_SUBPARTS (type1)))
12553 result_type = type0;
12554 converted = 1;
12556 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12557 || (gnu_vector_type_p (type0)
12558 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12559 && code1 == INTEGER_TYPE)
12561 doing_shift = true;
12562 if (TREE_CODE (op1) == INTEGER_CST)
12564 if (tree_int_cst_sgn (op1) < 0)
12566 int_const = false;
12567 if (c_inhibit_evaluation_warnings == 0)
12568 warning_at (location, OPT_Wshift_count_negative,
12569 "right shift count is negative");
12571 else if (code0 == VECTOR_TYPE)
12573 if (compare_tree_int (op1,
12574 TYPE_PRECISION (TREE_TYPE (type0)))
12575 >= 0)
12577 int_const = false;
12578 if (c_inhibit_evaluation_warnings == 0)
12579 warning_at (location, OPT_Wshift_count_overflow,
12580 "right shift count >= width of vector element");
12583 else
12585 if (!integer_zerop (op1))
12586 short_shift = 1;
12588 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12590 int_const = false;
12591 if (c_inhibit_evaluation_warnings == 0)
12592 warning_at (location, OPT_Wshift_count_overflow,
12593 "right shift count >= width of type");
12598 /* Use the type of the value to be shifted. */
12599 result_type = type0;
12600 /* Avoid converting op1 to result_type later. */
12601 converted = 1;
12603 break;
12605 case LSHIFT_EXPR:
12606 if (gnu_vector_type_p (type0)
12607 && gnu_vector_type_p (type1)
12608 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12609 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12610 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12611 TYPE_VECTOR_SUBPARTS (type1)))
12613 result_type = type0;
12614 converted = 1;
12616 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12617 || (gnu_vector_type_p (type0)
12618 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12619 && code1 == INTEGER_TYPE)
12621 doing_shift = true;
12622 if (TREE_CODE (op0) == INTEGER_CST
12623 && tree_int_cst_sgn (op0) < 0
12624 && !TYPE_OVERFLOW_WRAPS (type0))
12626 /* Don't reject a left shift of a negative value in a context
12627 where a constant expression is needed in C90. */
12628 if (flag_isoc99)
12629 int_const = false;
12630 if (c_inhibit_evaluation_warnings == 0)
12631 warning_at (location, OPT_Wshift_negative_value,
12632 "left shift of negative value");
12634 if (TREE_CODE (op1) == INTEGER_CST)
12636 if (tree_int_cst_sgn (op1) < 0)
12638 int_const = false;
12639 if (c_inhibit_evaluation_warnings == 0)
12640 warning_at (location, OPT_Wshift_count_negative,
12641 "left shift count is negative");
12643 else if (code0 == VECTOR_TYPE)
12645 if (compare_tree_int (op1,
12646 TYPE_PRECISION (TREE_TYPE (type0)))
12647 >= 0)
12649 int_const = false;
12650 if (c_inhibit_evaluation_warnings == 0)
12651 warning_at (location, OPT_Wshift_count_overflow,
12652 "left shift count >= width of vector element");
12655 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12657 int_const = false;
12658 if (c_inhibit_evaluation_warnings == 0)
12659 warning_at (location, OPT_Wshift_count_overflow,
12660 "left shift count >= width of type");
12662 else if (TREE_CODE (op0) == INTEGER_CST
12663 && maybe_warn_shift_overflow (location, op0, op1)
12664 && flag_isoc99)
12665 int_const = false;
12668 /* Use the type of the value to be shifted. */
12669 result_type = type0;
12670 /* Avoid converting op1 to result_type later. */
12671 converted = 1;
12673 break;
12675 case EQ_EXPR:
12676 case NE_EXPR:
12677 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12679 tree intt;
12680 if (!vector_types_compatible_elements_p (type0, type1))
12682 error_at (location, "comparing vectors with different "
12683 "element types");
12684 return error_mark_node;
12687 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12688 TYPE_VECTOR_SUBPARTS (type1)))
12690 error_at (location, "comparing vectors with different "
12691 "number of elements");
12692 return error_mark_node;
12695 /* It's not precisely specified how the usual arithmetic
12696 conversions apply to the vector types. Here, we use
12697 the unsigned type if one of the operands is signed and
12698 the other one is unsigned. */
12699 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12701 if (!TYPE_UNSIGNED (type0))
12702 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12703 else
12704 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12705 warning_at (location, OPT_Wsign_compare, "comparison between "
12706 "types %qT and %qT", type0, type1);
12709 /* Always construct signed integer vector type. */
12710 intt = c_common_type_for_size (GET_MODE_BITSIZE
12711 (SCALAR_TYPE_MODE
12712 (TREE_TYPE (type0))), 0);
12713 if (!intt)
12715 error_at (location, "could not find an integer type "
12716 "of the same size as %qT",
12717 TREE_TYPE (type0));
12718 return error_mark_node;
12720 result_type = build_opaque_vector_type (intt,
12721 TYPE_VECTOR_SUBPARTS (type0));
12722 converted = 1;
12723 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12724 goto return_build_binary_op;
12726 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12727 warning_at (location,
12728 OPT_Wfloat_equal,
12729 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12730 /* Result of comparison is always int,
12731 but don't convert the args to int! */
12732 build_type = integer_type_node;
12733 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12734 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12735 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12736 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12737 short_compare = 1;
12738 else if (code0 == POINTER_TYPE
12739 && (code1 == NULLPTR_TYPE
12740 || null_pointer_constant_p (orig_op1)))
12742 maybe_warn_for_null_address (location, op0, code);
12743 result_type = type0;
12745 else if (code1 == POINTER_TYPE
12746 && (code0 == NULLPTR_TYPE
12747 || null_pointer_constant_p (orig_op0)))
12749 maybe_warn_for_null_address (location, op1, code);
12750 result_type = type1;
12752 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12754 tree tt0 = TREE_TYPE (type0);
12755 tree tt1 = TREE_TYPE (type1);
12756 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12757 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12758 addr_space_t as_common = ADDR_SPACE_GENERIC;
12760 /* Anything compares with void *. void * compares with anything.
12761 Otherwise, the targets must be compatible
12762 and both must be object or both incomplete. */
12763 if (comp_target_types (location, type0, type1))
12764 result_type = common_pointer_type (type0, type1);
12765 else if (!addr_space_superset (as0, as1, &as_common))
12767 error_at (location, "comparison of pointers to "
12768 "disjoint address spaces");
12769 return error_mark_node;
12771 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12773 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12774 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12775 "comparison of %<void *%> with function pointer");
12777 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12779 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12780 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12781 "comparison of %<void *%> with function pointer");
12783 else
12784 /* Avoid warning about the volatile ObjC EH puts on decls. */
12785 if (!objc_ok)
12786 pedwarn (location, 0,
12787 "comparison of distinct pointer types lacks a cast");
12789 if (result_type == NULL_TREE)
12791 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12792 result_type = build_pointer_type
12793 (build_qualified_type (void_type_node, qual));
12796 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12798 result_type = type0;
12799 pedwarn (location, 0, "comparison between pointer and integer");
12801 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12803 result_type = type1;
12804 pedwarn (location, 0, "comparison between pointer and integer");
12806 /* 6.5.9: One of the following shall hold:
12807 -- both operands have type nullptr_t; */
12808 else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE)
12810 result_type = nullptr_type_node;
12811 /* No need to convert the operands to result_type later. */
12812 converted = 1;
12814 /* -- one operand has type nullptr_t and the other is a null pointer
12815 constant. We will have to convert the former to the type of the
12816 latter, because during gimplification we can't have mismatching
12817 comparison operand type. We convert from nullptr_t to the other
12818 type, since only nullptr_t can be converted to nullptr_t. Also,
12819 even a constant 0 is a null pointer constant, so we may have to
12820 create a pointer type from its type. */
12821 else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1))
12822 result_type = (INTEGRAL_TYPE_P (type1)
12823 ? build_pointer_type (type1) : type1);
12824 else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0))
12825 result_type = (INTEGRAL_TYPE_P (type0)
12826 ? build_pointer_type (type0) : type0);
12827 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
12828 || truth_value_p (TREE_CODE (orig_op0)))
12829 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
12830 || truth_value_p (TREE_CODE (orig_op1))))
12831 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12832 break;
12834 case LE_EXPR:
12835 case GE_EXPR:
12836 case LT_EXPR:
12837 case GT_EXPR:
12838 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12840 tree intt;
12841 if (!vector_types_compatible_elements_p (type0, type1))
12843 error_at (location, "comparing vectors with different "
12844 "element types");
12845 return error_mark_node;
12848 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12849 TYPE_VECTOR_SUBPARTS (type1)))
12851 error_at (location, "comparing vectors with different "
12852 "number of elements");
12853 return error_mark_node;
12856 /* It's not precisely specified how the usual arithmetic
12857 conversions apply to the vector types. Here, we use
12858 the unsigned type if one of the operands is signed and
12859 the other one is unsigned. */
12860 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12862 if (!TYPE_UNSIGNED (type0))
12863 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12864 else
12865 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12866 warning_at (location, OPT_Wsign_compare, "comparison between "
12867 "types %qT and %qT", type0, type1);
12870 /* Always construct signed integer vector type. */
12871 intt = c_common_type_for_size (GET_MODE_BITSIZE
12872 (SCALAR_TYPE_MODE
12873 (TREE_TYPE (type0))), 0);
12874 if (!intt)
12876 error_at (location, "could not find an integer type "
12877 "of the same size as %qT",
12878 TREE_TYPE (type0));
12879 return error_mark_node;
12881 result_type = build_opaque_vector_type (intt,
12882 TYPE_VECTOR_SUBPARTS (type0));
12883 converted = 1;
12884 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12885 goto return_build_binary_op;
12887 build_type = integer_type_node;
12888 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12889 || code0 == FIXED_POINT_TYPE)
12890 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12891 || code1 == FIXED_POINT_TYPE))
12892 short_compare = 1;
12893 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12895 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12896 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12897 addr_space_t as_common;
12899 if (comp_target_types (location, type0, type1))
12901 result_type = common_pointer_type (type0, type1);
12902 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12903 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12904 pedwarn_c99 (location, OPT_Wpedantic,
12905 "comparison of complete and incomplete pointers");
12906 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12907 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12908 "ordered comparisons of pointers to functions");
12909 else if (null_pointer_constant_p (orig_op0)
12910 || null_pointer_constant_p (orig_op1))
12911 warning_at (location, OPT_Wextra,
12912 "ordered comparison of pointer with null pointer");
12915 else if (!addr_space_superset (as0, as1, &as_common))
12917 error_at (location, "comparison of pointers to "
12918 "disjoint address spaces");
12919 return error_mark_node;
12921 else
12923 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12924 result_type = build_pointer_type
12925 (build_qualified_type (void_type_node, qual));
12926 pedwarn (location, 0,
12927 "comparison of distinct pointer types lacks a cast");
12930 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12932 result_type = type0;
12933 if (pedantic)
12934 pedwarn (location, OPT_Wpedantic,
12935 "ordered comparison of pointer with integer zero");
12936 else if (extra_warnings)
12937 warning_at (location, OPT_Wextra,
12938 "ordered comparison of pointer with integer zero");
12940 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12942 result_type = type1;
12943 if (pedantic)
12944 pedwarn (location, OPT_Wpedantic,
12945 "ordered comparison of pointer with integer zero");
12946 else if (extra_warnings)
12947 warning_at (location, OPT_Wextra,
12948 "ordered comparison of pointer with integer zero");
12950 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12952 result_type = type0;
12953 pedwarn (location, 0, "comparison between pointer and integer");
12955 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12957 result_type = type1;
12958 pedwarn (location, 0, "comparison between pointer and integer");
12961 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12962 && current_function_decl != NULL_TREE
12963 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12965 op0 = save_expr (op0);
12966 op1 = save_expr (op1);
12968 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12969 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12972 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
12973 || truth_value_p (TREE_CODE (orig_op0)))
12974 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
12975 || truth_value_p (TREE_CODE (orig_op1))))
12976 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12977 break;
12979 case MIN_EXPR:
12980 case MAX_EXPR:
12981 /* Used for OpenMP atomics. */
12982 gcc_assert (flag_openmp);
12983 common = 1;
12984 break;
12986 default:
12987 gcc_unreachable ();
12990 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12991 return error_mark_node;
12993 if (gnu_vector_type_p (type0)
12994 && gnu_vector_type_p (type1)
12995 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12996 || !vector_types_compatible_elements_p (type0, type1)))
12998 gcc_rich_location richloc (location);
12999 maybe_range_label_for_tree_type_mismatch
13000 label_for_op0 (orig_op0, orig_op1),
13001 label_for_op1 (orig_op1, orig_op0);
13002 richloc.maybe_add_expr (orig_op0, &label_for_op0);
13003 richloc.maybe_add_expr (orig_op1, &label_for_op1);
13004 binary_op_error (&richloc, code, type0, type1);
13005 return error_mark_node;
13008 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
13009 || code0 == FIXED_POINT_TYPE
13010 || gnu_vector_type_p (type0))
13012 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
13013 || code1 == FIXED_POINT_TYPE
13014 || gnu_vector_type_p (type1)))
13016 bool first_complex = (code0 == COMPLEX_TYPE);
13017 bool second_complex = (code1 == COMPLEX_TYPE);
13018 int none_complex = (!first_complex && !second_complex);
13020 if (shorten || common || short_compare)
13022 result_type = c_common_type (type0, type1);
13023 do_warn_double_promotion (result_type, type0, type1,
13024 "implicit conversion from %qT to %qT "
13025 "to match other operand of binary "
13026 "expression",
13027 location);
13028 if (result_type == error_mark_node)
13029 return error_mark_node;
13032 if (first_complex != second_complex
13033 && (code == PLUS_EXPR
13034 || code == MINUS_EXPR
13035 || code == MULT_EXPR
13036 || (code == TRUNC_DIV_EXPR && first_complex))
13037 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
13038 && flag_signed_zeros)
13040 /* An operation on mixed real/complex operands must be
13041 handled specially, but the language-independent code can
13042 more easily optimize the plain complex arithmetic if
13043 -fno-signed-zeros. */
13044 tree real_type = TREE_TYPE (result_type);
13045 tree real, imag;
13046 if (type0 != orig_type0 || type1 != orig_type1)
13048 gcc_assert (may_need_excess_precision && common);
13049 semantic_result_type = c_common_type (orig_type0, orig_type1);
13051 if (first_complex)
13053 if (TREE_TYPE (op0) != result_type)
13054 op0 = convert_and_check (location, result_type, op0);
13055 if (TREE_TYPE (op1) != real_type)
13056 op1 = convert_and_check (location, real_type, op1);
13058 else
13060 if (TREE_TYPE (op0) != real_type)
13061 op0 = convert_and_check (location, real_type, op0);
13062 if (TREE_TYPE (op1) != result_type)
13063 op1 = convert_and_check (location, result_type, op1);
13065 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
13066 return error_mark_node;
13067 if (first_complex)
13069 op0 = save_expr (op0);
13070 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
13071 op0, true);
13072 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
13073 op0, true);
13074 switch (code)
13076 case MULT_EXPR:
13077 case TRUNC_DIV_EXPR:
13078 op1 = save_expr (op1);
13079 imag = build2 (resultcode, real_type, imag, op1);
13080 /* Fall through. */
13081 case PLUS_EXPR:
13082 case MINUS_EXPR:
13083 real = build2 (resultcode, real_type, real, op1);
13084 break;
13085 default:
13086 gcc_unreachable();
13089 else
13091 op1 = save_expr (op1);
13092 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
13093 op1, true);
13094 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
13095 op1, true);
13096 switch (code)
13098 case MULT_EXPR:
13099 op0 = save_expr (op0);
13100 imag = build2 (resultcode, real_type, op0, imag);
13101 /* Fall through. */
13102 case PLUS_EXPR:
13103 real = build2 (resultcode, real_type, op0, real);
13104 break;
13105 case MINUS_EXPR:
13106 real = build2 (resultcode, real_type, op0, real);
13107 imag = build1 (NEGATE_EXPR, real_type, imag);
13108 break;
13109 default:
13110 gcc_unreachable();
13113 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
13114 goto return_build_binary_op;
13117 /* For certain operations (which identify themselves by shorten != 0)
13118 if both args were extended from the same smaller type,
13119 do the arithmetic in that type and then extend.
13121 shorten !=0 and !=1 indicates a bitwise operation.
13122 For them, this optimization is safe only if
13123 both args are zero-extended or both are sign-extended.
13124 Otherwise, we might change the result.
13125 Eg, (short)-1 | (unsigned short)-1 is (int)-1
13126 but calculated in (unsigned short) it would be (unsigned short)-1. */
13128 if (shorten && none_complex)
13130 final_type = result_type;
13131 result_type = shorten_binary_op (result_type, op0, op1,
13132 shorten == -1);
13135 /* Shifts can be shortened if shifting right. */
13137 if (short_shift)
13139 int unsigned_arg;
13140 tree arg0 = get_narrower (op0, &unsigned_arg);
13142 final_type = result_type;
13144 if (arg0 == op0 && final_type == TREE_TYPE (op0))
13145 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
13147 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
13148 && tree_int_cst_sgn (op1) > 0
13149 /* We can shorten only if the shift count is less than the
13150 number of bits in the smaller type size. */
13151 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
13152 /* We cannot drop an unsigned shift after sign-extension. */
13153 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
13155 /* Do an unsigned shift if the operand was zero-extended. */
13156 result_type
13157 = c_common_signed_or_unsigned_type (unsigned_arg,
13158 TREE_TYPE (arg0));
13159 /* Convert value-to-be-shifted to that type. */
13160 if (TREE_TYPE (op0) != result_type)
13161 op0 = convert (result_type, op0);
13162 converted = 1;
13166 /* Comparison operations are shortened too but differently.
13167 They identify themselves by setting short_compare = 1. */
13169 if (short_compare)
13171 /* Don't write &op0, etc., because that would prevent op0
13172 from being kept in a register.
13173 Instead, make copies of the our local variables and
13174 pass the copies by reference, then copy them back afterward. */
13175 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
13176 enum tree_code xresultcode = resultcode;
13177 tree val
13178 = shorten_compare (location, &xop0, &xop1, &xresult_type,
13179 &xresultcode);
13181 if (val != NULL_TREE)
13183 ret = val;
13184 goto return_build_binary_op;
13187 op0 = xop0, op1 = xop1;
13188 converted = 1;
13189 resultcode = xresultcode;
13191 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
13193 bool op0_maybe_const = true;
13194 bool op1_maybe_const = true;
13195 tree orig_op0_folded, orig_op1_folded;
13197 if (in_late_binary_op)
13199 orig_op0_folded = orig_op0;
13200 orig_op1_folded = orig_op1;
13202 else
13204 /* Fold for the sake of possible warnings, as in
13205 build_conditional_expr. This requires the
13206 "original" values to be folded, not just op0 and
13207 op1. */
13208 c_inhibit_evaluation_warnings++;
13209 op0 = c_fully_fold (op0, require_constant_value,
13210 &op0_maybe_const);
13211 op1 = c_fully_fold (op1, require_constant_value,
13212 &op1_maybe_const);
13213 c_inhibit_evaluation_warnings--;
13214 orig_op0_folded = c_fully_fold (orig_op0,
13215 require_constant_value,
13216 NULL);
13217 orig_op1_folded = c_fully_fold (orig_op1,
13218 require_constant_value,
13219 NULL);
13222 if (warn_sign_compare)
13223 warn_for_sign_compare (location, orig_op0_folded,
13224 orig_op1_folded, op0, op1,
13225 result_type, resultcode);
13226 if (!in_late_binary_op && !int_operands)
13228 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
13229 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
13230 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
13231 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
13237 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
13238 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
13239 Then the expression will be built.
13240 It will be given type FINAL_TYPE if that is nonzero;
13241 otherwise, it will be given type RESULT_TYPE. */
13243 if (!result_type)
13245 /* Favor showing any expression locations that are available. */
13246 op_location_t oploc (location, UNKNOWN_LOCATION);
13247 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
13248 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
13249 return error_mark_node;
13252 if (build_type == NULL_TREE)
13254 build_type = result_type;
13255 if ((type0 != orig_type0 || type1 != orig_type1)
13256 && !boolean_op)
13258 gcc_assert (may_need_excess_precision && common);
13259 semantic_result_type = c_common_type (orig_type0, orig_type1);
13263 if (!converted)
13265 op0 = ep_convert_and_check (location, result_type, op0,
13266 semantic_result_type);
13267 op1 = ep_convert_and_check (location, result_type, op1,
13268 semantic_result_type);
13270 /* This can happen if one operand has a vector type, and the other
13271 has a different type. */
13272 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
13273 return error_mark_node;
13276 if (sanitize_flags_p ((SANITIZE_SHIFT
13277 | SANITIZE_DIVIDE
13278 | SANITIZE_FLOAT_DIVIDE
13279 | SANITIZE_SI_OVERFLOW))
13280 && current_function_decl != NULL_TREE
13281 && (doing_div_or_mod || doing_shift)
13282 && !require_constant_value)
13284 /* OP0 and/or OP1 might have side-effects. */
13285 op0 = save_expr (op0);
13286 op1 = save_expr (op1);
13287 op0 = c_fully_fold (op0, false, NULL);
13288 op1 = c_fully_fold (op1, false, NULL);
13289 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
13290 | SANITIZE_FLOAT_DIVIDE
13291 | SANITIZE_SI_OVERFLOW))))
13292 instrument_expr = ubsan_instrument_division (location, op0, op1);
13293 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
13294 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
13297 /* Treat expressions in initializers specially as they can't trap. */
13298 if (int_const_or_overflow)
13299 ret = (require_constant_value
13300 ? fold_build2_initializer_loc (location, resultcode, build_type,
13301 op0, op1)
13302 : fold_build2_loc (location, resultcode, build_type, op0, op1));
13303 else
13304 ret = build2 (resultcode, build_type, op0, op1);
13305 if (final_type != NULL_TREE)
13306 ret = convert (final_type, ret);
13308 return_build_binary_op:
13309 gcc_assert (ret != error_mark_node);
13310 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
13311 ret = (int_operands
13312 ? note_integer_operands (ret)
13313 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
13314 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
13315 && !in_late_binary_op)
13316 ret = note_integer_operands (ret);
13317 protected_set_expr_location (ret, location);
13319 if (instrument_expr != NULL)
13320 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
13321 instrument_expr, ret);
13323 if (semantic_result_type)
13324 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
13325 semantic_result_type, ret);
13327 return ret;
13331 /* Convert EXPR to be a truth-value, validating its type for this
13332 purpose. LOCATION is the source location for the expression. */
13334 tree
13335 c_objc_common_truthvalue_conversion (location_t location, tree expr)
13337 bool int_const, int_operands;
13339 switch (TREE_CODE (TREE_TYPE (expr)))
13341 case ARRAY_TYPE:
13342 error_at (location, "used array that cannot be converted to pointer where scalar is required");
13343 return error_mark_node;
13345 case RECORD_TYPE:
13346 error_at (location, "used struct type value where scalar is required");
13347 return error_mark_node;
13349 case UNION_TYPE:
13350 error_at (location, "used union type value where scalar is required");
13351 return error_mark_node;
13353 case VOID_TYPE:
13354 error_at (location, "void value not ignored as it ought to be");
13355 return error_mark_node;
13357 case POINTER_TYPE:
13358 if (reject_gcc_builtin (expr))
13359 return error_mark_node;
13360 break;
13362 case FUNCTION_TYPE:
13363 gcc_unreachable ();
13365 case VECTOR_TYPE:
13366 error_at (location, "used vector type where scalar is required");
13367 return error_mark_node;
13369 default:
13370 break;
13373 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
13374 int_operands = EXPR_INT_CONST_OPERANDS (expr);
13375 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
13377 expr = remove_c_maybe_const_expr (expr);
13378 expr = build2 (NE_EXPR, integer_type_node, expr,
13379 convert (TREE_TYPE (expr), integer_zero_node));
13380 expr = note_integer_operands (expr);
13382 else
13383 /* ??? Should we also give an error for vectors rather than leaving
13384 those to give errors later? */
13385 expr = c_common_truthvalue_conversion (location, expr);
13387 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
13389 if (TREE_OVERFLOW (expr))
13390 return expr;
13391 else
13392 return note_integer_operands (expr);
13394 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
13395 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
13396 return expr;
13400 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
13401 required. */
13403 tree
13404 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
13406 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
13408 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
13409 /* Executing a compound literal inside a function reinitializes
13410 it. */
13411 if (!TREE_STATIC (decl))
13412 *se = true;
13413 return decl;
13415 else
13416 return expr;
13419 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
13420 statement. LOC is the location of the construct. */
13422 tree
13423 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
13424 tree clauses)
13426 body = c_end_compound_stmt (loc, body, true);
13428 tree stmt = make_node (code);
13429 TREE_TYPE (stmt) = void_type_node;
13430 OMP_BODY (stmt) = body;
13431 OMP_CLAUSES (stmt) = clauses;
13432 SET_EXPR_LOCATION (stmt, loc);
13434 return add_stmt (stmt);
13437 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
13438 statement. LOC is the location of the OACC_DATA. */
13440 tree
13441 c_finish_oacc_data (location_t loc, tree clauses, tree block)
13443 tree stmt;
13445 block = c_end_compound_stmt (loc, block, true);
13447 stmt = make_node (OACC_DATA);
13448 TREE_TYPE (stmt) = void_type_node;
13449 OACC_DATA_CLAUSES (stmt) = clauses;
13450 OACC_DATA_BODY (stmt) = block;
13451 SET_EXPR_LOCATION (stmt, loc);
13453 return add_stmt (stmt);
13456 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
13457 statement. LOC is the location of the OACC_HOST_DATA. */
13459 tree
13460 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
13462 tree stmt;
13464 block = c_end_compound_stmt (loc, block, true);
13466 stmt = make_node (OACC_HOST_DATA);
13467 TREE_TYPE (stmt) = void_type_node;
13468 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
13469 OACC_HOST_DATA_BODY (stmt) = block;
13470 SET_EXPR_LOCATION (stmt, loc);
13472 return add_stmt (stmt);
13475 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13477 tree
13478 c_begin_omp_parallel (void)
13480 tree block;
13482 keep_next_level ();
13483 block = c_begin_compound_stmt (true);
13485 return block;
13488 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
13489 statement. LOC is the location of the OMP_PARALLEL. */
13491 tree
13492 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
13494 tree stmt;
13496 block = c_end_compound_stmt (loc, block, true);
13498 stmt = make_node (OMP_PARALLEL);
13499 TREE_TYPE (stmt) = void_type_node;
13500 OMP_PARALLEL_CLAUSES (stmt) = clauses;
13501 OMP_PARALLEL_BODY (stmt) = block;
13502 SET_EXPR_LOCATION (stmt, loc);
13504 return add_stmt (stmt);
13507 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13509 tree
13510 c_begin_omp_task (void)
13512 tree block;
13514 keep_next_level ();
13515 block = c_begin_compound_stmt (true);
13517 return block;
13520 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
13521 statement. LOC is the location of the #pragma. */
13523 tree
13524 c_finish_omp_task (location_t loc, tree clauses, tree block)
13526 tree stmt;
13528 block = c_end_compound_stmt (loc, block, true);
13530 stmt = make_node (OMP_TASK);
13531 TREE_TYPE (stmt) = void_type_node;
13532 OMP_TASK_CLAUSES (stmt) = clauses;
13533 OMP_TASK_BODY (stmt) = block;
13534 SET_EXPR_LOCATION (stmt, loc);
13536 return add_stmt (stmt);
13539 /* Generate GOMP_cancel call for #pragma omp cancel. */
13541 void
13542 c_finish_omp_cancel (location_t loc, tree clauses)
13544 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
13545 int mask = 0;
13546 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13547 mask = 1;
13548 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13549 mask = 2;
13550 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13551 mask = 4;
13552 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13553 mask = 8;
13554 else
13556 error_at (loc, "%<#pragma omp cancel%> must specify one of "
13557 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13558 "clauses");
13559 return;
13561 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
13562 if (ifc != NULL_TREE)
13564 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
13565 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
13566 error_at (OMP_CLAUSE_LOCATION (ifc),
13567 "expected %<cancel%> %<if%> clause modifier");
13568 else
13570 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
13571 if (ifc2 != NULL_TREE)
13573 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
13574 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13575 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13576 error_at (OMP_CLAUSE_LOCATION (ifc2),
13577 "expected %<cancel%> %<if%> clause modifier");
13581 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13582 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13583 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13584 build_zero_cst (type));
13586 else
13587 ifc = boolean_true_node;
13588 tree stmt = build_call_expr_loc (loc, fn, 2,
13589 build_int_cst (integer_type_node, mask),
13590 ifc);
13591 add_stmt (stmt);
13594 /* Generate GOMP_cancellation_point call for
13595 #pragma omp cancellation point. */
13597 void
13598 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13600 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13601 int mask = 0;
13602 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13603 mask = 1;
13604 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13605 mask = 2;
13606 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13607 mask = 4;
13608 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13609 mask = 8;
13610 else
13612 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13613 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13614 "clauses");
13615 return;
13617 tree stmt = build_call_expr_loc (loc, fn, 1,
13618 build_int_cst (integer_type_node, mask));
13619 add_stmt (stmt);
13622 /* Helper function for handle_omp_array_sections. Called recursively
13623 to handle multiple array-section-subscripts. C is the clause,
13624 T current expression (initially OMP_CLAUSE_DECL), which is either
13625 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13626 expression if specified, TREE_VALUE length expression if specified,
13627 TREE_CHAIN is what it has been specified after, or some decl.
13628 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13629 set to true if any of the array-section-subscript could have length
13630 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13631 first array-section-subscript which is known not to have length
13632 of one. Given say:
13633 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13634 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13635 all are or may have length of 1, array-section-subscript [:2] is the
13636 first one known not to have length 1. For array-section-subscript
13637 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13638 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13639 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
13640 case though, as some lengths could be zero. */
13642 static tree
13643 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13644 bool &maybe_zero_len, unsigned int &first_non_one,
13645 enum c_omp_region_type ort)
13647 tree ret, low_bound, length, type;
13648 if (TREE_CODE (t) != TREE_LIST)
13650 if (error_operand_p (t))
13651 return error_mark_node;
13652 ret = t;
13653 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13654 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13655 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13657 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13658 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13659 return error_mark_node;
13661 while (TREE_CODE (t) == INDIRECT_REF)
13663 t = TREE_OPERAND (t, 0);
13664 STRIP_NOPS (t);
13665 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13666 t = TREE_OPERAND (t, 0);
13668 while (TREE_CODE (t) == COMPOUND_EXPR)
13670 t = TREE_OPERAND (t, 1);
13671 STRIP_NOPS (t);
13673 if (TREE_CODE (t) == COMPONENT_REF
13674 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13675 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13676 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13678 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13680 error_at (OMP_CLAUSE_LOCATION (c),
13681 "bit-field %qE in %qs clause",
13682 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13683 return error_mark_node;
13685 while (TREE_CODE (t) == COMPONENT_REF)
13687 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13689 error_at (OMP_CLAUSE_LOCATION (c),
13690 "%qE is a member of a union", t);
13691 return error_mark_node;
13693 t = TREE_OPERAND (t, 0);
13694 while (TREE_CODE (t) == MEM_REF
13695 || TREE_CODE (t) == INDIRECT_REF
13696 || TREE_CODE (t) == ARRAY_REF)
13698 t = TREE_OPERAND (t, 0);
13699 STRIP_NOPS (t);
13700 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13701 t = TREE_OPERAND (t, 0);
13703 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13705 if (maybe_ne (mem_ref_offset (t), 0))
13706 error_at (OMP_CLAUSE_LOCATION (c),
13707 "cannot dereference %qE in %qs clause", t,
13708 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13709 else
13710 t = TREE_OPERAND (t, 0);
13714 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13716 if (DECL_P (t))
13717 error_at (OMP_CLAUSE_LOCATION (c),
13718 "%qD is not a variable in %qs clause", t,
13719 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13720 else
13721 error_at (OMP_CLAUSE_LOCATION (c),
13722 "%qE is not a variable in %qs clause", t,
13723 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13724 return error_mark_node;
13726 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13727 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13728 && TYPE_ATOMIC (TREE_TYPE (t)))
13730 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13731 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13732 return error_mark_node;
13734 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13735 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13736 && VAR_P (t)
13737 && DECL_THREAD_LOCAL_P (t))
13739 error_at (OMP_CLAUSE_LOCATION (c),
13740 "%qD is threadprivate variable in %qs clause", t,
13741 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13742 return error_mark_node;
13744 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13745 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13746 && TYPE_ATOMIC (TREE_TYPE (t))
13747 && POINTER_TYPE_P (TREE_TYPE (t)))
13749 /* If the array section is pointer based and the pointer
13750 itself is _Atomic qualified, we need to atomically load
13751 the pointer. */
13752 c_expr expr;
13753 memset (&expr, 0, sizeof (expr));
13754 expr.value = ret;
13755 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13756 expr, false, false);
13757 ret = expr.value;
13759 return ret;
13762 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13763 maybe_zero_len, first_non_one, ort);
13764 if (ret == error_mark_node || ret == NULL_TREE)
13765 return ret;
13767 type = TREE_TYPE (ret);
13768 low_bound = TREE_PURPOSE (t);
13769 length = TREE_VALUE (t);
13771 if (low_bound == error_mark_node || length == error_mark_node)
13772 return error_mark_node;
13774 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13776 error_at (OMP_CLAUSE_LOCATION (c),
13777 "low bound %qE of array section does not have integral type",
13778 low_bound);
13779 return error_mark_node;
13781 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13783 error_at (OMP_CLAUSE_LOCATION (c),
13784 "length %qE of array section does not have integral type",
13785 length);
13786 return error_mark_node;
13788 if (low_bound
13789 && TREE_CODE (low_bound) == INTEGER_CST
13790 && TYPE_PRECISION (TREE_TYPE (low_bound))
13791 > TYPE_PRECISION (sizetype))
13792 low_bound = fold_convert (sizetype, low_bound);
13793 if (length
13794 && TREE_CODE (length) == INTEGER_CST
13795 && TYPE_PRECISION (TREE_TYPE (length))
13796 > TYPE_PRECISION (sizetype))
13797 length = fold_convert (sizetype, length);
13798 if (low_bound == NULL_TREE)
13799 low_bound = integer_zero_node;
13800 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13801 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13802 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13804 if (length != integer_one_node)
13806 error_at (OMP_CLAUSE_LOCATION (c),
13807 "expected single pointer in %qs clause",
13808 user_omp_clause_code_name (c, ort == C_ORT_ACC));
13809 return error_mark_node;
13812 if (length != NULL_TREE)
13814 if (!integer_nonzerop (length))
13816 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13817 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13818 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13819 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13820 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13822 if (integer_zerop (length))
13824 error_at (OMP_CLAUSE_LOCATION (c),
13825 "zero length array section in %qs clause",
13826 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13827 return error_mark_node;
13830 else
13831 maybe_zero_len = true;
13833 if (first_non_one == types.length ()
13834 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13835 first_non_one++;
13837 if (TREE_CODE (type) == ARRAY_TYPE)
13839 if (length == NULL_TREE
13840 && (TYPE_DOMAIN (type) == NULL_TREE
13841 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13843 error_at (OMP_CLAUSE_LOCATION (c),
13844 "for unknown bound array type length expression must "
13845 "be specified");
13846 return error_mark_node;
13848 if (TREE_CODE (low_bound) == INTEGER_CST
13849 && tree_int_cst_sgn (low_bound) == -1)
13851 error_at (OMP_CLAUSE_LOCATION (c),
13852 "negative low bound in array section in %qs clause",
13853 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13854 return error_mark_node;
13856 if (length != NULL_TREE
13857 && TREE_CODE (length) == INTEGER_CST
13858 && tree_int_cst_sgn (length) == -1)
13860 error_at (OMP_CLAUSE_LOCATION (c),
13861 "negative length in array section in %qs clause",
13862 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13863 return error_mark_node;
13865 if (TYPE_DOMAIN (type)
13866 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13867 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13868 == INTEGER_CST)
13870 tree size
13871 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13872 size = size_binop (PLUS_EXPR, size, size_one_node);
13873 if (TREE_CODE (low_bound) == INTEGER_CST)
13875 if (tree_int_cst_lt (size, low_bound))
13877 error_at (OMP_CLAUSE_LOCATION (c),
13878 "low bound %qE above array section size "
13879 "in %qs clause", low_bound,
13880 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13881 return error_mark_node;
13883 if (tree_int_cst_equal (size, low_bound))
13885 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13886 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13887 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13888 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13889 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13891 error_at (OMP_CLAUSE_LOCATION (c),
13892 "zero length array section in %qs clause",
13893 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13894 return error_mark_node;
13896 maybe_zero_len = true;
13898 else if (length == NULL_TREE
13899 && first_non_one == types.length ()
13900 && tree_int_cst_equal
13901 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13902 low_bound))
13903 first_non_one++;
13905 else if (length == NULL_TREE)
13907 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13908 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13909 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13910 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13911 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13912 maybe_zero_len = true;
13913 if (first_non_one == types.length ())
13914 first_non_one++;
13916 if (length && TREE_CODE (length) == INTEGER_CST)
13918 if (tree_int_cst_lt (size, length))
13920 error_at (OMP_CLAUSE_LOCATION (c),
13921 "length %qE above array section size "
13922 "in %qs clause", length,
13923 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13924 return error_mark_node;
13926 if (TREE_CODE (low_bound) == INTEGER_CST)
13928 tree lbpluslen
13929 = size_binop (PLUS_EXPR,
13930 fold_convert (sizetype, low_bound),
13931 fold_convert (sizetype, length));
13932 if (TREE_CODE (lbpluslen) == INTEGER_CST
13933 && tree_int_cst_lt (size, lbpluslen))
13935 error_at (OMP_CLAUSE_LOCATION (c),
13936 "high bound %qE above array section size "
13937 "in %qs clause", lbpluslen,
13938 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13939 return error_mark_node;
13944 else if (length == NULL_TREE)
13946 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13947 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13948 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13949 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13950 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13951 maybe_zero_len = true;
13952 if (first_non_one == types.length ())
13953 first_non_one++;
13956 /* For [lb:] we will need to evaluate lb more than once. */
13957 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13959 tree lb = save_expr (low_bound);
13960 if (lb != low_bound)
13962 TREE_PURPOSE (t) = lb;
13963 low_bound = lb;
13967 else if (TREE_CODE (type) == POINTER_TYPE)
13969 if (length == NULL_TREE)
13971 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
13972 error_at (OMP_CLAUSE_LOCATION (c),
13973 "for array function parameter length expression "
13974 "must be specified");
13975 else
13976 error_at (OMP_CLAUSE_LOCATION (c),
13977 "for pointer type length expression must be specified");
13978 return error_mark_node;
13980 if (length != NULL_TREE
13981 && TREE_CODE (length) == INTEGER_CST
13982 && tree_int_cst_sgn (length) == -1)
13984 error_at (OMP_CLAUSE_LOCATION (c),
13985 "negative length in array section in %qs clause",
13986 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13987 return error_mark_node;
13989 /* If there is a pointer type anywhere but in the very first
13990 array-section-subscript, the array section could be non-contiguous. */
13991 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13992 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13993 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13995 /* If any prior dimension has a non-one length, then deem this
13996 array section as non-contiguous. */
13997 for (tree d = TREE_CHAIN (t); TREE_CODE (d) == TREE_LIST;
13998 d = TREE_CHAIN (d))
14000 tree d_length = TREE_VALUE (d);
14001 if (d_length == NULL_TREE || !integer_onep (d_length))
14003 error_at (OMP_CLAUSE_LOCATION (c),
14004 "array section is not contiguous in %qs clause",
14005 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14006 return error_mark_node;
14011 else
14013 error_at (OMP_CLAUSE_LOCATION (c),
14014 "%qE does not have pointer or array type", ret);
14015 return error_mark_node;
14017 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
14018 types.safe_push (TREE_TYPE (ret));
14019 /* We will need to evaluate lb more than once. */
14020 tree lb = save_expr (low_bound);
14021 if (lb != low_bound)
14023 TREE_PURPOSE (t) = lb;
14024 low_bound = lb;
14026 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
14027 return ret;
14030 /* Handle array sections for clause C. */
14032 static bool
14033 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
14035 bool maybe_zero_len = false;
14036 unsigned int first_non_one = 0;
14037 auto_vec<tree, 10> types;
14038 tree *tp = &OMP_CLAUSE_DECL (c);
14039 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14040 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
14041 && TREE_CODE (*tp) == TREE_LIST
14042 && TREE_PURPOSE (*tp)
14043 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
14044 tp = &TREE_VALUE (*tp);
14045 tree first = handle_omp_array_sections_1 (c, *tp, types,
14046 maybe_zero_len, first_non_one,
14047 ort);
14048 if (first == error_mark_node)
14049 return true;
14050 if (first == NULL_TREE)
14051 return false;
14052 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14053 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
14055 tree t = *tp;
14056 tree tem = NULL_TREE;
14057 /* Need to evaluate side effects in the length expressions
14058 if any. */
14059 while (TREE_CODE (t) == TREE_LIST)
14061 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
14063 if (tem == NULL_TREE)
14064 tem = TREE_VALUE (t);
14065 else
14066 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
14067 TREE_VALUE (t), tem);
14069 t = TREE_CHAIN (t);
14071 if (tem)
14072 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
14073 first = c_fully_fold (first, false, NULL, true);
14074 *tp = first;
14076 else
14078 unsigned int num = types.length (), i;
14079 tree t, side_effects = NULL_TREE, size = NULL_TREE;
14080 tree condition = NULL_TREE;
14082 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
14083 maybe_zero_len = true;
14085 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
14086 t = TREE_CHAIN (t))
14088 tree low_bound = TREE_PURPOSE (t);
14089 tree length = TREE_VALUE (t);
14091 i--;
14092 if (low_bound
14093 && TREE_CODE (low_bound) == INTEGER_CST
14094 && TYPE_PRECISION (TREE_TYPE (low_bound))
14095 > TYPE_PRECISION (sizetype))
14096 low_bound = fold_convert (sizetype, low_bound);
14097 if (length
14098 && TREE_CODE (length) == INTEGER_CST
14099 && TYPE_PRECISION (TREE_TYPE (length))
14100 > TYPE_PRECISION (sizetype))
14101 length = fold_convert (sizetype, length);
14102 if (low_bound == NULL_TREE)
14103 low_bound = integer_zero_node;
14104 if (!maybe_zero_len && i > first_non_one)
14106 if (integer_nonzerop (low_bound))
14107 goto do_warn_noncontiguous;
14108 if (length != NULL_TREE
14109 && TREE_CODE (length) == INTEGER_CST
14110 && TYPE_DOMAIN (types[i])
14111 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
14112 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
14113 == INTEGER_CST)
14115 tree size;
14116 size = size_binop (PLUS_EXPR,
14117 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
14118 size_one_node);
14119 if (!tree_int_cst_equal (length, size))
14121 do_warn_noncontiguous:
14122 error_at (OMP_CLAUSE_LOCATION (c),
14123 "array section is not contiguous in %qs "
14124 "clause",
14125 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14126 return true;
14129 if (length != NULL_TREE
14130 && TREE_SIDE_EFFECTS (length))
14132 if (side_effects == NULL_TREE)
14133 side_effects = length;
14134 else
14135 side_effects = build2 (COMPOUND_EXPR,
14136 TREE_TYPE (side_effects),
14137 length, side_effects);
14140 else
14142 tree l;
14144 if (i > first_non_one
14145 && ((length && integer_nonzerop (length))
14146 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14147 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14148 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
14149 continue;
14150 if (length)
14151 l = fold_convert (sizetype, length);
14152 else
14154 l = size_binop (PLUS_EXPR,
14155 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
14156 size_one_node);
14157 l = size_binop (MINUS_EXPR, l,
14158 fold_convert (sizetype, low_bound));
14160 if (i > first_non_one)
14162 l = fold_build2 (NE_EXPR, boolean_type_node, l,
14163 size_zero_node);
14164 if (condition == NULL_TREE)
14165 condition = l;
14166 else
14167 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
14168 l, condition);
14170 else if (size == NULL_TREE)
14172 size = size_in_bytes (TREE_TYPE (types[i]));
14173 tree eltype = TREE_TYPE (types[num - 1]);
14174 while (TREE_CODE (eltype) == ARRAY_TYPE)
14175 eltype = TREE_TYPE (eltype);
14176 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14177 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14178 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14180 if (integer_zerop (size)
14181 || integer_zerop (size_in_bytes (eltype)))
14183 error_at (OMP_CLAUSE_LOCATION (c),
14184 "zero length array section in %qs clause",
14185 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14186 return error_mark_node;
14188 size = size_binop (EXACT_DIV_EXPR, size,
14189 size_in_bytes (eltype));
14191 size = size_binop (MULT_EXPR, size, l);
14192 if (condition)
14193 size = fold_build3 (COND_EXPR, sizetype, condition,
14194 size, size_zero_node);
14196 else
14197 size = size_binop (MULT_EXPR, size, l);
14200 if (side_effects)
14201 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
14202 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14203 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14204 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14206 size = size_binop (MINUS_EXPR, size, size_one_node);
14207 size = c_fully_fold (size, false, NULL);
14208 size = save_expr (size);
14209 tree index_type = build_index_type (size);
14210 tree eltype = TREE_TYPE (first);
14211 while (TREE_CODE (eltype) == ARRAY_TYPE)
14212 eltype = TREE_TYPE (eltype);
14213 tree type = build_array_type (eltype, index_type);
14214 tree ptype = build_pointer_type (eltype);
14215 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14216 t = build_fold_addr_expr (t);
14217 tree t2 = build_fold_addr_expr (first);
14218 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14219 ptrdiff_type_node, t2);
14220 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14221 ptrdiff_type_node, t2,
14222 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14223 ptrdiff_type_node, t));
14224 t2 = c_fully_fold (t2, false, NULL);
14225 if (tree_fits_shwi_p (t2))
14226 t = build2 (MEM_REF, type, t,
14227 build_int_cst (ptype, tree_to_shwi (t2)));
14228 else
14230 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
14231 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
14232 TREE_TYPE (t), t, t2);
14233 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
14235 OMP_CLAUSE_DECL (c) = t;
14236 return false;
14238 first = c_fully_fold (first, false, NULL);
14239 OMP_CLAUSE_DECL (c) = first;
14240 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
14241 return false;
14242 if (size)
14243 size = c_fully_fold (size, false, NULL);
14244 OMP_CLAUSE_SIZE (c) = size;
14245 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14246 || (TREE_CODE (t) == COMPONENT_REF
14247 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
14248 return false;
14249 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
14250 switch (OMP_CLAUSE_MAP_KIND (c))
14252 case GOMP_MAP_ALLOC:
14253 case GOMP_MAP_IF_PRESENT:
14254 case GOMP_MAP_TO:
14255 case GOMP_MAP_FROM:
14256 case GOMP_MAP_TOFROM:
14257 case GOMP_MAP_ALWAYS_TO:
14258 case GOMP_MAP_ALWAYS_FROM:
14259 case GOMP_MAP_ALWAYS_TOFROM:
14260 case GOMP_MAP_RELEASE:
14261 case GOMP_MAP_DELETE:
14262 case GOMP_MAP_FORCE_TO:
14263 case GOMP_MAP_FORCE_FROM:
14264 case GOMP_MAP_FORCE_TOFROM:
14265 case GOMP_MAP_FORCE_PRESENT:
14266 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
14267 break;
14268 default:
14269 break;
14271 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
14272 if (TREE_CODE (t) == COMPONENT_REF)
14273 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
14274 else
14275 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
14276 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
14277 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
14278 && !c_mark_addressable (t))
14279 return false;
14280 OMP_CLAUSE_DECL (c2) = t;
14281 t = build_fold_addr_expr (first);
14282 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
14283 tree ptr = OMP_CLAUSE_DECL (c2);
14284 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
14285 ptr = build_fold_addr_expr (ptr);
14286 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14287 ptrdiff_type_node, t,
14288 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14289 ptrdiff_type_node, ptr));
14290 t = c_fully_fold (t, false, NULL);
14291 OMP_CLAUSE_SIZE (c2) = t;
14292 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
14293 OMP_CLAUSE_CHAIN (c) = c2;
14295 return false;
14298 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
14299 an inline call. But, remap
14300 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
14301 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
14303 static tree
14304 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
14305 tree decl, tree placeholder)
14307 copy_body_data id;
14308 hash_map<tree, tree> decl_map;
14310 decl_map.put (omp_decl1, placeholder);
14311 decl_map.put (omp_decl2, decl);
14312 memset (&id, 0, sizeof (id));
14313 id.src_fn = DECL_CONTEXT (omp_decl1);
14314 id.dst_fn = current_function_decl;
14315 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
14316 id.decl_map = &decl_map;
14318 id.copy_decl = copy_decl_no_change;
14319 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
14320 id.transform_new_cfg = true;
14321 id.transform_return_to_modify = false;
14322 id.eh_lp_nr = 0;
14323 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
14324 return stmt;
14327 /* Helper function of c_finish_omp_clauses, called via walk_tree.
14328 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
14330 static tree
14331 c_find_omp_placeholder_r (tree *tp, int *, void *data)
14333 if (*tp == (tree) data)
14334 return *tp;
14335 return NULL_TREE;
14338 /* Similarly, but also walk aggregate fields. */
14340 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
14342 static tree
14343 c_find_omp_var_r (tree *tp, int *, void *data)
14345 if (*tp == ((struct c_find_omp_var_s *) data)->var)
14346 return *tp;
14347 if (RECORD_OR_UNION_TYPE_P (*tp))
14349 tree field;
14350 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
14352 for (field = TYPE_FIELDS (*tp); field;
14353 field = DECL_CHAIN (field))
14354 if (TREE_CODE (field) == FIELD_DECL)
14356 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
14357 c_find_omp_var_r, data, pset);
14358 if (ret)
14359 return ret;
14360 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
14361 if (ret)
14362 return ret;
14363 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
14364 pset);
14365 if (ret)
14366 return ret;
14367 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
14368 if (ret)
14369 return ret;
14372 else if (INTEGRAL_TYPE_P (*tp))
14373 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
14374 ((struct c_find_omp_var_s *) data)->pset);
14375 return NULL_TREE;
14378 /* Finish OpenMP iterators ITER. Return true if they are errorneous
14379 and clauses containing them should be removed. */
14381 static bool
14382 c_omp_finish_iterators (tree iter)
14384 bool ret = false;
14385 for (tree it = iter; it; it = TREE_CHAIN (it))
14387 tree var = TREE_VEC_ELT (it, 0);
14388 tree begin = TREE_VEC_ELT (it, 1);
14389 tree end = TREE_VEC_ELT (it, 2);
14390 tree step = TREE_VEC_ELT (it, 3);
14391 tree orig_step;
14392 tree type = TREE_TYPE (var);
14393 location_t loc = DECL_SOURCE_LOCATION (var);
14394 if (type == error_mark_node)
14396 ret = true;
14397 continue;
14399 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14401 error_at (loc, "iterator %qD has neither integral nor pointer type",
14402 var);
14403 ret = true;
14404 continue;
14406 else if (TYPE_ATOMIC (type))
14408 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
14409 ret = true;
14410 continue;
14412 else if (TYPE_READONLY (type))
14414 error_at (loc, "iterator %qD has const qualified type", var);
14415 ret = true;
14416 continue;
14418 else if (step == error_mark_node
14419 || TREE_TYPE (step) == error_mark_node)
14421 ret = true;
14422 continue;
14424 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
14426 error_at (EXPR_LOC_OR_LOC (step, loc),
14427 "iterator step with non-integral type");
14428 ret = true;
14429 continue;
14431 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
14432 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
14433 orig_step = save_expr (c_fully_fold (step, false, NULL));
14434 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
14435 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
14436 if (POINTER_TYPE_P (type))
14438 begin = save_expr (begin);
14439 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
14440 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
14441 fold_convert (sizetype, step),
14442 fold_convert (sizetype, begin));
14443 step = fold_convert (ssizetype, step);
14445 if (integer_zerop (step))
14447 error_at (loc, "iterator %qD has zero step", var);
14448 ret = true;
14449 continue;
14452 if (begin == error_mark_node
14453 || end == error_mark_node
14454 || step == error_mark_node
14455 || orig_step == error_mark_node)
14457 ret = true;
14458 continue;
14460 hash_set<tree> pset;
14461 tree it2;
14462 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
14464 tree var2 = TREE_VEC_ELT (it2, 0);
14465 tree begin2 = TREE_VEC_ELT (it2, 1);
14466 tree end2 = TREE_VEC_ELT (it2, 2);
14467 tree step2 = TREE_VEC_ELT (it2, 3);
14468 tree type2 = TREE_TYPE (var2);
14469 location_t loc2 = DECL_SOURCE_LOCATION (var2);
14470 struct c_find_omp_var_s data = { var, &pset };
14471 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
14473 error_at (loc2,
14474 "type of iterator %qD refers to outer iterator %qD",
14475 var2, var);
14476 break;
14478 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
14480 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
14481 "begin expression refers to outer iterator %qD", var);
14482 break;
14484 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
14486 error_at (EXPR_LOC_OR_LOC (end2, loc2),
14487 "end expression refers to outer iterator %qD", var);
14488 break;
14490 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
14492 error_at (EXPR_LOC_OR_LOC (step2, loc2),
14493 "step expression refers to outer iterator %qD", var);
14494 break;
14497 if (it2)
14499 ret = true;
14500 continue;
14502 TREE_VEC_ELT (it, 1) = begin;
14503 TREE_VEC_ELT (it, 2) = end;
14504 TREE_VEC_ELT (it, 3) = step;
14505 TREE_VEC_ELT (it, 4) = orig_step;
14507 return ret;
14510 /* Ensure that pointers are used in OpenACC attach and detach clauses.
14511 Return true if an error has been detected. */
14513 static bool
14514 c_oacc_check_attachments (tree c)
14516 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14517 return false;
14519 /* OpenACC attach / detach clauses must be pointers. */
14520 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14521 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14523 tree t = OMP_CLAUSE_DECL (c);
14525 while (TREE_CODE (t) == TREE_LIST)
14526 t = TREE_CHAIN (t);
14528 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14530 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
14531 user_omp_clause_code_name (c, true));
14532 return true;
14536 return false;
14539 /* For all elements of CLAUSES, validate them against their constraints.
14540 Remove any elements from the list that are invalid. */
14542 tree
14543 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
14545 bitmap_head generic_head, firstprivate_head, lastprivate_head;
14546 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
14547 bitmap_head oacc_reduction_head, is_on_device_head;
14548 tree c, t, type, *pc;
14549 tree simdlen = NULL_TREE, safelen = NULL_TREE;
14550 bool branch_seen = false;
14551 bool copyprivate_seen = false;
14552 bool mergeable_seen = false;
14553 tree *detach_seen = NULL;
14554 bool linear_variable_step_check = false;
14555 tree *nowait_clause = NULL;
14556 tree ordered_clause = NULL_TREE;
14557 tree schedule_clause = NULL_TREE;
14558 bool oacc_async = false;
14559 bool indir_component_ref_p = false;
14560 tree last_iterators = NULL_TREE;
14561 bool last_iterators_remove = false;
14562 tree *nogroup_seen = NULL;
14563 tree *order_clause = NULL;
14564 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
14565 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
14566 int reduction_seen = 0;
14567 bool allocate_seen = false;
14568 bool implicit_moved = false;
14569 bool target_in_reduction_seen = false;
14571 bitmap_obstack_initialize (NULL);
14572 bitmap_initialize (&generic_head, &bitmap_default_obstack);
14573 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
14574 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
14575 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
14576 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
14577 bitmap_initialize (&map_head, &bitmap_default_obstack);
14578 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
14579 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
14580 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
14581 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
14582 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
14583 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
14585 if (ort & C_ORT_ACC)
14586 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14587 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
14589 oacc_async = true;
14590 break;
14593 tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
14595 for (pc = &clauses, c = clauses; c ; c = *pc)
14597 bool remove = false;
14598 bool need_complete = false;
14599 bool need_implicitly_determined = false;
14601 /* We've reached the end of a list of expanded nodes. Reset the group
14602 start pointer. */
14603 if (c == grp_sentinel)
14604 grp_start_p = NULL;
14606 switch (OMP_CLAUSE_CODE (c))
14608 case OMP_CLAUSE_SHARED:
14609 need_implicitly_determined = true;
14610 goto check_dup_generic;
14612 case OMP_CLAUSE_PRIVATE:
14613 need_complete = true;
14614 need_implicitly_determined = true;
14615 goto check_dup_generic;
14617 case OMP_CLAUSE_REDUCTION:
14618 if (reduction_seen == 0)
14619 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14620 else if (reduction_seen != -2
14621 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14622 ? -1 : 1))
14624 error_at (OMP_CLAUSE_LOCATION (c),
14625 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14626 "on the same construct");
14627 reduction_seen = -2;
14629 /* FALLTHRU */
14630 case OMP_CLAUSE_IN_REDUCTION:
14631 case OMP_CLAUSE_TASK_REDUCTION:
14632 need_implicitly_determined = true;
14633 t = OMP_CLAUSE_DECL (c);
14634 if (TREE_CODE (t) == TREE_LIST)
14636 if (handle_omp_array_sections (c, ort))
14638 remove = true;
14639 break;
14642 t = OMP_CLAUSE_DECL (c);
14643 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14644 && OMP_CLAUSE_REDUCTION_INSCAN (c))
14646 error_at (OMP_CLAUSE_LOCATION (c),
14647 "%<inscan%> %<reduction%> clause with array "
14648 "section");
14649 remove = true;
14650 break;
14653 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14654 if (t == error_mark_node)
14656 remove = true;
14657 break;
14659 if (oacc_async)
14660 c_mark_addressable (t);
14661 type = TREE_TYPE (t);
14662 if (TREE_CODE (t) == MEM_REF)
14663 type = TREE_TYPE (type);
14664 if (TREE_CODE (type) == ARRAY_TYPE)
14666 tree oatype = type;
14667 gcc_assert (TREE_CODE (t) != MEM_REF);
14668 while (TREE_CODE (type) == ARRAY_TYPE)
14669 type = TREE_TYPE (type);
14670 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14672 error_at (OMP_CLAUSE_LOCATION (c),
14673 "%qD in %<reduction%> clause is a zero size array",
14675 remove = true;
14676 break;
14678 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14679 TYPE_SIZE_UNIT (type));
14680 if (integer_zerop (size))
14682 error_at (OMP_CLAUSE_LOCATION (c),
14683 "%qD in %<reduction%> clause is a zero size array",
14685 remove = true;
14686 break;
14688 size = size_binop (MINUS_EXPR, size, size_one_node);
14689 size = save_expr (size);
14690 tree index_type = build_index_type (size);
14691 tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14692 index_type);
14693 atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14694 tree ptype = build_pointer_type (type);
14695 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14696 t = build_fold_addr_expr (t);
14697 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14698 OMP_CLAUSE_DECL (c) = t;
14700 if (TYPE_ATOMIC (type))
14702 error_at (OMP_CLAUSE_LOCATION (c),
14703 "%<_Atomic%> %qE in %<reduction%> clause", t);
14704 remove = true;
14705 break;
14707 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14708 || OMP_CLAUSE_REDUCTION_TASK (c))
14710 /* Disallow zero sized or potentially zero sized task
14711 reductions. */
14712 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14714 error_at (OMP_CLAUSE_LOCATION (c),
14715 "zero sized type %qT in %qs clause", type,
14716 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14717 remove = true;
14718 break;
14720 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14722 error_at (OMP_CLAUSE_LOCATION (c),
14723 "variable sized type %qT in %qs clause", type,
14724 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14725 remove = true;
14726 break;
14729 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14730 && (FLOAT_TYPE_P (type)
14731 || TREE_CODE (type) == COMPLEX_TYPE))
14733 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14734 const char *r_name = NULL;
14736 switch (r_code)
14738 case PLUS_EXPR:
14739 case MULT_EXPR:
14740 case MINUS_EXPR:
14741 case TRUTH_ANDIF_EXPR:
14742 case TRUTH_ORIF_EXPR:
14743 break;
14744 case MIN_EXPR:
14745 if (TREE_CODE (type) == COMPLEX_TYPE)
14746 r_name = "min";
14747 break;
14748 case MAX_EXPR:
14749 if (TREE_CODE (type) == COMPLEX_TYPE)
14750 r_name = "max";
14751 break;
14752 case BIT_AND_EXPR:
14753 r_name = "&";
14754 break;
14755 case BIT_XOR_EXPR:
14756 r_name = "^";
14757 break;
14758 case BIT_IOR_EXPR:
14759 r_name = "|";
14760 break;
14761 default:
14762 gcc_unreachable ();
14764 if (r_name)
14766 error_at (OMP_CLAUSE_LOCATION (c),
14767 "%qE has invalid type for %<reduction(%s)%>",
14768 t, r_name);
14769 remove = true;
14770 break;
14773 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14775 error_at (OMP_CLAUSE_LOCATION (c),
14776 "user defined reduction not found for %qE", t);
14777 remove = true;
14778 break;
14780 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14782 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14783 type = TYPE_MAIN_VARIANT (type);
14784 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14785 VAR_DECL, NULL_TREE, type);
14786 tree decl_placeholder = NULL_TREE;
14787 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14788 DECL_ARTIFICIAL (placeholder) = 1;
14789 DECL_IGNORED_P (placeholder) = 1;
14790 if (TREE_CODE (t) == MEM_REF)
14792 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14793 VAR_DECL, NULL_TREE, type);
14794 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14795 DECL_ARTIFICIAL (decl_placeholder) = 1;
14796 DECL_IGNORED_P (decl_placeholder) = 1;
14798 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14799 c_mark_addressable (placeholder);
14800 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14801 c_mark_addressable (decl_placeholder ? decl_placeholder
14802 : OMP_CLAUSE_DECL (c));
14803 OMP_CLAUSE_REDUCTION_MERGE (c)
14804 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14805 TREE_VEC_ELT (list, 0),
14806 TREE_VEC_ELT (list, 1),
14807 decl_placeholder ? decl_placeholder
14808 : OMP_CLAUSE_DECL (c), placeholder);
14809 OMP_CLAUSE_REDUCTION_MERGE (c)
14810 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14811 void_type_node, NULL_TREE,
14812 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14813 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14814 if (TREE_VEC_LENGTH (list) == 6)
14816 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14817 c_mark_addressable (decl_placeholder ? decl_placeholder
14818 : OMP_CLAUSE_DECL (c));
14819 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14820 c_mark_addressable (placeholder);
14821 tree init = TREE_VEC_ELT (list, 5);
14822 if (init == error_mark_node)
14823 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14824 OMP_CLAUSE_REDUCTION_INIT (c)
14825 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14826 TREE_VEC_ELT (list, 3),
14827 decl_placeholder ? decl_placeholder
14828 : OMP_CLAUSE_DECL (c), placeholder);
14829 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14831 tree v = decl_placeholder ? decl_placeholder : t;
14832 OMP_CLAUSE_REDUCTION_INIT (c)
14833 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14834 OMP_CLAUSE_REDUCTION_INIT (c));
14836 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14837 c_find_omp_placeholder_r,
14838 placeholder, NULL))
14839 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14841 else
14843 tree init;
14844 tree v = decl_placeholder ? decl_placeholder : t;
14845 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14846 init = build_constructor (TREE_TYPE (v), NULL);
14847 else
14848 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14849 OMP_CLAUSE_REDUCTION_INIT (c)
14850 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14852 OMP_CLAUSE_REDUCTION_INIT (c)
14853 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14854 void_type_node, NULL_TREE,
14855 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14856 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14858 if (TREE_CODE (t) == MEM_REF)
14860 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14861 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14862 != INTEGER_CST)
14864 sorry ("variable length element type in array "
14865 "%<reduction%> clause");
14866 remove = true;
14867 break;
14869 t = TREE_OPERAND (t, 0);
14870 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14871 t = TREE_OPERAND (t, 0);
14872 if (TREE_CODE (t) == ADDR_EXPR)
14873 t = TREE_OPERAND (t, 0);
14875 goto check_dup_generic_t;
14877 case OMP_CLAUSE_COPYPRIVATE:
14878 copyprivate_seen = true;
14879 if (nowait_clause)
14881 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14882 "%<nowait%> clause must not be used together "
14883 "with %<copyprivate%>");
14884 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14885 nowait_clause = NULL;
14887 goto check_dup_generic;
14889 case OMP_CLAUSE_COPYIN:
14890 t = OMP_CLAUSE_DECL (c);
14891 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14893 error_at (OMP_CLAUSE_LOCATION (c),
14894 "%qE must be %<threadprivate%> for %<copyin%>", t);
14895 remove = true;
14896 break;
14898 goto check_dup_generic;
14900 case OMP_CLAUSE_LINEAR:
14901 if (ort != C_ORT_OMP_DECLARE_SIMD)
14902 need_implicitly_determined = true;
14903 t = OMP_CLAUSE_DECL (c);
14904 if (ort != C_ORT_OMP_DECLARE_SIMD
14905 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
14906 && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
14908 error_at (OMP_CLAUSE_LOCATION (c),
14909 "modifier should not be specified in %<linear%> "
14910 "clause on %<simd%> or %<for%> constructs when not "
14911 "using OpenMP 5.2 modifiers");
14912 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14914 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14915 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14917 error_at (OMP_CLAUSE_LOCATION (c),
14918 "linear clause applied to non-integral non-pointer "
14919 "variable with type %qT", TREE_TYPE (t));
14920 remove = true;
14921 break;
14923 if (TYPE_ATOMIC (TREE_TYPE (t)))
14925 error_at (OMP_CLAUSE_LOCATION (c),
14926 "%<_Atomic%> %qD in %<linear%> clause", t);
14927 remove = true;
14928 break;
14930 if (ort == C_ORT_OMP_DECLARE_SIMD)
14932 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14933 if (TREE_CODE (s) == PARM_DECL)
14935 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14936 /* map_head bitmap is used as uniform_head if
14937 declare_simd. */
14938 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14939 linear_variable_step_check = true;
14940 goto check_dup_generic;
14942 if (TREE_CODE (s) != INTEGER_CST)
14944 error_at (OMP_CLAUSE_LOCATION (c),
14945 "%<linear%> clause step %qE is neither constant "
14946 "nor a parameter", s);
14947 remove = true;
14948 break;
14951 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14953 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14954 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14955 OMP_CLAUSE_DECL (c), s);
14956 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14957 sizetype, fold_convert (sizetype, s),
14958 fold_convert
14959 (sizetype, OMP_CLAUSE_DECL (c)));
14960 if (s == error_mark_node)
14961 s = size_one_node;
14962 OMP_CLAUSE_LINEAR_STEP (c) = s;
14964 else
14965 OMP_CLAUSE_LINEAR_STEP (c)
14966 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14967 goto check_dup_generic;
14969 check_dup_generic:
14970 t = OMP_CLAUSE_DECL (c);
14971 check_dup_generic_t:
14972 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14974 error_at (OMP_CLAUSE_LOCATION (c),
14975 "%qE is not a variable in clause %qs", t,
14976 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14977 remove = true;
14979 else if ((ort == C_ORT_ACC
14980 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14981 || (ort == C_ORT_OMP
14982 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14983 || (OMP_CLAUSE_CODE (c)
14984 == OMP_CLAUSE_USE_DEVICE_ADDR)))
14985 || (ort == C_ORT_OMP_TARGET
14986 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
14988 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14989 && (bitmap_bit_p (&generic_head, DECL_UID (t))
14990 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
14992 error_at (OMP_CLAUSE_LOCATION (c),
14993 "%qD appears more than once in data-sharing "
14994 "clauses", t);
14995 remove = true;
14996 break;
14998 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
14999 target_in_reduction_seen = true;
15000 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15002 error_at (OMP_CLAUSE_LOCATION (c),
15003 ort == C_ORT_ACC
15004 ? "%qD appears more than once in reduction clauses"
15005 : "%qD appears more than once in data clauses",
15007 remove = true;
15009 else
15010 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
15012 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15013 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15014 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
15015 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15017 error_at (OMP_CLAUSE_LOCATION (c),
15018 "%qE appears more than once in data clauses", t);
15019 remove = true;
15021 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15022 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
15023 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15024 && bitmap_bit_p (&map_head, DECL_UID (t)))
15026 if (ort == C_ORT_ACC)
15027 error_at (OMP_CLAUSE_LOCATION (c),
15028 "%qD appears more than once in data clauses", t);
15029 else
15030 error_at (OMP_CLAUSE_LOCATION (c),
15031 "%qD appears both in data and map clauses", t);
15032 remove = true;
15034 else
15035 bitmap_set_bit (&generic_head, DECL_UID (t));
15036 break;
15038 case OMP_CLAUSE_FIRSTPRIVATE:
15039 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
15041 move_implicit:
15042 implicit_moved = true;
15043 /* Move firstprivate and map clauses with
15044 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
15045 clauses chain. */
15046 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
15047 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
15048 while (*pc1)
15049 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
15050 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
15052 *pc3 = *pc1;
15053 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
15054 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
15056 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
15057 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
15059 *pc2 = *pc1;
15060 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
15061 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
15063 else
15064 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
15065 *pc3 = NULL;
15066 *pc2 = cl2;
15067 *pc1 = cl1;
15068 continue;
15070 t = OMP_CLAUSE_DECL (c);
15071 need_complete = true;
15072 need_implicitly_determined = true;
15073 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15075 error_at (OMP_CLAUSE_LOCATION (c),
15076 "%qE is not a variable in clause %<firstprivate%>", t);
15077 remove = true;
15079 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
15080 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
15081 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15082 remove = true;
15083 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15084 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15085 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15087 error_at (OMP_CLAUSE_LOCATION (c),
15088 "%qE appears more than once in data clauses", t);
15089 remove = true;
15091 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
15093 if (ort == C_ORT_ACC)
15094 error_at (OMP_CLAUSE_LOCATION (c),
15095 "%qD appears more than once in data clauses", t);
15096 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
15097 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
15098 /* Silently drop the clause. */;
15099 else
15100 error_at (OMP_CLAUSE_LOCATION (c),
15101 "%qD appears both in data and map clauses", t);
15102 remove = true;
15104 else
15105 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
15106 break;
15108 case OMP_CLAUSE_LASTPRIVATE:
15109 t = OMP_CLAUSE_DECL (c);
15110 need_complete = true;
15111 need_implicitly_determined = true;
15112 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15114 error_at (OMP_CLAUSE_LOCATION (c),
15115 "%qE is not a variable in clause %<lastprivate%>", t);
15116 remove = true;
15118 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15119 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
15121 error_at (OMP_CLAUSE_LOCATION (c),
15122 "%qE appears more than once in data clauses", t);
15123 remove = true;
15125 else
15126 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
15127 break;
15129 case OMP_CLAUSE_ALIGNED:
15130 t = OMP_CLAUSE_DECL (c);
15131 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15133 error_at (OMP_CLAUSE_LOCATION (c),
15134 "%qE is not a variable in %<aligned%> clause", t);
15135 remove = true;
15137 else if (!POINTER_TYPE_P (TREE_TYPE (t))
15138 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15140 error_at (OMP_CLAUSE_LOCATION (c),
15141 "%qE in %<aligned%> clause is neither a pointer nor "
15142 "an array", t);
15143 remove = true;
15145 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15147 error_at (OMP_CLAUSE_LOCATION (c),
15148 "%<_Atomic%> %qD in %<aligned%> clause", t);
15149 remove = true;
15150 break;
15152 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
15154 error_at (OMP_CLAUSE_LOCATION (c),
15155 "%qE appears more than once in %<aligned%> clauses",
15157 remove = true;
15159 else
15160 bitmap_set_bit (&aligned_head, DECL_UID (t));
15161 break;
15163 case OMP_CLAUSE_NONTEMPORAL:
15164 t = OMP_CLAUSE_DECL (c);
15165 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15167 error_at (OMP_CLAUSE_LOCATION (c),
15168 "%qE is not a variable in %<nontemporal%> clause", t);
15169 remove = true;
15171 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15173 error_at (OMP_CLAUSE_LOCATION (c),
15174 "%qE appears more than once in %<nontemporal%> "
15175 "clauses", t);
15176 remove = true;
15178 else
15179 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
15180 break;
15182 case OMP_CLAUSE_ALLOCATE:
15183 t = OMP_CLAUSE_DECL (c);
15184 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15186 error_at (OMP_CLAUSE_LOCATION (c),
15187 "%qE is not a variable in %<allocate%> clause", t);
15188 remove = true;
15190 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
15192 warning_at (OMP_CLAUSE_LOCATION (c), 0,
15193 "%qE appears more than once in %<allocate%> clauses",
15195 remove = true;
15197 else
15199 bitmap_set_bit (&aligned_head, DECL_UID (t));
15200 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
15201 allocate_seen = true;
15203 break;
15205 case OMP_CLAUSE_DOACROSS:
15206 t = OMP_CLAUSE_DECL (c);
15207 if (t == NULL_TREE)
15208 break;
15209 if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
15211 gcc_assert (TREE_CODE (t) == TREE_LIST);
15212 for (; t; t = TREE_CHAIN (t))
15214 tree decl = TREE_VALUE (t);
15215 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
15217 tree offset = TREE_PURPOSE (t);
15218 bool neg = wi::neg_p (wi::to_wide (offset));
15219 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
15220 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
15221 neg ? MINUS_EXPR : PLUS_EXPR,
15222 decl, offset);
15223 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
15224 sizetype,
15225 fold_convert (sizetype, t2),
15226 fold_convert (sizetype, decl));
15227 if (t2 == error_mark_node)
15229 remove = true;
15230 break;
15232 TREE_PURPOSE (t) = t2;
15235 break;
15237 gcc_unreachable ();
15238 case OMP_CLAUSE_DEPEND:
15239 case OMP_CLAUSE_AFFINITY:
15240 t = OMP_CLAUSE_DECL (c);
15241 if (TREE_CODE (t) == TREE_LIST
15242 && TREE_PURPOSE (t)
15243 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
15245 if (TREE_PURPOSE (t) != last_iterators)
15246 last_iterators_remove
15247 = c_omp_finish_iterators (TREE_PURPOSE (t));
15248 last_iterators = TREE_PURPOSE (t);
15249 t = TREE_VALUE (t);
15250 if (last_iterators_remove)
15251 t = error_mark_node;
15253 else
15254 last_iterators = NULL_TREE;
15255 if (TREE_CODE (t) == TREE_LIST)
15257 if (handle_omp_array_sections (c, ort))
15258 remove = true;
15259 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15260 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
15262 error_at (OMP_CLAUSE_LOCATION (c),
15263 "%<depend%> clause with %<depobj%> dependence "
15264 "type on array section");
15265 remove = true;
15267 break;
15269 if (t == error_mark_node)
15270 remove = true;
15271 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15272 && t == ridpointers[RID_OMP_ALL_MEMORY])
15274 if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
15275 && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
15277 error_at (OMP_CLAUSE_LOCATION (c),
15278 "%<omp_all_memory%> used with %<depend%> kind "
15279 "other than %<out%> or %<inout%>");
15280 remove = true;
15283 else if (!lvalue_p (t))
15285 error_at (OMP_CLAUSE_LOCATION (c),
15286 "%qE is not lvalue expression nor array section in "
15287 "%qs clause", t,
15288 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15289 remove = true;
15291 else if (TREE_CODE (t) == COMPONENT_REF
15292 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
15294 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15295 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
15296 error_at (OMP_CLAUSE_LOCATION (c),
15297 "bit-field %qE in %qs clause", t,
15298 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15299 remove = true;
15301 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15302 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
15304 if (!c_omp_depend_t_p (TREE_TYPE (t)))
15306 error_at (OMP_CLAUSE_LOCATION (c),
15307 "%qE does not have %<omp_depend_t%> type in "
15308 "%<depend%> clause with %<depobj%> dependence "
15309 "type", t);
15310 remove = true;
15313 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15314 && c_omp_depend_t_p (TREE_TYPE (t)))
15316 error_at (OMP_CLAUSE_LOCATION (c),
15317 "%qE should not have %<omp_depend_t%> type in "
15318 "%<depend%> clause with dependence type other than "
15319 "%<depobj%>", t);
15320 remove = true;
15322 if (!remove)
15324 if (t == ridpointers[RID_OMP_ALL_MEMORY])
15325 t = null_pointer_node;
15326 else
15328 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
15329 ADDR_EXPR, t, false);
15330 if (addr == error_mark_node)
15332 remove = true;
15333 break;
15335 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
15336 RO_UNARY_STAR);
15337 if (t == error_mark_node)
15339 remove = true;
15340 break;
15343 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
15344 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
15345 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
15346 == TREE_VEC))
15347 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
15348 else
15349 OMP_CLAUSE_DECL (c) = t;
15351 break;
15353 case OMP_CLAUSE_MAP:
15354 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
15355 goto move_implicit;
15356 /* FALLTHRU */
15357 case OMP_CLAUSE_TO:
15358 case OMP_CLAUSE_FROM:
15359 case OMP_CLAUSE__CACHE_:
15360 t = OMP_CLAUSE_DECL (c);
15361 if (TREE_CODE (t) == TREE_LIST)
15363 grp_start_p = pc;
15364 grp_sentinel = OMP_CLAUSE_CHAIN (c);
15366 if (handle_omp_array_sections (c, ort))
15367 remove = true;
15368 else
15370 t = OMP_CLAUSE_DECL (c);
15371 if (!omp_mappable_type (TREE_TYPE (t)))
15373 error_at (OMP_CLAUSE_LOCATION (c),
15374 "array section does not have mappable type "
15375 "in %qs clause",
15376 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15377 remove = true;
15379 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15381 error_at (OMP_CLAUSE_LOCATION (c),
15382 "%<_Atomic%> %qE in %qs clause", t,
15383 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15384 remove = true;
15386 while (TREE_CODE (t) == ARRAY_REF)
15387 t = TREE_OPERAND (t, 0);
15388 if (TREE_CODE (t) == COMPONENT_REF
15389 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
15393 t = TREE_OPERAND (t, 0);
15394 if (TREE_CODE (t) == MEM_REF
15395 || TREE_CODE (t) == INDIRECT_REF)
15397 t = TREE_OPERAND (t, 0);
15398 STRIP_NOPS (t);
15399 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15400 t = TREE_OPERAND (t, 0);
15403 while (TREE_CODE (t) == COMPONENT_REF
15404 || TREE_CODE (t) == ARRAY_REF);
15406 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15407 && OMP_CLAUSE_MAP_IMPLICIT (c)
15408 && (bitmap_bit_p (&map_head, DECL_UID (t))
15409 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15410 || bitmap_bit_p (&map_firstprivate_head,
15411 DECL_UID (t))))
15413 remove = true;
15414 break;
15416 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
15417 break;
15418 if (bitmap_bit_p (&map_head, DECL_UID (t)))
15420 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15421 error_at (OMP_CLAUSE_LOCATION (c),
15422 "%qD appears more than once in motion "
15423 "clauses", t);
15424 else if (ort == C_ORT_ACC)
15425 error_at (OMP_CLAUSE_LOCATION (c),
15426 "%qD appears more than once in data "
15427 "clauses", t);
15428 else
15429 error_at (OMP_CLAUSE_LOCATION (c),
15430 "%qD appears more than once in map "
15431 "clauses", t);
15432 remove = true;
15434 else
15436 bitmap_set_bit (&map_head, DECL_UID (t));
15437 bitmap_set_bit (&map_field_head, DECL_UID (t));
15441 if (c_oacc_check_attachments (c))
15442 remove = true;
15443 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15444 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15445 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15446 /* In this case, we have a single array element which is a
15447 pointer, and we already set OMP_CLAUSE_SIZE in
15448 handle_omp_array_sections above. For attach/detach clauses,
15449 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
15450 here. */
15451 OMP_CLAUSE_SIZE (c) = size_zero_node;
15452 break;
15454 if (t == error_mark_node)
15456 remove = true;
15457 break;
15459 /* OpenACC attach / detach clauses must be pointers. */
15460 if (c_oacc_check_attachments (c))
15462 remove = true;
15463 break;
15465 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15466 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15467 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15468 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
15469 bias) to zero here, so it is not set erroneously to the pointer
15470 size later on in gimplify.cc. */
15471 OMP_CLAUSE_SIZE (c) = size_zero_node;
15472 while (TREE_CODE (t) == INDIRECT_REF
15473 || TREE_CODE (t) == ARRAY_REF)
15475 t = TREE_OPERAND (t, 0);
15476 STRIP_NOPS (t);
15477 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15478 t = TREE_OPERAND (t, 0);
15480 while (TREE_CODE (t) == COMPOUND_EXPR)
15482 t = TREE_OPERAND (t, 1);
15483 STRIP_NOPS (t);
15485 indir_component_ref_p = false;
15486 if (TREE_CODE (t) == COMPONENT_REF
15487 && (TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
15488 || TREE_CODE (TREE_OPERAND (t, 0)) == INDIRECT_REF
15489 || TREE_CODE (TREE_OPERAND (t, 0)) == ARRAY_REF))
15491 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
15492 indir_component_ref_p = true;
15493 STRIP_NOPS (t);
15494 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15495 t = TREE_OPERAND (t, 0);
15498 if (TREE_CODE (t) == COMPONENT_REF
15499 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
15501 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
15503 error_at (OMP_CLAUSE_LOCATION (c),
15504 "bit-field %qE in %qs clause",
15505 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15506 remove = true;
15508 else if (!omp_mappable_type (TREE_TYPE (t)))
15510 error_at (OMP_CLAUSE_LOCATION (c),
15511 "%qE does not have a mappable type in %qs clause",
15512 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15513 remove = true;
15515 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15517 error_at (OMP_CLAUSE_LOCATION (c),
15518 "%<_Atomic%> %qE in %qs clause", t,
15519 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15520 remove = true;
15522 while (TREE_CODE (t) == COMPONENT_REF)
15524 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
15525 == UNION_TYPE)
15527 error_at (OMP_CLAUSE_LOCATION (c),
15528 "%qE is a member of a union", t);
15529 remove = true;
15530 break;
15532 t = TREE_OPERAND (t, 0);
15533 if (TREE_CODE (t) == MEM_REF)
15535 if (maybe_ne (mem_ref_offset (t), 0))
15536 error_at (OMP_CLAUSE_LOCATION (c),
15537 "cannot dereference %qE in %qs clause", t,
15538 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15539 else
15540 t = TREE_OPERAND (t, 0);
15542 while (TREE_CODE (t) == MEM_REF
15543 || TREE_CODE (t) == INDIRECT_REF
15544 || TREE_CODE (t) == ARRAY_REF)
15546 t = TREE_OPERAND (t, 0);
15547 STRIP_NOPS (t);
15548 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15549 t = TREE_OPERAND (t, 0);
15552 if (remove)
15553 break;
15554 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15556 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
15557 || (ort != C_ORT_ACC
15558 && bitmap_bit_p (&map_head, DECL_UID (t))))
15559 break;
15562 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15564 error_at (OMP_CLAUSE_LOCATION (c),
15565 "%qE is not a variable in %qs clause", t,
15566 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15567 remove = true;
15569 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15571 error_at (OMP_CLAUSE_LOCATION (c),
15572 "%qD is threadprivate variable in %qs clause", t,
15573 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15574 remove = true;
15576 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15577 || (OMP_CLAUSE_MAP_KIND (c)
15578 != GOMP_MAP_FIRSTPRIVATE_POINTER))
15579 && !indir_component_ref_p
15580 && !c_mark_addressable (t))
15581 remove = true;
15582 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15583 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
15584 || (OMP_CLAUSE_MAP_KIND (c)
15585 == GOMP_MAP_FIRSTPRIVATE_POINTER)
15586 || (OMP_CLAUSE_MAP_KIND (c)
15587 == GOMP_MAP_FORCE_DEVICEPTR)))
15588 && t == OMP_CLAUSE_DECL (c)
15589 && !omp_mappable_type (TREE_TYPE (t)))
15591 error_at (OMP_CLAUSE_LOCATION (c),
15592 "%qD does not have a mappable type in %qs clause", t,
15593 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15594 remove = true;
15596 else if (TREE_TYPE (t) == error_mark_node)
15597 remove = true;
15598 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15600 error_at (OMP_CLAUSE_LOCATION (c),
15601 "%<_Atomic%> %qE in %qs clause", t,
15602 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15603 remove = true;
15605 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15606 && OMP_CLAUSE_MAP_IMPLICIT (c)
15607 && (bitmap_bit_p (&map_head, DECL_UID (t))
15608 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15609 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))))
15610 remove = true;
15611 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15612 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
15614 if (bitmap_bit_p (&generic_head, DECL_UID (t))
15615 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15616 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15618 error_at (OMP_CLAUSE_LOCATION (c),
15619 "%qD appears more than once in data clauses", t);
15620 remove = true;
15622 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15623 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15625 if (ort == C_ORT_ACC)
15626 error_at (OMP_CLAUSE_LOCATION (c),
15627 "%qD appears more than once in data clauses", t);
15628 else
15629 error_at (OMP_CLAUSE_LOCATION (c),
15630 "%qD appears both in data and map clauses", t);
15631 remove = true;
15633 else
15634 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
15636 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15637 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15639 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15640 error_at (OMP_CLAUSE_LOCATION (c),
15641 "%qD appears more than once in motion clauses", t);
15642 else if (ort == C_ORT_ACC)
15643 error_at (OMP_CLAUSE_LOCATION (c),
15644 "%qD appears more than once in data clauses", t);
15645 else
15646 error_at (OMP_CLAUSE_LOCATION (c),
15647 "%qD appears more than once in map clauses", t);
15648 remove = true;
15650 else if (ort == C_ORT_ACC
15651 && bitmap_bit_p (&generic_head, DECL_UID (t)))
15653 error_at (OMP_CLAUSE_LOCATION (c),
15654 "%qD appears more than once in data clauses", t);
15655 remove = true;
15657 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15658 || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
15660 if (ort == C_ORT_ACC)
15661 error_at (OMP_CLAUSE_LOCATION (c),
15662 "%qD appears more than once in data clauses", t);
15663 else
15664 error_at (OMP_CLAUSE_LOCATION (c),
15665 "%qD appears both in data and map clauses", t);
15666 remove = true;
15668 else
15670 bitmap_set_bit (&map_head, DECL_UID (t));
15671 if (t != OMP_CLAUSE_DECL (c)
15672 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
15673 bitmap_set_bit (&map_field_head, DECL_UID (t));
15675 break;
15677 case OMP_CLAUSE_ENTER:
15678 case OMP_CLAUSE_LINK:
15679 t = OMP_CLAUSE_DECL (c);
15680 const char *cname;
15681 cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
15682 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
15683 && OMP_CLAUSE_ENTER_TO (c))
15684 cname = "to";
15685 if (TREE_CODE (t) == FUNCTION_DECL
15686 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15688 else if (!VAR_P (t))
15690 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15691 error_at (OMP_CLAUSE_LOCATION (c),
15692 "%qE is neither a variable nor a function name in "
15693 "clause %qs", t, cname);
15694 else
15695 error_at (OMP_CLAUSE_LOCATION (c),
15696 "%qE is not a variable in clause %qs", t, cname);
15697 remove = true;
15699 else if (DECL_THREAD_LOCAL_P (t))
15701 error_at (OMP_CLAUSE_LOCATION (c),
15702 "%qD is threadprivate variable in %qs clause", t,
15703 cname);
15704 remove = true;
15706 else if (!omp_mappable_type (TREE_TYPE (t)))
15708 error_at (OMP_CLAUSE_LOCATION (c),
15709 "%qD does not have a mappable type in %qs clause", t,
15710 cname);
15711 remove = true;
15713 if (remove)
15714 break;
15715 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15717 error_at (OMP_CLAUSE_LOCATION (c),
15718 "%qE appears more than once on the same "
15719 "%<declare target%> directive", t);
15720 remove = true;
15722 else
15723 bitmap_set_bit (&generic_head, DECL_UID (t));
15724 break;
15726 case OMP_CLAUSE_UNIFORM:
15727 t = OMP_CLAUSE_DECL (c);
15728 if (TREE_CODE (t) != PARM_DECL)
15730 if (DECL_P (t))
15731 error_at (OMP_CLAUSE_LOCATION (c),
15732 "%qD is not an argument in %<uniform%> clause", t);
15733 else
15734 error_at (OMP_CLAUSE_LOCATION (c),
15735 "%qE is not an argument in %<uniform%> clause", t);
15736 remove = true;
15737 break;
15739 /* map_head bitmap is used as uniform_head if declare_simd. */
15740 bitmap_set_bit (&map_head, DECL_UID (t));
15741 goto check_dup_generic;
15743 case OMP_CLAUSE_IS_DEVICE_PTR:
15744 case OMP_CLAUSE_USE_DEVICE_PTR:
15745 t = OMP_CLAUSE_DECL (c);
15746 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15747 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15748 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15750 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15751 && ort != C_ORT_ACC)
15753 error_at (OMP_CLAUSE_LOCATION (c),
15754 "%qs variable is not a pointer",
15755 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15756 remove = true;
15758 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15760 error_at (OMP_CLAUSE_LOCATION (c),
15761 "%qs variable is neither a pointer nor an array",
15762 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15763 remove = true;
15766 goto check_dup_generic;
15768 case OMP_CLAUSE_HAS_DEVICE_ADDR:
15769 t = OMP_CLAUSE_DECL (c);
15770 if (TREE_CODE (t) == TREE_LIST)
15772 if (handle_omp_array_sections (c, ort))
15773 remove = true;
15774 else
15776 t = OMP_CLAUSE_DECL (c);
15777 while (TREE_CODE (t) == ARRAY_REF)
15778 t = TREE_OPERAND (t, 0);
15781 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15782 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15783 c_mark_addressable (t);
15784 goto check_dup_generic_t;
15786 case OMP_CLAUSE_USE_DEVICE_ADDR:
15787 t = OMP_CLAUSE_DECL (c);
15788 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15789 c_mark_addressable (t);
15790 goto check_dup_generic;
15792 case OMP_CLAUSE_NOWAIT:
15793 if (copyprivate_seen)
15795 error_at (OMP_CLAUSE_LOCATION (c),
15796 "%<nowait%> clause must not be used together "
15797 "with %<copyprivate%>");
15798 remove = true;
15799 break;
15801 nowait_clause = pc;
15802 pc = &OMP_CLAUSE_CHAIN (c);
15803 continue;
15805 case OMP_CLAUSE_ORDER:
15806 if (ordered_clause)
15808 error_at (OMP_CLAUSE_LOCATION (c),
15809 "%<order%> clause must not be used together "
15810 "with %<ordered%>");
15811 remove = true;
15812 break;
15814 else if (order_clause)
15816 /* Silently remove duplicates. */
15817 remove = true;
15818 break;
15820 order_clause = pc;
15821 pc = &OMP_CLAUSE_CHAIN (c);
15822 continue;
15824 case OMP_CLAUSE_DETACH:
15825 t = OMP_CLAUSE_DECL (c);
15826 if (detach_seen)
15828 error_at (OMP_CLAUSE_LOCATION (c),
15829 "too many %qs clauses on a task construct",
15830 "detach");
15831 remove = true;
15832 break;
15834 detach_seen = pc;
15835 pc = &OMP_CLAUSE_CHAIN (c);
15836 c_mark_addressable (t);
15837 continue;
15839 case OMP_CLAUSE_IF:
15840 case OMP_CLAUSE_NUM_THREADS:
15841 case OMP_CLAUSE_NUM_TEAMS:
15842 case OMP_CLAUSE_THREAD_LIMIT:
15843 case OMP_CLAUSE_DEFAULT:
15844 case OMP_CLAUSE_UNTIED:
15845 case OMP_CLAUSE_COLLAPSE:
15846 case OMP_CLAUSE_FINAL:
15847 case OMP_CLAUSE_DEVICE:
15848 case OMP_CLAUSE_DIST_SCHEDULE:
15849 case OMP_CLAUSE_PARALLEL:
15850 case OMP_CLAUSE_FOR:
15851 case OMP_CLAUSE_SECTIONS:
15852 case OMP_CLAUSE_TASKGROUP:
15853 case OMP_CLAUSE_PROC_BIND:
15854 case OMP_CLAUSE_DEVICE_TYPE:
15855 case OMP_CLAUSE_PRIORITY:
15856 case OMP_CLAUSE_GRAINSIZE:
15857 case OMP_CLAUSE_NUM_TASKS:
15858 case OMP_CLAUSE_THREADS:
15859 case OMP_CLAUSE_SIMD:
15860 case OMP_CLAUSE_HINT:
15861 case OMP_CLAUSE_FILTER:
15862 case OMP_CLAUSE_DEFAULTMAP:
15863 case OMP_CLAUSE_BIND:
15864 case OMP_CLAUSE_NUM_GANGS:
15865 case OMP_CLAUSE_NUM_WORKERS:
15866 case OMP_CLAUSE_VECTOR_LENGTH:
15867 case OMP_CLAUSE_ASYNC:
15868 case OMP_CLAUSE_WAIT:
15869 case OMP_CLAUSE_AUTO:
15870 case OMP_CLAUSE_INDEPENDENT:
15871 case OMP_CLAUSE_SEQ:
15872 case OMP_CLAUSE_GANG:
15873 case OMP_CLAUSE_WORKER:
15874 case OMP_CLAUSE_VECTOR:
15875 case OMP_CLAUSE_TILE:
15876 case OMP_CLAUSE_IF_PRESENT:
15877 case OMP_CLAUSE_FINALIZE:
15878 case OMP_CLAUSE_NOHOST:
15879 pc = &OMP_CLAUSE_CHAIN (c);
15880 continue;
15882 case OMP_CLAUSE_MERGEABLE:
15883 mergeable_seen = true;
15884 pc = &OMP_CLAUSE_CHAIN (c);
15885 continue;
15887 case OMP_CLAUSE_NOGROUP:
15888 nogroup_seen = pc;
15889 pc = &OMP_CLAUSE_CHAIN (c);
15890 continue;
15892 case OMP_CLAUSE_SCHEDULE:
15893 schedule_clause = c;
15894 pc = &OMP_CLAUSE_CHAIN (c);
15895 continue;
15897 case OMP_CLAUSE_ORDERED:
15898 ordered_clause = c;
15899 if (order_clause)
15901 error_at (OMP_CLAUSE_LOCATION (*order_clause),
15902 "%<order%> clause must not be used together "
15903 "with %<ordered%>");
15904 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
15905 order_clause = NULL;
15907 pc = &OMP_CLAUSE_CHAIN (c);
15908 continue;
15910 case OMP_CLAUSE_SAFELEN:
15911 safelen = c;
15912 pc = &OMP_CLAUSE_CHAIN (c);
15913 continue;
15914 case OMP_CLAUSE_SIMDLEN:
15915 simdlen = c;
15916 pc = &OMP_CLAUSE_CHAIN (c);
15917 continue;
15919 case OMP_CLAUSE_INBRANCH:
15920 case OMP_CLAUSE_NOTINBRANCH:
15921 if (branch_seen)
15923 error_at (OMP_CLAUSE_LOCATION (c),
15924 "%<inbranch%> clause is incompatible with "
15925 "%<notinbranch%>");
15926 remove = true;
15927 break;
15929 branch_seen = true;
15930 pc = &OMP_CLAUSE_CHAIN (c);
15931 continue;
15933 case OMP_CLAUSE_INCLUSIVE:
15934 case OMP_CLAUSE_EXCLUSIVE:
15935 need_complete = true;
15936 need_implicitly_determined = true;
15937 t = OMP_CLAUSE_DECL (c);
15938 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15940 error_at (OMP_CLAUSE_LOCATION (c),
15941 "%qE is not a variable in clause %qs", t,
15942 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15943 remove = true;
15945 break;
15947 default:
15948 gcc_unreachable ();
15951 if (!remove)
15953 t = OMP_CLAUSE_DECL (c);
15955 if (need_complete)
15957 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15958 if (t == error_mark_node)
15959 remove = true;
15962 if (need_implicitly_determined)
15964 const char *share_name = NULL;
15966 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15967 share_name = "threadprivate";
15968 else switch (c_omp_predetermined_sharing (t))
15970 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15971 break;
15972 case OMP_CLAUSE_DEFAULT_SHARED:
15973 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15974 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15975 && c_omp_predefined_variable (t))
15976 /* The __func__ variable and similar function-local
15977 predefined variables may be listed in a shared or
15978 firstprivate clause. */
15979 break;
15980 share_name = "shared";
15981 break;
15982 case OMP_CLAUSE_DEFAULT_PRIVATE:
15983 share_name = "private";
15984 break;
15985 default:
15986 gcc_unreachable ();
15988 if (share_name)
15990 error_at (OMP_CLAUSE_LOCATION (c),
15991 "%qE is predetermined %qs for %qs",
15992 t, share_name,
15993 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15994 remove = true;
15996 else if (TREE_READONLY (t)
15997 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15998 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
16000 error_at (OMP_CLAUSE_LOCATION (c),
16001 "%<const%> qualified %qE may appear only in "
16002 "%<shared%> or %<firstprivate%> clauses", t);
16003 remove = true;
16008 if (remove)
16010 if (grp_start_p)
16012 /* If we found a clause to remove, we want to remove the whole
16013 expanded group, otherwise gimplify
16014 (omp_resolve_clause_dependencies) can get confused. */
16015 *grp_start_p = grp_sentinel;
16016 pc = grp_start_p;
16017 grp_start_p = NULL;
16019 else
16020 *pc = OMP_CLAUSE_CHAIN (c);
16022 else
16023 pc = &OMP_CLAUSE_CHAIN (c);
16026 if (simdlen
16027 && safelen
16028 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
16029 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
16031 error_at (OMP_CLAUSE_LOCATION (simdlen),
16032 "%<simdlen%> clause value is bigger than "
16033 "%<safelen%> clause value");
16034 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
16035 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
16038 if (ordered_clause
16039 && schedule_clause
16040 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16041 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
16043 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
16044 "%<nonmonotonic%> schedule modifier specified together "
16045 "with %<ordered%> clause");
16046 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16047 = (enum omp_clause_schedule_kind)
16048 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16049 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
16052 if (reduction_seen < 0 && ordered_clause)
16054 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
16055 "%qs clause specified together with %<inscan%> "
16056 "%<reduction%> clause", "ordered");
16057 reduction_seen = -2;
16060 if (reduction_seen < 0 && schedule_clause)
16062 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
16063 "%qs clause specified together with %<inscan%> "
16064 "%<reduction%> clause", "schedule");
16065 reduction_seen = -2;
16068 if (linear_variable_step_check
16069 || reduction_seen == -2
16070 || allocate_seen
16071 || target_in_reduction_seen)
16072 for (pc = &clauses, c = clauses; c ; c = *pc)
16074 bool remove = false;
16075 if (allocate_seen)
16076 switch (OMP_CLAUSE_CODE (c))
16078 case OMP_CLAUSE_REDUCTION:
16079 case OMP_CLAUSE_IN_REDUCTION:
16080 case OMP_CLAUSE_TASK_REDUCTION:
16081 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
16083 t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
16084 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
16085 t = TREE_OPERAND (t, 0);
16086 if (TREE_CODE (t) == ADDR_EXPR
16087 || TREE_CODE (t) == INDIRECT_REF)
16088 t = TREE_OPERAND (t, 0);
16089 if (DECL_P (t))
16090 bitmap_clear_bit (&aligned_head, DECL_UID (t));
16091 break;
16093 /* FALLTHRU */
16094 case OMP_CLAUSE_PRIVATE:
16095 case OMP_CLAUSE_FIRSTPRIVATE:
16096 case OMP_CLAUSE_LASTPRIVATE:
16097 case OMP_CLAUSE_LINEAR:
16098 if (DECL_P (OMP_CLAUSE_DECL (c)))
16099 bitmap_clear_bit (&aligned_head,
16100 DECL_UID (OMP_CLAUSE_DECL (c)));
16101 break;
16102 default:
16103 break;
16105 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
16106 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
16107 && !bitmap_bit_p (&map_head,
16108 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
16110 error_at (OMP_CLAUSE_LOCATION (c),
16111 "%<linear%> clause step is a parameter %qD not "
16112 "specified in %<uniform%> clause",
16113 OMP_CLAUSE_LINEAR_STEP (c));
16114 remove = true;
16116 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16117 && reduction_seen == -2)
16118 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
16119 if (target_in_reduction_seen
16120 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
16122 tree t = OMP_CLAUSE_DECL (c);
16123 while (handled_component_p (t)
16124 || TREE_CODE (t) == INDIRECT_REF
16125 || TREE_CODE (t) == ADDR_EXPR
16126 || TREE_CODE (t) == MEM_REF
16127 || TREE_CODE (t) == NON_LVALUE_EXPR)
16128 t = TREE_OPERAND (t, 0);
16129 if (DECL_P (t)
16130 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
16131 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
16134 if (remove)
16135 *pc = OMP_CLAUSE_CHAIN (c);
16136 else
16137 pc = &OMP_CLAUSE_CHAIN (c);
16140 if (allocate_seen)
16141 for (pc = &clauses, c = clauses; c ; c = *pc)
16143 bool remove = false;
16144 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
16145 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
16146 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
16148 error_at (OMP_CLAUSE_LOCATION (c),
16149 "%qD specified in %<allocate%> clause but not in "
16150 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
16151 remove = true;
16153 if (remove)
16154 *pc = OMP_CLAUSE_CHAIN (c);
16155 else
16156 pc = &OMP_CLAUSE_CHAIN (c);
16159 if (nogroup_seen && reduction_seen)
16161 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
16162 "%<nogroup%> clause must not be used together with "
16163 "%<reduction%> clause");
16164 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
16167 if (detach_seen)
16169 if (mergeable_seen)
16171 error_at (OMP_CLAUSE_LOCATION (*detach_seen),
16172 "%<detach%> clause must not be used together with "
16173 "%<mergeable%> clause");
16174 *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
16176 else
16178 tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
16180 for (pc = &clauses, c = clauses; c ; c = *pc)
16182 bool remove = false;
16183 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
16184 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
16185 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
16186 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
16187 && OMP_CLAUSE_DECL (c) == detach_decl)
16189 error_at (OMP_CLAUSE_LOCATION (c),
16190 "the event handle of a %<detach%> clause "
16191 "should not be in a data-sharing clause");
16192 remove = true;
16194 if (remove)
16195 *pc = OMP_CLAUSE_CHAIN (c);
16196 else
16197 pc = &OMP_CLAUSE_CHAIN (c);
16202 bitmap_obstack_release (NULL);
16203 return clauses;
16206 /* Return code to initialize DST with a copy constructor from SRC.
16207 C doesn't have copy constructors nor assignment operators, only for
16208 _Atomic vars we need to perform __atomic_load from src into a temporary
16209 followed by __atomic_store of the temporary to dst. */
16211 tree
16212 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
16214 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
16215 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
16217 location_t loc = OMP_CLAUSE_LOCATION (clause);
16218 tree type = TREE_TYPE (dst);
16219 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
16220 tree tmp = create_tmp_var (nonatomic_type);
16221 tree tmp_addr = build_fold_addr_expr (tmp);
16222 TREE_ADDRESSABLE (tmp) = 1;
16223 suppress_warning (tmp);
16224 tree src_addr = build_fold_addr_expr (src);
16225 tree dst_addr = build_fold_addr_expr (dst);
16226 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
16227 vec<tree, va_gc> *params;
16228 /* Expansion of a generic atomic load may require an addition
16229 element, so allocate enough to prevent a resize. */
16230 vec_alloc (params, 4);
16232 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
16233 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
16234 params->quick_push (src_addr);
16235 params->quick_push (tmp_addr);
16236 params->quick_push (seq_cst);
16237 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
16239 vec_alloc (params, 4);
16241 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
16242 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
16243 params->quick_push (dst_addr);
16244 params->quick_push (tmp_addr);
16245 params->quick_push (seq_cst);
16246 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
16247 return build2 (COMPOUND_EXPR, void_type_node, load, store);
16250 /* Create a transaction node. */
16252 tree
16253 c_finish_transaction (location_t loc, tree block, int flags)
16255 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
16256 if (flags & TM_STMT_ATTR_OUTER)
16257 TRANSACTION_EXPR_OUTER (stmt) = 1;
16258 if (flags & TM_STMT_ATTR_RELAXED)
16259 TRANSACTION_EXPR_RELAXED (stmt) = 1;
16260 return add_stmt (stmt);
16263 /* Make a variant type in the proper way for C/C++, propagating qualifiers
16264 down to the element type of an array. If ORIG_QUAL_TYPE is not
16265 NULL, then it should be used as the qualified type
16266 ORIG_QUAL_INDIRECT levels down in array type derivation (to
16267 preserve information about the typedef name from which an array
16268 type was derived). */
16270 tree
16271 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
16272 size_t orig_qual_indirect)
16274 if (type == error_mark_node)
16275 return type;
16277 if (TREE_CODE (type) == ARRAY_TYPE)
16279 tree t;
16280 tree element_type = c_build_qualified_type (TREE_TYPE (type),
16281 type_quals, orig_qual_type,
16282 orig_qual_indirect - 1);
16284 /* See if we already have an identically qualified type. */
16285 if (orig_qual_type && orig_qual_indirect == 0)
16286 t = orig_qual_type;
16287 else
16288 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
16290 if (TYPE_QUALS (strip_array_types (t)) == type_quals
16291 && TYPE_NAME (t) == TYPE_NAME (type)
16292 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
16293 && attribute_list_equal (TYPE_ATTRIBUTES (t),
16294 TYPE_ATTRIBUTES (type)))
16295 break;
16297 if (!t)
16299 tree domain = TYPE_DOMAIN (type);
16301 t = build_variant_type_copy (type);
16302 TREE_TYPE (t) = element_type;
16304 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
16305 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
16306 SET_TYPE_STRUCTURAL_EQUALITY (t);
16307 else if (TYPE_CANONICAL (element_type) != element_type
16308 || (domain && TYPE_CANONICAL (domain) != domain))
16310 tree unqualified_canon
16311 = build_array_type (TYPE_CANONICAL (element_type),
16312 domain? TYPE_CANONICAL (domain)
16313 : NULL_TREE);
16314 if (TYPE_REVERSE_STORAGE_ORDER (type))
16316 unqualified_canon
16317 = build_distinct_type_copy (unqualified_canon);
16318 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
16320 TYPE_CANONICAL (t)
16321 = c_build_qualified_type (unqualified_canon, type_quals);
16323 else
16324 TYPE_CANONICAL (t) = t;
16326 return t;
16329 /* A restrict-qualified pointer type must be a pointer to object or
16330 incomplete type. Note that the use of POINTER_TYPE_P also allows
16331 REFERENCE_TYPEs, which is appropriate for C++. */
16332 if ((type_quals & TYPE_QUAL_RESTRICT)
16333 && (!POINTER_TYPE_P (type)
16334 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
16336 error ("invalid use of %<restrict%>");
16337 type_quals &= ~TYPE_QUAL_RESTRICT;
16340 tree var_type = (orig_qual_type && orig_qual_indirect == 0
16341 ? orig_qual_type
16342 : build_qualified_type (type, type_quals));
16343 /* A variant type does not inherit the list of incomplete vars from the
16344 type main variant. */
16345 if ((RECORD_OR_UNION_TYPE_P (var_type)
16346 || TREE_CODE (var_type) == ENUMERAL_TYPE)
16347 && TYPE_MAIN_VARIANT (var_type) != var_type)
16348 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
16349 return var_type;
16352 /* Build a VA_ARG_EXPR for the C parser. */
16354 tree
16355 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
16357 if (error_operand_p (type))
16358 return error_mark_node;
16359 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
16360 order because it takes the address of the expression. */
16361 else if (handled_component_p (expr)
16362 && reverse_storage_order_for_component_p (expr))
16364 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
16365 return error_mark_node;
16367 else if (!COMPLETE_TYPE_P (type))
16369 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
16370 "type %qT", type);
16371 return error_mark_node;
16373 else if (TREE_CODE (type) == FUNCTION_TYPE)
16375 error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
16376 type);
16377 return error_mark_node;
16379 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
16380 warning_at (loc2, OPT_Wc___compat,
16381 "C++ requires promoted type, not enum type, in %<va_arg%>");
16382 return build_va_arg (loc2, expr, type);
16385 /* Return truthvalue of whether T1 is the same tree structure as T2.
16386 Return 1 if they are the same. Return false if they are different. */
16388 bool
16389 c_tree_equal (tree t1, tree t2)
16391 enum tree_code code1, code2;
16393 if (t1 == t2)
16394 return true;
16395 if (!t1 || !t2)
16396 return false;
16398 for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
16399 code1 = TREE_CODE (t1))
16400 t1 = TREE_OPERAND (t1, 0);
16401 for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
16402 code2 = TREE_CODE (t2))
16403 t2 = TREE_OPERAND (t2, 0);
16405 /* They might have become equal now. */
16406 if (t1 == t2)
16407 return true;
16409 if (code1 != code2)
16410 return false;
16412 if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
16413 return false;
16415 switch (code1)
16417 case INTEGER_CST:
16418 return wi::to_wide (t1) == wi::to_wide (t2);
16420 case REAL_CST:
16421 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
16423 case STRING_CST:
16424 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
16425 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
16426 TREE_STRING_LENGTH (t1));
16428 case FIXED_CST:
16429 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
16430 TREE_FIXED_CST (t2));
16432 case COMPLEX_CST:
16433 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
16434 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
16436 case VECTOR_CST:
16437 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
16439 case CONSTRUCTOR:
16440 /* We need to do this when determining whether or not two
16441 non-type pointer to member function template arguments
16442 are the same. */
16443 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
16444 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
16445 return false;
16447 tree field, value;
16448 unsigned int i;
16449 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
16451 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
16452 if (!c_tree_equal (field, elt2->index)
16453 || !c_tree_equal (value, elt2->value))
16454 return false;
16457 return true;
16459 case TREE_LIST:
16460 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
16461 return false;
16462 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
16463 return false;
16464 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
16466 case SAVE_EXPR:
16467 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16469 case CALL_EXPR:
16471 tree arg1, arg2;
16472 call_expr_arg_iterator iter1, iter2;
16473 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
16474 return false;
16475 for (arg1 = first_call_expr_arg (t1, &iter1),
16476 arg2 = first_call_expr_arg (t2, &iter2);
16477 arg1 && arg2;
16478 arg1 = next_call_expr_arg (&iter1),
16479 arg2 = next_call_expr_arg (&iter2))
16480 if (!c_tree_equal (arg1, arg2))
16481 return false;
16482 if (arg1 || arg2)
16483 return false;
16484 return true;
16487 case TARGET_EXPR:
16489 tree o1 = TREE_OPERAND (t1, 0);
16490 tree o2 = TREE_OPERAND (t2, 0);
16492 /* Special case: if either target is an unallocated VAR_DECL,
16493 it means that it's going to be unified with whatever the
16494 TARGET_EXPR is really supposed to initialize, so treat it
16495 as being equivalent to anything. */
16496 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
16497 && !DECL_RTL_SET_P (o1))
16498 /*Nop*/;
16499 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
16500 && !DECL_RTL_SET_P (o2))
16501 /*Nop*/;
16502 else if (!c_tree_equal (o1, o2))
16503 return false;
16505 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
16508 case COMPONENT_REF:
16509 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
16510 return false;
16511 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16513 case PARM_DECL:
16514 case VAR_DECL:
16515 case CONST_DECL:
16516 case FIELD_DECL:
16517 case FUNCTION_DECL:
16518 case IDENTIFIER_NODE:
16519 case SSA_NAME:
16520 return false;
16522 case TREE_VEC:
16524 unsigned ix;
16525 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
16526 return false;
16527 for (ix = TREE_VEC_LENGTH (t1); ix--;)
16528 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
16529 TREE_VEC_ELT (t2, ix)))
16530 return false;
16531 return true;
16534 CASE_CONVERT:
16535 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
16536 return false;
16537 break;
16539 default:
16540 break;
16543 switch (TREE_CODE_CLASS (code1))
16545 case tcc_unary:
16546 case tcc_binary:
16547 case tcc_comparison:
16548 case tcc_expression:
16549 case tcc_vl_exp:
16550 case tcc_reference:
16551 case tcc_statement:
16553 int i, n = TREE_OPERAND_LENGTH (t1);
16555 switch (code1)
16557 case PREINCREMENT_EXPR:
16558 case PREDECREMENT_EXPR:
16559 case POSTINCREMENT_EXPR:
16560 case POSTDECREMENT_EXPR:
16561 n = 1;
16562 break;
16563 case ARRAY_REF:
16564 n = 2;
16565 break;
16566 default:
16567 break;
16570 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
16571 && n != TREE_OPERAND_LENGTH (t2))
16572 return false;
16574 for (i = 0; i < n; ++i)
16575 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
16576 return false;
16578 return true;
16581 case tcc_type:
16582 return comptypes (t1, t2);
16583 default:
16584 gcc_unreachable ();
16588 /* Returns true when the function declaration FNDECL is implicit,
16589 introduced as a result of a call to an otherwise undeclared
16590 function, and false otherwise. */
16592 bool
16593 c_decl_implicit (const_tree fndecl)
16595 return C_DECL_IMPLICIT (fndecl);