middle-end/110495 - avoid associating constants with (VL) vectors
[official-gcc.git] / gcc / c / c-typeck.cc
blob7cf411155c60d45bf7d17238055625bfe8b7a039
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "memmodel.h"
30 #include "target.h"
31 #include "function.h"
32 #include "bitmap.h"
33 #include "c-tree.h"
34 #include "gimple-expr.h"
35 #include "predict.h"
36 #include "stor-layout.h"
37 #include "trans-mem.h"
38 #include "varasm.h"
39 #include "stmt.h"
40 #include "langhooks.h"
41 #include "c-lang.h"
42 #include "intl.h"
43 #include "tree-iterator.h"
44 #include "gimplify.h"
45 #include "tree-inline.h"
46 #include "omp-general.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
49 #include "gomp-constants.h"
50 #include "spellcheck-tree.h"
51 #include "gcc-rich-location.h"
52 #include "stringpool.h"
53 #include "attribs.h"
54 #include "asan.h"
55 #include "realmpfr.h"
57 /* Possible cases of implicit conversions. Used to select diagnostic messages
58 and control folding initializers in convert_for_assignment. */
59 enum impl_conv {
60 ic_argpass,
61 ic_assign,
62 ic_init,
63 ic_init_const,
64 ic_return
67 /* The level of nesting inside "__alignof__". */
68 int in_alignof;
70 /* The level of nesting inside "sizeof". */
71 int in_sizeof;
73 /* The level of nesting inside "typeof". */
74 int in_typeof;
76 /* True when parsing OpenMP loop expressions. */
77 bool c_in_omp_for;
79 /* The argument of last parsed sizeof expression, only to be tested
80 if expr.original_code == SIZEOF_EXPR. */
81 tree c_last_sizeof_arg;
82 location_t c_last_sizeof_loc;
84 /* Nonzero if we might need to print a "missing braces around
85 initializer" message within this initializer. */
86 static int found_missing_braces;
88 static bool require_constant_value;
89 static bool require_constant_elements;
90 static bool require_constexpr_value;
92 static tree qualify_type (tree, tree);
93 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
94 bool *);
95 static int comp_target_types (location_t, tree, tree);
96 static int function_types_compatible_p (const_tree, const_tree, bool *,
97 bool *);
98 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
99 static tree lookup_field (tree, tree);
100 static int convert_arguments (location_t, vec<location_t>, tree,
101 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
102 tree);
103 static tree pointer_diff (location_t, tree, tree, tree *);
104 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
105 enum impl_conv, bool, tree, tree, int,
106 int = 0);
107 static tree valid_compound_expr_initializer (tree, tree);
108 static void push_string (const char *);
109 static void push_member_name (tree);
110 static int spelling_length (void);
111 static char *print_spelling (char *);
112 static void warning_init (location_t, int, const char *);
113 static tree digest_init (location_t, tree, tree, tree, bool, bool, bool, bool,
114 bool, bool);
115 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
116 bool, struct obstack *);
117 static void output_pending_init_elements (int, struct obstack *);
118 static bool set_designator (location_t, bool, struct obstack *);
119 static void push_range_stack (tree, struct obstack *);
120 static void add_pending_init (location_t, tree, tree, tree, bool,
121 struct obstack *);
122 static void set_nonincremental_init (struct obstack *);
123 static void set_nonincremental_init_from_string (tree, struct obstack *);
124 static tree find_init_member (tree, struct obstack *);
125 static void readonly_warning (tree, enum lvalue_use);
126 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
127 static void record_maybe_used_decl (tree);
128 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
130 /* Return true if EXP is a null pointer constant, false otherwise. */
132 bool
133 null_pointer_constant_p (const_tree expr)
135 /* This should really operate on c_expr structures, but they aren't
136 yet available everywhere required. */
137 tree type = TREE_TYPE (expr);
139 /* An integer constant expression with the value 0, such an expression
140 cast to type void*, or the predefined constant nullptr, are a null
141 pointer constant. */
142 if (expr == nullptr_node)
143 return true;
145 return (TREE_CODE (expr) == INTEGER_CST
146 && !TREE_OVERFLOW (expr)
147 && integer_zerop (expr)
148 && (INTEGRAL_TYPE_P (type)
149 || (TREE_CODE (type) == POINTER_TYPE
150 && VOID_TYPE_P (TREE_TYPE (type))
151 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
154 /* EXPR may appear in an unevaluated part of an integer constant
155 expression, but not in an evaluated part. Wrap it in a
156 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
157 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
159 static tree
160 note_integer_operands (tree expr)
162 tree ret;
163 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
165 ret = copy_node (expr);
166 TREE_OVERFLOW (ret) = 1;
168 else
170 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
171 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
173 return ret;
176 /* Having checked whether EXPR may appear in an unevaluated part of an
177 integer constant expression and found that it may, remove any
178 C_MAYBE_CONST_EXPR noting this fact and return the resulting
179 expression. */
181 static inline tree
182 remove_c_maybe_const_expr (tree expr)
184 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
185 return C_MAYBE_CONST_EXPR_EXPR (expr);
186 else
187 return expr;
190 \f/* This is a cache to hold if two types are compatible or not. */
192 struct tagged_tu_seen_cache {
193 const struct tagged_tu_seen_cache * next;
194 const_tree t1;
195 const_tree t2;
196 /* The return value of tagged_types_tu_compatible_p if we had seen
197 these two types already. */
198 int val;
201 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
202 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
204 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
205 does not have an incomplete type. (That includes void types.)
206 LOC is the location of the use. */
208 tree
209 require_complete_type (location_t loc, tree value)
211 tree type = TREE_TYPE (value);
213 if (error_operand_p (value))
214 return error_mark_node;
216 /* First, detect a valid value with a complete type. */
217 if (COMPLETE_TYPE_P (type))
218 return value;
220 c_incomplete_type_error (loc, value, type);
221 return error_mark_node;
224 /* Print an error message for invalid use of an incomplete type.
225 VALUE is the expression that was used (or 0 if that isn't known)
226 and TYPE is the type that was invalid. LOC is the location for
227 the error. */
229 void
230 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
232 /* Avoid duplicate error message. */
233 if (TREE_CODE (type) == ERROR_MARK)
234 return;
236 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
237 error_at (loc, "%qD has an incomplete type %qT", value, type);
238 else
240 retry:
241 /* We must print an error message. Be clever about what it says. */
243 switch (TREE_CODE (type))
245 case RECORD_TYPE:
246 case UNION_TYPE:
247 case ENUMERAL_TYPE:
248 break;
250 case VOID_TYPE:
251 error_at (loc, "invalid use of void expression");
252 return;
254 case ARRAY_TYPE:
255 if (TYPE_DOMAIN (type))
257 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
259 error_at (loc, "invalid use of flexible array member");
260 return;
262 type = TREE_TYPE (type);
263 goto retry;
265 error_at (loc, "invalid use of array with unspecified bounds");
266 return;
268 default:
269 gcc_unreachable ();
272 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
273 error_at (loc, "invalid use of undefined type %qT", type);
274 else
275 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
276 error_at (loc, "invalid use of incomplete typedef %qT", type);
280 /* Given a type, apply default promotions wrt unnamed function
281 arguments and return the new type. */
283 tree
284 c_type_promotes_to (tree type)
286 tree ret = NULL_TREE;
288 if (TYPE_MAIN_VARIANT (type) == float_type_node)
289 ret = double_type_node;
290 else if (c_promoting_integer_type_p (type))
292 /* Preserve unsignedness if not really getting any wider. */
293 if (TYPE_UNSIGNED (type)
294 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
295 ret = unsigned_type_node;
296 else
297 ret = integer_type_node;
300 if (ret != NULL_TREE)
301 return (TYPE_ATOMIC (type)
302 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
303 : ret);
305 return type;
308 /* Return true if between two named address spaces, whether there is a superset
309 named address space that encompasses both address spaces. If there is a
310 superset, return which address space is the superset. */
312 static bool
313 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
315 if (as1 == as2)
317 *common = as1;
318 return true;
320 else if (targetm.addr_space.subset_p (as1, as2))
322 *common = as2;
323 return true;
325 else if (targetm.addr_space.subset_p (as2, as1))
327 *common = as1;
328 return true;
330 else
331 return false;
334 /* Return a variant of TYPE which has all the type qualifiers of LIKE
335 as well as those of TYPE. */
337 static tree
338 qualify_type (tree type, tree like)
340 addr_space_t as_type = TYPE_ADDR_SPACE (type);
341 addr_space_t as_like = TYPE_ADDR_SPACE (like);
342 addr_space_t as_common;
344 /* If the two named address spaces are different, determine the common
345 superset address space. If there isn't one, raise an error. */
346 if (!addr_space_superset (as_type, as_like, &as_common))
348 as_common = as_type;
349 error ("%qT and %qT are in disjoint named address spaces",
350 type, like);
353 return c_build_qualified_type (type,
354 TYPE_QUALS_NO_ADDR_SPACE (type)
355 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
356 | ENCODE_QUAL_ADDR_SPACE (as_common));
360 /* If NTYPE is a type of a non-variadic function with a prototype
361 and OTYPE is a type of a function without a prototype and ATTRS
362 contains attribute format, diagnosess and removes it from ATTRS.
363 Returns the result of build_type_attribute_variant of NTYPE and
364 the (possibly) modified ATTRS. */
366 static tree
367 build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
369 if (!prototype_p (otype)
370 && prototype_p (ntype)
371 && lookup_attribute ("format", attrs))
373 warning_at (input_location, OPT_Wattributes,
374 "%qs attribute cannot be applied to a function that "
375 "does not take variable arguments", "format");
376 attrs = remove_attribute ("format", attrs);
378 return build_type_attribute_variant (ntype, attrs);
381 /* Return the composite type of two compatible types.
383 We assume that comptypes has already been done and returned
384 nonzero; if that isn't so, this may crash. In particular, we
385 assume that qualifiers match. */
387 tree
388 composite_type (tree t1, tree t2)
390 enum tree_code code1;
391 enum tree_code code2;
392 tree attributes;
394 /* Save time if the two types are the same. */
396 if (t1 == t2) return t1;
398 /* If one type is nonsense, use the other. */
399 if (t1 == error_mark_node)
400 return t2;
401 if (t2 == error_mark_node)
402 return t1;
404 code1 = TREE_CODE (t1);
405 code2 = TREE_CODE (t2);
407 /* Merge the attributes. */
408 attributes = targetm.merge_type_attributes (t1, t2);
410 /* If one is an enumerated type and the other is the compatible
411 integer type, the composite type might be either of the two
412 (DR#013 question 3). For consistency, use the enumerated type as
413 the composite type. */
415 if (code1 == ENUMERAL_TYPE
416 && (code2 == INTEGER_TYPE || code2 == BOOLEAN_TYPE))
417 return t1;
418 if (code2 == ENUMERAL_TYPE
419 && (code1 == INTEGER_TYPE || code1 == BOOLEAN_TYPE))
420 return t2;
422 gcc_assert (code1 == code2);
424 switch (code1)
426 case POINTER_TYPE:
427 /* For two pointers, do this recursively on the target type. */
429 tree pointed_to_1 = TREE_TYPE (t1);
430 tree pointed_to_2 = TREE_TYPE (t2);
431 tree target = composite_type (pointed_to_1, pointed_to_2);
432 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
433 t1 = build_type_attribute_variant (t1, attributes);
434 return qualify_type (t1, t2);
437 case ARRAY_TYPE:
439 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
440 int quals;
441 tree unqual_elt;
442 tree d1 = TYPE_DOMAIN (t1);
443 tree d2 = TYPE_DOMAIN (t2);
444 bool d1_variable, d2_variable;
445 bool d1_zero, d2_zero;
446 bool t1_complete, t2_complete;
448 /* We should not have any type quals on arrays at all. */
449 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
450 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
452 t1_complete = COMPLETE_TYPE_P (t1);
453 t2_complete = COMPLETE_TYPE_P (t2);
455 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
456 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
458 d1_variable = (!d1_zero
459 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
460 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
461 d2_variable = (!d2_zero
462 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
463 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
464 d1_variable = d1_variable || (d1_zero && C_TYPE_VARIABLE_SIZE (t1));
465 d2_variable = d2_variable || (d2_zero && C_TYPE_VARIABLE_SIZE (t2));
467 /* Save space: see if the result is identical to one of the args. */
468 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
469 && (d2_variable || d2_zero || !d1_variable))
470 return build_type_attribute_variant (t1, attributes);
471 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
472 && (d1_variable || d1_zero || !d2_variable))
473 return build_type_attribute_variant (t2, attributes);
475 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
476 return build_type_attribute_variant (t1, attributes);
477 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
478 return build_type_attribute_variant (t2, attributes);
480 /* Merge the element types, and have a size if either arg has
481 one. We may have qualifiers on the element types. To set
482 up TYPE_MAIN_VARIANT correctly, we need to form the
483 composite of the unqualified types and add the qualifiers
484 back at the end. */
485 quals = TYPE_QUALS (strip_array_types (elt));
486 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
487 t1 = build_array_type (unqual_elt,
488 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
489 && (d2_variable
490 || d2_zero
491 || !d1_variable))
492 ? t1
493 : t2));
494 /* Ensure a composite type involving a zero-length array type
495 is a zero-length type not an incomplete type. */
496 if (d1_zero && d2_zero
497 && (t1_complete || t2_complete)
498 && !COMPLETE_TYPE_P (t1))
500 TYPE_SIZE (t1) = bitsize_zero_node;
501 TYPE_SIZE_UNIT (t1) = size_zero_node;
503 t1 = c_build_qualified_type (t1, quals);
504 return build_type_attribute_variant (t1, attributes);
507 case ENUMERAL_TYPE:
508 case RECORD_TYPE:
509 case UNION_TYPE:
510 if (attributes != NULL)
512 /* Try harder not to create a new aggregate type. */
513 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
514 return t1;
515 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
516 return t2;
518 return build_type_attribute_variant (t1, attributes);
520 case FUNCTION_TYPE:
521 /* Function types: prefer the one that specified arg types.
522 If both do, merge the arg types. Also merge the return types. */
524 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
525 tree p1 = TYPE_ARG_TYPES (t1);
526 tree p2 = TYPE_ARG_TYPES (t2);
527 int len;
528 tree newargs, n;
529 int i;
531 /* Save space: see if the result is identical to one of the args. */
532 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
533 return build_functype_attribute_variant (t1, t2, attributes);
534 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
535 return build_functype_attribute_variant (t2, t1, attributes);
537 /* Simple way if one arg fails to specify argument types. */
538 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
540 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2),
541 TYPE_NO_NAMED_ARGS_STDARG_P (t2));
542 t1 = build_type_attribute_variant (t1, attributes);
543 return qualify_type (t1, t2);
545 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
547 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1),
548 TYPE_NO_NAMED_ARGS_STDARG_P (t1));
549 t1 = build_type_attribute_variant (t1, attributes);
550 return qualify_type (t1, t2);
553 /* If both args specify argument types, we must merge the two
554 lists, argument by argument. */
556 for (len = 0, newargs = p1;
557 newargs && newargs != void_list_node;
558 len++, newargs = TREE_CHAIN (newargs))
561 for (i = 0; i < len; i++)
562 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
564 n = newargs;
566 for (; p1 && p1 != void_list_node;
567 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
569 /* A null type means arg type is not specified.
570 Take whatever the other function type has. */
571 if (TREE_VALUE (p1) == NULL_TREE)
573 TREE_VALUE (n) = TREE_VALUE (p2);
574 goto parm_done;
576 if (TREE_VALUE (p2) == NULL_TREE)
578 TREE_VALUE (n) = TREE_VALUE (p1);
579 goto parm_done;
582 /* Given wait (union {union wait *u; int *i} *)
583 and wait (union wait *),
584 prefer union wait * as type of parm. */
585 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
586 && TREE_VALUE (p1) != TREE_VALUE (p2))
588 tree memb;
589 tree mv2 = TREE_VALUE (p2);
590 if (mv2 && mv2 != error_mark_node
591 && TREE_CODE (mv2) != ARRAY_TYPE)
592 mv2 = TYPE_MAIN_VARIANT (mv2);
593 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
594 memb; memb = DECL_CHAIN (memb))
596 tree mv3 = TREE_TYPE (memb);
597 if (mv3 && mv3 != error_mark_node
598 && TREE_CODE (mv3) != ARRAY_TYPE)
599 mv3 = TYPE_MAIN_VARIANT (mv3);
600 if (comptypes (mv3, mv2))
602 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
603 TREE_VALUE (p2));
604 pedwarn (input_location, OPT_Wpedantic,
605 "function types not truly compatible in ISO C");
606 goto parm_done;
610 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
611 && TREE_VALUE (p2) != TREE_VALUE (p1))
613 tree memb;
614 tree mv1 = TREE_VALUE (p1);
615 if (mv1 && mv1 != error_mark_node
616 && TREE_CODE (mv1) != ARRAY_TYPE)
617 mv1 = TYPE_MAIN_VARIANT (mv1);
618 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
619 memb; memb = DECL_CHAIN (memb))
621 tree mv3 = TREE_TYPE (memb);
622 if (mv3 && mv3 != error_mark_node
623 && TREE_CODE (mv3) != ARRAY_TYPE)
624 mv3 = TYPE_MAIN_VARIANT (mv3);
625 if (comptypes (mv3, mv1))
627 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
628 TREE_VALUE (p1));
629 pedwarn (input_location, OPT_Wpedantic,
630 "function types not truly compatible in ISO C");
631 goto parm_done;
635 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
636 parm_done: ;
639 t1 = build_function_type (valtype, newargs);
640 t1 = qualify_type (t1, t2);
642 /* FALLTHRU */
644 default:
645 return build_type_attribute_variant (t1, attributes);
650 /* Return the type of a conditional expression between pointers to
651 possibly differently qualified versions of compatible types.
653 We assume that comp_target_types has already been done and returned
654 nonzero; if that isn't so, this may crash. */
656 static tree
657 common_pointer_type (tree t1, tree t2)
659 tree attributes;
660 tree pointed_to_1, mv1;
661 tree pointed_to_2, mv2;
662 tree target;
663 unsigned target_quals;
664 addr_space_t as1, as2, as_common;
665 int quals1, quals2;
667 /* Save time if the two types are the same. */
669 if (t1 == t2) return t1;
671 /* If one type is nonsense, use the other. */
672 if (t1 == error_mark_node)
673 return t2;
674 if (t2 == error_mark_node)
675 return t1;
677 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
678 && TREE_CODE (t2) == POINTER_TYPE);
680 /* Merge the attributes. */
681 attributes = targetm.merge_type_attributes (t1, t2);
683 /* Find the composite type of the target types, and combine the
684 qualifiers of the two types' targets. Do not lose qualifiers on
685 array element types by taking the TYPE_MAIN_VARIANT. */
686 mv1 = pointed_to_1 = TREE_TYPE (t1);
687 mv2 = pointed_to_2 = TREE_TYPE (t2);
688 if (TREE_CODE (mv1) != ARRAY_TYPE)
689 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
690 if (TREE_CODE (mv2) != ARRAY_TYPE)
691 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
692 target = composite_type (mv1, mv2);
694 /* Strip array types to get correct qualifier for pointers to arrays */
695 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
696 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
698 /* For function types do not merge const qualifiers, but drop them
699 if used inconsistently. The middle-end uses these to mark const
700 and noreturn functions. */
701 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
702 target_quals = (quals1 & quals2);
703 else
704 target_quals = (quals1 | quals2);
706 /* If the two named address spaces are different, determine the common
707 superset address space. This is guaranteed to exist due to the
708 assumption that comp_target_type returned non-zero. */
709 as1 = TYPE_ADDR_SPACE (pointed_to_1);
710 as2 = TYPE_ADDR_SPACE (pointed_to_2);
711 if (!addr_space_superset (as1, as2, &as_common))
712 gcc_unreachable ();
714 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
716 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
717 return build_type_attribute_variant (t1, attributes);
720 /* Return the common type for two arithmetic types under the usual
721 arithmetic conversions. The default conversions have already been
722 applied, and enumerated types converted to their compatible integer
723 types. The resulting type is unqualified and has no attributes.
725 This is the type for the result of most arithmetic operations
726 if the operands have the given two types. */
728 static tree
729 c_common_type (tree t1, tree t2)
731 enum tree_code code1;
732 enum tree_code code2;
734 /* If one type is nonsense, use the other. */
735 if (t1 == error_mark_node)
736 return t2;
737 if (t2 == error_mark_node)
738 return t1;
740 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
741 t1 = TYPE_MAIN_VARIANT (t1);
743 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
744 t2 = TYPE_MAIN_VARIANT (t2);
746 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
748 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
749 t1 = build_type_attribute_variant (t1, attrs);
752 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
754 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
755 t2 = build_type_attribute_variant (t2, attrs);
758 /* Save time if the two types are the same. */
760 if (t1 == t2) return t1;
762 code1 = TREE_CODE (t1);
763 code2 = TREE_CODE (t2);
765 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
766 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
767 || code1 == INTEGER_TYPE);
768 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
769 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
770 || code2 == INTEGER_TYPE);
772 /* When one operand is a decimal float type, the other operand cannot be
773 a generic float type or a complex type. We also disallow vector types
774 here. */
775 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
776 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
778 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
780 error ("cannot mix operands of decimal floating and vector types");
781 return error_mark_node;
783 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
785 error ("cannot mix operands of decimal floating and complex types");
786 return error_mark_node;
788 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
790 error ("cannot mix operands of decimal floating "
791 "and other floating types");
792 return error_mark_node;
796 /* If one type is a vector type, return that type. (How the usual
797 arithmetic conversions apply to the vector types extension is not
798 precisely specified.) */
799 if (code1 == VECTOR_TYPE)
800 return t1;
802 if (code2 == VECTOR_TYPE)
803 return t2;
805 /* If one type is complex, form the common type of the non-complex
806 components, then make that complex. Use T1 or T2 if it is the
807 required type. */
808 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
810 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
811 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
812 tree subtype = c_common_type (subtype1, subtype2);
814 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
815 return t1;
816 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
817 return t2;
818 else
819 return build_complex_type (subtype);
822 /* If only one is real, use it as the result. */
824 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
825 return t1;
827 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
828 return t2;
830 /* If both are real and either are decimal floating point types, use
831 the decimal floating point type with the greater precision. */
833 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
835 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
836 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
837 return dfloat128_type_node;
838 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
839 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
840 return dfloat64_type_node;
841 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
842 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
843 return dfloat32_type_node;
846 /* Deal with fixed-point types. */
847 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
849 unsigned int unsignedp = 0, satp = 0;
850 scalar_mode m1, m2;
851 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
853 m1 = SCALAR_TYPE_MODE (t1);
854 m2 = SCALAR_TYPE_MODE (t2);
856 /* If one input type is saturating, the result type is saturating. */
857 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
858 satp = 1;
860 /* If both fixed-point types are unsigned, the result type is unsigned.
861 When mixing fixed-point and integer types, follow the sign of the
862 fixed-point type.
863 Otherwise, the result type is signed. */
864 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
865 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
866 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
867 && TYPE_UNSIGNED (t1))
868 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
869 && TYPE_UNSIGNED (t2)))
870 unsignedp = 1;
872 /* The result type is signed. */
873 if (unsignedp == 0)
875 /* If the input type is unsigned, we need to convert to the
876 signed type. */
877 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
879 enum mode_class mclass = (enum mode_class) 0;
880 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
881 mclass = MODE_FRACT;
882 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
883 mclass = MODE_ACCUM;
884 else
885 gcc_unreachable ();
886 m1 = as_a <scalar_mode>
887 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
889 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
891 enum mode_class mclass = (enum mode_class) 0;
892 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
893 mclass = MODE_FRACT;
894 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
895 mclass = MODE_ACCUM;
896 else
897 gcc_unreachable ();
898 m2 = as_a <scalar_mode>
899 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
903 if (code1 == FIXED_POINT_TYPE)
905 fbit1 = GET_MODE_FBIT (m1);
906 ibit1 = GET_MODE_IBIT (m1);
908 else
910 fbit1 = 0;
911 /* Signed integers need to subtract one sign bit. */
912 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
915 if (code2 == FIXED_POINT_TYPE)
917 fbit2 = GET_MODE_FBIT (m2);
918 ibit2 = GET_MODE_IBIT (m2);
920 else
922 fbit2 = 0;
923 /* Signed integers need to subtract one sign bit. */
924 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
927 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
928 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
929 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
930 satp);
933 /* Both real or both integers; use the one with greater precision. */
935 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
936 return t1;
937 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
938 return t2;
940 /* Same precision. Prefer long longs to longs to ints when the
941 same precision, following the C99 rules on integer type rank
942 (which are equivalent to the C90 rules for C90 types). */
944 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
945 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
946 return long_long_unsigned_type_node;
948 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
949 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
951 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
952 return long_long_unsigned_type_node;
953 else
954 return long_long_integer_type_node;
957 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
958 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
959 return long_unsigned_type_node;
961 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
962 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
964 /* But preserve unsignedness from the other type,
965 since long cannot hold all the values of an unsigned int. */
966 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
967 return long_unsigned_type_node;
968 else
969 return long_integer_type_node;
972 /* For floating types of the same TYPE_PRECISION (which we here
973 assume means either the same set of values, or sets of values
974 neither a subset of the other, with behavior being undefined in
975 the latter case), follow the rules from TS 18661-3: prefer
976 interchange types _FloatN, then standard types long double,
977 double, float, then extended types _FloatNx. For extended types,
978 check them starting with _Float128x as that seems most consistent
979 in spirit with preferring long double to double; for interchange
980 types, also check in that order for consistency although it's not
981 possible for more than one of them to have the same
982 precision. */
983 tree mv1 = TYPE_MAIN_VARIANT (t1);
984 tree mv2 = TYPE_MAIN_VARIANT (t2);
986 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
987 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
988 return FLOATN_TYPE_NODE (i);
990 /* Likewise, prefer long double to double even if same size. */
991 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
992 return long_double_type_node;
994 /* Likewise, prefer double to float even if same size.
995 We got a couple of embedded targets with 32 bit doubles, and the
996 pdp11 might have 64 bit floats. */
997 if (mv1 == double_type_node || mv2 == double_type_node)
998 return double_type_node;
1000 if (mv1 == float_type_node || mv2 == float_type_node)
1001 return float_type_node;
1003 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1004 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1005 return FLOATNX_TYPE_NODE (i);
1007 /* Otherwise prefer the unsigned one. */
1009 if (TYPE_UNSIGNED (t1))
1010 return t1;
1011 else
1012 return t2;
1015 /* Wrapper around c_common_type that is used by c-common.cc and other
1016 front end optimizations that remove promotions. ENUMERAL_TYPEs
1017 are allowed here and are converted to their compatible integer types.
1018 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1019 preferably a non-Boolean type as the common type. */
1020 tree
1021 common_type (tree t1, tree t2)
1023 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1024 t1 = ENUM_UNDERLYING_TYPE (t1);
1025 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1026 t2 = ENUM_UNDERLYING_TYPE (t2);
1028 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1029 if (TREE_CODE (t1) == BOOLEAN_TYPE
1030 && TREE_CODE (t2) == BOOLEAN_TYPE)
1031 return boolean_type_node;
1033 /* If either type is BOOLEAN_TYPE, then return the other. */
1034 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1035 return t2;
1036 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1037 return t1;
1039 return c_common_type (t1, t2);
1042 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1043 or various other operations. Return 2 if they are compatible
1044 but a warning may be needed if you use them together. */
1047 comptypes (tree type1, tree type2)
1049 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1050 int val;
1052 val = comptypes_internal (type1, type2, NULL, NULL);
1053 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1055 return val;
1058 /* Like comptypes, but if it returns non-zero because enum and int are
1059 compatible, it sets *ENUM_AND_INT_P to true. */
1062 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1064 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1065 int val;
1067 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1068 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1070 return val;
1073 /* Like comptypes, but if it returns nonzero for different types, it
1074 sets *DIFFERENT_TYPES_P to true. */
1077 comptypes_check_different_types (tree type1, tree type2,
1078 bool *different_types_p)
1080 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1081 int val;
1083 val = comptypes_internal (type1, type2, NULL, different_types_p);
1084 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1086 return val;
1089 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1090 or various other operations. Return 2 if they are compatible
1091 but a warning may be needed if you use them together. If
1092 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1093 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1094 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1095 NULL, and the types are compatible but different enough not to be
1096 permitted in C11 typedef redeclarations, then this sets
1097 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1098 false, but may or may not be set if the types are incompatible.
1099 This differs from comptypes, in that we don't free the seen
1100 types. */
1102 static int
1103 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1104 bool *different_types_p)
1106 const_tree t1 = type1;
1107 const_tree t2 = type2;
1108 int attrval, val;
1110 /* Suppress errors caused by previously reported errors. */
1112 if (t1 == t2 || !t1 || !t2
1113 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1114 return 1;
1116 /* Enumerated types are compatible with integer types, but this is
1117 not transitive: two enumerated types in the same translation unit
1118 are compatible with each other only if they are the same type. */
1120 if (TREE_CODE (t1) == ENUMERAL_TYPE
1121 && COMPLETE_TYPE_P (t1)
1122 && TREE_CODE (t2) != ENUMERAL_TYPE)
1124 t1 = ENUM_UNDERLYING_TYPE (t1);
1125 if (TREE_CODE (t2) != VOID_TYPE)
1127 if (enum_and_int_p != NULL)
1128 *enum_and_int_p = true;
1129 if (different_types_p != NULL)
1130 *different_types_p = true;
1133 else if (TREE_CODE (t2) == ENUMERAL_TYPE
1134 && COMPLETE_TYPE_P (t2)
1135 && TREE_CODE (t1) != ENUMERAL_TYPE)
1137 t2 = ENUM_UNDERLYING_TYPE (t2);
1138 if (TREE_CODE (t1) != VOID_TYPE)
1140 if (enum_and_int_p != NULL)
1141 *enum_and_int_p = true;
1142 if (different_types_p != NULL)
1143 *different_types_p = true;
1147 if (t1 == t2)
1148 return 1;
1150 /* Different classes of types can't be compatible. */
1152 if (TREE_CODE (t1) != TREE_CODE (t2))
1153 return 0;
1155 /* Qualifiers must match. C99 6.7.3p9 */
1157 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1158 return 0;
1160 /* Allow for two different type nodes which have essentially the same
1161 definition. Note that we already checked for equality of the type
1162 qualifiers (just above). */
1164 if (TREE_CODE (t1) != ARRAY_TYPE
1165 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1166 return 1;
1168 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1169 if (!(attrval = comp_type_attributes (t1, t2)))
1170 return 0;
1172 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1173 val = 0;
1175 switch (TREE_CODE (t1))
1177 case INTEGER_TYPE:
1178 case FIXED_POINT_TYPE:
1179 case REAL_TYPE:
1180 /* With these nodes, we can't determine type equivalence by
1181 looking at what is stored in the nodes themselves, because
1182 two nodes might have different TYPE_MAIN_VARIANTs but still
1183 represent the same type. For example, wchar_t and int could
1184 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1185 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1186 and are distinct types. On the other hand, int and the
1187 following typedef
1189 typedef int INT __attribute((may_alias));
1191 have identical properties, different TYPE_MAIN_VARIANTs, but
1192 represent the same type. The canonical type system keeps
1193 track of equivalence in this case, so we fall back on it. */
1194 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1196 case POINTER_TYPE:
1197 /* Do not remove mode information. */
1198 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1199 break;
1200 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1201 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1202 enum_and_int_p, different_types_p));
1203 break;
1205 case FUNCTION_TYPE:
1206 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1207 different_types_p);
1208 break;
1210 case ARRAY_TYPE:
1212 tree d1 = TYPE_DOMAIN (t1);
1213 tree d2 = TYPE_DOMAIN (t2);
1214 bool d1_variable, d2_variable;
1215 bool d1_zero, d2_zero;
1216 val = 1;
1218 /* Target types must match incl. qualifiers. */
1219 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1220 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1221 enum_and_int_p,
1222 different_types_p)) == 0)
1223 return 0;
1225 if (different_types_p != NULL
1226 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1227 *different_types_p = true;
1228 /* Sizes must match unless one is missing or variable. */
1229 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1230 break;
1232 d1_zero = !TYPE_MAX_VALUE (d1);
1233 d2_zero = !TYPE_MAX_VALUE (d2);
1235 d1_variable = (!d1_zero
1236 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1237 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1238 d2_variable = (!d2_zero
1239 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1240 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1241 d1_variable = d1_variable || (d1_zero && C_TYPE_VARIABLE_SIZE (t1));
1242 d2_variable = d2_variable || (d2_zero && C_TYPE_VARIABLE_SIZE (t2));
1244 if (different_types_p != NULL
1245 && d1_variable != d2_variable)
1246 *different_types_p = true;
1247 if (d1_variable || d2_variable)
1248 break;
1249 if (d1_zero && d2_zero)
1250 break;
1251 if (d1_zero || d2_zero
1252 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1253 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1254 val = 0;
1256 break;
1259 case ENUMERAL_TYPE:
1260 case RECORD_TYPE:
1261 case UNION_TYPE:
1262 if (val != 1 && false)
1264 tree a1 = TYPE_ATTRIBUTES (t1);
1265 tree a2 = TYPE_ATTRIBUTES (t2);
1267 if (! attribute_list_contained (a1, a2)
1268 && ! attribute_list_contained (a2, a1))
1269 break;
1271 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1272 different_types_p);
1274 if (attrval != 2)
1275 return val;
1277 break;
1279 case VECTOR_TYPE:
1280 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1281 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1282 enum_and_int_p, different_types_p));
1283 break;
1285 default:
1286 break;
1288 return attrval == 2 && val == 1 ? 2 : val;
1291 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1292 their qualifiers, except for named address spaces. If the pointers point to
1293 different named addresses, then we must determine if one address space is a
1294 subset of the other. */
1296 static int
1297 comp_target_types (location_t location, tree ttl, tree ttr)
1299 int val;
1300 int val_ped;
1301 tree mvl = TREE_TYPE (ttl);
1302 tree mvr = TREE_TYPE (ttr);
1303 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1304 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1305 addr_space_t as_common;
1306 bool enum_and_int_p;
1308 /* Fail if pointers point to incompatible address spaces. */
1309 if (!addr_space_superset (asl, asr, &as_common))
1310 return 0;
1312 /* For pedantic record result of comptypes on arrays before losing
1313 qualifiers on the element type below. */
1314 val_ped = 1;
1316 if (TREE_CODE (mvl) == ARRAY_TYPE
1317 && TREE_CODE (mvr) == ARRAY_TYPE)
1318 val_ped = comptypes (mvl, mvr);
1320 /* Qualifiers on element types of array types that are
1321 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1323 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1324 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1325 : TYPE_MAIN_VARIANT (mvl));
1327 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1328 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1329 : TYPE_MAIN_VARIANT (mvr));
1331 enum_and_int_p = false;
1332 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1334 if (val == 1 && val_ped != 1)
1335 pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1336 "in ISO C before C2X");
1338 if (val == 2)
1339 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1341 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1342 warning_at (location, OPT_Wc___compat,
1343 "pointer target types incompatible in C++");
1345 return val;
1348 /* Subroutines of `comptypes'. */
1352 /* Allocate the seen two types, assuming that they are compatible. */
1354 static struct tagged_tu_seen_cache *
1355 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1357 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1358 tu->next = tagged_tu_seen_base;
1359 tu->t1 = t1;
1360 tu->t2 = t2;
1362 tagged_tu_seen_base = tu;
1364 /* The C standard says that two structures in different translation
1365 units are compatible with each other only if the types of their
1366 fields are compatible (among other things). We assume that they
1367 are compatible until proven otherwise when building the cache.
1368 An example where this can occur is:
1369 struct a
1371 struct a *next;
1373 If we are comparing this against a similar struct in another TU,
1374 and did not assume they were compatible, we end up with an infinite
1375 loop. */
1376 tu->val = 1;
1377 return tu;
1380 /* Free the seen types until we get to TU_TIL. */
1382 static void
1383 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1385 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1386 while (tu != tu_til)
1388 const struct tagged_tu_seen_cache *const tu1
1389 = (const struct tagged_tu_seen_cache *) tu;
1390 tu = tu1->next;
1391 XDELETE (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1393 tagged_tu_seen_base = tu_til;
1396 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1397 compatible. If the two types are not the same (which has been
1398 checked earlier), this can only happen when multiple translation
1399 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1400 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1401 comptypes_internal. */
1403 static int
1404 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1405 bool *enum_and_int_p, bool *different_types_p)
1407 tree s1, s2;
1408 bool needs_warning = false;
1410 /* We have to verify that the tags of the types are the same. This
1411 is harder than it looks because this may be a typedef, so we have
1412 to go look at the original type. It may even be a typedef of a
1413 typedef...
1414 In the case of compiler-created builtin structs the TYPE_DECL
1415 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1416 while (TYPE_NAME (t1)
1417 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1418 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1419 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1421 while (TYPE_NAME (t2)
1422 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1423 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1424 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1426 /* C90 didn't have the requirement that the two tags be the same. */
1427 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1428 return 0;
1430 /* C90 didn't say what happened if one or both of the types were
1431 incomplete; we choose to follow C99 rules here, which is that they
1432 are compatible. */
1433 if (TYPE_SIZE (t1) == NULL
1434 || TYPE_SIZE (t2) == NULL)
1435 return 1;
1438 const struct tagged_tu_seen_cache * tts_i;
1439 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1440 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1441 return tts_i->val;
1444 switch (TREE_CODE (t1))
1446 case ENUMERAL_TYPE:
1448 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1449 /* Speed up the case where the type values are in the same order. */
1450 tree tv1 = TYPE_VALUES (t1);
1451 tree tv2 = TYPE_VALUES (t2);
1453 if (tv1 == tv2)
1455 return 1;
1458 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1460 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1461 break;
1462 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1464 tu->val = 0;
1465 return 0;
1469 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1471 return 1;
1473 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1475 tu->val = 0;
1476 return 0;
1479 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1481 tu->val = 0;
1482 return 0;
1485 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1487 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1488 if (s2 == NULL
1489 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1491 tu->val = 0;
1492 return 0;
1495 return 1;
1498 case UNION_TYPE:
1500 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1502 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1504 tu->val = 0;
1505 return 0;
1508 /* Speed up the common case where the fields are in the same order. */
1509 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1510 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1512 int result;
1514 if (DECL_NAME (s1) != DECL_NAME (s2))
1515 break;
1516 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1517 enum_and_int_p, different_types_p);
1519 if (result != 1 && !DECL_NAME (s1))
1520 break;
1521 if (result == 0)
1523 tu->val = 0;
1524 return 0;
1526 if (result == 2)
1527 needs_warning = true;
1529 if (TREE_CODE (s1) == FIELD_DECL
1530 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1531 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1533 tu->val = 0;
1534 return 0;
1537 if (!s1 && !s2)
1539 tu->val = needs_warning ? 2 : 1;
1540 return tu->val;
1543 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1545 bool ok = false;
1547 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1548 if (DECL_NAME (s1) == DECL_NAME (s2))
1550 int result;
1552 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1553 enum_and_int_p,
1554 different_types_p);
1556 if (result != 1 && !DECL_NAME (s1))
1557 continue;
1558 if (result == 0)
1560 tu->val = 0;
1561 return 0;
1563 if (result == 2)
1564 needs_warning = true;
1566 if (TREE_CODE (s1) == FIELD_DECL
1567 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1568 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1569 break;
1571 ok = true;
1572 break;
1574 if (!ok)
1576 tu->val = 0;
1577 return 0;
1580 tu->val = needs_warning ? 2 : 1;
1581 return tu->val;
1584 case RECORD_TYPE:
1586 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1588 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1590 tu->val = 0;
1591 return 0;
1594 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1595 s1 && s2;
1596 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1598 int result;
1599 if (TREE_CODE (s1) != TREE_CODE (s2)
1600 || DECL_NAME (s1) != DECL_NAME (s2))
1601 break;
1602 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1603 enum_and_int_p, different_types_p);
1604 if (result == 0)
1605 break;
1606 if (result == 2)
1607 needs_warning = true;
1609 if (TREE_CODE (s1) == FIELD_DECL
1610 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1611 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1612 break;
1614 if (s1 && s2)
1615 tu->val = 0;
1616 else
1617 tu->val = needs_warning ? 2 : 1;
1618 return tu->val;
1621 default:
1622 gcc_unreachable ();
1626 /* Return 1 if two function types F1 and F2 are compatible.
1627 If either type specifies no argument types,
1628 the other must specify a fixed number of self-promoting arg types.
1629 Otherwise, if one type specifies only the number of arguments,
1630 the other must specify that number of self-promoting arg types.
1631 Otherwise, the argument types must match.
1632 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1634 static int
1635 function_types_compatible_p (const_tree f1, const_tree f2,
1636 bool *enum_and_int_p, bool *different_types_p)
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, enum_and_int_p, different_types_p);
1658 if (val == 0)
1659 return 0;
1661 args1 = TYPE_ARG_TYPES (f1);
1662 args2 = TYPE_ARG_TYPES (f2);
1664 if (different_types_p != NULL
1665 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1666 *different_types_p = true;
1668 /* An unspecified parmlist matches any specified parmlist
1669 whose argument types don't need default promotions. */
1671 if (args1 == NULL_TREE)
1673 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
1674 return 0;
1675 if (!self_promoting_args_p (args2))
1676 return 0;
1677 /* If one of these types comes from a non-prototype fn definition,
1678 compare that with the other type's arglist.
1679 If they don't match, ask for a warning (but no error). */
1680 if (TYPE_ACTUAL_ARG_TYPES (f1)
1681 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1682 enum_and_int_p, different_types_p) != 1)
1683 val = 2;
1684 return val;
1686 if (args2 == NULL_TREE)
1688 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
1689 return 0;
1690 if (!self_promoting_args_p (args1))
1691 return 0;
1692 if (TYPE_ACTUAL_ARG_TYPES (f2)
1693 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1694 enum_and_int_p, different_types_p) != 1)
1695 val = 2;
1696 return val;
1699 /* Both types have argument lists: compare them and propagate results. */
1700 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1701 different_types_p);
1702 return val1 != 1 ? val1 : val;
1705 /* Check two lists of types for compatibility, returning 0 for
1706 incompatible, 1 for compatible, or 2 for compatible with
1707 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1708 comptypes_internal. */
1710 static int
1711 type_lists_compatible_p (const_tree args1, const_tree args2,
1712 bool *enum_and_int_p, bool *different_types_p)
1714 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1715 int val = 1;
1716 int newval = 0;
1718 while (1)
1720 tree a1, mv1, a2, mv2;
1721 if (args1 == NULL_TREE && args2 == NULL_TREE)
1722 return val;
1723 /* If one list is shorter than the other,
1724 they fail to match. */
1725 if (args1 == NULL_TREE || args2 == NULL_TREE)
1726 return 0;
1727 mv1 = a1 = TREE_VALUE (args1);
1728 mv2 = a2 = TREE_VALUE (args2);
1729 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1730 mv1 = (TYPE_ATOMIC (mv1)
1731 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1732 TYPE_QUAL_ATOMIC)
1733 : TYPE_MAIN_VARIANT (mv1));
1734 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1735 mv2 = (TYPE_ATOMIC (mv2)
1736 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1737 TYPE_QUAL_ATOMIC)
1738 : TYPE_MAIN_VARIANT (mv2));
1739 /* A null pointer instead of a type
1740 means there is supposed to be an argument
1741 but nothing is specified about what type it has.
1742 So match anything that self-promotes. */
1743 if (different_types_p != NULL
1744 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1745 *different_types_p = true;
1746 if (a1 == NULL_TREE)
1748 if (c_type_promotes_to (a2) != a2)
1749 return 0;
1751 else if (a2 == NULL_TREE)
1753 if (c_type_promotes_to (a1) != a1)
1754 return 0;
1756 /* If one of the lists has an error marker, ignore this arg. */
1757 else if (TREE_CODE (a1) == ERROR_MARK
1758 || TREE_CODE (a2) == ERROR_MARK)
1760 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1761 different_types_p)))
1763 if (different_types_p != NULL)
1764 *different_types_p = true;
1765 /* Allow wait (union {union wait *u; int *i} *)
1766 and wait (union wait *) to be compatible. */
1767 if (TREE_CODE (a1) == UNION_TYPE
1768 && (TYPE_NAME (a1) == NULL_TREE
1769 || TYPE_TRANSPARENT_AGGR (a1))
1770 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1771 && tree_int_cst_equal (TYPE_SIZE (a1),
1772 TYPE_SIZE (a2)))
1774 tree memb;
1775 for (memb = TYPE_FIELDS (a1);
1776 memb; memb = DECL_CHAIN (memb))
1778 tree mv3 = TREE_TYPE (memb);
1779 if (mv3 && mv3 != error_mark_node
1780 && TREE_CODE (mv3) != ARRAY_TYPE)
1781 mv3 = (TYPE_ATOMIC (mv3)
1782 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1783 TYPE_QUAL_ATOMIC)
1784 : TYPE_MAIN_VARIANT (mv3));
1785 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1786 different_types_p))
1787 break;
1789 if (memb == NULL_TREE)
1790 return 0;
1792 else if (TREE_CODE (a2) == UNION_TYPE
1793 && (TYPE_NAME (a2) == NULL_TREE
1794 || TYPE_TRANSPARENT_AGGR (a2))
1795 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1796 && tree_int_cst_equal (TYPE_SIZE (a2),
1797 TYPE_SIZE (a1)))
1799 tree memb;
1800 for (memb = TYPE_FIELDS (a2);
1801 memb; memb = DECL_CHAIN (memb))
1803 tree mv3 = TREE_TYPE (memb);
1804 if (mv3 && mv3 != error_mark_node
1805 && TREE_CODE (mv3) != ARRAY_TYPE)
1806 mv3 = (TYPE_ATOMIC (mv3)
1807 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1808 TYPE_QUAL_ATOMIC)
1809 : TYPE_MAIN_VARIANT (mv3));
1810 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1811 different_types_p))
1812 break;
1814 if (memb == NULL_TREE)
1815 return 0;
1817 else
1818 return 0;
1821 /* comptypes said ok, but record if it said to warn. */
1822 if (newval > val)
1823 val = newval;
1825 args1 = TREE_CHAIN (args1);
1826 args2 = TREE_CHAIN (args2);
1830 /* Compute the size to increment a pointer by. When a function type or void
1831 type or incomplete type is passed, size_one_node is returned.
1832 This function does not emit any diagnostics; the caller is responsible
1833 for that. */
1835 static tree
1836 c_size_in_bytes (const_tree type)
1838 enum tree_code code = TREE_CODE (type);
1840 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1841 || !COMPLETE_TYPE_P (type))
1842 return size_one_node;
1844 /* Convert in case a char is more than one unit. */
1845 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1846 size_int (TYPE_PRECISION (char_type_node)
1847 / BITS_PER_UNIT));
1850 /* Return either DECL or its known constant value (if it has one). */
1852 tree
1853 decl_constant_value_1 (tree decl, bool in_init)
1855 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1856 TREE_CODE (decl) != PARM_DECL
1857 && !TREE_THIS_VOLATILE (decl)
1858 && TREE_READONLY (decl)
1859 && DECL_INITIAL (decl) != NULL_TREE
1860 && !error_operand_p (DECL_INITIAL (decl))
1861 /* This is invalid if initial value is not constant.
1862 If it has either a function call, a memory reference,
1863 or a variable, then re-evaluating it could give different results. */
1864 && TREE_CONSTANT (DECL_INITIAL (decl))
1865 /* Check for cases where this is sub-optimal, even though valid. */
1866 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1867 return DECL_INITIAL (decl);
1868 return decl;
1871 /* Return either DECL or its known constant value (if it has one).
1872 Like the above, but always return decl outside of functions. */
1874 tree
1875 decl_constant_value (tree decl)
1877 /* Don't change a variable array bound or initial value to a constant
1878 in a place where a variable is invalid. */
1879 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1882 /* Convert the array expression EXP to a pointer. */
1883 static tree
1884 array_to_pointer_conversion (location_t loc, tree exp)
1886 tree orig_exp = exp;
1887 tree type = TREE_TYPE (exp);
1888 tree adr;
1889 tree restype = TREE_TYPE (type);
1890 tree ptrtype;
1892 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1894 STRIP_TYPE_NOPS (exp);
1896 copy_warning (exp, orig_exp);
1898 ptrtype = build_pointer_type (restype);
1900 if (INDIRECT_REF_P (exp))
1901 return convert (ptrtype, TREE_OPERAND (exp, 0));
1903 /* In C++ array compound literals are temporary objects unless they are
1904 const or appear in namespace scope, so they are destroyed too soon
1905 to use them for much of anything (c++/53220). */
1906 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1908 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1909 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1910 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1911 "converting an array compound literal to a pointer "
1912 "is ill-formed in C++");
1915 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1916 return convert (ptrtype, adr);
1919 /* Convert the function expression EXP to a pointer. */
1920 static tree
1921 function_to_pointer_conversion (location_t loc, tree exp)
1923 tree orig_exp = exp;
1925 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1927 STRIP_TYPE_NOPS (exp);
1929 copy_warning (exp, orig_exp);
1931 return build_unary_op (loc, ADDR_EXPR, exp, false);
1934 /* Mark EXP as read, not just set, for set but not used -Wunused
1935 warning purposes. */
1937 void
1938 mark_exp_read (tree exp)
1940 switch (TREE_CODE (exp))
1942 case VAR_DECL:
1943 case PARM_DECL:
1944 DECL_READ_P (exp) = 1;
1945 break;
1946 case ARRAY_REF:
1947 case COMPONENT_REF:
1948 case MODIFY_EXPR:
1949 case REALPART_EXPR:
1950 case IMAGPART_EXPR:
1951 CASE_CONVERT:
1952 case ADDR_EXPR:
1953 case VIEW_CONVERT_EXPR:
1954 mark_exp_read (TREE_OPERAND (exp, 0));
1955 break;
1956 case COMPOUND_EXPR:
1957 /* Pattern match what build_atomic_assign produces with modifycode
1958 NOP_EXPR. */
1959 if (VAR_P (TREE_OPERAND (exp, 1))
1960 && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
1961 && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
1963 tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1964 tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
1965 if (TREE_CODE (t1) == TARGET_EXPR
1966 && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
1967 && TREE_CODE (t2) == CALL_EXPR)
1969 tree fndecl = get_callee_fndecl (t2);
1970 tree arg = NULL_TREE;
1971 if (fndecl
1972 && TREE_CODE (fndecl) == FUNCTION_DECL
1973 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1974 && call_expr_nargs (t2) >= 2)
1975 switch (DECL_FUNCTION_CODE (fndecl))
1977 case BUILT_IN_ATOMIC_STORE:
1978 arg = CALL_EXPR_ARG (t2, 1);
1979 break;
1980 case BUILT_IN_ATOMIC_STORE_1:
1981 case BUILT_IN_ATOMIC_STORE_2:
1982 case BUILT_IN_ATOMIC_STORE_4:
1983 case BUILT_IN_ATOMIC_STORE_8:
1984 case BUILT_IN_ATOMIC_STORE_16:
1985 arg = CALL_EXPR_ARG (t2, 0);
1986 break;
1987 default:
1988 break;
1990 if (arg)
1992 STRIP_NOPS (arg);
1993 if (TREE_CODE (arg) == ADDR_EXPR
1994 && DECL_P (TREE_OPERAND (arg, 0))
1995 && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
1996 mark_exp_read (TREE_OPERAND (arg, 0));
2000 /* FALLTHRU */
2001 case C_MAYBE_CONST_EXPR:
2002 mark_exp_read (TREE_OPERAND (exp, 1));
2003 break;
2004 default:
2005 break;
2009 /* Perform the default conversion of arrays and functions to pointers.
2010 Return the result of converting EXP. For any other expression, just
2011 return EXP.
2013 LOC is the location of the expression. */
2015 struct c_expr
2016 default_function_array_conversion (location_t loc, struct c_expr exp)
2018 tree orig_exp = exp.value;
2019 tree type = TREE_TYPE (exp.value);
2020 enum tree_code code = TREE_CODE (type);
2022 switch (code)
2024 case ARRAY_TYPE:
2026 bool not_lvalue = false;
2027 bool lvalue_array_p;
2029 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2030 || CONVERT_EXPR_P (exp.value))
2031 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2033 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2034 not_lvalue = true;
2035 exp.value = TREE_OPERAND (exp.value, 0);
2038 copy_warning (exp.value, orig_exp);
2040 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2041 if (!flag_isoc99 && !lvalue_array_p)
2043 /* Before C99, non-lvalue arrays do not decay to pointers.
2044 Normally, using such an array would be invalid; but it can
2045 be used correctly inside sizeof or as a statement expression.
2046 Thus, do not give an error here; an error will result later. */
2047 return exp;
2050 exp.value = array_to_pointer_conversion (loc, exp.value);
2052 break;
2053 case FUNCTION_TYPE:
2054 exp.value = function_to_pointer_conversion (loc, exp.value);
2055 break;
2056 default:
2057 break;
2060 return exp;
2063 struct c_expr
2064 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2066 mark_exp_read (exp.value);
2067 return default_function_array_conversion (loc, exp);
2070 /* Return whether EXPR should be treated as an atomic lvalue for the
2071 purposes of load and store handling. */
2073 static bool
2074 really_atomic_lvalue (tree expr)
2076 if (error_operand_p (expr))
2077 return false;
2078 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2079 return false;
2080 if (!lvalue_p (expr))
2081 return false;
2083 /* Ignore _Atomic on register variables, since their addresses can't
2084 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2085 sequences wouldn't work. Ignore _Atomic on structures containing
2086 bit-fields, since accessing elements of atomic structures or
2087 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2088 it's undefined at translation time or execution time, and the
2089 normal atomic sequences again wouldn't work. */
2090 while (handled_component_p (expr))
2092 if (TREE_CODE (expr) == COMPONENT_REF
2093 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2094 return false;
2095 expr = TREE_OPERAND (expr, 0);
2097 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2098 return false;
2099 return true;
2102 /* If EXPR is a named constant (C2x) derived from a constexpr variable
2103 - that is, a reference to such a variable, or a member extracted by
2104 a sequence of structure and union (but not array) member accesses
2105 (where union member accesses must access the same member as
2106 initialized) - then return the corresponding initializer;
2107 otherwise, return NULL_TREE. */
2109 static tree
2110 maybe_get_constexpr_init (tree expr)
2112 tree decl = NULL_TREE;
2113 if (TREE_CODE (expr) == VAR_DECL)
2114 decl = expr;
2115 else if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2116 decl = COMPOUND_LITERAL_EXPR_DECL (expr);
2117 if (decl
2118 && C_DECL_DECLARED_CONSTEXPR (decl)
2119 && DECL_INITIAL (decl) != NULL_TREE
2120 && !error_operand_p (DECL_INITIAL (decl)))
2121 return DECL_INITIAL (decl);
2122 if (TREE_CODE (expr) != COMPONENT_REF)
2123 return NULL_TREE;
2124 tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0));
2125 if (inner == NULL_TREE)
2126 return NULL_TREE;
2127 while ((CONVERT_EXPR_P (inner) || TREE_CODE (inner) == NON_LVALUE_EXPR)
2128 && !error_operand_p (inner)
2129 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
2130 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (inner, 0)))))
2131 inner = TREE_OPERAND (inner, 0);
2132 if (TREE_CODE (inner) != CONSTRUCTOR)
2133 return NULL_TREE;
2134 tree field = TREE_OPERAND (expr, 1);
2135 unsigned HOST_WIDE_INT cidx;
2136 tree cfield, cvalue;
2137 bool have_other_init = false;
2138 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner), cidx, cfield, cvalue)
2140 if (cfield == field)
2141 return cvalue;
2142 have_other_init = true;
2144 if (TREE_CODE (TREE_TYPE (inner)) == UNION_TYPE
2145 && (have_other_init || field != TYPE_FIELDS (TREE_TYPE (inner))))
2146 return NULL_TREE;
2147 /* Return a default initializer. */
2148 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (expr)))
2149 return build_constructor (TREE_TYPE (expr), NULL);
2150 return build_zero_cst (TREE_TYPE (expr));
2153 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2154 including converting functions and arrays to pointers if CONVERT_P.
2155 If READ_P, also mark the expression as having been read. If
2156 FOR_INIT, constexpr expressions of structure and union type should
2157 be replaced by the corresponding CONSTRUCTOR; otherwise, only
2158 constexpr scalars (including elements of structures and unions) are
2159 replaced by their initializers. */
2161 struct c_expr
2162 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2163 bool convert_p, bool read_p, bool for_init)
2165 bool force_non_npc = false;
2166 if (read_p)
2167 mark_exp_read (exp.value);
2168 if (convert_p)
2169 exp = default_function_array_conversion (loc, exp);
2170 if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2171 exp.value = require_complete_type (loc, exp.value);
2172 if (for_init || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp.value)))
2174 tree init = maybe_get_constexpr_init (exp.value);
2175 if (init != NULL_TREE)
2177 /* A named constant of pointer type or type nullptr_t is not
2178 a null pointer constant even if the initializer is
2179 one. */
2180 if (TREE_CODE (init) == INTEGER_CST
2181 && !INTEGRAL_TYPE_P (TREE_TYPE (init))
2182 && integer_zerop (init))
2183 force_non_npc = true;
2184 exp.value = init;
2187 if (really_atomic_lvalue (exp.value))
2189 vec<tree, va_gc> *params;
2190 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2191 tree expr_type = TREE_TYPE (exp.value);
2192 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2193 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2195 gcc_assert (TYPE_ATOMIC (expr_type));
2197 /* Expansion of a generic atomic load may require an addition
2198 element, so allocate enough to prevent a resize. */
2199 vec_alloc (params, 4);
2201 /* Remove the qualifiers for the rest of the expressions and
2202 create the VAL temp variable to hold the RHS. */
2203 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2204 tmp = create_tmp_var_raw (nonatomic_type);
2205 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2206 TREE_ADDRESSABLE (tmp) = 1;
2207 /* Do not disable warnings for TMP even though it's artificial.
2208 -Winvalid-memory-model depends on it. */
2210 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2211 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2212 params->quick_push (expr_addr);
2213 params->quick_push (tmp_addr);
2214 params->quick_push (seq_cst);
2215 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2217 /* EXPR is always read. */
2218 mark_exp_read (exp.value);
2220 /* Return tmp which contains the value loaded. */
2221 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2222 NULL_TREE, NULL_TREE);
2224 if (convert_p && !error_operand_p (exp.value)
2225 && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2226 exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2227 if (force_non_npc)
2228 exp.value = build1 (NOP_EXPR, TREE_TYPE (exp.value), exp.value);
2229 return exp;
2232 /* EXP is an expression of integer type. Apply the integer promotions
2233 to it and return the promoted value. */
2235 tree
2236 perform_integral_promotions (tree exp)
2238 tree type = TREE_TYPE (exp);
2239 enum tree_code code = TREE_CODE (type);
2241 gcc_assert (INTEGRAL_TYPE_P (type));
2243 /* Convert enums to the result of applying the integer promotions to
2244 their underlying type. */
2245 if (code == ENUMERAL_TYPE)
2247 type = ENUM_UNDERLYING_TYPE (type);
2248 if (c_promoting_integer_type_p (type))
2250 if (TYPE_UNSIGNED (type)
2251 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2252 type = unsigned_type_node;
2253 else
2254 type = integer_type_node;
2257 return convert (type, exp);
2260 /* ??? This should no longer be needed now bit-fields have their
2261 proper types. */
2262 if (TREE_CODE (exp) == COMPONENT_REF
2263 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2264 /* If it's thinner than an int, promote it like a
2265 c_promoting_integer_type_p, otherwise leave it alone. */
2266 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2267 TYPE_PRECISION (integer_type_node)) < 0)
2268 return convert (integer_type_node, exp);
2270 if (c_promoting_integer_type_p (type))
2272 /* Preserve unsignedness if not really getting any wider. */
2273 if (TYPE_UNSIGNED (type)
2274 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2275 return convert (unsigned_type_node, exp);
2277 return convert (integer_type_node, exp);
2280 return exp;
2284 /* Perform default promotions for C data used in expressions.
2285 Enumeral types or short or char are converted to int.
2286 In addition, manifest constants symbols are replaced by their values. */
2288 tree
2289 default_conversion (tree exp)
2291 tree orig_exp;
2292 tree type = TREE_TYPE (exp);
2293 enum tree_code code = TREE_CODE (type);
2294 tree promoted_type;
2296 mark_exp_read (exp);
2298 /* Functions and arrays have been converted during parsing. */
2299 gcc_assert (code != FUNCTION_TYPE);
2300 if (code == ARRAY_TYPE)
2301 return exp;
2303 /* Constants can be used directly unless they're not loadable. */
2304 if (TREE_CODE (exp) == CONST_DECL)
2305 exp = DECL_INITIAL (exp);
2307 /* Strip no-op conversions. */
2308 orig_exp = exp;
2309 STRIP_TYPE_NOPS (exp);
2311 copy_warning (exp, orig_exp);
2313 if (code == VOID_TYPE)
2315 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2316 "void value not ignored as it ought to be");
2317 return error_mark_node;
2320 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2321 if (exp == error_mark_node)
2322 return error_mark_node;
2324 promoted_type = targetm.promoted_type (type);
2325 if (promoted_type)
2326 return convert (promoted_type, exp);
2328 if (INTEGRAL_TYPE_P (type))
2329 return perform_integral_promotions (exp);
2331 return exp;
2334 /* Look up COMPONENT in a structure or union TYPE.
2336 If the component name is not found, returns NULL_TREE. Otherwise,
2337 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2338 stepping down the chain to the component, which is in the last
2339 TREE_VALUE of the list. Normally the list is of length one, but if
2340 the component is embedded within (nested) anonymous structures or
2341 unions, the list steps down the chain to the component. */
2343 static tree
2344 lookup_field (tree type, tree component)
2346 tree field;
2348 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2349 to the field elements. Use a binary search on this array to quickly
2350 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2351 will always be set for structures which have many elements.
2353 Duplicate field checking replaces duplicates with NULL_TREE so
2354 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2355 case just iterate using DECL_CHAIN. */
2357 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2358 && !seen_error ())
2360 int bot, top, half;
2361 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2363 field = TYPE_FIELDS (type);
2364 bot = 0;
2365 top = TYPE_LANG_SPECIFIC (type)->s->len;
2366 while (top - bot > 1)
2368 half = (top - bot + 1) >> 1;
2369 field = field_array[bot+half];
2371 if (DECL_NAME (field) == NULL_TREE)
2373 /* Step through all anon unions in linear fashion. */
2374 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2376 field = field_array[bot++];
2377 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2379 tree anon = lookup_field (TREE_TYPE (field), component);
2381 if (anon)
2382 return tree_cons (NULL_TREE, field, anon);
2384 /* The Plan 9 compiler permits referring
2385 directly to an anonymous struct/union field
2386 using a typedef name. */
2387 if (flag_plan9_extensions
2388 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2389 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2390 == TYPE_DECL)
2391 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2392 == component))
2393 break;
2397 /* Entire record is only anon unions. */
2398 if (bot > top)
2399 return NULL_TREE;
2401 /* Restart the binary search, with new lower bound. */
2402 continue;
2405 if (DECL_NAME (field) == component)
2406 break;
2407 if (DECL_NAME (field) < component)
2408 bot += half;
2409 else
2410 top = bot + half;
2413 if (DECL_NAME (field_array[bot]) == component)
2414 field = field_array[bot];
2415 else if (DECL_NAME (field) != component)
2416 return NULL_TREE;
2418 else
2420 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2422 if (DECL_NAME (field) == NULL_TREE
2423 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2425 tree anon = lookup_field (TREE_TYPE (field), component);
2427 if (anon)
2428 return tree_cons (NULL_TREE, field, anon);
2430 /* The Plan 9 compiler permits referring directly to an
2431 anonymous struct/union field using a typedef
2432 name. */
2433 if (flag_plan9_extensions
2434 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2435 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2436 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2437 == component))
2438 break;
2441 if (DECL_NAME (field) == component)
2442 break;
2445 if (field == NULL_TREE)
2446 return NULL_TREE;
2449 return tree_cons (NULL_TREE, field, NULL_TREE);
2452 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2454 static void
2455 lookup_field_fuzzy_find_candidates (tree type, tree component,
2456 vec<tree> *candidates)
2458 tree field;
2459 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2461 if (DECL_NAME (field) == NULL_TREE
2462 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2463 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2464 candidates);
2466 if (DECL_NAME (field))
2467 candidates->safe_push (DECL_NAME (field));
2471 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2472 rather than returning a TREE_LIST for an exact match. */
2474 static tree
2475 lookup_field_fuzzy (tree type, tree component)
2477 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2479 /* First, gather a list of candidates. */
2480 auto_vec <tree> candidates;
2482 lookup_field_fuzzy_find_candidates (type, component,
2483 &candidates);
2485 return find_closest_identifier (component, &candidates);
2488 /* Support function for build_component_ref's error-handling.
2490 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2491 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2493 static bool
2494 should_suggest_deref_p (tree datum_type)
2496 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2497 allows "." for ptrs; we could be handling a failed attempt
2498 to access a property. */
2499 if (c_dialect_objc ())
2500 return false;
2502 /* Only suggest it for pointers... */
2503 if (TREE_CODE (datum_type) != POINTER_TYPE)
2504 return false;
2506 /* ...to structs/unions. */
2507 tree underlying_type = TREE_TYPE (datum_type);
2508 enum tree_code code = TREE_CODE (underlying_type);
2509 if (code == RECORD_TYPE || code == UNION_TYPE)
2510 return true;
2511 else
2512 return false;
2515 /* Make an expression to refer to the COMPONENT field of structure or
2516 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2517 location of the COMPONENT_REF. COMPONENT_LOC is the location
2518 of COMPONENT. ARROW_LOC is the location of the first -> operand if
2519 it is from -> operator. */
2521 tree
2522 build_component_ref (location_t loc, tree datum, tree component,
2523 location_t component_loc, location_t arrow_loc)
2525 tree type = TREE_TYPE (datum);
2526 enum tree_code code = TREE_CODE (type);
2527 tree field = NULL;
2528 tree ref;
2529 bool datum_lvalue = lvalue_p (datum);
2531 if (!objc_is_public (datum, component))
2532 return error_mark_node;
2534 /* Detect Objective-C property syntax object.property. */
2535 if (c_dialect_objc ()
2536 && (ref = objc_maybe_build_component_ref (datum, component)))
2537 return ref;
2539 /* See if there is a field or component with name COMPONENT. */
2541 if (code == RECORD_TYPE || code == UNION_TYPE)
2543 if (!COMPLETE_TYPE_P (type))
2545 c_incomplete_type_error (loc, NULL_TREE, type);
2546 return error_mark_node;
2549 field = lookup_field (type, component);
2551 if (!field)
2553 tree guessed_id = lookup_field_fuzzy (type, component);
2554 if (guessed_id)
2556 /* Attempt to provide a fixit replacement hint, if
2557 we have a valid range for the component. */
2558 location_t reported_loc
2559 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2560 gcc_rich_location rich_loc (reported_loc);
2561 if (component_loc != UNKNOWN_LOCATION)
2562 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2563 error_at (&rich_loc,
2564 "%qT has no member named %qE; did you mean %qE?",
2565 type, component, guessed_id);
2567 else
2568 error_at (loc, "%qT has no member named %qE", type, component);
2569 return error_mark_node;
2572 /* Accessing elements of atomic structures or unions is undefined
2573 behavior (C11 6.5.2.3#5). */
2574 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2576 if (code == RECORD_TYPE)
2577 warning_at (loc, 0, "accessing a member %qE of an atomic "
2578 "structure %qE", component, datum);
2579 else
2580 warning_at (loc, 0, "accessing a member %qE of an atomic "
2581 "union %qE", component, datum);
2584 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2585 This might be better solved in future the way the C++ front
2586 end does it - by giving the anonymous entities each a
2587 separate name and type, and then have build_component_ref
2588 recursively call itself. We can't do that here. */
2591 tree subdatum = TREE_VALUE (field);
2592 int quals;
2593 tree subtype;
2594 bool use_datum_quals;
2596 if (TREE_TYPE (subdatum) == error_mark_node)
2597 return error_mark_node;
2599 /* If this is an rvalue, it does not have qualifiers in C
2600 standard terms and we must avoid propagating such
2601 qualifiers down to a non-lvalue array that is then
2602 converted to a pointer. */
2603 use_datum_quals = (datum_lvalue
2604 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2606 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2607 if (use_datum_quals)
2608 quals |= TYPE_QUALS (TREE_TYPE (datum));
2609 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2611 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2612 NULL_TREE);
2613 SET_EXPR_LOCATION (ref, loc);
2614 if (TREE_READONLY (subdatum)
2615 || (use_datum_quals && TREE_READONLY (datum)))
2616 TREE_READONLY (ref) = 1;
2617 if (TREE_THIS_VOLATILE (subdatum)
2618 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2619 TREE_THIS_VOLATILE (ref) = 1;
2621 if (TREE_UNAVAILABLE (subdatum))
2622 error_unavailable_use (subdatum, NULL_TREE);
2623 else if (TREE_DEPRECATED (subdatum))
2624 warn_deprecated_use (subdatum, NULL_TREE);
2626 datum = ref;
2628 field = TREE_CHAIN (field);
2630 while (field);
2632 return ref;
2634 else if (should_suggest_deref_p (type))
2636 /* Special-case the error message for "ptr.field" for the case
2637 where the user has confused "." vs "->". */
2638 rich_location richloc (line_table, loc);
2639 if (INDIRECT_REF_P (datum) && arrow_loc != UNKNOWN_LOCATION)
2641 richloc.add_fixit_insert_before (arrow_loc, "(*");
2642 richloc.add_fixit_insert_after (arrow_loc, ")");
2643 error_at (&richloc,
2644 "%qE is a pointer to pointer; did you mean to dereference "
2645 "it before applying %<->%> to it?",
2646 TREE_OPERAND (datum, 0));
2648 else
2650 /* "loc" should be the "." token. */
2651 richloc.add_fixit_replace ("->");
2652 error_at (&richloc,
2653 "%qE is a pointer; did you mean to use %<->%>?",
2654 datum);
2656 return error_mark_node;
2658 else if (code != ERROR_MARK)
2659 error_at (loc,
2660 "request for member %qE in something not a structure or union",
2661 component);
2663 return error_mark_node;
2666 /* Given an expression PTR for a pointer, return an expression
2667 for the value pointed to.
2668 ERRORSTRING is the name of the operator to appear in error messages.
2670 LOC is the location to use for the generated tree. */
2672 tree
2673 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2675 tree pointer = default_conversion (ptr);
2676 tree type = TREE_TYPE (pointer);
2677 tree ref;
2679 if (TREE_CODE (type) == POINTER_TYPE)
2681 if (CONVERT_EXPR_P (pointer)
2682 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2684 /* If a warning is issued, mark it to avoid duplicates from
2685 the backend. This only needs to be done at
2686 warn_strict_aliasing > 2. */
2687 if (warn_strict_aliasing > 2)
2688 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2689 type, TREE_OPERAND (pointer, 0)))
2690 suppress_warning (pointer, OPT_Wstrict_aliasing_);
2693 if (TREE_CODE (pointer) == ADDR_EXPR
2694 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2695 == TREE_TYPE (type)))
2697 ref = TREE_OPERAND (pointer, 0);
2698 protected_set_expr_location (ref, loc);
2699 return ref;
2701 else
2703 tree t = TREE_TYPE (type);
2705 ref = build1 (INDIRECT_REF, t, pointer);
2707 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2708 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2710 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2711 so that we get the proper error message if the result is used
2712 to assign to. Also, &* is supposed to be a no-op.
2713 And ANSI C seems to specify that the type of the result
2714 should be the const type. */
2715 /* A de-reference of a pointer to const is not a const. It is valid
2716 to change it via some other pointer. */
2717 TREE_READONLY (ref) = TYPE_READONLY (t);
2718 TREE_SIDE_EFFECTS (ref)
2719 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2720 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2721 protected_set_expr_location (ref, loc);
2722 return ref;
2725 else if (TREE_CODE (pointer) != ERROR_MARK)
2726 invalid_indirection_error (loc, type, errstring);
2728 return error_mark_node;
2731 /* This handles expressions of the form "a[i]", which denotes
2732 an array reference.
2734 This is logically equivalent in C to *(a+i), but we may do it differently.
2735 If A is a variable or a member, we generate a primitive ARRAY_REF.
2736 This avoids forcing the array out of registers, and can work on
2737 arrays that are not lvalues (for example, members of structures returned
2738 by functions).
2740 For vector types, allow vector[i] but not i[vector], and create
2741 *(((type*)&vectortype) + i) for the expression.
2743 LOC is the location to use for the returned expression. */
2745 tree
2746 build_array_ref (location_t loc, tree array, tree index)
2748 tree ret;
2749 bool swapped = false;
2750 if (TREE_TYPE (array) == error_mark_node
2751 || TREE_TYPE (index) == error_mark_node)
2752 return error_mark_node;
2754 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2755 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2756 /* Allow vector[index] but not index[vector]. */
2757 && !gnu_vector_type_p (TREE_TYPE (array)))
2759 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2760 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2762 error_at (loc,
2763 "subscripted value is neither array nor pointer nor vector");
2765 return error_mark_node;
2767 std::swap (array, index);
2768 swapped = true;
2771 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2773 error_at (loc, "array subscript is not an integer");
2774 return error_mark_node;
2777 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2779 error_at (loc, "subscripted value is pointer to function");
2780 return error_mark_node;
2783 /* ??? Existing practice has been to warn only when the char
2784 index is syntactically the index, not for char[array]. */
2785 if (!swapped)
2786 warn_array_subscript_with_type_char (loc, index);
2788 /* Apply default promotions *after* noticing character types. */
2789 index = default_conversion (index);
2790 if (index == error_mark_node)
2791 return error_mark_node;
2793 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2795 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2796 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2798 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2800 tree rval, type;
2802 /* An array that is indexed by a non-constant
2803 cannot be stored in a register; we must be able to do
2804 address arithmetic on its address.
2805 Likewise an array of elements of variable size. */
2806 if (TREE_CODE (index) != INTEGER_CST
2807 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2808 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2810 if (!c_mark_addressable (array, true))
2811 return error_mark_node;
2813 /* An array that is indexed by a constant value which is not within
2814 the array bounds cannot be stored in a register either; because we
2815 would get a crash in store_bit_field/extract_bit_field when trying
2816 to access a non-existent part of the register. */
2817 if (TREE_CODE (index) == INTEGER_CST
2818 && TYPE_DOMAIN (TREE_TYPE (array))
2819 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2821 if (!c_mark_addressable (array))
2822 return error_mark_node;
2825 if ((pedantic || warn_c90_c99_compat)
2826 && ! was_vector)
2828 tree foo = array;
2829 while (TREE_CODE (foo) == COMPONENT_REF)
2830 foo = TREE_OPERAND (foo, 0);
2831 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2832 pedwarn (loc, OPT_Wpedantic,
2833 "ISO C forbids subscripting %<register%> array");
2834 else if (!lvalue_p (foo))
2835 pedwarn_c90 (loc, OPT_Wpedantic,
2836 "ISO C90 forbids subscripting non-lvalue "
2837 "array");
2840 type = TREE_TYPE (TREE_TYPE (array));
2841 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2842 /* Array ref is const/volatile if the array elements are
2843 or if the array is. */
2844 TREE_READONLY (rval)
2845 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2846 | TREE_READONLY (array));
2847 TREE_SIDE_EFFECTS (rval)
2848 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2849 | TREE_SIDE_EFFECTS (array));
2850 TREE_THIS_VOLATILE (rval)
2851 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2852 /* This was added by rms on 16 Nov 91.
2853 It fixes vol struct foo *a; a->elts[1]
2854 in an inline function.
2855 Hope it doesn't break something else. */
2856 | TREE_THIS_VOLATILE (array));
2857 ret = require_complete_type (loc, rval);
2858 protected_set_expr_location (ret, loc);
2859 if (non_lvalue)
2860 ret = non_lvalue_loc (loc, ret);
2861 return ret;
2863 else
2865 tree ar = default_conversion (array);
2867 if (ar == error_mark_node)
2868 return ar;
2870 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2871 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2873 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2874 index, false),
2875 RO_ARRAY_INDEXING);
2876 if (non_lvalue)
2877 ret = non_lvalue_loc (loc, ret);
2878 return ret;
2882 /* Build an external reference to identifier ID. FUN indicates
2883 whether this will be used for a function call. LOC is the source
2884 location of the identifier. This sets *TYPE to the type of the
2885 identifier, which is not the same as the type of the returned value
2886 for CONST_DECLs defined as enum constants. If the type of the
2887 identifier is not available, *TYPE is set to NULL. */
2888 tree
2889 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2891 tree ref;
2892 tree decl = lookup_name (id);
2894 /* In Objective-C, an instance variable (ivar) may be preferred to
2895 whatever lookup_name() found. */
2896 decl = objc_lookup_ivar (decl, id);
2898 *type = NULL;
2899 if (decl && decl != error_mark_node)
2901 ref = decl;
2902 *type = TREE_TYPE (ref);
2903 if (DECL_P (decl) && C_DECL_UNDERSPECIFIED (decl))
2904 error_at (loc, "underspecified %qD referenced in its initializer",
2905 decl);
2907 else if (fun)
2908 /* Implicit function declaration. */
2909 ref = implicitly_declare (loc, id);
2910 else if (decl == error_mark_node)
2911 /* Don't complain about something that's already been
2912 complained about. */
2913 return error_mark_node;
2914 else
2916 undeclared_variable (loc, id);
2917 return error_mark_node;
2920 if (TREE_TYPE (ref) == error_mark_node)
2921 return error_mark_node;
2923 if (TREE_UNAVAILABLE (ref))
2924 error_unavailable_use (ref, NULL_TREE);
2925 else if (TREE_DEPRECATED (ref))
2926 warn_deprecated_use (ref, NULL_TREE);
2928 /* Recursive call does not count as usage. */
2929 if (ref != current_function_decl)
2931 TREE_USED (ref) = 1;
2934 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2936 if (!in_sizeof && !in_typeof)
2937 C_DECL_USED (ref) = 1;
2938 else if (DECL_INITIAL (ref) == NULL_TREE
2939 && DECL_EXTERNAL (ref)
2940 && !TREE_PUBLIC (ref))
2941 record_maybe_used_decl (ref);
2944 if (TREE_CODE (ref) == CONST_DECL)
2946 used_types_insert (TREE_TYPE (ref));
2948 if (warn_cxx_compat
2949 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2950 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2952 warning_at (loc, OPT_Wc___compat,
2953 ("enum constant defined in struct or union "
2954 "is not visible in C++"));
2955 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2958 ref = DECL_INITIAL (ref);
2959 TREE_CONSTANT (ref) = 1;
2961 else if (current_function_decl != NULL_TREE
2962 && !DECL_FILE_SCOPE_P (current_function_decl)
2963 && (VAR_OR_FUNCTION_DECL_P (ref)
2964 || TREE_CODE (ref) == PARM_DECL))
2966 tree context = decl_function_context (ref);
2968 if (context != NULL_TREE && context != current_function_decl)
2969 DECL_NONLOCAL (ref) = 1;
2971 /* C99 6.7.4p3: An inline definition of a function with external
2972 linkage ... shall not contain a reference to an identifier with
2973 internal linkage. */
2974 else if (current_function_decl != NULL_TREE
2975 && DECL_DECLARED_INLINE_P (current_function_decl)
2976 && DECL_EXTERNAL (current_function_decl)
2977 && VAR_OR_FUNCTION_DECL_P (ref)
2978 && (!VAR_P (ref) || TREE_STATIC (ref))
2979 && ! TREE_PUBLIC (ref)
2980 && DECL_CONTEXT (ref) != current_function_decl)
2981 record_inline_static (loc, current_function_decl, ref,
2982 csi_internal);
2984 return ref;
2987 /* Record details of decls possibly used inside sizeof or typeof. */
2988 struct maybe_used_decl
2990 /* The decl. */
2991 tree decl;
2992 /* The level seen at (in_sizeof + in_typeof). */
2993 int level;
2994 /* The next one at this level or above, or NULL. */
2995 struct maybe_used_decl *next;
2998 static struct maybe_used_decl *maybe_used_decls;
3000 /* Record that DECL, an undefined static function reference seen
3001 inside sizeof or typeof, might be used if the operand of sizeof is
3002 a VLA type or the operand of typeof is a variably modified
3003 type. */
3005 static void
3006 record_maybe_used_decl (tree decl)
3008 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
3009 t->decl = decl;
3010 t->level = in_sizeof + in_typeof;
3011 t->next = maybe_used_decls;
3012 maybe_used_decls = t;
3015 /* Pop the stack of decls possibly used inside sizeof or typeof. If
3016 USED is false, just discard them. If it is true, mark them used
3017 (if no longer inside sizeof or typeof) or move them to the next
3018 level up (if still inside sizeof or typeof). */
3020 void
3021 pop_maybe_used (bool used)
3023 struct maybe_used_decl *p = maybe_used_decls;
3024 int cur_level = in_sizeof + in_typeof;
3025 while (p && p->level > cur_level)
3027 if (used)
3029 if (cur_level == 0)
3030 C_DECL_USED (p->decl) = 1;
3031 else
3032 p->level = cur_level;
3034 p = p->next;
3036 if (!used || cur_level == 0)
3037 maybe_used_decls = p;
3040 /* Return the result of sizeof applied to EXPR. */
3042 struct c_expr
3043 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
3045 struct c_expr ret;
3046 if (expr.value == error_mark_node)
3048 ret.value = error_mark_node;
3049 ret.original_code = ERROR_MARK;
3050 ret.original_type = NULL;
3051 ret.m_decimal = 0;
3052 pop_maybe_used (false);
3054 else
3056 bool expr_const_operands = true;
3058 if (TREE_CODE (expr.value) == PARM_DECL
3059 && C_ARRAY_PARAMETER (expr.value))
3061 auto_diagnostic_group d;
3062 if (warning_at (loc, OPT_Wsizeof_array_argument,
3063 "%<sizeof%> on array function parameter %qE will "
3064 "return size of %qT", expr.value,
3065 TREE_TYPE (expr.value)))
3066 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
3068 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
3069 &expr_const_operands);
3070 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
3071 c_last_sizeof_arg = expr.value;
3072 c_last_sizeof_loc = loc;
3073 ret.original_code = SIZEOF_EXPR;
3074 ret.original_type = NULL;
3075 ret.m_decimal = 0;
3076 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
3078 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
3079 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3080 folded_expr, ret.value);
3081 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3082 SET_EXPR_LOCATION (ret.value, loc);
3084 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3086 return ret;
3089 /* Return the result of sizeof applied to T, a structure for the type
3090 name passed to sizeof (rather than the type itself). LOC is the
3091 location of the original expression. */
3093 struct c_expr
3094 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3096 tree type;
3097 struct c_expr ret;
3098 tree type_expr = NULL_TREE;
3099 bool type_expr_const = true;
3100 type = groktypename (t, &type_expr, &type_expr_const);
3101 ret.value = c_sizeof (loc, type);
3102 c_last_sizeof_arg = type;
3103 c_last_sizeof_loc = loc;
3104 ret.original_code = SIZEOF_EXPR;
3105 ret.original_type = NULL;
3106 ret.m_decimal = 0;
3107 if (type == error_mark_node)
3109 ret.value = error_mark_node;
3110 ret.original_code = ERROR_MARK;
3112 else
3113 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3114 && C_TYPE_VARIABLE_SIZE (type))
3116 /* If the type is a [*] array, it is a VLA but is represented as
3117 having a size of zero. In such a case we must ensure that
3118 the result of sizeof does not get folded to a constant by
3119 c_fully_fold, because if the size is evaluated the result is
3120 not constant and so constraints on zero or negative size
3121 arrays must not be applied when this sizeof call is inside
3122 another array declarator. */
3123 if (!type_expr)
3124 type_expr = integer_zero_node;
3125 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3126 type_expr, ret.value);
3127 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3129 pop_maybe_used (type != error_mark_node
3130 ? C_TYPE_VARIABLE_SIZE (type) : false);
3131 return ret;
3134 /* Build a function call to function FUNCTION with parameters PARAMS.
3135 The function call is at LOC.
3136 PARAMS is a list--a chain of TREE_LIST nodes--in which the
3137 TREE_VALUE of each node is a parameter-expression.
3138 FUNCTION's data type may be a function type or a pointer-to-function. */
3140 tree
3141 build_function_call (location_t loc, tree function, tree params)
3143 vec<tree, va_gc> *v;
3144 tree ret;
3146 vec_alloc (v, list_length (params));
3147 for (; params; params = TREE_CHAIN (params))
3148 v->quick_push (TREE_VALUE (params));
3149 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3150 vec_free (v);
3151 return ret;
3154 /* Give a note about the location of the declaration of DECL. */
3156 static void
3157 inform_declaration (tree decl)
3159 if (decl && (TREE_CODE (decl) != FUNCTION_DECL
3160 || !DECL_IS_UNDECLARED_BUILTIN (decl)))
3161 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3164 /* Build a function call to function FUNCTION with parameters PARAMS.
3165 If FUNCTION is the result of resolving an overloaded target built-in,
3166 ORIG_FUNDECL is the original function decl, otherwise it is null.
3167 ORIGTYPES, if not NULL, is a vector of types; each element is
3168 either NULL or the original type of the corresponding element in
3169 PARAMS. The original type may differ from TREE_TYPE of the
3170 parameter for enums. FUNCTION's data type may be a function type
3171 or pointer-to-function. This function changes the elements of
3172 PARAMS. */
3174 tree
3175 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3176 tree function, vec<tree, va_gc> *params,
3177 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3179 tree fntype, fundecl = NULL_TREE;
3180 tree name = NULL_TREE, result;
3181 tree tem;
3182 int nargs;
3183 tree *argarray;
3186 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3187 STRIP_TYPE_NOPS (function);
3189 /* Convert anything with function type to a pointer-to-function. */
3190 if (TREE_CODE (function) == FUNCTION_DECL)
3192 name = DECL_NAME (function);
3194 if (flag_tm)
3195 tm_malloc_replacement (function);
3196 fundecl = function;
3197 if (!orig_fundecl)
3198 orig_fundecl = fundecl;
3199 /* Atomic functions have type checking/casting already done. They are
3200 often rewritten and don't match the original parameter list. */
3201 if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
3202 origtypes = NULL;
3204 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3205 function = function_to_pointer_conversion (loc, function);
3207 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3208 expressions, like those used for ObjC messenger dispatches. */
3209 if (params && !params->is_empty ())
3210 function = objc_rewrite_function_call (function, (*params)[0]);
3212 function = c_fully_fold (function, false, NULL);
3214 fntype = TREE_TYPE (function);
3216 if (TREE_CODE (fntype) == ERROR_MARK)
3217 return error_mark_node;
3219 if (!(TREE_CODE (fntype) == POINTER_TYPE
3220 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3222 if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
3223 error_at (loc,
3224 "called object %qE is not a function or function pointer",
3225 function);
3226 else if (DECL_P (function))
3228 error_at (loc,
3229 "called object %qD is not a function or function pointer",
3230 function);
3231 inform_declaration (function);
3233 else
3234 error_at (loc,
3235 "called object is not a function or function pointer");
3236 return error_mark_node;
3239 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3240 current_function_returns_abnormally = 1;
3242 /* fntype now gets the type of function pointed to. */
3243 fntype = TREE_TYPE (fntype);
3244 tree return_type = TREE_TYPE (fntype);
3246 /* Convert the parameters to the types declared in the
3247 function prototype, or apply default promotions. */
3249 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3250 origtypes, function, fundecl);
3251 if (nargs < 0)
3252 return error_mark_node;
3254 /* Check that the function is called through a compatible prototype.
3255 If it is not, warn. */
3256 if (CONVERT_EXPR_P (function)
3257 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3258 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3259 && !comptypes (fntype, TREE_TYPE (tem)))
3261 /* This situation leads to run-time undefined behavior. We can't,
3262 therefore, simply error unless we can prove that all possible
3263 executions of the program must execute the code. */
3264 warning_at (loc, 0, "function called through a non-compatible type");
3266 if (VOID_TYPE_P (return_type)
3267 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3268 pedwarn (loc, 0,
3269 "function with qualified void return type called");
3272 argarray = vec_safe_address (params);
3274 /* Check that arguments to builtin functions match the expectations. */
3275 if (fundecl
3276 && fndecl_built_in_p (fundecl)
3277 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3278 orig_fundecl, nargs, argarray))
3279 return error_mark_node;
3281 /* Check that the arguments to the function are valid. */
3282 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3283 nargs, argarray, &arg_loc);
3285 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
3286 && !VOID_TYPE_P (return_type))
3287 return_type = c_build_qualified_type (return_type, TYPE_UNQUALIFIED);
3288 if (name != NULL_TREE
3289 && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
3291 if (require_constant_value)
3292 result
3293 = fold_build_call_array_initializer_loc (loc, return_type,
3294 function, nargs, argarray);
3295 else
3296 result = fold_build_call_array_loc (loc, return_type,
3297 function, nargs, argarray);
3298 if (TREE_CODE (result) == NOP_EXPR
3299 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3300 STRIP_TYPE_NOPS (result);
3302 else
3303 result = build_call_array_loc (loc, return_type,
3304 function, nargs, argarray);
3305 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3306 later. */
3307 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3308 suppress_warning (result, OPT_Wnonnull);
3310 /* In this improbable scenario, a nested function returns a VM type.
3311 Create a TARGET_EXPR so that the call always has a LHS, much as
3312 what the C++ FE does for functions returning non-PODs. */
3313 if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (fntype)))
3315 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3316 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3317 NULL_TREE, NULL_TREE);
3320 if (VOID_TYPE_P (TREE_TYPE (result)))
3322 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3323 pedwarn (loc, 0,
3324 "function with qualified void return type called");
3325 return result;
3327 return require_complete_type (loc, result);
3330 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3332 tree
3333 c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
3334 tree function, vec<tree, va_gc> *params,
3335 vec<tree, va_gc> *origtypes)
3337 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3338 STRIP_TYPE_NOPS (function);
3340 /* Convert anything with function type to a pointer-to-function. */
3341 if (TREE_CODE (function) == FUNCTION_DECL)
3343 /* Implement type-directed function overloading for builtins.
3344 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3345 handle all the type checking. The result is a complete expression
3346 that implements this function call. */
3347 tree tem = resolve_overloaded_builtin (loc, function, params);
3348 if (tem)
3349 return tem;
3351 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3354 /* Helper for convert_arguments called to convert the VALue of argument
3355 number ARGNUM from ORIGTYPE to the corresponding parameter number
3356 PARMNUM and TYPE.
3357 PLOC is the location where the conversion is being performed.
3358 FUNCTION and FUNDECL are the same as in convert_arguments.
3359 VALTYPE is the original type of VAL before the conversion and,
3360 for EXCESS_PRECISION_EXPR, the operand of the expression.
3361 NPC is true if VAL represents the null pointer constant (VAL itself
3362 will have been folded to an integer constant).
3363 RNAME is the same as FUNCTION except in Objective C when it's
3364 the function selector.
3365 EXCESS_PRECISION is true when VAL was originally represented
3366 as EXCESS_PRECISION_EXPR.
3367 WARNOPT is the same as in convert_for_assignment. */
3369 static tree
3370 convert_argument (location_t ploc, tree function, tree fundecl,
3371 tree type, tree origtype, tree val, tree valtype,
3372 bool npc, tree rname, int parmnum, int argnum,
3373 bool excess_precision, int warnopt)
3375 /* Formal parm type is specified by a function prototype. */
3377 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3379 error_at (ploc, "type of formal parameter %d is incomplete",
3380 parmnum + 1);
3381 return val;
3384 /* Optionally warn about conversions that differ from the default
3385 conversions. */
3386 if (warn_traditional_conversion || warn_traditional)
3388 if (INTEGRAL_TYPE_P (type)
3389 && SCALAR_FLOAT_TYPE_P (valtype))
3390 warning_at (ploc, OPT_Wtraditional_conversion,
3391 "passing argument %d of %qE as integer rather "
3392 "than floating due to prototype",
3393 argnum, rname);
3394 if (INTEGRAL_TYPE_P (type)
3395 && TREE_CODE (valtype) == COMPLEX_TYPE)
3396 warning_at (ploc, OPT_Wtraditional_conversion,
3397 "passing argument %d of %qE as integer rather "
3398 "than complex due to prototype",
3399 argnum, rname);
3400 else if (TREE_CODE (type) == COMPLEX_TYPE
3401 && SCALAR_FLOAT_TYPE_P (valtype))
3402 warning_at (ploc, OPT_Wtraditional_conversion,
3403 "passing argument %d of %qE as complex rather "
3404 "than floating due to prototype",
3405 argnum, rname);
3406 else if (SCALAR_FLOAT_TYPE_P (type)
3407 && INTEGRAL_TYPE_P (valtype))
3408 warning_at (ploc, OPT_Wtraditional_conversion,
3409 "passing argument %d of %qE as floating rather "
3410 "than integer due to prototype",
3411 argnum, rname);
3412 else if (TREE_CODE (type) == COMPLEX_TYPE
3413 && INTEGRAL_TYPE_P (valtype))
3414 warning_at (ploc, OPT_Wtraditional_conversion,
3415 "passing argument %d of %qE as complex rather "
3416 "than integer due to prototype",
3417 argnum, rname);
3418 else if (SCALAR_FLOAT_TYPE_P (type)
3419 && TREE_CODE (valtype) == COMPLEX_TYPE)
3420 warning_at (ploc, OPT_Wtraditional_conversion,
3421 "passing argument %d of %qE as floating rather "
3422 "than complex due to prototype",
3423 argnum, rname);
3424 /* ??? At some point, messages should be written about
3425 conversions between complex types, but that's too messy
3426 to do now. */
3427 else if (SCALAR_FLOAT_TYPE_P (type)
3428 && SCALAR_FLOAT_TYPE_P (valtype))
3430 unsigned int formal_prec = TYPE_PRECISION (type);
3432 /* Warn if any argument is passed as `float',
3433 since without a prototype it would be `double'. */
3434 if (formal_prec == TYPE_PRECISION (float_type_node)
3435 && type != dfloat32_type_node)
3436 warning_at (ploc, 0,
3437 "passing argument %d of %qE as %<float%> "
3438 "rather than %<double%> due to prototype",
3439 argnum, rname);
3441 /* Warn if mismatch between argument and prototype
3442 for decimal float types. Warn of conversions with
3443 binary float types and of precision narrowing due to
3444 prototype. */
3445 else if (type != valtype
3446 && (type == dfloat32_type_node
3447 || type == dfloat64_type_node
3448 || type == dfloat128_type_node
3449 || valtype == dfloat32_type_node
3450 || valtype == dfloat64_type_node
3451 || valtype == dfloat128_type_node)
3452 && (formal_prec
3453 <= TYPE_PRECISION (valtype)
3454 || (type == dfloat128_type_node
3455 && (valtype
3456 != dfloat64_type_node
3457 && (valtype
3458 != dfloat32_type_node)))
3459 || (type == dfloat64_type_node
3460 && (valtype
3461 != dfloat32_type_node))))
3462 warning_at (ploc, 0,
3463 "passing argument %d of %qE as %qT "
3464 "rather than %qT due to prototype",
3465 argnum, rname, type, valtype);
3468 /* Detect integer changing in width or signedness.
3469 These warnings are only activated with
3470 -Wtraditional-conversion, not with -Wtraditional. */
3471 else if (warn_traditional_conversion
3472 && INTEGRAL_TYPE_P (type)
3473 && INTEGRAL_TYPE_P (valtype))
3475 unsigned int formal_prec = TYPE_PRECISION (type);
3476 tree would_have_been = default_conversion (val);
3477 tree type1 = TREE_TYPE (would_have_been);
3479 if (val == error_mark_node)
3480 /* VAL could have been of incomplete type. */;
3481 else if (TREE_CODE (type) == ENUMERAL_TYPE
3482 && (TYPE_MAIN_VARIANT (type)
3483 == TYPE_MAIN_VARIANT (valtype)))
3484 /* No warning if function asks for enum
3485 and the actual arg is that enum type. */
3487 else if (formal_prec != TYPE_PRECISION (type1))
3488 warning_at (ploc, OPT_Wtraditional_conversion,
3489 "passing argument %d of %qE "
3490 "with different width due to prototype",
3491 argnum, rname);
3492 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3494 /* Don't complain if the formal parameter type
3495 is an enum, because we can't tell now whether
3496 the value was an enum--even the same enum. */
3497 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3499 else if (TREE_CODE (val) == INTEGER_CST
3500 && int_fits_type_p (val, type))
3501 /* Change in signedness doesn't matter
3502 if a constant value is unaffected. */
3504 /* If the value is extended from a narrower
3505 unsigned type, it doesn't matter whether we
3506 pass it as signed or unsigned; the value
3507 certainly is the same either way. */
3508 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3509 && TYPE_UNSIGNED (valtype))
3511 else if (TYPE_UNSIGNED (type))
3512 warning_at (ploc, OPT_Wtraditional_conversion,
3513 "passing argument %d of %qE "
3514 "as unsigned due to prototype",
3515 argnum, rname);
3516 else
3517 warning_at (ploc, OPT_Wtraditional_conversion,
3518 "passing argument %d of %qE "
3519 "as signed due to prototype",
3520 argnum, rname);
3524 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3525 sake of better warnings from convert_and_check. */
3526 if (excess_precision)
3527 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3529 tree parmval = convert_for_assignment (ploc, ploc, type,
3530 val, origtype, ic_argpass,
3531 npc, fundecl, function,
3532 parmnum + 1, warnopt);
3534 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3535 && INTEGRAL_TYPE_P (type)
3536 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3537 parmval = default_conversion (parmval);
3539 return parmval;
3542 /* Convert the argument expressions in the vector VALUES
3543 to the types in the list TYPELIST.
3545 If TYPELIST is exhausted, or when an element has NULL as its type,
3546 perform the default conversions.
3548 ORIGTYPES is the original types of the expressions in VALUES. This
3549 holds the type of enum values which have been converted to integral
3550 types. It may be NULL.
3552 FUNCTION is a tree for the called function. It is used only for
3553 error messages, where it is formatted with %qE.
3555 This is also where warnings about wrong number of args are generated.
3557 ARG_LOC are locations of function arguments (if any).
3559 Returns the actual number of arguments processed (which may be less
3560 than the length of VALUES in some error situations), or -1 on
3561 failure. */
3563 static int
3564 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3565 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3566 tree function, tree fundecl)
3568 unsigned int parmnum;
3569 bool error_args = false;
3570 const bool type_generic = fundecl
3571 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3572 bool type_generic_remove_excess_precision = false;
3573 bool type_generic_overflow_p = false;
3574 tree selector;
3576 /* Change pointer to function to the function itself for
3577 diagnostics. */
3578 if (TREE_CODE (function) == ADDR_EXPR
3579 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3580 function = TREE_OPERAND (function, 0);
3582 /* Handle an ObjC selector specially for diagnostics. */
3583 selector = objc_message_selector ();
3585 /* For a call to a built-in function declared without a prototype,
3586 set to the built-in function's argument list. */
3587 tree builtin_typelist = NULL_TREE;
3589 /* For type-generic built-in functions, determine whether excess
3590 precision should be removed (classification) or not
3591 (comparison). */
3592 if (fundecl
3593 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3595 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3596 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3598 /* For a call to a built-in function declared without a prototype
3599 use the types of the parameters of the internal built-in to
3600 match those of the arguments to. */
3601 if (tree bdecl = builtin_decl_explicit (code))
3602 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3605 /* For type-generic built-in functions, determine whether excess
3606 precision should be removed (classification) or not
3607 (comparison). */
3608 if (type_generic)
3609 switch (code)
3611 case BUILT_IN_ISFINITE:
3612 case BUILT_IN_ISINF:
3613 case BUILT_IN_ISINF_SIGN:
3614 case BUILT_IN_ISNAN:
3615 case BUILT_IN_ISNORMAL:
3616 case BUILT_IN_ISSIGNALING:
3617 case BUILT_IN_FPCLASSIFY:
3618 type_generic_remove_excess_precision = true;
3619 break;
3621 case BUILT_IN_ADD_OVERFLOW_P:
3622 case BUILT_IN_SUB_OVERFLOW_P:
3623 case BUILT_IN_MUL_OVERFLOW_P:
3624 /* The last argument of these type-generic builtins
3625 should not be promoted. */
3626 type_generic_overflow_p = true;
3627 break;
3629 default:
3630 break;
3634 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3635 individual converted arguments. */
3637 tree typetail, builtin_typetail, val;
3638 for (typetail = typelist,
3639 builtin_typetail = builtin_typelist,
3640 parmnum = 0;
3641 values && values->iterate (parmnum, &val);
3642 ++parmnum)
3644 /* The type of the function parameter (if it was declared with one). */
3645 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3646 /* The type of the built-in function parameter (if the function
3647 is a built-in). Used to detect type incompatibilities in
3648 calls to built-ins declared without a prototype. */
3649 tree builtin_type = (builtin_typetail
3650 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3651 /* The original type of the argument being passed to the function. */
3652 tree valtype = TREE_TYPE (val);
3653 /* The called function (or function selector in Objective C). */
3654 tree rname = function;
3655 int argnum = parmnum + 1;
3656 const char *invalid_func_diag;
3657 /* Set for EXCESS_PRECISION_EXPR arguments. */
3658 bool excess_precision = false;
3659 /* The value of the argument after conversion to the type
3660 of the function parameter it is passed to. */
3661 tree parmval;
3662 /* Some __atomic_* builtins have additional hidden argument at
3663 position 0. */
3664 location_t ploc
3665 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3666 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3667 : input_location;
3669 if (type == void_type_node)
3671 if (selector)
3672 error_at (loc, "too many arguments to method %qE", selector);
3673 else
3674 error_at (loc, "too many arguments to function %qE", function);
3675 inform_declaration (fundecl);
3676 return error_args ? -1 : (int) parmnum;
3679 if (builtin_type == void_type_node)
3681 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3682 "too many arguments to built-in function %qE "
3683 "expecting %d", function, parmnum))
3684 inform_declaration (fundecl);
3685 builtin_typetail = NULL_TREE;
3688 if (selector && argnum > 2)
3690 rname = selector;
3691 argnum -= 2;
3694 /* Determine if VAL is a null pointer constant before folding it. */
3695 bool npc = null_pointer_constant_p (val);
3697 /* If there is excess precision and a prototype, convert once to
3698 the required type rather than converting via the semantic
3699 type. Likewise without a prototype a float value represented
3700 as long double should be converted once to double. But for
3701 type-generic classification functions excess precision must
3702 be removed here. */
3703 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3704 && (type || !type_generic || !type_generic_remove_excess_precision))
3706 val = TREE_OPERAND (val, 0);
3707 excess_precision = true;
3709 val = c_fully_fold (val, false, NULL);
3710 STRIP_TYPE_NOPS (val);
3712 val = require_complete_type (ploc, val);
3714 /* Some floating-point arguments must be promoted to double when
3715 no type is specified by a prototype. This applies to
3716 arguments of type float, and to architecture-specific types
3717 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3718 bool promote_float_arg = false;
3719 if (type == NULL_TREE
3720 && TREE_CODE (valtype) == REAL_TYPE
3721 && (TYPE_PRECISION (valtype)
3722 <= TYPE_PRECISION (double_type_node))
3723 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3724 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3725 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3727 /* Promote this argument, unless it has a _FloatN or
3728 _FloatNx type. */
3729 promote_float_arg = true;
3730 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3731 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3733 promote_float_arg = false;
3734 break;
3736 /* Don't promote __bf16 either. */
3737 if (TYPE_MAIN_VARIANT (valtype) == bfloat16_type_node)
3738 promote_float_arg = false;
3741 if (type != NULL_TREE)
3743 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3744 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3745 val, valtype, npc, rname, parmnum, argnum,
3746 excess_precision, 0);
3748 else if (promote_float_arg)
3750 if (type_generic)
3751 parmval = val;
3752 else
3754 /* Convert `float' to `double'. */
3755 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3756 warning_at (ploc, OPT_Wdouble_promotion,
3757 "implicit conversion from %qT to %qT when passing "
3758 "argument to function",
3759 valtype, double_type_node);
3760 parmval = convert (double_type_node, val);
3763 else if ((excess_precision && !type_generic)
3764 || (type_generic_overflow_p && parmnum == 2))
3765 /* A "double" argument with excess precision being passed
3766 without a prototype or in variable arguments.
3767 The last argument of __builtin_*_overflow_p should not be
3768 promoted. */
3769 parmval = convert (valtype, val);
3770 else if ((invalid_func_diag =
3771 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3773 error (invalid_func_diag);
3774 return -1;
3776 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3778 return -1;
3780 else
3781 /* Convert `short' and `char' to full-size `int'. */
3782 parmval = default_conversion (val);
3784 (*values)[parmnum] = parmval;
3785 if (parmval == error_mark_node)
3786 error_args = true;
3788 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3790 /* For a call to a built-in function declared without a prototype,
3791 perform the conversions from the argument to the expected type
3792 but issue warnings rather than errors for any mismatches.
3793 Ignore the converted argument and use the PARMVAL obtained
3794 above by applying default conversions instead. */
3795 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3796 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3797 val, valtype, npc, rname, parmnum, argnum,
3798 excess_precision,
3799 OPT_Wbuiltin_declaration_mismatch);
3802 if (typetail)
3803 typetail = TREE_CHAIN (typetail);
3805 if (builtin_typetail)
3806 builtin_typetail = TREE_CHAIN (builtin_typetail);
3809 gcc_assert (parmnum == vec_safe_length (values));
3811 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3813 error_at (loc, "too few arguments to function %qE", function);
3814 inform_declaration (fundecl);
3815 return -1;
3818 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3820 unsigned nargs = parmnum;
3821 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3822 ++nargs;
3824 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3825 "too few arguments to built-in function %qE "
3826 "expecting %u", function, nargs - 1))
3827 inform_declaration (fundecl);
3830 return error_args ? -1 : (int) parmnum;
3833 /* This is the entry point used by the parser to build unary operators
3834 in the input. CODE, a tree_code, specifies the unary operator, and
3835 ARG is the operand. For unary plus, the C parser currently uses
3836 CONVERT_EXPR for code.
3838 LOC is the location to use for the tree generated.
3841 struct c_expr
3842 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3844 struct c_expr result;
3846 result.original_code = code;
3847 result.original_type = NULL;
3848 result.m_decimal = 0;
3850 if (reject_gcc_builtin (arg.value))
3852 result.value = error_mark_node;
3854 else
3856 result.value = build_unary_op (loc, code, arg.value, false);
3858 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3859 overflow_warning (loc, result.value, arg.value);
3862 /* We are typically called when parsing a prefix token at LOC acting on
3863 ARG. Reflect this by updating the source range of the result to
3864 start at LOC and end at the end of ARG. */
3865 set_c_expr_source_range (&result,
3866 loc, arg.get_finish ());
3868 return result;
3871 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3873 bool
3874 char_type_p (tree type)
3876 return (type == char_type_node
3877 || type == unsigned_char_type_node
3878 || type == signed_char_type_node
3879 || type == char16_type_node
3880 || type == char32_type_node);
3883 /* This is the entry point used by the parser to build binary operators
3884 in the input. CODE, a tree_code, specifies the binary operator, and
3885 ARG1 and ARG2 are the operands. In addition to constructing the
3886 expression, we check for operands that were written with other binary
3887 operators in a way that is likely to confuse the user.
3889 LOCATION is the location of the binary operator. */
3891 struct c_expr
3892 parser_build_binary_op (location_t location, enum tree_code code,
3893 struct c_expr arg1, struct c_expr arg2)
3895 struct c_expr result;
3896 result.m_decimal = 0;
3898 enum tree_code code1 = arg1.original_code;
3899 enum tree_code code2 = arg2.original_code;
3900 tree type1 = (arg1.original_type
3901 ? arg1.original_type
3902 : TREE_TYPE (arg1.value));
3903 tree type2 = (arg2.original_type
3904 ? arg2.original_type
3905 : TREE_TYPE (arg2.value));
3907 result.value = build_binary_op (location, code,
3908 arg1.value, arg2.value, true);
3909 result.original_code = code;
3910 result.original_type = NULL;
3911 result.m_decimal = 0;
3913 if (TREE_CODE (result.value) == ERROR_MARK)
3915 set_c_expr_source_range (&result,
3916 arg1.get_start (),
3917 arg2.get_finish ());
3918 return result;
3921 if (location != UNKNOWN_LOCATION)
3922 protected_set_expr_location (result.value, location);
3924 set_c_expr_source_range (&result,
3925 arg1.get_start (),
3926 arg2.get_finish ());
3928 /* Check for cases such as x+y<<z which users are likely
3929 to misinterpret. */
3930 if (warn_parentheses)
3931 warn_about_parentheses (location, code, code1, arg1.value, code2,
3932 arg2.value);
3934 if (warn_logical_op)
3935 warn_logical_operator (location, code, TREE_TYPE (result.value),
3936 code1, arg1.value, code2, arg2.value);
3938 if (warn_tautological_compare)
3940 tree lhs = arg1.value;
3941 tree rhs = arg2.value;
3942 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3944 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3945 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3946 lhs = NULL_TREE;
3947 else
3948 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3950 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3952 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3953 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3954 rhs = NULL_TREE;
3955 else
3956 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3958 if (lhs != NULL_TREE && rhs != NULL_TREE)
3959 warn_tautological_cmp (location, code, lhs, rhs);
3962 if (warn_logical_not_paren
3963 && TREE_CODE_CLASS (code) == tcc_comparison
3964 && code1 == TRUTH_NOT_EXPR
3965 && code2 != TRUTH_NOT_EXPR
3966 /* Avoid warning for !!x == y. */
3967 && (TREE_CODE (arg1.value) != NE_EXPR
3968 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3970 /* Avoid warning for !b == y where b has _Bool type. */
3971 tree t = integer_zero_node;
3972 if (TREE_CODE (arg1.value) == EQ_EXPR
3973 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3974 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3976 t = TREE_OPERAND (arg1.value, 0);
3979 if (TREE_TYPE (t) != integer_type_node)
3980 break;
3981 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3982 t = C_MAYBE_CONST_EXPR_EXPR (t);
3983 else if (CONVERT_EXPR_P (t))
3984 t = TREE_OPERAND (t, 0);
3985 else
3986 break;
3988 while (1);
3990 if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t)))
3991 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3994 /* Warn about comparisons against string literals, with the exception
3995 of testing for equality or inequality of a string literal with NULL. */
3996 if (code == EQ_EXPR || code == NE_EXPR)
3998 if ((code1 == STRING_CST
3999 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
4000 || (code2 == STRING_CST
4001 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
4002 warning_at (location, OPT_Waddress,
4003 "comparison with string literal results in unspecified behavior");
4004 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
4005 if (POINTER_TYPE_P (type1)
4006 && null_pointer_constant_p (arg2.value)
4007 && char_type_p (type2))
4009 auto_diagnostic_group d;
4010 if (warning_at (location, OPT_Wpointer_compare,
4011 "comparison between pointer and zero character "
4012 "constant"))
4013 inform (arg1.get_start (),
4014 "did you mean to dereference the pointer?");
4016 else if (POINTER_TYPE_P (type2)
4017 && null_pointer_constant_p (arg1.value)
4018 && char_type_p (type1))
4020 auto_diagnostic_group d;
4021 if (warning_at (location, OPT_Wpointer_compare,
4022 "comparison between pointer and zero character "
4023 "constant"))
4024 inform (arg2.get_start (),
4025 "did you mean to dereference the pointer?");
4028 else if (TREE_CODE_CLASS (code) == tcc_comparison
4029 && (code1 == STRING_CST || code2 == STRING_CST))
4030 warning_at (location, OPT_Waddress,
4031 "comparison with string literal results in unspecified "
4032 "behavior");
4034 if (warn_array_compare
4035 && TREE_CODE_CLASS (code) == tcc_comparison
4036 && TREE_CODE (type1) == ARRAY_TYPE
4037 && TREE_CODE (type2) == ARRAY_TYPE)
4038 do_warn_array_compare (location, code, arg1.value, arg2.value);
4040 if (TREE_OVERFLOW_P (result.value)
4041 && !TREE_OVERFLOW_P (arg1.value)
4042 && !TREE_OVERFLOW_P (arg2.value))
4043 overflow_warning (location, result.value);
4045 /* Warn about comparisons of different enum types. */
4046 if (warn_enum_compare
4047 && TREE_CODE_CLASS (code) == tcc_comparison
4048 && TREE_CODE (type1) == ENUMERAL_TYPE
4049 && TREE_CODE (type2) == ENUMERAL_TYPE
4050 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
4051 warning_at (location, OPT_Wenum_compare,
4052 "comparison between %qT and %qT",
4053 type1, type2);
4055 if (warn_xor_used_as_pow
4056 && code == BIT_XOR_EXPR
4057 && arg1.m_decimal
4058 && arg2.m_decimal)
4059 check_for_xor_used_as_pow (arg1.get_location (), arg1.value,
4060 location,
4061 arg2.get_location (), arg2.value);
4063 return result;
4066 /* Return a tree for the difference of pointers OP0 and OP1.
4067 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
4068 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
4070 static tree
4071 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
4073 tree restype = ptrdiff_type_node;
4074 tree result, inttype;
4076 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
4077 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
4078 tree target_type = TREE_TYPE (TREE_TYPE (op0));
4079 tree orig_op0 = op0;
4080 tree orig_op1 = op1;
4082 /* If the operands point into different address spaces, we need to
4083 explicitly convert them to pointers into the common address space
4084 before we can subtract the numerical address values. */
4085 if (as0 != as1)
4087 addr_space_t as_common;
4088 tree common_type;
4090 /* Determine the common superset address space. This is guaranteed
4091 to exist because the caller verified that comp_target_types
4092 returned non-zero. */
4093 if (!addr_space_superset (as0, as1, &as_common))
4094 gcc_unreachable ();
4096 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
4097 op0 = convert (common_type, op0);
4098 op1 = convert (common_type, op1);
4101 /* Determine integer type result of the subtraction. This will usually
4102 be the same as the result type (ptrdiff_t), but may need to be a wider
4103 type if pointers for the address space are wider than ptrdiff_t. */
4104 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
4105 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
4106 else
4107 inttype = restype;
4109 if (VOID_TYPE_P (target_type))
4110 pedwarn (loc, OPT_Wpointer_arith,
4111 "pointer of type %<void *%> used in subtraction");
4112 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4113 pedwarn (loc, OPT_Wpointer_arith,
4114 "pointer to a function used in subtraction");
4116 if (current_function_decl != NULL_TREE
4117 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
4119 op0 = save_expr (op0);
4120 op1 = save_expr (op1);
4122 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
4123 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
4126 /* First do the subtraction, then build the divide operator
4127 and only convert at the very end.
4128 Do not do default conversions in case restype is a short type. */
4130 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4131 pointers. If some platform cannot provide that, or has a larger
4132 ptrdiff_type to support differences larger than half the address
4133 space, cast the pointers to some larger integer type and do the
4134 computations in that type. */
4135 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4136 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4137 convert (inttype, op1), false);
4138 else
4140 /* Cast away qualifiers. */
4141 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4142 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4143 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4146 /* This generates an error if op1 is pointer to incomplete type. */
4147 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4148 error_at (loc, "arithmetic on pointer to an incomplete type");
4149 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4150 TREE_TYPE (TREE_TYPE (orig_op0))))
4151 verify_type_context (loc, TCTX_POINTER_ARITH,
4152 TREE_TYPE (TREE_TYPE (orig_op1)));
4154 op1 = c_size_in_bytes (target_type);
4156 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4157 error_at (loc, "arithmetic on pointer to an empty aggregate");
4159 /* Divide by the size, in easiest possible way. */
4160 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4161 op0, convert (inttype, op1));
4163 /* Convert to final result type if necessary. */
4164 return convert (restype, result);
4167 /* Expand atomic compound assignments into an appropriate sequence as
4168 specified by the C11 standard section 6.5.16.2.
4170 _Atomic T1 E1
4171 T2 E2
4172 E1 op= E2
4174 This sequence is used for all types for which these operations are
4175 supported.
4177 In addition, built-in versions of the 'fe' prefixed routines may
4178 need to be invoked for floating point (real, complex or vector) when
4179 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4181 T1 newval;
4182 T1 old;
4183 T1 *addr
4184 T2 val
4185 fenv_t fenv
4187 addr = &E1;
4188 val = (E2);
4189 __atomic_load (addr, &old, SEQ_CST);
4190 feholdexcept (&fenv);
4191 loop:
4192 newval = old op val;
4193 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4194 SEQ_CST))
4195 goto done;
4196 feclearexcept (FE_ALL_EXCEPT);
4197 goto loop:
4198 done:
4199 feupdateenv (&fenv);
4201 The compiler will issue the __atomic_fetch_* built-in when possible,
4202 otherwise it will generate the generic form of the atomic operations.
4203 This requires temp(s) and has their address taken. The atomic processing
4204 is smart enough to figure out when the size of an object can utilize
4205 a lock-free version, and convert the built-in call to the appropriate
4206 lock-free routine. The optimizers will then dispose of any temps that
4207 are no longer required, and lock-free implementations are utilized as
4208 long as there is target support for the required size.
4210 If the operator is NOP_EXPR, then this is a simple assignment, and
4211 an __atomic_store is issued to perform the assignment rather than
4212 the above loop. */
4214 /* Build an atomic assignment at LOC, expanding into the proper
4215 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4216 the result of the operation, unless RETURN_OLD_P, in which case
4217 return the old value of LHS (this is only for postincrement and
4218 postdecrement). */
4220 static tree
4221 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4222 tree rhs, bool return_old_p)
4224 tree fndecl, func_call;
4225 vec<tree, va_gc> *params;
4226 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4227 tree old, old_addr;
4228 tree compound_stmt = NULL_TREE;
4229 tree stmt, goto_stmt;
4230 tree loop_label, loop_decl, done_label, done_decl;
4232 tree lhs_type = TREE_TYPE (lhs);
4233 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4234 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4235 tree rhs_semantic_type = TREE_TYPE (rhs);
4236 tree nonatomic_rhs_semantic_type;
4237 tree rhs_type;
4239 gcc_assert (TYPE_ATOMIC (lhs_type));
4241 if (return_old_p)
4242 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4244 /* Allocate enough vector items for a compare_exchange. */
4245 vec_alloc (params, 6);
4247 /* Create a compound statement to hold the sequence of statements
4248 with a loop. */
4249 if (modifycode != NOP_EXPR)
4251 compound_stmt = c_begin_compound_stmt (false);
4253 /* For consistency with build_modify_expr on non-_Atomic,
4254 mark the lhs as read. Also, it would be very hard to match
4255 such expressions in mark_exp_read. */
4256 mark_exp_read (lhs);
4259 /* Remove any excess precision (which is only present here in the
4260 case of compound assignments). */
4261 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4263 gcc_assert (modifycode != NOP_EXPR);
4264 rhs = TREE_OPERAND (rhs, 0);
4266 rhs_type = TREE_TYPE (rhs);
4268 /* Fold the RHS if it hasn't already been folded. */
4269 if (modifycode != NOP_EXPR)
4270 rhs = c_fully_fold (rhs, false, NULL);
4272 /* Remove the qualifiers for the rest of the expressions and create
4273 the VAL temp variable to hold the RHS. */
4274 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4275 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4276 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4277 TYPE_UNQUALIFIED);
4278 val = create_tmp_var_raw (nonatomic_rhs_type);
4279 TREE_ADDRESSABLE (val) = 1;
4280 suppress_warning (val);
4281 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4282 NULL_TREE);
4283 TREE_SIDE_EFFECTS (rhs) = 1;
4284 SET_EXPR_LOCATION (rhs, loc);
4285 if (modifycode != NOP_EXPR)
4286 add_stmt (rhs);
4288 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4289 an atomic_store. */
4290 if (modifycode == NOP_EXPR)
4292 compound_stmt = rhs;
4293 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4294 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4295 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4296 params->quick_push (lhs_addr);
4297 params->quick_push (rhs);
4298 params->quick_push (seq_cst);
4299 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4301 compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4302 compound_stmt, func_call);
4304 /* VAL is the value which was stored, return a COMPOUND_STMT of
4305 the statement and that value. */
4306 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4309 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4310 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4311 isn't applicable for such builtins. ??? Do we want to handle enums? */
4312 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4313 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4315 built_in_function fncode;
4316 switch (modifycode)
4318 case PLUS_EXPR:
4319 case POINTER_PLUS_EXPR:
4320 fncode = (return_old_p
4321 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4322 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4323 break;
4324 case MINUS_EXPR:
4325 fncode = (return_old_p
4326 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4327 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4328 break;
4329 case BIT_AND_EXPR:
4330 fncode = (return_old_p
4331 ? BUILT_IN_ATOMIC_FETCH_AND_N
4332 : BUILT_IN_ATOMIC_AND_FETCH_N);
4333 break;
4334 case BIT_IOR_EXPR:
4335 fncode = (return_old_p
4336 ? BUILT_IN_ATOMIC_FETCH_OR_N
4337 : BUILT_IN_ATOMIC_OR_FETCH_N);
4338 break;
4339 case BIT_XOR_EXPR:
4340 fncode = (return_old_p
4341 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4342 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4343 break;
4344 default:
4345 goto cas_loop;
4348 /* We can only use "_1" through "_16" variants of the atomic fetch
4349 built-ins. */
4350 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4351 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4352 goto cas_loop;
4354 /* If this is a pointer type, we need to multiply by the size of
4355 the pointer target type. */
4356 if (POINTER_TYPE_P (lhs_type))
4358 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4359 /* ??? This would introduce -Wdiscarded-qualifiers
4360 warning: __atomic_fetch_* expect volatile void *
4361 type as the first argument. (Assignments between
4362 atomic and non-atomic objects are OK.) */
4363 || TYPE_RESTRICT (lhs_type))
4364 goto cas_loop;
4365 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4366 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4367 convert (ptrdiff_type_node, rhs),
4368 convert (ptrdiff_type_node, sz));
4371 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4372 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4373 fndecl = builtin_decl_explicit (fncode);
4374 params->quick_push (lhs_addr);
4375 params->quick_push (rhs);
4376 params->quick_push (seq_cst);
4377 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4379 newval = create_tmp_var_raw (nonatomic_lhs_type);
4380 TREE_ADDRESSABLE (newval) = 1;
4381 suppress_warning (newval);
4382 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4383 NULL_TREE, NULL_TREE);
4384 SET_EXPR_LOCATION (rhs, loc);
4385 add_stmt (rhs);
4387 /* Finish the compound statement. */
4388 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4390 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4391 the statement and that value. */
4392 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4395 cas_loop:
4396 /* Create the variables and labels required for the op= form. */
4397 old = create_tmp_var_raw (nonatomic_lhs_type);
4398 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4399 TREE_ADDRESSABLE (old) = 1;
4400 suppress_warning (old);
4402 newval = create_tmp_var_raw (nonatomic_lhs_type);
4403 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4404 TREE_ADDRESSABLE (newval) = 1;
4405 suppress_warning (newval);
4407 loop_decl = create_artificial_label (loc);
4408 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4410 done_decl = create_artificial_label (loc);
4411 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4413 /* __atomic_load (addr, &old, SEQ_CST). */
4414 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4415 params->quick_push (lhs_addr);
4416 params->quick_push (old_addr);
4417 params->quick_push (seq_cst);
4418 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4419 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4420 NULL_TREE);
4421 add_stmt (old);
4422 params->truncate (0);
4424 /* Create the expressions for floating-point environment
4425 manipulation, if required. */
4426 bool need_fenv = (flag_trapping_math
4427 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4428 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4429 if (need_fenv)
4430 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4432 if (hold_call)
4433 add_stmt (hold_call);
4435 /* loop: */
4436 add_stmt (loop_label);
4438 /* newval = old + val; */
4439 if (rhs_type != rhs_semantic_type)
4440 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4441 rhs = build_binary_op (loc, modifycode, old, val, true);
4442 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4444 tree eptype = TREE_TYPE (rhs);
4445 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4446 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4448 else
4449 rhs = c_fully_fold (rhs, false, NULL);
4450 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4451 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4452 NULL_TREE, 0);
4453 if (rhs != error_mark_node)
4455 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4456 NULL_TREE);
4457 SET_EXPR_LOCATION (rhs, loc);
4458 add_stmt (rhs);
4461 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4462 goto done; */
4463 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4464 params->quick_push (lhs_addr);
4465 params->quick_push (old_addr);
4466 params->quick_push (newval_addr);
4467 params->quick_push (integer_zero_node);
4468 params->quick_push (seq_cst);
4469 params->quick_push (seq_cst);
4470 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4472 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4473 SET_EXPR_LOCATION (goto_stmt, loc);
4475 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4476 SET_EXPR_LOCATION (stmt, loc);
4477 add_stmt (stmt);
4479 if (clear_call)
4480 add_stmt (clear_call);
4482 /* goto loop; */
4483 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4484 SET_EXPR_LOCATION (goto_stmt, loc);
4485 add_stmt (goto_stmt);
4487 /* done: */
4488 add_stmt (done_label);
4490 if (update_call)
4491 add_stmt (update_call);
4493 /* Finish the compound statement. */
4494 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4496 /* NEWVAL is the value that was successfully stored, return a
4497 COMPOUND_EXPR of the statement and the appropriate value. */
4498 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4499 return_old_p ? old : newval);
4502 /* Construct and perhaps optimize a tree representation
4503 for a unary operation. CODE, a tree_code, specifies the operation
4504 and XARG is the operand.
4505 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4506 promotions (such as from short to int).
4507 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4508 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4509 to pointers in C99.
4511 LOCATION is the location of the operator. */
4513 tree
4514 build_unary_op (location_t location, enum tree_code code, tree xarg,
4515 bool noconvert)
4517 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4518 tree arg = xarg;
4519 tree argtype = NULL_TREE;
4520 enum tree_code typecode;
4521 tree val;
4522 tree ret = error_mark_node;
4523 tree eptype = NULL_TREE;
4524 const char *invalid_op_diag;
4525 bool int_operands;
4527 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4528 if (int_operands)
4529 arg = remove_c_maybe_const_expr (arg);
4531 if (code != ADDR_EXPR)
4532 arg = require_complete_type (location, arg);
4534 typecode = TREE_CODE (TREE_TYPE (arg));
4535 if (typecode == ERROR_MARK)
4536 return error_mark_node;
4537 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4538 typecode = INTEGER_TYPE;
4540 if ((invalid_op_diag
4541 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4543 error_at (location, invalid_op_diag);
4544 return error_mark_node;
4547 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4549 eptype = TREE_TYPE (arg);
4550 arg = TREE_OPERAND (arg, 0);
4553 switch (code)
4555 case CONVERT_EXPR:
4556 /* This is used for unary plus, because a CONVERT_EXPR
4557 is enough to prevent anybody from looking inside for
4558 associativity, but won't generate any code. */
4559 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4560 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4561 || gnu_vector_type_p (TREE_TYPE (arg))))
4563 error_at (location, "wrong type argument to unary plus");
4564 return error_mark_node;
4566 else if (!noconvert)
4567 arg = default_conversion (arg);
4568 arg = non_lvalue_loc (location, arg);
4569 break;
4571 case NEGATE_EXPR:
4572 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4573 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4574 || gnu_vector_type_p (TREE_TYPE (arg))))
4576 error_at (location, "wrong type argument to unary minus");
4577 return error_mark_node;
4579 else if (!noconvert)
4580 arg = default_conversion (arg);
4581 break;
4583 case BIT_NOT_EXPR:
4584 /* ~ works on integer types and non float vectors. */
4585 if (typecode == INTEGER_TYPE
4586 || (gnu_vector_type_p (TREE_TYPE (arg))
4587 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4589 tree e = arg;
4591 /* Warn if the expression has boolean value. */
4592 while (TREE_CODE (e) == COMPOUND_EXPR)
4593 e = TREE_OPERAND (e, 1);
4595 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg))
4596 || truth_value_p (TREE_CODE (e))))
4598 auto_diagnostic_group d;
4599 if (warning_at (location, OPT_Wbool_operation,
4600 "%<~%> on a boolean expression"))
4602 gcc_rich_location richloc (location);
4603 richloc.add_fixit_insert_before (location, "!");
4604 inform (&richloc, "did you mean to use logical not?");
4607 if (!noconvert)
4608 arg = default_conversion (arg);
4610 else if (typecode == COMPLEX_TYPE)
4612 code = CONJ_EXPR;
4613 pedwarn (location, OPT_Wpedantic,
4614 "ISO C does not support %<~%> for complex conjugation");
4615 if (!noconvert)
4616 arg = default_conversion (arg);
4618 else
4620 error_at (location, "wrong type argument to bit-complement");
4621 return error_mark_node;
4623 break;
4625 case ABS_EXPR:
4626 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4628 error_at (location, "wrong type argument to abs");
4629 return error_mark_node;
4631 else if (!noconvert)
4632 arg = default_conversion (arg);
4633 break;
4635 case ABSU_EXPR:
4636 if (!(typecode == INTEGER_TYPE))
4638 error_at (location, "wrong type argument to absu");
4639 return error_mark_node;
4641 else if (!noconvert)
4642 arg = default_conversion (arg);
4643 break;
4645 case CONJ_EXPR:
4646 /* Conjugating a real value is a no-op, but allow it anyway. */
4647 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4648 || typecode == COMPLEX_TYPE))
4650 error_at (location, "wrong type argument to conjugation");
4651 return error_mark_node;
4653 else if (!noconvert)
4654 arg = default_conversion (arg);
4655 break;
4657 case TRUTH_NOT_EXPR:
4658 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4659 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4660 && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE)
4662 error_at (location,
4663 "wrong type argument to unary exclamation mark");
4664 return error_mark_node;
4666 if (int_operands)
4668 arg = c_objc_common_truthvalue_conversion (location, xarg);
4669 arg = remove_c_maybe_const_expr (arg);
4671 else
4672 arg = c_objc_common_truthvalue_conversion (location, arg);
4673 ret = invert_truthvalue_loc (location, arg);
4674 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4675 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4676 location = EXPR_LOCATION (ret);
4677 goto return_build_unary_op;
4679 case REALPART_EXPR:
4680 case IMAGPART_EXPR:
4681 ret = build_real_imag_expr (location, code, arg);
4682 if (ret == error_mark_node)
4683 return error_mark_node;
4684 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4685 eptype = TREE_TYPE (eptype);
4686 goto return_build_unary_op;
4688 case PREINCREMENT_EXPR:
4689 case POSTINCREMENT_EXPR:
4690 case PREDECREMENT_EXPR:
4691 case POSTDECREMENT_EXPR:
4693 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4695 tree inner = build_unary_op (location, code,
4696 C_MAYBE_CONST_EXPR_EXPR (arg),
4697 noconvert);
4698 if (inner == error_mark_node)
4699 return error_mark_node;
4700 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4701 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4702 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4703 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4704 goto return_build_unary_op;
4707 /* Complain about anything that is not a true lvalue. In
4708 Objective-C, skip this check for property_refs. */
4709 if (!objc_is_property_ref (arg)
4710 && !lvalue_or_else (location,
4711 arg, ((code == PREINCREMENT_EXPR
4712 || code == POSTINCREMENT_EXPR)
4713 ? lv_increment
4714 : lv_decrement)))
4715 return error_mark_node;
4717 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4719 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4720 warning_at (location, OPT_Wc___compat,
4721 "increment of enumeration value is invalid in C++");
4722 else
4723 warning_at (location, OPT_Wc___compat,
4724 "decrement of enumeration value is invalid in C++");
4727 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
4729 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4730 warning_at (location, OPT_Wbool_operation,
4731 "increment of a boolean expression");
4732 else
4733 warning_at (location, OPT_Wbool_operation,
4734 "decrement of a boolean expression");
4737 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4738 arg = c_fully_fold (arg, false, NULL, true);
4740 bool atomic_op;
4741 atomic_op = really_atomic_lvalue (arg);
4743 /* Increment or decrement the real part of the value,
4744 and don't change the imaginary part. */
4745 if (typecode == COMPLEX_TYPE)
4747 tree real, imag;
4749 pedwarn (location, OPT_Wpedantic,
4750 "ISO C does not support %<++%> and %<--%> on complex types");
4752 if (!atomic_op)
4754 arg = stabilize_reference (arg);
4755 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4756 true);
4757 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4758 true);
4759 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4760 if (real == error_mark_node || imag == error_mark_node)
4761 return error_mark_node;
4762 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4763 real, imag);
4764 goto return_build_unary_op;
4768 /* Report invalid types. */
4770 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4771 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4772 && typecode != COMPLEX_TYPE
4773 && !gnu_vector_type_p (TREE_TYPE (arg)))
4775 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4776 error_at (location, "wrong type argument to increment");
4777 else
4778 error_at (location, "wrong type argument to decrement");
4780 return error_mark_node;
4784 tree inc;
4786 argtype = TREE_TYPE (arg);
4788 /* Compute the increment. */
4790 if (typecode == POINTER_TYPE)
4792 /* If pointer target is an incomplete type,
4793 we just cannot know how to do the arithmetic. */
4794 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4796 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4797 error_at (location,
4798 "increment of pointer to an incomplete type %qT",
4799 TREE_TYPE (argtype));
4800 else
4801 error_at (location,
4802 "decrement of pointer to an incomplete type %qT",
4803 TREE_TYPE (argtype));
4805 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4806 || VOID_TYPE_P (TREE_TYPE (argtype)))
4808 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4809 pedwarn (location, OPT_Wpointer_arith,
4810 "wrong type argument to increment");
4811 else
4812 pedwarn (location, OPT_Wpointer_arith,
4813 "wrong type argument to decrement");
4815 else
4816 verify_type_context (location, TCTX_POINTER_ARITH,
4817 TREE_TYPE (argtype));
4819 inc = c_size_in_bytes (TREE_TYPE (argtype));
4820 inc = convert_to_ptrofftype_loc (location, inc);
4822 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4824 /* For signed fract types, we invert ++ to -- or
4825 -- to ++, and change inc from 1 to -1, because
4826 it is not possible to represent 1 in signed fract constants.
4827 For unsigned fract types, the result always overflows and
4828 we get an undefined (original) or the maximum value. */
4829 if (code == PREINCREMENT_EXPR)
4830 code = PREDECREMENT_EXPR;
4831 else if (code == PREDECREMENT_EXPR)
4832 code = PREINCREMENT_EXPR;
4833 else if (code == POSTINCREMENT_EXPR)
4834 code = POSTDECREMENT_EXPR;
4835 else /* code == POSTDECREMENT_EXPR */
4836 code = POSTINCREMENT_EXPR;
4838 inc = integer_minus_one_node;
4839 inc = convert (argtype, inc);
4841 else
4843 inc = VECTOR_TYPE_P (argtype)
4844 ? build_one_cst (argtype)
4845 : integer_one_node;
4846 inc = convert (argtype, inc);
4849 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4850 need to ask Objective-C to build the increment or decrement
4851 expression for it. */
4852 if (objc_is_property_ref (arg))
4853 return objc_build_incr_expr_for_property_ref (location, code,
4854 arg, inc);
4856 /* Report a read-only lvalue. */
4857 if (TYPE_READONLY (argtype))
4859 readonly_error (location, arg,
4860 ((code == PREINCREMENT_EXPR
4861 || code == POSTINCREMENT_EXPR)
4862 ? lv_increment : lv_decrement));
4863 return error_mark_node;
4865 else if (TREE_READONLY (arg))
4866 readonly_warning (arg,
4867 ((code == PREINCREMENT_EXPR
4868 || code == POSTINCREMENT_EXPR)
4869 ? lv_increment : lv_decrement));
4871 /* If the argument is atomic, use the special code sequences for
4872 atomic compound assignment. */
4873 if (atomic_op)
4875 arg = stabilize_reference (arg);
4876 ret = build_atomic_assign (location, arg,
4877 ((code == PREINCREMENT_EXPR
4878 || code == POSTINCREMENT_EXPR)
4879 ? PLUS_EXPR
4880 : MINUS_EXPR),
4881 (FRACT_MODE_P (TYPE_MODE (argtype))
4882 ? inc
4883 : integer_one_node),
4884 (code == POSTINCREMENT_EXPR
4885 || code == POSTDECREMENT_EXPR));
4886 goto return_build_unary_op;
4889 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
4890 val = boolean_increment (code, arg);
4891 else
4892 val = build2 (code, TREE_TYPE (arg), arg, inc);
4893 TREE_SIDE_EFFECTS (val) = 1;
4894 if (TYPE_QUALS (TREE_TYPE (val)) != TYPE_UNQUALIFIED)
4895 TREE_TYPE (val) = c_build_qualified_type (TREE_TYPE (val),
4896 TYPE_UNQUALIFIED);
4897 ret = val;
4898 goto return_build_unary_op;
4901 case ADDR_EXPR:
4902 /* Note that this operation never does default_conversion. */
4904 /* The operand of unary '&' must be an lvalue (which excludes
4905 expressions of type void), or, in C99, the result of a [] or
4906 unary '*' operator. */
4907 if (VOID_TYPE_P (TREE_TYPE (arg))
4908 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4909 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4910 pedwarn (location, 0, "taking address of expression of type %<void%>");
4912 /* Let &* cancel out to simplify resulting code. */
4913 if (INDIRECT_REF_P (arg))
4915 /* Don't let this be an lvalue. */
4916 if (lvalue_p (TREE_OPERAND (arg, 0)))
4917 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4918 ret = TREE_OPERAND (arg, 0);
4919 goto return_build_unary_op;
4922 /* Anything not already handled and not a true memory reference
4923 or a non-lvalue array is an error. */
4924 if (typecode != FUNCTION_TYPE && !noconvert
4925 && !lvalue_or_else (location, arg, lv_addressof))
4926 return error_mark_node;
4928 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4929 folding later. */
4930 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4932 tree inner = build_unary_op (location, code,
4933 C_MAYBE_CONST_EXPR_EXPR (arg),
4934 noconvert);
4935 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4936 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4937 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4938 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4939 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4940 goto return_build_unary_op;
4943 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4944 argtype = TREE_TYPE (arg);
4946 /* If the lvalue is const or volatile, merge that into the type
4947 to which the address will point. This is only needed
4948 for function types. */
4949 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4950 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4951 && TREE_CODE (argtype) == FUNCTION_TYPE)
4953 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4954 int quals = orig_quals;
4956 if (TREE_READONLY (arg))
4957 quals |= TYPE_QUAL_CONST;
4958 if (TREE_THIS_VOLATILE (arg))
4959 quals |= TYPE_QUAL_VOLATILE;
4961 argtype = c_build_qualified_type (argtype, quals);
4964 switch (TREE_CODE (arg))
4966 case COMPONENT_REF:
4967 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4969 error_at (location, "cannot take address of bit-field %qD",
4970 TREE_OPERAND (arg, 1));
4971 return error_mark_node;
4974 /* fall through */
4976 case ARRAY_REF:
4977 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4979 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4980 && !POINTER_TYPE_P (TREE_TYPE (arg))
4981 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4983 error_at (location, "cannot take address of scalar with "
4984 "reverse storage order");
4985 return error_mark_node;
4988 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4989 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4990 warning_at (location, OPT_Wscalar_storage_order,
4991 "address of array with reverse scalar storage "
4992 "order requested");
4995 default:
4996 break;
4999 if (!c_mark_addressable (arg))
5000 return error_mark_node;
5002 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
5003 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
5005 argtype = build_pointer_type (argtype);
5007 /* ??? Cope with user tricks that amount to offsetof. Delete this
5008 when we have proper support for integer constant expressions. */
5009 val = get_base_address (arg);
5010 if (val && INDIRECT_REF_P (val)
5011 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5013 ret = fold_offsetof (arg, argtype);
5014 goto return_build_unary_op;
5017 val = build1 (ADDR_EXPR, argtype, arg);
5019 ret = val;
5020 goto return_build_unary_op;
5022 case PAREN_EXPR:
5023 ret = build1 (code, TREE_TYPE (arg), arg);
5024 goto return_build_unary_op;
5026 default:
5027 gcc_unreachable ();
5030 if (argtype == NULL_TREE)
5031 argtype = TREE_TYPE (arg);
5032 if (TREE_CODE (arg) == INTEGER_CST)
5033 ret = (require_constant_value
5034 ? fold_build1_initializer_loc (location, code, argtype, arg)
5035 : fold_build1_loc (location, code, argtype, arg));
5036 else
5037 ret = build1 (code, argtype, arg);
5038 return_build_unary_op:
5039 gcc_assert (ret != error_mark_node);
5040 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
5041 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
5042 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
5043 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
5044 ret = note_integer_operands (ret);
5045 if (eptype)
5046 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5047 protected_set_expr_location (ret, location);
5048 return ret;
5051 /* Return nonzero if REF is an lvalue valid for this language.
5052 Lvalues can be assigned, unless their type has TYPE_READONLY.
5053 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
5055 bool
5056 lvalue_p (const_tree ref)
5058 const enum tree_code code = TREE_CODE (ref);
5060 switch (code)
5062 case REALPART_EXPR:
5063 case IMAGPART_EXPR:
5064 case COMPONENT_REF:
5065 return lvalue_p (TREE_OPERAND (ref, 0));
5067 case C_MAYBE_CONST_EXPR:
5068 return lvalue_p (TREE_OPERAND (ref, 1));
5070 case COMPOUND_LITERAL_EXPR:
5071 case STRING_CST:
5072 return true;
5074 case MEM_REF:
5075 case TARGET_MEM_REF:
5076 /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
5077 here as well. */
5078 case INDIRECT_REF:
5079 case ARRAY_REF:
5080 case VAR_DECL:
5081 case PARM_DECL:
5082 case RESULT_DECL:
5083 case ERROR_MARK:
5084 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
5085 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
5087 case BIND_EXPR:
5088 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
5090 default:
5091 return false;
5095 /* Give a warning for storing in something that is read-only in GCC
5096 terms but not const in ISO C terms. */
5098 static void
5099 readonly_warning (tree arg, enum lvalue_use use)
5101 switch (use)
5103 case lv_assign:
5104 warning (0, "assignment of read-only location %qE", arg);
5105 break;
5106 case lv_increment:
5107 warning (0, "increment of read-only location %qE", arg);
5108 break;
5109 case lv_decrement:
5110 warning (0, "decrement of read-only location %qE", arg);
5111 break;
5112 default:
5113 gcc_unreachable ();
5115 return;
5119 /* Return nonzero if REF is an lvalue valid for this language;
5120 otherwise, print an error message and return zero. USE says
5121 how the lvalue is being used and so selects the error message.
5122 LOCATION is the location at which any error should be reported. */
5124 static int
5125 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
5127 int win = lvalue_p (ref);
5129 if (!win)
5130 lvalue_error (loc, use);
5132 return win;
5135 /* Mark EXP saying that we need to be able to take the
5136 address of it; it should not be allocated in a register.
5137 Returns true if successful. ARRAY_REF_P is true if this
5138 is for ARRAY_REF construction - in that case we don't want
5139 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5140 it is fine to use ARRAY_REFs for vector subscripts on vector
5141 register variables. */
5143 bool
5144 c_mark_addressable (tree exp, bool array_ref_p)
5146 tree x = exp;
5148 while (1)
5149 switch (TREE_CODE (x))
5151 case VIEW_CONVERT_EXPR:
5152 if (array_ref_p
5153 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5154 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5155 return true;
5156 x = TREE_OPERAND (x, 0);
5157 break;
5159 case COMPONENT_REF:
5160 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
5162 error ("cannot take address of bit-field %qD",
5163 TREE_OPERAND (x, 1));
5164 return false;
5166 /* FALLTHRU */
5167 case ADDR_EXPR:
5168 case ARRAY_REF:
5169 case REALPART_EXPR:
5170 case IMAGPART_EXPR:
5171 x = TREE_OPERAND (x, 0);
5172 break;
5174 case COMPOUND_LITERAL_EXPR:
5175 if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)))
5177 error ("address of register compound literal requested");
5178 return false;
5180 TREE_ADDRESSABLE (x) = 1;
5181 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5182 return true;
5184 case CONSTRUCTOR:
5185 TREE_ADDRESSABLE (x) = 1;
5186 return true;
5188 case VAR_DECL:
5189 case CONST_DECL:
5190 case PARM_DECL:
5191 case RESULT_DECL:
5192 if (C_DECL_REGISTER (x)
5193 && DECL_NONLOCAL (x))
5195 if (TREE_PUBLIC (x) || is_global_var (x))
5197 error
5198 ("global register variable %qD used in nested function", x);
5199 return false;
5201 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5203 else if (C_DECL_REGISTER (x))
5205 if (TREE_PUBLIC (x) || is_global_var (x))
5206 error ("address of global register variable %qD requested", x);
5207 else
5208 error ("address of register variable %qD requested", x);
5209 return false;
5212 /* FALLTHRU */
5213 case FUNCTION_DECL:
5214 TREE_ADDRESSABLE (x) = 1;
5215 /* FALLTHRU */
5216 default:
5217 return true;
5221 /* Convert EXPR to TYPE, warning about conversion problems with
5222 constants. SEMANTIC_TYPE is the type this conversion would use
5223 without excess precision. If SEMANTIC_TYPE is NULL, this function
5224 is equivalent to convert_and_check. This function is a wrapper that
5225 handles conversions that may be different than
5226 the usual ones because of excess precision. */
5228 static tree
5229 ep_convert_and_check (location_t loc, tree type, tree expr,
5230 tree semantic_type)
5232 if (TREE_TYPE (expr) == type)
5233 return expr;
5235 /* For C11, integer conversions may have results with excess
5236 precision. */
5237 if (flag_isoc11 || !semantic_type)
5238 return convert_and_check (loc, type, expr);
5240 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5241 && TREE_TYPE (expr) != semantic_type)
5243 /* For integers, we need to check the real conversion, not
5244 the conversion to the excess precision type. */
5245 expr = convert_and_check (loc, semantic_type, expr);
5247 /* Result type is the excess precision type, which should be
5248 large enough, so do not check. */
5249 return convert (type, expr);
5252 /* If EXPR refers to a built-in declared without a prototype returns
5253 the actual type of the built-in and, if non-null, set *BLTIN to
5254 a pointer to the built-in. Otherwise return the type of EXPR
5255 and clear *BLTIN if non-null. */
5257 static tree
5258 type_or_builtin_type (tree expr, tree *bltin = NULL)
5260 tree dummy;
5261 if (!bltin)
5262 bltin = &dummy;
5264 *bltin = NULL_TREE;
5266 tree type = TREE_TYPE (expr);
5267 if (TREE_CODE (expr) != ADDR_EXPR)
5268 return type;
5270 tree oper = TREE_OPERAND (expr, 0);
5271 if (!DECL_P (oper)
5272 || TREE_CODE (oper) != FUNCTION_DECL
5273 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5274 return type;
5276 built_in_function code = DECL_FUNCTION_CODE (oper);
5277 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5278 return type;
5280 if ((*bltin = builtin_decl_implicit (code)))
5281 type = build_pointer_type (TREE_TYPE (*bltin));
5283 return type;
5286 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5287 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5288 if folded to an integer constant then the unselected half may
5289 contain arbitrary operations not normally permitted in constant
5290 expressions. Set the location of the expression to LOC. */
5292 tree
5293 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5294 tree op1, tree op1_original_type, location_t op1_loc,
5295 tree op2, tree op2_original_type, location_t op2_loc)
5297 tree type1;
5298 tree type2;
5299 enum tree_code code1;
5300 enum tree_code code2;
5301 tree result_type = NULL;
5302 tree semantic_result_type = NULL;
5303 tree orig_op1 = op1, orig_op2 = op2;
5304 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5305 bool ifexp_int_operands;
5306 tree ret;
5308 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5309 if (op1_int_operands)
5310 op1 = remove_c_maybe_const_expr (op1);
5311 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5312 if (op2_int_operands)
5313 op2 = remove_c_maybe_const_expr (op2);
5314 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5315 if (ifexp_int_operands)
5316 ifexp = remove_c_maybe_const_expr (ifexp);
5318 /* Promote both alternatives. */
5320 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5321 op1 = default_conversion (op1);
5322 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5323 op2 = default_conversion (op2);
5325 if (TREE_CODE (ifexp) == ERROR_MARK
5326 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5327 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5328 return error_mark_node;
5330 tree bltin1 = NULL_TREE;
5331 tree bltin2 = NULL_TREE;
5332 type1 = type_or_builtin_type (op1, &bltin1);
5333 code1 = TREE_CODE (type1);
5334 type2 = type_or_builtin_type (op2, &bltin2);
5335 code2 = TREE_CODE (type2);
5337 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5338 return error_mark_node;
5340 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5341 return error_mark_node;
5343 /* C90 does not permit non-lvalue arrays in conditional expressions.
5344 In C99 they will be pointers by now. */
5345 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5347 error_at (colon_loc, "non-lvalue array in conditional expression");
5348 return error_mark_node;
5351 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5352 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5353 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5354 || code1 == COMPLEX_TYPE)
5355 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5356 || code2 == COMPLEX_TYPE))
5358 semantic_result_type = c_common_type (type1, type2);
5359 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5361 op1 = TREE_OPERAND (op1, 0);
5362 type1 = TREE_TYPE (op1);
5363 gcc_assert (TREE_CODE (type1) == code1);
5365 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5367 op2 = TREE_OPERAND (op2, 0);
5368 type2 = TREE_TYPE (op2);
5369 gcc_assert (TREE_CODE (type2) == code2);
5373 if (warn_cxx_compat)
5375 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5376 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5378 if (TREE_CODE (t1) == ENUMERAL_TYPE
5379 && TREE_CODE (t2) == ENUMERAL_TYPE
5380 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5381 warning_at (colon_loc, OPT_Wc___compat,
5382 ("different enum types in conditional is "
5383 "invalid in C++: %qT vs %qT"),
5384 t1, t2);
5387 /* Quickly detect the usual case where op1 and op2 have the same type
5388 after promotion. */
5389 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5391 if (type1 == type2)
5392 result_type = type1;
5393 else
5394 result_type = TYPE_MAIN_VARIANT (type1);
5396 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5397 || code1 == COMPLEX_TYPE)
5398 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5399 || code2 == COMPLEX_TYPE))
5401 /* In C11, a conditional expression between a floating-point
5402 type and an integer type should convert the integer type to
5403 the evaluation format of the floating-point type, with
5404 possible excess precision. */
5405 tree eptype1 = type1;
5406 tree eptype2 = type2;
5407 if (flag_isoc11)
5409 tree eptype;
5410 if (ANY_INTEGRAL_TYPE_P (type1)
5411 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5413 eptype2 = eptype;
5414 if (!semantic_result_type)
5415 semantic_result_type = c_common_type (type1, type2);
5417 else if (ANY_INTEGRAL_TYPE_P (type2)
5418 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5420 eptype1 = eptype;
5421 if (!semantic_result_type)
5422 semantic_result_type = c_common_type (type1, type2);
5425 result_type = c_common_type (eptype1, eptype2);
5426 if (result_type == error_mark_node)
5427 return error_mark_node;
5428 do_warn_double_promotion (result_type, type1, type2,
5429 "implicit conversion from %qT to %qT to "
5430 "match other result of conditional",
5431 colon_loc);
5433 /* If -Wsign-compare, warn here if type1 and type2 have
5434 different signedness. We'll promote the signed to unsigned
5435 and later code won't know it used to be different.
5436 Do this check on the original types, so that explicit casts
5437 will be considered, but default promotions won't. */
5438 if (c_inhibit_evaluation_warnings == 0)
5440 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5441 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5443 if (unsigned_op1 ^ unsigned_op2)
5445 bool ovf;
5447 /* Do not warn if the result type is signed, since the
5448 signed type will only be chosen if it can represent
5449 all the values of the unsigned type. */
5450 if (!TYPE_UNSIGNED (result_type))
5451 /* OK */;
5452 else
5454 bool op1_maybe_const = true;
5455 bool op2_maybe_const = true;
5457 /* Do not warn if the signed quantity is an
5458 unsuffixed integer literal (or some static
5459 constant expression involving such literals) and
5460 it is non-negative. This warning requires the
5461 operands to be folded for best results, so do
5462 that folding in this case even without
5463 warn_sign_compare to avoid warning options
5464 possibly affecting code generation. */
5465 c_inhibit_evaluation_warnings
5466 += (ifexp == truthvalue_false_node);
5467 op1 = c_fully_fold (op1, require_constant_value,
5468 &op1_maybe_const);
5469 c_inhibit_evaluation_warnings
5470 -= (ifexp == truthvalue_false_node);
5472 c_inhibit_evaluation_warnings
5473 += (ifexp == truthvalue_true_node);
5474 op2 = c_fully_fold (op2, require_constant_value,
5475 &op2_maybe_const);
5476 c_inhibit_evaluation_warnings
5477 -= (ifexp == truthvalue_true_node);
5479 if (warn_sign_compare)
5481 if ((unsigned_op2
5482 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5483 || (unsigned_op1
5484 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5485 /* OK */;
5486 else if (unsigned_op2)
5487 warning_at (op1_loc, OPT_Wsign_compare,
5488 "operand of %<?:%> changes signedness from "
5489 "%qT to %qT due to unsignedness of other "
5490 "operand", TREE_TYPE (orig_op1),
5491 TREE_TYPE (orig_op2));
5492 else
5493 warning_at (op2_loc, OPT_Wsign_compare,
5494 "operand of %<?:%> changes signedness from "
5495 "%qT to %qT due to unsignedness of other "
5496 "operand", TREE_TYPE (orig_op2),
5497 TREE_TYPE (orig_op1));
5499 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5500 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5501 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5502 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5507 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5509 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5510 pedwarn (colon_loc, OPT_Wpedantic,
5511 "ISO C forbids conditional expr with only one void side");
5512 result_type = void_type_node;
5514 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5516 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5517 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5518 addr_space_t as_common;
5520 if (comp_target_types (colon_loc, type1, type2))
5521 result_type = common_pointer_type (type1, type2);
5522 else if (null_pointer_constant_p (orig_op1))
5523 result_type = type2;
5524 else if (null_pointer_constant_p (orig_op2))
5525 result_type = type1;
5526 else if (!addr_space_superset (as1, as2, &as_common))
5528 error_at (colon_loc, "pointers to disjoint address spaces "
5529 "used in conditional expression");
5530 return error_mark_node;
5532 else if ((VOID_TYPE_P (TREE_TYPE (type1))
5533 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5534 || (VOID_TYPE_P (TREE_TYPE (type2))
5535 && !TYPE_ATOMIC (TREE_TYPE (type2))))
5537 tree t1 = TREE_TYPE (type1);
5538 tree t2 = TREE_TYPE (type2);
5539 if (!(VOID_TYPE_P (t1)
5540 && !TYPE_ATOMIC (t1)))
5542 /* roles are swapped */
5543 t1 = t2;
5544 t2 = TREE_TYPE (type1);
5546 tree t2_stripped = strip_array_types (t2);
5547 if ((TREE_CODE (t2) == ARRAY_TYPE)
5548 && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
5550 if (!flag_isoc2x)
5551 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5552 "pointer to array loses qualifier "
5553 "in conditional expression");
5554 else if (warn_c11_c2x_compat > 0)
5555 warning_at (colon_loc, OPT_Wc11_c2x_compat,
5556 "pointer to array loses qualifier "
5557 "in conditional expression in ISO C before C2X");
5559 if (TREE_CODE (t2) == FUNCTION_TYPE)
5560 pedwarn (colon_loc, OPT_Wpedantic,
5561 "ISO C forbids conditional expr between "
5562 "%<void *%> and function pointer");
5563 /* for array, use qualifiers of element type */
5564 if (flag_isoc2x)
5565 t2 = t2_stripped;
5566 result_type = build_pointer_type (qualify_type (t1, t2));
5568 /* Objective-C pointer comparisons are a bit more lenient. */
5569 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5570 result_type = objc_common_type (type1, type2);
5571 else
5573 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5574 if (bltin1 && bltin2)
5575 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5576 "pointer type mismatch between %qT and %qT "
5577 "of %qD and %qD in conditional expression",
5578 type1, type2, bltin1, bltin2);
5579 else
5580 pedwarn (colon_loc, 0,
5581 "pointer type mismatch in conditional expression");
5582 result_type = build_pointer_type
5583 (build_qualified_type (void_type_node, qual));
5586 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5588 if (!null_pointer_constant_p (orig_op2))
5589 pedwarn (colon_loc, 0,
5590 "pointer/integer type mismatch in conditional expression");
5591 else
5593 op2 = null_pointer_node;
5595 result_type = type1;
5597 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5599 if (!null_pointer_constant_p (orig_op1))
5600 pedwarn (colon_loc, 0,
5601 "pointer/integer type mismatch in conditional expression");
5602 else
5604 op1 = null_pointer_node;
5606 result_type = type2;
5608 /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
5609 type nullptr_t and the other is a pointer, the result type is the pointer
5610 type." */
5611 else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE)
5612 result_type = type2;
5613 else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE)
5614 result_type = type1;
5616 if (!result_type)
5618 if (flag_cond_mismatch)
5619 result_type = void_type_node;
5620 else
5622 error_at (colon_loc, "type mismatch in conditional expression");
5623 return error_mark_node;
5627 /* Merge const and volatile flags of the incoming types. */
5628 result_type
5629 = build_type_variant (result_type,
5630 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5631 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5633 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5634 semantic_result_type);
5635 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5636 semantic_result_type);
5638 if (ifexp_bcp && ifexp == truthvalue_true_node)
5640 op2_int_operands = true;
5641 op1 = c_fully_fold (op1, require_constant_value, NULL);
5643 if (ifexp_bcp && ifexp == truthvalue_false_node)
5645 op1_int_operands = true;
5646 op2 = c_fully_fold (op2, require_constant_value, NULL);
5648 int_const = int_operands = (ifexp_int_operands
5649 && op1_int_operands
5650 && op2_int_operands);
5651 if (int_operands)
5653 int_const = ((ifexp == truthvalue_true_node
5654 && TREE_CODE (orig_op1) == INTEGER_CST
5655 && !TREE_OVERFLOW (orig_op1))
5656 || (ifexp == truthvalue_false_node
5657 && TREE_CODE (orig_op2) == INTEGER_CST
5658 && !TREE_OVERFLOW (orig_op2)));
5661 /* Need to convert condition operand into a vector mask. */
5662 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5664 tree vectype = TREE_TYPE (ifexp);
5665 tree elem_type = TREE_TYPE (vectype);
5666 tree zero = build_int_cst (elem_type, 0);
5667 tree zero_vec = build_vector_from_val (vectype, zero);
5668 tree cmp_type = truth_type_for (vectype);
5669 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5672 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5673 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5674 else
5676 if (int_operands)
5678 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5679 nested inside of the expression. */
5680 op1 = c_fully_fold (op1, false, NULL);
5681 op2 = c_fully_fold (op2, false, NULL);
5683 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5684 if (int_operands)
5685 ret = note_integer_operands (ret);
5687 if (semantic_result_type)
5688 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5690 protected_set_expr_location (ret, colon_loc);
5692 /* If the OP1 and OP2 are the same and don't have side-effects,
5693 warn here, because the COND_EXPR will be turned into OP1. */
5694 if (warn_duplicated_branches
5695 && TREE_CODE (ret) == COND_EXPR
5696 && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
5697 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5698 "this condition has identical branches");
5700 return ret;
5703 /* EXPR is an expression, location LOC, whose result is discarded.
5704 Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
5705 whose right-hand operand is such a call, possibly recursively). */
5707 static void
5708 maybe_warn_nodiscard (location_t loc, tree expr)
5710 if (VOID_TYPE_P (TREE_TYPE (expr)))
5711 return;
5712 while (TREE_CODE (expr) == COMPOUND_EXPR)
5714 expr = TREE_OPERAND (expr, 1);
5715 if (EXPR_HAS_LOCATION (expr))
5716 loc = EXPR_LOCATION (expr);
5718 if (TREE_CODE (expr) != CALL_EXPR)
5719 return;
5720 tree fn = CALL_EXPR_FN (expr);
5721 if (!fn)
5722 return;
5723 tree attr;
5724 if (TREE_CODE (fn) == ADDR_EXPR
5725 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5726 && (attr = lookup_attribute ("nodiscard",
5727 DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
5729 fn = TREE_OPERAND (fn, 0);
5730 tree args = TREE_VALUE (attr);
5731 if (args)
5732 args = TREE_VALUE (args);
5733 auto_diagnostic_group d;
5734 int warned;
5735 if (args)
5736 warned = warning_at (loc, OPT_Wunused_result,
5737 "ignoring return value of %qD, declared with "
5738 "attribute %<nodiscard%>: %E", fn, args);
5739 else
5740 warned = warning_at (loc, OPT_Wunused_result,
5741 "ignoring return value of %qD, declared with "
5742 "attribute %<nodiscard%>", fn);
5743 if (warned)
5744 inform (DECL_SOURCE_LOCATION (fn), "declared here");
5746 else
5748 tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
5749 attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
5750 if (!attr)
5751 return;
5752 tree args = TREE_VALUE (attr);
5753 if (args)
5754 args = TREE_VALUE (args);
5755 auto_diagnostic_group d;
5756 int warned;
5757 if (args)
5758 warned = warning_at (loc, OPT_Wunused_result,
5759 "ignoring return value of type %qT, declared "
5760 "with attribute %<nodiscard%>: %E",
5761 rettype, args);
5762 else
5763 warned = warning_at (loc, OPT_Wunused_result,
5764 "ignoring return value of type %qT, declared "
5765 "with attribute %<nodiscard%>", rettype);
5766 if (warned)
5768 if (TREE_CODE (fn) == ADDR_EXPR)
5770 fn = TREE_OPERAND (fn, 0);
5771 if (TREE_CODE (fn) == FUNCTION_DECL)
5772 inform (DECL_SOURCE_LOCATION (fn),
5773 "in call to %qD, declared here", fn);
5779 /* Return a compound expression that performs two expressions and
5780 returns the value of the second of them.
5782 LOC is the location of the COMPOUND_EXPR. */
5784 tree
5785 build_compound_expr (location_t loc, tree expr1, tree expr2)
5787 bool expr1_int_operands, expr2_int_operands;
5788 tree eptype = NULL_TREE;
5789 tree ret;
5791 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5792 if (expr1_int_operands)
5793 expr1 = remove_c_maybe_const_expr (expr1);
5794 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5795 if (expr2_int_operands)
5796 expr2 = remove_c_maybe_const_expr (expr2);
5798 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5799 expr1 = TREE_OPERAND (expr1, 0);
5800 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5802 eptype = TREE_TYPE (expr2);
5803 expr2 = TREE_OPERAND (expr2, 0);
5806 if (!TREE_SIDE_EFFECTS (expr1))
5808 /* The left-hand operand of a comma expression is like an expression
5809 statement: with -Wunused, we should warn if it doesn't have
5810 any side-effects, unless it was explicitly cast to (void). */
5811 if (warn_unused_value)
5813 if (VOID_TYPE_P (TREE_TYPE (expr1))
5814 && CONVERT_EXPR_P (expr1))
5815 ; /* (void) a, b */
5816 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5817 && TREE_CODE (expr1) == COMPOUND_EXPR
5818 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5819 ; /* (void) a, (void) b, c */
5820 else
5821 warning_at (loc, OPT_Wunused_value,
5822 "left-hand operand of comma expression has no effect");
5825 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5826 && warn_unused_value)
5828 tree r = expr1;
5829 location_t cloc = loc;
5830 while (TREE_CODE (r) == COMPOUND_EXPR)
5832 if (EXPR_HAS_LOCATION (r))
5833 cloc = EXPR_LOCATION (r);
5834 r = TREE_OPERAND (r, 1);
5836 if (!TREE_SIDE_EFFECTS (r)
5837 && !VOID_TYPE_P (TREE_TYPE (r))
5838 && !CONVERT_EXPR_P (r))
5839 warning_at (cloc, OPT_Wunused_value,
5840 "right-hand operand of comma expression has no effect");
5843 /* With -Wunused, we should also warn if the left-hand operand does have
5844 side-effects, but computes a value which is not used. For example, in
5845 `foo() + bar(), baz()' the result of the `+' operator is not used,
5846 so we should issue a warning. */
5847 else if (warn_unused_value)
5848 warn_if_unused_value (expr1, loc);
5850 maybe_warn_nodiscard (loc, expr1);
5852 if (expr2 == error_mark_node)
5853 return error_mark_node;
5855 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5857 if (flag_isoc99
5858 && expr1_int_operands
5859 && expr2_int_operands)
5860 ret = note_integer_operands (ret);
5862 if (eptype)
5863 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5865 protected_set_expr_location (ret, loc);
5866 return ret;
5869 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5870 which we are casting. OTYPE is the type of the expression being
5871 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5872 of the cast. -Wcast-qual appeared on the command line. Named
5873 address space qualifiers are not handled here, because they result
5874 in different warnings. */
5876 static void
5877 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5879 tree in_type = type;
5880 tree in_otype = otype;
5881 int added = 0;
5882 int discarded = 0;
5883 bool is_const;
5885 /* Check that the qualifiers on IN_TYPE are a superset of the
5886 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5887 nodes is uninteresting and we stop as soon as we hit a
5888 non-POINTER_TYPE node on either type. */
5891 in_otype = TREE_TYPE (in_otype);
5892 in_type = TREE_TYPE (in_type);
5894 /* GNU C allows cv-qualified function types. 'const' means the
5895 function is very pure, 'volatile' means it can't return. We
5896 need to warn when such qualifiers are added, not when they're
5897 taken away. */
5898 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5899 && TREE_CODE (in_type) == FUNCTION_TYPE)
5900 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5901 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5902 else
5903 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5904 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5906 while (TREE_CODE (in_type) == POINTER_TYPE
5907 && TREE_CODE (in_otype) == POINTER_TYPE);
5909 if (added)
5910 warning_at (loc, OPT_Wcast_qual,
5911 "cast adds %q#v qualifier to function type", added);
5913 if (discarded)
5914 /* There are qualifiers present in IN_OTYPE that are not present
5915 in IN_TYPE. */
5916 warning_at (loc, OPT_Wcast_qual,
5917 "cast discards %qv qualifier from pointer target type",
5918 discarded);
5920 if (added || discarded)
5921 return;
5923 /* A cast from **T to const **T is unsafe, because it can cause a
5924 const value to be changed with no additional warning. We only
5925 issue this warning if T is the same on both sides, and we only
5926 issue the warning if there are the same number of pointers on
5927 both sides, as otherwise the cast is clearly unsafe anyhow. A
5928 cast is unsafe when a qualifier is added at one level and const
5929 is not present at all outer levels.
5931 To issue this warning, we check at each level whether the cast
5932 adds new qualifiers not already seen. We don't need to special
5933 case function types, as they won't have the same
5934 TYPE_MAIN_VARIANT. */
5936 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5937 return;
5938 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5939 return;
5941 in_type = type;
5942 in_otype = otype;
5943 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5946 in_type = TREE_TYPE (in_type);
5947 in_otype = TREE_TYPE (in_otype);
5948 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5949 && !is_const)
5951 warning_at (loc, OPT_Wcast_qual,
5952 "to be safe all intermediate pointers in cast from "
5953 "%qT to %qT must be %<const%> qualified",
5954 otype, type);
5955 break;
5957 if (is_const)
5958 is_const = TYPE_READONLY (in_type);
5960 while (TREE_CODE (in_type) == POINTER_TYPE);
5963 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5965 static bool
5966 c_safe_arg_type_equiv_p (tree t1, tree t2)
5968 t1 = TYPE_MAIN_VARIANT (t1);
5969 t2 = TYPE_MAIN_VARIANT (t2);
5971 if (TREE_CODE (t1) == POINTER_TYPE
5972 && TREE_CODE (t2) == POINTER_TYPE)
5973 return true;
5975 /* The signedness of the parameter matters only when an integral
5976 type smaller than int is promoted to int, otherwise only the
5977 precision of the parameter matters.
5978 This check should make sure that the callee does not see
5979 undefined values in argument registers. */
5980 if (INTEGRAL_TYPE_P (t1)
5981 && INTEGRAL_TYPE_P (t2)
5982 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5983 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5984 || !targetm.calls.promote_prototypes (NULL_TREE)
5985 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5986 return true;
5988 return comptypes (t1, t2);
5991 /* Check if a type cast between two function types can be considered safe. */
5993 static bool
5994 c_safe_function_type_cast_p (tree t1, tree t2)
5996 if (TREE_TYPE (t1) == void_type_node &&
5997 TYPE_ARG_TYPES (t1) == void_list_node)
5998 return true;
6000 if (TREE_TYPE (t2) == void_type_node &&
6001 TYPE_ARG_TYPES (t2) == void_list_node)
6002 return true;
6004 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
6005 return false;
6007 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
6008 t1 && t2;
6009 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6010 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
6011 return false;
6013 return true;
6016 /* Build an expression representing a cast to type TYPE of expression EXPR.
6017 LOC is the location of the cast-- typically the open paren of the cast. */
6019 tree
6020 build_c_cast (location_t loc, tree type, tree expr)
6022 tree value;
6024 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
6026 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
6027 expr = TREE_OPERAND (expr, 0);
6029 value = expr;
6030 if (int_operands)
6031 value = remove_c_maybe_const_expr (value);
6033 if (type == error_mark_node || expr == error_mark_node)
6034 return error_mark_node;
6036 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
6037 only in <protocol> qualifications. But when constructing cast expressions,
6038 the protocols do matter and must be kept around. */
6039 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
6040 return build1 (NOP_EXPR, type, expr);
6042 type = TYPE_MAIN_VARIANT (type);
6044 if (TREE_CODE (type) == ARRAY_TYPE)
6046 error_at (loc, "cast specifies array type");
6047 return error_mark_node;
6050 if (TREE_CODE (type) == FUNCTION_TYPE)
6052 error_at (loc, "cast specifies function type");
6053 return error_mark_node;
6056 if (!VOID_TYPE_P (type))
6058 value = require_complete_type (loc, value);
6059 if (value == error_mark_node)
6060 return error_mark_node;
6063 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
6065 if (RECORD_OR_UNION_TYPE_P (type))
6066 pedwarn (loc, OPT_Wpedantic,
6067 "ISO C forbids casting nonscalar to the same type");
6069 /* Convert to remove any qualifiers from VALUE's type. */
6070 value = convert (type, value);
6072 else if (TREE_CODE (type) == UNION_TYPE)
6074 tree field;
6076 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6077 if (TREE_TYPE (field) != error_mark_node
6078 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
6079 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
6080 break;
6082 if (field)
6084 tree t;
6085 bool maybe_const = true;
6087 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
6088 t = c_fully_fold (value, false, &maybe_const);
6089 t = build_constructor_single (type, field, t);
6090 if (!maybe_const)
6091 t = c_wrap_maybe_const (t, true);
6092 t = digest_init (loc, type, t,
6093 NULL_TREE, false, false, false, true, false, false);
6094 TREE_CONSTANT (t) = TREE_CONSTANT (value);
6095 return t;
6097 error_at (loc, "cast to union type from type not present in union");
6098 return error_mark_node;
6100 else
6102 tree otype, ovalue;
6104 if (type == void_type_node)
6106 tree t = build1 (CONVERT_EXPR, type, value);
6107 SET_EXPR_LOCATION (t, loc);
6108 return t;
6111 otype = TREE_TYPE (value);
6113 /* Optionally warn about potentially worrisome casts. */
6114 if (warn_cast_qual
6115 && TREE_CODE (type) == POINTER_TYPE
6116 && TREE_CODE (otype) == POINTER_TYPE)
6117 handle_warn_cast_qual (loc, type, otype);
6119 /* Warn about conversions between pointers to disjoint
6120 address spaces. */
6121 if (TREE_CODE (type) == POINTER_TYPE
6122 && TREE_CODE (otype) == POINTER_TYPE
6123 && !null_pointer_constant_p (value))
6125 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
6126 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
6127 addr_space_t as_common;
6129 if (!addr_space_superset (as_to, as_from, &as_common))
6131 if (ADDR_SPACE_GENERIC_P (as_from))
6132 warning_at (loc, 0, "cast to %qs address space pointer "
6133 "from disjoint generic address space pointer",
6134 c_addr_space_name (as_to));
6136 else if (ADDR_SPACE_GENERIC_P (as_to))
6137 warning_at (loc, 0, "cast to generic address space pointer "
6138 "from disjoint %qs address space pointer",
6139 c_addr_space_name (as_from));
6141 else
6142 warning_at (loc, 0, "cast to %qs address space pointer "
6143 "from disjoint %qs address space pointer",
6144 c_addr_space_name (as_to),
6145 c_addr_space_name (as_from));
6149 /* Warn about possible alignment problems. */
6150 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
6151 && TREE_CODE (type) == POINTER_TYPE
6152 && TREE_CODE (otype) == POINTER_TYPE
6153 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
6154 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6155 /* Don't warn about opaque types, where the actual alignment
6156 restriction is unknown. */
6157 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
6158 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
6159 && min_align_of_type (TREE_TYPE (type))
6160 > min_align_of_type (TREE_TYPE (otype)))
6161 warning_at (loc, OPT_Wcast_align,
6162 "cast increases required alignment of target type");
6164 if (TREE_CODE (type) == INTEGER_TYPE
6165 && TREE_CODE (otype) == POINTER_TYPE
6166 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
6167 /* Unlike conversion of integers to pointers, where the
6168 warning is disabled for converting constants because
6169 of cases such as SIG_*, warn about converting constant
6170 pointers to integers. In some cases it may cause unwanted
6171 sign extension, and a warning is appropriate. */
6172 warning_at (loc, OPT_Wpointer_to_int_cast,
6173 "cast from pointer to integer of different size");
6175 if (TREE_CODE (value) == CALL_EXPR
6176 && TREE_CODE (type) != TREE_CODE (otype))
6177 warning_at (loc, OPT_Wbad_function_cast,
6178 "cast from function call of type %qT "
6179 "to non-matching type %qT", otype, type);
6181 if (TREE_CODE (type) == POINTER_TYPE
6182 && TREE_CODE (otype) == INTEGER_TYPE
6183 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
6184 /* Don't warn about converting any constant. */
6185 && !TREE_CONSTANT (value))
6186 warning_at (loc,
6187 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
6188 "of different size");
6190 if (warn_strict_aliasing <= 2)
6191 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
6193 /* If pedantic, warn for conversions between function and object
6194 pointer types, except for converting a null pointer constant
6195 to function pointer type. */
6196 if (pedantic
6197 && TREE_CODE (type) == POINTER_TYPE
6198 && TREE_CODE (otype) == POINTER_TYPE
6199 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6200 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
6201 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6202 "conversion of function pointer to object pointer type");
6204 if (pedantic
6205 && TREE_CODE (type) == POINTER_TYPE
6206 && TREE_CODE (otype) == POINTER_TYPE
6207 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6208 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6209 && !null_pointer_constant_p (value))
6210 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6211 "conversion of object pointer to function pointer type");
6213 if (TREE_CODE (type) == POINTER_TYPE
6214 && TREE_CODE (otype) == POINTER_TYPE
6215 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6216 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6217 && !c_safe_function_type_cast_p (TREE_TYPE (type),
6218 TREE_TYPE (otype)))
6219 warning_at (loc, OPT_Wcast_function_type,
6220 "cast between incompatible function types"
6221 " from %qT to %qT", otype, type);
6223 ovalue = value;
6224 value = convert (type, value);
6226 /* Ignore any integer overflow caused by the cast. */
6227 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
6229 if (TREE_OVERFLOW_P (ovalue))
6231 if (!TREE_OVERFLOW (value))
6233 /* Avoid clobbering a shared constant. */
6234 value = copy_node (value);
6235 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
6238 else if (TREE_OVERFLOW (value))
6239 /* Reset VALUE's overflow flags, ensuring constant sharing. */
6240 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6244 /* Don't let a cast be an lvalue. */
6245 if (lvalue_p (value))
6246 value = non_lvalue_loc (loc, value);
6248 /* Don't allow the results of casting to floating-point or complex
6249 types be confused with actual constants, or casts involving
6250 integer and pointer types other than direct integer-to-integer
6251 and integer-to-pointer be confused with integer constant
6252 expressions and null pointer constants. */
6253 if (TREE_CODE (value) == REAL_CST
6254 || TREE_CODE (value) == COMPLEX_CST
6255 || (TREE_CODE (value) == INTEGER_CST
6256 && !((TREE_CODE (expr) == INTEGER_CST
6257 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6258 || TREE_CODE (expr) == REAL_CST
6259 || TREE_CODE (expr) == COMPLEX_CST)))
6260 value = build1 (NOP_EXPR, type, value);
6262 /* If the expression has integer operands and so can occur in an
6263 unevaluated part of an integer constant expression, ensure the
6264 return value reflects this. */
6265 if (int_operands
6266 && INTEGRAL_TYPE_P (type)
6267 && value != error_mark_node
6268 && !EXPR_INT_CONST_OPERANDS (value))
6269 value = note_integer_operands (value);
6271 protected_set_expr_location (value, loc);
6272 return value;
6275 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
6276 location of the open paren of the cast, or the position of the cast
6277 expr. */
6278 tree
6279 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6281 tree type;
6282 tree type_expr = NULL_TREE;
6283 bool type_expr_const = true;
6284 tree ret;
6285 int saved_wsp = warn_strict_prototypes;
6287 /* This avoids warnings about unprototyped casts on
6288 integers. E.g. "#define SIG_DFL (void(*)())0". */
6289 if (TREE_CODE (expr) == INTEGER_CST)
6290 warn_strict_prototypes = 0;
6291 type = groktypename (type_name, &type_expr, &type_expr_const);
6292 warn_strict_prototypes = saved_wsp;
6294 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6295 && reject_gcc_builtin (expr))
6296 return error_mark_node;
6298 ret = build_c_cast (loc, type, expr);
6299 if (type_expr)
6301 bool inner_expr_const = true;
6302 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6303 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6304 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6305 && inner_expr_const);
6306 SET_EXPR_LOCATION (ret, loc);
6309 if (!EXPR_HAS_LOCATION (ret))
6310 protected_set_expr_location (ret, loc);
6312 /* C++ does not permits types to be defined in a cast, but it
6313 allows references to incomplete types. */
6314 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6315 warning_at (loc, OPT_Wc___compat,
6316 "defining a type in a cast is invalid in C++");
6318 return ret;
6321 /* Build an assignment expression of lvalue LHS from value RHS.
6322 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6323 may differ from TREE_TYPE (LHS) for an enum bitfield.
6324 MODIFYCODE is the code for a binary operator that we use
6325 to combine the old value of LHS with RHS to get the new value.
6326 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6327 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6328 which may differ from TREE_TYPE (RHS) for an enum value.
6330 LOCATION is the location of the MODIFYCODE operator.
6331 RHS_LOC is the location of the RHS. */
6333 tree
6334 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6335 enum tree_code modifycode,
6336 location_t rhs_loc, tree rhs, tree rhs_origtype)
6338 tree result;
6339 tree newrhs;
6340 tree rhseval = NULL_TREE;
6341 tree lhstype = TREE_TYPE (lhs);
6342 tree olhstype = lhstype;
6343 bool npc;
6344 bool is_atomic_op;
6346 /* Types that aren't fully specified cannot be used in assignments. */
6347 lhs = require_complete_type (location, lhs);
6349 /* Avoid duplicate error messages from operands that had errors. */
6350 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6351 return error_mark_node;
6353 /* Ensure an error for assigning a non-lvalue array to an array in
6354 C90. */
6355 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6357 error_at (location, "assignment to expression with array type");
6358 return error_mark_node;
6361 /* For ObjC properties, defer this check. */
6362 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6363 return error_mark_node;
6365 is_atomic_op = really_atomic_lvalue (lhs);
6367 newrhs = rhs;
6369 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6371 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6372 lhs_origtype, modifycode, rhs_loc, rhs,
6373 rhs_origtype);
6374 if (inner == error_mark_node)
6375 return error_mark_node;
6376 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6377 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6378 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6379 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6380 protected_set_expr_location (result, location);
6381 return result;
6384 /* If a binary op has been requested, combine the old LHS value with the RHS
6385 producing the value we should actually store into the LHS. */
6387 if (modifycode != NOP_EXPR)
6389 lhs = c_fully_fold (lhs, false, NULL, true);
6390 lhs = stabilize_reference (lhs);
6392 /* Construct the RHS for any non-atomic compound assignemnt. */
6393 if (!is_atomic_op)
6395 /* If in LHS op= RHS the RHS has side-effects, ensure they
6396 are preevaluated before the rest of the assignment expression's
6397 side-effects, because RHS could contain e.g. function calls
6398 that modify LHS. */
6399 if (TREE_SIDE_EFFECTS (rhs))
6401 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6402 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6403 else
6404 newrhs = save_expr (rhs);
6405 rhseval = newrhs;
6406 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6407 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6408 newrhs);
6410 newrhs = build_binary_op (location,
6411 modifycode, lhs, newrhs, true);
6413 /* The original type of the right hand side is no longer
6414 meaningful. */
6415 rhs_origtype = NULL_TREE;
6419 if (c_dialect_objc ())
6421 /* Check if we are modifying an Objective-C property reference;
6422 if so, we need to generate setter calls. */
6423 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6424 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6425 else
6426 result = objc_maybe_build_modify_expr (lhs, newrhs);
6427 if (result)
6428 goto return_result;
6430 /* Else, do the check that we postponed for Objective-C. */
6431 if (!lvalue_or_else (location, lhs, lv_assign))
6432 return error_mark_node;
6435 /* Give an error for storing in something that is 'const'. */
6437 if (TYPE_READONLY (lhstype)
6438 || (RECORD_OR_UNION_TYPE_P (lhstype)
6439 && C_TYPE_FIELDS_READONLY (lhstype)))
6441 readonly_error (location, lhs, lv_assign);
6442 return error_mark_node;
6444 else if (TREE_READONLY (lhs))
6445 readonly_warning (lhs, lv_assign);
6447 /* If storing into a structure or union member,
6448 it has probably been given type `int'.
6449 Compute the type that would go with
6450 the actual amount of storage the member occupies. */
6452 if (TREE_CODE (lhs) == COMPONENT_REF
6453 && (TREE_CODE (lhstype) == INTEGER_TYPE
6454 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6455 || SCALAR_FLOAT_TYPE_P (lhstype)
6456 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6457 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6459 /* If storing in a field that is in actuality a short or narrower than one,
6460 we must store in the field in its actual type. */
6462 if (lhstype != TREE_TYPE (lhs))
6464 lhs = copy_node (lhs);
6465 TREE_TYPE (lhs) = lhstype;
6468 /* Issue -Wc++-compat warnings about an assignment to an enum type
6469 when LHS does not have its original type. This happens for,
6470 e.g., an enum bitfield in a struct. */
6471 if (warn_cxx_compat
6472 && lhs_origtype != NULL_TREE
6473 && lhs_origtype != lhstype
6474 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6476 tree checktype = (rhs_origtype != NULL_TREE
6477 ? rhs_origtype
6478 : TREE_TYPE (rhs));
6479 if (checktype != error_mark_node
6480 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6481 || (is_atomic_op && modifycode != NOP_EXPR)))
6482 warning_at (location, OPT_Wc___compat,
6483 "enum conversion in assignment is invalid in C++");
6486 /* Remove qualifiers. */
6487 lhstype = build_qualified_type (lhstype, TYPE_UNQUALIFIED);
6488 olhstype = build_qualified_type (olhstype, TYPE_UNQUALIFIED);
6490 /* Convert new value to destination type. Fold it first, then
6491 restore any excess precision information, for the sake of
6492 conversion warnings. */
6494 if (!(is_atomic_op && modifycode != NOP_EXPR))
6496 tree rhs_semantic_type = NULL_TREE;
6497 if (!c_in_omp_for)
6499 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6501 rhs_semantic_type = TREE_TYPE (newrhs);
6502 newrhs = TREE_OPERAND (newrhs, 0);
6504 npc = null_pointer_constant_p (newrhs);
6505 newrhs = c_fully_fold (newrhs, false, NULL);
6506 if (rhs_semantic_type)
6507 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6509 else
6510 npc = null_pointer_constant_p (newrhs);
6511 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6512 rhs_origtype, ic_assign, npc,
6513 NULL_TREE, NULL_TREE, 0);
6514 if (TREE_CODE (newrhs) == ERROR_MARK)
6515 return error_mark_node;
6518 /* Emit ObjC write barrier, if necessary. */
6519 if (c_dialect_objc () && flag_objc_gc)
6521 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6522 if (result)
6524 protected_set_expr_location (result, location);
6525 goto return_result;
6529 /* Scan operands. */
6531 if (is_atomic_op)
6532 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6533 else
6535 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6536 TREE_SIDE_EFFECTS (result) = 1;
6537 protected_set_expr_location (result, location);
6540 /* If we got the LHS in a different type for storing in,
6541 convert the result back to the nominal type of LHS
6542 so that the value we return always has the same type
6543 as the LHS argument. */
6545 if (olhstype == TREE_TYPE (result))
6546 goto return_result;
6548 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6549 rhs_origtype, ic_assign, false, NULL_TREE,
6550 NULL_TREE, 0);
6551 protected_set_expr_location (result, location);
6553 return_result:
6554 if (rhseval)
6555 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6556 return result;
6559 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6560 This is used to implement -fplan9-extensions. */
6562 static bool
6563 find_anonymous_field_with_type (tree struct_type, tree type)
6565 tree field;
6566 bool found;
6568 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6569 found = false;
6570 for (field = TYPE_FIELDS (struct_type);
6571 field != NULL_TREE;
6572 field = TREE_CHAIN (field))
6574 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6575 ? c_build_qualified_type (TREE_TYPE (field),
6576 TYPE_QUAL_ATOMIC)
6577 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6578 if (DECL_NAME (field) == NULL
6579 && comptypes (type, fieldtype))
6581 if (found)
6582 return false;
6583 found = true;
6585 else if (DECL_NAME (field) == NULL
6586 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6587 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6589 if (found)
6590 return false;
6591 found = true;
6594 return found;
6597 /* RHS is an expression whose type is pointer to struct. If there is
6598 an anonymous field in RHS with type TYPE, then return a pointer to
6599 that field in RHS. This is used with -fplan9-extensions. This
6600 returns NULL if no conversion could be found. */
6602 static tree
6603 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6605 tree rhs_struct_type, lhs_main_type;
6606 tree field, found_field;
6607 bool found_sub_field;
6608 tree ret;
6610 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6611 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6612 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6614 gcc_assert (POINTER_TYPE_P (type));
6615 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6616 ? c_build_qualified_type (TREE_TYPE (type),
6617 TYPE_QUAL_ATOMIC)
6618 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6620 found_field = NULL_TREE;
6621 found_sub_field = false;
6622 for (field = TYPE_FIELDS (rhs_struct_type);
6623 field != NULL_TREE;
6624 field = TREE_CHAIN (field))
6626 if (DECL_NAME (field) != NULL_TREE
6627 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6628 continue;
6629 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6630 ? c_build_qualified_type (TREE_TYPE (field),
6631 TYPE_QUAL_ATOMIC)
6632 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6633 if (comptypes (lhs_main_type, fieldtype))
6635 if (found_field != NULL_TREE)
6636 return NULL_TREE;
6637 found_field = field;
6639 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6640 lhs_main_type))
6642 if (found_field != NULL_TREE)
6643 return NULL_TREE;
6644 found_field = field;
6645 found_sub_field = true;
6649 if (found_field == NULL_TREE)
6650 return NULL_TREE;
6652 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6653 build_fold_indirect_ref (rhs), found_field,
6654 NULL_TREE);
6655 ret = build_fold_addr_expr_loc (location, ret);
6657 if (found_sub_field)
6659 ret = convert_to_anonymous_field (location, type, ret);
6660 gcc_assert (ret != NULL_TREE);
6663 return ret;
6666 /* Issue an error message for a bad initializer component.
6667 GMSGID identifies the message.
6668 The component name is taken from the spelling stack. */
6670 static void ATTRIBUTE_GCC_DIAG (2,0)
6671 error_init (location_t loc, const char *gmsgid, ...)
6673 char *ofwhat;
6675 auto_diagnostic_group d;
6677 /* The gmsgid may be a format string with %< and %>. */
6678 va_list ap;
6679 va_start (ap, gmsgid);
6680 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6681 va_end (ap);
6683 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6684 if (*ofwhat && warned)
6685 inform (loc, "(near initialization for %qs)", ofwhat);
6688 /* Issue a pedantic warning for a bad initializer component. OPT is
6689 the option OPT_* (from options.h) controlling this warning or 0 if
6690 it is unconditionally given. GMSGID identifies the message. The
6691 component name is taken from the spelling stack. */
6693 static void ATTRIBUTE_GCC_DIAG (3,0)
6694 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6696 /* Use the location where a macro was expanded rather than where
6697 it was defined to make sure macros defined in system headers
6698 but used incorrectly elsewhere are diagnosed. */
6699 location_t exploc = expansion_point_location_if_in_system_header (loc);
6700 auto_diagnostic_group d;
6701 va_list ap;
6702 va_start (ap, gmsgid);
6703 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6704 va_end (ap);
6705 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6706 if (*ofwhat && warned)
6707 inform (exploc, "(near initialization for %qs)", ofwhat);
6710 /* Issue a warning for a bad initializer component.
6712 OPT is the OPT_W* value corresponding to the warning option that
6713 controls this warning. GMSGID identifies the message. The
6714 component name is taken from the spelling stack. */
6716 static void
6717 warning_init (location_t loc, int opt, const char *gmsgid)
6719 char *ofwhat;
6720 bool warned;
6722 auto_diagnostic_group d;
6724 /* Use the location where a macro was expanded rather than where
6725 it was defined to make sure macros defined in system headers
6726 but used incorrectly elsewhere are diagnosed. */
6727 location_t exploc = expansion_point_location_if_in_system_header (loc);
6729 /* The gmsgid may be a format string with %< and %>. */
6730 warned = warning_at (exploc, opt, gmsgid);
6731 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6732 if (*ofwhat && warned)
6733 inform (exploc, "(near initialization for %qs)", ofwhat);
6736 /* If TYPE is an array type and EXPR is a parenthesized string
6737 constant, warn if pedantic that EXPR is being used to initialize an
6738 object of type TYPE. */
6740 void
6741 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6743 if (pedantic
6744 && TREE_CODE (type) == ARRAY_TYPE
6745 && TREE_CODE (expr.value) == STRING_CST
6746 && expr.original_code != STRING_CST)
6747 pedwarn_init (loc, OPT_Wpedantic,
6748 "array initialized from parenthesized string constant");
6751 /* Attempt to locate the parameter with the given index within FNDECL,
6752 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6754 static location_t
6755 get_fndecl_argument_location (tree fndecl, int argnum)
6757 int i;
6758 tree param;
6760 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6761 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6762 i < argnum && param;
6763 i++, param = TREE_CHAIN (param))
6766 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6767 return DECL_SOURCE_LOCATION (FNDECL). */
6768 if (param == NULL)
6769 return DECL_SOURCE_LOCATION (fndecl);
6771 return DECL_SOURCE_LOCATION (param);
6774 /* Issue a note about a mismatching argument for parameter PARMNUM
6775 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6776 Attempt to issue the note at the pertinent parameter of the decl;
6777 failing that issue it at the location of FUNDECL; failing that
6778 issue it at PLOC. */
6780 static void
6781 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6782 tree expected_type, tree actual_type)
6784 location_t loc;
6785 if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6786 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6787 else
6788 loc = ploc;
6790 inform (loc,
6791 "expected %qT but argument is of type %qT",
6792 expected_type, actual_type);
6795 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6796 function FUNDECL declared without prototype to parameter PARMNUM of
6797 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6799 static void
6800 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6801 tree parmtype, tree argtype)
6803 tree_code parmcode = TREE_CODE (parmtype);
6804 tree_code argcode = TREE_CODE (argtype);
6805 tree promoted = c_type_promotes_to (argtype);
6807 /* Avoid warning for enum arguments that promote to an integer type
6808 of the same size/mode. */
6809 if (parmcode == INTEGER_TYPE
6810 && argcode == ENUMERAL_TYPE
6811 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6812 return;
6814 if ((parmcode == argcode
6815 || (parmcode == INTEGER_TYPE
6816 && argcode == ENUMERAL_TYPE))
6817 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6818 return;
6820 /* This diagnoses even signed/unsigned mismatches. Those might be
6821 safe in many cases but GCC may emit suboptimal code for them so
6822 warning on those cases drives efficiency improvements. */
6823 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6824 TYPE_MAIN_VARIANT (promoted) == argtype
6825 ? G_("%qD argument %d type is %qT where %qT is expected "
6826 "in a call to built-in function declared without "
6827 "prototype")
6828 : G_("%qD argument %d promotes to %qT where %qT is expected "
6829 "in a call to built-in function declared without "
6830 "prototype"),
6831 fundecl, parmnum, promoted, parmtype))
6832 inform (DECL_SOURCE_LOCATION (fundecl),
6833 "built-in %qD declared here",
6834 fundecl);
6837 /* Convert value RHS to type TYPE as preparation for an assignment to
6838 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6839 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6840 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6841 constant before any folding.
6842 The real work of conversion is done by `convert'.
6843 The purpose of this function is to generate error messages
6844 for assignments that are not allowed in C.
6845 ERRTYPE says whether it is argument passing, assignment,
6846 initialization or return.
6848 In the following example, '~' denotes where EXPR_LOC and '^' where
6849 LOCATION point to:
6851 f (var); [ic_argpass]
6852 ^ ~~~
6853 x = var; [ic_assign]
6854 ^ ~~~;
6855 int x = var; [ic_init]
6857 return x; [ic_return]
6860 FUNCTION is a tree for the function being called.
6861 PARMNUM is the number of the argument, for printing in error messages.
6862 WARNOPT may be set to a warning option to issue the corresponding warning
6863 rather than an error for invalid conversions. Used for calls to built-in
6864 functions declared without a prototype. */
6866 static tree
6867 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6868 tree rhs, tree origtype, enum impl_conv errtype,
6869 bool null_pointer_constant, tree fundecl,
6870 tree function, int parmnum, int warnopt /* = 0 */)
6872 enum tree_code codel = TREE_CODE (type);
6873 tree orig_rhs = rhs;
6874 tree rhstype;
6875 enum tree_code coder;
6876 tree rname = NULL_TREE;
6877 bool objc_ok = false;
6879 /* Use the expansion point location to handle cases such as user's
6880 function returning a wrong-type macro defined in a system header. */
6881 location = expansion_point_location_if_in_system_header (location);
6883 if (errtype == ic_argpass)
6885 tree selector;
6886 /* Change pointer to function to the function itself for
6887 diagnostics. */
6888 if (TREE_CODE (function) == ADDR_EXPR
6889 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6890 function = TREE_OPERAND (function, 0);
6892 /* Handle an ObjC selector specially for diagnostics. */
6893 selector = objc_message_selector ();
6894 rname = function;
6895 if (selector && parmnum > 2)
6897 rname = selector;
6898 parmnum -= 2;
6902 /* This macro is used to emit diagnostics to ensure that all format
6903 strings are complete sentences, visible to gettext and checked at
6904 compile time. */
6905 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6906 do { \
6907 switch (errtype) \
6909 case ic_argpass: \
6911 auto_diagnostic_group d; \
6912 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6913 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6915 break; \
6916 case ic_assign: \
6917 pedwarn (LOCATION, OPT, AS); \
6918 break; \
6919 case ic_init: \
6920 case ic_init_const: \
6921 pedwarn_init (LOCATION, OPT, IN); \
6922 break; \
6923 case ic_return: \
6924 pedwarn (LOCATION, OPT, RE); \
6925 break; \
6926 default: \
6927 gcc_unreachable (); \
6929 } while (0)
6931 /* This macro is used to emit diagnostics to ensure that all format
6932 strings are complete sentences, visible to gettext and checked at
6933 compile time. It can be called with 'pedwarn' or 'warning_at'. */
6934 #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6935 do { \
6936 switch (errtype) \
6938 case ic_argpass: \
6940 auto_diagnostic_group d; \
6941 if (PEDWARN) { \
6942 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6943 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6944 } else { \
6945 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6946 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6949 break; \
6950 case ic_assign: \
6951 if (PEDWARN) \
6952 pedwarn (LOCATION, OPT, AS, QUALS); \
6953 else \
6954 warning_at (LOCATION, OPT, AS, QUALS); \
6955 break; \
6956 case ic_init: \
6957 case ic_init_const: \
6958 if (PEDWARN) \
6959 pedwarn (LOCATION, OPT, IN, QUALS); \
6960 else \
6961 warning_at (LOCATION, OPT, IN, QUALS); \
6962 break; \
6963 case ic_return: \
6964 if (PEDWARN) \
6965 pedwarn (LOCATION, OPT, RE, QUALS); \
6966 else \
6967 warning_at (LOCATION, OPT, RE, QUALS); \
6968 break; \
6969 default: \
6970 gcc_unreachable (); \
6972 } while (0)
6974 /* This macro is used to emit diagnostics to ensure that all format
6975 strings are complete sentences, visible to gettext and checked at
6976 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6977 extra parameter to enumerate qualifiers. */
6978 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6979 WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
6982 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6983 rhs = TREE_OPERAND (rhs, 0);
6985 rhstype = TREE_TYPE (rhs);
6986 coder = TREE_CODE (rhstype);
6988 if (coder == ERROR_MARK)
6989 return error_mark_node;
6991 if (c_dialect_objc ())
6993 int parmno;
6995 switch (errtype)
6997 case ic_return:
6998 parmno = 0;
6999 break;
7001 case ic_assign:
7002 parmno = -1;
7003 break;
7005 case ic_init:
7006 case ic_init_const:
7007 parmno = -2;
7008 break;
7010 default:
7011 parmno = parmnum;
7012 break;
7015 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
7018 if (warn_cxx_compat)
7020 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
7021 if (checktype != error_mark_node
7022 && TREE_CODE (type) == ENUMERAL_TYPE
7023 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
7024 switch (errtype)
7026 case ic_argpass:
7027 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
7028 "passing argument %d of %qE is invalid in C++",
7029 parmnum, rname))
7030 inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
7031 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
7032 "expected %qT but argument is of type %qT",
7033 type, rhstype);
7034 break;
7035 case ic_assign:
7036 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
7037 "%qT in assignment is invalid in C++", rhstype, type);
7038 break;
7039 case ic_init:
7040 case ic_init_const:
7041 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
7042 "%qT to %qT in initialization is invalid in C++",
7043 rhstype, type);
7044 break;
7045 case ic_return:
7046 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
7047 "%qT in return is invalid in C++", rhstype, type);
7048 break;
7049 default:
7050 gcc_unreachable ();
7054 if (warn_enum_conversion)
7056 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
7057 if (checktype != error_mark_node
7058 && TREE_CODE (checktype) == ENUMERAL_TYPE
7059 && TREE_CODE (type) == ENUMERAL_TYPE
7060 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
7062 gcc_rich_location loc (location);
7063 warning_at (&loc, OPT_Wenum_conversion,
7064 "implicit conversion from %qT to %qT",
7065 checktype, type);
7069 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
7071 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7072 return rhs;
7075 if (coder == VOID_TYPE)
7077 /* Except for passing an argument to an unprototyped function,
7078 this is a constraint violation. When passing an argument to
7079 an unprototyped function, it is compile-time undefined;
7080 making it a constraint in that case was rejected in
7081 DR#252. */
7082 const char msg[] = "void value not ignored as it ought to be";
7083 if (warnopt)
7084 warning_at (location, warnopt, msg);
7085 else
7086 error_at (location, msg);
7087 return error_mark_node;
7089 rhs = require_complete_type (location, rhs);
7090 if (rhs == error_mark_node)
7091 return error_mark_node;
7093 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
7094 return error_mark_node;
7096 /* A non-reference type can convert to a reference. This handles
7097 va_start, va_copy and possibly port built-ins. */
7098 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
7100 if (!lvalue_p (rhs))
7102 const char msg[] = "cannot pass rvalue to reference parameter";
7103 if (warnopt)
7104 warning_at (location, warnopt, msg);
7105 else
7106 error_at (location, msg);
7107 return error_mark_node;
7109 if (!c_mark_addressable (rhs))
7110 return error_mark_node;
7111 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
7112 SET_EXPR_LOCATION (rhs, location);
7114 rhs = convert_for_assignment (location, expr_loc,
7115 build_pointer_type (TREE_TYPE (type)),
7116 rhs, origtype, errtype,
7117 null_pointer_constant, fundecl, function,
7118 parmnum, warnopt);
7119 if (rhs == error_mark_node)
7120 return error_mark_node;
7122 rhs = build1 (NOP_EXPR, type, rhs);
7123 SET_EXPR_LOCATION (rhs, location);
7124 return rhs;
7126 /* Some types can interconvert without explicit casts. */
7127 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
7128 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
7129 return convert (type, rhs);
7130 /* Arithmetic types all interconvert, and enum is treated like int. */
7131 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
7132 || codel == FIXED_POINT_TYPE
7133 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
7134 || codel == BOOLEAN_TYPE)
7135 && (coder == INTEGER_TYPE || coder == REAL_TYPE
7136 || coder == FIXED_POINT_TYPE
7137 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
7138 || coder == BOOLEAN_TYPE))
7140 if (warnopt && errtype == ic_argpass)
7141 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
7142 rhstype);
7144 bool save = in_late_binary_op;
7145 if (C_BOOLEAN_TYPE_P (type) || codel == COMPLEX_TYPE
7146 || (coder == REAL_TYPE
7147 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
7148 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
7149 in_late_binary_op = true;
7150 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
7151 ? expr_loc : location, type, orig_rhs,
7152 errtype == ic_init_const);
7153 in_late_binary_op = save;
7154 return ret;
7157 /* Aggregates in different TUs might need conversion. */
7158 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
7159 && codel == coder
7160 && comptypes (type, rhstype))
7161 return convert_and_check (expr_loc != UNKNOWN_LOCATION
7162 ? expr_loc : location, type, rhs);
7164 /* Conversion to a transparent union or record from its member types.
7165 This applies only to function arguments. */
7166 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
7167 && TYPE_TRANSPARENT_AGGR (type))
7168 && errtype == ic_argpass)
7170 tree memb, marginal_memb = NULL_TREE;
7172 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
7174 tree memb_type = TREE_TYPE (memb);
7176 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
7177 TYPE_MAIN_VARIANT (rhstype)))
7178 break;
7180 if (TREE_CODE (memb_type) != POINTER_TYPE)
7181 continue;
7183 if (coder == POINTER_TYPE)
7185 tree ttl = TREE_TYPE (memb_type);
7186 tree ttr = TREE_TYPE (rhstype);
7188 /* Any non-function converts to a [const][volatile] void *
7189 and vice versa; otherwise, targets must be the same.
7190 Meanwhile, the lhs target must have all the qualifiers of
7191 the rhs. */
7192 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7193 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7194 || comp_target_types (location, memb_type, rhstype))
7196 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
7197 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
7198 /* If this type won't generate any warnings, use it. */
7199 if (lquals == rquals
7200 || ((TREE_CODE (ttr) == FUNCTION_TYPE
7201 && TREE_CODE (ttl) == FUNCTION_TYPE)
7202 ? ((lquals | rquals) == rquals)
7203 : ((lquals | rquals) == lquals)))
7204 break;
7206 /* Keep looking for a better type, but remember this one. */
7207 if (!marginal_memb)
7208 marginal_memb = memb;
7212 /* Can convert integer zero to any pointer type. */
7213 if (null_pointer_constant)
7215 rhs = null_pointer_node;
7216 break;
7220 if (memb || marginal_memb)
7222 if (!memb)
7224 /* We have only a marginally acceptable member type;
7225 it needs a warning. */
7226 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
7227 tree ttr = TREE_TYPE (rhstype);
7229 /* Const and volatile mean something different for function
7230 types, so the usual warnings are not appropriate. */
7231 if (TREE_CODE (ttr) == FUNCTION_TYPE
7232 && TREE_CODE (ttl) == FUNCTION_TYPE)
7234 /* Because const and volatile on functions are
7235 restrictions that say the function will not do
7236 certain things, it is okay to use a const or volatile
7237 function where an ordinary one is wanted, but not
7238 vice-versa. */
7239 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7240 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7241 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7242 OPT_Wdiscarded_qualifiers,
7243 G_("passing argument %d of %qE "
7244 "makes %q#v qualified function "
7245 "pointer from unqualified"),
7246 G_("assignment makes %q#v qualified "
7247 "function pointer from "
7248 "unqualified"),
7249 G_("initialization makes %q#v qualified "
7250 "function pointer from "
7251 "unqualified"),
7252 G_("return makes %q#v qualified function "
7253 "pointer from unqualified"),
7254 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7256 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7257 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7258 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7259 OPT_Wdiscarded_qualifiers,
7260 G_("passing argument %d of %qE discards "
7261 "%qv qualifier from pointer target type"),
7262 G_("assignment discards %qv qualifier "
7263 "from pointer target type"),
7264 G_("initialization discards %qv qualifier "
7265 "from pointer target type"),
7266 G_("return discards %qv qualifier from "
7267 "pointer target type"),
7268 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7270 memb = marginal_memb;
7273 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7274 pedwarn (location, OPT_Wpedantic,
7275 "ISO C prohibits argument conversion to union type");
7277 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7278 return build_constructor_single (type, memb, rhs);
7282 /* Conversions among pointers */
7283 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7284 && (coder == codel))
7286 /* If RHS refers to a built-in declared without a prototype
7287 BLTIN is the declaration of the built-in with a prototype
7288 and RHSTYPE is set to the actual type of the built-in. */
7289 tree bltin;
7290 rhstype = type_or_builtin_type (rhs, &bltin);
7292 tree ttl = TREE_TYPE (type);
7293 tree ttr = TREE_TYPE (rhstype);
7294 tree mvl = ttl;
7295 tree mvr = ttr;
7296 bool is_opaque_pointer;
7297 int target_cmp = 0; /* Cache comp_target_types () result. */
7298 addr_space_t asl;
7299 addr_space_t asr;
7301 if (TREE_CODE (mvl) != ARRAY_TYPE)
7302 mvl = (TYPE_ATOMIC (mvl)
7303 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7304 TYPE_QUAL_ATOMIC)
7305 : TYPE_MAIN_VARIANT (mvl));
7306 if (TREE_CODE (mvr) != ARRAY_TYPE)
7307 mvr = (TYPE_ATOMIC (mvr)
7308 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7309 TYPE_QUAL_ATOMIC)
7310 : TYPE_MAIN_VARIANT (mvr));
7311 /* Opaque pointers are treated like void pointers. */
7312 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7314 /* The Plan 9 compiler permits a pointer to a struct to be
7315 automatically converted into a pointer to an anonymous field
7316 within the struct. */
7317 if (flag_plan9_extensions
7318 && RECORD_OR_UNION_TYPE_P (mvl)
7319 && RECORD_OR_UNION_TYPE_P (mvr)
7320 && mvl != mvr)
7322 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7323 if (new_rhs != NULL_TREE)
7325 rhs = new_rhs;
7326 rhstype = TREE_TYPE (rhs);
7327 coder = TREE_CODE (rhstype);
7328 ttr = TREE_TYPE (rhstype);
7329 mvr = TYPE_MAIN_VARIANT (ttr);
7333 /* C++ does not allow the implicit conversion void* -> T*. However,
7334 for the purpose of reducing the number of false positives, we
7335 tolerate the special case of
7337 int *p = NULL;
7339 where NULL is typically defined in C to be '(void *) 0'. */
7340 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7341 warning_at (errtype == ic_argpass ? expr_loc : location,
7342 OPT_Wc___compat,
7343 "request for implicit conversion "
7344 "from %qT to %qT not permitted in C++", rhstype, type);
7346 /* See if the pointers point to incompatible address spaces. */
7347 asl = TYPE_ADDR_SPACE (ttl);
7348 asr = TYPE_ADDR_SPACE (ttr);
7349 if (!null_pointer_constant_p (rhs)
7350 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7352 auto_diagnostic_group d;
7353 bool diagnosed = true;
7354 switch (errtype)
7356 case ic_argpass:
7358 const char msg[] = G_("passing argument %d of %qE from "
7359 "pointer to non-enclosed address space");
7360 if (warnopt)
7361 diagnosed
7362 = warning_at (expr_loc, warnopt, msg, parmnum, rname);
7363 else
7364 error_at (expr_loc, msg, parmnum, rname);
7365 break;
7367 case ic_assign:
7369 const char msg[] = G_("assignment from pointer to "
7370 "non-enclosed address space");
7371 if (warnopt)
7372 diagnosed = warning_at (location, warnopt, msg);
7373 else
7374 error_at (location, msg);
7375 break;
7377 case ic_init:
7378 case ic_init_const:
7380 const char msg[] = G_("initialization from pointer to "
7381 "non-enclosed address space");
7382 if (warnopt)
7383 diagnosed = warning_at (location, warnopt, msg);
7384 else
7385 error_at (location, msg);
7386 break;
7388 case ic_return:
7390 const char msg[] = G_("return from pointer to "
7391 "non-enclosed address space");
7392 if (warnopt)
7393 diagnosed = warning_at (location, warnopt, msg);
7394 else
7395 error_at (location, msg);
7396 break;
7398 default:
7399 gcc_unreachable ();
7401 if (diagnosed)
7403 if (errtype == ic_argpass)
7404 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7405 else
7406 inform (location, "expected %qT but pointer is of type %qT",
7407 type, rhstype);
7409 return error_mark_node;
7412 /* Check if the right-hand side has a format attribute but the
7413 left-hand side doesn't. */
7414 if (warn_suggest_attribute_format
7415 && check_missing_format_attribute (type, rhstype))
7417 switch (errtype)
7419 case ic_argpass:
7420 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7421 "argument %d of %qE might be "
7422 "a candidate for a format attribute",
7423 parmnum, rname);
7424 break;
7425 case ic_assign:
7426 warning_at (location, OPT_Wsuggest_attribute_format,
7427 "assignment left-hand side might be "
7428 "a candidate for a format attribute");
7429 break;
7430 case ic_init:
7431 case ic_init_const:
7432 warning_at (location, OPT_Wsuggest_attribute_format,
7433 "initialization left-hand side might be "
7434 "a candidate for a format attribute");
7435 break;
7436 case ic_return:
7437 warning_at (location, OPT_Wsuggest_attribute_format,
7438 "return type might be "
7439 "a candidate for a format attribute");
7440 break;
7441 default:
7442 gcc_unreachable ();
7446 /* See if the pointers point to incompatible scalar storage orders. */
7447 if (warn_scalar_storage_order
7448 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7449 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7451 tree t;
7453 switch (errtype)
7455 case ic_argpass:
7456 /* Do not warn for built-in functions, for example memcpy, since we
7457 control how they behave and they can be useful in this area. */
7458 if (TREE_CODE (rname) != FUNCTION_DECL
7459 || !fndecl_built_in_p (rname))
7460 warning_at (location, OPT_Wscalar_storage_order,
7461 "passing argument %d of %qE from incompatible "
7462 "scalar storage order", parmnum, rname);
7463 break;
7464 case ic_assign:
7465 /* Do not warn if the RHS is a call to a function that returns a
7466 pointer that is not an alias. */
7467 if (TREE_CODE (rhs) != CALL_EXPR
7468 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7469 || !DECL_IS_MALLOC (t))
7470 warning_at (location, OPT_Wscalar_storage_order,
7471 "assignment to %qT from pointer type %qT with "
7472 "incompatible scalar storage order", type, rhstype);
7473 break;
7474 case ic_init:
7475 case ic_init_const:
7476 /* Likewise. */
7477 if (TREE_CODE (rhs) != CALL_EXPR
7478 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7479 || !DECL_IS_MALLOC (t))
7480 warning_at (location, OPT_Wscalar_storage_order,
7481 "initialization of %qT from pointer type %qT with "
7482 "incompatible scalar storage order", type, rhstype);
7483 break;
7484 case ic_return:
7485 warning_at (location, OPT_Wscalar_storage_order,
7486 "returning %qT from pointer type with incompatible "
7487 "scalar storage order %qT", rhstype, type);
7488 break;
7489 default:
7490 gcc_unreachable ();
7494 /* Any non-function converts to a [const][volatile] void *
7495 and vice versa; otherwise, targets must be the same.
7496 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7497 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7498 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7499 || (target_cmp = comp_target_types (location, type, rhstype))
7500 || is_opaque_pointer
7501 || ((c_common_unsigned_type (mvl)
7502 == c_common_unsigned_type (mvr))
7503 && (c_common_signed_type (mvl)
7504 == c_common_signed_type (mvr))
7505 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7507 /* Warn about loss of qualifers from pointers to arrays with
7508 qualifiers on the element type. */
7509 if (TREE_CODE (ttr) == ARRAY_TYPE)
7511 ttr = strip_array_types (ttr);
7512 ttl = strip_array_types (ttl);
7514 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7515 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7516 WARNING_FOR_QUALIFIERS (flag_isoc2x,
7517 location, expr_loc,
7518 OPT_Wdiscarded_array_qualifiers,
7519 G_("passing argument %d of %qE discards "
7520 "%qv qualifier from pointer target type"),
7521 G_("assignment discards %qv qualifier "
7522 "from pointer target type"),
7523 G_("initialization discards %qv qualifier "
7524 "from pointer target type"),
7525 G_("return discards %qv qualifier from "
7526 "pointer target type"),
7527 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7529 else if (pedantic
7530 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7532 (VOID_TYPE_P (ttr)
7533 && !null_pointer_constant
7534 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7535 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7536 G_("ISO C forbids passing argument %d of "
7537 "%qE between function pointer "
7538 "and %<void *%>"),
7539 G_("ISO C forbids assignment between "
7540 "function pointer and %<void *%>"),
7541 G_("ISO C forbids initialization between "
7542 "function pointer and %<void *%>"),
7543 G_("ISO C forbids return between function "
7544 "pointer and %<void *%>"));
7545 /* Const and volatile mean something different for function types,
7546 so the usual warnings are not appropriate. */
7547 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7548 && TREE_CODE (ttl) != FUNCTION_TYPE)
7550 /* Assignments between atomic and non-atomic objects are OK. */
7551 bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7552 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
7553 bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7554 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
7556 /* Don't warn about loss of qualifier for conversions from
7557 qualified void* to pointers to arrays with corresponding
7558 qualifier on the element type (except for pedantic before C23). */
7559 if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc2x))
7560 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7561 OPT_Wdiscarded_qualifiers,
7562 G_("passing argument %d of %qE discards "
7563 "%qv qualifier from pointer target type"),
7564 G_("assignment discards %qv qualifier "
7565 "from pointer target type"),
7566 G_("initialization discards %qv qualifier "
7567 "from pointer target type"),
7568 G_("return discards %qv qualifier from "
7569 "pointer target type"),
7570 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7571 else if (warn_quals_ped)
7572 pedwarn_c11 (location, OPT_Wc11_c2x_compat,
7573 "array with qualifier on the element is not qualified before C2X");
7575 /* If this is not a case of ignoring a mismatch in signedness,
7576 no warning. */
7577 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7578 || target_cmp)
7580 /* If there is a mismatch, do warn. */
7581 else if (warn_pointer_sign)
7582 switch (errtype)
7584 case ic_argpass:
7586 auto_diagnostic_group d;
7587 range_label_for_type_mismatch rhs_label (rhstype, type);
7588 gcc_rich_location richloc (expr_loc, &rhs_label);
7589 if (pedwarn (&richloc, OPT_Wpointer_sign,
7590 "pointer targets in passing argument %d of "
7591 "%qE differ in signedness", parmnum, rname))
7592 inform_for_arg (fundecl, expr_loc, parmnum, type,
7593 rhstype);
7595 break;
7596 case ic_assign:
7597 pedwarn (location, OPT_Wpointer_sign,
7598 "pointer targets in assignment from %qT to %qT "
7599 "differ in signedness", rhstype, type);
7600 break;
7601 case ic_init:
7602 case ic_init_const:
7603 pedwarn_init (location, OPT_Wpointer_sign,
7604 "pointer targets in initialization of %qT "
7605 "from %qT differ in signedness", type,
7606 rhstype);
7607 break;
7608 case ic_return:
7609 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7610 "returning %qT from a function with return type "
7611 "%qT differ in signedness", rhstype, type);
7612 break;
7613 default:
7614 gcc_unreachable ();
7617 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7618 && TREE_CODE (ttr) == FUNCTION_TYPE)
7620 /* Because const and volatile on functions are restrictions
7621 that say the function will not do certain things,
7622 it is okay to use a const or volatile function
7623 where an ordinary one is wanted, but not vice-versa. */
7624 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7625 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7626 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7627 OPT_Wdiscarded_qualifiers,
7628 G_("passing argument %d of %qE makes "
7629 "%q#v qualified function pointer "
7630 "from unqualified"),
7631 G_("assignment makes %q#v qualified function "
7632 "pointer from unqualified"),
7633 G_("initialization makes %q#v qualified "
7634 "function pointer from unqualified"),
7635 G_("return makes %q#v qualified function "
7636 "pointer from unqualified"),
7637 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7640 /* Avoid warning about the volatile ObjC EH puts on decls. */
7641 else if (!objc_ok)
7643 switch (errtype)
7645 case ic_argpass:
7647 auto_diagnostic_group d;
7648 range_label_for_type_mismatch rhs_label (rhstype, type);
7649 gcc_rich_location richloc (expr_loc, &rhs_label);
7650 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7651 "passing argument %d of %qE from incompatible "
7652 "pointer type", parmnum, rname))
7653 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7655 break;
7656 case ic_assign:
7657 if (bltin)
7658 pedwarn (location, OPT_Wincompatible_pointer_types,
7659 "assignment to %qT from pointer to "
7660 "%qD with incompatible type %qT",
7661 type, bltin, rhstype);
7662 else
7663 pedwarn (location, OPT_Wincompatible_pointer_types,
7664 "assignment to %qT from incompatible pointer type %qT",
7665 type, rhstype);
7666 break;
7667 case ic_init:
7668 case ic_init_const:
7669 if (bltin)
7670 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7671 "initialization of %qT from pointer to "
7672 "%qD with incompatible type %qT",
7673 type, bltin, rhstype);
7674 else
7675 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7676 "initialization of %qT from incompatible "
7677 "pointer type %qT",
7678 type, rhstype);
7679 break;
7680 case ic_return:
7681 if (bltin)
7682 pedwarn (location, OPT_Wincompatible_pointer_types,
7683 "returning pointer to %qD of type %qT from "
7684 "a function with incompatible type %qT",
7685 bltin, rhstype, type);
7686 else
7687 pedwarn (location, OPT_Wincompatible_pointer_types,
7688 "returning %qT from a function with incompatible "
7689 "return type %qT", rhstype, type);
7690 break;
7691 default:
7692 gcc_unreachable ();
7696 /* If RHS isn't an address, check pointer or array of packed
7697 struct or union. */
7698 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7700 return convert (type, rhs);
7702 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7704 /* ??? This should not be an error when inlining calls to
7705 unprototyped functions. */
7706 const char msg[] = "invalid use of non-lvalue array";
7707 if (warnopt)
7708 warning_at (location, warnopt, msg);
7709 else
7710 error_at (location, msg);
7711 return error_mark_node;
7713 else if (codel == POINTER_TYPE
7714 && (coder == INTEGER_TYPE || coder == NULLPTR_TYPE))
7716 /* An explicit constant 0 or type nullptr_t can convert to a pointer,
7717 or one that results from arithmetic, even including a cast to
7718 integer type. */
7719 if (!null_pointer_constant && coder != NULLPTR_TYPE)
7720 switch (errtype)
7722 case ic_argpass:
7724 auto_diagnostic_group d;
7725 range_label_for_type_mismatch rhs_label (rhstype, type);
7726 gcc_rich_location richloc (expr_loc, &rhs_label);
7727 if (pedwarn (&richloc, OPT_Wint_conversion,
7728 "passing argument %d of %qE makes pointer from "
7729 "integer without a cast", parmnum, rname))
7730 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7732 break;
7733 case ic_assign:
7734 pedwarn (location, OPT_Wint_conversion,
7735 "assignment to %qT from %qT makes pointer from integer "
7736 "without a cast", type, rhstype);
7737 break;
7738 case ic_init:
7739 case ic_init_const:
7740 pedwarn_init (location, OPT_Wint_conversion,
7741 "initialization of %qT from %qT makes pointer from "
7742 "integer without a cast", type, rhstype);
7743 break;
7744 case ic_return:
7745 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7746 "function with return type %qT makes pointer from "
7747 "integer without a cast", rhstype, type);
7748 break;
7749 default:
7750 gcc_unreachable ();
7753 return convert (type, rhs);
7755 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
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_Wint_conversion,
7765 "passing argument %d of %qE makes integer from "
7766 "pointer without a cast", parmnum, rname))
7767 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7769 break;
7770 case ic_assign:
7771 pedwarn (location, OPT_Wint_conversion,
7772 "assignment to %qT from %qT makes integer from pointer "
7773 "without a cast", type, rhstype);
7774 break;
7775 case ic_init:
7776 case ic_init_const:
7777 pedwarn_init (location, OPT_Wint_conversion,
7778 "initialization of %qT from %qT makes integer from "
7779 "pointer without a cast", type, rhstype);
7780 break;
7781 case ic_return:
7782 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7783 "function with return type %qT makes integer from "
7784 "pointer without a cast", rhstype, type);
7785 break;
7786 default:
7787 gcc_unreachable ();
7790 return convert (type, rhs);
7792 else if (C_BOOLEAN_TYPE_P (type)
7793 /* The type nullptr_t may be converted to bool. The
7794 result is false. */
7795 && (coder == POINTER_TYPE || coder == NULLPTR_TYPE))
7797 tree ret;
7798 bool save = in_late_binary_op;
7799 in_late_binary_op = true;
7800 ret = convert (type, rhs);
7801 in_late_binary_op = save;
7802 return ret;
7804 else if (codel == NULLPTR_TYPE && null_pointer_constant)
7805 return convert (type, rhs);
7807 switch (errtype)
7809 case ic_argpass:
7811 auto_diagnostic_group d;
7812 range_label_for_type_mismatch rhs_label (rhstype, type);
7813 gcc_rich_location richloc (expr_loc, &rhs_label);
7814 const char msg[] = G_("incompatible type for argument %d of %qE");
7815 if (warnopt)
7816 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7817 else
7818 error_at (&richloc, msg, parmnum, rname);
7819 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7821 break;
7822 case ic_assign:
7824 const char msg[]
7825 = G_("incompatible types when assigning to type %qT from type %qT");
7826 if (warnopt)
7827 warning_at (expr_loc, 0, msg, type, rhstype);
7828 else
7829 error_at (expr_loc, msg, type, rhstype);
7830 break;
7832 case ic_init:
7833 case ic_init_const:
7835 const char msg[]
7836 = G_("incompatible types when initializing type %qT using type %qT");
7837 if (warnopt)
7838 warning_at (location, 0, msg, type, rhstype);
7839 else
7840 error_at (location, msg, type, rhstype);
7841 break;
7843 case ic_return:
7845 const char msg[]
7846 = G_("incompatible types when returning type %qT but %qT was expected");
7847 if (warnopt)
7848 warning_at (location, 0, msg, rhstype, type);
7849 else
7850 error_at (location, msg, rhstype, type);
7851 break;
7853 default:
7854 gcc_unreachable ();
7857 return error_mark_node;
7860 /* If VALUE is a compound expr all of whose expressions are constant, then
7861 return its value. Otherwise, return error_mark_node.
7863 This is for handling COMPOUND_EXPRs as initializer elements
7864 which is allowed with a warning when -pedantic is specified. */
7866 static tree
7867 valid_compound_expr_initializer (tree value, tree endtype)
7869 if (TREE_CODE (value) == COMPOUND_EXPR)
7871 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7872 == error_mark_node)
7873 return error_mark_node;
7874 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7875 endtype);
7877 else if (!initializer_constant_valid_p (value, endtype))
7878 return error_mark_node;
7879 else
7880 return value;
7883 /* Perform appropriate conversions on the initial value of a variable,
7884 store it in the declaration DECL,
7885 and print any error messages that are appropriate.
7886 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7887 If the init is invalid, store an ERROR_MARK.
7889 INIT_LOC is the location of the initial value. */
7891 void
7892 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7894 tree value, type;
7895 bool npc = false;
7896 bool int_const_expr = false;
7897 bool arith_const_expr = false;
7899 /* If variable's type was invalidly declared, just ignore it. */
7901 type = TREE_TYPE (decl);
7902 if (TREE_CODE (type) == ERROR_MARK)
7903 return;
7905 /* Digest the specified initializer into an expression. */
7907 if (init)
7909 npc = null_pointer_constant_p (init);
7910 int_const_expr = (TREE_CODE (init) == INTEGER_CST
7911 && !TREE_OVERFLOW (init)
7912 && INTEGRAL_TYPE_P (TREE_TYPE (init)));
7913 /* Not fully determined before folding. */
7914 arith_const_expr = true;
7916 bool constexpr_p = (VAR_P (decl)
7917 && C_DECL_DECLARED_CONSTEXPR (decl));
7918 value = digest_init (init_loc, type, init, origtype, npc, int_const_expr,
7919 arith_const_expr, true,
7920 TREE_STATIC (decl) || constexpr_p, constexpr_p);
7922 /* Store the expression if valid; else report error. */
7924 if (!in_system_header_at (input_location)
7925 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7926 warning (OPT_Wtraditional, "traditional C rejects automatic "
7927 "aggregate initialization");
7929 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7930 DECL_INITIAL (decl) = value;
7932 /* ANSI wants warnings about out-of-range constant initializers. */
7933 STRIP_TYPE_NOPS (value);
7934 if (TREE_STATIC (decl))
7935 constant_expression_warning (value);
7937 /* Check if we need to set array size from compound literal size. */
7938 if (TREE_CODE (type) == ARRAY_TYPE
7939 && TYPE_DOMAIN (type) == NULL_TREE
7940 && value != error_mark_node)
7942 tree inside_init = init;
7944 STRIP_TYPE_NOPS (inside_init);
7945 inside_init = fold (inside_init);
7947 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7949 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7951 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7953 /* For int foo[] = (int [3]){1}; we need to set array size
7954 now since later on array initializer will be just the
7955 brace enclosed list of the compound literal. */
7956 tree etype = strip_array_types (TREE_TYPE (decl));
7957 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7958 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7959 layout_type (type);
7960 layout_decl (cldecl, 0);
7961 TREE_TYPE (decl)
7962 = c_build_qualified_type (type, TYPE_QUALS (etype));
7968 /* Methods for storing and printing names for error messages. */
7970 /* Implement a spelling stack that allows components of a name to be pushed
7971 and popped. Each element on the stack is this structure. */
7973 struct spelling
7975 int kind;
7976 union
7978 unsigned HOST_WIDE_INT i;
7979 const char *s;
7980 } u;
7983 #define SPELLING_STRING 1
7984 #define SPELLING_MEMBER 2
7985 #define SPELLING_BOUNDS 3
7987 static struct spelling *spelling; /* Next stack element (unused). */
7988 static struct spelling *spelling_base; /* Spelling stack base. */
7989 static int spelling_size; /* Size of the spelling stack. */
7991 /* Macros to save and restore the spelling stack around push_... functions.
7992 Alternative to SAVE_SPELLING_STACK. */
7994 #define SPELLING_DEPTH() (spelling - spelling_base)
7995 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7997 /* Push an element on the spelling stack with type KIND and assign VALUE
7998 to MEMBER. */
8000 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
8002 int depth = SPELLING_DEPTH (); \
8004 if (depth >= spelling_size) \
8006 spelling_size += 10; \
8007 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
8008 spelling_size); \
8009 RESTORE_SPELLING_DEPTH (depth); \
8012 spelling->kind = (KIND); \
8013 spelling->MEMBER = (VALUE); \
8014 spelling++; \
8017 /* Push STRING on the stack. Printed literally. */
8019 static void
8020 push_string (const char *string)
8022 PUSH_SPELLING (SPELLING_STRING, string, u.s);
8025 /* Push a member name on the stack. Printed as '.' STRING. */
8027 static void
8028 push_member_name (tree decl)
8030 const char *const string
8031 = (DECL_NAME (decl)
8032 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
8033 : _("<anonymous>"));
8034 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
8037 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
8039 static void
8040 push_array_bounds (unsigned HOST_WIDE_INT bounds)
8042 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
8045 /* Compute the maximum size in bytes of the printed spelling. */
8047 static int
8048 spelling_length (void)
8050 int size = 0;
8051 struct spelling *p;
8053 for (p = spelling_base; p < spelling; p++)
8055 if (p->kind == SPELLING_BOUNDS)
8056 size += 25;
8057 else
8058 size += strlen (p->u.s) + 1;
8061 return size;
8064 /* Print the spelling to BUFFER and return it. */
8066 static char *
8067 print_spelling (char *buffer)
8069 char *d = buffer;
8070 struct spelling *p;
8072 for (p = spelling_base; p < spelling; p++)
8073 if (p->kind == SPELLING_BOUNDS)
8075 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
8076 d += strlen (d);
8078 else
8080 const char *s;
8081 if (p->kind == SPELLING_MEMBER)
8082 *d++ = '.';
8083 for (s = p->u.s; (*d = *s++); d++)
8086 *d++ = '\0';
8087 return buffer;
8090 /* Check whether INIT, a floating or integer constant, is
8091 representable in TYPE, a real floating type with the same radix or
8092 a decimal floating type initialized with a binary floating
8093 constant. Return true if OK, false if not. */
8094 static bool
8095 constexpr_init_fits_real_type (tree type, tree init)
8097 gcc_assert (SCALAR_FLOAT_TYPE_P (type));
8098 gcc_assert (TREE_CODE (init) == INTEGER_CST || TREE_CODE (init) == REAL_CST);
8099 if (TREE_CODE (init) == REAL_CST
8100 && TYPE_MODE (TREE_TYPE (init)) == TYPE_MODE (type))
8102 /* Same mode, no conversion required except for the case of
8103 signaling NaNs if the types are incompatible (e.g. double and
8104 long double with the same mode). */
8105 if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init))
8106 && !comptypes (TYPE_MAIN_VARIANT (type),
8107 TYPE_MAIN_VARIANT (TREE_TYPE (init))))
8108 return false;
8109 return true;
8111 if (TREE_CODE (init) == INTEGER_CST)
8113 tree converted = build_real_from_int_cst (type, init);
8114 bool fail = false;
8115 wide_int w = real_to_integer (&TREE_REAL_CST (converted), &fail,
8116 TYPE_PRECISION (TREE_TYPE (init)));
8117 return !fail && wi::eq_p (w, wi::to_wide (init));
8119 if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init)))
8120 return false;
8121 if ((REAL_VALUE_ISINF (TREE_REAL_CST (init))
8122 && MODE_HAS_INFINITIES (TYPE_MODE (type)))
8123 || (REAL_VALUE_ISNAN (TREE_REAL_CST (init))
8124 && MODE_HAS_NANS (TYPE_MODE (type))))
8125 return true;
8126 if (DECIMAL_FLOAT_TYPE_P (type)
8127 && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init)))
8129 /* This is valid if the real number represented by the
8130 initializer can be exactly represented in the decimal
8131 type. Compare the values using MPFR. */
8132 REAL_VALUE_TYPE t;
8133 real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
8134 mpfr_t bin_val, dec_val;
8135 mpfr_init2 (bin_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
8136 mpfr_init2 (dec_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
8137 mpfr_from_real (bin_val, &TREE_REAL_CST (init), MPFR_RNDN);
8138 char string[256];
8139 real_to_decimal (string, &t, sizeof string, 0, 1);
8140 bool res = (mpfr_strtofr (dec_val, string, NULL, 10, MPFR_RNDN) == 0
8141 && mpfr_equal_p (bin_val, dec_val));
8142 mpfr_clear (bin_val);
8143 mpfr_clear (dec_val);
8144 return res;
8146 /* exact_real_truncate is not quite right here, since it doesn't
8147 allow even an exact conversion to subnormal values. */
8148 REAL_VALUE_TYPE t;
8149 real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
8150 return real_identical (&t, &TREE_REAL_CST (init));
8153 /* Check whether INIT (location LOC) is valid as a 'constexpr'
8154 initializer for type TYPE, and give an error if not. INIT has
8155 already been folded and verified to be constant. INT_CONST_EXPR
8156 and ARITH_CONST_EXPR say whether it is an integer constant
8157 expression or arithmetic constant expression, respectively. If
8158 TYPE is not a scalar type, this function does nothing. */
8160 static void
8161 check_constexpr_init (location_t loc, tree type, tree init,
8162 bool int_const_expr, bool arith_const_expr)
8164 if (POINTER_TYPE_P (type))
8166 /* The initializer must be null. */
8167 if (TREE_CODE (init) != INTEGER_CST || !integer_zerop (init))
8168 error_at (loc, "%<constexpr%> pointer initializer is not null");
8169 return;
8171 if (INTEGRAL_TYPE_P (type))
8173 /* The initializer must be an integer constant expression,
8174 representable in the target type. */
8175 if (!int_const_expr)
8176 error_at (loc, "%<constexpr%> integer initializer is not an "
8177 "integer constant expression");
8178 if (!int_fits_type_p (init, type))
8179 error_at (loc, "%<constexpr%> initializer not representable in "
8180 "type of object");
8181 return;
8183 /* We don't apply any extra checks to extension types such as vector
8184 or fixed-point types. */
8185 if (TREE_CODE (type) != REAL_TYPE && TREE_CODE (type) != COMPLEX_TYPE)
8186 return;
8187 if (!arith_const_expr)
8189 error_at (loc, "%<constexpr%> initializer is not an arithmetic "
8190 "constant expression");
8191 return;
8193 /* We don't apply any extra checks to complex integers. */
8194 if (TREE_CODE (type) == COMPLEX_TYPE
8195 && TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8196 return;
8197 /* Following N3082, a real type cannot be initialized from a complex
8198 type and a binary type cannot be initialized from a decimal type
8199 (but initializing a decimal type from a binary type is OK).
8200 Signaling NaN initializers are OK only if the types are
8201 compatible (not just the same mode); all quiet NaN and infinity
8202 initializations are considered to preserve the value. */
8203 if (TREE_CODE (TREE_TYPE (init)) == COMPLEX_TYPE
8204 && SCALAR_FLOAT_TYPE_P (type))
8206 error_at (loc, "%<constexpr%> initializer for a real type is of "
8207 "complex type");
8208 return;
8210 if (SCALAR_FLOAT_TYPE_P (type)
8211 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (init))
8212 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init))
8213 && !DECIMAL_FLOAT_TYPE_P (type))
8215 error_at (loc, "%<constexpr%> initializer for a binary "
8216 "floating-point type is of decimal type");
8217 return;
8219 bool fits;
8220 if (TREE_CODE (type) == COMPLEX_TYPE)
8222 switch (TREE_CODE (init))
8224 case INTEGER_CST:
8225 case REAL_CST:
8226 fits = constexpr_init_fits_real_type (TREE_TYPE (type), init);
8227 break;
8228 case COMPLEX_CST:
8229 fits = (constexpr_init_fits_real_type (TREE_TYPE (type),
8230 TREE_REALPART (init))
8231 && constexpr_init_fits_real_type (TREE_TYPE (type),
8232 TREE_IMAGPART (init)));
8233 break;
8234 default:
8235 gcc_unreachable ();
8238 else
8239 fits = constexpr_init_fits_real_type (type, init);
8240 if (!fits)
8241 error_at (loc, "%<constexpr%> initializer not representable in "
8242 "type of object");
8245 /* Digest the parser output INIT as an initializer for type TYPE.
8246 Return a C expression of type TYPE to represent the initial value.
8248 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
8250 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant,
8251 INT_CONST_EXPR is true if INIT is an integer constant expression,
8252 and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic
8253 constant expression, false if it has already been determined in the
8254 caller that it is not (but folding may have made the value passed here
8255 indistinguishable from an arithmetic constant expression).
8257 If INIT is a string constant, STRICT_STRING is true if it is
8258 unparenthesized or we should not warn here for it being parenthesized.
8259 For other types of INIT, STRICT_STRING is not used.
8261 INIT_LOC is the location of the INIT.
8263 REQUIRE_CONSTANT requests an error if non-constant initializers or
8264 elements are seen. REQUIRE_CONSTEXPR means the stricter requirements
8265 on initializers for 'constexpr' objects apply. */
8267 static tree
8268 digest_init (location_t init_loc, tree type, tree init, tree origtype,
8269 bool null_pointer_constant, bool int_const_expr,
8270 bool arith_const_expr, bool strict_string,
8271 bool require_constant, bool require_constexpr)
8273 enum tree_code code = TREE_CODE (type);
8274 tree inside_init = init;
8275 tree semantic_type = NULL_TREE;
8276 bool maybe_const = true;
8278 if (type == error_mark_node
8279 || !init
8280 || error_operand_p (init))
8281 return error_mark_node;
8283 STRIP_TYPE_NOPS (inside_init);
8285 if (!c_in_omp_for)
8287 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
8289 semantic_type = TREE_TYPE (inside_init);
8290 inside_init = TREE_OPERAND (inside_init, 0);
8292 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
8294 /* TODO: this may not detect all cases of expressions folding to
8295 constants that are not arithmetic constant expressions. */
8296 if (!maybe_const)
8297 arith_const_expr = false;
8298 else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init))
8299 && TREE_CODE (TREE_TYPE (inside_init)) != REAL_TYPE
8300 && TREE_CODE (TREE_TYPE (inside_init)) != COMPLEX_TYPE)
8301 arith_const_expr = false;
8302 else if (TREE_CODE (inside_init) != INTEGER_CST
8303 && TREE_CODE (inside_init) != REAL_CST
8304 && TREE_CODE (inside_init) != COMPLEX_CST)
8305 arith_const_expr = false;
8306 else if (TREE_OVERFLOW (inside_init))
8307 arith_const_expr = false;
8309 /* Initialization of an array of chars from a string constant
8310 optionally enclosed in braces. */
8312 if (code == ARRAY_TYPE && inside_init
8313 && TREE_CODE (inside_init) == STRING_CST)
8315 tree typ1
8316 = (TYPE_ATOMIC (TREE_TYPE (type))
8317 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
8318 TYPE_QUAL_ATOMIC)
8319 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
8320 /* Note that an array could be both an array of character type
8321 and an array of wchar_t if wchar_t is signed char or unsigned
8322 char. */
8323 bool char_array = (typ1 == char_type_node
8324 || typ1 == signed_char_type_node
8325 || typ1 == unsigned_char_type_node);
8326 bool wchar_array = !!comptypes (typ1, wchar_type_node);
8327 bool char16_array = !!comptypes (typ1, char16_type_node);
8328 bool char32_array = !!comptypes (typ1, char32_type_node);
8330 if (char_array || wchar_array || char16_array || char32_array)
8332 struct c_expr expr;
8333 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
8334 bool incompat_string_cst = false;
8335 expr.value = inside_init;
8336 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
8337 expr.original_type = NULL;
8338 expr.m_decimal = 0;
8339 maybe_warn_string_init (init_loc, type, expr);
8341 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8342 pedwarn_init (init_loc, OPT_Wpedantic,
8343 "initialization of a flexible array member");
8345 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8346 TYPE_MAIN_VARIANT (type)))
8347 return inside_init;
8349 if (char_array)
8351 if (typ2 != char_type_node && typ2 != char8_type_node)
8352 incompat_string_cst = true;
8354 else if (!comptypes (typ1, typ2))
8355 incompat_string_cst = true;
8357 if (incompat_string_cst)
8359 error_init (init_loc, "cannot initialize array of %qT from "
8360 "a string literal with type array of %qT",
8361 typ1, typ2);
8362 return error_mark_node;
8365 if (require_constexpr
8366 && TYPE_UNSIGNED (typ1) != TYPE_UNSIGNED (typ2))
8368 /* Check if all characters of the string can be
8369 represented in the type of the constexpr object being
8370 initialized. */
8371 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8372 const unsigned char *p =
8373 (const unsigned char *) TREE_STRING_POINTER (inside_init);
8374 gcc_assert (CHAR_TYPE_SIZE == 8 && CHAR_BIT == 8);
8375 for (unsigned i = 0; i < len; i++)
8376 if (p[i] > 127)
8378 error_init (init_loc, "%<constexpr%> initializer not "
8379 "representable in type of object");
8380 break;
8384 if (TYPE_DOMAIN (type) != NULL_TREE
8385 && TYPE_SIZE (type) != NULL_TREE
8386 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8388 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8389 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
8391 /* Subtract the size of a single (possibly wide) character
8392 because it's ok to ignore the terminating null char
8393 that is counted in the length of the constant. */
8394 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
8395 pedwarn_init (init_loc, 0,
8396 ("initializer-string for array of %qT "
8397 "is too long"), typ1);
8398 else if (warn_cxx_compat
8399 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8400 warning_at (init_loc, OPT_Wc___compat,
8401 ("initializer-string for array of %qT "
8402 "is too long for C++"), typ1);
8403 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8405 unsigned HOST_WIDE_INT size
8406 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8407 const char *p = TREE_STRING_POINTER (inside_init);
8409 inside_init = build_string (size, p);
8413 TREE_TYPE (inside_init) = type;
8414 return inside_init;
8416 else if (INTEGRAL_TYPE_P (typ1))
8418 error_init (init_loc, "array of inappropriate type initialized "
8419 "from string constant");
8420 return error_mark_node;
8424 /* Build a VECTOR_CST from a *constant* vector constructor. If the
8425 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
8426 below and handle as a constructor. */
8427 if (code == VECTOR_TYPE
8428 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
8429 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
8430 && TREE_CONSTANT (inside_init))
8432 if (TREE_CODE (inside_init) == VECTOR_CST
8433 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8434 TYPE_MAIN_VARIANT (type)))
8435 return inside_init;
8437 if (TREE_CODE (inside_init) == CONSTRUCTOR)
8439 unsigned HOST_WIDE_INT ix;
8440 tree value;
8441 bool constant_p = true;
8443 /* Iterate through elements and check if all constructor
8444 elements are *_CSTs. */
8445 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
8446 if (!CONSTANT_CLASS_P (value))
8448 constant_p = false;
8449 break;
8452 if (constant_p)
8453 return build_vector_from_ctor (type,
8454 CONSTRUCTOR_ELTS (inside_init));
8458 if (warn_sequence_point)
8459 verify_sequence_points (inside_init);
8461 /* Any type can be initialized
8462 from an expression of the same type, optionally with braces. */
8464 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
8465 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8466 TYPE_MAIN_VARIANT (type))
8467 || (code == ARRAY_TYPE
8468 && comptypes (TREE_TYPE (inside_init), type))
8469 || (gnu_vector_type_p (type)
8470 && comptypes (TREE_TYPE (inside_init), type))
8471 || (code == POINTER_TYPE
8472 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
8473 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
8474 TREE_TYPE (type)))))
8476 if (code == POINTER_TYPE)
8478 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
8480 if (TREE_CODE (inside_init) == STRING_CST
8481 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8482 inside_init = array_to_pointer_conversion
8483 (init_loc, inside_init);
8484 else
8486 error_init (init_loc, "invalid use of non-lvalue array");
8487 return error_mark_node;
8492 if (code == VECTOR_TYPE)
8493 /* Although the types are compatible, we may require a
8494 conversion. */
8495 inside_init = convert (type, inside_init);
8497 if (require_constant
8498 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8500 /* As an extension, allow initializing objects with static storage
8501 duration with compound literals (which are then treated just as
8502 the brace enclosed list they contain). Also allow this for
8503 vectors, as we can only assign them with compound literals. */
8504 if (flag_isoc99 && code != VECTOR_TYPE)
8505 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
8506 "is not constant");
8507 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8508 inside_init = DECL_INITIAL (decl);
8511 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
8512 && TREE_CODE (inside_init) != CONSTRUCTOR)
8514 error_init (init_loc, "array initialized from non-constant array "
8515 "expression");
8516 return error_mark_node;
8519 /* Compound expressions can only occur here if -Wpedantic or
8520 -pedantic-errors is specified. In the later case, we always want
8521 an error. In the former case, we simply want a warning. */
8522 if (require_constant && pedantic
8523 && TREE_CODE (inside_init) == COMPOUND_EXPR)
8525 inside_init
8526 = valid_compound_expr_initializer (inside_init,
8527 TREE_TYPE (inside_init));
8528 if (inside_init == error_mark_node)
8529 error_init (init_loc, "initializer element is not constant");
8530 else
8531 pedwarn_init (init_loc, OPT_Wpedantic,
8532 "initializer element is not constant");
8533 if (flag_pedantic_errors)
8534 inside_init = error_mark_node;
8536 else if (require_constant
8537 && !initializer_constant_valid_p (inside_init,
8538 TREE_TYPE (inside_init)))
8540 error_init (init_loc, "initializer element is not constant");
8541 inside_init = error_mark_node;
8543 else if (require_constant && !maybe_const)
8544 pedwarn_init (init_loc, OPT_Wpedantic,
8545 "initializer element is not a constant expression");
8546 else if (require_constexpr)
8547 check_constexpr_init (init_loc, type, inside_init,
8548 int_const_expr, arith_const_expr);
8550 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8551 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8552 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8553 type, inside_init, origtype,
8554 (require_constant
8555 ? ic_init_const
8556 : ic_init), null_pointer_constant,
8557 NULL_TREE, NULL_TREE, 0);
8558 return inside_init;
8561 /* Handle scalar types, including conversions. */
8563 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8564 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8565 || code == COMPLEX_TYPE || code == VECTOR_TYPE || code == NULLPTR_TYPE)
8567 tree unconverted_init = inside_init;
8568 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8569 && (TREE_CODE (init) == STRING_CST
8570 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8571 inside_init = init = array_to_pointer_conversion (init_loc, init);
8572 if (semantic_type)
8573 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8574 inside_init);
8575 inside_init
8576 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8577 inside_init, origtype,
8578 require_constant ? ic_init_const : ic_init,
8579 null_pointer_constant, NULL_TREE, NULL_TREE,
8582 /* Check to see if we have already given an error message. */
8583 if (inside_init == error_mark_node)
8585 else if (require_constant && !TREE_CONSTANT (inside_init))
8587 error_init (init_loc, "initializer element is not constant");
8588 inside_init = error_mark_node;
8590 else if (require_constant
8591 && !initializer_constant_valid_p (inside_init,
8592 TREE_TYPE (inside_init)))
8594 error_init (init_loc, "initializer element is not computable at "
8595 "load time");
8596 inside_init = error_mark_node;
8598 else if (require_constant && !maybe_const)
8599 pedwarn_init (init_loc, OPT_Wpedantic,
8600 "initializer element is not a constant expression");
8601 else if (require_constexpr)
8602 check_constexpr_init (init_loc, type, unconverted_init,
8603 int_const_expr, arith_const_expr);
8605 return inside_init;
8608 /* Come here only for records and arrays. */
8610 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8612 error_init (init_loc,
8613 "variable-sized object may not be initialized except "
8614 "with an empty initializer");
8615 return error_mark_node;
8618 error_init (init_loc, "invalid initializer");
8619 return error_mark_node;
8622 /* Handle initializers that use braces. */
8624 /* Type of object we are accumulating a constructor for.
8625 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8626 static tree constructor_type;
8628 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8629 left to fill. */
8630 static tree constructor_fields;
8632 /* For an ARRAY_TYPE, this is the specified index
8633 at which to store the next element we get. */
8634 static tree constructor_index;
8636 /* For an ARRAY_TYPE, this is the maximum index. */
8637 static tree constructor_max_index;
8639 /* For a RECORD_TYPE, this is the first field not yet written out. */
8640 static tree constructor_unfilled_fields;
8642 /* For an ARRAY_TYPE, this is the index of the first element
8643 not yet written out. */
8644 static tree constructor_unfilled_index;
8646 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8647 This is so we can generate gaps between fields, when appropriate. */
8648 static tree constructor_bit_index;
8650 /* If we are saving up the elements rather than allocating them,
8651 this is the list of elements so far (in reverse order,
8652 most recent first). */
8653 static vec<constructor_elt, va_gc> *constructor_elements;
8655 /* 1 if constructor should be incrementally stored into a constructor chain,
8656 0 if all the elements should be kept in AVL tree. */
8657 static int constructor_incremental;
8659 /* 1 if so far this constructor's elements are all compile-time constants. */
8660 static int constructor_constant;
8662 /* 1 if so far this constructor's elements are all valid address constants. */
8663 static int constructor_simple;
8665 /* 1 if this constructor has an element that cannot be part of a
8666 constant expression. */
8667 static int constructor_nonconst;
8669 /* 1 if this constructor is erroneous so far. */
8670 static int constructor_erroneous;
8672 /* 1 if this constructor is the universal zero initializer { 0 }. */
8673 static int constructor_zeroinit;
8675 /* Structure for managing pending initializer elements, organized as an
8676 AVL tree. */
8678 struct init_node
8680 struct init_node *left, *right;
8681 struct init_node *parent;
8682 int balance;
8683 tree purpose;
8684 tree value;
8685 tree origtype;
8688 /* Tree of pending elements at this constructor level.
8689 These are elements encountered out of order
8690 which belong at places we haven't reached yet in actually
8691 writing the output.
8692 Will never hold tree nodes across GC runs. */
8693 static struct init_node *constructor_pending_elts;
8695 /* The SPELLING_DEPTH of this constructor. */
8696 static int constructor_depth;
8698 /* DECL node for which an initializer is being read.
8699 0 means we are reading a constructor expression
8700 such as (struct foo) {...}. */
8701 static tree constructor_decl;
8703 /* Nonzero if there were any member designators in this initializer. */
8704 static int constructor_designated;
8706 /* Nesting depth of designator list. */
8707 static int designator_depth;
8709 /* Nonzero if there were diagnosed errors in this designator list. */
8710 static int designator_erroneous;
8713 /* This stack has a level for each implicit or explicit level of
8714 structuring in the initializer, including the outermost one. It
8715 saves the values of most of the variables above. */
8717 struct constructor_range_stack;
8719 struct constructor_stack
8721 struct constructor_stack *next;
8722 tree type;
8723 tree fields;
8724 tree index;
8725 tree max_index;
8726 tree unfilled_index;
8727 tree unfilled_fields;
8728 tree bit_index;
8729 vec<constructor_elt, va_gc> *elements;
8730 struct init_node *pending_elts;
8731 int offset;
8732 int depth;
8733 /* If value nonzero, this value should replace the entire
8734 constructor at this level. */
8735 struct c_expr replacement_value;
8736 struct constructor_range_stack *range_stack;
8737 char constant;
8738 char simple;
8739 char nonconst;
8740 char implicit;
8741 char erroneous;
8742 char outer;
8743 char incremental;
8744 char designated;
8745 int designator_depth;
8748 static struct constructor_stack *constructor_stack;
8750 /* This stack represents designators from some range designator up to
8751 the last designator in the list. */
8753 struct constructor_range_stack
8755 struct constructor_range_stack *next, *prev;
8756 struct constructor_stack *stack;
8757 tree range_start;
8758 tree index;
8759 tree range_end;
8760 tree fields;
8763 static struct constructor_range_stack *constructor_range_stack;
8765 /* This stack records separate initializers that are nested.
8766 Nested initializers can't happen in ANSI C, but GNU C allows them
8767 in cases like { ... (struct foo) { ... } ... }. */
8769 struct initializer_stack
8771 struct initializer_stack *next;
8772 tree decl;
8773 struct constructor_stack *constructor_stack;
8774 struct constructor_range_stack *constructor_range_stack;
8775 vec<constructor_elt, va_gc> *elements;
8776 struct spelling *spelling;
8777 struct spelling *spelling_base;
8778 int spelling_size;
8779 char require_constant_value;
8780 char require_constant_elements;
8781 char require_constexpr_value;
8782 char designated;
8783 rich_location *missing_brace_richloc;
8786 static struct initializer_stack *initializer_stack;
8788 /* Prepare to parse and output the initializer for variable DECL. */
8790 void
8791 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED,
8792 bool init_require_constant, bool init_require_constexpr,
8793 rich_location *richloc)
8795 const char *locus;
8796 struct initializer_stack *p = XNEW (struct initializer_stack);
8798 p->decl = constructor_decl;
8799 p->require_constant_value = require_constant_value;
8800 p->require_constant_elements = require_constant_elements;
8801 p->require_constexpr_value = require_constexpr_value;
8802 p->constructor_stack = constructor_stack;
8803 p->constructor_range_stack = constructor_range_stack;
8804 p->elements = constructor_elements;
8805 p->spelling = spelling;
8806 p->spelling_base = spelling_base;
8807 p->spelling_size = spelling_size;
8808 p->next = initializer_stack;
8809 p->missing_brace_richloc = richloc;
8810 p->designated = constructor_designated;
8811 initializer_stack = p;
8813 constructor_decl = decl;
8814 constructor_designated = 0;
8816 require_constant_value = init_require_constant;
8817 require_constexpr_value = init_require_constexpr;
8818 if (decl != NULL_TREE && decl != error_mark_node)
8820 require_constant_elements
8821 = ((init_require_constant || (pedantic && !flag_isoc99))
8822 /* For a scalar, you can always use any value to initialize,
8823 even within braces. */
8824 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8825 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8827 else
8829 require_constant_elements = false;
8830 locus = _("(anonymous)");
8833 constructor_stack = 0;
8834 constructor_range_stack = 0;
8836 found_missing_braces = 0;
8838 spelling_base = 0;
8839 spelling_size = 0;
8840 RESTORE_SPELLING_DEPTH (0);
8842 if (locus)
8843 push_string (locus);
8846 void
8847 finish_init (void)
8849 struct initializer_stack *p = initializer_stack;
8851 /* Free the whole constructor stack of this initializer. */
8852 while (constructor_stack)
8854 struct constructor_stack *q = constructor_stack;
8855 constructor_stack = q->next;
8856 XDELETE (q);
8859 gcc_assert (!constructor_range_stack);
8861 /* Pop back to the data of the outer initializer (if any). */
8862 XDELETE (spelling_base);
8864 constructor_decl = p->decl;
8865 require_constant_value = p->require_constant_value;
8866 require_constant_elements = p->require_constant_elements;
8867 require_constexpr_value = p->require_constexpr_value;
8868 constructor_stack = p->constructor_stack;
8869 constructor_designated = p->designated;
8870 constructor_range_stack = p->constructor_range_stack;
8871 constructor_elements = p->elements;
8872 spelling = p->spelling;
8873 spelling_base = p->spelling_base;
8874 spelling_size = p->spelling_size;
8875 initializer_stack = p->next;
8876 XDELETE (p);
8879 /* Call here when we see the initializer is surrounded by braces.
8880 This is instead of a call to push_init_level;
8881 it is matched by a call to pop_init_level.
8883 TYPE is the type to initialize, for a constructor expression.
8884 For an initializer for a decl, TYPE is zero. */
8886 void
8887 really_start_incremental_init (tree type)
8889 struct constructor_stack *p = XNEW (struct constructor_stack);
8891 if (type == NULL_TREE)
8892 type = TREE_TYPE (constructor_decl);
8894 if (VECTOR_TYPE_P (type)
8895 && TYPE_VECTOR_OPAQUE (type))
8896 error ("opaque vector types cannot be initialized");
8898 p->type = constructor_type;
8899 p->fields = constructor_fields;
8900 p->index = constructor_index;
8901 p->max_index = constructor_max_index;
8902 p->unfilled_index = constructor_unfilled_index;
8903 p->unfilled_fields = constructor_unfilled_fields;
8904 p->bit_index = constructor_bit_index;
8905 p->elements = constructor_elements;
8906 p->constant = constructor_constant;
8907 p->simple = constructor_simple;
8908 p->nonconst = constructor_nonconst;
8909 p->erroneous = constructor_erroneous;
8910 p->pending_elts = constructor_pending_elts;
8911 p->depth = constructor_depth;
8912 p->replacement_value.value = 0;
8913 p->replacement_value.original_code = ERROR_MARK;
8914 p->replacement_value.original_type = NULL;
8915 p->implicit = 0;
8916 p->range_stack = 0;
8917 p->outer = 0;
8918 p->incremental = constructor_incremental;
8919 p->designated = constructor_designated;
8920 p->designator_depth = designator_depth;
8921 p->next = 0;
8922 constructor_stack = p;
8924 constructor_constant = 1;
8925 constructor_simple = 1;
8926 constructor_nonconst = 0;
8927 constructor_depth = SPELLING_DEPTH ();
8928 constructor_elements = NULL;
8929 constructor_pending_elts = 0;
8930 constructor_type = type;
8931 constructor_incremental = 1;
8932 constructor_designated = 0;
8933 constructor_zeroinit = 1;
8934 designator_depth = 0;
8935 designator_erroneous = 0;
8937 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8939 constructor_fields = TYPE_FIELDS (constructor_type);
8940 /* Skip any nameless bit fields at the beginning. */
8941 while (constructor_fields != NULL_TREE
8942 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8943 constructor_fields = DECL_CHAIN (constructor_fields);
8945 constructor_unfilled_fields = constructor_fields;
8946 constructor_bit_index = bitsize_zero_node;
8948 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8950 if (TYPE_DOMAIN (constructor_type))
8952 constructor_max_index
8953 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8955 /* Detect non-empty initializations of zero-length arrays. */
8956 if (constructor_max_index == NULL_TREE
8957 && TYPE_SIZE (constructor_type))
8958 constructor_max_index = integer_minus_one_node;
8960 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8961 to initialize VLAs with a nonempty initializer will cause a
8962 proper error; avoid tree checking errors as well by setting a
8963 safe value. */
8964 if (constructor_max_index
8965 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8966 constructor_max_index = integer_minus_one_node;
8968 constructor_index
8969 = convert (bitsizetype,
8970 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8972 else
8974 constructor_index = bitsize_zero_node;
8975 constructor_max_index = NULL_TREE;
8978 constructor_unfilled_index = constructor_index;
8980 else if (gnu_vector_type_p (constructor_type))
8982 /* Vectors are like simple fixed-size arrays. */
8983 constructor_max_index =
8984 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8985 constructor_index = bitsize_zero_node;
8986 constructor_unfilled_index = constructor_index;
8988 else
8990 /* Handle the case of int x = {5}; */
8991 constructor_fields = constructor_type;
8992 constructor_unfilled_fields = constructor_type;
8996 extern location_t last_init_list_comma;
8998 /* Called when we see an open brace for a nested initializer. Finish
8999 off any pending levels with implicit braces. */
9000 void
9001 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
9003 while (constructor_stack->implicit)
9005 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9006 && constructor_fields == NULL_TREE)
9007 process_init_element (input_location,
9008 pop_init_level (loc, 1, braced_init_obstack,
9009 last_init_list_comma),
9010 true, braced_init_obstack);
9011 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
9012 && constructor_max_index
9013 && tree_int_cst_lt (constructor_max_index,
9014 constructor_index))
9015 process_init_element (input_location,
9016 pop_init_level (loc, 1, braced_init_obstack,
9017 last_init_list_comma),
9018 true, braced_init_obstack);
9019 else
9020 break;
9024 /* Push down into a subobject, for initialization.
9025 If this is for an explicit set of braces, IMPLICIT is 0.
9026 If it is because the next element belongs at a lower level,
9027 IMPLICIT is 1 (or 2 if the push is because of designator list). */
9029 void
9030 push_init_level (location_t loc, int implicit,
9031 struct obstack *braced_init_obstack)
9033 struct constructor_stack *p;
9034 tree value = NULL_TREE;
9036 /* Unless this is an explicit brace, we need to preserve previous
9037 content if any. */
9038 if (implicit)
9040 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
9041 value = find_init_member (constructor_fields, braced_init_obstack);
9042 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9043 value = find_init_member (constructor_index, braced_init_obstack);
9046 p = XNEW (struct constructor_stack);
9047 p->type = constructor_type;
9048 p->fields = constructor_fields;
9049 p->index = constructor_index;
9050 p->max_index = constructor_max_index;
9051 p->unfilled_index = constructor_unfilled_index;
9052 p->unfilled_fields = constructor_unfilled_fields;
9053 p->bit_index = constructor_bit_index;
9054 p->elements = constructor_elements;
9055 p->constant = constructor_constant;
9056 p->simple = constructor_simple;
9057 p->nonconst = constructor_nonconst;
9058 p->erroneous = constructor_erroneous;
9059 p->pending_elts = constructor_pending_elts;
9060 p->depth = constructor_depth;
9061 p->replacement_value.value = NULL_TREE;
9062 p->replacement_value.original_code = ERROR_MARK;
9063 p->replacement_value.original_type = NULL;
9064 p->implicit = implicit;
9065 p->outer = 0;
9066 p->incremental = constructor_incremental;
9067 p->designated = constructor_designated;
9068 p->designator_depth = designator_depth;
9069 p->next = constructor_stack;
9070 p->range_stack = 0;
9071 constructor_stack = p;
9073 constructor_constant = 1;
9074 constructor_simple = 1;
9075 constructor_nonconst = 0;
9076 constructor_depth = SPELLING_DEPTH ();
9077 constructor_elements = NULL;
9078 constructor_incremental = 1;
9079 /* If the upper initializer is designated, then mark this as
9080 designated too to prevent bogus warnings. */
9081 constructor_designated = p->designated;
9082 constructor_pending_elts = 0;
9083 if (!implicit)
9085 p->range_stack = constructor_range_stack;
9086 constructor_range_stack = 0;
9087 designator_depth = 0;
9088 designator_erroneous = 0;
9091 /* Don't die if an entire brace-pair level is superfluous
9092 in the containing level. */
9093 if (constructor_type == NULL_TREE)
9095 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9097 /* Don't die if there are extra init elts at the end. */
9098 if (constructor_fields == NULL_TREE)
9099 constructor_type = NULL_TREE;
9100 else
9102 constructor_type = TREE_TYPE (constructor_fields);
9103 push_member_name (constructor_fields);
9104 constructor_depth++;
9107 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9109 constructor_type = TREE_TYPE (constructor_type);
9110 push_array_bounds (tree_to_uhwi (constructor_index));
9111 constructor_depth++;
9114 if (constructor_type == NULL_TREE)
9116 error_init (loc, "extra brace group at end of initializer");
9117 constructor_fields = NULL_TREE;
9118 constructor_unfilled_fields = NULL_TREE;
9119 return;
9122 if (value && TREE_CODE (value) == CONSTRUCTOR)
9124 constructor_constant = TREE_CONSTANT (value);
9125 constructor_simple = TREE_STATIC (value);
9126 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
9127 constructor_elements = CONSTRUCTOR_ELTS (value);
9128 if (!vec_safe_is_empty (constructor_elements)
9129 && (TREE_CODE (constructor_type) == RECORD_TYPE
9130 || TREE_CODE (constructor_type) == ARRAY_TYPE))
9131 set_nonincremental_init (braced_init_obstack);
9134 if (implicit == 1)
9136 found_missing_braces = 1;
9137 if (initializer_stack->missing_brace_richloc)
9138 initializer_stack->missing_brace_richloc->add_fixit_insert_before
9139 (loc, "{");
9142 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9144 constructor_fields = TYPE_FIELDS (constructor_type);
9145 /* Skip any nameless bit fields at the beginning. */
9146 while (constructor_fields != NULL_TREE
9147 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
9148 constructor_fields = DECL_CHAIN (constructor_fields);
9150 constructor_unfilled_fields = constructor_fields;
9151 constructor_bit_index = bitsize_zero_node;
9153 else if (gnu_vector_type_p (constructor_type))
9155 /* Vectors are like simple fixed-size arrays. */
9156 constructor_max_index =
9157 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
9158 constructor_index = bitsize_int (0);
9159 constructor_unfilled_index = constructor_index;
9161 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9163 if (TYPE_DOMAIN (constructor_type))
9165 constructor_max_index
9166 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
9168 /* Detect non-empty initializations of zero-length arrays. */
9169 if (constructor_max_index == NULL_TREE
9170 && TYPE_SIZE (constructor_type))
9171 constructor_max_index = integer_minus_one_node;
9173 /* constructor_max_index needs to be an INTEGER_CST. Attempts
9174 to initialize VLAs will cause a proper error; avoid tree
9175 checking errors as well by setting a safe value. */
9176 if (constructor_max_index
9177 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9178 constructor_max_index = integer_minus_one_node;
9180 constructor_index
9181 = convert (bitsizetype,
9182 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9184 else
9185 constructor_index = bitsize_zero_node;
9187 constructor_unfilled_index = constructor_index;
9188 if (value && TREE_CODE (value) == STRING_CST)
9190 /* We need to split the char/wchar array into individual
9191 characters, so that we don't have to special case it
9192 everywhere. */
9193 set_nonincremental_init_from_string (value, braced_init_obstack);
9196 else
9198 if (constructor_type != error_mark_node)
9199 warning_init (input_location, 0, "braces around scalar initializer");
9200 constructor_fields = constructor_type;
9201 constructor_unfilled_fields = constructor_type;
9205 /* At the end of an implicit or explicit brace level,
9206 finish up that level of constructor. If a single expression
9207 with redundant braces initialized that level, return the
9208 c_expr structure for that expression. Otherwise, the original_code
9209 element is set to ERROR_MARK.
9210 If we were outputting the elements as they are read, return 0 as the value
9211 from inner levels (process_init_element ignores that),
9212 but return error_mark_node as the value from the outermost level
9213 (that's what we want to put in DECL_INITIAL).
9214 Otherwise, return a CONSTRUCTOR expression as the value. */
9216 struct c_expr
9217 pop_init_level (location_t loc, int implicit,
9218 struct obstack *braced_init_obstack,
9219 location_t insert_before)
9221 struct constructor_stack *p;
9222 struct c_expr ret;
9223 ret.value = NULL_TREE;
9224 ret.original_code = ERROR_MARK;
9225 ret.original_type = NULL;
9226 ret.m_decimal = 0;
9228 if (implicit == 0)
9230 /* When we come to an explicit close brace,
9231 pop any inner levels that didn't have explicit braces. */
9232 while (constructor_stack->implicit)
9233 process_init_element (input_location,
9234 pop_init_level (loc, 1, braced_init_obstack,
9235 insert_before),
9236 true, braced_init_obstack);
9237 gcc_assert (!constructor_range_stack);
9239 else
9240 if (initializer_stack->missing_brace_richloc)
9241 initializer_stack->missing_brace_richloc->add_fixit_insert_before
9242 (insert_before, "}");
9244 /* Now output all pending elements. */
9245 constructor_incremental = 1;
9246 output_pending_init_elements (1, braced_init_obstack);
9248 p = constructor_stack;
9250 /* Error for initializing a flexible array member, or a zero-length
9251 array member in an inappropriate context. */
9252 if (constructor_type && constructor_fields
9253 && TREE_CODE (constructor_type) == ARRAY_TYPE
9254 && TYPE_DOMAIN (constructor_type)
9255 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
9257 /* Silently discard empty initializations. The parser will
9258 already have pedwarned for empty brackets. */
9259 if (integer_zerop (constructor_unfilled_index))
9260 constructor_type = NULL_TREE;
9261 else
9263 gcc_assert (!TYPE_SIZE (constructor_type));
9265 if (constructor_depth > 2)
9266 error_init (loc, "initialization of flexible array member in a nested context");
9267 else
9268 pedwarn_init (loc, OPT_Wpedantic,
9269 "initialization of a flexible array member");
9271 /* We have already issued an error message for the existence
9272 of a flexible array member not at the end of the structure.
9273 Discard the initializer so that we do not die later. */
9274 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
9275 constructor_type = NULL_TREE;
9279 switch (vec_safe_length (constructor_elements))
9281 case 0:
9282 /* Initialization with { } counts as zeroinit. */
9283 constructor_zeroinit = 1;
9284 break;
9285 case 1:
9286 /* This might be zeroinit as well. */
9287 if (integer_zerop ((*constructor_elements)[0].value))
9288 constructor_zeroinit = 1;
9289 break;
9290 default:
9291 /* If the constructor has more than one element, it can't be { 0 }. */
9292 constructor_zeroinit = 0;
9293 break;
9296 /* Warn when some structs are initialized with direct aggregation. */
9297 if (!implicit && found_missing_braces && warn_missing_braces
9298 && !constructor_zeroinit)
9300 gcc_assert (initializer_stack->missing_brace_richloc);
9301 warning_at (initializer_stack->missing_brace_richloc,
9302 OPT_Wmissing_braces,
9303 "missing braces around initializer");
9306 /* Warn when some struct elements are implicitly initialized to zero. */
9307 if (warn_missing_field_initializers
9308 && constructor_type
9309 && TREE_CODE (constructor_type) == RECORD_TYPE
9310 && constructor_unfilled_fields)
9312 /* Do not warn for flexible array members or zero-length arrays. */
9313 while (constructor_unfilled_fields
9314 && (!DECL_SIZE (constructor_unfilled_fields)
9315 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
9316 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
9318 if (constructor_unfilled_fields
9319 /* Do not warn if this level of the initializer uses member
9320 designators; it is likely to be deliberate. */
9321 && !constructor_designated
9322 /* Do not warn about initializing with { 0 } or with { }. */
9323 && !constructor_zeroinit)
9325 if (warning_at (input_location, OPT_Wmissing_field_initializers,
9326 "missing initializer for field %qD of %qT",
9327 constructor_unfilled_fields,
9328 constructor_type))
9329 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
9330 "%qD declared here", constructor_unfilled_fields);
9334 /* Pad out the end of the structure. */
9335 if (p->replacement_value.value)
9336 /* If this closes a superfluous brace pair,
9337 just pass out the element between them. */
9338 ret = p->replacement_value;
9339 else if (constructor_type == NULL_TREE)
9341 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
9342 && TREE_CODE (constructor_type) != ARRAY_TYPE
9343 && !gnu_vector_type_p (constructor_type))
9345 /* A nonincremental scalar initializer--just return
9346 the element, after verifying there is just one.
9347 Empty scalar initializers are supported in C2X. */
9348 if (vec_safe_is_empty (constructor_elements))
9350 if (constructor_erroneous || constructor_type == error_mark_node)
9351 ret.value = error_mark_node;
9352 else if (TREE_CODE (constructor_type) == FUNCTION_TYPE)
9354 error_init (loc, "invalid initializer");
9355 ret.value = error_mark_node;
9357 else if (TREE_CODE (constructor_type) == POINTER_TYPE)
9358 /* Ensure this is a null pointer constant in the case of a
9359 'constexpr' object initialized with {}. */
9360 ret.value = build_zero_cst (ptr_type_node);
9361 else
9362 ret.value = build_zero_cst (constructor_type);
9364 else if (vec_safe_length (constructor_elements) != 1)
9366 error_init (loc, "extra elements in scalar initializer");
9367 ret.value = (*constructor_elements)[0].value;
9369 else
9370 ret.value = (*constructor_elements)[0].value;
9372 else
9374 if (constructor_erroneous)
9375 ret.value = error_mark_node;
9376 else
9378 ret.value = build_constructor (constructor_type,
9379 constructor_elements);
9380 if (constructor_constant)
9381 TREE_CONSTANT (ret.value) = 1;
9382 if (constructor_constant && constructor_simple)
9383 TREE_STATIC (ret.value) = 1;
9384 if (constructor_nonconst)
9385 CONSTRUCTOR_NON_CONST (ret.value) = 1;
9389 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
9391 if (constructor_nonconst)
9392 ret.original_code = C_MAYBE_CONST_EXPR;
9393 else if (ret.original_code == C_MAYBE_CONST_EXPR)
9394 ret.original_code = ERROR_MARK;
9397 constructor_type = p->type;
9398 constructor_fields = p->fields;
9399 constructor_index = p->index;
9400 constructor_max_index = p->max_index;
9401 constructor_unfilled_index = p->unfilled_index;
9402 constructor_unfilled_fields = p->unfilled_fields;
9403 constructor_bit_index = p->bit_index;
9404 constructor_elements = p->elements;
9405 constructor_constant = p->constant;
9406 constructor_simple = p->simple;
9407 constructor_nonconst = p->nonconst;
9408 constructor_erroneous = p->erroneous;
9409 constructor_incremental = p->incremental;
9410 constructor_designated = p->designated;
9411 designator_depth = p->designator_depth;
9412 constructor_pending_elts = p->pending_elts;
9413 constructor_depth = p->depth;
9414 if (!p->implicit)
9415 constructor_range_stack = p->range_stack;
9416 RESTORE_SPELLING_DEPTH (constructor_depth);
9418 constructor_stack = p->next;
9419 XDELETE (p);
9421 if (ret.value == NULL_TREE && constructor_stack == 0)
9422 ret.value = error_mark_node;
9423 return ret;
9426 /* Common handling for both array range and field name designators.
9427 ARRAY argument is nonzero for array ranges. Returns false for success. */
9429 static bool
9430 set_designator (location_t loc, bool array,
9431 struct obstack *braced_init_obstack)
9433 tree subtype;
9434 enum tree_code subcode;
9436 /* Don't die if an entire brace-pair level is superfluous
9437 in the containing level, or for an erroneous type. */
9438 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9439 return true;
9441 /* If there were errors in this designator list already, bail out
9442 silently. */
9443 if (designator_erroneous)
9444 return true;
9446 /* Likewise for an initializer for a variable-size type. Those are
9447 diagnosed in the parser, except for empty initializer braces. */
9448 if (COMPLETE_TYPE_P (constructor_type)
9449 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9450 return true;
9452 if (!designator_depth)
9454 gcc_assert (!constructor_range_stack);
9456 /* Designator list starts at the level of closest explicit
9457 braces. */
9458 while (constructor_stack->implicit)
9459 process_init_element (input_location,
9460 pop_init_level (loc, 1, braced_init_obstack,
9461 last_init_list_comma),
9462 true, braced_init_obstack);
9463 constructor_designated = 1;
9464 return false;
9467 switch (TREE_CODE (constructor_type))
9469 case RECORD_TYPE:
9470 case UNION_TYPE:
9471 subtype = TREE_TYPE (constructor_fields);
9472 if (subtype != error_mark_node)
9473 subtype = TYPE_MAIN_VARIANT (subtype);
9474 break;
9475 case ARRAY_TYPE:
9476 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9477 break;
9478 default:
9479 gcc_unreachable ();
9482 subcode = TREE_CODE (subtype);
9483 if (array && subcode != ARRAY_TYPE)
9485 error_init (loc, "array index in non-array initializer");
9486 return true;
9488 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9490 error_init (loc, "field name not in record or union initializer");
9491 return true;
9494 constructor_designated = 1;
9495 finish_implicit_inits (loc, braced_init_obstack);
9496 push_init_level (loc, 2, braced_init_obstack);
9497 return false;
9500 /* If there are range designators in designator list, push a new designator
9501 to constructor_range_stack. RANGE_END is end of such stack range or
9502 NULL_TREE if there is no range designator at this level. */
9504 static void
9505 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9507 struct constructor_range_stack *p;
9509 p = (struct constructor_range_stack *)
9510 obstack_alloc (braced_init_obstack,
9511 sizeof (struct constructor_range_stack));
9512 p->prev = constructor_range_stack;
9513 p->next = 0;
9514 p->fields = constructor_fields;
9515 p->range_start = constructor_index;
9516 p->index = constructor_index;
9517 p->stack = constructor_stack;
9518 p->range_end = range_end;
9519 if (constructor_range_stack)
9520 constructor_range_stack->next = p;
9521 constructor_range_stack = p;
9524 /* Within an array initializer, specify the next index to be initialized.
9525 FIRST is that index. If LAST is nonzero, then initialize a range
9526 of indices, running from FIRST through LAST. */
9528 void
9529 set_init_index (location_t loc, tree first, tree last,
9530 struct obstack *braced_init_obstack)
9532 if (set_designator (loc, true, braced_init_obstack))
9533 return;
9535 designator_erroneous = 1;
9537 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9538 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9540 error_init (loc, "array index in initializer not of integer type");
9541 return;
9544 if (TREE_CODE (first) != INTEGER_CST)
9546 first = c_fully_fold (first, false, NULL);
9547 if (TREE_CODE (first) == INTEGER_CST)
9548 pedwarn_init (loc, OPT_Wpedantic,
9549 "array index in initializer is not "
9550 "an integer constant expression");
9553 if (last && TREE_CODE (last) != INTEGER_CST)
9555 last = c_fully_fold (last, false, NULL);
9556 if (TREE_CODE (last) == INTEGER_CST)
9557 pedwarn_init (loc, OPT_Wpedantic,
9558 "array index in initializer is not "
9559 "an integer constant expression");
9562 if (TREE_CODE (first) != INTEGER_CST)
9563 error_init (loc, "nonconstant array index in initializer");
9564 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9565 error_init (loc, "nonconstant array index in initializer");
9566 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9567 error_init (loc, "array index in non-array initializer");
9568 else if (tree_int_cst_sgn (first) == -1)
9569 error_init (loc, "array index in initializer exceeds array bounds");
9570 else if (constructor_max_index
9571 && tree_int_cst_lt (constructor_max_index, first))
9572 error_init (loc, "array index in initializer exceeds array bounds");
9573 else
9575 constant_expression_warning (first);
9576 if (last)
9577 constant_expression_warning (last);
9578 constructor_index = convert (bitsizetype, first);
9579 if (tree_int_cst_lt (constructor_index, first))
9581 constructor_index = copy_node (constructor_index);
9582 TREE_OVERFLOW (constructor_index) = 1;
9585 if (last)
9587 if (tree_int_cst_equal (first, last))
9588 last = NULL_TREE;
9589 else if (tree_int_cst_lt (last, first))
9591 error_init (loc, "empty index range in initializer");
9592 last = NULL_TREE;
9594 else
9596 last = convert (bitsizetype, last);
9597 if (constructor_max_index != NULL_TREE
9598 && tree_int_cst_lt (constructor_max_index, last))
9600 error_init (loc, "array index range in initializer exceeds "
9601 "array bounds");
9602 last = NULL_TREE;
9607 designator_depth++;
9608 designator_erroneous = 0;
9609 if (constructor_range_stack || last)
9610 push_range_stack (last, braced_init_obstack);
9614 /* Within a struct initializer, specify the next field to be initialized. */
9616 void
9617 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9618 struct obstack *braced_init_obstack)
9620 tree field;
9622 if (set_designator (loc, false, braced_init_obstack))
9623 return;
9625 designator_erroneous = 1;
9627 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9629 error_init (loc, "field name not in record or union initializer");
9630 return;
9633 field = lookup_field (constructor_type, fieldname);
9635 if (field == NULL_TREE)
9637 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9638 if (guessed_id)
9640 gcc_rich_location rich_loc (fieldname_loc);
9641 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9642 error_at (&rich_loc,
9643 "%qT has no member named %qE; did you mean %qE?",
9644 constructor_type, fieldname, guessed_id);
9646 else
9647 error_at (fieldname_loc, "%qT has no member named %qE",
9648 constructor_type, fieldname);
9650 else
9653 constructor_fields = TREE_VALUE (field);
9654 designator_depth++;
9655 designator_erroneous = 0;
9656 if (constructor_range_stack)
9657 push_range_stack (NULL_TREE, braced_init_obstack);
9658 field = TREE_CHAIN (field);
9659 if (field)
9661 if (set_designator (loc, false, braced_init_obstack))
9662 return;
9665 while (field != NULL_TREE);
9668 /* Add a new initializer to the tree of pending initializers. PURPOSE
9669 identifies the initializer, either array index or field in a structure.
9670 VALUE is the value of that index or field. If ORIGTYPE is not
9671 NULL_TREE, it is the original type of VALUE.
9673 IMPLICIT is true if value comes from pop_init_level (1),
9674 the new initializer has been merged with the existing one
9675 and thus no warnings should be emitted about overriding an
9676 existing initializer. */
9678 static void
9679 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9680 bool implicit, struct obstack *braced_init_obstack)
9682 struct init_node *p, **q, *r;
9684 q = &constructor_pending_elts;
9685 p = 0;
9687 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9689 while (*q != 0)
9691 p = *q;
9692 if (tree_int_cst_lt (purpose, p->purpose))
9693 q = &p->left;
9694 else if (tree_int_cst_lt (p->purpose, purpose))
9695 q = &p->right;
9696 else
9698 if (!implicit)
9700 if (TREE_SIDE_EFFECTS (p->value))
9701 warning_init (loc, OPT_Woverride_init_side_effects,
9702 "initialized field with side-effects "
9703 "overwritten");
9704 else if (warn_override_init)
9705 warning_init (loc, OPT_Woverride_init,
9706 "initialized field overwritten");
9708 p->value = value;
9709 p->origtype = origtype;
9710 return;
9714 else
9716 tree bitpos;
9718 bitpos = bit_position (purpose);
9719 while (*q != NULL)
9721 p = *q;
9722 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9723 q = &p->left;
9724 else if (p->purpose != purpose)
9725 q = &p->right;
9726 else
9728 if (!implicit)
9730 if (TREE_SIDE_EFFECTS (p->value))
9731 warning_init (loc, OPT_Woverride_init_side_effects,
9732 "initialized field with side-effects "
9733 "overwritten");
9734 else if (warn_override_init)
9735 warning_init (loc, OPT_Woverride_init,
9736 "initialized field overwritten");
9738 p->value = value;
9739 p->origtype = origtype;
9740 return;
9745 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9746 sizeof (struct init_node));
9747 r->purpose = purpose;
9748 r->value = value;
9749 r->origtype = origtype;
9751 *q = r;
9752 r->parent = p;
9753 r->left = 0;
9754 r->right = 0;
9755 r->balance = 0;
9757 while (p)
9759 struct init_node *s;
9761 if (r == p->left)
9763 if (p->balance == 0)
9764 p->balance = -1;
9765 else if (p->balance < 0)
9767 if (r->balance < 0)
9769 /* L rotation. */
9770 p->left = r->right;
9771 if (p->left)
9772 p->left->parent = p;
9773 r->right = p;
9775 p->balance = 0;
9776 r->balance = 0;
9778 s = p->parent;
9779 p->parent = r;
9780 r->parent = s;
9781 if (s)
9783 if (s->left == p)
9784 s->left = r;
9785 else
9786 s->right = r;
9788 else
9789 constructor_pending_elts = r;
9791 else
9793 /* LR rotation. */
9794 struct init_node *t = r->right;
9796 r->right = t->left;
9797 if (r->right)
9798 r->right->parent = r;
9799 t->left = r;
9801 p->left = t->right;
9802 if (p->left)
9803 p->left->parent = p;
9804 t->right = p;
9806 p->balance = t->balance < 0;
9807 r->balance = -(t->balance > 0);
9808 t->balance = 0;
9810 s = p->parent;
9811 p->parent = t;
9812 r->parent = t;
9813 t->parent = s;
9814 if (s)
9816 if (s->left == p)
9817 s->left = t;
9818 else
9819 s->right = t;
9821 else
9822 constructor_pending_elts = t;
9824 break;
9826 else
9828 /* p->balance == +1; growth of left side balances the node. */
9829 p->balance = 0;
9830 break;
9833 else /* r == p->right */
9835 if (p->balance == 0)
9836 /* Growth propagation from right side. */
9837 p->balance++;
9838 else if (p->balance > 0)
9840 if (r->balance > 0)
9842 /* R rotation. */
9843 p->right = r->left;
9844 if (p->right)
9845 p->right->parent = p;
9846 r->left = p;
9848 p->balance = 0;
9849 r->balance = 0;
9851 s = p->parent;
9852 p->parent = r;
9853 r->parent = s;
9854 if (s)
9856 if (s->left == p)
9857 s->left = r;
9858 else
9859 s->right = r;
9861 else
9862 constructor_pending_elts = r;
9864 else /* r->balance == -1 */
9866 /* RL rotation */
9867 struct init_node *t = r->left;
9869 r->left = t->right;
9870 if (r->left)
9871 r->left->parent = r;
9872 t->right = r;
9874 p->right = t->left;
9875 if (p->right)
9876 p->right->parent = p;
9877 t->left = p;
9879 r->balance = (t->balance < 0);
9880 p->balance = -(t->balance > 0);
9881 t->balance = 0;
9883 s = p->parent;
9884 p->parent = t;
9885 r->parent = t;
9886 t->parent = s;
9887 if (s)
9889 if (s->left == p)
9890 s->left = t;
9891 else
9892 s->right = t;
9894 else
9895 constructor_pending_elts = t;
9897 break;
9899 else
9901 /* p->balance == -1; growth of right side balances the node. */
9902 p->balance = 0;
9903 break;
9907 r = p;
9908 p = p->parent;
9912 /* Build AVL tree from a sorted chain. */
9914 static void
9915 set_nonincremental_init (struct obstack * braced_init_obstack)
9917 unsigned HOST_WIDE_INT ix;
9918 tree index, value;
9920 if (TREE_CODE (constructor_type) != RECORD_TYPE
9921 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9922 return;
9924 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9925 add_pending_init (input_location, index, value, NULL_TREE, true,
9926 braced_init_obstack);
9927 constructor_elements = NULL;
9928 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9930 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9931 /* Skip any nameless bit fields at the beginning. */
9932 while (constructor_unfilled_fields != NULL_TREE
9933 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9934 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9937 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9939 if (TYPE_DOMAIN (constructor_type))
9940 constructor_unfilled_index
9941 = convert (bitsizetype,
9942 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9943 else
9944 constructor_unfilled_index = bitsize_zero_node;
9946 constructor_incremental = 0;
9949 /* Build AVL tree from a string constant. */
9951 static void
9952 set_nonincremental_init_from_string (tree str,
9953 struct obstack * braced_init_obstack)
9955 tree value, purpose, type;
9956 HOST_WIDE_INT val[2];
9957 const char *p, *end;
9958 int byte, wchar_bytes, charwidth, bitpos;
9960 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9962 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9963 charwidth = TYPE_PRECISION (char_type_node);
9964 gcc_assert ((size_t) wchar_bytes * charwidth
9965 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9966 type = TREE_TYPE (constructor_type);
9967 p = TREE_STRING_POINTER (str);
9968 end = p + TREE_STRING_LENGTH (str);
9970 for (purpose = bitsize_zero_node;
9971 p < end
9972 && !(constructor_max_index
9973 && tree_int_cst_lt (constructor_max_index, purpose));
9974 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9976 if (wchar_bytes == 1)
9978 val[0] = (unsigned char) *p++;
9979 val[1] = 0;
9981 else
9983 val[1] = 0;
9984 val[0] = 0;
9985 for (byte = 0; byte < wchar_bytes; byte++)
9987 if (BYTES_BIG_ENDIAN)
9988 bitpos = (wchar_bytes - byte - 1) * charwidth;
9989 else
9990 bitpos = byte * charwidth;
9991 val[bitpos / HOST_BITS_PER_WIDE_INT]
9992 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9993 << (bitpos % HOST_BITS_PER_WIDE_INT);
9997 if (!TYPE_UNSIGNED (type))
9999 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
10000 if (bitpos < HOST_BITS_PER_WIDE_INT)
10002 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
10004 val[0] |= HOST_WIDE_INT_M1U << bitpos;
10005 val[1] = -1;
10008 else if (bitpos == HOST_BITS_PER_WIDE_INT)
10010 if (val[0] < 0)
10011 val[1] = -1;
10013 else if (val[1] & (HOST_WIDE_INT_1
10014 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
10015 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
10018 value = wide_int_to_tree (type,
10019 wide_int::from_array (val, 2,
10020 HOST_BITS_PER_WIDE_INT * 2));
10021 add_pending_init (input_location, purpose, value, NULL_TREE, true,
10022 braced_init_obstack);
10025 constructor_incremental = 0;
10028 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
10029 not initialized yet. */
10031 static tree
10032 find_init_member (tree field, struct obstack * braced_init_obstack)
10034 struct init_node *p;
10036 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10038 if (constructor_incremental
10039 && tree_int_cst_lt (field, constructor_unfilled_index))
10040 set_nonincremental_init (braced_init_obstack);
10042 p = constructor_pending_elts;
10043 while (p)
10045 if (tree_int_cst_lt (field, p->purpose))
10046 p = p->left;
10047 else if (tree_int_cst_lt (p->purpose, field))
10048 p = p->right;
10049 else
10050 return p->value;
10053 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
10055 tree bitpos = bit_position (field);
10057 if (constructor_incremental
10058 && (!constructor_unfilled_fields
10059 || tree_int_cst_lt (bitpos,
10060 bit_position (constructor_unfilled_fields))))
10061 set_nonincremental_init (braced_init_obstack);
10063 p = constructor_pending_elts;
10064 while (p)
10066 if (field == p->purpose)
10067 return p->value;
10068 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
10069 p = p->left;
10070 else
10071 p = p->right;
10074 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10076 if (!vec_safe_is_empty (constructor_elements)
10077 && (constructor_elements->last ().index == field))
10078 return constructor_elements->last ().value;
10080 return NULL_TREE;
10083 /* "Output" the next constructor element.
10084 At top level, really output it to assembler code now.
10085 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
10086 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
10087 TYPE is the data type that the containing data type wants here.
10088 FIELD is the field (a FIELD_DECL) or the index that this element fills.
10089 If VALUE is a string constant, STRICT_STRING is true if it is
10090 unparenthesized or we should not warn here for it being parenthesized.
10091 For other types of VALUE, STRICT_STRING is not used.
10093 PENDING if true means output pending elements that belong
10094 right after this element. (PENDING is normally true;
10095 it is false while outputting pending elements, to avoid recursion.)
10097 IMPLICIT is true if value comes from pop_init_level (1),
10098 the new initializer has been merged with the existing one
10099 and thus no warnings should be emitted about overriding an
10100 existing initializer. */
10102 static void
10103 output_init_element (location_t loc, tree value, tree origtype,
10104 bool strict_string, tree type, tree field, bool pending,
10105 bool implicit, struct obstack * braced_init_obstack)
10107 tree semantic_type = NULL_TREE;
10108 bool maybe_const = true;
10109 bool npc, int_const_expr, arith_const_expr;
10111 if (type == error_mark_node || value == error_mark_node)
10113 constructor_erroneous = 1;
10114 return;
10116 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
10117 && (TREE_CODE (value) == STRING_CST
10118 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
10119 && !(TREE_CODE (value) == STRING_CST
10120 && TREE_CODE (type) == ARRAY_TYPE
10121 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
10122 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
10123 TYPE_MAIN_VARIANT (type)))
10124 value = array_to_pointer_conversion (input_location, value);
10126 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
10127 && require_constant_value && pending)
10129 /* As an extension, allow initializing objects with static storage
10130 duration with compound literals (which are then treated just as
10131 the brace enclosed list they contain). */
10132 if (flag_isoc99)
10133 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
10134 "constant");
10135 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
10136 value = DECL_INITIAL (decl);
10139 npc = null_pointer_constant_p (value);
10140 int_const_expr = (TREE_CODE (value) == INTEGER_CST
10141 && !TREE_OVERFLOW (value)
10142 && INTEGRAL_TYPE_P (TREE_TYPE (value)));
10143 /* Not fully determined before folding. */
10144 arith_const_expr = true;
10145 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
10147 semantic_type = TREE_TYPE (value);
10148 value = TREE_OPERAND (value, 0);
10150 value = c_fully_fold (value, require_constant_value, &maybe_const);
10151 /* TODO: this may not detect all cases of expressions folding to
10152 constants that are not arithmetic constant expressions. */
10153 if (!maybe_const)
10154 arith_const_expr = false;
10155 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
10156 && TREE_CODE (TREE_TYPE (value)) != REAL_TYPE
10157 && TREE_CODE (TREE_TYPE (value)) != COMPLEX_TYPE)
10158 arith_const_expr = false;
10159 else if (TREE_CODE (value) != INTEGER_CST
10160 && TREE_CODE (value) != REAL_CST
10161 && TREE_CODE (value) != COMPLEX_CST)
10162 arith_const_expr = false;
10163 else if (TREE_OVERFLOW (value))
10164 arith_const_expr = false;
10166 if (value == error_mark_node)
10167 constructor_erroneous = 1;
10168 else if (!TREE_CONSTANT (value))
10169 constructor_constant = 0;
10170 else if (!initializer_constant_valid_p (value,
10171 TREE_TYPE (value),
10172 AGGREGATE_TYPE_P (constructor_type)
10173 && TYPE_REVERSE_STORAGE_ORDER
10174 (constructor_type))
10175 || (RECORD_OR_UNION_TYPE_P (constructor_type)
10176 && DECL_C_BIT_FIELD (field)
10177 && TREE_CODE (value) != INTEGER_CST))
10178 constructor_simple = 0;
10179 if (!maybe_const)
10180 constructor_nonconst = 1;
10182 /* Digest the initializer and issue any errors about incompatible
10183 types before issuing errors about non-constant initializers. */
10184 tree new_value = value;
10185 if (semantic_type)
10186 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
10187 /* In the case of braces around a scalar initializer, the result of
10188 this initializer processing goes through digest_init again at the
10189 outer level. In the case of a constexpr initializer for a
10190 pointer, avoid converting a null pointer constant to something
10191 that is not a null pointer constant to avoid a spurious error
10192 from that second processing. */
10193 if (!require_constexpr_value
10194 || !npc
10195 || TREE_CODE (constructor_type) != POINTER_TYPE)
10196 new_value = digest_init (loc, type, new_value, origtype, npc,
10197 int_const_expr, arith_const_expr, strict_string,
10198 require_constant_value, require_constexpr_value);
10199 if (new_value == error_mark_node)
10201 constructor_erroneous = 1;
10202 return;
10204 if (require_constant_value || require_constant_elements)
10205 constant_expression_warning (new_value);
10207 /* Proceed to check the constness of the original initializer. */
10208 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
10210 if (require_constant_value)
10212 error_init (loc, "initializer element is not constant");
10213 value = error_mark_node;
10215 else if (require_constant_elements)
10216 pedwarn (loc, OPT_Wpedantic,
10217 "initializer element is not computable at load time");
10219 else if (!maybe_const
10220 && (require_constant_value || require_constant_elements))
10221 pedwarn_init (loc, OPT_Wpedantic,
10222 "initializer element is not a constant expression");
10223 /* digest_init has already carried out the additional checks
10224 required for 'constexpr' initializers (using the information
10225 passed to it about whether the original initializer was certain
10226 kinds of constant expression), so that check does not need to be
10227 repeated here. */
10229 /* Issue -Wc++-compat warnings about initializing a bitfield with
10230 enum type. */
10231 if (warn_cxx_compat
10232 && field != NULL_TREE
10233 && TREE_CODE (field) == FIELD_DECL
10234 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
10235 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
10236 != TYPE_MAIN_VARIANT (type))
10237 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
10239 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
10240 if (checktype != error_mark_node
10241 && (TYPE_MAIN_VARIANT (checktype)
10242 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
10243 warning_init (loc, OPT_Wc___compat,
10244 "enum conversion in initialization is invalid in C++");
10247 /* If this field is empty and does not have side effects (and is not at
10248 the end of structure), don't do anything other than checking the
10249 initializer. */
10250 if (field
10251 && (TREE_TYPE (field) == error_mark_node
10252 || (COMPLETE_TYPE_P (TREE_TYPE (field))
10253 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
10254 && !TREE_SIDE_EFFECTS (new_value)
10255 && (TREE_CODE (constructor_type) == ARRAY_TYPE
10256 || DECL_CHAIN (field)))))
10257 return;
10259 /* Finally, set VALUE to the initializer value digested above. */
10260 value = new_value;
10262 /* If this element doesn't come next in sequence,
10263 put it on constructor_pending_elts. */
10264 if (TREE_CODE (constructor_type) == ARRAY_TYPE
10265 && (!constructor_incremental
10266 || !tree_int_cst_equal (field, constructor_unfilled_index)))
10268 if (constructor_incremental
10269 && tree_int_cst_lt (field, constructor_unfilled_index))
10270 set_nonincremental_init (braced_init_obstack);
10272 add_pending_init (loc, field, value, origtype, implicit,
10273 braced_init_obstack);
10274 return;
10276 else if (TREE_CODE (constructor_type) == RECORD_TYPE
10277 && (!constructor_incremental
10278 || field != constructor_unfilled_fields))
10280 /* We do this for records but not for unions. In a union,
10281 no matter which field is specified, it can be initialized
10282 right away since it starts at the beginning of the union. */
10283 if (constructor_incremental)
10285 if (!constructor_unfilled_fields)
10286 set_nonincremental_init (braced_init_obstack);
10287 else
10289 tree bitpos, unfillpos;
10291 bitpos = bit_position (field);
10292 unfillpos = bit_position (constructor_unfilled_fields);
10294 if (tree_int_cst_lt (bitpos, unfillpos))
10295 set_nonincremental_init (braced_init_obstack);
10299 add_pending_init (loc, field, value, origtype, implicit,
10300 braced_init_obstack);
10301 return;
10303 else if (TREE_CODE (constructor_type) == UNION_TYPE
10304 && !vec_safe_is_empty (constructor_elements))
10306 if (!implicit)
10308 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
10309 warning_init (loc, OPT_Woverride_init_side_effects,
10310 "initialized field with side-effects overwritten");
10311 else if (warn_override_init)
10312 warning_init (loc, OPT_Woverride_init,
10313 "initialized field overwritten");
10316 /* We can have just one union field set. */
10317 constructor_elements = NULL;
10320 /* Otherwise, output this element either to
10321 constructor_elements or to the assembler file. */
10323 constructor_elt celt = {field, value};
10324 vec_safe_push (constructor_elements, celt);
10326 /* Advance the variable that indicates sequential elements output. */
10327 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10328 constructor_unfilled_index
10329 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
10330 bitsize_one_node);
10331 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
10333 constructor_unfilled_fields
10334 = DECL_CHAIN (constructor_unfilled_fields);
10336 /* Skip any nameless bit fields. */
10337 while (constructor_unfilled_fields != NULL_TREE
10338 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
10339 constructor_unfilled_fields =
10340 DECL_CHAIN (constructor_unfilled_fields);
10342 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10343 constructor_unfilled_fields = NULL_TREE;
10345 /* Now output any pending elements which have become next. */
10346 if (pending)
10347 output_pending_init_elements (0, braced_init_obstack);
10350 /* For two FIELD_DECLs in the same chain, return -1 if field1
10351 comes before field2, 1 if field1 comes after field2 and
10352 0 if field1 == field2. */
10354 static int
10355 init_field_decl_cmp (tree field1, tree field2)
10357 if (field1 == field2)
10358 return 0;
10360 tree bitpos1 = bit_position (field1);
10361 tree bitpos2 = bit_position (field2);
10362 if (tree_int_cst_equal (bitpos1, bitpos2))
10364 /* If one of the fields has non-zero bitsize, then that
10365 field must be the last one in a sequence of zero
10366 sized fields, fields after it will have bigger
10367 bit_position. */
10368 if (TREE_TYPE (field1) != error_mark_node
10369 && COMPLETE_TYPE_P (TREE_TYPE (field1))
10370 && integer_nonzerop (TREE_TYPE (field1)))
10371 return 1;
10372 if (TREE_TYPE (field2) != error_mark_node
10373 && COMPLETE_TYPE_P (TREE_TYPE (field2))
10374 && integer_nonzerop (TREE_TYPE (field2)))
10375 return -1;
10376 /* Otherwise, fallback to DECL_CHAIN walk to find out
10377 which field comes earlier. Walk chains of both
10378 fields, so that if field1 and field2 are close to each
10379 other in either order, it is found soon even for large
10380 sequences of zero sized fields. */
10381 tree f1 = field1, f2 = field2;
10382 while (1)
10384 f1 = DECL_CHAIN (f1);
10385 f2 = DECL_CHAIN (f2);
10386 if (f1 == NULL_TREE)
10388 gcc_assert (f2);
10389 return 1;
10391 if (f2 == NULL_TREE)
10392 return -1;
10393 if (f1 == field2)
10394 return -1;
10395 if (f2 == field1)
10396 return 1;
10397 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
10398 return 1;
10399 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
10400 return -1;
10403 else if (tree_int_cst_lt (bitpos1, bitpos2))
10404 return -1;
10405 else
10406 return 1;
10409 /* Output any pending elements which have become next.
10410 As we output elements, constructor_unfilled_{fields,index}
10411 advances, which may cause other elements to become next;
10412 if so, they too are output.
10414 If ALL is 0, we return when there are
10415 no more pending elements to output now.
10417 If ALL is 1, we output space as necessary so that
10418 we can output all the pending elements. */
10419 static void
10420 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
10422 struct init_node *elt = constructor_pending_elts;
10423 tree next;
10425 retry:
10427 /* Look through the whole pending tree.
10428 If we find an element that should be output now,
10429 output it. Otherwise, set NEXT to the element
10430 that comes first among those still pending. */
10432 next = NULL_TREE;
10433 while (elt)
10435 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10437 if (tree_int_cst_equal (elt->purpose,
10438 constructor_unfilled_index))
10439 output_init_element (input_location, elt->value, elt->origtype,
10440 true, TREE_TYPE (constructor_type),
10441 constructor_unfilled_index, false, false,
10442 braced_init_obstack);
10443 else if (tree_int_cst_lt (constructor_unfilled_index,
10444 elt->purpose))
10446 /* Advance to the next smaller node. */
10447 if (elt->left)
10448 elt = elt->left;
10449 else
10451 /* We have reached the smallest node bigger than the
10452 current unfilled index. Fill the space first. */
10453 next = elt->purpose;
10454 break;
10457 else
10459 /* Advance to the next bigger node. */
10460 if (elt->right)
10461 elt = elt->right;
10462 else
10464 /* We have reached the biggest node in a subtree. Find
10465 the parent of it, which is the next bigger node. */
10466 while (elt->parent && elt->parent->right == elt)
10467 elt = elt->parent;
10468 elt = elt->parent;
10469 if (elt && tree_int_cst_lt (constructor_unfilled_index,
10470 elt->purpose))
10472 next = elt->purpose;
10473 break;
10478 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10480 /* If the current record is complete we are done. */
10481 if (constructor_unfilled_fields == NULL_TREE)
10482 break;
10484 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10485 elt->purpose);
10486 if (cmp == 0)
10487 output_init_element (input_location, elt->value, elt->origtype,
10488 true, TREE_TYPE (elt->purpose),
10489 elt->purpose, false, false,
10490 braced_init_obstack);
10491 else if (cmp < 0)
10493 /* Advance to the next smaller node. */
10494 if (elt->left)
10495 elt = elt->left;
10496 else
10498 /* We have reached the smallest node bigger than the
10499 current unfilled field. Fill the space first. */
10500 next = elt->purpose;
10501 break;
10504 else
10506 /* Advance to the next bigger node. */
10507 if (elt->right)
10508 elt = elt->right;
10509 else
10511 /* We have reached the biggest node in a subtree. Find
10512 the parent of it, which is the next bigger node. */
10513 while (elt->parent && elt->parent->right == elt)
10514 elt = elt->parent;
10515 elt = elt->parent;
10516 if (elt
10517 && init_field_decl_cmp (constructor_unfilled_fields,
10518 elt->purpose) < 0)
10520 next = elt->purpose;
10521 break;
10528 /* Ordinarily return, but not if we want to output all
10529 and there are elements left. */
10530 if (!(all && next != NULL_TREE))
10531 return;
10533 /* If it's not incremental, just skip over the gap, so that after
10534 jumping to retry we will output the next successive element. */
10535 if (RECORD_OR_UNION_TYPE_P (constructor_type))
10536 constructor_unfilled_fields = next;
10537 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10538 constructor_unfilled_index = next;
10540 /* ELT now points to the node in the pending tree with the next
10541 initializer to output. */
10542 goto retry;
10545 /* Expression VALUE coincides with the start of type TYPE in a braced
10546 initializer. Return true if we should treat VALUE as initializing
10547 the first element of TYPE, false if we should treat it as initializing
10548 TYPE as a whole.
10550 If the initializer is clearly invalid, the question becomes:
10551 which choice gives the best error message? */
10553 static bool
10554 initialize_elementwise_p (tree type, tree value)
10556 if (type == error_mark_node || value == error_mark_node)
10557 return false;
10559 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10561 tree value_type = TREE_TYPE (value);
10562 if (value_type == error_mark_node)
10563 return false;
10565 /* GNU vectors can be initialized elementwise. However, treat any
10566 kind of vector value as initializing the vector type as a whole,
10567 regardless of whether the value is a GNU vector. Such initializers
10568 are valid if and only if they would have been valid in a non-braced
10569 initializer like:
10571 TYPE foo = VALUE;
10573 so recursing into the vector type would be at best confusing or at
10574 worst wrong. For example, when -flax-vector-conversions is in effect,
10575 it's possible to initialize a V8HI from a V4SI, even though the vectors
10576 have different element types and different numbers of elements. */
10577 if (gnu_vector_type_p (type))
10578 return !VECTOR_TYPE_P (value_type);
10580 if (AGGREGATE_TYPE_P (type))
10581 return type != TYPE_MAIN_VARIANT (value_type);
10583 return false;
10586 /* Add one non-braced element to the current constructor level.
10587 This adjusts the current position within the constructor's type.
10588 This may also start or terminate implicit levels
10589 to handle a partly-braced initializer.
10591 Once this has found the correct level for the new element,
10592 it calls output_init_element.
10594 IMPLICIT is true if value comes from pop_init_level (1),
10595 the new initializer has been merged with the existing one
10596 and thus no warnings should be emitted about overriding an
10597 existing initializer. */
10599 void
10600 process_init_element (location_t loc, struct c_expr value, bool implicit,
10601 struct obstack * braced_init_obstack)
10603 tree orig_value = value.value;
10604 int string_flag
10605 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10606 bool strict_string = value.original_code == STRING_CST;
10607 bool was_designated = designator_depth != 0;
10609 designator_depth = 0;
10610 designator_erroneous = 0;
10612 if (!implicit && value.value && !integer_zerop (value.value))
10613 constructor_zeroinit = 0;
10615 /* Handle superfluous braces around string cst as in
10616 char x[] = {"foo"}; */
10617 if (constructor_type
10618 && !was_designated
10619 && TREE_CODE (constructor_type) == ARRAY_TYPE
10620 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10621 && integer_zerop (constructor_unfilled_index))
10623 if (constructor_stack->replacement_value.value)
10625 error_init (loc, "excess elements in %qT initializer", constructor_type);
10626 return;
10628 else if (string_flag)
10630 constructor_stack->replacement_value = value;
10631 return;
10635 if (constructor_stack->replacement_value.value != NULL_TREE)
10637 error_init (loc, "excess elements in struct initializer");
10638 return;
10641 /* Ignore elements of a brace group if it is entirely superfluous
10642 and has already been diagnosed, or if the type is erroneous. */
10643 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10644 return;
10646 /* Ignore elements of an initializer for a variable-size type.
10647 Those are diagnosed in the parser (empty initializer braces are OK). */
10648 if (COMPLETE_TYPE_P (constructor_type)
10649 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10650 return;
10652 if (!implicit && warn_designated_init && !was_designated
10653 && TREE_CODE (constructor_type) == RECORD_TYPE
10654 && lookup_attribute ("designated_init",
10655 TYPE_ATTRIBUTES (constructor_type)))
10656 warning_init (loc,
10657 OPT_Wdesignated_init,
10658 "positional initialization of field "
10659 "in %<struct%> declared with %<designated_init%> attribute");
10661 /* If we've exhausted any levels that didn't have braces,
10662 pop them now. */
10663 while (constructor_stack->implicit)
10665 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10666 && constructor_fields == NULL_TREE)
10667 process_init_element (loc,
10668 pop_init_level (loc, 1, braced_init_obstack,
10669 last_init_list_comma),
10670 true, braced_init_obstack);
10671 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10672 || gnu_vector_type_p (constructor_type))
10673 && constructor_max_index
10674 && tree_int_cst_lt (constructor_max_index,
10675 constructor_index))
10676 process_init_element (loc,
10677 pop_init_level (loc, 1, braced_init_obstack,
10678 last_init_list_comma),
10679 true, braced_init_obstack);
10680 else
10681 break;
10684 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10685 if (constructor_range_stack)
10687 /* If value is a compound literal and we'll be just using its
10688 content, don't put it into a SAVE_EXPR. */
10689 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10690 || !require_constant_value)
10692 tree semantic_type = NULL_TREE;
10693 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10695 semantic_type = TREE_TYPE (value.value);
10696 value.value = TREE_OPERAND (value.value, 0);
10698 value.value = save_expr (value.value);
10699 if (semantic_type)
10700 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10701 value.value);
10705 while (1)
10707 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10709 tree fieldtype;
10710 enum tree_code fieldcode;
10712 if (constructor_fields == NULL_TREE)
10714 pedwarn_init (loc, 0, "excess elements in struct initializer");
10715 break;
10718 fieldtype = TREE_TYPE (constructor_fields);
10719 if (fieldtype != error_mark_node)
10720 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10721 fieldcode = TREE_CODE (fieldtype);
10723 /* Error for non-static initialization of a flexible array member. */
10724 if (fieldcode == ARRAY_TYPE
10725 && !require_constant_value
10726 && TYPE_SIZE (fieldtype) == NULL_TREE
10727 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10729 error_init (loc, "non-static initialization of a flexible "
10730 "array member");
10731 break;
10734 /* Error for initialization of a flexible array member with
10735 a string constant if the structure is in an array. E.g.:
10736 struct S { int x; char y[]; };
10737 struct S s[] = { { 1, "foo" } };
10738 is invalid. */
10739 if (string_flag
10740 && fieldcode == ARRAY_TYPE
10741 && constructor_depth > 1
10742 && TYPE_SIZE (fieldtype) == NULL_TREE
10743 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10745 bool in_array_p = false;
10746 for (struct constructor_stack *p = constructor_stack;
10747 p && p->type; p = p->next)
10748 if (TREE_CODE (p->type) == ARRAY_TYPE)
10750 in_array_p = true;
10751 break;
10753 if (in_array_p)
10755 error_init (loc, "initialization of flexible array "
10756 "member in a nested context");
10757 break;
10761 /* Accept a string constant to initialize a subarray. */
10762 if (value.value != NULL_TREE
10763 && fieldcode == ARRAY_TYPE
10764 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10765 && string_flag)
10766 value.value = orig_value;
10767 /* Otherwise, if we have come to a subaggregate,
10768 and we don't have an element of its type, push into it. */
10769 else if (value.value != NULL_TREE
10770 && initialize_elementwise_p (fieldtype, value.value))
10772 push_init_level (loc, 1, braced_init_obstack);
10773 continue;
10776 if (value.value)
10778 push_member_name (constructor_fields);
10779 output_init_element (loc, value.value, value.original_type,
10780 strict_string, fieldtype,
10781 constructor_fields, true, implicit,
10782 braced_init_obstack);
10783 RESTORE_SPELLING_DEPTH (constructor_depth);
10785 else
10786 /* Do the bookkeeping for an element that was
10787 directly output as a constructor. */
10789 /* For a record, keep track of end position of last field. */
10790 if (DECL_SIZE (constructor_fields))
10791 constructor_bit_index
10792 = size_binop_loc (input_location, PLUS_EXPR,
10793 bit_position (constructor_fields),
10794 DECL_SIZE (constructor_fields));
10796 /* If the current field was the first one not yet written out,
10797 it isn't now, so update. */
10798 if (constructor_unfilled_fields == constructor_fields)
10800 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10801 /* Skip any nameless bit fields. */
10802 while (constructor_unfilled_fields != 0
10803 && (DECL_UNNAMED_BIT_FIELD
10804 (constructor_unfilled_fields)))
10805 constructor_unfilled_fields =
10806 DECL_CHAIN (constructor_unfilled_fields);
10810 constructor_fields = DECL_CHAIN (constructor_fields);
10811 /* Skip any nameless bit fields at the beginning. */
10812 while (constructor_fields != NULL_TREE
10813 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10814 constructor_fields = DECL_CHAIN (constructor_fields);
10816 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10818 tree fieldtype;
10819 enum tree_code fieldcode;
10821 if (constructor_fields == NULL_TREE)
10823 pedwarn_init (loc, 0,
10824 "excess elements in union initializer");
10825 break;
10828 fieldtype = TREE_TYPE (constructor_fields);
10829 if (fieldtype != error_mark_node)
10830 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10831 fieldcode = TREE_CODE (fieldtype);
10833 /* Warn that traditional C rejects initialization of unions.
10834 We skip the warning if the value is zero. This is done
10835 under the assumption that the zero initializer in user
10836 code appears conditioned on e.g. __STDC__ to avoid
10837 "missing initializer" warnings and relies on default
10838 initialization to zero in the traditional C case.
10839 We also skip the warning if the initializer is designated,
10840 again on the assumption that this must be conditional on
10841 __STDC__ anyway (and we've already complained about the
10842 member-designator already). */
10843 if (!in_system_header_at (input_location) && !constructor_designated
10844 && !(value.value && (integer_zerop (value.value)
10845 || real_zerop (value.value))))
10846 warning (OPT_Wtraditional, "traditional C rejects initialization "
10847 "of unions");
10849 /* Accept a string constant to initialize a subarray. */
10850 if (value.value != NULL_TREE
10851 && fieldcode == ARRAY_TYPE
10852 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10853 && string_flag)
10854 value.value = orig_value;
10855 /* Otherwise, if we have come to a subaggregate,
10856 and we don't have an element of its type, push into it. */
10857 else if (value.value != NULL_TREE
10858 && initialize_elementwise_p (fieldtype, value.value))
10860 push_init_level (loc, 1, braced_init_obstack);
10861 continue;
10864 if (value.value)
10866 push_member_name (constructor_fields);
10867 output_init_element (loc, value.value, value.original_type,
10868 strict_string, fieldtype,
10869 constructor_fields, true, implicit,
10870 braced_init_obstack);
10871 RESTORE_SPELLING_DEPTH (constructor_depth);
10873 else
10874 /* Do the bookkeeping for an element that was
10875 directly output as a constructor. */
10877 constructor_bit_index = DECL_SIZE (constructor_fields);
10878 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10881 constructor_fields = NULL_TREE;
10883 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10885 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10886 enum tree_code eltcode = TREE_CODE (elttype);
10888 /* Accept a string constant to initialize a subarray. */
10889 if (value.value != NULL_TREE
10890 && eltcode == ARRAY_TYPE
10891 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10892 && string_flag)
10893 value.value = orig_value;
10894 /* Otherwise, if we have come to a subaggregate,
10895 and we don't have an element of its type, push into it. */
10896 else if (value.value != NULL_TREE
10897 && initialize_elementwise_p (elttype, value.value))
10899 push_init_level (loc, 1, braced_init_obstack);
10900 continue;
10903 if (constructor_max_index != NULL_TREE
10904 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10905 || integer_all_onesp (constructor_max_index)))
10907 pedwarn_init (loc, 0,
10908 "excess elements in array initializer");
10909 break;
10912 /* Now output the actual element. */
10913 if (value.value)
10915 push_array_bounds (tree_to_uhwi (constructor_index));
10916 output_init_element (loc, value.value, value.original_type,
10917 strict_string, elttype,
10918 constructor_index, true, implicit,
10919 braced_init_obstack);
10920 RESTORE_SPELLING_DEPTH (constructor_depth);
10923 constructor_index
10924 = size_binop_loc (input_location, PLUS_EXPR,
10925 constructor_index, bitsize_one_node);
10927 if (!value.value)
10928 /* If we are doing the bookkeeping for an element that was
10929 directly output as a constructor, we must update
10930 constructor_unfilled_index. */
10931 constructor_unfilled_index = constructor_index;
10933 else if (gnu_vector_type_p (constructor_type))
10935 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10937 /* Do a basic check of initializer size. Note that vectors
10938 always have a fixed size derived from their type. */
10939 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10941 pedwarn_init (loc, 0,
10942 "excess elements in vector initializer");
10943 break;
10946 /* Now output the actual element. */
10947 if (value.value)
10949 if (TREE_CODE (value.value) == VECTOR_CST)
10950 elttype = TYPE_MAIN_VARIANT (constructor_type);
10951 output_init_element (loc, value.value, value.original_type,
10952 strict_string, elttype,
10953 constructor_index, true, implicit,
10954 braced_init_obstack);
10957 constructor_index
10958 = size_binop_loc (input_location,
10959 PLUS_EXPR, constructor_index, bitsize_one_node);
10961 if (!value.value)
10962 /* If we are doing the bookkeeping for an element that was
10963 directly output as a constructor, we must update
10964 constructor_unfilled_index. */
10965 constructor_unfilled_index = constructor_index;
10968 /* Handle the sole element allowed in a braced initializer
10969 for a scalar variable. */
10970 else if (constructor_type != error_mark_node
10971 && constructor_fields == NULL_TREE)
10973 pedwarn_init (loc, 0,
10974 "excess elements in scalar initializer");
10975 break;
10977 else
10979 if (value.value)
10980 output_init_element (loc, value.value, value.original_type,
10981 strict_string, constructor_type,
10982 NULL_TREE, true, implicit,
10983 braced_init_obstack);
10984 constructor_fields = NULL_TREE;
10987 /* Handle range initializers either at this level or anywhere higher
10988 in the designator stack. */
10989 if (constructor_range_stack)
10991 struct constructor_range_stack *p, *range_stack;
10992 int finish = 0;
10994 range_stack = constructor_range_stack;
10995 constructor_range_stack = 0;
10996 while (constructor_stack != range_stack->stack)
10998 gcc_assert (constructor_stack->implicit);
10999 process_init_element (loc,
11000 pop_init_level (loc, 1,
11001 braced_init_obstack,
11002 last_init_list_comma),
11003 true, braced_init_obstack);
11005 for (p = range_stack;
11006 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
11007 p = p->prev)
11009 gcc_assert (constructor_stack->implicit);
11010 process_init_element (loc,
11011 pop_init_level (loc, 1,
11012 braced_init_obstack,
11013 last_init_list_comma),
11014 true, braced_init_obstack);
11017 p->index = size_binop_loc (input_location,
11018 PLUS_EXPR, p->index, bitsize_one_node);
11019 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
11020 finish = 1;
11022 while (1)
11024 constructor_index = p->index;
11025 constructor_fields = p->fields;
11026 if (finish && p->range_end && p->index == p->range_start)
11028 finish = 0;
11029 p->prev = 0;
11031 p = p->next;
11032 if (!p)
11033 break;
11034 finish_implicit_inits (loc, braced_init_obstack);
11035 push_init_level (loc, 2, braced_init_obstack);
11036 p->stack = constructor_stack;
11037 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
11038 p->index = p->range_start;
11041 if (!finish)
11042 constructor_range_stack = range_stack;
11043 continue;
11046 break;
11049 constructor_range_stack = 0;
11052 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
11053 (guaranteed to be 'volatile' or null) and ARGS (represented using
11054 an ASM_EXPR node). */
11055 tree
11056 build_asm_stmt (bool is_volatile, tree args)
11058 if (is_volatile)
11059 ASM_VOLATILE_P (args) = 1;
11060 return add_stmt (args);
11063 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
11064 some INPUTS, and some CLOBBERS. The latter three may be NULL.
11065 SIMPLE indicates whether there was anything at all after the
11066 string in the asm expression -- asm("blah") and asm("blah" : )
11067 are subtly different. We use a ASM_EXPR node to represent this.
11068 LOC is the location of the asm, and IS_INLINE says whether this
11069 is asm inline. */
11070 tree
11071 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
11072 tree clobbers, tree labels, bool simple, bool is_inline)
11074 tree tail;
11075 tree args;
11076 int i;
11077 const char *constraint;
11078 const char **oconstraints;
11079 bool allows_mem, allows_reg, is_inout;
11080 int ninputs, noutputs;
11082 ninputs = list_length (inputs);
11083 noutputs = list_length (outputs);
11084 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
11086 string = resolve_asm_operand_names (string, outputs, inputs, labels);
11088 /* Remove output conversions that change the type but not the mode. */
11089 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
11091 tree output = TREE_VALUE (tail);
11093 output = c_fully_fold (output, false, NULL, true);
11095 /* ??? Really, this should not be here. Users should be using a
11096 proper lvalue, dammit. But there's a long history of using casts
11097 in the output operands. In cases like longlong.h, this becomes a
11098 primitive form of typechecking -- if the cast can be removed, then
11099 the output operand had a type of the proper width; otherwise we'll
11100 get an error. Gross, but ... */
11101 STRIP_NOPS (output);
11103 if (!lvalue_or_else (loc, output, lv_asm))
11104 output = error_mark_node;
11106 if (output != error_mark_node
11107 && (TREE_READONLY (output)
11108 || TYPE_READONLY (TREE_TYPE (output))
11109 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
11110 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
11111 readonly_error (loc, output, lv_asm);
11113 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
11114 oconstraints[i] = constraint;
11116 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
11117 &allows_mem, &allows_reg, &is_inout))
11119 /* If the operand is going to end up in memory,
11120 mark it addressable. */
11121 if (!allows_reg && !c_mark_addressable (output))
11122 output = error_mark_node;
11123 if (!(!allows_reg && allows_mem)
11124 && output != error_mark_node
11125 && VOID_TYPE_P (TREE_TYPE (output)))
11127 error_at (loc, "invalid use of void expression");
11128 output = error_mark_node;
11131 else
11132 output = error_mark_node;
11134 TREE_VALUE (tail) = output;
11137 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
11139 tree input;
11141 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
11142 input = TREE_VALUE (tail);
11144 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
11145 oconstraints, &allows_mem, &allows_reg))
11147 /* If the operand is going to end up in memory,
11148 mark it addressable. */
11149 if (!allows_reg && allows_mem)
11151 input = c_fully_fold (input, false, NULL, true);
11153 /* Strip the nops as we allow this case. FIXME, this really
11154 should be rejected or made deprecated. */
11155 STRIP_NOPS (input);
11156 if (!c_mark_addressable (input))
11157 input = error_mark_node;
11159 else
11161 struct c_expr expr;
11162 memset (&expr, 0, sizeof (expr));
11163 expr.value = input;
11164 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
11165 input = c_fully_fold (expr.value, false, NULL);
11167 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
11169 error_at (loc, "invalid use of void expression");
11170 input = error_mark_node;
11174 else
11175 input = error_mark_node;
11177 TREE_VALUE (tail) = input;
11180 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
11182 /* asm statements without outputs, including simple ones, are treated
11183 as volatile. */
11184 ASM_INPUT_P (args) = simple;
11185 ASM_VOLATILE_P (args) = (noutputs == 0);
11186 ASM_INLINE_P (args) = is_inline;
11188 return args;
11191 /* Generate a goto statement to LABEL. LOC is the location of the
11192 GOTO. */
11194 tree
11195 c_finish_goto_label (location_t loc, tree label)
11197 tree decl = lookup_label_for_goto (loc, label);
11198 if (!decl)
11199 return NULL_TREE;
11200 TREE_USED (decl) = 1;
11202 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
11203 tree t = build1 (GOTO_EXPR, void_type_node, decl);
11204 SET_EXPR_LOCATION (t, loc);
11205 return add_stmt (t);
11209 /* Generate a computed goto statement to EXPR. LOC is the location of
11210 the GOTO. */
11212 tree
11213 c_finish_goto_ptr (location_t loc, c_expr val)
11215 tree expr = val.value;
11216 tree t;
11217 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
11218 if (expr != error_mark_node
11219 && !POINTER_TYPE_P (TREE_TYPE (expr))
11220 && !null_pointer_constant_p (expr))
11222 error_at (val.get_location (),
11223 "computed goto must be pointer type");
11224 expr = build_zero_cst (ptr_type_node);
11226 expr = c_fully_fold (expr, false, NULL);
11227 expr = convert (ptr_type_node, expr);
11228 t = build1 (GOTO_EXPR, void_type_node, expr);
11229 SET_EXPR_LOCATION (t, loc);
11230 return add_stmt (t);
11233 /* Generate a C `return' statement. RETVAL is the expression for what
11234 to return, or a null pointer for `return;' with no value. LOC is
11235 the location of the return statement, or the location of the expression,
11236 if the statement has any. If ORIGTYPE is not NULL_TREE, it
11237 is the original type of RETVAL. */
11239 tree
11240 c_finish_return (location_t loc, tree retval, tree origtype)
11242 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
11243 bool no_warning = false;
11244 bool npc = false;
11246 /* Use the expansion point to handle cases such as returning NULL
11247 in a function returning void. */
11248 location_t xloc = expansion_point_location_if_in_system_header (loc);
11250 if (TREE_THIS_VOLATILE (current_function_decl))
11251 warning_at (xloc, 0,
11252 "function declared %<noreturn%> has a %<return%> statement");
11254 if (retval)
11256 tree semantic_type = NULL_TREE;
11257 npc = null_pointer_constant_p (retval);
11258 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
11260 semantic_type = TREE_TYPE (retval);
11261 retval = TREE_OPERAND (retval, 0);
11263 retval = c_fully_fold (retval, false, NULL);
11264 if (semantic_type
11265 && valtype != NULL_TREE
11266 && TREE_CODE (valtype) != VOID_TYPE)
11267 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
11270 if (!retval)
11272 current_function_returns_null = 1;
11273 if ((warn_return_type >= 0 || flag_isoc99)
11274 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
11276 bool warned_here;
11277 if (flag_isoc99)
11278 warned_here = pedwarn
11279 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
11280 "%<return%> with no value, in function returning non-void");
11281 else
11282 warned_here = warning_at
11283 (loc, OPT_Wreturn_type,
11284 "%<return%> with no value, in function returning non-void");
11285 no_warning = true;
11286 if (warned_here)
11287 inform (DECL_SOURCE_LOCATION (current_function_decl),
11288 "declared here");
11291 else if (valtype == NULL_TREE || VOID_TYPE_P (valtype))
11293 current_function_returns_null = 1;
11294 bool warned_here;
11295 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
11296 warned_here = pedwarn
11297 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
11298 "%<return%> with a value, in function returning void");
11299 else
11300 warned_here = pedwarn
11301 (xloc, OPT_Wpedantic, "ISO C forbids "
11302 "%<return%> with expression, in function returning void");
11303 if (warned_here)
11304 inform (DECL_SOURCE_LOCATION (current_function_decl),
11305 "declared here");
11307 else
11309 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
11310 retval, origtype, ic_return,
11311 npc, NULL_TREE, NULL_TREE, 0);
11312 tree res = DECL_RESULT (current_function_decl);
11313 tree inner;
11314 bool save;
11316 current_function_returns_value = 1;
11317 if (t == error_mark_node)
11318 return NULL_TREE;
11320 save = in_late_binary_op;
11321 if (C_BOOLEAN_TYPE_P (TREE_TYPE (res))
11322 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
11323 || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
11324 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
11325 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
11326 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
11327 in_late_binary_op = true;
11328 inner = t = convert (TREE_TYPE (res), t);
11329 in_late_binary_op = save;
11331 /* Strip any conversions, additions, and subtractions, and see if
11332 we are returning the address of a local variable. Warn if so. */
11333 while (1)
11335 switch (TREE_CODE (inner))
11337 CASE_CONVERT:
11338 case NON_LVALUE_EXPR:
11339 case PLUS_EXPR:
11340 case POINTER_PLUS_EXPR:
11341 inner = TREE_OPERAND (inner, 0);
11342 continue;
11344 case MINUS_EXPR:
11345 /* If the second operand of the MINUS_EXPR has a pointer
11346 type (or is converted from it), this may be valid, so
11347 don't give a warning. */
11349 tree op1 = TREE_OPERAND (inner, 1);
11351 while (!POINTER_TYPE_P (TREE_TYPE (op1))
11352 && (CONVERT_EXPR_P (op1)
11353 || TREE_CODE (op1) == NON_LVALUE_EXPR))
11354 op1 = TREE_OPERAND (op1, 0);
11356 if (POINTER_TYPE_P (TREE_TYPE (op1)))
11357 break;
11359 inner = TREE_OPERAND (inner, 0);
11360 continue;
11363 case ADDR_EXPR:
11364 inner = TREE_OPERAND (inner, 0);
11366 while (REFERENCE_CLASS_P (inner)
11367 && !INDIRECT_REF_P (inner))
11368 inner = TREE_OPERAND (inner, 0);
11370 if (DECL_P (inner)
11371 && !DECL_EXTERNAL (inner)
11372 && !TREE_STATIC (inner)
11373 && DECL_CONTEXT (inner) == current_function_decl
11374 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
11376 if (TREE_CODE (inner) == LABEL_DECL)
11377 warning_at (loc, OPT_Wreturn_local_addr,
11378 "function returns address of label");
11379 else
11381 warning_at (loc, OPT_Wreturn_local_addr,
11382 "function returns address of local variable");
11383 tree zero = build_zero_cst (TREE_TYPE (res));
11384 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
11387 break;
11389 default:
11390 break;
11393 break;
11396 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
11397 SET_EXPR_LOCATION (retval, loc);
11399 if (warn_sequence_point)
11400 verify_sequence_points (retval);
11403 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
11404 if (no_warning)
11405 suppress_warning (ret_stmt, OPT_Wreturn_type);
11406 return add_stmt (ret_stmt);
11409 struct c_switch {
11410 /* The SWITCH_STMT being built. */
11411 tree switch_stmt;
11413 /* The original type of the testing expression, i.e. before the
11414 default conversion is applied. */
11415 tree orig_type;
11417 /* A splay-tree mapping the low element of a case range to the high
11418 element, or NULL_TREE if there is no high element. Used to
11419 determine whether or not a new case label duplicates an old case
11420 label. We need a tree, rather than simply a hash table, because
11421 of the GNU case range extension. */
11422 splay_tree cases;
11424 /* The bindings at the point of the switch. This is used for
11425 warnings crossing decls when branching to a case label. */
11426 struct c_spot_bindings *bindings;
11428 /* Whether the switch includes any break statements. */
11429 bool break_stmt_seen_p;
11431 /* The next node on the stack. */
11432 struct c_switch *next;
11434 /* Remember whether the controlling expression had boolean type
11435 before integer promotions for the sake of -Wswitch-bool. */
11436 bool bool_cond_p;
11439 /* A stack of the currently active switch statements. The innermost
11440 switch statement is on the top of the stack. There is no need to
11441 mark the stack for garbage collection because it is only active
11442 during the processing of the body of a function, and we never
11443 collect at that point. */
11445 struct c_switch *c_switch_stack;
11447 /* Start a C switch statement, testing expression EXP. Return the new
11448 SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
11449 SWITCH_COND_LOC is the location of the switch's condition.
11450 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
11452 tree
11453 c_start_switch (location_t switch_loc,
11454 location_t switch_cond_loc,
11455 tree exp, bool explicit_cast_p)
11457 tree orig_type = error_mark_node;
11458 bool bool_cond_p = false;
11459 struct c_switch *cs;
11461 if (exp != error_mark_node)
11463 orig_type = TREE_TYPE (exp);
11465 if (!INTEGRAL_TYPE_P (orig_type))
11467 if (orig_type != error_mark_node)
11469 error_at (switch_cond_loc, "switch quantity not an integer");
11470 orig_type = error_mark_node;
11472 exp = integer_zero_node;
11474 else
11476 tree type = TYPE_MAIN_VARIANT (orig_type);
11477 tree e = exp;
11479 /* Warn if the condition has boolean value. */
11480 while (TREE_CODE (e) == COMPOUND_EXPR)
11481 e = TREE_OPERAND (e, 1);
11483 if ((C_BOOLEAN_TYPE_P (type)
11484 || truth_value_p (TREE_CODE (e)))
11485 /* Explicit cast to int suppresses this warning. */
11486 && !(TREE_CODE (type) == INTEGER_TYPE
11487 && explicit_cast_p))
11488 bool_cond_p = true;
11490 if (!in_system_header_at (input_location)
11491 && (type == long_integer_type_node
11492 || type == long_unsigned_type_node))
11493 warning_at (switch_cond_loc,
11494 OPT_Wtraditional, "%<long%> switch expression not "
11495 "converted to %<int%> in ISO C");
11497 exp = c_fully_fold (exp, false, NULL);
11498 exp = default_conversion (exp);
11500 if (warn_sequence_point)
11501 verify_sequence_points (exp);
11505 /* Add this new SWITCH_STMT to the stack. */
11506 cs = XNEW (struct c_switch);
11507 cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11508 NULL_TREE, orig_type, NULL_TREE);
11509 cs->orig_type = orig_type;
11510 cs->cases = splay_tree_new (case_compare, NULL, NULL);
11511 cs->bindings = c_get_switch_bindings ();
11512 cs->break_stmt_seen_p = false;
11513 cs->bool_cond_p = bool_cond_p;
11514 cs->next = c_switch_stack;
11515 c_switch_stack = cs;
11517 return add_stmt (cs->switch_stmt);
11520 /* Process a case label at location LOC, with attributes ATTRS. */
11522 tree
11523 do_case (location_t loc, tree low_value, tree high_value, tree attrs)
11525 tree label = NULL_TREE;
11527 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11529 low_value = c_fully_fold (low_value, false, NULL);
11530 if (TREE_CODE (low_value) == INTEGER_CST)
11531 pedwarn (loc, OPT_Wpedantic,
11532 "case label is not an integer constant expression");
11535 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11537 high_value = c_fully_fold (high_value, false, NULL);
11538 if (TREE_CODE (high_value) == INTEGER_CST)
11539 pedwarn (input_location, OPT_Wpedantic,
11540 "case label is not an integer constant expression");
11543 if (c_switch_stack == NULL)
11545 if (low_value)
11546 error_at (loc, "case label not within a switch statement");
11547 else
11548 error_at (loc, "%<default%> label not within a switch statement");
11549 return NULL_TREE;
11552 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11553 EXPR_LOCATION (c_switch_stack->switch_stmt),
11554 loc))
11555 return NULL_TREE;
11557 label = c_add_case_label (loc, c_switch_stack->cases,
11558 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11559 low_value, high_value, attrs);
11560 if (label == error_mark_node)
11561 label = NULL_TREE;
11562 return label;
11565 /* Finish the switch statement. TYPE is the original type of the
11566 controlling expression of the switch, or NULL_TREE. */
11568 void
11569 c_finish_switch (tree body, tree type)
11571 struct c_switch *cs = c_switch_stack;
11572 location_t switch_location;
11574 SWITCH_STMT_BODY (cs->switch_stmt) = body;
11576 /* Emit warnings as needed. */
11577 switch_location = EXPR_LOCATION (cs->switch_stmt);
11578 c_do_switch_warnings (cs->cases, switch_location,
11579 type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11580 SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11581 if (c_switch_covers_all_cases_p (cs->cases,
11582 SWITCH_STMT_TYPE (cs->switch_stmt)))
11583 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11584 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11586 /* Pop the stack. */
11587 c_switch_stack = cs->next;
11588 splay_tree_delete (cs->cases);
11589 c_release_switch_bindings (cs->bindings);
11590 XDELETE (cs);
11593 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
11594 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11595 may be null. */
11597 void
11598 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11599 tree else_block)
11601 tree stmt;
11603 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11604 SET_EXPR_LOCATION (stmt, if_locus);
11605 add_stmt (stmt);
11608 tree
11609 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11611 /* In switch statements break is sometimes stylistically used after
11612 a return statement. This can lead to spurious warnings about
11613 control reaching the end of a non-void function when it is
11614 inlined. Note that we are calling block_may_fallthru with
11615 language specific tree nodes; this works because
11616 block_may_fallthru returns true when given something it does not
11617 understand. */
11618 bool skip = !block_may_fallthru (cur_stmt_list);
11620 if (is_break)
11621 switch (in_statement)
11623 case 0:
11624 error_at (loc, "break statement not within loop or switch");
11625 return NULL_TREE;
11626 case IN_OMP_BLOCK:
11627 error_at (loc, "invalid exit from OpenMP structured block");
11628 return NULL_TREE;
11629 case IN_OMP_FOR:
11630 error_at (loc, "break statement used with OpenMP for loop");
11631 return NULL_TREE;
11632 case IN_ITERATION_STMT:
11633 case IN_OBJC_FOREACH:
11634 break;
11635 default:
11636 gcc_assert (in_statement & IN_SWITCH_STMT);
11637 c_switch_stack->break_stmt_seen_p = true;
11638 break;
11640 else
11641 switch (in_statement & ~IN_SWITCH_STMT)
11643 case 0:
11644 error_at (loc, "continue statement not within a loop");
11645 return NULL_TREE;
11646 case IN_OMP_BLOCK:
11647 error_at (loc, "invalid exit from OpenMP structured block");
11648 return NULL_TREE;
11649 case IN_ITERATION_STMT:
11650 case IN_OMP_FOR:
11651 case IN_OBJC_FOREACH:
11652 break;
11653 default:
11654 gcc_unreachable ();
11657 if (skip)
11658 return NULL_TREE;
11659 else if ((in_statement & IN_OBJC_FOREACH)
11660 && !(is_break && (in_statement & IN_SWITCH_STMT)))
11662 /* The foreach expander produces low-level code using gotos instead
11663 of a structured loop construct. */
11664 gcc_assert (label);
11665 return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11667 return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11670 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11672 static void
11673 emit_side_effect_warnings (location_t loc, tree expr)
11675 maybe_warn_nodiscard (loc, expr);
11676 if (!warn_unused_value)
11677 return;
11678 if (expr == error_mark_node)
11680 else if (!TREE_SIDE_EFFECTS (expr))
11682 if (!VOID_TYPE_P (TREE_TYPE (expr))
11683 && !warning_suppressed_p (expr, OPT_Wunused_value))
11684 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11686 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11688 tree r = expr;
11689 location_t cloc = loc;
11690 while (TREE_CODE (r) == COMPOUND_EXPR)
11692 if (EXPR_HAS_LOCATION (r))
11693 cloc = EXPR_LOCATION (r);
11694 r = TREE_OPERAND (r, 1);
11696 if (!TREE_SIDE_EFFECTS (r)
11697 && !VOID_TYPE_P (TREE_TYPE (r))
11698 && !CONVERT_EXPR_P (r)
11699 && !warning_suppressed_p (r, OPT_Wunused_value)
11700 && !warning_suppressed_p (expr, OPT_Wunused_value))
11701 warning_at (cloc, OPT_Wunused_value,
11702 "right-hand operand of comma expression has no effect");
11704 else
11705 warn_if_unused_value (expr, loc);
11708 /* Process an expression as if it were a complete statement. Emit
11709 diagnostics, but do not call ADD_STMT. LOC is the location of the
11710 statement. */
11712 tree
11713 c_process_expr_stmt (location_t loc, tree expr)
11715 tree exprv;
11717 if (!expr)
11718 return NULL_TREE;
11720 expr = c_fully_fold (expr, false, NULL);
11722 if (warn_sequence_point)
11723 verify_sequence_points (expr);
11725 if (TREE_TYPE (expr) != error_mark_node
11726 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11727 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11728 error_at (loc, "expression statement has incomplete type");
11730 /* If we're not processing a statement expression, warn about unused values.
11731 Warnings for statement expressions will be emitted later, once we figure
11732 out which is the result. */
11733 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11734 && (warn_unused_value || warn_unused_result))
11735 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11737 exprv = expr;
11738 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11739 exprv = TREE_OPERAND (exprv, 1);
11740 while (CONVERT_EXPR_P (exprv))
11741 exprv = TREE_OPERAND (exprv, 0);
11742 if (DECL_P (exprv)
11743 || handled_component_p (exprv)
11744 || TREE_CODE (exprv) == ADDR_EXPR)
11745 mark_exp_read (exprv);
11747 /* If the expression is not of a type to which we cannot assign a line
11748 number, wrap the thing in a no-op NOP_EXPR. */
11749 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11751 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11752 SET_EXPR_LOCATION (expr, loc);
11755 return expr;
11758 /* Emit an expression as a statement. LOC is the location of the
11759 expression. */
11761 tree
11762 c_finish_expr_stmt (location_t loc, tree expr)
11764 if (expr)
11765 return add_stmt (c_process_expr_stmt (loc, expr));
11766 else
11767 return NULL;
11770 /* Do the opposite and emit a statement as an expression. To begin,
11771 create a new binding level and return it. */
11773 tree
11774 c_begin_stmt_expr (void)
11776 tree ret;
11778 /* We must force a BLOCK for this level so that, if it is not expanded
11779 later, there is a way to turn off the entire subtree of blocks that
11780 are contained in it. */
11781 keep_next_level ();
11782 ret = c_begin_compound_stmt (true);
11784 c_bindings_start_stmt_expr (c_switch_stack == NULL
11785 ? NULL
11786 : c_switch_stack->bindings);
11788 /* Mark the current statement list as belonging to a statement list. */
11789 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11791 return ret;
11794 /* LOC is the location of the compound statement to which this body
11795 belongs. */
11797 tree
11798 c_finish_stmt_expr (location_t loc, tree body)
11800 tree last, type, tmp, val;
11801 tree *last_p;
11803 body = c_end_compound_stmt (loc, body, true);
11805 c_bindings_end_stmt_expr (c_switch_stack == NULL
11806 ? NULL
11807 : c_switch_stack->bindings);
11809 /* Locate the last statement in BODY. See c_end_compound_stmt
11810 about always returning a BIND_EXPR. */
11811 last_p = &BIND_EXPR_BODY (body);
11812 last = BIND_EXPR_BODY (body);
11814 continue_searching:
11815 if (TREE_CODE (last) == STATEMENT_LIST)
11817 tree_stmt_iterator l = tsi_last (last);
11819 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11820 tsi_prev (&l);
11822 /* This can happen with degenerate cases like ({ }). No value. */
11823 if (tsi_end_p (l))
11824 return body;
11826 /* If we're supposed to generate side effects warnings, process
11827 all of the statements except the last. */
11828 if (warn_unused_value || warn_unused_result)
11830 for (tree_stmt_iterator i = tsi_start (last);
11831 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11833 location_t tloc;
11834 tree t = tsi_stmt (i);
11836 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11837 emit_side_effect_warnings (tloc, t);
11840 last_p = tsi_stmt_ptr (l);
11841 last = *last_p;
11844 /* If the end of the list is exception related, then the list was split
11845 by a call to push_cleanup. Continue searching. */
11846 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11847 || TREE_CODE (last) == TRY_CATCH_EXPR)
11849 last_p = &TREE_OPERAND (last, 0);
11850 last = *last_p;
11851 goto continue_searching;
11854 if (last == error_mark_node)
11855 return last;
11857 /* In the case that the BIND_EXPR is not necessary, return the
11858 expression out from inside it. */
11859 if ((last == BIND_EXPR_BODY (body)
11860 /* Skip nested debug stmts. */
11861 || last == expr_first (BIND_EXPR_BODY (body)))
11862 && BIND_EXPR_VARS (body) == NULL)
11864 /* Even if this looks constant, do not allow it in a constant
11865 expression. */
11866 last = c_wrap_maybe_const (last, true);
11867 /* Do not warn if the return value of a statement expression is
11868 unused. */
11869 suppress_warning (last, OPT_Wunused);
11870 return last;
11873 /* Extract the type of said expression. */
11874 type = TREE_TYPE (last);
11876 /* If we're not returning a value at all, then the BIND_EXPR that
11877 we already have is a fine expression to return. */
11878 if (!type || VOID_TYPE_P (type))
11879 return body;
11881 /* Now that we've located the expression containing the value, it seems
11882 silly to make voidify_wrapper_expr repeat the process. Create a
11883 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11884 tmp = create_tmp_var_raw (type);
11886 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11887 tree_expr_nonnegative_p giving up immediately. */
11888 val = last;
11889 if (TREE_CODE (val) == NOP_EXPR
11890 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11891 val = TREE_OPERAND (val, 0);
11893 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11894 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11897 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11898 SET_EXPR_LOCATION (t, loc);
11899 return t;
11903 /* Begin and end compound statements. This is as simple as pushing
11904 and popping new statement lists from the tree. */
11906 tree
11907 c_begin_compound_stmt (bool do_scope)
11909 tree stmt = push_stmt_list ();
11910 if (do_scope)
11911 push_scope ();
11912 return stmt;
11915 /* End a compound statement. STMT is the statement. LOC is the
11916 location of the compound statement-- this is usually the location
11917 of the opening brace. */
11919 tree
11920 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11922 tree block = NULL;
11924 if (do_scope)
11926 if (c_dialect_objc ())
11927 objc_clear_super_receiver ();
11928 block = pop_scope ();
11931 stmt = pop_stmt_list (stmt);
11932 stmt = c_build_bind_expr (loc, block, stmt);
11934 /* If this compound statement is nested immediately inside a statement
11935 expression, then force a BIND_EXPR to be created. Otherwise we'll
11936 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11937 STATEMENT_LISTs merge, and thus we can lose track of what statement
11938 was really last. */
11939 if (building_stmt_list_p ()
11940 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11941 && TREE_CODE (stmt) != BIND_EXPR)
11943 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11944 TREE_SIDE_EFFECTS (stmt) = 1;
11945 SET_EXPR_LOCATION (stmt, loc);
11948 return stmt;
11951 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11952 when the current scope is exited. EH_ONLY is true when this is not
11953 meant to apply to normal control flow transfer. */
11955 void
11956 push_cleanup (tree decl, tree cleanup, bool eh_only)
11958 enum tree_code code;
11959 tree stmt, list;
11960 bool stmt_expr;
11962 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11963 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11964 add_stmt (stmt);
11965 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11966 list = push_stmt_list ();
11967 TREE_OPERAND (stmt, 0) = list;
11968 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11971 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11972 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11974 static tree
11975 build_vec_cmp (tree_code code, tree type,
11976 tree arg0, tree arg1)
11978 tree zero_vec = build_zero_cst (type);
11979 tree minus_one_vec = build_minus_one_cst (type);
11980 tree cmp_type = truth_type_for (type);
11981 tree cmp = build2 (code, cmp_type, arg0, arg1);
11982 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11985 /* Possibly warn about an address of OP never being NULL in a comparison
11986 operation CODE involving null. */
11988 static void
11989 maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
11991 /* Prevent warnings issued for macro expansion. */
11992 if (!warn_address
11993 || warning_suppressed_p (op, OPT_Waddress)
11994 || from_macro_expansion_at (loc))
11995 return;
11997 if (TREE_CODE (op) == NOP_EXPR)
11999 /* Allow casts to intptr_t to suppress the warning. */
12000 tree type = TREE_TYPE (op);
12001 if (TREE_CODE (type) == INTEGER_TYPE)
12002 return;
12003 op = TREE_OPERAND (op, 0);
12006 if (TREE_CODE (op) == POINTER_PLUS_EXPR)
12008 /* Allow a cast to void* to suppress the warning. */
12009 tree type = TREE_TYPE (TREE_TYPE (op));
12010 if (VOID_TYPE_P (type))
12011 return;
12013 /* Adding any value to a null pointer, including zero, is undefined
12014 in C. This includes the expression &p[0] where p is the null
12015 pointer, although &p[0] will have been folded to p by this point
12016 and so not diagnosed. */
12017 if (code == EQ_EXPR)
12018 warning_at (loc, OPT_Waddress,
12019 "the comparison will always evaluate as %<false%> "
12020 "for the pointer operand in %qE must not be NULL",
12021 op);
12022 else
12023 warning_at (loc, OPT_Waddress,
12024 "the comparison will always evaluate as %<true%> "
12025 "for the pointer operand in %qE must not be NULL",
12026 op);
12028 return;
12031 if (TREE_CODE (op) != ADDR_EXPR)
12032 return;
12034 op = TREE_OPERAND (op, 0);
12036 if (TREE_CODE (op) == IMAGPART_EXPR
12037 || TREE_CODE (op) == REALPART_EXPR)
12039 /* The address of either complex part may not be null. */
12040 if (code == EQ_EXPR)
12041 warning_at (loc, OPT_Waddress,
12042 "the comparison will always evaluate as %<false%> "
12043 "for the address of %qE will never be NULL",
12044 op);
12045 else
12046 warning_at (loc, OPT_Waddress,
12047 "the comparison will always evaluate as %<true%> "
12048 "for the address of %qE will never be NULL",
12049 op);
12050 return;
12053 /* Set to true in the loop below if OP dereferences is operand.
12054 In such a case the ultimate target need not be a decl for
12055 the null [in]equality test to be constant. */
12056 bool deref = false;
12058 /* Get the outermost array or object, or member. */
12059 while (handled_component_p (op))
12061 if (TREE_CODE (op) == COMPONENT_REF)
12063 /* Get the member (its address is never null). */
12064 op = TREE_OPERAND (op, 1);
12065 break;
12068 /* Get the outer array/object to refer to in the warning. */
12069 op = TREE_OPERAND (op, 0);
12070 deref = true;
12073 if ((!deref && !decl_with_nonnull_addr_p (op))
12074 || from_macro_expansion_at (loc))
12075 return;
12077 bool w;
12078 if (code == EQ_EXPR)
12079 w = warning_at (loc, OPT_Waddress,
12080 "the comparison will always evaluate as %<false%> "
12081 "for the address of %qE will never be NULL",
12082 op);
12083 else
12084 w = warning_at (loc, OPT_Waddress,
12085 "the comparison will always evaluate as %<true%> "
12086 "for the address of %qE will never be NULL",
12087 op);
12089 if (w && DECL_P (op))
12090 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
12093 /* Build a binary-operation expression without default conversions.
12094 CODE is the kind of expression to build.
12095 LOCATION is the operator's location.
12096 This function differs from `build' in several ways:
12097 the data type of the result is computed and recorded in it,
12098 warnings are generated if arg data types are invalid,
12099 special handling for addition and subtraction of pointers is known,
12100 and some optimization is done (operations on narrow ints
12101 are done in the narrower type when that gives the same result).
12102 Constant folding is also done before the result is returned.
12104 Note that the operands will never have enumeral types, or function
12105 or array types, because either they will have the default conversions
12106 performed or they have both just been converted to some other type in which
12107 the arithmetic is to be done. */
12109 tree
12110 build_binary_op (location_t location, enum tree_code code,
12111 tree orig_op0, tree orig_op1, bool convert_p)
12113 tree type0, type1, orig_type0, orig_type1;
12114 tree eptype;
12115 enum tree_code code0, code1;
12116 tree op0, op1;
12117 tree ret = error_mark_node;
12118 const char *invalid_op_diag;
12119 bool op0_int_operands, op1_int_operands;
12120 bool int_const, int_const_or_overflow, int_operands;
12122 /* Expression code to give to the expression when it is built.
12123 Normally this is CODE, which is what the caller asked for,
12124 but in some special cases we change it. */
12125 enum tree_code resultcode = code;
12127 /* Data type in which the computation is to be performed.
12128 In the simplest cases this is the common type of the arguments. */
12129 tree result_type = NULL;
12131 /* When the computation is in excess precision, the type of the
12132 final EXCESS_PRECISION_EXPR. */
12133 tree semantic_result_type = NULL;
12135 /* Nonzero means operands have already been type-converted
12136 in whatever way is necessary.
12137 Zero means they need to be converted to RESULT_TYPE. */
12138 int converted = 0;
12140 /* Nonzero means create the expression with this type, rather than
12141 RESULT_TYPE. */
12142 tree build_type = NULL_TREE;
12144 /* Nonzero means after finally constructing the expression
12145 convert it to this type. */
12146 tree final_type = NULL_TREE;
12148 /* Nonzero if this is an operation like MIN or MAX which can
12149 safely be computed in short if both args are promoted shorts.
12150 Also implies COMMON.
12151 -1 indicates a bitwise operation; this makes a difference
12152 in the exact conditions for when it is safe to do the operation
12153 in a narrower mode. */
12154 int shorten = 0;
12156 /* Nonzero if this is a comparison operation;
12157 if both args are promoted shorts, compare the original shorts.
12158 Also implies COMMON. */
12159 int short_compare = 0;
12161 /* Nonzero if this is a right-shift operation, which can be computed on the
12162 original short and then promoted if the operand is a promoted short. */
12163 int short_shift = 0;
12165 /* Nonzero means set RESULT_TYPE to the common type of the args. */
12166 int common = 0;
12168 /* True means types are compatible as far as ObjC is concerned. */
12169 bool objc_ok;
12171 /* True means this is an arithmetic operation that may need excess
12172 precision. */
12173 bool may_need_excess_precision;
12175 /* True means this is a boolean operation that converts both its
12176 operands to truth-values. */
12177 bool boolean_op = false;
12179 /* Remember whether we're doing / or %. */
12180 bool doing_div_or_mod = false;
12182 /* Remember whether we're doing << or >>. */
12183 bool doing_shift = false;
12185 /* Tree holding instrumentation expression. */
12186 tree instrument_expr = NULL;
12188 if (location == UNKNOWN_LOCATION)
12189 location = input_location;
12191 op0 = orig_op0;
12192 op1 = orig_op1;
12194 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
12195 if (op0_int_operands)
12196 op0 = remove_c_maybe_const_expr (op0);
12197 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
12198 if (op1_int_operands)
12199 op1 = remove_c_maybe_const_expr (op1);
12200 int_operands = (op0_int_operands && op1_int_operands);
12201 if (int_operands)
12203 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
12204 && TREE_CODE (orig_op1) == INTEGER_CST);
12205 int_const = (int_const_or_overflow
12206 && !TREE_OVERFLOW (orig_op0)
12207 && !TREE_OVERFLOW (orig_op1));
12209 else
12210 int_const = int_const_or_overflow = false;
12212 /* Do not apply default conversion in mixed vector/scalar expression. */
12213 if (convert_p
12214 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
12216 op0 = default_conversion (op0);
12217 op1 = default_conversion (op1);
12220 orig_type0 = type0 = TREE_TYPE (op0);
12222 orig_type1 = type1 = TREE_TYPE (op1);
12224 /* The expression codes of the data types of the arguments tell us
12225 whether the arguments are integers, floating, pointers, etc. */
12226 code0 = TREE_CODE (type0);
12227 code1 = TREE_CODE (type1);
12229 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
12230 STRIP_TYPE_NOPS (op0);
12231 STRIP_TYPE_NOPS (op1);
12233 /* If an error was already reported for one of the arguments,
12234 avoid reporting another error. */
12236 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12237 return error_mark_node;
12239 if (code0 == POINTER_TYPE
12240 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
12241 return error_mark_node;
12243 if (code1 == POINTER_TYPE
12244 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
12245 return error_mark_node;
12247 if ((invalid_op_diag
12248 = targetm.invalid_binary_op (code, type0, type1)))
12250 error_at (location, invalid_op_diag);
12251 return error_mark_node;
12254 switch (code)
12256 case PLUS_EXPR:
12257 case MINUS_EXPR:
12258 case MULT_EXPR:
12259 case TRUNC_DIV_EXPR:
12260 case CEIL_DIV_EXPR:
12261 case FLOOR_DIV_EXPR:
12262 case ROUND_DIV_EXPR:
12263 case EXACT_DIV_EXPR:
12264 may_need_excess_precision = true;
12265 break;
12267 case EQ_EXPR:
12268 case NE_EXPR:
12269 case LE_EXPR:
12270 case GE_EXPR:
12271 case LT_EXPR:
12272 case GT_EXPR:
12273 /* Excess precision for implicit conversions of integers to
12274 floating point in C11 and later. */
12275 may_need_excess_precision = (flag_isoc11
12276 && (ANY_INTEGRAL_TYPE_P (type0)
12277 || ANY_INTEGRAL_TYPE_P (type1)));
12278 break;
12280 default:
12281 may_need_excess_precision = false;
12282 break;
12284 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
12286 op0 = TREE_OPERAND (op0, 0);
12287 type0 = TREE_TYPE (op0);
12289 else if (may_need_excess_precision
12290 && (eptype = excess_precision_type (type0)) != NULL_TREE)
12292 type0 = eptype;
12293 op0 = convert (eptype, op0);
12295 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
12297 op1 = TREE_OPERAND (op1, 0);
12298 type1 = TREE_TYPE (op1);
12300 else if (may_need_excess_precision
12301 && (eptype = excess_precision_type (type1)) != NULL_TREE)
12303 type1 = eptype;
12304 op1 = convert (eptype, op1);
12307 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
12309 /* In case when one of the operands of the binary operation is
12310 a vector and another is a scalar -- convert scalar to vector. */
12311 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
12312 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
12314 enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
12315 orig_op1, true);
12317 switch (convert_flag)
12319 case stv_error:
12320 return error_mark_node;
12321 case stv_firstarg:
12323 bool maybe_const = true;
12324 tree sc;
12325 sc = c_fully_fold (op0, false, &maybe_const);
12326 sc = save_expr (sc);
12327 sc = convert (TREE_TYPE (type1), sc);
12328 op0 = build_vector_from_val (type1, sc);
12329 if (!maybe_const)
12330 op0 = c_wrap_maybe_const (op0, true);
12331 orig_type0 = type0 = TREE_TYPE (op0);
12332 code0 = TREE_CODE (type0);
12333 converted = 1;
12334 break;
12336 case stv_secondarg:
12338 bool maybe_const = true;
12339 tree sc;
12340 sc = c_fully_fold (op1, false, &maybe_const);
12341 sc = save_expr (sc);
12342 sc = convert (TREE_TYPE (type0), sc);
12343 op1 = build_vector_from_val (type0, sc);
12344 if (!maybe_const)
12345 op1 = c_wrap_maybe_const (op1, true);
12346 orig_type1 = type1 = TREE_TYPE (op1);
12347 code1 = TREE_CODE (type1);
12348 converted = 1;
12349 break;
12351 default:
12352 break;
12356 switch (code)
12358 case PLUS_EXPR:
12359 /* Handle the pointer + int case. */
12360 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12362 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
12363 goto return_build_binary_op;
12365 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
12367 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
12368 goto return_build_binary_op;
12370 else
12371 common = 1;
12372 break;
12374 case MINUS_EXPR:
12375 /* Subtraction of two similar pointers.
12376 We must subtract them as integers, then divide by object size. */
12377 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
12378 && comp_target_types (location, type0, type1))
12380 ret = pointer_diff (location, op0, op1, &instrument_expr);
12381 goto return_build_binary_op;
12383 /* Handle pointer minus int. Just like pointer plus int. */
12384 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12386 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
12387 goto return_build_binary_op;
12389 else
12390 common = 1;
12391 break;
12393 case MULT_EXPR:
12394 common = 1;
12395 break;
12397 case TRUNC_DIV_EXPR:
12398 case CEIL_DIV_EXPR:
12399 case FLOOR_DIV_EXPR:
12400 case ROUND_DIV_EXPR:
12401 case EXACT_DIV_EXPR:
12402 doing_div_or_mod = true;
12403 warn_for_div_by_zero (location, op1);
12405 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12406 || code0 == FIXED_POINT_TYPE
12407 || code0 == COMPLEX_TYPE
12408 || gnu_vector_type_p (type0))
12409 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12410 || code1 == FIXED_POINT_TYPE
12411 || code1 == COMPLEX_TYPE
12412 || gnu_vector_type_p (type1)))
12414 enum tree_code tcode0 = code0, tcode1 = code1;
12416 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
12417 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
12418 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
12419 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
12421 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
12422 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
12423 resultcode = RDIV_EXPR;
12424 else
12425 /* Although it would be tempting to shorten always here, that
12426 loses on some targets, since the modulo instruction is
12427 undefined if the quotient can't be represented in the
12428 computation mode. We shorten only if unsigned or if
12429 dividing by something we know != -1. */
12430 shorten = may_shorten_divmod (op0, op1);
12431 common = 1;
12433 break;
12435 case BIT_AND_EXPR:
12436 case BIT_IOR_EXPR:
12437 case BIT_XOR_EXPR:
12438 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12439 shorten = -1;
12440 /* Allow vector types which are not floating point types. */
12441 else if (gnu_vector_type_p (type0)
12442 && gnu_vector_type_p (type1)
12443 && !VECTOR_FLOAT_TYPE_P (type0)
12444 && !VECTOR_FLOAT_TYPE_P (type1))
12445 common = 1;
12446 break;
12448 case TRUNC_MOD_EXPR:
12449 case FLOOR_MOD_EXPR:
12450 doing_div_or_mod = true;
12451 warn_for_div_by_zero (location, op1);
12453 if (gnu_vector_type_p (type0)
12454 && gnu_vector_type_p (type1)
12455 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12456 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12457 common = 1;
12458 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12460 /* Although it would be tempting to shorten always here, that loses
12461 on some targets, since the modulo instruction is undefined if the
12462 quotient can't be represented in the computation mode. We shorten
12463 only if unsigned or if dividing by something we know != -1. */
12464 shorten = may_shorten_divmod (op0, op1);
12465 common = 1;
12467 break;
12469 case TRUTH_ANDIF_EXPR:
12470 case TRUTH_ORIF_EXPR:
12471 case TRUTH_AND_EXPR:
12472 case TRUTH_OR_EXPR:
12473 case TRUTH_XOR_EXPR:
12474 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
12475 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12476 || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE)
12477 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
12478 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12479 || code1 == FIXED_POINT_TYPE || code1 == NULLPTR_TYPE))
12481 /* Result of these operations is always an int,
12482 but that does not mean the operands should be
12483 converted to ints! */
12484 result_type = integer_type_node;
12485 if (op0_int_operands)
12487 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
12488 op0 = remove_c_maybe_const_expr (op0);
12490 else
12491 op0 = c_objc_common_truthvalue_conversion (location, op0);
12492 if (op1_int_operands)
12494 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
12495 op1 = remove_c_maybe_const_expr (op1);
12497 else
12498 op1 = c_objc_common_truthvalue_conversion (location, op1);
12499 converted = 1;
12500 boolean_op = true;
12502 if (code == TRUTH_ANDIF_EXPR)
12504 int_const_or_overflow = (int_operands
12505 && TREE_CODE (orig_op0) == INTEGER_CST
12506 && (op0 == truthvalue_false_node
12507 || TREE_CODE (orig_op1) == INTEGER_CST));
12508 int_const = (int_const_or_overflow
12509 && !TREE_OVERFLOW (orig_op0)
12510 && (op0 == truthvalue_false_node
12511 || !TREE_OVERFLOW (orig_op1)));
12513 else if (code == TRUTH_ORIF_EXPR)
12515 int_const_or_overflow = (int_operands
12516 && TREE_CODE (orig_op0) == INTEGER_CST
12517 && (op0 == truthvalue_true_node
12518 || TREE_CODE (orig_op1) == INTEGER_CST));
12519 int_const = (int_const_or_overflow
12520 && !TREE_OVERFLOW (orig_op0)
12521 && (op0 == truthvalue_true_node
12522 || !TREE_OVERFLOW (orig_op1)));
12524 break;
12526 /* Shift operations: result has same type as first operand;
12527 always convert second operand to int.
12528 Also set SHORT_SHIFT if shifting rightward. */
12530 case RSHIFT_EXPR:
12531 if (gnu_vector_type_p (type0)
12532 && gnu_vector_type_p (type1)
12533 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12534 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12535 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12536 TYPE_VECTOR_SUBPARTS (type1)))
12538 result_type = type0;
12539 converted = 1;
12541 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12542 || (gnu_vector_type_p (type0)
12543 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12544 && code1 == INTEGER_TYPE)
12546 doing_shift = true;
12547 if (TREE_CODE (op1) == INTEGER_CST)
12549 if (tree_int_cst_sgn (op1) < 0)
12551 int_const = false;
12552 if (c_inhibit_evaluation_warnings == 0)
12553 warning_at (location, OPT_Wshift_count_negative,
12554 "right shift count is negative");
12556 else if (code0 == VECTOR_TYPE)
12558 if (compare_tree_int (op1,
12559 TYPE_PRECISION (TREE_TYPE (type0)))
12560 >= 0)
12562 int_const = false;
12563 if (c_inhibit_evaluation_warnings == 0)
12564 warning_at (location, OPT_Wshift_count_overflow,
12565 "right shift count >= width of vector element");
12568 else
12570 if (!integer_zerop (op1))
12571 short_shift = 1;
12573 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12575 int_const = false;
12576 if (c_inhibit_evaluation_warnings == 0)
12577 warning_at (location, OPT_Wshift_count_overflow,
12578 "right shift count >= width of type");
12583 /* Use the type of the value to be shifted. */
12584 result_type = type0;
12585 /* Avoid converting op1 to result_type later. */
12586 converted = 1;
12588 break;
12590 case LSHIFT_EXPR:
12591 if (gnu_vector_type_p (type0)
12592 && gnu_vector_type_p (type1)
12593 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12594 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12595 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12596 TYPE_VECTOR_SUBPARTS (type1)))
12598 result_type = type0;
12599 converted = 1;
12601 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12602 || (gnu_vector_type_p (type0)
12603 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12604 && code1 == INTEGER_TYPE)
12606 doing_shift = true;
12607 if (TREE_CODE (op0) == INTEGER_CST
12608 && tree_int_cst_sgn (op0) < 0
12609 && !TYPE_OVERFLOW_WRAPS (type0))
12611 /* Don't reject a left shift of a negative value in a context
12612 where a constant expression is needed in C90. */
12613 if (flag_isoc99)
12614 int_const = false;
12615 if (c_inhibit_evaluation_warnings == 0)
12616 warning_at (location, OPT_Wshift_negative_value,
12617 "left shift of negative value");
12619 if (TREE_CODE (op1) == INTEGER_CST)
12621 if (tree_int_cst_sgn (op1) < 0)
12623 int_const = false;
12624 if (c_inhibit_evaluation_warnings == 0)
12625 warning_at (location, OPT_Wshift_count_negative,
12626 "left shift count is negative");
12628 else if (code0 == VECTOR_TYPE)
12630 if (compare_tree_int (op1,
12631 TYPE_PRECISION (TREE_TYPE (type0)))
12632 >= 0)
12634 int_const = false;
12635 if (c_inhibit_evaluation_warnings == 0)
12636 warning_at (location, OPT_Wshift_count_overflow,
12637 "left shift count >= width of vector element");
12640 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12642 int_const = false;
12643 if (c_inhibit_evaluation_warnings == 0)
12644 warning_at (location, OPT_Wshift_count_overflow,
12645 "left shift count >= width of type");
12647 else if (TREE_CODE (op0) == INTEGER_CST
12648 && maybe_warn_shift_overflow (location, op0, op1)
12649 && flag_isoc99)
12650 int_const = false;
12653 /* Use the type of the value to be shifted. */
12654 result_type = type0;
12655 /* Avoid converting op1 to result_type later. */
12656 converted = 1;
12658 break;
12660 case EQ_EXPR:
12661 case NE_EXPR:
12662 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12664 tree intt;
12665 if (!vector_types_compatible_elements_p (type0, type1))
12667 error_at (location, "comparing vectors with different "
12668 "element types");
12669 return error_mark_node;
12672 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12673 TYPE_VECTOR_SUBPARTS (type1)))
12675 error_at (location, "comparing vectors with different "
12676 "number of elements");
12677 return error_mark_node;
12680 /* It's not precisely specified how the usual arithmetic
12681 conversions apply to the vector types. Here, we use
12682 the unsigned type if one of the operands is signed and
12683 the other one is unsigned. */
12684 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12686 if (!TYPE_UNSIGNED (type0))
12687 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12688 else
12689 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12690 warning_at (location, OPT_Wsign_compare, "comparison between "
12691 "types %qT and %qT", type0, type1);
12694 /* Always construct signed integer vector type. */
12695 intt = c_common_type_for_size (GET_MODE_BITSIZE
12696 (SCALAR_TYPE_MODE
12697 (TREE_TYPE (type0))), 0);
12698 if (!intt)
12700 error_at (location, "could not find an integer type "
12701 "of the same size as %qT",
12702 TREE_TYPE (type0));
12703 return error_mark_node;
12705 result_type = build_opaque_vector_type (intt,
12706 TYPE_VECTOR_SUBPARTS (type0));
12707 converted = 1;
12708 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12709 goto return_build_binary_op;
12711 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12712 warning_at (location,
12713 OPT_Wfloat_equal,
12714 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12715 /* Result of comparison is always int,
12716 but don't convert the args to int! */
12717 build_type = integer_type_node;
12718 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12719 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12720 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12721 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12722 short_compare = 1;
12723 else if (code0 == POINTER_TYPE
12724 && (code1 == NULLPTR_TYPE
12725 || null_pointer_constant_p (orig_op1)))
12727 maybe_warn_for_null_address (location, op0, code);
12728 result_type = type0;
12730 else if (code1 == POINTER_TYPE
12731 && (code0 == NULLPTR_TYPE
12732 || null_pointer_constant_p (orig_op0)))
12734 maybe_warn_for_null_address (location, op1, code);
12735 result_type = type1;
12737 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12739 tree tt0 = TREE_TYPE (type0);
12740 tree tt1 = TREE_TYPE (type1);
12741 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12742 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12743 addr_space_t as_common = ADDR_SPACE_GENERIC;
12745 /* Anything compares with void *. void * compares with anything.
12746 Otherwise, the targets must be compatible
12747 and both must be object or both incomplete. */
12748 if (comp_target_types (location, type0, type1))
12749 result_type = common_pointer_type (type0, type1);
12750 else if (!addr_space_superset (as0, as1, &as_common))
12752 error_at (location, "comparison of pointers to "
12753 "disjoint address spaces");
12754 return error_mark_node;
12756 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12758 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12759 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12760 "comparison of %<void *%> with function pointer");
12762 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12764 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12765 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12766 "comparison of %<void *%> with function pointer");
12768 else
12769 /* Avoid warning about the volatile ObjC EH puts on decls. */
12770 if (!objc_ok)
12771 pedwarn (location, 0,
12772 "comparison of distinct pointer types lacks a cast");
12774 if (result_type == NULL_TREE)
12776 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12777 result_type = build_pointer_type
12778 (build_qualified_type (void_type_node, qual));
12781 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12783 result_type = type0;
12784 pedwarn (location, 0, "comparison between pointer and integer");
12786 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12788 result_type = type1;
12789 pedwarn (location, 0, "comparison between pointer and integer");
12791 /* 6.5.9: One of the following shall hold:
12792 -- both operands have type nullptr_t; */
12793 else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE)
12795 result_type = nullptr_type_node;
12796 /* No need to convert the operands to result_type later. */
12797 converted = 1;
12799 /* -- one operand has type nullptr_t and the other is a null pointer
12800 constant. We will have to convert the former to the type of the
12801 latter, because during gimplification we can't have mismatching
12802 comparison operand type. We convert from nullptr_t to the other
12803 type, since only nullptr_t can be converted to nullptr_t. Also,
12804 even a constant 0 is a null pointer constant, so we may have to
12805 create a pointer type from its type. */
12806 else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1))
12807 result_type = (INTEGRAL_TYPE_P (type1)
12808 ? build_pointer_type (type1) : type1);
12809 else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0))
12810 result_type = (INTEGRAL_TYPE_P (type0)
12811 ? build_pointer_type (type0) : type0);
12812 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
12813 || truth_value_p (TREE_CODE (orig_op0)))
12814 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
12815 || truth_value_p (TREE_CODE (orig_op1))))
12816 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12817 break;
12819 case LE_EXPR:
12820 case GE_EXPR:
12821 case LT_EXPR:
12822 case GT_EXPR:
12823 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12825 tree intt;
12826 if (!vector_types_compatible_elements_p (type0, type1))
12828 error_at (location, "comparing vectors with different "
12829 "element types");
12830 return error_mark_node;
12833 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12834 TYPE_VECTOR_SUBPARTS (type1)))
12836 error_at (location, "comparing vectors with different "
12837 "number of elements");
12838 return error_mark_node;
12841 /* It's not precisely specified how the usual arithmetic
12842 conversions apply to the vector types. Here, we use
12843 the unsigned type if one of the operands is signed and
12844 the other one is unsigned. */
12845 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12847 if (!TYPE_UNSIGNED (type0))
12848 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12849 else
12850 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12851 warning_at (location, OPT_Wsign_compare, "comparison between "
12852 "types %qT and %qT", type0, type1);
12855 /* Always construct signed integer vector type. */
12856 intt = c_common_type_for_size (GET_MODE_BITSIZE
12857 (SCALAR_TYPE_MODE
12858 (TREE_TYPE (type0))), 0);
12859 if (!intt)
12861 error_at (location, "could not find an integer type "
12862 "of the same size as %qT",
12863 TREE_TYPE (type0));
12864 return error_mark_node;
12866 result_type = build_opaque_vector_type (intt,
12867 TYPE_VECTOR_SUBPARTS (type0));
12868 converted = 1;
12869 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12870 goto return_build_binary_op;
12872 build_type = integer_type_node;
12873 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12874 || code0 == FIXED_POINT_TYPE)
12875 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12876 || code1 == FIXED_POINT_TYPE))
12877 short_compare = 1;
12878 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12880 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12881 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12882 addr_space_t as_common;
12884 if (comp_target_types (location, type0, type1))
12886 result_type = common_pointer_type (type0, type1);
12887 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12888 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12889 pedwarn_c99 (location, OPT_Wpedantic,
12890 "comparison of complete and incomplete pointers");
12891 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12892 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12893 "ordered comparisons of pointers to functions");
12894 else if (null_pointer_constant_p (orig_op0)
12895 || null_pointer_constant_p (orig_op1))
12896 warning_at (location, OPT_Wextra,
12897 "ordered comparison of pointer with null pointer");
12900 else if (!addr_space_superset (as0, as1, &as_common))
12902 error_at (location, "comparison of pointers to "
12903 "disjoint address spaces");
12904 return error_mark_node;
12906 else
12908 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12909 result_type = build_pointer_type
12910 (build_qualified_type (void_type_node, qual));
12911 pedwarn (location, 0,
12912 "comparison of distinct pointer types lacks a cast");
12915 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12917 result_type = type0;
12918 if (pedantic)
12919 pedwarn (location, OPT_Wpedantic,
12920 "ordered comparison of pointer with integer zero");
12921 else if (extra_warnings)
12922 warning_at (location, OPT_Wextra,
12923 "ordered comparison of pointer with integer zero");
12925 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12927 result_type = type1;
12928 if (pedantic)
12929 pedwarn (location, OPT_Wpedantic,
12930 "ordered comparison of pointer with integer zero");
12931 else if (extra_warnings)
12932 warning_at (location, OPT_Wextra,
12933 "ordered comparison of pointer with integer zero");
12935 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12937 result_type = type0;
12938 pedwarn (location, 0, "comparison between pointer and integer");
12940 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12942 result_type = type1;
12943 pedwarn (location, 0, "comparison between pointer and integer");
12946 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12947 && current_function_decl != NULL_TREE
12948 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12950 op0 = save_expr (op0);
12951 op1 = save_expr (op1);
12953 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12954 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12957 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
12958 || truth_value_p (TREE_CODE (orig_op0)))
12959 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
12960 || truth_value_p (TREE_CODE (orig_op1))))
12961 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12962 break;
12964 case MIN_EXPR:
12965 case MAX_EXPR:
12966 /* Used for OpenMP atomics. */
12967 gcc_assert (flag_openmp);
12968 common = 1;
12969 break;
12971 default:
12972 gcc_unreachable ();
12975 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12976 return error_mark_node;
12978 if (gnu_vector_type_p (type0)
12979 && gnu_vector_type_p (type1)
12980 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12981 || !vector_types_compatible_elements_p (type0, type1)))
12983 gcc_rich_location richloc (location);
12984 maybe_range_label_for_tree_type_mismatch
12985 label_for_op0 (orig_op0, orig_op1),
12986 label_for_op1 (orig_op1, orig_op0);
12987 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12988 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12989 binary_op_error (&richloc, code, type0, type1);
12990 return error_mark_node;
12993 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12994 || code0 == FIXED_POINT_TYPE
12995 || gnu_vector_type_p (type0))
12997 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12998 || code1 == FIXED_POINT_TYPE
12999 || gnu_vector_type_p (type1)))
13001 bool first_complex = (code0 == COMPLEX_TYPE);
13002 bool second_complex = (code1 == COMPLEX_TYPE);
13003 int none_complex = (!first_complex && !second_complex);
13005 if (shorten || common || short_compare)
13007 result_type = c_common_type (type0, type1);
13008 do_warn_double_promotion (result_type, type0, type1,
13009 "implicit conversion from %qT to %qT "
13010 "to match other operand of binary "
13011 "expression",
13012 location);
13013 if (result_type == error_mark_node)
13014 return error_mark_node;
13017 if (first_complex != second_complex
13018 && (code == PLUS_EXPR
13019 || code == MINUS_EXPR
13020 || code == MULT_EXPR
13021 || (code == TRUNC_DIV_EXPR && first_complex))
13022 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
13023 && flag_signed_zeros)
13025 /* An operation on mixed real/complex operands must be
13026 handled specially, but the language-independent code can
13027 more easily optimize the plain complex arithmetic if
13028 -fno-signed-zeros. */
13029 tree real_type = TREE_TYPE (result_type);
13030 tree real, imag;
13031 if (type0 != orig_type0 || type1 != orig_type1)
13033 gcc_assert (may_need_excess_precision && common);
13034 semantic_result_type = c_common_type (orig_type0, orig_type1);
13036 if (first_complex)
13038 if (TREE_TYPE (op0) != result_type)
13039 op0 = convert_and_check (location, result_type, op0);
13040 if (TREE_TYPE (op1) != real_type)
13041 op1 = convert_and_check (location, real_type, op1);
13043 else
13045 if (TREE_TYPE (op0) != real_type)
13046 op0 = convert_and_check (location, real_type, op0);
13047 if (TREE_TYPE (op1) != result_type)
13048 op1 = convert_and_check (location, result_type, op1);
13050 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
13051 return error_mark_node;
13052 if (first_complex)
13054 op0 = save_expr (op0);
13055 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
13056 op0, true);
13057 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
13058 op0, true);
13059 switch (code)
13061 case MULT_EXPR:
13062 case TRUNC_DIV_EXPR:
13063 op1 = save_expr (op1);
13064 imag = build2 (resultcode, real_type, imag, op1);
13065 /* Fall through. */
13066 case PLUS_EXPR:
13067 case MINUS_EXPR:
13068 real = build2 (resultcode, real_type, real, op1);
13069 break;
13070 default:
13071 gcc_unreachable();
13074 else
13076 op1 = save_expr (op1);
13077 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
13078 op1, true);
13079 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
13080 op1, true);
13081 switch (code)
13083 case MULT_EXPR:
13084 op0 = save_expr (op0);
13085 imag = build2 (resultcode, real_type, op0, imag);
13086 /* Fall through. */
13087 case PLUS_EXPR:
13088 real = build2 (resultcode, real_type, op0, real);
13089 break;
13090 case MINUS_EXPR:
13091 real = build2 (resultcode, real_type, op0, real);
13092 imag = build1 (NEGATE_EXPR, real_type, imag);
13093 break;
13094 default:
13095 gcc_unreachable();
13098 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
13099 goto return_build_binary_op;
13102 /* For certain operations (which identify themselves by shorten != 0)
13103 if both args were extended from the same smaller type,
13104 do the arithmetic in that type and then extend.
13106 shorten !=0 and !=1 indicates a bitwise operation.
13107 For them, this optimization is safe only if
13108 both args are zero-extended or both are sign-extended.
13109 Otherwise, we might change the result.
13110 Eg, (short)-1 | (unsigned short)-1 is (int)-1
13111 but calculated in (unsigned short) it would be (unsigned short)-1. */
13113 if (shorten && none_complex)
13115 final_type = result_type;
13116 result_type = shorten_binary_op (result_type, op0, op1,
13117 shorten == -1);
13120 /* Shifts can be shortened if shifting right. */
13122 if (short_shift)
13124 int unsigned_arg;
13125 tree arg0 = get_narrower (op0, &unsigned_arg);
13127 final_type = result_type;
13129 if (arg0 == op0 && final_type == TREE_TYPE (op0))
13130 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
13132 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
13133 && tree_int_cst_sgn (op1) > 0
13134 /* We can shorten only if the shift count is less than the
13135 number of bits in the smaller type size. */
13136 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
13137 /* We cannot drop an unsigned shift after sign-extension. */
13138 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
13140 /* Do an unsigned shift if the operand was zero-extended. */
13141 result_type
13142 = c_common_signed_or_unsigned_type (unsigned_arg,
13143 TREE_TYPE (arg0));
13144 /* Convert value-to-be-shifted to that type. */
13145 if (TREE_TYPE (op0) != result_type)
13146 op0 = convert (result_type, op0);
13147 converted = 1;
13151 /* Comparison operations are shortened too but differently.
13152 They identify themselves by setting short_compare = 1. */
13154 if (short_compare)
13156 /* Don't write &op0, etc., because that would prevent op0
13157 from being kept in a register.
13158 Instead, make copies of the our local variables and
13159 pass the copies by reference, then copy them back afterward. */
13160 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
13161 enum tree_code xresultcode = resultcode;
13162 tree val
13163 = shorten_compare (location, &xop0, &xop1, &xresult_type,
13164 &xresultcode);
13166 if (val != NULL_TREE)
13168 ret = val;
13169 goto return_build_binary_op;
13172 op0 = xop0, op1 = xop1;
13173 converted = 1;
13174 resultcode = xresultcode;
13176 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
13178 bool op0_maybe_const = true;
13179 bool op1_maybe_const = true;
13180 tree orig_op0_folded, orig_op1_folded;
13182 if (in_late_binary_op)
13184 orig_op0_folded = orig_op0;
13185 orig_op1_folded = orig_op1;
13187 else
13189 /* Fold for the sake of possible warnings, as in
13190 build_conditional_expr. This requires the
13191 "original" values to be folded, not just op0 and
13192 op1. */
13193 c_inhibit_evaluation_warnings++;
13194 op0 = c_fully_fold (op0, require_constant_value,
13195 &op0_maybe_const);
13196 op1 = c_fully_fold (op1, require_constant_value,
13197 &op1_maybe_const);
13198 c_inhibit_evaluation_warnings--;
13199 orig_op0_folded = c_fully_fold (orig_op0,
13200 require_constant_value,
13201 NULL);
13202 orig_op1_folded = c_fully_fold (orig_op1,
13203 require_constant_value,
13204 NULL);
13207 if (warn_sign_compare)
13208 warn_for_sign_compare (location, orig_op0_folded,
13209 orig_op1_folded, op0, op1,
13210 result_type, resultcode);
13211 if (!in_late_binary_op && !int_operands)
13213 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
13214 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
13215 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
13216 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
13222 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
13223 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
13224 Then the expression will be built.
13225 It will be given type FINAL_TYPE if that is nonzero;
13226 otherwise, it will be given type RESULT_TYPE. */
13228 if (!result_type)
13230 /* Favor showing any expression locations that are available. */
13231 op_location_t oploc (location, UNKNOWN_LOCATION);
13232 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
13233 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
13234 return error_mark_node;
13237 if (build_type == NULL_TREE)
13239 build_type = result_type;
13240 if ((type0 != orig_type0 || type1 != orig_type1)
13241 && !boolean_op)
13243 gcc_assert (may_need_excess_precision && common);
13244 semantic_result_type = c_common_type (orig_type0, orig_type1);
13248 if (!converted)
13250 op0 = ep_convert_and_check (location, result_type, op0,
13251 semantic_result_type);
13252 op1 = ep_convert_and_check (location, result_type, op1,
13253 semantic_result_type);
13255 /* This can happen if one operand has a vector type, and the other
13256 has a different type. */
13257 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
13258 return error_mark_node;
13261 if (sanitize_flags_p ((SANITIZE_SHIFT
13262 | SANITIZE_DIVIDE
13263 | SANITIZE_FLOAT_DIVIDE
13264 | SANITIZE_SI_OVERFLOW))
13265 && current_function_decl != NULL_TREE
13266 && (doing_div_or_mod || doing_shift)
13267 && !require_constant_value)
13269 /* OP0 and/or OP1 might have side-effects. */
13270 op0 = save_expr (op0);
13271 op1 = save_expr (op1);
13272 op0 = c_fully_fold (op0, false, NULL);
13273 op1 = c_fully_fold (op1, false, NULL);
13274 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
13275 | SANITIZE_FLOAT_DIVIDE
13276 | SANITIZE_SI_OVERFLOW))))
13277 instrument_expr = ubsan_instrument_division (location, op0, op1);
13278 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
13279 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
13282 /* Treat expressions in initializers specially as they can't trap. */
13283 if (int_const_or_overflow)
13284 ret = (require_constant_value
13285 ? fold_build2_initializer_loc (location, resultcode, build_type,
13286 op0, op1)
13287 : fold_build2_loc (location, resultcode, build_type, op0, op1));
13288 else
13289 ret = build2 (resultcode, build_type, op0, op1);
13290 if (final_type != NULL_TREE)
13291 ret = convert (final_type, ret);
13293 return_build_binary_op:
13294 gcc_assert (ret != error_mark_node);
13295 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
13296 ret = (int_operands
13297 ? note_integer_operands (ret)
13298 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
13299 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
13300 && !in_late_binary_op)
13301 ret = note_integer_operands (ret);
13302 protected_set_expr_location (ret, location);
13304 if (instrument_expr != NULL)
13305 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
13306 instrument_expr, ret);
13308 if (semantic_result_type)
13309 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
13310 semantic_result_type, ret);
13312 return ret;
13316 /* Convert EXPR to be a truth-value, validating its type for this
13317 purpose. LOCATION is the source location for the expression. */
13319 tree
13320 c_objc_common_truthvalue_conversion (location_t location, tree expr)
13322 bool int_const, int_operands;
13324 switch (TREE_CODE (TREE_TYPE (expr)))
13326 case ARRAY_TYPE:
13327 error_at (location, "used array that cannot be converted to pointer where scalar is required");
13328 return error_mark_node;
13330 case RECORD_TYPE:
13331 error_at (location, "used struct type value where scalar is required");
13332 return error_mark_node;
13334 case UNION_TYPE:
13335 error_at (location, "used union type value where scalar is required");
13336 return error_mark_node;
13338 case VOID_TYPE:
13339 error_at (location, "void value not ignored as it ought to be");
13340 return error_mark_node;
13342 case POINTER_TYPE:
13343 if (reject_gcc_builtin (expr))
13344 return error_mark_node;
13345 break;
13347 case FUNCTION_TYPE:
13348 gcc_unreachable ();
13350 case VECTOR_TYPE:
13351 error_at (location, "used vector type where scalar is required");
13352 return error_mark_node;
13354 default:
13355 break;
13358 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
13359 int_operands = EXPR_INT_CONST_OPERANDS (expr);
13360 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
13362 expr = remove_c_maybe_const_expr (expr);
13363 expr = build2 (NE_EXPR, integer_type_node, expr,
13364 convert (TREE_TYPE (expr), integer_zero_node));
13365 expr = note_integer_operands (expr);
13367 else
13368 /* ??? Should we also give an error for vectors rather than leaving
13369 those to give errors later? */
13370 expr = c_common_truthvalue_conversion (location, expr);
13372 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
13374 if (TREE_OVERFLOW (expr))
13375 return expr;
13376 else
13377 return note_integer_operands (expr);
13379 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
13380 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
13381 return expr;
13385 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
13386 required. */
13388 tree
13389 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
13391 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
13393 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
13394 /* Executing a compound literal inside a function reinitializes
13395 it. */
13396 if (!TREE_STATIC (decl))
13397 *se = true;
13398 return decl;
13400 else
13401 return expr;
13404 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
13405 statement. LOC is the location of the construct. */
13407 tree
13408 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
13409 tree clauses)
13411 body = c_end_compound_stmt (loc, body, true);
13413 tree stmt = make_node (code);
13414 TREE_TYPE (stmt) = void_type_node;
13415 OMP_BODY (stmt) = body;
13416 OMP_CLAUSES (stmt) = clauses;
13417 SET_EXPR_LOCATION (stmt, loc);
13419 return add_stmt (stmt);
13422 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
13423 statement. LOC is the location of the OACC_DATA. */
13425 tree
13426 c_finish_oacc_data (location_t loc, tree clauses, tree block)
13428 tree stmt;
13430 block = c_end_compound_stmt (loc, block, true);
13432 stmt = make_node (OACC_DATA);
13433 TREE_TYPE (stmt) = void_type_node;
13434 OACC_DATA_CLAUSES (stmt) = clauses;
13435 OACC_DATA_BODY (stmt) = block;
13436 SET_EXPR_LOCATION (stmt, loc);
13438 return add_stmt (stmt);
13441 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
13442 statement. LOC is the location of the OACC_HOST_DATA. */
13444 tree
13445 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
13447 tree stmt;
13449 block = c_end_compound_stmt (loc, block, true);
13451 stmt = make_node (OACC_HOST_DATA);
13452 TREE_TYPE (stmt) = void_type_node;
13453 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
13454 OACC_HOST_DATA_BODY (stmt) = block;
13455 SET_EXPR_LOCATION (stmt, loc);
13457 return add_stmt (stmt);
13460 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13462 tree
13463 c_begin_omp_parallel (void)
13465 tree block;
13467 keep_next_level ();
13468 block = c_begin_compound_stmt (true);
13470 return block;
13473 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
13474 statement. LOC is the location of the OMP_PARALLEL. */
13476 tree
13477 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
13479 tree stmt;
13481 block = c_end_compound_stmt (loc, block, true);
13483 stmt = make_node (OMP_PARALLEL);
13484 TREE_TYPE (stmt) = void_type_node;
13485 OMP_PARALLEL_CLAUSES (stmt) = clauses;
13486 OMP_PARALLEL_BODY (stmt) = block;
13487 SET_EXPR_LOCATION (stmt, loc);
13489 return add_stmt (stmt);
13492 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13494 tree
13495 c_begin_omp_task (void)
13497 tree block;
13499 keep_next_level ();
13500 block = c_begin_compound_stmt (true);
13502 return block;
13505 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
13506 statement. LOC is the location of the #pragma. */
13508 tree
13509 c_finish_omp_task (location_t loc, tree clauses, tree block)
13511 tree stmt;
13513 block = c_end_compound_stmt (loc, block, true);
13515 stmt = make_node (OMP_TASK);
13516 TREE_TYPE (stmt) = void_type_node;
13517 OMP_TASK_CLAUSES (stmt) = clauses;
13518 OMP_TASK_BODY (stmt) = block;
13519 SET_EXPR_LOCATION (stmt, loc);
13521 return add_stmt (stmt);
13524 /* Generate GOMP_cancel call for #pragma omp cancel. */
13526 void
13527 c_finish_omp_cancel (location_t loc, tree clauses)
13529 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
13530 int mask = 0;
13531 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13532 mask = 1;
13533 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13534 mask = 2;
13535 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13536 mask = 4;
13537 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13538 mask = 8;
13539 else
13541 error_at (loc, "%<#pragma omp cancel%> must specify one of "
13542 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13543 "clauses");
13544 return;
13546 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
13547 if (ifc != NULL_TREE)
13549 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
13550 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
13551 error_at (OMP_CLAUSE_LOCATION (ifc),
13552 "expected %<cancel%> %<if%> clause modifier");
13553 else
13555 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
13556 if (ifc2 != NULL_TREE)
13558 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
13559 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13560 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13561 error_at (OMP_CLAUSE_LOCATION (ifc2),
13562 "expected %<cancel%> %<if%> clause modifier");
13566 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13567 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13568 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13569 build_zero_cst (type));
13571 else
13572 ifc = boolean_true_node;
13573 tree stmt = build_call_expr_loc (loc, fn, 2,
13574 build_int_cst (integer_type_node, mask),
13575 ifc);
13576 add_stmt (stmt);
13579 /* Generate GOMP_cancellation_point call for
13580 #pragma omp cancellation point. */
13582 void
13583 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13585 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13586 int mask = 0;
13587 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13588 mask = 1;
13589 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13590 mask = 2;
13591 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13592 mask = 4;
13593 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13594 mask = 8;
13595 else
13597 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13598 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13599 "clauses");
13600 return;
13602 tree stmt = build_call_expr_loc (loc, fn, 1,
13603 build_int_cst (integer_type_node, mask));
13604 add_stmt (stmt);
13607 /* Helper function for handle_omp_array_sections. Called recursively
13608 to handle multiple array-section-subscripts. C is the clause,
13609 T current expression (initially OMP_CLAUSE_DECL), which is either
13610 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13611 expression if specified, TREE_VALUE length expression if specified,
13612 TREE_CHAIN is what it has been specified after, or some decl.
13613 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13614 set to true if any of the array-section-subscript could have length
13615 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13616 first array-section-subscript which is known not to have length
13617 of one. Given say:
13618 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13619 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13620 all are or may have length of 1, array-section-subscript [:2] is the
13621 first one known not to have length 1. For array-section-subscript
13622 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13623 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13624 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
13625 case though, as some lengths could be zero. */
13627 static tree
13628 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13629 bool &maybe_zero_len, unsigned int &first_non_one,
13630 enum c_omp_region_type ort)
13632 tree ret, low_bound, length, type;
13633 if (TREE_CODE (t) != TREE_LIST)
13635 if (error_operand_p (t))
13636 return error_mark_node;
13637 ret = t;
13638 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13639 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13640 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13642 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13643 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13644 return error_mark_node;
13646 while (INDIRECT_REF_P (t))
13648 t = TREE_OPERAND (t, 0);
13649 STRIP_NOPS (t);
13650 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13651 t = TREE_OPERAND (t, 0);
13653 while (TREE_CODE (t) == COMPOUND_EXPR)
13655 t = TREE_OPERAND (t, 1);
13656 STRIP_NOPS (t);
13658 if (TREE_CODE (t) == COMPONENT_REF
13659 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13660 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13661 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13663 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13665 error_at (OMP_CLAUSE_LOCATION (c),
13666 "bit-field %qE in %qs clause",
13667 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13668 return error_mark_node;
13670 while (TREE_CODE (t) == COMPONENT_REF)
13672 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13674 error_at (OMP_CLAUSE_LOCATION (c),
13675 "%qE is a member of a union", t);
13676 return error_mark_node;
13678 t = TREE_OPERAND (t, 0);
13679 while (TREE_CODE (t) == MEM_REF
13680 || INDIRECT_REF_P (t)
13681 || TREE_CODE (t) == ARRAY_REF)
13683 t = TREE_OPERAND (t, 0);
13684 STRIP_NOPS (t);
13685 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13686 t = TREE_OPERAND (t, 0);
13688 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13690 if (maybe_ne (mem_ref_offset (t), 0))
13691 error_at (OMP_CLAUSE_LOCATION (c),
13692 "cannot dereference %qE in %qs clause", t,
13693 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13694 else
13695 t = TREE_OPERAND (t, 0);
13699 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13701 if (DECL_P (t))
13702 error_at (OMP_CLAUSE_LOCATION (c),
13703 "%qD is not a variable in %qs clause", t,
13704 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13705 else
13706 error_at (OMP_CLAUSE_LOCATION (c),
13707 "%qE is not a variable in %qs clause", t,
13708 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13709 return error_mark_node;
13711 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13712 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13713 && TYPE_ATOMIC (TREE_TYPE (t)))
13715 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13716 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13717 return error_mark_node;
13719 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13720 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13721 && VAR_P (t)
13722 && DECL_THREAD_LOCAL_P (t))
13724 error_at (OMP_CLAUSE_LOCATION (c),
13725 "%qD is threadprivate variable in %qs clause", t,
13726 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13727 return error_mark_node;
13729 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13730 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13731 && TYPE_ATOMIC (TREE_TYPE (t))
13732 && POINTER_TYPE_P (TREE_TYPE (t)))
13734 /* If the array section is pointer based and the pointer
13735 itself is _Atomic qualified, we need to atomically load
13736 the pointer. */
13737 c_expr expr;
13738 memset (&expr, 0, sizeof (expr));
13739 expr.value = ret;
13740 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13741 expr, false, false);
13742 ret = expr.value;
13744 return ret;
13747 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13748 maybe_zero_len, first_non_one, ort);
13749 if (ret == error_mark_node || ret == NULL_TREE)
13750 return ret;
13752 type = TREE_TYPE (ret);
13753 low_bound = TREE_PURPOSE (t);
13754 length = TREE_VALUE (t);
13756 if (low_bound == error_mark_node || length == error_mark_node)
13757 return error_mark_node;
13759 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13761 error_at (OMP_CLAUSE_LOCATION (c),
13762 "low bound %qE of array section does not have integral type",
13763 low_bound);
13764 return error_mark_node;
13766 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13768 error_at (OMP_CLAUSE_LOCATION (c),
13769 "length %qE of array section does not have integral type",
13770 length);
13771 return error_mark_node;
13773 if (low_bound
13774 && TREE_CODE (low_bound) == INTEGER_CST
13775 && TYPE_PRECISION (TREE_TYPE (low_bound))
13776 > TYPE_PRECISION (sizetype))
13777 low_bound = fold_convert (sizetype, low_bound);
13778 if (length
13779 && TREE_CODE (length) == INTEGER_CST
13780 && TYPE_PRECISION (TREE_TYPE (length))
13781 > TYPE_PRECISION (sizetype))
13782 length = fold_convert (sizetype, length);
13783 if (low_bound == NULL_TREE)
13784 low_bound = integer_zero_node;
13785 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13786 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13787 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13789 if (length != integer_one_node)
13791 error_at (OMP_CLAUSE_LOCATION (c),
13792 "expected single pointer in %qs clause",
13793 user_omp_clause_code_name (c, ort == C_ORT_ACC));
13794 return error_mark_node;
13797 if (length != NULL_TREE)
13799 if (!integer_nonzerop (length))
13801 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13802 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13803 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13804 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13805 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13807 if (integer_zerop (length))
13809 error_at (OMP_CLAUSE_LOCATION (c),
13810 "zero length array section in %qs clause",
13811 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13812 return error_mark_node;
13815 else
13816 maybe_zero_len = true;
13818 if (first_non_one == types.length ()
13819 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13820 first_non_one++;
13822 if (TREE_CODE (type) == ARRAY_TYPE)
13824 if (length == NULL_TREE
13825 && (TYPE_DOMAIN (type) == NULL_TREE
13826 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13828 error_at (OMP_CLAUSE_LOCATION (c),
13829 "for unknown bound array type length expression must "
13830 "be specified");
13831 return error_mark_node;
13833 if (TREE_CODE (low_bound) == INTEGER_CST
13834 && tree_int_cst_sgn (low_bound) == -1)
13836 error_at (OMP_CLAUSE_LOCATION (c),
13837 "negative low bound in array section in %qs clause",
13838 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13839 return error_mark_node;
13841 if (length != NULL_TREE
13842 && TREE_CODE (length) == INTEGER_CST
13843 && tree_int_cst_sgn (length) == -1)
13845 error_at (OMP_CLAUSE_LOCATION (c),
13846 "negative length in array section in %qs clause",
13847 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13848 return error_mark_node;
13850 if (TYPE_DOMAIN (type)
13851 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13852 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13853 == INTEGER_CST)
13855 tree size
13856 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13857 size = size_binop (PLUS_EXPR, size, size_one_node);
13858 if (TREE_CODE (low_bound) == INTEGER_CST)
13860 if (tree_int_cst_lt (size, low_bound))
13862 error_at (OMP_CLAUSE_LOCATION (c),
13863 "low bound %qE above array section size "
13864 "in %qs clause", low_bound,
13865 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13866 return error_mark_node;
13868 if (tree_int_cst_equal (size, low_bound))
13870 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13871 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13872 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13873 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13874 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13876 error_at (OMP_CLAUSE_LOCATION (c),
13877 "zero length array section in %qs clause",
13878 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13879 return error_mark_node;
13881 maybe_zero_len = true;
13883 else if (length == NULL_TREE
13884 && first_non_one == types.length ()
13885 && tree_int_cst_equal
13886 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13887 low_bound))
13888 first_non_one++;
13890 else if (length == NULL_TREE)
13892 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13893 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13894 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13895 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13896 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13897 maybe_zero_len = true;
13898 if (first_non_one == types.length ())
13899 first_non_one++;
13901 if (length && TREE_CODE (length) == INTEGER_CST)
13903 if (tree_int_cst_lt (size, length))
13905 error_at (OMP_CLAUSE_LOCATION (c),
13906 "length %qE above array section size "
13907 "in %qs clause", length,
13908 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13909 return error_mark_node;
13911 if (TREE_CODE (low_bound) == INTEGER_CST)
13913 tree lbpluslen
13914 = size_binop (PLUS_EXPR,
13915 fold_convert (sizetype, low_bound),
13916 fold_convert (sizetype, length));
13917 if (TREE_CODE (lbpluslen) == INTEGER_CST
13918 && tree_int_cst_lt (size, lbpluslen))
13920 error_at (OMP_CLAUSE_LOCATION (c),
13921 "high bound %qE above array section size "
13922 "in %qs clause", lbpluslen,
13923 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13924 return error_mark_node;
13929 else if (length == NULL_TREE)
13931 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13932 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13933 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13934 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13935 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13936 maybe_zero_len = true;
13937 if (first_non_one == types.length ())
13938 first_non_one++;
13941 /* For [lb:] we will need to evaluate lb more than once. */
13942 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13944 tree lb = save_expr (low_bound);
13945 if (lb != low_bound)
13947 TREE_PURPOSE (t) = lb;
13948 low_bound = lb;
13952 else if (TREE_CODE (type) == POINTER_TYPE)
13954 if (length == NULL_TREE)
13956 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
13957 error_at (OMP_CLAUSE_LOCATION (c),
13958 "for array function parameter length expression "
13959 "must be specified");
13960 else
13961 error_at (OMP_CLAUSE_LOCATION (c),
13962 "for pointer type length expression must be specified");
13963 return error_mark_node;
13965 if (length != NULL_TREE
13966 && TREE_CODE (length) == INTEGER_CST
13967 && tree_int_cst_sgn (length) == -1)
13969 error_at (OMP_CLAUSE_LOCATION (c),
13970 "negative length in array section in %qs clause",
13971 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13972 return error_mark_node;
13974 /* If there is a pointer type anywhere but in the very first
13975 array-section-subscript, the array section could be non-contiguous. */
13976 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13977 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13978 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13980 /* If any prior dimension has a non-one length, then deem this
13981 array section as non-contiguous. */
13982 for (tree d = TREE_CHAIN (t); TREE_CODE (d) == TREE_LIST;
13983 d = TREE_CHAIN (d))
13985 tree d_length = TREE_VALUE (d);
13986 if (d_length == NULL_TREE || !integer_onep (d_length))
13988 error_at (OMP_CLAUSE_LOCATION (c),
13989 "array section is not contiguous in %qs clause",
13990 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13991 return error_mark_node;
13996 else
13998 error_at (OMP_CLAUSE_LOCATION (c),
13999 "%qE does not have pointer or array type", ret);
14000 return error_mark_node;
14002 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
14003 types.safe_push (TREE_TYPE (ret));
14004 /* We will need to evaluate lb more than once. */
14005 tree lb = save_expr (low_bound);
14006 if (lb != low_bound)
14008 TREE_PURPOSE (t) = lb;
14009 low_bound = lb;
14011 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
14012 return ret;
14015 /* Handle array sections for clause C. */
14017 static bool
14018 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
14020 bool maybe_zero_len = false;
14021 unsigned int first_non_one = 0;
14022 auto_vec<tree, 10> types;
14023 tree *tp = &OMP_CLAUSE_DECL (c);
14024 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14025 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
14026 && TREE_CODE (*tp) == TREE_LIST
14027 && TREE_PURPOSE (*tp)
14028 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
14029 tp = &TREE_VALUE (*tp);
14030 tree first = handle_omp_array_sections_1 (c, *tp, types,
14031 maybe_zero_len, first_non_one,
14032 ort);
14033 if (first == error_mark_node)
14034 return true;
14035 if (first == NULL_TREE)
14036 return false;
14037 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14038 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
14040 tree t = *tp;
14041 tree tem = NULL_TREE;
14042 /* Need to evaluate side effects in the length expressions
14043 if any. */
14044 while (TREE_CODE (t) == TREE_LIST)
14046 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
14048 if (tem == NULL_TREE)
14049 tem = TREE_VALUE (t);
14050 else
14051 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
14052 TREE_VALUE (t), tem);
14054 t = TREE_CHAIN (t);
14056 if (tem)
14057 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
14058 first = c_fully_fold (first, false, NULL, true);
14059 *tp = first;
14061 else
14063 unsigned int num = types.length (), i;
14064 tree t, side_effects = NULL_TREE, size = NULL_TREE;
14065 tree condition = NULL_TREE;
14067 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
14068 maybe_zero_len = true;
14070 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
14071 t = TREE_CHAIN (t))
14073 tree low_bound = TREE_PURPOSE (t);
14074 tree length = TREE_VALUE (t);
14076 i--;
14077 if (low_bound
14078 && TREE_CODE (low_bound) == INTEGER_CST
14079 && TYPE_PRECISION (TREE_TYPE (low_bound))
14080 > TYPE_PRECISION (sizetype))
14081 low_bound = fold_convert (sizetype, low_bound);
14082 if (length
14083 && TREE_CODE (length) == INTEGER_CST
14084 && TYPE_PRECISION (TREE_TYPE (length))
14085 > TYPE_PRECISION (sizetype))
14086 length = fold_convert (sizetype, length);
14087 if (low_bound == NULL_TREE)
14088 low_bound = integer_zero_node;
14089 if (!maybe_zero_len && i > first_non_one)
14091 if (integer_nonzerop (low_bound))
14092 goto do_warn_noncontiguous;
14093 if (length != NULL_TREE
14094 && TREE_CODE (length) == INTEGER_CST
14095 && TYPE_DOMAIN (types[i])
14096 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
14097 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
14098 == INTEGER_CST)
14100 tree size;
14101 size = size_binop (PLUS_EXPR,
14102 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
14103 size_one_node);
14104 if (!tree_int_cst_equal (length, size))
14106 do_warn_noncontiguous:
14107 error_at (OMP_CLAUSE_LOCATION (c),
14108 "array section is not contiguous in %qs "
14109 "clause",
14110 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14111 return true;
14114 if (length != NULL_TREE
14115 && TREE_SIDE_EFFECTS (length))
14117 if (side_effects == NULL_TREE)
14118 side_effects = length;
14119 else
14120 side_effects = build2 (COMPOUND_EXPR,
14121 TREE_TYPE (side_effects),
14122 length, side_effects);
14125 else
14127 tree l;
14129 if (i > first_non_one
14130 && ((length && integer_nonzerop (length))
14131 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14132 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14133 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
14134 continue;
14135 if (length)
14136 l = fold_convert (sizetype, length);
14137 else
14139 l = size_binop (PLUS_EXPR,
14140 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
14141 size_one_node);
14142 l = size_binop (MINUS_EXPR, l,
14143 fold_convert (sizetype, low_bound));
14145 if (i > first_non_one)
14147 l = fold_build2 (NE_EXPR, boolean_type_node, l,
14148 size_zero_node);
14149 if (condition == NULL_TREE)
14150 condition = l;
14151 else
14152 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
14153 l, condition);
14155 else if (size == NULL_TREE)
14157 size = size_in_bytes (TREE_TYPE (types[i]));
14158 tree eltype = TREE_TYPE (types[num - 1]);
14159 while (TREE_CODE (eltype) == ARRAY_TYPE)
14160 eltype = TREE_TYPE (eltype);
14161 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14162 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14163 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14165 if (integer_zerop (size)
14166 || integer_zerop (size_in_bytes (eltype)))
14168 error_at (OMP_CLAUSE_LOCATION (c),
14169 "zero length array section in %qs clause",
14170 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14171 return error_mark_node;
14173 size = size_binop (EXACT_DIV_EXPR, size,
14174 size_in_bytes (eltype));
14176 size = size_binop (MULT_EXPR, size, l);
14177 if (condition)
14178 size = fold_build3 (COND_EXPR, sizetype, condition,
14179 size, size_zero_node);
14181 else
14182 size = size_binop (MULT_EXPR, size, l);
14185 if (side_effects)
14186 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
14187 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14188 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14189 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14191 size = size_binop (MINUS_EXPR, size, size_one_node);
14192 size = c_fully_fold (size, false, NULL);
14193 size = save_expr (size);
14194 tree index_type = build_index_type (size);
14195 tree eltype = TREE_TYPE (first);
14196 while (TREE_CODE (eltype) == ARRAY_TYPE)
14197 eltype = TREE_TYPE (eltype);
14198 tree type = build_array_type (eltype, index_type);
14199 tree ptype = build_pointer_type (eltype);
14200 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14201 t = build_fold_addr_expr (t);
14202 tree t2 = build_fold_addr_expr (first);
14203 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14204 ptrdiff_type_node, t2);
14205 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14206 ptrdiff_type_node, t2,
14207 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14208 ptrdiff_type_node, t));
14209 t2 = c_fully_fold (t2, false, NULL);
14210 if (tree_fits_shwi_p (t2))
14211 t = build2 (MEM_REF, type, t,
14212 build_int_cst (ptype, tree_to_shwi (t2)));
14213 else
14215 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
14216 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
14217 TREE_TYPE (t), t, t2);
14218 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
14220 OMP_CLAUSE_DECL (c) = t;
14221 return false;
14223 first = c_fully_fold (first, false, NULL);
14224 OMP_CLAUSE_DECL (c) = first;
14225 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
14226 return false;
14227 if (size)
14228 size = c_fully_fold (size, false, NULL);
14229 OMP_CLAUSE_SIZE (c) = size;
14230 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14231 || (TREE_CODE (t) == COMPONENT_REF
14232 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
14233 return false;
14234 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
14235 switch (OMP_CLAUSE_MAP_KIND (c))
14237 case GOMP_MAP_ALLOC:
14238 case GOMP_MAP_IF_PRESENT:
14239 case GOMP_MAP_TO:
14240 case GOMP_MAP_FROM:
14241 case GOMP_MAP_TOFROM:
14242 case GOMP_MAP_ALWAYS_TO:
14243 case GOMP_MAP_ALWAYS_FROM:
14244 case GOMP_MAP_ALWAYS_TOFROM:
14245 case GOMP_MAP_RELEASE:
14246 case GOMP_MAP_DELETE:
14247 case GOMP_MAP_FORCE_TO:
14248 case GOMP_MAP_FORCE_FROM:
14249 case GOMP_MAP_FORCE_TOFROM:
14250 case GOMP_MAP_FORCE_PRESENT:
14251 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
14252 break;
14253 default:
14254 break;
14256 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
14257 if (TREE_CODE (t) == COMPONENT_REF)
14258 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
14259 else
14260 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
14261 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
14262 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
14263 && !c_mark_addressable (t))
14264 return false;
14265 OMP_CLAUSE_DECL (c2) = t;
14266 t = build_fold_addr_expr (first);
14267 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
14268 tree ptr = OMP_CLAUSE_DECL (c2);
14269 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
14270 ptr = build_fold_addr_expr (ptr);
14271 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14272 ptrdiff_type_node, t,
14273 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14274 ptrdiff_type_node, ptr));
14275 t = c_fully_fold (t, false, NULL);
14276 OMP_CLAUSE_SIZE (c2) = t;
14277 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
14278 OMP_CLAUSE_CHAIN (c) = c2;
14280 return false;
14283 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
14284 an inline call. But, remap
14285 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
14286 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
14288 static tree
14289 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
14290 tree decl, tree placeholder)
14292 copy_body_data id;
14293 hash_map<tree, tree> decl_map;
14295 decl_map.put (omp_decl1, placeholder);
14296 decl_map.put (omp_decl2, decl);
14297 memset (&id, 0, sizeof (id));
14298 id.src_fn = DECL_CONTEXT (omp_decl1);
14299 id.dst_fn = current_function_decl;
14300 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
14301 id.decl_map = &decl_map;
14303 id.copy_decl = copy_decl_no_change;
14304 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
14305 id.transform_new_cfg = true;
14306 id.transform_return_to_modify = false;
14307 id.eh_lp_nr = 0;
14308 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
14309 return stmt;
14312 /* Helper function of c_finish_omp_clauses, called via walk_tree.
14313 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
14315 static tree
14316 c_find_omp_placeholder_r (tree *tp, int *, void *data)
14318 if (*tp == (tree) data)
14319 return *tp;
14320 return NULL_TREE;
14323 /* Similarly, but also walk aggregate fields. */
14325 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
14327 static tree
14328 c_find_omp_var_r (tree *tp, int *, void *data)
14330 if (*tp == ((struct c_find_omp_var_s *) data)->var)
14331 return *tp;
14332 if (RECORD_OR_UNION_TYPE_P (*tp))
14334 tree field;
14335 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
14337 for (field = TYPE_FIELDS (*tp); field;
14338 field = DECL_CHAIN (field))
14339 if (TREE_CODE (field) == FIELD_DECL)
14341 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
14342 c_find_omp_var_r, data, pset);
14343 if (ret)
14344 return ret;
14345 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
14346 if (ret)
14347 return ret;
14348 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
14349 pset);
14350 if (ret)
14351 return ret;
14352 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
14353 if (ret)
14354 return ret;
14357 else if (INTEGRAL_TYPE_P (*tp))
14358 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
14359 ((struct c_find_omp_var_s *) data)->pset);
14360 return NULL_TREE;
14363 /* Finish OpenMP iterators ITER. Return true if they are errorneous
14364 and clauses containing them should be removed. */
14366 static bool
14367 c_omp_finish_iterators (tree iter)
14369 bool ret = false;
14370 for (tree it = iter; it; it = TREE_CHAIN (it))
14372 tree var = TREE_VEC_ELT (it, 0);
14373 tree begin = TREE_VEC_ELT (it, 1);
14374 tree end = TREE_VEC_ELT (it, 2);
14375 tree step = TREE_VEC_ELT (it, 3);
14376 tree orig_step;
14377 tree type = TREE_TYPE (var);
14378 location_t loc = DECL_SOURCE_LOCATION (var);
14379 if (type == error_mark_node)
14381 ret = true;
14382 continue;
14384 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14386 error_at (loc, "iterator %qD has neither integral nor pointer type",
14387 var);
14388 ret = true;
14389 continue;
14391 else if (TYPE_ATOMIC (type))
14393 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
14394 ret = true;
14395 continue;
14397 else if (TYPE_READONLY (type))
14399 error_at (loc, "iterator %qD has const qualified type", var);
14400 ret = true;
14401 continue;
14403 else if (step == error_mark_node
14404 || TREE_TYPE (step) == error_mark_node)
14406 ret = true;
14407 continue;
14409 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
14411 error_at (EXPR_LOC_OR_LOC (step, loc),
14412 "iterator step with non-integral type");
14413 ret = true;
14414 continue;
14416 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
14417 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
14418 orig_step = save_expr (c_fully_fold (step, false, NULL));
14419 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
14420 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
14421 if (POINTER_TYPE_P (type))
14423 begin = save_expr (begin);
14424 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
14425 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
14426 fold_convert (sizetype, step),
14427 fold_convert (sizetype, begin));
14428 step = fold_convert (ssizetype, step);
14430 if (integer_zerop (step))
14432 error_at (loc, "iterator %qD has zero step", var);
14433 ret = true;
14434 continue;
14437 if (begin == error_mark_node
14438 || end == error_mark_node
14439 || step == error_mark_node
14440 || orig_step == error_mark_node)
14442 ret = true;
14443 continue;
14445 hash_set<tree> pset;
14446 tree it2;
14447 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
14449 tree var2 = TREE_VEC_ELT (it2, 0);
14450 tree begin2 = TREE_VEC_ELT (it2, 1);
14451 tree end2 = TREE_VEC_ELT (it2, 2);
14452 tree step2 = TREE_VEC_ELT (it2, 3);
14453 tree type2 = TREE_TYPE (var2);
14454 location_t loc2 = DECL_SOURCE_LOCATION (var2);
14455 struct c_find_omp_var_s data = { var, &pset };
14456 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
14458 error_at (loc2,
14459 "type of iterator %qD refers to outer iterator %qD",
14460 var2, var);
14461 break;
14463 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
14465 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
14466 "begin expression refers to outer iterator %qD", var);
14467 break;
14469 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
14471 error_at (EXPR_LOC_OR_LOC (end2, loc2),
14472 "end expression refers to outer iterator %qD", var);
14473 break;
14475 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
14477 error_at (EXPR_LOC_OR_LOC (step2, loc2),
14478 "step expression refers to outer iterator %qD", var);
14479 break;
14482 if (it2)
14484 ret = true;
14485 continue;
14487 TREE_VEC_ELT (it, 1) = begin;
14488 TREE_VEC_ELT (it, 2) = end;
14489 TREE_VEC_ELT (it, 3) = step;
14490 TREE_VEC_ELT (it, 4) = orig_step;
14492 return ret;
14495 /* Ensure that pointers are used in OpenACC attach and detach clauses.
14496 Return true if an error has been detected. */
14498 static bool
14499 c_oacc_check_attachments (tree c)
14501 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14502 return false;
14504 /* OpenACC attach / detach clauses must be pointers. */
14505 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14506 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14508 tree t = OMP_CLAUSE_DECL (c);
14510 while (TREE_CODE (t) == TREE_LIST)
14511 t = TREE_CHAIN (t);
14513 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14515 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
14516 user_omp_clause_code_name (c, true));
14517 return true;
14521 return false;
14524 /* For all elements of CLAUSES, validate them against their constraints.
14525 Remove any elements from the list that are invalid. */
14527 tree
14528 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
14530 bitmap_head generic_head, firstprivate_head, lastprivate_head;
14531 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
14532 bitmap_head oacc_reduction_head, is_on_device_head;
14533 tree c, t, type, *pc;
14534 tree simdlen = NULL_TREE, safelen = NULL_TREE;
14535 bool branch_seen = false;
14536 bool copyprivate_seen = false;
14537 bool mergeable_seen = false;
14538 tree *detach_seen = NULL;
14539 bool linear_variable_step_check = false;
14540 tree *nowait_clause = NULL;
14541 tree ordered_clause = NULL_TREE;
14542 tree schedule_clause = NULL_TREE;
14543 bool oacc_async = false;
14544 bool indir_component_ref_p = false;
14545 tree last_iterators = NULL_TREE;
14546 bool last_iterators_remove = false;
14547 tree *nogroup_seen = NULL;
14548 tree *order_clause = NULL;
14549 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
14550 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
14551 int reduction_seen = 0;
14552 bool allocate_seen = false;
14553 bool implicit_moved = false;
14554 bool target_in_reduction_seen = false;
14556 bitmap_obstack_initialize (NULL);
14557 bitmap_initialize (&generic_head, &bitmap_default_obstack);
14558 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
14559 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
14560 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
14561 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
14562 bitmap_initialize (&map_head, &bitmap_default_obstack);
14563 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
14564 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
14565 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
14566 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
14567 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
14568 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
14570 if (ort & C_ORT_ACC)
14571 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14572 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
14574 oacc_async = true;
14575 break;
14578 tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
14580 for (pc = &clauses, c = clauses; c ; c = *pc)
14582 bool remove = false;
14583 bool need_complete = false;
14584 bool need_implicitly_determined = false;
14586 /* We've reached the end of a list of expanded nodes. Reset the group
14587 start pointer. */
14588 if (c == grp_sentinel)
14589 grp_start_p = NULL;
14591 switch (OMP_CLAUSE_CODE (c))
14593 case OMP_CLAUSE_SHARED:
14594 need_implicitly_determined = true;
14595 goto check_dup_generic;
14597 case OMP_CLAUSE_PRIVATE:
14598 need_complete = true;
14599 need_implicitly_determined = true;
14600 goto check_dup_generic;
14602 case OMP_CLAUSE_REDUCTION:
14603 if (reduction_seen == 0)
14604 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14605 else if (reduction_seen != -2
14606 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14607 ? -1 : 1))
14609 error_at (OMP_CLAUSE_LOCATION (c),
14610 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14611 "on the same construct");
14612 reduction_seen = -2;
14614 /* FALLTHRU */
14615 case OMP_CLAUSE_IN_REDUCTION:
14616 case OMP_CLAUSE_TASK_REDUCTION:
14617 need_implicitly_determined = true;
14618 t = OMP_CLAUSE_DECL (c);
14619 if (TREE_CODE (t) == TREE_LIST)
14621 if (handle_omp_array_sections (c, ort))
14623 remove = true;
14624 break;
14627 t = OMP_CLAUSE_DECL (c);
14628 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14629 && OMP_CLAUSE_REDUCTION_INSCAN (c))
14631 error_at (OMP_CLAUSE_LOCATION (c),
14632 "%<inscan%> %<reduction%> clause with array "
14633 "section");
14634 remove = true;
14635 break;
14638 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14639 if (t == error_mark_node)
14641 remove = true;
14642 break;
14644 if (oacc_async)
14645 c_mark_addressable (t);
14646 type = TREE_TYPE (t);
14647 if (TREE_CODE (t) == MEM_REF)
14648 type = TREE_TYPE (type);
14649 if (TREE_CODE (type) == ARRAY_TYPE)
14651 tree oatype = type;
14652 gcc_assert (TREE_CODE (t) != MEM_REF);
14653 while (TREE_CODE (type) == ARRAY_TYPE)
14654 type = TREE_TYPE (type);
14655 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14657 error_at (OMP_CLAUSE_LOCATION (c),
14658 "%qD in %<reduction%> clause is a zero size array",
14660 remove = true;
14661 break;
14663 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14664 TYPE_SIZE_UNIT (type));
14665 if (integer_zerop (size))
14667 error_at (OMP_CLAUSE_LOCATION (c),
14668 "%qD in %<reduction%> clause is a zero size array",
14670 remove = true;
14671 break;
14673 size = size_binop (MINUS_EXPR, size, size_one_node);
14674 size = save_expr (size);
14675 tree index_type = build_index_type (size);
14676 tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14677 index_type);
14678 atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14679 tree ptype = build_pointer_type (type);
14680 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14681 t = build_fold_addr_expr (t);
14682 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14683 OMP_CLAUSE_DECL (c) = t;
14685 if (TYPE_ATOMIC (type))
14687 error_at (OMP_CLAUSE_LOCATION (c),
14688 "%<_Atomic%> %qE in %<reduction%> clause", t);
14689 remove = true;
14690 break;
14692 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14693 || OMP_CLAUSE_REDUCTION_TASK (c))
14695 /* Disallow zero sized or potentially zero sized task
14696 reductions. */
14697 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14699 error_at (OMP_CLAUSE_LOCATION (c),
14700 "zero sized type %qT in %qs clause", type,
14701 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14702 remove = true;
14703 break;
14705 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14707 error_at (OMP_CLAUSE_LOCATION (c),
14708 "variable sized type %qT in %qs clause", type,
14709 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14710 remove = true;
14711 break;
14714 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14715 && (FLOAT_TYPE_P (type)
14716 || TREE_CODE (type) == COMPLEX_TYPE))
14718 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14719 const char *r_name = NULL;
14721 switch (r_code)
14723 case PLUS_EXPR:
14724 case MULT_EXPR:
14725 case MINUS_EXPR:
14726 case TRUTH_ANDIF_EXPR:
14727 case TRUTH_ORIF_EXPR:
14728 break;
14729 case MIN_EXPR:
14730 if (TREE_CODE (type) == COMPLEX_TYPE)
14731 r_name = "min";
14732 break;
14733 case MAX_EXPR:
14734 if (TREE_CODE (type) == COMPLEX_TYPE)
14735 r_name = "max";
14736 break;
14737 case BIT_AND_EXPR:
14738 r_name = "&";
14739 break;
14740 case BIT_XOR_EXPR:
14741 r_name = "^";
14742 break;
14743 case BIT_IOR_EXPR:
14744 r_name = "|";
14745 break;
14746 default:
14747 gcc_unreachable ();
14749 if (r_name)
14751 error_at (OMP_CLAUSE_LOCATION (c),
14752 "%qE has invalid type for %<reduction(%s)%>",
14753 t, r_name);
14754 remove = true;
14755 break;
14758 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14760 error_at (OMP_CLAUSE_LOCATION (c),
14761 "user defined reduction not found for %qE", t);
14762 remove = true;
14763 break;
14765 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14767 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14768 type = TYPE_MAIN_VARIANT (type);
14769 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14770 VAR_DECL, NULL_TREE, type);
14771 tree decl_placeholder = NULL_TREE;
14772 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14773 DECL_ARTIFICIAL (placeholder) = 1;
14774 DECL_IGNORED_P (placeholder) = 1;
14775 if (TREE_CODE (t) == MEM_REF)
14777 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14778 VAR_DECL, NULL_TREE, type);
14779 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14780 DECL_ARTIFICIAL (decl_placeholder) = 1;
14781 DECL_IGNORED_P (decl_placeholder) = 1;
14783 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14784 c_mark_addressable (placeholder);
14785 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14786 c_mark_addressable (decl_placeholder ? decl_placeholder
14787 : OMP_CLAUSE_DECL (c));
14788 OMP_CLAUSE_REDUCTION_MERGE (c)
14789 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14790 TREE_VEC_ELT (list, 0),
14791 TREE_VEC_ELT (list, 1),
14792 decl_placeholder ? decl_placeholder
14793 : OMP_CLAUSE_DECL (c), placeholder);
14794 OMP_CLAUSE_REDUCTION_MERGE (c)
14795 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14796 void_type_node, NULL_TREE,
14797 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14798 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14799 if (TREE_VEC_LENGTH (list) == 6)
14801 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14802 c_mark_addressable (decl_placeholder ? decl_placeholder
14803 : OMP_CLAUSE_DECL (c));
14804 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14805 c_mark_addressable (placeholder);
14806 tree init = TREE_VEC_ELT (list, 5);
14807 if (init == error_mark_node)
14808 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14809 OMP_CLAUSE_REDUCTION_INIT (c)
14810 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14811 TREE_VEC_ELT (list, 3),
14812 decl_placeholder ? decl_placeholder
14813 : OMP_CLAUSE_DECL (c), placeholder);
14814 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14816 tree v = decl_placeholder ? decl_placeholder : t;
14817 OMP_CLAUSE_REDUCTION_INIT (c)
14818 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14819 OMP_CLAUSE_REDUCTION_INIT (c));
14821 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14822 c_find_omp_placeholder_r,
14823 placeholder, NULL))
14824 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14826 else
14828 tree init;
14829 tree v = decl_placeholder ? decl_placeholder : t;
14830 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14831 init = build_constructor (TREE_TYPE (v), NULL);
14832 else
14833 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14834 OMP_CLAUSE_REDUCTION_INIT (c)
14835 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14837 OMP_CLAUSE_REDUCTION_INIT (c)
14838 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14839 void_type_node, NULL_TREE,
14840 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14841 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14843 if (TREE_CODE (t) == MEM_REF)
14845 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14846 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14847 != INTEGER_CST)
14849 sorry ("variable length element type in array "
14850 "%<reduction%> clause");
14851 remove = true;
14852 break;
14854 t = TREE_OPERAND (t, 0);
14855 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14856 t = TREE_OPERAND (t, 0);
14857 if (TREE_CODE (t) == ADDR_EXPR)
14858 t = TREE_OPERAND (t, 0);
14860 goto check_dup_generic_t;
14862 case OMP_CLAUSE_COPYPRIVATE:
14863 copyprivate_seen = true;
14864 if (nowait_clause)
14866 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14867 "%<nowait%> clause must not be used together "
14868 "with %<copyprivate%>");
14869 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14870 nowait_clause = NULL;
14872 goto check_dup_generic;
14874 case OMP_CLAUSE_COPYIN:
14875 t = OMP_CLAUSE_DECL (c);
14876 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14878 error_at (OMP_CLAUSE_LOCATION (c),
14879 "%qE must be %<threadprivate%> for %<copyin%>", t);
14880 remove = true;
14881 break;
14883 goto check_dup_generic;
14885 case OMP_CLAUSE_LINEAR:
14886 if (ort != C_ORT_OMP_DECLARE_SIMD)
14887 need_implicitly_determined = true;
14888 t = OMP_CLAUSE_DECL (c);
14889 if (ort != C_ORT_OMP_DECLARE_SIMD
14890 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
14891 && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
14893 error_at (OMP_CLAUSE_LOCATION (c),
14894 "modifier should not be specified in %<linear%> "
14895 "clause on %<simd%> or %<for%> constructs when not "
14896 "using OpenMP 5.2 modifiers");
14897 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14899 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14900 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14902 error_at (OMP_CLAUSE_LOCATION (c),
14903 "linear clause applied to non-integral non-pointer "
14904 "variable with type %qT", TREE_TYPE (t));
14905 remove = true;
14906 break;
14908 if (TYPE_ATOMIC (TREE_TYPE (t)))
14910 error_at (OMP_CLAUSE_LOCATION (c),
14911 "%<_Atomic%> %qD in %<linear%> clause", t);
14912 remove = true;
14913 break;
14915 if (ort == C_ORT_OMP_DECLARE_SIMD)
14917 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14918 if (TREE_CODE (s) == PARM_DECL)
14920 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14921 /* map_head bitmap is used as uniform_head if
14922 declare_simd. */
14923 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14924 linear_variable_step_check = true;
14925 goto check_dup_generic;
14927 if (TREE_CODE (s) != INTEGER_CST)
14929 error_at (OMP_CLAUSE_LOCATION (c),
14930 "%<linear%> clause step %qE is neither constant "
14931 "nor a parameter", s);
14932 remove = true;
14933 break;
14936 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14938 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14939 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14940 OMP_CLAUSE_DECL (c), s);
14941 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14942 sizetype, fold_convert (sizetype, s),
14943 fold_convert
14944 (sizetype, OMP_CLAUSE_DECL (c)));
14945 if (s == error_mark_node)
14946 s = size_one_node;
14947 OMP_CLAUSE_LINEAR_STEP (c) = s;
14949 else
14950 OMP_CLAUSE_LINEAR_STEP (c)
14951 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14952 goto check_dup_generic;
14954 check_dup_generic:
14955 t = OMP_CLAUSE_DECL (c);
14956 check_dup_generic_t:
14957 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14959 error_at (OMP_CLAUSE_LOCATION (c),
14960 "%qE is not a variable in clause %qs", t,
14961 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14962 remove = true;
14964 else if ((ort == C_ORT_ACC
14965 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14966 || (ort == C_ORT_OMP
14967 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14968 || (OMP_CLAUSE_CODE (c)
14969 == OMP_CLAUSE_USE_DEVICE_ADDR)))
14970 || (ort == C_ORT_OMP_TARGET
14971 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
14973 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14974 && (bitmap_bit_p (&generic_head, DECL_UID (t))
14975 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
14977 error_at (OMP_CLAUSE_LOCATION (c),
14978 "%qD appears more than once in data-sharing "
14979 "clauses", t);
14980 remove = true;
14981 break;
14983 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
14984 target_in_reduction_seen = true;
14985 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14987 error_at (OMP_CLAUSE_LOCATION (c),
14988 ort == C_ORT_ACC
14989 ? "%qD appears more than once in reduction clauses"
14990 : "%qD appears more than once in data clauses",
14992 remove = true;
14994 else
14995 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14997 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14998 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14999 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
15000 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15002 error_at (OMP_CLAUSE_LOCATION (c),
15003 "%qE appears more than once in data clauses", t);
15004 remove = true;
15006 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15007 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
15008 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15009 && bitmap_bit_p (&map_head, DECL_UID (t)))
15011 if (ort == C_ORT_ACC)
15012 error_at (OMP_CLAUSE_LOCATION (c),
15013 "%qD appears more than once in data clauses", t);
15014 else
15015 error_at (OMP_CLAUSE_LOCATION (c),
15016 "%qD appears both in data and map clauses", t);
15017 remove = true;
15019 else
15020 bitmap_set_bit (&generic_head, DECL_UID (t));
15021 break;
15023 case OMP_CLAUSE_FIRSTPRIVATE:
15024 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
15026 move_implicit:
15027 implicit_moved = true;
15028 /* Move firstprivate and map clauses with
15029 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
15030 clauses chain. */
15031 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
15032 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
15033 while (*pc1)
15034 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
15035 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
15037 *pc3 = *pc1;
15038 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
15039 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
15041 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
15042 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
15044 *pc2 = *pc1;
15045 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
15046 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
15048 else
15049 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
15050 *pc3 = NULL;
15051 *pc2 = cl2;
15052 *pc1 = cl1;
15053 continue;
15055 t = OMP_CLAUSE_DECL (c);
15056 need_complete = true;
15057 need_implicitly_determined = true;
15058 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15060 error_at (OMP_CLAUSE_LOCATION (c),
15061 "%qE is not a variable in clause %<firstprivate%>", t);
15062 remove = true;
15064 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
15065 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
15066 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15067 remove = true;
15068 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15069 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15070 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15072 error_at (OMP_CLAUSE_LOCATION (c),
15073 "%qE appears more than once in data clauses", t);
15074 remove = true;
15076 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
15078 if (ort == C_ORT_ACC)
15079 error_at (OMP_CLAUSE_LOCATION (c),
15080 "%qD appears more than once in data clauses", t);
15081 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
15082 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
15083 /* Silently drop the clause. */;
15084 else
15085 error_at (OMP_CLAUSE_LOCATION (c),
15086 "%qD appears both in data and map clauses", t);
15087 remove = true;
15089 else
15090 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
15091 break;
15093 case OMP_CLAUSE_LASTPRIVATE:
15094 t = OMP_CLAUSE_DECL (c);
15095 need_complete = true;
15096 need_implicitly_determined = true;
15097 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15099 error_at (OMP_CLAUSE_LOCATION (c),
15100 "%qE is not a variable in clause %<lastprivate%>", t);
15101 remove = true;
15103 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15104 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
15106 error_at (OMP_CLAUSE_LOCATION (c),
15107 "%qE appears more than once in data clauses", t);
15108 remove = true;
15110 else
15111 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
15112 break;
15114 case OMP_CLAUSE_ALIGNED:
15115 t = OMP_CLAUSE_DECL (c);
15116 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15118 error_at (OMP_CLAUSE_LOCATION (c),
15119 "%qE is not a variable in %<aligned%> clause", t);
15120 remove = true;
15122 else if (!POINTER_TYPE_P (TREE_TYPE (t))
15123 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15125 error_at (OMP_CLAUSE_LOCATION (c),
15126 "%qE in %<aligned%> clause is neither a pointer nor "
15127 "an array", t);
15128 remove = true;
15130 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15132 error_at (OMP_CLAUSE_LOCATION (c),
15133 "%<_Atomic%> %qD in %<aligned%> clause", t);
15134 remove = true;
15135 break;
15137 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
15139 error_at (OMP_CLAUSE_LOCATION (c),
15140 "%qE appears more than once in %<aligned%> clauses",
15142 remove = true;
15144 else
15145 bitmap_set_bit (&aligned_head, DECL_UID (t));
15146 break;
15148 case OMP_CLAUSE_NONTEMPORAL:
15149 t = OMP_CLAUSE_DECL (c);
15150 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15152 error_at (OMP_CLAUSE_LOCATION (c),
15153 "%qE is not a variable in %<nontemporal%> clause", t);
15154 remove = true;
15156 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15158 error_at (OMP_CLAUSE_LOCATION (c),
15159 "%qE appears more than once in %<nontemporal%> "
15160 "clauses", t);
15161 remove = true;
15163 else
15164 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
15165 break;
15167 case OMP_CLAUSE_ALLOCATE:
15168 t = OMP_CLAUSE_DECL (c);
15169 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15171 error_at (OMP_CLAUSE_LOCATION (c),
15172 "%qE is not a variable in %<allocate%> clause", t);
15173 remove = true;
15175 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
15177 warning_at (OMP_CLAUSE_LOCATION (c), 0,
15178 "%qE appears more than once in %<allocate%> clauses",
15180 remove = true;
15182 else
15184 bitmap_set_bit (&aligned_head, DECL_UID (t));
15185 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
15186 allocate_seen = true;
15188 break;
15190 case OMP_CLAUSE_DOACROSS:
15191 t = OMP_CLAUSE_DECL (c);
15192 if (t == NULL_TREE)
15193 break;
15194 if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
15196 gcc_assert (TREE_CODE (t) == TREE_LIST);
15197 for (; t; t = TREE_CHAIN (t))
15199 tree decl = TREE_VALUE (t);
15200 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
15202 tree offset = TREE_PURPOSE (t);
15203 bool neg = wi::neg_p (wi::to_wide (offset));
15204 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
15205 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
15206 neg ? MINUS_EXPR : PLUS_EXPR,
15207 decl, offset);
15208 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
15209 sizetype,
15210 fold_convert (sizetype, t2),
15211 fold_convert (sizetype, decl));
15212 if (t2 == error_mark_node)
15214 remove = true;
15215 break;
15217 TREE_PURPOSE (t) = t2;
15220 break;
15222 gcc_unreachable ();
15223 case OMP_CLAUSE_DEPEND:
15224 case OMP_CLAUSE_AFFINITY:
15225 t = OMP_CLAUSE_DECL (c);
15226 if (TREE_CODE (t) == TREE_LIST
15227 && TREE_PURPOSE (t)
15228 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
15230 if (TREE_PURPOSE (t) != last_iterators)
15231 last_iterators_remove
15232 = c_omp_finish_iterators (TREE_PURPOSE (t));
15233 last_iterators = TREE_PURPOSE (t);
15234 t = TREE_VALUE (t);
15235 if (last_iterators_remove)
15236 t = error_mark_node;
15238 else
15239 last_iterators = NULL_TREE;
15240 if (TREE_CODE (t) == TREE_LIST)
15242 if (handle_omp_array_sections (c, ort))
15243 remove = true;
15244 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15245 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
15247 error_at (OMP_CLAUSE_LOCATION (c),
15248 "%<depend%> clause with %<depobj%> dependence "
15249 "type on array section");
15250 remove = true;
15252 break;
15254 if (t == error_mark_node)
15255 remove = true;
15256 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15257 && t == ridpointers[RID_OMP_ALL_MEMORY])
15259 if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
15260 && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
15262 error_at (OMP_CLAUSE_LOCATION (c),
15263 "%<omp_all_memory%> used with %<depend%> kind "
15264 "other than %<out%> or %<inout%>");
15265 remove = true;
15268 else if (!lvalue_p (t))
15270 error_at (OMP_CLAUSE_LOCATION (c),
15271 "%qE is not lvalue expression nor array section in "
15272 "%qs clause", t,
15273 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15274 remove = true;
15276 else if (TREE_CODE (t) == COMPONENT_REF
15277 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
15279 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15280 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
15281 error_at (OMP_CLAUSE_LOCATION (c),
15282 "bit-field %qE in %qs clause", t,
15283 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15284 remove = true;
15286 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15287 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
15289 if (!c_omp_depend_t_p (TREE_TYPE (t)))
15291 error_at (OMP_CLAUSE_LOCATION (c),
15292 "%qE does not have %<omp_depend_t%> type in "
15293 "%<depend%> clause with %<depobj%> dependence "
15294 "type", t);
15295 remove = true;
15298 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15299 && c_omp_depend_t_p (TREE_TYPE (t)))
15301 error_at (OMP_CLAUSE_LOCATION (c),
15302 "%qE should not have %<omp_depend_t%> type in "
15303 "%<depend%> clause with dependence type other than "
15304 "%<depobj%>", t);
15305 remove = true;
15307 if (!remove)
15309 if (t == ridpointers[RID_OMP_ALL_MEMORY])
15310 t = null_pointer_node;
15311 else
15313 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
15314 ADDR_EXPR, t, false);
15315 if (addr == error_mark_node)
15317 remove = true;
15318 break;
15320 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
15321 RO_UNARY_STAR);
15322 if (t == error_mark_node)
15324 remove = true;
15325 break;
15328 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
15329 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
15330 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
15331 == TREE_VEC))
15332 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
15333 else
15334 OMP_CLAUSE_DECL (c) = t;
15336 break;
15338 case OMP_CLAUSE_MAP:
15339 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
15340 goto move_implicit;
15341 /* FALLTHRU */
15342 case OMP_CLAUSE_TO:
15343 case OMP_CLAUSE_FROM:
15344 case OMP_CLAUSE__CACHE_:
15345 t = OMP_CLAUSE_DECL (c);
15346 if (TREE_CODE (t) == TREE_LIST)
15348 grp_start_p = pc;
15349 grp_sentinel = OMP_CLAUSE_CHAIN (c);
15351 if (handle_omp_array_sections (c, ort))
15352 remove = true;
15353 else
15355 t = OMP_CLAUSE_DECL (c);
15356 if (!omp_mappable_type (TREE_TYPE (t)))
15358 error_at (OMP_CLAUSE_LOCATION (c),
15359 "array section does not have mappable type "
15360 "in %qs clause",
15361 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15362 remove = true;
15364 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15366 error_at (OMP_CLAUSE_LOCATION (c),
15367 "%<_Atomic%> %qE in %qs clause", t,
15368 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15369 remove = true;
15371 while (TREE_CODE (t) == ARRAY_REF)
15372 t = TREE_OPERAND (t, 0);
15373 if (TREE_CODE (t) == COMPONENT_REF
15374 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
15378 t = TREE_OPERAND (t, 0);
15379 if (TREE_CODE (t) == MEM_REF
15380 || INDIRECT_REF_P (t))
15382 t = TREE_OPERAND (t, 0);
15383 STRIP_NOPS (t);
15384 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15385 t = TREE_OPERAND (t, 0);
15388 while (TREE_CODE (t) == COMPONENT_REF
15389 || TREE_CODE (t) == ARRAY_REF);
15391 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15392 && OMP_CLAUSE_MAP_IMPLICIT (c)
15393 && (bitmap_bit_p (&map_head, DECL_UID (t))
15394 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15395 || bitmap_bit_p (&map_firstprivate_head,
15396 DECL_UID (t))))
15398 remove = true;
15399 break;
15401 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
15402 break;
15403 if (bitmap_bit_p (&map_head, DECL_UID (t)))
15405 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15406 error_at (OMP_CLAUSE_LOCATION (c),
15407 "%qD appears more than once in motion "
15408 "clauses", t);
15409 else if (ort == C_ORT_ACC)
15410 error_at (OMP_CLAUSE_LOCATION (c),
15411 "%qD appears more than once in data "
15412 "clauses", t);
15413 else
15414 error_at (OMP_CLAUSE_LOCATION (c),
15415 "%qD appears more than once in map "
15416 "clauses", t);
15417 remove = true;
15419 else
15421 bitmap_set_bit (&map_head, DECL_UID (t));
15422 bitmap_set_bit (&map_field_head, DECL_UID (t));
15426 if (c_oacc_check_attachments (c))
15427 remove = true;
15428 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15429 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15430 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15431 /* In this case, we have a single array element which is a
15432 pointer, and we already set OMP_CLAUSE_SIZE in
15433 handle_omp_array_sections above. For attach/detach clauses,
15434 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
15435 here. */
15436 OMP_CLAUSE_SIZE (c) = size_zero_node;
15437 break;
15439 if (t == error_mark_node)
15441 remove = true;
15442 break;
15444 /* OpenACC attach / detach clauses must be pointers. */
15445 if (c_oacc_check_attachments (c))
15447 remove = true;
15448 break;
15450 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15451 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15452 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15453 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
15454 bias) to zero here, so it is not set erroneously to the pointer
15455 size later on in gimplify.cc. */
15456 OMP_CLAUSE_SIZE (c) = size_zero_node;
15457 while (INDIRECT_REF_P (t)
15458 || TREE_CODE (t) == ARRAY_REF)
15460 t = TREE_OPERAND (t, 0);
15461 STRIP_NOPS (t);
15462 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15463 t = TREE_OPERAND (t, 0);
15465 while (TREE_CODE (t) == COMPOUND_EXPR)
15467 t = TREE_OPERAND (t, 1);
15468 STRIP_NOPS (t);
15470 indir_component_ref_p = false;
15471 if (TREE_CODE (t) == COMPONENT_REF
15472 && (TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
15473 || INDIRECT_REF_P (TREE_OPERAND (t, 0))
15474 || TREE_CODE (TREE_OPERAND (t, 0)) == ARRAY_REF))
15476 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
15477 indir_component_ref_p = true;
15478 STRIP_NOPS (t);
15479 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15480 t = TREE_OPERAND (t, 0);
15483 if (TREE_CODE (t) == COMPONENT_REF
15484 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
15486 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
15488 error_at (OMP_CLAUSE_LOCATION (c),
15489 "bit-field %qE in %qs clause",
15490 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15491 remove = true;
15493 else if (!omp_mappable_type (TREE_TYPE (t)))
15495 error_at (OMP_CLAUSE_LOCATION (c),
15496 "%qE does not have a mappable type in %qs clause",
15497 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15498 remove = true;
15500 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15502 error_at (OMP_CLAUSE_LOCATION (c),
15503 "%<_Atomic%> %qE in %qs clause", t,
15504 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15505 remove = true;
15507 while (TREE_CODE (t) == COMPONENT_REF)
15509 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
15510 == UNION_TYPE)
15512 error_at (OMP_CLAUSE_LOCATION (c),
15513 "%qE is a member of a union", t);
15514 remove = true;
15515 break;
15517 t = TREE_OPERAND (t, 0);
15518 if (TREE_CODE (t) == MEM_REF)
15520 if (maybe_ne (mem_ref_offset (t), 0))
15521 error_at (OMP_CLAUSE_LOCATION (c),
15522 "cannot dereference %qE in %qs clause", t,
15523 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15524 else
15525 t = TREE_OPERAND (t, 0);
15527 while (TREE_CODE (t) == MEM_REF
15528 || INDIRECT_REF_P (t)
15529 || TREE_CODE (t) == ARRAY_REF)
15531 t = TREE_OPERAND (t, 0);
15532 STRIP_NOPS (t);
15533 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15534 t = TREE_OPERAND (t, 0);
15537 if (remove)
15538 break;
15539 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15541 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
15542 || (ort != C_ORT_ACC
15543 && bitmap_bit_p (&map_head, DECL_UID (t))))
15544 break;
15547 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15549 error_at (OMP_CLAUSE_LOCATION (c),
15550 "%qE is not a variable in %qs clause", t,
15551 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15552 remove = true;
15554 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15556 error_at (OMP_CLAUSE_LOCATION (c),
15557 "%qD is threadprivate variable in %qs clause", t,
15558 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15559 remove = true;
15561 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15562 || (OMP_CLAUSE_MAP_KIND (c)
15563 != GOMP_MAP_FIRSTPRIVATE_POINTER))
15564 && !indir_component_ref_p
15565 && !c_mark_addressable (t))
15566 remove = true;
15567 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15568 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
15569 || (OMP_CLAUSE_MAP_KIND (c)
15570 == GOMP_MAP_FIRSTPRIVATE_POINTER)
15571 || (OMP_CLAUSE_MAP_KIND (c)
15572 == GOMP_MAP_FORCE_DEVICEPTR)))
15573 && t == OMP_CLAUSE_DECL (c)
15574 && !omp_mappable_type (TREE_TYPE (t)))
15576 error_at (OMP_CLAUSE_LOCATION (c),
15577 "%qD does not have a mappable type in %qs clause", t,
15578 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15579 remove = true;
15581 else if (TREE_TYPE (t) == error_mark_node)
15582 remove = true;
15583 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15585 error_at (OMP_CLAUSE_LOCATION (c),
15586 "%<_Atomic%> %qE in %qs clause", t,
15587 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15588 remove = true;
15590 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15591 && OMP_CLAUSE_MAP_IMPLICIT (c)
15592 && (bitmap_bit_p (&map_head, DECL_UID (t))
15593 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15594 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))))
15595 remove = true;
15596 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15597 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
15599 if (bitmap_bit_p (&generic_head, DECL_UID (t))
15600 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15601 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15603 error_at (OMP_CLAUSE_LOCATION (c),
15604 "%qD appears more than once in data clauses", t);
15605 remove = true;
15607 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15608 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15610 if (ort == C_ORT_ACC)
15611 error_at (OMP_CLAUSE_LOCATION (c),
15612 "%qD appears more than once in data clauses", t);
15613 else
15614 error_at (OMP_CLAUSE_LOCATION (c),
15615 "%qD appears both in data and map clauses", t);
15616 remove = true;
15618 else
15619 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
15621 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15622 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15624 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15625 error_at (OMP_CLAUSE_LOCATION (c),
15626 "%qD appears more than once in motion clauses", t);
15627 else if (ort == C_ORT_ACC)
15628 error_at (OMP_CLAUSE_LOCATION (c),
15629 "%qD appears more than once in data clauses", t);
15630 else
15631 error_at (OMP_CLAUSE_LOCATION (c),
15632 "%qD appears more than once in map clauses", t);
15633 remove = true;
15635 else if (ort == C_ORT_ACC
15636 && bitmap_bit_p (&generic_head, DECL_UID (t)))
15638 error_at (OMP_CLAUSE_LOCATION (c),
15639 "%qD appears more than once in data clauses", t);
15640 remove = true;
15642 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15643 || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
15645 if (ort == C_ORT_ACC)
15646 error_at (OMP_CLAUSE_LOCATION (c),
15647 "%qD appears more than once in data clauses", t);
15648 else
15649 error_at (OMP_CLAUSE_LOCATION (c),
15650 "%qD appears both in data and map clauses", t);
15651 remove = true;
15653 else
15655 bitmap_set_bit (&map_head, DECL_UID (t));
15656 if (t != OMP_CLAUSE_DECL (c)
15657 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
15658 bitmap_set_bit (&map_field_head, DECL_UID (t));
15660 break;
15662 case OMP_CLAUSE_ENTER:
15663 case OMP_CLAUSE_LINK:
15664 t = OMP_CLAUSE_DECL (c);
15665 const char *cname;
15666 cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
15667 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
15668 && OMP_CLAUSE_ENTER_TO (c))
15669 cname = "to";
15670 if (TREE_CODE (t) == FUNCTION_DECL
15671 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15673 else if (!VAR_P (t))
15675 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15676 error_at (OMP_CLAUSE_LOCATION (c),
15677 "%qE is neither a variable nor a function name in "
15678 "clause %qs", t, cname);
15679 else
15680 error_at (OMP_CLAUSE_LOCATION (c),
15681 "%qE is not a variable in clause %qs", t, cname);
15682 remove = true;
15684 else if (DECL_THREAD_LOCAL_P (t))
15686 error_at (OMP_CLAUSE_LOCATION (c),
15687 "%qD is threadprivate variable in %qs clause", t,
15688 cname);
15689 remove = true;
15691 else if (!omp_mappable_type (TREE_TYPE (t)))
15693 error_at (OMP_CLAUSE_LOCATION (c),
15694 "%qD does not have a mappable type in %qs clause", t,
15695 cname);
15696 remove = true;
15698 if (remove)
15699 break;
15700 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15702 error_at (OMP_CLAUSE_LOCATION (c),
15703 "%qE appears more than once on the same "
15704 "%<declare target%> directive", t);
15705 remove = true;
15707 else
15708 bitmap_set_bit (&generic_head, DECL_UID (t));
15709 break;
15711 case OMP_CLAUSE_UNIFORM:
15712 t = OMP_CLAUSE_DECL (c);
15713 if (TREE_CODE (t) != PARM_DECL)
15715 if (DECL_P (t))
15716 error_at (OMP_CLAUSE_LOCATION (c),
15717 "%qD is not an argument in %<uniform%> clause", t);
15718 else
15719 error_at (OMP_CLAUSE_LOCATION (c),
15720 "%qE is not an argument in %<uniform%> clause", t);
15721 remove = true;
15722 break;
15724 /* map_head bitmap is used as uniform_head if declare_simd. */
15725 bitmap_set_bit (&map_head, DECL_UID (t));
15726 goto check_dup_generic;
15728 case OMP_CLAUSE_IS_DEVICE_PTR:
15729 case OMP_CLAUSE_USE_DEVICE_PTR:
15730 t = OMP_CLAUSE_DECL (c);
15731 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15732 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15733 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15735 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15736 && ort != C_ORT_ACC)
15738 error_at (OMP_CLAUSE_LOCATION (c),
15739 "%qs variable is not a pointer",
15740 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15741 remove = true;
15743 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15745 error_at (OMP_CLAUSE_LOCATION (c),
15746 "%qs variable is neither a pointer nor an array",
15747 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15748 remove = true;
15751 goto check_dup_generic;
15753 case OMP_CLAUSE_HAS_DEVICE_ADDR:
15754 t = OMP_CLAUSE_DECL (c);
15755 if (TREE_CODE (t) == TREE_LIST)
15757 if (handle_omp_array_sections (c, ort))
15758 remove = true;
15759 else
15761 t = OMP_CLAUSE_DECL (c);
15762 while (TREE_CODE (t) == ARRAY_REF)
15763 t = TREE_OPERAND (t, 0);
15766 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15767 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15768 c_mark_addressable (t);
15769 goto check_dup_generic_t;
15771 case OMP_CLAUSE_USE_DEVICE_ADDR:
15772 t = OMP_CLAUSE_DECL (c);
15773 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15774 c_mark_addressable (t);
15775 goto check_dup_generic;
15777 case OMP_CLAUSE_NOWAIT:
15778 if (copyprivate_seen)
15780 error_at (OMP_CLAUSE_LOCATION (c),
15781 "%<nowait%> clause must not be used together "
15782 "with %<copyprivate%>");
15783 remove = true;
15784 break;
15786 nowait_clause = pc;
15787 pc = &OMP_CLAUSE_CHAIN (c);
15788 continue;
15790 case OMP_CLAUSE_ORDER:
15791 if (ordered_clause)
15793 error_at (OMP_CLAUSE_LOCATION (c),
15794 "%<order%> clause must not be used together "
15795 "with %<ordered%>");
15796 remove = true;
15797 break;
15799 else if (order_clause)
15801 /* Silently remove duplicates. */
15802 remove = true;
15803 break;
15805 order_clause = pc;
15806 pc = &OMP_CLAUSE_CHAIN (c);
15807 continue;
15809 case OMP_CLAUSE_DETACH:
15810 t = OMP_CLAUSE_DECL (c);
15811 if (detach_seen)
15813 error_at (OMP_CLAUSE_LOCATION (c),
15814 "too many %qs clauses on a task construct",
15815 "detach");
15816 remove = true;
15817 break;
15819 detach_seen = pc;
15820 pc = &OMP_CLAUSE_CHAIN (c);
15821 c_mark_addressable (t);
15822 continue;
15824 case OMP_CLAUSE_IF:
15825 case OMP_CLAUSE_NUM_THREADS:
15826 case OMP_CLAUSE_NUM_TEAMS:
15827 case OMP_CLAUSE_THREAD_LIMIT:
15828 case OMP_CLAUSE_DEFAULT:
15829 case OMP_CLAUSE_UNTIED:
15830 case OMP_CLAUSE_COLLAPSE:
15831 case OMP_CLAUSE_FINAL:
15832 case OMP_CLAUSE_DEVICE:
15833 case OMP_CLAUSE_DIST_SCHEDULE:
15834 case OMP_CLAUSE_PARALLEL:
15835 case OMP_CLAUSE_FOR:
15836 case OMP_CLAUSE_SECTIONS:
15837 case OMP_CLAUSE_TASKGROUP:
15838 case OMP_CLAUSE_PROC_BIND:
15839 case OMP_CLAUSE_DEVICE_TYPE:
15840 case OMP_CLAUSE_PRIORITY:
15841 case OMP_CLAUSE_GRAINSIZE:
15842 case OMP_CLAUSE_NUM_TASKS:
15843 case OMP_CLAUSE_THREADS:
15844 case OMP_CLAUSE_SIMD:
15845 case OMP_CLAUSE_HINT:
15846 case OMP_CLAUSE_FILTER:
15847 case OMP_CLAUSE_DEFAULTMAP:
15848 case OMP_CLAUSE_BIND:
15849 case OMP_CLAUSE_NUM_GANGS:
15850 case OMP_CLAUSE_NUM_WORKERS:
15851 case OMP_CLAUSE_VECTOR_LENGTH:
15852 case OMP_CLAUSE_ASYNC:
15853 case OMP_CLAUSE_WAIT:
15854 case OMP_CLAUSE_AUTO:
15855 case OMP_CLAUSE_INDEPENDENT:
15856 case OMP_CLAUSE_SEQ:
15857 case OMP_CLAUSE_GANG:
15858 case OMP_CLAUSE_WORKER:
15859 case OMP_CLAUSE_VECTOR:
15860 case OMP_CLAUSE_TILE:
15861 case OMP_CLAUSE_IF_PRESENT:
15862 case OMP_CLAUSE_FINALIZE:
15863 case OMP_CLAUSE_NOHOST:
15864 pc = &OMP_CLAUSE_CHAIN (c);
15865 continue;
15867 case OMP_CLAUSE_MERGEABLE:
15868 mergeable_seen = true;
15869 pc = &OMP_CLAUSE_CHAIN (c);
15870 continue;
15872 case OMP_CLAUSE_NOGROUP:
15873 nogroup_seen = pc;
15874 pc = &OMP_CLAUSE_CHAIN (c);
15875 continue;
15877 case OMP_CLAUSE_SCHEDULE:
15878 schedule_clause = c;
15879 pc = &OMP_CLAUSE_CHAIN (c);
15880 continue;
15882 case OMP_CLAUSE_ORDERED:
15883 ordered_clause = c;
15884 if (order_clause)
15886 error_at (OMP_CLAUSE_LOCATION (*order_clause),
15887 "%<order%> clause must not be used together "
15888 "with %<ordered%>");
15889 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
15890 order_clause = NULL;
15892 pc = &OMP_CLAUSE_CHAIN (c);
15893 continue;
15895 case OMP_CLAUSE_SAFELEN:
15896 safelen = c;
15897 pc = &OMP_CLAUSE_CHAIN (c);
15898 continue;
15899 case OMP_CLAUSE_SIMDLEN:
15900 simdlen = c;
15901 pc = &OMP_CLAUSE_CHAIN (c);
15902 continue;
15904 case OMP_CLAUSE_INBRANCH:
15905 case OMP_CLAUSE_NOTINBRANCH:
15906 if (branch_seen)
15908 error_at (OMP_CLAUSE_LOCATION (c),
15909 "%<inbranch%> clause is incompatible with "
15910 "%<notinbranch%>");
15911 remove = true;
15912 break;
15914 branch_seen = true;
15915 pc = &OMP_CLAUSE_CHAIN (c);
15916 continue;
15918 case OMP_CLAUSE_INCLUSIVE:
15919 case OMP_CLAUSE_EXCLUSIVE:
15920 need_complete = true;
15921 need_implicitly_determined = true;
15922 t = OMP_CLAUSE_DECL (c);
15923 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15925 error_at (OMP_CLAUSE_LOCATION (c),
15926 "%qE is not a variable in clause %qs", t,
15927 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15928 remove = true;
15930 break;
15932 default:
15933 gcc_unreachable ();
15936 if (!remove)
15938 t = OMP_CLAUSE_DECL (c);
15940 if (need_complete)
15942 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15943 if (t == error_mark_node)
15944 remove = true;
15947 if (need_implicitly_determined)
15949 const char *share_name = NULL;
15951 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15952 share_name = "threadprivate";
15953 else switch (c_omp_predetermined_sharing (t))
15955 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15956 break;
15957 case OMP_CLAUSE_DEFAULT_SHARED:
15958 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15959 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15960 && c_omp_predefined_variable (t))
15961 /* The __func__ variable and similar function-local
15962 predefined variables may be listed in a shared or
15963 firstprivate clause. */
15964 break;
15965 share_name = "shared";
15966 break;
15967 case OMP_CLAUSE_DEFAULT_PRIVATE:
15968 share_name = "private";
15969 break;
15970 default:
15971 gcc_unreachable ();
15973 if (share_name)
15975 error_at (OMP_CLAUSE_LOCATION (c),
15976 "%qE is predetermined %qs for %qs",
15977 t, share_name,
15978 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15979 remove = true;
15981 else if (TREE_READONLY (t)
15982 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15983 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15985 error_at (OMP_CLAUSE_LOCATION (c),
15986 "%<const%> qualified %qE may appear only in "
15987 "%<shared%> or %<firstprivate%> clauses", t);
15988 remove = true;
15993 if (remove)
15995 if (grp_start_p)
15997 /* If we found a clause to remove, we want to remove the whole
15998 expanded group, otherwise gimplify
15999 (omp_resolve_clause_dependencies) can get confused. */
16000 *grp_start_p = grp_sentinel;
16001 pc = grp_start_p;
16002 grp_start_p = NULL;
16004 else
16005 *pc = OMP_CLAUSE_CHAIN (c);
16007 else
16008 pc = &OMP_CLAUSE_CHAIN (c);
16011 if (simdlen
16012 && safelen
16013 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
16014 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
16016 error_at (OMP_CLAUSE_LOCATION (simdlen),
16017 "%<simdlen%> clause value is bigger than "
16018 "%<safelen%> clause value");
16019 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
16020 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
16023 if (ordered_clause
16024 && schedule_clause
16025 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16026 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
16028 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
16029 "%<nonmonotonic%> schedule modifier specified together "
16030 "with %<ordered%> clause");
16031 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16032 = (enum omp_clause_schedule_kind)
16033 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16034 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
16037 if (reduction_seen < 0 && ordered_clause)
16039 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
16040 "%qs clause specified together with %<inscan%> "
16041 "%<reduction%> clause", "ordered");
16042 reduction_seen = -2;
16045 if (reduction_seen < 0 && schedule_clause)
16047 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
16048 "%qs clause specified together with %<inscan%> "
16049 "%<reduction%> clause", "schedule");
16050 reduction_seen = -2;
16053 if (linear_variable_step_check
16054 || reduction_seen == -2
16055 || allocate_seen
16056 || target_in_reduction_seen)
16057 for (pc = &clauses, c = clauses; c ; c = *pc)
16059 bool remove = false;
16060 if (allocate_seen)
16061 switch (OMP_CLAUSE_CODE (c))
16063 case OMP_CLAUSE_REDUCTION:
16064 case OMP_CLAUSE_IN_REDUCTION:
16065 case OMP_CLAUSE_TASK_REDUCTION:
16066 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
16068 t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
16069 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
16070 t = TREE_OPERAND (t, 0);
16071 if (TREE_CODE (t) == ADDR_EXPR
16072 || INDIRECT_REF_P (t))
16073 t = TREE_OPERAND (t, 0);
16074 if (DECL_P (t))
16075 bitmap_clear_bit (&aligned_head, DECL_UID (t));
16076 break;
16078 /* FALLTHRU */
16079 case OMP_CLAUSE_PRIVATE:
16080 case OMP_CLAUSE_FIRSTPRIVATE:
16081 case OMP_CLAUSE_LASTPRIVATE:
16082 case OMP_CLAUSE_LINEAR:
16083 if (DECL_P (OMP_CLAUSE_DECL (c)))
16084 bitmap_clear_bit (&aligned_head,
16085 DECL_UID (OMP_CLAUSE_DECL (c)));
16086 break;
16087 default:
16088 break;
16090 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
16091 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
16092 && !bitmap_bit_p (&map_head,
16093 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
16095 error_at (OMP_CLAUSE_LOCATION (c),
16096 "%<linear%> clause step is a parameter %qD not "
16097 "specified in %<uniform%> clause",
16098 OMP_CLAUSE_LINEAR_STEP (c));
16099 remove = true;
16101 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16102 && reduction_seen == -2)
16103 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
16104 if (target_in_reduction_seen
16105 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
16107 tree t = OMP_CLAUSE_DECL (c);
16108 while (handled_component_p (t)
16109 || INDIRECT_REF_P (t)
16110 || TREE_CODE (t) == ADDR_EXPR
16111 || TREE_CODE (t) == MEM_REF
16112 || TREE_CODE (t) == NON_LVALUE_EXPR)
16113 t = TREE_OPERAND (t, 0);
16114 if (DECL_P (t)
16115 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
16116 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
16119 if (remove)
16120 *pc = OMP_CLAUSE_CHAIN (c);
16121 else
16122 pc = &OMP_CLAUSE_CHAIN (c);
16125 if (allocate_seen)
16126 for (pc = &clauses, c = clauses; c ; c = *pc)
16128 bool remove = false;
16129 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
16130 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
16131 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
16133 error_at (OMP_CLAUSE_LOCATION (c),
16134 "%qD specified in %<allocate%> clause but not in "
16135 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
16136 remove = true;
16138 if (remove)
16139 *pc = OMP_CLAUSE_CHAIN (c);
16140 else
16141 pc = &OMP_CLAUSE_CHAIN (c);
16144 if (nogroup_seen && reduction_seen)
16146 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
16147 "%<nogroup%> clause must not be used together with "
16148 "%<reduction%> clause");
16149 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
16152 if (detach_seen)
16154 if (mergeable_seen)
16156 error_at (OMP_CLAUSE_LOCATION (*detach_seen),
16157 "%<detach%> clause must not be used together with "
16158 "%<mergeable%> clause");
16159 *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
16161 else
16163 tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
16165 for (pc = &clauses, c = clauses; c ; c = *pc)
16167 bool remove = false;
16168 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
16169 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
16170 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
16171 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
16172 && OMP_CLAUSE_DECL (c) == detach_decl)
16174 error_at (OMP_CLAUSE_LOCATION (c),
16175 "the event handle of a %<detach%> clause "
16176 "should not be in a data-sharing clause");
16177 remove = true;
16179 if (remove)
16180 *pc = OMP_CLAUSE_CHAIN (c);
16181 else
16182 pc = &OMP_CLAUSE_CHAIN (c);
16187 bitmap_obstack_release (NULL);
16188 return clauses;
16191 /* Return code to initialize DST with a copy constructor from SRC.
16192 C doesn't have copy constructors nor assignment operators, only for
16193 _Atomic vars we need to perform __atomic_load from src into a temporary
16194 followed by __atomic_store of the temporary to dst. */
16196 tree
16197 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
16199 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
16200 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
16202 location_t loc = OMP_CLAUSE_LOCATION (clause);
16203 tree type = TREE_TYPE (dst);
16204 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
16205 tree tmp = create_tmp_var (nonatomic_type);
16206 tree tmp_addr = build_fold_addr_expr (tmp);
16207 TREE_ADDRESSABLE (tmp) = 1;
16208 suppress_warning (tmp);
16209 tree src_addr = build_fold_addr_expr (src);
16210 tree dst_addr = build_fold_addr_expr (dst);
16211 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
16212 vec<tree, va_gc> *params;
16213 /* Expansion of a generic atomic load may require an addition
16214 element, so allocate enough to prevent a resize. */
16215 vec_alloc (params, 4);
16217 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
16218 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
16219 params->quick_push (src_addr);
16220 params->quick_push (tmp_addr);
16221 params->quick_push (seq_cst);
16222 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
16224 vec_alloc (params, 4);
16226 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
16227 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
16228 params->quick_push (dst_addr);
16229 params->quick_push (tmp_addr);
16230 params->quick_push (seq_cst);
16231 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
16232 return build2 (COMPOUND_EXPR, void_type_node, load, store);
16235 /* Create a transaction node. */
16237 tree
16238 c_finish_transaction (location_t loc, tree block, int flags)
16240 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
16241 if (flags & TM_STMT_ATTR_OUTER)
16242 TRANSACTION_EXPR_OUTER (stmt) = 1;
16243 if (flags & TM_STMT_ATTR_RELAXED)
16244 TRANSACTION_EXPR_RELAXED (stmt) = 1;
16245 return add_stmt (stmt);
16248 /* Make a variant type in the proper way for C/C++, propagating qualifiers
16249 down to the element type of an array. If ORIG_QUAL_TYPE is not
16250 NULL, then it should be used as the qualified type
16251 ORIG_QUAL_INDIRECT levels down in array type derivation (to
16252 preserve information about the typedef name from which an array
16253 type was derived). */
16255 tree
16256 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
16257 size_t orig_qual_indirect)
16259 if (type == error_mark_node)
16260 return type;
16262 if (TREE_CODE (type) == ARRAY_TYPE)
16264 tree t;
16265 tree element_type = c_build_qualified_type (TREE_TYPE (type),
16266 type_quals, orig_qual_type,
16267 orig_qual_indirect - 1);
16269 /* See if we already have an identically qualified type. */
16270 if (orig_qual_type && orig_qual_indirect == 0)
16271 t = orig_qual_type;
16272 else
16273 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
16275 if (TYPE_QUALS (strip_array_types (t)) == type_quals
16276 && TYPE_NAME (t) == TYPE_NAME (type)
16277 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
16278 && attribute_list_equal (TYPE_ATTRIBUTES (t),
16279 TYPE_ATTRIBUTES (type)))
16280 break;
16282 if (!t)
16284 tree domain = TYPE_DOMAIN (type);
16286 t = build_variant_type_copy (type);
16287 TREE_TYPE (t) = element_type;
16289 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
16290 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
16291 SET_TYPE_STRUCTURAL_EQUALITY (t);
16292 else if (TYPE_CANONICAL (element_type) != element_type
16293 || (domain && TYPE_CANONICAL (domain) != domain))
16295 tree unqualified_canon
16296 = build_array_type (TYPE_CANONICAL (element_type),
16297 domain? TYPE_CANONICAL (domain)
16298 : NULL_TREE);
16299 if (TYPE_REVERSE_STORAGE_ORDER (type))
16301 unqualified_canon
16302 = build_distinct_type_copy (unqualified_canon);
16303 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
16305 TYPE_CANONICAL (t)
16306 = c_build_qualified_type (unqualified_canon, type_quals);
16308 else
16309 TYPE_CANONICAL (t) = t;
16311 return t;
16314 /* A restrict-qualified pointer type must be a pointer to object or
16315 incomplete type. Note that the use of POINTER_TYPE_P also allows
16316 REFERENCE_TYPEs, which is appropriate for C++. */
16317 if ((type_quals & TYPE_QUAL_RESTRICT)
16318 && (!POINTER_TYPE_P (type)
16319 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
16321 error ("invalid use of %<restrict%>");
16322 type_quals &= ~TYPE_QUAL_RESTRICT;
16325 tree var_type = (orig_qual_type && orig_qual_indirect == 0
16326 ? orig_qual_type
16327 : build_qualified_type (type, type_quals));
16328 /* A variant type does not inherit the list of incomplete vars from the
16329 type main variant. */
16330 if ((RECORD_OR_UNION_TYPE_P (var_type)
16331 || TREE_CODE (var_type) == ENUMERAL_TYPE)
16332 && TYPE_MAIN_VARIANT (var_type) != var_type)
16333 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
16334 return var_type;
16337 /* Build a VA_ARG_EXPR for the C parser. */
16339 tree
16340 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
16342 if (error_operand_p (type))
16343 return error_mark_node;
16344 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
16345 order because it takes the address of the expression. */
16346 else if (handled_component_p (expr)
16347 && reverse_storage_order_for_component_p (expr))
16349 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
16350 return error_mark_node;
16352 else if (!COMPLETE_TYPE_P (type))
16354 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
16355 "type %qT", type);
16356 return error_mark_node;
16358 else if (TREE_CODE (type) == FUNCTION_TYPE)
16360 error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
16361 type);
16362 return error_mark_node;
16364 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
16365 warning_at (loc2, OPT_Wc___compat,
16366 "C++ requires promoted type, not enum type, in %<va_arg%>");
16367 return build_va_arg (loc2, expr, type);
16370 /* Return truthvalue of whether T1 is the same tree structure as T2.
16371 Return 1 if they are the same. Return false if they are different. */
16373 bool
16374 c_tree_equal (tree t1, tree t2)
16376 enum tree_code code1, code2;
16378 if (t1 == t2)
16379 return true;
16380 if (!t1 || !t2)
16381 return false;
16383 for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
16384 code1 = TREE_CODE (t1))
16385 t1 = TREE_OPERAND (t1, 0);
16386 for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
16387 code2 = TREE_CODE (t2))
16388 t2 = TREE_OPERAND (t2, 0);
16390 /* They might have become equal now. */
16391 if (t1 == t2)
16392 return true;
16394 if (code1 != code2)
16395 return false;
16397 if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
16398 return false;
16400 switch (code1)
16402 case INTEGER_CST:
16403 return wi::to_wide (t1) == wi::to_wide (t2);
16405 case REAL_CST:
16406 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
16408 case STRING_CST:
16409 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
16410 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
16411 TREE_STRING_LENGTH (t1));
16413 case FIXED_CST:
16414 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
16415 TREE_FIXED_CST (t2));
16417 case COMPLEX_CST:
16418 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
16419 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
16421 case VECTOR_CST:
16422 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
16424 case CONSTRUCTOR:
16425 /* We need to do this when determining whether or not two
16426 non-type pointer to member function template arguments
16427 are the same. */
16428 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
16429 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
16430 return false;
16432 tree field, value;
16433 unsigned int i;
16434 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
16436 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
16437 if (!c_tree_equal (field, elt2->index)
16438 || !c_tree_equal (value, elt2->value))
16439 return false;
16442 return true;
16444 case TREE_LIST:
16445 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
16446 return false;
16447 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
16448 return false;
16449 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
16451 case SAVE_EXPR:
16452 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16454 case CALL_EXPR:
16456 tree arg1, arg2;
16457 call_expr_arg_iterator iter1, iter2;
16458 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
16459 return false;
16460 for (arg1 = first_call_expr_arg (t1, &iter1),
16461 arg2 = first_call_expr_arg (t2, &iter2);
16462 arg1 && arg2;
16463 arg1 = next_call_expr_arg (&iter1),
16464 arg2 = next_call_expr_arg (&iter2))
16465 if (!c_tree_equal (arg1, arg2))
16466 return false;
16467 if (arg1 || arg2)
16468 return false;
16469 return true;
16472 case TARGET_EXPR:
16474 tree o1 = TREE_OPERAND (t1, 0);
16475 tree o2 = TREE_OPERAND (t2, 0);
16477 /* Special case: if either target is an unallocated VAR_DECL,
16478 it means that it's going to be unified with whatever the
16479 TARGET_EXPR is really supposed to initialize, so treat it
16480 as being equivalent to anything. */
16481 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
16482 && !DECL_RTL_SET_P (o1))
16483 /*Nop*/;
16484 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
16485 && !DECL_RTL_SET_P (o2))
16486 /*Nop*/;
16487 else if (!c_tree_equal (o1, o2))
16488 return false;
16490 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
16493 case COMPONENT_REF:
16494 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
16495 return false;
16496 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16498 case PARM_DECL:
16499 case VAR_DECL:
16500 case CONST_DECL:
16501 case FIELD_DECL:
16502 case FUNCTION_DECL:
16503 case IDENTIFIER_NODE:
16504 case SSA_NAME:
16505 return false;
16507 case TREE_VEC:
16509 unsigned ix;
16510 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
16511 return false;
16512 for (ix = TREE_VEC_LENGTH (t1); ix--;)
16513 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
16514 TREE_VEC_ELT (t2, ix)))
16515 return false;
16516 return true;
16519 CASE_CONVERT:
16520 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
16521 return false;
16522 break;
16524 default:
16525 break;
16528 switch (TREE_CODE_CLASS (code1))
16530 case tcc_unary:
16531 case tcc_binary:
16532 case tcc_comparison:
16533 case tcc_expression:
16534 case tcc_vl_exp:
16535 case tcc_reference:
16536 case tcc_statement:
16538 int i, n = TREE_OPERAND_LENGTH (t1);
16540 switch (code1)
16542 case PREINCREMENT_EXPR:
16543 case PREDECREMENT_EXPR:
16544 case POSTINCREMENT_EXPR:
16545 case POSTDECREMENT_EXPR:
16546 n = 1;
16547 break;
16548 case ARRAY_REF:
16549 n = 2;
16550 break;
16551 default:
16552 break;
16555 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
16556 && n != TREE_OPERAND_LENGTH (t2))
16557 return false;
16559 for (i = 0; i < n; ++i)
16560 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
16561 return false;
16563 return true;
16566 case tcc_type:
16567 return comptypes (t1, t2);
16568 default:
16569 gcc_unreachable ();
16573 /* Returns true when the function declaration FNDECL is implicit,
16574 introduced as a result of a call to an otherwise undeclared
16575 function, and false otherwise. */
16577 bool
16578 c_decl_implicit (const_tree fndecl)
16580 return C_DECL_IMPLICIT (fndecl);