hppa: Always enable PIE on 64-bit target
[official-gcc.git] / gcc / c / c-typeck.cc
blob66c6abc9f076a77a8bd4f4f1e9c6d6546492d5ca
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2024 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 /* True when parsing OpenMP map clause. */
80 bool c_omp_array_section_p;
82 /* The argument of last parsed sizeof expression, only to be tested
83 if expr.original_code == SIZEOF_EXPR. */
84 tree c_last_sizeof_arg;
85 location_t c_last_sizeof_loc;
87 /* Nonzero if we might need to print a "missing braces around
88 initializer" message within this initializer. */
89 static int found_missing_braces;
91 static bool require_constant_value;
92 static bool require_constant_elements;
93 static bool require_constexpr_value;
95 static tree qualify_type (tree, tree);
96 struct comptypes_data;
97 static bool tagged_types_tu_compatible_p (const_tree, const_tree,
98 struct comptypes_data *);
99 static bool comp_target_types (location_t, tree, tree);
100 static bool function_types_compatible_p (const_tree, const_tree,
101 struct comptypes_data *);
102 static bool type_lists_compatible_p (const_tree, const_tree,
103 struct comptypes_data *);
104 static tree lookup_field (tree, tree);
105 static int convert_arguments (location_t, vec<location_t>, tree,
106 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
107 tree);
108 static tree pointer_diff (location_t, tree, tree, tree *);
109 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
110 enum impl_conv, bool, tree, tree, int,
111 int = 0);
112 static tree valid_compound_expr_initializer (tree, tree);
113 static void push_string (const char *);
114 static void push_member_name (tree);
115 static int spelling_length (void);
116 static char *print_spelling (char *);
117 static void warning_init (location_t, int, const char *);
118 static tree digest_init (location_t, tree, tree, tree, bool, bool, bool, bool,
119 bool, bool);
120 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
121 bool, struct obstack *);
122 static void output_pending_init_elements (int, struct obstack *);
123 static bool set_designator (location_t, bool, struct obstack *);
124 static void push_range_stack (tree, struct obstack *);
125 static void add_pending_init (location_t, tree, tree, tree, bool,
126 struct obstack *);
127 static void set_nonincremental_init (struct obstack *);
128 static void set_nonincremental_init_from_string (tree, struct obstack *);
129 static tree find_init_member (tree, struct obstack *);
130 static void readonly_warning (tree, enum lvalue_use);
131 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
132 static void record_maybe_used_decl (tree);
133 static bool comptypes_internal (const_tree, const_tree,
134 struct comptypes_data *data);
136 /* Return true if EXP is a null pointer constant, false otherwise. */
138 bool
139 null_pointer_constant_p (const_tree expr)
141 /* This should really operate on c_expr structures, but they aren't
142 yet available everywhere required. */
143 tree type = TREE_TYPE (expr);
145 /* An integer constant expression with the value 0, such an expression
146 cast to type void*, or the predefined constant nullptr, are a null
147 pointer constant. */
148 if (expr == nullptr_node)
149 return true;
151 return (TREE_CODE (expr) == INTEGER_CST
152 && !TREE_OVERFLOW (expr)
153 && integer_zerop (expr)
154 && (INTEGRAL_TYPE_P (type)
155 || (TREE_CODE (type) == POINTER_TYPE
156 && VOID_TYPE_P (TREE_TYPE (type))
157 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
160 /* EXPR may appear in an unevaluated part of an integer constant
161 expression, but not in an evaluated part. Wrap it in a
162 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
163 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
165 static tree
166 note_integer_operands (tree expr)
168 tree ret;
169 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
171 ret = copy_node (expr);
172 TREE_OVERFLOW (ret) = 1;
174 else
176 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
177 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
179 return ret;
182 /* Having checked whether EXPR may appear in an unevaluated part of an
183 integer constant expression and found that it may, remove any
184 C_MAYBE_CONST_EXPR noting this fact and return the resulting
185 expression. */
187 static inline tree
188 remove_c_maybe_const_expr (tree expr)
190 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
191 return C_MAYBE_CONST_EXPR_EXPR (expr);
192 else
193 return expr;
196 \f/* This is a cache to hold if two types are seen. */
198 struct tagged_tu_seen_cache {
199 const struct tagged_tu_seen_cache * next;
200 const_tree t1;
201 const_tree t2;
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 struct composite_cache {
388 tree t1;
389 tree t2;
390 tree composite;
391 struct composite_cache* next;
394 tree
395 composite_type_internal (tree t1, tree t2, struct composite_cache* cache)
397 enum tree_code code1;
398 enum tree_code code2;
399 tree attributes;
401 /* Save time if the two types are the same. */
403 if (t1 == t2) return t1;
405 /* If one type is nonsense, use the other. */
406 if (t1 == error_mark_node)
407 return t2;
408 if (t2 == error_mark_node)
409 return t1;
411 code1 = TREE_CODE (t1);
412 code2 = TREE_CODE (t2);
414 /* Merge the attributes. */
415 attributes = targetm.merge_type_attributes (t1, t2);
417 /* If one is an enumerated type and the other is the compatible
418 integer type, the composite type might be either of the two
419 (DR#013 question 3). For consistency, use the enumerated type as
420 the composite type. */
422 if (code1 == ENUMERAL_TYPE
423 && (code2 == INTEGER_TYPE
424 || code2 == BOOLEAN_TYPE))
425 return t1;
426 if (code2 == ENUMERAL_TYPE
427 && (code1 == INTEGER_TYPE
428 || code1 == BOOLEAN_TYPE))
429 return t2;
431 gcc_assert (code1 == code2);
433 switch (code1)
435 case POINTER_TYPE:
436 /* For two pointers, do this recursively on the target type. */
438 tree pointed_to_1 = TREE_TYPE (t1);
439 tree pointed_to_2 = TREE_TYPE (t2);
440 tree target = composite_type_internal (pointed_to_1,
441 pointed_to_2, cache);
442 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
443 t1 = build_type_attribute_variant (t1, attributes);
444 return qualify_type (t1, t2);
447 case ARRAY_TYPE:
449 tree elt = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
450 cache);
451 int quals;
452 tree unqual_elt;
453 tree d1 = TYPE_DOMAIN (t1);
454 tree d2 = TYPE_DOMAIN (t2);
455 bool d1_variable, d2_variable;
456 bool d1_zero, d2_zero;
457 bool t1_complete, t2_complete;
459 /* We should not have any type quals on arrays at all. */
460 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
461 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
463 t1_complete = COMPLETE_TYPE_P (t1);
464 t2_complete = COMPLETE_TYPE_P (t2);
466 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
467 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
469 d1_variable = (!d1_zero
470 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
471 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
472 d2_variable = (!d2_zero
473 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
474 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
475 d1_variable = d1_variable || (d1_zero && C_TYPE_VARIABLE_SIZE (t1));
476 d2_variable = d2_variable || (d2_zero && C_TYPE_VARIABLE_SIZE (t2));
478 /* Save space: see if the result is identical to one of the args. */
479 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
480 && (d2_variable || d2_zero || !d1_variable))
481 return build_type_attribute_variant (t1, attributes);
482 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
483 && (d1_variable || d1_zero || !d2_variable))
484 return build_type_attribute_variant (t2, attributes);
486 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
487 return build_type_attribute_variant (t1, attributes);
488 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
489 return build_type_attribute_variant (t2, attributes);
491 /* Merge the element types, and have a size if either arg has
492 one. We may have qualifiers on the element types. To set
493 up TYPE_MAIN_VARIANT correctly, we need to form the
494 composite of the unqualified types and add the qualifiers
495 back at the end. */
496 quals = TYPE_QUALS (strip_array_types (elt));
497 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
498 t1 = build_array_type (unqual_elt,
499 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
500 && (d2_variable
501 || d2_zero
502 || !d1_variable))
503 ? t1
504 : t2));
505 /* Ensure a composite type involving a zero-length array type
506 is a zero-length type not an incomplete type. */
507 if (d1_zero && d2_zero
508 && (t1_complete || t2_complete)
509 && !COMPLETE_TYPE_P (t1))
511 TYPE_SIZE (t1) = bitsize_zero_node;
512 TYPE_SIZE_UNIT (t1) = size_zero_node;
514 t1 = c_build_qualified_type (t1, quals);
515 return build_type_attribute_variant (t1, attributes);
518 case RECORD_TYPE:
519 case UNION_TYPE:
520 if (flag_isoc23 && !comptypes_same_p (t1, t2))
522 gcc_checking_assert (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2));
523 gcc_checking_assert (!TYPE_NAME (t1) || comptypes (t1, t2));
525 /* If a composite type for these two types is already under
526 construction, return it. */
528 for (struct composite_cache *c = cache; c != NULL; c = c->next)
529 if (c->t1 == t1 && c->t2 == t2)
530 return c->composite;
532 /* Otherwise, create a new type node and link it into the cache. */
534 tree n = make_node (code1);
535 TYPE_NAME (n) = TYPE_NAME (t1);
537 struct composite_cache cache2 = { t1, t2, n, cache };
538 cache = &cache2;
540 tree f1 = TYPE_FIELDS (t1);
541 tree f2 = TYPE_FIELDS (t2);
542 tree fields = NULL_TREE;
544 for (tree a = f1, b = f2; a && b;
545 a = DECL_CHAIN (a), b = DECL_CHAIN (b))
547 tree ta = TREE_TYPE (a);
548 tree tb = TREE_TYPE (b);
550 if (DECL_C_BIT_FIELD (a))
552 ta = DECL_BIT_FIELD_TYPE (a);
553 tb = DECL_BIT_FIELD_TYPE (b);
556 gcc_assert (DECL_NAME (a) == DECL_NAME (b));
557 gcc_checking_assert (!DECL_NAME (a) || comptypes (ta, tb));
559 tree t = composite_type_internal (ta, tb, cache);
560 tree f = build_decl (input_location, FIELD_DECL, DECL_NAME (a), t);
562 DECL_PACKED (f) = DECL_PACKED (a);
563 SET_DECL_ALIGN (f, DECL_ALIGN (a));
564 DECL_ATTRIBUTES (f) = DECL_ATTRIBUTES (a);
565 C_DECL_VARIABLE_SIZE (f) = C_TYPE_VARIABLE_SIZE (t);
567 finish_decl (f, input_location, NULL, NULL, NULL);
569 if (DECL_C_BIT_FIELD (a))
571 /* This will be processed by finish_struct. */
572 SET_DECL_C_BIT_FIELD (f);
573 DECL_INITIAL (f) = build_int_cst (integer_type_node,
574 tree_to_uhwi (DECL_SIZE (a)));
575 DECL_NONADDRESSABLE_P (f) = true;
576 DECL_PADDING_P (f) = !DECL_NAME (a);
579 DECL_CHAIN (f) = fields;
580 fields = f;
583 fields = nreverse (fields);
585 /* Setup the struct/union type. Because we inherit all variably
586 modified components, we can ignore the size expression. */
587 tree expr = NULL_TREE;
588 n = finish_struct(input_location, n, fields, attributes, NULL, &expr);
590 n = qualify_type (n, t1);
592 gcc_checking_assert (!TYPE_NAME (n) || comptypes (n, t1));
593 gcc_checking_assert (!TYPE_NAME (n) || comptypes (n, t2));
595 return n;
597 /* FALLTHRU */
598 case ENUMERAL_TYPE:
599 if (attributes != NULL)
601 /* Try harder not to create a new aggregate type. */
602 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
603 return t1;
604 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
605 return t2;
607 return build_type_attribute_variant (t1, attributes);
609 case FUNCTION_TYPE:
610 /* Function types: prefer the one that specified arg types.
611 If both do, merge the arg types. Also merge the return types. */
613 tree valtype = composite_type_internal (TREE_TYPE (t1),
614 TREE_TYPE (t2), cache);
615 tree p1 = TYPE_ARG_TYPES (t1);
616 tree p2 = TYPE_ARG_TYPES (t2);
617 int len;
618 tree newargs, n;
619 int i;
621 /* Save space: see if the result is identical to one of the args. */
622 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
623 return build_functype_attribute_variant (t1, t2, attributes);
624 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
625 return build_functype_attribute_variant (t2, t1, attributes);
627 /* Simple way if one arg fails to specify argument types. */
628 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
630 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2),
631 TYPE_NO_NAMED_ARGS_STDARG_P (t2));
632 t1 = build_type_attribute_variant (t1, attributes);
633 return qualify_type (t1, t2);
635 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
637 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1),
638 TYPE_NO_NAMED_ARGS_STDARG_P (t1));
639 t1 = build_type_attribute_variant (t1, attributes);
640 return qualify_type (t1, t2);
643 /* If both args specify argument types, we must merge the two
644 lists, argument by argument. */
646 for (len = 0, newargs = p1;
647 newargs && newargs != void_list_node;
648 len++, newargs = TREE_CHAIN (newargs))
651 for (i = 0; i < len; i++)
652 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
654 n = newargs;
656 for (; p1 && p1 != void_list_node;
657 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
659 tree mv1 = TREE_VALUE (p1);
660 if (mv1 && mv1 != error_mark_node
661 && TREE_CODE (mv1) != ARRAY_TYPE)
662 mv1 = TYPE_MAIN_VARIANT (mv1);
664 tree mv2 = TREE_VALUE (p2);
665 if (mv2 && mv2 != error_mark_node
666 && TREE_CODE (mv2) != ARRAY_TYPE)
667 mv2 = TYPE_MAIN_VARIANT (mv2);
669 /* A null type means arg type is not specified.
670 Take whatever the other function type has. */
671 if (TREE_VALUE (p1) == NULL_TREE)
673 TREE_VALUE (n) = TREE_VALUE (p2);
674 goto parm_done;
676 if (TREE_VALUE (p2) == NULL_TREE)
678 TREE_VALUE (n) = TREE_VALUE (p1);
679 goto parm_done;
682 /* Given wait (union {union wait *u; int *i} *)
683 and wait (union wait *),
684 prefer union wait * as type of parm. */
685 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
686 && TREE_VALUE (p1) != TREE_VALUE (p2))
688 tree memb;
689 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
690 memb; memb = DECL_CHAIN (memb))
692 tree mv3 = TREE_TYPE (memb);
693 if (mv3 && mv3 != error_mark_node
694 && TREE_CODE (mv3) != ARRAY_TYPE)
695 mv3 = TYPE_MAIN_VARIANT (mv3);
696 if (comptypes (mv3, mv2))
698 TREE_VALUE (n) = composite_type_internal (TREE_TYPE (memb),
699 TREE_VALUE (p2),
700 cache);
701 pedwarn (input_location, OPT_Wpedantic,
702 "function types not truly compatible in ISO C");
703 goto parm_done;
707 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
708 && TREE_VALUE (p2) != TREE_VALUE (p1))
710 tree memb;
711 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
712 memb; memb = DECL_CHAIN (memb))
714 tree mv3 = TREE_TYPE (memb);
715 if (mv3 && mv3 != error_mark_node
716 && TREE_CODE (mv3) != ARRAY_TYPE)
717 mv3 = TYPE_MAIN_VARIANT (mv3);
718 if (comptypes (mv3, mv1))
720 TREE_VALUE (n)
721 = composite_type_internal (TREE_TYPE (memb),
722 TREE_VALUE (p1),
723 cache);
724 pedwarn (input_location, OPT_Wpedantic,
725 "function types not truly compatible in ISO C");
726 goto parm_done;
730 TREE_VALUE (n) = composite_type_internal (mv1, mv2, cache);
731 parm_done: ;
734 t1 = build_function_type (valtype, newargs);
735 t1 = qualify_type (t1, t2);
737 /* FALLTHRU */
739 default:
740 return build_type_attribute_variant (t1, attributes);
744 tree
745 composite_type (tree t1, tree t2)
747 struct composite_cache cache = { };
748 return composite_type_internal (t1, t2, &cache);
751 /* Return the type of a conditional expression between pointers to
752 possibly differently qualified versions of compatible types.
754 We assume that comp_target_types has already been done and returned
755 true; if that isn't so, this may crash. */
757 static tree
758 common_pointer_type (tree t1, tree t2)
760 tree attributes;
761 tree pointed_to_1, mv1;
762 tree pointed_to_2, mv2;
763 tree target;
764 unsigned target_quals;
765 addr_space_t as1, as2, as_common;
766 int quals1, quals2;
768 /* Save time if the two types are the same. */
770 if (t1 == t2) return t1;
772 /* If one type is nonsense, use the other. */
773 if (t1 == error_mark_node)
774 return t2;
775 if (t2 == error_mark_node)
776 return t1;
778 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
779 && TREE_CODE (t2) == POINTER_TYPE);
781 /* Merge the attributes. */
782 attributes = targetm.merge_type_attributes (t1, t2);
784 /* Find the composite type of the target types, and combine the
785 qualifiers of the two types' targets. Do not lose qualifiers on
786 array element types by taking the TYPE_MAIN_VARIANT. */
787 mv1 = pointed_to_1 = TREE_TYPE (t1);
788 mv2 = pointed_to_2 = TREE_TYPE (t2);
789 if (TREE_CODE (mv1) != ARRAY_TYPE)
790 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
791 if (TREE_CODE (mv2) != ARRAY_TYPE)
792 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
793 target = composite_type (mv1, mv2);
795 /* Strip array types to get correct qualifier for pointers to arrays */
796 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
797 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
799 /* For function types do not merge const qualifiers, but drop them
800 if used inconsistently. The middle-end uses these to mark const
801 and noreturn functions. */
802 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
803 target_quals = (quals1 & quals2);
804 else
805 target_quals = (quals1 | quals2);
807 /* If the two named address spaces are different, determine the common
808 superset address space. This is guaranteed to exist due to the
809 assumption that comp_target_type returned true. */
810 as1 = TYPE_ADDR_SPACE (pointed_to_1);
811 as2 = TYPE_ADDR_SPACE (pointed_to_2);
812 if (!addr_space_superset (as1, as2, &as_common))
813 gcc_unreachable ();
815 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
817 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
818 return build_type_attribute_variant (t1, attributes);
821 /* Return the common type for two arithmetic types under the usual
822 arithmetic conversions. The default conversions have already been
823 applied, and enumerated types converted to their compatible integer
824 types. The resulting type is unqualified and has no attributes.
826 This is the type for the result of most arithmetic operations
827 if the operands have the given two types. */
829 static tree
830 c_common_type (tree t1, tree t2)
832 enum tree_code code1;
833 enum tree_code code2;
835 /* If one type is nonsense, use the other. */
836 if (t1 == error_mark_node)
837 return t2;
838 if (t2 == error_mark_node)
839 return t1;
841 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
842 t1 = TYPE_MAIN_VARIANT (t1);
844 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
845 t2 = TYPE_MAIN_VARIANT (t2);
847 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
849 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
850 t1 = build_type_attribute_variant (t1, attrs);
853 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
855 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
856 t2 = build_type_attribute_variant (t2, attrs);
859 /* Save time if the two types are the same. */
861 if (t1 == t2) return t1;
863 code1 = TREE_CODE (t1);
864 code2 = TREE_CODE (t2);
866 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
867 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
868 || code1 == INTEGER_TYPE || code1 == BITINT_TYPE);
869 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
870 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
871 || code2 == INTEGER_TYPE || code2 == BITINT_TYPE);
873 /* When one operand is a decimal float type, the other operand cannot be
874 a generic float type or a complex type. We also disallow vector types
875 here. */
876 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
877 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
879 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
881 error ("cannot mix operands of decimal floating and vector types");
882 return error_mark_node;
884 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
886 error ("cannot mix operands of decimal floating and complex types");
887 return error_mark_node;
889 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
891 error ("cannot mix operands of decimal floating "
892 "and other floating types");
893 return error_mark_node;
897 /* If one type is a vector type, return that type. (How the usual
898 arithmetic conversions apply to the vector types extension is not
899 precisely specified.) */
900 if (code1 == VECTOR_TYPE)
901 return t1;
903 if (code2 == VECTOR_TYPE)
904 return t2;
906 /* If one type is complex, form the common type of the non-complex
907 components, then make that complex. Use T1 or T2 if it is the
908 required type. */
909 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
911 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
912 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
913 tree subtype = c_common_type (subtype1, subtype2);
915 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
916 return t1;
917 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
918 return t2;
919 else if (TREE_CODE (subtype) == BITINT_TYPE)
921 sorry ("%<_Complex _BitInt(%d)%> unsupported",
922 TYPE_PRECISION (subtype));
923 return code1 == COMPLEX_TYPE ? t1 : t2;
925 else
926 return build_complex_type (subtype);
929 /* If only one is real, use it as the result. */
931 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
932 return t1;
934 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
935 return t2;
937 /* If both are real and either are decimal floating point types, use
938 the decimal floating point type with the greater precision. */
940 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
942 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
943 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
944 return dfloat128_type_node;
945 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
946 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
947 return dfloat64_type_node;
948 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
949 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
950 return dfloat32_type_node;
953 /* Deal with fixed-point types. */
954 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
956 unsigned int unsignedp = 0, satp = 0;
957 scalar_mode m1, m2;
958 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
960 m1 = SCALAR_TYPE_MODE (t1);
961 m2 = SCALAR_TYPE_MODE (t2);
963 /* If one input type is saturating, the result type is saturating. */
964 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
965 satp = 1;
967 /* If both fixed-point types are unsigned, the result type is unsigned.
968 When mixing fixed-point and integer types, follow the sign of the
969 fixed-point type.
970 Otherwise, the result type is signed. */
971 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
972 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
973 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
974 && TYPE_UNSIGNED (t1))
975 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
976 && TYPE_UNSIGNED (t2)))
977 unsignedp = 1;
979 /* The result type is signed. */
980 if (unsignedp == 0)
982 /* If the input type is unsigned, we need to convert to the
983 signed type. */
984 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
986 enum mode_class mclass = (enum mode_class) 0;
987 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
988 mclass = MODE_FRACT;
989 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
990 mclass = MODE_ACCUM;
991 else
992 gcc_unreachable ();
993 m1 = as_a <scalar_mode>
994 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
996 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
998 enum mode_class mclass = (enum mode_class) 0;
999 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
1000 mclass = MODE_FRACT;
1001 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
1002 mclass = MODE_ACCUM;
1003 else
1004 gcc_unreachable ();
1005 m2 = as_a <scalar_mode>
1006 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
1010 if (code1 == FIXED_POINT_TYPE)
1012 fbit1 = GET_MODE_FBIT (m1);
1013 ibit1 = GET_MODE_IBIT (m1);
1015 else
1017 fbit1 = 0;
1018 /* Signed integers need to subtract one sign bit. */
1019 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
1022 if (code2 == FIXED_POINT_TYPE)
1024 fbit2 = GET_MODE_FBIT (m2);
1025 ibit2 = GET_MODE_IBIT (m2);
1027 else
1029 fbit2 = 0;
1030 /* Signed integers need to subtract one sign bit. */
1031 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
1034 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
1035 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
1036 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
1037 satp);
1040 /* Both real or both integers; use the one with greater precision. */
1042 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
1043 return t1;
1044 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
1045 return t2;
1047 /* Same precision. Prefer long longs to longs to ints when the
1048 same precision, following the C99 rules on integer type rank
1049 (which are equivalent to the C90 rules for C90 types). */
1051 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
1052 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
1053 return long_long_unsigned_type_node;
1055 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
1056 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
1058 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1059 return long_long_unsigned_type_node;
1060 else
1061 return long_long_integer_type_node;
1064 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
1065 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
1066 return long_unsigned_type_node;
1068 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
1069 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
1071 /* But preserve unsignedness from the other type,
1072 since long cannot hold all the values of an unsigned int. */
1073 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1074 return long_unsigned_type_node;
1075 else
1076 return long_integer_type_node;
1079 /* For floating types of the same TYPE_PRECISION (which we here
1080 assume means either the same set of values, or sets of values
1081 neither a subset of the other, with behavior being undefined in
1082 the latter case), follow the rules from TS 18661-3: prefer
1083 interchange types _FloatN, then standard types long double,
1084 double, float, then extended types _FloatNx. For extended types,
1085 check them starting with _Float128x as that seems most consistent
1086 in spirit with preferring long double to double; for interchange
1087 types, also check in that order for consistency although it's not
1088 possible for more than one of them to have the same
1089 precision. */
1090 tree mv1 = TYPE_MAIN_VARIANT (t1);
1091 tree mv2 = TYPE_MAIN_VARIANT (t2);
1093 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
1094 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
1095 return FLOATN_TYPE_NODE (i);
1097 /* Likewise, prefer long double to double even if same size. */
1098 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
1099 return long_double_type_node;
1101 /* Likewise, prefer double to float even if same size.
1102 We got a couple of embedded targets with 32 bit doubles, and the
1103 pdp11 might have 64 bit floats. */
1104 if (mv1 == double_type_node || mv2 == double_type_node)
1105 return double_type_node;
1107 if (mv1 == float_type_node || mv2 == float_type_node)
1108 return float_type_node;
1110 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1111 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1112 return FLOATNX_TYPE_NODE (i);
1114 if ((code1 == BITINT_TYPE || code2 == BITINT_TYPE) && code1 != code2)
1116 /* Prefer any other integral types over bit-precise integer types. */
1117 if (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2))
1118 return code1 == BITINT_TYPE ? t2 : t1;
1119 /* If BITINT_TYPE is unsigned and the other type is signed
1120 non-BITINT_TYPE with the same precision, the latter has higher rank.
1121 In that case:
1122 Otherwise, both operands are converted to the unsigned integer type
1123 corresponding to the type of the operand with signed integer type. */
1124 if (TYPE_UNSIGNED (code1 == BITINT_TYPE ? t1 : t2))
1125 return c_common_unsigned_type (code1 == BITINT_TYPE ? t2 : t1);
1128 /* Otherwise prefer the unsigned one. */
1130 if (TYPE_UNSIGNED (t1))
1131 return t1;
1132 else
1133 return t2;
1136 /* Wrapper around c_common_type that is used by c-common.cc and other
1137 front end optimizations that remove promotions. ENUMERAL_TYPEs
1138 are allowed here and are converted to their compatible integer types.
1139 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1140 preferably a non-Boolean type as the common type. */
1141 tree
1142 common_type (tree t1, tree t2)
1144 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1145 t1 = ENUM_UNDERLYING_TYPE (t1);
1146 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1147 t2 = ENUM_UNDERLYING_TYPE (t2);
1149 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1150 if (TREE_CODE (t1) == BOOLEAN_TYPE
1151 && TREE_CODE (t2) == BOOLEAN_TYPE)
1152 return boolean_type_node;
1154 /* If either type is BOOLEAN_TYPE, then return the other. */
1155 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1156 return t2;
1157 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1158 return t1;
1160 return c_common_type (t1, t2);
1163 struct comptypes_data {
1164 bool enum_and_int_p;
1165 bool different_types_p;
1166 bool warning_needed;
1167 bool anon_field;
1168 bool equiv;
1170 const struct tagged_tu_seen_cache* cache;
1173 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1174 or various other operations. Return 2 if they are compatible
1175 but a warning may be needed if you use them together. */
1178 comptypes (tree type1, tree type2)
1180 struct comptypes_data data = { };
1181 bool ret = comptypes_internal (type1, type2, &data);
1183 return ret ? (data.warning_needed ? 2 : 1) : 0;
1187 /* Like comptypes, but it returns non-zero only for identical
1188 types. */
1190 bool
1191 comptypes_same_p (tree type1, tree type2)
1193 struct comptypes_data data = { };
1194 bool ret = comptypes_internal (type1, type2, &data);
1196 if (data.different_types_p)
1197 return false;
1199 return ret;
1203 /* Like comptypes, but if it returns non-zero because enum and int are
1204 compatible, it sets *ENUM_AND_INT_P to true. */
1207 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1209 struct comptypes_data data = { };
1210 bool ret = comptypes_internal (type1, type2, &data);
1211 *enum_and_int_p = data.enum_and_int_p;
1213 return ret ? (data.warning_needed ? 2 : 1) : 0;
1216 /* Like comptypes, but if it returns nonzero for different types, it
1217 sets *DIFFERENT_TYPES_P to true. */
1220 comptypes_check_different_types (tree type1, tree type2,
1221 bool *different_types_p)
1223 struct comptypes_data data = { };
1224 bool ret = comptypes_internal (type1, type2, &data);
1225 *different_types_p = data.different_types_p;
1227 return ret ? (data.warning_needed ? 2 : 1) : 0;
1231 /* Like comptypes, but if it returns nonzero for struct and union
1232 types considered equivalent for aliasing purposes. */
1234 bool
1235 comptypes_equiv_p (tree type1, tree type2)
1237 struct comptypes_data data = { };
1238 data.equiv = true;
1239 bool ret = comptypes_internal (type1, type2, &data);
1241 return ret;
1245 /* Return true if TYPE1 and TYPE2 are compatible types for assignment
1246 or various other operations. If they are compatible but a warning may
1247 be needed if you use them together, 'warning_needed' in DATA is set.
1248 If one type is an enum and the other a compatible integer type, then
1249 this sets 'enum_and_int_p' in DATA to true (it is never set to
1250 false). If the types are compatible but different enough not to be
1251 permitted in C11 typedef redeclarations, then this sets
1252 'different_types_p' in DATA to true; it is never set to
1253 false, but may or may not be set if the types are incompatible.
1254 This differs from comptypes, in that we don't free the seen
1255 types. */
1257 static bool
1258 comptypes_internal (const_tree type1, const_tree type2,
1259 struct comptypes_data *data)
1261 const_tree t1 = type1;
1262 const_tree t2 = type2;
1264 /* Suppress errors caused by previously reported errors. */
1266 if (t1 == t2 || !t1 || !t2
1267 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1268 return true;
1270 /* Enumerated types are compatible with integer types, but this is
1271 not transitive: two enumerated types in the same translation unit
1272 are compatible with each other only if they are the same type. */
1274 if (TREE_CODE (t1) == ENUMERAL_TYPE
1275 && COMPLETE_TYPE_P (t1)
1276 && TREE_CODE (t2) != ENUMERAL_TYPE)
1278 t1 = ENUM_UNDERLYING_TYPE (t1);
1279 if (TREE_CODE (t2) != VOID_TYPE)
1281 data->enum_and_int_p = true;
1282 data->different_types_p = true;
1285 else if (TREE_CODE (t2) == ENUMERAL_TYPE
1286 && COMPLETE_TYPE_P (t2)
1287 && TREE_CODE (t1) != ENUMERAL_TYPE)
1289 t2 = ENUM_UNDERLYING_TYPE (t2);
1290 if (TREE_CODE (t1) != VOID_TYPE)
1292 data->enum_and_int_p = true;
1293 data->different_types_p = true;
1297 if (t1 == t2)
1298 return true;
1300 /* Different classes of types can't be compatible. */
1302 if (TREE_CODE (t1) != TREE_CODE (t2))
1303 return false;
1305 /* Qualifiers must match. C99 6.7.3p9 */
1307 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1308 return false;
1310 /* Allow for two different type nodes which have essentially the same
1311 definition. Note that we already checked for equality of the type
1312 qualifiers (just above). */
1314 if (TREE_CODE (t1) != ARRAY_TYPE
1315 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1316 return true;
1318 int attrval;
1320 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1321 if (!(attrval = comp_type_attributes (t1, t2)))
1322 return false;
1324 if (2 == attrval)
1325 data->warning_needed = true;
1327 switch (TREE_CODE (t1))
1329 case INTEGER_TYPE:
1330 case FIXED_POINT_TYPE:
1331 case REAL_TYPE:
1332 case BITINT_TYPE:
1333 /* With these nodes, we can't determine type equivalence by
1334 looking at what is stored in the nodes themselves, because
1335 two nodes might have different TYPE_MAIN_VARIANTs but still
1336 represent the same type. For example, wchar_t and int could
1337 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1338 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1339 and are distinct types. On the other hand, int and the
1340 following typedef
1342 typedef int INT __attribute((may_alias));
1344 have identical properties, different TYPE_MAIN_VARIANTs, but
1345 represent the same type. The canonical type system keeps
1346 track of equivalence in this case, so we fall back on it. */
1347 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1349 case POINTER_TYPE:
1350 /* Do not remove mode information. */
1351 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1352 return false;
1353 return comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1355 case FUNCTION_TYPE:
1356 return function_types_compatible_p (t1, t2, data);
1358 case ARRAY_TYPE:
1360 tree d1 = TYPE_DOMAIN (t1);
1361 tree d2 = TYPE_DOMAIN (t2);
1362 bool d1_variable, d2_variable;
1363 bool d1_zero, d2_zero;
1365 /* Target types must match incl. qualifiers. */
1366 if (!comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data))
1367 return false;
1369 if ((d1 == NULL_TREE) != (d2 == NULL_TREE))
1370 data->different_types_p = true;
1371 /* Ignore size mismatches. */
1372 if (data->equiv)
1373 return true;
1374 /* Sizes must match unless one is missing or variable. */
1375 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1376 return true;
1378 d1_zero = !TYPE_MAX_VALUE (d1);
1379 d2_zero = !TYPE_MAX_VALUE (d2);
1381 d1_variable = (!d1_zero
1382 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1383 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1384 d2_variable = (!d2_zero
1385 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1386 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1387 d1_variable = d1_variable || (d1_zero && C_TYPE_VARIABLE_SIZE (t1));
1388 d2_variable = d2_variable || (d2_zero && C_TYPE_VARIABLE_SIZE (t2));
1390 if (d1_variable != d2_variable)
1391 data->different_types_p = true;
1392 if (d1_variable || d2_variable)
1393 return true;
1394 if (d1_zero && d2_zero)
1395 return true;
1396 if (d1_zero || d2_zero
1397 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1398 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1399 return false;
1401 return true;
1404 case ENUMERAL_TYPE:
1405 case RECORD_TYPE:
1406 case UNION_TYPE:
1408 if (!flag_isoc23)
1409 return false;
1411 return tagged_types_tu_compatible_p (t1, t2, data);
1413 case VECTOR_TYPE:
1414 return known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1415 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1417 default:
1418 return false;
1420 gcc_unreachable ();
1423 /* Return true if TTL and TTR are pointers to types that are equivalent, ignoring
1424 their qualifiers, except for named address spaces. If the pointers point to
1425 different named addresses, then we must determine if one address space is a
1426 subset of the other. */
1428 static bool
1429 comp_target_types (location_t location, tree ttl, tree ttr)
1431 int val;
1432 int val_ped;
1433 tree mvl = TREE_TYPE (ttl);
1434 tree mvr = TREE_TYPE (ttr);
1435 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1436 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1437 addr_space_t as_common;
1438 bool enum_and_int_p;
1440 /* Fail if pointers point to incompatible address spaces. */
1441 if (!addr_space_superset (asl, asr, &as_common))
1442 return 0;
1444 /* For pedantic record result of comptypes on arrays before losing
1445 qualifiers on the element type below. */
1446 val_ped = 1;
1448 if (TREE_CODE (mvl) == ARRAY_TYPE
1449 && TREE_CODE (mvr) == ARRAY_TYPE)
1450 val_ped = comptypes (mvl, mvr);
1452 /* Qualifiers on element types of array types that are
1453 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1455 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1456 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1457 : TYPE_MAIN_VARIANT (mvl));
1459 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1460 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1461 : TYPE_MAIN_VARIANT (mvr));
1463 enum_and_int_p = false;
1464 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1466 if (val == 1 && val_ped != 1)
1467 pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1468 "in ISO C before C23");
1470 if (val == 2)
1471 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1473 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1474 warning_at (location, OPT_Wc___compat,
1475 "pointer target types incompatible in C++");
1477 return val;
1480 /* Subroutines of `comptypes'. */
1482 /* Return true if two 'struct', 'union', or 'enum' types T1 and T2 are
1483 compatible. The two types are not the same (which has been
1484 checked earlier in comptypes_internal). */
1486 static bool
1487 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1488 struct comptypes_data *data)
1490 tree s1, s2;
1492 /* We have to verify that the tags of the types are the same. This
1493 is harder than it looks because this may be a typedef, so we have
1494 to go look at the original type. It may even be a typedef of a
1495 typedef...
1496 In the case of compiler-created builtin structs the TYPE_DECL
1497 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1498 while (TYPE_NAME (t1)
1499 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1500 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1501 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1503 while (TYPE_NAME (t2)
1504 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1505 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1506 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1508 if (TYPE_NAME (t1) != TYPE_NAME (t2))
1509 return false;
1511 if (!data->anon_field && NULL_TREE == TYPE_NAME (t1))
1512 return false;
1514 if (!data->anon_field && TYPE_STUB_DECL (t1) != TYPE_STUB_DECL (t2))
1515 data->different_types_p = true;
1517 /* Incomplete types are incompatible inside a TU. */
1518 if (TYPE_SIZE (t1) == NULL || TYPE_SIZE (t2) == NULL)
1519 return false;
1521 if (ENUMERAL_TYPE != TREE_CODE (t1)
1522 && (TYPE_REVERSE_STORAGE_ORDER (t1)
1523 != TYPE_REVERSE_STORAGE_ORDER (t2)))
1524 return false;
1526 /* For types already being looked at in some active
1527 invocation of this function, assume compatibility.
1528 The cache is built as a linked list on the stack
1529 with the head of the list passed downwards. */
1530 for (const struct tagged_tu_seen_cache *t = data->cache;
1531 t != NULL; t = t->next)
1532 if (t->t1 == t1 && t->t2 == t2)
1533 return true;
1535 const struct tagged_tu_seen_cache entry = { data->cache, t1, t2 };
1537 switch (TREE_CODE (t1))
1539 case ENUMERAL_TYPE:
1541 if (!comptypes (ENUM_UNDERLYING_TYPE (t1), ENUM_UNDERLYING_TYPE (t2)))
1542 return false;
1544 /* Speed up the case where the type values are in the same order. */
1545 tree tv1 = TYPE_VALUES (t1);
1546 tree tv2 = TYPE_VALUES (t2);
1548 if (tv1 == tv2)
1549 return true;
1551 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1553 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1554 break;
1556 if (simple_cst_equal (DECL_INITIAL (TREE_VALUE (tv1)),
1557 DECL_INITIAL (TREE_VALUE (tv2))) != 1)
1558 break;
1561 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1562 return true;
1564 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1565 return false;
1567 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1568 return false;
1570 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1572 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1574 if (s2 == NULL
1575 || simple_cst_equal (DECL_INITIAL (TREE_VALUE (s1)),
1576 DECL_INITIAL (TREE_VALUE (s2))) != 1)
1577 return false;
1580 return true;
1583 case UNION_TYPE:
1584 case RECORD_TYPE:
1586 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1587 return false;
1589 if (data->equiv && (C_TYPE_VARIABLE_SIZE (t1) || C_TYPE_VARIABLE_SIZE (t2)))
1590 return false;
1592 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1593 s1 && s2;
1594 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1596 gcc_assert (TREE_CODE (s1) == FIELD_DECL);
1597 gcc_assert (TREE_CODE (s2) == FIELD_DECL);
1599 if (DECL_NAME (s1) != DECL_NAME (s2))
1600 return false;
1602 if (DECL_ALIGN (s1) != DECL_ALIGN (s2))
1603 return false;
1605 data->anon_field = !DECL_NAME (s1);
1607 data->cache = &entry;
1608 if (!comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), data))
1609 return false;
1611 tree st1 = TYPE_SIZE (TREE_TYPE (s1));
1612 tree st2 = TYPE_SIZE (TREE_TYPE (s2));
1614 if (data->equiv
1615 && st1 && TREE_CODE (st1) == INTEGER_CST
1616 && st2 && TREE_CODE (st2) == INTEGER_CST
1617 && !tree_int_cst_equal (st1, st2))
1618 return false;
1620 return true;
1622 default:
1623 gcc_unreachable ();
1627 /* Return true if two function types F1 and F2 are compatible.
1628 If either type specifies no argument types,
1629 the other must specify a fixed number of self-promoting arg types.
1630 Otherwise, if one type specifies only the number of arguments,
1631 the other must specify that number of self-promoting arg types.
1632 Otherwise, the argument types must match. */
1634 static bool
1635 function_types_compatible_p (const_tree f1, const_tree f2,
1636 struct comptypes_data *data)
1638 tree args1, args2;
1639 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1640 int val = 1;
1641 int val1;
1642 tree ret1, ret2;
1644 ret1 = TREE_TYPE (f1);
1645 ret2 = TREE_TYPE (f2);
1647 /* 'volatile' qualifiers on a function's return type used to mean
1648 the function is noreturn. */
1649 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1650 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1651 if (TYPE_VOLATILE (ret1))
1652 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1653 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1654 if (TYPE_VOLATILE (ret2))
1655 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1656 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1657 val = comptypes_internal (ret1, ret2, data);
1658 if (val == 0)
1659 return 0;
1661 args1 = TYPE_ARG_TYPES (f1);
1662 args2 = TYPE_ARG_TYPES (f2);
1664 if ((args1 == NULL_TREE) != (args2 == NULL_TREE))
1665 data->different_types_p = true;
1667 /* An unspecified parmlist matches any specified parmlist
1668 whose argument types don't need default promotions. */
1670 if (args1 == NULL_TREE)
1672 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
1673 return 0;
1674 if (!self_promoting_args_p (args2))
1675 return 0;
1676 /* If one of these types comes from a non-prototype fn definition,
1677 compare that with the other type's arglist.
1678 If they don't match, ask for a warning (but no error). */
1679 if (TYPE_ACTUAL_ARG_TYPES (f1)
1680 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1681 data) != 1)
1683 val = 1;
1684 data->warning_needed = true;
1686 return val;
1688 if (args2 == NULL_TREE)
1690 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
1691 return 0;
1692 if (!self_promoting_args_p (args1))
1693 return 0;
1694 if (TYPE_ACTUAL_ARG_TYPES (f2)
1695 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1696 data) != 1)
1698 val = 1;
1699 data->warning_needed = true;
1701 return val;
1704 /* Both types have argument lists: compare them and propagate results. */
1705 val1 = type_lists_compatible_p (args1, args2, data);
1706 return val1;
1709 /* Check two lists of types for compatibility, returning false for
1710 incompatible, true for compatible. */
1712 static bool
1713 type_lists_compatible_p (const_tree args1, const_tree args2,
1714 struct comptypes_data *data)
1716 while (1)
1718 tree a1, mv1, a2, mv2;
1719 if (args1 == NULL_TREE && args2 == NULL_TREE)
1720 return true;
1721 /* If one list is shorter than the other,
1722 they fail to match. */
1723 if (args1 == NULL_TREE || args2 == NULL_TREE)
1724 return 0;
1725 mv1 = a1 = TREE_VALUE (args1);
1726 mv2 = a2 = TREE_VALUE (args2);
1727 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1728 mv1 = (TYPE_ATOMIC (mv1)
1729 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1730 TYPE_QUAL_ATOMIC)
1731 : TYPE_MAIN_VARIANT (mv1));
1732 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1733 mv2 = (TYPE_ATOMIC (mv2)
1734 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1735 TYPE_QUAL_ATOMIC)
1736 : TYPE_MAIN_VARIANT (mv2));
1737 /* A null pointer instead of a type
1738 means there is supposed to be an argument
1739 but nothing is specified about what type it has.
1740 So match anything that self-promotes. */
1741 if ((a1 == NULL_TREE) != (a2 == NULL_TREE))
1742 data->different_types_p = true;
1743 if (a1 == NULL_TREE)
1745 if (c_type_promotes_to (a2) != a2)
1746 return 0;
1748 else if (a2 == NULL_TREE)
1750 if (c_type_promotes_to (a1) != a1)
1751 return 0;
1753 /* If one of the lists has an error marker, ignore this arg. */
1754 else if (TREE_CODE (a1) == ERROR_MARK
1755 || TREE_CODE (a2) == ERROR_MARK)
1757 else if (!comptypes_internal (mv1, mv2, data))
1759 data->different_types_p = true;
1760 /* Allow wait (union {union wait *u; int *i} *)
1761 and wait (union wait *) to be compatible. */
1762 if (TREE_CODE (a1) == UNION_TYPE
1763 && (TYPE_NAME (a1) == NULL_TREE
1764 || TYPE_TRANSPARENT_AGGR (a1))
1765 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1766 && tree_int_cst_equal (TYPE_SIZE (a1),
1767 TYPE_SIZE (a2)))
1769 tree memb;
1770 for (memb = TYPE_FIELDS (a1);
1771 memb; memb = DECL_CHAIN (memb))
1773 tree mv3 = TREE_TYPE (memb);
1774 if (mv3 && mv3 != error_mark_node
1775 && TREE_CODE (mv3) != ARRAY_TYPE)
1776 mv3 = (TYPE_ATOMIC (mv3)
1777 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1778 TYPE_QUAL_ATOMIC)
1779 : TYPE_MAIN_VARIANT (mv3));
1780 if (comptypes_internal (mv3, mv2, data))
1781 break;
1783 if (memb == NULL_TREE)
1784 return 0;
1786 else if (TREE_CODE (a2) == UNION_TYPE
1787 && (TYPE_NAME (a2) == NULL_TREE
1788 || TYPE_TRANSPARENT_AGGR (a2))
1789 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1790 && tree_int_cst_equal (TYPE_SIZE (a2),
1791 TYPE_SIZE (a1)))
1793 tree memb;
1794 for (memb = TYPE_FIELDS (a2);
1795 memb; memb = DECL_CHAIN (memb))
1797 tree mv3 = TREE_TYPE (memb);
1798 if (mv3 && mv3 != error_mark_node
1799 && TREE_CODE (mv3) != ARRAY_TYPE)
1800 mv3 = (TYPE_ATOMIC (mv3)
1801 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1802 TYPE_QUAL_ATOMIC)
1803 : TYPE_MAIN_VARIANT (mv3));
1804 if (comptypes_internal (mv3, mv1, data))
1805 break;
1807 if (memb == NULL_TREE)
1808 return 0;
1810 else
1811 return 0;
1814 args1 = TREE_CHAIN (args1);
1815 args2 = TREE_CHAIN (args2);
1819 /* Compute the size to increment a pointer by. When a function type or void
1820 type or incomplete type is passed, size_one_node is returned.
1821 This function does not emit any diagnostics; the caller is responsible
1822 for that. */
1824 static tree
1825 c_size_in_bytes (const_tree type)
1827 enum tree_code code = TREE_CODE (type);
1829 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1830 || !COMPLETE_TYPE_P (type))
1831 return size_one_node;
1833 /* Convert in case a char is more than one unit. */
1834 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1835 size_int (TYPE_PRECISION (char_type_node)
1836 / BITS_PER_UNIT));
1839 /* Return either DECL or its known constant value (if it has one). */
1841 tree
1842 decl_constant_value_1 (tree decl, bool in_init)
1844 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1845 TREE_CODE (decl) != PARM_DECL
1846 && !TREE_THIS_VOLATILE (decl)
1847 && TREE_READONLY (decl)
1848 && DECL_INITIAL (decl) != NULL_TREE
1849 && !error_operand_p (DECL_INITIAL (decl))
1850 /* This is invalid if initial value is not constant.
1851 If it has either a function call, a memory reference,
1852 or a variable, then re-evaluating it could give different results. */
1853 && TREE_CONSTANT (DECL_INITIAL (decl))
1854 /* Check for cases where this is sub-optimal, even though valid. */
1855 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1856 return DECL_INITIAL (decl);
1857 return decl;
1860 /* Return either DECL or its known constant value (if it has one).
1861 Like the above, but always return decl outside of functions. */
1863 tree
1864 decl_constant_value (tree decl)
1866 /* Don't change a variable array bound or initial value to a constant
1867 in a place where a variable is invalid. */
1868 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1871 /* Convert the array expression EXP to a pointer. */
1872 static tree
1873 array_to_pointer_conversion (location_t loc, tree exp)
1875 tree orig_exp = exp;
1876 tree type = TREE_TYPE (exp);
1877 tree adr;
1878 tree restype = TREE_TYPE (type);
1879 tree ptrtype;
1881 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1883 STRIP_TYPE_NOPS (exp);
1885 copy_warning (exp, orig_exp);
1887 ptrtype = build_pointer_type (restype);
1889 if (INDIRECT_REF_P (exp))
1890 return convert (ptrtype, TREE_OPERAND (exp, 0));
1892 /* In C++ array compound literals are temporary objects unless they are
1893 const or appear in namespace scope, so they are destroyed too soon
1894 to use them for much of anything (c++/53220). */
1895 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1897 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1898 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1899 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1900 "converting an array compound literal to a pointer "
1901 "leads to a dangling pointer in C++");
1904 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1905 return convert (ptrtype, adr);
1908 /* Convert the function expression EXP to a pointer. */
1909 static tree
1910 function_to_pointer_conversion (location_t loc, tree exp)
1912 tree orig_exp = exp;
1914 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1916 STRIP_TYPE_NOPS (exp);
1918 copy_warning (exp, orig_exp);
1920 return build_unary_op (loc, ADDR_EXPR, exp, false);
1923 /* Mark EXP as read, not just set, for set but not used -Wunused
1924 warning purposes. */
1926 void
1927 mark_exp_read (tree exp)
1929 switch (TREE_CODE (exp))
1931 case VAR_DECL:
1932 case PARM_DECL:
1933 DECL_READ_P (exp) = 1;
1934 break;
1935 case ARRAY_REF:
1936 case COMPONENT_REF:
1937 case MODIFY_EXPR:
1938 case REALPART_EXPR:
1939 case IMAGPART_EXPR:
1940 CASE_CONVERT:
1941 case ADDR_EXPR:
1942 case VIEW_CONVERT_EXPR:
1943 mark_exp_read (TREE_OPERAND (exp, 0));
1944 break;
1945 case COMPOUND_EXPR:
1946 /* Pattern match what build_atomic_assign produces with modifycode
1947 NOP_EXPR. */
1948 if (VAR_P (TREE_OPERAND (exp, 1))
1949 && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
1950 && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
1952 tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1953 tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
1954 if (TREE_CODE (t1) == TARGET_EXPR
1955 && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
1956 && TREE_CODE (t2) == CALL_EXPR)
1958 tree fndecl = get_callee_fndecl (t2);
1959 tree arg = NULL_TREE;
1960 if (fndecl
1961 && TREE_CODE (fndecl) == FUNCTION_DECL
1962 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1963 && call_expr_nargs (t2) >= 2)
1964 switch (DECL_FUNCTION_CODE (fndecl))
1966 case BUILT_IN_ATOMIC_STORE:
1967 arg = CALL_EXPR_ARG (t2, 1);
1968 break;
1969 case BUILT_IN_ATOMIC_STORE_1:
1970 case BUILT_IN_ATOMIC_STORE_2:
1971 case BUILT_IN_ATOMIC_STORE_4:
1972 case BUILT_IN_ATOMIC_STORE_8:
1973 case BUILT_IN_ATOMIC_STORE_16:
1974 arg = CALL_EXPR_ARG (t2, 0);
1975 break;
1976 default:
1977 break;
1979 if (arg)
1981 STRIP_NOPS (arg);
1982 if (TREE_CODE (arg) == ADDR_EXPR
1983 && DECL_P (TREE_OPERAND (arg, 0))
1984 && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
1985 mark_exp_read (TREE_OPERAND (arg, 0));
1989 /* FALLTHRU */
1990 case C_MAYBE_CONST_EXPR:
1991 mark_exp_read (TREE_OPERAND (exp, 1));
1992 break;
1993 case OMP_ARRAY_SECTION:
1994 mark_exp_read (TREE_OPERAND (exp, 0));
1995 if (TREE_OPERAND (exp, 1))
1996 mark_exp_read (TREE_OPERAND (exp, 1));
1997 if (TREE_OPERAND (exp, 2))
1998 mark_exp_read (TREE_OPERAND (exp, 2));
1999 break;
2000 default:
2001 break;
2005 /* Perform the default conversion of arrays and functions to pointers.
2006 Return the result of converting EXP. For any other expression, just
2007 return EXP.
2009 LOC is the location of the expression. */
2011 struct c_expr
2012 default_function_array_conversion (location_t loc, struct c_expr exp)
2014 tree orig_exp = exp.value;
2015 tree type = TREE_TYPE (exp.value);
2016 enum tree_code code = TREE_CODE (type);
2018 switch (code)
2020 case ARRAY_TYPE:
2022 bool not_lvalue = false;
2023 bool lvalue_array_p;
2025 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2026 || CONVERT_EXPR_P (exp.value))
2027 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2029 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2030 not_lvalue = true;
2031 exp.value = TREE_OPERAND (exp.value, 0);
2034 copy_warning (exp.value, orig_exp);
2036 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2037 if (!flag_isoc99 && !lvalue_array_p)
2039 /* Before C99, non-lvalue arrays do not decay to pointers.
2040 Normally, using such an array would be invalid; but it can
2041 be used correctly inside sizeof or as a statement expression.
2042 Thus, do not give an error here; an error will result later. */
2043 return exp;
2046 exp.value = array_to_pointer_conversion (loc, exp.value);
2048 break;
2049 case FUNCTION_TYPE:
2050 exp.value = function_to_pointer_conversion (loc, exp.value);
2051 break;
2052 default:
2053 break;
2056 return exp;
2059 struct c_expr
2060 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2062 mark_exp_read (exp.value);
2063 return default_function_array_conversion (loc, exp);
2066 /* Return whether EXPR should be treated as an atomic lvalue for the
2067 purposes of load and store handling. */
2069 static bool
2070 really_atomic_lvalue (tree expr)
2072 if (error_operand_p (expr))
2073 return false;
2074 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2075 return false;
2076 if (!lvalue_p (expr))
2077 return false;
2079 /* Ignore _Atomic on register variables, since their addresses can't
2080 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2081 sequences wouldn't work. Ignore _Atomic on structures containing
2082 bit-fields, since accessing elements of atomic structures or
2083 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2084 it's undefined at translation time or execution time, and the
2085 normal atomic sequences again wouldn't work. */
2086 while (handled_component_p (expr))
2088 if (TREE_CODE (expr) == COMPONENT_REF
2089 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2090 return false;
2091 expr = TREE_OPERAND (expr, 0);
2093 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2094 return false;
2095 return true;
2098 /* If EXPR is a named constant (C23) derived from a constexpr variable
2099 - that is, a reference to such a variable, or a member extracted by
2100 a sequence of structure and union (but not array) member accesses
2101 (where union member accesses must access the same member as
2102 initialized) - then return the corresponding initializer;
2103 otherwise, return NULL_TREE. */
2105 static tree
2106 maybe_get_constexpr_init (tree expr)
2108 tree decl = NULL_TREE;
2109 if (TREE_CODE (expr) == VAR_DECL)
2110 decl = expr;
2111 else if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2112 decl = COMPOUND_LITERAL_EXPR_DECL (expr);
2113 if (decl
2114 && C_DECL_DECLARED_CONSTEXPR (decl)
2115 && DECL_INITIAL (decl) != NULL_TREE
2116 && !error_operand_p (DECL_INITIAL (decl)))
2117 return DECL_INITIAL (decl);
2118 if (TREE_CODE (expr) != COMPONENT_REF)
2119 return NULL_TREE;
2120 tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0));
2121 if (inner == NULL_TREE)
2122 return NULL_TREE;
2123 while ((CONVERT_EXPR_P (inner) || TREE_CODE (inner) == NON_LVALUE_EXPR)
2124 && !error_operand_p (inner)
2125 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
2126 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (inner, 0)))))
2127 inner = TREE_OPERAND (inner, 0);
2128 if (TREE_CODE (inner) != CONSTRUCTOR)
2129 return NULL_TREE;
2130 tree field = TREE_OPERAND (expr, 1);
2131 unsigned HOST_WIDE_INT cidx;
2132 tree cfield, cvalue;
2133 bool have_other_init = false;
2134 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner), cidx, cfield, cvalue)
2136 if (cfield == field)
2137 return cvalue;
2138 have_other_init = true;
2140 if (TREE_CODE (TREE_TYPE (inner)) == UNION_TYPE
2141 && (have_other_init || field != TYPE_FIELDS (TREE_TYPE (inner))))
2142 return NULL_TREE;
2143 /* Return a default initializer. */
2144 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (expr)))
2145 return build_constructor (TREE_TYPE (expr), NULL);
2146 return build_zero_cst (TREE_TYPE (expr));
2149 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2150 including converting functions and arrays to pointers if CONVERT_P.
2151 If READ_P, also mark the expression as having been read. If
2152 FOR_INIT, constexpr expressions of structure and union type should
2153 be replaced by the corresponding CONSTRUCTOR; otherwise, only
2154 constexpr scalars (including elements of structures and unions) are
2155 replaced by their initializers. */
2157 struct c_expr
2158 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2159 bool convert_p, bool read_p, bool for_init)
2161 bool force_non_npc = false;
2162 if (read_p)
2163 mark_exp_read (exp.value);
2164 if (convert_p)
2165 exp = default_function_array_conversion (loc, exp);
2166 if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2167 exp.value = require_complete_type (loc, exp.value);
2168 if (for_init || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp.value)))
2170 tree init = maybe_get_constexpr_init (exp.value);
2171 if (init != NULL_TREE)
2173 /* A named constant of pointer type or type nullptr_t is not
2174 a null pointer constant even if the initializer is
2175 one. */
2176 if (TREE_CODE (init) == INTEGER_CST
2177 && !INTEGRAL_TYPE_P (TREE_TYPE (init))
2178 && integer_zerop (init))
2179 force_non_npc = true;
2180 exp.value = init;
2183 if (really_atomic_lvalue (exp.value))
2185 vec<tree, va_gc> *params;
2186 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2187 tree expr_type = TREE_TYPE (exp.value);
2188 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2189 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2191 gcc_assert (TYPE_ATOMIC (expr_type));
2193 /* Expansion of a generic atomic load may require an addition
2194 element, so allocate enough to prevent a resize. */
2195 vec_alloc (params, 4);
2197 /* Remove the qualifiers for the rest of the expressions and
2198 create the VAL temp variable to hold the RHS. */
2199 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2200 tmp = create_tmp_var_raw (nonatomic_type);
2201 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2202 TREE_ADDRESSABLE (tmp) = 1;
2203 /* Do not disable warnings for TMP even though it's artificial.
2204 -Winvalid-memory-model depends on it. */
2206 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2207 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2208 params->quick_push (expr_addr);
2209 params->quick_push (tmp_addr);
2210 params->quick_push (seq_cst);
2211 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2213 /* EXPR is always read. */
2214 mark_exp_read (exp.value);
2216 /* Return tmp which contains the value loaded. */
2217 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2218 NULL_TREE, NULL_TREE);
2220 if (convert_p && !error_operand_p (exp.value)
2221 && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2222 exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2223 if (force_non_npc)
2224 exp.value = build1 (NOP_EXPR, TREE_TYPE (exp.value), exp.value);
2227 tree false_value, true_value;
2228 if (convert_p && !error_operand_p (exp.value)
2229 && c_hardbool_type_attr (TREE_TYPE (exp.value),
2230 &false_value, &true_value))
2232 tree t = save_expr (exp.value);
2234 mark_exp_read (exp.value);
2236 tree trapfn = builtin_decl_explicit (BUILT_IN_TRAP);
2237 tree expr = build_call_expr_loc (loc, trapfn, 0);
2238 expr = build_compound_expr (loc, expr, boolean_true_node);
2239 expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2240 fold_build2_loc (loc, NE_EXPR,
2241 boolean_type_node,
2242 t, true_value),
2243 expr, boolean_true_node);
2244 expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2245 fold_build2_loc (loc, NE_EXPR,
2246 boolean_type_node,
2247 t, false_value),
2248 expr, boolean_false_node);
2250 exp.value = expr;
2254 return exp;
2257 /* EXP is an expression of integer type. Apply the integer promotions
2258 to it and return the promoted value. */
2260 tree
2261 perform_integral_promotions (tree exp)
2263 tree type = TREE_TYPE (exp);
2264 enum tree_code code = TREE_CODE (type);
2266 gcc_assert (INTEGRAL_TYPE_P (type));
2268 /* Convert enums to the result of applying the integer promotions to
2269 their underlying type. */
2270 if (code == ENUMERAL_TYPE)
2272 type = ENUM_UNDERLYING_TYPE (type);
2273 if (c_promoting_integer_type_p (type))
2275 if (TYPE_UNSIGNED (type)
2276 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2277 type = unsigned_type_node;
2278 else
2279 type = integer_type_node;
2282 return convert (type, exp);
2285 /* ??? This should no longer be needed now bit-fields have their
2286 proper types. */
2287 if (TREE_CODE (exp) == COMPONENT_REF
2288 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
2290 if (TREE_CODE (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)))
2291 == BITINT_TYPE)
2292 return convert (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)), exp);
2293 /* If it's thinner than an int, promote it like a
2294 c_promoting_integer_type_p, otherwise leave it alone. */
2295 if (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2296 TYPE_PRECISION (integer_type_node)) < 0)
2297 return convert (integer_type_node, exp);
2300 if (c_promoting_integer_type_p (type))
2302 /* Preserve unsignedness if not really getting any wider. */
2303 if (TYPE_UNSIGNED (type)
2304 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2305 return convert (unsigned_type_node, exp);
2307 return convert (integer_type_node, exp);
2310 return exp;
2314 /* Perform default promotions for C data used in expressions.
2315 Enumeral types or short or char are converted to int.
2316 In addition, manifest constants symbols are replaced by their values. */
2318 tree
2319 default_conversion (tree exp)
2321 tree orig_exp;
2322 tree type = TREE_TYPE (exp);
2323 enum tree_code code = TREE_CODE (type);
2324 tree promoted_type;
2326 mark_exp_read (exp);
2328 /* Functions and arrays have been converted during parsing. */
2329 gcc_assert (code != FUNCTION_TYPE);
2330 if (code == ARRAY_TYPE)
2331 return exp;
2333 /* Constants can be used directly unless they're not loadable. */
2334 if (TREE_CODE (exp) == CONST_DECL)
2335 exp = DECL_INITIAL (exp);
2337 /* Strip no-op conversions. */
2338 orig_exp = exp;
2339 STRIP_TYPE_NOPS (exp);
2341 copy_warning (exp, orig_exp);
2343 if (code == VOID_TYPE)
2345 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2346 "void value not ignored as it ought to be");
2347 return error_mark_node;
2350 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2351 if (exp == error_mark_node)
2352 return error_mark_node;
2354 promoted_type = targetm.promoted_type (type);
2355 if (promoted_type)
2356 return convert (promoted_type, exp);
2358 if (INTEGRAL_TYPE_P (type))
2359 return perform_integral_promotions (exp);
2361 return exp;
2364 /* Look up COMPONENT in a structure or union TYPE.
2366 If the component name is not found, returns NULL_TREE. Otherwise,
2367 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2368 stepping down the chain to the component, which is in the last
2369 TREE_VALUE of the list. Normally the list is of length one, but if
2370 the component is embedded within (nested) anonymous structures or
2371 unions, the list steps down the chain to the component. */
2373 static tree
2374 lookup_field (tree type, tree component)
2376 tree field;
2378 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2379 to the field elements. Use a binary search on this array to quickly
2380 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2381 will always be set for structures which have many elements.
2383 Duplicate field checking replaces duplicates with NULL_TREE so
2384 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2385 case just iterate using DECL_CHAIN. */
2387 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2388 && !seen_error ())
2390 int bot, top, half;
2391 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2393 field = TYPE_FIELDS (type);
2394 bot = 0;
2395 top = TYPE_LANG_SPECIFIC (type)->s->len;
2396 while (top - bot > 1)
2398 half = (top - bot + 1) >> 1;
2399 field = field_array[bot+half];
2401 if (DECL_NAME (field) == NULL_TREE)
2403 /* Step through all anon unions in linear fashion. */
2404 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2406 field = field_array[bot++];
2407 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2409 tree anon = lookup_field (TREE_TYPE (field), component);
2411 if (anon)
2412 return tree_cons (NULL_TREE, field, anon);
2414 /* The Plan 9 compiler permits referring
2415 directly to an anonymous struct/union field
2416 using a typedef name. */
2417 if (flag_plan9_extensions
2418 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2419 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2420 == TYPE_DECL)
2421 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2422 == component))
2423 break;
2427 /* Entire record is only anon unions. */
2428 if (bot > top)
2429 return NULL_TREE;
2431 /* Restart the binary search, with new lower bound. */
2432 continue;
2435 if (DECL_NAME (field) == component)
2436 break;
2437 if (DECL_NAME (field) < component)
2438 bot += half;
2439 else
2440 top = bot + half;
2443 if (DECL_NAME (field_array[bot]) == component)
2444 field = field_array[bot];
2445 else if (DECL_NAME (field) != component)
2446 return NULL_TREE;
2448 else
2450 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2452 if (DECL_NAME (field) == NULL_TREE
2453 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2455 tree anon = lookup_field (TREE_TYPE (field), component);
2457 if (anon)
2458 return tree_cons (NULL_TREE, field, anon);
2460 /* The Plan 9 compiler permits referring directly to an
2461 anonymous struct/union field using a typedef
2462 name. */
2463 if (flag_plan9_extensions
2464 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2465 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2466 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2467 == component))
2468 break;
2471 if (DECL_NAME (field) == component)
2472 break;
2475 if (field == NULL_TREE)
2476 return NULL_TREE;
2479 return tree_cons (NULL_TREE, field, NULL_TREE);
2482 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2484 static void
2485 lookup_field_fuzzy_find_candidates (tree type, tree component,
2486 vec<tree> *candidates)
2488 tree field;
2489 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2491 if (DECL_NAME (field) == NULL_TREE
2492 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2493 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2494 candidates);
2496 if (DECL_NAME (field))
2497 candidates->safe_push (DECL_NAME (field));
2501 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2502 rather than returning a TREE_LIST for an exact match. */
2504 static tree
2505 lookup_field_fuzzy (tree type, tree component)
2507 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2509 /* First, gather a list of candidates. */
2510 auto_vec <tree> candidates;
2512 lookup_field_fuzzy_find_candidates (type, component,
2513 &candidates);
2515 return find_closest_identifier (component, &candidates);
2518 /* Support function for build_component_ref's error-handling.
2520 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2521 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2523 static bool
2524 should_suggest_deref_p (tree datum_type)
2526 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2527 allows "." for ptrs; we could be handling a failed attempt
2528 to access a property. */
2529 if (c_dialect_objc ())
2530 return false;
2532 /* Only suggest it for pointers... */
2533 if (TREE_CODE (datum_type) != POINTER_TYPE)
2534 return false;
2536 /* ...to structs/unions. */
2537 tree underlying_type = TREE_TYPE (datum_type);
2538 enum tree_code code = TREE_CODE (underlying_type);
2539 if (code == RECORD_TYPE || code == UNION_TYPE)
2540 return true;
2541 else
2542 return false;
2545 /* Make an expression to refer to the COMPONENT field of structure or
2546 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2547 location of the COMPONENT_REF. COMPONENT_LOC is the location
2548 of COMPONENT. ARROW_LOC is the location of the first -> operand if
2549 it is from -> operator. */
2551 tree
2552 build_component_ref (location_t loc, tree datum, tree component,
2553 location_t component_loc, location_t arrow_loc)
2555 tree type = TREE_TYPE (datum);
2556 enum tree_code code = TREE_CODE (type);
2557 tree field = NULL;
2558 tree ref;
2559 bool datum_lvalue = lvalue_p (datum);
2561 if (!objc_is_public (datum, component))
2562 return error_mark_node;
2564 /* Detect Objective-C property syntax object.property. */
2565 if (c_dialect_objc ()
2566 && (ref = objc_maybe_build_component_ref (datum, component)))
2567 return ref;
2569 /* See if there is a field or component with name COMPONENT. */
2571 if (code == RECORD_TYPE || code == UNION_TYPE)
2573 if (!COMPLETE_TYPE_P (type))
2575 c_incomplete_type_error (loc, NULL_TREE, type);
2576 return error_mark_node;
2579 field = lookup_field (type, component);
2581 if (!field)
2583 tree guessed_id = lookup_field_fuzzy (type, component);
2584 if (guessed_id)
2586 /* Attempt to provide a fixit replacement hint, if
2587 we have a valid range for the component. */
2588 location_t reported_loc
2589 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2590 gcc_rich_location rich_loc (reported_loc);
2591 if (component_loc != UNKNOWN_LOCATION)
2592 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2593 error_at (&rich_loc,
2594 "%qT has no member named %qE; did you mean %qE?",
2595 type, component, guessed_id);
2597 else
2598 error_at (loc, "%qT has no member named %qE", type, component);
2599 return error_mark_node;
2602 /* Accessing elements of atomic structures or unions is undefined
2603 behavior (C11 6.5.2.3#5). */
2604 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2606 if (code == RECORD_TYPE)
2607 warning_at (loc, 0, "accessing a member %qE of an atomic "
2608 "structure %qE", component, datum);
2609 else
2610 warning_at (loc, 0, "accessing a member %qE of an atomic "
2611 "union %qE", component, datum);
2614 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2615 This might be better solved in future the way the C++ front
2616 end does it - by giving the anonymous entities each a
2617 separate name and type, and then have build_component_ref
2618 recursively call itself. We can't do that here. */
2621 tree subdatum = TREE_VALUE (field);
2622 int quals;
2623 tree subtype;
2624 bool use_datum_quals;
2626 if (TREE_TYPE (subdatum) == error_mark_node)
2627 return error_mark_node;
2629 /* If this is an rvalue, it does not have qualifiers in C
2630 standard terms and we must avoid propagating such
2631 qualifiers down to a non-lvalue array that is then
2632 converted to a pointer. */
2633 use_datum_quals = (datum_lvalue
2634 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2636 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2637 if (use_datum_quals)
2638 quals |= TYPE_QUALS (TREE_TYPE (datum));
2639 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2641 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2642 NULL_TREE);
2643 SET_EXPR_LOCATION (ref, loc);
2644 if (TREE_READONLY (subdatum)
2645 || (use_datum_quals && TREE_READONLY (datum)))
2646 TREE_READONLY (ref) = 1;
2647 if (TREE_THIS_VOLATILE (subdatum)
2648 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2649 TREE_THIS_VOLATILE (ref) = 1;
2651 if (TREE_UNAVAILABLE (subdatum))
2652 error_unavailable_use (subdatum, NULL_TREE);
2653 else if (TREE_DEPRECATED (subdatum))
2654 warn_deprecated_use (subdatum, NULL_TREE);
2656 datum = ref;
2658 field = TREE_CHAIN (field);
2660 while (field);
2662 return ref;
2664 else if (should_suggest_deref_p (type))
2666 /* Special-case the error message for "ptr.field" for the case
2667 where the user has confused "." vs "->". */
2668 rich_location richloc (line_table, loc);
2669 if (INDIRECT_REF_P (datum) && arrow_loc != UNKNOWN_LOCATION)
2671 richloc.add_fixit_insert_before (arrow_loc, "(*");
2672 richloc.add_fixit_insert_after (arrow_loc, ")");
2673 error_at (&richloc,
2674 "%qE is a pointer to pointer; did you mean to dereference "
2675 "it before applying %<->%> to it?",
2676 TREE_OPERAND (datum, 0));
2678 else
2680 /* "loc" should be the "." token. */
2681 richloc.add_fixit_replace ("->");
2682 error_at (&richloc,
2683 "%qE is a pointer; did you mean to use %<->%>?",
2684 datum);
2686 return error_mark_node;
2688 else if (code != ERROR_MARK)
2689 error_at (loc,
2690 "request for member %qE in something not a structure or union",
2691 component);
2693 return error_mark_node;
2696 /* Given an expression PTR for a pointer, return an expression
2697 for the value pointed to.
2698 ERRORSTRING is the name of the operator to appear in error messages.
2700 LOC is the location to use for the generated tree. */
2702 tree
2703 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2705 tree pointer = default_conversion (ptr);
2706 tree type = TREE_TYPE (pointer);
2707 tree ref;
2709 if (TREE_CODE (type) == POINTER_TYPE)
2711 if (CONVERT_EXPR_P (pointer)
2712 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2714 /* If a warning is issued, mark it to avoid duplicates from
2715 the backend. This only needs to be done at
2716 warn_strict_aliasing > 2. */
2717 if (warn_strict_aliasing > 2)
2718 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2719 type, TREE_OPERAND (pointer, 0)))
2720 suppress_warning (pointer, OPT_Wstrict_aliasing_);
2723 if (TREE_CODE (pointer) == ADDR_EXPR
2724 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2725 == TREE_TYPE (type)))
2727 ref = TREE_OPERAND (pointer, 0);
2728 protected_set_expr_location (ref, loc);
2729 return ref;
2731 else
2733 tree t = TREE_TYPE (type);
2735 ref = build1 (INDIRECT_REF, t, pointer);
2737 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2738 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2740 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2741 so that we get the proper error message if the result is used
2742 to assign to. Also, &* is supposed to be a no-op.
2743 And ANSI C seems to specify that the type of the result
2744 should be the const type. */
2745 /* A de-reference of a pointer to const is not a const. It is valid
2746 to change it via some other pointer. */
2747 TREE_READONLY (ref) = TYPE_READONLY (t);
2748 TREE_SIDE_EFFECTS (ref)
2749 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2750 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2751 protected_set_expr_location (ref, loc);
2752 return ref;
2755 else if (TREE_CODE (pointer) != ERROR_MARK)
2756 invalid_indirection_error (loc, type, errstring);
2758 return error_mark_node;
2761 /* This handles expressions of the form "a[i]", which denotes
2762 an array reference.
2764 This is logically equivalent in C to *(a+i), but we may do it differently.
2765 If A is a variable or a member, we generate a primitive ARRAY_REF.
2766 This avoids forcing the array out of registers, and can work on
2767 arrays that are not lvalues (for example, members of structures returned
2768 by functions).
2770 For vector types, allow vector[i] but not i[vector], and create
2771 *(((type*)&vectortype) + i) for the expression.
2773 LOC is the location to use for the returned expression. */
2775 tree
2776 build_array_ref (location_t loc, tree array, tree index)
2778 tree ret;
2779 bool swapped = false;
2780 if (TREE_TYPE (array) == error_mark_node
2781 || TREE_TYPE (index) == error_mark_node)
2782 return error_mark_node;
2784 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2785 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2786 /* Allow vector[index] but not index[vector]. */
2787 && !gnu_vector_type_p (TREE_TYPE (array)))
2789 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2790 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2792 error_at (loc,
2793 "subscripted value is neither array nor pointer nor vector");
2795 return error_mark_node;
2797 std::swap (array, index);
2798 swapped = true;
2801 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2803 error_at (loc, "array subscript is not an integer");
2804 return error_mark_node;
2807 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2809 error_at (loc, "subscripted value is pointer to function");
2810 return error_mark_node;
2813 /* ??? Existing practice has been to warn only when the char
2814 index is syntactically the index, not for char[array]. */
2815 if (!swapped)
2816 warn_array_subscript_with_type_char (loc, index);
2818 /* Apply default promotions *after* noticing character types. */
2819 index = default_conversion (index);
2820 if (index == error_mark_node)
2821 return error_mark_node;
2823 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
2824 || TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE);
2826 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2827 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2829 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2831 tree rval, type;
2833 /* An array that is indexed by a non-constant
2834 cannot be stored in a register; we must be able to do
2835 address arithmetic on its address.
2836 Likewise an array of elements of variable size. */
2837 if (TREE_CODE (index) != INTEGER_CST
2838 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2839 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2841 if (!c_mark_addressable (array, true))
2842 return error_mark_node;
2844 /* An array that is indexed by a constant value which is not within
2845 the array bounds cannot be stored in a register either; because we
2846 would get a crash in store_bit_field/extract_bit_field when trying
2847 to access a non-existent part of the register. */
2848 if (TREE_CODE (index) == INTEGER_CST
2849 && TYPE_DOMAIN (TREE_TYPE (array))
2850 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2852 if (!c_mark_addressable (array))
2853 return error_mark_node;
2856 if ((pedantic || warn_c90_c99_compat)
2857 && ! was_vector)
2859 tree foo = array;
2860 while (TREE_CODE (foo) == COMPONENT_REF)
2861 foo = TREE_OPERAND (foo, 0);
2862 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2863 pedwarn (loc, OPT_Wpedantic,
2864 "ISO C forbids subscripting %<register%> array");
2865 else if (!lvalue_p (foo))
2866 pedwarn_c90 (loc, OPT_Wpedantic,
2867 "ISO C90 forbids subscripting non-lvalue "
2868 "array");
2871 if (TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE
2872 && TYPE_PRECISION (TREE_TYPE (index)) > TYPE_PRECISION (sizetype))
2873 index = fold_convert (sizetype, index);
2875 type = TREE_TYPE (TREE_TYPE (array));
2876 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2877 /* Array ref is const/volatile if the array elements are
2878 or if the array is. */
2879 TREE_READONLY (rval)
2880 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2881 | TREE_READONLY (array));
2882 TREE_SIDE_EFFECTS (rval)
2883 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2884 | TREE_SIDE_EFFECTS (array));
2885 TREE_THIS_VOLATILE (rval)
2886 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2887 /* This was added by rms on 16 Nov 91.
2888 It fixes vol struct foo *a; a->elts[1]
2889 in an inline function.
2890 Hope it doesn't break something else. */
2891 | TREE_THIS_VOLATILE (array));
2892 ret = require_complete_type (loc, rval);
2893 protected_set_expr_location (ret, loc);
2894 if (non_lvalue)
2895 ret = non_lvalue_loc (loc, ret);
2896 return ret;
2898 else
2900 tree ar = default_conversion (array);
2902 if (ar == error_mark_node)
2903 return ar;
2905 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2906 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2908 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2909 index, false),
2910 RO_ARRAY_INDEXING);
2911 if (non_lvalue)
2912 ret = non_lvalue_loc (loc, ret);
2913 return ret;
2917 /* Build an OpenMP array section reference, creating an exact type for the
2918 resulting expression based on the element type and bounds if possible. If
2919 we have variable bounds, create an incomplete array type for the result
2920 instead. */
2922 tree
2923 build_omp_array_section (location_t loc, tree array, tree index, tree length)
2925 tree type = TREE_TYPE (array);
2926 gcc_assert (type);
2928 tree sectype, eltype = TREE_TYPE (type);
2930 /* It's not an array or pointer type. Just reuse the type of the original
2931 expression as the type of the array section (an error will be raised
2932 anyway, later). */
2933 if (eltype == NULL_TREE || error_operand_p (eltype))
2934 sectype = TREE_TYPE (array);
2935 else
2937 tree idxtype = NULL_TREE;
2939 if (index != NULL_TREE
2940 && length != NULL_TREE
2941 && INTEGRAL_TYPE_P (TREE_TYPE (index))
2942 && INTEGRAL_TYPE_P (TREE_TYPE (length)))
2944 tree low = fold_convert (sizetype, index);
2945 tree high = fold_convert (sizetype, length);
2946 high = size_binop (PLUS_EXPR, low, high);
2947 high = size_binop (MINUS_EXPR, high, size_one_node);
2948 idxtype = build_range_type (sizetype, low, high);
2950 else if ((index == NULL_TREE || integer_zerop (index))
2951 && length != NULL_TREE
2952 && INTEGRAL_TYPE_P (TREE_TYPE (length)))
2953 idxtype = build_index_type (length);
2955 gcc_assert (!error_operand_p (idxtype));
2957 sectype = build_array_type (eltype, idxtype);
2960 return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array, index, length);
2964 /* Build an external reference to identifier ID. FUN indicates
2965 whether this will be used for a function call. LOC is the source
2966 location of the identifier. This sets *TYPE to the type of the
2967 identifier, which is not the same as the type of the returned value
2968 for CONST_DECLs defined as enum constants. If the type of the
2969 identifier is not available, *TYPE is set to NULL. */
2970 tree
2971 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2973 tree ref;
2974 tree decl = lookup_name (id);
2976 /* In Objective-C, an instance variable (ivar) may be preferred to
2977 whatever lookup_name() found. */
2978 decl = objc_lookup_ivar (decl, id);
2980 *type = NULL;
2981 if (decl && decl != error_mark_node)
2983 ref = decl;
2984 *type = TREE_TYPE (ref);
2985 if (DECL_P (decl) && C_DECL_UNDERSPECIFIED (decl))
2986 error_at (loc, "underspecified %qD referenced in its initializer",
2987 decl);
2989 else if (fun)
2990 /* Implicit function declaration. */
2991 ref = implicitly_declare (loc, id);
2992 else if (decl == error_mark_node)
2993 /* Don't complain about something that's already been
2994 complained about. */
2995 return error_mark_node;
2996 else
2998 undeclared_variable (loc, id);
2999 return error_mark_node;
3002 /* For an OpenMP map clause, we can get better diagnostics for decls with
3003 unmappable types if we return the decl with an error_mark_node type,
3004 rather than returning error_mark_node for the decl itself. */
3005 if (TREE_TYPE (ref) == error_mark_node
3006 && !c_omp_array_section_p)
3007 return error_mark_node;
3009 if (TREE_UNAVAILABLE (ref))
3010 error_unavailable_use (ref, NULL_TREE);
3011 else if (TREE_DEPRECATED (ref))
3012 warn_deprecated_use (ref, NULL_TREE);
3014 /* Recursive call does not count as usage. */
3015 if (ref != current_function_decl)
3017 TREE_USED (ref) = 1;
3020 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
3022 if (!in_sizeof && !in_typeof)
3023 C_DECL_USED (ref) = 1;
3024 else if (DECL_INITIAL (ref) == NULL_TREE
3025 && DECL_EXTERNAL (ref)
3026 && !TREE_PUBLIC (ref))
3027 record_maybe_used_decl (ref);
3030 if (TREE_CODE (ref) == CONST_DECL)
3032 used_types_insert (TREE_TYPE (ref));
3034 if (warn_cxx_compat
3035 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
3036 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
3038 warning_at (loc, OPT_Wc___compat,
3039 ("enum constant defined in struct or union "
3040 "is not visible in C++"));
3041 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
3044 ref = DECL_INITIAL (ref);
3045 TREE_CONSTANT (ref) = 1;
3047 else if (current_function_decl != NULL_TREE
3048 && !DECL_FILE_SCOPE_P (current_function_decl)
3049 && (VAR_OR_FUNCTION_DECL_P (ref)
3050 || TREE_CODE (ref) == PARM_DECL))
3052 tree context = decl_function_context (ref);
3054 if (context != NULL_TREE && context != current_function_decl)
3055 DECL_NONLOCAL (ref) = 1;
3057 /* C99 6.7.4p3: An inline definition of a function with external
3058 linkage ... shall not contain a reference to an identifier with
3059 internal linkage. */
3060 else if (current_function_decl != NULL_TREE
3061 && DECL_DECLARED_INLINE_P (current_function_decl)
3062 && DECL_EXTERNAL (current_function_decl)
3063 && VAR_OR_FUNCTION_DECL_P (ref)
3064 && (!VAR_P (ref) || TREE_STATIC (ref))
3065 && ! TREE_PUBLIC (ref)
3066 && DECL_CONTEXT (ref) != current_function_decl)
3067 record_inline_static (loc, current_function_decl, ref,
3068 csi_internal);
3070 return ref;
3073 /* Record details of decls possibly used inside sizeof or typeof. */
3074 struct maybe_used_decl
3076 /* The decl. */
3077 tree decl;
3078 /* The level seen at (in_sizeof + in_typeof). */
3079 int level;
3080 /* The next one at this level or above, or NULL. */
3081 struct maybe_used_decl *next;
3084 static struct maybe_used_decl *maybe_used_decls;
3086 /* Record that DECL, an undefined static function reference seen
3087 inside sizeof or typeof, might be used if the operand of sizeof is
3088 a VLA type or the operand of typeof is a variably modified
3089 type. */
3091 static void
3092 record_maybe_used_decl (tree decl)
3094 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
3095 t->decl = decl;
3096 t->level = in_sizeof + in_typeof;
3097 t->next = maybe_used_decls;
3098 maybe_used_decls = t;
3101 /* Pop the stack of decls possibly used inside sizeof or typeof. If
3102 USED is false, just discard them. If it is true, mark them used
3103 (if no longer inside sizeof or typeof) or move them to the next
3104 level up (if still inside sizeof or typeof). */
3106 void
3107 pop_maybe_used (bool used)
3109 struct maybe_used_decl *p = maybe_used_decls;
3110 int cur_level = in_sizeof + in_typeof;
3111 while (p && p->level > cur_level)
3113 if (used)
3115 if (cur_level == 0)
3116 C_DECL_USED (p->decl) = 1;
3117 else
3118 p->level = cur_level;
3120 p = p->next;
3122 if (!used || cur_level == 0)
3123 maybe_used_decls = p;
3126 /* Return the result of sizeof applied to EXPR. */
3128 struct c_expr
3129 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
3131 struct c_expr ret;
3132 if (expr.value == error_mark_node)
3134 ret.value = error_mark_node;
3135 ret.original_code = ERROR_MARK;
3136 ret.original_type = NULL;
3137 ret.m_decimal = 0;
3138 pop_maybe_used (false);
3140 else
3142 bool expr_const_operands = true;
3144 if (TREE_CODE (expr.value) == PARM_DECL
3145 && C_ARRAY_PARAMETER (expr.value))
3147 auto_diagnostic_group d;
3148 if (warning_at (loc, OPT_Wsizeof_array_argument,
3149 "%<sizeof%> on array function parameter %qE will "
3150 "return size of %qT", expr.value,
3151 TREE_TYPE (expr.value)))
3152 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
3154 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
3155 &expr_const_operands);
3156 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
3157 c_last_sizeof_arg = expr.value;
3158 c_last_sizeof_loc = loc;
3159 ret.original_code = SIZEOF_EXPR;
3160 ret.original_type = NULL;
3161 ret.m_decimal = 0;
3162 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
3164 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
3165 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3166 folded_expr, ret.value);
3167 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3168 SET_EXPR_LOCATION (ret.value, loc);
3170 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3172 return ret;
3175 /* Return the result of sizeof applied to T, a structure for the type
3176 name passed to sizeof (rather than the type itself). LOC is the
3177 location of the original expression. */
3179 struct c_expr
3180 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3182 tree type;
3183 struct c_expr ret;
3184 tree type_expr = NULL_TREE;
3185 bool type_expr_const = true;
3186 type = groktypename (t, &type_expr, &type_expr_const);
3187 ret.value = c_sizeof (loc, type);
3188 c_last_sizeof_arg = type;
3189 c_last_sizeof_loc = loc;
3190 ret.original_code = SIZEOF_EXPR;
3191 ret.original_type = NULL;
3192 ret.m_decimal = 0;
3193 if (type == error_mark_node)
3195 ret.value = error_mark_node;
3196 ret.original_code = ERROR_MARK;
3198 else
3199 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3200 && C_TYPE_VARIABLE_SIZE (type))
3202 /* If the type is a [*] array, it is a VLA but is represented as
3203 having a size of zero. In such a case we must ensure that
3204 the result of sizeof does not get folded to a constant by
3205 c_fully_fold, because if the size is evaluated the result is
3206 not constant and so constraints on zero or negative size
3207 arrays must not be applied when this sizeof call is inside
3208 another array declarator. */
3209 if (!type_expr)
3210 type_expr = integer_zero_node;
3211 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3212 type_expr, ret.value);
3213 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3215 pop_maybe_used (type != error_mark_node
3216 ? C_TYPE_VARIABLE_SIZE (type) : false);
3217 return ret;
3220 /* Build a function call to function FUNCTION with parameters PARAMS.
3221 The function call is at LOC.
3222 PARAMS is a list--a chain of TREE_LIST nodes--in which the
3223 TREE_VALUE of each node is a parameter-expression.
3224 FUNCTION's data type may be a function type or a pointer-to-function. */
3226 tree
3227 build_function_call (location_t loc, tree function, tree params)
3229 vec<tree, va_gc> *v;
3230 tree ret;
3232 vec_alloc (v, list_length (params));
3233 for (; params; params = TREE_CHAIN (params))
3234 v->quick_push (TREE_VALUE (params));
3235 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3236 vec_free (v);
3237 return ret;
3240 /* Give a note about the location of the declaration of DECL. */
3242 static void
3243 inform_declaration (tree decl)
3245 if (decl && (TREE_CODE (decl) != FUNCTION_DECL
3246 || !DECL_IS_UNDECLARED_BUILTIN (decl)))
3247 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3250 /* Build a function call to function FUNCTION with parameters PARAMS.
3251 If FUNCTION is the result of resolving an overloaded target built-in,
3252 ORIG_FUNDECL is the original function decl, otherwise it is null.
3253 ORIGTYPES, if not NULL, is a vector of types; each element is
3254 either NULL or the original type of the corresponding element in
3255 PARAMS. The original type may differ from TREE_TYPE of the
3256 parameter for enums. FUNCTION's data type may be a function type
3257 or pointer-to-function. This function changes the elements of
3258 PARAMS. */
3260 tree
3261 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3262 tree function, vec<tree, va_gc> *params,
3263 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3265 tree fntype, fundecl = NULL_TREE;
3266 tree name = NULL_TREE, result;
3267 tree tem;
3268 int nargs;
3269 tree *argarray;
3272 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3273 STRIP_TYPE_NOPS (function);
3275 /* Convert anything with function type to a pointer-to-function. */
3276 if (TREE_CODE (function) == FUNCTION_DECL)
3278 name = DECL_NAME (function);
3280 if (flag_tm)
3281 tm_malloc_replacement (function);
3282 fundecl = function;
3283 if (!orig_fundecl)
3284 orig_fundecl = fundecl;
3285 /* Atomic functions have type checking/casting already done. They are
3286 often rewritten and don't match the original parameter list. */
3287 if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
3288 origtypes = NULL;
3290 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3291 function = function_to_pointer_conversion (loc, function);
3293 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3294 expressions, like those used for ObjC messenger dispatches. */
3295 if (params && !params->is_empty ())
3296 function = objc_rewrite_function_call (function, (*params)[0]);
3298 function = c_fully_fold (function, false, NULL);
3300 fntype = TREE_TYPE (function);
3302 if (TREE_CODE (fntype) == ERROR_MARK)
3303 return error_mark_node;
3305 if (!(TREE_CODE (fntype) == POINTER_TYPE
3306 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3308 if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
3309 error_at (loc,
3310 "called object %qE is not a function or function pointer",
3311 function);
3312 else if (DECL_P (function))
3314 error_at (loc,
3315 "called object %qD is not a function or function pointer",
3316 function);
3317 inform_declaration (function);
3319 else
3320 error_at (loc,
3321 "called object is not a function or function pointer");
3322 return error_mark_node;
3325 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3326 current_function_returns_abnormally = 1;
3328 /* fntype now gets the type of function pointed to. */
3329 fntype = TREE_TYPE (fntype);
3330 tree return_type = TREE_TYPE (fntype);
3332 /* Convert the parameters to the types declared in the
3333 function prototype, or apply default promotions. */
3335 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3336 origtypes, function, fundecl);
3337 if (nargs < 0)
3338 return error_mark_node;
3340 /* Check that the function is called through a compatible prototype.
3341 If it is not, warn. */
3342 if (CONVERT_EXPR_P (function)
3343 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3344 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3345 && !comptypes (fntype, TREE_TYPE (tem)))
3347 /* This situation leads to run-time undefined behavior. We can't,
3348 therefore, simply error unless we can prove that all possible
3349 executions of the program must execute the code. */
3350 warning_at (loc, 0, "function called through a non-compatible type");
3352 if (VOID_TYPE_P (return_type)
3353 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3354 pedwarn (loc, 0,
3355 "function with qualified void return type called");
3358 argarray = vec_safe_address (params);
3360 /* Check that arguments to builtin functions match the expectations. */
3361 if (fundecl
3362 && fndecl_built_in_p (fundecl)
3363 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3364 orig_fundecl, nargs, argarray))
3365 return error_mark_node;
3367 /* Check that the arguments to the function are valid. */
3368 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3369 nargs, argarray, &arg_loc);
3371 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
3372 && !VOID_TYPE_P (return_type))
3373 return_type = c_build_qualified_type (return_type, TYPE_UNQUALIFIED);
3374 if (name != NULL_TREE
3375 && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
3377 if (require_constant_value)
3378 result
3379 = fold_build_call_array_initializer_loc (loc, return_type,
3380 function, nargs, argarray);
3381 else
3382 result = fold_build_call_array_loc (loc, return_type,
3383 function, nargs, argarray);
3384 if (TREE_CODE (result) == NOP_EXPR
3385 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3386 STRIP_TYPE_NOPS (result);
3388 else
3389 result = build_call_array_loc (loc, return_type,
3390 function, nargs, argarray);
3391 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3392 later. */
3393 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3394 suppress_warning (result, OPT_Wnonnull);
3396 /* In this improbable scenario, a nested function returns a VM type.
3397 Create a TARGET_EXPR so that the call always has a LHS, much as
3398 what the C++ FE does for functions returning non-PODs. */
3399 if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (fntype)))
3401 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3402 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3403 NULL_TREE, NULL_TREE);
3406 if (VOID_TYPE_P (TREE_TYPE (result)))
3408 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3409 pedwarn (loc, 0,
3410 "function with qualified void return type called");
3411 return result;
3413 return require_complete_type (loc, result);
3416 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3418 tree
3419 c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
3420 tree function, vec<tree, va_gc> *params,
3421 vec<tree, va_gc> *origtypes)
3423 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3424 STRIP_TYPE_NOPS (function);
3426 /* Convert anything with function type to a pointer-to-function. */
3427 if (TREE_CODE (function) == FUNCTION_DECL)
3429 /* Implement type-directed function overloading for builtins.
3430 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3431 handle all the type checking. The result is a complete expression
3432 that implements this function call. */
3433 tree tem = resolve_overloaded_builtin (loc, function, params);
3434 if (tem)
3435 return tem;
3437 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3440 /* Helper for convert_arguments called to convert the VALue of argument
3441 number ARGNUM from ORIGTYPE to the corresponding parameter number
3442 PARMNUM and TYPE.
3443 PLOC is the location where the conversion is being performed.
3444 FUNCTION and FUNDECL are the same as in convert_arguments.
3445 VALTYPE is the original type of VAL before the conversion and,
3446 for EXCESS_PRECISION_EXPR, the operand of the expression.
3447 NPC is true if VAL represents the null pointer constant (VAL itself
3448 will have been folded to an integer constant).
3449 RNAME is the same as FUNCTION except in Objective C when it's
3450 the function selector.
3451 EXCESS_PRECISION is true when VAL was originally represented
3452 as EXCESS_PRECISION_EXPR.
3453 WARNOPT is the same as in convert_for_assignment. */
3455 static tree
3456 convert_argument (location_t ploc, tree function, tree fundecl,
3457 tree type, tree origtype, tree val, tree valtype,
3458 bool npc, tree rname, int parmnum, int argnum,
3459 bool excess_precision, int warnopt)
3461 /* Formal parm type is specified by a function prototype. */
3463 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3465 error_at (ploc, "type of formal parameter %d is incomplete",
3466 parmnum + 1);
3467 return error_mark_node;
3470 /* Optionally warn about conversions that differ from the default
3471 conversions. */
3472 if (warn_traditional_conversion || warn_traditional)
3474 if (INTEGRAL_TYPE_P (type)
3475 && SCALAR_FLOAT_TYPE_P (valtype))
3476 warning_at (ploc, OPT_Wtraditional_conversion,
3477 "passing argument %d of %qE as integer rather "
3478 "than floating due to prototype",
3479 argnum, rname);
3480 if (INTEGRAL_TYPE_P (type)
3481 && TREE_CODE (valtype) == COMPLEX_TYPE)
3482 warning_at (ploc, OPT_Wtraditional_conversion,
3483 "passing argument %d of %qE as integer rather "
3484 "than complex due to prototype",
3485 argnum, rname);
3486 else if (TREE_CODE (type) == COMPLEX_TYPE
3487 && SCALAR_FLOAT_TYPE_P (valtype))
3488 warning_at (ploc, OPT_Wtraditional_conversion,
3489 "passing argument %d of %qE as complex rather "
3490 "than floating due to prototype",
3491 argnum, rname);
3492 else if (SCALAR_FLOAT_TYPE_P (type)
3493 && INTEGRAL_TYPE_P (valtype))
3494 warning_at (ploc, OPT_Wtraditional_conversion,
3495 "passing argument %d of %qE as floating rather "
3496 "than integer due to prototype",
3497 argnum, rname);
3498 else if (TREE_CODE (type) == COMPLEX_TYPE
3499 && INTEGRAL_TYPE_P (valtype))
3500 warning_at (ploc, OPT_Wtraditional_conversion,
3501 "passing argument %d of %qE as complex rather "
3502 "than integer due to prototype",
3503 argnum, rname);
3504 else if (SCALAR_FLOAT_TYPE_P (type)
3505 && TREE_CODE (valtype) == COMPLEX_TYPE)
3506 warning_at (ploc, OPT_Wtraditional_conversion,
3507 "passing argument %d of %qE as floating rather "
3508 "than complex due to prototype",
3509 argnum, rname);
3510 /* ??? At some point, messages should be written about
3511 conversions between complex types, but that's too messy
3512 to do now. */
3513 else if (SCALAR_FLOAT_TYPE_P (type)
3514 && SCALAR_FLOAT_TYPE_P (valtype))
3516 unsigned int formal_prec = TYPE_PRECISION (type);
3518 /* Warn if any argument is passed as `float',
3519 since without a prototype it would be `double'. */
3520 if (formal_prec == TYPE_PRECISION (float_type_node)
3521 && type != dfloat32_type_node)
3522 warning_at (ploc, 0,
3523 "passing argument %d of %qE as %<float%> "
3524 "rather than %<double%> due to prototype",
3525 argnum, rname);
3527 /* Warn if mismatch between argument and prototype
3528 for decimal float types. Warn of conversions with
3529 binary float types and of precision narrowing due to
3530 prototype. */
3531 else if (type != valtype
3532 && (type == dfloat32_type_node
3533 || type == dfloat64_type_node
3534 || type == dfloat128_type_node
3535 || valtype == dfloat32_type_node
3536 || valtype == dfloat64_type_node
3537 || valtype == dfloat128_type_node)
3538 && (formal_prec
3539 <= TYPE_PRECISION (valtype)
3540 || (type == dfloat128_type_node
3541 && (valtype
3542 != dfloat64_type_node
3543 && (valtype
3544 != dfloat32_type_node)))
3545 || (type == dfloat64_type_node
3546 && (valtype
3547 != dfloat32_type_node))))
3548 warning_at (ploc, 0,
3549 "passing argument %d of %qE as %qT "
3550 "rather than %qT due to prototype",
3551 argnum, rname, type, valtype);
3554 /* Detect integer changing in width or signedness.
3555 These warnings are only activated with
3556 -Wtraditional-conversion, not with -Wtraditional. */
3557 else if (warn_traditional_conversion
3558 && INTEGRAL_TYPE_P (type)
3559 && INTEGRAL_TYPE_P (valtype))
3561 unsigned int formal_prec = TYPE_PRECISION (type);
3562 tree would_have_been = default_conversion (val);
3563 tree type1 = TREE_TYPE (would_have_been);
3565 if (val == error_mark_node)
3566 /* VAL could have been of incomplete type. */;
3567 else if (TREE_CODE (type) == ENUMERAL_TYPE
3568 && (TYPE_MAIN_VARIANT (type)
3569 == TYPE_MAIN_VARIANT (valtype)))
3570 /* No warning if function asks for enum
3571 and the actual arg is that enum type. */
3573 else if (formal_prec != TYPE_PRECISION (type1))
3574 warning_at (ploc, OPT_Wtraditional_conversion,
3575 "passing argument %d of %qE "
3576 "with different width due to prototype",
3577 argnum, rname);
3578 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3580 /* Don't complain if the formal parameter type
3581 is an enum, because we can't tell now whether
3582 the value was an enum--even the same enum. */
3583 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3585 else if (TREE_CODE (val) == INTEGER_CST
3586 && int_fits_type_p (val, type))
3587 /* Change in signedness doesn't matter
3588 if a constant value is unaffected. */
3590 /* If the value is extended from a narrower
3591 unsigned type, it doesn't matter whether we
3592 pass it as signed or unsigned; the value
3593 certainly is the same either way. */
3594 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3595 && TYPE_UNSIGNED (valtype))
3597 else if (TYPE_UNSIGNED (type))
3598 warning_at (ploc, OPT_Wtraditional_conversion,
3599 "passing argument %d of %qE "
3600 "as unsigned due to prototype",
3601 argnum, rname);
3602 else
3603 warning_at (ploc, OPT_Wtraditional_conversion,
3604 "passing argument %d of %qE "
3605 "as signed due to prototype",
3606 argnum, rname);
3610 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3611 sake of better warnings from convert_and_check. */
3612 if (excess_precision)
3613 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3615 tree parmval = convert_for_assignment (ploc, ploc, type,
3616 val, origtype, ic_argpass,
3617 npc, fundecl, function,
3618 parmnum + 1, warnopt);
3620 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3621 && INTEGRAL_TYPE_P (type)
3622 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3623 parmval = default_conversion (parmval);
3625 return parmval;
3628 /* Convert the argument expressions in the vector VALUES
3629 to the types in the list TYPELIST.
3631 If TYPELIST is exhausted, or when an element has NULL as its type,
3632 perform the default conversions.
3634 ORIGTYPES is the original types of the expressions in VALUES. This
3635 holds the type of enum values which have been converted to integral
3636 types. It may be NULL.
3638 FUNCTION is a tree for the called function. It is used only for
3639 error messages, where it is formatted with %qE.
3641 This is also where warnings about wrong number of args are generated.
3643 ARG_LOC are locations of function arguments (if any).
3645 Returns the actual number of arguments processed (which may be less
3646 than the length of VALUES in some error situations), or -1 on
3647 failure. */
3649 static int
3650 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3651 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3652 tree function, tree fundecl)
3654 unsigned int parmnum;
3655 bool error_args = false;
3656 const bool type_generic = fundecl
3657 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3658 bool type_generic_remove_excess_precision = false;
3659 bool type_generic_overflow_p = false;
3660 bool type_generic_bit_query = false;
3661 tree selector;
3663 /* Change pointer to function to the function itself for
3664 diagnostics. */
3665 if (TREE_CODE (function) == ADDR_EXPR
3666 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3667 function = TREE_OPERAND (function, 0);
3669 /* Handle an ObjC selector specially for diagnostics. */
3670 selector = objc_message_selector ();
3672 /* For a call to a built-in function declared without a prototype,
3673 set to the built-in function's argument list. */
3674 tree builtin_typelist = NULL_TREE;
3676 /* For type-generic built-in functions, determine whether excess
3677 precision should be removed (classification) or not
3678 (comparison). */
3679 if (fundecl
3680 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3682 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3683 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3685 /* For a call to a built-in function declared without a prototype
3686 use the types of the parameters of the internal built-in to
3687 match those of the arguments to. */
3688 if (tree bdecl = builtin_decl_explicit (code))
3689 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3692 /* For type-generic built-in functions, determine whether excess
3693 precision should be removed (classification) or not
3694 (comparison). */
3695 if (type_generic)
3696 switch (code)
3698 case BUILT_IN_ISFINITE:
3699 case BUILT_IN_ISINF:
3700 case BUILT_IN_ISINF_SIGN:
3701 case BUILT_IN_ISNAN:
3702 case BUILT_IN_ISNORMAL:
3703 case BUILT_IN_ISSIGNALING:
3704 case BUILT_IN_FPCLASSIFY:
3705 type_generic_remove_excess_precision = true;
3706 break;
3708 case BUILT_IN_ADD_OVERFLOW_P:
3709 case BUILT_IN_SUB_OVERFLOW_P:
3710 case BUILT_IN_MUL_OVERFLOW_P:
3711 /* The last argument of these type-generic builtins
3712 should not be promoted. */
3713 type_generic_overflow_p = true;
3714 break;
3716 case BUILT_IN_CLZG:
3717 case BUILT_IN_CTZG:
3718 case BUILT_IN_CLRSBG:
3719 case BUILT_IN_FFSG:
3720 case BUILT_IN_PARITYG:
3721 case BUILT_IN_POPCOUNTG:
3722 /* The first argument of these type-generic builtins
3723 should not be promoted. */
3724 type_generic_bit_query = true;
3725 break;
3727 default:
3728 break;
3732 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3733 individual converted arguments. */
3735 tree typetail, builtin_typetail, val;
3736 for (typetail = typelist,
3737 builtin_typetail = builtin_typelist,
3738 parmnum = 0;
3739 values && values->iterate (parmnum, &val);
3740 ++parmnum)
3742 /* The type of the function parameter (if it was declared with one). */
3743 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3744 /* The type of the built-in function parameter (if the function
3745 is a built-in). Used to detect type incompatibilities in
3746 calls to built-ins declared without a prototype. */
3747 tree builtin_type = (builtin_typetail
3748 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3749 /* The original type of the argument being passed to the function. */
3750 tree valtype = TREE_TYPE (val);
3751 /* The called function (or function selector in Objective C). */
3752 tree rname = function;
3753 int argnum = parmnum + 1;
3754 const char *invalid_func_diag;
3755 /* Set for EXCESS_PRECISION_EXPR arguments. */
3756 bool excess_precision = false;
3757 /* The value of the argument after conversion to the type
3758 of the function parameter it is passed to. */
3759 tree parmval;
3760 /* Some __atomic_* builtins have additional hidden argument at
3761 position 0. */
3762 location_t ploc
3763 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3764 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3765 : input_location;
3767 if (type == void_type_node)
3769 if (selector)
3770 error_at (loc, "too many arguments to method %qE", selector);
3771 else
3772 error_at (loc, "too many arguments to function %qE", function);
3773 inform_declaration (fundecl);
3774 return error_args ? -1 : (int) parmnum;
3777 if (builtin_type == void_type_node)
3779 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3780 "too many arguments to built-in function %qE "
3781 "expecting %d", function, parmnum))
3782 inform_declaration (fundecl);
3783 builtin_typetail = NULL_TREE;
3786 if (selector && argnum > 2)
3788 rname = selector;
3789 argnum -= 2;
3792 /* Determine if VAL is a null pointer constant before folding it. */
3793 bool npc = null_pointer_constant_p (val);
3795 /* If there is excess precision and a prototype, convert once to
3796 the required type rather than converting via the semantic
3797 type. Likewise without a prototype a float value represented
3798 as long double should be converted once to double. But for
3799 type-generic classification functions excess precision must
3800 be removed here. */
3801 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3802 && (type || !type_generic || !type_generic_remove_excess_precision))
3804 val = TREE_OPERAND (val, 0);
3805 excess_precision = true;
3807 val = c_fully_fold (val, false, NULL);
3808 STRIP_TYPE_NOPS (val);
3810 val = require_complete_type (ploc, val);
3812 /* Some floating-point arguments must be promoted to double when
3813 no type is specified by a prototype. This applies to
3814 arguments of type float, and to architecture-specific types
3815 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3816 bool promote_float_arg = false;
3817 if (type == NULL_TREE
3818 && TREE_CODE (valtype) == REAL_TYPE
3819 && (TYPE_PRECISION (valtype)
3820 <= TYPE_PRECISION (double_type_node))
3821 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3822 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3823 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3825 /* Promote this argument, unless it has a _FloatN or
3826 _FloatNx type. */
3827 promote_float_arg = true;
3828 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3829 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3831 promote_float_arg = false;
3832 break;
3834 /* Don't promote __bf16 either. */
3835 if (TYPE_MAIN_VARIANT (valtype) == bfloat16_type_node)
3836 promote_float_arg = false;
3839 if (type != NULL_TREE)
3841 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3842 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3843 val, valtype, npc, rname, parmnum, argnum,
3844 excess_precision, 0);
3846 else if (promote_float_arg)
3848 if (type_generic)
3849 parmval = val;
3850 else
3852 /* Convert `float' to `double'. */
3853 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3854 warning_at (ploc, OPT_Wdouble_promotion,
3855 "implicit conversion from %qT to %qT when passing "
3856 "argument to function",
3857 valtype, double_type_node);
3858 parmval = convert (double_type_node, val);
3861 else if ((excess_precision && !type_generic)
3862 || (type_generic_overflow_p && parmnum == 2)
3863 || (type_generic_bit_query && parmnum == 0))
3864 /* A "double" argument with excess precision being passed
3865 without a prototype or in variable arguments.
3866 The last argument of __builtin_*_overflow_p should not be
3867 promoted, similarly the first argument of
3868 __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
3869 parmval = convert (valtype, val);
3870 else if ((invalid_func_diag =
3871 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3873 error (invalid_func_diag);
3874 return -1;
3876 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3878 return -1;
3880 else
3881 /* Convert `short' and `char' to full-size `int'. */
3882 parmval = default_conversion (val);
3884 (*values)[parmnum] = parmval;
3885 if (parmval == error_mark_node)
3886 error_args = true;
3888 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3890 /* For a call to a built-in function declared without a prototype,
3891 perform the conversions from the argument to the expected type
3892 but issue warnings rather than errors for any mismatches.
3893 Ignore the converted argument and use the PARMVAL obtained
3894 above by applying default conversions instead. */
3895 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3896 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3897 val, valtype, npc, rname, parmnum, argnum,
3898 excess_precision,
3899 OPT_Wbuiltin_declaration_mismatch);
3902 if (typetail)
3903 typetail = TREE_CHAIN (typetail);
3905 if (builtin_typetail)
3906 builtin_typetail = TREE_CHAIN (builtin_typetail);
3909 gcc_assert (parmnum == vec_safe_length (values));
3911 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3913 error_at (loc, "too few arguments to function %qE", function);
3914 inform_declaration (fundecl);
3915 return -1;
3918 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3920 unsigned nargs = parmnum;
3921 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3922 ++nargs;
3924 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3925 "too few arguments to built-in function %qE "
3926 "expecting %u", function, nargs - 1))
3927 inform_declaration (fundecl);
3930 return error_args ? -1 : (int) parmnum;
3933 /* This is the entry point used by the parser to build unary operators
3934 in the input. CODE, a tree_code, specifies the unary operator, and
3935 ARG is the operand. For unary plus, the C parser currently uses
3936 CONVERT_EXPR for code.
3938 LOC is the location to use for the tree generated.
3941 struct c_expr
3942 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3944 struct c_expr result;
3946 result.original_code = code;
3947 result.original_type = NULL;
3948 result.m_decimal = 0;
3950 if (reject_gcc_builtin (arg.value))
3952 result.value = error_mark_node;
3954 else
3956 result.value = build_unary_op (loc, code, arg.value, false);
3958 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3959 overflow_warning (loc, result.value, arg.value);
3962 /* We are typically called when parsing a prefix token at LOC acting on
3963 ARG. Reflect this by updating the source range of the result to
3964 start at LOC and end at the end of ARG. */
3965 set_c_expr_source_range (&result,
3966 loc, arg.get_finish ());
3968 return result;
3971 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3973 bool
3974 char_type_p (tree type)
3976 return (type == char_type_node
3977 || type == unsigned_char_type_node
3978 || type == signed_char_type_node
3979 || type == char16_type_node
3980 || type == char32_type_node);
3983 /* This is the entry point used by the parser to build binary operators
3984 in the input. CODE, a tree_code, specifies the binary operator, and
3985 ARG1 and ARG2 are the operands. In addition to constructing the
3986 expression, we check for operands that were written with other binary
3987 operators in a way that is likely to confuse the user.
3989 LOCATION is the location of the binary operator. */
3991 struct c_expr
3992 parser_build_binary_op (location_t location, enum tree_code code,
3993 struct c_expr arg1, struct c_expr arg2)
3995 struct c_expr result;
3996 result.m_decimal = 0;
3998 enum tree_code code1 = arg1.original_code;
3999 enum tree_code code2 = arg2.original_code;
4000 tree type1 = (arg1.original_type
4001 ? arg1.original_type
4002 : TREE_TYPE (arg1.value));
4003 tree type2 = (arg2.original_type
4004 ? arg2.original_type
4005 : TREE_TYPE (arg2.value));
4007 result.value = build_binary_op (location, code,
4008 arg1.value, arg2.value, true);
4009 result.original_code = code;
4010 result.original_type = NULL;
4011 result.m_decimal = 0;
4013 if (TREE_CODE (result.value) == ERROR_MARK)
4015 set_c_expr_source_range (&result,
4016 arg1.get_start (),
4017 arg2.get_finish ());
4018 return result;
4021 if (location != UNKNOWN_LOCATION)
4022 protected_set_expr_location (result.value, location);
4024 set_c_expr_source_range (&result,
4025 arg1.get_start (),
4026 arg2.get_finish ());
4028 /* Check for cases such as x+y<<z which users are likely
4029 to misinterpret. */
4030 if (warn_parentheses)
4031 warn_about_parentheses (location, code, code1, arg1.value, code2,
4032 arg2.value);
4034 if (warn_logical_op)
4035 warn_logical_operator (location, code, TREE_TYPE (result.value),
4036 code1, arg1.value, code2, arg2.value);
4038 if (warn_tautological_compare)
4040 tree lhs = arg1.value;
4041 tree rhs = arg2.value;
4042 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4044 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
4045 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
4046 lhs = NULL_TREE;
4047 else
4048 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
4050 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
4052 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
4053 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
4054 rhs = NULL_TREE;
4055 else
4056 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
4058 if (lhs != NULL_TREE && rhs != NULL_TREE)
4059 warn_tautological_cmp (location, code, lhs, rhs);
4062 if (warn_logical_not_paren
4063 && TREE_CODE_CLASS (code) == tcc_comparison
4064 && code1 == TRUTH_NOT_EXPR
4065 && code2 != TRUTH_NOT_EXPR
4066 /* Avoid warning for !!x == y. */
4067 && (TREE_CODE (arg1.value) != NE_EXPR
4068 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
4070 /* Avoid warning for !b == y where b has _Bool type. */
4071 tree t = integer_zero_node;
4072 if (TREE_CODE (arg1.value) == EQ_EXPR
4073 && integer_zerop (TREE_OPERAND (arg1.value, 1))
4074 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
4076 t = TREE_OPERAND (arg1.value, 0);
4079 if (TREE_TYPE (t) != integer_type_node)
4080 break;
4081 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
4082 t = C_MAYBE_CONST_EXPR_EXPR (t);
4083 else if (CONVERT_EXPR_P (t))
4084 t = TREE_OPERAND (t, 0);
4085 else
4086 break;
4088 while (1);
4090 if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t)))
4091 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
4094 /* Warn about comparisons against string literals, with the exception
4095 of testing for equality or inequality of a string literal with NULL. */
4096 if (code == EQ_EXPR || code == NE_EXPR)
4098 if ((code1 == STRING_CST
4099 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
4100 || (code2 == STRING_CST
4101 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
4102 warning_at (location, OPT_Waddress,
4103 "comparison with string literal results in unspecified behavior");
4104 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
4105 if (POINTER_TYPE_P (type1)
4106 && null_pointer_constant_p (arg2.value)
4107 && char_type_p (type2))
4109 auto_diagnostic_group d;
4110 if (warning_at (location, OPT_Wpointer_compare,
4111 "comparison between pointer and zero character "
4112 "constant"))
4113 inform (arg1.get_start (),
4114 "did you mean to dereference the pointer?");
4116 else if (POINTER_TYPE_P (type2)
4117 && null_pointer_constant_p (arg1.value)
4118 && char_type_p (type1))
4120 auto_diagnostic_group d;
4121 if (warning_at (location, OPT_Wpointer_compare,
4122 "comparison between pointer and zero character "
4123 "constant"))
4124 inform (arg2.get_start (),
4125 "did you mean to dereference the pointer?");
4128 else if (TREE_CODE_CLASS (code) == tcc_comparison
4129 && (code1 == STRING_CST || code2 == STRING_CST))
4130 warning_at (location, OPT_Waddress,
4131 "comparison with string literal results in unspecified "
4132 "behavior");
4134 if (warn_array_compare
4135 && TREE_CODE_CLASS (code) == tcc_comparison
4136 && TREE_CODE (type1) == ARRAY_TYPE
4137 && TREE_CODE (type2) == ARRAY_TYPE)
4138 do_warn_array_compare (location, code, arg1.value, arg2.value);
4140 if (TREE_OVERFLOW_P (result.value)
4141 && !TREE_OVERFLOW_P (arg1.value)
4142 && !TREE_OVERFLOW_P (arg2.value))
4143 overflow_warning (location, result.value);
4145 /* Warn about comparisons of different enum types. */
4146 if (warn_enum_compare
4147 && TREE_CODE_CLASS (code) == tcc_comparison
4148 && TREE_CODE (type1) == ENUMERAL_TYPE
4149 && TREE_CODE (type2) == ENUMERAL_TYPE
4150 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
4151 warning_at (location, OPT_Wenum_compare,
4152 "comparison between %qT and %qT",
4153 type1, type2);
4155 if (warn_xor_used_as_pow
4156 && code == BIT_XOR_EXPR
4157 && arg1.m_decimal
4158 && arg2.m_decimal)
4159 check_for_xor_used_as_pow (arg1.get_location (), arg1.value,
4160 location,
4161 arg2.get_location (), arg2.value);
4163 return result;
4166 /* Return a tree for the difference of pointers OP0 and OP1.
4167 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
4168 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
4170 static tree
4171 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
4173 tree restype = ptrdiff_type_node;
4174 tree result, inttype;
4176 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
4177 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
4178 tree target_type = TREE_TYPE (TREE_TYPE (op0));
4179 tree orig_op0 = op0;
4180 tree orig_op1 = op1;
4182 /* If the operands point into different address spaces, we need to
4183 explicitly convert them to pointers into the common address space
4184 before we can subtract the numerical address values. */
4185 if (as0 != as1)
4187 addr_space_t as_common;
4188 tree common_type;
4190 /* Determine the common superset address space. This is guaranteed
4191 to exist because the caller verified that comp_target_types
4192 returned non-zero. */
4193 if (!addr_space_superset (as0, as1, &as_common))
4194 gcc_unreachable ();
4196 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
4197 op0 = convert (common_type, op0);
4198 op1 = convert (common_type, op1);
4201 /* Determine integer type result of the subtraction. This will usually
4202 be the same as the result type (ptrdiff_t), but may need to be a wider
4203 type if pointers for the address space are wider than ptrdiff_t. */
4204 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
4205 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
4206 else
4207 inttype = restype;
4209 if (VOID_TYPE_P (target_type))
4210 pedwarn (loc, OPT_Wpointer_arith,
4211 "pointer of type %<void *%> used in subtraction");
4212 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4213 pedwarn (loc, OPT_Wpointer_arith,
4214 "pointer to a function used in subtraction");
4216 if (current_function_decl != NULL_TREE
4217 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
4219 op0 = save_expr (op0);
4220 op1 = save_expr (op1);
4222 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
4223 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
4226 /* First do the subtraction, then build the divide operator
4227 and only convert at the very end.
4228 Do not do default conversions in case restype is a short type. */
4230 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4231 pointers. If some platform cannot provide that, or has a larger
4232 ptrdiff_type to support differences larger than half the address
4233 space, cast the pointers to some larger integer type and do the
4234 computations in that type. */
4235 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4236 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4237 convert (inttype, op1), false);
4238 else
4240 /* Cast away qualifiers. */
4241 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4242 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4243 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4246 /* This generates an error if op1 is pointer to incomplete type. */
4247 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4248 error_at (loc, "arithmetic on pointer to an incomplete type");
4249 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4250 TREE_TYPE (TREE_TYPE (orig_op0))))
4251 verify_type_context (loc, TCTX_POINTER_ARITH,
4252 TREE_TYPE (TREE_TYPE (orig_op1)));
4254 op1 = c_size_in_bytes (target_type);
4256 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4257 error_at (loc, "arithmetic on pointer to an empty aggregate");
4259 /* Divide by the size, in easiest possible way. */
4260 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4261 op0, convert (inttype, op1));
4263 /* Convert to final result type if necessary. */
4264 return convert (restype, result);
4267 /* Expand atomic compound assignments into an appropriate sequence as
4268 specified by the C11 standard section 6.5.16.2.
4270 _Atomic T1 E1
4271 T2 E2
4272 E1 op= E2
4274 This sequence is used for all types for which these operations are
4275 supported.
4277 In addition, built-in versions of the 'fe' prefixed routines may
4278 need to be invoked for floating point (real, complex or vector) when
4279 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4281 T1 newval;
4282 T1 old;
4283 T1 *addr
4284 T2 val
4285 fenv_t fenv
4287 addr = &E1;
4288 val = (E2);
4289 __atomic_load (addr, &old, SEQ_CST);
4290 feholdexcept (&fenv);
4291 loop:
4292 newval = old op val;
4293 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4294 SEQ_CST))
4295 goto done;
4296 feclearexcept (FE_ALL_EXCEPT);
4297 goto loop:
4298 done:
4299 feupdateenv (&fenv);
4301 The compiler will issue the __atomic_fetch_* built-in when possible,
4302 otherwise it will generate the generic form of the atomic operations.
4303 This requires temp(s) and has their address taken. The atomic processing
4304 is smart enough to figure out when the size of an object can utilize
4305 a lock-free version, and convert the built-in call to the appropriate
4306 lock-free routine. The optimizers will then dispose of any temps that
4307 are no longer required, and lock-free implementations are utilized as
4308 long as there is target support for the required size.
4310 If the operator is NOP_EXPR, then this is a simple assignment, and
4311 an __atomic_store is issued to perform the assignment rather than
4312 the above loop. */
4314 /* Build an atomic assignment at LOC, expanding into the proper
4315 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4316 the result of the operation, unless RETURN_OLD_P, in which case
4317 return the old value of LHS (this is only for postincrement and
4318 postdecrement). */
4320 static tree
4321 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4322 tree rhs, bool return_old_p)
4324 tree fndecl, func_call;
4325 vec<tree, va_gc> *params;
4326 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4327 tree old, old_addr;
4328 tree compound_stmt = NULL_TREE;
4329 tree stmt, goto_stmt;
4330 tree loop_label, loop_decl, done_label, done_decl;
4332 tree lhs_type = TREE_TYPE (lhs);
4333 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4334 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4335 tree rhs_semantic_type = TREE_TYPE (rhs);
4336 tree nonatomic_rhs_semantic_type;
4337 tree rhs_type;
4339 gcc_assert (TYPE_ATOMIC (lhs_type));
4341 if (return_old_p)
4342 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4344 /* Allocate enough vector items for a compare_exchange. */
4345 vec_alloc (params, 6);
4347 /* Create a compound statement to hold the sequence of statements
4348 with a loop. */
4349 if (modifycode != NOP_EXPR)
4351 compound_stmt = c_begin_compound_stmt (false);
4353 /* For consistency with build_modify_expr on non-_Atomic,
4354 mark the lhs as read. Also, it would be very hard to match
4355 such expressions in mark_exp_read. */
4356 mark_exp_read (lhs);
4359 /* Remove any excess precision (which is only present here in the
4360 case of compound assignments). */
4361 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4363 gcc_assert (modifycode != NOP_EXPR);
4364 rhs = TREE_OPERAND (rhs, 0);
4366 rhs_type = TREE_TYPE (rhs);
4368 /* Fold the RHS if it hasn't already been folded. */
4369 if (modifycode != NOP_EXPR)
4370 rhs = c_fully_fold (rhs, false, NULL);
4372 /* Remove the qualifiers for the rest of the expressions and create
4373 the VAL temp variable to hold the RHS. */
4374 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4375 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4376 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4377 TYPE_UNQUALIFIED);
4378 val = create_tmp_var_raw (nonatomic_rhs_type);
4379 TREE_ADDRESSABLE (val) = 1;
4380 suppress_warning (val);
4381 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4382 NULL_TREE);
4383 TREE_SIDE_EFFECTS (rhs) = 1;
4384 SET_EXPR_LOCATION (rhs, loc);
4385 if (modifycode != NOP_EXPR)
4386 add_stmt (rhs);
4388 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4389 an atomic_store. */
4390 if (modifycode == NOP_EXPR)
4392 compound_stmt = rhs;
4393 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4394 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4395 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4396 params->quick_push (lhs_addr);
4397 params->quick_push (rhs);
4398 params->quick_push (seq_cst);
4399 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4401 compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4402 compound_stmt, func_call);
4404 /* VAL is the value which was stored, return a COMPOUND_STMT of
4405 the statement and that value. */
4406 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4409 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4410 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4411 isn't applicable for such builtins. ??? Do we want to handle enums? */
4412 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4413 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4415 built_in_function fncode;
4416 switch (modifycode)
4418 case PLUS_EXPR:
4419 case POINTER_PLUS_EXPR:
4420 fncode = (return_old_p
4421 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4422 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4423 break;
4424 case MINUS_EXPR:
4425 fncode = (return_old_p
4426 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4427 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4428 break;
4429 case BIT_AND_EXPR:
4430 fncode = (return_old_p
4431 ? BUILT_IN_ATOMIC_FETCH_AND_N
4432 : BUILT_IN_ATOMIC_AND_FETCH_N);
4433 break;
4434 case BIT_IOR_EXPR:
4435 fncode = (return_old_p
4436 ? BUILT_IN_ATOMIC_FETCH_OR_N
4437 : BUILT_IN_ATOMIC_OR_FETCH_N);
4438 break;
4439 case BIT_XOR_EXPR:
4440 fncode = (return_old_p
4441 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4442 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4443 break;
4444 default:
4445 goto cas_loop;
4448 /* We can only use "_1" through "_16" variants of the atomic fetch
4449 built-ins. */
4450 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4451 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4452 goto cas_loop;
4454 /* If this is a pointer type, we need to multiply by the size of
4455 the pointer target type. */
4456 if (POINTER_TYPE_P (lhs_type))
4458 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4459 /* ??? This would introduce -Wdiscarded-qualifiers
4460 warning: __atomic_fetch_* expect volatile void *
4461 type as the first argument. (Assignments between
4462 atomic and non-atomic objects are OK.) */
4463 || TYPE_RESTRICT (lhs_type))
4464 goto cas_loop;
4465 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4466 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4467 convert (ptrdiff_type_node, rhs),
4468 convert (ptrdiff_type_node, sz));
4471 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4472 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4473 fndecl = builtin_decl_explicit (fncode);
4474 params->quick_push (lhs_addr);
4475 params->quick_push (rhs);
4476 params->quick_push (seq_cst);
4477 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4479 newval = create_tmp_var_raw (nonatomic_lhs_type);
4480 TREE_ADDRESSABLE (newval) = 1;
4481 suppress_warning (newval);
4482 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4483 NULL_TREE, NULL_TREE);
4484 SET_EXPR_LOCATION (rhs, loc);
4485 add_stmt (rhs);
4487 /* Finish the compound statement. */
4488 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4490 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4491 the statement and that value. */
4492 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4495 cas_loop:
4496 /* Create the variables and labels required for the op= form. */
4497 old = create_tmp_var_raw (nonatomic_lhs_type);
4498 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4499 TREE_ADDRESSABLE (old) = 1;
4500 suppress_warning (old);
4502 newval = create_tmp_var_raw (nonatomic_lhs_type);
4503 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4504 TREE_ADDRESSABLE (newval) = 1;
4505 suppress_warning (newval);
4507 loop_decl = create_artificial_label (loc);
4508 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4510 done_decl = create_artificial_label (loc);
4511 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4513 /* __atomic_load (addr, &old, SEQ_CST). */
4514 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4515 params->quick_push (lhs_addr);
4516 params->quick_push (old_addr);
4517 params->quick_push (seq_cst);
4518 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4519 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4520 NULL_TREE);
4521 add_stmt (old);
4522 params->truncate (0);
4524 /* Create the expressions for floating-point environment
4525 manipulation, if required. */
4526 bool need_fenv = (flag_trapping_math
4527 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4528 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4529 if (need_fenv)
4530 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4532 if (hold_call)
4533 add_stmt (hold_call);
4535 /* loop: */
4536 add_stmt (loop_label);
4538 /* newval = old + val; */
4539 if (rhs_type != rhs_semantic_type)
4540 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4541 rhs = build_binary_op (loc, modifycode, old, val, true);
4542 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4544 tree eptype = TREE_TYPE (rhs);
4545 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4546 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4548 else
4549 rhs = c_fully_fold (rhs, false, NULL);
4550 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4551 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4552 NULL_TREE, 0);
4553 if (rhs != error_mark_node)
4555 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4556 NULL_TREE);
4557 SET_EXPR_LOCATION (rhs, loc);
4558 add_stmt (rhs);
4561 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4562 goto done; */
4563 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4564 params->quick_push (lhs_addr);
4565 params->quick_push (old_addr);
4566 params->quick_push (newval_addr);
4567 params->quick_push (integer_zero_node);
4568 params->quick_push (seq_cst);
4569 params->quick_push (seq_cst);
4570 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4572 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4573 SET_EXPR_LOCATION (goto_stmt, loc);
4575 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4576 SET_EXPR_LOCATION (stmt, loc);
4577 add_stmt (stmt);
4579 if (clear_call)
4580 add_stmt (clear_call);
4582 /* goto loop; */
4583 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4584 SET_EXPR_LOCATION (goto_stmt, loc);
4585 add_stmt (goto_stmt);
4587 /* done: */
4588 add_stmt (done_label);
4590 if (update_call)
4591 add_stmt (update_call);
4593 /* Finish the compound statement. */
4594 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4596 /* NEWVAL is the value that was successfully stored, return a
4597 COMPOUND_EXPR of the statement and the appropriate value. */
4598 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4599 return_old_p ? old : newval);
4602 /* Construct and perhaps optimize a tree representation
4603 for a unary operation. CODE, a tree_code, specifies the operation
4604 and XARG is the operand.
4605 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4606 promotions (such as from short to int).
4607 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4608 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4609 to pointers in C99.
4611 LOCATION is the location of the operator. */
4613 tree
4614 build_unary_op (location_t location, enum tree_code code, tree xarg,
4615 bool noconvert)
4617 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4618 tree arg = xarg;
4619 tree argtype = NULL_TREE;
4620 enum tree_code typecode;
4621 tree val;
4622 tree ret = error_mark_node;
4623 tree eptype = NULL_TREE;
4624 const char *invalid_op_diag;
4625 bool int_operands;
4627 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4628 if (int_operands)
4629 arg = remove_c_maybe_const_expr (arg);
4631 if (code != ADDR_EXPR)
4632 arg = require_complete_type (location, arg);
4634 typecode = TREE_CODE (TREE_TYPE (arg));
4635 if (typecode == ERROR_MARK)
4636 return error_mark_node;
4637 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4638 typecode = INTEGER_TYPE;
4640 if ((invalid_op_diag
4641 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4643 error_at (location, invalid_op_diag);
4644 return error_mark_node;
4647 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4649 eptype = TREE_TYPE (arg);
4650 arg = TREE_OPERAND (arg, 0);
4653 switch (code)
4655 case CONVERT_EXPR:
4656 /* This is used for unary plus, because a CONVERT_EXPR
4657 is enough to prevent anybody from looking inside for
4658 associativity, but won't generate any code. */
4659 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4660 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4661 || typecode == BITINT_TYPE
4662 || gnu_vector_type_p (TREE_TYPE (arg))))
4664 error_at (location, "wrong type argument to unary plus");
4665 return error_mark_node;
4667 else if (!noconvert)
4668 arg = default_conversion (arg);
4669 arg = non_lvalue_loc (location, arg);
4670 break;
4672 case NEGATE_EXPR:
4673 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4674 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4675 || typecode == BITINT_TYPE
4676 || gnu_vector_type_p (TREE_TYPE (arg))))
4678 error_at (location, "wrong type argument to unary minus");
4679 return error_mark_node;
4681 else if (!noconvert)
4682 arg = default_conversion (arg);
4683 break;
4685 case BIT_NOT_EXPR:
4686 /* ~ works on integer types and non float vectors. */
4687 if (typecode == INTEGER_TYPE
4688 || typecode == BITINT_TYPE
4689 || (gnu_vector_type_p (TREE_TYPE (arg))
4690 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4692 tree e = arg;
4694 /* Warn if the expression has boolean value. */
4695 while (TREE_CODE (e) == COMPOUND_EXPR)
4696 e = TREE_OPERAND (e, 1);
4698 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg))
4699 || truth_value_p (TREE_CODE (e))))
4701 auto_diagnostic_group d;
4702 if (warning_at (location, OPT_Wbool_operation,
4703 "%<~%> on a boolean expression"))
4705 gcc_rich_location richloc (location);
4706 richloc.add_fixit_insert_before (location, "!");
4707 inform (&richloc, "did you mean to use logical not?");
4710 if (!noconvert)
4711 arg = default_conversion (arg);
4713 else if (typecode == COMPLEX_TYPE)
4715 code = CONJ_EXPR;
4716 pedwarn (location, OPT_Wpedantic,
4717 "ISO C does not support %<~%> for complex conjugation");
4718 if (!noconvert)
4719 arg = default_conversion (arg);
4721 else
4723 error_at (location, "wrong type argument to bit-complement");
4724 return error_mark_node;
4726 break;
4728 case ABS_EXPR:
4729 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4731 error_at (location, "wrong type argument to abs");
4732 return error_mark_node;
4734 else if (!noconvert)
4735 arg = default_conversion (arg);
4736 break;
4738 case ABSU_EXPR:
4739 if (!(typecode == INTEGER_TYPE))
4741 error_at (location, "wrong type argument to absu");
4742 return error_mark_node;
4744 else if (!noconvert)
4745 arg = default_conversion (arg);
4746 break;
4748 case CONJ_EXPR:
4749 /* Conjugating a real value is a no-op, but allow it anyway. */
4750 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4751 || typecode == COMPLEX_TYPE))
4753 error_at (location, "wrong type argument to conjugation");
4754 return error_mark_node;
4756 else if (!noconvert)
4757 arg = default_conversion (arg);
4758 break;
4760 case TRUTH_NOT_EXPR:
4761 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4762 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4763 && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE
4764 && typecode != BITINT_TYPE)
4766 error_at (location,
4767 "wrong type argument to unary exclamation mark");
4768 return error_mark_node;
4770 if (int_operands)
4772 arg = c_objc_common_truthvalue_conversion (location, xarg);
4773 arg = remove_c_maybe_const_expr (arg);
4775 else
4776 arg = c_objc_common_truthvalue_conversion (location, arg);
4777 ret = invert_truthvalue_loc (location, arg);
4778 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4779 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4780 location = EXPR_LOCATION (ret);
4781 goto return_build_unary_op;
4783 case REALPART_EXPR:
4784 case IMAGPART_EXPR:
4785 ret = build_real_imag_expr (location, code, arg);
4786 if (ret == error_mark_node)
4787 return error_mark_node;
4788 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4789 eptype = TREE_TYPE (eptype);
4790 goto return_build_unary_op;
4792 case PREINCREMENT_EXPR:
4793 case POSTINCREMENT_EXPR:
4794 case PREDECREMENT_EXPR:
4795 case POSTDECREMENT_EXPR:
4797 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4799 tree inner = build_unary_op (location, code,
4800 C_MAYBE_CONST_EXPR_EXPR (arg),
4801 noconvert);
4802 if (inner == error_mark_node)
4803 return error_mark_node;
4804 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4805 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4806 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4807 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4808 goto return_build_unary_op;
4811 /* Complain about anything that is not a true lvalue. In
4812 Objective-C, skip this check for property_refs. */
4813 if (!objc_is_property_ref (arg)
4814 && !lvalue_or_else (location,
4815 arg, ((code == PREINCREMENT_EXPR
4816 || code == POSTINCREMENT_EXPR)
4817 ? lv_increment
4818 : lv_decrement)))
4819 return error_mark_node;
4821 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4823 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4824 warning_at (location, OPT_Wc___compat,
4825 "increment of enumeration value is invalid in C++");
4826 else
4827 warning_at (location, OPT_Wc___compat,
4828 "decrement of enumeration value is invalid in C++");
4831 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
4833 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4834 warning_at (location, OPT_Wbool_operation,
4835 "increment of a boolean expression");
4836 else
4837 warning_at (location, OPT_Wbool_operation,
4838 "decrement of a boolean expression");
4841 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4842 arg = c_fully_fold (arg, false, NULL, true);
4844 bool atomic_op;
4845 atomic_op = really_atomic_lvalue (arg);
4847 /* Increment or decrement the real part of the value,
4848 and don't change the imaginary part. */
4849 if (typecode == COMPLEX_TYPE)
4851 tree real, imag;
4853 pedwarn (location, OPT_Wpedantic,
4854 "ISO C does not support %<++%> and %<--%> on complex types");
4856 if (!atomic_op)
4858 arg = stabilize_reference (arg);
4859 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4860 true);
4861 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4862 true);
4863 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4864 if (real == error_mark_node || imag == error_mark_node)
4865 return error_mark_node;
4866 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4867 real, imag);
4868 goto return_build_unary_op;
4872 /* Report invalid types. */
4874 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4875 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4876 && typecode != COMPLEX_TYPE && typecode != BITINT_TYPE
4877 && !gnu_vector_type_p (TREE_TYPE (arg)))
4879 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4880 error_at (location, "wrong type argument to increment");
4881 else
4882 error_at (location, "wrong type argument to decrement");
4884 return error_mark_node;
4888 tree inc;
4890 argtype = TREE_TYPE (arg);
4892 /* Compute the increment. */
4894 if (typecode == POINTER_TYPE)
4896 /* If pointer target is an incomplete type,
4897 we just cannot know how to do the arithmetic. */
4898 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4900 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4901 error_at (location,
4902 "increment of pointer to an incomplete type %qT",
4903 TREE_TYPE (argtype));
4904 else
4905 error_at (location,
4906 "decrement of pointer to an incomplete type %qT",
4907 TREE_TYPE (argtype));
4909 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4910 || VOID_TYPE_P (TREE_TYPE (argtype)))
4912 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4913 pedwarn (location, OPT_Wpointer_arith,
4914 "wrong type argument to increment");
4915 else
4916 pedwarn (location, OPT_Wpointer_arith,
4917 "wrong type argument to decrement");
4919 else
4920 verify_type_context (location, TCTX_POINTER_ARITH,
4921 TREE_TYPE (argtype));
4923 inc = c_size_in_bytes (TREE_TYPE (argtype));
4924 inc = convert_to_ptrofftype_loc (location, inc);
4926 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4928 /* For signed fract types, we invert ++ to -- or
4929 -- to ++, and change inc from 1 to -1, because
4930 it is not possible to represent 1 in signed fract constants.
4931 For unsigned fract types, the result always overflows and
4932 we get an undefined (original) or the maximum value. */
4933 if (code == PREINCREMENT_EXPR)
4934 code = PREDECREMENT_EXPR;
4935 else if (code == PREDECREMENT_EXPR)
4936 code = PREINCREMENT_EXPR;
4937 else if (code == POSTINCREMENT_EXPR)
4938 code = POSTDECREMENT_EXPR;
4939 else /* code == POSTDECREMENT_EXPR */
4940 code = POSTINCREMENT_EXPR;
4942 inc = integer_minus_one_node;
4943 inc = convert (argtype, inc);
4945 else
4947 inc = VECTOR_TYPE_P (argtype)
4948 ? build_one_cst (argtype)
4949 : integer_one_node;
4950 inc = convert (argtype, inc);
4953 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4954 need to ask Objective-C to build the increment or decrement
4955 expression for it. */
4956 if (objc_is_property_ref (arg))
4957 return objc_build_incr_expr_for_property_ref (location, code,
4958 arg, inc);
4960 /* Report a read-only lvalue. */
4961 if (TYPE_READONLY (argtype))
4963 readonly_error (location, arg,
4964 ((code == PREINCREMENT_EXPR
4965 || code == POSTINCREMENT_EXPR)
4966 ? lv_increment : lv_decrement));
4967 return error_mark_node;
4969 else if (TREE_READONLY (arg))
4970 readonly_warning (arg,
4971 ((code == PREINCREMENT_EXPR
4972 || code == POSTINCREMENT_EXPR)
4973 ? lv_increment : lv_decrement));
4975 /* If the argument is atomic, use the special code sequences for
4976 atomic compound assignment. */
4977 if (atomic_op)
4979 arg = stabilize_reference (arg);
4980 ret = build_atomic_assign (location, arg,
4981 ((code == PREINCREMENT_EXPR
4982 || code == POSTINCREMENT_EXPR)
4983 ? PLUS_EXPR
4984 : MINUS_EXPR),
4985 (FRACT_MODE_P (TYPE_MODE (argtype))
4986 ? inc
4987 : integer_one_node),
4988 (code == POSTINCREMENT_EXPR
4989 || code == POSTDECREMENT_EXPR));
4990 goto return_build_unary_op;
4993 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
4994 val = boolean_increment (code, arg);
4995 else
4996 val = build2 (code, TREE_TYPE (arg), arg, inc);
4997 TREE_SIDE_EFFECTS (val) = 1;
4998 if (TYPE_QUALS (TREE_TYPE (val)) != TYPE_UNQUALIFIED)
4999 TREE_TYPE (val) = c_build_qualified_type (TREE_TYPE (val),
5000 TYPE_UNQUALIFIED);
5001 ret = val;
5002 goto return_build_unary_op;
5005 case ADDR_EXPR:
5006 /* Note that this operation never does default_conversion. */
5008 /* The operand of unary '&' must be an lvalue (which excludes
5009 expressions of type void), or, in C99, the result of a [] or
5010 unary '*' operator. */
5011 if (VOID_TYPE_P (TREE_TYPE (arg))
5012 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
5013 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
5014 pedwarn (location, 0, "taking address of expression of type %<void%>");
5016 /* Let &* cancel out to simplify resulting code. */
5017 if (INDIRECT_REF_P (arg))
5019 /* Don't let this be an lvalue. */
5020 if (lvalue_p (TREE_OPERAND (arg, 0)))
5021 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
5022 ret = TREE_OPERAND (arg, 0);
5023 goto return_build_unary_op;
5026 /* Anything not already handled and not a true memory reference
5027 or a non-lvalue array is an error. */
5028 if (typecode != FUNCTION_TYPE && !noconvert
5029 && !lvalue_or_else (location, arg, lv_addressof))
5030 return error_mark_node;
5032 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
5033 folding later. */
5034 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
5036 tree inner = build_unary_op (location, code,
5037 C_MAYBE_CONST_EXPR_EXPR (arg),
5038 noconvert);
5039 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5040 C_MAYBE_CONST_EXPR_PRE (arg), inner);
5041 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
5042 C_MAYBE_CONST_EXPR_NON_CONST (ret)
5043 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
5044 goto return_build_unary_op;
5047 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
5048 argtype = TREE_TYPE (arg);
5050 /* If the lvalue is const or volatile, merge that into the type
5051 to which the address will point. This is only needed
5052 for function types. */
5053 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
5054 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
5055 && TREE_CODE (argtype) == FUNCTION_TYPE)
5057 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
5058 int quals = orig_quals;
5060 if (TREE_READONLY (arg))
5061 quals |= TYPE_QUAL_CONST;
5062 if (TREE_THIS_VOLATILE (arg))
5063 quals |= TYPE_QUAL_VOLATILE;
5065 argtype = c_build_qualified_type (argtype, quals);
5068 switch (TREE_CODE (arg))
5070 case COMPONENT_REF:
5071 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5073 error_at (location, "cannot take address of bit-field %qD",
5074 TREE_OPERAND (arg, 1));
5075 return error_mark_node;
5078 /* fall through */
5080 case ARRAY_REF:
5081 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
5083 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
5084 && !POINTER_TYPE_P (TREE_TYPE (arg))
5085 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
5087 error_at (location, "cannot take address of scalar with "
5088 "reverse storage order");
5089 return error_mark_node;
5092 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
5093 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
5094 warning_at (location, OPT_Wscalar_storage_order,
5095 "address of array with reverse scalar storage "
5096 "order requested");
5099 default:
5100 break;
5103 if (!c_mark_addressable (arg))
5104 return error_mark_node;
5106 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
5107 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
5109 argtype = build_pointer_type (argtype);
5111 /* ??? Cope with user tricks that amount to offsetof. Delete this
5112 when we have proper support for integer constant expressions. */
5113 val = get_base_address (arg);
5114 if (val && INDIRECT_REF_P (val)
5115 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5117 ret = fold_offsetof (arg, argtype);
5118 goto return_build_unary_op;
5121 val = build1 (ADDR_EXPR, argtype, arg);
5123 ret = val;
5124 goto return_build_unary_op;
5126 case PAREN_EXPR:
5127 ret = build1 (code, TREE_TYPE (arg), arg);
5128 goto return_build_unary_op;
5130 default:
5131 gcc_unreachable ();
5134 if (argtype == NULL_TREE)
5135 argtype = TREE_TYPE (arg);
5136 if (TREE_CODE (arg) == INTEGER_CST)
5137 ret = (require_constant_value
5138 ? fold_build1_initializer_loc (location, code, argtype, arg)
5139 : fold_build1_loc (location, code, argtype, arg));
5140 else
5141 ret = build1 (code, argtype, arg);
5142 return_build_unary_op:
5143 gcc_assert (ret != error_mark_node);
5144 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
5145 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
5146 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
5147 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
5148 ret = note_integer_operands (ret);
5149 if (eptype)
5150 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5151 protected_set_expr_location (ret, location);
5152 return ret;
5155 /* Return nonzero if REF is an lvalue valid for this language.
5156 Lvalues can be assigned, unless their type has TYPE_READONLY.
5157 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
5159 bool
5160 lvalue_p (const_tree ref)
5162 const enum tree_code code = TREE_CODE (ref);
5164 switch (code)
5166 case REALPART_EXPR:
5167 case IMAGPART_EXPR:
5168 case COMPONENT_REF:
5169 return lvalue_p (TREE_OPERAND (ref, 0));
5171 case C_MAYBE_CONST_EXPR:
5172 return lvalue_p (TREE_OPERAND (ref, 1));
5174 case COMPOUND_LITERAL_EXPR:
5175 case STRING_CST:
5176 return true;
5178 case MEM_REF:
5179 case TARGET_MEM_REF:
5180 /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
5181 here as well. */
5182 case INDIRECT_REF:
5183 case ARRAY_REF:
5184 case VAR_DECL:
5185 case PARM_DECL:
5186 case RESULT_DECL:
5187 case ERROR_MARK:
5188 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
5189 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
5191 case BIND_EXPR:
5192 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
5194 default:
5195 return false;
5199 /* Give a warning for storing in something that is read-only in GCC
5200 terms but not const in ISO C terms. */
5202 static void
5203 readonly_warning (tree arg, enum lvalue_use use)
5205 switch (use)
5207 case lv_assign:
5208 warning (0, "assignment of read-only location %qE", arg);
5209 break;
5210 case lv_increment:
5211 warning (0, "increment of read-only location %qE", arg);
5212 break;
5213 case lv_decrement:
5214 warning (0, "decrement of read-only location %qE", arg);
5215 break;
5216 default:
5217 gcc_unreachable ();
5219 return;
5223 /* Return nonzero if REF is an lvalue valid for this language;
5224 otherwise, print an error message and return zero. USE says
5225 how the lvalue is being used and so selects the error message.
5226 LOCATION is the location at which any error should be reported. */
5228 static int
5229 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
5231 int win = lvalue_p (ref);
5233 if (!win)
5234 lvalue_error (loc, use);
5236 return win;
5239 /* Mark EXP saying that we need to be able to take the
5240 address of it; it should not be allocated in a register.
5241 Returns true if successful. ARRAY_REF_P is true if this
5242 is for ARRAY_REF construction - in that case we don't want
5243 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5244 it is fine to use ARRAY_REFs for vector subscripts on vector
5245 register variables. */
5247 bool
5248 c_mark_addressable (tree exp, bool array_ref_p)
5250 tree x = exp;
5252 while (1)
5253 switch (TREE_CODE (x))
5255 case VIEW_CONVERT_EXPR:
5256 if (array_ref_p
5257 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5258 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5259 return true;
5260 x = TREE_OPERAND (x, 0);
5261 break;
5263 case COMPONENT_REF:
5264 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
5266 error ("cannot take address of bit-field %qD",
5267 TREE_OPERAND (x, 1));
5268 return false;
5270 /* FALLTHRU */
5271 case ADDR_EXPR:
5272 case ARRAY_REF:
5273 case REALPART_EXPR:
5274 case IMAGPART_EXPR:
5275 x = TREE_OPERAND (x, 0);
5276 break;
5278 case COMPOUND_LITERAL_EXPR:
5279 if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)))
5281 error ("address of register compound literal requested");
5282 return false;
5284 TREE_ADDRESSABLE (x) = 1;
5285 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5286 return true;
5288 case CONSTRUCTOR:
5289 TREE_ADDRESSABLE (x) = 1;
5290 return true;
5292 case VAR_DECL:
5293 case CONST_DECL:
5294 case PARM_DECL:
5295 case RESULT_DECL:
5296 if (C_DECL_REGISTER (x)
5297 && DECL_NONLOCAL (x))
5299 if (TREE_PUBLIC (x) || is_global_var (x))
5301 error
5302 ("global register variable %qD used in nested function", x);
5303 return false;
5305 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5307 else if (C_DECL_REGISTER (x))
5309 if (TREE_PUBLIC (x) || is_global_var (x))
5310 error ("address of global register variable %qD requested", x);
5311 else
5312 error ("address of register variable %qD requested", x);
5313 return false;
5316 /* FALLTHRU */
5317 case FUNCTION_DECL:
5318 TREE_ADDRESSABLE (x) = 1;
5319 /* FALLTHRU */
5320 default:
5321 return true;
5325 /* Convert EXPR to TYPE, warning about conversion problems with
5326 constants. SEMANTIC_TYPE is the type this conversion would use
5327 without excess precision. If SEMANTIC_TYPE is NULL, this function
5328 is equivalent to convert_and_check. This function is a wrapper that
5329 handles conversions that may be different than
5330 the usual ones because of excess precision. */
5332 static tree
5333 ep_convert_and_check (location_t loc, tree type, tree expr,
5334 tree semantic_type)
5336 if (TREE_TYPE (expr) == type)
5337 return expr;
5339 /* For C11, integer conversions may have results with excess
5340 precision. */
5341 if (flag_isoc11 || !semantic_type)
5342 return convert_and_check (loc, type, expr);
5344 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5345 && TREE_TYPE (expr) != semantic_type)
5347 /* For integers, we need to check the real conversion, not
5348 the conversion to the excess precision type. */
5349 expr = convert_and_check (loc, semantic_type, expr);
5351 /* Result type is the excess precision type, which should be
5352 large enough, so do not check. */
5353 return convert (type, expr);
5356 /* If EXPR refers to a built-in declared without a prototype returns
5357 the actual type of the built-in and, if non-null, set *BLTIN to
5358 a pointer to the built-in. Otherwise return the type of EXPR
5359 and clear *BLTIN if non-null. */
5361 static tree
5362 type_or_builtin_type (tree expr, tree *bltin = NULL)
5364 tree dummy;
5365 if (!bltin)
5366 bltin = &dummy;
5368 *bltin = NULL_TREE;
5370 tree type = TREE_TYPE (expr);
5371 if (TREE_CODE (expr) != ADDR_EXPR)
5372 return type;
5374 tree oper = TREE_OPERAND (expr, 0);
5375 if (!DECL_P (oper)
5376 || TREE_CODE (oper) != FUNCTION_DECL
5377 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5378 return type;
5380 built_in_function code = DECL_FUNCTION_CODE (oper);
5381 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5382 return type;
5384 if ((*bltin = builtin_decl_implicit (code)))
5385 type = build_pointer_type (TREE_TYPE (*bltin));
5387 return type;
5390 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5391 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5392 if folded to an integer constant then the unselected half may
5393 contain arbitrary operations not normally permitted in constant
5394 expressions. Set the location of the expression to LOC. */
5396 tree
5397 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5398 tree op1, tree op1_original_type, location_t op1_loc,
5399 tree op2, tree op2_original_type, location_t op2_loc)
5401 tree type1;
5402 tree type2;
5403 enum tree_code code1;
5404 enum tree_code code2;
5405 tree result_type = NULL;
5406 tree semantic_result_type = NULL;
5407 tree orig_op1 = op1, orig_op2 = op2;
5408 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5409 bool ifexp_int_operands;
5410 tree ret;
5412 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5413 if (op1_int_operands)
5414 op1 = remove_c_maybe_const_expr (op1);
5415 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5416 if (op2_int_operands)
5417 op2 = remove_c_maybe_const_expr (op2);
5418 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5419 if (ifexp_int_operands)
5420 ifexp = remove_c_maybe_const_expr (ifexp);
5422 /* Promote both alternatives. */
5424 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5425 op1 = default_conversion (op1);
5426 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5427 op2 = default_conversion (op2);
5429 if (TREE_CODE (ifexp) == ERROR_MARK
5430 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5431 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5432 return error_mark_node;
5434 tree bltin1 = NULL_TREE;
5435 tree bltin2 = NULL_TREE;
5436 type1 = type_or_builtin_type (op1, &bltin1);
5437 code1 = TREE_CODE (type1);
5438 type2 = type_or_builtin_type (op2, &bltin2);
5439 code2 = TREE_CODE (type2);
5441 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5442 return error_mark_node;
5444 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5445 return error_mark_node;
5447 /* C90 does not permit non-lvalue arrays in conditional expressions.
5448 In C99 they will be pointers by now. */
5449 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5451 error_at (colon_loc, "non-lvalue array in conditional expression");
5452 return error_mark_node;
5455 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5456 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5457 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5458 || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
5459 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5460 || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
5462 semantic_result_type = c_common_type (type1, type2);
5463 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5465 op1 = TREE_OPERAND (op1, 0);
5466 type1 = TREE_TYPE (op1);
5467 gcc_assert (TREE_CODE (type1) == code1);
5469 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5471 op2 = TREE_OPERAND (op2, 0);
5472 type2 = TREE_TYPE (op2);
5473 gcc_assert (TREE_CODE (type2) == code2);
5477 if (warn_cxx_compat)
5479 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5480 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5482 if (TREE_CODE (t1) == ENUMERAL_TYPE
5483 && TREE_CODE (t2) == ENUMERAL_TYPE
5484 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5485 warning_at (colon_loc, OPT_Wc___compat,
5486 ("different enum types in conditional is "
5487 "invalid in C++: %qT vs %qT"),
5488 t1, t2);
5491 /* Quickly detect the usual case where op1 and op2 have the same type
5492 after promotion. */
5493 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5495 if (type1 == type2)
5496 result_type = type1;
5497 else
5498 result_type = TYPE_MAIN_VARIANT (type1);
5500 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5501 || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
5502 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5503 || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
5505 /* In C11, a conditional expression between a floating-point
5506 type and an integer type should convert the integer type to
5507 the evaluation format of the floating-point type, with
5508 possible excess precision. */
5509 tree eptype1 = type1;
5510 tree eptype2 = type2;
5511 if (flag_isoc11)
5513 tree eptype;
5514 if (ANY_INTEGRAL_TYPE_P (type1)
5515 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5517 eptype2 = eptype;
5518 if (!semantic_result_type)
5519 semantic_result_type = c_common_type (type1, type2);
5521 else if (ANY_INTEGRAL_TYPE_P (type2)
5522 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5524 eptype1 = eptype;
5525 if (!semantic_result_type)
5526 semantic_result_type = c_common_type (type1, type2);
5529 result_type = c_common_type (eptype1, eptype2);
5530 if (result_type == error_mark_node)
5531 return error_mark_node;
5532 do_warn_double_promotion (result_type, type1, type2,
5533 "implicit conversion from %qT to %qT to "
5534 "match other result of conditional",
5535 colon_loc);
5537 /* If -Wsign-compare, warn here if type1 and type2 have
5538 different signedness. We'll promote the signed to unsigned
5539 and later code won't know it used to be different.
5540 Do this check on the original types, so that explicit casts
5541 will be considered, but default promotions won't. */
5542 if (c_inhibit_evaluation_warnings == 0)
5544 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5545 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5547 if (unsigned_op1 ^ unsigned_op2)
5549 bool ovf;
5551 /* Do not warn if the result type is signed, since the
5552 signed type will only be chosen if it can represent
5553 all the values of the unsigned type. */
5554 if (!TYPE_UNSIGNED (result_type))
5555 /* OK */;
5556 else
5558 bool op1_maybe_const = true;
5559 bool op2_maybe_const = true;
5561 /* Do not warn if the signed quantity is an
5562 unsuffixed integer literal (or some static
5563 constant expression involving such literals) and
5564 it is non-negative. This warning requires the
5565 operands to be folded for best results, so do
5566 that folding in this case even without
5567 warn_sign_compare to avoid warning options
5568 possibly affecting code generation. */
5569 c_inhibit_evaluation_warnings
5570 += (ifexp == truthvalue_false_node);
5571 op1 = c_fully_fold (op1, require_constant_value,
5572 &op1_maybe_const);
5573 c_inhibit_evaluation_warnings
5574 -= (ifexp == truthvalue_false_node);
5576 c_inhibit_evaluation_warnings
5577 += (ifexp == truthvalue_true_node);
5578 op2 = c_fully_fold (op2, require_constant_value,
5579 &op2_maybe_const);
5580 c_inhibit_evaluation_warnings
5581 -= (ifexp == truthvalue_true_node);
5583 if (warn_sign_compare)
5585 if ((unsigned_op2
5586 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5587 || (unsigned_op1
5588 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5589 /* OK */;
5590 else if (unsigned_op2)
5591 warning_at (op1_loc, OPT_Wsign_compare,
5592 "operand of %<?:%> changes signedness from "
5593 "%qT to %qT due to unsignedness of other "
5594 "operand", TREE_TYPE (orig_op1),
5595 TREE_TYPE (orig_op2));
5596 else
5597 warning_at (op2_loc, OPT_Wsign_compare,
5598 "operand of %<?:%> changes signedness from "
5599 "%qT to %qT due to unsignedness of other "
5600 "operand", TREE_TYPE (orig_op2),
5601 TREE_TYPE (orig_op1));
5603 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5604 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5605 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5606 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5611 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5613 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5614 pedwarn (colon_loc, OPT_Wpedantic,
5615 "ISO C forbids conditional expr with only one void side");
5616 result_type = void_type_node;
5618 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5620 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5621 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5622 addr_space_t as_common;
5624 if (comp_target_types (colon_loc, type1, type2))
5625 result_type = common_pointer_type (type1, type2);
5626 else if (null_pointer_constant_p (orig_op1))
5627 result_type = type2;
5628 else if (null_pointer_constant_p (orig_op2))
5629 result_type = type1;
5630 else if (!addr_space_superset (as1, as2, &as_common))
5632 error_at (colon_loc, "pointers to disjoint address spaces "
5633 "used in conditional expression");
5634 return error_mark_node;
5636 else if ((VOID_TYPE_P (TREE_TYPE (type1))
5637 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5638 || (VOID_TYPE_P (TREE_TYPE (type2))
5639 && !TYPE_ATOMIC (TREE_TYPE (type2))))
5641 tree t1 = TREE_TYPE (type1);
5642 tree t2 = TREE_TYPE (type2);
5643 if (!(VOID_TYPE_P (t1)
5644 && !TYPE_ATOMIC (t1)))
5646 /* roles are swapped */
5647 t1 = t2;
5648 t2 = TREE_TYPE (type1);
5650 tree t2_stripped = strip_array_types (t2);
5651 if ((TREE_CODE (t2) == ARRAY_TYPE)
5652 && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
5654 if (!flag_isoc23)
5655 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5656 "pointer to array loses qualifier "
5657 "in conditional expression");
5658 else if (warn_c11_c23_compat > 0)
5659 warning_at (colon_loc, OPT_Wc11_c23_compat,
5660 "pointer to array loses qualifier "
5661 "in conditional expression in ISO C before C23");
5663 if (TREE_CODE (t2) == FUNCTION_TYPE)
5664 pedwarn (colon_loc, OPT_Wpedantic,
5665 "ISO C forbids conditional expr between "
5666 "%<void *%> and function pointer");
5667 /* for array, use qualifiers of element type */
5668 if (flag_isoc23)
5669 t2 = t2_stripped;
5670 result_type = build_pointer_type (qualify_type (t1, t2));
5672 /* Objective-C pointer comparisons are a bit more lenient. */
5673 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5674 result_type = objc_common_type (type1, type2);
5675 else
5677 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5678 diagnostic_t kind = DK_PERMERROR;
5679 if (!flag_isoc99)
5680 /* This downgrade to a warning ensures that -std=gnu89
5681 -pedantic-errors does not flag these mismatches between
5682 builtins as errors (as DK_PERMERROR would). ISO C99
5683 and later do not have implicit function declarations,
5684 so the mismatch cannot occur naturally there. */
5685 kind = bltin1 && bltin2 ? DK_WARNING : DK_PEDWARN;
5686 if (emit_diagnostic (kind, colon_loc, OPT_Wincompatible_pointer_types,
5687 "pointer type mismatch "
5688 "in conditional expression"))
5690 inform (op1_loc, "first expression has type %qT", type1);
5691 inform (op2_loc, "second expression has type %qT", type2);
5693 result_type = build_pointer_type
5694 (build_qualified_type (void_type_node, qual));
5697 else if (code1 == POINTER_TYPE
5698 && (code2 == INTEGER_TYPE || code2 == BITINT_TYPE))
5700 if (!null_pointer_constant_p (orig_op2))
5701 permerror_opt (colon_loc, OPT_Wint_conversion,
5702 "pointer/integer type mismatch "
5703 "in conditional expression");
5704 else
5706 op2 = null_pointer_node;
5708 result_type = type1;
5710 else if (code2 == POINTER_TYPE
5711 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
5713 if (!null_pointer_constant_p (orig_op1))
5714 permerror_opt (colon_loc, OPT_Wint_conversion,
5715 "pointer/integer type mismatch "
5716 "in conditional expression");
5717 else
5719 op1 = null_pointer_node;
5721 result_type = type2;
5723 /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
5724 type nullptr_t and the other is a pointer, the result type is the pointer
5725 type." */
5726 else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE)
5727 result_type = type2;
5728 else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE)
5729 result_type = type1;
5730 else if (RECORD_OR_UNION_TYPE_P (type1) && RECORD_OR_UNION_TYPE_P (type2)
5731 && comptypes (TYPE_MAIN_VARIANT (type1),
5732 TYPE_MAIN_VARIANT (type2)))
5733 result_type = composite_type (TYPE_MAIN_VARIANT (type1),
5734 TYPE_MAIN_VARIANT (type2));
5736 if (!result_type)
5738 if (flag_cond_mismatch)
5739 result_type = void_type_node;
5740 else
5742 error_at (colon_loc, "type mismatch in conditional expression");
5743 return error_mark_node;
5747 /* Merge const and volatile flags of the incoming types. */
5748 result_type
5749 = build_type_variant (result_type,
5750 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5751 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5753 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5754 semantic_result_type);
5755 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5756 semantic_result_type);
5758 if (ifexp_bcp && ifexp == truthvalue_true_node)
5760 op2_int_operands = true;
5761 op1 = c_fully_fold (op1, require_constant_value, NULL);
5763 if (ifexp_bcp && ifexp == truthvalue_false_node)
5765 op1_int_operands = true;
5766 op2 = c_fully_fold (op2, require_constant_value, NULL);
5768 int_const = int_operands = (ifexp_int_operands
5769 && op1_int_operands
5770 && op2_int_operands);
5771 if (int_operands)
5773 int_const = ((ifexp == truthvalue_true_node
5774 && TREE_CODE (orig_op1) == INTEGER_CST
5775 && !TREE_OVERFLOW (orig_op1))
5776 || (ifexp == truthvalue_false_node
5777 && TREE_CODE (orig_op2) == INTEGER_CST
5778 && !TREE_OVERFLOW (orig_op2)));
5781 /* Need to convert condition operand into a vector mask. */
5782 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5784 tree vectype = TREE_TYPE (ifexp);
5785 tree elem_type = TREE_TYPE (vectype);
5786 tree zero = build_int_cst (elem_type, 0);
5787 tree zero_vec = build_vector_from_val (vectype, zero);
5788 tree cmp_type = truth_type_for (vectype);
5789 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5792 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5793 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5794 else
5796 if (int_operands)
5798 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5799 nested inside of the expression. */
5800 op1 = c_fully_fold (op1, false, NULL);
5801 op2 = c_fully_fold (op2, false, NULL);
5803 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5804 if (int_operands)
5805 ret = note_integer_operands (ret);
5807 if (semantic_result_type)
5808 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5810 protected_set_expr_location (ret, colon_loc);
5812 /* If the OP1 and OP2 are the same and don't have side-effects,
5813 warn here, because the COND_EXPR will be turned into OP1. */
5814 if (warn_duplicated_branches
5815 && TREE_CODE (ret) == COND_EXPR
5816 && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
5817 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5818 "this condition has identical branches");
5820 return ret;
5823 /* EXPR is an expression, location LOC, whose result is discarded.
5824 Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
5825 whose right-hand operand is such a call, possibly recursively). */
5827 static void
5828 maybe_warn_nodiscard (location_t loc, tree expr)
5830 if (VOID_TYPE_P (TREE_TYPE (expr)))
5831 return;
5832 while (TREE_CODE (expr) == COMPOUND_EXPR)
5834 expr = TREE_OPERAND (expr, 1);
5835 if (EXPR_HAS_LOCATION (expr))
5836 loc = EXPR_LOCATION (expr);
5838 if (TREE_CODE (expr) != CALL_EXPR)
5839 return;
5840 tree fn = CALL_EXPR_FN (expr);
5841 if (!fn)
5842 return;
5843 tree attr;
5844 if (TREE_CODE (fn) == ADDR_EXPR
5845 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5846 && (attr = lookup_attribute ("nodiscard",
5847 DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
5849 fn = TREE_OPERAND (fn, 0);
5850 tree args = TREE_VALUE (attr);
5851 if (args)
5852 args = TREE_VALUE (args);
5853 auto_diagnostic_group d;
5854 int warned;
5855 if (args)
5856 warned = warning_at (loc, OPT_Wunused_result,
5857 "ignoring return value of %qD, declared with "
5858 "attribute %<nodiscard%>: %E", fn, args);
5859 else
5860 warned = warning_at (loc, OPT_Wunused_result,
5861 "ignoring return value of %qD, declared with "
5862 "attribute %<nodiscard%>", fn);
5863 if (warned)
5864 inform (DECL_SOURCE_LOCATION (fn), "declared here");
5866 else
5868 tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
5869 attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
5870 if (!attr)
5871 return;
5872 tree args = TREE_VALUE (attr);
5873 if (args)
5874 args = TREE_VALUE (args);
5875 auto_diagnostic_group d;
5876 int warned;
5877 if (args)
5878 warned = warning_at (loc, OPT_Wunused_result,
5879 "ignoring return value of type %qT, declared "
5880 "with attribute %<nodiscard%>: %E",
5881 rettype, args);
5882 else
5883 warned = warning_at (loc, OPT_Wunused_result,
5884 "ignoring return value of type %qT, declared "
5885 "with attribute %<nodiscard%>", rettype);
5886 if (warned)
5888 if (TREE_CODE (fn) == ADDR_EXPR)
5890 fn = TREE_OPERAND (fn, 0);
5891 if (TREE_CODE (fn) == FUNCTION_DECL)
5892 inform (DECL_SOURCE_LOCATION (fn),
5893 "in call to %qD, declared here", fn);
5899 /* Return a compound expression that performs two expressions and
5900 returns the value of the second of them.
5902 LOC is the location of the COMPOUND_EXPR. */
5904 tree
5905 build_compound_expr (location_t loc, tree expr1, tree expr2)
5907 bool expr1_int_operands, expr2_int_operands;
5908 tree eptype = NULL_TREE;
5909 tree ret;
5911 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5912 if (expr1_int_operands)
5913 expr1 = remove_c_maybe_const_expr (expr1);
5914 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5915 if (expr2_int_operands)
5916 expr2 = remove_c_maybe_const_expr (expr2);
5918 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5919 expr1 = TREE_OPERAND (expr1, 0);
5920 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5922 eptype = TREE_TYPE (expr2);
5923 expr2 = TREE_OPERAND (expr2, 0);
5926 if (!TREE_SIDE_EFFECTS (expr1))
5928 /* The left-hand operand of a comma expression is like an expression
5929 statement: with -Wunused, we should warn if it doesn't have
5930 any side-effects, unless it was explicitly cast to (void). */
5931 if (warn_unused_value)
5933 if (VOID_TYPE_P (TREE_TYPE (expr1))
5934 && CONVERT_EXPR_P (expr1))
5935 ; /* (void) a, b */
5936 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5937 && TREE_CODE (expr1) == COMPOUND_EXPR
5938 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5939 ; /* (void) a, (void) b, c */
5940 else
5941 warning_at (loc, OPT_Wunused_value,
5942 "left-hand operand of comma expression has no effect");
5945 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5946 && warn_unused_value)
5948 tree r = expr1;
5949 location_t cloc = loc;
5950 while (TREE_CODE (r) == COMPOUND_EXPR)
5952 if (EXPR_HAS_LOCATION (r))
5953 cloc = EXPR_LOCATION (r);
5954 r = TREE_OPERAND (r, 1);
5956 if (!TREE_SIDE_EFFECTS (r)
5957 && !VOID_TYPE_P (TREE_TYPE (r))
5958 && !CONVERT_EXPR_P (r))
5959 warning_at (cloc, OPT_Wunused_value,
5960 "right-hand operand of comma expression has no effect");
5963 /* With -Wunused, we should also warn if the left-hand operand does have
5964 side-effects, but computes a value which is not used. For example, in
5965 `foo() + bar(), baz()' the result of the `+' operator is not used,
5966 so we should issue a warning. */
5967 else if (warn_unused_value)
5968 warn_if_unused_value (expr1, loc);
5970 maybe_warn_nodiscard (loc, expr1);
5972 if (expr2 == error_mark_node)
5973 return error_mark_node;
5975 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5977 if (flag_isoc99
5978 && expr1_int_operands
5979 && expr2_int_operands)
5980 ret = note_integer_operands (ret);
5982 if (eptype)
5983 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5985 protected_set_expr_location (ret, loc);
5986 return ret;
5989 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5990 which we are casting. OTYPE is the type of the expression being
5991 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5992 of the cast. -Wcast-qual appeared on the command line. Named
5993 address space qualifiers are not handled here, because they result
5994 in different warnings. */
5996 static void
5997 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5999 tree in_type = type;
6000 tree in_otype = otype;
6001 int added = 0;
6002 int discarded = 0;
6003 bool is_const;
6005 /* Check that the qualifiers on IN_TYPE are a superset of the
6006 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
6007 nodes is uninteresting and we stop as soon as we hit a
6008 non-POINTER_TYPE node on either type. */
6011 in_otype = TREE_TYPE (in_otype);
6012 in_type = TREE_TYPE (in_type);
6014 /* GNU C allows cv-qualified function types. 'const' means the
6015 function is very pure, 'volatile' means it can't return. We
6016 need to warn when such qualifiers are added, not when they're
6017 taken away. */
6018 if (TREE_CODE (in_otype) == FUNCTION_TYPE
6019 && TREE_CODE (in_type) == FUNCTION_TYPE)
6020 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
6021 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
6022 else
6023 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
6024 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
6026 while (TREE_CODE (in_type) == POINTER_TYPE
6027 && TREE_CODE (in_otype) == POINTER_TYPE);
6029 if (added)
6030 warning_at (loc, OPT_Wcast_qual,
6031 "cast adds %q#v qualifier to function type", added);
6033 if (discarded)
6034 /* There are qualifiers present in IN_OTYPE that are not present
6035 in IN_TYPE. */
6036 warning_at (loc, OPT_Wcast_qual,
6037 "cast discards %qv qualifier from pointer target type",
6038 discarded);
6040 if (added || discarded)
6041 return;
6043 /* A cast from **T to const **T is unsafe, because it can cause a
6044 const value to be changed with no additional warning. We only
6045 issue this warning if T is the same on both sides, and we only
6046 issue the warning if there are the same number of pointers on
6047 both sides, as otherwise the cast is clearly unsafe anyhow. A
6048 cast is unsafe when a qualifier is added at one level and const
6049 is not present at all outer levels.
6051 To issue this warning, we check at each level whether the cast
6052 adds new qualifiers not already seen. We don't need to special
6053 case function types, as they won't have the same
6054 TYPE_MAIN_VARIANT. */
6056 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
6057 return;
6058 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
6059 return;
6061 in_type = type;
6062 in_otype = otype;
6063 is_const = TYPE_READONLY (TREE_TYPE (in_type));
6066 in_type = TREE_TYPE (in_type);
6067 in_otype = TREE_TYPE (in_otype);
6068 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
6069 && !is_const)
6071 warning_at (loc, OPT_Wcast_qual,
6072 "to be safe all intermediate pointers in cast from "
6073 "%qT to %qT must be %<const%> qualified",
6074 otype, type);
6075 break;
6077 if (is_const)
6078 is_const = TYPE_READONLY (in_type);
6080 while (TREE_CODE (in_type) == POINTER_TYPE);
6083 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
6085 static bool
6086 c_safe_arg_type_equiv_p (tree t1, tree t2)
6088 if (error_operand_p (t1) || error_operand_p (t2))
6089 return true;
6091 t1 = TYPE_MAIN_VARIANT (t1);
6092 t2 = TYPE_MAIN_VARIANT (t2);
6094 if (TREE_CODE (t1) == POINTER_TYPE
6095 && TREE_CODE (t2) == POINTER_TYPE)
6096 return true;
6098 /* The signedness of the parameter matters only when an integral
6099 type smaller than int is promoted to int, otherwise only the
6100 precision of the parameter matters.
6101 This check should make sure that the callee does not see
6102 undefined values in argument registers. */
6103 if (INTEGRAL_TYPE_P (t1)
6104 && INTEGRAL_TYPE_P (t2)
6105 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
6106 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
6107 || !targetm.calls.promote_prototypes (NULL_TREE)
6108 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
6109 return true;
6111 return comptypes (t1, t2);
6114 /* Check if a type cast between two function types can be considered safe. */
6116 static bool
6117 c_safe_function_type_cast_p (tree t1, tree t2)
6119 if (TREE_TYPE (t1) == void_type_node &&
6120 TYPE_ARG_TYPES (t1) == void_list_node)
6121 return true;
6123 if (TREE_TYPE (t2) == void_type_node &&
6124 TYPE_ARG_TYPES (t2) == void_list_node)
6125 return true;
6127 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
6128 return false;
6130 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
6131 t1 && t2;
6132 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6133 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
6134 return false;
6136 return true;
6139 /* Build an expression representing a cast to type TYPE of expression EXPR.
6140 LOC is the location of the cast-- typically the open paren of the cast. */
6142 tree
6143 build_c_cast (location_t loc, tree type, tree expr)
6145 tree value;
6147 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
6149 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
6150 expr = TREE_OPERAND (expr, 0);
6152 value = expr;
6153 if (int_operands)
6154 value = remove_c_maybe_const_expr (value);
6156 if (type == error_mark_node || expr == error_mark_node)
6157 return error_mark_node;
6159 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
6160 only in <protocol> qualifications. But when constructing cast expressions,
6161 the protocols do matter and must be kept around. */
6162 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
6163 return build1 (NOP_EXPR, type, expr);
6165 type = TYPE_MAIN_VARIANT (type);
6167 if (TREE_CODE (type) == ARRAY_TYPE)
6169 error_at (loc, "cast specifies array type");
6170 return error_mark_node;
6173 if (TREE_CODE (type) == FUNCTION_TYPE)
6175 error_at (loc, "cast specifies function type");
6176 return error_mark_node;
6179 if (!VOID_TYPE_P (type))
6181 value = require_complete_type (loc, value);
6182 if (value == error_mark_node)
6183 return error_mark_node;
6186 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
6188 if (RECORD_OR_UNION_TYPE_P (type)
6189 && pedwarn (loc, OPT_Wpedantic,
6190 "ISO C forbids casting nonscalar to the same type"))
6192 else if (warn_useless_cast)
6193 warning_at (loc, OPT_Wuseless_cast,
6194 "useless cast to type %qT", type);
6196 /* Convert to remove any qualifiers from VALUE's type. */
6197 value = convert (type, value);
6199 else if (TREE_CODE (type) == UNION_TYPE)
6201 tree field;
6203 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6204 if (TREE_TYPE (field) != error_mark_node
6205 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
6206 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
6207 break;
6209 if (field)
6211 tree t;
6212 bool maybe_const = true;
6214 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
6215 t = c_fully_fold (value, false, &maybe_const);
6216 t = build_constructor_single (type, field, t);
6217 if (!maybe_const)
6218 t = c_wrap_maybe_const (t, true);
6219 t = digest_init (loc, type, t,
6220 NULL_TREE, false, false, false, true, false, false);
6221 TREE_CONSTANT (t) = TREE_CONSTANT (value);
6222 return t;
6224 error_at (loc, "cast to union type from type not present in union");
6225 return error_mark_node;
6227 else
6229 tree otype, ovalue;
6231 if (type == void_type_node)
6233 tree t = build1 (CONVERT_EXPR, type, value);
6234 SET_EXPR_LOCATION (t, loc);
6235 return t;
6238 otype = TREE_TYPE (value);
6240 /* Optionally warn about potentially worrisome casts. */
6241 if (warn_cast_qual
6242 && TREE_CODE (type) == POINTER_TYPE
6243 && TREE_CODE (otype) == POINTER_TYPE)
6244 handle_warn_cast_qual (loc, type, otype);
6246 /* Warn about conversions between pointers to disjoint
6247 address spaces. */
6248 if (TREE_CODE (type) == POINTER_TYPE
6249 && TREE_CODE (otype) == POINTER_TYPE
6250 && !null_pointer_constant_p (value))
6252 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
6253 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
6254 addr_space_t as_common;
6256 if (!addr_space_superset (as_to, as_from, &as_common))
6258 if (ADDR_SPACE_GENERIC_P (as_from))
6259 warning_at (loc, 0, "cast to %qs address space pointer "
6260 "from disjoint generic address space pointer",
6261 c_addr_space_name (as_to));
6263 else if (ADDR_SPACE_GENERIC_P (as_to))
6264 warning_at (loc, 0, "cast to generic address space pointer "
6265 "from disjoint %qs address space pointer",
6266 c_addr_space_name (as_from));
6268 else
6269 warning_at (loc, 0, "cast to %qs address space pointer "
6270 "from disjoint %qs address space pointer",
6271 c_addr_space_name (as_to),
6272 c_addr_space_name (as_from));
6275 /* Warn of new allocations that are not big enough for the target
6276 type. */
6277 if (warn_alloc_size && TREE_CODE (value) == CALL_EXPR)
6278 if (tree fndecl = get_callee_fndecl (value))
6279 if (DECL_IS_MALLOC (fndecl))
6281 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
6282 tree alloc_size = lookup_attribute ("alloc_size", attrs);
6283 if (alloc_size)
6284 warn_for_alloc_size (loc, TREE_TYPE (type), value,
6285 alloc_size);
6289 /* Warn about possible alignment problems. */
6290 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
6291 && TREE_CODE (type) == POINTER_TYPE
6292 && TREE_CODE (otype) == POINTER_TYPE
6293 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
6294 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6295 /* Don't warn about opaque types, where the actual alignment
6296 restriction is unknown. */
6297 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
6298 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
6299 && min_align_of_type (TREE_TYPE (type))
6300 > min_align_of_type (TREE_TYPE (otype)))
6301 warning_at (loc, OPT_Wcast_align,
6302 "cast increases required alignment of target type");
6304 if ((TREE_CODE (type) == INTEGER_TYPE
6305 || TREE_CODE (type) == BITINT_TYPE)
6306 && TREE_CODE (otype) == POINTER_TYPE
6307 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
6308 /* Unlike conversion of integers to pointers, where the
6309 warning is disabled for converting constants because
6310 of cases such as SIG_*, warn about converting constant
6311 pointers to integers. In some cases it may cause unwanted
6312 sign extension, and a warning is appropriate. */
6313 warning_at (loc, OPT_Wpointer_to_int_cast,
6314 "cast from pointer to integer of different size");
6316 if (TREE_CODE (value) == CALL_EXPR
6317 && TREE_CODE (type) != TREE_CODE (otype))
6318 warning_at (loc, OPT_Wbad_function_cast,
6319 "cast from function call of type %qT "
6320 "to non-matching type %qT", otype, type);
6322 if (TREE_CODE (type) == POINTER_TYPE
6323 && (TREE_CODE (otype) == INTEGER_TYPE
6324 || TREE_CODE (otype) == BITINT_TYPE)
6325 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
6326 /* Don't warn about converting any constant. */
6327 && !TREE_CONSTANT (value))
6328 warning_at (loc,
6329 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
6330 "of different size");
6332 if (warn_strict_aliasing <= 2)
6333 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
6335 /* If pedantic, warn for conversions between function and object
6336 pointer types, except for converting a null pointer constant
6337 to function pointer type. */
6338 if (pedantic
6339 && TREE_CODE (type) == POINTER_TYPE
6340 && TREE_CODE (otype) == POINTER_TYPE
6341 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6342 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
6343 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6344 "conversion of function pointer to object pointer type");
6346 if (pedantic
6347 && TREE_CODE (type) == POINTER_TYPE
6348 && TREE_CODE (otype) == POINTER_TYPE
6349 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6350 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6351 && !null_pointer_constant_p (value))
6352 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6353 "conversion of object pointer to function pointer type");
6355 if (TREE_CODE (type) == POINTER_TYPE
6356 && TREE_CODE (otype) == POINTER_TYPE
6357 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6358 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6359 && !c_safe_function_type_cast_p (TREE_TYPE (type),
6360 TREE_TYPE (otype)))
6361 warning_at (loc, OPT_Wcast_function_type,
6362 "cast between incompatible function types"
6363 " from %qT to %qT", otype, type);
6365 ovalue = value;
6366 value = convert (type, value);
6368 /* Ignore any integer overflow caused by the cast. */
6369 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
6371 if (TREE_OVERFLOW_P (ovalue))
6373 if (!TREE_OVERFLOW (value))
6375 /* Avoid clobbering a shared constant. */
6376 value = copy_node (value);
6377 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
6380 else if (TREE_OVERFLOW (value))
6381 /* Reset VALUE's overflow flags, ensuring constant sharing. */
6382 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6386 /* Don't let a cast be an lvalue. */
6387 if (lvalue_p (value))
6388 value = non_lvalue_loc (loc, value);
6390 /* Don't allow the results of casting to floating-point or complex
6391 types be confused with actual constants, or casts involving
6392 integer and pointer types other than direct integer-to-integer
6393 and integer-to-pointer be confused with integer constant
6394 expressions and null pointer constants. */
6395 if (TREE_CODE (value) == REAL_CST
6396 || TREE_CODE (value) == COMPLEX_CST
6397 || (TREE_CODE (value) == INTEGER_CST
6398 && !((TREE_CODE (expr) == INTEGER_CST
6399 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6400 || TREE_CODE (expr) == REAL_CST
6401 || TREE_CODE (expr) == COMPLEX_CST)))
6402 value = build1 (NOP_EXPR, type, value);
6404 /* If the expression has integer operands and so can occur in an
6405 unevaluated part of an integer constant expression, ensure the
6406 return value reflects this. */
6407 if (int_operands
6408 && INTEGRAL_TYPE_P (type)
6409 && value != error_mark_node
6410 && !EXPR_INT_CONST_OPERANDS (value))
6411 value = note_integer_operands (value);
6413 protected_set_expr_location (value, loc);
6414 return value;
6417 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
6418 location of the open paren of the cast, or the position of the cast
6419 expr. */
6420 tree
6421 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6423 tree type;
6424 tree type_expr = NULL_TREE;
6425 bool type_expr_const = true;
6426 tree ret;
6427 int saved_wsp = warn_strict_prototypes;
6429 /* This avoids warnings about unprototyped casts on
6430 integers. E.g. "#define SIG_DFL (void(*)())0". */
6431 if (TREE_CODE (expr) == INTEGER_CST)
6432 warn_strict_prototypes = 0;
6433 type = groktypename (type_name, &type_expr, &type_expr_const);
6434 warn_strict_prototypes = saved_wsp;
6436 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6437 && reject_gcc_builtin (expr))
6438 return error_mark_node;
6440 ret = build_c_cast (loc, type, expr);
6441 if (type_expr)
6443 bool inner_expr_const = true;
6444 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6445 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6446 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6447 && inner_expr_const);
6448 SET_EXPR_LOCATION (ret, loc);
6451 if (!EXPR_HAS_LOCATION (ret))
6452 protected_set_expr_location (ret, loc);
6454 /* C++ does not permits types to be defined in a cast, but it
6455 allows references to incomplete types. */
6456 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6457 warning_at (loc, OPT_Wc___compat,
6458 "defining a type in a cast is invalid in C++");
6460 return ret;
6463 /* Build an assignment expression of lvalue LHS from value RHS.
6464 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6465 may differ from TREE_TYPE (LHS) for an enum bitfield.
6466 MODIFYCODE is the code for a binary operator that we use
6467 to combine the old value of LHS with RHS to get the new value.
6468 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6469 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6470 which may differ from TREE_TYPE (RHS) for an enum value.
6472 LOCATION is the location of the MODIFYCODE operator.
6473 RHS_LOC is the location of the RHS. */
6475 tree
6476 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6477 enum tree_code modifycode,
6478 location_t rhs_loc, tree rhs, tree rhs_origtype)
6480 tree result;
6481 tree newrhs;
6482 tree rhseval = NULL_TREE;
6483 tree lhstype = TREE_TYPE (lhs);
6484 tree olhstype = lhstype;
6485 bool npc;
6486 bool is_atomic_op;
6488 /* Types that aren't fully specified cannot be used in assignments. */
6489 lhs = require_complete_type (location, lhs);
6491 /* Avoid duplicate error messages from operands that had errors. */
6492 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6493 return error_mark_node;
6495 /* Ensure an error for assigning a non-lvalue array to an array in
6496 C90. */
6497 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6499 error_at (location, "assignment to expression with array type");
6500 return error_mark_node;
6503 /* For ObjC properties, defer this check. */
6504 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6505 return error_mark_node;
6507 is_atomic_op = really_atomic_lvalue (lhs);
6509 newrhs = rhs;
6511 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6513 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6514 lhs_origtype, modifycode, rhs_loc, rhs,
6515 rhs_origtype);
6516 if (inner == error_mark_node)
6517 return error_mark_node;
6518 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6519 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6520 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6521 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6522 protected_set_expr_location (result, location);
6523 return result;
6526 /* If a binary op has been requested, combine the old LHS value with the RHS
6527 producing the value we should actually store into the LHS. */
6529 if (modifycode != NOP_EXPR)
6531 lhs = c_fully_fold (lhs, false, NULL, true);
6532 lhs = stabilize_reference (lhs);
6534 /* Construct the RHS for any non-atomic compound assignemnt. */
6535 if (!is_atomic_op)
6537 /* If in LHS op= RHS the RHS has side-effects, ensure they
6538 are preevaluated before the rest of the assignment expression's
6539 side-effects, because RHS could contain e.g. function calls
6540 that modify LHS. */
6541 if (TREE_SIDE_EFFECTS (rhs))
6543 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6544 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6545 else
6546 newrhs = save_expr (rhs);
6547 rhseval = newrhs;
6548 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6549 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6550 newrhs);
6552 newrhs = build_binary_op (location,
6553 modifycode, lhs, newrhs, true);
6555 /* The original type of the right hand side is no longer
6556 meaningful. */
6557 rhs_origtype = NULL_TREE;
6561 if (c_dialect_objc ())
6563 /* Check if we are modifying an Objective-C property reference;
6564 if so, we need to generate setter calls. */
6565 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6566 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6567 else
6568 result = objc_maybe_build_modify_expr (lhs, newrhs);
6569 if (result)
6570 goto return_result;
6572 /* Else, do the check that we postponed for Objective-C. */
6573 if (!lvalue_or_else (location, lhs, lv_assign))
6574 return error_mark_node;
6577 /* Give an error for storing in something that is 'const'. */
6579 if (TYPE_READONLY (lhstype)
6580 || (RECORD_OR_UNION_TYPE_P (lhstype)
6581 && C_TYPE_FIELDS_READONLY (lhstype)))
6583 readonly_error (location, lhs, lv_assign);
6584 return error_mark_node;
6586 else if (TREE_READONLY (lhs))
6587 readonly_warning (lhs, lv_assign);
6589 /* If storing into a structure or union member,
6590 it has probably been given type `int'.
6591 Compute the type that would go with
6592 the actual amount of storage the member occupies. */
6594 if (TREE_CODE (lhs) == COMPONENT_REF
6595 && (TREE_CODE (lhstype) == INTEGER_TYPE
6596 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6597 || SCALAR_FLOAT_TYPE_P (lhstype)
6598 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6599 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6601 /* If storing in a field that is in actuality a short or narrower than one,
6602 we must store in the field in its actual type. */
6604 if (lhstype != TREE_TYPE (lhs))
6606 lhs = copy_node (lhs);
6607 TREE_TYPE (lhs) = lhstype;
6610 /* Issue -Wc++-compat warnings about an assignment to an enum type
6611 when LHS does not have its original type. This happens for,
6612 e.g., an enum bitfield in a struct. */
6613 if (warn_cxx_compat
6614 && lhs_origtype != NULL_TREE
6615 && lhs_origtype != lhstype
6616 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6618 tree checktype = (rhs_origtype != NULL_TREE
6619 ? rhs_origtype
6620 : TREE_TYPE (rhs));
6621 if (checktype != error_mark_node
6622 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6623 || (is_atomic_op && modifycode != NOP_EXPR)))
6624 warning_at (location, OPT_Wc___compat,
6625 "enum conversion in assignment is invalid in C++");
6628 /* Remove qualifiers. */
6629 lhstype = build_qualified_type (lhstype, TYPE_UNQUALIFIED);
6630 olhstype = build_qualified_type (olhstype, TYPE_UNQUALIFIED);
6632 /* Convert new value to destination type. Fold it first, then
6633 restore any excess precision information, for the sake of
6634 conversion warnings. */
6636 if (!(is_atomic_op && modifycode != NOP_EXPR))
6638 tree rhs_semantic_type = NULL_TREE;
6639 if (!c_in_omp_for)
6641 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6643 rhs_semantic_type = TREE_TYPE (newrhs);
6644 newrhs = TREE_OPERAND (newrhs, 0);
6646 npc = null_pointer_constant_p (newrhs);
6647 newrhs = c_fully_fold (newrhs, false, NULL);
6648 if (rhs_semantic_type)
6649 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6651 else
6652 npc = null_pointer_constant_p (newrhs);
6653 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6654 rhs_origtype, ic_assign, npc,
6655 NULL_TREE, NULL_TREE, 0);
6656 if (TREE_CODE (newrhs) == ERROR_MARK)
6657 return error_mark_node;
6660 /* Emit ObjC write barrier, if necessary. */
6661 if (c_dialect_objc () && flag_objc_gc)
6663 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6664 if (result)
6666 protected_set_expr_location (result, location);
6667 goto return_result;
6671 /* Scan operands. */
6673 if (is_atomic_op)
6674 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6675 else
6677 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6678 TREE_SIDE_EFFECTS (result) = 1;
6679 protected_set_expr_location (result, location);
6682 /* If we got the LHS in a different type for storing in,
6683 convert the result back to the nominal type of LHS
6684 so that the value we return always has the same type
6685 as the LHS argument. */
6687 if (olhstype == TREE_TYPE (result))
6688 goto return_result;
6690 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6691 rhs_origtype, ic_assign, false, NULL_TREE,
6692 NULL_TREE, 0);
6693 protected_set_expr_location (result, location);
6695 return_result:
6696 if (rhseval)
6697 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6698 return result;
6701 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6702 This is used to implement -fplan9-extensions. */
6704 static bool
6705 find_anonymous_field_with_type (tree struct_type, tree type)
6707 tree field;
6708 bool found;
6710 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6711 found = false;
6712 for (field = TYPE_FIELDS (struct_type);
6713 field != NULL_TREE;
6714 field = TREE_CHAIN (field))
6716 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6717 ? c_build_qualified_type (TREE_TYPE (field),
6718 TYPE_QUAL_ATOMIC)
6719 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6720 if (DECL_NAME (field) == NULL
6721 && comptypes (type, fieldtype))
6723 if (found)
6724 return false;
6725 found = true;
6727 else if (DECL_NAME (field) == NULL
6728 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6729 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6731 if (found)
6732 return false;
6733 found = true;
6736 return found;
6739 /* RHS is an expression whose type is pointer to struct. If there is
6740 an anonymous field in RHS with type TYPE, then return a pointer to
6741 that field in RHS. This is used with -fplan9-extensions. This
6742 returns NULL if no conversion could be found. */
6744 static tree
6745 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6747 tree rhs_struct_type, lhs_main_type;
6748 tree field, found_field;
6749 bool found_sub_field;
6750 tree ret;
6752 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6753 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6754 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6756 gcc_assert (POINTER_TYPE_P (type));
6757 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6758 ? c_build_qualified_type (TREE_TYPE (type),
6759 TYPE_QUAL_ATOMIC)
6760 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6762 found_field = NULL_TREE;
6763 found_sub_field = false;
6764 for (field = TYPE_FIELDS (rhs_struct_type);
6765 field != NULL_TREE;
6766 field = TREE_CHAIN (field))
6768 if (DECL_NAME (field) != NULL_TREE
6769 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6770 continue;
6771 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6772 ? c_build_qualified_type (TREE_TYPE (field),
6773 TYPE_QUAL_ATOMIC)
6774 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6775 if (comptypes (lhs_main_type, fieldtype))
6777 if (found_field != NULL_TREE)
6778 return NULL_TREE;
6779 found_field = field;
6781 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6782 lhs_main_type))
6784 if (found_field != NULL_TREE)
6785 return NULL_TREE;
6786 found_field = field;
6787 found_sub_field = true;
6791 if (found_field == NULL_TREE)
6792 return NULL_TREE;
6794 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6795 build_fold_indirect_ref (rhs), found_field,
6796 NULL_TREE);
6797 ret = build_fold_addr_expr_loc (location, ret);
6799 if (found_sub_field)
6801 ret = convert_to_anonymous_field (location, type, ret);
6802 gcc_assert (ret != NULL_TREE);
6805 return ret;
6808 /* Issue an error message for a bad initializer component.
6809 GMSGID identifies the message.
6810 The component name is taken from the spelling stack. */
6812 static void ATTRIBUTE_GCC_DIAG (2,0)
6813 error_init (location_t loc, const char *gmsgid, ...)
6815 char *ofwhat;
6817 auto_diagnostic_group d;
6819 /* The gmsgid may be a format string with %< and %>. */
6820 va_list ap;
6821 va_start (ap, gmsgid);
6822 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6823 va_end (ap);
6825 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6826 if (*ofwhat && warned)
6827 inform (loc, "(near initialization for %qs)", ofwhat);
6830 /* Used to implement pedwarn_init and permerror_init. */
6832 static void ATTRIBUTE_GCC_DIAG (3,0)
6833 pedwarn_permerror_init (location_t loc, int opt, const char *gmsgid,
6834 va_list *ap, diagnostic_t kind)
6836 /* Use the location where a macro was expanded rather than where
6837 it was defined to make sure macros defined in system headers
6838 but used incorrectly elsewhere are diagnosed. */
6839 location_t exploc = expansion_point_location_if_in_system_header (loc);
6840 auto_diagnostic_group d;
6841 bool warned = emit_diagnostic_valist (kind, exploc, opt, gmsgid, ap);
6842 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6843 if (*ofwhat && warned)
6844 inform (exploc, "(near initialization for %qs)", ofwhat);
6847 /* Issue a pedantic warning for a bad initializer component. OPT is
6848 the option OPT_* (from options.h) controlling this warning or 0 if
6849 it is unconditionally given. GMSGID identifies the message. The
6850 component name is taken from the spelling stack. */
6852 static void ATTRIBUTE_GCC_DIAG (3,0)
6853 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6855 va_list ap;
6856 va_start (ap, gmsgid);
6857 pedwarn_permerror_init (loc, opt, gmsgid, &ap, DK_PEDWARN);
6858 va_end (ap);
6861 /* Like pedwarn_init, but issue a permerror. */
6863 static void ATTRIBUTE_GCC_DIAG (3,0)
6864 permerror_init (location_t loc, int opt, const char *gmsgid, ...)
6866 va_list ap;
6867 va_start (ap, gmsgid);
6868 pedwarn_permerror_init (loc, opt, gmsgid, &ap, DK_PERMERROR);
6869 va_end (ap);
6872 /* Issue a warning for a bad initializer component.
6874 OPT is the OPT_W* value corresponding to the warning option that
6875 controls this warning. GMSGID identifies the message. The
6876 component name is taken from the spelling stack. */
6878 static void
6879 warning_init (location_t loc, int opt, const char *gmsgid)
6881 char *ofwhat;
6882 bool warned;
6884 auto_diagnostic_group d;
6886 /* Use the location where a macro was expanded rather than where
6887 it was defined to make sure macros defined in system headers
6888 but used incorrectly elsewhere are diagnosed. */
6889 location_t exploc = expansion_point_location_if_in_system_header (loc);
6891 /* The gmsgid may be a format string with %< and %>. */
6892 warned = warning_at (exploc, opt, gmsgid);
6893 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6894 if (*ofwhat && warned)
6895 inform (exploc, "(near initialization for %qs)", ofwhat);
6898 /* If TYPE is an array type and EXPR is a parenthesized string
6899 constant, warn if pedantic that EXPR is being used to initialize an
6900 object of type TYPE. */
6902 void
6903 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6905 if (pedantic
6906 && TREE_CODE (type) == ARRAY_TYPE
6907 && TREE_CODE (expr.value) == STRING_CST
6908 && expr.original_code != STRING_CST)
6909 pedwarn_init (loc, OPT_Wpedantic,
6910 "array initialized from parenthesized string constant");
6913 /* Attempt to locate the parameter with the given index within FNDECL,
6914 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6916 static location_t
6917 get_fndecl_argument_location (tree fndecl, int argnum)
6919 int i;
6920 tree param;
6922 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6923 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6924 i < argnum && param;
6925 i++, param = TREE_CHAIN (param))
6928 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6929 return DECL_SOURCE_LOCATION (FNDECL). */
6930 if (param == NULL)
6931 return DECL_SOURCE_LOCATION (fndecl);
6933 return DECL_SOURCE_LOCATION (param);
6936 /* Issue a note about a mismatching argument for parameter PARMNUM
6937 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6938 Attempt to issue the note at the pertinent parameter of the decl;
6939 failing that issue it at the location of FUNDECL; failing that
6940 issue it at PLOC. */
6942 static void
6943 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6944 tree expected_type, tree actual_type)
6946 location_t loc;
6947 if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6948 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6949 else
6950 loc = ploc;
6952 inform (loc,
6953 "expected %qT but argument is of type %qT",
6954 expected_type, actual_type);
6957 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6958 function FUNDECL declared without prototype to parameter PARMNUM of
6959 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6961 static void
6962 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6963 tree parmtype, tree argtype)
6965 tree_code parmcode = TREE_CODE (parmtype);
6966 tree_code argcode = TREE_CODE (argtype);
6967 tree promoted = c_type_promotes_to (argtype);
6969 /* Avoid warning for enum arguments that promote to an integer type
6970 of the same size/mode. */
6971 if (parmcode == INTEGER_TYPE
6972 && argcode == ENUMERAL_TYPE
6973 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6974 return;
6976 if ((parmcode == argcode
6977 || (parmcode == INTEGER_TYPE
6978 && argcode == ENUMERAL_TYPE))
6979 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6980 return;
6982 /* This diagnoses even signed/unsigned mismatches. Those might be
6983 safe in many cases but GCC may emit suboptimal code for them so
6984 warning on those cases drives efficiency improvements. */
6985 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6986 TYPE_MAIN_VARIANT (promoted) == argtype
6987 ? G_("%qD argument %d type is %qT where %qT is expected "
6988 "in a call to built-in function declared without "
6989 "prototype")
6990 : G_("%qD argument %d promotes to %qT where %qT is expected "
6991 "in a call to built-in function declared without "
6992 "prototype"),
6993 fundecl, parmnum, promoted, parmtype))
6994 inform (DECL_SOURCE_LOCATION (fundecl),
6995 "built-in %qD declared here",
6996 fundecl);
6999 /* Convert value RHS to type TYPE as preparation for an assignment to
7000 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
7001 original type of RHS; this differs from TREE_TYPE (RHS) for enum
7002 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
7003 constant before any folding.
7004 The real work of conversion is done by `convert'.
7005 The purpose of this function is to generate error messages
7006 for assignments that are not allowed in C.
7007 ERRTYPE says whether it is argument passing, assignment,
7008 initialization or return.
7010 In the following example, '~' denotes where EXPR_LOC and '^' where
7011 LOCATION point to:
7013 f (var); [ic_argpass]
7014 ^ ~~~
7015 x = var; [ic_assign]
7016 ^ ~~~;
7017 int x = var; [ic_init]
7019 return x; [ic_return]
7022 FUNCTION is a tree for the function being called.
7023 PARMNUM is the number of the argument, for printing in error messages.
7024 WARNOPT may be set to a warning option to issue the corresponding warning
7025 rather than an error for invalid conversions. Used for calls to built-in
7026 functions declared without a prototype. */
7028 static tree
7029 convert_for_assignment (location_t location, location_t expr_loc, tree type,
7030 tree rhs, tree origtype, enum impl_conv errtype,
7031 bool null_pointer_constant, tree fundecl,
7032 tree function, int parmnum, int warnopt /* = 0 */)
7034 enum tree_code codel = TREE_CODE (type);
7035 tree orig_rhs = rhs;
7036 tree rhstype;
7037 enum tree_code coder;
7038 tree rname = NULL_TREE;
7039 bool objc_ok = false;
7041 /* Use the expansion point location to handle cases such as user's
7042 function returning a wrong-type macro defined in a system header. */
7043 location = expansion_point_location_if_in_system_header (location);
7045 if (errtype == ic_argpass)
7047 tree selector;
7048 /* Change pointer to function to the function itself for
7049 diagnostics. */
7050 if (TREE_CODE (function) == ADDR_EXPR
7051 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
7052 function = TREE_OPERAND (function, 0);
7054 /* Handle an ObjC selector specially for diagnostics. */
7055 selector = objc_message_selector ();
7056 rname = function;
7057 if (selector && parmnum > 2)
7059 rname = selector;
7060 parmnum -= 2;
7064 /* This macro is used to emit diagnostics to ensure that all format
7065 strings are complete sentences, visible to gettext and checked at
7066 compile time. */
7067 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
7068 do { \
7069 switch (errtype) \
7071 case ic_argpass: \
7073 auto_diagnostic_group d; \
7074 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
7075 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
7077 break; \
7078 case ic_assign: \
7079 pedwarn (LOCATION, OPT, AS); \
7080 break; \
7081 case ic_init: \
7082 case ic_init_const: \
7083 pedwarn_init (LOCATION, OPT, IN); \
7084 break; \
7085 case ic_return: \
7086 pedwarn (LOCATION, OPT, RE); \
7087 break; \
7088 default: \
7089 gcc_unreachable (); \
7091 } while (0)
7093 /* This macro is used to emit diagnostics to ensure that all format
7094 strings are complete sentences, visible to gettext and checked at
7095 compile time. It can be called with 'pedwarn' or 'warning_at'. */
7096 #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
7097 do { \
7098 switch (errtype) \
7100 case ic_argpass: \
7102 auto_diagnostic_group d; \
7103 if (PEDWARN) { \
7104 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
7105 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
7106 } else { \
7107 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
7108 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
7111 break; \
7112 case ic_assign: \
7113 if (PEDWARN) \
7114 pedwarn (LOCATION, OPT, AS, QUALS); \
7115 else \
7116 warning_at (LOCATION, OPT, AS, QUALS); \
7117 break; \
7118 case ic_init: \
7119 case ic_init_const: \
7120 if (PEDWARN) \
7121 pedwarn (LOCATION, OPT, IN, QUALS); \
7122 else \
7123 warning_at (LOCATION, OPT, IN, QUALS); \
7124 break; \
7125 case ic_return: \
7126 if (PEDWARN) \
7127 pedwarn (LOCATION, OPT, RE, QUALS); \
7128 else \
7129 warning_at (LOCATION, OPT, RE, QUALS); \
7130 break; \
7131 default: \
7132 gcc_unreachable (); \
7134 } while (0)
7136 /* This macro is used to emit diagnostics to ensure that all format
7137 strings are complete sentences, visible to gettext and checked at
7138 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
7139 extra parameter to enumerate qualifiers. */
7140 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
7141 WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
7144 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7145 rhs = TREE_OPERAND (rhs, 0);
7147 rhstype = TREE_TYPE (rhs);
7148 coder = TREE_CODE (rhstype);
7150 if (coder == ERROR_MARK)
7151 return error_mark_node;
7153 if (c_dialect_objc ())
7155 int parmno;
7157 switch (errtype)
7159 case ic_return:
7160 parmno = 0;
7161 break;
7163 case ic_assign:
7164 parmno = -1;
7165 break;
7167 case ic_init:
7168 case ic_init_const:
7169 parmno = -2;
7170 break;
7172 default:
7173 parmno = parmnum;
7174 break;
7177 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
7180 if (warn_cxx_compat)
7182 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
7183 if (checktype != error_mark_node
7184 && TREE_CODE (type) == ENUMERAL_TYPE
7185 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
7186 switch (errtype)
7188 case ic_argpass:
7189 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
7190 "passing argument %d of %qE is invalid in C++",
7191 parmnum, rname))
7192 inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
7193 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
7194 "expected %qT but argument is of type %qT",
7195 type, rhstype);
7196 break;
7197 case ic_assign:
7198 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
7199 "%qT in assignment is invalid in C++", rhstype, type);
7200 break;
7201 case ic_init:
7202 case ic_init_const:
7203 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
7204 "%qT to %qT in initialization is invalid in C++",
7205 rhstype, type);
7206 break;
7207 case ic_return:
7208 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
7209 "%qT in return is invalid in C++", rhstype, type);
7210 break;
7211 default:
7212 gcc_unreachable ();
7216 if (warn_enum_conversion)
7218 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
7219 if (checktype != error_mark_node
7220 && TREE_CODE (checktype) == ENUMERAL_TYPE
7221 && TREE_CODE (type) == ENUMERAL_TYPE
7222 && !comptypes (TYPE_MAIN_VARIANT (checktype), TYPE_MAIN_VARIANT (type)))
7224 gcc_rich_location loc (location);
7225 warning_at (&loc, OPT_Wenum_conversion,
7226 "implicit conversion from %qT to %qT",
7227 checktype, type);
7231 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
7233 warn_for_address_of_packed_member (type, orig_rhs);
7234 return rhs;
7237 if (coder == VOID_TYPE)
7239 /* Except for passing an argument to an unprototyped function,
7240 this is a constraint violation. When passing an argument to
7241 an unprototyped function, it is compile-time undefined;
7242 making it a constraint in that case was rejected in
7243 DR#252. */
7244 const char msg[] = "void value not ignored as it ought to be";
7245 if (warnopt)
7246 warning_at (location, warnopt, msg);
7247 else
7248 error_at (location, msg);
7249 return error_mark_node;
7251 rhs = require_complete_type (location, rhs);
7252 if (rhs == error_mark_node)
7253 return error_mark_node;
7255 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
7256 return error_mark_node;
7258 /* A non-reference type can convert to a reference. This handles
7259 va_start, va_copy and possibly port built-ins. */
7260 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
7262 if (!lvalue_p (rhs))
7264 const char msg[] = "cannot pass rvalue to reference parameter";
7265 if (warnopt)
7266 warning_at (location, warnopt, msg);
7267 else
7268 error_at (location, msg);
7269 return error_mark_node;
7271 if (!c_mark_addressable (rhs))
7272 return error_mark_node;
7273 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
7274 SET_EXPR_LOCATION (rhs, location);
7276 rhs = convert_for_assignment (location, expr_loc,
7277 build_pointer_type (TREE_TYPE (type)),
7278 rhs, origtype, errtype,
7279 null_pointer_constant, fundecl, function,
7280 parmnum, warnopt);
7281 if (rhs == error_mark_node)
7282 return error_mark_node;
7284 rhs = build1 (NOP_EXPR, type, rhs);
7285 SET_EXPR_LOCATION (rhs, location);
7286 return rhs;
7288 /* Some types can interconvert without explicit casts. */
7289 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
7290 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
7291 return convert (type, rhs);
7292 /* Arithmetic types all interconvert, and enum is treated like int. */
7293 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
7294 || codel == FIXED_POINT_TYPE
7295 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
7296 || codel == BOOLEAN_TYPE || codel == BITINT_TYPE)
7297 && (coder == INTEGER_TYPE || coder == REAL_TYPE
7298 || coder == FIXED_POINT_TYPE
7299 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
7300 || coder == BOOLEAN_TYPE || coder == BITINT_TYPE))
7302 if (warnopt && errtype == ic_argpass)
7303 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
7304 rhstype);
7306 bool save = in_late_binary_op;
7307 if (C_BOOLEAN_TYPE_P (type) || codel == COMPLEX_TYPE
7308 || (coder == REAL_TYPE
7309 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
7310 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
7311 in_late_binary_op = true;
7312 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
7313 ? expr_loc : location, type, orig_rhs,
7314 errtype == ic_init_const);
7315 in_late_binary_op = save;
7316 return ret;
7319 /* Aggregates in different TUs might need conversion. */
7320 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
7321 && codel == coder
7322 && comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (rhstype)))
7323 return convert_and_check (expr_loc != UNKNOWN_LOCATION
7324 ? expr_loc : location, type, rhs);
7326 /* Conversion to a transparent union or record from its member types.
7327 This applies only to function arguments. */
7328 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
7329 && TYPE_TRANSPARENT_AGGR (type))
7330 && errtype == ic_argpass)
7332 tree memb, marginal_memb = NULL_TREE;
7334 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
7336 tree memb_type = TREE_TYPE (memb);
7338 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
7339 TYPE_MAIN_VARIANT (rhstype)))
7340 break;
7342 if (TREE_CODE (memb_type) != POINTER_TYPE)
7343 continue;
7345 if (coder == POINTER_TYPE)
7347 tree ttl = TREE_TYPE (memb_type);
7348 tree ttr = TREE_TYPE (rhstype);
7350 /* Any non-function converts to a [const][volatile] void *
7351 and vice versa; otherwise, targets must be the same.
7352 Meanwhile, the lhs target must have all the qualifiers of
7353 the rhs. */
7354 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7355 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7356 || comp_target_types (location, memb_type, rhstype))
7358 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
7359 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
7360 /* If this type won't generate any warnings, use it. */
7361 if (lquals == rquals
7362 || ((TREE_CODE (ttr) == FUNCTION_TYPE
7363 && TREE_CODE (ttl) == FUNCTION_TYPE)
7364 ? ((lquals | rquals) == rquals)
7365 : ((lquals | rquals) == lquals)))
7366 break;
7368 /* Keep looking for a better type, but remember this one. */
7369 if (!marginal_memb)
7370 marginal_memb = memb;
7374 /* Can convert integer zero to any pointer type. */
7375 if (null_pointer_constant)
7377 rhs = null_pointer_node;
7378 break;
7382 if (memb || marginal_memb)
7384 if (!memb)
7386 /* We have only a marginally acceptable member type;
7387 it needs a warning. */
7388 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
7389 tree ttr = TREE_TYPE (rhstype);
7391 /* Const and volatile mean something different for function
7392 types, so the usual warnings are not appropriate. */
7393 if (TREE_CODE (ttr) == FUNCTION_TYPE
7394 && TREE_CODE (ttl) == FUNCTION_TYPE)
7396 /* Because const and volatile on functions are
7397 restrictions that say the function will not do
7398 certain things, it is okay to use a const or volatile
7399 function where an ordinary one is wanted, but not
7400 vice-versa. */
7401 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7402 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7403 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7404 OPT_Wdiscarded_qualifiers,
7405 G_("passing argument %d of %qE "
7406 "makes %q#v qualified function "
7407 "pointer from unqualified"),
7408 G_("assignment makes %q#v qualified "
7409 "function pointer from "
7410 "unqualified"),
7411 G_("initialization makes %q#v qualified "
7412 "function pointer from "
7413 "unqualified"),
7414 G_("return makes %q#v qualified function "
7415 "pointer from unqualified"),
7416 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7418 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7419 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7420 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7421 OPT_Wdiscarded_qualifiers,
7422 G_("passing argument %d of %qE discards "
7423 "%qv qualifier from pointer target type"),
7424 G_("assignment discards %qv qualifier "
7425 "from pointer target type"),
7426 G_("initialization discards %qv qualifier "
7427 "from pointer target type"),
7428 G_("return discards %qv qualifier from "
7429 "pointer target type"),
7430 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7432 memb = marginal_memb;
7435 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7436 pedwarn (location, OPT_Wpedantic,
7437 "ISO C prohibits argument conversion to union type");
7439 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7440 return build_constructor_single (type, memb, rhs);
7444 /* Conversions among pointers */
7445 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7446 && (coder == codel))
7448 /* If RHS refers to a built-in declared without a prototype
7449 BLTIN is the declaration of the built-in with a prototype
7450 and RHSTYPE is set to the actual type of the built-in. */
7451 tree bltin;
7452 rhstype = type_or_builtin_type (rhs, &bltin);
7454 tree ttl = TREE_TYPE (type);
7455 tree ttr = TREE_TYPE (rhstype);
7456 tree mvl = ttl;
7457 tree mvr = ttr;
7458 bool is_opaque_pointer;
7459 bool target_cmp = false; /* Cache comp_target_types () result. */
7460 addr_space_t asl;
7461 addr_space_t asr;
7463 if (TREE_CODE (mvl) != ARRAY_TYPE)
7464 mvl = (TYPE_ATOMIC (mvl)
7465 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7466 TYPE_QUAL_ATOMIC)
7467 : TYPE_MAIN_VARIANT (mvl));
7468 if (TREE_CODE (mvr) != ARRAY_TYPE)
7469 mvr = (TYPE_ATOMIC (mvr)
7470 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7471 TYPE_QUAL_ATOMIC)
7472 : TYPE_MAIN_VARIANT (mvr));
7473 /* Opaque pointers are treated like void pointers. */
7474 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7476 /* The Plan 9 compiler permits a pointer to a struct to be
7477 automatically converted into a pointer to an anonymous field
7478 within the struct. */
7479 if (flag_plan9_extensions
7480 && RECORD_OR_UNION_TYPE_P (mvl)
7481 && RECORD_OR_UNION_TYPE_P (mvr)
7482 && mvl != mvr)
7484 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7485 if (new_rhs != NULL_TREE)
7487 rhs = new_rhs;
7488 rhstype = TREE_TYPE (rhs);
7489 coder = TREE_CODE (rhstype);
7490 ttr = TREE_TYPE (rhstype);
7491 mvr = TYPE_MAIN_VARIANT (ttr);
7495 /* C++ does not allow the implicit conversion void* -> T*. However,
7496 for the purpose of reducing the number of false positives, we
7497 tolerate the special case of
7499 int *p = NULL;
7501 where NULL is typically defined in C to be '(void *) 0'. */
7502 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7503 warning_at (errtype == ic_argpass ? expr_loc : location,
7504 OPT_Wc___compat,
7505 "request for implicit conversion "
7506 "from %qT to %qT not permitted in C++", rhstype, type);
7508 /* Warn of new allocations that are not big enough for the target
7509 type. */
7510 if (warn_alloc_size && TREE_CODE (rhs) == CALL_EXPR)
7511 if (tree fndecl = get_callee_fndecl (rhs))
7512 if (DECL_IS_MALLOC (fndecl))
7514 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
7515 tree alloc_size = lookup_attribute ("alloc_size", attrs);
7516 if (alloc_size)
7517 warn_for_alloc_size (location, ttl, rhs, alloc_size);
7520 /* See if the pointers point to incompatible address spaces. */
7521 asl = TYPE_ADDR_SPACE (ttl);
7522 asr = TYPE_ADDR_SPACE (ttr);
7523 if (!null_pointer_constant_p (rhs)
7524 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7526 auto_diagnostic_group d;
7527 bool diagnosed = true;
7528 switch (errtype)
7530 case ic_argpass:
7532 const char msg[] = G_("passing argument %d of %qE from "
7533 "pointer to non-enclosed address space");
7534 if (warnopt)
7535 diagnosed
7536 = warning_at (expr_loc, warnopt, msg, parmnum, rname);
7537 else
7538 error_at (expr_loc, msg, parmnum, rname);
7539 break;
7541 case ic_assign:
7543 const char msg[] = G_("assignment from pointer to "
7544 "non-enclosed address space");
7545 if (warnopt)
7546 diagnosed = warning_at (location, warnopt, msg);
7547 else
7548 error_at (location, msg);
7549 break;
7551 case ic_init:
7552 case ic_init_const:
7554 const char msg[] = G_("initialization from pointer to "
7555 "non-enclosed address space");
7556 if (warnopt)
7557 diagnosed = warning_at (location, warnopt, msg);
7558 else
7559 error_at (location, msg);
7560 break;
7562 case ic_return:
7564 const char msg[] = G_("return from pointer to "
7565 "non-enclosed address space");
7566 if (warnopt)
7567 diagnosed = warning_at (location, warnopt, msg);
7568 else
7569 error_at (location, msg);
7570 break;
7572 default:
7573 gcc_unreachable ();
7575 if (diagnosed)
7577 if (errtype == ic_argpass)
7578 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7579 else
7580 inform (location, "expected %qT but pointer is of type %qT",
7581 type, rhstype);
7583 return error_mark_node;
7586 /* Check if the right-hand side has a format attribute but the
7587 left-hand side doesn't. */
7588 if (warn_suggest_attribute_format
7589 && check_missing_format_attribute (type, rhstype))
7591 switch (errtype)
7593 case ic_argpass:
7594 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7595 "argument %d of %qE might be "
7596 "a candidate for a format attribute",
7597 parmnum, rname);
7598 break;
7599 case ic_assign:
7600 warning_at (location, OPT_Wsuggest_attribute_format,
7601 "assignment left-hand side might be "
7602 "a candidate for a format attribute");
7603 break;
7604 case ic_init:
7605 case ic_init_const:
7606 warning_at (location, OPT_Wsuggest_attribute_format,
7607 "initialization left-hand side might be "
7608 "a candidate for a format attribute");
7609 break;
7610 case ic_return:
7611 warning_at (location, OPT_Wsuggest_attribute_format,
7612 "return type might be "
7613 "a candidate for a format attribute");
7614 break;
7615 default:
7616 gcc_unreachable ();
7620 /* See if the pointers point to incompatible scalar storage orders. */
7621 if (warn_scalar_storage_order
7622 && !null_pointer_constant_p (rhs)
7623 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7624 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7626 tree t;
7628 switch (errtype)
7630 case ic_argpass:
7631 /* Do not warn for built-in functions, for example memcpy, since we
7632 control how they behave and they can be useful in this area. */
7633 if (TREE_CODE (rname) != FUNCTION_DECL
7634 || !fndecl_built_in_p (rname))
7635 warning_at (location, OPT_Wscalar_storage_order,
7636 "passing argument %d of %qE from incompatible "
7637 "scalar storage order", parmnum, rname);
7638 break;
7639 case ic_assign:
7640 /* Do not warn if the RHS is a call to a function that returns a
7641 pointer that is not an alias. */
7642 if (TREE_CODE (rhs) != CALL_EXPR
7643 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7644 || !DECL_IS_MALLOC (t))
7645 warning_at (location, OPT_Wscalar_storage_order,
7646 "assignment to %qT from pointer type %qT with "
7647 "incompatible scalar storage order", type, rhstype);
7648 break;
7649 case ic_init:
7650 case ic_init_const:
7651 /* Likewise. */
7652 if (TREE_CODE (rhs) != CALL_EXPR
7653 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7654 || !DECL_IS_MALLOC (t))
7655 warning_at (location, OPT_Wscalar_storage_order,
7656 "initialization of %qT from pointer type %qT with "
7657 "incompatible scalar storage order", type, rhstype);
7658 break;
7659 case ic_return:
7660 warning_at (location, OPT_Wscalar_storage_order,
7661 "returning %qT from pointer type with incompatible "
7662 "scalar storage order %qT", rhstype, type);
7663 break;
7664 default:
7665 gcc_unreachable ();
7669 /* Any non-function converts to a [const][volatile] void *
7670 and vice versa; otherwise, targets must be the same.
7671 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7672 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7673 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7674 || (target_cmp = comp_target_types (location, type, rhstype))
7675 || is_opaque_pointer
7676 || ((c_common_unsigned_type (mvl)
7677 == c_common_unsigned_type (mvr))
7678 && (c_common_signed_type (mvl)
7679 == c_common_signed_type (mvr))
7680 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7682 /* Warn about loss of qualifers from pointers to arrays with
7683 qualifiers on the element type. */
7684 if (TREE_CODE (ttr) == ARRAY_TYPE)
7686 ttr = strip_array_types (ttr);
7687 ttl = strip_array_types (ttl);
7689 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7690 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7691 WARNING_FOR_QUALIFIERS (flag_isoc23,
7692 location, expr_loc,
7693 OPT_Wdiscarded_array_qualifiers,
7694 G_("passing argument %d of %qE discards "
7695 "%qv qualifier from pointer target type"),
7696 G_("assignment discards %qv qualifier "
7697 "from pointer target type"),
7698 G_("initialization discards %qv qualifier "
7699 "from pointer target type"),
7700 G_("return discards %qv qualifier from "
7701 "pointer target type"),
7702 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7704 else if (pedantic
7705 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7707 (VOID_TYPE_P (ttr)
7708 && !null_pointer_constant
7709 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7710 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7711 G_("ISO C forbids passing argument %d of "
7712 "%qE between function pointer "
7713 "and %<void *%>"),
7714 G_("ISO C forbids assignment between "
7715 "function pointer and %<void *%>"),
7716 G_("ISO C forbids initialization between "
7717 "function pointer and %<void *%>"),
7718 G_("ISO C forbids return between function "
7719 "pointer and %<void *%>"));
7720 /* Const and volatile mean something different for function types,
7721 so the usual warnings are not appropriate. */
7722 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7723 && TREE_CODE (ttl) != FUNCTION_TYPE)
7725 /* Assignments between atomic and non-atomic objects are OK. */
7726 bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7727 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
7728 bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7729 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
7731 /* Don't warn about loss of qualifier for conversions from
7732 qualified void* to pointers to arrays with corresponding
7733 qualifier on the element type (except for pedantic before C23). */
7734 if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc23))
7735 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7736 OPT_Wdiscarded_qualifiers,
7737 G_("passing argument %d of %qE discards "
7738 "%qv qualifier from pointer target type"),
7739 G_("assignment discards %qv qualifier "
7740 "from pointer target type"),
7741 G_("initialization discards %qv qualifier "
7742 "from pointer target type"),
7743 G_("return discards %qv qualifier from "
7744 "pointer target type"),
7745 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7746 else if (warn_quals_ped)
7747 pedwarn_c11 (location, OPT_Wc11_c23_compat,
7748 "array with qualifier on the element is not qualified before C23");
7750 /* If this is not a case of ignoring a mismatch in signedness,
7751 no warning. */
7752 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7753 || target_cmp)
7755 /* If there is a mismatch, do warn. */
7756 else if (warn_pointer_sign)
7757 switch (errtype)
7759 case ic_argpass:
7761 auto_diagnostic_group d;
7762 range_label_for_type_mismatch rhs_label (rhstype, type);
7763 gcc_rich_location richloc (expr_loc, &rhs_label);
7764 if (pedwarn (&richloc, OPT_Wpointer_sign,
7765 "pointer targets in passing argument %d of "
7766 "%qE differ in signedness", parmnum, rname))
7767 inform_for_arg (fundecl, expr_loc, parmnum, type,
7768 rhstype);
7770 break;
7771 case ic_assign:
7772 pedwarn (location, OPT_Wpointer_sign,
7773 "pointer targets in assignment from %qT to %qT "
7774 "differ in signedness", rhstype, type);
7775 break;
7776 case ic_init:
7777 case ic_init_const:
7778 pedwarn_init (location, OPT_Wpointer_sign,
7779 "pointer targets in initialization of %qT "
7780 "from %qT differ in signedness", type,
7781 rhstype);
7782 break;
7783 case ic_return:
7784 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7785 "returning %qT from a function with return type "
7786 "%qT differ in signedness", rhstype, type);
7787 break;
7788 default:
7789 gcc_unreachable ();
7792 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7793 && TREE_CODE (ttr) == FUNCTION_TYPE)
7795 /* Because const and volatile on functions are restrictions
7796 that say the function will not do certain things,
7797 it is okay to use a const or volatile function
7798 where an ordinary one is wanted, but not vice-versa. */
7799 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7800 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7801 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7802 OPT_Wdiscarded_qualifiers,
7803 G_("passing argument %d of %qE makes "
7804 "%q#v qualified function pointer "
7805 "from unqualified"),
7806 G_("assignment makes %q#v qualified function "
7807 "pointer from unqualified"),
7808 G_("initialization makes %q#v qualified "
7809 "function pointer from unqualified"),
7810 G_("return makes %q#v qualified function "
7811 "pointer from unqualified"),
7812 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7815 /* Avoid warning about the volatile ObjC EH puts on decls. */
7816 else if (!objc_ok)
7818 switch (errtype)
7820 case ic_argpass:
7822 auto_diagnostic_group d;
7823 range_label_for_type_mismatch rhs_label (rhstype, type);
7824 gcc_rich_location richloc (expr_loc, &rhs_label);
7825 if (permerror_opt (&richloc, OPT_Wincompatible_pointer_types,
7826 "passing argument %d of %qE from "
7827 "incompatible pointer type",
7828 parmnum, rname))
7829 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7831 break;
7832 case ic_assign:
7833 if (bltin)
7834 permerror_opt (location, OPT_Wincompatible_pointer_types,
7835 "assignment to %qT from pointer to "
7836 "%qD with incompatible type %qT",
7837 type, bltin, rhstype);
7838 else
7839 permerror_opt (location, OPT_Wincompatible_pointer_types,
7840 "assignment to %qT from incompatible pointer "
7841 "type %qT", type, rhstype);
7842 break;
7843 case ic_init:
7844 case ic_init_const:
7845 if (bltin)
7846 permerror_init (location, OPT_Wincompatible_pointer_types,
7847 "initialization of %qT from pointer to "
7848 "%qD with incompatible type %qT",
7849 type, bltin, rhstype);
7850 else
7851 permerror_init (location, OPT_Wincompatible_pointer_types,
7852 "initialization of %qT from incompatible "
7853 "pointer type %qT",
7854 type, rhstype);
7855 break;
7856 case ic_return:
7857 if (bltin)
7858 permerror_opt (location, OPT_Wincompatible_pointer_types,
7859 "returning pointer to %qD of type %qT from "
7860 "a function with incompatible type %qT",
7861 bltin, rhstype, type);
7862 else
7863 permerror_opt (location, OPT_Wincompatible_pointer_types,
7864 "returning %qT from a function with "
7865 "incompatible return type %qT", rhstype, type);
7866 break;
7867 default:
7868 gcc_unreachable ();
7872 /* If RHS isn't an address, check pointer or array of packed
7873 struct or union. */
7874 warn_for_address_of_packed_member (type, orig_rhs);
7876 return convert (type, rhs);
7878 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7880 /* ??? This should not be an error when inlining calls to
7881 unprototyped functions. */
7882 const char msg[] = "invalid use of non-lvalue array";
7883 if (warnopt)
7884 warning_at (location, warnopt, msg);
7885 else
7886 error_at (location, msg);
7887 return error_mark_node;
7889 else if (codel == POINTER_TYPE
7890 && (coder == INTEGER_TYPE
7891 || coder == NULLPTR_TYPE
7892 || coder == BITINT_TYPE))
7894 /* An explicit constant 0 or type nullptr_t can convert to a pointer,
7895 or one that results from arithmetic, even including a cast to
7896 integer type. */
7897 if (!null_pointer_constant && coder != NULLPTR_TYPE)
7898 switch (errtype)
7900 case ic_argpass:
7902 auto_diagnostic_group d;
7903 range_label_for_type_mismatch rhs_label (rhstype, type);
7904 gcc_rich_location richloc (expr_loc, &rhs_label);
7905 if (permerror_opt (&richloc, OPT_Wint_conversion,
7906 "passing argument %d of %qE makes pointer "
7907 "from integer without a cast", parmnum, rname))
7908 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7910 break;
7911 case ic_assign:
7912 permerror_opt (location, OPT_Wint_conversion,
7913 "assignment to %qT from %qT makes pointer from "
7914 "integer without a cast", type, rhstype);
7915 break;
7916 case ic_init:
7917 case ic_init_const:
7918 permerror_init (location, OPT_Wint_conversion,
7919 "initialization of %qT from %qT makes pointer "
7920 "from integer without a cast", type, rhstype);
7921 break;
7922 case ic_return:
7923 permerror_init (location, OPT_Wint_conversion,
7924 "returning %qT from a function with return type "
7925 "%qT makes pointer from integer without a cast",
7926 rhstype, type);
7927 break;
7928 default:
7929 gcc_unreachable ();
7932 return convert (type, rhs);
7934 else if ((codel == INTEGER_TYPE || codel == BITINT_TYPE)
7935 && coder == POINTER_TYPE)
7937 switch (errtype)
7939 case ic_argpass:
7941 auto_diagnostic_group d;
7942 range_label_for_type_mismatch rhs_label (rhstype, type);
7943 gcc_rich_location richloc (expr_loc, &rhs_label);
7944 if (permerror_opt (&richloc, OPT_Wint_conversion,
7945 "passing argument %d of %qE makes integer from "
7946 "pointer without a cast", parmnum, rname))
7947 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7949 break;
7950 case ic_assign:
7951 permerror_opt (location, OPT_Wint_conversion,
7952 "assignment to %qT from %qT makes integer from "
7953 "pointer without a cast", type, rhstype);
7954 break;
7955 case ic_init:
7956 case ic_init_const:
7957 permerror_init (location, OPT_Wint_conversion,
7958 "initialization of %qT from %qT makes integer "
7959 "from pointer without a cast", type, rhstype);
7960 break;
7961 case ic_return:
7962 permerror_opt (location, OPT_Wint_conversion, "returning %qT from a "
7963 "function with return type %qT makes integer from "
7964 "pointer without a cast", rhstype, type);
7965 break;
7966 default:
7967 gcc_unreachable ();
7970 return convert (type, rhs);
7972 else if (C_BOOLEAN_TYPE_P (type)
7973 /* The type nullptr_t may be converted to bool. The
7974 result is false. */
7975 && (coder == POINTER_TYPE || coder == NULLPTR_TYPE))
7977 tree ret;
7978 bool save = in_late_binary_op;
7979 in_late_binary_op = true;
7980 ret = convert (type, rhs);
7981 in_late_binary_op = save;
7982 return ret;
7984 else if (codel == NULLPTR_TYPE && null_pointer_constant)
7985 return convert (type, rhs);
7987 switch (errtype)
7989 case ic_argpass:
7991 auto_diagnostic_group d;
7992 range_label_for_type_mismatch rhs_label (rhstype, type);
7993 gcc_rich_location richloc (expr_loc, &rhs_label);
7994 const char msg[] = G_("incompatible type for argument %d of %qE");
7995 if (warnopt)
7996 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7997 else
7998 error_at (&richloc, msg, parmnum, rname);
7999 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
8001 break;
8002 case ic_assign:
8004 const char msg[]
8005 = G_("incompatible types when assigning to type %qT from type %qT");
8006 if (warnopt)
8007 warning_at (expr_loc, 0, msg, type, rhstype);
8008 else
8009 error_at (expr_loc, msg, type, rhstype);
8010 break;
8012 case ic_init:
8013 case ic_init_const:
8015 const char msg[]
8016 = G_("incompatible types when initializing type %qT using type %qT");
8017 if (warnopt)
8018 warning_at (location, 0, msg, type, rhstype);
8019 else
8020 error_at (location, msg, type, rhstype);
8021 break;
8023 case ic_return:
8025 const char msg[]
8026 = G_("incompatible types when returning type %qT but %qT was expected");
8027 if (warnopt)
8028 warning_at (location, 0, msg, rhstype, type);
8029 else
8030 error_at (location, msg, rhstype, type);
8031 break;
8033 default:
8034 gcc_unreachable ();
8037 return error_mark_node;
8040 /* If VALUE is a compound expr all of whose expressions are constant, then
8041 return its value. Otherwise, return error_mark_node.
8043 This is for handling COMPOUND_EXPRs as initializer elements
8044 which is allowed with a warning when -pedantic is specified. */
8046 static tree
8047 valid_compound_expr_initializer (tree value, tree endtype)
8049 if (TREE_CODE (value) == COMPOUND_EXPR)
8051 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
8052 == error_mark_node)
8053 return error_mark_node;
8054 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
8055 endtype);
8057 else if (!initializer_constant_valid_p (value, endtype))
8058 return error_mark_node;
8059 else
8060 return value;
8063 /* Perform appropriate conversions on the initial value of a variable,
8064 store it in the declaration DECL,
8065 and print any error messages that are appropriate.
8066 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
8067 If the init is invalid, store an ERROR_MARK.
8069 INIT_LOC is the location of the initial value. */
8071 void
8072 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
8074 tree value, type;
8075 bool npc = false;
8076 bool int_const_expr = false;
8077 bool arith_const_expr = false;
8079 /* If variable's type was invalidly declared, just ignore it. */
8081 type = TREE_TYPE (decl);
8082 if (TREE_CODE (type) == ERROR_MARK)
8083 return;
8085 /* Digest the specified initializer into an expression. */
8087 if (init)
8089 npc = null_pointer_constant_p (init);
8090 int_const_expr = (TREE_CODE (init) == INTEGER_CST
8091 && !TREE_OVERFLOW (init)
8092 && INTEGRAL_TYPE_P (TREE_TYPE (init)));
8093 /* Not fully determined before folding. */
8094 arith_const_expr = true;
8096 bool constexpr_p = (VAR_P (decl)
8097 && C_DECL_DECLARED_CONSTEXPR (decl));
8098 value = digest_init (init_loc, type, init, origtype, npc, int_const_expr,
8099 arith_const_expr, true,
8100 TREE_STATIC (decl) || constexpr_p, constexpr_p);
8102 /* Store the expression if valid; else report error. */
8104 if (!in_system_header_at (input_location)
8105 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
8106 warning (OPT_Wtraditional, "traditional C rejects automatic "
8107 "aggregate initialization");
8109 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
8110 DECL_INITIAL (decl) = value;
8112 /* ANSI wants warnings about out-of-range constant initializers. */
8113 STRIP_TYPE_NOPS (value);
8114 if (TREE_STATIC (decl))
8115 constant_expression_warning (value);
8117 /* Check if we need to set array size from compound literal size. */
8118 if (TREE_CODE (type) == ARRAY_TYPE
8119 && TYPE_DOMAIN (type) == NULL_TREE
8120 && value != error_mark_node)
8122 tree inside_init = init;
8124 STRIP_TYPE_NOPS (inside_init);
8125 inside_init = fold (inside_init);
8127 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8129 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8131 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
8133 /* For int foo[] = (int [3]){1}; we need to set array size
8134 now since later on array initializer will be just the
8135 brace enclosed list of the compound literal. */
8136 tree etype = strip_array_types (TREE_TYPE (decl));
8137 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8138 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
8139 layout_type (type);
8140 layout_decl (cldecl, 0);
8141 TREE_TYPE (decl)
8142 = c_build_qualified_type (type, TYPE_QUALS (etype));
8148 /* Methods for storing and printing names for error messages. */
8150 /* Implement a spelling stack that allows components of a name to be pushed
8151 and popped. Each element on the stack is this structure. */
8153 struct spelling
8155 int kind;
8156 union
8158 unsigned HOST_WIDE_INT i;
8159 const char *s;
8160 } u;
8163 #define SPELLING_STRING 1
8164 #define SPELLING_MEMBER 2
8165 #define SPELLING_BOUNDS 3
8167 static struct spelling *spelling; /* Next stack element (unused). */
8168 static struct spelling *spelling_base; /* Spelling stack base. */
8169 static int spelling_size; /* Size of the spelling stack. */
8171 /* Macros to save and restore the spelling stack around push_... functions.
8172 Alternative to SAVE_SPELLING_STACK. */
8174 #define SPELLING_DEPTH() (spelling - spelling_base)
8175 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
8177 /* Push an element on the spelling stack with type KIND and assign VALUE
8178 to MEMBER. */
8180 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
8182 int depth = SPELLING_DEPTH (); \
8184 if (depth >= spelling_size) \
8186 spelling_size += 10; \
8187 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
8188 spelling_size); \
8189 RESTORE_SPELLING_DEPTH (depth); \
8192 spelling->kind = (KIND); \
8193 spelling->MEMBER = (VALUE); \
8194 spelling++; \
8197 /* Push STRING on the stack. Printed literally. */
8199 static void
8200 push_string (const char *string)
8202 PUSH_SPELLING (SPELLING_STRING, string, u.s);
8205 /* Push a member name on the stack. Printed as '.' STRING. */
8207 static void
8208 push_member_name (tree decl)
8210 const char *const string
8211 = (DECL_NAME (decl)
8212 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
8213 : _("<anonymous>"));
8214 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
8217 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
8219 static void
8220 push_array_bounds (unsigned HOST_WIDE_INT bounds)
8222 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
8225 /* Compute the maximum size in bytes of the printed spelling. */
8227 static int
8228 spelling_length (void)
8230 int size = 0;
8231 struct spelling *p;
8233 for (p = spelling_base; p < spelling; p++)
8235 if (p->kind == SPELLING_BOUNDS)
8236 size += 25;
8237 else
8238 size += strlen (p->u.s) + 1;
8241 return size;
8244 /* Print the spelling to BUFFER and return it. */
8246 static char *
8247 print_spelling (char *buffer)
8249 char *d = buffer;
8250 struct spelling *p;
8252 for (p = spelling_base; p < spelling; p++)
8253 if (p->kind == SPELLING_BOUNDS)
8255 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
8256 d += strlen (d);
8258 else
8260 const char *s;
8261 if (p->kind == SPELLING_MEMBER)
8262 *d++ = '.';
8263 for (s = p->u.s; (*d = *s++); d++)
8266 *d++ = '\0';
8267 return buffer;
8270 /* Check whether INIT, a floating or integer constant, is
8271 representable in TYPE, a real floating type with the same radix or
8272 a decimal floating type initialized with a binary floating
8273 constant. Return true if OK, false if not. */
8274 static bool
8275 constexpr_init_fits_real_type (tree type, tree init)
8277 gcc_assert (SCALAR_FLOAT_TYPE_P (type));
8278 gcc_assert (TREE_CODE (init) == INTEGER_CST || TREE_CODE (init) == REAL_CST);
8279 if (TREE_CODE (init) == REAL_CST
8280 && TYPE_MODE (TREE_TYPE (init)) == TYPE_MODE (type))
8282 /* Same mode, no conversion required except for the case of
8283 signaling NaNs if the types are incompatible (e.g. double and
8284 long double with the same mode). */
8285 if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init))
8286 && !comptypes (TYPE_MAIN_VARIANT (type),
8287 TYPE_MAIN_VARIANT (TREE_TYPE (init))))
8288 return false;
8289 return true;
8291 if (TREE_CODE (init) == INTEGER_CST)
8293 tree converted = build_real_from_int_cst (type, init);
8294 bool fail = false;
8295 wide_int w = real_to_integer (&TREE_REAL_CST (converted), &fail,
8296 TYPE_PRECISION (TREE_TYPE (init)));
8297 return !fail && wi::eq_p (w, wi::to_wide (init));
8299 if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init)))
8300 return false;
8301 if ((REAL_VALUE_ISINF (TREE_REAL_CST (init))
8302 && MODE_HAS_INFINITIES (TYPE_MODE (type)))
8303 || (REAL_VALUE_ISNAN (TREE_REAL_CST (init))
8304 && MODE_HAS_NANS (TYPE_MODE (type))))
8305 return true;
8306 if (DECIMAL_FLOAT_TYPE_P (type)
8307 && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init)))
8309 /* This is valid if the real number represented by the
8310 initializer can be exactly represented in the decimal
8311 type. Compare the values using MPFR. */
8312 REAL_VALUE_TYPE t;
8313 real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
8314 mpfr_t bin_val, dec_val;
8315 mpfr_init2 (bin_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
8316 mpfr_init2 (dec_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
8317 mpfr_from_real (bin_val, &TREE_REAL_CST (init), MPFR_RNDN);
8318 char string[256];
8319 real_to_decimal (string, &t, sizeof string, 0, 1);
8320 bool res = (mpfr_strtofr (dec_val, string, NULL, 10, MPFR_RNDN) == 0
8321 && mpfr_equal_p (bin_val, dec_val));
8322 mpfr_clear (bin_val);
8323 mpfr_clear (dec_val);
8324 return res;
8326 /* exact_real_truncate is not quite right here, since it doesn't
8327 allow even an exact conversion to subnormal values. */
8328 REAL_VALUE_TYPE t;
8329 real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
8330 return real_identical (&t, &TREE_REAL_CST (init));
8333 /* Check whether INIT (location LOC) is valid as a 'constexpr'
8334 initializer for type TYPE, and give an error if not. INIT has
8335 already been folded and verified to be constant. INT_CONST_EXPR
8336 and ARITH_CONST_EXPR say whether it is an integer constant
8337 expression or arithmetic constant expression, respectively. If
8338 TYPE is not a scalar type, this function does nothing. */
8340 static void
8341 check_constexpr_init (location_t loc, tree type, tree init,
8342 bool int_const_expr, bool arith_const_expr)
8344 if (POINTER_TYPE_P (type))
8346 /* The initializer must be null. */
8347 if (TREE_CODE (init) != INTEGER_CST || !integer_zerop (init))
8348 error_at (loc, "%<constexpr%> pointer initializer is not null");
8349 return;
8351 if (INTEGRAL_TYPE_P (type))
8353 /* The initializer must be an integer constant expression,
8354 representable in the target type. */
8355 if (!int_const_expr)
8356 error_at (loc, "%<constexpr%> integer initializer is not an "
8357 "integer constant expression");
8358 if (!int_fits_type_p (init, type))
8359 error_at (loc, "%<constexpr%> initializer not representable in "
8360 "type of object");
8361 return;
8363 /* We don't apply any extra checks to extension types such as vector
8364 or fixed-point types. */
8365 if (TREE_CODE (type) != REAL_TYPE && TREE_CODE (type) != COMPLEX_TYPE)
8366 return;
8367 if (!arith_const_expr)
8369 error_at (loc, "%<constexpr%> initializer is not an arithmetic "
8370 "constant expression");
8371 return;
8373 /* We don't apply any extra checks to complex integers. */
8374 if (TREE_CODE (type) == COMPLEX_TYPE
8375 && TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8376 return;
8377 /* Following N3082, a real type cannot be initialized from a complex
8378 type and a binary type cannot be initialized from a decimal type
8379 (but initializing a decimal type from a binary type is OK).
8380 Signaling NaN initializers are OK only if the types are
8381 compatible (not just the same mode); all quiet NaN and infinity
8382 initializations are considered to preserve the value. */
8383 if (TREE_CODE (TREE_TYPE (init)) == COMPLEX_TYPE
8384 && SCALAR_FLOAT_TYPE_P (type))
8386 error_at (loc, "%<constexpr%> initializer for a real type is of "
8387 "complex type");
8388 return;
8390 if (SCALAR_FLOAT_TYPE_P (type)
8391 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (init))
8392 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init))
8393 && !DECIMAL_FLOAT_TYPE_P (type))
8395 error_at (loc, "%<constexpr%> initializer for a binary "
8396 "floating-point type is of decimal type");
8397 return;
8399 bool fits;
8400 if (TREE_CODE (type) == COMPLEX_TYPE)
8402 switch (TREE_CODE (init))
8404 case INTEGER_CST:
8405 case REAL_CST:
8406 fits = constexpr_init_fits_real_type (TREE_TYPE (type), init);
8407 break;
8408 case COMPLEX_CST:
8409 fits = (constexpr_init_fits_real_type (TREE_TYPE (type),
8410 TREE_REALPART (init))
8411 && constexpr_init_fits_real_type (TREE_TYPE (type),
8412 TREE_IMAGPART (init)));
8413 break;
8414 default:
8415 gcc_unreachable ();
8418 else
8419 fits = constexpr_init_fits_real_type (type, init);
8420 if (!fits)
8421 error_at (loc, "%<constexpr%> initializer not representable in "
8422 "type of object");
8425 /* Digest the parser output INIT as an initializer for type TYPE.
8426 Return a C expression of type TYPE to represent the initial value.
8428 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
8430 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant,
8431 INT_CONST_EXPR is true if INIT is an integer constant expression,
8432 and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic
8433 constant expression, false if it has already been determined in the
8434 caller that it is not (but folding may have made the value passed here
8435 indistinguishable from an arithmetic constant expression).
8437 If INIT is a string constant, STRICT_STRING is true if it is
8438 unparenthesized or we should not warn here for it being parenthesized.
8439 For other types of INIT, STRICT_STRING is not used.
8441 INIT_LOC is the location of the INIT.
8443 REQUIRE_CONSTANT requests an error if non-constant initializers or
8444 elements are seen. REQUIRE_CONSTEXPR means the stricter requirements
8445 on initializers for 'constexpr' objects apply. */
8447 static tree
8448 digest_init (location_t init_loc, tree type, tree init, tree origtype,
8449 bool null_pointer_constant, bool int_const_expr,
8450 bool arith_const_expr, bool strict_string,
8451 bool require_constant, bool require_constexpr)
8453 enum tree_code code = TREE_CODE (type);
8454 tree inside_init = init;
8455 tree semantic_type = NULL_TREE;
8456 bool maybe_const = true;
8458 if (type == error_mark_node
8459 || !init
8460 || error_operand_p (init))
8461 return error_mark_node;
8463 STRIP_TYPE_NOPS (inside_init);
8465 if (!c_in_omp_for)
8467 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
8469 semantic_type = TREE_TYPE (inside_init);
8470 inside_init = TREE_OPERAND (inside_init, 0);
8472 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
8474 /* TODO: this may not detect all cases of expressions folding to
8475 constants that are not arithmetic constant expressions. */
8476 if (!maybe_const)
8477 arith_const_expr = false;
8478 else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init))
8479 && TREE_CODE (TREE_TYPE (inside_init)) != REAL_TYPE
8480 && TREE_CODE (TREE_TYPE (inside_init)) != COMPLEX_TYPE)
8481 arith_const_expr = false;
8482 else if (TREE_CODE (inside_init) != INTEGER_CST
8483 && TREE_CODE (inside_init) != REAL_CST
8484 && TREE_CODE (inside_init) != COMPLEX_CST)
8485 arith_const_expr = false;
8486 else if (TREE_OVERFLOW (inside_init))
8487 arith_const_expr = false;
8489 /* Initialization of an array of chars from a string constant
8490 optionally enclosed in braces. */
8492 if (code == ARRAY_TYPE && inside_init
8493 && TREE_CODE (inside_init) == STRING_CST)
8495 tree typ1
8496 = (TYPE_ATOMIC (TREE_TYPE (type))
8497 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
8498 TYPE_QUAL_ATOMIC)
8499 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
8500 /* Note that an array could be both an array of character type
8501 and an array of wchar_t if wchar_t is signed char or unsigned
8502 char. */
8503 bool char_array = (typ1 == char_type_node
8504 || typ1 == signed_char_type_node
8505 || typ1 == unsigned_char_type_node);
8506 bool wchar_array = !!comptypes (typ1, wchar_type_node);
8507 bool char16_array = !!comptypes (typ1, char16_type_node);
8508 bool char32_array = !!comptypes (typ1, char32_type_node);
8510 if (char_array || wchar_array || char16_array || char32_array)
8512 struct c_expr expr;
8513 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
8514 bool incompat_string_cst = false;
8515 expr.value = inside_init;
8516 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
8517 expr.original_type = NULL;
8518 expr.m_decimal = 0;
8519 maybe_warn_string_init (init_loc, type, expr);
8521 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8522 pedwarn_init (init_loc, OPT_Wpedantic,
8523 "initialization of a flexible array member");
8525 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8526 TYPE_MAIN_VARIANT (type)))
8527 return inside_init;
8529 if (char_array)
8531 if (typ2 != char_type_node && typ2 != char8_type_node)
8532 incompat_string_cst = true;
8534 else if (!comptypes (typ1, typ2))
8535 incompat_string_cst = true;
8537 if (incompat_string_cst)
8539 error_init (init_loc, "cannot initialize array of %qT from "
8540 "a string literal with type array of %qT",
8541 typ1, typ2);
8542 return error_mark_node;
8545 if (require_constexpr
8546 && TYPE_UNSIGNED (typ1) != TYPE_UNSIGNED (typ2))
8548 /* Check if all characters of the string can be
8549 represented in the type of the constexpr object being
8550 initialized. */
8551 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8552 const unsigned char *p =
8553 (const unsigned char *) TREE_STRING_POINTER (inside_init);
8554 gcc_assert (CHAR_TYPE_SIZE == 8 && CHAR_BIT == 8);
8555 for (unsigned i = 0; i < len; i++)
8556 if (p[i] > 127)
8558 error_init (init_loc, "%<constexpr%> initializer not "
8559 "representable in type of object");
8560 break;
8564 if (TYPE_DOMAIN (type) != NULL_TREE
8565 && TYPE_SIZE (type) != NULL_TREE
8566 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8568 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8569 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
8571 /* Subtract the size of a single (possibly wide) character
8572 because it's ok to ignore the terminating null char
8573 that is counted in the length of the constant. */
8574 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
8575 pedwarn_init (init_loc, 0,
8576 ("initializer-string for array of %qT "
8577 "is too long"), typ1);
8578 else if (warn_cxx_compat
8579 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8580 warning_at (init_loc, OPT_Wc___compat,
8581 ("initializer-string for array of %qT "
8582 "is too long for C++"), typ1);
8583 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8585 unsigned HOST_WIDE_INT size
8586 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8587 const char *p = TREE_STRING_POINTER (inside_init);
8589 inside_init = build_string (size, p);
8593 TREE_TYPE (inside_init) = type;
8594 return inside_init;
8596 else if (INTEGRAL_TYPE_P (typ1))
8598 error_init (init_loc, "array of inappropriate type initialized "
8599 "from string constant");
8600 return error_mark_node;
8604 /* Build a VECTOR_CST from a *constant* vector constructor. If the
8605 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
8606 below and handle as a constructor. */
8607 if (code == VECTOR_TYPE
8608 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
8609 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
8610 && TREE_CONSTANT (inside_init))
8612 if (TREE_CODE (inside_init) == VECTOR_CST
8613 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8614 TYPE_MAIN_VARIANT (type)))
8615 return inside_init;
8617 if (TREE_CODE (inside_init) == CONSTRUCTOR)
8619 unsigned HOST_WIDE_INT ix;
8620 tree value;
8621 bool constant_p = true;
8623 /* Iterate through elements and check if all constructor
8624 elements are *_CSTs. */
8625 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
8626 if (!CONSTANT_CLASS_P (value))
8628 constant_p = false;
8629 break;
8632 if (constant_p)
8633 return build_vector_from_ctor (type,
8634 CONSTRUCTOR_ELTS (inside_init));
8638 if (warn_sequence_point)
8639 verify_sequence_points (inside_init);
8641 /* Any type can be initialized
8642 from an expression of the same type, optionally with braces. */
8644 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
8645 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8646 TYPE_MAIN_VARIANT (type))
8647 || (code == ARRAY_TYPE
8648 && comptypes (TREE_TYPE (inside_init), type))
8649 || (gnu_vector_type_p (type)
8650 && comptypes (TREE_TYPE (inside_init), type))
8651 || (code == POINTER_TYPE
8652 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
8653 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
8654 TREE_TYPE (type)))))
8656 if (code == POINTER_TYPE)
8658 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
8660 if (TREE_CODE (inside_init) == STRING_CST
8661 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8662 inside_init = array_to_pointer_conversion
8663 (init_loc, inside_init);
8664 else
8666 error_init (init_loc, "invalid use of non-lvalue array");
8667 return error_mark_node;
8672 if (code == VECTOR_TYPE || c_hardbool_type_attr (type))
8673 /* Although the types are compatible, we may require a
8674 conversion. */
8675 inside_init = convert (type, inside_init);
8677 if ((code == RECORD_TYPE || code == UNION_TYPE)
8678 && !comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (TREE_TYPE (inside_init))))
8680 error_init (init_loc, "invalid initializer");
8681 return error_mark_node;
8684 if (require_constant
8685 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8687 /* As an extension, allow initializing objects with static storage
8688 duration with compound literals (which are then treated just as
8689 the brace enclosed list they contain). Also allow this for
8690 vectors, as we can only assign them with compound literals. */
8691 if (flag_isoc99 && code != VECTOR_TYPE)
8692 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
8693 "is not constant");
8694 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8695 inside_init = DECL_INITIAL (decl);
8698 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
8699 && TREE_CODE (inside_init) != CONSTRUCTOR)
8701 error_init (init_loc, "array initialized from non-constant array "
8702 "expression");
8703 return error_mark_node;
8706 /* Compound expressions can only occur here if -Wpedantic or
8707 -pedantic-errors is specified. In the later case, we always want
8708 an error. In the former case, we simply want a warning. */
8709 if (require_constant && pedantic
8710 && TREE_CODE (inside_init) == COMPOUND_EXPR)
8712 inside_init
8713 = valid_compound_expr_initializer (inside_init,
8714 TREE_TYPE (inside_init));
8715 if (inside_init == error_mark_node)
8716 error_init (init_loc, "initializer element is not constant");
8717 else
8718 pedwarn_init (init_loc, OPT_Wpedantic,
8719 "initializer element is not constant");
8720 if (flag_pedantic_errors)
8721 inside_init = error_mark_node;
8723 else if (require_constant
8724 && !initializer_constant_valid_p (inside_init,
8725 TREE_TYPE (inside_init)))
8727 error_init (init_loc, "initializer element is not constant");
8728 inside_init = error_mark_node;
8730 else if (require_constant && !maybe_const)
8731 pedwarn_init (init_loc, OPT_Wpedantic,
8732 "initializer element is not a constant expression");
8733 else if (require_constexpr)
8734 check_constexpr_init (init_loc, type, inside_init,
8735 int_const_expr, arith_const_expr);
8737 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8738 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8739 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8740 type, inside_init, origtype,
8741 (require_constant
8742 ? ic_init_const
8743 : ic_init), null_pointer_constant,
8744 NULL_TREE, NULL_TREE, 0);
8745 return inside_init;
8748 /* Handle scalar types, including conversions. */
8750 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8751 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8752 || code == COMPLEX_TYPE || code == VECTOR_TYPE || code == NULLPTR_TYPE
8753 || code == BITINT_TYPE)
8755 tree unconverted_init = inside_init;
8756 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8757 && (TREE_CODE (init) == STRING_CST
8758 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8759 inside_init = init = array_to_pointer_conversion (init_loc, init);
8760 if (semantic_type)
8761 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8762 inside_init);
8763 inside_init
8764 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8765 inside_init, origtype,
8766 require_constant ? ic_init_const : ic_init,
8767 null_pointer_constant, NULL_TREE, NULL_TREE,
8770 /* Check to see if we have already given an error message. */
8771 if (inside_init == error_mark_node)
8773 else if (require_constant && !TREE_CONSTANT (inside_init))
8775 error_init (init_loc, "initializer element is not constant");
8776 inside_init = error_mark_node;
8778 else if (require_constant
8779 && !initializer_constant_valid_p (inside_init,
8780 TREE_TYPE (inside_init)))
8782 error_init (init_loc, "initializer element is not computable at "
8783 "load time");
8784 inside_init = error_mark_node;
8786 else if (require_constant && !maybe_const)
8787 pedwarn_init (init_loc, OPT_Wpedantic,
8788 "initializer element is not a constant expression");
8789 else if (require_constexpr)
8790 check_constexpr_init (init_loc, type, unconverted_init,
8791 int_const_expr, arith_const_expr);
8793 return inside_init;
8796 /* Come here only for records and arrays. */
8798 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8800 error_init (init_loc,
8801 "variable-sized object may not be initialized except "
8802 "with an empty initializer");
8803 return error_mark_node;
8806 error_init (init_loc, "invalid initializer");
8807 return error_mark_node;
8810 /* Handle initializers that use braces. */
8812 /* Type of object we are accumulating a constructor for.
8813 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8814 static tree constructor_type;
8816 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8817 left to fill. */
8818 static tree constructor_fields;
8820 /* For an ARRAY_TYPE, this is the specified index
8821 at which to store the next element we get. */
8822 static tree constructor_index;
8824 /* For an ARRAY_TYPE, this is the maximum index. */
8825 static tree constructor_max_index;
8827 /* For a RECORD_TYPE, this is the first field not yet written out. */
8828 static tree constructor_unfilled_fields;
8830 /* For an ARRAY_TYPE, this is the index of the first element
8831 not yet written out. */
8832 static tree constructor_unfilled_index;
8834 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8835 This is so we can generate gaps between fields, when appropriate. */
8836 static tree constructor_bit_index;
8838 /* If we are saving up the elements rather than allocating them,
8839 this is the list of elements so far (in reverse order,
8840 most recent first). */
8841 static vec<constructor_elt, va_gc> *constructor_elements;
8843 /* 1 if constructor should be incrementally stored into a constructor chain,
8844 0 if all the elements should be kept in AVL tree. */
8845 static int constructor_incremental;
8847 /* 1 if so far this constructor's elements are all compile-time constants. */
8848 static int constructor_constant;
8850 /* 1 if so far this constructor's elements are all valid address constants. */
8851 static int constructor_simple;
8853 /* 1 if this constructor has an element that cannot be part of a
8854 constant expression. */
8855 static int constructor_nonconst;
8857 /* 1 if this constructor is erroneous so far. */
8858 static int constructor_erroneous;
8860 /* 1 if this constructor is the universal zero initializer { 0 }. */
8861 static int constructor_zeroinit;
8863 /* Structure for managing pending initializer elements, organized as an
8864 AVL tree. */
8866 struct init_node
8868 struct init_node *left, *right;
8869 struct init_node *parent;
8870 int balance;
8871 tree purpose;
8872 tree value;
8873 tree origtype;
8876 /* Tree of pending elements at this constructor level.
8877 These are elements encountered out of order
8878 which belong at places we haven't reached yet in actually
8879 writing the output.
8880 Will never hold tree nodes across GC runs. */
8881 static struct init_node *constructor_pending_elts;
8883 /* The SPELLING_DEPTH of this constructor. */
8884 static int constructor_depth;
8886 /* DECL node for which an initializer is being read.
8887 0 means we are reading a constructor expression
8888 such as (struct foo) {...}. */
8889 static tree constructor_decl;
8891 /* Nonzero if there were any member designators in this initializer. */
8892 static int constructor_designated;
8894 /* Nesting depth of designator list. */
8895 static int designator_depth;
8897 /* Nonzero if there were diagnosed errors in this designator list. */
8898 static int designator_erroneous;
8901 /* This stack has a level for each implicit or explicit level of
8902 structuring in the initializer, including the outermost one. It
8903 saves the values of most of the variables above. */
8905 struct constructor_range_stack;
8907 struct constructor_stack
8909 struct constructor_stack *next;
8910 tree type;
8911 tree fields;
8912 tree index;
8913 tree max_index;
8914 tree unfilled_index;
8915 tree unfilled_fields;
8916 tree bit_index;
8917 vec<constructor_elt, va_gc> *elements;
8918 struct init_node *pending_elts;
8919 int offset;
8920 int depth;
8921 /* If value nonzero, this value should replace the entire
8922 constructor at this level. */
8923 struct c_expr replacement_value;
8924 struct constructor_range_stack *range_stack;
8925 char constant;
8926 char simple;
8927 char nonconst;
8928 char implicit;
8929 char erroneous;
8930 char outer;
8931 char incremental;
8932 char designated;
8933 int designator_depth;
8936 static struct constructor_stack *constructor_stack;
8938 /* This stack represents designators from some range designator up to
8939 the last designator in the list. */
8941 struct constructor_range_stack
8943 struct constructor_range_stack *next, *prev;
8944 struct constructor_stack *stack;
8945 tree range_start;
8946 tree index;
8947 tree range_end;
8948 tree fields;
8951 static struct constructor_range_stack *constructor_range_stack;
8953 /* This stack records separate initializers that are nested.
8954 Nested initializers can't happen in ANSI C, but GNU C allows them
8955 in cases like { ... (struct foo) { ... } ... }. */
8957 struct initializer_stack
8959 struct initializer_stack *next;
8960 tree decl;
8961 struct constructor_stack *constructor_stack;
8962 struct constructor_range_stack *constructor_range_stack;
8963 vec<constructor_elt, va_gc> *elements;
8964 struct spelling *spelling;
8965 struct spelling *spelling_base;
8966 int spelling_size;
8967 char require_constant_value;
8968 char require_constant_elements;
8969 char require_constexpr_value;
8970 char designated;
8971 rich_location *missing_brace_richloc;
8974 static struct initializer_stack *initializer_stack;
8976 /* Prepare to parse and output the initializer for variable DECL. */
8978 void
8979 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED,
8980 bool init_require_constant, bool init_require_constexpr,
8981 rich_location *richloc)
8983 const char *locus;
8984 struct initializer_stack *p = XNEW (struct initializer_stack);
8986 p->decl = constructor_decl;
8987 p->require_constant_value = require_constant_value;
8988 p->require_constant_elements = require_constant_elements;
8989 p->require_constexpr_value = require_constexpr_value;
8990 p->constructor_stack = constructor_stack;
8991 p->constructor_range_stack = constructor_range_stack;
8992 p->elements = constructor_elements;
8993 p->spelling = spelling;
8994 p->spelling_base = spelling_base;
8995 p->spelling_size = spelling_size;
8996 p->next = initializer_stack;
8997 p->missing_brace_richloc = richloc;
8998 p->designated = constructor_designated;
8999 initializer_stack = p;
9001 constructor_decl = decl;
9002 constructor_designated = 0;
9004 require_constant_value = init_require_constant;
9005 require_constexpr_value = init_require_constexpr;
9006 if (decl != NULL_TREE && decl != error_mark_node)
9008 require_constant_elements
9009 = ((init_require_constant || (pedantic && !flag_isoc99))
9010 /* For a scalar, you can always use any value to initialize,
9011 even within braces. */
9012 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
9013 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
9015 else
9017 require_constant_elements = false;
9018 locus = _("(anonymous)");
9021 constructor_stack = 0;
9022 constructor_range_stack = 0;
9024 found_missing_braces = 0;
9026 spelling_base = 0;
9027 spelling_size = 0;
9028 RESTORE_SPELLING_DEPTH (0);
9030 if (locus)
9031 push_string (locus);
9034 void
9035 finish_init (void)
9037 struct initializer_stack *p = initializer_stack;
9039 /* Free the whole constructor stack of this initializer. */
9040 while (constructor_stack)
9042 struct constructor_stack *q = constructor_stack;
9043 constructor_stack = q->next;
9044 XDELETE (q);
9047 gcc_assert (!constructor_range_stack);
9049 /* Pop back to the data of the outer initializer (if any). */
9050 XDELETE (spelling_base);
9052 constructor_decl = p->decl;
9053 require_constant_value = p->require_constant_value;
9054 require_constant_elements = p->require_constant_elements;
9055 require_constexpr_value = p->require_constexpr_value;
9056 constructor_stack = p->constructor_stack;
9057 constructor_designated = p->designated;
9058 constructor_range_stack = p->constructor_range_stack;
9059 constructor_elements = p->elements;
9060 spelling = p->spelling;
9061 spelling_base = p->spelling_base;
9062 spelling_size = p->spelling_size;
9063 initializer_stack = p->next;
9064 XDELETE (p);
9067 /* Call here when we see the initializer is surrounded by braces.
9068 This is instead of a call to push_init_level;
9069 it is matched by a call to pop_init_level.
9071 TYPE is the type to initialize, for a constructor expression.
9072 For an initializer for a decl, TYPE is zero. */
9074 void
9075 really_start_incremental_init (tree type)
9077 struct constructor_stack *p = XNEW (struct constructor_stack);
9079 if (type == NULL_TREE)
9080 type = TREE_TYPE (constructor_decl);
9082 if (VECTOR_TYPE_P (type)
9083 && TYPE_VECTOR_OPAQUE (type))
9084 error ("opaque vector types cannot be initialized");
9086 p->type = constructor_type;
9087 p->fields = constructor_fields;
9088 p->index = constructor_index;
9089 p->max_index = constructor_max_index;
9090 p->unfilled_index = constructor_unfilled_index;
9091 p->unfilled_fields = constructor_unfilled_fields;
9092 p->bit_index = constructor_bit_index;
9093 p->elements = constructor_elements;
9094 p->constant = constructor_constant;
9095 p->simple = constructor_simple;
9096 p->nonconst = constructor_nonconst;
9097 p->erroneous = constructor_erroneous;
9098 p->pending_elts = constructor_pending_elts;
9099 p->depth = constructor_depth;
9100 p->replacement_value.value = 0;
9101 p->replacement_value.original_code = ERROR_MARK;
9102 p->replacement_value.original_type = NULL;
9103 p->implicit = 0;
9104 p->range_stack = 0;
9105 p->outer = 0;
9106 p->incremental = constructor_incremental;
9107 p->designated = constructor_designated;
9108 p->designator_depth = designator_depth;
9109 p->next = 0;
9110 constructor_stack = p;
9112 constructor_constant = 1;
9113 constructor_simple = 1;
9114 constructor_nonconst = 0;
9115 constructor_depth = SPELLING_DEPTH ();
9116 constructor_elements = NULL;
9117 constructor_pending_elts = 0;
9118 constructor_type = type;
9119 constructor_incremental = 1;
9120 constructor_designated = 0;
9121 constructor_zeroinit = 1;
9122 designator_depth = 0;
9123 designator_erroneous = 0;
9125 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9127 constructor_fields = TYPE_FIELDS (constructor_type);
9128 /* Skip any nameless bit fields at the beginning. */
9129 while (constructor_fields != NULL_TREE
9130 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
9131 constructor_fields = DECL_CHAIN (constructor_fields);
9133 constructor_unfilled_fields = constructor_fields;
9134 constructor_bit_index = bitsize_zero_node;
9136 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9138 if (TYPE_DOMAIN (constructor_type))
9140 constructor_max_index
9141 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
9143 /* Detect non-empty initializations of zero-length arrays. */
9144 if (constructor_max_index == NULL_TREE
9145 && TYPE_SIZE (constructor_type))
9146 constructor_max_index = integer_minus_one_node;
9148 /* constructor_max_index needs to be an INTEGER_CST. Attempts
9149 to initialize VLAs with a nonempty initializer will cause a
9150 proper error; avoid tree checking errors as well by setting a
9151 safe value. */
9152 if (constructor_max_index
9153 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9154 constructor_max_index = integer_minus_one_node;
9156 constructor_index
9157 = convert (bitsizetype,
9158 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9160 else
9162 constructor_index = bitsize_zero_node;
9163 constructor_max_index = NULL_TREE;
9166 constructor_unfilled_index = constructor_index;
9168 else if (gnu_vector_type_p (constructor_type))
9170 /* Vectors are like simple fixed-size arrays. */
9171 constructor_max_index =
9172 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
9173 constructor_index = bitsize_zero_node;
9174 constructor_unfilled_index = constructor_index;
9176 else
9178 /* Handle the case of int x = {5}; */
9179 constructor_fields = constructor_type;
9180 constructor_unfilled_fields = constructor_type;
9184 extern location_t last_init_list_comma;
9186 /* Called when we see an open brace for a nested initializer. Finish
9187 off any pending levels with implicit braces. */
9188 void
9189 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
9191 while (constructor_stack->implicit)
9193 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9194 && constructor_fields == NULL_TREE)
9195 process_init_element (input_location,
9196 pop_init_level (loc, 1, braced_init_obstack,
9197 last_init_list_comma),
9198 true, braced_init_obstack);
9199 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
9200 && constructor_max_index
9201 && tree_int_cst_lt (constructor_max_index,
9202 constructor_index))
9203 process_init_element (input_location,
9204 pop_init_level (loc, 1, braced_init_obstack,
9205 last_init_list_comma),
9206 true, braced_init_obstack);
9207 else
9208 break;
9212 /* Push down into a subobject, for initialization.
9213 If this is for an explicit set of braces, IMPLICIT is 0.
9214 If it is because the next element belongs at a lower level,
9215 IMPLICIT is 1 (or 2 if the push is because of designator list). */
9217 void
9218 push_init_level (location_t loc, int implicit,
9219 struct obstack *braced_init_obstack)
9221 struct constructor_stack *p;
9222 tree value = NULL_TREE;
9224 /* Unless this is an explicit brace, we need to preserve previous
9225 content if any. */
9226 if (implicit)
9228 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
9229 value = find_init_member (constructor_fields, braced_init_obstack);
9230 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9231 value = find_init_member (constructor_index, braced_init_obstack);
9234 p = XNEW (struct constructor_stack);
9235 p->type = constructor_type;
9236 p->fields = constructor_fields;
9237 p->index = constructor_index;
9238 p->max_index = constructor_max_index;
9239 p->unfilled_index = constructor_unfilled_index;
9240 p->unfilled_fields = constructor_unfilled_fields;
9241 p->bit_index = constructor_bit_index;
9242 p->elements = constructor_elements;
9243 p->constant = constructor_constant;
9244 p->simple = constructor_simple;
9245 p->nonconst = constructor_nonconst;
9246 p->erroneous = constructor_erroneous;
9247 p->pending_elts = constructor_pending_elts;
9248 p->depth = constructor_depth;
9249 p->replacement_value.value = NULL_TREE;
9250 p->replacement_value.original_code = ERROR_MARK;
9251 p->replacement_value.original_type = NULL;
9252 p->implicit = implicit;
9253 p->outer = 0;
9254 p->incremental = constructor_incremental;
9255 p->designated = constructor_designated;
9256 p->designator_depth = designator_depth;
9257 p->next = constructor_stack;
9258 p->range_stack = 0;
9259 constructor_stack = p;
9261 constructor_constant = 1;
9262 constructor_simple = 1;
9263 constructor_nonconst = 0;
9264 constructor_depth = SPELLING_DEPTH ();
9265 constructor_elements = NULL;
9266 constructor_incremental = 1;
9267 /* If the upper initializer is designated, then mark this as
9268 designated too to prevent bogus warnings. */
9269 constructor_designated = p->designated;
9270 constructor_pending_elts = 0;
9271 if (!implicit)
9273 p->range_stack = constructor_range_stack;
9274 constructor_range_stack = 0;
9275 designator_depth = 0;
9276 designator_erroneous = 0;
9279 /* Don't die if an entire brace-pair level is superfluous
9280 in the containing level. */
9281 if (constructor_type == NULL_TREE)
9283 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9285 /* Don't die if there are extra init elts at the end. */
9286 if (constructor_fields == NULL_TREE)
9287 constructor_type = NULL_TREE;
9288 else
9290 constructor_type = TREE_TYPE (constructor_fields);
9291 push_member_name (constructor_fields);
9292 constructor_depth++;
9295 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9297 constructor_type = TREE_TYPE (constructor_type);
9298 push_array_bounds (tree_to_uhwi (constructor_index));
9299 constructor_depth++;
9302 if (constructor_type == NULL_TREE)
9304 error_init (loc, "extra brace group at end of initializer");
9305 constructor_fields = NULL_TREE;
9306 constructor_unfilled_fields = NULL_TREE;
9307 return;
9310 if (value && TREE_CODE (value) == CONSTRUCTOR)
9312 constructor_constant = TREE_CONSTANT (value);
9313 constructor_simple = TREE_STATIC (value);
9314 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
9315 constructor_elements = CONSTRUCTOR_ELTS (value);
9316 if (!vec_safe_is_empty (constructor_elements)
9317 && (TREE_CODE (constructor_type) == RECORD_TYPE
9318 || TREE_CODE (constructor_type) == ARRAY_TYPE))
9319 set_nonincremental_init (braced_init_obstack);
9322 if (implicit == 1)
9324 found_missing_braces = 1;
9325 if (initializer_stack->missing_brace_richloc)
9326 initializer_stack->missing_brace_richloc->add_fixit_insert_before
9327 (loc, "{");
9330 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9332 constructor_fields = TYPE_FIELDS (constructor_type);
9333 /* Skip any nameless bit fields at the beginning. */
9334 while (constructor_fields != NULL_TREE
9335 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
9336 constructor_fields = DECL_CHAIN (constructor_fields);
9338 constructor_unfilled_fields = constructor_fields;
9339 constructor_bit_index = bitsize_zero_node;
9341 else if (gnu_vector_type_p (constructor_type))
9343 /* Vectors are like simple fixed-size arrays. */
9344 constructor_max_index =
9345 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
9346 constructor_index = bitsize_int (0);
9347 constructor_unfilled_index = constructor_index;
9349 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9351 if (TYPE_DOMAIN (constructor_type))
9353 constructor_max_index
9354 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
9356 /* Detect non-empty initializations of zero-length arrays. */
9357 if (constructor_max_index == NULL_TREE
9358 && TYPE_SIZE (constructor_type))
9359 constructor_max_index = integer_minus_one_node;
9361 /* constructor_max_index needs to be an INTEGER_CST. Attempts
9362 to initialize VLAs will cause a proper error; avoid tree
9363 checking errors as well by setting a safe value. */
9364 if (constructor_max_index
9365 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9366 constructor_max_index = integer_minus_one_node;
9368 constructor_index
9369 = convert (bitsizetype,
9370 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9372 else
9373 constructor_index = bitsize_zero_node;
9375 constructor_unfilled_index = constructor_index;
9376 if (value && TREE_CODE (value) == STRING_CST)
9378 /* We need to split the char/wchar array into individual
9379 characters, so that we don't have to special case it
9380 everywhere. */
9381 set_nonincremental_init_from_string (value, braced_init_obstack);
9384 else
9386 if (constructor_type != error_mark_node)
9387 warning_init (input_location, 0, "braces around scalar initializer");
9388 constructor_fields = constructor_type;
9389 constructor_unfilled_fields = constructor_type;
9393 /* At the end of an implicit or explicit brace level,
9394 finish up that level of constructor. If a single expression
9395 with redundant braces initialized that level, return the
9396 c_expr structure for that expression. Otherwise, the original_code
9397 element is set to ERROR_MARK.
9398 If we were outputting the elements as they are read, return 0 as the value
9399 from inner levels (process_init_element ignores that),
9400 but return error_mark_node as the value from the outermost level
9401 (that's what we want to put in DECL_INITIAL).
9402 Otherwise, return a CONSTRUCTOR expression as the value. */
9404 struct c_expr
9405 pop_init_level (location_t loc, int implicit,
9406 struct obstack *braced_init_obstack,
9407 location_t insert_before)
9409 struct constructor_stack *p;
9410 struct c_expr ret;
9411 ret.value = NULL_TREE;
9412 ret.original_code = ERROR_MARK;
9413 ret.original_type = NULL;
9414 ret.m_decimal = 0;
9416 if (implicit == 0)
9418 /* When we come to an explicit close brace,
9419 pop any inner levels that didn't have explicit braces. */
9420 while (constructor_stack->implicit)
9421 process_init_element (input_location,
9422 pop_init_level (loc, 1, braced_init_obstack,
9423 insert_before),
9424 true, braced_init_obstack);
9425 gcc_assert (!constructor_range_stack);
9427 else
9428 if (initializer_stack->missing_brace_richloc)
9429 initializer_stack->missing_brace_richloc->add_fixit_insert_before
9430 (insert_before, "}");
9432 /* Now output all pending elements. */
9433 constructor_incremental = 1;
9434 output_pending_init_elements (1, braced_init_obstack);
9436 p = constructor_stack;
9438 /* Error for initializing a flexible array member, or a zero-length
9439 array member in an inappropriate context. */
9440 if (constructor_type && constructor_fields
9441 && TREE_CODE (constructor_type) == ARRAY_TYPE
9442 && TYPE_DOMAIN (constructor_type)
9443 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
9445 /* Silently discard empty initializations. The parser will
9446 already have pedwarned for empty brackets. */
9447 if (integer_zerop (constructor_unfilled_index))
9448 constructor_type = NULL_TREE;
9449 else
9451 gcc_assert (!TYPE_SIZE (constructor_type));
9453 if (constructor_depth > 2)
9454 error_init (loc, "initialization of flexible array member in a nested context");
9455 else
9456 pedwarn_init (loc, OPT_Wpedantic,
9457 "initialization of a flexible array member");
9459 /* We have already issued an error message for the existence
9460 of a flexible array member not at the end of the structure.
9461 Discard the initializer so that we do not die later. */
9462 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
9463 constructor_type = NULL_TREE;
9467 switch (vec_safe_length (constructor_elements))
9469 case 0:
9470 /* Initialization with { } counts as zeroinit. */
9471 constructor_zeroinit = 1;
9472 break;
9473 case 1:
9474 /* This might be zeroinit as well. */
9475 if (integer_zerop ((*constructor_elements)[0].value))
9476 constructor_zeroinit = 1;
9477 break;
9478 default:
9479 /* If the constructor has more than one element, it can't be { 0 }. */
9480 constructor_zeroinit = 0;
9481 break;
9484 /* Warn when some structs are initialized with direct aggregation. */
9485 if (!implicit && found_missing_braces && warn_missing_braces
9486 && !constructor_zeroinit)
9488 gcc_assert (initializer_stack->missing_brace_richloc);
9489 warning_at (initializer_stack->missing_brace_richloc,
9490 OPT_Wmissing_braces,
9491 "missing braces around initializer");
9494 /* Warn when some struct elements are implicitly initialized to zero. */
9495 if (warn_missing_field_initializers
9496 && constructor_type
9497 && TREE_CODE (constructor_type) == RECORD_TYPE
9498 && constructor_unfilled_fields)
9500 /* Do not warn for flexible array members or zero-length arrays. */
9501 while (constructor_unfilled_fields
9502 && (!DECL_SIZE (constructor_unfilled_fields)
9503 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
9504 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
9506 if (constructor_unfilled_fields
9507 /* Do not warn if this level of the initializer uses member
9508 designators; it is likely to be deliberate. */
9509 && !constructor_designated
9510 /* Do not warn about initializing with { 0 } or with { }. */
9511 && !constructor_zeroinit)
9513 if (warning_at (input_location, OPT_Wmissing_field_initializers,
9514 "missing initializer for field %qD of %qT",
9515 constructor_unfilled_fields,
9516 constructor_type))
9517 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
9518 "%qD declared here", constructor_unfilled_fields);
9522 /* Pad out the end of the structure. */
9523 if (p->replacement_value.value)
9524 /* If this closes a superfluous brace pair,
9525 just pass out the element between them. */
9526 ret = p->replacement_value;
9527 else if (constructor_type == NULL_TREE)
9529 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
9530 && TREE_CODE (constructor_type) != ARRAY_TYPE
9531 && !gnu_vector_type_p (constructor_type))
9533 /* A nonincremental scalar initializer--just return
9534 the element, after verifying there is just one.
9535 Empty scalar initializers are supported in C23. */
9536 if (vec_safe_is_empty (constructor_elements))
9538 if (constructor_erroneous || constructor_type == error_mark_node)
9539 ret.value = error_mark_node;
9540 else if (TREE_CODE (constructor_type) == FUNCTION_TYPE)
9542 error_init (loc, "invalid initializer");
9543 ret.value = error_mark_node;
9545 else if (TREE_CODE (constructor_type) == POINTER_TYPE)
9546 /* Ensure this is a null pointer constant in the case of a
9547 'constexpr' object initialized with {}. */
9548 ret.value = build_zero_cst (ptr_type_node);
9549 else
9550 ret.value = build_zero_cst (constructor_type);
9552 else if (vec_safe_length (constructor_elements) != 1)
9554 error_init (loc, "extra elements in scalar initializer");
9555 ret.value = (*constructor_elements)[0].value;
9557 else
9558 ret.value = (*constructor_elements)[0].value;
9560 else
9562 if (constructor_erroneous)
9563 ret.value = error_mark_node;
9564 else
9566 ret.value = build_constructor (constructor_type,
9567 constructor_elements);
9568 if (constructor_constant)
9569 TREE_CONSTANT (ret.value) = 1;
9570 if (constructor_constant && constructor_simple)
9571 TREE_STATIC (ret.value) = 1;
9572 if (constructor_nonconst)
9573 CONSTRUCTOR_NON_CONST (ret.value) = 1;
9577 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
9579 if (constructor_nonconst)
9580 ret.original_code = C_MAYBE_CONST_EXPR;
9581 else if (ret.original_code == C_MAYBE_CONST_EXPR)
9582 ret.original_code = ERROR_MARK;
9585 constructor_type = p->type;
9586 constructor_fields = p->fields;
9587 constructor_index = p->index;
9588 constructor_max_index = p->max_index;
9589 constructor_unfilled_index = p->unfilled_index;
9590 constructor_unfilled_fields = p->unfilled_fields;
9591 constructor_bit_index = p->bit_index;
9592 constructor_elements = p->elements;
9593 constructor_constant = p->constant;
9594 constructor_simple = p->simple;
9595 constructor_nonconst = p->nonconst;
9596 constructor_erroneous = p->erroneous;
9597 constructor_incremental = p->incremental;
9598 constructor_designated = p->designated;
9599 designator_depth = p->designator_depth;
9600 constructor_pending_elts = p->pending_elts;
9601 constructor_depth = p->depth;
9602 if (!p->implicit)
9603 constructor_range_stack = p->range_stack;
9604 RESTORE_SPELLING_DEPTH (constructor_depth);
9606 constructor_stack = p->next;
9607 XDELETE (p);
9609 if (ret.value == NULL_TREE && constructor_stack == 0)
9610 ret.value = error_mark_node;
9611 return ret;
9614 /* Common handling for both array range and field name designators.
9615 ARRAY argument is nonzero for array ranges. Returns false for success. */
9617 static bool
9618 set_designator (location_t loc, bool array,
9619 struct obstack *braced_init_obstack)
9621 tree subtype;
9622 enum tree_code subcode;
9624 /* Don't die if an entire brace-pair level is superfluous
9625 in the containing level, or for an erroneous type. */
9626 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9627 return true;
9629 /* If there were errors in this designator list already, bail out
9630 silently. */
9631 if (designator_erroneous)
9632 return true;
9634 /* Likewise for an initializer for a variable-size type. Those are
9635 diagnosed in the parser, except for empty initializer braces. */
9636 if (COMPLETE_TYPE_P (constructor_type)
9637 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9638 return true;
9640 if (!designator_depth)
9642 gcc_assert (!constructor_range_stack);
9644 /* Designator list starts at the level of closest explicit
9645 braces. */
9646 while (constructor_stack->implicit)
9647 process_init_element (input_location,
9648 pop_init_level (loc, 1, braced_init_obstack,
9649 last_init_list_comma),
9650 true, braced_init_obstack);
9651 constructor_designated = 1;
9652 return false;
9655 switch (TREE_CODE (constructor_type))
9657 case RECORD_TYPE:
9658 case UNION_TYPE:
9659 subtype = TREE_TYPE (constructor_fields);
9660 if (subtype != error_mark_node)
9661 subtype = TYPE_MAIN_VARIANT (subtype);
9662 break;
9663 case ARRAY_TYPE:
9664 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9665 break;
9666 default:
9667 gcc_unreachable ();
9670 subcode = TREE_CODE (subtype);
9671 if (array && subcode != ARRAY_TYPE)
9673 error_init (loc, "array index in non-array initializer");
9674 return true;
9676 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9678 error_init (loc, "field name not in record or union initializer");
9679 return true;
9682 constructor_designated = 1;
9683 finish_implicit_inits (loc, braced_init_obstack);
9684 push_init_level (loc, 2, braced_init_obstack);
9685 return false;
9688 /* If there are range designators in designator list, push a new designator
9689 to constructor_range_stack. RANGE_END is end of such stack range or
9690 NULL_TREE if there is no range designator at this level. */
9692 static void
9693 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9695 struct constructor_range_stack *p;
9697 p = (struct constructor_range_stack *)
9698 obstack_alloc (braced_init_obstack,
9699 sizeof (struct constructor_range_stack));
9700 p->prev = constructor_range_stack;
9701 p->next = 0;
9702 p->fields = constructor_fields;
9703 p->range_start = constructor_index;
9704 p->index = constructor_index;
9705 p->stack = constructor_stack;
9706 p->range_end = range_end;
9707 if (constructor_range_stack)
9708 constructor_range_stack->next = p;
9709 constructor_range_stack = p;
9712 /* Within an array initializer, specify the next index to be initialized.
9713 FIRST is that index. If LAST is nonzero, then initialize a range
9714 of indices, running from FIRST through LAST. */
9716 void
9717 set_init_index (location_t loc, tree first, tree last,
9718 struct obstack *braced_init_obstack)
9720 if (set_designator (loc, true, braced_init_obstack))
9721 return;
9723 designator_erroneous = 1;
9725 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9726 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9728 error_init (loc, "array index in initializer not of integer type");
9729 return;
9732 if (TREE_CODE (first) != INTEGER_CST)
9734 first = c_fully_fold (first, false, NULL);
9735 if (TREE_CODE (first) == INTEGER_CST)
9736 pedwarn_init (loc, OPT_Wpedantic,
9737 "array index in initializer is not "
9738 "an integer constant expression");
9741 if (last && TREE_CODE (last) != INTEGER_CST)
9743 last = c_fully_fold (last, false, NULL);
9744 if (TREE_CODE (last) == INTEGER_CST)
9745 pedwarn_init (loc, OPT_Wpedantic,
9746 "array index in initializer is not "
9747 "an integer constant expression");
9750 if (TREE_CODE (first) != INTEGER_CST)
9751 error_init (loc, "nonconstant array index in initializer");
9752 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9753 error_init (loc, "nonconstant array index in initializer");
9754 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9755 error_init (loc, "array index in non-array initializer");
9756 else if (tree_int_cst_sgn (first) == -1)
9757 error_init (loc, "array index in initializer exceeds array bounds");
9758 else if (constructor_max_index
9759 && tree_int_cst_lt (constructor_max_index, first))
9760 error_init (loc, "array index in initializer exceeds array bounds");
9761 else
9763 constant_expression_warning (first);
9764 if (last)
9765 constant_expression_warning (last);
9766 constructor_index = convert (bitsizetype, first);
9767 if (tree_int_cst_lt (constructor_index, first))
9769 constructor_index = copy_node (constructor_index);
9770 TREE_OVERFLOW (constructor_index) = 1;
9773 if (last)
9775 if (tree_int_cst_equal (first, last))
9776 last = NULL_TREE;
9777 else if (tree_int_cst_lt (last, first))
9779 error_init (loc, "empty index range in initializer");
9780 last = NULL_TREE;
9782 else
9784 last = convert (bitsizetype, last);
9785 if (constructor_max_index != NULL_TREE
9786 && tree_int_cst_lt (constructor_max_index, last))
9788 error_init (loc, "array index range in initializer exceeds "
9789 "array bounds");
9790 last = NULL_TREE;
9795 designator_depth++;
9796 designator_erroneous = 0;
9797 if (constructor_range_stack || last)
9798 push_range_stack (last, braced_init_obstack);
9802 /* Within a struct initializer, specify the next field to be initialized. */
9804 void
9805 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9806 struct obstack *braced_init_obstack)
9808 tree field;
9810 if (set_designator (loc, false, braced_init_obstack))
9811 return;
9813 designator_erroneous = 1;
9815 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9817 error_init (loc, "field name not in record or union initializer");
9818 return;
9821 field = lookup_field (constructor_type, fieldname);
9823 if (field == NULL_TREE)
9825 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9826 if (guessed_id)
9828 gcc_rich_location rich_loc (fieldname_loc);
9829 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9830 error_at (&rich_loc,
9831 "%qT has no member named %qE; did you mean %qE?",
9832 constructor_type, fieldname, guessed_id);
9834 else
9835 error_at (fieldname_loc, "%qT has no member named %qE",
9836 constructor_type, fieldname);
9838 else
9841 constructor_fields = TREE_VALUE (field);
9842 designator_depth++;
9843 designator_erroneous = 0;
9844 if (constructor_range_stack)
9845 push_range_stack (NULL_TREE, braced_init_obstack);
9846 field = TREE_CHAIN (field);
9847 if (field)
9849 if (set_designator (loc, false, braced_init_obstack))
9850 return;
9853 while (field != NULL_TREE);
9856 /* Add a new initializer to the tree of pending initializers. PURPOSE
9857 identifies the initializer, either array index or field in a structure.
9858 VALUE is the value of that index or field. If ORIGTYPE is not
9859 NULL_TREE, it is the original type of VALUE.
9861 IMPLICIT is true if value comes from pop_init_level (1),
9862 the new initializer has been merged with the existing one
9863 and thus no warnings should be emitted about overriding an
9864 existing initializer. */
9866 static void
9867 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9868 bool implicit, struct obstack *braced_init_obstack)
9870 struct init_node *p, **q, *r;
9872 q = &constructor_pending_elts;
9873 p = 0;
9875 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9877 while (*q != 0)
9879 p = *q;
9880 if (tree_int_cst_lt (purpose, p->purpose))
9881 q = &p->left;
9882 else if (tree_int_cst_lt (p->purpose, purpose))
9883 q = &p->right;
9884 else
9886 if (!implicit)
9888 if (TREE_SIDE_EFFECTS (p->value))
9889 warning_init (loc, OPT_Woverride_init_side_effects,
9890 "initialized field with side-effects "
9891 "overwritten");
9892 else if (warn_override_init)
9893 warning_init (loc, OPT_Woverride_init,
9894 "initialized field overwritten");
9896 p->value = value;
9897 p->origtype = origtype;
9898 return;
9902 else
9904 tree bitpos;
9906 bitpos = bit_position (purpose);
9907 while (*q != NULL)
9909 p = *q;
9910 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9911 q = &p->left;
9912 else if (p->purpose != purpose)
9913 q = &p->right;
9914 else
9916 if (!implicit)
9918 if (TREE_SIDE_EFFECTS (p->value))
9919 warning_init (loc, OPT_Woverride_init_side_effects,
9920 "initialized field with side-effects "
9921 "overwritten");
9922 else if (warn_override_init)
9923 warning_init (loc, OPT_Woverride_init,
9924 "initialized field overwritten");
9926 p->value = value;
9927 p->origtype = origtype;
9928 return;
9933 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9934 sizeof (struct init_node));
9935 r->purpose = purpose;
9936 r->value = value;
9937 r->origtype = origtype;
9939 *q = r;
9940 r->parent = p;
9941 r->left = 0;
9942 r->right = 0;
9943 r->balance = 0;
9945 while (p)
9947 struct init_node *s;
9949 if (r == p->left)
9951 if (p->balance == 0)
9952 p->balance = -1;
9953 else if (p->balance < 0)
9955 if (r->balance < 0)
9957 /* L rotation. */
9958 p->left = r->right;
9959 if (p->left)
9960 p->left->parent = p;
9961 r->right = p;
9963 p->balance = 0;
9964 r->balance = 0;
9966 s = p->parent;
9967 p->parent = r;
9968 r->parent = s;
9969 if (s)
9971 if (s->left == p)
9972 s->left = r;
9973 else
9974 s->right = r;
9976 else
9977 constructor_pending_elts = r;
9979 else
9981 /* LR rotation. */
9982 struct init_node *t = r->right;
9984 r->right = t->left;
9985 if (r->right)
9986 r->right->parent = r;
9987 t->left = r;
9989 p->left = t->right;
9990 if (p->left)
9991 p->left->parent = p;
9992 t->right = p;
9994 p->balance = t->balance < 0;
9995 r->balance = -(t->balance > 0);
9996 t->balance = 0;
9998 s = p->parent;
9999 p->parent = t;
10000 r->parent = t;
10001 t->parent = s;
10002 if (s)
10004 if (s->left == p)
10005 s->left = t;
10006 else
10007 s->right = t;
10009 else
10010 constructor_pending_elts = t;
10012 break;
10014 else
10016 /* p->balance == +1; growth of left side balances the node. */
10017 p->balance = 0;
10018 break;
10021 else /* r == p->right */
10023 if (p->balance == 0)
10024 /* Growth propagation from right side. */
10025 p->balance++;
10026 else if (p->balance > 0)
10028 if (r->balance > 0)
10030 /* R rotation. */
10031 p->right = r->left;
10032 if (p->right)
10033 p->right->parent = p;
10034 r->left = p;
10036 p->balance = 0;
10037 r->balance = 0;
10039 s = p->parent;
10040 p->parent = r;
10041 r->parent = s;
10042 if (s)
10044 if (s->left == p)
10045 s->left = r;
10046 else
10047 s->right = r;
10049 else
10050 constructor_pending_elts = r;
10052 else /* r->balance == -1 */
10054 /* RL rotation */
10055 struct init_node *t = r->left;
10057 r->left = t->right;
10058 if (r->left)
10059 r->left->parent = r;
10060 t->right = r;
10062 p->right = t->left;
10063 if (p->right)
10064 p->right->parent = p;
10065 t->left = p;
10067 r->balance = (t->balance < 0);
10068 p->balance = -(t->balance > 0);
10069 t->balance = 0;
10071 s = p->parent;
10072 p->parent = t;
10073 r->parent = t;
10074 t->parent = s;
10075 if (s)
10077 if (s->left == p)
10078 s->left = t;
10079 else
10080 s->right = t;
10082 else
10083 constructor_pending_elts = t;
10085 break;
10087 else
10089 /* p->balance == -1; growth of right side balances the node. */
10090 p->balance = 0;
10091 break;
10095 r = p;
10096 p = p->parent;
10100 /* Build AVL tree from a sorted chain. */
10102 static void
10103 set_nonincremental_init (struct obstack * braced_init_obstack)
10105 unsigned HOST_WIDE_INT ix;
10106 tree index, value;
10108 if (TREE_CODE (constructor_type) != RECORD_TYPE
10109 && TREE_CODE (constructor_type) != ARRAY_TYPE)
10110 return;
10112 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
10113 add_pending_init (input_location, index, value, NULL_TREE, true,
10114 braced_init_obstack);
10115 constructor_elements = NULL;
10116 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10118 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
10119 /* Skip any nameless bit fields at the beginning. */
10120 while (constructor_unfilled_fields != NULL_TREE
10121 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
10122 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
10125 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10127 if (TYPE_DOMAIN (constructor_type))
10128 constructor_unfilled_index
10129 = convert (bitsizetype,
10130 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10131 else
10132 constructor_unfilled_index = bitsize_zero_node;
10134 constructor_incremental = 0;
10137 /* Build AVL tree from a string constant. */
10139 static void
10140 set_nonincremental_init_from_string (tree str,
10141 struct obstack * braced_init_obstack)
10143 tree value, purpose, type;
10144 HOST_WIDE_INT val[2];
10145 const char *p, *end;
10146 int byte, wchar_bytes, charwidth, bitpos;
10148 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
10150 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
10151 charwidth = TYPE_PRECISION (char_type_node);
10152 gcc_assert ((size_t) wchar_bytes * charwidth
10153 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
10154 type = TREE_TYPE (constructor_type);
10155 p = TREE_STRING_POINTER (str);
10156 end = p + TREE_STRING_LENGTH (str);
10158 for (purpose = bitsize_zero_node;
10159 p < end
10160 && !(constructor_max_index
10161 && tree_int_cst_lt (constructor_max_index, purpose));
10162 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
10164 if (wchar_bytes == 1)
10166 val[0] = (unsigned char) *p++;
10167 val[1] = 0;
10169 else
10171 val[1] = 0;
10172 val[0] = 0;
10173 for (byte = 0; byte < wchar_bytes; byte++)
10175 if (BYTES_BIG_ENDIAN)
10176 bitpos = (wchar_bytes - byte - 1) * charwidth;
10177 else
10178 bitpos = byte * charwidth;
10179 val[bitpos / HOST_BITS_PER_WIDE_INT]
10180 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
10181 << (bitpos % HOST_BITS_PER_WIDE_INT);
10185 if (!TYPE_UNSIGNED (type))
10187 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
10188 if (bitpos < HOST_BITS_PER_WIDE_INT)
10190 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
10192 val[0] |= HOST_WIDE_INT_M1U << bitpos;
10193 val[1] = -1;
10196 else if (bitpos == HOST_BITS_PER_WIDE_INT)
10198 if (val[0] < 0)
10199 val[1] = -1;
10201 else if (val[1] & (HOST_WIDE_INT_1
10202 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
10203 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
10206 value = wide_int_to_tree (type,
10207 wide_int::from_array (val, 2,
10208 HOST_BITS_PER_WIDE_INT * 2));
10209 add_pending_init (input_location, purpose, value, NULL_TREE, true,
10210 braced_init_obstack);
10213 constructor_incremental = 0;
10216 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
10217 not initialized yet. */
10219 static tree
10220 find_init_member (tree field, struct obstack * braced_init_obstack)
10222 struct init_node *p;
10224 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10226 if (constructor_incremental
10227 && tree_int_cst_lt (field, constructor_unfilled_index))
10228 set_nonincremental_init (braced_init_obstack);
10230 p = constructor_pending_elts;
10231 while (p)
10233 if (tree_int_cst_lt (field, p->purpose))
10234 p = p->left;
10235 else if (tree_int_cst_lt (p->purpose, field))
10236 p = p->right;
10237 else
10238 return p->value;
10241 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
10243 tree bitpos = bit_position (field);
10245 if (constructor_incremental
10246 && (!constructor_unfilled_fields
10247 || tree_int_cst_lt (bitpos,
10248 bit_position (constructor_unfilled_fields))))
10249 set_nonincremental_init (braced_init_obstack);
10251 p = constructor_pending_elts;
10252 while (p)
10254 if (field == p->purpose)
10255 return p->value;
10256 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
10257 p = p->left;
10258 else
10259 p = p->right;
10262 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10264 if (!vec_safe_is_empty (constructor_elements)
10265 && (constructor_elements->last ().index == field))
10266 return constructor_elements->last ().value;
10268 return NULL_TREE;
10271 /* "Output" the next constructor element.
10272 At top level, really output it to assembler code now.
10273 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
10274 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
10275 TYPE is the data type that the containing data type wants here.
10276 FIELD is the field (a FIELD_DECL) or the index that this element fills.
10277 If VALUE is a string constant, STRICT_STRING is true if it is
10278 unparenthesized or we should not warn here for it being parenthesized.
10279 For other types of VALUE, STRICT_STRING is not used.
10281 PENDING if true means output pending elements that belong
10282 right after this element. (PENDING is normally true;
10283 it is false while outputting pending elements, to avoid recursion.)
10285 IMPLICIT is true if value comes from pop_init_level (1),
10286 the new initializer has been merged with the existing one
10287 and thus no warnings should be emitted about overriding an
10288 existing initializer. */
10290 static void
10291 output_init_element (location_t loc, tree value, tree origtype,
10292 bool strict_string, tree type, tree field, bool pending,
10293 bool implicit, struct obstack * braced_init_obstack)
10295 tree semantic_type = NULL_TREE;
10296 bool maybe_const = true;
10297 bool npc, int_const_expr, arith_const_expr;
10299 if (type == error_mark_node || value == error_mark_node)
10301 constructor_erroneous = 1;
10302 return;
10304 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
10305 && (TREE_CODE (value) == STRING_CST
10306 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
10307 && !(TREE_CODE (value) == STRING_CST
10308 && TREE_CODE (type) == ARRAY_TYPE
10309 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
10310 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
10311 TYPE_MAIN_VARIANT (type)))
10312 value = array_to_pointer_conversion (input_location, value);
10314 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
10315 && require_constant_value && pending)
10317 /* As an extension, allow initializing objects with static storage
10318 duration with compound literals (which are then treated just as
10319 the brace enclosed list they contain). */
10320 if (flag_isoc99)
10321 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
10322 "constant");
10323 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
10324 value = DECL_INITIAL (decl);
10327 npc = null_pointer_constant_p (value);
10328 int_const_expr = (TREE_CODE (value) == INTEGER_CST
10329 && !TREE_OVERFLOW (value)
10330 && INTEGRAL_TYPE_P (TREE_TYPE (value)));
10331 /* Not fully determined before folding. */
10332 arith_const_expr = true;
10333 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
10335 semantic_type = TREE_TYPE (value);
10336 value = TREE_OPERAND (value, 0);
10338 value = c_fully_fold (value, require_constant_value, &maybe_const);
10339 /* TODO: this may not detect all cases of expressions folding to
10340 constants that are not arithmetic constant expressions. */
10341 if (!maybe_const)
10342 arith_const_expr = false;
10343 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
10344 && TREE_CODE (TREE_TYPE (value)) != REAL_TYPE
10345 && TREE_CODE (TREE_TYPE (value)) != COMPLEX_TYPE)
10346 arith_const_expr = false;
10347 else if (TREE_CODE (value) != INTEGER_CST
10348 && TREE_CODE (value) != REAL_CST
10349 && TREE_CODE (value) != COMPLEX_CST)
10350 arith_const_expr = false;
10351 else if (TREE_OVERFLOW (value))
10352 arith_const_expr = false;
10354 if (value == error_mark_node)
10355 constructor_erroneous = 1;
10356 else if (!TREE_CONSTANT (value))
10357 constructor_constant = 0;
10358 else if (!initializer_constant_valid_p (value,
10359 TREE_TYPE (value),
10360 AGGREGATE_TYPE_P (constructor_type)
10361 && TYPE_REVERSE_STORAGE_ORDER
10362 (constructor_type))
10363 || (RECORD_OR_UNION_TYPE_P (constructor_type)
10364 && DECL_C_BIT_FIELD (field)
10365 && TREE_CODE (value) != INTEGER_CST))
10366 constructor_simple = 0;
10367 if (!maybe_const)
10368 constructor_nonconst = 1;
10370 /* Digest the initializer and issue any errors about incompatible
10371 types before issuing errors about non-constant initializers. */
10372 tree new_value = value;
10373 if (semantic_type)
10374 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
10375 /* In the case of braces around a scalar initializer, the result of
10376 this initializer processing goes through digest_init again at the
10377 outer level. In the case of a constexpr initializer for a
10378 pointer, avoid converting a null pointer constant to something
10379 that is not a null pointer constant to avoid a spurious error
10380 from that second processing. */
10381 if (!require_constexpr_value
10382 || !npc
10383 || TREE_CODE (constructor_type) != POINTER_TYPE)
10384 new_value = digest_init (loc, type, new_value, origtype, npc,
10385 int_const_expr, arith_const_expr, strict_string,
10386 require_constant_value, require_constexpr_value);
10387 if (new_value == error_mark_node)
10389 constructor_erroneous = 1;
10390 return;
10392 if (require_constant_value || require_constant_elements)
10393 constant_expression_warning (new_value);
10395 /* Proceed to check the constness of the original initializer. */
10396 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
10398 if (require_constant_value)
10400 error_init (loc, "initializer element is not constant");
10401 value = error_mark_node;
10403 else if (require_constant_elements)
10404 pedwarn (loc, OPT_Wpedantic,
10405 "initializer element is not computable at load time");
10407 else if (!maybe_const
10408 && (require_constant_value || require_constant_elements))
10409 pedwarn_init (loc, OPT_Wpedantic,
10410 "initializer element is not a constant expression");
10411 /* digest_init has already carried out the additional checks
10412 required for 'constexpr' initializers (using the information
10413 passed to it about whether the original initializer was certain
10414 kinds of constant expression), so that check does not need to be
10415 repeated here. */
10417 /* Issue -Wc++-compat warnings about initializing a bitfield with
10418 enum type. */
10419 if (warn_cxx_compat
10420 && field != NULL_TREE
10421 && TREE_CODE (field) == FIELD_DECL
10422 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
10423 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
10424 != TYPE_MAIN_VARIANT (type))
10425 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
10427 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
10428 if (checktype != error_mark_node
10429 && (TYPE_MAIN_VARIANT (checktype)
10430 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
10431 warning_init (loc, OPT_Wc___compat,
10432 "enum conversion in initialization is invalid in C++");
10435 /* If this field is empty and does not have side effects (and is not at
10436 the end of structure), don't do anything other than checking the
10437 initializer. */
10438 if (field
10439 && (TREE_TYPE (field) == error_mark_node
10440 || (COMPLETE_TYPE_P (TREE_TYPE (field))
10441 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
10442 && !TREE_SIDE_EFFECTS (new_value)
10443 && (TREE_CODE (constructor_type) == ARRAY_TYPE
10444 || DECL_CHAIN (field)))))
10445 return;
10447 /* Finally, set VALUE to the initializer value digested above. */
10448 value = new_value;
10450 /* If this element doesn't come next in sequence,
10451 put it on constructor_pending_elts. */
10452 if (TREE_CODE (constructor_type) == ARRAY_TYPE
10453 && (!constructor_incremental
10454 || !tree_int_cst_equal (field, constructor_unfilled_index)))
10456 if (constructor_incremental
10457 && tree_int_cst_lt (field, constructor_unfilled_index))
10458 set_nonincremental_init (braced_init_obstack);
10460 add_pending_init (loc, field, value, origtype, implicit,
10461 braced_init_obstack);
10462 return;
10464 else if (TREE_CODE (constructor_type) == RECORD_TYPE
10465 && (!constructor_incremental
10466 || field != constructor_unfilled_fields))
10468 /* We do this for records but not for unions. In a union,
10469 no matter which field is specified, it can be initialized
10470 right away since it starts at the beginning of the union. */
10471 if (constructor_incremental)
10473 if (!constructor_unfilled_fields)
10474 set_nonincremental_init (braced_init_obstack);
10475 else
10477 tree bitpos, unfillpos;
10479 bitpos = bit_position (field);
10480 unfillpos = bit_position (constructor_unfilled_fields);
10482 if (tree_int_cst_lt (bitpos, unfillpos))
10483 set_nonincremental_init (braced_init_obstack);
10487 add_pending_init (loc, field, value, origtype, implicit,
10488 braced_init_obstack);
10489 return;
10491 else if (TREE_CODE (constructor_type) == UNION_TYPE
10492 && !vec_safe_is_empty (constructor_elements))
10494 if (!implicit)
10496 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
10497 warning_init (loc, OPT_Woverride_init_side_effects,
10498 "initialized field with side-effects overwritten");
10499 else if (warn_override_init)
10500 warning_init (loc, OPT_Woverride_init,
10501 "initialized field overwritten");
10504 /* We can have just one union field set. */
10505 constructor_elements = NULL;
10508 /* Otherwise, output this element either to
10509 constructor_elements or to the assembler file. */
10511 constructor_elt celt = {field, value};
10512 vec_safe_push (constructor_elements, celt);
10514 /* Advance the variable that indicates sequential elements output. */
10515 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10516 constructor_unfilled_index
10517 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
10518 bitsize_one_node);
10519 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
10521 constructor_unfilled_fields
10522 = DECL_CHAIN (constructor_unfilled_fields);
10524 /* Skip any nameless bit fields. */
10525 while (constructor_unfilled_fields != NULL_TREE
10526 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
10527 constructor_unfilled_fields =
10528 DECL_CHAIN (constructor_unfilled_fields);
10530 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10531 constructor_unfilled_fields = NULL_TREE;
10533 /* Now output any pending elements which have become next. */
10534 if (pending)
10535 output_pending_init_elements (0, braced_init_obstack);
10538 /* For two FIELD_DECLs in the same chain, return -1 if field1
10539 comes before field2, 1 if field1 comes after field2 and
10540 0 if field1 == field2. */
10542 static int
10543 init_field_decl_cmp (tree field1, tree field2)
10545 if (field1 == field2)
10546 return 0;
10548 tree bitpos1 = bit_position (field1);
10549 tree bitpos2 = bit_position (field2);
10550 if (tree_int_cst_equal (bitpos1, bitpos2))
10552 /* If one of the fields has non-zero bitsize, then that
10553 field must be the last one in a sequence of zero
10554 sized fields, fields after it will have bigger
10555 bit_position. */
10556 if (TREE_TYPE (field1) != error_mark_node
10557 && COMPLETE_TYPE_P (TREE_TYPE (field1))
10558 && integer_nonzerop (TREE_TYPE (field1)))
10559 return 1;
10560 if (TREE_TYPE (field2) != error_mark_node
10561 && COMPLETE_TYPE_P (TREE_TYPE (field2))
10562 && integer_nonzerop (TREE_TYPE (field2)))
10563 return -1;
10564 /* Otherwise, fallback to DECL_CHAIN walk to find out
10565 which field comes earlier. Walk chains of both
10566 fields, so that if field1 and field2 are close to each
10567 other in either order, it is found soon even for large
10568 sequences of zero sized fields. */
10569 tree f1 = field1, f2 = field2;
10570 while (1)
10572 f1 = DECL_CHAIN (f1);
10573 f2 = DECL_CHAIN (f2);
10574 if (f1 == NULL_TREE)
10576 gcc_assert (f2);
10577 return 1;
10579 if (f2 == NULL_TREE)
10580 return -1;
10581 if (f1 == field2)
10582 return -1;
10583 if (f2 == field1)
10584 return 1;
10585 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
10586 return 1;
10587 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
10588 return -1;
10591 else if (tree_int_cst_lt (bitpos1, bitpos2))
10592 return -1;
10593 else
10594 return 1;
10597 /* Output any pending elements which have become next.
10598 As we output elements, constructor_unfilled_{fields,index}
10599 advances, which may cause other elements to become next;
10600 if so, they too are output.
10602 If ALL is 0, we return when there are
10603 no more pending elements to output now.
10605 If ALL is 1, we output space as necessary so that
10606 we can output all the pending elements. */
10607 static void
10608 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
10610 struct init_node *elt = constructor_pending_elts;
10611 tree next;
10613 retry:
10615 /* Look through the whole pending tree.
10616 If we find an element that should be output now,
10617 output it. Otherwise, set NEXT to the element
10618 that comes first among those still pending. */
10620 next = NULL_TREE;
10621 while (elt)
10623 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10625 if (tree_int_cst_equal (elt->purpose,
10626 constructor_unfilled_index))
10627 output_init_element (input_location, elt->value, elt->origtype,
10628 true, TREE_TYPE (constructor_type),
10629 constructor_unfilled_index, false, false,
10630 braced_init_obstack);
10631 else if (tree_int_cst_lt (constructor_unfilled_index,
10632 elt->purpose))
10634 /* Advance to the next smaller node. */
10635 if (elt->left)
10636 elt = elt->left;
10637 else
10639 /* We have reached the smallest node bigger than the
10640 current unfilled index. Fill the space first. */
10641 next = elt->purpose;
10642 break;
10645 else
10647 /* Advance to the next bigger node. */
10648 if (elt->right)
10649 elt = elt->right;
10650 else
10652 /* We have reached the biggest node in a subtree. Find
10653 the parent of it, which is the next bigger node. */
10654 while (elt->parent && elt->parent->right == elt)
10655 elt = elt->parent;
10656 elt = elt->parent;
10657 if (elt && tree_int_cst_lt (constructor_unfilled_index,
10658 elt->purpose))
10660 next = elt->purpose;
10661 break;
10666 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10668 /* If the current record is complete we are done. */
10669 if (constructor_unfilled_fields == NULL_TREE)
10670 break;
10672 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10673 elt->purpose);
10674 if (cmp == 0)
10675 output_init_element (input_location, elt->value, elt->origtype,
10676 true, TREE_TYPE (elt->purpose),
10677 elt->purpose, false, false,
10678 braced_init_obstack);
10679 else if (cmp < 0)
10681 /* Advance to the next smaller node. */
10682 if (elt->left)
10683 elt = elt->left;
10684 else
10686 /* We have reached the smallest node bigger than the
10687 current unfilled field. Fill the space first. */
10688 next = elt->purpose;
10689 break;
10692 else
10694 /* Advance to the next bigger node. */
10695 if (elt->right)
10696 elt = elt->right;
10697 else
10699 /* We have reached the biggest node in a subtree. Find
10700 the parent of it, which is the next bigger node. */
10701 while (elt->parent && elt->parent->right == elt)
10702 elt = elt->parent;
10703 elt = elt->parent;
10704 if (elt
10705 && init_field_decl_cmp (constructor_unfilled_fields,
10706 elt->purpose) < 0)
10708 next = elt->purpose;
10709 break;
10716 /* Ordinarily return, but not if we want to output all
10717 and there are elements left. */
10718 if (!(all && next != NULL_TREE))
10719 return;
10721 /* If it's not incremental, just skip over the gap, so that after
10722 jumping to retry we will output the next successive element. */
10723 if (RECORD_OR_UNION_TYPE_P (constructor_type))
10724 constructor_unfilled_fields = next;
10725 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10726 constructor_unfilled_index = next;
10728 /* ELT now points to the node in the pending tree with the next
10729 initializer to output. */
10730 goto retry;
10733 /* Expression VALUE coincides with the start of type TYPE in a braced
10734 initializer. Return true if we should treat VALUE as initializing
10735 the first element of TYPE, false if we should treat it as initializing
10736 TYPE as a whole.
10738 If the initializer is clearly invalid, the question becomes:
10739 which choice gives the best error message? */
10741 static bool
10742 initialize_elementwise_p (tree type, tree value)
10744 if (type == error_mark_node || value == error_mark_node)
10745 return false;
10747 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10749 tree value_type = TREE_TYPE (value);
10750 if (value_type == error_mark_node)
10751 return false;
10753 /* GNU vectors can be initialized elementwise. However, treat any
10754 kind of vector value as initializing the vector type as a whole,
10755 regardless of whether the value is a GNU vector. Such initializers
10756 are valid if and only if they would have been valid in a non-braced
10757 initializer like:
10759 TYPE foo = VALUE;
10761 so recursing into the vector type would be at best confusing or at
10762 worst wrong. For example, when -flax-vector-conversions is in effect,
10763 it's possible to initialize a V8HI from a V4SI, even though the vectors
10764 have different element types and different numbers of elements. */
10765 if (gnu_vector_type_p (type))
10766 return !VECTOR_TYPE_P (value_type);
10768 if (AGGREGATE_TYPE_P (type))
10769 return !comptypes (type, TYPE_MAIN_VARIANT (value_type));
10771 return false;
10774 /* Add one non-braced element to the current constructor level.
10775 This adjusts the current position within the constructor's type.
10776 This may also start or terminate implicit levels
10777 to handle a partly-braced initializer.
10779 Once this has found the correct level for the new element,
10780 it calls output_init_element.
10782 IMPLICIT is true if value comes from pop_init_level (1),
10783 the new initializer has been merged with the existing one
10784 and thus no warnings should be emitted about overriding an
10785 existing initializer. */
10787 void
10788 process_init_element (location_t loc, struct c_expr value, bool implicit,
10789 struct obstack * braced_init_obstack)
10791 tree orig_value = value.value;
10792 int string_flag
10793 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10794 bool strict_string = value.original_code == STRING_CST;
10795 bool was_designated = designator_depth != 0;
10797 designator_depth = 0;
10798 designator_erroneous = 0;
10800 if (!implicit && value.value && !integer_zerop (value.value))
10801 constructor_zeroinit = 0;
10803 /* Handle superfluous braces around string cst as in
10804 char x[] = {"foo"}; */
10805 if (constructor_type
10806 && !was_designated
10807 && TREE_CODE (constructor_type) == ARRAY_TYPE
10808 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10809 && integer_zerop (constructor_unfilled_index))
10811 if (constructor_stack->replacement_value.value)
10813 error_init (loc, "excess elements in %qT initializer", constructor_type);
10814 return;
10816 else if (string_flag)
10818 constructor_stack->replacement_value = value;
10819 return;
10823 if (constructor_stack->replacement_value.value != NULL_TREE)
10825 error_init (loc, "excess elements in struct initializer");
10826 return;
10829 /* Ignore elements of a brace group if it is entirely superfluous
10830 and has already been diagnosed, or if the type is erroneous. */
10831 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10832 return;
10834 /* Ignore elements of an initializer for a variable-size type.
10835 Those are diagnosed in the parser (empty initializer braces are OK). */
10836 if (COMPLETE_TYPE_P (constructor_type)
10837 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10838 return;
10840 if (!implicit && warn_designated_init && !was_designated
10841 && TREE_CODE (constructor_type) == RECORD_TYPE
10842 && lookup_attribute ("designated_init",
10843 TYPE_ATTRIBUTES (constructor_type)))
10844 warning_init (loc,
10845 OPT_Wdesignated_init,
10846 "positional initialization of field "
10847 "in %<struct%> declared with %<designated_init%> attribute");
10849 /* If we've exhausted any levels that didn't have braces,
10850 pop them now. */
10851 while (constructor_stack->implicit)
10853 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10854 && constructor_fields == NULL_TREE)
10855 process_init_element (loc,
10856 pop_init_level (loc, 1, braced_init_obstack,
10857 last_init_list_comma),
10858 true, braced_init_obstack);
10859 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10860 || gnu_vector_type_p (constructor_type))
10861 && constructor_max_index
10862 && tree_int_cst_lt (constructor_max_index,
10863 constructor_index))
10864 process_init_element (loc,
10865 pop_init_level (loc, 1, braced_init_obstack,
10866 last_init_list_comma),
10867 true, braced_init_obstack);
10868 else
10869 break;
10872 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10873 if (constructor_range_stack)
10875 /* If value is a compound literal and we'll be just using its
10876 content, don't put it into a SAVE_EXPR. */
10877 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10878 || !require_constant_value)
10880 tree semantic_type = NULL_TREE;
10881 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10883 semantic_type = TREE_TYPE (value.value);
10884 value.value = TREE_OPERAND (value.value, 0);
10886 value.value = save_expr (value.value);
10887 if (semantic_type)
10888 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10889 value.value);
10893 while (1)
10895 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10897 tree fieldtype;
10898 enum tree_code fieldcode;
10900 if (constructor_fields == NULL_TREE)
10902 pedwarn_init (loc, 0, "excess elements in struct initializer");
10903 break;
10906 fieldtype = TREE_TYPE (constructor_fields);
10907 if (fieldtype != error_mark_node)
10908 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10909 fieldcode = TREE_CODE (fieldtype);
10911 /* Error for non-static initialization of a flexible array member. */
10912 if (fieldcode == ARRAY_TYPE
10913 && !require_constant_value
10914 && TYPE_SIZE (fieldtype) == NULL_TREE
10915 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10917 error_init (loc, "non-static initialization of a flexible "
10918 "array member");
10919 break;
10922 /* Error for initialization of a flexible array member with
10923 a string constant if the structure is in an array. E.g.:
10924 struct S { int x; char y[]; };
10925 struct S s[] = { { 1, "foo" } };
10926 is invalid. */
10927 if (string_flag
10928 && fieldcode == ARRAY_TYPE
10929 && constructor_depth > 1
10930 && TYPE_SIZE (fieldtype) == NULL_TREE
10931 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10933 bool in_array_p = false;
10934 for (struct constructor_stack *p = constructor_stack;
10935 p && p->type; p = p->next)
10936 if (TREE_CODE (p->type) == ARRAY_TYPE)
10938 in_array_p = true;
10939 break;
10941 if (in_array_p)
10943 error_init (loc, "initialization of flexible array "
10944 "member in a nested context");
10945 break;
10949 /* Accept a string constant to initialize a subarray. */
10950 if (value.value != NULL_TREE
10951 && fieldcode == ARRAY_TYPE
10952 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10953 && string_flag)
10954 value.value = orig_value;
10955 /* Otherwise, if we have come to a subaggregate,
10956 and we don't have an element of its type, push into it. */
10957 else if (value.value != NULL_TREE
10958 && initialize_elementwise_p (fieldtype, value.value))
10960 push_init_level (loc, 1, braced_init_obstack);
10961 continue;
10964 if (value.value)
10966 push_member_name (constructor_fields);
10967 output_init_element (loc, value.value, value.original_type,
10968 strict_string, fieldtype,
10969 constructor_fields, true, implicit,
10970 braced_init_obstack);
10971 RESTORE_SPELLING_DEPTH (constructor_depth);
10973 else
10974 /* Do the bookkeeping for an element that was
10975 directly output as a constructor. */
10977 /* For a record, keep track of end position of last field. */
10978 if (DECL_SIZE (constructor_fields))
10979 constructor_bit_index
10980 = size_binop_loc (input_location, PLUS_EXPR,
10981 bit_position (constructor_fields),
10982 DECL_SIZE (constructor_fields));
10984 /* If the current field was the first one not yet written out,
10985 it isn't now, so update. */
10986 if (constructor_unfilled_fields == constructor_fields)
10988 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10989 /* Skip any nameless bit fields. */
10990 while (constructor_unfilled_fields != 0
10991 && (DECL_UNNAMED_BIT_FIELD
10992 (constructor_unfilled_fields)))
10993 constructor_unfilled_fields =
10994 DECL_CHAIN (constructor_unfilled_fields);
10998 constructor_fields = DECL_CHAIN (constructor_fields);
10999 /* Skip any nameless bit fields at the beginning. */
11000 while (constructor_fields != NULL_TREE
11001 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
11002 constructor_fields = DECL_CHAIN (constructor_fields);
11004 else if (TREE_CODE (constructor_type) == UNION_TYPE)
11006 tree fieldtype;
11007 enum tree_code fieldcode;
11009 if (constructor_fields == NULL_TREE)
11011 pedwarn_init (loc, 0,
11012 "excess elements in union initializer");
11013 break;
11016 fieldtype = TREE_TYPE (constructor_fields);
11017 if (fieldtype != error_mark_node)
11018 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
11019 fieldcode = TREE_CODE (fieldtype);
11021 /* Warn that traditional C rejects initialization of unions.
11022 We skip the warning if the value is zero. This is done
11023 under the assumption that the zero initializer in user
11024 code appears conditioned on e.g. __STDC__ to avoid
11025 "missing initializer" warnings and relies on default
11026 initialization to zero in the traditional C case.
11027 We also skip the warning if the initializer is designated,
11028 again on the assumption that this must be conditional on
11029 __STDC__ anyway (and we've already complained about the
11030 member-designator already). */
11031 if (!in_system_header_at (input_location) && !constructor_designated
11032 && !(value.value && (integer_zerop (value.value)
11033 || real_zerop (value.value))))
11034 warning (OPT_Wtraditional, "traditional C rejects initialization "
11035 "of unions");
11037 /* Accept a string constant to initialize a subarray. */
11038 if (value.value != NULL_TREE
11039 && fieldcode == ARRAY_TYPE
11040 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
11041 && string_flag)
11042 value.value = orig_value;
11043 /* Otherwise, if we have come to a subaggregate,
11044 and we don't have an element of its type, push into it. */
11045 else if (value.value != NULL_TREE
11046 && initialize_elementwise_p (fieldtype, value.value))
11048 push_init_level (loc, 1, braced_init_obstack);
11049 continue;
11052 if (value.value)
11054 push_member_name (constructor_fields);
11055 output_init_element (loc, value.value, value.original_type,
11056 strict_string, fieldtype,
11057 constructor_fields, true, implicit,
11058 braced_init_obstack);
11059 RESTORE_SPELLING_DEPTH (constructor_depth);
11061 else
11062 /* Do the bookkeeping for an element that was
11063 directly output as a constructor. */
11065 constructor_bit_index = DECL_SIZE (constructor_fields);
11066 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
11069 constructor_fields = NULL_TREE;
11071 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11073 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
11074 enum tree_code eltcode = TREE_CODE (elttype);
11076 /* Accept a string constant to initialize a subarray. */
11077 if (value.value != NULL_TREE
11078 && eltcode == ARRAY_TYPE
11079 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
11080 && string_flag)
11081 value.value = orig_value;
11082 /* Otherwise, if we have come to a subaggregate,
11083 and we don't have an element of its type, push into it. */
11084 else if (value.value != NULL_TREE
11085 && initialize_elementwise_p (elttype, value.value))
11087 push_init_level (loc, 1, braced_init_obstack);
11088 continue;
11091 if (constructor_max_index != NULL_TREE
11092 && (tree_int_cst_lt (constructor_max_index, constructor_index)
11093 || integer_all_onesp (constructor_max_index)))
11095 pedwarn_init (loc, 0,
11096 "excess elements in array initializer");
11097 break;
11100 /* Now output the actual element. */
11101 if (value.value)
11103 push_array_bounds (tree_to_uhwi (constructor_index));
11104 output_init_element (loc, value.value, value.original_type,
11105 strict_string, elttype,
11106 constructor_index, true, implicit,
11107 braced_init_obstack);
11108 RESTORE_SPELLING_DEPTH (constructor_depth);
11111 constructor_index
11112 = size_binop_loc (input_location, PLUS_EXPR,
11113 constructor_index, bitsize_one_node);
11115 if (!value.value)
11116 /* If we are doing the bookkeeping for an element that was
11117 directly output as a constructor, we must update
11118 constructor_unfilled_index. */
11119 constructor_unfilled_index = constructor_index;
11121 else if (gnu_vector_type_p (constructor_type))
11123 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
11125 /* Do a basic check of initializer size. Note that vectors
11126 always have a fixed size derived from their type. */
11127 if (tree_int_cst_lt (constructor_max_index, constructor_index))
11129 pedwarn_init (loc, 0,
11130 "excess elements in vector initializer");
11131 break;
11134 /* Now output the actual element. */
11135 if (value.value)
11137 if (TREE_CODE (value.value) == VECTOR_CST)
11138 elttype = TYPE_MAIN_VARIANT (constructor_type);
11139 output_init_element (loc, value.value, value.original_type,
11140 strict_string, elttype,
11141 constructor_index, true, implicit,
11142 braced_init_obstack);
11145 constructor_index
11146 = size_binop_loc (input_location,
11147 PLUS_EXPR, constructor_index, bitsize_one_node);
11149 if (!value.value)
11150 /* If we are doing the bookkeeping for an element that was
11151 directly output as a constructor, we must update
11152 constructor_unfilled_index. */
11153 constructor_unfilled_index = constructor_index;
11156 /* Handle the sole element allowed in a braced initializer
11157 for a scalar variable. */
11158 else if (constructor_type != error_mark_node
11159 && constructor_fields == NULL_TREE)
11161 pedwarn_init (loc, 0,
11162 "excess elements in scalar initializer");
11163 break;
11165 else
11167 if (value.value)
11168 output_init_element (loc, value.value, value.original_type,
11169 strict_string, constructor_type,
11170 NULL_TREE, true, implicit,
11171 braced_init_obstack);
11172 constructor_fields = NULL_TREE;
11175 /* Handle range initializers either at this level or anywhere higher
11176 in the designator stack. */
11177 if (constructor_range_stack)
11179 struct constructor_range_stack *p, *range_stack;
11180 int finish = 0;
11182 range_stack = constructor_range_stack;
11183 constructor_range_stack = 0;
11184 while (constructor_stack != range_stack->stack)
11186 gcc_assert (constructor_stack->implicit);
11187 process_init_element (loc,
11188 pop_init_level (loc, 1,
11189 braced_init_obstack,
11190 last_init_list_comma),
11191 true, braced_init_obstack);
11193 for (p = range_stack;
11194 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
11195 p = p->prev)
11197 gcc_assert (constructor_stack->implicit);
11198 process_init_element (loc,
11199 pop_init_level (loc, 1,
11200 braced_init_obstack,
11201 last_init_list_comma),
11202 true, braced_init_obstack);
11205 p->index = size_binop_loc (input_location,
11206 PLUS_EXPR, p->index, bitsize_one_node);
11207 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
11208 finish = 1;
11210 while (1)
11212 constructor_index = p->index;
11213 constructor_fields = p->fields;
11214 if (finish && p->range_end && p->index == p->range_start)
11216 finish = 0;
11217 p->prev = 0;
11219 p = p->next;
11220 if (!p)
11221 break;
11222 finish_implicit_inits (loc, braced_init_obstack);
11223 push_init_level (loc, 2, braced_init_obstack);
11224 p->stack = constructor_stack;
11225 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
11226 p->index = p->range_start;
11229 if (!finish)
11230 constructor_range_stack = range_stack;
11231 continue;
11234 break;
11237 constructor_range_stack = 0;
11240 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
11241 (guaranteed to be 'volatile' or null) and ARGS (represented using
11242 an ASM_EXPR node). */
11243 tree
11244 build_asm_stmt (bool is_volatile, tree args)
11246 if (is_volatile)
11247 ASM_VOLATILE_P (args) = 1;
11248 return add_stmt (args);
11251 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
11252 some INPUTS, and some CLOBBERS. The latter three may be NULL.
11253 SIMPLE indicates whether there was anything at all after the
11254 string in the asm expression -- asm("blah") and asm("blah" : )
11255 are subtly different. We use a ASM_EXPR node to represent this.
11256 LOC is the location of the asm, and IS_INLINE says whether this
11257 is asm inline. */
11258 tree
11259 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
11260 tree clobbers, tree labels, bool simple, bool is_inline)
11262 tree tail;
11263 tree args;
11264 int i;
11265 const char *constraint;
11266 const char **oconstraints;
11267 bool allows_mem, allows_reg, is_inout;
11268 int ninputs, noutputs;
11270 ninputs = list_length (inputs);
11271 noutputs = list_length (outputs);
11272 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
11274 string = resolve_asm_operand_names (string, outputs, inputs, labels);
11276 /* Remove output conversions that change the type but not the mode. */
11277 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
11279 tree output = TREE_VALUE (tail);
11281 output = c_fully_fold (output, false, NULL, true);
11283 /* ??? Really, this should not be here. Users should be using a
11284 proper lvalue, dammit. But there's a long history of using casts
11285 in the output operands. In cases like longlong.h, this becomes a
11286 primitive form of typechecking -- if the cast can be removed, then
11287 the output operand had a type of the proper width; otherwise we'll
11288 get an error. Gross, but ... */
11289 STRIP_NOPS (output);
11291 if (!lvalue_or_else (loc, output, lv_asm))
11292 output = error_mark_node;
11294 if (output != error_mark_node
11295 && (TREE_READONLY (output)
11296 || TYPE_READONLY (TREE_TYPE (output))
11297 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
11298 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
11299 readonly_error (loc, output, lv_asm);
11301 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
11302 oconstraints[i] = constraint;
11304 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
11305 &allows_mem, &allows_reg, &is_inout))
11307 /* If the operand is going to end up in memory,
11308 mark it addressable. */
11309 if (!allows_reg && !c_mark_addressable (output))
11310 output = error_mark_node;
11311 if (!(!allows_reg && allows_mem)
11312 && output != error_mark_node
11313 && VOID_TYPE_P (TREE_TYPE (output)))
11315 error_at (loc, "invalid use of void expression");
11316 output = error_mark_node;
11319 else
11320 output = error_mark_node;
11322 TREE_VALUE (tail) = output;
11325 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
11327 tree input;
11329 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
11330 input = TREE_VALUE (tail);
11332 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
11333 oconstraints, &allows_mem, &allows_reg))
11335 /* If the operand is going to end up in memory,
11336 mark it addressable. */
11337 if (!allows_reg && allows_mem)
11339 input = c_fully_fold (input, false, NULL, true);
11341 /* Strip the nops as we allow this case. FIXME, this really
11342 should be rejected or made deprecated. */
11343 STRIP_NOPS (input);
11344 if (!c_mark_addressable (input))
11345 input = error_mark_node;
11347 else
11349 struct c_expr expr;
11350 memset (&expr, 0, sizeof (expr));
11351 expr.value = input;
11352 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
11353 input = c_fully_fold (expr.value, false, NULL);
11355 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
11357 error_at (loc, "invalid use of void expression");
11358 input = error_mark_node;
11362 else
11363 input = error_mark_node;
11365 TREE_VALUE (tail) = input;
11368 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
11370 /* asm statements without outputs, including simple ones, are treated
11371 as volatile. */
11372 ASM_INPUT_P (args) = simple;
11373 ASM_VOLATILE_P (args) = (noutputs == 0);
11374 ASM_INLINE_P (args) = is_inline;
11376 return args;
11379 /* Generate a goto statement to LABEL. LOC is the location of the
11380 GOTO. */
11382 tree
11383 c_finish_goto_label (location_t loc, tree label)
11385 tree decl = lookup_label_for_goto (loc, label);
11386 if (!decl)
11387 return NULL_TREE;
11388 TREE_USED (decl) = 1;
11390 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
11391 tree t = build1 (GOTO_EXPR, void_type_node, decl);
11392 SET_EXPR_LOCATION (t, loc);
11393 return add_stmt (t);
11397 /* Generate a computed goto statement to EXPR. LOC is the location of
11398 the GOTO. */
11400 tree
11401 c_finish_goto_ptr (location_t loc, c_expr val)
11403 tree expr = val.value;
11404 tree t;
11405 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
11406 if (expr != error_mark_node
11407 && !POINTER_TYPE_P (TREE_TYPE (expr))
11408 && !null_pointer_constant_p (expr))
11410 error_at (val.get_location (),
11411 "computed goto must be pointer type");
11412 expr = build_zero_cst (ptr_type_node);
11414 expr = c_fully_fold (expr, false, NULL);
11415 expr = convert (ptr_type_node, expr);
11416 t = build1 (GOTO_EXPR, void_type_node, expr);
11417 SET_EXPR_LOCATION (t, loc);
11418 return add_stmt (t);
11421 /* Generate a C `return' statement. RETVAL is the expression for what
11422 to return, or a null pointer for `return;' with no value. LOC is
11423 the location of the return statement, or the location of the expression,
11424 if the statement has any. If ORIGTYPE is not NULL_TREE, it
11425 is the original type of RETVAL. */
11427 tree
11428 c_finish_return (location_t loc, tree retval, tree origtype)
11430 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
11431 bool no_warning = false;
11432 bool npc = false;
11434 /* Use the expansion point to handle cases such as returning NULL
11435 in a function returning void. */
11436 location_t xloc = expansion_point_location_if_in_system_header (loc);
11438 if (TREE_THIS_VOLATILE (current_function_decl))
11439 warning_at (xloc, 0,
11440 "function declared %<noreturn%> has a %<return%> statement");
11442 if (retval)
11444 tree semantic_type = NULL_TREE;
11445 npc = null_pointer_constant_p (retval);
11446 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
11448 semantic_type = TREE_TYPE (retval);
11449 retval = TREE_OPERAND (retval, 0);
11451 retval = c_fully_fold (retval, false, NULL);
11452 if (semantic_type
11453 && valtype != NULL_TREE
11454 && TREE_CODE (valtype) != VOID_TYPE)
11455 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
11458 if (!retval)
11460 current_function_returns_null = 1;
11461 if ((warn_return_type >= 0 || flag_isoc99)
11462 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
11464 no_warning = true;
11465 if (emit_diagnostic (flag_isoc99 ? DK_PERMERROR : DK_WARNING,
11466 loc, OPT_Wreturn_mismatch,
11467 "%<return%> with no value,"
11468 " in function returning non-void"))
11469 inform (DECL_SOURCE_LOCATION (current_function_decl),
11470 "declared here");
11473 else if (valtype == NULL_TREE || VOID_TYPE_P (valtype))
11475 current_function_returns_null = 1;
11476 bool warned_here;
11477 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
11478 warned_here = permerror_opt
11479 (xloc, OPT_Wreturn_mismatch,
11480 "%<return%> with a value, in function returning void");
11481 else
11482 warned_here = pedwarn
11483 (xloc, OPT_Wpedantic, "ISO C forbids "
11484 "%<return%> with expression, in function returning void");
11485 if (warned_here)
11486 inform (DECL_SOURCE_LOCATION (current_function_decl),
11487 "declared here");
11489 else
11491 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
11492 retval, origtype, ic_return,
11493 npc, NULL_TREE, NULL_TREE, 0);
11494 tree res = DECL_RESULT (current_function_decl);
11495 tree inner;
11496 bool save;
11498 current_function_returns_value = 1;
11499 if (t == error_mark_node)
11500 return NULL_TREE;
11502 save = in_late_binary_op;
11503 if (C_BOOLEAN_TYPE_P (TREE_TYPE (res))
11504 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
11505 || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
11506 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
11507 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
11508 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
11509 in_late_binary_op = true;
11510 inner = t = convert (TREE_TYPE (res), t);
11511 in_late_binary_op = save;
11513 /* Strip any conversions, additions, and subtractions, and see if
11514 we are returning the address of a local variable. Warn if so. */
11515 while (1)
11517 switch (TREE_CODE (inner))
11519 CASE_CONVERT:
11520 case NON_LVALUE_EXPR:
11521 case PLUS_EXPR:
11522 case POINTER_PLUS_EXPR:
11523 inner = TREE_OPERAND (inner, 0);
11524 continue;
11526 case MINUS_EXPR:
11527 /* If the second operand of the MINUS_EXPR has a pointer
11528 type (or is converted from it), this may be valid, so
11529 don't give a warning. */
11531 tree op1 = TREE_OPERAND (inner, 1);
11533 while (!POINTER_TYPE_P (TREE_TYPE (op1))
11534 && (CONVERT_EXPR_P (op1)
11535 || TREE_CODE (op1) == NON_LVALUE_EXPR))
11536 op1 = TREE_OPERAND (op1, 0);
11538 if (POINTER_TYPE_P (TREE_TYPE (op1)))
11539 break;
11541 inner = TREE_OPERAND (inner, 0);
11542 continue;
11545 case ADDR_EXPR:
11546 inner = TREE_OPERAND (inner, 0);
11548 while (REFERENCE_CLASS_P (inner)
11549 && !INDIRECT_REF_P (inner))
11550 inner = TREE_OPERAND (inner, 0);
11552 if (DECL_P (inner)
11553 && !DECL_EXTERNAL (inner)
11554 && !TREE_STATIC (inner)
11555 && DECL_CONTEXT (inner) == current_function_decl
11556 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
11558 if (TREE_CODE (inner) == LABEL_DECL)
11559 warning_at (loc, OPT_Wreturn_local_addr,
11560 "function returns address of label");
11561 else
11563 warning_at (loc, OPT_Wreturn_local_addr,
11564 "function returns address of local variable");
11565 tree zero = build_zero_cst (TREE_TYPE (res));
11566 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
11569 break;
11571 default:
11572 break;
11575 break;
11578 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
11579 SET_EXPR_LOCATION (retval, loc);
11581 if (warn_sequence_point)
11582 verify_sequence_points (retval);
11585 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
11586 if (no_warning)
11587 suppress_warning (ret_stmt, OPT_Wreturn_type);
11588 return add_stmt (ret_stmt);
11591 struct c_switch {
11592 /* The SWITCH_STMT being built. */
11593 tree switch_stmt;
11595 /* The original type of the testing expression, i.e. before the
11596 default conversion is applied. */
11597 tree orig_type;
11599 /* A splay-tree mapping the low element of a case range to the high
11600 element, or NULL_TREE if there is no high element. Used to
11601 determine whether or not a new case label duplicates an old case
11602 label. We need a tree, rather than simply a hash table, because
11603 of the GNU case range extension. */
11604 splay_tree cases;
11606 /* The bindings at the point of the switch. This is used for
11607 warnings crossing decls when branching to a case label. */
11608 struct c_spot_bindings *bindings;
11610 /* Whether the switch includes any break statements. */
11611 bool break_stmt_seen_p;
11613 /* The next node on the stack. */
11614 struct c_switch *next;
11616 /* Remember whether the controlling expression had boolean type
11617 before integer promotions for the sake of -Wswitch-bool. */
11618 bool bool_cond_p;
11621 /* A stack of the currently active switch statements. The innermost
11622 switch statement is on the top of the stack. There is no need to
11623 mark the stack for garbage collection because it is only active
11624 during the processing of the body of a function, and we never
11625 collect at that point. */
11627 struct c_switch *c_switch_stack;
11629 /* Start a C switch statement, testing expression EXP. Return the new
11630 SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
11631 SWITCH_COND_LOC is the location of the switch's condition.
11632 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
11634 tree
11635 c_start_switch (location_t switch_loc,
11636 location_t switch_cond_loc,
11637 tree exp, bool explicit_cast_p)
11639 tree orig_type = error_mark_node;
11640 bool bool_cond_p = false;
11641 struct c_switch *cs;
11643 if (exp != error_mark_node)
11645 orig_type = TREE_TYPE (exp);
11647 if (!INTEGRAL_TYPE_P (orig_type))
11649 if (orig_type != error_mark_node)
11651 error_at (switch_cond_loc, "switch quantity not an integer");
11652 orig_type = error_mark_node;
11654 exp = integer_zero_node;
11656 else
11658 tree type = TYPE_MAIN_VARIANT (orig_type);
11659 tree e = exp;
11661 /* Warn if the condition has boolean value. */
11662 while (TREE_CODE (e) == COMPOUND_EXPR)
11663 e = TREE_OPERAND (e, 1);
11665 if ((C_BOOLEAN_TYPE_P (type)
11666 || truth_value_p (TREE_CODE (e)))
11667 /* Explicit cast to int suppresses this warning. */
11668 && !(TREE_CODE (type) == INTEGER_TYPE
11669 && explicit_cast_p))
11670 bool_cond_p = true;
11672 if (!in_system_header_at (input_location)
11673 && (type == long_integer_type_node
11674 || type == long_unsigned_type_node))
11675 warning_at (switch_cond_loc,
11676 OPT_Wtraditional, "%<long%> switch expression not "
11677 "converted to %<int%> in ISO C");
11679 exp = c_fully_fold (exp, false, NULL);
11680 exp = default_conversion (exp);
11682 if (warn_sequence_point)
11683 verify_sequence_points (exp);
11687 /* Add this new SWITCH_STMT to the stack. */
11688 cs = XNEW (struct c_switch);
11689 cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11690 NULL_TREE, orig_type, NULL_TREE);
11691 cs->orig_type = orig_type;
11692 cs->cases = splay_tree_new (case_compare, NULL, NULL);
11693 cs->bindings = c_get_switch_bindings ();
11694 cs->break_stmt_seen_p = false;
11695 cs->bool_cond_p = bool_cond_p;
11696 cs->next = c_switch_stack;
11697 c_switch_stack = cs;
11699 return add_stmt (cs->switch_stmt);
11702 /* Process a case label at location LOC, with attributes ATTRS. */
11704 tree
11705 do_case (location_t loc, tree low_value, tree high_value, tree attrs)
11707 tree label = NULL_TREE;
11709 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11711 low_value = c_fully_fold (low_value, false, NULL);
11712 if (TREE_CODE (low_value) == INTEGER_CST)
11713 pedwarn (loc, OPT_Wpedantic,
11714 "case label is not an integer constant expression");
11717 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11719 high_value = c_fully_fold (high_value, false, NULL);
11720 if (TREE_CODE (high_value) == INTEGER_CST)
11721 pedwarn (input_location, OPT_Wpedantic,
11722 "case label is not an integer constant expression");
11725 if (c_switch_stack == NULL)
11727 if (low_value)
11728 error_at (loc, "case label not within a switch statement");
11729 else
11730 error_at (loc, "%<default%> label not within a switch statement");
11731 return NULL_TREE;
11734 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11735 EXPR_LOCATION (c_switch_stack->switch_stmt),
11736 loc))
11737 return NULL_TREE;
11739 label = c_add_case_label (loc, c_switch_stack->cases,
11740 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11741 low_value, high_value, attrs);
11742 if (label == error_mark_node)
11743 label = NULL_TREE;
11744 return label;
11747 /* Finish the switch statement. TYPE is the original type of the
11748 controlling expression of the switch, or NULL_TREE. */
11750 void
11751 c_finish_switch (tree body, tree type)
11753 struct c_switch *cs = c_switch_stack;
11754 location_t switch_location;
11756 SWITCH_STMT_BODY (cs->switch_stmt) = body;
11758 /* Emit warnings as needed. */
11759 switch_location = EXPR_LOCATION (cs->switch_stmt);
11760 c_do_switch_warnings (cs->cases, switch_location,
11761 type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11762 SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11763 if (c_switch_covers_all_cases_p (cs->cases,
11764 SWITCH_STMT_TYPE (cs->switch_stmt)))
11765 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11766 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11768 /* Pop the stack. */
11769 c_switch_stack = cs->next;
11770 splay_tree_delete (cs->cases);
11771 c_release_switch_bindings (cs->bindings);
11772 XDELETE (cs);
11775 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
11776 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11777 may be null. */
11779 void
11780 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11781 tree else_block)
11783 tree stmt;
11785 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11786 SET_EXPR_LOCATION (stmt, if_locus);
11787 add_stmt (stmt);
11790 tree
11791 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11793 /* In switch statements break is sometimes stylistically used after
11794 a return statement. This can lead to spurious warnings about
11795 control reaching the end of a non-void function when it is
11796 inlined. Note that we are calling block_may_fallthru with
11797 language specific tree nodes; this works because
11798 block_may_fallthru returns true when given something it does not
11799 understand. */
11800 bool skip = !block_may_fallthru (cur_stmt_list);
11802 if (is_break)
11803 switch (in_statement)
11805 case 0:
11806 error_at (loc, "break statement not within loop or switch");
11807 return NULL_TREE;
11808 case IN_OMP_BLOCK:
11809 error_at (loc, "invalid exit from OpenMP structured block");
11810 return NULL_TREE;
11811 case IN_OMP_FOR:
11812 error_at (loc, "break statement used with OpenMP for loop");
11813 return NULL_TREE;
11814 case IN_ITERATION_STMT:
11815 case IN_OBJC_FOREACH:
11816 break;
11817 default:
11818 gcc_assert (in_statement & IN_SWITCH_STMT);
11819 c_switch_stack->break_stmt_seen_p = true;
11820 break;
11822 else
11823 switch (in_statement & ~IN_SWITCH_STMT)
11825 case 0:
11826 error_at (loc, "continue statement not within a loop");
11827 return NULL_TREE;
11828 case IN_OMP_BLOCK:
11829 error_at (loc, "invalid exit from OpenMP structured block");
11830 return NULL_TREE;
11831 case IN_ITERATION_STMT:
11832 case IN_OMP_FOR:
11833 case IN_OBJC_FOREACH:
11834 break;
11835 default:
11836 gcc_unreachable ();
11839 if (skip)
11840 return NULL_TREE;
11841 else if ((in_statement & IN_OBJC_FOREACH)
11842 && !(is_break && (in_statement & IN_SWITCH_STMT)))
11844 /* The foreach expander produces low-level code using gotos instead
11845 of a structured loop construct. */
11846 gcc_assert (label);
11847 return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11849 return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11852 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11854 static void
11855 emit_side_effect_warnings (location_t loc, tree expr)
11857 maybe_warn_nodiscard (loc, expr);
11858 if (!warn_unused_value)
11859 return;
11860 if (expr == error_mark_node)
11862 else if (!TREE_SIDE_EFFECTS (expr))
11864 if (!VOID_TYPE_P (TREE_TYPE (expr))
11865 && !warning_suppressed_p (expr, OPT_Wunused_value))
11866 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11868 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11870 tree r = expr;
11871 location_t cloc = loc;
11872 while (TREE_CODE (r) == COMPOUND_EXPR)
11874 if (EXPR_HAS_LOCATION (r))
11875 cloc = EXPR_LOCATION (r);
11876 r = TREE_OPERAND (r, 1);
11878 if (!TREE_SIDE_EFFECTS (r)
11879 && !VOID_TYPE_P (TREE_TYPE (r))
11880 && !CONVERT_EXPR_P (r)
11881 && !warning_suppressed_p (r, OPT_Wunused_value)
11882 && !warning_suppressed_p (expr, OPT_Wunused_value))
11883 warning_at (cloc, OPT_Wunused_value,
11884 "right-hand operand of comma expression has no effect");
11886 else
11887 warn_if_unused_value (expr, loc);
11890 /* Process an expression as if it were a complete statement. Emit
11891 diagnostics, but do not call ADD_STMT. LOC is the location of the
11892 statement. */
11894 tree
11895 c_process_expr_stmt (location_t loc, tree expr)
11897 tree exprv;
11899 if (!expr)
11900 return NULL_TREE;
11902 expr = c_fully_fold (expr, false, NULL);
11904 if (warn_sequence_point)
11905 verify_sequence_points (expr);
11907 if (TREE_TYPE (expr) != error_mark_node
11908 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11909 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11910 error_at (loc, "expression statement has incomplete type");
11912 /* If we're not processing a statement expression, warn about unused values.
11913 Warnings for statement expressions will be emitted later, once we figure
11914 out which is the result. */
11915 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11916 && (warn_unused_value || warn_unused_result))
11917 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11919 exprv = expr;
11920 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11921 exprv = TREE_OPERAND (exprv, 1);
11922 while (CONVERT_EXPR_P (exprv))
11923 exprv = TREE_OPERAND (exprv, 0);
11924 if (DECL_P (exprv)
11925 || handled_component_p (exprv)
11926 || TREE_CODE (exprv) == ADDR_EXPR)
11927 mark_exp_read (exprv);
11929 /* If the expression is not of a type to which we cannot assign a line
11930 number, wrap the thing in a no-op NOP_EXPR. */
11931 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11933 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11934 SET_EXPR_LOCATION (expr, loc);
11937 return expr;
11940 /* Emit an expression as a statement. LOC is the location of the
11941 expression. */
11943 tree
11944 c_finish_expr_stmt (location_t loc, tree expr)
11946 if (expr)
11947 return add_stmt (c_process_expr_stmt (loc, expr));
11948 else
11949 return NULL;
11952 /* Do the opposite and emit a statement as an expression. To begin,
11953 create a new binding level and return it. */
11955 tree
11956 c_begin_stmt_expr (void)
11958 tree ret;
11960 /* We must force a BLOCK for this level so that, if it is not expanded
11961 later, there is a way to turn off the entire subtree of blocks that
11962 are contained in it. */
11963 keep_next_level ();
11964 ret = c_begin_compound_stmt (true);
11966 c_bindings_start_stmt_expr (c_switch_stack == NULL
11967 ? NULL
11968 : c_switch_stack->bindings);
11970 /* Mark the current statement list as belonging to a statement list. */
11971 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11973 return ret;
11976 /* LOC is the location of the compound statement to which this body
11977 belongs. */
11979 tree
11980 c_finish_stmt_expr (location_t loc, tree body)
11982 tree last, type, tmp, val;
11983 tree *last_p;
11985 body = c_end_compound_stmt (loc, body, true);
11987 c_bindings_end_stmt_expr (c_switch_stack == NULL
11988 ? NULL
11989 : c_switch_stack->bindings);
11991 /* Locate the last statement in BODY. See c_end_compound_stmt
11992 about always returning a BIND_EXPR. */
11993 last_p = &BIND_EXPR_BODY (body);
11994 last = BIND_EXPR_BODY (body);
11996 continue_searching:
11997 if (TREE_CODE (last) == STATEMENT_LIST)
11999 tree_stmt_iterator l = tsi_last (last);
12001 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
12002 tsi_prev (&l);
12004 /* This can happen with degenerate cases like ({ }). No value. */
12005 if (tsi_end_p (l))
12006 return body;
12008 /* If we're supposed to generate side effects warnings, process
12009 all of the statements except the last. */
12010 if (warn_unused_value || warn_unused_result)
12012 for (tree_stmt_iterator i = tsi_start (last);
12013 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
12015 location_t tloc;
12016 tree t = tsi_stmt (i);
12018 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
12019 emit_side_effect_warnings (tloc, t);
12022 last_p = tsi_stmt_ptr (l);
12023 last = *last_p;
12026 /* If the end of the list is exception related, then the list was split
12027 by a call to push_cleanup. Continue searching. */
12028 if (TREE_CODE (last) == TRY_FINALLY_EXPR
12029 || TREE_CODE (last) == TRY_CATCH_EXPR)
12031 last_p = &TREE_OPERAND (last, 0);
12032 last = *last_p;
12033 goto continue_searching;
12036 if (last == error_mark_node)
12037 return last;
12039 /* In the case that the BIND_EXPR is not necessary, return the
12040 expression out from inside it. */
12041 if ((last == BIND_EXPR_BODY (body)
12042 /* Skip nested debug stmts. */
12043 || last == expr_first (BIND_EXPR_BODY (body)))
12044 && BIND_EXPR_VARS (body) == NULL)
12046 /* Even if this looks constant, do not allow it in a constant
12047 expression. */
12048 last = c_wrap_maybe_const (last, true);
12049 /* Do not warn if the return value of a statement expression is
12050 unused. */
12051 suppress_warning (last, OPT_Wunused);
12052 return last;
12055 /* Extract the type of said expression. */
12056 type = TREE_TYPE (last);
12058 /* If we're not returning a value at all, then the BIND_EXPR that
12059 we already have is a fine expression to return. */
12060 if (!type || VOID_TYPE_P (type))
12061 return body;
12063 /* Now that we've located the expression containing the value, it seems
12064 silly to make voidify_wrapper_expr repeat the process. Create a
12065 temporary of the appropriate type and stick it in a TARGET_EXPR. */
12066 tmp = create_tmp_var_raw (type);
12068 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
12069 tree_expr_nonnegative_p giving up immediately. */
12070 val = last;
12071 if (TREE_CODE (val) == NOP_EXPR
12072 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
12073 val = TREE_OPERAND (val, 0);
12075 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
12076 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
12079 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
12080 SET_EXPR_LOCATION (t, loc);
12081 return t;
12085 /* Begin and end compound statements. This is as simple as pushing
12086 and popping new statement lists from the tree. */
12088 tree
12089 c_begin_compound_stmt (bool do_scope)
12091 tree stmt = push_stmt_list ();
12092 if (do_scope)
12093 push_scope ();
12094 return stmt;
12097 /* End a compound statement. STMT is the statement. LOC is the
12098 location of the compound statement-- this is usually the location
12099 of the opening brace. */
12101 tree
12102 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
12104 tree block = NULL;
12106 if (do_scope)
12108 if (c_dialect_objc ())
12109 objc_clear_super_receiver ();
12110 block = pop_scope ();
12113 stmt = pop_stmt_list (stmt);
12114 stmt = c_build_bind_expr (loc, block, stmt);
12116 /* If this compound statement is nested immediately inside a statement
12117 expression, then force a BIND_EXPR to be created. Otherwise we'll
12118 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
12119 STATEMENT_LISTs merge, and thus we can lose track of what statement
12120 was really last. */
12121 if (building_stmt_list_p ()
12122 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
12123 && TREE_CODE (stmt) != BIND_EXPR)
12125 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
12126 TREE_SIDE_EFFECTS (stmt) = 1;
12127 SET_EXPR_LOCATION (stmt, loc);
12130 return stmt;
12133 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
12134 when the current scope is exited. EH_ONLY is true when this is not
12135 meant to apply to normal control flow transfer. */
12137 void
12138 push_cleanup (tree decl, tree cleanup, bool eh_only)
12140 enum tree_code code;
12141 tree stmt, list;
12142 bool stmt_expr;
12144 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
12145 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
12146 add_stmt (stmt);
12147 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
12148 list = push_stmt_list ();
12149 TREE_OPERAND (stmt, 0) = list;
12150 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
12153 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
12154 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
12156 static tree
12157 build_vec_cmp (tree_code code, tree type,
12158 tree arg0, tree arg1)
12160 tree zero_vec = build_zero_cst (type);
12161 tree minus_one_vec = build_minus_one_cst (type);
12162 tree cmp_type = truth_type_for (TREE_TYPE (arg0));
12163 tree cmp = build2 (code, cmp_type, arg0, arg1);
12164 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
12167 /* Possibly warn about an address of OP never being NULL in a comparison
12168 operation CODE involving null. */
12170 static void
12171 maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
12173 /* Prevent warnings issued for macro expansion. */
12174 if (!warn_address
12175 || warning_suppressed_p (op, OPT_Waddress)
12176 || from_macro_expansion_at (loc))
12177 return;
12179 if (TREE_CODE (op) == NOP_EXPR)
12181 /* Allow casts to intptr_t to suppress the warning. */
12182 tree type = TREE_TYPE (op);
12183 if (TREE_CODE (type) == INTEGER_TYPE)
12184 return;
12185 op = TREE_OPERAND (op, 0);
12188 if (TREE_CODE (op) == POINTER_PLUS_EXPR)
12190 /* Allow a cast to void* to suppress the warning. */
12191 tree type = TREE_TYPE (TREE_TYPE (op));
12192 if (VOID_TYPE_P (type))
12193 return;
12195 /* Adding any value to a null pointer, including zero, is undefined
12196 in C. This includes the expression &p[0] where p is the null
12197 pointer, although &p[0] will have been folded to p by this point
12198 and so not diagnosed. */
12199 if (code == EQ_EXPR)
12200 warning_at (loc, OPT_Waddress,
12201 "the comparison will always evaluate as %<false%> "
12202 "for the pointer operand in %qE must not be NULL",
12203 op);
12204 else
12205 warning_at (loc, OPT_Waddress,
12206 "the comparison will always evaluate as %<true%> "
12207 "for the pointer operand in %qE must not be NULL",
12208 op);
12210 return;
12213 if (TREE_CODE (op) != ADDR_EXPR)
12214 return;
12216 op = TREE_OPERAND (op, 0);
12218 if (TREE_CODE (op) == IMAGPART_EXPR
12219 || TREE_CODE (op) == REALPART_EXPR)
12221 /* The address of either complex part may not be null. */
12222 if (code == EQ_EXPR)
12223 warning_at (loc, OPT_Waddress,
12224 "the comparison will always evaluate as %<false%> "
12225 "for the address of %qE will never be NULL",
12226 op);
12227 else
12228 warning_at (loc, OPT_Waddress,
12229 "the comparison will always evaluate as %<true%> "
12230 "for the address of %qE will never be NULL",
12231 op);
12232 return;
12235 /* Set to true in the loop below if OP dereferences is operand.
12236 In such a case the ultimate target need not be a decl for
12237 the null [in]equality test to be constant. */
12238 bool deref = false;
12240 /* Get the outermost array or object, or member. */
12241 while (handled_component_p (op))
12243 if (TREE_CODE (op) == COMPONENT_REF)
12245 /* Get the member (its address is never null). */
12246 op = TREE_OPERAND (op, 1);
12247 break;
12250 /* Get the outer array/object to refer to in the warning. */
12251 op = TREE_OPERAND (op, 0);
12252 deref = true;
12255 if ((!deref && !decl_with_nonnull_addr_p (op))
12256 || from_macro_expansion_at (loc))
12257 return;
12259 bool w;
12260 if (code == EQ_EXPR)
12261 w = warning_at (loc, OPT_Waddress,
12262 "the comparison will always evaluate as %<false%> "
12263 "for the address of %qE will never be NULL",
12264 op);
12265 else
12266 w = warning_at (loc, OPT_Waddress,
12267 "the comparison will always evaluate as %<true%> "
12268 "for the address of %qE will never be NULL",
12269 op);
12271 if (w && DECL_P (op))
12272 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
12275 /* Build a binary-operation expression without default conversions.
12276 CODE is the kind of expression to build.
12277 LOCATION is the operator's location.
12278 This function differs from `build' in several ways:
12279 the data type of the result is computed and recorded in it,
12280 warnings are generated if arg data types are invalid,
12281 special handling for addition and subtraction of pointers is known,
12282 and some optimization is done (operations on narrow ints
12283 are done in the narrower type when that gives the same result).
12284 Constant folding is also done before the result is returned.
12286 Note that the operands will never have enumeral types, or function
12287 or array types, because either they will have the default conversions
12288 performed or they have both just been converted to some other type in which
12289 the arithmetic is to be done. */
12291 tree
12292 build_binary_op (location_t location, enum tree_code code,
12293 tree orig_op0, tree orig_op1, bool convert_p)
12295 tree type0, type1, orig_type0, orig_type1;
12296 tree eptype;
12297 enum tree_code code0, code1;
12298 tree op0, op1;
12299 tree ret = error_mark_node;
12300 const char *invalid_op_diag;
12301 bool op0_int_operands, op1_int_operands;
12302 bool int_const, int_const_or_overflow, int_operands;
12304 /* Expression code to give to the expression when it is built.
12305 Normally this is CODE, which is what the caller asked for,
12306 but in some special cases we change it. */
12307 enum tree_code resultcode = code;
12309 /* Data type in which the computation is to be performed.
12310 In the simplest cases this is the common type of the arguments. */
12311 tree result_type = NULL;
12313 /* When the computation is in excess precision, the type of the
12314 final EXCESS_PRECISION_EXPR. */
12315 tree semantic_result_type = NULL;
12317 /* Nonzero means operands have already been type-converted
12318 in whatever way is necessary.
12319 Zero means they need to be converted to RESULT_TYPE. */
12320 int converted = 0;
12322 /* Nonzero means create the expression with this type, rather than
12323 RESULT_TYPE. */
12324 tree build_type = NULL_TREE;
12326 /* Nonzero means after finally constructing the expression
12327 convert it to this type. */
12328 tree final_type = NULL_TREE;
12330 /* Nonzero if this is an operation like MIN or MAX which can
12331 safely be computed in short if both args are promoted shorts.
12332 Also implies COMMON.
12333 -1 indicates a bitwise operation; this makes a difference
12334 in the exact conditions for when it is safe to do the operation
12335 in a narrower mode. */
12336 int shorten = 0;
12338 /* Nonzero if this is a comparison operation;
12339 if both args are promoted shorts, compare the original shorts.
12340 Also implies COMMON. */
12341 int short_compare = 0;
12343 /* Nonzero if this is a right-shift operation, which can be computed on the
12344 original short and then promoted if the operand is a promoted short. */
12345 int short_shift = 0;
12347 /* Nonzero means set RESULT_TYPE to the common type of the args. */
12348 int common = 0;
12350 /* True means types are compatible as far as ObjC is concerned. */
12351 bool objc_ok;
12353 /* True means this is an arithmetic operation that may need excess
12354 precision. */
12355 bool may_need_excess_precision;
12357 /* True means this is a boolean operation that converts both its
12358 operands to truth-values. */
12359 bool boolean_op = false;
12361 /* Remember whether we're doing / or %. */
12362 bool doing_div_or_mod = false;
12364 /* Remember whether we're doing << or >>. */
12365 bool doing_shift = false;
12367 /* Tree holding instrumentation expression. */
12368 tree instrument_expr = NULL;
12370 if (location == UNKNOWN_LOCATION)
12371 location = input_location;
12373 op0 = orig_op0;
12374 op1 = orig_op1;
12376 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
12377 if (op0_int_operands)
12378 op0 = remove_c_maybe_const_expr (op0);
12379 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
12380 if (op1_int_operands)
12381 op1 = remove_c_maybe_const_expr (op1);
12382 int_operands = (op0_int_operands && op1_int_operands);
12383 if (int_operands)
12385 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
12386 && TREE_CODE (orig_op1) == INTEGER_CST);
12387 int_const = (int_const_or_overflow
12388 && !TREE_OVERFLOW (orig_op0)
12389 && !TREE_OVERFLOW (orig_op1));
12391 else
12392 int_const = int_const_or_overflow = false;
12394 /* Do not apply default conversion in mixed vector/scalar expression. */
12395 if (convert_p
12396 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
12398 op0 = default_conversion (op0);
12399 op1 = default_conversion (op1);
12402 orig_type0 = type0 = TREE_TYPE (op0);
12404 orig_type1 = type1 = TREE_TYPE (op1);
12406 /* The expression codes of the data types of the arguments tell us
12407 whether the arguments are integers, floating, pointers, etc. */
12408 code0 = TREE_CODE (type0);
12409 code1 = TREE_CODE (type1);
12411 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
12412 STRIP_TYPE_NOPS (op0);
12413 STRIP_TYPE_NOPS (op1);
12415 /* If an error was already reported for one of the arguments,
12416 avoid reporting another error. */
12418 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12419 return error_mark_node;
12421 if (code0 == POINTER_TYPE
12422 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
12423 return error_mark_node;
12425 if (code1 == POINTER_TYPE
12426 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
12427 return error_mark_node;
12429 if ((invalid_op_diag
12430 = targetm.invalid_binary_op (code, type0, type1)))
12432 error_at (location, invalid_op_diag);
12433 return error_mark_node;
12436 switch (code)
12438 case PLUS_EXPR:
12439 case MINUS_EXPR:
12440 case MULT_EXPR:
12441 case TRUNC_DIV_EXPR:
12442 case CEIL_DIV_EXPR:
12443 case FLOOR_DIV_EXPR:
12444 case ROUND_DIV_EXPR:
12445 case EXACT_DIV_EXPR:
12446 may_need_excess_precision = true;
12447 break;
12449 case EQ_EXPR:
12450 case NE_EXPR:
12451 case LE_EXPR:
12452 case GE_EXPR:
12453 case LT_EXPR:
12454 case GT_EXPR:
12455 /* Excess precision for implicit conversions of integers to
12456 floating point in C11 and later. */
12457 may_need_excess_precision = (flag_isoc11
12458 && (ANY_INTEGRAL_TYPE_P (type0)
12459 || ANY_INTEGRAL_TYPE_P (type1)));
12460 break;
12462 default:
12463 may_need_excess_precision = false;
12464 break;
12466 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
12468 op0 = TREE_OPERAND (op0, 0);
12469 type0 = TREE_TYPE (op0);
12471 else if (may_need_excess_precision
12472 && (eptype = excess_precision_type (type0)) != NULL_TREE)
12474 type0 = eptype;
12475 op0 = convert (eptype, op0);
12477 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
12479 op1 = TREE_OPERAND (op1, 0);
12480 type1 = TREE_TYPE (op1);
12482 else if (may_need_excess_precision
12483 && (eptype = excess_precision_type (type1)) != NULL_TREE)
12485 type1 = eptype;
12486 op1 = convert (eptype, op1);
12489 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
12491 /* In case when one of the operands of the binary operation is
12492 a vector and another is a scalar -- convert scalar to vector. */
12493 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
12494 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
12496 enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
12497 orig_op1, true);
12499 switch (convert_flag)
12501 case stv_error:
12502 return error_mark_node;
12503 case stv_firstarg:
12505 bool maybe_const = true;
12506 tree sc;
12507 sc = c_fully_fold (op0, false, &maybe_const);
12508 sc = save_expr (sc);
12509 sc = convert (TREE_TYPE (type1), sc);
12510 op0 = build_vector_from_val (type1, sc);
12511 if (!maybe_const)
12512 op0 = c_wrap_maybe_const (op0, true);
12513 orig_type0 = type0 = TREE_TYPE (op0);
12514 code0 = TREE_CODE (type0);
12515 converted = 1;
12516 break;
12518 case stv_secondarg:
12520 bool maybe_const = true;
12521 tree sc;
12522 sc = c_fully_fold (op1, false, &maybe_const);
12523 sc = save_expr (sc);
12524 sc = convert (TREE_TYPE (type0), sc);
12525 op1 = build_vector_from_val (type0, sc);
12526 if (!maybe_const)
12527 op1 = c_wrap_maybe_const (op1, true);
12528 orig_type1 = type1 = TREE_TYPE (op1);
12529 code1 = TREE_CODE (type1);
12530 converted = 1;
12531 break;
12533 default:
12534 break;
12538 switch (code)
12540 case PLUS_EXPR:
12541 /* Handle the pointer + int case. */
12542 if (code0 == POINTER_TYPE
12543 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12545 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
12546 goto return_build_binary_op;
12548 else if (code1 == POINTER_TYPE
12549 && (code0 == INTEGER_TYPE || code0 == BITINT_TYPE))
12551 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
12552 goto return_build_binary_op;
12554 else
12555 common = 1;
12556 break;
12558 case MINUS_EXPR:
12559 /* Subtraction of two similar pointers.
12560 We must subtract them as integers, then divide by object size. */
12561 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
12562 && comp_target_types (location, type0, type1))
12564 ret = pointer_diff (location, op0, op1, &instrument_expr);
12565 goto return_build_binary_op;
12567 /* Handle pointer minus int. Just like pointer plus int. */
12568 else if (code0 == POINTER_TYPE
12569 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12571 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
12572 goto return_build_binary_op;
12574 else
12575 common = 1;
12576 break;
12578 case MULT_EXPR:
12579 common = 1;
12580 break;
12582 case TRUNC_DIV_EXPR:
12583 case CEIL_DIV_EXPR:
12584 case FLOOR_DIV_EXPR:
12585 case ROUND_DIV_EXPR:
12586 case EXACT_DIV_EXPR:
12587 doing_div_or_mod = true;
12588 warn_for_div_by_zero (location, op1);
12590 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12591 || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
12592 || code0 == COMPLEX_TYPE
12593 || gnu_vector_type_p (type0))
12594 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12595 || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
12596 || code1 == COMPLEX_TYPE
12597 || gnu_vector_type_p (type1)))
12599 enum tree_code tcode0 = code0, tcode1 = code1;
12601 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
12602 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
12603 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
12604 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
12606 if (!(((tcode0 == INTEGER_TYPE || tcode0 == BITINT_TYPE)
12607 && (tcode1 == INTEGER_TYPE || tcode1 == BITINT_TYPE))
12608 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
12609 resultcode = RDIV_EXPR;
12610 else
12611 /* Although it would be tempting to shorten always here, that
12612 loses on some targets, since the modulo instruction is
12613 undefined if the quotient can't be represented in the
12614 computation mode. We shorten only if unsigned or if
12615 dividing by something we know != -1. */
12616 shorten = may_shorten_divmod (op0, op1);
12617 common = 1;
12619 break;
12621 case BIT_AND_EXPR:
12622 case BIT_IOR_EXPR:
12623 case BIT_XOR_EXPR:
12624 if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
12625 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12626 shorten = -1;
12627 /* Allow vector types which are not floating point types. */
12628 else if (gnu_vector_type_p (type0)
12629 && gnu_vector_type_p (type1)
12630 && !VECTOR_FLOAT_TYPE_P (type0)
12631 && !VECTOR_FLOAT_TYPE_P (type1))
12632 common = 1;
12633 break;
12635 case TRUNC_MOD_EXPR:
12636 case FLOOR_MOD_EXPR:
12637 doing_div_or_mod = true;
12638 warn_for_div_by_zero (location, op1);
12640 if (gnu_vector_type_p (type0)
12641 && gnu_vector_type_p (type1)
12642 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12643 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12644 common = 1;
12645 else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
12646 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12648 /* Although it would be tempting to shorten always here, that loses
12649 on some targets, since the modulo instruction is undefined if the
12650 quotient can't be represented in the computation mode. We shorten
12651 only if unsigned or if dividing by something we know != -1. */
12652 shorten = may_shorten_divmod (op0, op1);
12653 common = 1;
12655 break;
12657 case TRUTH_ANDIF_EXPR:
12658 case TRUTH_ORIF_EXPR:
12659 case TRUTH_AND_EXPR:
12660 case TRUTH_OR_EXPR:
12661 case TRUTH_XOR_EXPR:
12662 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
12663 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12664 || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE
12665 || code0 == BITINT_TYPE)
12666 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
12667 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12668 || code1 == FIXED_POINT_TYPE || code1 == NULLPTR_TYPE
12669 || code1 == BITINT_TYPE))
12671 /* Result of these operations is always an int,
12672 but that does not mean the operands should be
12673 converted to ints! */
12674 result_type = integer_type_node;
12675 if (op0_int_operands)
12677 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
12678 op0 = remove_c_maybe_const_expr (op0);
12680 else
12681 op0 = c_objc_common_truthvalue_conversion (location, op0);
12682 if (op1_int_operands)
12684 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
12685 op1 = remove_c_maybe_const_expr (op1);
12687 else
12688 op1 = c_objc_common_truthvalue_conversion (location, op1);
12689 converted = 1;
12690 boolean_op = true;
12692 if (code == TRUTH_ANDIF_EXPR)
12694 int_const_or_overflow = (int_operands
12695 && TREE_CODE (orig_op0) == INTEGER_CST
12696 && (op0 == truthvalue_false_node
12697 || TREE_CODE (orig_op1) == INTEGER_CST));
12698 int_const = (int_const_or_overflow
12699 && !TREE_OVERFLOW (orig_op0)
12700 && (op0 == truthvalue_false_node
12701 || !TREE_OVERFLOW (orig_op1)));
12703 else if (code == TRUTH_ORIF_EXPR)
12705 int_const_or_overflow = (int_operands
12706 && TREE_CODE (orig_op0) == INTEGER_CST
12707 && (op0 == truthvalue_true_node
12708 || TREE_CODE (orig_op1) == INTEGER_CST));
12709 int_const = (int_const_or_overflow
12710 && !TREE_OVERFLOW (orig_op0)
12711 && (op0 == truthvalue_true_node
12712 || !TREE_OVERFLOW (orig_op1)));
12714 break;
12716 /* Shift operations: result has same type as first operand;
12717 always convert second operand to int.
12718 Also set SHORT_SHIFT if shifting rightward. */
12720 case RSHIFT_EXPR:
12721 if (gnu_vector_type_p (type0)
12722 && gnu_vector_type_p (type1)
12723 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12724 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12725 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12726 TYPE_VECTOR_SUBPARTS (type1)))
12728 result_type = type0;
12729 converted = 1;
12731 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12732 || code0 == BITINT_TYPE
12733 || (gnu_vector_type_p (type0)
12734 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12735 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12737 doing_shift = true;
12738 if (TREE_CODE (op1) == INTEGER_CST)
12740 if (tree_int_cst_sgn (op1) < 0)
12742 int_const = false;
12743 if (c_inhibit_evaluation_warnings == 0)
12744 warning_at (location, OPT_Wshift_count_negative,
12745 "right shift count is negative");
12747 else if (code0 == VECTOR_TYPE)
12749 if (compare_tree_int (op1,
12750 TYPE_PRECISION (TREE_TYPE (type0)))
12751 >= 0)
12753 int_const = false;
12754 if (c_inhibit_evaluation_warnings == 0)
12755 warning_at (location, OPT_Wshift_count_overflow,
12756 "right shift count >= width of vector element");
12759 else
12761 if (!integer_zerop (op1))
12762 short_shift = 1;
12764 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12766 int_const = false;
12767 if (c_inhibit_evaluation_warnings == 0)
12768 warning_at (location, OPT_Wshift_count_overflow,
12769 "right shift count >= width of type");
12774 /* Use the type of the value to be shifted. */
12775 result_type = type0;
12776 /* Avoid converting op1 to result_type later. */
12777 converted = 1;
12779 break;
12781 case LSHIFT_EXPR:
12782 if (gnu_vector_type_p (type0)
12783 && gnu_vector_type_p (type1)
12784 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12785 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12786 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12787 TYPE_VECTOR_SUBPARTS (type1)))
12789 result_type = type0;
12790 converted = 1;
12792 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12793 || code0 == BITINT_TYPE
12794 || (gnu_vector_type_p (type0)
12795 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12796 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12798 doing_shift = true;
12799 if (TREE_CODE (op0) == INTEGER_CST
12800 && tree_int_cst_sgn (op0) < 0
12801 && !TYPE_OVERFLOW_WRAPS (type0))
12803 /* Don't reject a left shift of a negative value in a context
12804 where a constant expression is needed in C90. */
12805 if (flag_isoc99)
12806 int_const = false;
12807 if (c_inhibit_evaluation_warnings == 0)
12808 warning_at (location, OPT_Wshift_negative_value,
12809 "left shift of negative value");
12811 if (TREE_CODE (op1) == INTEGER_CST)
12813 if (tree_int_cst_sgn (op1) < 0)
12815 int_const = false;
12816 if (c_inhibit_evaluation_warnings == 0)
12817 warning_at (location, OPT_Wshift_count_negative,
12818 "left shift count is negative");
12820 else if (code0 == VECTOR_TYPE)
12822 if (compare_tree_int (op1,
12823 TYPE_PRECISION (TREE_TYPE (type0)))
12824 >= 0)
12826 int_const = false;
12827 if (c_inhibit_evaluation_warnings == 0)
12828 warning_at (location, OPT_Wshift_count_overflow,
12829 "left shift count >= width of vector element");
12832 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12834 int_const = false;
12835 if (c_inhibit_evaluation_warnings == 0)
12836 warning_at (location, OPT_Wshift_count_overflow,
12837 "left shift count >= width of type");
12839 else if (TREE_CODE (op0) == INTEGER_CST
12840 && maybe_warn_shift_overflow (location, op0, op1)
12841 && flag_isoc99)
12842 int_const = false;
12845 /* Use the type of the value to be shifted. */
12846 result_type = type0;
12847 /* Avoid converting op1 to result_type later. */
12848 converted = 1;
12850 break;
12852 case EQ_EXPR:
12853 case NE_EXPR:
12854 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12856 tree intt;
12857 if (!vector_types_compatible_elements_p (type0, type1))
12859 error_at (location, "comparing vectors with different "
12860 "element types");
12861 return error_mark_node;
12864 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12865 TYPE_VECTOR_SUBPARTS (type1)))
12867 error_at (location, "comparing vectors with different "
12868 "number of elements");
12869 return error_mark_node;
12872 /* It's not precisely specified how the usual arithmetic
12873 conversions apply to the vector types. Here, we use
12874 the unsigned type if one of the operands is signed and
12875 the other one is unsigned. */
12876 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12878 if (!TYPE_UNSIGNED (type0))
12879 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12880 else
12881 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12882 warning_at (location, OPT_Wsign_compare, "comparison between "
12883 "types %qT and %qT", type0, type1);
12886 /* Always construct signed integer vector type. */
12887 intt = c_common_type_for_size (GET_MODE_BITSIZE
12888 (SCALAR_TYPE_MODE
12889 (TREE_TYPE (type0))), 0);
12890 if (!intt)
12892 error_at (location, "could not find an integer type "
12893 "of the same size as %qT",
12894 TREE_TYPE (type0));
12895 return error_mark_node;
12897 result_type = build_opaque_vector_type (intt,
12898 TYPE_VECTOR_SUBPARTS (type0));
12899 converted = 1;
12900 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12901 goto return_build_binary_op;
12903 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12904 warning_at (location,
12905 OPT_Wfloat_equal,
12906 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12907 /* Result of comparison is always int,
12908 but don't convert the args to int! */
12909 build_type = integer_type_node;
12910 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == BITINT_TYPE
12911 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12912 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12913 || code1 == BITINT_TYPE
12914 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12915 short_compare = 1;
12916 else if (code0 == POINTER_TYPE
12917 && (code1 == NULLPTR_TYPE
12918 || null_pointer_constant_p (orig_op1)))
12920 maybe_warn_for_null_address (location, op0, code);
12921 result_type = type0;
12923 else if (code1 == POINTER_TYPE
12924 && (code0 == NULLPTR_TYPE
12925 || null_pointer_constant_p (orig_op0)))
12927 maybe_warn_for_null_address (location, op1, code);
12928 result_type = type1;
12930 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12932 tree tt0 = TREE_TYPE (type0);
12933 tree tt1 = TREE_TYPE (type1);
12934 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12935 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12936 addr_space_t as_common = ADDR_SPACE_GENERIC;
12938 /* Anything compares with void *. void * compares with anything.
12939 Otherwise, the targets must be compatible
12940 and both must be object or both incomplete. */
12941 if (comp_target_types (location, type0, type1))
12942 result_type = common_pointer_type (type0, type1);
12943 else if (!addr_space_superset (as0, as1, &as_common))
12945 error_at (location, "comparison of pointers to "
12946 "disjoint address spaces");
12947 return error_mark_node;
12949 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12951 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12952 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12953 "comparison of %<void *%> with function pointer");
12955 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12957 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12958 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12959 "comparison of %<void *%> with function pointer");
12961 else
12962 /* Avoid warning about the volatile ObjC EH puts on decls. */
12963 if (!objc_ok)
12964 pedwarn (location, OPT_Wcompare_distinct_pointer_types,
12965 "comparison of distinct pointer types lacks a cast");
12967 if (result_type == NULL_TREE)
12969 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12970 result_type = build_pointer_type
12971 (build_qualified_type (void_type_node, qual));
12974 else if (code0 == POINTER_TYPE
12975 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12977 result_type = type0;
12978 pedwarn (location, 0, "comparison between pointer and integer");
12980 else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
12981 && code1 == POINTER_TYPE)
12983 result_type = type1;
12984 pedwarn (location, 0, "comparison between pointer and integer");
12986 /* 6.5.9: One of the following shall hold:
12987 -- both operands have type nullptr_t; */
12988 else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE)
12990 result_type = nullptr_type_node;
12991 /* No need to convert the operands to result_type later. */
12992 converted = 1;
12994 /* -- one operand has type nullptr_t and the other is a null pointer
12995 constant. We will have to convert the former to the type of the
12996 latter, because during gimplification we can't have mismatching
12997 comparison operand type. We convert from nullptr_t to the other
12998 type, since only nullptr_t can be converted to nullptr_t. Also,
12999 even a constant 0 is a null pointer constant, so we may have to
13000 create a pointer type from its type. */
13001 else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1))
13002 result_type = (INTEGRAL_TYPE_P (type1)
13003 ? build_pointer_type (type1) : type1);
13004 else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0))
13005 result_type = (INTEGRAL_TYPE_P (type0)
13006 ? build_pointer_type (type0) : type0);
13007 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
13008 || truth_value_p (TREE_CODE (orig_op0)))
13009 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
13010 || truth_value_p (TREE_CODE (orig_op1))))
13011 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
13012 break;
13014 case LE_EXPR:
13015 case GE_EXPR:
13016 case LT_EXPR:
13017 case GT_EXPR:
13018 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
13020 tree intt;
13021 if (!vector_types_compatible_elements_p (type0, type1))
13023 error_at (location, "comparing vectors with different "
13024 "element types");
13025 return error_mark_node;
13028 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
13029 TYPE_VECTOR_SUBPARTS (type1)))
13031 error_at (location, "comparing vectors with different "
13032 "number of elements");
13033 return error_mark_node;
13036 /* It's not precisely specified how the usual arithmetic
13037 conversions apply to the vector types. Here, we use
13038 the unsigned type if one of the operands is signed and
13039 the other one is unsigned. */
13040 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
13042 if (!TYPE_UNSIGNED (type0))
13043 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
13044 else
13045 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
13046 warning_at (location, OPT_Wsign_compare, "comparison between "
13047 "types %qT and %qT", type0, type1);
13050 /* Always construct signed integer vector type. */
13051 intt = c_common_type_for_size (GET_MODE_BITSIZE
13052 (SCALAR_TYPE_MODE
13053 (TREE_TYPE (type0))), 0);
13054 if (!intt)
13056 error_at (location, "could not find an integer type "
13057 "of the same size as %qT",
13058 TREE_TYPE (type0));
13059 return error_mark_node;
13061 result_type = build_opaque_vector_type (intt,
13062 TYPE_VECTOR_SUBPARTS (type0));
13063 converted = 1;
13064 ret = build_vec_cmp (resultcode, result_type, op0, op1);
13065 goto return_build_binary_op;
13067 build_type = integer_type_node;
13068 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
13069 || code0 == BITINT_TYPE || code0 == FIXED_POINT_TYPE)
13070 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
13071 || code1 == BITINT_TYPE || code1 == FIXED_POINT_TYPE))
13072 short_compare = 1;
13073 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
13075 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
13076 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
13077 addr_space_t as_common;
13079 if (comp_target_types (location, type0, type1))
13081 result_type = common_pointer_type (type0, type1);
13082 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
13083 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
13084 pedwarn_c99 (location, OPT_Wpedantic,
13085 "comparison of complete and incomplete pointers");
13086 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
13087 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
13088 "ordered comparisons of pointers to functions");
13089 else if (null_pointer_constant_p (orig_op0)
13090 || null_pointer_constant_p (orig_op1))
13091 warning_at (location, OPT_Wextra,
13092 "ordered comparison of pointer with null pointer");
13095 else if (!addr_space_superset (as0, as1, &as_common))
13097 error_at (location, "comparison of pointers to "
13098 "disjoint address spaces");
13099 return error_mark_node;
13101 else
13103 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
13104 result_type = build_pointer_type
13105 (build_qualified_type (void_type_node, qual));
13106 pedwarn (location, OPT_Wcompare_distinct_pointer_types,
13107 "comparison of distinct pointer types lacks a cast");
13110 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
13112 result_type = type0;
13113 if (pedantic)
13114 pedwarn (location, OPT_Wpedantic,
13115 "ordered comparison of pointer with integer zero");
13116 else if (extra_warnings)
13117 warning_at (location, OPT_Wextra,
13118 "ordered comparison of pointer with integer zero");
13120 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
13122 result_type = type1;
13123 if (pedantic)
13124 pedwarn (location, OPT_Wpedantic,
13125 "ordered comparison of pointer with integer zero");
13126 else if (extra_warnings)
13127 warning_at (location, OPT_Wextra,
13128 "ordered comparison of pointer with integer zero");
13130 else if (code0 == POINTER_TYPE
13131 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
13133 result_type = type0;
13134 pedwarn (location, 0, "comparison between pointer and integer");
13136 else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
13137 && code1 == POINTER_TYPE)
13139 result_type = type1;
13140 pedwarn (location, 0, "comparison between pointer and integer");
13143 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
13144 && current_function_decl != NULL_TREE
13145 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
13147 op0 = save_expr (op0);
13148 op1 = save_expr (op1);
13150 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
13151 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
13154 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
13155 || truth_value_p (TREE_CODE (orig_op0)))
13156 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
13157 || truth_value_p (TREE_CODE (orig_op1))))
13158 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
13159 break;
13161 case MIN_EXPR:
13162 case MAX_EXPR:
13163 /* Used for OpenMP atomics. */
13164 gcc_assert (flag_openmp);
13165 common = 1;
13166 break;
13168 default:
13169 gcc_unreachable ();
13172 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
13173 return error_mark_node;
13175 if (gnu_vector_type_p (type0)
13176 && gnu_vector_type_p (type1)
13177 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
13178 || !vector_types_compatible_elements_p (type0, type1)))
13180 gcc_rich_location richloc (location);
13181 maybe_range_label_for_tree_type_mismatch
13182 label_for_op0 (orig_op0, orig_op1),
13183 label_for_op1 (orig_op1, orig_op0);
13184 richloc.maybe_add_expr (orig_op0, &label_for_op0);
13185 richloc.maybe_add_expr (orig_op1, &label_for_op1);
13186 binary_op_error (&richloc, code, type0, type1);
13187 return error_mark_node;
13190 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
13191 || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
13192 || gnu_vector_type_p (type0))
13193 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
13194 || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
13195 || gnu_vector_type_p (type1)))
13197 bool first_complex = (code0 == COMPLEX_TYPE);
13198 bool second_complex = (code1 == COMPLEX_TYPE);
13199 int none_complex = (!first_complex && !second_complex);
13201 if (shorten || common || short_compare)
13203 result_type = c_common_type (type0, type1);
13204 do_warn_double_promotion (result_type, type0, type1,
13205 "implicit conversion from %qT to %qT "
13206 "to match other operand of binary "
13207 "expression",
13208 location);
13209 if (result_type == error_mark_node)
13210 return error_mark_node;
13213 if (first_complex != second_complex
13214 && (code == PLUS_EXPR
13215 || code == MINUS_EXPR
13216 || code == MULT_EXPR
13217 || (code == TRUNC_DIV_EXPR && first_complex))
13218 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
13219 && flag_signed_zeros)
13221 /* An operation on mixed real/complex operands must be
13222 handled specially, but the language-independent code can
13223 more easily optimize the plain complex arithmetic if
13224 -fno-signed-zeros. */
13225 tree real_type = TREE_TYPE (result_type);
13226 tree real, imag;
13227 if (type0 != orig_type0 || type1 != orig_type1)
13229 gcc_assert (may_need_excess_precision && common);
13230 semantic_result_type = c_common_type (orig_type0, orig_type1);
13232 if (first_complex)
13234 if (TREE_TYPE (op0) != result_type)
13235 op0 = convert_and_check (location, result_type, op0);
13236 if (TREE_TYPE (op1) != real_type)
13237 op1 = convert_and_check (location, real_type, op1);
13239 else
13241 if (TREE_TYPE (op0) != real_type)
13242 op0 = convert_and_check (location, real_type, op0);
13243 if (TREE_TYPE (op1) != result_type)
13244 op1 = convert_and_check (location, result_type, op1);
13246 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
13247 return error_mark_node;
13248 if (first_complex)
13250 op0 = save_expr (op0);
13251 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
13252 op0, true);
13253 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
13254 op0, true);
13255 switch (code)
13257 case MULT_EXPR:
13258 case TRUNC_DIV_EXPR:
13259 op1 = save_expr (op1);
13260 imag = build2 (resultcode, real_type, imag, op1);
13261 /* Fall through. */
13262 case PLUS_EXPR:
13263 case MINUS_EXPR:
13264 real = build2 (resultcode, real_type, real, op1);
13265 break;
13266 default:
13267 gcc_unreachable();
13270 else
13272 op1 = save_expr (op1);
13273 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
13274 op1, true);
13275 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
13276 op1, true);
13277 switch (code)
13279 case MULT_EXPR:
13280 op0 = save_expr (op0);
13281 imag = build2 (resultcode, real_type, op0, imag);
13282 /* Fall through. */
13283 case PLUS_EXPR:
13284 real = build2 (resultcode, real_type, op0, real);
13285 break;
13286 case MINUS_EXPR:
13287 real = build2 (resultcode, real_type, op0, real);
13288 imag = build1 (NEGATE_EXPR, real_type, imag);
13289 break;
13290 default:
13291 gcc_unreachable();
13294 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
13295 goto return_build_binary_op;
13298 /* For certain operations (which identify themselves by shorten != 0)
13299 if both args were extended from the same smaller type,
13300 do the arithmetic in that type and then extend.
13302 shorten !=0 and !=1 indicates a bitwise operation.
13303 For them, this optimization is safe only if
13304 both args are zero-extended or both are sign-extended.
13305 Otherwise, we might change the result.
13306 Eg, (short)-1 | (unsigned short)-1 is (int)-1
13307 but calculated in (unsigned short) it would be (unsigned short)-1. */
13309 if (shorten && none_complex)
13311 final_type = result_type;
13312 result_type = shorten_binary_op (result_type, op0, op1,
13313 shorten == -1);
13316 /* Shifts can be shortened if shifting right. */
13318 if (short_shift)
13320 int unsigned_arg;
13321 tree arg0 = get_narrower (op0, &unsigned_arg);
13323 final_type = result_type;
13325 if (arg0 == op0 && final_type == TREE_TYPE (op0))
13326 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
13328 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
13329 && tree_int_cst_sgn (op1) > 0
13330 /* We can shorten only if the shift count is less than the
13331 number of bits in the smaller type size. */
13332 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
13333 /* We cannot drop an unsigned shift after sign-extension. */
13334 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
13336 /* Do an unsigned shift if the operand was zero-extended. */
13337 result_type
13338 = c_common_signed_or_unsigned_type (unsigned_arg,
13339 TREE_TYPE (arg0));
13340 /* Convert value-to-be-shifted to that type. */
13341 if (TREE_TYPE (op0) != result_type)
13342 op0 = convert (result_type, op0);
13343 converted = 1;
13347 /* Comparison operations are shortened too but differently.
13348 They identify themselves by setting short_compare = 1. */
13350 if (short_compare)
13352 /* Don't write &op0, etc., because that would prevent op0
13353 from being kept in a register.
13354 Instead, make copies of the our local variables and
13355 pass the copies by reference, then copy them back afterward. */
13356 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
13357 enum tree_code xresultcode = resultcode;
13358 tree val
13359 = shorten_compare (location, &xop0, &xop1, &xresult_type,
13360 &xresultcode);
13362 if (val != NULL_TREE)
13364 ret = val;
13365 goto return_build_binary_op;
13368 op0 = xop0, op1 = xop1;
13369 converted = 1;
13370 resultcode = xresultcode;
13372 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
13374 bool op0_maybe_const = true;
13375 bool op1_maybe_const = true;
13376 tree orig_op0_folded, orig_op1_folded;
13378 if (in_late_binary_op)
13380 orig_op0_folded = orig_op0;
13381 orig_op1_folded = orig_op1;
13383 else
13385 /* Fold for the sake of possible warnings, as in
13386 build_conditional_expr. This requires the
13387 "original" values to be folded, not just op0 and
13388 op1. */
13389 c_inhibit_evaluation_warnings++;
13390 op0 = c_fully_fold (op0, require_constant_value,
13391 &op0_maybe_const);
13392 op1 = c_fully_fold (op1, require_constant_value,
13393 &op1_maybe_const);
13394 c_inhibit_evaluation_warnings--;
13395 orig_op0_folded = c_fully_fold (orig_op0,
13396 require_constant_value,
13397 NULL);
13398 orig_op1_folded = c_fully_fold (orig_op1,
13399 require_constant_value,
13400 NULL);
13403 if (warn_sign_compare)
13404 warn_for_sign_compare (location, orig_op0_folded,
13405 orig_op1_folded, op0, op1,
13406 result_type, resultcode);
13407 if (!in_late_binary_op && !int_operands)
13409 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
13410 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
13411 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
13412 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
13418 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
13419 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
13420 Then the expression will be built.
13421 It will be given type FINAL_TYPE if that is nonzero;
13422 otherwise, it will be given type RESULT_TYPE. */
13424 if (!result_type)
13426 /* Favor showing any expression locations that are available. */
13427 op_location_t oploc (location, UNKNOWN_LOCATION);
13428 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
13429 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
13430 return error_mark_node;
13433 if (build_type == NULL_TREE)
13435 build_type = result_type;
13436 if ((type0 != orig_type0 || type1 != orig_type1)
13437 && !boolean_op)
13439 gcc_assert (may_need_excess_precision && common);
13440 semantic_result_type = c_common_type (orig_type0, orig_type1);
13444 if (!converted)
13446 op0 = ep_convert_and_check (location, result_type, op0,
13447 semantic_result_type);
13448 op1 = ep_convert_and_check (location, result_type, op1,
13449 semantic_result_type);
13451 /* This can happen if one operand has a vector type, and the other
13452 has a different type. */
13453 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
13454 return error_mark_node;
13457 if (sanitize_flags_p ((SANITIZE_SHIFT
13458 | SANITIZE_DIVIDE
13459 | SANITIZE_FLOAT_DIVIDE
13460 | SANITIZE_SI_OVERFLOW))
13461 && current_function_decl != NULL_TREE
13462 && (doing_div_or_mod || doing_shift)
13463 && !require_constant_value)
13465 /* OP0 and/or OP1 might have side-effects. */
13466 op0 = save_expr (op0);
13467 op1 = save_expr (op1);
13468 op0 = c_fully_fold (op0, false, NULL);
13469 op1 = c_fully_fold (op1, false, NULL);
13470 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
13471 | SANITIZE_FLOAT_DIVIDE
13472 | SANITIZE_SI_OVERFLOW))))
13473 instrument_expr = ubsan_instrument_division (location, op0, op1);
13474 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
13475 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
13478 /* Treat expressions in initializers specially as they can't trap. */
13479 if (int_const_or_overflow)
13480 ret = (require_constant_value
13481 ? fold_build2_initializer_loc (location, resultcode, build_type,
13482 op0, op1)
13483 : fold_build2_loc (location, resultcode, build_type, op0, op1));
13484 else
13485 ret = build2 (resultcode, build_type, op0, op1);
13486 if (final_type != NULL_TREE)
13487 ret = convert (final_type, ret);
13489 return_build_binary_op:
13490 gcc_assert (ret != error_mark_node);
13491 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
13492 ret = (int_operands
13493 ? note_integer_operands (ret)
13494 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
13495 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
13496 && !in_late_binary_op)
13497 ret = note_integer_operands (ret);
13498 protected_set_expr_location (ret, location);
13500 if (instrument_expr != NULL)
13501 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
13502 instrument_expr, ret);
13504 if (semantic_result_type)
13505 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
13506 semantic_result_type, ret);
13508 return ret;
13512 /* Convert EXPR to be a truth-value, validating its type for this
13513 purpose. LOCATION is the source location for the expression. */
13515 tree
13516 c_objc_common_truthvalue_conversion (location_t location, tree expr)
13518 bool int_const, int_operands;
13520 switch (TREE_CODE (TREE_TYPE (expr)))
13522 case ARRAY_TYPE:
13523 error_at (location, "used array that cannot be converted to pointer where scalar is required");
13524 return error_mark_node;
13526 case RECORD_TYPE:
13527 error_at (location, "used struct type value where scalar is required");
13528 return error_mark_node;
13530 case UNION_TYPE:
13531 error_at (location, "used union type value where scalar is required");
13532 return error_mark_node;
13534 case VOID_TYPE:
13535 error_at (location, "void value not ignored as it ought to be");
13536 return error_mark_node;
13538 case POINTER_TYPE:
13539 if (reject_gcc_builtin (expr))
13540 return error_mark_node;
13541 break;
13543 case FUNCTION_TYPE:
13544 gcc_unreachable ();
13546 case VECTOR_TYPE:
13547 error_at (location, "used vector type where scalar is required");
13548 return error_mark_node;
13550 default:
13551 break;
13554 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
13555 int_operands = EXPR_INT_CONST_OPERANDS (expr);
13556 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
13558 expr = remove_c_maybe_const_expr (expr);
13559 expr = build2 (NE_EXPR, integer_type_node, expr,
13560 convert (TREE_TYPE (expr), integer_zero_node));
13561 expr = note_integer_operands (expr);
13563 else
13564 /* ??? Should we also give an error for vectors rather than leaving
13565 those to give errors later? */
13566 expr = c_common_truthvalue_conversion (location, expr);
13568 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
13570 if (TREE_OVERFLOW (expr))
13571 return expr;
13572 else
13573 return note_integer_operands (expr);
13575 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
13576 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
13577 return expr;
13581 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
13582 required. */
13584 tree
13585 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
13587 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
13589 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
13590 /* Executing a compound literal inside a function reinitializes
13591 it. */
13592 if (!TREE_STATIC (decl))
13593 *se = true;
13594 return decl;
13596 else
13597 return expr;
13600 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
13601 statement. LOC is the location of the construct. */
13603 tree
13604 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
13605 tree clauses)
13607 body = c_end_compound_stmt (loc, body, true);
13609 tree stmt = make_node (code);
13610 TREE_TYPE (stmt) = void_type_node;
13611 OMP_BODY (stmt) = body;
13612 OMP_CLAUSES (stmt) = clauses;
13613 SET_EXPR_LOCATION (stmt, loc);
13615 return add_stmt (stmt);
13618 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
13619 statement. LOC is the location of the OACC_DATA. */
13621 tree
13622 c_finish_oacc_data (location_t loc, tree clauses, tree block)
13624 tree stmt;
13626 block = c_end_compound_stmt (loc, block, true);
13628 stmt = make_node (OACC_DATA);
13629 TREE_TYPE (stmt) = void_type_node;
13630 OACC_DATA_CLAUSES (stmt) = clauses;
13631 OACC_DATA_BODY (stmt) = block;
13632 SET_EXPR_LOCATION (stmt, loc);
13634 return add_stmt (stmt);
13637 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
13638 statement. LOC is the location of the OACC_HOST_DATA. */
13640 tree
13641 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
13643 tree stmt;
13645 block = c_end_compound_stmt (loc, block, true);
13647 stmt = make_node (OACC_HOST_DATA);
13648 TREE_TYPE (stmt) = void_type_node;
13649 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
13650 OACC_HOST_DATA_BODY (stmt) = block;
13651 SET_EXPR_LOCATION (stmt, loc);
13653 return add_stmt (stmt);
13656 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13658 tree
13659 c_begin_omp_parallel (void)
13661 tree block;
13663 keep_next_level ();
13664 block = c_begin_compound_stmt (true);
13666 return block;
13669 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
13670 statement. LOC is the location of the OMP_PARALLEL. */
13672 tree
13673 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
13675 tree stmt;
13677 block = c_end_compound_stmt (loc, block, true);
13679 stmt = make_node (OMP_PARALLEL);
13680 TREE_TYPE (stmt) = void_type_node;
13681 OMP_PARALLEL_CLAUSES (stmt) = clauses;
13682 OMP_PARALLEL_BODY (stmt) = block;
13683 SET_EXPR_LOCATION (stmt, loc);
13685 return add_stmt (stmt);
13688 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13690 tree
13691 c_begin_omp_task (void)
13693 tree block;
13695 keep_next_level ();
13696 block = c_begin_compound_stmt (true);
13698 return block;
13701 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
13702 statement. LOC is the location of the #pragma. */
13704 tree
13705 c_finish_omp_task (location_t loc, tree clauses, tree block)
13707 tree stmt;
13709 block = c_end_compound_stmt (loc, block, true);
13711 stmt = make_node (OMP_TASK);
13712 TREE_TYPE (stmt) = void_type_node;
13713 OMP_TASK_CLAUSES (stmt) = clauses;
13714 OMP_TASK_BODY (stmt) = block;
13715 SET_EXPR_LOCATION (stmt, loc);
13717 return add_stmt (stmt);
13720 /* Generate GOMP_cancel call for #pragma omp cancel. */
13722 void
13723 c_finish_omp_cancel (location_t loc, tree clauses)
13725 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
13726 int mask = 0;
13727 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13728 mask = 1;
13729 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13730 mask = 2;
13731 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13732 mask = 4;
13733 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13734 mask = 8;
13735 else
13737 error_at (loc, "%<#pragma omp cancel%> must specify one of "
13738 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13739 "clauses");
13740 return;
13742 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
13743 if (ifc != NULL_TREE)
13745 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
13746 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
13747 error_at (OMP_CLAUSE_LOCATION (ifc),
13748 "expected %<cancel%> %<if%> clause modifier");
13749 else
13751 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
13752 if (ifc2 != NULL_TREE)
13754 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
13755 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13756 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13757 error_at (OMP_CLAUSE_LOCATION (ifc2),
13758 "expected %<cancel%> %<if%> clause modifier");
13762 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13763 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13764 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13765 build_zero_cst (type));
13767 else
13768 ifc = boolean_true_node;
13769 tree stmt = build_call_expr_loc (loc, fn, 2,
13770 build_int_cst (integer_type_node, mask),
13771 ifc);
13772 add_stmt (stmt);
13775 /* Generate GOMP_cancellation_point call for
13776 #pragma omp cancellation point. */
13778 void
13779 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13781 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13782 int mask = 0;
13783 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13784 mask = 1;
13785 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13786 mask = 2;
13787 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13788 mask = 4;
13789 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13790 mask = 8;
13791 else
13793 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13794 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13795 "clauses");
13796 return;
13798 tree stmt = build_call_expr_loc (loc, fn, 1,
13799 build_int_cst (integer_type_node, mask));
13800 add_stmt (stmt);
13803 /* Helper function for handle_omp_array_sections. Called recursively
13804 to handle multiple array-section-subscripts. C is the clause,
13805 T current expression (initially OMP_CLAUSE_DECL), which is either
13806 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13807 expression if specified, TREE_VALUE length expression if specified,
13808 TREE_CHAIN is what it has been specified after, or some decl.
13809 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13810 set to true if any of the array-section-subscript could have length
13811 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13812 first array-section-subscript which is known not to have length
13813 of one. Given say:
13814 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13815 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13816 all are or may have length of 1, array-section-subscript [:2] is the
13817 first one known not to have length 1. For array-section-subscript
13818 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13819 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13820 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
13821 case though, as some lengths could be zero. */
13823 static tree
13824 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13825 bool &maybe_zero_len, unsigned int &first_non_one,
13826 enum c_omp_region_type ort)
13828 tree ret, low_bound, length, type;
13829 bool openacc = (ort & C_ORT_ACC) != 0;
13830 if (TREE_CODE (t) != OMP_ARRAY_SECTION)
13832 if (error_operand_p (t))
13833 return error_mark_node;
13834 c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
13835 ret = t;
13836 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13837 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13838 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13840 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13841 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13842 return error_mark_node;
13844 if (!ai.check_clause (c))
13845 return error_mark_node;
13846 else if (ai.component_access_p ()
13847 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13848 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13849 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13850 t = ai.get_root_term (true);
13851 else
13852 t = ai.unconverted_ref_origin ();
13853 if (t == error_mark_node)
13854 return error_mark_node;
13855 if (!VAR_P (t)
13856 && (ort == C_ORT_ACC || !EXPR_P (t))
13857 && TREE_CODE (t) != PARM_DECL)
13859 if (DECL_P (t))
13860 error_at (OMP_CLAUSE_LOCATION (c),
13861 "%qD is not a variable in %qs clause", t,
13862 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13863 else
13864 error_at (OMP_CLAUSE_LOCATION (c),
13865 "%qE is not a variable in %qs clause", t,
13866 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13867 return error_mark_node;
13869 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13870 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13871 && TYPE_ATOMIC (TREE_TYPE (t)))
13873 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13874 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13875 return error_mark_node;
13877 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13878 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13879 && VAR_P (t)
13880 && DECL_THREAD_LOCAL_P (t))
13882 error_at (OMP_CLAUSE_LOCATION (c),
13883 "%qD is threadprivate variable in %qs clause", t,
13884 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13885 return error_mark_node;
13887 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13888 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13889 && TYPE_ATOMIC (TREE_TYPE (t))
13890 && POINTER_TYPE_P (TREE_TYPE (t)))
13892 /* If the array section is pointer based and the pointer
13893 itself is _Atomic qualified, we need to atomically load
13894 the pointer. */
13895 c_expr expr;
13896 memset (&expr, 0, sizeof (expr));
13897 expr.value = ret;
13898 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13899 expr, false, false);
13900 ret = expr.value;
13902 return ret;
13905 ret = handle_omp_array_sections_1 (c, TREE_OPERAND (t, 0), types,
13906 maybe_zero_len, first_non_one, ort);
13907 if (ret == error_mark_node || ret == NULL_TREE)
13908 return ret;
13910 type = TREE_TYPE (ret);
13911 low_bound = TREE_OPERAND (t, 1);
13912 length = TREE_OPERAND (t, 2);
13914 if (low_bound == error_mark_node || length == error_mark_node)
13915 return error_mark_node;
13917 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13919 error_at (OMP_CLAUSE_LOCATION (c),
13920 "low bound %qE of array section does not have integral type",
13921 low_bound);
13922 return error_mark_node;
13924 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13926 error_at (OMP_CLAUSE_LOCATION (c),
13927 "length %qE of array section does not have integral type",
13928 length);
13929 return error_mark_node;
13931 if (low_bound
13932 && TREE_CODE (low_bound) == INTEGER_CST
13933 && TYPE_PRECISION (TREE_TYPE (low_bound))
13934 > TYPE_PRECISION (sizetype))
13935 low_bound = fold_convert (sizetype, low_bound);
13936 if (length
13937 && TREE_CODE (length) == INTEGER_CST
13938 && TYPE_PRECISION (TREE_TYPE (length))
13939 > TYPE_PRECISION (sizetype))
13940 length = fold_convert (sizetype, length);
13941 if (low_bound == NULL_TREE)
13942 low_bound = integer_zero_node;
13943 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13944 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13945 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13947 if (length != integer_one_node)
13949 error_at (OMP_CLAUSE_LOCATION (c),
13950 "expected single pointer in %qs clause",
13951 user_omp_clause_code_name (c, openacc));
13952 return error_mark_node;
13955 if (length != NULL_TREE)
13957 if (!integer_nonzerop (length))
13959 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13960 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13961 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13962 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13963 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13965 if (integer_zerop (length))
13967 error_at (OMP_CLAUSE_LOCATION (c),
13968 "zero length array section in %qs clause",
13969 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13970 return error_mark_node;
13973 else
13974 maybe_zero_len = true;
13976 if (first_non_one == types.length ()
13977 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13978 first_non_one++;
13980 if (TREE_CODE (type) == ARRAY_TYPE)
13982 if (length == NULL_TREE
13983 && (TYPE_DOMAIN (type) == NULL_TREE
13984 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13986 error_at (OMP_CLAUSE_LOCATION (c),
13987 "for unknown bound array type length expression must "
13988 "be specified");
13989 return error_mark_node;
13991 if (TREE_CODE (low_bound) == INTEGER_CST
13992 && tree_int_cst_sgn (low_bound) == -1)
13994 error_at (OMP_CLAUSE_LOCATION (c),
13995 "negative low bound in array section in %qs clause",
13996 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13997 return error_mark_node;
13999 if (length != NULL_TREE
14000 && TREE_CODE (length) == INTEGER_CST
14001 && tree_int_cst_sgn (length) == -1)
14003 error_at (OMP_CLAUSE_LOCATION (c),
14004 "negative length in array section in %qs clause",
14005 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14006 return error_mark_node;
14008 if (TYPE_DOMAIN (type)
14009 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
14010 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
14011 == INTEGER_CST)
14013 tree size
14014 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
14015 size = size_binop (PLUS_EXPR, size, size_one_node);
14016 if (TREE_CODE (low_bound) == INTEGER_CST)
14018 if (tree_int_cst_lt (size, low_bound))
14020 error_at (OMP_CLAUSE_LOCATION (c),
14021 "low bound %qE above array section size "
14022 "in %qs clause", low_bound,
14023 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14024 return error_mark_node;
14026 if (tree_int_cst_equal (size, low_bound))
14028 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
14029 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14030 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14031 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14032 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14034 error_at (OMP_CLAUSE_LOCATION (c),
14035 "zero length array section in %qs clause",
14036 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14037 return error_mark_node;
14039 maybe_zero_len = true;
14041 else if (length == NULL_TREE
14042 && first_non_one == types.length ()
14043 && tree_int_cst_equal
14044 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
14045 low_bound))
14046 first_non_one++;
14048 else if (length == NULL_TREE)
14050 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
14051 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
14052 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14053 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
14054 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
14055 maybe_zero_len = true;
14056 if (first_non_one == types.length ())
14057 first_non_one++;
14059 if (length && TREE_CODE (length) == INTEGER_CST)
14061 if (tree_int_cst_lt (size, length))
14063 error_at (OMP_CLAUSE_LOCATION (c),
14064 "length %qE above array section size "
14065 "in %qs clause", length,
14066 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14067 return error_mark_node;
14069 if (TREE_CODE (low_bound) == INTEGER_CST)
14071 tree lbpluslen
14072 = size_binop (PLUS_EXPR,
14073 fold_convert (sizetype, low_bound),
14074 fold_convert (sizetype, length));
14075 if (TREE_CODE (lbpluslen) == INTEGER_CST
14076 && tree_int_cst_lt (size, lbpluslen))
14078 error_at (OMP_CLAUSE_LOCATION (c),
14079 "high bound %qE above array section size "
14080 "in %qs clause", lbpluslen,
14081 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14082 return error_mark_node;
14087 else if (length == NULL_TREE)
14089 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
14090 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
14091 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14092 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
14093 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
14094 maybe_zero_len = true;
14095 if (first_non_one == types.length ())
14096 first_non_one++;
14099 /* For [lb:] we will need to evaluate lb more than once. */
14100 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
14102 tree lb = save_expr (low_bound);
14103 if (lb != low_bound)
14105 TREE_OPERAND (t, 1) = lb;
14106 low_bound = lb;
14110 else if (TREE_CODE (type) == POINTER_TYPE)
14112 if (length == NULL_TREE)
14114 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
14115 error_at (OMP_CLAUSE_LOCATION (c),
14116 "for array function parameter length expression "
14117 "must be specified");
14118 else
14119 error_at (OMP_CLAUSE_LOCATION (c),
14120 "for pointer type length expression must be specified");
14121 return error_mark_node;
14123 if (length != NULL_TREE
14124 && TREE_CODE (length) == INTEGER_CST
14125 && tree_int_cst_sgn (length) == -1)
14127 error_at (OMP_CLAUSE_LOCATION (c),
14128 "negative length in array section in %qs clause",
14129 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14130 return error_mark_node;
14132 /* If there is a pointer type anywhere but in the very first
14133 array-section-subscript, the array section could be non-contiguous. */
14134 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
14135 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
14136 && TREE_CODE (TREE_OPERAND (t, 0)) == OMP_ARRAY_SECTION)
14138 /* If any prior dimension has a non-one length, then deem this
14139 array section as non-contiguous. */
14140 for (tree d = TREE_OPERAND (t, 0);
14141 TREE_CODE (d) == OMP_ARRAY_SECTION;
14142 d = TREE_OPERAND (d, 0))
14144 tree d_length = TREE_OPERAND (d, 2);
14145 if (d_length == NULL_TREE || !integer_onep (d_length))
14147 error_at (OMP_CLAUSE_LOCATION (c),
14148 "array section is not contiguous in %qs clause",
14149 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14150 return error_mark_node;
14155 else
14157 error_at (OMP_CLAUSE_LOCATION (c),
14158 "%qE does not have pointer or array type", ret);
14159 return error_mark_node;
14161 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
14162 types.safe_push (TREE_TYPE (ret));
14163 /* We will need to evaluate lb more than once. */
14164 tree lb = save_expr (low_bound);
14165 if (lb != low_bound)
14167 TREE_OPERAND (t, 1) = lb;
14168 low_bound = lb;
14170 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
14171 return ret;
14174 /* Handle array sections for clause C. */
14176 static bool
14177 handle_omp_array_sections (tree &c, enum c_omp_region_type ort)
14179 bool maybe_zero_len = false;
14180 unsigned int first_non_one = 0;
14181 auto_vec<tree, 10> types;
14182 tree *tp = &OMP_CLAUSE_DECL (c);
14183 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14184 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
14185 && TREE_CODE (*tp) == TREE_LIST
14186 && TREE_PURPOSE (*tp)
14187 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
14188 tp = &TREE_VALUE (*tp);
14189 tree first = handle_omp_array_sections_1 (c, *tp, types,
14190 maybe_zero_len, first_non_one,
14191 ort);
14192 if (first == error_mark_node)
14193 return true;
14194 if (first == NULL_TREE)
14195 return false;
14196 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14197 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
14199 tree t = *tp;
14200 tree tem = NULL_TREE;
14201 /* Need to evaluate side effects in the length expressions
14202 if any. */
14203 while (TREE_CODE (t) == TREE_LIST)
14205 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
14207 if (tem == NULL_TREE)
14208 tem = TREE_VALUE (t);
14209 else
14210 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
14211 TREE_VALUE (t), tem);
14213 t = TREE_CHAIN (t);
14215 if (tem)
14216 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
14217 first = c_fully_fold (first, false, NULL, true);
14218 *tp = first;
14220 else
14222 unsigned int num = types.length (), i;
14223 tree t, side_effects = NULL_TREE, size = NULL_TREE;
14224 tree condition = NULL_TREE;
14226 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
14227 maybe_zero_len = true;
14229 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
14230 t = TREE_OPERAND (t, 0))
14232 tree low_bound = TREE_OPERAND (t, 1);
14233 tree length = TREE_OPERAND (t, 2);
14235 i--;
14236 if (low_bound
14237 && TREE_CODE (low_bound) == INTEGER_CST
14238 && TYPE_PRECISION (TREE_TYPE (low_bound))
14239 > TYPE_PRECISION (sizetype))
14240 low_bound = fold_convert (sizetype, low_bound);
14241 if (length
14242 && TREE_CODE (length) == INTEGER_CST
14243 && TYPE_PRECISION (TREE_TYPE (length))
14244 > TYPE_PRECISION (sizetype))
14245 length = fold_convert (sizetype, length);
14246 if (low_bound == NULL_TREE)
14247 low_bound = integer_zero_node;
14248 if (!maybe_zero_len && i > first_non_one)
14250 if (integer_nonzerop (low_bound))
14251 goto do_warn_noncontiguous;
14252 if (length != NULL_TREE
14253 && TREE_CODE (length) == INTEGER_CST
14254 && TYPE_DOMAIN (types[i])
14255 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
14256 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
14257 == INTEGER_CST)
14259 tree size;
14260 size = size_binop (PLUS_EXPR,
14261 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
14262 size_one_node);
14263 if (!tree_int_cst_equal (length, size))
14265 do_warn_noncontiguous:
14266 error_at (OMP_CLAUSE_LOCATION (c),
14267 "array section is not contiguous in %qs "
14268 "clause",
14269 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14270 return true;
14273 if (length != NULL_TREE
14274 && TREE_SIDE_EFFECTS (length))
14276 if (side_effects == NULL_TREE)
14277 side_effects = length;
14278 else
14279 side_effects = build2 (COMPOUND_EXPR,
14280 TREE_TYPE (side_effects),
14281 length, side_effects);
14284 else
14286 tree l;
14288 if (i > first_non_one
14289 && ((length && integer_nonzerop (length))
14290 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14291 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14292 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
14293 continue;
14294 if (length)
14295 l = fold_convert (sizetype, length);
14296 else
14298 l = size_binop (PLUS_EXPR,
14299 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
14300 size_one_node);
14301 l = size_binop (MINUS_EXPR, l,
14302 fold_convert (sizetype, low_bound));
14304 if (i > first_non_one)
14306 l = fold_build2 (NE_EXPR, boolean_type_node, l,
14307 size_zero_node);
14308 if (condition == NULL_TREE)
14309 condition = l;
14310 else
14311 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
14312 l, condition);
14314 else if (size == NULL_TREE)
14316 size = size_in_bytes (TREE_TYPE (types[i]));
14317 tree eltype = TREE_TYPE (types[num - 1]);
14318 while (TREE_CODE (eltype) == ARRAY_TYPE)
14319 eltype = TREE_TYPE (eltype);
14320 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14321 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14322 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14324 if (integer_zerop (size)
14325 || integer_zerop (size_in_bytes (eltype)))
14327 error_at (OMP_CLAUSE_LOCATION (c),
14328 "zero length array section in %qs clause",
14329 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14330 return error_mark_node;
14332 size = size_binop (EXACT_DIV_EXPR, size,
14333 size_in_bytes (eltype));
14335 size = size_binop (MULT_EXPR, size, l);
14336 if (condition)
14337 size = fold_build3 (COND_EXPR, sizetype, condition,
14338 size, size_zero_node);
14340 else
14341 size = size_binop (MULT_EXPR, size, l);
14344 if (side_effects)
14345 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
14346 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14347 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14348 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14350 size = size_binop (MINUS_EXPR, size, size_one_node);
14351 size = c_fully_fold (size, false, NULL);
14352 size = save_expr (size);
14353 tree index_type = build_index_type (size);
14354 tree eltype = TREE_TYPE (first);
14355 while (TREE_CODE (eltype) == ARRAY_TYPE)
14356 eltype = TREE_TYPE (eltype);
14357 tree type = build_array_type (eltype, index_type);
14358 tree ptype = build_pointer_type (eltype);
14359 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14360 t = build_fold_addr_expr (t);
14361 tree t2 = build_fold_addr_expr (first);
14362 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14363 ptrdiff_type_node, t2);
14364 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14365 ptrdiff_type_node, t2,
14366 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14367 ptrdiff_type_node, t));
14368 t2 = c_fully_fold (t2, false, NULL);
14369 if (tree_fits_shwi_p (t2))
14370 t = build2 (MEM_REF, type, t,
14371 build_int_cst (ptype, tree_to_shwi (t2)));
14372 else
14374 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
14375 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
14376 TREE_TYPE (t), t, t2);
14377 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
14379 OMP_CLAUSE_DECL (c) = t;
14380 return false;
14382 first = c_fully_fold (first, false, NULL);
14383 OMP_CLAUSE_DECL (c) = first;
14384 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
14385 return false;
14386 /* Don't set OMP_CLAUSE_SIZE for bare attach/detach clauses. */
14387 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14388 || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
14389 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
14390 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH))
14392 if (size)
14393 size = c_fully_fold (size, false, NULL);
14394 OMP_CLAUSE_SIZE (c) = size;
14397 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14398 return false;
14400 auto_vec<omp_addr_token *, 10> addr_tokens;
14402 if (!omp_parse_expr (addr_tokens, first))
14403 return true;
14405 c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
14407 tree nc = ai.expand_map_clause (c, first, addr_tokens, ort);
14408 if (nc != error_mark_node)
14410 using namespace omp_addr_tokenizer;
14412 if (ai.maybe_zero_length_array_section (c))
14413 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
14415 /* !!! If we're accessing a base decl via chained access
14416 methods (e.g. multiple indirections), duplicate clause
14417 detection won't work properly. Skip it in that case. */
14418 if ((addr_tokens[0]->type == STRUCTURE_BASE
14419 || addr_tokens[0]->type == ARRAY_BASE)
14420 && addr_tokens[0]->u.structure_base_kind == BASE_DECL
14421 && addr_tokens[1]->type == ACCESS_METHOD
14422 && omp_access_chain_p (addr_tokens, 1))
14423 c = nc;
14425 return false;
14428 return false;
14431 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
14432 an inline call. But, remap
14433 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
14434 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
14436 static tree
14437 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
14438 tree decl, tree placeholder)
14440 copy_body_data id;
14441 hash_map<tree, tree> decl_map;
14443 decl_map.put (omp_decl1, placeholder);
14444 decl_map.put (omp_decl2, decl);
14445 memset (&id, 0, sizeof (id));
14446 id.src_fn = DECL_CONTEXT (omp_decl1);
14447 id.dst_fn = current_function_decl;
14448 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
14449 id.decl_map = &decl_map;
14451 id.copy_decl = copy_decl_no_change;
14452 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
14453 id.transform_new_cfg = true;
14454 id.transform_return_to_modify = false;
14455 id.eh_lp_nr = 0;
14456 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
14457 return stmt;
14460 /* Helper function of c_finish_omp_clauses, called via walk_tree.
14461 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
14463 static tree
14464 c_find_omp_placeholder_r (tree *tp, int *, void *data)
14466 if (*tp == (tree) data)
14467 return *tp;
14468 return NULL_TREE;
14471 /* Similarly, but also walk aggregate fields. */
14473 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
14475 static tree
14476 c_find_omp_var_r (tree *tp, int *, void *data)
14478 if (*tp == ((struct c_find_omp_var_s *) data)->var)
14479 return *tp;
14480 if (RECORD_OR_UNION_TYPE_P (*tp))
14482 tree field;
14483 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
14485 for (field = TYPE_FIELDS (*tp); field;
14486 field = DECL_CHAIN (field))
14487 if (TREE_CODE (field) == FIELD_DECL)
14489 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
14490 c_find_omp_var_r, data, pset);
14491 if (ret)
14492 return ret;
14493 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
14494 if (ret)
14495 return ret;
14496 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
14497 pset);
14498 if (ret)
14499 return ret;
14500 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
14501 if (ret)
14502 return ret;
14505 else if (INTEGRAL_TYPE_P (*tp))
14506 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
14507 ((struct c_find_omp_var_s *) data)->pset);
14508 return NULL_TREE;
14511 /* Finish OpenMP iterators ITER. Return true if they are errorneous
14512 and clauses containing them should be removed. */
14514 static bool
14515 c_omp_finish_iterators (tree iter)
14517 bool ret = false;
14518 for (tree it = iter; it; it = TREE_CHAIN (it))
14520 tree var = TREE_VEC_ELT (it, 0);
14521 tree begin = TREE_VEC_ELT (it, 1);
14522 tree end = TREE_VEC_ELT (it, 2);
14523 tree step = TREE_VEC_ELT (it, 3);
14524 tree orig_step;
14525 tree type = TREE_TYPE (var);
14526 location_t loc = DECL_SOURCE_LOCATION (var);
14527 if (type == error_mark_node)
14529 ret = true;
14530 continue;
14532 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14534 error_at (loc, "iterator %qD has neither integral nor pointer type",
14535 var);
14536 ret = true;
14537 continue;
14539 else if (TYPE_ATOMIC (type))
14541 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
14542 ret = true;
14543 continue;
14545 else if (TYPE_READONLY (type))
14547 error_at (loc, "iterator %qD has const qualified type", var);
14548 ret = true;
14549 continue;
14551 else if (step == error_mark_node
14552 || TREE_TYPE (step) == error_mark_node)
14554 ret = true;
14555 continue;
14557 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
14559 error_at (EXPR_LOC_OR_LOC (step, loc),
14560 "iterator step with non-integral type");
14561 ret = true;
14562 continue;
14564 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
14565 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
14566 orig_step = save_expr (c_fully_fold (step, false, NULL));
14567 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
14568 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
14569 if (POINTER_TYPE_P (type))
14571 begin = save_expr (begin);
14572 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
14573 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
14574 fold_convert (sizetype, step),
14575 fold_convert (sizetype, begin));
14576 step = fold_convert (ssizetype, step);
14578 if (integer_zerop (step))
14580 error_at (loc, "iterator %qD has zero step", var);
14581 ret = true;
14582 continue;
14585 if (begin == error_mark_node
14586 || end == error_mark_node
14587 || step == error_mark_node
14588 || orig_step == error_mark_node)
14590 ret = true;
14591 continue;
14593 hash_set<tree> pset;
14594 tree it2;
14595 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
14597 tree var2 = TREE_VEC_ELT (it2, 0);
14598 tree begin2 = TREE_VEC_ELT (it2, 1);
14599 tree end2 = TREE_VEC_ELT (it2, 2);
14600 tree step2 = TREE_VEC_ELT (it2, 3);
14601 tree type2 = TREE_TYPE (var2);
14602 location_t loc2 = DECL_SOURCE_LOCATION (var2);
14603 struct c_find_omp_var_s data = { var, &pset };
14604 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
14606 error_at (loc2,
14607 "type of iterator %qD refers to outer iterator %qD",
14608 var2, var);
14609 break;
14611 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
14613 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
14614 "begin expression refers to outer iterator %qD", var);
14615 break;
14617 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
14619 error_at (EXPR_LOC_OR_LOC (end2, loc2),
14620 "end expression refers to outer iterator %qD", var);
14621 break;
14623 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
14625 error_at (EXPR_LOC_OR_LOC (step2, loc2),
14626 "step expression refers to outer iterator %qD", var);
14627 break;
14630 if (it2)
14632 ret = true;
14633 continue;
14635 TREE_VEC_ELT (it, 1) = begin;
14636 TREE_VEC_ELT (it, 2) = end;
14637 TREE_VEC_ELT (it, 3) = step;
14638 TREE_VEC_ELT (it, 4) = orig_step;
14640 return ret;
14643 /* Ensure that pointers are used in OpenACC attach and detach clauses.
14644 Return true if an error has been detected. */
14646 static bool
14647 c_oacc_check_attachments (tree c)
14649 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14650 return false;
14652 /* OpenACC attach / detach clauses must be pointers. */
14653 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14654 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14656 tree t = OMP_CLAUSE_DECL (c);
14658 while (TREE_CODE (t) == OMP_ARRAY_SECTION)
14659 t = TREE_OPERAND (t, 0);
14661 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14663 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
14664 user_omp_clause_code_name (c, true));
14665 return true;
14669 return false;
14672 /* For all elements of CLAUSES, validate them against their constraints.
14673 Remove any elements from the list that are invalid. */
14675 tree
14676 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
14678 bitmap_head generic_head, firstprivate_head, lastprivate_head;
14679 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
14680 bitmap_head oacc_reduction_head, is_on_device_head;
14681 tree c, t, type, *pc;
14682 tree simdlen = NULL_TREE, safelen = NULL_TREE;
14683 bool branch_seen = false;
14684 bool copyprivate_seen = false;
14685 bool mergeable_seen = false;
14686 tree *detach_seen = NULL;
14687 bool linear_variable_step_check = false;
14688 tree *nowait_clause = NULL;
14689 tree ordered_clause = NULL_TREE;
14690 tree schedule_clause = NULL_TREE;
14691 bool oacc_async = false;
14692 tree last_iterators = NULL_TREE;
14693 bool last_iterators_remove = false;
14694 tree *nogroup_seen = NULL;
14695 tree *order_clause = NULL;
14696 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
14697 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
14698 int reduction_seen = 0;
14699 bool allocate_seen = false;
14700 bool implicit_moved = false;
14701 bool target_in_reduction_seen = false;
14702 bool openacc = (ort & C_ORT_ACC) != 0;
14704 bitmap_obstack_initialize (NULL);
14705 bitmap_initialize (&generic_head, &bitmap_default_obstack);
14706 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
14707 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
14708 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
14709 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
14710 bitmap_initialize (&map_head, &bitmap_default_obstack);
14711 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
14712 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
14713 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
14714 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
14715 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
14716 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
14718 if (openacc)
14719 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14720 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
14722 oacc_async = true;
14723 break;
14726 tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
14728 for (pc = &clauses, c = clauses; c ; c = *pc)
14730 bool remove = false;
14731 bool need_complete = false;
14732 bool need_implicitly_determined = false;
14734 /* We've reached the end of a list of expanded nodes. Reset the group
14735 start pointer. */
14736 if (c == grp_sentinel)
14737 grp_start_p = NULL;
14739 switch (OMP_CLAUSE_CODE (c))
14741 case OMP_CLAUSE_SHARED:
14742 need_implicitly_determined = true;
14743 goto check_dup_generic;
14745 case OMP_CLAUSE_PRIVATE:
14746 need_complete = true;
14747 need_implicitly_determined = true;
14748 goto check_dup_generic;
14750 case OMP_CLAUSE_REDUCTION:
14751 if (reduction_seen == 0)
14752 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14753 else if (reduction_seen != -2
14754 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14755 ? -1 : 1))
14757 error_at (OMP_CLAUSE_LOCATION (c),
14758 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14759 "on the same construct");
14760 reduction_seen = -2;
14762 /* FALLTHRU */
14763 case OMP_CLAUSE_IN_REDUCTION:
14764 case OMP_CLAUSE_TASK_REDUCTION:
14765 need_implicitly_determined = true;
14766 t = OMP_CLAUSE_DECL (c);
14767 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
14769 if (handle_omp_array_sections (c, ort))
14771 remove = true;
14772 break;
14775 t = OMP_CLAUSE_DECL (c);
14776 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14777 && OMP_CLAUSE_REDUCTION_INSCAN (c))
14779 error_at (OMP_CLAUSE_LOCATION (c),
14780 "%<inscan%> %<reduction%> clause with array "
14781 "section");
14782 remove = true;
14783 break;
14786 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14787 if (t == error_mark_node)
14789 remove = true;
14790 break;
14792 if (oacc_async)
14793 c_mark_addressable (t);
14794 type = TREE_TYPE (t);
14795 if (TREE_CODE (t) == MEM_REF)
14796 type = TREE_TYPE (type);
14797 if (TREE_CODE (type) == ARRAY_TYPE)
14799 tree oatype = type;
14800 gcc_assert (TREE_CODE (t) != MEM_REF);
14801 while (TREE_CODE (type) == ARRAY_TYPE)
14802 type = TREE_TYPE (type);
14803 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14805 error_at (OMP_CLAUSE_LOCATION (c),
14806 "%qD in %<reduction%> clause is a zero size array",
14808 remove = true;
14809 break;
14811 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14812 TYPE_SIZE_UNIT (type));
14813 if (integer_zerop (size))
14815 error_at (OMP_CLAUSE_LOCATION (c),
14816 "%qD in %<reduction%> clause is a zero size array",
14818 remove = true;
14819 break;
14821 size = size_binop (MINUS_EXPR, size, size_one_node);
14822 size = save_expr (size);
14823 tree index_type = build_index_type (size);
14824 tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14825 index_type);
14826 atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14827 tree ptype = build_pointer_type (type);
14828 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14829 t = build_fold_addr_expr (t);
14830 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14831 OMP_CLAUSE_DECL (c) = t;
14833 if (TYPE_ATOMIC (type))
14835 error_at (OMP_CLAUSE_LOCATION (c),
14836 "%<_Atomic%> %qE in %<reduction%> clause", t);
14837 remove = true;
14838 break;
14840 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14841 || OMP_CLAUSE_REDUCTION_TASK (c))
14843 /* Disallow zero sized or potentially zero sized task
14844 reductions. */
14845 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14847 error_at (OMP_CLAUSE_LOCATION (c),
14848 "zero sized type %qT in %qs clause", type,
14849 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14850 remove = true;
14851 break;
14853 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14855 error_at (OMP_CLAUSE_LOCATION (c),
14856 "variable sized type %qT in %qs clause", type,
14857 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14858 remove = true;
14859 break;
14862 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14863 && (FLOAT_TYPE_P (type)
14864 || TREE_CODE (type) == COMPLEX_TYPE))
14866 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14867 const char *r_name = NULL;
14869 switch (r_code)
14871 case PLUS_EXPR:
14872 case MULT_EXPR:
14873 case MINUS_EXPR:
14874 case TRUTH_ANDIF_EXPR:
14875 case TRUTH_ORIF_EXPR:
14876 break;
14877 case MIN_EXPR:
14878 if (TREE_CODE (type) == COMPLEX_TYPE)
14879 r_name = "min";
14880 break;
14881 case MAX_EXPR:
14882 if (TREE_CODE (type) == COMPLEX_TYPE)
14883 r_name = "max";
14884 break;
14885 case BIT_AND_EXPR:
14886 r_name = "&";
14887 break;
14888 case BIT_XOR_EXPR:
14889 r_name = "^";
14890 break;
14891 case BIT_IOR_EXPR:
14892 r_name = "|";
14893 break;
14894 default:
14895 gcc_unreachable ();
14897 if (r_name)
14899 error_at (OMP_CLAUSE_LOCATION (c),
14900 "%qE has invalid type for %<reduction(%s)%>",
14901 t, r_name);
14902 remove = true;
14903 break;
14906 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14908 error_at (OMP_CLAUSE_LOCATION (c),
14909 "user defined reduction not found for %qE", t);
14910 remove = true;
14911 break;
14913 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14915 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14916 type = TYPE_MAIN_VARIANT (type);
14917 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14918 VAR_DECL, NULL_TREE, type);
14919 tree decl_placeholder = NULL_TREE;
14920 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14921 DECL_ARTIFICIAL (placeholder) = 1;
14922 DECL_IGNORED_P (placeholder) = 1;
14923 if (TREE_CODE (t) == MEM_REF)
14925 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14926 VAR_DECL, NULL_TREE, type);
14927 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14928 DECL_ARTIFICIAL (decl_placeholder) = 1;
14929 DECL_IGNORED_P (decl_placeholder) = 1;
14931 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14932 c_mark_addressable (placeholder);
14933 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14934 c_mark_addressable (decl_placeholder ? decl_placeholder
14935 : OMP_CLAUSE_DECL (c));
14936 OMP_CLAUSE_REDUCTION_MERGE (c)
14937 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14938 TREE_VEC_ELT (list, 0),
14939 TREE_VEC_ELT (list, 1),
14940 decl_placeholder ? decl_placeholder
14941 : OMP_CLAUSE_DECL (c), placeholder);
14942 OMP_CLAUSE_REDUCTION_MERGE (c)
14943 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14944 void_type_node, NULL_TREE,
14945 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14946 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14947 if (TREE_VEC_LENGTH (list) == 6)
14949 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14950 c_mark_addressable (decl_placeholder ? decl_placeholder
14951 : OMP_CLAUSE_DECL (c));
14952 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14953 c_mark_addressable (placeholder);
14954 tree init = TREE_VEC_ELT (list, 5);
14955 if (init == error_mark_node)
14956 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14957 OMP_CLAUSE_REDUCTION_INIT (c)
14958 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14959 TREE_VEC_ELT (list, 3),
14960 decl_placeholder ? decl_placeholder
14961 : OMP_CLAUSE_DECL (c), placeholder);
14962 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14964 tree v = decl_placeholder ? decl_placeholder : t;
14965 OMP_CLAUSE_REDUCTION_INIT (c)
14966 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14967 OMP_CLAUSE_REDUCTION_INIT (c));
14969 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14970 c_find_omp_placeholder_r,
14971 placeholder, NULL))
14972 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14974 else
14976 tree init;
14977 tree v = decl_placeholder ? decl_placeholder : t;
14978 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14979 init = build_constructor (TREE_TYPE (v), NULL);
14980 else
14981 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14982 OMP_CLAUSE_REDUCTION_INIT (c)
14983 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14985 OMP_CLAUSE_REDUCTION_INIT (c)
14986 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14987 void_type_node, NULL_TREE,
14988 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14989 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14991 if (TREE_CODE (t) == MEM_REF)
14993 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14994 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14995 != INTEGER_CST)
14997 sorry ("variable length element type in array "
14998 "%<reduction%> clause");
14999 remove = true;
15000 break;
15002 t = TREE_OPERAND (t, 0);
15003 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15004 t = TREE_OPERAND (t, 0);
15005 if (TREE_CODE (t) == ADDR_EXPR)
15006 t = TREE_OPERAND (t, 0);
15008 goto check_dup_generic_t;
15010 case OMP_CLAUSE_COPYPRIVATE:
15011 copyprivate_seen = true;
15012 if (nowait_clause)
15014 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
15015 "%<nowait%> clause must not be used together "
15016 "with %<copyprivate%>");
15017 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
15018 nowait_clause = NULL;
15020 goto check_dup_generic;
15022 case OMP_CLAUSE_COPYIN:
15023 t = OMP_CLAUSE_DECL (c);
15024 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
15026 error_at (OMP_CLAUSE_LOCATION (c),
15027 "%qE must be %<threadprivate%> for %<copyin%>", t);
15028 remove = true;
15029 break;
15031 goto check_dup_generic;
15033 case OMP_CLAUSE_LINEAR:
15034 if (ort != C_ORT_OMP_DECLARE_SIMD)
15035 need_implicitly_determined = true;
15036 t = OMP_CLAUSE_DECL (c);
15037 if (ort != C_ORT_OMP_DECLARE_SIMD
15038 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
15039 && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
15041 error_at (OMP_CLAUSE_LOCATION (c),
15042 "modifier should not be specified in %<linear%> "
15043 "clause on %<simd%> or %<for%> constructs when not "
15044 "using OpenMP 5.2 modifiers");
15045 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
15047 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
15048 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15050 error_at (OMP_CLAUSE_LOCATION (c),
15051 "linear clause applied to non-integral non-pointer "
15052 "variable with type %qT", TREE_TYPE (t));
15053 remove = true;
15054 break;
15056 if (TYPE_ATOMIC (TREE_TYPE (t)))
15058 error_at (OMP_CLAUSE_LOCATION (c),
15059 "%<_Atomic%> %qD in %<linear%> clause", t);
15060 remove = true;
15061 break;
15063 if (ort == C_ORT_OMP_DECLARE_SIMD)
15065 tree s = OMP_CLAUSE_LINEAR_STEP (c);
15066 if (TREE_CODE (s) == PARM_DECL)
15068 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
15069 /* map_head bitmap is used as uniform_head if
15070 declare_simd. */
15071 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
15072 linear_variable_step_check = true;
15073 goto check_dup_generic;
15075 if (TREE_CODE (s) != INTEGER_CST)
15077 error_at (OMP_CLAUSE_LOCATION (c),
15078 "%<linear%> clause step %qE is neither constant "
15079 "nor a parameter", s);
15080 remove = true;
15081 break;
15084 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
15086 tree s = OMP_CLAUSE_LINEAR_STEP (c);
15087 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
15088 OMP_CLAUSE_DECL (c), s);
15089 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
15090 sizetype, fold_convert (sizetype, s),
15091 fold_convert
15092 (sizetype, OMP_CLAUSE_DECL (c)));
15093 if (s == error_mark_node)
15094 s = size_one_node;
15095 OMP_CLAUSE_LINEAR_STEP (c) = s;
15097 else
15098 OMP_CLAUSE_LINEAR_STEP (c)
15099 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
15100 goto check_dup_generic;
15102 check_dup_generic:
15103 t = OMP_CLAUSE_DECL (c);
15104 check_dup_generic_t:
15105 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15107 error_at (OMP_CLAUSE_LOCATION (c),
15108 "%qE is not a variable in clause %qs", t,
15109 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15110 remove = true;
15112 else if ((openacc && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
15113 || (ort == C_ORT_OMP
15114 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15115 || (OMP_CLAUSE_CODE (c)
15116 == OMP_CLAUSE_USE_DEVICE_ADDR)))
15117 || (ort == C_ORT_OMP_TARGET
15118 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
15120 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15121 && (bitmap_bit_p (&generic_head, DECL_UID (t))
15122 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
15124 error_at (OMP_CLAUSE_LOCATION (c),
15125 "%qD appears more than once in data-sharing "
15126 "clauses", t);
15127 remove = true;
15128 break;
15130 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
15131 target_in_reduction_seen = true;
15132 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15134 error_at (OMP_CLAUSE_LOCATION (c),
15135 openacc
15136 ? "%qD appears more than once in reduction clauses"
15137 : "%qD appears more than once in data clauses",
15139 remove = true;
15141 else
15142 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
15144 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15145 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15146 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
15147 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15149 error_at (OMP_CLAUSE_LOCATION (c),
15150 "%qE appears more than once in data clauses", t);
15151 remove = true;
15153 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15154 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
15155 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15156 && bitmap_bit_p (&map_head, DECL_UID (t)))
15158 if (openacc)
15159 error_at (OMP_CLAUSE_LOCATION (c),
15160 "%qD appears more than once in data clauses", t);
15161 else
15162 error_at (OMP_CLAUSE_LOCATION (c),
15163 "%qD appears both in data and map clauses", t);
15164 remove = true;
15166 else
15167 bitmap_set_bit (&generic_head, DECL_UID (t));
15168 break;
15170 case OMP_CLAUSE_FIRSTPRIVATE:
15171 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
15173 move_implicit:
15174 implicit_moved = true;
15175 /* Move firstprivate and map clauses with
15176 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
15177 clauses chain. */
15178 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
15179 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
15180 while (*pc1)
15181 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
15182 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
15184 *pc3 = *pc1;
15185 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
15186 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
15188 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
15189 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
15191 *pc2 = *pc1;
15192 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
15193 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
15195 else
15196 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
15197 *pc3 = NULL;
15198 *pc2 = cl2;
15199 *pc1 = cl1;
15200 continue;
15202 t = OMP_CLAUSE_DECL (c);
15203 need_complete = true;
15204 need_implicitly_determined = true;
15205 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15207 error_at (OMP_CLAUSE_LOCATION (c),
15208 "%qE is not a variable in clause %<firstprivate%>", t);
15209 remove = true;
15211 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
15212 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
15213 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15214 remove = true;
15215 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15216 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15217 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15219 error_at (OMP_CLAUSE_LOCATION (c),
15220 "%qE appears more than once in data clauses", t);
15221 remove = true;
15223 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15224 || bitmap_bit_p (&map_field_head, DECL_UID (t)))
15226 if (openacc)
15227 error_at (OMP_CLAUSE_LOCATION (c),
15228 "%qD appears more than once in data clauses", t);
15229 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
15230 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
15231 /* Silently drop the clause. */;
15232 else
15233 error_at (OMP_CLAUSE_LOCATION (c),
15234 "%qD appears both in data and map clauses", t);
15235 remove = true;
15237 else
15238 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
15239 break;
15241 case OMP_CLAUSE_LASTPRIVATE:
15242 t = OMP_CLAUSE_DECL (c);
15243 need_complete = true;
15244 need_implicitly_determined = true;
15245 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15247 error_at (OMP_CLAUSE_LOCATION (c),
15248 "%qE is not a variable in clause %<lastprivate%>", t);
15249 remove = true;
15251 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15252 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
15254 error_at (OMP_CLAUSE_LOCATION (c),
15255 "%qE appears more than once in data clauses", t);
15256 remove = true;
15258 else
15259 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
15260 break;
15262 case OMP_CLAUSE_ALIGNED:
15263 t = OMP_CLAUSE_DECL (c);
15264 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15266 error_at (OMP_CLAUSE_LOCATION (c),
15267 "%qE is not a variable in %<aligned%> clause", t);
15268 remove = true;
15270 else if (!POINTER_TYPE_P (TREE_TYPE (t))
15271 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15273 error_at (OMP_CLAUSE_LOCATION (c),
15274 "%qE in %<aligned%> clause is neither a pointer nor "
15275 "an array", t);
15276 remove = true;
15278 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15280 error_at (OMP_CLAUSE_LOCATION (c),
15281 "%<_Atomic%> %qD in %<aligned%> clause", t);
15282 remove = true;
15283 break;
15285 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
15287 error_at (OMP_CLAUSE_LOCATION (c),
15288 "%qE appears more than once in %<aligned%> clauses",
15290 remove = true;
15292 else
15293 bitmap_set_bit (&aligned_head, DECL_UID (t));
15294 break;
15296 case OMP_CLAUSE_NONTEMPORAL:
15297 t = OMP_CLAUSE_DECL (c);
15298 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15300 error_at (OMP_CLAUSE_LOCATION (c),
15301 "%qE is not a variable in %<nontemporal%> clause", t);
15302 remove = true;
15304 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15306 error_at (OMP_CLAUSE_LOCATION (c),
15307 "%qE appears more than once in %<nontemporal%> "
15308 "clauses", t);
15309 remove = true;
15311 else
15312 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
15313 break;
15315 case OMP_CLAUSE_ALLOCATE:
15316 t = OMP_CLAUSE_DECL (c);
15317 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15319 error_at (OMP_CLAUSE_LOCATION (c),
15320 "%qE is not a variable in %<allocate%> clause", t);
15321 remove = true;
15323 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
15325 warning_at (OMP_CLAUSE_LOCATION (c), 0,
15326 "%qE appears more than once in %<allocate%> clauses",
15328 remove = true;
15330 else
15332 bitmap_set_bit (&aligned_head, DECL_UID (t));
15333 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
15334 allocate_seen = true;
15336 break;
15338 case OMP_CLAUSE_DOACROSS:
15339 t = OMP_CLAUSE_DECL (c);
15340 if (t == NULL_TREE)
15341 break;
15342 if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
15344 gcc_assert (TREE_CODE (t) == TREE_LIST);
15345 for (; t; t = TREE_CHAIN (t))
15347 tree decl = TREE_VALUE (t);
15348 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
15350 tree offset = TREE_PURPOSE (t);
15351 bool neg = wi::neg_p (wi::to_wide (offset));
15352 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
15353 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
15354 neg ? MINUS_EXPR : PLUS_EXPR,
15355 decl, offset);
15356 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
15357 sizetype,
15358 fold_convert (sizetype, t2),
15359 fold_convert (sizetype, decl));
15360 if (t2 == error_mark_node)
15362 remove = true;
15363 break;
15365 TREE_PURPOSE (t) = t2;
15368 break;
15370 gcc_unreachable ();
15371 case OMP_CLAUSE_DEPEND:
15372 case OMP_CLAUSE_AFFINITY:
15373 t = OMP_CLAUSE_DECL (c);
15374 if (TREE_CODE (t) == TREE_LIST
15375 && TREE_PURPOSE (t)
15376 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
15378 if (TREE_PURPOSE (t) != last_iterators)
15379 last_iterators_remove
15380 = c_omp_finish_iterators (TREE_PURPOSE (t));
15381 last_iterators = TREE_PURPOSE (t);
15382 t = TREE_VALUE (t);
15383 if (last_iterators_remove)
15384 t = error_mark_node;
15386 else
15387 last_iterators = NULL_TREE;
15388 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
15390 if (handle_omp_array_sections (c, ort))
15391 remove = true;
15392 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15393 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
15395 error_at (OMP_CLAUSE_LOCATION (c),
15396 "%<depend%> clause with %<depobj%> dependence "
15397 "type on array section");
15398 remove = true;
15400 break;
15402 if (t == error_mark_node)
15403 remove = true;
15404 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15405 && t == ridpointers[RID_OMP_ALL_MEMORY])
15407 if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
15408 && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
15410 error_at (OMP_CLAUSE_LOCATION (c),
15411 "%<omp_all_memory%> used with %<depend%> kind "
15412 "other than %<out%> or %<inout%>");
15413 remove = true;
15416 else if (!lvalue_p (t))
15418 error_at (OMP_CLAUSE_LOCATION (c),
15419 "%qE is not lvalue expression nor array section in "
15420 "%qs clause", t,
15421 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15422 remove = true;
15424 else if (TREE_CODE (t) == COMPONENT_REF
15425 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
15427 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15428 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
15429 error_at (OMP_CLAUSE_LOCATION (c),
15430 "bit-field %qE in %qs clause", t,
15431 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15432 remove = true;
15434 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15435 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
15437 if (!c_omp_depend_t_p (TREE_TYPE (t)))
15439 error_at (OMP_CLAUSE_LOCATION (c),
15440 "%qE does not have %<omp_depend_t%> type in "
15441 "%<depend%> clause with %<depobj%> dependence "
15442 "type", t);
15443 remove = true;
15446 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15447 && c_omp_depend_t_p (TREE_TYPE (t)))
15449 error_at (OMP_CLAUSE_LOCATION (c),
15450 "%qE should not have %<omp_depend_t%> type in "
15451 "%<depend%> clause with dependence type other than "
15452 "%<depobj%>", t);
15453 remove = true;
15455 if (!remove)
15457 if (t == ridpointers[RID_OMP_ALL_MEMORY])
15458 t = null_pointer_node;
15459 else
15461 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
15462 ADDR_EXPR, t, false);
15463 if (addr == error_mark_node)
15465 remove = true;
15466 break;
15468 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
15469 RO_UNARY_STAR);
15470 if (t == error_mark_node)
15472 remove = true;
15473 break;
15476 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
15477 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
15478 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
15479 == TREE_VEC))
15480 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
15481 else
15482 OMP_CLAUSE_DECL (c) = t;
15484 break;
15486 case OMP_CLAUSE_MAP:
15487 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
15488 goto move_implicit;
15489 /* FALLTHRU */
15490 case OMP_CLAUSE_TO:
15491 case OMP_CLAUSE_FROM:
15492 case OMP_CLAUSE__CACHE_:
15494 using namespace omp_addr_tokenizer;
15495 auto_vec<omp_addr_token *, 10> addr_tokens;
15497 t = OMP_CLAUSE_DECL (c);
15498 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
15500 grp_start_p = pc;
15501 grp_sentinel = OMP_CLAUSE_CHAIN (c);
15503 if (handle_omp_array_sections (c, ort))
15504 remove = true;
15505 else
15507 t = OMP_CLAUSE_DECL (c);
15508 if (!omp_mappable_type (TREE_TYPE (t)))
15510 error_at (OMP_CLAUSE_LOCATION (c),
15511 "array section does not have mappable type "
15512 "in %qs clause",
15513 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15514 remove = true;
15516 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15518 error_at (OMP_CLAUSE_LOCATION (c),
15519 "%<_Atomic%> %qE in %qs clause", t,
15520 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15521 remove = true;
15523 while (TREE_CODE (t) == ARRAY_REF)
15524 t = TREE_OPERAND (t, 0);
15526 c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
15528 if (!omp_parse_expr (addr_tokens, t))
15530 sorry_at (OMP_CLAUSE_LOCATION (c),
15531 "unsupported map expression %qE",
15532 OMP_CLAUSE_DECL (c));
15533 remove = true;
15534 break;
15537 /* This check is to determine if this will be the only map
15538 node created for this clause. Otherwise, we'll check
15539 the following FIRSTPRIVATE_POINTER or ATTACH_DETACH
15540 node on the next iteration(s) of the loop. */
15541 if (addr_tokens.length () >= 4
15542 && addr_tokens[0]->type == STRUCTURE_BASE
15543 && addr_tokens[0]->u.structure_base_kind == BASE_DECL
15544 && addr_tokens[1]->type == ACCESS_METHOD
15545 && addr_tokens[2]->type == COMPONENT_SELECTOR
15546 && addr_tokens[3]->type == ACCESS_METHOD
15547 && (addr_tokens[3]->u.access_kind == ACCESS_DIRECT
15548 || (addr_tokens[3]->u.access_kind
15549 == ACCESS_INDEXED_ARRAY)))
15551 tree rt = addr_tokens[1]->expr;
15553 gcc_assert (DECL_P (rt));
15555 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15556 && OMP_CLAUSE_MAP_IMPLICIT (c)
15557 && (bitmap_bit_p (&map_head, DECL_UID (rt))
15558 || bitmap_bit_p (&map_field_head, DECL_UID (rt))
15559 || bitmap_bit_p (&map_firstprivate_head,
15560 DECL_UID (rt))))
15562 remove = true;
15563 break;
15565 if (bitmap_bit_p (&map_field_head, DECL_UID (rt)))
15566 break;
15567 if (bitmap_bit_p (&map_head, DECL_UID (rt)))
15569 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15570 error_at (OMP_CLAUSE_LOCATION (c),
15571 "%qD appears more than once in motion "
15572 "clauses", rt);
15573 else if (openacc)
15574 error_at (OMP_CLAUSE_LOCATION (c),
15575 "%qD appears more than once in data "
15576 "clauses", rt);
15577 else
15578 error_at (OMP_CLAUSE_LOCATION (c),
15579 "%qD appears more than once in map "
15580 "clauses", rt);
15581 remove = true;
15583 else
15585 bitmap_set_bit (&map_head, DECL_UID (rt));
15586 bitmap_set_bit (&map_field_head, DECL_UID (rt));
15590 if (c_oacc_check_attachments (c))
15591 remove = true;
15592 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15593 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15594 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
15595 && !OMP_CLAUSE_SIZE (c))
15596 /* In this case, we have a single array element which is a
15597 pointer, and we already set OMP_CLAUSE_SIZE in
15598 handle_omp_array_sections above. For attach/detach
15599 clauses, reset the OMP_CLAUSE_SIZE (representing a bias)
15600 to zero here. */
15601 OMP_CLAUSE_SIZE (c) = size_zero_node;
15602 break;
15604 else if (!omp_parse_expr (addr_tokens, t))
15606 sorry_at (OMP_CLAUSE_LOCATION (c),
15607 "unsupported map expression %qE",
15608 OMP_CLAUSE_DECL (c));
15609 remove = true;
15610 break;
15612 if (t == error_mark_node)
15614 remove = true;
15615 break;
15617 /* OpenACC attach / detach clauses must be pointers. */
15618 if (c_oacc_check_attachments (c))
15620 remove = true;
15621 break;
15623 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15624 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15625 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
15626 && !OMP_CLAUSE_SIZE (c))
15627 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
15628 bias) to zero here, so it is not set erroneously to the pointer
15629 size later on in gimplify.cc. */
15630 OMP_CLAUSE_SIZE (c) = size_zero_node;
15632 c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
15634 if (!ai.check_clause (c))
15636 remove = true;
15637 break;
15640 if (!ai.map_supported_p ())
15642 sorry_at (OMP_CLAUSE_LOCATION (c),
15643 "unsupported map expression %qE",
15644 OMP_CLAUSE_DECL (c));
15645 remove = true;
15646 break;
15649 gcc_assert ((addr_tokens[0]->type == ARRAY_BASE
15650 || addr_tokens[0]->type == STRUCTURE_BASE)
15651 && addr_tokens[1]->type == ACCESS_METHOD);
15653 t = addr_tokens[1]->expr;
15655 if (addr_tokens[0]->u.structure_base_kind != BASE_DECL)
15656 goto skip_decl_checks;
15658 /* For OpenMP, we can access a struct "t" and "t.d" on the same
15659 mapping. OpenACC allows multiple fields of the same structure
15660 to be written. */
15661 if (addr_tokens[0]->type == STRUCTURE_BASE
15662 && (bitmap_bit_p (&map_field_head, DECL_UID (t))
15663 || (!openacc && bitmap_bit_p (&map_head, DECL_UID (t)))))
15664 goto skip_decl_checks;
15666 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15668 if (ort != C_ORT_ACC && EXPR_P (t))
15669 break;
15671 error_at (OMP_CLAUSE_LOCATION (c),
15672 "%qE is not a variable in %qs clause", t,
15673 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15674 remove = true;
15676 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15678 error_at (OMP_CLAUSE_LOCATION (c),
15679 "%qD is threadprivate variable in %qs clause", t,
15680 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15681 remove = true;
15683 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15684 || (OMP_CLAUSE_MAP_KIND (c)
15685 != GOMP_MAP_FIRSTPRIVATE_POINTER))
15686 && !c_mark_addressable (t))
15687 remove = true;
15688 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15689 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
15690 || (OMP_CLAUSE_MAP_KIND (c)
15691 == GOMP_MAP_FIRSTPRIVATE_POINTER)
15692 || (OMP_CLAUSE_MAP_KIND (c)
15693 == GOMP_MAP_FORCE_DEVICEPTR)))
15694 && t == OMP_CLAUSE_DECL (c)
15695 && !omp_mappable_type (TREE_TYPE (t)))
15697 error_at (OMP_CLAUSE_LOCATION (c),
15698 "%qD does not have a mappable type in %qs clause", t,
15699 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15700 remove = true;
15702 else if (TREE_TYPE (t) == error_mark_node)
15703 remove = true;
15704 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15706 error_at (OMP_CLAUSE_LOCATION (c),
15707 "%<_Atomic%> %qE in %qs clause", t,
15708 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15709 remove = true;
15711 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15712 && OMP_CLAUSE_MAP_IMPLICIT (c)
15713 && (bitmap_bit_p (&map_head, DECL_UID (t))
15714 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15715 || bitmap_bit_p (&map_firstprivate_head,
15716 DECL_UID (t))))
15717 remove = true;
15718 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15719 && (OMP_CLAUSE_MAP_KIND (c)
15720 == GOMP_MAP_FIRSTPRIVATE_POINTER))
15722 if (bitmap_bit_p (&generic_head, DECL_UID (t))
15723 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15724 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15726 error_at (OMP_CLAUSE_LOCATION (c),
15727 "%qD appears more than once in data clauses", t);
15728 remove = true;
15730 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15731 && !bitmap_bit_p (&map_field_head, DECL_UID (t))
15732 && openacc)
15734 error_at (OMP_CLAUSE_LOCATION (c),
15735 "%qD appears more than once in data clauses", t);
15736 remove = true;
15738 else
15739 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
15741 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15742 && !bitmap_bit_p (&map_field_head, DECL_UID (t))
15743 && ort != C_ORT_OMP
15744 && ort != C_ORT_OMP_EXIT_DATA)
15746 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15747 error_at (OMP_CLAUSE_LOCATION (c),
15748 "%qD appears more than once in motion clauses", t);
15749 else if (openacc)
15750 error_at (OMP_CLAUSE_LOCATION (c),
15751 "%qD appears more than once in data clauses", t);
15752 else
15753 error_at (OMP_CLAUSE_LOCATION (c),
15754 "%qD appears more than once in map clauses", t);
15755 remove = true;
15757 else if (openacc && bitmap_bit_p (&generic_head, DECL_UID (t)))
15759 error_at (OMP_CLAUSE_LOCATION (c),
15760 "%qD appears more than once in data clauses", t);
15761 remove = true;
15763 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15764 || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
15766 if (openacc)
15767 error_at (OMP_CLAUSE_LOCATION (c),
15768 "%qD appears more than once in data clauses", t);
15769 else
15770 error_at (OMP_CLAUSE_LOCATION (c),
15771 "%qD appears both in data and map clauses", t);
15772 remove = true;
15774 else if (!omp_access_chain_p (addr_tokens, 1))
15776 bitmap_set_bit (&map_head, DECL_UID (t));
15777 if (t != OMP_CLAUSE_DECL (c)
15778 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
15779 bitmap_set_bit (&map_field_head, DECL_UID (t));
15782 skip_decl_checks:
15783 /* If we call omp_expand_map_clause in handle_omp_array_sections,
15784 the containing loop (here) iterates through the new nodes
15785 created by that expansion. Avoid expanding those again (just
15786 by checking the node type). */
15787 if (!remove
15788 && ort != C_ORT_DECLARE_SIMD
15789 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15790 || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
15791 && (OMP_CLAUSE_MAP_KIND (c)
15792 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
15793 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
15794 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
15795 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
15796 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH)))
15798 grp_start_p = pc;
15799 grp_sentinel = OMP_CLAUSE_CHAIN (c);
15800 tree nc = ai.expand_map_clause (c, OMP_CLAUSE_DECL (c),
15801 addr_tokens, ort);
15802 if (nc != error_mark_node)
15803 c = nc;
15806 break;
15808 case OMP_CLAUSE_ENTER:
15809 case OMP_CLAUSE_LINK:
15810 t = OMP_CLAUSE_DECL (c);
15811 const char *cname;
15812 cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
15813 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
15814 && OMP_CLAUSE_ENTER_TO (c))
15815 cname = "to";
15816 if (TREE_CODE (t) == FUNCTION_DECL
15817 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15819 else if (!VAR_P (t))
15821 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15822 error_at (OMP_CLAUSE_LOCATION (c),
15823 "%qE is neither a variable nor a function name in "
15824 "clause %qs", t, cname);
15825 else
15826 error_at (OMP_CLAUSE_LOCATION (c),
15827 "%qE is not a variable in clause %qs", t, cname);
15828 remove = true;
15830 else if (DECL_THREAD_LOCAL_P (t))
15832 error_at (OMP_CLAUSE_LOCATION (c),
15833 "%qD is threadprivate variable in %qs clause", t,
15834 cname);
15835 remove = true;
15837 else if (!omp_mappable_type (TREE_TYPE (t)))
15839 error_at (OMP_CLAUSE_LOCATION (c),
15840 "%qD does not have a mappable type in %qs clause", t,
15841 cname);
15842 remove = true;
15844 if (remove)
15845 break;
15846 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15848 error_at (OMP_CLAUSE_LOCATION (c),
15849 "%qE appears more than once on the same "
15850 "%<declare target%> directive", t);
15851 remove = true;
15853 else
15854 bitmap_set_bit (&generic_head, DECL_UID (t));
15855 break;
15857 case OMP_CLAUSE_UNIFORM:
15858 t = OMP_CLAUSE_DECL (c);
15859 if (TREE_CODE (t) != PARM_DECL)
15861 if (DECL_P (t))
15862 error_at (OMP_CLAUSE_LOCATION (c),
15863 "%qD is not an argument in %<uniform%> clause", t);
15864 else
15865 error_at (OMP_CLAUSE_LOCATION (c),
15866 "%qE is not an argument in %<uniform%> clause", t);
15867 remove = true;
15868 break;
15870 /* map_head bitmap is used as uniform_head if declare_simd. */
15871 bitmap_set_bit (&map_head, DECL_UID (t));
15872 goto check_dup_generic;
15874 case OMP_CLAUSE_IS_DEVICE_PTR:
15875 case OMP_CLAUSE_USE_DEVICE_PTR:
15876 t = OMP_CLAUSE_DECL (c);
15877 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15878 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15879 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15881 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15882 && !openacc)
15884 error_at (OMP_CLAUSE_LOCATION (c),
15885 "%qs variable is not a pointer",
15886 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15887 remove = true;
15889 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15891 error_at (OMP_CLAUSE_LOCATION (c),
15892 "%qs variable is neither a pointer nor an array",
15893 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15894 remove = true;
15897 goto check_dup_generic;
15899 case OMP_CLAUSE_HAS_DEVICE_ADDR:
15900 t = OMP_CLAUSE_DECL (c);
15901 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
15903 if (handle_omp_array_sections (c, ort))
15904 remove = true;
15905 else
15907 t = OMP_CLAUSE_DECL (c);
15908 while (TREE_CODE (t) == ARRAY_REF)
15909 t = TREE_OPERAND (t, 0);
15912 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15913 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15914 c_mark_addressable (t);
15915 goto check_dup_generic_t;
15917 case OMP_CLAUSE_USE_DEVICE_ADDR:
15918 t = OMP_CLAUSE_DECL (c);
15919 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15920 c_mark_addressable (t);
15921 goto check_dup_generic;
15923 case OMP_CLAUSE_NOWAIT:
15924 if (copyprivate_seen)
15926 error_at (OMP_CLAUSE_LOCATION (c),
15927 "%<nowait%> clause must not be used together "
15928 "with %<copyprivate%>");
15929 remove = true;
15930 break;
15932 nowait_clause = pc;
15933 pc = &OMP_CLAUSE_CHAIN (c);
15934 continue;
15936 case OMP_CLAUSE_ORDER:
15937 if (ordered_clause)
15939 error_at (OMP_CLAUSE_LOCATION (c),
15940 "%<order%> clause must not be used together "
15941 "with %<ordered%>");
15942 remove = true;
15943 break;
15945 else if (order_clause)
15947 /* Silently remove duplicates. */
15948 remove = true;
15949 break;
15951 order_clause = pc;
15952 pc = &OMP_CLAUSE_CHAIN (c);
15953 continue;
15955 case OMP_CLAUSE_DETACH:
15956 t = OMP_CLAUSE_DECL (c);
15957 if (detach_seen)
15959 error_at (OMP_CLAUSE_LOCATION (c),
15960 "too many %qs clauses on a task construct",
15961 "detach");
15962 remove = true;
15963 break;
15965 detach_seen = pc;
15966 pc = &OMP_CLAUSE_CHAIN (c);
15967 c_mark_addressable (t);
15968 continue;
15970 case OMP_CLAUSE_IF:
15971 case OMP_CLAUSE_SELF:
15972 case OMP_CLAUSE_NUM_THREADS:
15973 case OMP_CLAUSE_NUM_TEAMS:
15974 case OMP_CLAUSE_THREAD_LIMIT:
15975 case OMP_CLAUSE_DEFAULT:
15976 case OMP_CLAUSE_UNTIED:
15977 case OMP_CLAUSE_COLLAPSE:
15978 case OMP_CLAUSE_FINAL:
15979 case OMP_CLAUSE_DEVICE:
15980 case OMP_CLAUSE_DIST_SCHEDULE:
15981 case OMP_CLAUSE_PARALLEL:
15982 case OMP_CLAUSE_FOR:
15983 case OMP_CLAUSE_SECTIONS:
15984 case OMP_CLAUSE_TASKGROUP:
15985 case OMP_CLAUSE_PROC_BIND:
15986 case OMP_CLAUSE_DEVICE_TYPE:
15987 case OMP_CLAUSE_PRIORITY:
15988 case OMP_CLAUSE_GRAINSIZE:
15989 case OMP_CLAUSE_NUM_TASKS:
15990 case OMP_CLAUSE_THREADS:
15991 case OMP_CLAUSE_SIMD:
15992 case OMP_CLAUSE_HINT:
15993 case OMP_CLAUSE_FILTER:
15994 case OMP_CLAUSE_DEFAULTMAP:
15995 case OMP_CLAUSE_BIND:
15996 case OMP_CLAUSE_NUM_GANGS:
15997 case OMP_CLAUSE_NUM_WORKERS:
15998 case OMP_CLAUSE_VECTOR_LENGTH:
15999 case OMP_CLAUSE_ASYNC:
16000 case OMP_CLAUSE_WAIT:
16001 case OMP_CLAUSE_AUTO:
16002 case OMP_CLAUSE_INDEPENDENT:
16003 case OMP_CLAUSE_SEQ:
16004 case OMP_CLAUSE_GANG:
16005 case OMP_CLAUSE_WORKER:
16006 case OMP_CLAUSE_VECTOR:
16007 case OMP_CLAUSE_TILE:
16008 case OMP_CLAUSE_IF_PRESENT:
16009 case OMP_CLAUSE_FINALIZE:
16010 case OMP_CLAUSE_NOHOST:
16011 case OMP_CLAUSE_INDIRECT:
16012 pc = &OMP_CLAUSE_CHAIN (c);
16013 continue;
16015 case OMP_CLAUSE_MERGEABLE:
16016 mergeable_seen = true;
16017 pc = &OMP_CLAUSE_CHAIN (c);
16018 continue;
16020 case OMP_CLAUSE_NOGROUP:
16021 nogroup_seen = pc;
16022 pc = &OMP_CLAUSE_CHAIN (c);
16023 continue;
16025 case OMP_CLAUSE_SCHEDULE:
16026 schedule_clause = c;
16027 pc = &OMP_CLAUSE_CHAIN (c);
16028 continue;
16030 case OMP_CLAUSE_ORDERED:
16031 ordered_clause = c;
16032 if (order_clause)
16034 error_at (OMP_CLAUSE_LOCATION (*order_clause),
16035 "%<order%> clause must not be used together "
16036 "with %<ordered%>");
16037 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
16038 order_clause = NULL;
16040 pc = &OMP_CLAUSE_CHAIN (c);
16041 continue;
16043 case OMP_CLAUSE_SAFELEN:
16044 safelen = c;
16045 pc = &OMP_CLAUSE_CHAIN (c);
16046 continue;
16047 case OMP_CLAUSE_SIMDLEN:
16048 simdlen = c;
16049 pc = &OMP_CLAUSE_CHAIN (c);
16050 continue;
16052 case OMP_CLAUSE_INBRANCH:
16053 case OMP_CLAUSE_NOTINBRANCH:
16054 if (branch_seen)
16056 error_at (OMP_CLAUSE_LOCATION (c),
16057 "%<inbranch%> clause is incompatible with "
16058 "%<notinbranch%>");
16059 remove = true;
16060 break;
16062 branch_seen = true;
16063 pc = &OMP_CLAUSE_CHAIN (c);
16064 continue;
16066 case OMP_CLAUSE_INCLUSIVE:
16067 case OMP_CLAUSE_EXCLUSIVE:
16068 need_complete = true;
16069 need_implicitly_determined = true;
16070 t = OMP_CLAUSE_DECL (c);
16071 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
16073 error_at (OMP_CLAUSE_LOCATION (c),
16074 "%qE is not a variable in clause %qs", t,
16075 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16076 remove = true;
16078 break;
16080 default:
16081 gcc_unreachable ();
16084 if (!remove)
16086 t = OMP_CLAUSE_DECL (c);
16088 if (need_complete)
16090 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
16091 if (t == error_mark_node)
16092 remove = true;
16095 if (need_implicitly_determined)
16097 const char *share_name = NULL;
16099 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
16100 share_name = "threadprivate";
16101 else switch (c_omp_predetermined_sharing (t))
16103 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
16104 break;
16105 case OMP_CLAUSE_DEFAULT_SHARED:
16106 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
16107 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
16108 && c_omp_predefined_variable (t))
16109 /* The __func__ variable and similar function-local
16110 predefined variables may be listed in a shared or
16111 firstprivate clause. */
16112 break;
16113 share_name = "shared";
16114 break;
16115 case OMP_CLAUSE_DEFAULT_PRIVATE:
16116 share_name = "private";
16117 break;
16118 default:
16119 gcc_unreachable ();
16121 if (share_name)
16123 error_at (OMP_CLAUSE_LOCATION (c),
16124 "%qE is predetermined %qs for %qs",
16125 t, share_name,
16126 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16127 remove = true;
16129 else if (TREE_READONLY (t)
16130 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
16131 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
16133 error_at (OMP_CLAUSE_LOCATION (c),
16134 "%<const%> qualified %qE may appear only in "
16135 "%<shared%> or %<firstprivate%> clauses", t);
16136 remove = true;
16141 if (remove)
16143 if (grp_start_p)
16145 /* If we found a clause to remove, we want to remove the whole
16146 expanded group, otherwise gimplify
16147 (omp_resolve_clause_dependencies) can get confused. */
16148 *grp_start_p = grp_sentinel;
16149 pc = grp_start_p;
16150 grp_start_p = NULL;
16152 else
16153 *pc = OMP_CLAUSE_CHAIN (c);
16155 else
16156 pc = &OMP_CLAUSE_CHAIN (c);
16159 if (simdlen
16160 && safelen
16161 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
16162 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
16164 error_at (OMP_CLAUSE_LOCATION (simdlen),
16165 "%<simdlen%> clause value is bigger than "
16166 "%<safelen%> clause value");
16167 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
16168 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
16171 if (ordered_clause
16172 && schedule_clause
16173 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16174 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
16176 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
16177 "%<nonmonotonic%> schedule modifier specified together "
16178 "with %<ordered%> clause");
16179 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16180 = (enum omp_clause_schedule_kind)
16181 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16182 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
16185 if (reduction_seen < 0 && ordered_clause)
16187 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
16188 "%qs clause specified together with %<inscan%> "
16189 "%<reduction%> clause", "ordered");
16190 reduction_seen = -2;
16193 if (reduction_seen < 0 && schedule_clause)
16195 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
16196 "%qs clause specified together with %<inscan%> "
16197 "%<reduction%> clause", "schedule");
16198 reduction_seen = -2;
16201 if (linear_variable_step_check
16202 || reduction_seen == -2
16203 || allocate_seen
16204 || target_in_reduction_seen)
16205 for (pc = &clauses, c = clauses; c ; c = *pc)
16207 bool remove = false;
16208 if (allocate_seen)
16209 switch (OMP_CLAUSE_CODE (c))
16211 case OMP_CLAUSE_REDUCTION:
16212 case OMP_CLAUSE_IN_REDUCTION:
16213 case OMP_CLAUSE_TASK_REDUCTION:
16214 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
16216 t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
16217 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
16218 t = TREE_OPERAND (t, 0);
16219 if (TREE_CODE (t) == ADDR_EXPR
16220 || INDIRECT_REF_P (t))
16221 t = TREE_OPERAND (t, 0);
16222 if (DECL_P (t))
16223 bitmap_clear_bit (&aligned_head, DECL_UID (t));
16224 break;
16226 /* FALLTHRU */
16227 case OMP_CLAUSE_PRIVATE:
16228 case OMP_CLAUSE_FIRSTPRIVATE:
16229 case OMP_CLAUSE_LASTPRIVATE:
16230 case OMP_CLAUSE_LINEAR:
16231 if (DECL_P (OMP_CLAUSE_DECL (c)))
16232 bitmap_clear_bit (&aligned_head,
16233 DECL_UID (OMP_CLAUSE_DECL (c)));
16234 break;
16235 default:
16236 break;
16238 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
16239 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
16240 && !bitmap_bit_p (&map_head,
16241 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
16243 error_at (OMP_CLAUSE_LOCATION (c),
16244 "%<linear%> clause step is a parameter %qD not "
16245 "specified in %<uniform%> clause",
16246 OMP_CLAUSE_LINEAR_STEP (c));
16247 remove = true;
16249 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16250 && reduction_seen == -2)
16251 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
16252 if (target_in_reduction_seen
16253 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
16255 tree t = OMP_CLAUSE_DECL (c);
16256 while (handled_component_p (t)
16257 || INDIRECT_REF_P (t)
16258 || TREE_CODE (t) == ADDR_EXPR
16259 || TREE_CODE (t) == MEM_REF
16260 || TREE_CODE (t) == NON_LVALUE_EXPR)
16261 t = TREE_OPERAND (t, 0);
16262 if (DECL_P (t)
16263 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
16264 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
16267 if (remove)
16268 *pc = OMP_CLAUSE_CHAIN (c);
16269 else
16270 pc = &OMP_CLAUSE_CHAIN (c);
16273 if (allocate_seen)
16274 for (pc = &clauses, c = clauses; c ; c = *pc)
16276 bool remove = false;
16277 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
16278 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
16279 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
16281 error_at (OMP_CLAUSE_LOCATION (c),
16282 "%qD specified in %<allocate%> clause but not in "
16283 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
16284 remove = true;
16286 if (remove)
16287 *pc = OMP_CLAUSE_CHAIN (c);
16288 else
16289 pc = &OMP_CLAUSE_CHAIN (c);
16292 if (nogroup_seen && reduction_seen)
16294 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
16295 "%<nogroup%> clause must not be used together with "
16296 "%<reduction%> clause");
16297 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
16300 if (detach_seen)
16302 if (mergeable_seen)
16304 error_at (OMP_CLAUSE_LOCATION (*detach_seen),
16305 "%<detach%> clause must not be used together with "
16306 "%<mergeable%> clause");
16307 *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
16309 else
16311 tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
16313 for (pc = &clauses, c = clauses; c ; c = *pc)
16315 bool remove = false;
16316 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
16317 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
16318 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
16319 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
16320 && OMP_CLAUSE_DECL (c) == detach_decl)
16322 error_at (OMP_CLAUSE_LOCATION (c),
16323 "the event handle of a %<detach%> clause "
16324 "should not be in a data-sharing clause");
16325 remove = true;
16327 if (remove)
16328 *pc = OMP_CLAUSE_CHAIN (c);
16329 else
16330 pc = &OMP_CLAUSE_CHAIN (c);
16335 bitmap_obstack_release (NULL);
16336 return clauses;
16339 /* Return code to initialize DST with a copy constructor from SRC.
16340 C doesn't have copy constructors nor assignment operators, only for
16341 _Atomic vars we need to perform __atomic_load from src into a temporary
16342 followed by __atomic_store of the temporary to dst. */
16344 tree
16345 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
16347 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
16348 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
16350 location_t loc = OMP_CLAUSE_LOCATION (clause);
16351 tree type = TREE_TYPE (dst);
16352 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
16353 tree tmp = create_tmp_var (nonatomic_type);
16354 tree tmp_addr = build_fold_addr_expr (tmp);
16355 TREE_ADDRESSABLE (tmp) = 1;
16356 suppress_warning (tmp);
16357 tree src_addr = build_fold_addr_expr (src);
16358 tree dst_addr = build_fold_addr_expr (dst);
16359 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
16360 vec<tree, va_gc> *params;
16361 /* Expansion of a generic atomic load may require an addition
16362 element, so allocate enough to prevent a resize. */
16363 vec_alloc (params, 4);
16365 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
16366 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
16367 params->quick_push (src_addr);
16368 params->quick_push (tmp_addr);
16369 params->quick_push (seq_cst);
16370 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
16372 vec_alloc (params, 4);
16374 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
16375 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
16376 params->quick_push (dst_addr);
16377 params->quick_push (tmp_addr);
16378 params->quick_push (seq_cst);
16379 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
16380 return build2 (COMPOUND_EXPR, void_type_node, load, store);
16383 /* Create a transaction node. */
16385 tree
16386 c_finish_transaction (location_t loc, tree block, int flags)
16388 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
16389 if (flags & TM_STMT_ATTR_OUTER)
16390 TRANSACTION_EXPR_OUTER (stmt) = 1;
16391 if (flags & TM_STMT_ATTR_RELAXED)
16392 TRANSACTION_EXPR_RELAXED (stmt) = 1;
16393 return add_stmt (stmt);
16396 /* Make a variant type in the proper way for C/C++, propagating qualifiers
16397 down to the element type of an array. If ORIG_QUAL_TYPE is not
16398 NULL, then it should be used as the qualified type
16399 ORIG_QUAL_INDIRECT levels down in array type derivation (to
16400 preserve information about the typedef name from which an array
16401 type was derived). */
16403 tree
16404 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
16405 size_t orig_qual_indirect)
16407 if (type == error_mark_node)
16408 return type;
16410 if (TREE_CODE (type) == ARRAY_TYPE)
16412 tree t;
16413 tree element_type = c_build_qualified_type (TREE_TYPE (type),
16414 type_quals, orig_qual_type,
16415 orig_qual_indirect - 1);
16417 /* See if we already have an identically qualified type. */
16418 if (orig_qual_type && orig_qual_indirect == 0)
16419 t = orig_qual_type;
16420 else
16421 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
16423 if (TYPE_QUALS (strip_array_types (t)) == type_quals
16424 && TYPE_NAME (t) == TYPE_NAME (type)
16425 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
16426 && attribute_list_equal (TYPE_ATTRIBUTES (t),
16427 TYPE_ATTRIBUTES (type)))
16428 break;
16430 if (!t)
16432 tree domain = TYPE_DOMAIN (type);
16434 t = build_variant_type_copy (type);
16435 TREE_TYPE (t) = element_type;
16436 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (element_type);
16438 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
16439 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
16440 SET_TYPE_STRUCTURAL_EQUALITY (t);
16441 else if (TYPE_CANONICAL (element_type) != element_type
16442 || (domain && TYPE_CANONICAL (domain) != domain))
16444 tree unqualified_canon
16445 = build_array_type (TYPE_CANONICAL (element_type),
16446 domain? TYPE_CANONICAL (domain)
16447 : NULL_TREE);
16448 if (TYPE_REVERSE_STORAGE_ORDER (type))
16450 unqualified_canon
16451 = build_distinct_type_copy (unqualified_canon);
16452 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
16454 TYPE_CANONICAL (t)
16455 = c_build_qualified_type (unqualified_canon, type_quals);
16457 else
16458 TYPE_CANONICAL (t) = t;
16460 return t;
16463 /* A restrict-qualified pointer type must be a pointer to object or
16464 incomplete type. Note that the use of POINTER_TYPE_P also allows
16465 REFERENCE_TYPEs, which is appropriate for C++. */
16466 if ((type_quals & TYPE_QUAL_RESTRICT)
16467 && (!POINTER_TYPE_P (type)
16468 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
16470 error ("invalid use of %<restrict%>");
16471 type_quals &= ~TYPE_QUAL_RESTRICT;
16474 tree var_type = (orig_qual_type && orig_qual_indirect == 0
16475 ? orig_qual_type
16476 : build_qualified_type (type, type_quals));
16477 /* A variant type does not inherit the list of incomplete vars from the
16478 type main variant. */
16479 if ((RECORD_OR_UNION_TYPE_P (var_type)
16480 || TREE_CODE (var_type) == ENUMERAL_TYPE)
16481 && TYPE_MAIN_VARIANT (var_type) != var_type)
16482 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
16483 return var_type;
16486 /* Build a VA_ARG_EXPR for the C parser. */
16488 tree
16489 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
16491 if (error_operand_p (type))
16492 return error_mark_node;
16493 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
16494 order because it takes the address of the expression. */
16495 else if (handled_component_p (expr)
16496 && reverse_storage_order_for_component_p (expr))
16498 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
16499 return error_mark_node;
16501 else if (!COMPLETE_TYPE_P (type))
16503 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
16504 "type %qT", type);
16505 return error_mark_node;
16507 else if (TREE_CODE (type) == FUNCTION_TYPE)
16509 error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
16510 type);
16511 return error_mark_node;
16513 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
16514 warning_at (loc2, OPT_Wc___compat,
16515 "C++ requires promoted type, not enum type, in %<va_arg%>");
16516 return build_va_arg (loc2, expr, type);
16519 /* Return truthvalue of whether T1 is the same tree structure as T2.
16520 Return 1 if they are the same. Return false if they are different. */
16522 bool
16523 c_tree_equal (tree t1, tree t2)
16525 enum tree_code code1, code2;
16527 if (t1 == t2)
16528 return true;
16529 if (!t1 || !t2)
16530 return false;
16532 for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
16533 code1 = TREE_CODE (t1))
16534 t1 = TREE_OPERAND (t1, 0);
16535 for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
16536 code2 = TREE_CODE (t2))
16537 t2 = TREE_OPERAND (t2, 0);
16539 /* They might have become equal now. */
16540 if (t1 == t2)
16541 return true;
16543 if (code1 != code2)
16544 return false;
16546 if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
16547 return false;
16549 switch (code1)
16551 case INTEGER_CST:
16552 return wi::to_wide (t1) == wi::to_wide (t2);
16554 case REAL_CST:
16555 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
16557 case STRING_CST:
16558 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
16559 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
16560 TREE_STRING_LENGTH (t1));
16562 case FIXED_CST:
16563 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
16564 TREE_FIXED_CST (t2));
16566 case COMPLEX_CST:
16567 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
16568 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
16570 case VECTOR_CST:
16571 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
16573 case CONSTRUCTOR:
16574 /* We need to do this when determining whether or not two
16575 non-type pointer to member function template arguments
16576 are the same. */
16577 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
16578 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
16579 return false;
16581 tree field, value;
16582 unsigned int i;
16583 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
16585 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
16586 if (!c_tree_equal (field, elt2->index)
16587 || !c_tree_equal (value, elt2->value))
16588 return false;
16591 return true;
16593 case TREE_LIST:
16594 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
16595 return false;
16596 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
16597 return false;
16598 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
16600 case SAVE_EXPR:
16601 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16603 case CALL_EXPR:
16605 tree arg1, arg2;
16606 call_expr_arg_iterator iter1, iter2;
16607 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
16608 return false;
16609 for (arg1 = first_call_expr_arg (t1, &iter1),
16610 arg2 = first_call_expr_arg (t2, &iter2);
16611 arg1 && arg2;
16612 arg1 = next_call_expr_arg (&iter1),
16613 arg2 = next_call_expr_arg (&iter2))
16614 if (!c_tree_equal (arg1, arg2))
16615 return false;
16616 if (arg1 || arg2)
16617 return false;
16618 return true;
16621 case TARGET_EXPR:
16623 tree o1 = TREE_OPERAND (t1, 0);
16624 tree o2 = TREE_OPERAND (t2, 0);
16626 /* Special case: if either target is an unallocated VAR_DECL,
16627 it means that it's going to be unified with whatever the
16628 TARGET_EXPR is really supposed to initialize, so treat it
16629 as being equivalent to anything. */
16630 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
16631 && !DECL_RTL_SET_P (o1))
16632 /*Nop*/;
16633 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
16634 && !DECL_RTL_SET_P (o2))
16635 /*Nop*/;
16636 else if (!c_tree_equal (o1, o2))
16637 return false;
16639 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
16642 case COMPONENT_REF:
16643 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
16644 return false;
16645 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16647 case PARM_DECL:
16648 case VAR_DECL:
16649 case CONST_DECL:
16650 case FIELD_DECL:
16651 case FUNCTION_DECL:
16652 case IDENTIFIER_NODE:
16653 case SSA_NAME:
16654 return false;
16656 case TREE_VEC:
16658 unsigned ix;
16659 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
16660 return false;
16661 for (ix = TREE_VEC_LENGTH (t1); ix--;)
16662 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
16663 TREE_VEC_ELT (t2, ix)))
16664 return false;
16665 return true;
16668 CASE_CONVERT:
16669 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
16670 return false;
16671 break;
16673 default:
16674 break;
16677 switch (TREE_CODE_CLASS (code1))
16679 case tcc_unary:
16680 case tcc_binary:
16681 case tcc_comparison:
16682 case tcc_expression:
16683 case tcc_vl_exp:
16684 case tcc_reference:
16685 case tcc_statement:
16687 int i, n = TREE_OPERAND_LENGTH (t1);
16689 switch (code1)
16691 case PREINCREMENT_EXPR:
16692 case PREDECREMENT_EXPR:
16693 case POSTINCREMENT_EXPR:
16694 case POSTDECREMENT_EXPR:
16695 n = 1;
16696 break;
16697 case ARRAY_REF:
16698 n = 2;
16699 break;
16700 default:
16701 break;
16704 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
16705 && n != TREE_OPERAND_LENGTH (t2))
16706 return false;
16708 for (i = 0; i < n; ++i)
16709 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
16710 return false;
16712 return true;
16715 case tcc_type:
16716 return comptypes (t1, t2);
16717 default:
16718 gcc_unreachable ();
16722 /* Returns true when the function declaration FNDECL is implicit,
16723 introduced as a result of a call to an otherwise undeclared
16724 function, and false otherwise. */
16726 bool
16727 c_decl_implicit (const_tree fndecl)
16729 return C_DECL_IMPLICIT (fndecl);